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