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