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