]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
basic/log: split out invoked_by_systemd() utility function
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 15 Jul 2022 09:38:01 +0000 (11:38 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 15 Jul 2022 13:47:23 +0000 (15:47 +0200)
src/basic/log.c
src/basic/process-util.c
src/basic/process-util.h

index 02fc2c6f92f959c43d8a8d8d3424c0c0eadcea33..cac17a76f4d2307162f29854023019332c834835 100644 (file)
@@ -1153,30 +1153,12 @@ static int parse_proc_cmdline_item(const char *key, const char *value, void *dat
 }
 
 static bool should_parse_proc_cmdline(void) {
-        const char *e;
-        pid_t p;
-
         /* PID1 always reads the kernel command line. */
         if (getpid_cached() == 1)
                 return true;
 
-        /* If the process is directly executed by PID1 (e.g. ExecStart= or generator), systemd-importd,
-         * or systemd-homed, then $SYSTEMD_EXEC_PID= is set, and read the command line. */
-        e = getenv("SYSTEMD_EXEC_PID");
-        if (!e)
-                return false;
-
-        if (streq(e, "*"))
-                /* For testing. */
-                return true;
-
-        if (parse_pid(e, &p) < 0) {
-                /* We know that systemd sets the variable correctly. Something else must have set it. */
-                log_debug("Failed to parse \"$SYSTEMD_EXEC_PID=%s\". Ignoring.", e);
-                return false;
-        }
-
-        return getpid_cached() == p;
+        /* Otherwise, parse the commandline if invoked directly by systemd. */
+        return invoked_by_systemd();
 }
 
 void log_parse_environment_variables(void) {
index 6980e0c4f695023670b9db8f14390e9bff244c9e..d885d92dfb2638901afaace9d8d48b34734d6a90 100644 (file)
@@ -1579,6 +1579,30 @@ bool invoked_as(char *argv[], const char *token) {
         return strstr(last_path_component(argv[0]), token);
 }
 
+bool invoked_by_systemd(void) {
+        int r;
+
+        /* If the process is directly executed by PID1 (e.g. ExecStart= or generator), systemd-importd,
+         * or systemd-homed, then $SYSTEMD_EXEC_PID= is set, and read the command line. */
+        const char *e = getenv("SYSTEMD_EXEC_PID");
+        if (!e)
+                return false;
+
+        if (streq(e, "*"))
+                /* For testing. */
+                return true;
+
+        pid_t p;
+        r = parse_pid(e, &p);
+        if (r < 0) {
+                /* We know that systemd sets the variable correctly. Something else must have set it. */
+                log_debug_errno(r, "Failed to parse \"SYSTEMD_EXEC_PID=%s\", ignoring: %m", e);
+                return false;
+        }
+
+        return getpid_cached() == p;
+}
+
 _noreturn_ void freeze(void) {
         log_close();
 
index c07575e5802a4453fc4832dbe97a849a236ab55e..f8c374a310367b745983ec9e95fed0bda8f69d3d 100644 (file)
@@ -190,6 +190,8 @@ int setpriority_closest(int priority);
 
 bool invoked_as(char *argv[], const char *token);
 
+bool invoked_by_systemd(void);
+
 _noreturn_ void freeze(void);
 
 bool argv_looks_like_help(int argc, char **argv);