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