From: Maciej Wereski Date: Tue, 8 Sep 2015 13:36:30 +0000 (+0200) Subject: sd_pid_notify_with_fds: fix computing msg_controllen X-Git-Tag: v227~161^2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F1215%2Fhead;p=thirdparty%2Fsystemd.git sd_pid_notify_with_fds: fix computing msg_controllen CMSG_SPACE(0) may return value other than 0. This caused sendmsg to fail with EINVAL, when have_pid or n_fds was 0. --- diff --git a/src/libsystemd/sd-daemon/sd-daemon.c b/src/libsystemd/sd-daemon/sd-daemon.c index d230a48dafb..9ec73406c6b 100644 --- a/src/libsystemd/sd-daemon/sd-daemon.c +++ b/src/libsystemd/sd-daemon/sd-daemon.c @@ -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);