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

Benjamin Robin dev at benjarobin.fr
Sat Sep 19 07:20:49 PDT 2015


The "systemd-notify --pid=$$" call 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.

Further more the documentation is not exactly right, it said:

--pid= Inform the init system about the main PID of the daemon. ...
This is equivalent to systemd-notify MAINPID=$PID.

With the --pid option the sd_pid_notify function is called with a valid pid.
And with the MAINPID= option the sd_pid_notify function is called
without a valid pid (pid = 0)

*****************************
Fix sd_pid_notify_with_fds with valid pid. The msg_controllen
 was invalid, 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