fiss-minit

A standalone service supervisor based on minit
Log | Files | Refs | README | LICENSE

commit 602b3468ce483ec938a050ee10f52924cd9f460d
parent 15fa8d79685fdc2290275ec5ea4e4528cb88903f
Author: Friedel Schön <[email protected]>
Date:   Tue, 14 May 2024 16:41:07 +0200

removing i_am_init as it will not run as init

Diffstat:
Mminit.c | 103+++++++++++++------------------------------------------------------------------
1 file changed, 17 insertions(+), 86 deletions(-)

diff --git a/minit.c b/minit.c @@ -27,7 +27,7 @@ static struct process* root; static int maxprocess = -1; static int processalloc; -char** Argv; +static char** Argv; #undef printf extern int printf(const char* format, ...); @@ -39,20 +39,16 @@ extern void opendevconsole(); static int doupdate; #endif -static int i_am_init; - extern int openreadclose(char* fn, char** buf, size_t* len); extern char** split(char* buf, int c, size_t* len, size_t plus, size_t ofs); -extern char** environ; - #define HISTORY 10 #ifdef HISTORY -int history[HISTORY]; +static int history[HISTORY]; #endif /* return index of service in process data structure or -1 if not found */ -int findservice(char* service) { +static int findservice(char* service) { int i; for (i = 0; i <= maxprocess; ++i) { if (!strcmp(root[i].name, service)) @@ -62,7 +58,7 @@ int findservice(char* service) { } /* look up process index in data structure by PID */ -int findbypid(pid_t pid) { +static int findbypid(pid_t pid) { int i; for (i = 0; i <= maxprocess; ++i) { if (root[i].pid == pid) @@ -72,14 +68,14 @@ int findbypid(pid_t pid) { } /* clear circular dependency detection flags */ -void circsweep() { +static void circsweep() { int i; for (i = 0; i <= maxprocess; ++i) root[i].circular = 0; } /* add process to data structure, return index or -1 */ -int addprocess(struct process* p) { +static int addprocess(struct process* p) { if (maxprocess + 1 >= processalloc) { struct process* fump; processalloc += 8; @@ -92,7 +88,7 @@ int addprocess(struct process* p) { /* load a service into the process data structure and return index or -1 * if failed */ -int loadservice(char* service) { +static int loadservice(char* service) { struct process tmp; int fd; if (*service == 0) return -1; @@ -130,22 +126,15 @@ int loadservice(char* service) { /* usage: isup(findservice("sshd")). * returns nonzero if process is up */ -int isup(int service) { +static int isup(int service) { if (service < 0) return 0; return (root[service].pid != 0); } -int startservice(int service, int pause, int father); - -void sulogin() { /* exiting on an initialization failure is not a good idea for init */ - char* argv[] = { "sulogin", 0 }; - if (i_am_init) - execve("/sbin/sulogin", argv, environ); - _exit(1); -} +static int startservice(int service, int pause, int father); #undef debug -void handlekilled(pid_t killed) { +static void handlekilled(pid_t killed) { int i; #ifdef debug { @@ -160,10 +149,7 @@ void handlekilled(pid_t killed) { write(2, "all services exited.\n", 21); saidso = 1; } - if (i_am_init) - sulogin(); - else - exit(0); + exit(0); } if (killed == 0) return; i = findbypid(killed); @@ -180,7 +166,7 @@ void handlekilled(pid_t killed) { } /* called from inside the service directory, return the PID or 0 on error */ -pid_t forkandexec(int pause, int service) { +static pid_t forkandexec(int pause, int service) { char** argv = 0; int count = 0; pid_t p; @@ -199,37 +185,6 @@ again: case 0: /* child */ - if (i_am_init) { - ioctl(0, TIOCNOTTY, 0); - if (setsid() == -1) { - __write2("setsid failed unexpectedly.\n"); - // This should never fail. init is run as root. - // If it does fail, don't exit for fear of bricking the system - } - opendevconsole(); - /* ioctl(0, TIOCSCTTY, 1); */ - int r = tcsetpgrp(0, getpgrp()); - if (r == -1 && errno != ENOTTY) { // will get this error for log services - __write2("tcsetpgrp failed unexpectedly: "); - switch (errno) { - case EBADF: - __write2("EBADF\n"); - break; - case EINVAL: - __write2("EINVAL\n"); - break; - // case ENOTTY: __write2("ENOTTY\n"); break; - case EPERM: - __write2("EPERM\n"); - break; - default: - __write2("unhandled\n"); - } - // This should never fail. init is run as root. - // If it does fail, don't exit for fear of bricking the system - } - } - if (pause) { struct timespec req; req.tv_sec = 0; @@ -342,7 +297,7 @@ again: } /* start a service, return nonzero on error */ -int startnodep(int service, int pause) { +static int startnodep(int service, int pause) { /* step 1: see if the process is already up */ if (isup(service)) return 0; /* step 2: fork and exec service, put PID in data structure */ @@ -412,7 +367,7 @@ static void _puts(const char* s) { write(1, s, strlen(s)); } -void childhandler() { +static void childhandler() { int status; pid_t killed; #ifdef debug @@ -431,15 +386,7 @@ void childhandler() { static volatile int dowinch = 0; static volatile int doint = 0; -void sigchild(int sig) { (void) sig; } -void sigwinch(int sig) { - (void) sig; - dowinch = 1; -} -void sigint(int sig) { - (void) sig; - doint = 1; -} +static void sigchild(int sig) { (void) sig; } int main(int argc, char* argv[]) { /* Schritt 1: argv[1] als Service nehmen und starten */ @@ -460,16 +407,6 @@ int main(int argc, char* argv[]) { infd = open(MINITROOT "/in", O_RDWR); outfd = open(MINITROOT "/out", O_RDWR | O_NONBLOCK); - if (getpid() == 1) { - int fd; - i_am_init = 1; - reboot(RB_DISABLE_CAD); - if ((fd = open("/dev/console", O_RDWR | O_NOCTTY)) != -1) { - ioctl(fd, KDSIGACCEPT, SIGWINCH); - close(fd); - } else - ioctl(0, KDSIGACCEPT, SIGWINCH); - } /* signal(SIGPWR,sighandler); don't know what to do about it */ /* signal(SIGHUP,sighandler); ??? */ { @@ -481,18 +418,12 @@ int main(int argc, char* argv[]) { sa.sa_handler = sigchild; sigaction(SIGCHLD, &sa, 0); sa.sa_flags = SA_RESTART; - if (i_am_init) { - sa.sa_handler = sigint; - sigaction(SIGINT, &sa, 0); /* ctrl-alt-del */ - sa.sa_handler = sigwinch; - sigaction(SIGWINCH, &sa, 0); /* keyboard request */ - } if (errno) _puts("sigaction failed!\n"); } if (infd < 0 || outfd < 0) { _puts("minit: could not open " MINITROOT "/in or " MINITROOT "/out\n"); - sulogin(); + exit(1); nfds = 0; } else pfd.fd = infd; @@ -554,7 +485,7 @@ int main(int argc, char* argv[]) { } opendevconsole(); _puts("poll failed!\n"); - sulogin(); + exit(1); /* what should we do if poll fails?! */ break; case 1: