From 97ae63e2a9c07ee64ab9f226b9b6121114f1d732 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 18 Oct 2012 01:18:50 +0200 Subject: [PATCH 1/1] service: when invoking service processes in --user mode set MANAGERPID to PID of systemd --- man/systemd.service.xml | 19 +++++++++++++------ src/core/service.c | 15 ++++++++++----- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/man/systemd.service.xml b/man/systemd.service.xml index 11f600663..59ecf810f 100644 --- a/man/systemd.service.xml +++ b/man/systemd.service.xml @@ -365,6 +365,13 @@ explicitly to a shell implementation of some kind. Example: ExecStart=/bin/sh -c 'dmesg | tac' + + For services run by a user + instance of systemd the special + environment variable + MANAGERPID is set + to the PID of the systemd + instance. @@ -407,12 +414,12 @@ variable substitution is supported here following the same scheme as for ExecStart=. One - special environment variable is set: - if known $MAINPID is - set to the main process of the - daemon, and may be used for command - lines like the following: - /bin/kill -HUP + additional special environment + variables is set: if known + $MAINPID is set to + the main process of the daemon, and + may be used for command lines like the + following: /bin/kill -HUP $MAINPID. diff --git a/src/core/service.c b/src/core/service.c index 7f9a53af4..5742968d9 100644 --- a/src/core/service.c +++ b/src/core/service.c @@ -1801,7 +1801,8 @@ static int service_spawn( goto fail; } - if (!(our_env = new0(char*, 4))) { + our_env = new0(char*, 5); + if (!our_env) { r = -ENOMEM; goto fail; } @@ -1824,10 +1825,14 @@ static int service_spawn( goto fail; } - if (!(final_env = strv_env_merge(2, - UNIT(s)->manager->environment, - our_env, - NULL))) { + if (s->meta.manager->running_as != SYSTEMD_SYSTEM) + if (asprintf(our_env + n_env++, "MANAGERPID=%lu", (unsigned long) getpid()) < 0) { + r = -ENOMEM; + goto fail; + } + + final_env = strv_env_merge(2, UNIT(s)->manager->environment, our_env, NULL); + if (!final_env) { r = -ENOMEM; goto fail; } -- 2.30.2