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