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