Debian Codegraph @

lib/packagekit-glib2/pk-spawn-polkit-agent.c

raw
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 *
3 * Copyright (C) 2011 Lennart Poettering <[email protected]>
4 * Copyright (C) 2012 Matthias Klumpp <[email protected]>
5 *
6 * Licensed under the GNU Lesser General Public License Version 2.1
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but 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 <stdio.h>
23 #include <sys/types.h>
24 #ifdef __linux__
25 #include <sys/prctl.h>
26 #endif
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <string.h>
30 #include <dirent.h>
31 #include <signal.h>
32 #include <fcntl.h>
33 #include <errno.h>
34 #include <inttypes.h>
35 #include <sys/poll.h>
36 #include <sys/wait.h>
37 #include <glib.h>
38
39 #include "pk-spawn-polkit-agent.h"
40
41 #define memzero(x,l) (memset((x), 0, (l)))
42 #define zero(x) (memzero(&(x), sizeof(x)))
43
44 #define POLKIT_TTY_AGENT_BINARY_PATH "/usr/bin/pkttyagent"
45
46 static pid_t agent_pid = 0;
47
48 static int
49 fork_agent (pid_t *pid, const char *path, ...) {
50 pid_t parent_pid, n_agent_pid;
51 int fd;
52 gboolean stdout_is_tty, stderr_is_tty;
53 unsigned n, i;
54 va_list ap;
55 char **l;
56
57 g_return_val_if_fail (pid != 0, 0);
58 g_assert (path);
59
60 parent_pid = getpid ();
61
62 /* Spawns a temporary TTY agent, making sure it goes away when
63 * we go away */
64
65 n_agent_pid = fork ();
66 if (n_agent_pid < 0)
67 return -errno;
68
69 if (n_agent_pid != 0) {
70 *pid = n_agent_pid;
71 return 0;
72 }
73 #ifdef __linux__
74
75 /* In the child:
76 *
77 * Make sure the agent goes away when the parent dies */
78 if (prctl (PR_SET_PDEATHSIG, SIGTERM) < 0)
79 _exit (EXIT_FAILURE);
80 #endif
81 /* Check whether our parent died before we were able
82 * to set the death signal */
83 if (getppid () != parent_pid)
84 _exit (EXIT_SUCCESS);
85
86 /* TODO: it might be more clean to close all FDs so we don't leak them to the agent */
87
88 stdout_is_tty = isatty (STDOUT_FILENO);
89 stderr_is_tty = isatty (STDERR_FILENO);
90
91 if (!stdout_is_tty || !stderr_is_tty) {
92 /* Detach from stdout/stderr. and reopen
93 * /dev/tty for them. This is important to
94 * ensure that when systemctl is started via
95 * popen() or a similar call that expects to
96 * read EOF we actually do generate EOF and
97 * not delay this indefinitely by because we
98 * keep an unused copy of stdin around. */
99 fd = open("/dev/tty", O_WRONLY);
100 if (fd < 0) {
101 g_error ("Failed to open /dev/tty: %m");
102 _exit(EXIT_FAILURE);
103 }
104
105 if (!stdout_is_tty)
106 dup2(fd, STDOUT_FILENO);
107
108 if (!stderr_is_tty)
109 dup2(fd, STDERR_FILENO);
110
111 if (fd > 2)
112 close(fd);
113 }
114
115 /* Count arguments */
116 va_start (ap, path);
117 for (n = 0; va_arg(ap, char*); n++)
118 ;
119 va_end(ap);
120
121 /* Allocate strv */
122 l = alloca (sizeof(char *) * (n + 1));
123
124 /* Fill in arguments */
125 va_start (ap, path);
126 for (i = 0; i <= n; i++)
127 l[i] = va_arg (ap, char*);
128 va_end (ap);
129
130 execv (path, l);
131 _exit (EXIT_FAILURE);
132 }
133
134 static int
135 close_nointr (int fd) {
136 g_assert (fd >= 0);
137
138 for (;;) {
139 int r;
140
141 r = close (fd);
142 if (r >= 0)
143 return r;
144
145 if (errno != EINTR)
146 return -errno;
147 }
148 }
149
150 static void
151 close_nointr_nofail (int fd) {
152 int saved_errno = errno;
153
154 /* cannot fail, and guarantees errno is unchanged */
155
156 g_assert (close_nointr (fd) == 0);
157
158 errno = saved_errno;
159 }
160
161 static int
162 fd_wait_for_event (int fd, int event, uint64_t t) {
163 struct pollfd pollfd;
164 int r;
165
166 zero(pollfd);
167 pollfd.fd = fd;
168 pollfd.events = event;
169
170 r = poll(&pollfd, 1, t == (uint64_t) -1 ? -1 : (int) (t / 1000));
171 if (r < 0)
172 return -errno;
173
174 if (r == 0)
175 return 0;
176
177 return pollfd.revents;
178 }
179
180 static int
181 wait_for_terminate (pid_t pid) {
182 int status;
183 g_assert (pid >= 1);
184
185 for (;;) {
186 if (waitpid(pid, &status, 0) < 0) {
187 if (errno == EINTR)
188 continue;
189 return -errno;
190 }
191 return 0;
192 }
193 }
194
195 int
196 pk_polkit_agent_open (void) {
197 int r;
198 int pipe_fd[2];
199 char notify_fd[10 + 1];
200
201 if (agent_pid > 0)
202 return 0;
203
204 /* We check STDIN here, not STDOUT, since this is about input,
205 * not output */
206 if (!isatty (STDIN_FILENO))
207 return 0;
208
209 if (pipe (pipe_fd) < 0)
210 return -errno;
211
212 snprintf (notify_fd, sizeof (notify_fd), "%i", pipe_fd[1]);
213 notify_fd[sizeof (notify_fd) -1] = 0;
214
215 r = fork_agent (&agent_pid,
216 POLKIT_TTY_AGENT_BINARY_PATH,
217 POLKIT_TTY_AGENT_BINARY_PATH, "--notify-fd", notify_fd, "--fallback", NULL);
218
219 /* Close the writing side, because that's the one for the agent */
220 close_nointr_nofail (pipe_fd[1]);
221
222 if (r < 0)
223 g_warning ("Failed to fork TTY ask password agent: %s", strerror (-r));
224 else
225 /* Wait until the agent closes the fd */
226 fd_wait_for_event (pipe_fd[0], POLLHUP, (uint64_t) -1);
227
228 close_nointr_nofail (pipe_fd[0]);
229
230 return r;
231 }
232
233 void
234 pk_polkit_agent_close (void) {
235
236 if (agent_pid <= 0)
237 return;
238
239 /* Inform agent that we are done */
240 kill (agent_pid, SIGTERM);
241 kill (agent_pid, SIGCONT);
242 wait_for_terminate (agent_pid);
243 agent_pid = 0;
244 }