Bug Summary

File:build-scan/../src/systemctl/systemctl.c
Warning:line 6924, column 23
Assigned value is garbage or undefined

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-unknown-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name systemctl.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model static -mframe-pointer=all -relaxed-aliasing -menable-no-infs -menable-no-nans -menable-unsafe-fp-math -fno-signed-zeros -mreassociate -freciprocal-math -fdenormal-fp-math=preserve-sign,preserve-sign -ffp-contract=fast -fno-rounding-math -ffast-math -ffinite-math-only -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -fno-split-dwarf-inlining -debugger-tuning=gdb -resource-dir /usr/lib64/clang/12.0.0 -include config.h -I systemctl.p -I . -I .. -I src/basic -I ../src/basic -I src/shared -I ../src/shared -I src/systemd -I ../src/systemd -I src/journal -I ../src/journal -I src/journal-remote -I ../src/journal-remote -I src/nspawn -I ../src/nspawn -I src/resolve -I ../src/resolve -I src/timesync -I ../src/timesync -I ../src/time-wait-sync -I src/login -I ../src/login -I src/udev -I ../src/udev -I src/libudev -I ../src/libudev -I src/core -I ../src/core -I ../src/libsystemd/sd-bus -I ../src/libsystemd/sd-device -I ../src/libsystemd/sd-hwdb -I ../src/libsystemd/sd-id128 -I ../src/libsystemd/sd-netlink -I ../src/libsystemd/sd-network -I src/libsystemd-network -I ../src/libsystemd-network -D _FILE_OFFSET_BITS=64 -internal-isystem /usr/local/include -internal-isystem /usr/lib64/clang/12.0.0/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -Wwrite-strings -Wno-unused-parameter -Wno-missing-field-initializers -Wno-unused-result -Wno-format-signedness -Wno-error=nonnull -std=gnu99 -fconst-strings -fdebug-compilation-dir /home/mrc0mmand/repos/@redhat-plumbers/systemd-rhel8/build-scan -ferror-limit 19 -fvisibility hidden -stack-protector 2 -fgnuc-version=4.2.1 -fcolor-diagnostics -analyzer-output=html -faddrsig -o /tmp/scan-build-2021-07-16-221226-1465241-1 -x c ../src/systemctl/systemctl.c
1/* SPDX-License-Identifier: LGPL-2.1+ */
2/***
3 Copyright © 2013 Marc-Antoine Perennou
4***/
5
6#include <errno(*__errno_location ()).h>
7#include <fcntl.h>
8#include <getopt.h>
9#include <locale.h>
10#include <stdbool.h>
11#include <stddef.h>
12#include <stdio.h>
13#include <string.h>
14#include <sys/prctl.h>
15#include <sys/reboot.h>
16#include <sys/socket.h>
17#include <unistd.h>
18
19#include "sd-bus.h"
20#include "sd-daemon.h"
21#include "sd-login.h"
22
23#include "alloc-util.h"
24#include "bootspec.h"
25#include "bus-common-errors.h"
26#include "bus-error.h"
27#include "bus-message.h"
28#include "bus-unit-util.h"
29#include "bus-util.h"
30#include "bus-wait-for-units.h"
31#include "cgroup-show.h"
32#include "cgroup-util.h"
33#include "copy.h"
34#include "cpu-set-util.h"
35#include "dropin.h"
36#include "efivars.h"
37#include "env-util.h"
38#include "escape.h"
39#include "exit-status.h"
40#include "fd-util.h"
41#include "fileio.h"
42#include "format-util.h"
43#include "fs-util.h"
44#include "glob-util.h"
45#include "hexdecoct.h"
46#include "hostname-util.h"
47#include "initreq.h"
48#include "install.h"
49#include "io-util.h"
50#include "list.h"
51#include "locale-util.h"
52#include "log.h"
53#include "logs-show.h"
54#include "macro.h"
55#include "mkdir.h"
56#include "pager.h"
57#include "parse-util.h"
58#include "path-lookup.h"
59#include "path-util.h"
60#include "process-util.h"
61#include "reboot-util.h"
62#include "rlimit-util.h"
63#include "set.h"
64#include "sigbus.h"
65#include "signal-util.h"
66#include "socket-util.h"
67#include "spawn-ask-password-agent.h"
68#include "spawn-polkit-agent.h"
69#include "special.h"
70#include "stat-util.h"
71#include "string-table.h"
72#include "strv.h"
73#include "terminal-util.h"
74#include "unit-def.h"
75#include "unit-name.h"
76#include "user-util.h"
77#include "util.h"
78#include "utmp-wtmp.h"
79#include "verbs.h"
80#include "virt.h"
81
82/* The init script exit status codes
83 0 program is running or service is OK
84 1 program is dead and /var/run pid file exists
85 2 program is dead and /var/lock lock file exists
86 3 program is not running
87 4 program or service status is unknown
88 5-99 reserved for future LSB use
89 100-149 reserved for distribution use
90 150-199 reserved for application use
91 200-254 reserved
92*/
93enum {
94 EXIT_PROGRAM_RUNNING_OR_SERVICE_OK = 0,
95 EXIT_PROGRAM_DEAD_AND_PID_EXISTS = 1,
96 EXIT_PROGRAM_DEAD_AND_LOCK_FILE_EXISTS = 2,
97 EXIT_PROGRAM_NOT_RUNNING = 3,
98 EXIT_PROGRAM_OR_SERVICES_STATUS_UNKNOWN = 4,
99};
100
101static char **arg_types = NULL((void*)0);
102static char **arg_states = NULL((void*)0);
103static char **arg_properties = NULL((void*)0);
104static bool_Bool arg_all = false0;
105static enum dependency {
106 DEPENDENCY_FORWARD,
107 DEPENDENCY_REVERSE,
108 DEPENDENCY_AFTER,
109 DEPENDENCY_BEFORE,
110 _DEPENDENCY_MAX
111} arg_dependency = DEPENDENCY_FORWARD;
112static const char *arg_job_mode = "replace";
113static UnitFileScope arg_scope = UNIT_FILE_SYSTEM;
114static bool_Bool arg_wait = false0;
115static bool_Bool arg_no_block = false0;
116static bool_Bool arg_no_legend = false0;
117static bool_Bool arg_no_pager = false0;
118static bool_Bool arg_no_wtmp = false0;
119static bool_Bool arg_no_sync = false0;
120static bool_Bool arg_no_wall = false0;
121static bool_Bool arg_no_reload = false0;
122static bool_Bool arg_value = false0;
123static bool_Bool arg_show_types = false0;
124static bool_Bool arg_ignore_inhibitors = false0;
125static bool_Bool arg_dry_run = false0;
126static bool_Bool arg_quiet = false0;
127static bool_Bool arg_full = false0;
128static bool_Bool arg_recursive = false0;
129static bool_Bool arg_show_transaction = false0;
130static int arg_force = 0;
131static bool_Bool arg_ask_password = false0;
132static bool_Bool arg_runtime = false0;
133static UnitFilePresetMode arg_preset_mode = UNIT_FILE_PRESET_FULL;
134static char **arg_wall = NULL((void*)0);
135static const char *arg_kill_who = NULL((void*)0);
136static int arg_signal = SIGTERM15;
137static char *arg_root = NULL((void*)0);
138static usec_t arg_when = 0;
139static char *arg_esp_path = NULL((void*)0);
140static char *argv_cmdline = NULL((void*)0);
141static enum action {
142 ACTION_SYSTEMCTL,
143 ACTION_HALT,
144 ACTION_POWEROFF,
145 ACTION_REBOOT,
146 ACTION_KEXEC,
147 ACTION_EXIT,
148 ACTION_SUSPEND,
149 ACTION_HIBERNATE,
150 ACTION_HYBRID_SLEEP,
151 ACTION_SUSPEND_THEN_HIBERNATE,
152 ACTION_RUNLEVEL2,
153 ACTION_RUNLEVEL3,
154 ACTION_RUNLEVEL4,
155 ACTION_RUNLEVEL5,
156 ACTION_RESCUE,
157 ACTION_EMERGENCY,
158 ACTION_DEFAULT,
159 ACTION_RELOAD,
160 ACTION_REEXEC,
161 ACTION_RUNLEVEL,
162 ACTION_CANCEL_SHUTDOWN,
163 _ACTION_MAX,
164 _ACTION_INVALID = -1
165} arg_action = ACTION_SYSTEMCTL;
166static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
167static const char *arg_host = NULL((void*)0);
168static unsigned arg_lines = 10;
169static OutputMode arg_output = OUTPUT_SHORT;
170static bool_Bool arg_plain = false0;
171static bool_Bool arg_firmware_setup = false0;
172static bool_Bool arg_now = false0;
173static bool_Bool arg_jobs_before = false0;
174static bool_Bool arg_jobs_after = false0;
175
176static int daemon_reload(int argc, char *argv[], void* userdata);
177static int trivial_method(int argc, char *argv[], void *userdata);
178static int halt_now(enum action a);
179static int get_state_one_unit(sd_bus *bus, const char *name, UnitActiveState *active_state);
180
181static bool_Bool original_stdout_is_tty;
182
183typedef 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
189static sd_bus *busses[_BUS_FOCUS_MAX] = {};
190
191static UnitFileFlags args_to_flags(void) {
192 return (arg_runtime ? UNIT_FILE_RUNTIME : 0) |
193 (arg_force ? UNIT_FILE_FORCE : 0);
194}
195
196static int acquire_bus(BusFocus focus, sd_bus **ret) {
197 int r;
198
199 assert(focus < _BUS_FOCUS_MAX)do { if ((__builtin_expect(!!(!(focus < _BUS_FOCUS_MAX)),0
))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("focus < _BUS_FOCUS_MAX"
), "../src/systemctl/systemctl.c", 199, __PRETTY_FUNCTION__);
} while (0)
;
200 assert(ret)do { if ((__builtin_expect(!!(!(ret)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("ret"), "../src/systemctl/systemctl.c", 200
, __PRETTY_FUNCTION__); } while (0)
;
201
202 /* We only go directly to the manager, if we are using a local transport */
203 if (arg_transport != BUS_TRANSPORT_LOCAL)
204 focus = BUS_FULL;
205
206 if (getenv_bool("SYSTEMCTL_FORCE_BUS") > 0)
207 focus = BUS_FULL;
208
209 if (!busses[focus]) {
210 bool_Bool user;
211
212 user = arg_scope != UNIT_FILE_SYSTEM;
213
214 if (focus == BUS_MANAGER)
215 r = bus_connect_transport_systemd(arg_transport, arg_host, user, &busses[focus]);
216 else
217 r = bus_connect_transport(arg_transport, arg_host, user, &busses[focus]);
218 if (r < 0)
219 return log_error_errno(r, "Failed to connect to bus: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 219, __func__, "Failed to connect to bus: %m"
) : -abs(_e); })
;
220
221 (void) sd_bus_set_allow_interactive_authorization(busses[focus], arg_ask_password);
222 }
223
224 *ret = busses[focus];
225 return 0;
226}
227
228static void release_busses(void) {
229 BusFocus w;
230
231 for (w = 0; w < _BUS_FOCUS_MAX; w++)
232 busses[w] = sd_bus_flush_close_unref(busses[w]);
233}
234
235static void ask_password_agent_open_if_enabled(void) {
236 /* Open the password agent as a child process if necessary */
237
238 if (arg_dry_run)
239 return;
240
241 if (!arg_ask_password)
242 return;
243
244 if (arg_scope != UNIT_FILE_SYSTEM)
245 return;
246
247 if (arg_transport != BUS_TRANSPORT_LOCAL)
248 return;
249
250 ask_password_agent_open();
251}
252
253static void polkit_agent_open_maybe(void) {
254 /* Open the polkit agent as a child process if necessary */
255
256 if (arg_scope != UNIT_FILE_SYSTEM)
257 return;
258
259 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
260}
261
262static OutputFlags get_output_flags(void) {
263 return
264 arg_all * OUTPUT_SHOW_ALL |
265 (arg_full || !on_tty() || pager_have()) * OUTPUT_FULL_WIDTH |
266 colors_enabled() * OUTPUT_COLOR |
267 !arg_quiet * OUTPUT_WARN_CUTOFF;
268}
269
270static int translate_bus_error_to_exit_status(int r, const sd_bus_error *error) {
271 assert(error)do { if ((__builtin_expect(!!(!(error)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("error"), "../src/systemctl/systemctl.c"
, 271, __PRETTY_FUNCTION__); } while (0)
;
272
273 if (!sd_bus_error_is_set(error))
274 return r;
275
276 if (sd_bus_error_has_name(error, SD_BUS_ERROR_ACCESS_DENIED"org.freedesktop.DBus.Error.AccessDenied") ||
277 sd_bus_error_has_name(error, BUS_ERROR_ONLY_BY_DEPENDENCY"org.freedesktop.systemd1.OnlyByDependency") ||
278 sd_bus_error_has_name(error, BUS_ERROR_NO_ISOLATION"org.freedesktop.systemd1.NoIsolation") ||
279 sd_bus_error_has_name(error, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE"org.freedesktop.systemd1.TransactionIsDestructive"))
280 return EXIT_NOPERMISSION;
281
282 if (sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT"org.freedesktop.systemd1.NoSuchUnit"))
283 return EXIT_NOTINSTALLED;
284
285 if (sd_bus_error_has_name(error, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE"org.freedesktop.systemd1.JobTypeNotApplicable") ||
286 sd_bus_error_has_name(error, SD_BUS_ERROR_NOT_SUPPORTED"org.freedesktop.DBus.Error.NotSupported"))
287 return EXIT_NOTIMPLEMENTED;
288
289 if (sd_bus_error_has_name(error, BUS_ERROR_LOAD_FAILED"org.freedesktop.systemd1.LoadFailed"))
290 return EXIT_NOTCONFIGURED;
291
292 if (r != 0)
293 return r;
294
295 return EXIT_FAILURE1;
296}
297
298static bool_Bool install_client_side(void) {
299 /* Decides when to execute enable/disable/... operations
300 * client-side rather than server-side. */
301
302 if (running_in_chroot_or_offline())
303 return true1;
304
305 if (sd_booted() <= 0)
306 return true1;
307
308 if (!isempty(arg_root))
309 return true1;
310
311 if (arg_scope == UNIT_FILE_GLOBAL)
312 return true1;
313
314 /* Unsupported environment variable, mostly for debugging purposes */
315 if (getenv_bool("SYSTEMCTL_INSTALL_CLIENT_SIDE") > 0)
316 return true1;
317
318 return false0;
319}
320
321static int compare_unit_info(const void *a, const void *b) {
322 const UnitInfo *u = a, *v = b;
323 const char *d1, *d2;
324 int r;
325
326 /* First, order by machine */
327 if (!u->machine && v->machine)
328 return -1;
329 if (u->machine && !v->machine)
330 return 1;
331 if (u->machine && v->machine) {
332 r = strcasecmp(u->machine, v->machine);
333 if (r != 0)
334 return r;
335 }
336
337 /* Second, order by unit type */
338 d1 = strrchr(u->id, '.');
339 d2 = strrchr(v->id, '.');
340 if (d1 && d2) {
341 r = strcasecmp(d1, d2);
342 if (r != 0)
343 return r;
344 }
345
346 /* Third, order by name */
347 return strcasecmp(u->id, v->id);
348}
349
350static const char* unit_type_suffix(const char *name) {
351 const char *dot;
352
353 dot = strrchr(name, '.');
354 if (!dot)
355 return "";
356
357 return dot + 1;
358}
359
360static bool_Bool output_show_unit(const UnitInfo *u, char **patterns) {
361 assert(u)do { if ((__builtin_expect(!!(!(u)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("u"), "../src/systemctl/systemctl.c", 361
, __PRETTY_FUNCTION__); } while (0)
;
362
363 if (!strv_fnmatch_or_empty(patterns, u->id, FNM_NOESCAPE(1 << 1)))
364 return false0;
365
366 if (arg_types && !strv_find(arg_types, unit_type_suffix(u->id)))
367 return false0;
368
369 if (arg_all)
370 return true1;
371
372 /* Note that '--all' is not purely a state filter, but also a
373 * filter that hides units that "follow" other units (which is
374 * used for device units that appear under different names). */
375 if (!isempty(u->following))
376 return false0;
377
378 if (!strv_isempty(arg_states))
379 return true1;
380
381 /* By default show all units except the ones in inactive
382 * state and with no pending job */
383 if (u->job_id > 0)
384 return true1;
385
386 if (streq(u->active_state, "inactive")(strcmp((u->active_state),("inactive")) == 0))
387 return false0;
388
389 return true1;
390}
391
392static int output_units_list(const UnitInfo *unit_infos, unsigned c) {
393 unsigned circle_len = 0, id_len, max_id_len, load_len, active_len, sub_len, job_len, desc_len, max_desc_len;
394 const UnitInfo *u;
395 unsigned n_shown = 0;
396 int job_count = 0;
397
398 max_id_len = STRLEN("UNIT")(sizeof("""UNIT""") - 1);
399 load_len = STRLEN("LOAD")(sizeof("""LOAD""") - 1);
400 active_len = STRLEN("ACTIVE")(sizeof("""ACTIVE""") - 1);
401 sub_len = STRLEN("SUB")(sizeof("""SUB""") - 1);
402 job_len = STRLEN("JOB")(sizeof("""JOB""") - 1);
403 max_desc_len = STRLEN("DESCRIPTION")(sizeof("""DESCRIPTION""") - 1);
404
405 for (u = unit_infos; u < unit_infos + c; u++) {
406 max_id_len = MAX(max_id_len, strlen(u->id) + (u->machine ? strlen(u->machine)+1 : 0))__extension__ ({ const typeof((max_id_len)) __unique_prefix_A2539
= ((max_id_len)); const typeof((strlen(u->id) + (u->machine
? strlen(u->machine)+1 : 0))) __unique_prefix_B2540 = ((strlen
(u->id) + (u->machine ? strlen(u->machine)+1 : 0)));
__unique_prefix_A2539 > __unique_prefix_B2540 ? __unique_prefix_A2539
: __unique_prefix_B2540; })
;
407 load_len = MAX(load_len, strlen(u->load_state))__extension__ ({ const typeof((load_len)) __unique_prefix_A2541
= ((load_len)); const typeof((strlen(u->load_state))) __unique_prefix_B2542
= ((strlen(u->load_state))); __unique_prefix_A2541 > __unique_prefix_B2542
? __unique_prefix_A2541 : __unique_prefix_B2542; })
;
408 active_len = MAX(active_len, strlen(u->active_state))__extension__ ({ const typeof((active_len)) __unique_prefix_A2543
= ((active_len)); const typeof((strlen(u->active_state)))
__unique_prefix_B2544 = ((strlen(u->active_state))); __unique_prefix_A2543
> __unique_prefix_B2544 ? __unique_prefix_A2543 : __unique_prefix_B2544
; })
;
409 sub_len = MAX(sub_len, strlen(u->sub_state))__extension__ ({ const typeof((sub_len)) __unique_prefix_A2545
= ((sub_len)); const typeof((strlen(u->sub_state))) __unique_prefix_B2546
= ((strlen(u->sub_state))); __unique_prefix_A2545 > __unique_prefix_B2546
? __unique_prefix_A2545 : __unique_prefix_B2546; })
;
410 max_desc_len = MAX(max_desc_len, strlen(u->description))__extension__ ({ const typeof((max_desc_len)) __unique_prefix_A2547
= ((max_desc_len)); const typeof((strlen(u->description))
) __unique_prefix_B2548 = ((strlen(u->description))); __unique_prefix_A2547
> __unique_prefix_B2548 ? __unique_prefix_A2547 : __unique_prefix_B2548
; })
;
411
412 if (u->job_id != 0) {
413 job_len = MAX(job_len, strlen(u->job_type))__extension__ ({ const typeof((job_len)) __unique_prefix_A2549
= ((job_len)); const typeof((strlen(u->job_type))) __unique_prefix_B2550
= ((strlen(u->job_type))); __unique_prefix_A2549 > __unique_prefix_B2550
? __unique_prefix_A2549 : __unique_prefix_B2550; })
;
414 job_count++;
415 }
416
417 if (!arg_no_legend &&
418 (streq(u->active_state, "failed")(strcmp((u->active_state),("failed")) == 0) ||
419 STR_IN_SET(u->load_state, "error", "not-found", "bad-setting", "masked")(!!strv_find((((char**) ((const char*[]) { "error", "not-found"
, "bad-setting", "masked", ((void*)0) }))), (u->load_state
)))
))
420 circle_len = 2;
421 }
422
423 if (!arg_full && original_stdout_is_tty) {
424 unsigned basic_len;
425
426 id_len = MIN(max_id_len, 25u)__extension__ ({ const typeof((max_id_len)) __unique_prefix_A2551
= ((max_id_len)); const typeof((25u)) __unique_prefix_B2552 =
((25u)); __unique_prefix_A2551 < __unique_prefix_B2552 ? __unique_prefix_A2551
: __unique_prefix_B2552; })
; /* as much as it needs, but at most 25 for now */
427 basic_len = circle_len + 1 + id_len + 1 + load_len + 1 + active_len + 1 + sub_len + 1;
428
429 if (job_count)
430 basic_len += job_len + 1;
431
432 if (basic_len < (unsigned) columns()) {
433 unsigned extra_len, incr;
434 extra_len = columns() - basic_len;
435
436 /* Either UNIT already got 25, or is fully satisfied.
437 * Grant up to 25 to DESC now. */
438 incr = MIN(extra_len, 25u)__extension__ ({ const typeof((extra_len)) __unique_prefix_A2553
= ((extra_len)); const typeof((25u)) __unique_prefix_B2554 =
((25u)); __unique_prefix_A2553 < __unique_prefix_B2554 ? __unique_prefix_A2553
: __unique_prefix_B2554; })
;
439 desc_len = incr;
440 extra_len -= incr;
441
442 /* Of the remainder give as much as the ID needs to the ID, and give the rest to the
443 * description but not more than it needs. */
444 if (extra_len > 0) {
445 incr = MIN(max_id_len - id_len, extra_len)__extension__ ({ const typeof((max_id_len - id_len)) __unique_prefix_A2555
= ((max_id_len - id_len)); const typeof((extra_len)) __unique_prefix_B2556
= ((extra_len)); __unique_prefix_A2555 < __unique_prefix_B2556
? __unique_prefix_A2555 : __unique_prefix_B2556; })
;
446 id_len += incr;
447 desc_len += MIN(extra_len - incr, max_desc_len - desc_len)__extension__ ({ const typeof((extra_len - incr)) __unique_prefix_A2557
= ((extra_len - incr)); const typeof((max_desc_len - desc_len
)) __unique_prefix_B2558 = ((max_desc_len - desc_len)); __unique_prefix_A2557
< __unique_prefix_B2558 ? __unique_prefix_A2557 : __unique_prefix_B2558
; })
;
448 }
449 } else
450 desc_len = 0;
451 } else {
452 id_len = max_id_len;
453 desc_len = max_desc_len;
454 }
455
456 for (u = unit_infos; u < unit_infos + c; u++) {
457 _cleanup_free___attribute__((cleanup(freep))) char *e = NULL((void*)0), *j = NULL((void*)0);
458 const char *on_underline = "", *off_underline = "";
459 const char *on_loaded = "", *off_loaded = "";
460 const char *on_active = "", *off_active = "";
461 const char *on_circle = "", *off_circle = "";
462 const char *id;
463 bool_Bool circle = false0, underline = false0;
464
465 if (!n_shown && !arg_no_legend) {
466
467 if (circle_len > 0)
468 fputs(" ", stdoutstdout);
469
470 printf("%s%-*s %-*s %-*s %-*s ",
471 ansi_underline(),
472 id_len, "UNIT",
473 load_len, "LOAD",
474 active_len, "ACTIVE",
475 sub_len, "SUB");
476
477 if (job_count)
478 printf("%-*s ", job_len, "JOB");
479
480 printf("%-*.*s%s\n",
481 desc_len,
482 !arg_full && arg_no_pager ? (int) desc_len : -1,
483 "DESCRIPTION",
484 ansi_normal());
485 }
486
487 n_shown++;
488
489 if (u + 1 < unit_infos + c &&
490 !streq(unit_type_suffix(u->id), unit_type_suffix((u + 1)->id))(strcmp((unit_type_suffix(u->id)),(unit_type_suffix((u + 1
)->id))) == 0)
) {
491 on_underline = ansi_underline();
492 off_underline = ansi_normal();
493 underline = true1;
494 }
495
496 if (STR_IN_SET(u->load_state, "error", "not-found", "bad-setting", "masked")(!!strv_find((((char**) ((const char*[]) { "error", "not-found"
, "bad-setting", "masked", ((void*)0) }))), (u->load_state
)))
&& !arg_plain) {
497 on_circle = ansi_highlight_yellow();
498 off_circle = ansi_normal();
499 circle = true1;
500 on_loaded = underline ? ansi_highlight_red_underline() : ansi_highlight_red();
501 off_loaded = underline ? on_underline : ansi_normal();
502 } else if (streq(u->active_state, "failed")(strcmp((u->active_state),("failed")) == 0) && !arg_plain) {
503 on_circle = ansi_highlight_red();
504 off_circle = ansi_normal();
505 circle = true1;
506 on_active = underline ? ansi_highlight_red_underline() : ansi_highlight_red();
507 off_active = underline ? on_underline : ansi_normal();
508 }
509
510 if (u->machine) {
511 j = strjoin(u->machine, ":", u->id)strjoin_real((u->machine), ":", u->id, ((void*)0));
512 if (!j)
513 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 513, __func__)
;
514
515 id = j;
516 } else
517 id = u->id;
518
519 if (arg_full) {
520 e = ellipsize(id, id_len, 33);
521 if (!e)
522 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 522, __func__)
;
523
524 id = e;
525 }
526
527 if (circle_len > 0)
528 printf("%s%s%s ", on_circle, circle ? special_glyph(BLACK_CIRCLE) : " ", off_circle);
529
530 printf("%s%s%-*s%s %s%-*s%s %s%-*s %-*s%s %-*s",
531 on_underline,
532 on_active, id_len, id, off_active,
533 on_loaded, load_len, u->load_state, off_loaded,
534 on_active, active_len, u->active_state,
535 sub_len, u->sub_state, off_active,
536 job_count ? job_len + 1 : 0, u->job_id ? u->job_type : "");
537
538 printf("%-*.*s%s\n",
539 desc_len,
540 !arg_full && arg_no_pager ? (int) desc_len : -1,
541 u->description,
542 off_underline);
543 }
544
545 if (!arg_no_legend) {
546 const char *on, *off;
547
548 if (n_shown) {
549 puts("\n"
550 "LOAD = Reflects whether the unit definition was properly loaded.\n"
551 "ACTIVE = The high-level unit activation state, i.e. generalization of SUB.\n"
552 "SUB = The low-level unit activation state, values depend on unit type.");
553 puts(job_count ? "JOB = Pending job for the unit.\n" : "");
554 on = ansi_highlight();
555 off = ansi_normal();
556 } else {
557 on = ansi_highlight_red();
558 off = ansi_normal();
559 }
560
561 if (arg_all)
562 printf("%s%u loaded units listed.%s\n"
563 "To show all installed unit files use 'systemctl list-unit-files'.\n",
564 on, n_shown, off);
565 else
566 printf("%s%u loaded units listed.%s Pass --all to see loaded but inactive units, too.\n"
567 "To show all installed unit files use 'systemctl list-unit-files'.\n",
568 on, n_shown, off);
569 }
570
571 return 0;
572}
573
574static int get_unit_list(
575 sd_bus *bus,
576 const char *machine,
577 char **patterns,
578 UnitInfo **unit_infos,
579 int c,
580 sd_bus_message **_reply) {
581
582 _cleanup_(sd_bus_message_unrefp)__attribute__((cleanup(sd_bus_message_unrefp))) sd_bus_message *m = NULL((void*)0);
583 _cleanup_(sd_bus_error_free)__attribute__((cleanup(sd_bus_error_free))) sd_bus_error error = SD_BUS_ERROR_NULL((const sd_bus_error) {(((void*)0)), (((void*)0)), 0});
584 _cleanup_(sd_bus_message_unrefp)__attribute__((cleanup(sd_bus_message_unrefp))) sd_bus_message *reply = NULL((void*)0);
585 size_t size = c;
586 int r;
587 UnitInfo u;
588 bool_Bool fallback = false0;
589
590 assert(bus)do { if ((__builtin_expect(!!(!(bus)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/systemctl/systemctl.c", 590
, __PRETTY_FUNCTION__); } while (0)
;
591 assert(unit_infos)do { if ((__builtin_expect(!!(!(unit_infos)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("unit_infos"), "../src/systemctl/systemctl.c"
, 591, __PRETTY_FUNCTION__); } while (0)
;
592 assert(_reply)do { if ((__builtin_expect(!!(!(_reply)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("_reply"), "../src/systemctl/systemctl.c"
, 592, __PRETTY_FUNCTION__); } while (0)
;
593
594 r = sd_bus_message_new_method_call(
595 bus,
596 &m,
597 "org.freedesktop.systemd1",
598 "/org/freedesktop/systemd1",
599 "org.freedesktop.systemd1.Manager",
600 "ListUnitsByPatterns");
601 if (r < 0)
602 return bus_log_create_error(r);
603
604 r = sd_bus_message_append_strv(m, arg_states);
605 if (r < 0)
606 return bus_log_create_error(r);
607
608 r = sd_bus_message_append_strv(m, patterns);
609 if (r < 0)
610 return bus_log_create_error(r);
611
612 r = sd_bus_call(bus, m, 0, &error, &reply);
613 if (r < 0 && (sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_METHOD"org.freedesktop.DBus.Error.UnknownMethod") ||
614 sd_bus_error_has_name(&error, SD_BUS_ERROR_ACCESS_DENIED"org.freedesktop.DBus.Error.AccessDenied"))) {
615 /* Fallback to legacy ListUnitsFiltered method */
616 fallback = true1;
617 log_debug_errno(r, "Failed to list units: %s Falling back to ListUnitsFiltered method.", bus_error_message(&error, r))({ int _level = ((7)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 617, __func__, "Failed to list units: %s Falling back to ListUnitsFiltered method."
, bus_error_message(&error, r)) : -abs(_e); })
;
618 m = sd_bus_message_unref(m);
619 sd_bus_error_free(&error);
620
621 r = sd_bus_message_new_method_call(
622 bus,
623 &m,
624 "org.freedesktop.systemd1",
625 "/org/freedesktop/systemd1",
626 "org.freedesktop.systemd1.Manager",
627 "ListUnitsFiltered");
628 if (r < 0)
629 return bus_log_create_error(r);
630
631 r = sd_bus_message_append_strv(m, arg_states);
632 if (r < 0)
633 return bus_log_create_error(r);
634
635 r = sd_bus_call(bus, m, 0, &error, &reply);
636 }
637 if (r < 0)
638 return log_error_errno(r, "Failed to list units: %s", bus_error_message(&error, r))({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 638, __func__, "Failed to list units: %s"
, bus_error_message(&error, r)) : -abs(_e); })
;
639
640 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssssouso)");
641 if (r < 0)
642 return bus_log_parse_error(r);
643
644 while ((r = bus_parse_unit_info(reply, &u)) > 0) {
645 u.machine = machine;
646
647 if (!output_show_unit(&u, fallback ? patterns : NULL((void*)0)))
648 continue;
649
650 if (!GREEDY_REALLOC(*unit_infos, size, c+1)greedy_realloc((void**) &(*unit_infos), &(size), (c+1
), sizeof((*unit_infos)[0]))
)
651 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 651, __func__)
;
652
653 (*unit_infos)[c++] = u;
654 }
655 if (r < 0)
656 return bus_log_parse_error(r);
657
658 r = sd_bus_message_exit_container(reply);
659 if (r < 0)
660 return bus_log_parse_error(r);
661
662 *_reply = TAKE_PTR(reply)({ typeof(reply) _ptr_ = (reply); (reply) = ((void*)0); _ptr_
; })
;
663
664 return c;
665}
666
667static void message_set_freep(Set **set) {
668 set_free_with_destructor(*set, sd_bus_message_unref)({ ({ void *_item; while ((_item = set_steal_first(*set))) sd_bus_message_unref
(_item); }); set_free(*set); })
;
669}
670
671static int get_unit_list_recursive(
672 sd_bus *bus,
673 char **patterns,
674 UnitInfo **_unit_infos,
675 Set **_replies,
676 char ***_machines) {
677
678 _cleanup_free___attribute__((cleanup(freep))) UnitInfo *unit_infos = NULL((void*)0);
679 _cleanup_(message_set_freep)__attribute__((cleanup(message_set_freep))) Set *replies;
680 sd_bus_message *reply;
681 int c, r;
682
683 assert(bus)do { if ((__builtin_expect(!!(!(bus)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/systemctl/systemctl.c", 683
, __PRETTY_FUNCTION__); } while (0)
;
684 assert(_replies)do { if ((__builtin_expect(!!(!(_replies)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("_replies"), "../src/systemctl/systemctl.c"
, 684, __PRETTY_FUNCTION__); } while (0)
;
685 assert(_unit_infos)do { if ((__builtin_expect(!!(!(_unit_infos)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("_unit_infos"), "../src/systemctl/systemctl.c"
, 685, __PRETTY_FUNCTION__); } while (0)
;
686 assert(_machines)do { if ((__builtin_expect(!!(!(_machines)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("_machines"), "../src/systemctl/systemctl.c"
, 686, __PRETTY_FUNCTION__); } while (0)
;
687
688 replies = set_new(NULL)internal_set_new(((void*)0) );
689 if (!replies)
690 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 690, __func__)
;
691
692 c = get_unit_list(bus, NULL((void*)0), patterns, &unit_infos, 0, &reply);
693 if (c < 0)
694 return c;
695
696 r = set_put(replies, reply);
697 if (r < 0) {
698 sd_bus_message_unref(reply);
699 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 699, __func__)
;
700 }
701
702 if (arg_recursive) {
703 _cleanup_strv_free___attribute__((cleanup(strv_freep))) char **machines = NULL((void*)0);
704 char **i;
705
706 r = sd_get_machine_names(&machines);
707 if (r < 0)
708 return log_error_errno(r, "Failed to get machine names: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 708, __func__, "Failed to get machine names: %m"
) : -abs(_e); })
;
709
710 STRV_FOREACH(i, machines)for ((i) = (machines); (i) && *(i); (i)++) {
711 _cleanup_(sd_bus_flush_close_unrefp)__attribute__((cleanup(sd_bus_flush_close_unrefp))) sd_bus *container = NULL((void*)0);
712 int k;
713
714 r = sd_bus_open_system_machine(&container, *i);
715 if (r < 0) {
716 log_warning_errno(r, "Failed to connect to container %s, ignoring: %m", *i)({ int _level = ((4)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 716, __func__, "Failed to connect to container %s, ignoring: %m"
, *i) : -abs(_e); })
;
717 continue;
718 }
719
720 k = get_unit_list(container, *i, patterns, &unit_infos, c, &reply);
721 if (k < 0)
722 return k;
723
724 c = k;
725
726 r = set_put(replies, reply);
727 if (r < 0) {
728 sd_bus_message_unref(reply);
729 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 729, __func__)
;
730 }
731 }
732
733 *_machines = TAKE_PTR(machines)({ typeof(machines) _ptr_ = (machines); (machines) = ((void*)
0); _ptr_; })
;
734 } else
735 *_machines = NULL((void*)0);
736
737 *_unit_infos = TAKE_PTR(unit_infos)({ typeof(unit_infos) _ptr_ = (unit_infos); (unit_infos) = ((
void*)0); _ptr_; })
;
738 *_replies = TAKE_PTR(replies)({ typeof(replies) _ptr_ = (replies); (replies) = ((void*)0);
_ptr_; })
;
739
740 return c;
741}
742
743static int list_units(int argc, char *argv[], void *userdata) {
744 _cleanup_free___attribute__((cleanup(freep))) UnitInfo *unit_infos = NULL((void*)0);
745 _cleanup_(message_set_freep)__attribute__((cleanup(message_set_freep))) Set *replies = NULL((void*)0);
746 _cleanup_strv_free___attribute__((cleanup(strv_freep))) char **machines = NULL((void*)0);
747 sd_bus *bus;
748 int r;
749
750 r = acquire_bus(BUS_MANAGER, &bus);
751 if (r < 0)
752 return r;
753
754 (void) pager_open(arg_no_pager, false0);
755
756 r = get_unit_list_recursive(bus, strv_skip(argv, 1), &unit_infos, &replies, &machines);
757 if (r < 0)
758 return r;
759
760 qsort_safe(unit_infos, r, sizeof(UnitInfo), compare_unit_info);
761 return output_units_list(unit_infos, r);
762}
763
764static int get_triggered_units(
765 sd_bus *bus,
766 const char* path,
767 char*** ret) {
768
769 _cleanup_(sd_bus_error_free)__attribute__((cleanup(sd_bus_error_free))) sd_bus_error error = SD_BUS_ERROR_NULL((const sd_bus_error) {(((void*)0)), (((void*)0)), 0});
770 int r;
771
772 assert(bus)do { if ((__builtin_expect(!!(!(bus)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/systemctl/systemctl.c", 772
, __PRETTY_FUNCTION__); } while (0)
;
773 assert(path)do { if ((__builtin_expect(!!(!(path)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("path"), "../src/systemctl/systemctl.c",
773, __PRETTY_FUNCTION__); } while (0)
;
774 assert(ret)do { if ((__builtin_expect(!!(!(ret)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("ret"), "../src/systemctl/systemctl.c", 774
, __PRETTY_FUNCTION__); } while (0)
;
775
776 r = sd_bus_get_property_strv(
777 bus,
778 "org.freedesktop.systemd1",
779 path,
780 "org.freedesktop.systemd1.Unit",
781 "Triggers",
782 &error,
783 ret);
784 if (r < 0)
785 return log_error_errno(r, "Failed to determine triggers: %s", bus_error_message(&error, r))({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 785, __func__, "Failed to determine triggers: %s"
, bus_error_message(&error, r)) : -abs(_e); })
;
786
787 return 0;
788}
789
790static int get_listening(
791 sd_bus *bus,
792 const char* unit_path,
793 char*** listening) {
794
795 _cleanup_(sd_bus_error_free)__attribute__((cleanup(sd_bus_error_free))) sd_bus_error error = SD_BUS_ERROR_NULL((const sd_bus_error) {(((void*)0)), (((void*)0)), 0});
796 _cleanup_(sd_bus_message_unrefp)__attribute__((cleanup(sd_bus_message_unrefp))) sd_bus_message *reply = NULL((void*)0);
797 const char *type, *path;
798 int r, n = 0;
799
800 r = sd_bus_get_property(
801 bus,
802 "org.freedesktop.systemd1",
803 unit_path,
804 "org.freedesktop.systemd1.Socket",
805 "Listen",
806 &error,
807 &reply,
808 "a(ss)");
809 if (r < 0)
810 return log_error_errno(r, "Failed to get list of listening sockets: %s", bus_error_message(&error, r))({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 810, __func__, "Failed to get list of listening sockets: %s"
, bus_error_message(&error, r)) : -abs(_e); })
;
811
812 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ss)");
813 if (r < 0)
814 return bus_log_parse_error(r);
815
816 while ((r = sd_bus_message_read(reply, "(ss)", &type, &path)) > 0) {
817
818 r = strv_extend(listening, type);
819 if (r < 0)
820 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 820, __func__)
;
821
822 r = strv_extend(listening, path);
823 if (r < 0)
824 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 824, __func__)
;
825
826 n++;
827 }
828 if (r < 0)
829 return bus_log_parse_error(r);
830
831 r = sd_bus_message_exit_container(reply);
832 if (r < 0)
833 return bus_log_parse_error(r);
834
835 return n;
836}
837
838struct socket_info {
839 const char *machine;
840 const char* id;
841
842 char* type;
843 char* path;
844
845 /* Note: triggered is a list here, although it almost certainly
846 * will always be one unit. Nevertheless, dbus API allows for multiple
847 * values, so let's follow that. */
848 char** triggered;
849
850 /* The strv above is shared. free is set only in the first one. */
851 bool_Bool own_triggered;
852};
853
854static int socket_info_compare(const struct socket_info *a, const struct socket_info *b) {
855 int o;
856
857 assert(a)do { if ((__builtin_expect(!!(!(a)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("a"), "../src/systemctl/systemctl.c", 857
, __PRETTY_FUNCTION__); } while (0)
;
858 assert(b)do { if ((__builtin_expect(!!(!(b)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("b"), "../src/systemctl/systemctl.c", 858
, __PRETTY_FUNCTION__); } while (0)
;
859
860 if (!a->machine && b->machine)
861 return -1;
862 if (a->machine && !b->machine)
863 return 1;
864 if (a->machine && b->machine) {
865 o = strcasecmp(a->machine, b->machine);
866 if (o != 0)
867 return o;
868 }
869
870 o = strcmp(a->path, b->path);
871 if (o == 0)
872 o = strcmp(a->type, b->type);
873
874 return o;
875}
876
877static int output_sockets_list(struct socket_info *socket_infos, unsigned cs) {
878 struct socket_info *s;
879 unsigned pathlen = STRLEN("LISTEN")(sizeof("""LISTEN""") - 1),
880 typelen = STRLEN("TYPE")(sizeof("""TYPE""") - 1) * arg_show_types,
881 socklen = STRLEN("UNIT")(sizeof("""UNIT""") - 1),
882 servlen = STRLEN("ACTIVATES")(sizeof("""ACTIVATES""") - 1);
883 const char *on, *off;
884
885 for (s = socket_infos; s < socket_infos + cs; s++) {
886 unsigned tmp = 0;
887 char **a;
888
889 socklen = MAX(socklen, strlen(s->id))__extension__ ({ const typeof((socklen)) __unique_prefix_A2559
= ((socklen)); const typeof((strlen(s->id))) __unique_prefix_B2560
= ((strlen(s->id))); __unique_prefix_A2559 > __unique_prefix_B2560
? __unique_prefix_A2559 : __unique_prefix_B2560; })
;
890 if (arg_show_types)
891 typelen = MAX(typelen, strlen(s->type))__extension__ ({ const typeof((typelen)) __unique_prefix_A2561
= ((typelen)); const typeof((strlen(s->type))) __unique_prefix_B2562
= ((strlen(s->type))); __unique_prefix_A2561 > __unique_prefix_B2562
? __unique_prefix_A2561 : __unique_prefix_B2562; })
;
892 pathlen = MAX(pathlen, strlen(s->path) + (s->machine ? strlen(s->machine)+1 : 0))__extension__ ({ const typeof((pathlen)) __unique_prefix_A2563
= ((pathlen)); const typeof((strlen(s->path) + (s->machine
? strlen(s->machine)+1 : 0))) __unique_prefix_B2564 = ((strlen
(s->path) + (s->machine ? strlen(s->machine)+1 : 0))
); __unique_prefix_A2563 > __unique_prefix_B2564 ? __unique_prefix_A2563
: __unique_prefix_B2564; })
;
893
894 STRV_FOREACH(a, s->triggered)for ((a) = (s->triggered); (a) && *(a); (a)++)
895 tmp += strlen(*a) + 2*(a != s->triggered);
896 servlen = MAX(servlen, tmp)__extension__ ({ const typeof((servlen)) __unique_prefix_A2565
= ((servlen)); const typeof((tmp)) __unique_prefix_B2566 = (
(tmp)); __unique_prefix_A2565 > __unique_prefix_B2566 ? __unique_prefix_A2565
: __unique_prefix_B2566; })
;
897 }
898
899 if (cs) {
900 if (!arg_no_legend)
901 printf("%-*s %-*.*s%-*s %s\n",
902 pathlen, "LISTEN",
903 typelen + arg_show_types, typelen + arg_show_types, "TYPE ",
904 socklen, "UNIT",
905 "ACTIVATES");
906
907 for (s = socket_infos; s < socket_infos + cs; s++) {
908 _cleanup_free___attribute__((cleanup(freep))) char *j = NULL((void*)0);
909 const char *path;
910 char **a;
911
912 if (s->machine) {
913 j = strjoin(s->machine, ":", s->path)strjoin_real((s->machine), ":", s->path, ((void*)0));
914 if (!j)
915 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 915, __func__)
;
916 path = j;
917 } else
918 path = s->path;
919
920 if (arg_show_types)
921 printf("%-*s %-*s %-*s",
922 pathlen, path, typelen, s->type, socklen, s->id);
923 else
924 printf("%-*s %-*s",
925 pathlen, path, socklen, s->id);
926 STRV_FOREACH(a, s->triggered)for ((a) = (s->triggered); (a) && *(a); (a)++)
927 printf("%s %s",
928 a == s->triggered ? "" : ",", *a);
929 printf("\n");
930 }
931
932 on = ansi_highlight();
933 off = ansi_normal();
934 if (!arg_no_legend)
935 printf("\n");
936 } else {
937 on = ansi_highlight_red();
938 off = ansi_normal();
939 }
940
941 if (!arg_no_legend) {
942 printf("%s%u sockets listed.%s\n", on, cs, off);
943 if (!arg_all)
944 printf("Pass --all to see loaded but inactive sockets, too.\n");
945 }
946
947 return 0;
948}
949
950static int list_sockets(int argc, char *argv[], void *userdata) {
951 _cleanup_(message_set_freep)__attribute__((cleanup(message_set_freep))) Set *replies = NULL((void*)0);
952 _cleanup_strv_free___attribute__((cleanup(strv_freep))) char **machines = NULL((void*)0);
953 _cleanup_free___attribute__((cleanup(freep))) UnitInfo *unit_infos = NULL((void*)0);
954 _cleanup_free___attribute__((cleanup(freep))) struct socket_info *socket_infos = NULL((void*)0);
955 const UnitInfo *u;
956 struct socket_info *s;
957 unsigned cs = 0;
958 size_t size = 0;
959 int r = 0, n;
960 sd_bus *bus;
961
962 r = acquire_bus(BUS_MANAGER, &bus);
963 if (r < 0)
964 return r;
965
966 (void) pager_open(arg_no_pager, false0);
967
968 n = get_unit_list_recursive(bus, strv_skip(argv, 1), &unit_infos, &replies, &machines);
969 if (n < 0)
970 return n;
971
972 for (u = unit_infos; u < unit_infos + n; u++) {
973 _cleanup_strv_free___attribute__((cleanup(strv_freep))) char **listening = NULL((void*)0), **triggered = NULL((void*)0);
974 int i, c;
975
976 if (!endswith(u->id, ".socket"))
977 continue;
978
979 r = get_triggered_units(bus, u->unit_path, &triggered);
980 if (r < 0)
981 goto cleanup;
982
983 c = get_listening(bus, u->unit_path, &listening);
984 if (c < 0) {
985 r = c;
986 goto cleanup;
987 }
988
989 if (!GREEDY_REALLOC(socket_infos, size, cs + c)greedy_realloc((void**) &(socket_infos), &(size), (cs
+ c), sizeof((socket_infos)[0]))
) {
990 r = log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 990, __func__)
;
991 goto cleanup;
992 }
993
994 for (i = 0; i < c; i++)
995 socket_infos[cs + i] = (struct socket_info) {
996 .machine = u->machine,
997 .id = u->id,
998 .type = listening[i*2],
999 .path = listening[i*2 + 1],
1000 .triggered = triggered,
1001 .own_triggered = i==0,
1002 };
1003
1004 /* from this point on we will cleanup those socket_infos */
1005 cs += c;
1006 free(listening);
1007 listening = triggered = NULL((void*)0); /* avoid cleanup */
1008 }
1009
1010 qsort_safe(socket_infos, cs, sizeof(struct socket_info),
1011 (__compar_fn_t) socket_info_compare);
1012
1013 output_sockets_list(socket_infos, cs);
1014
1015 cleanup:
1016 assert(cs == 0 || socket_infos)do { if ((__builtin_expect(!!(!(cs == 0 || socket_infos)),0))
) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("cs == 0 || socket_infos"
), "../src/systemctl/systemctl.c", 1016, __PRETTY_FUNCTION__)
; } while (0)
;
1017 for (s = socket_infos; s < socket_infos + cs; s++) {
1018 free(s->type);
1019 free(s->path);
1020 if (s->own_triggered)
1021 strv_free(s->triggered);
1022 }
1023
1024 return r;
1025}
1026
1027static int get_next_elapse(
1028 sd_bus *bus,
1029 const char *path,
1030 dual_timestamp *next) {
1031
1032 _cleanup_(sd_bus_error_free)__attribute__((cleanup(sd_bus_error_free))) sd_bus_error error = SD_BUS_ERROR_NULL((const sd_bus_error) {(((void*)0)), (((void*)0)), 0});
1033 dual_timestamp t;
1034 int r;
1035
1036 assert(bus)do { if ((__builtin_expect(!!(!(bus)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/systemctl/systemctl.c", 1036
, __PRETTY_FUNCTION__); } while (0)
;
1037 assert(path)do { if ((__builtin_expect(!!(!(path)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("path"), "../src/systemctl/systemctl.c",
1037, __PRETTY_FUNCTION__); } while (0)
;
1038 assert(next)do { if ((__builtin_expect(!!(!(next)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("next"), "../src/systemctl/systemctl.c",
1038, __PRETTY_FUNCTION__); } while (0)
;
1039
1040 r = sd_bus_get_property_trivial(
1041 bus,
1042 "org.freedesktop.systemd1",
1043 path,
1044 "org.freedesktop.systemd1.Timer",
1045 "NextElapseUSecMonotonic",
1046 &error,
1047 't',
1048 &t.monotonic);
1049 if (r < 0)
1050 return log_error_errno(r, "Failed to get next elapse time: %s", bus_error_message(&error, r))({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 1050, __func__, "Failed to get next elapse time: %s"
, bus_error_message(&error, r)) : -abs(_e); })
;
1051
1052 r = sd_bus_get_property_trivial(
1053 bus,
1054 "org.freedesktop.systemd1",
1055 path,
1056 "org.freedesktop.systemd1.Timer",
1057 "NextElapseUSecRealtime",
1058 &error,
1059 't',
1060 &t.realtime);
1061 if (r < 0)
1062 return log_error_errno(r, "Failed to get next elapse time: %s", bus_error_message(&error, r))({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 1062, __func__, "Failed to get next elapse time: %s"
, bus_error_message(&error, r)) : -abs(_e); })
;
1063
1064 *next = t;
1065 return 0;
1066}
1067
1068static int get_last_trigger(
1069 sd_bus *bus,
1070 const char *path,
1071 usec_t *last) {
1072
1073 _cleanup_(sd_bus_error_free)__attribute__((cleanup(sd_bus_error_free))) sd_bus_error error = SD_BUS_ERROR_NULL((const sd_bus_error) {(((void*)0)), (((void*)0)), 0});
1074 int r;
1075
1076 assert(bus)do { if ((__builtin_expect(!!(!(bus)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/systemctl/systemctl.c", 1076
, __PRETTY_FUNCTION__); } while (0)
;
1077 assert(path)do { if ((__builtin_expect(!!(!(path)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("path"), "../src/systemctl/systemctl.c",
1077, __PRETTY_FUNCTION__); } while (0)
;
1078 assert(last)do { if ((__builtin_expect(!!(!(last)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("last"), "../src/systemctl/systemctl.c",
1078, __PRETTY_FUNCTION__); } while (0)
;
1079
1080 r = sd_bus_get_property_trivial(
1081 bus,
1082 "org.freedesktop.systemd1",
1083 path,
1084 "org.freedesktop.systemd1.Timer",
1085 "LastTriggerUSec",
1086 &error,
1087 't',
1088 last);
1089 if (r < 0)
1090 return log_error_errno(r, "Failed to get last trigger time: %s", bus_error_message(&error, r))({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 1090, __func__, "Failed to get last trigger time: %s"
, bus_error_message(&error, r)) : -abs(_e); })
;
1091
1092 return 0;
1093}
1094
1095struct timer_info {
1096 const char* machine;
1097 const char* id;
1098 usec_t next_elapse;
1099 usec_t last_trigger;
1100 char** triggered;
1101};
1102
1103static int timer_info_compare(const struct timer_info *a, const struct timer_info *b) {
1104 int o;
1105
1106 assert(a)do { if ((__builtin_expect(!!(!(a)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("a"), "../src/systemctl/systemctl.c", 1106
, __PRETTY_FUNCTION__); } while (0)
;
1107 assert(b)do { if ((__builtin_expect(!!(!(b)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("b"), "../src/systemctl/systemctl.c", 1107
, __PRETTY_FUNCTION__); } while (0)
;
1108
1109 if (!a->machine && b->machine)
1110 return -1;
1111 if (a->machine && !b->machine)
1112 return 1;
1113 if (a->machine && b->machine) {
1114 o = strcasecmp(a->machine, b->machine);
1115 if (o != 0)
1116 return o;
1117 }
1118
1119 if (a->next_elapse < b->next_elapse)
1120 return -1;
1121 if (a->next_elapse > b->next_elapse)
1122 return 1;
1123
1124 return strcmp(a->id, b->id);
1125}
1126
1127static int output_timers_list(struct timer_info *timer_infos, unsigned n) {
1128 struct timer_info *t;
1129 unsigned
1130 nextlen = STRLEN("NEXT")(sizeof("""NEXT""") - 1),
1131 leftlen = STRLEN("LEFT")(sizeof("""LEFT""") - 1),
1132 lastlen = STRLEN("LAST")(sizeof("""LAST""") - 1),
1133 passedlen = STRLEN("PASSED")(sizeof("""PASSED""") - 1),
1134 unitlen = STRLEN("UNIT")(sizeof("""UNIT""") - 1),
1135 activatelen = STRLEN("ACTIVATES")(sizeof("""ACTIVATES""") - 1);
1136
1137 const char *on, *off;
1138
1139 assert(timer_infos || n == 0)do { if ((__builtin_expect(!!(!(timer_infos || n == 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("timer_infos || n == 0"), "../src/systemctl/systemctl.c"
, 1139, __PRETTY_FUNCTION__); } while (0)
;
1140
1141 for (t = timer_infos; t < timer_infos + n; t++) {
1142 unsigned ul = 0;
1143 char **a;
1144
1145 if (t->next_elapse > 0) {
1146 char tstamp[FORMAT_TIMESTAMP_MAX(3+1+10+1+8+1+6+1+6+1)] = "", trel[FORMAT_TIMESTAMP_RELATIVE_MAX256] = "";
1147
1148 format_timestamp(tstamp, sizeof(tstamp), t->next_elapse);
1149 nextlen = MAX(nextlen, strlen(tstamp) + 1)__extension__ ({ const typeof((nextlen)) __unique_prefix_A2567
= ((nextlen)); const typeof((strlen(tstamp) + 1)) __unique_prefix_B2568
= ((strlen(tstamp) + 1)); __unique_prefix_A2567 > __unique_prefix_B2568
? __unique_prefix_A2567 : __unique_prefix_B2568; })
;
1150
1151 format_timestamp_relative(trel, sizeof(trel), t->next_elapse);
1152 leftlen = MAX(leftlen, strlen(trel))__extension__ ({ const typeof((leftlen)) __unique_prefix_A2569
= ((leftlen)); const typeof((strlen(trel))) __unique_prefix_B2570
= ((strlen(trel))); __unique_prefix_A2569 > __unique_prefix_B2570
? __unique_prefix_A2569 : __unique_prefix_B2570; })
;
1153 }
1154
1155 if (t->last_trigger > 0) {
1156 char tstamp[FORMAT_TIMESTAMP_MAX(3+1+10+1+8+1+6+1+6+1)] = "", trel[FORMAT_TIMESTAMP_RELATIVE_MAX256] = "";
1157
1158 format_timestamp(tstamp, sizeof(tstamp), t->last_trigger);
1159 lastlen = MAX(lastlen, strlen(tstamp) + 1)__extension__ ({ const typeof((lastlen)) __unique_prefix_A2571
= ((lastlen)); const typeof((strlen(tstamp) + 1)) __unique_prefix_B2572
= ((strlen(tstamp) + 1)); __unique_prefix_A2571 > __unique_prefix_B2572
? __unique_prefix_A2571 : __unique_prefix_B2572; })
;
1160
1161 format_timestamp_relative(trel, sizeof(trel), t->last_trigger);
1162 passedlen = MAX(passedlen, strlen(trel))__extension__ ({ const typeof((passedlen)) __unique_prefix_A2573
= ((passedlen)); const typeof((strlen(trel))) __unique_prefix_B2574
= ((strlen(trel))); __unique_prefix_A2573 > __unique_prefix_B2574
? __unique_prefix_A2573 : __unique_prefix_B2574; })
;
1163 }
1164
1165 unitlen = MAX(unitlen, strlen(t->id) + (t->machine ? strlen(t->machine)+1 : 0))__extension__ ({ const typeof((unitlen)) __unique_prefix_A2575
= ((unitlen)); const typeof((strlen(t->id) + (t->machine
? strlen(t->machine)+1 : 0))) __unique_prefix_B2576 = ((strlen
(t->id) + (t->machine ? strlen(t->machine)+1 : 0)));
__unique_prefix_A2575 > __unique_prefix_B2576 ? __unique_prefix_A2575
: __unique_prefix_B2576; })
;
1166
1167 STRV_FOREACH(a, t->triggered)for ((a) = (t->triggered); (a) && *(a); (a)++)
1168 ul += strlen(*a) + 2*(a != t->triggered);
1169
1170 activatelen = MAX(activatelen, ul)__extension__ ({ const typeof((activatelen)) __unique_prefix_A2577
= ((activatelen)); const typeof((ul)) __unique_prefix_B2578 =
((ul)); __unique_prefix_A2577 > __unique_prefix_B2578 ? __unique_prefix_A2577
: __unique_prefix_B2578; })
;
1171 }
1172
1173 if (n > 0) {
1174 if (!arg_no_legend)
1175 printf("%-*s %-*s %-*s %-*s %-*s %s\n",
1176 nextlen, "NEXT",
1177 leftlen, "LEFT",
1178 lastlen, "LAST",
1179 passedlen, "PASSED",
1180 unitlen, "UNIT",
1181 "ACTIVATES");
1182
1183 for (t = timer_infos; t < timer_infos + n; t++) {
1184 _cleanup_free___attribute__((cleanup(freep))) char *j = NULL((void*)0);
1185 const char *unit;
1186 char tstamp1[FORMAT_TIMESTAMP_MAX(3+1+10+1+8+1+6+1+6+1)] = "n/a", trel1[FORMAT_TIMESTAMP_RELATIVE_MAX256] = "n/a";
1187 char tstamp2[FORMAT_TIMESTAMP_MAX(3+1+10+1+8+1+6+1+6+1)] = "n/a", trel2[FORMAT_TIMESTAMP_RELATIVE_MAX256] = "n/a";
1188 char **a;
1189
1190 format_timestamp(tstamp1, sizeof(tstamp1), t->next_elapse);
1191 format_timestamp_relative(trel1, sizeof(trel1), t->next_elapse);
1192
1193 format_timestamp(tstamp2, sizeof(tstamp2), t->last_trigger);
1194 format_timestamp_relative(trel2, sizeof(trel2), t->last_trigger);
1195
1196 if (t->machine) {
1197 j = strjoin(t->machine, ":", t->id)strjoin_real((t->machine), ":", t->id, ((void*)0));
1198 if (!j)
1199 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 1199, __func__)
;
1200 unit = j;
1201 } else
1202 unit = t->id;
1203
1204 printf("%-*s %-*s %-*s %-*s %-*s",
1205 nextlen, tstamp1, leftlen, trel1, lastlen, tstamp2, passedlen, trel2, unitlen, unit);
1206
1207 STRV_FOREACH(a, t->triggered)for ((a) = (t->triggered); (a) && *(a); (a)++)
1208 printf("%s %s",
1209 a == t->triggered ? "" : ",", *a);
1210 printf("\n");
1211 }
1212
1213 on = ansi_highlight();
1214 off = ansi_normal();
1215 if (!arg_no_legend)
1216 printf("\n");
1217 } else {
1218 on = ansi_highlight_red();
1219 off = ansi_normal();
1220 }
1221
1222 if (!arg_no_legend) {
1223 printf("%s%u timers listed.%s\n", on, n, off);
1224 if (!arg_all)
1225 printf("Pass --all to see loaded but inactive timers, too.\n");
1226 }
1227
1228 return 0;
1229}
1230
1231static usec_t calc_next_elapse(dual_timestamp *nw, dual_timestamp *next) {
1232 usec_t next_elapse;
1233
1234 assert(nw)do { if ((__builtin_expect(!!(!(nw)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("nw"), "../src/systemctl/systemctl.c", 1234
, __PRETTY_FUNCTION__); } while (0)
;
1235 assert(next)do { if ((__builtin_expect(!!(!(next)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("next"), "../src/systemctl/systemctl.c",
1235, __PRETTY_FUNCTION__); } while (0)
;
1236
1237 if (next->monotonic != USEC_INFINITY((usec_t) -1) && next->monotonic > 0) {
1238 usec_t converted;
1239
1240 if (next->monotonic > nw->monotonic)
1241 converted = nw->realtime + (next->monotonic - nw->monotonic);
1242 else
1243 converted = nw->realtime - (nw->monotonic - next->monotonic);
1244
1245 if (next->realtime != USEC_INFINITY((usec_t) -1) && next->realtime > 0)
1246 next_elapse = MIN(converted, next->realtime)__extension__ ({ const typeof((converted)) __unique_prefix_A2579
= ((converted)); const typeof((next->realtime)) __unique_prefix_B2580
= ((next->realtime)); __unique_prefix_A2579 < __unique_prefix_B2580
? __unique_prefix_A2579 : __unique_prefix_B2580; })
;
1247 else
1248 next_elapse = converted;
1249
1250 } else
1251 next_elapse = next->realtime;
1252
1253 return next_elapse;
1254}
1255
1256static int list_timers(int argc, char *argv[], void *userdata) {
1257 _cleanup_(message_set_freep)__attribute__((cleanup(message_set_freep))) Set *replies = NULL((void*)0);
1258 _cleanup_strv_free___attribute__((cleanup(strv_freep))) char **machines = NULL((void*)0);
1259 _cleanup_free___attribute__((cleanup(freep))) struct timer_info *timer_infos = NULL((void*)0);
1260 _cleanup_free___attribute__((cleanup(freep))) UnitInfo *unit_infos = NULL((void*)0);
1261 struct timer_info *t;
1262 const UnitInfo *u;
1263 size_t size = 0;
1264 int n, c = 0;
1265 dual_timestamp nw;
1266 sd_bus *bus;
1267 int r = 0;
1268
1269 r = acquire_bus(BUS_MANAGER, &bus);
1270 if (r < 0)
1271 return r;
1272
1273 (void) pager_open(arg_no_pager, false0);
1274
1275 n = get_unit_list_recursive(bus, strv_skip(argv, 1), &unit_infos, &replies, &machines);
1276 if (n < 0)
1277 return n;
1278
1279 dual_timestamp_get(&nw);
1280
1281 for (u = unit_infos; u < unit_infos + n; u++) {
1282 _cleanup_strv_free___attribute__((cleanup(strv_freep))) char **triggered = NULL((void*)0);
1283 dual_timestamp next = DUAL_TIMESTAMP_NULL((struct dual_timestamp) {});
1284 usec_t m, last = 0;
1285
1286 if (!endswith(u->id, ".timer"))
1287 continue;
1288
1289 r = get_triggered_units(bus, u->unit_path, &triggered);
1290 if (r < 0)
1291 goto cleanup;
1292
1293 r = get_next_elapse(bus, u->unit_path, &next);
1294 if (r < 0)
1295 goto cleanup;
1296
1297 get_last_trigger(bus, u->unit_path, &last);
1298
1299 if (!GREEDY_REALLOC(timer_infos, size, c+1)greedy_realloc((void**) &(timer_infos), &(size), (c+1
), sizeof((timer_infos)[0]))
) {
1300 r = log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 1300, __func__)
;
1301 goto cleanup;
1302 }
1303
1304 m = calc_next_elapse(&nw, &next);
1305
1306 timer_infos[c++] = (struct timer_info) {
1307 .machine = u->machine,
1308 .id = u->id,
1309 .next_elapse = m,
1310 .last_trigger = last,
1311 .triggered = TAKE_PTR(triggered)({ typeof(triggered) _ptr_ = (triggered); (triggered) = ((void
*)0); _ptr_; })
,
1312 };
1313 }
1314
1315 qsort_safe(timer_infos, c, sizeof(struct timer_info),
1316 (__compar_fn_t) timer_info_compare);
1317
1318 output_timers_list(timer_infos, c);
1319
1320 cleanup:
1321 for (t = timer_infos; t < timer_infos + c; t++)
1322 strv_free(t->triggered);
1323
1324 return r;
1325}
1326
1327static int compare_unit_file_list(const void *a, const void *b) {
1328 const char *d1, *d2;
1329 const UnitFileList *u = a, *v = b;
1330
1331 d1 = strrchr(u->path, '.');
1332 d2 = strrchr(v->path, '.');
1333
1334 if (d1 && d2) {
1335 int r;
1336
1337 r = strcasecmp(d1, d2);
1338 if (r != 0)
1339 return r;
1340 }
1341
1342 return strcasecmp(basename(u->path), basename(v->path));
1343}
1344
1345static bool_Bool output_show_unit_file(const UnitFileList *u, char **states, char **patterns) {
1346 assert(u)do { if ((__builtin_expect(!!(!(u)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("u"), "../src/systemctl/systemctl.c", 1346
, __PRETTY_FUNCTION__); } while (0)
;
1347
1348 if (!strv_fnmatch_or_empty(patterns, basename(u->path), FNM_NOESCAPE(1 << 1)))
1349 return false0;
1350
1351 if (!strv_isempty(arg_types)) {
1352 const char *dot;
1353
1354 dot = strrchr(u->path, '.');
1355 if (!dot)
1356 return false0;
1357
1358 if (!strv_find(arg_types, dot+1))
1359 return false0;
1360 }
1361
1362 if (!strv_isempty(states) &&
1363 !strv_find(states, unit_file_state_to_string(u->state)))
1364 return false0;
1365
1366 return true1;
1367}
1368
1369static void output_unit_file_list(const UnitFileList *units, unsigned c) {
1370 unsigned max_id_len, id_cols, state_cols;
1371 const UnitFileList *u;
1372
1373 max_id_len = STRLEN("UNIT FILE")(sizeof("""UNIT FILE""") - 1);
1374 state_cols = STRLEN("STATE")(sizeof("""STATE""") - 1);
1375
1376 for (u = units; u < units + c; u++) {
1377 max_id_len = MAX(max_id_len, strlen(basename(u->path)))__extension__ ({ const typeof((max_id_len)) __unique_prefix_A2581
= ((max_id_len)); const typeof((strlen(basename(u->path))
)) __unique_prefix_B2582 = ((strlen(basename(u->path)))); __unique_prefix_A2581
> __unique_prefix_B2582 ? __unique_prefix_A2581 : __unique_prefix_B2582
; })
;
1378 state_cols = MAX(state_cols, strlen(unit_file_state_to_string(u->state)))__extension__ ({ const typeof((state_cols)) __unique_prefix_A2583
= ((state_cols)); const typeof((strlen(unit_file_state_to_string
(u->state)))) __unique_prefix_B2584 = ((strlen(unit_file_state_to_string
(u->state)))); __unique_prefix_A2583 > __unique_prefix_B2584
? __unique_prefix_A2583 : __unique_prefix_B2584; })
;
1379 }
1380
1381 if (!arg_full) {
1382 unsigned basic_cols;
1383
1384 id_cols = MIN(max_id_len, 25u)__extension__ ({ const typeof((max_id_len)) __unique_prefix_A2585
= ((max_id_len)); const typeof((25u)) __unique_prefix_B2586 =
((25u)); __unique_prefix_A2585 < __unique_prefix_B2586 ? __unique_prefix_A2585
: __unique_prefix_B2586; })
;
1385 basic_cols = 1 + id_cols + state_cols;
1386 if (basic_cols < (unsigned) columns())
1387 id_cols += MIN(columns() - basic_cols, max_id_len - id_cols)__extension__ ({ const typeof((columns() - basic_cols)) __unique_prefix_A2587
= ((columns() - basic_cols)); const typeof((max_id_len - id_cols
)) __unique_prefix_B2588 = ((max_id_len - id_cols)); __unique_prefix_A2587
< __unique_prefix_B2588 ? __unique_prefix_A2587 : __unique_prefix_B2588
; })
;
1388 } else
1389 id_cols = max_id_len;
1390
1391 if (!arg_no_legend && c > 0)
1392 printf("%s%-*s %-*s%s\n",
1393 ansi_underline(),
1394 id_cols, "UNIT FILE",
1395 state_cols, "STATE",
1396 ansi_normal());
1397
1398 for (u = units; u < units + c; u++) {
1399 const char *on_underline = NULL((void*)0), *on_color = NULL((void*)0), *off = NULL((void*)0), *id;
1400 _cleanup_free___attribute__((cleanup(freep))) char *e = NULL((void*)0);
1401 bool_Bool underline;
1402
1403 underline = u + 1 < units + c &&
1404 !streq(unit_type_suffix(u->path), unit_type_suffix((u + 1)->path))(strcmp((unit_type_suffix(u->path)),(unit_type_suffix((u +
1)->path))) == 0)
;
1405
1406 if (underline)
1407 on_underline = ansi_underline();
1408
1409 if (IN_SET(u->state,({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){UNIT_FILE_MASKED, UNIT_FILE_MASKED_RUNTIME
, UNIT_FILE_DISABLED, UNIT_FILE_BAD})/sizeof(int)]; switch(u->
state) { case UNIT_FILE_MASKED: case UNIT_FILE_MASKED_RUNTIME
: case UNIT_FILE_DISABLED: case UNIT_FILE_BAD: _found = 1; break
; default: break; } _found; })
1410 UNIT_FILE_MASKED,({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){UNIT_FILE_MASKED, UNIT_FILE_MASKED_RUNTIME
, UNIT_FILE_DISABLED, UNIT_FILE_BAD})/sizeof(int)]; switch(u->
state) { case UNIT_FILE_MASKED: case UNIT_FILE_MASKED_RUNTIME
: case UNIT_FILE_DISABLED: case UNIT_FILE_BAD: _found = 1; break
; default: break; } _found; })
1411 UNIT_FILE_MASKED_RUNTIME,({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){UNIT_FILE_MASKED, UNIT_FILE_MASKED_RUNTIME
, UNIT_FILE_DISABLED, UNIT_FILE_BAD})/sizeof(int)]; switch(u->
state) { case UNIT_FILE_MASKED: case UNIT_FILE_MASKED_RUNTIME
: case UNIT_FILE_DISABLED: case UNIT_FILE_BAD: _found = 1; break
; default: break; } _found; })
1412 UNIT_FILE_DISABLED,({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){UNIT_FILE_MASKED, UNIT_FILE_MASKED_RUNTIME
, UNIT_FILE_DISABLED, UNIT_FILE_BAD})/sizeof(int)]; switch(u->
state) { case UNIT_FILE_MASKED: case UNIT_FILE_MASKED_RUNTIME
: case UNIT_FILE_DISABLED: case UNIT_FILE_BAD: _found = 1; break
; default: break; } _found; })
1413 UNIT_FILE_BAD)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){UNIT_FILE_MASKED, UNIT_FILE_MASKED_RUNTIME
, UNIT_FILE_DISABLED, UNIT_FILE_BAD})/sizeof(int)]; switch(u->
state) { case UNIT_FILE_MASKED: case UNIT_FILE_MASKED_RUNTIME
: case UNIT_FILE_DISABLED: case UNIT_FILE_BAD: _found = 1; break
; default: break; } _found; })
)
1414 on_color = underline ? ansi_highlight_red_underline() : ansi_highlight_red();
1415 else if (u->state == UNIT_FILE_ENABLED)
1416 on_color = underline ? ansi_highlight_green_underline() : ansi_highlight_green();
1417
1418 if (on_underline || on_color)
1419 off = ansi_normal();
1420
1421 id = basename(u->path);
1422
1423 e = arg_full ? NULL((void*)0) : ellipsize(id, id_cols, 33);
1424
1425 printf("%s%-*s %s%-*s%s\n",
1426 strempty(on_underline),
1427 id_cols, e ? e : id,
1428 strempty(on_color), state_cols, unit_file_state_to_string(u->state), strempty(off));
1429 }
1430
1431 if (!arg_no_legend)
1432 printf("\n%u unit files listed.\n", c);
1433}
1434
1435static int list_unit_files(int argc, char *argv[], void *userdata) {
1436 _cleanup_(sd_bus_message_unrefp)__attribute__((cleanup(sd_bus_message_unrefp))) sd_bus_message *reply = NULL((void*)0);
1437 _cleanup_free___attribute__((cleanup(freep))) UnitFileList *units = NULL((void*)0);
1438 UnitFileList *unit;
1439 size_t size = 0;
1440 unsigned c = 0;
1441 const char *state;
1442 char *path;
1443 int r;
1444 bool_Bool fallback = false0;
1445
1446 if (install_client_side()) {
1447 Hashmap *h;
1448 UnitFileList *u;
1449 Iterator i;
1450 unsigned n_units;
1451
1452 h = hashmap_new(&string_hash_ops)internal_hashmap_new(&string_hash_ops );
1453 if (!h)
1454 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 1454, __func__)
;
1455
1456 r = unit_file_get_list(arg_scope, arg_root, h, arg_states, strv_skip(argv, 1));
1457 if (r < 0) {
1458 unit_file_list_free(h);
1459 return log_error_errno(r, "Failed to get unit file list: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 1459, __func__, "Failed to get unit file list: %m"
) : -abs(_e); })
;
1460 }
1461
1462 n_units = hashmap_size(h);
1463
1464 units = new(UnitFileList, n_units ?: 1)((UnitFileList*) malloc_multiply(sizeof(UnitFileList), (n_units
?: 1)))
; /* avoid malloc(0) */
1465 if (!units) {
1466 unit_file_list_free(h);
1467 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 1467, __func__)
;
1468 }
1469
1470 HASHMAP_FOREACH(u, h, i)for ((i) = ((Iterator) { .idx = ((2147483647 *2U +1U) - 1), .
next_key = ((void*)0) }); hashmap_iterate((h), &(i), (void
**)&(u), ((void*)0)); )
{
1471 if (!output_show_unit_file(u, NULL((void*)0), NULL((void*)0)))
1472 continue;
1473
1474 units[c++] = *u;
1475 free(u);
1476 }
1477
1478 assert(c <= n_units)do { if ((__builtin_expect(!!(!(c <= n_units)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("c <= n_units"), "../src/systemctl/systemctl.c"
, 1478, __PRETTY_FUNCTION__); } while (0)
;
1479 hashmap_free(h);
1480
1481 r = 0;
1482 } else {
1483 _cleanup_(sd_bus_message_unrefp)__attribute__((cleanup(sd_bus_message_unrefp))) sd_bus_message *m = NULL((void*)0);
1484 _cleanup_(sd_bus_error_free)__attribute__((cleanup(sd_bus_error_free))) sd_bus_error error = SD_BUS_ERROR_NULL((const sd_bus_error) {(((void*)0)), (((void*)0)), 0});
1485 sd_bus *bus;
1486
1487 r = acquire_bus(BUS_MANAGER, &bus);
1488 if (r < 0)
1489 return r;
1490
1491 r = sd_bus_message_new_method_call(
1492 bus,
1493 &m,
1494 "org.freedesktop.systemd1",
1495 "/org/freedesktop/systemd1",
1496 "org.freedesktop.systemd1.Manager",
1497 "ListUnitFilesByPatterns");
1498 if (r < 0)
1499 return bus_log_create_error(r);
1500
1501 r = sd_bus_message_append_strv(m, arg_states);
1502 if (r < 0)
1503 return bus_log_create_error(r);
1504
1505 r = sd_bus_message_append_strv(m, strv_skip(argv, 1));
1506 if (r < 0)
1507 return bus_log_create_error(r);
1508
1509 r = sd_bus_call(bus, m, 0, &error, &reply);
1510 if (r < 0 && sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_METHOD"org.freedesktop.DBus.Error.UnknownMethod")) {
1511 /* Fallback to legacy ListUnitFiles method */
1512 fallback = true1;
1513 log_debug_errno(r, "Failed to list unit files: %s Falling back to ListUnitsFiles method.", bus_error_message(&error, r))({ int _level = ((7)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 1513, __func__, "Failed to list unit files: %s Falling back to ListUnitsFiles method."
, bus_error_message(&error, r)) : -abs(_e); })
;
1514 m = sd_bus_message_unref(m);
1515 sd_bus_error_free(&error);
1516
1517 r = sd_bus_message_new_method_call(
1518 bus,
1519 &m,
1520 "org.freedesktop.systemd1",
1521 "/org/freedesktop/systemd1",
1522 "org.freedesktop.systemd1.Manager",
1523 "ListUnitFiles");
1524 if (r < 0)
1525 return bus_log_create_error(r);
1526
1527 r = sd_bus_call(bus, m, 0, &error, &reply);
1528 }
1529 if (r < 0)
1530 return log_error_errno(r, "Failed to list unit files: %s", bus_error_message(&error, r))({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 1530, __func__, "Failed to list unit files: %s"
, bus_error_message(&error, r)) : -abs(_e); })
;
1531
1532 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ss)");
1533 if (r < 0)
1534 return bus_log_parse_error(r);
1535
1536 while ((r = sd_bus_message_read(reply, "(ss)", &path, &state)) > 0) {
1537
1538 if (!GREEDY_REALLOC(units, size, c + 1)greedy_realloc((void**) &(units), &(size), (c + 1), sizeof
((units)[0]))
)
1539 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 1539, __func__)
;
1540
1541 units[c] = (struct UnitFileList) {
1542 path,
1543 unit_file_state_from_string(state)
1544 };
1545
1546 if (output_show_unit_file(&units[c],
1547 fallback ? arg_states : NULL((void*)0),
1548 fallback ? strv_skip(argv, 1) : NULL((void*)0)))
1549 c++;
1550
1551 }
1552 if (r < 0)
1553 return bus_log_parse_error(r);
1554
1555 r = sd_bus_message_exit_container(reply);
1556 if (r < 0)
1557 return bus_log_parse_error(r);
1558 }
1559
1560 (void) pager_open(arg_no_pager, false0);
1561
1562 qsort_safe(units, c, sizeof(UnitFileList), compare_unit_file_list);
1563 output_unit_file_list(units, c);
1564
1565 if (install_client_side())
1566 for (unit = units; unit < units + c; unit++)
1567 free(unit->path);
1568
1569 return 0;
1570}
1571
1572static int list_dependencies_print(const char *name, int level, unsigned int branches, bool_Bool last) {
1573 _cleanup_free___attribute__((cleanup(freep))) char *n = NULL((void*)0);
1574 size_t max_len = MAX(columns(),20u)__extension__ ({ const typeof((columns())) __unique_prefix_A2589
= ((columns())); const typeof((20u)) __unique_prefix_B2590 =
((20u)); __unique_prefix_A2589 > __unique_prefix_B2590 ? __unique_prefix_A2589
: __unique_prefix_B2590; })
;
1575 size_t len = 0;
1576 int i;
1577
1578 if (!arg_plain) {
1579
1580 for (i = level - 1; i >= 0; i--) {
1581 len += 2;
1582 if (len > max_len - 3 && !arg_full) {
1583 printf("%s...\n",max_len % 2 ? "" : " ");
1584 return 0;
1585 }
1586 printf("%s", special_glyph(branches & (1 << i) ? TREE_VERTICAL : TREE_SPACE));
1587 }
1588 len += 2;
1589
1590 if (len > max_len - 3 && !arg_full) {
1591 printf("%s...\n",max_len % 2 ? "" : " ");
1592 return 0;
1593 }
1594
1595 printf("%s", special_glyph(last ? TREE_RIGHT : TREE_BRANCH));
1596 }
1597
1598 if (arg_full) {
1599 printf("%s\n", name);
1600 return 0;
1601 }
1602
1603 n = ellipsize(name, max_len-len, 100);
1604 if (!n)
1605 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 1605, __func__)
;
1606
1607 printf("%s\n", n);
1608 return 0;
1609}
1610
1611static int list_dependencies_get_dependencies(sd_bus *bus, const char *name, char ***deps) {
1612 struct DependencyStatusInfo {
1613 char **dep[5];
1614 } info = {};
1615
1616 static const struct bus_properties_map map[_DEPENDENCY_MAX][6] = {
1617 [DEPENDENCY_FORWARD] = {
1618 { "Requires", "as", NULL((void*)0), offsetof(struct DependencyStatusInfo, dep[0])__builtin_offsetof(struct DependencyStatusInfo, dep[0]) },
1619 { "Requisite", "as", NULL((void*)0), offsetof(struct DependencyStatusInfo, dep[1])__builtin_offsetof(struct DependencyStatusInfo, dep[1]) },
1620 { "Wants", "as", NULL((void*)0), offsetof(struct DependencyStatusInfo, dep[2])__builtin_offsetof(struct DependencyStatusInfo, dep[2]) },
1621 { "ConsistsOf", "as", NULL((void*)0), offsetof(struct DependencyStatusInfo, dep[3])__builtin_offsetof(struct DependencyStatusInfo, dep[3]) },
1622 { "BindsTo", "as", NULL((void*)0), offsetof(struct DependencyStatusInfo, dep[4])__builtin_offsetof(struct DependencyStatusInfo, dep[4]) },
1623 {}
1624 },
1625 [DEPENDENCY_REVERSE] = {
1626 { "RequiredBy", "as", NULL((void*)0), offsetof(struct DependencyStatusInfo, dep[0])__builtin_offsetof(struct DependencyStatusInfo, dep[0]) },
1627 { "RequisiteOf", "as", NULL((void*)0), offsetof(struct DependencyStatusInfo, dep[1])__builtin_offsetof(struct DependencyStatusInfo, dep[1]) },
1628 { "WantedBy", "as", NULL((void*)0), offsetof(struct DependencyStatusInfo, dep[2])__builtin_offsetof(struct DependencyStatusInfo, dep[2]) },
1629 { "PartOf", "as", NULL((void*)0), offsetof(struct DependencyStatusInfo, dep[3])__builtin_offsetof(struct DependencyStatusInfo, dep[3]) },
1630 { "BoundBy", "as", NULL((void*)0), offsetof(struct DependencyStatusInfo, dep[4])__builtin_offsetof(struct DependencyStatusInfo, dep[4]) },
1631 {}
1632 },
1633 [DEPENDENCY_AFTER] = {
1634 { "After", "as", NULL((void*)0), offsetof(struct DependencyStatusInfo, dep[0])__builtin_offsetof(struct DependencyStatusInfo, dep[0]) },
1635 {}
1636 },
1637 [DEPENDENCY_BEFORE] = {
1638 { "Before", "as", NULL((void*)0), offsetof(struct DependencyStatusInfo, dep[0])__builtin_offsetof(struct DependencyStatusInfo, dep[0]) },
1639 {}
1640 },
1641 };
1642
1643 _cleanup_(sd_bus_error_free)__attribute__((cleanup(sd_bus_error_free))) sd_bus_error error = SD_BUS_ERROR_NULL((const sd_bus_error) {(((void*)0)), (((void*)0)), 0});
1644 _cleanup_strv_free___attribute__((cleanup(strv_freep))) char **ret = NULL((void*)0);
1645 _cleanup_free___attribute__((cleanup(freep))) char *path = NULL((void*)0);
1646 int i, r;
1647
1648 assert(bus)do { if ((__builtin_expect(!!(!(bus)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/systemctl/systemctl.c", 1648
, __PRETTY_FUNCTION__); } while (0)
;
1649 assert(name)do { if ((__builtin_expect(!!(!(name)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("name"), "../src/systemctl/systemctl.c",
1649, __PRETTY_FUNCTION__); } while (0)
;
1650 assert(deps)do { if ((__builtin_expect(!!(!(deps)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("deps"), "../src/systemctl/systemctl.c",
1650, __PRETTY_FUNCTION__); } while (0)
;
1651
1652 path = unit_dbus_path_from_name(name);
1653 if (!path)
1654 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 1654, __func__)
;
1655
1656 r = bus_map_all_properties(bus,
1657 "org.freedesktop.systemd1",
1658 path,
1659 map[arg_dependency],
1660 BUS_MAP_STRDUP,
1661 &error,
1662 NULL((void*)0),
1663 &info);
1664 if (r < 0)
1665 return log_error_errno(r, "Failed to get properties of %s: %s", name, bus_error_message(&error, r))({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 1665, __func__, "Failed to get properties of %s: %s"
, name, bus_error_message(&error, r)) : -abs(_e); })
;
1666
1667 if (IN_SET(arg_dependency, DEPENDENCY_AFTER, DEPENDENCY_BEFORE)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){DEPENDENCY_AFTER, DEPENDENCY_BEFORE})/sizeof
(int)]; switch(arg_dependency) { case DEPENDENCY_AFTER: case DEPENDENCY_BEFORE
: _found = 1; break; default: break; } _found; })
) {
1668 *deps = info.dep[0];
1669 return 0;
1670 }
1671
1672 for (i = 0; i < 5; i++) {
1673 r = strv_extend_strv(&ret, info.dep[i], true1);
1674 if (r < 0)
1675 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 1675, __func__)
;
1676 info.dep[i] = strv_free(info.dep[i]);
1677 }
1678
1679 *deps = TAKE_PTR(ret)({ typeof(ret) _ptr_ = (ret); (ret) = ((void*)0); _ptr_; });
1680
1681 return 0;
1682}
1683
1684static int list_dependencies_compare(const void *_a, const void *_b) {
1685 const char **a = (const char**) _a, **b = (const char**) _b;
1686
1687 if (unit_name_to_type(*a) == UNIT_TARGET && unit_name_to_type(*b) != UNIT_TARGET)
1688 return 1;
1689 if (unit_name_to_type(*a) != UNIT_TARGET && unit_name_to_type(*b) == UNIT_TARGET)
1690 return -1;
1691
1692 return strcasecmp(*a, *b);
1693}
1694
1695static int list_dependencies_one(
1696 sd_bus *bus,
1697 const char *name,
1698 int level,
1699 char ***units,
1700 unsigned int branches) {
1701
1702 _cleanup_strv_free___attribute__((cleanup(strv_freep))) char **deps = NULL((void*)0);
1703 char **c;
1704 int r = 0;
1705
1706 assert(bus)do { if ((__builtin_expect(!!(!(bus)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/systemctl/systemctl.c", 1706
, __PRETTY_FUNCTION__); } while (0)
;
1707 assert(name)do { if ((__builtin_expect(!!(!(name)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("name"), "../src/systemctl/systemctl.c",
1707, __PRETTY_FUNCTION__); } while (0)
;
1708 assert(units)do { if ((__builtin_expect(!!(!(units)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("units"), "../src/systemctl/systemctl.c"
, 1708, __PRETTY_FUNCTION__); } while (0)
;
1709
1710 r = strv_extend(units, name);
1711 if (r < 0)
1712 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 1712, __func__)
;
1713
1714 r = list_dependencies_get_dependencies(bus, name, &deps);
1715 if (r < 0)
1716 return r;
1717
1718 qsort_safe(deps, strv_length(deps), sizeof (char*), list_dependencies_compare);
1719
1720 STRV_FOREACH(c, deps)for ((c) = (deps); (c) && *(c); (c)++) {
1721 if (strv_contains(*units, *c)(!!strv_find((*units), (*c)))) {
1722 if (!arg_plain) {
1723 printf(" ");
1724 r = list_dependencies_print("...", level + 1, (branches << 1) | (c[1] == NULL((void*)0) ? 0 : 1), 1);
1725 if (r < 0)
1726 return r;
1727 }
1728 continue;
1729 }
1730
1731 if (arg_plain)
1732 printf(" ");
1733 else {
1734 UnitActiveState active_state = _UNIT_ACTIVE_STATE_INVALID;
1735 const char *on;
1736
1737 (void) get_state_one_unit(bus, *c, &active_state);
1738
1739 switch (active_state) {
1740 case UNIT_ACTIVE:
1741 case UNIT_RELOADING:
1742 case UNIT_ACTIVATING:
1743 on = ansi_highlight_green();
1744 break;
1745
1746 case UNIT_INACTIVE:
1747 case UNIT_DEACTIVATING:
1748 on = ansi_normal();
1749 break;
1750
1751 default:
1752 on = ansi_highlight_red();
1753 break;
1754 }
1755
1756 printf("%s%s%s ", on, special_glyph(BLACK_CIRCLE), ansi_normal());
1757 }
1758
1759 r = list_dependencies_print(*c, level, branches, c[1] == NULL((void*)0));
1760 if (r < 0)
1761 return r;
1762
1763 if (arg_all || unit_name_to_type(*c) == UNIT_TARGET) {
1764 r = list_dependencies_one(bus, *c, level + 1, units, (branches << 1) | (c[1] == NULL((void*)0) ? 0 : 1));
1765 if (r < 0)
1766 return r;
1767 }
1768 }
1769
1770 if (!arg_plain)
1771 strv_remove(*units, name);
1772
1773 return 0;
1774}
1775
1776static int list_dependencies(int argc, char *argv[], void *userdata) {
1777 _cleanup_strv_free___attribute__((cleanup(strv_freep))) char **units = NULL((void*)0);
1778 _cleanup_free___attribute__((cleanup(freep))) char *unit = NULL((void*)0);
1779 const char *u;
1780 sd_bus *bus;
1781 int r;
1782
1783 if (argv[1]) {
1784 r = unit_name_mangle(argv[1], arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN, &unit);
1785 if (r < 0)
1786 return log_error_errno(r, "Failed to mangle unit name: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 1786, __func__, "Failed to mangle unit name: %m"
) : -abs(_e); })
;
1787
1788 u = unit;
1789 } else
1790 u = SPECIAL_DEFAULT_TARGET"default.target";
1791
1792 r = acquire_bus(BUS_MANAGER, &bus);
1793 if (r < 0)
1794 return r;
1795
1796 (void) pager_open(arg_no_pager, false0);
1797
1798 puts(u);
1799
1800 return list_dependencies_one(bus, u, 0, &units, 0);
1801}
1802
1803struct machine_info {
1804 bool_Bool is_host;
1805 char *name;
1806 char *state;
1807 char *control_group;
1808 uint32_t n_failed_units;
1809 uint32_t n_jobs;
1810 usec_t timestamp;
1811};
1812
1813static const struct bus_properties_map machine_info_property_map[] = {
1814 { "SystemState", "s", NULL((void*)0), offsetof(struct machine_info, state)__builtin_offsetof(struct machine_info, state) },
1815 { "NJobs", "u", NULL((void*)0), offsetof(struct machine_info, n_jobs)__builtin_offsetof(struct machine_info, n_jobs) },
1816 { "NFailedUnits", "u", NULL((void*)0), offsetof(struct machine_info, n_failed_units)__builtin_offsetof(struct machine_info, n_failed_units) },
1817 { "ControlGroup", "s", NULL((void*)0), offsetof(struct machine_info, control_group)__builtin_offsetof(struct machine_info, control_group) },
1818 { "UserspaceTimestamp", "t", NULL((void*)0), offsetof(struct machine_info, timestamp)__builtin_offsetof(struct machine_info, timestamp) },
1819 {}
1820};
1821
1822static void machine_info_clear(struct machine_info *info) {
1823 assert(info)do { if ((__builtin_expect(!!(!(info)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("info"), "../src/systemctl/systemctl.c",
1823, __PRETTY_FUNCTION__); } while (0)
;
1824
1825 free(info->name);
1826 free(info->state);
1827 free(info->control_group);
1828 zero(*info)(({ size_t _l_ = (sizeof(*info)); void *_x_ = (&(*info));
_l_ == 0 ? _x_ : memset(_x_, 0, _l_); }))
;
1829}
1830
1831static void free_machines_list(struct machine_info *machine_infos, int n) {
1832 int i;
1833
1834 if (!machine_infos)
1835 return;
1836
1837 for (i = 0; i < n; i++)
1838 machine_info_clear(&machine_infos[i]);
1839
1840 free(machine_infos);
1841}
1842
1843static int compare_machine_info(const void *a, const void *b) {
1844 const struct machine_info *u = a, *v = b;
1845
1846 if (u->is_host != v->is_host)
1847 return u->is_host > v->is_host ? -1 : 1;
1848
1849 return strcasecmp(u->name, v->name);
1850}
1851
1852static int get_machine_properties(sd_bus *bus, struct machine_info *mi) {
1853 _cleanup_(sd_bus_flush_close_unrefp)__attribute__((cleanup(sd_bus_flush_close_unrefp))) sd_bus *container = NULL((void*)0);
1854 int r;
1855
1856 assert(mi)do { if ((__builtin_expect(!!(!(mi)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("mi"), "../src/systemctl/systemctl.c", 1856
, __PRETTY_FUNCTION__); } while (0)
;
1857
1858 if (!bus) {
1859 r = sd_bus_open_system_machine(&container, mi->name);
1860 if (r < 0)
1861 return r;
1862
1863 bus = container;
1864 }
1865
1866 r = bus_map_all_properties(
1867 bus,
1868 "org.freedesktop.systemd1",
1869 "/org/freedesktop/systemd1",
1870 machine_info_property_map,
1871 BUS_MAP_STRDUP,
1872 NULL((void*)0),
1873 NULL((void*)0),
1874 mi);
1875 if (r < 0)
1876 return r;
1877
1878 return 0;
1879}
1880
1881static bool_Bool output_show_machine(const char *name, char **patterns) {
1882 return strv_fnmatch_or_empty(patterns, name, FNM_NOESCAPE(1 << 1));
1883}
1884
1885static int get_machine_list(
1886 sd_bus *bus,
1887 struct machine_info **_machine_infos,
1888 char **patterns) {
1889
1890 struct machine_info *machine_infos = NULL((void*)0);
1891 _cleanup_strv_free___attribute__((cleanup(strv_freep))) char **m = NULL((void*)0);
1892 _cleanup_free___attribute__((cleanup(freep))) char *hn = NULL((void*)0);
1893 size_t sz = 0;
1894 char **i;
1895 int c = 0, r;
1896
1897 hn = gethostname_malloc();
1898 if (!hn)
1899 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 1899, __func__)
;
1900
1901 if (output_show_machine(hn, patterns)) {
1902 if (!GREEDY_REALLOC0(machine_infos, sz, c+1)greedy_realloc0((void**) &(machine_infos), &(sz), (c+
1), sizeof((machine_infos)[0]))
)
1903 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 1903, __func__)
;
1904
1905 machine_infos[c].is_host = true1;
1906 machine_infos[c].name = TAKE_PTR(hn)({ typeof(hn) _ptr_ = (hn); (hn) = ((void*)0); _ptr_; });
1907
1908 (void) get_machine_properties(bus, &machine_infos[c]);
1909 c++;
1910 }
1911
1912 r = sd_get_machine_names(&m);
1913 if (r < 0)
1914 return log_error_errno(r, "Failed to get machine list: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 1914, __func__, "Failed to get machine list: %m"
) : -abs(_e); })
;
1915
1916 STRV_FOREACH(i, m)for ((i) = (m); (i) && *(i); (i)++) {
1917 _cleanup_free___attribute__((cleanup(freep))) char *class = NULL((void*)0);
1918
1919 if (!output_show_machine(*i, patterns))
1920 continue;
1921
1922 sd_machine_get_class(*i, &class);
1923 if (!streq_ptr(class, "container"))
1924 continue;
1925
1926 if (!GREEDY_REALLOC0(machine_infos, sz, c+1)greedy_realloc0((void**) &(machine_infos), &(sz), (c+
1), sizeof((machine_infos)[0]))
) {
1927 free_machines_list(machine_infos, c);
1928 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 1928, __func__)
;
1929 }
1930
1931 machine_infos[c].is_host = false0;
1932 machine_infos[c].name = strdup(*i);
1933 if (!machine_infos[c].name) {
1934 free_machines_list(machine_infos, c);
1935 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 1935, __func__)
;
1936 }
1937
1938 (void) get_machine_properties(NULL((void*)0), &machine_infos[c]);
1939 c++;
1940 }
1941
1942 *_machine_infos = machine_infos;
1943 return c;
1944}
1945
1946static void output_machines_list(struct machine_info *machine_infos, unsigned n) {
1947 struct machine_info *m;
1948 unsigned
1949 circle_len = 0,
1950 namelen = STRLEN("NAME")(sizeof("""NAME""") - 1),
1951 statelen = STRLEN("STATE")(sizeof("""STATE""") - 1),
1952 failedlen = STRLEN("FAILED")(sizeof("""FAILED""") - 1),
1953 jobslen = STRLEN("JOBS")(sizeof("""JOBS""") - 1);
1954
1955 assert(machine_infos || n == 0)do { if ((__builtin_expect(!!(!(machine_infos || n == 0)),0))
) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("machine_infos || n == 0"
), "../src/systemctl/systemctl.c", 1955, __PRETTY_FUNCTION__)
; } while (0)
;
1956
1957 for (m = machine_infos; m < machine_infos + n; m++) {
1958 namelen = MAX(namelen,__extension__ ({ const typeof((namelen)) __unique_prefix_A2591
= ((namelen)); const typeof((strlen(m->name) + (m->is_host
? (sizeof(""" (host)""") - 1) : 0))) __unique_prefix_B2592 =
((strlen(m->name) + (m->is_host ? (sizeof(""" (host)"""
) - 1) : 0))); __unique_prefix_A2591 > __unique_prefix_B2592
? __unique_prefix_A2591 : __unique_prefix_B2592; })
1959 strlen(m->name) + (m->is_host ? STRLEN(" (host)") : 0))__extension__ ({ const typeof((namelen)) __unique_prefix_A2591
= ((namelen)); const typeof((strlen(m->name) + (m->is_host
? (sizeof(""" (host)""") - 1) : 0))) __unique_prefix_B2592 =
((strlen(m->name) + (m->is_host ? (sizeof(""" (host)"""
) - 1) : 0))); __unique_prefix_A2591 > __unique_prefix_B2592
? __unique_prefix_A2591 : __unique_prefix_B2592; })
;
1960 statelen = MAX(statelen, strlen_ptr(m->state))__extension__ ({ const typeof((statelen)) __unique_prefix_A2593
= ((statelen)); const typeof((strlen_ptr(m->state))) __unique_prefix_B2594
= ((strlen_ptr(m->state))); __unique_prefix_A2593 > __unique_prefix_B2594
? __unique_prefix_A2593 : __unique_prefix_B2594; })
;
1961 failedlen = MAX(failedlen, DECIMAL_STR_WIDTH(m->n_failed_units))__extension__ ({ const typeof((failedlen)) __unique_prefix_A2595
= ((failedlen)); const typeof((({ typeof(m->n_failed_units
) _x_ = (m->n_failed_units); unsigned ans = 1; while ((_x_
/= 10) != 0) ans++; ans; }))) __unique_prefix_B2596 = ((({ typeof
(m->n_failed_units) _x_ = (m->n_failed_units); unsigned
ans = 1; while ((_x_ /= 10) != 0) ans++; ans; }))); __unique_prefix_A2595
> __unique_prefix_B2596 ? __unique_prefix_A2595 : __unique_prefix_B2596
; })
;
1962 jobslen = MAX(jobslen, DECIMAL_STR_WIDTH(m->n_jobs))__extension__ ({ const typeof((jobslen)) __unique_prefix_A2597
= ((jobslen)); const typeof((({ typeof(m->n_jobs) _x_ = (
m->n_jobs); unsigned ans = 1; while ((_x_ /= 10) != 0) ans
++; ans; }))) __unique_prefix_B2598 = ((({ typeof(m->n_jobs
) _x_ = (m->n_jobs); unsigned ans = 1; while ((_x_ /= 10) !=
0) ans++; ans; }))); __unique_prefix_A2597 > __unique_prefix_B2598
? __unique_prefix_A2597 : __unique_prefix_B2598; })
;
1963
1964 if (!arg_plain && !streq_ptr(m->state, "running"))
1965 circle_len = 2;
1966 }
1967
1968 if (!arg_no_legend) {
1969 if (circle_len > 0)
1970 fputs(" ", stdoutstdout);
1971
1972 printf("%-*s %-*s %-*s %-*s\n",
1973 namelen, "NAME",
1974 statelen, "STATE",
1975 failedlen, "FAILED",
1976 jobslen, "JOBS");
1977 }
1978
1979 for (m = machine_infos; m < machine_infos + n; m++) {
1980 const char *on_state = "", *off_state = "";
1981 const char *on_failed = "", *off_failed = "";
1982 bool_Bool circle = false0;
1983
1984 if (streq_ptr(m->state, "degraded")) {
1985 on_state = ansi_highlight_red();
1986 off_state = ansi_normal();
1987 circle = true1;
1988 } else if (!streq_ptr(m->state, "running")) {
1989 on_state = ansi_highlight_yellow();
1990 off_state = ansi_normal();
1991 circle = true1;
1992 }
1993
1994 if (m->n_failed_units > 0) {
1995 on_failed = ansi_highlight_red();
1996 off_failed = ansi_normal();
1997 } else
1998 on_failed = off_failed = "";
1999
2000 if (circle_len > 0)
2001 printf("%s%s%s ", on_state, circle ? special_glyph(BLACK_CIRCLE) : " ", off_state);
2002
2003 if (m->is_host)
2004 printf("%-*s (host) %s%-*s%s %s%*" PRIu32"u" "%s %*" PRIu32"u" "\n",
2005 (int) (namelen - (STRLEN(" (host)")(sizeof(""" (host)""") - 1))),
2006 strna(m->name),
2007 on_state, statelen, strna(m->state), off_state,
2008 on_failed, failedlen, m->n_failed_units, off_failed,
2009 jobslen, m->n_jobs);
2010 else
2011 printf("%-*s %s%-*s%s %s%*" PRIu32"u" "%s %*" PRIu32"u" "\n",
2012 namelen, 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 }
2017
2018 if (!arg_no_legend)
2019 printf("\n%u machines listed.\n", n);
2020}
2021
2022static int list_machines(int argc, char *argv[], void *userdata) {
2023 struct machine_info *machine_infos = NULL((void*)0);
2024 sd_bus *bus;
2025 int r;
2026
2027 r = acquire_bus(BUS_MANAGER, &bus);
2028 if (r < 0)
2029 return r;
2030
2031 r = get_machine_list(bus, &machine_infos, strv_skip(argv, 1));
2032 if (r < 0)
2033 return r;
2034
2035 (void) pager_open(arg_no_pager, false0);
2036
2037 qsort_safe(machine_infos, r, sizeof(struct machine_info), compare_machine_info);
2038 output_machines_list(machine_infos, r);
2039 free_machines_list(machine_infos, r);
2040
2041 return 0;
2042}
2043
2044static int get_default(int argc, char *argv[], void *userdata) {
2045 _cleanup_(sd_bus_message_unrefp)__attribute__((cleanup(sd_bus_message_unrefp))) sd_bus_message *reply = NULL((void*)0);
2046 _cleanup_free___attribute__((cleanup(freep))) char *_path = NULL((void*)0);
2047 const char *path;
2048 int r;
2049
2050 if (install_client_side()) {
2051 r = unit_file_get_default(arg_scope, arg_root, &_path);
2052 if (r < 0)
2053 return log_error_errno(r, "Failed to get default target: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 2053, __func__, "Failed to get default target: %m"
) : -abs(_e); })
;
2054 path = _path;
2055
2056 r = 0;
2057 } else {
2058 _cleanup_(sd_bus_error_free)__attribute__((cleanup(sd_bus_error_free))) sd_bus_error error = SD_BUS_ERROR_NULL((const sd_bus_error) {(((void*)0)), (((void*)0)), 0});
2059 sd_bus *bus;
2060
2061 r = acquire_bus(BUS_MANAGER, &bus);
2062 if (r < 0)
2063 return r;
2064
2065 r = sd_bus_call_method(
2066 bus,
2067 "org.freedesktop.systemd1",
2068 "/org/freedesktop/systemd1",
2069 "org.freedesktop.systemd1.Manager",
2070 "GetDefaultTarget",
2071 &error,
2072 &reply,
2073 NULL((void*)0));
2074 if (r < 0)
2075 return log_error_errno(r, "Failed to get default target: %s", bus_error_message(&error, r))({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 2075, __func__, "Failed to get default target: %s"
, bus_error_message(&error, r)) : -abs(_e); })
;
2076
2077 r = sd_bus_message_read(reply, "s", &path);
2078 if (r < 0)
2079 return bus_log_parse_error(r);
2080 }
2081
2082 if (path)
2083 printf("%s\n", path);
2084
2085 return 0;
2086}
2087
2088static int set_default(int argc, char *argv[], void *userdata) {
2089 _cleanup_free___attribute__((cleanup(freep))) char *unit = NULL((void*)0);
2090 UnitFileChange *changes = NULL((void*)0);
2091 size_t n_changes = 0;
2092 int r;
2093
2094 assert(argc >= 2)do { if ((__builtin_expect(!!(!(argc >= 2)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("argc >= 2"), "../src/systemctl/systemctl.c"
, 2094, __PRETTY_FUNCTION__); } while (0)
;
2095 assert(argv)do { if ((__builtin_expect(!!(!(argv)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("argv"), "../src/systemctl/systemctl.c",
2095, __PRETTY_FUNCTION__); } while (0)
;
2096
2097 r = unit_name_mangle_with_suffix(argv[1], arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN, ".target", &unit);
2098 if (r < 0)
2099 return log_error_errno(r, "Failed to mangle unit name: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 2099, __func__, "Failed to mangle unit name: %m"
) : -abs(_e); })
;
2100
2101 if (install_client_side()) {
2102 r = unit_file_set_default(arg_scope, UNIT_FILE_FORCE, arg_root, unit, &changes, &n_changes);
2103 unit_file_dump_changes(r, "set default", changes, n_changes, arg_quiet);
2104
2105 if (r > 0)
2106 r = 0;
2107 } else {
2108 _cleanup_(sd_bus_error_free)__attribute__((cleanup(sd_bus_error_free))) sd_bus_error error = SD_BUS_ERROR_NULL((const sd_bus_error) {(((void*)0)), (((void*)0)), 0});
2109 _cleanup_(sd_bus_message_unrefp)__attribute__((cleanup(sd_bus_message_unrefp))) sd_bus_message *reply = NULL((void*)0);
2110 sd_bus *bus;
2111
2112 polkit_agent_open_maybe();
2113
2114 r = acquire_bus(BUS_MANAGER, &bus);
2115 if (r < 0)
2116 return r;
2117
2118 r = sd_bus_call_method(
2119 bus,
2120 "org.freedesktop.systemd1",
2121 "/org/freedesktop/systemd1",
2122 "org.freedesktop.systemd1.Manager",
2123 "SetDefaultTarget",
2124 &error,
2125 &reply,
2126 "sb", unit, 1);
2127 if (r < 0)
2128 return log_error_errno(r, "Failed to set default target: %s", bus_error_message(&error, r))({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 2128, __func__, "Failed to set default target: %s"
, bus_error_message(&error, r)) : -abs(_e); })
;
2129
2130 r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet, &changes, &n_changes);
2131 if (r < 0)
2132 goto finish;
2133
2134 /* Try to reload if enabled */
2135 if (!arg_no_reload)
2136 r = daemon_reload(argc, argv, userdata);
2137 else
2138 r = 0;
2139 }
2140
2141finish:
2142 unit_file_changes_free(changes, n_changes);
2143
2144 return r;
2145}
2146
2147static int output_waiting_jobs(sd_bus *bus, uint32_t id, const char *method, const char *prefix) {
2148 _cleanup_(sd_bus_error_free)__attribute__((cleanup(sd_bus_error_free))) sd_bus_error error = SD_BUS_ERROR_NULL((const sd_bus_error) {(((void*)0)), (((void*)0)), 0});
2149 _cleanup_(sd_bus_message_unrefp)__attribute__((cleanup(sd_bus_message_unrefp))) sd_bus_message *reply = NULL((void*)0);
2150 const char *name, *type;
2151 uint32_t other_id;
2152 int r;
2153
2154 assert(bus)do { if ((__builtin_expect(!!(!(bus)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/systemctl/systemctl.c", 2154
, __PRETTY_FUNCTION__); } while (0)
;
2155
2156 r = sd_bus_call_method(
2157 bus,
2158 "org.freedesktop.systemd1",
2159 "/org/freedesktop/systemd1",
2160 "org.freedesktop.systemd1.Manager",
2161 method,
2162 &error,
2163 &reply,
2164 "u", id);
2165 if (r < 0)
2166 return log_debug_errno(r, "Failed to get waiting jobs for job %" PRIu32, id)({ int _level = ((7)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 2166, __func__, "Failed to get waiting jobs for job %"
"u", id) : -abs(_e); })
;
2167
2168 r = sd_bus_message_enter_container(reply, 'a', "(usssoo)");
2169 if (r < 0)
2170 return bus_log_parse_error(r);
2171
2172 while ((r = sd_bus_message_read(reply, "(usssoo)", &other_id, &name, &type, NULL((void*)0), NULL((void*)0), NULL((void*)0))) > 0)
2173 printf("%s %u (%s/%s)\n", prefix, other_id, name, type);
2174 if (r < 0)
2175 return bus_log_parse_error(r);
2176
2177 r = sd_bus_message_exit_container(reply);
2178 if (r < 0)
2179 return bus_log_parse_error(r);
2180
2181 return 0;
2182}
2183
2184struct job_info {
2185 uint32_t id;
2186 const char *name, *type, *state;
2187};
2188
2189static void output_jobs_list(sd_bus *bus, const struct job_info* jobs, unsigned n, bool_Bool skipped) {
2190 unsigned id_len, unit_len, type_len, state_len;
2191 const struct job_info *j;
2192 const char *on, *off;
2193 bool_Bool shorten = false0;
2194
2195 assert(n == 0 || jobs)do { if ((__builtin_expect(!!(!(n == 0 || jobs)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("n == 0 || jobs"), "../src/systemctl/systemctl.c"
, 2195, __PRETTY_FUNCTION__); } while (0)
;
2196
2197 if (n == 0) {
2198 if (!arg_no_legend) {
2199 on = ansi_highlight_green();
2200 off = ansi_normal();
2201
2202 printf("%sNo jobs %s.%s\n", on, skipped ? "listed" : "running", off);
2203 }
2204 return;
2205 }
2206
2207 (void) pager_open(arg_no_pager, false0);
2208
2209 id_len = STRLEN("JOB")(sizeof("""JOB""") - 1);
2210 unit_len = STRLEN("UNIT")(sizeof("""UNIT""") - 1);
2211 type_len = STRLEN("TYPE")(sizeof("""TYPE""") - 1);
2212 state_len = STRLEN("STATE")(sizeof("""STATE""") - 1);
2213
2214 for (j = jobs; j < jobs + n; j++) {
2215 uint32_t id = j->id;
2216 assert(j->name && j->type && j->state)do { if ((__builtin_expect(!!(!(j->name && j->type
&& j->state)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD
, ("j->name && j->type && j->state")
, "../src/systemctl/systemctl.c", 2216, __PRETTY_FUNCTION__);
} while (0)
;
2217
2218 id_len = MAX(id_len, DECIMAL_STR_WIDTH(id))__extension__ ({ const typeof((id_len)) __unique_prefix_A2599
= ((id_len)); const typeof((({ typeof(id) _x_ = (id); unsigned
ans = 1; while ((_x_ /= 10) != 0) ans++; ans; }))) __unique_prefix_B2600
= ((({ typeof(id) _x_ = (id); unsigned ans = 1; while ((_x_ /=
10) != 0) ans++; ans; }))); __unique_prefix_A2599 > __unique_prefix_B2600
? __unique_prefix_A2599 : __unique_prefix_B2600; })
;
2219 unit_len = MAX(unit_len, strlen(j->name))__extension__ ({ const typeof((unit_len)) __unique_prefix_A2601
= ((unit_len)); const typeof((strlen(j->name))) __unique_prefix_B2602
= ((strlen(j->name))); __unique_prefix_A2601 > __unique_prefix_B2602
? __unique_prefix_A2601 : __unique_prefix_B2602; })
;
2220 type_len = MAX(type_len, strlen(j->type))__extension__ ({ const typeof((type_len)) __unique_prefix_A2603
= ((type_len)); const typeof((strlen(j->type))) __unique_prefix_B2604
= ((strlen(j->type))); __unique_prefix_A2603 > __unique_prefix_B2604
? __unique_prefix_A2603 : __unique_prefix_B2604; })
;
2221 state_len = MAX(state_len, strlen(j->state))__extension__ ({ const typeof((state_len)) __unique_prefix_A2605
= ((state_len)); const typeof((strlen(j->state))) __unique_prefix_B2606
= ((strlen(j->state))); __unique_prefix_A2605 > __unique_prefix_B2606
? __unique_prefix_A2605 : __unique_prefix_B2606; })
;
2222 }
2223
2224 if (!arg_full && id_len + 1 + unit_len + type_len + 1 + state_len > columns()) {
2225 unit_len = MAX(33u, columns() - id_len - type_len - state_len - 3)__extension__ ({ const typeof((33u)) __unique_prefix_A2607 = (
(33u)); const typeof((columns() - id_len - type_len - state_len
- 3)) __unique_prefix_B2608 = ((columns() - id_len - type_len
- state_len - 3)); __unique_prefix_A2607 > __unique_prefix_B2608
? __unique_prefix_A2607 : __unique_prefix_B2608; })
;
2226 shorten = true1;
2227 }
2228
2229 if (!arg_no_legend)
2230 printf("%*s %-*s %-*s %-*s\n",
2231 id_len, "JOB",
2232 unit_len, "UNIT",
2233 type_len, "TYPE",
2234 state_len, "STATE");
2235
2236 for (j = jobs; j < jobs + n; j++) {
2237 _cleanup_free___attribute__((cleanup(freep))) char *e = NULL((void*)0);
2238
2239 if (streq(j->state, "running")(strcmp((j->state),("running")) == 0)) {
2240 on = ansi_highlight();
2241 off = ansi_normal();
2242 } else
2243 on = off = "";
2244
2245 e = shorten ? ellipsize(j->name, unit_len, 33) : NULL((void*)0);
2246 printf("%*u %s%-*s%s %-*s %s%-*s%s\n",
2247 id_len, j->id,
2248 on, unit_len, e ? e : j->name, off,
2249 type_len, j->type,
2250 on, state_len, j->state, off);
2251
2252 if (arg_jobs_after)
2253 output_waiting_jobs(bus, j->id, "GetJobAfter", "\twaiting for job");
2254 if (arg_jobs_before)
2255 output_waiting_jobs(bus, j->id, "GetJobBefore", "\tblocking job");
2256 }
2257
2258 if (!arg_no_legend) {
2259 on = ansi_highlight();
2260 off = ansi_normal();
2261
2262 printf("\n%s%u jobs listed%s.\n", on, n, off);
2263 }
2264}
2265
2266static bool_Bool output_show_job(struct job_info *job, char **patterns) {
2267 return strv_fnmatch_or_empty(patterns, job->name, FNM_NOESCAPE(1 << 1));
2268}
2269
2270static int list_jobs(int argc, char *argv[], void *userdata) {
2271 _cleanup_(sd_bus_error_free)__attribute__((cleanup(sd_bus_error_free))) sd_bus_error error = SD_BUS_ERROR_NULL((const sd_bus_error) {(((void*)0)), (((void*)0)), 0});
2272 _cleanup_(sd_bus_message_unrefp)__attribute__((cleanup(sd_bus_message_unrefp))) sd_bus_message *reply = NULL((void*)0);
2273 _cleanup_free___attribute__((cleanup(freep))) struct job_info *jobs = NULL((void*)0);
2274 const char *name, *type, *state;
2275 bool_Bool skipped = false0;
2276 size_t size = 0;
2277 unsigned c = 0;
2278 sd_bus *bus;
2279 uint32_t id;
2280 int r;
2281
2282 r = acquire_bus(BUS_MANAGER, &bus);
2283 if (r < 0)
2284 return r;
2285
2286 r = sd_bus_call_method(
2287 bus,
2288 "org.freedesktop.systemd1",
2289 "/org/freedesktop/systemd1",
2290 "org.freedesktop.systemd1.Manager",
2291 "ListJobs",
2292 &error,
2293 &reply,
2294 NULL((void*)0));
2295 if (r < 0)
2296 return log_error_errno(r, "Failed to list jobs: %s", bus_error_message(&error, r))({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 2296, __func__, "Failed to list jobs: %s"
, bus_error_message(&error, r)) : -abs(_e); })
;
2297
2298 r = sd_bus_message_enter_container(reply, 'a', "(usssoo)");
2299 if (r < 0)
2300 return bus_log_parse_error(r);
2301
2302 while ((r = sd_bus_message_read(reply, "(usssoo)", &id, &name, &type, &state, NULL((void*)0), NULL((void*)0))) > 0) {
2303 struct job_info job = { id, name, type, state };
2304
2305 if (!output_show_job(&job, strv_skip(argv, 1))) {
2306 skipped = true1;
2307 continue;
2308 }
2309
2310 if (!GREEDY_REALLOC(jobs, size, c + 1)greedy_realloc((void**) &(jobs), &(size), (c + 1), sizeof
((jobs)[0]))
)
2311 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 2311, __func__)
;
2312
2313 jobs[c++] = job;
2314 }
2315 if (r < 0)
2316 return bus_log_parse_error(r);
2317
2318 r = sd_bus_message_exit_container(reply);
2319 if (r < 0)
2320 return bus_log_parse_error(r);
2321
2322 (void) pager_open(arg_no_pager, false0);
2323
2324 output_jobs_list(bus, jobs, c, skipped);
2325 return 0;
2326}
2327
2328static int cancel_job(int argc, char *argv[], void *userdata) {
2329 sd_bus *bus;
2330 char **name;
2331 int r = 0;
2332
2333 if (argc <= 1)
2334 return trivial_method(argc, argv, userdata);
2335
2336 r = acquire_bus(BUS_MANAGER, &bus);
2337 if (r < 0)
2338 return r;
2339
2340 polkit_agent_open_maybe();
2341
2342 STRV_FOREACH(name, strv_skip(argv, 1))for ((name) = (strv_skip(argv, 1)); (name) && *(name)
; (name)++)
{
2343 _cleanup_(sd_bus_error_free)__attribute__((cleanup(sd_bus_error_free))) sd_bus_error error = SD_BUS_ERROR_NULL((const sd_bus_error) {(((void*)0)), (((void*)0)), 0});
2344 uint32_t id;
2345 int q;
2346
2347 q = safe_atou32(*name, &id);
2348 if (q < 0)
2349 return log_error_errno(q, "Failed to parse job id \"%s\": %m", *name)({ int _level = ((3)), _e = ((q)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 2349, __func__, "Failed to parse job id \"%s\": %m"
, *name) : -abs(_e); })
;
2350
2351 q = sd_bus_call_method(
2352 bus,
2353 "org.freedesktop.systemd1",
2354 "/org/freedesktop/systemd1",
2355 "org.freedesktop.systemd1.Manager",
2356 "CancelJob",
2357 &error,
2358 NULL((void*)0),
2359 "u", id);
2360 if (q < 0) {
2361 log_error_errno(q, "Failed to cancel job %"PRIu32": %s", id, bus_error_message(&error, q))({ int _level = ((3)), _e = ((q)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 2361, __func__, "Failed to cancel job %"
"u"": %s", id, bus_error_message(&error, q)) : -abs(_e); }
)
;
2362 if (r == 0)
2363 r = q;
2364 }
2365 }
2366
2367 return r;
2368}
2369
2370static int need_daemon_reload(sd_bus *bus, const char *unit) {
2371 _cleanup_(sd_bus_message_unrefp)__attribute__((cleanup(sd_bus_message_unrefp))) sd_bus_message *reply = NULL((void*)0);
2372 const char *path;
2373 int b, r;
2374
2375 /* We ignore all errors here, since this is used to show a
2376 * warning only */
2377
2378 /* We don't use unit_dbus_path_from_name() directly since we
2379 * don't want to load the unit if it isn't loaded. */
2380
2381 r = sd_bus_call_method(
2382 bus,
2383 "org.freedesktop.systemd1",
2384 "/org/freedesktop/systemd1",
2385 "org.freedesktop.systemd1.Manager",
2386 "GetUnit",
2387 NULL((void*)0),
2388 &reply,
2389 "s", unit);
2390 if (r < 0)
2391 return r;
2392
2393 r = sd_bus_message_read(reply, "o", &path);
2394 if (r < 0)
2395 return r;
2396
2397 r = sd_bus_get_property_trivial(
2398 bus,
2399 "org.freedesktop.systemd1",
2400 path,
2401 "org.freedesktop.systemd1.Unit",
2402 "NeedDaemonReload",
2403 NULL((void*)0),
2404 'b', &b);
2405 if (r < 0)
2406 return r;
2407
2408 return b;
2409}
2410
2411static void warn_unit_file_changed(const char *name) {
2412 assert(name)do { if ((__builtin_expect(!!(!(name)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("name"), "../src/systemctl/systemctl.c",
2412, __PRETTY_FUNCTION__); } while (0)
;
2413
2414 log_warning("%sWarning:%s The unit file, source configuration file or drop-ins of %s changed on disk. Run 'systemctl%s daemon-reload' to reload units.",({ int _level = (((4))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 2418, __func__, "%sWarning:%s The unit file, source configuration file or drop-ins of %s changed on disk. Run 'systemctl%s daemon-reload' to reload units."
, ansi_highlight_red(), ansi_normal(), name, arg_scope == UNIT_FILE_SYSTEM
? "" : " --user") : -abs(_e); })
2415 ansi_highlight_red(),({ int _level = (((4))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 2418, __func__, "%sWarning:%s The unit file, source configuration file or drop-ins of %s changed on disk. Run 'systemctl%s daemon-reload' to reload units."
, ansi_highlight_red(), ansi_normal(), name, arg_scope == UNIT_FILE_SYSTEM
? "" : " --user") : -abs(_e); })
2416 ansi_normal(),({ int _level = (((4))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 2418, __func__, "%sWarning:%s The unit file, source configuration file or drop-ins of %s changed on disk. Run 'systemctl%s daemon-reload' to reload units."
, ansi_highlight_red(), ansi_normal(), name, arg_scope == UNIT_FILE_SYSTEM
? "" : " --user") : -abs(_e); })
2417 name,({ int _level = (((4))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 2418, __func__, "%sWarning:%s The unit file, source configuration file or drop-ins of %s changed on disk. Run 'systemctl%s daemon-reload' to reload units."
, ansi_highlight_red(), ansi_normal(), name, arg_scope == UNIT_FILE_SYSTEM
? "" : " --user") : -abs(_e); })
2418 arg_scope == UNIT_FILE_SYSTEM ? "" : " --user")({ int _level = (((4))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 2418, __func__, "%sWarning:%s The unit file, source configuration file or drop-ins of %s changed on disk. Run 'systemctl%s daemon-reload' to reload units."
, ansi_highlight_red(), ansi_normal(), name, arg_scope == UNIT_FILE_SYSTEM
? "" : " --user") : -abs(_e); })
;
2419}
2420
2421static int unit_file_find_path(LookupPaths *lp, const char *unit_name, char **unit_path) {
2422 char **p;
2423
2424 assert(lp)do { if ((__builtin_expect(!!(!(lp)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("lp"), "../src/systemctl/systemctl.c", 2424
, __PRETTY_FUNCTION__); } while (0)
;
2425 assert(unit_name)do { if ((__builtin_expect(!!(!(unit_name)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("unit_name"), "../src/systemctl/systemctl.c"
, 2425, __PRETTY_FUNCTION__); } while (0)
;
2426
2427 STRV_FOREACH(p, lp->search_path)for ((p) = (lp->search_path); (p) && *(p); (p)++) {
2428 _cleanup_free___attribute__((cleanup(freep))) char *path = NULL((void*)0), *lpath = NULL((void*)0);
2429 int r;
2430
2431 path = path_join(NULL((void*)0), *p, unit_name);
2432 if (!path)
2433 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 2433, __func__)
;
2434
2435 r = chase_symlinks(path, arg_root, 0, &lpath);
2436 if (r == -ENOENT2)
2437 continue;
2438 if (r == -ENOMEM12)
2439 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 2439, __func__)
;
2440 if (r < 0)
2441 return log_error_errno(r, "Failed to access path '%s': %m", path)({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 2441, __func__, "Failed to access path '%s': %m"
, path) : -abs(_e); })
;
2442
2443 if (unit_path)
2444 *unit_path = TAKE_PTR(lpath)({ typeof(lpath) _ptr_ = (lpath); (lpath) = ((void*)0); _ptr_
; })
;
2445
2446 return 1;
2447 }
2448
2449 return 0;
2450}
2451
2452static int unit_find_template_path(
2453 const char *unit_name,
2454 LookupPaths *lp,
2455 char **fragment_path,
2456 char **template) {
2457
2458 _cleanup_free___attribute__((cleanup(freep))) char *_template = NULL((void*)0);
2459 int r;
2460
2461 /* Returns 1 if a fragment was found, 0 if not found, negative on error. */
2462
2463 r = unit_file_find_path(lp, unit_name, fragment_path);
2464 if (r != 0)
2465 return r; /* error or found a real unit */
2466
2467 r = unit_name_template(unit_name, &_template);
2468 if (r == -EINVAL22)
2469 return 0; /* not a template, does not exist */
2470 if (r < 0)
2471 return log_error_errno(r, "Failed to determine template name: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 2471, __func__, "Failed to determine template name: %m"
) : -abs(_e); })
;
2472
2473 r = unit_file_find_path(lp, _template, fragment_path);
2474 if (r < 0)
2475 return r;
2476
2477 if (template)
2478 *template = TAKE_PTR(_template)({ typeof(_template) _ptr_ = (_template); (_template) = ((void
*)0); _ptr_; })
;
2479
2480 return r;
2481}
2482
2483static int unit_find_paths(
2484 sd_bus *bus,
2485 const char *unit_name,
2486 LookupPaths *lp,
2487 char **fragment_path,
2488 char ***dropin_paths) {
2489
2490 _cleanup_free___attribute__((cleanup(freep))) char *path = NULL((void*)0);
2491 _cleanup_strv_free___attribute__((cleanup(strv_freep))) char **dropins = NULL((void*)0);
2492 int r;
2493
2494 /**
2495 * Finds where the unit is defined on disk. Returns 0 if the unit
2496 * is not found. Returns 1 if it is found, and sets
2497 * - the path to the unit in *path, if it exists on disk,
2498 * - and a strv of existing drop-ins in *dropins,
2499 * if the arg is not NULL and any dropins were found.
2500 */
2501
2502 assert(unit_name)do { if ((__builtin_expect(!!(!(unit_name)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("unit_name"), "../src/systemctl/systemctl.c"
, 2502, __PRETTY_FUNCTION__); } while (0)
;
2503 assert(fragment_path)do { if ((__builtin_expect(!!(!(fragment_path)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("fragment_path"), "../src/systemctl/systemctl.c"
, 2503, __PRETTY_FUNCTION__); } while (0)
;
2504 assert(lp)do { if ((__builtin_expect(!!(!(lp)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("lp"), "../src/systemctl/systemctl.c", 2504
, __PRETTY_FUNCTION__); } while (0)
;
2505
2506 if (!install_client_side() && !unit_name_is_valid(unit_name, UNIT_NAME_TEMPLATE)) {
2507 _cleanup_(sd_bus_error_free)__attribute__((cleanup(sd_bus_error_free))) sd_bus_error error = SD_BUS_ERROR_NULL((const sd_bus_error) {(((void*)0)), (((void*)0)), 0});
2508 _cleanup_free___attribute__((cleanup(freep))) char *unit = NULL((void*)0);
2509
2510 unit = unit_dbus_path_from_name(unit_name);
2511 if (!unit)
2512 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 2512, __func__)
;
2513
2514 r = sd_bus_get_property_string(
2515 bus,
2516 "org.freedesktop.systemd1",
2517 unit,
2518 "org.freedesktop.systemd1.Unit",
2519 "FragmentPath",
2520 &error,
2521 &path);
2522 if (r < 0)
2523 return log_error_errno(r, "Failed to get FragmentPath: %s", bus_error_message(&error, r))({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 2523, __func__, "Failed to get FragmentPath: %s"
, bus_error_message(&error, r)) : -abs(_e); })
;
2524
2525 if (dropin_paths) {
2526 r = sd_bus_get_property_strv(
2527 bus,
2528 "org.freedesktop.systemd1",
2529 unit,
2530 "org.freedesktop.systemd1.Unit",
2531 "DropInPaths",
2532 &error,
2533 &dropins);
2534 if (r < 0)
2535 return log_error_errno(r, "Failed to get DropInPaths: %s", bus_error_message(&error, r))({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 2535, __func__, "Failed to get DropInPaths: %s"
, bus_error_message(&error, r)) : -abs(_e); })
;
2536 }
2537 } else {
2538 _cleanup_set_free___attribute__((cleanup(set_freep))) Set *names = NULL((void*)0);
2539 _cleanup_free___attribute__((cleanup(freep))) char *template = NULL((void*)0);
2540
2541 names = set_new(NULL)internal_set_new(((void*)0) );
2542 if (!names)
2543 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 2543, __func__)
;
2544
2545 r = unit_find_template_path(unit_name, lp, &path, &template);
2546 if (r < 0)
2547 return r;
2548
2549 if (r > 0)
2550 /* We found the unit file. If we followed symlinks, this name might be
2551 * different then the unit_name with started with. Look for dropins matching
2552 * that "final" name. */
2553 r = set_put(names, basename(path));
2554 else if (!template)
2555 /* No unit file, let's look for dropins matching the original name.
2556 * systemd has fairly complicated rules (based on unit type and provenience),
2557 * which units are allowed not to have the main unit file. We err on the
2558 * side of including too many files, and always try to load dropins. */
2559 r = set_put(names, unit_name);
2560 else
2561 /* The cases where we allow a unit to exist without the main file are
2562 * never valid for templates. Don't try to load dropins in this case. */
2563 goto not_found;
2564
2565 if (r < 0)
2566 return log_error_errno(r, "Failed to add unit name: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 2566, __func__, "Failed to add unit name: %m"
) : -abs(_e); })
;
2567
2568 if (dropin_paths) {
2569 r = unit_file_find_dropin_conf_paths(arg_root, lp->search_path,
2570 NULL((void*)0), names, &dropins);
2571 if (r < 0)
2572 return r;
2573 }
2574 }
2575
2576 r = 0;
2577
2578 if (!isempty(path)) {
2579 *fragment_path = TAKE_PTR(path)({ typeof(path) _ptr_ = (path); (path) = ((void*)0); _ptr_; }
)
;
2580 r = 1;
2581 }
2582
2583 if (dropin_paths && !strv_isempty(dropins)) {
2584 *dropin_paths = TAKE_PTR(dropins)({ typeof(dropins) _ptr_ = (dropins); (dropins) = ((void*)0);
_ptr_; })
;
2585 r = 1;
2586 }
2587 not_found:
2588 if (r == 0 && !arg_force)
2589 log_error("No files found for %s.", unit_name)({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 2589, __func__, "No files found for %s."
, unit_name) : -abs(_e); })
;
2590
2591 return r;
2592}
2593
2594static int get_state_one_unit(sd_bus *bus, const char *name, UnitActiveState *active_state) {
2595 _cleanup_(sd_bus_error_free)__attribute__((cleanup(sd_bus_error_free))) sd_bus_error error = SD_BUS_ERROR_NULL((const sd_bus_error) {(((void*)0)), (((void*)0)), 0});
2596 _cleanup_free___attribute__((cleanup(freep))) char *buf = NULL((void*)0), *path = NULL((void*)0);
2597 UnitActiveState state;
2598 int r;
2599
2600 assert(name)do { if ((__builtin_expect(!!(!(name)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("name"), "../src/systemctl/systemctl.c",
2600, __PRETTY_FUNCTION__); } while (0)
;
2601 assert(active_state)do { if ((__builtin_expect(!!(!(active_state)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("active_state"), "../src/systemctl/systemctl.c"
, 2601, __PRETTY_FUNCTION__); } while (0)
;
2602
2603 path = unit_dbus_path_from_name(name);
2604 if (!path)
2605 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 2605, __func__)
;
2606
2607 r = sd_bus_get_property_string(
2608 bus,
2609 "org.freedesktop.systemd1",
2610 path,
2611 "org.freedesktop.systemd1.Unit",
2612 "ActiveState",
2613 &error,
2614 &buf);
2615 if (r < 0)
2616 return log_error_errno(r, "Failed to retrieve unit state: %s", bus_error_message(&error, r))({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 2616, __func__, "Failed to retrieve unit state: %s"
, bus_error_message(&error, r)) : -abs(_e); })
;
2617
2618 state = unit_active_state_from_string(buf);
2619 if (state == _UNIT_ACTIVE_STATE_INVALID) {
2620 log_error("Invalid unit state '%s' for: %s", buf, name)({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 2620, __func__, "Invalid unit state '%s' for: %s"
, buf, name) : -abs(_e); })
;
2621 return -EINVAL22;
2622 }
2623
2624 *active_state = state;
2625 return 0;
2626}
2627
2628static int unit_is_masked(sd_bus *bus, const char *name) {
2629 _cleanup_free___attribute__((cleanup(freep))) char *load_state = NULL((void*)0);
2630 int r;
2631
2632 r = unit_load_state(bus, name, &load_state);
2633 if (r < 0)
2634 return r;
2635
2636 return streq(load_state, "masked")(strcmp((load_state),("masked")) == 0);
2637}
2638
2639static int check_triggering_units(sd_bus *bus, const char *name) {
2640 _cleanup_(sd_bus_error_free)__attribute__((cleanup(sd_bus_error_free))) sd_bus_error error = SD_BUS_ERROR_NULL((const sd_bus_error) {(((void*)0)), (((void*)0)), 0});
2641 _cleanup_free___attribute__((cleanup(freep))) char *n = NULL((void*)0), *path = NULL((void*)0);
2642 _cleanup_strv_free___attribute__((cleanup(strv_freep))) char **triggered_by = NULL((void*)0);
2643 bool_Bool print_warning_label = true1;
2644 UnitActiveState active_state;
2645 char **i;
2646 int r;
2647
2648 r = unit_name_mangle(name, 0, &n);
2649 if (r < 0)
2650 return log_error_errno(r, "Failed to mangle unit name: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 2650, __func__, "Failed to mangle unit name: %m"
) : -abs(_e); })
;
2651
2652 r = unit_is_masked(bus, n);
2653 if (r != 0)
2654 return r < 0 ? r : 0;
2655
2656 path = unit_dbus_path_from_name(n);
2657 if (!path)
2658 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 2658, __func__)
;
2659
2660 r = sd_bus_get_property_strv(
2661 bus,
2662 "org.freedesktop.systemd1",
2663 path,
2664 "org.freedesktop.systemd1.Unit",
2665 "TriggeredBy",
2666 &error,
2667 &triggered_by);
2668 if (r < 0)
2669 return log_error_errno(r, "Failed to get triggered by array of %s: %s", n, bus_error_message(&error, r))({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 2669, __func__, "Failed to get triggered by array of %s: %s"
, n, bus_error_message(&error, r)) : -abs(_e); })
;
2670
2671 STRV_FOREACH(i, triggered_by)for ((i) = (triggered_by); (i) && *(i); (i)++) {
2672 r = get_state_one_unit(bus, *i, &active_state);
2673 if (r < 0)
2674 return r;
2675
2676 if (!IN_SET(active_state, UNIT_ACTIVE, UNIT_RELOADING)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){UNIT_ACTIVE, UNIT_RELOADING})/sizeof(int
)]; switch(active_state) { case UNIT_ACTIVE: case UNIT_RELOADING
: _found = 1; break; default: break; } _found; })
)
2677 continue;
2678
2679 if (print_warning_label) {
2680 log_warning("Warning: Stopping %s, but it can still be activated by:", n)({ int _level = (((4))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 2680, __func__, "Warning: Stopping %s, but it can still be activated by:"
, n) : -abs(_e); })
;
2681 print_warning_label = false0;
2682 }
2683
2684 log_warning(" %s", *i)({ int _level = (((4))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 2684, __func__, " %s", *i)
: -abs(_e); })
;
2685 }
2686
2687 return 0;
2688}
2689
2690static const struct {
2691 const char *verb; /* systemctl verb */
2692 const char *method; /* Name of the specific D-Bus method */
2693 const char *job_type; /* Job type when passing to the generic EnqueueUnitJob() method */
2694} unit_actions[] = {
2695 { "start", "StartUnit", "start" },
2696 { "stop", "StopUnit", "stop" },
2697 { "condstop", "StopUnit", "stop" }, /* legacy alias */
2698 { "reload", "ReloadUnit", "reload" },
2699 { "restart", "RestartUnit", "restart" },
2700 { "try-restart", "TryRestartUnit", "try-restart" },
2701 { "condrestart", "TryRestartUnit", "try-restart" }, /* legacy alias */
2702 { "reload-or-restart", "ReloadOrRestartUnit", "reload-or-restart" },
2703 { "try-reload-or-restart", "ReloadOrTryRestartUnit", "reload-or-try-restart" },
2704 { "reload-or-try-restart", "ReloadOrTryRestartUnit", "reload-or-try-restart" }, /* legacy alias */
2705 { "condreload", "ReloadOrTryRestartUnit", "reload-or-try-restart" }, /* legacy alias */
2706 { "force-reload", "ReloadOrTryRestartUnit", "reload-or-try-restart" }, /* legacy alias */
2707};
2708
2709static const char *verb_to_method(const char *verb) {
2710 size_t i;
2711
2712 for (i = 0; i < ELEMENTSOF(unit_actions)__extension__ (__builtin_choose_expr( !__builtin_types_compatible_p
(typeof(unit_actions), typeof(&*(unit_actions))), sizeof(
unit_actions)/sizeof((unit_actions)[0]), ((void)0)))
; i++)
2713 if (streq_ptr(unit_actions[i].verb, verb))
2714 return unit_actions[i].method;
2715
2716 return "StartUnit";
2717}
2718
2719static const char *verb_to_job_type(const char *verb) {
2720 size_t i;
2721
2722 for (i = 0; i < ELEMENTSOF(unit_actions)__extension__ (__builtin_choose_expr( !__builtin_types_compatible_p
(typeof(unit_actions), typeof(&*(unit_actions))), sizeof(
unit_actions)/sizeof((unit_actions)[0]), ((void)0)))
; i++)
2723 if (streq_ptr(unit_actions[i].verb, verb))
2724 return unit_actions[i].job_type;
2725
2726 return "start";
2727}
2728
2729typedef struct {
2730 sd_bus_slot *match;
2731 sd_event *event;
2732 Set *unit_paths;
2733 bool_Bool any_failed;
2734} WaitContext;
2735
2736static void wait_context_free(WaitContext *c) {
2737 c->match = sd_bus_slot_unref(c->match);
2738 c->event = sd_event_unref(c->event);
2739 c->unit_paths = set_free_free(c->unit_paths);
2740}
2741
2742static int on_properties_changed(sd_bus_message *m, void *userdata, sd_bus_error *error) {
2743 WaitContext *c = userdata;
2744 const char *path;
2745 int r;
2746
2747 path = sd_bus_message_get_path(m);
2748 if (!set_contains(c->unit_paths, path))
2749 return 0;
2750
2751 /* Check if ActiveState changed to inactive/failed */
2752 /* (s interface, a{sv} changed_properties, as invalidated_properties) */
2753 r = sd_bus_message_skip(m, "s");
2754 if (r < 0)
2755 return bus_log_parse_error(r);
2756
2757 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "{sv}");
2758 if (r < 0)
2759 return bus_log_parse_error(r);
2760
2761 while ((r = sd_bus_message_enter_container(m, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) {
2762 const char *s;
2763
2764 r = sd_bus_message_read(m, "s", &s);
2765 if (r < 0)
2766 return bus_log_parse_error(r);
2767
2768 if (streq(s, "ActiveState")(strcmp((s),("ActiveState")) == 0)) {
2769 bool_Bool is_failed;
2770
2771 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_VARIANT, "s");
2772 if (r < 0)
2773 return bus_log_parse_error(r);
2774
2775 r = sd_bus_message_read(m, "s", &s);
2776 if (r < 0)
2777 return bus_log_parse_error(r);
2778
2779 is_failed = streq(s, "failed")(strcmp((s),("failed")) == 0);
2780 if (streq(s, "inactive")(strcmp((s),("inactive")) == 0) || is_failed) {
2781 log_debug("%s became %s, dropping from --wait tracking", path, s)({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 2781, __func__, "%s became %s, dropping from --wait tracking"
, path, s) : -abs(_e); })
;
2782 free(set_remove(c->unit_paths, path));
2783 c->any_failed = c->any_failed || is_failed;
2784 } else
2785 log_debug("ActiveState on %s changed to %s", path, s)({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 2785, __func__, "ActiveState on %s changed to %s"
, path, s) : -abs(_e); })
;
2786
2787 break; /* no need to dissect the rest of the message */
2788 } else {
2789 /* other property */
2790 r = sd_bus_message_skip(m, "v");
2791 if (r < 0)
2792 return bus_log_parse_error(r);
2793 }
2794 r = sd_bus_message_exit_container(m);
2795 if (r < 0)
2796 return bus_log_parse_error(r);
2797 }
2798 if (r < 0)
2799 return bus_log_parse_error(r);
2800
2801 if (set_isempty(c->unit_paths))
2802 sd_event_exit(c->event, EXIT_SUCCESS0);
2803
2804 return 0;
2805}
2806
2807static int start_unit_one(
2808 sd_bus *bus,
2809 const char *method, /* When using classic per-job bus methods */
2810 const char *job_type, /* When using new-style EnqueueUnitJob() */
2811 const char *name,
2812 const char *mode,
2813 sd_bus_error *error,
2814 BusWaitForJobs *w,
2815 WaitContext *wait_context) {
2816
2817 _cleanup_(sd_bus_message_unrefp)__attribute__((cleanup(sd_bus_message_unrefp))) sd_bus_message *reply = NULL((void*)0);
2818 const char *path;
2819 bool_Bool done = false0;
2820 int r;
2821
2822 assert(method)do { if ((__builtin_expect(!!(!(method)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("method"), "../src/systemctl/systemctl.c"
, 2822, __PRETTY_FUNCTION__); } while (0)
;
2823 assert(name)do { if ((__builtin_expect(!!(!(name)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("name"), "../src/systemctl/systemctl.c",
2823, __PRETTY_FUNCTION__); } while (0)
;
2824 assert(mode)do { if ((__builtin_expect(!!(!(mode)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("mode"), "../src/systemctl/systemctl.c",
2824, __PRETTY_FUNCTION__); } while (0)
;
2825 assert(error)do { if ((__builtin_expect(!!(!(error)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("error"), "../src/systemctl/systemctl.c"
, 2825, __PRETTY_FUNCTION__); } while (0)
;
2826
2827 if (wait_context) {
2828 _cleanup_free___attribute__((cleanup(freep))) char *unit_path = NULL((void*)0);
2829
2830 log_debug("Watching for property changes of %s", name)({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 2830, __func__, "Watching for property changes of %s"
, name) : -abs(_e); })
;
2831 r = sd_bus_call_method(
2832 bus,
2833 "org.freedesktop.systemd1",
2834 "/org/freedesktop/systemd1",
2835 "org.freedesktop.systemd1.Manager",
2836 "RefUnit",
2837 error,
2838 NULL((void*)0),
2839 "s", name);
2840 if (r < 0)
2841 return log_error_errno(r, "Failed to RefUnit %s: %s", name, bus_error_message(error, r))({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 2841, __func__, "Failed to RefUnit %s: %s"
, name, bus_error_message(error, r)) : -abs(_e); })
;
2842
2843 unit_path = unit_dbus_path_from_name(name);
2844 if (!unit_path)
2845 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 2845, __func__)
;
2846
2847 r = set_put_strdup(wait_context->unit_paths, unit_path);
2848 if (r < 0)
2849 return log_error_errno(r, "Failed to add unit path %s to set: %m", unit_path)({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 2849, __func__, "Failed to add unit path %s to set: %m"
, unit_path) : -abs(_e); })
;
2850
2851 r = sd_bus_match_signal_async(bus,
2852 &wait_context->match,
2853 NULL((void*)0),
2854 unit_path,
2855 "org.freedesktop.DBus.Properties",
2856 "PropertiesChanged",
2857 on_properties_changed, NULL((void*)0), wait_context);
2858 if (r < 0)
2859 return log_error_errno(r, "Failed to request match for PropertiesChanged signal: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 2859, __func__, "Failed to request match for PropertiesChanged signal: %m"
) : -abs(_e); })
;
2860 }
2861
2862 log_debug("%s dbus call org.freedesktop.systemd1.Manager %s(%s, %s)",({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 2864, __func__, "%s dbus call org.freedesktop.systemd1.Manager %s(%s, %s)"
, arg_dry_run ? "Would execute" : "Executing", method, name, mode
) : -abs(_e); })
2863 arg_dry_run ? "Would execute" : "Executing",({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 2864, __func__, "%s dbus call org.freedesktop.systemd1.Manager %s(%s, %s)"
, arg_dry_run ? "Would execute" : "Executing", method, name, mode
) : -abs(_e); })
2864 method, name, mode)({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 2864, __func__, "%s dbus call org.freedesktop.systemd1.Manager %s(%s, %s)"
, arg_dry_run ? "Would execute" : "Executing", method, name, mode
) : -abs(_e); })
;
2865
2866 if (arg_dry_run)
2867 return 0;
2868
2869 if (arg_show_transaction) {
2870 _cleanup_(sd_bus_error_free)__attribute__((cleanup(sd_bus_error_free))) sd_bus_error enqueue_error = SD_BUS_ERROR_NULL((const sd_bus_error) {(((void*)0)), (((void*)0)), 0});
2871
2872 /* Use the new, fancy EnqueueUnitJob() API if the user wants us to print the transaction */
2873 r = sd_bus_call_method(
2874 bus,
2875 "org.freedesktop.systemd1",
2876 "/org/freedesktop/systemd1",
2877 "org.freedesktop.systemd1.Manager",
2878 "EnqueueUnitJob",
2879 &enqueue_error,
2880 &reply,
2881 "sss",
2882 name, job_type, mode);
2883 if (r < 0) {
2884 if (!sd_bus_error_has_name(&enqueue_error, SD_BUS_ERROR_UNKNOWN_METHOD"org.freedesktop.DBus.Error.UnknownMethod")) {
2885 (void) sd_bus_error_copy(error, &enqueue_error);
2886 sd_bus_error_free(&enqueue_error);
2887 goto fail;
2888 }
2889
2890 /* Hmm, the API is not yet available. Let's use the classic API instead (see below). */
2891 log_notice("--show-transaction not supported by this service manager, proceeding without.")({ int _level = (((5))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 2891, __func__, "--show-transaction not supported by this service manager, proceeding without."
) : -abs(_e); })
;
2892 } else {
2893 const char *u, *jt;
2894 uint32_t id;
2895
2896 r = sd_bus_message_read(reply, "uosos", &id, &path, &u, NULL((void*)0), &jt);
2897 if (r < 0)
2898 return bus_log_parse_error(r);
2899
2900 log_info("Enqueued anchor job %" PRIu32 " %s/%s.", id, u, jt)({ int _level = (((6))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 2900, __func__, "Enqueued anchor job %"
"u" " %s/%s.", id, u, jt) : -abs(_e); })
;
2901
2902 r = sd_bus_message_enter_container(reply, 'a', "(uosos)");
2903 if (r < 0)
2904 return bus_log_parse_error(r);
2905 for (;;) {
2906 r = sd_bus_message_read(reply, "(uosos)", &id, NULL((void*)0), &u, NULL((void*)0), &jt);
2907 if (r < 0)
2908 return bus_log_parse_error(r);
2909 if (r == 0)
2910 break;
2911
2912 log_info("Enqueued auxiliary job %" PRIu32 " %s/%s.", id, u, jt)({ int _level = (((6))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 2912, __func__, "Enqueued auxiliary job %"
"u" " %s/%s.", id, u, jt) : -abs(_e); })
;
2913 }
2914
2915 r = sd_bus_message_exit_container(reply);
2916 if (r < 0)
2917 return bus_log_parse_error(r);
2918
2919 done = true1;
2920 }
2921 }
2922
2923 if (!done) {
2924 r = sd_bus_call_method(
2925 bus,
2926 "org.freedesktop.systemd1",
2927 "/org/freedesktop/systemd1",
2928 "org.freedesktop.systemd1.Manager",
2929 method,
2930 error,
2931 &reply,
2932 "ss", name, mode);
2933 if (r < 0)
2934 goto fail;
2935
2936 r = sd_bus_message_read(reply, "o", &path);
2937 if (r < 0)
2938 return bus_log_parse_error(r);
2939 }
2940
2941 if (need_daemon_reload(bus, name) > 0)
2942 warn_unit_file_changed(name);
2943
2944 if (w) {
2945 log_debug("Adding %s to the set", path)({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 2945, __func__, "Adding %s to the set"
, path) : -abs(_e); })
;
2946 r = bus_wait_for_jobs_add(w, path);
2947 if (r < 0)
2948 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 2948, __func__)
;
2949 }
2950
2951 return 0;
2952
2953fail:
2954 /* There's always a fallback possible for legacy actions. */
2955 if (arg_action != ACTION_SYSTEMCTL)
2956 return r;
2957
2958 log_error_errno(r, "Failed to %s %s: %s", job_type, name, bus_error_message(error, r))({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 2958, __func__, "Failed to %s %s: %s"
, job_type, name, bus_error_message(error, r)) : -abs(_e); })
;
2959
2960 if (!sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT"org.freedesktop.systemd1.NoSuchUnit") &&
2961 !sd_bus_error_has_name(error, BUS_ERROR_UNIT_MASKED"org.freedesktop.systemd1.UnitMasked") &&
2962 !sd_bus_error_has_name(error, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE"org.freedesktop.systemd1.JobTypeNotApplicable"))
2963 log_error("See %s logs and 'systemctl%s status%s %s' for details.",({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 2967, __func__, "See %s logs and 'systemctl%s status%s %s' for details."
, arg_scope == UNIT_FILE_SYSTEM ? "system" : "user", arg_scope
== UNIT_FILE_SYSTEM ? "" : " --user", name[0] == '-' ? " --"
: "", name) : -abs(_e); })
2964 arg_scope == UNIT_FILE_SYSTEM ? "system" : "user",({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 2967, __func__, "See %s logs and 'systemctl%s status%s %s' for details."
, arg_scope == UNIT_FILE_SYSTEM ? "system" : "user", arg_scope
== UNIT_FILE_SYSTEM ? "" : " --user", name[0] == '-' ? " --"
: "", name) : -abs(_e); })
2965 arg_scope == UNIT_FILE_SYSTEM ? "" : " --user",({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 2967, __func__, "See %s logs and 'systemctl%s status%s %s' for details."
, arg_scope == UNIT_FILE_SYSTEM ? "system" : "user", arg_scope
== UNIT_FILE_SYSTEM ? "" : " --user", name[0] == '-' ? " --"
: "", name) : -abs(_e); })
2966 name[0] == '-' ? " --" : "",({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 2967, __func__, "See %s logs and 'systemctl%s status%s %s' for details."
, arg_scope == UNIT_FILE_SYSTEM ? "system" : "user", arg_scope
== UNIT_FILE_SYSTEM ? "" : " --user", name[0] == '-' ? " --"
: "", name) : -abs(_e); })
2967 name)({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 2967, __func__, "See %s logs and 'systemctl%s status%s %s' for details."
, arg_scope == UNIT_FILE_SYSTEM ? "system" : "user", arg_scope
== UNIT_FILE_SYSTEM ? "" : " --user", name[0] == '-' ? " --"
: "", name) : -abs(_e); })
;
2968
2969 return r;
2970}
2971
2972static int expand_names(sd_bus *bus, char **names, const char* suffix, char ***ret) {
2973 _cleanup_strv_free___attribute__((cleanup(strv_freep))) char **mangled = NULL((void*)0), **globs = NULL((void*)0);
2974 char **name;
2975 int r, i;
2976
2977 assert(bus)do { if ((__builtin_expect(!!(!(bus)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/systemctl/systemctl.c", 2977
, __PRETTY_FUNCTION__); } while (0)
;
2978 assert(ret)do { if ((__builtin_expect(!!(!(ret)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("ret"), "../src/systemctl/systemctl.c", 2978
, __PRETTY_FUNCTION__); } while (0)
;
2979
2980 STRV_FOREACH(name, names)for ((name) = (names); (name) && *(name); (name)++) {
2981 char *t;
2982 UnitNameMangle options = UNIT_NAME_MANGLE_GLOB | (arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN);
2983
2984 if (suffix)
2985 r = unit_name_mangle_with_suffix(*name, options, suffix, &t);
2986 else
2987 r = unit_name_mangle(*name, options, &t);
2988 if (r < 0)
2989 return log_error_errno(r, "Failed to mangle name: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 2989, __func__, "Failed to mangle name: %m"
) : -abs(_e); })
;
2990
2991 if (string_is_glob(t))
2992 r = strv_consume(&globs, t);
2993 else
2994 r = strv_consume(&mangled, t);
2995 if (r < 0)
2996 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 2996, __func__)
;
2997 }
2998
2999 /* Query the manager only if any of the names are a glob, since
3000 * this is fairly expensive */
3001 if (!strv_isempty(globs)) {
3002 _cleanup_(sd_bus_message_unrefp)__attribute__((cleanup(sd_bus_message_unrefp))) sd_bus_message *reply = NULL((void*)0);
3003 _cleanup_free___attribute__((cleanup(freep))) UnitInfo *unit_infos = NULL((void*)0);
3004 size_t allocated, n;
3005
3006 r = get_unit_list(bus, NULL((void*)0), globs, &unit_infos, 0, &reply);
3007 if (r < 0)
3008 return r;
3009
3010 n = strv_length(mangled);
3011 allocated = n + 1;
3012
3013 for (i = 0; i < r; i++) {
3014 if (!GREEDY_REALLOC(mangled, allocated, n+2)greedy_realloc((void**) &(mangled), &(allocated), (n+
2), sizeof((mangled)[0]))
)
3015 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 3015, __func__)
;
3016
3017 mangled[n] = strdup(unit_infos[i].id);
3018 if (!mangled[n])
3019 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 3019, __func__)
;
3020
3021 mangled[++n] = NULL((void*)0);
3022 }
3023 }
3024
3025 *ret = TAKE_PTR(mangled)({ typeof(mangled) _ptr_ = (mangled); (mangled) = ((void*)0);
_ptr_; })
;
3026 return 0;
3027}
3028
3029static const struct {
3030 const char *target;
3031 const char *verb;
3032 const char *mode;
3033} action_table[_ACTION_MAX] = {
3034 [ACTION_HALT] = { SPECIAL_HALT_TARGET"halt.target", "halt", "replace-irreversibly" },
3035 [ACTION_POWEROFF] = { SPECIAL_POWEROFF_TARGET"poweroff.target", "poweroff", "replace-irreversibly" },
3036 [ACTION_REBOOT] = { SPECIAL_REBOOT_TARGET"reboot.target", "reboot", "replace-irreversibly" },
3037 [ACTION_KEXEC] = { SPECIAL_KEXEC_TARGET"kexec.target", "kexec", "replace-irreversibly" },
3038 [ACTION_RUNLEVEL2] = { SPECIAL_MULTI_USER_TARGET"multi-user.target", NULL((void*)0), "isolate" },
3039 [ACTION_RUNLEVEL3] = { SPECIAL_MULTI_USER_TARGET"multi-user.target", NULL((void*)0), "isolate" },
3040 [ACTION_RUNLEVEL4] = { SPECIAL_MULTI_USER_TARGET"multi-user.target", NULL((void*)0), "isolate" },
3041 [ACTION_RUNLEVEL5] = { SPECIAL_GRAPHICAL_TARGET"graphical.target", NULL((void*)0), "isolate" },
3042 [ACTION_RESCUE] = { SPECIAL_RESCUE_TARGET"rescue.target", "rescue", "isolate" },
3043 [ACTION_EMERGENCY] = { SPECIAL_EMERGENCY_TARGET"emergency.target", "emergency", "isolate" },
3044 [ACTION_DEFAULT] = { SPECIAL_DEFAULT_TARGET"default.target", "default", "isolate" },
3045 [ACTION_EXIT] = { SPECIAL_EXIT_TARGET"exit.target", "exit", "replace-irreversibly" },
3046 [ACTION_SUSPEND] = { SPECIAL_SUSPEND_TARGET"suspend.target", "suspend", "replace-irreversibly" },
3047 [ACTION_HIBERNATE] = { SPECIAL_HIBERNATE_TARGET"hibernate.target", "hibernate", "replace-irreversibly" },
3048 [ACTION_HYBRID_SLEEP] = { SPECIAL_HYBRID_SLEEP_TARGET"hybrid-sleep.target", "hybrid-sleep", "replace-irreversibly" },
3049 [ACTION_SUSPEND_THEN_HIBERNATE] = { SPECIAL_SUSPEND_THEN_HIBERNATE_TARGET"suspend-then-hibernate.target", "suspend-then-hibernate", "replace-irreversibly" },
3050};
3051
3052static enum action verb_to_action(const char *verb) {
3053 enum action i;
3054
3055 for (i = 0; i < _ACTION_MAX; i++)
3056 if (streq_ptr(action_table[i].verb, verb))
3057 return i;
3058
3059 return _ACTION_INVALID;
3060}
3061
3062static const char** make_extra_args(const char *extra_args[static 4]) {
3063 size_t n = 0;
3064
3065 if (arg_scope != UNIT_FILE_SYSTEM)
3066 extra_args[n++] = "--user";
3067
3068 if (arg_transport == BUS_TRANSPORT_REMOTE) {
3069 extra_args[n++] = "-H";
3070 extra_args[n++] = arg_host;
3071 } else if (arg_transport == BUS_TRANSPORT_MACHINE) {
3072 extra_args[n++] = "-M";
3073 extra_args[n++] = arg_host;
3074 } else
3075 assert(arg_transport == BUS_TRANSPORT_LOCAL)do { if ((__builtin_expect(!!(!(arg_transport == BUS_TRANSPORT_LOCAL
)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("arg_transport == BUS_TRANSPORT_LOCAL"
), "../src/systemctl/systemctl.c", 3075, __PRETTY_FUNCTION__)
; } while (0)
;
3076
3077 extra_args[n] = NULL((void*)0);
3078 return extra_args;
3079}
3080
3081static int start_unit(int argc, char *argv[], void *userdata) {
3082 _cleanup_(bus_wait_for_jobs_freep)__attribute__((cleanup(bus_wait_for_jobs_freep))) BusWaitForJobs *w = NULL((void*)0);
3083 _cleanup_(wait_context_free)__attribute__((cleanup(wait_context_free))) WaitContext wait_context = {};
3084 const char *method, *job_type, *mode, *one_name, *suffix = NULL((void*)0);
3085 _cleanup_strv_free___attribute__((cleanup(strv_freep))) char **names = NULL((void*)0);
3086 int r, ret = EXIT_SUCCESS0;
3087 sd_bus *bus;
3088 char **name;
3089
3090 if (arg_wait && !STR_IN_SET(argv[0], "start", "restart")(!!strv_find((((char**) ((const char*[]) { "start", "restart"
, ((void*)0) }))), (argv[0])))
) {
3091 log_error("--wait may only be used with the 'start' or 'restart' commands.")({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 3091, __func__, "--wait may only be used with the 'start' or 'restart' commands."
) : -abs(_e); })
;
3092 return -EINVAL22;
3093 }
3094
3095 /* we cannot do sender tracking on the private bus, so we need the full
3096 * one for RefUnit to implement --wait */
3097 r = acquire_bus(arg_wait ? BUS_FULL : BUS_MANAGER, &bus);
3098 if (r < 0)
3099 return r;
3100
3101 ask_password_agent_open_if_enabled();
3102 polkit_agent_open_maybe();
3103
3104 if (arg_action == ACTION_SYSTEMCTL) {
3105 enum action action;
3106
3107 action = verb_to_action(argv[0]);
3108
3109 if (action != _ACTION_INVALID) {
3110 /* A command in style "systemctl reboot", "systemctl poweroff", … */
3111 method = "StartUnit";
3112 job_type = "start";
3113 mode = action_table[action].mode;
3114 one_name = action_table[action].target;
3115 } else {
3116 if (streq(argv[0], "isolate")(strcmp((argv[0]),("isolate")) == 0)) {
3117 /* A "systemctl isolate <unit1> <unit2> …" command */
3118 method = "StartUnit";
3119 job_type = "start";
3120 mode = "isolate";
3121 suffix = ".target";
3122 } else {
3123 /* A command in style of "systemctl start <unit1> <unit2> …", "sysemctl stop <unit1> <unit2> …" and so on */
3124 method = verb_to_method(argv[0]);
3125 job_type = verb_to_job_type(argv[0]);
3126 mode = arg_job_mode;
3127 }
3128 one_name = NULL((void*)0);
3129 }
3130 } else {
3131 /* A SysV legacy command such as "halt", "reboot", "poweroff", … */
3132 assert(arg_action >= 0 && arg_action < _ACTION_MAX)do { if ((__builtin_expect(!!(!(arg_action >= 0 &&
arg_action < _ACTION_MAX)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD
, ("arg_action >= 0 && arg_action < _ACTION_MAX"
), "../src/systemctl/systemctl.c", 3132, __PRETTY_FUNCTION__)
; } while (0)
;
3133 assert(action_table[arg_action].target)do { if ((__builtin_expect(!!(!(action_table[arg_action].target
)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("action_table[arg_action].target"
), "../src/systemctl/systemctl.c", 3133, __PRETTY_FUNCTION__)
; } while (0)
;
3134 assert(action_table[arg_action].mode)do { if ((__builtin_expect(!!(!(action_table[arg_action].mode
)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("action_table[arg_action].mode"
), "../src/systemctl/systemctl.c", 3134, __PRETTY_FUNCTION__)
; } while (0)
;
3135
3136 method = "StartUnit";
3137 job_type = "start";
3138 mode = action_table[arg_action].mode;
3139 one_name = action_table[arg_action].target;
3140 }
3141
3142 if (one_name)
3143 names = strv_new(one_name, NULL((void*)0));
3144 else {
3145 r = expand_names(bus, strv_skip(argv, 1), suffix, &names);
3146 if (r < 0)
3147 return log_error_errno(r, "Failed to expand names: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 3147, __func__, "Failed to expand names: %m"
) : -abs(_e); })
;
3148 }
3149
3150 if (!arg_no_block) {
3151 r = bus_wait_for_jobs_new(bus, &w);
3152 if (r < 0)
3153 return log_error_errno(r, "Could not watch jobs: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 3153, __func__, "Could not watch jobs: %m"
) : -abs(_e); })
;
3154 }
3155
3156 if (arg_wait) {
3157 wait_context.unit_paths = set_new(&string_hash_ops)internal_set_new(&string_hash_ops );
3158 if (!wait_context.unit_paths)
3159 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 3159, __func__)
;
3160
3161 r = sd_bus_call_method_async(
3162 bus,
3163 NULL((void*)0),
3164 "org.freedesktop.systemd1",
3165 "/org/freedesktop/systemd1",
3166 "org.freedesktop.systemd1.Manager",
3167 "Subscribe",
3168 NULL((void*)0), NULL((void*)0),
3169 NULL((void*)0));
3170 if (r < 0)
3171 return log_error_errno(r, "Failed to enable subscription: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 3171, __func__, "Failed to enable subscription: %m"
) : -abs(_e); })
;
3172
3173 r = sd_event_default(&wait_context.event);
3174 if (r < 0)
3175 return log_error_errno(r, "Failed to allocate event loop: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 3175, __func__, "Failed to allocate event loop: %m"
) : -abs(_e); })
;
3176
3177 r = sd_bus_attach_event(bus, wait_context.event, 0);
3178 if (r < 0)
3179 return log_error_errno(r, "Failed to attach bus to event loop: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 3179, __func__, "Failed to attach bus to event loop: %m"
) : -abs(_e); })
;
3180 }
3181
3182 STRV_FOREACH(name, names)for ((name) = (names); (name) && *(name); (name)++) {
3183 _cleanup_(sd_bus_error_free)__attribute__((cleanup(sd_bus_error_free))) sd_bus_error error = SD_BUS_ERROR_NULL((const sd_bus_error) {(((void*)0)), (((void*)0)), 0});
3184
3185 r = start_unit_one(bus, method, job_type, *name, mode, &error, w, arg_wait ? &wait_context : NULL((void*)0));
3186 if (ret == EXIT_SUCCESS0 && r < 0)
3187 ret = translate_bus_error_to_exit_status(r, &error);
3188 }
3189
3190 if (!arg_no_block) {
3191 const char* extra_args[4];
3192
3193 r = bus_wait_for_jobs(w, arg_quiet, make_extra_args(extra_args));
3194 if (r < 0)
3195 return r;
3196
3197 /* When stopping units, warn if they can still be triggered by
3198 * another active unit (socket, path, timer) */
3199 if (!arg_quiet && streq(method, "StopUnit")(strcmp((method),("StopUnit")) == 0))
3200 STRV_FOREACH(name, names)for ((name) = (names); (name) && *(name); (name)++)
3201 check_triggering_units(bus, *name);
3202 }
3203
3204 if (ret == EXIT_SUCCESS0 && arg_wait && !set_isempty(wait_context.unit_paths)) {
3205 r = sd_event_loop(wait_context.event);
3206 if (r < 0)
3207 return log_error_errno(r, "Failed to run event loop: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 3207, __func__, "Failed to run event loop: %m"
) : -abs(_e); })
;
3208 if (wait_context.any_failed)
3209 ret = EXIT_FAILURE1;
3210 }
3211
3212 return ret;
3213}
3214
3215#if ENABLE_LOGIND1
3216static int logind_set_wall_message(void) {
3217 _cleanup_(sd_bus_error_free)__attribute__((cleanup(sd_bus_error_free))) sd_bus_error error = SD_BUS_ERROR_NULL((const sd_bus_error) {(((void*)0)), (((void*)0)), 0});
3218 sd_bus *bus;
3219 _cleanup_free___attribute__((cleanup(freep))) char *m = NULL((void*)0);
3220 int r;
3221
3222 r = acquire_bus(BUS_FULL, &bus);
3223 if (r < 0)
3224 return r;
3225
3226 m = strv_join(arg_wall, " ");
3227 if (!m)
3228 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 3228, __func__)
;
3229
3230 log_debug("%s wall message \"%s\".", arg_dry_run ? "Would set" : "Setting", m)({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 3230, __func__, "%s wall message \"%s\"."
, arg_dry_run ? "Would set" : "Setting", m) : -abs(_e); })
;
3231 if (arg_dry_run)
3232 return 0;
3233
3234 r = sd_bus_call_method(
3235 bus,
3236 "org.freedesktop.login1",
3237 "/org/freedesktop/login1",
3238 "org.freedesktop.login1.Manager",
3239 "SetWallMessage",
3240 &error,
3241 NULL((void*)0),
3242 "sb",
3243 m,
3244 !arg_no_wall);
3245
3246 if (r < 0)
3247 return log_warning_errno(r, "Failed to set wall message, ignoring: %s", bus_error_message(&error, r))({ int _level = ((4)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 3247, __func__, "Failed to set wall message, ignoring: %s"
, bus_error_message(&error, r)) : -abs(_e); })
;
3248 return 0;
3249}
3250#endif
3251
3252/* Ask systemd-logind, which might grant access to unprivileged users through polkit */
3253static int logind_reboot(enum action a) {
3254#if ENABLE_LOGIND1
3255 static const struct {
3256 const char *method;
3257 const char *description;
3258 } actions[_ACTION_MAX] = {
3259 [ACTION_POWEROFF] = { "PowerOff", "power off system" },
3260 [ACTION_REBOOT] = { "Reboot", "reboot system" },
3261 [ACTION_HALT] = { "Halt", "halt system" },
3262 [ACTION_SUSPEND] = { "Suspend", "suspend system" },
3263 [ACTION_HIBERNATE] = { "Hibernate", "hibernate system" },
3264 [ACTION_HYBRID_SLEEP] = { "HybridSleep", "put system into hybrid sleep" },
3265 [ACTION_SUSPEND_THEN_HIBERNATE] = { "SuspendThenHibernate", "suspend system, hibernate later" },
3266 };
3267
3268 _cleanup_(sd_bus_error_free)__attribute__((cleanup(sd_bus_error_free))) sd_bus_error error = SD_BUS_ERROR_NULL((const sd_bus_error) {(((void*)0)), (((void*)0)), 0});
3269 sd_bus *bus;
3270 int r;
3271
3272 if (a < 0 || a >= _ACTION_MAX || !actions[a].method)
3273 return -EINVAL22;
3274
3275 r = acquire_bus(BUS_FULL, &bus);
3276 if (r < 0)
3277 return r;
3278
3279 polkit_agent_open_maybe();
3280 (void) logind_set_wall_message();
3281
3282 log_debug("%s org.freedesktop.login1.Manager %s dbus call.", arg_dry_run ? "Would execute" : "Executing", actions[a].method)({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 3282, __func__, "%s org.freedesktop.login1.Manager %s dbus call."
, arg_dry_run ? "Would execute" : "Executing", actions[a].method
) : -abs(_e); })
;
3283
3284 if (arg_dry_run)
3285 return 0;
3286
3287 r = sd_bus_call_method(
3288 bus,
3289 "org.freedesktop.login1",
3290 "/org/freedesktop/login1",
3291 "org.freedesktop.login1.Manager",
3292 actions[a].method,
3293 &error,
3294 NULL((void*)0),
3295 "b", arg_ask_password);
3296 if (r < 0)
3297 return log_error_errno(r, "Failed to %s via logind: %s", actions[a].description, bus_error_message(&error, r))({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 3297, __func__, "Failed to %s via logind: %s"
, actions[a].description, bus_error_message(&error, r)) :
-abs(_e); })
;
3298
3299 return 0;
3300#else
3301 return -ENOSYS38;
3302#endif
3303}
3304
3305static int logind_check_inhibitors(enum action a) {
3306#if ENABLE_LOGIND1
3307 _cleanup_(sd_bus_message_unrefp)__attribute__((cleanup(sd_bus_message_unrefp))) sd_bus_message *reply = NULL((void*)0);
3308 _cleanup_strv_free___attribute__((cleanup(strv_freep))) char **sessions = NULL((void*)0);
3309 const char *what, *who, *why, *mode;
3310 uint32_t uid, pid;
3311 sd_bus *bus;
3312 unsigned c = 0;
3313 char **s;
3314 int r;
3315
3316 if (arg_ignore_inhibitors || arg_force > 0)
3317 return 0;
3318
3319 if (arg_when > 0)
3320 return 0;
3321
3322 if (geteuid() == 0)
3323 return 0;
3324
3325 if (!on_tty())
3326 return 0;
3327
3328 if (arg_transport != BUS_TRANSPORT_LOCAL)
3329 return 0;
3330
3331 r = acquire_bus(BUS_FULL, &bus);
3332 if (r < 0)
3333 return r;
3334
3335 r = sd_bus_call_method(
3336 bus,
3337 "org.freedesktop.login1",
3338 "/org/freedesktop/login1",
3339 "org.freedesktop.login1.Manager",
3340 "ListInhibitors",
3341 NULL((void*)0),
3342 &reply,
3343 NULL((void*)0));
3344 if (r < 0)
3345 /* If logind is not around, then there are no inhibitors... */
3346 return 0;
3347
3348 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssuu)");
3349 if (r < 0)
3350 return bus_log_parse_error(r);
3351
3352 while ((r = sd_bus_message_read(reply, "(ssssuu)", &what, &who, &why, &mode, &uid, &pid)) > 0) {
3353 _cleanup_free___attribute__((cleanup(freep))) char *comm = NULL((void*)0), *user = NULL((void*)0);
3354 _cleanup_strv_free___attribute__((cleanup(strv_freep))) char **sv = NULL((void*)0);
3355
3356 if (!streq(mode, "block")(strcmp((mode),("block")) == 0))
3357 continue;
3358
3359 sv = strv_split(what, ":");
3360 if (!sv)
3361 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 3361, __func__)
;
3362
3363 if (!pid_is_valid((pid_t) pid)) {
3364 log_error("Invalid PID "PID_FMT".", (pid_t) pid)({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 3364, __func__, "Invalid PID "
"%" "i"".", (pid_t) pid) : -abs(_e); })
;
3365 return -ERANGE34;
3366 }
3367
3368 if (!strv_contains(sv,(!!strv_find((sv), (({ _Bool _found = 0; static __attribute__
((unused)) char _static_assert__macros_need_to_be_extended[20
- sizeof((int[]){ACTION_HALT, ACTION_POWEROFF, ACTION_REBOOT
, ACTION_KEXEC})/sizeof(int)]; switch(a) { case ACTION_HALT: case
ACTION_POWEROFF: case ACTION_REBOOT: case ACTION_KEXEC: _found
= 1; break; default: break; } _found; }) ? "shutdown" : "sleep"
)))
3369 IN_SET(a,(!!strv_find((sv), (({ _Bool _found = 0; static __attribute__
((unused)) char _static_assert__macros_need_to_be_extended[20
- sizeof((int[]){ACTION_HALT, ACTION_POWEROFF, ACTION_REBOOT
, ACTION_KEXEC})/sizeof(int)]; switch(a) { case ACTION_HALT: case
ACTION_POWEROFF: case ACTION_REBOOT: case ACTION_KEXEC: _found
= 1; break; default: break; } _found; }) ? "shutdown" : "sleep"
)))
3370 ACTION_HALT,(!!strv_find((sv), (({ _Bool _found = 0; static __attribute__
((unused)) char _static_assert__macros_need_to_be_extended[20
- sizeof((int[]){ACTION_HALT, ACTION_POWEROFF, ACTION_REBOOT
, ACTION_KEXEC})/sizeof(int)]; switch(a) { case ACTION_HALT: case
ACTION_POWEROFF: case ACTION_REBOOT: case ACTION_KEXEC: _found
= 1; break; default: break; } _found; }) ? "shutdown" : "sleep"
)))
3371 ACTION_POWEROFF,(!!strv_find((sv), (({ _Bool _found = 0; static __attribute__
((unused)) char _static_assert__macros_need_to_be_extended[20
- sizeof((int[]){ACTION_HALT, ACTION_POWEROFF, ACTION_REBOOT
, ACTION_KEXEC})/sizeof(int)]; switch(a) { case ACTION_HALT: case
ACTION_POWEROFF: case ACTION_REBOOT: case ACTION_KEXEC: _found
= 1; break; default: break; } _found; }) ? "shutdown" : "sleep"
)))
3372 ACTION_REBOOT,(!!strv_find((sv), (({ _Bool _found = 0; static __attribute__
((unused)) char _static_assert__macros_need_to_be_extended[20
- sizeof((int[]){ACTION_HALT, ACTION_POWEROFF, ACTION_REBOOT
, ACTION_KEXEC})/sizeof(int)]; switch(a) { case ACTION_HALT: case
ACTION_POWEROFF: case ACTION_REBOOT: case ACTION_KEXEC: _found
= 1; break; default: break; } _found; }) ? "shutdown" : "sleep"
)))
3373 ACTION_KEXEC) ? "shutdown" : "sleep")(!!strv_find((sv), (({ _Bool _found = 0; static __attribute__
((unused)) char _static_assert__macros_need_to_be_extended[20
- sizeof((int[]){ACTION_HALT, ACTION_POWEROFF, ACTION_REBOOT
, ACTION_KEXEC})/sizeof(int)]; switch(a) { case ACTION_HALT: case
ACTION_POWEROFF: case ACTION_REBOOT: case ACTION_KEXEC: _found
= 1; break; default: break; } _found; }) ? "shutdown" : "sleep"
)))
)
3374 continue;
3375
3376 get_process_comm(pid, &comm);
3377 user = uid_to_name(uid);
3378
3379 log_warning("Operation inhibited by \"%s\" (PID "PID_FMT" \"%s\", user %s), reason is \"%s\".",({ int _level = (((4))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 3380, __func__, "Operation inhibited by \"%s\" (PID "
"%" "i"" \"%s\", user %s), reason is \"%s\".", who, (pid_t) pid
, strna(comm), strna(user), why) : -abs(_e); })
3380 who, (pid_t) pid, strna(comm), strna(user), why)({ int _level = (((4))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 3380, __func__, "Operation inhibited by \"%s\" (PID "
"%" "i"" \"%s\", user %s), reason is \"%s\".", who, (pid_t) pid
, strna(comm), strna(user), why) : -abs(_e); })
;
3381
3382 c++;
3383 }
3384 if (r < 0)
3385 return bus_log_parse_error(r);
3386
3387 r = sd_bus_message_exit_container(reply);
3388 if (r < 0)
3389 return bus_log_parse_error(r);
3390
3391 /* Check for current sessions */
3392 sd_get_sessions(&sessions);
3393 STRV_FOREACH(s, sessions)for ((s) = (sessions); (s) && *(s); (s)++) {
3394 _cleanup_free___attribute__((cleanup(freep))) char *type = NULL((void*)0), *tty = NULL((void*)0), *seat = NULL((void*)0), *user = NULL((void*)0), *service = NULL((void*)0), *class = NULL((void*)0);
3395
3396 if (sd_session_get_uid(*s, &uid) < 0 || uid == getuid())
3397 continue;
3398
3399 if (sd_session_get_class(*s, &class) < 0 || !streq(class, "user")(strcmp((class),("user")) == 0))
3400 continue;
3401
3402 if (sd_session_get_type(*s, &type) < 0 || !STR_IN_SET(type, "x11", "wayland", "tty", "mir")(!!strv_find((((char**) ((const char*[]) { "x11", "wayland", "tty"
, "mir", ((void*)0) }))), (type)))
)
3403 continue;
3404
3405 sd_session_get_tty(*s, &tty);
3406 sd_session_get_seat(*s, &seat);
3407 sd_session_get_service(*s, &service);
3408 user = uid_to_name(uid);
3409
3410 log_warning("User %s is logged in on %s.", strna(user), isempty(tty) ? (isempty(seat) ? strna(service) : seat) : tty)({ int _level = (((4))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 3410, __func__, "User %s is logged in on %s."
, strna(user), isempty(tty) ? (isempty(seat) ? strna(service)
: seat) : tty) : -abs(_e); })
;
3411 c++;
3412 }
3413
3414 if (c <= 0)
3415 return 0;
3416
3417 log_error("Please retry operation after closing inhibitors and logging out other users.\nAlternatively, ignore inhibitors and users with 'systemctl %s -i'.",({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 3418, __func__, "Please retry operation after closing inhibitors and logging out other users.\nAlternatively, ignore inhibitors and users with 'systemctl %s -i'."
, action_table[a].verb) : -abs(_e); })
3418 action_table[a].verb)({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 3418, __func__, "Please retry operation after closing inhibitors and logging out other users.\nAlternatively, ignore inhibitors and users with 'systemctl %s -i'."
, action_table[a].verb) : -abs(_e); })
;
3419
3420 return -EPERM1;
3421#else
3422 return 0;
3423#endif
3424}
3425
3426static int logind_prepare_firmware_setup(void) {
3427#if ENABLE_LOGIND1
3428 _cleanup_(sd_bus_error_free)__attribute__((cleanup(sd_bus_error_free))) sd_bus_error error = SD_BUS_ERROR_NULL((const sd_bus_error) {(((void*)0)), (((void*)0)), 0});
3429 sd_bus *bus;
3430 int r;
3431
3432 r = acquire_bus(BUS_FULL, &bus);
3433 if (r < 0)
3434 return r;
3435
3436 r = sd_bus_call_method(
3437 bus,
3438 "org.freedesktop.login1",
3439 "/org/freedesktop/login1",
3440 "org.freedesktop.login1.Manager",
3441 "SetRebootToFirmwareSetup",
3442 &error,
3443 NULL((void*)0),
3444 "b", true1);
3445 if (r < 0)
3446 return log_error_errno(r, "Cannot indicate to EFI to boot into setup mode: %s", bus_error_message(&error, r))({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 3446, __func__, "Cannot indicate to EFI to boot into setup mode: %s"
, bus_error_message(&error, r)) : -abs(_e); })
;
3447
3448 return 0;
3449#else
3450 log_error("Cannot remotely indicate to EFI to boot into setup mode.")({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 3450, __func__, "Cannot remotely indicate to EFI to boot into setup mode."
) : -abs(_e); })
;
3451 return -ENOSYS38;
3452#endif
3453}
3454
3455static int prepare_firmware_setup(void) {
3456 int r;
3457
3458 if (!arg_firmware_setup)
3459 return 0;
3460
3461 if (arg_transport == BUS_TRANSPORT_LOCAL) {
3462
3463 r = efi_set_reboot_to_firmware(true1);
3464 if (r < 0)
3465 log_debug_errno(r, "Cannot indicate to EFI to boot into setup mode, will retry via logind: %m")({ int _level = ((7)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 3465, __func__, "Cannot indicate to EFI to boot into setup mode, will retry via logind: %m"
) : -abs(_e); })
;
3466 else
3467 return r;
3468 }
3469
3470 return logind_prepare_firmware_setup();
3471}
3472
3473static int load_kexec_kernel(void) {
3474 _cleanup_(boot_config_free)__attribute__((cleanup(boot_config_free))) BootConfig config = {};
3475 _cleanup_free___attribute__((cleanup(freep))) char *where = NULL((void*)0), *kernel = NULL((void*)0), *initrd = NULL((void*)0), *options = NULL((void*)0);
3476 const BootEntry *e;
3477 pid_t pid;
3478 int r;
3479
3480 if (kexec_loaded()) {
3481 log_debug("Kexec kernel already loaded.")({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 3481, __func__, "Kexec kernel already loaded."
) : -abs(_e); })
;
3482 return 0;
3483 }
3484
3485 if (access(KEXEC"/usr/sbin/kexec", X_OK1) < 0)
3486 return log_error_errno(errno, KEXEC" is not available: %m")({ int _level = ((3)), _e = (((*__errno_location ()))), _realm
= (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >=
((_level) & 0x07)) ? log_internal_realm(((_realm) <<
10 | (_level)), _e, "../src/systemctl/systemctl.c", 3486, __func__
, "/usr/sbin/kexec"" is not available: %m") : -abs(_e); })
;
3487
3488 r = find_esp_and_warn(arg_esp_path, false0, &where, NULL((void*)0), NULL((void*)0), NULL((void*)0), NULL((void*)0));
3489 if (r == -ENOKEY126) /* find_esp_and_warn() doesn't warn about this case */
3490 return log_error_errno(r, "Cannot find the ESP partition mount point.")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 3490, __func__, "Cannot find the ESP partition mount point."
) : -abs(_e); })
;
3491 if (r < 0) /* But it logs about all these cases, hence don't log here again */
3492 return r;
3493
3494 r = boot_entries_load_config(where, &config);
3495 if (r < 0)
3496 return log_error_errno(r, "Failed to load bootspec config from \"%s/loader\": %m", where)({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 3496, __func__, "Failed to load bootspec config from \"%s/loader\": %m"
, where) : -abs(_e); })
;
3497
3498 if (config.default_entry < 0) {
3499 log_error("No entry suitable as default, refusing to guess.")({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 3499, __func__, "No entry suitable as default, refusing to guess."
) : -abs(_e); })
;
3500 return -ENOENT2;
3501 }
3502 e = &config.entries[config.default_entry];
3503
3504 if (strv_length(e->initrd) > 1) {
3505 log_error("Boot entry specifies multiple initrds, which is not supported currently.")({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 3505, __func__, "Boot entry specifies multiple initrds, which is not supported currently."
) : -abs(_e); })
;
3506 return -EINVAL22;
3507 }
3508
3509 kernel = path_join(NULL((void*)0), where, e->kernel);
3510 if (!strv_isempty(e->initrd))
3511 initrd = path_join(NULL((void*)0), where, *e->initrd);
3512 options = strv_join(e->options, " ");
3513 if (!options)
3514 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 3514, __func__)
;
3515
3516 log_full(arg_quiet ? LOG_DEBUG : LOG_INFO,({ int _level = (((arg_quiet ? 7 : 6))), _e = ((0)), _realm =
(LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= (
(_level) & 0x07)) ? log_internal_realm(((_realm) <<
10 | (_level)), _e, "../src/systemctl/systemctl.c", 3521, __func__
, "%s ""/usr/sbin/kexec"" --load \"%s\" --append \"%s\"%s%s%s"
, arg_dry_run ? "Would run" : "Running", kernel, options, initrd
? " --initrd \"" : ((void*)0), strempty(initrd), initrd ? "\""
: "") : -abs(_e); })
3517 "%s "KEXEC" --load \"%s\" --append \"%s\"%s%s%s",({ int _level = (((arg_quiet ? 7 : 6))), _e = ((0)), _realm =
(LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= (
(_level) & 0x07)) ? log_internal_realm(((_realm) <<
10 | (_level)), _e, "../src/systemctl/systemctl.c", 3521, __func__
, "%s ""/usr/sbin/kexec"" --load \"%s\" --append \"%s\"%s%s%s"
, arg_dry_run ? "Would run" : "Running", kernel, options, initrd
? " --initrd \"" : ((void*)0), strempty(initrd), initrd ? "\""
: "") : -abs(_e); })
3518 arg_dry_run ? "Would run" : "Running",({ int _level = (((arg_quiet ? 7 : 6))), _e = ((0)), _realm =
(LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= (
(_level) & 0x07)) ? log_internal_realm(((_realm) <<
10 | (_level)), _e, "../src/systemctl/systemctl.c", 3521, __func__
, "%s ""/usr/sbin/kexec"" --load \"%s\" --append \"%s\"%s%s%s"
, arg_dry_run ? "Would run" : "Running", kernel, options, initrd
? " --initrd \"" : ((void*)0), strempty(initrd), initrd ? "\""
: "") : -abs(_e); })
3519 kernel,({ int _level = (((arg_quiet ? 7 : 6))), _e = ((0)), _realm =
(LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= (
(_level) & 0x07)) ? log_internal_realm(((_realm) <<
10 | (_level)), _e, "../src/systemctl/systemctl.c", 3521, __func__
, "%s ""/usr/sbin/kexec"" --load \"%s\" --append \"%s\"%s%s%s"
, arg_dry_run ? "Would run" : "Running", kernel, options, initrd
? " --initrd \"" : ((void*)0), strempty(initrd), initrd ? "\""
: "") : -abs(_e); })
3520 options,({ int _level = (((arg_quiet ? 7 : 6))), _e = ((0)), _realm =
(LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= (
(_level) & 0x07)) ? log_internal_realm(((_realm) <<
10 | (_level)), _e, "../src/systemctl/systemctl.c", 3521, __func__
, "%s ""/usr/sbin/kexec"" --load \"%s\" --append \"%s\"%s%s%s"
, arg_dry_run ? "Would run" : "Running", kernel, options, initrd
? " --initrd \"" : ((void*)0), strempty(initrd), initrd ? "\""
: "") : -abs(_e); })
3521 initrd ? " --initrd \"" : NULL, strempty(initrd), initrd ? "\"" : "")({ int _level = (((arg_quiet ? 7 : 6))), _e = ((0)), _realm =
(LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= (
(_level) & 0x07)) ? log_internal_realm(((_realm) <<
10 | (_level)), _e, "../src/systemctl/systemctl.c", 3521, __func__
, "%s ""/usr/sbin/kexec"" --load \"%s\" --append \"%s\"%s%s%s"
, arg_dry_run ? "Would run" : "Running", kernel, options, initrd
? " --initrd \"" : ((void*)0), strempty(initrd), initrd ? "\""
: "") : -abs(_e); })
;
3522 if (arg_dry_run)
3523 return 0;
3524
3525 r = safe_fork("(kexec)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_LOG, &pid);
3526 if (r < 0)
3527 return r;
3528 if (r == 0) {
3529 const char* const args[] = {
3530 KEXEC"/usr/sbin/kexec",
3531 "--load", kernel,
3532 "--append", options,
3533 initrd ? "--initrd" : NULL((void*)0), initrd,
3534 NULL((void*)0) };
3535
3536 /* Child */
3537 execv(args[0], (char * const *) args);
3538 _exit(EXIT_FAILURE1);
3539 }
3540
3541 r = wait_for_terminate_and_check("kexec", pid, WAIT_LOG);
3542 if (r < 0)
3543 return r;
3544 if (r > 0)
3545 /* Command failed */
3546 return -EPROTO71;
3547 return 0;
3548}
3549
3550static int set_exit_code(uint8_t code) {
3551 _cleanup_(sd_bus_error_free)__attribute__((cleanup(sd_bus_error_free))) sd_bus_error error = SD_BUS_ERROR_NULL((const sd_bus_error) {(((void*)0)), (((void*)0)), 0});
3552 sd_bus *bus;
3553 int r;
3554
3555 r = acquire_bus(BUS_MANAGER, &bus);
3556 if (r < 0)
3557 return r;
3558
3559 r = sd_bus_call_method(
3560 bus,
3561 "org.freedesktop.systemd1",
3562 "/org/freedesktop/systemd1",
3563 "org.freedesktop.systemd1.Manager",
3564 "SetExitCode",
3565 &error,
3566 NULL((void*)0),
3567 "y", code);
3568 if (r < 0)
3569 return log_error_errno(r, "Failed to set exit code: %s", bus_error_message(&error, r))({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 3569, __func__, "Failed to set exit code: %s"
, bus_error_message(&error, r)) : -abs(_e); })
;
3570
3571 return 0;
3572}
3573
3574static int start_special(int argc, char *argv[], void *userdata) {
3575 enum action a;
3576 int r;
3577 bool_Bool termination_action; /* an action that terminates the manager,
3578 * can be performed also by signal. */
3579
3580 assert(argv)do { if ((__builtin_expect(!!(!(argv)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("argv"), "../src/systemctl/systemctl.c",
3580, __PRETTY_FUNCTION__); } while (0)
;
3581
3582 a = verb_to_action(argv[0]);
3583
3584 r = logind_check_inhibitors(a);
3585 if (r < 0)
3586 return r;
3587
3588 if (arg_force >= 2) {
3589 r = must_be_root();
3590 if (r < 0)
3591 return r;
3592 }
3593
3594 r = prepare_firmware_setup();
3595 if (r < 0)
3596 return r;
3597
3598 if (a == ACTION_REBOOT && argc > 1) {
3599 r = update_reboot_parameter_and_warn(argv[1]);
3600 if (r < 0)
3601 return r;
3602
3603 } else if (a == ACTION_KEXEC) {
3604 r = load_kexec_kernel();
3605 if (r < 0 && arg_force >= 1)
3606 log_notice("Failed to load kexec kernel, continuing without.")({ int _level = (((5))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 3606, __func__, "Failed to load kexec kernel, continuing without."
) : -abs(_e); })
;
3607 else if (r < 0)
3608 return r;
3609
3610 } else if (a == ACTION_EXIT && argc > 1) {
3611 uint8_t code;
3612
3613 /* If the exit code is not given on the command line,
3614 * don't reset it to zero: just keep it as it might
3615 * have been set previously. */
3616
3617 r = safe_atou8(argv[1], &code);
3618 if (r < 0)
3619 return log_error_errno(r, "Invalid exit code.")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 3619, __func__, "Invalid exit code."
) : -abs(_e); })
;
3620
3621 r = set_exit_code(code);
3622 if (r < 0)
3623 return r;
3624 }
3625
3626 termination_action = IN_SET(a,({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){ACTION_HALT, ACTION_POWEROFF, ACTION_REBOOT
})/sizeof(int)]; switch(a) { case ACTION_HALT: case ACTION_POWEROFF
: case ACTION_REBOOT: _found = 1; break; default: break; } _found
; })
3627 ACTION_HALT,({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){ACTION_HALT, ACTION_POWEROFF, ACTION_REBOOT
})/sizeof(int)]; switch(a) { case ACTION_HALT: case ACTION_POWEROFF
: case ACTION_REBOOT: _found = 1; break; default: break; } _found
; })
3628 ACTION_POWEROFF,({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){ACTION_HALT, ACTION_POWEROFF, ACTION_REBOOT
})/sizeof(int)]; switch(a) { case ACTION_HALT: case ACTION_POWEROFF
: case ACTION_REBOOT: _found = 1; break; default: break; } _found
; })
3629 ACTION_REBOOT)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){ACTION_HALT, ACTION_POWEROFF, ACTION_REBOOT
})/sizeof(int)]; switch(a) { case ACTION_HALT: case ACTION_POWEROFF
: case ACTION_REBOOT: _found = 1; break; default: break; } _found
; })
;
3630 if (termination_action && arg_force >= 2)
3631 return halt_now(a);
3632
3633 if (arg_force >= 1 &&
3634 (termination_action || IN_SET(a, ACTION_KEXEC, ACTION_EXIT)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){ACTION_KEXEC, ACTION_EXIT})/sizeof(int)]
; switch(a) { case ACTION_KEXEC: case ACTION_EXIT: _found = 1
; break; default: break; } _found; })
))
3635 r = trivial_method(argc, argv, userdata);
3636 else {
3637 /* First try logind, to allow authentication with polkit */
3638 if (IN_SET(a,({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){ACTION_POWEROFF, ACTION_REBOOT, ACTION_HALT
, ACTION_SUSPEND, ACTION_HIBERNATE, ACTION_HYBRID_SLEEP, ACTION_SUSPEND_THEN_HIBERNATE
})/sizeof(int)]; switch(a) { case ACTION_POWEROFF: case ACTION_REBOOT
: case ACTION_HALT: case ACTION_SUSPEND: case ACTION_HIBERNATE
: case ACTION_HYBRID_SLEEP: case ACTION_SUSPEND_THEN_HIBERNATE
: _found = 1; break; default: break; } _found; })
3639 ACTION_POWEROFF,({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){ACTION_POWEROFF, ACTION_REBOOT, ACTION_HALT
, ACTION_SUSPEND, ACTION_HIBERNATE, ACTION_HYBRID_SLEEP, ACTION_SUSPEND_THEN_HIBERNATE
})/sizeof(int)]; switch(a) { case ACTION_POWEROFF: case ACTION_REBOOT
: case ACTION_HALT: case ACTION_SUSPEND: case ACTION_HIBERNATE
: case ACTION_HYBRID_SLEEP: case ACTION_SUSPEND_THEN_HIBERNATE
: _found = 1; break; default: break; } _found; })
3640 ACTION_REBOOT,({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){ACTION_POWEROFF, ACTION_REBOOT, ACTION_HALT
, ACTION_SUSPEND, ACTION_HIBERNATE, ACTION_HYBRID_SLEEP, ACTION_SUSPEND_THEN_HIBERNATE
})/sizeof(int)]; switch(a) { case ACTION_POWEROFF: case ACTION_REBOOT
: case ACTION_HALT: case ACTION_SUSPEND: case ACTION_HIBERNATE
: case ACTION_HYBRID_SLEEP: case ACTION_SUSPEND_THEN_HIBERNATE
: _found = 1; break; default: break; } _found; })
3641 ACTION_HALT,({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){ACTION_POWEROFF, ACTION_REBOOT, ACTION_HALT
, ACTION_SUSPEND, ACTION_HIBERNATE, ACTION_HYBRID_SLEEP, ACTION_SUSPEND_THEN_HIBERNATE
})/sizeof(int)]; switch(a) { case ACTION_POWEROFF: case ACTION_REBOOT
: case ACTION_HALT: case ACTION_SUSPEND: case ACTION_HIBERNATE
: case ACTION_HYBRID_SLEEP: case ACTION_SUSPEND_THEN_HIBERNATE
: _found = 1; break; default: break; } _found; })
3642 ACTION_SUSPEND,({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){ACTION_POWEROFF, ACTION_REBOOT, ACTION_HALT
, ACTION_SUSPEND, ACTION_HIBERNATE, ACTION_HYBRID_SLEEP, ACTION_SUSPEND_THEN_HIBERNATE
})/sizeof(int)]; switch(a) { case ACTION_POWEROFF: case ACTION_REBOOT
: case ACTION_HALT: case ACTION_SUSPEND: case ACTION_HIBERNATE
: case ACTION_HYBRID_SLEEP: case ACTION_SUSPEND_THEN_HIBERNATE
: _found = 1; break; default: break; } _found; })
3643 ACTION_HIBERNATE,({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){ACTION_POWEROFF, ACTION_REBOOT, ACTION_HALT
, ACTION_SUSPEND, ACTION_HIBERNATE, ACTION_HYBRID_SLEEP, ACTION_SUSPEND_THEN_HIBERNATE
})/sizeof(int)]; switch(a) { case ACTION_POWEROFF: case ACTION_REBOOT
: case ACTION_HALT: case ACTION_SUSPEND: case ACTION_HIBERNATE
: case ACTION_HYBRID_SLEEP: case ACTION_SUSPEND_THEN_HIBERNATE
: _found = 1; break; default: break; } _found; })
3644 ACTION_HYBRID_SLEEP,({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){ACTION_POWEROFF, ACTION_REBOOT, ACTION_HALT
, ACTION_SUSPEND, ACTION_HIBERNATE, ACTION_HYBRID_SLEEP, ACTION_SUSPEND_THEN_HIBERNATE
})/sizeof(int)]; switch(a) { case ACTION_POWEROFF: case ACTION_REBOOT
: case ACTION_HALT: case ACTION_SUSPEND: case ACTION_HIBERNATE
: case ACTION_HYBRID_SLEEP: case ACTION_SUSPEND_THEN_HIBERNATE
: _found = 1; break; default: break; } _found; })
3645 ACTION_SUSPEND_THEN_HIBERNATE)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){ACTION_POWEROFF, ACTION_REBOOT, ACTION_HALT
, ACTION_SUSPEND, ACTION_HIBERNATE, ACTION_HYBRID_SLEEP, ACTION_SUSPEND_THEN_HIBERNATE
})/sizeof(int)]; switch(a) { case ACTION_POWEROFF: case ACTION_REBOOT
: case ACTION_HALT: case ACTION_SUSPEND: case ACTION_HIBERNATE
: case ACTION_HYBRID_SLEEP: case ACTION_SUSPEND_THEN_HIBERNATE
: _found = 1; break; default: break; } _found; })
) {
3646
3647 r = logind_reboot(a);
3648 if (r >= 0)
3649 return r;
3650 if (IN_SET(r, -EOPNOTSUPP, -EINPROGRESS)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){-95, -115})/sizeof(int)]; switch(r) { case
-95: case -115: _found = 1; break; default: break; } _found;
})
)
3651 /* requested operation is not supported or already in progress */
3652 return r;
3653
3654 /* On all other errors, try low-level operation. In order to minimize the difference between
3655 * operation with and without logind, we explicitly enable non-blocking mode for this, as
3656 * logind's shutdown operations are always non-blocking. */
3657
3658 arg_no_block = true1;
3659
3660 } else if (IN_SET(a, ACTION_EXIT, ACTION_KEXEC)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){ACTION_EXIT, ACTION_KEXEC})/sizeof(int)]
; switch(a) { case ACTION_EXIT: case ACTION_KEXEC: _found = 1
; break; default: break; } _found; })
)
3661 /* Since exit/kexec are so close in behaviour to power-off/reboot, let's also make them
3662 * asynchronous, in order to not confuse the user needlessly with unexpected behaviour. */
3663 arg_no_block = true1;
3664
3665 r = start_unit(argc, argv, userdata);
3666 }
3667
3668 if (termination_action && arg_force < 2 &&
3669 IN_SET(r, -ENOENT, -ETIMEDOUT)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){-2, -110})/sizeof(int)]; switch(r) { case
-2: case -110: _found = 1; break; default: break; } _found; }
)
)
3670 log_notice("It is possible to perform action directly, see discussion of --force --force in man:systemctl(1).")({ int _level = (((5))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 3670, __func__, "It is possible to perform action directly, see discussion of --force --force in man:systemctl(1)."
) : -abs(_e); })
;
3671
3672 return r;
3673}
3674
3675static int start_system_special(int argc, char *argv[], void *userdata) {
3676 /* Like start_special above, but raises an error when running in user mode */
3677
3678 if (arg_scope != UNIT_FILE_SYSTEM) {
3679 log_error("Bad action for %s mode.",({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 3680, __func__, "Bad action for %s mode."
, arg_scope == UNIT_FILE_GLOBAL ? "--global" : "--user") : -abs
(_e); })
3680 arg_scope == UNIT_FILE_GLOBAL ? "--global" : "--user")({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 3680, __func__, "Bad action for %s mode."
, arg_scope == UNIT_FILE_GLOBAL ? "--global" : "--user") : -abs
(_e); })
;
3681 return -EINVAL22;
3682 }
3683
3684 return start_special(argc, argv, userdata);
3685}
3686
3687static int check_unit_generic(int code, const UnitActiveState good_states[], int nb_states, char **args) {
3688 _cleanup_strv_free___attribute__((cleanup(strv_freep))) char **names = NULL((void*)0);
3689 UnitActiveState active_state;
3690 sd_bus *bus;
3691 char **name;
3692 int r, i;
3693 bool_Bool found = false0;
3694
3695 r = acquire_bus(BUS_MANAGER, &bus);
3696 if (r < 0)
3697 return r;
3698
3699 r = expand_names(bus, args, NULL((void*)0), &names);
3700 if (r < 0)
3701 return log_error_errno(r, "Failed to expand names: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 3701, __func__, "Failed to expand names: %m"
) : -abs(_e); })
;
3702
3703 STRV_FOREACH(name, names)for ((name) = (names); (name) && *(name); (name)++) {
3704 r = get_state_one_unit(bus, *name, &active_state);
3705 if (r < 0)
3706 return r;
3707
3708 if (!arg_quiet)
3709 puts(unit_active_state_to_string(active_state));
3710
3711 for (i = 0; i < nb_states; ++i)
3712 if (good_states[i] == active_state)
3713 found = true1;
3714 }
3715
3716 /* use the given return code for the case that we won't find
3717 * any unit which matches the list */
3718 return found ? 0 : code;
3719}
3720
3721static int check_unit_active(int argc, char *argv[], void *userdata) {
3722 const UnitActiveState states[] = { UNIT_ACTIVE, UNIT_RELOADING };
3723 /* According to LSB: 3, "program is not running" */
3724 return check_unit_generic(EXIT_PROGRAM_NOT_RUNNING, states, ELEMENTSOF(states)__extension__ (__builtin_choose_expr( !__builtin_types_compatible_p
(typeof(states), typeof(&*(states))), sizeof(states)/sizeof
((states)[0]), ((void)0)))
, strv_skip(argv, 1));
3725}
3726
3727static int check_unit_failed(int argc, char *argv[], void *userdata) {
3728 const UnitActiveState states[] = { UNIT_FAILED };
3729 return check_unit_generic(EXIT_PROGRAM_DEAD_AND_PID_EXISTS, states, ELEMENTSOF(states)__extension__ (__builtin_choose_expr( !__builtin_types_compatible_p
(typeof(states), typeof(&*(states))), sizeof(states)/sizeof
((states)[0]), ((void)0)))
, strv_skip(argv, 1));
3730}
3731
3732static int kill_unit(int argc, char *argv[], void *userdata) {
3733 _cleanup_strv_free___attribute__((cleanup(strv_freep))) char **names = NULL((void*)0);
3734 char *kill_who = NULL((void*)0), **name;
3735 sd_bus *bus;
3736 int r, q;
3737
3738 r = acquire_bus(BUS_MANAGER, &bus);
3739 if (r < 0)
3740 return r;
3741
3742 polkit_agent_open_maybe();
3743
3744 if (!arg_kill_who)
3745 arg_kill_who = "all";
3746
3747 /* --fail was specified */
3748 if (streq(arg_job_mode, "fail")(strcmp((arg_job_mode),("fail")) == 0))
3749 kill_who = strjoina(arg_kill_who, "-fail")({ const char *_appendees_[] = { arg_kill_who, "-fail" }; char
*_d_, *_p_; size_t _len_ = 0; size_t _i_; for (_i_ = 0; _i_ <
__extension__ (__builtin_choose_expr( !__builtin_types_compatible_p
(typeof(_appendees_), typeof(&*(_appendees_))), sizeof(_appendees_
)/sizeof((_appendees_)[0]), ((void)0))) && _appendees_
[_i_]; _i_++) _len_ += strlen(_appendees_[_i_]); _p_ = _d_ = __builtin_alloca
(_len_ + 1); for (_i_ = 0; _i_ < __extension__ (__builtin_choose_expr
( !__builtin_types_compatible_p(typeof(_appendees_), typeof(&
*(_appendees_))), sizeof(_appendees_)/sizeof((_appendees_)[0]
), ((void)0))) && _appendees_[_i_]; _i_++) _p_ = stpcpy
(_p_, _appendees_[_i_]); *_p_ = 0; _d_; })
;
3750
3751 r = expand_names(bus, strv_skip(argv, 1), NULL((void*)0), &names);
3752 if (r < 0)
3753 return log_error_errno(r, "Failed to expand names: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 3753, __func__, "Failed to expand names: %m"
) : -abs(_e); })
;
3754
3755 STRV_FOREACH(name, names)for ((name) = (names); (name) && *(name); (name)++) {
3756 _cleanup_(sd_bus_error_free)__attribute__((cleanup(sd_bus_error_free))) sd_bus_error error = SD_BUS_ERROR_NULL((const sd_bus_error) {(((void*)0)), (((void*)0)), 0});
3757
3758 q = sd_bus_call_method(
3759 bus,
3760 "org.freedesktop.systemd1",
3761 "/org/freedesktop/systemd1",
3762 "org.freedesktop.systemd1.Manager",
3763 "KillUnit",
3764 &error,
3765 NULL((void*)0),
3766 "ssi", *name, kill_who ? kill_who : arg_kill_who, arg_signal);
3767 if (q < 0) {
3768 log_error_errno(q, "Failed to kill unit %s: %s", *name, bus_error_message(&error, q))({ int _level = ((3)), _e = ((q)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 3768, __func__, "Failed to kill unit %s: %s"
, *name, bus_error_message(&error, q)) : -abs(_e); })
;
3769 if (r == 0)
3770 r = q;
3771 }
3772 }
3773
3774 return r;
3775}
3776static int freeze_or_thaw_unit(int argc, char *argv[], void *userdata) {
3777 _cleanup_(bus_wait_for_units_freep)__attribute__((cleanup(bus_wait_for_units_freep))) BusWaitForUnits *w = NULL((void*)0);
3778 _cleanup_strv_free___attribute__((cleanup(strv_freep))) char **names = NULL((void*)0);
3779 int r, ret = EXIT_SUCCESS0;
3780 char **name;
3781 const char *method;
3782 sd_bus *bus;
3783
3784 r = acquire_bus(BUS_FULL, &bus);
3785 if (r < 0)
3786 return r;
3787
3788 polkit_agent_open_maybe();
3789
3790 r = expand_names(bus, strv_skip(argv, 1), NULL((void*)0), &names);
3791 if (r < 0)
3792 return log_error_errno(r, "Failed to expand names: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 3792, __func__, "Failed to expand names: %m"
) : -abs(_e); })
;
3793
3794 if (!arg_no_block) {
3795 r = bus_wait_for_units_new(bus, &w);
3796 if (r < 0)
3797 return log_error_errno(r, "Failed to allocate unit waiter: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 3797, __func__, "Failed to allocate unit waiter: %m"
) : -abs(_e); })
;
3798 }
3799
3800 if (streq(argv[0], "freeze")(strcmp((argv[0]),("freeze")) == 0))
3801 method = "FreezeUnit";
3802 else if (streq(argv[0], "thaw")(strcmp((argv[0]),("thaw")) == 0))
3803 method = "ThawUnit";
3804 else
3805 assert_not_reached("Unhandled method")do { log_assert_failed_unreachable_realm(LOG_REALM_SYSTEMD, (
"Unhandled method"), "../src/systemctl/systemctl.c", 3805, __PRETTY_FUNCTION__
); } while (0)
;
3806
3807 STRV_FOREACH(name, names)for ((name) = (names); (name) && *(name); (name)++) {
3808 _cleanup_(sd_bus_error_free)__attribute__((cleanup(sd_bus_error_free))) sd_bus_error error = SD_BUS_ERROR_NULL((const sd_bus_error) {(((void*)0)), (((void*)0)), 0});
3809 _cleanup_(sd_bus_message_unrefp)__attribute__((cleanup(sd_bus_message_unrefp))) sd_bus_message *m = NULL((void*)0);
3810
3811 if (w) {
3812 /* If we shall wait for the cleaning to complete, let's add a ref on the unit first */
3813 r = sd_bus_call_method(
3814 bus,
3815 "org.freedesktop.systemd1",
3816 "/org/freedesktop/systemd1",
3817 "org.freedesktop.systemd1.Manager",
3818 "RefUnit",
3819 &error,
3820 NULL((void*)0),
3821 "s", *name);
3822 if (r < 0) {
3823 log_error_errno(r, "Failed to add reference to unit %s: %s", *name, bus_error_message(&error, r))({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 3823, __func__, "Failed to add reference to unit %s: %s"
, *name, bus_error_message(&error, r)) : -abs(_e); })
;
3824 if (ret == EXIT_SUCCESS0)
3825 ret = r;
3826 continue;
3827 }
3828 }
3829
3830 r = sd_bus_message_new_method_call(
3831 bus,
3832 &m,
3833 "org.freedesktop.systemd1",
3834 "/org/freedesktop/systemd1",
3835 "org.freedesktop.systemd1.Manager",
3836 method);
3837 if (r < 0)
3838 return bus_log_create_error(r);
3839
3840 r = sd_bus_message_append(m, "s", *name);
3841 if (r < 0)
3842 return bus_log_create_error(r);
3843
3844 r = sd_bus_call(bus, m, 0, &error, NULL((void*)0));
3845 if (r < 0) {
3846 log_error_errno(r, "Failed to %s unit %s: %s", argv[0], *name, bus_error_message(&error, r))({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 3846, __func__, "Failed to %s unit %s: %s"
, argv[0], *name, bus_error_message(&error, r)) : -abs(_e
); })
;
3847 if (ret == EXIT_SUCCESS0) {
3848 ret = r;
3849 continue;
3850 }
3851 }
3852
3853 if (w) {
3854 r = bus_wait_for_units_add_unit(w, *name, BUS_WAIT_REFFED|BUS_WAIT_FOR_MAINTENANCE_END, NULL((void*)0), NULL((void*)0));
3855 if (r < 0)
3856 return log_error_errno(r, "Failed to watch unit %s: %m", *name)({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 3856, __func__, "Failed to watch unit %s: %m"
, *name) : -abs(_e); })
;
3857 }
3858 }
3859
3860 r = bus_wait_for_units_run(w);
3861 if (r < 0)
3862 return log_error_errno(r, "Failed to wait for units: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 3862, __func__, "Failed to wait for units: %m"
) : -abs(_e); })
;
3863 if (r == BUS_WAIT_FAILURE)
3864 ret = EXIT_FAILURE1;
3865
3866 return ret;
3867}
3868
3869typedef struct ExecStatusInfo {
3870 char *name;
3871
3872 char *path;
3873 char **argv;
3874
3875 bool_Bool ignore;
3876
3877 usec_t start_timestamp;
3878 usec_t exit_timestamp;
3879 pid_t pid;
3880 int code;
3881 int status;
3882
3883 LIST_FIELDS(struct ExecStatusInfo, exec)struct ExecStatusInfo *exec_next, *exec_prev;
3884} ExecStatusInfo;
3885
3886static void exec_status_info_free(ExecStatusInfo *i) {
3887 assert(i)do { if ((__builtin_expect(!!(!(i)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("i"), "../src/systemctl/systemctl.c", 3887
, __PRETTY_FUNCTION__); } while (0)
;
3888
3889 free(i->name);
3890 free(i->path);
3891 strv_free(i->argv);
3892 free(i);
3893}
3894
3895static int exec_status_info_deserialize(sd_bus_message *m, ExecStatusInfo *i) {
3896 uint64_t start_timestamp, exit_timestamp, start_timestamp_monotonic, exit_timestamp_monotonic;
3897 const char *path;
3898 uint32_t pid;
3899 int32_t code, status;
3900 int ignore, r;
3901
3902 assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("m"), "../src/systemctl/systemctl.c", 3902
, __PRETTY_FUNCTION__); } while (0)
;
3903 assert(i)do { if ((__builtin_expect(!!(!(i)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("i"), "../src/systemctl/systemctl.c", 3903
, __PRETTY_FUNCTION__); } while (0)
;
3904
3905 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_STRUCT, "sasbttttuii");
3906 if (r < 0)
3907 return bus_log_parse_error(r);
3908 else if (r == 0)
3909 return 0;
3910
3911 r = sd_bus_message_read(m, "s", &path);
3912 if (r < 0)
3913 return bus_log_parse_error(r);
3914
3915 i->path = strdup(path);
3916 if (!i->path)
3917 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 3917, __func__)
;
3918
3919 r = sd_bus_message_read_strv(m, &i->argv);
3920 if (r < 0)
3921 return bus_log_parse_error(r);
3922
3923 r = sd_bus_message_read(m,
3924 "bttttuii",
3925 &ignore,
3926 &start_timestamp, &start_timestamp_monotonic,
3927 &exit_timestamp, &exit_timestamp_monotonic,
3928 &pid,
3929 &code, &status);
3930 if (r < 0)
3931 return bus_log_parse_error(r);
3932
3933 i->ignore = ignore;
3934 i->start_timestamp = (usec_t) start_timestamp;
3935 i->exit_timestamp = (usec_t) exit_timestamp;
3936 i->pid = (pid_t) pid;
3937 i->code = code;
3938 i->status = status;
3939
3940 r = sd_bus_message_exit_container(m);
3941 if (r < 0)
3942 return bus_log_parse_error(r);
3943
3944 return 1;
3945}
3946
3947typedef struct UnitCondition {
3948 char *name;
3949 char *param;
3950 bool_Bool trigger;
3951 bool_Bool negate;
3952 int tristate;
3953
3954 LIST_FIELDS(struct UnitCondition, conditions)struct UnitCondition *conditions_next, *conditions_prev;
3955} UnitCondition;
3956
3957static void unit_condition_free(UnitCondition *c) {
3958 if (!c)
3959 return;
3960
3961 free(c->name);
3962 free(c->param);
3963 free(c);
3964}
3965
3966DEFINE_TRIVIAL_CLEANUP_FUNC(UnitCondition*, unit_condition_free)static inline void unit_condition_freep(UnitCondition* *p) { if
(*p) unit_condition_free(*p); }
;
3967
3968typedef struct UnitStatusInfo {
3969 const char *id;
3970 const char *load_state;
3971 const char *active_state;
3972 const char *freezer_state;
3973 const char *sub_state;
3974 const char *unit_file_state;
3975 const char *unit_file_preset;
3976
3977 const char *description;
3978 const char *following;
3979
3980 char **documentation;
3981
3982 const char *fragment_path;
3983 const char *source_path;
3984 const char *control_group;
3985
3986 char **dropin_paths;
3987
3988 const char *load_error;
3989 const char *result;
3990
3991 usec_t inactive_exit_timestamp;
3992 usec_t inactive_exit_timestamp_monotonic;
3993 usec_t active_enter_timestamp;
3994 usec_t active_exit_timestamp;
3995 usec_t inactive_enter_timestamp;
3996
3997 bool_Bool need_daemon_reload;
3998 bool_Bool transient;
3999
4000 /* Service */
4001 pid_t main_pid;
4002 pid_t control_pid;
4003 const char *status_text;
4004 const char *pid_file;
4005 bool_Bool running:1;
4006 int status_errno;
4007
4008 usec_t start_timestamp;
4009 usec_t exit_timestamp;
4010
4011 int exit_code, exit_status;
4012
4013 usec_t condition_timestamp;
4014 bool_Bool condition_result;
4015 LIST_HEAD(UnitCondition, conditions)UnitCondition *conditions;
4016
4017 usec_t assert_timestamp;
4018 bool_Bool assert_result;
4019 bool_Bool failed_assert_trigger;
4020 bool_Bool failed_assert_negate;
4021 const char *failed_assert;
4022 const char *failed_assert_parameter;
4023 usec_t next_elapse_real;
4024 usec_t next_elapse_monotonic;
4025
4026 /* Socket */
4027 unsigned n_accepted;
4028 unsigned n_connections;
4029 unsigned n_refused;
4030 bool_Bool accept;
4031
4032 /* Pairs of type, path */
4033 char **listen;
4034
4035 /* Device */
4036 const char *sysfs_path;
4037
4038 /* Mount, Automount */
4039 const char *where;
4040
4041 /* Swap */
4042 const char *what;
4043
4044 /* CGroup */
4045 uint64_t memory_current;
4046 uint64_t memory_min;
4047 uint64_t memory_low;
4048 uint64_t memory_high;
4049 uint64_t memory_max;
4050 uint64_t memory_swap_max;
4051 uint64_t memory_limit;
4052 uint64_t cpu_usage_nsec;
4053 uint64_t tasks_current;
4054 uint64_t tasks_max;
4055
4056 uint64_t ip_ingress_bytes;
4057 uint64_t ip_egress_bytes;
4058
4059 uint64_t default_memory_min;
4060 uint64_t default_memory_low;
4061
4062 LIST_HEAD(ExecStatusInfo, exec)ExecStatusInfo *exec;
4063} UnitStatusInfo;
4064
4065static void unit_status_info_free(UnitStatusInfo *info) {
4066 ExecStatusInfo *p;
4067 UnitCondition *c;
4068
4069 strv_free(info->documentation);
4070 strv_free(info->dropin_paths);
4071 strv_free(info->listen);
4072
4073 while ((c = info->conditions)) {
4074 LIST_REMOVE(conditions, info->conditions, c)do { typeof(*(info->conditions)) **_head = &(info->
conditions), *_item = (c); do { if ((__builtin_expect(!!(!(_item
)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("_item"),
"../src/systemctl/systemctl.c", 4074, __PRETTY_FUNCTION__); }
while (0); if (_item->conditions_next) _item->conditions_next
->conditions_prev = _item->conditions_prev; if (_item->
conditions_prev) _item->conditions_prev->conditions_next
= _item->conditions_next; else { do { if ((__builtin_expect
(!!(!(*_head == _item)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD
, ("*_head == _item"), "../src/systemctl/systemctl.c", 4074, __PRETTY_FUNCTION__
); } while (0); *_head = _item->conditions_next; } _item->
conditions_next = _item->conditions_prev = ((void*)0); } while
(0)
;
4075 unit_condition_free(c);
4076 }
4077
4078 while ((p = info->exec)) {
4079 LIST_REMOVE(exec, info->exec, p)do { typeof(*(info->exec)) **_head = &(info->exec),
*_item = (p); do { if ((__builtin_expect(!!(!(_item)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("_item"), "../src/systemctl/systemctl.c"
, 4079, __PRETTY_FUNCTION__); } while (0); if (_item->exec_next
) _item->exec_next->exec_prev = _item->exec_prev; if
(_item->exec_prev) _item->exec_prev->exec_next = _item
->exec_next; else { do { if ((__builtin_expect(!!(!(*_head
== _item)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, (
"*_head == _item"), "../src/systemctl/systemctl.c", 4079, __PRETTY_FUNCTION__
); } while (0); *_head = _item->exec_next; } _item->exec_next
= _item->exec_prev = ((void*)0); } while (0)
;
4080 exec_status_info_free(p);
4081 }
4082}
4083
4084static void print_status_info(
4085 sd_bus *bus,
4086 UnitStatusInfo *i,
4087 bool_Bool *ellipsized) {
4088
4089 char since1[FORMAT_TIMESTAMP_RELATIVE_MAX256], since2[FORMAT_TIMESTAMP_MAX(3+1+10+1+8+1+6+1+6+1)];
4090 const char *s1, *s2, *active_on, *active_off, *on, *off, *ss, *fs;
4091 _cleanup_free___attribute__((cleanup(freep))) char *formatted_path = NULL((void*)0);
4092 ExecStatusInfo *p;
4093 usec_t timestamp;
4094 const char *path;
4095 char **t, **t2;
4096 int r;
4097
4098 assert(i)do { if ((__builtin_expect(!!(!(i)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("i"), "../src/systemctl/systemctl.c", 4098
, __PRETTY_FUNCTION__); } while (0)
;
4099
4100 /* This shows pretty information about a unit. See
4101 * print_property() for a low-level property printer */
4102
4103 if (streq_ptr(i->active_state, "failed")) {
4104 active_on = ansi_highlight_red();
4105 active_off = ansi_normal();
4106 } else if (STRPTR_IN_SET(i->active_state, "active", "reloading")({ const char* _x = (i->active_state); _x && (!!strv_find
((((char**) ((const char*[]) { "active", "reloading", ((void*
)0) }))), (_x))); })
) {
4107 active_on = ansi_highlight_green();
4108 active_off = ansi_normal();
4109 } else
4110 active_on = active_off = "";
4111
4112 printf("%s%s%s %s", active_on, special_glyph(BLACK_CIRCLE), active_off, strna(i->id));
4113
4114 if (i->description && !streq_ptr(i->id, i->description))
4115 printf(" - %s", i->description);
4116
4117 printf("\n");
4118
4119 if (i->following)
4120 printf(" Follow: unit currently follows state of %s\n", i->following);
4121
4122 if (STRPTR_IN_SET(i->load_state, "error", "not-found", "bad-setting")({ const char* _x = (i->load_state); _x && (!!strv_find
((((char**) ((const char*[]) { "error", "not-found", "bad-setting"
, ((void*)0) }))), (_x))); })
) {
4123 on = ansi_highlight_red();
4124 off = ansi_normal();
4125 } else
4126 on = off = "";
4127
4128 path = i->source_path ?: i->fragment_path;
4129 if (path && terminal_urlify_path(path, NULL((void*)0), &formatted_path) >= 0)
4130 path = formatted_path;
4131
4132 if (!isempty(i->load_error))
4133 printf(" Loaded: %s%s%s (Reason: %s)\n",
4134 on, strna(i->load_state), off, i->load_error);
4135 else if (path && !isempty(i->unit_file_state) && !isempty(i->unit_file_preset) &&
4136 !STR_IN_SET(i->unit_file_state, "generated", "transient")(!!strv_find((((char**) ((const char*[]) { "generated", "transient"
, ((void*)0) }))), (i->unit_file_state)))
)
4137 printf(" Loaded: %s%s%s (%s; %s; vendor preset: %s)\n",
4138 on, strna(i->load_state), off, path, i->unit_file_state, i->unit_file_preset);
4139 else if (path && !isempty(i->unit_file_state))
4140 printf(" Loaded: %s%s%s (%s; %s)\n",
4141 on, strna(i->load_state), off, path, i->unit_file_state);
4142 else if (path)
4143 printf(" Loaded: %s%s%s (%s)\n",
4144 on, strna(i->load_state), off, path);
4145 else
4146 printf(" Loaded: %s%s%s\n",
4147 on, strna(i->load_state), off);
4148
4149 if (i->transient)
4150 printf("Transient: yes\n");
4151
4152 if (!strv_isempty(i->dropin_paths)) {
4153 _cleanup_free___attribute__((cleanup(freep))) char *dir = NULL((void*)0);
4154 bool_Bool last = false0;
4155 char ** dropin;
4156
4157 STRV_FOREACH(dropin, i->dropin_paths)for ((dropin) = (i->dropin_paths); (dropin) && *(dropin
); (dropin)++)
{
4158 _cleanup_free___attribute__((cleanup(freep))) char *dropin_formatted = NULL((void*)0);
4159 const char *df;
4160
4161 if (!dir || last) {
4162 printf(dir ? " " :
4163 " Drop-In: ");
4164
4165 dir = mfree(dir);
4166
4167 dir = dirname_malloc(*dropin);
4168 if (!dir) {
4169 log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 4169, __func__)
;
4170 return;
4171 }
4172
4173 printf("%s\n"
4174 " %s", dir,
4175 special_glyph(TREE_RIGHT));
4176 }
4177
4178 last = ! (*(dropin + 1) && startswith(*(dropin + 1), dir));
4179
4180 if (terminal_urlify_path(*dropin, basename(*dropin), &dropin_formatted) >= 0)
4181 df = dropin_formatted;
4182 else
4183 df = *dropin;
4184
4185 printf("%s%s", df, last ? "\n" : ", ");
4186 }
4187 }
4188
4189 ss = streq_ptr(i->active_state, i->sub_state) ? NULL((void*)0) : i->sub_state;
4190 if (ss)
4191 printf(" Active: %s%s (%s)%s",
4192 active_on, strna(i->active_state), ss, active_off);
4193 else
4194 printf(" Active: %s%s%s",
4195 active_on, strna(i->active_state), active_off);
4196
4197 fs = !isempty(i->freezer_state) && !streq(i->freezer_state, "running")(strcmp((i->freezer_state),("running")) == 0) ? i->freezer_state : NULL((void*)0);
4198 if (fs)
4199 printf(" %s(%s)%s", ansi_highlight_yellow(), fs, active_off);
4200
4201 if (!isempty(i->result) && !streq(i->result, "success")(strcmp((i->result),("success")) == 0))
4202 printf(" (Result: %s)", i->result);
4203
4204 timestamp = STRPTR_IN_SET(i->active_state, "active", "reloading")({ const char* _x = (i->active_state); _x && (!!strv_find
((((char**) ((const char*[]) { "active", "reloading", ((void*
)0) }))), (_x))); })
? i->active_enter_timestamp :
4205 STRPTR_IN_SET(i->active_state, "inactive", "failed")({ const char* _x = (i->active_state); _x && (!!strv_find
((((char**) ((const char*[]) { "inactive", "failed", ((void*)
0) }))), (_x))); })
? i->inactive_enter_timestamp :
4206 STRPTR_IN_SET(i->active_state, "activating")({ const char* _x = (i->active_state); _x && (!!strv_find
((((char**) ((const char*[]) { "activating", ((void*)0) }))),
(_x))); })
? i->inactive_exit_timestamp :
4207 i->active_exit_timestamp;
4208
4209 s1 = format_timestamp_relative(since1, sizeof(since1), timestamp);
4210 s2 = format_timestamp(since2, sizeof(since2), timestamp);
4211
4212 if (s1)
4213 printf(" since %s; %s\n", s2, s1);
4214 else if (s2)
4215 printf(" since %s\n", s2);
4216 else
4217 printf("\n");
4218
4219 if (endswith(i->id, ".timer")) {
4220 char tstamp1[FORMAT_TIMESTAMP_RELATIVE_MAX256],
4221 tstamp2[FORMAT_TIMESTAMP_MAX(3+1+10+1+8+1+6+1+6+1)];
4222 const char *next_rel_time, *next_time;
4223 dual_timestamp nw, next = {i->next_elapse_real,
4224 i->next_elapse_monotonic};
4225 usec_t next_elapse;
4226
4227 printf(" Trigger: ");
4228
4229 dual_timestamp_get(&nw);
4230 next_elapse = calc_next_elapse(&nw, &next);
4231 next_rel_time = format_timestamp_relative(tstamp1, sizeof tstamp1, next_elapse);
4232 next_time = format_timestamp(tstamp2, sizeof tstamp2, next_elapse);
4233
4234 if (next_time && next_rel_time)
4235 printf("%s; %s\n", next_time, next_rel_time);
4236 else
4237 printf("n/a\n");
4238 }
4239
4240 if (!i->condition_result && i->condition_timestamp > 0) {
4241 UnitCondition *c;
4242 int n = 0;
4243
4244 s1 = format_timestamp_relative(since1, sizeof(since1), i->condition_timestamp);
4245 s2 = format_timestamp(since2, sizeof(since2), i->condition_timestamp);
4246
4247 printf("Condition: start %scondition failed%s at %s%s%s\n",
4248 ansi_highlight_yellow(), ansi_normal(),
4249 s2, s1 ? "; " : "", strempty(s1));
4250
4251 LIST_FOREACH(conditions, c, i->conditions)for ((c) = (i->conditions); (c); (c) = (c)->conditions_next
)
4252 if (c->tristate < 0)
4253 n++;
4254
4255 LIST_FOREACH(conditions, c, i->conditions)for ((c) = (i->conditions); (c); (c) = (c)->conditions_next
)
4256 if (c->tristate < 0)
4257 printf(" %s %s=%s%s%s was not met\n",
4258 --n ? special_glyph(TREE_BRANCH) : special_glyph(TREE_RIGHT),
4259 c->name,
4260 c->trigger ? "|" : "",
4261 c->negate ? "!" : "",
4262 c->param);
4263 }
4264
4265 if (!i->assert_result && i->assert_timestamp > 0) {
4266 s1 = format_timestamp_relative(since1, sizeof(since1), i->assert_timestamp);
4267 s2 = format_timestamp(since2, sizeof(since2), i->assert_timestamp);
4268
4269 printf(" Assert: start %sassertion failed%s at %s%s%s\n",
4270 ansi_highlight_red(), ansi_normal(),
4271 s2, s1 ? "; " : "", strempty(s1));
4272 if (i->failed_assert_trigger)
4273 printf(" none of the trigger assertions were met\n");
4274 else if (i->failed_assert)
4275 printf(" %s=%s%s was not met\n",
4276 i->failed_assert,
4277 i->failed_assert_negate ? "!" : "",
4278 i->failed_assert_parameter);
4279 }
4280
4281 if (i->sysfs_path)
4282 printf(" Device: %s\n", i->sysfs_path);
4283 if (i->where)
4284 printf(" Where: %s\n", i->where);
4285 if (i->what)
4286 printf(" What: %s\n", i->what);
4287
4288 STRV_FOREACH(t, i->documentation)for ((t) = (i->documentation); (t) && *(t); (t)++) {
4289 _cleanup_free___attribute__((cleanup(freep))) char *formatted = NULL((void*)0);
4290 const char *q;
4291
4292 if (terminal_urlify(*t, NULL((void*)0), &formatted) >= 0)
4293 q = formatted;
4294 else
4295 q = *t;
4296
4297 printf(" %*s %s\n", 9, t == i->documentation ? "Docs:" : "", q);
4298 }
4299
4300 STRV_FOREACH_PAIR(t, t2, i->listen)for ((t) = (i->listen), (t2) = (t+1); (t) && *(t) &&
*(t2); (t) += 2, (t2) = (t + 1))
4301 printf(" %*s %s (%s)\n", 9, t == i->listen ? "Listen:" : "", *t2, *t);
4302
4303 if (i->accept) {
4304 printf(" Accepted: %u; Connected: %u;", i->n_accepted, i->n_connections);
4305 if (i->n_refused)
4306 printf(" Refused: %u", i->n_refused);
4307 printf("\n");
4308 }
4309
4310 LIST_FOREACH(exec, p, i->exec)for ((p) = (i->exec); (p); (p) = (p)->exec_next) {
4311 _cleanup_free___attribute__((cleanup(freep))) char *argv = NULL((void*)0);
4312 bool_Bool good;
4313
4314 /* Only show exited processes here */
4315 if (p->code == 0)
4316 continue;
4317
4318 argv = strv_join(p->argv, " ");
4319 printf(" Process: "PID_FMT"%" "i"" %s=%s ", p->pid, p->name, strna(argv));
4320
4321 good = is_clean_exit(p->code, p->status, EXIT_CLEAN_DAEMON, NULL((void*)0));
4322 if (!good) {
4323 on = ansi_highlight_red();
4324 off = ansi_normal();
4325 } else
4326 on = off = "";
4327
4328 printf("%s(code=%s, ", on, sigchld_code_to_string(p->code));
4329
4330 if (p->code == CLD_EXITEDCLD_EXITED) {
4331 const char *c;
4332
4333 printf("status=%i", p->status);
4334
4335 c = exit_status_to_string(p->status, EXIT_STATUS_SYSTEMD);
4336 if (c)
4337 printf("/%s", c);
4338
4339 } else
4340 printf("signal=%s", signal_to_string(p->status));
4341
4342 printf(")%s\n", off);
4343
4344 if (i->main_pid == p->pid &&
4345 i->start_timestamp == p->start_timestamp &&
4346 i->exit_timestamp == p->start_timestamp)
4347 /* Let's not show this twice */
4348 i->main_pid = 0;
4349
4350 if (p->pid == i->control_pid)
4351 i->control_pid = 0;
4352 }
4353
4354 if (i->main_pid > 0 || i->control_pid > 0) {
4355 if (i->main_pid > 0) {
4356 printf(" Main PID: "PID_FMT"%" "i", i->main_pid);
4357
4358 if (i->running) {
4359
4360 if (arg_transport == BUS_TRANSPORT_LOCAL) {
4361 _cleanup_free___attribute__((cleanup(freep))) char *comm = NULL((void*)0);
4362
4363 (void) get_process_comm(i->main_pid, &comm);
4364 if (comm)
4365 printf(" (%s)", comm);
4366 }
4367
4368 } else if (i->exit_code > 0) {
4369 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
4370
4371 if (i->exit_code == CLD_EXITEDCLD_EXITED) {
4372 const char *c;
4373
4374 printf("status=%i", i->exit_status);
4375
4376 c = exit_status_to_string(i->exit_status, EXIT_STATUS_SYSTEMD);
4377 if (c)
4378 printf("/%s", c);
4379
4380 } else
4381 printf("signal=%s", signal_to_string(i->exit_status));
4382 printf(")");
4383 }
4384 }
4385
4386 if (i->control_pid > 0) {
4387 _cleanup_free___attribute__((cleanup(freep))) char *c = NULL((void*)0);
4388
4389 if (i->main_pid > 0)
4390 fputs("; Control PID: ", stdoutstdout);
4391 else
4392 fputs("Cntrl PID: ", stdoutstdout); /* if first in column, abbreviated so it fits alignment */
4393
4394 printf(PID_FMT"%" "i", i->control_pid);
4395
4396 if (arg_transport == BUS_TRANSPORT_LOCAL) {
4397 (void) get_process_comm(i->control_pid, &c);
4398 if (c)
4399 printf(" (%s)", c);
4400 }
4401 }
4402
4403 printf("\n");
4404 }
4405
4406 if (i->status_text)
4407 printf(" Status: \"%s\"\n", i->status_text);
4408 if (i->status_errno > 0)
4409 printf(" Error: %i (%s)\n", i->status_errno, strerror(i->status_errno));
4410
4411 if (i->ip_ingress_bytes != (uint64_t) -1 && i->ip_egress_bytes != (uint64_t) -1) {
4412 char buf_in[FORMAT_BYTES_MAX8], buf_out[FORMAT_BYTES_MAX8];
4413
4414 printf(" IP: %s in, %s out\n",
4415 format_bytes(buf_in, sizeof(buf_in), i->ip_ingress_bytes),
4416 format_bytes(buf_out, sizeof(buf_out), i->ip_egress_bytes));
4417 }
4418
4419 if (i->tasks_current != (uint64_t) -1) {
4420 printf(" Tasks: %" PRIu64"l" "u", i->tasks_current);
4421
4422 if (i->tasks_max != (uint64_t) -1)
4423 printf(" (limit: %" PRIu64"l" "u" ")\n", i->tasks_max);
4424 else
4425 printf("\n");
4426 }
4427
4428 if (i->memory_current != (uint64_t) -1) {
4429 char buf[FORMAT_BYTES_MAX8];
4430
4431 printf(" Memory: %s", format_bytes(buf, sizeof(buf), i->memory_current));
4432
4433 if (i->memory_min > 0 || i->memory_low > 0 ||
4434 i->memory_high != CGROUP_LIMIT_MAX((uint64_t) -1) || i->memory_max != CGROUP_LIMIT_MAX((uint64_t) -1) ||
4435 i->memory_swap_max != CGROUP_LIMIT_MAX((uint64_t) -1) ||
4436 i->memory_limit != CGROUP_LIMIT_MAX((uint64_t) -1)) {
4437 const char *prefix = "";
4438
4439 printf(" (");
4440 if (i->memory_min > 0) {
4441 printf("%smin: %s", prefix, format_bytes(buf, sizeof(buf), i->memory_min));
4442 prefix = " ";
4443 }
4444 if (i->memory_low > 0) {
4445 printf("%slow: %s", prefix, format_bytes(buf, sizeof(buf), i->memory_low));
4446 prefix = " ";
4447 }
4448 if (i->memory_high != CGROUP_LIMIT_MAX((uint64_t) -1)) {
4449 printf("%shigh: %s", prefix, format_bytes(buf, sizeof(buf), i->memory_high));
4450 prefix = " ";
4451 }
4452 if (i->memory_max != CGROUP_LIMIT_MAX((uint64_t) -1)) {
4453 printf("%smax: %s", prefix, format_bytes(buf, sizeof(buf), i->memory_max));
4454 prefix = " ";
4455 }
4456 if (i->memory_swap_max != CGROUP_LIMIT_MAX((uint64_t) -1)) {
4457 printf("%sswap max: %s", prefix, format_bytes(buf, sizeof(buf), i->memory_swap_max));
4458 prefix = " ";
4459 }
4460 if (i->memory_limit != CGROUP_LIMIT_MAX((uint64_t) -1)) {
4461 printf("%slimit: %s", prefix, format_bytes(buf, sizeof(buf), i->memory_limit));
4462 prefix = " ";
4463 }
4464 printf(")");
4465 }
4466 printf("\n");
4467 }
4468
4469 if (i->cpu_usage_nsec != (uint64_t) -1) {
4470 char buf[FORMAT_TIMESPAN_MAX64];
4471 printf(" CPU: %s\n", format_timespan(buf, sizeof(buf), i->cpu_usage_nsec / NSEC_PER_USEC((nsec_t) 1000ULL), USEC_PER_MSEC((usec_t) 1000ULL)));
4472 }
4473
4474 if (i->control_group) {
4475 _cleanup_(sd_bus_error_free)__attribute__((cleanup(sd_bus_error_free))) sd_bus_error error = SD_BUS_ERROR_NULL((const sd_bus_error) {(((void*)0)), (((void*)0)), 0});
4476 static const char prefix[] = " ";
4477 unsigned c;
4478
4479 printf(" CGroup: %s\n", i->control_group);
4480
4481 c = columns();
4482 if (c > sizeof(prefix) - 1)
4483 c -= sizeof(prefix) - 1;
4484 else
4485 c = 0;
4486
4487 r = unit_show_processes(bus, i->id, i->control_group, prefix, c, get_output_flags(), &error);
4488 if (r == -EBADR53) {
4489 unsigned k = 0;
4490 pid_t extra[2];
4491
4492 /* Fallback for older systemd versions where the GetUnitProcesses() call is not yet available */
4493
4494 if (i->main_pid > 0)
4495 extra[k++] = i->main_pid;
4496
4497 if (i->control_pid > 0)
4498 extra[k++] = i->control_pid;
4499
4500 show_cgroup_and_extra(SYSTEMD_CGROUP_CONTROLLER"_systemd", i->control_group, prefix, c, extra, k, get_output_flags());
4501 } else if (r < 0)
4502 log_warning_errno(r, "Failed to dump process list for '%s', ignoring: %s", i->id, bus_error_message(&error, r))({ int _level = ((4)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 4502, __func__, "Failed to dump process list for '%s', ignoring: %s"
, i->id, bus_error_message(&error, r)) : -abs(_e); })
;
4503 }
4504
4505 if (i->id && arg_transport == BUS_TRANSPORT_LOCAL)
4506 show_journal_by_unit(
4507 stdoutstdout,
4508 i->id,
4509 arg_output,
4510 0,
4511 i->inactive_exit_timestamp_monotonic,
4512 arg_lines,
4513 getuid(),
4514 get_output_flags() | OUTPUT_BEGIN_NEWLINE,
4515 SD_JOURNAL_LOCAL_ONLY,
4516 arg_scope == UNIT_FILE_SYSTEM,
4517 ellipsized);
4518
4519 if (i->need_daemon_reload)
4520 warn_unit_file_changed(i->id);
4521}
4522
4523static void show_unit_help(UnitStatusInfo *i) {
4524 char **p;
4525
4526 assert(i)do { if ((__builtin_expect(!!(!(i)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("i"), "../src/systemctl/systemctl.c", 4526
, __PRETTY_FUNCTION__); } while (0)
;
4527
4528 if (!i->documentation) {
4529 log_info("Documentation for %s not known.", i->id)({ int _level = (((6))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 4529, __func__, "Documentation for %s not known."
, i->id) : -abs(_e); })
;
4530 return;
4531 }
4532
4533 STRV_FOREACH(p, i->documentation)for ((p) = (i->documentation); (p) && *(p); (p)++)
4534 if (startswith(*p, "man:"))
4535 show_man_page(*p + 4, false0);
4536 else
4537 log_info("Can't show: %s", *p)({ int _level = (((6))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 4537, __func__, "Can't show: %s"
, *p) : -abs(_e); })
;
4538}
4539
4540static int map_main_pid(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
4541 UnitStatusInfo *i = userdata;
4542 uint32_t u;
4543 int r;
4544
4545 r = sd_bus_message_read(m, "u", &u);
4546 if (r < 0)
4547 return r;
4548
4549 i->main_pid = (pid_t) u;
4550 i->running = u > 0;
4551
4552 return 0;
4553}
4554
4555static int map_load_error(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
4556 const char *message, **p = userdata;
4557 int r;
4558
4559 r = sd_bus_message_read(m, "(ss)", NULL((void*)0), &message);
4560 if (r < 0)
4561 return r;
4562
4563 if (!isempty(message))
4564 *p = message;
4565
4566 return 0;
4567}
4568
4569static int map_listen(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
4570 const char *type, *path;
4571 char ***p = userdata;
4572 int r;
4573
4574 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(ss)");
4575 if (r < 0)
4576 return r;
4577
4578 while ((r = sd_bus_message_read(m, "(ss)", &type, &path)) > 0) {
4579
4580 r = strv_extend(p, type);
4581 if (r < 0)
4582 return r;
4583
4584 r = strv_extend(p, path);
4585 if (r < 0)
4586 return r;
4587 }
4588 if (r < 0)
4589 return r;
4590
4591 r = sd_bus_message_exit_container(m);
4592 if (r < 0)
4593 return r;
4594
4595 return 0;
4596}
4597
4598static int map_conditions(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
4599 UnitStatusInfo *i = userdata;
4600 const char *cond, *param;
4601 int trigger, negate;
4602 int32_t state;
4603 int r;
4604
4605 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sbbsi)");
4606 if (r < 0)
4607 return r;
4608
4609 while ((r = sd_bus_message_read(m, "(sbbsi)", &cond, &trigger, &negate, &param, &state)) > 0) {
4610 _cleanup_(unit_condition_freep)__attribute__((cleanup(unit_condition_freep))) UnitCondition *c = NULL((void*)0);
4611
4612 c = new0(UnitCondition, 1)((UnitCondition*) calloc((1), sizeof(UnitCondition)));
4613 if (!c)
4614 return -ENOMEM12;
4615
4616 c->name = strdup(cond);
4617 c->param = strdup(param);
4618 if (!c->name || !c->param)
4619 return -ENOMEM12;
4620
4621 c->trigger = trigger;
4622 c->negate = negate;
4623 c->tristate = state;
4624
4625 LIST_PREPEND(conditions, i->conditions, c)do { typeof(*(i->conditions)) **_head = &(i->conditions
), *_item = (c); do { if ((__builtin_expect(!!(!(_item)),0)))
log_assert_failed_realm(LOG_REALM_SYSTEMD, ("_item"), "../src/systemctl/systemctl.c"
, 4625, __PRETTY_FUNCTION__); } while (0); if ((_item->conditions_next
= *_head)) _item->conditions_next->conditions_prev = _item
; _item->conditions_prev = ((void*)0); *_head = _item; } while
(0)
;
4626 c = NULL((void*)0);
4627 }
4628 if (r < 0)
4629 return r;
4630
4631 r = sd_bus_message_exit_container(m);
4632 if (r < 0)
4633 return r;
4634
4635 return 0;
4636}
4637
4638static int map_asserts(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
4639 UnitStatusInfo *i = userdata;
4640 const char *cond, *param;
4641 int trigger, negate;
4642 int32_t state;
4643 int r;
4644
4645 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sbbsi)");
4646 if (r < 0)
4647 return r;
4648
4649 while ((r = sd_bus_message_read(m, "(sbbsi)", &cond, &trigger, &negate, &param, &state)) > 0) {
4650 if (state < 0 && (!trigger || !i->failed_assert)) {
4651 i->failed_assert = cond;
4652 i->failed_assert_trigger = trigger;
4653 i->failed_assert_negate = negate;
4654 i->failed_assert_parameter = param;
4655 }
4656 }
4657 if (r < 0)
4658 return r;
4659
4660 r = sd_bus_message_exit_container(m);
4661 if (r < 0)
4662 return r;
4663
4664 return 0;
4665}
4666
4667static int map_exec(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
4668 _cleanup_free___attribute__((cleanup(freep))) ExecStatusInfo *info = NULL((void*)0);
4669 UnitStatusInfo *i = userdata;
4670 int r;
4671
4672 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sasbttttuii)");
4673 if (r < 0)
4674 return r;
4675
4676 info = new0(ExecStatusInfo, 1)((ExecStatusInfo*) calloc((1), sizeof(ExecStatusInfo)));
4677 if (!info)
4678 return -ENOMEM12;
4679
4680 while ((r = exec_status_info_deserialize(m, info)) > 0) {
4681
4682 info->name = strdup(member);
4683 if (!info->name)
4684 return -ENOMEM12;
4685
4686 LIST_PREPEND(exec, i->exec, info)do { typeof(*(i->exec)) **_head = &(i->exec), *_item
= (info); do { if ((__builtin_expect(!!(!(_item)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("_item"), "../src/systemctl/systemctl.c"
, 4686, __PRETTY_FUNCTION__); } while (0); if ((_item->exec_next
= *_head)) _item->exec_next->exec_prev = _item; _item->
exec_prev = ((void*)0); *_head = _item; } while (0)
;
4687
4688 info = new0(ExecStatusInfo, 1)((ExecStatusInfo*) calloc((1), sizeof(ExecStatusInfo)));
4689 if (!info)
4690 return -ENOMEM12;
4691 }
4692 if (r < 0)
4693 return r;
4694
4695 r = sd_bus_message_exit_container(m);
4696 if (r < 0)
4697 return r;
4698
4699 return 0;
4700}
4701
4702#define print_prop(name, fmt, ...)do { if (arg_value) printf(fmt "\n", ...); else printf("%s=" fmt
"\n", name, ...); } while (0)
\
4703 do { \
4704 if (arg_value) \
4705 printf(fmt "\n", __VA_ARGS__); \
4706 else \
4707 printf("%s=" fmt "\n", name, __VA_ARGS__); \
4708 } while (0)
4709
4710static int print_property(const char *name, sd_bus_message *m, bool_Bool value, bool_Bool all) {
4711 char bus_type;
4712 const char *contents;
4713 int r;
4714
4715 assert(name)do { if ((__builtin_expect(!!(!(name)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("name"), "../src/systemctl/systemctl.c",
4715, __PRETTY_FUNCTION__); } while (0)
;
4716 assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("m"), "../src/systemctl/systemctl.c", 4716
, __PRETTY_FUNCTION__); } while (0)
;
4717
4718 /* This is a low-level property printer, see
4719 * print_status_info() for the nicer output */
4720
4721 r = sd_bus_message_peek_type(m, &bus_type, &contents);
4722 if (r < 0)
4723 return r;
4724
4725 switch (bus_type) {
4726
4727 case SD_BUS_TYPE_INT32:
4728 if (streq(name, "NUMAPolicy")(strcmp((name),("NUMAPolicy")) == 0)) {
4729 int32_t i;
4730
4731 r = sd_bus_message_read_basic(m, bus_type, &i);
4732 if (r < 0)
4733 return r;
4734
4735 print_prop(name, "%s", strna(mpol_to_string(i)))do { if (arg_value) printf("%s" "\n", strna(mpol_to_string(i)
)); else printf("%s=" "%s" "\n", name, strna(mpol_to_string(i
))); } while (0)
;
4736
4737 return 1;
4738 }
4739 break;
4740
4741 case SD_BUS_TYPE_STRUCT:
4742
4743 if (contents[0] == SD_BUS_TYPE_UINT32 && streq(name, "Job")(strcmp((name),("Job")) == 0)) {
4744 uint32_t u;
4745
4746 r = sd_bus_message_read(m, "(uo)", &u, NULL((void*)0));
4747 if (r < 0)
4748 return bus_log_parse_error(r);
4749
4750 if (u > 0)
4751 print_prop(name, "%"PRIu32, u)do { if (arg_value) printf("%""u" "\n", u); else printf("%s="
"%""u" "\n", name, u); } while (0)
;
4752 else if (all)
4753 print_prop(name, "%s", "")do { if (arg_value) printf("%s" "\n", ""); else printf("%s=" "%s"
"\n", name, ""); } while (0)
;
4754
4755 return 1;
4756
4757 } else if (contents[0] == SD_BUS_TYPE_STRING && streq(name, "Unit")(strcmp((name),("Unit")) == 0)) {
4758 const char *s;
4759
4760 r = sd_bus_message_read(m, "(so)", &s, NULL((void*)0));
4761 if (r < 0)
4762 return bus_log_parse_error(r);
4763
4764 if (all || !isempty(s))
4765 print_prop(name, "%s", s)do { if (arg_value) printf("%s" "\n", s); else printf("%s=" "%s"
"\n", name, s); } while (0)
;
4766
4767 return 1;
4768
4769 } else if (contents[0] == SD_BUS_TYPE_STRING && streq(name, "LoadError")(strcmp((name),("LoadError")) == 0)) {
4770 const char *a = NULL((void*)0), *b = NULL((void*)0);
4771
4772 r = sd_bus_message_read(m, "(ss)", &a, &b);
4773 if (r < 0)
4774 return bus_log_parse_error(r);
4775
4776 if (all || !isempty(a) || !isempty(b))
4777 print_prop(name, "%s \"%s\"", strempty(a), strempty(b))do { if (arg_value) printf("%s \"%s\"" "\n", strempty(a), strempty
(b)); else printf("%s=" "%s \"%s\"" "\n", name, strempty(a), strempty
(b)); } while (0)
;
4778
4779 return 1;
4780 } else if (streq_ptr(name, "SystemCallFilter")) {
4781 _cleanup_strv_free___attribute__((cleanup(strv_freep))) char **l = NULL((void*)0);
4782 int whitelist;
4783
4784 r = sd_bus_message_enter_container(m, 'r', "bas");
4785 if (r < 0)
4786 return bus_log_parse_error(r);
4787
4788 r = sd_bus_message_read(m, "b", &whitelist);
4789 if (r < 0)
4790 return bus_log_parse_error(r);
4791
4792 r = sd_bus_message_read_strv(m, &l);
4793 if (r < 0)
4794 return bus_log_parse_error(r);
4795
4796 r = sd_bus_message_exit_container(m);
4797 if (r < 0)
4798 return bus_log_parse_error(r);
4799
4800 if (all || whitelist || !strv_isempty(l)) {
4801 bool_Bool first = true1;
4802 char **i;
4803
4804 if (!value) {
4805 fputs(name, stdoutstdout);
4806 fputc('=', stdoutstdout);
4807 }
4808
4809 if (!whitelist)
4810 fputc('~', stdoutstdout);
4811
4812 STRV_FOREACH(i, l)for ((i) = (l); (i) && *(i); (i)++) {
4813 if (first)
4814 first = false0;
4815 else
4816 fputc(' ', stdoutstdout);
4817
4818 fputs(*i, stdoutstdout);
4819 }
4820 fputc('\n', stdoutstdout);
4821 }
4822
4823 return 1;
4824 }
4825
4826 break;
4827
4828 case SD_BUS_TYPE_ARRAY:
4829
4830 if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "EnvironmentFiles")(strcmp((name),("EnvironmentFiles")) == 0)) {
4831 const char *path;
4832 int ignore;
4833
4834 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sb)");
4835 if (r < 0)
4836 return bus_log_parse_error(r);
4837
4838 while ((r = sd_bus_message_read(m, "(sb)", &path, &ignore)) > 0)
4839 print_prop(name, "%s (ignore_errors=%s)", path, yes_no(ignore))do { if (arg_value) printf("%s (ignore_errors=%s)" "\n", path
, yes_no(ignore)); else printf("%s=" "%s (ignore_errors=%s)" "\n"
, name, path, yes_no(ignore)); } while (0)
;
4840
4841 if (r < 0)
4842 return bus_log_parse_error(r);
4843
4844 r = sd_bus_message_exit_container(m);
4845 if (r < 0)
4846 return bus_log_parse_error(r);
4847
4848 return 1;
4849
4850 } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "Paths")(strcmp((name),("Paths")) == 0)) {
4851 const char *type, *path;
4852
4853 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(ss)");
4854 if (r < 0)
4855 return bus_log_parse_error(r);
4856
4857 while ((r = sd_bus_message_read(m, "(ss)", &type, &path)) > 0)
4858 print_prop(name, "%s (%s)", path, type)do { if (arg_value) printf("%s (%s)" "\n", path, type); else printf
("%s=" "%s (%s)" "\n", name, path, type); } while (0)
;
4859 if (r < 0)
4860 return bus_log_parse_error(r);
4861
4862 r = sd_bus_message_exit_container(m);
4863 if (r < 0)
4864 return bus_log_parse_error(r);
4865
4866 return 1;
4867
4868 } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "Listen")(strcmp((name),("Listen")) == 0)) {
4869 const char *type, *path;
4870
4871 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(ss)");
4872 if (r < 0)
4873 return bus_log_parse_error(r);
4874
4875 while ((r = sd_bus_message_read(m, "(ss)", &type, &path)) > 0)
4876 print_prop(name, "%s (%s)", path, type)do { if (arg_value) printf("%s (%s)" "\n", path, type); else printf
("%s=" "%s (%s)" "\n", name, path, type); } while (0)
;
4877 if (r < 0)
4878 return bus_log_parse_error(r);
4879
4880 r = sd_bus_message_exit_container(m);
4881 if (r < 0)
4882 return bus_log_parse_error(r);
4883
4884 return 1;
4885
4886 } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "TimersMonotonic")(strcmp((name),("TimersMonotonic")) == 0)) {
4887 const char *base;
4888 uint64_t v, next_elapse;
4889
4890 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(stt)");
4891 if (r < 0)
4892 return bus_log_parse_error(r);
4893
4894 while ((r = sd_bus_message_read(m, "(stt)", &base, &v, &next_elapse)) > 0) {
4895 char timespan1[FORMAT_TIMESPAN_MAX64], timespan2[FORMAT_TIMESPAN_MAX64];
4896
4897 print_prop(name, "{ %s=%s ; next_elapse=%s }", base,do { if (arg_value) printf("{ %s=%s ; next_elapse=%s }" "\n",
base, format_timespan(timespan1, sizeof(timespan1), v, 0), format_timespan
(timespan2, sizeof(timespan2), next_elapse, 0)); else printf(
"%s=" "{ %s=%s ; next_elapse=%s }" "\n", name, base, format_timespan
(timespan1, sizeof(timespan1), v, 0), format_timespan(timespan2
, sizeof(timespan2), next_elapse, 0)); } while (0)
4898 format_timespan(timespan1, sizeof(timespan1), v, 0),do { if (arg_value) printf("{ %s=%s ; next_elapse=%s }" "\n",
base, format_timespan(timespan1, sizeof(timespan1), v, 0), format_timespan
(timespan2, sizeof(timespan2), next_elapse, 0)); else printf(
"%s=" "{ %s=%s ; next_elapse=%s }" "\n", name, base, format_timespan
(timespan1, sizeof(timespan1), v, 0), format_timespan(timespan2
, sizeof(timespan2), next_elapse, 0)); } while (0)
4899 format_timespan(timespan2, sizeof(timespan2), next_elapse, 0))do { if (arg_value) printf("{ %s=%s ; next_elapse=%s }" "\n",
base, format_timespan(timespan1, sizeof(timespan1), v, 0), format_timespan
(timespan2, sizeof(timespan2), next_elapse, 0)); else printf(
"%s=" "{ %s=%s ; next_elapse=%s }" "\n", name, base, format_timespan
(timespan1, sizeof(timespan1), v, 0), format_timespan(timespan2
, sizeof(timespan2), next_elapse, 0)); } while (0)
;
4900 }
4901 if (r < 0)
4902 return bus_log_parse_error(r);
4903
4904 r = sd_bus_message_exit_container(m);
4905 if (r < 0)
4906 return bus_log_parse_error(r);
4907
4908 return 1;
4909
4910 } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "TimersCalendar")(strcmp((name),("TimersCalendar")) == 0)) {
4911 const char *base, *spec;
4912 uint64_t next_elapse;
4913
4914 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sst)");
4915 if (r < 0)
4916 return bus_log_parse_error(r);
4917
4918 while ((r = sd_bus_message_read(m, "(sst)", &base, &spec, &next_elapse)) > 0) {
4919 char timestamp[FORMAT_TIMESTAMP_MAX(3+1+10+1+8+1+6+1+6+1)];
4920
4921 print_prop(name, "{ %s=%s ; next_elapse=%s }", base, spec,do { if (arg_value) printf("{ %s=%s ; next_elapse=%s }" "\n",
base, spec, format_timestamp(timestamp, sizeof(timestamp), next_elapse
)); else printf("%s=" "{ %s=%s ; next_elapse=%s }" "\n", name
, base, spec, format_timestamp(timestamp, sizeof(timestamp), next_elapse
)); } while (0)
4922 format_timestamp(timestamp, sizeof(timestamp), next_elapse))do { if (arg_value) printf("{ %s=%s ; next_elapse=%s }" "\n",
base, spec, format_timestamp(timestamp, sizeof(timestamp), next_elapse
)); else printf("%s=" "{ %s=%s ; next_elapse=%s }" "\n", name
, base, spec, format_timestamp(timestamp, sizeof(timestamp), next_elapse
)); } while (0)
;
4923 }
4924 if (r < 0)
4925 return bus_log_parse_error(r);
4926
4927 r = sd_bus_message_exit_container(m);
4928 if (r < 0)
4929 return bus_log_parse_error(r);
4930
4931 return 1;
4932
4933 } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && startswith(name, "Exec")) {
4934 ExecStatusInfo info = {};
4935
4936 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sasbttttuii)");
4937 if (r < 0)
4938 return bus_log_parse_error(r);
4939
4940 while ((r = exec_status_info_deserialize(m, &info)) > 0) {
4941 char timestamp1[FORMAT_TIMESTAMP_MAX(3+1+10+1+8+1+6+1+6+1)], timestamp2[FORMAT_TIMESTAMP_MAX(3+1+10+1+8+1+6+1+6+1)];
4942 _cleanup_free___attribute__((cleanup(freep))) char *tt;
4943
4944 tt = strv_join(info.argv, " ");
4945
4946 print_prop(name,do { if (arg_value) printf("{ path=%s ; argv[]=%s ; ignore_errors=%s ; start_time=[%s] ; stop_time=[%s] ; pid="
"%" "i"" ; code=%s ; status=%i%s%s }" "\n", strna(info.path),
strna(tt), yes_no(info.ignore), strna(format_timestamp(timestamp1
, sizeof(timestamp1), info.start_timestamp)), strna(format_timestamp
(timestamp2, sizeof(timestamp2), info.exit_timestamp)), info.
pid, sigchld_code_to_string(info.code), info.status, info.code
== CLD_EXITED ? "" : "/", strempty(info.code == CLD_EXITED ?
((void*)0) : signal_to_string(info.status))); else printf("%s="
"{ path=%s ; argv[]=%s ; ignore_errors=%s ; start_time=[%s] ; stop_time=[%s] ; pid="
"%" "i"" ; code=%s ; status=%i%s%s }" "\n", name, strna(info.
path), strna(tt), yes_no(info.ignore), strna(format_timestamp
(timestamp1, sizeof(timestamp1), info.start_timestamp)), strna
(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp
)), info.pid, sigchld_code_to_string(info.code), info.status,
info.code == CLD_EXITED ? "" : "/", strempty(info.code == CLD_EXITED
? ((void*)0) : signal_to_string(info.status))); } while (0)
4947 "{ path=%s ; argv[]=%s ; ignore_errors=%s ; start_time=[%s] ; stop_time=[%s] ; pid="PID_FMT" ; code=%s ; status=%i%s%s }",do { if (arg_value) printf("{ path=%s ; argv[]=%s ; ignore_errors=%s ; start_time=[%s] ; stop_time=[%s] ; pid="
"%" "i"" ; code=%s ; status=%i%s%s }" "\n", strna(info.path),
strna(tt), yes_no(info.ignore), strna(format_timestamp(timestamp1
, sizeof(timestamp1), info.start_timestamp)), strna(format_timestamp
(timestamp2, sizeof(timestamp2), info.exit_timestamp)), info.
pid, sigchld_code_to_string(info.code), info.status, info.code
== CLD_EXITED ? "" : "/", strempty(info.code == CLD_EXITED ?
((void*)0) : signal_to_string(info.status))); else printf("%s="
"{ path=%s ; argv[]=%s ; ignore_errors=%s ; start_time=[%s] ; stop_time=[%s] ; pid="
"%" "i"" ; code=%s ; status=%i%s%s }" "\n", name, strna(info.
path), strna(tt), yes_no(info.ignore), strna(format_timestamp
(timestamp1, sizeof(timestamp1), info.start_timestamp)), strna
(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp
)), info.pid, sigchld_code_to_string(info.code), info.status,
info.code == CLD_EXITED ? "" : "/", strempty(info.code == CLD_EXITED
? ((void*)0) : signal_to_string(info.status))); } while (0)
4948 strna(info.path),do { if (arg_value) printf("{ path=%s ; argv[]=%s ; ignore_errors=%s ; start_time=[%s] ; stop_time=[%s] ; pid="
"%" "i"" ; code=%s ; status=%i%s%s }" "\n", strna(info.path),
strna(tt), yes_no(info.ignore), strna(format_timestamp(timestamp1
, sizeof(timestamp1), info.start_timestamp)), strna(format_timestamp
(timestamp2, sizeof(timestamp2), info.exit_timestamp)), info.
pid, sigchld_code_to_string(info.code), info.status, info.code
== CLD_EXITED ? "" : "/", strempty(info.code == CLD_EXITED ?
((void*)0) : signal_to_string(info.status))); else printf("%s="
"{ path=%s ; argv[]=%s ; ignore_errors=%s ; start_time=[%s] ; stop_time=[%s] ; pid="
"%" "i"" ; code=%s ; status=%i%s%s }" "\n", name, strna(info.
path), strna(tt), yes_no(info.ignore), strna(format_timestamp
(timestamp1, sizeof(timestamp1), info.start_timestamp)), strna
(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp
)), info.pid, sigchld_code_to_string(info.code), info.status,
info.code == CLD_EXITED ? "" : "/", strempty(info.code == CLD_EXITED
? ((void*)0) : signal_to_string(info.status))); } while (0)
4949 strna(tt),do { if (arg_value) printf("{ path=%s ; argv[]=%s ; ignore_errors=%s ; start_time=[%s] ; stop_time=[%s] ; pid="
"%" "i"" ; code=%s ; status=%i%s%s }" "\n", strna(info.path),
strna(tt), yes_no(info.ignore), strna(format_timestamp(timestamp1
, sizeof(timestamp1), info.start_timestamp)), strna(format_timestamp
(timestamp2, sizeof(timestamp2), info.exit_timestamp)), info.
pid, sigchld_code_to_string(info.code), info.status, info.code
== CLD_EXITED ? "" : "/", strempty(info.code == CLD_EXITED ?
((void*)0) : signal_to_string(info.status))); else printf("%s="
"{ path=%s ; argv[]=%s ; ignore_errors=%s ; start_time=[%s] ; stop_time=[%s] ; pid="
"%" "i"" ; code=%s ; status=%i%s%s }" "\n", name, strna(info.
path), strna(tt), yes_no(info.ignore), strna(format_timestamp
(timestamp1, sizeof(timestamp1), info.start_timestamp)), strna
(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp
)), info.pid, sigchld_code_to_string(info.code), info.status,
info.code == CLD_EXITED ? "" : "/", strempty(info.code == CLD_EXITED
? ((void*)0) : signal_to_string(info.status))); } while (0)
4950 yes_no(info.ignore),do { if (arg_value) printf("{ path=%s ; argv[]=%s ; ignore_errors=%s ; start_time=[%s] ; stop_time=[%s] ; pid="
"%" "i"" ; code=%s ; status=%i%s%s }" "\n", strna(info.path),
strna(tt), yes_no(info.ignore), strna(format_timestamp(timestamp1
, sizeof(timestamp1), info.start_timestamp)), strna(format_timestamp
(timestamp2, sizeof(timestamp2), info.exit_timestamp)), info.
pid, sigchld_code_to_string(info.code), info.status, info.code
== CLD_EXITED ? "" : "/", strempty(info.code == CLD_EXITED ?
((void*)0) : signal_to_string(info.status))); else printf("%s="
"{ path=%s ; argv[]=%s ; ignore_errors=%s ; start_time=[%s] ; stop_time=[%s] ; pid="
"%" "i"" ; code=%s ; status=%i%s%s }" "\n", name, strna(info.
path), strna(tt), yes_no(info.ignore), strna(format_timestamp
(timestamp1, sizeof(timestamp1), info.start_timestamp)), strna
(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp
)), info.pid, sigchld_code_to_string(info.code), info.status,
info.code == CLD_EXITED ? "" : "/", strempty(info.code == CLD_EXITED
? ((void*)0) : signal_to_string(info.status))); } while (0)
4951 strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)),do { if (arg_value) printf("{ path=%s ; argv[]=%s ; ignore_errors=%s ; start_time=[%s] ; stop_time=[%s] ; pid="
"%" "i"" ; code=%s ; status=%i%s%s }" "\n", strna(info.path),
strna(tt), yes_no(info.ignore), strna(format_timestamp(timestamp1
, sizeof(timestamp1), info.start_timestamp)), strna(format_timestamp
(timestamp2, sizeof(timestamp2), info.exit_timestamp)), info.
pid, sigchld_code_to_string(info.code), info.status, info.code
== CLD_EXITED ? "" : "/", strempty(info.code == CLD_EXITED ?
((void*)0) : signal_to_string(info.status))); else printf("%s="
"{ path=%s ; argv[]=%s ; ignore_errors=%s ; start_time=[%s] ; stop_time=[%s] ; pid="
"%" "i"" ; code=%s ; status=%i%s%s }" "\n", name, strna(info.
path), strna(tt), yes_no(info.ignore), strna(format_timestamp
(timestamp1, sizeof(timestamp1), info.start_timestamp)), strna
(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp
)), info.pid, sigchld_code_to_string(info.code), info.status,
info.code == CLD_EXITED ? "" : "/", strempty(info.code == CLD_EXITED
? ((void*)0) : signal_to_string(info.status))); } while (0)
4952 strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)),do { if (arg_value) printf("{ path=%s ; argv[]=%s ; ignore_errors=%s ; start_time=[%s] ; stop_time=[%s] ; pid="
"%" "i"" ; code=%s ; status=%i%s%s }" "\n", strna(info.path),
strna(tt), yes_no(info.ignore), strna(format_timestamp(timestamp1
, sizeof(timestamp1), info.start_timestamp)), strna(format_timestamp
(timestamp2, sizeof(timestamp2), info.exit_timestamp)), info.
pid, sigchld_code_to_string(info.code), info.status, info.code
== CLD_EXITED ? "" : "/", strempty(info.code == CLD_EXITED ?
((void*)0) : signal_to_string(info.status))); else printf("%s="
"{ path=%s ; argv[]=%s ; ignore_errors=%s ; start_time=[%s] ; stop_time=[%s] ; pid="
"%" "i"" ; code=%s ; status=%i%s%s }" "\n", name, strna(info.
path), strna(tt), yes_no(info.ignore), strna(format_timestamp
(timestamp1, sizeof(timestamp1), info.start_timestamp)), strna
(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp
)), info.pid, sigchld_code_to_string(info.code), info.status,
info.code == CLD_EXITED ? "" : "/", strempty(info.code == CLD_EXITED
? ((void*)0) : signal_to_string(info.status))); } while (0)
4953 info.pid,do { if (arg_value) printf("{ path=%s ; argv[]=%s ; ignore_errors=%s ; start_time=[%s] ; stop_time=[%s] ; pid="
"%" "i"" ; code=%s ; status=%i%s%s }" "\n", strna(info.path),
strna(tt), yes_no(info.ignore), strna(format_timestamp(timestamp1
, sizeof(timestamp1), info.start_timestamp)), strna(format_timestamp
(timestamp2, sizeof(timestamp2), info.exit_timestamp)), info.
pid, sigchld_code_to_string(info.code), info.status, info.code
== CLD_EXITED ? "" : "/", strempty(info.code == CLD_EXITED ?
((void*)0) : signal_to_string(info.status))); else printf("%s="
"{ path=%s ; argv[]=%s ; ignore_errors=%s ; start_time=[%s] ; stop_time=[%s] ; pid="
"%" "i"" ; code=%s ; status=%i%s%s }" "\n", name, strna(info.
path), strna(tt), yes_no(info.ignore), strna(format_timestamp
(timestamp1, sizeof(timestamp1), info.start_timestamp)), strna
(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp
)), info.pid, sigchld_code_to_string(info.code), info.status,
info.code == CLD_EXITED ? "" : "/", strempty(info.code == CLD_EXITED
? ((void*)0) : signal_to_string(info.status))); } while (0)
4954 sigchld_code_to_string(info.code),do { if (arg_value) printf("{ path=%s ; argv[]=%s ; ignore_errors=%s ; start_time=[%s] ; stop_time=[%s] ; pid="
"%" "i"" ; code=%s ; status=%i%s%s }" "\n", strna(info.path),
strna(tt), yes_no(info.ignore), strna(format_timestamp(timestamp1
, sizeof(timestamp1), info.start_timestamp)), strna(format_timestamp
(timestamp2, sizeof(timestamp2), info.exit_timestamp)), info.
pid, sigchld_code_to_string(info.code), info.status, info.code
== CLD_EXITED ? "" : "/", strempty(info.code == CLD_EXITED ?
((void*)0) : signal_to_string(info.status))); else printf("%s="
"{ path=%s ; argv[]=%s ; ignore_errors=%s ; start_time=[%s] ; stop_time=[%s] ; pid="
"%" "i"" ; code=%s ; status=%i%s%s }" "\n", name, strna(info.
path), strna(tt), yes_no(info.ignore), strna(format_timestamp
(timestamp1, sizeof(timestamp1), info.start_timestamp)), strna
(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp
)), info.pid, sigchld_code_to_string(info.code), info.status,
info.code == CLD_EXITED ? "" : "/", strempty(info.code == CLD_EXITED
? ((void*)0) : signal_to_string(info.status))); } while (0)
4955 info.status,do { if (arg_value) printf("{ path=%s ; argv[]=%s ; ignore_errors=%s ; start_time=[%s] ; stop_time=[%s] ; pid="
"%" "i"" ; code=%s ; status=%i%s%s }" "\n", strna(info.path),
strna(tt), yes_no(info.ignore), strna(format_timestamp(timestamp1
, sizeof(timestamp1), info.start_timestamp)), strna(format_timestamp
(timestamp2, sizeof(timestamp2), info.exit_timestamp)), info.
pid, sigchld_code_to_string(info.code), info.status, info.code
== CLD_EXITED ? "" : "/", strempty(info.code == CLD_EXITED ?
((void*)0) : signal_to_string(info.status))); else printf("%s="
"{ path=%s ; argv[]=%s ; ignore_errors=%s ; start_time=[%s] ; stop_time=[%s] ; pid="
"%" "i"" ; code=%s ; status=%i%s%s }" "\n", name, strna(info.
path), strna(tt), yes_no(info.ignore), strna(format_timestamp
(timestamp1, sizeof(timestamp1), info.start_timestamp)), strna
(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp
)), info.pid, sigchld_code_to_string(info.code), info.status,
info.code == CLD_EXITED ? "" : "/", strempty(info.code == CLD_EXITED
? ((void*)0) : signal_to_string(info.status))); } while (0)
4956 info.code == CLD_EXITED ? "" : "/",do { if (arg_value) printf("{ path=%s ; argv[]=%s ; ignore_errors=%s ; start_time=[%s] ; stop_time=[%s] ; pid="
"%" "i"" ; code=%s ; status=%i%s%s }" "\n", strna(info.path),
strna(tt), yes_no(info.ignore), strna(format_timestamp(timestamp1
, sizeof(timestamp1), info.start_timestamp)), strna(format_timestamp
(timestamp2, sizeof(timestamp2), info.exit_timestamp)), info.
pid, sigchld_code_to_string(info.code), info.status, info.code
== CLD_EXITED ? "" : "/", strempty(info.code == CLD_EXITED ?
((void*)0) : signal_to_string(info.status))); else printf("%s="
"{ path=%s ; argv[]=%s ; ignore_errors=%s ; start_time=[%s] ; stop_time=[%s] ; pid="
"%" "i"" ; code=%s ; status=%i%s%s }" "\n", name, strna(info.
path), strna(tt), yes_no(info.ignore), strna(format_timestamp
(timestamp1, sizeof(timestamp1), info.start_timestamp)), strna
(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp
)), info.pid, sigchld_code_to_string(info.code), info.status,
info.code == CLD_EXITED ? "" : "/", strempty(info.code == CLD_EXITED
? ((void*)0) : signal_to_string(info.status))); } while (0)
4957 strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status)))do { if (arg_value) printf("{ path=%s ; argv[]=%s ; ignore_errors=%s ; start_time=[%s] ; stop_time=[%s] ; pid="
"%" "i"" ; code=%s ; status=%i%s%s }" "\n", strna(info.path),
strna(tt), yes_no(info.ignore), strna(format_timestamp(timestamp1
, sizeof(timestamp1), info.start_timestamp)), strna(format_timestamp
(timestamp2, sizeof(timestamp2), info.exit_timestamp)), info.
pid, sigchld_code_to_string(info.code), info.status, info.code
== CLD_EXITED ? "" : "/", strempty(info.code == CLD_EXITED ?
((void*)0) : signal_to_string(info.status))); else printf("%s="
"{ path=%s ; argv[]=%s ; ignore_errors=%s ; start_time=[%s] ; stop_time=[%s] ; pid="
"%" "i"" ; code=%s ; status=%i%s%s }" "\n", name, strna(info.
path), strna(tt), yes_no(info.ignore), strna(format_timestamp
(timestamp1, sizeof(timestamp1), info.start_timestamp)), strna
(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp
)), info.pid, sigchld_code_to_string(info.code), info.status,
info.code == CLD_EXITED ? "" : "/", strempty(info.code == CLD_EXITED
? ((void*)0) : signal_to_string(info.status))); } while (0)
;
4958
4959 free(info.path);
4960 strv_free(info.argv);
4961 zero(info)(({ size_t _l_ = (sizeof(info)); void *_x_ = (&(info)); _l_
== 0 ? _x_ : memset(_x_, 0, _l_); }))
;
4962 }
4963
4964 r = sd_bus_message_exit_container(m);
4965 if (r < 0)
4966 return bus_log_parse_error(r);
4967
4968 return 1;
4969
4970 } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "DeviceAllow")(strcmp((name),("DeviceAllow")) == 0)) {
4971 const char *path, *rwm;
4972
4973 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(ss)");
4974 if (r < 0)
4975 return bus_log_parse_error(r);
4976
4977 while ((r = sd_bus_message_read(m, "(ss)", &path, &rwm)) > 0)
4978 print_prop(name, "%s %s", strna(path), strna(rwm))do { if (arg_value) printf("%s %s" "\n", strna(path), strna(rwm
)); else printf("%s=" "%s %s" "\n", name, strna(path), strna(
rwm)); } while (0)
;
4979 if (r < 0)
4980 return bus_log_parse_error(r);
4981
4982 r = sd_bus_message_exit_container(m);
4983 if (r < 0)
4984 return bus_log_parse_error(r);
4985
4986 return 1;
4987
4988 } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN &&
4989 STR_IN_SET(name, "IODeviceWeight", "BlockIODeviceWeight")(!!strv_find((((char**) ((const char*[]) { "IODeviceWeight", "BlockIODeviceWeight"
, ((void*)0) }))), (name)))
) {
4990 const char *path;
4991 uint64_t weight;
4992
4993 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(st)");
4994 if (r < 0)
4995 return bus_log_parse_error(r);
4996
4997 while ((r = sd_bus_message_read(m, "(st)", &path, &weight)) > 0)
4998 print_prop(name, "%s %"PRIu64, strna(path), weight)do { if (arg_value) printf("%s %""l" "u" "\n", strna(path), weight
); else printf("%s=" "%s %""l" "u" "\n", name, strna(path), weight
); } while (0)
;
4999 if (r < 0)
5000 return bus_log_parse_error(r);
5001
5002 r = sd_bus_message_exit_container(m);
5003 if (r < 0)
5004 return bus_log_parse_error(r);
5005
5006 return 1;
5007
5008 } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN &&
5009 (cgroup_io_limit_type_from_string(name) >= 0 ||
5010 STR_IN_SET(name, "BlockIOReadBandwidth", "BlockIOWriteBandwidth")(!!strv_find((((char**) ((const char*[]) { "BlockIOReadBandwidth"
, "BlockIOWriteBandwidth", ((void*)0) }))), (name)))
)) {
5011 const char *path;
5012 uint64_t bandwidth;
5013
5014 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(st)");
5015 if (r < 0)
5016 return bus_log_parse_error(r);
5017
5018 while ((r = sd_bus_message_read(m, "(st)", &path, &bandwidth)) > 0)
5019 print_prop(name, "%s %"PRIu64, strna(path), bandwidth)do { if (arg_value) printf("%s %""l" "u" "\n", strna(path), bandwidth
); else printf("%s=" "%s %""l" "u" "\n", name, strna(path), bandwidth
); } while (0)
;
5020 if (r < 0)
5021 return bus_log_parse_error(r);
5022
5023 r = sd_bus_message_exit_container(m);
5024 if (r < 0)
5025 return bus_log_parse_error(r);
5026
5027 return 1;
5028
5029 } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN &&
5030 streq(name, "IODeviceLatencyTargetUSec")(strcmp((name),("IODeviceLatencyTargetUSec")) == 0)) {
5031 char ts[FORMAT_TIMESPAN_MAX64];
5032 const char *path;
5033 uint64_t target;
5034
5035 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(st)");
5036 if (r < 0)
5037 return bus_log_parse_error(r);
5038
5039 while ((r = sd_bus_message_read(m, "(st)", &path, &target)) > 0)
5040 print_prop(name, "%s %s", strna(path),do { if (arg_value) printf("%s %s" "\n", strna(path), format_timespan
(ts, sizeof(ts), target, 1)); else printf("%s=" "%s %s" "\n",
name, strna(path), format_timespan(ts, sizeof(ts), target, 1
)); } while (0)
5041 format_timespan(ts, sizeof(ts), target, 1))do { if (arg_value) printf("%s %s" "\n", strna(path), format_timespan
(ts, sizeof(ts), target, 1)); else printf("%s=" "%s %s" "\n",
name, strna(path), format_timespan(ts, sizeof(ts), target, 1
)); } while (0)
;
5042 if (r < 0)
5043 return bus_log_parse_error(r);
5044
5045 r = sd_bus_message_exit_container(m);
5046 if (r < 0)
5047 return bus_log_parse_error(r);
5048
5049 return 1;
5050
5051 } else if (contents[0] == SD_BUS_TYPE_BYTE && streq(name, "StandardInputData")(strcmp((name),("StandardInputData")) == 0)) {
5052 _cleanup_free___attribute__((cleanup(freep))) char *h = NULL((void*)0);
5053 const void *p;
5054 size_t sz;
5055 ssize_t n;
5056
5057 r = sd_bus_message_read_array(m, 'y', &p, &sz);
5058 if (r < 0)
5059 return bus_log_parse_error(r);
5060
5061 n = base64mem(p, sz, &h);
5062 if (n < 0)
5063 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 5063, __func__)
;
5064
5065 print_prop(name, "%s", h)do { if (arg_value) printf("%s" "\n", h); else printf("%s=" "%s"
"\n", name, h); } while (0)
;
5066
5067 return 1;
5068 } else if (contents[0] == SD_BUS_TYPE_BYTE && STR_IN_SET(name, "CPUAffinity", "NUMAMask", "AllowedCPUs", "AllowedMemoryNodes", "EffectiveCPUs", "EffectiveMemoryNodes")(!!strv_find((((char**) ((const char*[]) { "CPUAffinity", "NUMAMask"
, "AllowedCPUs", "AllowedMemoryNodes", "EffectiveCPUs", "EffectiveMemoryNodes"
, ((void*)0) }))), (name)))
) {
5069 _cleanup_free___attribute__((cleanup(freep))) char *affinity = NULL((void*)0);
5070 _cleanup_(cpu_set_reset)__attribute__((cleanup(cpu_set_reset))) CPUSet set = {};
5071 const void *a;
5072 size_t n;
5073
5074 r = sd_bus_message_read_array(m, 'y', &a, &n);
5075 if (r < 0)
5076 return bus_log_parse_error(r);
5077
5078 r = cpu_set_from_dbus(a, n, &set);
5079 if (r < 0)
5080 return log_error_errno(r, "Failed to deserialize %s: %m", name)({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 5080, __func__, "Failed to deserialize %s: %m"
, name) : -abs(_e); })
;
5081
5082 affinity = cpu_set_to_range_string(&set);
5083 if (!affinity)
5084 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 5084, __func__)
;
5085
5086 print_prop(name, "%s", affinity)do { if (arg_value) printf("%s" "\n", affinity); else printf(
"%s=" "%s" "\n", name, affinity); } while (0)
;
5087
5088 return 1;
5089 }
5090
5091 break;
5092 }
5093
5094 return 0;
5095}
5096
5097typedef enum SystemctlShowMode{
5098 SYSTEMCTL_SHOW_PROPERTIES,
5099 SYSTEMCTL_SHOW_STATUS,
5100 SYSTEMCTL_SHOW_HELP,
5101 _SYSTEMCTL_SHOW_MODE_MAX,
5102 _SYSTEMCTL_SHOW_MODE_INVALID = -1,
5103} SystemctlShowMode;
5104
5105static const char* const systemctl_show_mode_table[] = {
5106 [SYSTEMCTL_SHOW_PROPERTIES] = "show",
5107 [SYSTEMCTL_SHOW_STATUS] = "status",
5108 [SYSTEMCTL_SHOW_HELP] = "help",
5109};
5110
5111DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(systemctl_show_mode, SystemctlShowMode)static SystemctlShowMode systemctl_show_mode_from_string(const
char *s) { return (SystemctlShowMode) string_table_lookup(systemctl_show_mode_table
, __extension__ (__builtin_choose_expr( !__builtin_types_compatible_p
(typeof(systemctl_show_mode_table), typeof(&*(systemctl_show_mode_table
))), sizeof(systemctl_show_mode_table)/sizeof((systemctl_show_mode_table
)[0]), ((void)0))), s); }
;
5112
5113static int show_one(
5114 sd_bus *bus,
5115 const char *path,
5116 const char *unit,
5117 SystemctlShowMode show_mode,
5118 bool_Bool *new_line,
5119 bool_Bool *ellipsized) {
5120
5121 static const struct bus_properties_map property_map[] = {
5122 { "LoadState", "s", NULL((void*)0), offsetof(UnitStatusInfo, load_state)__builtin_offsetof(UnitStatusInfo, load_state) },
5123 { "ActiveState", "s", NULL((void*)0), offsetof(UnitStatusInfo, active_state)__builtin_offsetof(UnitStatusInfo, active_state) },
5124 { "Documentation", "as", NULL((void*)0), offsetof(UnitStatusInfo, documentation)__builtin_offsetof(UnitStatusInfo, documentation) },
5125 {}
5126 }, status_map[] = {
5127 { "Id", "s", NULL((void*)0), offsetof(UnitStatusInfo, id)__builtin_offsetof(UnitStatusInfo, id) },
5128 { "LoadState", "s", NULL((void*)0), offsetof(UnitStatusInfo, load_state)__builtin_offsetof(UnitStatusInfo, load_state) },
5129 { "ActiveState", "s", NULL((void*)0), offsetof(UnitStatusInfo, active_state)__builtin_offsetof(UnitStatusInfo, active_state) },
5130 { "FreezerState", "s", NULL((void*)0), offsetof(UnitStatusInfo, freezer_state)__builtin_offsetof(UnitStatusInfo, freezer_state) },
5131 { "SubState", "s", NULL((void*)0), offsetof(UnitStatusInfo, sub_state)__builtin_offsetof(UnitStatusInfo, sub_state) },
5132 { "UnitFileState", "s", NULL((void*)0), offsetof(UnitStatusInfo, unit_file_state)__builtin_offsetof(UnitStatusInfo, unit_file_state) },
5133 { "UnitFilePreset", "s", NULL((void*)0), offsetof(UnitStatusInfo, unit_file_preset)__builtin_offsetof(UnitStatusInfo, unit_file_preset) },
5134 { "Description", "s", NULL((void*)0), offsetof(UnitStatusInfo, description)__builtin_offsetof(UnitStatusInfo, description) },
5135 { "Following", "s", NULL((void*)0), offsetof(UnitStatusInfo, following)__builtin_offsetof(UnitStatusInfo, following) },
5136 { "Documentation", "as", NULL((void*)0), offsetof(UnitStatusInfo, documentation)__builtin_offsetof(UnitStatusInfo, documentation) },
5137 { "FragmentPath", "s", NULL((void*)0), offsetof(UnitStatusInfo, fragment_path)__builtin_offsetof(UnitStatusInfo, fragment_path) },
5138 { "SourcePath", "s", NULL((void*)0), offsetof(UnitStatusInfo, source_path)__builtin_offsetof(UnitStatusInfo, source_path) },
5139 { "ControlGroup", "s", NULL((void*)0), offsetof(UnitStatusInfo, control_group)__builtin_offsetof(UnitStatusInfo, control_group) },
5140 { "DropInPaths", "as", NULL((void*)0), offsetof(UnitStatusInfo, dropin_paths)__builtin_offsetof(UnitStatusInfo, dropin_paths) },
5141 { "LoadError", "(ss)", map_load_error, offsetof(UnitStatusInfo, load_error)__builtin_offsetof(UnitStatusInfo, load_error) },
5142 { "Result", "s", NULL((void*)0), offsetof(UnitStatusInfo, result)__builtin_offsetof(UnitStatusInfo, result) },
5143 { "InactiveExitTimestamp", "t", NULL((void*)0), offsetof(UnitStatusInfo, inactive_exit_timestamp)__builtin_offsetof(UnitStatusInfo, inactive_exit_timestamp) },
5144 { "InactiveExitTimestampMonotonic", "t", NULL((void*)0), offsetof(UnitStatusInfo, inactive_exit_timestamp_monotonic)__builtin_offsetof(UnitStatusInfo, inactive_exit_timestamp_monotonic
)
},
5145 { "ActiveEnterTimestamp", "t", NULL((void*)0), offsetof(UnitStatusInfo, active_enter_timestamp)__builtin_offsetof(UnitStatusInfo, active_enter_timestamp) },
5146 { "ActiveExitTimestamp", "t", NULL((void*)0), offsetof(UnitStatusInfo, active_exit_timestamp)__builtin_offsetof(UnitStatusInfo, active_exit_timestamp) },
5147 { "InactiveEnterTimestamp", "t", NULL((void*)0), offsetof(UnitStatusInfo, inactive_enter_timestamp)__builtin_offsetof(UnitStatusInfo, inactive_enter_timestamp) },
5148 { "NeedDaemonReload", "b", NULL((void*)0), offsetof(UnitStatusInfo, need_daemon_reload)__builtin_offsetof(UnitStatusInfo, need_daemon_reload) },
5149 { "Transient", "b", NULL((void*)0), offsetof(UnitStatusInfo, transient)__builtin_offsetof(UnitStatusInfo, transient) },
5150 { "ExecMainPID", "u", NULL((void*)0), offsetof(UnitStatusInfo, main_pid)__builtin_offsetof(UnitStatusInfo, main_pid) },
5151 { "MainPID", "u", map_main_pid, 0 },
5152 { "ControlPID", "u", NULL((void*)0), offsetof(UnitStatusInfo, control_pid)__builtin_offsetof(UnitStatusInfo, control_pid) },
5153 { "StatusText", "s", NULL((void*)0), offsetof(UnitStatusInfo, status_text)__builtin_offsetof(UnitStatusInfo, status_text) },
5154 { "PIDFile", "s", NULL((void*)0), offsetof(UnitStatusInfo, pid_file)__builtin_offsetof(UnitStatusInfo, pid_file) },
5155 { "StatusErrno", "i", NULL((void*)0), offsetof(UnitStatusInfo, status_errno)__builtin_offsetof(UnitStatusInfo, status_errno) },
5156 { "ExecMainStartTimestamp", "t", NULL((void*)0), offsetof(UnitStatusInfo, start_timestamp)__builtin_offsetof(UnitStatusInfo, start_timestamp) },
5157 { "ExecMainExitTimestamp", "t", NULL((void*)0), offsetof(UnitStatusInfo, exit_timestamp)__builtin_offsetof(UnitStatusInfo, exit_timestamp) },
5158 { "ExecMainCode", "i", NULL((void*)0), offsetof(UnitStatusInfo, exit_code)__builtin_offsetof(UnitStatusInfo, exit_code) },
5159 { "ExecMainStatus", "i", NULL((void*)0), offsetof(UnitStatusInfo, exit_status)__builtin_offsetof(UnitStatusInfo, exit_status) },
5160 { "ConditionTimestamp", "t", NULL((void*)0), offsetof(UnitStatusInfo, condition_timestamp)__builtin_offsetof(UnitStatusInfo, condition_timestamp) },
5161 { "ConditionResult", "b", NULL((void*)0), offsetof(UnitStatusInfo, condition_result)__builtin_offsetof(UnitStatusInfo, condition_result) },
5162 { "Conditions", "a(sbbsi)", map_conditions, 0 },
5163 { "AssertTimestamp", "t", NULL((void*)0), offsetof(UnitStatusInfo, assert_timestamp)__builtin_offsetof(UnitStatusInfo, assert_timestamp) },
5164 { "AssertResult", "b", NULL((void*)0), offsetof(UnitStatusInfo, assert_result)__builtin_offsetof(UnitStatusInfo, assert_result) },
5165 { "Asserts", "a(sbbsi)", map_asserts, 0 },
5166 { "NextElapseUSecRealtime", "t", NULL((void*)0), offsetof(UnitStatusInfo, next_elapse_real)__builtin_offsetof(UnitStatusInfo, next_elapse_real) },
5167 { "NextElapseUSecMonotonic", "t", NULL((void*)0), offsetof(UnitStatusInfo, next_elapse_monotonic)__builtin_offsetof(UnitStatusInfo, next_elapse_monotonic) },
5168 { "NAccepted", "u", NULL((void*)0), offsetof(UnitStatusInfo, n_accepted)__builtin_offsetof(UnitStatusInfo, n_accepted) },
5169 { "NConnections", "u", NULL((void*)0), offsetof(UnitStatusInfo, n_connections)__builtin_offsetof(UnitStatusInfo, n_connections) },
5170 { "NRefused", "u", NULL((void*)0), offsetof(UnitStatusInfo, n_refused)__builtin_offsetof(UnitStatusInfo, n_refused) },
5171 { "Accept", "b", NULL((void*)0), offsetof(UnitStatusInfo, accept)__builtin_offsetof(UnitStatusInfo, accept) },
5172 { "Listen", "a(ss)", map_listen, offsetof(UnitStatusInfo, listen)__builtin_offsetof(UnitStatusInfo, listen) },
5173 { "SysFSPath", "s", NULL((void*)0), offsetof(UnitStatusInfo, sysfs_path)__builtin_offsetof(UnitStatusInfo, sysfs_path) },
5174 { "Where", "s", NULL((void*)0), offsetof(UnitStatusInfo, where)__builtin_offsetof(UnitStatusInfo, where) },
5175 { "What", "s", NULL((void*)0), offsetof(UnitStatusInfo, what)__builtin_offsetof(UnitStatusInfo, what) },
5176 { "MemoryCurrent", "t", NULL((void*)0), offsetof(UnitStatusInfo, memory_current)__builtin_offsetof(UnitStatusInfo, memory_current) },
5177 { "DefaultMemoryMin", "t", NULL((void*)0), offsetof(UnitStatusInfo, default_memory_min)__builtin_offsetof(UnitStatusInfo, default_memory_min) },
5178 { "DefaultMemoryLow", "t", NULL((void*)0), offsetof(UnitStatusInfo, default_memory_low)__builtin_offsetof(UnitStatusInfo, default_memory_low) },
5179 { "MemoryMin", "t", NULL((void*)0), offsetof(UnitStatusInfo, memory_min)__builtin_offsetof(UnitStatusInfo, memory_min) },
5180 { "MemoryLow", "t", NULL((void*)0), offsetof(UnitStatusInfo, memory_low)__builtin_offsetof(UnitStatusInfo, memory_low) },
5181 { "MemoryHigh", "t", NULL((void*)0), offsetof(UnitStatusInfo, memory_high)__builtin_offsetof(UnitStatusInfo, memory_high) },
5182 { "MemoryMax", "t", NULL((void*)0), offsetof(UnitStatusInfo, memory_max)__builtin_offsetof(UnitStatusInfo, memory_max) },
5183 { "MemorySwapMax", "t", NULL((void*)0), offsetof(UnitStatusInfo, memory_swap_max)__builtin_offsetof(UnitStatusInfo, memory_swap_max) },
5184 { "MemoryLimit", "t", NULL((void*)0), offsetof(UnitStatusInfo, memory_limit)__builtin_offsetof(UnitStatusInfo, memory_limit) },
5185 { "CPUUsageNSec", "t", NULL((void*)0), offsetof(UnitStatusInfo, cpu_usage_nsec)__builtin_offsetof(UnitStatusInfo, cpu_usage_nsec) },
5186 { "TasksCurrent", "t", NULL((void*)0), offsetof(UnitStatusInfo, tasks_current)__builtin_offsetof(UnitStatusInfo, tasks_current) },
5187 { "TasksMax", "t", NULL((void*)0), offsetof(UnitStatusInfo, tasks_max)__builtin_offsetof(UnitStatusInfo, tasks_max) },
5188 { "IPIngressBytes", "t", NULL((void*)0), offsetof(UnitStatusInfo, ip_ingress_bytes)__builtin_offsetof(UnitStatusInfo, ip_ingress_bytes) },
5189 { "IPEgressBytes", "t", NULL((void*)0), offsetof(UnitStatusInfo, ip_egress_bytes)__builtin_offsetof(UnitStatusInfo, ip_egress_bytes) },
5190 { "ExecStartPre", "a(sasbttttuii)", map_exec, 0 },
5191 { "ExecStart", "a(sasbttttuii)", map_exec, 0 },
5192 { "ExecStartPost", "a(sasbttttuii)", map_exec, 0 },
5193 { "ExecReload", "a(sasbttttuii)", map_exec, 0 },
5194 { "ExecStopPre", "a(sasbttttuii)", map_exec, 0 },
5195 { "ExecStop", "a(sasbttttuii)", map_exec, 0 },
5196 { "ExecStopPost", "a(sasbttttuii)", map_exec, 0 },
5197 {}
5198 };
5199
5200 _cleanup_(sd_bus_message_unrefp)__attribute__((cleanup(sd_bus_message_unrefp))) sd_bus_message *reply = NULL((void*)0);
5201 _cleanup_(sd_bus_error_free)__attribute__((cleanup(sd_bus_error_free))) sd_bus_error error = SD_BUS_ERROR_NULL((const sd_bus_error) {(((void*)0)), (((void*)0)), 0});
5202 _cleanup_set_free___attribute__((cleanup(set_freep))) Set *found_properties = NULL((void*)0);
5203 _cleanup_(unit_status_info_free)__attribute__((cleanup(unit_status_info_free))) UnitStatusInfo info = {
5204 .memory_current = (uint64_t) -1,
5205 .memory_high = CGROUP_LIMIT_MAX((uint64_t) -1),
5206 .memory_max = CGROUP_LIMIT_MAX((uint64_t) -1),
5207 .memory_swap_max = CGROUP_LIMIT_MAX((uint64_t) -1),
5208 .memory_limit = (uint64_t) -1,
5209 .cpu_usage_nsec = (uint64_t) -1,
5210 .tasks_current = (uint64_t) -1,
5211 .tasks_max = (uint64_t) -1,
5212 .ip_ingress_bytes = (uint64_t) -1,
5213 .ip_egress_bytes = (uint64_t) -1,
5214 };
5215 char **pp;
5216 int r;
5217
5218 assert(path)do { if ((__builtin_expect(!!(!(path)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("path"), "../src/systemctl/systemctl.c",
5218, __PRETTY_FUNCTION__); } while (0)
;
5219 assert(new_line)do { if ((__builtin_expect(!!(!(new_line)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("new_line"), "../src/systemctl/systemctl.c"
, 5219, __PRETTY_FUNCTION__); } while (0)
;
5220
5221 log_debug("Showing one %s", path)({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 5221, __func__, "Showing one %s"
, path) : -abs(_e); })
;
5222
5223 r = bus_map_all_properties(
5224 bus,
5225 "org.freedesktop.systemd1",
5226 path,
5227 show_mode == SYSTEMCTL_SHOW_STATUS ? status_map : property_map,
5228 BUS_MAP_BOOLEAN_AS_BOOL,
5229 &error,
5230 &reply,
5231 &info);
5232 if (r < 0)
5233 return log_error_errno(r, "Failed to get properties: %s", bus_error_message(&error, r))({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 5233, __func__, "Failed to get properties: %s"
, bus_error_message(&error, r)) : -abs(_e); })
;
5234
5235 if (unit && streq_ptr(info.load_state, "not-found") && streq_ptr(info.active_state, "inactive")) {
5236 log_full(show_mode == SYSTEMCTL_SHOW_STATUS ? LOG_ERR : LOG_DEBUG,({ int _level = (((show_mode == SYSTEMCTL_SHOW_STATUS ? 3 : 7
))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm
(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((
_realm) << 10 | (_level)), _e, "../src/systemctl/systemctl.c"
, 5237, __func__, "Unit %s could not be found.", unit) : -abs
(_e); })
5237 "Unit %s could not be found.", unit)({ int _level = (((show_mode == SYSTEMCTL_SHOW_STATUS ? 3 : 7
))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm
(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((
_realm) << 10 | (_level)), _e, "../src/systemctl/systemctl.c"
, 5237, __func__, "Unit %s could not be found.", unit) : -abs
(_e); })
;
5238
5239 if (show_mode == SYSTEMCTL_SHOW_STATUS)
5240 return EXIT_PROGRAM_OR_SERVICES_STATUS_UNKNOWN;
5241 else if (show_mode == SYSTEMCTL_SHOW_HELP)
5242 return -ENOENT2;
5243 }
5244
5245 if (*new_line)
5246 printf("\n");
5247
5248 *new_line = true1;
5249
5250 if (show_mode == SYSTEMCTL_SHOW_STATUS) {
5251 print_status_info(bus, &info, ellipsized);
5252
5253 if (info.active_state && !STR_IN_SET(info.active_state, "active", "reloading")(!!strv_find((((char**) ((const char*[]) { "active", "reloading"
, ((void*)0) }))), (info.active_state)))
)
5254 return EXIT_PROGRAM_NOT_RUNNING;
5255
5256 return EXIT_PROGRAM_RUNNING_OR_SERVICE_OK;
5257
5258 } else if (show_mode == SYSTEMCTL_SHOW_HELP) {
5259 show_unit_help(&info);
5260 return 0;
5261 }
5262
5263 r = sd_bus_message_rewind(reply, true1);
5264 if (r < 0)
5265 return log_error_errno(r, "Failed to rewind: %s", bus_error_message(&error, r))({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 5265, __func__, "Failed to rewind: %s"
, bus_error_message(&error, r)) : -abs(_e); })
;
5266
5267 r = bus_message_print_all_properties(reply, print_property, arg_properties, arg_value, arg_all, &found_properties);
5268 if (r < 0)
5269 return bus_log_parse_error(r);
5270
5271 STRV_FOREACH(pp, arg_properties)for ((pp) = (arg_properties); (pp) && *(pp); (pp)++)
5272 if (!set_contains(found_properties, *pp))
5273 log_debug("Property %s does not exist.", *pp)({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 5273, __func__, "Property %s does not exist."
, *pp) : -abs(_e); })
;
5274
5275 return 0;
5276}
5277
5278static int get_unit_dbus_path_by_pid(
5279 sd_bus *bus,
5280 uint32_t pid,
5281 char **unit) {
5282
5283 _cleanup_(sd_bus_error_free)__attribute__((cleanup(sd_bus_error_free))) sd_bus_error error = SD_BUS_ERROR_NULL((const sd_bus_error) {(((void*)0)), (((void*)0)), 0});
5284 _cleanup_(sd_bus_message_unrefp)__attribute__((cleanup(sd_bus_message_unrefp))) sd_bus_message *reply = NULL((void*)0);
5285 char *u;
5286 int r;
5287
5288 r = sd_bus_call_method(
5289 bus,
5290 "org.freedesktop.systemd1",
5291 "/org/freedesktop/systemd1",
5292 "org.freedesktop.systemd1.Manager",
5293 "GetUnitByPID",
5294 &error,
5295 &reply,
5296 "u", pid);
5297 if (r < 0)
5298 return log_error_errno(r, "Failed to get unit for PID %"PRIu32": %s", pid, bus_error_message(&error, r))({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 5298, __func__, "Failed to get unit for PID %"
"u"": %s", pid, bus_error_message(&error, r)) : -abs(_e);
})
;
5299
5300 r = sd_bus_message_read(reply, "o", &u);
5301 if (r < 0)
5302 return bus_log_parse_error(r);
5303
5304 u = strdup(u);
5305 if (!u)
5306 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 5306, __func__)
;
5307
5308 *unit = u;
5309 return 0;
5310}
5311
5312static int show_all(
5313 sd_bus *bus,
5314 bool_Bool *new_line,
5315 bool_Bool *ellipsized) {
5316
5317 _cleanup_(sd_bus_message_unrefp)__attribute__((cleanup(sd_bus_message_unrefp))) sd_bus_message *reply = NULL((void*)0);
5318 _cleanup_free___attribute__((cleanup(freep))) UnitInfo *unit_infos = NULL((void*)0);
5319 const UnitInfo *u;
5320 unsigned c;
5321 int r, ret = 0;
5322
5323 r = get_unit_list(bus, NULL((void*)0), NULL((void*)0), &unit_infos, 0, &reply);
5324 if (r < 0)
5325 return r;
5326
5327 (void) pager_open(arg_no_pager, false0);
5328
5329 c = (unsigned) r;
5330
5331 qsort_safe(unit_infos, c, sizeof(UnitInfo), compare_unit_info);
5332
5333 for (u = unit_infos; u < unit_infos + c; u++) {
5334 _cleanup_free___attribute__((cleanup(freep))) char *p = NULL((void*)0);
5335
5336 p = unit_dbus_path_from_name(u->id);
5337 if (!p)
5338 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 5338, __func__)
;
5339
5340 r = show_one(bus, p, u->id, SYSTEMCTL_SHOW_STATUS, new_line, ellipsized);
5341 if (r < 0)
5342 return r;
5343 else if (r > 0 && ret == 0)
5344 ret = r;
5345 }
5346
5347 return ret;
5348}
5349
5350static int show_system_status(sd_bus *bus) {
5351 char since1[FORMAT_TIMESTAMP_RELATIVE_MAX256], since2[FORMAT_TIMESTAMP_MAX(3+1+10+1+8+1+6+1+6+1)];
5352 _cleanup_(sd_bus_error_free)__attribute__((cleanup(sd_bus_error_free))) sd_bus_error error = SD_BUS_ERROR_NULL((const sd_bus_error) {(((void*)0)), (((void*)0)), 0});
5353 _cleanup_(machine_info_clear)__attribute__((cleanup(machine_info_clear))) struct machine_info mi = {};
5354 _cleanup_free___attribute__((cleanup(freep))) char *hn = NULL((void*)0);
5355 const char *on, *off;
5356 int r;
5357
5358 hn = gethostname_malloc();
5359 if (!hn)
5360 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 5360, __func__)
;
5361
5362 r = bus_map_all_properties(
5363 bus,
5364 "org.freedesktop.systemd1",
5365 "/org/freedesktop/systemd1",
5366 machine_info_property_map,
5367 BUS_MAP_STRDUP,
5368 &error,
5369 NULL((void*)0),
5370 &mi);
5371 if (r < 0)
5372 return log_error_errno(r, "Failed to read server status: %s", bus_error_message(&error, r))({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 5372, __func__, "Failed to read server status: %s"
, bus_error_message(&error, r)) : -abs(_e); })
;
5373
5374 if (streq_ptr(mi.state, "degraded")) {
5375 on = ansi_highlight_red();
5376 off = ansi_normal();
5377 } else if (streq_ptr(mi.state, "running")) {
5378 on = ansi_highlight_green();
5379 off = ansi_normal();
5380 } else {
5381 on = ansi_highlight_yellow();
5382 off = ansi_normal();
5383 }
5384
5385 printf("%s%s%s %s\n", on, special_glyph(BLACK_CIRCLE), off, arg_host ? arg_host : hn);
5386
5387 printf(" State: %s%s%s\n",
5388 on, strna(mi.state), off);
5389
5390 printf(" Jobs: %" PRIu32"u" " queued\n", mi.n_jobs);
5391 printf(" Failed: %" PRIu32"u" " units\n", mi.n_failed_units);
5392
5393 printf(" Since: %s; %s\n",
5394 format_timestamp(since2, sizeof(since2), mi.timestamp),
5395 format_timestamp_relative(since1, sizeof(since1), mi.timestamp));
5396
5397 printf(" CGroup: %s\n", mi.control_group ?: "/");
5398 if (IN_SET(arg_transport,({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){BUS_TRANSPORT_LOCAL, BUS_TRANSPORT_MACHINE
})/sizeof(int)]; switch(arg_transport) { case BUS_TRANSPORT_LOCAL
: case BUS_TRANSPORT_MACHINE: _found = 1; break; default: break
; } _found; })
5399 BUS_TRANSPORT_LOCAL,({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){BUS_TRANSPORT_LOCAL, BUS_TRANSPORT_MACHINE
})/sizeof(int)]; switch(arg_transport) { case BUS_TRANSPORT_LOCAL
: case BUS_TRANSPORT_MACHINE: _found = 1; break; default: break
; } _found; })
5400 BUS_TRANSPORT_MACHINE)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){BUS_TRANSPORT_LOCAL, BUS_TRANSPORT_MACHINE
})/sizeof(int)]; switch(arg_transport) { case BUS_TRANSPORT_LOCAL
: case BUS_TRANSPORT_MACHINE: _found = 1; break; default: break
; } _found; })
) {
5401 static const char prefix[] = " ";
5402 unsigned c;
5403
5404 c = columns();
5405 if (c > sizeof(prefix) - 1)
5406 c -= sizeof(prefix) - 1;
5407 else
5408 c = 0;
5409
5410 show_cgroup(SYSTEMD_CGROUP_CONTROLLER"_systemd", strempty(mi.control_group), prefix, c, get_output_flags());
5411 }
5412
5413 return 0;
5414}
5415
5416static int show(int argc, char *argv[], void *userdata) {
5417 bool_Bool new_line = false0, ellipsized = false0;
5418 SystemctlShowMode show_mode;
5419 int r, ret = 0;
5420 sd_bus *bus;
5421
5422 assert(argv)do { if ((__builtin_expect(!!(!(argv)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("argv"), "../src/systemctl/systemctl.c",
5422, __PRETTY_FUNCTION__); } while (0)
;
5423
5424 show_mode = systemctl_show_mode_from_string(argv[0]);
5425 if (show_mode < 0) {
5426 log_error("Invalid argument.")({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 5426, __func__, "Invalid argument."
) : -abs(_e); })
;
5427 return -EINVAL22;
5428 }
5429
5430 if (show_mode == SYSTEMCTL_SHOW_HELP && argc <= 1) {
5431 log_error("This command expects one or more unit names. Did you mean --help?")({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 5431, __func__, "This command expects one or more unit names. Did you mean --help?"
) : -abs(_e); })
;
5432 return -EINVAL22;
5433 }
5434
5435 r = acquire_bus(BUS_MANAGER, &bus);
5436 if (r < 0)
5437 return r;
5438
5439 (void) pager_open(arg_no_pager, false0);
5440
5441 if (show_mode == SYSTEMCTL_SHOW_STATUS)
5442 /* Increase max number of open files to 16K if we can, we
5443 * might needs this when browsing journal files, which might
5444 * be split up into many files. */
5445 setrlimit_closest(RLIMIT_NOFILERLIMIT_NOFILE, &RLIMIT_MAKE_CONST(16384)((struct rlimit) { 16384, 16384 }));
5446
5447 /* If no argument is specified inspect the manager itself */
5448 if (show_mode == SYSTEMCTL_SHOW_PROPERTIES && argc <= 1)
5449 return show_one(bus, "/org/freedesktop/systemd1", NULL((void*)0), show_mode, &new_line, &ellipsized);
5450
5451 if (show_mode == SYSTEMCTL_SHOW_STATUS && argc <= 1) {
5452
5453 show_system_status(bus);
5454 new_line = true1;
5455
5456 if (arg_all)
5457 ret = show_all(bus, &new_line, &ellipsized);
5458 } else {
5459 _cleanup_free___attribute__((cleanup(freep))) char **patterns = NULL((void*)0);
5460 char **name;
5461
5462 STRV_FOREACH(name, strv_skip(argv, 1))for ((name) = (strv_skip(argv, 1)); (name) && *(name)
; (name)++)
{
5463 _cleanup_free___attribute__((cleanup(freep))) char *path = NULL((void*)0), *unit = NULL((void*)0);
5464 uint32_t id;
5465
5466 if (safe_atou32(*name, &id) < 0) {
5467 if (strv_push(&patterns, *name) < 0)
5468 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 5468, __func__)
;
5469
5470 continue;
5471 } else if (show_mode == SYSTEMCTL_SHOW_PROPERTIES) {
5472 /* Interpret as job id */
5473 if (asprintf(&path, "/org/freedesktop/systemd1/job/%u", id) < 0)
5474 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 5474, __func__)
;
5475
5476 } else {
5477 /* Interpret as PID */
5478 r = get_unit_dbus_path_by_pid(bus, id, &path);
5479 if (r < 0) {
5480 ret = r;
5481 continue;
5482 }
5483
5484 r = unit_name_from_dbus_path(path, &unit);
5485 if (r < 0)
5486 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 5486, __func__)
;
5487 }
5488
5489 r = show_one(bus, path, unit, show_mode, &new_line, &ellipsized);
5490 if (r < 0)
5491 return r;
5492 else if (r > 0 && ret == 0)
5493 ret = r;
5494 }
5495
5496 if (!strv_isempty(patterns)) {
5497 _cleanup_strv_free___attribute__((cleanup(strv_freep))) char **names = NULL((void*)0);
5498
5499 r = expand_names(bus, patterns, NULL((void*)0), &names);
5500 if (r < 0)
5501 return log_error_errno(r, "Failed to expand names: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 5501, __func__, "Failed to expand names: %m"
) : -abs(_e); })
;
5502
5503 STRV_FOREACH(name, names)for ((name) = (names); (name) && *(name); (name)++) {
5504 _cleanup_free___attribute__((cleanup(freep))) char *path;
5505
5506 path = unit_dbus_path_from_name(*name);
5507 if (!path)
5508 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 5508, __func__)
;
5509
5510 r = show_one(bus, path, *name, show_mode, &new_line, &ellipsized);
5511 if (r < 0)
5512 return r;
5513 if (r > 0 && ret == 0)
5514 ret = r;
5515 }
5516 }
5517 }
5518
5519 if (ellipsized && !arg_quiet)
5520 printf("Hint: Some lines were ellipsized, use -l to show in full.\n");
5521
5522 return ret;
5523}
5524
5525static int cat(int argc, char *argv[], void *userdata) {
5526 _cleanup_(lookup_paths_free)__attribute__((cleanup(lookup_paths_free))) LookupPaths lp = {};
5527 _cleanup_strv_free___attribute__((cleanup(strv_freep))) char **names = NULL((void*)0);
5528 char **name;
5529 sd_bus *bus;
5530 bool_Bool first = true1;
5531 int r;
5532
5533 if (arg_transport != BUS_TRANSPORT_LOCAL) {
5534 log_error("Cannot remotely cat units.")({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 5534, __func__, "Cannot remotely cat units."
) : -abs(_e); })
;
5535 return -EINVAL22;
5536 }
5537
5538 r = lookup_paths_init(&lp, arg_scope, 0, arg_root);
5539 if (r < 0)
5540 return log_error_errno(r, "Failed to determine unit paths: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 5540, __func__, "Failed to determine unit paths: %m"
) : -abs(_e); })
;
5541
5542 r = acquire_bus(BUS_MANAGER, &bus);
5543 if (r < 0)
5544 return r;
5545
5546 r = expand_names(bus, strv_skip(argv, 1), NULL((void*)0), &names);
5547 if (r < 0)
5548 return log_error_errno(r, "Failed to expand names: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 5548, __func__, "Failed to expand names: %m"
) : -abs(_e); })
;
5549
5550 (void) pager_open(arg_no_pager, false0);
5551
5552 STRV_FOREACH(name, names)for ((name) = (names); (name) && *(name); (name)++) {
5553 _cleanup_free___attribute__((cleanup(freep))) char *fragment_path = NULL((void*)0);
5554 _cleanup_strv_free___attribute__((cleanup(strv_freep))) char **dropin_paths = NULL((void*)0);
5555
5556 r = unit_find_paths(bus, *name, &lp, &fragment_path, &dropin_paths);
5557 if (r < 0)
5558 return r;
5559 else if (r == 0)
5560 return -ENOENT2;
5561
5562 if (first)
5563 first = false0;
5564 else
5565 puts("");
5566
5567 if (need_daemon_reload(bus, *name) > 0) /* ignore errors (<0), this is informational output */
5568 fprintf(stderrstderr,
5569 "%s# Warning: %s changed on disk, the version systemd has loaded is outdated.\n"
5570 "%s# This output shows the current version of the unit's original fragment and drop-in files.\n"
5571 "%s# If fragments or drop-ins were added or removed, they are not properly reflected in this output.\n"
5572 "%s# Run 'systemctl%s daemon-reload' to reload units.%s\n",
5573 ansi_highlight_red(),
5574 *name,
5575 ansi_highlight_red(),
5576 ansi_highlight_red(),
5577 ansi_highlight_red(),
5578 arg_scope == UNIT_FILE_SYSTEM ? "" : " --user",
5579 ansi_normal());
5580
5581 r = cat_files(fragment_path, dropin_paths, 0);
5582 if (r < 0)
5583 return r;
5584 }
5585
5586 return 0;
5587}
5588
5589static int set_property(int argc, char *argv[], void *userdata) {
5590 _cleanup_(sd_bus_message_unrefp)__attribute__((cleanup(sd_bus_message_unrefp))) sd_bus_message *m = NULL((void*)0);
5591 _cleanup_(sd_bus_error_free)__attribute__((cleanup(sd_bus_error_free))) sd_bus_error error = SD_BUS_ERROR_NULL((const sd_bus_error) {(((void*)0)), (((void*)0)), 0});
5592 _cleanup_free___attribute__((cleanup(freep))) char *n = NULL((void*)0);
5593 UnitType t;
5594 sd_bus *bus;
5595 int r;
5596
5597 r = acquire_bus(BUS_MANAGER, &bus);
5598 if (r < 0)
5599 return r;
5600
5601 polkit_agent_open_maybe();
5602
5603 r = sd_bus_message_new_method_call(
5604 bus,
5605 &m,
5606 "org.freedesktop.systemd1",
5607 "/org/freedesktop/systemd1",
5608 "org.freedesktop.systemd1.Manager",
5609 "SetUnitProperties");
5610 if (r < 0)
5611 return bus_log_create_error(r);
5612
5613 r = unit_name_mangle(argv[1], arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN, &n);
5614 if (r < 0)
5615 return log_error_errno(r, "Failed to mangle unit name: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 5615, __func__, "Failed to mangle unit name: %m"
) : -abs(_e); })
;
5616
5617 t = unit_name_to_type(n);
5618 if (t < 0) {
5619 log_error("Invalid unit type: %s", n)({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 5619, __func__, "Invalid unit type: %s"
, n) : -abs(_e); })
;
5620 return -EINVAL22;
5621 }
5622
5623 r = sd_bus_message_append(m, "sb", n, arg_runtime);
5624 if (r < 0)
5625 return bus_log_create_error(r);
5626
5627 r = sd_bus_message_open_container(m, SD_BUS_TYPE_ARRAY, "(sv)");
5628 if (r < 0)
5629 return bus_log_create_error(r);
5630
5631 r = bus_append_unit_property_assignment_many(m, t, strv_skip(argv, 2));
5632 if (r < 0)
5633 return r;
5634
5635 r = sd_bus_message_close_container(m);
5636 if (r < 0)
5637 return bus_log_create_error(r);
5638
5639 r = sd_bus_call(bus, m, 0, &error, NULL((void*)0));
5640 if (r < 0)
5641 return log_error_errno(r, "Failed to set unit properties on %s: %s", n, bus_error_message(&error, r))({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 5641, __func__, "Failed to set unit properties on %s: %s"
, n, bus_error_message(&error, r)) : -abs(_e); })
;
5642
5643 return 0;
5644}
5645
5646static int daemon_reload(int argc, char *argv[], void *userdata) {
5647 _cleanup_(sd_bus_error_free)__attribute__((cleanup(sd_bus_error_free))) sd_bus_error error = SD_BUS_ERROR_NULL((const sd_bus_error) {(((void*)0)), (((void*)0)), 0});
5648 _cleanup_(sd_bus_message_unrefp)__attribute__((cleanup(sd_bus_message_unrefp))) sd_bus_message *m = NULL((void*)0);
5649 const char *method;
5650 sd_bus *bus;
5651 int r;
5652
5653 r = acquire_bus(BUS_MANAGER, &bus);
5654 if (r < 0)
5655 return r;
5656
5657 polkit_agent_open_maybe();
5658
5659 switch (arg_action) {
5660
5661 case ACTION_RELOAD:
5662 method = "Reload";
5663 break;
5664
5665 case ACTION_REEXEC:
5666 method = "Reexecute";
5667 break;
5668
5669 case ACTION_SYSTEMCTL:
5670 method = streq(argv[0], "daemon-reexec")(strcmp((argv[0]),("daemon-reexec")) == 0) ? "Reexecute" :
5671 /* "daemon-reload" */ "Reload";
5672 break;
5673
5674 default:
5675 assert_not_reached("Unexpected action")do { log_assert_failed_unreachable_realm(LOG_REALM_SYSTEMD, (
"Unexpected action"), "../src/systemctl/systemctl.c", 5675, __PRETTY_FUNCTION__
); } while (0)
;
5676 }
5677
5678 r = sd_bus_message_new_method_call(
5679 bus,
5680 &m,
5681 "org.freedesktop.systemd1",
5682 "/org/freedesktop/systemd1",
5683 "org.freedesktop.systemd1.Manager",
5684 method);
5685 if (r < 0)
5686 return bus_log_create_error(r);
5687
5688 /* Note we use an extra-long timeout here. This is because a reload or reexec means generators are rerun which
5689 * are timed out after DEFAULT_TIMEOUT_USEC. Let's use twice that time here, so that the generators can have
5690 * their timeout, and for everything else there's the same time budget in place. */
5691
5692 r = sd_bus_call(bus, m, DEFAULT_TIMEOUT_USEC(90*((usec_t) 1000000ULL)) * 2, &error, NULL((void*)0));
5693
5694 /* On reexecution, we expect a disconnect, not a reply */
5695 if (IN_SET(r, -ETIMEDOUT, -ECONNRESET)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){-110, -104})/sizeof(int)]; switch(r) { case
-110: case -104: _found = 1; break; default: break; } _found
; })
&& streq(method, "Reexecute")(strcmp((method),("Reexecute")) == 0))
5696 r = 0;
5697
5698 if (r < 0 && arg_action == ACTION_SYSTEMCTL)
5699 return log_error_errno(r, "Failed to reload daemon: %s", bus_error_message(&error, r))({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 5699, __func__, "Failed to reload daemon: %s"
, bus_error_message(&error, r)) : -abs(_e); })
;
5700
5701 /* Note that for the legacy commands (i.e. those with action != ACTION_SYSTEMCTL) we support fallbacks to the
5702 * old ways of doing things, hence don't log any error in that case here. */
5703
5704 return r < 0 ? r : 0;
5705}
5706
5707static int trivial_method(int argc, char *argv[], void *userdata) {
5708 _cleanup_(sd_bus_error_free)__attribute__((cleanup(sd_bus_error_free))) sd_bus_error error = SD_BUS_ERROR_NULL((const sd_bus_error) {(((void*)0)), (((void*)0)), 0});
5709 const char *method;
5710 sd_bus *bus;
5711 int r;
5712
5713 if (arg_dry_run)
5714 return 0;
5715
5716 r = acquire_bus(BUS_MANAGER, &bus);
5717 if (r < 0)
5718 return r;
5719
5720 polkit_agent_open_maybe();
5721
5722 method =
5723 streq(argv[0], "clear-jobs")(strcmp((argv[0]),("clear-jobs")) == 0) ||
5724 streq(argv[0], "cancel")(strcmp((argv[0]),("cancel")) == 0) ? "ClearJobs" :
5725 streq(argv[0], "reset-failed")(strcmp((argv[0]),("reset-failed")) == 0) ? "ResetFailed" :
5726 streq(argv[0], "halt")(strcmp((argv[0]),("halt")) == 0) ? "Halt" :
5727 streq(argv[0], "reboot")(strcmp((argv[0]),("reboot")) == 0) ? "Reboot" :
5728 streq(argv[0], "kexec")(strcmp((argv[0]),("kexec")) == 0) ? "KExec" :
5729 streq(argv[0], "exit")(strcmp((argv[0]),("exit")) == 0) ? "Exit" :
5730 /* poweroff */ "PowerOff";
5731
5732 r = sd_bus_call_method(
5733 bus,
5734 "org.freedesktop.systemd1",
5735 "/org/freedesktop/systemd1",
5736 "org.freedesktop.systemd1.Manager",
5737 method,
5738 &error,
5739 NULL((void*)0),
5740 NULL((void*)0));
5741 if (r < 0 && arg_action == ACTION_SYSTEMCTL)
5742 return log_error_errno(r, "Failed to execute operation: %s", bus_error_message(&error, r))({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 5742, __func__, "Failed to execute operation: %s"
, bus_error_message(&error, r)) : -abs(_e); })
;
5743
5744 /* Note that for the legacy commands (i.e. those with action != ACTION_SYSTEMCTL) we support fallbacks to the
5745 * old ways of doing things, hence don't log any error in that case here. */
5746
5747 return r < 0 ? r : 0;
5748}
5749
5750static int reset_failed(int argc, char *argv[], void *userdata) {
5751 _cleanup_strv_free___attribute__((cleanup(strv_freep))) char **names = NULL((void*)0);
5752 sd_bus *bus;
5753 char **name;
5754 int r, q;
5755
5756 if (argc <= 1)
5757 return trivial_method(argc, argv, userdata);
5758
5759 r = acquire_bus(BUS_MANAGER, &bus);
5760 if (r < 0)
5761 return r;
5762
5763 polkit_agent_open_maybe();
5764
5765 r = expand_names(bus, strv_skip(argv, 1), NULL((void*)0), &names);
5766 if (r < 0)
5767 return log_error_errno(r, "Failed to expand names: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 5767, __func__, "Failed to expand names: %m"
) : -abs(_e); })
;
5768
5769 STRV_FOREACH(name, names)for ((name) = (names); (name) && *(name); (name)++) {
5770 _cleanup_(sd_bus_error_free)__attribute__((cleanup(sd_bus_error_free))) sd_bus_error error = SD_BUS_ERROR_NULL((const sd_bus_error) {(((void*)0)), (((void*)0)), 0});
5771
5772 q = sd_bus_call_method(
5773 bus,
5774 "org.freedesktop.systemd1",
5775 "/org/freedesktop/systemd1",
5776 "org.freedesktop.systemd1.Manager",
5777 "ResetFailedUnit",
5778 &error,
5779 NULL((void*)0),
5780 "s", *name);
5781 if (q < 0) {
5782 log_error_errno(q, "Failed to reset failed state of unit %s: %s", *name, bus_error_message(&error, q))({ int _level = ((3)), _e = ((q)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 5782, __func__, "Failed to reset failed state of unit %s: %s"
, *name, bus_error_message(&error, q)) : -abs(_e); })
;
5783 if (r == 0)
5784 r = q;
5785 }
5786 }
5787
5788 return r;
5789}
5790
5791static int print_variable(const char *s) {
5792 const char *sep;
5793 _cleanup_free___attribute__((cleanup(freep))) char *esc = NULL((void*)0);
5794
5795 sep = strchr(s, '=');
5796 if (!sep) {
5797 log_error("Invalid environment block")({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 5797, __func__, "Invalid environment block"
) : -abs(_e); })
;
5798 return -EUCLEAN117;
5799 }
5800
5801 esc = shell_maybe_quote(sep + 1, ESCAPE_POSIX);
5802 if (!esc)
5803 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 5803, __func__)
;
5804
5805 printf("%.*s=%s\n", (int)(sep-s), s, esc);
5806 return 0;
5807}
5808
5809static int show_environment(int argc, char *argv[], void *userdata) {
5810 _cleanup_(sd_bus_error_free)__attribute__((cleanup(sd_bus_error_free))) sd_bus_error error = SD_BUS_ERROR_NULL((const sd_bus_error) {(((void*)0)), (((void*)0)), 0});
5811 _cleanup_(sd_bus_message_unrefp)__attribute__((cleanup(sd_bus_message_unrefp))) sd_bus_message *reply = NULL((void*)0);
5812 const char *text;
5813 sd_bus *bus;
5814 int r;
5815
5816 r = acquire_bus(BUS_MANAGER, &bus);
5817 if (r < 0)
5818 return r;
5819
5820 (void) pager_open(arg_no_pager, false0);
5821
5822 r = sd_bus_get_property(
5823 bus,
5824 "org.freedesktop.systemd1",
5825 "/org/freedesktop/systemd1",
5826 "org.freedesktop.systemd1.Manager",
5827 "Environment",
5828 &error,
5829 &reply,
5830 "as");
5831 if (r < 0)
5832 return log_error_errno(r, "Failed to get environment: %s", bus_error_message(&error, r))({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 5832, __func__, "Failed to get environment: %s"
, bus_error_message(&error, r)) : -abs(_e); })
;
5833
5834 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "s");
5835 if (r < 0)
5836 return bus_log_parse_error(r);
5837
5838 while ((r = sd_bus_message_read_basic(reply, SD_BUS_TYPE_STRING, &text)) > 0) {
5839 r = print_variable(text);
5840 if (r < 0)
5841 return r;
5842 }
5843 if (r < 0)
5844 return bus_log_parse_error(r);
5845
5846 r = sd_bus_message_exit_container(reply);
5847 if (r < 0)
5848 return bus_log_parse_error(r);
5849
5850 return 0;
5851}
5852
5853static int switch_root(int argc, char *argv[], void *userdata) {
5854 _cleanup_(sd_bus_error_free)__attribute__((cleanup(sd_bus_error_free))) sd_bus_error error = SD_BUS_ERROR_NULL((const sd_bus_error) {(((void*)0)), (((void*)0)), 0});
5855 _cleanup_free___attribute__((cleanup(freep))) char *cmdline_init = NULL((void*)0);
5856 const char *root, *init;
5857 sd_bus *bus;
5858 int r;
5859
5860 if (arg_transport != BUS_TRANSPORT_LOCAL) {
5861 log_error("Cannot switch root remotely.")({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 5861, __func__, "Cannot switch root remotely."
) : -abs(_e); })
;
5862 return -EINVAL22;
5863 }
5864
5865 if (argc < 2 || argc > 3) {
5866 log_error("Wrong number of arguments.")({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 5866, __func__, "Wrong number of arguments."
) : -abs(_e); })
;
5867 return -EINVAL22;
5868 }
5869
5870 root = argv[1];
5871
5872 if (argc >= 3)
5873 init = argv[2];
5874 else {
5875 r = parse_env_file(NULL((void*)0), "/proc/cmdline", WHITESPACE" \t\n\r",
5876 "init", &cmdline_init,
5877 NULL((void*)0));
5878 if (r < 0)
5879 log_debug_errno(r, "Failed to parse /proc/cmdline: %m")({ int _level = ((7)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 5879, __func__, "Failed to parse /proc/cmdline: %m"
) : -abs(_e); })
;
5880
5881 init = cmdline_init;
5882 }
5883
5884 init = empty_to_null(init);
5885 if (init) {
5886 const char *root_systemd_path = NULL((void*)0), *root_init_path = NULL((void*)0);
5887
5888 root_systemd_path = strjoina(root, "/" SYSTEMD_BINARY_PATH)({ const char *_appendees_[] = { root, "/" "/usr/lib/systemd/systemd"
}; char *_d_, *_p_; size_t _len_ = 0; size_t _i_; for (_i_ =
0; _i_ < __extension__ (__builtin_choose_expr( !__builtin_types_compatible_p
(typeof(_appendees_), typeof(&*(_appendees_))), sizeof(_appendees_
)/sizeof((_appendees_)[0]), ((void)0))) && _appendees_
[_i_]; _i_++) _len_ += strlen(_appendees_[_i_]); _p_ = _d_ = __builtin_alloca
(_len_ + 1); for (_i_ = 0; _i_ < __extension__ (__builtin_choose_expr
( !__builtin_types_compatible_p(typeof(_appendees_), typeof(&
*(_appendees_))), sizeof(_appendees_)/sizeof((_appendees_)[0]
), ((void)0))) && _appendees_[_i_]; _i_++) _p_ = stpcpy
(_p_, _appendees_[_i_]); *_p_ = 0; _d_; })
;
5889 root_init_path = strjoina(root, "/", init)({ const char *_appendees_[] = { root, "/", init }; char *_d_
, *_p_; size_t _len_ = 0; size_t _i_; for (_i_ = 0; _i_ < __extension__
(__builtin_choose_expr( !__builtin_types_compatible_p(typeof
(_appendees_), typeof(&*(_appendees_))), sizeof(_appendees_
)/sizeof((_appendees_)[0]), ((void)0))) && _appendees_
[_i_]; _i_++) _len_ += strlen(_appendees_[_i_]); _p_ = _d_ = __builtin_alloca
(_len_ + 1); for (_i_ = 0; _i_ < __extension__ (__builtin_choose_expr
( !__builtin_types_compatible_p(typeof(_appendees_), typeof(&
*(_appendees_))), sizeof(_appendees_)/sizeof((_appendees_)[0]
), ((void)0))) && _appendees_[_i_]; _i_++) _p_ = stpcpy
(_p_, _appendees_[_i_]); *_p_ = 0; _d_; })
;
5890
5891 /* If the passed init is actually the same as the
5892 * systemd binary, then let's suppress it. */
5893 if (files_same(root_init_path, root_systemd_path, 0) > 0)
5894 init = NULL((void*)0);
5895 }
5896
5897 /* Instruct PID1 to exclude us from its killing spree applied during
5898 * the transition. Otherwise we would exit with a failure status even
5899 * though the switch to the new root has succeed. */
5900 argv_cmdline[0] = '@';
5901
5902 r = acquire_bus(BUS_MANAGER, &bus);
5903 if (r < 0)
5904 return r;
5905
5906 /* If we are slow to exit after the root switch, the new systemd instance
5907 * will send us a signal to terminate. Just ignore it and exit normally.
5908 * This way the unit does not end up as failed.
5909 */
5910 r = ignore_signals(SIGTERM15, -1);
5911 if (r < 0)
5912 log_warning_errno(r, "Failed to change disposition of SIGTERM to ignore: %m")({ int _level = ((4)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 5912, __func__, "Failed to change disposition of SIGTERM to ignore: %m"
) : -abs(_e); })
;
5913
5914 log_debug("Switching root - root: %s; init: %s", root, strna(init))({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 5914, __func__, "Switching root - root: %s; init: %s"
, root, strna(init)) : -abs(_e); })
;
5915
5916 r = sd_bus_call_method(
5917 bus,
5918 "org.freedesktop.systemd1",
5919 "/org/freedesktop/systemd1",
5920 "org.freedesktop.systemd1.Manager",
5921 "SwitchRoot",
5922 &error,
5923 NULL((void*)0),
5924 "ss", root, init);
5925 if (r < 0) {
5926 (void) default_signals(SIGTERM15, -1);
5927
5928 return log_error_errno(r, "Failed to switch root: %s", bus_error_message(&error, r))({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 5928, __func__, "Failed to switch root: %s"
, bus_error_message(&error, r)) : -abs(_e); })
;
5929 }
5930
5931 return 0;
5932}
5933
5934static int set_environment(int argc, char *argv[], void *userdata) {
5935 _cleanup_(sd_bus_error_free)__attribute__((cleanup(sd_bus_error_free))) sd_bus_error error = SD_BUS_ERROR_NULL((const sd_bus_error) {(((void*)0)), (((void*)0)), 0});
5936 _cleanup_(sd_bus_message_unrefp)__attribute__((cleanup(sd_bus_message_unrefp))) sd_bus_message *m = NULL((void*)0);
5937 const char *method;
5938 sd_bus *bus;
5939 int r;
5940
5941 assert(argc > 1)do { if ((__builtin_expect(!!(!(argc > 1)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("argc > 1"), "../src/systemctl/systemctl.c"
, 5941, __PRETTY_FUNCTION__); } while (0)
;
5942 assert(argv)do { if ((__builtin_expect(!!(!(argv)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("argv"), "../src/systemctl/systemctl.c",
5942, __PRETTY_FUNCTION__); } while (0)
;
5943
5944 r = acquire_bus(BUS_MANAGER, &bus);
5945 if (r < 0)
5946 return r;
5947
5948 polkit_agent_open_maybe();
5949
5950 method = streq(argv[0], "set-environment")(strcmp((argv[0]),("set-environment")) == 0)
5951 ? "SetEnvironment"
5952 : "UnsetEnvironment";
5953
5954 r = sd_bus_message_new_method_call(
5955 bus,
5956 &m,
5957 "org.freedesktop.systemd1",
5958 "/org/freedesktop/systemd1",
5959 "org.freedesktop.systemd1.Manager",
5960 method);
5961 if (r < 0)
5962 return bus_log_create_error(r);
5963
5964 r = sd_bus_message_append_strv(m, strv_skip(argv, 1));
5965 if (r < 0)
5966 return bus_log_create_error(r);
5967
5968 r = sd_bus_call(bus, m, 0, &error, NULL((void*)0));
5969 if (r < 0)
5970 return log_error_errno(r, "Failed to set environment: %s", bus_error_message(&error, r))({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 5970, __func__, "Failed to set environment: %s"
, bus_error_message(&error, r)) : -abs(_e); })
;
5971
5972 return 0;
5973}
5974
5975static int import_environment(int argc, char *argv[], void *userdata) {
5976 _cleanup_(sd_bus_error_free)__attribute__((cleanup(sd_bus_error_free))) sd_bus_error error = SD_BUS_ERROR_NULL((const sd_bus_error) {(((void*)0)), (((void*)0)), 0});
5977 _cleanup_(sd_bus_message_unrefp)__attribute__((cleanup(sd_bus_message_unrefp))) sd_bus_message *m = NULL((void*)0);
5978 sd_bus *bus;
5979 int r;
5980
5981 r = acquire_bus(BUS_MANAGER, &bus);
5982 if (r < 0)
5983 return r;
5984
5985 polkit_agent_open_maybe();
5986
5987 r = sd_bus_message_new_method_call(
5988 bus,
5989 &m,
5990 "org.freedesktop.systemd1",
5991 "/org/freedesktop/systemd1",
5992 "org.freedesktop.systemd1.Manager",
5993 "SetEnvironment");
5994 if (r < 0)
5995 return bus_log_create_error(r);
5996
5997 if (argc < 2)
5998 r = sd_bus_message_append_strv(m, environ);
5999 else {
6000 char **a, **b;
6001
6002 r = sd_bus_message_open_container(m, 'a', "s");
6003 if (r < 0)
6004 return bus_log_create_error(r);
6005
6006 STRV_FOREACH(a, strv_skip(argv, 1))for ((a) = (strv_skip(argv, 1)); (a) && *(a); (a)++) {
6007
6008 if (!env_name_is_valid(*a)) {
6009 log_error("Not a valid environment variable name: %s", *a)({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 6009, __func__, "Not a valid environment variable name: %s"
, *a) : -abs(_e); })
;
6010 return -EINVAL22;
6011 }
6012
6013 STRV_FOREACH(b, environ)for ((b) = (environ); (b) && *(b); (b)++) {
6014 const char *eq;
6015
6016 eq = startswith(*b, *a);
6017 if (eq && *eq == '=') {
6018
6019 r = sd_bus_message_append(m, "s", *b);
6020 if (r < 0)
6021 return bus_log_create_error(r);
6022
6023 break;
6024 }
6025 }
6026 }
6027
6028 r = sd_bus_message_close_container(m);
6029 }
6030 if (r < 0)
6031 return bus_log_create_error(r);
6032
6033 r = sd_bus_call(bus, m, 0, &error, NULL((void*)0));
6034 if (r < 0)
6035 return log_error_errno(r, "Failed to import environment: %s", bus_error_message(&error, r))({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 6035, __func__, "Failed to import environment: %s"
, bus_error_message(&error, r)) : -abs(_e); })
;
6036
6037 return 0;
6038}
6039
6040static int enable_sysv_units(const char *verb, char **args) {
6041 int r = 0;
6042
6043#if HAVE_SYSV_COMPAT1
6044 _cleanup_(lookup_paths_free)__attribute__((cleanup(lookup_paths_free))) LookupPaths paths = {};
6045 unsigned f = 0;
6046
6047 /* Processes all SysV units, and reshuffles the array so that afterwards only the native units remain */
6048
6049 if (arg_scope != UNIT_FILE_SYSTEM)
6050 return 0;
6051
6052 if (getenv_bool("SYSTEMCTL_SKIP_SYSV") > 0)
6053 return 0;
6054
6055 if (!STR_IN_SET(verb,(!!strv_find((((char**) ((const char*[]) { "enable", "disable"
, "is-enabled", ((void*)0) }))), (verb)))
6056 "enable",(!!strv_find((((char**) ((const char*[]) { "enable", "disable"
, "is-enabled", ((void*)0) }))), (verb)))
6057 "disable",(!!strv_find((((char**) ((const char*[]) { "enable", "disable"
, "is-enabled", ((void*)0) }))), (verb)))
6058 "is-enabled")(!!strv_find((((char**) ((const char*[]) { "enable", "disable"
, "is-enabled", ((void*)0) }))), (verb)))
)
6059 return 0;
6060
6061 r = lookup_paths_init(&paths, arg_scope, LOOKUP_PATHS_EXCLUDE_GENERATED, arg_root);
6062 if (r < 0)
6063 return r;
6064
6065 r = 0;
6066 while (args[f]) {
6067
6068 const char *argv[] = {
6069 ROOTLIBEXECDIR"/usr/lib/systemd" "/systemd-sysv-install",
6070 NULL((void*)0),
6071 NULL((void*)0),
6072 NULL((void*)0),
6073 NULL((void*)0),
6074 };
6075
6076 _cleanup_free___attribute__((cleanup(freep))) char *p = NULL((void*)0), *q = NULL((void*)0), *l = NULL((void*)0);
6077 bool_Bool found_native = false0, found_sysv;
6078 const char *name;
6079 unsigned c = 1;
6080 pid_t pid;
6081 int j;
6082
6083 name = args[f++];
6084
6085 if (!endswith(name, ".service"))
6086 continue;
6087
6088 if (path_is_absolute(name))
6089 continue;
6090
6091 j = unit_file_exists(arg_scope, &paths, name);
6092 if (j < 0 && !IN_SET(j, -ELOOP, -ERFKILL, -EADDRNOTAVAIL)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){-40, -132, -99})/sizeof(int)]; switch(j)
{ case -40: case -132: case -99: _found = 1; break; default:
break; } _found; })
)
6093 return log_error_errno(j, "Failed to lookup unit file state: %m")({ int _level = ((3)), _e = ((j)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 6093, __func__, "Failed to lookup unit file state: %m"
) : -abs(_e); })
;
6094 found_native = j != 0;
6095
6096 /* If we have both a native unit and a SysV script, enable/disable them both (below); for is-enabled,
6097 * prefer the native unit */
6098 if (found_native && streq(verb, "is-enabled")(strcmp((verb),("is-enabled")) == 0))
6099 continue;
6100
6101 p = path_join(arg_root, SYSTEM_SYSVINIT_PATH"/etc/init.d", name);
6102 if (!p)
6103 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 6103, __func__)
;
6104
6105 p[strlen(p) - STRLEN(".service")(sizeof(""".service""") - 1)] = 0;
6106 found_sysv = access(p, F_OK0) >= 0;
6107 if (!found_sysv)
6108 continue;
6109
6110 if (!arg_quiet) {
6111 if (found_native)
6112 log_info("Synchronizing state of %s with SysV service script with %s.", name, argv[0])({ int _level = (((6))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 6112, __func__, "Synchronizing state of %s with SysV service script with %s."
, name, argv[0]) : -abs(_e); })
;
6113 else
6114 log_info("%s is not a native service, redirecting to systemd-sysv-install.", name)({ int _level = (((6))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 6114, __func__, "%s is not a native service, redirecting to systemd-sysv-install."
, name) : -abs(_e); })
;
6115 }
6116
6117 if (!isempty(arg_root))
6118 argv[c++] = q = strappend("--root=", arg_root);
6119
6120 argv[c++] = verb;
6121 argv[c++] = basename(p);
6122 argv[c] = NULL((void*)0);
6123
6124 l = strv_join((char**)argv, " ");
6125 if (!l)
6126 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 6126, __func__)
;
6127
6128 if (!arg_quiet)
6129 log_info("Executing: %s", l)({ int _level = (((6))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 6129, __func__, "Executing: %s"
, l) : -abs(_e); })
;
6130
6131 j = safe_fork("(sysv-install)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_LOG, &pid);
6132 if (j < 0)
6133 return j;
6134 if (j == 0) {
6135 /* Child */
6136 execv(argv[0], (char**) argv);
6137 log_error_errno(errno, "Failed to execute %s: %m", argv[0])({ int _level = ((3)), _e = (((*__errno_location ()))), _realm
= (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >=
((_level) & 0x07)) ? log_internal_realm(((_realm) <<
10 | (_level)), _e, "../src/systemctl/systemctl.c", 6137, __func__
, "Failed to execute %s: %m", argv[0]) : -abs(_e); })
;
6138 _exit(EXIT_FAILURE1);
6139 }
6140
6141 j = wait_for_terminate_and_check("sysv-install", pid, WAIT_LOG_ABNORMAL);
6142 if (j < 0)
6143 return j;
6144 if (streq(verb, "is-enabled")(strcmp((verb),("is-enabled")) == 0)) {
6145 if (j == EXIT_SUCCESS0) {
6146 if (!arg_quiet)
6147 puts("enabled");
6148 r = 1;
6149 } else {
6150 if (!arg_quiet)
6151 puts("disabled");
6152 }
6153
6154 } else if (j != EXIT_SUCCESS0)
6155 return -EBADE52; /* We don't warn here, under the assumption the script already showed an explanation */
6156
6157 if (found_native)
6158 continue;
6159
6160 /* Remove this entry, so that we don't try enabling it as native unit */
6161 assert(f > 0)do { if ((__builtin_expect(!!(!(f > 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("f > 0"), "../src/systemctl/systemctl.c"
, 6161, __PRETTY_FUNCTION__); } while (0)
;
6162 f--;
6163 assert(args[f] == name)do { if ((__builtin_expect(!!(!(args[f] == name)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("args[f] == name"), "../src/systemctl/systemctl.c"
, 6163, __PRETTY_FUNCTION__); } while (0)
;
6164 strv_remove(args, name);
6165 }
6166
6167#endif
6168 return r;
6169}
6170
6171static int mangle_names(char **original_names, char ***mangled_names) {
6172 char **i, **l, **name;
6173 int r;
6174
6175 l = i = new(char*, strv_length(original_names) + 1)((char**) malloc_multiply(sizeof(char*), (strv_length(original_names
) + 1)))
;
6176 if (!l)
6177 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 6177, __func__)
;
6178
6179 STRV_FOREACH(name, original_names)for ((name) = (original_names); (name) && *(name); (name
)++)
{
6180
6181 /* When enabling units qualified path names are OK,
6182 * too, hence allow them explicitly. */
6183
6184 if (is_path(*name)) {
6185 *i = strdup(*name);
6186 if (!*i) {
6187 strv_free(l);
6188 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 6188, __func__)
;
6189 }
6190 } else {
6191 r = unit_name_mangle(*name, arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN, i);
6192 if (r < 0) {
6193 *i = NULL((void*)0);
6194 strv_free(l);
6195 return log_error_errno(r, "Failed to mangle unit name: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 6195, __func__, "Failed to mangle unit name: %m"
) : -abs(_e); })
;
6196 }
6197 }
6198
6199 i++;
6200 }
6201
6202 *i = NULL((void*)0);
6203 *mangled_names = l;
6204
6205 return 0;
6206}
6207
6208static int normalize_filenames(char **names) {
6209 char **u;
6210 int r;
6211
6212 STRV_FOREACH(u, names)for ((u) = (names); (u) && *(u); (u)++)
6213 if (!path_is_absolute(*u)) {
6214 char* normalized_path;
6215
6216 if (!isempty(arg_root)) {
6217 log_error("Non-absolute paths are not allowed when --root is used: %s", *u)({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 6217, __func__, "Non-absolute paths are not allowed when --root is used: %s"
, *u) : -abs(_e); })
;
6218 return -EINVAL22;
6219 }
6220
6221 if (!strchr(*u,'/')) {
6222 log_error("Link argument does contain at least one directory separator: %s", *u)({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 6222, __func__, "Link argument does contain at least one directory separator: %s"
, *u) : -abs(_e); })
;
6223 return -EINVAL22;
6224 }
6225
6226 r = path_make_absolute_cwd(*u, &normalized_path);
6227 if (r < 0)
6228 return r;
6229
6230 free_and_replace(*u, normalized_path)({ free(*u); (*u) = (normalized_path); (normalized_path) = ((
void*)0); 0; })
;
6231 }
6232
6233 return 0;
6234}
6235
6236static int normalize_names(char **names, bool_Bool warn_if_path) {
6237 char **u;
6238 bool_Bool was_path = false0;
6239
6240 STRV_FOREACH(u, names)for ((u) = (names); (u) && *(u); (u)++) {
6241 int r;
6242
6243 if (!is_path(*u))
6244 continue;
6245
6246 r = free_and_strdup(u, basename(*u));
6247 if (r < 0)
6248 return log_error_errno(r, "Failed to normalize unit file path: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 6248, __func__, "Failed to normalize unit file path: %m"
) : -abs(_e); })
;
6249
6250 was_path = true1;
6251 }
6252
6253 if (warn_if_path && was_path)
6254 log_warning("Warning: Can't execute disable on the unit file path. Proceeding with the unit name.")({ int _level = (((4))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 6254, __func__, "Warning: Can't execute disable on the unit file path. Proceeding with the unit name."
) : -abs(_e); })
;
6255
6256 return 0;
6257}
6258
6259static int unit_exists(LookupPaths *lp, const char *unit) {
6260 _cleanup_(sd_bus_error_free)__attribute__((cleanup(sd_bus_error_free))) sd_bus_error error = SD_BUS_ERROR_NULL((const sd_bus_error) {(((void*)0)), (((void*)0)), 0});
6261 _cleanup_(sd_bus_message_unrefp)__attribute__((cleanup(sd_bus_message_unrefp))) sd_bus_message *m = NULL((void*)0);
6262 _cleanup_free___attribute__((cleanup(freep))) char *path = NULL((void*)0);
6263 static const struct bus_properties_map property_map[] = {
6264 { "LoadState", "s", NULL((void*)0), offsetof(UnitStatusInfo, load_state)__builtin_offsetof(UnitStatusInfo, load_state) },
6265 { "ActiveState", "s", NULL((void*)0), offsetof(UnitStatusInfo, active_state)__builtin_offsetof(UnitStatusInfo, active_state)},
6266 {},
6267 };
6268 UnitStatusInfo info = {};
6269 sd_bus *bus;
6270 int r;
6271
6272 if (unit_name_is_valid(unit, UNIT_NAME_TEMPLATE))
6273 return unit_find_template_path(unit, lp, NULL((void*)0), NULL((void*)0));
6274
6275 path = unit_dbus_path_from_name(unit);
6276 if (!path)
6277 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 6277, __func__)
;
6278
6279 r = acquire_bus(BUS_MANAGER, &bus);
6280 if (r < 0)
6281 return r;
6282
6283 r = bus_map_all_properties(bus, "org.freedesktop.systemd1", path, property_map, 0, &error, &m, &info);
6284 if (r < 0)
6285 return log_error_errno(r, "Failed to get properties: %s", bus_error_message(&error, r))({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 6285, __func__, "Failed to get properties: %s"
, bus_error_message(&error, r)) : -abs(_e); })
;
6286
6287 return !streq_ptr(info.load_state, "not-found") || !streq_ptr(info.active_state, "inactive");
6288}
6289
6290static int enable_unit(int argc, char *argv[], void *userdata) {
6291 _cleanup_strv_free___attribute__((cleanup(strv_freep))) char **names = NULL((void*)0);
6292 const char *verb = argv[0];
6293 UnitFileChange *changes = NULL((void*)0);
6294 size_t n_changes = 0;
6295 int carries_install_info = -1;
6296 bool_Bool ignore_carries_install_info = arg_quiet;
6297 int r;
6298
6299 if (!argv[1])
6300 return 0;
6301
6302 r = mangle_names(strv_skip(argv, 1), &names);
6303 if (r < 0)
6304 return r;
6305
6306 r = enable_sysv_units(verb, names);
6307 if (r < 0)
6308 return r;
6309
6310 /* If the operation was fully executed by the SysV compat, let's finish early */
6311 if (strv_isempty(names)) {
6312 if (arg_no_reload || install_client_side())
6313 return 0;
6314 return daemon_reload(argc, argv, userdata);
6315 }
6316
6317 if (streq(verb, "disable")(strcmp((verb),("disable")) == 0)) {
6318 r = normalize_names(names, true1);
6319 if (r < 0)
6320 return r;
6321 }
6322
6323 if (streq(verb, "link")(strcmp((verb),("link")) == 0)) {
6324 r = normalize_filenames(names);
6325 if (r < 0)
6326 return r;
6327 }
6328
6329 if (install_client_side()) {
6330 UnitFileFlags flags;
6331
6332 flags = args_to_flags();
6333 if (streq(verb, "enable")(strcmp((verb),("enable")) == 0)) {
6334 r = unit_file_enable(arg_scope, flags, arg_root, names, &changes, &n_changes);
6335 carries_install_info = r;
6336 } else if (streq(verb, "disable")(strcmp((verb),("disable")) == 0))
6337 r = unit_file_disable(arg_scope, flags, arg_root, names, &changes, &n_changes);
6338 else if (streq(verb, "reenable")(strcmp((verb),("reenable")) == 0)) {
6339 r = unit_file_reenable(arg_scope, flags, arg_root, names, &changes, &n_changes);
6340 carries_install_info = r;
6341 } else if (streq(verb, "link")(strcmp((verb),("link")) == 0))
6342 r = unit_file_link(arg_scope, flags, arg_root, names, &changes, &n_changes);
6343 else if (streq(verb, "preset")(strcmp((verb),("preset")) == 0)) {
6344 r = unit_file_preset(arg_scope, flags, arg_root, names, arg_preset_mode, &changes, &n_changes);
6345 } else if (streq(verb, "mask")(strcmp((verb),("mask")) == 0))
6346 r = unit_file_mask(arg_scope, flags, arg_root, names, &changes, &n_changes);
6347 else if (streq(verb, "unmask")(strcmp((verb),("unmask")) == 0))
6348 r = unit_file_unmask(arg_scope, flags, arg_root, names, &changes, &n_changes);
6349 else if (streq(verb, "revert")(strcmp((verb),("revert")) == 0))
6350 r = unit_file_revert(arg_scope, arg_root, names, &changes, &n_changes);
6351 else
6352 assert_not_reached("Unknown verb")do { log_assert_failed_unreachable_realm(LOG_REALM_SYSTEMD, (
"Unknown verb"), "../src/systemctl/systemctl.c", 6352, __PRETTY_FUNCTION__
); } while (0)
;
6353
6354 unit_file_dump_changes(r, verb, changes, n_changes, arg_quiet);
6355 if (r < 0)
6356 goto finish;
6357 r = 0;
6358 } else {
6359 _cleanup_(sd_bus_message_unrefp)__attribute__((cleanup(sd_bus_message_unrefp))) sd_bus_message *reply = NULL((void*)0), *m = NULL((void*)0);
6360 _cleanup_(sd_bus_error_free)__attribute__((cleanup(sd_bus_error_free))) sd_bus_error error = SD_BUS_ERROR_NULL((const sd_bus_error) {(((void*)0)), (((void*)0)), 0});
6361 bool_Bool expect_carries_install_info = false0;
6362 bool_Bool send_runtime = true1, send_force = true1, send_preset_mode = false0;
6363 const char *method;
6364 sd_bus *bus;
6365
6366 if (STR_IN_SET(verb, "mask", "unmask")(!!strv_find((((char**) ((const char*[]) { "mask", "unmask", (
(void*)0) }))), (verb)))
) {
6367 char **name;
6368 _cleanup_(lookup_paths_free)__attribute__((cleanup(lookup_paths_free))) LookupPaths lp = {};
6369
6370 r = lookup_paths_init(&lp, arg_scope, 0, arg_root);
6371 if (r < 0)
6372 return r;
6373
6374 STRV_FOREACH(name, names)for ((name) = (names); (name) && *(name); (name)++) {
6375 r = unit_exists(&lp, *name);
6376 if (r < 0)
6377 return r;
6378 if (r == 0)
6379 log_notice("Unit %s does not exist, proceeding anyway.", *name)({ int _level = (((5))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 6379, __func__, "Unit %s does not exist, proceeding anyway."
, *name) : -abs(_e); })
;
6380 }
6381 }
6382
6383 r = acquire_bus(BUS_MANAGER, &bus);
6384 if (r < 0)
6385 return r;
6386
6387 polkit_agent_open_maybe();
6388
6389 if (streq(verb, "enable")(strcmp((verb),("enable")) == 0)) {
6390 method = "EnableUnitFiles";
6391 expect_carries_install_info = true1;
6392 } else if (streq(verb, "disable")(strcmp((verb),("disable")) == 0)) {
6393 method = "DisableUnitFiles";
6394 send_force = false0;
6395 } else if (streq(verb, "reenable")(strcmp((verb),("reenable")) == 0)) {
6396 method = "ReenableUnitFiles";
6397 expect_carries_install_info = true1;
6398 } else if (streq(verb, "link")(strcmp((verb),("link")) == 0))
6399 method = "LinkUnitFiles";
6400 else if (streq(verb, "preset")(strcmp((verb),("preset")) == 0)) {
6401
6402 if (arg_preset_mode != UNIT_FILE_PRESET_FULL) {
6403 method = "PresetUnitFilesWithMode";
6404 send_preset_mode = true1;
6405 } else
6406 method = "PresetUnitFiles";
6407
6408 expect_carries_install_info = true1;
6409 ignore_carries_install_info = true1;
6410 } else if (streq(verb, "mask")(strcmp((verb),("mask")) == 0))
6411 method = "MaskUnitFiles";
6412 else if (streq(verb, "unmask")(strcmp((verb),("unmask")) == 0)) {
6413 method = "UnmaskUnitFiles";
6414 send_force = false0;
6415 } else if (streq(verb, "revert")(strcmp((verb),("revert")) == 0)) {
6416 method = "RevertUnitFiles";
6417 send_runtime = send_force = false0;
6418 } else
6419 assert_not_reached("Unknown verb")do { log_assert_failed_unreachable_realm(LOG_REALM_SYSTEMD, (
"Unknown verb"), "../src/systemctl/systemctl.c", 6419, __PRETTY_FUNCTION__
); } while (0)
;
6420
6421 r = sd_bus_message_new_method_call(
6422 bus,
6423 &m,
6424 "org.freedesktop.systemd1",
6425 "/org/freedesktop/systemd1",
6426 "org.freedesktop.systemd1.Manager",
6427 method);
6428 if (r < 0)
6429 return bus_log_create_error(r);
6430
6431 r = sd_bus_message_append_strv(m, names);
6432 if (r < 0)
6433 return bus_log_create_error(r);
6434
6435 if (send_preset_mode) {
6436 r = sd_bus_message_append(m, "s", unit_file_preset_mode_to_string(arg_preset_mode));
6437 if (r < 0)
6438 return bus_log_create_error(r);
6439 }
6440
6441 if (send_runtime) {
6442 r = sd_bus_message_append(m, "b", arg_runtime);
6443 if (r < 0)
6444 return bus_log_create_error(r);
6445 }
6446
6447 if (send_force) {
6448 r = sd_bus_message_append(m, "b", arg_force);
6449 if (r < 0)
6450 return bus_log_create_error(r);
6451 }
6452
6453 r = sd_bus_call(bus, m, 0, &error, &reply);
6454 if (r < 0)
6455 return log_error_errno(r, "Failed to %s unit: %s", verb, bus_error_message(&error, r))({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 6455, __func__, "Failed to %s unit: %s"
, verb, bus_error_message(&error, r)) : -abs(_e); })
;
6456
6457 if (expect_carries_install_info) {
6458 r = sd_bus_message_read(reply, "b", &carries_install_info);
6459 if (r < 0)
6460 return bus_log_parse_error(r);
6461 }
6462
6463 r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet, &changes, &n_changes);
6464 if (r < 0)
6465 goto finish;
6466
6467 /* Try to reload if enabled */
6468 if (!arg_no_reload)
6469 r = daemon_reload(argc, argv, userdata);
6470 else
6471 r = 0;
6472 }
6473
6474 if (carries_install_info == 0 && !ignore_carries_install_info)
6475 log_warning("The unit files have no installation config (WantedBy, RequiredBy, Also, Alias\n"({ int _level = (((4))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 6486, __func__, "The unit files have no installation config (WantedBy, RequiredBy, Also, Alias\n"
"settings in the [Install] section, and DefaultInstance for template units).\n"
"This means they are not meant to be enabled using systemctl.\n"
"Possible reasons for having this kind of units are:\n" "1) A unit may be statically enabled by being symlinked from another unit's\n"
" .wants/ or .requires/ directory.\n" "2) A unit's purpose may be to act as a helper for some other unit which has\n"
" a requirement dependency on it.\n" "3) A unit may be started when needed via activation (socket, path, timer,\n"
" D-Bus, udev, scripted systemctl call, ...).\n" "4) In case of template units, the unit is meant to be enabled with some\n"
" instance name specified.") : -abs(_e); })
6476 "settings in the [Install] section, and DefaultInstance for template units).\n"({ int _level = (((4))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 6486, __func__, "The unit files have no installation config (WantedBy, RequiredBy, Also, Alias\n"
"settings in the [Install] section, and DefaultInstance for template units).\n"
"This means they are not meant to be enabled using systemctl.\n"
"Possible reasons for having this kind of units are:\n" "1) A unit may be statically enabled by being symlinked from another unit's\n"
" .wants/ or .requires/ directory.\n" "2) A unit's purpose may be to act as a helper for some other unit which has\n"
" a requirement dependency on it.\n" "3) A unit may be started when needed via activation (socket, path, timer,\n"
" D-Bus, udev, scripted systemctl call, ...).\n" "4) In case of template units, the unit is meant to be enabled with some\n"
" instance name specified.") : -abs(_e); })
6477 "This means they are not meant to be enabled using systemctl.\n"({ int _level = (((4))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 6486, __func__, "The unit files have no installation config (WantedBy, RequiredBy, Also, Alias\n"
"settings in the [Install] section, and DefaultInstance for template units).\n"
"This means they are not meant to be enabled using systemctl.\n"
"Possible reasons for having this kind of units are:\n" "1) A unit may be statically enabled by being symlinked from another unit's\n"
" .wants/ or .requires/ directory.\n" "2) A unit's purpose may be to act as a helper for some other unit which has\n"
" a requirement dependency on it.\n" "3) A unit may be started when needed via activation (socket, path, timer,\n"
" D-Bus, udev, scripted systemctl call, ...).\n" "4) In case of template units, the unit is meant to be enabled with some\n"
" instance name specified.") : -abs(_e); })
6478 "Possible reasons for having this kind of units are:\n"({ int _level = (((4))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 6486, __func__, "The unit files have no installation config (WantedBy, RequiredBy, Also, Alias\n"
"settings in the [Install] section, and DefaultInstance for template units).\n"
"This means they are not meant to be enabled using systemctl.\n"
"Possible reasons for having this kind of units are:\n" "1) A unit may be statically enabled by being symlinked from another unit's\n"
" .wants/ or .requires/ directory.\n" "2) A unit's purpose may be to act as a helper for some other unit which has\n"
" a requirement dependency on it.\n" "3) A unit may be started when needed via activation (socket, path, timer,\n"
" D-Bus, udev, scripted systemctl call, ...).\n" "4) In case of template units, the unit is meant to be enabled with some\n"
" instance name specified.") : -abs(_e); })
6479 "1) A unit may be statically enabled by being symlinked from another unit's\n"({ int _level = (((4))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 6486, __func__, "The unit files have no installation config (WantedBy, RequiredBy, Also, Alias\n"
"settings in the [Install] section, and DefaultInstance for template units).\n"
"This means they are not meant to be enabled using systemctl.\n"
"Possible reasons for having this kind of units are:\n" "1) A unit may be statically enabled by being symlinked from another unit's\n"
" .wants/ or .requires/ directory.\n" "2) A unit's purpose may be to act as a helper for some other unit which has\n"
" a requirement dependency on it.\n" "3) A unit may be started when needed via activation (socket, path, timer,\n"
" D-Bus, udev, scripted systemctl call, ...).\n" "4) In case of template units, the unit is meant to be enabled with some\n"
" instance name specified.") : -abs(_e); })
6480 " .wants/ or .requires/ directory.\n"({ int _level = (((4))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 6486, __func__, "The unit files have no installation config (WantedBy, RequiredBy, Also, Alias\n"
"settings in the [Install] section, and DefaultInstance for template units).\n"
"This means they are not meant to be enabled using systemctl.\n"
"Possible reasons for having this kind of units are:\n" "1) A unit may be statically enabled by being symlinked from another unit's\n"
" .wants/ or .requires/ directory.\n" "2) A unit's purpose may be to act as a helper for some other unit which has\n"
" a requirement dependency on it.\n" "3) A unit may be started when needed via activation (socket, path, timer,\n"
" D-Bus, udev, scripted systemctl call, ...).\n" "4) In case of template units, the unit is meant to be enabled with some\n"
" instance name specified.") : -abs(_e); })
6481 "2) A unit's purpose may be to act as a helper for some other unit which has\n"({ int _level = (((4))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 6486, __func__, "The unit files have no installation config (WantedBy, RequiredBy, Also, Alias\n"
"settings in the [Install] section, and DefaultInstance for template units).\n"
"This means they are not meant to be enabled using systemctl.\n"
"Possible reasons for having this kind of units are:\n" "1) A unit may be statically enabled by being symlinked from another unit's\n"
" .wants/ or .requires/ directory.\n" "2) A unit's purpose may be to act as a helper for some other unit which has\n"
" a requirement dependency on it.\n" "3) A unit may be started when needed via activation (socket, path, timer,\n"
" D-Bus, udev, scripted systemctl call, ...).\n" "4) In case of template units, the unit is meant to be enabled with some\n"
" instance name specified.") : -abs(_e); })
6482 " a requirement dependency on it.\n"({ int _level = (((4))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 6486, __func__, "The unit files have no installation config (WantedBy, RequiredBy, Also, Alias\n"
"settings in the [Install] section, and DefaultInstance for template units).\n"
"This means they are not meant to be enabled using systemctl.\n"
"Possible reasons for having this kind of units are:\n" "1) A unit may be statically enabled by being symlinked from another unit's\n"
" .wants/ or .requires/ directory.\n" "2) A unit's purpose may be to act as a helper for some other unit which has\n"
" a requirement dependency on it.\n" "3) A unit may be started when needed via activation (socket, path, timer,\n"
" D-Bus, udev, scripted systemctl call, ...).\n" "4) In case of template units, the unit is meant to be enabled with some\n"
" instance name specified.") : -abs(_e); })
6483 "3) A unit may be started when needed via activation (socket, path, timer,\n"({ int _level = (((4))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 6486, __func__, "The unit files have no installation config (WantedBy, RequiredBy, Also, Alias\n"
"settings in the [Install] section, and DefaultInstance for template units).\n"
"This means they are not meant to be enabled using systemctl.\n"
"Possible reasons for having this kind of units are:\n" "1) A unit may be statically enabled by being symlinked from another unit's\n"
" .wants/ or .requires/ directory.\n" "2) A unit's purpose may be to act as a helper for some other unit which has\n"
" a requirement dependency on it.\n" "3) A unit may be started when needed via activation (socket, path, timer,\n"
" D-Bus, udev, scripted systemctl call, ...).\n" "4) In case of template units, the unit is meant to be enabled with some\n"
" instance name specified.") : -abs(_e); })
6484 " D-Bus, udev, scripted systemctl call, ...).\n"({ int _level = (((4))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 6486, __func__, "The unit files have no installation config (WantedBy, RequiredBy, Also, Alias\n"
"settings in the [Install] section, and DefaultInstance for template units).\n"
"This means they are not meant to be enabled using systemctl.\n"
"Possible reasons for having this kind of units are:\n" "1) A unit may be statically enabled by being symlinked from another unit's\n"
" .wants/ or .requires/ directory.\n" "2) A unit's purpose may be to act as a helper for some other unit which has\n"
" a requirement dependency on it.\n" "3) A unit may be started when needed via activation (socket, path, timer,\n"
" D-Bus, udev, scripted systemctl call, ...).\n" "4) In case of template units, the unit is meant to be enabled with some\n"
" instance name specified.") : -abs(_e); })
6485 "4) In case of template units, the unit is meant to be enabled with some\n"({ int _level = (((4))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 6486, __func__, "The unit files have no installation config (WantedBy, RequiredBy, Also, Alias\n"
"settings in the [Install] section, and DefaultInstance for template units).\n"
"This means they are not meant to be enabled using systemctl.\n"
"Possible reasons for having this kind of units are:\n" "1) A unit may be statically enabled by being symlinked from another unit's\n"
" .wants/ or .requires/ directory.\n" "2) A unit's purpose may be to act as a helper for some other unit which has\n"
" a requirement dependency on it.\n" "3) A unit may be started when needed via activation (socket, path, timer,\n"
" D-Bus, udev, scripted systemctl call, ...).\n" "4) In case of template units, the unit is meant to be enabled with some\n"
" instance name specified.") : -abs(_e); })
6486 " instance name specified.")({ int _level = (((4))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 6486, __func__, "The unit files have no installation config (WantedBy, RequiredBy, Also, Alias\n"
"settings in the [Install] section, and DefaultInstance for template units).\n"
"This means they are not meant to be enabled using systemctl.\n"
"Possible reasons for having this kind of units are:\n" "1) A unit may be statically enabled by being symlinked from another unit's\n"
" .wants/ or .requires/ directory.\n" "2) A unit's purpose may be to act as a helper for some other unit which has\n"
" a requirement dependency on it.\n" "3) A unit may be started when needed via activation (socket, path, timer,\n"
" D-Bus, udev, scripted systemctl call, ...).\n" "4) In case of template units, the unit is meant to be enabled with some\n"
" instance name specified.") : -abs(_e); })
;
6487
6488 if (arg_now && STR_IN_SET(argv[0], "enable", "disable", "mask")(!!strv_find((((char**) ((const char*[]) { "enable", "disable"
, "mask", ((void*)0) }))), (argv[0])))
) {
6489 sd_bus *bus;
6490 size_t len, i;
6491
6492 r = acquire_bus(BUS_MANAGER, &bus);
6493 if (r < 0)
6494 goto finish;
6495
6496 len = strv_length(names);
6497 {
6498 char *new_args[len + 2];
6499
6500 new_args[0] = (char*) (streq(argv[0], "enable")(strcmp((argv[0]),("enable")) == 0) ? "start" : "stop");
6501 for (i = 0; i < len; i++)
6502 new_args[i + 1] = basename(names[i]);
6503 new_args[i + 1] = NULL((void*)0);
6504
6505 r = start_unit(len + 1, new_args, userdata);
6506 }
6507 }
6508
6509finish:
6510 unit_file_changes_free(changes, n_changes);
6511
6512 return r;
6513}
6514
6515static int add_dependency(int argc, char *argv[], void *userdata) {
6516 _cleanup_strv_free___attribute__((cleanup(strv_freep))) char **names = NULL((void*)0);
6517 _cleanup_free___attribute__((cleanup(freep))) char *target = NULL((void*)0);
6518 const char *verb = argv[0];
6519 UnitFileChange *changes = NULL((void*)0);
6520 size_t n_changes = 0;
6521 UnitDependency dep;
6522 int r = 0;
6523
6524 if (!argv[1])
6525 return 0;
6526
6527 r = unit_name_mangle_with_suffix(argv[1], arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN, ".target", &target);
6528 if (r < 0)
6529 return log_error_errno(r, "Failed to mangle unit name: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 6529, __func__, "Failed to mangle unit name: %m"
) : -abs(_e); })
;
6530
6531 r = mangle_names(strv_skip(argv, 2), &names);
6532 if (r < 0)
6533 return r;
6534
6535 if (streq(verb, "add-wants")(strcmp((verb),("add-wants")) == 0))
6536 dep = UNIT_WANTS;
6537 else if (streq(verb, "add-requires")(strcmp((verb),("add-requires")) == 0))
6538 dep = UNIT_REQUIRES;
6539 else
6540 assert_not_reached("Unknown verb")do { log_assert_failed_unreachable_realm(LOG_REALM_SYSTEMD, (
"Unknown verb"), "../src/systemctl/systemctl.c", 6540, __PRETTY_FUNCTION__
); } while (0)
;
6541
6542 if (install_client_side()) {
6543 r = unit_file_add_dependency(arg_scope, args_to_flags(), arg_root, names, target, dep, &changes, &n_changes);
6544 unit_file_dump_changes(r, "add dependency on", changes, n_changes, arg_quiet);
6545
6546 if (r > 0)
6547 r = 0;
6548 } else {
6549 _cleanup_(sd_bus_message_unrefp)__attribute__((cleanup(sd_bus_message_unrefp))) sd_bus_message *reply = NULL((void*)0), *m = NULL((void*)0);
6550 _cleanup_(sd_bus_error_free)__attribute__((cleanup(sd_bus_error_free))) sd_bus_error error = SD_BUS_ERROR_NULL((const sd_bus_error) {(((void*)0)), (((void*)0)), 0});
6551 sd_bus *bus;
6552
6553 r = acquire_bus(BUS_MANAGER, &bus);
6554 if (r < 0)
6555 return r;
6556
6557 polkit_agent_open_maybe();
6558
6559 r = sd_bus_message_new_method_call(
6560 bus,
6561 &m,
6562 "org.freedesktop.systemd1",
6563 "/org/freedesktop/systemd1",
6564 "org.freedesktop.systemd1.Manager",
6565 "AddDependencyUnitFiles");
6566 if (r < 0)
6567 return bus_log_create_error(r);
6568
6569 r = sd_bus_message_append_strv(m, names);
6570 if (r < 0)
6571 return bus_log_create_error(r);
6572
6573 r = sd_bus_message_append(m, "ssbb", target, unit_dependency_to_string(dep), arg_runtime, arg_force);
6574 if (r < 0)
6575 return bus_log_create_error(r);
6576
6577 r = sd_bus_call(bus, m, 0, &error, &reply);
6578 if (r < 0)
6579 return log_error_errno(r, "Failed to add dependency: %s", bus_error_message(&error, r))({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 6579, __func__, "Failed to add dependency: %s"
, bus_error_message(&error, r)) : -abs(_e); })
;
6580
6581 r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet, &changes, &n_changes);
6582 if (r < 0)
6583 goto finish;
6584
6585 if (arg_no_reload) {
6586 r = 0;
6587 goto finish;
6588 }
6589
6590 r = daemon_reload(argc, argv, userdata);
6591 }
6592
6593finish:
6594 unit_file_changes_free(changes, n_changes);
6595
6596 return r;
6597}
6598
6599static int preset_all(int argc, char *argv[], void *userdata) {
6600 UnitFileChange *changes = NULL((void*)0);
6601 size_t n_changes = 0;
6602 int r;
6603
6604 if (install_client_side()) {
6605 r = unit_file_preset_all(arg_scope, args_to_flags(), arg_root, arg_preset_mode, &changes, &n_changes);
6606 unit_file_dump_changes(r, "preset", changes, n_changes, arg_quiet);
6607
6608 if (r > 0)
6609 r = 0;
6610 } else {
6611 _cleanup_(sd_bus_error_free)__attribute__((cleanup(sd_bus_error_free))) sd_bus_error error = SD_BUS_ERROR_NULL((const sd_bus_error) {(((void*)0)), (((void*)0)), 0});
6612 _cleanup_(sd_bus_message_unrefp)__attribute__((cleanup(sd_bus_message_unrefp))) sd_bus_message *reply = NULL((void*)0);
6613 sd_bus *bus;
6614
6615 r = acquire_bus(BUS_MANAGER, &bus);
6616 if (r < 0)
6617 return r;
6618
6619 polkit_agent_open_maybe();
6620
6621 r = sd_bus_call_method(
6622 bus,
6623 "org.freedesktop.systemd1",
6624 "/org/freedesktop/systemd1",
6625 "org.freedesktop.systemd1.Manager",
6626 "PresetAllUnitFiles",
6627 &error,
6628 &reply,
6629 "sbb",
6630 unit_file_preset_mode_to_string(arg_preset_mode),
6631 arg_runtime,
6632 arg_force);
6633 if (r < 0)
6634 return log_error_errno(r, "Failed to preset all units: %s", bus_error_message(&error, r))({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 6634, __func__, "Failed to preset all units: %s"
, bus_error_message(&error, r)) : -abs(_e); })
;
6635
6636 r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet, &changes, &n_changes);
6637 if (r < 0)
6638 goto finish;
6639
6640 if (arg_no_reload) {
6641 r = 0;
6642 goto finish;
6643 }
6644
6645 r = daemon_reload(argc, argv, userdata);
6646 }
6647
6648finish:
6649 unit_file_changes_free(changes, n_changes);
6650
6651 return r;
6652}
6653
6654static int show_installation_targets_client_side(const char *name) {
6655 UnitFileChange *changes = NULL((void*)0);
6656 size_t n_changes = 0, i;
6657 UnitFileFlags flags;
6658 char **p;
6659 int r;
6660
6661 p = STRV_MAKE(name)((char**) ((const char*[]) { name, ((void*)0) }));
6662 flags = UNIT_FILE_DRY_RUN |
6663 (arg_runtime ? UNIT_FILE_RUNTIME : 0);
6664
6665 r = unit_file_disable(UNIT_FILE_SYSTEM, flags, NULL((void*)0), p, &changes, &n_changes);
6666 if (r < 0)
6667 return log_error_errno(r, "Failed to get file links for %s: %m", name)({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 6667, __func__, "Failed to get file links for %s: %m"
, name) : -abs(_e); })
;
6668
6669 for (i = 0; i < n_changes; i++)
6670 if (changes[i].type == UNIT_FILE_UNLINK)
6671 printf(" %s\n", changes[i].path);
6672
6673 return 0;
6674}
6675
6676static int show_installation_targets(sd_bus *bus, const char *name) {
6677 _cleanup_(sd_bus_message_unrefp)__attribute__((cleanup(sd_bus_message_unrefp))) sd_bus_message *reply = NULL((void*)0);
6678 _cleanup_(sd_bus_error_free)__attribute__((cleanup(sd_bus_error_free))) sd_bus_error error = SD_BUS_ERROR_NULL((const sd_bus_error) {(((void*)0)), (((void*)0)), 0});
6679 const char *link;
6680 int r;
6681
6682 r = sd_bus_call_method(
6683 bus,
6684 "org.freedesktop.systemd1",
6685 "/org/freedesktop/systemd1",
6686 "org.freedesktop.systemd1.Manager",
6687 "GetUnitFileLinks",
6688 &error,
6689 &reply,
6690 "sb", name, arg_runtime);
6691 if (r < 0)
6692 return log_error_errno(r, "Failed to get unit file links for %s: %s", name, bus_error_message(&error, r))({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 6692, __func__, "Failed to get unit file links for %s: %s"
, name, bus_error_message(&error, r)) : -abs(_e); })
;
6693
6694 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "s");
6695 if (r < 0)
6696 return bus_log_parse_error(r);
6697
6698 while ((r = sd_bus_message_read(reply, "s", &link)) > 0)
6699 printf(" %s\n", link);
6700
6701 if (r < 0)
6702 return bus_log_parse_error(r);
6703
6704 r = sd_bus_message_exit_container(reply);
6705 if (r < 0)
6706 return bus_log_parse_error(r);
6707
6708 return 0;
6709}
6710
6711static int unit_is_enabled(int argc, char *argv[], void *userdata) {
6712 _cleanup_strv_free___attribute__((cleanup(strv_freep))) char **names = NULL((void*)0);
6713 bool_Bool enabled;
6714 char **name;
6715 int r;
6716
6717 r = mangle_names(strv_skip(argv, 1), &names);
6718 if (r < 0)
6719 return r;
6720
6721 r = enable_sysv_units(argv[0], names);
6722 if (r < 0)
6723 return r;
6724
6725 enabled = r > 0;
6726
6727 if (install_client_side()) {
6728 STRV_FOREACH(name, names)for ((name) = (names); (name) && *(name); (name)++) {
6729 UnitFileState state;
6730
6731 r = unit_file_get_state(arg_scope, arg_root, *name, &state);
6732 if (r < 0)
6733 return log_error_errno(r, "Failed to get unit file state for %s: %m", *name)({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 6733, __func__, "Failed to get unit file state for %s: %m"
, *name) : -abs(_e); })
;
6734
6735 if (IN_SET(state,({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){UNIT_FILE_ENABLED, UNIT_FILE_ENABLED_RUNTIME
, UNIT_FILE_STATIC, UNIT_FILE_INDIRECT, UNIT_FILE_GENERATED})
/sizeof(int)]; switch(state) { case UNIT_FILE_ENABLED: case UNIT_FILE_ENABLED_RUNTIME
: case UNIT_FILE_STATIC: case UNIT_FILE_INDIRECT: case UNIT_FILE_GENERATED
: _found = 1; break; default: break; } _found; })
6736 UNIT_FILE_ENABLED,({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){UNIT_FILE_ENABLED, UNIT_FILE_ENABLED_RUNTIME
, UNIT_FILE_STATIC, UNIT_FILE_INDIRECT, UNIT_FILE_GENERATED})
/sizeof(int)]; switch(state) { case UNIT_FILE_ENABLED: case UNIT_FILE_ENABLED_RUNTIME
: case UNIT_FILE_STATIC: case UNIT_FILE_INDIRECT: case UNIT_FILE_GENERATED
: _found = 1; break; default: break; } _found; })
6737 UNIT_FILE_ENABLED_RUNTIME,({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){UNIT_FILE_ENABLED, UNIT_FILE_ENABLED_RUNTIME
, UNIT_FILE_STATIC, UNIT_FILE_INDIRECT, UNIT_FILE_GENERATED})
/sizeof(int)]; switch(state) { case UNIT_FILE_ENABLED: case UNIT_FILE_ENABLED_RUNTIME
: case UNIT_FILE_STATIC: case UNIT_FILE_INDIRECT: case UNIT_FILE_GENERATED
: _found = 1; break; default: break; } _found; })
6738 UNIT_FILE_STATIC,({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){UNIT_FILE_ENABLED, UNIT_FILE_ENABLED_RUNTIME
, UNIT_FILE_STATIC, UNIT_FILE_INDIRECT, UNIT_FILE_GENERATED})
/sizeof(int)]; switch(state) { case UNIT_FILE_ENABLED: case UNIT_FILE_ENABLED_RUNTIME
: case UNIT_FILE_STATIC: case UNIT_FILE_INDIRECT: case UNIT_FILE_GENERATED
: _found = 1; break; default: break; } _found; })
6739 UNIT_FILE_INDIRECT,({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){UNIT_FILE_ENABLED, UNIT_FILE_ENABLED_RUNTIME
, UNIT_FILE_STATIC, UNIT_FILE_INDIRECT, UNIT_FILE_GENERATED})
/sizeof(int)]; switch(state) { case UNIT_FILE_ENABLED: case UNIT_FILE_ENABLED_RUNTIME
: case UNIT_FILE_STATIC: case UNIT_FILE_INDIRECT: case UNIT_FILE_GENERATED
: _found = 1; break; default: break; } _found; })
6740 UNIT_FILE_GENERATED)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){UNIT_FILE_ENABLED, UNIT_FILE_ENABLED_RUNTIME
, UNIT_FILE_STATIC, UNIT_FILE_INDIRECT, UNIT_FILE_GENERATED})
/sizeof(int)]; switch(state) { case UNIT_FILE_ENABLED: case UNIT_FILE_ENABLED_RUNTIME
: case UNIT_FILE_STATIC: case UNIT_FILE_INDIRECT: case UNIT_FILE_GENERATED
: _found = 1; break; default: break; } _found; })
)
6741 enabled = true1;
6742
6743 if (!arg_quiet) {
6744 puts(unit_file_state_to_string(state));
6745 if (arg_full) {
6746 r = show_installation_targets_client_side(*name);
6747 if (r < 0)
6748 return r;
6749 }
6750 }
6751 }
6752
6753 r = 0;
6754 } else {
6755 _cleanup_(sd_bus_error_free)__attribute__((cleanup(sd_bus_error_free))) sd_bus_error error = SD_BUS_ERROR_NULL((const sd_bus_error) {(((void*)0)), (((void*)0)), 0});
6756 sd_bus *bus;
6757
6758 r = acquire_bus(BUS_MANAGER, &bus);
6759 if (r < 0)
6760 return r;
6761
6762 STRV_FOREACH(name, names)for ((name) = (names); (name) && *(name); (name)++) {
6763 _cleanup_(sd_bus_message_unrefp)__attribute__((cleanup(sd_bus_message_unrefp))) sd_bus_message *reply = NULL((void*)0);
6764 const char *s;
6765
6766 r = sd_bus_call_method(
6767 bus,
6768 "org.freedesktop.systemd1",
6769 "/org/freedesktop/systemd1",
6770 "org.freedesktop.systemd1.Manager",
6771 "GetUnitFileState",
6772 &error,
6773 &reply,
6774 "s", *name);
6775 if (r < 0)
6776 return log_error_errno(r, "Failed to get unit file state for %s: %s", *name, bus_error_message(&error, r))({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 6776, __func__, "Failed to get unit file state for %s: %s"
, *name, bus_error_message(&error, r)) : -abs(_e); })
;
6777
6778 r = sd_bus_message_read(reply, "s", &s);
6779 if (r < 0)
6780 return bus_log_parse_error(r);
6781
6782 if (STR_IN_SET(s, "enabled", "enabled-runtime", "static", "indirect", "generated")(!!strv_find((((char**) ((const char*[]) { "enabled", "enabled-runtime"
, "static", "indirect", "generated", ((void*)0) }))), (s)))
)
6783 enabled = true1;
6784
6785 if (!arg_quiet) {
6786 puts(s);
6787 if (arg_full) {
6788 r = show_installation_targets(bus, *name);
6789 if (r < 0)
6790 return r;
6791 }
6792 }
6793 }
6794 }
6795
6796 return enabled ? EXIT_SUCCESS0 : EXIT_FAILURE1;
6797}
6798
6799static int is_system_running(int argc, char *argv[], void *userdata) {
6800 _cleanup_free___attribute__((cleanup(freep))) char *state = NULL((void*)0);
6801 sd_bus *bus;
6802 int r;
6803
6804 if (running_in_chroot() > 0 || (arg_transport == BUS_TRANSPORT_LOCAL && !sd_booted())) {
6805 if (!arg_quiet)
6806 puts("offline");
6807 return EXIT_FAILURE1;
6808 }
6809
6810 r = acquire_bus(BUS_MANAGER, &bus);
6811 if (r < 0)
6812 return r;
6813
6814 r = sd_bus_get_property_string(
6815 bus,
6816 "org.freedesktop.systemd1",
6817 "/org/freedesktop/systemd1",
6818 "org.freedesktop.systemd1.Manager",
6819 "SystemState",
6820 NULL((void*)0),
6821 &state);
6822 if (r < 0) {
6823 if (!arg_quiet)
6824 puts("unknown");
6825 return 0;
6826 }
6827
6828 if (!arg_quiet)
6829 puts(state);
6830
6831 return streq(state, "running")(strcmp((state),("running")) == 0) ? EXIT_SUCCESS0 : EXIT_FAILURE1;
6832}
6833
6834static int create_edit_temp_file(const char *new_path, const char *original_path, char **ret_tmp_fn) {
6835 _cleanup_free___attribute__((cleanup(freep))) char *t = NULL((void*)0);
6836 int r;
6837
6838 assert(new_path)do { if ((__builtin_expect(!!(!(new_path)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("new_path"), "../src/systemctl/systemctl.c"
, 6838, __PRETTY_FUNCTION__); } while (0)
;
46
Taking false branch
47
Loop condition is false. Exiting loop
6839 assert(original_path)do { if ((__builtin_expect(!!(!(original_path)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("original_path"), "../src/systemctl/systemctl.c"
, 6839, __PRETTY_FUNCTION__); } while (0)
;
48
Taking false branch
49
Loop condition is false. Exiting loop
6840 assert(ret_tmp_fn)do { if ((__builtin_expect(!!(!(ret_tmp_fn)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("ret_tmp_fn"), "../src/systemctl/systemctl.c"
, 6840, __PRETTY_FUNCTION__); } while (0)
;
50
Taking false branch
51
Loop condition is false. Exiting loop
6841
6842 r = tempfn_random(new_path, NULL((void*)0), &t);
6843 if (r < 0)
52
Assuming 'r' is < 0
53
Taking true branch
6844 return log_error_errno(r, "Failed to determine temporary filename for \"%s\": %m", new_path)({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 6844, __func__, "Failed to determine temporary filename for \"%s\": %m"
, new_path) : -abs(_e); })
;
54
Assuming the condition is false
55
'?' condition is false
56
Returning without writing to '*ret_tmp_fn'
6845
6846 r = mkdir_parents(new_path, 0755);
6847 if (r < 0)
6848 return log_error_errno(r, "Failed to create directories for \"%s\": %m", new_path)({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 6848, __func__, "Failed to create directories for \"%s\": %m"
, new_path) : -abs(_e); })
;
6849
6850 r = copy_file(original_path, t, 0, 0644, 0, COPY_REFLINK);
6851 if (r == -ENOENT2) {
6852
6853 r = touch(t);
6854 if (r < 0)
6855 return log_error_errno(r, "Failed to create temporary file \"%s\": %m", t)({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 6855, __func__, "Failed to create temporary file \"%s\": %m"
, t) : -abs(_e); })
;
6856
6857 } else if (r < 0)
6858 return log_error_errno(r, "Failed to create temporary file for \"%s\": %m", new_path)({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 6858, __func__, "Failed to create temporary file for \"%s\": %m"
, new_path) : -abs(_e); })
;
6859
6860 *ret_tmp_fn = TAKE_PTR(t)({ typeof(t) _ptr_ = (t); (t) = ((void*)0); _ptr_; });
6861
6862 return 0;
6863}
6864
6865static int get_file_to_edit(
6866 const LookupPaths *paths,
6867 const char *name,
6868 char **ret_path) {
6869
6870 _cleanup_free___attribute__((cleanup(freep))) char *path = NULL((void*)0), *run = NULL((void*)0);
6871
6872 assert(name)do { if ((__builtin_expect(!!(!(name)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("name"), "../src/systemctl/systemctl.c",
6872, __PRETTY_FUNCTION__); } while (0)
;
6873 assert(ret_path)do { if ((__builtin_expect(!!(!(ret_path)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("ret_path"), "../src/systemctl/systemctl.c"
, 6873, __PRETTY_FUNCTION__); } while (0)
;
6874
6875 path = strjoin(paths->persistent_config, "/", name)strjoin_real((paths->persistent_config), "/", name, ((void
*)0))
;
6876 if (!path)
6877 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 6877, __func__)
;
6878
6879 if (arg_runtime) {
6880 run = strjoin(paths->runtime_config, "/", name)strjoin_real((paths->runtime_config), "/", name, ((void*)0
))
;
6881 if (!run)
6882 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 6882, __func__)
;
6883 }
6884
6885 if (arg_runtime) {
6886 if (access(path, F_OK0) >= 0) {
6887 log_error("Refusing to create \"%s\" because it would be overridden by \"%s\" anyway.", run, path)({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 6887, __func__, "Refusing to create \"%s\" because it would be overridden by \"%s\" anyway."
, run, path) : -abs(_e); })
;
6888 return -EEXIST17;
6889 }
6890
6891 *ret_path = TAKE_PTR(run)({ typeof(run) _ptr_ = (run); (run) = ((void*)0); _ptr_; });
6892 } else
6893 *ret_path = TAKE_PTR(path)({ typeof(path) _ptr_ = (path); (path) = ((void*)0); _ptr_; }
)
;
6894
6895 return 0;
6896}
6897
6898static int unit_file_create_new(
6899 const LookupPaths *paths,
6900 const char *unit_name,
6901 const char *suffix,
6902 char **ret_new_path,
6903 char **ret_tmp_path) {
6904
6905 char *tmp_new_path, *tmp_tmp_path, *ending;
33
'tmp_tmp_path' declared without an initial value
6906 int r;
6907
6908 assert(unit_name)do { if ((__builtin_expect(!!(!(unit_name)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("unit_name"), "../src/systemctl/systemctl.c"
, 6908, __PRETTY_FUNCTION__); } while (0)
;
34
Taking false branch
35
Loop condition is false. Exiting loop
6909 assert(ret_new_path)do { if ((__builtin_expect(!!(!(ret_new_path)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("ret_new_path"), "../src/systemctl/systemctl.c"
, 6909, __PRETTY_FUNCTION__); } while (0)
;
36
Taking false branch
37
Loop condition is false. Exiting loop
6910 assert(ret_tmp_path)do { if ((__builtin_expect(!!(!(ret_tmp_path)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("ret_tmp_path"), "../src/systemctl/systemctl.c"
, 6910, __PRETTY_FUNCTION__); } while (0)
;
38
Taking false branch
39
Loop condition is false. Exiting loop
6911
6912 ending = strjoina(unit_name, suffix)({ const char *_appendees_[] = { unit_name, suffix }; char *_d_
, *_p_; size_t _len_ = 0; size_t _i_; for (_i_ = 0; _i_ < __extension__
(__builtin_choose_expr( !__builtin_types_compatible_p(typeof
(_appendees_), typeof(&*(_appendees_))), sizeof(_appendees_
)/sizeof((_appendees_)[0]), ((void)0))) && _appendees_
[_i_]; _i_++) _len_ += strlen(_appendees_[_i_]); _p_ = _d_ = __builtin_alloca
(_len_ + 1); for (_i_ = 0; _i_ < __extension__ (__builtin_choose_expr
( !__builtin_types_compatible_p(typeof(_appendees_), typeof(&
*(_appendees_))), sizeof(_appendees_)/sizeof((_appendees_)[0]
), ((void)0))) && _appendees_[_i_]; _i_++) _p_ = stpcpy
(_p_, _appendees_[_i_]); *_p_ = 0; _d_; })
;
40
Loop condition is true. Entering loop body
41
Loop condition is false. Execution continues on line 6912
42
Loop condition is true. Entering loop body
43
Loop condition is false. Execution continues on line 6912
6913 r = get_file_to_edit(paths, ending, &tmp_new_path);
6914 if (r
43.1
'r' is >= 0
< 0)
44
Taking false branch
6915 return r;
6916
6917 r = create_edit_temp_file(tmp_new_path, tmp_new_path, &tmp_tmp_path);
45
Calling 'create_edit_temp_file'
57
Returning from 'create_edit_temp_file'
6918 if (r < 0) {
58
Assuming 'r' is >= 0
59
Taking false branch
6919 free(tmp_new_path);
6920 return r;
6921 }
6922
6923 *ret_new_path = tmp_new_path;
6924 *ret_tmp_path = tmp_tmp_path;
60
Assigned value is garbage or undefined
6925
6926 return 0;
6927}
6928
6929static int unit_file_create_copy(
6930 const LookupPaths *paths,
6931 const char *unit_name,
6932 const char *fragment_path,
6933 char **ret_new_path,
6934 char **ret_tmp_path) {
6935
6936 char *tmp_new_path, *tmp_tmp_path;
6937 int r;
6938
6939 assert(fragment_path)do { if ((__builtin_expect(!!(!(fragment_path)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("fragment_path"), "../src/systemctl/systemctl.c"
, 6939, __PRETTY_FUNCTION__); } while (0)
;
6940 assert(unit_name)do { if ((__builtin_expect(!!(!(unit_name)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("unit_name"), "../src/systemctl/systemctl.c"
, 6940, __PRETTY_FUNCTION__); } while (0)
;
6941 assert(ret_new_path)do { if ((__builtin_expect(!!(!(ret_new_path)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("ret_new_path"), "../src/systemctl/systemctl.c"
, 6941, __PRETTY_FUNCTION__); } while (0)
;
6942 assert(ret_tmp_path)do { if ((__builtin_expect(!!(!(ret_tmp_path)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("ret_tmp_path"), "../src/systemctl/systemctl.c"
, 6942, __PRETTY_FUNCTION__); } while (0)
;
6943
6944 r = get_file_to_edit(paths, unit_name, &tmp_new_path);
6945 if (r < 0)
6946 return r;
6947
6948 if (!path_equal(fragment_path, tmp_new_path) && access(tmp_new_path, F_OK0) == 0) {
6949 char response;
6950
6951 r = ask_char(&response, "yn", "\"%s\" already exists. Overwrite with \"%s\"? [(y)es, (n)o] ", tmp_new_path, fragment_path);
6952 if (r < 0) {
6953 free(tmp_new_path);
6954 return r;
6955 }
6956 if (response != 'y') {
6957 log_warning("%s ignored", unit_name)({ int _level = (((4))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 6957, __func__, "%s ignored"
, unit_name) : -abs(_e); })
;
6958 free(tmp_new_path);
6959 return -EKEYREJECTED129;
6960 }
6961 }
6962
6963 r = create_edit_temp_file(tmp_new_path, fragment_path, &tmp_tmp_path);
6964 if (r < 0) {
6965 free(tmp_new_path);
6966 return r;
6967 }
6968
6969 *ret_new_path = tmp_new_path;
6970 *ret_tmp_path = tmp_tmp_path;
6971
6972 return 0;
6973}
6974
6975static int run_editor(char **paths) {
6976 int r;
6977
6978 assert(paths)do { if ((__builtin_expect(!!(!(paths)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("paths"), "../src/systemctl/systemctl.c"
, 6978, __PRETTY_FUNCTION__); } while (0)
;
6979
6980 r = safe_fork("(editor)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_LOG|FORK_WAIT, NULL((void*)0));
6981 if (r < 0)
6982 return r;
6983 if (r == 0) {
6984 const char **args;
6985 char *editor, **editor_args = NULL((void*)0);
6986 char **tmp_path, **original_path, *p;
6987 size_t n_editor_args = 0, i = 1;
6988 size_t argc;
6989
6990 argc = strv_length(paths)/2 + 1;
6991
6992 /* SYSTEMD_EDITOR takes precedence over EDITOR which takes precedence over VISUAL
6993 * If neither SYSTEMD_EDITOR nor EDITOR nor VISUAL are present,
6994 * we try to execute well known editors
6995 */
6996 editor = getenv("SYSTEMD_EDITOR");
6997 if (!editor)
6998 editor = getenv("EDITOR");
6999 if (!editor)
7000 editor = getenv("VISUAL");
7001
7002 if (!isempty(editor)) {
7003 editor_args = strv_split(editor, WHITESPACE" \t\n\r");
7004 if (!editor_args) {
7005 (void) log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 7005, __func__)
;
7006 _exit(EXIT_FAILURE1);
7007 }
7008 n_editor_args = strv_length(editor_args);
7009 argc += n_editor_args - 1;
7010 }
7011 args = newa(const char*, argc + 1)({ do { if ((__builtin_expect(!!(!(!size_multiply_overflow(sizeof
(const char*), argc + 1))),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD
, ("!size_multiply_overflow(sizeof(const char*), argc + 1)"),
"../src/systemctl/systemctl.c", 7011, __PRETTY_FUNCTION__); }
while (0); (const char**) __builtin_alloca (sizeof(const char
*)*(argc + 1)); })
;
7012
7013 if (n_editor_args > 0) {
7014 args[0] = editor_args[0];
7015 for (; i < n_editor_args; i++)
7016 args[i] = editor_args[i];
7017 }
7018
7019 STRV_FOREACH_PAIR(original_path, tmp_path, paths)for ((original_path) = (paths), (tmp_path) = (original_path+1
); (original_path) && *(original_path) && *(tmp_path
); (original_path) += 2, (tmp_path) = (original_path + 1))
{
7020 args[i] = *tmp_path;
7021 i++;
7022 }
7023 args[i] = NULL((void*)0);
7024
7025 if (n_editor_args > 0)
7026 execvp(args[0], (char* const*) args);
7027
7028 FOREACH_STRING(p, "editor", "nano", "vim", "vi")for (char **_l = ({ char **_ll = ((char**) ((const char*[]) {
"editor", "nano", "vim", "vi", ((void*)0) })); p = _ll ? _ll
[0] : ((void*)0); _ll; }); _l && *_l; p = ({ _l ++; _l
[0]; }))
{
7029 args[0] = p;
7030 execvp(p, (char* const*) args);
7031 /* We do not fail if the editor doesn't exist
7032 * because we want to try each one of them before
7033 * failing.
7034 */
7035 if (errno(*__errno_location ()) != ENOENT2) {
7036 log_error_errno(errno, "Failed to execute %s: %m", editor)({ int _level = ((3)), _e = (((*__errno_location ()))), _realm
= (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >=
((_level) & 0x07)) ? log_internal_realm(((_realm) <<
10 | (_level)), _e, "../src/systemctl/systemctl.c", 7036, __func__
, "Failed to execute %s: %m", editor) : -abs(_e); })
;
7037 _exit(EXIT_FAILURE1);
7038 }
7039 }
7040
7041 log_error("Cannot edit unit(s), no editor available. Please set either $SYSTEMD_EDITOR, $EDITOR or $VISUAL.")({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 7041, __func__, "Cannot edit unit(s), no editor available. Please set either $SYSTEMD_EDITOR, $EDITOR or $VISUAL."
) : -abs(_e); })
;
7042 _exit(EXIT_FAILURE1);
7043 }
7044
7045 return 0;
7046}
7047
7048static int find_paths_to_edit(sd_bus *bus, char **names, char ***paths) {
7049 _cleanup_(lookup_paths_free)__attribute__((cleanup(lookup_paths_free))) LookupPaths lp = {};
7050 char **name;
7051 int r;
7052
7053 assert(names)do { if ((__builtin_expect(!!(!(names)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("names"), "../src/systemctl/systemctl.c"
, 7053, __PRETTY_FUNCTION__); } while (0)
;
15
Taking false branch
16
Loop condition is false. Exiting loop
7054 assert(paths)do { if ((__builtin_expect(!!(!(paths)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("paths"), "../src/systemctl/systemctl.c"
, 7054, __PRETTY_FUNCTION__); } while (0)
;
17
Taking false branch
18
Loop condition is false. Exiting loop
7055
7056 r = lookup_paths_init(&lp, arg_scope, 0, arg_root);
7057 if (r < 0)
19
Assuming 'r' is >= 0
20
Taking false branch
7058 return r;
7059
7060 STRV_FOREACH(name, names)for ((name) = (names); (name) && *(name); (name)++) {
21
Loop condition is true. Entering loop body
7061 _cleanup_free___attribute__((cleanup(freep))) char *path = NULL((void*)0), *new_path = NULL((void*)0), *tmp_path = NULL((void*)0), *tmp_name = NULL((void*)0);
7062 const char *unit_name;
7063
7064 r = unit_find_paths(bus, *name, &lp, &path, NULL((void*)0));
7065 if (r < 0)
22
Assuming 'r' is >= 0
23
Taking false branch
7066 return r;
7067
7068 if (r == 0) {
24
Assuming 'r' is equal to 0
25
Taking true branch
7069 assert(!path)do { if ((__builtin_expect(!!(!(!path)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("!path"), "../src/systemctl/systemctl.c"
, 7069, __PRETTY_FUNCTION__); } while (0)
;
26
Taking false branch
27
Loop condition is false. Exiting loop
7070
7071 if (!arg_force) {
28
Assuming 'arg_force' is not equal to 0
29
Taking false branch
7072 log_error("Run 'systemctl edit%s --force %s' to create a new unit.",({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 7075, __func__, "Run 'systemctl edit%s --force %s' to create a new unit."
, arg_scope == UNIT_FILE_GLOBAL ? " --global" : arg_scope == UNIT_FILE_USER
? " --user" : "", *name) : -abs(_e); })
7073 arg_scope == UNIT_FILE_GLOBAL ? " --global" :({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 7075, __func__, "Run 'systemctl edit%s --force %s' to create a new unit."
, arg_scope == UNIT_FILE_GLOBAL ? " --global" : arg_scope == UNIT_FILE_USER
? " --user" : "", *name) : -abs(_e); })
7074 arg_scope == UNIT_FILE_USER ? " --user" : "",({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 7075, __func__, "Run 'systemctl edit%s --force %s' to create a new unit."
, arg_scope == UNIT_FILE_GLOBAL ? " --global" : arg_scope == UNIT_FILE_USER
? " --user" : "", *name) : -abs(_e); })
7075 *name)({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 7075, __func__, "Run 'systemctl edit%s --force %s' to create a new unit."
, arg_scope == UNIT_FILE_GLOBAL ? " --global" : arg_scope == UNIT_FILE_USER
? " --user" : "", *name) : -abs(_e); })
;
7076 return -ENOENT2;
7077 }
7078
7079 /* Create a new unit from scratch */
7080 unit_name = *name;
7081 r = unit_file_create_new(&lp, unit_name,
32
Calling 'unit_file_create_new'
7082 arg_full ? NULL((void*)0) : ".d/override.conf",
30
Assuming 'arg_full' is true
31
'?' condition is true
7083 &new_path, &tmp_path);
7084 } else {
7085 assert(path)do { if ((__builtin_expect(!!(!(path)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("path"), "../src/systemctl/systemctl.c",
7085, __PRETTY_FUNCTION__); } while (0)
;
7086
7087 unit_name = basename(path);
7088 /* We follow unit aliases, but we need to propagate the instance */
7089 if (unit_name_is_valid(*name, UNIT_NAME_INSTANCE) &&
7090 unit_name_is_valid(unit_name, UNIT_NAME_TEMPLATE)) {
7091 _cleanup_free___attribute__((cleanup(freep))) char *instance = NULL((void*)0);
7092
7093 r = unit_name_to_instance(*name, &instance);
7094 if (r < 0)
7095 return r;
7096
7097 r = unit_name_replace_instance(unit_name, instance, &tmp_name);
7098 if (r < 0)
7099 return r;
7100
7101 unit_name = tmp_name;
7102 }
7103
7104 if (arg_full)
7105 r = unit_file_create_copy(&lp, unit_name, path, &new_path, &tmp_path);
7106 else
7107 r = unit_file_create_new(&lp, unit_name, ".d/override.conf", &new_path, &tmp_path);
7108 }
7109 if (r < 0)
7110 return r;
7111
7112 r = strv_push_pair(paths, new_path, tmp_path);
7113 if (r < 0)
7114 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 7114, __func__)
;
7115 new_path = tmp_path = NULL((void*)0);
7116 }
7117
7118 return 0;
7119}
7120
7121static int edit(int argc, char *argv[], void *userdata) {
7122 _cleanup_strv_free___attribute__((cleanup(strv_freep))) char **names = NULL((void*)0);
7123 _cleanup_strv_free___attribute__((cleanup(strv_freep))) char **paths = NULL((void*)0);
7124 char **original, **tmp;
7125 sd_bus *bus;
7126 int r;
7127
7128 if (!on_tty()) {
1
Assuming the condition is false
2
Taking false branch
7129 log_error("Cannot edit units if not on a tty.")({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 7129, __func__, "Cannot edit units if not on a tty."
) : -abs(_e); })
;
7130 return -EINVAL22;
7131 }
7132
7133 if (arg_transport != BUS_TRANSPORT_LOCAL) {
3
Assuming 'arg_transport' is equal to BUS_TRANSPORT_LOCAL
4
Taking false branch
7134 log_error("Cannot edit units remotely.")({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 7134, __func__, "Cannot edit units remotely."
) : -abs(_e); })
;
7135 return -EINVAL22;
7136 }
7137
7138 r = acquire_bus(BUS_MANAGER, &bus);
7139 if (r < 0)
5
Assuming 'r' is >= 0
6
Taking false branch
7140 return r;
7141
7142 r = expand_names(bus, strv_skip(argv, 1), NULL((void*)0), &names);
7143 if (r < 0)
7
Assuming 'r' is >= 0
8
Taking false branch
7144 return log_error_errno(r, "Failed to expand names: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 7144, __func__, "Failed to expand names: %m"
) : -abs(_e); })
;
7145
7146 STRV_FOREACH(tmp, names)for ((tmp) = (names); (tmp) && *(tmp); (tmp)++) {
9
Assuming 'tmp' is non-null
10
Loop condition is true. Entering loop body
13
Loop condition is false. Execution continues on line 7157
7147 r = unit_is_masked(bus, *tmp);
7148 if (r
10.1
'r' is >= 0
< 0)
11
Taking false branch
7149 return r;
7150
7151 if (r
11.1
'r' is <= 0
> 0) {
12
Taking false branch
7152 log_error("Cannot edit %s: unit is masked.", *tmp)({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 7152, __func__, "Cannot edit %s: unit is masked."
, *tmp) : -abs(_e); })
;
7153 return -EINVAL22;
7154 }
7155 }
7156
7157 r = find_paths_to_edit(bus, names, &paths);
14
Calling 'find_paths_to_edit'
7158 if (r < 0)
7159 return r;
7160
7161 if (strv_isempty(paths))
7162 return -ENOENT2;
7163
7164 r = run_editor(paths);
7165 if (r < 0)
7166 goto end;
7167
7168 STRV_FOREACH_PAIR(original, tmp, paths)for ((original) = (paths), (tmp) = (original+1); (original) &&
*(original) && *(tmp); (original) += 2, (tmp) = (original
+ 1))
{
7169 /* If the temporary file is empty we ignore it.
7170 * This allows the user to cancel the modification.
7171 */
7172 if (null_or_empty_path(*tmp)) {
7173 log_warning("Editing \"%s\" canceled: temporary file is empty.", *original)({ int _level = (((4))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 7173, __func__, "Editing \"%s\" canceled: temporary file is empty."
, *original) : -abs(_e); })
;
7174 continue;
7175 }
7176
7177 r = rename(*tmp, *original);
7178 if (r < 0) {
7179 r = log_error_errno(errno, "Failed to rename \"%s\" to \"%s\": %m", *tmp, *original)({ int _level = ((3)), _e = (((*__errno_location ()))), _realm
= (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >=
((_level) & 0x07)) ? log_internal_realm(((_realm) <<
10 | (_level)), _e, "../src/systemctl/systemctl.c", 7179, __func__
, "Failed to rename \"%s\" to \"%s\": %m", *tmp, *original) :
-abs(_e); })
;
7180 goto end;
7181 }
7182 }
7183
7184 r = 0;
7185
7186 if (!arg_no_reload && !install_client_side())
7187 r = daemon_reload(argc, argv, userdata);
7188
7189end:
7190 STRV_FOREACH_PAIR(original, tmp, paths)for ((original) = (paths), (tmp) = (original+1); (original) &&
*(original) && *(tmp); (original) += 2, (tmp) = (original
+ 1))
{
7191 (void) unlink(*tmp);
7192
7193 /* Removing empty dropin dirs */
7194 if (!arg_full) {
7195 _cleanup_free___attribute__((cleanup(freep))) char *dir;
7196
7197 dir = dirname_malloc(*original);
7198 if (!dir)
7199 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 7199, __func__)
;
7200
7201 /* no need to check if the dir is empty, rmdir
7202 * does nothing if it is not the case.
7203 */
7204 (void) rmdir(dir);
7205 }
7206 }
7207
7208 return r;
7209}
7210
7211static void systemctl_help(void) {
7212 (void) pager_open(arg_no_pager, false0);
7213
7214 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
7215 "Query or send control commands to the systemd manager.\n\n"
7216 " -h --help Show this help\n"
7217 " --version Show package version\n"
7218 " --system Connect to system manager\n"
7219 " --user Connect to user service manager\n"
7220 " -H --host=[USER@]HOST\n"
7221 " Operate on remote host\n"
7222 " -M --machine=CONTAINER\n"
7223 " Operate on local container\n"
7224 " -t --type=TYPE List units of a particular type\n"
7225 " --state=STATE List units with particular LOAD or SUB or ACTIVE state\n"
7226 " -p --property=NAME Show only properties by this name\n"
7227 " -a --all Show all properties/all units currently in memory,\n"
7228 " including dead/empty ones. To list all units installed on\n"
7229 " the system, use the 'list-unit-files' command instead.\n"
7230 " --failed Same as --state=failed\n"
7231 " -l --full Don't ellipsize unit names on output\n"
7232 " -r --recursive Show unit list of host and local containers\n"
7233 " --reverse Show reverse dependencies with 'list-dependencies'\n"
7234 " --job-mode=MODE Specify how to deal with already queued jobs, when\n"
7235 " queueing a new job\n"
7236 " -T --show-transaction\n"
7237 " When enqueuing a unit job, show full transaction\n"
7238 " --show-types When showing sockets, explicitly show their type\n"
7239 " --value When showing properties, only print the value\n"
7240 " -i --ignore-inhibitors\n"
7241 " When shutting down or sleeping, ignore inhibitors\n"
7242 " --kill-who=WHO Who to send signal to\n"
7243 " -s --signal=SIGNAL Which signal to send\n"
7244 " --now Start or stop unit in addition to enabling or disabling it\n"
7245 " --dry-run Only print what would be done\n"
7246 " -q --quiet Suppress output\n"
7247 " --wait For (re)start, wait until service stopped again\n"
7248 " --no-block Do not wait until operation finished\n"
7249 " --no-wall Don't send wall message before halt/power-off/reboot\n"
7250 " --no-reload Don't reload daemon after en-/dis-abling unit files\n"
7251 " --no-legend Do not print a legend (column headers and hints)\n"
7252 " --no-pager Do not pipe output into a pager\n"
7253 " --no-ask-password\n"
7254 " Do not ask for system passwords\n"
7255 " --global Enable/disable/mask unit files globally\n"
7256 " --runtime Enable/disable/mask unit files temporarily until next\n"
7257 " reboot\n"
7258 " -f --force When enabling unit files, override existing symlinks\n"
7259 " When shutting down, execute action immediately\n"
7260 " --preset-mode= Apply only enable, only disable, or all presets\n"
7261 " --root=PATH Enable/disable/mask unit files in the specified root\n"
7262 " directory\n"
7263 " -n --lines=INTEGER Number of journal entries to show\n"
7264 " -o --output=STRING Change journal output mode (short, short-precise,\n"
7265 " short-iso, short-iso-precise, short-full,\n"
7266 " short-monotonic, short-unix,\n"
7267 " verbose, export, json, json-pretty, json-sse, cat)\n"
7268 " --firmware-setup Tell the firmware to show the setup menu on next boot\n"
7269 " --plain Print unit dependencies as a list instead of a tree\n\n"
7270 "Unit Commands:\n"
7271 " list-units [PATTERN...] List units currently in memory\n"
7272 " list-sockets [PATTERN...] List socket units currently in memory,\n"
7273 " ordered by address\n"
7274 " list-timers [PATTERN...] List timer units currently in memory,\n"
7275 " ordered by next elapse\n"
7276 " start UNIT... Start (activate) one or more units\n"
7277 " stop UNIT... Stop (deactivate) one or more units\n"
7278 " reload UNIT... Reload one or more units\n"
7279 " restart UNIT... Start or restart one or more units\n"
7280 " try-restart UNIT... Restart one or more units if active\n"
7281 " reload-or-restart UNIT... Reload one or more units if possible,\n"
7282 " otherwise start or restart\n"
7283 " try-reload-or-restart UNIT... If active, reload one or more units,\n"
7284 " if supported, otherwise restart\n"
7285 " isolate UNIT Start one unit and stop all others\n"
7286 " kill UNIT... Send signal to processes of a unit\n"
7287 " freeze PATTERN... Freeze execution of unit processes\n"
7288 " thaw PATTERN... Resume execution of a frozen unit\n"
7289 " is-active PATTERN... Check whether units are active\n"
7290 " is-failed PATTERN... Check whether units are failed\n"
7291 " status [PATTERN...|PID...] Show runtime status of one or more units\n"
7292 " show [PATTERN...|JOB...] Show properties of one or more\n"
7293 " units/jobs or the manager\n"
7294 " cat PATTERN... Show files and drop-ins of specified units\n"
7295 " set-property UNIT PROPERTY=VALUE... Sets one or more properties of a unit\n"
7296 " help PATTERN...|PID... Show manual for one or more units\n"
7297 " reset-failed [PATTERN...] Reset failed state for all, one, or more\n"
7298 " units\n"
7299 " list-dependencies [UNIT] Recursively show units which are required\n"
7300 " or wanted by this unit or by which this\n"
7301 " unit is required or wanted\n\n"
7302 "Unit File Commands:\n"
7303 " list-unit-files [PATTERN...] List installed unit files\n"
7304 " enable [UNIT...|PATH...] Enable one or more unit files\n"
7305 " disable UNIT... Disable one or more unit files\n"
7306 " reenable UNIT... Reenable one or more unit files\n"
7307 " preset UNIT... Enable/disable one or more unit files\n"
7308 " based on preset configuration\n"
7309 " preset-all Enable/disable all unit files based on\n"
7310 " preset configuration\n"
7311 " is-enabled UNIT... Check whether unit files are enabled\n"
7312 " mask UNIT... Mask one or more units\n"
7313 " unmask UNIT... Unmask one or more units\n"
7314 " link PATH... Link one or more units files into\n"
7315 " the search path\n"
7316 " revert UNIT... Revert one or more unit files to vendor\n"
7317 " version\n"
7318 " add-wants TARGET UNIT... Add 'Wants' dependency for the target\n"
7319 " on specified one or more units\n"
7320 " add-requires TARGET UNIT... Add 'Requires' dependency for the target\n"
7321 " on specified one or more units\n"
7322 " edit UNIT... Edit one or more unit files\n"
7323 " get-default Get the name of the default target\n"
7324 " set-default TARGET Set the default target\n\n"
7325 "Machine Commands:\n"
7326 " list-machines [PATTERN...] List local containers and host\n\n"
7327 "Job Commands:\n"
7328 " list-jobs [PATTERN...] List jobs\n"
7329 " cancel [JOB...] Cancel all, one, or more jobs\n\n"
7330 "Environment Commands:\n"
7331 " show-environment Dump environment\n"
7332 " set-environment VARIABLE=VALUE... Set one or more environment variables\n"
7333 " unset-environment VARIABLE... Unset one or more environment variables\n"
7334 " import-environment [VARIABLE...] Import all or some environment variables\n\n"
7335 "Manager Lifecycle Commands:\n"
7336 " daemon-reload Reload systemd manager configuration\n"
7337 " daemon-reexec Reexecute systemd manager\n\n"
7338 "System Commands:\n"
7339 " is-system-running Check whether system is fully running\n"
7340 " default Enter system default mode\n"
7341 " rescue Enter system rescue mode\n"
7342 " emergency Enter system emergency mode\n"
7343 " halt Shut down and halt the system\n"
7344 " poweroff Shut down and power-off the system\n"
7345 " reboot [ARG] Shut down and reboot the system\n"
7346 " kexec Shut down and reboot the system with kexec\n"
7347 " exit [EXIT_CODE] Request user instance or container exit\n"
7348 " switch-root ROOT [INIT] Change to a different root file system\n"
7349 " suspend Suspend the system\n"
7350 " hibernate Hibernate the system\n"
7351 " hybrid-sleep Hibernate and suspend the system\n"
7352 " suspend-then-hibernate Suspend the system, wake after a period of\n"
7353 " time and put it into hibernate\n",
7354 program_invocation_short_name);
7355}
7356
7357static void halt_help(void) {
7358 printf("%s [OPTIONS...]%s\n\n"
7359 "%s the system.\n\n"
7360 " --help Show this help\n"
7361 " --halt Halt the machine\n"
7362 " -p --poweroff Switch off the machine\n"
7363 " --reboot Reboot the machine\n"
7364 " -f --force Force immediate halt/power-off/reboot\n"
7365 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
7366 " -d --no-wtmp Don't write wtmp record\n"
7367 " --no-wall Don't send wall message before halt/power-off/reboot\n",
7368 program_invocation_short_name,
7369 arg_action == ACTION_REBOOT ? " [ARG]" : "",
7370 arg_action == ACTION_REBOOT ? "Reboot" :
7371 arg_action == ACTION_POWEROFF ? "Power off" :
7372 "Halt");
7373}
7374
7375static void shutdown_help(void) {
7376 printf("%s [OPTIONS...] [TIME] [WALL...]\n\n"
7377 "Shut down the system.\n\n"
7378 " --help Show this help\n"
7379 " -H --halt Halt the machine\n"
7380 " -P --poweroff Power-off the machine\n"
7381 " -r --reboot Reboot the machine\n"
7382 " -h Equivalent to --poweroff, overridden by --halt\n"
7383 " -k Don't halt/power-off/reboot, just send warnings\n"
7384 " --no-wall Don't send wall message before halt/power-off/reboot\n"
7385 " -c Cancel a pending shutdown\n",
7386 program_invocation_short_name);
7387}
7388
7389static void telinit_help(void) {
7390 printf("%s [OPTIONS...] {COMMAND}\n\n"
7391 "Send control commands to the init daemon.\n\n"
7392 " --help Show this help\n"
7393 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
7394 "Commands:\n"
7395 " 0 Power-off the machine\n"
7396 " 6 Reboot the machine\n"
7397 " 2, 3, 4, 5 Start runlevelX.target unit\n"
7398 " 1, s, S Enter rescue mode\n"
7399 " q, Q Reload init daemon configuration\n"
7400 " u, U Reexecute init daemon\n",
7401 program_invocation_short_name);
7402}
7403
7404static void runlevel_help(void) {
7405 printf("%s [OPTIONS...]\n\n"
7406 "Prints the previous and current runlevel of the init system.\n\n"
7407 " --help Show this help\n",
7408 program_invocation_short_name);
7409}
7410
7411static void help_types(void) {
7412 if (!arg_no_legend)
7413 puts("Available unit types:");
7414
7415 DUMP_STRING_TABLE(unit_type, UnitType, _UNIT_TYPE_MAX)do { UnitType _k; flockfile(stdout); for (_k = 0; _k < (_UNIT_TYPE_MAX
); _k++) { const char *_t; _t = unit_type_to_string(_k); if (
!_t) continue; fputs_unlocked(_t, stdout); fputc_unlocked('\n'
, stdout); } funlockfile(stdout); } while(0)
;
7416}
7417
7418static void help_states(void) {
7419 if (!arg_no_legend)
7420 puts("Available unit load states:");
7421 DUMP_STRING_TABLE(unit_load_state, UnitLoadState, _UNIT_LOAD_STATE_MAX)do { UnitLoadState _k; flockfile(stdout); for (_k = 0; _k <
(_UNIT_LOAD_STATE_MAX); _k++) { const char *_t; _t = unit_load_state_to_string
(_k); if (!_t) continue; fputs_unlocked(_t, stdout); fputc_unlocked
('\n', stdout); } funlockfile(stdout); } while(0)
;
7422
7423 if (!arg_no_legend)
7424 puts("\nAvailable unit active states:");
7425 DUMP_STRING_TABLE(unit_active_state, UnitActiveState, _UNIT_ACTIVE_STATE_MAX)do { UnitActiveState _k; flockfile(stdout); for (_k = 0; _k <
(_UNIT_ACTIVE_STATE_MAX); _k++) { const char *_t; _t = unit_active_state_to_string
(_k); if (!_t) continue; fputs_unlocked(_t, stdout); fputc_unlocked
('\n', stdout); } funlockfile(stdout); } while(0)
;
7426
7427 if (!arg_no_legend)
7428 puts("\nAvailable automount unit substates:");
7429 DUMP_STRING_TABLE(automount_state, AutomountState, _AUTOMOUNT_STATE_MAX)do { AutomountState _k; flockfile(stdout); for (_k = 0; _k <
(_AUTOMOUNT_STATE_MAX); _k++) { const char *_t; _t = automount_state_to_string
(_k); if (!_t) continue; fputs_unlocked(_t, stdout); fputc_unlocked
('\n', stdout); } funlockfile(stdout); } while(0)
;
7430
7431 if (!arg_no_legend)
7432 puts("\nAvailable device unit substates:");
7433 DUMP_STRING_TABLE(device_state, DeviceState, _DEVICE_STATE_MAX)do { DeviceState _k; flockfile(stdout); for (_k = 0; _k < (
_DEVICE_STATE_MAX); _k++) { const char *_t; _t = device_state_to_string
(_k); if (!_t) continue; fputs_unlocked(_t, stdout); fputc_unlocked
('\n', stdout); } funlockfile(stdout); } while(0)
;
7434
7435 if (!arg_no_legend)
7436 puts("\nAvailable mount unit substates:");
7437 DUMP_STRING_TABLE(mount_state, MountState, _MOUNT_STATE_MAX)do { MountState _k; flockfile(stdout); for (_k = 0; _k < (
_MOUNT_STATE_MAX); _k++) { const char *_t; _t = mount_state_to_string
(_k); if (!_t) continue; fputs_unlocked(_t, stdout); fputc_unlocked
('\n', stdout); } funlockfile(stdout); } while(0)
;
7438
7439 if (!arg_no_legend)
7440 puts("\nAvailable path unit substates:");
7441 DUMP_STRING_TABLE(path_state, PathState, _PATH_STATE_MAX)do { PathState _k; flockfile(stdout); for (_k = 0; _k < (_PATH_STATE_MAX
); _k++) { const char *_t; _t = path_state_to_string(_k); if (
!_t) continue; fputs_unlocked(_t, stdout); fputc_unlocked('\n'
, stdout); } funlockfile(stdout); } while(0)
;
7442
7443 if (!arg_no_legend)
7444 puts("\nAvailable scope unit substates:");
7445 DUMP_STRING_TABLE(scope_state, ScopeState, _SCOPE_STATE_MAX)do { ScopeState _k; flockfile(stdout); for (_k = 0; _k < (
_SCOPE_STATE_MAX); _k++) { const char *_t; _t = scope_state_to_string
(_k); if (!_t) continue; fputs_unlocked(_t, stdout); fputc_unlocked
('\n', stdout); } funlockfile(stdout); } while(0)
;
7446
7447 if (!arg_no_legend)
7448 puts("\nAvailable service unit substates:");
7449 DUMP_STRING_TABLE(service_state, ServiceState, _SERVICE_STATE_MAX)do { ServiceState _k; flockfile(stdout); for (_k = 0; _k <
(_SERVICE_STATE_MAX); _k++) { const char *_t; _t = service_state_to_string
(_k); if (!_t) continue; fputs_unlocked(_t, stdout); fputc_unlocked
('\n', stdout); } funlockfile(stdout); } while(0)
;
7450
7451 if (!arg_no_legend)
7452 puts("\nAvailable slice unit substates:");
7453 DUMP_STRING_TABLE(slice_state, SliceState, _SLICE_STATE_MAX)do { SliceState _k; flockfile(stdout); for (_k = 0; _k < (
_SLICE_STATE_MAX); _k++) { const char *_t; _t = slice_state_to_string
(_k); if (!_t) continue; fputs_unlocked(_t, stdout); fputc_unlocked
('\n', stdout); } funlockfile(stdout); } while(0)
;
7454
7455 if (!arg_no_legend)
7456 puts("\nAvailable socket unit substates:");
7457 DUMP_STRING_TABLE(socket_state, SocketState, _SOCKET_STATE_MAX)do { SocketState _k; flockfile(stdout); for (_k = 0; _k < (
_SOCKET_STATE_MAX); _k++) { const char *_t; _t = socket_state_to_string
(_k); if (!_t) continue; fputs_unlocked(_t, stdout); fputc_unlocked
('\n', stdout); } funlockfile(stdout); } while(0)
;
7458
7459 if (!arg_no_legend)
7460 puts("\nAvailable swap unit substates:");
7461 DUMP_STRING_TABLE(swap_state, SwapState, _SWAP_STATE_MAX)do { SwapState _k; flockfile(stdout); for (_k = 0; _k < (_SWAP_STATE_MAX
); _k++) { const char *_t; _t = swap_state_to_string(_k); if (
!_t) continue; fputs_unlocked(_t, stdout); fputc_unlocked('\n'
, stdout); } funlockfile(stdout); } while(0)
;
7462
7463 if (!arg_no_legend)
7464 puts("\nAvailable target unit substates:");
7465 DUMP_STRING_TABLE(target_state, TargetState, _TARGET_STATE_MAX)do { TargetState _k; flockfile(stdout); for (_k = 0; _k < (
_TARGET_STATE_MAX); _k++) { const char *_t; _t = target_state_to_string
(_k); if (!_t) continue; fputs_unlocked(_t, stdout); fputc_unlocked
('\n', stdout); } funlockfile(stdout); } while(0)
;
7466
7467 if (!arg_no_legend)
7468 puts("\nAvailable timer unit substates:");
7469 DUMP_STRING_TABLE(timer_state, TimerState, _TIMER_STATE_MAX)do { TimerState _k; flockfile(stdout); for (_k = 0; _k < (
_TIMER_STATE_MAX); _k++) { const char *_t; _t = timer_state_to_string
(_k); if (!_t) continue; fputs_unlocked(_t, stdout); fputc_unlocked
('\n', stdout); } funlockfile(stdout); } while(0)
;
7470}
7471
7472static int systemctl_parse_argv(int argc, char *argv[]) {
7473 enum {
7474 ARG_FAIL = 0x100,
7475 ARG_REVERSE,
7476 ARG_AFTER,
7477 ARG_BEFORE,
7478 ARG_DRY_RUN,
7479 ARG_SHOW_TYPES,
7480 ARG_IRREVERSIBLE,
7481 ARG_IGNORE_DEPENDENCIES,
7482 ARG_VALUE,
7483 ARG_VERSION,
7484 ARG_USER,
7485 ARG_SYSTEM,
7486 ARG_GLOBAL,
7487 ARG_NO_BLOCK,
7488 ARG_NO_LEGEND,
7489 ARG_NO_PAGER,
7490 ARG_NO_WALL,
7491 ARG_ROOT,
7492 ARG_NO_RELOAD,
7493 ARG_KILL_WHO,
7494 ARG_NO_ASK_PASSWORD,
7495 ARG_FAILED,
7496 ARG_RUNTIME,
7497 ARG_PLAIN,
7498 ARG_STATE,
7499 ARG_JOB_MODE,
7500 ARG_PRESET_MODE,
7501 ARG_FIRMWARE_SETUP,
7502 ARG_NOW,
7503 ARG_MESSAGE,
7504 ARG_WAIT,
7505 };
7506
7507 static const struct option options[] = {
7508 { "help", no_argument0, NULL((void*)0), 'h' },
7509 { "version", no_argument0, NULL((void*)0), ARG_VERSION },
7510 { "type", required_argument1, NULL((void*)0), 't' },
7511 { "property", required_argument1, NULL((void*)0), 'p' },
7512 { "all", no_argument0, NULL((void*)0), 'a' },
7513 { "reverse", no_argument0, NULL((void*)0), ARG_REVERSE },
7514 { "after", no_argument0, NULL((void*)0), ARG_AFTER },
7515 { "before", no_argument0, NULL((void*)0), ARG_BEFORE },
7516 { "show-types", no_argument0, NULL((void*)0), ARG_SHOW_TYPES },
7517 { "failed", no_argument0, NULL((void*)0), ARG_FAILED }, /* compatibility only */
7518 { "full", no_argument0, NULL((void*)0), 'l' },
7519 { "job-mode", required_argument1, NULL((void*)0), ARG_JOB_MODE },
7520 { "fail", no_argument0, NULL((void*)0), ARG_FAIL }, /* compatibility only */
7521 { "irreversible", no_argument0, NULL((void*)0), ARG_IRREVERSIBLE }, /* compatibility only */
7522 { "ignore-dependencies", no_argument0, NULL((void*)0), ARG_IGNORE_DEPENDENCIES }, /* compatibility only */
7523 { "ignore-inhibitors", no_argument0, NULL((void*)0), 'i' },
7524 { "value", no_argument0, NULL((void*)0), ARG_VALUE },
7525 { "user", no_argument0, NULL((void*)0), ARG_USER },
7526 { "system", no_argument0, NULL((void*)0), ARG_SYSTEM },
7527 { "global", no_argument0, NULL((void*)0), ARG_GLOBAL },
7528 { "wait", no_argument0, NULL((void*)0), ARG_WAIT },
7529 { "no-block", no_argument0, NULL((void*)0), ARG_NO_BLOCK },
7530 { "no-legend", no_argument0, NULL((void*)0), ARG_NO_LEGEND },
7531 { "no-pager", no_argument0, NULL((void*)0), ARG_NO_PAGER },
7532 { "no-wall", no_argument0, NULL((void*)0), ARG_NO_WALL },
7533 { "dry-run", no_argument0, NULL((void*)0), ARG_DRY_RUN },
7534 { "quiet", no_argument0, NULL((void*)0), 'q' },
7535 { "root", required_argument1, NULL((void*)0), ARG_ROOT },
7536 { "force", no_argument0, NULL((void*)0), 'f' },
7537 { "no-reload", no_argument0, NULL((void*)0), ARG_NO_RELOAD },
7538 { "kill-who", required_argument1, NULL((void*)0), ARG_KILL_WHO },
7539 { "signal", required_argument1, NULL((void*)0), 's' },
7540 { "no-ask-password", no_argument0, NULL((void*)0), ARG_NO_ASK_PASSWORD },
7541 { "host", required_argument1, NULL((void*)0), 'H' },
7542 { "machine", required_argument1, NULL((void*)0), 'M' },
7543 { "runtime", no_argument0, NULL((void*)0), ARG_RUNTIME },
7544 { "lines", required_argument1, NULL((void*)0), 'n' },
7545 { "output", required_argument1, NULL((void*)0), 'o' },
7546 { "plain", no_argument0, NULL((void*)0), ARG_PLAIN },
7547 { "state", required_argument1, NULL((void*)0), ARG_STATE },
7548 { "recursive", no_argument0, NULL((void*)0), 'r' },
7549 { "preset-mode", required_argument1, NULL((void*)0), ARG_PRESET_MODE },
7550 { "firmware-setup", no_argument0, NULL((void*)0), ARG_FIRMWARE_SETUP },
7551 { "now", no_argument0, NULL((void*)0), ARG_NOW },
7552 { "message", required_argument1, NULL((void*)0), ARG_MESSAGE },
7553 { "show-transaction", no_argument0, NULL((void*)0), 'T' },
7554 {}
7555 };
7556
7557 const char *p;
7558 int c, r;
7559
7560 assert(argc >= 0)do { if ((__builtin_expect(!!(!(argc >= 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("argc >= 0"), "../src/systemctl/systemctl.c"
, 7560, __PRETTY_FUNCTION__); } while (0)
;
7561 assert(argv)do { if ((__builtin_expect(!!(!(argv)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("argv"), "../src/systemctl/systemctl.c",
7561, __PRETTY_FUNCTION__); } while (0)
;
7562
7563 /* we default to allowing interactive authorization only in systemctl (not in the legacy commands) */
7564 arg_ask_password = true1;
7565
7566 while ((c = getopt_long(argc, argv, "ht:p:alqfs:H:M:n:o:iTr", options, NULL((void*)0))) >= 0)
7567
7568 switch (c) {
7569
7570 case 'h':
7571 systemctl_help();
7572 return 0;
7573
7574 case ARG_VERSION:
7575 return version();
7576
7577 case 't': {
7578 if (isempty(optarg)) {
7579 log_error("--type= requires arguments.")({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 7579, __func__, "--type= requires arguments."
) : -abs(_e); })
;
7580 return -EINVAL22;
7581 }
7582
7583 for (p = optarg;;) {
7584 _cleanup_free___attribute__((cleanup(freep))) char *type = NULL((void*)0);
7585
7586 r = extract_first_word(&p, &type, ",", 0);
7587 if (r < 0)
7588 return log_error_errno(r, "Failed to parse type: %s", optarg)({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 7588, __func__, "Failed to parse type: %s"
, optarg) : -abs(_e); })
;
7589 if (r == 0)
7590 break;
7591
7592 if (streq(type, "help")(strcmp((type),("help")) == 0)) {
7593 help_types();
7594 return 0;
7595 }
7596
7597 if (unit_type_from_string(type) >= 0) {
7598 if (strv_push(&arg_types, type) < 0)
7599 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 7599, __func__)
;
7600 type = NULL((void*)0);
7601 continue;
7602 }
7603
7604 /* It's much nicer to use --state= for
7605 * load states, but let's support this
7606 * in --types= too for compatibility
7607 * with old versions */
7608 if (unit_load_state_from_string(type) >= 0) {
7609 if (strv_push(&arg_states, type) < 0)
7610 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 7610, __func__)
;
7611 type = NULL((void*)0);
7612 continue;
7613 }
7614
7615 log_error("Unknown unit type or load state '%s'.", type)({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 7615, __func__, "Unknown unit type or load state '%s'."
, type) : -abs(_e); })
;
7616 log_info("Use -t help to see a list of allowed values.")({ int _level = (((6))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 7616, __func__, "Use -t help to see a list of allowed values."
) : -abs(_e); })
;
7617 return -EINVAL22;
7618 }
7619
7620 break;
7621 }
7622
7623 case 'p': {
7624 /* Make sure that if the empty property list
7625 was specified, we won't show any properties. */
7626 if (isempty(optarg) && !arg_properties) {
7627 arg_properties = new0(char*, 1)((char**) calloc((1), sizeof(char*)));
7628 if (!arg_properties)
7629 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 7629, __func__)
;
7630 } else
7631 for (p = optarg;;) {
7632 _cleanup_free___attribute__((cleanup(freep))) char *prop = NULL((void*)0);
7633
7634 r = extract_first_word(&p, &prop, ",", 0);
7635 if (r < 0)
7636 return log_error_errno(r, "Failed to parse property: %s", optarg)({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 7636, __func__, "Failed to parse property: %s"
, optarg) : -abs(_e); })
;
7637 if (r == 0)
7638 break;
7639
7640 if (strv_push(&arg_properties, prop) < 0)
7641 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 7641, __func__)
;
7642
7643 prop = NULL((void*)0);
7644 }
7645
7646 /* If the user asked for a particular
7647 * property, show it to him, even if it is
7648 * empty. */
7649 arg_all = true1;
7650
7651 break;
7652 }
7653
7654 case 'a':
7655 arg_all = true1;
7656 break;
7657
7658 case ARG_REVERSE:
7659 arg_dependency = DEPENDENCY_REVERSE;
7660 break;
7661
7662 case ARG_AFTER:
7663 arg_dependency = DEPENDENCY_AFTER;
7664 arg_jobs_after = true1;
7665 break;
7666
7667 case ARG_BEFORE:
7668 arg_dependency = DEPENDENCY_BEFORE;
7669 arg_jobs_before = true1;
7670 break;
7671
7672 case ARG_SHOW_TYPES:
7673 arg_show_types = true1;
7674 break;
7675
7676 case ARG_VALUE:
7677 arg_value = true1;
7678 break;
7679
7680 case ARG_JOB_MODE:
7681 arg_job_mode = optarg;
7682 break;
7683
7684 case ARG_FAIL:
7685 arg_job_mode = "fail";
7686 break;
7687
7688 case ARG_IRREVERSIBLE:
7689 arg_job_mode = "replace-irreversibly";
7690 break;
7691
7692 case ARG_IGNORE_DEPENDENCIES:
7693 arg_job_mode = "ignore-dependencies";
7694 break;
7695
7696 case ARG_USER:
7697 arg_scope = UNIT_FILE_USER;
7698 break;
7699
7700 case ARG_SYSTEM:
7701 arg_scope = UNIT_FILE_SYSTEM;
7702 break;
7703
7704 case ARG_GLOBAL:
7705 arg_scope = UNIT_FILE_GLOBAL;
7706 break;
7707
7708 case ARG_WAIT:
7709 arg_wait = true1;
7710 break;
7711
7712 case ARG_NO_BLOCK:
7713 arg_no_block = true1;
7714 break;
7715
7716 case ARG_NO_LEGEND:
7717 arg_no_legend = true1;
7718 break;
7719
7720 case ARG_NO_PAGER:
7721 arg_no_pager = true1;
7722 break;
7723
7724 case ARG_NO_WALL:
7725 arg_no_wall = true1;
7726 break;
7727
7728 case ARG_ROOT:
7729 r = parse_path_argument_and_warn(optarg, false0, &arg_root);
7730 if (r < 0)
7731 return r;
7732 break;
7733
7734 case 'l':
7735 arg_full = true1;
7736 break;
7737
7738 case ARG_FAILED:
7739 if (strv_extend(&arg_states, "failed") < 0)
7740 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 7740, __func__)
;
7741
7742 break;
7743
7744 case ARG_DRY_RUN:
7745 arg_dry_run = true1;
7746 break;
7747
7748 case 'q':
7749 arg_quiet = true1;
7750 break;
7751
7752 case 'f':
7753 arg_force++;
7754 break;
7755
7756 case ARG_NO_RELOAD:
7757 arg_no_reload = true1;
7758 break;
7759
7760 case ARG_KILL_WHO:
7761 arg_kill_who = optarg;
7762 break;
7763
7764 case 's':
7765 if (streq(optarg, "help")(strcmp((optarg),("help")) == 0)) {
7766 DUMP_STRING_TABLE(signal, int, _NSIG)do { int _k; flockfile(stdout); for (_k = 0; _k < ((64 + 1
)); _k++) { const char *_t; _t = signal_to_string(_k); if (!_t
) continue; fputs_unlocked(_t, stdout); fputc_unlocked('\n', stdout
); } funlockfile(stdout); } while(0)
;
7767 return 0;
7768 }
7769
7770 arg_signal = signal_from_string(optarg);
7771 if (arg_signal < 0) {
7772 log_error("Failed to parse signal string %s.", optarg)({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 7772, __func__, "Failed to parse signal string %s."
, optarg) : -abs(_e); })
;
7773 return -EINVAL22;
7774 }
7775 break;
7776
7777 case ARG_NO_ASK_PASSWORD:
7778 arg_ask_password = false0;
7779 break;
7780
7781 case 'H':
7782 arg_transport = BUS_TRANSPORT_REMOTE;
7783 arg_host = optarg;
7784 break;
7785
7786 case 'M':
7787 arg_transport = BUS_TRANSPORT_MACHINE;
7788 arg_host = optarg;
7789 break;
7790
7791 case ARG_RUNTIME:
7792 arg_runtime = true1;
7793 break;
7794
7795 case 'n':
7796 if (safe_atou(optarg, &arg_lines) < 0) {
7797 log_error("Failed to parse lines '%s'", optarg)({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 7797, __func__, "Failed to parse lines '%s'"
, optarg) : -abs(_e); })
;
7798 return -EINVAL22;
7799 }
7800 break;
7801
7802 case 'o':
7803 if (streq(optarg, "help")(strcmp((optarg),("help")) == 0)) {
7804 DUMP_STRING_TABLE(output_mode, OutputMode, _OUTPUT_MODE_MAX)do { OutputMode _k; flockfile(stdout); for (_k = 0; _k < (
_OUTPUT_MODE_MAX); _k++) { const char *_t; _t = output_mode_to_string
(_k); if (!_t) continue; fputs_unlocked(_t, stdout); fputc_unlocked
('\n', stdout); } funlockfile(stdout); } while(0)
;
7805 return 0;
7806 }
7807
7808 arg_output = output_mode_from_string(optarg);
7809 if (arg_output < 0) {
7810 log_error("Unknown output '%s'.", optarg)({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 7810, __func__, "Unknown output '%s'."
, optarg) : -abs(_e); })
;
7811 return -EINVAL22;
7812 }
7813 break;
7814
7815 case 'i':
7816 arg_ignore_inhibitors = true1;
7817 break;
7818
7819 case ARG_PLAIN:
7820 arg_plain = true1;
7821 break;
7822
7823 case ARG_FIRMWARE_SETUP:
7824 arg_firmware_setup = true1;
7825 break;
7826
7827 case ARG_STATE: {
7828 if (isempty(optarg)) {
7829 log_error("--state= requires arguments.")({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 7829, __func__, "--state= requires arguments."
) : -abs(_e); })
;
7830 return -EINVAL22;
7831 }
7832
7833 for (p = optarg;;) {
7834 _cleanup_free___attribute__((cleanup(freep))) char *s = NULL((void*)0);
7835
7836 r = extract_first_word(&p, &s, ",", 0);
7837 if (r < 0)
7838 return log_error_errno(r, "Failed to parse state: %s", optarg)({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 7838, __func__, "Failed to parse state: %s"
, optarg) : -abs(_e); })
;
7839 if (r == 0)
7840 break;
7841
7842 if (streq(s, "help")(strcmp((s),("help")) == 0)) {
7843 help_states();
7844 return 0;
7845 }
7846
7847 if (strv_push(&arg_states, s) < 0)
7848 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 7848, __func__)
;
7849
7850 s = NULL((void*)0);
7851 }
7852 break;
7853 }
7854
7855 case 'r':
7856 if (geteuid() != 0) {
7857 log_error("--recursive requires root privileges.")({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 7857, __func__, "--recursive requires root privileges."
) : -abs(_e); })
;
7858 return -EPERM1;
7859 }
7860
7861 arg_recursive = true1;
7862 break;
7863
7864 case ARG_PRESET_MODE:
7865 if (streq(optarg, "help")(strcmp((optarg),("help")) == 0)) {
7866 DUMP_STRING_TABLE(unit_file_preset_mode, UnitFilePresetMode, _UNIT_FILE_PRESET_MAX)do { UnitFilePresetMode _k; flockfile(stdout); for (_k = 0; _k
< (_UNIT_FILE_PRESET_MAX); _k++) { const char *_t; _t = unit_file_preset_mode_to_string
(_k); if (!_t) continue; fputs_unlocked(_t, stdout); fputc_unlocked
('\n', stdout); } funlockfile(stdout); } while(0)
;
7867 return 0;
7868 }
7869
7870 arg_preset_mode = unit_file_preset_mode_from_string(optarg);
7871 if (arg_preset_mode < 0) {
7872 log_error("Failed to parse preset mode: %s.", optarg)({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 7872, __func__, "Failed to parse preset mode: %s."
, optarg) : -abs(_e); })
;
7873 return -EINVAL22;
7874 }
7875
7876 break;
7877
7878 case ARG_NOW:
7879 arg_now = true1;
7880 break;
7881
7882 case ARG_MESSAGE:
7883 if (strv_extend(&arg_wall, optarg) < 0)
7884 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 7884, __func__)
;
7885 break;
7886
7887 case 'T':
7888 arg_show_transaction = true1;
7889 break;
7890
7891 case '?':
7892 return -EINVAL22;
7893
7894 default:
7895 assert_not_reached("Unhandled option")do { log_assert_failed_unreachable_realm(LOG_REALM_SYSTEMD, (
"Unhandled option"), "../src/systemctl/systemctl.c", 7895, __PRETTY_FUNCTION__
); } while (0)
;
7896 }
7897
7898 if (arg_transport != BUS_TRANSPORT_LOCAL && arg_scope != UNIT_FILE_SYSTEM) {
7899 log_error("Cannot access user instance remotely.")({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 7899, __func__, "Cannot access user instance remotely."
) : -abs(_e); })
;
7900 return -EINVAL22;
7901 }
7902
7903 if (arg_wait && arg_no_block) {
7904 log_error("--wait may not be combined with --no-block.")({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 7904, __func__, "--wait may not be combined with --no-block."
) : -abs(_e); })
;
7905 return -EINVAL22;
7906 }
7907
7908 if (arg_runtime && STRPTR_IN_SET(argv[optind], "disable", "unmask", "preset", "preset-all")({ const char* _x = (argv[optind]); _x && (!!strv_find
((((char**) ((const char*[]) { "disable", "unmask", "preset",
"preset-all", ((void*)0) }))), (_x))); })
) {
7909 log_error("--runtime cannot be used with %s", argv[optind])({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 7909, __func__, "--runtime cannot be used with %s"
, argv[optind]) : -abs(_e); })
;
7910 return -EINVAL22;
7911 }
7912
7913 return 1;
7914}
7915
7916static int halt_parse_argv(int argc, char *argv[]) {
7917 enum {
7918 ARG_HELP = 0x100,
7919 ARG_HALT,
7920 ARG_REBOOT,
7921 ARG_NO_WALL
7922 };
7923
7924 static const struct option options[] = {
7925 { "help", no_argument0, NULL((void*)0), ARG_HELP },
7926 { "halt", no_argument0, NULL((void*)0), ARG_HALT },
7927 { "poweroff", no_argument0, NULL((void*)0), 'p' },
7928 { "reboot", no_argument0, NULL((void*)0), ARG_REBOOT },
7929 { "force", no_argument0, NULL((void*)0), 'f' },
7930 { "wtmp-only", no_argument0, NULL((void*)0), 'w' },
7931 { "no-wtmp", no_argument0, NULL((void*)0), 'd' },
7932 { "no-sync", no_argument0, NULL((void*)0), 'n' },
7933 { "no-wall", no_argument0, NULL((void*)0), ARG_NO_WALL },
7934 {}
7935 };
7936
7937 int c, r, runlevel;
7938
7939 assert(argc >= 0)do { if ((__builtin_expect(!!(!(argc >= 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("argc >= 0"), "../src/systemctl/systemctl.c"
, 7939, __PRETTY_FUNCTION__); } while (0)
;
7940 assert(argv)do { if ((__builtin_expect(!!(!(argv)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("argv"), "../src/systemctl/systemctl.c",
7940, __PRETTY_FUNCTION__); } while (0)
;
7941
7942 if (utmp_get_runlevel(&runlevel, NULL((void*)0)) >= 0)
7943 if (IN_SET(runlevel, '0', '6')({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){'0', '6'})/sizeof(int)]; switch(runlevel
) { case '0': case '6': _found = 1; break; default: break; } _found
; })
)
7944 arg_force = 2;
7945
7946 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL((void*)0))) >= 0)
7947 switch (c) {
7948
7949 case ARG_HELP:
7950 halt_help();
7951 return 0;
7952
7953 case ARG_HALT:
7954 arg_action = ACTION_HALT;
7955 break;
7956
7957 case 'p':
7958 if (arg_action != ACTION_REBOOT)
7959 arg_action = ACTION_POWEROFF;
7960 break;
7961
7962 case ARG_REBOOT:
7963 arg_action = ACTION_REBOOT;
7964 break;
7965
7966 case 'f':
7967 arg_force = 2;
7968 break;
7969
7970 case 'w':
7971 arg_dry_run = true1;
7972 break;
7973
7974 case 'd':
7975 arg_no_wtmp = true1;
7976 break;
7977
7978 case 'n':
7979 arg_no_sync = true1;
7980 break;
7981
7982 case ARG_NO_WALL:
7983 arg_no_wall = true1;
7984 break;
7985
7986 case 'i':
7987 case 'h':
7988 /* Compatibility nops */
7989 break;
7990
7991 case '?':
7992 return -EINVAL22;
7993
7994 default:
7995 assert_not_reached("Unhandled option")do { log_assert_failed_unreachable_realm(LOG_REALM_SYSTEMD, (
"Unhandled option"), "../src/systemctl/systemctl.c", 7995, __PRETTY_FUNCTION__
); } while (0)
;
7996 }
7997
7998 if (arg_action == ACTION_REBOOT && (argc == optind || argc == optind + 1)) {
7999 r = update_reboot_parameter_and_warn(argc == optind + 1 ? argv[optind] : NULL((void*)0));
8000 if (r < 0)
8001 return r;
8002 } else if (optind < argc) {
8003 log_error("Too many arguments.")({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 8003, __func__, "Too many arguments."
) : -abs(_e); })
;
8004 return -EINVAL22;
8005 }
8006
8007 return 1;
8008}
8009
8010static int parse_shutdown_time_spec(const char *t, usec_t *_u) {
8011 assert(t)do { if ((__builtin_expect(!!(!(t)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("t"), "../src/systemctl/systemctl.c", 8011
, __PRETTY_FUNCTION__); } while (0)
;
8012 assert(_u)do { if ((__builtin_expect(!!(!(_u)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("_u"), "../src/systemctl/systemctl.c", 8012
, __PRETTY_FUNCTION__); } while (0)
;
8013
8014 if (streq(t, "now")(strcmp((t),("now")) == 0))
8015 *_u = 0;
8016 else if (!strchr(t, ':')) {
8017 uint64_t u;
8018
8019 if (safe_atou64(t, &u) < 0)
8020 return -EINVAL22;
8021
8022 *_u = now(CLOCK_REALTIME0) + USEC_PER_MINUTE((usec_t) (60ULL*((usec_t) 1000000ULL))) * u;
8023 } else {
8024 char *e = NULL((void*)0);
8025 long hour, minute;
8026 struct tm tm = {};
8027 time_t s;
8028 usec_t n;
8029
8030 errno(*__errno_location ()) = 0;
8031 hour = strtol(t, &e, 10);
8032 if (errno(*__errno_location ()) > 0 || *e != ':' || hour < 0 || hour > 23)
8033 return -EINVAL22;
8034
8035 minute = strtol(e+1, &e, 10);
8036 if (errno(*__errno_location ()) > 0 || *e != 0 || minute < 0 || minute > 59)
8037 return -EINVAL22;
8038
8039 n = now(CLOCK_REALTIME0);
8040 s = (time_t) (n / USEC_PER_SEC((usec_t) 1000000ULL));
8041
8042 assert_se(localtime_r(&s, &tm))do { if ((__builtin_expect(!!(!(localtime_r(&s, &tm))
),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("localtime_r(&s, &tm)"
), "../src/systemctl/systemctl.c", 8042, __PRETTY_FUNCTION__)
; } while (0)
;
8043
8044 tm.tm_hour = (int) hour;
8045 tm.tm_min = (int) minute;
8046 tm.tm_sec = 0;
8047
8048 assert_se(s = mktime(&tm))do { if ((__builtin_expect(!!(!(s = mktime(&tm))),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("s = mktime(&tm)"), "../src/systemctl/systemctl.c"
, 8048, __PRETTY_FUNCTION__); } while (0)
;
8049
8050 *_u = (usec_t) s * USEC_PER_SEC((usec_t) 1000000ULL);
8051
8052 while (*_u <= n)
8053 *_u += USEC_PER_DAY((usec_t) (24ULL*((usec_t) (60ULL*((usec_t) (60ULL*((usec_t) 1000000ULL
)))))))
;
8054 }
8055
8056 return 0;
8057}
8058
8059static int shutdown_parse_argv(int argc, char *argv[]) {
8060 enum {
8061 ARG_HELP = 0x100,
8062 ARG_NO_WALL
8063 };
8064
8065 static const struct option options[] = {
8066 { "help", no_argument0, NULL((void*)0), ARG_HELP },
8067 { "halt", no_argument0, NULL((void*)0), 'H' },
8068 { "poweroff", no_argument0, NULL((void*)0), 'P' },
8069 { "reboot", no_argument0, NULL((void*)0), 'r' },
8070 { "kexec", no_argument0, NULL((void*)0), 'K' }, /* not documented extension */
8071 { "no-wall", no_argument0, NULL((void*)0), ARG_NO_WALL },
8072 {}
8073 };
8074
8075 char **wall = NULL((void*)0);
8076 int c, r;
8077
8078 assert(argc >= 0)do { if ((__builtin_expect(!!(!(argc >= 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("argc >= 0"), "../src/systemctl/systemctl.c"
, 8078, __PRETTY_FUNCTION__); } while (0)
;
8079 assert(argv)do { if ((__builtin_expect(!!(!(argv)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("argv"), "../src/systemctl/systemctl.c",
8079, __PRETTY_FUNCTION__); } while (0)
;
8080
8081 while ((c = getopt_long(argc, argv, "HPrhkKat:fFc", options, NULL((void*)0))) >= 0)
8082 switch (c) {
8083
8084 case ARG_HELP:
8085 shutdown_help();
8086 return 0;
8087
8088 case 'H':
8089 arg_action = ACTION_HALT;
8090 break;
8091
8092 case 'P':
8093 arg_action = ACTION_POWEROFF;
8094 break;
8095
8096 case 'r':
8097 if (kexec_loaded())
8098 arg_action = ACTION_KEXEC;
8099 else
8100 arg_action = ACTION_REBOOT;
8101 break;
8102
8103 case 'K':
8104 arg_action = ACTION_KEXEC;
8105 break;
8106
8107 case 'h':
8108 if (arg_action != ACTION_HALT)
8109 arg_action = ACTION_POWEROFF;
8110 break;
8111
8112 case 'k':
8113 arg_dry_run = true1;
8114 break;
8115
8116 case ARG_NO_WALL:
8117 arg_no_wall = true1;
8118 break;
8119
8120 case 'a':
8121 case 't': /* Note that we also ignore any passed argument to -t, not just the -t itself */
8122 case 'f':
8123 case 'F':
8124 /* Compatibility nops */
8125 break;
8126
8127 case 'c':
8128 arg_action = ACTION_CANCEL_SHUTDOWN;
8129 break;
8130
8131 case '?':
8132 return -EINVAL22;
8133
8134 default:
8135 assert_not_reached("Unhandled option")do { log_assert_failed_unreachable_realm(LOG_REALM_SYSTEMD, (
"Unhandled option"), "../src/systemctl/systemctl.c", 8135, __PRETTY_FUNCTION__
); } while (0)
;
8136 }
8137
8138 if (argc > optind && arg_action != ACTION_CANCEL_SHUTDOWN) {
8139 r = parse_shutdown_time_spec(argv[optind], &arg_when);
8140 if (r < 0) {
8141 log_error("Failed to parse time specification: %s", argv[optind])({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 8141, __func__, "Failed to parse time specification: %s"
, argv[optind]) : -abs(_e); })
;
8142 return r;
8143 }
8144 } else
8145 arg_when = now(CLOCK_REALTIME0) + USEC_PER_MINUTE((usec_t) (60ULL*((usec_t) 1000000ULL)));
8146
8147 if (argc > optind && arg_action == ACTION_CANCEL_SHUTDOWN)
8148 /* No time argument for shutdown cancel */
8149 wall = argv + optind;
8150 else if (argc > optind + 1)
8151 /* We skip the time argument */
8152 wall = argv + optind + 1;
8153
8154 if (wall) {
8155 arg_wall = strv_copy(wall);
8156 if (!arg_wall)
8157 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/systemctl/systemctl.c"
, 8157, __func__)
;
8158 }
8159
8160 optind = argc;
8161
8162 return 1;
8163}
8164
8165static int telinit_parse_argv(int argc, char *argv[]) {
8166 enum {
8167 ARG_HELP = 0x100,
8168 ARG_NO_WALL
8169 };
8170
8171 static const struct option options[] = {
8172 { "help", no_argument0, NULL((void*)0), ARG_HELP },
8173 { "no-wall", no_argument0, NULL((void*)0), ARG_NO_WALL },
8174 {}
8175 };
8176
8177 static const struct {
8178 char from;
8179 enum action to;
8180 } table[] = {
8181 { '0', ACTION_POWEROFF },
8182 { '6', ACTION_REBOOT },
8183 { '1', ACTION_RESCUE },
8184 { '2', ACTION_RUNLEVEL2 },
8185 { '3', ACTION_RUNLEVEL3 },
8186 { '4', ACTION_RUNLEVEL4 },
8187 { '5', ACTION_RUNLEVEL5 },
8188 { 's', ACTION_RESCUE },
8189 { 'S', ACTION_RESCUE },
8190 { 'q', ACTION_RELOAD },
8191 { 'Q', ACTION_RELOAD },
8192 { 'u', ACTION_REEXEC },
8193 { 'U', ACTION_REEXEC }
8194 };
8195
8196 unsigned i;
8197 int c;
8198
8199 assert(argc >= 0)do { if ((__builtin_expect(!!(!(argc >= 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("argc >= 0"), "../src/systemctl/systemctl.c"
, 8199, __PRETTY_FUNCTION__); } while (0)
;
8200 assert(argv)do { if ((__builtin_expect(!!(!(argv)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("argv"), "../src/systemctl/systemctl.c",
8200, __PRETTY_FUNCTION__); } while (0)
;
8201
8202 while ((c = getopt_long(argc, argv, "", options, NULL((void*)0))) >= 0)
8203 switch (c) {
8204
8205 case ARG_HELP:
8206 telinit_help();
8207 return 0;
8208
8209 case ARG_NO_WALL:
8210 arg_no_wall = true1;
8211 break;
8212
8213 case '?':
8214 return -EINVAL22;
8215
8216 default:
8217 assert_not_reached("Unhandled option")do { log_assert_failed_unreachable_realm(LOG_REALM_SYSTEMD, (
"Unhandled option"), "../src/systemctl/systemctl.c", 8217, __PRETTY_FUNCTION__
); } while (0)
;
8218 }
8219
8220 if (optind >= argc) {
8221 log_error("%s: required argument missing.", program_invocation_short_name)({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 8221, __func__, "%s: required argument missing."
, program_invocation_short_name) : -abs(_e); })
;
8222 return -EINVAL22;
8223 }
8224
8225 if (optind + 1 < argc) {
8226 log_error("Too many arguments.")({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 8226, __func__, "Too many arguments."
) : -abs(_e); })
;
8227 return -EINVAL22;
8228 }
8229
8230 if (strlen(argv[optind]) != 1) {
8231 log_error("Expected single character argument.")({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 8231, __func__, "Expected single character argument."
) : -abs(_e); })
;
8232 return -EINVAL22;
8233 }
8234
8235 for (i = 0; i < ELEMENTSOF(table)__extension__ (__builtin_choose_expr( !__builtin_types_compatible_p
(typeof(table), typeof(&*(table))), sizeof(table)/sizeof(
(table)[0]), ((void)0)))
; i++)
8236 if (table[i].from == argv[optind][0])
8237 break;
8238
8239 if (i >= ELEMENTSOF(table)__extension__ (__builtin_choose_expr( !__builtin_types_compatible_p
(typeof(table), typeof(&*(table))), sizeof(table)/sizeof(
(table)[0]), ((void)0)))
) {
8240 log_error("Unknown command '%s'.", argv[optind])({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 8240, __func__, "Unknown command '%s'."
, argv[optind]) : -abs(_e); })
;
8241 return -EINVAL22;
8242 }
8243
8244 arg_action = table[i].to;
8245
8246 optind++;
8247
8248 return 1;
8249}
8250
8251static int runlevel_parse_argv(int argc, char *argv[]) {
8252 enum {
8253 ARG_HELP = 0x100,
8254 };
8255
8256 static const struct option options[] = {
8257 { "help", no_argument0, NULL((void*)0), ARG_HELP },
8258 {}
8259 };
8260
8261 int c;
8262
8263 assert(argc >= 0)do { if ((__builtin_expect(!!(!(argc >= 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("argc >= 0"), "../src/systemctl/systemctl.c"
, 8263, __PRETTY_FUNCTION__); } while (0)
;
8264 assert(argv)do { if ((__builtin_expect(!!(!(argv)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("argv"), "../src/systemctl/systemctl.c",
8264, __PRETTY_FUNCTION__); } while (0)
;
8265
8266 while ((c = getopt_long(argc, argv, "", options, NULL((void*)0))) >= 0)
8267 switch (c) {
8268
8269 case ARG_HELP:
8270 runlevel_help();
8271 return 0;
8272
8273 case '?':
8274 return -EINVAL22;
8275
8276 default:
8277 assert_not_reached("Unhandled option")do { log_assert_failed_unreachable_realm(LOG_REALM_SYSTEMD, (
"Unhandled option"), "../src/systemctl/systemctl.c", 8277, __PRETTY_FUNCTION__
); } while (0)
;
8278 }
8279
8280 if (optind < argc) {
8281 log_error("Too many arguments.")({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 8281, __func__, "Too many arguments."
) : -abs(_e); })
;
8282 return -EINVAL22;
8283 }
8284
8285 return 1;
8286}
8287
8288static int parse_argv(int argc, char *argv[]) {
8289 assert(argc >= 0)do { if ((__builtin_expect(!!(!(argc >= 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("argc >= 0"), "../src/systemctl/systemctl.c"
, 8289, __PRETTY_FUNCTION__); } while (0)
;
8290 assert(argv)do { if ((__builtin_expect(!!(!(argv)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("argv"), "../src/systemctl/systemctl.c",
8290, __PRETTY_FUNCTION__); } while (0)
;
8291
8292 if (program_invocation_short_name) {
8293
8294 if (strstr(program_invocation_short_name, "halt")) {
8295 arg_action = ACTION_HALT;
8296 return halt_parse_argv(argc, argv);
8297
8298 } else if (strstr(program_invocation_short_name, "poweroff")) {
8299 arg_action = ACTION_POWEROFF;
8300 return halt_parse_argv(argc, argv);
8301
8302 } else if (strstr(program_invocation_short_name, "reboot")) {
8303 if (kexec_loaded())
8304 arg_action = ACTION_KEXEC;
8305 else
8306 arg_action = ACTION_REBOOT;
8307 return halt_parse_argv(argc, argv);
8308
8309 } else if (strstr(program_invocation_short_name, "shutdown")) {
8310 arg_action = ACTION_POWEROFF;
8311 return shutdown_parse_argv(argc, argv);
8312
8313 } else if (strstr(program_invocation_short_name, "init")) {
8314
8315 /* Matches invocations as "init" as well as "telinit", which are synonymous when run as PID !=
8316 * 1 on SysV.
8317 *
8318 * On SysV "telinit" was the official command to communicate with PID 1, but "init" would
8319 * redirect itself to "telinit" if called with PID != 1. We follow the same logic here still,
8320 * though we add one level of indirection, as we implement "telinit" in "systemctl". Hence, for
8321 * us if you invoke "init" you get "systemd", but it will execve() "systemctl" immediately with
8322 * argv[] unmodified if PID is != 1. If you invoke "telinit" you directly get "systemctl". In
8323 * both cases we shall do the same thing, which is why we do strstr(p_i_s_n, "init") here, as a
8324 * quick way to match both.
8325 *
8326 * Also see redirect_telinit() in src/core/main.c. */
8327
8328 if (sd_booted() > 0) {
8329 arg_action = _ACTION_INVALID;
8330 return telinit_parse_argv(argc, argv);
8331 } else {
8332 /* Hmm, so some other init system is running, we need to forward this request to
8333 * it. For now we simply guess that it is Upstart. */
8334
8335 execv(TELINIT"/lib/sysvinit/telinit", argv);
8336
8337 log_error("Couldn't find an alternative telinit implementation to spawn.")({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 8337, __func__, "Couldn't find an alternative telinit implementation to spawn."
) : -abs(_e); })
;
8338 return -EIO5;
8339 }
8340
8341 } else if (strstr(program_invocation_short_name, "runlevel")) {
8342 arg_action = ACTION_RUNLEVEL;
8343 return runlevel_parse_argv(argc, argv);
8344 }
8345 }
8346
8347 arg_action = ACTION_SYSTEMCTL;
8348 return systemctl_parse_argv(argc, argv);
8349}
8350
8351#if HAVE_SYSV_COMPAT1
8352_pure___attribute__ ((pure)) static int action_to_runlevel(void) {
8353 static const char table[_ACTION_MAX] = {
8354 [ACTION_HALT] = '0',
8355 [ACTION_POWEROFF] = '0',
8356 [ACTION_REBOOT] = '6',
8357 [ACTION_RUNLEVEL2] = '2',
8358 [ACTION_RUNLEVEL3] = '3',
8359 [ACTION_RUNLEVEL4] = '4',
8360 [ACTION_RUNLEVEL5] = '5',
8361 [ACTION_RESCUE] = '1'
8362 };
8363
8364 assert(arg_action >= 0 && arg_action < _ACTION_MAX)do { if ((__builtin_expect(!!(!(arg_action >= 0 &&
arg_action < _ACTION_MAX)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD
, ("arg_action >= 0 && arg_action < _ACTION_MAX"
), "../src/systemctl/systemctl.c", 8364, __PRETTY_FUNCTION__)
; } while (0)
;
8365
8366 return table[arg_action];
8367}
8368#endif
8369
8370static int talk_initctl(void) {
8371#if HAVE_SYSV_COMPAT1
8372 struct init_request request = {
8373 .magic = INIT_MAGIC0x03091969,
8374 .sleeptime = 0,
8375 .cmd = INIT_CMD_RUNLVL1
8376 };
8377
8378 _cleanup_close___attribute__((cleanup(closep))) int fd = -1;
8379 char rl;
8380 int r;
8381 const char *p;
8382
8383 rl = action_to_runlevel();
8384 if (!rl)
8385 return 0;
8386
8387 request.runlevel = rl;
8388
8389 FOREACH_STRING(p, "/run/initctl", "/dev/initctl")for (char **_l = ({ char **_ll = ((char**) ((const char*[]) {
"/run/initctl", "/dev/initctl", ((void*)0) })); p = _ll ? _ll
[0] : ((void*)0); _ll; }); _l && *_l; p = ({ _l ++; _l
[0]; }))
{
8390 fd = open(p, O_WRONLY01|O_NONBLOCK04000|O_CLOEXEC02000000|O_NOCTTY0400);
8391 if (fd >= 0 || errno(*__errno_location ()) != ENOENT2)
8392 break;
8393 }
8394 if (fd < 0) {
8395 if (errno(*__errno_location ()) == ENOENT2)
8396 return 0;
8397
8398 return log_error_errno(errno, "Failed to open initctl fifo: %m")({ int _level = ((3)), _e = (((*__errno_location ()))), _realm
= (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >=
((_level) & 0x07)) ? log_internal_realm(((_realm) <<
10 | (_level)), _e, "../src/systemctl/systemctl.c", 8398, __func__
, "Failed to open initctl fifo: %m") : -abs(_e); })
;
8399 }
8400
8401 r = loop_write(fd, &request, sizeof(request), false0);
8402 if (r < 0)
8403 return log_error_errno(r, "Failed to write to %s: %m", p)({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 8403, __func__, "Failed to write to %s: %m"
, p) : -abs(_e); })
;
8404
8405 return 1;
8406#else
8407 return 0;
8408#endif
8409}
8410
8411static int systemctl_main(int argc, char *argv[]) {
8412 static const Verb verbs[] = {
8413 { "list-units", VERB_ANY((unsigned) -1), VERB_ANY((unsigned) -1), VERB_DEFAULT|VERB_ONLINE_ONLY, list_units },
8414 { "list-unit-files", VERB_ANY((unsigned) -1), VERB_ANY((unsigned) -1), 0, list_unit_files },
8415 { "list-sockets", VERB_ANY((unsigned) -1), VERB_ANY((unsigned) -1), VERB_ONLINE_ONLY, list_sockets },
8416 { "list-timers", VERB_ANY((unsigned) -1), VERB_ANY((unsigned) -1), VERB_ONLINE_ONLY, list_timers },
8417 { "list-jobs", VERB_ANY((unsigned) -1), VERB_ANY((unsigned) -1), VERB_ONLINE_ONLY, list_jobs },
8418 { "list-machines", VERB_ANY((unsigned) -1), VERB_ANY((unsigned) -1), VERB_ONLINE_ONLY|VERB_MUST_BE_ROOT, list_machines },
8419 { "clear-jobs", VERB_ANY((unsigned) -1), 1, VERB_ONLINE_ONLY, trivial_method },
8420 { "cancel", VERB_ANY((unsigned) -1), VERB_ANY((unsigned) -1), VERB_ONLINE_ONLY, cancel_job },
8421 { "start", 2, VERB_ANY((unsigned) -1), VERB_ONLINE_ONLY, start_unit },
8422 { "stop", 2, VERB_ANY((unsigned) -1), VERB_ONLINE_ONLY, start_unit },
8423 { "condstop", 2, VERB_ANY((unsigned) -1), VERB_ONLINE_ONLY, start_unit }, /* For compatibility with ALTLinux */
8424 { "reload", 2, VERB_ANY((unsigned) -1), VERB_ONLINE_ONLY, start_unit },
8425 { "restart", 2, VERB_ANY((unsigned) -1), VERB_ONLINE_ONLY, start_unit },
8426 { "try-restart", 2, VERB_ANY((unsigned) -1), VERB_ONLINE_ONLY, start_unit },
8427 { "reload-or-restart", 2, VERB_ANY((unsigned) -1), VERB_ONLINE_ONLY, start_unit },
8428 { "reload-or-try-restart", 2, VERB_ANY((unsigned) -1), VERB_ONLINE_ONLY, start_unit }, /* For compatbility with old systemctl <= 228 */
8429 { "try-reload-or-restart", 2, VERB_ANY((unsigned) -1), VERB_ONLINE_ONLY, start_unit },
8430 { "force-reload", 2, VERB_ANY((unsigned) -1), VERB_ONLINE_ONLY, start_unit }, /* For compatibility with SysV */
8431 { "condreload", 2, VERB_ANY((unsigned) -1), VERB_ONLINE_ONLY, start_unit }, /* For compatibility with ALTLinux */
8432 { "condrestart", 2, VERB_ANY((unsigned) -1), VERB_ONLINE_ONLY, start_unit }, /* For compatibility with RH */
8433 { "isolate", 2, 2, VERB_ONLINE_ONLY, start_unit },
8434 { "kill", 2, VERB_ANY((unsigned) -1), VERB_ONLINE_ONLY, kill_unit },
8435 { "freeze", 2, VERB_ANY((unsigned) -1), VERB_ONLINE_ONLY, freeze_or_thaw_unit },
8436 { "thaw", 2, VERB_ANY((unsigned) -1), VERB_ONLINE_ONLY, freeze_or_thaw_unit },
8437 { "is-active", 2, VERB_ANY((unsigned) -1), VERB_ONLINE_ONLY, check_unit_active },
8438 { "check", 2, VERB_ANY((unsigned) -1), VERB_ONLINE_ONLY, check_unit_active }, /* deprecated alias of is-active */
8439 { "is-failed", 2, VERB_ANY((unsigned) -1), VERB_ONLINE_ONLY, check_unit_failed },
8440 { "show", VERB_ANY((unsigned) -1), VERB_ANY((unsigned) -1), VERB_ONLINE_ONLY, show },
8441 { "cat", 2, VERB_ANY((unsigned) -1), VERB_ONLINE_ONLY, cat },
8442 { "status", VERB_ANY((unsigned) -1), VERB_ANY((unsigned) -1), VERB_ONLINE_ONLY, show },
8443 { "help", VERB_ANY((unsigned) -1), VERB_ANY((unsigned) -1), VERB_ONLINE_ONLY, show },
8444 { "daemon-reload", VERB_ANY((unsigned) -1), 1, VERB_ONLINE_ONLY, daemon_reload },
8445 { "daemon-reexec", VERB_ANY((unsigned) -1), 1, VERB_ONLINE_ONLY, daemon_reload },
8446 { "show-environment", VERB_ANY((unsigned) -1), 1, VERB_ONLINE_ONLY, show_environment },
8447 { "set-environment", 2, VERB_ANY((unsigned) -1), VERB_ONLINE_ONLY, set_environment },
8448 { "unset-environment", 2, VERB_ANY((unsigned) -1), VERB_ONLINE_ONLY, set_environment },
8449 { "import-environment", VERB_ANY((unsigned) -1), VERB_ANY((unsigned) -1), VERB_ONLINE_ONLY, import_environment },
8450 { "halt", VERB_ANY((unsigned) -1), 1, VERB_ONLINE_ONLY, start_system_special },
8451 { "poweroff", VERB_ANY((unsigned) -1), 1, VERB_ONLINE_ONLY, start_system_special },
8452 { "reboot", VERB_ANY((unsigned) -1), 2, VERB_ONLINE_ONLY, start_system_special },
8453 { "kexec", VERB_ANY((unsigned) -1), 1, VERB_ONLINE_ONLY, start_system_special },
8454 { "suspend", VERB_ANY((unsigned) -1), 1, VERB_ONLINE_ONLY, start_system_special },
8455 { "hibernate", VERB_ANY((unsigned) -1), 1, VERB_ONLINE_ONLY, start_system_special },
8456 { "hybrid-sleep", VERB_ANY((unsigned) -1), 1, VERB_ONLINE_ONLY, start_system_special },
8457 { "suspend-then-hibernate",VERB_ANY((unsigned) -1), 1, VERB_ONLINE_ONLY, start_system_special },
8458 { "default", VERB_ANY((unsigned) -1), 1, VERB_ONLINE_ONLY, start_special },
8459 { "rescue", VERB_ANY((unsigned) -1), 1, VERB_ONLINE_ONLY, start_system_special },
8460 { "emergency", VERB_ANY((unsigned) -1), 1, VERB_ONLINE_ONLY, start_system_special },
8461 { "exit", VERB_ANY((unsigned) -1), 2, VERB_ONLINE_ONLY, start_special },
8462 { "reset-failed", VERB_ANY((unsigned) -1), VERB_ANY((unsigned) -1), VERB_ONLINE_ONLY, reset_failed },
8463 { "enable", 2, VERB_ANY((unsigned) -1), 0, enable_unit },
8464 { "disable", 2, VERB_ANY((unsigned) -1), 0, enable_unit },
8465 { "is-enabled", 2, VERB_ANY((unsigned) -1), 0, unit_is_enabled },
8466 { "reenable", 2, VERB_ANY((unsigned) -1), 0, enable_unit },
8467 { "preset", 2, VERB_ANY((unsigned) -1), 0, enable_unit },
8468 { "preset-all", VERB_ANY((unsigned) -1), 1, 0, preset_all },
8469 { "mask", 2, VERB_ANY((unsigned) -1), 0, enable_unit },
8470 { "unmask", 2, VERB_ANY((unsigned) -1), 0, enable_unit },
8471 { "link", 2, VERB_ANY((unsigned) -1), 0, enable_unit },
8472 { "revert", 2, VERB_ANY((unsigned) -1), 0, enable_unit },
8473 { "switch-root", 2, VERB_ANY((unsigned) -1), VERB_ONLINE_ONLY, switch_root },
8474 { "list-dependencies", VERB_ANY((unsigned) -1), 2, VERB_ONLINE_ONLY, list_dependencies },
8475 { "set-default", 2, 2, 0, set_default },
8476 { "get-default", VERB_ANY((unsigned) -1), 1, 0, get_default },
8477 { "set-property", 3, VERB_ANY((unsigned) -1), VERB_ONLINE_ONLY, set_property },
8478 { "is-system-running", VERB_ANY((unsigned) -1), 1, 0, is_system_running },
8479 { "add-wants", 3, VERB_ANY((unsigned) -1), 0, add_dependency },
8480 { "add-requires", 3, VERB_ANY((unsigned) -1), 0, add_dependency },
8481 { "edit", 2, VERB_ANY((unsigned) -1), VERB_ONLINE_ONLY, edit },
8482 {}
8483 };
8484
8485 return dispatch_verb(argc, argv, verbs, NULL((void*)0));
8486}
8487
8488static int reload_with_fallback(void) {
8489 /* First, try systemd via D-Bus. */
8490 if (daemon_reload(0, NULL((void*)0), NULL((void*)0)) >= 0)
8491 return 0;
8492
8493 /* Nothing else worked, so let's try signals */
8494 assert(IN_SET(arg_action, ACTION_RELOAD, ACTION_REEXEC))do { if ((__builtin_expect(!!(!(({ _Bool _found = 0; static __attribute__
((unused)) char _static_assert__macros_need_to_be_extended[20
- sizeof((int[]){ACTION_RELOAD, ACTION_REEXEC})/sizeof(int)]
; switch(arg_action) { case ACTION_RELOAD: case ACTION_REEXEC
: _found = 1; break; default: break; } _found; }))),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("IN_SET(arg_action, ACTION_RELOAD, ACTION_REEXEC)"
), "../src/systemctl/systemctl.c", 8494, __PRETTY_FUNCTION__)
; } while (0)
;
8495
8496 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP1 : SIGTERM15) < 0)
8497 return log_error_errno(errno, "kill() failed: %m")({ int _level = ((3)), _e = (((*__errno_location ()))), _realm
= (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >=
((_level) & 0x07)) ? log_internal_realm(((_realm) <<
10 | (_level)), _e, "../src/systemctl/systemctl.c", 8497, __func__
, "kill() failed: %m") : -abs(_e); })
;
8498
8499 return 0;
8500}
8501
8502static int start_with_fallback(void) {
8503 /* First, try systemd via D-Bus. */
8504 if (start_unit(0, NULL((void*)0), NULL((void*)0)) >= 0)
8505 return 0;
8506
8507 /* Nothing else worked, so let's try /dev/initctl */
8508 if (talk_initctl() > 0)
8509 return 0;
8510
8511 log_error("Failed to talk to init daemon.")({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 8511, __func__, "Failed to talk to init daemon."
) : -abs(_e); })
;
8512 return -EIO5;
8513}
8514
8515static int halt_now(enum action a) {
8516 /* The kernel will automatically flush ATA disks and suchlike on reboot(), but the file systems need to be
8517 * synce'd explicitly in advance. */
8518 if (!arg_no_sync && !arg_dry_run)
8519 (void) sync();
8520
8521 /* Make sure C-A-D is handled by the kernel from this point on... */
8522 if (!arg_dry_run)
8523 (void) reboot(RB_ENABLE_CAD0x89abcdef);
8524
8525 switch (a) {
8526
8527 case ACTION_HALT:
8528 if (!arg_quiet)
8529 log_info("Halting.")({ int _level = (((6))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 8529, __func__, "Halting.")
: -abs(_e); })
;
8530 if (arg_dry_run)
8531 return 0;
8532 (void) reboot(RB_HALT_SYSTEM0xcdef0123);
8533 return -errno(*__errno_location ());
8534
8535 case ACTION_POWEROFF:
8536 if (!arg_quiet)
8537 log_info("Powering off.")({ int _level = (((6))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 8537, __func__, "Powering off."
) : -abs(_e); })
;
8538 if (arg_dry_run)
8539 return 0;
8540 (void) reboot(RB_POWER_OFF0x4321fedc);
8541 return -errno(*__errno_location ());
8542
8543 case ACTION_KEXEC:
8544 case ACTION_REBOOT:
8545 return reboot_with_parameter(REBOOT_FALLBACK |
8546 (arg_quiet ? 0 : REBOOT_LOG) |
8547 (arg_dry_run ? REBOOT_DRY_RUN : 0));
8548
8549 default:
8550 assert_not_reached("Unknown action.")do { log_assert_failed_unreachable_realm(LOG_REALM_SYSTEMD, (
"Unknown action."), "../src/systemctl/systemctl.c", 8550, __PRETTY_FUNCTION__
); } while (0)
;
8551 }
8552}
8553
8554static int logind_schedule_shutdown(void) {
8555
8556#if ENABLE_LOGIND1
8557 _cleanup_(sd_bus_error_free)__attribute__((cleanup(sd_bus_error_free))) sd_bus_error error = SD_BUS_ERROR_NULL((const sd_bus_error) {(((void*)0)), (((void*)0)), 0});
8558 char date[FORMAT_TIMESTAMP_MAX(3+1+10+1+8+1+6+1+6+1)];
8559 const char *action;
8560 sd_bus *bus;
8561 int r;
8562
8563 r = acquire_bus(BUS_FULL, &bus);
8564 if (r < 0)
8565 return r;
8566
8567 switch (arg_action) {
8568 case ACTION_HALT:
8569 action = "halt";
8570 break;
8571 case ACTION_POWEROFF:
8572 action = "poweroff";
8573 break;
8574 case ACTION_KEXEC:
8575 action = "kexec";
8576 break;
8577 case ACTION_EXIT:
8578 action = "exit";
8579 break;
8580 case ACTION_REBOOT:
8581 default:
8582 action = "reboot";
8583 break;
8584 }
8585
8586 if (arg_dry_run)
8587 action = strjoina("dry-", action)({ const char *_appendees_[] = { "dry-", action }; char *_d_,
*_p_; size_t _len_ = 0; size_t _i_; for (_i_ = 0; _i_ < __extension__
(__builtin_choose_expr( !__builtin_types_compatible_p(typeof
(_appendees_), typeof(&*(_appendees_))), sizeof(_appendees_
)/sizeof((_appendees_)[0]), ((void)0))) && _appendees_
[_i_]; _i_++) _len_ += strlen(_appendees_[_i_]); _p_ = _d_ = __builtin_alloca
(_len_ + 1); for (_i_ = 0; _i_ < __extension__ (__builtin_choose_expr
( !__builtin_types_compatible_p(typeof(_appendees_), typeof(&
*(_appendees_))), sizeof(_appendees_)/sizeof((_appendees_)[0]
), ((void)0))) && _appendees_[_i_]; _i_++) _p_ = stpcpy
(_p_, _appendees_[_i_]); *_p_ = 0; _d_; })
;
8588
8589 (void) logind_set_wall_message();
8590
8591 r = sd_bus_call_method(
8592 bus,
8593 "org.freedesktop.login1",
8594 "/org/freedesktop/login1",
8595 "org.freedesktop.login1.Manager",
8596 "ScheduleShutdown",
8597 &error,
8598 NULL((void*)0),
8599 "st",
8600 action,
8601 arg_when);
8602 if (r < 0)
8603 return log_warning_errno(r, "Failed to call ScheduleShutdown in logind, proceeding with immediate shutdown: %s", bus_error_message(&error, r))({ int _level = ((4)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 8603, __func__, "Failed to call ScheduleShutdown in logind, proceeding with immediate shutdown: %s"
, bus_error_message(&error, r)) : -abs(_e); })
;
8604
8605 if (!arg_quiet)
8606 log_info("Shutdown scheduled for %s, use 'shutdown -c' to cancel.", format_timestamp(date, sizeof(date), arg_when))({ int _level = (((6))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 8606, __func__, "Shutdown scheduled for %s, use 'shutdown -c' to cancel."
, format_timestamp(date, sizeof(date), arg_when)) : -abs(_e);
})
;
8607 return 0;
8608#else
8609 log_error("Cannot schedule shutdown without logind support, proceeding with immediate shutdown.")({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 8609, __func__, "Cannot schedule shutdown without logind support, proceeding with immediate shutdown."
) : -abs(_e); })
;
8610 return -ENOSYS38;
8611#endif
8612}
8613
8614static int halt_main(void) {
8615 int r;
8616
8617 r = logind_check_inhibitors(arg_action);
8618 if (r < 0)
8619 return r;
8620
8621 if (arg_when > 0)
8622 return logind_schedule_shutdown();
8623
8624 if (geteuid() != 0) {
8625 if (arg_dry_run || arg_force > 0) {
8626 (void) must_be_root();
8627 return -EPERM1;
8628 }
8629
8630 /* Try logind if we are a normal user and no special
8631 * mode applies. Maybe PolicyKit allows us to shutdown
8632 * the machine. */
8633 if (IN_SET(arg_action, ACTION_POWEROFF, ACTION_REBOOT, ACTION_HALT)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){ACTION_POWEROFF, ACTION_REBOOT, ACTION_HALT
})/sizeof(int)]; switch(arg_action) { case ACTION_POWEROFF: case
ACTION_REBOOT: case ACTION_HALT: _found = 1; break; default:
break; } _found; })
) {
8634 r = logind_reboot(arg_action);
8635 if (r >= 0)
8636 return r;
8637 if (IN_SET(r, -EOPNOTSUPP, -EINPROGRESS)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){-95, -115})/sizeof(int)]; switch(r) { case
-95: case -115: _found = 1; break; default: break; } _found;
})
)
8638 /* requested operation is not
8639 * supported on the local system or
8640 * already in progress */
8641 return r;
8642 /* on all other errors, try low-level operation */
8643 }
8644 }
8645
8646 /* In order to minimize the difference between operation with and
8647 * without logind, we explicitly enable non-blocking mode for this,
8648 * as logind's shutdown operations are always non-blocking. */
8649 arg_no_block = true1;
8650
8651 if (!arg_dry_run && !arg_force)
8652 return start_with_fallback();
8653
8654 assert(geteuid() == 0)do { if ((__builtin_expect(!!(!(geteuid() == 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("geteuid() == 0"), "../src/systemctl/systemctl.c"
, 8654, __PRETTY_FUNCTION__); } while (0)
;
8655
8656 if (!arg_no_wtmp) {
8657 if (sd_booted() > 0)
8658 log_debug("Not writing utmp record, assuming that systemd-update-utmp is used.")({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 8658, __func__, "Not writing utmp record, assuming that systemd-update-utmp is used."
) : -abs(_e); })
;
8659 else {
8660 r = utmp_put_shutdown();
8661 if (r < 0)
8662 log_warning_errno(r, "Failed to write utmp record: %m")({ int _level = ((4)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 8662, __func__, "Failed to write utmp record: %m"
) : -abs(_e); })
;
8663 }
8664 }
8665
8666 if (arg_dry_run)
8667 return 0;
8668
8669 r = halt_now(arg_action);
8670 return log_error_errno(r, "Failed to reboot: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 8670, __func__, "Failed to reboot: %m"
) : -abs(_e); })
;
8671}
8672
8673static int runlevel_main(void) {
8674 int r, runlevel, previous;
8675
8676 r = utmp_get_runlevel(&runlevel, &previous);
8677 if (r < 0) {
8678 puts("unknown");
8679 return r;
8680 }
8681
8682 printf("%c %c\n",
8683 previous <= 0 ? 'N' : previous,
8684 runlevel <= 0 ? 'N' : runlevel);
8685
8686 return 0;
8687}
8688
8689static int logind_cancel_shutdown(void) {
8690#if ENABLE_LOGIND1
8691 _cleanup_(sd_bus_error_free)__attribute__((cleanup(sd_bus_error_free))) sd_bus_error error = SD_BUS_ERROR_NULL((const sd_bus_error) {(((void*)0)), (((void*)0)), 0});
8692 sd_bus *bus;
8693 int r;
8694
8695 r = acquire_bus(BUS_FULL, &bus);
8696 if (r < 0)
8697 return r;
8698
8699 (void) logind_set_wall_message();
8700
8701 r = sd_bus_call_method(
8702 bus,
8703 "org.freedesktop.login1",
8704 "/org/freedesktop/login1",
8705 "org.freedesktop.login1.Manager",
8706 "CancelScheduledShutdown",
8707 &error,
8708 NULL((void*)0), NULL((void*)0));
8709 if (r < 0)
8710 return log_warning_errno(r, "Failed to talk to logind, shutdown hasn't been cancelled: %s", bus_error_message(&error, r))({ int _level = ((4)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 8710, __func__, "Failed to talk to logind, shutdown hasn't been cancelled: %s"
, bus_error_message(&error, r)) : -abs(_e); })
;
8711
8712 return 0;
8713#else
8714 log_error("Not compiled with logind support, cannot cancel scheduled shutdowns.")({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 8714, __func__, "Not compiled with logind support, cannot cancel scheduled shutdowns."
) : -abs(_e); })
;
8715 return -ENOSYS38;
8716#endif
8717}
8718
8719int main(int argc, char*argv[]) {
8720 int r;
8721
8722 argv_cmdline = argv[0];
8723
8724 setlocale(LC_ALL6, "");
8725 log_parse_environment()log_parse_environment_realm(LOG_REALM_SYSTEMD);
8726 log_open();
8727 sigbus_install();
8728
8729 /* Explicitly not on_tty() to avoid setting cached value.
8730 * This becomes relevant for piping output which might be
8731 * ellipsized. */
8732 original_stdout_is_tty = isatty(STDOUT_FILENO1);
8733
8734 r = parse_argv(argc, argv);
8735 if (r <= 0)
8736 goto finish;
8737
8738 if (arg_action != ACTION_SYSTEMCTL && running_in_chroot() > 0) {
8739
8740 if (!arg_quiet)
8741 log_info("Running in chroot, ignoring request.")({ int _level = (((6))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/systemctl/systemctl.c", 8741, __func__, "Running in chroot, ignoring request."
) : -abs(_e); })
;
8742 r = 0;
8743 goto finish;
8744 }
8745
8746 /* systemctl_main() will print an error message for the bus
8747 * connection, but only if it needs to */
8748
8749 switch (arg_action) {
8750
8751 case ACTION_SYSTEMCTL:
8752 r = systemctl_main(argc, argv);
8753 break;
8754
8755 /* Legacy command aliases set arg_action. They provide some fallbacks,
8756 * e.g. to tell sysvinit to reboot after you have installed systemd
8757 * binaries. */
8758
8759 case ACTION_HALT:
8760 case ACTION_POWEROFF:
8761 case ACTION_REBOOT:
8762 case ACTION_KEXEC:
8763 r = halt_main();
8764 break;
8765
8766 case ACTION_RUNLEVEL2:
8767 case ACTION_RUNLEVEL3:
8768 case ACTION_RUNLEVEL4:
8769 case ACTION_RUNLEVEL5:
8770 case ACTION_RESCUE:
8771 r = start_with_fallback();
8772 break;
8773
8774 case ACTION_RELOAD:
8775 case ACTION_REEXEC:
8776 r = reload_with_fallback();
8777 break;
8778
8779 case ACTION_CANCEL_SHUTDOWN:
8780 r = logind_cancel_shutdown();
8781 break;
8782
8783 case ACTION_RUNLEVEL:
8784 r = runlevel_main();
8785 break;
8786
8787 case ACTION_EXIT:
8788 case ACTION_SUSPEND:
8789 case ACTION_HIBERNATE:
8790 case ACTION_HYBRID_SLEEP:
8791 case ACTION_SUSPEND_THEN_HIBERNATE:
8792 case ACTION_EMERGENCY:
8793 case ACTION_DEFAULT:
8794 /* systemctl verbs with no equivalent in the legacy commands.
8795 * These cannot appear in arg_action. Fall through. */
8796
8797 case _ACTION_INVALID:
8798 default:
8799 assert_not_reached("Unknown action")do { log_assert_failed_unreachable_realm(LOG_REALM_SYSTEMD, (
"Unknown action"), "../src/systemctl/systemctl.c", 8799, __PRETTY_FUNCTION__
); } while (0)
;
8800 }
8801
8802finish:
8803 release_busses();
8804
8805 pager_close();
8806 ask_password_agent_close();
8807 polkit_agent_close();
8808
8809 strv_free(arg_types);
8810 strv_free(arg_states);
8811 strv_free(arg_properties);
8812
8813 strv_free(arg_wall);
8814 free(arg_root);
8815 free(arg_esp_path);
8816
8817 /* Note that we return r here, not EXIT_SUCCESS, so that we can implement the LSB-like return codes */
8818 return r < 0 ? EXIT_FAILURE1 : r;
8819}