]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
polkit-agent: modernize code a bit
authorYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 3 Dec 2024 09:36:15 +0000 (18:36 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 11 Dec 2024 23:30:55 +0000 (08:30 +0900)
- Use _cleanup_close_pair_ attribute for the pipe FDs,
- Return earlier on failure in forking polkit agent.

src/shared/polkit-agent.c

index d87eb56164dc175757f505a4424b3f2e30601281..4a0d83e8ba9146e2c39897afe60935928c8f510a 100644 (file)
@@ -21,8 +21,9 @@
 static pid_t agent_pid = 0;
 
 int polkit_agent_open(void) {
+        _cleanup_close_pair_ int pipe_fd[2] = EBADF_PAIR;
         char notify_fd[DECIMAL_STR_MAX(int) + 1];
-        int pipe_fd[2], r;
+        int r;
 
         if (agent_pid > 0)
                 return 0;
@@ -55,19 +56,16 @@ int polkit_agent_open(void) {
                        POLKIT_AGENT_BINARY_PATH,
                        "--notify-fd", notify_fd,
                        "--fallback");
+        if (r < 0)
+                return log_error_errno(r, "Failed to fork polkit agent: %m");
 
         /* Close the writing side, because that's the one for the agent */
-        safe_close(pipe_fd[1]);
-
-        if (r < 0)
-                log_error_errno(r, "Failed to fork polkit agent: %m");
-        else
-                /* Wait until the agent closes the fd */
-                (void) fd_wait_for_event(pipe_fd[0], POLLHUP, USEC_INFINITY);
+        pipe_fd[1] = safe_close(pipe_fd[1]);
 
-        safe_close(pipe_fd[0]);
+        /* Wait until the agent closes the fd */
+        (void) fd_wait_for_event(pipe_fd[0], POLLHUP, USEC_INFINITY);
 
-        return r;
+        return 1;
 }
 
 void polkit_agent_close(void) {