]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd_pid_notify_with_fds: fix computing msg_controllen 1215/head
authorMaciej Wereski <m.wereski@partner.samsung.com>
Tue, 8 Sep 2015 13:36:30 +0000 (15:36 +0200)
committerMaciej Wereski <m.wereski@partner.samsung.com>
Wed, 9 Sep 2015 12:28:43 +0000 (14:28 +0200)
CMSG_SPACE(0) may return value other than 0. This caused sendmsg to fail
with EINVAL, when have_pid or n_fds was 0.

src/libsystemd/sd-daemon/sd-daemon.c

index d230a48dafbb99d6bfc6b8d03a2417c4962eb1c4..9ec73406c6b44f2f6713ccc57f26d90c2aafd9d5 100644 (file)
@@ -395,8 +395,9 @@ _public_ int sd_pid_notify_with_fds(pid_t pid, int unset_environment, const char
         have_pid = pid != 0 && pid != getpid();
 
         if (n_fds > 0 || have_pid) {
-                msghdr.msg_controllen = CMSG_SPACE(sizeof(int) * n_fds) +
-                                        CMSG_SPACE(sizeof(struct ucred) * have_pid);
+                /* CMSG_SPACE(0) may return value different then zero, which results in miscalculated controllen. */
+                msghdr.msg_controllen = (n_fds ? CMSG_SPACE(sizeof(int) * n_fds) : 0) +
+                                        CMSG_SPACE(sizeof(struct ucred)) * have_pid;
                 msghdr.msg_control = alloca(msghdr.msg_controllen);
 
                 cmsg = CMSG_FIRSTHDR(&msghdr);