/* Custom /sbin/init for diskless Free/Net/OpenBSD test system */ #include #include #include #include #include #include #ifdef __FreeBSD__ #include #else #include #endif #define CONSOLE "/dev/console" void errorexit(char *proc) { syslog(LOG_ERR, "ERROR: %s() returned %s", proc, strerror(errno)); exit(1); } void main(int argc, char *argv[]) { int status; int f; openlog("init", LOG_CONS|LOG_ODELAY, LOG_AUTH); status = setsid(); if (status < 0) errorexit("setsid"); status = setlogin("root"); if (status < 0) errorexit("setlogin"); /* Open stdin, stdout, stderr */ status = revoke(CONSOLE); if (status < 0) errorexit("revoke"); f = open(CONSOLE, O_RDWR, 0); if (f < 0) errorexit("open"); status = login_tty(f); if (status < 0) errorexit("login_tty"); /* Set default PATH */ putenv("PATH=/bin:/sbin"); /* Run /etc/rc */ system("/etc/rc"); /* Start a command shell */ for (;;) { status = system("exec -sh"); if (status == 9) sleep(60); // Happens on halt or reboot if (status == 15) sleep(60); // Happens on halt or reboot } }