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