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