1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
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 "terminal-util.h"
24 #include "user-util.h"
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 static PagerFlags arg_pager_flags
= 0;
32 static bool arg_legend
= true;
37 } arg_action
= ACTION_INHIBIT
;
39 static int inhibit(sd_bus
*bus
, sd_bus_error
*error
) {
40 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
44 r
= sd_bus_call_method(
46 "org.freedesktop.login1",
47 "/org/freedesktop/login1",
48 "org.freedesktop.login1.Manager",
52 "ssss", arg_what
, arg_who
, arg_why
, arg_mode
);
56 r
= sd_bus_message_read_basic(reply
, SD_BUS_TYPE_UNIX_FD
, &fd
);
60 r
= fcntl(fd
, F_DUPFD_CLOEXEC
, 3);
67 static int print_inhibitors(sd_bus
*bus
) {
68 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
69 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
70 _cleanup_(table_unrefp
) Table
*table
= NULL
;
73 (void) pager_open(arg_pager_flags
);
75 r
= sd_bus_call_method(
77 "org.freedesktop.login1",
78 "/org/freedesktop/login1",
79 "org.freedesktop.login1.Manager",
85 return log_error_errno(r
, "Could not get active inhibitors: %s", bus_error_message(&error
, r
));
87 table
= table_new("who", "uid", "user", "pid", "comm", "what", "why", "mode");
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 (void) table_set_maximum_width(table
, TABLE_HEADER_CELL(0), columns()/2);
95 r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_ARRAY
, "(ssssuu)");
97 return bus_log_parse_error(r
);
100 _cleanup_free_
char *comm
= NULL
, *u
= NULL
;
101 const char *what
, *who
, *why
, *mode
;
104 r
= sd_bus_message_read(reply
, "(ssssuu)", &what
, &who
, &why
, &mode
, &uid
, &pid
);
106 return bus_log_parse_error(r
);
110 if (arg_mode
&& !streq(mode
, arg_mode
))
113 (void) get_process_comm(pid
, &comm
);
114 u
= uid_to_name(uid
);
116 r
= table_add_many(table
,
118 TABLE_UID
, (uid_t
) uid
,
119 TABLE_STRING
, strna(u
),
120 TABLE_PID
, (pid_t
) pid
,
121 TABLE_STRING
, strna(comm
),
126 return table_log_add_error(r
);
129 r
= sd_bus_message_exit_container(reply
);
131 return bus_log_parse_error(r
);
133 if (table_get_rows(table
) > 1) {
134 r
= table_set_sort(table
, (size_t) 1, (size_t) 0, (size_t) 5, (size_t) 6, (size_t) -1);
136 return table_log_sort_error(r
);
138 table_set_header(table
, arg_legend
);
140 r
= table_print(table
, NULL
);
142 return table_log_print_error(r
);
146 if (table_get_rows(table
) > 1)
147 printf("\n%zu inhibitors listed.\n", table_get_rows(table
) - 1);
149 printf("No inhibitors.\n");
155 static int help(void) {
156 _cleanup_free_
char *link
= NULL
;
159 r
= terminal_urlify_man("systemd-inhibit", "1", &link
);
163 printf("%s [OPTIONS...] COMMAND ...\n"
164 "\n%sExecute a process while inhibiting shutdown/sleep/idle.%s\n\n"
165 " -h --help Show this help\n"
166 " --version Show package version\n"
167 " --no-pager Do not pipe output into a pager\n"
168 " --no-legend Do not show the headers and footers\n"
169 " --what=WHAT Operations to inhibit, colon separated list of:\n"
170 " shutdown, sleep, idle, handle-power-key,\n"
171 " handle-suspend-key, handle-hibernate-key,\n"
172 " handle-lid-switch\n"
173 " --who=STRING A descriptive string who is inhibiting\n"
174 " --why=STRING A descriptive string why is being inhibited\n"
175 " --mode=MODE One of block or delay\n"
176 " --list List active inhibitors\n"
177 "\nSee the %s for details.\n",
178 program_invocation_short_name
,
186 static int parse_argv(int argc
, char *argv
[]) {
199 static const struct option options
[] = {
200 { "help", no_argument
, NULL
, 'h' },
201 { "version", no_argument
, NULL
, ARG_VERSION
},
202 { "what", required_argument
, NULL
, ARG_WHAT
},
203 { "who", required_argument
, NULL
, ARG_WHO
},
204 { "why", required_argument
, NULL
, ARG_WHY
},
205 { "mode", required_argument
, NULL
, ARG_MODE
},
206 { "list", no_argument
, NULL
, ARG_LIST
},
207 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
208 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
217 while ((c
= getopt_long(argc
, argv
, "+h", options
, NULL
)) >= 0)
244 arg_action
= ACTION_LIST
;
248 arg_pager_flags
|= PAGER_DISABLE
;
259 assert_not_reached("Unhandled option");
262 if (arg_action
== ACTION_INHIBIT
&& optind
== argc
)
263 arg_action
= ACTION_LIST
;
265 else if (arg_action
== ACTION_INHIBIT
&& optind
>= argc
)
266 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
267 "Missing command line to execute.");
272 static int run(int argc
, char *argv
[]) {
273 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
276 log_show_color(true);
277 log_parse_environment();
280 r
= parse_argv(argc
, argv
);
284 r
= sd_bus_default_system(&bus
);
286 return bus_log_connect_error(r
);
288 if (arg_action
== ACTION_LIST
)
289 return print_inhibitors(bus
);
291 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
292 _cleanup_strv_free_
char **arguments
= NULL
;
293 _cleanup_free_
char *w
= NULL
;
294 _cleanup_close_
int fd
= -1;
297 /* Ignore SIGINT and allow the forked process to receive it */
298 (void) ignore_signals(SIGINT
, -1);
301 w
= strv_join(argv
+ optind
, " ");
311 fd
= inhibit(bus
, &error
);
313 return log_error_errno(fd
, "Failed to inhibit: %s", bus_error_message(&error
, fd
));
315 arguments
= strv_copy(argv
+ optind
);
319 r
= safe_fork("(inhibit)", FORK_RESET_SIGNALS
|FORK_DEATHSIG
|FORK_CLOSE_ALL_FDS
|FORK_RLIMIT_NOFILE_SAFE
|FORK_LOG
, &pid
);
324 execvp(arguments
[0], arguments
);
326 log_error_errno(errno
, "Failed to execute %s: %m", argv
[optind
]);
330 return wait_for_terminate_and_check(argv
[optind
], pid
, WAIT_LOG
);
334 DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run
);