anitazha / rpms / systemd

Forked from rpms/systemd 3 years ago
Clone
Blob Blame History Raw
From f065b88b17bd569dda412b2e6f34d921f7badb79 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
Date: Fri, 13 Mar 2015 21:22:05 -0500
Subject: [PATCH] sd-daemon: simplify sd_pid_notify_with_fds

Coverity was complaining that CMSG_NXTHDR is used without
checking the return value. In this case it cannot fail, but
it is a good excuse to simplify the function a bit.

CID #1261726.

(cherry picked from commit 64144440a5d2d94482f882b992fd2a4e0dca7a05)

http://lists.freedesktop.org/archives/systemd-devel/2015-April/031348.html

Cherry-picked from: c1258d6
Resolves: #1222517
---
 src/libsystemd/sd-daemon/sd-daemon.c | 61 ++++++++++++----------------
 1 file changed, 27 insertions(+), 34 deletions(-)

diff --git a/src/libsystemd/sd-daemon/sd-daemon.c b/src/libsystemd/sd-daemon/sd-daemon.c
index 22a3a5347a..1474321c95 100644
--- a/src/libsystemd/sd-daemon/sd-daemon.c
+++ b/src/libsystemd/sd-daemon/sd-daemon.c
@@ -352,12 +352,10 @@ _public_ int sd_pid_notify_with_fds(pid_t pid, int unset_environment, const char
                 .msg_iovlen = 1,
                 .msg_name = &sockaddr,
         };
-        struct cmsghdr *control;
         _cleanup_close_ int fd = -1;
         struct cmsghdr *cmsg = NULL;
         const char *e;
-        size_t controllen_without_ucred = 0;
-        bool try_without_ucred = false;
+        bool have_pid;
         int r;
 
         if (!state) {
@@ -396,42 +394,37 @@ _public_ int sd_pid_notify_with_fds(pid_t pid, int unset_environment, const char
         if (msghdr.msg_namelen > sizeof(struct sockaddr_un))
                 msghdr.msg_namelen = sizeof(struct sockaddr_un);
 
-        control = alloca(CMSG_SPACE(sizeof(struct ucred)) + CMSG_SPACE(sizeof(int) * n_fds));
+        have_pid = pid != 0 && pid != getpid();
 
-        if (n_fds > 0) {
-                msghdr.msg_control = control;
-                msghdr.msg_controllen = CMSG_LEN(sizeof(int) * n_fds);
+        if (n_fds > 0 || have_pid) {
+                msghdr.msg_controllen = CMSG_SPACE(sizeof(int) * n_fds) +
+                                        CMSG_SPACE(sizeof(struct ucred) * have_pid);
+                msghdr.msg_control = alloca(msghdr.msg_controllen);
 
                 cmsg = CMSG_FIRSTHDR(&msghdr);
-                cmsg->cmsg_level = SOL_SOCKET;
-                cmsg->cmsg_type = SCM_RIGHTS;
-                cmsg->cmsg_len = CMSG_LEN(sizeof(int) * n_fds);
+                if (n_fds > 0) {
+                        cmsg->cmsg_level = SOL_SOCKET;
+                        cmsg->cmsg_type = SCM_RIGHTS;
+                        cmsg->cmsg_len = CMSG_LEN(sizeof(int) * n_fds);
 
-                memcpy(CMSG_DATA(cmsg), fds, sizeof(int) * n_fds);
-        }
-
-        if (pid != 0 && pid != getpid()) {
-                struct ucred *ucred;
-
-                try_without_ucred = true;
-                controllen_without_ucred = msghdr.msg_controllen;
+                        memcpy(CMSG_DATA(cmsg), fds, sizeof(int) * n_fds);
 
-                msghdr.msg_control = control;
-                msghdr.msg_controllen += CMSG_LEN(sizeof(struct ucred));
+                        if (have_pid)
+                                assert_se(cmsg = CMSG_NXTHDR(&msghdr, cmsg));
+                }
 
-                if (cmsg)
-                        cmsg = CMSG_NXTHDR(&msghdr, cmsg);
-                else
-                        cmsg = CMSG_FIRSTHDR(&msghdr);
+                if (have_pid) {
+                        struct ucred *ucred;
 
-                cmsg->cmsg_level = SOL_SOCKET;
-                cmsg->cmsg_type = SCM_CREDENTIALS;
-                cmsg->cmsg_len = CMSG_LEN(sizeof(struct ucred));
+                        cmsg->cmsg_level = SOL_SOCKET;
+                        cmsg->cmsg_type = SCM_CREDENTIALS;
+                        cmsg->cmsg_len = CMSG_LEN(sizeof(struct ucred));
 
-                ucred = (struct ucred*) CMSG_DATA(cmsg);
-                ucred->pid = pid;
-                ucred->uid = getuid();
-                ucred->gid = getgid();
+                        ucred = (struct ucred*) CMSG_DATA(cmsg);
+                        ucred->pid = pid;
+                        ucred->uid = getuid();
+                        ucred->gid = getgid();
+                }
         }
 
         /* First try with fake ucred data, as requested */
@@ -441,10 +434,10 @@ _public_ int sd_pid_notify_with_fds(pid_t pid, int unset_environment, const char
         }
 
         /* If that failed, try with our own ucred instead */
-        if (try_without_ucred) {
-                if (controllen_without_ucred <= 0)
+        if (have_pid) {
+                msghdr.msg_controllen -= CMSG_SPACE(sizeof(struct ucred));
+                if (msghdr.msg_controllen == 0)
                         msghdr.msg_control = NULL;
-                msghdr.msg_controllen = controllen_without_ucred;
 
                 if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) >= 0) {
                         r = 1;