summaryrefslogtreecommitdiff
path: root/src/libsystemd-daemon/sd-daemon.c
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/libsystemd-daemon/sd-daemon.c
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/libsystemd-daemon/sd-daemon.c')
-rw-r--r--src/libsystemd-daemon/sd-daemon.c66
1 files changed, 66 insertions, 0 deletions
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
+}