aboutsummaryrefslogtreecommitdiff
path: root/src/pies.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/pies.c')
-rw-r--r--src/pies.c75
1 files changed, 60 insertions, 15 deletions
diff --git a/src/pies.c b/src/pies.c
index a8529ac..2aa110f 100644
--- a/src/pies.c
+++ b/src/pies.c
@@ -2262,7 +2262,66 @@ set_state_file_names (const char *base)
if (!qotdfile)
qotdfile = mkfilename (statedir, base, ".qotd");
}
+
+/* Return 1 if pies is run from docker and 0 otherwise. */
+static int
+is_docker (void)
+{
+ FILE *fp;
+ char *id = NULL;
+ int res;
+
+ fp = fopen ("/proc/self/cgroup", "r");
+ if (!fp)
+ return 0;
+ res = fscanf (fp, "%*d:%*[^:]:/docker/%ms\n", &id) == 1;
+ fclose (fp);
+ free (id);
+ return res;
+}
+
+/*
+ * Check if `--no-init' option is present in argv/argc. Return 1 if so,
+ * 0 otherwise.
+ */
+static int
+no_init_option (int argc, char **argv)
+{
+ int i;
+ for (i = 1; i < argc; i++)
+ {
+ if (strcmp (argv[i], "--no-init") == 0)
+ return 1;
+ }
+ return 0;
+}
+/*
+ * init_detect - detect if we're run as init process.
+ *
+ * Pies runs as init process if (1) it has PID 1, (2) is not started as docker
+ * entrypoint, and (3) the `--no-init' command line option is not given.
+ *
+ * The function sets init_process to 1 if pies runs as init process and 0
+ * otherwise.
+ *
+ * No matter the result, if PID is 1 it installs an early SIGCHLD handler,
+ * to ensure that the configuration preprocessor (if such is started) will be
+ * cleaned up properly on exit.
+ */
+static void
+init_detect (int argc, char **argv)
+{
+ init_process = getpid () == 1;
+ if (init_process)
+ {
+ int s[] = { SIGCHLD };
+ setsigvhan (sigchld_early, s, 1);
+ if (no_init_option (argc, argv) || is_docker ())
+ init_process = 0;
+ }
+}
+
size_t pies_master_argc;
char **pies_master_argv;
@@ -2290,22 +2349,8 @@ main (int argc, char **argv)
set_quoting_style (NULL, shell_quoting_style);
- init_process = getpid () == 1;
- if (init_process)
- {
- int s[] = { SIGCHLD };
- setsigvhan (sigchld_early, s, 1);
- }
+ init_detect (argc, argv);
- for (i = 1; i < argc; i++)
- {
- if (strcmp (argv[i], "--no-init") == 0)
- {
- init_process = 0;
- break;
- }
- }
-
/* Set default logging */
if (SYSVINIT_ACTIVE)
{

Return to:

Send suggestions and report system problems to the System administrator.