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