From 67408cf8fb6cfc648bd1e622ef497b068e078ef1 Mon Sep 17 00:00:00 2001 From: Val Packett Date: Fri, 3 Oct 2025 04:13:26 -0300 Subject: [PATCH] init: allow running the user-provided init as pid 1 (fix #223) Some init systems like systemd absolutely need to run as PID 1. Add an environment variable to allow doing so at the cost of the return code feature. Signed-off-by: Val Packett --- init/init.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/init/init.c b/init/init.c index 309f41c64..5ab2c7ee7 100644 --- a/init/init.c +++ b/init/init.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -1044,6 +1045,7 @@ int main(int argc, char **argv) int sockfd; int status; int saved_errno; + bool init_pid1 = false; char localhost[] = "localhost\0"; char *hostname; char *krun_home; @@ -1052,6 +1054,7 @@ int main(int argc, char **argv) char *krun_root; char *krun_root_fstype; char *krun_root_options; + char *env_init_pid1; char *config_workdir, *env_workdir; char *rlimits; char **config_argv, **exec_argv; @@ -1186,12 +1189,21 @@ int main(int argc, char **argv) exec_argv[0] = &DEFAULT_KRUN_INIT[0]; } + env_init_pid1 = getenv("KRUN_INIT_PID1"); + if (env_init_pid1 && *env_init_pid1 == '1') { + init_pid1 = true; + } + #ifdef __TIMESYNC__ if (fork() == 0) { clock_worker(); } #endif + if (init_pid1) { + goto exec_init; + } + // We need to fork ourselves, because pid 1 cannot doesn't receive SIGINT // signal int child = fork(); @@ -1201,6 +1213,7 @@ int main(int argc, char **argv) exit(125); } if (child == 0) { // child + exec_init: if (setup_redirects() < 0) { exit(125); }