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

systemd / systemd / 28272947092

26 Jun 2026 08:38PM UTC coverage: 72.893% (+0.2%) from 72.703%
28272947092

push

github

poettering
sysupdate: Address review feedback on CheckNew varlink scaffolding

Follow-up to #42422:

 - Rename process_image() to context_process_image(), since it now
   operates on a Context object.
 - Use IN_SET() in image_type_can_sysupdate() instead of a switch.
 - Name the return parameters of context_list_components() ret_xyz, per
   our coding style.
 - Drop a redundant "else" after a return in vl_method_check_new().

9 of 11 new or added lines in 1 file covered. (81.82%)

12567 existing lines in 144 files now uncovered.

341026 of 467845 relevant lines covered (72.89%)

1339355.33 hits per line

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

98.25
/src/basic/sysctl-util.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <stdio.h>
4

5
#include "af-list.h"
6
#include "alloc-util.h"
7
#include "fileio.h"
8
#include "hashmap.h"
9
#include "log.h"
10
#include "parse-util.h"
11
#include "path-util.h"
12
#include "socket-util.h"
13
#include "string-util.h"
14
#include "sysctl-util.h"
15

16
char* sysctl_normalize(char *s) {
27,538✔
17
        char *n;
27,538✔
18

19
        assert(s);
27,538✔
20

21
        n = strpbrk(s, "/.");
27,538✔
22

23
        /* If the first separator is a slash, the path is
24
         * assumed to be normalized and slashes remain slashes
25
         * and dots remains dots. */
26

27
        if (n && *n == '.')
27,538✔
28
                /* Dots become slashes and slashes become dots. Fun. */
29
                do {
53,407✔
30
                        if (*n == '.')
53,407✔
31
                                *n = '/';
53,405✔
32
                        else
33
                                *n = '.';
2✔
34

35
                        n = strpbrk(n + 1, "/.");
53,407✔
36
                } while (n);
53,407✔
37

38
        path_simplify(s);
27,538✔
39

40
        /* Kill the leading slash, but keep the first character of the string in the same place. */
41
        if (s[0] == '/' && s[1] != 0)
27,538✔
42
                memmove(s, s+1, strlen(s));
3,101✔
43

44
        return s;
27,538✔
45
}
46

47
static int shadow_update(Hashmap **shadow, const char *property, const char *value) {
10,273✔
48
        _cleanup_free_ char *k = NULL, *v = NULL, *cur_k = NULL, *cur_v = NULL;
10,273✔
49
        int r;
10,273✔
50

51
        assert(property);
10,273✔
52
        assert(value);
10,273✔
53

54
        if (!shadow)
10,273✔
55
                return 0;
56

57
        k = strdup(property);
4,634✔
58
        if (!k)
4,634✔
59
                return -ENOMEM;
60

61
        v = strdup(value);
4,634✔
62
        if (!v)
4,634✔
63
                return -ENOMEM;
64

65
        cur_v = hashmap_remove2(*shadow, k, (void**)&cur_k);
4,634✔
66

67
        r = hashmap_ensure_put(shadow, &path_hash_ops_free_free, k, v);
4,634✔
68
        if (r < 0) {
4,634✔
UNCOV
69
                assert(r != -EEXIST);
×
70

71
                return r;
72
        }
73

74
        TAKE_PTR(k);
75
        TAKE_PTR(v);
76

77
        return 0;
78
}
79

80
int sysctl_write_full(const char *property, const char *value, Hashmap **shadow) {
10,273✔
81
        char *p;
10,273✔
82
        int r;
10,273✔
83

84
        assert(property);
10,273✔
85
        assert(value);
10,273✔
86

87
        p = strjoina("/proc/sys/", property);
51,365✔
88

89
        path_simplify(p);
10,273✔
90
        if (!path_is_normalized(p))
10,273✔
91
                return -EINVAL;
92

93
        log_debug("Setting '%s' to '%s'", p, value);
10,273✔
94

95
        r = shadow_update(shadow, p, value);
10,273✔
96
        if (r < 0)
10,273✔
97
                return r;
98

99
        return write_string_file(p, value, WRITE_STRING_FILE_VERIFY_ON_FAILURE | WRITE_STRING_FILE_DISABLE_BUFFER | WRITE_STRING_FILE_SUPPRESS_REDUNDANT_VIRTUAL);
10,273✔
100
}
101

102
int sysctl_writef(const char *property, const char *format, ...) {
44✔
103
        _cleanup_free_ char *v = NULL;
44✔
104
        va_list ap;
44✔
105
        int r;
44✔
106

107
        va_start(ap, format);
44✔
108
        r = vasprintf(&v, format, ap);
44✔
109
        va_end(ap);
44✔
110

111
        if (r < 0)
44✔
112
                return -ENOMEM;
113

114
        return sysctl_write(property, v);
44✔
115
}
116

117
static const char* af_to_sysctl_dir(int af) {
78,056✔
118
        if (af == AF_MPLS)
78,056✔
119
                return "mpls";
120

121
        return af_to_ipv4_ipv6(af);
78,055✔
122
}
123

124
int sysctl_write_ip_property(int af, const char *ifname, const char *property, const char *value, Hashmap **shadow) {
4,593✔
125
        const char *p;
4,593✔
126

127
        assert(property);
4,593✔
128
        assert(value);
4,593✔
129

130
        if (!IN_SET(af, AF_INET, AF_INET6, AF_MPLS))
4,593✔
131
                return -EAFNOSUPPORT;
132

133
        if (ifname) {
4,593✔
134
                if (!ifname_valid_full(ifname, IFNAME_VALID_SPECIAL))
4,592✔
135
                        return -EINVAL;
136

137
                p = strjoina("net/", af_to_sysctl_dir(af), "/conf/", ifname, "/", property);
59,696✔
138
        } else
139
                p = strjoina("net/", af_to_sysctl_dir(af), "/", property);
9✔
140

141
        return sysctl_write_full(p, value, shadow);
4,593✔
142
}
143

144
int sysctl_write_ip_property_boolean(int af, const char *ifname, const char *property, bool value, Hashmap **shadow) {
2,696✔
145
        return sysctl_write_ip_property(af, ifname, property, one_zero(value), shadow);
4,366✔
146
}
147

148
int sysctl_write_ip_neighbor_property(int af, const char *ifname, const char *property, const char *value, Hashmap **shadow) {
44✔
149
        const char *p;
44✔
150

151
        assert(property);
44✔
152
        assert(value);
44✔
153
        assert(ifname);
44✔
154

155
        if (!IN_SET(af, AF_INET, AF_INET6))
44✔
156
                return -EAFNOSUPPORT;
157

158
        if (ifname) {
44✔
159
                if (!ifname_valid_full(ifname, IFNAME_VALID_SPECIAL))
44✔
160
                        return -EINVAL;
161
                p = strjoina("net/", af_to_ipv4_ipv6(af), "/neigh/", ifname, "/", property);
572✔
162
        } else
UNCOV
163
                p = strjoina("net/", af_to_ipv4_ipv6(af), "/neigh/default/", property);
×
164

165
        return sysctl_write_full(p, value, shadow);
44✔
166
}
167

168
int sysctl_write_ip_neighbor_property_uint32(int af, const char *ifname, const char *property, uint32_t value, Hashmap **shadow) {
44✔
169
        char buf[DECIMAL_STR_MAX(uint32_t)];
44✔
170
        xsprintf(buf, "%u", value);
44✔
171
        return sysctl_write_ip_neighbor_property(af, ifname, property, buf, shadow);
44✔
172
}
173

174
int sysctl_read(const char *property, char **ret) {
73,465✔
175
        char *p;
73,465✔
176
        int r;
73,465✔
177

178
        assert(property);
73,465✔
179

180
        p = strjoina("/proc/sys/", property);
367,325✔
181

182
        path_simplify(p);
73,465✔
183
        if (!path_is_normalized(p)) /* Filter out attempts to write to /proc/sys/../../…, just in case */
73,465✔
184
                return -EINVAL;
185

186
        r = read_full_virtual_file(p, ret, NULL);
73,465✔
187
        if (r < 0)
73,465✔
188
                return r;
189
        if (ret)
73,464✔
190
                delete_trailing_chars(*ret, NEWLINE);
73,464✔
191

192
        return r;
193
}
194

195
int sysctl_read_ip_property(int af, const char *ifname, const char *property, char **ret) {
73,463✔
196
        const char *p;
73,463✔
197

198
        assert(property);
73,463✔
199

200
        if (!IN_SET(af, AF_INET, AF_INET6, AF_MPLS))
73,463✔
201
                return -EAFNOSUPPORT;
202

203
        if (ifname) {
73,463✔
204
                if (!ifname_valid_full(ifname, IFNAME_VALID_SPECIAL))
73,304✔
205
                        return -EINVAL;
206

207
                p = strjoina("net/", af_to_sysctl_dir(af), "/conf/", ifname, "/", property);
952,952✔
208
        } else
209
                p = strjoina("net/", af_to_sysctl_dir(af), "/", property);
1,431✔
210

211
        return sysctl_read(p, ret);
73,463✔
212
}
213

214
int sysctl_read_ip_property_int(int af, const char *ifname, const char *property, int *ret) {
158✔
215
        _cleanup_free_ char *s = NULL;
158✔
216
        int r;
158✔
217

218
        assert(ret);
158✔
219

220
        r = sysctl_read_ip_property(af, ifname, property, &s);
158✔
221
        if (r < 0)
158✔
222
                return r;
223

224
        return safe_atoi(s, ret);
158✔
225
}
226

227
int sysctl_read_ip_property_uint32(int af, const char *ifname, const char *property, uint32_t *ret) {
56✔
228
        _cleanup_free_ char *s = NULL;
56✔
229
        int r;
56✔
230

231
        assert(ret);
56✔
232

233
        r = sysctl_read_ip_property(af, ifname, property, &s);
56✔
234
        if (r < 0)
56✔
235
                return r;
236

237
        return safe_atou32(s, ret);
56✔
238
}
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