chiark / gitweb /
Prep v236 : Add missing SPDX-License-Identifier (5/9) src/login
[elogind.git] / src / login / inhibit.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3   This file is part of systemd.
4
5   Copyright 2012 Lennart Poettering
6
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.
11
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.
16
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/>.
19 ***/
20
21 #include <fcntl.h>
22 #include <getopt.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26
27 #include "sd-bus.h"
28
29 #include "alloc-util.h"
30 #include "bus-error.h"
31 #include "bus-util.h"
32 #include "fd-util.h"
33 #include "format-util.h"
34 #include "process-util.h"
35 #include "signal-util.h"
36 #include "strv.h"
37 #include "user-util.h"
38 #include "util.h"
39
40 /// Additional includes needed by elogind
41 #include "musl_missing.h"
42
43 static const char* arg_what = "idle:sleep:shutdown";
44 static const char* arg_who = NULL;
45 static const char* arg_why = "Unknown reason";
46 static const char* arg_mode = NULL;
47
48 static enum {
49         ACTION_INHIBIT,
50         ACTION_LIST
51 } arg_action = ACTION_INHIBIT;
52
53 static int inhibit(sd_bus *bus, sd_bus_error *error) {
54         _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
55         int r;
56         int fd;
57
58         r = sd_bus_call_method(
59                         bus,
60                         "org.freedesktop.login1",
61                         "/org/freedesktop/login1",
62                         "org.freedesktop.login1.Manager",
63                         "Inhibit",
64                         error,
65                         &reply,
66                         "ssss", arg_what, arg_who, arg_why, arg_mode);
67         if (r < 0)
68                 return r;
69
70         r = sd_bus_message_read_basic(reply, SD_BUS_TYPE_UNIX_FD, &fd);
71         if (r < 0)
72                 return r;
73
74         r = fcntl(fd, F_DUPFD_CLOEXEC, 3);
75         if (r < 0)
76                 return -errno;
77
78         return r;
79 }
80
81 static int print_inhibitors(sd_bus *bus, sd_bus_error *error) {
82         _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
83         const char *what, *who, *why, *mode;
84         unsigned int uid, pid;
85         unsigned n = 0;
86         int r;
87
88         r = sd_bus_call_method(
89                         bus,
90                         "org.freedesktop.login1",
91                         "/org/freedesktop/login1",
92                         "org.freedesktop.login1.Manager",
93                         "ListInhibitors",
94                         error,
95                         &reply,
96                         "");
97         if (r < 0)
98                 return r;
99
100         r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssuu)");
101         if (r < 0)
102                 return bus_log_parse_error(r);
103
104         while ((r = sd_bus_message_read(reply, "(ssssuu)", &what, &who, &why, &mode, &uid, &pid)) > 0) {
105                 _cleanup_free_ char *comm = NULL, *u = NULL;
106
107                 if (arg_mode && !streq(mode, arg_mode))
108                         continue;
109
110                 get_process_comm(pid, &comm);
111                 u = uid_to_name(uid);
112
113                 printf("     Who: %s (UID "UID_FMT"/%s, PID "PID_FMT"/%s)\n"
114                        "    What: %s\n"
115                        "     Why: %s\n"
116                        "    Mode: %s\n\n",
117                        who, uid, strna(u), pid, strna(comm),
118                        what,
119                        why,
120                        mode);
121
122                 n++;
123         }
124         if (r < 0)
125                 return bus_log_parse_error(r);
126
127         r = sd_bus_message_exit_container(reply);
128         if (r < 0)
129                 return bus_log_parse_error(r);
130
131         printf("%u inhibitors listed.\n", n);
132         return 0;
133 }
134
135 static void help(void) {
136         printf("%s [OPTIONS...] {COMMAND} ...\n\n"
137                "Execute a process while inhibiting shutdown/sleep/idle.\n\n"
138                "  -h --help               Show this help\n"
139                "     --version            Show package version\n"
140                "     --what=WHAT          Operations to inhibit, colon separated list of:\n"
141                "                          shutdown, sleep, idle, handle-power-key,\n"
142                "                          handle-suspend-key, handle-hibernate-key,\n"
143                "                          handle-lid-switch\n"
144                "     --who=STRING         A descriptive string who is inhibiting\n"
145                "     --why=STRING         A descriptive string why is being inhibited\n"
146                "     --mode=MODE          One of block or delay\n"
147                "     --list               List active inhibitors\n"
148                , program_invocation_short_name);
149 }
150
151 static int parse_argv(int argc, char *argv[]) {
152
153         enum {
154                 ARG_VERSION = 0x100,
155                 ARG_WHAT,
156                 ARG_WHO,
157                 ARG_WHY,
158                 ARG_MODE,
159                 ARG_LIST,
160         };
161
162         static const struct option options[] = {
163                 { "help",         no_argument,       NULL, 'h'              },
164                 { "version",      no_argument,       NULL, ARG_VERSION      },
165                 { "what",         required_argument, NULL, ARG_WHAT         },
166                 { "who",          required_argument, NULL, ARG_WHO          },
167                 { "why",          required_argument, NULL, ARG_WHY          },
168                 { "mode",         required_argument, NULL, ARG_MODE         },
169                 { "list",         no_argument,       NULL, ARG_LIST         },
170                 {}
171         };
172
173         int c;
174
175         assert(argc >= 0);
176         assert(argv);
177
178         while ((c = getopt_long(argc, argv, "+h", options, NULL)) >= 0)
179
180                 switch (c) {
181
182                 case 'h':
183                         help();
184                         return 0;
185
186                 case ARG_VERSION:
187                         return version();
188
189                 case ARG_WHAT:
190                         arg_what = optarg;
191                         break;
192
193                 case ARG_WHO:
194                         arg_who = optarg;
195                         break;
196
197                 case ARG_WHY:
198                         arg_why = optarg;
199                         break;
200
201                 case ARG_MODE:
202                         arg_mode = optarg;
203                         break;
204
205                 case ARG_LIST:
206                         arg_action = ACTION_LIST;
207                         break;
208
209                 case '?':
210                         return -EINVAL;
211
212                 default:
213                         assert_not_reached("Unhandled option");
214                 }
215
216         if (arg_action == ACTION_INHIBIT && optind == argc)
217                 arg_action = ACTION_LIST;
218
219         else if (arg_action == ACTION_INHIBIT && optind >= argc) {
220                 log_error("Missing command line to execute.");
221                 return -EINVAL;
222         }
223
224         return 1;
225 }
226
227 int main(int argc, char *argv[]) {
228         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
229         _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
230         int r;
231
232         elogind_set_program_name(argv[0]);
233         log_parse_environment();
234         log_open();
235
236         r = parse_argv(argc, argv);
237         if (r < 0)
238                 return EXIT_FAILURE;
239         if (r == 0)
240                 return EXIT_SUCCESS;
241
242         r = sd_bus_default_system(&bus);
243         if (r < 0) {
244                 log_error_errno(r, "Failed to connect to bus: %m");
245                 return EXIT_FAILURE;
246         }
247
248         if (arg_action == ACTION_LIST) {
249
250                 r = print_inhibitors(bus, &error);
251                 if (r < 0) {
252                         log_error("Failed to list inhibitors: %s", bus_error_message(&error, -r));
253                         return EXIT_FAILURE;
254                 }
255
256         } else {
257                 _cleanup_close_ int fd = -1;
258                 _cleanup_free_ char *w = NULL;
259                 pid_t pid;
260
261                 if (!arg_who)
262                         arg_who = w = strv_join(argv + optind, " ");
263
264                 if (!arg_mode)
265                         arg_mode = "block";
266
267                 fd = inhibit(bus, &error);
268                 if (fd < 0) {
269                         log_error("Failed to inhibit: %s", bus_error_message(&error, fd));
270                         return EXIT_FAILURE;
271                 }
272
273                 pid = fork();
274                 if (pid < 0) {
275                         log_error_errno(errno, "Failed to fork: %m");
276                         return EXIT_FAILURE;
277                 }
278
279                 if (pid == 0) {
280                         /* Child */
281
282                         (void) reset_all_signal_handlers();
283                         (void) reset_signal_mask();
284
285                         close_all_fds(NULL, 0);
286
287                         execvp(argv[optind], argv + optind);
288                         log_error_errno(errno, "Failed to execute %s: %m", argv[optind]);
289                         _exit(EXIT_FAILURE);
290                 }
291
292                 r = wait_for_terminate_and_warn(argv[optind], pid, true);
293                 return r < 0 ? EXIT_FAILURE : r;
294         }
295
296         return 0;
297 }