1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
11 #include "main-func.h"
12 #include "output-mode.h"
14 #include "parse-argument.h"
15 #include "path-util.h"
16 #include "pretty-print.h"
17 #include "rlimit-util.h"
19 #include "signal-util.h"
20 #include "string-table.h"
21 #include "systemctl-add-dependency.h"
22 #include "systemctl-cancel-job.h"
23 #include "systemctl-clean-or-freeze.h"
24 #include "systemctl-compat-halt.h"
25 #include "systemctl-compat-runlevel.h"
26 #include "systemctl-compat-shutdown.h"
27 #include "systemctl-compat-telinit.h"
28 #include "systemctl-daemon-reload.h"
29 #include "systemctl-edit.h"
30 #include "systemctl-enable.h"
31 #include "systemctl-is-active.h"
32 #include "systemctl-is-enabled.h"
33 #include "systemctl-is-system-running.h"
34 #include "systemctl-kill.h"
35 #include "systemctl-list-dependencies.h"
36 #include "systemctl-list-jobs.h"
37 #include "systemctl-list-machines.h"
38 #include "systemctl-list-unit-files.h"
39 #include "systemctl-list-units.h"
40 #include "systemctl-log-setting.h"
41 #include "systemctl-logind.h"
42 #include "systemctl-mount.h"
43 #include "systemctl-preset-all.h"
44 #include "systemctl-reset-failed.h"
45 #include "systemctl-service-watchdogs.h"
46 #include "systemctl-set-default.h"
47 #include "systemctl-set-environment.h"
48 #include "systemctl-set-property.h"
49 #include "systemctl-show.h"
50 #include "systemctl-start-special.h"
51 #include "systemctl-start-unit.h"
52 #include "systemctl-switch-root.h"
53 #include "systemctl-sysv-compat.h"
54 #include "systemctl-trivial-method.h"
55 #include "systemctl-util.h"
56 #include "systemctl.h"
57 #include "terminal-util.h"
58 #include "time-util.h"
62 char **arg_types
= NULL
;
63 char **arg_states
= NULL
;
64 char **arg_properties
= NULL
;
66 enum dependency arg_dependency
= DEPENDENCY_FORWARD
;
67 const char *arg_job_mode
= "replace";
68 UnitFileScope arg_scope
= UNIT_FILE_SYSTEM
;
69 bool arg_wait
= false;
70 bool arg_no_block
= false;
71 bool arg_no_legend
= false;
72 PagerFlags arg_pager_flags
= 0;
73 bool arg_no_wtmp
= false;
74 bool arg_no_sync
= false;
75 bool arg_no_wall
= false;
76 bool arg_no_reload
= false;
77 bool arg_value
= false;
78 bool arg_show_types
= false;
79 int arg_check_inhibitors
= -1;
80 bool arg_dry_run
= false;
81 bool arg_quiet
= false;
82 bool arg_full
= false;
83 bool arg_recursive
= false;
84 bool arg_with_dependencies
= false;
85 bool arg_show_transaction
= false;
87 bool arg_ask_password
= false;
88 bool arg_runtime
= false;
89 UnitFilePresetMode arg_preset_mode
= UNIT_FILE_PRESET_FULL
;
90 char **arg_wall
= NULL
;
91 const char *arg_kill_who
= NULL
;
92 int arg_signal
= SIGTERM
;
93 char *arg_root
= NULL
;
95 const char *arg_reboot_argument
= NULL
;
96 enum action arg_action
= ACTION_SYSTEMCTL
;
97 BusTransport arg_transport
= BUS_TRANSPORT_LOCAL
;
98 const char *arg_host
= NULL
;
99 unsigned arg_lines
= 10;
100 OutputMode arg_output
= OUTPUT_SHORT
;
101 bool arg_plain
= false;
102 bool arg_firmware_setup
= false;
103 usec_t arg_boot_loader_menu
= USEC_INFINITY
;
104 const char *arg_boot_loader_entry
= NULL
;
105 bool arg_now
= false;
106 bool arg_jobs_before
= false;
107 bool arg_jobs_after
= false;
108 char **arg_clean_what
= NULL
;
109 TimestampStyle arg_timestamp_style
= TIMESTAMP_PRETTY
;
110 bool arg_read_only
= false;
111 bool arg_mkdir
= false;
112 bool arg_marked
= false;
114 STATIC_DESTRUCTOR_REGISTER(arg_wall
, strv_freep
);
115 STATIC_DESTRUCTOR_REGISTER(arg_root
, freep
);
116 STATIC_DESTRUCTOR_REGISTER(arg_types
, strv_freep
);
117 STATIC_DESTRUCTOR_REGISTER(arg_states
, strv_freep
);
118 STATIC_DESTRUCTOR_REGISTER(arg_properties
, strv_freep
);
119 STATIC_DESTRUCTOR_REGISTER(arg_clean_what
, strv_freep
);
121 static int systemctl_help(void) {
122 _cleanup_free_
char *link
= NULL
;
125 (void) pager_open(arg_pager_flags
);
127 r
= terminal_urlify_man("systemctl", "1", &link
);
131 printf("%1$s [OPTIONS...] COMMAND ...\n\n"
132 "%5$sQuery or send control commands to the system manager.%6$s\n"
133 "\n%3$sUnit Commands:%4$s\n"
134 " list-units [PATTERN...] List units currently in memory\n"
135 " list-sockets [PATTERN...] List socket units currently in memory,\n"
136 " ordered by address\n"
137 " list-timers [PATTERN...] List timer units currently in memory,\n"
138 " ordered by next elapse\n"
139 " is-active PATTERN... Check whether units are active\n"
140 " is-failed PATTERN... Check whether units are failed\n"
141 " status [PATTERN...|PID...] Show runtime status of one or more units\n"
142 " show [PATTERN...|JOB...] Show properties of one or more\n"
143 " units/jobs or the manager\n"
144 " cat PATTERN... Show files and drop-ins of specified units\n"
145 " help PATTERN...|PID... Show manual for one or more units\n"
146 " list-dependencies [UNIT...] Recursively show units which are required\n"
147 " or wanted by the units or by which those\n"
148 " units are required or wanted\n"
149 " start UNIT... Start (activate) one or more units\n"
150 " stop UNIT... Stop (deactivate) one or more units\n"
151 " reload UNIT... Reload one or more units\n"
152 " restart UNIT... Start or restart one or more units\n"
153 " try-restart UNIT... Restart one or more units if active\n"
154 " reload-or-restart UNIT... Reload one or more units if possible,\n"
155 " otherwise start or restart\n"
156 " try-reload-or-restart UNIT... If active, reload one or more units,\n"
157 " if supported, otherwise restart\n"
158 " isolate UNIT Start one unit and stop all others\n"
159 " kill UNIT... Send signal to processes of a unit\n"
160 " clean UNIT... Clean runtime, cache, state, logs or\n"
161 " configuration of unit\n"
162 " freeze PATTERN... Freeze execution of unit processes\n"
163 " thaw PATTERN... Resume execution of a frozen unit\n"
164 " set-property UNIT PROPERTY=VALUE... Sets one or more properties of a unit\n"
165 " bind UNIT PATH [PATH] Bind-mount a path from the host into a\n"
166 " unit's namespace\n"
167 " mount-image UNIT PATH [PATH [OPTS]] Mount an image from the host into a\n"
168 " unit's namespace\n"
169 " service-log-level SERVICE [LEVEL] Get/set logging threshold for service\n"
170 " service-log-target SERVICE [TARGET] Get/set logging target for service\n"
171 " reset-failed [PATTERN...] Reset failed state for all, one, or more\n"
173 "\n%3$sUnit File Commands:%4$s\n"
174 " list-unit-files [PATTERN...] List installed unit files\n"
175 " enable [UNIT...|PATH...] Enable one or more unit files\n"
176 " disable UNIT... Disable one or more unit files\n"
177 " reenable UNIT... Reenable one or more unit files\n"
178 " preset UNIT... Enable/disable one or more unit files\n"
179 " based on preset configuration\n"
180 " preset-all Enable/disable all unit files based on\n"
181 " preset configuration\n"
182 " is-enabled UNIT... Check whether unit files are enabled\n"
183 " mask UNIT... Mask one or more units\n"
184 " unmask UNIT... Unmask one or more units\n"
185 " link PATH... Link one or more units files into\n"
187 " revert UNIT... Revert one or more unit files to vendor\n"
189 " add-wants TARGET UNIT... Add 'Wants' dependency for the target\n"
190 " on specified one or more units\n"
191 " add-requires TARGET UNIT... Add 'Requires' dependency for the target\n"
192 " on specified one or more units\n"
193 " edit UNIT... Edit one or more unit files\n"
194 " get-default Get the name of the default target\n"
195 " set-default TARGET Set the default target\n"
196 "\n%3$sMachine Commands:%4$s\n"
197 " list-machines [PATTERN...] List local containers and host\n"
198 "\n%3$sJob Commands:%4$s\n"
199 " list-jobs [PATTERN...] List jobs\n"
200 " cancel [JOB...] Cancel all, one, or more jobs\n"
201 "\n%3$sEnvironment Commands:%4$s\n"
202 " show-environment Dump environment\n"
203 " set-environment VARIABLE=VALUE... Set one or more environment variables\n"
204 " unset-environment VARIABLE... Unset one or more environment variables\n"
205 " import-environment VARIABLE... Import all or some environment variables\n"
206 "\n%3$sManager State Commands:%4$s\n"
207 " daemon-reload Reload systemd manager configuration\n"
208 " daemon-reexec Reexecute systemd manager\n"
209 " log-level [LEVEL] Get/set logging threshold for manager\n"
210 " log-target [TARGET] Get/set logging target for manager\n"
211 " service-watchdogs [BOOL] Get/set service watchdog state\n"
212 "\n%3$sSystem Commands:%4$s\n"
213 " is-system-running Check whether system is fully running\n"
214 " default Enter system default mode\n"
215 " rescue Enter system rescue mode\n"
216 " emergency Enter system emergency mode\n"
217 " halt Shut down and halt the system\n"
218 " poweroff Shut down and power-off the system\n"
219 " reboot Shut down and reboot the system\n"
220 " kexec Shut down and reboot the system with kexec\n"
221 " exit [EXIT_CODE] Request user instance or container exit\n"
222 " switch-root ROOT [INIT] Change to a different root file system\n"
223 " suspend Suspend the system\n"
224 " hibernate Hibernate the system\n"
225 " hybrid-sleep Hibernate and suspend the system\n"
226 " suspend-then-hibernate Suspend the system, wake after a period of\n"
227 " time, and hibernate"
228 "\n%3$sOptions:%4$s\n"
229 " -h --help Show this help\n"
230 " --version Show package version\n"
231 " --system Connect to system manager\n"
232 " --user Connect to user service manager\n"
233 " -H --host=[USER@]HOST Operate on remote host\n"
234 " -M --machine=CONTAINER Operate on a local container\n"
235 " -t --type=TYPE List units of a particular type\n"
236 " --state=STATE List units with particular LOAD or SUB or ACTIVE state\n"
237 " --failed Shortcut for --state=failed\n"
238 " -p --property=NAME Show only properties by this name\n"
239 " -P NAME Equivalent to --value --property=NAME\n"
240 " -a --all Show all properties/all units currently in memory,\n"
241 " including dead/empty ones. To list all units installed\n"
242 " on the system, use 'list-unit-files' instead.\n"
243 " -l --full Don't ellipsize unit names on output\n"
244 " -r --recursive Show unit list of host and local containers\n"
245 " --reverse Show reverse dependencies with 'list-dependencies'\n"
246 " --with-dependencies Show unit dependencies with 'status', 'cat',\n"
247 " 'list-units', and 'list-unit-files'.\n"
248 " --job-mode=MODE Specify how to deal with already queued jobs, when\n"
249 " queueing a new job\n"
250 " -T --show-transaction When enqueuing a unit job, show full transaction\n"
251 " --show-types When showing sockets, explicitly show their type\n"
252 " --value When showing properties, only print the value\n"
253 " --check-inhibitors=MODE\n"
254 " Specify if checking inhibitors before shutting down,\n"
255 " sleeping or hibernating\n"
256 " -i Shortcut for --check-inhibitors=no\n"
257 " --kill-who=WHO Whom to send signal to\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, suspend, hibernate, suspend-then-hibernate,\n"
264 " hybrid-sleep, default, rescue, emergency, and exit.\n"
265 " -q --quiet Suppress output\n"
266 " --wait For (re)start, wait until service stopped again\n"
267 " For is-system-running, wait until startup is completed\n"
268 " --no-block Do not wait until operation finished\n"
269 " --no-wall Don't send wall message before halt/power-off/reboot\n"
270 " --no-reload Don't reload daemon after en-/dis-abling unit files\n"
271 " --no-legend Do not print a legend (column headers and hints)\n"
272 " --no-pager Do not pipe output into a pager\n"
273 " --no-ask-password Do not ask for system passwords\n"
274 " --global Enable/disable/mask unit files globally\n"
275 " --runtime Enable/disable/mask unit files temporarily until next\n"
277 " -f --force When enabling unit files, override existing symlinks\n"
278 " When shutting down, execute action immediately\n"
279 " --preset-mode= Apply only enable, only disable, or all presets\n"
280 " --root=PATH Enable/disable/mask unit files in the specified root\n"
282 " -n --lines=INTEGER Number of journal entries to show\n"
283 " -o --output=STRING Change journal output mode (short, short-precise,\n"
284 " short-iso, short-iso-precise, short-full,\n"
285 " short-monotonic, short-unix,\n"
286 " verbose, export, json, json-pretty, json-sse, cat)\n"
287 " --firmware-setup Tell the firmware to show the setup menu on next boot\n"
288 " --boot-loader-menu=TIME\n"
289 " Boot into boot loader menu on next boot\n"
290 " --boot-loader-entry=NAME\n"
291 " Boot into a specific boot loader entry on next boot\n"
292 " --plain Print unit dependencies as a list instead of a tree\n"
293 " --timestamp=FORMAT Change format of printed timestamps.\n"
294 " 'pretty' (default): 'Day YYYY-MM-DD HH:MM:SS TZ\n"
295 " 'us': 'Day YYYY-MM-DD HH:MM:SS.UUUUUU TZ\n"
296 " 'utc': 'Day YYYY-MM-DD HH:MM:SS UTC\n"
297 " 'us+utc': 'Day YYYY-MM-DD HH:MM:SS.UUUUUU UTC\n"
298 " --read-only Create read-only bind mount\n"
299 " --mkdir Create directory before mounting, if missing\n"
300 " --marked Restart/reload previously marked units\n"
301 "\nSee the %2$s for details.\n",
302 program_invocation_short_name
,
312 static void help_types(void) {
314 puts("Available unit types:");
316 DUMP_STRING_TABLE(unit_type
, UnitType
, _UNIT_TYPE_MAX
);
319 static void help_states(void) {
321 puts("Available unit load states:");
322 DUMP_STRING_TABLE(unit_load_state
, UnitLoadState
, _UNIT_LOAD_STATE_MAX
);
325 puts("\nAvailable unit active states:");
326 DUMP_STRING_TABLE(unit_active_state
, UnitActiveState
, _UNIT_ACTIVE_STATE_MAX
);
329 puts("\nAvailable unit file states:");
330 DUMP_STRING_TABLE(unit_file_state
, UnitFileState
, _UNIT_FILE_STATE_MAX
);
333 puts("\nAvailable automount unit substates:");
334 DUMP_STRING_TABLE(automount_state
, AutomountState
, _AUTOMOUNT_STATE_MAX
);
337 puts("\nAvailable device unit substates:");
338 DUMP_STRING_TABLE(device_state
, DeviceState
, _DEVICE_STATE_MAX
);
341 puts("\nAvailable mount unit substates:");
342 DUMP_STRING_TABLE(mount_state
, MountState
, _MOUNT_STATE_MAX
);
345 puts("\nAvailable path unit substates:");
346 DUMP_STRING_TABLE(path_state
, PathState
, _PATH_STATE_MAX
);
349 puts("\nAvailable scope unit substates:");
350 DUMP_STRING_TABLE(scope_state
, ScopeState
, _SCOPE_STATE_MAX
);
353 puts("\nAvailable service unit substates:");
354 DUMP_STRING_TABLE(service_state
, ServiceState
, _SERVICE_STATE_MAX
);
357 puts("\nAvailable slice unit substates:");
358 DUMP_STRING_TABLE(slice_state
, SliceState
, _SLICE_STATE_MAX
);
361 puts("\nAvailable socket unit substates:");
362 DUMP_STRING_TABLE(socket_state
, SocketState
, _SOCKET_STATE_MAX
);
365 puts("\nAvailable swap unit substates:");
366 DUMP_STRING_TABLE(swap_state
, SwapState
, _SWAP_STATE_MAX
);
369 puts("\nAvailable target unit substates:");
370 DUMP_STRING_TABLE(target_state
, TargetState
, _TARGET_STATE_MAX
);
373 puts("\nAvailable timer unit substates:");
374 DUMP_STRING_TABLE(timer_state
, TimerState
, _TIMER_STATE_MAX
);
377 static int systemctl_parse_argv(int argc
, char *argv
[]) {
383 ARG_CHECK_INHIBITORS
,
387 ARG_IGNORE_DEPENDENCIES
,
408 ARG_BOOT_LOADER_MENU
,
409 ARG_BOOT_LOADER_ENTRY
,
412 ARG_WITH_DEPENDENCIES
,
422 static const struct option options
[] = {
423 { "help", no_argument
, NULL
, 'h' },
424 { "version", no_argument
, NULL
, ARG_VERSION
},
425 { "type", required_argument
, NULL
, 't' },
426 { "property", required_argument
, NULL
, 'p' },
427 { "all", no_argument
, NULL
, 'a' },
428 { "reverse", no_argument
, NULL
, ARG_REVERSE
},
429 { "after", no_argument
, NULL
, ARG_AFTER
},
430 { "before", no_argument
, NULL
, ARG_BEFORE
},
431 { "show-types", no_argument
, NULL
, ARG_SHOW_TYPES
},
432 { "failed", no_argument
, NULL
, ARG_FAILED
},
433 { "full", no_argument
, NULL
, 'l' },
434 { "job-mode", required_argument
, NULL
, ARG_JOB_MODE
},
435 { "fail", no_argument
, NULL
, ARG_FAIL
}, /* compatibility only */
436 { "irreversible", no_argument
, NULL
, ARG_IRREVERSIBLE
}, /* compatibility only */
437 { "ignore-dependencies", no_argument
, NULL
, ARG_IGNORE_DEPENDENCIES
}, /* compatibility only */
438 { "ignore-inhibitors", no_argument
, NULL
, 'i' }, /* compatibility only */
439 { "check-inhibitors", required_argument
, NULL
, ARG_CHECK_INHIBITORS
},
440 { "value", no_argument
, NULL
, ARG_VALUE
},
441 { "user", no_argument
, NULL
, ARG_USER
},
442 { "system", no_argument
, NULL
, ARG_SYSTEM
},
443 { "global", no_argument
, NULL
, ARG_GLOBAL
},
444 { "wait", no_argument
, NULL
, ARG_WAIT
},
445 { "no-block", no_argument
, NULL
, ARG_NO_BLOCK
},
446 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
447 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
448 { "no-wall", no_argument
, NULL
, ARG_NO_WALL
},
449 { "dry-run", no_argument
, NULL
, ARG_DRY_RUN
},
450 { "quiet", no_argument
, NULL
, 'q' },
451 { "root", required_argument
, NULL
, ARG_ROOT
},
452 { "force", no_argument
, NULL
, 'f' },
453 { "no-reload", no_argument
, NULL
, ARG_NO_RELOAD
},
454 { "kill-who", required_argument
, NULL
, ARG_KILL_WHO
},
455 { "signal", required_argument
, NULL
, 's' },
456 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
457 { "host", required_argument
, NULL
, 'H' },
458 { "machine", required_argument
, NULL
, 'M' },
459 { "runtime", no_argument
, NULL
, ARG_RUNTIME
},
460 { "lines", required_argument
, NULL
, 'n' },
461 { "output", required_argument
, NULL
, 'o' },
462 { "plain", no_argument
, NULL
, ARG_PLAIN
},
463 { "state", required_argument
, NULL
, ARG_STATE
},
464 { "recursive", no_argument
, NULL
, 'r' },
465 { "with-dependencies", no_argument
, NULL
, ARG_WITH_DEPENDENCIES
},
466 { "preset-mode", required_argument
, NULL
, ARG_PRESET_MODE
},
467 { "firmware-setup", no_argument
, NULL
, ARG_FIRMWARE_SETUP
},
468 { "boot-loader-menu", required_argument
, NULL
, ARG_BOOT_LOADER_MENU
},
469 { "boot-loader-entry", required_argument
, NULL
, ARG_BOOT_LOADER_ENTRY
},
470 { "now", no_argument
, NULL
, ARG_NOW
},
471 { "message", required_argument
, NULL
, ARG_MESSAGE
},
472 { "show-transaction", no_argument
, NULL
, 'T' },
473 { "what", required_argument
, NULL
, ARG_WHAT
},
474 { "reboot-argument", required_argument
, NULL
, ARG_REBOOT_ARG
},
475 { "timestamp", required_argument
, NULL
, ARG_TIMESTAMP_STYLE
},
476 { "read-only", no_argument
, NULL
, ARG_READ_ONLY
},
477 { "mkdir", no_argument
, NULL
, ARG_MKDIR
},
478 { "marked", no_argument
, NULL
, ARG_MARKED
},
487 /* We default to allowing interactive authorization only in systemctl (not in the legacy commands) */
488 arg_ask_password
= true;
490 while ((c
= getopt_long(argc
, argv
, "ht:p:P:alqfs:H:M:n:o:iTr.::", options
, NULL
)) >= 0)
495 return systemctl_help();
502 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
503 "--type= requires arguments.");
505 for (const char *p
= optarg
;;) {
506 _cleanup_free_
char *type
= NULL
;
508 r
= extract_first_word(&p
, &type
, ",", 0);
510 return log_error_errno(r
, "Failed to parse type: %s", optarg
);
514 if (streq(type
, "help")) {
519 if (unit_type_from_string(type
) >= 0) {
520 if (strv_consume(&arg_types
, TAKE_PTR(type
)) < 0)
525 /* It's much nicer to use --state= for load states, but let's support this in
526 * --types= too for compatibility with old versions */
527 if (unit_load_state_from_string(type
) >= 0) {
528 if (strv_consume(&arg_states
, TAKE_PTR(type
)) < 0)
533 log_error("Unknown unit type or load state '%s'.", type
);
534 return log_info_errno(SYNTHETIC_ERRNO(EINVAL
),
535 "Use -t help to see a list of allowed values.");
545 /* Make sure that if the empty property list was specified, we won't show any
547 if (isempty(optarg
) && !arg_properties
) {
548 arg_properties
= new0(char*, 1);
552 for (const char *p
= optarg
;;) {
553 _cleanup_free_
char *prop
= NULL
;
555 r
= extract_first_word(&p
, &prop
, ",", 0);
557 return log_error_errno(r
, "Failed to parse property: %s", optarg
);
561 if (strv_consume(&arg_properties
, TAKE_PTR(prop
)) < 0)
565 /* If the user asked for a particular property, show it, even if it is empty. */
575 arg_dependency
= DEPENDENCY_REVERSE
;
579 arg_dependency
= DEPENDENCY_AFTER
;
580 arg_jobs_after
= true;
584 arg_dependency
= DEPENDENCY_BEFORE
;
585 arg_jobs_before
= true;
589 arg_show_types
= true;
597 arg_job_mode
= optarg
;
601 arg_job_mode
= "fail";
604 case ARG_IRREVERSIBLE
:
605 arg_job_mode
= "replace-irreversibly";
608 case ARG_IGNORE_DEPENDENCIES
:
609 arg_job_mode
= "ignore-dependencies";
613 arg_scope
= UNIT_FILE_USER
;
617 arg_scope
= UNIT_FILE_SYSTEM
;
621 arg_scope
= UNIT_FILE_GLOBAL
;
633 arg_no_legend
= true;
637 arg_pager_flags
|= PAGER_DISABLE
;
645 r
= parse_path_argument(optarg
, false, &arg_root
);
655 if (strv_extend(&arg_states
, "failed") < 0)
673 arg_no_reload
= true;
677 arg_kill_who
= optarg
;
681 r
= parse_signal_argument(optarg
, &arg_signal
);
686 case ARG_NO_ASK_PASSWORD
:
687 arg_ask_password
= false;
691 arg_transport
= BUS_TRANSPORT_REMOTE
;
696 arg_transport
= BUS_TRANSPORT_MACHINE
;
705 if (safe_atou(optarg
, &arg_lines
) < 0)
706 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
707 "Failed to parse lines '%s'",
712 if (streq(optarg
, "help")) {
713 DUMP_STRING_TABLE(output_mode
, OutputMode
, _OUTPUT_MODE_MAX
);
717 arg_output
= output_mode_from_string(optarg
);
719 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
720 "Unknown output '%s'.",
723 if (OUTPUT_MODE_IS_JSON(arg_output
)) {
724 arg_no_legend
= true;
730 arg_check_inhibitors
= 0;
733 case ARG_CHECK_INHIBITORS
:
734 if (streq(optarg
, "auto"))
735 arg_check_inhibitors
= -1;
737 r
= parse_boolean(optarg
);
739 return log_error_errno(r
, "Failed to parse --check-inhibitors= argument: %s", optarg
);
740 arg_check_inhibitors
= r
;
748 case ARG_FIRMWARE_SETUP
:
749 arg_firmware_setup
= true;
752 case ARG_BOOT_LOADER_MENU
:
754 r
= parse_sec(optarg
, &arg_boot_loader_menu
);
756 return log_error_errno(r
, "Failed to parse --boot-loader-menu= argument '%s': %m", optarg
);
760 case ARG_BOOT_LOADER_ENTRY
:
762 if (streq(optarg
, "help")) { /* Yes, this means, "help" is not a valid boot loader entry name we can deal with */
763 r
= help_boot_loader_entry();
770 arg_boot_loader_entry
= empty_to_null(optarg
);
775 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
776 "--state= requires arguments.");
778 for (const char *p
= optarg
;;) {
779 _cleanup_free_
char *s
= NULL
;
781 r
= extract_first_word(&p
, &s
, ",", 0);
783 return log_error_errno(r
, "Failed to parse state: %s", optarg
);
787 if (streq(s
, "help")) {
792 if (strv_consume(&arg_states
, TAKE_PTR(s
)) < 0)
799 return log_error_errno(SYNTHETIC_ERRNO(EPERM
),
800 "--recursive requires root privileges.");
802 arg_recursive
= true;
805 case ARG_PRESET_MODE
:
806 if (streq(optarg
, "help")) {
807 DUMP_STRING_TABLE(unit_file_preset_mode
, UnitFilePresetMode
, _UNIT_FILE_PRESET_MAX
);
811 arg_preset_mode
= unit_file_preset_mode_from_string(optarg
);
812 if (arg_preset_mode
< 0)
813 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
814 "Failed to parse preset mode: %s.", optarg
);
823 if (strv_extend(&arg_wall
, optarg
) < 0)
828 arg_show_transaction
= true;
831 case ARG_WITH_DEPENDENCIES
:
832 arg_with_dependencies
= true;
837 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "--what= requires arguments.");
839 for (const char *p
= optarg
;;) {
840 _cleanup_free_
char *k
= NULL
;
842 r
= extract_first_word(&p
, &k
, ",", 0);
844 return log_error_errno(r
, "Failed to parse directory type: %s", optarg
);
848 if (streq(k
, "help")) {
857 r
= strv_consume(&arg_clean_what
, TAKE_PTR(k
));
865 arg_reboot_argument
= optarg
;
868 case ARG_TIMESTAMP_STYLE
:
869 if (streq(optarg
, "help")) {
870 DUMP_STRING_TABLE(timestamp_style
, TimestampStyle
, _TIMESTAMP_STYLE_MAX
);
874 arg_timestamp_style
= timestamp_style_from_string(optarg
);
875 if (arg_timestamp_style
< 0)
876 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
877 "Invalid value: %s.", optarg
);
882 arg_read_only
= true;
894 /* Output an error mimicking getopt, and print a hint afterwards */
895 log_error("%s: invalid option -- '.'", program_invocation_name
);
896 log_notice("Hint: to specify units starting with a dash, use \"--\":\n"
897 " %s [OPTIONS...] COMMAND -- -.%s ...",
898 program_invocation_name
, optarg
?: "mount");
905 assert_not_reached("Unhandled option");
908 if (arg_transport
== BUS_TRANSPORT_REMOTE
&& arg_scope
!= UNIT_FILE_SYSTEM
)
909 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
910 "Cannot access user instance remotely.");
912 if (arg_wait
&& arg_no_block
)
913 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
914 "--wait may not be combined with --no-block.");
916 bool do_reload_or_restart
= streq_ptr(argv
[optind
], "reload-or-restart");
918 if (!do_reload_or_restart
)
919 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
920 "--marked may only be used with 'reload-or-restart'.");
921 if (optind
+ 1 < argc
)
922 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
923 "No additional arguments allowed with 'reload-or-restart --marked'.");
925 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
926 "--marked --wait is not supported.");
927 if (arg_show_transaction
)
928 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
929 "--marked --show-transaction is not supported.");
931 } else if (do_reload_or_restart
) {
932 if (optind
+ 1 >= argc
)
933 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
934 "List of units to restart/reload is required.");
940 int systemctl_dispatch_parse_argv(int argc
, char *argv
[]) {
944 if (strstr_ptr(argv
[0], "halt")) {
945 arg_action
= ACTION_HALT
;
946 return halt_parse_argv(argc
, argv
);
948 } else if (strstr_ptr(argv
[0], "poweroff")) {
949 arg_action
= ACTION_POWEROFF
;
950 return halt_parse_argv(argc
, argv
);
952 } else if (strstr_ptr(argv
[0], "reboot")) {
954 arg_action
= ACTION_KEXEC
;
956 arg_action
= ACTION_REBOOT
;
957 return halt_parse_argv(argc
, argv
);
959 } else if (strstr_ptr(argv
[0], "shutdown")) {
960 arg_action
= ACTION_POWEROFF
;
961 return shutdown_parse_argv(argc
, argv
);
963 } else if (strstr_ptr(argv
[0], "init")) {
965 /* Matches invocations as "init" as well as "telinit", which are synonymous when run
966 * as PID != 1 on SysV.
968 * On SysV "telinit" was the official command to communicate with PID 1, but "init" would
969 * redirect itself to "telinit" if called with PID != 1. We follow the same logic here still,
970 * though we add one level of indirection, as we implement "telinit" in "systemctl". Hence,
971 * for us if you invoke "init" you get "systemd", but it will execve() "systemctl"
972 * immediately with argv[] unmodified if PID is != 1. If you invoke "telinit" you directly
973 * get "systemctl". In both cases we shall do the same thing, which is why we do
974 * strstr_ptr(argv[0], "init") here, as a quick way to match both.
976 * Also see redirect_telinit() in src/core/main.c. */
978 if (sd_booted() > 0) {
979 arg_action
= _ACTION_INVALID
;
980 return telinit_parse_argv(argc
, argv
);
982 /* Hmm, so some other init system is running, we need to forward this request to it.
984 arg_action
= ACTION_TELINIT
;
988 } else if (strstr_ptr(argv
[0], "runlevel")) {
989 arg_action
= ACTION_RUNLEVEL
;
990 return runlevel_parse_argv(argc
, argv
);
993 arg_action
= ACTION_SYSTEMCTL
;
994 return systemctl_parse_argv(argc
, argv
);
997 #ifndef FUZZ_SYSTEMCTL_PARSE_ARGV
998 static int systemctl_main(int argc
, char *argv
[]) {
999 static const Verb verbs
[] = {
1000 { "list-units", VERB_ANY
, VERB_ANY
, VERB_DEFAULT
|VERB_ONLINE_ONLY
, list_units
},
1001 { "list-unit-files", VERB_ANY
, VERB_ANY
, 0, list_unit_files
},
1002 { "list-sockets", VERB_ANY
, VERB_ANY
, VERB_ONLINE_ONLY
, list_sockets
},
1003 { "list-timers", VERB_ANY
, VERB_ANY
, VERB_ONLINE_ONLY
, list_timers
},
1004 { "list-jobs", VERB_ANY
, VERB_ANY
, VERB_ONLINE_ONLY
, list_jobs
},
1005 { "list-machines", VERB_ANY
, VERB_ANY
, VERB_ONLINE_ONLY
, list_machines
},
1006 { "clear-jobs", VERB_ANY
, 1, VERB_ONLINE_ONLY
, trivial_method
},
1007 { "cancel", VERB_ANY
, VERB_ANY
, VERB_ONLINE_ONLY
, cancel_job
},
1008 { "start", 2, VERB_ANY
, VERB_ONLINE_ONLY
, start_unit
},
1009 { "stop", 2, VERB_ANY
, VERB_ONLINE_ONLY
, start_unit
},
1010 { "condstop", 2, VERB_ANY
, VERB_ONLINE_ONLY
, start_unit
}, /* For compatibility with ALTLinux */
1011 { "reload", 2, VERB_ANY
, VERB_ONLINE_ONLY
, start_unit
},
1012 { "restart", 2, VERB_ANY
, VERB_ONLINE_ONLY
, start_unit
},
1013 { "try-restart", 2, VERB_ANY
, VERB_ONLINE_ONLY
, start_unit
},
1014 { "reload-or-restart", VERB_ANY
, VERB_ANY
, VERB_ONLINE_ONLY
, start_unit
},
1015 { "reload-or-try-restart", 2, VERB_ANY
, VERB_ONLINE_ONLY
, start_unit
}, /* For compatibility with old systemctl <= 228 */
1016 { "try-reload-or-restart", 2, VERB_ANY
, VERB_ONLINE_ONLY
, start_unit
},
1017 { "force-reload", 2, VERB_ANY
, VERB_ONLINE_ONLY
, start_unit
}, /* For compatibility with SysV */
1018 { "condreload", 2, VERB_ANY
, VERB_ONLINE_ONLY
, start_unit
}, /* For compatibility with ALTLinux */
1019 { "condrestart", 2, VERB_ANY
, VERB_ONLINE_ONLY
, start_unit
}, /* For compatibility with RH */
1020 { "isolate", 2, 2, VERB_ONLINE_ONLY
, start_unit
},
1021 { "kill", 2, VERB_ANY
, VERB_ONLINE_ONLY
, kill_unit
},
1022 { "clean", 2, VERB_ANY
, VERB_ONLINE_ONLY
, clean_or_freeze_unit
},
1023 { "freeze", 2, VERB_ANY
, VERB_ONLINE_ONLY
, clean_or_freeze_unit
},
1024 { "thaw", 2, VERB_ANY
, VERB_ONLINE_ONLY
, clean_or_freeze_unit
},
1025 { "is-active", 2, VERB_ANY
, VERB_ONLINE_ONLY
, check_unit_active
},
1026 { "check", 2, VERB_ANY
, VERB_ONLINE_ONLY
, check_unit_active
}, /* deprecated alias of is-active */
1027 { "is-failed", 2, VERB_ANY
, VERB_ONLINE_ONLY
, check_unit_failed
},
1028 { "show", VERB_ANY
, VERB_ANY
, VERB_ONLINE_ONLY
, show
},
1029 { "cat", 2, VERB_ANY
, VERB_ONLINE_ONLY
, cat
},
1030 { "status", VERB_ANY
, VERB_ANY
, VERB_ONLINE_ONLY
, show
},
1031 { "help", VERB_ANY
, VERB_ANY
, VERB_ONLINE_ONLY
, show
},
1032 { "daemon-reload", VERB_ANY
, 1, VERB_ONLINE_ONLY
, daemon_reload
},
1033 { "daemon-reexec", VERB_ANY
, 1, VERB_ONLINE_ONLY
, daemon_reload
},
1034 { "log-level", VERB_ANY
, 2, VERB_ONLINE_ONLY
, log_setting
},
1035 { "log-target", VERB_ANY
, 2, VERB_ONLINE_ONLY
, log_setting
},
1036 { "service-log-level", 2, 3, VERB_ONLINE_ONLY
, service_log_setting
},
1037 { "service-log-target", 2, 3, VERB_ONLINE_ONLY
, service_log_setting
},
1038 { "service-watchdogs", VERB_ANY
, 2, VERB_ONLINE_ONLY
, service_watchdogs
},
1039 { "show-environment", VERB_ANY
, 1, VERB_ONLINE_ONLY
, show_environment
},
1040 { "set-environment", 2, VERB_ANY
, VERB_ONLINE_ONLY
, set_environment
},
1041 { "unset-environment", 2, VERB_ANY
, VERB_ONLINE_ONLY
, set_environment
},
1042 { "import-environment", VERB_ANY
, VERB_ANY
, VERB_ONLINE_ONLY
, import_environment
},
1043 { "halt", VERB_ANY
, 1, VERB_ONLINE_ONLY
, start_system_special
},
1044 { "poweroff", VERB_ANY
, 1, VERB_ONLINE_ONLY
, start_system_special
},
1045 { "reboot", VERB_ANY
, 2, VERB_ONLINE_ONLY
, start_system_special
},
1046 { "kexec", VERB_ANY
, 1, VERB_ONLINE_ONLY
, start_system_special
},
1047 { "suspend", VERB_ANY
, 1, VERB_ONLINE_ONLY
, start_system_special
},
1048 { "hibernate", VERB_ANY
, 1, VERB_ONLINE_ONLY
, start_system_special
},
1049 { "hybrid-sleep", VERB_ANY
, 1, VERB_ONLINE_ONLY
, start_system_special
},
1050 { "suspend-then-hibernate",VERB_ANY
, 1, VERB_ONLINE_ONLY
, start_system_special
},
1051 { "default", VERB_ANY
, 1, VERB_ONLINE_ONLY
, start_special
},
1052 { "rescue", VERB_ANY
, 1, VERB_ONLINE_ONLY
, start_system_special
},
1053 { "emergency", VERB_ANY
, 1, VERB_ONLINE_ONLY
, start_system_special
},
1054 { "exit", VERB_ANY
, 2, VERB_ONLINE_ONLY
, start_special
},
1055 { "reset-failed", VERB_ANY
, VERB_ANY
, VERB_ONLINE_ONLY
, reset_failed
},
1056 { "enable", 2, VERB_ANY
, 0, enable_unit
},
1057 { "disable", 2, VERB_ANY
, 0, enable_unit
},
1058 { "is-enabled", 2, VERB_ANY
, 0, unit_is_enabled
},
1059 { "reenable", 2, VERB_ANY
, 0, enable_unit
},
1060 { "preset", 2, VERB_ANY
, 0, enable_unit
},
1061 { "preset-all", VERB_ANY
, 1, 0, preset_all
},
1062 { "mask", 2, VERB_ANY
, 0, enable_unit
},
1063 { "unmask", 2, VERB_ANY
, 0, enable_unit
},
1064 { "link", 2, VERB_ANY
, 0, enable_unit
},
1065 { "revert", 2, VERB_ANY
, 0, enable_unit
},
1066 { "switch-root", 2, VERB_ANY
, VERB_ONLINE_ONLY
, switch_root
},
1067 { "list-dependencies", VERB_ANY
, VERB_ANY
, VERB_ONLINE_ONLY
, list_dependencies
},
1068 { "set-default", 2, 2, 0, set_default
},
1069 { "get-default", VERB_ANY
, 1, 0, get_default
},
1070 { "set-property", 3, VERB_ANY
, VERB_ONLINE_ONLY
, set_property
},
1071 { "is-system-running", VERB_ANY
, 1, 0, is_system_running
},
1072 { "add-wants", 3, VERB_ANY
, 0, add_dependency
},
1073 { "add-requires", 3, VERB_ANY
, 0, add_dependency
},
1074 { "edit", 2, VERB_ANY
, VERB_ONLINE_ONLY
, edit
},
1075 { "bind", 3, 4, VERB_ONLINE_ONLY
, mount_bind
},
1076 { "mount-image", 4, 5, VERB_ONLINE_ONLY
, mount_image
},
1080 const Verb
*verb
= verbs_find_verb(argv
[optind
], verbs
);
1081 if (verb
&& (verb
->flags
& VERB_ONLINE_ONLY
) && arg_root
)
1082 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1083 "Verb '%s' cannot be used with --root=.",
1084 argv
[optind
] ?: verb
->verb
);
1086 return dispatch_verb(argc
, argv
, verbs
, NULL
);
1089 static int run(int argc
, char *argv
[]) {
1092 setlocale(LC_ALL
, "");
1093 log_parse_environment();
1096 /* The journal merging logic potentially needs a lot of fds. */
1097 (void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE
);
1101 r
= systemctl_dispatch_parse_argv(argc
, argv
);
1105 if (arg_action
!= ACTION_SYSTEMCTL
&& running_in_chroot() > 0) {
1107 log_info("Running in chroot, ignoring request.");
1112 /* systemctl_main() will print an error message for the bus connection, but only if it needs to */
1114 switch (arg_action
) {
1116 case ACTION_SYSTEMCTL
:
1117 r
= systemctl_main(argc
, argv
);
1120 /* Legacy command aliases set arg_action. They provide some fallbacks, e.g. to tell sysvinit to
1121 * reboot after you have installed systemd binaries. */
1124 case ACTION_POWEROFF
:
1130 case ACTION_RUNLEVEL2
:
1131 case ACTION_RUNLEVEL3
:
1132 case ACTION_RUNLEVEL4
:
1133 case ACTION_RUNLEVEL5
:
1135 r
= start_with_fallback();
1140 r
= reload_with_fallback();
1143 case ACTION_CANCEL_SHUTDOWN
:
1144 r
= logind_cancel_shutdown();
1147 case ACTION_RUNLEVEL
:
1148 r
= runlevel_main();
1151 case ACTION_TELINIT
:
1152 r
= exec_telinit(argv
);
1156 case ACTION_SUSPEND
:
1157 case ACTION_HIBERNATE
:
1158 case ACTION_HYBRID_SLEEP
:
1159 case ACTION_SUSPEND_THEN_HIBERNATE
:
1160 case ACTION_EMERGENCY
:
1161 case ACTION_DEFAULT
:
1162 /* systemctl verbs with no equivalent in the legacy commands. These cannot appear in
1163 * arg_action. Fall through. */
1165 case _ACTION_INVALID
:
1167 assert_not_reached("Unknown action");
1173 /* Note that we return r here, not 0, so that we can implement the LSB-like return codes */
1177 DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run
);