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

systemd / systemd / 14766779411

30 Apr 2025 04:55PM UTC coverage: 72.225% (-0.06%) from 72.282%
14766779411

push

github

web-flow
wait-online: handle varlink connection errors while waiting for DNS (#37283)

Currently, if systemd-networkd-wait-online is started with --dns, and
systemd-resolved is not running, it will exit with an error right away.
Similarly, if systemd-resolved is restarted while waiting for DNS
configuration, systemd-networkd-wait-online will not attempt to
re-connect, and will potentially never see subsequent DNS
configurations.

Improve this by adding socket units for the systemd-resolved varlink
servers, and re-establish the connection in systemd-networkd-wait-online
when we receive `SD_VARLINK_ERROR_DISCONNECTED`.

8 of 16 new or added lines in 2 files covered. (50.0%)

5825 existing lines in 217 files now uncovered.

297168 of 411450 relevant lines covered (72.22%)

695892.62 hits per line

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

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

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

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

145
static int systemctl_help(void) {
×
146
        _cleanup_free_ char *link = NULL;
×
147
        int r;
×
148

149
        pager_open(arg_pager_flags);
×
150

151
        r = terminal_urlify_man("systemctl", "1", &link);
×
152
        if (r < 0)
×
153
                return log_oom();
×
154

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

359
        return 0;
360
}
361

362
static void help_types(void) {
1✔
363
        if (arg_legend != 0)
1✔
364
                puts("Available unit types:");
1✔
365

366
        DUMP_STRING_TABLE(unit_type, UnitType, _UNIT_TYPE_MAX);
12✔
367
}
1✔
368

369
static void help_states(void) {
1✔
370
        if (arg_legend != 0)
1✔
371
                puts("Available unit load states:");
1✔
372
        DUMP_STRING_TABLE(unit_load_state, UnitLoadState, _UNIT_LOAD_STATE_MAX);
8✔
373

374
        if (arg_legend != 0)
1✔
375
                puts("\nAvailable unit active states:");
1✔
376
        DUMP_STRING_TABLE(unit_active_state, UnitActiveState, _UNIT_ACTIVE_STATE_MAX);
9✔
377

378
        if (arg_legend != 0)
1✔
379
                puts("\nAvailable unit file states:");
1✔
380
        DUMP_STRING_TABLE(unit_file_state, UnitFileState, _UNIT_FILE_STATE_MAX);
14✔
381

382
        if (arg_legend != 0)
1✔
383
                puts("\nAvailable automount unit substates:");
1✔
384
        DUMP_STRING_TABLE(automount_state, AutomountState, _AUTOMOUNT_STATE_MAX);
5✔
385

386
        if (arg_legend != 0)
1✔
387
                puts("\nAvailable device unit substates:");
1✔
388
        DUMP_STRING_TABLE(device_state, DeviceState, _DEVICE_STATE_MAX);
4✔
389

390
        if (arg_legend != 0)
1✔
391
                puts("\nAvailable mount unit substates:");
1✔
392
        DUMP_STRING_TABLE(mount_state, MountState, _MOUNT_STATE_MAX);
13✔
393

394
        if (arg_legend != 0)
1✔
395
                puts("\nAvailable path unit substates:");
1✔
396
        DUMP_STRING_TABLE(path_state, PathState, _PATH_STATE_MAX);
5✔
397

398
        if (arg_legend != 0)
1✔
399
                puts("\nAvailable scope unit substates:");
1✔
400
        DUMP_STRING_TABLE(scope_state, ScopeState, _SCOPE_STATE_MAX);
8✔
401

402
        if (arg_legend != 0)
1✔
403
                puts("\nAvailable service unit substates:");
1✔
404
        DUMP_STRING_TABLE(service_state, ServiceState, _SERVICE_STATE_MAX);
27✔
405

406
        if (arg_legend != 0)
1✔
407
                puts("\nAvailable slice unit substates:");
1✔
408
        DUMP_STRING_TABLE(slice_state, SliceState, _SLICE_STATE_MAX);
3✔
409

410
        if (arg_legend != 0)
1✔
411
                puts("\nAvailable socket unit substates:");
1✔
412
        DUMP_STRING_TABLE(socket_state, SocketState, _SOCKET_STATE_MAX);
16✔
413

414
        if (arg_legend != 0)
1✔
415
                puts("\nAvailable swap unit substates:");
1✔
416
        DUMP_STRING_TABLE(swap_state, SwapState, _SWAP_STATE_MAX);
10✔
417

418
        if (arg_legend != 0)
1✔
419
                puts("\nAvailable target unit substates:");
1✔
420
        DUMP_STRING_TABLE(target_state, TargetState, _TARGET_STATE_MAX);
3✔
421

422
        if (arg_legend != 0)
1✔
423
                puts("\nAvailable timer unit substates:");
1✔
424
        DUMP_STRING_TABLE(timer_state, TimerState, _TIMER_STATE_MAX);
6✔
425
}
1✔
426

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

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

549
        int c, r;
11,218✔
550

551
        assert(argc >= 0);
11,218✔
552
        assert(argv);
11,218✔
553

554
        /* We default to allowing interactive authorization only in systemctl (not in the legacy commands) */
555
        arg_ask_password = true;
11,218✔
556

557
        while ((c = getopt_long(argc, argv, "hC:t:p:P:alqfs:H:M:n:o:iTr.::", options, NULL)) >= 0)
35,693✔
558

559
                switch (c) {
13,267✔
560

561
                case 'h':
×
562
                        return systemctl_help();
×
563

564
                case ARG_VERSION:
7✔
565
                        return version();
7✔
566

567
                case 't':
54✔
568
                        if (isempty(optarg))
54✔
569
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
570
                                                       "--type= requires arguments.");
571

572
                        for (const char *p = optarg;;) {
54✔
573
                                _cleanup_free_ char *type = NULL;
57✔
574

575
                                r = extract_first_word(&p, &type, ",", 0);
110✔
576
                                if (r < 0)
110✔
577
                                        return log_error_errno(r, "Failed to parse type: %s", optarg);
×
578
                                if (r == 0)
110✔
579
                                        break;
580

581
                                if (streq(type, "help")) {
57✔
582
                                        help_types();
1✔
583
                                        return 0;
584
                                }
585

586
                                if (unit_type_from_string(type) >= 0) {
56✔
587
                                        if (strv_consume(&arg_types, TAKE_PTR(type)) < 0)
54✔
588
                                                return log_oom();
×
589
                                        continue;
54✔
590
                                }
591

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

600
                                log_error("Unknown unit type or load state '%s'.", type);
×
UNCOV
601
                                return log_info_errno(SYNTHETIC_ERRNO(EINVAL),
×
602
                                                      "Use -t help to see a list of allowed values.");
603
                        }
604

605
                        break;
53✔
606

607
                case 'P':
240✔
608
                        SET_FLAG(arg_print_flags, BUS_PRINT_PROPERTY_ONLY_VALUE, true);
240✔
609
                        _fallthrough_;
5,376✔
610

611
                case 'p':
5,376✔
612
                        /* Make sure that if the empty property list was specified, we won't show any
613
                           properties. */
614
                        if (isempty(optarg) && !arg_properties) {
5,377✔
615
                                arg_properties = new0(char*, 1);
1✔
616
                                if (!arg_properties)
1✔
617
                                        return log_oom();
×
618
                        } else
619
                                for (const char *p = optarg;;) {
5,375✔
620
                                        _cleanup_free_ char *prop = NULL;
10,760✔
621

622
                                        r = extract_first_word(&p, &prop, ",", 0);
10,760✔
623
                                        if (r < 0)
10,760✔
624
                                                return log_error_errno(r, "Failed to parse property: %s", optarg);
×
625
                                        if (r == 0)
10,760✔
626
                                                break;
627

628
                                        if (strv_consume(&arg_properties, TAKE_PTR(prop)) < 0)
5,385✔
629
                                                return log_oom();
×
630
                                }
631

632
                        /* If the user asked for a particular property, show it, even if it is empty. */
633
                        SET_FLAG(arg_print_flags, BUS_PRINT_PROPERTY_SHOW_EMPTY, true);
5,376✔
634

635
                        break;
5,376✔
636

637
                case 'a':
54✔
638
                        SET_FLAG(arg_print_flags, BUS_PRINT_PROPERTY_SHOW_EMPTY, true);
54✔
639
                        arg_all = true;
54✔
640
                        break;
54✔
641

642
                case ARG_REVERSE:
3✔
643
                        arg_dependency = DEPENDENCY_REVERSE;
3✔
644
                        break;
3✔
645

646
                case ARG_AFTER:
5✔
647
                        arg_dependency = DEPENDENCY_AFTER;
5✔
648
                        arg_jobs_after = true;
5✔
649
                        break;
5✔
650

651
                case ARG_BEFORE:
5✔
652
                        arg_dependency = DEPENDENCY_BEFORE;
5✔
653
                        arg_jobs_before = true;
5✔
654
                        break;
5✔
655

656
                case ARG_SHOW_TYPES:
1✔
657
                        arg_show_types = true;
1✔
658
                        break;
1✔
659

660
                case ARG_VALUE:
5,031✔
661
                        SET_FLAG(arg_print_flags, BUS_PRINT_PROPERTY_ONLY_VALUE, true);
5,031✔
662
                        break;
5,031✔
663

664
                case ARG_JOB_MODE:
10✔
665
                        _arg_job_mode = optarg;
10✔
666
                        break;
10✔
667

668
                case ARG_FAIL:
×
669
                        _arg_job_mode = "fail";
×
670
                        break;
×
671

672
                case ARG_IRREVERSIBLE:
×
673
                        _arg_job_mode = "replace-irreversibly";
×
674
                        break;
×
675

676
                case ARG_IGNORE_DEPENDENCIES:
×
677
                        _arg_job_mode = "ignore-dependencies";
×
678
                        break;
×
679

680
                case ARG_USER:
195✔
681
                        arg_runtime_scope = RUNTIME_SCOPE_USER;
195✔
682
                        break;
195✔
683

684
                case ARG_SYSTEM:
×
685
                        arg_runtime_scope = RUNTIME_SCOPE_SYSTEM;
×
686
                        break;
×
687

688
                case ARG_GLOBAL:
×
689
                        arg_runtime_scope = RUNTIME_SCOPE_GLOBAL;
×
690
                        break;
×
691

692
                case 'C':
3✔
693
                        r = capsule_name_is_valid(optarg);
3✔
694
                        if (r < 0)
3✔
695
                                return log_error_errno(r, "Unable to validate capsule name '%s': %m", optarg);
×
696
                        if (r == 0)
3✔
697
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid capsule name: %s", optarg);
×
698

699
                        arg_host = optarg;
3✔
700
                        arg_transport = BUS_TRANSPORT_CAPSULE;
3✔
701
                        arg_runtime_scope = RUNTIME_SCOPE_USER;
3✔
702
                        break;
3✔
703

704
                case ARG_WAIT:
14✔
705
                        arg_wait = true;
14✔
706
                        break;
14✔
707

708
                case ARG_NO_BLOCK:
105✔
709
                        arg_no_block = true;
105✔
710
                        break;
105✔
711

712
                case ARG_NO_LEGEND:
54✔
713
                        arg_legend = false;
54✔
714
                        break;
54✔
715

716
                case ARG_LEGEND:
3✔
717
                        r = parse_boolean_argument("--legend", optarg, NULL);
3✔
718
                        if (r < 0)
3✔
719
                                return r;
720
                        arg_legend = r;
3✔
721
                        break;
3✔
722

723
                case ARG_NO_PAGER:
7✔
724
                        arg_pager_flags |= PAGER_DISABLE;
7✔
725
                        break;
7✔
726

727
                case ARG_NO_WALL:
3✔
728
                        arg_no_wall = true;
3✔
729
                        break;
3✔
730

731
                case ARG_ROOT:
40✔
732
                        r = parse_path_argument(optarg, false, &arg_root);
40✔
733
                        if (r < 0)
40✔
734
                                return r;
735
                        break;
736

737
                case ARG_IMAGE:
×
738
                        r = parse_path_argument(optarg, false, &arg_image);
×
739
                        if (r < 0)
×
740
                                return r;
741
                        break;
742

743
                case ARG_IMAGE_POLICY:
×
744
                        r = parse_image_policy_argument(optarg, &arg_image_policy);
×
745
                        if (r < 0)
×
746
                                return r;
747
                        break;
748

749
                case 'l':
5✔
750
                        arg_full = true;
5✔
751
                        break;
5✔
752

753
                case ARG_FAILED:
754
                        if (strv_extend(&arg_states, "failed") < 0)
1✔
755
                                return log_oom();
×
756

757
                        break;
758

759
                case ARG_DRY_RUN:
23✔
760
                        arg_dry_run = true;
23✔
761
                        break;
23✔
762

763
                case 'q':
2,079✔
764
                        arg_quiet = true;
2,079✔
765

766
                        if (arg_legend < 0)
2,079✔
767
                                arg_legend = false;
2,079✔
768

769
                        break;
770

771
                case 'f':
13✔
772
                        arg_force++;
13✔
773
                        break;
13✔
774

775
                case ARG_NO_RELOAD:
×
776
                        arg_no_reload = true;
×
777
                        break;
×
778

779
                case ARG_KILL_WHOM:
9✔
780
                        arg_kill_whom = optarg;
9✔
781
                        break;
9✔
782

783
                case ARG_KILL_VALUE: {
6✔
784
                        unsigned u;
6✔
785

786
                        if (isempty(optarg)) {
6✔
787
                                arg_kill_value_set = false;
×
788
                                return 0;
×
789
                        }
790

791
                        /* First, try to parse unsigned, so that we can support the prefixes 0x, 0o, 0b */
792
                        r = safe_atou_full(optarg, 0, &u);
6✔
793
                        if (r < 0)
6✔
794
                                /* If this didn't work, try as signed integer, without those prefixes */
795
                                r = safe_atoi(optarg, &arg_kill_value);
×
796
                        else if (u > INT_MAX)
6✔
797
                                r = -ERANGE;
798
                        else
799
                                arg_kill_value = (int) u;
6✔
800
                        if (r < 0)
6✔
801
                                return log_error_errno(r, "Unable to parse signal queue value: %s", optarg);
×
802

803
                        arg_kill_value_set = true;
6✔
804
                        break;
6✔
805
                }
806

807
                case 's':
22✔
808
                        r = parse_signal_argument(optarg, &arg_signal);
22✔
809
                        if (r <= 0)
22✔
810
                                return r;
811
                        break;
812

813
                case ARG_NO_ASK_PASSWORD:
×
814
                        arg_ask_password = false;
×
815
                        break;
×
816

817
                case 'H':
×
818
                        arg_transport = BUS_TRANSPORT_REMOTE;
×
819
                        arg_host = optarg;
×
820
                        break;
×
821

822
                case 'M':
20✔
823
                        arg_transport = BUS_TRANSPORT_MACHINE;
20✔
824
                        arg_host = optarg;
20✔
825
                        break;
20✔
826

827
                case ARG_RUNTIME:
16✔
828
                        arg_runtime = true;
16✔
829
                        break;
16✔
830

831
                case 'n':
9✔
832
                        if (safe_atou(optarg, &arg_lines) < 0)
9✔
833
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
834
                                                       "Failed to parse lines '%s'",
835
                                                       optarg);
836
                        break;
837

838
                case 'o':
1✔
839
                        if (streq(optarg, "help")) {
1✔
840
                                DUMP_STRING_TABLE(output_mode, OutputMode, _OUTPUT_MODE_MAX);
×
841
                                return 0;
×
842
                        }
843

844
                        arg_output = output_mode_from_string(optarg);
1✔
845
                        if (arg_output < 0)
1✔
846
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
847
                                                       "Unknown output '%s'.",
848
                                                       optarg);
849

850
                        if (OUTPUT_MODE_IS_JSON(arg_output)) {
24,476✔
851
                                arg_legend = false;
1✔
852
                                arg_plain = true;
1✔
853
                        }
854
                        break;
855

856
                case 'i':
×
857
                        arg_check_inhibitors = 0;
×
858
                        break;
×
859

860
                case ARG_CHECK_INHIBITORS:
×
861
                        r = parse_tristate_full(optarg, "auto", &arg_check_inhibitors);
×
862
                        if (r < 0)
×
863
                                return log_error_errno(r, "Failed to parse --check-inhibitors= argument: %s", optarg);
×
864
                        break;
865

866
                case ARG_PLAIN:
1✔
867
                        arg_plain = true;
1✔
868
                        break;
1✔
869

870
                case ARG_FIRMWARE_SETUP:
×
871
                        arg_firmware_setup = true;
×
872
                        break;
×
873

874
                case ARG_BOOT_LOADER_MENU:
×
875

876
                        r = parse_sec(optarg, &arg_boot_loader_menu);
×
877
                        if (r < 0)
×
878
                                return log_error_errno(r, "Failed to parse --boot-loader-menu= argument '%s': %m", optarg);
×
879

880
                        break;
881

882
                case ARG_BOOT_LOADER_ENTRY:
×
883

884
                        if (streq(optarg, "help")) { /* Yes, this means, "help" is not a valid boot loader entry name we can deal with */
×
885
                                r = help_boot_loader_entry();
×
886
                                if (r < 0)
×
887
                                        return r;
888

889
                                return 0;
×
890
                        }
891

892
                        arg_boot_loader_entry = empty_to_null(optarg);
×
893
                        break;
×
894

895
                case ARG_STATE:
10✔
896
                        if (isempty(optarg))
10✔
897
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
898
                                                       "--state= requires arguments.");
899

900
                        for (const char *p = optarg;;) {
10✔
901
                                _cleanup_free_ char *s = NULL;
12✔
902

903
                                r = extract_first_word(&p, &s, ",", 0);
21✔
904
                                if (r < 0)
21✔
905
                                        return log_error_errno(r, "Failed to parse state: %s", optarg);
×
906
                                if (r == 0)
21✔
907
                                        break;
908

909
                                if (streq(s, "help")) {
12✔
910
                                        help_states();
1✔
911
                                        return 0;
912
                                }
913

914
                                if (strv_consume(&arg_states, TAKE_PTR(s)) < 0)
11✔
915
                                        return log_oom();
×
916
                        }
917
                        break;
9✔
918

919
                case 'r':
1✔
920
                        if (geteuid() != 0)
1✔
921
                                return log_error_errno(SYNTHETIC_ERRNO(EPERM),
×
922
                                                       "--recursive requires root privileges.");
923

924
                        arg_recursive = true;
1✔
925
                        break;
1✔
926

927
                case ARG_PRESET_MODE:
4✔
928
                        if (streq(optarg, "help")) {
4✔
929
                                DUMP_STRING_TABLE(unit_file_preset_mode, UnitFilePresetMode, _UNIT_FILE_PRESET_MODE_MAX);
×
930
                                return 0;
×
931
                        }
932

933
                        arg_preset_mode = unit_file_preset_mode_from_string(optarg);
4✔
934
                        if (arg_preset_mode < 0)
4✔
935
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
936
                                                       "Failed to parse preset mode: %s.", optarg);
937

938
                        break;
939

940
                case ARG_NOW:
8✔
941
                        arg_now = true;
8✔
942
                        break;
8✔
943

944
                case ARG_MESSAGE:
3✔
945
                        if (strv_extend(&arg_wall, optarg) < 0)
3✔
946
                                return log_oom();
×
947
                        break;
948

949
                case 'T':
10✔
950
                        arg_show_transaction = true;
10✔
951
                        break;
10✔
952

953
                case ARG_WITH_DEPENDENCIES:
3✔
954
                        arg_with_dependencies = true;
3✔
955
                        break;
3✔
956

957
                case ARG_WHAT:
20✔
958
                        if (isempty(optarg))
20✔
959
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--what= requires arguments.");
×
960

961
                        for (const char *p = optarg;;) {
20✔
962
                                _cleanup_free_ char *k = NULL;
20✔
963

964
                                r = extract_first_word(&p, &k, ",", 0);
40✔
965
                                if (r < 0)
40✔
966
                                        return log_error_errno(r, "Failed to parse directory type: %s", optarg);
×
967
                                if (r == 0)
40✔
968
                                        break;
969

970
                                if (streq(k, "help")) {
20✔
971
                                        puts("runtime\n"
×
972
                                             "state\n"
973
                                             "cache\n"
974
                                             "logs\n"
975
                                             "configuration\n"
976
                                             "fdstore");
977
                                        return 0;
978
                                }
979

980
                                r = strv_consume(&arg_clean_what, TAKE_PTR(k));
20✔
981
                                if (r < 0)
20✔
982
                                        return log_oom();
×
983
                        }
984

985
                        break;
20✔
986

987
                case ARG_REBOOT_ARG:
×
988
                        arg_reboot_argument = optarg;
×
989
                        break;
×
990

991
                case ARG_TIMESTAMP_STYLE:
6✔
992
                        if (streq(optarg, "help")) {
6✔
993
                                DUMP_STRING_TABLE(timestamp_style, TimestampStyle, _TIMESTAMP_STYLE_MAX);
×
994
                                return 0;
×
995
                        }
996

997
                        arg_timestamp_style = timestamp_style_from_string(optarg);
6✔
998
                        if (arg_timestamp_style < 0)
6✔
999
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1000
                                                       "Invalid value: %s.", optarg);
1001

1002
                        break;
1003

1004
                case ARG_READ_ONLY:
×
1005
                        arg_read_only = true;
×
1006
                        break;
×
1007

1008
                case ARG_MKDIR:
6✔
1009
                        arg_mkdir = true;
6✔
1010
                        break;
6✔
1011

1012
                case ARG_MARKED:
1✔
1013
                        arg_marked = true;
1✔
1014
                        break;
1✔
1015

1016
                case ARG_NO_WARN:
×
1017
                        arg_no_warn = true;
×
1018
                        break;
×
1019

1020
                case ARG_DROP_IN:
9✔
1021
                        arg_drop_in = optarg;
9✔
1022
                        break;
9✔
1023

1024
                case ARG_WHEN:
×
1025
                        if (streq(optarg, "show")) {
×
1026
                                arg_action = ACTION_SYSTEMCTL_SHOW_SHUTDOWN;
×
1027
                                return 1;
×
1028
                        }
1029

1030
                        if (STR_IN_SET(optarg, "", "cancel")) {
×
1031
                                arg_action = ACTION_CANCEL_SHUTDOWN;
×
1032
                                return 1;
×
1033
                        }
1034

1035
                        if (streq(optarg, "auto")) {
×
1036
                                arg_when = USEC_INFINITY; /* logind chooses on server side */
×
1037
                                break;
×
1038
                        }
1039

1040
                        r = parse_timestamp(optarg, &arg_when);
×
1041
                        if (r < 0)
×
1042
                                return log_error_errno(r, "Failed to parse --when= argument '%s': %m", optarg);
×
1043

1044
                        if (!timestamp_is_set(arg_when))
×
1045
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1046
                                                       "Invalid timestamp '%s' specified for --when=.", optarg);
1047

1048
                        break;
1049

1050
                case ARG_STDIN:
11✔
1051
                        arg_stdin = true;
11✔
1052
                        break;
11✔
1053

1054
                case '.':
1055
                        /* Output an error mimicking getopt, and print a hint afterwards */
1056
                        log_error("%s: invalid option -- '.'", program_invocation_name);
×
UNCOV
1057
                        log_notice("Hint: to specify units starting with a dash, use \"--\":\n"
×
1058
                                   "      %s [OPTIONS...] COMMAND -- -.%s ...",
1059
                                   program_invocation_name, optarg ?: "mount");
1060
                        _fallthrough_;
1061

1062
                case '?':
1063
                        return -EINVAL;
1064

1065
                default:
×
1066
                        assert_not_reached();
×
1067
                }
1068

1069
        /* If we are in --user mode, there's no point in talking to PolicyKit or the infra to query system
1070
         * passwords */
1071
        if (arg_runtime_scope != RUNTIME_SCOPE_SYSTEM)
11,208✔
1072
                arg_ask_password = false;
198✔
1073

1074
        if (arg_transport == BUS_TRANSPORT_REMOTE && arg_runtime_scope != RUNTIME_SCOPE_SYSTEM)
11,208✔
1075
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1076
                                       "Cannot access user instance remotely.");
1077

1078
        if (arg_transport == BUS_TRANSPORT_CAPSULE && arg_runtime_scope != RUNTIME_SCOPE_USER)
11,208✔
1079
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1080
                                       "Cannot access system instance with --capsule=/-C.");
1081

1082
        if (arg_wait && arg_no_block)
11,208✔
1083
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1084
                                       "--wait may not be combined with --no-block.");
1085

1086
        bool do_reload_or_restart = streq_ptr(argv[optind], "reload-or-restart");
11,208✔
1087
        if (arg_marked) {
11,208✔
1088
                if (!do_reload_or_restart)
1✔
1089
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1090
                                               "--marked may only be used with 'reload-or-restart'.");
1091
                if (optind + 1 < argc)
1✔
1092
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1093
                                               "No additional arguments allowed with 'reload-or-restart --marked'.");
1094
                if (arg_wait)
1✔
1095
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1096
                                               "--marked --wait is not supported.");
1097
                if (arg_show_transaction)
1✔
1098
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1099
                                               "--marked --show-transaction is not supported.");
1100

1101
        } else if (do_reload_or_restart) {
11,207✔
1102
                if (optind + 1 >= argc)
×
1103
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1104
                                               "List of units to restart/reload is required.");
1105
        }
1106

1107
        if (arg_image && arg_root)
11,208✔
1108
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1109
                                       "Please specify either --root= or --image=, the combination of both is not supported.");
1110

1111
        return 1;
1112
}
1113

1114
int systemctl_dispatch_parse_argv(int argc, char *argv[]) {
11,230✔
1115
        assert(argc >= 0);
11,230✔
1116
        assert(argv);
11,230✔
1117

1118
        if (invoked_as(argv, "halt")) {
11,230✔
1119
                arg_action = ACTION_HALT;
×
1120
                return halt_parse_argv(argc, argv);
×
1121

1122
        } else if (invoked_as(argv, "poweroff")) {
11,230✔
1123
                arg_action = ACTION_POWEROFF;
×
1124
                return halt_parse_argv(argc, argv);
×
1125

1126
        } else if (invoked_as(argv, "reboot")) {
11,230✔
1127
                arg_action = ACTION_REBOOT;
5✔
1128
                return halt_parse_argv(argc, argv);
5✔
1129

1130
        } else if (invoked_as(argv, "shutdown")) {
11,225✔
1131
                arg_action = ACTION_POWEROFF;
7✔
1132
                return shutdown_parse_argv(argc, argv);
7✔
1133

1134
        } else if (invoked_as(argv, "init")) {
11,218✔
1135

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

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

1152
        } else if (invoked_as(argv, "runlevel")) {
11,218✔
1153
                arg_action = ACTION_RUNLEVEL;
×
1154
                return runlevel_parse_argv(argc, argv);
×
1155
        }
1156

1157
        arg_action = ACTION_SYSTEMCTL;
11,218✔
1158
        return systemctl_parse_argv(argc, argv);
11,218✔
1159
}
1160

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

1249
        const Verb *verb = verbs_find_verb(argv[optind], verbs);
11,208✔
1250
        if (verb && (verb->flags & VERB_ONLINE_ONLY) && arg_root)
11,208✔
1251
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1252
                                       "Verb '%s' cannot be used with --root= or --image=.",
1253
                                       argv[optind] ?: verb->verb);
1254

1255
        return dispatch_verb(argc, argv, verbs, NULL);
11,208✔
1256
}
1257

1258
static int run(int argc, char *argv[]) {
11,230✔
1259
        _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
11,230✔
1260
        _cleanup_(umount_and_freep) char *mounted_dir = NULL;
11,230✔
1261
        int r;
11,230✔
1262

1263
        setlocale(LC_ALL, "");
11,230✔
1264
        log_setup();
11,230✔
1265

1266
        r = systemctl_dispatch_parse_argv(argc, argv);
11,230✔
1267
        if (r <= 0)
11,230✔
1268
                goto finish;
10✔
1269

1270
        journal_browse_prepare();
11,220✔
1271

1272
        if (proc_mounted() == 0)
11,220✔
1273
                log_full(arg_no_warn ? LOG_DEBUG : LOG_WARNING,
×
1274
                         "%s%s/proc/ is not mounted. This is not a supported mode of operation. Please fix\n"
1275
                         "your invocation environment to mount /proc/ and /sys/ properly. Proceeding anyway.\n"
1276
                         "Your mileage may vary.",
1277
                         optional_glyph(GLYPH_WARNING_SIGN),
1278
                         optional_glyph(GLYPH_SPACE));
1279

1280
        if (arg_action != ACTION_SYSTEMCTL && running_in_chroot() > 0) {
11,220✔
1281
                if (!arg_quiet)
×
1282
                        log_info("Running in chroot, ignoring request.");
×
1283
                r = 0;
×
1284
                goto finish;
×
1285
        }
1286

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

1289
        if (arg_image) {
11,220✔
1290
                assert(!arg_root);
×
1291

1292
                r = mount_image_privately_interactively(
×
1293
                                arg_image,
1294
                                arg_image_policy,
1295
                                DISSECT_IMAGE_GENERIC_ROOT |
1296
                                DISSECT_IMAGE_REQUIRE_ROOT |
1297
                                DISSECT_IMAGE_RELAX_VAR_CHECK |
1298
                                DISSECT_IMAGE_VALIDATE_OS |
1299
                                DISSECT_IMAGE_ALLOW_USERSPACE_VERITY,
1300
                                &mounted_dir,
1301
                                /* ret_dir_fd= */ NULL,
1302
                                &loop_device);
1303
                if (r < 0)
×
1304
                        return r;
1305

1306
                arg_root = strdup(mounted_dir);
×
1307
                if (!arg_root)
×
1308
                        return log_oom();
×
1309
        }
1310

1311
        switch (arg_action) {
11,220✔
1312

1313
        case ACTION_SYSTEMCTL:
11,208✔
1314
                r = systemctl_main(argc, argv);
11,208✔
1315
                break;
1316

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

1320
        case ACTION_HALT:
8✔
1321
        case ACTION_POWEROFF:
1322
        case ACTION_REBOOT:
1323
        case ACTION_KEXEC:
1324
                r = halt_main();
8✔
1325
                break;
1326

1327
        case ACTION_RUNLEVEL2:
×
1328
        case ACTION_RUNLEVEL3:
1329
        case ACTION_RUNLEVEL4:
1330
        case ACTION_RUNLEVEL5:
1331
        case ACTION_RESCUE:
1332
                r = start_with_fallback();
×
1333
                break;
1334

1335
        case ACTION_RELOAD:
×
1336
        case ACTION_REEXEC:
1337
                r = reload_with_fallback();
×
1338
                break;
1339

1340
        case ACTION_CANCEL_SHUTDOWN:
3✔
1341
                r = logind_cancel_shutdown();
3✔
1342
                break;
1343

1344
        case ACTION_SHOW_SHUTDOWN:
1✔
1345
        case ACTION_SYSTEMCTL_SHOW_SHUTDOWN:
1346
                r = logind_show_shutdown();
1✔
1347
                break;
1348

1349
        case ACTION_RUNLEVEL:
×
1350
                r = runlevel_main();
×
1351
                break;
1352

1353
        case ACTION_EXIT:
×
1354
        case ACTION_SLEEP:
1355
        case ACTION_SUSPEND:
1356
        case ACTION_HIBERNATE:
1357
        case ACTION_HYBRID_SLEEP:
1358
        case ACTION_SUSPEND_THEN_HIBERNATE:
1359
        case ACTION_EMERGENCY:
1360
        case ACTION_DEFAULT:
1361
                /* systemctl verbs with no equivalent in the legacy commands. These cannot appear in
1362
                 * arg_action. Fall through. */
1363

1364
        case _ACTION_INVALID:
1365
        default:
1366
                assert_not_reached();
×
1367
        }
1368

1369
finish:
11,230✔
1370
        release_busses();
11,230✔
1371

1372
        /* Note that we return r here, not 0, so that we can implement the LSB-like return codes */
1373
        return r;
1374
}
1375

1376
DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);
22,460✔
1377
#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