1 /* SPDX-License-Identifier: LGPL-2.1+ */
13 #include "process-util.h"
14 #include "spawn-polkit-agent.h"
15 #include "stdio-util.h"
16 #include "time-util.h"
20 static pid_t agent_pid = 0;
22 int polkit_agent_open(void) {
23 char notify_fd[DECIMAL_STR_MAX(int) + 1];
29 /* Clients that run as root don't need to activate/query polkit */
33 /* We check STDIN here, not STDOUT, since this is about input, not output */
34 if (!isatty(STDIN_FILENO))
37 if (!is_main_thread())
40 if (pipe2(pipe_fd, 0) < 0)
43 xsprintf(notify_fd, "%i", pipe_fd[1]);
45 r = fork_agent("(polkit-agent)",
48 POLKIT_AGENT_BINARY_PATH,
49 POLKIT_AGENT_BINARY_PATH, "--notify-fd", notify_fd, "--fallback", NULL);
51 /* Close the writing side, because that's the one for the agent */
52 safe_close(pipe_fd[1]);
55 log_error_errno(r, "Failed to fork TTY ask password agent: %m");
57 /* Wait until the agent closes the fd */
58 fd_wait_for_event(pipe_fd[0], POLLHUP, USEC_INFINITY);
60 safe_close(pipe_fd[0]);
65 void polkit_agent_close(void) {
70 /* Inform agent that we are done */
71 (void) kill_and_sigcont(agent_pid, SIGTERM);
72 (void) wait_for_terminate(agent_pid, NULL);
78 int polkit_agent_open(void) {
82 void polkit_agent_close(void) {