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