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