1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
10 #include "alloc-util.h"
12 #include "bus-error.h"
13 #include "bus-locator.h"
15 #include "errno-util.h"
17 #include "format-table.h"
19 #include "main-func.h"
21 #include "polkit-agent.h"
22 #include "pretty-print.h"
23 #include "process-util.h"
24 #include "runtime-scope.h"
25 #include "signal-util.h"
26 #include "string-util.h"
28 #include "terminal-util.h"
29 #include "user-util.h"
31 static const char *arg_what
= "idle:sleep:shutdown";
32 static const char *arg_who
= NULL
;
33 static const char *arg_why
= "Unknown reason";
34 static const char *arg_mode
= NULL
;
35 static bool arg_ask_password
= true;
36 static PagerFlags arg_pager_flags
= 0;
37 static bool arg_legend
= true;
42 } arg_action
= ACTION_INHIBIT
;
44 static int inhibit(sd_bus
*bus
, sd_bus_error
*error
) {
45 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
49 (void) polkit_agent_open_if_enabled(BUS_TRANSPORT_LOCAL
, arg_ask_password
);
51 r
= bus_call_method(bus
, bus_login_mgr
, "Inhibit", error
, &reply
, "ssss", arg_what
, arg_who
, arg_why
, arg_mode
);
55 r
= sd_bus_message_read_basic(reply
, SD_BUS_TYPE_UNIX_FD
, &fd
);
59 return RET_NERRNO(fcntl(fd
, F_DUPFD_CLOEXEC
, 3));
62 static int print_inhibitors(sd_bus
*bus
) {
63 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
64 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
65 _cleanup_(table_unrefp
) Table
*table
= NULL
;
68 pager_open(arg_pager_flags
);
70 r
= bus_call_method(bus
, bus_login_mgr
, "ListInhibitors", &error
, &reply
, NULL
);
72 return log_error_errno(r
, "Could not get active inhibitors: %s", bus_error_message(&error
, r
));
74 table
= table_new("who", "uid", "user", "pid", "comm", "what", "why", "mode");
78 /* If there's not enough space, shorten the "WHY" column, as it's little more than an explaining comment. */
79 (void) table_set_weight(table
, TABLE_HEADER_CELL(6), 20);
80 (void) table_set_maximum_width(table
, TABLE_HEADER_CELL(0), columns()/2);
82 r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_ARRAY
, "(ssssuu)");
84 return bus_log_parse_error(r
);
87 _cleanup_free_
char *comm
= NULL
, *u
= NULL
;
88 const char *what
, *who
, *why
, *mode
;
91 r
= sd_bus_message_read(reply
, "(ssssuu)", &what
, &who
, &why
, &mode
, &uid
, &pid
);
93 return bus_log_parse_error(r
);
97 if (arg_mode
&& !streq(mode
, arg_mode
))
100 (void) pid_get_comm(pid
, &comm
);
101 u
= uid_to_name(uid
);
103 r
= table_add_many(table
,
105 TABLE_UID
, (uid_t
) uid
,
106 TABLE_STRING
, strna(u
),
107 TABLE_PID
, (pid_t
) pid
,
108 TABLE_STRING
, strna(comm
),
113 return table_log_add_error(r
);
116 r
= sd_bus_message_exit_container(reply
);
118 return bus_log_parse_error(r
);
120 if (!table_isempty(table
)) {
121 r
= table_set_sort(table
, (size_t) 1, (size_t) 0, (size_t) 5, (size_t) 6);
123 return table_log_sort_error(r
);
125 table_set_header(table
, arg_legend
);
127 r
= table_print(table
, NULL
);
129 return table_log_print_error(r
);
133 if (table_isempty(table
))
134 printf("No inhibitors.\n");
136 printf("\n%zu inhibitors listed.\n", table_get_rows(table
) - 1);
142 static int help(void) {
143 _cleanup_free_
char *link
= NULL
;
146 r
= terminal_urlify_man("systemd-inhibit", "1", &link
);
150 printf("%s [OPTIONS...] COMMAND ...\n"
151 "\n%sExecute a process while inhibiting shutdown/sleep/idle.%s\n\n"
152 " -h --help Show this help\n"
153 " --version Show package version\n"
154 " --no-ask-password Do not attempt interactive authorization\n"
155 " --no-pager Do not pipe output into a pager\n"
156 " --no-legend Do not show the headers and footers\n"
157 " --what=WHAT Operations to inhibit, colon separated list of:\n"
158 " shutdown, sleep, idle, handle-power-key,\n"
159 " handle-suspend-key, handle-hibernate-key,\n"
160 " handle-lid-switch\n"
161 " --who=STRING A descriptive string who is inhibiting\n"
162 " --why=STRING A descriptive string why is being inhibited\n"
163 " --mode=MODE One of block, block-weak, or delay\n"
164 " --list List active inhibitors\n"
165 "\nSee the %s for details.\n",
166 program_invocation_short_name
,
174 static int parse_argv(int argc
, char *argv
[]) {
188 static const struct option options
[] = {
189 { "help", no_argument
, NULL
, 'h' },
190 { "version", no_argument
, NULL
, ARG_VERSION
},
191 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
192 { "what", required_argument
, NULL
, ARG_WHAT
},
193 { "who", required_argument
, NULL
, ARG_WHO
},
194 { "why", required_argument
, NULL
, ARG_WHY
},
195 { "mode", required_argument
, NULL
, ARG_MODE
},
196 { "list", no_argument
, NULL
, ARG_LIST
},
197 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
198 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
207 /* Resetting to 0 forces the invocation of an internal initialization routine of getopt_long()
208 * that checks for GNU extensions in optstring ('-' or '+' at the beginning). */
210 while ((c
= getopt_long(argc
, argv
, "+h", options
, NULL
)) >= 0)
237 arg_action
= ACTION_LIST
;
240 case ARG_NO_ASK_PASSWORD
:
241 arg_ask_password
= false;
245 arg_pager_flags
|= PAGER_DISABLE
;
256 assert_not_reached();
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
;
275 r
= parse_argv(argc
, argv
);
279 r
= sd_bus_default_system(&bus
);
281 return bus_log_connect_error(r
, BUS_TRANSPORT_LOCAL
, RUNTIME_SCOPE_SYSTEM
);
283 (void) sd_bus_set_allow_interactive_authorization(bus
, arg_ask_password
);
285 if (arg_action
== ACTION_LIST
)
286 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
= -EBADF
;
294 /* Ignore SIGINT and allow the forked process to receive it */
295 (void) ignore_signals(SIGINT
);
298 w
= strv_join(argv
+ optind
, " ");
308 fd
= inhibit(bus
, &error
);
310 return log_error_errno(fd
, "Failed to inhibit: %s", bus_error_message(&error
, fd
));
312 arguments
= strv_copy(argv
+ optind
);
316 r
= safe_fork("(inhibit)", FORK_RESET_SIGNALS
|FORK_DEATHSIG_SIGTERM
|FORK_CLOSE_ALL_FDS
|FORK_RLIMIT_NOFILE_SAFE
|FORK_LOG
, &pid
);
321 execvp(arguments
[0], arguments
);
323 log_error_errno(errno
, "Failed to execute %s: %m", argv
[optind
]);
327 return wait_for_terminate_and_check(argv
[optind
], pid
, WAIT_LOG
);
331 DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run
);