anitazha / rpms / systemd

Forked from rpms/systemd 3 years ago
Clone

Blame SOURCES/0177-sd-daemon-simplify-sd_pid_notify_with_fds.patch

1ff636
From f065b88b17bd569dda412b2e6f34d921f7badb79 Mon Sep 17 00:00:00 2001
1ff636
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
1ff636
Date: Fri, 13 Mar 2015 21:22:05 -0500
1ff636
Subject: [PATCH] sd-daemon: simplify sd_pid_notify_with_fds
1ff636
1ff636
Coverity was complaining that CMSG_NXTHDR is used without
1ff636
checking the return value. In this case it cannot fail, but
1ff636
it is a good excuse to simplify the function a bit.
1ff636
1ff636
CID #1261726.
1ff636
1ff636
(cherry picked from commit 64144440a5d2d94482f882b992fd2a4e0dca7a05)
1ff636
1ff636
http://lists.freedesktop.org/archives/systemd-devel/2015-April/031348.html
1ff636
1ff636
Cherry-picked from: c1258d6
1ff636
Resolves: #1222517
1ff636
---
23b3cf
 src/libsystemd/sd-daemon/sd-daemon.c | 61 ++++++++++++----------------
1ff636
 1 file changed, 27 insertions(+), 34 deletions(-)
1ff636
1ff636
diff --git a/src/libsystemd/sd-daemon/sd-daemon.c b/src/libsystemd/sd-daemon/sd-daemon.c
c62b8e
index 22a3a5347a..1474321c95 100644
1ff636
--- a/src/libsystemd/sd-daemon/sd-daemon.c
1ff636
+++ b/src/libsystemd/sd-daemon/sd-daemon.c
1ff636
@@ -352,12 +352,10 @@ _public_ int sd_pid_notify_with_fds(pid_t pid, int unset_environment, const char
1ff636
                 .msg_iovlen = 1,
1ff636
                 .msg_name = &sockaddr,
1ff636
         };
1ff636
-        struct cmsghdr *control;
1ff636
         _cleanup_close_ int fd = -1;
1ff636
         struct cmsghdr *cmsg = NULL;
1ff636
         const char *e;
1ff636
-        size_t controllen_without_ucred = 0;
1ff636
-        bool try_without_ucred = false;
1ff636
+        bool have_pid;
1ff636
         int r;
1ff636
 
1ff636
         if (!state) {
1ff636
@@ -396,42 +394,37 @@ _public_ int sd_pid_notify_with_fds(pid_t pid, int unset_environment, const char
1ff636
         if (msghdr.msg_namelen > sizeof(struct sockaddr_un))
1ff636
                 msghdr.msg_namelen = sizeof(struct sockaddr_un);
1ff636
 
1ff636
-        control = alloca(CMSG_SPACE(sizeof(struct ucred)) + CMSG_SPACE(sizeof(int) * n_fds));
1ff636
+        have_pid = pid != 0 && pid != getpid();
1ff636
 
1ff636
-        if (n_fds > 0) {
1ff636
-                msghdr.msg_control = control;
1ff636
-                msghdr.msg_controllen = CMSG_LEN(sizeof(int) * n_fds);
1ff636
+        if (n_fds > 0 || have_pid) {
1ff636
+                msghdr.msg_controllen = CMSG_SPACE(sizeof(int) * n_fds) +
1ff636
+                                        CMSG_SPACE(sizeof(struct ucred) * have_pid);
1ff636
+                msghdr.msg_control = alloca(msghdr.msg_controllen);
1ff636
 
1ff636
                 cmsg = CMSG_FIRSTHDR(&msghdr);
1ff636
-                cmsg->cmsg_level = SOL_SOCKET;
1ff636
-                cmsg->cmsg_type = SCM_RIGHTS;
1ff636
-                cmsg->cmsg_len = CMSG_LEN(sizeof(int) * n_fds);
1ff636
+                if (n_fds > 0) {
1ff636
+                        cmsg->cmsg_level = SOL_SOCKET;
1ff636
+                        cmsg->cmsg_type = SCM_RIGHTS;
1ff636
+                        cmsg->cmsg_len = CMSG_LEN(sizeof(int) * n_fds);
1ff636
 
1ff636
-                memcpy(CMSG_DATA(cmsg), fds, sizeof(int) * n_fds);
1ff636
-        }
1ff636
-
1ff636
-        if (pid != 0 && pid != getpid()) {
1ff636
-                struct ucred *ucred;
1ff636
-
1ff636
-                try_without_ucred = true;
1ff636
-                controllen_without_ucred = msghdr.msg_controllen;
1ff636
+                        memcpy(CMSG_DATA(cmsg), fds, sizeof(int) * n_fds);
1ff636
 
1ff636
-                msghdr.msg_control = control;
1ff636
-                msghdr.msg_controllen += CMSG_LEN(sizeof(struct ucred));
1ff636
+                        if (have_pid)
1ff636
+                                assert_se(cmsg = CMSG_NXTHDR(&msghdr, cmsg));
1ff636
+                }
1ff636
 
1ff636
-                if (cmsg)
1ff636
-                        cmsg = CMSG_NXTHDR(&msghdr, cmsg);
1ff636
-                else
1ff636
-                        cmsg = CMSG_FIRSTHDR(&msghdr);
1ff636
+                if (have_pid) {
1ff636
+                        struct ucred *ucred;
1ff636
 
1ff636
-                cmsg->cmsg_level = SOL_SOCKET;
1ff636
-                cmsg->cmsg_type = SCM_CREDENTIALS;
1ff636
-                cmsg->cmsg_len = CMSG_LEN(sizeof(struct ucred));
1ff636
+                        cmsg->cmsg_level = SOL_SOCKET;
1ff636
+                        cmsg->cmsg_type = SCM_CREDENTIALS;
1ff636
+                        cmsg->cmsg_len = CMSG_LEN(sizeof(struct ucred));
1ff636
 
1ff636
-                ucred = (struct ucred*) CMSG_DATA(cmsg);
1ff636
-                ucred->pid = pid;
1ff636
-                ucred->uid = getuid();
1ff636
-                ucred->gid = getgid();
1ff636
+                        ucred = (struct ucred*) CMSG_DATA(cmsg);
1ff636
+                        ucred->pid = pid;
1ff636
+                        ucred->uid = getuid();
1ff636
+                        ucred->gid = getgid();
1ff636
+                }
1ff636
         }
1ff636
 
1ff636
         /* First try with fake ucred data, as requested */
1ff636
@@ -441,10 +434,10 @@ _public_ int sd_pid_notify_with_fds(pid_t pid, int unset_environment, const char
1ff636
         }
1ff636
 
1ff636
         /* If that failed, try with our own ucred instead */
1ff636
-        if (try_without_ucred) {
1ff636
-                if (controllen_without_ucred <= 0)
1ff636
+        if (have_pid) {
1ff636
+                msghdr.msg_controllen -= CMSG_SPACE(sizeof(struct ucred));
1ff636
+                if (msghdr.msg_controllen == 0)
1ff636
                         msghdr.msg_control = NULL;
1ff636
-                msghdr.msg_controllen = controllen_without_ucred;
1ff636
 
1ff636
                 if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) >= 0) {
1ff636
                         r = 1;