From d7401b2a961d9fd9aeca72011394d5a00e0b2b50 Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Mon, 18 Dec 2017 11:20:21 +0100 Subject: [PATCH] Follow-up to issue #81: remove SysV shared memory IPC altogether This change removes a long standing kernel requirement and architectural mishap in the design of Finit v1. It is no longer necessary to include CONFIG_SYSVIPC in the kernel to use Finit and its command line tool initctl. It is also no longer possible to connect to the internals of PID 1 using shmat(), the initctl tool has already been updated to use the domain socket API. Signed-off-by: Joachim Nilsson --- doc/TODO.md | 3 --- src/finit.c | 4 ---- src/svc.c | 31 +++++++------------------------ src/svc.h | 19 ------------------- 4 files changed, 7 insertions(+), 50 deletions(-) diff --git a/doc/TODO.md b/doc/TODO.md index cbf98bbe..d6bf8aa0 100644 --- a/doc/TODO.md +++ b/doc/TODO.md @@ -21,9 +21,6 @@ can also be used. General ------- -* Remove shm, let `initctl` call `api.c` for each `svc_t` instead of - opening up the inner parts of PID 1 to other processes. Also, this - would prevent issues like #81 from happening. * Add support for timed shutdown in Finit, including cancelled shutdown. * SysV init and systemd use SIGUSR1 to restart their FIFO/D-Bus. Add API restart to SIGHUP callback for the same functionality in Finit. diff --git a/src/finit.c b/src/finit.c index b8d445bb..811a6e26 100644 --- a/src/finit.c +++ b/src/finit.c @@ -378,10 +378,6 @@ int main(int argc, char* argv[]) makedir("/dev/pts", 0755); mount("devpts", "/dev/pts", "devpts", 0, "gid=5,mode=620"); - makedir("/dev/shm", 0755); - if (!fismnt("/dev/shm")) - mount("shm", "/dev/shm", "tmpfs", 0, NULL); - /* * New tmpfs based /run for volatile runtime data * For details, see http://lwn.net/Articles/436012/ diff --git a/src/svc.c b/src/svc.c index 39ed62e0..7d706ce7 100644 --- a/src/svc.c +++ b/src/svc.c @@ -35,37 +35,20 @@ /* Each svc_t needs a unique job# */ static int jobcounter = 1; -static svc_t *workaround = NULL; +static svc_t *svc_list = NULL; static svc_t *__connect_shm(void) { - svc_t *list; + if (svc_list) + return svc_list; - list = finit_svc_connect(); - if (!list) { - if (workaround) - return workaround; - - /* Linux not built with CONFIG_SYSVIPC, or libc does not support shmat()/shmget() */ - if (ENOSYS == errno) - warn("Kernel does support SYSV shmat() IPC, error %d", errno); - - /* Try to prevent PID 1 from aborting, issue #81 */ - if (getpid() == 1) { - warnx("Implementing PID 1 workaround, initctl tool will not work ..."); - if (!workaround) - workaround = calloc(MAX_NUM_SVC, sizeof(svc_t)); - - list = workaround; - } - } - - if (!list) { - warn("Failed connecting to shared memory, error %d", errno); + svc_list = calloc(MAX_NUM_SVC, sizeof(svc_t)); + if (!svc_list) { + warn("Failed allocating static list of services, error %d", errno); abort(); } - return list; + return svc_list; } /** diff --git a/src/svc.h b/src/svc.h index 4f6623ab..540f4442 100644 --- a/src/svc.h +++ b/src/svc.h @@ -27,7 +27,6 @@ #include /* IPC_CREAT */ #include -#include /* shmat() */ #include /* pid_t */ #include @@ -66,7 +65,6 @@ typedef enum { SVC_BLOCK_RESTARTING, } svc_block_t; -#define FINIT_SHM_ID 0x494E4954 /* "INIT", see ascii(7) */ #define MAX_ARG_LEN 64 #define MAX_STR_LEN 64 #define MAX_COND_LEN (MAX_ARG_LEN * 3) @@ -129,23 +127,6 @@ typedef struct svc { void (*timer_cb)(struct svc *svc); } svc_t; -typedef struct svc_map svc_map_t; - -/* Put services array in shm */ -static inline svc_t *finit_svc_connect(void) -{ - static void *ptr = (void *)-1; - - if ((void *)-1 == ptr) { - ptr = shmat(shmget(FINIT_SHM_ID, sizeof(svc_t) * MAX_NUM_SVC, - 0600 | IPC_CREAT), NULL, 0); - if ((void *)-1 == ptr) - return NULL; - } - - return (svc_t *)ptr; -} - svc_t *svc_new (char *cmd, int id, int type); int svc_del (svc_t *svc);