]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/spawn-polkit-agent.c
util-lib: split out IO related calls to io-util.[ch]
[thirdparty/systemd.git] / src / shared / spawn-polkit-agent.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2011 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <signal.h>
25 #include <errno.h>
26 #include <poll.h>
27
28 #include "fd-util.h"
29 #include "io-util.h"
30 #include "log.h"
31 #include "process-util.h"
32 #include "spawn-polkit-agent.h"
33 #include "util.h"
34
35 #ifdef ENABLE_POLKIT
36 static pid_t agent_pid = 0;
37
38 int polkit_agent_open(void) {
39 int r;
40 int pipe_fd[2];
41 char notify_fd[DECIMAL_STR_MAX(int) + 1];
42
43 if (agent_pid > 0)
44 return 0;
45
46 /* We check STDIN here, not STDOUT, since this is about input,
47 * not output */
48 if (!isatty(STDIN_FILENO))
49 return 0;
50
51 if (pipe2(pipe_fd, 0) < 0)
52 return -errno;
53
54 xsprintf(notify_fd, "%i", pipe_fd[1]);
55
56 r = fork_agent(&agent_pid,
57 &pipe_fd[1], 1,
58 POLKIT_AGENT_BINARY_PATH,
59 POLKIT_AGENT_BINARY_PATH, "--notify-fd", notify_fd, "--fallback", NULL);
60
61 /* Close the writing side, because that's the one for the agent */
62 safe_close(pipe_fd[1]);
63
64 if (r < 0)
65 log_error_errno(r, "Failed to fork TTY ask password agent: %m");
66 else
67 /* Wait until the agent closes the fd */
68 fd_wait_for_event(pipe_fd[0], POLLHUP, USEC_INFINITY);
69
70 safe_close(pipe_fd[0]);
71
72 return r;
73 }
74
75 void polkit_agent_close(void) {
76
77 if (agent_pid <= 0)
78 return;
79
80 /* Inform agent that we are done */
81 kill(agent_pid, SIGTERM);
82 kill(agent_pid, SIGCONT);
83 (void) wait_for_terminate(agent_pid, NULL);
84 agent_pid = 0;
85 }
86
87 #else
88
89 int polkit_agent_open(void) {
90 return 0;
91 }
92
93 void polkit_agent_close(void) {
94 }
95
96 #endif