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