]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/spawn-polkit-agent.c
Merge pull request #14592 from keszybz/simplifications
[thirdparty/systemd.git] / src / shared / spawn-polkit-agent.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <errno.h>
4 #include <poll.h>
5 #include <signal.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8
9 #include "fd-util.h"
10 #include "io-util.h"
11 #include "log.h"
12 #include "macro.h"
13 #include "process-util.h"
14 #include "spawn-polkit-agent.h"
15 #include "stdio-util.h"
16 #include "time-util.h"
17 #include "util.h"
18
19 #if ENABLE_POLKIT
20 static pid_t agent_pid = 0;
21
22 int polkit_agent_open(void) {
23 char notify_fd[DECIMAL_STR_MAX(int) + 1];
24 int pipe_fd[2], r;
25
26 if (agent_pid > 0)
27 return 0;
28
29 /* Clients that run as root don't need to activate/query polkit */
30 if (geteuid() == 0)
31 return 0;
32
33 /* We check STDIN here, not STDOUT, since this is about input, not output */
34 if (!isatty(STDIN_FILENO))
35 return 0;
36
37 if (!is_main_thread())
38 return -EPERM;
39
40 if (pipe2(pipe_fd, 0) < 0)
41 return -errno;
42
43 xsprintf(notify_fd, "%i", pipe_fd[1]);
44
45 r = fork_agent("(polkit-agent)",
46 &pipe_fd[1], 1,
47 &agent_pid,
48 POLKIT_AGENT_BINARY_PATH,
49 POLKIT_AGENT_BINARY_PATH, "--notify-fd", notify_fd, "--fallback", NULL);
50
51 /* Close the writing side, because that's the one for the agent */
52 safe_close(pipe_fd[1]);
53
54 if (r < 0)
55 log_error_errno(r, "Failed to fork TTY ask password agent: %m");
56 else
57 /* Wait until the agent closes the fd */
58 fd_wait_for_event(pipe_fd[0], POLLHUP, USEC_INFINITY);
59
60 safe_close(pipe_fd[0]);
61
62 return r;
63 }
64
65 void polkit_agent_close(void) {
66
67 if (agent_pid <= 0)
68 return;
69
70 /* Inform agent that we are done */
71 (void) kill_and_sigcont(agent_pid, SIGTERM);
72 (void) wait_for_terminate(agent_pid, NULL);
73 agent_pid = 0;
74 }
75
76 #else
77
78 int polkit_agent_open(void) {
79 return 0;
80 }
81
82 void polkit_agent_close(void) {
83 }
84
85 #endif
86
87 int polkit_agent_open_if_enabled(BusTransport transport, bool ask_password) {
88
89 /* Open the polkit agent as a child process if necessary */
90
91 if (transport != BUS_TRANSPORT_LOCAL)
92 return 0;
93
94 if (!ask_password)
95 return 0;
96
97 return polkit_agent_open();
98 }