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