• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 15057632786

15 May 2025 09:01PM UTC coverage: 72.267% (+0.02%) from 72.244%
15057632786

push

github

bluca
man: document how to hook stuff into system wakeup

Fixes: #6364

298523 of 413084 relevant lines covered (72.27%)

738132.88 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

66.33
/src/systemctl/systemctl.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <getopt.h>
4
#include <locale.h>
5
#include <unistd.h>
6

7
#include "sd-daemon.h"
8

9
#include "argv-util.h"
10
#include "build.h"
11
#include "bus-util.h"
12
#include "capsule-util.h"
13
#include "dissect-image.h"
14
#include "install.h"
15
#include "logs-show.h"
16
#include "main-func.h"
17
#include "mount-util.h"
18
#include "output-mode.h"
19
#include "pager.h"
20
#include "parse-argument.h"
21
#include "parse-util.h"
22
#include "path-util.h"
23
#include "pretty-print.h"
24
#include "process-util.h"
25
#include "reboot-util.h"
26
#include "rlimit-util.h"
27
#include "signal-util.h"
28
#include "stat-util.h"
29
#include "string-table.h"
30
#include "systemctl.h"
31
#include "systemctl-add-dependency.h"
32
#include "systemctl-cancel-job.h"
33
#include "systemctl-clean-or-freeze.h"
34
#include "systemctl-compat-halt.h"
35
#include "systemctl-compat-runlevel.h"
36
#include "systemctl-compat-shutdown.h"
37
#include "systemctl-compat-telinit.h"
38
#include "systemctl-daemon-reload.h"
39
#include "systemctl-edit.h"
40
#include "systemctl-enable.h"
41
#include "systemctl-is-active.h"
42
#include "systemctl-is-enabled.h"
43
#include "systemctl-is-system-running.h"
44
#include "systemctl-kill.h"
45
#include "systemctl-list-dependencies.h"
46
#include "systemctl-list-jobs.h"
47
#include "systemctl-list-machines.h"
48
#include "systemctl-list-unit-files.h"
49
#include "systemctl-list-units.h"
50
#include "systemctl-log-setting.h"
51
#include "systemctl-logind.h"
52
#include "systemctl-mount.h"
53
#include "systemctl-preset-all.h"
54
#include "systemctl-reset-failed.h"
55
#include "systemctl-service-watchdogs.h"
56
#include "systemctl-set-default.h"
57
#include "systemctl-set-environment.h"
58
#include "systemctl-set-property.h"
59
#include "systemctl-show.h"
60
#include "systemctl-start-special.h"
61
#include "systemctl-start-unit.h"
62
#include "systemctl-switch-root.h"
63
#include "systemctl-sysv-compat.h"
64
#include "systemctl-trivial-method.h"
65
#include "systemctl-util.h"
66
#include "systemctl-whoami.h"
67
#include "terminal-util.h"
68
#include "time-util.h"
69
#include "user-util.h"
70
#include "verbs.h"
71
#include "virt.h"
72

73
char **arg_types = NULL;
74
char **arg_states = NULL;
75
char **arg_properties = NULL;
76
bool arg_all = false;
77
enum dependency arg_dependency = DEPENDENCY_FORWARD;
78
const char *_arg_job_mode = NULL;
79
RuntimeScope arg_runtime_scope = RUNTIME_SCOPE_SYSTEM;
80
bool arg_wait = false;
81
bool arg_no_block = false;
82
int arg_legend = -1; /* -1: true, unless --quiet is passed, 1: true */
83
PagerFlags arg_pager_flags = 0;
84
bool arg_no_wtmp = false;
85
bool arg_no_sync = false;
86
bool arg_no_wall = false;
87
bool arg_no_reload = false;
88
BusPrintPropertyFlags arg_print_flags = 0;
89
bool arg_show_types = false;
90
int arg_check_inhibitors = -1;
91
bool arg_dry_run = false;
92
bool arg_quiet = false;
93
bool arg_verbose = false;
94
bool arg_no_warn = false;
95
bool arg_full = false;
96
bool arg_recursive = false;
97
bool arg_with_dependencies = false;
98
bool arg_show_transaction = false;
99
int arg_force = 0;
100
bool arg_ask_password = false;
101
bool arg_runtime = false;
102
UnitFilePresetMode arg_preset_mode = UNIT_FILE_PRESET_FULL;
103
char **arg_wall = NULL;
104
const char *arg_kill_whom = NULL;
105
int arg_signal = SIGTERM;
106
int arg_kill_value;
107
bool arg_kill_value_set = false;
108
char *arg_root = NULL;
109
char *arg_image = NULL;
110
usec_t arg_when = 0;
111
bool arg_stdin = false;
112
const char *arg_reboot_argument = NULL;
113
enum action arg_action = ACTION_SYSTEMCTL;
114
BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
115
const char *arg_host = NULL;
116
unsigned arg_lines = 10;
117
OutputMode arg_output = OUTPUT_SHORT;
118
bool arg_plain = false;
119
bool arg_firmware_setup = false;
120
usec_t arg_boot_loader_menu = USEC_INFINITY;
121
const char *arg_boot_loader_entry = NULL;
122
bool arg_now = false;
123
bool arg_jobs_before = false;
124
bool arg_jobs_after = false;
125
char **arg_clean_what = NULL;
126
TimestampStyle arg_timestamp_style = TIMESTAMP_PRETTY;
127
bool arg_read_only = false;
128
bool arg_mkdir = false;
129
bool arg_marked = false;
130
const char *arg_drop_in = NULL;
131
ImagePolicy *arg_image_policy = NULL;
132

133
STATIC_DESTRUCTOR_REGISTER(arg_types, strv_freep);
11,248✔
134
STATIC_DESTRUCTOR_REGISTER(arg_states, strv_freep);
11,248✔
135
STATIC_DESTRUCTOR_REGISTER(arg_properties, strv_freep);
11,248✔
136
STATIC_DESTRUCTOR_REGISTER(_arg_job_mode, unsetp);
11,248✔
137
STATIC_DESTRUCTOR_REGISTER(arg_wall, strv_freep);
11,248✔
138
STATIC_DESTRUCTOR_REGISTER(arg_kill_whom, unsetp);
11,248✔
139
STATIC_DESTRUCTOR_REGISTER(arg_root, freep);
11,248✔
140
STATIC_DESTRUCTOR_REGISTER(arg_image, freep);
11,248✔
141
STATIC_DESTRUCTOR_REGISTER(arg_reboot_argument, unsetp);
11,248✔
142
STATIC_DESTRUCTOR_REGISTER(arg_host, unsetp);
11,248✔
143
STATIC_DESTRUCTOR_REGISTER(arg_boot_loader_entry, unsetp);
11,248✔
144
STATIC_DESTRUCTOR_REGISTER(arg_clean_what, strv_freep);
11,248✔
145
STATIC_DESTRUCTOR_REGISTER(arg_drop_in, unsetp);
11,248✔
146
STATIC_DESTRUCTOR_REGISTER(arg_image_policy, image_policy_freep);
11,248✔
147

148
static int systemctl_help(void) {
×
149
        _cleanup_free_ char *link = NULL;
×
150
        int r;
×
151

152
        pager_open(arg_pager_flags);
×
153

154
        r = terminal_urlify_man("systemctl", "1", &link);
×
155
        if (r < 0)
×
156
                return log_oom();
×
157

158
        printf("%1$s [OPTIONS...] COMMAND ...\n\n"
×
159
               "%5$sQuery or send control commands to the system manager.%6$s\n"
160
               "\n%3$sUnit Commands:%4$s\n"
161
               "  list-units [PATTERN...]             List units currently in memory\n"
162
               "  list-automounts [PATTERN...]        List automount units currently in memory,\n"
163
               "                                      ordered by path\n"
164
               "  list-paths [PATTERN...]             List path units currently in memory,\n"
165
               "                                      ordered by path\n"
166
               "  list-sockets [PATTERN...]           List socket units currently in memory,\n"
167
               "                                      ordered by address\n"
168
               "  list-timers [PATTERN...]            List timer units currently in memory,\n"
169
               "                                      ordered by next elapse\n"
170
               "  is-active PATTERN...                Check whether units are active\n"
171
               "  is-failed [PATTERN...]              Check whether units are failed or\n"
172
               "                                      system is in degraded state\n"
173
               "  status [PATTERN...|PID...]          Show runtime status of one or more units\n"
174
               "  show [PATTERN...|JOB...]            Show properties of one or more\n"
175
               "                                      units/jobs or the manager\n"
176
               "  cat PATTERN...                      Show files and drop-ins of specified units\n"
177
               "  help PATTERN...|PID...              Show manual for one or more units\n"
178
               "  list-dependencies [UNIT...]         Recursively show units which are required\n"
179
               "                                      or wanted by the units or by which those\n"
180
               "                                      units are required or wanted\n"
181
               "  start UNIT...                       Start (activate) one or more units\n"
182
               "  stop UNIT...                        Stop (deactivate) one or more units\n"
183
               "  reload UNIT...                      Reload one or more units\n"
184
               "  restart UNIT...                     Start or restart one or more units\n"
185
               "  try-restart UNIT...                 Restart one or more units if active\n"
186
               "  reload-or-restart UNIT...           Reload one or more units if possible,\n"
187
               "                                      otherwise start or restart\n"
188
               "  try-reload-or-restart UNIT...       If active, reload one or more units,\n"
189
               "                                      if supported, otherwise restart\n"
190
               "  isolate UNIT                        Start one unit and stop all others\n"
191
               "  kill UNIT...                        Send signal to processes of a unit\n"
192
               "  clean UNIT...                       Clean runtime, cache, state, logs or\n"
193
               "                                      configuration of unit\n"
194
               "  freeze PATTERN...                   Freeze execution of unit processes\n"
195
               "  thaw PATTERN...                     Resume execution of a frozen unit\n"
196
               "  set-property UNIT PROPERTY=VALUE... Sets one or more properties of a unit\n"
197
               "  bind UNIT PATH [PATH]               Bind-mount a path from the host into a\n"
198
               "                                      unit's namespace\n"
199
               "  mount-image UNIT PATH [PATH [OPTS]] Mount an image from the host into a\n"
200
               "                                      unit's namespace\n"
201
               "  service-log-level SERVICE [LEVEL]   Get/set logging threshold for service\n"
202
               "  service-log-target SERVICE [TARGET] Get/set logging target for service\n"
203
               "  reset-failed [PATTERN...]           Reset failed state for all, one, or more\n"
204
               "                                      units\n"
205
               "  whoami [PID...]                     Return unit caller or specified PIDs are\n"
206
               "                                      part of\n"
207
               "\n%3$sUnit File Commands:%4$s\n"
208
               "  list-unit-files [PATTERN...]        List installed unit files\n"
209
               "  enable [UNIT...|PATH...]            Enable one or more unit files\n"
210
               "  disable UNIT...                     Disable one or more unit files\n"
211
               "  reenable UNIT...                    Reenable one or more unit files\n"
212
               "  preset UNIT...                      Enable/disable one or more unit files\n"
213
               "                                      based on preset configuration\n"
214
               "  preset-all                          Enable/disable all unit files based on\n"
215
               "                                      preset configuration\n"
216
               "  is-enabled UNIT...                  Check whether unit files are enabled\n"
217
               "  mask UNIT...                        Mask one or more units\n"
218
               "  unmask UNIT...                      Unmask one or more units\n"
219
               "  link PATH...                        Link one or more units files into\n"
220
               "                                      the search path\n"
221
               "  revert UNIT...                      Revert one or more unit files to vendor\n"
222
               "                                      version\n"
223
               "  add-wants TARGET UNIT...            Add 'Wants' dependency for the target\n"
224
               "                                      on specified one or more units\n"
225
               "  add-requires TARGET UNIT...         Add 'Requires' dependency for the target\n"
226
               "                                      on specified one or more units\n"
227
               "  edit UNIT...                        Edit one or more unit files\n"
228
               "  get-default                         Get the name of the default target\n"
229
               "  set-default TARGET                  Set the default target\n"
230
               "\n%3$sMachine Commands:%4$s\n"
231
               "  list-machines [PATTERN...]          List local containers and host\n"
232
               "\n%3$sJob Commands:%4$s\n"
233
               "  list-jobs [PATTERN...]              List jobs\n"
234
               "  cancel [JOB...]                     Cancel all, one, or more jobs\n"
235
               "\n%3$sEnvironment Commands:%4$s\n"
236
               "  show-environment                    Dump environment\n"
237
               "  set-environment VARIABLE=VALUE...   Set one or more environment variables\n"
238
               "  unset-environment VARIABLE...       Unset one or more environment variables\n"
239
               "  import-environment VARIABLE...      Import all or some environment variables\n"
240
               "\n%3$sManager State Commands:%4$s\n"
241
               "  daemon-reload                       Reload systemd manager configuration\n"
242
               "  daemon-reexec                       Reexecute systemd manager\n"
243
               "  log-level [LEVEL]                   Get/set logging threshold for manager\n"
244
               "  log-target [TARGET]                 Get/set logging target for manager\n"
245
               "  service-watchdogs [BOOL]            Get/set service watchdog state\n"
246
               "\n%3$sSystem Commands:%4$s\n"
247
               "  is-system-running                   Check whether system is fully running\n"
248
               "  default                             Enter system default mode\n"
249
               "  rescue                              Enter system rescue mode\n"
250
               "  emergency                           Enter system emergency mode\n"
251
               "  halt                                Shut down and halt the system\n"
252
               "  poweroff                            Shut down and power-off the system\n"
253
               "  reboot                              Shut down and reboot the system\n"
254
               "  kexec                               Shut down and reboot the system with kexec\n"
255
               "  soft-reboot                         Shut down and reboot userspace\n"
256
               "  exit [EXIT_CODE]                    Request user instance or container exit\n"
257
               "  switch-root [ROOT [INIT]]           Change to a different root file system\n"
258
               "  sleep                               Put the system to sleep (through one of\n"
259
               "                                      the operations below)\n"
260
               "  suspend                             Suspend the system\n"
261
               "  hibernate                           Hibernate the system\n"
262
               "  hybrid-sleep                        Hibernate and suspend the system\n"
263
               "  suspend-then-hibernate              Suspend the system, wake after a period of\n"
264
               "                                      time, and hibernate"
265
               "\n%3$sOptions:%4$s\n"
266
               "  -h --help              Show this help\n"
267
               "     --version           Show package version\n"
268
               "     --system            Connect to system manager\n"
269
               "     --user              Connect to user service manager\n"
270
               "  -C --capsule=NAME      Connect to service manager of specified capsule\n"
271
               "  -H --host=[USER@]HOST  Operate on remote host\n"
272
               "  -M --machine=CONTAINER Operate on a local container\n"
273
               "  -t --type=TYPE         List units of a particular type\n"
274
               "     --state=STATE       List units with particular LOAD or SUB or ACTIVE state\n"
275
               "     --failed            Shortcut for --state=failed\n"
276
               "  -p --property=NAME     Show only properties by this name\n"
277
               "  -P NAME                Equivalent to --value --property=NAME\n"
278
               "  -a --all               Show all properties/all units currently in memory,\n"
279
               "                         including dead/empty ones. To list all units installed\n"
280
               "                         on the system, use 'list-unit-files' instead.\n"
281
               "  -l --full              Don't ellipsize unit names on output\n"
282
               "  -r --recursive         Show unit list of host and local containers\n"
283
               "     --reverse           Show reverse dependencies with 'list-dependencies'\n"
284
               "     --before            Show units ordered before with 'list-dependencies'\n"
285
               "     --after             Show units ordered after with 'list-dependencies'\n"
286
               "     --with-dependencies Show unit dependencies with 'status', 'cat',\n"
287
               "                         'list-units', and 'list-unit-files'.\n"
288
               "     --job-mode=MODE     Specify how to deal with already queued jobs, when\n"
289
               "                         queueing a new job\n"
290
               "  -T --show-transaction  When enqueuing a unit job, show full transaction\n"
291
               "     --show-types        When showing sockets, explicitly show their type\n"
292
               "     --value             When showing properties, only print the value\n"
293
               "     --check-inhibitors=MODE\n"
294
               "                         Whether to check inhibitors before shutting down,\n"
295
               "                         sleeping, or hibernating\n"
296
               "  -i                     Shortcut for --check-inhibitors=no\n"
297
               "     --kill-whom=WHOM    Whom to send signal to\n"
298
               "     --kill-value=INT    Signal value to enqueue\n"
299
               "  -s --signal=SIGNAL     Which signal to send\n"
300
               "     --what=RESOURCES    Which types of resources to remove\n"
301
               "     --now               Start or stop unit after enabling or disabling it\n"
302
               "     --dry-run           Only print what would be done\n"
303
               "                         Currently supported by verbs: halt, poweroff, reboot,\n"
304
               "                             kexec, soft-reboot, suspend, hibernate, \n"
305
               "                             suspend-then-hibernate, hybrid-sleep, default,\n"
306
               "                             rescue, emergency, and exit.\n"
307
               "  -q --quiet             Suppress output\n"
308
               "  -v --verbose           Show unit logs while executing operation\n"
309
               "     --no-warn           Suppress several warnings shown by default\n"
310
               "     --wait              For (re)start, wait until service stopped again\n"
311
               "                         For is-system-running, wait until startup is completed\n"
312
               "                         For kill, wait until service stopped\n"
313
               "     --no-block          Do not wait until operation finished\n"
314
               "     --no-wall           Don't send wall message before halt/power-off/reboot\n"
315
               "     --message=MESSAGE   Specify human readable reason for system shutdown\n"
316
               "     --no-reload         Don't reload daemon after en-/dis-abling unit files\n"
317
               "     --legend=BOOL       Enable/disable the legend (column headers and hints)\n"
318
               "     --no-pager          Do not pipe output into a pager\n"
319
               "     --no-ask-password   Do not ask for system passwords\n"
320
               "     --global            Edit/enable/disable/mask default user unit files\n"
321
               "                         globally\n"
322
               "     --runtime           Edit/enable/disable/mask unit files temporarily until\n"
323
               "                         next reboot\n"
324
               "  -f --force             When enabling unit files, override existing symlinks\n"
325
               "                         When shutting down, execute action immediately\n"
326
               "     --preset-mode=      Apply only enable, only disable, or all presets\n"
327
               "     --root=PATH         Edit/enable/disable/mask unit files in the specified\n"
328
               "                         root directory\n"
329
               "     --image=PATH        Edit/enable/disable/mask unit files in the specified\n"
330
               "                         disk image\n"
331
               "     --image-policy=POLICY\n"
332
               "                         Specify disk image dissection policy\n"
333
               "  -n --lines=INTEGER     Number of journal entries to show\n"
334
               "  -o --output=STRING     Change journal output mode (short, short-precise,\n"
335
               "                             short-iso, short-iso-precise, short-full,\n"
336
               "                             short-monotonic, short-unix, short-delta,\n"
337
               "                             verbose, export, json, json-pretty, json-sse, cat)\n"
338
               "     --firmware-setup    Tell the firmware to show the setup menu on next boot\n"
339
               "     --boot-loader-menu=TIME\n"
340
               "                         Boot into boot loader menu on next boot\n"
341
               "     --boot-loader-entry=NAME\n"
342
               "                         Boot into a specific boot loader entry on next boot\n"
343
               "     --reboot-argument=ARG\n"
344
               "                         Specify argument string to pass to reboot()\n"
345
               "     --plain             Print unit dependencies as a list instead of a tree\n"
346
               "     --timestamp=FORMAT  Change format of printed timestamps (pretty, unix,\n"
347
               "                             us, utc, us+utc)\n"
348
               "     --read-only         Create read-only bind mount\n"
349
               "     --mkdir             Create directory before mounting, if missing\n"
350
               "     --marked            Restart/reload previously marked units\n"
351
               "     --drop-in=NAME      Edit unit files using the specified drop-in file name\n"
352
               "     --when=TIME         Schedule halt/power-off/reboot/kexec action after\n"
353
               "                         a certain timestamp\n"
354
               "     --stdin             Read new contents of edited file from stdin\n"
355
               "\nSee the %2$s for details.\n",
356
               program_invocation_short_name,
357
               link,
358
               ansi_underline(),
359
               ansi_normal(),
360
               ansi_highlight(),
361
               ansi_normal());
362

363
        return 0;
364
}
365

366
static void help_types(void) {
1✔
367
        if (arg_legend != 0)
1✔
368
                puts("Available unit types:");
1✔
369

370
        DUMP_STRING_TABLE(unit_type, UnitType, _UNIT_TYPE_MAX);
12✔
371
}
1✔
372

373
static void help_states(void) {
1✔
374
        if (arg_legend != 0)
1✔
375
                puts("Available unit load states:");
1✔
376
        DUMP_STRING_TABLE(unit_load_state, UnitLoadState, _UNIT_LOAD_STATE_MAX);
8✔
377

378
        if (arg_legend != 0)
1✔
379
                puts("\nAvailable unit active states:");
1✔
380
        DUMP_STRING_TABLE(unit_active_state, UnitActiveState, _UNIT_ACTIVE_STATE_MAX);
9✔
381

382
        if (arg_legend != 0)
1✔
383
                puts("\nAvailable unit file states:");
1✔
384
        DUMP_STRING_TABLE(unit_file_state, UnitFileState, _UNIT_FILE_STATE_MAX);
14✔
385

386
        if (arg_legend != 0)
1✔
387
                puts("\nAvailable automount unit substates:");
1✔
388
        DUMP_STRING_TABLE(automount_state, AutomountState, _AUTOMOUNT_STATE_MAX);
5✔
389

390
        if (arg_legend != 0)
1✔
391
                puts("\nAvailable device unit substates:");
1✔
392
        DUMP_STRING_TABLE(device_state, DeviceState, _DEVICE_STATE_MAX);
4✔
393

394
        if (arg_legend != 0)
1✔
395
                puts("\nAvailable mount unit substates:");
1✔
396
        DUMP_STRING_TABLE(mount_state, MountState, _MOUNT_STATE_MAX);
13✔
397

398
        if (arg_legend != 0)
1✔
399
                puts("\nAvailable path unit substates:");
1✔
400
        DUMP_STRING_TABLE(path_state, PathState, _PATH_STATE_MAX);
5✔
401

402
        if (arg_legend != 0)
1✔
403
                puts("\nAvailable scope unit substates:");
1✔
404
        DUMP_STRING_TABLE(scope_state, ScopeState, _SCOPE_STATE_MAX);
8✔
405

406
        if (arg_legend != 0)
1✔
407
                puts("\nAvailable service unit substates:");
1✔
408
        DUMP_STRING_TABLE(service_state, ServiceState, _SERVICE_STATE_MAX);
27✔
409

410
        if (arg_legend != 0)
1✔
411
                puts("\nAvailable slice unit substates:");
1✔
412
        DUMP_STRING_TABLE(slice_state, SliceState, _SLICE_STATE_MAX);
3✔
413

414
        if (arg_legend != 0)
1✔
415
                puts("\nAvailable socket unit substates:");
1✔
416
        DUMP_STRING_TABLE(socket_state, SocketState, _SOCKET_STATE_MAX);
16✔
417

418
        if (arg_legend != 0)
1✔
419
                puts("\nAvailable swap unit substates:");
1✔
420
        DUMP_STRING_TABLE(swap_state, SwapState, _SWAP_STATE_MAX);
10✔
421

422
        if (arg_legend != 0)
1✔
423
                puts("\nAvailable target unit substates:");
1✔
424
        DUMP_STRING_TABLE(target_state, TargetState, _TARGET_STATE_MAX);
3✔
425

426
        if (arg_legend != 0)
1✔
427
                puts("\nAvailable timer unit substates:");
1✔
428
        DUMP_STRING_TABLE(timer_state, TimerState, _TIMER_STATE_MAX);
6✔
429
}
1✔
430

431
static int systemctl_parse_argv(int argc, char *argv[]) {
11,236✔
432
        enum {
11,236✔
433
                ARG_FAIL = 0x100,            /* compatibility only */
434
                ARG_REVERSE,
435
                ARG_AFTER,
436
                ARG_BEFORE,
437
                ARG_CHECK_INHIBITORS,
438
                ARG_DRY_RUN,
439
                ARG_SHOW_TYPES,
440
                ARG_IRREVERSIBLE,            /* compatibility only */
441
                ARG_IGNORE_DEPENDENCIES,     /* compatibility only */
442
                ARG_VALUE,
443
                ARG_VERSION,
444
                ARG_USER,
445
                ARG_SYSTEM,
446
                ARG_GLOBAL,
447
                ARG_NO_BLOCK,
448
                ARG_LEGEND,
449
                ARG_NO_LEGEND,                /* compatibility only */
450
                ARG_NO_PAGER,
451
                ARG_NO_WALL,
452
                ARG_ROOT,
453
                ARG_IMAGE,
454
                ARG_IMAGE_POLICY,
455
                ARG_NO_RELOAD,
456
                ARG_KILL_WHOM,
457
                ARG_KILL_VALUE,
458
                ARG_NO_ASK_PASSWORD,
459
                ARG_FAILED,
460
                ARG_RUNTIME,
461
                ARG_PLAIN,
462
                ARG_STATE,
463
                ARG_JOB_MODE,
464
                ARG_PRESET_MODE,
465
                ARG_FIRMWARE_SETUP,
466
                ARG_BOOT_LOADER_MENU,
467
                ARG_BOOT_LOADER_ENTRY,
468
                ARG_NOW,
469
                ARG_MESSAGE,
470
                ARG_WITH_DEPENDENCIES,
471
                ARG_WAIT,
472
                ARG_WHAT,
473
                ARG_REBOOT_ARG,
474
                ARG_TIMESTAMP_STYLE,
475
                ARG_READ_ONLY,
476
                ARG_MKDIR,
477
                ARG_MARKED,
478
                ARG_NO_WARN,
479
                ARG_DROP_IN,
480
                ARG_WHEN,
481
                ARG_STDIN,
482
        };
483

484
        static const struct option options[] = {
11,236✔
485
                { "help",                no_argument,       NULL, 'h'                     },
486
                { "version",             no_argument,       NULL, ARG_VERSION             },
487
                { "type",                required_argument, NULL, 't'                     },
488
                { "property",            required_argument, NULL, 'p'                     },
489
                { "all",                 no_argument,       NULL, 'a'                     },
490
                { "reverse",             no_argument,       NULL, ARG_REVERSE             },
491
                { "after",               no_argument,       NULL, ARG_AFTER               },
492
                { "before",              no_argument,       NULL, ARG_BEFORE              },
493
                { "show-types",          no_argument,       NULL, ARG_SHOW_TYPES          },
494
                { "failed",              no_argument,       NULL, ARG_FAILED              },
495
                { "full",                no_argument,       NULL, 'l'                     },
496
                { "job-mode",            required_argument, NULL, ARG_JOB_MODE            },
497
                { "fail",                no_argument,       NULL, ARG_FAIL                }, /* compatibility only */
498
                { "irreversible",        no_argument,       NULL, ARG_IRREVERSIBLE        }, /* compatibility only */
499
                { "ignore-dependencies", no_argument,       NULL, ARG_IGNORE_DEPENDENCIES }, /* compatibility only */
500
                { "ignore-inhibitors",   no_argument,       NULL, 'i'                     }, /* compatibility only */
501
                { "check-inhibitors",    required_argument, NULL, ARG_CHECK_INHIBITORS    },
502
                { "value",               no_argument,       NULL, ARG_VALUE               },
503
                { "user",                no_argument,       NULL, ARG_USER                },
504
                { "system",              no_argument,       NULL, ARG_SYSTEM              },
505
                { "global",              no_argument,       NULL, ARG_GLOBAL              },
506
                { "capsule",             required_argument, NULL, 'C'                     },
507
                { "wait",                no_argument,       NULL, ARG_WAIT                },
508
                { "no-block",            no_argument,       NULL, ARG_NO_BLOCK            },
509
                { "legend",              required_argument, NULL, ARG_LEGEND              },
510
                { "no-legend",           no_argument,       NULL, ARG_NO_LEGEND           }, /* compatibility only */
511
                { "no-pager",            no_argument,       NULL, ARG_NO_PAGER            },
512
                { "no-wall",             no_argument,       NULL, ARG_NO_WALL             },
513
                { "dry-run",             no_argument,       NULL, ARG_DRY_RUN             },
514
                { "quiet",               no_argument,       NULL, 'q'                     },
515
                { "verbose",             no_argument,       NULL, 'v'                     },
516
                { "no-warn",             no_argument,       NULL, ARG_NO_WARN             },
517
                { "root",                required_argument, NULL, ARG_ROOT                },
518
                { "image",               required_argument, NULL, ARG_IMAGE               },
519
                { "image-policy",        required_argument, NULL, ARG_IMAGE_POLICY        },
520
                { "force",               no_argument,       NULL, 'f'                     },
521
                { "no-reload",           no_argument,       NULL, ARG_NO_RELOAD           },
522
                { "kill-whom",           required_argument, NULL, ARG_KILL_WHOM           },
523
                { "kill-value",          required_argument, NULL, ARG_KILL_VALUE          },
524
                { "signal",              required_argument, NULL, 's'                     },
525
                { "no-ask-password",     no_argument,       NULL, ARG_NO_ASK_PASSWORD     },
526
                { "host",                required_argument, NULL, 'H'                     },
527
                { "machine",             required_argument, NULL, 'M'                     },
528
                { "runtime",             no_argument,       NULL, ARG_RUNTIME             },
529
                { "lines",               required_argument, NULL, 'n'                     },
530
                { "output",              required_argument, NULL, 'o'                     },
531
                { "plain",               no_argument,       NULL, ARG_PLAIN               },
532
                { "state",               required_argument, NULL, ARG_STATE               },
533
                { "recursive",           no_argument,       NULL, 'r'                     },
534
                { "with-dependencies",   no_argument,       NULL, ARG_WITH_DEPENDENCIES   },
535
                { "preset-mode",         required_argument, NULL, ARG_PRESET_MODE         },
536
                { "firmware-setup",      no_argument,       NULL, ARG_FIRMWARE_SETUP      },
537
                { "boot-loader-menu",    required_argument, NULL, ARG_BOOT_LOADER_MENU    },
538
                { "boot-loader-entry",   required_argument, NULL, ARG_BOOT_LOADER_ENTRY   },
539
                { "now",                 no_argument,       NULL, ARG_NOW                 },
540
                { "message",             required_argument, NULL, ARG_MESSAGE             },
541
                { "show-transaction",    no_argument,       NULL, 'T'                     },
542
                { "what",                required_argument, NULL, ARG_WHAT                },
543
                { "reboot-argument",     required_argument, NULL, ARG_REBOOT_ARG          },
544
                { "timestamp",           required_argument, NULL, ARG_TIMESTAMP_STYLE     },
545
                { "read-only",           no_argument,       NULL, ARG_READ_ONLY           },
546
                { "mkdir",               no_argument,       NULL, ARG_MKDIR               },
547
                { "marked",              no_argument,       NULL, ARG_MARKED              },
548
                { "drop-in",             required_argument, NULL, ARG_DROP_IN             },
549
                { "when",                required_argument, NULL, ARG_WHEN                },
550
                { "stdin",               no_argument,       NULL, ARG_STDIN               },
551
                {}
552
        };
553

554
        int c, r;
11,236✔
555

556
        assert(argc >= 0);
11,236✔
557
        assert(argv);
11,236✔
558

559
        /* We default to allowing interactive authorization only in systemctl (not in the legacy commands) */
560
        arg_ask_password = true;
11,236✔
561

562
        while ((c = getopt_long(argc, argv, "hC:t:p:P:alqvfs:H:M:n:o:iTr.::", options, NULL)) >= 0)
35,754✔
563

564
                switch (c) {
13,292✔
565

566
                case 'h':
×
567
                        return systemctl_help();
×
568

569
                case ARG_VERSION:
7✔
570
                        return version();
7✔
571

572
                case 't':
54✔
573
                        if (isempty(optarg))
54✔
574
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
575
                                                       "--type= requires arguments.");
576

577
                        for (const char *p = optarg;;) {
54✔
578
                                _cleanup_free_ char *type = NULL;
57✔
579

580
                                r = extract_first_word(&p, &type, ",", 0);
110✔
581
                                if (r < 0)
110✔
582
                                        return log_error_errno(r, "Failed to parse type: %s", optarg);
×
583
                                if (r == 0)
110✔
584
                                        break;
585

586
                                if (streq(type, "help")) {
57✔
587
                                        help_types();
1✔
588
                                        return 0;
589
                                }
590

591
                                if (unit_type_from_string(type) >= 0) {
56✔
592
                                        if (strv_consume(&arg_types, TAKE_PTR(type)) < 0)
54✔
593
                                                return log_oom();
×
594
                                        continue;
54✔
595
                                }
596

597
                                /* It's much nicer to use --state= for load states, but let's support this in
598
                                 * --types= too for compatibility with old versions */
599
                                if (unit_load_state_from_string(type) >= 0) {
2✔
600
                                        if (strv_consume(&arg_states, TAKE_PTR(type)) < 0)
2✔
601
                                                return log_oom();
×
602
                                        continue;
2✔
603
                                }
604

605
                                log_error("Unknown unit type or load state '%s'.", type);
×
606
                                return log_info_errno(SYNTHETIC_ERRNO(EINVAL),
×
607
                                                      "Use -t help to see a list of allowed values.");
608
                        }
609

610
                        break;
53✔
611

612
                case 'P':
239✔
613
                        SET_FLAG(arg_print_flags, BUS_PRINT_PROPERTY_ONLY_VALUE, true);
239✔
614
                        _fallthrough_;
5,386✔
615

616
                case 'p':
5,386✔
617
                        /* Make sure that if the empty property list was specified, we won't show any
618
                           properties. */
619
                        if (isempty(optarg) && !arg_properties) {
5,387✔
620
                                arg_properties = new0(char*, 1);
1✔
621
                                if (!arg_properties)
1✔
622
                                        return log_oom();
×
623
                        } else
624
                                for (const char *p = optarg;;) {
5,385✔
625
                                        _cleanup_free_ char *prop = NULL;
10,780✔
626

627
                                        r = extract_first_word(&p, &prop, ",", 0);
10,780✔
628
                                        if (r < 0)
10,780✔
629
                                                return log_error_errno(r, "Failed to parse property: %s", optarg);
×
630
                                        if (r == 0)
10,780✔
631
                                                break;
632

633
                                        if (strv_consume(&arg_properties, TAKE_PTR(prop)) < 0)
5,395✔
634
                                                return log_oom();
×
635
                                }
636

637
                        /* If the user asked for a particular property, show it, even if it is empty. */
638
                        SET_FLAG(arg_print_flags, BUS_PRINT_PROPERTY_SHOW_EMPTY, true);
5,386✔
639

640
                        break;
5,386✔
641

642
                case 'a':
54✔
643
                        SET_FLAG(arg_print_flags, BUS_PRINT_PROPERTY_SHOW_EMPTY, true);
54✔
644
                        arg_all = true;
54✔
645
                        break;
54✔
646

647
                case ARG_REVERSE:
3✔
648
                        arg_dependency = DEPENDENCY_REVERSE;
3✔
649
                        break;
3✔
650

651
                case ARG_AFTER:
5✔
652
                        arg_dependency = DEPENDENCY_AFTER;
5✔
653
                        arg_jobs_after = true;
5✔
654
                        break;
5✔
655

656
                case ARG_BEFORE:
5✔
657
                        arg_dependency = DEPENDENCY_BEFORE;
5✔
658
                        arg_jobs_before = true;
5✔
659
                        break;
5✔
660

661
                case ARG_SHOW_TYPES:
1✔
662
                        arg_show_types = true;
1✔
663
                        break;
1✔
664

665
                case ARG_VALUE:
5,041✔
666
                        SET_FLAG(arg_print_flags, BUS_PRINT_PROPERTY_ONLY_VALUE, true);
5,041✔
667
                        break;
5,041✔
668

669
                case ARG_JOB_MODE:
10✔
670
                        _arg_job_mode = optarg;
10✔
671
                        break;
10✔
672

673
                case ARG_FAIL:
×
674
                        _arg_job_mode = "fail";
×
675
                        break;
×
676

677
                case ARG_IRREVERSIBLE:
×
678
                        _arg_job_mode = "replace-irreversibly";
×
679
                        break;
×
680

681
                case ARG_IGNORE_DEPENDENCIES:
×
682
                        _arg_job_mode = "ignore-dependencies";
×
683
                        break;
×
684

685
                case ARG_USER:
197✔
686
                        arg_runtime_scope = RUNTIME_SCOPE_USER;
197✔
687
                        break;
197✔
688

689
                case ARG_SYSTEM:
×
690
                        arg_runtime_scope = RUNTIME_SCOPE_SYSTEM;
×
691
                        break;
×
692

693
                case ARG_GLOBAL:
×
694
                        arg_runtime_scope = RUNTIME_SCOPE_GLOBAL;
×
695
                        break;
×
696

697
                case 'C':
3✔
698
                        r = capsule_name_is_valid(optarg);
3✔
699
                        if (r < 0)
3✔
700
                                return log_error_errno(r, "Unable to validate capsule name '%s': %m", optarg);
×
701
                        if (r == 0)
3✔
702
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid capsule name: %s", optarg);
×
703

704
                        arg_host = optarg;
3✔
705
                        arg_transport = BUS_TRANSPORT_CAPSULE;
3✔
706
                        arg_runtime_scope = RUNTIME_SCOPE_USER;
3✔
707
                        break;
3✔
708

709
                case ARG_WAIT:
14✔
710
                        arg_wait = true;
14✔
711
                        break;
14✔
712

713
                case ARG_NO_BLOCK:
104✔
714
                        arg_no_block = true;
104✔
715
                        break;
104✔
716

717
                case ARG_NO_LEGEND:
54✔
718
                        arg_legend = false;
54✔
719
                        break;
54✔
720

721
                case ARG_LEGEND:
3✔
722
                        r = parse_boolean_argument("--legend", optarg, NULL);
3✔
723
                        if (r < 0)
3✔
724
                                return r;
725
                        arg_legend = r;
3✔
726
                        break;
3✔
727

728
                case ARG_NO_PAGER:
7✔
729
                        arg_pager_flags |= PAGER_DISABLE;
7✔
730
                        break;
7✔
731

732
                case ARG_NO_WALL:
3✔
733
                        arg_no_wall = true;
3✔
734
                        break;
3✔
735

736
                case ARG_ROOT:
41✔
737
                        r = parse_path_argument(optarg, false, &arg_root);
41✔
738
                        if (r < 0)
41✔
739
                                return r;
740
                        break;
741

742
                case ARG_IMAGE:
×
743
                        r = parse_path_argument(optarg, false, &arg_image);
×
744
                        if (r < 0)
×
745
                                return r;
746
                        break;
747

748
                case ARG_IMAGE_POLICY:
×
749
                        r = parse_image_policy_argument(optarg, &arg_image_policy);
×
750
                        if (r < 0)
×
751
                                return r;
752
                        break;
753

754
                case 'l':
5✔
755
                        arg_full = true;
5✔
756
                        break;
5✔
757

758
                case ARG_FAILED:
759
                        if (strv_extend(&arg_states, "failed") < 0)
1✔
760
                                return log_oom();
×
761

762
                        break;
763

764
                case ARG_DRY_RUN:
23✔
765
                        arg_dry_run = true;
23✔
766
                        break;
23✔
767

768
                case 'q':
2,081✔
769
                        arg_quiet = true;
2,081✔
770

771
                        if (arg_legend < 0)
2,081✔
772
                                arg_legend = false;
2,081✔
773

774
                        break;
775

776
                case 'v':
×
777
                        arg_verbose = true;
×
778
                        break;
×
779

780
                case 'f':
13✔
781
                        arg_force++;
13✔
782
                        break;
13✔
783

784
                case ARG_NO_RELOAD:
×
785
                        arg_no_reload = true;
×
786
                        break;
×
787

788
                case ARG_KILL_WHOM:
9✔
789
                        arg_kill_whom = optarg;
9✔
790
                        break;
9✔
791

792
                case ARG_KILL_VALUE: {
6✔
793
                        unsigned u;
6✔
794

795
                        if (isempty(optarg)) {
6✔
796
                                arg_kill_value_set = false;
×
797
                                return 0;
×
798
                        }
799

800
                        /* First, try to parse unsigned, so that we can support the prefixes 0x, 0o, 0b */
801
                        r = safe_atou_full(optarg, 0, &u);
6✔
802
                        if (r < 0)
6✔
803
                                /* If this didn't work, try as signed integer, without those prefixes */
804
                                r = safe_atoi(optarg, &arg_kill_value);
×
805
                        else if (u > INT_MAX)
6✔
806
                                r = -ERANGE;
807
                        else
808
                                arg_kill_value = (int) u;
6✔
809
                        if (r < 0)
6✔
810
                                return log_error_errno(r, "Unable to parse signal queue value: %s", optarg);
×
811

812
                        arg_kill_value_set = true;
6✔
813
                        break;
6✔
814
                }
815

816
                case 's':
22✔
817
                        r = parse_signal_argument(optarg, &arg_signal);
22✔
818
                        if (r <= 0)
22✔
819
                                return r;
820
                        break;
821

822
                case ARG_NO_ASK_PASSWORD:
×
823
                        arg_ask_password = false;
×
824
                        break;
×
825

826
                case 'H':
×
827
                        arg_transport = BUS_TRANSPORT_REMOTE;
×
828
                        arg_host = optarg;
×
829
                        break;
×
830

831
                case 'M':
21✔
832
                        arg_transport = BUS_TRANSPORT_MACHINE;
21✔
833
                        arg_host = optarg;
21✔
834
                        break;
21✔
835

836
                case ARG_RUNTIME:
16✔
837
                        arg_runtime = true;
16✔
838
                        break;
16✔
839

840
                case 'n':
9✔
841
                        if (safe_atou(optarg, &arg_lines) < 0)
9✔
842
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
843
                                                       "Failed to parse lines '%s'",
844
                                                       optarg);
845
                        break;
846

847
                case 'o':
1✔
848
                        if (streq(optarg, "help")) {
1✔
849
                                DUMP_STRING_TABLE(output_mode, OutputMode, _OUTPUT_MODE_MAX);
×
850
                                return 0;
×
851
                        }
852

853
                        arg_output = output_mode_from_string(optarg);
1✔
854
                        if (arg_output < 0)
1✔
855
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
856
                                                       "Unknown output '%s'.",
857
                                                       optarg);
858

859
                        if (OUTPUT_MODE_IS_JSON(arg_output)) {
24,519✔
860
                                arg_legend = false;
1✔
861
                                arg_plain = true;
1✔
862
                        }
863
                        break;
864

865
                case 'i':
×
866
                        arg_check_inhibitors = 0;
×
867
                        break;
×
868

869
                case ARG_CHECK_INHIBITORS:
×
870
                        r = parse_tristate_full(optarg, "auto", &arg_check_inhibitors);
×
871
                        if (r < 0)
×
872
                                return log_error_errno(r, "Failed to parse --check-inhibitors= argument: %s", optarg);
×
873
                        break;
874

875
                case ARG_PLAIN:
1✔
876
                        arg_plain = true;
1✔
877
                        break;
1✔
878

879
                case ARG_FIRMWARE_SETUP:
×
880
                        arg_firmware_setup = true;
×
881
                        break;
×
882

883
                case ARG_BOOT_LOADER_MENU:
×
884

885
                        r = parse_sec(optarg, &arg_boot_loader_menu);
×
886
                        if (r < 0)
×
887
                                return log_error_errno(r, "Failed to parse --boot-loader-menu= argument '%s': %m", optarg);
×
888

889
                        break;
890

891
                case ARG_BOOT_LOADER_ENTRY:
×
892

893
                        if (streq(optarg, "help")) { /* Yes, this means, "help" is not a valid boot loader entry name we can deal with */
×
894
                                r = help_boot_loader_entry();
×
895
                                if (r < 0)
×
896
                                        return r;
897

898
                                return 0;
×
899
                        }
900

901
                        arg_boot_loader_entry = empty_to_null(optarg);
×
902
                        break;
×
903

904
                case ARG_STATE:
10✔
905
                        if (isempty(optarg))
10✔
906
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
907
                                                       "--state= requires arguments.");
908

909
                        for (const char *p = optarg;;) {
10✔
910
                                _cleanup_free_ char *s = NULL;
12✔
911

912
                                r = extract_first_word(&p, &s, ",", 0);
21✔
913
                                if (r < 0)
21✔
914
                                        return log_error_errno(r, "Failed to parse state: %s", optarg);
×
915
                                if (r == 0)
21✔
916
                                        break;
917

918
                                if (streq(s, "help")) {
12✔
919
                                        help_states();
1✔
920
                                        return 0;
921
                                }
922

923
                                if (strv_consume(&arg_states, TAKE_PTR(s)) < 0)
11✔
924
                                        return log_oom();
×
925
                        }
926
                        break;
9✔
927

928
                case 'r':
1✔
929
                        if (geteuid() != 0)
1✔
930
                                return log_error_errno(SYNTHETIC_ERRNO(EPERM),
×
931
                                                       "--recursive requires root privileges.");
932

933
                        arg_recursive = true;
1✔
934
                        break;
1✔
935

936
                case ARG_PRESET_MODE:
4✔
937
                        if (streq(optarg, "help")) {
4✔
938
                                DUMP_STRING_TABLE(unit_file_preset_mode, UnitFilePresetMode, _UNIT_FILE_PRESET_MODE_MAX);
×
939
                                return 0;
×
940
                        }
941

942
                        arg_preset_mode = unit_file_preset_mode_from_string(optarg);
4✔
943
                        if (arg_preset_mode < 0)
4✔
944
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
945
                                                       "Failed to parse preset mode: %s.", optarg);
946

947
                        break;
948

949
                case ARG_NOW:
8✔
950
                        arg_now = true;
8✔
951
                        break;
8✔
952

953
                case ARG_MESSAGE:
3✔
954
                        if (strv_extend(&arg_wall, optarg) < 0)
3✔
955
                                return log_oom();
×
956
                        break;
957

958
                case 'T':
10✔
959
                        arg_show_transaction = true;
10✔
960
                        break;
10✔
961

962
                case ARG_WITH_DEPENDENCIES:
3✔
963
                        arg_with_dependencies = true;
3✔
964
                        break;
3✔
965

966
                case ARG_WHAT:
20✔
967
                        if (isempty(optarg))
20✔
968
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--what= requires arguments (see --what=help).");
×
969

970
                        for (const char *p = optarg;;) {
20✔
971
                                _cleanup_free_ char *k = NULL;
20✔
972

973
                                r = extract_first_word(&p, &k, ",", 0);
40✔
974
                                if (r < 0)
40✔
975
                                        return log_error_errno(r, "Failed to parse directory type: %s", optarg);
×
976
                                if (r == 0)
40✔
977
                                        break;
978

979
                                if (streq(k, "help")) {
20✔
980
                                        puts("runtime\n"
×
981
                                             "state\n"
982
                                             "cache\n"
983
                                             "logs\n"
984
                                             "configuration\n"
985
                                             "fdstore\n"
986
                                             "all");
987
                                        return 0;
988
                                }
989

990
                                r = strv_consume(&arg_clean_what, TAKE_PTR(k));
20✔
991
                                if (r < 0)
20✔
992
                                        return log_oom();
×
993
                        }
994

995
                        break;
20✔
996

997
                case ARG_REBOOT_ARG:
×
998
                        arg_reboot_argument = optarg;
×
999
                        break;
×
1000

1001
                case ARG_TIMESTAMP_STYLE:
6✔
1002
                        if (streq(optarg, "help")) {
6✔
1003
                                DUMP_STRING_TABLE(timestamp_style, TimestampStyle, _TIMESTAMP_STYLE_MAX);
×
1004
                                return 0;
×
1005
                        }
1006

1007
                        arg_timestamp_style = timestamp_style_from_string(optarg);
6✔
1008
                        if (arg_timestamp_style < 0)
6✔
1009
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1010
                                                       "Invalid value: %s.", optarg);
1011

1012
                        break;
1013

1014
                case ARG_READ_ONLY:
×
1015
                        arg_read_only = true;
×
1016
                        break;
×
1017

1018
                case ARG_MKDIR:
6✔
1019
                        arg_mkdir = true;
6✔
1020
                        break;
6✔
1021

1022
                case ARG_MARKED:
1✔
1023
                        arg_marked = true;
1✔
1024
                        break;
1✔
1025

1026
                case ARG_NO_WARN:
×
1027
                        arg_no_warn = true;
×
1028
                        break;
×
1029

1030
                case ARG_DROP_IN:
9✔
1031
                        arg_drop_in = optarg;
9✔
1032
                        break;
9✔
1033

1034
                case ARG_WHEN:
×
1035
                        if (streq(optarg, "show")) {
×
1036
                                arg_action = ACTION_SYSTEMCTL_SHOW_SHUTDOWN;
×
1037
                                return 1;
×
1038
                        }
1039

1040
                        if (STR_IN_SET(optarg, "", "cancel")) {
×
1041
                                arg_action = ACTION_CANCEL_SHUTDOWN;
×
1042
                                return 1;
×
1043
                        }
1044

1045
                        if (streq(optarg, "auto")) {
×
1046
                                arg_when = USEC_INFINITY; /* logind chooses on server side */
×
1047
                                break;
×
1048
                        }
1049

1050
                        r = parse_timestamp(optarg, &arg_when);
×
1051
                        if (r < 0)
×
1052
                                return log_error_errno(r, "Failed to parse --when= argument '%s': %m", optarg);
×
1053

1054
                        if (!timestamp_is_set(arg_when))
×
1055
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1056
                                                       "Invalid timestamp '%s' specified for --when=.", optarg);
1057

1058
                        break;
1059

1060
                case ARG_STDIN:
11✔
1061
                        arg_stdin = true;
11✔
1062
                        break;
11✔
1063

1064
                case '.':
1065
                        /* Output an error mimicking getopt, and print a hint afterwards */
1066
                        log_error("%s: invalid option -- '.'", program_invocation_name);
×
1067
                        log_notice("Hint: to specify units starting with a dash, use \"--\":\n"
×
1068
                                   "      %s [OPTIONS...] COMMAND -- -.%s ...",
1069
                                   program_invocation_name, optarg ?: "mount");
1070
                        _fallthrough_;
1071

1072
                case '?':
1073
                        return -EINVAL;
1074

1075
                default:
×
1076
                        assert_not_reached();
×
1077
                }
1078

1079
        /* If we are in --user mode, there's no point in talking to PolicyKit or the infra to query system
1080
         * passwords */
1081
        if (arg_runtime_scope != RUNTIME_SCOPE_SYSTEM)
11,226✔
1082
                arg_ask_password = false;
200✔
1083

1084
        if (arg_transport == BUS_TRANSPORT_REMOTE && arg_runtime_scope != RUNTIME_SCOPE_SYSTEM)
11,226✔
1085
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1086
                                       "Cannot access user instance remotely.");
1087

1088
        if (arg_transport == BUS_TRANSPORT_CAPSULE && arg_runtime_scope != RUNTIME_SCOPE_USER)
11,226✔
1089
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1090
                                       "Cannot access system instance with --capsule=/-C.");
1091

1092
        if (arg_wait && arg_no_block)
11,226✔
1093
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1094
                                       "--wait may not be combined with --no-block.");
1095

1096
        bool do_reload_or_restart = streq_ptr(argv[optind], "reload-or-restart");
11,226✔
1097
        if (arg_marked) {
11,226✔
1098
                if (!do_reload_or_restart)
1✔
1099
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1100
                                               "--marked may only be used with 'reload-or-restart'.");
1101
                if (optind + 1 < argc)
1✔
1102
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1103
                                               "No additional arguments allowed with 'reload-or-restart --marked'.");
1104
                if (arg_wait)
1✔
1105
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1106
                                               "--marked --wait is not supported.");
1107
                if (arg_show_transaction)
1✔
1108
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1109
                                               "--marked --show-transaction is not supported.");
1110

1111
        } else if (do_reload_or_restart) {
11,225✔
1112
                if (optind + 1 >= argc)
×
1113
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1114
                                               "List of units to restart/reload is required.");
1115
        }
1116

1117
        if (arg_image && arg_root)
11,226✔
1118
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1119
                                       "Please specify either --root= or --image=, the combination of both is not supported.");
1120

1121
        return 1;
1122
}
1123

1124
int systemctl_dispatch_parse_argv(int argc, char *argv[]) {
11,248✔
1125
        assert(argc >= 0);
11,248✔
1126
        assert(argv);
11,248✔
1127

1128
        if (invoked_as(argv, "halt")) {
11,248✔
1129
                arg_action = ACTION_HALT;
×
1130
                return halt_parse_argv(argc, argv);
×
1131

1132
        } else if (invoked_as(argv, "poweroff")) {
11,248✔
1133
                arg_action = ACTION_POWEROFF;
×
1134
                return halt_parse_argv(argc, argv);
×
1135

1136
        } else if (invoked_as(argv, "reboot")) {
11,248✔
1137
                arg_action = ACTION_REBOOT;
5✔
1138
                return halt_parse_argv(argc, argv);
5✔
1139

1140
        } else if (invoked_as(argv, "shutdown")) {
11,243✔
1141
                arg_action = ACTION_POWEROFF;
7✔
1142
                return shutdown_parse_argv(argc, argv);
7✔
1143

1144
        } else if (invoked_as(argv, "init")) {
11,236✔
1145

1146
                /* Matches invocations as "init" as well as "telinit", which are synonymous when run
1147
                 * as PID != 1 on SysV.
1148
                 *
1149
                 * On SysV "telinit" was the official command to communicate with PID 1, but "init" would
1150
                 * redirect itself to "telinit" if called with PID != 1. We follow the same logic here still,
1151
                 * though we add one level of indirection, as we implement "telinit" in "systemctl". Hence,
1152
                 * for us if you invoke "init" you get "systemd", but it will execve() "systemctl"
1153
                 * immediately with argv[] unmodified if PID is != 1. If you invoke "telinit" you directly
1154
                 * get "systemctl". In both cases we shall do the same thing, which is why we do
1155
                 * invoked_as(argv, "init") here, as a quick way to match both.
1156
                 *
1157
                 * Also see redirect_telinit() in src/core/main.c. */
1158

1159
                arg_action = _ACTION_INVALID; /* telinit_parse_argv() will figure out the actual action we'll execute */
×
1160
                return telinit_parse_argv(argc, argv);
×
1161

1162
        } else if (invoked_as(argv, "runlevel")) {
11,236✔
1163
                arg_action = ACTION_RUNLEVEL;
×
1164
                return runlevel_parse_argv(argc, argv);
×
1165
        }
1166

1167
        arg_action = ACTION_SYSTEMCTL;
11,236✔
1168
        return systemctl_parse_argv(argc, argv);
11,236✔
1169
}
1170

1171
#ifndef FUZZ_SYSTEMCTL_PARSE_ARGV
1172
static int systemctl_main(int argc, char *argv[]) {
11,226✔
1173
        static const Verb verbs[] = {
11,226✔
1174
                { "list-units",            VERB_ANY, VERB_ANY, VERB_DEFAULT|VERB_ONLINE_ONLY, verb_list_units },
1175
                { "list-unit-files",       VERB_ANY, VERB_ANY, 0,                verb_list_unit_files         },
1176
                { "list-automounts",       VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, verb_list_automounts         },
1177
                { "list-paths",            VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, verb_list_paths              },
1178
                { "list-sockets",          VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, verb_list_sockets            },
1179
                { "list-timers",           VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, verb_list_timers             },
1180
                { "list-jobs",             VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, verb_list_jobs               },
1181
                { "list-machines",         VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, verb_list_machines           },
1182
                { "clear-jobs",            VERB_ANY, 1,        VERB_ONLINE_ONLY, verb_trivial_method          },
1183
                { "cancel",                VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, verb_cancel                  },
1184
                { "start",                 2,        VERB_ANY, VERB_ONLINE_ONLY, verb_start                   },
1185
                { "stop",                  2,        VERB_ANY, VERB_ONLINE_ONLY, verb_start                   },
1186
                { "condstop",              2,        VERB_ANY, VERB_ONLINE_ONLY, verb_start                   }, /* For compatibility with ALTLinux */
1187
                { "reload",                2,        VERB_ANY, VERB_ONLINE_ONLY, verb_start                   },
1188
                { "restart",               2,        VERB_ANY, VERB_ONLINE_ONLY, verb_start                   },
1189
                { "try-restart",           2,        VERB_ANY, VERB_ONLINE_ONLY, verb_start                   },
1190
                { "reload-or-restart",     VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, verb_start                   },
1191
                { "reload-or-try-restart", 2,        VERB_ANY, VERB_ONLINE_ONLY, verb_start                   }, /* For compatibility with old systemctl <= 228 */
1192
                { "try-reload-or-restart", 2,        VERB_ANY, VERB_ONLINE_ONLY, verb_start                   },
1193
                { "force-reload",          2,        VERB_ANY, VERB_ONLINE_ONLY, verb_start                   }, /* For compatibility with SysV */
1194
                { "condreload",            2,        VERB_ANY, VERB_ONLINE_ONLY, verb_start                   }, /* For compatibility with ALTLinux */
1195
                { "condrestart",           2,        VERB_ANY, VERB_ONLINE_ONLY, verb_start                   }, /* For compatibility with RH */
1196
                { "isolate",               2,        2,        VERB_ONLINE_ONLY, verb_start                   },
1197
                { "kill",                  2,        VERB_ANY, VERB_ONLINE_ONLY, verb_kill                    },
1198
                { "clean",                 2,        VERB_ANY, VERB_ONLINE_ONLY, verb_clean_or_freeze         },
1199
                { "freeze",                2,        VERB_ANY, VERB_ONLINE_ONLY, verb_clean_or_freeze         },
1200
                { "thaw",                  2,        VERB_ANY, VERB_ONLINE_ONLY, verb_clean_or_freeze         },
1201
                { "is-active",             2,        VERB_ANY, VERB_ONLINE_ONLY, verb_is_active               },
1202
                { "check",                 2,        VERB_ANY, VERB_ONLINE_ONLY, verb_is_active               }, /* deprecated alias of is-active */
1203
                { "is-failed",             VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, verb_is_failed               },
1204
                { "show",                  VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, verb_show                    },
1205
                { "cat",                   2,        VERB_ANY, VERB_ONLINE_ONLY, verb_cat                     },
1206
                { "status",                VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, verb_show                    },
1207
                { "help",                  VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, verb_show                    },
1208
                { "daemon-reload",         1,        1,        VERB_ONLINE_ONLY, verb_daemon_reload           },
1209
                { "daemon-reexec",         1,        1,        VERB_ONLINE_ONLY, verb_daemon_reload           },
1210
                { "log-level",             VERB_ANY, 2,        VERB_ONLINE_ONLY, verb_log_setting             },
1211
                { "log-target",            VERB_ANY, 2,        VERB_ONLINE_ONLY, verb_log_setting             },
1212
                { "service-log-level",     2,        3,        VERB_ONLINE_ONLY, verb_service_log_setting     },
1213
                { "service-log-target",    2,        3,        VERB_ONLINE_ONLY, verb_service_log_setting     },
1214
                { "service-watchdogs",     VERB_ANY, 2,        VERB_ONLINE_ONLY, verb_service_watchdogs       },
1215
                { "show-environment",      VERB_ANY, 1,        VERB_ONLINE_ONLY, verb_show_environment        },
1216
                { "set-environment",       2,        VERB_ANY, VERB_ONLINE_ONLY, verb_set_environment         },
1217
                { "unset-environment",     2,        VERB_ANY, VERB_ONLINE_ONLY, verb_set_environment         },
1218
                { "import-environment",    VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, verb_import_environment      },
1219
                { "halt",                  VERB_ANY, 1,        VERB_ONLINE_ONLY, verb_start_system_special    },
1220
                { "poweroff",              VERB_ANY, 1,        VERB_ONLINE_ONLY, verb_start_system_special    },
1221
                { "reboot",                VERB_ANY, 1,        VERB_ONLINE_ONLY, verb_start_system_special    },
1222
                { "kexec",                 VERB_ANY, 1,        VERB_ONLINE_ONLY, verb_start_system_special    },
1223
                { "soft-reboot",           VERB_ANY, 1,        VERB_ONLINE_ONLY, verb_start_system_special    },
1224
                { "sleep",                 VERB_ANY, 1,        VERB_ONLINE_ONLY, verb_start_system_special    },
1225
                { "suspend",               VERB_ANY, 1,        VERB_ONLINE_ONLY, verb_start_system_special    },
1226
                { "hibernate",             VERB_ANY, 1,        VERB_ONLINE_ONLY, verb_start_system_special    },
1227
                { "hybrid-sleep",          VERB_ANY, 1,        VERB_ONLINE_ONLY, verb_start_system_special    },
1228
                { "suspend-then-hibernate",VERB_ANY, 1,        VERB_ONLINE_ONLY, verb_start_system_special    },
1229
                { "default",               VERB_ANY, 1,        VERB_ONLINE_ONLY, verb_start_special           },
1230
                { "rescue",                VERB_ANY, 1,        VERB_ONLINE_ONLY, verb_start_system_special    },
1231
                { "emergency",             VERB_ANY, 1,        VERB_ONLINE_ONLY, verb_start_system_special    },
1232
                { "exit",                  VERB_ANY, 2,        VERB_ONLINE_ONLY, verb_start_special           },
1233
                { "reset-failed",          VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, verb_reset_failed            },
1234
                { "enable",                2,        VERB_ANY, 0,                verb_enable                  },
1235
                { "disable",               2,        VERB_ANY, 0,                verb_enable                  },
1236
                { "is-enabled",            2,        VERB_ANY, 0,                verb_is_enabled              },
1237
                { "reenable",              2,        VERB_ANY, 0,                verb_enable                  },
1238
                { "preset",                2,        VERB_ANY, 0,                verb_enable                  },
1239
                { "preset-all",            VERB_ANY, 1,        0,                verb_preset_all              },
1240
                { "mask",                  2,        VERB_ANY, 0,                verb_enable                  },
1241
                { "unmask",                2,        VERB_ANY, 0,                verb_enable                  },
1242
                { "link",                  2,        VERB_ANY, 0,                verb_enable                  },
1243
                { "revert",                2,        VERB_ANY, 0,                verb_enable                  },
1244
                { "switch-root",           VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, verb_switch_root             },
1245
                { "list-dependencies",     VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, verb_list_dependencies       },
1246
                { "set-default",           2,        2,        0,                verb_set_default             },
1247
                { "get-default",           VERB_ANY, 1,        0,                verb_get_default             },
1248
                { "set-property",          3,        VERB_ANY, VERB_ONLINE_ONLY, verb_set_property            },
1249
                { "is-system-running",     VERB_ANY, 1,        0,                verb_is_system_running       },
1250
                { "add-wants",             3,        VERB_ANY, 0,                verb_add_dependency          },
1251
                { "add-requires",          3,        VERB_ANY, 0,                verb_add_dependency          },
1252
                { "edit",                  2,        VERB_ANY, VERB_ONLINE_ONLY, verb_edit                    },
1253
                { "bind",                  3,        4,        VERB_ONLINE_ONLY, verb_bind                    },
1254
                { "mount-image",           4,        5,        VERB_ONLINE_ONLY, verb_mount_image             },
1255
                { "whoami",                VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, verb_whoami                  },
1256
                {}
1257
        };
1258

1259
        const Verb *verb = verbs_find_verb(argv[optind], verbs);
11,226✔
1260
        if (verb && (verb->flags & VERB_ONLINE_ONLY) && arg_root)
11,226✔
1261
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1262
                                       "Verb '%s' cannot be used with --root= or --image=.",
1263
                                       argv[optind] ?: verb->verb);
1264

1265
        return dispatch_verb(argc, argv, verbs, NULL);
11,226✔
1266
}
1267

1268
static int run(int argc, char *argv[]) {
11,248✔
1269
        _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
11,248✔
1270
        _cleanup_(umount_and_freep) char *mounted_dir = NULL;
11,248✔
1271
        int r;
11,248✔
1272

1273
        setlocale(LC_ALL, "");
11,248✔
1274
        log_setup();
11,248✔
1275

1276
        r = systemctl_dispatch_parse_argv(argc, argv);
11,248✔
1277
        if (r <= 0)
11,248✔
1278
                goto finish;
10✔
1279

1280
        journal_browse_prepare();
11,238✔
1281

1282
        if (proc_mounted() == 0)
11,238✔
1283
                log_full(arg_no_warn ? LOG_DEBUG : LOG_WARNING,
×
1284
                         "%s%s/proc/ is not mounted. This is not a supported mode of operation. Please fix\n"
1285
                         "your invocation environment to mount /proc/ and /sys/ properly. Proceeding anyway.\n"
1286
                         "Your mileage may vary.",
1287
                         optional_glyph(GLYPH_WARNING_SIGN),
1288
                         optional_glyph(GLYPH_SPACE));
1289

1290
        if (arg_action != ACTION_SYSTEMCTL && running_in_chroot() > 0) {
11,238✔
1291
                if (!arg_quiet)
×
1292
                        log_info("Running in chroot, ignoring request.");
×
1293
                r = 0;
×
1294
                goto finish;
×
1295
        }
1296

1297
        /* systemctl_main() will print an error message for the bus connection, but only if it needs to */
1298

1299
        if (arg_image) {
11,238✔
1300
                assert(!arg_root);
×
1301

1302
                r = mount_image_privately_interactively(
×
1303
                                arg_image,
1304
                                arg_image_policy,
1305
                                DISSECT_IMAGE_GENERIC_ROOT |
1306
                                DISSECT_IMAGE_REQUIRE_ROOT |
1307
                                DISSECT_IMAGE_RELAX_VAR_CHECK |
1308
                                DISSECT_IMAGE_VALIDATE_OS |
1309
                                DISSECT_IMAGE_ALLOW_USERSPACE_VERITY,
1310
                                &mounted_dir,
1311
                                /* ret_dir_fd= */ NULL,
1312
                                &loop_device);
1313
                if (r < 0)
×
1314
                        return r;
1315

1316
                arg_root = strdup(mounted_dir);
×
1317
                if (!arg_root)
×
1318
                        return log_oom();
×
1319
        }
1320

1321
        switch (arg_action) {
11,238✔
1322

1323
        case ACTION_SYSTEMCTL:
11,226✔
1324
                r = systemctl_main(argc, argv);
11,226✔
1325
                break;
1326

1327
        /* Legacy command aliases set arg_action. They provide some fallbacks, e.g. to tell sysvinit to
1328
         * reboot after you have installed systemd binaries. */
1329

1330
        case ACTION_HALT:
8✔
1331
        case ACTION_POWEROFF:
1332
        case ACTION_REBOOT:
1333
        case ACTION_KEXEC:
1334
                r = halt_main();
8✔
1335
                break;
1336

1337
        case ACTION_RUNLEVEL2:
×
1338
        case ACTION_RUNLEVEL3:
1339
        case ACTION_RUNLEVEL4:
1340
        case ACTION_RUNLEVEL5:
1341
        case ACTION_RESCUE:
1342
                r = start_with_fallback();
×
1343
                break;
1344

1345
        case ACTION_RELOAD:
×
1346
        case ACTION_REEXEC:
1347
                r = reload_with_fallback();
×
1348
                break;
1349

1350
        case ACTION_CANCEL_SHUTDOWN:
3✔
1351
                r = logind_cancel_shutdown();
3✔
1352
                break;
1353

1354
        case ACTION_SHOW_SHUTDOWN:
1✔
1355
        case ACTION_SYSTEMCTL_SHOW_SHUTDOWN:
1356
                r = logind_show_shutdown();
1✔
1357
                break;
1358

1359
        case ACTION_RUNLEVEL:
×
1360
                r = runlevel_main();
×
1361
                break;
1362

1363
        case ACTION_EXIT:
×
1364
        case ACTION_SLEEP:
1365
        case ACTION_SUSPEND:
1366
        case ACTION_HIBERNATE:
1367
        case ACTION_HYBRID_SLEEP:
1368
        case ACTION_SUSPEND_THEN_HIBERNATE:
1369
        case ACTION_EMERGENCY:
1370
        case ACTION_DEFAULT:
1371
                /* systemctl verbs with no equivalent in the legacy commands. These cannot appear in
1372
                 * arg_action. Fall through. */
1373

1374
        case _ACTION_INVALID:
1375
        default:
1376
                assert_not_reached();
×
1377
        }
1378

1379
finish:
11,248✔
1380
        release_busses();
11,248✔
1381

1382
        /* Note that we return r here, not 0, so that we can implement the LSB-like return codes */
1383
        return r;
1384
}
1385

1386
DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);
11,312✔
1387
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc