]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/spawn-polkit-agent.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
14 #include "process-util.h"
15 #include "spawn-polkit-agent.h"
16 #include "stdio-util.h"
17 #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 (void) 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 sigterm_wait(TAKE_PID(agent_pid
));
76 int polkit_agent_open(void) {
80 void polkit_agent_close(void) {
85 int polkit_agent_open_if_enabled(BusTransport transport
, bool ask_password
) {
87 /* Open the polkit agent as a child process if necessary */
89 if (transport
!= BUS_TRANSPORT_LOCAL
)
95 return polkit_agent_open();