]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/systemctl/systemctl.c
systemctl: reduce scope of iterator variables
[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
daf71ef6 495 case 't': {
d81bc51f
LP
496 const char *p;
497
498 if (isempty(optarg))
daf71ef6
LP
499 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
500 "--type= requires arguments.");
d81bc51f
LP
501
502 for (p = optarg;;) {
daf71ef6 503 _cleanup_free_ char *type = NULL;
d81bc51f 504
daf71ef6 505 r = extract_first_word(&p, &type, ",", 0);
d81bc51f 506 if (r < 0)
daf71ef6 507 return log_error_errno(r, "Failed to parse type: %s", optarg);
d81bc51f
LP
508 if (r == 0)
509 break;
510
daf71ef6
LP
511 if (streq(type, "help")) {
512 help_types();
d81bc51f
LP
513 return 0;
514 }
515
daf71ef6
LP
516 if (unit_type_from_string(type) >= 0) {
517 if (strv_consume(&arg_types, TAKE_PTR(type)) < 0)
518 return log_oom();
519 continue;
520 }
521
522 /* It's much nicer to use --state= for load states, but let's support this in
523 * --types= too for compatibility with old versions */
524 if (unit_load_state_from_string(type) >= 0) {
525 if (strv_consume(&arg_states, TAKE_PTR(type)) < 0)
526 return log_oom();
527 continue;
528 }
529
530 log_error("Unknown unit type or load state '%s'.", type);
531 return log_info_errno(SYNTHETIC_ERRNO(EINVAL),
532 "Use -t help to see a list of allowed values.");
d81bc51f
LP
533 }
534
535 break;
536 }
537
daf71ef6
LP
538 case 'P':
539 arg_value = true;
540 _fallthrough_;
dae710be 541
daf71ef6
LP
542 case 'p':
543 /* Make sure that if the empty property list was specified, we won't show any
544 properties. */
545 if (isempty(optarg) && !arg_properties) {
546 arg_properties = new0(char*, 1);
547 if (!arg_properties)
548 return log_oom();
549 } else {
550 const char *p;
46ad9c53 551
daf71ef6
LP
552 for (p = optarg;;) {
553 _cleanup_free_ char *prop = NULL;
46ad9c53 554
daf71ef6
LP
555 r = extract_first_word(&p, &prop, ",", 0);
556 if (r < 0)
557 return log_error_errno(r, "Failed to parse property: %s", optarg);
558 if (r == 0)
559 break;
46ad9c53 560
daf71ef6
LP
561 if (strv_consume(&arg_properties, TAKE_PTR(prop)) < 0)
562 return log_oom();
563 }
564 }
2e7e19ca 565
daf71ef6
LP
566 /* If the user asked for a particular property, show it, even if it is empty. */
567 arg_all = true;
7e4249b9 568
daf71ef6 569 break;
7e4249b9 570
daf71ef6
LP
571 case 'a':
572 arg_all = true;
573 break;
a8f11321 574
daf71ef6
LP
575 case ARG_REVERSE:
576 arg_dependency = DEPENDENCY_REVERSE;
577 break;
93a08841 578
daf71ef6
LP
579 case ARG_AFTER:
580 arg_dependency = DEPENDENCY_AFTER;
581 arg_jobs_after = true;
582 break;
7e4249b9 583
daf71ef6
LP
584 case ARG_BEFORE:
585 arg_dependency = DEPENDENCY_BEFORE;
586 arg_jobs_before = true;
587 break;
e4b61340 588
daf71ef6
LP
589 case ARG_SHOW_TYPES:
590 arg_show_types = true;
591 break;
e4b61340 592
daf71ef6
LP
593 case ARG_VALUE:
594 arg_value = true;
595 break;
e4b61340 596
daf71ef6
LP
597 case ARG_JOB_MODE:
598 arg_job_mode = optarg;
599 break;
e4b61340 600
daf71ef6
LP
601 case ARG_FAIL:
602 arg_job_mode = "fail";
603 break;
e4b61340 604
daf71ef6
LP
605 case ARG_IRREVERSIBLE:
606 arg_job_mode = "replace-irreversibly";
607 break;
e4b61340 608
daf71ef6
LP
609 case ARG_IGNORE_DEPENDENCIES:
610 arg_job_mode = "ignore-dependencies";
611 break;
e4b61340 612
daf71ef6
LP
613 case ARG_USER:
614 arg_scope = UNIT_FILE_USER;
e4b61340
LP
615 break;
616
daf71ef6
LP
617 case ARG_SYSTEM:
618 arg_scope = UNIT_FILE_SYSTEM;
e4b61340
LP
619 break;
620
daf71ef6
LP
621 case ARG_GLOBAL:
622 arg_scope = UNIT_FILE_GLOBAL;
e4b61340
LP
623 break;
624
daf71ef6
LP
625 case ARG_WAIT:
626 arg_wait = true;
e4b61340
LP
627 break;
628
daf71ef6
LP
629 case ARG_NO_BLOCK:
630 arg_no_block = true;
e4b61340
LP
631 break;
632
daf71ef6
LP
633 case ARG_NO_LEGEND:
634 arg_no_legend = true;
e4b61340
LP
635 break;
636
daf71ef6
LP
637 case ARG_NO_PAGER:
638 arg_pager_flags |= PAGER_DISABLE;
f3f054f0
TB
639 break;
640
514f4ef5
LP
641 case ARG_NO_WALL:
642 arg_no_wall = true;
643 break;
644
daf71ef6
LP
645 case ARG_ROOT:
646 r = parse_path_argument_and_warn(optarg, false, &arg_root);
647 if (r < 0)
648 return r;
e4b61340
LP
649 break;
650
daf71ef6
LP
651 case 'l':
652 arg_full = true;
653 break;
e4b61340 654
daf71ef6
LP
655 case ARG_FAILED:
656 if (strv_extend(&arg_states, "failed") < 0)
657 return log_oom();
e4b61340 658
daf71ef6 659 break;
e4b61340 660
daf71ef6
LP
661 case ARG_DRY_RUN:
662 arg_dry_run = true;
663 break;
e4b61340 664
daf71ef6
LP
665 case 'q':
666 arg_quiet = true;
667 break;
e4b61340 668
daf71ef6
LP
669 case 'f':
670 arg_force++;
671 break;
e4b61340 672
daf71ef6
LP
673 case ARG_NO_RELOAD:
674 arg_no_reload = true;
675 break;
e4b61340 676
daf71ef6
LP
677 case ARG_KILL_WHO:
678 arg_kill_who = optarg;
679 break;
e4b61340 680
daf71ef6
LP
681 case 's':
682 if (streq(optarg, "help")) {
683 DUMP_STRING_TABLE(signal, int, _NSIG);
684 return 0;
685 }
686
687 arg_signal = signal_from_string(optarg);
688 if (arg_signal < 0)
689 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
690 "Failed to parse signal string %s.",
691 optarg);
692 break;
e4b61340 693
daf71ef6
LP
694 case ARG_NO_ASK_PASSWORD:
695 arg_ask_password = false;
696 break;
e4b61340
LP
697
698 case 'H':
daf71ef6
LP
699 arg_transport = BUS_TRANSPORT_REMOTE;
700 arg_host = optarg;
e4b61340
LP
701 break;
702
daf71ef6
LP
703 case 'M':
704 arg_transport = BUS_TRANSPORT_MACHINE;
705 arg_host = optarg;
e4b61340
LP
706 break;
707
daf71ef6
LP
708 case ARG_RUNTIME:
709 arg_runtime = true;
e4b61340
LP
710 break;
711
daf71ef6
LP
712 case 'n':
713 if (safe_atou(optarg, &arg_lines) < 0)
714 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
715 "Failed to parse lines '%s'",
716 optarg);
04ebb595
LP
717 break;
718
daf71ef6
LP
719 case 'o':
720 if (streq(optarg, "help")) {
721 DUMP_STRING_TABLE(output_mode, OutputMode, _OUTPUT_MODE_MAX);
722 return 0;
723 }
724
725 arg_output = output_mode_from_string(optarg);
726 if (arg_output < 0)
727 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
728 "Unknown output '%s'.",
729 optarg);
730
731 if (OUTPUT_MODE_IS_JSON(arg_output)) {
732 arg_no_legend = true;
733 arg_plain = true;
734 }
e4b61340
LP
735 break;
736
daf71ef6 737 case 'i':
4327574f
FS
738 arg_check_inhibitors = 0;
739 break;
740
741 case ARG_CHECK_INHIBITORS:
742 if (streq(optarg, "auto"))
743 arg_check_inhibitors = -1;
744 else {
745 r = parse_boolean(optarg);
746 if (r < 0)
747 return log_error_errno(r, "Failed to parse --check-inhibitors= argument: %s", optarg);
748 arg_check_inhibitors = r;
749 }
e4b61340
LP
750 break;
751
daf71ef6
LP
752 case ARG_PLAIN:
753 arg_plain = true;
514f4ef5
LP
754 break;
755
daf71ef6
LP
756 case ARG_FIRMWARE_SETUP:
757 arg_firmware_setup = true;
e4b61340
LP
758 break;
759
daf71ef6 760 case ARG_BOOT_LOADER_MENU:
f6144808 761
daf71ef6
LP
762 r = parse_sec(optarg, &arg_boot_loader_menu);
763 if (r < 0)
764 return log_error_errno(r, "Failed to parse --boot-loader-menu= argument '%s': %m", optarg);
e4b61340 765
daf71ef6 766 break;
e4b61340 767
daf71ef6 768 case ARG_BOOT_LOADER_ENTRY:
e4b61340 769
daf71ef6
LP
770 if (streq(optarg, "help")) { /* Yes, this means, "help" is not a valid boot loader entry name we can deal with */
771 r = help_boot_loader_entry();
772 if (r < 0)
773 return r;
e4b61340 774
daf71ef6
LP
775 return 0;
776 }
e4b61340 777
daf71ef6
LP
778 arg_boot_loader_entry = empty_to_null(optarg);
779 break;
e4b61340 780
daf71ef6
LP
781 case ARG_STATE: {
782 const char *p;
e4b61340 783
daf71ef6
LP
784 if (isempty(optarg))
785 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
786 "--state= requires arguments.");
e4b61340 787
daf71ef6
LP
788 for (p = optarg;;) {
789 _cleanup_free_ char *s = NULL;
e4b61340 790
daf71ef6
LP
791 r = extract_first_word(&p, &s, ",", 0);
792 if (r < 0)
793 return log_error_errno(r, "Failed to parse state: %s", optarg);
794 if (r == 0)
795 break;
e4b61340 796
daf71ef6
LP
797 if (streq(s, "help")) {
798 help_states();
799 return 0;
800 }
e4b61340 801
daf71ef6
LP
802 if (strv_consume(&arg_states, TAKE_PTR(s)) < 0)
803 return log_oom();
804 }
805 break;
806 }
e4b61340 807
daf71ef6
LP
808 case 'r':
809 if (geteuid() != 0)
810 return log_error_errno(SYNTHETIC_ERRNO(EPERM),
811 "--recursive requires root privileges.");
812
813 arg_recursive = true;
514f4ef5
LP
814 break;
815
daf71ef6
LP
816 case ARG_PRESET_MODE:
817 if (streq(optarg, "help")) {
818 DUMP_STRING_TABLE(unit_file_preset_mode, UnitFilePresetMode, _UNIT_FILE_PRESET_MAX);
819 return 0;
820 }
e4b61340 821
daf71ef6
LP
822 arg_preset_mode = unit_file_preset_mode_from_string(optarg);
823 if (arg_preset_mode < 0)
824 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
825 "Failed to parse preset mode: %s.", optarg);
e4b61340 826
daf71ef6 827 break;
e4b61340 828
daf71ef6
LP
829 case ARG_NOW:
830 arg_now = true;
831 break;
e4b61340 832
daf71ef6
LP
833 case ARG_MESSAGE:
834 if (strv_extend(&arg_wall, optarg) < 0)
835 return log_oom();
836 break;
e4b61340 837
daf71ef6
LP
838 case 'T':
839 arg_show_transaction = true;
e4b61340
LP
840 break;
841
daf71ef6
LP
842 case ARG_WITH_DEPENDENCIES:
843 arg_with_dependencies = true;
844 break;
e4b61340 845
daf71ef6
LP
846 case ARG_WHAT: {
847 const char *p;
e4b61340 848
daf71ef6
LP
849 if (isempty(optarg))
850 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--what= requires arguments.");
e4b61340 851
daf71ef6
LP
852 for (p = optarg;;) {
853 _cleanup_free_ char *k = NULL;
e4b61340 854
daf71ef6
LP
855 r = extract_first_word(&p, &k, ",", 0);
856 if (r < 0)
857 return log_error_errno(r, "Failed to parse directory type: %s", optarg);
858 if (r == 0)
859 break;
e4b61340 860
daf71ef6
LP
861 if (streq(k, "help")) {
862 puts("runtime\n"
863 "state\n"
864 "cache\n"
865 "logs\n"
866 "configuration");
867 return 0;
868 }
e4b61340 869
daf71ef6
LP
870 r = strv_consume(&arg_clean_what, TAKE_PTR(k));
871 if (r < 0)
872 return log_oom();
873 }
e4b61340 874
daf71ef6
LP
875 break;
876 }
e4b61340 877
daf71ef6
LP
878 case ARG_REBOOT_ARG:
879 arg_reboot_argument = optarg;
880 break;
881
882 case ARG_TIMESTAMP_STYLE:
883 if (streq(optarg, "help")) {
884 DUMP_STRING_TABLE(timestamp_style, TimestampStyle, _TIMESTAMP_STYLE_MAX);
885 return 0;
886 }
887
888 arg_timestamp_style = timestamp_style_from_string(optarg);
889 if (arg_timestamp_style < 0)
890 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
891 "Invalid value: %s.", optarg);
892
893 break;
e4b61340 894
5e8deb94
LB
895 case ARG_READ_ONLY:
896 arg_read_only = true;
897 break;
898
899 case ARG_MKDIR:
900 arg_mkdir = true;
901 break;
902
daf71ef6
LP
903 case '.':
904 /* Output an error mimicking getopt, and print a hint afterwards */
905 log_error("%s: invalid option -- '.'", program_invocation_name);
906 log_notice("Hint: to specify units starting with a dash, use \"--\":\n"
907 " %s [OPTIONS...] COMMAND -- -.%s ...",
908 program_invocation_name, optarg ?: "mount");
909 _fallthrough_;
e4b61340
LP
910
911 case '?':
912 return -EINVAL;
913
914 default:
eb9da376 915 assert_not_reached("Unhandled option");
e4b61340 916 }
e4b61340 917
1b630835 918 if (arg_transport == BUS_TRANSPORT_REMOTE && arg_scope != UNIT_FILE_SYSTEM)
daf71ef6
LP
919 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
920 "Cannot access user instance remotely.");
921
922 if (arg_wait && arg_no_block)
baaa35ad 923 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
daf71ef6 924 "--wait may not be combined with --no-block.");
e4b61340
LP
925
926 return 1;
927}
928
5fd87823 929int systemctl_dispatch_parse_argv(int argc, char *argv[]) {
e4b61340
LP
930 assert(argc >= 0);
931 assert(argv);
932
933 if (program_invocation_short_name) {
934
935 if (strstr(program_invocation_short_name, "halt")) {
936 arg_action = ACTION_HALT;
937 return halt_parse_argv(argc, argv);
c54819ca 938
e4b61340
LP
939 } else if (strstr(program_invocation_short_name, "poweroff")) {
940 arg_action = ACTION_POWEROFF;
941 return halt_parse_argv(argc, argv);
c54819ca 942
e4b61340 943 } else if (strstr(program_invocation_short_name, "reboot")) {
5622dde3
KS
944 if (kexec_loaded())
945 arg_action = ACTION_KEXEC;
946 else
947 arg_action = ACTION_REBOOT;
e4b61340 948 return halt_parse_argv(argc, argv);
c54819ca 949
e4b61340
LP
950 } else if (strstr(program_invocation_short_name, "shutdown")) {
951 arg_action = ACTION_POWEROFF;
952 return shutdown_parse_argv(argc, argv);
c54819ca 953
e4b61340 954 } else if (strstr(program_invocation_short_name, "init")) {
d5ca5f11 955
daf71ef6
LP
956 /* Matches invocations as "init" as well as "telinit", which are synonymous when run
957 * as PID != 1 on SysV.
c54819ca
LP
958 *
959 * On SysV "telinit" was the official command to communicate with PID 1, but "init" would
960 * redirect itself to "telinit" if called with PID != 1. We follow the same logic here still,
961 * though we add one level of indirection, as we implement "telinit" in "systemctl". Hence, for
962 * us if you invoke "init" you get "systemd", but it will execve() "systemctl" immediately with
963 * argv[] unmodified if PID is != 1. If you invoke "telinit" you directly get "systemctl". In
964 * both cases we shall do the same thing, which is why we do strstr(p_i_s_n, "init") here, as a
965 * quick way to match both.
966 *
967 * Also see redirect_telinit() in src/core/main.c. */
968
d5ca5f11 969 if (sd_booted() > 0) {
f459b602 970 arg_action = _ACTION_INVALID;
d5ca5f11
LP
971 return telinit_parse_argv(argc, argv);
972 } else {
54d04cd1
ZJS
973 /* Hmm, so some other init system is running, we need to forward this request
974 * to it. */
d5ca5f11 975
ea71b460
ZJS
976 arg_action = ACTION_TELINIT;
977 return 1;
d5ca5f11
LP
978 }
979
e4b61340
LP
980 } else if (strstr(program_invocation_short_name, "runlevel")) {
981 arg_action = ACTION_RUNLEVEL;
982 return runlevel_parse_argv(argc, argv);
983 }
984 }
985
986 arg_action = ACTION_SYSTEMCTL;
987 return systemctl_parse_argv(argc, argv);
988}
989
5fd87823 990#ifndef FUZZ_SYSTEMCTL_PARSE_ARGV
e449de87 991static int systemctl_main(int argc, char *argv[]) {
e449de87 992 static const Verb verbs[] = {
c56d1e2c 993 { "list-units", VERB_ANY, VERB_ANY, VERB_DEFAULT|VERB_ONLINE_ONLY, list_units },
38fcb7f7
ZJS
994 { "list-unit-files", VERB_ANY, VERB_ANY, 0, list_unit_files },
995 { "list-sockets", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, list_sockets },
996 { "list-timers", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, list_timers },
997 { "list-jobs", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, list_jobs },
998 { "list-machines", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, list_machines },
999 { "clear-jobs", VERB_ANY, 1, VERB_ONLINE_ONLY, trivial_method },
1000 { "cancel", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, cancel_job },
1001 { "start", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit },
1002 { "stop", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit },
1003 { "condstop", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, /* For compatibility with ALTLinux */
1004 { "reload", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit },
1005 { "restart", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit },
1006 { "try-restart", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit },
1007 { "reload-or-restart", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit },
1008 { "reload-or-try-restart", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, /* For compatibility with old systemctl <= 228 */
1009 { "try-reload-or-restart", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit },
1010 { "force-reload", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, /* For compatibility with SysV */
1011 { "condreload", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, /* For compatibility with ALTLinux */
1012 { "condrestart", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, /* For compatibility with RH */
1013 { "isolate", 2, 2, VERB_ONLINE_ONLY, start_unit },
1014 { "kill", 2, VERB_ANY, VERB_ONLINE_ONLY, kill_unit },
d9e45bc3
MS
1015 { "clean", 2, VERB_ANY, VERB_ONLINE_ONLY, clean_or_freeze_unit },
1016 { "freeze", 2, VERB_ANY, VERB_ONLINE_ONLY, clean_or_freeze_unit },
1017 { "thaw", 2, VERB_ANY, VERB_ONLINE_ONLY, clean_or_freeze_unit },
38fcb7f7
ZJS
1018 { "is-active", 2, VERB_ANY, VERB_ONLINE_ONLY, check_unit_active },
1019 { "check", 2, VERB_ANY, VERB_ONLINE_ONLY, check_unit_active }, /* deprecated alias of is-active */
1020 { "is-failed", 2, VERB_ANY, VERB_ONLINE_ONLY, check_unit_failed },
1021 { "show", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, show },
1022 { "cat", 2, VERB_ANY, VERB_ONLINE_ONLY, cat },
1023 { "status", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, show },
1024 { "help", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, show },
1025 { "daemon-reload", VERB_ANY, 1, VERB_ONLINE_ONLY, daemon_reload },
1026 { "daemon-reexec", VERB_ANY, 1, VERB_ONLINE_ONLY, daemon_reload },
77db9433
ZJS
1027 { "log-level", VERB_ANY, 2, VERB_ONLINE_ONLY, log_setting },
1028 { "log-target", VERB_ANY, 2, VERB_ONLINE_ONLY, log_setting },
6824c132
ZJS
1029 { "service-log-level", 2, 3, VERB_ONLINE_ONLY, service_log_setting },
1030 { "service-log-target", 2, 3, VERB_ONLINE_ONLY, service_log_setting },
ae98f726 1031 { "service-watchdogs", VERB_ANY, 2, VERB_ONLINE_ONLY, service_watchdogs },
38fcb7f7
ZJS
1032 { "show-environment", VERB_ANY, 1, VERB_ONLINE_ONLY, show_environment },
1033 { "set-environment", 2, VERB_ANY, VERB_ONLINE_ONLY, set_environment },
1034 { "unset-environment", 2, VERB_ANY, VERB_ONLINE_ONLY, set_environment },
1035 { "import-environment", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, import_environment },
1036 { "halt", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special },
1037 { "poweroff", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special },
1038 { "reboot", VERB_ANY, 2, VERB_ONLINE_ONLY, start_system_special },
1039 { "kexec", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special },
1040 { "suspend", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special },
1041 { "hibernate", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special },
1042 { "hybrid-sleep", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special },
1043 { "suspend-then-hibernate",VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special },
1044 { "default", VERB_ANY, 1, VERB_ONLINE_ONLY, start_special },
1045 { "rescue", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special },
1046 { "emergency", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special },
1047 { "exit", VERB_ANY, 2, VERB_ONLINE_ONLY, start_special },
1048 { "reset-failed", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, reset_failed },
1049 { "enable", 2, VERB_ANY, 0, enable_unit },
1050 { "disable", 2, VERB_ANY, 0, enable_unit },
1051 { "is-enabled", 2, VERB_ANY, 0, unit_is_enabled },
1052 { "reenable", 2, VERB_ANY, 0, enable_unit },
1053 { "preset", 2, VERB_ANY, 0, enable_unit },
1054 { "preset-all", VERB_ANY, 1, 0, preset_all },
1055 { "mask", 2, VERB_ANY, 0, enable_unit },
1056 { "unmask", 2, VERB_ANY, 0, enable_unit },
1057 { "link", 2, VERB_ANY, 0, enable_unit },
1058 { "revert", 2, VERB_ANY, 0, enable_unit },
1059 { "switch-root", 2, VERB_ANY, VERB_ONLINE_ONLY, switch_root },
94c3a838 1060 { "list-dependencies", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, list_dependencies },
38fcb7f7
ZJS
1061 { "set-default", 2, 2, 0, set_default },
1062 { "get-default", VERB_ANY, 1, 0, get_default },
1063 { "set-property", 3, VERB_ANY, VERB_ONLINE_ONLY, set_property },
1064 { "is-system-running", VERB_ANY, 1, 0, is_system_running },
1065 { "add-wants", 3, VERB_ANY, 0, add_dependency },
1066 { "add-requires", 3, VERB_ANY, 0, add_dependency },
1067 { "edit", 2, VERB_ANY, VERB_ONLINE_ONLY, edit },
5e8deb94 1068 { "bind", 3, 4, VERB_ONLINE_ONLY, mount_bind },
6faecbd3 1069 { "mount-image", 4, 5, VERB_ONLINE_ONLY, mount_image },
d08e75ed 1070 {}
e449de87 1071 };
7e4249b9 1072
4cb1015e
ZJS
1073 const Verb *verb = verbs_find_verb(argv[optind], verbs);
1074 if (verb && (verb->flags & VERB_ONLINE_ONLY) && arg_root)
1075 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1076 "Verb '%s' cannot be used with --root=.",
1077 argv[optind] ?: verb->verb);
1078
e449de87 1079 return dispatch_verb(argc, argv, verbs, NULL);
e4b61340
LP
1080}
1081
f2a3de01 1082static int run(int argc, char *argv[]) {
f459b602 1083 int r;
e4b61340 1084
a9cdc94f 1085 setlocale(LC_ALL, "");
74189020 1086 log_parse_environment();
2396fb04 1087 log_open();
1abaf488
LP
1088
1089 /* The journal merging logic potentially needs a lot of fds. */
1090 (void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE);
1091
592705f2 1092 sigbus_install();
e4b61340 1093
5fd87823 1094 r = systemctl_dispatch_parse_argv(argc, argv);
f459b602 1095 if (r <= 0)
e4b61340 1096 goto finish;
7e4249b9 1097
040524b4 1098 if (arg_action != ACTION_SYSTEMCTL && running_in_chroot() > 0) {
60675884
LP
1099 if (!arg_quiet)
1100 log_info("Running in chroot, ignoring request.");
f459b602 1101 r = 0;
82e23ddd
LP
1102 goto finish;
1103 }
1104
daf71ef6 1105 /* systemctl_main() will print an error message for the bus connection, but only if it needs to */
e4b61340
LP
1106
1107 switch (arg_action) {
1108
22f4096c 1109 case ACTION_SYSTEMCTL:
e449de87 1110 r = systemctl_main(argc, argv);
e4b61340 1111 break;
e4b61340 1112
daf71ef6
LP
1113 /* Legacy command aliases set arg_action. They provide some fallbacks, e.g. to tell sysvinit to
1114 * reboot after you have installed systemd binaries. */
081dc638 1115
e4b61340
LP
1116 case ACTION_HALT:
1117 case ACTION_POWEROFF:
1118 case ACTION_REBOOT:
5622dde3 1119 case ACTION_KEXEC:
4fbd7192 1120 r = halt_main();
e4b61340
LP
1121 break;
1122
e4b61340
LP
1123 case ACTION_RUNLEVEL2:
1124 case ACTION_RUNLEVEL3:
1125 case ACTION_RUNLEVEL4:
1126 case ACTION_RUNLEVEL5:
1127 case ACTION_RESCUE:
4fbd7192 1128 r = start_with_fallback();
e4b61340 1129 break;
7e4249b9 1130
e4b61340
LP
1131 case ACTION_RELOAD:
1132 case ACTION_REEXEC:
4fbd7192 1133 r = reload_with_fallback();
e4b61340
LP
1134 break;
1135
949d9ce9 1136 case ACTION_CANCEL_SHUTDOWN:
2cf05793 1137 r = logind_cancel_shutdown();
f6144808
LP
1138 break;
1139
eb22ac37 1140 case ACTION_RUNLEVEL:
4f16c1f4
LP
1141 r = runlevel_main();
1142 break;
1143
ea71b460
ZJS
1144 case ACTION_TELINIT:
1145 r = exec_telinit(argv);
1146 break;
1147
081dc638
AJ
1148 case ACTION_EXIT:
1149 case ACTION_SUSPEND:
1150 case ACTION_HIBERNATE:
1151 case ACTION_HYBRID_SLEEP:
e68c79db 1152 case ACTION_SUSPEND_THEN_HIBERNATE:
081dc638
AJ
1153 case ACTION_EMERGENCY:
1154 case ACTION_DEFAULT:
daf71ef6
LP
1155 /* systemctl verbs with no equivalent in the legacy commands. These cannot appear in
1156 * arg_action. Fall through. */
081dc638 1157
f459b602 1158 case _ACTION_INVALID:
e4b61340
LP
1159 default:
1160 assert_not_reached("Unknown action");
1161 }
7e4249b9
LP
1162
1163finish:
cf647b69
LP
1164 release_busses();
1165
d1405af3
ZJS
1166 /* Note that we return r here, not 0, so that we can implement the LSB-like return codes */
1167 return r;
7e4249b9 1168}
d1405af3
ZJS
1169
1170DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);
5fd87823 1171#endif