1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
11 #include "main-func.h"
12 #include "output-mode.h"
14 #include "path-util.h"
15 #include "pretty-print.h"
16 #include "rlimit-util.h"
18 #include "signal-util.h"
19 #include "string-table.h"
20 #include "systemctl-add-dependency.h"
21 #include "systemctl-cancel-job.h"
22 #include "systemctl-clean-or-freeze.h"
23 #include "systemctl-compat-halt.h"
24 #include "systemctl-compat-runlevel.h"
25 #include "systemctl-compat-shutdown.h"
26 #include "systemctl-compat-telinit.h"
27 #include "systemctl-daemon-reload.h"
28 #include "systemctl-edit.h"
29 #include "systemctl-enable.h"
30 #include "systemctl-is-active.h"
31 #include "systemctl-is-enabled.h"
32 #include "systemctl-is-system-running.h"
33 #include "systemctl-kill.h"
34 #include "systemctl-list-dependencies.h"
35 #include "systemctl-list-jobs.h"
36 #include "systemctl-list-machines.h"
37 #include "systemctl-list-unit-files.h"
38 #include "systemctl-list-units.h"
39 #include "systemctl-log-setting.h"
40 #include "systemctl-logind.h"
41 #include "systemctl-preset-all.h"
42 #include "systemctl-reset-failed.h"
43 #include "systemctl-service-watchdogs.h"
44 #include "systemctl-set-default.h"
45 #include "systemctl-set-environment.h"
46 #include "systemctl-set-property.h"
47 #include "systemctl-show.h"
48 #include "systemctl-start-special.h"
49 #include "systemctl-start-unit.h"
50 #include "systemctl-switch-root.h"
51 #include "systemctl-sysv-compat.h"
52 #include "systemctl-trivial-method.h"
53 #include "systemctl-util.h"
54 #include "systemctl.h"
55 #include "terminal-util.h"
56 #include "time-util.h"
60 char **arg_types
= NULL
;
61 char **arg_states
= NULL
;
62 char **arg_properties
= NULL
;
64 enum dependency arg_dependency
= DEPENDENCY_FORWARD
;
65 const char *arg_job_mode
= "replace";
66 UnitFileScope arg_scope
= UNIT_FILE_SYSTEM
;
67 bool arg_wait
= false;
68 bool arg_no_block
= false;
69 bool arg_no_legend
= false;
70 PagerFlags arg_pager_flags
= 0;
71 bool arg_no_wtmp
= false;
72 bool arg_no_sync
= false;
73 bool arg_no_wall
= false;
74 bool arg_no_reload
= false;
75 bool arg_value
= false;
76 bool arg_show_types
= false;
77 bool arg_ignore_inhibitors
= false;
78 bool arg_dry_run
= false;
79 bool arg_quiet
= false;
80 bool arg_full
= false;
81 bool arg_recursive
= false;
82 bool arg_with_dependencies
= false;
83 bool arg_show_transaction
= false;
85 bool arg_ask_password
= false;
86 bool arg_runtime
= false;
87 UnitFilePresetMode arg_preset_mode
= UNIT_FILE_PRESET_FULL
;
88 char **arg_wall
= NULL
;
89 const char *arg_kill_who
= NULL
;
90 int arg_signal
= SIGTERM
;
91 char *arg_root
= NULL
;
93 const char *arg_reboot_argument
= NULL
;
94 enum action arg_action
= ACTION_SYSTEMCTL
;
95 BusTransport arg_transport
= BUS_TRANSPORT_LOCAL
;
96 const char *arg_host
= NULL
;
97 unsigned arg_lines
= 10;
98 OutputMode arg_output
= OUTPUT_SHORT
;
99 bool arg_plain
= false;
100 bool arg_firmware_setup
= false;
101 usec_t arg_boot_loader_menu
= USEC_INFINITY
;
102 const char *arg_boot_loader_entry
= NULL
;
103 bool arg_now
= false;
104 bool arg_jobs_before
= false;
105 bool arg_jobs_after
= false;
106 char **arg_clean_what
= NULL
;
107 TimestampStyle arg_timestamp_style
= TIMESTAMP_PRETTY
;
109 STATIC_DESTRUCTOR_REGISTER(arg_wall
, strv_freep
);
110 STATIC_DESTRUCTOR_REGISTER(arg_root
, freep
);
111 STATIC_DESTRUCTOR_REGISTER(arg_types
, strv_freep
);
112 STATIC_DESTRUCTOR_REGISTER(arg_states
, strv_freep
);
113 STATIC_DESTRUCTOR_REGISTER(arg_properties
, strv_freep
);
114 STATIC_DESTRUCTOR_REGISTER(arg_clean_what
, strv_freep
);
116 static int systemctl_help(void) {
117 _cleanup_free_
char *link
= NULL
;
120 (void) pager_open(arg_pager_flags
);
122 r
= terminal_urlify_man("systemctl", "1", &link
);
126 printf("%1$s [OPTIONS...] COMMAND ...\n\n"
127 "%5$sQuery or send control commands to the system manager.%6$s\n"
128 "\n%3$sUnit Commands:%4$s\n"
129 " list-units [PATTERN...] List units currently in memory\n"
130 " list-sockets [PATTERN...] List socket units currently in memory,\n"
131 " ordered by address\n"
132 " list-timers [PATTERN...] List timer units currently in memory,\n"
133 " ordered by next elapse\n"
134 " is-active PATTERN... Check whether units are active\n"
135 " is-failed PATTERN... Check whether units are failed\n"
136 " status [PATTERN...|PID...] Show runtime status of one or more units\n"
137 " show [PATTERN...|JOB...] Show properties of one or more\n"
138 " units/jobs or the manager\n"
139 " cat PATTERN... Show files and drop-ins of specified units\n"
140 " help PATTERN...|PID... Show manual for one or more units\n"
141 " list-dependencies [UNIT...] Recursively show units which are required\n"
142 " or wanted by the units or by which those\n"
143 " units are required or wanted\n"
144 " start UNIT... Start (activate) one or more units\n"
145 " stop UNIT... Stop (deactivate) one or more units\n"
146 " reload UNIT... Reload one or more units\n"
147 " restart UNIT... Start or restart one or more units\n"
148 " try-restart UNIT... Restart one or more units if active\n"
149 " reload-or-restart UNIT... Reload one or more units if possible,\n"
150 " otherwise start or restart\n"
151 " try-reload-or-restart UNIT... If active, reload one or more units,\n"
152 " if supported, otherwise restart\n"
153 " isolate UNIT Start one unit and stop all others\n"
154 " kill UNIT... Send signal to processes of a unit\n"
155 " clean UNIT... Clean runtime, cache, state, logs or\n"
156 " configuration of unit\n"
157 " freeze PATTERN... Freeze execution of unit processes\n"
158 " thaw PATTERN... Resume execution of a frozen unit\n"
159 " set-property UNIT PROPERTY=VALUE... Sets one or more properties of a unit\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"
164 "\n%3$sUnit File Commands:%4$s\n"
165 " list-unit-files [PATTERN...] List installed unit files\n"
166 " enable [UNIT...|PATH...] Enable one or more unit files\n"
167 " disable UNIT... Disable one or more unit files\n"
168 " reenable UNIT... Reenable one or more unit files\n"
169 " preset UNIT... Enable/disable one or more unit files\n"
170 " based on preset configuration\n"
171 " preset-all Enable/disable all unit files based on\n"
172 " preset configuration\n"
173 " is-enabled UNIT... Check whether unit files are enabled\n"
174 " mask UNIT... Mask one or more units\n"
175 " unmask UNIT... Unmask one or more units\n"
176 " link PATH... Link one or more units files into\n"
178 " revert UNIT... Revert one or more unit files to vendor\n"
180 " add-wants TARGET UNIT... Add 'Wants' dependency for the target\n"
181 " on specified one or more units\n"
182 " add-requires TARGET UNIT... Add 'Requires' dependency for the target\n"
183 " on specified one or more units\n"
184 " edit UNIT... Edit one or more unit files\n"
185 " get-default Get the name of the default target\n"
186 " set-default TARGET Set the default target\n"
187 "\n%3$sMachine Commands:%4$s\n"
188 " list-machines [PATTERN...] List local containers and host\n"
189 "\n%3$sJob Commands:%4$s\n"
190 " list-jobs [PATTERN...] List jobs\n"
191 " cancel [JOB...] Cancel all, one, or more jobs\n"
192 "\n%3$sEnvironment Commands:%4$s\n"
193 " show-environment Dump environment\n"
194 " set-environment VARIABLE=VALUE... Set one or more environment variables\n"
195 " unset-environment VARIABLE... Unset one or more environment variables\n"
196 " import-environment [VARIABLE...] Import all or some environment variables\n"
197 "\n%3$sManager State Commands:%4$s\n"
198 " daemon-reload Reload systemd manager configuration\n"
199 " daemon-reexec Reexecute systemd manager\n"
200 " log-level [LEVEL] Get/set logging threshold for manager\n"
201 " log-target [TARGET] Get/set logging target for manager\n"
202 " service-watchdogs [BOOL] Get/set service watchdog state\n"
203 "\n%3$sSystem Commands:%4$s\n"
204 " is-system-running Check whether system is fully running\n"
205 " default Enter system default mode\n"
206 " rescue Enter system rescue mode\n"
207 " emergency Enter system emergency mode\n"
208 " halt Shut down and halt the system\n"
209 " poweroff Shut down and power-off the system\n"
210 " reboot Shut down and reboot the system\n"
211 " kexec Shut down and reboot the system with kexec\n"
212 " exit [EXIT_CODE] Request user instance or container exit\n"
213 " switch-root ROOT [INIT] Change to a different root file system\n"
214 " suspend Suspend the system\n"
215 " hibernate Hibernate the system\n"
216 " hybrid-sleep Hibernate and suspend the system\n"
217 " suspend-then-hibernate Suspend the system, wake after a period of\n"
218 " time, and hibernate"
219 "\n%3$sOptions:%4$s\n"
220 " -h --help Show this help\n"
221 " --version Show package version\n"
222 " --system Connect to system manager\n"
223 " --user Connect to user service manager\n"
224 " -H --host=[USER@]HOST Operate on remote host\n"
225 " -M --machine=CONTAINER Operate on a local container\n"
226 " -t --type=TYPE List units of a particular type\n"
227 " --state=STATE List units with particular LOAD or SUB or ACTIVE state\n"
228 " --failed Shortcut for --state=failed\n"
229 " -p --property=NAME Show only properties by this name\n"
230 " -P NAME Equivalent to --value --property=NAME\n"
231 " -a --all Show all properties/all units currently in memory,\n"
232 " including dead/empty ones. To list all units installed\n"
233 " on the system, use 'list-unit-files' instead.\n"
234 " -l --full Don't ellipsize unit names on output\n"
235 " -r --recursive Show unit list of host and local containers\n"
236 " --reverse Show reverse dependencies with 'list-dependencies'\n"
237 " --with-dependencies Show unit dependencies with 'status', 'cat',\n"
238 " 'list-units', and 'list-unit-files'.\n"
239 " --job-mode=MODE Specify how to deal with already queued jobs, when\n"
240 " queueing a new job\n"
241 " -T --show-transaction When enqueuing a unit job, show full transaction\n"
242 " --show-types When showing sockets, explicitly show their type\n"
243 " --value When showing properties, only print the value\n"
244 " -i --ignore-inhibitors When shutting down or sleeping, ignore inhibitors\n"
245 " --kill-who=WHO Whom to send signal to\n"
246 " -s --signal=SIGNAL Which signal to send\n"
247 " --what=RESOURCES Which types of resources to remove\n"
248 " --now Start or stop unit after enabling or disabling it\n"
249 " --dry-run Only print what would be done\n"
250 " Currently supported by verbs: halt, poweroff, reboot,\n"
251 " kexec, suspend, hibernate, suspend-then-hibernate,\n"
252 " hybrid-sleep, default, rescue, emergency, and exit.\n"
253 " -q --quiet Suppress output\n"
254 " --wait For (re)start, wait until service stopped again\n"
255 " For is-system-running, wait until startup is completed\n"
256 " --no-block Do not wait until operation finished\n"
257 " --no-wall Don't send wall message before halt/power-off/reboot\n"
258 " --no-reload Don't reload daemon after en-/dis-abling unit files\n"
259 " --no-legend Do not print a legend (column headers and hints)\n"
260 " --no-pager Do not pipe output into a pager\n"
261 " --no-ask-password Do not ask for system passwords\n"
262 " --global Enable/disable/mask unit files globally\n"
263 " --runtime Enable/disable/mask unit files temporarily until next\n"
265 " -f --force When enabling unit files, override existing symlinks\n"
266 " When shutting down, execute action immediately\n"
267 " --preset-mode= Apply only enable, only disable, or all presets\n"
268 " --root=PATH Enable/disable/mask unit files in the specified root\n"
270 " -n --lines=INTEGER Number of journal entries to show\n"
271 " -o --output=STRING Change journal output mode (short, short-precise,\n"
272 " short-iso, short-iso-precise, short-full,\n"
273 " short-monotonic, short-unix,\n"
274 " verbose, export, json, json-pretty, json-sse, cat)\n"
275 " --firmware-setup Tell the firmware to show the setup menu on next boot\n"
276 " --boot-loader-menu=TIME\n"
277 " Boot into boot loader menu on next boot\n"
278 " --boot-loader-entry=NAME\n"
279 " Boot into a specific boot loader entry on next boot\n"
280 " --plain Print unit dependencies as a list instead of a tree\n"
281 " --timestamp=FORMAT Change format of printed timestamps.\n"
282 " 'pretty' (default): 'Day YYYY-MM-DD HH:MM:SS TZ\n"
283 " 'us': 'Day YYYY-MM-DD HH:MM:SS.UUUUUU TZ\n"
284 " 'utc': 'Day YYYY-MM-DD HH:MM:SS UTC\n"
285 " 'us+utc': 'Day YYYY-MM-DD HH:MM:SS.UUUUUU UTC\n"
286 "\nSee the %2$s for details.\n"
287 , program_invocation_short_name
289 , ansi_underline(), ansi_normal()
290 , ansi_highlight(), ansi_normal()
296 static void help_types(void) {
298 puts("Available unit types:");
300 DUMP_STRING_TABLE(unit_type
, UnitType
, _UNIT_TYPE_MAX
);
303 static void help_states(void) {
305 puts("Available unit load states:");
306 DUMP_STRING_TABLE(unit_load_state
, UnitLoadState
, _UNIT_LOAD_STATE_MAX
);
309 puts("\nAvailable unit active states:");
310 DUMP_STRING_TABLE(unit_active_state
, UnitActiveState
, _UNIT_ACTIVE_STATE_MAX
);
313 puts("\nAvailable unit file states:");
314 DUMP_STRING_TABLE(unit_file_state
, UnitFileState
, _UNIT_FILE_STATE_MAX
);
317 puts("\nAvailable automount unit substates:");
318 DUMP_STRING_TABLE(automount_state
, AutomountState
, _AUTOMOUNT_STATE_MAX
);
321 puts("\nAvailable device unit substates:");
322 DUMP_STRING_TABLE(device_state
, DeviceState
, _DEVICE_STATE_MAX
);
325 puts("\nAvailable mount unit substates:");
326 DUMP_STRING_TABLE(mount_state
, MountState
, _MOUNT_STATE_MAX
);
329 puts("\nAvailable path unit substates:");
330 DUMP_STRING_TABLE(path_state
, PathState
, _PATH_STATE_MAX
);
333 puts("\nAvailable scope unit substates:");
334 DUMP_STRING_TABLE(scope_state
, ScopeState
, _SCOPE_STATE_MAX
);
337 puts("\nAvailable service unit substates:");
338 DUMP_STRING_TABLE(service_state
, ServiceState
, _SERVICE_STATE_MAX
);
341 puts("\nAvailable slice unit substates:");
342 DUMP_STRING_TABLE(slice_state
, SliceState
, _SLICE_STATE_MAX
);
345 puts("\nAvailable socket unit substates:");
346 DUMP_STRING_TABLE(socket_state
, SocketState
, _SOCKET_STATE_MAX
);
349 puts("\nAvailable swap unit substates:");
350 DUMP_STRING_TABLE(swap_state
, SwapState
, _SWAP_STATE_MAX
);
353 puts("\nAvailable target unit substates:");
354 DUMP_STRING_TABLE(target_state
, TargetState
, _TARGET_STATE_MAX
);
357 puts("\nAvailable timer unit substates:");
358 DUMP_STRING_TABLE(timer_state
, TimerState
, _TIMER_STATE_MAX
);
361 static int systemctl_parse_argv(int argc
, char *argv
[]) {
370 ARG_IGNORE_DEPENDENCIES
,
391 ARG_BOOT_LOADER_MENU
,
392 ARG_BOOT_LOADER_ENTRY
,
395 ARG_WITH_DEPENDENCIES
,
402 static const struct option options
[] = {
403 { "help", no_argument
, NULL
, 'h' },
404 { "version", no_argument
, NULL
, ARG_VERSION
},
405 { "type", required_argument
, NULL
, 't' },
406 { "property", required_argument
, NULL
, 'p' },
407 { "all", no_argument
, NULL
, 'a' },
408 { "reverse", no_argument
, NULL
, ARG_REVERSE
},
409 { "after", no_argument
, NULL
, ARG_AFTER
},
410 { "before", no_argument
, NULL
, ARG_BEFORE
},
411 { "show-types", no_argument
, NULL
, ARG_SHOW_TYPES
},
412 { "failed", no_argument
, NULL
, ARG_FAILED
}, /* compatibility only */
413 { "full", no_argument
, NULL
, 'l' },
414 { "job-mode", required_argument
, NULL
, ARG_JOB_MODE
},
415 { "fail", no_argument
, NULL
, ARG_FAIL
}, /* compatibility only */
416 { "irreversible", no_argument
, NULL
, ARG_IRREVERSIBLE
}, /* compatibility only */
417 { "ignore-dependencies", no_argument
, NULL
, ARG_IGNORE_DEPENDENCIES
}, /* compatibility only */
418 { "ignore-inhibitors", no_argument
, NULL
, 'i' },
419 { "value", no_argument
, NULL
, ARG_VALUE
},
420 { "user", no_argument
, NULL
, ARG_USER
},
421 { "system", no_argument
, NULL
, ARG_SYSTEM
},
422 { "global", no_argument
, NULL
, ARG_GLOBAL
},
423 { "wait", no_argument
, NULL
, ARG_WAIT
},
424 { "no-block", no_argument
, NULL
, ARG_NO_BLOCK
},
425 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
426 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
427 { "no-wall", no_argument
, NULL
, ARG_NO_WALL
},
428 { "dry-run", no_argument
, NULL
, ARG_DRY_RUN
},
429 { "quiet", no_argument
, NULL
, 'q' },
430 { "root", required_argument
, NULL
, ARG_ROOT
},
431 { "force", no_argument
, NULL
, 'f' },
432 { "no-reload", no_argument
, NULL
, ARG_NO_RELOAD
},
433 { "kill-who", required_argument
, NULL
, ARG_KILL_WHO
},
434 { "signal", required_argument
, NULL
, 's' },
435 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
436 { "host", required_argument
, NULL
, 'H' },
437 { "machine", required_argument
, NULL
, 'M' },
438 { "runtime", no_argument
, NULL
, ARG_RUNTIME
},
439 { "lines", required_argument
, NULL
, 'n' },
440 { "output", required_argument
, NULL
, 'o' },
441 { "plain", no_argument
, NULL
, ARG_PLAIN
},
442 { "state", required_argument
, NULL
, ARG_STATE
},
443 { "recursive", no_argument
, NULL
, 'r' },
444 { "with-dependencies", no_argument
, NULL
, ARG_WITH_DEPENDENCIES
},
445 { "preset-mode", required_argument
, NULL
, ARG_PRESET_MODE
},
446 { "firmware-setup", no_argument
, NULL
, ARG_FIRMWARE_SETUP
},
447 { "boot-loader-menu", required_argument
, NULL
, ARG_BOOT_LOADER_MENU
},
448 { "boot-loader-entry", required_argument
, NULL
, ARG_BOOT_LOADER_ENTRY
},
449 { "now", no_argument
, NULL
, ARG_NOW
},
450 { "message", required_argument
, NULL
, ARG_MESSAGE
},
451 { "show-transaction", no_argument
, NULL
, 'T' },
452 { "what", required_argument
, NULL
, ARG_WHAT
},
453 { "reboot-argument", required_argument
, NULL
, ARG_REBOOT_ARG
},
454 { "timestamp", required_argument
, NULL
, ARG_TIMESTAMP_STYLE
},
463 /* We default to allowing interactive authorization only in systemctl (not in the legacy commands) */
464 arg_ask_password
= true;
466 while ((c
= getopt_long(argc
, argv
, "ht:p:P:alqfs:H:M:n:o:iTr.::", options
, NULL
)) >= 0)
471 return systemctl_help();
480 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
481 "--type= requires arguments.");
484 _cleanup_free_
char *type
= NULL
;
486 r
= extract_first_word(&p
, &type
, ",", 0);
488 return log_error_errno(r
, "Failed to parse type: %s", optarg
);
492 if (streq(type
, "help")) {
497 if (unit_type_from_string(type
) >= 0) {
498 if (strv_consume(&arg_types
, TAKE_PTR(type
)) < 0)
503 /* It's much nicer to use --state= for load states, but let's support this in
504 * --types= too for compatibility with old versions */
505 if (unit_load_state_from_string(type
) >= 0) {
506 if (strv_consume(&arg_states
, TAKE_PTR(type
)) < 0)
511 log_error("Unknown unit type or load state '%s'.", type
);
512 return log_info_errno(SYNTHETIC_ERRNO(EINVAL
),
513 "Use -t help to see a list of allowed values.");
524 /* Make sure that if the empty property list was specified, we won't show any
526 if (isempty(optarg
) && !arg_properties
) {
527 arg_properties
= new0(char*, 1);
534 _cleanup_free_
char *prop
= NULL
;
536 r
= extract_first_word(&p
, &prop
, ",", 0);
538 return log_error_errno(r
, "Failed to parse property: %s", optarg
);
542 if (strv_consume(&arg_properties
, TAKE_PTR(prop
)) < 0)
547 /* If the user asked for a particular property, show it, even if it is empty. */
557 arg_dependency
= DEPENDENCY_REVERSE
;
561 arg_dependency
= DEPENDENCY_AFTER
;
562 arg_jobs_after
= true;
566 arg_dependency
= DEPENDENCY_BEFORE
;
567 arg_jobs_before
= true;
571 arg_show_types
= true;
579 arg_job_mode
= optarg
;
583 arg_job_mode
= "fail";
586 case ARG_IRREVERSIBLE
:
587 arg_job_mode
= "replace-irreversibly";
590 case ARG_IGNORE_DEPENDENCIES
:
591 arg_job_mode
= "ignore-dependencies";
595 arg_scope
= UNIT_FILE_USER
;
599 arg_scope
= UNIT_FILE_SYSTEM
;
603 arg_scope
= UNIT_FILE_GLOBAL
;
615 arg_no_legend
= true;
619 arg_pager_flags
|= PAGER_DISABLE
;
627 r
= parse_path_argument_and_warn(optarg
, false, &arg_root
);
637 if (strv_extend(&arg_states
, "failed") < 0)
655 arg_no_reload
= true;
659 arg_kill_who
= optarg
;
663 if (streq(optarg
, "help")) {
664 DUMP_STRING_TABLE(signal
, int, _NSIG
);
668 arg_signal
= signal_from_string(optarg
);
670 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
671 "Failed to parse signal string %s.",
675 case ARG_NO_ASK_PASSWORD
:
676 arg_ask_password
= false;
680 arg_transport
= BUS_TRANSPORT_REMOTE
;
685 arg_transport
= BUS_TRANSPORT_MACHINE
;
694 if (safe_atou(optarg
, &arg_lines
) < 0)
695 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
696 "Failed to parse lines '%s'",
701 if (streq(optarg
, "help")) {
702 DUMP_STRING_TABLE(output_mode
, OutputMode
, _OUTPUT_MODE_MAX
);
706 arg_output
= output_mode_from_string(optarg
);
708 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
709 "Unknown output '%s'.",
712 if (OUTPUT_MODE_IS_JSON(arg_output
)) {
713 arg_no_legend
= true;
719 arg_ignore_inhibitors
= true;
726 case ARG_FIRMWARE_SETUP
:
727 arg_firmware_setup
= true;
730 case ARG_BOOT_LOADER_MENU
:
732 r
= parse_sec(optarg
, &arg_boot_loader_menu
);
734 return log_error_errno(r
, "Failed to parse --boot-loader-menu= argument '%s': %m", optarg
);
738 case ARG_BOOT_LOADER_ENTRY
:
740 if (streq(optarg
, "help")) { /* Yes, this means, "help" is not a valid boot loader entry name we can deal with */
741 r
= help_boot_loader_entry();
748 arg_boot_loader_entry
= empty_to_null(optarg
);
755 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
756 "--state= requires arguments.");
759 _cleanup_free_
char *s
= NULL
;
761 r
= extract_first_word(&p
, &s
, ",", 0);
763 return log_error_errno(r
, "Failed to parse state: %s", optarg
);
767 if (streq(s
, "help")) {
772 if (strv_consume(&arg_states
, TAKE_PTR(s
)) < 0)
780 return log_error_errno(SYNTHETIC_ERRNO(EPERM
),
781 "--recursive requires root privileges.");
783 arg_recursive
= true;
786 case ARG_PRESET_MODE
:
787 if (streq(optarg
, "help")) {
788 DUMP_STRING_TABLE(unit_file_preset_mode
, UnitFilePresetMode
, _UNIT_FILE_PRESET_MAX
);
792 arg_preset_mode
= unit_file_preset_mode_from_string(optarg
);
793 if (arg_preset_mode
< 0)
794 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
795 "Failed to parse preset mode: %s.", optarg
);
804 if (strv_extend(&arg_wall
, optarg
) < 0)
809 arg_show_transaction
= true;
812 case ARG_WITH_DEPENDENCIES
:
813 arg_with_dependencies
= true;
820 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "--what= requires arguments.");
823 _cleanup_free_
char *k
= NULL
;
825 r
= extract_first_word(&p
, &k
, ",", 0);
827 return log_error_errno(r
, "Failed to parse directory type: %s", optarg
);
831 if (streq(k
, "help")) {
840 r
= strv_consume(&arg_clean_what
, TAKE_PTR(k
));
849 arg_reboot_argument
= optarg
;
852 case ARG_TIMESTAMP_STYLE
:
853 if (streq(optarg
, "help")) {
854 DUMP_STRING_TABLE(timestamp_style
, TimestampStyle
, _TIMESTAMP_STYLE_MAX
);
858 arg_timestamp_style
= timestamp_style_from_string(optarg
);
859 if (arg_timestamp_style
< 0)
860 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
861 "Invalid value: %s.", optarg
);
866 /* Output an error mimicking getopt, and print a hint afterwards */
867 log_error("%s: invalid option -- '.'", program_invocation_name
);
868 log_notice("Hint: to specify units starting with a dash, use \"--\":\n"
869 " %s [OPTIONS...] COMMAND -- -.%s ...",
870 program_invocation_name
, optarg
?: "mount");
877 assert_not_reached("Unhandled option");
880 if (arg_transport
!= BUS_TRANSPORT_LOCAL
&& arg_scope
!= UNIT_FILE_SYSTEM
)
881 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
882 "Cannot access user instance remotely.");
884 if (arg_wait
&& arg_no_block
)
885 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
886 "--wait may not be combined with --no-block.");
891 static int parse_argv(int argc
, char *argv
[]) {
895 if (program_invocation_short_name
) {
897 if (strstr(program_invocation_short_name
, "halt")) {
898 arg_action
= ACTION_HALT
;
899 return halt_parse_argv(argc
, argv
);
901 } else if (strstr(program_invocation_short_name
, "poweroff")) {
902 arg_action
= ACTION_POWEROFF
;
903 return halt_parse_argv(argc
, argv
);
905 } else if (strstr(program_invocation_short_name
, "reboot")) {
907 arg_action
= ACTION_KEXEC
;
909 arg_action
= ACTION_REBOOT
;
910 return halt_parse_argv(argc
, argv
);
912 } else if (strstr(program_invocation_short_name
, "shutdown")) {
913 arg_action
= ACTION_POWEROFF
;
914 return shutdown_parse_argv(argc
, argv
);
916 } else if (strstr(program_invocation_short_name
, "init")) {
918 /* Matches invocations as "init" as well as "telinit", which are synonymous when run
919 * as PID != 1 on SysV.
921 * On SysV "telinit" was the official command to communicate with PID 1, but "init" would
922 * redirect itself to "telinit" if called with PID != 1. We follow the same logic here still,
923 * though we add one level of indirection, as we implement "telinit" in "systemctl". Hence, for
924 * us if you invoke "init" you get "systemd", but it will execve() "systemctl" immediately with
925 * argv[] unmodified if PID is != 1. If you invoke "telinit" you directly get "systemctl". In
926 * both cases we shall do the same thing, which is why we do strstr(p_i_s_n, "init") here, as a
927 * quick way to match both.
929 * Also see redirect_telinit() in src/core/main.c. */
931 if (sd_booted() > 0) {
932 arg_action
= _ACTION_INVALID
;
933 return telinit_parse_argv(argc
, argv
);
935 /* Hmm, so some other init system is running, we need to forward this request to
936 * it. For now we simply guess that it is Upstart. */
938 (void) rlimit_nofile_safe();
939 execv(TELINIT
, argv
);
941 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
942 "Couldn't find an alternative telinit implementation to spawn.");
945 } else if (strstr(program_invocation_short_name
, "runlevel")) {
946 arg_action
= ACTION_RUNLEVEL
;
947 return runlevel_parse_argv(argc
, argv
);
951 arg_action
= ACTION_SYSTEMCTL
;
952 return systemctl_parse_argv(argc
, argv
);
955 static int systemctl_main(int argc
, char *argv
[]) {
956 static const Verb verbs
[] = {
957 { "list-units", VERB_ANY
, VERB_ANY
, VERB_DEFAULT
|VERB_ONLINE_ONLY
, list_units
},
958 { "list-unit-files", VERB_ANY
, VERB_ANY
, 0, list_unit_files
},
959 { "list-sockets", VERB_ANY
, VERB_ANY
, VERB_ONLINE_ONLY
, list_sockets
},
960 { "list-timers", VERB_ANY
, VERB_ANY
, VERB_ONLINE_ONLY
, list_timers
},
961 { "list-jobs", VERB_ANY
, VERB_ANY
, VERB_ONLINE_ONLY
, list_jobs
},
962 { "list-machines", VERB_ANY
, VERB_ANY
, VERB_ONLINE_ONLY
, list_machines
},
963 { "clear-jobs", VERB_ANY
, 1, VERB_ONLINE_ONLY
, trivial_method
},
964 { "cancel", VERB_ANY
, VERB_ANY
, VERB_ONLINE_ONLY
, cancel_job
},
965 { "start", 2, VERB_ANY
, VERB_ONLINE_ONLY
, start_unit
},
966 { "stop", 2, VERB_ANY
, VERB_ONLINE_ONLY
, start_unit
},
967 { "condstop", 2, VERB_ANY
, VERB_ONLINE_ONLY
, start_unit
}, /* For compatibility with ALTLinux */
968 { "reload", 2, VERB_ANY
, VERB_ONLINE_ONLY
, start_unit
},
969 { "restart", 2, VERB_ANY
, VERB_ONLINE_ONLY
, start_unit
},
970 { "try-restart", 2, VERB_ANY
, VERB_ONLINE_ONLY
, start_unit
},
971 { "reload-or-restart", 2, VERB_ANY
, VERB_ONLINE_ONLY
, start_unit
},
972 { "reload-or-try-restart", 2, VERB_ANY
, VERB_ONLINE_ONLY
, start_unit
}, /* For compatibility with old systemctl <= 228 */
973 { "try-reload-or-restart", 2, VERB_ANY
, VERB_ONLINE_ONLY
, start_unit
},
974 { "force-reload", 2, VERB_ANY
, VERB_ONLINE_ONLY
, start_unit
}, /* For compatibility with SysV */
975 { "condreload", 2, VERB_ANY
, VERB_ONLINE_ONLY
, start_unit
}, /* For compatibility with ALTLinux */
976 { "condrestart", 2, VERB_ANY
, VERB_ONLINE_ONLY
, start_unit
}, /* For compatibility with RH */
977 { "isolate", 2, 2, VERB_ONLINE_ONLY
, start_unit
},
978 { "kill", 2, VERB_ANY
, VERB_ONLINE_ONLY
, kill_unit
},
979 { "clean", 2, VERB_ANY
, VERB_ONLINE_ONLY
, clean_or_freeze_unit
},
980 { "freeze", 2, VERB_ANY
, VERB_ONLINE_ONLY
, clean_or_freeze_unit
},
981 { "thaw", 2, VERB_ANY
, VERB_ONLINE_ONLY
, clean_or_freeze_unit
},
982 { "is-active", 2, VERB_ANY
, VERB_ONLINE_ONLY
, check_unit_active
},
983 { "check", 2, VERB_ANY
, VERB_ONLINE_ONLY
, check_unit_active
}, /* deprecated alias of is-active */
984 { "is-failed", 2, VERB_ANY
, VERB_ONLINE_ONLY
, check_unit_failed
},
985 { "show", VERB_ANY
, VERB_ANY
, VERB_ONLINE_ONLY
, show
},
986 { "cat", 2, VERB_ANY
, VERB_ONLINE_ONLY
, cat
},
987 { "status", VERB_ANY
, VERB_ANY
, VERB_ONLINE_ONLY
, show
},
988 { "help", VERB_ANY
, VERB_ANY
, VERB_ONLINE_ONLY
, show
},
989 { "daemon-reload", VERB_ANY
, 1, VERB_ONLINE_ONLY
, daemon_reload
},
990 { "daemon-reexec", VERB_ANY
, 1, VERB_ONLINE_ONLY
, daemon_reload
},
991 { "log-level", VERB_ANY
, 2, VERB_ONLINE_ONLY
, log_setting
},
992 { "log-target", VERB_ANY
, 2, VERB_ONLINE_ONLY
, log_setting
},
993 { "service-log-level", 2, 3, VERB_ONLINE_ONLY
, service_log_setting
},
994 { "service-log-target", 2, 3, VERB_ONLINE_ONLY
, service_log_setting
},
995 { "service-watchdogs", VERB_ANY
, 2, VERB_ONLINE_ONLY
, service_watchdogs
},
996 { "show-environment", VERB_ANY
, 1, VERB_ONLINE_ONLY
, show_environment
},
997 { "set-environment", 2, VERB_ANY
, VERB_ONLINE_ONLY
, set_environment
},
998 { "unset-environment", 2, VERB_ANY
, VERB_ONLINE_ONLY
, set_environment
},
999 { "import-environment", VERB_ANY
, VERB_ANY
, VERB_ONLINE_ONLY
, import_environment
},
1000 { "halt", VERB_ANY
, 1, VERB_ONLINE_ONLY
, start_system_special
},
1001 { "poweroff", VERB_ANY
, 1, VERB_ONLINE_ONLY
, start_system_special
},
1002 { "reboot", VERB_ANY
, 2, VERB_ONLINE_ONLY
, start_system_special
},
1003 { "kexec", VERB_ANY
, 1, VERB_ONLINE_ONLY
, start_system_special
},
1004 { "suspend", VERB_ANY
, 1, VERB_ONLINE_ONLY
, start_system_special
},
1005 { "hibernate", VERB_ANY
, 1, VERB_ONLINE_ONLY
, start_system_special
},
1006 { "hybrid-sleep", VERB_ANY
, 1, VERB_ONLINE_ONLY
, start_system_special
},
1007 { "suspend-then-hibernate",VERB_ANY
, 1, VERB_ONLINE_ONLY
, start_system_special
},
1008 { "default", VERB_ANY
, 1, VERB_ONLINE_ONLY
, start_special
},
1009 { "rescue", VERB_ANY
, 1, VERB_ONLINE_ONLY
, start_system_special
},
1010 { "emergency", VERB_ANY
, 1, VERB_ONLINE_ONLY
, start_system_special
},
1011 { "exit", VERB_ANY
, 2, VERB_ONLINE_ONLY
, start_special
},
1012 { "reset-failed", VERB_ANY
, VERB_ANY
, VERB_ONLINE_ONLY
, reset_failed
},
1013 { "enable", 2, VERB_ANY
, 0, enable_unit
},
1014 { "disable", 2, VERB_ANY
, 0, enable_unit
},
1015 { "is-enabled", 2, VERB_ANY
, 0, unit_is_enabled
},
1016 { "reenable", 2, VERB_ANY
, 0, enable_unit
},
1017 { "preset", 2, VERB_ANY
, 0, enable_unit
},
1018 { "preset-all", VERB_ANY
, 1, 0, preset_all
},
1019 { "mask", 2, VERB_ANY
, 0, enable_unit
},
1020 { "unmask", 2, VERB_ANY
, 0, enable_unit
},
1021 { "link", 2, VERB_ANY
, 0, enable_unit
},
1022 { "revert", 2, VERB_ANY
, 0, enable_unit
},
1023 { "switch-root", 2, VERB_ANY
, VERB_ONLINE_ONLY
, switch_root
},
1024 { "list-dependencies", VERB_ANY
, VERB_ANY
, VERB_ONLINE_ONLY
, list_dependencies
},
1025 { "set-default", 2, 2, 0, set_default
},
1026 { "get-default", VERB_ANY
, 1, 0, get_default
},
1027 { "set-property", 3, VERB_ANY
, VERB_ONLINE_ONLY
, set_property
},
1028 { "is-system-running", VERB_ANY
, 1, 0, is_system_running
},
1029 { "add-wants", 3, VERB_ANY
, 0, add_dependency
},
1030 { "add-requires", 3, VERB_ANY
, 0, add_dependency
},
1031 { "edit", 2, VERB_ANY
, VERB_ONLINE_ONLY
, edit
},
1035 const Verb
*verb
= verbs_find_verb(argv
[optind
], verbs
);
1036 if (verb
&& (verb
->flags
& VERB_ONLINE_ONLY
) && arg_root
)
1037 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1038 "Verb '%s' cannot be used with --root=.",
1039 argv
[optind
] ?: verb
->verb
);
1041 return dispatch_verb(argc
, argv
, verbs
, NULL
);
1044 static int run(int argc
, char *argv
[]) {
1047 setlocale(LC_ALL
, "");
1048 log_parse_environment_cli();
1051 /* The journal merging logic potentially needs a lot of fds. */
1052 (void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE
);
1056 r
= parse_argv(argc
, argv
);
1060 if (arg_action
!= ACTION_SYSTEMCTL
&& running_in_chroot() > 0) {
1062 log_info("Running in chroot, ignoring request.");
1067 /* systemctl_main() will print an error message for the bus connection, but only if it needs to */
1069 switch (arg_action
) {
1071 case ACTION_SYSTEMCTL
:
1072 r
= systemctl_main(argc
, argv
);
1075 /* Legacy command aliases set arg_action. They provide some fallbacks, e.g. to tell sysvinit to
1076 * reboot after you have installed systemd binaries. */
1079 case ACTION_POWEROFF
:
1085 case ACTION_RUNLEVEL2
:
1086 case ACTION_RUNLEVEL3
:
1087 case ACTION_RUNLEVEL4
:
1088 case ACTION_RUNLEVEL5
:
1090 r
= start_with_fallback();
1095 r
= reload_with_fallback();
1098 case ACTION_CANCEL_SHUTDOWN
:
1099 r
= logind_cancel_shutdown();
1102 case ACTION_RUNLEVEL
:
1103 r
= runlevel_main();
1107 case ACTION_SUSPEND
:
1108 case ACTION_HIBERNATE
:
1109 case ACTION_HYBRID_SLEEP
:
1110 case ACTION_SUSPEND_THEN_HIBERNATE
:
1111 case ACTION_EMERGENCY
:
1112 case ACTION_DEFAULT
:
1113 /* systemctl verbs with no equivalent in the legacy commands. These cannot appear in
1114 * arg_action. Fall through. */
1116 case _ACTION_INVALID
:
1118 assert_not_reached("Unknown action");
1124 /* Note that we return r here, not 0, so that we can implement the LSB-like return codes */
1128 DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run
);