summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2014-10-21 18:44:09 -0400
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2014-10-23 00:17:18 -0400
commita9becdd65bb4b64675bc0c109d14ab12b1ecd2b7 (patch)
tree45faa8c9e120afe1e8227926bc238d5e0eeb2920 /src
parent203af57fcdced5debfc26e1083eaefa031e322f4 (diff)
sd-daemon,man: ignore missing $WATCHDOG_PID
Systemd 209 started setting $WATCHDOG_PID, and sd-daemon watch was modified to check for this variable. This means that sd_watchdog_enabled() stopped working with previous versions of systemd. But sd-event is a public library and API and we must keep it working even when a program compiled with a newer version of the libary is used on a system running an older version of the manager. getenv() and unsetenv() are fairly expensive calls, so optimize sd_watchdog_enabled() by not calling them when unnecessary. man: centralize the description of $WATCHDOG_PID and $WATCHDOG_USEC in the sd_watchdog_enabled manpage. It is better not to repeat the same stuff in two places.
Diffstat (limited to 'src')
-rw-r--r--src/libsystemd/sd-daemon/sd-daemon.c48
1 files changed, 22 insertions, 26 deletions
diff --git a/src/libsystemd/sd-daemon/sd-daemon.c b/src/libsystemd/sd-daemon/sd-daemon.c
index 46241f77ff..1f2a53393f 100644
--- a/src/libsystemd/sd-daemon/sd-daemon.c
+++ b/src/libsystemd/sd-daemon/sd-daemon.c
@@ -491,39 +491,35 @@ _public_ int sd_booted(void) {
}
_public_ int sd_watchdog_enabled(int unset_environment, uint64_t *usec) {
- const char *e;
+ const char *s, *p = ""; /* p is set to dummy value to do unsetting */
uint64_t u;
- pid_t pid;
- int r;
+ int r = 0;
- e = getenv("WATCHDOG_PID");
- if (!e) {
- r = 0;
+ s = getenv("WATCHDOG_USEC");
+ if (!s)
goto finish;
- }
- r = parse_pid(e, &pid);
+ r = safe_atou64(s, &u);
if (r < 0)
goto finish;
-
- /* Is this for us? */
- if (getpid() != pid) {
- r = 0;
- goto finish;
- }
-
- e = getenv("WATCHDOG_USEC");
- if (!e) {
+ if (u <= 0) {
r = -EINVAL;
goto finish;
}
- r = safe_atou64(e, &u);
- if (r < 0)
- goto finish;
- if (u <= 0) {
- r = -EINVAL;
- goto finish;
+ p = getenv("WATCHDOG_PID");
+ if (p) {
+ pid_t pid;
+
+ r = parse_pid(p, &pid);
+ if (r < 0)
+ goto finish;
+
+ /* Is this for us? */
+ if (getpid() != pid) {
+ r = 0;
+ goto finish;
+ }
}
if (usec)
@@ -532,10 +528,10 @@ _public_ int sd_watchdog_enabled(int unset_environment, uint64_t *usec) {
r = 1;
finish:
- if (unset_environment) {
- unsetenv("WATCHDOG_PID");
+ if (unset_environment && s)
unsetenv("WATCHDOG_USEC");
- }
+ if (unset_environment && p)
+ unsetenv("WATCHDOG_PID");
return r;
}