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"
17 #include "main-func.h"
19 #include "pretty-print.h"
20 #include "process-util.h"
21 #include "signal-util.h"
23 #include "user-util.h"
26 static const char* arg_what
= "idle:sleep:shutdown";
27 static const char* arg_who
= NULL
;
28 static const char* arg_why
= "Unknown reason";
29 static const char* arg_mode
= NULL
;
30 static PagerFlags arg_pager_flags
= 0;
31 static bool arg_legend
= true;
36 } arg_action
= ACTION_INHIBIT
;
38 static int inhibit(sd_bus
*bus
, sd_bus_error
*error
) {
39 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
43 r
= sd_bus_call_method(
45 "org.freedesktop.login1",
46 "/org/freedesktop/login1",
47 "org.freedesktop.login1.Manager",
51 "ssss", arg_what
, arg_who
, arg_why
, arg_mode
);
55 r
= sd_bus_message_read_basic(reply
, SD_BUS_TYPE_UNIX_FD
, &fd
);
59 r
= fcntl(fd
, F_DUPFD_CLOEXEC
, 3);
66 static int print_inhibitors(sd_bus
*bus
) {
67 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
68 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
69 _cleanup_(table_unrefp
) Table
*table
= NULL
;
72 (void) pager_open(arg_pager_flags
);
74 r
= sd_bus_call_method(
76 "org.freedesktop.login1",
77 "/org/freedesktop/login1",
78 "org.freedesktop.login1.Manager",
84 return log_error_errno(r
, "Could not get active inhibitors: %s", bus_error_message(&error
, r
));
86 table
= table_new("who", "uid", "user", "pid", "comm", "what", "why", "mode");
90 /* If there's not enough space, shorten the "WHY" column, as it's little more than an explaining comment. */
91 (void) table_set_weight(table
, TABLE_HEADER_CELL(6), 20);
93 r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_ARRAY
, "(ssssuu)");
95 return bus_log_parse_error(r
);
98 _cleanup_free_
char *comm
= NULL
, *u
= NULL
;
99 const char *what
, *who
, *why
, *mode
;
102 r
= sd_bus_message_read(reply
, "(ssssuu)", &what
, &who
, &why
, &mode
, &uid
, &pid
);
104 return bus_log_parse_error(r
);
108 if (arg_mode
&& !streq(mode
, arg_mode
))
111 (void) get_process_comm(pid
, &comm
);
112 u
= uid_to_name(uid
);
114 r
= table_add_many(table
,
117 TABLE_STRING
, strna(u
),
119 TABLE_STRING
, strna(comm
),
124 return log_error_errno(r
, "Failed to add table row: %m");
127 r
= sd_bus_message_exit_container(reply
);
129 return bus_log_parse_error(r
);
131 if (table_get_rows(table
) > 1) {
132 r
= table_set_sort(table
, (size_t) 1, (size_t) 0, (size_t) 5, (size_t) 6, (size_t) -1);
134 return log_error_errno(r
, "Failed to sort table: %m");
136 table_set_header(table
, arg_legend
);
138 r
= table_print(table
, NULL
);
140 return log_error_errno(r
, "Failed to show table: %m");
144 if (table_get_rows(table
) > 1)
145 printf("\n%zu inhibitors listed.\n", table_get_rows(table
) - 1);
147 printf("No inhibitors.\n");
153 static int help(void) {
154 _cleanup_free_
char *link
= NULL
;
157 r
= terminal_urlify_man("systemd-inhibit", "1", &link
);
161 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
162 "Execute a process while inhibiting shutdown/sleep/idle.\n\n"
163 " -h --help Show this help\n"
164 " --version Show package version\n"
165 " --no-pager Do not pipe output into a pager\n"
166 " --no-legend Do not show the headers and footers\n"
167 " --what=WHAT Operations to inhibit, colon separated list of:\n"
168 " shutdown, sleep, idle, handle-power-key,\n"
169 " handle-suspend-key, handle-hibernate-key,\n"
170 " handle-lid-switch\n"
171 " --who=STRING A descriptive string who is inhibiting\n"
172 " --why=STRING A descriptive string why is being inhibited\n"
173 " --mode=MODE One of block or delay\n"
174 " --list List active inhibitors\n"
175 "\nSee the %s for details.\n"
176 , program_invocation_short_name
183 static int parse_argv(int argc
, char *argv
[]) {
196 static const struct option options
[] = {
197 { "help", no_argument
, NULL
, 'h' },
198 { "version", no_argument
, NULL
, ARG_VERSION
},
199 { "what", required_argument
, NULL
, ARG_WHAT
},
200 { "who", required_argument
, NULL
, ARG_WHO
},
201 { "why", required_argument
, NULL
, ARG_WHY
},
202 { "mode", required_argument
, NULL
, ARG_MODE
},
203 { "list", no_argument
, NULL
, ARG_LIST
},
204 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
205 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
214 while ((c
= getopt_long(argc
, argv
, "+h", options
, NULL
)) >= 0)
241 arg_action
= ACTION_LIST
;
245 arg_pager_flags
|= PAGER_DISABLE
;
256 assert_not_reached("Unhandled option");
259 if (arg_action
== ACTION_INHIBIT
&& optind
== argc
)
260 arg_action
= ACTION_LIST
;
262 else if (arg_action
== ACTION_INHIBIT
&& optind
>= argc
)
263 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
264 "Missing command line to execute.");
269 static int run(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
);
280 r
= sd_bus_default_system(&bus
);
282 return log_error_errno(r
, "Failed to connect to bus: %m");
284 if (arg_action
== ACTION_LIST
)
285 return print_inhibitors(bus
);
288 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
289 _cleanup_strv_free_
char **arguments
= NULL
;
290 _cleanup_free_
char *w
= NULL
;
291 _cleanup_close_
int fd
= -1;
294 /* Ignore SIGINT and allow the forked process to receive it */
295 (void) ignore_signals(SIGINT
, -1);
298 arg_who
= w
= strv_join(argv
+ optind
, " ");
303 fd
= inhibit(bus
, &error
);
305 return log_error_errno(fd
, "Failed to inhibit: %s", bus_error_message(&error
, fd
));
307 arguments
= strv_copy(argv
+ optind
);
311 r
= safe_fork("(inhibit)", FORK_RESET_SIGNALS
|FORK_DEATHSIG
|FORK_CLOSE_ALL_FDS
|FORK_RLIMIT_NOFILE_SAFE
|FORK_LOG
, &pid
);
316 execvp(arguments
[0], arguments
);
318 log_error_errno(errno
, "Failed to execute %s: %m", argv
[optind
]);
322 return wait_for_terminate_and_check(argv
[optind
], pid
, WAIT_LOG
);
326 DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run
);