richardphibel / rpms / systemd

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