]>
Commit | Line | Data |
---|---|---|
d6c9574f | 1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
7e4249b9 LP |
2 | |
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2010 Lennart Poettering | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 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 | General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ***/ | |
21 | ||
e4b61340 | 22 | #include <sys/reboot.h> |
7e4249b9 LP |
23 | #include <stdio.h> |
24 | #include <getopt.h> | |
25 | #include <stdbool.h> | |
26 | #include <string.h> | |
27 | #include <errno.h> | |
28 | #include <sys/ioctl.h> | |
29 | #include <termios.h> | |
30 | #include <unistd.h> | |
eb22ac37 | 31 | #include <fcntl.h> |
f1c5860b | 32 | #include <sys/socket.h> |
ee5762e3 | 33 | #include <sys/stat.h> |
0e098b15 | 34 | #include <stddef.h> |
501fc174 | 35 | #include <sys/prctl.h> |
7e4249b9 LP |
36 | |
37 | #include <dbus/dbus.h> | |
38 | ||
39 | #include "log.h" | |
40 | #include "util.h" | |
41 | #include "macro.h" | |
42 | #include "set.h" | |
e4b61340 | 43 | #include "utmp-wtmp.h" |
514f4ef5 | 44 | #include "special.h" |
eb22ac37 | 45 | #include "initreq.h" |
e4a9373f | 46 | #include "strv.h" |
9a1ac7b9 | 47 | #include "dbus-common.h" |
ab35fb1b | 48 | #include "cgroup-show.h" |
c6c18be3 | 49 | #include "cgroup-util.h" |
582a507f | 50 | #include "list.h" |
ee5762e3 LP |
51 | #include "path-lookup.h" |
52 | #include "conf-parser.h" | |
53 | #include "sd-daemon.h" | |
f6144808 | 54 | #include "shutdownd.h" |
d06dacd0 | 55 | #include "exit-status.h" |
22f4096c | 56 | #include "bus-errors.h" |
7d568925 | 57 | #include "build.h" |
71fad675 | 58 | #include "unit-name.h" |
7e4249b9 LP |
59 | |
60 | static const char *arg_type = NULL; | |
ea4a240d | 61 | static char **arg_property = NULL; |
7e4249b9 | 62 | static bool arg_all = false; |
e67c3609 | 63 | static const char *arg_job_mode = "replace"; |
af2d49f7 | 64 | static bool arg_user = false; |
ee5762e3 | 65 | static bool arg_global = false; |
e4b61340 | 66 | static bool arg_immediate = false; |
ee5762e3 | 67 | static bool arg_no_block = false; |
0736af98 | 68 | static bool arg_no_pager = false; |
e4b61340 LP |
69 | static bool arg_no_wtmp = false; |
70 | static bool arg_no_sync = false; | |
514f4ef5 | 71 | static bool arg_no_wall = false; |
ee5762e3 | 72 | static bool arg_no_reload = false; |
e4b61340 | 73 | static bool arg_dry = false; |
0183528f | 74 | static bool arg_quiet = false; |
ee5762e3 LP |
75 | static bool arg_full = false; |
76 | static bool arg_force = false; | |
77 | static bool arg_defaults = false; | |
501fc174 | 78 | static bool arg_ask_password = false; |
30732560 | 79 | static bool arg_failed = false; |
e4b61340 | 80 | static char **arg_wall = NULL; |
8a0867d6 LP |
81 | static const char *arg_kill_who = NULL; |
82 | static const char *arg_kill_mode = NULL; | |
83 | static int arg_signal = SIGTERM; | |
f6144808 | 84 | static usec_t arg_when = 0; |
4445a875 | 85 | static enum action { |
e4b61340 LP |
86 | ACTION_INVALID, |
87 | ACTION_SYSTEMCTL, | |
88 | ACTION_HALT, | |
89 | ACTION_POWEROFF, | |
90 | ACTION_REBOOT, | |
20b09ca7 LP |
91 | ACTION_KEXEC, |
92 | ACTION_EXIT, | |
e4b61340 LP |
93 | ACTION_RUNLEVEL2, |
94 | ACTION_RUNLEVEL3, | |
95 | ACTION_RUNLEVEL4, | |
96 | ACTION_RUNLEVEL5, | |
97 | ACTION_RESCUE, | |
514f4ef5 LP |
98 | ACTION_EMERGENCY, |
99 | ACTION_DEFAULT, | |
e4b61340 LP |
100 | ACTION_RELOAD, |
101 | ACTION_REEXEC, | |
102 | ACTION_RUNLEVEL, | |
f6144808 | 103 | ACTION_CANCEL_SHUTDOWN, |
e4b61340 LP |
104 | _ACTION_MAX |
105 | } arg_action = ACTION_SYSTEMCTL; | |
4445a875 LP |
106 | static enum dot { |
107 | DOT_ALL, | |
108 | DOT_ORDER, | |
109 | DOT_REQUIRE | |
110 | } arg_dot = DOT_ALL; | |
e4b61340 | 111 | |
f4579ce7 LP |
112 | static bool private_bus = false; |
113 | ||
1888c907 LP |
114 | static pid_t pager_pid = 0; |
115 | ||
ee5762e3 | 116 | static int daemon_reload(DBusConnection *bus, char **args, unsigned n); |
ec14911e | 117 | static void pager_open(void); |
ee5762e3 | 118 | |
2ee68f72 | 119 | static bool on_tty(void) { |
2cc59dbf LP |
120 | static int t = -1; |
121 | ||
75d12d57 LP |
122 | /* Note that this is invoked relatively early, before we start |
123 | * the pager. That means the value we return reflects whether | |
124 | * we originally were started on a tty, not if we currently | |
060ed82e | 125 | * are. But this is intended, since we want colour and so on |
75d12d57 LP |
126 | * when run in our own pager. */ |
127 | ||
2cc59dbf LP |
128 | if (_unlikely_(t < 0)) |
129 | t = isatty(STDOUT_FILENO) > 0; | |
130 | ||
2ee68f72 LP |
131 | return t; |
132 | } | |
133 | ||
501fc174 LP |
134 | static void spawn_ask_password_agent(void) { |
135 | pid_t parent, child; | |
136 | ||
137 | /* We check STDIN here, not STDOUT, since this is about input, | |
138 | * not output */ | |
139 | if (!isatty(STDIN_FILENO)) | |
140 | return; | |
141 | ||
142 | if (!arg_ask_password) | |
143 | return; | |
715554e7 LP |
144 | |
145 | if (arg_user) | |
146 | return; | |
501fc174 LP |
147 | |
148 | parent = getpid(); | |
149 | ||
150 | /* Spawns a temporary TTY agent, making sure it goes away when | |
151 | * we go away */ | |
152 | ||
153 | if ((child = fork()) < 0) | |
154 | return; | |
155 | ||
156 | if (child == 0) { | |
157 | /* In the child */ | |
501fc174 LP |
158 | const char * const args[] = { |
159 | SYSTEMD_TTY_ASK_PASSWORD_AGENT_BINARY_PATH, | |
160 | "--watch", | |
161 | NULL | |
162 | }; | |
163 | ||
7f6d6135 | 164 | int fd; |
7fc01d33 | 165 | bool stdout_is_tty, stderr_is_tty; |
7f6d6135 LP |
166 | |
167 | /* Make sure the agent goes away when the parent dies */ | |
501fc174 LP |
168 | if (prctl(PR_SET_PDEATHSIG, SIGTERM) < 0) |
169 | _exit(EXIT_FAILURE); | |
170 | ||
171 | /* Check whether our parent died before we were able | |
172 | * to set the death signal */ | |
173 | if (getppid() != parent) | |
174 | _exit(EXIT_SUCCESS); | |
175 | ||
7f6d6135 LP |
176 | /* Don't leak fds to the agent */ |
177 | close_all_fds(NULL, 0); | |
178 | ||
7fc01d33 LP |
179 | stdout_is_tty = isatty(STDOUT_FILENO); |
180 | stderr_is_tty = isatty(STDERR_FILENO); | |
181 | ||
182 | if (!stdout_is_tty || !stderr_is_tty) { | |
060ed82e LP |
183 | /* Detach from stdout/stderr. and reopen |
184 | * /dev/tty for them. This is important to | |
185 | * ensure that when systemctl is started via | |
186 | * popen() or a similar call that expects to | |
187 | * read EOF we actually do generate EOF and | |
188 | * not delay this indefinitely by because we | |
189 | * keep an unused copy of stdin around. */ | |
190 | if ((fd = open("/dev/tty", O_WRONLY)) < 0) { | |
191 | log_error("Failed to open /dev/tty: %m"); | |
192 | _exit(EXIT_FAILURE); | |
193 | } | |
194 | ||
7fc01d33 | 195 | if (!stdout_is_tty) |
060ed82e | 196 | dup2(fd, STDOUT_FILENO); |
060ed82e | 197 | |
7fc01d33 | 198 | if (!stderr_is_tty) |
060ed82e | 199 | dup2(fd, STDERR_FILENO); |
060ed82e LP |
200 | |
201 | if (fd > 2) | |
202 | close(fd); | |
7f6d6135 LP |
203 | } |
204 | ||
501fc174 LP |
205 | execv(args[0], (char **) args); |
206 | _exit(EXIT_FAILURE); | |
207 | } | |
208 | } | |
209 | ||
2ee68f72 LP |
210 | static const char *ansi_highlight(bool b) { |
211 | ||
212 | if (!on_tty()) | |
2cc59dbf LP |
213 | return ""; |
214 | ||
215 | return b ? ANSI_HIGHLIGHT_ON : ANSI_HIGHLIGHT_OFF; | |
216 | } | |
217 | ||
2ee68f72 LP |
218 | static const char *ansi_highlight_green(bool b) { |
219 | ||
220 | if (!on_tty()) | |
221 | return ""; | |
222 | ||
223 | return b ? ANSI_HIGHLIGHT_GREEN_ON : ANSI_HIGHLIGHT_OFF; | |
224 | } | |
225 | ||
f73e33d9 | 226 | static bool error_is_no_service(const DBusError *error) { |
514f4ef5 LP |
227 | assert(error); |
228 | ||
e4b61340 LP |
229 | if (!dbus_error_is_set(error)) |
230 | return false; | |
231 | ||
232 | if (dbus_error_has_name(error, DBUS_ERROR_NAME_HAS_NO_OWNER)) | |
233 | return true; | |
234 | ||
235 | if (dbus_error_has_name(error, DBUS_ERROR_SERVICE_UNKNOWN)) | |
236 | return true; | |
237 | ||
238 | return startswith(error->name, "org.freedesktop.DBus.Error.Spawn."); | |
239 | } | |
7e4249b9 | 240 | |
22f4096c LP |
241 | static int translate_bus_error_to_exit_status(int r, const DBusError *error) { |
242 | assert(error); | |
243 | ||
244 | if (!dbus_error_is_set(error)) | |
245 | return r; | |
246 | ||
247 | if (dbus_error_has_name(error, DBUS_ERROR_ACCESS_DENIED) || | |
248 | dbus_error_has_name(error, BUS_ERROR_ONLY_BY_DEPENDENCY) || | |
249 | dbus_error_has_name(error, BUS_ERROR_NO_ISOLATION) || | |
250 | dbus_error_has_name(error, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE)) | |
251 | return EXIT_NOPERMISSION; | |
252 | ||
253 | if (dbus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT)) | |
254 | return EXIT_NOTINSTALLED; | |
255 | ||
256 | if (dbus_error_has_name(error, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE) || | |
257 | dbus_error_has_name(error, BUS_ERROR_NOT_SUPPORTED)) | |
258 | return EXIT_NOTIMPLEMENTED; | |
259 | ||
260 | if (dbus_error_has_name(error, BUS_ERROR_LOAD_FAILED)) | |
261 | return EXIT_NOTCONFIGURED; | |
262 | ||
263 | if (r != 0) | |
264 | return r; | |
265 | ||
266 | return EXIT_FAILURE; | |
267 | } | |
268 | ||
7e4249b9 LP |
269 | static int bus_iter_get_basic_and_next(DBusMessageIter *iter, int type, void *data, bool next) { |
270 | ||
514f4ef5 LP |
271 | assert(iter); |
272 | assert(data); | |
273 | ||
7e4249b9 LP |
274 | if (dbus_message_iter_get_arg_type(iter) != type) |
275 | return -EIO; | |
276 | ||
277 | dbus_message_iter_get_basic(iter, data); | |
278 | ||
279 | if (!dbus_message_iter_next(iter) != !next) | |
280 | return -EIO; | |
281 | ||
282 | return 0; | |
283 | } | |
284 | ||
514f4ef5 | 285 | static void warn_wall(enum action action) { |
ef2f1067 | 286 | static const char *table[_ACTION_MAX] = { |
514f4ef5 LP |
287 | [ACTION_HALT] = "The system is going down for system halt NOW!", |
288 | [ACTION_REBOOT] = "The system is going down for reboot NOW!", | |
289 | [ACTION_POWEROFF] = "The system is going down for power-off NOW!", | |
20b09ca7 | 290 | [ACTION_KEXEC] = "The system is going down for kexec reboot NOW!", |
514f4ef5 LP |
291 | [ACTION_RESCUE] = "The system is going down to rescue mode NOW!", |
292 | [ACTION_EMERGENCY] = "The system is going down to emergency mode NOW!" | |
ef2f1067 LP |
293 | }; |
294 | ||
514f4ef5 LP |
295 | if (arg_no_wall) |
296 | return; | |
297 | ||
e4a9373f LP |
298 | if (arg_wall) { |
299 | char *p; | |
300 | ||
301 | if (!(p = strv_join(arg_wall, " "))) { | |
302 | log_error("Failed to join strings."); | |
303 | return; | |
304 | } | |
305 | ||
306 | if (*p) { | |
7af53310 | 307 | utmp_wall(p, NULL); |
e4a9373f LP |
308 | free(p); |
309 | return; | |
310 | } | |
311 | ||
312 | free(p); | |
313 | } | |
314 | ||
514f4ef5 | 315 | if (!table[action]) |
ef2f1067 LP |
316 | return; |
317 | ||
7af53310 | 318 | utmp_wall(table[action], NULL); |
ef2f1067 LP |
319 | } |
320 | ||
36c32ba2 LP |
321 | struct unit_info { |
322 | const char *id; | |
323 | const char *description; | |
324 | const char *load_state; | |
325 | const char *active_state; | |
326 | const char *sub_state; | |
327 | const char *following; | |
328 | const char *unit_path; | |
329 | uint32_t job_id; | |
330 | const char *job_type; | |
331 | const char *job_path; | |
332 | }; | |
333 | ||
334 | static int compare_unit_info(const void *a, const void *b) { | |
335 | const char *d1, *d2; | |
336 | const struct unit_info *u = a, *v = b; | |
337 | ||
338 | d1 = strrchr(u->id, '.'); | |
339 | d2 = strrchr(v->id, '.'); | |
340 | ||
341 | if (d1 && d2) { | |
342 | int r; | |
343 | ||
a2a3a5b9 | 344 | if ((r = strcasecmp(d1, d2)) != 0) |
36c32ba2 LP |
345 | return r; |
346 | } | |
347 | ||
a2a3a5b9 | 348 | return strcasecmp(u->id, v->id); |
36c32ba2 LP |
349 | } |
350 | ||
30732560 | 351 | static bool output_show_unit(const struct unit_info *u) { |
33330222 | 352 | const char *dot; |
b036fc00 | 353 | |
30732560 LP |
354 | if (arg_failed) |
355 | return streq(u->active_state, "failed"); | |
356 | ||
33330222 ZJS |
357 | return (!arg_type || ((dot = strrchr(u->id, '.')) && |
358 | streq(dot+1, arg_type))) && | |
359 | (arg_all || !(streq(u->active_state, "inactive") || u->following[0]) || u->job_id > 0); | |
360 | } | |
361 | ||
eb68c413 | 362 | static void output_units_list(const struct unit_info *unit_infos, unsigned c) { |
688c6725 | 363 | unsigned active_len, sub_len, job_len, n_shown = 0; |
b036fc00 | 364 | const struct unit_info *u; |
33330222 | 365 | |
b036fc00 LP |
366 | active_len = sizeof("ACTIVE")-1; |
367 | sub_len = sizeof("SUB")-1; | |
368 | job_len = sizeof("JOB")-1; | |
369 | ||
370 | for (u = unit_infos; u < unit_infos + c; u++) { | |
30732560 | 371 | if (!output_show_unit(u)) |
b036fc00 LP |
372 | continue; |
373 | ||
374 | active_len = MAX(active_len, strlen(u->active_state)); | |
375 | sub_len = MAX(sub_len, strlen(u->sub_state)); | |
376 | if (u->job_id != 0) | |
377 | job_len = MAX(job_len, strlen(u->job_type)); | |
33330222 ZJS |
378 | } |
379 | ||
eb68c413 | 380 | if (on_tty()) { |
33330222 ZJS |
381 | printf("%-25s %-6s %-*s %-*s %-*s", "UNIT", "LOAD", |
382 | active_len, "ACTIVE", sub_len, "SUB", job_len, "JOB"); | |
eb68c413 | 383 | if (columns() >= 80+12 || arg_full) |
33330222 | 384 | printf(" %s\n", "DESCRIPTION"); |
eb68c413 | 385 | else |
33330222 | 386 | printf("\n"); |
eb68c413 ZJS |
387 | } |
388 | ||
b036fc00 LP |
389 | for (u = unit_infos; u < unit_infos + c; u++) { |
390 | char *e; | |
391 | int a = 0, b = 0; | |
392 | const char *on_loaded, *off_loaded; | |
393 | const char *on_active, *off_active; | |
394 | ||
30732560 | 395 | if (!output_show_unit(u)) |
b036fc00 LP |
396 | continue; |
397 | ||
688c6725 LP |
398 | n_shown++; |
399 | ||
00dc5d76 LP |
400 | if (!streq(u->load_state, "loaded") && |
401 | !streq(u->load_state, "banned")) { | |
b036fc00 LP |
402 | on_loaded = ansi_highlight(true); |
403 | off_loaded = ansi_highlight(false); | |
404 | } else | |
405 | on_loaded = off_loaded = ""; | |
406 | ||
407 | if (streq(u->active_state, "failed")) { | |
408 | on_active = ansi_highlight(true); | |
409 | off_active = ansi_highlight(false); | |
410 | } else | |
411 | on_active = off_active = ""; | |
eb68c413 | 412 | |
b036fc00 | 413 | e = arg_full ? NULL : ellipsize(u->id, 25, 33); |
eb68c413 | 414 | |
b036fc00 LP |
415 | printf("%-25s %s%-6s%s %s%-*s %-*s%s%n", |
416 | e ? e : u->id, | |
417 | on_loaded, u->load_state, off_loaded, | |
418 | on_active, active_len, u->active_state, | |
419 | sub_len, u->sub_state, off_active, | |
420 | &a); | |
eb68c413 | 421 | |
b036fc00 LP |
422 | free(e); |
423 | ||
424 | a -= strlen(on_loaded) + strlen(off_loaded); | |
425 | a -= strlen(on_active) + strlen(off_active); | |
426 | ||
427 | if (u->job_id != 0) | |
428 | printf(" %-*s", job_len, u->job_type); | |
429 | else | |
430 | b = 1 + job_len; | |
431 | ||
432 | if (a + b + 1 < columns()) { | |
433 | if (u->job_id == 0) | |
434 | printf(" %-*s", job_len, ""); | |
435 | ||
436 | if (arg_full) | |
437 | printf(" %s", u->description); | |
438 | else | |
439 | printf(" %.*s", columns() - a - b - 1, u->description); | |
eb68c413 | 440 | } |
b036fc00 LP |
441 | |
442 | fputs("\n", stdout); | |
eb68c413 ZJS |
443 | } |
444 | ||
9b2fcafb | 445 | if (on_tty()) { |
eb68c413 ZJS |
446 | printf("\nLOAD = Reflects whether the unit definition was properly loaded.\n" |
447 | "ACTIVE = The high-level unit activation state, i.e. generalization of SUB.\n" | |
448 | "SUB = The low-level unit activation state, values depend on unit type.\n" | |
449 | "JOB = Pending job for the unit.\n"); | |
450 | ||
451 | if (arg_all) | |
688c6725 | 452 | printf("\n%u units listed.\n", n_shown); |
eb68c413 | 453 | else |
688c6725 | 454 | printf("\n%u units listed. Pass --all to see inactive units, too.\n", n_shown); |
eb68c413 ZJS |
455 | } |
456 | } | |
457 | ||
7e4249b9 LP |
458 | static int list_units(DBusConnection *bus, char **args, unsigned n) { |
459 | DBusMessage *m = NULL, *reply = NULL; | |
460 | DBusError error; | |
461 | int r; | |
462 | DBusMessageIter iter, sub, sub2; | |
eb68c413 | 463 | unsigned c = 0, n_units = 0; |
36c32ba2 | 464 | struct unit_info *unit_infos = NULL; |
7e4249b9 LP |
465 | |
466 | dbus_error_init(&error); | |
467 | ||
514f4ef5 LP |
468 | assert(bus); |
469 | ||
ec14911e LP |
470 | pager_open(); |
471 | ||
7e4249b9 LP |
472 | if (!(m = dbus_message_new_method_call( |
473 | "org.freedesktop.systemd1", | |
474 | "/org/freedesktop/systemd1", | |
475 | "org.freedesktop.systemd1.Manager", | |
476 | "ListUnits"))) { | |
477 | log_error("Could not allocate message."); | |
478 | return -ENOMEM; | |
479 | } | |
480 | ||
481 | if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) { | |
4cf5d675 | 482 | log_error("Failed to issue method call: %s", bus_error_message(&error)); |
7e4249b9 LP |
483 | r = -EIO; |
484 | goto finish; | |
485 | } | |
486 | ||
487 | if (!dbus_message_iter_init(reply, &iter) || | |
488 | dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY || | |
489 | dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) { | |
490 | log_error("Failed to parse reply."); | |
491 | r = -EIO; | |
492 | goto finish; | |
493 | } | |
494 | ||
495 | dbus_message_iter_recurse(&iter, &sub); | |
496 | ||
7e4249b9 | 497 | while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) { |
36c32ba2 | 498 | struct unit_info *u; |
7e4249b9 LP |
499 | |
500 | if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) { | |
501 | log_error("Failed to parse reply."); | |
502 | r = -EIO; | |
503 | goto finish; | |
504 | } | |
505 | ||
36c32ba2 LP |
506 | if (c >= n_units) { |
507 | struct unit_info *w; | |
508 | ||
509 | n_units = MAX(2*c, 16); | |
510 | w = realloc(unit_infos, sizeof(struct unit_info) * n_units); | |
511 | ||
512 | if (!w) { | |
513 | log_error("Failed to allocate unit array."); | |
514 | r = -ENOMEM; | |
515 | goto finish; | |
516 | } | |
517 | ||
518 | unit_infos = w; | |
519 | } | |
520 | ||
521 | u = unit_infos+c; | |
522 | ||
7e4249b9 LP |
523 | dbus_message_iter_recurse(&sub, &sub2); |
524 | ||
36c32ba2 LP |
525 | if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->id, true) < 0 || |
526 | bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->description, true) < 0 || | |
527 | bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->load_state, true) < 0 || | |
528 | bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->active_state, true) < 0 || | |
529 | bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->sub_state, true) < 0 || | |
530 | bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->following, true) < 0 || | |
531 | bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &u->unit_path, true) < 0 || | |
532 | bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &u->job_id, true) < 0 || | |
533 | bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->job_type, true) < 0 || | |
534 | bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &u->job_path, false) < 0) { | |
7e4249b9 LP |
535 | log_error("Failed to parse reply."); |
536 | r = -EIO; | |
537 | goto finish; | |
538 | } | |
539 | ||
36c32ba2 LP |
540 | dbus_message_iter_next(&sub); |
541 | c++; | |
542 | } | |
543 | ||
bd40a2d8 LP |
544 | if (c > 0) { |
545 | qsort(unit_infos, c, sizeof(struct unit_info), compare_unit_info); | |
546 | output_units_list(unit_infos, c); | |
547 | } | |
4445a875 LP |
548 | |
549 | r = 0; | |
550 | ||
551 | finish: | |
552 | if (m) | |
553 | dbus_message_unref(m); | |
554 | ||
555 | if (reply) | |
556 | dbus_message_unref(reply); | |
557 | ||
36c32ba2 LP |
558 | free(unit_infos); |
559 | ||
4445a875 LP |
560 | dbus_error_free(&error); |
561 | ||
562 | return r; | |
563 | } | |
564 | ||
565 | static int dot_one_property(const char *name, const char *prop, DBusMessageIter *iter) { | |
566 | static const char * const colors[] = { | |
567 | "Requires", "[color=\"black\"]", | |
568 | "RequiresOverridable", "[color=\"black\"]", | |
569 | "Requisite", "[color=\"darkblue\"]", | |
570 | "RequisiteOverridable", "[color=\"darkblue\"]", | |
571 | "Wants", "[color=\"darkgrey\"]", | |
572 | "Conflicts", "[color=\"red\"]", | |
69dd2852 | 573 | "ConflictedBy", "[color=\"red\"]", |
4445a875 LP |
574 | "After", "[color=\"green\"]" |
575 | }; | |
576 | ||
577 | const char *c = NULL; | |
578 | unsigned i; | |
579 | ||
580 | assert(name); | |
581 | assert(prop); | |
582 | assert(iter); | |
583 | ||
584 | for (i = 0; i < ELEMENTSOF(colors); i += 2) | |
585 | if (streq(colors[i], prop)) { | |
586 | c = colors[i+1]; | |
587 | break; | |
588 | } | |
589 | ||
590 | if (!c) | |
591 | return 0; | |
592 | ||
593 | if (arg_dot != DOT_ALL) | |
594 | if ((arg_dot == DOT_ORDER) != streq(prop, "After")) | |
595 | return 0; | |
596 | ||
597 | switch (dbus_message_iter_get_arg_type(iter)) { | |
598 | ||
599 | case DBUS_TYPE_ARRAY: | |
600 | ||
601 | if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) { | |
602 | DBusMessageIter sub; | |
603 | ||
604 | dbus_message_iter_recurse(iter, &sub); | |
605 | ||
606 | while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) { | |
607 | const char *s; | |
608 | ||
609 | assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING); | |
610 | dbus_message_iter_get_basic(&sub, &s); | |
611 | printf("\t\"%s\"->\"%s\" %s;\n", name, s, c); | |
612 | ||
613 | dbus_message_iter_next(&sub); | |
614 | } | |
615 | ||
616 | return 0; | |
617 | } | |
618 | } | |
619 | ||
620 | return 0; | |
621 | } | |
622 | ||
623 | static int dot_one(DBusConnection *bus, const char *name, const char *path) { | |
624 | DBusMessage *m = NULL, *reply = NULL; | |
625 | const char *interface = "org.freedesktop.systemd1.Unit"; | |
626 | int r; | |
627 | DBusError error; | |
628 | DBusMessageIter iter, sub, sub2, sub3; | |
629 | ||
630 | assert(bus); | |
631 | assert(path); | |
632 | ||
633 | dbus_error_init(&error); | |
634 | ||
635 | if (!(m = dbus_message_new_method_call( | |
636 | "org.freedesktop.systemd1", | |
637 | path, | |
638 | "org.freedesktop.DBus.Properties", | |
639 | "GetAll"))) { | |
640 | log_error("Could not allocate message."); | |
641 | r = -ENOMEM; | |
642 | goto finish; | |
643 | } | |
644 | ||
645 | if (!dbus_message_append_args(m, | |
646 | DBUS_TYPE_STRING, &interface, | |
647 | DBUS_TYPE_INVALID)) { | |
648 | log_error("Could not append arguments to message."); | |
649 | r = -ENOMEM; | |
650 | goto finish; | |
651 | } | |
652 | ||
653 | if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) { | |
4cf5d675 | 654 | log_error("Failed to issue method call: %s", bus_error_message(&error)); |
4445a875 LP |
655 | r = -EIO; |
656 | goto finish; | |
657 | } | |
658 | ||
659 | if (!dbus_message_iter_init(reply, &iter) || | |
660 | dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY || | |
661 | dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) { | |
662 | log_error("Failed to parse reply."); | |
663 | r = -EIO; | |
664 | goto finish; | |
665 | } | |
666 | ||
667 | dbus_message_iter_recurse(&iter, &sub); | |
668 | ||
669 | while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) { | |
670 | const char *prop; | |
671 | ||
672 | if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) { | |
673 | log_error("Failed to parse reply."); | |
674 | r = -EIO; | |
675 | goto finish; | |
676 | } | |
677 | ||
678 | dbus_message_iter_recurse(&sub, &sub2); | |
679 | ||
680 | if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &prop, true) < 0) { | |
681 | log_error("Failed to parse reply."); | |
682 | r = -EIO; | |
683 | goto finish; | |
684 | } | |
685 | ||
686 | if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) { | |
687 | log_error("Failed to parse reply."); | |
688 | r = -EIO; | |
689 | goto finish; | |
690 | } | |
691 | ||
692 | dbus_message_iter_recurse(&sub2, &sub3); | |
693 | ||
694 | if (dot_one_property(name, prop, &sub3)) { | |
695 | log_error("Failed to parse reply."); | |
696 | r = -EIO; | |
697 | goto finish; | |
698 | } | |
699 | ||
700 | dbus_message_iter_next(&sub); | |
701 | } | |
702 | ||
e364ad06 LP |
703 | r = 0; |
704 | ||
4445a875 LP |
705 | finish: |
706 | if (m) | |
707 | dbus_message_unref(m); | |
708 | ||
709 | if (reply) | |
710 | dbus_message_unref(reply); | |
711 | ||
712 | dbus_error_free(&error); | |
713 | ||
714 | return r; | |
715 | } | |
716 | ||
717 | static int dot(DBusConnection *bus, char **args, unsigned n) { | |
718 | DBusMessage *m = NULL, *reply = NULL; | |
719 | DBusError error; | |
720 | int r; | |
721 | DBusMessageIter iter, sub, sub2; | |
722 | ||
723 | dbus_error_init(&error); | |
724 | ||
725 | assert(bus); | |
726 | ||
727 | if (!(m = dbus_message_new_method_call( | |
728 | "org.freedesktop.systemd1", | |
729 | "/org/freedesktop/systemd1", | |
730 | "org.freedesktop.systemd1.Manager", | |
731 | "ListUnits"))) { | |
732 | log_error("Could not allocate message."); | |
733 | return -ENOMEM; | |
734 | } | |
735 | ||
736 | if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) { | |
4cf5d675 | 737 | log_error("Failed to issue method call: %s", bus_error_message(&error)); |
4445a875 LP |
738 | r = -EIO; |
739 | goto finish; | |
740 | } | |
741 | ||
742 | if (!dbus_message_iter_init(reply, &iter) || | |
743 | dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY || | |
744 | dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) { | |
745 | log_error("Failed to parse reply."); | |
746 | r = -EIO; | |
747 | goto finish; | |
748 | } | |
749 | ||
750 | printf("digraph systemd {\n"); | |
751 | ||
752 | dbus_message_iter_recurse(&iter, &sub); | |
753 | while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) { | |
4a4d6b4b | 754 | const char *id, *description, *load_state, *active_state, *sub_state, *following, *unit_path; |
4445a875 LP |
755 | |
756 | if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) { | |
757 | log_error("Failed to parse reply."); | |
758 | r = -EIO; | |
759 | goto finish; | |
760 | } | |
761 | ||
762 | dbus_message_iter_recurse(&sub, &sub2); | |
763 | ||
764 | if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 || | |
765 | bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 || | |
766 | bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 || | |
767 | bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 || | |
768 | bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 || | |
4a4d6b4b | 769 | bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &following, true) < 0 || |
4445a875 LP |
770 | bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, true) < 0) { |
771 | log_error("Failed to parse reply."); | |
772 | r = -EIO; | |
773 | goto finish; | |
774 | } | |
775 | ||
776 | if ((r = dot_one(bus, id, unit_path)) < 0) | |
777 | goto finish; | |
778 | ||
779 | /* printf("\t\"%s\";\n", id); */ | |
780 | dbus_message_iter_next(&sub); | |
781 | } | |
782 | ||
783 | printf("}\n"); | |
784 | ||
785 | log_info(" Color legend: black = Requires\n" | |
786 | " dark blue = Requisite\n" | |
787 | " dark grey = Wants\n" | |
788 | " red = Conflicts\n" | |
789 | " green = After\n"); | |
790 | ||
791 | if (isatty(fileno(stdout))) | |
792 | log_notice("-- You probably want to process this output with graphviz' dot tool.\n" | |
793 | "-- Try a shell pipeline like 'systemctl dot | dot -Tsvg > systemd.svg'!\n"); | |
7e4249b9 LP |
794 | |
795 | r = 0; | |
796 | ||
797 | finish: | |
798 | if (m) | |
799 | dbus_message_unref(m); | |
800 | ||
801 | if (reply) | |
802 | dbus_message_unref(reply); | |
803 | ||
804 | dbus_error_free(&error); | |
805 | ||
806 | return r; | |
807 | } | |
808 | ||
809 | static int list_jobs(DBusConnection *bus, char **args, unsigned n) { | |
810 | DBusMessage *m = NULL, *reply = NULL; | |
811 | DBusError error; | |
812 | int r; | |
813 | DBusMessageIter iter, sub, sub2; | |
814 | unsigned k = 0; | |
815 | ||
816 | dbus_error_init(&error); | |
817 | ||
514f4ef5 LP |
818 | assert(bus); |
819 | ||
ec14911e LP |
820 | pager_open(); |
821 | ||
7e4249b9 LP |
822 | if (!(m = dbus_message_new_method_call( |
823 | "org.freedesktop.systemd1", | |
824 | "/org/freedesktop/systemd1", | |
825 | "org.freedesktop.systemd1.Manager", | |
826 | "ListJobs"))) { | |
827 | log_error("Could not allocate message."); | |
828 | return -ENOMEM; | |
829 | } | |
830 | ||
831 | if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) { | |
4cf5d675 | 832 | log_error("Failed to issue method call: %s", bus_error_message(&error)); |
7e4249b9 LP |
833 | r = -EIO; |
834 | goto finish; | |
835 | } | |
836 | ||
837 | if (!dbus_message_iter_init(reply, &iter) || | |
838 | dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY || | |
839 | dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) { | |
840 | log_error("Failed to parse reply."); | |
841 | r = -EIO; | |
842 | goto finish; | |
843 | } | |
844 | ||
845 | dbus_message_iter_recurse(&iter, &sub); | |
846 | ||
f73e33d9 LP |
847 | if (isatty(STDOUT_FILENO)) |
848 | printf("%4s %-25s %-15s %-7s\n", "JOB", "UNIT", "TYPE", "STATE"); | |
7e4249b9 LP |
849 | |
850 | while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) { | |
851 | const char *name, *type, *state, *job_path, *unit_path; | |
852 | uint32_t id; | |
8fe914ec | 853 | char *e; |
7e4249b9 LP |
854 | |
855 | if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) { | |
856 | log_error("Failed to parse reply."); | |
857 | r = -EIO; | |
858 | goto finish; | |
859 | } | |
860 | ||
861 | dbus_message_iter_recurse(&sub, &sub2); | |
862 | ||
863 | if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &id, true) < 0 || | |
864 | bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0 || | |
865 | bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 || | |
866 | bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, true) < 0 || | |
867 | bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, true) < 0 || | |
868 | bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, false) < 0) { | |
869 | log_error("Failed to parse reply."); | |
870 | r = -EIO; | |
871 | goto finish; | |
872 | } | |
873 | ||
f73e33d9 LP |
874 | e = arg_full ? NULL : ellipsize(name, 25, 33); |
875 | printf("%4u %-25s %-15s %-7s\n", id, e ? e : name, type, state); | |
8fe914ec LP |
876 | free(e); |
877 | ||
7e4249b9 LP |
878 | k++; |
879 | ||
880 | dbus_message_iter_next(&sub); | |
881 | } | |
882 | ||
f73e33d9 LP |
883 | if (isatty(STDOUT_FILENO)) |
884 | printf("\n%u jobs listed.\n", k); | |
885 | ||
7e4249b9 LP |
886 | r = 0; |
887 | ||
888 | finish: | |
889 | if (m) | |
890 | dbus_message_unref(m); | |
891 | ||
892 | if (reply) | |
893 | dbus_message_unref(reply); | |
894 | ||
895 | dbus_error_free(&error); | |
896 | ||
897 | return r; | |
898 | } | |
899 | ||
900 | static int load_unit(DBusConnection *bus, char **args, unsigned n) { | |
901 | DBusMessage *m = NULL, *reply = NULL; | |
902 | DBusError error; | |
903 | int r; | |
904 | unsigned i; | |
905 | ||
906 | dbus_error_init(&error); | |
907 | ||
514f4ef5 LP |
908 | assert(bus); |
909 | assert(args); | |
910 | ||
7e4249b9 LP |
911 | for (i = 1; i < n; i++) { |
912 | ||
913 | if (!(m = dbus_message_new_method_call( | |
914 | "org.freedesktop.systemd1", | |
915 | "/org/freedesktop/systemd1", | |
916 | "org.freedesktop.systemd1.Manager", | |
917 | "LoadUnit"))) { | |
918 | log_error("Could not allocate message."); | |
919 | r = -ENOMEM; | |
920 | goto finish; | |
921 | } | |
922 | ||
923 | if (!dbus_message_append_args(m, | |
924 | DBUS_TYPE_STRING, &args[i], | |
925 | DBUS_TYPE_INVALID)) { | |
926 | log_error("Could not append arguments to message."); | |
927 | r = -ENOMEM; | |
928 | goto finish; | |
929 | } | |
930 | ||
931 | if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) { | |
4cf5d675 | 932 | log_error("Failed to issue method call: %s", bus_error_message(&error)); |
7e4249b9 LP |
933 | r = -EIO; |
934 | goto finish; | |
935 | } | |
936 | ||
937 | dbus_message_unref(m); | |
938 | dbus_message_unref(reply); | |
939 | ||
940 | m = reply = NULL; | |
941 | } | |
942 | ||
943 | r = 0; | |
944 | ||
945 | finish: | |
946 | if (m) | |
947 | dbus_message_unref(m); | |
948 | ||
949 | if (reply) | |
950 | dbus_message_unref(reply); | |
951 | ||
952 | dbus_error_free(&error); | |
953 | ||
954 | return r; | |
955 | } | |
956 | ||
957 | static int cancel_job(DBusConnection *bus, char **args, unsigned n) { | |
958 | DBusMessage *m = NULL, *reply = NULL; | |
959 | DBusError error; | |
960 | int r; | |
961 | unsigned i; | |
962 | ||
963 | dbus_error_init(&error); | |
964 | ||
514f4ef5 LP |
965 | assert(bus); |
966 | assert(args); | |
967 | ||
ee5762e3 LP |
968 | if (n <= 1) |
969 | return daemon_reload(bus, args, n); | |
970 | ||
7e4249b9 LP |
971 | for (i = 1; i < n; i++) { |
972 | unsigned id; | |
973 | const char *path; | |
974 | ||
975 | if (!(m = dbus_message_new_method_call( | |
976 | "org.freedesktop.systemd1", | |
977 | "/org/freedesktop/systemd1", | |
978 | "org.freedesktop.systemd1.Manager", | |
979 | "GetJob"))) { | |
980 | log_error("Could not allocate message."); | |
981 | r = -ENOMEM; | |
982 | goto finish; | |
983 | } | |
984 | ||
985 | if ((r = safe_atou(args[i], &id)) < 0) { | |
986 | log_error("Failed to parse job id: %s", strerror(-r)); | |
987 | goto finish; | |
988 | } | |
989 | ||
990 | assert_cc(sizeof(uint32_t) == sizeof(id)); | |
991 | if (!dbus_message_append_args(m, | |
992 | DBUS_TYPE_UINT32, &id, | |
993 | DBUS_TYPE_INVALID)) { | |
994 | log_error("Could not append arguments to message."); | |
995 | r = -ENOMEM; | |
996 | goto finish; | |
997 | } | |
998 | ||
999 | if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) { | |
4cf5d675 | 1000 | log_error("Failed to issue method call: %s", bus_error_message(&error)); |
7e4249b9 LP |
1001 | r = -EIO; |
1002 | goto finish; | |
1003 | } | |
1004 | ||
1005 | if (!dbus_message_get_args(reply, &error, | |
1006 | DBUS_TYPE_OBJECT_PATH, &path, | |
1007 | DBUS_TYPE_INVALID)) { | |
4cf5d675 | 1008 | log_error("Failed to parse reply: %s", bus_error_message(&error)); |
7e4249b9 LP |
1009 | r = -EIO; |
1010 | goto finish; | |
1011 | } | |
1012 | ||
1013 | dbus_message_unref(m); | |
1014 | if (!(m = dbus_message_new_method_call( | |
1015 | "org.freedesktop.systemd1", | |
1016 | path, | |
1017 | "org.freedesktop.systemd1.Job", | |
1018 | "Cancel"))) { | |
1019 | log_error("Could not allocate message."); | |
1020 | r = -ENOMEM; | |
1021 | goto finish; | |
1022 | } | |
1023 | ||
1024 | dbus_message_unref(reply); | |
1025 | if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) { | |
4cf5d675 | 1026 | log_error("Failed to issue method call: %s", bus_error_message(&error)); |
7e4249b9 LP |
1027 | r = -EIO; |
1028 | goto finish; | |
1029 | } | |
1030 | ||
1031 | dbus_message_unref(m); | |
1032 | dbus_message_unref(reply); | |
1033 | m = reply = NULL; | |
1034 | } | |
1035 | ||
1036 | r = 0; | |
1037 | ||
1038 | finish: | |
1039 | if (m) | |
1040 | dbus_message_unref(m); | |
1041 | ||
1042 | if (reply) | |
1043 | dbus_message_unref(reply); | |
1044 | ||
1045 | dbus_error_free(&error); | |
1046 | ||
1047 | return r; | |
1048 | } | |
1049 | ||
ee5762e3 | 1050 | static bool need_daemon_reload(DBusConnection *bus, const char *unit) { |
e364ad06 | 1051 | DBusMessage *m = NULL, *reply = NULL; |
45fb0699 LP |
1052 | dbus_bool_t b = FALSE; |
1053 | DBusMessageIter iter, sub; | |
1054 | const char | |
1055 | *interface = "org.freedesktop.systemd1.Unit", | |
1056 | *property = "NeedDaemonReload", | |
1057 | *path; | |
1058 | ||
1059 | /* We ignore all errors here, since this is used to show a warning only */ | |
1060 | ||
1061 | if (!(m = dbus_message_new_method_call( | |
1062 | "org.freedesktop.systemd1", | |
1063 | "/org/freedesktop/systemd1", | |
1064 | "org.freedesktop.systemd1.Manager", | |
1065 | "GetUnit"))) | |
1066 | goto finish; | |
1067 | ||
1068 | if (!dbus_message_append_args(m, | |
1069 | DBUS_TYPE_STRING, &unit, | |
1070 | DBUS_TYPE_INVALID)) | |
1071 | goto finish; | |
1072 | ||
1073 | if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL))) | |
1074 | goto finish; | |
1075 | ||
1076 | if (!dbus_message_get_args(reply, NULL, | |
1077 | DBUS_TYPE_OBJECT_PATH, &path, | |
1078 | DBUS_TYPE_INVALID)) | |
1079 | goto finish; | |
1080 | ||
1081 | dbus_message_unref(m); | |
1082 | if (!(m = dbus_message_new_method_call( | |
1083 | "org.freedesktop.systemd1", | |
1084 | path, | |
1085 | "org.freedesktop.DBus.Properties", | |
1086 | "Get"))) | |
1087 | goto finish; | |
1088 | ||
1089 | if (!dbus_message_append_args(m, | |
1090 | DBUS_TYPE_STRING, &interface, | |
1091 | DBUS_TYPE_STRING, &property, | |
1092 | DBUS_TYPE_INVALID)) { | |
1093 | goto finish; | |
1094 | } | |
1095 | ||
1096 | dbus_message_unref(reply); | |
1097 | if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL))) | |
1098 | goto finish; | |
1099 | ||
1100 | if (!dbus_message_iter_init(reply, &iter) || | |
1101 | dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) | |
1102 | goto finish; | |
1103 | ||
1104 | dbus_message_iter_recurse(&iter, &sub); | |
1105 | ||
1106 | if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_BOOLEAN) | |
1107 | goto finish; | |
1108 | ||
1109 | dbus_message_iter_get_basic(&sub, &b); | |
1110 | ||
1111 | finish: | |
1112 | if (m) | |
1113 | dbus_message_unref(m); | |
1114 | ||
1115 | if (reply) | |
1116 | dbus_message_unref(reply); | |
1117 | ||
1118 | return b; | |
1119 | } | |
1120 | ||
5e374895 LP |
1121 | typedef struct WaitData { |
1122 | Set *set; | |
1123 | bool failed; | |
1124 | } WaitData; | |
1125 | ||
7e4249b9 LP |
1126 | static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *message, void *data) { |
1127 | DBusError error; | |
5e374895 | 1128 | WaitData *d = data; |
7e4249b9 LP |
1129 | |
1130 | assert(connection); | |
1131 | assert(message); | |
5e374895 | 1132 | assert(d); |
7e4249b9 LP |
1133 | |
1134 | dbus_error_init(&error); | |
1135 | ||
54165a39 LP |
1136 | log_debug("Got D-Bus request: %s.%s() on %s", |
1137 | dbus_message_get_interface(message), | |
1138 | dbus_message_get_member(message), | |
1139 | dbus_message_get_path(message)); | |
7e4249b9 LP |
1140 | |
1141 | if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) { | |
1142 | log_error("Warning! D-Bus connection terminated."); | |
1143 | dbus_connection_close(connection); | |
1144 | ||
1145 | } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) { | |
1146 | uint32_t id; | |
1147 | const char *path; | |
5e374895 | 1148 | dbus_bool_t success = true; |
7e4249b9 LP |
1149 | |
1150 | if (!dbus_message_get_args(message, &error, | |
1151 | DBUS_TYPE_UINT32, &id, | |
1152 | DBUS_TYPE_OBJECT_PATH, &path, | |
5e374895 | 1153 | DBUS_TYPE_BOOLEAN, &success, |
7e4249b9 | 1154 | DBUS_TYPE_INVALID)) |
4cf5d675 | 1155 | log_error("Failed to parse message: %s", bus_error_message(&error)); |
7e4249b9 LP |
1156 | else { |
1157 | char *p; | |
1158 | ||
5e374895 | 1159 | if ((p = set_remove(d->set, (char*) path))) |
7e4249b9 | 1160 | free(p); |
5e374895 LP |
1161 | |
1162 | if (!success) | |
1163 | d->failed = true; | |
7e4249b9 LP |
1164 | } |
1165 | } | |
1166 | ||
1167 | dbus_error_free(&error); | |
1168 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; | |
1169 | } | |
1170 | ||
479ef5d3 | 1171 | static int enable_wait_for_jobs(DBusConnection *bus) { |
7e4249b9 | 1172 | DBusError error; |
7e4249b9 LP |
1173 | |
1174 | assert(bus); | |
7e4249b9 | 1175 | |
f4579ce7 LP |
1176 | if (private_bus) |
1177 | return 0; | |
1178 | ||
7e4249b9 | 1179 | dbus_error_init(&error); |
7e4249b9 LP |
1180 | dbus_bus_add_match(bus, |
1181 | "type='signal'," | |
1182 | "sender='org.freedesktop.systemd1'," | |
1183 | "interface='org.freedesktop.systemd1.Manager'," | |
1184 | "member='JobRemoved'," | |
1185 | "path='/org/freedesktop/systemd1'", | |
1186 | &error); | |
1187 | ||
1188 | if (dbus_error_is_set(&error)) { | |
4cf5d675 | 1189 | log_error("Failed to add match: %s", bus_error_message(&error)); |
a567261a LP |
1190 | dbus_error_free(&error); |
1191 | return -EIO; | |
7e4249b9 LP |
1192 | } |
1193 | ||
479ef5d3 | 1194 | /* This is slightly dirty, since we don't undo the match registrations. */ |
a567261a | 1195 | return 0; |
7e4249b9 LP |
1196 | } |
1197 | ||
479ef5d3 LP |
1198 | static int wait_for_jobs(DBusConnection *bus, Set *s) { |
1199 | int r; | |
5e374895 | 1200 | WaitData d; |
479ef5d3 LP |
1201 | |
1202 | assert(bus); | |
1203 | assert(s); | |
1204 | ||
5e374895 LP |
1205 | zero(d); |
1206 | d.set = s; | |
1207 | d.failed = false; | |
1208 | ||
1209 | if (!dbus_connection_add_filter(bus, wait_filter, &d, NULL)) { | |
479ef5d3 LP |
1210 | log_error("Failed to add filter."); |
1211 | r = -ENOMEM; | |
1212 | goto finish; | |
1213 | } | |
1214 | ||
1215 | while (!set_isempty(s) && | |
1216 | dbus_connection_read_write_dispatch(bus, -1)) | |
1217 | ; | |
1218 | ||
5e374895 | 1219 | if (!arg_quiet && d.failed) |
da8f9f8c | 1220 | log_error("Job failed. See system logs and 'systemctl status' for details."); |
5e374895 LP |
1221 | |
1222 | r = d.failed ? -EIO : 0; | |
479ef5d3 LP |
1223 | |
1224 | finish: | |
1225 | /* This is slightly dirty, since we don't undo the filter registration. */ | |
1226 | ||
1227 | return r; | |
1228 | } | |
1229 | ||
e4b61340 LP |
1230 | static int start_unit_one( |
1231 | DBusConnection *bus, | |
1232 | const char *method, | |
1233 | const char *name, | |
1234 | const char *mode, | |
22f4096c | 1235 | DBusError *error, |
e4b61340 | 1236 | Set *s) { |
7e4249b9 | 1237 | |
7e4249b9 | 1238 | DBusMessage *m = NULL, *reply = NULL; |
45fb0699 | 1239 | const char *path; |
7e4249b9 | 1240 | int r; |
7e4249b9 | 1241 | |
e4b61340 LP |
1242 | assert(bus); |
1243 | assert(method); | |
1244 | assert(name); | |
1245 | assert(mode); | |
22f4096c | 1246 | assert(error); |
6e905d93 | 1247 | assert(arg_no_block || s); |
7e4249b9 LP |
1248 | |
1249 | if (!(m = dbus_message_new_method_call( | |
1250 | "org.freedesktop.systemd1", | |
1251 | "/org/freedesktop/systemd1", | |
1252 | "org.freedesktop.systemd1.Manager", | |
e4b61340 | 1253 | method))) { |
7e4249b9 LP |
1254 | log_error("Could not allocate message."); |
1255 | r = -ENOMEM; | |
1256 | goto finish; | |
1257 | } | |
1258 | ||
1259 | if (!dbus_message_append_args(m, | |
e4b61340 | 1260 | DBUS_TYPE_STRING, &name, |
7e4249b9 LP |
1261 | DBUS_TYPE_STRING, &mode, |
1262 | DBUS_TYPE_INVALID)) { | |
1263 | log_error("Could not append arguments to message."); | |
1264 | r = -ENOMEM; | |
1265 | goto finish; | |
1266 | } | |
1267 | ||
22f4096c | 1268 | if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, error))) { |
e4b61340 | 1269 | |
22f4096c | 1270 | if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(error)) { |
e4b61340 LP |
1271 | /* There's always a fallback possible for |
1272 | * legacy actions. */ | |
706900b7 | 1273 | r = -EADDRNOTAVAIL; |
e4b61340 LP |
1274 | goto finish; |
1275 | } | |
1276 | ||
22f4096c | 1277 | log_error("Failed to issue method call: %s", bus_error_message(error)); |
7e4249b9 LP |
1278 | r = -EIO; |
1279 | goto finish; | |
1280 | } | |
1281 | ||
22f4096c | 1282 | if (!dbus_message_get_args(reply, error, |
45fb0699 LP |
1283 | DBUS_TYPE_OBJECT_PATH, &path, |
1284 | DBUS_TYPE_INVALID)) { | |
22f4096c | 1285 | log_error("Failed to parse reply: %s", bus_error_message(error)); |
45fb0699 LP |
1286 | r = -EIO; |
1287 | goto finish; | |
1288 | } | |
1289 | ||
ee5762e3 | 1290 | if (need_daemon_reload(bus, name)) |
45fb0699 | 1291 | log_warning("Unit file of created job changed on disk, 'systemctl %s daemon-reload' recommended.", |
af2d49f7 | 1292 | arg_user ? "--user" : "--system"); |
45fb0699 | 1293 | |
6e905d93 | 1294 | if (!arg_no_block) { |
e4b61340 | 1295 | char *p; |
7e4249b9 | 1296 | |
7e4249b9 LP |
1297 | if (!(p = strdup(path))) { |
1298 | log_error("Failed to duplicate path."); | |
1299 | r = -ENOMEM; | |
1300 | goto finish; | |
1301 | } | |
1302 | ||
1303 | if ((r = set_put(s, p)) < 0) { | |
e4b61340 | 1304 | free(p); |
7e4249b9 LP |
1305 | log_error("Failed to add path to set."); |
1306 | goto finish; | |
1307 | } | |
e4b61340 | 1308 | } |
7e4249b9 | 1309 | |
706900b7 | 1310 | r = 0; |
22f4096c | 1311 | |
7e4249b9 | 1312 | finish: |
7e4249b9 LP |
1313 | if (m) |
1314 | dbus_message_unref(m); | |
1315 | ||
1316 | if (reply) | |
1317 | dbus_message_unref(reply); | |
1318 | ||
7e4249b9 LP |
1319 | return r; |
1320 | } | |
1321 | ||
514f4ef5 LP |
1322 | static enum action verb_to_action(const char *verb) { |
1323 | if (streq(verb, "halt")) | |
1324 | return ACTION_HALT; | |
1325 | else if (streq(verb, "poweroff")) | |
1326 | return ACTION_POWEROFF; | |
1327 | else if (streq(verb, "reboot")) | |
1328 | return ACTION_REBOOT; | |
20b09ca7 LP |
1329 | else if (streq(verb, "kexec")) |
1330 | return ACTION_KEXEC; | |
514f4ef5 LP |
1331 | else if (streq(verb, "rescue")) |
1332 | return ACTION_RESCUE; | |
1333 | else if (streq(verb, "emergency")) | |
1334 | return ACTION_EMERGENCY; | |
1335 | else if (streq(verb, "default")) | |
1336 | return ACTION_DEFAULT; | |
20b09ca7 LP |
1337 | else if (streq(verb, "exit")) |
1338 | return ACTION_EXIT; | |
514f4ef5 LP |
1339 | else |
1340 | return ACTION_INVALID; | |
1341 | } | |
1342 | ||
e4b61340 LP |
1343 | static int start_unit(DBusConnection *bus, char **args, unsigned n) { |
1344 | ||
1345 | static const char * const table[_ACTION_MAX] = { | |
514f4ef5 LP |
1346 | [ACTION_HALT] = SPECIAL_HALT_TARGET, |
1347 | [ACTION_POWEROFF] = SPECIAL_POWEROFF_TARGET, | |
1348 | [ACTION_REBOOT] = SPECIAL_REBOOT_TARGET, | |
20b09ca7 | 1349 | [ACTION_KEXEC] = SPECIAL_KEXEC_TARGET, |
514f4ef5 LP |
1350 | [ACTION_RUNLEVEL2] = SPECIAL_RUNLEVEL2_TARGET, |
1351 | [ACTION_RUNLEVEL3] = SPECIAL_RUNLEVEL3_TARGET, | |
1352 | [ACTION_RUNLEVEL4] = SPECIAL_RUNLEVEL4_TARGET, | |
1353 | [ACTION_RUNLEVEL5] = SPECIAL_RUNLEVEL5_TARGET, | |
1354 | [ACTION_RESCUE] = SPECIAL_RESCUE_TARGET, | |
f057408c | 1355 | [ACTION_EMERGENCY] = SPECIAL_EMERGENCY_TARGET, |
20b09ca7 LP |
1356 | [ACTION_DEFAULT] = SPECIAL_DEFAULT_TARGET, |
1357 | [ACTION_EXIT] = SPECIAL_EXIT_TARGET | |
e4b61340 LP |
1358 | }; |
1359 | ||
22f4096c | 1360 | int r, ret = 0; |
e4b61340 | 1361 | unsigned i; |
514f4ef5 | 1362 | const char *method, *mode, *one_name; |
e4b61340 | 1363 | Set *s = NULL; |
22f4096c LP |
1364 | DBusError error; |
1365 | ||
1366 | dbus_error_init(&error); | |
e4b61340 | 1367 | |
514f4ef5 LP |
1368 | assert(bus); |
1369 | ||
501fc174 LP |
1370 | spawn_ask_password_agent(); |
1371 | ||
e4b61340 LP |
1372 | if (arg_action == ACTION_SYSTEMCTL) { |
1373 | method = | |
6f28c033 LP |
1374 | streq(args[0], "stop") ? "StopUnit" : |
1375 | streq(args[0], "reload") ? "ReloadUnit" : | |
1376 | streq(args[0], "restart") ? "RestartUnit" : | |
aa5939a3 MS |
1377 | streq(args[0], "try-restart") || |
1378 | streq(args[0], "condrestart") ? "TryRestartUnit" : | |
6f28c033 | 1379 | streq(args[0], "reload-or-restart") ? "ReloadOrRestartUnit" : |
9d8a57ff | 1380 | streq(args[0], "reload-or-try-restart") || |
aa5939a3 | 1381 | streq(args[0], "force-reload") ? "ReloadOrTryRestartUnit" : |
6f28c033 | 1382 | "StartUnit"; |
e4b61340 LP |
1383 | |
1384 | mode = | |
514f4ef5 LP |
1385 | (streq(args[0], "isolate") || |
1386 | streq(args[0], "rescue") || | |
e67c3609 | 1387 | streq(args[0], "emergency")) ? "isolate" : arg_job_mode; |
e4b61340 | 1388 | |
514f4ef5 | 1389 | one_name = table[verb_to_action(args[0])]; |
e4b61340 | 1390 | |
e4b61340 LP |
1391 | } else { |
1392 | assert(arg_action < ELEMENTSOF(table)); | |
1393 | assert(table[arg_action]); | |
1394 | ||
1395 | method = "StartUnit"; | |
514f4ef5 LP |
1396 | |
1397 | mode = (arg_action == ACTION_EMERGENCY || | |
6f0d624e LP |
1398 | arg_action == ACTION_RESCUE || |
1399 | arg_action == ACTION_RUNLEVEL2 || | |
1400 | arg_action == ACTION_RUNLEVEL3 || | |
1401 | arg_action == ACTION_RUNLEVEL4 || | |
1402 | arg_action == ACTION_RUNLEVEL5) ? "isolate" : "replace"; | |
514f4ef5 LP |
1403 | |
1404 | one_name = table[arg_action]; | |
1405 | } | |
1406 | ||
6e905d93 | 1407 | if (!arg_no_block) { |
22f4096c LP |
1408 | if ((ret = enable_wait_for_jobs(bus)) < 0) { |
1409 | log_error("Could not watch jobs: %s", strerror(-ret)); | |
514f4ef5 LP |
1410 | goto finish; |
1411 | } | |
1412 | ||
1413 | if (!(s = set_new(string_hash_func, string_compare_func))) { | |
1414 | log_error("Failed to allocate set."); | |
22f4096c | 1415 | ret = -ENOMEM; |
514f4ef5 LP |
1416 | goto finish; |
1417 | } | |
e4b61340 LP |
1418 | } |
1419 | ||
514f4ef5 | 1420 | if (one_name) { |
22f4096c | 1421 | if ((ret = start_unit_one(bus, method, one_name, mode, &error, s)) <= 0) |
514f4ef5 LP |
1422 | goto finish; |
1423 | } else { | |
e4b61340 | 1424 | for (i = 1; i < n; i++) |
22f4096c | 1425 | if ((r = start_unit_one(bus, method, args[i], mode, &error, s)) != 0) { |
706900b7 | 1426 | ret = translate_bus_error_to_exit_status(r, &error); |
22f4096c LP |
1427 | dbus_error_free(&error); |
1428 | } | |
e4b61340 LP |
1429 | } |
1430 | ||
6e905d93 | 1431 | if (!arg_no_block) |
22f4096c LP |
1432 | if ((r = wait_for_jobs(bus, s)) < 0) { |
1433 | ret = r; | |
fbc43921 | 1434 | goto finish; |
22f4096c | 1435 | } |
514f4ef5 | 1436 | |
e4b61340 LP |
1437 | finish: |
1438 | if (s) | |
1439 | set_free_free(s); | |
1440 | ||
22f4096c LP |
1441 | dbus_error_free(&error); |
1442 | ||
1443 | return ret; | |
e4b61340 LP |
1444 | } |
1445 | ||
514f4ef5 | 1446 | static int start_special(DBusConnection *bus, char **args, unsigned n) { |
983d9c90 LP |
1447 | int r; |
1448 | ||
514f4ef5 LP |
1449 | assert(bus); |
1450 | assert(args); | |
1451 | ||
20b09ca7 LP |
1452 | if (arg_force && |
1453 | (streq(args[0], "halt") || | |
1454 | streq(args[0], "poweroff") || | |
1455 | streq(args[0], "reboot") || | |
1456 | streq(args[0], "kexec") || | |
1457 | streq(args[0], "exit"))) | |
1458 | return daemon_reload(bus, args, n); | |
1459 | ||
983d9c90 LP |
1460 | r = start_unit(bus, args, n); |
1461 | ||
1462 | if (r >= 0) | |
1463 | warn_wall(verb_to_action(args[0])); | |
514f4ef5 | 1464 | |
983d9c90 | 1465 | return r; |
514f4ef5 LP |
1466 | } |
1467 | ||
0183528f LP |
1468 | static int check_unit(DBusConnection *bus, char **args, unsigned n) { |
1469 | DBusMessage *m = NULL, *reply = NULL; | |
1470 | const char | |
1471 | *interface = "org.freedesktop.systemd1.Unit", | |
1472 | *property = "ActiveState"; | |
22f4096c | 1473 | int r = 3; /* According to LSB: "program is not running" */ |
0183528f LP |
1474 | DBusError error; |
1475 | unsigned i; | |
1476 | ||
1477 | assert(bus); | |
1478 | assert(args); | |
1479 | ||
1480 | dbus_error_init(&error); | |
1481 | ||
1482 | for (i = 1; i < n; i++) { | |
1483 | const char *path = NULL; | |
1484 | const char *state; | |
1485 | DBusMessageIter iter, sub; | |
1486 | ||
1487 | if (!(m = dbus_message_new_method_call( | |
1488 | "org.freedesktop.systemd1", | |
1489 | "/org/freedesktop/systemd1", | |
1490 | "org.freedesktop.systemd1.Manager", | |
1491 | "GetUnit"))) { | |
1492 | log_error("Could not allocate message."); | |
1493 | r = -ENOMEM; | |
1494 | goto finish; | |
1495 | } | |
1496 | ||
1497 | if (!dbus_message_append_args(m, | |
1498 | DBUS_TYPE_STRING, &args[i], | |
1499 | DBUS_TYPE_INVALID)) { | |
1500 | log_error("Could not append arguments to message."); | |
1501 | r = -ENOMEM; | |
1502 | goto finish; | |
1503 | } | |
1504 | ||
1505 | if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) { | |
1506 | ||
1507 | /* Hmm, cannot figure out anything about this unit... */ | |
1508 | if (!arg_quiet) | |
1509 | puts("unknown"); | |
1510 | ||
ed2d7a44 | 1511 | dbus_error_free(&error); |
8a0867d6 | 1512 | dbus_message_unref(m); |
0183528f LP |
1513 | continue; |
1514 | } | |
1515 | ||
1516 | if (!dbus_message_get_args(reply, &error, | |
1517 | DBUS_TYPE_OBJECT_PATH, &path, | |
1518 | DBUS_TYPE_INVALID)) { | |
4cf5d675 | 1519 | log_error("Failed to parse reply: %s", bus_error_message(&error)); |
0183528f LP |
1520 | r = -EIO; |
1521 | goto finish; | |
1522 | } | |
1523 | ||
1524 | dbus_message_unref(m); | |
1525 | if (!(m = dbus_message_new_method_call( | |
1526 | "org.freedesktop.systemd1", | |
1527 | path, | |
1528 | "org.freedesktop.DBus.Properties", | |
1529 | "Get"))) { | |
1530 | log_error("Could not allocate message."); | |
1531 | r = -ENOMEM; | |
1532 | goto finish; | |
1533 | } | |
1534 | ||
1535 | if (!dbus_message_append_args(m, | |
1536 | DBUS_TYPE_STRING, &interface, | |
1537 | DBUS_TYPE_STRING, &property, | |
1538 | DBUS_TYPE_INVALID)) { | |
1539 | log_error("Could not append arguments to message."); | |
1540 | r = -ENOMEM; | |
1541 | goto finish; | |
1542 | } | |
1543 | ||
1544 | dbus_message_unref(reply); | |
1545 | if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) { | |
4cf5d675 | 1546 | log_error("Failed to issue method call: %s", bus_error_message(&error)); |
0183528f LP |
1547 | r = -EIO; |
1548 | goto finish; | |
1549 | } | |
1550 | ||
1551 | if (!dbus_message_iter_init(reply, &iter) || | |
1552 | dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) { | |
1553 | log_error("Failed to parse reply."); | |
1554 | r = -EIO; | |
1555 | goto finish; | |
1556 | } | |
1557 | ||
1558 | dbus_message_iter_recurse(&iter, &sub); | |
1559 | ||
1560 | if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) { | |
1561 | log_error("Failed to parse reply."); | |
1562 | r = -EIO; | |
1563 | goto finish; | |
1564 | } | |
1565 | ||
1566 | dbus_message_iter_get_basic(&sub, &state); | |
1567 | ||
1568 | if (!arg_quiet) | |
1569 | puts(state); | |
1570 | ||
f1e36d67 | 1571 | if (streq(state, "active") || streq(state, "reloading")) |
0183528f LP |
1572 | r = 0; |
1573 | ||
1574 | dbus_message_unref(m); | |
1575 | dbus_message_unref(reply); | |
1576 | m = reply = NULL; | |
1577 | } | |
1578 | ||
1579 | finish: | |
1580 | if (m) | |
1581 | dbus_message_unref(m); | |
1582 | ||
1583 | if (reply) | |
1584 | dbus_message_unref(reply); | |
1585 | ||
1586 | dbus_error_free(&error); | |
1587 | ||
1588 | return r; | |
48220598 LP |
1589 | } |
1590 | ||
8a0867d6 LP |
1591 | static int kill_unit(DBusConnection *bus, char **args, unsigned n) { |
1592 | DBusMessage *m = NULL, *reply = NULL; | |
1593 | int r = 0; | |
1594 | DBusError error; | |
1595 | unsigned i; | |
1596 | ||
1597 | assert(bus); | |
1598 | assert(args); | |
1599 | ||
1600 | dbus_error_init(&error); | |
1601 | ||
1602 | if (!arg_kill_who) | |
1603 | arg_kill_who = "all"; | |
1604 | ||
1605 | if (!arg_kill_mode) | |
1606 | arg_kill_mode = streq(arg_kill_who, "all") ? "control-group" : "process"; | |
1607 | ||
1608 | for (i = 1; i < n; i++) { | |
1609 | ||
1610 | if (!(m = dbus_message_new_method_call( | |
1611 | "org.freedesktop.systemd1", | |
1612 | "/org/freedesktop/systemd1", | |
1613 | "org.freedesktop.systemd1.Manager", | |
1614 | "KillUnit"))) { | |
1615 | log_error("Could not allocate message."); | |
1616 | r = -ENOMEM; | |
1617 | goto finish; | |
1618 | } | |
1619 | ||
1620 | if (!dbus_message_append_args(m, | |
1621 | DBUS_TYPE_STRING, &args[i], | |
1622 | DBUS_TYPE_STRING, &arg_kill_who, | |
1623 | DBUS_TYPE_STRING, &arg_kill_mode, | |
1624 | DBUS_TYPE_INT32, &arg_signal, | |
1625 | DBUS_TYPE_INVALID)) { | |
1626 | log_error("Could not append arguments to message."); | |
1627 | r = -ENOMEM; | |
1628 | goto finish; | |
1629 | } | |
1630 | ||
1631 | if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) { | |
1632 | log_error("Failed to issue method call: %s", bus_error_message(&error)); | |
1633 | dbus_error_free(&error); | |
1634 | r = -EIO; | |
1635 | } | |
1636 | ||
1637 | dbus_message_unref(m); | |
1638 | ||
1639 | if (reply) | |
1640 | dbus_message_unref(reply); | |
1641 | m = reply = NULL; | |
1642 | } | |
1643 | ||
1644 | finish: | |
1645 | if (m) | |
1646 | dbus_message_unref(m); | |
1647 | ||
1648 | if (reply) | |
1649 | dbus_message_unref(reply); | |
1650 | ||
1651 | dbus_error_free(&error); | |
1652 | ||
1653 | return r; | |
1654 | } | |
1655 | ||
582a507f | 1656 | typedef struct ExecStatusInfo { |
0129173a LP |
1657 | char *name; |
1658 | ||
582a507f LP |
1659 | char *path; |
1660 | char **argv; | |
1661 | ||
b708e7ce LP |
1662 | bool ignore; |
1663 | ||
582a507f LP |
1664 | usec_t start_timestamp; |
1665 | usec_t exit_timestamp; | |
1666 | pid_t pid; | |
1667 | int code; | |
1668 | int status; | |
1669 | ||
1670 | LIST_FIELDS(struct ExecStatusInfo, exec); | |
1671 | } ExecStatusInfo; | |
1672 | ||
1673 | static void exec_status_info_free(ExecStatusInfo *i) { | |
1674 | assert(i); | |
1675 | ||
0129173a | 1676 | free(i->name); |
582a507f LP |
1677 | free(i->path); |
1678 | strv_free(i->argv); | |
1679 | free(i); | |
1680 | } | |
1681 | ||
1682 | static int exec_status_info_deserialize(DBusMessageIter *sub, ExecStatusInfo *i) { | |
1683 | uint64_t start_timestamp, exit_timestamp; | |
1684 | DBusMessageIter sub2, sub3; | |
1685 | const char*path; | |
1686 | unsigned n; | |
1687 | uint32_t pid; | |
1688 | int32_t code, status; | |
b708e7ce | 1689 | dbus_bool_t ignore; |
582a507f LP |
1690 | |
1691 | assert(i); | |
1692 | assert(i); | |
1693 | ||
1694 | if (dbus_message_iter_get_arg_type(sub) != DBUS_TYPE_STRUCT) | |
1695 | return -EIO; | |
1696 | ||
1697 | dbus_message_iter_recurse(sub, &sub2); | |
1698 | ||
1699 | if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0) | |
1700 | return -EIO; | |
1701 | ||
1702 | if (!(i->path = strdup(path))) | |
1703 | return -ENOMEM; | |
1704 | ||
1705 | if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_ARRAY || | |
1706 | dbus_message_iter_get_element_type(&sub2) != DBUS_TYPE_STRING) | |
1707 | return -EIO; | |
1708 | ||
1709 | n = 0; | |
1710 | dbus_message_iter_recurse(&sub2, &sub3); | |
1711 | while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) { | |
1712 | assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING); | |
1713 | dbus_message_iter_next(&sub3); | |
1714 | n++; | |
1715 | } | |
1716 | ||
1717 | ||
1718 | if (!(i->argv = new0(char*, n+1))) | |
1719 | return -ENOMEM; | |
1720 | ||
1721 | n = 0; | |
1722 | dbus_message_iter_recurse(&sub2, &sub3); | |
1723 | while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) { | |
1724 | const char *s; | |
1725 | ||
1726 | assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING); | |
1727 | dbus_message_iter_get_basic(&sub3, &s); | |
1728 | dbus_message_iter_next(&sub3); | |
1729 | ||
1730 | if (!(i->argv[n++] = strdup(s))) | |
1731 | return -ENOMEM; | |
1732 | } | |
1733 | ||
1734 | if (!dbus_message_iter_next(&sub2) || | |
b708e7ce | 1735 | bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &ignore, true) < 0 || |
582a507f LP |
1736 | bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_timestamp, true) < 0 || |
1737 | bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_timestamp, true) < 0 || | |
1738 | bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, true) < 0 || | |
1739 | bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &code, true) < 0 || | |
1740 | bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &status, false) < 0) | |
1741 | return -EIO; | |
1742 | ||
b708e7ce | 1743 | i->ignore = ignore; |
582a507f LP |
1744 | i->start_timestamp = (usec_t) start_timestamp; |
1745 | i->exit_timestamp = (usec_t) exit_timestamp; | |
1746 | i->pid = (pid_t) pid; | |
1747 | i->code = code; | |
1748 | i->status = status; | |
1749 | ||
1750 | return 0; | |
1751 | } | |
1752 | ||
61cbdc4b LP |
1753 | typedef struct UnitStatusInfo { |
1754 | const char *id; | |
1755 | const char *load_state; | |
1756 | const char *active_state; | |
1757 | const char *sub_state; | |
1758 | ||
1759 | const char *description; | |
4a9e2fff | 1760 | const char *following; |
61cbdc4b | 1761 | |
c31b4423 | 1762 | const char *path; |
61cbdc4b LP |
1763 | const char *default_control_group; |
1764 | ||
584be568 LP |
1765 | usec_t inactive_exit_timestamp; |
1766 | usec_t active_enter_timestamp; | |
1767 | usec_t active_exit_timestamp; | |
1768 | usec_t inactive_enter_timestamp; | |
1769 | ||
45fb0699 LP |
1770 | bool need_daemon_reload; |
1771 | ||
61cbdc4b LP |
1772 | /* Service */ |
1773 | pid_t main_pid; | |
1774 | pid_t control_pid; | |
1775 | const char *status_text; | |
d06dacd0 | 1776 | bool running:1; |
07459bb6 | 1777 | #ifdef HAVE_SYSV_COMPAT |
d06dacd0 | 1778 | bool is_sysv:1; |
07459bb6 | 1779 | #endif |
61cbdc4b LP |
1780 | |
1781 | usec_t start_timestamp; | |
1782 | usec_t exit_timestamp; | |
1783 | ||
1784 | int exit_code, exit_status; | |
1785 | ||
1786 | /* Socket */ | |
1787 | unsigned n_accepted; | |
1788 | unsigned n_connections; | |
b8131a87 | 1789 | bool accept; |
61cbdc4b LP |
1790 | |
1791 | /* Device */ | |
1792 | const char *sysfs_path; | |
1793 | ||
1794 | /* Mount, Automount */ | |
1795 | const char *where; | |
1796 | ||
1797 | /* Swap */ | |
1798 | const char *what; | |
582a507f LP |
1799 | |
1800 | LIST_HEAD(ExecStatusInfo, exec); | |
61cbdc4b LP |
1801 | } UnitStatusInfo; |
1802 | ||
1803 | static void print_status_info(UnitStatusInfo *i) { | |
582a507f | 1804 | ExecStatusInfo *p; |
2ee68f72 | 1805 | const char *on, *off, *ss; |
584be568 LP |
1806 | usec_t timestamp; |
1807 | char since1[FORMAT_TIMESTAMP_PRETTY_MAX], *s1; | |
1808 | char since2[FORMAT_TIMESTAMP_MAX], *s2; | |
582a507f | 1809 | |
61cbdc4b LP |
1810 | assert(i); |
1811 | ||
1812 | /* This shows pretty information about a unit. See | |
1813 | * print_property() for a low-level property printer */ | |
1814 | ||
1815 | printf("%s", strna(i->id)); | |
1816 | ||
1817 | if (i->description && !streq_ptr(i->id, i->description)) | |
1818 | printf(" - %s", i->description); | |
1819 | ||
1820 | printf("\n"); | |
1821 | ||
4a9e2fff LP |
1822 | if (i->following) |
1823 | printf("\t Follow: unit currently follows state of %s\n", i->following); | |
1824 | ||
00dc5d76 LP |
1825 | if (streq_ptr(i->load_state, "failed") || |
1826 | streq_ptr(i->load_state, "banned")) { | |
c31b4423 LP |
1827 | on = ansi_highlight(true); |
1828 | off = ansi_highlight(false); | |
1829 | } else | |
1830 | on = off = ""; | |
1831 | ||
1832 | if (i->path) | |
1833 | printf("\t Loaded: %s%s%s (%s)\n", on, strna(i->load_state), off, i->path); | |
61cbdc4b | 1834 | else |
c31b4423 | 1835 | printf("\t Loaded: %s%s%s\n", on, strna(i->load_state), off); |
61cbdc4b | 1836 | |
2ee68f72 LP |
1837 | ss = streq_ptr(i->active_state, i->sub_state) ? NULL : i->sub_state; |
1838 | ||
fdf20a31 | 1839 | if (streq_ptr(i->active_state, "failed")) { |
2ee68f72 LP |
1840 | on = ansi_highlight(true); |
1841 | off = ansi_highlight(false); | |
1842 | } else if (streq_ptr(i->active_state, "active") || streq_ptr(i->active_state, "reloading")) { | |
1843 | on = ansi_highlight_green(true); | |
1844 | off = ansi_highlight_green(false); | |
1845 | } else | |
1846 | on = off = ""; | |
1847 | ||
1848 | if (ss) | |
584be568 | 1849 | printf("\t Active: %s%s (%s)%s", |
2ee68f72 LP |
1850 | on, |
1851 | strna(i->active_state), | |
1852 | ss, | |
1853 | off); | |
1854 | else | |
584be568 | 1855 | printf("\t Active: %s%s%s", |
2ee68f72 LP |
1856 | on, |
1857 | strna(i->active_state), | |
1858 | off); | |
61cbdc4b | 1859 | |
584be568 LP |
1860 | timestamp = (streq_ptr(i->active_state, "active") || |
1861 | streq_ptr(i->active_state, "reloading")) ? i->active_enter_timestamp : | |
1862 | (streq_ptr(i->active_state, "inactive") || | |
fdf20a31 | 1863 | streq_ptr(i->active_state, "failed")) ? i->inactive_enter_timestamp : |
584be568 LP |
1864 | streq_ptr(i->active_state, "activating") ? i->inactive_exit_timestamp : |
1865 | i->active_exit_timestamp; | |
1866 | ||
1867 | s1 = format_timestamp_pretty(since1, sizeof(since1), timestamp); | |
1868 | s2 = format_timestamp(since2, sizeof(since2), timestamp); | |
1869 | ||
1870 | if (s1) | |
538da63d | 1871 | printf(" since %s; %s\n", s2, s1); |
584be568 | 1872 | else if (s2) |
538da63d | 1873 | printf(" since %s\n", s2); |
584be568 LP |
1874 | else |
1875 | printf("\n"); | |
1876 | ||
61cbdc4b LP |
1877 | if (i->sysfs_path) |
1878 | printf("\t Device: %s\n", i->sysfs_path); | |
9feeba4b | 1879 | if (i->where) |
61cbdc4b | 1880 | printf("\t Where: %s\n", i->where); |
9feeba4b | 1881 | if (i->what) |
61cbdc4b LP |
1882 | printf("\t What: %s\n", i->what); |
1883 | ||
b8131a87 | 1884 | if (i->accept) |
61cbdc4b LP |
1885 | printf("\tAccepted: %u; Connected: %u\n", i->n_accepted, i->n_connections); |
1886 | ||
582a507f LP |
1887 | LIST_FOREACH(exec, p, i->exec) { |
1888 | char *t; | |
9a57c629 | 1889 | bool good; |
582a507f LP |
1890 | |
1891 | /* Only show exited processes here */ | |
1892 | if (p->code == 0) | |
1893 | continue; | |
1894 | ||
1895 | t = strv_join(p->argv, " "); | |
9a57c629 | 1896 | printf("\t Process: %u %s=%s ", p->pid, p->name, strna(t)); |
582a507f LP |
1897 | free(t); |
1898 | ||
9a57c629 LP |
1899 | #ifdef HAVE_SYSV_COMPAT |
1900 | if (i->is_sysv) | |
1901 | good = is_clean_exit_lsb(p->code, p->status); | |
1902 | else | |
1903 | #endif | |
1904 | good = is_clean_exit(p->code, p->status); | |
1905 | ||
1906 | if (!good) { | |
1907 | on = ansi_highlight(true); | |
1908 | off = ansi_highlight(false); | |
1909 | } else | |
1910 | on = off = ""; | |
1911 | ||
1912 | printf("%s(code=%s, ", on, sigchld_code_to_string(p->code)); | |
1913 | ||
d06dacd0 LP |
1914 | if (p->code == CLD_EXITED) { |
1915 | const char *c; | |
1916 | ||
582a507f | 1917 | printf("status=%i", p->status); |
d06dacd0 | 1918 | |
07459bb6 | 1919 | #ifdef HAVE_SYSV_COMPAT |
d06dacd0 | 1920 | if ((c = exit_status_to_string(p->status, i->is_sysv ? EXIT_STATUS_LSB : EXIT_STATUS_SYSTEMD))) |
07459bb6 FF |
1921 | #else |
1922 | if ((c = exit_status_to_string(p->status, EXIT_STATUS_SYSTEMD))) | |
1923 | #endif | |
d06dacd0 LP |
1924 | printf("/%s", c); |
1925 | ||
1926 | } else | |
582a507f | 1927 | printf("signal=%s", signal_to_string(p->status)); |
9a57c629 LP |
1928 | |
1929 | printf(")%s\n", off); | |
1930 | ||
1931 | on = off = NULL; | |
582a507f LP |
1932 | |
1933 | if (i->main_pid == p->pid && | |
1934 | i->start_timestamp == p->start_timestamp && | |
1935 | i->exit_timestamp == p->start_timestamp) | |
1936 | /* Let's not show this twice */ | |
1937 | i->main_pid = 0; | |
1938 | ||
1939 | if (p->pid == i->control_pid) | |
1940 | i->control_pid = 0; | |
1941 | } | |
1942 | ||
61cbdc4b LP |
1943 | if (i->main_pid > 0 || i->control_pid > 0) { |
1944 | printf("\t"); | |
1945 | ||
1946 | if (i->main_pid > 0) { | |
f3d41013 | 1947 | printf("Main PID: %u", (unsigned) i->main_pid); |
61cbdc4b LP |
1948 | |
1949 | if (i->running) { | |
1950 | char *t = NULL; | |
1951 | get_process_name(i->main_pid, &t); | |
1952 | if (t) { | |
1953 | printf(" (%s)", t); | |
1954 | free(t); | |
1955 | } | |
6d4fc029 | 1956 | } else if (i->exit_code > 0) { |
61cbdc4b LP |
1957 | printf(" (code=%s, ", sigchld_code_to_string(i->exit_code)); |
1958 | ||
d06dacd0 LP |
1959 | if (i->exit_code == CLD_EXITED) { |
1960 | const char *c; | |
1961 | ||
61cbdc4b | 1962 | printf("status=%i", i->exit_status); |
d06dacd0 | 1963 | |
07459bb6 | 1964 | #ifdef HAVE_SYSV_COMPAT |
d06dacd0 | 1965 | if ((c = exit_status_to_string(i->exit_status, i->is_sysv ? EXIT_STATUS_LSB : EXIT_STATUS_SYSTEMD))) |
07459bb6 FF |
1966 | #else |
1967 | if ((c = exit_status_to_string(i->exit_status, EXIT_STATUS_SYSTEMD))) | |
1968 | #endif | |
d06dacd0 LP |
1969 | printf("/%s", c); |
1970 | ||
1971 | } else | |
582a507f | 1972 | printf("signal=%s", signal_to_string(i->exit_status)); |
6d4fc029 LP |
1973 | printf(")"); |
1974 | } | |
61cbdc4b LP |
1975 | } |
1976 | ||
1977 | if (i->main_pid > 0 && i->control_pid > 0) | |
1978 | printf(";"); | |
1979 | ||
1980 | if (i->control_pid > 0) { | |
1981 | char *t = NULL; | |
1982 | ||
1983 | printf(" Control: %u", (unsigned) i->control_pid); | |
1984 | ||
1985 | get_process_name(i->control_pid, &t); | |
1986 | if (t) { | |
1987 | printf(" (%s)", t); | |
1988 | free(t); | |
1989 | } | |
1990 | } | |
1991 | ||
1992 | printf("\n"); | |
1993 | } | |
1994 | ||
17bb7382 LP |
1995 | if (i->status_text) |
1996 | printf("\t Status: \"%s\"\n", i->status_text); | |
1997 | ||
c59760ee | 1998 | if (i->default_control_group) { |
ab35fb1b LP |
1999 | unsigned c; |
2000 | ||
61cbdc4b | 2001 | printf("\t CGroup: %s\n", i->default_control_group); |
ab35fb1b LP |
2002 | |
2003 | if ((c = columns()) > 18) | |
2004 | c -= 18; | |
2005 | else | |
2006 | c = 0; | |
2007 | ||
35d2e7ec | 2008 | show_cgroup_by_path(i->default_control_group, "\t\t ", c); |
c59760ee | 2009 | } |
45fb0699 LP |
2010 | |
2011 | if (i->need_daemon_reload) | |
2cc59dbf LP |
2012 | printf("\n%sWarning:%s Unit file changed on disk, 'systemctl %s daemon-reload' recommended.\n", |
2013 | ansi_highlight(true), | |
2014 | ansi_highlight(false), | |
af2d49f7 | 2015 | arg_user ? "--user" : "--system"); |
61cbdc4b LP |
2016 | } |
2017 | ||
2018 | static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) { | |
2019 | ||
2020 | switch (dbus_message_iter_get_arg_type(iter)) { | |
2021 | ||
2022 | case DBUS_TYPE_STRING: { | |
2023 | const char *s; | |
2024 | ||
2025 | dbus_message_iter_get_basic(iter, &s); | |
2026 | ||
2027 | if (s[0]) { | |
2028 | if (streq(name, "Id")) | |
2029 | i->id = s; | |
2030 | else if (streq(name, "LoadState")) | |
2031 | i->load_state = s; | |
2032 | else if (streq(name, "ActiveState")) | |
2033 | i->active_state = s; | |
2034 | else if (streq(name, "SubState")) | |
2035 | i->sub_state = s; | |
2036 | else if (streq(name, "Description")) | |
2037 | i->description = s; | |
2038 | else if (streq(name, "FragmentPath")) | |
c31b4423 | 2039 | i->path = s; |
07459bb6 | 2040 | #ifdef HAVE_SYSV_COMPAT |
d06dacd0 LP |
2041 | else if (streq(name, "SysVPath")) { |
2042 | i->is_sysv = true; | |
c31b4423 | 2043 | i->path = s; |
07459bb6 FF |
2044 | } |
2045 | #endif | |
2046 | else if (streq(name, "DefaultControlGroup")) | |
61cbdc4b LP |
2047 | i->default_control_group = s; |
2048 | else if (streq(name, "StatusText")) | |
2049 | i->status_text = s; | |
2050 | else if (streq(name, "SysFSPath")) | |
2051 | i->sysfs_path = s; | |
2052 | else if (streq(name, "Where")) | |
2053 | i->where = s; | |
2054 | else if (streq(name, "What")) | |
2055 | i->what = s; | |
4a9e2fff LP |
2056 | else if (streq(name, "Following")) |
2057 | i->following = s; | |
61cbdc4b LP |
2058 | } |
2059 | ||
2060 | break; | |
2061 | } | |
2062 | ||
b8131a87 LP |
2063 | case DBUS_TYPE_BOOLEAN: { |
2064 | dbus_bool_t b; | |
2065 | ||
2066 | dbus_message_iter_get_basic(iter, &b); | |
2067 | ||
2068 | if (streq(name, "Accept")) | |
2069 | i->accept = b; | |
45fb0699 LP |
2070 | else if (streq(name, "NeedDaemonReload")) |
2071 | i->need_daemon_reload = b; | |
b8131a87 LP |
2072 | |
2073 | break; | |
2074 | } | |
2075 | ||
61cbdc4b LP |
2076 | case DBUS_TYPE_UINT32: { |
2077 | uint32_t u; | |
2078 | ||
2079 | dbus_message_iter_get_basic(iter, &u); | |
2080 | ||
2081 | if (streq(name, "MainPID")) { | |
2082 | if (u > 0) { | |
2083 | i->main_pid = (pid_t) u; | |
2084 | i->running = true; | |
2085 | } | |
2086 | } else if (streq(name, "ControlPID")) | |
2087 | i->control_pid = (pid_t) u; | |
2088 | else if (streq(name, "ExecMainPID")) { | |
2089 | if (u > 0) | |
2090 | i->main_pid = (pid_t) u; | |
2091 | } else if (streq(name, "NAccepted")) | |
2092 | i->n_accepted = u; | |
2093 | else if (streq(name, "NConnections")) | |
2094 | i->n_connections = u; | |
2095 | ||
2096 | break; | |
2097 | } | |
2098 | ||
2099 | case DBUS_TYPE_INT32: { | |
2100 | int32_t j; | |
2101 | ||
2102 | dbus_message_iter_get_basic(iter, &j); | |
2103 | ||
2104 | if (streq(name, "ExecMainCode")) | |
2105 | i->exit_code = (int) j; | |
2106 | else if (streq(name, "ExecMainStatus")) | |
2107 | i->exit_status = (int) j; | |
2108 | ||
2109 | break; | |
2110 | } | |
2111 | ||
2112 | case DBUS_TYPE_UINT64: { | |
2113 | uint64_t u; | |
2114 | ||
2115 | dbus_message_iter_get_basic(iter, &u); | |
2116 | ||
2117 | if (streq(name, "ExecMainStartTimestamp")) | |
2118 | i->start_timestamp = (usec_t) u; | |
2119 | else if (streq(name, "ExecMainExitTimestamp")) | |
2120 | i->exit_timestamp = (usec_t) u; | |
584be568 LP |
2121 | else if (streq(name, "ActiveEnterTimestamp")) |
2122 | i->active_enter_timestamp = (usec_t) u; | |
2123 | else if (streq(name, "InactiveEnterTimestamp")) | |
2124 | i->inactive_enter_timestamp = (usec_t) u; | |
2125 | else if (streq(name, "InactiveExitTimestamp")) | |
2126 | i->inactive_exit_timestamp = (usec_t) u; | |
2127 | else if (streq(name, "ActiveExitTimestamp")) | |
2128 | i->active_exit_timestamp = (usec_t) u; | |
61cbdc4b LP |
2129 | |
2130 | break; | |
2131 | } | |
582a507f LP |
2132 | |
2133 | case DBUS_TYPE_ARRAY: { | |
2134 | ||
2135 | if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && | |
2136 | startswith(name, "Exec")) { | |
2137 | DBusMessageIter sub; | |
2138 | ||
2139 | dbus_message_iter_recurse(iter, &sub); | |
2140 | while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) { | |
2141 | ExecStatusInfo *info; | |
2142 | int r; | |
2143 | ||
2144 | if (!(info = new0(ExecStatusInfo, 1))) | |
2145 | return -ENOMEM; | |
2146 | ||
0129173a LP |
2147 | if (!(info->name = strdup(name))) { |
2148 | free(info); | |
2149 | return -ENOMEM; | |
2150 | } | |
2151 | ||
582a507f LP |
2152 | if ((r = exec_status_info_deserialize(&sub, info)) < 0) { |
2153 | free(info); | |
2154 | return r; | |
2155 | } | |
2156 | ||
2157 | LIST_PREPEND(ExecStatusInfo, exec, i->exec, info); | |
2158 | ||
2159 | dbus_message_iter_next(&sub); | |
2160 | } | |
2161 | } | |
2162 | ||
2163 | break; | |
2164 | } | |
61cbdc4b LP |
2165 | } |
2166 | ||
2167 | return 0; | |
2168 | } | |
2169 | ||
48220598 LP |
2170 | static int print_property(const char *name, DBusMessageIter *iter) { |
2171 | assert(name); | |
2172 | assert(iter); | |
2173 | ||
61cbdc4b LP |
2174 | /* This is a low-level property printer, see |
2175 | * print_status_info() for the nicer output */ | |
2176 | ||
ea4a240d | 2177 | if (arg_property && !strv_find(arg_property, name)) |
48220598 LP |
2178 | return 0; |
2179 | ||
2180 | switch (dbus_message_iter_get_arg_type(iter)) { | |
2181 | ||
2182 | case DBUS_TYPE_STRING: { | |
2183 | const char *s; | |
2184 | dbus_message_iter_get_basic(iter, &s); | |
2185 | ||
2186 | if (arg_all || s[0]) | |
2187 | printf("%s=%s\n", name, s); | |
2188 | ||
2189 | return 0; | |
2190 | } | |
2191 | ||
2192 | case DBUS_TYPE_BOOLEAN: { | |
2193 | dbus_bool_t b; | |
2194 | dbus_message_iter_get_basic(iter, &b); | |
2195 | printf("%s=%s\n", name, yes_no(b)); | |
2196 | ||
2197 | return 0; | |
2198 | } | |
2199 | ||
2200 | case DBUS_TYPE_UINT64: { | |
2201 | uint64_t u; | |
2202 | dbus_message_iter_get_basic(iter, &u); | |
2203 | ||
2204 | /* Yes, heuristics! But we can change this check | |
2205 | * should it turn out to not be sufficient */ | |
2206 | ||
14ad1d14 | 2207 | if (strstr(name, "Timestamp")) { |
48220598 LP |
2208 | char timestamp[FORMAT_TIMESTAMP_MAX], *t; |
2209 | ||
2210 | if ((t = format_timestamp(timestamp, sizeof(timestamp), u)) || arg_all) | |
2211 | printf("%s=%s\n", name, strempty(t)); | |
552e4331 LP |
2212 | } else if (strstr(name, "USec")) { |
2213 | char timespan[FORMAT_TIMESPAN_MAX]; | |
2214 | ||
2215 | printf("%s=%s\n", name, format_timespan(timespan, sizeof(timespan), u)); | |
48220598 LP |
2216 | } else |
2217 | printf("%s=%llu\n", name, (unsigned long long) u); | |
2218 | ||
2219 | return 0; | |
2220 | } | |
2221 | ||
2222 | case DBUS_TYPE_UINT32: { | |
2223 | uint32_t u; | |
2224 | dbus_message_iter_get_basic(iter, &u); | |
2225 | ||
5bd07073 | 2226 | if (strstr(name, "UMask") || strstr(name, "Mode")) |
48220598 LP |
2227 | printf("%s=%04o\n", name, u); |
2228 | else | |
2229 | printf("%s=%u\n", name, (unsigned) u); | |
2230 | ||
2231 | return 0; | |
2232 | } | |
2233 | ||
2234 | case DBUS_TYPE_INT32: { | |
2235 | int32_t i; | |
2236 | dbus_message_iter_get_basic(iter, &i); | |
2237 | ||
2238 | printf("%s=%i\n", name, (int) i); | |
2239 | return 0; | |
2240 | } | |
2241 | ||
05d6a3b6 LP |
2242 | case DBUS_TYPE_DOUBLE: { |
2243 | double d; | |
2244 | dbus_message_iter_get_basic(iter, &d); | |
2245 | ||
2246 | printf("%s=%g\n", name, d); | |
2247 | return 0; | |
2248 | } | |
2249 | ||
48220598 LP |
2250 | case DBUS_TYPE_STRUCT: { |
2251 | DBusMessageIter sub; | |
2252 | dbus_message_iter_recurse(iter, &sub); | |
2253 | ||
ebf57b80 | 2254 | if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) { |
48220598 LP |
2255 | uint32_t u; |
2256 | ||
2257 | dbus_message_iter_get_basic(&sub, &u); | |
2258 | ||
2259 | if (u) | |
2260 | printf("%s=%u\n", name, (unsigned) u); | |
2261 | else if (arg_all) | |
2262 | printf("%s=\n", name); | |
2263 | ||
2264 | return 0; | |
ebf57b80 | 2265 | } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) { |
48220598 LP |
2266 | const char *s; |
2267 | ||
2268 | dbus_message_iter_get_basic(&sub, &s); | |
2269 | ||
2270 | if (arg_all || s[0]) | |
2271 | printf("%s=%s\n", name, s); | |
2272 | ||
2273 | return 0; | |
2274 | } | |
2275 | ||
2276 | break; | |
2277 | } | |
2278 | ||
2279 | case DBUS_TYPE_ARRAY: | |
2280 | ||
2281 | if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) { | |
2282 | DBusMessageIter sub; | |
2283 | bool space = false; | |
2284 | ||
2285 | dbus_message_iter_recurse(iter, &sub); | |
48220598 LP |
2286 | if (arg_all || |
2287 | dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) { | |
2288 | printf("%s=", name); | |
2289 | ||
2290 | while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) { | |
2291 | const char *s; | |
2292 | ||
2293 | assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING); | |
2294 | dbus_message_iter_get_basic(&sub, &s); | |
2295 | printf("%s%s", space ? " " : "", s); | |
2296 | ||
2297 | space = true; | |
2298 | dbus_message_iter_next(&sub); | |
2299 | } | |
2300 | ||
2301 | puts(""); | |
2302 | } | |
2303 | ||
ebf57b80 | 2304 | return 0; |
582a507f | 2305 | |
82c121a4 LP |
2306 | } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_BYTE) { |
2307 | DBusMessageIter sub; | |
2308 | ||
2309 | dbus_message_iter_recurse(iter, &sub); | |
82c121a4 LP |
2310 | if (arg_all || |
2311 | dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) { | |
2312 | printf("%s=", name); | |
2313 | ||
2314 | while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) { | |
2315 | uint8_t u; | |
ebf57b80 | 2316 | |
82c121a4 LP |
2317 | assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_BYTE); |
2318 | dbus_message_iter_get_basic(&sub, &u); | |
2319 | printf("%02x", u); | |
2320 | ||
2321 | dbus_message_iter_next(&sub); | |
2322 | } | |
2323 | ||
2324 | puts(""); | |
2325 | } | |
2326 | ||
2327 | return 0; | |
582a507f | 2328 | |
ebf57b80 LP |
2329 | } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) { |
2330 | DBusMessageIter sub, sub2; | |
2331 | ||
2332 | dbus_message_iter_recurse(iter, &sub); | |
ebf57b80 LP |
2333 | while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) { |
2334 | const char *type, *path; | |
2335 | ||
2336 | dbus_message_iter_recurse(&sub, &sub2); | |
2337 | ||
2338 | if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 && | |
2339 | bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0) | |
2340 | printf("%s=%s\n", type, path); | |
2341 | ||
2342 | dbus_message_iter_next(&sub); | |
2343 | } | |
2344 | ||
707e5e52 | 2345 | return 0; |
582a507f | 2346 | |
707e5e52 LP |
2347 | } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) { |
2348 | DBusMessageIter sub, sub2; | |
2349 | ||
2350 | dbus_message_iter_recurse(iter, &sub); | |
707e5e52 LP |
2351 | while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) { |
2352 | const char *base; | |
2353 | uint64_t value, next_elapse; | |
2354 | ||
2355 | dbus_message_iter_recurse(&sub, &sub2); | |
2356 | ||
2357 | if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 && | |
2358 | bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 && | |
552e4331 LP |
2359 | bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) { |
2360 | char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX]; | |
2361 | ||
2362 | printf("%s={ value=%s ; next_elapse=%s }\n", | |
fe68089d | 2363 | base, |
552e4331 LP |
2364 | format_timespan(timespan1, sizeof(timespan1), value), |
2365 | format_timespan(timespan2, sizeof(timespan2), next_elapse)); | |
2366 | } | |
fe68089d LP |
2367 | |
2368 | dbus_message_iter_next(&sub); | |
2369 | } | |
2370 | ||
2371 | return 0; | |
fe68089d | 2372 | |
582a507f LP |
2373 | } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) { |
2374 | DBusMessageIter sub; | |
fe68089d LP |
2375 | |
2376 | dbus_message_iter_recurse(iter, &sub); | |
fe68089d | 2377 | while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) { |
582a507f | 2378 | ExecStatusInfo info; |
fe68089d | 2379 | |
582a507f LP |
2380 | zero(info); |
2381 | if (exec_status_info_deserialize(&sub, &info) >= 0) { | |
fe68089d | 2382 | char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX]; |
582a507f LP |
2383 | char *t; |
2384 | ||
2385 | t = strv_join(info.argv, " "); | |
2386 | ||
b708e7ce | 2387 | printf("%s={ path=%s ; argv[]=%s ; ignore=%s ; start_time=[%s] ; stop_time=[%s] ; pid=%u ; code=%s ; status=%i%s%s }\n", |
582a507f LP |
2388 | name, |
2389 | strna(info.path), | |
2390 | strna(t), | |
b708e7ce | 2391 | yes_no(info.ignore), |
582a507f LP |
2392 | strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)), |
2393 | strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)), | |
2394 | (unsigned) info. pid, | |
2395 | sigchld_code_to_string(info.code), | |
2396 | info.status, | |
2397 | info.code == CLD_EXITED ? "" : "/", | |
2398 | strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status))); | |
fe68089d | 2399 | |
582a507f | 2400 | free(t); |
fe68089d LP |
2401 | } |
2402 | ||
582a507f LP |
2403 | free(info.path); |
2404 | strv_free(info.argv); | |
707e5e52 LP |
2405 | |
2406 | dbus_message_iter_next(&sub); | |
2407 | } | |
2408 | ||
48220598 LP |
2409 | return 0; |
2410 | } | |
2411 | ||
2412 | break; | |
2413 | } | |
2414 | ||
2415 | if (arg_all) | |
2416 | printf("%s=[unprintable]\n", name); | |
2417 | ||
2418 | return 0; | |
2419 | } | |
2420 | ||
be8088a2 | 2421 | static int show_one(const char *verb, DBusConnection *bus, const char *path, bool show_properties, bool *new_line) { |
48220598 LP |
2422 | DBusMessage *m = NULL, *reply = NULL; |
2423 | const char *interface = ""; | |
2424 | int r; | |
2425 | DBusError error; | |
2426 | DBusMessageIter iter, sub, sub2, sub3; | |
61cbdc4b | 2427 | UnitStatusInfo info; |
582a507f | 2428 | ExecStatusInfo *p; |
48220598 LP |
2429 | |
2430 | assert(bus); | |
2431 | assert(path); | |
61cbdc4b | 2432 | assert(new_line); |
48220598 | 2433 | |
61cbdc4b | 2434 | zero(info); |
48220598 LP |
2435 | dbus_error_init(&error); |
2436 | ||
2437 | if (!(m = dbus_message_new_method_call( | |
2438 | "org.freedesktop.systemd1", | |
2439 | path, | |
2440 | "org.freedesktop.DBus.Properties", | |
2441 | "GetAll"))) { | |
2442 | log_error("Could not allocate message."); | |
2443 | r = -ENOMEM; | |
2444 | goto finish; | |
2445 | } | |
2446 | ||
2447 | if (!dbus_message_append_args(m, | |
2448 | DBUS_TYPE_STRING, &interface, | |
2449 | DBUS_TYPE_INVALID)) { | |
2450 | log_error("Could not append arguments to message."); | |
2451 | r = -ENOMEM; | |
2452 | goto finish; | |
2453 | } | |
2454 | ||
2455 | if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) { | |
4cf5d675 | 2456 | log_error("Failed to issue method call: %s", bus_error_message(&error)); |
48220598 LP |
2457 | r = -EIO; |
2458 | goto finish; | |
2459 | } | |
2460 | ||
2461 | if (!dbus_message_iter_init(reply, &iter) || | |
2462 | dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY || | |
2463 | dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) { | |
2464 | log_error("Failed to parse reply."); | |
2465 | r = -EIO; | |
2466 | goto finish; | |
2467 | } | |
2468 | ||
2469 | dbus_message_iter_recurse(&iter, &sub); | |
2470 | ||
61cbdc4b LP |
2471 | if (*new_line) |
2472 | printf("\n"); | |
2473 | ||
2474 | *new_line = true; | |
2475 | ||
48220598 LP |
2476 | while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) { |
2477 | const char *name; | |
2478 | ||
2479 | if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) { | |
2480 | log_error("Failed to parse reply."); | |
2481 | r = -EIO; | |
2482 | goto finish; | |
2483 | } | |
2484 | ||
2485 | dbus_message_iter_recurse(&sub, &sub2); | |
0183528f | 2486 | |
48220598 LP |
2487 | if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) { |
2488 | log_error("Failed to parse reply."); | |
2489 | r = -EIO; | |
2490 | goto finish; | |
2491 | } | |
2492 | ||
2493 | if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) { | |
2494 | log_error("Failed to parse reply."); | |
2495 | r = -EIO; | |
2496 | goto finish; | |
2497 | } | |
2498 | ||
2499 | dbus_message_iter_recurse(&sub2, &sub3); | |
2500 | ||
61cbdc4b LP |
2501 | if (show_properties) |
2502 | r = print_property(name, &sub3); | |
2503 | else | |
2504 | r = status_property(name, &sub3, &info); | |
2505 | ||
2506 | if (r < 0) { | |
48220598 LP |
2507 | log_error("Failed to parse reply."); |
2508 | r = -EIO; | |
2509 | goto finish; | |
2510 | } | |
2511 | ||
2512 | dbus_message_iter_next(&sub); | |
2513 | } | |
2514 | ||
f1e36d67 LP |
2515 | r = 0; |
2516 | ||
22f4096c LP |
2517 | if (!show_properties) |
2518 | print_status_info(&info); | |
f1e36d67 | 2519 | |
22f4096c | 2520 | if (!streq_ptr(info.active_state, "active") && |
be8088a2 LP |
2521 | !streq_ptr(info.active_state, "reloading") && |
2522 | streq(verb, "status")) | |
22f4096c LP |
2523 | /* According to LSB: "program not running" */ |
2524 | r = 3; | |
61cbdc4b | 2525 | |
582a507f LP |
2526 | while ((p = info.exec)) { |
2527 | LIST_REMOVE(ExecStatusInfo, exec, info.exec, p); | |
2528 | exec_status_info_free(p); | |
2529 | } | |
2530 | ||
48220598 LP |
2531 | finish: |
2532 | if (m) | |
2533 | dbus_message_unref(m); | |
2534 | ||
2535 | if (reply) | |
2536 | dbus_message_unref(reply); | |
2537 | ||
2538 | dbus_error_free(&error); | |
2539 | ||
2540 | return r; | |
2541 | } | |
2542 | ||
2543 | static int show(DBusConnection *bus, char **args, unsigned n) { | |
2544 | DBusMessage *m = NULL, *reply = NULL; | |
22f4096c | 2545 | int r, ret = 0; |
48220598 LP |
2546 | DBusError error; |
2547 | unsigned i; | |
61cbdc4b | 2548 | bool show_properties, new_line = false; |
48220598 LP |
2549 | |
2550 | assert(bus); | |
2551 | assert(args); | |
2552 | ||
2553 | dbus_error_init(&error); | |
2554 | ||
61cbdc4b LP |
2555 | show_properties = !streq(args[0], "status"); |
2556 | ||
ec14911e LP |
2557 | if (show_properties) |
2558 | pager_open(); | |
2559 | ||
61cbdc4b | 2560 | if (show_properties && n <= 1) { |
48220598 LP |
2561 | /* If not argument is specified inspect the manager |
2562 | * itself */ | |
2563 | ||
be8088a2 | 2564 | ret = show_one(args[0], bus, "/org/freedesktop/systemd1", show_properties, &new_line); |
48220598 LP |
2565 | goto finish; |
2566 | } | |
2567 | ||
2568 | for (i = 1; i < n; i++) { | |
2569 | const char *path = NULL; | |
2570 | uint32_t id; | |
2571 | ||
598b557b LP |
2572 | if (safe_atou32(args[i], &id) < 0) { |
2573 | ||
2574 | /* Interpret as unit name */ | |
48220598 LP |
2575 | |
2576 | if (!(m = dbus_message_new_method_call( | |
2577 | "org.freedesktop.systemd1", | |
2578 | "/org/freedesktop/systemd1", | |
2579 | "org.freedesktop.systemd1.Manager", | |
e87d1818 | 2580 | "LoadUnit"))) { |
48220598 | 2581 | log_error("Could not allocate message."); |
22f4096c | 2582 | ret = -ENOMEM; |
48220598 LP |
2583 | goto finish; |
2584 | } | |
2585 | ||
2586 | if (!dbus_message_append_args(m, | |
2587 | DBUS_TYPE_STRING, &args[i], | |
2588 | DBUS_TYPE_INVALID)) { | |
2589 | log_error("Could not append arguments to message."); | |
22f4096c | 2590 | ret = -ENOMEM; |
48220598 LP |
2591 | goto finish; |
2592 | } | |
2593 | ||
ed2d7a44 LP |
2594 | if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) { |
2595 | ||
2596 | if (!dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED)) { | |
4cf5d675 | 2597 | log_error("Failed to issue method call: %s", bus_error_message(&error)); |
22f4096c | 2598 | ret = -EIO; |
ed2d7a44 LP |
2599 | goto finish; |
2600 | } | |
2601 | ||
2602 | dbus_error_free(&error); | |
2603 | ||
2604 | dbus_message_unref(m); | |
2605 | if (!(m = dbus_message_new_method_call( | |
2606 | "org.freedesktop.systemd1", | |
2607 | "/org/freedesktop/systemd1", | |
2608 | "org.freedesktop.systemd1.Manager", | |
2609 | "GetUnit"))) { | |
2610 | log_error("Could not allocate message."); | |
22f4096c | 2611 | ret = -ENOMEM; |
ed2d7a44 LP |
2612 | goto finish; |
2613 | } | |
2614 | ||
2615 | if (!dbus_message_append_args(m, | |
2616 | DBUS_TYPE_STRING, &args[i], | |
2617 | DBUS_TYPE_INVALID)) { | |
2618 | log_error("Could not append arguments to message."); | |
22f4096c | 2619 | ret = -ENOMEM; |
ed2d7a44 LP |
2620 | goto finish; |
2621 | } | |
2622 | ||
2623 | if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) { | |
4cf5d675 | 2624 | log_error("Failed to issue method call: %s", bus_error_message(&error)); |
22f4096c LP |
2625 | |
2626 | if (dbus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT)) | |
2627 | ret = 4; /* According to LSB: "program or service status is unknown" */ | |
2628 | else | |
2629 | ret = -EIO; | |
ed2d7a44 LP |
2630 | goto finish; |
2631 | } | |
2632 | } | |
2633 | ||
598b557b LP |
2634 | } else if (show_properties) { |
2635 | ||
2636 | /* Interpret as job id */ | |
48220598 LP |
2637 | |
2638 | if (!(m = dbus_message_new_method_call( | |
2639 | "org.freedesktop.systemd1", | |
2640 | "/org/freedesktop/systemd1", | |
2641 | "org.freedesktop.systemd1.Manager", | |
2642 | "GetJob"))) { | |
2643 | log_error("Could not allocate message."); | |
22f4096c | 2644 | ret = -ENOMEM; |
48220598 LP |
2645 | goto finish; |
2646 | } | |
2647 | ||
2648 | if (!dbus_message_append_args(m, | |
2649 | DBUS_TYPE_UINT32, &id, | |
2650 | DBUS_TYPE_INVALID)) { | |
2651 | log_error("Could not append arguments to message."); | |
22f4096c | 2652 | ret = -ENOMEM; |
48220598 LP |
2653 | goto finish; |
2654 | } | |
48220598 | 2655 | |
598b557b | 2656 | if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) { |
4cf5d675 | 2657 | log_error("Failed to issue method call: %s", bus_error_message(&error)); |
22f4096c | 2658 | ret = -EIO; |
598b557b LP |
2659 | goto finish; |
2660 | } | |
2661 | } else { | |
2662 | ||
2663 | /* Interpret as PID */ | |
2664 | ||
2665 | if (!(m = dbus_message_new_method_call( | |
2666 | "org.freedesktop.systemd1", | |
2667 | "/org/freedesktop/systemd1", | |
2668 | "org.freedesktop.systemd1.Manager", | |
2669 | "GetUnitByPID"))) { | |
2670 | log_error("Could not allocate message."); | |
22f4096c | 2671 | ret = -ENOMEM; |
598b557b LP |
2672 | goto finish; |
2673 | } | |
2674 | ||
2675 | if (!dbus_message_append_args(m, | |
2676 | DBUS_TYPE_UINT32, &id, | |
2677 | DBUS_TYPE_INVALID)) { | |
2678 | log_error("Could not append arguments to message."); | |
22f4096c | 2679 | ret = -ENOMEM; |
598b557b LP |
2680 | goto finish; |
2681 | } | |
2682 | ||
ed2d7a44 | 2683 | if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) { |
4cf5d675 | 2684 | log_error("Failed to issue method call: %s", bus_error_message(&error)); |
22f4096c | 2685 | ret = -EIO; |
ed2d7a44 LP |
2686 | goto finish; |
2687 | } | |
48220598 LP |
2688 | } |
2689 | ||
2690 | if (!dbus_message_get_args(reply, &error, | |
2691 | DBUS_TYPE_OBJECT_PATH, &path, | |
2692 | DBUS_TYPE_INVALID)) { | |
4cf5d675 | 2693 | log_error("Failed to parse reply: %s", bus_error_message(&error)); |
22f4096c | 2694 | ret = -EIO; |
48220598 LP |
2695 | goto finish; |
2696 | } | |
2697 | ||
be8088a2 | 2698 | if ((r = show_one(args[0], bus, path, show_properties, &new_line)) != 0) |
22f4096c | 2699 | ret = r; |
48220598 LP |
2700 | |
2701 | dbus_message_unref(m); | |
2702 | dbus_message_unref(reply); | |
2703 | m = reply = NULL; | |
2704 | } | |
2705 | ||
48220598 LP |
2706 | finish: |
2707 | if (m) | |
2708 | dbus_message_unref(m); | |
2709 | ||
2710 | if (reply) | |
2711 | dbus_message_unref(reply); | |
2712 | ||
2713 | dbus_error_free(&error); | |
2714 | ||
22f4096c | 2715 | return ret; |
0183528f LP |
2716 | } |
2717 | ||
7e4249b9 LP |
2718 | static DBusHandlerResult monitor_filter(DBusConnection *connection, DBusMessage *message, void *data) { |
2719 | DBusError error; | |
2720 | DBusMessage *m = NULL, *reply = NULL; | |
2721 | ||
2722 | assert(connection); | |
2723 | assert(message); | |
2724 | ||
2725 | dbus_error_init(&error); | |
2726 | ||
54165a39 LP |
2727 | log_debug("Got D-Bus request: %s.%s() on %s", |
2728 | dbus_message_get_interface(message), | |
2729 | dbus_message_get_member(message), | |
2730 | dbus_message_get_path(message)); | |
7e4249b9 LP |
2731 | |
2732 | if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) { | |
2733 | log_error("Warning! D-Bus connection terminated."); | |
2734 | dbus_connection_close(connection); | |
2735 | ||
2736 | } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitNew") || | |
2737 | dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitRemoved")) { | |
2738 | const char *id, *path; | |
2739 | ||
2740 | if (!dbus_message_get_args(message, &error, | |
2741 | DBUS_TYPE_STRING, &id, | |
2742 | DBUS_TYPE_OBJECT_PATH, &path, | |
2743 | DBUS_TYPE_INVALID)) | |
4cf5d675 | 2744 | log_error("Failed to parse message: %s", bus_error_message(&error)); |
7e4249b9 LP |
2745 | else if (streq(dbus_message_get_member(message), "UnitNew")) |
2746 | printf("Unit %s added.\n", id); | |
2747 | else | |
2748 | printf("Unit %s removed.\n", id); | |
2749 | ||
2750 | } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobNew") || | |
2751 | dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) { | |
2752 | uint32_t id; | |
2753 | const char *path; | |
2754 | ||
2755 | if (!dbus_message_get_args(message, &error, | |
2756 | DBUS_TYPE_UINT32, &id, | |
2757 | DBUS_TYPE_OBJECT_PATH, &path, | |
2758 | DBUS_TYPE_INVALID)) | |
4cf5d675 | 2759 | log_error("Failed to parse message: %s", bus_error_message(&error)); |
7e4249b9 LP |
2760 | else if (streq(dbus_message_get_member(message), "JobNew")) |
2761 | printf("Job %u added.\n", id); | |
2762 | else | |
2763 | printf("Job %u removed.\n", id); | |
2764 | ||
2765 | ||
c4e2ceae | 2766 | } else if (dbus_message_is_signal(message, "org.freedesktop.DBus.Properties", "PropertiesChanged")) { |
7e4249b9 LP |
2767 | |
2768 | const char *path, *interface, *property = "Id"; | |
2769 | DBusMessageIter iter, sub; | |
2770 | ||
2771 | path = dbus_message_get_path(message); | |
c4e2ceae LP |
2772 | |
2773 | if (!dbus_message_get_args(message, &error, | |
2774 | DBUS_TYPE_STRING, &interface, | |
2775 | DBUS_TYPE_INVALID)) { | |
4cf5d675 | 2776 | log_error("Failed to parse message: %s", bus_error_message(&error)); |
c4e2ceae LP |
2777 | goto finish; |
2778 | } | |
2779 | ||
2780 | if (!streq(interface, "org.freedesktop.systemd1.Job") && | |
2781 | !streq(interface, "org.freedesktop.systemd1.Unit")) | |
2782 | goto finish; | |
7e4249b9 LP |
2783 | |
2784 | if (!(m = dbus_message_new_method_call( | |
2785 | "org.freedesktop.systemd1", | |
2786 | path, | |
2787 | "org.freedesktop.DBus.Properties", | |
2788 | "Get"))) { | |
2789 | log_error("Could not allocate message."); | |
2790 | goto oom; | |
2791 | } | |
2792 | ||
2793 | if (!dbus_message_append_args(m, | |
2794 | DBUS_TYPE_STRING, &interface, | |
2795 | DBUS_TYPE_STRING, &property, | |
2796 | DBUS_TYPE_INVALID)) { | |
2797 | log_error("Could not append arguments to message."); | |
2798 | goto finish; | |
2799 | } | |
2800 | ||
2801 | if (!(reply = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) { | |
4cf5d675 | 2802 | log_error("Failed to issue method call: %s", bus_error_message(&error)); |
7e4249b9 LP |
2803 | goto finish; |
2804 | } | |
2805 | ||
2806 | if (!dbus_message_iter_init(reply, &iter) || | |
2807 | dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) { | |
2808 | log_error("Failed to parse reply."); | |
2809 | goto finish; | |
2810 | } | |
2811 | ||
2812 | dbus_message_iter_recurse(&iter, &sub); | |
2813 | ||
2814 | if (streq(interface, "org.freedesktop.systemd1.Unit")) { | |
2815 | const char *id; | |
2816 | ||
2817 | if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) { | |
2818 | log_error("Failed to parse reply."); | |
2819 | goto finish; | |
2820 | } | |
2821 | ||
2822 | dbus_message_iter_get_basic(&sub, &id); | |
2823 | printf("Unit %s changed.\n", id); | |
2824 | } else { | |
2825 | uint32_t id; | |
2826 | ||
2827 | if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT32) { | |
2828 | log_error("Failed to parse reply."); | |
2829 | goto finish; | |
2830 | } | |
2831 | ||
2832 | dbus_message_iter_get_basic(&sub, &id); | |
2833 | printf("Job %u changed.\n", id); | |
2834 | } | |
2835 | } | |
2836 | ||
2837 | finish: | |
2838 | if (m) | |
2839 | dbus_message_unref(m); | |
2840 | ||
2841 | if (reply) | |
2842 | dbus_message_unref(reply); | |
2843 | ||
2844 | dbus_error_free(&error); | |
2845 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; | |
2846 | ||
2847 | oom: | |
2848 | if (m) | |
2849 | dbus_message_unref(m); | |
2850 | ||
2851 | if (reply) | |
2852 | dbus_message_unref(reply); | |
2853 | ||
2854 | dbus_error_free(&error); | |
2855 | return DBUS_HANDLER_RESULT_NEED_MEMORY; | |
2856 | } | |
2857 | ||
2858 | static int monitor(DBusConnection *bus, char **args, unsigned n) { | |
2859 | DBusMessage *m = NULL, *reply = NULL; | |
2860 | DBusError error; | |
2861 | int r; | |
2862 | ||
2863 | dbus_error_init(&error); | |
2864 | ||
f4579ce7 LP |
2865 | if (!private_bus) { |
2866 | dbus_bus_add_match(bus, | |
2867 | "type='signal'," | |
2868 | "sender='org.freedesktop.systemd1'," | |
2869 | "interface='org.freedesktop.systemd1.Manager'," | |
2870 | "path='/org/freedesktop/systemd1'", | |
2871 | &error); | |
7e4249b9 | 2872 | |
f4579ce7 | 2873 | if (dbus_error_is_set(&error)) { |
4cf5d675 | 2874 | log_error("Failed to add match: %s", bus_error_message(&error)); |
f4579ce7 LP |
2875 | r = -EIO; |
2876 | goto finish; | |
2877 | } | |
7e4249b9 | 2878 | |
f4579ce7 LP |
2879 | dbus_bus_add_match(bus, |
2880 | "type='signal'," | |
2881 | "sender='org.freedesktop.systemd1'," | |
c4e2ceae LP |
2882 | "interface='org.freedesktop.DBus.Properties'," |
2883 | "member='PropertiesChanged'", | |
f4579ce7 | 2884 | &error); |
7e4249b9 | 2885 | |
f4579ce7 | 2886 | if (dbus_error_is_set(&error)) { |
4cf5d675 | 2887 | log_error("Failed to add match: %s", bus_error_message(&error)); |
f4579ce7 LP |
2888 | r = -EIO; |
2889 | goto finish; | |
2890 | } | |
7e4249b9 LP |
2891 | } |
2892 | ||
2893 | if (!dbus_connection_add_filter(bus, monitor_filter, NULL, NULL)) { | |
2894 | log_error("Failed to add filter."); | |
2895 | r = -ENOMEM; | |
2896 | goto finish; | |
2897 | } | |
2898 | ||
2899 | if (!(m = dbus_message_new_method_call( | |
2900 | "org.freedesktop.systemd1", | |
2901 | "/org/freedesktop/systemd1", | |
2902 | "org.freedesktop.systemd1.Manager", | |
2903 | "Subscribe"))) { | |
2904 | log_error("Could not allocate message."); | |
2905 | r = -ENOMEM; | |
2906 | goto finish; | |
2907 | } | |
2908 | ||
2909 | if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) { | |
4cf5d675 | 2910 | log_error("Failed to issue method call: %s", bus_error_message(&error)); |
7e4249b9 LP |
2911 | r = -EIO; |
2912 | goto finish; | |
2913 | } | |
2914 | ||
2915 | while (dbus_connection_read_write_dispatch(bus, -1)) | |
2916 | ; | |
2917 | ||
2918 | r = 0; | |
2919 | ||
2920 | finish: | |
2921 | ||
2922 | /* This is slightly dirty, since we don't undo the filter or the matches. */ | |
2923 | ||
2924 | if (m) | |
2925 | dbus_message_unref(m); | |
2926 | ||
2927 | if (reply) | |
2928 | dbus_message_unref(reply); | |
2929 | ||
2930 | dbus_error_free(&error); | |
2931 | ||
2932 | return r; | |
2933 | } | |
2934 | ||
2935 | static int dump(DBusConnection *bus, char **args, unsigned n) { | |
2936 | DBusMessage *m = NULL, *reply = NULL; | |
2937 | DBusError error; | |
2938 | int r; | |
2939 | const char *text; | |
2940 | ||
2941 | dbus_error_init(&error); | |
2942 | ||
ec14911e LP |
2943 | pager_open(); |
2944 | ||
7e4249b9 LP |
2945 | if (!(m = dbus_message_new_method_call( |
2946 | "org.freedesktop.systemd1", | |
2947 | "/org/freedesktop/systemd1", | |
2948 | "org.freedesktop.systemd1.Manager", | |
2949 | "Dump"))) { | |
2950 | log_error("Could not allocate message."); | |
2951 | return -ENOMEM; | |
2952 | } | |
2953 | ||
2954 | if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) { | |
4cf5d675 | 2955 | log_error("Failed to issue method call: %s", bus_error_message(&error)); |
7e4249b9 LP |
2956 | r = -EIO; |
2957 | goto finish; | |
2958 | } | |
2959 | ||
2960 | if (!dbus_message_get_args(reply, &error, | |
2961 | DBUS_TYPE_STRING, &text, | |
2962 | DBUS_TYPE_INVALID)) { | |
4cf5d675 | 2963 | log_error("Failed to parse reply: %s", bus_error_message(&error)); |
7e4249b9 LP |
2964 | r = -EIO; |
2965 | goto finish; | |
2966 | } | |
2967 | ||
2968 | fputs(text, stdout); | |
2969 | ||
2970 | r = 0; | |
2971 | ||
2972 | finish: | |
2973 | if (m) | |
2974 | dbus_message_unref(m); | |
2975 | ||
2976 | if (reply) | |
2977 | dbus_message_unref(reply); | |
2978 | ||
2979 | dbus_error_free(&error); | |
2980 | ||
2981 | return r; | |
2982 | } | |
2983 | ||
2984 | static int snapshot(DBusConnection *bus, char **args, unsigned n) { | |
2985 | DBusMessage *m = NULL, *reply = NULL; | |
2986 | DBusError error; | |
2987 | int r; | |
2988 | const char *name = "", *path, *id; | |
2989 | dbus_bool_t cleanup = FALSE; | |
2990 | DBusMessageIter iter, sub; | |
2991 | const char | |
2992 | *interface = "org.freedesktop.systemd1.Unit", | |
2993 | *property = "Id"; | |
2994 | ||
2995 | dbus_error_init(&error); | |
2996 | ||
2997 | if (!(m = dbus_message_new_method_call( | |
2998 | "org.freedesktop.systemd1", | |
2999 | "/org/freedesktop/systemd1", | |
3000 | "org.freedesktop.systemd1.Manager", | |
3001 | "CreateSnapshot"))) { | |
3002 | log_error("Could not allocate message."); | |
3003 | return -ENOMEM; | |
3004 | } | |
3005 | ||
3006 | if (n > 1) | |
3007 | name = args[1]; | |
3008 | ||
3009 | if (!dbus_message_append_args(m, | |
3010 | DBUS_TYPE_STRING, &name, | |
3011 | DBUS_TYPE_BOOLEAN, &cleanup, | |
3012 | DBUS_TYPE_INVALID)) { | |
3013 | log_error("Could not append arguments to message."); | |
3014 | r = -ENOMEM; | |
3015 | goto finish; | |
3016 | } | |
3017 | ||
3018 | if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) { | |
4cf5d675 | 3019 | log_error("Failed to issue method call: %s", bus_error_message(&error)); |
7e4249b9 LP |
3020 | r = -EIO; |
3021 | goto finish; | |
3022 | } | |
3023 | ||
3024 | if (!dbus_message_get_args(reply, &error, | |
3025 | DBUS_TYPE_OBJECT_PATH, &path, | |
3026 | DBUS_TYPE_INVALID)) { | |
4cf5d675 | 3027 | log_error("Failed to parse reply: %s", bus_error_message(&error)); |
7e4249b9 LP |
3028 | r = -EIO; |
3029 | goto finish; | |
3030 | } | |
3031 | ||
3032 | dbus_message_unref(m); | |
3033 | if (!(m = dbus_message_new_method_call( | |
3034 | "org.freedesktop.systemd1", | |
3035 | path, | |
3036 | "org.freedesktop.DBus.Properties", | |
3037 | "Get"))) { | |
3038 | log_error("Could not allocate message."); | |
3039 | return -ENOMEM; | |
3040 | } | |
3041 | ||
3042 | if (!dbus_message_append_args(m, | |
3043 | DBUS_TYPE_STRING, &interface, | |
3044 | DBUS_TYPE_STRING, &property, | |
3045 | DBUS_TYPE_INVALID)) { | |
3046 | log_error("Could not append arguments to message."); | |
3047 | r = -ENOMEM; | |
3048 | goto finish; | |
3049 | } | |
3050 | ||
3051 | dbus_message_unref(reply); | |
3052 | if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) { | |
4cf5d675 | 3053 | log_error("Failed to issue method call: %s", bus_error_message(&error)); |
7e4249b9 LP |
3054 | r = -EIO; |
3055 | goto finish; | |
3056 | } | |
3057 | ||
3058 | if (!dbus_message_iter_init(reply, &iter) || | |
3059 | dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) { | |
3060 | log_error("Failed to parse reply."); | |
3061 | r = -EIO; | |
3062 | goto finish; | |
3063 | } | |
3064 | ||
3065 | dbus_message_iter_recurse(&iter, &sub); | |
3066 | ||
3067 | if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) { | |
3068 | log_error("Failed to parse reply."); | |
3069 | r = -EIO; | |
3070 | goto finish; | |
3071 | } | |
3072 | ||
3073 | dbus_message_iter_get_basic(&sub, &id); | |
0183528f LP |
3074 | |
3075 | if (!arg_quiet) | |
3076 | puts(id); | |
7e4249b9 LP |
3077 | r = 0; |
3078 | ||
3079 | finish: | |
3080 | if (m) | |
3081 | dbus_message_unref(m); | |
3082 | ||
3083 | if (reply) | |
3084 | dbus_message_unref(reply); | |
3085 | ||
3086 | dbus_error_free(&error); | |
3087 | ||
3088 | return r; | |
3089 | } | |
3090 | ||
6759e7a7 LP |
3091 | static int delete_snapshot(DBusConnection *bus, char **args, unsigned n) { |
3092 | DBusMessage *m = NULL, *reply = NULL; | |
3093 | int r; | |
3094 | DBusError error; | |
3095 | unsigned i; | |
3096 | ||
3097 | assert(bus); | |
3098 | assert(args); | |
3099 | ||
3100 | dbus_error_init(&error); | |
3101 | ||
3102 | for (i = 1; i < n; i++) { | |
3103 | const char *path = NULL; | |
3104 | ||
3105 | if (!(m = dbus_message_new_method_call( | |
3106 | "org.freedesktop.systemd1", | |
3107 | "/org/freedesktop/systemd1", | |
3108 | "org.freedesktop.systemd1.Manager", | |
3109 | "GetUnit"))) { | |
3110 | log_error("Could not allocate message."); | |
3111 | r = -ENOMEM; | |
3112 | goto finish; | |
3113 | } | |
3114 | ||
3115 | if (!dbus_message_append_args(m, | |
3116 | DBUS_TYPE_STRING, &args[i], | |
3117 | DBUS_TYPE_INVALID)) { | |
3118 | log_error("Could not append arguments to message."); | |
3119 | r = -ENOMEM; | |
3120 | goto finish; | |
3121 | } | |
3122 | ||
3123 | if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) { | |
4cf5d675 | 3124 | log_error("Failed to issue method call: %s", bus_error_message(&error)); |
6759e7a7 LP |
3125 | r = -EIO; |
3126 | goto finish; | |
3127 | } | |
3128 | ||
3129 | if (!dbus_message_get_args(reply, &error, | |
3130 | DBUS_TYPE_OBJECT_PATH, &path, | |
3131 | DBUS_TYPE_INVALID)) { | |
4cf5d675 | 3132 | log_error("Failed to parse reply: %s", bus_error_message(&error)); |
6759e7a7 LP |
3133 | r = -EIO; |
3134 | goto finish; | |
3135 | } | |
3136 | ||
3137 | dbus_message_unref(m); | |
3138 | if (!(m = dbus_message_new_method_call( | |
3139 | "org.freedesktop.systemd1", | |
3140 | path, | |
3141 | "org.freedesktop.systemd1.Snapshot", | |
3142 | "Remove"))) { | |
3143 | log_error("Could not allocate message."); | |
3144 | r = -ENOMEM; | |
3145 | goto finish; | |
3146 | } | |
3147 | ||
3148 | dbus_message_unref(reply); | |
3149 | if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) { | |
4cf5d675 | 3150 | log_error("Failed to issue method call: %s", bus_error_message(&error)); |
6759e7a7 LP |
3151 | r = -EIO; |
3152 | goto finish; | |
3153 | } | |
3154 | ||
3155 | dbus_message_unref(m); | |
3156 | dbus_message_unref(reply); | |
3157 | m = reply = NULL; | |
3158 | } | |
3159 | ||
3160 | r = 0; | |
3161 | ||
3162 | finish: | |
3163 | if (m) | |
3164 | dbus_message_unref(m); | |
3165 | ||
3166 | if (reply) | |
3167 | dbus_message_unref(reply); | |
3168 | ||
3169 | dbus_error_free(&error); | |
3170 | ||
3171 | return r; | |
3172 | } | |
3173 | ||
ee5762e3 | 3174 | static int daemon_reload(DBusConnection *bus, char **args, unsigned n) { |
7e4249b9 LP |
3175 | DBusMessage *m = NULL, *reply = NULL; |
3176 | DBusError error; | |
3177 | int r; | |
3178 | const char *method; | |
3179 | ||
3180 | dbus_error_init(&error); | |
3181 | ||
e4b61340 LP |
3182 | if (arg_action == ACTION_RELOAD) |
3183 | method = "Reload"; | |
3184 | else if (arg_action == ACTION_REEXEC) | |
3185 | method = "Reexecute"; | |
3186 | else { | |
3187 | assert(arg_action == ACTION_SYSTEMCTL); | |
3188 | ||
3189 | method = | |
20b09ca7 LP |
3190 | streq(args[0], "clear-jobs") || |
3191 | streq(args[0], "cancel") ? "ClearJobs" : | |
3192 | streq(args[0], "daemon-reexec") ? "Reexecute" : | |
3193 | streq(args[0], "reset-failed") ? "ResetFailed" : | |
3194 | streq(args[0], "halt") ? "Halt" : | |
3195 | streq(args[0], "poweroff") ? "PowerOff" : | |
3196 | streq(args[0], "reboot") ? "Reboot" : | |
3197 | streq(args[0], "kexec") ? "KExec" : | |
3198 | streq(args[0], "exit") ? "Exit" : | |
3199 | /* "daemon-reload" */ "Reload"; | |
e4b61340 | 3200 | } |
7e4249b9 LP |
3201 | |
3202 | if (!(m = dbus_message_new_method_call( | |
3203 | "org.freedesktop.systemd1", | |
3204 | "/org/freedesktop/systemd1", | |
3205 | "org.freedesktop.systemd1.Manager", | |
3206 | method))) { | |
3207 | log_error("Could not allocate message."); | |
3208 | return -ENOMEM; | |
3209 | } | |
3210 | ||
3211 | if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) { | |
e4b61340 LP |
3212 | |
3213 | if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) { | |
3214 | /* There's always a fallback possible for | |
3215 | * legacy actions. */ | |
aabd9b11 | 3216 | r = -EADDRNOTAVAIL; |
e4b61340 LP |
3217 | goto finish; |
3218 | } | |
3219 | ||
4cf5d675 | 3220 | log_error("Failed to issue method call: %s", bus_error_message(&error)); |
7e4249b9 LP |
3221 | r = -EIO; |
3222 | goto finish; | |
3223 | } | |
3224 | ||
aabd9b11 | 3225 | r = 0; |
7e4249b9 LP |
3226 | |
3227 | finish: | |
3228 | if (m) | |
3229 | dbus_message_unref(m); | |
3230 | ||
3231 | if (reply) | |
3232 | dbus_message_unref(reply); | |
3233 | ||
3234 | dbus_error_free(&error); | |
3235 | ||
3236 | return r; | |
3237 | } | |
3238 | ||
fdf20a31 | 3239 | static int reset_failed(DBusConnection *bus, char **args, unsigned n) { |
5632e374 LP |
3240 | DBusMessage *m = NULL, *reply = NULL; |
3241 | unsigned i; | |
3242 | int r; | |
3243 | DBusError error; | |
3244 | ||
3245 | assert(bus); | |
3246 | dbus_error_init(&error); | |
3247 | ||
3248 | if (n <= 1) | |
ee5762e3 | 3249 | return daemon_reload(bus, args, n); |
5632e374 LP |
3250 | |
3251 | for (i = 1; i < n; i++) { | |
3252 | ||
3253 | if (!(m = dbus_message_new_method_call( | |
3254 | "org.freedesktop.systemd1", | |
3255 | "/org/freedesktop/systemd1", | |
3256 | "org.freedesktop.systemd1.Manager", | |
fdf20a31 | 3257 | "ResetFailedUnit"))) { |
5632e374 LP |
3258 | log_error("Could not allocate message."); |
3259 | r = -ENOMEM; | |
3260 | goto finish; | |
3261 | } | |
3262 | ||
3263 | if (!dbus_message_append_args(m, | |
3264 | DBUS_TYPE_STRING, args + i, | |
3265 | DBUS_TYPE_INVALID)) { | |
3266 | log_error("Could not append arguments to message."); | |
3267 | r = -ENOMEM; | |
3268 | goto finish; | |
3269 | } | |
3270 | ||
3271 | if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) { | |
4cf5d675 | 3272 | log_error("Failed to issue method call: %s", bus_error_message(&error)); |
5632e374 LP |
3273 | r = -EIO; |
3274 | goto finish; | |
3275 | } | |
3276 | ||
3277 | dbus_message_unref(m); | |
3278 | dbus_message_unref(reply); | |
3279 | m = reply = NULL; | |
3280 | } | |
3281 | ||
3282 | r = 0; | |
3283 | ||
3284 | finish: | |
3285 | if (m) | |
3286 | dbus_message_unref(m); | |
3287 | ||
3288 | if (reply) | |
3289 | dbus_message_unref(reply); | |
3290 | ||
3291 | dbus_error_free(&error); | |
3292 | ||
3293 | return r; | |
3294 | } | |
3295 | ||
7e4249b9 LP |
3296 | static int show_enviroment(DBusConnection *bus, char **args, unsigned n) { |
3297 | DBusMessage *m = NULL, *reply = NULL; | |
3298 | DBusError error; | |
3299 | DBusMessageIter iter, sub, sub2; | |
3300 | int r; | |
3301 | const char | |
3302 | *interface = "org.freedesktop.systemd1.Manager", | |
3303 | *property = "Environment"; | |
3304 | ||
3305 | dbus_error_init(&error); | |
3306 | ||
ec14911e LP |
3307 | pager_open(); |
3308 | ||
7e4249b9 LP |
3309 | if (!(m = dbus_message_new_method_call( |
3310 | "org.freedesktop.systemd1", | |
3311 | "/org/freedesktop/systemd1", | |
3312 | "org.freedesktop.DBus.Properties", | |
3313 | "Get"))) { | |
3314 | log_error("Could not allocate message."); | |
3315 | return -ENOMEM; | |
3316 | } | |
3317 | ||
3318 | if (!dbus_message_append_args(m, | |
3319 | DBUS_TYPE_STRING, &interface, | |
3320 | DBUS_TYPE_STRING, &property, | |
3321 | DBUS_TYPE_INVALID)) { | |
3322 | log_error("Could not append arguments to message."); | |
3323 | r = -ENOMEM; | |
3324 | goto finish; | |
3325 | } | |
3326 | ||
3327 | if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) { | |
4cf5d675 | 3328 | log_error("Failed to issue method call: %s", bus_error_message(&error)); |
7e4249b9 LP |
3329 | r = -EIO; |
3330 | goto finish; | |
3331 | } | |
3332 | ||
3333 | if (!dbus_message_iter_init(reply, &iter) || | |
3334 | dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) { | |
3335 | log_error("Failed to parse reply."); | |
3336 | r = -EIO; | |
3337 | goto finish; | |
3338 | } | |
3339 | ||
3340 | dbus_message_iter_recurse(&iter, &sub); | |
3341 | ||
3342 | if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY || | |
3343 | dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) { | |
3344 | log_error("Failed to parse reply."); | |
3345 | r = -EIO; | |
3346 | goto finish; | |
3347 | } | |
3348 | ||
3349 | dbus_message_iter_recurse(&sub, &sub2); | |
3350 | ||
3351 | while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) { | |
3352 | const char *text; | |
3353 | ||
3354 | if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) { | |
3355 | log_error("Failed to parse reply."); | |
3356 | r = -EIO; | |
3357 | goto finish; | |
3358 | } | |
3359 | ||
3360 | dbus_message_iter_get_basic(&sub2, &text); | |
3361 | printf("%s\n", text); | |
3362 | ||
3363 | dbus_message_iter_next(&sub2); | |
3364 | } | |
3365 | ||
3366 | r = 0; | |
3367 | ||
3368 | finish: | |
3369 | if (m) | |
3370 | dbus_message_unref(m); | |
3371 | ||
3372 | if (reply) | |
3373 | dbus_message_unref(reply); | |
3374 | ||
3375 | dbus_error_free(&error); | |
3376 | ||
3377 | return r; | |
3378 | } | |
3379 | ||
3380 | static int set_environment(DBusConnection *bus, char **args, unsigned n) { | |
3381 | DBusMessage *m = NULL, *reply = NULL; | |
3382 | DBusError error; | |
3383 | int r; | |
3384 | const char *method; | |
3385 | DBusMessageIter iter, sub; | |
3386 | unsigned i; | |
3387 | ||
3388 | dbus_error_init(&error); | |
3389 | ||
3390 | method = streq(args[0], "set-environment") | |
3391 | ? "SetEnvironment" | |
3392 | : "UnsetEnvironment"; | |
3393 | ||
3394 | if (!(m = dbus_message_new_method_call( | |
3395 | "org.freedesktop.systemd1", | |
3396 | "/org/freedesktop/systemd1", | |
3397 | "org.freedesktop.systemd1.Manager", | |
3398 | method))) { | |
3399 | ||
3400 | log_error("Could not allocate message."); | |
3401 | return -ENOMEM; | |
3402 | } | |
3403 | ||
3404 | dbus_message_iter_init_append(m, &iter); | |
3405 | ||
3406 | if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) { | |
3407 | log_error("Could not append arguments to message."); | |
3408 | r = -ENOMEM; | |
3409 | goto finish; | |
3410 | } | |
3411 | ||
3412 | for (i = 1; i < n; i++) | |
3413 | if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &args[i])) { | |
3414 | log_error("Could not append arguments to message."); | |
3415 | r = -ENOMEM; | |
3416 | goto finish; | |
3417 | } | |
3418 | ||
3419 | if (!dbus_message_iter_close_container(&iter, &sub)) { | |
3420 | log_error("Could not append arguments to message."); | |
3421 | r = -ENOMEM; | |
3422 | goto finish; | |
3423 | } | |
3424 | ||
3425 | if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) { | |
4cf5d675 | 3426 | log_error("Failed to issue method call: %s", bus_error_message(&error)); |
7e4249b9 LP |
3427 | r = -EIO; |
3428 | goto finish; | |
3429 | } | |
3430 | ||
3431 | r = 0; | |
3432 | ||
3433 | finish: | |
3434 | if (m) | |
3435 | dbus_message_unref(m); | |
3436 | ||
3437 | if (reply) | |
3438 | dbus_message_unref(reply); | |
3439 | ||
3440 | dbus_error_free(&error); | |
3441 | ||
3442 | return r; | |
3443 | } | |
3444 | ||
ee5762e3 LP |
3445 | typedef struct { |
3446 | char *name; | |
3447 | char *path; | |
3448 | ||
3449 | char **aliases; | |
3450 | char **wanted_by; | |
3451 | } InstallInfo; | |
3452 | ||
3453 | static Hashmap *will_install = NULL, *have_installed = NULL; | |
3454 | static Set *remove_symlinks_to = NULL; | |
560d8f23 | 3455 | static unsigned n_symlinks = 0; |
ee5762e3 LP |
3456 | |
3457 | static void install_info_free(InstallInfo *i) { | |
3458 | assert(i); | |
3459 | ||
3460 | free(i->name); | |
3461 | free(i->path); | |
3462 | strv_free(i->aliases); | |
3463 | strv_free(i->wanted_by); | |
3464 | free(i); | |
3465 | } | |
3466 | ||
3467 | static void install_info_hashmap_free(Hashmap *m) { | |
3468 | InstallInfo *i; | |
3469 | ||
3470 | while ((i = hashmap_steal_first(m))) | |
3471 | install_info_free(i); | |
3472 | ||
3473 | hashmap_free(m); | |
3474 | } | |
3475 | ||
ee5762e3 LP |
3476 | static int install_info_add(const char *name) { |
3477 | InstallInfo *i; | |
3478 | int r; | |
3479 | ||
3480 | assert(will_install); | |
3481 | ||
b9c0d441 | 3482 | if (!unit_name_is_valid_no_type(name, true)) { |
71fad675 | 3483 | log_warning("Unit name %s is not a valid unit name.", name); |
ee5762e3 | 3484 | return -EINVAL; |
71fad675 | 3485 | } |
ee5762e3 LP |
3486 | |
3487 | if (hashmap_get(have_installed, name) || | |
3488 | hashmap_get(will_install, name)) | |
3489 | return 0; | |
3490 | ||
3491 | if (!(i = new0(InstallInfo, 1))) { | |
3492 | r = -ENOMEM; | |
3493 | goto fail; | |
3494 | } | |
3495 | ||
3496 | if (!(i->name = strdup(name))) { | |
3497 | r = -ENOMEM; | |
3498 | goto fail; | |
3499 | } | |
3500 | ||
3501 | if ((r = hashmap_put(will_install, i->name, i)) < 0) | |
3502 | goto fail; | |
3503 | ||
3504 | return 0; | |
3505 | ||
3506 | fail: | |
3507 | if (i) | |
3508 | install_info_free(i); | |
3509 | ||
3510 | return r; | |
3511 | } | |
3512 | ||
3513 | static int config_parse_also( | |
3514 | const char *filename, | |
3515 | unsigned line, | |
3516 | const char *section, | |
3517 | const char *lvalue, | |
3518 | const char *rvalue, | |
3519 | void *data, | |
3520 | void *userdata) { | |
3521 | ||
3522 | char *w; | |
3523 | size_t l; | |
3524 | char *state; | |
3525 | ||
3526 | assert(filename); | |
3527 | assert(lvalue); | |
3528 | assert(rvalue); | |
3529 | ||
3530 | FOREACH_WORD_QUOTED(w, l, rvalue, state) { | |
3531 | char *n; | |
3532 | int r; | |
3533 | ||
3534 | if (!(n = strndup(w, l))) | |
3535 | return -ENOMEM; | |
3536 | ||
71fad675 LP |
3537 | if ((r = install_info_add(n)) < 0) { |
3538 | log_warning("Cannot install unit %s: %s", n, strerror(-r)); | |
3539 | free(n); | |
ee5762e3 | 3540 | return r; |
71fad675 LP |
3541 | } |
3542 | ||
3543 | free(n); | |
ee5762e3 LP |
3544 | } |
3545 | ||
3546 | return 0; | |
3547 | } | |
3548 | ||
3549 | static int mark_symlink_for_removal(const char *p) { | |
3550 | char *n; | |
3551 | int r; | |
3552 | ||
3553 | assert(p); | |
3554 | assert(path_is_absolute(p)); | |
3555 | ||
3556 | if (!remove_symlinks_to) | |
3557 | return 0; | |
3558 | ||
3559 | if (!(n = strdup(p))) | |
3560 | return -ENOMEM; | |
3561 | ||
3562 | path_kill_slashes(n); | |
3563 | ||
3564 | if ((r = set_put(remove_symlinks_to, n)) < 0) { | |
3565 | free(n); | |
3566 | return r == -EEXIST ? 0 : r; | |
3567 | } | |
3568 | ||
3569 | return 0; | |
3570 | } | |
3571 | ||
3572 | static int remove_marked_symlinks_fd(int fd, const char *config_path, const char *root, bool *deleted) { | |
3573 | int r = 0; | |
3574 | DIR *d; | |
3575 | struct dirent *de; | |
3576 | ||
3577 | assert(fd >= 0); | |
3578 | assert(root); | |
3579 | assert(deleted); | |
3580 | ||
3581 | if (!(d = fdopendir(fd))) { | |
3582 | close_nointr_nofail(fd); | |
3583 | return -errno; | |
3584 | } | |
3585 | ||
3586 | rewinddir(d); | |
3587 | ||
3588 | while ((de = readdir(d))) { | |
3589 | bool is_dir = false, is_link = false; | |
3590 | ||
3591 | if (ignore_file(de->d_name)) | |
3592 | continue; | |
3593 | ||
3594 | if (de->d_type == DT_LNK) | |
3595 | is_link = true; | |
3596 | else if (de->d_type == DT_DIR) | |
3597 | is_dir = true; | |
3598 | else if (de->d_type == DT_UNKNOWN) { | |
3599 | struct stat st; | |
3600 | ||
3601 | if (fstatat(fd, de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) { | |
3602 | log_error("Failed to stat %s/%s: %m", root, de->d_name); | |
3603 | ||
3604 | if (r == 0) | |
3605 | r = -errno; | |
3606 | continue; | |
3607 | } | |
3608 | ||
3609 | is_link = S_ISLNK(st.st_mode); | |
3610 | is_dir = S_ISDIR(st.st_mode); | |
3611 | } else | |
3612 | continue; | |
3613 | ||
3614 | if (is_dir) { | |
3615 | int nfd, q; | |
3616 | char *p; | |
3617 | ||
3618 | if ((nfd = openat(fd, de->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW)) < 0) { | |
3619 | log_error("Failed to open %s/%s: %m", root, de->d_name); | |
3620 | ||
3621 | if (r == 0) | |
3622 | r = -errno; | |
3623 | continue; | |
3624 | } | |
3625 | ||
3626 | if (asprintf(&p, "%s/%s", root, de->d_name) < 0) { | |
3627 | log_error("Failed to allocate directory string."); | |
3628 | close_nointr_nofail(nfd); | |
3629 | r = -ENOMEM; | |
3630 | break; | |
3631 | } | |
3632 | ||
3633 | /* This will close nfd, regardless whether it succeeds or not */ | |
3634 | q = remove_marked_symlinks_fd(nfd, config_path, p, deleted); | |
3635 | free(p); | |
3636 | ||
3637 | if (r == 0) | |
e364ad06 | 3638 | r = q; |
ee5762e3 LP |
3639 | |
3640 | } else if (is_link) { | |
3641 | char *p, *dest, *c; | |
3642 | int q; | |
3643 | ||
3644 | if (asprintf(&p, "%s/%s", root, de->d_name) < 0) { | |
3645 | log_error("Failed to allocate symlink string."); | |
3646 | r = -ENOMEM; | |
3647 | break; | |
3648 | } | |
3649 | ||
3650 | if ((q = readlink_and_make_absolute(p, &dest)) < 0) { | |
3651 | log_error("Cannot read symlink %s: %s", p, strerror(-q)); | |
3652 | free(p); | |
3653 | ||
3654 | if (r == 0) | |
3655 | r = q; | |
3656 | continue; | |
3657 | } | |
3658 | ||
3659 | if ((c = canonicalize_file_name(dest))) { | |
3660 | /* This might fail if the destination | |
3661 | * is already removed */ | |
3662 | ||
3663 | free(dest); | |
3664 | dest = c; | |
3665 | } | |
3666 | ||
3667 | path_kill_slashes(dest); | |
3668 | if (set_get(remove_symlinks_to, dest)) { | |
3669 | ||
3670 | if (!arg_quiet) | |
3671 | log_info("rm '%s'", p); | |
3672 | ||
3673 | if (unlink(p) < 0) { | |
3674 | log_error("Cannot unlink symlink %s: %m", p); | |
3675 | ||
3676 | if (r == 0) | |
3677 | r = -errno; | |
3678 | } else { | |
3679 | rmdir_parents(p, config_path); | |
3680 | path_kill_slashes(p); | |
3681 | ||
3682 | if (!set_get(remove_symlinks_to, p)) { | |
3683 | ||
3684 | if ((r = mark_symlink_for_removal(p)) < 0) { | |
3685 | if (r == 0) | |
3686 | r = q; | |
3687 | } else | |
3688 | *deleted = true; | |
3689 | } | |
3690 | } | |
3691 | } | |
3692 | ||
3693 | free(p); | |
3694 | free(dest); | |
3695 | } | |
3696 | } | |
3697 | ||
3698 | closedir(d); | |
3699 | ||
3700 | return r; | |
3701 | } | |
3702 | ||
3703 | static int remove_marked_symlinks(const char *config_path) { | |
3704 | int fd, r = 0; | |
3705 | bool deleted; | |
3706 | ||
3707 | assert(config_path); | |
3708 | ||
3709 | if (set_size(remove_symlinks_to) <= 0) | |
3710 | return 0; | |
3711 | ||
3712 | if ((fd = open(config_path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW)) < 0) | |
3713 | return -errno; | |
3714 | ||
3715 | do { | |
3716 | int q, cfd; | |
3717 | deleted = false; | |
3718 | ||
3719 | if ((cfd = dup(fd)) < 0) { | |
3720 | r = -errno; | |
3721 | break; | |
3722 | } | |
3723 | ||
3724 | /* This takes possession of cfd and closes it */ | |
3725 | if ((q = remove_marked_symlinks_fd(cfd, config_path, config_path, &deleted)) < 0) { | |
3726 | if (r == 0) | |
3727 | r = q; | |
3728 | } | |
3729 | } while (deleted); | |
3730 | ||
3731 | close_nointr_nofail(fd); | |
3732 | ||
3733 | return r; | |
3734 | } | |
3735 | ||
3736 | static int create_symlink(const char *verb, const char *old_path, const char *new_path) { | |
3737 | int r; | |
3738 | ||
3739 | assert(old_path); | |
3740 | assert(new_path); | |
3741 | assert(verb); | |
3742 | ||
3743 | if (streq(verb, "enable")) { | |
3744 | char *dest; | |
3745 | ||
3746 | mkdir_parents(new_path, 0755); | |
3747 | ||
3748 | if (symlink(old_path, new_path) >= 0) { | |
3749 | ||
3750 | if (!arg_quiet) | |
3751 | log_info("ln -s '%s' '%s'", old_path, new_path); | |
3752 | ||
3753 | return 0; | |
3754 | } | |
3755 | ||
3756 | if (errno != EEXIST) { | |
3757 | log_error("Cannot link %s to %s: %m", old_path, new_path); | |
3758 | return -errno; | |
3759 | } | |
3760 | ||
3761 | if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) { | |
3762 | ||
3763 | if (errno == EINVAL) { | |
3764 | log_error("Cannot link %s to %s, file exists already and is not a symlink.", old_path, new_path); | |
3765 | return -EEXIST; | |
3766 | } | |
3767 | ||
3768 | log_error("readlink() failed: %s", strerror(-r)); | |
3769 | return r; | |
3770 | } | |
3771 | ||
3772 | if (streq(dest, old_path)) { | |
3773 | free(dest); | |
3774 | return 0; | |
3775 | } | |
3776 | ||
3777 | if (!arg_force) { | |
3778 | log_error("Cannot link %s to %s, symlink exists already and points to %s.", old_path, new_path, dest); | |
3779 | free(dest); | |
3780 | return -EEXIST; | |
3781 | } | |
3782 | ||
3783 | free(dest); | |
3784 | unlink(new_path); | |
3785 | ||
3786 | if (!arg_quiet) | |
3787 | log_info("ln -s '%s' '%s'", old_path, new_path); | |
3788 | ||
3789 | if (symlink(old_path, new_path) >= 0) | |
3790 | return 0; | |
3791 | ||
3792 | log_error("Cannot link %s to %s: %m", old_path, new_path); | |
3793 | return -errno; | |
3794 | ||
3795 | } else if (streq(verb, "disable")) { | |
3796 | char *dest; | |
3797 | ||
3798 | if ((r = mark_symlink_for_removal(old_path)) < 0) | |
3799 | return r; | |
3800 | ||
3801 | if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) { | |
3802 | if (errno == ENOENT) | |
3803 | return 0; | |
3804 | ||
3805 | if (errno == EINVAL) { | |
3806 | log_warning("File %s not a symlink, ignoring.", old_path); | |
3807 | return 0; | |
3808 | } | |
3809 | ||
3810 | log_error("readlink() failed: %s", strerror(-r)); | |
3811 | return r; | |
3812 | } | |
3813 | ||
3814 | if (!streq(dest, old_path)) { | |
3815 | log_warning("File %s not a symlink to %s but points to %s, ignoring.", new_path, old_path, dest); | |
3816 | free(dest); | |
3817 | return 0; | |
3818 | } | |
3819 | ||
3820 | free(dest); | |
3821 | ||
3822 | if ((r = mark_symlink_for_removal(new_path)) < 0) | |
3823 | return r; | |
3824 | ||
3825 | if (!arg_quiet) | |
3826 | log_info("rm '%s'", new_path); | |
3827 | ||
3828 | if (unlink(new_path) >= 0) | |
3829 | return 0; | |
3830 | ||
3831 | log_error("Cannot unlink %s: %m", new_path); | |
3832 | return -errno; | |
3833 | ||
3834 | } else if (streq(verb, "is-enabled")) { | |
3835 | char *dest; | |
3836 | ||
3837 | if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) { | |
3838 | ||
3839 | if (errno == ENOENT || errno == EINVAL) | |
3840 | return 0; | |
3841 | ||
3842 | log_error("readlink() failed: %s", strerror(-r)); | |
3843 | return r; | |
3844 | } | |
3845 | ||
3846 | if (streq(dest, old_path)) { | |
3847 | free(dest); | |
3848 | return 1; | |
3849 | } | |
3850 | ||
3851 | return 0; | |
3852 | } | |
3853 | ||
3854 | assert_not_reached("Unknown action."); | |
3855 | } | |
3856 | ||
3857 | static int install_info_symlink_alias(const char *verb, InstallInfo *i, const char *config_path) { | |
3858 | char **s; | |
3859 | char *alias_path = NULL; | |
3860 | int r; | |
3861 | ||
3862 | assert(verb); | |
3863 | assert(i); | |
3864 | assert(config_path); | |
3865 | ||
3866 | STRV_FOREACH(s, i->aliases) { | |
3867 | ||
ee5762e3 LP |
3868 | free(alias_path); |
3869 | if (!(alias_path = path_make_absolute(*s, config_path))) { | |
3870 | log_error("Out of memory"); | |
3871 | r = -ENOMEM; | |
3872 | goto finish; | |
3873 | } | |
3874 | ||
3875 | if ((r = create_symlink(verb, i->path, alias_path)) != 0) | |
3876 | goto finish; | |
3877 | ||
3878 | if (streq(verb, "disable")) | |
3879 | rmdir_parents(alias_path, config_path); | |
3880 | } | |
ee5762e3 LP |
3881 | r = 0; |
3882 | ||
3883 | finish: | |
3884 | free(alias_path); | |
3885 | ||
3886 | return r; | |
3887 | } | |
3888 | ||
3889 | static int install_info_symlink_wants(const char *verb, InstallInfo *i, const char *config_path) { | |
3890 | char **s; | |
3891 | char *alias_path = NULL; | |
3892 | int r; | |
3893 | ||
3894 | assert(verb); | |
3895 | assert(i); | |
3896 | assert(config_path); | |
3897 | ||
3898 | STRV_FOREACH(s, i->wanted_by) { | |
b9c0d441 | 3899 | if (!unit_name_is_valid_no_type(*s, true)) { |
ee5762e3 LP |
3900 | log_error("Invalid name %s.", *s); |
3901 | r = -EINVAL; | |
3902 | goto finish; | |
3903 | } | |
3904 | ||
3905 | free(alias_path); | |
3906 | alias_path = NULL; | |
3907 | ||
3908 | if (asprintf(&alias_path, "%s/%s.wants/%s", config_path, *s, i->name) < 0) { | |
3909 | log_error("Out of memory"); | |
3910 | r = -ENOMEM; | |
3911 | goto finish; | |
3912 | } | |
3913 | ||
3914 | if ((r = create_symlink(verb, i->path, alias_path)) != 0) | |
3915 | goto finish; | |
3916 | ||
3917 | if (streq(verb, "disable")) | |
3918 | rmdir_parents(alias_path, config_path); | |
3919 | } | |
3920 | ||
3921 | r = 0; | |
3922 | ||
3923 | finish: | |
3924 | free(alias_path); | |
3925 | ||
3926 | return r; | |
3927 | } | |
3928 | ||
3929 | static int install_info_apply(const char *verb, LookupPaths *paths, InstallInfo *i, const char *config_path) { | |
3930 | ||
3931 | const ConfigItem items[] = { | |
3932 | { "Alias", config_parse_strv, &i->aliases, "Install" }, | |
3933 | { "WantedBy", config_parse_strv, &i->wanted_by, "Install" }, | |
3934 | { "Also", config_parse_also, NULL, "Install" }, | |
3935 | ||
3936 | { NULL, NULL, NULL, NULL } | |
3937 | }; | |
3938 | ||
3939 | char **p; | |
3940 | char *filename = NULL; | |
3941 | FILE *f = NULL; | |
3942 | int r; | |
3943 | ||
3944 | assert(paths); | |
3945 | assert(i); | |
3946 | ||
3947 | STRV_FOREACH(p, paths->unit_path) { | |
3948 | int fd; | |
3949 | ||
3950 | if (!(filename = path_make_absolute(i->name, *p))) { | |
3951 | log_error("Out of memory"); | |
3952 | return -ENOMEM; | |
3953 | } | |
3954 | ||
3955 | /* Ensure that we don't follow symlinks */ | |
3956 | if ((fd = open(filename, O_RDONLY|O_CLOEXEC|O_NOFOLLOW|O_NOCTTY)) >= 0) | |
3957 | if ((f = fdopen(fd, "re"))) | |
3958 | break; | |
3959 | ||
3960 | if (errno == ELOOP) { | |
3961 | log_error("Refusing to operate on symlinks, please pass unit names or absolute paths to unit files."); | |
3962 | free(filename); | |
3963 | return -errno; | |
3964 | } | |
3965 | ||
3966 | if (errno != ENOENT) { | |
3967 | log_error("Failed to open %s: %m", filename); | |
3968 | free(filename); | |
3969 | return -errno; | |
3970 | } | |
3971 | ||
3972 | free(filename); | |
3973 | filename = NULL; | |
3974 | } | |
3975 | ||
3976 | if (!f) { | |
c8b2e52c LP |
3977 | #if defined(TARGET_FEDORA) && defined (HAVE_SYSV_COMPAT) |
3978 | ||
3979 | if (endswith(i->name, ".service")) { | |
3980 | char *sysv; | |
3981 | bool exists; | |
3982 | ||
3983 | if (asprintf(&sysv, SYSTEM_SYSVINIT_PATH "/%s", i->name) < 0) { | |
3984 | log_error("Out of memory"); | |
3985 | return -ENOMEM; | |
3986 | } | |
3987 | ||
3988 | sysv[strlen(sysv) - sizeof(".service") + 1] = 0; | |
3989 | exists = access(sysv, F_OK) >= 0; | |
3990 | ||
3991 | if (exists) { | |
3992 | pid_t pid; | |
3993 | siginfo_t status; | |
3994 | ||
3995 | const char *argv[] = { | |
3996 | "/sbin/chkconfig", | |
3997 | NULL, | |
3998 | NULL, | |
3999 | NULL | |
4000 | }; | |
4001 | ||
4002 | log_info("%s is not a native service, redirecting to /sbin/chkconfig.", i->name); | |
4003 | ||
4004 | argv[1] = file_name_from_path(sysv); | |
4005 | argv[2] = | |
4006 | streq(verb, "enable") ? "on" : | |
4007 | streq(verb, "disable") ? "off" : NULL; | |
4008 | ||
4009 | log_info("Executing %s %s %s", argv[0], argv[1], strempty(argv[2])); | |
4010 | ||
4011 | if ((pid = fork()) < 0) { | |
4012 | log_error("Failed to fork: %m"); | |
4013 | free(sysv); | |
4014 | return -errno; | |
4015 | } else if (pid == 0) { | |
4016 | execv(argv[0], (char**) argv); | |
4017 | _exit(EXIT_FAILURE); | |
4018 | } | |
4019 | ||
4020 | free(sysv); | |
4021 | ||
4022 | if ((r = wait_for_terminate(pid, &status)) < 0) | |
4023 | return r; | |
4024 | ||
4025 | if (status.si_code == CLD_EXITED) { | |
4026 | if (status.si_status == 0 && (streq(verb, "enable") || streq(verb, "disable"))) | |
4027 | n_symlinks ++; | |
4028 | ||
4029 | return status.si_status == 0 ? 0 : -EINVAL; | |
4030 | } else | |
4031 | return -EPROTO; | |
4032 | } | |
4033 | ||
4034 | free(sysv); | |
4035 | } | |
4036 | ||
4037 | #endif | |
4038 | ||
ee5762e3 LP |
4039 | log_error("Couldn't find %s.", i->name); |
4040 | return -ENOENT; | |
4041 | } | |
4042 | ||
4043 | i->path = filename; | |
4044 | ||
4045 | if ((r = config_parse(filename, f, NULL, items, true, i)) < 0) { | |
4046 | fclose(f); | |
4047 | return r; | |
4048 | } | |
4049 | ||
560d8f23 LP |
4050 | n_symlinks += strv_length(i->aliases); |
4051 | n_symlinks += strv_length(i->wanted_by); | |
4052 | ||
ee5762e3 LP |
4053 | fclose(f); |
4054 | ||
4055 | if ((r = install_info_symlink_alias(verb, i, config_path)) != 0) | |
4056 | return r; | |
4057 | ||
4058 | if ((r = install_info_symlink_wants(verb, i, config_path)) != 0) | |
4059 | return r; | |
4060 | ||
4061 | if ((r = mark_symlink_for_removal(filename)) < 0) | |
4062 | return r; | |
4063 | ||
4064 | if ((r = remove_marked_symlinks(config_path)) < 0) | |
4065 | return r; | |
4066 | ||
4067 | return 0; | |
4068 | } | |
4069 | ||
4070 | static char *get_config_path(void) { | |
4071 | ||
af2d49f7 LP |
4072 | if (arg_user && arg_global) |
4073 | return strdup(USER_CONFIG_UNIT_PATH); | |
ee5762e3 | 4074 | |
af2d49f7 | 4075 | if (arg_user) { |
ee5762e3 LP |
4076 | char *p; |
4077 | ||
af2d49f7 | 4078 | if (user_config_home(&p) < 0) |
ee5762e3 LP |
4079 | return NULL; |
4080 | ||
4081 | return p; | |
4082 | } | |
4083 | ||
4084 | return strdup(SYSTEM_CONFIG_UNIT_PATH); | |
4085 | } | |
4086 | ||
4087 | static int enable_unit(DBusConnection *bus, char **args, unsigned n) { | |
4088 | DBusError error; | |
4089 | int r; | |
4090 | LookupPaths paths; | |
4091 | char *config_path = NULL; | |
4092 | unsigned j; | |
4093 | InstallInfo *i; | |
4094 | const char *verb = args[0]; | |
4095 | ||
4096 | dbus_error_init(&error); | |
4097 | ||
4098 | zero(paths); | |
af2d49f7 | 4099 | if ((r = lookup_paths_init(&paths, arg_user ? MANAGER_USER : MANAGER_SYSTEM)) < 0) { |
ee5762e3 LP |
4100 | log_error("Failed to determine lookup paths: %s", strerror(-r)); |
4101 | goto finish; | |
4102 | } | |
4103 | ||
4104 | if (!(config_path = get_config_path())) { | |
4105 | log_error("Failed to determine config path"); | |
4106 | r = -ENOMEM; | |
4107 | goto finish; | |
4108 | } | |
4109 | ||
4110 | will_install = hashmap_new(string_hash_func, string_compare_func); | |
4111 | have_installed = hashmap_new(string_hash_func, string_compare_func); | |
4112 | ||
4113 | if (!will_install || !have_installed) { | |
4114 | log_error("Failed to allocate unit sets."); | |
4115 | r = -ENOMEM; | |
4116 | goto finish; | |
4117 | } | |
4118 | ||
4119 | if (!arg_defaults && streq(verb, "disable")) | |
4120 | if (!(remove_symlinks_to = set_new(string_hash_func, string_compare_func))) { | |
4121 | log_error("Failed to allocate symlink sets."); | |
4122 | r = -ENOMEM; | |
4123 | goto finish; | |
4124 | } | |
4125 | ||
4126 | for (j = 1; j < n; j++) | |
71fad675 LP |
4127 | if ((r = install_info_add(args[j])) < 0) { |
4128 | log_warning("Cannot install unit %s: %s", args[j], strerror(-r)); | |
ee5762e3 | 4129 | goto finish; |
71fad675 | 4130 | } |
ee5762e3 LP |
4131 | |
4132 | while ((i = hashmap_first(will_install))) { | |
4133 | int q; | |
4134 | ||
4135 | assert_se(hashmap_move_one(have_installed, will_install, i->name) == 0); | |
4136 | ||
4137 | if ((q = install_info_apply(verb, &paths, i, config_path)) != 0) { | |
4138 | ||
4139 | if (q < 0) { | |
4140 | if (r == 0) | |
4141 | r = q; | |
4142 | goto finish; | |
4143 | } | |
4144 | ||
4145 | /* In test mode and found something */ | |
4146 | r = 1; | |
4147 | break; | |
4148 | } | |
4149 | } | |
4150 | ||
4151 | if (streq(verb, "is-enabled")) | |
4152 | r = r > 0 ? 0 : -ENOENT; | |
560d8f23 LP |
4153 | else { |
4154 | if (n_symlinks <= 0) | |
4155 | log_warning("Unit files contain no applicable installation information. Ignoring."); | |
4156 | ||
4157 | if (bus && | |
4158 | /* Don't try to reload anything if the user asked us to not do this */ | |
4159 | !arg_no_reload && | |
4160 | /* Don't try to reload anything when updating a unit globally */ | |
4161 | !arg_global && | |
4162 | /* Don't try to reload anything if we are called for system changes but the system wasn't booted with systemd */ | |
af2d49f7 | 4163 | (arg_user || sd_booted() > 0) && |
560d8f23 | 4164 | /* Don't try to reload anything if we are running in a chroot environment */ |
af2d49f7 | 4165 | (arg_user || running_in_chroot() <= 0) ) { |
560d8f23 | 4166 | int q; |
ee5762e3 | 4167 | |
560d8f23 LP |
4168 | if ((q = daemon_reload(bus, args, n)) < 0) |
4169 | r = q; | |
4170 | } | |
ee5762e3 LP |
4171 | } |
4172 | ||
4173 | finish: | |
4174 | install_info_hashmap_free(will_install); | |
4175 | install_info_hashmap_free(have_installed); | |
4176 | ||
4177 | set_free_free(remove_symlinks_to); | |
4178 | ||
4179 | lookup_paths_free(&paths); | |
4180 | ||
4181 | free(config_path); | |
4182 | ||
4183 | return r; | |
4184 | } | |
4185 | ||
e4b61340 | 4186 | static int systemctl_help(void) { |
7e4249b9 | 4187 | |
2e33c433 | 4188 | printf("%s [OPTIONS...] {COMMAND} ...\n\n" |
5632e374 | 4189 | "Send control commands to or query the systemd manager.\n\n" |
8a0867d6 LP |
4190 | " -h --help Show this help\n" |
4191 | " --version Show package version\n" | |
4192 | " -t --type=TYPE List only units of a particular type\n" | |
4193 | " -p --property=NAME Show only properties by this name\n" | |
4194 | " -a --all Show all units/properties, including dead/empty ones\n" | |
30732560 | 4195 | " --failed Show only failed units\n" |
8a0867d6 LP |
4196 | " --full Don't ellipsize unit names on output\n" |
4197 | " --fail When queueing a new job, fail if conflicting jobs are\n" | |
4198 | " pending\n" | |
e67c3609 LP |
4199 | " --ignore-dependencies\n" |
4200 | " When queueing a new job, ignore all its dependencies\n" | |
8a0867d6 LP |
4201 | " -q --quiet Suppress output\n" |
4202 | " --no-block Do not wait until operation finished\n" | |
0736af98 | 4203 | " --no-pager Do not pipe output into a pager.\n" |
af2d49f7 LP |
4204 | " --system Connect to system manager\n" |
4205 | " --user Connect to user service manager\n" | |
8a0867d6 LP |
4206 | " --order When generating graph for dot, show only order\n" |
4207 | " --require When generating graph for dot, show only requirement\n" | |
4208 | " --no-wall Don't send wall message before halt/power-off/reboot\n" | |
4209 | " --global Enable/disable unit files globally\n" | |
4210 | " --no-reload When enabling/disabling unit files, don't reload daemon\n" | |
4211 | " configuration\n" | |
501fc174 LP |
4212 | " --no-ask-password\n" |
4213 | " Do not ask for system passwords\n" | |
8a0867d6 LP |
4214 | " --kill-mode=MODE How to send signal\n" |
4215 | " --kill-who=WHO Who to send signal to\n" | |
4216 | " -s --signal=SIGNAL Which signal to send\n" | |
4217 | " -f --force When enabling unit files, override existing symlinks\n" | |
4218 | " When shutting down, execute action immediately\n" | |
4219 | " --defaults When disabling unit files, remove default symlinks only\n\n" | |
7e4249b9 LP |
4220 | "Commands:\n" |
4221 | " list-units List units\n" | |
ee5762e3 LP |
4222 | " start [NAME...] Start (activate) one or more units\n" |
4223 | " stop [NAME...] Stop (deactivate) one or more units\n" | |
7e4249b9 | 4224 | " reload [NAME...] Reload one or more units\n" |
6f28c033 LP |
4225 | " restart [NAME...] Start or restart one or more units\n" |
4226 | " try-restart [NAME...] Restart one or more units if active\n" | |
4227 | " reload-or-restart [NAME...] Reload one or more units is possible,\n" | |
4228 | " otherwise start or restart\n" | |
4229 | " reload-or-try-restart [NAME...] Reload one or more units is possible,\n" | |
4230 | " otherwise restart if active\n" | |
7e4249b9 | 4231 | " isolate [NAME] Start one unit and stop all others\n" |
8a0867d6 | 4232 | " kill [NAME...] Send signal to processes of a unit\n" |
ee5762e3 | 4233 | " is-active [NAME...] Check whether units are active\n" |
75676b72 | 4234 | " status [NAME...|PID...] Show runtime status of one or more units\n" |
6f28c033 | 4235 | " show [NAME...|JOB...] Show properties of one or more\n" |
ee5762e3 | 4236 | " units/jobs or the manager\n" |
fdf20a31 MM |
4237 | " reset-failed [NAME...] Reset failed state for all, one, or more\n" |
4238 | " units\n" | |
ee5762e3 LP |
4239 | " enable [NAME...] Enable one or more unit files\n" |
4240 | " disable [NAME...] Disable one or more unit files\n" | |
4241 | " is-enabled [NAME...] Check whether unit files are enabled\n" | |
48220598 LP |
4242 | " load [NAME...] Load one or more units\n" |
4243 | " list-jobs List jobs\n" | |
ee5762e3 | 4244 | " cancel [JOB...] Cancel all, one, or more jobs\n" |
7e4249b9 LP |
4245 | " monitor Monitor unit/job changes\n" |
4246 | " dump Dump server status\n" | |
4445a875 | 4247 | " dot Dump dependency graph for dot(1)\n" |
7e4249b9 | 4248 | " snapshot [NAME] Create a snapshot\n" |
6759e7a7 | 4249 | " delete [NAME...] Remove one or more snapshots\n" |
5ec7ed4e LP |
4250 | " daemon-reload Reload systemd manager configuration\n" |
4251 | " daemon-reexec Reexecute systemd manager\n" | |
7e4249b9 LP |
4252 | " show-environment Dump environment\n" |
4253 | " set-environment [NAME=VALUE...] Set one or more environment variables\n" | |
514f4ef5 | 4254 | " unset-environment [NAME...] Unset one or more environment variables\n" |
20b09ca7 LP |
4255 | " default Enter system default mode\n" |
4256 | " rescue Enter system rescue mode\n" | |
4257 | " emergency Enter system emergency mode\n" | |
514f4ef5 | 4258 | " halt Shut down and halt the system\n" |
2e33c433 | 4259 | " poweroff Shut down and power-off the system\n" |
514f4ef5 | 4260 | " reboot Shut down and reboot the system\n" |
20b09ca7 | 4261 | " kexec Shut down and reboot the system with kexec\n" |
af2d49f7 | 4262 | " exit Ask for user instance termination\n", |
5b6319dc | 4263 | program_invocation_short_name); |
7e4249b9 LP |
4264 | |
4265 | return 0; | |
4266 | } | |
4267 | ||
e4b61340 LP |
4268 | static int halt_help(void) { |
4269 | ||
2e33c433 | 4270 | printf("%s [OPTIONS...]\n\n" |
e4b61340 LP |
4271 | "%s the system.\n\n" |
4272 | " --help Show this help\n" | |
4273 | " --halt Halt the machine\n" | |
4274 | " -p --poweroff Switch off the machine\n" | |
4275 | " --reboot Reboot the machine\n" | |
2e33c433 LP |
4276 | " -f --force Force immediate halt/power-off/reboot\n" |
4277 | " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n" | |
e4b61340 | 4278 | " -d --no-wtmp Don't write wtmp record\n" |
2e33c433 LP |
4279 | " -n --no-sync Don't sync before halt/power-off/reboot\n" |
4280 | " --no-wall Don't send wall message before halt/power-off/reboot\n", | |
e4b61340 LP |
4281 | program_invocation_short_name, |
4282 | arg_action == ACTION_REBOOT ? "Reboot" : | |
4283 | arg_action == ACTION_POWEROFF ? "Power off" : | |
4284 | "Halt"); | |
4285 | ||
4286 | return 0; | |
4287 | } | |
4288 | ||
4289 | static int shutdown_help(void) { | |
4290 | ||
08e4b1c5 | 4291 | printf("%s [OPTIONS...] [TIME] [WALL...]\n\n" |
e4b61340 LP |
4292 | "Shut down the system.\n\n" |
4293 | " --help Show this help\n" | |
4294 | " -H --halt Halt the machine\n" | |
4295 | " -P --poweroff Power-off the machine\n" | |
4296 | " -r --reboot Reboot the machine\n" | |
4297 | " -h Equivalent to --poweroff, overriden by --halt\n" | |
2e33c433 | 4298 | " -k Don't halt/power-off/reboot, just send warnings\n" |
f6144808 | 4299 | " --no-wall Don't send wall message before halt/power-off/reboot\n" |
f6144808 | 4300 | " -c Cancel a pending shutdown\n", |
e4b61340 LP |
4301 | program_invocation_short_name); |
4302 | ||
4303 | return 0; | |
4304 | } | |
4305 | ||
4306 | static int telinit_help(void) { | |
4307 | ||
2e33c433 | 4308 | printf("%s [OPTIONS...] {COMMAND}\n\n" |
514f4ef5 LP |
4309 | "Send control commands to the init daemon.\n\n" |
4310 | " --help Show this help\n" | |
2e33c433 | 4311 | " --no-wall Don't send wall message before halt/power-off/reboot\n\n" |
e4b61340 LP |
4312 | "Commands:\n" |
4313 | " 0 Power-off the machine\n" | |
4314 | " 6 Reboot the machine\n" | |
514f4ef5 LP |
4315 | " 2, 3, 4, 5 Start runlevelX.target unit\n" |
4316 | " 1, s, S Enter rescue mode\n" | |
4317 | " q, Q Reload init daemon configuration\n" | |
4318 | " u, U Reexecute init daemon\n", | |
e4b61340 LP |
4319 | program_invocation_short_name); |
4320 | ||
4321 | return 0; | |
4322 | } | |
4323 | ||
4324 | static int runlevel_help(void) { | |
4325 | ||
2e33c433 | 4326 | printf("%s [OPTIONS...]\n\n" |
e4b61340 LP |
4327 | "Prints the previous and current runlevel of the init system.\n\n" |
4328 | " --help Show this help\n", | |
4329 | program_invocation_short_name); | |
4330 | ||
4331 | return 0; | |
4332 | } | |
4333 | ||
4334 | static int systemctl_parse_argv(int argc, char *argv[]) { | |
7e4249b9 LP |
4335 | |
4336 | enum { | |
90d473a1 | 4337 | ARG_FAIL = 0x100, |
e67c3609 | 4338 | ARG_IGNORE_DEPENDENCIES, |
35df8f27 | 4339 | ARG_VERSION, |
af2d49f7 | 4340 | ARG_USER, |
7e4249b9 | 4341 | ARG_SYSTEM, |
ee5762e3 | 4342 | ARG_GLOBAL, |
6e905d93 | 4343 | ARG_NO_BLOCK, |
611efaac | 4344 | ARG_NO_PAGER, |
4445a875 LP |
4345 | ARG_NO_WALL, |
4346 | ARG_ORDER, | |
8fe914ec | 4347 | ARG_REQUIRE, |
ee5762e3 | 4348 | ARG_FULL, |
ee5762e3 | 4349 | ARG_NO_RELOAD, |
8a0867d6 LP |
4350 | ARG_DEFAULTS, |
4351 | ARG_KILL_MODE, | |
501fc174 | 4352 | ARG_KILL_WHO, |
30732560 LP |
4353 | ARG_NO_ASK_PASSWORD, |
4354 | ARG_FAILED | |
7e4249b9 LP |
4355 | }; |
4356 | ||
4357 | static const struct option options[] = { | |
ee5762e3 | 4358 | { "help", no_argument, NULL, 'h' }, |
35df8f27 | 4359 | { "version", no_argument, NULL, ARG_VERSION }, |
ee5762e3 LP |
4360 | { "type", required_argument, NULL, 't' }, |
4361 | { "property", required_argument, NULL, 'p' }, | |
4362 | { "all", no_argument, NULL, 'a' }, | |
30732560 | 4363 | { "failed", no_argument, NULL, ARG_FAILED }, |
ee5762e3 LP |
4364 | { "full", no_argument, NULL, ARG_FULL }, |
4365 | { "fail", no_argument, NULL, ARG_FAIL }, | |
e67c3609 | 4366 | { "ignore-dependencies", no_argument, NULL, ARG_IGNORE_DEPENDENCIES }, |
af2d49f7 | 4367 | { "user", no_argument, NULL, ARG_USER }, |
ee5762e3 LP |
4368 | { "system", no_argument, NULL, ARG_SYSTEM }, |
4369 | { "global", no_argument, NULL, ARG_GLOBAL }, | |
4370 | { "no-block", no_argument, NULL, ARG_NO_BLOCK }, | |
0736af98 | 4371 | { "no-pager", no_argument, NULL, ARG_NO_PAGER }, |
ee5762e3 LP |
4372 | { "no-wall", no_argument, NULL, ARG_NO_WALL }, |
4373 | { "quiet", no_argument, NULL, 'q' }, | |
4374 | { "order", no_argument, NULL, ARG_ORDER }, | |
4375 | { "require", no_argument, NULL, ARG_REQUIRE }, | |
b4f27ccc | 4376 | { "force", no_argument, NULL, 'f' }, |
ee5762e3 | 4377 | { "no-reload", no_argument, NULL, ARG_NO_RELOAD }, |
f1e36d67 | 4378 | { "defaults", no_argument, NULL, ARG_DEFAULTS }, |
8a0867d6 LP |
4379 | { "kill-mode", required_argument, NULL, ARG_KILL_MODE }, |
4380 | { "kill-who", required_argument, NULL, ARG_KILL_WHO }, | |
4381 | { "signal", required_argument, NULL, 's' }, | |
501fc174 | 4382 | { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD }, |
ee5762e3 | 4383 | { NULL, 0, NULL, 0 } |
7e4249b9 LP |
4384 | }; |
4385 | ||
4386 | int c; | |
4387 | ||
e4b61340 | 4388 | assert(argc >= 0); |
7e4249b9 LP |
4389 | assert(argv); |
4390 | ||
501fc174 LP |
4391 | /* Only when running as systemctl we ask for passwords */ |
4392 | arg_ask_password = true; | |
4393 | ||
8a0867d6 | 4394 | while ((c = getopt_long(argc, argv, "ht:p:aqfs:", options, NULL)) >= 0) { |
7e4249b9 LP |
4395 | |
4396 | switch (c) { | |
4397 | ||
4398 | case 'h': | |
e4b61340 | 4399 | systemctl_help(); |
7e4249b9 | 4400 | return 0; |
35df8f27 LP |
4401 | |
4402 | case ARG_VERSION: | |
4403 | puts(PACKAGE_STRING); | |
7d568925 LP |
4404 | puts(DISTRIBUTION); |
4405 | puts(SYSTEMD_FEATURES); | |
35df8f27 | 4406 | return 0; |
7e4249b9 LP |
4407 | |
4408 | case 't': | |
4409 | arg_type = optarg; | |
4410 | break; | |
4411 | ||
ea4a240d LP |
4412 | case 'p': { |
4413 | char **l; | |
4414 | ||
4415 | if (!(l = strv_append(arg_property, optarg))) | |
4416 | return -ENOMEM; | |
4417 | ||
4418 | strv_free(arg_property); | |
4419 | arg_property = l; | |
48220598 LP |
4420 | |
4421 | /* If the user asked for a particular | |
4422 | * property, show it to him, even if it is | |
4423 | * empty. */ | |
4424 | arg_all = true; | |
4425 | break; | |
ea4a240d | 4426 | } |
48220598 | 4427 | |
7e4249b9 LP |
4428 | case 'a': |
4429 | arg_all = true; | |
4430 | break; | |
4431 | ||
90d473a1 | 4432 | case ARG_FAIL: |
e67c3609 LP |
4433 | arg_job_mode = "fail"; |
4434 | break; | |
4435 | ||
4436 | case ARG_IGNORE_DEPENDENCIES: | |
4437 | arg_job_mode = "ignore-dependencies"; | |
7e4249b9 LP |
4438 | break; |
4439 | ||
af2d49f7 LP |
4440 | case ARG_USER: |
4441 | arg_user = true; | |
7e4249b9 LP |
4442 | break; |
4443 | ||
4444 | case ARG_SYSTEM: | |
af2d49f7 | 4445 | arg_user = false; |
7e4249b9 LP |
4446 | break; |
4447 | ||
6e905d93 LP |
4448 | case ARG_NO_BLOCK: |
4449 | arg_no_block = true; | |
7e4249b9 LP |
4450 | break; |
4451 | ||
611efaac LP |
4452 | case ARG_NO_PAGER: |
4453 | arg_no_pager = true; | |
4454 | break; | |
0736af98 | 4455 | |
514f4ef5 LP |
4456 | case ARG_NO_WALL: |
4457 | arg_no_wall = true; | |
4458 | break; | |
4459 | ||
4445a875 LP |
4460 | case ARG_ORDER: |
4461 | arg_dot = DOT_ORDER; | |
4462 | break; | |
4463 | ||
4464 | case ARG_REQUIRE: | |
4465 | arg_dot = DOT_REQUIRE; | |
4466 | break; | |
4467 | ||
8fe914ec LP |
4468 | case ARG_FULL: |
4469 | arg_full = true; | |
4470 | break; | |
4471 | ||
30732560 LP |
4472 | case ARG_FAILED: |
4473 | arg_failed = true; | |
4474 | break; | |
4475 | ||
0183528f LP |
4476 | case 'q': |
4477 | arg_quiet = true; | |
4478 | break; | |
4479 | ||
b4f27ccc | 4480 | case 'f': |
ee5762e3 LP |
4481 | arg_force = true; |
4482 | break; | |
4483 | ||
4484 | case ARG_NO_RELOAD: | |
4485 | arg_no_reload = true; | |
4486 | break; | |
4487 | ||
4488 | case ARG_GLOBAL: | |
4489 | arg_global = true; | |
af2d49f7 | 4490 | arg_user = true; |
ee5762e3 LP |
4491 | break; |
4492 | ||
4493 | case ARG_DEFAULTS: | |
4494 | arg_defaults = true; | |
4495 | break; | |
4496 | ||
8a0867d6 LP |
4497 | case ARG_KILL_WHO: |
4498 | arg_kill_who = optarg; | |
4499 | break; | |
4500 | ||
4501 | case ARG_KILL_MODE: | |
4502 | arg_kill_mode = optarg; | |
4503 | break; | |
4504 | ||
4505 | case 's': | |
4506 | if ((arg_signal = signal_from_string_try_harder(optarg)) < 0) { | |
4507 | log_error("Failed to parse signal string %s.", optarg); | |
4508 | return -EINVAL; | |
4509 | } | |
4510 | break; | |
4511 | ||
501fc174 LP |
4512 | case ARG_NO_ASK_PASSWORD: |
4513 | arg_ask_password = false; | |
4514 | break; | |
4515 | ||
7e4249b9 LP |
4516 | case '?': |
4517 | return -EINVAL; | |
4518 | ||
4519 | default: | |
4520 | log_error("Unknown option code %c", c); | |
4521 | return -EINVAL; | |
4522 | } | |
4523 | } | |
4524 | ||
4525 | return 1; | |
4526 | } | |
4527 | ||
e4b61340 LP |
4528 | static int halt_parse_argv(int argc, char *argv[]) { |
4529 | ||
4530 | enum { | |
4531 | ARG_HELP = 0x100, | |
4532 | ARG_HALT, | |
514f4ef5 LP |
4533 | ARG_REBOOT, |
4534 | ARG_NO_WALL | |
e4b61340 LP |
4535 | }; |
4536 | ||
4537 | static const struct option options[] = { | |
4538 | { "help", no_argument, NULL, ARG_HELP }, | |
4539 | { "halt", no_argument, NULL, ARG_HALT }, | |
4540 | { "poweroff", no_argument, NULL, 'p' }, | |
4541 | { "reboot", no_argument, NULL, ARG_REBOOT }, | |
4542 | { "force", no_argument, NULL, 'f' }, | |
4543 | { "wtmp-only", no_argument, NULL, 'w' }, | |
4544 | { "no-wtmp", no_argument, NULL, 'd' }, | |
4545 | { "no-sync", no_argument, NULL, 'n' }, | |
514f4ef5 | 4546 | { "no-wall", no_argument, NULL, ARG_NO_WALL }, |
e4b61340 LP |
4547 | { NULL, 0, NULL, 0 } |
4548 | }; | |
4549 | ||
4550 | int c, runlevel; | |
4551 | ||
4552 | assert(argc >= 0); | |
4553 | assert(argv); | |
4554 | ||
4555 | if (utmp_get_runlevel(&runlevel, NULL) >= 0) | |
4556 | if (runlevel == '0' || runlevel == '6') | |
4557 | arg_immediate = true; | |
4558 | ||
4559 | while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) { | |
4560 | switch (c) { | |
4561 | ||
4562 | case ARG_HELP: | |
4563 | halt_help(); | |
4564 | return 0; | |
4565 | ||
4566 | case ARG_HALT: | |
4567 | arg_action = ACTION_HALT; | |
4568 | break; | |
4569 | ||
4570 | case 'p': | |
a042efad MS |
4571 | if (arg_action != ACTION_REBOOT) |
4572 | arg_action = ACTION_POWEROFF; | |
e4b61340 LP |
4573 | break; |
4574 | ||
4575 | case ARG_REBOOT: | |
4576 | arg_action = ACTION_REBOOT; | |
4577 | break; | |
4578 | ||
4579 | case 'f': | |
4580 | arg_immediate = true; | |
4581 | break; | |
4582 | ||
4583 | case 'w': | |
4584 | arg_dry = true; | |
4585 | break; | |
4586 | ||
4587 | case 'd': | |
4588 | arg_no_wtmp = true; | |
4589 | break; | |
4590 | ||
4591 | case 'n': | |
4592 | arg_no_sync = true; | |
4593 | break; | |
4594 | ||
514f4ef5 LP |
4595 | case ARG_NO_WALL: |
4596 | arg_no_wall = true; | |
4597 | break; | |
4598 | ||
e4b61340 LP |
4599 | case 'i': |
4600 | case 'h': | |
4601 | /* Compatibility nops */ | |
4602 | break; | |
4603 | ||
4604 | case '?': | |
4605 | return -EINVAL; | |
4606 | ||
4607 | default: | |
4608 | log_error("Unknown option code %c", c); | |
4609 | return -EINVAL; | |
4610 | } | |
4611 | } | |
4612 | ||
4613 | if (optind < argc) { | |
4614 | log_error("Too many arguments."); | |
4615 | return -EINVAL; | |
4616 | } | |
4617 | ||
4618 | return 1; | |
4619 | } | |
4620 | ||
f6144808 LP |
4621 | static int parse_time_spec(const char *t, usec_t *_u) { |
4622 | assert(t); | |
4623 | assert(_u); | |
4624 | ||
4625 | if (streq(t, "now")) | |
4626 | *_u = 0; | |
4627 | else if (t[0] == '+') { | |
4628 | uint64_t u; | |
4629 | ||
4630 | if (safe_atou64(t + 1, &u) < 0) | |
4631 | return -EINVAL; | |
4632 | ||
4633 | *_u = now(CLOCK_REALTIME) + USEC_PER_MINUTE * u; | |
4634 | } else { | |
4635 | char *e = NULL; | |
4636 | long hour, minute; | |
4637 | struct tm tm; | |
4638 | time_t s; | |
4639 | usec_t n; | |
4640 | ||
4641 | errno = 0; | |
4642 | hour = strtol(t, &e, 10); | |
4643 | if (errno != 0 || *e != ':' || hour < 0 || hour > 23) | |
4644 | return -EINVAL; | |
4645 | ||
4646 | minute = strtol(e+1, &e, 10); | |
4647 | if (errno != 0 || *e != 0 || minute < 0 || minute > 59) | |
4648 | return -EINVAL; | |
4649 | ||
4650 | n = now(CLOCK_REALTIME); | |
08e4b1c5 LP |
4651 | s = (time_t) (n / USEC_PER_SEC); |
4652 | ||
4653 | zero(tm); | |
f6144808 LP |
4654 | assert_se(localtime_r(&s, &tm)); |
4655 | ||
4656 | tm.tm_hour = (int) hour; | |
4657 | tm.tm_min = (int) minute; | |
08e4b1c5 | 4658 | tm.tm_sec = 0; |
f6144808 LP |
4659 | |
4660 | assert_se(s = mktime(&tm)); | |
4661 | ||
4662 | *_u = (usec_t) s * USEC_PER_SEC; | |
4663 | ||
4664 | while (*_u <= n) | |
4665 | *_u += USEC_PER_DAY; | |
4666 | } | |
4667 | ||
4668 | return 0; | |
4669 | } | |
4670 | ||
e4b61340 LP |
4671 | static int shutdown_parse_argv(int argc, char *argv[]) { |
4672 | ||
4673 | enum { | |
4674 | ARG_HELP = 0x100, | |
514f4ef5 | 4675 | ARG_NO_WALL |
e4b61340 LP |
4676 | }; |
4677 | ||
4678 | static const struct option options[] = { | |
4679 | { "help", no_argument, NULL, ARG_HELP }, | |
4680 | { "halt", no_argument, NULL, 'H' }, | |
4681 | { "poweroff", no_argument, NULL, 'P' }, | |
4682 | { "reboot", no_argument, NULL, 'r' }, | |
514f4ef5 | 4683 | { "no-wall", no_argument, NULL, ARG_NO_WALL }, |
e4b61340 LP |
4684 | { NULL, 0, NULL, 0 } |
4685 | }; | |
4686 | ||
f6144808 | 4687 | int c, r; |
e4b61340 LP |
4688 | |
4689 | assert(argc >= 0); | |
4690 | assert(argv); | |
4691 | ||
f6144808 | 4692 | while ((c = getopt_long(argc, argv, "HPrhkt:afFc", options, NULL)) >= 0) { |
e4b61340 LP |
4693 | switch (c) { |
4694 | ||
4695 | case ARG_HELP: | |
4696 | shutdown_help(); | |
4697 | return 0; | |
4698 | ||
4699 | case 'H': | |
4700 | arg_action = ACTION_HALT; | |
4701 | break; | |
4702 | ||
4703 | case 'P': | |
4704 | arg_action = ACTION_POWEROFF; | |
4705 | break; | |
4706 | ||
4707 | case 'r': | |
4708 | arg_action = ACTION_REBOOT; | |
4709 | break; | |
4710 | ||
4711 | case 'h': | |
4712 | if (arg_action != ACTION_HALT) | |
4713 | arg_action = ACTION_POWEROFF; | |
4714 | break; | |
4715 | ||
4716 | case 'k': | |
4717 | arg_dry = true; | |
4718 | break; | |
4719 | ||
514f4ef5 LP |
4720 | case ARG_NO_WALL: |
4721 | arg_no_wall = true; | |
4722 | break; | |
4723 | ||
e4b61340 LP |
4724 | case 't': |
4725 | case 'a': | |
4726 | /* Compatibility nops */ | |
4727 | break; | |
4728 | ||
f6144808 LP |
4729 | case 'c': |
4730 | arg_action = ACTION_CANCEL_SHUTDOWN; | |
4731 | break; | |
4732 | ||
e4b61340 LP |
4733 | case '?': |
4734 | return -EINVAL; | |
4735 | ||
4736 | default: | |
4737 | log_error("Unknown option code %c", c); | |
4738 | return -EINVAL; | |
4739 | } | |
4740 | } | |
4741 | ||
6b5ad000 | 4742 | if (argc > optind) { |
f6144808 LP |
4743 | if ((r = parse_time_spec(argv[optind], &arg_when)) < 0) { |
4744 | log_error("Failed to parse time specification: %s", argv[optind]); | |
4745 | return r; | |
4746 | } | |
6b5ad000 | 4747 | } else |
08e4b1c5 | 4748 | arg_when = now(CLOCK_REALTIME) + USEC_PER_MINUTE; |
442b9094 | 4749 | |
f6144808 | 4750 | /* We skip the time argument */ |
e4b61340 LP |
4751 | if (argc > optind + 1) |
4752 | arg_wall = argv + optind + 1; | |
4753 | ||
4754 | optind = argc; | |
4755 | ||
4756 | return 1; | |
e4b61340 LP |
4757 | } |
4758 | ||
4759 | static int telinit_parse_argv(int argc, char *argv[]) { | |
4760 | ||
4761 | enum { | |
4762 | ARG_HELP = 0x100, | |
514f4ef5 | 4763 | ARG_NO_WALL |
e4b61340 LP |
4764 | }; |
4765 | ||
4766 | static const struct option options[] = { | |
4767 | { "help", no_argument, NULL, ARG_HELP }, | |
514f4ef5 | 4768 | { "no-wall", no_argument, NULL, ARG_NO_WALL }, |
e4b61340 LP |
4769 | { NULL, 0, NULL, 0 } |
4770 | }; | |
4771 | ||
4772 | static const struct { | |
4773 | char from; | |
4774 | enum action to; | |
4775 | } table[] = { | |
4776 | { '0', ACTION_POWEROFF }, | |
4777 | { '6', ACTION_REBOOT }, | |
ef2f1067 | 4778 | { '1', ACTION_RESCUE }, |
e4b61340 LP |
4779 | { '2', ACTION_RUNLEVEL2 }, |
4780 | { '3', ACTION_RUNLEVEL3 }, | |
4781 | { '4', ACTION_RUNLEVEL4 }, | |
4782 | { '5', ACTION_RUNLEVEL5 }, | |
4783 | { 's', ACTION_RESCUE }, | |
4784 | { 'S', ACTION_RESCUE }, | |
4785 | { 'q', ACTION_RELOAD }, | |
4786 | { 'Q', ACTION_RELOAD }, | |
4787 | { 'u', ACTION_REEXEC }, | |
4788 | { 'U', ACTION_REEXEC } | |
4789 | }; | |
4790 | ||
4791 | unsigned i; | |
4792 | int c; | |
4793 | ||
4794 | assert(argc >= 0); | |
4795 | assert(argv); | |
4796 | ||
4797 | while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) { | |
4798 | switch (c) { | |
4799 | ||
4800 | case ARG_HELP: | |
4801 | telinit_help(); | |
4802 | return 0; | |
4803 | ||
514f4ef5 LP |
4804 | case ARG_NO_WALL: |
4805 | arg_no_wall = true; | |
4806 | break; | |
4807 | ||
e4b61340 LP |
4808 | case '?': |
4809 | return -EINVAL; | |
4810 | ||
4811 | default: | |
4812 | log_error("Unknown option code %c", c); | |
4813 | return -EINVAL; | |
4814 | } | |
4815 | } | |
4816 | ||
4817 | if (optind >= argc) { | |
2f02ce40 | 4818 | telinit_help(); |
e4b61340 LP |
4819 | return -EINVAL; |
4820 | } | |
4821 | ||
4822 | if (optind + 1 < argc) { | |
4823 | log_error("Too many arguments."); | |
4824 | return -EINVAL; | |
4825 | } | |
4826 | ||
4827 | if (strlen(argv[optind]) != 1) { | |
4828 | log_error("Expected single character argument."); | |
4829 | return -EINVAL; | |
4830 | } | |
4831 | ||
4832 | for (i = 0; i < ELEMENTSOF(table); i++) | |
4833 | if (table[i].from == argv[optind][0]) | |
4834 | break; | |
4835 | ||
4836 | if (i >= ELEMENTSOF(table)) { | |
4837 | log_error("Unknown command %s.", argv[optind]); | |
4838 | return -EINVAL; | |
4839 | } | |
4840 | ||
4841 | arg_action = table[i].to; | |
4842 | ||
4843 | optind ++; | |
4844 | ||
4845 | return 1; | |
4846 | } | |
4847 | ||
4848 | static int runlevel_parse_argv(int argc, char *argv[]) { | |
4849 | ||
4850 | enum { | |
4851 | ARG_HELP = 0x100, | |
4852 | }; | |
4853 | ||
4854 | static const struct option options[] = { | |
4855 | { "help", no_argument, NULL, ARG_HELP }, | |
4856 | { NULL, 0, NULL, 0 } | |
4857 | }; | |
4858 | ||
4859 | int c; | |
4860 | ||
4861 | assert(argc >= 0); | |
4862 | assert(argv); | |
4863 | ||
4864 | while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) { | |
4865 | switch (c) { | |
4866 | ||
4867 | case ARG_HELP: | |
4868 | runlevel_help(); | |
4869 | return 0; | |
4870 | ||
4871 | case '?': | |
4872 | return -EINVAL; | |
4873 | ||
4874 | default: | |
4875 | log_error("Unknown option code %c", c); | |
4876 | return -EINVAL; | |
4877 | } | |
4878 | } | |
4879 | ||
4880 | if (optind < argc) { | |
4881 | log_error("Too many arguments."); | |
4882 | return -EINVAL; | |
4883 | } | |
4884 | ||
4885 | return 1; | |
4886 | } | |
4887 | ||
4888 | static int parse_argv(int argc, char *argv[]) { | |
4889 | assert(argc >= 0); | |
4890 | assert(argv); | |
4891 | ||
4892 | if (program_invocation_short_name) { | |
4893 | ||
4894 | if (strstr(program_invocation_short_name, "halt")) { | |
4895 | arg_action = ACTION_HALT; | |
4896 | return halt_parse_argv(argc, argv); | |
4897 | } else if (strstr(program_invocation_short_name, "poweroff")) { | |
4898 | arg_action = ACTION_POWEROFF; | |
4899 | return halt_parse_argv(argc, argv); | |
4900 | } else if (strstr(program_invocation_short_name, "reboot")) { | |
4901 | arg_action = ACTION_REBOOT; | |
4902 | return halt_parse_argv(argc, argv); | |
4903 | } else if (strstr(program_invocation_short_name, "shutdown")) { | |
4904 | arg_action = ACTION_POWEROFF; | |
4905 | return shutdown_parse_argv(argc, argv); | |
4906 | } else if (strstr(program_invocation_short_name, "init")) { | |
d5ca5f11 LP |
4907 | |
4908 | if (sd_booted() > 0) { | |
4909 | arg_action = ACTION_INVALID; | |
4910 | return telinit_parse_argv(argc, argv); | |
4911 | } else { | |
4912 | /* Hmm, so some other init system is | |
4913 | * running, we need to forward this | |
4914 | * request to it. For now we simply | |
4915 | * guess that it is Upstart. */ | |
4916 | ||
4917 | execv("/lib/upstart/telinit", argv); | |
4918 | ||
4919 | log_error("Couldn't find an alternative telinit implementation to spawn."); | |
4920 | return -EIO; | |
4921 | } | |
4922 | ||
e4b61340 LP |
4923 | } else if (strstr(program_invocation_short_name, "runlevel")) { |
4924 | arg_action = ACTION_RUNLEVEL; | |
4925 | return runlevel_parse_argv(argc, argv); | |
4926 | } | |
4927 | } | |
4928 | ||
4929 | arg_action = ACTION_SYSTEMCTL; | |
4930 | return systemctl_parse_argv(argc, argv); | |
4931 | } | |
4932 | ||
d55ae9e6 | 4933 | static int action_to_runlevel(void) { |
eb22ac37 LP |
4934 | |
4935 | static const char table[_ACTION_MAX] = { | |
4936 | [ACTION_HALT] = '0', | |
4937 | [ACTION_POWEROFF] = '0', | |
4938 | [ACTION_REBOOT] = '6', | |
4939 | [ACTION_RUNLEVEL2] = '2', | |
4940 | [ACTION_RUNLEVEL3] = '3', | |
4941 | [ACTION_RUNLEVEL4] = '4', | |
4942 | [ACTION_RUNLEVEL5] = '5', | |
4943 | [ACTION_RESCUE] = '1' | |
4944 | }; | |
4945 | ||
d55ae9e6 LP |
4946 | assert(arg_action < _ACTION_MAX); |
4947 | ||
4948 | return table[arg_action]; | |
4949 | } | |
4950 | ||
f1c5860b | 4951 | static int talk_upstart(void) { |
d55ae9e6 LP |
4952 | DBusMessage *m = NULL, *reply = NULL; |
4953 | DBusError error; | |
4954 | int previous, rl, r; | |
4955 | char | |
4956 | env1_buf[] = "RUNLEVEL=X", | |
4957 | env2_buf[] = "PREVLEVEL=X"; | |
4958 | char *env1 = env1_buf, *env2 = env2_buf; | |
4959 | const char *emit = "runlevel"; | |
4960 | dbus_bool_t b_false = FALSE; | |
4961 | DBusMessageIter iter, sub; | |
f1c5860b | 4962 | DBusConnection *bus; |
d55ae9e6 LP |
4963 | |
4964 | dbus_error_init(&error); | |
4965 | ||
4966 | if (!(rl = action_to_runlevel())) | |
4967 | return 0; | |
4968 | ||
4969 | if (utmp_get_runlevel(&previous, NULL) < 0) | |
4970 | previous = 'N'; | |
4971 | ||
b574246b | 4972 | if (!(bus = dbus_connection_open_private("unix:abstract=/com/ubuntu/upstart", &error))) { |
f1c5860b LP |
4973 | if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) { |
4974 | r = 0; | |
4975 | goto finish; | |
4976 | } | |
4977 | ||
4cf5d675 | 4978 | log_error("Failed to connect to Upstart bus: %s", bus_error_message(&error)); |
f1c5860b LP |
4979 | r = -EIO; |
4980 | goto finish; | |
4981 | } | |
4982 | ||
4983 | if ((r = bus_check_peercred(bus)) < 0) { | |
4984 | log_error("Failed to verify owner of bus."); | |
4985 | goto finish; | |
4986 | } | |
4987 | ||
d55ae9e6 LP |
4988 | if (!(m = dbus_message_new_method_call( |
4989 | "com.ubuntu.Upstart", | |
4990 | "/com/ubuntu/Upstart", | |
4991 | "com.ubuntu.Upstart0_6", | |
4992 | "EmitEvent"))) { | |
4993 | ||
4994 | log_error("Could not allocate message."); | |
f1c5860b LP |
4995 | r = -ENOMEM; |
4996 | goto finish; | |
d55ae9e6 LP |
4997 | } |
4998 | ||
4999 | dbus_message_iter_init_append(m, &iter); | |
5000 | ||
5001 | env1_buf[sizeof(env1_buf)-2] = rl; | |
5002 | env2_buf[sizeof(env2_buf)-2] = previous; | |
5003 | ||
5004 | if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) || | |
5005 | !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) || | |
5006 | !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) || | |
5007 | !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) || | |
5008 | !dbus_message_iter_close_container(&iter, &sub) || | |
5009 | !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) { | |
5010 | log_error("Could not append arguments to message."); | |
5011 | r = -ENOMEM; | |
5012 | goto finish; | |
5013 | } | |
5014 | ||
5015 | if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) { | |
5016 | ||
5017 | if (error_is_no_service(&error)) { | |
aabd9b11 | 5018 | r = -EADDRNOTAVAIL; |
d55ae9e6 LP |
5019 | goto finish; |
5020 | } | |
5021 | ||
4cf5d675 | 5022 | log_error("Failed to issue method call: %s", bus_error_message(&error)); |
d55ae9e6 LP |
5023 | r = -EIO; |
5024 | goto finish; | |
5025 | } | |
5026 | ||
aabd9b11 | 5027 | r = 0; |
d55ae9e6 LP |
5028 | |
5029 | finish: | |
5030 | if (m) | |
5031 | dbus_message_unref(m); | |
5032 | ||
5033 | if (reply) | |
5034 | dbus_message_unref(reply); | |
5035 | ||
b574246b | 5036 | if (bus) { |
5d452f9c | 5037 | dbus_connection_flush(bus); |
b574246b | 5038 | dbus_connection_close(bus); |
f1c5860b | 5039 | dbus_connection_unref(bus); |
b574246b | 5040 | } |
f1c5860b | 5041 | |
d55ae9e6 LP |
5042 | dbus_error_free(&error); |
5043 | ||
5044 | return r; | |
5045 | } | |
5046 | ||
5047 | static int talk_initctl(void) { | |
eb22ac37 LP |
5048 | struct init_request request; |
5049 | int r, fd; | |
d55ae9e6 | 5050 | char rl; |
eb22ac37 | 5051 | |
d55ae9e6 | 5052 | if (!(rl = action_to_runlevel())) |
eb22ac37 LP |
5053 | return 0; |
5054 | ||
5055 | zero(request); | |
5056 | request.magic = INIT_MAGIC; | |
5057 | request.sleeptime = 0; | |
5058 | request.cmd = INIT_CMD_RUNLVL; | |
d55ae9e6 LP |
5059 | request.runlevel = rl; |
5060 | ||
5061 | if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) { | |
5062 | ||
5063 | if (errno == ENOENT) | |
5064 | return 0; | |
eb22ac37 | 5065 | |
d55ae9e6 | 5066 | log_error("Failed to open "INIT_FIFO": %m"); |
eb22ac37 | 5067 | return -errno; |
d55ae9e6 | 5068 | } |
eb22ac37 | 5069 | |
d55ae9e6 | 5070 | errno = 0; |
eb22ac37 LP |
5071 | r = loop_write(fd, &request, sizeof(request), false) != sizeof(request); |
5072 | close_nointr_nofail(fd); | |
5073 | ||
d55ae9e6 LP |
5074 | if (r < 0) { |
5075 | log_error("Failed to write to "INIT_FIFO": %m"); | |
eb22ac37 | 5076 | return errno ? -errno : -EIO; |
d55ae9e6 | 5077 | } |
eb22ac37 LP |
5078 | |
5079 | return 1; | |
e4b61340 LP |
5080 | } |
5081 | ||
ee5762e3 | 5082 | static int systemctl_main(DBusConnection *bus, int argc, char *argv[], DBusError *error) { |
7e4249b9 | 5083 | |
7e4249b9 LP |
5084 | static const struct { |
5085 | const char* verb; | |
5086 | const enum { | |
5087 | MORE, | |
5088 | LESS, | |
5089 | EQUAL | |
5090 | } argc_cmp; | |
5091 | const int argc; | |
5092 | int (* const dispatch)(DBusConnection *bus, char **args, unsigned n); | |
5093 | } verbs[] = { | |
ee5762e3 LP |
5094 | { "list-units", LESS, 1, list_units }, |
5095 | { "list-jobs", EQUAL, 1, list_jobs }, | |
5096 | { "clear-jobs", EQUAL, 1, daemon_reload }, | |
5097 | { "load", MORE, 2, load_unit }, | |
5098 | { "cancel", MORE, 2, cancel_job }, | |
5099 | { "start", MORE, 2, start_unit }, | |
5100 | { "stop", MORE, 2, start_unit }, | |
5101 | { "reload", MORE, 2, start_unit }, | |
5102 | { "restart", MORE, 2, start_unit }, | |
5103 | { "try-restart", MORE, 2, start_unit }, | |
5104 | { "reload-or-restart", MORE, 2, start_unit }, | |
5105 | { "reload-or-try-restart", MORE, 2, start_unit }, | |
5106 | { "force-reload", MORE, 2, start_unit }, /* For compatibility with SysV */ | |
5107 | { "condrestart", MORE, 2, start_unit }, /* For compatibility with RH */ | |
5108 | { "isolate", EQUAL, 2, start_unit }, | |
8a0867d6 | 5109 | { "kill", MORE, 2, kill_unit }, |
ee5762e3 LP |
5110 | { "is-active", MORE, 2, check_unit }, |
5111 | { "check", MORE, 2, check_unit }, | |
5112 | { "show", MORE, 1, show }, | |
5113 | { "status", MORE, 2, show }, | |
5114 | { "monitor", EQUAL, 1, monitor }, | |
5115 | { "dump", EQUAL, 1, dump }, | |
5116 | { "dot", EQUAL, 1, dot }, | |
5117 | { "snapshot", LESS, 2, snapshot }, | |
5118 | { "delete", MORE, 2, delete_snapshot }, | |
5119 | { "daemon-reload", EQUAL, 1, daemon_reload }, | |
5120 | { "daemon-reexec", EQUAL, 1, daemon_reload }, | |
ee5762e3 LP |
5121 | { "show-environment", EQUAL, 1, show_enviroment }, |
5122 | { "set-environment", MORE, 2, set_environment }, | |
5123 | { "unset-environment", MORE, 2, set_environment }, | |
5124 | { "halt", EQUAL, 1, start_special }, | |
5125 | { "poweroff", EQUAL, 1, start_special }, | |
5126 | { "reboot", EQUAL, 1, start_special }, | |
20b09ca7 | 5127 | { "kexec", EQUAL, 1, start_special }, |
ee5762e3 LP |
5128 | { "default", EQUAL, 1, start_special }, |
5129 | { "rescue", EQUAL, 1, start_special }, | |
5130 | { "emergency", EQUAL, 1, start_special }, | |
20b09ca7 | 5131 | { "exit", EQUAL, 1, start_special }, |
fdf20a31 | 5132 | { "reset-failed", MORE, 1, reset_failed }, |
ee5762e3 LP |
5133 | { "enable", MORE, 2, enable_unit }, |
5134 | { "disable", MORE, 2, enable_unit }, | |
5135 | { "is-enabled", MORE, 2, enable_unit } | |
7e4249b9 LP |
5136 | }; |
5137 | ||
e4b61340 | 5138 | int left; |
7e4249b9 | 5139 | unsigned i; |
7e4249b9 | 5140 | |
e4b61340 LP |
5141 | assert(argc >= 0); |
5142 | assert(argv); | |
ee5762e3 | 5143 | assert(error); |
7e4249b9 LP |
5144 | |
5145 | left = argc - optind; | |
5146 | ||
5147 | if (left <= 0) | |
5148 | /* Special rule: no arguments means "list-units" */ | |
5149 | i = 0; | |
5150 | else { | |
0183528f LP |
5151 | if (streq(argv[optind], "help")) { |
5152 | systemctl_help(); | |
5153 | return 0; | |
5154 | } | |
5155 | ||
7e4249b9 LP |
5156 | for (i = 0; i < ELEMENTSOF(verbs); i++) |
5157 | if (streq(argv[optind], verbs[i].verb)) | |
5158 | break; | |
5159 | ||
5160 | if (i >= ELEMENTSOF(verbs)) { | |
5161 | log_error("Unknown operation %s", argv[optind]); | |
e4b61340 | 5162 | return -EINVAL; |
7e4249b9 LP |
5163 | } |
5164 | } | |
5165 | ||
5166 | switch (verbs[i].argc_cmp) { | |
5167 | ||
5168 | case EQUAL: | |
5169 | if (left != verbs[i].argc) { | |
5170 | log_error("Invalid number of arguments."); | |
e4b61340 | 5171 | return -EINVAL; |
7e4249b9 LP |
5172 | } |
5173 | ||
5174 | break; | |
5175 | ||
5176 | case MORE: | |
5177 | if (left < verbs[i].argc) { | |
5178 | log_error("Too few arguments."); | |
e4b61340 | 5179 | return -EINVAL; |
7e4249b9 LP |
5180 | } |
5181 | ||
5182 | break; | |
5183 | ||
5184 | case LESS: | |
5185 | if (left > verbs[i].argc) { | |
5186 | log_error("Too many arguments."); | |
e4b61340 | 5187 | return -EINVAL; |
7e4249b9 LP |
5188 | } |
5189 | ||
5190 | break; | |
5191 | ||
5192 | default: | |
5193 | assert_not_reached("Unknown comparison operator."); | |
5194 | } | |
5195 | ||
ee5762e3 LP |
5196 | /* Require a bus connection for all operations but |
5197 | * enable/disable */ | |
5198 | if (!streq(verbs[i].verb, "enable") && | |
5199 | !streq(verbs[i].verb, "disable") && | |
5200 | !bus) { | |
5201 | log_error("Failed to get D-Bus connection: %s", error->message); | |
5202 | return -EIO; | |
5203 | } | |
5204 | ||
e4b61340 LP |
5205 | return verbs[i].dispatch(bus, argv + optind, left); |
5206 | } | |
5207 | ||
9be9828c | 5208 | static int send_shutdownd(usec_t t, char mode, bool warn, const char *message) { |
f6144808 LP |
5209 | int fd = -1; |
5210 | struct msghdr msghdr; | |
5211 | struct iovec iovec; | |
5212 | union sockaddr_union sockaddr; | |
f6144808 LP |
5213 | struct shutdownd_command c; |
5214 | ||
5215 | zero(c); | |
5216 | c.elapse = t; | |
5217 | c.mode = mode; | |
9be9828c LP |
5218 | c.warn_wall = warn; |
5219 | ||
5220 | if (message) | |
5221 | strncpy(c.wall_message, message, sizeof(c.wall_message)); | |
f6144808 LP |
5222 | |
5223 | if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0) | |
5224 | return -errno; | |
5225 | ||
5226 | zero(sockaddr); | |
5227 | sockaddr.sa.sa_family = AF_UNIX; | |
5228 | sockaddr.un.sun_path[0] = 0; | |
5229 | strncpy(sockaddr.un.sun_path+1, "/org/freedesktop/systemd1/shutdownd", sizeof(sockaddr.un.sun_path)-1); | |
5230 | ||
5231 | zero(iovec); | |
5232 | iovec.iov_base = (char*) &c; | |
5233 | iovec.iov_len = sizeof(c); | |
5234 | ||
f6144808 LP |
5235 | zero(msghdr); |
5236 | msghdr.msg_name = &sockaddr; | |
0e098b15 | 5237 | msghdr.msg_namelen = offsetof(struct sockaddr_un, sun_path) + 1 + sizeof("/org/freedesktop/systemd1/shutdownd") - 1; |
f6144808 LP |
5238 | |
5239 | msghdr.msg_iov = &iovec; | |
5240 | msghdr.msg_iovlen = 1; | |
f6144808 LP |
5241 | |
5242 | if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) < 0) { | |
5243 | close_nointr_nofail(fd); | |
5244 | return -errno; | |
5245 | } | |
5246 | ||
5247 | close_nointr_nofail(fd); | |
5248 | return 0; | |
5249 | } | |
5250 | ||
e4b61340 | 5251 | static int reload_with_fallback(DBusConnection *bus) { |
e4b61340 LP |
5252 | |
5253 | if (bus) { | |
5254 | /* First, try systemd via D-Bus. */ | |
e364ad06 | 5255 | if (daemon_reload(bus, NULL, 0) > 0) |
e4b61340 LP |
5256 | return 0; |
5257 | } | |
5258 | ||
5259 | /* Nothing else worked, so let's try signals */ | |
5260 | assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC); | |
5261 | ||
5262 | if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) { | |
5263 | log_error("kill() failed: %m"); | |
5264 | return -errno; | |
5265 | } | |
5266 | ||
5267 | return 0; | |
5268 | } | |
5269 | ||
5270 | static int start_with_fallback(DBusConnection *bus) { | |
e4b61340 LP |
5271 | |
5272 | if (bus) { | |
5273 | /* First, try systemd via D-Bus. */ | |
706900b7 | 5274 | if (start_unit(bus, NULL, 0) >= 0) |
983d9c90 | 5275 | goto done; |
e4b61340 LP |
5276 | } |
5277 | ||
ec7f7f20 LP |
5278 | /* Hmm, talking to systemd via D-Bus didn't work. Then |
5279 | * let's try to talk to Upstart via D-Bus. */ | |
e364ad06 | 5280 | if (talk_upstart() > 0) |
ec7f7f20 LP |
5281 | goto done; |
5282 | ||
e4b61340 LP |
5283 | /* Nothing else worked, so let's try |
5284 | * /dev/initctl */ | |
fbc43921 | 5285 | if (talk_initctl() > 0) |
983d9c90 | 5286 | goto done; |
d55ae9e6 LP |
5287 | |
5288 | log_error("Failed to talk to init daemon."); | |
5289 | return -EIO; | |
983d9c90 LP |
5290 | |
5291 | done: | |
5292 | warn_wall(arg_action); | |
5293 | return 0; | |
e4b61340 LP |
5294 | } |
5295 | ||
5296 | static int halt_main(DBusConnection *bus) { | |
5297 | int r; | |
5298 | ||
bc8c2f5c | 5299 | if (geteuid() != 0) { |
cc8a7a61 | 5300 | log_error("Must be root."); |
bc8c2f5c LP |
5301 | return -EPERM; |
5302 | } | |
5303 | ||
f6144808 | 5304 | if (arg_when > 0) { |
9be9828c | 5305 | char *m; |
08e4b1c5 | 5306 | char date[FORMAT_TIMESTAMP_MAX]; |
9be9828c LP |
5307 | |
5308 | m = strv_join(arg_wall, " "); | |
5309 | r = send_shutdownd(arg_when, | |
5310 | arg_action == ACTION_HALT ? 'H' : | |
5311 | arg_action == ACTION_POWEROFF ? 'P' : | |
5312 | 'r', | |
5313 | !arg_no_wall, | |
5314 | m); | |
5315 | free(m); | |
5316 | ||
5317 | if (r < 0) | |
f6144808 | 5318 | log_warning("Failed to talk to shutdownd, proceeding with immediate shutdown: %s", strerror(-r)); |
08e4b1c5 LP |
5319 | else { |
5320 | log_info("Shutdown scheduled for %s, use 'shutdown -c' to cancel.", | |
5321 | format_timestamp(date, sizeof(date), arg_when)); | |
f6144808 | 5322 | return 0; |
08e4b1c5 | 5323 | } |
f6144808 LP |
5324 | } |
5325 | ||
d47b555b | 5326 | if (!arg_dry && !arg_immediate) |
e4b61340 LP |
5327 | return start_with_fallback(bus); |
5328 | ||
d90e1a30 LP |
5329 | if (!arg_no_wtmp) { |
5330 | if (sd_booted() > 0) | |
5331 | log_debug("Not writing utmp record, assuming that systemd-update-utmp is used."); | |
5332 | else if ((r = utmp_put_shutdown(0)) < 0) | |
e4b61340 | 5333 | log_warning("Failed to write utmp record: %s", strerror(-r)); |
d90e1a30 | 5334 | } |
e4b61340 LP |
5335 | |
5336 | if (!arg_no_sync) | |
5337 | sync(); | |
5338 | ||
5339 | if (arg_dry) | |
5340 | return 0; | |
5341 | ||
5342 | /* Make sure C-A-D is handled by the kernel from this | |
5343 | * point on... */ | |
5344 | reboot(RB_ENABLE_CAD); | |
5345 | ||
5346 | switch (arg_action) { | |
5347 | ||
5348 | case ACTION_HALT: | |
3059b1c1 | 5349 | log_info("Halting."); |
e4b61340 LP |
5350 | reboot(RB_HALT_SYSTEM); |
5351 | break; | |
5352 | ||
5353 | case ACTION_POWEROFF: | |
3059b1c1 | 5354 | log_info("Powering off."); |
e4b61340 LP |
5355 | reboot(RB_POWER_OFF); |
5356 | break; | |
5357 | ||
5358 | case ACTION_REBOOT: | |
3059b1c1 | 5359 | log_info("Rebooting."); |
e4b61340 LP |
5360 | reboot(RB_AUTOBOOT); |
5361 | break; | |
5362 | ||
5363 | default: | |
5364 | assert_not_reached("Unknown halt action."); | |
5365 | } | |
5366 | ||
5367 | /* We should never reach this. */ | |
5368 | return -ENOSYS; | |
5369 | } | |
5370 | ||
5371 | static int runlevel_main(void) { | |
5372 | int r, runlevel, previous; | |
5373 | ||
5374 | if ((r = utmp_get_runlevel(&runlevel, &previous)) < 0) { | |
cd6d0a45 | 5375 | printf("unknown\n"); |
e4b61340 LP |
5376 | return r; |
5377 | } | |
5378 | ||
5379 | printf("%c %c\n", | |
5380 | previous <= 0 ? 'N' : previous, | |
5381 | runlevel <= 0 ? 'N' : runlevel); | |
5382 | ||
5383 | return 0; | |
5384 | } | |
5385 | ||
0736af98 | 5386 | static void pager_open(void) { |
611efaac LP |
5387 | int fd[2]; |
5388 | const char *pager; | |
5389 | ||
1888c907 LP |
5390 | if (pager_pid > 0) |
5391 | return; | |
5392 | ||
611efaac LP |
5393 | if (!on_tty() || arg_no_pager) |
5394 | return; | |
5395 | ||
5396 | if ((pager = getenv("PAGER"))) | |
5397 | if (!*pager || streq(pager, "cat")) | |
5398 | return; | |
5399 | ||
4bc5f808 LP |
5400 | /* Determine and cache number of columns before we spawn the |
5401 | * pager so that we get the value from the actual tty */ | |
5402 | columns(); | |
5403 | ||
611efaac LP |
5404 | if (pipe(fd) < 0) { |
5405 | log_error("Failed to create pager pipe: %m"); | |
5406 | return; | |
5407 | } | |
5408 | ||
1888c907 LP |
5409 | pager_pid = fork(); |
5410 | if (pager_pid < 0) { | |
611efaac LP |
5411 | log_error("Failed to fork pager: %m"); |
5412 | close_pipe(fd); | |
5413 | return; | |
5414 | } | |
5415 | ||
1888c907 LP |
5416 | /* In the child start the pager */ |
5417 | if (pager_pid == 0) { | |
611efaac LP |
5418 | |
5419 | dup2(fd[0], STDIN_FILENO); | |
5420 | close_pipe(fd); | |
5421 | ||
4c00bf25 | 5422 | setenv("LESS", "FRSX", 0); |
611efaac | 5423 | |
1888c907 LP |
5424 | prctl(PR_SET_PDEATHSIG, SIGTERM); |
5425 | ||
611efaac LP |
5426 | if (pager) { |
5427 | execlp(pager, pager, NULL); | |
5428 | execl("/bin/sh", "sh", "-c", pager, NULL); | |
5429 | } else { | |
75d12d57 LP |
5430 | /* Debian's alternatives command for pagers is |
5431 | * called 'pager'. Note that we do not call | |
5432 | * sensible-pagers here, since that is just a | |
5433 | * shell script that implements a logic that | |
5434 | * is similar to this one anyway, but is | |
5435 | * Debian-specific. */ | |
5436 | execlp("pager", "pager", NULL); | |
5437 | ||
611efaac LP |
5438 | execlp("less", "less", NULL); |
5439 | execlp("more", "more", NULL); | |
5440 | } | |
5441 | ||
5442 | log_error("Unable to execute pager: %m"); | |
5443 | _exit(EXIT_FAILURE); | |
5444 | } | |
5445 | ||
1888c907 | 5446 | /* Return in the parent */ |
611efaac LP |
5447 | if (dup2(fd[1], STDOUT_FILENO) < 0) |
5448 | log_error("Failed to duplicate pager pipe: %m"); | |
5449 | ||
5450 | close_pipe(fd); | |
0736af98 MV |
5451 | } |
5452 | ||
1888c907 LP |
5453 | static void pager_close(void) { |
5454 | siginfo_t dummy; | |
5455 | ||
5456 | if (pager_pid <= 0) | |
5457 | return; | |
5458 | ||
5459 | /* Inform pager that we are done */ | |
5460 | fclose(stdout); | |
5461 | wait_for_terminate(pager_pid, &dummy); | |
5462 | pager_pid = 0; | |
5463 | } | |
5464 | ||
e4b61340 | 5465 | int main(int argc, char*argv[]) { |
22f4096c | 5466 | int r, retval = EXIT_FAILURE; |
e4b61340 LP |
5467 | DBusConnection *bus = NULL; |
5468 | DBusError error; | |
5469 | ||
5470 | dbus_error_init(&error); | |
5471 | ||
5472 | log_parse_environment(); | |
2396fb04 | 5473 | log_open(); |
e4b61340 LP |
5474 | |
5475 | if ((r = parse_argv(argc, argv)) < 0) | |
5476 | goto finish; | |
5477 | else if (r == 0) { | |
22f4096c | 5478 | retval = EXIT_SUCCESS; |
7e4249b9 LP |
5479 | goto finish; |
5480 | } | |
5481 | ||
e4b61340 LP |
5482 | /* /sbin/runlevel doesn't need to communicate via D-Bus, so |
5483 | * let's shortcut this */ | |
5484 | if (arg_action == ACTION_RUNLEVEL) { | |
22f4096c LP |
5485 | r = runlevel_main(); |
5486 | retval = r < 0 ? EXIT_FAILURE : r; | |
e4b61340 LP |
5487 | goto finish; |
5488 | } | |
5489 | ||
af2d49f7 | 5490 | bus_connect(arg_user ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &bus, &private_bus, &error); |
e4b61340 LP |
5491 | |
5492 | switch (arg_action) { | |
5493 | ||
22f4096c LP |
5494 | case ACTION_SYSTEMCTL: |
5495 | r = systemctl_main(bus, argc, argv, &error); | |
e4b61340 | 5496 | break; |
e4b61340 LP |
5497 | |
5498 | case ACTION_HALT: | |
5499 | case ACTION_POWEROFF: | |
5500 | case ACTION_REBOOT: | |
22f4096c | 5501 | r = halt_main(bus); |
e4b61340 LP |
5502 | break; |
5503 | ||
e4b61340 LP |
5504 | case ACTION_RUNLEVEL2: |
5505 | case ACTION_RUNLEVEL3: | |
5506 | case ACTION_RUNLEVEL4: | |
5507 | case ACTION_RUNLEVEL5: | |
5508 | case ACTION_RESCUE: | |
514f4ef5 | 5509 | case ACTION_EMERGENCY: |
eb22ac37 | 5510 | case ACTION_DEFAULT: |
22f4096c | 5511 | r = start_with_fallback(bus); |
e4b61340 | 5512 | break; |
7e4249b9 | 5513 | |
e4b61340 LP |
5514 | case ACTION_RELOAD: |
5515 | case ACTION_REEXEC: | |
22f4096c | 5516 | r = reload_with_fallback(bus); |
e4b61340 LP |
5517 | break; |
5518 | ||
f6144808 | 5519 | case ACTION_CANCEL_SHUTDOWN: |
22f4096c | 5520 | r = send_shutdownd(0, 0, false, NULL); |
f6144808 LP |
5521 | break; |
5522 | ||
eb22ac37 LP |
5523 | case ACTION_INVALID: |
5524 | case ACTION_RUNLEVEL: | |
e4b61340 LP |
5525 | default: |
5526 | assert_not_reached("Unknown action"); | |
5527 | } | |
7e4249b9 | 5528 | |
22f4096c LP |
5529 | retval = r < 0 ? EXIT_FAILURE : r; |
5530 | ||
7e4249b9 LP |
5531 | finish: |
5532 | ||
b574246b | 5533 | if (bus) { |
5d452f9c | 5534 | dbus_connection_flush(bus); |
b574246b | 5535 | dbus_connection_close(bus); |
7e4249b9 | 5536 | dbus_connection_unref(bus); |
b574246b | 5537 | } |
7e4249b9 | 5538 | |
e4b61340 LP |
5539 | dbus_error_free(&error); |
5540 | ||
7e4249b9 LP |
5541 | dbus_shutdown(); |
5542 | ||
ea4a240d LP |
5543 | strv_free(arg_property); |
5544 | ||
1888c907 LP |
5545 | pager_close(); |
5546 | ||
7e4249b9 LP |
5547 | return retval; |
5548 | } |