summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2013-12-22 22:14:05 +0100
committerLennart Poettering <lennart@poettering.net>2013-12-22 22:19:03 +0100
commit09812eb764b440651f3ff4cb5d37bd343f800560 (patch)
tree75a7a86e915c814ded076cd0efc84d20018ff13b /src
parent565a9388f261c6e459e1726e358284ff687ec941 (diff)
sd-daemon: introduce sd_watchdog_enabled() for parsing $WATCHDOG_USEC
Also, introduce a new environment variable named $WATCHDOG_PID which cotnains the PID of the process that is supposed to send the keep-alive events. This is similar how $LISTEN_FDS and $LISTEN_PID work together, and protects against confusing processes further down the process tree due to inherited environment.
Diffstat (limited to 'src')
-rw-r--r--src/core/execute.c18
-rw-r--r--src/core/execute.h1
-rw-r--r--src/core/mount.c1
-rw-r--r--src/core/service.c9
-rw-r--r--src/core/socket.c1
-rw-r--r--src/core/swap.c1
-rw-r--r--src/libsystemd-bus/sd-event.c11
-rw-r--r--src/libsystemd-daemon/libsystemd-daemon.sym5
-rw-r--r--src/libsystemd-daemon/sd-daemon.c66
-rw-r--r--src/systemd/sd-daemon.h21
10 files changed, 114 insertions, 20 deletions
diff --git a/src/core/execute.c b/src/core/execute.c
index 4265717895..9b33ec0ec5 100644
--- a/src/core/execute.c
+++ b/src/core/execute.c
@@ -1020,6 +1020,7 @@ static void do_idle_pipe_dance(int idle_pipe[4]) {
static int build_environment(
ExecContext *c,
unsigned n_fds,
+ usec_t watchdog_usec,
const char *home,
const char *username,
const char *shell,
@@ -1032,7 +1033,7 @@ static int build_environment(
assert(c);
assert(ret);
- our_env = new(char*, 8);
+ our_env = new0(char*, 10);
if (!our_env)
return -ENOMEM;
@@ -1046,6 +1047,16 @@ static int build_environment(
our_env[n_env++] = x;
}
+ if (watchdog_usec > 0) {
+ if (asprintf(&x, "WATCHDOG_PID=%lu", (unsigned long) getpid()) < 0)
+ return -ENOMEM;
+ our_env[n_env++] = x;
+
+ if (asprintf(&x, "WATCHDOG_USEC=%llu", (unsigned long long) watchdog_usec) < 0)
+ return -ENOMEM;
+ our_env[n_env++] = x;
+ }
+
if (home) {
x = strappend("HOME=", home);
if (!x)
@@ -1084,7 +1095,7 @@ static int build_environment(
}
our_env[n_env++] = NULL;
- assert(n_env <= 8);
+ assert(n_env <= 10);
*ret = our_env;
our_env = NULL;
@@ -1104,6 +1115,7 @@ int exec_spawn(ExecCommand *command,
CGroupControllerMask cgroup_supported,
const char *cgroup_path,
const char *unit_id,
+ usec_t watchdog_usec,
int idle_pipe[4],
ExecRuntime *runtime,
pid_t *ret) {
@@ -1560,7 +1572,7 @@ int exec_spawn(ExecCommand *command,
}
}
- err = build_environment(context, n_fds, home, username, shell, &our_env);
+ err = build_environment(context, n_fds, watchdog_usec, home, username, shell, &our_env);
if (r < 0) {
r = EXIT_MEMORY;
goto fail_child;
diff --git a/src/core/execute.h b/src/core/execute.h
index bd3db0b633..989373f481 100644
--- a/src/core/execute.h
+++ b/src/core/execute.h
@@ -181,6 +181,7 @@ int exec_spawn(ExecCommand *command,
CGroupControllerMask cgroup_mask,
const char *cgroup_path,
const char *unit_id,
+ usec_t watchdog_usec,
int pipe_fd[2],
ExecRuntime *runtime,
pid_t *ret);
diff --git a/src/core/mount.c b/src/core/mount.c
index 2b7934e404..09efa1b6e0 100644
--- a/src/core/mount.c
+++ b/src/core/mount.c
@@ -787,6 +787,7 @@ static int mount_spawn(Mount *m, ExecCommand *c, pid_t *_pid) {
UNIT(m)->manager->cgroup_supported,
UNIT(m)->cgroup_path,
UNIT(m)->id,
+ 0,
NULL,
m->exec_runtime,
&pid);
diff --git a/src/core/service.c b/src/core/service.c
index 87eaa29378..4eb3d9e668 100644
--- a/src/core/service.c
+++ b/src/core/service.c
@@ -1750,7 +1750,7 @@ static int service_spawn(
if (r < 0)
goto fail;
- our_env = new0(char*, 5);
+ our_env = new0(char*, 4);
if (!our_env) {
r = -ENOMEM;
goto fail;
@@ -1768,12 +1768,6 @@ static int service_spawn(
goto fail;
}
- if (s->watchdog_usec > 0)
- if (asprintf(our_env + n_env++, "WATCHDOG_USEC=%llu", (unsigned long long) s->watchdog_usec) < 0) {
- r = -ENOMEM;
- goto fail;
- }
-
if (UNIT(s)->manager->running_as != SYSTEMD_SYSTEM)
if (asprintf(our_env + n_env++, "MANAGERPID=%lu", (unsigned long) getpid()) < 0) {
r = -ENOMEM;
@@ -1804,6 +1798,7 @@ static int service_spawn(
UNIT(s)->manager->cgroup_supported,
path,
UNIT(s)->id,
+ s->watchdog_usec,
s->type == SERVICE_IDLE ? UNIT(s)->manager->idle_pipe : NULL,
s->exec_runtime,
&pid);
diff --git a/src/core/socket.c b/src/core/socket.c
index 2442221355..31fc2a2526 100644
--- a/src/core/socket.c
+++ b/src/core/socket.c
@@ -1254,6 +1254,7 @@ static int socket_spawn(Socket *s, ExecCommand *c, pid_t *_pid) {
UNIT(s)->manager->cgroup_supported,
UNIT(s)->cgroup_path,
UNIT(s)->id,
+ 0,
NULL,
s->exec_runtime,
&pid);
diff --git a/src/core/swap.c b/src/core/swap.c
index 79862e5b87..e0627db965 100644
--- a/src/core/swap.c
+++ b/src/core/swap.c
@@ -645,6 +645,7 @@ static int swap_spawn(Swap *s, ExecCommand *c, pid_t *_pid) {
UNIT(s)->manager->cgroup_supported,
UNIT(s)->cgroup_path,
UNIT(s)->id,
+ 0,
NULL,
s->exec_runtime,
&pid);
diff --git a/src/libsystemd-bus/sd-event.c b/src/libsystemd-bus/sd-event.c
index a1baac52aa..bfc798ca6e 100644
--- a/src/libsystemd-bus/sd-event.c
+++ b/src/libsystemd-bus/sd-event.c
@@ -2164,17 +2164,10 @@ _public_ int sd_event_set_watchdog(sd_event *e, int b) {
if (b) {
struct epoll_event ev = {};
- const char *env;
- env = getenv("WATCHDOG_USEC");
- if (!env)
- return false;
-
- r = safe_atou64(env, &e->watchdog_period);
- if (r < 0)
+ r = sd_watchdog_enabled(false, &e->watchdog_period);
+ if (r <= 0)
return r;
- if (e->watchdog_period <= 0)
- return -EIO;
/* Issue first ping immediately */
sd_notify(false, "WATCHDOG=1");
diff --git a/src/libsystemd-daemon/libsystemd-daemon.sym b/src/libsystemd-daemon/libsystemd-daemon.sym
index f440238931..aa9be51c6a 100644
--- a/src/libsystemd-daemon/libsystemd-daemon.sym
+++ b/src/libsystemd-daemon/libsystemd-daemon.sym
@@ -25,3 +25,8 @@ global:
local:
*;
};
+
+LIBSYSTEMD_DAEMON_209 {
+global:
+ sd_watchdog_enabled;
+} LIBSYSTEMD_DAEMON_31;
diff --git a/src/libsystemd-daemon/sd-daemon.c b/src/libsystemd-daemon/sd-daemon.c
index 485b301023..94230c9ed6 100644
--- a/src/libsystemd-daemon/sd-daemon.c
+++ b/src/libsystemd-daemon/sd-daemon.c
@@ -518,3 +518,69 @@ _sd_export_ int sd_booted(void) {
return !!S_ISDIR(st.st_mode);
#endif
}
+
+_sd_export_ int sd_watchdog_enabled(int unset_environment, uint64_t *usec) {
+
+#if defined(DISABLE_SYSTEMD) || !defined(__linux__)
+ return 0;
+#else
+ unsigned long long ll;
+ unsigned long l;
+ const char *e;
+ char *p = NULL;
+ int r;
+
+ e = getenv("WATCHDOG_PID");
+ if (!e) {
+ r = 0;
+ goto finish;
+ }
+
+ errno = 0;
+ l = strtoul(e, &p, 10);
+ if (errno > 0) {
+ r = -errno;
+ goto finish;
+ }
+ if (!p || p == e || *p || l <= 0) {
+ r = -EINVAL;
+ goto finish;
+ }
+
+ /* Is this for us? */
+ if (getpid() != (pid_t) l) {
+ r = 0;
+ goto finish;
+ }
+
+ e = getenv("WATCHDOG_USEC");
+ if (!e) {
+ r = -EINVAL;
+ goto finish;
+ }
+
+ errno = 0;
+ ll = strtoull(e, &p, 10);
+ if (errno > 0) {
+ r = -errno;
+ goto finish;
+ }
+ if (!p || p == e || *p || l <= 0) {
+ r = -EINVAL;
+ goto finish;
+ }
+
+ if (usec)
+ *usec = ll;
+
+ r = 1;
+
+finish:
+ if (unset_environment) {
+ unsetenv("WATCHDOG_PID");
+ unsetenv("WATCHDOG_USEC");
+ }
+
+ return r;
+#endif
+}
diff --git a/src/systemd/sd-daemon.h b/src/systemd/sd-daemon.h
index daa3f4c857..43deb8cc7d 100644
--- a/src/systemd/sd-daemon.h
+++ b/src/systemd/sd-daemon.h
@@ -186,6 +186,8 @@ int sd_is_socket_unix(int fd, int type, int listening, const char *path, size_t
the file descriptor is a POSIX Message Queue of the specified name,
0 otherwise. If path is NULL a message queue name check is not
done. Returns a negative errno style error code on failure.
+
+ See sd_is_mq(3) for more information.
*/
int sd_is_mq(int fd, const char *path);
@@ -220,7 +222,8 @@ int sd_is_mq(int fd, const char *path);
WATCHDOG=1 Tells systemd to update the watchdog timestamp.
Services using this feature should do this in
regular intervals. A watchdog framework can use the
- timestamps to detect failed services.
+ timestamps to detect failed services. Also see
+ sd_watchdog_enabled() below.
Daemons can choose to send additional variables. However, it is
recommended to prefix variable names not listed above with X_.
@@ -275,6 +278,22 @@ int sd_notifyf(int unset_environment, const char *format, ...) _sd_printf_attr_(
*/
int sd_booted(void);
+/*
+ Returns > 0 if the service manager expects watchdog keep-alive
+ events to be sent regularly via sd_notify(0, "WATCHDOG=1"). Returns
+ 0 if it does not expect this. If the usec argument is non-NULL
+ returns the watchdog timeout in µs after which the service manager
+ will act on a process that has not sent a watchdog keep alive
+ message. This function is useful to implement services that
+ recognize automatically if they are being run under supervision of
+ systemd with WatchdogSec= set. It is recommended for clients to
+ generate keep-alive pings via sd_notify(0, "WATCHDOG=1") every half
+ of the returned time.
+
+ See sd_watchdog_enabled(3) for more information.
+*/
+int sd_watchdog_enabled(int unset_environment, uint64_t *usec);
+
#ifdef __cplusplus
}
#endif