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