]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/systemctl/systemctl.c
zsh: remove _files prefixes
[thirdparty/systemd.git] / src / systemctl / systemctl.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
7e4249b9 2
7e4249b9 3#include <getopt.h>
0bbd8e18 4#include <signal.h>
03a7b521 5#include <unistd.h>
81527be1 6
76d62b63 7#include "argv-util.h"
d6b4d1c7 8#include "build.h"
0bbd8e18 9#include "bus-print-properties.h"
daf71ef6 10#include "bus-util.h"
2a17866b 11#include "capsule-util.h"
0bbd8e18
DDM
12#include "extract-word.h"
13#include "image-policy.h"
03a7b521 14#include "install.h"
daf71ef6 15#include "output-mode.h"
03a7b521 16#include "pager.h"
86beb213 17#include "parse-argument.h"
c94f6ab1 18#include "parse-util.h"
294bf0c3 19#include "pretty-print.h"
0bbd8e18 20#include "static-destruct.h"
8559e61d 21#include "string-table.h"
0bbd8e18
DDM
22#include "string-util.h"
23#include "strv.h"
1cf40697 24#include "systemctl.h"
daf71ef6
LP
25#include "systemctl-compat-halt.h"
26#include "systemctl-compat-runlevel.h"
27#include "systemctl-compat-shutdown.h"
28#include "systemctl-compat-telinit.h"
daf71ef6 29#include "systemctl-logind.h"
daf71ef6 30#include "time-util.h"
7e4249b9 31
daf71ef6
LP
32char **arg_types = NULL;
33char **arg_states = NULL;
34char **arg_properties = NULL;
35bool arg_all = false;
36enum dependency arg_dependency = DEPENDENCY_FORWARD;
a88f9dba 37const char *_arg_job_mode = NULL;
4870133b 38RuntimeScope arg_runtime_scope = RUNTIME_SCOPE_SYSTEM;
daf71ef6
LP
39bool arg_wait = false;
40bool arg_no_block = false;
6906da26 41int arg_legend = -1; /* -1: true, unless --quiet is passed, 1: true */
daf71ef6
LP
42PagerFlags arg_pager_flags = 0;
43bool arg_no_wtmp = false;
44bool arg_no_sync = false;
45bool arg_no_wall = false;
46bool arg_no_reload = false;
255b1fc8 47BusPrintPropertyFlags arg_print_flags = 0;
daf71ef6 48bool arg_show_types = false;
4327574f 49int arg_check_inhibitors = -1;
daf71ef6
LP
50bool arg_dry_run = false;
51bool arg_quiet = false;
c8aff47a 52bool arg_verbose = false;
108d35ac 53bool arg_no_warn = false;
daf71ef6
LP
54bool arg_full = false;
55bool arg_recursive = false;
56bool arg_with_dependencies = false;
57bool arg_show_transaction = false;
58int arg_force = 0;
59bool arg_ask_password = false;
60bool arg_runtime = false;
61UnitFilePresetMode arg_preset_mode = UNIT_FILE_PRESET_FULL;
62char **arg_wall = NULL;
4ccde410 63const char *arg_kill_whom = NULL;
daf71ef6 64int arg_signal = SIGTERM;
d06e6199
LP
65int arg_kill_value;
66bool arg_kill_value_set = false;
daf71ef6 67char *arg_root = NULL;
8aa3894e 68char *arg_image = NULL;
daf71ef6 69usec_t arg_when = 0;
329050c5 70bool arg_stdin = false;
daf71ef6
LP
71const char *arg_reboot_argument = NULL;
72enum action arg_action = ACTION_SYSTEMCTL;
73BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
74const char *arg_host = NULL;
75unsigned arg_lines = 10;
76OutputMode arg_output = OUTPUT_SHORT;
77bool arg_plain = false;
78bool arg_firmware_setup = false;
79usec_t arg_boot_loader_menu = USEC_INFINITY;
80const char *arg_boot_loader_entry = NULL;
81bool arg_now = false;
82bool arg_jobs_before = false;
83bool arg_jobs_after = false;
84char **arg_clean_what = NULL;
85TimestampStyle arg_timestamp_style = TIMESTAMP_PRETTY;
5e8deb94
LB
86bool arg_read_only = false;
87bool arg_mkdir = false;
c9615f73 88bool arg_marked = false;
f206809b 89const char *arg_drop_in = NULL;
84be0c71 90ImagePolicy *arg_image_policy = NULL;
e8630e69 91
61ff45db
LP
92STATIC_DESTRUCTOR_REGISTER(arg_types, strv_freep);
93STATIC_DESTRUCTOR_REGISTER(arg_states, strv_freep);
94STATIC_DESTRUCTOR_REGISTER(arg_properties, strv_freep);
a88f9dba 95STATIC_DESTRUCTOR_REGISTER(_arg_job_mode, unsetp);
6b42227e 96STATIC_DESTRUCTOR_REGISTER(arg_wall, strv_freep);
4ccde410 97STATIC_DESTRUCTOR_REGISTER(arg_kill_whom, unsetp);
6b42227e 98STATIC_DESTRUCTOR_REGISTER(arg_root, freep);
8aa3894e 99STATIC_DESTRUCTOR_REGISTER(arg_image, freep);
a88f9dba
ZJS
100STATIC_DESTRUCTOR_REGISTER(arg_reboot_argument, unsetp);
101STATIC_DESTRUCTOR_REGISTER(arg_host, unsetp);
102STATIC_DESTRUCTOR_REGISTER(arg_boot_loader_entry, unsetp);
d81bc51f 103STATIC_DESTRUCTOR_REGISTER(arg_clean_what, strv_freep);
f206809b 104STATIC_DESTRUCTOR_REGISTER(arg_drop_in, unsetp);
84be0c71 105STATIC_DESTRUCTOR_REGISTER(arg_image_policy, image_policy_freep);
4fbd7192 106
daf71ef6
LP
107static int systemctl_help(void) {
108 _cleanup_free_ char *link = NULL;
4fbd7192
LP
109 int r;
110
384c2c32 111 pager_open(arg_pager_flags);
4fbd7192 112
daf71ef6
LP
113 r = terminal_urlify_man("systemctl", "1", &link);
114 if (r < 0)
115 return log_oom();
4fbd7192 116
daf71ef6
LP
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"
ed462ea3
DT
121 " list-automounts [PATTERN...] List automount units currently in memory,\n"
122 " ordered by path\n"
5fb5f49b
DT
123 " list-paths [PATTERN...] List path units currently in memory,\n"
124 " ordered by path\n"
daf71ef6
LP
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"
2ee34616
MY
130 " is-failed [PATTERN...] Check whether units are failed or\n"
131 " system is in degraded state\n"
daf71ef6
LP
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"
5e8deb94
LB
156 " bind UNIT PATH [PATH] Bind-mount a path from the host into a\n"
157 " unit's namespace\n"
6faecbd3
LB
158 " mount-image UNIT PATH [PATH [OPTS]] Mount an image from the host into a\n"
159 " unit's namespace\n"
daf71ef6
LP
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"
c86a9508
LP
163 " units\n"
164 " whoami [PID...] Return unit caller or specified PIDs are\n"
165 " part of\n"
daf71ef6
LP
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"
179 " the search path\n"
180 " revert UNIT... Revert one or more unit files to vendor\n"
181 " version\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"
32854f70 198 " import-environment VARIABLE... Import all or some environment variables\n"
daf71ef6
LP
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"
34f21ff6 214 " soft-reboot Shut down and reboot userspace\n"
daf71ef6 215 " exit [EXIT_CODE] Request user instance or container exit\n"
5ae89ef3 216 " switch-root [ROOT [INIT]] Change to a different root file system\n"
5b356289
MY
217 " sleep Put the system to sleep (through one of\n"
218 " the operations below)\n"
daf71ef6
LP
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"
56cb74c3 229 " -C --capsule=NAME Connect to service manager of specified capsule\n"
daf71ef6
LP
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"
965d7f6e
LP
243 " --before Show units ordered before with 'list-dependencies'\n"
244 " --after Show units ordered after with 'list-dependencies'\n"
daf71ef6
LP
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"
4327574f 252 " --check-inhibitors=MODE\n"
5b69a7c5
ZJS
253 " Whether to check inhibitors before shutting down,\n"
254 " sleeping, or hibernating\n"
4327574f 255 " -i Shortcut for --check-inhibitors=no\n"
4ccde410 256 " --kill-whom=WHOM Whom to send signal to\n"
d06e6199 257 " --kill-value=INT Signal value to enqueue\n"
daf71ef6
LP
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"
34f21ff6
LP
263 " kexec, soft-reboot, suspend, hibernate, \n"
264 " suspend-then-hibernate, hybrid-sleep, default,\n"
265 " rescue, emergency, and exit.\n"
daf71ef6 266 " -q --quiet Suppress output\n"
c8aff47a 267 " -v --verbose Show unit logs while executing operation\n"
91dfb74e 268 " --no-warn Suppress several warnings shown by default\n"
daf71ef6
LP
269 " --wait For (re)start, wait until service stopped again\n"
270 " For is-system-running, wait until startup is completed\n"
6be4dab0 271 " For kill, wait until service stopped\n"
daf71ef6
LP
272 " --no-block Do not wait until operation finished\n"
273 " --no-wall Don't send wall message before halt/power-off/reboot\n"
fd0b8775 274 " --message=MESSAGE Specify human readable reason for system shutdown\n"
daf71ef6 275 " --no-reload Don't reload daemon after en-/dis-abling unit files\n"
6906da26 276 " --legend=BOOL Enable/disable the legend (column headers and hints)\n"
daf71ef6
LP
277 " --no-pager Do not pipe output into a pager\n"
278 " --no-ask-password Do not ask for system passwords\n"
4218f81d
EP
279 " --global Edit/enable/disable/mask default user unit files\n"
280 " globally\n"
281 " --runtime Edit/enable/disable/mask unit files temporarily until\n"
282 " next reboot\n"
daf71ef6
LP
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"
4218f81d
EP
286 " --root=PATH Edit/enable/disable/mask unit files in the specified\n"
287 " root directory\n"
8aa3894e 288 " --image=PATH Edit/enable/disable/mask unit files in the specified\n"
84be0c71
LP
289 " disk image\n"
290 " --image-policy=POLICY\n"
291 " Specify disk image dissection policy\n"
daf71ef6
LP
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"
893bcd3d 295 " short-monotonic, short-unix, short-delta,\n"
daf71ef6
LP
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"
ede3a146
LP
302 " --reboot-argument=ARG\n"
303 " Specify argument string to pass to reboot()\n"
daf71ef6 304 " --plain Print unit dependencies as a list instead of a tree\n"
b58b4a9f
FS
305 " --timestamp=FORMAT Change format of printed timestamps (pretty, unix,\n"
306 " us, utc, us+utc)\n"
5e8deb94 307 " --read-only Create read-only bind mount\n"
6faecbd3 308 " --mkdir Create directory before mounting, if missing\n"
c9615f73 309 " --marked Restart/reload previously marked units\n"
f206809b 310 " --drop-in=NAME Edit unit files using the specified drop-in file name\n"
1433e1f9
MY
311 " --when=TIME Schedule halt/power-off/reboot/kexec action after\n"
312 " a certain timestamp\n"
119cba78 313 " --stdin Read new contents of edited file from stdin\n"
bc556335
DDM
314 "\nSee the %2$s for details.\n",
315 program_invocation_short_name,
316 link,
317 ansi_underline(),
318 ansi_normal(),
319 ansi_highlight(),
320 ansi_normal());
4fbd7192 321
4fbd7192
LP
322 return 0;
323}
324
daf71ef6 325static void help_types(void) {
6906da26 326 if (arg_legend != 0)
daf71ef6 327 puts("Available unit types:");
501fc174 328
daf71ef6 329 DUMP_STRING_TABLE(unit_type, UnitType, _UNIT_TYPE_MAX);
3c756001
LP
330}
331
daf71ef6 332static void help_states(void) {
6906da26 333 if (arg_legend != 0)
daf71ef6
LP
334 puts("Available unit load states:");
335 DUMP_STRING_TABLE(unit_load_state, UnitLoadState, _UNIT_LOAD_STATE_MAX);
22f4096c 336
6906da26 337 if (arg_legend != 0)
daf71ef6
LP
338 puts("\nAvailable unit active states:");
339 DUMP_STRING_TABLE(unit_active_state, UnitActiveState, _UNIT_ACTIVE_STATE_MAX);
22f4096c 340
6906da26 341 if (arg_legend != 0)
daf71ef6
LP
342 puts("\nAvailable unit file states:");
343 DUMP_STRING_TABLE(unit_file_state, UnitFileState, _UNIT_FILE_STATE_MAX);
22f4096c 344
6906da26 345 if (arg_legend != 0)
daf71ef6
LP
346 puts("\nAvailable automount unit substates:");
347 DUMP_STRING_TABLE(automount_state, AutomountState, _AUTOMOUNT_STATE_MAX);
22f4096c 348
6906da26 349 if (arg_legend != 0)
daf71ef6
LP
350 puts("\nAvailable device unit substates:");
351 DUMP_STRING_TABLE(device_state, DeviceState, _DEVICE_STATE_MAX);
4f16c1f4 352
6906da26 353 if (arg_legend != 0)
daf71ef6
LP
354 puts("\nAvailable mount unit substates:");
355 DUMP_STRING_TABLE(mount_state, MountState, _MOUNT_STATE_MAX);
729e3769 356
6906da26 357 if (arg_legend != 0)
daf71ef6
LP
358 puts("\nAvailable path unit substates:");
359 DUMP_STRING_TABLE(path_state, PathState, _PATH_STATE_MAX);
729e3769 360
6906da26 361 if (arg_legend != 0)
daf71ef6
LP
362 puts("\nAvailable scope unit substates:");
363 DUMP_STRING_TABLE(scope_state, ScopeState, _SCOPE_STATE_MAX);
729e3769 364
6906da26 365 if (arg_legend != 0)
daf71ef6
LP
366 puts("\nAvailable service unit substates:");
367 DUMP_STRING_TABLE(service_state, ServiceState, _SERVICE_STATE_MAX);
729e3769 368
6906da26 369 if (arg_legend != 0)
daf71ef6
LP
370 puts("\nAvailable slice unit substates:");
371 DUMP_STRING_TABLE(slice_state, SliceState, _SLICE_STATE_MAX);
b41b9d2a 372
6906da26 373 if (arg_legend != 0)
daf71ef6
LP
374 puts("\nAvailable socket unit substates:");
375 DUMP_STRING_TABLE(socket_state, SocketState, _SOCKET_STATE_MAX);
729e3769 376
6906da26 377 if (arg_legend != 0)
daf71ef6
LP
378 puts("\nAvailable swap unit substates:");
379 DUMP_STRING_TABLE(swap_state, SwapState, _SWAP_STATE_MAX);
16484a8a 380
6906da26 381 if (arg_legend != 0)
daf71ef6
LP
382 puts("\nAvailable target unit substates:");
383 DUMP_STRING_TABLE(target_state, TargetState, _TARGET_STATE_MAX);
16484a8a 384
6906da26 385 if (arg_legend != 0)
daf71ef6
LP
386 puts("\nAvailable timer unit substates:");
387 DUMP_STRING_TABLE(timer_state, TimerState, _TIMER_STATE_MAX);
16484a8a
ZJS
388}
389
daf71ef6
LP
390static int systemctl_parse_argv(int argc, char *argv[]) {
391 enum {
6906da26 392 ARG_FAIL = 0x100, /* compatibility only */
daf71ef6
LP
393 ARG_REVERSE,
394 ARG_AFTER,
395 ARG_BEFORE,
4327574f 396 ARG_CHECK_INHIBITORS,
daf71ef6
LP
397 ARG_DRY_RUN,
398 ARG_SHOW_TYPES,
6906da26
ZJS
399 ARG_IRREVERSIBLE, /* compatibility only */
400 ARG_IGNORE_DEPENDENCIES, /* compatibility only */
daf71ef6
LP
401 ARG_VALUE,
402 ARG_VERSION,
403 ARG_USER,
404 ARG_SYSTEM,
405 ARG_GLOBAL,
406 ARG_NO_BLOCK,
6906da26
ZJS
407 ARG_LEGEND,
408 ARG_NO_LEGEND, /* compatibility only */
daf71ef6
LP
409 ARG_NO_PAGER,
410 ARG_NO_WALL,
411 ARG_ROOT,
8aa3894e 412 ARG_IMAGE,
06e78680 413 ARG_IMAGE_POLICY,
daf71ef6 414 ARG_NO_RELOAD,
4ccde410 415 ARG_KILL_WHOM,
d06e6199 416 ARG_KILL_VALUE,
daf71ef6
LP
417 ARG_NO_ASK_PASSWORD,
418 ARG_FAILED,
419 ARG_RUNTIME,
420 ARG_PLAIN,
421 ARG_STATE,
422 ARG_JOB_MODE,
423 ARG_PRESET_MODE,
424 ARG_FIRMWARE_SETUP,
425 ARG_BOOT_LOADER_MENU,
426 ARG_BOOT_LOADER_ENTRY,
427 ARG_NOW,
428 ARG_MESSAGE,
429 ARG_WITH_DEPENDENCIES,
430 ARG_WAIT,
431 ARG_WHAT,
432 ARG_REBOOT_ARG,
433 ARG_TIMESTAMP_STYLE,
5e8deb94
LB
434 ARG_READ_ONLY,
435 ARG_MKDIR,
c9615f73 436 ARG_MARKED,
108d35ac 437 ARG_NO_WARN,
f206809b 438 ARG_DROP_IN,
1433e1f9 439 ARG_WHEN,
329050c5 440 ARG_STDIN,
daf71ef6 441 };
1238ee09 442
daf71ef6
LP
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 },
40a49f12 453 { "failed", no_argument, NULL, ARG_FAILED },
daf71ef6
LP
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 */
4327574f
FS
459 { "ignore-inhibitors", no_argument, NULL, 'i' }, /* compatibility only */
460 { "check-inhibitors", required_argument, NULL, ARG_CHECK_INHIBITORS },
daf71ef6
LP
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 },
56cb74c3 465 { "capsule", required_argument, NULL, 'C' },
daf71ef6
LP
466 { "wait", no_argument, NULL, ARG_WAIT },
467 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
6906da26
ZJS
468 { "legend", required_argument, NULL, ARG_LEGEND },
469 { "no-legend", no_argument, NULL, ARG_NO_LEGEND }, /* compatibility only */
daf71ef6
LP
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' },
c8aff47a 474 { "verbose", no_argument, NULL, 'v' },
108d35ac 475 { "no-warn", no_argument, NULL, ARG_NO_WARN },
daf71ef6 476 { "root", required_argument, NULL, ARG_ROOT },
8aa3894e 477 { "image", required_argument, NULL, ARG_IMAGE },
06e78680 478 { "image-policy", required_argument, NULL, ARG_IMAGE_POLICY },
daf71ef6
LP
479 { "force", no_argument, NULL, 'f' },
480 { "no-reload", no_argument, NULL, ARG_NO_RELOAD },
4ccde410 481 { "kill-whom", required_argument, NULL, ARG_KILL_WHOM },
d06e6199 482 { "kill-value", required_argument, NULL, ARG_KILL_VALUE },
daf71ef6
LP
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 },
5e8deb94
LB
504 { "read-only", no_argument, NULL, ARG_READ_ONLY },
505 { "mkdir", no_argument, NULL, ARG_MKDIR },
c9615f73 506 { "marked", no_argument, NULL, ARG_MARKED },
f206809b 507 { "drop-in", required_argument, NULL, ARG_DROP_IN },
1433e1f9 508 { "when", required_argument, NULL, ARG_WHEN },
329050c5 509 { "stdin", no_argument, NULL, ARG_STDIN },
daf71ef6
LP
510 {}
511 };
1238ee09 512
daf71ef6 513 int c, r;
d309c1c3 514
daf71ef6
LP
515 assert(argc >= 0);
516 assert(argv);
d309c1c3 517
daf71ef6
LP
518 /* We default to allowing interactive authorization only in systemctl (not in the legacy commands) */
519 arg_ask_password = true;
d309c1c3 520
c8aff47a 521 while ((c = getopt_long(argc, argv, "hC:t:p:P:alqvfs:H:M:n:o:iTr.::", options, NULL)) >= 0)
57ab2eab 522
daf71ef6 523 switch (c) {
9ef15026 524
daf71ef6
LP
525 case 'h':
526 return systemctl_help();
85d9b598 527
daf71ef6
LP
528 case ARG_VERSION:
529 return version();
e9c387c8 530
76e5e267 531 case 't':
d81bc51f 532 if (isempty(optarg))
daf71ef6
LP
533 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
534 "--type= requires arguments.");
d81bc51f 535
76e5e267 536 for (const char *p = optarg;;) {
daf71ef6 537 _cleanup_free_ char *type = NULL;
d81bc51f 538
daf71ef6 539 r = extract_first_word(&p, &type, ",", 0);
d81bc51f 540 if (r < 0)
daf71ef6 541 return log_error_errno(r, "Failed to parse type: %s", optarg);
d81bc51f
LP
542 if (r == 0)
543 break;
544
daf71ef6
LP
545 if (streq(type, "help")) {
546 help_types();
d81bc51f
LP
547 return 0;
548 }
549
daf71ef6
LP
550 if (unit_type_from_string(type) >= 0) {
551 if (strv_consume(&arg_types, TAKE_PTR(type)) < 0)
552 return log_oom();
553 continue;
554 }
555
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)
560 return log_oom();
561 continue;
562 }
563
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.");
d81bc51f
LP
567 }
568
569 break;
d81bc51f 570
daf71ef6 571 case 'P':
255b1fc8 572 SET_FLAG(arg_print_flags, BUS_PRINT_PROPERTY_ONLY_VALUE, true);
daf71ef6 573 _fallthrough_;
dae710be 574
daf71ef6
LP
575 case 'p':
576 /* Make sure that if the empty property list was specified, we won't show any
577 properties. */
578 if (isempty(optarg) && !arg_properties) {
579 arg_properties = new0(char*, 1);
580 if (!arg_properties)
581 return log_oom();
76e5e267
ZJS
582 } else
583 for (const char *p = optarg;;) {
daf71ef6 584 _cleanup_free_ char *prop = NULL;
46ad9c53 585
daf71ef6
LP
586 r = extract_first_word(&p, &prop, ",", 0);
587 if (r < 0)
588 return log_error_errno(r, "Failed to parse property: %s", optarg);
589 if (r == 0)
590 break;
46ad9c53 591
daf71ef6
LP
592 if (strv_consume(&arg_properties, TAKE_PTR(prop)) < 0)
593 return log_oom();
594 }
2e7e19ca 595
daf71ef6 596 /* If the user asked for a particular property, show it, even if it is empty. */
255b1fc8 597 SET_FLAG(arg_print_flags, BUS_PRINT_PROPERTY_SHOW_EMPTY, true);
7e4249b9 598
daf71ef6 599 break;
7e4249b9 600
daf71ef6 601 case 'a':
255b1fc8 602 SET_FLAG(arg_print_flags, BUS_PRINT_PROPERTY_SHOW_EMPTY, true);
daf71ef6
LP
603 arg_all = true;
604 break;
a8f11321 605
daf71ef6
LP
606 case ARG_REVERSE:
607 arg_dependency = DEPENDENCY_REVERSE;
608 break;
93a08841 609
daf71ef6
LP
610 case ARG_AFTER:
611 arg_dependency = DEPENDENCY_AFTER;
612 arg_jobs_after = true;
613 break;
7e4249b9 614
daf71ef6
LP
615 case ARG_BEFORE:
616 arg_dependency = DEPENDENCY_BEFORE;
617 arg_jobs_before = true;
618 break;
e4b61340 619
daf71ef6
LP
620 case ARG_SHOW_TYPES:
621 arg_show_types = true;
622 break;
e4b61340 623
daf71ef6 624 case ARG_VALUE:
255b1fc8 625 SET_FLAG(arg_print_flags, BUS_PRINT_PROPERTY_ONLY_VALUE, true);
daf71ef6 626 break;
e4b61340 627
daf71ef6 628 case ARG_JOB_MODE:
a88f9dba 629 _arg_job_mode = optarg;
daf71ef6 630 break;
e4b61340 631
daf71ef6 632 case ARG_FAIL:
a88f9dba 633 _arg_job_mode = "fail";
daf71ef6 634 break;
e4b61340 635
daf71ef6 636 case ARG_IRREVERSIBLE:
a88f9dba 637 _arg_job_mode = "replace-irreversibly";
daf71ef6 638 break;
e4b61340 639
daf71ef6 640 case ARG_IGNORE_DEPENDENCIES:
a88f9dba 641 _arg_job_mode = "ignore-dependencies";
daf71ef6 642 break;
e4b61340 643
daf71ef6 644 case ARG_USER:
4870133b 645 arg_runtime_scope = RUNTIME_SCOPE_USER;
e4b61340
LP
646 break;
647
daf71ef6 648 case ARG_SYSTEM:
4870133b 649 arg_runtime_scope = RUNTIME_SCOPE_SYSTEM;
e4b61340
LP
650 break;
651
daf71ef6 652 case ARG_GLOBAL:
4870133b 653 arg_runtime_scope = RUNTIME_SCOPE_GLOBAL;
e4b61340
LP
654 break;
655
56cb74c3
LP
656 case 'C':
657 r = capsule_name_is_valid(optarg);
658 if (r < 0)
659 return log_error_errno(r, "Unable to validate capsule name '%s': %m", optarg);
660 if (r == 0)
661 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid capsule name: %s", optarg);
662
663 arg_host = optarg;
664 arg_transport = BUS_TRANSPORT_CAPSULE;
665 arg_runtime_scope = RUNTIME_SCOPE_USER;
666 break;
667
daf71ef6
LP
668 case ARG_WAIT:
669 arg_wait = true;
e4b61340
LP
670 break;
671
daf71ef6
LP
672 case ARG_NO_BLOCK:
673 arg_no_block = true;
e4b61340
LP
674 break;
675
daf71ef6 676 case ARG_NO_LEGEND:
6906da26
ZJS
677 arg_legend = false;
678 break;
679
680 case ARG_LEGEND:
681 r = parse_boolean_argument("--legend", optarg, NULL);
682 if (r < 0)
683 return r;
684 arg_legend = r;
e4b61340
LP
685 break;
686
daf71ef6
LP
687 case ARG_NO_PAGER:
688 arg_pager_flags |= PAGER_DISABLE;
f3f054f0
TB
689 break;
690
514f4ef5
LP
691 case ARG_NO_WALL:
692 arg_no_wall = true;
693 break;
694
daf71ef6 695 case ARG_ROOT:
614b022c 696 r = parse_path_argument(optarg, false, &arg_root);
daf71ef6
LP
697 if (r < 0)
698 return r;
e4b61340
LP
699 break;
700
8aa3894e
RP
701 case ARG_IMAGE:
702 r = parse_path_argument(optarg, false, &arg_image);
703 if (r < 0)
704 return r;
705 break;
706
06e78680
YW
707 case ARG_IMAGE_POLICY:
708 r = parse_image_policy_argument(optarg, &arg_image_policy);
709 if (r < 0)
710 return r;
711 break;
712
daf71ef6
LP
713 case 'l':
714 arg_full = true;
715 break;
e4b61340 716
daf71ef6
LP
717 case ARG_FAILED:
718 if (strv_extend(&arg_states, "failed") < 0)
719 return log_oom();
e4b61340 720
daf71ef6 721 break;
e4b61340 722
daf71ef6
LP
723 case ARG_DRY_RUN:
724 arg_dry_run = true;
725 break;
e4b61340 726
daf71ef6
LP
727 case 'q':
728 arg_quiet = true;
6906da26
ZJS
729
730 if (arg_legend < 0)
731 arg_legend = false;
732
daf71ef6 733 break;
e4b61340 734
c8aff47a
LP
735 case 'v':
736 arg_verbose = true;
737 break;
738
daf71ef6
LP
739 case 'f':
740 arg_force++;
741 break;
e4b61340 742
daf71ef6
LP
743 case ARG_NO_RELOAD:
744 arg_no_reload = true;
745 break;
e4b61340 746
4ccde410
ZJS
747 case ARG_KILL_WHOM:
748 arg_kill_whom = optarg;
daf71ef6 749 break;
e4b61340 750
d06e6199
LP
751 case ARG_KILL_VALUE: {
752 unsigned u;
753
754 if (isempty(optarg)) {
755 arg_kill_value_set = false;
756 return 0;
757 }
758
759 /* First, try to parse unsigned, so that we can support the prefixes 0x, 0o, 0b */
760 r = safe_atou_full(optarg, 0, &u);
761 if (r < 0)
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)
765 r = -ERANGE;
766 else
767 arg_kill_value = (int) u;
768 if (r < 0)
769 return log_error_errno(r, "Unable to parse signal queue value: %s", optarg);
770
771 arg_kill_value_set = true;
772 break;
773 }
774
daf71ef6 775 case 's':
86beb213
ZJS
776 r = parse_signal_argument(optarg, &arg_signal);
777 if (r <= 0)
778 return r;
daf71ef6 779 break;
e4b61340 780
daf71ef6
LP
781 case ARG_NO_ASK_PASSWORD:
782 arg_ask_password = false;
783 break;
e4b61340
LP
784
785 case 'H':
daf71ef6
LP
786 arg_transport = BUS_TRANSPORT_REMOTE;
787 arg_host = optarg;
e4b61340
LP
788 break;
789
daf71ef6 790 case 'M':
a59cc386
MY
791 r = parse_machine_argument(optarg, &arg_host, &arg_transport);
792 if (r < 0)
793 return r;
e4b61340
LP
794 break;
795
daf71ef6
LP
796 case ARG_RUNTIME:
797 arg_runtime = true;
e4b61340
LP
798 break;
799
daf71ef6
LP
800 case 'n':
801 if (safe_atou(optarg, &arg_lines) < 0)
802 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
803 "Failed to parse lines '%s'",
804 optarg);
04ebb595
LP
805 break;
806
daf71ef6
LP
807 case 'o':
808 if (streq(optarg, "help")) {
809 DUMP_STRING_TABLE(output_mode, OutputMode, _OUTPUT_MODE_MAX);
810 return 0;
811 }
812
813 arg_output = output_mode_from_string(optarg);
814 if (arg_output < 0)
815 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
816 "Unknown output '%s'.",
817 optarg);
818
819 if (OUTPUT_MODE_IS_JSON(arg_output)) {
6906da26 820 arg_legend = false;
daf71ef6
LP
821 arg_plain = true;
822 }
e4b61340
LP
823 break;
824
daf71ef6 825 case 'i':
4327574f
FS
826 arg_check_inhibitors = 0;
827 break;
828
829 case ARG_CHECK_INHIBITORS:
b71a721f
LP
830 r = parse_tristate_full(optarg, "auto", &arg_check_inhibitors);
831 if (r < 0)
832 return log_error_errno(r, "Failed to parse --check-inhibitors= argument: %s", optarg);
e4b61340
LP
833 break;
834
daf71ef6
LP
835 case ARG_PLAIN:
836 arg_plain = true;
514f4ef5
LP
837 break;
838
daf71ef6
LP
839 case ARG_FIRMWARE_SETUP:
840 arg_firmware_setup = true;
e4b61340
LP
841 break;
842
daf71ef6 843 case ARG_BOOT_LOADER_MENU:
f6144808 844
daf71ef6
LP
845 r = parse_sec(optarg, &arg_boot_loader_menu);
846 if (r < 0)
847 return log_error_errno(r, "Failed to parse --boot-loader-menu= argument '%s': %m", optarg);
e4b61340 848
daf71ef6 849 break;
e4b61340 850
daf71ef6 851 case ARG_BOOT_LOADER_ENTRY:
e4b61340 852
daf71ef6
LP
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();
855 if (r < 0)
856 return r;
e4b61340 857
daf71ef6
LP
858 return 0;
859 }
e4b61340 860
daf71ef6
LP
861 arg_boot_loader_entry = empty_to_null(optarg);
862 break;
e4b61340 863
76e5e267 864 case ARG_STATE:
daf71ef6
LP
865 if (isempty(optarg))
866 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
867 "--state= requires arguments.");
e4b61340 868
76e5e267 869 for (const char *p = optarg;;) {
daf71ef6 870 _cleanup_free_ char *s = NULL;
e4b61340 871
daf71ef6
LP
872 r = extract_first_word(&p, &s, ",", 0);
873 if (r < 0)
874 return log_error_errno(r, "Failed to parse state: %s", optarg);
875 if (r == 0)
876 break;
e4b61340 877
daf71ef6
LP
878 if (streq(s, "help")) {
879 help_states();
880 return 0;
881 }
e4b61340 882
daf71ef6
LP
883 if (strv_consume(&arg_states, TAKE_PTR(s)) < 0)
884 return log_oom();
885 }
886 break;
e4b61340 887
daf71ef6
LP
888 case 'r':
889 if (geteuid() != 0)
890 return log_error_errno(SYNTHETIC_ERRNO(EPERM),
891 "--recursive requires root privileges.");
892
893 arg_recursive = true;
514f4ef5
LP
894 break;
895
daf71ef6
LP
896 case ARG_PRESET_MODE:
897 if (streq(optarg, "help")) {
b364c4de 898 DUMP_STRING_TABLE(unit_file_preset_mode, UnitFilePresetMode, _UNIT_FILE_PRESET_MODE_MAX);
daf71ef6
LP
899 return 0;
900 }
e4b61340 901
daf71ef6
LP
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);
e4b61340 906
daf71ef6 907 break;
e4b61340 908
daf71ef6
LP
909 case ARG_NOW:
910 arg_now = true;
911 break;
e4b61340 912
daf71ef6
LP
913 case ARG_MESSAGE:
914 if (strv_extend(&arg_wall, optarg) < 0)
915 return log_oom();
916 break;
e4b61340 917
daf71ef6
LP
918 case 'T':
919 arg_show_transaction = true;
e4b61340
LP
920 break;
921
daf71ef6
LP
922 case ARG_WITH_DEPENDENCIES:
923 arg_with_dependencies = true;
924 break;
e4b61340 925
76e5e267 926 case ARG_WHAT:
daf71ef6 927 if (isempty(optarg))
028de729 928 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--what= requires arguments (see --what=help).");
e4b61340 929
76e5e267 930 for (const char *p = optarg;;) {
daf71ef6 931 _cleanup_free_ char *k = NULL;
e4b61340 932
daf71ef6
LP
933 r = extract_first_word(&p, &k, ",", 0);
934 if (r < 0)
935 return log_error_errno(r, "Failed to parse directory type: %s", optarg);
936 if (r == 0)
937 break;
e4b61340 938
daf71ef6
LP
939 if (streq(k, "help")) {
940 puts("runtime\n"
941 "state\n"
942 "cache\n"
943 "logs\n"
4fb8f1e8 944 "configuration\n"
68b6289a
JB
945 "fdstore\n"
946 "all");
daf71ef6
LP
947 return 0;
948 }
e4b61340 949
daf71ef6
LP
950 r = strv_consume(&arg_clean_what, TAKE_PTR(k));
951 if (r < 0)
952 return log_oom();
953 }
e4b61340 954
daf71ef6 955 break;
e4b61340 956
daf71ef6
LP
957 case ARG_REBOOT_ARG:
958 arg_reboot_argument = optarg;
959 break;
960
961 case ARG_TIMESTAMP_STYLE:
962 if (streq(optarg, "help")) {
963 DUMP_STRING_TABLE(timestamp_style, TimestampStyle, _TIMESTAMP_STYLE_MAX);
964 return 0;
965 }
966
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);
971
972 break;
e4b61340 973
5e8deb94
LB
974 case ARG_READ_ONLY:
975 arg_read_only = true;
976 break;
977
978 case ARG_MKDIR:
979 arg_mkdir = true;
980 break;
981
c9615f73
ZJS
982 case ARG_MARKED:
983 arg_marked = true;
984 break;
985
108d35ac
MY
986 case ARG_NO_WARN:
987 arg_no_warn = true;
988 break;
989
f206809b
MY
990 case ARG_DROP_IN:
991 arg_drop_in = optarg;
992 break;
993
1433e1f9
MY
994 case ARG_WHEN:
995 if (streq(optarg, "show")) {
0e10c3d8
LN
996 arg_action = ACTION_SYSTEMCTL_SHOW_SHUTDOWN;
997 return 1;
1433e1f9
MY
998 }
999
1000 if (STR_IN_SET(optarg, "", "cancel")) {
0e10c3d8
LN
1001 arg_action = ACTION_CANCEL_SHUTDOWN;
1002 return 1;
1003 }
1004
1005 if (streq(optarg, "auto")) {
1006 arg_when = USEC_INFINITY; /* logind chooses on server side */
1433e1f9
MY
1007 break;
1008 }
1009
1010 r = parse_timestamp(optarg, &arg_when);
1011 if (r < 0)
1012 return log_error_errno(r, "Failed to parse --when= argument '%s': %m", optarg);
1013
1014 if (!timestamp_is_set(arg_when))
1015 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1016 "Invalid timestamp '%s' specified for --when=.", optarg);
1017
1018 break;
1019
329050c5
ZJS
1020 case ARG_STDIN:
1021 arg_stdin = true;
1022 break;
1023
daf71ef6
LP
1024 case '.':
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");
1030 _fallthrough_;
e4b61340
LP
1031
1032 case '?':
1033 return -EINVAL;
1034
1035 default:
04499a70 1036 assert_not_reached();
e4b61340 1037 }
e4b61340 1038
966f3a24
LP
1039 /* If we are in --user mode, there's no point in talking to PolicyKit or the infra to query system
1040 * passwords */
4870133b 1041 if (arg_runtime_scope != RUNTIME_SCOPE_SYSTEM)
966f3a24
LP
1042 arg_ask_password = false;
1043
4870133b 1044 if (arg_transport == BUS_TRANSPORT_REMOTE && arg_runtime_scope != RUNTIME_SCOPE_SYSTEM)
daf71ef6
LP
1045 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1046 "Cannot access user instance remotely.");
1047
bcf98222
YW
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.");
1051
daf71ef6 1052 if (arg_wait && arg_no_block)
baaa35ad 1053 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
daf71ef6 1054 "--wait may not be combined with --no-block.");
e4b61340 1055
c9615f73
ZJS
1056 bool do_reload_or_restart = streq_ptr(argv[optind], "reload-or-restart");
1057 if (arg_marked) {
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'.");
1064 if (arg_wait)
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.");
1070
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.");
1075 }
1076
8aa3894e 1077 if (arg_image && arg_root)
329050c5
ZJS
1078 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1079 "Please specify either --root= or --image=, the combination of both is not supported.");
8aa3894e 1080
e4b61340
LP
1081 return 1;
1082}
1083
5fd87823 1084int systemctl_dispatch_parse_argv(int argc, char *argv[]) {
e4b61340
LP
1085 assert(argc >= 0);
1086 assert(argv);
1087
2306d177 1088 if (invoked_as(argv, "halt")) {
d41a9e4f
ZJS
1089 arg_action = ACTION_HALT;
1090 return halt_parse_argv(argc, argv);
1091
2306d177 1092 } else if (invoked_as(argv, "poweroff")) {
d41a9e4f
ZJS
1093 arg_action = ACTION_POWEROFF;
1094 return halt_parse_argv(argc, argv);
1095
2306d177 1096 } else if (invoked_as(argv, "reboot")) {
665a3d6d 1097 arg_action = ACTION_REBOOT;
d41a9e4f
ZJS
1098 return halt_parse_argv(argc, argv);
1099
2306d177 1100 } else if (invoked_as(argv, "shutdown")) {
d41a9e4f
ZJS
1101 arg_action = ACTION_POWEROFF;
1102 return shutdown_parse_argv(argc, argv);
1103
2306d177 1104 } else if (invoked_as(argv, "init")) {
d41a9e4f
ZJS
1105
1106 /* Matches invocations as "init" as well as "telinit", which are synonymous when run
1107 * as PID != 1 on SysV.
1108 *
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
2306d177 1115 * invoked_as(argv, "init") here, as a quick way to match both.
d41a9e4f
ZJS
1116 *
1117 * Also see redirect_telinit() in src/core/main.c. */
1118
50b68cff
MB
1119 arg_action = _ACTION_INVALID; /* telinit_parse_argv() will figure out the actual action we'll execute */
1120 return telinit_parse_argv(argc, argv);
d41a9e4f 1121
2306d177 1122 } else if (invoked_as(argv, "runlevel")) {
d41a9e4f
ZJS
1123 arg_action = ACTION_RUNLEVEL;
1124 return runlevel_parse_argv(argc, argv);
e4b61340
LP
1125 }
1126
1127 arg_action = ACTION_SYSTEMCTL;
1128 return systemctl_parse_argv(argc, argv);
1129}