summaryrefslogtreecommitdiffstats
path: root/src/basic/process-util.c
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2022-07-15 11:38:01 +0200
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2022-07-15 15:47:23 +0200
commit494f4ee9c770e401ea3d570a9caf4b3058cf7758 (patch)
tree487af69bafd7d0bee4c1932f484b5ea3b001c739 /src/basic/process-util.c
parentgenerators: accept one or three args, do not write to /tmp (diff)
downloadsystemd-494f4ee9c770e401ea3d570a9caf4b3058cf7758.tar.xz
systemd-494f4ee9c770e401ea3d570a9caf4b3058cf7758.zip
basic/log: split out invoked_by_systemd() utility function
Diffstat (limited to 'src/basic/process-util.c')
-rw-r--r--src/basic/process-util.c24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/basic/process-util.c b/src/basic/process-util.c
index 6980e0c4f6..d885d92dfb 100644
--- a/src/basic/process-util.c
+++ b/src/basic/process-util.c
@@ -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();