From: Shamus Hammons Date: Wed, 5 Apr 2017 01:29:14 +0000 (-0500) Subject: Added version which conforms to openrc coding guidelines. X-Git-Url: http://shamusworld.gotdns.org/cgi-bin/gitweb.cgi?p=init-ng;a=commitdiff_plain;h=b607d974fb798903921fc2c1df05c89a1ae73307 Added version which conforms to openrc coding guidelines. --- diff --git a/init-ng.c b/init-ng.c index cc51777..7dba91a 100644 --- a/init-ng.c +++ b/init-ng.c @@ -38,7 +38,7 @@ static char * args[256]; // 256 arguments ought to be enough for anybody ;-) // Find a process in the monitored process list (MPL) by PID. Returns the index // of the entry in the MPL. // -int FindMonitoredPID(pid_t pid) +static int FindMonitoredPID(pid_t pid) { for(int i=0; i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static const char fifoPath[] = "/dev/initctl"; +static int shuttingDown = 0; +static int verbose = 0; /* N.B.: There is no mechanism to set this ATM */ +static struct sigaction action; +static int freeProcessSlot = 0; +static pid_t processPID[1024]; +static char * processCmd[1024]; +static char * processName[1024]; +static char * args[256]; /* 256 arguments ought to be enough for anybody ;-) */ + + +/* + * Find a process in the monitored process list (MPL) by PID. Returns the index + * of the entry in the MPL. + */ +static int find_monitored_pid(pid_t pid) +{ + int i; + + for(i=0; i 1) + { + if ((strcmp(argv[1], "poweroff") == 0) + || (strcmp(argv[1], "restart") == 0) + || (strcmp(argv[1], "halt") == 0) + || (strcmp(argv[1], "status") == 0)) + { + do_cmd(argv[1]); + } + else if (strcmp(argv[1], "test") == 0) + { + shut_down(RB_POWER_OFF); + } + else + printf("Invalid command\n"); + } + else + printf("Usage: poweroff, restart, halt, status, test\n"); + + return 1; + } + + /* We are PID EINS, so start the system using OpenRC... */ + init(); + + /* Launch TTYs... + * N.B.: To be a drop in replacement for SysV init, we really should read + * and parse the entries in /etc/inittab; at least the c entries... + */ + for(i=1; i<=6; i++) + { + sprintf(buf, "agetty%i", i); + sprintf(buf2, "/sbin/agetty tty%i --noclear", i); + launch(buf, buf2, 1); + } + + /* Install SIGCHLD signal handler to do process monitoring */ + memset(&action, 0, sizeof(action)); + action.sa_handler = handle_sigchld; + action.sa_flags = SA_SIGINFO; + sigaction(SIGCHLD, &action, NULL); + + /* Create a named pipe to listen on for commands */ + if (mkfifo(fifoPath, 0600) == -1) + { + /* This is bad... If this happens, we have no comms */ + perror("mkfifo"); + } + + /* Main loop for PID 1. All commands are received and acted upon from here. */ + while (1) + { + /* This will block until a command is sent down the pipe... */ + file = fopen(fifoPath, "r"); + + if (file == NULL) + { + /* Check to see if the signal handler blew things up on us */ + if (errno == EINTR) + continue; + + perror("fopen"); + continue; + } + + /* Grab whatever was shoved into the FIFO */ + read = fread(buf, 1, 2047, file); + buf[read] = 0; + fclose(file); + + if (verbose) + printf("PID1: Received \"%s\" from FIFO...\n", buf); + + /* Now do something with it... */ + if (strcmp(buf, "poweroff") == 0) + { + shut_down(RB_POWER_OFF); + } + else if (strcmp(buf, "restart") == 0) + { + shut_down(RB_AUTOBOOT); + } + else if (strcmp(buf, "halt") == 0) + { + shut_down(RB_HALT_SYSTEM); + } + else if (strcmp(buf, "status") == 0) + { + printf("[Status placeholder]\n"); + } + } + + /* We should never get here... */ + return 0; +} +