[systemd-devel] [PATCH] Fix: Cannot call sd_pid_notify_with_fds with valid pid

Benjamin Robin dev at benjarobin.fr
Sat Sep 19 09:54:45 PDT 2015


The tool called this way: "systemd-notify --pid=$$" is not working

When calling sd_pid_notify* functions with a valid pid (pid != 0),
the sendmsg failed.
The msg_controllen is invalid, because CMSG_SPACE(0) is not equal to 0.

Signed-off-by: Benjamin Robin <dev at benjarobin.fr>
---
 src/libsystemd/sd-daemon/sd-daemon.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/src/libsystemd/sd-daemon/sd-daemon.c b/src/libsystemd/sd-daemon/sd-daemon.c
index d230a48..f7608c7 100644
--- a/src/libsystemd/sd-daemon/sd-daemon.c
+++ b/src/libsystemd/sd-daemon/sd-daemon.c
@@ -395,8 +395,12 @@ _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);
+                if (n_fds > 0) {
+                        msghdr.msg_controllen += CMSG_SPACE(sizeof(int) * n_fds);
+                }
+                if (have_pid) {
+                        msghdr.msg_controllen += CMSG_SPACE(sizeof(struct ucred));
+                }
                 msghdr.msg_control = alloca(msghdr.msg_controllen);

                 cmsg = CMSG_FIRSTHDR(&msghdr);
--
2.5.1



More information about the systemd-devel mailing list