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

systemd / systemd / 22330640014

23 Feb 2026 11:36PM UTC coverage: 72.575% (+0.3%) from 72.238%
22330640014

push

github

web-flow
manager: add needs-stop/needs-start (#40709)

Intend to use these in the deb packaging scriptlets

Fixes: #19755

6 of 39 new or added lines in 6 files covered. (15.38%)

2174 existing lines in 61 files now uncovered.

315134 of 434220 relevant lines covered (72.57%)

1132926.37 hits per line

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

90.59
/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,075✔
19
        int r;
2,075✔
20

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

23
        assert(optname);
2,075✔
24

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

30
                if (ret)
2,036✔
31
                        *ret = r;
1,550✔
32
                return r;
2,036✔
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) {
125✔
42
        int r;
125✔
43

44
        assert(optname);
125✔
45
        assert(s); /* We refuse NULL optarg here, since that would be ambiguous on cmdline:
125✔
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);
125✔
51
        if (r < 0)
125✔
52
                return log_error_errno(r, "Failed to parse tristate argument to '%s': %s", optname, s);
×
53

54
        return 0;
55
}
56

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

61
        if (streq(s, "pretty"))
515✔
62
                *ret = SD_JSON_FORMAT_PRETTY|SD_JSON_FORMAT_COLOR_AUTO;
103✔
63
        else if (streq(s, "short"))
412✔
64
                *ret = SD_JSON_FORMAT_NEWLINE;
389✔
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,422✔
79
        char *p;
2,422✔
80
        int r;
2,422✔
81

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

90
        if (isempty(path)) {
2,422✔
91
                *arg = mfree(*arg);
8✔
92
                return 0;
8✔
93
        }
94

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

99
        path_simplify(p);
2,414✔
100
        if (suppress_root && empty_or_root(p))
2,414✔
101
                p = mfree(p);
3✔
102

103
        return free_and_replace(*arg, p);
2,414✔
104
}
105

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

109
        assert(s);
28✔
110
        assert(ret);
28✔
111

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

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

118
                table = table_new("signal", "name");
1✔
119
                if (!table)
1✔
UNCOV
120
                        return log_oom();
×
121

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

131
                r = table_print(table, NULL);
1✔
132
                if (r < 0)
1✔
UNCOV
133
                        return table_log_print_error(r);
×
134

135
                return 0;
136
        }
137

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

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

146
int parse_machine_argument(const char *s, const char **ret_host, BusTransport *ret_transport) {
116✔
147
        int r;
116✔
148

149
        assert(s);
116✔
150
        assert(ret_host);
116✔
151

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

158
        *ret_host = s;
116✔
159

160
        if (ret_transport)
116✔
161
                *ret_transport = BUS_TRANSPORT_MACHINE;
116✔
162

163
        return 0;
164
}
165

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

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