1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2011 Lennart Poettering
18 #include "process-util.h"
19 #include "spawn-polkit-agent.h"
20 #include "stdio-util.h"
21 #include "time-util.h"
25 static pid_t agent_pid = 0;
27 int polkit_agent_open(void) {
28 char notify_fd[DECIMAL_STR_MAX(int) + 1];
34 /* Clients that run as root don't need to activate/query polkit */
38 /* We check STDIN here, not STDOUT, since this is about input, not output */
39 if (!isatty(STDIN_FILENO))
42 if (!is_main_thread())
45 if (pipe2(pipe_fd, 0) < 0)
48 xsprintf(notify_fd, "%i", pipe_fd[1]);
50 r = fork_agent("(polkit-agent)",
53 POLKIT_AGENT_BINARY_PATH,
54 POLKIT_AGENT_BINARY_PATH, "--notify-fd", notify_fd, "--fallback", NULL);
56 /* Close the writing side, because that's the one for the agent */
57 safe_close(pipe_fd[1]);
60 log_error_errno(r, "Failed to fork TTY ask password agent: %m");
62 /* Wait until the agent closes the fd */
63 fd_wait_for_event(pipe_fd[0], POLLHUP, USEC_INFINITY);
65 safe_close(pipe_fd[0]);
70 void polkit_agent_close(void) {
75 /* Inform agent that we are done */
76 (void) kill_and_sigcont(agent_pid, SIGTERM);
77 (void) wait_for_terminate(agent_pid, NULL);
83 int polkit_agent_open(void) {
87 void polkit_agent_close(void) {