1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2012 Lennart Poettering
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
29 #include "alloc-util.h"
30 #include "bus-error.h"
33 #include "format-util.h"
34 #include "process-util.h"
35 #include "signal-util.h"
37 #include "user-util.h"
40 static const char* arg_what
= "idle:sleep:shutdown";
41 static const char* arg_who
= NULL
;
42 static const char* arg_why
= "Unknown reason";
43 static const char* arg_mode
= NULL
;
48 } arg_action
= ACTION_INHIBIT
;
50 static int inhibit(sd_bus
*bus
, sd_bus_error
*error
) {
51 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
55 r
= sd_bus_call_method(
57 "org.freedesktop.login1",
58 "/org/freedesktop/login1",
59 "org.freedesktop.login1.Manager",
63 "ssss", arg_what
, arg_who
, arg_why
, arg_mode
);
67 r
= sd_bus_message_read_basic(reply
, SD_BUS_TYPE_UNIX_FD
, &fd
);
71 r
= fcntl(fd
, F_DUPFD_CLOEXEC
, 3);
78 static int print_inhibitors(sd_bus
*bus
, sd_bus_error
*error
) {
79 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
80 const char *what
, *who
, *why
, *mode
;
81 unsigned int uid
, pid
;
85 r
= sd_bus_call_method(
87 "org.freedesktop.login1",
88 "/org/freedesktop/login1",
89 "org.freedesktop.login1.Manager",
97 r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_ARRAY
, "(ssssuu)");
99 return bus_log_parse_error(r
);
101 while ((r
= sd_bus_message_read(reply
, "(ssssuu)", &what
, &who
, &why
, &mode
, &uid
, &pid
)) > 0) {
102 _cleanup_free_
char *comm
= NULL
, *u
= NULL
;
104 if (arg_mode
&& !streq(mode
, arg_mode
))
107 get_process_comm(pid
, &comm
);
108 u
= uid_to_name(uid
);
110 printf(" Who: %s (UID "UID_FMT
"/%s, PID "PID_FMT
"/%s)\n"
114 who
, uid
, strna(u
), pid
, strna(comm
),
122 return bus_log_parse_error(r
);
124 r
= sd_bus_message_exit_container(reply
);
126 return bus_log_parse_error(r
);
128 printf("%u inhibitors listed.\n", n
);
132 static void help(void) {
133 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
134 "Execute a process while inhibiting shutdown/sleep/idle.\n\n"
135 " -h --help Show this help\n"
136 " --version Show package version\n"
137 " --what=WHAT Operations to inhibit, colon separated list of:\n"
138 " shutdown, sleep, idle, handle-power-key,\n"
139 " handle-suspend-key, handle-hibernate-key,\n"
140 " handle-lid-switch\n"
141 " --who=STRING A descriptive string who is inhibiting\n"
142 " --why=STRING A descriptive string why is being inhibited\n"
143 " --mode=MODE One of block or delay\n"
144 " --list List active inhibitors\n"
145 , program_invocation_short_name
);
148 static int parse_argv(int argc
, char *argv
[]) {
159 static const struct option options
[] = {
160 { "help", no_argument
, NULL
, 'h' },
161 { "version", no_argument
, NULL
, ARG_VERSION
},
162 { "what", required_argument
, NULL
, ARG_WHAT
},
163 { "who", required_argument
, NULL
, ARG_WHO
},
164 { "why", required_argument
, NULL
, ARG_WHY
},
165 { "mode", required_argument
, NULL
, ARG_MODE
},
166 { "list", no_argument
, NULL
, ARG_LIST
},
175 while ((c
= getopt_long(argc
, argv
, "+h", options
, NULL
)) >= 0)
203 arg_action
= ACTION_LIST
;
210 assert_not_reached("Unhandled option");
213 if (arg_action
== ACTION_INHIBIT
&& optind
== argc
)
214 arg_action
= ACTION_LIST
;
216 else if (arg_action
== ACTION_INHIBIT
&& optind
>= argc
) {
217 log_error("Missing command line to execute.");
224 int main(int argc
, char *argv
[]) {
225 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
226 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
229 log_parse_environment();
232 r
= parse_argv(argc
, argv
);
238 r
= sd_bus_default_system(&bus
);
240 log_error_errno(r
, "Failed to connect to bus: %m");
244 if (arg_action
== ACTION_LIST
) {
246 r
= print_inhibitors(bus
, &error
);
248 log_error("Failed to list inhibitors: %s", bus_error_message(&error
, -r
));
253 _cleanup_close_
int fd
= -1;
254 _cleanup_free_
char *w
= NULL
;
258 arg_who
= w
= strv_join(argv
+ optind
, " ");
263 fd
= inhibit(bus
, &error
);
265 log_error("Failed to inhibit: %s", bus_error_message(&error
, fd
));
271 log_error_errno(errno
, "Failed to fork: %m");
278 (void) reset_all_signal_handlers();
279 (void) reset_signal_mask();
281 close_all_fds(NULL
, 0);
283 execvp(argv
[optind
], argv
+ optind
);
284 log_error_errno(errno
, "Failed to execute %s: %m", argv
[optind
]);
288 r
= wait_for_terminate_and_warn(argv
[optind
], pid
, true);
289 return r
< 0 ? EXIT_FAILURE
: r
;