From: Yu Watanabe Date: Tue, 26 Jan 2021 06:56:42 +0000 (+0900) Subject: core: set $SYSTEMD_EXEC_PID= environment variable for executed commands X-Git-Tag: v248-rc1~187^2~5 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=dc4e2940e87f4f0969476b45a1d25322b5210643;p=thirdparty%2Fsystemd.git core: set $SYSTEMD_EXEC_PID= environment variable for executed commands It may be useful to detect a command is directly executed by systemd manager, or indirectly as a child of another process. --- diff --git a/man/systemd.exec.xml b/man/systemd.exec.xml index 203fc0355d7..5b5590a2336 100644 --- a/man/systemd.exec.xml +++ b/man/systemd.exec.xml @@ -3043,6 +3043,17 @@ StandardInputData=SWNrIHNpdHplIGRhIHVuJyBlc3NlIEtsb3BzLAp1ZmYgZWVtYWwga2xvcHAncy + + $SYSTEMD_EXEC_PID + + The PID of the unit process (e.g. process invoked by + ExecStart=). The child process can use this information to determine + whether the process is directly invoked by the service manager or indirectly as a child of + another process by comparing this value with the current PID (as similar to the scheme used in + sd_listen_fds3 + with $LISTEN_PID and $LISTEN_FDS). + + $TERM diff --git a/src/core/execute.c b/src/core/execute.c index b7d78f2197e..29fe9f05b12 100644 --- a/src/core/execute.c +++ b/src/core/execute.c @@ -1794,7 +1794,7 @@ static int build_environment( assert(p); assert(ret); -#define N_ENV_VARS 16 +#define N_ENV_VARS 17 our_env = new0(char*, N_ENV_VARS + _EXEC_DIRECTORY_TYPE_MAX); if (!our_env) return -ENOMEM; @@ -1950,6 +1950,11 @@ static int build_environment( our_env[n_env++] = x; } + if (asprintf(&x, "SYSTEMD_EXEC_PID=" PID_FMT, getpid_cached()) < 0) + return -ENOMEM; + + our_env[n_env++] = x; + our_env[n_env++] = NULL; assert(n_env <= N_ENV_VARS + _EXEC_DIRECTORY_TYPE_MAX); #undef N_ENV_VARS