]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
6bb92a16 LP |
2 | /*** |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright 2011 Lennart Poettering | |
6 | ||
7 | systemd is free software; you can redistribute it and/or modify it | |
5430f7f2 LP |
8 | under the terms of the GNU Lesser General Public License as published by |
9 | the Free Software Foundation; either version 2.1 of the License, or | |
6bb92a16 LP |
10 | (at your option) any later version. |
11 | ||
12 | systemd is distributed in the hope that it will be useful, but | |
13 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
5430f7f2 | 15 | Lesser General Public License for more details. |
6bb92a16 | 16 | |
5430f7f2 | 17 | You should have received a copy of the GNU Lesser General Public License |
6bb92a16 LP |
18 | along with systemd; If not, see <http://www.gnu.org/licenses/>. |
19 | ***/ | |
20 | ||
9bdc770c | 21 | #include <errno.h> |
0a6f50c0 | 22 | #include <poll.h> |
cf0fbc49 TA |
23 | #include <signal.h> |
24 | #include <stdlib.h> | |
25 | #include <unistd.h> | |
6bb92a16 | 26 | |
c004493c LP |
27 | #include "fd-util.h" |
28 | #include "io-util.h" | |
6bb92a16 | 29 | #include "log.h" |
a8fbdf54 | 30 | #include "macro.h" |
0b452006 | 31 | #include "process-util.h" |
6bb92a16 | 32 | #include "spawn-polkit-agent.h" |
15a5e950 | 33 | #include "stdio-util.h" |
a8fbdf54 | 34 | #include "time-util.h" |
c004493c | 35 | #include "util.h" |
6bb92a16 | 36 | |
349cc4a5 | 37 | #if ENABLE_POLKIT |
6bb92a16 LP |
38 | static pid_t agent_pid = 0; |
39 | ||
40 | int polkit_agent_open(void) { | |
5ffa8c81 | 41 | char notify_fd[DECIMAL_STR_MAX(int) + 1]; |
78752f2e | 42 | int pipe_fd[2], r; |
6bb92a16 LP |
43 | |
44 | if (agent_pid > 0) | |
45 | return 0; | |
46 | ||
89d03482 MP |
47 | /* Clients that run as root don't need to activate/query polkit */ |
48 | if (geteuid() == 0) | |
49 | return 0; | |
50 | ||
78752f2e | 51 | /* We check STDIN here, not STDOUT, since this is about input, not output */ |
6bb92a16 LP |
52 | if (!isatty(STDIN_FILENO)) |
53 | return 0; | |
54 | ||
9bdc770c LP |
55 | if (pipe2(pipe_fd, 0) < 0) |
56 | return -errno; | |
57 | ||
5ffa8c81 | 58 | xsprintf(notify_fd, "%i", pipe_fd[1]); |
9bdc770c | 59 | |
78752f2e | 60 | r = fork_agent("(polkit-agent)", |
9bdc770c | 61 | &pipe_fd[1], 1, |
78752f2e | 62 | &agent_pid, |
9bdc770c | 63 | POLKIT_AGENT_BINARY_PATH, |
8aec53fb | 64 | POLKIT_AGENT_BINARY_PATH, "--notify-fd", notify_fd, "--fallback", NULL); |
9bdc770c LP |
65 | |
66 | /* Close the writing side, because that's the one for the agent */ | |
03e334a1 | 67 | safe_close(pipe_fd[1]); |
9bdc770c | 68 | |
6bb92a16 | 69 | if (r < 0) |
da927ba9 | 70 | log_error_errno(r, "Failed to fork TTY ask password agent: %m"); |
9bdc770c LP |
71 | else |
72 | /* Wait until the agent closes the fd */ | |
3a43da28 | 73 | fd_wait_for_event(pipe_fd[0], POLLHUP, USEC_INFINITY); |
9bdc770c | 74 | |
03e334a1 | 75 | safe_close(pipe_fd[0]); |
6bb92a16 LP |
76 | |
77 | return r; | |
78 | } | |
79 | ||
80 | void polkit_agent_close(void) { | |
81 | ||
82 | if (agent_pid <= 0) | |
83 | return; | |
84 | ||
85 | /* Inform agent that we are done */ | |
0adc28ce | 86 | (void) kill_and_sigcont(agent_pid, SIGTERM); |
c73d180d | 87 | (void) wait_for_terminate(agent_pid, NULL); |
6bb92a16 LP |
88 | agent_pid = 0; |
89 | } | |
46ba8aae LP |
90 | |
91 | #else | |
92 | ||
93 | int polkit_agent_open(void) { | |
94 | return 0; | |
95 | } | |
96 | ||
97 | void polkit_agent_close(void) { | |
98 | } | |
99 | ||
100 | #endif |