From 388d6c5f37238a9f5c6a2abe231d9b2633ba8b3b Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Tue, 3 Dec 2024 18:36:15 +0900 Subject: [PATCH] polkit-agent: modernize code a bit - Use _cleanup_close_pair_ attribute for the pipe FDs, - Return earlier on failure in forking polkit agent. --- src/shared/polkit-agent.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/shared/polkit-agent.c b/src/shared/polkit-agent.c index d87eb56164..4a0d83e8ba 100644 --- a/src/shared/polkit-agent.c +++ b/src/shared/polkit-agent.c @@ -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]); + pipe_fd[1] = 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); + /* Wait until the agent closes the fd */ + (void) fd_wait_for_event(pipe_fd[0], POLLHUP, USEC_INFINITY); - safe_close(pipe_fd[0]); - - return r; + return 1; } void polkit_agent_close(void) {