1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
11 #include "alloc-util.h"
13 #include "bus-error.h"
14 #include "bus-locator.h"
17 #include "format-table.h"
18 #include "format-util.h"
19 #include "main-func.h"
21 #include "pretty-print.h"
22 #include "process-util.h"
23 #include "signal-util.h"
25 #include "terminal-util.h"
26 #include "user-util.h"
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;
38 } arg_action
= ACTION_INHIBIT
;
40 static int inhibit(sd_bus
*bus
, sd_bus_error
*error
) {
41 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
45 r
= bus_call_method(bus
, bus_login_mgr
, "Inhibit", error
, &reply
, "ssss", arg_what
, arg_who
, arg_why
, arg_mode
);
49 r
= sd_bus_message_read_basic(reply
, SD_BUS_TYPE_UNIX_FD
, &fd
);
53 return RET_NERRNO(fcntl(fd
, F_DUPFD_CLOEXEC
, 3));
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
;
62 pager_open(arg_pager_flags
);
64 r
= bus_call_method(bus
, bus_login_mgr
, "ListInhibitors", &error
, &reply
, NULL
);
66 return log_error_errno(r
, "Could not get active inhibitors: %s", bus_error_message(&error
, r
));
68 table
= table_new("who", "uid", "user", "pid", "comm", "what", "why", "mode");
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);
76 r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_ARRAY
, "(ssssuu)");
78 return bus_log_parse_error(r
);
81 _cleanup_free_
char *comm
= NULL
, *u
= NULL
;
82 const char *what
, *who
, *why
, *mode
;
85 r
= sd_bus_message_read(reply
, "(ssssuu)", &what
, &who
, &why
, &mode
, &uid
, &pid
);
87 return bus_log_parse_error(r
);
91 if (arg_mode
&& !streq(mode
, arg_mode
))
94 (void) pid_get_comm(pid
, &comm
);
97 r
= table_add_many(table
,
99 TABLE_UID
, (uid_t
) uid
,
100 TABLE_STRING
, strna(u
),
101 TABLE_PID
, (pid_t
) pid
,
102 TABLE_STRING
, strna(comm
),
107 return table_log_add_error(r
);
110 r
= sd_bus_message_exit_container(reply
);
112 return bus_log_parse_error(r
);
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);
117 return table_log_sort_error(r
);
119 table_set_header(table
, arg_legend
);
121 r
= table_print(table
, NULL
);
123 return table_log_print_error(r
);
127 if (table_get_rows(table
) > 1)
128 printf("\n%zu inhibitors listed.\n", table_get_rows(table
) - 1);
130 printf("No inhibitors.\n");
136 static int help(void) {
137 _cleanup_free_
char *link
= NULL
;
140 r
= terminal_urlify_man("systemd-inhibit", "1", &link
);
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
,
167 static int parse_argv(int argc
, char *argv
[]) {
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
},
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). */
201 while ((c
= getopt_long(argc
, argv
, "+h", options
, NULL
)) >= 0)
228 arg_action
= ACTION_LIST
;
232 arg_pager_flags
|= PAGER_DISABLE
;
243 assert_not_reached();
246 if (arg_action
== ACTION_INHIBIT
&& optind
== argc
)
247 arg_action
= ACTION_LIST
;
249 else if (arg_action
== ACTION_INHIBIT
&& optind
>= argc
)
250 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
251 "Missing command line to execute.");
256 static int run(int argc
, char *argv
[]) {
257 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
260 log_show_color(true);
261 log_parse_environment();
264 r
= parse_argv(argc
, argv
);
268 r
= sd_bus_default_system(&bus
);
270 return bus_log_connect_error(r
, BUS_TRANSPORT_LOCAL
);
272 if (arg_action
== ACTION_LIST
)
273 return print_inhibitors(bus
);
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
;
281 /* Ignore SIGINT and allow the forked process to receive it */
282 (void) ignore_signals(SIGINT
);
285 w
= strv_join(argv
+ optind
, " ");
295 fd
= inhibit(bus
, &error
);
297 return log_error_errno(fd
, "Failed to inhibit: %s", bus_error_message(&error
, fd
));
299 arguments
= strv_copy(argv
+ optind
);
303 r
= safe_fork("(inhibit)", FORK_RESET_SIGNALS
|FORK_DEATHSIG
|FORK_CLOSE_ALL_FDS
|FORK_RLIMIT_NOFILE_SAFE
|FORK_LOG
, &pid
);
308 execvp(arguments
[0], arguments
);
310 log_error_errno(errno
, "Failed to execute %s: %m", argv
[optind
]);
314 return wait_for_terminate_and_check(argv
[optind
], pid
, WAIT_LOG
);
318 DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run
);