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

systemd / systemd / 19397511130

15 Nov 2025 09:47PM UTC coverage: 72.518% (+0.1%) from 72.37%
19397511130

push

github

web-flow
sd-event: several follow-ups for recent change (#39743)

3 of 4 new or added lines in 2 files covered. (75.0%)

1869 existing lines in 55 files now uncovered.

308519 of 425439 relevant lines covered (72.52%)

1258617.71 hits per line

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

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

3
#include <getopt.h>
4
#include <stdio.h>
5
#include <sys/stat.h>
6

7
#include "alloc-util.h"
8
#include "build.h"
9
#include "conf-files.h"
10
#include "constants.h"
11
#include "creds-util.h"
12
#include "errno-util.h"
13
#include "fd-util.h"
14
#include "fileio.h"
15
#include "glob-util.h"
16
#include "hashmap.h"
17
#include "log.h"
18
#include "main-func.h"
19
#include "pager.h"
20
#include "path-util.h"
21
#include "pretty-print.h"
22
#include "string-util.h"
23
#include "strv.h"
24
#include "sysctl-util.h"
25

26
static char **arg_prefixes = NULL;
27
static CatFlags arg_cat_flags = CAT_CONFIG_OFF;
28
static bool arg_strict = false;
29
static bool arg_inline = false;
30
static PagerFlags arg_pager_flags = 0;
31

32
STATIC_DESTRUCTOR_REGISTER(arg_prefixes, strv_freep);
1,119✔
33

34
typedef struct Option {
35
        char *key;
36
        char *value;
37
        bool ignore_failure;
38
} Option;
39

40
static Option* option_free(Option *o) {
6,621✔
41
        if (!o)
6,621✔
42
                return NULL;
43

44
        free(o->key);
6,621✔
45
        free(o->value);
6,621✔
46

47
        return mfree(o);
6,621✔
48
}
49

50
DEFINE_TRIVIAL_CLEANUP_FUNC(Option*, option_free);
13,242✔
51
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
6,621✔
52
                option_hash_ops,
53
                char, string_hash_func, string_compare_func,
54
                Option, option_free);
55

56
static bool test_prefix(const char *p) {
26,508✔
57
        if (strv_isempty(arg_prefixes))
26,508✔
58
                return true;
59

60
        return path_startswith_strv(p, arg_prefixes);
23,208✔
61
}
62

63
static Option* option_new(
6,621✔
64
                const char *key,
65
                const char *value,
66
                bool ignore_failure) {
67

68
        _cleanup_(option_freep) Option *o = NULL;
6,621✔
69

70
        assert(key);
6,621✔
71

72
        o = new(Option, 1);
6,621✔
73
        if (!o)
6,621✔
74
                return NULL;
75

76
        *o = (Option) {
13,242✔
77
                .key = strdup(key),
6,621✔
78
                .value = value ? strdup(value) : NULL,
6,621✔
79
                .ignore_failure = ignore_failure,
80
        };
81

82
        if (!o->key)
6,621✔
83
                return NULL;
84
        if (value && !o->value)
6,621✔
85
                return NULL;
86

87
        return TAKE_PTR(o);
6,621✔
88
}
89

90
static int sysctl_write_or_warn(const char *key, const char *value, bool ignore_failure, bool ignore_enoent) {
6,207✔
91
        int r;
6,207✔
92

93
        r = sysctl_write(key, value);
6,207✔
94
        if (r < 0) {
6,207✔
95
                /* Proceed without failing if ignore_failure is true.
96
                 * If the sysctl is not available in the kernel or we are running with reduced privileges and
97
                 * cannot write it, then log about the issue, and proceed without failing. Unless strict mode
98
                 * (arg_strict = true) is enabled, in which case we should fail. (EROFS is treated as a
99
                 * permission problem here, since that's how container managers usually protected their
100
                 * sysctls.)
101
                 * In all other cases log an error and make the tool fail. */
102
                if (ignore_failure || (!arg_strict && ERRNO_IS_NEG_FS_WRITE_REFUSED(r)))
1,117✔
103
                        log_debug_errno(r, "Couldn't write '%s' to '%s', ignoring: %m", value, key);
446✔
104
                else if (ignore_enoent && r == -ENOENT)
671✔
105
                        log_warning_errno(r, "Couldn't write '%s' to '%s', ignoring: %m", value, key);
668✔
106
                else
107
                        return log_error_errno(r, "Couldn't write '%s' to '%s': %m", value, key);
3✔
108
        }
109

110
        return 0;
111
}
112

113
static int apply_glob_option_with_prefix(OrderedHashmap *sysctl_options, Option *option, const char *prefix) {
12,024✔
UNCOV
114
        _cleanup_strv_free_ char **paths = NULL;
×
115
        _cleanup_free_ char *pattern = NULL;
12,024✔
116
        int r;
12,024✔
117

118
        assert(sysctl_options);
12,024✔
119
        assert(option);
12,024✔
120

121
        if (prefix) {
12,024✔
122
                _cleanup_free_ char *key = NULL;
11,613✔
123

124
                r = path_glob_can_match(option->key, prefix, &key);
11,613✔
125
                if (r < 0)
11,613✔
UNCOV
126
                        return log_error_errno(r, "Failed to check if the glob '%s' matches prefix '%s': %m",
×
127
                                               option->key, prefix);
128
                if (r == 0) {
11,613✔
129
                        log_debug("The glob '%s' does not match prefix '%s'.", option->key, prefix);
8,706✔
130
                        return 0;
8,706✔
131
                }
132

133
                log_debug("The glob '%s' is prefixed with '%s': '%s'", option->key, prefix, key);
2,907✔
134

135
                if (!string_is_glob(key)) {
2,907✔
136
                        /* The prefixed pattern is not glob anymore. Let's skip to call glob(). */
137
                        if (ordered_hashmap_contains(sysctl_options, key)) {
2,907✔
UNCOV
138
                                log_debug("Not setting %s (explicit setting exists).", key);
×
UNCOV
139
                                return 0;
×
140
                        }
141

142
                        return sysctl_write_or_warn(key, option->value,
2,907✔
143
                                                    /* ignore_failure = */ option->ignore_failure,
2,907✔
144
                                                    /* ignore_enoent = */ true);
145
                }
146

UNCOV
147
                pattern = path_join("/proc/sys", key);
×
148
        } else
149
                pattern = path_join("/proc/sys", option->key);
411✔
150
        if (!pattern)
411✔
UNCOV
151
                return log_oom();
×
152

153
        r = glob_extend(&paths, pattern, GLOB_NOCHECK);
411✔
154
        if (r < 0) {
411✔
UNCOV
155
                if (r == -ENOENT) {
×
UNCOV
156
                        log_debug("No match for glob: %s", option->key);
×
157
                        return 0;
×
158
                }
159
                if (option->ignore_failure || ERRNO_IS_PRIVILEGE(r)) {
×
UNCOV
160
                        log_debug_errno(r, "Failed to resolve glob '%s', ignoring: %m", option->key);
×
161
                        return 0;
×
162
                }
163

UNCOV
164
                return log_error_errno(r, "Couldn't resolve glob '%s': %m", option->key);
×
165
        }
166

167
        STRV_FOREACH(s, paths) {
1,644✔
168
                const char *key = ASSERT_SE_PTR(path_startswith(*s, "/proc/sys"));
1,233✔
169

170
                if (ordered_hashmap_contains(sysctl_options, key)) {
1,233✔
171
                        log_debug("Not setting %s (explicit setting exists).", key);
822✔
172
                        continue;
822✔
173
                }
174

175
                RET_GATHER(r,
411✔
176
                           sysctl_write_or_warn(key, option->value,
177
                                                /* ignore_failure = */ option->ignore_failure,
178
                                                /* ignore_enoent = */ !arg_strict));
179
        }
180

181
        return r;
182
}
183

184
static int apply_glob_option(OrderedHashmap *sysctl_options, Option *option) {
3,321✔
185
        int r = 0;
3,321✔
186

187
        if (strv_isempty(arg_prefixes))
3,321✔
188
                return apply_glob_option_with_prefix(sysctl_options, option, NULL);
411✔
189

190
        STRV_FOREACH(i, arg_prefixes)
14,523✔
191
                RET_GATHER(r, apply_glob_option_with_prefix(sysctl_options, option, *i));
11,613✔
192
        return r;
193
}
194

195
static int apply_all(OrderedHashmap *sysctl_options) {
1,119✔
196
        Option *option;
1,119✔
197
        int r = 0;
1,119✔
198

199
        ORDERED_HASHMAP_FOREACH(option, sysctl_options) {
7,740✔
200
                int k;
6,621✔
201

202
                /* Ignore "negative match" options, they are there only to exclude stuff from globs. */
203
                if (!option->value)
6,621✔
204
                        continue;
411✔
205

206
                if (string_is_glob(option->key))
6,210✔
207
                        k = apply_glob_option(sysctl_options, option);
3,321✔
208
                else
209
                        k = sysctl_write_or_warn(option->key, option->value,
2,889✔
210
                                                 /* ignore_failure = */ option->ignore_failure,
2,889✔
211
                                                 /* ignore_enoent = */ !arg_strict);
2,889✔
212
                RET_GATHER(r, k);
6,210✔
213
        }
214

215
        return r;
1,119✔
216
}
217

218
static int parse_line(const char *fname, unsigned line, const char *buffer, bool *invalid_config, void *userdata) {
29,829✔
219
        OrderedHashmap **sysctl_options = ASSERT_PTR(userdata);
29,829✔
220
        _cleanup_free_ char *k = NULL, *v = NULL;
29,829✔
221
        bool ignore_failure = false;
29,829✔
222
        int r;
29,829✔
223

224
        const char *eq = strchr(buffer, '=');
29,829✔
225
        if (eq) {
29,829✔
226
                if (buffer[0] == '-') {
26,517✔
227
                        ignore_failure = true;
2,214✔
228
                        buffer++;
2,214✔
229
                }
230

231
                k = strndup(buffer, eq - buffer);
26,517✔
232
                if (!k)
26,517✔
UNCOV
233
                        return log_oom();
×
234

235
                v = strdup(eq + 1);
26,517✔
236
                if (!v)
26,517✔
UNCOV
237
                        return log_oom();
×
238

239
        } else {
240
                if (buffer[0] == '-')
3,312✔
241
                        /* We have a "negative match" option. Let's continue with value==NULL. */
242
                        buffer++;
3,312✔
243
                else
UNCOV
244
                        return log_syntax(NULL, LOG_WARNING, fname, line, SYNTHETIC_ERRNO(EINVAL),
×
245
                                          "Line is not an assignment, ignoring: %s", buffer);
246

247
                k = strdup(buffer);
3,312✔
248
                if (!k)
3,312✔
UNCOV
249
                        return log_oom();
×
250
        }
251

252
        const char *key = sysctl_normalize(strstrip(k)), *value = strstrip(v);
29,829✔
253

254
        /* We can't filter out globs at this point, we'll need to do that later. */
255
        if (!string_is_glob(key) && !test_prefix(key))
29,829✔
256
                return 0;
257

258
        Option *existing = ordered_hashmap_get(*sysctl_options, key);
6,621✔
259
        if (existing) {
6,621✔
UNCOV
260
                if (streq_ptr(value, existing->value)) {
×
UNCOV
261
                        existing->ignore_failure = existing->ignore_failure || ignore_failure;
×
262
                        return 0;
×
263
                }
264

UNCOV
265
                log_syntax(NULL, LOG_DEBUG, fname, line, 0,
×
266
                           "Overwriting earlier assignment of '%s'.", key);
267
                option_free(ordered_hashmap_remove(*sysctl_options, key));
×
268
        }
269

270
        _cleanup_(option_freep) Option *option = option_new(key, value, ignore_failure);
13,242✔
271
        if (!option)
6,621✔
UNCOV
272
                return log_oom();
×
273

274
        r = ordered_hashmap_ensure_put(sysctl_options, &option_hash_ops, option->key, option);
6,621✔
275
        if (r < 0)
6,621✔
UNCOV
276
                return log_error_errno(r, "Failed to add sysctl variable '%s' to hashmap: %m", key);
×
277

278
        TAKE_PTR(option);
6,621✔
279
        return 0;
6,621✔
280
}
281

282
static int parse_file(OrderedHashmap **sysctl_options, const char *path, bool ignore_enoent) {
5,667✔
283
        return conf_file_read(
11,334✔
284
                        /* root = */ NULL,
285
                        (const char**) CONF_PATHS_STRV("sysctl.d"),
5,667✔
286
                        path,
287
                        parse_line,
288
                        sysctl_options,
289
                        ignore_enoent,
290
                        /* invalid_config = */ NULL);
291
}
292

293
static int read_credential_lines(OrderedHashmap **sysctl_options) {
1,104✔
294
        _cleanup_free_ char *j = NULL;
1,104✔
295
        const char *d;
1,104✔
296
        int r;
1,104✔
297

298
        r = get_credentials_dir(&d);
1,104✔
299
        if (r == -ENXIO)
1,104✔
300
                return 0;
301
        if (r < 0)
137✔
UNCOV
302
                return log_error_errno(r, "Failed to get credentials directory: %m");
×
303

304
        j = path_join(d, "sysctl.extra");
137✔
305
        if (!j)
137✔
UNCOV
306
                return log_oom();
×
307

308
        return parse_file(sysctl_options, j, /* ignore_enoent= */ true);
137✔
309
}
310

UNCOV
311
static int cat_config(char **files) {
×
UNCOV
312
        pager_open(arg_pager_flags);
×
313

314
        return cat_files(NULL, files, arg_cat_flags);
×
315
}
316

UNCOV
317
static int help(void) {
×
UNCOV
318
        _cleanup_free_ char *link = NULL;
×
319
        int r;
×
320

321
        r = terminal_urlify_man("systemd-sysctl.service", "8", &link);
×
UNCOV
322
        if (r < 0)
×
323
                return log_oom();
×
324

325
        printf("%1$s [OPTIONS...] [CONFIGURATION FILE...]\n"
×
326
               "\n%2$sApplies kernel sysctl settings.%4$s\n"
327
               "\n%3$sCommands:%4$s\n"
328
               "     --cat-config       Show configuration files\n"
329
               "     --tldr             Show non-comment parts of configuration\n"
330
               "  -h --help             Show this help\n"
331
               "     --version          Show package version\n"
332
               "\n%3$sOptions:%4$s\n"
333
               "     --prefix=PATH      Only apply rules with the specified prefix\n"
334
               "     --no-pager         Do not pipe output into a pager\n"
335
               "     --strict           Fail on any kind of failures\n"
336
               "     --inline           Treat arguments as configuration lines\n"
337
               "\nSee the %5$s for details.\n",
338
               program_invocation_short_name,
339
               ansi_highlight(),
340
               ansi_underline(),
341
               ansi_normal(),
342
               link);
343

344
        return 0;
345
}
346

347
static int parse_argv(int argc, char *argv[]) {
1,119✔
348

349
        enum {
1,119✔
350
                ARG_VERSION = 0x100,
351
                ARG_CAT_CONFIG,
352
                ARG_TLDR,
353
                ARG_PREFIX,
354
                ARG_NO_PAGER,
355
                ARG_STRICT,
356
                ARG_INLINE,
357
        };
358

359
        static const struct option options[] = {
1,119✔
360
                { "help",       no_argument,       NULL, 'h'            },
361
                { "version",    no_argument,       NULL, ARG_VERSION    },
362
                { "cat-config", no_argument,       NULL, ARG_CAT_CONFIG },
363
                { "tldr",       no_argument,       NULL, ARG_TLDR       },
364
                { "prefix",     required_argument, NULL, ARG_PREFIX     },
365
                { "no-pager",   no_argument,       NULL, ARG_NO_PAGER   },
366
                { "strict",     no_argument,       NULL, ARG_STRICT     },
367
                { "inline",     no_argument,       NULL, ARG_INLINE     },
368
                {}
369
        };
370

371
        int c;
1,119✔
372

373
        assert(argc >= 0);
1,119✔
374
        assert(argv);
1,119✔
375

376
        while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
5,001✔
377

378
                switch (c) {
3,882✔
379

UNCOV
380
                case 'h':
×
UNCOV
381
                        return help();
×
382

383
                case ARG_VERSION:
×
UNCOV
384
                        return version();
×
385

386
                case ARG_CAT_CONFIG:
×
UNCOV
387
                        arg_cat_flags = CAT_CONFIG_ON;
×
388
                        break;
×
389

390
                case ARG_TLDR:
×
UNCOV
391
                        arg_cat_flags = CAT_TLDR;
×
392
                        break;
×
393

394
                case ARG_PREFIX: {
3,871✔
395
                        const char *s;
3,871✔
396
                        char *p;
3,871✔
397

398
                        /* We used to require people to specify absolute paths
399
                         * in /proc/sys in the past. This is kinda useless, but
400
                         * we need to keep compatibility. We now support any
401
                         * sysctl name available. */
402
                        sysctl_normalize(optarg);
3,871✔
403

404
                        s = path_startswith(optarg, "/proc/sys");
3,871✔
405
                        p = strdup(s ?: optarg);
3,871✔
406
                        if (!p)
3,871✔
UNCOV
407
                                return log_oom();
×
408

409
                        if (strv_consume(&arg_prefixes, p) < 0)
3,871✔
UNCOV
410
                                return log_oom();
×
411

412
                        break;
413
                }
414

UNCOV
415
                case ARG_NO_PAGER:
×
UNCOV
416
                        arg_pager_flags |= PAGER_DISABLE;
×
417
                        break;
×
418

419
                case ARG_STRICT:
6✔
420
                        arg_strict = true;
6✔
421
                        break;
6✔
422

423
                case ARG_INLINE:
5✔
424
                        arg_inline = true;
5✔
425
                        break;
5✔
426

427
                case '?':
428
                        return -EINVAL;
429

UNCOV
430
                default:
×
UNCOV
431
                        assert_not_reached();
×
432
                }
433

434
        if (arg_cat_flags != CAT_CONFIG_OFF && argc > optind)
1,119✔
UNCOV
435
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
436
                                       "Positional arguments are not allowed with --cat-config/--tldr.");
437

438
        return 1;
439
}
440

441
static int run(int argc, char *argv[]) {
1,119✔
442
        _cleanup_ordered_hashmap_free_ OrderedHashmap *sysctl_options = NULL;
1,119✔
443
        int r;
1,119✔
444

445
        r = parse_argv(argc, argv);
1,119✔
446
        if (r <= 0)
1,119✔
447
                return r;
448

449
        log_setup();
1,119✔
450

451
        umask(0022);
1,119✔
452

453
        if (argc > optind) {
1,119✔
454
                unsigned pos = 0;
15✔
455

456
                STRV_FOREACH(arg, strv_skip(argv, optind)) {
32✔
457
                        if (arg_inline)
17✔
458
                                /* Use (argument):n, where n==1 for the first positional arg */
459
                                RET_GATHER(r, parse_line("(argument)", ++pos, *arg, /* invalid_config = */ NULL, &sysctl_options));
7✔
460
                        else
461
                                RET_GATHER(r, parse_file(&sysctl_options, *arg, false));
10✔
462
                }
463
        } else {
UNCOV
464
                _cleanup_strv_free_ char **files = NULL;
×
465

466
                r = conf_files_list_strv(&files, ".conf", NULL, 0, (const char**) CONF_PATHS_STRV("sysctl.d"));
1,104✔
467
                if (r < 0)
1,104✔
UNCOV
468
                        return log_error_errno(r, "Failed to enumerate sysctl.d files: %m");
×
469

470
                if (arg_cat_flags != CAT_CONFIG_OFF)
1,104✔
471
                        return cat_config(files);
×
472

473
                STRV_FOREACH(f, files)
6,624✔
474
                        RET_GATHER(r, parse_file(&sysctl_options, *f, true));
5,520✔
475

476
                RET_GATHER(r, read_credential_lines(&sysctl_options));
1,104✔
477
        }
478

479
        return RET_GATHER(r, apply_all(sysctl_options));
1,119✔
480
}
481

482
DEFINE_MAIN_FUNCTION(run);
1,119✔
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