1 /* SPDX-License-Identifier: LGPL-2.1+ */
11 #include "alloc-util.h"
12 #include "bus-error.h"
15 #include "format-util.h"
17 #include "process-util.h"
18 #include "signal-util.h"
20 #include "user-util.h"
23 static const char* arg_what
= "idle:sleep:shutdown";
24 static const char* arg_who
= NULL
;
25 static const char* arg_why
= "Unknown reason";
26 static const char* arg_mode
= NULL
;
27 static bool arg_no_pager
= false;
32 } arg_action
= ACTION_INHIBIT
;
34 static int inhibit(sd_bus
*bus
, sd_bus_error
*error
) {
35 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
39 r
= sd_bus_call_method(
41 "org.freedesktop.login1",
42 "/org/freedesktop/login1",
43 "org.freedesktop.login1.Manager",
47 "ssss", arg_what
, arg_who
, arg_why
, arg_mode
);
51 r
= sd_bus_message_read_basic(reply
, SD_BUS_TYPE_UNIX_FD
, &fd
);
55 r
= fcntl(fd
, F_DUPFD_CLOEXEC
, 3);
62 static int print_inhibitors(sd_bus
*bus
, sd_bus_error
*error
) {
63 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
64 const char *what
, *who
, *why
, *mode
;
65 unsigned int uid
, pid
;
69 (void) pager_open(arg_no_pager
, false);
71 r
= sd_bus_call_method(
73 "org.freedesktop.login1",
74 "/org/freedesktop/login1",
75 "org.freedesktop.login1.Manager",
83 r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_ARRAY
, "(ssssuu)");
85 return bus_log_parse_error(r
);
87 while ((r
= sd_bus_message_read(reply
, "(ssssuu)", &what
, &who
, &why
, &mode
, &uid
, &pid
)) > 0) {
88 _cleanup_free_
char *comm
= NULL
, *u
= NULL
;
90 if (arg_mode
&& !streq(mode
, arg_mode
))
93 get_process_comm(pid
, &comm
);
96 printf(" Who: %s (UID "UID_FMT
"/%s, PID "PID_FMT
"/%s)\n"
100 who
, uid
, strna(u
), pid
, strna(comm
),
108 return bus_log_parse_error(r
);
110 r
= sd_bus_message_exit_container(reply
);
112 return bus_log_parse_error(r
);
114 printf("%u inhibitors listed.\n", n
);
118 static void help(void) {
119 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
120 "Execute a process while inhibiting shutdown/sleep/idle.\n\n"
121 " -h --help Show this help\n"
122 " --version Show package version\n"
123 " --no-pager Do not pipe output into a pager\n"
124 " --what=WHAT Operations to inhibit, colon separated list of:\n"
125 " shutdown, sleep, idle, handle-power-key,\n"
126 " handle-suspend-key, handle-hibernate-key,\n"
127 " handle-lid-switch\n"
128 " --who=STRING A descriptive string who is inhibiting\n"
129 " --why=STRING A descriptive string why is being inhibited\n"
130 " --mode=MODE One of block or delay\n"
131 " --list List active inhibitors\n"
132 , program_invocation_short_name
);
135 static int parse_argv(int argc
, char *argv
[]) {
147 static const struct option options
[] = {
148 { "help", no_argument
, NULL
, 'h' },
149 { "version", no_argument
, NULL
, ARG_VERSION
},
150 { "what", required_argument
, NULL
, ARG_WHAT
},
151 { "who", required_argument
, NULL
, ARG_WHO
},
152 { "why", required_argument
, NULL
, ARG_WHY
},
153 { "mode", required_argument
, NULL
, ARG_MODE
},
154 { "list", no_argument
, NULL
, ARG_LIST
},
155 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
164 while ((c
= getopt_long(argc
, argv
, "+h", options
, NULL
)) >= 0)
192 arg_action
= ACTION_LIST
;
203 assert_not_reached("Unhandled option");
206 if (arg_action
== ACTION_INHIBIT
&& optind
== argc
)
207 arg_action
= ACTION_LIST
;
209 else if (arg_action
== ACTION_INHIBIT
&& optind
>= argc
) {
210 log_error("Missing command line to execute.");
217 int main(int argc
, char *argv
[]) {
218 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
219 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
222 log_parse_environment();
225 r
= parse_argv(argc
, argv
);
231 r
= sd_bus_default_system(&bus
);
233 log_error_errno(r
, "Failed to connect to bus: %m");
237 if (arg_action
== ACTION_LIST
) {
239 r
= print_inhibitors(bus
, &error
);
242 log_error("Failed to list inhibitors: %s", bus_error_message(&error
, -r
));
247 _cleanup_close_
int fd
= -1;
248 _cleanup_free_
char *w
= NULL
;
251 /* Ignore SIGINT and allow the forked process to receive it */
252 (void) ignore_signals(SIGINT
, -1);
255 arg_who
= w
= strv_join(argv
+ optind
, " ");
260 fd
= inhibit(bus
, &error
);
262 log_error("Failed to inhibit: %s", bus_error_message(&error
, fd
));
266 r
= safe_fork("(inhibit)", FORK_RESET_SIGNALS
|FORK_DEATHSIG
|FORK_CLOSE_ALL_FDS
|FORK_LOG
, &pid
);
271 execvp(argv
[optind
], argv
+ optind
);
273 log_error_errno(errno
, "Failed to execute %s: %m", argv
[optind
]);
277 r
= wait_for_terminate_and_check(argv
[optind
], pid
, WAIT_LOG
);
278 return r
< 0 ? EXIT_FAILURE
: r
;