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