]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/systemctl/systemctl.c
systemctl: inline iterator declarations
[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
LP
8
9#include "bus-util.h"
03a7b521 10#include "install.h"
d1405af3 11#include "main-func.h"
daf71ef6 12#include "output-mode.h"
03a7b521 13#include "pager.h"
03a7b521 14#include "path-util.h"
294bf0c3 15#include "pretty-print.h"
78f22b97 16#include "rlimit-util.h"
592705f2 17#include "sigbus.h"
24882e06 18#include "signal-util.h"
8559e61d 19#include "string-table.h"
daf71ef6
LP
20#include "systemctl-add-dependency.h"
21#include "systemctl-cancel-job.h"
22#include "systemctl-clean-or-freeze.h"
23#include "systemctl-compat-halt.h"
24#include "systemctl-compat-runlevel.h"
25#include "systemctl-compat-shutdown.h"
26#include "systemctl-compat-telinit.h"
27#include "systemctl-daemon-reload.h"
28#include "systemctl-edit.h"
29#include "systemctl-enable.h"
30#include "systemctl-is-active.h"
31#include "systemctl-is-enabled.h"
32#include "systemctl-is-system-running.h"
33#include "systemctl-kill.h"
34#include "systemctl-list-dependencies.h"
35#include "systemctl-list-jobs.h"
36#include "systemctl-list-machines.h"
37#include "systemctl-list-unit-files.h"
38#include "systemctl-list-units.h"
39#include "systemctl-log-setting.h"
40#include "systemctl-logind.h"
5e8deb94 41#include "systemctl-mount.h"
daf71ef6
LP
42#include "systemctl-preset-all.h"
43#include "systemctl-reset-failed.h"
44#include "systemctl-service-watchdogs.h"
45#include "systemctl-set-default.h"
46#include "systemctl-set-environment.h"
47#include "systemctl-set-property.h"
48#include "systemctl-show.h"
49#include "systemctl-start-special.h"
50#include "systemctl-start-unit.h"
51#include "systemctl-switch-root.h"
52#include "systemctl-sysv-compat.h"
53#include "systemctl-trivial-method.h"
54#include "systemctl-util.h"
55#include "systemctl.h"
03a7b521 56#include "terminal-util.h"
daf71ef6 57#include "time-util.h"
e449de87 58#include "verbs.h"
7f4b3c5e 59#include "virt.h"
7e4249b9 60
daf71ef6
LP
61char **arg_types = NULL;
62char **arg_states = NULL;
63char **arg_properties = NULL;
64bool arg_all = false;
65enum dependency arg_dependency = DEPENDENCY_FORWARD;
66const char *arg_job_mode = "replace";
67UnitFileScope arg_scope = UNIT_FILE_SYSTEM;
68bool arg_wait = false;
69bool arg_no_block = false;
70bool arg_no_legend = false;
71PagerFlags arg_pager_flags = 0;
72bool arg_no_wtmp = false;
73bool arg_no_sync = false;
74bool arg_no_wall = false;
75bool arg_no_reload = false;
76bool arg_value = false;
77bool arg_show_types = false;
4327574f 78int arg_check_inhibitors = -1;
daf71ef6
LP
79bool arg_dry_run = false;
80bool arg_quiet = false;
81bool arg_full = false;
82bool arg_recursive = false;
83bool arg_with_dependencies = false;
84bool arg_show_transaction = false;
85int arg_force = 0;
86bool arg_ask_password = false;
87bool arg_runtime = false;
88UnitFilePresetMode arg_preset_mode = UNIT_FILE_PRESET_FULL;
89char **arg_wall = NULL;
90const char *arg_kill_who = NULL;
91int arg_signal = SIGTERM;
92char *arg_root = NULL;
93usec_t arg_when = 0;
94const char *arg_reboot_argument = NULL;
95enum action arg_action = ACTION_SYSTEMCTL;
96BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
97const char *arg_host = NULL;
98unsigned arg_lines = 10;
99OutputMode arg_output = OUTPUT_SHORT;
100bool arg_plain = false;
101bool arg_firmware_setup = false;
102usec_t arg_boot_loader_menu = USEC_INFINITY;
103const char *arg_boot_loader_entry = NULL;
104bool arg_now = false;
105bool arg_jobs_before = false;
106bool arg_jobs_after = false;
107char **arg_clean_what = NULL;
108TimestampStyle arg_timestamp_style = TIMESTAMP_PRETTY;
5e8deb94
LB
109bool arg_read_only = false;
110bool arg_mkdir = false;
e8630e69 111
61ff45db
LP
112STATIC_DESTRUCTOR_REGISTER(arg_wall, strv_freep);
113STATIC_DESTRUCTOR_REGISTER(arg_root, freep);
114STATIC_DESTRUCTOR_REGISTER(arg_types, strv_freep);
115STATIC_DESTRUCTOR_REGISTER(arg_states, strv_freep);
116STATIC_DESTRUCTOR_REGISTER(arg_properties, strv_freep);
d81bc51f 117STATIC_DESTRUCTOR_REGISTER(arg_clean_what, strv_freep);
4fbd7192 118
daf71ef6
LP
119static int systemctl_help(void) {
120 _cleanup_free_ char *link = NULL;
4fbd7192
LP
121 int r;
122
daf71ef6 123 (void) pager_open(arg_pager_flags);
4fbd7192 124
daf71ef6
LP
125 r = terminal_urlify_man("systemctl", "1", &link);
126 if (r < 0)
127 return log_oom();
4fbd7192 128
daf71ef6
LP
129 printf("%1$s [OPTIONS...] COMMAND ...\n\n"
130 "%5$sQuery or send control commands to the system manager.%6$s\n"
131 "\n%3$sUnit Commands:%4$s\n"
132 " list-units [PATTERN...] List units currently in memory\n"
133 " list-sockets [PATTERN...] List socket units currently in memory,\n"
134 " ordered by address\n"
135 " list-timers [PATTERN...] List timer units currently in memory,\n"
136 " ordered by next elapse\n"
137 " is-active PATTERN... Check whether units are active\n"
138 " is-failed PATTERN... Check whether units are failed\n"
139 " status [PATTERN...|PID...] Show runtime status of one or more units\n"
140 " show [PATTERN...|JOB...] Show properties of one or more\n"
141 " units/jobs or the manager\n"
142 " cat PATTERN... Show files and drop-ins of specified units\n"
143 " help PATTERN...|PID... Show manual for one or more units\n"
144 " list-dependencies [UNIT...] Recursively show units which are required\n"
145 " or wanted by the units or by which those\n"
146 " units are required or wanted\n"
147 " start UNIT... Start (activate) one or more units\n"
148 " stop UNIT... Stop (deactivate) one or more units\n"
149 " reload UNIT... Reload one or more units\n"
150 " restart UNIT... Start or restart one or more units\n"
151 " try-restart UNIT... Restart one or more units if active\n"
152 " reload-or-restart UNIT... Reload one or more units if possible,\n"
153 " otherwise start or restart\n"
154 " try-reload-or-restart UNIT... If active, reload one or more units,\n"
155 " if supported, otherwise restart\n"
156 " isolate UNIT Start one unit and stop all others\n"
157 " kill UNIT... Send signal to processes of a unit\n"
158 " clean UNIT... Clean runtime, cache, state, logs or\n"
159 " configuration of unit\n"
160 " freeze PATTERN... Freeze execution of unit processes\n"
161 " thaw PATTERN... Resume execution of a frozen unit\n"
162 " set-property UNIT PROPERTY=VALUE... Sets one or more properties of a unit\n"
5e8deb94
LB
163 " bind UNIT PATH [PATH] Bind-mount a path from the host into a\n"
164 " unit's namespace\n"
6faecbd3
LB
165 " mount-image UNIT PATH [PATH [OPTS]] Mount an image from the host into a\n"
166 " unit's namespace\n"
daf71ef6
LP
167 " service-log-level SERVICE [LEVEL] Get/set logging threshold for service\n"
168 " service-log-target SERVICE [TARGET] Get/set logging target for service\n"
169 " reset-failed [PATTERN...] Reset failed state for all, one, or more\n"
170 " units"
171 "\n%3$sUnit File Commands:%4$s\n"
172 " list-unit-files [PATTERN...] List installed unit files\n"
173 " enable [UNIT...|PATH...] Enable one or more unit files\n"
174 " disable UNIT... Disable one or more unit files\n"
175 " reenable UNIT... Reenable one or more unit files\n"
176 " preset UNIT... Enable/disable one or more unit files\n"
177 " based on preset configuration\n"
178 " preset-all Enable/disable all unit files based on\n"
179 " preset configuration\n"
180 " is-enabled UNIT... Check whether unit files are enabled\n"
181 " mask UNIT... Mask one or more units\n"
182 " unmask UNIT... Unmask one or more units\n"
183 " link PATH... Link one or more units files into\n"
184 " the search path\n"
185 " revert UNIT... Revert one or more unit files to vendor\n"
186 " version\n"
187 " add-wants TARGET UNIT... Add 'Wants' dependency for the target\n"
188 " on specified one or more units\n"
189 " add-requires TARGET UNIT... Add 'Requires' dependency for the target\n"
190 " on specified one or more units\n"
191 " edit UNIT... Edit one or more unit files\n"
192 " get-default Get the name of the default target\n"
193 " set-default TARGET Set the default target\n"
194 "\n%3$sMachine Commands:%4$s\n"
195 " list-machines [PATTERN...] List local containers and host\n"
196 "\n%3$sJob Commands:%4$s\n"
197 " list-jobs [PATTERN...] List jobs\n"
198 " cancel [JOB...] Cancel all, one, or more jobs\n"
199 "\n%3$sEnvironment Commands:%4$s\n"
200 " show-environment Dump environment\n"
201 " set-environment VARIABLE=VALUE... Set one or more environment variables\n"
202 " unset-environment VARIABLE... Unset one or more environment variables\n"
32854f70 203 " import-environment VARIABLE... Import all or some environment variables\n"
daf71ef6
LP
204 "\n%3$sManager State Commands:%4$s\n"
205 " daemon-reload Reload systemd manager configuration\n"
206 " daemon-reexec Reexecute systemd manager\n"
207 " log-level [LEVEL] Get/set logging threshold for manager\n"
208 " log-target [TARGET] Get/set logging target for manager\n"
209 " service-watchdogs [BOOL] Get/set service watchdog state\n"
210 "\n%3$sSystem Commands:%4$s\n"
211 " is-system-running Check whether system is fully running\n"
212 " default Enter system default mode\n"
213 " rescue Enter system rescue mode\n"
214 " emergency Enter system emergency mode\n"
215 " halt Shut down and halt the system\n"
216 " poweroff Shut down and power-off the system\n"
217 " reboot Shut down and reboot the system\n"
218 " kexec Shut down and reboot the system with kexec\n"
219 " exit [EXIT_CODE] Request user instance or container exit\n"
220 " switch-root ROOT [INIT] Change to a different root file system\n"
221 " suspend Suspend the system\n"
222 " hibernate Hibernate the system\n"
223 " hybrid-sleep Hibernate and suspend the system\n"
224 " suspend-then-hibernate Suspend the system, wake after a period of\n"
225 " time, and hibernate"
226 "\n%3$sOptions:%4$s\n"
227 " -h --help Show this help\n"
228 " --version Show package version\n"
229 " --system Connect to system manager\n"
230 " --user Connect to user service manager\n"
231 " -H --host=[USER@]HOST Operate on remote host\n"
232 " -M --machine=CONTAINER Operate on a local container\n"
233 " -t --type=TYPE List units of a particular type\n"
234 " --state=STATE List units with particular LOAD or SUB or ACTIVE state\n"
235 " --failed Shortcut for --state=failed\n"
236 " -p --property=NAME Show only properties by this name\n"
237 " -P NAME Equivalent to --value --property=NAME\n"
238 " -a --all Show all properties/all units currently in memory,\n"
239 " including dead/empty ones. To list all units installed\n"
240 " on the system, use 'list-unit-files' instead.\n"
241 " -l --full Don't ellipsize unit names on output\n"
242 " -r --recursive Show unit list of host and local containers\n"
243 " --reverse Show reverse dependencies with 'list-dependencies'\n"
244 " --with-dependencies Show unit dependencies with 'status', 'cat',\n"
245 " 'list-units', and 'list-unit-files'.\n"
246 " --job-mode=MODE Specify how to deal with already queued jobs, when\n"
247 " queueing a new job\n"
248 " -T --show-transaction When enqueuing a unit job, show full transaction\n"
249 " --show-types When showing sockets, explicitly show their type\n"
250 " --value When showing properties, only print the value\n"
4327574f
FS
251 " --check-inhibitors=MODE\n"
252 " Specify if checking inhibitors before shutting down,\n"
253 " sleeping or hibernating\n"
254 " -i Shortcut for --check-inhibitors=no\n"
daf71ef6
LP
255 " --kill-who=WHO Whom to send signal to\n"
256 " -s --signal=SIGNAL Which signal to send\n"
257 " --what=RESOURCES Which types of resources to remove\n"
258 " --now Start or stop unit after enabling or disabling it\n"
259 " --dry-run Only print what would be done\n"
260 " Currently supported by verbs: halt, poweroff, reboot,\n"
261 " kexec, suspend, hibernate, suspend-then-hibernate,\n"
262 " hybrid-sleep, default, rescue, emergency, and exit.\n"
263 " -q --quiet Suppress output\n"
264 " --wait For (re)start, wait until service stopped again\n"
265 " For is-system-running, wait until startup is completed\n"
266 " --no-block Do not wait until operation finished\n"
267 " --no-wall Don't send wall message before halt/power-off/reboot\n"
268 " --no-reload Don't reload daemon after en-/dis-abling unit files\n"
269 " --no-legend Do not print a legend (column headers and hints)\n"
270 " --no-pager Do not pipe output into a pager\n"
271 " --no-ask-password Do not ask for system passwords\n"
272 " --global Enable/disable/mask unit files globally\n"
273 " --runtime Enable/disable/mask unit files temporarily until next\n"
274 " reboot\n"
275 " -f --force When enabling unit files, override existing symlinks\n"
276 " When shutting down, execute action immediately\n"
277 " --preset-mode= Apply only enable, only disable, or all presets\n"
278 " --root=PATH Enable/disable/mask unit files in the specified root\n"
279 " directory\n"
280 " -n --lines=INTEGER Number of journal entries to show\n"
281 " -o --output=STRING Change journal output mode (short, short-precise,\n"
282 " short-iso, short-iso-precise, short-full,\n"
283 " short-monotonic, short-unix,\n"
284 " verbose, export, json, json-pretty, json-sse, cat)\n"
285 " --firmware-setup Tell the firmware to show the setup menu on next boot\n"
286 " --boot-loader-menu=TIME\n"
287 " Boot into boot loader menu on next boot\n"
288 " --boot-loader-entry=NAME\n"
289 " Boot into a specific boot loader entry on next boot\n"
290 " --plain Print unit dependencies as a list instead of a tree\n"
291 " --timestamp=FORMAT Change format of printed timestamps.\n"
292 " 'pretty' (default): 'Day YYYY-MM-DD HH:MM:SS TZ\n"
293 " 'us': 'Day YYYY-MM-DD HH:MM:SS.UUUUUU TZ\n"
294 " 'utc': 'Day YYYY-MM-DD HH:MM:SS UTC\n"
295 " 'us+utc': 'Day YYYY-MM-DD HH:MM:SS.UUUUUU UTC\n"
5e8deb94 296 " --read-only Create read-only bind mount\n"
6faecbd3 297 " --mkdir Create directory before mounting, if missing\n"
bc556335
DDM
298 "\nSee the %2$s for details.\n",
299 program_invocation_short_name,
300 link,
301 ansi_underline(),
302 ansi_normal(),
303 ansi_highlight(),
304 ansi_normal());
4fbd7192 305
4fbd7192
LP
306 return 0;
307}
308
daf71ef6
LP
309static void help_types(void) {
310 if (!arg_no_legend)
311 puts("Available unit types:");
501fc174 312
daf71ef6 313 DUMP_STRING_TABLE(unit_type, UnitType, _UNIT_TYPE_MAX);
3c756001
LP
314}
315
daf71ef6
LP
316static void help_states(void) {
317 if (!arg_no_legend)
318 puts("Available unit load states:");
319 DUMP_STRING_TABLE(unit_load_state, UnitLoadState, _UNIT_LOAD_STATE_MAX);
22f4096c 320
daf71ef6
LP
321 if (!arg_no_legend)
322 puts("\nAvailable unit active states:");
323 DUMP_STRING_TABLE(unit_active_state, UnitActiveState, _UNIT_ACTIVE_STATE_MAX);
22f4096c 324
daf71ef6
LP
325 if (!arg_no_legend)
326 puts("\nAvailable unit file states:");
327 DUMP_STRING_TABLE(unit_file_state, UnitFileState, _UNIT_FILE_STATE_MAX);
22f4096c 328
daf71ef6
LP
329 if (!arg_no_legend)
330 puts("\nAvailable automount unit substates:");
331 DUMP_STRING_TABLE(automount_state, AutomountState, _AUTOMOUNT_STATE_MAX);
22f4096c 332
daf71ef6
LP
333 if (!arg_no_legend)
334 puts("\nAvailable device unit substates:");
335 DUMP_STRING_TABLE(device_state, DeviceState, _DEVICE_STATE_MAX);
4f16c1f4 336
daf71ef6
LP
337 if (!arg_no_legend)
338 puts("\nAvailable mount unit substates:");
339 DUMP_STRING_TABLE(mount_state, MountState, _MOUNT_STATE_MAX);
729e3769 340
daf71ef6
LP
341 if (!arg_no_legend)
342 puts("\nAvailable path unit substates:");
343 DUMP_STRING_TABLE(path_state, PathState, _PATH_STATE_MAX);
729e3769 344
daf71ef6
LP
345 if (!arg_no_legend)
346 puts("\nAvailable scope unit substates:");
347 DUMP_STRING_TABLE(scope_state, ScopeState, _SCOPE_STATE_MAX);
729e3769 348
daf71ef6
LP
349 if (!arg_no_legend)
350 puts("\nAvailable service unit substates:");
351 DUMP_STRING_TABLE(service_state, ServiceState, _SERVICE_STATE_MAX);
729e3769 352
daf71ef6
LP
353 if (!arg_no_legend)
354 puts("\nAvailable slice unit substates:");
355 DUMP_STRING_TABLE(slice_state, SliceState, _SLICE_STATE_MAX);
b41b9d2a 356
daf71ef6
LP
357 if (!arg_no_legend)
358 puts("\nAvailable socket unit substates:");
359 DUMP_STRING_TABLE(socket_state, SocketState, _SOCKET_STATE_MAX);
729e3769 360
daf71ef6
LP
361 if (!arg_no_legend)
362 puts("\nAvailable swap unit substates:");
363 DUMP_STRING_TABLE(swap_state, SwapState, _SWAP_STATE_MAX);
16484a8a 364
daf71ef6
LP
365 if (!arg_no_legend)
366 puts("\nAvailable target unit substates:");
367 DUMP_STRING_TABLE(target_state, TargetState, _TARGET_STATE_MAX);
16484a8a 368
daf71ef6
LP
369 if (!arg_no_legend)
370 puts("\nAvailable timer unit substates:");
371 DUMP_STRING_TABLE(timer_state, TimerState, _TIMER_STATE_MAX);
16484a8a
ZJS
372}
373
daf71ef6
LP
374static int systemctl_parse_argv(int argc, char *argv[]) {
375 enum {
376 ARG_FAIL = 0x100,
377 ARG_REVERSE,
378 ARG_AFTER,
379 ARG_BEFORE,
4327574f 380 ARG_CHECK_INHIBITORS,
daf71ef6
LP
381 ARG_DRY_RUN,
382 ARG_SHOW_TYPES,
383 ARG_IRREVERSIBLE,
384 ARG_IGNORE_DEPENDENCIES,
385 ARG_VALUE,
386 ARG_VERSION,
387 ARG_USER,
388 ARG_SYSTEM,
389 ARG_GLOBAL,
390 ARG_NO_BLOCK,
391 ARG_NO_LEGEND,
392 ARG_NO_PAGER,
393 ARG_NO_WALL,
394 ARG_ROOT,
395 ARG_NO_RELOAD,
396 ARG_KILL_WHO,
397 ARG_NO_ASK_PASSWORD,
398 ARG_FAILED,
399 ARG_RUNTIME,
400 ARG_PLAIN,
401 ARG_STATE,
402 ARG_JOB_MODE,
403 ARG_PRESET_MODE,
404 ARG_FIRMWARE_SETUP,
405 ARG_BOOT_LOADER_MENU,
406 ARG_BOOT_LOADER_ENTRY,
407 ARG_NOW,
408 ARG_MESSAGE,
409 ARG_WITH_DEPENDENCIES,
410 ARG_WAIT,
411 ARG_WHAT,
412 ARG_REBOOT_ARG,
413 ARG_TIMESTAMP_STYLE,
5e8deb94
LB
414 ARG_READ_ONLY,
415 ARG_MKDIR,
daf71ef6 416 };
1238ee09 417
daf71ef6
LP
418 static const struct option options[] = {
419 { "help", no_argument, NULL, 'h' },
420 { "version", no_argument, NULL, ARG_VERSION },
421 { "type", required_argument, NULL, 't' },
422 { "property", required_argument, NULL, 'p' },
423 { "all", no_argument, NULL, 'a' },
424 { "reverse", no_argument, NULL, ARG_REVERSE },
425 { "after", no_argument, NULL, ARG_AFTER },
426 { "before", no_argument, NULL, ARG_BEFORE },
427 { "show-types", no_argument, NULL, ARG_SHOW_TYPES },
40a49f12 428 { "failed", no_argument, NULL, ARG_FAILED },
daf71ef6
LP
429 { "full", no_argument, NULL, 'l' },
430 { "job-mode", required_argument, NULL, ARG_JOB_MODE },
431 { "fail", no_argument, NULL, ARG_FAIL }, /* compatibility only */
432 { "irreversible", no_argument, NULL, ARG_IRREVERSIBLE }, /* compatibility only */
433 { "ignore-dependencies", no_argument, NULL, ARG_IGNORE_DEPENDENCIES }, /* compatibility only */
4327574f
FS
434 { "ignore-inhibitors", no_argument, NULL, 'i' }, /* compatibility only */
435 { "check-inhibitors", required_argument, NULL, ARG_CHECK_INHIBITORS },
daf71ef6
LP
436 { "value", no_argument, NULL, ARG_VALUE },
437 { "user", no_argument, NULL, ARG_USER },
438 { "system", no_argument, NULL, ARG_SYSTEM },
439 { "global", no_argument, NULL, ARG_GLOBAL },
440 { "wait", no_argument, NULL, ARG_WAIT },
441 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
442 { "no-legend", no_argument, NULL, ARG_NO_LEGEND },
443 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
444 { "no-wall", no_argument, NULL, ARG_NO_WALL },
445 { "dry-run", no_argument, NULL, ARG_DRY_RUN },
446 { "quiet", no_argument, NULL, 'q' },
447 { "root", required_argument, NULL, ARG_ROOT },
448 { "force", no_argument, NULL, 'f' },
449 { "no-reload", no_argument, NULL, ARG_NO_RELOAD },
450 { "kill-who", required_argument, NULL, ARG_KILL_WHO },
451 { "signal", required_argument, NULL, 's' },
452 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
453 { "host", required_argument, NULL, 'H' },
454 { "machine", required_argument, NULL, 'M' },
455 { "runtime", no_argument, NULL, ARG_RUNTIME },
456 { "lines", required_argument, NULL, 'n' },
457 { "output", required_argument, NULL, 'o' },
458 { "plain", no_argument, NULL, ARG_PLAIN },
459 { "state", required_argument, NULL, ARG_STATE },
460 { "recursive", no_argument, NULL, 'r' },
461 { "with-dependencies", no_argument, NULL, ARG_WITH_DEPENDENCIES },
462 { "preset-mode", required_argument, NULL, ARG_PRESET_MODE },
463 { "firmware-setup", no_argument, NULL, ARG_FIRMWARE_SETUP },
464 { "boot-loader-menu", required_argument, NULL, ARG_BOOT_LOADER_MENU },
465 { "boot-loader-entry", required_argument, NULL, ARG_BOOT_LOADER_ENTRY },
466 { "now", no_argument, NULL, ARG_NOW },
467 { "message", required_argument, NULL, ARG_MESSAGE },
468 { "show-transaction", no_argument, NULL, 'T' },
469 { "what", required_argument, NULL, ARG_WHAT },
470 { "reboot-argument", required_argument, NULL, ARG_REBOOT_ARG },
471 { "timestamp", required_argument, NULL, ARG_TIMESTAMP_STYLE },
5e8deb94
LB
472 { "read-only", no_argument, NULL, ARG_READ_ONLY },
473 { "mkdir", no_argument, NULL, ARG_MKDIR },
daf71ef6
LP
474 {}
475 };
1238ee09 476
daf71ef6 477 int c, r;
d309c1c3 478
daf71ef6
LP
479 assert(argc >= 0);
480 assert(argv);
d309c1c3 481
daf71ef6
LP
482 /* We default to allowing interactive authorization only in systemctl (not in the legacy commands) */
483 arg_ask_password = true;
d309c1c3 484
daf71ef6 485 while ((c = getopt_long(argc, argv, "ht:p:P:alqfs:H:M:n:o:iTr.::", options, NULL)) >= 0)
57ab2eab 486
daf71ef6 487 switch (c) {
9ef15026 488
daf71ef6
LP
489 case 'h':
490 return systemctl_help();
85d9b598 491
daf71ef6
LP
492 case ARG_VERSION:
493 return version();
e9c387c8 494
76e5e267 495 case 't':
d81bc51f 496 if (isempty(optarg))
daf71ef6
LP
497 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
498 "--type= requires arguments.");
d81bc51f 499
76e5e267 500 for (const char *p = optarg;;) {
daf71ef6 501 _cleanup_free_ char *type = NULL;
d81bc51f 502
daf71ef6 503 r = extract_first_word(&p, &type, ",", 0);
d81bc51f 504 if (r < 0)
daf71ef6 505 return log_error_errno(r, "Failed to parse type: %s", optarg);
d81bc51f
LP
506 if (r == 0)
507 break;
508
daf71ef6
LP
509 if (streq(type, "help")) {
510 help_types();
d81bc51f
LP
511 return 0;
512 }
513
daf71ef6
LP
514 if (unit_type_from_string(type) >= 0) {
515 if (strv_consume(&arg_types, TAKE_PTR(type)) < 0)
516 return log_oom();
517 continue;
518 }
519
520 /* It's much nicer to use --state= for load states, but let's support this in
521 * --types= too for compatibility with old versions */
522 if (unit_load_state_from_string(type) >= 0) {
523 if (strv_consume(&arg_states, TAKE_PTR(type)) < 0)
524 return log_oom();
525 continue;
526 }
527
528 log_error("Unknown unit type or load state '%s'.", type);
529 return log_info_errno(SYNTHETIC_ERRNO(EINVAL),
530 "Use -t help to see a list of allowed values.");
d81bc51f
LP
531 }
532
533 break;
d81bc51f 534
daf71ef6
LP
535 case 'P':
536 arg_value = true;
537 _fallthrough_;
dae710be 538
daf71ef6
LP
539 case 'p':
540 /* Make sure that if the empty property list was specified, we won't show any
541 properties. */
542 if (isempty(optarg) && !arg_properties) {
543 arg_properties = new0(char*, 1);
544 if (!arg_properties)
545 return log_oom();
76e5e267
ZJS
546 } else
547 for (const char *p = optarg;;) {
daf71ef6 548 _cleanup_free_ char *prop = NULL;
46ad9c53 549
daf71ef6
LP
550 r = extract_first_word(&p, &prop, ",", 0);
551 if (r < 0)
552 return log_error_errno(r, "Failed to parse property: %s", optarg);
553 if (r == 0)
554 break;
46ad9c53 555
daf71ef6
LP
556 if (strv_consume(&arg_properties, TAKE_PTR(prop)) < 0)
557 return log_oom();
558 }
2e7e19ca 559
daf71ef6
LP
560 /* If the user asked for a particular property, show it, even if it is empty. */
561 arg_all = true;
7e4249b9 562
daf71ef6 563 break;
7e4249b9 564
daf71ef6
LP
565 case 'a':
566 arg_all = true;
567 break;
a8f11321 568
daf71ef6
LP
569 case ARG_REVERSE:
570 arg_dependency = DEPENDENCY_REVERSE;
571 break;
93a08841 572
daf71ef6
LP
573 case ARG_AFTER:
574 arg_dependency = DEPENDENCY_AFTER;
575 arg_jobs_after = true;
576 break;
7e4249b9 577
daf71ef6
LP
578 case ARG_BEFORE:
579 arg_dependency = DEPENDENCY_BEFORE;
580 arg_jobs_before = true;
581 break;
e4b61340 582
daf71ef6
LP
583 case ARG_SHOW_TYPES:
584 arg_show_types = true;
585 break;
e4b61340 586
daf71ef6
LP
587 case ARG_VALUE:
588 arg_value = true;
589 break;
e4b61340 590
daf71ef6
LP
591 case ARG_JOB_MODE:
592 arg_job_mode = optarg;
593 break;
e4b61340 594
daf71ef6
LP
595 case ARG_FAIL:
596 arg_job_mode = "fail";
597 break;
e4b61340 598
daf71ef6
LP
599 case ARG_IRREVERSIBLE:
600 arg_job_mode = "replace-irreversibly";
601 break;
e4b61340 602
daf71ef6
LP
603 case ARG_IGNORE_DEPENDENCIES:
604 arg_job_mode = "ignore-dependencies";
605 break;
e4b61340 606
daf71ef6
LP
607 case ARG_USER:
608 arg_scope = UNIT_FILE_USER;
e4b61340
LP
609 break;
610
daf71ef6
LP
611 case ARG_SYSTEM:
612 arg_scope = UNIT_FILE_SYSTEM;
e4b61340
LP
613 break;
614
daf71ef6
LP
615 case ARG_GLOBAL:
616 arg_scope = UNIT_FILE_GLOBAL;
e4b61340
LP
617 break;
618
daf71ef6
LP
619 case ARG_WAIT:
620 arg_wait = true;
e4b61340
LP
621 break;
622
daf71ef6
LP
623 case ARG_NO_BLOCK:
624 arg_no_block = true;
e4b61340
LP
625 break;
626
daf71ef6
LP
627 case ARG_NO_LEGEND:
628 arg_no_legend = true;
e4b61340
LP
629 break;
630
daf71ef6
LP
631 case ARG_NO_PAGER:
632 arg_pager_flags |= PAGER_DISABLE;
f3f054f0
TB
633 break;
634
514f4ef5
LP
635 case ARG_NO_WALL:
636 arg_no_wall = true;
637 break;
638
daf71ef6
LP
639 case ARG_ROOT:
640 r = parse_path_argument_and_warn(optarg, false, &arg_root);
641 if (r < 0)
642 return r;
e4b61340
LP
643 break;
644
daf71ef6
LP
645 case 'l':
646 arg_full = true;
647 break;
e4b61340 648
daf71ef6
LP
649 case ARG_FAILED:
650 if (strv_extend(&arg_states, "failed") < 0)
651 return log_oom();
e4b61340 652
daf71ef6 653 break;
e4b61340 654
daf71ef6
LP
655 case ARG_DRY_RUN:
656 arg_dry_run = true;
657 break;
e4b61340 658
daf71ef6
LP
659 case 'q':
660 arg_quiet = true;
661 break;
e4b61340 662
daf71ef6
LP
663 case 'f':
664 arg_force++;
665 break;
e4b61340 666
daf71ef6
LP
667 case ARG_NO_RELOAD:
668 arg_no_reload = true;
669 break;
e4b61340 670
daf71ef6
LP
671 case ARG_KILL_WHO:
672 arg_kill_who = optarg;
673 break;
e4b61340 674
daf71ef6
LP
675 case 's':
676 if (streq(optarg, "help")) {
677 DUMP_STRING_TABLE(signal, int, _NSIG);
678 return 0;
679 }
680
681 arg_signal = signal_from_string(optarg);
682 if (arg_signal < 0)
683 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
684 "Failed to parse signal string %s.",
685 optarg);
686 break;
e4b61340 687
daf71ef6
LP
688 case ARG_NO_ASK_PASSWORD:
689 arg_ask_password = false;
690 break;
e4b61340
LP
691
692 case 'H':
daf71ef6
LP
693 arg_transport = BUS_TRANSPORT_REMOTE;
694 arg_host = optarg;
e4b61340
LP
695 break;
696
daf71ef6
LP
697 case 'M':
698 arg_transport = BUS_TRANSPORT_MACHINE;
699 arg_host = optarg;
e4b61340
LP
700 break;
701
daf71ef6
LP
702 case ARG_RUNTIME:
703 arg_runtime = true;
e4b61340
LP
704 break;
705
daf71ef6
LP
706 case 'n':
707 if (safe_atou(optarg, &arg_lines) < 0)
708 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
709 "Failed to parse lines '%s'",
710 optarg);
04ebb595
LP
711 break;
712
daf71ef6
LP
713 case 'o':
714 if (streq(optarg, "help")) {
715 DUMP_STRING_TABLE(output_mode, OutputMode, _OUTPUT_MODE_MAX);
716 return 0;
717 }
718
719 arg_output = output_mode_from_string(optarg);
720 if (arg_output < 0)
721 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
722 "Unknown output '%s'.",
723 optarg);
724
725 if (OUTPUT_MODE_IS_JSON(arg_output)) {
726 arg_no_legend = true;
727 arg_plain = true;
728 }
e4b61340
LP
729 break;
730
daf71ef6 731 case 'i':
4327574f
FS
732 arg_check_inhibitors = 0;
733 break;
734
735 case ARG_CHECK_INHIBITORS:
736 if (streq(optarg, "auto"))
737 arg_check_inhibitors = -1;
738 else {
739 r = parse_boolean(optarg);
740 if (r < 0)
741 return log_error_errno(r, "Failed to parse --check-inhibitors= argument: %s", optarg);
742 arg_check_inhibitors = r;
743 }
e4b61340
LP
744 break;
745
daf71ef6
LP
746 case ARG_PLAIN:
747 arg_plain = true;
514f4ef5
LP
748 break;
749
daf71ef6
LP
750 case ARG_FIRMWARE_SETUP:
751 arg_firmware_setup = true;
e4b61340
LP
752 break;
753
daf71ef6 754 case ARG_BOOT_LOADER_MENU:
f6144808 755
daf71ef6
LP
756 r = parse_sec(optarg, &arg_boot_loader_menu);
757 if (r < 0)
758 return log_error_errno(r, "Failed to parse --boot-loader-menu= argument '%s': %m", optarg);
e4b61340 759
daf71ef6 760 break;
e4b61340 761
daf71ef6 762 case ARG_BOOT_LOADER_ENTRY:
e4b61340 763
daf71ef6
LP
764 if (streq(optarg, "help")) { /* Yes, this means, "help" is not a valid boot loader entry name we can deal with */
765 r = help_boot_loader_entry();
766 if (r < 0)
767 return r;
e4b61340 768
daf71ef6
LP
769 return 0;
770 }
e4b61340 771
daf71ef6
LP
772 arg_boot_loader_entry = empty_to_null(optarg);
773 break;
e4b61340 774
76e5e267 775 case ARG_STATE:
daf71ef6
LP
776 if (isempty(optarg))
777 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
778 "--state= requires arguments.");
e4b61340 779
76e5e267 780 for (const char *p = optarg;;) {
daf71ef6 781 _cleanup_free_ char *s = NULL;
e4b61340 782
daf71ef6
LP
783 r = extract_first_word(&p, &s, ",", 0);
784 if (r < 0)
785 return log_error_errno(r, "Failed to parse state: %s", optarg);
786 if (r == 0)
787 break;
e4b61340 788
daf71ef6
LP
789 if (streq(s, "help")) {
790 help_states();
791 return 0;
792 }
e4b61340 793
daf71ef6
LP
794 if (strv_consume(&arg_states, TAKE_PTR(s)) < 0)
795 return log_oom();
796 }
797 break;
e4b61340 798
daf71ef6
LP
799 case 'r':
800 if (geteuid() != 0)
801 return log_error_errno(SYNTHETIC_ERRNO(EPERM),
802 "--recursive requires root privileges.");
803
804 arg_recursive = true;
514f4ef5
LP
805 break;
806
daf71ef6
LP
807 case ARG_PRESET_MODE:
808 if (streq(optarg, "help")) {
809 DUMP_STRING_TABLE(unit_file_preset_mode, UnitFilePresetMode, _UNIT_FILE_PRESET_MAX);
810 return 0;
811 }
e4b61340 812
daf71ef6
LP
813 arg_preset_mode = unit_file_preset_mode_from_string(optarg);
814 if (arg_preset_mode < 0)
815 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
816 "Failed to parse preset mode: %s.", optarg);
e4b61340 817
daf71ef6 818 break;
e4b61340 819
daf71ef6
LP
820 case ARG_NOW:
821 arg_now = true;
822 break;
e4b61340 823
daf71ef6
LP
824 case ARG_MESSAGE:
825 if (strv_extend(&arg_wall, optarg) < 0)
826 return log_oom();
827 break;
e4b61340 828
daf71ef6
LP
829 case 'T':
830 arg_show_transaction = true;
e4b61340
LP
831 break;
832
daf71ef6
LP
833 case ARG_WITH_DEPENDENCIES:
834 arg_with_dependencies = true;
835 break;
e4b61340 836
76e5e267 837 case ARG_WHAT:
daf71ef6
LP
838 if (isempty(optarg))
839 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--what= requires arguments.");
e4b61340 840
76e5e267 841 for (const char *p = optarg;;) {
daf71ef6 842 _cleanup_free_ char *k = NULL;
e4b61340 843
daf71ef6
LP
844 r = extract_first_word(&p, &k, ",", 0);
845 if (r < 0)
846 return log_error_errno(r, "Failed to parse directory type: %s", optarg);
847 if (r == 0)
848 break;
e4b61340 849
daf71ef6
LP
850 if (streq(k, "help")) {
851 puts("runtime\n"
852 "state\n"
853 "cache\n"
854 "logs\n"
855 "configuration");
856 return 0;
857 }
e4b61340 858
daf71ef6
LP
859 r = strv_consume(&arg_clean_what, TAKE_PTR(k));
860 if (r < 0)
861 return log_oom();
862 }
e4b61340 863
daf71ef6 864 break;
e4b61340 865
daf71ef6
LP
866 case ARG_REBOOT_ARG:
867 arg_reboot_argument = optarg;
868 break;
869
870 case ARG_TIMESTAMP_STYLE:
871 if (streq(optarg, "help")) {
872 DUMP_STRING_TABLE(timestamp_style, TimestampStyle, _TIMESTAMP_STYLE_MAX);
873 return 0;
874 }
875
876 arg_timestamp_style = timestamp_style_from_string(optarg);
877 if (arg_timestamp_style < 0)
878 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
879 "Invalid value: %s.", optarg);
880
881 break;
e4b61340 882
5e8deb94
LB
883 case ARG_READ_ONLY:
884 arg_read_only = true;
885 break;
886
887 case ARG_MKDIR:
888 arg_mkdir = true;
889 break;
890
daf71ef6
LP
891 case '.':
892 /* Output an error mimicking getopt, and print a hint afterwards */
893 log_error("%s: invalid option -- '.'", program_invocation_name);
894 log_notice("Hint: to specify units starting with a dash, use \"--\":\n"
895 " %s [OPTIONS...] COMMAND -- -.%s ...",
896 program_invocation_name, optarg ?: "mount");
897 _fallthrough_;
e4b61340
LP
898
899 case '?':
900 return -EINVAL;
901
902 default:
eb9da376 903 assert_not_reached("Unhandled option");
e4b61340 904 }
e4b61340 905
1b630835 906 if (arg_transport == BUS_TRANSPORT_REMOTE && arg_scope != UNIT_FILE_SYSTEM)
daf71ef6
LP
907 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
908 "Cannot access user instance remotely.");
909
910 if (arg_wait && arg_no_block)
baaa35ad 911 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
daf71ef6 912 "--wait may not be combined with --no-block.");
e4b61340
LP
913
914 return 1;
915}
916
5fd87823 917int systemctl_dispatch_parse_argv(int argc, char *argv[]) {
e4b61340
LP
918 assert(argc >= 0);
919 assert(argv);
920
921 if (program_invocation_short_name) {
922
923 if (strstr(program_invocation_short_name, "halt")) {
924 arg_action = ACTION_HALT;
925 return halt_parse_argv(argc, argv);
c54819ca 926
e4b61340
LP
927 } else if (strstr(program_invocation_short_name, "poweroff")) {
928 arg_action = ACTION_POWEROFF;
929 return halt_parse_argv(argc, argv);
c54819ca 930
e4b61340 931 } else if (strstr(program_invocation_short_name, "reboot")) {
5622dde3
KS
932 if (kexec_loaded())
933 arg_action = ACTION_KEXEC;
934 else
935 arg_action = ACTION_REBOOT;
e4b61340 936 return halt_parse_argv(argc, argv);
c54819ca 937
e4b61340
LP
938 } else if (strstr(program_invocation_short_name, "shutdown")) {
939 arg_action = ACTION_POWEROFF;
940 return shutdown_parse_argv(argc, argv);
c54819ca 941
e4b61340 942 } else if (strstr(program_invocation_short_name, "init")) {
d5ca5f11 943
daf71ef6
LP
944 /* Matches invocations as "init" as well as "telinit", which are synonymous when run
945 * as PID != 1 on SysV.
c54819ca
LP
946 *
947 * On SysV "telinit" was the official command to communicate with PID 1, but "init" would
948 * redirect itself to "telinit" if called with PID != 1. We follow the same logic here still,
949 * though we add one level of indirection, as we implement "telinit" in "systemctl". Hence, for
950 * us if you invoke "init" you get "systemd", but it will execve() "systemctl" immediately with
951 * argv[] unmodified if PID is != 1. If you invoke "telinit" you directly get "systemctl". In
952 * both cases we shall do the same thing, which is why we do strstr(p_i_s_n, "init") here, as a
953 * quick way to match both.
954 *
955 * Also see redirect_telinit() in src/core/main.c. */
956
d5ca5f11 957 if (sd_booted() > 0) {
f459b602 958 arg_action = _ACTION_INVALID;
d5ca5f11
LP
959 return telinit_parse_argv(argc, argv);
960 } else {
54d04cd1
ZJS
961 /* Hmm, so some other init system is running, we need to forward this request
962 * to it. */
d5ca5f11 963
ea71b460
ZJS
964 arg_action = ACTION_TELINIT;
965 return 1;
d5ca5f11
LP
966 }
967
e4b61340
LP
968 } else if (strstr(program_invocation_short_name, "runlevel")) {
969 arg_action = ACTION_RUNLEVEL;
970 return runlevel_parse_argv(argc, argv);
971 }
972 }
973
974 arg_action = ACTION_SYSTEMCTL;
975 return systemctl_parse_argv(argc, argv);
976}
977
5fd87823 978#ifndef FUZZ_SYSTEMCTL_PARSE_ARGV
e449de87 979static int systemctl_main(int argc, char *argv[]) {
e449de87 980 static const Verb verbs[] = {
c56d1e2c 981 { "list-units", VERB_ANY, VERB_ANY, VERB_DEFAULT|VERB_ONLINE_ONLY, list_units },
38fcb7f7
ZJS
982 { "list-unit-files", VERB_ANY, VERB_ANY, 0, list_unit_files },
983 { "list-sockets", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, list_sockets },
984 { "list-timers", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, list_timers },
985 { "list-jobs", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, list_jobs },
986 { "list-machines", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, list_machines },
987 { "clear-jobs", VERB_ANY, 1, VERB_ONLINE_ONLY, trivial_method },
988 { "cancel", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, cancel_job },
989 { "start", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit },
990 { "stop", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit },
991 { "condstop", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, /* For compatibility with ALTLinux */
992 { "reload", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit },
993 { "restart", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit },
994 { "try-restart", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit },
995 { "reload-or-restart", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit },
996 { "reload-or-try-restart", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, /* For compatibility with old systemctl <= 228 */
997 { "try-reload-or-restart", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit },
998 { "force-reload", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, /* For compatibility with SysV */
999 { "condreload", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, /* For compatibility with ALTLinux */
1000 { "condrestart", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, /* For compatibility with RH */
1001 { "isolate", 2, 2, VERB_ONLINE_ONLY, start_unit },
1002 { "kill", 2, VERB_ANY, VERB_ONLINE_ONLY, kill_unit },
d9e45bc3
MS
1003 { "clean", 2, VERB_ANY, VERB_ONLINE_ONLY, clean_or_freeze_unit },
1004 { "freeze", 2, VERB_ANY, VERB_ONLINE_ONLY, clean_or_freeze_unit },
1005 { "thaw", 2, VERB_ANY, VERB_ONLINE_ONLY, clean_or_freeze_unit },
38fcb7f7
ZJS
1006 { "is-active", 2, VERB_ANY, VERB_ONLINE_ONLY, check_unit_active },
1007 { "check", 2, VERB_ANY, VERB_ONLINE_ONLY, check_unit_active }, /* deprecated alias of is-active */
1008 { "is-failed", 2, VERB_ANY, VERB_ONLINE_ONLY, check_unit_failed },
1009 { "show", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, show },
1010 { "cat", 2, VERB_ANY, VERB_ONLINE_ONLY, cat },
1011 { "status", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, show },
1012 { "help", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, show },
1013 { "daemon-reload", VERB_ANY, 1, VERB_ONLINE_ONLY, daemon_reload },
1014 { "daemon-reexec", VERB_ANY, 1, VERB_ONLINE_ONLY, daemon_reload },
77db9433
ZJS
1015 { "log-level", VERB_ANY, 2, VERB_ONLINE_ONLY, log_setting },
1016 { "log-target", VERB_ANY, 2, VERB_ONLINE_ONLY, log_setting },
6824c132
ZJS
1017 { "service-log-level", 2, 3, VERB_ONLINE_ONLY, service_log_setting },
1018 { "service-log-target", 2, 3, VERB_ONLINE_ONLY, service_log_setting },
ae98f726 1019 { "service-watchdogs", VERB_ANY, 2, VERB_ONLINE_ONLY, service_watchdogs },
38fcb7f7
ZJS
1020 { "show-environment", VERB_ANY, 1, VERB_ONLINE_ONLY, show_environment },
1021 { "set-environment", 2, VERB_ANY, VERB_ONLINE_ONLY, set_environment },
1022 { "unset-environment", 2, VERB_ANY, VERB_ONLINE_ONLY, set_environment },
1023 { "import-environment", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, import_environment },
1024 { "halt", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special },
1025 { "poweroff", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special },
1026 { "reboot", VERB_ANY, 2, VERB_ONLINE_ONLY, start_system_special },
1027 { "kexec", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special },
1028 { "suspend", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special },
1029 { "hibernate", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special },
1030 { "hybrid-sleep", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special },
1031 { "suspend-then-hibernate",VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special },
1032 { "default", VERB_ANY, 1, VERB_ONLINE_ONLY, start_special },
1033 { "rescue", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special },
1034 { "emergency", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special },
1035 { "exit", VERB_ANY, 2, VERB_ONLINE_ONLY, start_special },
1036 { "reset-failed", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, reset_failed },
1037 { "enable", 2, VERB_ANY, 0, enable_unit },
1038 { "disable", 2, VERB_ANY, 0, enable_unit },
1039 { "is-enabled", 2, VERB_ANY, 0, unit_is_enabled },
1040 { "reenable", 2, VERB_ANY, 0, enable_unit },
1041 { "preset", 2, VERB_ANY, 0, enable_unit },
1042 { "preset-all", VERB_ANY, 1, 0, preset_all },
1043 { "mask", 2, VERB_ANY, 0, enable_unit },
1044 { "unmask", 2, VERB_ANY, 0, enable_unit },
1045 { "link", 2, VERB_ANY, 0, enable_unit },
1046 { "revert", 2, VERB_ANY, 0, enable_unit },
1047 { "switch-root", 2, VERB_ANY, VERB_ONLINE_ONLY, switch_root },
94c3a838 1048 { "list-dependencies", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, list_dependencies },
38fcb7f7
ZJS
1049 { "set-default", 2, 2, 0, set_default },
1050 { "get-default", VERB_ANY, 1, 0, get_default },
1051 { "set-property", 3, VERB_ANY, VERB_ONLINE_ONLY, set_property },
1052 { "is-system-running", VERB_ANY, 1, 0, is_system_running },
1053 { "add-wants", 3, VERB_ANY, 0, add_dependency },
1054 { "add-requires", 3, VERB_ANY, 0, add_dependency },
1055 { "edit", 2, VERB_ANY, VERB_ONLINE_ONLY, edit },
5e8deb94 1056 { "bind", 3, 4, VERB_ONLINE_ONLY, mount_bind },
6faecbd3 1057 { "mount-image", 4, 5, VERB_ONLINE_ONLY, mount_image },
d08e75ed 1058 {}
e449de87 1059 };
7e4249b9 1060
4cb1015e
ZJS
1061 const Verb *verb = verbs_find_verb(argv[optind], verbs);
1062 if (verb && (verb->flags & VERB_ONLINE_ONLY) && arg_root)
1063 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1064 "Verb '%s' cannot be used with --root=.",
1065 argv[optind] ?: verb->verb);
1066
e449de87 1067 return dispatch_verb(argc, argv, verbs, NULL);
e4b61340
LP
1068}
1069
f2a3de01 1070static int run(int argc, char *argv[]) {
f459b602 1071 int r;
e4b61340 1072
a9cdc94f 1073 setlocale(LC_ALL, "");
74189020 1074 log_parse_environment();
2396fb04 1075 log_open();
1abaf488
LP
1076
1077 /* The journal merging logic potentially needs a lot of fds. */
1078 (void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE);
1079
592705f2 1080 sigbus_install();
e4b61340 1081
5fd87823 1082 r = systemctl_dispatch_parse_argv(argc, argv);
f459b602 1083 if (r <= 0)
e4b61340 1084 goto finish;
7e4249b9 1085
040524b4 1086 if (arg_action != ACTION_SYSTEMCTL && running_in_chroot() > 0) {
60675884
LP
1087 if (!arg_quiet)
1088 log_info("Running in chroot, ignoring request.");
f459b602 1089 r = 0;
82e23ddd
LP
1090 goto finish;
1091 }
1092
daf71ef6 1093 /* systemctl_main() will print an error message for the bus connection, but only if it needs to */
e4b61340
LP
1094
1095 switch (arg_action) {
1096
22f4096c 1097 case ACTION_SYSTEMCTL:
e449de87 1098 r = systemctl_main(argc, argv);
e4b61340 1099 break;
e4b61340 1100
daf71ef6
LP
1101 /* Legacy command aliases set arg_action. They provide some fallbacks, e.g. to tell sysvinit to
1102 * reboot after you have installed systemd binaries. */
081dc638 1103
e4b61340
LP
1104 case ACTION_HALT:
1105 case ACTION_POWEROFF:
1106 case ACTION_REBOOT:
5622dde3 1107 case ACTION_KEXEC:
4fbd7192 1108 r = halt_main();
e4b61340
LP
1109 break;
1110
e4b61340
LP
1111 case ACTION_RUNLEVEL2:
1112 case ACTION_RUNLEVEL3:
1113 case ACTION_RUNLEVEL4:
1114 case ACTION_RUNLEVEL5:
1115 case ACTION_RESCUE:
4fbd7192 1116 r = start_with_fallback();
e4b61340 1117 break;
7e4249b9 1118
e4b61340
LP
1119 case ACTION_RELOAD:
1120 case ACTION_REEXEC:
4fbd7192 1121 r = reload_with_fallback();
e4b61340
LP
1122 break;
1123
949d9ce9 1124 case ACTION_CANCEL_SHUTDOWN:
2cf05793 1125 r = logind_cancel_shutdown();
f6144808
LP
1126 break;
1127
eb22ac37 1128 case ACTION_RUNLEVEL:
4f16c1f4
LP
1129 r = runlevel_main();
1130 break;
1131
ea71b460
ZJS
1132 case ACTION_TELINIT:
1133 r = exec_telinit(argv);
1134 break;
1135
081dc638
AJ
1136 case ACTION_EXIT:
1137 case ACTION_SUSPEND:
1138 case ACTION_HIBERNATE:
1139 case ACTION_HYBRID_SLEEP:
e68c79db 1140 case ACTION_SUSPEND_THEN_HIBERNATE:
081dc638
AJ
1141 case ACTION_EMERGENCY:
1142 case ACTION_DEFAULT:
daf71ef6
LP
1143 /* systemctl verbs with no equivalent in the legacy commands. These cannot appear in
1144 * arg_action. Fall through. */
081dc638 1145
f459b602 1146 case _ACTION_INVALID:
e4b61340
LP
1147 default:
1148 assert_not_reached("Unknown action");
1149 }
7e4249b9
LP
1150
1151finish:
cf647b69
LP
1152 release_busses();
1153
d1405af3
ZJS
1154 /* Note that we return r here, not 0, so that we can implement the LSB-like return codes */
1155 return r;
7e4249b9 1156}
d1405af3
ZJS
1157
1158DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);
5fd87823 1159#endif