chiark / gitweb /
core: move config_parse_set_status() into load-fragment.c
[elogind.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 <sys/types.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <string.h>
26 #include <sys/prctl.h>
27 #include <signal.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <sys/poll.h>
31
32 #include "log.h"
33 #include "util.h"
34 #include "spawn-polkit-agent.h"
35
36 #ifdef ENABLE_POLKIT
37 static pid_t agent_pid = 0;
38
39 int polkit_agent_open(void) {
40         int r;
41         int pipe_fd[2];
42         char notify_fd[10 + 1];
43
44         if (agent_pid > 0)
45                 return 0;
46
47         /* We check STDIN here, not STDOUT, since this is about input,
48          * not output */
49         if (!isatty(STDIN_FILENO))
50                 return 0;
51
52         if (pipe2(pipe_fd, 0) < 0)
53                 return -errno;
54
55         snprintf(notify_fd, sizeof(notify_fd), "%i", pipe_fd[1]);
56         char_array_0(notify_fd);
57
58         r = fork_agent(&agent_pid,
59                        &pipe_fd[1], 1,
60                        POLKIT_AGENT_BINARY_PATH,
61                        POLKIT_AGENT_BINARY_PATH, "--notify-fd", notify_fd, "--fallback", NULL);
62
63         /* Close the writing side, because that's the one for the agent */
64         close_nointr_nofail(pipe_fd[1]);
65
66         if (r < 0)
67                 log_error("Failed to fork TTY ask password agent: %s", strerror(-r));
68         else
69                 /* Wait until the agent closes the fd */
70                 fd_wait_for_event(pipe_fd[0], POLLHUP, (usec_t) -1);
71
72         close_nointr_nofail(pipe_fd[0]);
73
74         return r;
75 }
76
77 void polkit_agent_close(void) {
78
79         if (agent_pid <= 0)
80                 return;
81
82         /* Inform agent that we are done */
83         kill(agent_pid, SIGTERM);
84         kill(agent_pid, SIGCONT);
85         wait_for_terminate(agent_pid, NULL);
86         agent_pid = 0;
87 }
88
89 #else
90
91 int polkit_agent_open(void) {
92         return 0;
93 }
94
95 void polkit_agent_close(void) {
96 }
97
98 #endif