Blame SOURCES/0001-systemd-update-sd-daemon.-ch.patch

c8b730
From 8c902327f91616af5e87fd2a6d4b7ea38bb3aa32 Mon Sep 17 00:00:00 2001
c8b730
From: Lennart Poettering <lennart@poettering.net>
c8b730
Date: Thu, 26 Sep 2013 21:53:49 +0200
c8b730
Subject: [PATCH] systemd: update sd-daemon.[ch]
c8b730
c8b730
---
c8b730
 sd-daemon.c | 100 +++++++++++++++++++++++-----------------------------
c8b730
 sd-daemon.h |  14 ++++----
c8b730
 2 files changed, 52 insertions(+), 62 deletions(-)
c8b730
c8b730
diff --git a/sd-daemon.c b/sd-daemon.c
c8b730
index 763e079b4e..485b301023 100644
c8b730
--- a/sd-daemon.c
c8b730
+++ b/sd-daemon.c
c8b730
@@ -25,18 +25,14 @@
c8b730
 ***/
c8b730
 
c8b730
 #ifndef _GNU_SOURCE
c8b730
-#define _GNU_SOURCE
c8b730
+#  define _GNU_SOURCE
c8b730
 #endif
c8b730
 
c8b730
 #include <sys/types.h>
c8b730
 #include <sys/stat.h>
c8b730
 #include <sys/socket.h>
c8b730
 #include <sys/un.h>
c8b730
-#ifdef __BIONIC__
c8b730
-#include <linux/fcntl.h>
c8b730
-#else
c8b730
-#include <sys/fcntl.h>
c8b730
-#endif
c8b730
+#include <fcntl.h>
c8b730
 #include <netinet/in.h>
c8b730
 #include <stdlib.h>
c8b730
 #include <errno.h>
c8b730
@@ -47,22 +43,22 @@
c8b730
 #include <stddef.h>
c8b730
 #include <limits.h>
c8b730
 
c8b730
-#if defined(__linux__)
c8b730
-#include <mqueue.h>
c8b730
+#if defined(__linux__) && !defined(SD_DAEMON_DISABLE_MQ)
c8b730
+#  include <mqueue.h>
c8b730
 #endif
c8b730
 
c8b730
 #include "sd-daemon.h"
c8b730
 
c8b730
 #if (__GNUC__ >= 4)
c8b730
-#ifdef SD_EXPORT_SYMBOLS
c8b730
+#  ifdef SD_EXPORT_SYMBOLS
c8b730
 /* Export symbols */
c8b730
-#define _sd_export_ __attribute__ ((visibility("default")))
c8b730
-#else
c8b730
+#    define _sd_export_ __attribute__ ((visibility("default")))
c8b730
+#  else
c8b730
 /* Don't export the symbols */
c8b730
-#define _sd_export_ __attribute__ ((visibility("hidden")))
c8b730
-#endif
c8b730
+#    define _sd_export_ __attribute__ ((visibility("hidden")))
c8b730
+#  endif
c8b730
 #else
c8b730
-#define _sd_export_
c8b730
+#  define _sd_export_
c8b730
 #endif
c8b730
 
c8b730
 _sd_export_ int sd_listen_fds(int unset_environment) {
c8b730
@@ -75,7 +71,8 @@ _sd_export_ int sd_listen_fds(int unset_environment) {
c8b730
         char *p = NULL;
c8b730
         unsigned long l;
c8b730
 
c8b730
-        if (!(e = getenv("LISTEN_PID"))) {
c8b730
+        e = getenv("LISTEN_PID");
c8b730
+        if (!e) {
c8b730
                 r = 0;
c8b730
                 goto finish;
c8b730
         }
c8b730
@@ -83,12 +80,12 @@ _sd_export_ int sd_listen_fds(int unset_environment) {
c8b730
         errno = 0;
c8b730
         l = strtoul(e, &p, 10);
c8b730
 
c8b730
-        if (errno != 0) {
c8b730
+        if (errno > 0) {
c8b730
                 r = -errno;
c8b730
                 goto finish;
c8b730
         }
c8b730
 
c8b730
-        if (!p || *p || l <= 0) {
c8b730
+        if (!p || p == e || *p || l <= 0) {
c8b730
                 r = -EINVAL;
c8b730
                 goto finish;
c8b730
         }
c8b730
@@ -99,7 +96,8 @@ _sd_export_ int sd_listen_fds(int unset_environment) {
c8b730
                 goto finish;
c8b730
         }
c8b730
 
c8b730
-        if (!(e = getenv("LISTEN_FDS"))) {
c8b730
+        e = getenv("LISTEN_FDS");
c8b730
+        if (!e) {
c8b730
                 r = 0;
c8b730
                 goto finish;
c8b730
         }
c8b730
@@ -107,12 +105,12 @@ _sd_export_ int sd_listen_fds(int unset_environment) {
c8b730
         errno = 0;
c8b730
         l = strtoul(e, &p, 10);
c8b730
 
c8b730
-        if (errno != 0) {
c8b730
+        if (errno > 0) {
c8b730
                 r = -errno;
c8b730
                 goto finish;
c8b730
         }
c8b730
 
c8b730
-        if (!p || *p) {
c8b730
+        if (!p || p == e || *p) {
c8b730
                 r = -EINVAL;
c8b730
                 goto finish;
c8b730
         }
c8b730
@@ -120,7 +118,8 @@ _sd_export_ int sd_listen_fds(int unset_environment) {
c8b730
         for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + (int) l; fd ++) {
c8b730
                 int flags;
c8b730
 
c8b730
-                if ((flags = fcntl(fd, F_GETFD)) < 0) {
c8b730
+                flags = fcntl(fd, F_GETFD);
c8b730
+                if (flags < 0) {
c8b730
                         r = -errno;
c8b730
                         goto finish;
c8b730
                 }
c8b730
@@ -152,7 +151,6 @@ _sd_export_ int sd_is_fifo(int fd, const char *path) {
c8b730
         if (fd < 0)
c8b730
                 return -EINVAL;
c8b730
 
c8b730
-        memset(&st_fd, 0, sizeof(st_fd));
c8b730
         if (fstat(fd, &st_fd) < 0)
c8b730
                 return -errno;
c8b730
 
c8b730
@@ -162,7 +160,6 @@ _sd_export_ int sd_is_fifo(int fd, const char *path) {
c8b730
         if (path) {
c8b730
                 struct stat st_path;
c8b730
 
c8b730
-                memset(&st_path, 0, sizeof(st_path));
c8b730
                 if (stat(path, &st_path) < 0) {
c8b730
 
c8b730
                         if (errno == ENOENT || errno == ENOTDIR)
c8b730
@@ -272,15 +269,13 @@ _sd_export_ int sd_is_socket(int fd, int family, int type, int listening) {
c8b730
         if (family < 0)
c8b730
                 return -EINVAL;
c8b730
 
c8b730
-        if ((r = sd_is_socket_internal(fd, type, listening)) <= 0)
c8b730
+        r = sd_is_socket_internal(fd, type, listening);
c8b730
+        if (r <= 0)
c8b730
                 return r;
c8b730
 
c8b730
         if (family > 0) {
c8b730
-                union sockaddr_union sockaddr;
c8b730
-                socklen_t l;
c8b730
-
c8b730
-                memset(&sockaddr, 0, sizeof(sockaddr));
c8b730
-                l = sizeof(sockaddr);
c8b730
+                union sockaddr_union sockaddr = {};
c8b730
+                socklen_t l = sizeof(sockaddr);
c8b730
 
c8b730
                 if (getsockname(fd, &sockaddr.sa, &l) < 0)
c8b730
                         return -errno;
c8b730
@@ -295,19 +290,17 @@ _sd_export_ int sd_is_socket(int fd, int family, int type, int listening) {
c8b730
 }
c8b730
 
c8b730
 _sd_export_ int sd_is_socket_inet(int fd, int family, int type, int listening, uint16_t port) {
c8b730
-        union sockaddr_union sockaddr;
c8b730
-        socklen_t l;
c8b730
+        union sockaddr_union sockaddr = {};
c8b730
+        socklen_t l = sizeof(sockaddr);
c8b730
         int r;
c8b730
 
c8b730
         if (family != 0 && family != AF_INET && family != AF_INET6)
c8b730
                 return -EINVAL;
c8b730
 
c8b730
-        if ((r = sd_is_socket_internal(fd, type, listening)) <= 0)
c8b730
+        r = sd_is_socket_internal(fd, type, listening);
c8b730
+        if (r <= 0)
c8b730
                 return r;
c8b730
 
c8b730
-        memset(&sockaddr, 0, sizeof(sockaddr));
c8b730
-        l = sizeof(sockaddr);
c8b730
-
c8b730
         if (getsockname(fd, &sockaddr.sa, &l) < 0)
c8b730
                 return -errno;
c8b730
 
c8b730
@@ -340,16 +333,14 @@ _sd_export_ int sd_is_socket_inet(int fd, int family, int type, int listening, u
c8b730
 }
c8b730
 
c8b730
 _sd_export_ int sd_is_socket_unix(int fd, int type, int listening, const char *path, size_t length) {
c8b730
-        union sockaddr_union sockaddr;
c8b730
-        socklen_t l;
c8b730
+        union sockaddr_union sockaddr = {};
c8b730
+        socklen_t l = sizeof(sockaddr);
c8b730
         int r;
c8b730
 
c8b730
-        if ((r = sd_is_socket_internal(fd, type, listening)) <= 0)
c8b730
+        r = sd_is_socket_internal(fd, type, listening);
c8b730
+        if (r <= 0)
c8b730
                 return r;
c8b730
 
c8b730
-        memset(&sockaddr, 0, sizeof(sockaddr));
c8b730
-        l = sizeof(sockaddr);
c8b730
-
c8b730
         if (getsockname(fd, &sockaddr.sa, &l) < 0)
c8b730
                 return -errno;
c8b730
 
c8b730
@@ -360,10 +351,10 @@ _sd_export_ int sd_is_socket_unix(int fd, int type, int listening, const char *p
c8b730
                 return 0;
c8b730
 
c8b730
         if (path) {
c8b730
-                if (length <= 0)
c8b730
+                if (length == 0)
c8b730
                         length = strlen(path);
c8b730
 
c8b730
-                if (length <= 0)
c8b730
+                if (length == 0)
c8b730
                         /* Unnamed socket */
c8b730
                         return l == offsetof(struct sockaddr_un, sun_path);
c8b730
 
c8b730
@@ -383,7 +374,7 @@ _sd_export_ int sd_is_socket_unix(int fd, int type, int listening, const char *p
c8b730
 }
c8b730
 
c8b730
 _sd_export_ int sd_is_mq(int fd, const char *path) {
c8b730
-#if !defined(__linux__)
c8b730
+#if !defined(__linux__) || defined(SD_DAEMON_DISABLE_MQ)
c8b730
         return 0;
c8b730
 #else
c8b730
         struct mq_attr attr;
c8b730
@@ -434,7 +425,8 @@ _sd_export_ int sd_notify(int unset_environment, const char *state) {
c8b730
                 goto finish;
c8b730
         }
c8b730
 
c8b730
-        if (!(e = getenv("NOTIFY_SOCKET")))
c8b730
+        e = getenv("NOTIFY_SOCKET");
c8b730
+        if (!e)
c8b730
                 return 0;
c8b730
 
c8b730
         /* Must be an abstract socket, or an absolute path */
c8b730
@@ -443,7 +435,8 @@ _sd_export_ int sd_notify(int unset_environment, const char *state) {
c8b730
                 goto finish;
c8b730
         }
c8b730
 
c8b730
-        if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0) {
c8b730
+        fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
c8b730
+        if (fd < 0) {
c8b730
                 r = -errno;
c8b730
                 goto finish;
c8b730
         }
c8b730
@@ -513,18 +506,15 @@ _sd_export_ int sd_booted(void) {
c8b730
 #if defined(DISABLE_SYSTEMD) || !defined(__linux__)
c8b730
         return 0;
c8b730
 #else
c8b730
+        struct stat st;
c8b730
 
c8b730
-        struct stat a, b;
c8b730
+        /* We test whether the runtime unit file directory has been
c8b730
+         * created. This takes place in mount-setup.c, so is
c8b730
+         * guaranteed to happen very early during boot. */
c8b730
 
c8b730
-        /* We simply test whether the systemd cgroup hierarchy is
c8b730
-         * mounted */
c8b730
-
c8b730
-        if (lstat("/sys/fs/cgroup", &a) < 0)
c8b730
-                return 0;
c8b730
-
c8b730
-        if (lstat("/sys/fs/cgroup/systemd", &b) < 0)
c8b730
+        if (lstat("/run/systemd/system/", &st) < 0)
c8b730
                 return 0;
c8b730
 
c8b730
-        return a.st_dev != b.st_dev;
c8b730
+        return !!S_ISDIR(st.st_mode);
c8b730
 #endif
c8b730
 }
c8b730
diff --git a/sd-daemon.h b/sd-daemon.h
c8b730
index fe51159ee6..daa3f4c857 100644
c8b730
--- a/sd-daemon.h
c8b730
+++ b/sd-daemon.h
c8b730
@@ -59,20 +59,20 @@ extern "C" {
c8b730
   You may find an up-to-date version of these source files online:
c8b730
 
c8b730
   http://cgit.freedesktop.org/systemd/systemd/plain/src/systemd/sd-daemon.h
c8b730
-  http://cgit.freedesktop.org/systemd/systemd/plain/src/sd-daemon.c
c8b730
+  http://cgit.freedesktop.org/systemd/systemd/plain/src/libsystemd-daemon/sd-daemon.c
c8b730
 
c8b730
   This should compile on non-Linux systems, too, but with the
c8b730
   exception of the sd_is_xxx() calls all functions will become NOPs.
c8b730
 
c8b730
-  See sd-daemon(7) for more information.
c8b730
+  See sd-daemon(3) for more information.
c8b730
 */
c8b730
 
c8b730
 #ifndef _sd_printf_attr_
c8b730
-#if __GNUC__ >= 4
c8b730
-#define _sd_printf_attr_(a,b) __attribute__ ((format (printf, a, b)))
c8b730
-#else
c8b730
-#define _sd_printf_attr_(a,b)
c8b730
-#endif
c8b730
+#  if __GNUC__ >= 4
c8b730
+#    define _sd_printf_attr_(a,b) __attribute__ ((format (printf, a, b)))
c8b730
+#  else
c8b730
+#    define _sd_printf_attr_(a,b)
c8b730
+#  endif
c8b730
 #endif
c8b730
 
c8b730
 /*