]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/systemctl/systemctl.c
c62a514777edb12858679f8e45b576e5cd31d607
[thirdparty/systemd.git] / src / systemctl / systemctl.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <getopt.h>
4 #include <signal.h>
5 #include <unistd.h>
6
7 #include "argv-util.h"
8 #include "build.h"
9 #include "bus-print-properties.h"
10 #include "bus-util.h"
11 #include "capsule-util.h"
12 #include "extract-word.h"
13 #include "image-policy.h"
14 #include "install.h"
15 #include "output-mode.h"
16 #include "pager.h"
17 #include "parse-argument.h"
18 #include "parse-util.h"
19 #include "pretty-print.h"
20 #include "static-destruct.h"
21 #include "string-table.h"
22 #include "string-util.h"
23 #include "strv.h"
24 #include "systemctl.h"
25 #include "systemctl-compat-halt.h"
26 #include "systemctl-compat-runlevel.h"
27 #include "systemctl-compat-shutdown.h"
28 #include "systemctl-compat-telinit.h"
29 #include "systemctl-logind.h"
30 #include "time-util.h"
31
32 char **arg_types = NULL;
33 char **arg_states = NULL;
34 char **arg_properties = NULL;
35 bool arg_all = false;
36 enum dependency arg_dependency = DEPENDENCY_FORWARD;
37 const char *_arg_job_mode = NULL;
38 RuntimeScope arg_runtime_scope = RUNTIME_SCOPE_SYSTEM;
39 bool arg_wait = false;
40 bool arg_no_block = false;
41 int arg_legend = -1; /* -1: true, unless --quiet is passed, 1: true */
42 PagerFlags arg_pager_flags = 0;
43 bool arg_no_wtmp = false;
44 bool arg_no_sync = false;
45 bool arg_no_wall = false;
46 bool arg_no_reload = false;
47 BusPrintPropertyFlags arg_print_flags = 0;
48 bool arg_show_types = false;
49 int arg_check_inhibitors = -1;
50 bool arg_dry_run = false;
51 bool arg_quiet = false;
52 bool arg_verbose = false;
53 bool arg_no_warn = false;
54 bool arg_full = false;
55 bool arg_recursive = false;
56 bool arg_with_dependencies = false;
57 bool arg_show_transaction = false;
58 int arg_force = 0;
59 bool arg_ask_password = false;
60 bool arg_runtime = false;
61 UnitFilePresetMode arg_preset_mode = UNIT_FILE_PRESET_FULL;
62 char **arg_wall = NULL;
63 const char *arg_kill_whom = NULL;
64 int arg_signal = SIGTERM;
65 int arg_kill_value;
66 bool arg_kill_value_set = false;
67 char *arg_root = NULL;
68 char *arg_image = NULL;
69 usec_t arg_when = 0;
70 bool arg_stdin = false;
71 const char *arg_reboot_argument = NULL;
72 enum action arg_action = ACTION_SYSTEMCTL;
73 BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
74 const char *arg_host = NULL;
75 unsigned arg_lines = 10;
76 OutputMode arg_output = OUTPUT_SHORT;
77 bool arg_plain = false;
78 bool arg_firmware_setup = false;
79 usec_t arg_boot_loader_menu = USEC_INFINITY;
80 const char *arg_boot_loader_entry = NULL;
81 bool arg_now = false;
82 bool arg_jobs_before = false;
83 bool arg_jobs_after = false;
84 char **arg_clean_what = NULL;
85 TimestampStyle arg_timestamp_style = TIMESTAMP_PRETTY;
86 bool arg_read_only = false;
87 bool arg_mkdir = false;
88 bool arg_marked = false;
89 const char *arg_drop_in = NULL;
90 ImagePolicy *arg_image_policy = NULL;
91
92 STATIC_DESTRUCTOR_REGISTER(arg_types, strv_freep);
93 STATIC_DESTRUCTOR_REGISTER(arg_states, strv_freep);
94 STATIC_DESTRUCTOR_REGISTER(arg_properties, strv_freep);
95 STATIC_DESTRUCTOR_REGISTER(_arg_job_mode, unsetp);
96 STATIC_DESTRUCTOR_REGISTER(arg_wall, strv_freep);
97 STATIC_DESTRUCTOR_REGISTER(arg_kill_whom, unsetp);
98 STATIC_DESTRUCTOR_REGISTER(arg_root, freep);
99 STATIC_DESTRUCTOR_REGISTER(arg_image, freep);
100 STATIC_DESTRUCTOR_REGISTER(arg_reboot_argument, unsetp);
101 STATIC_DESTRUCTOR_REGISTER(arg_host, unsetp);
102 STATIC_DESTRUCTOR_REGISTER(arg_boot_loader_entry, unsetp);
103 STATIC_DESTRUCTOR_REGISTER(arg_clean_what, strv_freep);
104 STATIC_DESTRUCTOR_REGISTER(arg_drop_in, unsetp);
105 STATIC_DESTRUCTOR_REGISTER(arg_image_policy, image_policy_freep);
106
107 static int systemctl_help(void) {
108 _cleanup_free_ char *link = NULL;
109 int r;
110
111 pager_open(arg_pager_flags);
112
113 r = terminal_urlify_man("systemctl", "1", &link);
114 if (r < 0)
115 return log_oom();
116
117 printf("%1$s [OPTIONS...] COMMAND ...\n\n"
118 "%5$sQuery or send control commands to the system manager.%6$s\n"
119 "\n%3$sUnit Commands:%4$s\n"
120 " list-units [PATTERN...] List units currently in memory\n"
121 " list-automounts [PATTERN...] List automount units currently in memory,\n"
122 " ordered by path\n"
123 " list-paths [PATTERN...] List path units currently in memory,\n"
124 " ordered by path\n"
125 " list-sockets [PATTERN...] List socket units currently in memory,\n"
126 " ordered by address\n"
127 " list-timers [PATTERN...] List timer units currently in memory,\n"
128 " ordered by next elapse\n"
129 " is-active PATTERN... Check whether units are active\n"
130 " is-failed [PATTERN...] Check whether units are failed or\n"
131 " system is in degraded state\n"
132 " status [PATTERN...|PID...] Show runtime status of one or more units\n"
133 " show [PATTERN...|JOB...] Show properties of one or more\n"
134 " units/jobs or the manager\n"
135 " cat PATTERN... Show files and drop-ins of specified units\n"
136 " help PATTERN...|PID... Show manual for one or more units\n"
137 " list-dependencies [UNIT...] Recursively show units which are required\n"
138 " or wanted by the units or by which those\n"
139 " units are required or wanted\n"
140 " start UNIT... Start (activate) one or more units\n"
141 " stop UNIT... Stop (deactivate) one or more units\n"
142 " reload UNIT... Reload one or more units\n"
143 " restart UNIT... Start or restart one or more units\n"
144 " try-restart UNIT... Restart one or more units if active\n"
145 " reload-or-restart UNIT... Reload one or more units if possible,\n"
146 " otherwise start or restart\n"
147 " try-reload-or-restart UNIT... If active, reload one or more units,\n"
148 " if supported, otherwise restart\n"
149 " isolate UNIT Start one unit and stop all others\n"
150 " kill UNIT... Send signal to processes of a unit\n"
151 " clean UNIT... Clean runtime, cache, state, logs or\n"
152 " configuration of unit\n"
153 " freeze PATTERN... Freeze execution of unit processes\n"
154 " thaw PATTERN... Resume execution of a frozen unit\n"
155 " set-property UNIT PROPERTY=VALUE... Sets one or more properties of a unit\n"
156 " bind UNIT PATH [PATH] Bind-mount a path from the host into a\n"
157 " unit's namespace\n"
158 " mount-image UNIT PATH [PATH [OPTS]] Mount an image from the host into a\n"
159 " unit's namespace\n"
160 " service-log-level SERVICE [LEVEL] Get/set logging threshold for service\n"
161 " service-log-target SERVICE [TARGET] Get/set logging target for service\n"
162 " reset-failed [PATTERN...] Reset failed state for all, one, or more\n"
163 " units\n"
164 " whoami [PID...] Return unit caller or specified PIDs are\n"
165 " part of\n"
166 "\n%3$sUnit File Commands:%4$s\n"
167 " list-unit-files [PATTERN...] List installed unit files\n"
168 " enable [UNIT...|PATH...] Enable one or more unit files\n"
169 " disable UNIT... Disable one or more unit files\n"
170 " reenable UNIT... Reenable one or more unit files\n"
171 " preset UNIT... Enable/disable one or more unit files\n"
172 " based on preset configuration\n"
173 " preset-all Enable/disable all unit files based on\n"
174 " preset configuration\n"
175 " is-enabled UNIT... Check whether unit files are enabled\n"
176 " mask UNIT... Mask one or more units\n"
177 " unmask UNIT... Unmask one or more units\n"
178 " link PATH... Link one or more units files into\n"
179 " the search path\n"
180 " revert UNIT... Revert one or more unit files to vendor\n"
181 " version\n"
182 " add-wants TARGET UNIT... Add 'Wants' dependency for the target\n"
183 " on specified one or more units\n"
184 " add-requires TARGET UNIT... Add 'Requires' dependency for the target\n"
185 " on specified one or more units\n"
186 " edit UNIT... Edit one or more unit files\n"
187 " get-default Get the name of the default target\n"
188 " set-default TARGET Set the default target\n"
189 "\n%3$sMachine Commands:%4$s\n"
190 " list-machines [PATTERN...] List local containers and host\n"
191 "\n%3$sJob Commands:%4$s\n"
192 " list-jobs [PATTERN...] List jobs\n"
193 " cancel [JOB...] Cancel all, one, or more jobs\n"
194 "\n%3$sEnvironment Commands:%4$s\n"
195 " show-environment Dump environment\n"
196 " set-environment VARIABLE=VALUE... Set one or more environment variables\n"
197 " unset-environment VARIABLE... Unset one or more environment variables\n"
198 " import-environment VARIABLE... Import all or some environment variables\n"
199 "\n%3$sManager State Commands:%4$s\n"
200 " daemon-reload Reload systemd manager configuration\n"
201 " daemon-reexec Reexecute systemd manager\n"
202 " log-level [LEVEL] Get/set logging threshold for manager\n"
203 " log-target [TARGET] Get/set logging target for manager\n"
204 " service-watchdogs [BOOL] Get/set service watchdog state\n"
205 "\n%3$sSystem Commands:%4$s\n"
206 " is-system-running Check whether system is fully running\n"
207 " default Enter system default mode\n"
208 " rescue Enter system rescue mode\n"
209 " emergency Enter system emergency mode\n"
210 " halt Shut down and halt the system\n"
211 " poweroff Shut down and power-off the system\n"
212 " reboot Shut down and reboot the system\n"
213 " kexec Shut down and reboot the system with kexec\n"
214 " soft-reboot Shut down and reboot userspace\n"
215 " exit [EXIT_CODE] Request user instance or container exit\n"
216 " switch-root [ROOT [INIT]] Change to a different root file system\n"
217 " sleep Put the system to sleep (through one of\n"
218 " the operations below)\n"
219 " suspend Suspend the system\n"
220 " hibernate Hibernate the system\n"
221 " hybrid-sleep Hibernate and suspend the system\n"
222 " suspend-then-hibernate Suspend the system, wake after a period of\n"
223 " time, and hibernate"
224 "\n%3$sOptions:%4$s\n"
225 " -h --help Show this help\n"
226 " --version Show package version\n"
227 " --system Connect to system manager\n"
228 " --user Connect to user service manager\n"
229 " -C --capsule=NAME Connect to service manager of specified capsule\n"
230 " -H --host=[USER@]HOST Operate on remote host\n"
231 " -M --machine=CONTAINER Operate on a local container\n"
232 " -t --type=TYPE List units of a particular type\n"
233 " --state=STATE List units with particular LOAD or SUB or ACTIVE state\n"
234 " --failed Shortcut for --state=failed\n"
235 " -p --property=NAME Show only properties by this name\n"
236 " -P NAME Equivalent to --value --property=NAME\n"
237 " -a --all Show all properties/all units currently in memory,\n"
238 " including dead/empty ones. To list all units installed\n"
239 " on the system, use 'list-unit-files' instead.\n"
240 " -l --full Don't ellipsize unit names on output\n"
241 " -r --recursive Show unit list of host and local containers\n"
242 " --reverse Show reverse dependencies with 'list-dependencies'\n"
243 " --before Show units ordered before with 'list-dependencies'\n"
244 " --after Show units ordered after with 'list-dependencies'\n"
245 " --with-dependencies Show unit dependencies with 'status', 'cat',\n"
246 " 'list-units', and 'list-unit-files'.\n"
247 " --job-mode=MODE Specify how to deal with already queued jobs, when\n"
248 " queueing a new job\n"
249 " -T --show-transaction When enqueuing a unit job, show full transaction\n"
250 " --show-types When showing sockets, explicitly show their type\n"
251 " --value When showing properties, only print the value\n"
252 " --check-inhibitors=MODE\n"
253 " Whether to check inhibitors before shutting down,\n"
254 " sleeping, or hibernating\n"
255 " -i Shortcut for --check-inhibitors=no\n"
256 " --kill-whom=WHOM Whom to send signal to\n"
257 " --kill-value=INT Signal value to enqueue\n"
258 " -s --signal=SIGNAL Which signal to send\n"
259 " --what=RESOURCES Which types of resources to remove\n"
260 " --now Start or stop unit after enabling or disabling it\n"
261 " --dry-run Only print what would be done\n"
262 " Currently supported by verbs: halt, poweroff, reboot,\n"
263 " kexec, soft-reboot, suspend, hibernate, \n"
264 " suspend-then-hibernate, hybrid-sleep, default,\n"
265 " rescue, emergency, and exit.\n"
266 " -q --quiet Suppress output\n"
267 " -v --verbose Show unit logs while executing operation\n"
268 " --no-warn Suppress several warnings shown by default\n"
269 " --wait For (re)start, wait until service stopped again\n"
270 " For is-system-running, wait until startup is completed\n"
271 " For kill, wait until service stopped\n"
272 " --no-block Do not wait until operation finished\n"
273 " --no-wall Don't send wall message before halt/power-off/reboot\n"
274 " --message=MESSAGE Specify human readable reason for system shutdown\n"
275 " --no-reload Don't reload daemon after en-/dis-abling unit files\n"
276 " --legend=BOOL Enable/disable the legend (column headers and hints)\n"
277 " --no-pager Do not pipe output into a pager\n"
278 " --no-ask-password Do not ask for system passwords\n"
279 " --global Edit/enable/disable/mask default user unit files\n"
280 " globally\n"
281 " --runtime Edit/enable/disable/mask unit files temporarily until\n"
282 " next reboot\n"
283 " -f --force When enabling unit files, override existing symlinks\n"
284 " When shutting down, execute action immediately\n"
285 " --preset-mode= Apply only enable, only disable, or all presets\n"
286 " --root=PATH Edit/enable/disable/mask unit files in the specified\n"
287 " root directory\n"
288 " --image=PATH Edit/enable/disable/mask unit files in the specified\n"
289 " disk image\n"
290 " --image-policy=POLICY\n"
291 " Specify disk image dissection policy\n"
292 " -n --lines=INTEGER Number of journal entries to show\n"
293 " -o --output=STRING Change journal output mode (short, short-precise,\n"
294 " short-iso, short-iso-precise, short-full,\n"
295 " short-monotonic, short-unix, short-delta,\n"
296 " verbose, export, json, json-pretty, json-sse, cat)\n"
297 " --firmware-setup Tell the firmware to show the setup menu on next boot\n"
298 " --boot-loader-menu=TIME\n"
299 " Boot into boot loader menu on next boot\n"
300 " --boot-loader-entry=NAME\n"
301 " Boot into a specific boot loader entry on next boot\n"
302 " --reboot-argument=ARG\n"
303 " Specify argument string to pass to reboot()\n"
304 " --plain Print unit dependencies as a list instead of a tree\n"
305 " --timestamp=FORMAT Change format of printed timestamps (pretty, unix,\n"
306 " us, utc, us+utc)\n"
307 " --read-only Create read-only bind mount\n"
308 " --mkdir Create directory before mounting, if missing\n"
309 " --marked Restart/reload previously marked units\n"
310 " --drop-in=NAME Edit unit files using the specified drop-in file name\n"
311 " --when=TIME Schedule halt/power-off/reboot/kexec action after\n"
312 " a certain timestamp\n"
313 " --stdin Read new contents of edited file from stdin\n"
314 "\nSee the %2$s for details.\n",
315 program_invocation_short_name,
316 link,
317 ansi_underline(),
318 ansi_normal(),
319 ansi_highlight(),
320 ansi_normal());
321
322 return 0;
323 }
324
325 static void help_types(void) {
326 if (arg_legend != 0)
327 puts("Available unit types:");
328
329 DUMP_STRING_TABLE(unit_type, UnitType, _UNIT_TYPE_MAX);
330 }
331
332 static void help_states(void) {
333 if (arg_legend != 0)
334 puts("Available unit load states:");
335 DUMP_STRING_TABLE(unit_load_state, UnitLoadState, _UNIT_LOAD_STATE_MAX);
336
337 if (arg_legend != 0)
338 puts("\nAvailable unit active states:");
339 DUMP_STRING_TABLE(unit_active_state, UnitActiveState, _UNIT_ACTIVE_STATE_MAX);
340
341 if (arg_legend != 0)
342 puts("\nAvailable unit file states:");
343 DUMP_STRING_TABLE(unit_file_state, UnitFileState, _UNIT_FILE_STATE_MAX);
344
345 if (arg_legend != 0)
346 puts("\nAvailable automount unit substates:");
347 DUMP_STRING_TABLE(automount_state, AutomountState, _AUTOMOUNT_STATE_MAX);
348
349 if (arg_legend != 0)
350 puts("\nAvailable device unit substates:");
351 DUMP_STRING_TABLE(device_state, DeviceState, _DEVICE_STATE_MAX);
352
353 if (arg_legend != 0)
354 puts("\nAvailable mount unit substates:");
355 DUMP_STRING_TABLE(mount_state, MountState, _MOUNT_STATE_MAX);
356
357 if (arg_legend != 0)
358 puts("\nAvailable path unit substates:");
359 DUMP_STRING_TABLE(path_state, PathState, _PATH_STATE_MAX);
360
361 if (arg_legend != 0)
362 puts("\nAvailable scope unit substates:");
363 DUMP_STRING_TABLE(scope_state, ScopeState, _SCOPE_STATE_MAX);
364
365 if (arg_legend != 0)
366 puts("\nAvailable service unit substates:");
367 DUMP_STRING_TABLE(service_state, ServiceState, _SERVICE_STATE_MAX);
368
369 if (arg_legend != 0)
370 puts("\nAvailable slice unit substates:");
371 DUMP_STRING_TABLE(slice_state, SliceState, _SLICE_STATE_MAX);
372
373 if (arg_legend != 0)
374 puts("\nAvailable socket unit substates:");
375 DUMP_STRING_TABLE(socket_state, SocketState, _SOCKET_STATE_MAX);
376
377 if (arg_legend != 0)
378 puts("\nAvailable swap unit substates:");
379 DUMP_STRING_TABLE(swap_state, SwapState, _SWAP_STATE_MAX);
380
381 if (arg_legend != 0)
382 puts("\nAvailable target unit substates:");
383 DUMP_STRING_TABLE(target_state, TargetState, _TARGET_STATE_MAX);
384
385 if (arg_legend != 0)
386 puts("\nAvailable timer unit substates:");
387 DUMP_STRING_TABLE(timer_state, TimerState, _TIMER_STATE_MAX);
388 }
389
390 static int systemctl_parse_argv(int argc, char *argv[]) {
391 enum {
392 ARG_FAIL = 0x100, /* compatibility only */
393 ARG_REVERSE,
394 ARG_AFTER,
395 ARG_BEFORE,
396 ARG_CHECK_INHIBITORS,
397 ARG_DRY_RUN,
398 ARG_SHOW_TYPES,
399 ARG_IRREVERSIBLE, /* compatibility only */
400 ARG_IGNORE_DEPENDENCIES, /* compatibility only */
401 ARG_VALUE,
402 ARG_VERSION,
403 ARG_USER,
404 ARG_SYSTEM,
405 ARG_GLOBAL,
406 ARG_NO_BLOCK,
407 ARG_LEGEND,
408 ARG_NO_LEGEND, /* compatibility only */
409 ARG_NO_PAGER,
410 ARG_NO_WALL,
411 ARG_ROOT,
412 ARG_IMAGE,
413 ARG_IMAGE_POLICY,
414 ARG_NO_RELOAD,
415 ARG_KILL_WHOM,
416 ARG_KILL_VALUE,
417 ARG_NO_ASK_PASSWORD,
418 ARG_FAILED,
419 ARG_RUNTIME,
420 ARG_PLAIN,
421 ARG_STATE,
422 ARG_JOB_MODE,
423 ARG_PRESET_MODE,
424 ARG_FIRMWARE_SETUP,
425 ARG_BOOT_LOADER_MENU,
426 ARG_BOOT_LOADER_ENTRY,
427 ARG_NOW,
428 ARG_MESSAGE,
429 ARG_WITH_DEPENDENCIES,
430 ARG_WAIT,
431 ARG_WHAT,
432 ARG_REBOOT_ARG,
433 ARG_TIMESTAMP_STYLE,
434 ARG_READ_ONLY,
435 ARG_MKDIR,
436 ARG_MARKED,
437 ARG_NO_WARN,
438 ARG_DROP_IN,
439 ARG_WHEN,
440 ARG_STDIN,
441 };
442
443 static const struct option options[] = {
444 { "help", no_argument, NULL, 'h' },
445 { "version", no_argument, NULL, ARG_VERSION },
446 { "type", required_argument, NULL, 't' },
447 { "property", required_argument, NULL, 'p' },
448 { "all", no_argument, NULL, 'a' },
449 { "reverse", no_argument, NULL, ARG_REVERSE },
450 { "after", no_argument, NULL, ARG_AFTER },
451 { "before", no_argument, NULL, ARG_BEFORE },
452 { "show-types", no_argument, NULL, ARG_SHOW_TYPES },
453 { "failed", no_argument, NULL, ARG_FAILED },
454 { "full", no_argument, NULL, 'l' },
455 { "job-mode", required_argument, NULL, ARG_JOB_MODE },
456 { "fail", no_argument, NULL, ARG_FAIL }, /* compatibility only */
457 { "irreversible", no_argument, NULL, ARG_IRREVERSIBLE }, /* compatibility only */
458 { "ignore-dependencies", no_argument, NULL, ARG_IGNORE_DEPENDENCIES }, /* compatibility only */
459 { "ignore-inhibitors", no_argument, NULL, 'i' }, /* compatibility only */
460 { "check-inhibitors", required_argument, NULL, ARG_CHECK_INHIBITORS },
461 { "value", no_argument, NULL, ARG_VALUE },
462 { "user", no_argument, NULL, ARG_USER },
463 { "system", no_argument, NULL, ARG_SYSTEM },
464 { "global", no_argument, NULL, ARG_GLOBAL },
465 { "capsule", required_argument, NULL, 'C' },
466 { "wait", no_argument, NULL, ARG_WAIT },
467 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
468 { "legend", required_argument, NULL, ARG_LEGEND },
469 { "no-legend", no_argument, NULL, ARG_NO_LEGEND }, /* compatibility only */
470 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
471 { "no-wall", no_argument, NULL, ARG_NO_WALL },
472 { "dry-run", no_argument, NULL, ARG_DRY_RUN },
473 { "quiet", no_argument, NULL, 'q' },
474 { "verbose", no_argument, NULL, 'v' },
475 { "no-warn", no_argument, NULL, ARG_NO_WARN },
476 { "root", required_argument, NULL, ARG_ROOT },
477 { "image", required_argument, NULL, ARG_IMAGE },
478 { "image-policy", required_argument, NULL, ARG_IMAGE_POLICY },
479 { "force", no_argument, NULL, 'f' },
480 { "no-reload", no_argument, NULL, ARG_NO_RELOAD },
481 { "kill-whom", required_argument, NULL, ARG_KILL_WHOM },
482 { "kill-value", required_argument, NULL, ARG_KILL_VALUE },
483 { "signal", required_argument, NULL, 's' },
484 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
485 { "host", required_argument, NULL, 'H' },
486 { "machine", required_argument, NULL, 'M' },
487 { "runtime", no_argument, NULL, ARG_RUNTIME },
488 { "lines", required_argument, NULL, 'n' },
489 { "output", required_argument, NULL, 'o' },
490 { "plain", no_argument, NULL, ARG_PLAIN },
491 { "state", required_argument, NULL, ARG_STATE },
492 { "recursive", no_argument, NULL, 'r' },
493 { "with-dependencies", no_argument, NULL, ARG_WITH_DEPENDENCIES },
494 { "preset-mode", required_argument, NULL, ARG_PRESET_MODE },
495 { "firmware-setup", no_argument, NULL, ARG_FIRMWARE_SETUP },
496 { "boot-loader-menu", required_argument, NULL, ARG_BOOT_LOADER_MENU },
497 { "boot-loader-entry", required_argument, NULL, ARG_BOOT_LOADER_ENTRY },
498 { "now", no_argument, NULL, ARG_NOW },
499 { "message", required_argument, NULL, ARG_MESSAGE },
500 { "show-transaction", no_argument, NULL, 'T' },
501 { "what", required_argument, NULL, ARG_WHAT },
502 { "reboot-argument", required_argument, NULL, ARG_REBOOT_ARG },
503 { "timestamp", required_argument, NULL, ARG_TIMESTAMP_STYLE },
504 { "read-only", no_argument, NULL, ARG_READ_ONLY },
505 { "mkdir", no_argument, NULL, ARG_MKDIR },
506 { "marked", no_argument, NULL, ARG_MARKED },
507 { "drop-in", required_argument, NULL, ARG_DROP_IN },
508 { "when", required_argument, NULL, ARG_WHEN },
509 { "stdin", no_argument, NULL, ARG_STDIN },
510 {}
511 };
512
513 int c, r;
514
515 assert(argc >= 0);
516 assert(argv);
517
518 /* We default to allowing interactive authorization only in systemctl (not in the legacy commands) */
519 arg_ask_password = true;
520
521 while ((c = getopt_long(argc, argv, "hC:t:p:P:alqvfs:H:M:n:o:iTr.::", options, NULL)) >= 0)
522
523 switch (c) {
524
525 case 'h':
526 return systemctl_help();
527
528 case ARG_VERSION:
529 return version();
530
531 case 't':
532 if (isempty(optarg))
533 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
534 "--type= requires arguments.");
535
536 for (const char *p = optarg;;) {
537 _cleanup_free_ char *type = NULL;
538
539 r = extract_first_word(&p, &type, ",", 0);
540 if (r < 0)
541 return log_error_errno(r, "Failed to parse type: %s", optarg);
542 if (r == 0)
543 break;
544
545 if (streq(type, "help")) {
546 help_types();
547 return 0;
548 }
549
550 if (unit_type_from_string(type) >= 0) {
551 if (strv_consume(&arg_types, TAKE_PTR(type)) < 0)
552 return log_oom();
553 continue;
554 }
555
556 /* It's much nicer to use --state= for load states, but let's support this in
557 * --types= too for compatibility with old versions */
558 if (unit_load_state_from_string(type) >= 0) {
559 if (strv_consume(&arg_states, TAKE_PTR(type)) < 0)
560 return log_oom();
561 continue;
562 }
563
564 log_error("Unknown unit type or load state '%s'.", type);
565 return log_info_errno(SYNTHETIC_ERRNO(EINVAL),
566 "Use -t help to see a list of allowed values.");
567 }
568
569 break;
570
571 case 'P':
572 SET_FLAG(arg_print_flags, BUS_PRINT_PROPERTY_ONLY_VALUE, true);
573 _fallthrough_;
574
575 case 'p':
576 /* Make sure that if the empty property list was specified, we won't show any
577 properties. */
578 if (isempty(optarg) && !arg_properties) {
579 arg_properties = new0(char*, 1);
580 if (!arg_properties)
581 return log_oom();
582 } else
583 for (const char *p = optarg;;) {
584 _cleanup_free_ char *prop = NULL;
585
586 r = extract_first_word(&p, &prop, ",", 0);
587 if (r < 0)
588 return log_error_errno(r, "Failed to parse property: %s", optarg);
589 if (r == 0)
590 break;
591
592 if (strv_consume(&arg_properties, TAKE_PTR(prop)) < 0)
593 return log_oom();
594 }
595
596 /* If the user asked for a particular property, show it, even if it is empty. */
597 SET_FLAG(arg_print_flags, BUS_PRINT_PROPERTY_SHOW_EMPTY, true);
598
599 break;
600
601 case 'a':
602 SET_FLAG(arg_print_flags, BUS_PRINT_PROPERTY_SHOW_EMPTY, true);
603 arg_all = true;
604 break;
605
606 case ARG_REVERSE:
607 arg_dependency = DEPENDENCY_REVERSE;
608 break;
609
610 case ARG_AFTER:
611 arg_dependency = DEPENDENCY_AFTER;
612 arg_jobs_after = true;
613 break;
614
615 case ARG_BEFORE:
616 arg_dependency = DEPENDENCY_BEFORE;
617 arg_jobs_before = true;
618 break;
619
620 case ARG_SHOW_TYPES:
621 arg_show_types = true;
622 break;
623
624 case ARG_VALUE:
625 SET_FLAG(arg_print_flags, BUS_PRINT_PROPERTY_ONLY_VALUE, true);
626 break;
627
628 case ARG_JOB_MODE:
629 _arg_job_mode = optarg;
630 break;
631
632 case ARG_FAIL:
633 _arg_job_mode = "fail";
634 break;
635
636 case ARG_IRREVERSIBLE:
637 _arg_job_mode = "replace-irreversibly";
638 break;
639
640 case ARG_IGNORE_DEPENDENCIES:
641 _arg_job_mode = "ignore-dependencies";
642 break;
643
644 case ARG_USER:
645 arg_runtime_scope = RUNTIME_SCOPE_USER;
646 break;
647
648 case ARG_SYSTEM:
649 arg_runtime_scope = RUNTIME_SCOPE_SYSTEM;
650 break;
651
652 case ARG_GLOBAL:
653 arg_runtime_scope = RUNTIME_SCOPE_GLOBAL;
654 break;
655
656 case 'C':
657 r = capsule_name_is_valid(optarg);
658 if (r < 0)
659 return log_error_errno(r, "Unable to validate capsule name '%s': %m", optarg);
660 if (r == 0)
661 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid capsule name: %s", optarg);
662
663 arg_host = optarg;
664 arg_transport = BUS_TRANSPORT_CAPSULE;
665 arg_runtime_scope = RUNTIME_SCOPE_USER;
666 break;
667
668 case ARG_WAIT:
669 arg_wait = true;
670 break;
671
672 case ARG_NO_BLOCK:
673 arg_no_block = true;
674 break;
675
676 case ARG_NO_LEGEND:
677 arg_legend = false;
678 break;
679
680 case ARG_LEGEND:
681 r = parse_boolean_argument("--legend", optarg, NULL);
682 if (r < 0)
683 return r;
684 arg_legend = r;
685 break;
686
687 case ARG_NO_PAGER:
688 arg_pager_flags |= PAGER_DISABLE;
689 break;
690
691 case ARG_NO_WALL:
692 arg_no_wall = true;
693 break;
694
695 case ARG_ROOT:
696 r = parse_path_argument(optarg, false, &arg_root);
697 if (r < 0)
698 return r;
699 break;
700
701 case ARG_IMAGE:
702 r = parse_path_argument(optarg, false, &arg_image);
703 if (r < 0)
704 return r;
705 break;
706
707 case ARG_IMAGE_POLICY:
708 r = parse_image_policy_argument(optarg, &arg_image_policy);
709 if (r < 0)
710 return r;
711 break;
712
713 case 'l':
714 arg_full = true;
715 break;
716
717 case ARG_FAILED:
718 if (strv_extend(&arg_states, "failed") < 0)
719 return log_oom();
720
721 break;
722
723 case ARG_DRY_RUN:
724 arg_dry_run = true;
725 break;
726
727 case 'q':
728 arg_quiet = true;
729
730 if (arg_legend < 0)
731 arg_legend = false;
732
733 break;
734
735 case 'v':
736 arg_verbose = true;
737 break;
738
739 case 'f':
740 arg_force++;
741 break;
742
743 case ARG_NO_RELOAD:
744 arg_no_reload = true;
745 break;
746
747 case ARG_KILL_WHOM:
748 arg_kill_whom = optarg;
749 break;
750
751 case ARG_KILL_VALUE: {
752 unsigned u;
753
754 if (isempty(optarg)) {
755 arg_kill_value_set = false;
756 return 0;
757 }
758
759 /* First, try to parse unsigned, so that we can support the prefixes 0x, 0o, 0b */
760 r = safe_atou_full(optarg, 0, &u);
761 if (r < 0)
762 /* If this didn't work, try as signed integer, without those prefixes */
763 r = safe_atoi(optarg, &arg_kill_value);
764 else if (u > INT_MAX)
765 r = -ERANGE;
766 else
767 arg_kill_value = (int) u;
768 if (r < 0)
769 return log_error_errno(r, "Unable to parse signal queue value: %s", optarg);
770
771 arg_kill_value_set = true;
772 break;
773 }
774
775 case 's':
776 r = parse_signal_argument(optarg, &arg_signal);
777 if (r <= 0)
778 return r;
779 break;
780
781 case ARG_NO_ASK_PASSWORD:
782 arg_ask_password = false;
783 break;
784
785 case 'H':
786 arg_transport = BUS_TRANSPORT_REMOTE;
787 arg_host = optarg;
788 break;
789
790 case 'M':
791 r = parse_machine_argument(optarg, &arg_host, &arg_transport);
792 if (r < 0)
793 return r;
794 break;
795
796 case ARG_RUNTIME:
797 arg_runtime = true;
798 break;
799
800 case 'n':
801 if (safe_atou(optarg, &arg_lines) < 0)
802 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
803 "Failed to parse lines '%s'",
804 optarg);
805 break;
806
807 case 'o':
808 if (streq(optarg, "help")) {
809 DUMP_STRING_TABLE(output_mode, OutputMode, _OUTPUT_MODE_MAX);
810 return 0;
811 }
812
813 arg_output = output_mode_from_string(optarg);
814 if (arg_output < 0)
815 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
816 "Unknown output '%s'.",
817 optarg);
818
819 if (OUTPUT_MODE_IS_JSON(arg_output)) {
820 arg_legend = false;
821 arg_plain = true;
822 }
823 break;
824
825 case 'i':
826 arg_check_inhibitors = 0;
827 break;
828
829 case ARG_CHECK_INHIBITORS:
830 r = parse_tristate_full(optarg, "auto", &arg_check_inhibitors);
831 if (r < 0)
832 return log_error_errno(r, "Failed to parse --check-inhibitors= argument: %s", optarg);
833 break;
834
835 case ARG_PLAIN:
836 arg_plain = true;
837 break;
838
839 case ARG_FIRMWARE_SETUP:
840 arg_firmware_setup = true;
841 break;
842
843 case ARG_BOOT_LOADER_MENU:
844
845 r = parse_sec(optarg, &arg_boot_loader_menu);
846 if (r < 0)
847 return log_error_errno(r, "Failed to parse --boot-loader-menu= argument '%s': %m", optarg);
848
849 break;
850
851 case ARG_BOOT_LOADER_ENTRY:
852
853 if (streq(optarg, "help")) { /* Yes, this means, "help" is not a valid boot loader entry name we can deal with */
854 r = help_boot_loader_entry();
855 if (r < 0)
856 return r;
857
858 return 0;
859 }
860
861 arg_boot_loader_entry = empty_to_null(optarg);
862 break;
863
864 case ARG_STATE:
865 if (isempty(optarg))
866 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
867 "--state= requires arguments.");
868
869 for (const char *p = optarg;;) {
870 _cleanup_free_ char *s = NULL;
871
872 r = extract_first_word(&p, &s, ",", 0);
873 if (r < 0)
874 return log_error_errno(r, "Failed to parse state: %s", optarg);
875 if (r == 0)
876 break;
877
878 if (streq(s, "help")) {
879 help_states();
880 return 0;
881 }
882
883 if (strv_consume(&arg_states, TAKE_PTR(s)) < 0)
884 return log_oom();
885 }
886 break;
887
888 case 'r':
889 if (geteuid() != 0)
890 return log_error_errno(SYNTHETIC_ERRNO(EPERM),
891 "--recursive requires root privileges.");
892
893 arg_recursive = true;
894 break;
895
896 case ARG_PRESET_MODE:
897 if (streq(optarg, "help")) {
898 DUMP_STRING_TABLE(unit_file_preset_mode, UnitFilePresetMode, _UNIT_FILE_PRESET_MODE_MAX);
899 return 0;
900 }
901
902 arg_preset_mode = unit_file_preset_mode_from_string(optarg);
903 if (arg_preset_mode < 0)
904 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
905 "Failed to parse preset mode: %s.", optarg);
906
907 break;
908
909 case ARG_NOW:
910 arg_now = true;
911 break;
912
913 case ARG_MESSAGE:
914 if (strv_extend(&arg_wall, optarg) < 0)
915 return log_oom();
916 break;
917
918 case 'T':
919 arg_show_transaction = true;
920 break;
921
922 case ARG_WITH_DEPENDENCIES:
923 arg_with_dependencies = true;
924 break;
925
926 case ARG_WHAT:
927 if (isempty(optarg))
928 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--what= requires arguments (see --what=help).");
929
930 for (const char *p = optarg;;) {
931 _cleanup_free_ char *k = NULL;
932
933 r = extract_first_word(&p, &k, ",", 0);
934 if (r < 0)
935 return log_error_errno(r, "Failed to parse directory type: %s", optarg);
936 if (r == 0)
937 break;
938
939 if (streq(k, "help")) {
940 puts("runtime\n"
941 "state\n"
942 "cache\n"
943 "logs\n"
944 "configuration\n"
945 "fdstore\n"
946 "all");
947 return 0;
948 }
949
950 r = strv_consume(&arg_clean_what, TAKE_PTR(k));
951 if (r < 0)
952 return log_oom();
953 }
954
955 break;
956
957 case ARG_REBOOT_ARG:
958 arg_reboot_argument = optarg;
959 break;
960
961 case ARG_TIMESTAMP_STYLE:
962 if (streq(optarg, "help")) {
963 DUMP_STRING_TABLE(timestamp_style, TimestampStyle, _TIMESTAMP_STYLE_MAX);
964 return 0;
965 }
966
967 arg_timestamp_style = timestamp_style_from_string(optarg);
968 if (arg_timestamp_style < 0)
969 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
970 "Invalid value: %s.", optarg);
971
972 break;
973
974 case ARG_READ_ONLY:
975 arg_read_only = true;
976 break;
977
978 case ARG_MKDIR:
979 arg_mkdir = true;
980 break;
981
982 case ARG_MARKED:
983 arg_marked = true;
984 break;
985
986 case ARG_NO_WARN:
987 arg_no_warn = true;
988 break;
989
990 case ARG_DROP_IN:
991 arg_drop_in = optarg;
992 break;
993
994 case ARG_WHEN:
995 if (streq(optarg, "show")) {
996 arg_action = ACTION_SYSTEMCTL_SHOW_SHUTDOWN;
997 return 1;
998 }
999
1000 if (STR_IN_SET(optarg, "", "cancel")) {
1001 arg_action = ACTION_CANCEL_SHUTDOWN;
1002 return 1;
1003 }
1004
1005 if (streq(optarg, "auto")) {
1006 arg_when = USEC_INFINITY; /* logind chooses on server side */
1007 break;
1008 }
1009
1010 r = parse_timestamp(optarg, &arg_when);
1011 if (r < 0)
1012 return log_error_errno(r, "Failed to parse --when= argument '%s': %m", optarg);
1013
1014 if (!timestamp_is_set(arg_when))
1015 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1016 "Invalid timestamp '%s' specified for --when=.", optarg);
1017
1018 break;
1019
1020 case ARG_STDIN:
1021 arg_stdin = true;
1022 break;
1023
1024 case '.':
1025 /* Output an error mimicking getopt, and print a hint afterwards */
1026 log_error("%s: invalid option -- '.'", program_invocation_name);
1027 log_notice("Hint: to specify units starting with a dash, use \"--\":\n"
1028 " %s [OPTIONS...] COMMAND -- -.%s ...",
1029 program_invocation_name, optarg ?: "mount");
1030 _fallthrough_;
1031
1032 case '?':
1033 return -EINVAL;
1034
1035 default:
1036 assert_not_reached();
1037 }
1038
1039 /* If we are in --user mode, there's no point in talking to PolicyKit or the infra to query system
1040 * passwords */
1041 if (arg_runtime_scope != RUNTIME_SCOPE_SYSTEM)
1042 arg_ask_password = false;
1043
1044 if (arg_transport == BUS_TRANSPORT_REMOTE && arg_runtime_scope != RUNTIME_SCOPE_SYSTEM)
1045 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1046 "Cannot access user instance remotely.");
1047
1048 if (arg_transport == BUS_TRANSPORT_CAPSULE && arg_runtime_scope != RUNTIME_SCOPE_USER)
1049 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1050 "Cannot access system instance with --capsule=/-C.");
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 arg_action = _ACTION_INVALID; /* telinit_parse_argv() will figure out the actual action we'll execute */
1120 return telinit_parse_argv(argc, argv);
1121
1122 } else if (invoked_as(argv, "runlevel")) {
1123 arg_action = ACTION_RUNLEVEL;
1124 return runlevel_parse_argv(argc, argv);
1125 }
1126
1127 arg_action = ACTION_SYSTEMCTL;
1128 return systemctl_parse_argv(argc, argv);
1129 }