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