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