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