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