]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/systemctl/systemctl.c
Merge pull request #31531 from poettering/verity-userspace-optional
[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>
daf71ef6 4#include <locale.h>
03a7b521 5#include <unistd.h>
81527be1 6
f459b602 7#include "sd-daemon.h"
daf71ef6 8
d6b4d1c7 9#include "build.h"
daf71ef6 10#include "bus-util.h"
8aa3894e 11#include "dissect-image.h"
03a7b521 12#include "install.h"
d1405af3 13#include "main-func.h"
8aa3894e 14#include "mount-util.h"
daf71ef6 15#include "output-mode.h"
03a7b521 16#include "pager.h"
86beb213 17#include "parse-argument.h"
03a7b521 18#include "path-util.h"
294bf0c3 19#include "pretty-print.h"
2306d177 20#include "process-util.h"
ede5a78f 21#include "reboot-util.h"
78f22b97 22#include "rlimit-util.h"
592705f2 23#include "sigbus.h"
24882e06 24#include "signal-util.h"
0f958c8d 25#include "stat-util.h"
8559e61d 26#include "string-table.h"
daf71ef6
LP
27#include "systemctl-add-dependency.h"
28#include "systemctl-cancel-job.h"
29#include "systemctl-clean-or-freeze.h"
30#include "systemctl-compat-halt.h"
31#include "systemctl-compat-runlevel.h"
32#include "systemctl-compat-shutdown.h"
33#include "systemctl-compat-telinit.h"
34#include "systemctl-daemon-reload.h"
35#include "systemctl-edit.h"
36#include "systemctl-enable.h"
37#include "systemctl-is-active.h"
38#include "systemctl-is-enabled.h"
39#include "systemctl-is-system-running.h"
40#include "systemctl-kill.h"
41#include "systemctl-list-dependencies.h"
42#include "systemctl-list-jobs.h"
43#include "systemctl-list-machines.h"
44#include "systemctl-list-unit-files.h"
45#include "systemctl-list-units.h"
46#include "systemctl-log-setting.h"
47#include "systemctl-logind.h"
5e8deb94 48#include "systemctl-mount.h"
daf71ef6
LP
49#include "systemctl-preset-all.h"
50#include "systemctl-reset-failed.h"
51#include "systemctl-service-watchdogs.h"
52#include "systemctl-set-default.h"
53#include "systemctl-set-environment.h"
54#include "systemctl-set-property.h"
55#include "systemctl-show.h"
56#include "systemctl-start-special.h"
57#include "systemctl-start-unit.h"
58#include "systemctl-switch-root.h"
59#include "systemctl-sysv-compat.h"
60#include "systemctl-trivial-method.h"
61#include "systemctl-util.h"
c86a9508 62#include "systemctl-whoami.h"
daf71ef6 63#include "systemctl.h"
03a7b521 64#include "terminal-util.h"
daf71ef6 65#include "time-util.h"
e449de87 66#include "verbs.h"
7f4b3c5e 67#include "virt.h"
7e4249b9 68
daf71ef6
LP
69char **arg_types = NULL;
70char **arg_states = NULL;
71char **arg_properties = NULL;
72bool arg_all = false;
73enum dependency arg_dependency = DEPENDENCY_FORWARD;
a88f9dba 74const char *_arg_job_mode = NULL;
4870133b 75RuntimeScope arg_runtime_scope = RUNTIME_SCOPE_SYSTEM;
daf71ef6
LP
76bool arg_wait = false;
77bool arg_no_block = false;
6906da26 78int arg_legend = -1; /* -1: true, unless --quiet is passed, 1: true */
daf71ef6
LP
79PagerFlags arg_pager_flags = 0;
80bool arg_no_wtmp = false;
81bool arg_no_sync = false;
82bool arg_no_wall = false;
83bool arg_no_reload = false;
255b1fc8 84BusPrintPropertyFlags arg_print_flags = 0;
daf71ef6 85bool arg_show_types = false;
4327574f 86int arg_check_inhibitors = -1;
daf71ef6
LP
87bool arg_dry_run = false;
88bool arg_quiet = false;
108d35ac 89bool arg_no_warn = false;
daf71ef6
LP
90bool arg_full = false;
91bool arg_recursive = false;
92bool arg_with_dependencies = false;
93bool arg_show_transaction = false;
94int arg_force = 0;
95bool arg_ask_password = false;
96bool arg_runtime = false;
97UnitFilePresetMode arg_preset_mode = UNIT_FILE_PRESET_FULL;
98char **arg_wall = NULL;
4ccde410 99const char *arg_kill_whom = NULL;
daf71ef6 100int arg_signal = SIGTERM;
d06e6199
LP
101int arg_kill_value;
102bool arg_kill_value_set = false;
daf71ef6 103char *arg_root = NULL;
8aa3894e 104char *arg_image = NULL;
daf71ef6 105usec_t arg_when = 0;
329050c5 106bool arg_stdin = false;
daf71ef6
LP
107const char *arg_reboot_argument = NULL;
108enum action arg_action = ACTION_SYSTEMCTL;
109BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
110const char *arg_host = NULL;
111unsigned arg_lines = 10;
112OutputMode arg_output = OUTPUT_SHORT;
113bool arg_plain = false;
114bool arg_firmware_setup = false;
115usec_t arg_boot_loader_menu = USEC_INFINITY;
116const char *arg_boot_loader_entry = NULL;
117bool arg_now = false;
118bool arg_jobs_before = false;
119bool arg_jobs_after = false;
120char **arg_clean_what = NULL;
121TimestampStyle arg_timestamp_style = TIMESTAMP_PRETTY;
5e8deb94
LB
122bool arg_read_only = false;
123bool arg_mkdir = false;
c9615f73 124bool arg_marked = false;
f206809b 125const char *arg_drop_in = NULL;
84be0c71 126ImagePolicy *arg_image_policy = NULL;
e8630e69 127
61ff45db
LP
128STATIC_DESTRUCTOR_REGISTER(arg_types, strv_freep);
129STATIC_DESTRUCTOR_REGISTER(arg_states, strv_freep);
130STATIC_DESTRUCTOR_REGISTER(arg_properties, strv_freep);
a88f9dba 131STATIC_DESTRUCTOR_REGISTER(_arg_job_mode, unsetp);
6b42227e 132STATIC_DESTRUCTOR_REGISTER(arg_wall, strv_freep);
4ccde410 133STATIC_DESTRUCTOR_REGISTER(arg_kill_whom, unsetp);
6b42227e 134STATIC_DESTRUCTOR_REGISTER(arg_root, freep);
8aa3894e 135STATIC_DESTRUCTOR_REGISTER(arg_image, freep);
a88f9dba
ZJS
136STATIC_DESTRUCTOR_REGISTER(arg_reboot_argument, unsetp);
137STATIC_DESTRUCTOR_REGISTER(arg_host, unsetp);
138STATIC_DESTRUCTOR_REGISTER(arg_boot_loader_entry, unsetp);
d81bc51f 139STATIC_DESTRUCTOR_REGISTER(arg_clean_what, strv_freep);
f206809b 140STATIC_DESTRUCTOR_REGISTER(arg_drop_in, unsetp);
84be0c71 141STATIC_DESTRUCTOR_REGISTER(arg_image_policy, image_policy_freep);
4fbd7192 142
daf71ef6
LP
143static int systemctl_help(void) {
144 _cleanup_free_ char *link = NULL;
4fbd7192
LP
145 int r;
146
384c2c32 147 pager_open(arg_pager_flags);
4fbd7192 148
daf71ef6
LP
149 r = terminal_urlify_man("systemctl", "1", &link);
150 if (r < 0)
151 return log_oom();
4fbd7192 152
daf71ef6
LP
153 printf("%1$s [OPTIONS...] COMMAND ...\n\n"
154 "%5$sQuery or send control commands to the system manager.%6$s\n"
155 "\n%3$sUnit Commands:%4$s\n"
156 " list-units [PATTERN...] List units currently in memory\n"
ed462ea3
DT
157 " list-automounts [PATTERN...] List automount units currently in memory,\n"
158 " ordered by path\n"
5fb5f49b
DT
159 " list-paths [PATTERN...] List path units currently in memory,\n"
160 " ordered by path\n"
daf71ef6
LP
161 " list-sockets [PATTERN...] List socket units currently in memory,\n"
162 " ordered by address\n"
163 " list-timers [PATTERN...] List timer units currently in memory,\n"
164 " ordered by next elapse\n"
165 " is-active PATTERN... Check whether units are active\n"
2ee34616
MY
166 " is-failed [PATTERN...] Check whether units are failed or\n"
167 " system is in degraded state\n"
daf71ef6
LP
168 " status [PATTERN...|PID...] Show runtime status of one or more units\n"
169 " show [PATTERN...|JOB...] Show properties of one or more\n"
170 " units/jobs or the manager\n"
171 " cat PATTERN... Show files and drop-ins of specified units\n"
172 " help PATTERN...|PID... Show manual for one or more units\n"
173 " list-dependencies [UNIT...] Recursively show units which are required\n"
174 " or wanted by the units or by which those\n"
175 " units are required or wanted\n"
176 " start UNIT... Start (activate) one or more units\n"
177 " stop UNIT... Stop (deactivate) one or more units\n"
178 " reload UNIT... Reload one or more units\n"
179 " restart UNIT... Start or restart one or more units\n"
180 " try-restart UNIT... Restart one or more units if active\n"
181 " reload-or-restart UNIT... Reload one or more units if possible,\n"
182 " otherwise start or restart\n"
183 " try-reload-or-restart UNIT... If active, reload one or more units,\n"
184 " if supported, otherwise restart\n"
185 " isolate UNIT Start one unit and stop all others\n"
186 " kill UNIT... Send signal to processes of a unit\n"
187 " clean UNIT... Clean runtime, cache, state, logs or\n"
188 " configuration of unit\n"
189 " freeze PATTERN... Freeze execution of unit processes\n"
190 " thaw PATTERN... Resume execution of a frozen unit\n"
191 " set-property UNIT PROPERTY=VALUE... Sets one or more properties of a unit\n"
5e8deb94
LB
192 " bind UNIT PATH [PATH] Bind-mount a path from the host into a\n"
193 " unit's namespace\n"
6faecbd3
LB
194 " mount-image UNIT PATH [PATH [OPTS]] Mount an image from the host into a\n"
195 " unit's namespace\n"
daf71ef6
LP
196 " service-log-level SERVICE [LEVEL] Get/set logging threshold for service\n"
197 " service-log-target SERVICE [TARGET] Get/set logging target for service\n"
198 " reset-failed [PATTERN...] Reset failed state for all, one, or more\n"
c86a9508
LP
199 " units\n"
200 " whoami [PID...] Return unit caller or specified PIDs are\n"
201 " part of\n"
daf71ef6
LP
202 "\n%3$sUnit File Commands:%4$s\n"
203 " list-unit-files [PATTERN...] List installed unit files\n"
204 " enable [UNIT...|PATH...] Enable one or more unit files\n"
205 " disable UNIT... Disable one or more unit files\n"
206 " reenable UNIT... Reenable one or more unit files\n"
207 " preset UNIT... Enable/disable one or more unit files\n"
208 " based on preset configuration\n"
209 " preset-all Enable/disable all unit files based on\n"
210 " preset configuration\n"
211 " is-enabled UNIT... Check whether unit files are enabled\n"
212 " mask UNIT... Mask one or more units\n"
213 " unmask UNIT... Unmask one or more units\n"
214 " link PATH... Link one or more units files into\n"
215 " the search path\n"
216 " revert UNIT... Revert one or more unit files to vendor\n"
217 " version\n"
218 " add-wants TARGET UNIT... Add 'Wants' dependency for the target\n"
219 " on specified one or more units\n"
220 " add-requires TARGET UNIT... Add 'Requires' dependency for the target\n"
221 " on specified one or more units\n"
222 " edit UNIT... Edit one or more unit files\n"
223 " get-default Get the name of the default target\n"
224 " set-default TARGET Set the default target\n"
225 "\n%3$sMachine Commands:%4$s\n"
226 " list-machines [PATTERN...] List local containers and host\n"
227 "\n%3$sJob Commands:%4$s\n"
228 " list-jobs [PATTERN...] List jobs\n"
229 " cancel [JOB...] Cancel all, one, or more jobs\n"
230 "\n%3$sEnvironment Commands:%4$s\n"
231 " show-environment Dump environment\n"
232 " set-environment VARIABLE=VALUE... Set one or more environment variables\n"
233 " unset-environment VARIABLE... Unset one or more environment variables\n"
32854f70 234 " import-environment VARIABLE... Import all or some environment variables\n"
daf71ef6
LP
235 "\n%3$sManager State Commands:%4$s\n"
236 " daemon-reload Reload systemd manager configuration\n"
237 " daemon-reexec Reexecute systemd manager\n"
238 " log-level [LEVEL] Get/set logging threshold for manager\n"
239 " log-target [TARGET] Get/set logging target for manager\n"
240 " service-watchdogs [BOOL] Get/set service watchdog state\n"
241 "\n%3$sSystem Commands:%4$s\n"
242 " is-system-running Check whether system is fully running\n"
243 " default Enter system default mode\n"
244 " rescue Enter system rescue mode\n"
245 " emergency Enter system emergency mode\n"
246 " halt Shut down and halt the system\n"
247 " poweroff Shut down and power-off the system\n"
248 " reboot Shut down and reboot the system\n"
249 " kexec Shut down and reboot the system with kexec\n"
34f21ff6 250 " soft-reboot Shut down and reboot userspace\n"
daf71ef6 251 " exit [EXIT_CODE] Request user instance or container exit\n"
5ae89ef3 252 " switch-root [ROOT [INIT]] Change to a different root file system\n"
5b356289
MY
253 " sleep Put the system to sleep (through one of\n"
254 " the operations below)\n"
daf71ef6
LP
255 " suspend Suspend the system\n"
256 " hibernate Hibernate the system\n"
257 " hybrid-sleep Hibernate and suspend the system\n"
258 " suspend-then-hibernate Suspend the system, wake after a period of\n"
259 " time, and hibernate"
260 "\n%3$sOptions:%4$s\n"
261 " -h --help Show this help\n"
262 " --version Show package version\n"
263 " --system Connect to system manager\n"
264 " --user Connect to user service manager\n"
265 " -H --host=[USER@]HOST Operate on remote host\n"
266 " -M --machine=CONTAINER Operate on a local container\n"
267 " -t --type=TYPE List units of a particular type\n"
268 " --state=STATE List units with particular LOAD or SUB or ACTIVE state\n"
269 " --failed Shortcut for --state=failed\n"
270 " -p --property=NAME Show only properties by this name\n"
271 " -P NAME Equivalent to --value --property=NAME\n"
272 " -a --all Show all properties/all units currently in memory,\n"
273 " including dead/empty ones. To list all units installed\n"
274 " on the system, use 'list-unit-files' instead.\n"
275 " -l --full Don't ellipsize unit names on output\n"
276 " -r --recursive Show unit list of host and local containers\n"
277 " --reverse Show reverse dependencies with 'list-dependencies'\n"
278 " --with-dependencies Show unit dependencies with 'status', 'cat',\n"
279 " 'list-units', and 'list-unit-files'.\n"
280 " --job-mode=MODE Specify how to deal with already queued jobs, when\n"
281 " queueing a new job\n"
282 " -T --show-transaction When enqueuing a unit job, show full transaction\n"
283 " --show-types When showing sockets, explicitly show their type\n"
284 " --value When showing properties, only print the value\n"
4327574f 285 " --check-inhibitors=MODE\n"
5b69a7c5
ZJS
286 " Whether to check inhibitors before shutting down,\n"
287 " sleeping, or hibernating\n"
4327574f 288 " -i Shortcut for --check-inhibitors=no\n"
4ccde410 289 " --kill-whom=WHOM Whom to send signal to\n"
d06e6199 290 " --kill-value=INT Signal value to enqueue\n"
daf71ef6
LP
291 " -s --signal=SIGNAL Which signal to send\n"
292 " --what=RESOURCES Which types of resources to remove\n"
293 " --now Start or stop unit after enabling or disabling it\n"
294 " --dry-run Only print what would be done\n"
295 " Currently supported by verbs: halt, poweroff, reboot,\n"
34f21ff6
LP
296 " kexec, soft-reboot, suspend, hibernate, \n"
297 " suspend-then-hibernate, hybrid-sleep, default,\n"
298 " rescue, emergency, and exit.\n"
daf71ef6 299 " -q --quiet Suppress output\n"
91dfb74e 300 " --no-warn Suppress several warnings shown by default\n"
daf71ef6
LP
301 " --wait For (re)start, wait until service stopped again\n"
302 " For is-system-running, wait until startup is completed\n"
303 " --no-block Do not wait until operation finished\n"
304 " --no-wall Don't send wall message before halt/power-off/reboot\n"
305 " --no-reload Don't reload daemon after en-/dis-abling unit files\n"
6906da26 306 " --legend=BOOL Enable/disable the legend (column headers and hints)\n"
daf71ef6
LP
307 " --no-pager Do not pipe output into a pager\n"
308 " --no-ask-password Do not ask for system passwords\n"
4218f81d
EP
309 " --global Edit/enable/disable/mask default user unit files\n"
310 " globally\n"
311 " --runtime Edit/enable/disable/mask unit files temporarily until\n"
312 " next reboot\n"
daf71ef6
LP
313 " -f --force When enabling unit files, override existing symlinks\n"
314 " When shutting down, execute action immediately\n"
315 " --preset-mode= Apply only enable, only disable, or all presets\n"
4218f81d
EP
316 " --root=PATH Edit/enable/disable/mask unit files in the specified\n"
317 " root directory\n"
8aa3894e 318 " --image=PATH Edit/enable/disable/mask unit files in the specified\n"
84be0c71
LP
319 " disk image\n"
320 " --image-policy=POLICY\n"
321 " Specify disk image dissection policy\n"
daf71ef6
LP
322 " -n --lines=INTEGER Number of journal entries to show\n"
323 " -o --output=STRING Change journal output mode (short, short-precise,\n"
324 " short-iso, short-iso-precise, short-full,\n"
893bcd3d 325 " short-monotonic, short-unix, short-delta,\n"
daf71ef6
LP
326 " verbose, export, json, json-pretty, json-sse, cat)\n"
327 " --firmware-setup Tell the firmware to show the setup menu on next boot\n"
328 " --boot-loader-menu=TIME\n"
329 " Boot into boot loader menu on next boot\n"
330 " --boot-loader-entry=NAME\n"
331 " Boot into a specific boot loader entry on next boot\n"
332 " --plain Print unit dependencies as a list instead of a tree\n"
b58b4a9f
FS
333 " --timestamp=FORMAT Change format of printed timestamps (pretty, unix,\n"
334 " us, utc, us+utc)\n"
5e8deb94 335 " --read-only Create read-only bind mount\n"
6faecbd3 336 " --mkdir Create directory before mounting, if missing\n"
c9615f73 337 " --marked Restart/reload previously marked units\n"
f206809b 338 " --drop-in=NAME Edit unit files using the specified drop-in file name\n"
1433e1f9
MY
339 " --when=TIME Schedule halt/power-off/reboot/kexec action after\n"
340 " a certain timestamp\n"
329050c5 341 " --stdin Read contents of edited file from stdin\n"
bc556335
DDM
342 "\nSee the %2$s for details.\n",
343 program_invocation_short_name,
344 link,
345 ansi_underline(),
346 ansi_normal(),
347 ansi_highlight(),
348 ansi_normal());
4fbd7192 349
4fbd7192
LP
350 return 0;
351}
352
daf71ef6 353static void help_types(void) {
6906da26 354 if (arg_legend != 0)
daf71ef6 355 puts("Available unit types:");
501fc174 356
daf71ef6 357 DUMP_STRING_TABLE(unit_type, UnitType, _UNIT_TYPE_MAX);
3c756001
LP
358}
359
daf71ef6 360static void help_states(void) {
6906da26 361 if (arg_legend != 0)
daf71ef6
LP
362 puts("Available unit load states:");
363 DUMP_STRING_TABLE(unit_load_state, UnitLoadState, _UNIT_LOAD_STATE_MAX);
22f4096c 364
6906da26 365 if (arg_legend != 0)
daf71ef6
LP
366 puts("\nAvailable unit active states:");
367 DUMP_STRING_TABLE(unit_active_state, UnitActiveState, _UNIT_ACTIVE_STATE_MAX);
22f4096c 368
6906da26 369 if (arg_legend != 0)
daf71ef6
LP
370 puts("\nAvailable unit file states:");
371 DUMP_STRING_TABLE(unit_file_state, UnitFileState, _UNIT_FILE_STATE_MAX);
22f4096c 372
6906da26 373 if (arg_legend != 0)
daf71ef6
LP
374 puts("\nAvailable automount unit substates:");
375 DUMP_STRING_TABLE(automount_state, AutomountState, _AUTOMOUNT_STATE_MAX);
22f4096c 376
6906da26 377 if (arg_legend != 0)
daf71ef6
LP
378 puts("\nAvailable device unit substates:");
379 DUMP_STRING_TABLE(device_state, DeviceState, _DEVICE_STATE_MAX);
4f16c1f4 380
6906da26 381 if (arg_legend != 0)
daf71ef6
LP
382 puts("\nAvailable mount unit substates:");
383 DUMP_STRING_TABLE(mount_state, MountState, _MOUNT_STATE_MAX);
729e3769 384
6906da26 385 if (arg_legend != 0)
daf71ef6
LP
386 puts("\nAvailable path unit substates:");
387 DUMP_STRING_TABLE(path_state, PathState, _PATH_STATE_MAX);
729e3769 388
6906da26 389 if (arg_legend != 0)
daf71ef6
LP
390 puts("\nAvailable scope unit substates:");
391 DUMP_STRING_TABLE(scope_state, ScopeState, _SCOPE_STATE_MAX);
729e3769 392
6906da26 393 if (arg_legend != 0)
daf71ef6
LP
394 puts("\nAvailable service unit substates:");
395 DUMP_STRING_TABLE(service_state, ServiceState, _SERVICE_STATE_MAX);
729e3769 396
6906da26 397 if (arg_legend != 0)
daf71ef6
LP
398 puts("\nAvailable slice unit substates:");
399 DUMP_STRING_TABLE(slice_state, SliceState, _SLICE_STATE_MAX);
b41b9d2a 400
6906da26 401 if (arg_legend != 0)
daf71ef6
LP
402 puts("\nAvailable socket unit substates:");
403 DUMP_STRING_TABLE(socket_state, SocketState, _SOCKET_STATE_MAX);
729e3769 404
6906da26 405 if (arg_legend != 0)
daf71ef6
LP
406 puts("\nAvailable swap unit substates:");
407 DUMP_STRING_TABLE(swap_state, SwapState, _SWAP_STATE_MAX);
16484a8a 408
6906da26 409 if (arg_legend != 0)
daf71ef6
LP
410 puts("\nAvailable target unit substates:");
411 DUMP_STRING_TABLE(target_state, TargetState, _TARGET_STATE_MAX);
16484a8a 412
6906da26 413 if (arg_legend != 0)
daf71ef6
LP
414 puts("\nAvailable timer unit substates:");
415 DUMP_STRING_TABLE(timer_state, TimerState, _TIMER_STATE_MAX);
16484a8a
ZJS
416}
417
daf71ef6
LP
418static int systemctl_parse_argv(int argc, char *argv[]) {
419 enum {
6906da26 420 ARG_FAIL = 0x100, /* compatibility only */
daf71ef6
LP
421 ARG_REVERSE,
422 ARG_AFTER,
423 ARG_BEFORE,
4327574f 424 ARG_CHECK_INHIBITORS,
daf71ef6
LP
425 ARG_DRY_RUN,
426 ARG_SHOW_TYPES,
6906da26
ZJS
427 ARG_IRREVERSIBLE, /* compatibility only */
428 ARG_IGNORE_DEPENDENCIES, /* compatibility only */
daf71ef6
LP
429 ARG_VALUE,
430 ARG_VERSION,
431 ARG_USER,
432 ARG_SYSTEM,
433 ARG_GLOBAL,
434 ARG_NO_BLOCK,
6906da26
ZJS
435 ARG_LEGEND,
436 ARG_NO_LEGEND, /* compatibility only */
daf71ef6
LP
437 ARG_NO_PAGER,
438 ARG_NO_WALL,
439 ARG_ROOT,
8aa3894e 440 ARG_IMAGE,
06e78680 441 ARG_IMAGE_POLICY,
daf71ef6 442 ARG_NO_RELOAD,
4ccde410 443 ARG_KILL_WHOM,
d06e6199 444 ARG_KILL_VALUE,
daf71ef6
LP
445 ARG_NO_ASK_PASSWORD,
446 ARG_FAILED,
447 ARG_RUNTIME,
448 ARG_PLAIN,
449 ARG_STATE,
450 ARG_JOB_MODE,
451 ARG_PRESET_MODE,
452 ARG_FIRMWARE_SETUP,
453 ARG_BOOT_LOADER_MENU,
454 ARG_BOOT_LOADER_ENTRY,
455 ARG_NOW,
456 ARG_MESSAGE,
457 ARG_WITH_DEPENDENCIES,
458 ARG_WAIT,
459 ARG_WHAT,
460 ARG_REBOOT_ARG,
461 ARG_TIMESTAMP_STYLE,
5e8deb94
LB
462 ARG_READ_ONLY,
463 ARG_MKDIR,
c9615f73 464 ARG_MARKED,
108d35ac 465 ARG_NO_WARN,
f206809b 466 ARG_DROP_IN,
1433e1f9 467 ARG_WHEN,
329050c5 468 ARG_STDIN,
daf71ef6 469 };
1238ee09 470
daf71ef6
LP
471 static const struct option options[] = {
472 { "help", no_argument, NULL, 'h' },
473 { "version", no_argument, NULL, ARG_VERSION },
474 { "type", required_argument, NULL, 't' },
475 { "property", required_argument, NULL, 'p' },
476 { "all", no_argument, NULL, 'a' },
477 { "reverse", no_argument, NULL, ARG_REVERSE },
478 { "after", no_argument, NULL, ARG_AFTER },
479 { "before", no_argument, NULL, ARG_BEFORE },
480 { "show-types", no_argument, NULL, ARG_SHOW_TYPES },
40a49f12 481 { "failed", no_argument, NULL, ARG_FAILED },
daf71ef6
LP
482 { "full", no_argument, NULL, 'l' },
483 { "job-mode", required_argument, NULL, ARG_JOB_MODE },
484 { "fail", no_argument, NULL, ARG_FAIL }, /* compatibility only */
485 { "irreversible", no_argument, NULL, ARG_IRREVERSIBLE }, /* compatibility only */
486 { "ignore-dependencies", no_argument, NULL, ARG_IGNORE_DEPENDENCIES }, /* compatibility only */
4327574f
FS
487 { "ignore-inhibitors", no_argument, NULL, 'i' }, /* compatibility only */
488 { "check-inhibitors", required_argument, NULL, ARG_CHECK_INHIBITORS },
daf71ef6
LP
489 { "value", no_argument, NULL, ARG_VALUE },
490 { "user", no_argument, NULL, ARG_USER },
491 { "system", no_argument, NULL, ARG_SYSTEM },
492 { "global", no_argument, NULL, ARG_GLOBAL },
493 { "wait", no_argument, NULL, ARG_WAIT },
494 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
6906da26
ZJS
495 { "legend", required_argument, NULL, ARG_LEGEND },
496 { "no-legend", no_argument, NULL, ARG_NO_LEGEND }, /* compatibility only */
daf71ef6
LP
497 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
498 { "no-wall", no_argument, NULL, ARG_NO_WALL },
499 { "dry-run", no_argument, NULL, ARG_DRY_RUN },
500 { "quiet", no_argument, NULL, 'q' },
108d35ac 501 { "no-warn", no_argument, NULL, ARG_NO_WARN },
daf71ef6 502 { "root", required_argument, NULL, ARG_ROOT },
8aa3894e 503 { "image", required_argument, NULL, ARG_IMAGE },
06e78680 504 { "image-policy", required_argument, NULL, ARG_IMAGE_POLICY },
daf71ef6
LP
505 { "force", no_argument, NULL, 'f' },
506 { "no-reload", no_argument, NULL, ARG_NO_RELOAD },
4ccde410 507 { "kill-whom", required_argument, NULL, ARG_KILL_WHOM },
d06e6199 508 { "kill-value", required_argument, NULL, ARG_KILL_VALUE },
daf71ef6
LP
509 { "signal", required_argument, NULL, 's' },
510 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
511 { "host", required_argument, NULL, 'H' },
512 { "machine", required_argument, NULL, 'M' },
513 { "runtime", no_argument, NULL, ARG_RUNTIME },
514 { "lines", required_argument, NULL, 'n' },
515 { "output", required_argument, NULL, 'o' },
516 { "plain", no_argument, NULL, ARG_PLAIN },
517 { "state", required_argument, NULL, ARG_STATE },
518 { "recursive", no_argument, NULL, 'r' },
519 { "with-dependencies", no_argument, NULL, ARG_WITH_DEPENDENCIES },
520 { "preset-mode", required_argument, NULL, ARG_PRESET_MODE },
521 { "firmware-setup", no_argument, NULL, ARG_FIRMWARE_SETUP },
522 { "boot-loader-menu", required_argument, NULL, ARG_BOOT_LOADER_MENU },
523 { "boot-loader-entry", required_argument, NULL, ARG_BOOT_LOADER_ENTRY },
524 { "now", no_argument, NULL, ARG_NOW },
525 { "message", required_argument, NULL, ARG_MESSAGE },
526 { "show-transaction", no_argument, NULL, 'T' },
527 { "what", required_argument, NULL, ARG_WHAT },
528 { "reboot-argument", required_argument, NULL, ARG_REBOOT_ARG },
529 { "timestamp", required_argument, NULL, ARG_TIMESTAMP_STYLE },
5e8deb94
LB
530 { "read-only", no_argument, NULL, ARG_READ_ONLY },
531 { "mkdir", no_argument, NULL, ARG_MKDIR },
c9615f73 532 { "marked", no_argument, NULL, ARG_MARKED },
f206809b 533 { "drop-in", required_argument, NULL, ARG_DROP_IN },
1433e1f9 534 { "when", required_argument, NULL, ARG_WHEN },
329050c5 535 { "stdin", no_argument, NULL, ARG_STDIN },
daf71ef6
LP
536 {}
537 };
1238ee09 538
daf71ef6 539 int c, r;
d309c1c3 540
daf71ef6
LP
541 assert(argc >= 0);
542 assert(argv);
d309c1c3 543
daf71ef6
LP
544 /* We default to allowing interactive authorization only in systemctl (not in the legacy commands) */
545 arg_ask_password = true;
d309c1c3 546
daf71ef6 547 while ((c = getopt_long(argc, argv, "ht:p:P:alqfs:H:M:n:o:iTr.::", options, NULL)) >= 0)
57ab2eab 548
daf71ef6 549 switch (c) {
9ef15026 550
daf71ef6
LP
551 case 'h':
552 return systemctl_help();
85d9b598 553
daf71ef6
LP
554 case ARG_VERSION:
555 return version();
e9c387c8 556
76e5e267 557 case 't':
d81bc51f 558 if (isempty(optarg))
daf71ef6
LP
559 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
560 "--type= requires arguments.");
d81bc51f 561
76e5e267 562 for (const char *p = optarg;;) {
daf71ef6 563 _cleanup_free_ char *type = NULL;
d81bc51f 564
daf71ef6 565 r = extract_first_word(&p, &type, ",", 0);
d81bc51f 566 if (r < 0)
daf71ef6 567 return log_error_errno(r, "Failed to parse type: %s", optarg);
d81bc51f
LP
568 if (r == 0)
569 break;
570
daf71ef6
LP
571 if (streq(type, "help")) {
572 help_types();
d81bc51f
LP
573 return 0;
574 }
575
daf71ef6
LP
576 if (unit_type_from_string(type) >= 0) {
577 if (strv_consume(&arg_types, TAKE_PTR(type)) < 0)
578 return log_oom();
579 continue;
580 }
581
582 /* It's much nicer to use --state= for load states, but let's support this in
583 * --types= too for compatibility with old versions */
584 if (unit_load_state_from_string(type) >= 0) {
585 if (strv_consume(&arg_states, TAKE_PTR(type)) < 0)
586 return log_oom();
587 continue;
588 }
589
590 log_error("Unknown unit type or load state '%s'.", type);
591 return log_info_errno(SYNTHETIC_ERRNO(EINVAL),
592 "Use -t help to see a list of allowed values.");
d81bc51f
LP
593 }
594
595 break;
d81bc51f 596
daf71ef6 597 case 'P':
255b1fc8 598 SET_FLAG(arg_print_flags, BUS_PRINT_PROPERTY_ONLY_VALUE, true);
daf71ef6 599 _fallthrough_;
dae710be 600
daf71ef6
LP
601 case 'p':
602 /* Make sure that if the empty property list was specified, we won't show any
603 properties. */
604 if (isempty(optarg) && !arg_properties) {
605 arg_properties = new0(char*, 1);
606 if (!arg_properties)
607 return log_oom();
76e5e267
ZJS
608 } else
609 for (const char *p = optarg;;) {
daf71ef6 610 _cleanup_free_ char *prop = NULL;
46ad9c53 611
daf71ef6
LP
612 r = extract_first_word(&p, &prop, ",", 0);
613 if (r < 0)
614 return log_error_errno(r, "Failed to parse property: %s", optarg);
615 if (r == 0)
616 break;
46ad9c53 617
daf71ef6
LP
618 if (strv_consume(&arg_properties, TAKE_PTR(prop)) < 0)
619 return log_oom();
620 }
2e7e19ca 621
daf71ef6 622 /* If the user asked for a particular property, show it, even if it is empty. */
255b1fc8 623 SET_FLAG(arg_print_flags, BUS_PRINT_PROPERTY_SHOW_EMPTY, true);
7e4249b9 624
daf71ef6 625 break;
7e4249b9 626
daf71ef6 627 case 'a':
255b1fc8 628 SET_FLAG(arg_print_flags, BUS_PRINT_PROPERTY_SHOW_EMPTY, true);
daf71ef6
LP
629 arg_all = true;
630 break;
a8f11321 631
daf71ef6
LP
632 case ARG_REVERSE:
633 arg_dependency = DEPENDENCY_REVERSE;
634 break;
93a08841 635
daf71ef6
LP
636 case ARG_AFTER:
637 arg_dependency = DEPENDENCY_AFTER;
638 arg_jobs_after = true;
639 break;
7e4249b9 640
daf71ef6
LP
641 case ARG_BEFORE:
642 arg_dependency = DEPENDENCY_BEFORE;
643 arg_jobs_before = true;
644 break;
e4b61340 645
daf71ef6
LP
646 case ARG_SHOW_TYPES:
647 arg_show_types = true;
648 break;
e4b61340 649
daf71ef6 650 case ARG_VALUE:
255b1fc8 651 SET_FLAG(arg_print_flags, BUS_PRINT_PROPERTY_ONLY_VALUE, true);
daf71ef6 652 break;
e4b61340 653
daf71ef6 654 case ARG_JOB_MODE:
a88f9dba 655 _arg_job_mode = optarg;
daf71ef6 656 break;
e4b61340 657
daf71ef6 658 case ARG_FAIL:
a88f9dba 659 _arg_job_mode = "fail";
daf71ef6 660 break;
e4b61340 661
daf71ef6 662 case ARG_IRREVERSIBLE:
a88f9dba 663 _arg_job_mode = "replace-irreversibly";
daf71ef6 664 break;
e4b61340 665
daf71ef6 666 case ARG_IGNORE_DEPENDENCIES:
a88f9dba 667 _arg_job_mode = "ignore-dependencies";
daf71ef6 668 break;
e4b61340 669
daf71ef6 670 case ARG_USER:
4870133b 671 arg_runtime_scope = RUNTIME_SCOPE_USER;
e4b61340
LP
672 break;
673
daf71ef6 674 case ARG_SYSTEM:
4870133b 675 arg_runtime_scope = RUNTIME_SCOPE_SYSTEM;
e4b61340
LP
676 break;
677
daf71ef6 678 case ARG_GLOBAL:
4870133b 679 arg_runtime_scope = RUNTIME_SCOPE_GLOBAL;
e4b61340
LP
680 break;
681
daf71ef6
LP
682 case ARG_WAIT:
683 arg_wait = true;
e4b61340
LP
684 break;
685
daf71ef6
LP
686 case ARG_NO_BLOCK:
687 arg_no_block = true;
e4b61340
LP
688 break;
689
daf71ef6 690 case ARG_NO_LEGEND:
6906da26
ZJS
691 arg_legend = false;
692 break;
693
694 case ARG_LEGEND:
695 r = parse_boolean_argument("--legend", optarg, NULL);
696 if (r < 0)
697 return r;
698 arg_legend = r;
e4b61340
LP
699 break;
700
daf71ef6
LP
701 case ARG_NO_PAGER:
702 arg_pager_flags |= PAGER_DISABLE;
f3f054f0
TB
703 break;
704
514f4ef5
LP
705 case ARG_NO_WALL:
706 arg_no_wall = true;
707 break;
708
daf71ef6 709 case ARG_ROOT:
614b022c 710 r = parse_path_argument(optarg, false, &arg_root);
daf71ef6
LP
711 if (r < 0)
712 return r;
e4b61340
LP
713 break;
714
8aa3894e
RP
715 case ARG_IMAGE:
716 r = parse_path_argument(optarg, false, &arg_image);
717 if (r < 0)
718 return r;
719 break;
720
06e78680
YW
721 case ARG_IMAGE_POLICY:
722 r = parse_image_policy_argument(optarg, &arg_image_policy);
723 if (r < 0)
724 return r;
725 break;
726
daf71ef6
LP
727 case 'l':
728 arg_full = true;
729 break;
e4b61340 730
daf71ef6
LP
731 case ARG_FAILED:
732 if (strv_extend(&arg_states, "failed") < 0)
733 return log_oom();
e4b61340 734
daf71ef6 735 break;
e4b61340 736
daf71ef6
LP
737 case ARG_DRY_RUN:
738 arg_dry_run = true;
739 break;
e4b61340 740
daf71ef6
LP
741 case 'q':
742 arg_quiet = true;
6906da26
ZJS
743
744 if (arg_legend < 0)
745 arg_legend = false;
746
daf71ef6 747 break;
e4b61340 748
daf71ef6
LP
749 case 'f':
750 arg_force++;
751 break;
e4b61340 752
daf71ef6
LP
753 case ARG_NO_RELOAD:
754 arg_no_reload = true;
755 break;
e4b61340 756
4ccde410
ZJS
757 case ARG_KILL_WHOM:
758 arg_kill_whom = optarg;
daf71ef6 759 break;
e4b61340 760
d06e6199
LP
761 case ARG_KILL_VALUE: {
762 unsigned u;
763
764 if (isempty(optarg)) {
765 arg_kill_value_set = false;
766 return 0;
767 }
768
769 /* First, try to parse unsigned, so that we can support the prefixes 0x, 0o, 0b */
770 r = safe_atou_full(optarg, 0, &u);
771 if (r < 0)
772 /* If this didn't work, try as signed integer, without those prefixes */
773 r = safe_atoi(optarg, &arg_kill_value);
774 else if (u > INT_MAX)
775 r = -ERANGE;
776 else
777 arg_kill_value = (int) u;
778 if (r < 0)
779 return log_error_errno(r, "Unable to parse signal queue value: %s", optarg);
780
781 arg_kill_value_set = true;
782 break;
783 }
784
daf71ef6 785 case 's':
86beb213
ZJS
786 r = parse_signal_argument(optarg, &arg_signal);
787 if (r <= 0)
788 return r;
daf71ef6 789 break;
e4b61340 790
daf71ef6
LP
791 case ARG_NO_ASK_PASSWORD:
792 arg_ask_password = false;
793 break;
e4b61340
LP
794
795 case 'H':
daf71ef6
LP
796 arg_transport = BUS_TRANSPORT_REMOTE;
797 arg_host = optarg;
e4b61340
LP
798 break;
799
daf71ef6
LP
800 case 'M':
801 arg_transport = BUS_TRANSPORT_MACHINE;
802 arg_host = optarg;
e4b61340
LP
803 break;
804
daf71ef6
LP
805 case ARG_RUNTIME:
806 arg_runtime = true;
e4b61340
LP
807 break;
808
daf71ef6
LP
809 case 'n':
810 if (safe_atou(optarg, &arg_lines) < 0)
811 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
812 "Failed to parse lines '%s'",
813 optarg);
04ebb595
LP
814 break;
815
daf71ef6
LP
816 case 'o':
817 if (streq(optarg, "help")) {
818 DUMP_STRING_TABLE(output_mode, OutputMode, _OUTPUT_MODE_MAX);
819 return 0;
820 }
821
822 arg_output = output_mode_from_string(optarg);
823 if (arg_output < 0)
824 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
825 "Unknown output '%s'.",
826 optarg);
827
828 if (OUTPUT_MODE_IS_JSON(arg_output)) {
6906da26 829 arg_legend = false;
daf71ef6
LP
830 arg_plain = true;
831 }
e4b61340
LP
832 break;
833
daf71ef6 834 case 'i':
4327574f
FS
835 arg_check_inhibitors = 0;
836 break;
837
838 case ARG_CHECK_INHIBITORS:
b71a721f
LP
839 r = parse_tristate_full(optarg, "auto", &arg_check_inhibitors);
840 if (r < 0)
841 return log_error_errno(r, "Failed to parse --check-inhibitors= argument: %s", optarg);
e4b61340
LP
842 break;
843
daf71ef6
LP
844 case ARG_PLAIN:
845 arg_plain = true;
514f4ef5
LP
846 break;
847
daf71ef6
LP
848 case ARG_FIRMWARE_SETUP:
849 arg_firmware_setup = true;
e4b61340
LP
850 break;
851
daf71ef6 852 case ARG_BOOT_LOADER_MENU:
f6144808 853
daf71ef6
LP
854 r = parse_sec(optarg, &arg_boot_loader_menu);
855 if (r < 0)
856 return log_error_errno(r, "Failed to parse --boot-loader-menu= argument '%s': %m", optarg);
e4b61340 857
daf71ef6 858 break;
e4b61340 859
daf71ef6 860 case ARG_BOOT_LOADER_ENTRY:
e4b61340 861
daf71ef6
LP
862 if (streq(optarg, "help")) { /* Yes, this means, "help" is not a valid boot loader entry name we can deal with */
863 r = help_boot_loader_entry();
864 if (r < 0)
865 return r;
e4b61340 866
daf71ef6
LP
867 return 0;
868 }
e4b61340 869
daf71ef6
LP
870 arg_boot_loader_entry = empty_to_null(optarg);
871 break;
e4b61340 872
76e5e267 873 case ARG_STATE:
daf71ef6
LP
874 if (isempty(optarg))
875 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
876 "--state= requires arguments.");
e4b61340 877
76e5e267 878 for (const char *p = optarg;;) {
daf71ef6 879 _cleanup_free_ char *s = NULL;
e4b61340 880
daf71ef6
LP
881 r = extract_first_word(&p, &s, ",", 0);
882 if (r < 0)
883 return log_error_errno(r, "Failed to parse state: %s", optarg);
884 if (r == 0)
885 break;
e4b61340 886
daf71ef6
LP
887 if (streq(s, "help")) {
888 help_states();
889 return 0;
890 }
e4b61340 891
daf71ef6
LP
892 if (strv_consume(&arg_states, TAKE_PTR(s)) < 0)
893 return log_oom();
894 }
895 break;
e4b61340 896
daf71ef6
LP
897 case 'r':
898 if (geteuid() != 0)
899 return log_error_errno(SYNTHETIC_ERRNO(EPERM),
900 "--recursive requires root privileges.");
901
902 arg_recursive = true;
514f4ef5
LP
903 break;
904
daf71ef6
LP
905 case ARG_PRESET_MODE:
906 if (streq(optarg, "help")) {
b364c4de 907 DUMP_STRING_TABLE(unit_file_preset_mode, UnitFilePresetMode, _UNIT_FILE_PRESET_MODE_MAX);
daf71ef6
LP
908 return 0;
909 }
e4b61340 910
daf71ef6
LP
911 arg_preset_mode = unit_file_preset_mode_from_string(optarg);
912 if (arg_preset_mode < 0)
913 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
914 "Failed to parse preset mode: %s.", optarg);
e4b61340 915
daf71ef6 916 break;
e4b61340 917
daf71ef6
LP
918 case ARG_NOW:
919 arg_now = true;
920 break;
e4b61340 921
daf71ef6
LP
922 case ARG_MESSAGE:
923 if (strv_extend(&arg_wall, optarg) < 0)
924 return log_oom();
925 break;
e4b61340 926
daf71ef6
LP
927 case 'T':
928 arg_show_transaction = true;
e4b61340
LP
929 break;
930
daf71ef6
LP
931 case ARG_WITH_DEPENDENCIES:
932 arg_with_dependencies = true;
933 break;
e4b61340 934
76e5e267 935 case ARG_WHAT:
daf71ef6
LP
936 if (isempty(optarg))
937 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--what= requires arguments.");
e4b61340 938
76e5e267 939 for (const char *p = optarg;;) {
daf71ef6 940 _cleanup_free_ char *k = NULL;
e4b61340 941
daf71ef6
LP
942 r = extract_first_word(&p, &k, ",", 0);
943 if (r < 0)
944 return log_error_errno(r, "Failed to parse directory type: %s", optarg);
945 if (r == 0)
946 break;
e4b61340 947
daf71ef6
LP
948 if (streq(k, "help")) {
949 puts("runtime\n"
950 "state\n"
951 "cache\n"
952 "logs\n"
4fb8f1e8
LP
953 "configuration\n"
954 "fdstore");
daf71ef6
LP
955 return 0;
956 }
e4b61340 957
daf71ef6
LP
958 r = strv_consume(&arg_clean_what, TAKE_PTR(k));
959 if (r < 0)
960 return log_oom();
961 }
e4b61340 962
daf71ef6 963 break;
e4b61340 964
daf71ef6
LP
965 case ARG_REBOOT_ARG:
966 arg_reboot_argument = optarg;
967 break;
968
969 case ARG_TIMESTAMP_STYLE:
970 if (streq(optarg, "help")) {
971 DUMP_STRING_TABLE(timestamp_style, TimestampStyle, _TIMESTAMP_STYLE_MAX);
972 return 0;
973 }
974
975 arg_timestamp_style = timestamp_style_from_string(optarg);
976 if (arg_timestamp_style < 0)
977 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
978 "Invalid value: %s.", optarg);
979
980 break;
e4b61340 981
5e8deb94
LB
982 case ARG_READ_ONLY:
983 arg_read_only = true;
984 break;
985
986 case ARG_MKDIR:
987 arg_mkdir = true;
988 break;
989
c9615f73
ZJS
990 case ARG_MARKED:
991 arg_marked = true;
992 break;
993
108d35ac
MY
994 case ARG_NO_WARN:
995 arg_no_warn = true;
996 break;
997
f206809b
MY
998 case ARG_DROP_IN:
999 arg_drop_in = optarg;
1000 break;
1001
1433e1f9
MY
1002 case ARG_WHEN:
1003 if (streq(optarg, "show")) {
1004 r = logind_show_shutdown();
1005 if (r < 0 && r != -ENODATA)
1006 return r;
1007
1008 return 0;
1009 }
1010
1011 if (STR_IN_SET(optarg, "", "cancel")) {
1012 arg_when = USEC_INFINITY;
1013 break;
1014 }
1015
1016 r = parse_timestamp(optarg, &arg_when);
1017 if (r < 0)
1018 return log_error_errno(r, "Failed to parse --when= argument '%s': %m", optarg);
1019
1020 if (!timestamp_is_set(arg_when))
1021 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1022 "Invalid timestamp '%s' specified for --when=.", optarg);
1023
1024 break;
1025
329050c5
ZJS
1026 case ARG_STDIN:
1027 arg_stdin = true;
1028 break;
1029
daf71ef6
LP
1030 case '.':
1031 /* Output an error mimicking getopt, and print a hint afterwards */
1032 log_error("%s: invalid option -- '.'", program_invocation_name);
1033 log_notice("Hint: to specify units starting with a dash, use \"--\":\n"
1034 " %s [OPTIONS...] COMMAND -- -.%s ...",
1035 program_invocation_name, optarg ?: "mount");
1036 _fallthrough_;
e4b61340
LP
1037
1038 case '?':
1039 return -EINVAL;
1040
1041 default:
04499a70 1042 assert_not_reached();
e4b61340 1043 }
e4b61340 1044
966f3a24
LP
1045 /* If we are in --user mode, there's no point in talking to PolicyKit or the infra to query system
1046 * passwords */
4870133b 1047 if (arg_runtime_scope != RUNTIME_SCOPE_SYSTEM)
966f3a24
LP
1048 arg_ask_password = false;
1049
4870133b 1050 if (arg_transport == BUS_TRANSPORT_REMOTE && arg_runtime_scope != RUNTIME_SCOPE_SYSTEM)
daf71ef6
LP
1051 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1052 "Cannot access user instance remotely.");
1053
1054 if (arg_wait && arg_no_block)
baaa35ad 1055 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
daf71ef6 1056 "--wait may not be combined with --no-block.");
e4b61340 1057
c9615f73
ZJS
1058 bool do_reload_or_restart = streq_ptr(argv[optind], "reload-or-restart");
1059 if (arg_marked) {
1060 if (!do_reload_or_restart)
1061 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1062 "--marked may only be used with 'reload-or-restart'.");
1063 if (optind + 1 < argc)
1064 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1065 "No additional arguments allowed with 'reload-or-restart --marked'.");
1066 if (arg_wait)
1067 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1068 "--marked --wait is not supported.");
1069 if (arg_show_transaction)
1070 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1071 "--marked --show-transaction is not supported.");
1072
1073 } else if (do_reload_or_restart) {
1074 if (optind + 1 >= argc)
1075 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1076 "List of units to restart/reload is required.");
1077 }
1078
8aa3894e 1079 if (arg_image && arg_root)
329050c5
ZJS
1080 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1081 "Please specify either --root= or --image=, the combination of both is not supported.");
8aa3894e 1082
e4b61340
LP
1083 return 1;
1084}
1085
5fd87823 1086int systemctl_dispatch_parse_argv(int argc, char *argv[]) {
e4b61340
LP
1087 assert(argc >= 0);
1088 assert(argv);
1089
2306d177 1090 if (invoked_as(argv, "halt")) {
d41a9e4f
ZJS
1091 arg_action = ACTION_HALT;
1092 return halt_parse_argv(argc, argv);
1093
2306d177 1094 } else if (invoked_as(argv, "poweroff")) {
d41a9e4f
ZJS
1095 arg_action = ACTION_POWEROFF;
1096 return halt_parse_argv(argc, argv);
1097
2306d177 1098 } else if (invoked_as(argv, "reboot")) {
665a3d6d 1099 arg_action = ACTION_REBOOT;
d41a9e4f
ZJS
1100 return halt_parse_argv(argc, argv);
1101
2306d177 1102 } else if (invoked_as(argv, "shutdown")) {
d41a9e4f
ZJS
1103 arg_action = ACTION_POWEROFF;
1104 return shutdown_parse_argv(argc, argv);
1105
2306d177 1106 } else if (invoked_as(argv, "init")) {
d41a9e4f
ZJS
1107
1108 /* Matches invocations as "init" as well as "telinit", which are synonymous when run
1109 * as PID != 1 on SysV.
1110 *
1111 * On SysV "telinit" was the official command to communicate with PID 1, but "init" would
1112 * redirect itself to "telinit" if called with PID != 1. We follow the same logic here still,
1113 * though we add one level of indirection, as we implement "telinit" in "systemctl". Hence,
1114 * for us if you invoke "init" you get "systemd", but it will execve() "systemctl"
1115 * immediately with argv[] unmodified if PID is != 1. If you invoke "telinit" you directly
1116 * get "systemctl". In both cases we shall do the same thing, which is why we do
2306d177 1117 * invoked_as(argv, "init") here, as a quick way to match both.
d41a9e4f
ZJS
1118 *
1119 * Also see redirect_telinit() in src/core/main.c. */
1120
50b68cff
MB
1121 arg_action = _ACTION_INVALID; /* telinit_parse_argv() will figure out the actual action we'll execute */
1122 return telinit_parse_argv(argc, argv);
d41a9e4f 1123
2306d177 1124 } else if (invoked_as(argv, "runlevel")) {
d41a9e4f
ZJS
1125 arg_action = ACTION_RUNLEVEL;
1126 return runlevel_parse_argv(argc, argv);
e4b61340
LP
1127 }
1128
1129 arg_action = ACTION_SYSTEMCTL;
1130 return systemctl_parse_argv(argc, argv);
1131}
1132
5fd87823 1133#ifndef FUZZ_SYSTEMCTL_PARSE_ARGV
e449de87 1134static int systemctl_main(int argc, char *argv[]) {
e449de87 1135 static const Verb verbs[] = {
32baf64d
LP
1136 { "list-units", VERB_ANY, VERB_ANY, VERB_DEFAULT|VERB_ONLINE_ONLY, verb_list_units },
1137 { "list-unit-files", VERB_ANY, VERB_ANY, 0, verb_list_unit_files },
ed462ea3 1138 { "list-automounts", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, verb_list_automounts },
5fb5f49b 1139 { "list-paths", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, verb_list_paths },
32baf64d
LP
1140 { "list-sockets", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, verb_list_sockets },
1141 { "list-timers", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, verb_list_timers },
1142 { "list-jobs", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, verb_list_jobs },
1143 { "list-machines", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, verb_list_machines },
1144 { "clear-jobs", VERB_ANY, 1, VERB_ONLINE_ONLY, verb_trivial_method },
1145 { "cancel", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, verb_cancel },
1146 { "start", 2, VERB_ANY, VERB_ONLINE_ONLY, verb_start },
1147 { "stop", 2, VERB_ANY, VERB_ONLINE_ONLY, verb_start },
1148 { "condstop", 2, VERB_ANY, VERB_ONLINE_ONLY, verb_start }, /* For compatibility with ALTLinux */
1149 { "reload", 2, VERB_ANY, VERB_ONLINE_ONLY, verb_start },
1150 { "restart", 2, VERB_ANY, VERB_ONLINE_ONLY, verb_start },
1151 { "try-restart", 2, VERB_ANY, VERB_ONLINE_ONLY, verb_start },
1152 { "reload-or-restart", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, verb_start },
1153 { "reload-or-try-restart", 2, VERB_ANY, VERB_ONLINE_ONLY, verb_start }, /* For compatibility with old systemctl <= 228 */
1154 { "try-reload-or-restart", 2, VERB_ANY, VERB_ONLINE_ONLY, verb_start },
1155 { "force-reload", 2, VERB_ANY, VERB_ONLINE_ONLY, verb_start }, /* For compatibility with SysV */
1156 { "condreload", 2, VERB_ANY, VERB_ONLINE_ONLY, verb_start }, /* For compatibility with ALTLinux */
1157 { "condrestart", 2, VERB_ANY, VERB_ONLINE_ONLY, verb_start }, /* For compatibility with RH */
1158 { "isolate", 2, 2, VERB_ONLINE_ONLY, verb_start },
1159 { "kill", 2, VERB_ANY, VERB_ONLINE_ONLY, verb_kill },
1160 { "clean", 2, VERB_ANY, VERB_ONLINE_ONLY, verb_clean_or_freeze },
1161 { "freeze", 2, VERB_ANY, VERB_ONLINE_ONLY, verb_clean_or_freeze },
1162 { "thaw", 2, VERB_ANY, VERB_ONLINE_ONLY, verb_clean_or_freeze },
1163 { "is-active", 2, VERB_ANY, VERB_ONLINE_ONLY, verb_is_active },
1164 { "check", 2, VERB_ANY, VERB_ONLINE_ONLY, verb_is_active }, /* deprecated alias of is-active */
2ee34616 1165 { "is-failed", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, verb_is_failed },
32baf64d
LP
1166 { "show", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, verb_show },
1167 { "cat", 2, VERB_ANY, VERB_ONLINE_ONLY, verb_cat },
1168 { "status", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, verb_show },
1169 { "help", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, verb_show },
623461c1
LP
1170 { "daemon-reload", 1, 1, VERB_ONLINE_ONLY, verb_daemon_reload },
1171 { "daemon-reexec", 1, 1, VERB_ONLINE_ONLY, verb_daemon_reload },
32baf64d
LP
1172 { "log-level", VERB_ANY, 2, VERB_ONLINE_ONLY, verb_log_setting },
1173 { "log-target", VERB_ANY, 2, VERB_ONLINE_ONLY, verb_log_setting },
1174 { "service-log-level", 2, 3, VERB_ONLINE_ONLY, verb_service_log_setting },
1175 { "service-log-target", 2, 3, VERB_ONLINE_ONLY, verb_service_log_setting },
1176 { "service-watchdogs", VERB_ANY, 2, VERB_ONLINE_ONLY, verb_service_watchdogs },
1177 { "show-environment", VERB_ANY, 1, VERB_ONLINE_ONLY, verb_show_environment },
1178 { "set-environment", 2, VERB_ANY, VERB_ONLINE_ONLY, verb_set_environment },
1179 { "unset-environment", 2, VERB_ANY, VERB_ONLINE_ONLY, verb_set_environment },
1180 { "import-environment", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, verb_import_environment },
1181 { "halt", VERB_ANY, 1, VERB_ONLINE_ONLY, verb_start_system_special },
1182 { "poweroff", VERB_ANY, 1, VERB_ONLINE_ONLY, verb_start_system_special },
31853609 1183 { "reboot", VERB_ANY, 1, VERB_ONLINE_ONLY, verb_start_system_special },
32baf64d 1184 { "kexec", VERB_ANY, 1, VERB_ONLINE_ONLY, verb_start_system_special },
34f21ff6 1185 { "soft-reboot", VERB_ANY, 1, VERB_ONLINE_ONLY, verb_start_system_special },
5b356289 1186 { "sleep", VERB_ANY, 1, VERB_ONLINE_ONLY, verb_start_system_special },
32baf64d
LP
1187 { "suspend", VERB_ANY, 1, VERB_ONLINE_ONLY, verb_start_system_special },
1188 { "hibernate", VERB_ANY, 1, VERB_ONLINE_ONLY, verb_start_system_special },
1189 { "hybrid-sleep", VERB_ANY, 1, VERB_ONLINE_ONLY, verb_start_system_special },
1190 { "suspend-then-hibernate",VERB_ANY, 1, VERB_ONLINE_ONLY, verb_start_system_special },
1191 { "default", VERB_ANY, 1, VERB_ONLINE_ONLY, verb_start_special },
1192 { "rescue", VERB_ANY, 1, VERB_ONLINE_ONLY, verb_start_system_special },
1193 { "emergency", VERB_ANY, 1, VERB_ONLINE_ONLY, verb_start_system_special },
1194 { "exit", VERB_ANY, 2, VERB_ONLINE_ONLY, verb_start_special },
1195 { "reset-failed", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, verb_reset_failed },
1196 { "enable", 2, VERB_ANY, 0, verb_enable },
1197 { "disable", 2, VERB_ANY, 0, verb_enable },
1198 { "is-enabled", 2, VERB_ANY, 0, verb_is_enabled },
1199 { "reenable", 2, VERB_ANY, 0, verb_enable },
1200 { "preset", 2, VERB_ANY, 0, verb_enable },
1201 { "preset-all", VERB_ANY, 1, 0, verb_preset_all },
1202 { "mask", 2, VERB_ANY, 0, verb_enable },
1203 { "unmask", 2, VERB_ANY, 0, verb_enable },
1204 { "link", 2, VERB_ANY, 0, verb_enable },
1205 { "revert", 2, VERB_ANY, 0, verb_enable },
5ae89ef3 1206 { "switch-root", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, verb_switch_root },
32baf64d
LP
1207 { "list-dependencies", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, verb_list_dependencies },
1208 { "set-default", 2, 2, 0, verb_set_default },
1209 { "get-default", VERB_ANY, 1, 0, verb_get_default },
1210 { "set-property", 3, VERB_ANY, VERB_ONLINE_ONLY, verb_set_property },
1211 { "is-system-running", VERB_ANY, 1, 0, verb_is_system_running },
1212 { "add-wants", 3, VERB_ANY, 0, verb_add_dependency },
1213 { "add-requires", 3, VERB_ANY, 0, verb_add_dependency },
1214 { "edit", 2, VERB_ANY, VERB_ONLINE_ONLY, verb_edit },
1215 { "bind", 3, 4, VERB_ONLINE_ONLY, verb_bind },
1216 { "mount-image", 4, 5, VERB_ONLINE_ONLY, verb_mount_image },
c86a9508 1217 { "whoami", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, verb_whoami },
d08e75ed 1218 {}
e449de87 1219 };
7e4249b9 1220
4cb1015e
ZJS
1221 const Verb *verb = verbs_find_verb(argv[optind], verbs);
1222 if (verb && (verb->flags & VERB_ONLINE_ONLY) && arg_root)
1223 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
8aa3894e 1224 "Verb '%s' cannot be used with --root= or --image=.",
4cb1015e
ZJS
1225 argv[optind] ?: verb->verb);
1226
e449de87 1227 return dispatch_verb(argc, argv, verbs, NULL);
e4b61340
LP
1228}
1229
f2a3de01 1230static int run(int argc, char *argv[]) {
8aa3894e 1231 _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
a4b3e942 1232 _cleanup_(umount_and_freep) char *mounted_dir = NULL;
f459b602 1233 int r;
e4b61340 1234
a9cdc94f 1235 setlocale(LC_ALL, "");
158fdd53 1236 log_setup();
1abaf488
LP
1237
1238 /* The journal merging logic potentially needs a lot of fds. */
1239 (void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE);
1240
592705f2 1241 sigbus_install();
e4b61340 1242
5fd87823 1243 r = systemctl_dispatch_parse_argv(argc, argv);
f459b602 1244 if (r <= 0)
e4b61340 1245 goto finish;
7e4249b9 1246
0f958c8d 1247 if (proc_mounted() == 0)
91dfb74e
YW
1248 log_full(arg_no_warn ? LOG_DEBUG : LOG_WARNING,
1249 "%s%s/proc/ is not mounted. This is not a supported mode of operation. Please fix\n"
1250 "your invocation environment to mount /proc/ and /sys/ properly. Proceeding anyway.\n"
1251 "Your mileage may vary.",
1252 emoji_enabled() ? special_glyph(SPECIAL_GLYPH_WARNING_SIGN) : "",
1253 emoji_enabled() ? " " : "");
0f958c8d 1254
040524b4 1255 if (arg_action != ACTION_SYSTEMCTL && running_in_chroot() > 0) {
60675884
LP
1256 if (!arg_quiet)
1257 log_info("Running in chroot, ignoring request.");
f459b602 1258 r = 0;
82e23ddd
LP
1259 goto finish;
1260 }
1261
daf71ef6 1262 /* systemctl_main() will print an error message for the bus connection, but only if it needs to */
e4b61340 1263
8aa3894e
RP
1264 if (arg_image) {
1265 assert(!arg_root);
1266
1267 r = mount_image_privately_interactively(
1268 arg_image,
84be0c71 1269 arg_image_policy,
8aa3894e
RP
1270 DISSECT_IMAGE_GENERIC_ROOT |
1271 DISSECT_IMAGE_REQUIRE_ROOT |
1272 DISSECT_IMAGE_RELAX_VAR_CHECK |
f4a63ce2
LP
1273 DISSECT_IMAGE_VALIDATE_OS |
1274 DISSECT_IMAGE_ALLOW_USERSPACE_VERITY,
8aa3894e 1275 &mounted_dir,
a133d2c3 1276 /* ret_dir_fd= */ NULL,
8aa3894e
RP
1277 &loop_device);
1278 if (r < 0)
1279 return r;
1280
1281 arg_root = strdup(mounted_dir);
1282 if (!arg_root)
1283 return log_oom();
1284 }
1285
e4b61340
LP
1286 switch (arg_action) {
1287
22f4096c 1288 case ACTION_SYSTEMCTL:
e449de87 1289 r = systemctl_main(argc, argv);
e4b61340 1290 break;
e4b61340 1291
daf71ef6
LP
1292 /* Legacy command aliases set arg_action. They provide some fallbacks, e.g. to tell sysvinit to
1293 * reboot after you have installed systemd binaries. */
081dc638 1294
e4b61340
LP
1295 case ACTION_HALT:
1296 case ACTION_POWEROFF:
1297 case ACTION_REBOOT:
5622dde3 1298 case ACTION_KEXEC:
4fbd7192 1299 r = halt_main();
e4b61340
LP
1300 break;
1301
e4b61340
LP
1302 case ACTION_RUNLEVEL2:
1303 case ACTION_RUNLEVEL3:
1304 case ACTION_RUNLEVEL4:
1305 case ACTION_RUNLEVEL5:
1306 case ACTION_RESCUE:
4fbd7192 1307 r = start_with_fallback();
e4b61340 1308 break;
7e4249b9 1309
e4b61340
LP
1310 case ACTION_RELOAD:
1311 case ACTION_REEXEC:
4fbd7192 1312 r = reload_with_fallback();
e4b61340
LP
1313 break;
1314
949d9ce9 1315 case ACTION_CANCEL_SHUTDOWN:
2cf05793 1316 r = logind_cancel_shutdown();
f6144808
LP
1317 break;
1318
a9c3cc8d
LN
1319 case ACTION_SHOW_SHUTDOWN:
1320 r = logind_show_shutdown();
1321 break;
1322
eb22ac37 1323 case ACTION_RUNLEVEL:
4f16c1f4
LP
1324 r = runlevel_main();
1325 break;
1326
081dc638 1327 case ACTION_EXIT:
5b356289 1328 case ACTION_SLEEP:
081dc638
AJ
1329 case ACTION_SUSPEND:
1330 case ACTION_HIBERNATE:
1331 case ACTION_HYBRID_SLEEP:
e68c79db 1332 case ACTION_SUSPEND_THEN_HIBERNATE:
081dc638
AJ
1333 case ACTION_EMERGENCY:
1334 case ACTION_DEFAULT:
36556f6e
ZJS
1335 /* systemctl verbs with no equivalent in the legacy commands. These cannot appear in
1336 * arg_action. Fall through. */
081dc638 1337
f459b602 1338 case _ACTION_INVALID:
e4b61340 1339 default:
04499a70 1340 assert_not_reached();
e4b61340 1341 }
7e4249b9
LP
1342
1343finish:
cf647b69
LP
1344 release_busses();
1345
d1405af3
ZJS
1346 /* Note that we return r here, not 0, so that we can implement the LSB-like return codes */
1347 return r;
7e4249b9 1348}
d1405af3
ZJS
1349
1350DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);
5fd87823 1351#endif