summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorYu Watanabe <watanabe.yu+github@gmail.com>2021-01-31 23:33:06 +0900
committerYu Watanabe <watanabe.yu+github@gmail.com>2021-02-01 01:13:44 +0900
commitcfd1c6e26280f27454a0759853befee08382bf7d (patch)
tree30e43e868aacea8b98bec27f46481353d5163cde /src
parent2824374f952ef23d82403ee33b8604cac907903f (diff)
downloadsystemd-cfd1c6e26280f27454a0759853befee08382bf7d.tar.gz
env-util: introduce env_update_systemd_exec_pid()
Will be used in later commits.
Diffstat (limited to 'src')
-rw-r--r--src/basic/env-util.c23
-rw-r--r--src/basic/env-util.h2
-rw-r--r--src/test/test-env-util.c38
3 files changed, 63 insertions, 0 deletions
diff --git a/src/basic/env-util.c b/src/basic/env-util.c
index 96c024afd7..a75186015e 100644
--- a/src/basic/env-util.c
+++ b/src/basic/env-util.c
@@ -12,6 +12,8 @@
#include "extract-word.h"
#include "macro.h"
#include "parse-util.h"
+#include "process-util.h"
+#include "stdio-util.h"
#include "string-util.h"
#include "strv.h"
#include "utf8.h"
@@ -749,3 +751,24 @@ int set_unset_env(const char *name, const char *value, bool overwrite) {
return -errno;
return 0;
}
+
+int setenv_systemd_exec_pid(bool update_only) {
+ char str[DECIMAL_STR_MAX(pid_t)];
+ const char *e;
+
+ /* Update $SYSTEMD_EXEC_PID=pid except when '*' is set for the variable. */
+
+ e = secure_getenv("SYSTEMD_EXEC_PID");
+ if (!e && update_only)
+ return 0;
+
+ if (streq_ptr(e, "*"))
+ return 0;
+
+ xsprintf(str, PID_FMT, getpid_cached());
+
+ if (setenv("SYSTEMD_EXEC_PID", str, 1) < 0)
+ return -errno;
+
+ return 1;
+}
diff --git a/src/basic/env-util.h b/src/basic/env-util.h
index 6684b3350f..5d8afec028 100644
--- a/src/basic/env-util.h
+++ b/src/basic/env-util.h
@@ -55,3 +55,5 @@ int getenv_bool_secure(const char *p);
/* Like setenv, but calls unsetenv if value == NULL. */
int set_unset_env(const char *name, const char *value, bool overwrite);
+
+int setenv_systemd_exec_pid(bool update_only);
diff --git a/src/test/test-env-util.c b/src/test/test-env-util.c
index 755aa069de..ce215fdd36 100644
--- a/src/test/test-env-util.c
+++ b/src/test/test-env-util.c
@@ -4,6 +4,8 @@
#include "fd-util.h"
#include "fileio.h"
#include "fs-util.h"
+#include "parse-util.h"
+#include "process-util.h"
#include "serialize.h"
#include "string-util.h"
#include "strv.h"
@@ -335,6 +337,41 @@ static void test_env_assignment_is_valid(void) {
assert_se(!env_assignment_is_valid("głąb=printf \"\x1b]0;<mock-chroot>\x07<mock-chroot>\""));
}
+static void test_setenv_systemd_exec_pid(void) {
+ _cleanup_free_ char *saved = NULL;
+ const char *e;
+ pid_t p;
+
+ log_info("/* %s */", __func__);
+
+ e = getenv("SYSTEMD_EXEC_PID");
+ if (e)
+ assert_se(saved = strdup(e));
+
+ assert_se(unsetenv("SYSTEMD_EXEC_PID") >= 0);
+ assert_se(setenv_systemd_exec_pid(true) == 0);
+ assert_se(!getenv("SYSTEMD_EXEC_PID"));
+
+ assert_se(setenv("SYSTEMD_EXEC_PID", "*", 1) >= 0);
+ assert_se(setenv_systemd_exec_pid(true) == 0);
+ assert_se(e = getenv("SYSTEMD_EXEC_PID"));
+ assert_se(streq(e, "*"));
+
+ assert_se(setenv("SYSTEMD_EXEC_PID", "123abc", 1) >= 0);
+ assert_se(setenv_systemd_exec_pid(true) == 1);
+ assert_se(e = getenv("SYSTEMD_EXEC_PID"));
+ assert_se(parse_pid(e, &p) >= 0);
+ assert_se(p == getpid_cached());
+
+ assert_se(unsetenv("SYSTEMD_EXEC_PID") >= 0);
+ assert_se(setenv_systemd_exec_pid(false) == 1);
+ assert_se(e = getenv("SYSTEMD_EXEC_PID"));
+ assert_se(parse_pid(e, &p) >= 0);
+ assert_se(p == getpid_cached());
+
+ assert_se(set_unset_env("SYSTEMD_EXEC_PID", saved, 1) >= 0);
+}
+
int main(int argc, char *argv[]) {
test_setup_logging(LOG_DEBUG);
@@ -353,6 +390,7 @@ int main(int argc, char *argv[]) {
test_env_name_is_valid();
test_env_value_is_valid();
test_env_assignment_is_valid();
+ test_setenv_systemd_exec_pid();
return 0;
}