1 /* SPDX-License-Identifier: LGPL-2.1+ */
11 #include "alloc-util.h"
12 #include "bus-error.h"
15 #include "format-table.h"
16 #include "format-util.h"
18 #include "process-util.h"
19 #include "signal-util.h"
21 #include "terminal-util.h"
22 #include "user-util.h"
25 static const char* arg_what
= "idle:sleep:shutdown";
26 static const char* arg_who
= NULL
;
27 static const char* arg_why
= "Unknown reason";
28 static const char* arg_mode
= NULL
;
29 static PagerFlags arg_pager_flags
= 0;
30 static bool arg_legend
= true;
35 } arg_action
= ACTION_INHIBIT
;
37 static int inhibit(sd_bus
*bus
, sd_bus_error
*error
) {
38 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
42 r
= sd_bus_call_method(
44 "org.freedesktop.login1",
45 "/org/freedesktop/login1",
46 "org.freedesktop.login1.Manager",
50 "ssss", arg_what
, arg_who
, arg_why
, arg_mode
);
54 r
= sd_bus_message_read_basic(reply
, SD_BUS_TYPE_UNIX_FD
, &fd
);
58 r
= fcntl(fd
, F_DUPFD_CLOEXEC
, 3);
65 static int print_inhibitors(sd_bus
*bus
) {
66 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
67 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
68 _cleanup_(table_unrefp
) Table
*table
= NULL
;
71 (void) pager_open(arg_pager_flags
);
73 r
= sd_bus_call_method(
75 "org.freedesktop.login1",
76 "/org/freedesktop/login1",
77 "org.freedesktop.login1.Manager",
83 return log_error_errno(r
, "Could not get active inhibitors: %s", bus_error_message(&error
, r
));
85 table
= table_new("WHO", "UID", "USER", "PID", "COMM", "WHAT", "WHY", "MODE");
89 /* If there's not enough space, shorten the "WHY" column, as it's little more than an explaining comment. */
90 (void) table_set_weight(table
, TABLE_HEADER_CELL(6), 20);
92 r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_ARRAY
, "(ssssuu)");
94 return bus_log_parse_error(r
);
97 _cleanup_free_
char *comm
= NULL
, *u
= NULL
;
98 const char *what
, *who
, *why
, *mode
;
101 r
= sd_bus_message_read(reply
, "(ssssuu)", &what
, &who
, &why
, &mode
, &uid
, &pid
);
103 return bus_log_parse_error(r
);
107 if (arg_mode
&& !streq(mode
, arg_mode
))
110 (void) get_process_comm(pid
, &comm
);
111 u
= uid_to_name(uid
);
113 r
= table_add_many(table
,
116 TABLE_STRING
, strna(u
),
118 TABLE_STRING
, strna(comm
),
123 return log_error_errno(r
, "Failed to add table row: %m");
126 r
= sd_bus_message_exit_container(reply
);
128 return bus_log_parse_error(r
);
130 if (table_get_rows(table
) > 1) {
131 r
= table_set_sort(table
, (size_t) 1, (size_t) 0, (size_t) 5, (size_t) 6, (size_t) -1);
133 return log_error_errno(r
, "Failed to sort table: %m");
135 table_set_header(table
, arg_legend
);
137 r
= table_print(table
, NULL
);
139 return log_error_errno(r
, "Failed to show table: %m");
143 if (table_get_rows(table
) > 1)
144 printf("\n%zu inhibitors listed.\n", table_get_rows(table
) - 1);
146 printf("No inhibitors.\n");
152 static int help(void) {
153 _cleanup_free_
char *link
= NULL
;
156 r
= terminal_urlify_man("systemd-inhibit", "1", &link
);
160 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
161 "Execute a process while inhibiting shutdown/sleep/idle.\n\n"
162 " -h --help Show this help\n"
163 " --version Show package version\n"
164 " --no-pager Do not pipe output into a pager\n"
165 " --no-legend Do not show the headers and footers\n"
166 " --what=WHAT Operations to inhibit, colon separated list of:\n"
167 " shutdown, sleep, idle, handle-power-key,\n"
168 " handle-suspend-key, handle-hibernate-key,\n"
169 " handle-lid-switch\n"
170 " --who=STRING A descriptive string who is inhibiting\n"
171 " --why=STRING A descriptive string why is being inhibited\n"
172 " --mode=MODE One of block or delay\n"
173 " --list List active inhibitors\n"
174 "\nSee the %s for details.\n"
175 , program_invocation_short_name
182 static int parse_argv(int argc
, char *argv
[]) {
195 static const struct option options
[] = {
196 { "help", no_argument
, NULL
, 'h' },
197 { "version", no_argument
, NULL
, ARG_VERSION
},
198 { "what", required_argument
, NULL
, ARG_WHAT
},
199 { "who", required_argument
, NULL
, ARG_WHO
},
200 { "why", required_argument
, NULL
, ARG_WHY
},
201 { "mode", required_argument
, NULL
, ARG_MODE
},
202 { "list", no_argument
, NULL
, ARG_LIST
},
203 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
204 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
213 while ((c
= getopt_long(argc
, argv
, "+h", options
, NULL
)) >= 0)
240 arg_action
= ACTION_LIST
;
244 arg_pager_flags
|= PAGER_DISABLE
;
255 assert_not_reached("Unhandled option");
258 if (arg_action
== ACTION_INHIBIT
&& optind
== argc
)
259 arg_action
= ACTION_LIST
;
261 else if (arg_action
== ACTION_INHIBIT
&& optind
>= argc
) {
262 log_error("Missing command line to execute.");
269 int main(int argc
, char *argv
[]) {
270 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
273 log_parse_environment();
276 r
= parse_argv(argc
, argv
);
282 r
= sd_bus_default_system(&bus
);
284 log_error_errno(r
, "Failed to connect to bus: %m");
288 if (arg_action
== ACTION_LIST
) {
290 r
= print_inhibitors(bus
);
296 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
297 _cleanup_close_
int fd
= -1;
298 _cleanup_free_
char *w
= NULL
;
301 /* Ignore SIGINT and allow the forked process to receive it */
302 (void) ignore_signals(SIGINT
, -1);
305 arg_who
= w
= strv_join(argv
+ optind
, " ");
310 fd
= inhibit(bus
, &error
);
312 log_error("Failed to inhibit: %s", bus_error_message(&error
, fd
));
316 r
= safe_fork("(inhibit)", FORK_RESET_SIGNALS
|FORK_DEATHSIG
|FORK_CLOSE_ALL_FDS
|FORK_LOG
, &pid
);
321 execvp(argv
[optind
], argv
+ optind
);
323 log_error_errno(errno
, "Failed to execute %s: %m", argv
[optind
]);
327 r
= wait_for_terminate_and_check(argv
[optind
], pid
, WAIT_LOG
);
328 return r
< 0 ? EXIT_FAILURE
: r
;