From: Shamus Hammons Date: Wed, 5 Apr 2017 00:51:33 +0000 (-0500) Subject: Switch out sockets IPC for a named pipe. Just like SysV does. X-Git-Url: http://shamusworld.gotdns.org/cgi-bin/gitweb.cgi?p=init-ng;a=commitdiff_plain;h=24d7b8d20f83ddaa75876b1479687fef86e867a3 Switch out sockets IPC for a named pipe. Just like SysV does. --- diff --git a/Makefile b/Makefile index 561f3c3..b6f94cc 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ # Doesn't need to be more complex than this init-ng: init-ng.c - gcc -std=c99 -g init-ng.c -o init-ng + gcc -std=c99 -g -D_DEFAULT_SOURCE init-ng.c -o init-ng diff --git a/init-ng.c b/init-ng.c index d161b84..cc51777 100644 --- a/init-ng.c +++ b/init-ng.c @@ -10,7 +10,6 @@ // (C) 2017 Underground Software // -#define _DEFAULT_SOURCE // Without this, functions in unistd.h go missing #include #include #include @@ -19,12 +18,12 @@ #include #include #include -#include +#include #include #include #include -static const char socketPath[] = "/run/initctl"; +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; @@ -54,7 +53,7 @@ int FindMonitoredPID(pid_t pid) // // Remove the given process from the MPL by index // -void RemoveEntry(int entry) +static void RemoveEntry(int entry) { free(processCmd[entry]); free(processName[entry]); @@ -68,7 +67,7 @@ void RemoveEntry(int entry) // // Add a process to the MPL // -void AddEntry(pid_t pid, char * name, char * command) +static void AddEntry(pid_t pid, char * name, char * command) { int i; @@ -91,7 +90,7 @@ void AddEntry(pid_t pid, char * name, char * command) // Take a given space delimited string and turn it into an array of strings // suitable for execv(). // -void MakeArgumentList(char * s) +static void MakeArgumentList(char * s) { char buf[4096]; char * tokSave; @@ -120,7 +119,7 @@ void MakeArgumentList(char * s) // monitor: Tells Launch whether or not this process should be monitored // and respawned if it goes away (0 = no, 1 = yes) // -pid_t Launch(char * name, char * command, int monitor) +static pid_t Launch(char * name, char * command, int monitor) { if (verbose) printf("Starting %s (%smonitored)...\n", name, (monitor ? "" : "un")); @@ -158,7 +157,7 @@ pid_t Launch(char * name, char * command, int monitor) // At some point, we'll read inittab instead of recording just our own internal // processes... // -void HandleSIGCHLD(int signum, siginfo_t * info, void * ptr) +static void HandleSIGCHLD(int signum) { while (1) { @@ -196,9 +195,9 @@ void HandleSIGCHLD(int signum, siginfo_t * info, void * ptr) // // Start up the system, using the awesome power of OpenRC // -void Init(void) +static void Init(void) { - printf("*** init-ng v1.0.0 starting...\n"); + printf("*** init-ng v1.0.1 starting...\n"); pid_t pid = Launch("openrc", "/sbin/rc sysinit", 0); waitpid(pid, NULL, 0); @@ -211,7 +210,7 @@ void Init(void) } -void ShutDown(int cmd) +static void ShutDown(int cmd) { shuttingDown = 1; pid_t pid = Launch("openrc", "/sbin/rc reboot", 0); @@ -227,34 +226,20 @@ void ShutDown(int cmd) // -// Connect to PID 1's socket to send a command +// Connect to PID 1's FIFO to send a command // -void DoCmd(char * cmd) +static void DoCmd(char * cmd) { - struct sockaddr_un remote; - int s = socket(AF_UNIX, SOCK_STREAM, 0); + FILE * file = fopen(fifoPath, "w"); - if (s == -1) + if (file == NULL) { - // Handle error - printf("Could not get socket in DoCmd()...\n"); + perror("DoCmd: fopen"); return; } - remote.sun_family = AF_UNIX; - strcpy(remote.sun_path, socketPath); - int len = strlen(remote.sun_path) + sizeof(remote.sun_family); - int s2 = connect(s, (struct sockaddr *)&remote, len); - - if (s2 == -1) - { - // Handle error - printf("Could not connect in DoCmd()...\n"); - return; - } - - send(s, cmd, strlen(cmd), 0); - close(s); + size_t ignored = fwrite(cmd, 1, strlen(cmd), file); + fclose(file); } @@ -303,61 +288,40 @@ int main(int argc, char * argv[]) // Install SIGCHLD signal handler to do process monitoring memset(&action, 0, sizeof(action)); - action.sa_sigaction = HandleSIGCHLD; + action.sa_handler = HandleSIGCHLD; action.sa_flags = SA_SIGINFO; sigaction(SIGCHLD, &action, NULL); - // Create socket to listen on for commands... - struct sockaddr_un local, remote; - unsigned int s = socket(AF_UNIX, SOCK_STREAM, 0); - - if (s == -1) + // Create a named pipe to listen on for commands + if (mkfifo(fifoPath, 0600) == -1) { - perror("socket"); - printf("init-ng: socket() failed!\n"); - } - - local.sun_family = AF_UNIX; - strcpy(local.sun_path, socketPath); - unlink(local.sun_path); - int len = strlen(local.sun_path) + sizeof(local.sun_family); - - if (bind(s, (struct sockaddr *)&local, len) == -1) - { - perror("bind"); - printf("init-ng: bind() failed!\n"); - } - - // Queue size of 20 ought to be enough for anybody ;-) - if (listen(s, 20) == -1) - { - perror("listen"); - printf("init-ng: listen() failed!\n"); + // 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) { - int t = sizeof(remote); - unsigned int s2 = accept(s, (struct sockaddr *)&remote, &t); + // This will block until a command is sent down the pipe... + FILE * file = fopen(fifoPath, "r"); - if (s2 == -1) + if (file == NULL) { // Check to see if the signal handler blew things up on us if (errno == EINTR) continue; - // An unanticipated error occurred, handle it - perror("accept"); - printf("init-ng: accept() failed!\n"); + perror("fopen"); + continue; } - // Get the message from the socket - int n = recv(s2, buf, 2048, 0); - buf[n] = 0; + // Grab whatever was shoved into the FIFO + size_t read = fread(buf, 1, 2047, file); + buf[read] = 0; + fclose(file); if (verbose) - printf("PID1: Received \"%s\" from socket...\n", buf); + printf("PID1: Received \"%s\" from FIFO...\n", buf); // Now do something with it... if (strcmp(buf, "poweroff") == 0) @@ -376,8 +340,6 @@ int main(int argc, char * argv[]) { printf("[Status placeholder]\n"); } - - close(s2); } // We should never get here...