]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/login/inhibit.c
61fbd5df148bb373368748de256f2ab6208c9af7
[thirdparty/systemd.git] / src / login / inhibit.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <fcntl.h>
4 #include <getopt.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8
9 #include "sd-bus.h"
10
11 #include "alloc-util.h"
12 #include "build.h"
13 #include "bus-error.h"
14 #include "bus-locator.h"
15 #include "bus-util.h"
16 #include "fd-util.h"
17 #include "format-table.h"
18 #include "format-util.h"
19 #include "main-func.h"
20 #include "pager.h"
21 #include "pretty-print.h"
22 #include "process-util.h"
23 #include "signal-util.h"
24 #include "strv.h"
25 #include "terminal-util.h"
26 #include "user-util.h"
27
28 static const char* arg_what = "idle:sleep:shutdown";
29 static const char* arg_who = NULL;
30 static const char* arg_why = "Unknown reason";
31 static const char* arg_mode = NULL;
32 static PagerFlags arg_pager_flags = 0;
33 static bool arg_legend = true;
34
35 static enum {
36 ACTION_INHIBIT,
37 ACTION_LIST
38 } arg_action = ACTION_INHIBIT;
39
40 static int inhibit(sd_bus *bus, sd_bus_error *error) {
41 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
42 int r;
43 int fd;
44
45 r = bus_call_method(bus, bus_login_mgr, "Inhibit", error, &reply, "ssss", arg_what, arg_who, arg_why, arg_mode);
46 if (r < 0)
47 return r;
48
49 r = sd_bus_message_read_basic(reply, SD_BUS_TYPE_UNIX_FD, &fd);
50 if (r < 0)
51 return r;
52
53 return RET_NERRNO(fcntl(fd, F_DUPFD_CLOEXEC, 3));
54 }
55
56 static int print_inhibitors(sd_bus *bus) {
57 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
58 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
59 _cleanup_(table_unrefp) Table *table = NULL;
60 int r;
61
62 pager_open(arg_pager_flags);
63
64 r = bus_call_method(bus, bus_login_mgr, "ListInhibitors", &error, &reply, NULL);
65 if (r < 0)
66 return log_error_errno(r, "Could not get active inhibitors: %s", bus_error_message(&error, r));
67
68 table = table_new("who", "uid", "user", "pid", "comm", "what", "why", "mode");
69 if (!table)
70 return log_oom();
71
72 /* If there's not enough space, shorten the "WHY" column, as it's little more than an explaining comment. */
73 (void) table_set_weight(table, TABLE_HEADER_CELL(6), 20);
74 (void) table_set_maximum_width(table, TABLE_HEADER_CELL(0), columns()/2);
75
76 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssuu)");
77 if (r < 0)
78 return bus_log_parse_error(r);
79
80 for (;;) {
81 _cleanup_free_ char *comm = NULL, *u = NULL;
82 const char *what, *who, *why, *mode;
83 uint32_t uid, pid;
84
85 r = sd_bus_message_read(reply, "(ssssuu)", &what, &who, &why, &mode, &uid, &pid);
86 if (r < 0)
87 return bus_log_parse_error(r);
88 if (r == 0)
89 break;
90
91 if (arg_mode && !streq(mode, arg_mode))
92 continue;
93
94 (void) pid_get_comm(pid, &comm);
95 u = uid_to_name(uid);
96
97 r = table_add_many(table,
98 TABLE_STRING, who,
99 TABLE_UID, (uid_t) uid,
100 TABLE_STRING, strna(u),
101 TABLE_PID, (pid_t) pid,
102 TABLE_STRING, strna(comm),
103 TABLE_STRING, what,
104 TABLE_STRING, why,
105 TABLE_STRING, mode);
106 if (r < 0)
107 return table_log_add_error(r);
108 }
109
110 r = sd_bus_message_exit_container(reply);
111 if (r < 0)
112 return bus_log_parse_error(r);
113
114 if (table_get_rows(table) > 1) {
115 r = table_set_sort(table, (size_t) 1, (size_t) 0, (size_t) 5, (size_t) 6);
116 if (r < 0)
117 return table_log_sort_error(r);
118
119 table_set_header(table, arg_legend);
120
121 r = table_print(table, NULL);
122 if (r < 0)
123 return table_log_print_error(r);
124 }
125
126 if (arg_legend) {
127 if (table_get_rows(table) > 1)
128 printf("\n%zu inhibitors listed.\n", table_get_rows(table) - 1);
129 else
130 printf("No inhibitors.\n");
131 }
132
133 return 0;
134 }
135
136 static int help(void) {
137 _cleanup_free_ char *link = NULL;
138 int r;
139
140 r = terminal_urlify_man("systemd-inhibit", "1", &link);
141 if (r < 0)
142 return log_oom();
143
144 printf("%s [OPTIONS...] COMMAND ...\n"
145 "\n%sExecute a process while inhibiting shutdown/sleep/idle.%s\n\n"
146 " -h --help Show this help\n"
147 " --version Show package version\n"
148 " --no-pager Do not pipe output into a pager\n"
149 " --no-legend Do not show the headers and footers\n"
150 " --what=WHAT Operations to inhibit, colon separated list of:\n"
151 " shutdown, sleep, idle, handle-power-key,\n"
152 " handle-suspend-key, handle-hibernate-key,\n"
153 " handle-lid-switch\n"
154 " --who=STRING A descriptive string who is inhibiting\n"
155 " --why=STRING A descriptive string why is being inhibited\n"
156 " --mode=MODE One of block or delay\n"
157 " --list List active inhibitors\n"
158 "\nSee the %s for details.\n",
159 program_invocation_short_name,
160 ansi_highlight(),
161 ansi_normal(),
162 link);
163
164 return 0;
165 }
166
167 static int parse_argv(int argc, char *argv[]) {
168
169 enum {
170 ARG_VERSION = 0x100,
171 ARG_WHAT,
172 ARG_WHO,
173 ARG_WHY,
174 ARG_MODE,
175 ARG_LIST,
176 ARG_NO_PAGER,
177 ARG_NO_LEGEND,
178 };
179
180 static const struct option options[] = {
181 { "help", no_argument, NULL, 'h' },
182 { "version", no_argument, NULL, ARG_VERSION },
183 { "what", required_argument, NULL, ARG_WHAT },
184 { "who", required_argument, NULL, ARG_WHO },
185 { "why", required_argument, NULL, ARG_WHY },
186 { "mode", required_argument, NULL, ARG_MODE },
187 { "list", no_argument, NULL, ARG_LIST },
188 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
189 { "no-legend", no_argument, NULL, ARG_NO_LEGEND },
190 {}
191 };
192
193 int c;
194
195 assert(argc >= 0);
196 assert(argv);
197
198 /* Resetting to 0 forces the invocation of an internal initialization routine of getopt_long()
199 * that checks for GNU extensions in optstring ('-' or '+' at the beginning). */
200 optind = 0;
201 while ((c = getopt_long(argc, argv, "+h", options, NULL)) >= 0)
202
203 switch (c) {
204
205 case 'h':
206 return help();
207
208 case ARG_VERSION:
209 return version();
210
211 case ARG_WHAT:
212 arg_what = optarg;
213 break;
214
215 case ARG_WHO:
216 arg_who = optarg;
217 break;
218
219 case ARG_WHY:
220 arg_why = optarg;
221 break;
222
223 case ARG_MODE:
224 arg_mode = optarg;
225 break;
226
227 case ARG_LIST:
228 arg_action = ACTION_LIST;
229 break;
230
231 case ARG_NO_PAGER:
232 arg_pager_flags |= PAGER_DISABLE;
233 break;
234
235 case ARG_NO_LEGEND:
236 arg_legend = false;
237 break;
238
239 case '?':
240 return -EINVAL;
241
242 default:
243 assert_not_reached();
244 }
245
246 if (arg_action == ACTION_INHIBIT && optind == argc)
247 arg_action = ACTION_LIST;
248
249 else if (arg_action == ACTION_INHIBIT && optind >= argc)
250 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
251 "Missing command line to execute.");
252
253 return 1;
254 }
255
256 static int run(int argc, char *argv[]) {
257 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
258 int r;
259
260 log_show_color(true);
261 log_parse_environment();
262 log_open();
263
264 r = parse_argv(argc, argv);
265 if (r <= 0)
266 return r;
267
268 r = sd_bus_default_system(&bus);
269 if (r < 0)
270 return bus_log_connect_error(r, BUS_TRANSPORT_LOCAL);
271
272 if (arg_action == ACTION_LIST)
273 return print_inhibitors(bus);
274 else {
275 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
276 _cleanup_strv_free_ char **arguments = NULL;
277 _cleanup_free_ char *w = NULL;
278 _cleanup_close_ int fd = -EBADF;
279 pid_t pid;
280
281 /* Ignore SIGINT and allow the forked process to receive it */
282 (void) ignore_signals(SIGINT);
283
284 if (!arg_who) {
285 w = strv_join(argv + optind, " ");
286 if (!w)
287 return log_oom();
288
289 arg_who = w;
290 }
291
292 if (!arg_mode)
293 arg_mode = "block";
294
295 fd = inhibit(bus, &error);
296 if (fd < 0)
297 return log_error_errno(fd, "Failed to inhibit: %s", bus_error_message(&error, fd));
298
299 arguments = strv_copy(argv + optind);
300 if (!arguments)
301 return log_oom();
302
303 r = safe_fork("(inhibit)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_CLOSE_ALL_FDS|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG, &pid);
304 if (r < 0)
305 return r;
306 if (r == 0) {
307 /* Child */
308 execvp(arguments[0], arguments);
309 log_open();
310 log_error_errno(errno, "Failed to execute %s: %m", argv[optind]);
311 _exit(EXIT_FAILURE);
312 }
313
314 return wait_for_terminate_and_check(argv[optind], pid, WAIT_LOG);
315 }
316 }
317
318 DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);