1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
9 #include "bus-print-properties.h"
11 #include "capsule-util.h"
12 #include "extract-word.h"
13 #include "image-policy.h"
15 #include "output-mode.h"
17 #include "parse-argument.h"
18 #include "parse-util.h"
19 #include "pretty-print.h"
20 #include "static-destruct.h"
21 #include "string-table.h"
22 #include "string-util.h"
24 #include "systemctl.h"
25 #include "systemctl-compat-halt.h"
26 #include "systemctl-compat-runlevel.h"
27 #include "systemctl-compat-shutdown.h"
28 #include "systemctl-compat-telinit.h"
29 #include "systemctl-logind.h"
30 #include "time-util.h"
32 char **arg_types
= NULL
;
33 char **arg_states
= NULL
;
34 char **arg_properties
= NULL
;
36 enum dependency arg_dependency
= DEPENDENCY_FORWARD
;
37 const char *_arg_job_mode
= NULL
;
38 RuntimeScope arg_runtime_scope
= RUNTIME_SCOPE_SYSTEM
;
39 bool arg_wait
= false;
40 bool arg_no_block
= false;
41 int arg_legend
= -1; /* -1: true, unless --quiet is passed, 1: true */
42 PagerFlags arg_pager_flags
= 0;
43 bool arg_no_wtmp
= false;
44 bool arg_no_sync
= false;
45 bool arg_no_wall
= false;
46 bool arg_no_reload
= false;
47 BusPrintPropertyFlags arg_print_flags
= 0;
48 bool arg_show_types
= false;
49 int arg_check_inhibitors
= -1;
50 bool arg_dry_run
= false;
51 bool arg_quiet
= false;
52 bool arg_verbose
= false;
53 bool arg_no_warn
= false;
54 bool arg_full
= false;
55 bool arg_recursive
= false;
56 bool arg_with_dependencies
= false;
57 bool arg_show_transaction
= false;
59 bool arg_ask_password
= false;
60 bool arg_runtime
= false;
61 UnitFilePresetMode arg_preset_mode
= UNIT_FILE_PRESET_FULL
;
62 char **arg_wall
= NULL
;
63 const char *arg_kill_whom
= NULL
;
64 int arg_signal
= SIGTERM
;
66 bool arg_kill_value_set
= false;
67 char *arg_root
= NULL
;
68 char *arg_image
= NULL
;
70 bool arg_stdin
= false;
71 const char *arg_reboot_argument
= NULL
;
72 enum action arg_action
= ACTION_SYSTEMCTL
;
73 BusTransport arg_transport
= BUS_TRANSPORT_LOCAL
;
74 const char *arg_host
= NULL
;
75 unsigned arg_lines
= 10;
76 OutputMode arg_output
= OUTPUT_SHORT
;
77 bool arg_plain
= false;
78 bool arg_firmware_setup
= false;
79 usec_t arg_boot_loader_menu
= USEC_INFINITY
;
80 const char *arg_boot_loader_entry
= NULL
;
82 bool arg_jobs_before
= false;
83 bool arg_jobs_after
= false;
84 char **arg_clean_what
= NULL
;
85 TimestampStyle arg_timestamp_style
= TIMESTAMP_PRETTY
;
86 bool arg_read_only
= false;
87 bool arg_mkdir
= false;
88 bool arg_marked
= false;
89 const char *arg_drop_in
= NULL
;
90 ImagePolicy
*arg_image_policy
= NULL
;
92 STATIC_DESTRUCTOR_REGISTER(arg_types
, strv_freep
);
93 STATIC_DESTRUCTOR_REGISTER(arg_states
, strv_freep
);
94 STATIC_DESTRUCTOR_REGISTER(arg_properties
, strv_freep
);
95 STATIC_DESTRUCTOR_REGISTER(_arg_job_mode
, unsetp
);
96 STATIC_DESTRUCTOR_REGISTER(arg_wall
, strv_freep
);
97 STATIC_DESTRUCTOR_REGISTER(arg_kill_whom
, unsetp
);
98 STATIC_DESTRUCTOR_REGISTER(arg_root
, freep
);
99 STATIC_DESTRUCTOR_REGISTER(arg_image
, freep
);
100 STATIC_DESTRUCTOR_REGISTER(arg_reboot_argument
, unsetp
);
101 STATIC_DESTRUCTOR_REGISTER(arg_host
, unsetp
);
102 STATIC_DESTRUCTOR_REGISTER(arg_boot_loader_entry
, unsetp
);
103 STATIC_DESTRUCTOR_REGISTER(arg_clean_what
, strv_freep
);
104 STATIC_DESTRUCTOR_REGISTER(arg_drop_in
, unsetp
);
105 STATIC_DESTRUCTOR_REGISTER(arg_image_policy
, image_policy_freep
);
107 static int systemctl_help(void) {
108 _cleanup_free_
char *link
= NULL
;
111 pager_open(arg_pager_flags
);
113 r
= terminal_urlify_man("systemctl", "1", &link
);
117 printf("%1$s [OPTIONS...] COMMAND ...\n\n"
118 "%5$sQuery or send control commands to the system manager.%6$s\n"
119 "\n%3$sUnit Commands:%4$s\n"
120 " list-units [PATTERN...] List units currently in memory\n"
121 " list-automounts [PATTERN...] List automount units currently in memory,\n"
123 " list-paths [PATTERN...] List path units currently in memory,\n"
125 " list-sockets [PATTERN...] List socket units currently in memory,\n"
126 " ordered by address\n"
127 " list-timers [PATTERN...] List timer units currently in memory,\n"
128 " ordered by next elapse\n"
129 " is-active PATTERN... Check whether units are active\n"
130 " is-failed [PATTERN...] Check whether units are failed or\n"
131 " system is in degraded state\n"
132 " status [PATTERN...|PID...] Show runtime status of one or more units\n"
133 " show [PATTERN...|JOB...] Show properties of one or more\n"
134 " units/jobs or the manager\n"
135 " cat PATTERN... Show files and drop-ins of specified units\n"
136 " help PATTERN...|PID... Show manual for one or more units\n"
137 " list-dependencies [UNIT...] Recursively show units which are required\n"
138 " or wanted by the units or by which those\n"
139 " units are required or wanted\n"
140 " start UNIT... Start (activate) one or more units\n"
141 " stop UNIT... Stop (deactivate) one or more units\n"
142 " reload UNIT... Reload one or more units\n"
143 " restart UNIT... Start or restart one or more units\n"
144 " try-restart UNIT... Restart one or more units if active\n"
145 " reload-or-restart UNIT... Reload one or more units if possible,\n"
146 " otherwise start or restart\n"
147 " try-reload-or-restart UNIT... If active, reload one or more units,\n"
148 " if supported, otherwise restart\n"
149 " isolate UNIT Start one unit and stop all others\n"
150 " kill UNIT... Send signal to processes of a unit\n"
151 " clean UNIT... Clean runtime, cache, state, logs or\n"
152 " configuration of unit\n"
153 " freeze PATTERN... Freeze execution of unit processes\n"
154 " thaw PATTERN... Resume execution of a frozen unit\n"
155 " set-property UNIT PROPERTY=VALUE... Sets one or more properties of a unit\n"
156 " bind UNIT PATH [PATH] Bind-mount a path from the host into a\n"
157 " unit's namespace\n"
158 " mount-image UNIT PATH [PATH [OPTS]] Mount an image from the host into a\n"
159 " unit's namespace\n"
160 " service-log-level SERVICE [LEVEL] Get/set logging threshold for service\n"
161 " service-log-target SERVICE [TARGET] Get/set logging target for service\n"
162 " reset-failed [PATTERN...] Reset failed state for all, one, or more\n"
164 " whoami [PID...] Return unit caller or specified PIDs are\n"
166 "\n%3$sUnit File Commands:%4$s\n"
167 " list-unit-files [PATTERN...] List installed unit files\n"
168 " enable [UNIT...|PATH...] Enable one or more unit files\n"
169 " disable UNIT... Disable one or more unit files\n"
170 " reenable UNIT... Reenable one or more unit files\n"
171 " preset UNIT... Enable/disable one or more unit files\n"
172 " based on preset configuration\n"
173 " preset-all Enable/disable all unit files based on\n"
174 " preset configuration\n"
175 " is-enabled UNIT... Check whether unit files are enabled\n"
176 " mask UNIT... Mask one or more units\n"
177 " unmask UNIT... Unmask one or more units\n"
178 " link PATH... Link one or more units files into\n"
180 " revert UNIT... Revert one or more unit files to vendor\n"
182 " add-wants TARGET UNIT... Add 'Wants' dependency for the target\n"
183 " on specified one or more units\n"
184 " add-requires TARGET UNIT... Add 'Requires' dependency for the target\n"
185 " on specified one or more units\n"
186 " edit UNIT... Edit one or more unit files\n"
187 " get-default Get the name of the default target\n"
188 " set-default TARGET Set the default target\n"
189 "\n%3$sMachine Commands:%4$s\n"
190 " list-machines [PATTERN...] List local containers and host\n"
191 "\n%3$sJob Commands:%4$s\n"
192 " list-jobs [PATTERN...] List jobs\n"
193 " cancel [JOB...] Cancel all, one, or more jobs\n"
194 "\n%3$sEnvironment Commands:%4$s\n"
195 " show-environment Dump environment\n"
196 " set-environment VARIABLE=VALUE... Set one or more environment variables\n"
197 " unset-environment VARIABLE... Unset one or more environment variables\n"
198 " import-environment VARIABLE... Import all or some environment variables\n"
199 "\n%3$sManager State Commands:%4$s\n"
200 " daemon-reload Reload systemd manager configuration\n"
201 " daemon-reexec Reexecute systemd manager\n"
202 " log-level [LEVEL] Get/set logging threshold for manager\n"
203 " log-target [TARGET] Get/set logging target for manager\n"
204 " service-watchdogs [BOOL] Get/set service watchdog state\n"
205 "\n%3$sSystem Commands:%4$s\n"
206 " is-system-running Check whether system is fully running\n"
207 " default Enter system default mode\n"
208 " rescue Enter system rescue mode\n"
209 " emergency Enter system emergency mode\n"
210 " halt Shut down and halt the system\n"
211 " poweroff Shut down and power-off the system\n"
212 " reboot Shut down and reboot the system\n"
213 " kexec Shut down and reboot the system with kexec\n"
214 " soft-reboot Shut down and reboot userspace\n"
215 " exit [EXIT_CODE] Request user instance or container exit\n"
216 " switch-root [ROOT [INIT]] Change to a different root file system\n"
217 " sleep Put the system to sleep (through one of\n"
218 " the operations below)\n"
219 " suspend Suspend the system\n"
220 " hibernate Hibernate the system\n"
221 " hybrid-sleep Hibernate and suspend the system\n"
222 " suspend-then-hibernate Suspend the system, wake after a period of\n"
223 " time, and hibernate"
224 "\n%3$sOptions:%4$s\n"
225 " -h --help Show this help\n"
226 " --version Show package version\n"
227 " --system Connect to system manager\n"
228 " --user Connect to user service manager\n"
229 " -C --capsule=NAME Connect to service manager of specified capsule\n"
230 " -H --host=[USER@]HOST Operate on remote host\n"
231 " -M --machine=CONTAINER Operate on a local container\n"
232 " -t --type=TYPE List units of a particular type\n"
233 " --state=STATE List units with particular LOAD or SUB or ACTIVE state\n"
234 " --failed Shortcut for --state=failed\n"
235 " -p --property=NAME Show only properties by this name\n"
236 " -P NAME Equivalent to --value --property=NAME\n"
237 " -a --all Show all properties/all units currently in memory,\n"
238 " including dead/empty ones. To list all units installed\n"
239 " on the system, use 'list-unit-files' instead.\n"
240 " -l --full Don't ellipsize unit names on output\n"
241 " -r --recursive Show unit list of host and local containers\n"
242 " --reverse Show reverse dependencies with 'list-dependencies'\n"
243 " --before Show units ordered before with 'list-dependencies'\n"
244 " --after Show units ordered after with 'list-dependencies'\n"
245 " --with-dependencies Show unit dependencies with 'status', 'cat',\n"
246 " 'list-units', and 'list-unit-files'.\n"
247 " --job-mode=MODE Specify how to deal with already queued jobs, when\n"
248 " queueing a new job\n"
249 " -T --show-transaction When enqueuing a unit job, show full transaction\n"
250 " --show-types When showing sockets, explicitly show their type\n"
251 " --value When showing properties, only print the value\n"
252 " --check-inhibitors=MODE\n"
253 " Whether to check inhibitors before shutting down,\n"
254 " sleeping, or hibernating\n"
255 " -i Shortcut for --check-inhibitors=no\n"
256 " --kill-whom=WHOM Whom to send signal to\n"
257 " --kill-value=INT Signal value to enqueue\n"
258 " -s --signal=SIGNAL Which signal to send\n"
259 " --what=RESOURCES Which types of resources to remove\n"
260 " --now Start or stop unit after enabling or disabling it\n"
261 " --dry-run Only print what would be done\n"
262 " Currently supported by verbs: halt, poweroff, reboot,\n"
263 " kexec, soft-reboot, suspend, hibernate, \n"
264 " suspend-then-hibernate, hybrid-sleep, default,\n"
265 " rescue, emergency, and exit.\n"
266 " -q --quiet Suppress output\n"
267 " -v --verbose Show unit logs while executing operation\n"
268 " --no-warn Suppress several warnings shown by default\n"
269 " --wait For (re)start, wait until service stopped again\n"
270 " For is-system-running, wait until startup is completed\n"
271 " For kill, wait until service stopped\n"
272 " --no-block Do not wait until operation finished\n"
273 " --no-wall Don't send wall message before halt/power-off/reboot\n"
274 " --message=MESSAGE Specify human readable reason for system shutdown\n"
275 " --no-reload Don't reload daemon after en-/dis-abling unit files\n"
276 " --legend=BOOL Enable/disable the legend (column headers and hints)\n"
277 " --no-pager Do not pipe output into a pager\n"
278 " --no-ask-password Do not ask for system passwords\n"
279 " --global Edit/enable/disable/mask default user unit files\n"
281 " --runtime Edit/enable/disable/mask unit files temporarily until\n"
283 " -f --force When enabling unit files, override existing symlinks\n"
284 " When shutting down, execute action immediately\n"
285 " --preset-mode= Apply only enable, only disable, or all presets\n"
286 " --root=PATH Edit/enable/disable/mask unit files in the specified\n"
288 " --image=PATH Edit/enable/disable/mask unit files in the specified\n"
290 " --image-policy=POLICY\n"
291 " Specify disk image dissection policy\n"
292 " -n --lines=INTEGER Number of journal entries to show\n"
293 " -o --output=STRING Change journal output mode (short, short-precise,\n"
294 " short-iso, short-iso-precise, short-full,\n"
295 " short-monotonic, short-unix, short-delta,\n"
296 " verbose, export, json, json-pretty, json-sse, cat)\n"
297 " --firmware-setup Tell the firmware to show the setup menu on next boot\n"
298 " --boot-loader-menu=TIME\n"
299 " Boot into boot loader menu on next boot\n"
300 " --boot-loader-entry=NAME\n"
301 " Boot into a specific boot loader entry on next boot\n"
302 " --reboot-argument=ARG\n"
303 " Specify argument string to pass to reboot()\n"
304 " --plain Print unit dependencies as a list instead of a tree\n"
305 " --timestamp=FORMAT Change format of printed timestamps (pretty, unix,\n"
306 " us, utc, us+utc)\n"
307 " --read-only Create read-only bind mount\n"
308 " --mkdir Create directory before mounting, if missing\n"
309 " --marked Restart/reload previously marked units\n"
310 " --drop-in=NAME Edit unit files using the specified drop-in file name\n"
311 " --when=TIME Schedule halt/power-off/reboot/kexec action after\n"
312 " a certain timestamp\n"
313 " --stdin Read new contents of edited file from stdin\n"
314 "\nSee the %2$s for details.\n",
315 program_invocation_short_name
,
325 static void help_types(void) {
327 puts("Available unit types:");
329 DUMP_STRING_TABLE(unit_type
, UnitType
, _UNIT_TYPE_MAX
);
332 static void help_states(void) {
334 puts("Available unit load states:");
335 DUMP_STRING_TABLE(unit_load_state
, UnitLoadState
, _UNIT_LOAD_STATE_MAX
);
338 puts("\nAvailable unit active states:");
339 DUMP_STRING_TABLE(unit_active_state
, UnitActiveState
, _UNIT_ACTIVE_STATE_MAX
);
342 puts("\nAvailable unit file states:");
343 DUMP_STRING_TABLE(unit_file_state
, UnitFileState
, _UNIT_FILE_STATE_MAX
);
346 puts("\nAvailable automount unit substates:");
347 DUMP_STRING_TABLE(automount_state
, AutomountState
, _AUTOMOUNT_STATE_MAX
);
350 puts("\nAvailable device unit substates:");
351 DUMP_STRING_TABLE(device_state
, DeviceState
, _DEVICE_STATE_MAX
);
354 puts("\nAvailable mount unit substates:");
355 DUMP_STRING_TABLE(mount_state
, MountState
, _MOUNT_STATE_MAX
);
358 puts("\nAvailable path unit substates:");
359 DUMP_STRING_TABLE(path_state
, PathState
, _PATH_STATE_MAX
);
362 puts("\nAvailable scope unit substates:");
363 DUMP_STRING_TABLE(scope_state
, ScopeState
, _SCOPE_STATE_MAX
);
366 puts("\nAvailable service unit substates:");
367 DUMP_STRING_TABLE(service_state
, ServiceState
, _SERVICE_STATE_MAX
);
370 puts("\nAvailable slice unit substates:");
371 DUMP_STRING_TABLE(slice_state
, SliceState
, _SLICE_STATE_MAX
);
374 puts("\nAvailable socket unit substates:");
375 DUMP_STRING_TABLE(socket_state
, SocketState
, _SOCKET_STATE_MAX
);
378 puts("\nAvailable swap unit substates:");
379 DUMP_STRING_TABLE(swap_state
, SwapState
, _SWAP_STATE_MAX
);
382 puts("\nAvailable target unit substates:");
383 DUMP_STRING_TABLE(target_state
, TargetState
, _TARGET_STATE_MAX
);
386 puts("\nAvailable timer unit substates:");
387 DUMP_STRING_TABLE(timer_state
, TimerState
, _TIMER_STATE_MAX
);
390 static int systemctl_parse_argv(int argc
, char *argv
[]) {
392 ARG_FAIL
= 0x100, /* compatibility only */
396 ARG_CHECK_INHIBITORS
,
399 ARG_IRREVERSIBLE
, /* compatibility only */
400 ARG_IGNORE_DEPENDENCIES
, /* compatibility only */
408 ARG_NO_LEGEND
, /* compatibility only */
425 ARG_BOOT_LOADER_MENU
,
426 ARG_BOOT_LOADER_ENTRY
,
429 ARG_WITH_DEPENDENCIES
,
443 static const struct option options
[] = {
444 { "help", no_argument
, NULL
, 'h' },
445 { "version", no_argument
, NULL
, ARG_VERSION
},
446 { "type", required_argument
, NULL
, 't' },
447 { "property", required_argument
, NULL
, 'p' },
448 { "all", no_argument
, NULL
, 'a' },
449 { "reverse", no_argument
, NULL
, ARG_REVERSE
},
450 { "after", no_argument
, NULL
, ARG_AFTER
},
451 { "before", no_argument
, NULL
, ARG_BEFORE
},
452 { "show-types", no_argument
, NULL
, ARG_SHOW_TYPES
},
453 { "failed", no_argument
, NULL
, ARG_FAILED
},
454 { "full", no_argument
, NULL
, 'l' },
455 { "job-mode", required_argument
, NULL
, ARG_JOB_MODE
},
456 { "fail", no_argument
, NULL
, ARG_FAIL
}, /* compatibility only */
457 { "irreversible", no_argument
, NULL
, ARG_IRREVERSIBLE
}, /* compatibility only */
458 { "ignore-dependencies", no_argument
, NULL
, ARG_IGNORE_DEPENDENCIES
}, /* compatibility only */
459 { "ignore-inhibitors", no_argument
, NULL
, 'i' }, /* compatibility only */
460 { "check-inhibitors", required_argument
, NULL
, ARG_CHECK_INHIBITORS
},
461 { "value", no_argument
, NULL
, ARG_VALUE
},
462 { "user", no_argument
, NULL
, ARG_USER
},
463 { "system", no_argument
, NULL
, ARG_SYSTEM
},
464 { "global", no_argument
, NULL
, ARG_GLOBAL
},
465 { "capsule", required_argument
, NULL
, 'C' },
466 { "wait", no_argument
, NULL
, ARG_WAIT
},
467 { "no-block", no_argument
, NULL
, ARG_NO_BLOCK
},
468 { "legend", required_argument
, NULL
, ARG_LEGEND
},
469 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
}, /* compatibility only */
470 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
471 { "no-wall", no_argument
, NULL
, ARG_NO_WALL
},
472 { "dry-run", no_argument
, NULL
, ARG_DRY_RUN
},
473 { "quiet", no_argument
, NULL
, 'q' },
474 { "verbose", no_argument
, NULL
, 'v' },
475 { "no-warn", no_argument
, NULL
, ARG_NO_WARN
},
476 { "root", required_argument
, NULL
, ARG_ROOT
},
477 { "image", required_argument
, NULL
, ARG_IMAGE
},
478 { "image-policy", required_argument
, NULL
, ARG_IMAGE_POLICY
},
479 { "force", no_argument
, NULL
, 'f' },
480 { "no-reload", no_argument
, NULL
, ARG_NO_RELOAD
},
481 { "kill-whom", required_argument
, NULL
, ARG_KILL_WHOM
},
482 { "kill-value", required_argument
, NULL
, ARG_KILL_VALUE
},
483 { "signal", required_argument
, NULL
, 's' },
484 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
485 { "host", required_argument
, NULL
, 'H' },
486 { "machine", required_argument
, NULL
, 'M' },
487 { "runtime", no_argument
, NULL
, ARG_RUNTIME
},
488 { "lines", required_argument
, NULL
, 'n' },
489 { "output", required_argument
, NULL
, 'o' },
490 { "plain", no_argument
, NULL
, ARG_PLAIN
},
491 { "state", required_argument
, NULL
, ARG_STATE
},
492 { "recursive", no_argument
, NULL
, 'r' },
493 { "with-dependencies", no_argument
, NULL
, ARG_WITH_DEPENDENCIES
},
494 { "preset-mode", required_argument
, NULL
, ARG_PRESET_MODE
},
495 { "firmware-setup", no_argument
, NULL
, ARG_FIRMWARE_SETUP
},
496 { "boot-loader-menu", required_argument
, NULL
, ARG_BOOT_LOADER_MENU
},
497 { "boot-loader-entry", required_argument
, NULL
, ARG_BOOT_LOADER_ENTRY
},
498 { "now", no_argument
, NULL
, ARG_NOW
},
499 { "message", required_argument
, NULL
, ARG_MESSAGE
},
500 { "show-transaction", no_argument
, NULL
, 'T' },
501 { "what", required_argument
, NULL
, ARG_WHAT
},
502 { "reboot-argument", required_argument
, NULL
, ARG_REBOOT_ARG
},
503 { "timestamp", required_argument
, NULL
, ARG_TIMESTAMP_STYLE
},
504 { "read-only", no_argument
, NULL
, ARG_READ_ONLY
},
505 { "mkdir", no_argument
, NULL
, ARG_MKDIR
},
506 { "marked", no_argument
, NULL
, ARG_MARKED
},
507 { "drop-in", required_argument
, NULL
, ARG_DROP_IN
},
508 { "when", required_argument
, NULL
, ARG_WHEN
},
509 { "stdin", no_argument
, NULL
, ARG_STDIN
},
518 /* We default to allowing interactive authorization only in systemctl (not in the legacy commands) */
519 arg_ask_password
= true;
521 while ((c
= getopt_long(argc
, argv
, "hC:t:p:P:alqvfs:H:M:n:o:iTr.::", options
, NULL
)) >= 0)
526 return systemctl_help();
533 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
534 "--type= requires arguments.");
536 for (const char *p
= optarg
;;) {
537 _cleanup_free_
char *type
= NULL
;
539 r
= extract_first_word(&p
, &type
, ",", 0);
541 return log_error_errno(r
, "Failed to parse type: %s", optarg
);
545 if (streq(type
, "help")) {
550 if (unit_type_from_string(type
) >= 0) {
551 if (strv_consume(&arg_types
, TAKE_PTR(type
)) < 0)
556 /* It's much nicer to use --state= for load states, but let's support this in
557 * --types= too for compatibility with old versions */
558 if (unit_load_state_from_string(type
) >= 0) {
559 if (strv_consume(&arg_states
, TAKE_PTR(type
)) < 0)
564 log_error("Unknown unit type or load state '%s'.", type
);
565 return log_info_errno(SYNTHETIC_ERRNO(EINVAL
),
566 "Use -t help to see a list of allowed values.");
572 SET_FLAG(arg_print_flags
, BUS_PRINT_PROPERTY_ONLY_VALUE
, true);
576 /* Make sure that if the empty property list was specified, we won't show any
578 if (isempty(optarg
) && !arg_properties
) {
579 arg_properties
= new0(char*, 1);
583 for (const char *p
= optarg
;;) {
584 _cleanup_free_
char *prop
= NULL
;
586 r
= extract_first_word(&p
, &prop
, ",", 0);
588 return log_error_errno(r
, "Failed to parse property: %s", optarg
);
592 if (strv_consume(&arg_properties
, TAKE_PTR(prop
)) < 0)
596 /* If the user asked for a particular property, show it, even if it is empty. */
597 SET_FLAG(arg_print_flags
, BUS_PRINT_PROPERTY_SHOW_EMPTY
, true);
602 SET_FLAG(arg_print_flags
, BUS_PRINT_PROPERTY_SHOW_EMPTY
, true);
607 arg_dependency
= DEPENDENCY_REVERSE
;
611 arg_dependency
= DEPENDENCY_AFTER
;
612 arg_jobs_after
= true;
616 arg_dependency
= DEPENDENCY_BEFORE
;
617 arg_jobs_before
= true;
621 arg_show_types
= true;
625 SET_FLAG(arg_print_flags
, BUS_PRINT_PROPERTY_ONLY_VALUE
, true);
629 _arg_job_mode
= optarg
;
633 _arg_job_mode
= "fail";
636 case ARG_IRREVERSIBLE
:
637 _arg_job_mode
= "replace-irreversibly";
640 case ARG_IGNORE_DEPENDENCIES
:
641 _arg_job_mode
= "ignore-dependencies";
645 arg_runtime_scope
= RUNTIME_SCOPE_USER
;
649 arg_runtime_scope
= RUNTIME_SCOPE_SYSTEM
;
653 arg_runtime_scope
= RUNTIME_SCOPE_GLOBAL
;
657 r
= capsule_name_is_valid(optarg
);
659 return log_error_errno(r
, "Unable to validate capsule name '%s': %m", optarg
);
661 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Invalid capsule name: %s", optarg
);
664 arg_transport
= BUS_TRANSPORT_CAPSULE
;
665 arg_runtime_scope
= RUNTIME_SCOPE_USER
;
681 r
= parse_boolean_argument("--legend", optarg
, NULL
);
688 arg_pager_flags
|= PAGER_DISABLE
;
696 r
= parse_path_argument(optarg
, false, &arg_root
);
702 r
= parse_path_argument(optarg
, false, &arg_image
);
707 case ARG_IMAGE_POLICY
:
708 r
= parse_image_policy_argument(optarg
, &arg_image_policy
);
718 if (strv_extend(&arg_states
, "failed") < 0)
744 arg_no_reload
= true;
748 arg_kill_whom
= optarg
;
751 case ARG_KILL_VALUE
: {
754 if (isempty(optarg
)) {
755 arg_kill_value_set
= false;
759 /* First, try to parse unsigned, so that we can support the prefixes 0x, 0o, 0b */
760 r
= safe_atou_full(optarg
, 0, &u
);
762 /* If this didn't work, try as signed integer, without those prefixes */
763 r
= safe_atoi(optarg
, &arg_kill_value
);
764 else if (u
> INT_MAX
)
767 arg_kill_value
= (int) u
;
769 return log_error_errno(r
, "Unable to parse signal queue value: %s", optarg
);
771 arg_kill_value_set
= true;
776 r
= parse_signal_argument(optarg
, &arg_signal
);
781 case ARG_NO_ASK_PASSWORD
:
782 arg_ask_password
= false;
786 arg_transport
= BUS_TRANSPORT_REMOTE
;
791 r
= parse_machine_argument(optarg
, &arg_host
, &arg_transport
);
801 if (safe_atou(optarg
, &arg_lines
) < 0)
802 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
803 "Failed to parse lines '%s'",
808 if (streq(optarg
, "help")) {
809 DUMP_STRING_TABLE(output_mode
, OutputMode
, _OUTPUT_MODE_MAX
);
813 arg_output
= output_mode_from_string(optarg
);
815 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
816 "Unknown output '%s'.",
819 if (OUTPUT_MODE_IS_JSON(arg_output
)) {
826 arg_check_inhibitors
= 0;
829 case ARG_CHECK_INHIBITORS
:
830 r
= parse_tristate_full(optarg
, "auto", &arg_check_inhibitors
);
832 return log_error_errno(r
, "Failed to parse --check-inhibitors= argument: %s", optarg
);
839 case ARG_FIRMWARE_SETUP
:
840 arg_firmware_setup
= true;
843 case ARG_BOOT_LOADER_MENU
:
845 r
= parse_sec(optarg
, &arg_boot_loader_menu
);
847 return log_error_errno(r
, "Failed to parse --boot-loader-menu= argument '%s': %m", optarg
);
851 case ARG_BOOT_LOADER_ENTRY
:
853 if (streq(optarg
, "help")) { /* Yes, this means, "help" is not a valid boot loader entry name we can deal with */
854 r
= help_boot_loader_entry();
861 arg_boot_loader_entry
= empty_to_null(optarg
);
866 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
867 "--state= requires arguments.");
869 for (const char *p
= optarg
;;) {
870 _cleanup_free_
char *s
= NULL
;
872 r
= extract_first_word(&p
, &s
, ",", 0);
874 return log_error_errno(r
, "Failed to parse state: %s", optarg
);
878 if (streq(s
, "help")) {
883 if (strv_consume(&arg_states
, TAKE_PTR(s
)) < 0)
890 return log_error_errno(SYNTHETIC_ERRNO(EPERM
),
891 "--recursive requires root privileges.");
893 arg_recursive
= true;
896 case ARG_PRESET_MODE
:
897 if (streq(optarg
, "help")) {
898 DUMP_STRING_TABLE(unit_file_preset_mode
, UnitFilePresetMode
, _UNIT_FILE_PRESET_MODE_MAX
);
902 arg_preset_mode
= unit_file_preset_mode_from_string(optarg
);
903 if (arg_preset_mode
< 0)
904 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
905 "Failed to parse preset mode: %s.", optarg
);
914 if (strv_extend(&arg_wall
, optarg
) < 0)
919 arg_show_transaction
= true;
922 case ARG_WITH_DEPENDENCIES
:
923 arg_with_dependencies
= true;
928 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "--what= requires arguments (see --what=help).");
930 for (const char *p
= optarg
;;) {
931 _cleanup_free_
char *k
= NULL
;
933 r
= extract_first_word(&p
, &k
, ",", 0);
935 return log_error_errno(r
, "Failed to parse directory type: %s", optarg
);
939 if (streq(k
, "help")) {
950 r
= strv_consume(&arg_clean_what
, TAKE_PTR(k
));
958 arg_reboot_argument
= optarg
;
961 case ARG_TIMESTAMP_STYLE
:
962 if (streq(optarg
, "help")) {
963 DUMP_STRING_TABLE(timestamp_style
, TimestampStyle
, _TIMESTAMP_STYLE_MAX
);
967 arg_timestamp_style
= timestamp_style_from_string(optarg
);
968 if (arg_timestamp_style
< 0)
969 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
970 "Invalid value: %s.", optarg
);
975 arg_read_only
= true;
991 arg_drop_in
= optarg
;
995 if (streq(optarg
, "show")) {
996 arg_action
= ACTION_SYSTEMCTL_SHOW_SHUTDOWN
;
1000 if (STR_IN_SET(optarg
, "", "cancel")) {
1001 arg_action
= ACTION_CANCEL_SHUTDOWN
;
1005 if (streq(optarg
, "auto")) {
1006 arg_when
= USEC_INFINITY
; /* logind chooses on server side */
1010 r
= parse_timestamp(optarg
, &arg_when
);
1012 return log_error_errno(r
, "Failed to parse --when= argument '%s': %m", optarg
);
1014 if (!timestamp_is_set(arg_when
))
1015 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1016 "Invalid timestamp '%s' specified for --when=.", optarg
);
1025 /* Output an error mimicking getopt, and print a hint afterwards */
1026 log_error("%s: invalid option -- '.'", program_invocation_name
);
1027 log_notice("Hint: to specify units starting with a dash, use \"--\":\n"
1028 " %s [OPTIONS...] COMMAND -- -.%s ...",
1029 program_invocation_name
, optarg
?: "mount");
1036 assert_not_reached();
1039 /* If we are in --user mode, there's no point in talking to PolicyKit or the infra to query system
1041 if (arg_runtime_scope
!= RUNTIME_SCOPE_SYSTEM
)
1042 arg_ask_password
= false;
1044 if (arg_transport
== BUS_TRANSPORT_REMOTE
&& arg_runtime_scope
!= RUNTIME_SCOPE_SYSTEM
)
1045 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1046 "Cannot access user instance remotely.");
1048 if (arg_transport
== BUS_TRANSPORT_CAPSULE
&& arg_runtime_scope
!= RUNTIME_SCOPE_USER
)
1049 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1050 "Cannot access system instance with --capsule=/-C.");
1052 if (arg_wait
&& arg_no_block
)
1053 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1054 "--wait may not be combined with --no-block.");
1056 bool do_reload_or_restart
= streq_ptr(argv
[optind
], "reload-or-restart");
1058 if (!do_reload_or_restart
)
1059 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1060 "--marked may only be used with 'reload-or-restart'.");
1061 if (optind
+ 1 < argc
)
1062 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1063 "No additional arguments allowed with 'reload-or-restart --marked'.");
1065 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1066 "--marked --wait is not supported.");
1067 if (arg_show_transaction
)
1068 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1069 "--marked --show-transaction is not supported.");
1071 } else if (do_reload_or_restart
) {
1072 if (optind
+ 1 >= argc
)
1073 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1074 "List of units to restart/reload is required.");
1077 if (arg_image
&& arg_root
)
1078 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1079 "Please specify either --root= or --image=, the combination of both is not supported.");
1084 int systemctl_dispatch_parse_argv(int argc
, char *argv
[]) {
1088 if (invoked_as(argv
, "halt")) {
1089 arg_action
= ACTION_HALT
;
1090 return halt_parse_argv(argc
, argv
);
1092 } else if (invoked_as(argv
, "poweroff")) {
1093 arg_action
= ACTION_POWEROFF
;
1094 return halt_parse_argv(argc
, argv
);
1096 } else if (invoked_as(argv
, "reboot")) {
1097 arg_action
= ACTION_REBOOT
;
1098 return halt_parse_argv(argc
, argv
);
1100 } else if (invoked_as(argv
, "shutdown")) {
1101 arg_action
= ACTION_POWEROFF
;
1102 return shutdown_parse_argv(argc
, argv
);
1104 } else if (invoked_as(argv
, "init")) {
1106 /* Matches invocations as "init" as well as "telinit", which are synonymous when run
1107 * as PID != 1 on SysV.
1109 * On SysV "telinit" was the official command to communicate with PID 1, but "init" would
1110 * redirect itself to "telinit" if called with PID != 1. We follow the same logic here still,
1111 * though we add one level of indirection, as we implement "telinit" in "systemctl". Hence,
1112 * for us if you invoke "init" you get "systemd", but it will execve() "systemctl"
1113 * immediately with argv[] unmodified if PID is != 1. If you invoke "telinit" you directly
1114 * get "systemctl". In both cases we shall do the same thing, which is why we do
1115 * invoked_as(argv, "init") here, as a quick way to match both.
1117 * Also see redirect_telinit() in src/core/main.c. */
1119 arg_action
= _ACTION_INVALID
; /* telinit_parse_argv() will figure out the actual action we'll execute */
1120 return telinit_parse_argv(argc
, argv
);
1122 } else if (invoked_as(argv
, "runlevel")) {
1123 arg_action
= ACTION_RUNLEVEL
;
1124 return runlevel_parse_argv(argc
, argv
);
1127 arg_action
= ACTION_SYSTEMCTL
;
1128 return systemctl_parse_argv(argc
, argv
);