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