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

systemd / systemd / 23990547145

04 Apr 2026 09:30PM UTC coverage: 72.373% (+0.3%) from 72.107%
23990547145

push

github

web-flow
shutdown: enforce a minimum uptime to make boot loops less annoying (#41215)

Fixes: #9453

Split out of #41016

3 of 39 new or added lines in 2 files covered. (7.69%)

2565 existing lines in 66 files now uncovered.

319531 of 441505 relevant lines covered (72.37%)

1187721.39 hits per line

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

92.86
/src/shared/parse-argument.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include "alloc-util.h"
4
#include "ansi-color.h"
5
#include "bus-util.h"
6
#include "format-table.h"
7
#include "hostname-util.h"
8
#include "log.h"
9
#include "parse-argument.h"
10
#include "parse-util.h"
11
#include "path-util.h"
12
#include "signal-util.h"
13
#include "string-table.h"
14
#include "string-util.h"
15

16
/* All functions in this file emit warnings. */
17

18
int parse_boolean_argument(const char *optname, const char *s, bool *ret) {
2,112✔
19
        int r;
2,112✔
20

21
        /* Returns the result through *ret and the return value. */
22

23
        assert(optname);
2,112✔
24

25
        if (s) {
2,112✔
26
                r = parse_boolean(s);
2,106✔
27
                if (r < 0)
2,106✔
28
                        return log_error_errno(r, "Failed to parse boolean argument to '%s': %s", optname, s);
32✔
29

30
                if (ret)
2,074✔
31
                        *ret = r;
1,585✔
32
                return r;
2,074✔
33
        } else {
34
                /* s may be NULL. This is controlled by getopt_long() parameters. */
35
                if (ret)
6✔
36
                        *ret = true;
6✔
37
                return true;
6✔
38
        }
39
}
40

41
int parse_tristate_argument_with_auto(const char *optname, const char *s, int *ret) {
130✔
42
        int r;
130✔
43

44
        assert(optname);
130✔
45
        assert(s); /* We refuse NULL optarg here, since that would be ambiguous on cmdline:
130✔
46
                      for --enable-a[=BOOL], --enable-a is intuitively interpreted as true rather than "auto"
47
                      (parse_boolean_argument() does exactly that). IOW, tristate options should require
48
                      arguments. */
49

50
        r = parse_tristate_full(s, "auto", ret);
130✔
51
        if (r < 0)
130✔
52
                return log_error_errno(r, "Failed to parse tristate argument to '%s': %s", optname, s);
1✔
53

54
        return 0;
55
}
56

57
int parse_json_argument(const char *s, sd_json_format_flags_t *ret) {
531✔
58
        assert(s);
531✔
59
        assert(ret);
531✔
60

61
        if (streq(s, "pretty"))
531✔
62
                *ret = SD_JSON_FORMAT_PRETTY|SD_JSON_FORMAT_COLOR_AUTO;
104✔
63
        else if (streq(s, "short"))
427✔
64
                *ret = SD_JSON_FORMAT_NEWLINE;
404✔
65
        else if (streq(s, "off"))
23✔
66
                *ret = SD_JSON_FORMAT_OFF;
11✔
67
        else if (streq(s, "help")) {
12✔
68
                puts("pretty\n"
4✔
69
                     "short\n"
70
                     "off");
71
                return 0; /* 0 means → we showed a brief help, exit now */
4✔
72
        } else
73
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Unknown argument to --json= switch: %s", s);
8✔
74

75
        return 1; /* 1 means → properly parsed */
76
}
77

78
int parse_path_argument(const char *path, bool suppress_root, char **arg) {
2,475✔
79
        char *p;
2,475✔
80
        int r;
2,475✔
81

82
        assert(arg);
2,475✔
83

84
        /*
85
         * This function is intended to be used in command line parsers, to handle paths that are passed
86
         * in. It makes the path absolute, and reduces it to NULL if omitted or root (the latter optionally).
87
         *
88
         * NOTE THAT THIS WILL FREE THE PREVIOUS ARGUMENT POINTER ON SUCCESS!
89
         * Hence, do not pass in uninitialized pointers.
90
         */
91

92
        if (isempty(path)) {
2,475✔
93
                *arg = mfree(*arg);
8✔
94
                return 0;
8✔
95
        }
96

97
        r = path_make_absolute_cwd(path, &p);
2,467✔
98
        if (r < 0)
2,467✔
99
                return log_error_errno(r, "Failed to parse path \"%s\" and make it absolute: %m", path);
×
100

101
        path_simplify(p);
2,467✔
102
        if (suppress_root && empty_or_root(p))
2,467✔
103
                p = mfree(p);
3✔
104

105
        return free_and_replace(*arg, p);
2,467✔
106
}
107

108
int parse_signal_argument(const char *s, int *ret) {
28✔
109
        int r;
28✔
110

111
        assert(s);
28✔
112
        assert(ret);
28✔
113

114
        if (streq(s, "help"))
28✔
115
                return DUMP_STRING_TABLE(signal, int, _NSIG);
132✔
116

117
        if (streq(s, "list")) {
26✔
118
                _cleanup_(table_unrefp) Table *table = NULL;
1✔
119

120
                table = table_new("signal", "name");
1✔
121
                if (!table)
1✔
122
                        return log_oom();
×
123

124
                for (int i = 1; i < _NSIG; i++) {
65✔
125
                        r = table_add_many(
64✔
126
                                        table,
127
                                        TABLE_INT, i,
128
                                        TABLE_SIGNAL, i);
129
                        if (r < 0)
64✔
130
                                return table_log_add_error(r);
×
131
                }
132

133
                return table_print_or_warn(table);
1✔
134
        }
135

136
        r = signal_from_string(s);
25✔
137
        if (r < 0)
25✔
UNCOV
138
                return log_error_errno(r, "Failed to parse signal string \"%s\".", s);
×
139

140
        *ret = r;
25✔
141
        return 1; /* work to do */
25✔
142
}
143

144
int parse_machine_argument(const char *s, const char **ret_host, BusTransport *ret_transport) {
117✔
145
        int r;
117✔
146

147
        assert(s);
117✔
148
        assert(ret_host);
117✔
149

150
        r = machine_spec_valid(s);
117✔
151
        if (r < 0)
117✔
UNCOV
152
                return log_error_errno(r, "Failed to validate --machine= argument '%s': %m", s);
×
153
        if (r == 0)
117✔
UNCOV
154
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid --machine= specified: %s", s);
×
155

156
        *ret_host = s;
117✔
157

158
        if (ret_transport)
117✔
159
                *ret_transport = BUS_TRANSPORT_MACHINE;
117✔
160

161
        return 0;
162
}
163

164
int parse_background_argument(const char *s, char **arg) {
9✔
165
        if (!isempty(s) && !looks_like_ansi_color_code(s))
17✔
166
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid --background= argument: %s", s);
4✔
167

168
        return free_and_strdup_warn(arg, s);
5✔
169
}
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