]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/spawn-polkit-agent.c
shared: add process-util.[ch]
[thirdparty/systemd.git] / src / shared / spawn-polkit-agent.c
CommitLineData
6bb92a16
LP
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
5430f7f2
LP
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
6bb92a16
LP
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
5430f7f2 16 Lesser General Public License for more details.
6bb92a16 17
5430f7f2 18 You should have received a copy of the GNU Lesser General Public License
6bb92a16
LP
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
6bb92a16
LP
22#include <stdlib.h>
23#include <unistd.h>
6bb92a16 24#include <signal.h>
9bdc770c 25#include <errno.h>
0a6f50c0 26#include <poll.h>
6bb92a16
LP
27
28#include "log.h"
29#include "util.h"
0b452006 30#include "process-util.h"
6bb92a16
LP
31#include "spawn-polkit-agent.h"
32
46ba8aae 33#ifdef ENABLE_POLKIT
6bb92a16
LP
34static pid_t agent_pid = 0;
35
36int polkit_agent_open(void) {
37 int r;
9bdc770c 38 int pipe_fd[2];
5ffa8c81 39 char notify_fd[DECIMAL_STR_MAX(int) + 1];
6bb92a16
LP
40
41 if (agent_pid > 0)
42 return 0;
43
44 /* We check STDIN here, not STDOUT, since this is about input,
45 * not output */
46 if (!isatty(STDIN_FILENO))
47 return 0;
48
9bdc770c
LP
49 if (pipe2(pipe_fd, 0) < 0)
50 return -errno;
51
5ffa8c81 52 xsprintf(notify_fd, "%i", pipe_fd[1]);
9bdc770c
LP
53
54 r = fork_agent(&agent_pid,
55 &pipe_fd[1], 1,
56 POLKIT_AGENT_BINARY_PATH,
8aec53fb 57 POLKIT_AGENT_BINARY_PATH, "--notify-fd", notify_fd, "--fallback", NULL);
9bdc770c
LP
58
59 /* Close the writing side, because that's the one for the agent */
03e334a1 60 safe_close(pipe_fd[1]);
9bdc770c 61
6bb92a16 62 if (r < 0)
da927ba9 63 log_error_errno(r, "Failed to fork TTY ask password agent: %m");
9bdc770c
LP
64 else
65 /* Wait until the agent closes the fd */
3a43da28 66 fd_wait_for_event(pipe_fd[0], POLLHUP, USEC_INFINITY);
9bdc770c 67
03e334a1 68 safe_close(pipe_fd[0]);
6bb92a16
LP
69
70 return r;
71}
72
73void polkit_agent_close(void) {
74
75 if (agent_pid <= 0)
76 return;
77
78 /* Inform agent that we are done */
79 kill(agent_pid, SIGTERM);
80 kill(agent_pid, SIGCONT);
c73d180d 81 (void) wait_for_terminate(agent_pid, NULL);
6bb92a16
LP
82 agent_pid = 0;
83}
46ba8aae
LP
84
85#else
86
87int polkit_agent_open(void) {
88 return 0;
89}
90
91void polkit_agent_close(void) {
92}
93
94#endif