]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/login/inhibit.c
Merge pull request #2495 from heftig/master
[thirdparty/systemd.git] / src / login / inhibit.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2012 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <fcntl.h>
21 #include <getopt.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25
26 #include "sd-bus.h"
27
28 #include "alloc-util.h"
29 #include "bus-error.h"
30 #include "bus-util.h"
31 #include "fd-util.h"
32 #include "formats-util.h"
33 #include "process-util.h"
34 #include "signal-util.h"
35 #include "strv.h"
36 #include "user-util.h"
37 #include "util.h"
38
39 static const char* arg_what = "idle:sleep:shutdown";
40 static const char* arg_who = NULL;
41 static const char* arg_why = "Unknown reason";
42 static const char* arg_mode = NULL;
43
44 static enum {
45 ACTION_INHIBIT,
46 ACTION_LIST
47 } arg_action = ACTION_INHIBIT;
48
49 static int inhibit(sd_bus *bus, sd_bus_error *error) {
50 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
51 int r;
52 int fd;
53
54 r = sd_bus_call_method(
55 bus,
56 "org.freedesktop.login1",
57 "/org/freedesktop/login1",
58 "org.freedesktop.login1.Manager",
59 "Inhibit",
60 error,
61 &reply,
62 "ssss", arg_what, arg_who, arg_why, arg_mode);
63 if (r < 0)
64 return r;
65
66 r = sd_bus_message_read_basic(reply, SD_BUS_TYPE_UNIX_FD, &fd);
67 if (r < 0)
68 return r;
69
70 r = fcntl(fd, F_DUPFD_CLOEXEC, 3);
71 if (r < 0)
72 return -errno;
73
74 return r;
75 }
76
77 static int print_inhibitors(sd_bus *bus, sd_bus_error *error) {
78 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
79 const char *what, *who, *why, *mode;
80 unsigned int uid, pid;
81 unsigned n = 0;
82 int r;
83
84 r = sd_bus_call_method(
85 bus,
86 "org.freedesktop.login1",
87 "/org/freedesktop/login1",
88 "org.freedesktop.login1.Manager",
89 "ListInhibitors",
90 error,
91 &reply,
92 "");
93 if (r < 0)
94 return r;
95
96 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssuu)");
97 if (r < 0)
98 return bus_log_parse_error(r);
99
100 while ((r = sd_bus_message_read(reply, "(ssssuu)", &what, &who, &why, &mode, &uid, &pid)) > 0) {
101 _cleanup_free_ char *comm = NULL, *u = NULL;
102
103 if (arg_mode && !streq(mode, arg_mode))
104 continue;
105
106 get_process_comm(pid, &comm);
107 u = uid_to_name(uid);
108
109 printf(" Who: %s (UID "UID_FMT"/%s, PID "PID_FMT"/%s)\n"
110 " What: %s\n"
111 " Why: %s\n"
112 " Mode: %s\n\n",
113 who, uid, strna(u), pid, strna(comm),
114 what,
115 why,
116 mode);
117
118 n++;
119 }
120 if (r < 0)
121 return bus_log_parse_error(r);
122
123 r = sd_bus_message_exit_container(reply);
124 if (r < 0)
125 return bus_log_parse_error(r);
126
127 printf("%u inhibitors listed.\n", n);
128 return 0;
129 }
130
131 static void help(void) {
132 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
133 "Execute a process while inhibiting shutdown/sleep/idle.\n\n"
134 " -h --help Show this help\n"
135 " --version Show package version\n"
136 " --what=WHAT Operations to inhibit, colon separated list of:\n"
137 " shutdown, sleep, idle, handle-power-key,\n"
138 " handle-suspend-key, handle-hibernate-key,\n"
139 " handle-lid-switch\n"
140 " --who=STRING A descriptive string who is inhibiting\n"
141 " --why=STRING A descriptive string why is being inhibited\n"
142 " --mode=MODE One of block or delay\n"
143 " --list List active inhibitors\n"
144 , program_invocation_short_name);
145 }
146
147 static int parse_argv(int argc, char *argv[]) {
148
149 enum {
150 ARG_VERSION = 0x100,
151 ARG_WHAT,
152 ARG_WHO,
153 ARG_WHY,
154 ARG_MODE,
155 ARG_LIST,
156 };
157
158 static const struct option options[] = {
159 { "help", no_argument, NULL, 'h' },
160 { "version", no_argument, NULL, ARG_VERSION },
161 { "what", required_argument, NULL, ARG_WHAT },
162 { "who", required_argument, NULL, ARG_WHO },
163 { "why", required_argument, NULL, ARG_WHY },
164 { "mode", required_argument, NULL, ARG_MODE },
165 { "list", no_argument, NULL, ARG_LIST },
166 {}
167 };
168
169 int c;
170
171 assert(argc >= 0);
172 assert(argv);
173
174 while ((c = getopt_long(argc, argv, "+h", options, NULL)) >= 0)
175
176 switch (c) {
177
178 case 'h':
179 help();
180 return 0;
181
182 case ARG_VERSION:
183 return version();
184
185 case ARG_WHAT:
186 arg_what = optarg;
187 break;
188
189 case ARG_WHO:
190 arg_who = optarg;
191 break;
192
193 case ARG_WHY:
194 arg_why = optarg;
195 break;
196
197 case ARG_MODE:
198 arg_mode = optarg;
199 break;
200
201 case ARG_LIST:
202 arg_action = ACTION_LIST;
203 break;
204
205 case '?':
206 return -EINVAL;
207
208 default:
209 assert_not_reached("Unhandled option");
210 }
211
212 if (arg_action == ACTION_INHIBIT && optind == argc)
213 arg_action = ACTION_LIST;
214
215 else if (arg_action == ACTION_INHIBIT && optind >= argc) {
216 log_error("Missing command line to execute.");
217 return -EINVAL;
218 }
219
220 return 1;
221 }
222
223 int main(int argc, char *argv[]) {
224 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
225 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
226 int r;
227
228 log_parse_environment();
229 log_open();
230
231 r = parse_argv(argc, argv);
232 if (r < 0)
233 return EXIT_FAILURE;
234 if (r == 0)
235 return EXIT_SUCCESS;
236
237 r = sd_bus_default_system(&bus);
238 if (r < 0) {
239 log_error_errno(r, "Failed to connect to bus: %m");
240 return EXIT_FAILURE;
241 }
242
243 if (arg_action == ACTION_LIST) {
244
245 r = print_inhibitors(bus, &error);
246 if (r < 0) {
247 log_error("Failed to list inhibitors: %s", bus_error_message(&error, -r));
248 return EXIT_FAILURE;
249 }
250
251 } else {
252 _cleanup_close_ int fd = -1;
253 _cleanup_free_ char *w = NULL;
254 pid_t pid;
255
256 if (!arg_who)
257 arg_who = w = strv_join(argv + optind, " ");
258
259 if (!arg_mode)
260 arg_mode = "block";
261
262 fd = inhibit(bus, &error);
263 if (fd < 0) {
264 log_error("Failed to inhibit: %s", bus_error_message(&error, fd));
265 return EXIT_FAILURE;
266 }
267
268 pid = fork();
269 if (pid < 0) {
270 log_error_errno(errno, "Failed to fork: %m");
271 return EXIT_FAILURE;
272 }
273
274 if (pid == 0) {
275 /* Child */
276
277 (void) reset_all_signal_handlers();
278 (void) reset_signal_mask();
279
280 close_all_fds(NULL, 0);
281
282 execvp(argv[optind], argv + optind);
283 log_error_errno(errno, "Failed to execute %s: %m", argv[optind]);
284 _exit(EXIT_FAILURE);
285 }
286
287 r = wait_for_terminate_and_warn(argv[optind], pid, true);
288 return r < 0 ? EXIT_FAILURE : r;
289 }
290
291 return 0;
292 }