aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org>2020-10-17 14:22:42 +0300
committerSergey Poznyakoff <gray@gnu.org>2020-10-17 14:59:33 +0300
commit6f999734206580ee2c693fe476cad2322a72fc0d (patch)
tree3a15cbb02c7abb80994ff9817ee248f96b2aaf48 /src
parent5f2769a03542c039720b98525477495acaa51a06 (diff)
downloadpies-6f999734206580ee2c693fe476cad2322a72fc0d.tar.gz
pies-6f999734206580ee2c693fe476cad2322a72fc0d.tar.bz2
Automatically detect if pies is started from docker
* lib/envop.c: Include sys/types.h (for ssize_t). * src/pies.c (is_docker,no_init_option,init_detect): New functions. (main): Call init_detect. * src/utmp.c: Additional cpp magic to determine WTMPX_FILE and UTMPX_FILE. * NEWS: Document the changes. * doc/pies.texi: Likewise. Also, fix improper node name (inetd.conf). * configure.ac: Version 1.4.91
Diffstat (limited to 'src')
-rw-r--r--src/pies.c75
-rw-r--r--src/utmp.c41
2 files changed, 92 insertions, 24 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)
{
diff --git a/src/utmp.c b/src/utmp.c
index 4a1e746..8045370 100644
--- a/src/utmp.c
+++ b/src/utmp.c
@@ -26,10 +26,6 @@ typedef struct utmpx UTMPX;
# define pies_updwtmpx updwtmpx
# endif
# define GETUTXID getutxid
-# undef UTMP_FILE
-# define UTMP_FILE UTMPX_FILE
-# undef WTMP_FILE
-# define WTMP_FILE WTMPX_FILE
# if defined HAVE_STRUCT_UTMPX_UT_NAME
# define UT_NAME(u) ((u)->ut_name)
# elif defined HAVE_STRUCT_UTMPX_UT_USER
@@ -52,6 +48,33 @@ typedef struct utmp UTMPX;
# define endutxent endutent
#endif
+#if !defined(UTMPX_FILE) || !defined(WTMPX_FILE)
+# if !defined(UTMP_FILE) || !defined(WTMP_FILE)
+# ifndef HAVE_UTMPX_H
+# include <utmp.h>
+# endif
+# if !defined(UTMP_FILE)
+# if !defined(_PATH_UTMP)
+# error "None of UTMPX_FILE, UTMP_FILE or _PATH_UTMP defined"
+# else
+# define UTMP_FILE _PATH_UTMP
+# endif
+# endif
+# if !defined(WTMP_FILE)
+# if !defined(_PATH_WTMP)
+# error "None of WTMPX_FILE, WTMP_FILE or _PATH_WTMP defined"
+# else
+# define WTMP_FILE _PATH_WTMP
+# endif
+# endif
+# endif
+# if !defined(UTMPX_FILE)
+# define UTMPX_FILE UTMP_FILE
+# endif
+# if !defined(WTMPX_FILE)
+# define WTMPX_FILE WTMP_FILE
+# endif
+#endif
#ifndef pies_updwtmpx
static void
@@ -111,8 +134,8 @@ write_wtmpx (int type, const char *user, const char *id, pid_t pid,
UTMPX utmp;
static int wtmpxreboot = 0;
- debug (2, (_("cannot open %s for writing"), WTMP_FILE));
- if (access (WTMP_FILE, W_OK))
+ debug (2, (_("cannot open %s for writing"), WTMPX_FILE));
+ if (access (WTMPX_FILE, W_OK))
{
if (type == BOOT_TIME)
wtmpxreboot++;
@@ -125,7 +148,7 @@ write_wtmpx (int type, const char *user, const char *id, pid_t pid,
--wtmpxreboot;
}
fill_utmp (&utmp, type, user, id, pid, line);
- pies_updwtmpx (WTMP_FILE, &utmp);
+ pies_updwtmpx (WTMPX_FILE, &utmp);
}
void
@@ -135,9 +158,9 @@ write_utmpx (int type, const char *user, const char *id, pid_t pid,
UTMPX utmp;
static int utmpreboot = 0;
- if (access (UTMP_FILE, W_OK))
+ if (access (UTMPX_FILE, W_OK))
{
- debug (1, (_("cannot open %s for writing"), UTMP_FILE));
+ debug (1, (_("cannot open %s for writing"), UTMPX_FILE));
if (type == BOOT_TIME)
utmpreboot++;
return;

Return to:

Send suggestions and report system problems to the System administrator.