chiark / gitweb /
e94843be7f5fb8f68f0f9c1aa73ee68b970b38be
[elogind.git] / src / login / inhibit.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2012 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <getopt.h>
23 #include <assert.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <dbus.h>
27 #include <unistd.h>
28
29 #include "dbus-common.h"
30 #include "util.h"
31 #include "build.h"
32 #include "strv.h"
33
34 static const char* arg_what = "idle:sleep:shutdown";
35 static const char* arg_who = NULL;
36 static const char* arg_why = "Unknown reason";
37 static const char* arg_mode = "block";
38
39 static enum {
40         ACTION_INHIBIT,
41         ACTION_LIST
42 } arg_action = ACTION_INHIBIT;
43
44 static int inhibit(DBusConnection *bus, DBusError *error) {
45         _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
46         int r;
47
48         r = bus_method_call_with_reply(
49                         bus,
50                         "org.freedesktop.login1",
51                         "/org/freedesktop/login1",
52                         "org.freedesktop.login1.Manager",
53                         "Inhibit",
54                         &reply,
55                         NULL,
56                         DBUS_TYPE_STRING, &arg_what,
57                         DBUS_TYPE_STRING, &arg_who,
58                         DBUS_TYPE_STRING, &arg_why,
59                         DBUS_TYPE_STRING, &arg_mode,
60                         DBUS_TYPE_INVALID);
61         if (r < 0)
62                 return r;
63
64         if (!dbus_message_get_args(reply, error,
65                                    DBUS_TYPE_UNIX_FD, &r,
66                                    DBUS_TYPE_INVALID))
67                 r = -EIO;
68
69         return r;
70 }
71
72 static int print_inhibitors(DBusConnection *bus, DBusError *error) {
73         _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
74         unsigned n = 0;
75         DBusMessageIter iter, sub, sub2;
76         int r;
77
78         r = bus_method_call_with_reply(
79                         bus,
80                         "org.freedesktop.login1",
81                         "/org/freedesktop/login1",
82                         "org.freedesktop.login1.Manager",
83                         "ListInhibitors",
84                         &reply,
85                         NULL,
86                         DBUS_TYPE_INVALID);
87         if (r < 0)
88                 goto finish;
89
90         if (!dbus_message_iter_init(reply, &iter)) {
91                 r = -ENOMEM;
92                 goto finish;
93         }
94
95         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) {
96                 r = -EIO;
97                 goto finish;
98         }
99
100         dbus_message_iter_recurse(&iter, &sub);
101         while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
102                 const char *what, *who, *why, *mode;
103                 dbus_uint32_t uid, pid;
104
105                 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
106                         r = -EIO;
107                         goto finish;
108                 }
109
110                 dbus_message_iter_recurse(&sub, &sub2);
111
112                 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &what, true) < 0 ||
113                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &who, true) < 0 ||
114                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &why, true) < 0 ||
115                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &mode, true) < 0 ||
116                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &uid, true) < 0 ||
117                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, false) < 0) {
118                         r = -EIO;
119                         goto finish;
120                 }
121
122                 printf("     Who: %s (UID %lu, PID %lu)\n"
123                        "    What: %s\n"
124                        "     Why: %s\n"
125                        "    Mode: %s\n\n",
126                        who, (unsigned long) uid, (unsigned long) pid,
127                        what,
128                        why,
129                        mode);
130
131                 dbus_message_iter_next(&sub);
132
133                 n++;
134         }
135
136         printf("%u inhibitors listed.\n", n);
137         r = 0;
138
139 finish:
140         return r;
141 }
142
143 static int help(void) {
144
145         printf("%s [OPTIONS...] {COMMAND} ...\n\n"
146                "Execute a process while inhibiting shutdown/sleep/idle.\n\n"
147                "  -h --help               Show this help\n"
148                "     --version            Show package version\n"
149                "     --what=WHAT          Operations to inhibit, colon separated list of:\n"
150                "                          shutdown, sleep, idle, handle-power-key,\n"
151                "                          handle-suspend-key, handle-hibernate-key,\n"
152                "                          handle-lid-switch\n"
153                "     --who=STRING         A descriptive string who is inhibiting\n"
154                "     --why=STRING         A descriptive string why is being inhibited\n"
155                "     --mode=MODE          One of block or delay\n"
156                "     --list               List active inhibitors\n",
157                program_invocation_short_name);
158
159         return 0;
160 }
161
162 static int parse_argv(int argc, char *argv[]) {
163
164         enum {
165                 ARG_VERSION = 0x100,
166                 ARG_WHAT,
167                 ARG_WHO,
168                 ARG_WHY,
169                 ARG_MODE,
170                 ARG_LIST,
171         };
172
173         static const struct option options[] = {
174                 { "help",         no_argument,       NULL, 'h'              },
175                 { "version",      no_argument,       NULL, ARG_VERSION      },
176                 { "what",         required_argument, NULL, ARG_WHAT         },
177                 { "who",          required_argument, NULL, ARG_WHO          },
178                 { "why",          required_argument, NULL, ARG_WHY          },
179                 { "mode",         required_argument, NULL, ARG_MODE         },
180                 { "list",         no_argument,       NULL, ARG_LIST         },
181                 { NULL,           0,                 NULL, 0                }
182         };
183
184         int c;
185
186         assert(argc >= 0);
187         assert(argv);
188
189         while ((c = getopt_long(argc, argv, "+h", options, NULL)) >= 0) {
190
191                 switch (c) {
192
193                 case 'h':
194                         help();
195                         return 0;
196
197                 case ARG_VERSION:
198                         puts(PACKAGE_STRING);
199                         puts(SYSTEMD_FEATURES);
200                         return 0;
201
202                 case ARG_WHAT:
203                         arg_what = optarg;
204                         break;
205
206                 case ARG_WHO:
207                         arg_who = optarg;
208                         break;
209
210                 case ARG_WHY:
211                         arg_why = optarg;
212                         break;
213
214                 case ARG_MODE:
215                         arg_mode = optarg;
216                         break;
217
218                 case ARG_LIST:
219                         arg_action = ACTION_LIST;
220                         break;
221
222                 default:
223                         log_error("Unknown option code %c", c);
224                         return -EINVAL;
225                 }
226         }
227
228         if (arg_action == ACTION_INHIBIT && argc == 1)
229                 arg_action = ACTION_LIST;
230
231         else if (arg_action == ACTION_INHIBIT && optind >= argc) {
232                 log_error("Missing command line to execute.");
233                 return -EINVAL;
234         }
235
236         return 1;
237 }
238
239 int main(int argc, char *argv[]) {
240         int r, exit_code = 0;
241         DBusConnection *bus = NULL;
242         DBusError error;
243         int fd = -1;
244
245         dbus_error_init(&error);
246
247         log_parse_environment();
248         log_open();
249
250         r = parse_argv(argc, argv);
251         if (r <= 0)
252                 goto finish;
253
254         bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
255         if (!bus) {
256                 log_error("Failed to connect to bus: %s", bus_error_message(&error));
257                 r = -EIO;
258                 goto finish;
259         }
260
261         if (arg_action == ACTION_LIST) {
262
263                 r = print_inhibitors(bus, &error);
264                 if (r < 0) {
265                         log_error("Failed to list inhibitors: %s", bus_error_message_or_strerror(&error, -r));
266                         goto finish;
267                 }
268
269         } else {
270                 char *w = NULL;
271                 pid_t pid;
272
273                 if (!arg_who)
274                         arg_who = w = strv_join(argv + optind, " ");
275
276                 fd = inhibit(bus, &error);
277                 free(w);
278
279                 if (fd < 0) {
280                         log_error("Failed to inhibit: %s", bus_error_message_or_strerror(&error, -r));
281                         r = fd;
282                         goto finish;
283                 }
284
285                 pid = fork();
286                 if (pid < 0) {
287                         log_error("Failed to fork: %m");
288                         r = -errno;
289                         goto finish;
290                 }
291
292                 if (pid == 0) {
293                         /* Child */
294
295                         close_nointr_nofail(fd);
296                         close_all_fds(NULL, 0);
297
298                         execvp(argv[optind], argv + optind);
299                         log_error("Failed to execute %s: %m", argv[optind]);
300                         _exit(EXIT_FAILURE);
301                 }
302
303                 r = wait_for_terminate_and_warn(argv[optind], pid);
304                 if (r >= 0)
305                         exit_code = r;
306         }
307
308 finish:
309         if (bus) {
310                 dbus_connection_close(bus);
311                 dbus_connection_unref(bus);
312         }
313
314         dbus_error_free(&error);
315
316         if (fd >= 0)
317                 close_nointr_nofail(fd);
318
319         return r < 0 ? EXIT_FAILURE : exit_code;
320 }