]>
Commit | Line | Data |
---|---|---|
7e4249b9 LP |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2010 Lennart Poettering | |
f459b602 | 5 | Copyright 2013 Marc-Antoine Perennou |
7e4249b9 LP |
6 | |
7 | systemd is free software; you can redistribute it and/or modify it | |
5430f7f2 LP |
8 | under the terms of the GNU Lesser General Public License as published by |
9 | the Free Software Foundation; either version 2.1 of the License, or | |
7e4249b9 LP |
10 | (at your option) any later version. |
11 | ||
12 | systemd is distributed in the hope that it will be useful, but | |
13 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
5430f7f2 | 15 | Lesser General Public License for more details. |
7e4249b9 | 16 | |
5430f7f2 | 17 | You should have received a copy of the GNU Lesser General Public License |
7e4249b9 LP |
18 | along with systemd; If not, see <http://www.gnu.org/licenses/>. |
19 | ***/ | |
20 | ||
03a7b521 LP |
21 | #include <errno.h> |
22 | #include <fcntl.h> | |
7e4249b9 | 23 | #include <getopt.h> |
03a7b521 | 24 | #include <linux/reboot.h> |
a9cdc94f | 25 | #include <locale.h> |
7e4249b9 | 26 | #include <stdbool.h> |
03a7b521 LP |
27 | #include <stddef.h> |
28 | #include <stdio.h> | |
7e4249b9 | 29 | #include <string.h> |
03a7b521 | 30 | #include <sys/reboot.h> |
f1c5860b | 31 | #include <sys/socket.h> |
03a7b521 | 32 | #include <unistd.h> |
81527be1 | 33 | |
03a7b521 | 34 | #include "sd-bus.h" |
f459b602 | 35 | #include "sd-daemon.h" |
f459b602 | 36 | #include "sd-login.h" |
03a7b521 | 37 | |
b5efdb8a | 38 | #include "alloc-util.h" |
03a7b521 LP |
39 | #include "bus-common-errors.h" |
40 | #include "bus-error.h" | |
41 | #include "bus-message.h" | |
291d565a | 42 | #include "bus-unit-util.h" |
03a7b521 | 43 | #include "bus-util.h" |
ab35fb1b | 44 | #include "cgroup-show.h" |
c6c18be3 | 45 | #include "cgroup-util.h" |
cda134ab | 46 | #include "copy.h" |
ad2a0358 | 47 | #include "dropin.h" |
5bdf2243 | 48 | #include "efivars.h" |
03a7b521 LP |
49 | #include "env-util.h" |
50 | #include "exit-status.h" | |
3ffd4af2 | 51 | #include "fd-util.h" |
03a7b521 | 52 | #include "fileio.h" |
6482f626 | 53 | #include "formats-util.h" |
f4f15635 | 54 | #include "fs-util.h" |
7d50b32a | 55 | #include "glob-util.h" |
958b66ea | 56 | #include "hostname-util.h" |
03a7b521 LP |
57 | #include "initreq.h" |
58 | #include "install.h" | |
c004493c | 59 | #include "io-util.h" |
03a7b521 | 60 | #include "list.h" |
8752c575 | 61 | #include "locale-util.h" |
03a7b521 LP |
62 | #include "log.h" |
63 | #include "logs-show.h" | |
64 | #include "macro.h" | |
65 | #include "mkdir.h" | |
66 | #include "pager.h" | |
6bedfcbb | 67 | #include "parse-util.h" |
03a7b521 LP |
68 | #include "path-lookup.h" |
69 | #include "path-util.h" | |
70 | #include "process-util.h" | |
78f22b97 | 71 | #include "rlimit-util.h" |
03a7b521 | 72 | #include "set.h" |
592705f2 | 73 | #include "sigbus.h" |
24882e06 | 74 | #include "signal-util.h" |
03a7b521 LP |
75 | #include "socket-util.h" |
76 | #include "spawn-ask-password-agent.h" | |
77 | #include "spawn-polkit-agent.h" | |
78 | #include "special.h" | |
8fcde012 | 79 | #include "stat-util.h" |
03a7b521 LP |
80 | #include "strv.h" |
81 | #include "terminal-util.h" | |
82 | #include "unit-name.h" | |
b1d4f8e1 | 83 | #include "user-util.h" |
03a7b521 LP |
84 | #include "util.h" |
85 | #include "utmp-wtmp.h" | |
e449de87 | 86 | #include "verbs.h" |
7f4b3c5e | 87 | #include "virt.h" |
7e4249b9 | 88 | |
b613907e SS |
89 | /* The init script exit status codes |
90 | 0 program is running or service is OK | |
91 | 1 program is dead and /var/run pid file exists | |
92 | 2 program is dead and /var/lock lock file exists | |
93 | 3 program is not running | |
94 | 4 program or service status is unknown | |
95 | 5-99 reserved for future LSB use | |
96 | 100-149 reserved for distribution use | |
97 | 150-199 reserved for application use | |
98 | 200-254 reserved | |
99 | */ | |
100 | enum { | |
101 | EXIT_PROGRAM_RUNNING_OR_SERVICE_OK = 0, | |
102 | EXIT_PROGRAM_DEAD_AND_PID_EXISTS = 1, | |
103 | EXIT_PROGRAM_DEAD_AND_LOCK_FILE_EXISTS = 2, | |
104 | EXIT_PROGRAM_NOT_RUNNING = 3, | |
105 | EXIT_PROGRAM_OR_SERVICES_STATUS_UNKNOWN = 4, | |
106 | }; | |
107 | ||
20b3f379 | 108 | static char **arg_types = NULL; |
9b9b3d36 | 109 | static char **arg_states = NULL; |
20b3f379 | 110 | static char **arg_properties = NULL; |
7e4249b9 | 111 | static bool arg_all = false; |
afba4199 ZJS |
112 | static enum dependency { |
113 | DEPENDENCY_FORWARD, | |
114 | DEPENDENCY_REVERSE, | |
115 | DEPENDENCY_AFTER, | |
116 | DEPENDENCY_BEFORE, | |
071066a5 | 117 | _DEPENDENCY_MAX |
afba4199 | 118 | } arg_dependency = DEPENDENCY_FORWARD; |
e67c3609 | 119 | static const char *arg_job_mode = "replace"; |
729e3769 | 120 | static UnitFileScope arg_scope = UNIT_FILE_SYSTEM; |
ee5762e3 | 121 | static bool arg_no_block = false; |
ebed32bf | 122 | static bool arg_no_legend = false; |
0736af98 | 123 | static bool arg_no_pager = false; |
e4b61340 | 124 | static bool arg_no_wtmp = false; |
f3f054f0 | 125 | static bool arg_no_sync = false; |
514f4ef5 | 126 | static bool arg_no_wall = false; |
ee5762e3 | 127 | static bool arg_no_reload = false; |
4f9a9105 | 128 | static bool arg_value = false; |
991f2a39 | 129 | static bool arg_show_types = false; |
b37844d3 | 130 | static bool arg_ignore_inhibitors = false; |
e4b61340 | 131 | static bool arg_dry = false; |
0183528f | 132 | static bool arg_quiet = false; |
ee5762e3 | 133 | static bool arg_full = false; |
1238ee09 | 134 | static bool arg_recursive = false; |
e606bb61 | 135 | static int arg_force = 0; |
16f017fa | 136 | static bool arg_ask_password = false; |
729e3769 | 137 | static bool arg_runtime = false; |
d309c1c3 | 138 | static UnitFilePresetMode arg_preset_mode = UNIT_FILE_PRESET_FULL; |
e4b61340 | 139 | static char **arg_wall = NULL; |
8a0867d6 | 140 | static const char *arg_kill_who = NULL; |
8a0867d6 | 141 | static int arg_signal = SIGTERM; |
0f03c2a4 | 142 | static char *arg_root = NULL; |
f6144808 | 143 | static usec_t arg_when = 0; |
4445a875 | 144 | static enum action { |
f459b602 | 145 | _ACTION_INVALID, |
e4b61340 LP |
146 | ACTION_SYSTEMCTL, |
147 | ACTION_HALT, | |
148 | ACTION_POWEROFF, | |
149 | ACTION_REBOOT, | |
20b09ca7 LP |
150 | ACTION_KEXEC, |
151 | ACTION_EXIT, | |
6edd7d0a LP |
152 | ACTION_SUSPEND, |
153 | ACTION_HIBERNATE, | |
6524990f | 154 | ACTION_HYBRID_SLEEP, |
e4b61340 LP |
155 | ACTION_RUNLEVEL2, |
156 | ACTION_RUNLEVEL3, | |
157 | ACTION_RUNLEVEL4, | |
158 | ACTION_RUNLEVEL5, | |
159 | ACTION_RESCUE, | |
514f4ef5 LP |
160 | ACTION_EMERGENCY, |
161 | ACTION_DEFAULT, | |
e4b61340 LP |
162 | ACTION_RELOAD, |
163 | ACTION_REEXEC, | |
164 | ACTION_RUNLEVEL, | |
f6144808 | 165 | ACTION_CANCEL_SHUTDOWN, |
e4b61340 LP |
166 | _ACTION_MAX |
167 | } arg_action = ACTION_SYSTEMCTL; | |
f459b602 | 168 | static BusTransport arg_transport = BUS_TRANSPORT_LOCAL; |
fc2ffaf1 | 169 | static const char *arg_host = NULL; |
df50185b LP |
170 | static unsigned arg_lines = 10; |
171 | static OutputMode arg_output = OUTPUT_SHORT; | |
5d0c05e5 | 172 | static bool arg_plain = false; |
5bdf2243 | 173 | static bool arg_firmware_setup = false; |
57ab2eab | 174 | static bool arg_now = false; |
1238ee09 | 175 | |
e449de87 | 176 | static int daemon_reload(int argc, char *argv[], void* userdata); |
2853b60a | 177 | static int trivial_method(int argc, char *argv[], void *userdata); |
477def80 | 178 | static int halt_now(enum action a); |
fa0d5878 | 179 | static int get_state_one_unit(sd_bus *bus, const char *name, UnitActiveState *active_state); |
dbc2c080 | 180 | |
4fbd7192 LP |
181 | static bool original_stdout_is_tty; |
182 | ||
183 | typedef enum BusFocus { | |
184 | BUS_FULL, /* The full bus indicated via --system or --user */ | |
185 | BUS_MANAGER, /* The manager itself, possibly directly, possibly via the bus */ | |
186 | _BUS_FOCUS_MAX | |
187 | } BusFocus; | |
188 | ||
189 | static sd_bus *busses[_BUS_FOCUS_MAX] = {}; | |
190 | ||
191 | static int acquire_bus(BusFocus focus, sd_bus **ret) { | |
192 | int r; | |
193 | ||
194 | assert(focus < _BUS_FOCUS_MAX); | |
195 | assert(ret); | |
196 | ||
197 | /* We only go directly to the manager, if we are using a local transport */ | |
198 | if (arg_transport != BUS_TRANSPORT_LOCAL) | |
199 | focus = BUS_FULL; | |
200 | ||
201 | if (!busses[focus]) { | |
202 | bool user; | |
203 | ||
204 | user = arg_scope != UNIT_FILE_SYSTEM; | |
205 | ||
206 | if (focus == BUS_MANAGER) | |
207 | r = bus_connect_transport_systemd(arg_transport, arg_host, user, &busses[focus]); | |
208 | else | |
209 | r = bus_connect_transport(arg_transport, arg_host, user, &busses[focus]); | |
210 | if (r < 0) | |
211 | return log_error_errno(r, "Failed to connect to bus: %m"); | |
212 | ||
213 | (void) sd_bus_set_allow_interactive_authorization(busses[focus], arg_ask_password); | |
214 | } | |
215 | ||
216 | *ret = busses[focus]; | |
217 | return 0; | |
218 | } | |
219 | ||
220 | static void release_busses(void) { | |
221 | BusFocus w; | |
222 | ||
223 | for (w = 0; w < _BUS_FOCUS_MAX; w++) | |
224 | busses[w] = sd_bus_flush_close_unref(busses[w]); | |
225 | } | |
226 | ||
6bb92a16 | 227 | static void ask_password_agent_open_if_enabled(void) { |
501fc174 | 228 | |
729e3769 | 229 | /* Open the password agent as a child process if necessary */ |
501fc174 LP |
230 | |
231 | if (!arg_ask_password) | |
232 | return; | |
715554e7 | 233 | |
729e3769 | 234 | if (arg_scope != UNIT_FILE_SYSTEM) |
501fc174 LP |
235 | return; |
236 | ||
cbc9fbd1 LP |
237 | if (arg_transport != BUS_TRANSPORT_LOCAL) |
238 | return; | |
239 | ||
6bb92a16 LP |
240 | ask_password_agent_open(); |
241 | } | |
242 | ||
243 | static void polkit_agent_open_if_enabled(void) { | |
244 | ||
245 | /* Open the polkit agent as a child process if necessary */ | |
246 | ||
247 | if (!arg_ask_password) | |
248 | return; | |
249 | ||
250 | if (arg_scope != UNIT_FILE_SYSTEM) | |
251 | return; | |
252 | ||
f459b602 MAP |
253 | if (arg_transport != BUS_TRANSPORT_LOCAL) |
254 | return; | |
255 | ||
6bb92a16 | 256 | polkit_agent_open(); |
501fc174 LP |
257 | } |
258 | ||
3c756001 LP |
259 | static OutputFlags get_output_flags(void) { |
260 | return | |
261 | arg_all * OUTPUT_SHOW_ALL | | |
8b0cc9a3 | 262 | arg_full * OUTPUT_FULL_WIDTH | |
3c756001 | 263 | (!on_tty() || pager_have()) * OUTPUT_FULL_WIDTH | |
40c9fe4c | 264 | colors_enabled() * OUTPUT_COLOR | |
8b0cc9a3 | 265 | !arg_quiet * OUTPUT_WARN_CUTOFF; |
3c756001 LP |
266 | } |
267 | ||
f459b602 | 268 | static int translate_bus_error_to_exit_status(int r, const sd_bus_error *error) { |
22f4096c LP |
269 | assert(error); |
270 | ||
f459b602 | 271 | if (!sd_bus_error_is_set(error)) |
22f4096c LP |
272 | return r; |
273 | ||
f459b602 MAP |
274 | if (sd_bus_error_has_name(error, SD_BUS_ERROR_ACCESS_DENIED) || |
275 | sd_bus_error_has_name(error, BUS_ERROR_ONLY_BY_DEPENDENCY) || | |
276 | sd_bus_error_has_name(error, BUS_ERROR_NO_ISOLATION) || | |
277 | sd_bus_error_has_name(error, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE)) | |
22f4096c LP |
278 | return EXIT_NOPERMISSION; |
279 | ||
f459b602 | 280 | if (sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT)) |
22f4096c LP |
281 | return EXIT_NOTINSTALLED; |
282 | ||
f459b602 | 283 | if (sd_bus_error_has_name(error, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE) || |
718db961 | 284 | sd_bus_error_has_name(error, SD_BUS_ERROR_NOT_SUPPORTED)) |
22f4096c LP |
285 | return EXIT_NOTIMPLEMENTED; |
286 | ||
f459b602 | 287 | if (sd_bus_error_has_name(error, BUS_ERROR_LOAD_FAILED)) |
22f4096c LP |
288 | return EXIT_NOTCONFIGURED; |
289 | ||
290 | if (r != 0) | |
291 | return r; | |
292 | ||
293 | return EXIT_FAILURE; | |
294 | } | |
295 | ||
4fbd7192 | 296 | static bool install_client_side(void) { |
729e3769 | 297 | |
4fbd7192 LP |
298 | /* Decides when to execute enable/disable/... operations |
299 | * client-side rather than server-side. */ | |
4f16c1f4 | 300 | |
729e3769 LP |
301 | if (running_in_chroot() > 0) |
302 | return true; | |
303 | ||
304 | if (sd_booted() <= 0) | |
305 | return true; | |
306 | ||
307 | if (!isempty(arg_root)) | |
308 | return true; | |
309 | ||
310 | if (arg_scope == UNIT_FILE_GLOBAL) | |
311 | return true; | |
312 | ||
b41b9d2a LP |
313 | /* Unsupported environment variable, mostly for debugging purposes */ |
314 | if (getenv_bool("SYSTEMCTL_INSTALL_CLIENT_SIDE") > 0) | |
315 | return true; | |
316 | ||
729e3769 LP |
317 | return false; |
318 | } | |
319 | ||
36c32ba2 | 320 | static int compare_unit_info(const void *a, const void *b) { |
f459b602 | 321 | const UnitInfo *u = a, *v = b; |
36c32ba2 | 322 | const char *d1, *d2; |
1238ee09 LP |
323 | int r; |
324 | ||
325 | /* First, order by machine */ | |
326 | if (!u->machine && v->machine) | |
327 | return -1; | |
328 | if (u->machine && !v->machine) | |
329 | return 1; | |
330 | if (u->machine && v->machine) { | |
331 | r = strcasecmp(u->machine, v->machine); | |
332 | if (r != 0) | |
333 | return r; | |
334 | } | |
36c32ba2 | 335 | |
1238ee09 | 336 | /* Second, order by unit type */ |
36c32ba2 LP |
337 | d1 = strrchr(u->id, '.'); |
338 | d2 = strrchr(v->id, '.'); | |
36c32ba2 | 339 | if (d1 && d2) { |
f84190d8 LP |
340 | r = strcasecmp(d1, d2); |
341 | if (r != 0) | |
36c32ba2 LP |
342 | return r; |
343 | } | |
344 | ||
1238ee09 | 345 | /* Third, order by name */ |
a2a3a5b9 | 346 | return strcasecmp(u->id, v->id); |
36c32ba2 LP |
347 | } |
348 | ||
d8fba7c6 | 349 | static bool output_show_unit(const UnitInfo *u, char **patterns) { |
f8591ee1 LP |
350 | assert(u); |
351 | ||
2404701e | 352 | if (!strv_fnmatch_or_empty(patterns, u->id, FNM_NOESCAPE)) |
d8fba7c6 | 353 | return false; |
d8fba7c6 | 354 | |
6c71341a ZJS |
355 | if (arg_types) { |
356 | const char *dot; | |
357 | ||
358 | dot = strrchr(u->id, '.'); | |
359 | if (!dot) | |
360 | return false; | |
361 | ||
362 | if (!strv_find(arg_types, dot+1)) | |
363 | return false; | |
364 | } | |
365 | ||
366 | if (arg_all) | |
367 | return true; | |
368 | ||
409472cb FB |
369 | /* Note that '--all' is not purely a state filter, but also a |
370 | * filter that hides units that "follow" other units (which is | |
371 | * used for device units that appear under different names). */ | |
372 | if (!isempty(u->following)) | |
373 | return false; | |
374 | ||
ebc96265 FB |
375 | if (!strv_isempty(arg_states)) |
376 | return true; | |
377 | ||
378 | /* By default show all units except the ones in inactive | |
379 | * state and with no pending job */ | |
6c71341a ZJS |
380 | if (u->job_id > 0) |
381 | return true; | |
382 | ||
409472cb | 383 | if (streq(u->active_state, "inactive")) |
6c71341a ZJS |
384 | return false; |
385 | ||
386 | return true; | |
33330222 ZJS |
387 | } |
388 | ||
1238ee09 | 389 | static int output_units_list(const UnitInfo *unit_infos, unsigned c) { |
90c3f79d | 390 | unsigned circle_len = 0, id_len, max_id_len, load_len, active_len, sub_len, job_len, desc_len; |
f459b602 | 391 | const UnitInfo *u; |
a1074881 | 392 | unsigned n_shown = 0; |
ccd41387 | 393 | int job_count = 0; |
33330222 | 394 | |
f8294e41 JT |
395 | max_id_len = strlen("UNIT"); |
396 | load_len = strlen("LOAD"); | |
397 | active_len = strlen("ACTIVE"); | |
398 | sub_len = strlen("SUB"); | |
399 | job_len = strlen("JOB"); | |
4deb3b93 | 400 | desc_len = 0; |
b036fc00 LP |
401 | |
402 | for (u = unit_infos; u < unit_infos + c; u++) { | |
1238ee09 | 403 | max_id_len = MAX(max_id_len, strlen(u->id) + (u->machine ? strlen(u->machine)+1 : 0)); |
a1074881 | 404 | load_len = MAX(load_len, strlen(u->load_state)); |
b036fc00 LP |
405 | active_len = MAX(active_len, strlen(u->active_state)); |
406 | sub_len = MAX(sub_len, strlen(u->sub_state)); | |
f459b602 | 407 | |
ccd41387 | 408 | if (u->job_id != 0) { |
b036fc00 | 409 | job_len = MAX(job_len, strlen(u->job_type)); |
ccd41387 ZJS |
410 | job_count++; |
411 | } | |
90c3f79d LP |
412 | |
413 | if (!arg_no_legend && | |
414 | (streq(u->active_state, "failed") || | |
415 | STR_IN_SET(u->load_state, "error", "not-found", "masked"))) | |
416 | circle_len = 2; | |
33330222 ZJS |
417 | } |
418 | ||
184ecaf7 | 419 | if (!arg_full && original_stdout_is_tty) { |
4deb3b93 | 420 | unsigned basic_len; |
cbc9fbd1 | 421 | |
9607d947 | 422 | id_len = MIN(max_id_len, 25u); |
90c3f79d | 423 | basic_len = circle_len + 5 + id_len + 5 + active_len + sub_len; |
cbc9fbd1 | 424 | |
ccd41387 ZJS |
425 | if (job_count) |
426 | basic_len += job_len + 1; | |
cbc9fbd1 | 427 | |
4deb3b93 MS |
428 | if (basic_len < (unsigned) columns()) { |
429 | unsigned extra_len, incr; | |
430 | extra_len = columns() - basic_len; | |
cbc9fbd1 | 431 | |
4deb3b93 MS |
432 | /* Either UNIT already got 25, or is fully satisfied. |
433 | * Grant up to 25 to DESC now. */ | |
9607d947 | 434 | incr = MIN(extra_len, 25u); |
4deb3b93 MS |
435 | desc_len += incr; |
436 | extra_len -= incr; | |
cbc9fbd1 | 437 | |
4deb3b93 MS |
438 | /* split the remaining space between UNIT and DESC, |
439 | * but do not give UNIT more than it needs. */ | |
440 | if (extra_len > 0) { | |
441 | incr = MIN(extra_len / 2, max_id_len - id_len); | |
442 | id_len += incr; | |
443 | desc_len += extra_len - incr; | |
444 | } | |
445 | } | |
446 | } else | |
447 | id_len = max_id_len; | |
448 | ||
b036fc00 | 449 | for (u = unit_infos; u < unit_infos + c; u++) { |
1238ee09 | 450 | _cleanup_free_ char *e = NULL, *j = NULL; |
90c3f79d LP |
451 | const char *on_loaded = "", *off_loaded = ""; |
452 | const char *on_active = "", *off_active = ""; | |
453 | const char *on_circle = "", *off_circle = ""; | |
1238ee09 | 454 | const char *id; |
90c3f79d | 455 | bool circle = false; |
b036fc00 | 456 | |
ad94ad63 | 457 | if (!n_shown && !arg_no_legend) { |
90c3f79d LP |
458 | |
459 | if (circle_len > 0) | |
460 | fputs(" ", stdout); | |
461 | ||
cbc9fbd1 LP |
462 | printf("%-*s %-*s %-*s %-*s ", |
463 | id_len, "UNIT", | |
464 | load_len, "LOAD", | |
465 | active_len, "ACTIVE", | |
466 | sub_len, "SUB"); | |
467 | ||
ccd41387 ZJS |
468 | if (job_count) |
469 | printf("%-*s ", job_len, "JOB"); | |
cbc9fbd1 | 470 | |
ad94ad63 ZJS |
471 | if (!arg_full && arg_no_pager) |
472 | printf("%.*s\n", desc_len, "DESCRIPTION"); | |
473 | else | |
474 | printf("%s\n", "DESCRIPTION"); | |
475 | } | |
476 | ||
688c6725 LP |
477 | n_shown++; |
478 | ||
250ba664 | 479 | if (STR_IN_SET(u->load_state, "error", "not-found", "masked") && !arg_plain) { |
90c3f79d LP |
480 | on_loaded = ansi_highlight_red(); |
481 | on_circle = ansi_highlight_yellow(); | |
1fc464f6 | 482 | off_loaded = off_circle = ansi_normal(); |
90c3f79d | 483 | circle = true; |
250ba664 | 484 | } else if (streq(u->active_state, "failed") && !arg_plain) { |
90c3f79d | 485 | on_circle = on_active = ansi_highlight_red(); |
1fc464f6 | 486 | off_circle = off_active = ansi_normal(); |
90c3f79d LP |
487 | circle = true; |
488 | } | |
eb68c413 | 489 | |
1238ee09 LP |
490 | if (u->machine) { |
491 | j = strjoin(u->machine, ":", u->id, NULL); | |
492 | if (!j) | |
493 | return log_oom(); | |
494 | ||
495 | id = j; | |
496 | } else | |
497 | id = u->id; | |
498 | ||
499 | if (arg_full) { | |
500 | e = ellipsize(id, id_len, 33); | |
501 | if (!e) | |
502 | return log_oom(); | |
503 | ||
504 | id = e; | |
505 | } | |
eb68c413 | 506 | |
90c3f79d | 507 | if (circle_len > 0) |
323b7dc9 | 508 | printf("%s%s%s ", on_circle, circle ? special_glyph(BLACK_CIRCLE) : " ", off_circle); |
90c3f79d | 509 | |
a1074881 | 510 | printf("%s%-*s%s %s%-*s%s %s%-*s %-*s%s %-*s", |
90c3f79d | 511 | on_active, id_len, id, off_active, |
a1074881 | 512 | on_loaded, load_len, u->load_state, off_loaded, |
b036fc00 LP |
513 | on_active, active_len, u->active_state, |
514 | sub_len, u->sub_state, off_active, | |
ccd41387 | 515 | job_count ? job_len + 1 : 0, u->job_id ? u->job_type : ""); |
cbc9fbd1 | 516 | |
184ecaf7 | 517 | if (desc_len > 0) |
798e258d MS |
518 | printf("%.*s\n", desc_len, u->description); |
519 | else | |
520 | printf("%s\n", u->description); | |
eb68c413 ZJS |
521 | } |
522 | ||
ebed32bf | 523 | if (!arg_no_legend) { |
57f7ae4f ZJS |
524 | const char *on, *off; |
525 | ||
526 | if (n_shown) { | |
90c3f79d LP |
527 | puts("\n" |
528 | "LOAD = Reflects whether the unit definition was properly loaded.\n" | |
e3e0314b ZJS |
529 | "ACTIVE = The high-level unit activation state, i.e. generalization of SUB.\n" |
530 | "SUB = The low-level unit activation state, values depend on unit type."); | |
531 | puts(job_count ? "JOB = Pending job for the unit.\n" : ""); | |
0b5a519c | 532 | on = ansi_highlight(); |
1fc464f6 | 533 | off = ansi_normal(); |
57f7ae4f | 534 | } else { |
0b5a519c | 535 | on = ansi_highlight_red(); |
1fc464f6 | 536 | off = ansi_normal(); |
57f7ae4f | 537 | } |
eb68c413 ZJS |
538 | |
539 | if (arg_all) | |
48c2826b | 540 | printf("%s%u loaded units listed.%s\n" |
57f7ae4f ZJS |
541 | "To show all installed unit files use 'systemctl list-unit-files'.\n", |
542 | on, n_shown, off); | |
eb68c413 | 543 | else |
48c2826b | 544 | printf("%s%u loaded units listed.%s Pass --all to see loaded but inactive units, too.\n" |
57f7ae4f ZJS |
545 | "To show all installed unit files use 'systemctl list-unit-files'.\n", |
546 | on, n_shown, off); | |
eb68c413 | 547 | } |
1238ee09 LP |
548 | |
549 | return 0; | |
eb68c413 ZJS |
550 | } |
551 | ||
a00963a2 | 552 | static int get_unit_list( |
f459b602 | 553 | sd_bus *bus, |
1238ee09 LP |
554 | const char *machine, |
555 | char **patterns, | |
556 | UnitInfo **unit_infos, | |
557 | int c, | |
558 | sd_bus_message **_reply) { | |
a00963a2 | 559 | |
4afd3348 LP |
560 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
561 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
562 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
ca2d3784 | 563 | size_t size = c; |
1238ee09 | 564 | int r; |
f459b602 | 565 | UnitInfo u; |
313fe66f | 566 | bool fallback = false; |
7e4249b9 | 567 | |
265a7a2a | 568 | assert(bus); |
1238ee09 | 569 | assert(unit_infos); |
f459b602 | 570 | assert(_reply); |
1238ee09 | 571 | |
cdc06ed7 | 572 | r = sd_bus_message_new_method_call( |
f22f08cd | 573 | bus, |
cdc06ed7 | 574 | &m, |
f22f08cd SP |
575 | "org.freedesktop.systemd1", |
576 | "/org/freedesktop/systemd1", | |
577 | "org.freedesktop.systemd1.Manager", | |
313fe66f | 578 | "ListUnitsByPatterns"); |
cdc06ed7 DS |
579 | if (r < 0) |
580 | return bus_log_create_error(r); | |
581 | ||
582 | r = sd_bus_message_append_strv(m, arg_states); | |
583 | if (r < 0) | |
584 | return bus_log_create_error(r); | |
585 | ||
313fe66f | 586 | r = sd_bus_message_append_strv(m, patterns); |
587 | if (r < 0) | |
588 | return bus_log_create_error(r); | |
589 | ||
cdc06ed7 | 590 | r = sd_bus_call(bus, m, 0, &error, &reply); |
33d52725 ZJS |
591 | if (r < 0 && (sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_METHOD) || |
592 | sd_bus_error_has_name(&error, SD_BUS_ERROR_ACCESS_DENIED))) { | |
313fe66f | 593 | /* Fallback to legacy ListUnitsFiltered method */ |
594 | fallback = true; | |
595 | log_debug_errno(r, "Failed to list units: %s Falling back to ListUnitsFiltered method.", bus_error_message(&error, r)); | |
596 | m = sd_bus_message_unref(m); | |
597 | sd_bus_error_free(&error); | |
598 | ||
599 | r = sd_bus_message_new_method_call( | |
600 | bus, | |
601 | &m, | |
602 | "org.freedesktop.systemd1", | |
603 | "/org/freedesktop/systemd1", | |
604 | "org.freedesktop.systemd1.Manager", | |
605 | "ListUnitsFiltered"); | |
606 | if (r < 0) | |
607 | return bus_log_create_error(r); | |
608 | ||
609 | r = sd_bus_message_append_strv(m, arg_states); | |
610 | if (r < 0) | |
611 | return bus_log_create_error(r); | |
612 | ||
613 | r = sd_bus_call(bus, m, 0, &error, &reply); | |
614 | } | |
4c3e8e39 LP |
615 | if (r < 0) |
616 | return log_error_errno(r, "Failed to list units: %s", bus_error_message(&error, r)); | |
7e4249b9 | 617 | |
f459b602 MAP |
618 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssssouso)"); |
619 | if (r < 0) | |
620 | return bus_log_parse_error(r); | |
7e4249b9 | 621 | |
f459b602 | 622 | while ((r = bus_parse_unit_info(reply, &u)) > 0) { |
1238ee09 LP |
623 | u.machine = machine; |
624 | ||
313fe66f | 625 | if (!output_show_unit(&u, fallback ? patterns : NULL)) |
8d5ba5a9 | 626 | continue; |
7e4249b9 | 627 | |
1238ee09 | 628 | if (!GREEDY_REALLOC(*unit_infos, size, c+1)) |
f459b602 | 629 | return log_oom(); |
36c32ba2 | 630 | |
1238ee09 | 631 | (*unit_infos)[c++] = u; |
991f2a39 | 632 | } |
f459b602 MAP |
633 | if (r < 0) |
634 | return bus_log_parse_error(r); | |
991f2a39 | 635 | |
f459b602 MAP |
636 | r = sd_bus_message_exit_container(reply); |
637 | if (r < 0) | |
638 | return bus_log_parse_error(r); | |
639 | ||
640 | *_reply = reply; | |
641 | reply = NULL; | |
642 | ||
1238ee09 LP |
643 | return c; |
644 | } | |
645 | ||
646 | static void message_set_freep(Set **set) { | |
647 | sd_bus_message *m; | |
648 | ||
649 | while ((m = set_steal_first(*set))) | |
650 | sd_bus_message_unref(m); | |
651 | ||
652 | set_free(*set); | |
653 | } | |
654 | ||
655 | static int get_unit_list_recursive( | |
656 | sd_bus *bus, | |
657 | char **patterns, | |
658 | UnitInfo **_unit_infos, | |
659 | Set **_replies, | |
660 | char ***_machines) { | |
661 | ||
662 | _cleanup_free_ UnitInfo *unit_infos = NULL; | |
663 | _cleanup_(message_set_freep) Set *replies; | |
664 | sd_bus_message *reply; | |
665 | int c, r; | |
666 | ||
667 | assert(bus); | |
668 | assert(_replies); | |
669 | assert(_unit_infos); | |
670 | assert(_machines); | |
671 | ||
d5099efc | 672 | replies = set_new(NULL); |
1238ee09 LP |
673 | if (!replies) |
674 | return log_oom(); | |
675 | ||
676 | c = get_unit_list(bus, NULL, patterns, &unit_infos, 0, &reply); | |
677 | if (c < 0) | |
678 | return c; | |
679 | ||
680 | r = set_put(replies, reply); | |
681 | if (r < 0) { | |
682 | sd_bus_message_unref(reply); | |
4c3e8e39 | 683 | return log_oom(); |
1238ee09 LP |
684 | } |
685 | ||
686 | if (arg_recursive) { | |
687 | _cleanup_strv_free_ char **machines = NULL; | |
688 | char **i; | |
689 | ||
690 | r = sd_get_machine_names(&machines); | |
691 | if (r < 0) | |
4c3e8e39 | 692 | return log_error_errno(r, "Failed to get machine names: %m"); |
1238ee09 LP |
693 | |
694 | STRV_FOREACH(i, machines) { | |
4afd3348 | 695 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *container = NULL; |
1238ee09 LP |
696 | int k; |
697 | ||
de33fc62 | 698 | r = sd_bus_open_system_machine(&container, *i); |
1238ee09 | 699 | if (r < 0) { |
4c3e8e39 | 700 | log_warning_errno(r, "Failed to connect to container %s, ignoring: %m", *i); |
1238ee09 LP |
701 | continue; |
702 | } | |
703 | ||
704 | k = get_unit_list(container, *i, patterns, &unit_infos, c, &reply); | |
705 | if (k < 0) | |
706 | return k; | |
707 | ||
708 | c = k; | |
709 | ||
710 | r = set_put(replies, reply); | |
711 | if (r < 0) { | |
712 | sd_bus_message_unref(reply); | |
4c3e8e39 | 713 | return log_oom(); |
1238ee09 LP |
714 | } |
715 | } | |
716 | ||
717 | *_machines = machines; | |
718 | machines = NULL; | |
719 | } else | |
720 | *_machines = NULL; | |
721 | ||
f459b602 MAP |
722 | *_unit_infos = unit_infos; |
723 | unit_infos = NULL; | |
724 | ||
1238ee09 LP |
725 | *_replies = replies; |
726 | replies = NULL; | |
727 | ||
f459b602 | 728 | return c; |
991f2a39 ZJS |
729 | } |
730 | ||
e449de87 | 731 | static int list_units(int argc, char *argv[], void *userdata) { |
f459b602 | 732 | _cleanup_free_ UnitInfo *unit_infos = NULL; |
1238ee09 LP |
733 | _cleanup_(message_set_freep) Set *replies = NULL; |
734 | _cleanup_strv_free_ char **machines = NULL; | |
4fbd7192 | 735 | sd_bus *bus; |
991f2a39 ZJS |
736 | int r; |
737 | ||
4fbd7192 LP |
738 | r = acquire_bus(BUS_MANAGER, &bus); |
739 | if (r < 0) | |
740 | return r; | |
741 | ||
d2ad7e1b ZJS |
742 | pager_open(arg_no_pager, false); |
743 | ||
e449de87 | 744 | r = get_unit_list_recursive(bus, strv_skip(argv, 1), &unit_infos, &replies, &machines); |
991f2a39 ZJS |
745 | if (r < 0) |
746 | return r; | |
747 | ||
f459b602 | 748 | qsort_safe(unit_infos, r, sizeof(UnitInfo), compare_unit_info); |
1238ee09 | 749 | return output_units_list(unit_infos, r); |
991f2a39 ZJS |
750 | } |
751 | ||
a00963a2 | 752 | static int get_triggered_units( |
f459b602 MAP |
753 | sd_bus *bus, |
754 | const char* path, | |
755 | char*** ret) { | |
a00963a2 | 756 | |
4afd3348 | 757 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
991f2a39 ZJS |
758 | int r; |
759 | ||
4fbd7192 LP |
760 | assert(bus); |
761 | assert(path); | |
762 | assert(ret); | |
763 | ||
f459b602 MAP |
764 | r = sd_bus_get_property_strv( |
765 | bus, | |
766 | "org.freedesktop.systemd1", | |
767 | path, | |
768 | "org.freedesktop.systemd1.Unit", | |
769 | "Triggers", | |
770 | &error, | |
771 | ret); | |
f459b602 | 772 | if (r < 0) |
691395d8 | 773 | return log_error_errno(r, "Failed to determine triggers: %s", bus_error_message(&error, r)); |
991f2a39 ZJS |
774 | |
775 | return 0; | |
776 | } | |
777 | ||
f459b602 MAP |
778 | static int get_listening( |
779 | sd_bus *bus, | |
780 | const char* unit_path, | |
cbb76c29 | 781 | char*** listening) { |
f459b602 | 782 | |
4afd3348 LP |
783 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
784 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
f459b602 | 785 | const char *type, *path; |
cbb76c29 | 786 | int r, n = 0; |
991f2a39 | 787 | |
f459b602 MAP |
788 | r = sd_bus_get_property( |
789 | bus, | |
790 | "org.freedesktop.systemd1", | |
791 | unit_path, | |
792 | "org.freedesktop.systemd1.Socket", | |
793 | "Listen", | |
794 | &error, | |
795 | &reply, | |
796 | "a(ss)"); | |
691395d8 LP |
797 | if (r < 0) |
798 | return log_error_errno(r, "Failed to get list of listening sockets: %s", bus_error_message(&error, r)); | |
991f2a39 | 799 | |
f459b602 MAP |
800 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ss)"); |
801 | if (r < 0) | |
802 | return bus_log_parse_error(r); | |
991f2a39 | 803 | |
f459b602 | 804 | while ((r = sd_bus_message_read(reply, "(ss)", &type, &path)) > 0) { |
36c32ba2 | 805 | |
0d95178e | 806 | r = strv_extend(listening, type); |
f459b602 MAP |
807 | if (r < 0) |
808 | return log_oom(); | |
991f2a39 | 809 | |
0d95178e | 810 | r = strv_extend(listening, path); |
f459b602 MAP |
811 | if (r < 0) |
812 | return log_oom(); | |
7e4249b9 | 813 | |
cbb76c29 | 814 | n++; |
36c32ba2 | 815 | } |
f459b602 MAP |
816 | if (r < 0) |
817 | return bus_log_parse_error(r); | |
818 | ||
819 | r = sd_bus_message_exit_container(reply); | |
820 | if (r < 0) | |
821 | return bus_log_parse_error(r); | |
36c32ba2 | 822 | |
cbb76c29 | 823 | return n; |
991f2a39 ZJS |
824 | } |
825 | ||
826 | struct socket_info { | |
0cfc3525 | 827 | const char *machine; |
991f2a39 ZJS |
828 | const char* id; |
829 | ||
830 | char* type; | |
831 | char* path; | |
832 | ||
833 | /* Note: triggered is a list here, although it almost certainly | |
834 | * will always be one unit. Nevertheless, dbus API allows for multiple | |
f7340ab2 | 835 | * values, so let's follow that. */ |
991f2a39 ZJS |
836 | char** triggered; |
837 | ||
838 | /* The strv above is shared. free is set only in the first one. */ | |
839 | bool own_triggered; | |
840 | }; | |
841 | ||
cbb76c29 | 842 | static int socket_info_compare(const struct socket_info *a, const struct socket_info *b) { |
cbc9fbd1 LP |
843 | int o; |
844 | ||
cbb76c29 LP |
845 | assert(a); |
846 | assert(b); | |
847 | ||
0cfc3525 TA |
848 | if (!a->machine && b->machine) |
849 | return -1; | |
850 | if (a->machine && !b->machine) | |
851 | return 1; | |
852 | if (a->machine && b->machine) { | |
853 | o = strcasecmp(a->machine, b->machine); | |
854 | if (o != 0) | |
855 | return o; | |
856 | } | |
857 | ||
cbc9fbd1 | 858 | o = strcmp(a->path, b->path); |
991f2a39 ZJS |
859 | if (o == 0) |
860 | o = strcmp(a->type, b->type); | |
cbc9fbd1 | 861 | |
991f2a39 ZJS |
862 | return o; |
863 | } | |
864 | ||
865 | static int output_sockets_list(struct socket_info *socket_infos, unsigned cs) { | |
866 | struct socket_info *s; | |
f8294e41 JT |
867 | unsigned pathlen = strlen("LISTEN"), |
868 | typelen = strlen("TYPE") * arg_show_types, | |
869 | socklen = strlen("UNIT"), | |
870 | servlen = strlen("ACTIVATES"); | |
991f2a39 ZJS |
871 | const char *on, *off; |
872 | ||
873 | for (s = socket_infos; s < socket_infos + cs; s++) { | |
991f2a39 | 874 | unsigned tmp = 0; |
cbc9fbd1 | 875 | char **a; |
991f2a39 ZJS |
876 | |
877 | socklen = MAX(socklen, strlen(s->id)); | |
878 | if (arg_show_types) | |
879 | typelen = MAX(typelen, strlen(s->type)); | |
0cfc3525 | 880 | pathlen = MAX(pathlen, strlen(s->path) + (s->machine ? strlen(s->machine)+1 : 0)); |
991f2a39 ZJS |
881 | |
882 | STRV_FOREACH(a, s->triggered) | |
883 | tmp += strlen(*a) + 2*(a != s->triggered); | |
884 | servlen = MAX(servlen, tmp); | |
885 | } | |
886 | ||
887 | if (cs) { | |
571bfc6c MM |
888 | if (!arg_no_legend) |
889 | printf("%-*s %-*.*s%-*s %s\n", | |
890 | pathlen, "LISTEN", | |
891 | typelen + arg_show_types, typelen + arg_show_types, "TYPE ", | |
892 | socklen, "UNIT", | |
893 | "ACTIVATES"); | |
991f2a39 ZJS |
894 | |
895 | for (s = socket_infos; s < socket_infos + cs; s++) { | |
0cfc3525 TA |
896 | _cleanup_free_ char *j = NULL; |
897 | const char *path; | |
991f2a39 ZJS |
898 | char **a; |
899 | ||
0cfc3525 TA |
900 | if (s->machine) { |
901 | j = strjoin(s->machine, ":", s->path, NULL); | |
902 | if (!j) | |
903 | return log_oom(); | |
904 | path = j; | |
905 | } else | |
906 | path = s->path; | |
907 | ||
991f2a39 ZJS |
908 | if (arg_show_types) |
909 | printf("%-*s %-*s %-*s", | |
0cfc3525 | 910 | pathlen, path, typelen, s->type, socklen, s->id); |
991f2a39 ZJS |
911 | else |
912 | printf("%-*s %-*s", | |
0cfc3525 | 913 | pathlen, path, socklen, s->id); |
991f2a39 ZJS |
914 | STRV_FOREACH(a, s->triggered) |
915 | printf("%s %s", | |
916 | a == s->triggered ? "" : ",", *a); | |
917 | printf("\n"); | |
918 | } | |
919 | ||
0b5a519c | 920 | on = ansi_highlight(); |
1fc464f6 | 921 | off = ansi_normal(); |
571bfc6c MM |
922 | if (!arg_no_legend) |
923 | printf("\n"); | |
991f2a39 | 924 | } else { |
0b5a519c | 925 | on = ansi_highlight_red(); |
1fc464f6 | 926 | off = ansi_normal(); |
991f2a39 ZJS |
927 | } |
928 | ||
571bfc6c MM |
929 | if (!arg_no_legend) { |
930 | printf("%s%u sockets listed.%s\n", on, cs, off); | |
931 | if (!arg_all) | |
932 | printf("Pass --all to see loaded but inactive sockets, too.\n"); | |
933 | } | |
265a7a2a ZJS |
934 | |
935 | return 0; | |
936 | } | |
937 | ||
e449de87 | 938 | static int list_sockets(int argc, char *argv[], void *userdata) { |
0cfc3525 TA |
939 | _cleanup_(message_set_freep) Set *replies = NULL; |
940 | _cleanup_strv_free_ char **machines = NULL; | |
f459b602 | 941 | _cleanup_free_ UnitInfo *unit_infos = NULL; |
8d5ba5a9 | 942 | _cleanup_free_ struct socket_info *socket_infos = NULL; |
f459b602 | 943 | const UnitInfo *u; |
991f2a39 | 944 | struct socket_info *s; |
cbb76c29 | 945 | unsigned cs = 0; |
991f2a39 | 946 | size_t size = 0; |
ad83b4c4 | 947 | int r = 0, n; |
4fbd7192 | 948 | sd_bus *bus; |
265a7a2a | 949 | |
4fbd7192 LP |
950 | r = acquire_bus(BUS_MANAGER, &bus); |
951 | if (r < 0) | |
952 | return r; | |
953 | ||
d2ad7e1b ZJS |
954 | pager_open(arg_no_pager, false); |
955 | ||
e449de87 | 956 | n = get_unit_list_recursive(bus, strv_skip(argv, 1), &unit_infos, &replies, &machines); |
cbb76c29 LP |
957 | if (n < 0) |
958 | return n; | |
265a7a2a | 959 | |
cbb76c29 | 960 | for (u = unit_infos; u < unit_infos + n; u++) { |
0d95178e | 961 | _cleanup_strv_free_ char **listening = NULL, **triggered = NULL; |
cbb76c29 | 962 | int i, c; |
991f2a39 | 963 | |
cbc9fbd1 | 964 | if (!endswith(u->id, ".socket")) |
991f2a39 ZJS |
965 | continue; |
966 | ||
967 | r = get_triggered_units(bus, u->unit_path, &triggered); | |
968 | if (r < 0) | |
969 | goto cleanup; | |
970 | ||
cbb76c29 LP |
971 | c = get_listening(bus, u->unit_path, &listening); |
972 | if (c < 0) { | |
973 | r = c; | |
991f2a39 | 974 | goto cleanup; |
cbb76c29 | 975 | } |
991f2a39 ZJS |
976 | |
977 | if (!GREEDY_REALLOC(socket_infos, size, cs + c)) { | |
978 | r = log_oom(); | |
979 | goto cleanup; | |
980 | } | |
981 | ||
982 | for (i = 0; i < c; i++) | |
983 | socket_infos[cs + i] = (struct socket_info) { | |
0cfc3525 | 984 | .machine = u->machine, |
991f2a39 | 985 | .id = u->id, |
0d95178e KS |
986 | .type = listening[i*2], |
987 | .path = listening[i*2 + 1], | |
991f2a39 ZJS |
988 | .triggered = triggered, |
989 | .own_triggered = i==0, | |
990 | }; | |
991 | ||
992 | /* from this point on we will cleanup those socket_infos */ | |
993 | cs += c; | |
0d95178e KS |
994 | free(listening); |
995 | listening = triggered = NULL; /* avoid cleanup */ | |
991f2a39 ZJS |
996 | } |
997 | ||
7ff7394d ZJS |
998 | qsort_safe(socket_infos, cs, sizeof(struct socket_info), |
999 | (__compar_fn_t) socket_info_compare); | |
991f2a39 ZJS |
1000 | |
1001 | output_sockets_list(socket_infos, cs); | |
1002 | ||
1003 | cleanup: | |
1004 | assert(cs == 0 || socket_infos); | |
1005 | for (s = socket_infos; s < socket_infos + cs; s++) { | |
1006 | free(s->type); | |
1007 | free(s->path); | |
1008 | if (s->own_triggered) | |
1009 | strv_free(s->triggered); | |
1010 | } | |
4445a875 | 1011 | |
872c8faa | 1012 | return r; |
4445a875 LP |
1013 | } |
1014 | ||
cbb76c29 LP |
1015 | static int get_next_elapse( |
1016 | sd_bus *bus, | |
1017 | const char *path, | |
1018 | dual_timestamp *next) { | |
1019 | ||
4afd3348 | 1020 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
cbb76c29 LP |
1021 | dual_timestamp t; |
1022 | int r; | |
1023 | ||
1024 | assert(bus); | |
1025 | assert(path); | |
1026 | assert(next); | |
1027 | ||
1028 | r = sd_bus_get_property_trivial( | |
1029 | bus, | |
1030 | "org.freedesktop.systemd1", | |
1031 | path, | |
1032 | "org.freedesktop.systemd1.Timer", | |
1033 | "NextElapseUSecMonotonic", | |
1034 | &error, | |
1035 | 't', | |
1036 | &t.monotonic); | |
691395d8 LP |
1037 | if (r < 0) |
1038 | return log_error_errno(r, "Failed to get next elapsation time: %s", bus_error_message(&error, r)); | |
cbb76c29 LP |
1039 | |
1040 | r = sd_bus_get_property_trivial( | |
1041 | bus, | |
1042 | "org.freedesktop.systemd1", | |
1043 | path, | |
1044 | "org.freedesktop.systemd1.Timer", | |
1045 | "NextElapseUSecRealtime", | |
1046 | &error, | |
1047 | 't', | |
1048 | &t.realtime); | |
691395d8 LP |
1049 | if (r < 0) |
1050 | return log_error_errno(r, "Failed to get next elapsation time: %s", bus_error_message(&error, r)); | |
cbb76c29 LP |
1051 | |
1052 | *next = t; | |
1053 | return 0; | |
1054 | } | |
1055 | ||
d784e2db LP |
1056 | static int get_last_trigger( |
1057 | sd_bus *bus, | |
1058 | const char *path, | |
1059 | usec_t *last) { | |
1060 | ||
4afd3348 | 1061 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
d784e2db LP |
1062 | int r; |
1063 | ||
1064 | assert(bus); | |
1065 | assert(path); | |
1066 | assert(last); | |
1067 | ||
1068 | r = sd_bus_get_property_trivial( | |
1069 | bus, | |
1070 | "org.freedesktop.systemd1", | |
1071 | path, | |
1072 | "org.freedesktop.systemd1.Timer", | |
dedabea4 | 1073 | "LastTriggerUSec", |
d784e2db LP |
1074 | &error, |
1075 | 't', | |
1076 | last); | |
691395d8 LP |
1077 | if (r < 0) |
1078 | return log_error_errno(r, "Failed to get last trigger time: %s", bus_error_message(&error, r)); | |
d784e2db LP |
1079 | |
1080 | return 0; | |
1081 | } | |
1082 | ||
cbb76c29 | 1083 | struct timer_info { |
806a37e7 | 1084 | const char* machine; |
cbb76c29 LP |
1085 | const char* id; |
1086 | usec_t next_elapse; | |
d784e2db | 1087 | usec_t last_trigger; |
cbb76c29 LP |
1088 | char** triggered; |
1089 | }; | |
1090 | ||
1091 | static int timer_info_compare(const struct timer_info *a, const struct timer_info *b) { | |
806a37e7 TA |
1092 | int o; |
1093 | ||
cbb76c29 LP |
1094 | assert(a); |
1095 | assert(b); | |
1096 | ||
806a37e7 TA |
1097 | if (!a->machine && b->machine) |
1098 | return -1; | |
1099 | if (a->machine && !b->machine) | |
1100 | return 1; | |
1101 | if (a->machine && b->machine) { | |
1102 | o = strcasecmp(a->machine, b->machine); | |
1103 | if (o != 0) | |
1104 | return o; | |
1105 | } | |
1106 | ||
cbb76c29 LP |
1107 | if (a->next_elapse < b->next_elapse) |
1108 | return -1; | |
1109 | if (a->next_elapse > b->next_elapse) | |
1110 | return 1; | |
1111 | ||
1112 | return strcmp(a->id, b->id); | |
1113 | } | |
1114 | ||
1115 | static int output_timers_list(struct timer_info *timer_infos, unsigned n) { | |
1116 | struct timer_info *t; | |
1117 | unsigned | |
f8294e41 JT |
1118 | nextlen = strlen("NEXT"), |
1119 | leftlen = strlen("LEFT"), | |
d784e2db LP |
1120 | lastlen = strlen("LAST"), |
1121 | passedlen = strlen("PASSED"), | |
f8294e41 JT |
1122 | unitlen = strlen("UNIT"), |
1123 | activatelen = strlen("ACTIVATES"); | |
cbb76c29 LP |
1124 | |
1125 | const char *on, *off; | |
1126 | ||
1127 | assert(timer_infos || n == 0); | |
1128 | ||
1129 | for (t = timer_infos; t < timer_infos + n; t++) { | |
1130 | unsigned ul = 0; | |
1131 | char **a; | |
1132 | ||
1133 | if (t->next_elapse > 0) { | |
1134 | char tstamp[FORMAT_TIMESTAMP_MAX] = "", trel[FORMAT_TIMESTAMP_RELATIVE_MAX] = ""; | |
1135 | ||
1136 | format_timestamp(tstamp, sizeof(tstamp), t->next_elapse); | |
1137 | nextlen = MAX(nextlen, strlen(tstamp) + 1); | |
1138 | ||
1139 | format_timestamp_relative(trel, sizeof(trel), t->next_elapse); | |
1140 | leftlen = MAX(leftlen, strlen(trel)); | |
1141 | } | |
1142 | ||
d784e2db LP |
1143 | if (t->last_trigger > 0) { |
1144 | char tstamp[FORMAT_TIMESTAMP_MAX] = "", trel[FORMAT_TIMESTAMP_RELATIVE_MAX] = ""; | |
1145 | ||
1146 | format_timestamp(tstamp, sizeof(tstamp), t->last_trigger); | |
1147 | lastlen = MAX(lastlen, strlen(tstamp) + 1); | |
1148 | ||
1149 | format_timestamp_relative(trel, sizeof(trel), t->last_trigger); | |
1150 | passedlen = MAX(passedlen, strlen(trel)); | |
1151 | } | |
1152 | ||
806a37e7 | 1153 | unitlen = MAX(unitlen, strlen(t->id) + (t->machine ? strlen(t->machine)+1 : 0)); |
cbb76c29 LP |
1154 | |
1155 | STRV_FOREACH(a, t->triggered) | |
1156 | ul += strlen(*a) + 2*(a != t->triggered); | |
d784e2db | 1157 | |
cbb76c29 LP |
1158 | activatelen = MAX(activatelen, ul); |
1159 | } | |
1160 | ||
1161 | if (n > 0) { | |
1162 | if (!arg_no_legend) | |
d784e2db LP |
1163 | printf("%-*s %-*s %-*s %-*s %-*s %s\n", |
1164 | nextlen, "NEXT", | |
1165 | leftlen, "LEFT", | |
1166 | lastlen, "LAST", | |
1167 | passedlen, "PASSED", | |
1168 | unitlen, "UNIT", | |
1169 | "ACTIVATES"); | |
cbb76c29 LP |
1170 | |
1171 | for (t = timer_infos; t < timer_infos + n; t++) { | |
806a37e7 TA |
1172 | _cleanup_free_ char *j = NULL; |
1173 | const char *unit; | |
d784e2db LP |
1174 | char tstamp1[FORMAT_TIMESTAMP_MAX] = "n/a", trel1[FORMAT_TIMESTAMP_RELATIVE_MAX] = "n/a"; |
1175 | char tstamp2[FORMAT_TIMESTAMP_MAX] = "n/a", trel2[FORMAT_TIMESTAMP_RELATIVE_MAX] = "n/a"; | |
cbb76c29 LP |
1176 | char **a; |
1177 | ||
d784e2db LP |
1178 | format_timestamp(tstamp1, sizeof(tstamp1), t->next_elapse); |
1179 | format_timestamp_relative(trel1, sizeof(trel1), t->next_elapse); | |
cbb76c29 | 1180 | |
d784e2db LP |
1181 | format_timestamp(tstamp2, sizeof(tstamp2), t->last_trigger); |
1182 | format_timestamp_relative(trel2, sizeof(trel2), t->last_trigger); | |
1183 | ||
806a37e7 TA |
1184 | if (t->machine) { |
1185 | j = strjoin(t->machine, ":", t->id, NULL); | |
1186 | if (!j) | |
1187 | return log_oom(); | |
1188 | unit = j; | |
1189 | } else | |
1190 | unit = t->id; | |
1191 | ||
d784e2db | 1192 | printf("%-*s %-*s %-*s %-*s %-*s", |
806a37e7 | 1193 | nextlen, tstamp1, leftlen, trel1, lastlen, tstamp2, passedlen, trel2, unitlen, unit); |
cbb76c29 LP |
1194 | |
1195 | STRV_FOREACH(a, t->triggered) | |
1196 | printf("%s %s", | |
1197 | a == t->triggered ? "" : ",", *a); | |
1198 | printf("\n"); | |
1199 | } | |
1200 | ||
1201 | on = ansi_highlight(); | |
1fc464f6 | 1202 | off = ansi_normal(); |
cbb76c29 LP |
1203 | if (!arg_no_legend) |
1204 | printf("\n"); | |
1205 | } else { | |
1206 | on = ansi_highlight_red(); | |
1fc464f6 | 1207 | off = ansi_normal(); |
cbb76c29 LP |
1208 | } |
1209 | ||
1210 | if (!arg_no_legend) { | |
1211 | printf("%s%u timers listed.%s\n", on, n, off); | |
1212 | if (!arg_all) | |
1213 | printf("Pass --all to see loaded but inactive timers, too.\n"); | |
1214 | } | |
1215 | ||
1216 | return 0; | |
1217 | } | |
1218 | ||
f5080e73 DH |
1219 | static usec_t calc_next_elapse(dual_timestamp *nw, dual_timestamp *next) { |
1220 | usec_t next_elapse; | |
1221 | ||
1222 | assert(nw); | |
1223 | assert(next); | |
1224 | ||
3a43da28 | 1225 | if (next->monotonic != USEC_INFINITY && next->monotonic > 0) { |
f5080e73 DH |
1226 | usec_t converted; |
1227 | ||
1228 | if (next->monotonic > nw->monotonic) | |
1229 | converted = nw->realtime + (next->monotonic - nw->monotonic); | |
1230 | else | |
1231 | converted = nw->realtime - (nw->monotonic - next->monotonic); | |
1232 | ||
3a43da28 | 1233 | if (next->realtime != USEC_INFINITY && next->realtime > 0) |
f5080e73 DH |
1234 | next_elapse = MIN(converted, next->realtime); |
1235 | else | |
1236 | next_elapse = converted; | |
1237 | ||
1238 | } else | |
1239 | next_elapse = next->realtime; | |
1240 | ||
1241 | return next_elapse; | |
1242 | } | |
1243 | ||
e449de87 | 1244 | static int list_timers(int argc, char *argv[], void *userdata) { |
806a37e7 TA |
1245 | _cleanup_(message_set_freep) Set *replies = NULL; |
1246 | _cleanup_strv_free_ char **machines = NULL; | |
cbb76c29 LP |
1247 | _cleanup_free_ struct timer_info *timer_infos = NULL; |
1248 | _cleanup_free_ UnitInfo *unit_infos = NULL; | |
1249 | struct timer_info *t; | |
1250 | const UnitInfo *u; | |
1251 | size_t size = 0; | |
1823b86e | 1252 | int n, c = 0; |
cbb76c29 | 1253 | dual_timestamp nw; |
4fbd7192 | 1254 | sd_bus *bus; |
1823b86e | 1255 | int r = 0; |
cbb76c29 | 1256 | |
4fbd7192 LP |
1257 | r = acquire_bus(BUS_MANAGER, &bus); |
1258 | if (r < 0) | |
1259 | return r; | |
1260 | ||
d2ad7e1b ZJS |
1261 | pager_open(arg_no_pager, false); |
1262 | ||
e449de87 | 1263 | n = get_unit_list_recursive(bus, strv_skip(argv, 1), &unit_infos, &replies, &machines); |
cbb76c29 LP |
1264 | if (n < 0) |
1265 | return n; | |
1266 | ||
1267 | dual_timestamp_get(&nw); | |
1268 | ||
1269 | for (u = unit_infos; u < unit_infos + n; u++) { | |
1270 | _cleanup_strv_free_ char **triggered = NULL; | |
5cb14b37 | 1271 | dual_timestamp next = DUAL_TIMESTAMP_NULL; |
d784e2db | 1272 | usec_t m, last = 0; |
cbb76c29 | 1273 | |
cbb76c29 LP |
1274 | if (!endswith(u->id, ".timer")) |
1275 | continue; | |
1276 | ||
1277 | r = get_triggered_units(bus, u->unit_path, &triggered); | |
1278 | if (r < 0) | |
1279 | goto cleanup; | |
1280 | ||
1281 | r = get_next_elapse(bus, u->unit_path, &next); | |
1282 | if (r < 0) | |
1283 | goto cleanup; | |
1284 | ||
d784e2db LP |
1285 | get_last_trigger(bus, u->unit_path, &last); |
1286 | ||
cbb76c29 LP |
1287 | if (!GREEDY_REALLOC(timer_infos, size, c+1)) { |
1288 | r = log_oom(); | |
1289 | goto cleanup; | |
1290 | } | |
1291 | ||
f5080e73 DH |
1292 | m = calc_next_elapse(&nw, &next); |
1293 | ||
cbb76c29 | 1294 | timer_infos[c++] = (struct timer_info) { |
806a37e7 | 1295 | .machine = u->machine, |
cbb76c29 LP |
1296 | .id = u->id, |
1297 | .next_elapse = m, | |
d784e2db | 1298 | .last_trigger = last, |
cbb76c29 LP |
1299 | .triggered = triggered, |
1300 | }; | |
1301 | ||
1302 | triggered = NULL; /* avoid cleanup */ | |
1303 | } | |
1304 | ||
1305 | qsort_safe(timer_infos, c, sizeof(struct timer_info), | |
1306 | (__compar_fn_t) timer_info_compare); | |
1307 | ||
1308 | output_timers_list(timer_infos, c); | |
1309 | ||
1310 | cleanup: | |
1311 | for (t = timer_infos; t < timer_infos + c; t++) | |
1312 | strv_free(t->triggered); | |
1313 | ||
1314 | return r; | |
1315 | } | |
1316 | ||
729e3769 LP |
1317 | static int compare_unit_file_list(const void *a, const void *b) { |
1318 | const char *d1, *d2; | |
1319 | const UnitFileList *u = a, *v = b; | |
1320 | ||
1321 | d1 = strrchr(u->path, '.'); | |
1322 | d2 = strrchr(v->path, '.'); | |
1323 | ||
1324 | if (d1 && d2) { | |
1325 | int r; | |
1326 | ||
1327 | r = strcasecmp(d1, d2); | |
1328 | if (r != 0) | |
1329 | return r; | |
1330 | } | |
1331 | ||
2b6bf07d | 1332 | return strcasecmp(basename(u->path), basename(v->path)); |
729e3769 LP |
1333 | } |
1334 | ||
313fe66f | 1335 | static bool output_show_unit_file(const UnitFileList *u, char **states, char **patterns) { |
f8591ee1 LP |
1336 | assert(u); |
1337 | ||
2404701e | 1338 | if (!strv_fnmatch_or_empty(patterns, basename(u->path), FNM_NOESCAPE)) |
d8fba7c6 | 1339 | return false; |
d8fba7c6 | 1340 | |
6c71341a ZJS |
1341 | if (!strv_isempty(arg_types)) { |
1342 | const char *dot; | |
1343 | ||
1344 | dot = strrchr(u->path, '.'); | |
1345 | if (!dot) | |
1346 | return false; | |
1347 | ||
1348 | if (!strv_find(arg_types, dot+1)) | |
1349 | return false; | |
1350 | } | |
1351 | ||
313fe66f | 1352 | if (!strv_isempty(states) && |
1353 | !strv_find(states, unit_file_state_to_string(u->state))) | |
bceccd5e | 1354 | return false; |
fec1530e | 1355 | |
6c71341a | 1356 | return true; |
729e3769 LP |
1357 | } |
1358 | ||
8d5ba5a9 | 1359 | static void output_unit_file_list(const UnitFileList *units, unsigned c) { |
0d292f5e | 1360 | unsigned max_id_len, id_cols, state_cols; |
729e3769 LP |
1361 | const UnitFileList *u; |
1362 | ||
f8294e41 JT |
1363 | max_id_len = strlen("UNIT FILE"); |
1364 | state_cols = strlen("STATE"); | |
cbc9fbd1 | 1365 | |
1c0a113f | 1366 | for (u = units; u < units + c; u++) { |
2b6bf07d | 1367 | max_id_len = MAX(max_id_len, strlen(basename(u->path))); |
1c0a113f ZJS |
1368 | state_cols = MAX(state_cols, strlen(unit_file_state_to_string(u->state))); |
1369 | } | |
1370 | ||
1371 | if (!arg_full) { | |
1372 | unsigned basic_cols; | |
cbc9fbd1 | 1373 | |
9607d947 | 1374 | id_cols = MIN(max_id_len, 25u); |
1c0a113f ZJS |
1375 | basic_cols = 1 + id_cols + state_cols; |
1376 | if (basic_cols < (unsigned) columns()) | |
1377 | id_cols += MIN(columns() - basic_cols, max_id_len - id_cols); | |
1378 | } else | |
1379 | id_cols = max_id_len; | |
1380 | ||
11690bcc | 1381 | if (!arg_no_legend && c > 0) |
cbc9fbd1 LP |
1382 | printf("%-*s %-*s\n", |
1383 | id_cols, "UNIT FILE", | |
1384 | state_cols, "STATE"); | |
729e3769 LP |
1385 | |
1386 | for (u = units; u < units + c; u++) { | |
7fd1b19b | 1387 | _cleanup_free_ char *e = NULL; |
729e3769 LP |
1388 | const char *on, *off; |
1389 | const char *id; | |
1390 | ||
4c315c2c IS |
1391 | if (IN_SET(u->state, |
1392 | UNIT_FILE_MASKED, | |
1393 | UNIT_FILE_MASKED_RUNTIME, | |
1394 | UNIT_FILE_DISABLED, | |
0ec0deaa | 1395 | UNIT_FILE_BAD)) { |
0b5a519c | 1396 | on = ansi_highlight_red(); |
1fc464f6 | 1397 | off = ansi_normal(); |
729e3769 | 1398 | } else if (u->state == UNIT_FILE_ENABLED) { |
0b5a519c | 1399 | on = ansi_highlight_green(); |
1fc464f6 | 1400 | off = ansi_normal(); |
729e3769 LP |
1401 | } else |
1402 | on = off = ""; | |
1403 | ||
2b6bf07d | 1404 | id = basename(u->path); |
729e3769 | 1405 | |
1c0a113f | 1406 | e = arg_full ? NULL : ellipsize(id, id_cols, 33); |
729e3769 | 1407 | |
1c0a113f ZJS |
1408 | printf("%-*s %s%-*s%s\n", |
1409 | id_cols, e ? e : id, | |
1410 | on, state_cols, unit_file_state_to_string(u->state), off); | |
729e3769 LP |
1411 | } |
1412 | ||
1c0a113f | 1413 | if (!arg_no_legend) |
0d292f5e | 1414 | printf("\n%u unit files listed.\n", c); |
729e3769 LP |
1415 | } |
1416 | ||
e449de87 | 1417 | static int list_unit_files(int argc, char *argv[], void *userdata) { |
4afd3348 | 1418 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
f84190d8 | 1419 | _cleanup_free_ UnitFileList *units = NULL; |
8d5ba5a9 ZJS |
1420 | UnitFileList *unit; |
1421 | size_t size = 0; | |
f459b602 MAP |
1422 | unsigned c = 0; |
1423 | const char *state; | |
1424 | char *path; | |
f84190d8 | 1425 | int r; |
313fe66f | 1426 | bool fallback = false; |
729e3769 | 1427 | |
4fbd7192 | 1428 | if (install_client_side()) { |
729e3769 LP |
1429 | Hashmap *h; |
1430 | UnitFileList *u; | |
1431 | Iterator i; | |
f459b602 | 1432 | unsigned n_units; |
729e3769 | 1433 | |
d5099efc | 1434 | h = hashmap_new(&string_hash_ops); |
0d0f0c50 SL |
1435 | if (!h) |
1436 | return log_oom(); | |
729e3769 | 1437 | |
313fe66f | 1438 | r = unit_file_get_list(arg_scope, arg_root, h, arg_states, strv_skip(argv, 1)); |
729e3769 | 1439 | if (r < 0) { |
8ea913b2 | 1440 | unit_file_list_free(h); |
691395d8 | 1441 | return log_error_errno(r, "Failed to get unit file list: %m"); |
729e3769 LP |
1442 | } |
1443 | ||
1444 | n_units = hashmap_size(h); | |
fdbdf6ec | 1445 | |
0da999fa ZJS |
1446 | units = new(UnitFileList, n_units ?: 1); /* avoid malloc(0) */ |
1447 | if (!units) { | |
729e3769 | 1448 | unit_file_list_free(h); |
0d0f0c50 | 1449 | return log_oom(); |
729e3769 LP |
1450 | } |
1451 | ||
1452 | HASHMAP_FOREACH(u, h, i) { | |
313fe66f | 1453 | if (!output_show_unit_file(u, NULL, NULL)) |
8d5ba5a9 ZJS |
1454 | continue; |
1455 | ||
1456 | units[c++] = *u; | |
729e3769 LP |
1457 | free(u); |
1458 | } | |
1459 | ||
8d5ba5a9 | 1460 | assert(c <= n_units); |
729e3769 | 1461 | hashmap_free(h); |
5f056378 CH |
1462 | |
1463 | r = 0; | |
729e3769 | 1464 | } else { |
313fe66f | 1465 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
4afd3348 | 1466 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4fbd7192 LP |
1467 | sd_bus *bus; |
1468 | ||
1469 | r = acquire_bus(BUS_MANAGER, &bus); | |
1470 | if (r < 0) | |
1471 | return r; | |
6e646d22 | 1472 | |
313fe66f | 1473 | r = sd_bus_message_new_method_call( |
f22f08cd | 1474 | bus, |
313fe66f | 1475 | &m, |
729e3769 LP |
1476 | "org.freedesktop.systemd1", |
1477 | "/org/freedesktop/systemd1", | |
1478 | "org.freedesktop.systemd1.Manager", | |
313fe66f | 1479 | "ListUnitFilesByPatterns"); |
1480 | if (r < 0) | |
1481 | return bus_log_create_error(r); | |
1482 | ||
1483 | r = sd_bus_message_append_strv(m, arg_states); | |
1484 | if (r < 0) | |
1485 | return bus_log_create_error(r); | |
1486 | ||
1487 | r = sd_bus_message_append_strv(m, strv_skip(argv, 1)); | |
1488 | if (r < 0) | |
1489 | return bus_log_create_error(r); | |
1490 | ||
1491 | r = sd_bus_call(bus, m, 0, &error, &reply); | |
1492 | if (r < 0 && sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_METHOD)) { | |
1493 | /* Fallback to legacy ListUnitFiles method */ | |
1494 | fallback = true; | |
1495 | log_debug_errno(r, "Failed to list unit files: %s Falling back to ListUnitsFiles method.", bus_error_message(&error, r)); | |
1496 | m = sd_bus_message_unref(m); | |
1497 | sd_bus_error_free(&error); | |
1498 | ||
1499 | r = sd_bus_message_new_method_call( | |
1500 | bus, | |
1501 | &m, | |
1502 | "org.freedesktop.systemd1", | |
1503 | "/org/freedesktop/systemd1", | |
1504 | "org.freedesktop.systemd1.Manager", | |
1505 | "ListUnitFiles"); | |
1506 | if (r < 0) | |
1507 | return bus_log_create_error(r); | |
1508 | ||
1509 | r = sd_bus_call(bus, m, 0, &error, &reply); | |
1510 | } | |
691395d8 LP |
1511 | if (r < 0) |
1512 | return log_error_errno(r, "Failed to list unit files: %s", bus_error_message(&error, r)); | |
729e3769 | 1513 | |
f459b602 MAP |
1514 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ss)"); |
1515 | if (r < 0) | |
1516 | return bus_log_parse_error(r); | |
729e3769 | 1517 | |
f459b602 | 1518 | while ((r = sd_bus_message_read(reply, "(ss)", &path, &state)) > 0) { |
729e3769 | 1519 | |
f459b602 MAP |
1520 | if (!GREEDY_REALLOC(units, size, c + 1)) |
1521 | return log_oom(); | |
729e3769 | 1522 | |
8d5ba5a9 | 1523 | units[c] = (struct UnitFileList) { |
f459b602 MAP |
1524 | path, |
1525 | unit_file_state_from_string(state) | |
1526 | }; | |
8d5ba5a9 | 1527 | |
313fe66f | 1528 | if (output_show_unit_file(&units[c], |
1529 | fallback ? arg_states : NULL, | |
1530 | fallback ? strv_skip(argv, 1) : NULL)) | |
313cefa1 | 1531 | c++; |
8d5ba5a9 | 1532 | |
729e3769 | 1533 | } |
f459b602 MAP |
1534 | if (r < 0) |
1535 | return bus_log_parse_error(r); | |
1536 | ||
1537 | r = sd_bus_message_exit_container(reply); | |
1538 | if (r < 0) | |
1539 | return bus_log_parse_error(r); | |
729e3769 LP |
1540 | } |
1541 | ||
d2ad7e1b ZJS |
1542 | pager_open(arg_no_pager, false); |
1543 | ||
4fe1be9c LP |
1544 | qsort_safe(units, c, sizeof(UnitFileList), compare_unit_file_list); |
1545 | output_unit_file_list(units, c); | |
729e3769 | 1546 | |
0da999fa | 1547 | if (install_client_side()) |
8d5ba5a9 ZJS |
1548 | for (unit = units; unit < units + c; unit++) |
1549 | free(unit->path); | |
1550 | ||
f84190d8 | 1551 | return 0; |
729e3769 LP |
1552 | } |
1553 | ||
55c0b89c | 1554 | static int list_dependencies_print(const char *name, int level, unsigned int branches, bool last) { |
55c0b89c | 1555 | _cleanup_free_ char *n = NULL; |
9607d947 | 1556 | size_t max_len = MAX(columns(),20u); |
cbc9fbd1 LP |
1557 | size_t len = 0; |
1558 | int i; | |
55c0b89c | 1559 | |
5d0c05e5 | 1560 | if (!arg_plain) { |
cbc9fbd1 | 1561 | |
5d0c05e5 LN |
1562 | for (i = level - 1; i >= 0; i--) { |
1563 | len += 2; | |
f168c273 | 1564 | if (len > max_len - 3 && !arg_full) { |
5d0c05e5 LN |
1565 | printf("%s...\n",max_len % 2 ? "" : " "); |
1566 | return 0; | |
1567 | } | |
323b7dc9 | 1568 | printf("%s", special_glyph(branches & (1 << i) ? TREE_VERTICAL : TREE_SPACE)); |
5d0c05e5 | 1569 | } |
55c0b89c | 1570 | len += 2; |
cbc9fbd1 | 1571 | |
f168c273 | 1572 | if (len > max_len - 3 && !arg_full) { |
55c0b89c LN |
1573 | printf("%s...\n",max_len % 2 ? "" : " "); |
1574 | return 0; | |
1575 | } | |
cbc9fbd1 | 1576 | |
323b7dc9 | 1577 | printf("%s", special_glyph(last ? TREE_RIGHT : TREE_BRANCH)); |
55c0b89c | 1578 | } |
55c0b89c | 1579 | |
9ed794a3 | 1580 | if (arg_full) { |
55c0b89c LN |
1581 | printf("%s\n", name); |
1582 | return 0; | |
1583 | } | |
1584 | ||
1585 | n = ellipsize(name, max_len-len, 100); | |
f168c273 | 1586 | if (!n) |
55c0b89c LN |
1587 | return log_oom(); |
1588 | ||
1589 | printf("%s\n", n); | |
1590 | return 0; | |
1591 | } | |
1592 | ||
f459b602 MAP |
1593 | static int list_dependencies_get_dependencies(sd_bus *bus, const char *name, char ***deps) { |
1594 | ||
071066a5 | 1595 | static const char *dependencies[_DEPENDENCY_MAX] = { |
afba4199 | 1596 | [DEPENDENCY_FORWARD] = "Requires\0" |
afba4199 | 1597 | "Requisite\0" |
c469089c | 1598 | "Wants\0" |
fb30c438 | 1599 | "ConsistsOf\0" |
c469089c | 1600 | "BindsTo\0", |
afba4199 | 1601 | [DEPENDENCY_REVERSE] = "RequiredBy\0" |
1143adf7 | 1602 | "RequisiteOf\0" |
afba4199 | 1603 | "WantedBy\0" |
c469089c ZJS |
1604 | "PartOf\0" |
1605 | "BoundBy\0", | |
afba4199 ZJS |
1606 | [DEPENDENCY_AFTER] = "After\0", |
1607 | [DEPENDENCY_BEFORE] = "Before\0", | |
1608 | }; | |
55c0b89c | 1609 | |
4afd3348 LP |
1610 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
1611 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
f459b602 MAP |
1612 | _cleanup_strv_free_ char **ret = NULL; |
1613 | _cleanup_free_ char *path = NULL; | |
1614 | int r; | |
55c0b89c LN |
1615 | |
1616 | assert(bus); | |
1617 | assert(name); | |
1618 | assert(deps); | |
071066a5 | 1619 | assert_cc(ELEMENTSOF(dependencies) == _DEPENDENCY_MAX); |
55c0b89c LN |
1620 | |
1621 | path = unit_dbus_path_from_name(name); | |
f459b602 MAP |
1622 | if (!path) |
1623 | return log_oom(); | |
55c0b89c | 1624 | |
f459b602 MAP |
1625 | r = sd_bus_call_method( |
1626 | bus, | |
1627 | "org.freedesktop.systemd1", | |
1628 | path, | |
1629 | "org.freedesktop.DBus.Properties", | |
1630 | "GetAll", | |
1631 | &error, | |
1632 | &reply, | |
1633 | "s", "org.freedesktop.systemd1.Unit"); | |
691395d8 LP |
1634 | if (r < 0) |
1635 | return log_error_errno(r, "Failed to get properties of %s: %s", name, bus_error_message(&error, r)); | |
55c0b89c | 1636 | |
f459b602 MAP |
1637 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "{sv}"); |
1638 | if (r < 0) | |
1639 | return bus_log_parse_error(r); | |
55c0b89c | 1640 | |
f459b602 | 1641 | while ((r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) { |
55c0b89c LN |
1642 | const char *prop; |
1643 | ||
f459b602 MAP |
1644 | r = sd_bus_message_read(reply, "s", &prop); |
1645 | if (r < 0) | |
1646 | return bus_log_parse_error(r); | |
55c0b89c | 1647 | |
f459b602 MAP |
1648 | if (!nulstr_contains(dependencies[arg_dependency], prop)) { |
1649 | r = sd_bus_message_skip(reply, "v"); | |
1650 | if (r < 0) | |
1651 | return bus_log_parse_error(r); | |
1652 | } else { | |
55c0b89c | 1653 | |
f459b602 MAP |
1654 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_VARIANT, "as"); |
1655 | if (r < 0) | |
1656 | return bus_log_parse_error(r); | |
55c0b89c | 1657 | |
f459b602 MAP |
1658 | r = bus_message_read_strv_extend(reply, &ret); |
1659 | if (r < 0) | |
1660 | return bus_log_parse_error(r); | |
55c0b89c | 1661 | |
f459b602 MAP |
1662 | r = sd_bus_message_exit_container(reply); |
1663 | if (r < 0) | |
1664 | return bus_log_parse_error(r); | |
1665 | } | |
55c0b89c | 1666 | |
f459b602 MAP |
1667 | r = sd_bus_message_exit_container(reply); |
1668 | if (r < 0) | |
1669 | return bus_log_parse_error(r); | |
55c0b89c | 1670 | |
f459b602 MAP |
1671 | } |
1672 | if (r < 0) | |
1673 | return bus_log_parse_error(r); | |
55c0b89c | 1674 | |
f459b602 MAP |
1675 | r = sd_bus_message_exit_container(reply); |
1676 | if (r < 0) | |
1677 | return bus_log_parse_error(r); | |
540e7dbe | 1678 | |
f459b602 MAP |
1679 | *deps = ret; |
1680 | ret = NULL; | |
540e7dbe | 1681 | |
f459b602 | 1682 | return 0; |
55c0b89c LN |
1683 | } |
1684 | ||
1685 | static int list_dependencies_compare(const void *_a, const void *_b) { | |
1686 | const char **a = (const char**) _a, **b = (const char**) _b; | |
cbc9fbd1 | 1687 | |
55c0b89c LN |
1688 | if (unit_name_to_type(*a) == UNIT_TARGET && unit_name_to_type(*b) != UNIT_TARGET) |
1689 | return 1; | |
1690 | if (unit_name_to_type(*a) != UNIT_TARGET && unit_name_to_type(*b) == UNIT_TARGET) | |
1691 | return -1; | |
cbc9fbd1 | 1692 | |
55c0b89c LN |
1693 | return strcasecmp(*a, *b); |
1694 | } | |
1695 | ||
f459b602 MAP |
1696 | static int list_dependencies_one( |
1697 | sd_bus *bus, | |
1698 | const char *name, | |
1699 | int level, | |
1700 | char ***units, | |
1701 | unsigned int branches) { | |
1702 | ||
e3e45d4f | 1703 | _cleanup_strv_free_ char **deps = NULL; |
55c0b89c | 1704 | char **c; |
55c0b89c LN |
1705 | int r = 0; |
1706 | ||
cbc9fbd1 LP |
1707 | assert(bus); |
1708 | assert(name); | |
1709 | assert(units); | |
1710 | ||
e3e45d4f SP |
1711 | r = strv_extend(units, name); |
1712 | if (r < 0) | |
55c0b89c LN |
1713 | return log_oom(); |
1714 | ||
1715 | r = list_dependencies_get_dependencies(bus, name, &deps); | |
1716 | if (r < 0) | |
cec7eda5 | 1717 | return r; |
55c0b89c | 1718 | |
7ff7394d | 1719 | qsort_safe(deps, strv_length(deps), sizeof (char*), list_dependencies_compare); |
55c0b89c LN |
1720 | |
1721 | STRV_FOREACH(c, deps) { | |
e3e45d4f | 1722 | if (strv_contains(*units, *c)) { |
5d0c05e5 LN |
1723 | if (!arg_plain) { |
1724 | r = list_dependencies_print("...", level + 1, (branches << 1) | (c[1] == NULL ? 0 : 1), 1); | |
1725 | if (r < 0) | |
1726 | return r; | |
1727 | } | |
55c0b89c LN |
1728 | continue; |
1729 | } | |
1730 | ||
250ba664 ZJS |
1731 | if (arg_plain) |
1732 | printf(" "); | |
1733 | else { | |
fa0d5878 | 1734 | UnitActiveState active_state = _UNIT_ACTIVE_STATE_INVALID; |
250ba664 ZJS |
1735 | const char *on; |
1736 | ||
fa0d5878 | 1737 | (void) get_state_one_unit(bus, *c, &active_state); |
baa9ecc1 | 1738 | |
fa0d5878 | 1739 | switch (active_state) { |
baa9ecc1 LP |
1740 | case UNIT_ACTIVE: |
1741 | case UNIT_RELOADING: | |
1742 | case UNIT_ACTIVATING: | |
1743 | on = ansi_highlight_green(); | |
1744 | break; | |
1745 | ||
1746 | case UNIT_INACTIVE: | |
1747 | case UNIT_DEACTIVATING: | |
1748 | on = ansi_normal(); | |
1749 | break; | |
1750 | ||
1751 | default: | |
1752 | on = ansi_highlight_red(); | |
1753 | break; | |
fa0d5878 BR |
1754 | } |
1755 | ||
323b7dc9 | 1756 | printf("%s%s%s ", on, special_glyph(BLACK_CIRCLE), ansi_normal()); |
250ba664 | 1757 | } |
dbc2c080 | 1758 | |
55c0b89c | 1759 | r = list_dependencies_print(*c, level, branches, c[1] == NULL); |
cec7eda5 ZJS |
1760 | if (r < 0) |
1761 | return r; | |
55c0b89c LN |
1762 | |
1763 | if (arg_all || unit_name_to_type(*c) == UNIT_TARGET) { | |
e3e45d4f | 1764 | r = list_dependencies_one(bus, *c, level + 1, units, (branches << 1) | (c[1] == NULL ? 0 : 1)); |
f168c273 | 1765 | if (r < 0) |
cec7eda5 | 1766 | return r; |
55c0b89c LN |
1767 | } |
1768 | } | |
f459b602 | 1769 | |
e3e45d4f SP |
1770 | if (!arg_plain) |
1771 | strv_remove(*units, name); | |
f459b602 | 1772 | |
cec7eda5 | 1773 | return 0; |
55c0b89c LN |
1774 | } |
1775 | ||
e449de87 | 1776 | static int list_dependencies(int argc, char *argv[], void *userdata) { |
5d0c05e5 | 1777 | _cleanup_strv_free_ char **units = NULL; |
f459b602 | 1778 | _cleanup_free_ char *unit = NULL; |
e31165b2 | 1779 | const char *u; |
4fbd7192 | 1780 | sd_bus *bus; |
7410616c | 1781 | int r; |
55c0b89c | 1782 | |
e449de87 LP |
1783 | if (argv[1]) { |
1784 | r = unit_name_mangle(argv[1], UNIT_NAME_NOGLOB, &unit); | |
7410616c LP |
1785 | if (r < 0) |
1786 | return log_error_errno(r, "Failed to mangle unit name: %m"); | |
1787 | ||
e31165b2 LP |
1788 | u = unit; |
1789 | } else | |
1790 | u = SPECIAL_DEFAULT_TARGET; | |
55c0b89c | 1791 | |
4fbd7192 LP |
1792 | r = acquire_bus(BUS_MANAGER, &bus); |
1793 | if (r < 0) | |
1794 | return r; | |
1795 | ||
d2ad7e1b ZJS |
1796 | pager_open(arg_no_pager, false); |
1797 | ||
e31165b2 LP |
1798 | puts(u); |
1799 | ||
5d0c05e5 | 1800 | return list_dependencies_one(bus, u, 0, &units, 0); |
55c0b89c LN |
1801 | } |
1802 | ||
0d292f5e LP |
1803 | struct machine_info { |
1804 | bool is_host; | |
1805 | char *name; | |
0d292f5e | 1806 | char *state; |
8fcf784d LP |
1807 | char *control_group; |
1808 | uint32_t n_failed_units; | |
1809 | uint32_t n_jobs; | |
1810 | usec_t timestamp; | |
1811 | }; | |
1812 | ||
1813 | static const struct bus_properties_map machine_info_property_map[] = { | |
1814 | { "SystemState", "s", NULL, offsetof(struct machine_info, state) }, | |
1815 | { "NJobs", "u", NULL, offsetof(struct machine_info, n_jobs) }, | |
1816 | { "NFailedUnits", "u", NULL, offsetof(struct machine_info, n_failed_units) }, | |
1817 | { "ControlGroup", "s", NULL, offsetof(struct machine_info, control_group) }, | |
1818 | { "UserspaceTimestamp", "t", NULL, offsetof(struct machine_info, timestamp) }, | |
1819 | {} | |
0d292f5e LP |
1820 | }; |
1821 | ||
e7e55dbd DH |
1822 | static void machine_info_clear(struct machine_info *info) { |
1823 | if (info) { | |
1824 | free(info->name); | |
1825 | free(info->state); | |
1826 | free(info->control_group); | |
1827 | zero(*info); | |
1828 | } | |
1829 | } | |
1830 | ||
0d292f5e LP |
1831 | static void free_machines_list(struct machine_info *machine_infos, int n) { |
1832 | int i; | |
1833 | ||
1834 | if (!machine_infos) | |
1835 | return; | |
1836 | ||
e7e55dbd DH |
1837 | for (i = 0; i < n; i++) |
1838 | machine_info_clear(&machine_infos[i]); | |
0d292f5e LP |
1839 | |
1840 | free(machine_infos); | |
1841 | } | |
1842 | ||
1843 | static int compare_machine_info(const void *a, const void *b) { | |
1844 | const struct machine_info *u = a, *v = b; | |
1845 | ||
1846 | if (u->is_host != v->is_host) | |
50933da0 | 1847 | return u->is_host > v->is_host ? -1 : 1; |
0d292f5e LP |
1848 | |
1849 | return strcasecmp(u->name, v->name); | |
1850 | } | |
1851 | ||
1852 | static int get_machine_properties(sd_bus *bus, struct machine_info *mi) { | |
4afd3348 | 1853 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *container = NULL; |
0d292f5e LP |
1854 | int r; |
1855 | ||
1856 | assert(mi); | |
1857 | ||
1858 | if (!bus) { | |
de33fc62 | 1859 | r = sd_bus_open_system_machine(&container, mi->name); |
0d292f5e LP |
1860 | if (r < 0) |
1861 | return r; | |
1862 | ||
1863 | bus = container; | |
1864 | } | |
1865 | ||
8fcf784d | 1866 | r = bus_map_all_properties(bus, "org.freedesktop.systemd1", "/org/freedesktop/systemd1", machine_info_property_map, mi); |
0d292f5e LP |
1867 | if (r < 0) |
1868 | return r; | |
1869 | ||
1870 | return 0; | |
1871 | } | |
1872 | ||
1873 | static bool output_show_machine(const char *name, char **patterns) { | |
2404701e | 1874 | return strv_fnmatch_or_empty(patterns, name, FNM_NOESCAPE); |
0d292f5e LP |
1875 | } |
1876 | ||
1877 | static int get_machine_list( | |
1878 | sd_bus *bus, | |
1879 | struct machine_info **_machine_infos, | |
1880 | char **patterns) { | |
1881 | ||
1882 | struct machine_info *machine_infos = NULL; | |
1883 | _cleanup_strv_free_ char **m = NULL; | |
1884 | _cleanup_free_ char *hn = NULL; | |
1885 | size_t sz = 0; | |
1886 | char **i; | |
4c3e8e39 | 1887 | int c = 0, r; |
0d292f5e LP |
1888 | |
1889 | hn = gethostname_malloc(); | |
1890 | if (!hn) | |
1891 | return log_oom(); | |
1892 | ||
1893 | if (output_show_machine(hn, patterns)) { | |
1894 | if (!GREEDY_REALLOC0(machine_infos, sz, c+1)) | |
1895 | return log_oom(); | |
1896 | ||
1897 | machine_infos[c].is_host = true; | |
1898 | machine_infos[c].name = hn; | |
1899 | hn = NULL; | |
1900 | ||
1901 | get_machine_properties(bus, &machine_infos[c]); | |
1902 | c++; | |
1903 | } | |
1904 | ||
4c3e8e39 LP |
1905 | r = sd_get_machine_names(&m); |
1906 | if (r < 0) | |
1907 | return log_error_errno(r, "Failed to get machine list: %m"); | |
1908 | ||
0d292f5e LP |
1909 | STRV_FOREACH(i, m) { |
1910 | _cleanup_free_ char *class = NULL; | |
1911 | ||
1912 | if (!output_show_machine(*i, patterns)) | |
1913 | continue; | |
1914 | ||
1915 | sd_machine_get_class(*i, &class); | |
1916 | if (!streq_ptr(class, "container")) | |
1917 | continue; | |
1918 | ||
1919 | if (!GREEDY_REALLOC0(machine_infos, sz, c+1)) { | |
1920 | free_machines_list(machine_infos, c); | |
1921 | return log_oom(); | |
1922 | } | |
1923 | ||
1924 | machine_infos[c].is_host = false; | |
1925 | machine_infos[c].name = strdup(*i); | |
1926 | if (!machine_infos[c].name) { | |
1927 | free_machines_list(machine_infos, c); | |
1928 | return log_oom(); | |
1929 | } | |
1930 | ||
1931 | get_machine_properties(NULL, &machine_infos[c]); | |
1932 | c++; | |
1933 | } | |
1934 | ||
1935 | *_machine_infos = machine_infos; | |
1936 | return c; | |
1937 | } | |
1938 | ||
1939 | static void output_machines_list(struct machine_info *machine_infos, unsigned n) { | |
1940 | struct machine_info *m; | |
1941 | unsigned | |
90c3f79d | 1942 | circle_len = 0, |
0d292f5e LP |
1943 | namelen = sizeof("NAME") - 1, |
1944 | statelen = sizeof("STATE") - 1, | |
1945 | failedlen = sizeof("FAILED") - 1, | |
1946 | jobslen = sizeof("JOBS") - 1; | |
1947 | ||
1948 | assert(machine_infos || n == 0); | |
1949 | ||
1950 | for (m = machine_infos; m < machine_infos + n; m++) { | |
1951 | namelen = MAX(namelen, strlen(m->name) + (m->is_host ? sizeof(" (host)") - 1 : 0)); | |
1952 | statelen = MAX(statelen, m->state ? strlen(m->state) : 0); | |
1953 | failedlen = MAX(failedlen, DECIMAL_STR_WIDTH(m->n_failed_units)); | |
1954 | jobslen = MAX(jobslen, DECIMAL_STR_WIDTH(m->n_jobs)); | |
90c3f79d | 1955 | |
250ba664 | 1956 | if (!arg_plain && !streq_ptr(m->state, "running")) |
90c3f79d | 1957 | circle_len = 2; |
0d292f5e LP |
1958 | } |
1959 | ||
90c3f79d LP |
1960 | if (!arg_no_legend) { |
1961 | if (circle_len > 0) | |
1962 | fputs(" ", stdout); | |
1963 | ||
0d292f5e LP |
1964 | printf("%-*s %-*s %-*s %-*s\n", |
1965 | namelen, "NAME", | |
1966 | statelen, "STATE", | |
1967 | failedlen, "FAILED", | |
1968 | jobslen, "JOBS"); | |
90c3f79d | 1969 | } |
0d292f5e LP |
1970 | |
1971 | for (m = machine_infos; m < machine_infos + n; m++) { | |
90c3f79d LP |
1972 | const char *on_state = "", *off_state = ""; |
1973 | const char *on_failed = "", *off_failed = ""; | |
1974 | bool circle = false; | |
0d292f5e LP |
1975 | |
1976 | if (streq_ptr(m->state, "degraded")) { | |
1977 | on_state = ansi_highlight_red(); | |
1fc464f6 | 1978 | off_state = ansi_normal(); |
90c3f79d | 1979 | circle = true; |
0d292f5e LP |
1980 | } else if (!streq_ptr(m->state, "running")) { |
1981 | on_state = ansi_highlight_yellow(); | |
1fc464f6 | 1982 | off_state = ansi_normal(); |
90c3f79d LP |
1983 | circle = true; |
1984 | } | |
0d292f5e LP |
1985 | |
1986 | if (m->n_failed_units > 0) { | |
1987 | on_failed = ansi_highlight_red(); | |
1fc464f6 | 1988 | off_failed = ansi_normal(); |
0d292f5e LP |
1989 | } else |
1990 | on_failed = off_failed = ""; | |
1991 | ||
90c3f79d | 1992 | if (circle_len > 0) |
323b7dc9 | 1993 | printf("%s%s%s ", on_state, circle ? special_glyph(BLACK_CIRCLE) : " ", off_state); |
90c3f79d | 1994 | |
0d292f5e | 1995 | if (m->is_host) |
c6ba5b80 | 1996 | printf("%-*s (host) %s%-*s%s %s%*" PRIu32 "%s %*" PRIu32 "\n", |
0d292f5e LP |
1997 | (int) (namelen - (sizeof(" (host)")-1)), strna(m->name), |
1998 | on_state, statelen, strna(m->state), off_state, | |
1999 | on_failed, failedlen, m->n_failed_units, off_failed, | |
2000 | jobslen, m->n_jobs); | |
2001 | else | |
c6ba5b80 | 2002 | printf("%-*s %s%-*s%s %s%*" PRIu32 "%s %*" PRIu32 "\n", |
0d292f5e LP |
2003 | namelen, strna(m->name), |
2004 | on_state, statelen, strna(m->state), off_state, | |
2005 | on_failed, failedlen, m->n_failed_units, off_failed, | |
2006 | jobslen, m->n_jobs); | |
2007 | } | |
2008 | ||
2009 | if (!arg_no_legend) | |
2010 | printf("\n%u machines listed.\n", n); | |
2011 | } | |
2012 | ||
e449de87 | 2013 | static int list_machines(int argc, char *argv[], void *userdata) { |
0d292f5e | 2014 | struct machine_info *machine_infos = NULL; |
4fbd7192 | 2015 | sd_bus *bus; |
0d292f5e LP |
2016 | int r; |
2017 | ||
0d292f5e LP |
2018 | if (geteuid() != 0) { |
2019 | log_error("Must be root."); | |
2020 | return -EPERM; | |
2021 | } | |
2022 | ||
4fbd7192 LP |
2023 | r = acquire_bus(BUS_MANAGER, &bus); |
2024 | if (r < 0) | |
2025 | return r; | |
2026 | ||
e449de87 | 2027 | r = get_machine_list(bus, &machine_infos, strv_skip(argv, 1)); |
0d292f5e LP |
2028 | if (r < 0) |
2029 | return r; | |
2030 | ||
d2ad7e1b ZJS |
2031 | pager_open(arg_no_pager, false); |
2032 | ||
0d292f5e LP |
2033 | qsort_safe(machine_infos, r, sizeof(struct machine_info), compare_machine_info); |
2034 | output_machines_list(machine_infos, r); | |
2035 | free_machines_list(machine_infos, r); | |
2036 | ||
2037 | return 0; | |
2038 | } | |
2039 | ||
e449de87 | 2040 | static int get_default(int argc, char *argv[], void *userdata) { |
4afd3348 | 2041 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
f459b602 MAP |
2042 | _cleanup_free_ char *_path = NULL; |
2043 | const char *path; | |
99504dd4 | 2044 | int r; |
99504dd4 | 2045 | |
4fbd7192 | 2046 | if (install_client_side()) { |
f459b602 | 2047 | r = unit_file_get_default(arg_scope, arg_root, &_path); |
f647962d MS |
2048 | if (r < 0) |
2049 | return log_error_errno(r, "Failed to get default target: %m"); | |
f459b602 | 2050 | path = _path; |
99504dd4 | 2051 | |
5f056378 | 2052 | r = 0; |
99504dd4 | 2053 | } else { |
4afd3348 | 2054 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4fbd7192 LP |
2055 | sd_bus *bus; |
2056 | ||
2057 | r = acquire_bus(BUS_MANAGER, &bus); | |
2058 | if (r < 0) | |
2059 | return r; | |
6e646d22 | 2060 | |
f459b602 MAP |
2061 | r = sd_bus_call_method( |
2062 | bus, | |
2063 | "org.freedesktop.systemd1", | |
2064 | "/org/freedesktop/systemd1", | |
2065 | "org.freedesktop.systemd1.Manager", | |
2066 | "GetDefaultTarget", | |
2067 | &error, | |
2068 | &reply, | |
2069 | NULL); | |
691395d8 LP |
2070 | if (r < 0) |
2071 | return log_error_errno(r, "Failed to get default target: %s", bus_error_message(&error, r)); | |
99504dd4 | 2072 | |
f459b602 MAP |
2073 | r = sd_bus_message_read(reply, "s", &path); |
2074 | if (r < 0) | |
2075 | return bus_log_parse_error(r); | |
99504dd4 VP |
2076 | } |
2077 | ||
2078 | if (path) | |
2079 | printf("%s\n", path); | |
2080 | ||
f459b602 | 2081 | return 0; |
99504dd4 VP |
2082 | } |
2083 | ||
e449de87 | 2084 | static int set_default(int argc, char *argv[], void *userdata) { |
718db961 | 2085 | _cleanup_free_ char *unit = NULL; |
acc0269c CH |
2086 | UnitFileChange *changes = NULL; |
2087 | unsigned n_changes = 0; | |
718db961 LP |
2088 | int r; |
2089 | ||
e449de87 LP |
2090 | assert(argc >= 2); |
2091 | assert(argv); | |
2092 | ||
2093 | r = unit_name_mangle_with_suffix(argv[1], UNIT_NAME_NOGLOB, ".target", &unit); | |
7410616c LP |
2094 | if (r < 0) |
2095 | return log_error_errno(r, "Failed to mangle unit name: %m"); | |
718db961 | 2096 | |
4fbd7192 | 2097 | if (install_client_side()) { |
a1484a21 | 2098 | r = unit_file_set_default(arg_scope, arg_root, unit, true, &changes, &n_changes); |
af3d8113 | 2099 | unit_file_dump_changes(r, "set default", changes, n_changes, arg_quiet); |
5f056378 CH |
2100 | |
2101 | if (r > 0) | |
2102 | r = 0; | |
718db961 | 2103 | } else { |
4afd3348 LP |
2104 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
2105 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
4fbd7192 | 2106 | sd_bus *bus; |
718db961 | 2107 | |
6e646d22 LP |
2108 | polkit_agent_open_if_enabled(); |
2109 | ||
4fbd7192 LP |
2110 | r = acquire_bus(BUS_MANAGER, &bus); |
2111 | if (r < 0) | |
2112 | return r; | |
2113 | ||
6e646d22 | 2114 | r = sd_bus_call_method( |
718db961 LP |
2115 | bus, |
2116 | "org.freedesktop.systemd1", | |
2117 | "/org/freedesktop/systemd1", | |
2118 | "org.freedesktop.systemd1.Manager", | |
6e646d22 LP |
2119 | "SetDefaultTarget", |
2120 | &error, | |
2121 | &reply, | |
2122 | "sb", unit, 1); | |
691395d8 LP |
2123 | if (r < 0) |
2124 | return log_error_errno(r, "Failed to set default target: %s", bus_error_message(&error, r)); | |
718db961 | 2125 | |
acc0269c | 2126 | r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet, &changes, &n_changes); |
718db961 | 2127 | if (r < 0) |
acc0269c | 2128 | goto finish; |
718db961 | 2129 | |
93c941e3 | 2130 | /* Try to reload if enabled */ |
718db961 | 2131 | if (!arg_no_reload) |
e449de87 | 2132 | r = daemon_reload(argc, argv, userdata); |
718db961 LP |
2133 | else |
2134 | r = 0; | |
2135 | } | |
2136 | ||
acc0269c CH |
2137 | finish: |
2138 | unit_file_changes_free(changes, n_changes); | |
2139 | ||
718db961 LP |
2140 | return r; |
2141 | } | |
2142 | ||
75add28a ZJS |
2143 | struct job_info { |
2144 | uint32_t id; | |
f459b602 | 2145 | const char *name, *type, *state; |
75add28a ZJS |
2146 | }; |
2147 | ||
d8fba7c6 | 2148 | static void output_jobs_list(const struct job_info* jobs, unsigned n, bool skipped) { |
f459b602 MAP |
2149 | unsigned id_len, unit_len, type_len, state_len; |
2150 | const struct job_info *j; | |
75add28a ZJS |
2151 | const char *on, *off; |
2152 | bool shorten = false; | |
2153 | ||
2154 | assert(n == 0 || jobs); | |
2155 | ||
2156 | if (n == 0) { | |
250ba664 ZJS |
2157 | if (!arg_no_legend) { |
2158 | on = ansi_highlight_green(); | |
1fc464f6 | 2159 | off = ansi_normal(); |
75add28a | 2160 | |
250ba664 ZJS |
2161 | printf("%sNo jobs %s.%s\n", on, skipped ? "listed" : "running", off); |
2162 | } | |
75add28a ZJS |
2163 | return; |
2164 | } | |
2165 | ||
ea4b98e6 | 2166 | pager_open(arg_no_pager, false); |
75add28a | 2167 | |
f8294e41 JT |
2168 | id_len = strlen("JOB"); |
2169 | unit_len = strlen("UNIT"); | |
2170 | type_len = strlen("TYPE"); | |
2171 | state_len = strlen("STATE"); | |
6d6d40c9 | 2172 | |
f459b602 MAP |
2173 | for (j = jobs; j < jobs + n; j++) { |
2174 | uint32_t id = j->id; | |
2175 | assert(j->name && j->type && j->state); | |
75add28a | 2176 | |
f459b602 MAP |
2177 | id_len = MAX(id_len, DECIMAL_STR_WIDTH(id)); |
2178 | unit_len = MAX(unit_len, strlen(j->name)); | |
2179 | type_len = MAX(type_len, strlen(j->type)); | |
2180 | state_len = MAX(state_len, strlen(j->state)); | |
2181 | } | |
75add28a | 2182 | |
f459b602 MAP |
2183 | if (!arg_full && id_len + 1 + unit_len + type_len + 1 + state_len > columns()) { |
2184 | unit_len = MAX(33u, columns() - id_len - type_len - state_len - 3); | |
2185 | shorten = true; | |
2186 | } | |
75add28a | 2187 | |
6ce774fd MM |
2188 | if (!arg_no_legend) |
2189 | printf("%*s %-*s %-*s %-*s\n", | |
2190 | id_len, "JOB", | |
2191 | unit_len, "UNIT", | |
2192 | type_len, "TYPE", | |
2193 | state_len, "STATE"); | |
75add28a | 2194 | |
f459b602 MAP |
2195 | for (j = jobs; j < jobs + n; j++) { |
2196 | _cleanup_free_ char *e = NULL; | |
75add28a | 2197 | |
f459b602 MAP |
2198 | if (streq(j->state, "running")) { |
2199 | on = ansi_highlight(); | |
1fc464f6 | 2200 | off = ansi_normal(); |
f459b602 MAP |
2201 | } else |
2202 | on = off = ""; | |
2203 | ||
2204 | e = shorten ? ellipsize(j->name, unit_len, 33) : NULL; | |
2205 | printf("%*u %s%-*s%s %-*s %s%-*s%s\n", | |
2206 | id_len, j->id, | |
2207 | on, unit_len, e ? e : j->name, off, | |
2208 | type_len, j->type, | |
2209 | on, state_len, j->state, off); | |
75add28a ZJS |
2210 | } |
2211 | ||
6ce774fd MM |
2212 | if (!arg_no_legend) { |
2213 | on = ansi_highlight(); | |
1fc464f6 | 2214 | off = ansi_normal(); |
75add28a | 2215 | |
6ce774fd MM |
2216 | printf("\n%s%u jobs listed%s.\n", on, n, off); |
2217 | } | |
75add28a ZJS |
2218 | } |
2219 | ||
d8fba7c6 | 2220 | static bool output_show_job(struct job_info *job, char **patterns) { |
2404701e | 2221 | return strv_fnmatch_or_empty(patterns, job->name, FNM_NOESCAPE); |
d8fba7c6 ZJS |
2222 | } |
2223 | ||
e449de87 | 2224 | static int list_jobs(int argc, char *argv[], void *userdata) { |
4afd3348 LP |
2225 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
2226 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
f459b602 MAP |
2227 | const char *name, *type, *state, *job_path, *unit_path; |
2228 | _cleanup_free_ struct job_info *jobs = NULL; | |
2229 | size_t size = 0; | |
2230 | unsigned c = 0; | |
4fbd7192 | 2231 | sd_bus *bus; |
f459b602 | 2232 | uint32_t id; |
f84190d8 | 2233 | int r; |
d8fba7c6 | 2234 | bool skipped = false; |
ec14911e | 2235 | |
4fbd7192 LP |
2236 | r = acquire_bus(BUS_MANAGER, &bus); |
2237 | if (r < 0) | |
2238 | return r; | |
2239 | ||
f459b602 | 2240 | r = sd_bus_call_method( |
f22f08cd SP |
2241 | bus, |
2242 | "org.freedesktop.systemd1", | |
2243 | "/org/freedesktop/systemd1", | |
2244 | "org.freedesktop.systemd1.Manager", | |
2245 | "ListJobs", | |
f459b602 | 2246 | &error, |
f22f08cd | 2247 | &reply, |
f459b602 | 2248 | NULL); |
691395d8 LP |
2249 | if (r < 0) |
2250 | return log_error_errno(r, "Failed to list jobs: %s", bus_error_message(&error, r)); | |
7e4249b9 | 2251 | |
f459b602 MAP |
2252 | r = sd_bus_message_enter_container(reply, 'a', "(usssoo)"); |
2253 | if (r < 0) | |
2254 | return bus_log_parse_error(r); | |
7e4249b9 | 2255 | |
f459b602 | 2256 | while ((r = sd_bus_message_read(reply, "(usssoo)", &id, &name, &type, &state, &job_path, &unit_path)) > 0) { |
d8fba7c6 ZJS |
2257 | struct job_info job = { id, name, type, state }; |
2258 | ||
e449de87 | 2259 | if (!output_show_job(&job, strv_skip(argv, 1))) { |
d8fba7c6 ZJS |
2260 | skipped = true; |
2261 | continue; | |
2262 | } | |
8fe914ec | 2263 | |
f459b602 MAP |
2264 | if (!GREEDY_REALLOC(jobs, size, c + 1)) |
2265 | return log_oom(); | |
7e4249b9 | 2266 | |
d8fba7c6 | 2267 | jobs[c++] = job; |
7e4249b9 | 2268 | } |
f459b602 MAP |
2269 | if (r < 0) |
2270 | return bus_log_parse_error(r); | |
7e4249b9 | 2271 | |
f459b602 MAP |
2272 | r = sd_bus_message_exit_container(reply); |
2273 | if (r < 0) | |
2274 | return bus_log_parse_error(r); | |
f73e33d9 | 2275 | |
d2ad7e1b ZJS |
2276 | pager_open(arg_no_pager, false); |
2277 | ||
d8fba7c6 | 2278 | output_jobs_list(jobs, c, skipped); |
91d0f17e | 2279 | return 0; |
7e4249b9 LP |
2280 | } |
2281 | ||
e449de87 | 2282 | static int cancel_job(int argc, char *argv[], void *userdata) { |
4fbd7192 | 2283 | sd_bus *bus; |
729e3769 | 2284 | char **name; |
342641fb | 2285 | int r = 0; |
7e4249b9 | 2286 | |
e449de87 | 2287 | if (argc <= 1) |
2853b60a | 2288 | return trivial_method(argc, argv, userdata); |
ee5762e3 | 2289 | |
4fbd7192 LP |
2290 | r = acquire_bus(BUS_MANAGER, &bus); |
2291 | if (r < 0) | |
2292 | return r; | |
2293 | ||
d2ad7e1b ZJS |
2294 | polkit_agent_open_if_enabled(); |
2295 | ||
e449de87 | 2296 | STRV_FOREACH(name, strv_skip(argv, 1)) { |
4afd3348 | 2297 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
5dd9014f | 2298 | uint32_t id; |
342641fb | 2299 | int q; |
7e4249b9 | 2300 | |
342641fb | 2301 | q = safe_atou32(*name, &id); |
f647962d MS |
2302 | if (q < 0) |
2303 | return log_error_errno(q, "Failed to parse job id \"%s\": %m", *name); | |
7e4249b9 | 2304 | |
6e646d22 | 2305 | q = sd_bus_call_method( |
f22f08cd SP |
2306 | bus, |
2307 | "org.freedesktop.systemd1", | |
2308 | "/org/freedesktop/systemd1", | |
2309 | "org.freedesktop.systemd1.Manager", | |
6e646d22 LP |
2310 | "CancelJob", |
2311 | &error, | |
2312 | NULL, | |
2313 | "u", id); | |
342641fb | 2314 | if (q < 0) { |
691395d8 | 2315 | log_error_errno(q, "Failed to cancel job %"PRIu32": %s", id, bus_error_message(&error, q)); |
342641fb LP |
2316 | if (r == 0) |
2317 | r = q; | |
f459b602 | 2318 | } |
7e4249b9 LP |
2319 | } |
2320 | ||
342641fb | 2321 | return r; |
7e4249b9 LP |
2322 | } |
2323 | ||
f459b602 | 2324 | static int need_daemon_reload(sd_bus *bus, const char *unit) { |
4afd3348 | 2325 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
f459b602 MAP |
2326 | const char *path; |
2327 | int b, r; | |
94c01aeb | 2328 | |
f459b602 MAP |
2329 | /* We ignore all errors here, since this is used to show a |
2330 | * warning only */ | |
45fb0699 | 2331 | |
f459b602 MAP |
2332 | /* We don't use unit_dbus_path_from_name() directly since we |
2333 | * don't want to load the unit if it isn't loaded. */ | |
f84190d8 | 2334 | |
f459b602 | 2335 | r = sd_bus_call_method( |
f22f08cd SP |
2336 | bus, |
2337 | "org.freedesktop.systemd1", | |
2338 | "/org/freedesktop/systemd1", | |
2339 | "org.freedesktop.systemd1.Manager", | |
2340 | "GetUnit", | |
f459b602 | 2341 | NULL, |
f22f08cd | 2342 | &reply, |
e3e0314b | 2343 | "s", unit); |
f84190d8 LP |
2344 | if (r < 0) |
2345 | return r; | |
45fb0699 | 2346 | |
f459b602 MAP |
2347 | r = sd_bus_message_read(reply, "o", &path); |
2348 | if (r < 0) | |
2349 | return r; | |
f84190d8 | 2350 | |
f459b602 | 2351 | r = sd_bus_get_property_trivial( |
f22f08cd | 2352 | bus, |
b0193f1c LP |
2353 | "org.freedesktop.systemd1", |
2354 | path, | |
f459b602 MAP |
2355 | "org.freedesktop.systemd1.Unit", |
2356 | "NeedDaemonReload", | |
2357 | NULL, | |
2358 | 'b', &b); | |
f84190d8 LP |
2359 | if (r < 0) |
2360 | return r; | |
45fb0699 | 2361 | |
45fb0699 LP |
2362 | return b; |
2363 | } | |
2364 | ||
3f36991e | 2365 | static void warn_unit_file_changed(const char *name) { |
87ec20ef LP |
2366 | assert(name); |
2367 | ||
3f36991e ZJS |
2368 | log_warning("%sWarning:%s %s changed on disk. Run 'systemctl%s daemon-reload' to reload units.", |
2369 | ansi_highlight_red(), | |
1fc464f6 | 2370 | ansi_normal(), |
3f36991e ZJS |
2371 | name, |
2372 | arg_scope == UNIT_FILE_SYSTEM ? "" : " --user"); | |
2373 | } | |
2374 | ||
33f6c497 ZJS |
2375 | static int unit_file_find_path(LookupPaths *lp, const char *unit_name, char **unit_path) { |
2376 | char **p; | |
2377 | ||
2378 | assert(lp); | |
2379 | assert(unit_name); | |
2380 | assert(unit_path); | |
2381 | ||
a3c4eb07 | 2382 | STRV_FOREACH(p, lp->search_path) { |
ad2a0358 | 2383 | _cleanup_free_ char *path; |
33f6c497 ZJS |
2384 | |
2385 | path = path_join(arg_root, *p, unit_name); | |
2386 | if (!path) | |
2387 | return log_oom(); | |
2388 | ||
2389 | if (access(path, F_OK) == 0) { | |
2390 | *unit_path = path; | |
ad2a0358 | 2391 | path = NULL; |
33f6c497 ZJS |
2392 | return 1; |
2393 | } | |
33f6c497 ZJS |
2394 | } |
2395 | ||
2396 | return 0; | |
2397 | } | |
2398 | ||
6e646d22 LP |
2399 | static int unit_find_paths( |
2400 | sd_bus *bus, | |
2401 | const char *unit_name, | |
6e646d22 LP |
2402 | LookupPaths *lp, |
2403 | char **fragment_path, | |
2404 | char ***dropin_paths) { | |
dab2bce8 IS |
2405 | |
2406 | _cleanup_free_ char *path = NULL; | |
2407 | _cleanup_strv_free_ char **dropins = NULL; | |
33f6c497 ZJS |
2408 | int r; |
2409 | ||
ad2a0358 ZJS |
2410 | /** |
2411 | * Finds where the unit is defined on disk. Returns 0 if the unit | |
2412 | * is not found. Returns 1 if it is found, and sets | |
2413 | * - the path to the unit in *path, if it exists on disk, | |
2414 | * - and a strv of existing drop-ins in *dropins, | |
2415 | * if the arg is not NULL and any dropins were found. | |
2416 | */ | |
2417 | ||
33f6c497 | 2418 | assert(unit_name); |
ad2a0358 | 2419 | assert(fragment_path); |
33f6c497 ZJS |
2420 | assert(lp); |
2421 | ||
4fbd7192 | 2422 | if (!install_client_side() && !unit_name_is_valid(unit_name, UNIT_NAME_TEMPLATE)) { |
4afd3348 | 2423 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
33f6c497 | 2424 | _cleanup_free_ char *unit = NULL; |
33f6c497 ZJS |
2425 | |
2426 | unit = unit_dbus_path_from_name(unit_name); | |
2427 | if (!unit) | |
2428 | return log_oom(); | |
2429 | ||
33f6c497 ZJS |
2430 | r = sd_bus_get_property_string( |
2431 | bus, | |
2432 | "org.freedesktop.systemd1", | |
2433 | unit, | |
2434 | "org.freedesktop.systemd1.Unit", | |
2435 | "FragmentPath", | |
2436 | &error, | |
ad2a0358 ZJS |
2437 | &path); |
2438 | if (r < 0) | |
2439 | return log_error_errno(r, "Failed to get FragmentPath: %s", bus_error_message(&error, r)); | |
2440 | ||
dab2bce8 IS |
2441 | if (dropin_paths) { |
2442 | r = sd_bus_get_property_strv( | |
2443 | bus, | |
2444 | "org.freedesktop.systemd1", | |
2445 | unit, | |
2446 | "org.freedesktop.systemd1.Unit", | |
2447 | "DropInPaths", | |
2448 | &error, | |
2449 | &dropins); | |
2450 | if (r < 0) | |
2451 | return log_error_errno(r, "Failed to get DropInPaths: %s", bus_error_message(&error, r)); | |
33f6c497 | 2452 | } |
ad2a0358 ZJS |
2453 | } else { |
2454 | _cleanup_set_free_ Set *names; | |
33f6c497 | 2455 | |
ad2a0358 ZJS |
2456 | names = set_new(NULL); |
2457 | if (!names) | |
7410616c | 2458 | return log_oom(); |
33f6c497 | 2459 | |
ad2a0358 ZJS |
2460 | r = set_put(names, unit_name); |
2461 | if (r < 0) | |
7410616c | 2462 | return log_error_errno(r, "Failed to add unit name: %m"); |
ad2a0358 | 2463 | |
dab2bce8 | 2464 | r = unit_file_find_path(lp, unit_name, &path); |
ad2a0358 ZJS |
2465 | if (r < 0) |
2466 | return r; | |
2467 | ||
2468 | if (r == 0) { | |
7410616c | 2469 | _cleanup_free_ char *template = NULL; |
ad2a0358 | 2470 | |
7410616c | 2471 | r = unit_name_template(unit_name, &template); |
eacd8534 | 2472 | if (r < 0 && r != -EINVAL) |
7410616c LP |
2473 | return log_error_errno(r, "Failed to determine template name: %m"); |
2474 | if (r >= 0) { | |
dab2bce8 | 2475 | r = unit_file_find_path(lp, template, &path); |
ad2a0358 ZJS |
2476 | if (r < 0) |
2477 | return r; | |
2478 | } | |
2479 | } | |
2480 | ||
dab2bce8 | 2481 | if (dropin_paths) { |
a3c4eb07 | 2482 | r = unit_file_find_dropin_paths(lp->search_path, NULL, names, &dropins); |
dab2bce8 IS |
2483 | if (r < 0) |
2484 | return r; | |
2485 | } | |
2486 | } | |
2487 | ||
2488 | r = 0; | |
2489 | ||
2490 | if (!isempty(path)) { | |
2491 | *fragment_path = path; | |
2492 | path = NULL; | |
2493 | r = 1; | |
2494 | } | |
2495 | ||
2496 | if (dropin_paths && !strv_isempty(dropins)) { | |
2497 | *dropin_paths = dropins; | |
2498 | dropins = NULL; | |
2499 | r = 1; | |
33f6c497 ZJS |
2500 | } |
2501 | ||
39c38ce1 | 2502 | if (r == 0 && !arg_force) |
b5e6a600 IS |
2503 | log_error("No files found for %s.", unit_name); |
2504 | ||
ad2a0358 | 2505 | return r; |
33f6c497 ZJS |
2506 | } |
2507 | ||
fa0d5878 | 2508 | static int get_state_one_unit(sd_bus *bus, const char *name, UnitActiveState *active_state) { |
b430f85f | 2509 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4afd3348 | 2510 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
b430f85f | 2511 | _cleanup_free_ char *buf = NULL; |
fa0d5878 BR |
2512 | UnitActiveState state; |
2513 | const char *path; | |
f22f08cd | 2514 | int r; |
701cdcb9 | 2515 | |
31be1221 | 2516 | assert(name); |
fa0d5878 | 2517 | assert(active_state); |
701cdcb9 | 2518 | |
b430f85f LP |
2519 | /* We don't use unit_dbus_path_from_name() directly since we don't want to load the unit unnecessarily, if it |
2520 | * isn't loaded. */ | |
f459b602 | 2521 | r = sd_bus_call_method( |
f22f08cd SP |
2522 | bus, |
2523 | "org.freedesktop.systemd1", | |
2524 | "/org/freedesktop/systemd1", | |
2525 | "org.freedesktop.systemd1.Manager", | |
2526 | "GetUnit", | |
b430f85f | 2527 | &error, |
f22f08cd | 2528 | &reply, |
6233b902 | 2529 | "s", name); |
60f9ba0b | 2530 | if (r < 0) { |
b430f85f LP |
2531 | if (!sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT)) |
2532 | return log_error_errno(r, "Failed to retrieve unit: %s", bus_error_message(&error, r)); | |
e61a3135 | 2533 | |
b430f85f LP |
2534 | /* The unit is currently not loaded, hence say it's "inactive", since all units that aren't loaded are |
2535 | * considered inactive. */ | |
fa0d5878 | 2536 | state = UNIT_INACTIVE; |
60f9ba0b | 2537 | |
b430f85f LP |
2538 | } else { |
2539 | r = sd_bus_message_read(reply, "o", &path); | |
2540 | if (r < 0) | |
2541 | return bus_log_parse_error(r); | |
2542 | ||
2543 | r = sd_bus_get_property_string( | |
2544 | bus, | |
2545 | "org.freedesktop.systemd1", | |
2546 | path, | |
2547 | "org.freedesktop.systemd1.Unit", | |
2548 | "ActiveState", | |
2549 | &error, | |
2550 | &buf); | |
2551 | if (r < 0) | |
2552 | return log_error_errno(r, "Failed to retrieve unit state: %s", bus_error_message(&error, r)); | |
2553 | ||
fa0d5878 BR |
2554 | state = unit_active_state_from_string(buf); |
2555 | if (state == _UNIT_ACTIVE_STATE_INVALID) { | |
2556 | log_error("Invalid unit state '%s' for: %s", buf, name); | |
2557 | return -EINVAL; | |
2558 | } | |
60f9ba0b | 2559 | } |
701cdcb9 | 2560 | |
fa0d5878 BR |
2561 | *active_state = state; |
2562 | return 0; | |
701cdcb9 MS |
2563 | } |
2564 | ||
f459b602 MAP |
2565 | static int check_triggering_units( |
2566 | sd_bus *bus, | |
2567 | const char *name) { | |
2568 | ||
4afd3348 | 2569 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
fa0d5878 | 2570 | _cleanup_free_ char *path = NULL, *n = NULL, *load_state = NULL; |
f459b602 | 2571 | _cleanup_strv_free_ char **triggered_by = NULL; |
e61a3135 | 2572 | bool print_warning_label = true; |
fa0d5878 | 2573 | UnitActiveState active_state; |
f459b602 | 2574 | char **i; |
f22f08cd | 2575 | int r; |
701cdcb9 | 2576 | |
7410616c LP |
2577 | r = unit_name_mangle(name, UNIT_NAME_NOGLOB, &n); |
2578 | if (r < 0) | |
2579 | return log_error_errno(r, "Failed to mangle unit name: %m"); | |
d3b52baf | 2580 | |
f459b602 MAP |
2581 | path = unit_dbus_path_from_name(n); |
2582 | if (!path) | |
2583 | return log_oom(); | |
701cdcb9 | 2584 | |
f459b602 | 2585 | r = sd_bus_get_property_string( |
d0a5cdb2 JJ |
2586 | bus, |
2587 | "org.freedesktop.systemd1", | |
f459b602 MAP |
2588 | path, |
2589 | "org.freedesktop.systemd1.Unit", | |
2590 | "LoadState", | |
2591 | &error, | |
fa0d5878 | 2592 | &load_state); |
691395d8 LP |
2593 | if (r < 0) |
2594 | return log_error_errno(r, "Failed to get load state of %s: %s", n, bus_error_message(&error, r)); | |
d0a5cdb2 | 2595 | |
fa0d5878 | 2596 | if (streq(load_state, "masked")) |
f459b602 | 2597 | return 0; |
701cdcb9 | 2598 | |
f459b602 MAP |
2599 | r = sd_bus_get_property_strv( |
2600 | bus, | |
2601 | "org.freedesktop.systemd1", | |
2602 | path, | |
2603 | "org.freedesktop.systemd1.Unit", | |
2604 | "TriggeredBy", | |
2605 | &error, | |
2606 | &triggered_by); | |
691395d8 LP |
2607 | if (r < 0) |
2608 | return log_error_errno(r, "Failed to get triggered by array of %s: %s", n, bus_error_message(&error, r)); | |
701cdcb9 | 2609 | |
f459b602 | 2610 | STRV_FOREACH(i, triggered_by) { |
fa0d5878 | 2611 | r = get_state_one_unit(bus, *i, &active_state); |
f647962d | 2612 | if (r < 0) |
fa0d5878 | 2613 | return r; |
701cdcb9 | 2614 | |
fa0d5878 | 2615 | if (!IN_SET(active_state, UNIT_ACTIVE, UNIT_RELOADING)) |
f459b602 | 2616 | continue; |
60f9ba0b | 2617 | |
f459b602 MAP |
2618 | if (print_warning_label) { |
2619 | log_warning("Warning: Stopping %s, but it can still be activated by:", n); | |
2620 | print_warning_label = false; | |
701cdcb9 | 2621 | } |
1c291cf3 | 2622 | |
f459b602 | 2623 | log_warning(" %s", *i); |
701cdcb9 | 2624 | } |
f459b602 MAP |
2625 | |
2626 | return 0; | |
701cdcb9 MS |
2627 | } |
2628 | ||
2fc9a280 LP |
2629 | static const struct { |
2630 | const char *verb; | |
2631 | const char *method; | |
2632 | } unit_actions[] = { | |
2633 | { "start", "StartUnit" }, | |
2634 | { "stop", "StopUnit" }, | |
2635 | { "condstop", "StopUnit" }, | |
2636 | { "reload", "ReloadUnit" }, | |
2637 | { "restart", "RestartUnit" }, | |
2638 | { "try-restart", "TryRestartUnit" }, | |
2639 | { "condrestart", "TryRestartUnit" }, | |
2640 | { "reload-or-restart", "ReloadOrRestartUnit" }, | |
aabf5d42 | 2641 | { "try-reload-or-restart", "ReloadOrTryRestartUnit" }, |
2fc9a280 LP |
2642 | { "reload-or-try-restart", "ReloadOrTryRestartUnit" }, |
2643 | { "condreload", "ReloadOrTryRestartUnit" }, | |
2644 | { "force-reload", "ReloadOrTryRestartUnit" } | |
2645 | }; | |
2646 | ||
39602c39 TA |
2647 | static const char *verb_to_method(const char *verb) { |
2648 | uint i; | |
2649 | ||
2650 | for (i = 0; i < ELEMENTSOF(unit_actions); i++) | |
2651 | if (streq_ptr(unit_actions[i].verb, verb)) | |
2652 | return unit_actions[i].method; | |
2653 | ||
2654 | return "StartUnit"; | |
2655 | } | |
2656 | ||
2657 | static const char *method_to_verb(const char *method) { | |
2658 | uint i; | |
2659 | ||
2660 | for (i = 0; i < ELEMENTSOF(unit_actions); i++) | |
2661 | if (streq_ptr(unit_actions[i].method, method)) | |
2662 | return unit_actions[i].verb; | |
2663 | ||
2664 | return "n/a"; | |
2665 | } | |
2666 | ||
e4b61340 | 2667 | static int start_unit_one( |
f459b602 | 2668 | sd_bus *bus, |
e4b61340 LP |
2669 | const char *method, |
2670 | const char *name, | |
2671 | const char *mode, | |
f459b602 | 2672 | sd_bus_error *error, |
ebd011d9 | 2673 | BusWaitForJobs *w) { |
7e4249b9 | 2674 | |
4afd3348 | 2675 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
45fb0699 | 2676 | const char *path; |
7e4249b9 | 2677 | int r; |
7e4249b9 | 2678 | |
e4b61340 LP |
2679 | assert(method); |
2680 | assert(name); | |
2681 | assert(mode); | |
22f4096c | 2682 | assert(error); |
7e4249b9 | 2683 | |
e3e0314b | 2684 | log_debug("Calling manager for %s on %s, %s", method, name, mode); |
342641fb | 2685 | |
6e646d22 | 2686 | r = sd_bus_call_method( |
f22f08cd | 2687 | bus, |
b0193f1c LP |
2688 | "org.freedesktop.systemd1", |
2689 | "/org/freedesktop/systemd1", | |
2690 | "org.freedesktop.systemd1.Manager", | |
6e646d22 LP |
2691 | method, |
2692 | error, | |
2693 | &reply, | |
2694 | "ss", name, mode); | |
f459b602 | 2695 | if (r < 0) { |
39602c39 TA |
2696 | const char *verb; |
2697 | ||
033f0ab8 LP |
2698 | /* There's always a fallback possible for legacy actions. */ |
2699 | if (arg_action != ACTION_SYSTEMCTL) | |
2700 | return r; | |
67f3c402 | 2701 | |
39602c39 TA |
2702 | verb = method_to_verb(method); |
2703 | ||
ee87525c FB |
2704 | log_error("Failed to %s %s: %s", verb, name, bus_error_message(error, r)); |
2705 | ||
2706 | if (!sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT) && | |
2707 | !sd_bus_error_has_name(error, BUS_ERROR_UNIT_MASKED)) | |
0b8505b7 RC |
2708 | log_error("See %s logs and 'systemctl%s status %s' for details.", |
2709 | arg_scope == UNIT_FILE_SYSTEM ? "system" : "user", | |
2710 | arg_scope == UNIT_FILE_SYSTEM ? "" : " --user", | |
2711 | name); | |
ee87525c FB |
2712 | |
2713 | return r; | |
7e4249b9 LP |
2714 | } |
2715 | ||
f459b602 MAP |
2716 | r = sd_bus_message_read(reply, "o", &path); |
2717 | if (r < 0) | |
2718 | return bus_log_parse_error(r); | |
45fb0699 | 2719 | |
e3e0314b | 2720 | if (need_daemon_reload(bus, name) > 0) |
3f36991e | 2721 | warn_unit_file_changed(name); |
45fb0699 | 2722 | |
ebd011d9 LP |
2723 | if (w) { |
2724 | log_debug("Adding %s to the set", path); | |
2725 | r = bus_wait_for_jobs_add(w, path); | |
cbc9fbd1 LP |
2726 | if (r < 0) |
2727 | return log_oom(); | |
e4b61340 | 2728 | } |
7e4249b9 | 2729 | |
46eddbb5 | 2730 | return 0; |
7e4249b9 LP |
2731 | } |
2732 | ||
e3e0314b | 2733 | static int expand_names(sd_bus *bus, char **names, const char* suffix, char ***ret) { |
e3e0314b ZJS |
2734 | _cleanup_strv_free_ char **mangled = NULL, **globs = NULL; |
2735 | char **name; | |
7410616c | 2736 | int r, i; |
e3e0314b | 2737 | |
4fbd7192 LP |
2738 | assert(bus); |
2739 | assert(ret); | |
2740 | ||
e3e0314b ZJS |
2741 | STRV_FOREACH(name, names) { |
2742 | char *t; | |
2743 | ||
e80733be | 2744 | if (suffix) |
7410616c | 2745 | r = unit_name_mangle_with_suffix(*name, UNIT_NAME_GLOB, suffix, &t); |
e80733be | 2746 | else |
7410616c LP |
2747 | r = unit_name_mangle(*name, UNIT_NAME_GLOB, &t); |
2748 | if (r < 0) | |
2749 | return log_error_errno(r, "Failed to mangle name: %m"); | |
e3e0314b ZJS |
2750 | |
2751 | if (string_is_glob(t)) | |
6e18964d | 2752 | r = strv_consume(&globs, t); |
e3e0314b | 2753 | else |
6e18964d ZJS |
2754 | r = strv_consume(&mangled, t); |
2755 | if (r < 0) | |
e3e0314b | 2756 | return log_oom(); |
e3e0314b ZJS |
2757 | } |
2758 | ||
2759 | /* Query the manager only if any of the names are a glob, since | |
2760 | * this is fairly expensive */ | |
2761 | if (!strv_isempty(globs)) { | |
4afd3348 | 2762 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
e3e0314b | 2763 | _cleanup_free_ UnitInfo *unit_infos = NULL; |
1b53f64b | 2764 | size_t allocated, n; |
e3e0314b | 2765 | |
1238ee09 | 2766 | r = get_unit_list(bus, NULL, globs, &unit_infos, 0, &reply); |
e3e0314b ZJS |
2767 | if (r < 0) |
2768 | return r; | |
2769 | ||
1b53f64b LP |
2770 | n = strv_length(mangled); |
2771 | allocated = n + 1; | |
2772 | ||
2773 | for (i = 0; i < r; i++) { | |
2774 | if (!GREEDY_REALLOC(mangled, allocated, n+2)) | |
2775 | return log_oom(); | |
2776 | ||
2777 | mangled[n] = strdup(unit_infos[i].id); | |
2778 | if (!mangled[n]) | |
e3e0314b | 2779 | return log_oom(); |
1b53f64b LP |
2780 | |
2781 | mangled[++n] = NULL; | |
2782 | } | |
e3e0314b ZJS |
2783 | } |
2784 | ||
2785 | *ret = mangled; | |
2786 | mangled = NULL; /* do not free */ | |
1238ee09 | 2787 | |
e3e0314b ZJS |
2788 | return 0; |
2789 | } | |
2790 | ||
47a0eaa6 MS |
2791 | static const struct { |
2792 | const char *target; | |
2793 | const char *verb; | |
2794 | const char *mode; | |
2795 | } action_table[_ACTION_MAX] = { | |
2796 | [ACTION_HALT] = { SPECIAL_HALT_TARGET, "halt", "replace-irreversibly" }, | |
2797 | [ACTION_POWEROFF] = { SPECIAL_POWEROFF_TARGET, "poweroff", "replace-irreversibly" }, | |
2798 | [ACTION_REBOOT] = { SPECIAL_REBOOT_TARGET, "reboot", "replace-irreversibly" }, | |
2799 | [ACTION_KEXEC] = { SPECIAL_KEXEC_TARGET, "kexec", "replace-irreversibly" }, | |
d5d8429a LP |
2800 | [ACTION_RUNLEVEL2] = { SPECIAL_MULTI_USER_TARGET, NULL, "isolate" }, |
2801 | [ACTION_RUNLEVEL3] = { SPECIAL_MULTI_USER_TARGET, NULL, "isolate" }, | |
2802 | [ACTION_RUNLEVEL4] = { SPECIAL_MULTI_USER_TARGET, NULL, "isolate" }, | |
2803 | [ACTION_RUNLEVEL5] = { SPECIAL_GRAPHICAL_TARGET, NULL, "isolate" }, | |
47a0eaa6 MS |
2804 | [ACTION_RESCUE] = { SPECIAL_RESCUE_TARGET, "rescue", "isolate" }, |
2805 | [ACTION_EMERGENCY] = { SPECIAL_EMERGENCY_TARGET, "emergency", "isolate" }, | |
2806 | [ACTION_DEFAULT] = { SPECIAL_DEFAULT_TARGET, "default", "isolate" }, | |
2807 | [ACTION_EXIT] = { SPECIAL_EXIT_TARGET, "exit", "replace-irreversibly" }, | |
2808 | [ACTION_SUSPEND] = { SPECIAL_SUSPEND_TARGET, "suspend", "replace-irreversibly" }, | |
2809 | [ACTION_HIBERNATE] = { SPECIAL_HIBERNATE_TARGET, "hibernate", "replace-irreversibly" }, | |
2810 | [ACTION_HYBRID_SLEEP] = { SPECIAL_HYBRID_SLEEP_TARGET, "hybrid-sleep", "replace-irreversibly" }, | |
2811 | }; | |
2812 | ||
514f4ef5 | 2813 | static enum action verb_to_action(const char *verb) { |
47a0eaa6 MS |
2814 | enum action i; |
2815 | ||
f459b602 MAP |
2816 | for (i = _ACTION_INVALID; i < _ACTION_MAX; i++) |
2817 | if (streq_ptr(action_table[i].verb, verb)) | |
47a0eaa6 | 2818 | return i; |
514f4ef5 | 2819 | |
f459b602 MAP |
2820 | return _ACTION_INVALID; |
2821 | } | |
e4b61340 | 2822 | |
e449de87 | 2823 | static int start_unit(int argc, char *argv[], void *userdata) { |
ebd011d9 | 2824 | _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL; |
08073121 | 2825 | const char *method, *mode, *one_name, *suffix = NULL; |
ebd011d9 | 2826 | _cleanup_strv_free_ char **names = NULL; |
4fbd7192 | 2827 | sd_bus *bus; |
729e3769 | 2828 | char **name; |
b6520546 | 2829 | int r = 0; |
e4b61340 | 2830 | |
4fbd7192 LP |
2831 | r = acquire_bus(BUS_MANAGER, &bus); |
2832 | if (r < 0) | |
2833 | return r; | |
2834 | ||
d2ad7e1b ZJS |
2835 | ask_password_agent_open_if_enabled(); |
2836 | polkit_agent_open_if_enabled(); | |
2837 | ||
e4b61340 | 2838 | if (arg_action == ACTION_SYSTEMCTL) { |
47a0eaa6 | 2839 | enum action action; |
e4b61340 | 2840 | |
e449de87 LP |
2841 | method = verb_to_method(argv[0]); |
2842 | action = verb_to_action(argv[0]); | |
2843 | ||
2844 | if (streq(argv[0], "isolate")) { | |
08073121 LP |
2845 | mode = "isolate"; |
2846 | suffix = ".target"; | |
2847 | } else | |
2848 | mode = action_table[action].mode ?: arg_job_mode; | |
e4b61340 | 2849 | |
e3e0314b | 2850 | one_name = action_table[action].target; |
e4b61340 | 2851 | } else { |
47a0eaa6 MS |
2852 | assert(arg_action < ELEMENTSOF(action_table)); |
2853 | assert(action_table[arg_action].target); | |
e4b61340 LP |
2854 | |
2855 | method = "StartUnit"; | |
514f4ef5 | 2856 | |
47a0eaa6 | 2857 | mode = action_table[arg_action].mode; |
e3e0314b | 2858 | one_name = action_table[arg_action].target; |
514f4ef5 LP |
2859 | } |
2860 | ||
e3e0314b ZJS |
2861 | if (one_name) |
2862 | names = strv_new(one_name, NULL); | |
2863 | else { | |
e449de87 | 2864 | r = expand_names(bus, strv_skip(argv, 1), suffix, &names); |
e3e0314b | 2865 | if (r < 0) |
691395d8 | 2866 | return log_error_errno(r, "Failed to expand names: %m"); |
e3e0314b | 2867 | } |
b6520546 | 2868 | |
6e905d93 | 2869 | if (!arg_no_block) { |
ebd011d9 | 2870 | r = bus_wait_for_jobs_new(bus, &w); |
f647962d MS |
2871 | if (r < 0) |
2872 | return log_error_errno(r, "Could not watch jobs: %m"); | |
e4b61340 LP |
2873 | } |
2874 | ||
b6520546 | 2875 | STRV_FOREACH(name, names) { |
4afd3348 | 2876 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
b6520546 | 2877 | int q; |
f459b602 | 2878 | |
ebd011d9 | 2879 | q = start_unit_one(bus, method, *name, mode, &error, w); |
e3e0314b | 2880 | if (r >= 0 && q < 0) |
b6520546 | 2881 | r = translate_bus_error_to_exit_status(q, &error); |
e4b61340 LP |
2882 | } |
2883 | ||
67f3c402 | 2884 | if (!arg_no_block) { |
c11bda1e ZJS |
2885 | int q, arg_count = 0; |
2886 | const char* extra_args[4] = {}; | |
4524439e | 2887 | |
4524439e ZJS |
2888 | if (arg_scope != UNIT_FILE_SYSTEM) |
2889 | extra_args[arg_count++] = "--user"; | |
2890 | ||
2891 | assert(IN_SET(arg_transport, BUS_TRANSPORT_LOCAL, BUS_TRANSPORT_REMOTE, BUS_TRANSPORT_MACHINE)); | |
2892 | if (arg_transport == BUS_TRANSPORT_REMOTE) { | |
2893 | extra_args[arg_count++] = "-H"; | |
2894 | extra_args[arg_count++] = arg_host; | |
2895 | } else if (arg_transport == BUS_TRANSPORT_MACHINE) { | |
2896 | extra_args[arg_count++] = "-M"; | |
2897 | extra_args[arg_count++] = arg_host; | |
2898 | } | |
f459b602 | 2899 | |
4524439e | 2900 | q = bus_wait_for_jobs(w, arg_quiet, extra_args); |
f459b602 MAP |
2901 | if (q < 0) |
2902 | return q; | |
49111a70 ZJS |
2903 | |
2904 | /* When stopping units, warn if they can still be triggered by | |
2905 | * another active unit (socket, path, timer) */ | |
b6520546 ZJS |
2906 | if (!arg_quiet && streq(method, "StopUnit")) |
2907 | STRV_FOREACH(name, names) | |
2908 | check_triggering_units(bus, *name); | |
67f3c402 | 2909 | } |
514f4ef5 | 2910 | |
f459b602 | 2911 | return r; |
e4b61340 LP |
2912 | } |
2913 | ||
4fbd7192 | 2914 | static int logind_set_wall_message(void) { |
f2d11d35 | 2915 | #ifdef HAVE_LOGIND |
4afd3348 | 2916 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4fbd7192 | 2917 | sd_bus *bus; |
f2d11d35 LP |
2918 | _cleanup_free_ char *m = NULL; |
2919 | int r; | |
2920 | ||
4fbd7192 LP |
2921 | r = acquire_bus(BUS_FULL, &bus); |
2922 | if (r < 0) | |
2923 | return r; | |
f2d11d35 LP |
2924 | |
2925 | m = strv_join(arg_wall, " "); | |
2926 | if (!m) | |
2927 | return log_oom(); | |
2928 | ||
2929 | r = sd_bus_call_method( | |
2930 | bus, | |
2931 | "org.freedesktop.login1", | |
2932 | "/org/freedesktop/login1", | |
2933 | "org.freedesktop.login1.Manager", | |
2934 | "SetWallMessage", | |
2935 | &error, | |
2936 | NULL, | |
2937 | "sb", | |
2938 | m, | |
2939 | !arg_no_wall); | |
2940 | ||
2941 | if (r < 0) | |
2942 | return log_warning_errno(r, "Failed to set wall message, ignoring: %s", bus_error_message(&error, r)); | |
2943 | ||
2944 | #endif | |
2945 | return 0; | |
2946 | } | |
2947 | ||
7e59bfcb LP |
2948 | /* Ask systemd-logind, which might grant access to unprivileged users |
2949 | * through PolicyKit */ | |
4fbd7192 | 2950 | static int logind_reboot(enum action a) { |
4c80c73c | 2951 | #ifdef HAVE_LOGIND |
4afd3348 | 2952 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
58158dc7 | 2953 | const char *method, *description; |
4fbd7192 | 2954 | sd_bus *bus; |
f459b602 | 2955 | int r; |
4c80c73c | 2956 | |
4fbd7192 LP |
2957 | r = acquire_bus(BUS_FULL, &bus); |
2958 | if (r < 0) | |
2959 | return r; | |
f2d11d35 | 2960 | |
4c80c73c KS |
2961 | switch (a) { |
2962 | ||
2963 | case ACTION_REBOOT: | |
2964 | method = "Reboot"; | |
58158dc7 | 2965 | description = "reboot system"; |
4c80c73c KS |
2966 | break; |
2967 | ||
2968 | case ACTION_POWEROFF: | |
2969 | method = "PowerOff"; | |
58158dc7 | 2970 | description = "power off system"; |
4c80c73c KS |
2971 | break; |
2972 | ||
d889a206 LP |
2973 | case ACTION_SUSPEND: |
2974 | method = "Suspend"; | |
58158dc7 | 2975 | description = "suspend system"; |
d889a206 LP |
2976 | break; |
2977 | ||
2978 | case ACTION_HIBERNATE: | |
2979 | method = "Hibernate"; | |
58158dc7 | 2980 | description = "hibernate system"; |
d889a206 LP |
2981 | break; |
2982 | ||
6524990f LP |
2983 | case ACTION_HYBRID_SLEEP: |
2984 | method = "HybridSleep"; | |
58158dc7 | 2985 | description = "put system into hybrid sleep"; |
6524990f LP |
2986 | break; |
2987 | ||
4c80c73c KS |
2988 | default: |
2989 | return -EINVAL; | |
2990 | } | |
2991 | ||
d2ad7e1b ZJS |
2992 | polkit_agent_open_if_enabled(); |
2993 | (void) logind_set_wall_message(); | |
2994 | ||
f459b602 | 2995 | r = sd_bus_call_method( |
f22f08cd SP |
2996 | bus, |
2997 | "org.freedesktop.login1", | |
2998 | "/org/freedesktop/login1", | |
2999 | "org.freedesktop.login1.Manager", | |
3000 | method, | |
f459b602 | 3001 | &error, |
f22f08cd | 3002 | NULL, |
342641fb | 3003 | "b", arg_ask_password); |
f459b602 | 3004 | if (r < 0) |
691395d8 | 3005 | return log_error_errno(r, "Failed to %s via logind: %s", description, bus_error_message(&error, r)); |
f459b602 | 3006 | |
691395d8 | 3007 | return 0; |
4c80c73c KS |
3008 | #else |
3009 | return -ENOSYS; | |
3010 | #endif | |
3011 | } | |
3012 | ||
4fbd7192 | 3013 | static int logind_check_inhibitors(enum action a) { |
b37844d3 | 3014 | #ifdef HAVE_LOGIND |
4afd3348 | 3015 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
59164be4 | 3016 | _cleanup_strv_free_ char **sessions = NULL; |
f459b602 MAP |
3017 | const char *what, *who, *why, *mode; |
3018 | uint32_t uid, pid; | |
4fbd7192 | 3019 | sd_bus *bus; |
f459b602 | 3020 | unsigned c = 0; |
59164be4 | 3021 | char **s; |
f459b602 | 3022 | int r; |
b37844d3 | 3023 | |
748ebafa LP |
3024 | if (arg_ignore_inhibitors || arg_force > 0) |
3025 | return 0; | |
3026 | ||
3027 | if (arg_when > 0) | |
3028 | return 0; | |
3029 | ||
3030 | if (geteuid() == 0) | |
b37844d3 LP |
3031 | return 0; |
3032 | ||
3033 | if (!on_tty()) | |
3034 | return 0; | |
3035 | ||
4fbd7192 LP |
3036 | r = acquire_bus(BUS_FULL, &bus); |
3037 | if (r < 0) | |
3038 | return r; | |
3039 | ||
f459b602 | 3040 | r = sd_bus_call_method( |
b37844d3 LP |
3041 | bus, |
3042 | "org.freedesktop.login1", | |
3043 | "/org/freedesktop/login1", | |
3044 | "org.freedesktop.login1.Manager", | |
3045 | "ListInhibitors", | |
b37844d3 | 3046 | NULL, |
f459b602 MAP |
3047 | &reply, |
3048 | NULL); | |
b37844d3 LP |
3049 | if (r < 0) |
3050 | /* If logind is not around, then there are no inhibitors... */ | |
3051 | return 0; | |
3052 | ||
4aa2beac | 3053 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssuu)"); |
f459b602 MAP |
3054 | if (r < 0) |
3055 | return bus_log_parse_error(r); | |
b37844d3 | 3056 | |
4aa2beac | 3057 | while ((r = sd_bus_message_read(reply, "(ssssuu)", &what, &who, &why, &mode, &uid, &pid)) > 0) { |
59164be4 | 3058 | _cleanup_free_ char *comm = NULL, *user = NULL; |
f459b602 | 3059 | _cleanup_strv_free_ char **sv = NULL; |
b37844d3 LP |
3060 | |
3061 | if (!streq(mode, "block")) | |
f459b602 | 3062 | continue; |
b37844d3 LP |
3063 | |
3064 | sv = strv_split(what, ":"); | |
3065 | if (!sv) | |
3066 | return log_oom(); | |
3067 | ||
d028e018 ZJS |
3068 | if ((pid_t) pid < 0) |
3069 | return log_error_errno(ERANGE, "Bad PID %"PRIu32": %m", pid); | |
3070 | ||
b37844d3 | 3071 | if (!strv_contains(sv, |
4c315c2c IS |
3072 | IN_SET(a, |
3073 | ACTION_HALT, | |
3074 | ACTION_POWEROFF, | |
3075 | ACTION_REBOOT, | |
3076 | ACTION_KEXEC) ? "shutdown" : "sleep")) | |
f459b602 | 3077 | continue; |
b37844d3 LP |
3078 | |
3079 | get_process_comm(pid, &comm); | |
59164be4 | 3080 | user = uid_to_name(uid); |
f459b602 | 3081 | |
de0671ee | 3082 | log_warning("Operation inhibited by \"%s\" (PID "PID_FMT" \"%s\", user %s), reason is \"%s\".", |
d028e018 | 3083 | who, (pid_t) pid, strna(comm), strna(user), why); |
b37844d3 | 3084 | |
f459b602 | 3085 | c++; |
b37844d3 | 3086 | } |
f459b602 MAP |
3087 | if (r < 0) |
3088 | return bus_log_parse_error(r); | |
b37844d3 | 3089 | |
f459b602 MAP |
3090 | r = sd_bus_message_exit_container(reply); |
3091 | if (r < 0) | |
3092 | return bus_log_parse_error(r); | |
b37844d3 | 3093 | |
59164be4 LP |
3094 | /* Check for current sessions */ |
3095 | sd_get_sessions(&sessions); | |
3096 | STRV_FOREACH(s, sessions) { | |
59164be4 LP |
3097 | _cleanup_free_ char *type = NULL, *tty = NULL, *seat = NULL, *user = NULL, *service = NULL, *class = NULL; |
3098 | ||
3099 | if (sd_session_get_uid(*s, &uid) < 0 || uid == getuid()) | |
3100 | continue; | |
3101 | ||
3102 | if (sd_session_get_class(*s, &class) < 0 || !streq(class, "user")) | |
3103 | continue; | |
3104 | ||
3105 | if (sd_session_get_type(*s, &type) < 0 || (!streq(type, "x11") && !streq(type, "tty"))) | |
3106 | continue; | |
3107 | ||
3108 | sd_session_get_tty(*s, &tty); | |
3109 | sd_session_get_seat(*s, &seat); | |
3110 | sd_session_get_service(*s, &service); | |
3111 | user = uid_to_name(uid); | |
3112 | ||
3113 | log_warning("User %s is logged in on %s.", strna(user), isempty(tty) ? (isempty(seat) ? strna(service) : seat) : tty); | |
3114 | c++; | |
3115 | } | |
3116 | ||
b37844d3 LP |
3117 | if (c <= 0) |
3118 | return 0; | |
3119 | ||
59164be4 | 3120 | log_error("Please retry operation after closing inhibitors and logging out other users.\nAlternatively, ignore inhibitors and users with 'systemctl %s -i'.", |
47a0eaa6 | 3121 | action_table[a].verb); |
b37844d3 LP |
3122 | |
3123 | return -EPERM; | |
3124 | #else | |
3125 | return 0; | |
3126 | #endif | |
3127 | } | |
3128 | ||
4fbd7192 | 3129 | static int logind_prepare_firmware_setup(void) { |
a4921cf4 | 3130 | #ifdef HAVE_LOGIND |
4afd3348 | 3131 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4fbd7192 | 3132 | sd_bus *bus; |
a4921cf4 JJ |
3133 | int r; |
3134 | ||
4fbd7192 LP |
3135 | r = acquire_bus(BUS_FULL, &bus); |
3136 | if (r < 0) | |
3137 | return r; | |
a4921cf4 | 3138 | |
a4921cf4 JJ |
3139 | r = sd_bus_call_method( |
3140 | bus, | |
3141 | "org.freedesktop.login1", | |
3142 | "/org/freedesktop/login1", | |
3143 | "org.freedesktop.login1.Manager", | |
3144 | "SetRebootToFirmwareSetup", | |
3145 | &error, | |
3146 | NULL, | |
3147 | "b", true); | |
4fbd7192 LP |
3148 | if (r < 0) |
3149 | return log_error_errno(r, "Cannot indicate to EFI to boot into setup mode: %s", bus_error_message(&error, r)); | |
a4921cf4 JJ |
3150 | |
3151 | return 0; | |
3152 | #else | |
3153 | log_error("Cannot remotely indicate to EFI to boot into setup mode."); | |
4fbd7192 | 3154 | return -ENOSYS; |
a4921cf4 JJ |
3155 | #endif |
3156 | } | |
3157 | ||
4fbd7192 LP |
3158 | static int prepare_firmware_setup(void) { |
3159 | int r; | |
3160 | ||
3161 | if (!arg_firmware_setup) | |
3162 | return 0; | |
3163 | ||
3164 | if (arg_transport == BUS_TRANSPORT_LOCAL) { | |
3165 | ||
3166 | r = efi_set_reboot_to_firmware(true); | |
3167 | if (r < 0) | |
3168 | log_debug_errno(r, "Cannot indicate to EFI to boot into setup mode, will retry via logind: %m"); | |
3169 | else | |
3170 | return r; | |
3171 | } | |
3172 | ||
3173 | return logind_prepare_firmware_setup(); | |
3174 | } | |
3175 | ||
57ab9006 | 3176 | static int set_exit_code(uint8_t code) { |
4afd3348 | 3177 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
57ab9006 LP |
3178 | sd_bus *bus; |
3179 | int r; | |
3180 | ||
3181 | r = acquire_bus(BUS_MANAGER, &bus); | |
3182 | if (r < 0) | |
3183 | return r; | |
3184 | ||
3185 | r = sd_bus_call_method( | |
3186 | bus, | |
3187 | "org.freedesktop.systemd1", | |
3188 | "/org/freedesktop/systemd1", | |
3189 | "org.freedesktop.systemd1.Manager", | |
3190 | "SetExitCode", | |
3191 | &error, | |
3192 | NULL, | |
3193 | "y", code); | |
3194 | if (r < 0) | |
af3d8113 | 3195 | return log_error_errno(r, "Failed to set exit code: %s", bus_error_message(&error, r)); |
57ab9006 LP |
3196 | |
3197 | return 0; | |
3198 | } | |
3199 | ||
e449de87 | 3200 | static int start_special(int argc, char *argv[], void *userdata) { |
4c80c73c | 3201 | enum action a; |
983d9c90 LP |
3202 | int r; |
3203 | ||
e449de87 | 3204 | assert(argv); |
514f4ef5 | 3205 | |
e449de87 | 3206 | a = verb_to_action(argv[0]); |
4c80c73c | 3207 | |
4fbd7192 | 3208 | r = logind_check_inhibitors(a); |
748ebafa LP |
3209 | if (r < 0) |
3210 | return r; | |
3211 | ||
c32b90de LP |
3212 | if (arg_force >= 2 && geteuid() != 0) { |
3213 | log_error("Must be root."); | |
3214 | return -EPERM; | |
3215 | } | |
3216 | ||
4fbd7192 | 3217 | r = prepare_firmware_setup(); |
a4921cf4 JJ |
3218 | if (r < 0) |
3219 | return r; | |
5bdf2243 | 3220 | |
e449de87 | 3221 | if (a == ACTION_REBOOT && argc > 1) { |
27c06cb5 | 3222 | r = update_reboot_parameter_and_warn(argv[1]); |
b986229e SW |
3223 | if (r < 0) |
3224 | return r; | |
57ab9006 | 3225 | |
e449de87 | 3226 | } else if (a == ACTION_EXIT && argc > 1) { |
7bbb5359 | 3227 | uint8_t code; |
287419c1 | 3228 | |
57ab9006 LP |
3229 | /* If the exit code is not given on the command line, |
3230 | * don't reset it to zero: just keep it as it might | |
3231 | * have been set previously. */ | |
287419c1 | 3232 | |
e449de87 | 3233 | r = safe_atou8(argv[1], &code); |
4fbd7192 | 3234 | if (r < 0) |
57ab9006 | 3235 | return log_error_errno(r, "Invalid exit code."); |
4fbd7192 | 3236 | |
57ab9006 | 3237 | r = set_exit_code(code); |
691395d8 | 3238 | if (r < 0) |
57ab9006 | 3239 | return r; |
b986229e SW |
3240 | } |
3241 | ||
7e59bfcb | 3242 | if (arg_force >= 2 && |
4c315c2c IS |
3243 | IN_SET(a, |
3244 | ACTION_HALT, | |
3245 | ACTION_POWEROFF, | |
3246 | ACTION_REBOOT)) | |
477def80 | 3247 | return halt_now(a); |
e606bb61 | 3248 | |
7e59bfcb | 3249 | if (arg_force >= 1 && |
4c315c2c IS |
3250 | IN_SET(a, |
3251 | ACTION_HALT, | |
3252 | ACTION_POWEROFF, | |
3253 | ACTION_REBOOT, | |
3254 | ACTION_KEXEC, | |
3255 | ACTION_EXIT)) | |
2853b60a | 3256 | return trivial_method(argc, argv, userdata); |
20b09ca7 | 3257 | |
7089051f LP |
3258 | /* First try logind, to allow authentication with polkit */ |
3259 | if (IN_SET(a, | |
4c315c2c IS |
3260 | ACTION_POWEROFF, |
3261 | ACTION_REBOOT, | |
3262 | ACTION_SUSPEND, | |
3263 | ACTION_HIBERNATE, | |
3264 | ACTION_HYBRID_SLEEP)) { | |
4fbd7192 | 3265 | r = logind_reboot(a); |
a9085ea3 | 3266 | if (r >= 0) |
7e59bfcb | 3267 | return r; |
a9085ea3 IS |
3268 | if (IN_SET(r, -EOPNOTSUPP, -EINPROGRESS)) |
3269 | /* requested operation is not supported or already in progress */ | |
3270 | return r; | |
7089051f LP |
3271 | |
3272 | /* On all other errors, try low-level operation */ | |
4c80c73c | 3273 | } |
983d9c90 | 3274 | |
e449de87 | 3275 | return start_unit(argc, argv, userdata); |
514f4ef5 LP |
3276 | } |
3277 | ||
988b3b17 ZJS |
3278 | static int start_system_special(int argc, char *argv[], void *userdata) { |
3279 | /* Like start_special above, but raises an error when running in user mode */ | |
3280 | ||
3281 | if (arg_scope != UNIT_FILE_SYSTEM) { | |
3282 | log_error("Bad action for %s mode.", | |
3283 | arg_scope == UNIT_FILE_GLOBAL ? "--global" : "--user"); | |
3284 | return -EINVAL; | |
3285 | } | |
3286 | ||
3287 | return start_special(argc, argv, userdata); | |
3288 | } | |
3289 | ||
fa0d5878 | 3290 | static int check_unit_generic(int code, const UnitActiveState good_states[], int nb_states, char **args) { |
e3e0314b | 3291 | _cleanup_strv_free_ char **names = NULL; |
fa0d5878 | 3292 | UnitActiveState active_state; |
4fbd7192 | 3293 | sd_bus *bus; |
729e3769 | 3294 | char **name; |
fa0d5878 | 3295 | int r, i; |
d60f6ad0 | 3296 | bool found = false; |
0183528f | 3297 | |
4fbd7192 LP |
3298 | r = acquire_bus(BUS_MANAGER, &bus); |
3299 | if (r < 0) | |
3300 | return r; | |
3301 | ||
e3e0314b | 3302 | r = expand_names(bus, args, NULL, &names); |
f647962d MS |
3303 | if (r < 0) |
3304 | return log_error_errno(r, "Failed to expand names: %m"); | |
e3e0314b ZJS |
3305 | |
3306 | STRV_FOREACH(name, names) { | |
fa0d5878 BR |
3307 | r = get_state_one_unit(bus, *name, &active_state); |
3308 | if (r < 0) | |
3309 | return r; | |
3310 | ||
3311 | if (!arg_quiet) | |
3312 | puts(unit_active_state_to_string(active_state)); | |
60f9ba0b | 3313 | |
fa0d5878 BR |
3314 | for (i = 0; i < nb_states; ++i) |
3315 | if (good_states[i] == active_state) | |
3316 | found = true; | |
1a0fce45 TA |
3317 | } |
3318 | ||
d60f6ad0 LN |
3319 | /* use the given return code for the case that we won't find |
3320 | * any unit which matches the list */ | |
3321 | return found ? 0 : code; | |
1a0fce45 TA |
3322 | } |
3323 | ||
e449de87 | 3324 | static int check_unit_active(int argc, char *argv[], void *userdata) { |
fa0d5878 | 3325 | const UnitActiveState states[] = { UNIT_ACTIVE, UNIT_RELOADING }; |
e3e0314b | 3326 | /* According to LSB: 3, "program is not running" */ |
b613907e | 3327 | return check_unit_generic(EXIT_PROGRAM_NOT_RUNNING, states, ELEMENTSOF(states), strv_skip(argv, 1)); |
e3e0314b | 3328 | } |
0183528f | 3329 | |
e449de87 | 3330 | static int check_unit_failed(int argc, char *argv[], void *userdata) { |
fa0d5878 | 3331 | const UnitActiveState states[] = { UNIT_FAILED }; |
b613907e | 3332 | return check_unit_generic(EXIT_PROGRAM_DEAD_AND_PID_EXISTS, states, ELEMENTSOF(states), strv_skip(argv, 1)); |
48220598 LP |
3333 | } |
3334 | ||
e449de87 | 3335 | static int kill_unit(int argc, char *argv[], void *userdata) { |
e3e0314b | 3336 | _cleanup_strv_free_ char **names = NULL; |
ac5e3a50 | 3337 | char *kill_who = NULL, **name; |
4fbd7192 | 3338 | sd_bus *bus; |
e3e0314b | 3339 | int r, q; |
8a0867d6 | 3340 | |
4fbd7192 LP |
3341 | r = acquire_bus(BUS_MANAGER, &bus); |
3342 | if (r < 0) | |
3343 | return r; | |
3344 | ||
d2ad7e1b ZJS |
3345 | polkit_agent_open_if_enabled(); |
3346 | ||
8a0867d6 LP |
3347 | if (!arg_kill_who) |
3348 | arg_kill_who = "all"; | |
3349 | ||
ac5e3a50 JS |
3350 | /* --fail was specified */ |
3351 | if (streq(arg_job_mode, "fail")) | |
81d62103 | 3352 | kill_who = strjoina(arg_kill_who, "-fail"); |
ac5e3a50 | 3353 | |
e449de87 | 3354 | r = expand_names(bus, strv_skip(argv, 1), NULL, &names); |
e3e0314b | 3355 | if (r < 0) |
691395d8 | 3356 | return log_error_errno(r, "Failed to expand names: %m"); |
60f9ba0b | 3357 | |
e3e0314b | 3358 | STRV_FOREACH(name, names) { |
4afd3348 | 3359 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
342641fb | 3360 | |
6e646d22 | 3361 | q = sd_bus_call_method( |
f22f08cd | 3362 | bus, |
b0193f1c LP |
3363 | "org.freedesktop.systemd1", |
3364 | "/org/freedesktop/systemd1", | |
3365 | "org.freedesktop.systemd1.Manager", | |
6e646d22 LP |
3366 | "KillUnit", |
3367 | &error, | |
3368 | NULL, | |
ac5e3a50 | 3369 | "ssi", *names, kill_who ? kill_who : arg_kill_who, arg_signal); |
e3e0314b | 3370 | if (q < 0) { |
691395d8 | 3371 | log_error_errno(q, "Failed to kill unit %s: %s", *names, bus_error_message(&error, q)); |
e3e0314b ZJS |
3372 | if (r == 0) |
3373 | r = q; | |
f459b602 | 3374 | } |
8a0867d6 | 3375 | } |
f459b602 | 3376 | |
e3e0314b | 3377 | return r; |
8a0867d6 LP |
3378 | } |
3379 | ||
582a507f | 3380 | typedef struct ExecStatusInfo { |
0129173a LP |
3381 | char *name; |
3382 | ||
582a507f LP |
3383 | char *path; |
3384 | char **argv; | |
3385 | ||
b708e7ce LP |
3386 | bool ignore; |
3387 | ||
582a507f LP |
3388 | usec_t start_timestamp; |
3389 | usec_t exit_timestamp; | |
3390 | pid_t pid; | |
3391 | int code; | |
3392 | int status; | |
3393 | ||
3394 | LIST_FIELDS(struct ExecStatusInfo, exec); | |
3395 | } ExecStatusInfo; | |
3396 | ||
3397 | static void exec_status_info_free(ExecStatusInfo *i) { | |
3398 | assert(i); | |
3399 | ||
0129173a | 3400 | free(i->name); |
582a507f LP |
3401 | free(i->path); |
3402 | strv_free(i->argv); | |
3403 | free(i); | |
3404 | } | |
3405 | ||
f459b602 | 3406 | static int exec_status_info_deserialize(sd_bus_message *m, ExecStatusInfo *i) { |
b21a0ef8 | 3407 | uint64_t start_timestamp, exit_timestamp, start_timestamp_monotonic, exit_timestamp_monotonic; |
f459b602 | 3408 | const char *path; |
582a507f LP |
3409 | uint32_t pid; |
3410 | int32_t code, status; | |
f459b602 | 3411 | int ignore, r; |
582a507f | 3412 | |
f459b602 | 3413 | assert(m); |
582a507f | 3414 | assert(i); |
582a507f | 3415 | |
f459b602 MAP |
3416 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_STRUCT, "sasbttttuii"); |
3417 | if (r < 0) | |
3418 | return bus_log_parse_error(r); | |
3419 | else if (r == 0) | |
3420 | return 0; | |
582a507f | 3421 | |
f459b602 MAP |
3422 | r = sd_bus_message_read(m, "s", &path); |
3423 | if (r < 0) | |
3424 | return bus_log_parse_error(r); | |
582a507f | 3425 | |
f84190d8 LP |
3426 | i->path = strdup(path); |
3427 | if (!i->path) | |
f459b602 | 3428 | return log_oom(); |
582a507f | 3429 | |
f459b602 MAP |
3430 | r = sd_bus_message_read_strv(m, &i->argv); |
3431 | if (r < 0) | |
3432 | return bus_log_parse_error(r); | |
3433 | ||
3434 | r = sd_bus_message_read(m, | |
3435 | "bttttuii", | |
3436 | &ignore, | |
3437 | &start_timestamp, &start_timestamp_monotonic, | |
3438 | &exit_timestamp, &exit_timestamp_monotonic, | |
3439 | &pid, | |
3440 | &code, &status); | |
3441 | if (r < 0) | |
3442 | return bus_log_parse_error(r); | |
582a507f | 3443 | |
b708e7ce | 3444 | i->ignore = ignore; |
582a507f LP |
3445 | i->start_timestamp = (usec_t) start_timestamp; |
3446 | i->exit_timestamp = (usec_t) exit_timestamp; | |
3447 | i->pid = (pid_t) pid; | |
3448 | i->code = code; | |
3449 | i->status = status; | |
3450 | ||
f459b602 MAP |
3451 | r = sd_bus_message_exit_container(m); |
3452 | if (r < 0) | |
3453 | return bus_log_parse_error(r); | |
3454 | ||
3455 | return 1; | |
582a507f LP |
3456 | } |
3457 | ||
61cbdc4b LP |
3458 | typedef struct UnitStatusInfo { |
3459 | const char *id; | |
3460 | const char *load_state; | |
3461 | const char *active_state; | |
3462 | const char *sub_state; | |
a4375746 | 3463 | const char *unit_file_state; |
d2dc52db | 3464 | const char *unit_file_preset; |
61cbdc4b LP |
3465 | |
3466 | const char *description; | |
4a9e2fff | 3467 | const char *following; |
61cbdc4b | 3468 | |
49dbfa7b LP |
3469 | char **documentation; |
3470 | ||
1b64d026 LP |
3471 | const char *fragment_path; |
3472 | const char *source_path; | |
4ad49000 | 3473 | const char *control_group; |
61cbdc4b | 3474 | |
76d14b87 OS |
3475 | char **dropin_paths; |
3476 | ||
9f39404c | 3477 | const char *load_error; |
f42806df | 3478 | const char *result; |
9f39404c | 3479 | |
584be568 | 3480 | usec_t inactive_exit_timestamp; |
df50185b | 3481 | usec_t inactive_exit_timestamp_monotonic; |
584be568 LP |
3482 | usec_t active_enter_timestamp; |
3483 | usec_t active_exit_timestamp; | |
3484 | usec_t inactive_enter_timestamp; | |
3485 | ||
45fb0699 | 3486 | bool need_daemon_reload; |
055ef36b | 3487 | bool transient; |
45fb0699 | 3488 | |
61cbdc4b LP |
3489 | /* Service */ |
3490 | pid_t main_pid; | |
3491 | pid_t control_pid; | |
3492 | const char *status_text; | |
175728c4 | 3493 | const char *pid_file; |
d06dacd0 | 3494 | bool running:1; |
b4af5a80 | 3495 | int status_errno; |
61cbdc4b LP |
3496 | |
3497 | usec_t start_timestamp; | |
3498 | usec_t exit_timestamp; | |
3499 | ||
3500 | int exit_code, exit_status; | |
3501 | ||
90bbc946 LP |
3502 | usec_t condition_timestamp; |
3503 | bool condition_result; | |
52990c2e ZJS |
3504 | bool failed_condition_trigger; |
3505 | bool failed_condition_negate; | |
3506 | const char *failed_condition; | |
59fccdc5 LP |
3507 | const char *failed_condition_parameter; |
3508 | ||
3509 | usec_t assert_timestamp; | |
3510 | bool assert_result; | |
3511 | bool failed_assert_trigger; | |
3512 | bool failed_assert_negate; | |
3513 | const char *failed_assert; | |
3514 | const char *failed_assert_parameter; | |
90bbc946 | 3515 | |
61cbdc4b LP |
3516 | /* Socket */ |
3517 | unsigned n_accepted; | |
3518 | unsigned n_connections; | |
b8131a87 | 3519 | bool accept; |
61cbdc4b | 3520 | |
13160134 | 3521 | /* Pairs of type, path */ |
67419600 OS |
3522 | char **listen; |
3523 | ||
61cbdc4b LP |
3524 | /* Device */ |
3525 | const char *sysfs_path; | |
3526 | ||
3527 | /* Mount, Automount */ | |
3528 | const char *where; | |
3529 | ||
3530 | /* Swap */ | |
3531 | const char *what; | |
582a507f | 3532 | |
934277fe LP |
3533 | /* CGroup */ |
3534 | uint64_t memory_current; | |
da4d897e TH |
3535 | uint64_t memory_low; |
3536 | uint64_t memory_high; | |
3537 | uint64_t memory_max; | |
934277fe | 3538 | uint64_t memory_limit; |
5ad096b3 | 3539 | uint64_t cpu_usage_nsec; |
03a7b521 LP |
3540 | uint64_t tasks_current; |
3541 | uint64_t tasks_max; | |
934277fe | 3542 | |
582a507f | 3543 | LIST_HEAD(ExecStatusInfo, exec); |
61cbdc4b LP |
3544 | } UnitStatusInfo; |
3545 | ||
f459b602 | 3546 | static void print_status_info( |
291d565a | 3547 | sd_bus *bus, |
f459b602 MAP |
3548 | UnitStatusInfo *i, |
3549 | bool *ellipsized) { | |
3550 | ||
582a507f | 3551 | ExecStatusInfo *p; |
b0d14c69 | 3552 | const char *active_on, *active_off, *on, *off, *ss; |
584be568 | 3553 | usec_t timestamp; |
9185c8e6 | 3554 | char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], *s1; |
584be568 | 3555 | char since2[FORMAT_TIMESTAMP_MAX], *s2; |
1b64d026 | 3556 | const char *path; |
13160134 | 3557 | char **t, **t2; |
291d565a | 3558 | int r; |
582a507f | 3559 | |
61cbdc4b LP |
3560 | assert(i); |
3561 | ||
3562 | /* This shows pretty information about a unit. See | |
3563 | * print_property() for a low-level property printer */ | |
3564 | ||
b0d14c69 LP |
3565 | if (streq_ptr(i->active_state, "failed")) { |
3566 | active_on = ansi_highlight_red(); | |
1fc464f6 | 3567 | active_off = ansi_normal(); |
b0d14c69 LP |
3568 | } else if (streq_ptr(i->active_state, "active") || streq_ptr(i->active_state, "reloading")) { |
3569 | active_on = ansi_highlight_green(); | |
1fc464f6 | 3570 | active_off = ansi_normal(); |
b0d14c69 LP |
3571 | } else |
3572 | active_on = active_off = ""; | |
3573 | ||
323b7dc9 | 3574 | printf("%s%s%s %s", active_on, special_glyph(BLACK_CIRCLE), active_off, strna(i->id)); |
61cbdc4b LP |
3575 | |
3576 | if (i->description && !streq_ptr(i->id, i->description)) | |
3577 | printf(" - %s", i->description); | |
3578 | ||
3579 | printf("\n"); | |
3580 | ||
4a9e2fff | 3581 | if (i->following) |
856323c9 | 3582 | printf(" Follow: unit currently follows state of %s\n", i->following); |
4a9e2fff | 3583 | |
f7b9e331 | 3584 | if (streq_ptr(i->load_state, "error")) { |
0b5a519c | 3585 | on = ansi_highlight_red(); |
1fc464f6 | 3586 | off = ansi_normal(); |
c31b4423 LP |
3587 | } else |
3588 | on = off = ""; | |
3589 | ||
1b64d026 LP |
3590 | path = i->source_path ? i->source_path : i->fragment_path; |
3591 | ||
055ef36b | 3592 | if (i->load_error != 0) |
856323c9 ZJS |
3593 | printf(" Loaded: %s%s%s (Reason: %s)\n", |
3594 | on, strna(i->load_state), off, i->load_error); | |
d2dc52db LP |
3595 | else if (path && !isempty(i->unit_file_state) && !isempty(i->unit_file_preset)) |
3596 | printf(" Loaded: %s%s%s (%s; %s; vendor preset: %s)\n", | |
3597 | on, strna(i->load_state), off, path, i->unit_file_state, i->unit_file_preset); | |
3598 | else if (path && !isempty(i->unit_file_state)) | |
856323c9 ZJS |
3599 | printf(" Loaded: %s%s%s (%s; %s)\n", |
3600 | on, strna(i->load_state), off, path, i->unit_file_state); | |
1b64d026 | 3601 | else if (path) |
856323c9 ZJS |
3602 | printf(" Loaded: %s%s%s (%s)\n", |
3603 | on, strna(i->load_state), off, path); | |
61cbdc4b | 3604 | else |
856323c9 ZJS |
3605 | printf(" Loaded: %s%s%s\n", |
3606 | on, strna(i->load_state), off); | |
61cbdc4b | 3607 | |
055ef36b LP |
3608 | if (i->transient) |
3609 | printf("Transient: yes\n"); | |
3610 | ||
76d14b87 | 3611 | if (!strv_isempty(i->dropin_paths)) { |
f459b602 | 3612 | _cleanup_free_ char *dir = NULL; |
76d14b87 | 3613 | bool last = false; |
f459b602 | 3614 | char ** dropin; |
76d14b87 OS |
3615 | |
3616 | STRV_FOREACH(dropin, i->dropin_paths) { | |
3617 | if (! dir || last) { | |
856323c9 | 3618 | printf(dir ? " " : " Drop-In: "); |
76d14b87 | 3619 | |
97b11eed | 3620 | dir = mfree(dir); |
76d14b87 | 3621 | |
5f311f8c LP |
3622 | dir = dirname_malloc(*dropin); |
3623 | if (!dir) { | |
76d14b87 OS |
3624 | log_oom(); |
3625 | return; | |
3626 | } | |
3627 | ||
856323c9 | 3628 | printf("%s\n %s", dir, |
323b7dc9 | 3629 | special_glyph(TREE_RIGHT)); |
76d14b87 OS |
3630 | } |
3631 | ||
3632 | last = ! (*(dropin + 1) && startswith(*(dropin + 1), dir)); | |
3633 | ||
2b6bf07d | 3634 | printf("%s%s", basename(*dropin), last ? "\n" : ", "); |
76d14b87 | 3635 | } |
76d14b87 OS |
3636 | } |
3637 | ||
2ee68f72 | 3638 | ss = streq_ptr(i->active_state, i->sub_state) ? NULL : i->sub_state; |
2ee68f72 | 3639 | if (ss) |
856323c9 | 3640 | printf(" Active: %s%s (%s)%s", |
b0d14c69 | 3641 | active_on, strna(i->active_state), ss, active_off); |
2ee68f72 | 3642 | else |
856323c9 | 3643 | printf(" Active: %s%s%s", |
b0d14c69 | 3644 | active_on, strna(i->active_state), active_off); |
61cbdc4b | 3645 | |
f42806df LP |
3646 | if (!isempty(i->result) && !streq(i->result, "success")) |
3647 | printf(" (Result: %s)", i->result); | |
3648 | ||
584be568 LP |
3649 | timestamp = (streq_ptr(i->active_state, "active") || |
3650 | streq_ptr(i->active_state, "reloading")) ? i->active_enter_timestamp : | |
3651 | (streq_ptr(i->active_state, "inactive") || | |
fdf20a31 | 3652 | streq_ptr(i->active_state, "failed")) ? i->inactive_enter_timestamp : |
584be568 LP |
3653 | streq_ptr(i->active_state, "activating") ? i->inactive_exit_timestamp : |
3654 | i->active_exit_timestamp; | |
3655 | ||
bbb8486e | 3656 | s1 = format_timestamp_relative(since1, sizeof(since1), timestamp); |
584be568 LP |
3657 | s2 = format_timestamp(since2, sizeof(since2), timestamp); |
3658 | ||
3659 | if (s1) | |
538da63d | 3660 | printf(" since %s; %s\n", s2, s1); |
584be568 | 3661 | else if (s2) |
538da63d | 3662 | printf(" since %s\n", s2); |
584be568 LP |
3663 | else |
3664 | printf("\n"); | |
3665 | ||
90bbc946 | 3666 | if (!i->condition_result && i->condition_timestamp > 0) { |
bbb8486e | 3667 | s1 = format_timestamp_relative(since1, sizeof(since1), i->condition_timestamp); |
90bbc946 LP |
3668 | s2 = format_timestamp(since2, sizeof(since2), i->condition_timestamp); |
3669 | ||
59fccdc5 | 3670 | printf("Condition: start %scondition failed%s at %s%s%s\n", |
1fc464f6 | 3671 | ansi_highlight_yellow(), ansi_normal(), |
5cfee414 | 3672 | s2, s1 ? "; " : "", strempty(s1)); |
52990c2e ZJS |
3673 | if (i->failed_condition_trigger) |
3674 | printf(" none of the trigger conditions were met\n"); | |
3675 | else if (i->failed_condition) | |
3676 | printf(" %s=%s%s was not met\n", | |
3677 | i->failed_condition, | |
3678 | i->failed_condition_negate ? "!" : "", | |
59fccdc5 LP |
3679 | i->failed_condition_parameter); |
3680 | } | |
3681 | ||
3682 | if (!i->assert_result && i->assert_timestamp > 0) { | |
3683 | s1 = format_timestamp_relative(since1, sizeof(since1), i->assert_timestamp); | |
3684 | s2 = format_timestamp(since2, sizeof(since2), i->assert_timestamp); | |
3685 | ||
3686 | printf(" Assert: start %sassertion failed%s at %s%s%s\n", | |
1fc464f6 | 3687 | ansi_highlight_red(), ansi_normal(), |
5cfee414 | 3688 | s2, s1 ? "; " : "", strempty(s1)); |
59fccdc5 LP |
3689 | if (i->failed_assert_trigger) |
3690 | printf(" none of the trigger assertions were met\n"); | |
3691 | else if (i->failed_assert) | |
3692 | printf(" %s=%s%s was not met\n", | |
3693 | i->failed_assert, | |
3694 | i->failed_assert_negate ? "!" : "", | |
3695 | i->failed_assert_parameter); | |
90bbc946 LP |
3696 | } |
3697 | ||
61cbdc4b | 3698 | if (i->sysfs_path) |
856323c9 | 3699 | printf(" Device: %s\n", i->sysfs_path); |
9feeba4b | 3700 | if (i->where) |
856323c9 | 3701 | printf(" Where: %s\n", i->where); |
9feeba4b | 3702 | if (i->what) |
856323c9 | 3703 | printf(" What: %s\n", i->what); |
49dbfa7b | 3704 | |
13160134 | 3705 | STRV_FOREACH(t, i->documentation) |
856323c9 | 3706 | printf(" %*s %s\n", 9, t == i->documentation ? "Docs:" : "", *t); |
49dbfa7b | 3707 | |
13160134 | 3708 | STRV_FOREACH_PAIR(t, t2, i->listen) |
856323c9 | 3709 | printf(" %*s %s (%s)\n", 9, t == i->listen ? "Listen:" : "", *t2, *t); |
67419600 | 3710 | |
b8131a87 | 3711 | if (i->accept) |
856323c9 | 3712 | printf(" Accepted: %u; Connected: %u\n", i->n_accepted, i->n_connections); |
61cbdc4b | 3713 | |
582a507f | 3714 | LIST_FOREACH(exec, p, i->exec) { |
13160134 | 3715 | _cleanup_free_ char *argv = NULL; |
9a57c629 | 3716 | bool good; |
582a507f LP |
3717 | |
3718 | /* Only show exited processes here */ | |
3719 | if (p->code == 0) | |
3720 | continue; | |
3721 | ||
13160134 | 3722 | argv = strv_join(p->argv, " "); |
1fa2f38f | 3723 | printf(" Process: "PID_FMT" %s=%s ", p->pid, p->name, strna(argv)); |
582a507f | 3724 | |
96342de6 | 3725 | good = is_clean_exit_lsb(p->code, p->status, NULL); |
9a57c629 | 3726 | if (!good) { |
0b5a519c | 3727 | on = ansi_highlight_red(); |
1fc464f6 | 3728 | off = ansi_normal(); |
9a57c629 LP |
3729 | } else |
3730 | on = off = ""; | |
3731 | ||
3732 | printf("%s(code=%s, ", on, sigchld_code_to_string(p->code)); | |
3733 | ||
d06dacd0 LP |
3734 | if (p->code == CLD_EXITED) { |
3735 | const char *c; | |
3736 | ||
582a507f | 3737 | printf("status=%i", p->status); |
d06dacd0 | 3738 | |
1b64d026 LP |
3739 | c = exit_status_to_string(p->status, EXIT_STATUS_SYSTEMD); |
3740 | if (c) | |
d06dacd0 LP |
3741 | printf("/%s", c); |
3742 | ||
3743 | } else | |
582a507f | 3744 | printf("signal=%s", signal_to_string(p->status)); |
9a57c629 LP |
3745 | |
3746 | printf(")%s\n", off); | |
3747 | ||
582a507f LP |
3748 | if (i->main_pid == p->pid && |
3749 | i->start_timestamp == p->start_timestamp && | |
3750 | i->exit_timestamp == p->start_timestamp) | |
3751 | /* Let's not show this twice */ | |
3752 | i->main_pid = 0; | |
3753 | ||
3754 | if (p->pid == i->control_pid) | |
3755 | i->control_pid = 0; | |
3756 | } | |
3757 | ||
61cbdc4b | 3758 | if (i->main_pid > 0 || i->control_pid > 0) { |
61cbdc4b | 3759 | if (i->main_pid > 0) { |
8c06592f | 3760 | printf(" Main PID: "PID_FMT, i->main_pid); |
61cbdc4b LP |
3761 | |
3762 | if (i->running) { | |
13160134 | 3763 | _cleanup_free_ char *comm = NULL; |
1089dcd4 | 3764 | (void) get_process_comm(i->main_pid, &comm); |
13160134 ZJS |
3765 | if (comm) |
3766 | printf(" (%s)", comm); | |
6d4fc029 | 3767 | } else if (i->exit_code > 0) { |
61cbdc4b LP |
3768 | printf(" (code=%s, ", sigchld_code_to_string(i->exit_code)); |
3769 | ||
d06dacd0 LP |
3770 | if (i->exit_code == CLD_EXITED) { |
3771 | const char *c; | |
3772 | ||
61cbdc4b | 3773 | printf("status=%i", i->exit_status); |
d06dacd0 | 3774 | |
1b64d026 LP |
3775 | c = exit_status_to_string(i->exit_status, EXIT_STATUS_SYSTEMD); |
3776 | if (c) | |
d06dacd0 LP |
3777 | printf("/%s", c); |
3778 | ||
3779 | } else | |
582a507f | 3780 | printf("signal=%s", signal_to_string(i->exit_status)); |
6d4fc029 LP |
3781 | printf(")"); |
3782 | } | |
13160134 | 3783 | } |
61cbdc4b LP |
3784 | |
3785 | if (i->control_pid > 0) { | |
13160134 | 3786 | _cleanup_free_ char *c = NULL; |
61cbdc4b | 3787 | |
1089dcd4 LP |
3788 | if (i->main_pid > 0) |
3789 | fputs("; Control PID: ", stdout); | |
3790 | else | |
3791 | fputs("Cntrl PID: ", stdout); /* if first in column, abbreviated so it fits alignment */ | |
3792 | ||
3793 | printf(PID_FMT, i->control_pid); | |
61cbdc4b | 3794 | |
1089dcd4 | 3795 | (void) get_process_comm(i->control_pid, &c); |
13160134 ZJS |
3796 | if (c) |
3797 | printf(" (%s)", c); | |
61cbdc4b LP |
3798 | } |
3799 | ||
3800 | printf("\n"); | |
3801 | } | |
3802 | ||
17bb7382 | 3803 | if (i->status_text) |
856323c9 | 3804 | printf(" Status: \"%s\"\n", i->status_text); |
b4af5a80 LP |
3805 | if (i->status_errno > 0) |
3806 | printf(" Error: %i (%s)\n", i->status_errno, strerror(i->status_errno)); | |
17bb7382 | 3807 | |
03a7b521 LP |
3808 | if (i->tasks_current != (uint64_t) -1) { |
3809 | printf(" Tasks: %" PRIu64, i->tasks_current); | |
3810 | ||
3811 | if (i->tasks_max != (uint64_t) -1) | |
3812 | printf(" (limit: %" PRIi64 ")\n", i->tasks_max); | |
3813 | else | |
3814 | printf("\n"); | |
3815 | } | |
3816 | ||
934277fe LP |
3817 | if (i->memory_current != (uint64_t) -1) { |
3818 | char buf[FORMAT_BYTES_MAX]; | |
3819 | ||
3820 | printf(" Memory: %s", format_bytes(buf, sizeof(buf), i->memory_current)); | |
3821 | ||
da4d897e TH |
3822 | if (i->memory_low > 0 || i->memory_high != CGROUP_LIMIT_MAX || i->memory_max != CGROUP_LIMIT_MAX || |
3823 | i->memory_limit != CGROUP_LIMIT_MAX) { | |
3824 | const char *prefix = ""; | |
3825 | ||
3826 | printf(" ("); | |
3827 | if (i->memory_low > 0) { | |
3828 | printf("%slow: %s", prefix, format_bytes(buf, sizeof(buf), i->memory_low)); | |
3829 | prefix = " "; | |
3830 | } | |
3831 | if (i->memory_high != CGROUP_LIMIT_MAX) { | |
3832 | printf("%shigh: %s", prefix, format_bytes(buf, sizeof(buf), i->memory_high)); | |
3833 | prefix = " "; | |
3834 | } | |
3835 | if (i->memory_max != CGROUP_LIMIT_MAX) { | |
3836 | printf("%smax: %s", prefix, format_bytes(buf, sizeof(buf), i->memory_max)); | |
3837 | prefix = " "; | |
3838 | } | |
3839 | if (i->memory_limit != CGROUP_LIMIT_MAX) { | |
3840 | printf("%slimit: %s", prefix, format_bytes(buf, sizeof(buf), i->memory_limit)); | |
3841 | prefix = " "; | |
3842 | } | |
3843 | printf(")"); | |
3844 | } | |
3845 | printf("\n"); | |
934277fe LP |
3846 | } |
3847 | ||
5ad096b3 LP |
3848 | if (i->cpu_usage_nsec != (uint64_t) -1) { |
3849 | char buf[FORMAT_TIMESPAN_MAX]; | |
3850 | printf(" CPU: %s\n", format_timespan(buf, sizeof(buf), i->cpu_usage_nsec / NSEC_PER_USEC, USEC_PER_MSEC)); | |
3851 | } | |
3852 | ||
51e22f88 | 3853 | if (i->control_group) { |
291d565a LP |
3854 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
3855 | static const char prefix[] = " "; | |
ab35fb1b LP |
3856 | unsigned c; |
3857 | ||
51e22f88 LP |
3858 | printf(" CGroup: %s\n", i->control_group); |
3859 | ||
291d565a LP |
3860 | c = columns(); |
3861 | if (c > sizeof(prefix) - 1) | |
3862 | c -= sizeof(prefix) - 1; | |
3863 | else | |
3864 | c = 0; | |
ab35fb1b | 3865 | |
291d565a LP |
3866 | r = unit_show_processes(bus, i->id, i->control_group, prefix, c, get_output_flags(), &error); |
3867 | if (r == -EBADR) { | |
b69d29ce LP |
3868 | unsigned k = 0; |
3869 | pid_t extra[2]; | |
3870 | ||
291d565a | 3871 | /* Fallback for older systemd versions where the GetUnitProcesses() call is not yet available */ |
ab35fb1b | 3872 | |
b69d29ce LP |
3873 | if (i->main_pid > 0) |
3874 | extra[k++] = i->main_pid; | |
3875 | ||
3876 | if (i->control_pid > 0) | |
3877 | extra[k++] = i->control_pid; | |
3878 | ||
0ff308c8 | 3879 | show_cgroup_and_extra(SYSTEMD_CGROUP_CONTROLLER, i->control_group, prefix, c, extra, k, get_output_flags()); |
291d565a LP |
3880 | } else if (r < 0) |
3881 | log_warning_errno(r, "Failed to dump process list, ignoring: %s", bus_error_message(&error, r)); | |
c59760ee | 3882 | } |
45fb0699 | 3883 | |
ece174c5 | 3884 | if (i->id && arg_transport == BUS_TRANSPORT_LOCAL) |
3c756001 LP |
3885 | show_journal_by_unit( |
3886 | stdout, | |
3887 | i->id, | |
3888 | arg_output, | |
3889 | 0, | |
3890 | i->inactive_exit_timestamp_monotonic, | |
3891 | arg_lines, | |
3892 | getuid(), | |
3893 | get_output_flags() | OUTPUT_BEGIN_NEWLINE, | |
3894 | SD_JOURNAL_LOCAL_ONLY, | |
3895 | arg_scope == UNIT_FILE_SYSTEM, | |
3896 | ellipsized); | |
86aa7ba4 | 3897 | |
45fb0699 | 3898 | if (i->need_daemon_reload) |
3f36991e | 3899 | warn_unit_file_changed(i->id); |
61cbdc4b LP |
3900 | } |
3901 | ||
b43f208f | 3902 | static void show_unit_help(UnitStatusInfo *i) { |
256425cc LP |
3903 | char **p; |
3904 | ||
3905 | assert(i); | |
3906 | ||
3907 | if (!i->documentation) { | |
3908 | log_info("Documentation for %s not known.", i->id); | |
3909 | return; | |
3910 | } | |
3911 | ||
78002a67 ZJS |
3912 | STRV_FOREACH(p, i->documentation) |
3913 | if (startswith(*p, "man:")) | |
3914 | show_man_page(*p + 4, false); | |
3915 | else | |
0315fe37 | 3916 | log_info("Can't show: %s", *p); |
256425cc LP |
3917 | } |
3918 | ||
f459b602 MAP |
3919 | static int status_property(const char *name, sd_bus_message *m, UnitStatusInfo *i, const char *contents) { |
3920 | int r; | |
61cbdc4b | 3921 | |
a4c279f8 | 3922 | assert(name); |
f459b602 | 3923 | assert(m); |
a4c279f8 LP |
3924 | assert(i); |
3925 | ||
f459b602 | 3926 | switch (contents[0]) { |
61cbdc4b | 3927 | |
f459b602 | 3928 | case SD_BUS_TYPE_STRING: { |
61cbdc4b LP |
3929 | const char *s; |
3930 | ||
f459b602 MAP |
3931 | r = sd_bus_message_read(m, "s", &s); |
3932 | if (r < 0) | |
3933 | return bus_log_parse_error(r); | |
61cbdc4b | 3934 | |
37a0d5bf | 3935 | if (!isempty(s)) { |
61cbdc4b LP |
3936 | if (streq(name, "Id")) |
3937 | i->id = s; | |
3938 | else if (streq(name, "LoadState")) | |
3939 | i->load_state = s; | |
3940 | else if (streq(name, "ActiveState")) | |
3941 | i->active_state = s; | |
3942 | else if (streq(name, "SubState")) | |
3943 | i->sub_state = s; | |
3944 | else if (streq(name, "Description")) | |
3945 | i->description = s; | |
3946 | else if (streq(name, "FragmentPath")) | |
1b64d026 LP |
3947 | i->fragment_path = s; |
3948 | else if (streq(name, "SourcePath")) | |
3949 | i->source_path = s; | |
286ca485 | 3950 | #ifndef NOLEGACY |
a00963a2 LP |
3951 | else if (streq(name, "DefaultControlGroup")) { |
3952 | const char *e; | |
3953 | e = startswith(s, SYSTEMD_CGROUP_CONTROLLER ":"); | |
3954 | if (e) | |
3955 | i->control_group = e; | |
3956 | } | |
4ad49000 | 3957 | #endif |
37a0d5bf LP |
3958 | else if (streq(name, "ControlGroup")) |
3959 | i->control_group = s; | |
61cbdc4b LP |
3960 | else if (streq(name, "StatusText")) |
3961 | i->status_text = s; | |
175728c4 HH |
3962 | else if (streq(name, "PIDFile")) |
3963 | i->pid_file = s; | |
61cbdc4b LP |
3964 | else if (streq(name, "SysFSPath")) |
3965 | i->sysfs_path = s; | |
3966 | else if (streq(name, "Where")) | |
3967 | i->where = s; | |
3968 | else if (streq(name, "What")) | |
3969 | i->what = s; | |
4a9e2fff LP |
3970 | else if (streq(name, "Following")) |
3971 | i->following = s; | |
a4375746 LP |
3972 | else if (streq(name, "UnitFileState")) |
3973 | i->unit_file_state = s; | |
d2dc52db LP |
3974 | else if (streq(name, "UnitFilePreset")) |
3975 | i->unit_file_preset = s; | |
f42806df LP |
3976 | else if (streq(name, "Result")) |
3977 | i->result = s; | |
61cbdc4b LP |
3978 | } |
3979 | ||
3980 | break; | |
3981 | } | |
3982 | ||
f459b602 MAP |
3983 | case SD_BUS_TYPE_BOOLEAN: { |
3984 | int b; | |
b8131a87 | 3985 | |
f459b602 MAP |
3986 | r = sd_bus_message_read(m, "b", &b); |
3987 | if (r < 0) | |
3988 | return bus_log_parse_error(r); | |
b8131a87 LP |
3989 | |
3990 | if (streq(name, "Accept")) | |
3991 | i->accept = b; | |
45fb0699 LP |
3992 | else if (streq(name, "NeedDaemonReload")) |
3993 | i->need_daemon_reload = b; | |
90bbc946 LP |
3994 | else if (streq(name, "ConditionResult")) |
3995 | i->condition_result = b; | |
59fccdc5 LP |
3996 | else if (streq(name, "AssertResult")) |
3997 | i->assert_result = b; | |
055ef36b LP |
3998 | else if (streq(name, "Transient")) |
3999 | i->transient = b; | |
b8131a87 LP |
4000 | |
4001 | break; | |
4002 | } | |
4003 | ||
f459b602 | 4004 | case SD_BUS_TYPE_UINT32: { |
61cbdc4b LP |
4005 | uint32_t u; |
4006 | ||
f459b602 MAP |
4007 | r = sd_bus_message_read(m, "u", &u); |
4008 | if (r < 0) | |
4009 | return bus_log_parse_error(r); | |
61cbdc4b LP |
4010 | |
4011 | if (streq(name, "MainPID")) { | |
4012 | if (u > 0) { | |
4013 | i->main_pid = (pid_t) u; | |
4014 | i->running = true; | |
4015 | } | |
4016 | } else if (streq(name, "ControlPID")) | |
4017 | i->control_pid = (pid_t) u; | |
4018 | else if (streq(name, "ExecMainPID")) { | |
4019 | if (u > 0) | |
4020 | i->main_pid = (pid_t) u; | |
4021 | } else if (streq(name, "NAccepted")) | |
4022 | i->n_accepted = u; | |
4023 | else if (streq(name, "NConnections")) | |
4024 | i->n_connections = u; | |
4025 | ||
4026 | break; | |
4027 | } | |
4028 | ||
f459b602 | 4029 | case SD_BUS_TYPE_INT32: { |
61cbdc4b LP |
4030 | int32_t j; |
4031 | ||
f459b602 MAP |
4032 | r = sd_bus_message_read(m, "i", &j); |
4033 | if (r < 0) | |
4034 | return bus_log_parse_error(r); | |
61cbdc4b LP |
4035 | |
4036 | if (streq(name, "ExecMainCode")) | |
4037 | i->exit_code = (int) j; | |
4038 | else if (streq(name, "ExecMainStatus")) | |
4039 | i->exit_status = (int) j; | |
b4af5a80 LP |
4040 | else if (streq(name, "StatusErrno")) |
4041 | i->status_errno = (int) j; | |
61cbdc4b LP |
4042 | |
4043 | break; | |
4044 | } | |
4045 | ||
f459b602 | 4046 | case SD_BUS_TYPE_UINT64: { |
61cbdc4b LP |
4047 | uint64_t u; |
4048 | ||
f459b602 MAP |
4049 | r = sd_bus_message_read(m, "t", &u); |
4050 | if (r < 0) | |
4051 | return bus_log_parse_error(r); | |
61cbdc4b LP |
4052 | |
4053 | if (streq(name, "ExecMainStartTimestamp")) | |
4054 | i->start_timestamp = (usec_t) u; | |
4055 | else if (streq(name, "ExecMainExitTimestamp")) | |
4056 | i->exit_timestamp = (usec_t) u; | |
584be568 LP |
4057 | else if (streq(name, "ActiveEnterTimestamp")) |
4058 | i->active_enter_timestamp = (usec_t) u; | |
4059 | else if (streq(name, "InactiveEnterTimestamp")) | |
4060 | i->inactive_enter_timestamp = (usec_t) u; | |
4061 | else if (streq(name, "InactiveExitTimestamp")) | |
4062 | i->inactive_exit_timestamp = (usec_t) u; | |
df50185b LP |
4063 | else if (streq(name, "InactiveExitTimestampMonotonic")) |
4064 | i->inactive_exit_timestamp_monotonic = (usec_t) u; | |
584be568 LP |
4065 | else if (streq(name, "ActiveExitTimestamp")) |
4066 | i->active_exit_timestamp = (usec_t) u; | |
90bbc946 LP |
4067 | else if (streq(name, "ConditionTimestamp")) |
4068 | i->condition_timestamp = (usec_t) u; | |
59fccdc5 LP |
4069 | else if (streq(name, "AssertTimestamp")) |
4070 | i->assert_timestamp = (usec_t) u; | |
934277fe LP |
4071 | else if (streq(name, "MemoryCurrent")) |
4072 | i->memory_current = u; | |
da4d897e TH |
4073 | else if (streq(name, "MemoryLow")) |
4074 | i->memory_low = u; | |
4075 | else if (streq(name, "MemoryHigh")) | |
4076 | i->memory_high = u; | |
4077 | else if (streq(name, "MemoryMax")) | |
4078 | i->memory_max = u; | |
934277fe LP |
4079 | else if (streq(name, "MemoryLimit")) |
4080 | i->memory_limit = u; | |
03a7b521 LP |
4081 | else if (streq(name, "TasksCurrent")) |
4082 | i->tasks_current = u; | |
4083 | else if (streq(name, "TasksMax")) | |
4084 | i->tasks_max = u; | |
5ad096b3 LP |
4085 | else if (streq(name, "CPUUsageNSec")) |
4086 | i->cpu_usage_nsec = u; | |
61cbdc4b LP |
4087 | |
4088 | break; | |
4089 | } | |
582a507f | 4090 | |
f459b602 | 4091 | case SD_BUS_TYPE_ARRAY: |
582a507f | 4092 | |
f459b602 MAP |
4093 | if (contents[1] == SD_BUS_TYPE_STRUCT_BEGIN && startswith(name, "Exec")) { |
4094 | _cleanup_free_ ExecStatusInfo *info = NULL; | |
582a507f | 4095 | |
f459b602 MAP |
4096 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sasbttttuii)"); |
4097 | if (r < 0) | |
4098 | return bus_log_parse_error(r); | |
582a507f | 4099 | |
f459b602 MAP |
4100 | info = new0(ExecStatusInfo, 1); |
4101 | if (!info) | |
4102 | return log_oom(); | |
582a507f | 4103 | |
f459b602 | 4104 | while ((r = exec_status_info_deserialize(m, info)) > 0) { |
0129173a | 4105 | |
f459b602 MAP |
4106 | info->name = strdup(name); |
4107 | if (!info->name) | |
691395d8 | 4108 | return log_oom(); |
582a507f | 4109 | |
71fda00f | 4110 | LIST_PREPEND(exec, i->exec, info); |
582a507f | 4111 | |
f459b602 MAP |
4112 | info = new0(ExecStatusInfo, 1); |
4113 | if (!info) | |
691395d8 | 4114 | return log_oom(); |
49dbfa7b | 4115 | } |
67419600 | 4116 | |
f459b602 MAP |
4117 | if (r < 0) |
4118 | return bus_log_parse_error(r); | |
67419600 | 4119 | |
f459b602 MAP |
4120 | r = sd_bus_message_exit_container(m); |
4121 | if (r < 0) | |
4122 | return bus_log_parse_error(r); | |
67419600 | 4123 | |
f459b602 | 4124 | return 0; |
67419600 | 4125 | |
f459b602 MAP |
4126 | } else if (contents[1] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "Listen")) { |
4127 | const char *type, *path; | |
13160134 | 4128 | |
f459b602 MAP |
4129 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(ss)"); |
4130 | if (r < 0) | |
4131 | return bus_log_parse_error(r); | |
67419600 | 4132 | |
f459b602 | 4133 | while ((r = sd_bus_message_read(m, "(ss)", &type, &path)) > 0) { |
67419600 | 4134 | |
f459b602 MAP |
4135 | r = strv_extend(&i->listen, type); |
4136 | if (r < 0) | |
4137 | return r; | |
67419600 | 4138 | |
f459b602 MAP |
4139 | r = strv_extend(&i->listen, path); |
4140 | if (r < 0) | |
4141 | return r; | |
4142 | } | |
76d14b87 | 4143 | if (r < 0) |
f459b602 | 4144 | return bus_log_parse_error(r); |
76d14b87 | 4145 | |
f459b602 MAP |
4146 | r = sd_bus_message_exit_container(m); |
4147 | if (r < 0) | |
4148 | return bus_log_parse_error(r); | |
49dbfa7b | 4149 | |
f459b602 | 4150 | return 0; |
49dbfa7b | 4151 | |
f459b602 | 4152 | } else if (contents[1] == SD_BUS_TYPE_STRING && streq(name, "DropInPaths")) { |
49dbfa7b | 4153 | |
f459b602 MAP |
4154 | r = sd_bus_message_read_strv(m, &i->dropin_paths); |
4155 | if (r < 0) | |
4156 | return bus_log_parse_error(r); | |
49dbfa7b | 4157 | |
f459b602 | 4158 | } else if (contents[1] == SD_BUS_TYPE_STRING && streq(name, "Documentation")) { |
49dbfa7b | 4159 | |
f459b602 MAP |
4160 | r = sd_bus_message_read_strv(m, &i->documentation); |
4161 | if (r < 0) | |
4162 | return bus_log_parse_error(r); | |
52990c2e | 4163 | |
f459b602 MAP |
4164 | } else if (contents[1] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "Conditions")) { |
4165 | const char *cond, *param; | |
4166 | int trigger, negate; | |
4167 | int32_t state; | |
52990c2e | 4168 | |
f459b602 MAP |
4169 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sbbsi)"); |
4170 | if (r < 0) | |
4171 | return bus_log_parse_error(r); | |
4172 | ||
4173 | while ((r = sd_bus_message_read(m, "(sbbsi)", &cond, &trigger, &negate, ¶m, &state)) > 0) { | |
4174 | log_debug("%s %d %d %s %d", cond, trigger, negate, param, state); | |
4175 | if (state < 0 && (!trigger || !i->failed_condition)) { | |
4176 | i->failed_condition = cond; | |
4177 | i->failed_condition_trigger = trigger; | |
4178 | i->failed_condition_negate = negate; | |
59fccdc5 LP |
4179 | i->failed_condition_parameter = param; |
4180 | } | |
4181 | } | |
4182 | if (r < 0) | |
4183 | return bus_log_parse_error(r); | |
4184 | ||
4185 | r = sd_bus_message_exit_container(m); | |
4186 | if (r < 0) | |
4187 | return bus_log_parse_error(r); | |
4188 | ||
4189 | } else if (contents[1] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "Asserts")) { | |
4190 | const char *cond, *param; | |
4191 | int trigger, negate; | |
4192 | int32_t state; | |
4193 | ||
4194 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sbbsi)"); | |
4195 | if (r < 0) | |
4196 | return bus_log_parse_error(r); | |
4197 | ||
4198 | while ((r = sd_bus_message_read(m, "(sbbsi)", &cond, &trigger, &negate, ¶m, &state)) > 0) { | |
4199 | log_debug("%s %d %d %s %d", cond, trigger, negate, param, state); | |
4200 | if (state < 0 && (!trigger || !i->failed_assert)) { | |
4201 | i->failed_assert = cond; | |
4202 | i->failed_assert_trigger = trigger; | |
4203 | i->failed_assert_negate = negate; | |
4204 | i->failed_assert_parameter = param; | |
f459b602 | 4205 | } |
582a507f | 4206 | } |
f459b602 MAP |
4207 | if (r < 0) |
4208 | return bus_log_parse_error(r); | |
4209 | ||
4210 | r = sd_bus_message_exit_container(m); | |
4211 | if (r < 0) | |
4212 | return bus_log_parse_error(r); | |
4213 | ||
4214 | } else | |
4215 | goto skip; | |
582a507f LP |
4216 | |
4217 | break; | |
9f39404c | 4218 | |
f459b602 | 4219 | case SD_BUS_TYPE_STRUCT_BEGIN: |
9f39404c LP |
4220 | |
4221 | if (streq(name, "LoadError")) { | |
9f39404c | 4222 | const char *n, *message; |
9f39404c | 4223 | |
f459b602 | 4224 | r = sd_bus_message_read(m, "(ss)", &n, &message); |
9f39404c | 4225 | if (r < 0) |
f459b602 | 4226 | return bus_log_parse_error(r); |
9f39404c LP |
4227 | |
4228 | if (!isempty(message)) | |
4229 | i->load_error = message; | |
f459b602 MAP |
4230 | } else |
4231 | goto skip; | |
9f39404c LP |
4232 | |
4233 | break; | |
f459b602 MAP |
4234 | |
4235 | default: | |
4236 | goto skip; | |
9f39404c | 4237 | } |
f459b602 MAP |
4238 | |
4239 | return 0; | |
4240 | ||
4241 | skip: | |
4242 | r = sd_bus_message_skip(m, contents); | |
4243 | if (r < 0) | |
4244 | return bus_log_parse_error(r); | |
61cbdc4b LP |
4245 | |
4246 | return 0; | |
4247 | } | |
4248 | ||
4f9a9105 ZJS |
4249 | #define print_prop(name, fmt, ...) \ |
4250 | do { \ | |
4251 | if (arg_value) \ | |
4252 | printf(fmt "\n", __VA_ARGS__); \ | |
4253 | else \ | |
4254 | printf("%s=" fmt "\n", name, __VA_ARGS__); \ | |
4255 | } while(0) | |
4256 | ||
f459b602 MAP |
4257 | static int print_property(const char *name, sd_bus_message *m, const char *contents) { |
4258 | int r; | |
4259 | ||
48220598 | 4260 | assert(name); |
f459b602 | 4261 | assert(m); |
48220598 | 4262 | |
61cbdc4b LP |
4263 | /* This is a low-level property printer, see |
4264 | * print_status_info() for the nicer output */ | |
4265 | ||
852c1b4d ZJS |
4266 | if (arg_properties && !strv_find(arg_properties, name)) { |
4267 | /* skip what we didn't read */ | |
4268 | r = sd_bus_message_skip(m, contents); | |
4269 | return r; | |
4270 | } | |
48220598 | 4271 | |
f459b602 | 4272 | switch (contents[0]) { |
48220598 | 4273 | |
f459b602 | 4274 | case SD_BUS_TYPE_STRUCT_BEGIN: |
48220598 | 4275 | |
f459b602 | 4276 | if (contents[1] == SD_BUS_TYPE_UINT32 && streq(name, "Job")) { |
48220598 LP |
4277 | uint32_t u; |
4278 | ||
f459b602 MAP |
4279 | r = sd_bus_message_read(m, "(uo)", &u, NULL); |
4280 | if (r < 0) | |
4281 | return bus_log_parse_error(r); | |
48220598 | 4282 | |
f459b602 | 4283 | if (u > 0) |
4f9a9105 | 4284 | print_prop(name, "%"PRIu32, u); |
48220598 | 4285 | else if (arg_all) |
4f9a9105 | 4286 | print_prop(name, "%s", ""); |
48220598 LP |
4287 | |
4288 | return 0; | |
f459b602 MAP |
4289 | |
4290 | } else if (contents[1] == SD_BUS_TYPE_STRING && streq(name, "Unit")) { | |
48220598 LP |
4291 | const char *s; |
4292 | ||
f459b602 MAP |
4293 | r = sd_bus_message_read(m, "(so)", &s, NULL); |
4294 | if (r < 0) | |
4295 | return bus_log_parse_error(r); | |
48220598 | 4296 | |
f459b602 | 4297 | if (arg_all || !isempty(s)) |
4f9a9105 | 4298 | print_prop(name, "%s", s); |
48220598 LP |
4299 | |
4300 | return 0; | |
f459b602 MAP |
4301 | |
4302 | } else if (contents[1] == SD_BUS_TYPE_STRING && streq(name, "LoadError")) { | |
9f39404c LP |
4303 | const char *a = NULL, *b = NULL; |
4304 | ||
f459b602 MAP |
4305 | r = sd_bus_message_read(m, "(ss)", &a, &b); |
4306 | if (r < 0) | |
4307 | return bus_log_parse_error(r); | |
9f39404c LP |
4308 | |
4309 | if (arg_all || !isempty(a) || !isempty(b)) | |
4f9a9105 | 4310 | print_prop(name, "%s \"%s\"", strempty(a), strempty(b)); |
f786e80d | 4311 | |
57183d11 LP |
4312 | return 0; |
4313 | } else if (streq_ptr(name, "SystemCallFilter")) { | |
4314 | _cleanup_strv_free_ char **l = NULL; | |
4315 | int whitelist; | |
4316 | ||
4317 | r = sd_bus_message_enter_container(m, 'r', "bas"); | |
4318 | if (r < 0) | |
4319 | return bus_log_parse_error(r); | |
4320 | ||
4321 | r = sd_bus_message_read(m, "b", &whitelist); | |
4322 | if (r < 0) | |
4323 | return bus_log_parse_error(r); | |
4324 | ||
4325 | r = sd_bus_message_read_strv(m, &l); | |
4326 | if (r < 0) | |
4327 | return bus_log_parse_error(r); | |
4328 | ||
4329 | r = sd_bus_message_exit_container(m); | |
4330 | if (r < 0) | |
4331 | return bus_log_parse_error(r); | |
4332 | ||
4333 | if (arg_all || whitelist || !strv_isempty(l)) { | |
4334 | bool first = true; | |
4335 | char **i; | |
4336 | ||
4f9a9105 ZJS |
4337 | if (!arg_value) { |
4338 | fputs(name, stdout); | |
4339 | fputc('=', stdout); | |
4340 | } | |
57183d11 LP |
4341 | |
4342 | if (!whitelist) | |
4343 | fputc('~', stdout); | |
4344 | ||
4345 | STRV_FOREACH(i, l) { | |
4346 | if (first) | |
4347 | first = false; | |
4348 | else | |
4349 | fputc(' ', stdout); | |
4350 | ||
4351 | fputs(*i, stdout); | |
4352 | } | |
4353 | fputc('\n', stdout); | |
4354 | } | |
4355 | ||
f786e80d | 4356 | return 0; |
48220598 LP |
4357 | } |
4358 | ||
4359 | break; | |
48220598 | 4360 | |
f459b602 | 4361 | case SD_BUS_TYPE_ARRAY: |
48220598 | 4362 | |
f459b602 MAP |
4363 | if (contents[1] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "EnvironmentFiles")) { |
4364 | const char *path; | |
4365 | int ignore; | |
8c7be95e | 4366 | |
f459b602 MAP |
4367 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sb)"); |
4368 | if (r < 0) | |
4369 | return bus_log_parse_error(r); | |
8c7be95e | 4370 | |
f459b602 | 4371 | while ((r = sd_bus_message_read(m, "(sb)", &path, &ignore)) > 0) |
a1abf5ea | 4372 | print_prop("EnvironmentFile", "%s (ignore_errors=%s)", path, yes_no(ignore)); |
8c7be95e | 4373 | |
f459b602 MAP |
4374 | if (r < 0) |
4375 | return bus_log_parse_error(r); | |
8c7be95e | 4376 | |
f459b602 MAP |
4377 | r = sd_bus_message_exit_container(m); |
4378 | if (r < 0) | |
4379 | return bus_log_parse_error(r); | |
8c7be95e LP |
4380 | |
4381 | return 0; | |
4382 | ||
f459b602 MAP |
4383 | } else if (contents[1] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "Paths")) { |
4384 | const char *type, *path; | |
67419600 | 4385 | |
f459b602 MAP |
4386 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(ss)"); |
4387 | if (r < 0) | |
4388 | return bus_log_parse_error(r); | |
ebf57b80 | 4389 | |
f459b602 | 4390 | while ((r = sd_bus_message_read(m, "(ss)", &type, &path)) > 0) |
4f9a9105 | 4391 | print_prop(type, "%s", path); |
f459b602 MAP |
4392 | if (r < 0) |
4393 | return bus_log_parse_error(r); | |
ebf57b80 | 4394 | |
f459b602 MAP |
4395 | r = sd_bus_message_exit_container(m); |
4396 | if (r < 0) | |
4397 | return bus_log_parse_error(r); | |
ebf57b80 | 4398 | |
707e5e52 | 4399 | return 0; |
582a507f | 4400 | |
f459b602 MAP |
4401 | } else if (contents[1] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "Listen")) { |
4402 | const char *type, *path; | |
67419600 | 4403 | |
f459b602 MAP |
4404 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(ss)"); |
4405 | if (r < 0) | |
4406 | return bus_log_parse_error(r); | |
67419600 | 4407 | |
f459b602 | 4408 | while ((r = sd_bus_message_read(m, "(ss)", &type, &path)) > 0) |
4f9a9105 ZJS |
4409 | if (arg_value) |
4410 | puts(path); | |
4411 | else | |
4412 | printf("Listen%s=%s\n", type, path); | |
f459b602 MAP |
4413 | if (r < 0) |
4414 | return bus_log_parse_error(r); | |
67419600 | 4415 | |
f459b602 MAP |
4416 | r = sd_bus_message_exit_container(m); |
4417 | if (r < 0) | |
4418 | return bus_log_parse_error(r); | |
67419600 OS |
4419 | |
4420 | return 0; | |
4421 | ||
f459b602 MAP |
4422 | } else if (contents[1] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "Timers")) { |
4423 | const char *base; | |
4424 | uint64_t value, next_elapse; | |
707e5e52 | 4425 | |
f459b602 MAP |
4426 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(stt)"); |
4427 | if (r < 0) | |
4428 | return bus_log_parse_error(r); | |
552e4331 | 4429 | |
f459b602 MAP |
4430 | while ((r = sd_bus_message_read(m, "(stt)", &base, &value, &next_elapse)) > 0) { |
4431 | char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX]; | |
fe68089d | 4432 | |
4f9a9105 ZJS |
4433 | print_prop(base, "{ value=%s ; next_elapse=%s }", |
4434 | format_timespan(timespan1, sizeof(timespan1), value, 0), | |
4435 | format_timespan(timespan2, sizeof(timespan2), next_elapse, 0)); | |
fe68089d | 4436 | } |
f459b602 MAP |
4437 | if (r < 0) |
4438 | return bus_log_parse_error(r); | |
4439 | ||
4440 | r = sd_bus_message_exit_container(m); | |
4441 | if (r < 0) | |
4442 | return bus_log_parse_error(r); | |
fe68089d LP |
4443 | |
4444 | return 0; | |
fe68089d | 4445 | |
f459b602 MAP |
4446 | } else if (contents[1] == SD_BUS_TYPE_STRUCT_BEGIN && startswith(name, "Exec")) { |
4447 | ExecStatusInfo info = {}; | |
4448 | ||
4449 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sasbttttuii)"); | |
4450 | if (r < 0) | |
4451 | return bus_log_parse_error(r); | |
4452 | ||
4453 | while ((r = exec_status_info_deserialize(m, &info)) > 0) { | |
4454 | char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX]; | |
4455 | _cleanup_free_ char *tt; | |
4456 | ||
4457 | tt = strv_join(info.argv, " "); | |
4458 | ||
4f9a9105 ZJS |
4459 | print_prop(name, |
4460 | "{ path=%s ; argv[]=%s ; ignore_errors=%s ; start_time=[%s] ; stop_time=[%s] ; pid="PID_FMT" ; code=%s ; status=%i%s%s }", | |
4461 | strna(info.path), | |
4462 | strna(tt), | |
4463 | yes_no(info.ignore), | |
4464 | strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)), | |
4465 | strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)), | |
4466 | info.pid, | |
4467 | sigchld_code_to_string(info.code), | |
4468 | info.status, | |
4469 | info.code == CLD_EXITED ? "" : "/", | |
4470 | strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status))); | |
fe68089d | 4471 | |
582a507f LP |
4472 | free(info.path); |
4473 | strv_free(info.argv); | |
f459b602 | 4474 | zero(info); |
707e5e52 LP |
4475 | } |
4476 | ||
f459b602 MAP |
4477 | r = sd_bus_message_exit_container(m); |
4478 | if (r < 0) | |
4479 | return bus_log_parse_error(r); | |
4480 | ||
48220598 | 4481 | return 0; |
4ad49000 | 4482 | |
f459b602 MAP |
4483 | } else if (contents[1] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "DeviceAllow")) { |
4484 | const char *path, *rwm; | |
4ad49000 | 4485 | |
f459b602 MAP |
4486 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(ss)"); |
4487 | if (r < 0) | |
4488 | return bus_log_parse_error(r); | |
4ad49000 | 4489 | |
f459b602 | 4490 | while ((r = sd_bus_message_read(m, "(ss)", &path, &rwm)) > 0) |
4f9a9105 | 4491 | print_prop(name, "%s %s", strna(path), strna(rwm)); |
f459b602 MAP |
4492 | if (r < 0) |
4493 | return bus_log_parse_error(r); | |
4ad49000 | 4494 | |
f459b602 MAP |
4495 | r = sd_bus_message_exit_container(m); |
4496 | if (r < 0) | |
4497 | return bus_log_parse_error(r); | |
4ad49000 | 4498 | |
4ad49000 LP |
4499 | return 0; |
4500 | ||
13c31542 | 4501 | } else if (contents[1] == SD_BUS_TYPE_STRUCT_BEGIN && (streq(name, "IODeviceWeight") || streq(name, "BlockIODeviceWeight"))) { |
f459b602 MAP |
4502 | const char *path; |
4503 | uint64_t weight; | |
b8ab2dc6 | 4504 | |
f459b602 MAP |
4505 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(st)"); |
4506 | if (r < 0) | |
4507 | return bus_log_parse_error(r); | |
b8ab2dc6 | 4508 | |
f459b602 | 4509 | while ((r = sd_bus_message_read(m, "(st)", &path, &weight)) > 0) |
4f9a9105 | 4510 | print_prop(name, "%s %"PRIu64, strna(path), weight); |
f459b602 MAP |
4511 | if (r < 0) |
4512 | return bus_log_parse_error(r); | |
b8ab2dc6 | 4513 | |
f459b602 MAP |
4514 | r = sd_bus_message_exit_container(m); |
4515 | if (r < 0) | |
4516 | return bus_log_parse_error(r); | |
b8ab2dc6 | 4517 | |
b8ab2dc6 G |
4518 | return 0; |
4519 | ||
9be57249 | 4520 | } else if (contents[1] == SD_BUS_TYPE_STRUCT_BEGIN && (cgroup_io_limit_type_from_string(name) >= 0 || |
13c31542 | 4521 | streq(name, "BlockIOReadBandwidth") || streq(name, "BlockIOWriteBandwidth"))) { |
f459b602 MAP |
4522 | const char *path; |
4523 | uint64_t bandwidth; | |
4ad49000 | 4524 | |
f459b602 MAP |
4525 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(st)"); |
4526 | if (r < 0) | |
4527 | return bus_log_parse_error(r); | |
4ad49000 | 4528 | |
f459b602 | 4529 | while ((r = sd_bus_message_read(m, "(st)", &path, &bandwidth)) > 0) |
4f9a9105 | 4530 | print_prop(name, "%s %"PRIu64, strna(path), bandwidth); |
f459b602 MAP |
4531 | if (r < 0) |
4532 | return bus_log_parse_error(r); | |
4ad49000 | 4533 | |
f459b602 MAP |
4534 | r = sd_bus_message_exit_container(m); |
4535 | if (r < 0) | |
4536 | return bus_log_parse_error(r); | |
4ad49000 | 4537 | |
4ad49000 | 4538 | return 0; |
48220598 LP |
4539 | } |
4540 | ||
4541 | break; | |
4542 | } | |
4543 | ||
4f9a9105 | 4544 | r = bus_print_property(name, m, arg_value, arg_all); |
f459b602 MAP |
4545 | if (r < 0) |
4546 | return bus_log_parse_error(r); | |
4547 | ||
4548 | if (r == 0) { | |
4549 | r = sd_bus_message_skip(m, contents); | |
4550 | if (r < 0) | |
4551 | return bus_log_parse_error(r); | |
a4c279f8 | 4552 | |
f459b602 MAP |
4553 | if (arg_all) |
4554 | printf("%s=[unprintable]\n", name); | |
4555 | } | |
48220598 LP |
4556 | |
4557 | return 0; | |
4558 | } | |
4559 | ||
f459b602 MAP |
4560 | static int show_one( |
4561 | const char *verb, | |
4562 | sd_bus *bus, | |
4563 | const char *path, | |
3dced37b | 4564 | const char *unit, |
f459b602 MAP |
4565 | bool show_properties, |
4566 | bool *new_line, | |
4567 | bool *ellipsized) { | |
4568 | ||
3dced37b LP |
4569 | static const struct bus_properties_map property_map[] = { |
4570 | { "LoadState", "s", NULL, offsetof(UnitStatusInfo, load_state) }, | |
4571 | { "ActiveState", "s", NULL, offsetof(UnitStatusInfo, active_state) }, | |
4572 | {} | |
4573 | }; | |
4574 | ||
4afd3348 LP |
4575 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
4576 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
3dced37b | 4577 | _cleanup_set_free_ Set *found_properties = NULL; |
934277fe LP |
4578 | UnitStatusInfo info = { |
4579 | .memory_current = (uint64_t) -1, | |
da4d897e TH |
4580 | .memory_high = CGROUP_LIMIT_MAX, |
4581 | .memory_max = CGROUP_LIMIT_MAX, | |
934277fe | 4582 | .memory_limit = (uint64_t) -1, |
5ad096b3 | 4583 | .cpu_usage_nsec = (uint64_t) -1, |
03a7b521 LP |
4584 | .tasks_current = (uint64_t) -1, |
4585 | .tasks_max = (uint64_t) -1, | |
934277fe | 4586 | }; |
582a507f | 4587 | ExecStatusInfo *p; |
f459b602 | 4588 | int r; |
48220598 | 4589 | |
48220598 | 4590 | assert(path); |
61cbdc4b | 4591 | assert(new_line); |
48220598 | 4592 | |
e3e0314b ZJS |
4593 | log_debug("Showing one %s", path); |
4594 | ||
f459b602 | 4595 | r = sd_bus_call_method( |
f22f08cd SP |
4596 | bus, |
4597 | "org.freedesktop.systemd1", | |
4598 | path, | |
4599 | "org.freedesktop.DBus.Properties", | |
4600 | "GetAll", | |
f459b602 | 4601 | &error, |
f22f08cd | 4602 | &reply, |
f459b602 | 4603 | "s", ""); |
4c3e8e39 LP |
4604 | if (r < 0) |
4605 | return log_error_errno(r, "Failed to get properties: %s", bus_error_message(&error, r)); | |
48220598 | 4606 | |
3dced37b LP |
4607 | if (unit) { |
4608 | r = bus_message_map_all_properties(reply, property_map, &info); | |
4609 | if (r < 0) | |
4610 | return log_error_errno(r, "Failed to map properties: %s", bus_error_message(&error, r)); | |
e33a06a1 | 4611 | |
3dced37b LP |
4612 | if (streq_ptr(info.load_state, "not-found") && streq_ptr(info.active_state, "inactive")) { |
4613 | log_error("Unit %s could not be found.", unit); | |
e33a06a1 | 4614 | |
3dced37b LP |
4615 | if (streq(verb, "status")) |
4616 | return EXIT_PROGRAM_OR_SERVICES_STATUS_UNKNOWN; | |
4617 | ||
4618 | return -ENOENT; | |
4619 | } | |
4620 | ||
4621 | r = sd_bus_message_rewind(reply, true); | |
4622 | if (r < 0) | |
4623 | return log_error_errno(r, "Failed to rewind: %s", bus_error_message(&error, r)); | |
4624 | } | |
e33a06a1 | 4625 | |
f459b602 MAP |
4626 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "{sv}"); |
4627 | if (r < 0) | |
4628 | return bus_log_parse_error(r); | |
48220598 | 4629 | |
61cbdc4b LP |
4630 | if (*new_line) |
4631 | printf("\n"); | |
4632 | ||
4633 | *new_line = true; | |
4634 | ||
f459b602 MAP |
4635 | while ((r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) { |
4636 | const char *name, *contents; | |
0183528f | 4637 | |
f459b602 MAP |
4638 | r = sd_bus_message_read(reply, "s", &name); |
4639 | if (r < 0) | |
4640 | return bus_log_parse_error(r); | |
48220598 | 4641 | |
f459b602 MAP |
4642 | r = sd_bus_message_peek_type(reply, NULL, &contents); |
4643 | if (r < 0) | |
4644 | return bus_log_parse_error(r); | |
4645 | ||
4646 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_VARIANT, contents); | |
4647 | if (r < 0) | |
4648 | return bus_log_parse_error(r); | |
48220598 | 4649 | |
3dced37b LP |
4650 | if (show_properties) { |
4651 | r = set_ensure_allocated(&found_properties, &string_hash_ops); | |
4652 | if (r < 0) | |
4653 | return log_oom(); | |
4654 | ||
4655 | r = set_put(found_properties, name); | |
4656 | if (r < 0 && r != EEXIST) | |
4657 | return log_oom(); | |
4658 | ||
f459b602 | 4659 | r = print_property(name, reply, contents); |
3dced37b | 4660 | } else |
f459b602 MAP |
4661 | r = status_property(name, reply, &info, contents); |
4662 | if (r < 0) | |
4663 | return r; | |
48220598 | 4664 | |
f459b602 MAP |
4665 | r = sd_bus_message_exit_container(reply); |
4666 | if (r < 0) | |
4667 | return bus_log_parse_error(r); | |
4668 | ||
4669 | r = sd_bus_message_exit_container(reply); | |
4670 | if (r < 0) | |
4671 | return bus_log_parse_error(r); | |
48220598 | 4672 | } |
f459b602 MAP |
4673 | if (r < 0) |
4674 | return bus_log_parse_error(r); | |
4675 | ||
4676 | r = sd_bus_message_exit_container(reply); | |
4677 | if (r < 0) | |
4678 | return bus_log_parse_error(r); | |
48220598 | 4679 | |
f1e36d67 LP |
4680 | r = 0; |
4681 | ||
3dced37b LP |
4682 | if (show_properties) { |
4683 | char **pp; | |
4684 | ||
4685 | STRV_FOREACH(pp, arg_properties) { | |
4686 | if (!set_contains(found_properties, *pp)) { | |
4687 | log_warning("Property %s does not exist.", *pp); | |
4688 | r = -ENXIO; | |
4689 | } | |
4690 | } | |
4691 | } else if (streq(verb, "help")) | |
4692 | show_unit_help(&info); | |
4693 | else if (streq(verb, "status")) { | |
4694 | print_status_info(bus, &info, ellipsized); | |
4695 | ||
4696 | if (info.active_state && STR_IN_SET(info.active_state, "inactive", "failed")) | |
4697 | r = EXIT_PROGRAM_NOT_RUNNING; | |
256425cc | 4698 | else |
3dced37b | 4699 | r = EXIT_PROGRAM_RUNNING_OR_SERVICE_OK; |
256425cc | 4700 | } |
f1e36d67 | 4701 | |
49dbfa7b | 4702 | strv_free(info.documentation); |
76d14b87 | 4703 | strv_free(info.dropin_paths); |
67419600 | 4704 | strv_free(info.listen); |
49dbfa7b | 4705 | |
582a507f | 4706 | while ((p = info.exec)) { |
71fda00f | 4707 | LIST_REMOVE(exec, info.exec, p); |
582a507f LP |
4708 | exec_status_info_free(p); |
4709 | } | |
4710 | ||
48220598 LP |
4711 | return r; |
4712 | } | |
4713 | ||
f74294c1 | 4714 | static int get_unit_dbus_path_by_pid( |
f459b602 MAP |
4715 | sd_bus *bus, |
4716 | uint32_t pid, | |
f74294c1 | 4717 | char **unit) { |
f459b602 | 4718 | |
4afd3348 LP |
4719 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4720 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
373d32c9 | 4721 | char *u; |
a223b325 MS |
4722 | int r; |
4723 | ||
f459b602 | 4724 | r = sd_bus_call_method( |
f22f08cd SP |
4725 | bus, |
4726 | "org.freedesktop.systemd1", | |
4727 | "/org/freedesktop/systemd1", | |
4728 | "org.freedesktop.systemd1.Manager", | |
4729 | "GetUnitByPID", | |
f459b602 | 4730 | &error, |
f22f08cd | 4731 | &reply, |
f459b602 | 4732 | "u", pid); |
691395d8 LP |
4733 | if (r < 0) |
4734 | return log_error_errno(r, "Failed to get unit for PID %"PRIu32": %s", pid, bus_error_message(&error, r)); | |
a223b325 | 4735 | |
373d32c9 | 4736 | r = sd_bus_message_read(reply, "o", &u); |
f459b602 MAP |
4737 | if (r < 0) |
4738 | return bus_log_parse_error(r); | |
4739 | ||
373d32c9 LP |
4740 | u = strdup(u); |
4741 | if (!u) | |
4742 | return log_oom(); | |
4743 | ||
4744 | *unit = u; | |
f74294c1 | 4745 | return 0; |
a223b325 MS |
4746 | } |
4747 | ||
f459b602 MAP |
4748 | static int show_all( |
4749 | const char* verb, | |
4750 | sd_bus *bus, | |
4751 | bool show_properties, | |
4752 | bool *new_line, | |
4753 | bool *ellipsized) { | |
4754 | ||
4afd3348 | 4755 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
f459b602 MAP |
4756 | _cleanup_free_ UnitInfo *unit_infos = NULL; |
4757 | const UnitInfo *u; | |
4758 | unsigned c; | |
5bb75bc7 | 4759 | int r, ret = 0; |
265a7a2a | 4760 | |
1238ee09 | 4761 | r = get_unit_list(bus, NULL, NULL, &unit_infos, 0, &reply); |
265a7a2a ZJS |
4762 | if (r < 0) |
4763 | return r; | |
4764 | ||
ea4b98e6 | 4765 | pager_open(arg_no_pager, false); |
dbed408b | 4766 | |
f459b602 MAP |
4767 | c = (unsigned) r; |
4768 | ||
4769 | qsort_safe(unit_infos, c, sizeof(UnitInfo), compare_unit_info); | |
991f2a39 | 4770 | |
265a7a2a | 4771 | for (u = unit_infos; u < unit_infos + c; u++) { |
7fd1b19b | 4772 | _cleanup_free_ char *p = NULL; |
265a7a2a | 4773 | |
265a7a2a ZJS |
4774 | p = unit_dbus_path_from_name(u->id); |
4775 | if (!p) | |
4776 | return log_oom(); | |
4777 | ||
3dced37b | 4778 | r = show_one(verb, bus, p, u->id, show_properties, new_line, ellipsized); |
3df538da | 4779 | if (r < 0) |
265a7a2a | 4780 | return r; |
5bb75bc7 ZJS |
4781 | else if (r > 0 && ret == 0) |
4782 | ret = r; | |
265a7a2a ZJS |
4783 | } |
4784 | ||
5bb75bc7 | 4785 | return ret; |
265a7a2a ZJS |
4786 | } |
4787 | ||
8fcf784d LP |
4788 | static int show_system_status(sd_bus *bus) { |
4789 | char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], since2[FORMAT_TIMESTAMP_MAX]; | |
4790 | _cleanup_free_ char *hn = NULL; | |
e7e55dbd | 4791 | _cleanup_(machine_info_clear) struct machine_info mi = {}; |
8fcf784d LP |
4792 | const char *on, *off; |
4793 | int r; | |
4794 | ||
4795 | hn = gethostname_malloc(); | |
4796 | if (!hn) | |
4797 | return log_oom(); | |
4798 | ||
4799 | r = bus_map_all_properties(bus, "org.freedesktop.systemd1", "/org/freedesktop/systemd1", machine_info_property_map, &mi); | |
f647962d MS |
4800 | if (r < 0) |
4801 | return log_error_errno(r, "Failed to read server status: %m"); | |
8fcf784d | 4802 | |
8fcf784d LP |
4803 | if (streq_ptr(mi.state, "degraded")) { |
4804 | on = ansi_highlight_red(); | |
1fc464f6 | 4805 | off = ansi_normal(); |
8fcf784d LP |
4806 | } else if (!streq_ptr(mi.state, "running")) { |
4807 | on = ansi_highlight_yellow(); | |
1fc464f6 | 4808 | off = ansi_normal(); |
8fcf784d LP |
4809 | } else |
4810 | on = off = ""; | |
4811 | ||
323b7dc9 | 4812 | printf("%s%s%s %s\n", on, special_glyph(BLACK_CIRCLE), off, arg_host ? arg_host : hn); |
b0d14c69 | 4813 | |
8fcf784d LP |
4814 | printf(" State: %s%s%s\n", |
4815 | on, strna(mi.state), off); | |
4816 | ||
c6ba5b80 MP |
4817 | printf(" Jobs: %" PRIu32 " queued\n", mi.n_jobs); |
4818 | printf(" Failed: %" PRIu32 " units\n", mi.n_failed_units); | |
8fcf784d LP |
4819 | |
4820 | printf(" Since: %s; %s\n", | |
4821 | format_timestamp(since2, sizeof(since2), mi.timestamp), | |
4822 | format_timestamp_relative(since1, sizeof(since1), mi.timestamp)); | |
4823 | ||
4824 | printf(" CGroup: %s\n", mi.control_group ?: "/"); | |
4c315c2c IS |
4825 | if (IN_SET(arg_transport, |
4826 | BUS_TRANSPORT_LOCAL, | |
4827 | BUS_TRANSPORT_MACHINE)) { | |
8fcf784d LP |
4828 | static const char prefix[] = " "; |
4829 | unsigned c; | |
4830 | ||
4831 | c = columns(); | |
4832 | if (c > sizeof(prefix) - 1) | |
4833 | c -= sizeof(prefix) - 1; | |
4834 | else | |
4835 | c = 0; | |
4836 | ||
0ff308c8 | 4837 | show_cgroup(SYSTEMD_CGROUP_CONTROLLER, strempty(mi.control_group), prefix, c, get_output_flags()); |
8fcf784d LP |
4838 | } |
4839 | ||
8fcf784d LP |
4840 | return 0; |
4841 | } | |
4842 | ||
e449de87 LP |
4843 | static int show(int argc, char *argv[], void *userdata) { |
4844 | bool show_properties, show_status, show_help, new_line = false; | |
94e0bd7d | 4845 | bool ellipsized = false; |
e3e0314b | 4846 | int r, ret = 0; |
4fbd7192 | 4847 | sd_bus *bus; |
48220598 | 4848 | |
e449de87 | 4849 | assert(argv); |
48220598 | 4850 | |
e449de87 LP |
4851 | show_properties = streq(argv[0], "show"); |
4852 | show_status = streq(argv[0], "status"); | |
4853 | show_help = streq(argv[0], "help"); | |
4854 | ||
4855 | if (show_help && argc <= 1) { | |
4856 | log_error("This command expects one or more unit names. Did you mean --help?"); | |
4857 | return -EINVAL; | |
4858 | } | |
61cbdc4b | 4859 | |
d2ad7e1b ZJS |
4860 | r = acquire_bus(BUS_MANAGER, &bus); |
4861 | if (r < 0) | |
4862 | return r; | |
4863 | ||
ea4b98e6 | 4864 | pager_open(arg_no_pager, false); |
ec14911e | 4865 | |
40acc203 ZJS |
4866 | if (show_status) |
4867 | /* Increase max number of open files to 16K if we can, we | |
4868 | * might needs this when browsing journal files, which might | |
4869 | * be split up into many files. */ | |
4870 | setrlimit_closest(RLIMIT_NOFILE, &RLIMIT_MAKE_CONST(16384)); | |
4871 | ||
4fbd7192 | 4872 | /* If no argument is specified inspect the manager itself */ |
e449de87 | 4873 | if (show_properties && argc <= 1) |
3dced37b | 4874 | return show_one(argv[0], bus, "/org/freedesktop/systemd1", NULL, show_properties, &new_line, &ellipsized); |
48220598 | 4875 | |
e449de87 | 4876 | if (show_status && argc <= 1) { |
8fcf784d | 4877 | |
8fcf784d LP |
4878 | show_system_status(bus); |
4879 | new_line = true; | |
4880 | ||
4881 | if (arg_all) | |
e449de87 | 4882 | ret = show_all(argv[0], bus, false, &new_line, &ellipsized); |
8fcf784d | 4883 | } else { |
e3e0314b ZJS |
4884 | _cleanup_free_ char **patterns = NULL; |
4885 | char **name; | |
4886 | ||
e449de87 | 4887 | STRV_FOREACH(name, strv_skip(argv, 1)) { |
3dced37b | 4888 | _cleanup_free_ char *path = NULL, *unit = NULL; |
94e0bd7d | 4889 | uint32_t id; |
48220598 | 4890 | |
94e0bd7d | 4891 | if (safe_atou32(*name, &id) < 0) { |
e3e0314b | 4892 | if (strv_push(&patterns, *name) < 0) |
94e0bd7d | 4893 | return log_oom(); |
48220598 | 4894 | |
e3e0314b | 4895 | continue; |
94e0bd7d | 4896 | } else if (show_properties) { |
94e0bd7d | 4897 | /* Interpret as job id */ |
3dced37b | 4898 | if (asprintf(&path, "/org/freedesktop/systemd1/job/%u", id) < 0) |
94e0bd7d | 4899 | return log_oom(); |
48220598 | 4900 | |
94e0bd7d ZJS |
4901 | } else { |
4902 | /* Interpret as PID */ | |
3dced37b | 4903 | r = get_unit_dbus_path_by_pid(bus, id, &path); |
373d32c9 | 4904 | if (r < 0) { |
94e0bd7d | 4905 | ret = r; |
373d32c9 LP |
4906 | continue; |
4907 | } | |
3dced37b LP |
4908 | |
4909 | r = unit_name_from_dbus_path(path, &unit); | |
4910 | if (r < 0) | |
4911 | return log_oom(); | |
94e0bd7d | 4912 | } |
f74294c1 | 4913 | |
3dced37b | 4914 | r = show_one(argv[0], bus, path, unit, show_properties, &new_line, &ellipsized); |
5bb75bc7 ZJS |
4915 | if (r < 0) |
4916 | return r; | |
4917 | else if (r > 0 && ret == 0) | |
4918 | ret = r; | |
48220598 | 4919 | } |
94e0bd7d | 4920 | |
e3e0314b ZJS |
4921 | if (!strv_isempty(patterns)) { |
4922 | _cleanup_strv_free_ char **names = NULL; | |
4923 | ||
4924 | r = expand_names(bus, patterns, NULL, &names); | |
4925 | if (r < 0) | |
691395d8 | 4926 | return log_error_errno(r, "Failed to expand names: %m"); |
e3e0314b ZJS |
4927 | |
4928 | STRV_FOREACH(name, names) { | |
3dced37b | 4929 | _cleanup_free_ char *path; |
e3e0314b | 4930 | |
3dced37b LP |
4931 | path = unit_dbus_path_from_name(*name); |
4932 | if (!path) | |
e3e0314b ZJS |
4933 | return log_oom(); |
4934 | ||
3dced37b | 4935 | r = show_one(argv[0], bus, path, *name, show_properties, &new_line, &ellipsized); |
5bb75bc7 ZJS |
4936 | if (r < 0) |
4937 | return r; | |
3dced37b | 4938 | if (r > 0 && ret == 0) |
5bb75bc7 | 4939 | ret = r; |
e3e0314b ZJS |
4940 | } |
4941 | } | |
4942 | } | |
4943 | ||
94e0bd7d ZJS |
4944 | if (ellipsized && !arg_quiet) |
4945 | printf("Hint: Some lines were ellipsized, use -l to show in full.\n"); | |
48220598 | 4946 | |
22f4096c | 4947 | return ret; |
0183528f LP |
4948 | } |
4949 | ||
8527b07b ZJS |
4950 | static int cat_file(const char *filename, bool newline) { |
4951 | _cleanup_close_ int fd; | |
4952 | ||
4953 | fd = open(filename, O_RDONLY|O_CLOEXEC|O_NOCTTY); | |
4954 | if (fd < 0) | |
4955 | return -errno; | |
4956 | ||
4957 | printf("%s%s# %s%s\n", | |
4958 | newline ? "\n" : "", | |
4959 | ansi_highlight_blue(), | |
4960 | filename, | |
1fc464f6 | 4961 | ansi_normal()); |
8527b07b ZJS |
4962 | fflush(stdout); |
4963 | ||
59f448cf | 4964 | return copy_bytes(fd, STDOUT_FILENO, (uint64_t) -1, false); |
8527b07b ZJS |
4965 | } |
4966 | ||
e449de87 | 4967 | static int cat(int argc, char *argv[], void *userdata) { |
ad2a0358 | 4968 | _cleanup_lookup_paths_free_ LookupPaths lp = {}; |
15ef1144 LP |
4969 | _cleanup_strv_free_ char **names = NULL; |
4970 | char **name; | |
4fbd7192 LP |
4971 | sd_bus *bus; |
4972 | bool first = true; | |
25586912 | 4973 | int r; |
15ef1144 | 4974 | |
3e7eed84 | 4975 | if (arg_transport != BUS_TRANSPORT_LOCAL) { |
4fbd7192 | 4976 | log_error("Cannot remotely cat units."); |
3e495a66 ZJS |
4977 | return -EINVAL; |
4978 | } | |
4979 | ||
4943d143 | 4980 | r = lookup_paths_init(&lp, arg_scope, 0, arg_root); |
ad2a0358 | 4981 | if (r < 0) |
c51932be | 4982 | return log_error_errno(r, "Failed to determine unit paths: %m"); |
ad2a0358 | 4983 | |
4fbd7192 | 4984 | r = acquire_bus(BUS_MANAGER, &bus); |
15ef1144 | 4985 | if (r < 0) |
4fbd7192 | 4986 | return r; |
15ef1144 | 4987 | |
e449de87 | 4988 | r = expand_names(bus, strv_skip(argv, 1), NULL, &names); |
4fbd7192 LP |
4989 | if (r < 0) |
4990 | return log_error_errno(r, "Failed to expand names: %m"); | |
ad2a0358 | 4991 | |
ea4b98e6 | 4992 | pager_open(arg_no_pager, false); |
15ef1144 LP |
4993 | |
4994 | STRV_FOREACH(name, names) { | |
ad2a0358 | 4995 | _cleanup_free_ char *fragment_path = NULL; |
15ef1144 | 4996 | _cleanup_strv_free_ char **dropin_paths = NULL; |
15ef1144 LP |
4997 | char **path; |
4998 | ||
4fbd7192 | 4999 | r = unit_find_paths(bus, *name, &lp, &fragment_path, &dropin_paths); |
ad2a0358 ZJS |
5000 | if (r < 0) |
5001 | return r; | |
b5e6a600 IS |
5002 | else if (r == 0) |
5003 | return -ENOENT; | |
15ef1144 LP |
5004 | |
5005 | if (first) | |
5006 | first = false; | |
5007 | else | |
5008 | puts(""); | |
5009 | ||
ad2a0358 | 5010 | if (fragment_path) { |
8527b07b ZJS |
5011 | r = cat_file(fragment_path, false); |
5012 | if (r < 0) | |
5013 | return log_warning_errno(r, "Failed to cat %s: %m", fragment_path); | |
15ef1144 LP |
5014 | } |
5015 | ||
5016 | STRV_FOREACH(path, dropin_paths) { | |
8527b07b ZJS |
5017 | r = cat_file(*path, path == dropin_paths); |
5018 | if (r < 0) | |
5019 | return log_warning_errno(r, "Failed to cat %s: %m", *path); | |
15ef1144 LP |
5020 | } |
5021 | } | |
5022 | ||
25586912 | 5023 | return 0; |
15ef1144 LP |
5024 | } |
5025 | ||
e449de87 | 5026 | static int set_property(int argc, char *argv[], void *userdata) { |
4afd3348 LP |
5027 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
5028 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
68372da6 | 5029 | _cleanup_free_ char *n = NULL; |
4fbd7192 | 5030 | sd_bus *bus; |
8e2af478 LP |
5031 | char **i; |
5032 | int r; | |
5033 | ||
4fbd7192 LP |
5034 | r = acquire_bus(BUS_MANAGER, &bus); |
5035 | if (r < 0) | |
5036 | return r; | |
5037 | ||
d2ad7e1b ZJS |
5038 | polkit_agent_open_if_enabled(); |
5039 | ||
f459b602 MAP |
5040 | r = sd_bus_message_new_method_call( |
5041 | bus, | |
151b9b96 | 5042 | &m, |
8e2af478 LP |
5043 | "org.freedesktop.systemd1", |
5044 | "/org/freedesktop/systemd1", | |
5045 | "org.freedesktop.systemd1.Manager", | |
151b9b96 | 5046 | "SetUnitProperties"); |
f459b602 MAP |
5047 | if (r < 0) |
5048 | return bus_log_create_error(r); | |
8e2af478 | 5049 | |
e449de87 | 5050 | r = unit_name_mangle(argv[1], UNIT_NAME_NOGLOB, &n); |
7410616c LP |
5051 | if (r < 0) |
5052 | return log_error_errno(r, "Failed to mangle unit name: %m"); | |
68372da6 | 5053 | |
f459b602 MAP |
5054 | r = sd_bus_message_append(m, "sb", n, arg_runtime); |
5055 | if (r < 0) | |
5056 | return bus_log_create_error(r); | |
8e2af478 | 5057 | |
f459b602 MAP |
5058 | r = sd_bus_message_open_container(m, SD_BUS_TYPE_ARRAY, "(sv)"); |
5059 | if (r < 0) | |
5060 | return bus_log_create_error(r); | |
8e2af478 | 5061 | |
e449de87 | 5062 | STRV_FOREACH(i, strv_skip(argv, 2)) { |
df31a6c0 | 5063 | r = bus_append_unit_property_assignment(m, *i); |
8e2af478 LP |
5064 | if (r < 0) |
5065 | return r; | |
8e2af478 LP |
5066 | } |
5067 | ||
f459b602 MAP |
5068 | r = sd_bus_message_close_container(m); |
5069 | if (r < 0) | |
5070 | return bus_log_create_error(r); | |
8e2af478 | 5071 | |
c49b30a2 | 5072 | r = sd_bus_call(bus, m, 0, &error, NULL); |
691395d8 LP |
5073 | if (r < 0) |
5074 | return log_error_errno(r, "Failed to set unit properties on %s: %s", n, bus_error_message(&error, r)); | |
8e2af478 LP |
5075 | |
5076 | return 0; | |
5077 | } | |
5078 | ||
e449de87 | 5079 | static int daemon_reload(int argc, char *argv[], void *userdata) { |
4afd3348 | 5080 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
2853b60a | 5081 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
7e4249b9 | 5082 | const char *method; |
4fbd7192 | 5083 | sd_bus *bus; |
f459b602 | 5084 | int r; |
7e4249b9 | 5085 | |
4fbd7192 LP |
5086 | r = acquire_bus(BUS_MANAGER, &bus); |
5087 | if (r < 0) | |
5088 | return r; | |
5089 | ||
d2ad7e1b ZJS |
5090 | polkit_agent_open_if_enabled(); |
5091 | ||
2853b60a LP |
5092 | switch (arg_action) { |
5093 | ||
5094 | case ACTION_RELOAD: | |
e4b61340 | 5095 | method = "Reload"; |
2853b60a LP |
5096 | break; |
5097 | ||
5098 | case ACTION_REEXEC: | |
e4b61340 | 5099 | method = "Reexecute"; |
2853b60a LP |
5100 | break; |
5101 | ||
5102 | case ACTION_SYSTEMCTL: | |
5103 | method = streq(argv[0], "daemon-reexec") ? "Reexecute" : | |
5104 | /* "daemon-reload" */ "Reload"; | |
5105 | break; | |
e4b61340 | 5106 | |
2853b60a LP |
5107 | default: |
5108 | assert_not_reached("Unexpected action"); | |
e4b61340 | 5109 | } |
7e4249b9 | 5110 | |
2853b60a LP |
5111 | r = sd_bus_message_new_method_call( |
5112 | bus, | |
5113 | &m, | |
5114 | "org.freedesktop.systemd1", | |
5115 | "/org/freedesktop/systemd1", | |
5116 | "org.freedesktop.systemd1.Manager", | |
5117 | method); | |
5118 | if (r < 0) | |
5119 | return bus_log_create_error(r); | |
5120 | ||
5121 | /* Note we use an extra-long timeout here. This is because a reload or reexec means generators are rerun which | |
5122 | * are timed out after DEFAULT_TIMEOUT_USEC. Let's use twice that time here, so that the generators can have | |
5123 | * their timeout, and for everything else there's the same time budget in place. */ | |
5124 | ||
5125 | r = sd_bus_call(bus, m, DEFAULT_TIMEOUT_USEC * 2, &error, NULL); | |
5126 | ||
5127 | /* On reexecution, we expect a disconnect, not a reply */ | |
5128 | if (IN_SET(r, -ETIMEDOUT, -ECONNRESET) && streq(method, "Reexecute")) | |
5129 | r = 0; | |
5130 | ||
5131 | if (r < 0 && arg_action == ACTION_SYSTEMCTL) | |
5132 | return log_error_errno(r, "Failed to reload daemon: %s", bus_error_message(&error, r)); | |
5133 | ||
5134 | /* Note that for the legacy commands (i.e. those with action != ACTION_SYSTEMCTL) we support fallbacks to the | |
5135 | * old ways of doing things, hence don't log any error in that case here. */ | |
5136 | ||
5137 | return r < 0 ? r : 0; | |
5138 | } | |
5139 | ||
5140 | static int trivial_method(int argc, char *argv[], void *userdata) { | |
5141 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
5142 | const char *method; | |
5143 | sd_bus *bus; | |
5144 | int r; | |
5145 | ||
2853b60a LP |
5146 | r = acquire_bus(BUS_MANAGER, &bus); |
5147 | if (r < 0) | |
5148 | return r; | |
5149 | ||
d2ad7e1b ZJS |
5150 | polkit_agent_open_if_enabled(); |
5151 | ||
2853b60a LP |
5152 | method = |
5153 | streq(argv[0], "clear-jobs") || | |
5154 | streq(argv[0], "cancel") ? "ClearJobs" : | |
5155 | streq(argv[0], "reset-failed") ? "ResetFailed" : | |
5156 | streq(argv[0], "halt") ? "Halt" : | |
5157 | streq(argv[0], "reboot") ? "Reboot" : | |
5158 | streq(argv[0], "kexec") ? "KExec" : | |
5159 | streq(argv[0], "exit") ? "Exit" : | |
5160 | /* poweroff */ "PowerOff"; | |
5161 | ||
6e646d22 | 5162 | r = sd_bus_call_method( |
f22f08cd SP |
5163 | bus, |
5164 | "org.freedesktop.systemd1", | |
5165 | "/org/freedesktop/systemd1", | |
5166 | "org.freedesktop.systemd1.Manager", | |
6e646d22 LP |
5167 | method, |
5168 | &error, | |
5169 | NULL, | |
5170 | NULL); | |
2853b60a LP |
5171 | if (r < 0 && arg_action == ACTION_SYSTEMCTL) |
5172 | return log_error_errno(r, "Failed to execute operation: %s", bus_error_message(&error, r)); | |
5173 | ||
5174 | /* Note that for the legacy commands (i.e. those with action != ACTION_SYSTEMCTL) we support fallbacks to the | |
5175 | * old ways of doing things, hence don't log any error in that case here. */ | |
7e4249b9 | 5176 | |
0a9776c2 | 5177 | return r < 0 ? r : 0; |
7e4249b9 LP |
5178 | } |
5179 | ||
e449de87 | 5180 | static int reset_failed(int argc, char *argv[], void *userdata) { |
e3e0314b | 5181 | _cleanup_strv_free_ char **names = NULL; |
4fbd7192 | 5182 | sd_bus *bus; |
f84190d8 | 5183 | char **name; |
e3e0314b | 5184 | int r, q; |
5632e374 | 5185 | |
e449de87 | 5186 | if (argc <= 1) |
2853b60a | 5187 | return trivial_method(argc, argv, userdata); |
5632e374 | 5188 | |
4fbd7192 LP |
5189 | r = acquire_bus(BUS_MANAGER, &bus); |
5190 | if (r < 0) | |
5191 | return r; | |
5192 | ||
d2ad7e1b ZJS |
5193 | polkit_agent_open_if_enabled(); |
5194 | ||
e449de87 | 5195 | r = expand_names(bus, strv_skip(argv, 1), NULL, &names); |
e3e0314b | 5196 | if (r < 0) |
691395d8 | 5197 | return log_error_errno(r, "Failed to expand names: %m"); |
f84190d8 | 5198 | |
e3e0314b | 5199 | STRV_FOREACH(name, names) { |
4afd3348 | 5200 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
342641fb | 5201 | |
6e646d22 | 5202 | q = sd_bus_call_method( |
f22f08cd | 5203 | bus, |
b0193f1c LP |
5204 | "org.freedesktop.systemd1", |
5205 | "/org/freedesktop/systemd1", | |
5206 | "org.freedesktop.systemd1.Manager", | |
6e646d22 LP |
5207 | "ResetFailedUnit", |
5208 | &error, | |
5209 | NULL, | |
5210 | "s", *name); | |
e3e0314b | 5211 | if (q < 0) { |
691395d8 | 5212 | log_error_errno(q, "Failed to reset failed state of unit %s: %s", *name, bus_error_message(&error, q)); |
e3e0314b ZJS |
5213 | if (r == 0) |
5214 | r = q; | |
f459b602 | 5215 | } |
5632e374 LP |
5216 | } |
5217 | ||
e3e0314b | 5218 | return r; |
5632e374 LP |
5219 | } |
5220 | ||
e449de87 | 5221 | static int show_environment(int argc, char *argv[], void *userdata) { |
4afd3348 LP |
5222 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
5223 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
f459b602 | 5224 | const char *text; |
4fbd7192 | 5225 | sd_bus *bus; |
7e4249b9 | 5226 | int r; |
7e4249b9 | 5227 | |
4fbd7192 LP |
5228 | r = acquire_bus(BUS_MANAGER, &bus); |
5229 | if (r < 0) | |
5230 | return r; | |
5231 | ||
d2ad7e1b ZJS |
5232 | pager_open(arg_no_pager, false); |
5233 | ||
f459b602 | 5234 | r = sd_bus_get_property( |
f22f08cd SP |
5235 | bus, |
5236 | "org.freedesktop.systemd1", | |
5237 | "/org/freedesktop/systemd1", | |
f459b602 MAP |
5238 | "org.freedesktop.systemd1.Manager", |
5239 | "Environment", | |
5240 | &error, | |
f22f08cd | 5241 | &reply, |
f459b602 | 5242 | "as"); |
691395d8 LP |
5243 | if (r < 0) |
5244 | return log_error_errno(r, "Failed to get environment: %s", bus_error_message(&error, r)); | |
7e4249b9 | 5245 | |
f459b602 MAP |
5246 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "s"); |
5247 | if (r < 0) | |
5248 | return bus_log_parse_error(r); | |
7e4249b9 | 5249 | |
f459b602 | 5250 | while ((r = sd_bus_message_read_basic(reply, SD_BUS_TYPE_STRING, &text)) > 0) |
f84190d8 | 5251 | puts(text); |
f459b602 MAP |
5252 | if (r < 0) |
5253 | return bus_log_parse_error(r); | |
7e4249b9 | 5254 | |
f459b602 MAP |
5255 | r = sd_bus_message_exit_container(reply); |
5256 | if (r < 0) | |
5257 | return bus_log_parse_error(r); | |
7e4249b9 | 5258 | |
f84190d8 | 5259 | return 0; |
7e4249b9 LP |
5260 | } |
5261 | ||
e449de87 | 5262 | static int switch_root(int argc, char *argv[], void *userdata) { |
4afd3348 | 5263 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
f39d4a08 HH |
5264 | _cleanup_free_ char *cmdline_init = NULL; |
5265 | const char *root, *init; | |
4fbd7192 | 5266 | sd_bus *bus; |
f459b602 | 5267 | int r; |
957eb8ca | 5268 | |
4fbd7192 LP |
5269 | if (arg_transport != BUS_TRANSPORT_LOCAL) { |
5270 | log_error("Cannot switch root remotely."); | |
5271 | return -EINVAL; | |
5272 | } | |
5273 | ||
e449de87 | 5274 | if (argc < 2 || argc > 3) { |
957eb8ca LP |
5275 | log_error("Wrong number of arguments."); |
5276 | return -EINVAL; | |
5277 | } | |
5278 | ||
e449de87 | 5279 | root = argv[1]; |
13068da8 | 5280 | |
e449de87 LP |
5281 | if (argc >= 3) |
5282 | init = argv[2]; | |
13068da8 | 5283 | else { |
f39d4a08 HH |
5284 | r = parse_env_file("/proc/cmdline", WHITESPACE, |
5285 | "init", &cmdline_init, | |
5286 | NULL); | |
5287 | if (r < 0) | |
da927ba9 | 5288 | log_debug_errno(r, "Failed to parse /proc/cmdline: %m"); |
13068da8 | 5289 | |
f39d4a08 | 5290 | init = cmdline_init; |
13068da8 | 5291 | } |
f459b602 | 5292 | |
3c6f7c34 | 5293 | init = empty_to_null(init); |
f39d4a08 HH |
5294 | if (init) { |
5295 | const char *root_systemd_path = NULL, *root_init_path = NULL; | |
5296 | ||
63c372cb LP |
5297 | root_systemd_path = strjoina(root, "/" SYSTEMD_BINARY_PATH); |
5298 | root_init_path = strjoina(root, "/", init); | |
f39d4a08 HH |
5299 | |
5300 | /* If the passed init is actually the same as the | |
5301 | * systemd binary, then let's suppress it. */ | |
5302 | if (files_same(root_init_path, root_systemd_path) > 0) | |
5303 | init = NULL; | |
5304 | } | |
13068da8 | 5305 | |
4fbd7192 LP |
5306 | r = acquire_bus(BUS_MANAGER, &bus); |
5307 | if (r < 0) | |
5308 | return r; | |
5309 | ||
f39d4a08 | 5310 | log_debug("Switching root - root: %s; init: %s", root, strna(init)); |
957eb8ca | 5311 | |
f459b602 | 5312 | r = sd_bus_call_method( |
f22f08cd | 5313 | bus, |
957eb8ca LP |
5314 | "org.freedesktop.systemd1", |
5315 | "/org/freedesktop/systemd1", | |
5316 | "org.freedesktop.systemd1.Manager", | |
f22f08cd | 5317 | "SwitchRoot", |
f459b602 | 5318 | &error, |
f22f08cd | 5319 | NULL, |
f459b602 | 5320 | "ss", root, init); |
691395d8 LP |
5321 | if (r < 0) |
5322 | return log_error_errno(r, "Failed to switch root: %s", bus_error_message(&error, r)); | |
f459b602 MAP |
5323 | |
5324 | return 0; | |
957eb8ca LP |
5325 | } |
5326 | ||
e449de87 | 5327 | static int set_environment(int argc, char *argv[], void *userdata) { |
4afd3348 LP |
5328 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
5329 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; | |
7e4249b9 | 5330 | const char *method; |
4fbd7192 | 5331 | sd_bus *bus; |
31e767f7 LP |
5332 | int r; |
5333 | ||
e449de87 LP |
5334 | assert(argc > 1); |
5335 | assert(argv); | |
7e4249b9 | 5336 | |
4fbd7192 LP |
5337 | r = acquire_bus(BUS_MANAGER, &bus); |
5338 | if (r < 0) | |
5339 | return r; | |
5340 | ||
d2ad7e1b ZJS |
5341 | polkit_agent_open_if_enabled(); |
5342 | ||
e449de87 | 5343 | method = streq(argv[0], "set-environment") |
7e4249b9 LP |
5344 | ? "SetEnvironment" |
5345 | : "UnsetEnvironment"; | |
5346 | ||
f459b602 MAP |
5347 | r = sd_bus_message_new_method_call( |
5348 | bus, | |
151b9b96 | 5349 | &m, |
31e767f7 LP |
5350 | "org.freedesktop.systemd1", |
5351 | "/org/freedesktop/systemd1", | |
5352 | "org.freedesktop.systemd1.Manager", | |
151b9b96 | 5353 | method); |
f459b602 MAP |
5354 | if (r < 0) |
5355 | return bus_log_create_error(r); | |
7e4249b9 | 5356 | |
e449de87 | 5357 | r = sd_bus_message_append_strv(m, strv_skip(argv, 1)); |
31e767f7 | 5358 | if (r < 0) |
f459b602 | 5359 | return bus_log_create_error(r); |
7e4249b9 | 5360 | |
c49b30a2 | 5361 | r = sd_bus_call(bus, m, 0, &error, NULL); |
691395d8 LP |
5362 | if (r < 0) |
5363 | return log_error_errno(r, "Failed to set environment: %s", bus_error_message(&error, r)); | |
7e4249b9 | 5364 | |
f84190d8 | 5365 | return 0; |
7e4249b9 LP |
5366 | } |
5367 | ||
e449de87 | 5368 | static int import_environment(int argc, char *argv[], void *userdata) { |
4afd3348 LP |
5369 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
5370 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; | |
4fbd7192 | 5371 | sd_bus *bus; |
ac3efa8a LP |
5372 | int r; |
5373 | ||
4fbd7192 LP |
5374 | r = acquire_bus(BUS_MANAGER, &bus); |
5375 | if (r < 0) | |
5376 | return r; | |
5377 | ||
d2ad7e1b ZJS |
5378 | polkit_agent_open_if_enabled(); |
5379 | ||
ac3efa8a LP |
5380 | r = sd_bus_message_new_method_call( |
5381 | bus, | |
151b9b96 | 5382 | &m, |
ac3efa8a LP |
5383 | "org.freedesktop.systemd1", |
5384 | "/org/freedesktop/systemd1", | |
5385 | "org.freedesktop.systemd1.Manager", | |
151b9b96 | 5386 | "SetEnvironment"); |
ac3efa8a LP |
5387 | if (r < 0) |
5388 | return bus_log_create_error(r); | |
5389 | ||
e449de87 | 5390 | if (argc < 2) |
ac3efa8a LP |
5391 | r = sd_bus_message_append_strv(m, environ); |
5392 | else { | |
5393 | char **a, **b; | |
5394 | ||
5395 | r = sd_bus_message_open_container(m, 'a', "s"); | |
5396 | if (r < 0) | |
5397 | return bus_log_create_error(r); | |
5398 | ||
e449de87 | 5399 | STRV_FOREACH(a, strv_skip(argv, 1)) { |
ac3efa8a LP |
5400 | |
5401 | if (!env_name_is_valid(*a)) { | |
5402 | log_error("Not a valid environment variable name: %s", *a); | |
5403 | return -EINVAL; | |
5404 | } | |
5405 | ||
5406 | STRV_FOREACH(b, environ) { | |
5407 | const char *eq; | |
5408 | ||
5409 | eq = startswith(*b, *a); | |
5410 | if (eq && *eq == '=') { | |
5411 | ||
5412 | r = sd_bus_message_append(m, "s", *b); | |
5413 | if (r < 0) | |
5414 | return bus_log_create_error(r); | |
5415 | ||
5416 | break; | |
5417 | } | |
5418 | } | |
5419 | } | |
5420 | ||
5421 | r = sd_bus_message_close_container(m); | |
5422 | } | |
5423 | if (r < 0) | |
5424 | return bus_log_create_error(r); | |
5425 | ||
5426 | r = sd_bus_call(bus, m, 0, &error, NULL); | |
691395d8 LP |
5427 | if (r < 0) |
5428 | return log_error_errno(r, "Failed to import environment: %s", bus_error_message(&error, r)); | |
ac3efa8a LP |
5429 | |
5430 | return 0; | |
5431 | } | |
5432 | ||
cbb13b2a | 5433 | static int enable_sysv_units(const char *verb, char **args) { |
729e3769 | 5434 | int r = 0; |
ee5762e3 | 5435 | |
0f0467e6 | 5436 | #if defined(HAVE_SYSV_COMPAT) |
fb15be83 | 5437 | _cleanup_lookup_paths_free_ LookupPaths paths = {}; |
e735decc LP |
5438 | unsigned f = 0; |
5439 | ||
5440 | /* Processes all SysV units, and reshuffles the array so that afterwards only the native units remain */ | |
ee5762e3 | 5441 | |
729e3769 LP |
5442 | if (arg_scope != UNIT_FILE_SYSTEM) |
5443 | return 0; | |
ee5762e3 | 5444 | |
b41b9d2a LP |
5445 | if (getenv_bool("SYSTEMCTL_SKIP_SYSV") > 0) |
5446 | return 0; | |
5447 | ||
4c315c2c IS |
5448 | if (!STR_IN_SET(verb, |
5449 | "enable", | |
5450 | "disable", | |
5451 | "is-enabled")) | |
729e3769 | 5452 | return 0; |
ee5762e3 | 5453 | |
4943d143 | 5454 | r = lookup_paths_init(&paths, arg_scope, LOOKUP_PATHS_EXCLUDE_GENERATED, arg_root); |
729e3769 LP |
5455 | if (r < 0) |
5456 | return r; | |
ee5762e3 | 5457 | |
729e3769 | 5458 | r = 0; |
a644abed | 5459 | while (args[f]) { |
e735decc LP |
5460 | |
5461 | const char *argv[] = { | |
5462 | ROOTLIBEXECDIR "/systemd-sysv-install", | |
5463 | NULL, | |
5464 | NULL, | |
5465 | NULL, | |
5466 | NULL, | |
5467 | }; | |
5468 | ||
05cae7f3 | 5469 | _cleanup_free_ char *p = NULL, *q = NULL, *l = NULL; |
729e3769 | 5470 | bool found_native = false, found_sysv; |
e735decc LP |
5471 | siginfo_t status; |
5472 | const char *name; | |
729e3769 | 5473 | unsigned c = 1; |
729e3769 | 5474 | pid_t pid; |
e735decc | 5475 | int j; |
ee5762e3 | 5476 | |
a644abed | 5477 | name = args[f++]; |
ee5762e3 | 5478 | |
729e3769 LP |
5479 | if (!endswith(name, ".service")) |
5480 | continue; | |
ee5762e3 | 5481 | |
729e3769 LP |
5482 | if (path_is_absolute(name)) |
5483 | continue; | |
ee5762e3 | 5484 | |
e735decc | 5485 | j = unit_file_exists(arg_scope, &paths, name); |
76ec966f | 5486 | if (j < 0 && !IN_SET(j, -ELOOP, -ERFKILL, -EADDRNOTAVAIL)) |
e735decc | 5487 | return log_error_errno(j, "Failed to lookup unit file state: %m"); |
3c6d8e57 | 5488 | found_native = j != 0; |
ee5762e3 | 5489 | |
e735decc LP |
5490 | /* If we have both a native unit and a SysV script, enable/disable them both (below); for is-enabled, |
5491 | * prefer the native unit */ | |
355ff449 | 5492 | if (found_native && streq(verb, "is-enabled")) |
729e3769 | 5493 | continue; |
ee5762e3 | 5494 | |
0c6ea3a4 ZJS |
5495 | p = path_join(arg_root, SYSTEM_SYSVINIT_PATH, name); |
5496 | if (!p) | |
60731f32 | 5497 | return log_oom(); |
ee5762e3 | 5498 | |
05cae7f3 | 5499 | p[strlen(p) - strlen(".service")] = 0; |
729e3769 | 5500 | found_sysv = access(p, F_OK) >= 0; |
4b6756a8 | 5501 | if (!found_sysv) |
729e3769 | 5502 | continue; |
71fad675 | 5503 | |
355ff449 | 5504 | if (found_native) |
e735decc | 5505 | log_info("Synchronizing state of %s with SysV service script with %s.", name, argv[0]); |
355ff449 | 5506 | else |
e735decc | 5507 | log_info("%s is not a native service, redirecting to systemd-sysv-install.", name); |
ee5762e3 | 5508 | |
729e3769 LP |
5509 | if (!isempty(arg_root)) |
5510 | argv[c++] = q = strappend("--root=", arg_root); | |
ee5762e3 | 5511 | |
0f0467e6 | 5512 | argv[c++] = verb; |
2b6bf07d | 5513 | argv[c++] = basename(p); |
729e3769 | 5514 | argv[c] = NULL; |
ee5762e3 | 5515 | |
729e3769 | 5516 | l = strv_join((char**)argv, " "); |
60731f32 ZJS |
5517 | if (!l) |
5518 | return log_oom(); | |
ee5762e3 | 5519 | |
e735decc | 5520 | log_info("Executing: %s", l); |
ee5762e3 | 5521 | |
729e3769 | 5522 | pid = fork(); |
4a62c710 MS |
5523 | if (pid < 0) |
5524 | return log_error_errno(errno, "Failed to fork: %m"); | |
5525 | else if (pid == 0) { | |
729e3769 | 5526 | /* Child */ |
ee5762e3 | 5527 | |
ce30c8dc LP |
5528 | (void) reset_all_signal_handlers(); |
5529 | (void) reset_signal_mask(); | |
5530 | ||
729e3769 | 5531 | execv(argv[0], (char**) argv); |
cc7cb0ca | 5532 | log_error_errno(errno, "Failed to execute %s: %m", argv[0]); |
729e3769 LP |
5533 | _exit(EXIT_FAILURE); |
5534 | } | |
ee5762e3 | 5535 | |
729e3769 LP |
5536 | j = wait_for_terminate(pid, &status); |
5537 | if (j < 0) { | |
691395d8 | 5538 | log_error_errno(j, "Failed to wait for child: %m"); |
60731f32 | 5539 | return j; |
729e3769 | 5540 | } |
ee5762e3 | 5541 | |
729e3769 LP |
5542 | if (status.si_code == CLD_EXITED) { |
5543 | if (streq(verb, "is-enabled")) { | |
5544 | if (status.si_status == 0) { | |
5545 | if (!arg_quiet) | |
5546 | puts("enabled"); | |
5547 | r = 1; | |
5548 | } else { | |
5549 | if (!arg_quiet) | |
5550 | puts("disabled"); | |
5551 | } | |
ee5762e3 | 5552 | |
60731f32 | 5553 | } else if (status.si_status != 0) |
c61b443d LP |
5554 | return -EBADE; /* We don't warn here, under the assumption the script already showed an explanation */ |
5555 | } else { | |
5556 | log_error("Unexpected waitid() result."); | |
60731f32 | 5557 | return -EPROTO; |
c61b443d | 5558 | } |
ee5762e3 | 5559 | |
355ff449 MP |
5560 | if (found_native) |
5561 | continue; | |
5562 | ||
a644abed | 5563 | /* Remove this entry, so that we don't try enabling it as native unit */ |
aba84331 LP |
5564 | assert(f > 0); |
5565 | f--; | |
5566 | assert(args[f] == name); | |
5567 | strv_remove(args, name); | |
729e3769 | 5568 | } |
ee5762e3 | 5569 | |
729e3769 LP |
5570 | #endif |
5571 | return r; | |
5572 | } | |
ee5762e3 | 5573 | |
37370d0c | 5574 | static int mangle_names(char **original_names, char ***mangled_names) { |
a33fdebb | 5575 | char **i, **l, **name; |
7410616c | 5576 | int r; |
37370d0c | 5577 | |
7410616c | 5578 | l = i = new(char*, strv_length(original_names) + 1); |
a33fdebb | 5579 | if (!l) |
37370d0c VP |
5580 | return log_oom(); |
5581 | ||
37370d0c | 5582 | STRV_FOREACH(name, original_names) { |
44386fc1 LN |
5583 | |
5584 | /* When enabling units qualified path names are OK, | |
5585 | * too, hence allow them explicitly. */ | |
5586 | ||
7410616c | 5587 | if (is_path(*name)) { |
44386fc1 | 5588 | *i = strdup(*name); |
7410616c LP |
5589 | if (!*i) { |
5590 | strv_free(l); | |
5591 | return log_oom(); | |
5592 | } | |
5593 | } else { | |
5594 | r = unit_name_mangle(*name, UNIT_NAME_NOGLOB, i); | |
5595 | if (r < 0) { | |
5596 | strv_free(l); | |
5597 | return log_error_errno(r, "Failed to mangle unit name: %m"); | |
5598 | } | |
a33fdebb LP |
5599 | } |
5600 | ||
5601 | i++; | |
37370d0c | 5602 | } |
a33fdebb LP |
5603 | |
5604 | *i = NULL; | |
5605 | *mangled_names = l; | |
37370d0c VP |
5606 | |
5607 | return 0; | |
5608 | } | |
5609 | ||
d6568222 | 5610 | static int unit_exists(const char *unit) { |
ba19c6e1 | 5611 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
d6568222 SS |
5612 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
5613 | _cleanup_free_ char *path = NULL; | |
5614 | static const struct bus_properties_map property_map[] = { | |
5615 | { "LoadState", "s", NULL, offsetof(UnitStatusInfo, load_state) }, | |
5616 | { "ActiveState", "s", NULL, offsetof(UnitStatusInfo, active_state)}, | |
5617 | {}, | |
5618 | }; | |
5619 | UnitStatusInfo info = {}; | |
5620 | sd_bus *bus; | |
5621 | int r; | |
5622 | ||
5623 | path = unit_dbus_path_from_name(unit); | |
5624 | if (!path) | |
5625 | return log_oom(); | |
5626 | ||
5627 | r = acquire_bus(BUS_MANAGER, &bus); | |
5628 | if (r < 0) | |
5629 | return r; | |
5630 | ||
5631 | r = sd_bus_call_method( | |
5632 | bus, | |
5633 | "org.freedesktop.systemd1", | |
5634 | path, | |
5635 | "org.freedesktop.DBus.Properties", | |
5636 | "GetAll", | |
5637 | &error, | |
5638 | &reply, | |
5639 | "s", ""); | |
5640 | if (r < 0) | |
5641 | return log_error_errno(r, "Failed to get properties: %s", bus_error_message(&error, r)); | |
5642 | ||
5643 | r = bus_message_map_all_properties(reply, property_map, &info); | |
5644 | if (r < 0) | |
5645 | return log_error_errno(r, "Failed to map properties: %s", bus_error_message(&error, r)); | |
5646 | ||
5647 | return !streq_ptr(info.load_state, "not-found") || !streq_ptr(info.active_state, "inactive"); | |
5648 | } | |
5649 | ||
e449de87 | 5650 | static int enable_unit(int argc, char *argv[], void *userdata) { |
e3e0314b | 5651 | _cleanup_strv_free_ char **names = NULL; |
e449de87 | 5652 | const char *verb = argv[0]; |
729e3769 | 5653 | UnitFileChange *changes = NULL; |
718db961 | 5654 | unsigned n_changes = 0; |
729e3769 | 5655 | int carries_install_info = -1; |
35b132e8 | 5656 | bool ignore_carries_install_info = arg_quiet; |
729e3769 | 5657 | int r; |
ee5762e3 | 5658 | |
e449de87 | 5659 | if (!argv[1]) |
ab5919fa MS |
5660 | return 0; |
5661 | ||
e449de87 | 5662 | r = mangle_names(strv_skip(argv, 1), &names); |
3a05c0f9 | 5663 | if (r < 0) |
cbb13b2a VP |
5664 | return r; |
5665 | ||
e3e0314b | 5666 | r = enable_sysv_units(verb, names); |
cbb13b2a VP |
5667 | if (r < 0) |
5668 | return r; | |
3a05c0f9 | 5669 | |
c61b443d | 5670 | /* If the operation was fully executed by the SysV compat, let's finish early */ |
823e5fab FB |
5671 | if (strv_isempty(names)) { |
5672 | if (arg_no_reload || install_client_side()) | |
5673 | return 0; | |
5674 | return daemon_reload(argc, argv, userdata); | |
5675 | } | |
67d66210 | 5676 | |
4fbd7192 | 5677 | if (install_client_side()) { |
729e3769 | 5678 | if (streq(verb, "enable")) { |
e3e0314b | 5679 | r = unit_file_enable(arg_scope, arg_runtime, arg_root, names, arg_force, &changes, &n_changes); |
729e3769 LP |
5680 | carries_install_info = r; |
5681 | } else if (streq(verb, "disable")) | |
e3e0314b | 5682 | r = unit_file_disable(arg_scope, arg_runtime, arg_root, names, &changes, &n_changes); |
729e3769 | 5683 | else if (streq(verb, "reenable")) { |
e3e0314b | 5684 | r = unit_file_reenable(arg_scope, arg_runtime, arg_root, names, arg_force, &changes, &n_changes); |
729e3769 LP |
5685 | carries_install_info = r; |
5686 | } else if (streq(verb, "link")) | |
e3e0314b | 5687 | r = unit_file_link(arg_scope, arg_runtime, arg_root, names, arg_force, &changes, &n_changes); |
729e3769 | 5688 | else if (streq(verb, "preset")) { |
d309c1c3 | 5689 | r = unit_file_preset(arg_scope, arg_runtime, arg_root, names, arg_preset_mode, arg_force, &changes, &n_changes); |
729e3769 | 5690 | } else if (streq(verb, "mask")) |
e3e0314b | 5691 | r = unit_file_mask(arg_scope, arg_runtime, arg_root, names, arg_force, &changes, &n_changes); |
729e3769 | 5692 | else if (streq(verb, "unmask")) |
e3e0314b | 5693 | r = unit_file_unmask(arg_scope, arg_runtime, arg_root, names, &changes, &n_changes); |
344ca755 LP |
5694 | else if (streq(verb, "revert")) |
5695 | r = unit_file_revert(arg_scope, arg_root, names, &changes, &n_changes); | |
729e3769 LP |
5696 | else |
5697 | assert_not_reached("Unknown verb"); | |
ee5762e3 | 5698 | |
af3d8113 | 5699 | unit_file_dump_changes(r, verb, changes, n_changes, arg_quiet); |
d073dea0 | 5700 | if (r < 0) |
85b78539 | 5701 | goto finish; |
df77cdf0 | 5702 | r = 0; |
729e3769 | 5703 | } else { |
4afd3348 LP |
5704 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL, *m = NULL; |
5705 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
39207373 | 5706 | bool expect_carries_install_info = false; |
344ca755 | 5707 | bool send_runtime = true, send_force = true, send_preset_mode = false; |
718db961 | 5708 | const char *method; |
4fbd7192 | 5709 | sd_bus *bus; |
729e3769 | 5710 | |
d6568222 SS |
5711 | if (STR_IN_SET(verb, "mask", "unmask")) { |
5712 | r = unit_exists(*names); | |
5713 | if (r < 0) | |
5714 | return r; | |
5715 | if (r == 0) | |
5716 | log_notice("Unit %s does not exist, proceeding anyway.", *names); | |
5717 | } | |
5718 | ||
4fbd7192 LP |
5719 | r = acquire_bus(BUS_MANAGER, &bus); |
5720 | if (r < 0) | |
5721 | return r; | |
5722 | ||
d2ad7e1b ZJS |
5723 | polkit_agent_open_if_enabled(); |
5724 | ||
729e3769 LP |
5725 | if (streq(verb, "enable")) { |
5726 | method = "EnableUnitFiles"; | |
5727 | expect_carries_install_info = true; | |
5728 | } else if (streq(verb, "disable")) { | |
5729 | method = "DisableUnitFiles"; | |
5730 | send_force = false; | |
5731 | } else if (streq(verb, "reenable")) { | |
5732 | method = "ReenableUnitFiles"; | |
5733 | expect_carries_install_info = true; | |
5734 | } else if (streq(verb, "link")) | |
5735 | method = "LinkUnitFiles"; | |
5736 | else if (streq(verb, "preset")) { | |
d309c1c3 LP |
5737 | |
5738 | if (arg_preset_mode != UNIT_FILE_PRESET_FULL) { | |
5739 | method = "PresetUnitFilesWithMode"; | |
5740 | send_preset_mode = true; | |
5741 | } else | |
5742 | method = "PresetUnitFiles"; | |
5743 | ||
729e3769 | 5744 | expect_carries_install_info = true; |
39207373 | 5745 | ignore_carries_install_info = true; |
729e3769 LP |
5746 | } else if (streq(verb, "mask")) |
5747 | method = "MaskUnitFiles"; | |
5748 | else if (streq(verb, "unmask")) { | |
5749 | method = "UnmaskUnitFiles"; | |
5750 | send_force = false; | |
344ca755 LP |
5751 | } else if (streq(verb, "revert")) { |
5752 | method = "RevertUnitFiles"; | |
5753 | send_runtime = send_force = false; | |
729e3769 LP |
5754 | } else |
5755 | assert_not_reached("Unknown verb"); | |
5756 | ||
f459b602 MAP |
5757 | r = sd_bus_message_new_method_call( |
5758 | bus, | |
151b9b96 | 5759 | &m, |
729e3769 LP |
5760 | "org.freedesktop.systemd1", |
5761 | "/org/freedesktop/systemd1", | |
5762 | "org.freedesktop.systemd1.Manager", | |
151b9b96 | 5763 | method); |
f459b602 MAP |
5764 | if (r < 0) |
5765 | return bus_log_create_error(r); | |
ee5762e3 | 5766 | |
e3e0314b | 5767 | r = sd_bus_message_append_strv(m, names); |
f459b602 MAP |
5768 | if (r < 0) |
5769 | return bus_log_create_error(r); | |
ee5762e3 | 5770 | |
d309c1c3 LP |
5771 | if (send_preset_mode) { |
5772 | r = sd_bus_message_append(m, "s", unit_file_preset_mode_to_string(arg_preset_mode)); | |
5773 | if (r < 0) | |
5774 | return bus_log_create_error(r); | |
5775 | } | |
5776 | ||
344ca755 LP |
5777 | if (send_runtime) { |
5778 | r = sd_bus_message_append(m, "b", arg_runtime); | |
5779 | if (r < 0) | |
5780 | return bus_log_create_error(r); | |
5781 | } | |
ee5762e3 | 5782 | |
729e3769 | 5783 | if (send_force) { |
f459b602 MAP |
5784 | r = sd_bus_message_append(m, "b", arg_force); |
5785 | if (r < 0) | |
5786 | return bus_log_create_error(r); | |
ee5762e3 LP |
5787 | } |
5788 | ||
c49b30a2 | 5789 | r = sd_bus_call(bus, m, 0, &error, &reply); |
691395d8 | 5790 | if (r < 0) |
af3d8113 | 5791 | return log_error_errno(r, "Failed to %s unit: %s", verb, bus_error_message(&error, r)); |
be394c48 | 5792 | |
729e3769 | 5793 | if (expect_carries_install_info) { |
f459b602 MAP |
5794 | r = sd_bus_message_read(reply, "b", &carries_install_info); |
5795 | if (r < 0) | |
5796 | return bus_log_parse_error(r); | |
ee5762e3 LP |
5797 | } |
5798 | ||
57ab2eab | 5799 | r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet, &changes, &n_changes); |
f459b602 | 5800 | if (r < 0) |
85b78539 | 5801 | goto finish; |
b77398f7 | 5802 | |
93c941e3 | 5803 | /* Try to reload if enabled */ |
d6cb60c7 | 5804 | if (!arg_no_reload) |
e449de87 | 5805 | r = daemon_reload(argc, argv, userdata); |
f459b602 MAP |
5806 | else |
5807 | r = 0; | |
b647f10d | 5808 | } |
3d3961f2 | 5809 | |
39207373 | 5810 | if (carries_install_info == 0 && !ignore_carries_install_info) |
fe4aede9 ZJS |
5811 | log_warning("The unit files have no installation config (WantedBy, RequiredBy, Also, Alias\n" |
5812 | "settings in the [Install] section, and DefaultInstance for template units).\n" | |
5813 | "This means they are not meant to be enabled using systemctl.\n" | |
416389f7 LP |
5814 | "Possible reasons for having this kind of units are:\n" |
5815 | "1) A unit may be statically enabled by being symlinked from another unit's\n" | |
5816 | " .wants/ or .requires/ directory.\n" | |
5817 | "2) A unit's purpose may be to act as a helper for some other unit which has\n" | |
5818 | " a requirement dependency on it.\n" | |
5819 | "3) A unit may be started when needed via activation (socket, path, timer,\n" | |
fe4aede9 ZJS |
5820 | " D-Bus, udev, scripted systemctl call, ...).\n" |
5821 | "4) In case of template units, the unit is meant to be enabled with some\n" | |
5822 | " instance name specified."); | |
ee5762e3 | 5823 | |
e449de87 | 5824 | if (arg_now && n_changes > 0 && STR_IN_SET(argv[0], "enable", "disable", "mask")) { |
57ab2eab | 5825 | char *new_args[n_changes + 2]; |
4fbd7192 | 5826 | sd_bus *bus; |
57ab2eab JS |
5827 | unsigned i; |
5828 | ||
4fbd7192 LP |
5829 | r = acquire_bus(BUS_MANAGER, &bus); |
5830 | if (r < 0) | |
d073dea0 | 5831 | goto finish; |
4fbd7192 | 5832 | |
e449de87 | 5833 | new_args[0] = (char*) (streq(argv[0], "enable") ? "start" : "stop"); |
57ab2eab JS |
5834 | for (i = 0; i < n_changes; i++) |
5835 | new_args[i + 1] = basename(changes[i].path); | |
5836 | new_args[i + 1] = NULL; | |
5837 | ||
e449de87 | 5838 | r = start_unit(strv_length(new_args), new_args, userdata); |
57ab2eab JS |
5839 | } |
5840 | ||
729e3769 | 5841 | finish: |
729e3769 | 5842 | unit_file_changes_free(changes, n_changes); |
ee5762e3 | 5843 | |
729e3769 | 5844 | return r; |
ee5762e3 LP |
5845 | } |
5846 | ||
e449de87 | 5847 | static int add_dependency(int argc, char *argv[], void *userdata) { |
e94937df LN |
5848 | _cleanup_strv_free_ char **names = NULL; |
5849 | _cleanup_free_ char *target = NULL; | |
e449de87 | 5850 | const char *verb = argv[0]; |
acc0269c CH |
5851 | UnitFileChange *changes = NULL; |
5852 | unsigned n_changes = 0; | |
e94937df LN |
5853 | UnitDependency dep; |
5854 | int r = 0; | |
5855 | ||
e449de87 | 5856 | if (!argv[1]) |
e94937df LN |
5857 | return 0; |
5858 | ||
e449de87 | 5859 | r = unit_name_mangle_with_suffix(argv[1], UNIT_NAME_NOGLOB, ".target", &target); |
7410616c LP |
5860 | if (r < 0) |
5861 | return log_error_errno(r, "Failed to mangle unit name: %m"); | |
e94937df | 5862 | |
e449de87 | 5863 | r = mangle_names(strv_skip(argv, 2), &names); |
e94937df LN |
5864 | if (r < 0) |
5865 | return r; | |
5866 | ||
5867 | if (streq(verb, "add-wants")) | |
5868 | dep = UNIT_WANTS; | |
5869 | else if (streq(verb, "add-requires")) | |
5870 | dep = UNIT_REQUIRES; | |
5871 | else | |
5872 | assert_not_reached("Unknown verb"); | |
5873 | ||
4fbd7192 | 5874 | if (install_client_side()) { |
e94937df | 5875 | r = unit_file_add_dependency(arg_scope, arg_runtime, arg_root, names, target, dep, arg_force, &changes, &n_changes); |
af3d8113 | 5876 | unit_file_dump_changes(r, "add dependency on", changes, n_changes, arg_quiet); |
5f056378 CH |
5877 | |
5878 | if (r > 0) | |
5879 | r = 0; | |
e94937df | 5880 | } else { |
4afd3348 LP |
5881 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL, *m = NULL; |
5882 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
4fbd7192 | 5883 | sd_bus *bus; |
e94937df | 5884 | |
4fbd7192 LP |
5885 | r = acquire_bus(BUS_MANAGER, &bus); |
5886 | if (r < 0) | |
5887 | return r; | |
5888 | ||
d2ad7e1b ZJS |
5889 | polkit_agent_open_if_enabled(); |
5890 | ||
e94937df LN |
5891 | r = sd_bus_message_new_method_call( |
5892 | bus, | |
5893 | &m, | |
5894 | "org.freedesktop.systemd1", | |
5895 | "/org/freedesktop/systemd1", | |
5896 | "org.freedesktop.systemd1.Manager", | |
5897 | "AddDependencyUnitFiles"); | |
5898 | if (r < 0) | |
5899 | return bus_log_create_error(r); | |
5900 | ||
342641fb | 5901 | r = sd_bus_message_append_strv(m, names); |
e94937df LN |
5902 | if (r < 0) |
5903 | return bus_log_create_error(r); | |
5904 | ||
342641fb | 5905 | r = sd_bus_message_append(m, "ssbb", target, unit_dependency_to_string(dep), arg_runtime, arg_force); |
e94937df LN |
5906 | if (r < 0) |
5907 | return bus_log_create_error(r); | |
5908 | ||
5909 | r = sd_bus_call(bus, m, 0, &error, &reply); | |
691395d8 | 5910 | if (r < 0) |
af3d8113 | 5911 | return log_error_errno(r, "Failed to add dependency: %s", bus_error_message(&error, r)); |
e94937df | 5912 | |
acc0269c | 5913 | r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet, &changes, &n_changes); |
e94937df | 5914 | if (r < 0) |
acc0269c | 5915 | goto finish; |
e94937df | 5916 | |
acc0269c CH |
5917 | if (arg_no_reload) { |
5918 | r = 0; | |
5919 | goto finish; | |
5920 | } | |
5921 | ||
5922 | r = daemon_reload(argc, argv, userdata); | |
e94937df | 5923 | } |
acc0269c CH |
5924 | |
5925 | finish: | |
5926 | unit_file_changes_free(changes, n_changes); | |
5927 | ||
5928 | return r; | |
e94937df LN |
5929 | } |
5930 | ||
e449de87 | 5931 | static int preset_all(int argc, char *argv[], void *userdata) { |
acc0269c CH |
5932 | UnitFileChange *changes = NULL; |
5933 | unsigned n_changes = 0; | |
d309c1c3 LP |
5934 | int r; |
5935 | ||
4fbd7192 | 5936 | if (install_client_side()) { |
d309c1c3 | 5937 | r = unit_file_preset_all(arg_scope, arg_runtime, arg_root, arg_preset_mode, arg_force, &changes, &n_changes); |
af3d8113 | 5938 | unit_file_dump_changes(r, "preset", changes, n_changes, arg_quiet); |
5f056378 CH |
5939 | |
5940 | if (r > 0) | |
5941 | r = 0; | |
d309c1c3 | 5942 | } else { |
4afd3348 LP |
5943 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
5944 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
4fbd7192 | 5945 | sd_bus *bus; |
d309c1c3 | 5946 | |
4fbd7192 LP |
5947 | r = acquire_bus(BUS_MANAGER, &bus); |
5948 | if (r < 0) | |
5949 | return r; | |
5950 | ||
d2ad7e1b ZJS |
5951 | polkit_agent_open_if_enabled(); |
5952 | ||
6e646d22 | 5953 | r = sd_bus_call_method( |
d309c1c3 LP |
5954 | bus, |
5955 | "org.freedesktop.systemd1", | |
5956 | "/org/freedesktop/systemd1", | |
5957 | "org.freedesktop.systemd1.Manager", | |
6e646d22 LP |
5958 | "PresetAllUnitFiles", |
5959 | &error, | |
5960 | &reply, | |
d309c1c3 LP |
5961 | "sbb", |
5962 | unit_file_preset_mode_to_string(arg_preset_mode), | |
5963 | arg_runtime, | |
5964 | arg_force); | |
691395d8 | 5965 | if (r < 0) |
af3d8113 | 5966 | return log_error_errno(r, "Failed to preset all units: %s", bus_error_message(&error, r)); |
d309c1c3 | 5967 | |
acc0269c | 5968 | r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet, &changes, &n_changes); |
d309c1c3 | 5969 | if (r < 0) |
acc0269c | 5970 | goto finish; |
d309c1c3 | 5971 | |
acc0269c CH |
5972 | if (arg_no_reload) { |
5973 | r = 0; | |
5974 | goto finish; | |
5975 | } | |
5976 | ||
5977 | r = daemon_reload(argc, argv, userdata); | |
d309c1c3 | 5978 | } |
acc0269c CH |
5979 | |
5980 | finish: | |
5981 | unit_file_changes_free(changes, n_changes); | |
5982 | ||
5983 | return r; | |
d309c1c3 LP |
5984 | } |
5985 | ||
e449de87 | 5986 | static int unit_is_enabled(int argc, char *argv[], void *userdata) { |
f459b602 | 5987 | |
e3e0314b | 5988 | _cleanup_strv_free_ char **names = NULL; |
729e3769 LP |
5989 | bool enabled; |
5990 | char **name; | |
f459b602 | 5991 | int r; |
ee5762e3 | 5992 | |
e449de87 | 5993 | r = mangle_names(strv_skip(argv, 1), &names); |
cbb13b2a VP |
5994 | if (r < 0) |
5995 | return r; | |
5996 | ||
e449de87 | 5997 | r = enable_sysv_units(argv[0], names); |
729e3769 LP |
5998 | if (r < 0) |
5999 | return r; | |
ee5762e3 | 6000 | |
729e3769 | 6001 | enabled = r > 0; |
ee5762e3 | 6002 | |
4fbd7192 | 6003 | if (install_client_side()) { |
ee5762e3 | 6004 | |
e3e0314b | 6005 | STRV_FOREACH(name, names) { |
729e3769 | 6006 | UnitFileState state; |
ee5762e3 | 6007 | |
0ec0deaa LP |
6008 | r = unit_file_get_state(arg_scope, arg_root, *name, &state); |
6009 | if (r < 0) | |
f647962d | 6010 | return log_error_errno(state, "Failed to get unit file state for %s: %m", *name); |
ee5762e3 | 6011 | |
4c315c2c IS |
6012 | if (IN_SET(state, |
6013 | UNIT_FILE_ENABLED, | |
6014 | UNIT_FILE_ENABLED_RUNTIME, | |
6015 | UNIT_FILE_STATIC, | |
f4139308 LP |
6016 | UNIT_FILE_INDIRECT, |
6017 | UNIT_FILE_GENERATED)) | |
729e3769 LP |
6018 | enabled = true; |
6019 | ||
6020 | if (!arg_quiet) | |
6021 | puts(unit_file_state_to_string(state)); | |
71fad675 | 6022 | } |
ee5762e3 | 6023 | |
5f056378 | 6024 | r = 0; |
729e3769 | 6025 | } else { |
4afd3348 | 6026 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4fbd7192 LP |
6027 | sd_bus *bus; |
6028 | ||
6029 | r = acquire_bus(BUS_MANAGER, &bus); | |
6030 | if (r < 0) | |
6031 | return r; | |
6032 | ||
e3e0314b | 6033 | STRV_FOREACH(name, names) { |
4afd3348 | 6034 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
729e3769 | 6035 | const char *s; |
63a723f3 | 6036 | |
f459b602 | 6037 | r = sd_bus_call_method( |
f22f08cd | 6038 | bus, |
729e3769 LP |
6039 | "org.freedesktop.systemd1", |
6040 | "/org/freedesktop/systemd1", | |
6041 | "org.freedesktop.systemd1.Manager", | |
f22f08cd | 6042 | "GetUnitFileState", |
f459b602 | 6043 | &error, |
f22f08cd | 6044 | &reply, |
04504f93 | 6045 | "s", *name); |
691395d8 LP |
6046 | if (r < 0) |
6047 | return log_error_errno(r, "Failed to get unit file state for %s: %s", *name, bus_error_message(&error, r)); | |
ee5762e3 | 6048 | |
f459b602 MAP |
6049 | r = sd_bus_message_read(reply, "s", &s); |
6050 | if (r < 0) | |
6051 | return bus_log_parse_error(r); | |
ee5762e3 | 6052 | |
f4139308 | 6053 | if (STR_IN_SET(s, "enabled", "enabled-runtime", "static", "indirect", "generated")) |
729e3769 LP |
6054 | enabled = true; |
6055 | ||
6056 | if (!arg_quiet) | |
6057 | puts(s); | |
560d8f23 | 6058 | } |
ee5762e3 LP |
6059 | } |
6060 | ||
f4139308 | 6061 | return enabled ? EXIT_SUCCESS : EXIT_FAILURE; |
ee5762e3 LP |
6062 | } |
6063 | ||
e449de87 | 6064 | static int is_system_running(int argc, char *argv[], void *userdata) { |
99813a19 | 6065 | _cleanup_free_ char *state = NULL; |
4fbd7192 | 6066 | sd_bus *bus; |
99813a19 LP |
6067 | int r; |
6068 | ||
040524b4 | 6069 | if (running_in_chroot() > 0 || (arg_transport == BUS_TRANSPORT_LOCAL && !sd_booted())) { |
94f099d8 LP |
6070 | if (!arg_quiet) |
6071 | puts("offline"); | |
6072 | return EXIT_FAILURE; | |
6073 | } | |
6074 | ||
4fbd7192 LP |
6075 | r = acquire_bus(BUS_MANAGER, &bus); |
6076 | if (r < 0) | |
6077 | return r; | |
6078 | ||
99813a19 LP |
6079 | r = sd_bus_get_property_string( |
6080 | bus, | |
6081 | "org.freedesktop.systemd1", | |
6082 | "/org/freedesktop/systemd1", | |
6083 | "org.freedesktop.systemd1.Manager", | |
6084 | "SystemState", | |
6085 | NULL, | |
6086 | &state); | |
6087 | if (r < 0) { | |
6088 | if (!arg_quiet) | |
6089 | puts("unknown"); | |
6090 | return 0; | |
6091 | } | |
6092 | ||
6093 | if (!arg_quiet) | |
6094 | puts(state); | |
6095 | ||
6096 | return streq(state, "running") ? EXIT_SUCCESS : EXIT_FAILURE; | |
6097 | } | |
6098 | ||
7d4fb3b1 | 6099 | static int create_edit_temp_file(const char *new_path, const char *original_path, char **ret_tmp_fn) { |
45519fd6 | 6100 | _cleanup_free_ char *t = NULL; |
ae6c3cc0 | 6101 | int r; |
7d4fb3b1 RC |
6102 | |
6103 | assert(new_path); | |
6104 | assert(original_path); | |
6105 | assert(ret_tmp_fn); | |
6106 | ||
14bcf25c | 6107 | r = tempfn_random(new_path, NULL, &t); |
ae6c3cc0 | 6108 | if (r < 0) |
029009d4 | 6109 | return log_error_errno(r, "Failed to determine temporary filename for \"%s\": %m", new_path); |
7d4fb3b1 RC |
6110 | |
6111 | r = mkdir_parents(new_path, 0755); | |
45519fd6 | 6112 | if (r < 0) |
691395d8 | 6113 | return log_error_errno(r, "Failed to create directories for \"%s\": %m", new_path); |
7d4fb3b1 | 6114 | |
f2068bcc | 6115 | r = copy_file(original_path, t, 0, 0644, 0); |
7d4fb3b1 | 6116 | if (r == -ENOENT) { |
45519fd6 | 6117 | |
7d4fb3b1 | 6118 | r = touch(t); |
45519fd6 LP |
6119 | if (r < 0) |
6120 | return log_error_errno(r, "Failed to create temporary file \"%s\": %m", t); | |
6121 | ||
6122 | } else if (r < 0) | |
39c38ce1 | 6123 | return log_error_errno(r, "Failed to create temporary file for \"%s\": %m", new_path); |
7d4fb3b1 RC |
6124 | |
6125 | *ret_tmp_fn = t; | |
45519fd6 | 6126 | t = NULL; |
7d4fb3b1 RC |
6127 | |
6128 | return 0; | |
6129 | } | |
6130 | ||
c51932be LP |
6131 | static int get_file_to_edit( |
6132 | const LookupPaths *paths, | |
6133 | const char *name, | |
6134 | char **ret_path) { | |
6135 | ||
6136 | _cleanup_free_ char *path = NULL, *run = NULL; | |
7d4fb3b1 | 6137 | |
45519fd6 LP |
6138 | assert(name); |
6139 | assert(ret_path); | |
6140 | ||
c51932be LP |
6141 | path = strjoin(paths->persistent_config, "/", name, NULL); |
6142 | if (!path) | |
7d4fb3b1 RC |
6143 | return log_oom(); |
6144 | ||
c51932be | 6145 | if (arg_runtime) { |
79d4ace1 | 6146 | run = strjoin(paths->runtime_config, "/", name, NULL); |
c51932be LP |
6147 | if (!run) |
6148 | return log_oom(); | |
6149 | } | |
6150 | ||
bc854dc7 | 6151 | if (arg_runtime) { |
691395d8 LP |
6152 | if (access(path, F_OK) >= 0) { |
6153 | log_error("Refusing to create \"%s\" because it would be overridden by \"%s\" anyway.", run, path); | |
6154 | return -EEXIST; | |
6155 | } | |
6156 | ||
bc854dc7 ZJS |
6157 | *ret_path = run; |
6158 | run = NULL; | |
6159 | } else { | |
6160 | *ret_path = path; | |
6161 | path = NULL; | |
6162 | } | |
7d4fb3b1 RC |
6163 | |
6164 | return 0; | |
6165 | } | |
6166 | ||
39c38ce1 | 6167 | static int unit_file_create_new( |
c51932be LP |
6168 | const LookupPaths *paths, |
6169 | const char *unit_name, | |
39c38ce1 | 6170 | const char *suffix, |
c51932be LP |
6171 | char **ret_new_path, |
6172 | char **ret_tmp_path) { | |
6173 | ||
45519fd6 | 6174 | char *tmp_new_path, *tmp_tmp_path, *ending; |
7d4fb3b1 RC |
6175 | int r; |
6176 | ||
6177 | assert(unit_name); | |
6178 | assert(ret_new_path); | |
6179 | assert(ret_tmp_path); | |
6180 | ||
39c38ce1 | 6181 | ending = strjoina(unit_name, suffix); |
c51932be | 6182 | r = get_file_to_edit(paths, ending, &tmp_new_path); |
7d4fb3b1 RC |
6183 | if (r < 0) |
6184 | return r; | |
6185 | ||
6186 | r = create_edit_temp_file(tmp_new_path, tmp_new_path, &tmp_tmp_path); | |
6187 | if (r < 0) { | |
6188 | free(tmp_new_path); | |
6189 | return r; | |
6190 | } | |
6191 | ||
6192 | *ret_new_path = tmp_new_path; | |
6193 | *ret_tmp_path = tmp_tmp_path; | |
6194 | ||
6195 | return 0; | |
6196 | } | |
6197 | ||
1cfa9a4c | 6198 | static int unit_file_create_copy( |
c51932be | 6199 | const LookupPaths *paths, |
1cfa9a4c LP |
6200 | const char *unit_name, |
6201 | const char *fragment_path, | |
1cfa9a4c LP |
6202 | char **ret_new_path, |
6203 | char **ret_tmp_path) { | |
6204 | ||
45519fd6 | 6205 | char *tmp_new_path, *tmp_tmp_path; |
7d4fb3b1 RC |
6206 | int r; |
6207 | ||
6208 | assert(fragment_path); | |
6209 | assert(unit_name); | |
6210 | assert(ret_new_path); | |
6211 | assert(ret_tmp_path); | |
6212 | ||
c51932be | 6213 | r = get_file_to_edit(paths, unit_name, &tmp_new_path); |
7d4fb3b1 RC |
6214 | if (r < 0) |
6215 | return r; | |
6216 | ||
6217 | if (!path_equal(fragment_path, tmp_new_path) && access(tmp_new_path, F_OK) == 0) { | |
6218 | char response; | |
6219 | ||
029009d4 | 6220 | r = ask_char(&response, "yn", "\"%s\" already exists. Overwrite with \"%s\"? [(y)es, (n)o] ", tmp_new_path, fragment_path); |
7d4fb3b1 RC |
6221 | if (r < 0) { |
6222 | free(tmp_new_path); | |
6223 | return r; | |
6224 | } | |
6225 | if (response != 'y') { | |
6226 | log_warning("%s ignored", unit_name); | |
6227 | free(tmp_new_path); | |
dbf43a1b | 6228 | return -EKEYREJECTED; |
7d4fb3b1 RC |
6229 | } |
6230 | } | |
6231 | ||
6232 | r = create_edit_temp_file(tmp_new_path, fragment_path, &tmp_tmp_path); | |
6233 | if (r < 0) { | |
7d4fb3b1 RC |
6234 | free(tmp_new_path); |
6235 | return r; | |
6236 | } | |
6237 | ||
6238 | *ret_new_path = tmp_new_path; | |
6239 | *ret_tmp_path = tmp_tmp_path; | |
6240 | ||
6241 | return 0; | |
6242 | } | |
6243 | ||
6244 | static int run_editor(char **paths) { | |
6245 | pid_t pid; | |
6246 | int r; | |
6247 | ||
6248 | assert(paths); | |
6249 | ||
6250 | pid = fork(); | |
691395d8 LP |
6251 | if (pid < 0) |
6252 | return log_error_errno(errno, "Failed to fork: %m"); | |
7d4fb3b1 RC |
6253 | |
6254 | if (pid == 0) { | |
6255 | const char **args; | |
9ef5d8f2 | 6256 | char *editor, **editor_args = NULL; |
1cfa9a4c | 6257 | char **tmp_path, **original_path, *p; |
9ef5d8f2 | 6258 | unsigned n_editor_args = 0, i = 1; |
7d4fb3b1 RC |
6259 | size_t argc; |
6260 | ||
ce30c8dc LP |
6261 | (void) reset_all_signal_handlers(); |
6262 | (void) reset_signal_mask(); | |
6263 | ||
7d4fb3b1 | 6264 | argc = strv_length(paths)/2 + 1; |
7d4fb3b1 RC |
6265 | |
6266 | /* SYSTEMD_EDITOR takes precedence over EDITOR which takes precedence over VISUAL | |
6267 | * If neither SYSTEMD_EDITOR nor EDITOR nor VISUAL are present, | |
6268 | * we try to execute well known editors | |
6269 | */ | |
6270 | editor = getenv("SYSTEMD_EDITOR"); | |
6271 | if (!editor) | |
6272 | editor = getenv("EDITOR"); | |
6273 | if (!editor) | |
6274 | editor = getenv("VISUAL"); | |
6275 | ||
6276 | if (!isempty(editor)) { | |
9ef5d8f2 JS |
6277 | editor_args = strv_split(editor, WHITESPACE); |
6278 | if (!editor_args) { | |
6279 | (void) log_oom(); | |
6280 | _exit(EXIT_FAILURE); | |
6281 | } | |
6282 | n_editor_args = strv_length(editor_args); | |
6283 | argc += n_editor_args - 1; | |
6284 | } | |
6285 | args = newa(const char*, argc + 1); | |
6286 | ||
6287 | if (n_editor_args > 0) { | |
6288 | args[0] = editor_args[0]; | |
6289 | for (; i < n_editor_args; i++) | |
6290 | args[i] = editor_args[i]; | |
6291 | } | |
6292 | ||
6293 | STRV_FOREACH_PAIR(original_path, tmp_path, paths) { | |
6294 | args[i] = *tmp_path; | |
6295 | i++; | |
7d4fb3b1 | 6296 | } |
9ef5d8f2 JS |
6297 | args[i] = NULL; |
6298 | ||
6299 | if (n_editor_args > 0) | |
6300 | execvp(args[0], (char* const*) args); | |
7d4fb3b1 | 6301 | |
9391a1c3 | 6302 | FOREACH_STRING(p, "editor", "nano", "vim", "vi") { |
1cfa9a4c LP |
6303 | args[0] = p; |
6304 | execvp(p, (char* const*) args); | |
7d4fb3b1 RC |
6305 | /* We do not fail if the editor doesn't exist |
6306 | * because we want to try each one of them before | |
6307 | * failing. | |
6308 | */ | |
6309 | if (errno != ENOENT) { | |
691395d8 | 6310 | log_error_errno(errno, "Failed to execute %s: %m", editor); |
7d4fb3b1 RC |
6311 | _exit(EXIT_FAILURE); |
6312 | } | |
6313 | } | |
6314 | ||
1cfa9a4c | 6315 | log_error("Cannot edit unit(s), no editor available. Please set either $SYSTEMD_EDITOR, $EDITOR or $VISUAL."); |
7d4fb3b1 RC |
6316 | _exit(EXIT_FAILURE); |
6317 | } | |
6318 | ||
6319 | r = wait_for_terminate_and_warn("editor", pid, true); | |
6320 | if (r < 0) | |
6321 | return log_error_errno(r, "Failed to wait for child: %m"); | |
6322 | ||
45519fd6 | 6323 | return 0; |
7d4fb3b1 RC |
6324 | } |
6325 | ||
6326 | static int find_paths_to_edit(sd_bus *bus, char **names, char ***paths) { | |
e9e310f8 | 6327 | _cleanup_lookup_paths_free_ LookupPaths lp = {}; |
7d4fb3b1 RC |
6328 | char **name; |
6329 | int r; | |
6330 | ||
6331 | assert(names); | |
6332 | assert(paths); | |
6333 | ||
4943d143 | 6334 | r = lookup_paths_init(&lp, arg_scope, 0, arg_root); |
5b013a2f | 6335 | if (r < 0) |
8df18507 | 6336 | return r; |
7d4fb3b1 | 6337 | |
e9e310f8 | 6338 | STRV_FOREACH(name, names) { |
78df0edc | 6339 | _cleanup_free_ char *path = NULL, *new_path = NULL, *tmp_path = NULL; |
7d4fb3b1 | 6340 | |
4fbd7192 | 6341 | r = unit_find_paths(bus, *name, &lp, &path, NULL); |
e9e310f8 RC |
6342 | if (r < 0) |
6343 | return r; | |
39c38ce1 DC |
6344 | else if (!arg_force) { |
6345 | if (r == 0) { | |
6346 | log_error("Run 'systemctl edit --force %s' to create a new unit.", *name); | |
6347 | return -ENOENT; | |
6348 | } else if (!path) { | |
6349 | // FIXME: support units with path==NULL (no FragmentPath) | |
6350 | log_error("No fragment exists for %s.", *name); | |
6351 | return -ENOENT; | |
6352 | } | |
6353 | } | |
6354 | ||
6355 | if (path) { | |
6356 | if (arg_full) | |
6357 | r = unit_file_create_copy(&lp, *name, path, &new_path, &tmp_path); | |
6358 | else | |
6359 | r = unit_file_create_new(&lp, *name, ".d/override.conf", &new_path, &tmp_path); | |
dbf43a1b | 6360 | } else |
39c38ce1 | 6361 | r = unit_file_create_new(&lp, *name, NULL, &new_path, &tmp_path); |
e9e310f8 | 6362 | if (r < 0) |
ad2a0358 | 6363 | return r; |
7d4fb3b1 | 6364 | |
e9e310f8 RC |
6365 | r = strv_push_pair(paths, new_path, tmp_path); |
6366 | if (r < 0) | |
6367 | return log_oom(); | |
78df0edc | 6368 | new_path = tmp_path = NULL; |
7d4fb3b1 RC |
6369 | } |
6370 | ||
6371 | return 0; | |
6372 | } | |
6373 | ||
e449de87 | 6374 | static int edit(int argc, char *argv[], void *userdata) { |
7d4fb3b1 RC |
6375 | _cleanup_strv_free_ char **names = NULL; |
6376 | _cleanup_strv_free_ char **paths = NULL; | |
6377 | char **original, **tmp; | |
4fbd7192 | 6378 | sd_bus *bus; |
7d4fb3b1 RC |
6379 | int r; |
6380 | ||
7d4fb3b1 | 6381 | if (!on_tty()) { |
4fbd7192 | 6382 | log_error("Cannot edit units if not on a tty."); |
7d4fb3b1 RC |
6383 | return -EINVAL; |
6384 | } | |
6385 | ||
6386 | if (arg_transport != BUS_TRANSPORT_LOCAL) { | |
4fbd7192 | 6387 | log_error("Cannot edit units remotely."); |
7d4fb3b1 RC |
6388 | return -EINVAL; |
6389 | } | |
6390 | ||
4fbd7192 LP |
6391 | r = acquire_bus(BUS_MANAGER, &bus); |
6392 | if (r < 0) | |
6393 | return r; | |
6394 | ||
e449de87 | 6395 | r = expand_names(bus, strv_skip(argv, 1), NULL, &names); |
7d4fb3b1 RC |
6396 | if (r < 0) |
6397 | return log_error_errno(r, "Failed to expand names: %m"); | |
6398 | ||
7d4fb3b1 RC |
6399 | r = find_paths_to_edit(bus, names, &paths); |
6400 | if (r < 0) | |
6401 | return r; | |
6402 | ||
b5e6a600 | 6403 | if (strv_isempty(paths)) |
7d4fb3b1 | 6404 | return -ENOENT; |
7d4fb3b1 RC |
6405 | |
6406 | r = run_editor(paths); | |
6407 | if (r < 0) | |
6408 | goto end; | |
6409 | ||
6410 | STRV_FOREACH_PAIR(original, tmp, paths) { | |
45519fd6 LP |
6411 | /* If the temporary file is empty we ignore it. It's |
6412 | * useful if the user wants to cancel its modification | |
7d4fb3b1 RC |
6413 | */ |
6414 | if (null_or_empty_path(*tmp)) { | |
45519fd6 | 6415 | log_warning("Editing \"%s\" canceled: temporary file is empty.", *original); |
7d4fb3b1 RC |
6416 | continue; |
6417 | } | |
45519fd6 | 6418 | |
7d4fb3b1 RC |
6419 | r = rename(*tmp, *original); |
6420 | if (r < 0) { | |
029009d4 | 6421 | r = log_error_errno(errno, "Failed to rename \"%s\" to \"%s\": %m", *tmp, *original); |
7d4fb3b1 RC |
6422 | goto end; |
6423 | } | |
6424 | } | |
6425 | ||
45519fd6 LP |
6426 | r = 0; |
6427 | ||
6428 | if (!arg_no_reload && !install_client_side()) | |
e449de87 | 6429 | r = daemon_reload(argc, argv, userdata); |
7d4fb3b1 RC |
6430 | |
6431 | end: | |
5f18271e | 6432 | STRV_FOREACH_PAIR(original, tmp, paths) { |
45519fd6 | 6433 | (void) unlink(*tmp); |
7d4fb3b1 | 6434 | |
5f18271e RC |
6435 | /* Removing empty dropin dirs */ |
6436 | if (!arg_full) { | |
043717f9 RC |
6437 | _cleanup_free_ char *dir; |
6438 | ||
6439 | dir = dirname_malloc(*original); | |
6440 | if (!dir) | |
6441 | return log_oom(); | |
6442 | ||
5f18271e RC |
6443 | /* no need to check if the dir is empty, rmdir |
6444 | * does nothing if it is not the case. | |
6445 | */ | |
6446 | (void) rmdir(dir); | |
6447 | } | |
6448 | } | |
6449 | ||
7d4fb3b1 RC |
6450 | return r; |
6451 | } | |
6452 | ||
601185b4 | 6453 | static void systemctl_help(void) { |
7e4249b9 | 6454 | |
ea4b98e6 | 6455 | pager_open(arg_no_pager, false); |
729e3769 | 6456 | |
2e33c433 | 6457 | printf("%s [OPTIONS...] {COMMAND} ...\n\n" |
729e3769 | 6458 | "Query or send control commands to the systemd manager.\n\n" |
8a0867d6 LP |
6459 | " -h --help Show this help\n" |
6460 | " --version Show package version\n" | |
f459b602 MAP |
6461 | " --system Connect to system manager\n" |
6462 | " --user Connect to user service manager\n" | |
6463 | " -H --host=[USER@]HOST\n" | |
6464 | " Operate on remote host\n" | |
6465 | " -M --machine=CONTAINER\n" | |
6466 | " Operate on local container\n" | |
3fb90db2 ZJS |
6467 | " -t --type=TYPE List units of a particular type\n" |
6468 | " --state=STATE List units with particular LOAD or SUB or ACTIVE state\n" | |
8a0867d6 | 6469 | " -p --property=NAME Show only properties by this name\n" |
a5e4972c LP |
6470 | " -a --all Show all loaded units/properties, including dead/empty\n" |
6471 | " ones. To list all units installed on the system, use\n" | |
6472 | " the 'list-unit-files' command instead.\n" | |
98a6e132 | 6473 | " -l --full Don't ellipsize unit names on output\n" |
1238ee09 | 6474 | " -r --recursive Show unit list of host and local containers\n" |
4dc5b821 LP |
6475 | " --reverse Show reverse dependencies with 'list-dependencies'\n" |
6476 | " --job-mode=MODE Specify how to deal with already queued jobs, when\n" | |
6477 | " queueing a new job\n" | |
a521ae4a | 6478 | " --show-types When showing sockets, explicitly show their type\n" |
4f9a9105 | 6479 | " --value When showing properties, only print the value\n" |
b37844d3 LP |
6480 | " -i --ignore-inhibitors\n" |
6481 | " When shutting down or sleeping, ignore inhibitors\n" | |
a8f11321 LP |
6482 | " --kill-who=WHO Who to send signal to\n" |
6483 | " -s --signal=SIGNAL Which signal to send\n" | |
57ab2eab | 6484 | " --now Start or stop unit in addition to enabling or disabling it\n" |
8a0867d6 LP |
6485 | " -q --quiet Suppress output\n" |
6486 | " --no-block Do not wait until operation finished\n" | |
8a0867d6 | 6487 | " --no-wall Don't send wall message before halt/power-off/reboot\n" |
3fb90db2 | 6488 | " --no-reload Don't reload daemon after en-/dis-abling unit files\n" |
ebed32bf | 6489 | " --no-legend Do not print a legend (column headers and hints)\n" |
69fc152f | 6490 | " --no-pager Do not pipe output into a pager\n" |
501fc174 LP |
6491 | " --no-ask-password\n" |
6492 | " Do not ask for system passwords\n" | |
a8f11321 | 6493 | " --global Enable/disable unit files globally\n" |
a521ae4a | 6494 | " --runtime Enable unit files only temporarily until next reboot\n" |
8a0867d6 LP |
6495 | " -f --force When enabling unit files, override existing symlinks\n" |
6496 | " When shutting down, execute action immediately\n" | |
3fb90db2 | 6497 | " --preset-mode= Apply only enable, only disable, or all presets\n" |
729e3769 | 6498 | " --root=PATH Enable unit files in the specified root directory\n" |
76fdc966 | 6499 | " -n --lines=INTEGER Number of journal entries to show\n" |
86d81e50 RC |
6500 | " -o --output=STRING Change journal output mode (short, short-iso,\n" |
6501 | " short-precise, short-monotonic, verbose,\n" | |
6502 | " export, json, json-pretty, json-sse, cat)\n" | |
5bdf2243 | 6503 | " --firmware-setup Tell the firmware to show the setup menu on next boot\n" |
815ebc54 | 6504 | " --plain Print unit dependencies as a list instead of a tree\n\n" |
34c4b47b | 6505 | "Unit Commands:\n" |
d8fba7c6 ZJS |
6506 | " list-units [PATTERN...] List loaded units\n" |
6507 | " list-sockets [PATTERN...] List loaded sockets ordered by address\n" | |
6508 | " list-timers [PATTERN...] List loaded timers ordered by next elapse\n" | |
4f8f66cb ZJS |
6509 | " start NAME... Start (activate) one or more units\n" |
6510 | " stop NAME... Stop (deactivate) one or more units\n" | |
6511 | " reload NAME... Reload one or more units\n" | |
6512 | " restart NAME... Start or restart one or more units\n" | |
6513 | " try-restart NAME... Restart one or more units if active\n" | |
6514 | " reload-or-restart NAME... Reload one or more units if possible,\n" | |
6f28c033 | 6515 | " otherwise start or restart\n" |
aabf5d42 LP |
6516 | " try-reload-or-restart NAME... If active, reload one or more units,\n" |
6517 | " if supported, otherwise restart\n" | |
4f8f66cb ZJS |
6518 | " isolate NAME Start one unit and stop all others\n" |
6519 | " kill NAME... Send signal to processes of a unit\n" | |
b3ae710c ZJS |
6520 | " is-active PATTERN... Check whether units are active\n" |
6521 | " is-failed PATTERN... Check whether units are failed\n" | |
6522 | " status [PATTERN...|PID...] Show runtime status of one or more units\n" | |
6523 | " show [PATTERN...|JOB...] Show properties of one or more\n" | |
ee5762e3 | 6524 | " units/jobs or the manager\n" |
b3ae710c | 6525 | " cat PATTERN... Show files and drop-ins of one or more units\n" |
4f8f66cb | 6526 | " set-property NAME ASSIGNMENT... Sets one or more properties of a unit\n" |
b3ae710c ZJS |
6527 | " help PATTERN...|PID... Show manual for one or more units\n" |
6528 | " reset-failed [PATTERN...] Reset failed state for all, one, or more\n" | |
fdf20a31 | 6529 | " units\n" |
55c0b89c | 6530 | " list-dependencies [NAME] Recursively show units which are required\n" |
afba4199 ZJS |
6531 | " or wanted by this unit or by which this\n" |
6532 | " unit is required or wanted\n\n" | |
34c4b47b | 6533 | "Unit File Commands:\n" |
d8fba7c6 | 6534 | " list-unit-files [PATTERN...] List installed unit files\n" |
4f8f66cb ZJS |
6535 | " enable NAME... Enable one or more unit files\n" |
6536 | " disable NAME... Disable one or more unit files\n" | |
6537 | " reenable NAME... Reenable one or more unit files\n" | |
6538 | " preset NAME... Enable/disable one or more unit files\n" | |
729e3769 | 6539 | " based on preset configuration\n" |
d309c1c3 LP |
6540 | " preset-all Enable/disable all unit files based on\n" |
6541 | " preset configuration\n" | |
b619ec8f | 6542 | " is-enabled NAME... Check whether unit files are enabled\n" |
4f8f66cb ZJS |
6543 | " mask NAME... Mask one or more units\n" |
6544 | " unmask NAME... Unmask one or more units\n" | |
6545 | " link PATH... Link one or more units files into\n" | |
729e3769 | 6546 | " the search path\n" |
344ca755 LP |
6547 | " revert NAME... Revert one or more unit files to vendor\n" |
6548 | " version\n" | |
e94937df LN |
6549 | " add-wants TARGET NAME... Add 'Wants' dependency for the target\n" |
6550 | " on specified one or more units\n" | |
6551 | " add-requires TARGET NAME... Add 'Requires' dependency for the target\n" | |
6552 | " on specified one or more units\n" | |
7d4fb3b1 | 6553 | " edit NAME... Edit one or more unit files\n" |
b619ec8f LP |
6554 | " get-default Get the name of the default target\n" |
6555 | " set-default NAME Set the default target\n\n" | |
0d292f5e LP |
6556 | "Machine Commands:\n" |
6557 | " list-machines [PATTERN...] List local containers and host\n\n" | |
34c4b47b | 6558 | "Job Commands:\n" |
d8fba7c6 | 6559 | " list-jobs [PATTERN...] List jobs\n" |
34c4b47b | 6560 | " cancel [JOB...] Cancel all, one, or more jobs\n\n" |
34c4b47b | 6561 | "Environment Commands:\n" |
7e4249b9 | 6562 | " show-environment Dump environment\n" |
4f8f66cb | 6563 | " set-environment NAME=VALUE... Set one or more environment variables\n" |
ac3efa8a | 6564 | " unset-environment NAME... Unset one or more environment variables\n" |
3fb90db2 | 6565 | " import-environment [NAME...] Import all or some environment variables\n\n" |
34c4b47b LP |
6566 | "Manager Lifecycle Commands:\n" |
6567 | " daemon-reload Reload systemd manager configuration\n" | |
6568 | " daemon-reexec Reexecute systemd manager\n\n" | |
6569 | "System Commands:\n" | |
99813a19 | 6570 | " is-system-running Check whether system is fully running\n" |
20b09ca7 LP |
6571 | " default Enter system default mode\n" |
6572 | " rescue Enter system rescue mode\n" | |
6573 | " emergency Enter system emergency mode\n" | |
514f4ef5 | 6574 | " halt Shut down and halt the system\n" |
2e33c433 | 6575 | " poweroff Shut down and power-off the system\n" |
37185ec8 | 6576 | " reboot [ARG] Shut down and reboot the system\n" |
20b09ca7 | 6577 | " kexec Shut down and reboot the system with kexec\n" |
287419c1 | 6578 | " exit [EXIT_CODE] Request user instance or container exit\n" |
4f8f66cb | 6579 | " switch-root ROOT [INIT] Change to a different root file system\n" |
6edd7d0a | 6580 | " suspend Suspend the system\n" |
6524990f LP |
6581 | " hibernate Hibernate the system\n" |
6582 | " hybrid-sleep Hibernate and suspend the system\n", | |
5b6319dc | 6583 | program_invocation_short_name); |
7e4249b9 LP |
6584 | } |
6585 | ||
601185b4 | 6586 | static void halt_help(void) { |
37185ec8 | 6587 | printf("%s [OPTIONS...]%s\n\n" |
e4b61340 LP |
6588 | "%s the system.\n\n" |
6589 | " --help Show this help\n" | |
6590 | " --halt Halt the machine\n" | |
6591 | " -p --poweroff Switch off the machine\n" | |
6592 | " --reboot Reboot the machine\n" | |
2e33c433 LP |
6593 | " -f --force Force immediate halt/power-off/reboot\n" |
6594 | " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n" | |
e4b61340 | 6595 | " -d --no-wtmp Don't write wtmp record\n" |
2e33c433 | 6596 | " --no-wall Don't send wall message before halt/power-off/reboot\n", |
e4b61340 | 6597 | program_invocation_short_name, |
37185ec8 | 6598 | arg_action == ACTION_REBOOT ? " [ARG]" : "", |
e4b61340 LP |
6599 | arg_action == ACTION_REBOOT ? "Reboot" : |
6600 | arg_action == ACTION_POWEROFF ? "Power off" : | |
6601 | "Halt"); | |
e4b61340 LP |
6602 | } |
6603 | ||
601185b4 | 6604 | static void shutdown_help(void) { |
08e4b1c5 | 6605 | printf("%s [OPTIONS...] [TIME] [WALL...]\n\n" |
e4b61340 LP |
6606 | "Shut down the system.\n\n" |
6607 | " --help Show this help\n" | |
6608 | " -H --halt Halt the machine\n" | |
6609 | " -P --poweroff Power-off the machine\n" | |
6610 | " -r --reboot Reboot the machine\n" | |
386da858 | 6611 | " -h Equivalent to --poweroff, overridden by --halt\n" |
2e33c433 | 6612 | " -k Don't halt/power-off/reboot, just send warnings\n" |
f6144808 | 6613 | " --no-wall Don't send wall message before halt/power-off/reboot\n" |
f6144808 | 6614 | " -c Cancel a pending shutdown\n", |
e4b61340 | 6615 | program_invocation_short_name); |
e4b61340 LP |
6616 | } |
6617 | ||
601185b4 | 6618 | static void telinit_help(void) { |
2e33c433 | 6619 | printf("%s [OPTIONS...] {COMMAND}\n\n" |
514f4ef5 LP |
6620 | "Send control commands to the init daemon.\n\n" |
6621 | " --help Show this help\n" | |
2e33c433 | 6622 | " --no-wall Don't send wall message before halt/power-off/reboot\n\n" |
e4b61340 LP |
6623 | "Commands:\n" |
6624 | " 0 Power-off the machine\n" | |
6625 | " 6 Reboot the machine\n" | |
514f4ef5 LP |
6626 | " 2, 3, 4, 5 Start runlevelX.target unit\n" |
6627 | " 1, s, S Enter rescue mode\n" | |
6628 | " q, Q Reload init daemon configuration\n" | |
6629 | " u, U Reexecute init daemon\n", | |
e4b61340 | 6630 | program_invocation_short_name); |
e4b61340 LP |
6631 | } |
6632 | ||
601185b4 | 6633 | static void runlevel_help(void) { |
2e33c433 | 6634 | printf("%s [OPTIONS...]\n\n" |
e4b61340 LP |
6635 | "Prints the previous and current runlevel of the init system.\n\n" |
6636 | " --help Show this help\n", | |
6637 | program_invocation_short_name); | |
e4b61340 LP |
6638 | } |
6639 | ||
b93312f5 | 6640 | static void help_types(void) { |
45c0c61d ZJS |
6641 | int i; |
6642 | ||
b93312f5 ZJS |
6643 | if (!arg_no_legend) |
6644 | puts("Available unit types:"); | |
e16972e6 ZJS |
6645 | for (i = 0; i < _UNIT_TYPE_MAX; i++) |
6646 | puts(unit_type_to_string(i)); | |
6647 | } | |
6648 | ||
6649 | static void help_states(void) { | |
6650 | int i; | |
6651 | ||
6652 | if (!arg_no_legend) | |
6653 | puts("Available unit load states:"); | |
6654 | for (i = 0; i < _UNIT_LOAD_STATE_MAX; i++) | |
6655 | puts(unit_load_state_to_string(i)); | |
6656 | ||
6657 | if (!arg_no_legend) | |
6658 | puts("\nAvailable unit active states:"); | |
6659 | for (i = 0; i < _UNIT_ACTIVE_STATE_MAX; i++) | |
6660 | puts(unit_active_state_to_string(i)); | |
7e55de3b ZJS |
6661 | |
6662 | if (!arg_no_legend) | |
6663 | puts("\nAvailable automount unit substates:"); | |
6664 | for (i = 0; i < _AUTOMOUNT_STATE_MAX; i++) | |
6665 | puts(automount_state_to_string(i)); | |
6666 | ||
6667 | if (!arg_no_legend) | |
6668 | puts("\nAvailable busname unit substates:"); | |
6669 | for (i = 0; i < _BUSNAME_STATE_MAX; i++) | |
6670 | puts(busname_state_to_string(i)); | |
6671 | ||
6672 | if (!arg_no_legend) | |
6673 | puts("\nAvailable device unit substates:"); | |
6674 | for (i = 0; i < _DEVICE_STATE_MAX; i++) | |
6675 | puts(device_state_to_string(i)); | |
6676 | ||
6677 | if (!arg_no_legend) | |
6678 | puts("\nAvailable mount unit substates:"); | |
6679 | for (i = 0; i < _MOUNT_STATE_MAX; i++) | |
6680 | puts(mount_state_to_string(i)); | |
6681 | ||
6682 | if (!arg_no_legend) | |
6683 | puts("\nAvailable path unit substates:"); | |
6684 | for (i = 0; i < _PATH_STATE_MAX; i++) | |
6685 | puts(path_state_to_string(i)); | |
6686 | ||
6687 | if (!arg_no_legend) | |
6688 | puts("\nAvailable scope unit substates:"); | |
6689 | for (i = 0; i < _SCOPE_STATE_MAX; i++) | |
6690 | puts(scope_state_to_string(i)); | |
6691 | ||
6692 | if (!arg_no_legend) | |
6693 | puts("\nAvailable service unit substates:"); | |
6694 | for (i = 0; i < _SERVICE_STATE_MAX; i++) | |
6695 | puts(service_state_to_string(i)); | |
6696 | ||
6697 | if (!arg_no_legend) | |
6698 | puts("\nAvailable slice unit substates:"); | |
6699 | for (i = 0; i < _SLICE_STATE_MAX; i++) | |
6700 | puts(slice_state_to_string(i)); | |
6701 | ||
7e55de3b ZJS |
6702 | if (!arg_no_legend) |
6703 | puts("\nAvailable socket unit substates:"); | |
6704 | for (i = 0; i < _SOCKET_STATE_MAX; i++) | |
6705 | puts(socket_state_to_string(i)); | |
6706 | ||
6707 | if (!arg_no_legend) | |
6708 | puts("\nAvailable swap unit substates:"); | |
6709 | for (i = 0; i < _SWAP_STATE_MAX; i++) | |
6710 | puts(swap_state_to_string(i)); | |
6711 | ||
6712 | if (!arg_no_legend) | |
6713 | puts("\nAvailable target unit substates:"); | |
6714 | for (i = 0; i < _TARGET_STATE_MAX; i++) | |
6715 | puts(target_state_to_string(i)); | |
6716 | ||
6717 | if (!arg_no_legend) | |
6718 | puts("\nAvailable timer unit substates:"); | |
6719 | for (i = 0; i < _TIMER_STATE_MAX; i++) | |
6720 | puts(timer_state_to_string(i)); | |
45c0c61d ZJS |
6721 | } |
6722 | ||
e4b61340 | 6723 | static int systemctl_parse_argv(int argc, char *argv[]) { |
7e4249b9 LP |
6724 | |
6725 | enum { | |
90d473a1 | 6726 | ARG_FAIL = 0x100, |
afba4199 ZJS |
6727 | ARG_REVERSE, |
6728 | ARG_AFTER, | |
6729 | ARG_BEFORE, | |
991f2a39 | 6730 | ARG_SHOW_TYPES, |
23ade460 | 6731 | ARG_IRREVERSIBLE, |
e67c3609 | 6732 | ARG_IGNORE_DEPENDENCIES, |
4f9a9105 | 6733 | ARG_VALUE, |
35df8f27 | 6734 | ARG_VERSION, |
af2d49f7 | 6735 | ARG_USER, |
7e4249b9 | 6736 | ARG_SYSTEM, |
ee5762e3 | 6737 | ARG_GLOBAL, |
6e905d93 | 6738 | ARG_NO_BLOCK, |
ebed32bf | 6739 | ARG_NO_LEGEND, |
611efaac | 6740 | ARG_NO_PAGER, |
4445a875 | 6741 | ARG_NO_WALL, |
be394c48 | 6742 | ARG_ROOT, |
ee5762e3 | 6743 | ARG_NO_RELOAD, |
501fc174 | 6744 | ARG_KILL_WHO, |
30732560 | 6745 | ARG_NO_ASK_PASSWORD, |
729e3769 | 6746 | ARG_FAILED, |
df50185b | 6747 | ARG_RUNTIME, |
5d0c05e5 | 6748 | ARG_FORCE, |
9b9b3d36 | 6749 | ARG_PLAIN, |
4dc5b821 | 6750 | ARG_STATE, |
d309c1c3 LP |
6751 | ARG_JOB_MODE, |
6752 | ARG_PRESET_MODE, | |
5bdf2243 | 6753 | ARG_FIRMWARE_SETUP, |
57ab2eab | 6754 | ARG_NOW, |
9ef15026 | 6755 | ARG_MESSAGE, |
7e4249b9 LP |
6756 | }; |
6757 | ||
6758 | static const struct option options[] = { | |
9ea9d4cf LP |
6759 | { "help", no_argument, NULL, 'h' }, |
6760 | { "version", no_argument, NULL, ARG_VERSION }, | |
6761 | { "type", required_argument, NULL, 't' }, | |
6762 | { "property", required_argument, NULL, 'p' }, | |
6763 | { "all", no_argument, NULL, 'a' }, | |
6764 | { "reverse", no_argument, NULL, ARG_REVERSE }, | |
6765 | { "after", no_argument, NULL, ARG_AFTER }, | |
6766 | { "before", no_argument, NULL, ARG_BEFORE }, | |
6767 | { "show-types", no_argument, NULL, ARG_SHOW_TYPES }, | |
6768 | { "failed", no_argument, NULL, ARG_FAILED }, /* compatibility only */ | |
6769 | { "full", no_argument, NULL, 'l' }, | |
4dc5b821 LP |
6770 | { "job-mode", required_argument, NULL, ARG_JOB_MODE }, |
6771 | { "fail", no_argument, NULL, ARG_FAIL }, /* compatibility only */ | |
6772 | { "irreversible", no_argument, NULL, ARG_IRREVERSIBLE }, /* compatibility only */ | |
6773 | { "ignore-dependencies", no_argument, NULL, ARG_IGNORE_DEPENDENCIES }, /* compatibility only */ | |
9ea9d4cf | 6774 | { "ignore-inhibitors", no_argument, NULL, 'i' }, |
4f9a9105 | 6775 | { "value", no_argument, NULL, ARG_VALUE }, |
9ea9d4cf LP |
6776 | { "user", no_argument, NULL, ARG_USER }, |
6777 | { "system", no_argument, NULL, ARG_SYSTEM }, | |
6778 | { "global", no_argument, NULL, ARG_GLOBAL }, | |
6779 | { "no-block", no_argument, NULL, ARG_NO_BLOCK }, | |
6780 | { "no-legend", no_argument, NULL, ARG_NO_LEGEND }, | |
6781 | { "no-pager", no_argument, NULL, ARG_NO_PAGER }, | |
6782 | { "no-wall", no_argument, NULL, ARG_NO_WALL }, | |
6783 | { "quiet", no_argument, NULL, 'q' }, | |
6784 | { "root", required_argument, NULL, ARG_ROOT }, | |
6785 | { "force", no_argument, NULL, ARG_FORCE }, | |
6786 | { "no-reload", no_argument, NULL, ARG_NO_RELOAD }, | |
6787 | { "kill-who", required_argument, NULL, ARG_KILL_WHO }, | |
6788 | { "signal", required_argument, NULL, 's' }, | |
6789 | { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD }, | |
6790 | { "host", required_argument, NULL, 'H' }, | |
f459b602 | 6791 | { "machine", required_argument, NULL, 'M' }, |
9ea9d4cf LP |
6792 | { "runtime", no_argument, NULL, ARG_RUNTIME }, |
6793 | { "lines", required_argument, NULL, 'n' }, | |
6794 | { "output", required_argument, NULL, 'o' }, | |
6795 | { "plain", no_argument, NULL, ARG_PLAIN }, | |
6796 | { "state", required_argument, NULL, ARG_STATE }, | |
1238ee09 | 6797 | { "recursive", no_argument, NULL, 'r' }, |
d309c1c3 | 6798 | { "preset-mode", required_argument, NULL, ARG_PRESET_MODE }, |
5bdf2243 | 6799 | { "firmware-setup", no_argument, NULL, ARG_FIRMWARE_SETUP }, |
57ab2eab | 6800 | { "now", no_argument, NULL, ARG_NOW }, |
9ef15026 | 6801 | { "message", required_argument, NULL, ARG_MESSAGE }, |
eb9da376 | 6802 | {} |
7e4249b9 LP |
6803 | }; |
6804 | ||
5ab22f33 | 6805 | const char *p; |
0f03c2a4 | 6806 | int c, r; |
7e4249b9 | 6807 | |
e4b61340 | 6808 | assert(argc >= 0); |
7e4249b9 LP |
6809 | assert(argv); |
6810 | ||
16f017fa IS |
6811 | /* we default to allowing interactive authorization only in systemctl (not in the legacy commands) */ |
6812 | arg_ask_password = true; | |
6813 | ||
601185b4 | 6814 | while ((c = getopt_long(argc, argv, "ht:p:alqfs:H:M:n:o:ir", options, NULL)) >= 0) |
7e4249b9 LP |
6815 | |
6816 | switch (c) { | |
6817 | ||
6818 | case 'h': | |
601185b4 ZJS |
6819 | systemctl_help(); |
6820 | return 0; | |
35df8f27 LP |
6821 | |
6822 | case ARG_VERSION: | |
3f6fd1ba | 6823 | return version(); |
7e4249b9 | 6824 | |
20b3f379 | 6825 | case 't': { |
bf409580 TA |
6826 | if (isempty(optarg)) { |
6827 | log_error("--type requires arguments."); | |
6828 | return -EINVAL; | |
6829 | } | |
45c0c61d | 6830 | |
5ab22f33 | 6831 | p = optarg; |
9ed794a3 | 6832 | for (;;) { |
5ab22f33 | 6833 | _cleanup_free_ char *type = NULL; |
20b3f379 | 6834 | |
5ab22f33 SS |
6835 | r = extract_first_word(&p, &type, ",", 0); |
6836 | if (r < 0) | |
6837 | return log_error_errno(r, "Failed to parse type: %s", optarg); | |
6838 | ||
6839 | if (r == 0) | |
6840 | break; | |
20b3f379 ZJS |
6841 | |
6842 | if (streq(type, "help")) { | |
6843 | help_types(); | |
6844 | return 0; | |
6845 | } | |
6846 | ||
6847 | if (unit_type_from_string(type) >= 0) { | |
7e9d36e0 | 6848 | if (strv_push(&arg_types, type) < 0) |
20b3f379 ZJS |
6849 | return log_oom(); |
6850 | type = NULL; | |
6851 | continue; | |
6852 | } | |
6853 | ||
9b9b3d36 MW |
6854 | /* It's much nicer to use --state= for |
6855 | * load states, but let's support this | |
6856 | * in --types= too for compatibility | |
6857 | * with old versions */ | |
7e9d36e0 | 6858 | if (unit_load_state_from_string(type) >= 0) { |
9b9b3d36 | 6859 | if (strv_push(&arg_states, type) < 0) |
20b3f379 ZJS |
6860 | return log_oom(); |
6861 | type = NULL; | |
6862 | continue; | |
6863 | } | |
6864 | ||
ab06eef8 | 6865 | log_error("Unknown unit type or load state '%s'.", type); |
20b3f379 ZJS |
6866 | log_info("Use -t help to see a list of allowed values."); |
6867 | return -EINVAL; | |
c147dc42 | 6868 | } |
20b3f379 ZJS |
6869 | |
6870 | break; | |
6871 | } | |
6872 | ||
ea4a240d | 6873 | case 'p': { |
033a842c ZJS |
6874 | /* Make sure that if the empty property list |
6875 | was specified, we won't show any properties. */ | |
20b3f379 | 6876 | if (isempty(optarg) && !arg_properties) { |
cbc9fbd1 | 6877 | arg_properties = new0(char*, 1); |
20b3f379 ZJS |
6878 | if (!arg_properties) |
6879 | return log_oom(); | |
6880 | } else { | |
5ab22f33 | 6881 | p = optarg; |
9ed794a3 | 6882 | for (;;) { |
5ab22f33 | 6883 | _cleanup_free_ char *prop = NULL; |
033a842c | 6884 | |
5ab22f33 SS |
6885 | r = extract_first_word(&p, &prop, ",", 0); |
6886 | if (r < 0) | |
6887 | return log_error_errno(r, "Failed to parse property: %s", optarg); | |
033a842c | 6888 | |
5ab22f33 SS |
6889 | if (r == 0) |
6890 | break; | |
ea4a240d | 6891 | |
5ab22f33 | 6892 | if (strv_push(&arg_properties, prop) < 0) |
20b3f379 | 6893 | return log_oom(); |
5ab22f33 SS |
6894 | |
6895 | prop = NULL; | |
20b3f379 | 6896 | } |
033a842c | 6897 | } |
48220598 LP |
6898 | |
6899 | /* If the user asked for a particular | |
6900 | * property, show it to him, even if it is | |
6901 | * empty. */ | |
6902 | arg_all = true; | |
033a842c | 6903 | |
48220598 | 6904 | break; |
ea4a240d | 6905 | } |
48220598 | 6906 | |
7e4249b9 LP |
6907 | case 'a': |
6908 | arg_all = true; | |
6909 | break; | |
6910 | ||
afba4199 ZJS |
6911 | case ARG_REVERSE: |
6912 | arg_dependency = DEPENDENCY_REVERSE; | |
6913 | break; | |
6914 | ||
6915 | case ARG_AFTER: | |
6916 | arg_dependency = DEPENDENCY_AFTER; | |
6917 | break; | |
6918 | ||
6919 | case ARG_BEFORE: | |
6920 | arg_dependency = DEPENDENCY_BEFORE; | |
6921 | break; | |
6922 | ||
991f2a39 ZJS |
6923 | case ARG_SHOW_TYPES: |
6924 | arg_show_types = true; | |
6925 | break; | |
6926 | ||
4f9a9105 ZJS |
6927 | case ARG_VALUE: |
6928 | arg_value = true; | |
6929 | break; | |
6930 | ||
4dc5b821 LP |
6931 | case ARG_JOB_MODE: |
6932 | arg_job_mode = optarg; | |
6933 | break; | |
6934 | ||
90d473a1 | 6935 | case ARG_FAIL: |
e67c3609 LP |
6936 | arg_job_mode = "fail"; |
6937 | break; | |
6938 | ||
23ade460 MS |
6939 | case ARG_IRREVERSIBLE: |
6940 | arg_job_mode = "replace-irreversibly"; | |
6941 | break; | |
6942 | ||
e67c3609 LP |
6943 | case ARG_IGNORE_DEPENDENCIES: |
6944 | arg_job_mode = "ignore-dependencies"; | |
7e4249b9 LP |
6945 | break; |
6946 | ||
af2d49f7 | 6947 | case ARG_USER: |
729e3769 | 6948 | arg_scope = UNIT_FILE_USER; |
7e4249b9 LP |
6949 | break; |
6950 | ||
6951 | case ARG_SYSTEM: | |
729e3769 LP |
6952 | arg_scope = UNIT_FILE_SYSTEM; |
6953 | break; | |
6954 | ||
6955 | case ARG_GLOBAL: | |
6956 | arg_scope = UNIT_FILE_GLOBAL; | |
7e4249b9 LP |
6957 | break; |
6958 | ||
6e905d93 LP |
6959 | case ARG_NO_BLOCK: |
6960 | arg_no_block = true; | |
7e4249b9 LP |
6961 | break; |
6962 | ||
ebed32bf MS |
6963 | case ARG_NO_LEGEND: |
6964 | arg_no_legend = true; | |
6965 | break; | |
6966 | ||
611efaac LP |
6967 | case ARG_NO_PAGER: |
6968 | arg_no_pager = true; | |
6969 | break; | |
0736af98 | 6970 | |
514f4ef5 LP |
6971 | case ARG_NO_WALL: |
6972 | arg_no_wall = true; | |
6973 | break; | |
6974 | ||
be394c48 | 6975 | case ARG_ROOT: |
bac75eb3 | 6976 | r = parse_path_argument_and_warn(optarg, false, &arg_root); |
0f03c2a4 LP |
6977 | if (r < 0) |
6978 | return r; | |
be394c48 FC |
6979 | break; |
6980 | ||
98a6e132 | 6981 | case 'l': |
8fe914ec LP |
6982 | arg_full = true; |
6983 | break; | |
6984 | ||
30732560 | 6985 | case ARG_FAILED: |
9b9b3d36 MW |
6986 | if (strv_extend(&arg_states, "failed") < 0) |
6987 | return log_oom(); | |
6988 | ||
30732560 LP |
6989 | break; |
6990 | ||
0183528f LP |
6991 | case 'q': |
6992 | arg_quiet = true; | |
6993 | break; | |
6994 | ||
568b679f | 6995 | case ARG_FORCE: |
313cefa1 | 6996 | arg_force++; |
568b679f LP |
6997 | break; |
6998 | ||
b4f27ccc | 6999 | case 'f': |
313cefa1 | 7000 | arg_force++; |
ee5762e3 LP |
7001 | break; |
7002 | ||
7003 | case ARG_NO_RELOAD: | |
7004 | arg_no_reload = true; | |
7005 | break; | |
7006 | ||
8a0867d6 LP |
7007 | case ARG_KILL_WHO: |
7008 | arg_kill_who = optarg; | |
7009 | break; | |
7010 | ||
8a0867d6 | 7011 | case 's': |
691395d8 LP |
7012 | arg_signal = signal_from_string_try_harder(optarg); |
7013 | if (arg_signal < 0) { | |
8a0867d6 LP |
7014 | log_error("Failed to parse signal string %s.", optarg); |
7015 | return -EINVAL; | |
7016 | } | |
7017 | break; | |
7018 | ||
501fc174 LP |
7019 | case ARG_NO_ASK_PASSWORD: |
7020 | arg_ask_password = false; | |
7021 | break; | |
7022 | ||
f459b602 MAP |
7023 | case 'H': |
7024 | arg_transport = BUS_TRANSPORT_REMOTE; | |
7025 | arg_host = optarg; | |
a8f11321 LP |
7026 | break; |
7027 | ||
f459b602 | 7028 | case 'M': |
de33fc62 | 7029 | arg_transport = BUS_TRANSPORT_MACHINE; |
f459b602 | 7030 | arg_host = optarg; |
a8f11321 LP |
7031 | break; |
7032 | ||
729e3769 LP |
7033 | case ARG_RUNTIME: |
7034 | arg_runtime = true; | |
7035 | break; | |
7036 | ||
df50185b LP |
7037 | case 'n': |
7038 | if (safe_atou(optarg, &arg_lines) < 0) { | |
7039 | log_error("Failed to parse lines '%s'", optarg); | |
7040 | return -EINVAL; | |
7041 | } | |
7042 | break; | |
7043 | ||
df50185b LP |
7044 | case 'o': |
7045 | arg_output = output_mode_from_string(optarg); | |
7046 | if (arg_output < 0) { | |
7047 | log_error("Unknown output '%s'.", optarg); | |
7048 | return -EINVAL; | |
7049 | } | |
7050 | break; | |
7051 | ||
b37844d3 LP |
7052 | case 'i': |
7053 | arg_ignore_inhibitors = true; | |
7054 | break; | |
7055 | ||
5d0c05e5 LN |
7056 | case ARG_PLAIN: |
7057 | arg_plain = true; | |
7058 | break; | |
7059 | ||
5bdf2243 JJ |
7060 | case ARG_FIRMWARE_SETUP: |
7061 | arg_firmware_setup = true; | |
7062 | break; | |
7063 | ||
9b9b3d36 | 7064 | case ARG_STATE: { |
bf409580 TA |
7065 | if (isempty(optarg)) { |
7066 | log_error("--signal requires arguments."); | |
7067 | return -EINVAL; | |
7068 | } | |
9b9b3d36 | 7069 | |
5ab22f33 | 7070 | p = optarg; |
9ed794a3 | 7071 | for (;;) { |
bc6c18fe | 7072 | _cleanup_free_ char *s = NULL; |
9b9b3d36 | 7073 | |
5ab22f33 SS |
7074 | r = extract_first_word(&p, &s, ",", 0); |
7075 | if (r < 0) | |
7076 | return log_error_errno(r, "Failed to parse signal: %s", optarg); | |
7077 | ||
7078 | if (r == 0) | |
7079 | break; | |
9b9b3d36 | 7080 | |
e16972e6 ZJS |
7081 | if (streq(s, "help")) { |
7082 | help_states(); | |
7083 | return 0; | |
7084 | } | |
7085 | ||
7e9d36e0 | 7086 | if (strv_push(&arg_states, s) < 0) |
9b9b3d36 | 7087 | return log_oom(); |
7e9d36e0 LP |
7088 | |
7089 | s = NULL; | |
9b9b3d36 MW |
7090 | } |
7091 | break; | |
7092 | } | |
7093 | ||
1238ee09 LP |
7094 | case 'r': |
7095 | if (geteuid() != 0) { | |
f1721625 | 7096 | log_error("--recursive requires root privileges."); |
1238ee09 LP |
7097 | return -EPERM; |
7098 | } | |
7099 | ||
7100 | arg_recursive = true; | |
7101 | break; | |
7102 | ||
d309c1c3 LP |
7103 | case ARG_PRESET_MODE: |
7104 | ||
7105 | arg_preset_mode = unit_file_preset_mode_from_string(optarg); | |
7106 | if (arg_preset_mode < 0) { | |
7107 | log_error("Failed to parse preset mode: %s.", optarg); | |
7108 | return -EINVAL; | |
7109 | } | |
7110 | ||
7111 | break; | |
7112 | ||
57ab2eab JS |
7113 | case ARG_NOW: |
7114 | arg_now = true; | |
7115 | break; | |
7116 | ||
9ef15026 JS |
7117 | case ARG_MESSAGE: |
7118 | if (strv_extend(&arg_wall, optarg) < 0) | |
7119 | return log_oom(); | |
7120 | break; | |
7121 | ||
7e4249b9 LP |
7122 | case '?': |
7123 | return -EINVAL; | |
7124 | ||
7125 | default: | |
eb9da376 | 7126 | assert_not_reached("Unhandled option"); |
7e4249b9 | 7127 | } |
7e4249b9 | 7128 | |
f459b602 | 7129 | if (arg_transport != BUS_TRANSPORT_LOCAL && arg_scope != UNIT_FILE_SYSTEM) { |
a8f11321 LP |
7130 | log_error("Cannot access user instance remotely."); |
7131 | return -EINVAL; | |
7132 | } | |
7133 | ||
7e4249b9 LP |
7134 | return 1; |
7135 | } | |
7136 | ||
e4b61340 LP |
7137 | static int halt_parse_argv(int argc, char *argv[]) { |
7138 | ||
7139 | enum { | |
7140 | ARG_HELP = 0x100, | |
7141 | ARG_HALT, | |
514f4ef5 LP |
7142 | ARG_REBOOT, |
7143 | ARG_NO_WALL | |
e4b61340 LP |
7144 | }; |
7145 | ||
7146 | static const struct option options[] = { | |
7147 | { "help", no_argument, NULL, ARG_HELP }, | |
7148 | { "halt", no_argument, NULL, ARG_HALT }, | |
7149 | { "poweroff", no_argument, NULL, 'p' }, | |
7150 | { "reboot", no_argument, NULL, ARG_REBOOT }, | |
7151 | { "force", no_argument, NULL, 'f' }, | |
7152 | { "wtmp-only", no_argument, NULL, 'w' }, | |
7153 | { "no-wtmp", no_argument, NULL, 'd' }, | |
f3f054f0 | 7154 | { "no-sync", no_argument, NULL, 'n' }, |
514f4ef5 | 7155 | { "no-wall", no_argument, NULL, ARG_NO_WALL }, |
eb9da376 | 7156 | {} |
e4b61340 LP |
7157 | }; |
7158 | ||
37185ec8 | 7159 | int c, r, runlevel; |
e4b61340 LP |
7160 | |
7161 | assert(argc >= 0); | |
7162 | assert(argv); | |
7163 | ||
7164 | if (utmp_get_runlevel(&runlevel, NULL) >= 0) | |
7165 | if (runlevel == '0' || runlevel == '6') | |
65491fd8 | 7166 | arg_force = 2; |
e4b61340 | 7167 | |
601185b4 | 7168 | while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) |
e4b61340 LP |
7169 | switch (c) { |
7170 | ||
7171 | case ARG_HELP: | |
601185b4 ZJS |
7172 | halt_help(); |
7173 | return 0; | |
e4b61340 LP |
7174 | |
7175 | case ARG_HALT: | |
7176 | arg_action = ACTION_HALT; | |
7177 | break; | |
7178 | ||
7179 | case 'p': | |
a042efad MS |
7180 | if (arg_action != ACTION_REBOOT) |
7181 | arg_action = ACTION_POWEROFF; | |
e4b61340 LP |
7182 | break; |
7183 | ||
7184 | case ARG_REBOOT: | |
7185 | arg_action = ACTION_REBOOT; | |
7186 | break; | |
7187 | ||
7188 | case 'f': | |
65491fd8 | 7189 | arg_force = 2; |
e4b61340 LP |
7190 | break; |
7191 | ||
7192 | case 'w': | |
7193 | arg_dry = true; | |
7194 | break; | |
7195 | ||
7196 | case 'd': | |
7197 | arg_no_wtmp = true; | |
7198 | break; | |
7199 | ||
f3f054f0 TB |
7200 | case 'n': |
7201 | arg_no_sync = true; | |
7202 | break; | |
7203 | ||
514f4ef5 LP |
7204 | case ARG_NO_WALL: |
7205 | arg_no_wall = true; | |
7206 | break; | |
7207 | ||
e4b61340 LP |
7208 | case 'i': |
7209 | case 'h': | |
7210 | /* Compatibility nops */ | |
7211 | break; | |
7212 | ||
7213 | case '?': | |
7214 | return -EINVAL; | |
7215 | ||
7216 | default: | |
eb9da376 | 7217 | assert_not_reached("Unhandled option"); |
e4b61340 | 7218 | } |
e4b61340 | 7219 | |
c5220a94 | 7220 | if (arg_action == ACTION_REBOOT && (argc == optind || argc == optind + 1)) { |
27c06cb5 | 7221 | r = update_reboot_parameter_and_warn(argc == optind + 1 ? argv[optind] : NULL); |
c5220a94 | 7222 | if (r < 0) |
37185ec8 | 7223 | return r; |
37185ec8 | 7224 | } else if (optind < argc) { |
e4b61340 LP |
7225 | log_error("Too many arguments."); |
7226 | return -EINVAL; | |
7227 | } | |
7228 | ||
7229 | return 1; | |
7230 | } | |
7231 | ||
2cc7b0a2 | 7232 | static int parse_shutdown_time_spec(const char *t, usec_t *_u) { |
f6144808 LP |
7233 | assert(t); |
7234 | assert(_u); | |
7235 | ||
7236 | if (streq(t, "now")) | |
7237 | *_u = 0; | |
1a639877 | 7238 | else if (!strchr(t, ':')) { |
f6144808 LP |
7239 | uint64_t u; |
7240 | ||
1a639877 | 7241 | if (safe_atou64(t, &u) < 0) |
f6144808 LP |
7242 | return -EINVAL; |
7243 | ||
7244 | *_u = now(CLOCK_REALTIME) + USEC_PER_MINUTE * u; | |
7245 | } else { | |
7246 | char *e = NULL; | |
7247 | long hour, minute; | |
b92bea5d | 7248 | struct tm tm = {}; |
f6144808 LP |
7249 | time_t s; |
7250 | usec_t n; | |
7251 | ||
7252 | errno = 0; | |
7253 | hour = strtol(t, &e, 10); | |
8333c77e | 7254 | if (errno > 0 || *e != ':' || hour < 0 || hour > 23) |
f6144808 LP |
7255 | return -EINVAL; |
7256 | ||
7257 | minute = strtol(e+1, &e, 10); | |
8333c77e | 7258 | if (errno > 0 || *e != 0 || minute < 0 || minute > 59) |
f6144808 LP |
7259 | return -EINVAL; |
7260 | ||
7261 | n = now(CLOCK_REALTIME); | |
08e4b1c5 LP |
7262 | s = (time_t) (n / USEC_PER_SEC); |
7263 | ||
f6144808 LP |
7264 | assert_se(localtime_r(&s, &tm)); |
7265 | ||
7266 | tm.tm_hour = (int) hour; | |
7267 | tm.tm_min = (int) minute; | |
08e4b1c5 | 7268 | tm.tm_sec = 0; |
f6144808 LP |
7269 | |
7270 | assert_se(s = mktime(&tm)); | |
7271 | ||
7272 | *_u = (usec_t) s * USEC_PER_SEC; | |
7273 | ||
7274 | while (*_u <= n) | |
7275 | *_u += USEC_PER_DAY; | |
7276 | } | |
7277 | ||
7278 | return 0; | |
7279 | } | |
7280 | ||
e4b61340 LP |
7281 | static int shutdown_parse_argv(int argc, char *argv[]) { |
7282 | ||
7283 | enum { | |
7284 | ARG_HELP = 0x100, | |
514f4ef5 | 7285 | ARG_NO_WALL |
e4b61340 LP |
7286 | }; |
7287 | ||
7288 | static const struct option options[] = { | |
7289 | { "help", no_argument, NULL, ARG_HELP }, | |
7290 | { "halt", no_argument, NULL, 'H' }, | |
7291 | { "poweroff", no_argument, NULL, 'P' }, | |
7292 | { "reboot", no_argument, NULL, 'r' }, | |
04ebb595 | 7293 | { "kexec", no_argument, NULL, 'K' }, /* not documented extension */ |
514f4ef5 | 7294 | { "no-wall", no_argument, NULL, ARG_NO_WALL }, |
eb9da376 | 7295 | {} |
e4b61340 LP |
7296 | }; |
7297 | ||
172d7abf | 7298 | char **wall = NULL; |
f6144808 | 7299 | int c, r; |
e4b61340 LP |
7300 | |
7301 | assert(argc >= 0); | |
7302 | assert(argv); | |
7303 | ||
a4420f7b | 7304 | while ((c = getopt_long(argc, argv, "HPrhkKtafFc", options, NULL)) >= 0) |
e4b61340 LP |
7305 | switch (c) { |
7306 | ||
7307 | case ARG_HELP: | |
601185b4 ZJS |
7308 | shutdown_help(); |
7309 | return 0; | |
e4b61340 LP |
7310 | |
7311 | case 'H': | |
7312 | arg_action = ACTION_HALT; | |
7313 | break; | |
7314 | ||
7315 | case 'P': | |
7316 | arg_action = ACTION_POWEROFF; | |
7317 | break; | |
7318 | ||
7319 | case 'r': | |
5622dde3 KS |
7320 | if (kexec_loaded()) |
7321 | arg_action = ACTION_KEXEC; | |
7322 | else | |
7323 | arg_action = ACTION_REBOOT; | |
e4b61340 LP |
7324 | break; |
7325 | ||
04ebb595 LP |
7326 | case 'K': |
7327 | arg_action = ACTION_KEXEC; | |
7328 | break; | |
7329 | ||
e4b61340 LP |
7330 | case 'h': |
7331 | if (arg_action != ACTION_HALT) | |
7332 | arg_action = ACTION_POWEROFF; | |
7333 | break; | |
7334 | ||
7335 | case 'k': | |
7336 | arg_dry = true; | |
7337 | break; | |
7338 | ||
514f4ef5 LP |
7339 | case ARG_NO_WALL: |
7340 | arg_no_wall = true; | |
7341 | break; | |
7342 | ||
e4b61340 LP |
7343 | case 't': |
7344 | case 'a': | |
75836b9d JS |
7345 | case 'f': |
7346 | case 'F': | |
e4b61340 LP |
7347 | /* Compatibility nops */ |
7348 | break; | |
7349 | ||
f6144808 LP |
7350 | case 'c': |
7351 | arg_action = ACTION_CANCEL_SHUTDOWN; | |
7352 | break; | |
7353 | ||
e4b61340 LP |
7354 | case '?': |
7355 | return -EINVAL; | |
7356 | ||
7357 | default: | |
eb9da376 | 7358 | assert_not_reached("Unhandled option"); |
e4b61340 | 7359 | } |
e4b61340 | 7360 | |
dfcc5c33 | 7361 | if (argc > optind && arg_action != ACTION_CANCEL_SHUTDOWN) { |
2cc7b0a2 | 7362 | r = parse_shutdown_time_spec(argv[optind], &arg_when); |
7e59bfcb | 7363 | if (r < 0) { |
f6144808 LP |
7364 | log_error("Failed to parse time specification: %s", argv[optind]); |
7365 | return r; | |
7366 | } | |
6b5ad000 | 7367 | } else |
08e4b1c5 | 7368 | arg_when = now(CLOCK_REALTIME) + USEC_PER_MINUTE; |
442b9094 | 7369 | |
dfcc5c33 MS |
7370 | if (argc > optind && arg_action == ACTION_CANCEL_SHUTDOWN) |
7371 | /* No time argument for shutdown cancel */ | |
172d7abf | 7372 | wall = argv + optind; |
dfcc5c33 MS |
7373 | else if (argc > optind + 1) |
7374 | /* We skip the time argument */ | |
172d7abf LP |
7375 | wall = argv + optind + 1; |
7376 | ||
7377 | if (wall) { | |
7378 | arg_wall = strv_copy(wall); | |
7379 | if (!arg_wall) | |
7380 | return log_oom(); | |
7381 | } | |
e4b61340 LP |
7382 | |
7383 | optind = argc; | |
7384 | ||
7385 | return 1; | |
e4b61340 LP |
7386 | } |
7387 | ||
7388 | static int telinit_parse_argv(int argc, char *argv[]) { | |
7389 | ||
7390 | enum { | |
7391 | ARG_HELP = 0x100, | |
514f4ef5 | 7392 | ARG_NO_WALL |
e4b61340 LP |
7393 | }; |
7394 | ||
7395 | static const struct option options[] = { | |
7396 | { "help", no_argument, NULL, ARG_HELP }, | |
514f4ef5 | 7397 | { "no-wall", no_argument, NULL, ARG_NO_WALL }, |
eb9da376 | 7398 | {} |
e4b61340 LP |
7399 | }; |
7400 | ||
7401 | static const struct { | |
7402 | char from; | |
7403 | enum action to; | |
7404 | } table[] = { | |
7405 | { '0', ACTION_POWEROFF }, | |
7406 | { '6', ACTION_REBOOT }, | |
ef2f1067 | 7407 | { '1', ACTION_RESCUE }, |
e4b61340 LP |
7408 | { '2', ACTION_RUNLEVEL2 }, |
7409 | { '3', ACTION_RUNLEVEL3 }, | |
7410 | { '4', ACTION_RUNLEVEL4 }, | |
7411 | { '5', ACTION_RUNLEVEL5 }, | |
7412 | { 's', ACTION_RESCUE }, | |
7413 | { 'S', ACTION_RESCUE }, | |
7414 | { 'q', ACTION_RELOAD }, | |
7415 | { 'Q', ACTION_RELOAD }, | |
7416 | { 'u', ACTION_REEXEC }, | |
7417 | { 'U', ACTION_REEXEC } | |
7418 | }; | |
7419 | ||
7420 | unsigned i; | |
7421 | int c; | |
7422 | ||
7423 | assert(argc >= 0); | |
7424 | assert(argv); | |
7425 | ||
601185b4 | 7426 | while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) |
e4b61340 LP |
7427 | switch (c) { |
7428 | ||
7429 | case ARG_HELP: | |
601185b4 ZJS |
7430 | telinit_help(); |
7431 | return 0; | |
e4b61340 | 7432 | |
514f4ef5 LP |
7433 | case ARG_NO_WALL: |
7434 | arg_no_wall = true; | |
7435 | break; | |
7436 | ||
e4b61340 LP |
7437 | case '?': |
7438 | return -EINVAL; | |
7439 | ||
7440 | default: | |
eb9da376 | 7441 | assert_not_reached("Unhandled option"); |
e4b61340 | 7442 | } |
e4b61340 LP |
7443 | |
7444 | if (optind >= argc) { | |
691395d8 | 7445 | log_error("%s: required argument missing.", program_invocation_short_name); |
e4b61340 LP |
7446 | return -EINVAL; |
7447 | } | |
7448 | ||
7449 | if (optind + 1 < argc) { | |
7450 | log_error("Too many arguments."); | |
7451 | return -EINVAL; | |
7452 | } | |
7453 | ||
7454 | if (strlen(argv[optind]) != 1) { | |
7455 | log_error("Expected single character argument."); | |
7456 | return -EINVAL; | |
7457 | } | |
7458 | ||
7459 | for (i = 0; i < ELEMENTSOF(table); i++) | |
7460 | if (table[i].from == argv[optind][0]) | |
7461 | break; | |
7462 | ||
7463 | if (i >= ELEMENTSOF(table)) { | |
b0193f1c | 7464 | log_error("Unknown command '%s'.", argv[optind]); |
e4b61340 LP |
7465 | return -EINVAL; |
7466 | } | |
7467 | ||
7468 | arg_action = table[i].to; | |
7469 | ||
313cefa1 | 7470 | optind++; |
e4b61340 LP |
7471 | |
7472 | return 1; | |
7473 | } | |
7474 | ||
7475 | static int runlevel_parse_argv(int argc, char *argv[]) { | |
7476 | ||
7477 | enum { | |
7478 | ARG_HELP = 0x100, | |
7479 | }; | |
7480 | ||
7481 | static const struct option options[] = { | |
7482 | { "help", no_argument, NULL, ARG_HELP }, | |
eb9da376 | 7483 | {} |
e4b61340 LP |
7484 | }; |
7485 | ||
7486 | int c; | |
7487 | ||
7488 | assert(argc >= 0); | |
7489 | assert(argv); | |
7490 | ||
601185b4 | 7491 | while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) |
e4b61340 LP |
7492 | switch (c) { |
7493 | ||
7494 | case ARG_HELP: | |
601185b4 ZJS |
7495 | runlevel_help(); |
7496 | return 0; | |
e4b61340 LP |
7497 | |
7498 | case '?': | |
7499 | return -EINVAL; | |
7500 | ||
7501 | default: | |
eb9da376 | 7502 | assert_not_reached("Unhandled option"); |
e4b61340 | 7503 | } |
e4b61340 LP |
7504 | |
7505 | if (optind < argc) { | |
7506 | log_error("Too many arguments."); | |
7507 | return -EINVAL; | |
7508 | } | |
7509 | ||
7510 | return 1; | |
7511 | } | |
7512 | ||
7513 | static int parse_argv(int argc, char *argv[]) { | |
7514 | assert(argc >= 0); | |
7515 | assert(argv); | |
7516 | ||
7517 | if (program_invocation_short_name) { | |
7518 | ||
7519 | if (strstr(program_invocation_short_name, "halt")) { | |
7520 | arg_action = ACTION_HALT; | |
7521 | return halt_parse_argv(argc, argv); | |
7522 | } else if (strstr(program_invocation_short_name, "poweroff")) { | |
7523 | arg_action = ACTION_POWEROFF; | |
7524 | return halt_parse_argv(argc, argv); | |
7525 | } else if (strstr(program_invocation_short_name, "reboot")) { | |
5622dde3 KS |
7526 | if (kexec_loaded()) |
7527 | arg_action = ACTION_KEXEC; | |
7528 | else | |
7529 | arg_action = ACTION_REBOOT; | |
e4b61340 LP |
7530 | return halt_parse_argv(argc, argv); |
7531 | } else if (strstr(program_invocation_short_name, "shutdown")) { | |
7532 | arg_action = ACTION_POWEROFF; | |
7533 | return shutdown_parse_argv(argc, argv); | |
7534 | } else if (strstr(program_invocation_short_name, "init")) { | |
d5ca5f11 LP |
7535 | |
7536 | if (sd_booted() > 0) { | |
f459b602 | 7537 | arg_action = _ACTION_INVALID; |
d5ca5f11 LP |
7538 | return telinit_parse_argv(argc, argv); |
7539 | } else { | |
7540 | /* Hmm, so some other init system is | |
7541 | * running, we need to forward this | |
7542 | * request to it. For now we simply | |
7543 | * guess that it is Upstart. */ | |
7544 | ||
4ad61fd1 | 7545 | execv(TELINIT, argv); |
d5ca5f11 LP |
7546 | |
7547 | log_error("Couldn't find an alternative telinit implementation to spawn."); | |
7548 | return -EIO; | |
7549 | } | |
7550 | ||
e4b61340 LP |
7551 | } else if (strstr(program_invocation_short_name, "runlevel")) { |
7552 | arg_action = ACTION_RUNLEVEL; | |
7553 | return runlevel_parse_argv(argc, argv); | |
7554 | } | |
7555 | } | |
7556 | ||
7557 | arg_action = ACTION_SYSTEMCTL; | |
7558 | return systemctl_parse_argv(argc, argv); | |
7559 | } | |
7560 | ||
d2e79673 | 7561 | #ifdef HAVE_SYSV_COMPAT |
44a6b1b6 | 7562 | _pure_ static int action_to_runlevel(void) { |
eb22ac37 LP |
7563 | |
7564 | static const char table[_ACTION_MAX] = { | |
7565 | [ACTION_HALT] = '0', | |
7566 | [ACTION_POWEROFF] = '0', | |
7567 | [ACTION_REBOOT] = '6', | |
7568 | [ACTION_RUNLEVEL2] = '2', | |
7569 | [ACTION_RUNLEVEL3] = '3', | |
7570 | [ACTION_RUNLEVEL4] = '4', | |
7571 | [ACTION_RUNLEVEL5] = '5', | |
7572 | [ACTION_RESCUE] = '1' | |
7573 | }; | |
7574 | ||
d55ae9e6 LP |
7575 | assert(arg_action < _ACTION_MAX); |
7576 | ||
7577 | return table[arg_action]; | |
7578 | } | |
d2e79673 | 7579 | #endif |
d55ae9e6 | 7580 | |
d55ae9e6 | 7581 | static int talk_initctl(void) { |
eca830be | 7582 | #ifdef HAVE_SYSV_COMPAT |
cbc9fbd1 LP |
7583 | struct init_request request = { |
7584 | .magic = INIT_MAGIC, | |
7585 | .sleeptime = 0, | |
7586 | .cmd = INIT_CMD_RUNLVL | |
7587 | }; | |
7588 | ||
7fd1b19b | 7589 | _cleanup_close_ int fd = -1; |
d55ae9e6 | 7590 | char rl; |
cbc9fbd1 | 7591 | int r; |
eb22ac37 | 7592 | |
427b47c4 ZJS |
7593 | rl = action_to_runlevel(); |
7594 | if (!rl) | |
eb22ac37 LP |
7595 | return 0; |
7596 | ||
d55ae9e6 LP |
7597 | request.runlevel = rl; |
7598 | ||
427b47c4 ZJS |
7599 | fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY); |
7600 | if (fd < 0) { | |
d55ae9e6 LP |
7601 | if (errno == ENOENT) |
7602 | return 0; | |
eb22ac37 | 7603 | |
eca830be | 7604 | return log_error_errno(errno, "Failed to open "INIT_FIFO": %m"); |
d55ae9e6 | 7605 | } |
eb22ac37 | 7606 | |
553acb7b ZJS |
7607 | r = loop_write(fd, &request, sizeof(request), false); |
7608 | if (r < 0) | |
7609 | return log_error_errno(r, "Failed to write to "INIT_FIFO": %m"); | |
eb22ac37 LP |
7610 | |
7611 | return 1; | |
eca830be LP |
7612 | #else |
7613 | return 0; | |
7614 | #endif | |
e4b61340 LP |
7615 | } |
7616 | ||
e449de87 LP |
7617 | static int systemctl_main(int argc, char *argv[]) { |
7618 | ||
7619 | static const Verb verbs[] = { | |
1e726cc9 | 7620 | { "list-units", VERB_ANY, VERB_ANY, VERB_DEFAULT|VERB_NOCHROOT, list_units }, |
988b3b17 ZJS |
7621 | { "list-unit-files", VERB_ANY, VERB_ANY, 0, list_unit_files }, |
7622 | { "list-sockets", VERB_ANY, VERB_ANY, VERB_NOCHROOT, list_sockets }, | |
7623 | { "list-timers", VERB_ANY, VERB_ANY, VERB_NOCHROOT, list_timers }, | |
7624 | { "list-jobs", VERB_ANY, VERB_ANY, VERB_NOCHROOT, list_jobs }, | |
7625 | { "list-machines", VERB_ANY, VERB_ANY, VERB_NOCHROOT, list_machines }, | |
7626 | { "clear-jobs", VERB_ANY, 1, VERB_NOCHROOT, trivial_method }, | |
7627 | { "cancel", VERB_ANY, VERB_ANY, VERB_NOCHROOT, cancel_job }, | |
7628 | { "start", 2, VERB_ANY, VERB_NOCHROOT, start_unit }, | |
7629 | { "stop", 2, VERB_ANY, VERB_NOCHROOT, start_unit }, | |
7630 | { "condstop", 2, VERB_ANY, VERB_NOCHROOT, start_unit }, /* For compatibility with ALTLinux */ | |
7631 | { "reload", 2, VERB_ANY, VERB_NOCHROOT, start_unit }, | |
7632 | { "restart", 2, VERB_ANY, VERB_NOCHROOT, start_unit }, | |
7633 | { "try-restart", 2, VERB_ANY, VERB_NOCHROOT, start_unit }, | |
7634 | { "reload-or-restart", 2, VERB_ANY, VERB_NOCHROOT, start_unit }, | |
7635 | { "reload-or-try-restart", 2, VERB_ANY, VERB_NOCHROOT, start_unit }, /* For compatbility with old systemctl <= 228 */ | |
7636 | { "try-reload-or-restart", 2, VERB_ANY, VERB_NOCHROOT, start_unit }, | |
7637 | { "force-reload", 2, VERB_ANY, VERB_NOCHROOT, start_unit }, /* For compatibility with SysV */ | |
7638 | { "condreload", 2, VERB_ANY, VERB_NOCHROOT, start_unit }, /* For compatibility with ALTLinux */ | |
7639 | { "condrestart", 2, VERB_ANY, VERB_NOCHROOT, start_unit }, /* For compatibility with RH */ | |
7640 | { "isolate", 2, 2, VERB_NOCHROOT, start_unit }, | |
7641 | { "kill", 2, VERB_ANY, VERB_NOCHROOT, kill_unit }, | |
7642 | { "is-active", 2, VERB_ANY, VERB_NOCHROOT, check_unit_active }, | |
7643 | { "check", 2, VERB_ANY, VERB_NOCHROOT, check_unit_active }, | |
7644 | { "is-failed", 2, VERB_ANY, VERB_NOCHROOT, check_unit_failed }, | |
7645 | { "show", VERB_ANY, VERB_ANY, VERB_NOCHROOT, show }, | |
7646 | { "cat", 2, VERB_ANY, VERB_NOCHROOT, cat }, | |
7647 | { "status", VERB_ANY, VERB_ANY, VERB_NOCHROOT, show }, | |
7648 | { "help", VERB_ANY, VERB_ANY, VERB_NOCHROOT, show }, | |
7649 | { "daemon-reload", VERB_ANY, 1, VERB_NOCHROOT, daemon_reload }, | |
7650 | { "daemon-reexec", VERB_ANY, 1, VERB_NOCHROOT, daemon_reload }, | |
7651 | { "show-environment", VERB_ANY, 1, VERB_NOCHROOT, show_environment }, | |
7652 | { "set-environment", 2, VERB_ANY, VERB_NOCHROOT, set_environment }, | |
7653 | { "unset-environment", 2, VERB_ANY, VERB_NOCHROOT, set_environment }, | |
7654 | { "import-environment", VERB_ANY, VERB_ANY, VERB_NOCHROOT, import_environment }, | |
7655 | { "halt", VERB_ANY, 1, VERB_NOCHROOT, start_system_special }, | |
7656 | { "poweroff", VERB_ANY, 1, VERB_NOCHROOT, start_system_special }, | |
7657 | { "reboot", VERB_ANY, 2, VERB_NOCHROOT, start_system_special }, | |
7658 | { "kexec", VERB_ANY, 1, VERB_NOCHROOT, start_system_special }, | |
7659 | { "suspend", VERB_ANY, 1, VERB_NOCHROOT, start_system_special }, | |
7660 | { "hibernate", VERB_ANY, 1, VERB_NOCHROOT, start_system_special }, | |
7661 | { "hybrid-sleep", VERB_ANY, 1, VERB_NOCHROOT, start_system_special }, | |
7662 | { "default", VERB_ANY, 1, VERB_NOCHROOT, start_special }, | |
7663 | { "rescue", VERB_ANY, 1, VERB_NOCHROOT, start_system_special }, | |
7664 | { "emergency", VERB_ANY, 1, VERB_NOCHROOT, start_system_special }, | |
7665 | { "exit", VERB_ANY, 2, VERB_NOCHROOT, start_special }, | |
7666 | { "reset-failed", VERB_ANY, VERB_ANY, VERB_NOCHROOT, reset_failed }, | |
7667 | { "enable", 2, VERB_ANY, 0, enable_unit }, | |
7668 | { "disable", 2, VERB_ANY, 0, enable_unit }, | |
7669 | { "is-enabled", 2, VERB_ANY, 0, unit_is_enabled }, | |
7670 | { "reenable", 2, VERB_ANY, 0, enable_unit }, | |
7671 | { "preset", 2, VERB_ANY, 0, enable_unit }, | |
7672 | { "preset-all", VERB_ANY, 1, 0, preset_all }, | |
7673 | { "mask", 2, VERB_ANY, 0, enable_unit }, | |
7674 | { "unmask", 2, VERB_ANY, 0, enable_unit }, | |
7675 | { "link", 2, VERB_ANY, 0, enable_unit }, | |
7676 | { "revert", 2, VERB_ANY, 0, enable_unit }, | |
7677 | { "switch-root", 2, VERB_ANY, VERB_NOCHROOT, switch_root }, | |
7678 | { "list-dependencies", VERB_ANY, 2, VERB_NOCHROOT, list_dependencies }, | |
7679 | { "set-default", 2, 2, 0, set_default }, | |
7680 | { "get-default", VERB_ANY, 1, 0, get_default }, | |
7681 | { "set-property", 3, VERB_ANY, VERB_NOCHROOT, set_property }, | |
7682 | { "is-system-running", VERB_ANY, 1, 0, is_system_running }, | |
7683 | { "add-wants", 3, VERB_ANY, 0, add_dependency }, | |
7684 | { "add-requires", 3, VERB_ANY, 0, add_dependency }, | |
7685 | { "edit", 2, VERB_ANY, VERB_NOCHROOT, edit }, | |
d08e75ed | 7686 | {} |
e449de87 | 7687 | }; |
7e4249b9 | 7688 | |
e449de87 | 7689 | return dispatch_verb(argc, argv, verbs, NULL); |
e4b61340 LP |
7690 | } |
7691 | ||
4fbd7192 | 7692 | static int reload_with_fallback(void) { |
e4b61340 | 7693 | |
4fbd7192 | 7694 | /* First, try systemd via D-Bus. */ |
e449de87 | 7695 | if (daemon_reload(0, NULL, NULL) >= 0) |
4fbd7192 | 7696 | return 0; |
e4b61340 LP |
7697 | |
7698 | /* Nothing else worked, so let's try signals */ | |
2853b60a | 7699 | assert(IN_SET(arg_action, ACTION_RELOAD, ACTION_REEXEC)); |
e4b61340 | 7700 | |
4a62c710 MS |
7701 | if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) |
7702 | return log_error_errno(errno, "kill() failed: %m"); | |
e4b61340 LP |
7703 | |
7704 | return 0; | |
7705 | } | |
7706 | ||
4fbd7192 | 7707 | static int start_with_fallback(void) { |
e4b61340 | 7708 | |
4fbd7192 | 7709 | /* First, try systemd via D-Bus. */ |
e449de87 | 7710 | if (start_unit(0, NULL, NULL) >= 0) |
4fbd7192 | 7711 | return 0; |
e4b61340 | 7712 | |
2853b60a | 7713 | /* Nothing else worked, so let's try /dev/initctl */ |
fbc43921 | 7714 | if (talk_initctl() > 0) |
48ec22bc | 7715 | return 0; |
d55ae9e6 LP |
7716 | |
7717 | log_error("Failed to talk to init daemon."); | |
7718 | return -EIO; | |
e4b61340 LP |
7719 | } |
7720 | ||
477def80 | 7721 | static int halt_now(enum action a) { |
27c06cb5 | 7722 | int r; |
e606bb61 | 7723 | |
4a3ad399 LP |
7724 | /* The kernel will automaticall flush ATA disks and suchlike |
7725 | * on reboot(), but the file systems need to be synce'd | |
7726 | * explicitly in advance. */ | |
f3f054f0 TB |
7727 | if (!arg_no_sync) |
7728 | (void) sync(); | |
4a3ad399 LP |
7729 | |
7730 | /* Make sure C-A-D is handled by the kernel from this point | |
7731 | * on... */ | |
19578bb2 | 7732 | (void) reboot(RB_ENABLE_CAD); |
e606bb61 | 7733 | |
4c80c73c | 7734 | switch (a) { |
e606bb61 LP |
7735 | |
7736 | case ACTION_HALT: | |
7737 | log_info("Halting."); | |
19578bb2 | 7738 | (void) reboot(RB_HALT_SYSTEM); |
477def80 | 7739 | return -errno; |
e606bb61 LP |
7740 | |
7741 | case ACTION_POWEROFF: | |
7742 | log_info("Powering off."); | |
19578bb2 | 7743 | (void) reboot(RB_POWER_OFF); |
477def80 | 7744 | return -errno; |
e606bb61 | 7745 | |
98d52feb | 7746 | case ACTION_KEXEC: |
477def80 LP |
7747 | case ACTION_REBOOT: { |
7748 | _cleanup_free_ char *param = NULL; | |
cbc9fbd1 | 7749 | |
27c06cb5 LP |
7750 | r = read_one_line_file("/run/systemd/reboot-param", ¶m); |
7751 | if (r < 0) | |
7752 | log_warning_errno(r, "Failed to read reboot parameter file: %m"); | |
7753 | ||
7754 | if (!isempty(param)) { | |
477def80 | 7755 | log_info("Rebooting with argument '%s'.", param); |
19578bb2 | 7756 | (void) syscall(SYS_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, param); |
27c06cb5 | 7757 | log_warning_errno(errno, "Failed to reboot with parameter, retrying without: %m"); |
37185ec8 | 7758 | } |
e606bb61 | 7759 | |
477def80 | 7760 | log_info("Rebooting."); |
19578bb2 | 7761 | (void) reboot(RB_AUTOBOOT); |
477def80 | 7762 | return -errno; |
e606bb61 LP |
7763 | } |
7764 | ||
477def80 LP |
7765 | default: |
7766 | assert_not_reached("Unknown action."); | |
7767 | } | |
e606bb61 LP |
7768 | } |
7769 | ||
56a730fa LP |
7770 | static int logind_schedule_shutdown(void) { |
7771 | ||
7772 | #ifdef HAVE_LOGIND | |
4afd3348 | 7773 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
56a730fa LP |
7774 | char date[FORMAT_TIMESTAMP_MAX]; |
7775 | const char *action; | |
4fbd7192 | 7776 | sd_bus *bus; |
56a730fa LP |
7777 | int r; |
7778 | ||
4fbd7192 | 7779 | r = acquire_bus(BUS_FULL, &bus); |
56a730fa | 7780 | if (r < 0) |
4fbd7192 | 7781 | return r; |
56a730fa LP |
7782 | |
7783 | switch (arg_action) { | |
7784 | case ACTION_HALT: | |
7785 | action = "halt"; | |
7786 | break; | |
7787 | case ACTION_POWEROFF: | |
7788 | action = "poweroff"; | |
7789 | break; | |
7790 | case ACTION_KEXEC: | |
7791 | action = "kexec"; | |
7792 | break; | |
a4420f7b LP |
7793 | case ACTION_EXIT: |
7794 | action = "exit"; | |
7795 | break; | |
7796 | case ACTION_REBOOT: | |
56a730fa LP |
7797 | default: |
7798 | action = "reboot"; | |
7799 | break; | |
7800 | } | |
7801 | ||
7802 | if (arg_dry) | |
7803 | action = strjoina("dry-", action); | |
7804 | ||
d2ad7e1b ZJS |
7805 | (void) logind_set_wall_message(); |
7806 | ||
56a730fa | 7807 | r = sd_bus_call_method( |
4fbd7192 | 7808 | bus, |
56a730fa LP |
7809 | "org.freedesktop.login1", |
7810 | "/org/freedesktop/login1", | |
7811 | "org.freedesktop.login1.Manager", | |
7812 | "ScheduleShutdown", | |
7813 | &error, | |
7814 | NULL, | |
7815 | "st", | |
7816 | action, | |
7817 | arg_when); | |
7818 | if (r < 0) | |
7819 | return log_warning_errno(r, "Failed to call ScheduleShutdown in logind, proceeding with immediate shutdown: %s", bus_error_message(&error, r)); | |
7820 | ||
7821 | log_info("Shutdown scheduled for %s, use 'shutdown -c' to cancel.", format_timestamp(date, sizeof(date), arg_when)); | |
7822 | return 0; | |
7823 | #else | |
7824 | log_error("Cannot schedule shutdown without logind support, proceeding with immediate shutdown."); | |
7825 | return -ENOSYS; | |
7826 | #endif | |
7827 | } | |
7828 | ||
4fbd7192 | 7829 | static int halt_main(void) { |
e4b61340 LP |
7830 | int r; |
7831 | ||
4fbd7192 | 7832 | r = logind_check_inhibitors(arg_action); |
748ebafa LP |
7833 | if (r < 0) |
7834 | return r; | |
b37844d3 | 7835 | |
7f96539d LP |
7836 | if (arg_when > 0) |
7837 | return logind_schedule_shutdown(); | |
7838 | ||
bc8c2f5c | 7839 | if (geteuid() != 0) { |
7f96539d | 7840 | if (arg_dry || arg_force > 0) { |
2ac3930f IS |
7841 | log_error("Must be root."); |
7842 | return -EPERM; | |
7843 | } | |
7844 | ||
7e59bfcb LP |
7845 | /* Try logind if we are a normal user and no special |
7846 | * mode applies. Maybe PolicyKit allows us to shutdown | |
7847 | * the machine. */ | |
7f96539d | 7848 | if (IN_SET(arg_action, ACTION_POWEROFF, ACTION_REBOOT)) { |
4fbd7192 | 7849 | r = logind_reboot(arg_action); |
4c80c73c KS |
7850 | if (r >= 0) |
7851 | return r; | |
a9085ea3 | 7852 | if (IN_SET(r, -EOPNOTSUPP, -EINPROGRESS)) |
7f96539d LP |
7853 | /* requested operation is not |
7854 | * supported on the local system or | |
7855 | * already in progress */ | |
a9085ea3 IS |
7856 | return r; |
7857 | /* on all other errors, try low-level operation */ | |
4c80c73c | 7858 | } |
bc8c2f5c LP |
7859 | } |
7860 | ||
65491fd8 | 7861 | if (!arg_dry && !arg_force) |
4fbd7192 | 7862 | return start_with_fallback(); |
e4b61340 | 7863 | |
2ac3930f IS |
7864 | assert(geteuid() == 0); |
7865 | ||
d90e1a30 LP |
7866 | if (!arg_no_wtmp) { |
7867 | if (sd_booted() > 0) | |
7868 | log_debug("Not writing utmp record, assuming that systemd-update-utmp is used."); | |
7e59bfcb LP |
7869 | else { |
7870 | r = utmp_put_shutdown(); | |
7871 | if (r < 0) | |
da927ba9 | 7872 | log_warning_errno(r, "Failed to write utmp record: %m"); |
7e59bfcb | 7873 | } |
d90e1a30 | 7874 | } |
e4b61340 | 7875 | |
e4b61340 LP |
7876 | if (arg_dry) |
7877 | return 0; | |
7878 | ||
477def80 | 7879 | r = halt_now(arg_action); |
691395d8 | 7880 | return log_error_errno(r, "Failed to reboot: %m"); |
e4b61340 LP |
7881 | } |
7882 | ||
7883 | static int runlevel_main(void) { | |
7884 | int r, runlevel, previous; | |
7885 | ||
729e3769 LP |
7886 | r = utmp_get_runlevel(&runlevel, &previous); |
7887 | if (r < 0) { | |
7888 | puts("unknown"); | |
e4b61340 LP |
7889 | return r; |
7890 | } | |
7891 | ||
7892 | printf("%c %c\n", | |
7893 | previous <= 0 ? 'N' : previous, | |
7894 | runlevel <= 0 ? 'N' : runlevel); | |
7895 | ||
7896 | return 0; | |
7897 | } | |
7898 | ||
2cf05793 LP |
7899 | static int logind_cancel_shutdown(void) { |
7900 | #ifdef HAVE_LOGIND | |
4afd3348 | 7901 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4fbd7192 | 7902 | sd_bus *bus; |
949d9ce9 LP |
7903 | int r; |
7904 | ||
4fbd7192 | 7905 | r = acquire_bus(BUS_FULL, &bus); |
949d9ce9 | 7906 | if (r < 0) |
4fbd7192 | 7907 | return r; |
949d9ce9 | 7908 | |
4fbd7192 | 7909 | (void) logind_set_wall_message(); |
949d9ce9 LP |
7910 | |
7911 | r = sd_bus_call_method( | |
4fbd7192 | 7912 | bus, |
949d9ce9 LP |
7913 | "org.freedesktop.login1", |
7914 | "/org/freedesktop/login1", | |
7915 | "org.freedesktop.login1.Manager", | |
7916 | "CancelScheduledShutdown", | |
7917 | &error, | |
7918 | NULL, NULL); | |
7919 | if (r < 0) | |
7920 | return log_warning_errno(r, "Failed to talk to logind, shutdown hasn't been cancelled: %s", bus_error_message(&error, r)); | |
7921 | ||
7922 | return 0; | |
2cf05793 LP |
7923 | #else |
7924 | log_error("Not compiled with logind support, cannot cancel scheduled shutdowns."); | |
7925 | return -ENOSYS; | |
7926 | #endif | |
949d9ce9 LP |
7927 | } |
7928 | ||
e4b61340 | 7929 | int main(int argc, char*argv[]) { |
f459b602 | 7930 | int r; |
e4b61340 | 7931 | |
a9cdc94f | 7932 | setlocale(LC_ALL, ""); |
e4b61340 | 7933 | log_parse_environment(); |
2396fb04 | 7934 | log_open(); |
592705f2 | 7935 | sigbus_install(); |
e4b61340 | 7936 | |
184ecaf7 DR |
7937 | /* Explicitly not on_tty() to avoid setting cached value. |
7938 | * This becomes relevant for piping output which might be | |
7939 | * ellipsized. */ | |
7940 | original_stdout_is_tty = isatty(STDOUT_FILENO); | |
7941 | ||
04ebb595 | 7942 | r = parse_argv(argc, argv); |
f459b602 | 7943 | if (r <= 0) |
e4b61340 | 7944 | goto finish; |
7e4249b9 | 7945 | |
040524b4 | 7946 | if (arg_action != ACTION_SYSTEMCTL && running_in_chroot() > 0) { |
82e23ddd | 7947 | log_info("Running in chroot, ignoring request."); |
f459b602 | 7948 | r = 0; |
82e23ddd LP |
7949 | goto finish; |
7950 | } | |
7951 | ||
41dd15e4 LP |
7952 | /* systemctl_main() will print an error message for the bus |
7953 | * connection, but only if it needs to */ | |
e4b61340 LP |
7954 | |
7955 | switch (arg_action) { | |
7956 | ||
22f4096c | 7957 | case ACTION_SYSTEMCTL: |
e449de87 | 7958 | r = systemctl_main(argc, argv); |
e4b61340 | 7959 | break; |
e4b61340 LP |
7960 | |
7961 | case ACTION_HALT: | |
7962 | case ACTION_POWEROFF: | |
7963 | case ACTION_REBOOT: | |
5622dde3 | 7964 | case ACTION_KEXEC: |
4fbd7192 | 7965 | r = halt_main(); |
e4b61340 LP |
7966 | break; |
7967 | ||
e4b61340 LP |
7968 | case ACTION_RUNLEVEL2: |
7969 | case ACTION_RUNLEVEL3: | |
7970 | case ACTION_RUNLEVEL4: | |
7971 | case ACTION_RUNLEVEL5: | |
7972 | case ACTION_RESCUE: | |
514f4ef5 | 7973 | case ACTION_EMERGENCY: |
eb22ac37 | 7974 | case ACTION_DEFAULT: |
4fbd7192 | 7975 | r = start_with_fallback(); |
e4b61340 | 7976 | break; |
7e4249b9 | 7977 | |
e4b61340 LP |
7978 | case ACTION_RELOAD: |
7979 | case ACTION_REEXEC: | |
4fbd7192 | 7980 | r = reload_with_fallback(); |
e4b61340 LP |
7981 | break; |
7982 | ||
949d9ce9 | 7983 | case ACTION_CANCEL_SHUTDOWN: |
2cf05793 | 7984 | r = logind_cancel_shutdown(); |
f6144808 LP |
7985 | break; |
7986 | ||
eb22ac37 | 7987 | case ACTION_RUNLEVEL: |
4f16c1f4 LP |
7988 | r = runlevel_main(); |
7989 | break; | |
7990 | ||
f459b602 | 7991 | case _ACTION_INVALID: |
e4b61340 LP |
7992 | default: |
7993 | assert_not_reached("Unknown action"); | |
7994 | } | |
7e4249b9 LP |
7995 | |
7996 | finish: | |
cf647b69 LP |
7997 | release_busses(); |
7998 | ||
f459b602 MAP |
7999 | pager_close(); |
8000 | ask_password_agent_close(); | |
8001 | polkit_agent_close(); | |
7e4249b9 | 8002 | |
20b3f379 | 8003 | strv_free(arg_types); |
9b9b3d36 | 8004 | strv_free(arg_states); |
20b3f379 | 8005 | strv_free(arg_properties); |
ea4a240d | 8006 | |
172d7abf | 8007 | strv_free(arg_wall); |
0f03c2a4 | 8008 | free(arg_root); |
172d7abf | 8009 | |
404f08d3 | 8010 | /* Note that we return r here, not EXIT_SUCCESS, so that we can implement the LSB-like return codes */ |
9eb4a501 | 8011 | return r < 0 ? EXIT_FAILURE : r; |
7e4249b9 | 8012 | } |