chiark / gitweb /
bus: rework bloom filter logic to operate with variable bloom filter
[elogind.git] / src / libsystemd-daemon / sd-daemon.c
index 9cc1c37b468ce6e8ee84a110c419dc49768fb7b2..94230c9ed66120faf3856598931e36b00eeed9fd 100644 (file)
 #include <sys/stat.h>
 #include <sys/socket.h>
 #include <sys/un.h>
-#ifdef __BIONIC__
-#  include <linux/fcntl.h>
-#else
-#  include <sys/fcntl.h>
-#endif
+#include <fcntl.h>
 #include <netinet/in.h>
 #include <stdlib.h>
 #include <errno.h>
@@ -278,11 +274,8 @@ _sd_export_ int sd_is_socket(int fd, int family, int type, int listening) {
                 return r;
 
         if (family > 0) {
-                union sockaddr_union sockaddr;
-                socklen_t l;
-
-                memset(&sockaddr, 0, sizeof(sockaddr));
-                l = sizeof(sockaddr);
+                union sockaddr_union sockaddr = {};
+                socklen_t l = sizeof(sockaddr);
 
                 if (getsockname(fd, &sockaddr.sa, &l) < 0)
                         return -errno;
@@ -297,8 +290,8 @@ _sd_export_ int sd_is_socket(int fd, int family, int type, int listening) {
 }
 
 _sd_export_ int sd_is_socket_inet(int fd, int family, int type, int listening, uint16_t port) {
-        union sockaddr_union sockaddr;
-        socklen_t l;
+        union sockaddr_union sockaddr = {};
+        socklen_t l = sizeof(sockaddr);
         int r;
 
         if (family != 0 && family != AF_INET && family != AF_INET6)
@@ -308,9 +301,6 @@ _sd_export_ int sd_is_socket_inet(int fd, int family, int type, int listening, u
         if (r <= 0)
                 return r;
 
-        memset(&sockaddr, 0, sizeof(sockaddr));
-        l = sizeof(sockaddr);
-
         if (getsockname(fd, &sockaddr.sa, &l) < 0)
                 return -errno;
 
@@ -343,17 +333,14 @@ _sd_export_ int sd_is_socket_inet(int fd, int family, int type, int listening, u
 }
 
 _sd_export_ int sd_is_socket_unix(int fd, int type, int listening, const char *path, size_t length) {
-        union sockaddr_union sockaddr;
-        socklen_t l;
+        union sockaddr_union sockaddr = {};
+        socklen_t l = sizeof(sockaddr);
         int r;
 
         r = sd_is_socket_internal(fd, type, listening);
         if (r <= 0)
                 return r;
 
-        memset(&sockaddr, 0, sizeof(sockaddr));
-        l = sizeof(sockaddr);
-
         if (getsockname(fd, &sockaddr.sa, &l) < 0)
                 return -errno;
 
@@ -531,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
+}