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