]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-daemon: minor modernization, use assert_return
authorMike Yuan <me@yhndnzj.com>
Sat, 27 Apr 2024 11:26:49 +0000 (19:26 +0800)
committerMike Yuan <me@yhndnzj.com>
Sat, 27 Apr 2024 11:26:49 +0000 (19:26 +0800)
src/libsystemd/sd-daemon/sd-daemon.c

index 58626463a8909ba3175c1fa5262cd693038c6c87..16a8ac6cce157967b1d6cfb1b2b1ae574a9b7ee4 100644 (file)
@@ -456,6 +456,7 @@ static int pid_notify_with_fds_internal(
                 const char *state,
                 const int *fds,
                 unsigned n_fds) {
+
         SocketAddress address;
         struct iovec iovec;
         struct msghdr msghdr = {
@@ -464,19 +465,12 @@ static int pid_notify_with_fds_internal(
                 .msg_name = &address.sockaddr,
         };
         _cleanup_close_ int fd = -EBADF;
-        struct cmsghdr *cmsg = NULL;
-        const char *e;
-        bool send_ucred;
-        ssize_t n;
         int type, r;
 
-        if (!state)
-                return -EINVAL;
+        assert_return(state, -EINVAL);
+        assert_return(fds || n_fds == 0, -EINVAL);
 
-        if (n_fds > 0 && !fds)
-                return -EINVAL;
-
-        e = getenv("NOTIFY_SOCKET");
+        const char *e = getenv("NOTIFY_SOCKET");
         if (!e)
                 return 0;
 
@@ -530,12 +524,14 @@ static int pid_notify_with_fds_internal(
 
         iovec = IOVEC_MAKE_STRING(state);
 
-        send_ucred =
+        bool send_ucred =
                 (pid != 0 && pid != getpid_cached()) ||
                 getuid() != geteuid() ||
                 getgid() != getegid();
 
         if (n_fds > 0 || send_ucred) {
+                struct cmsghdr *cmsg;
+
                 /* CMSG_SPACE(0) may return value different than zero, which results in miscalculated controllen. */
                 msghdr.msg_controllen =
                         (n_fds > 0 ? CMSG_SPACE(sizeof(int) * n_fds) : 0) +
@@ -569,6 +565,8 @@ static int pid_notify_with_fds_internal(
                 }
         }
 
+        ssize_t n;
+
         do {
                 /* First try with fake ucred data, as requested */
                 n = sendmsg(fd, &msghdr, MSG_NOSIGNAL);