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