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

systemd / systemd / 19282013399

12 Nov 2025 12:00AM UTC coverage: 72.412% (+0.01%) from 72.402%
19282013399

push

github

web-flow
core/exec-credentials: port to new mount API, ensure atomicity for creds installation (#39637)

103 of 137 new or added lines in 4 files covered. (75.18%)

850 existing lines in 45 files now uncovered.

307170 of 424195 relevant lines covered (72.41%)

1105108.57 hits per line

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

73.99
/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 "sd-path.h"
8

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

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

34
STATIC_DESTRUCTOR_REGISTER(arg_prefixes, strv_freep);
1,090✔
35

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

42
static Option* option_free(Option *o) {
6,054✔
43
        if (!o)
6,054✔
44
                return NULL;
45

46
        free(o->key);
6,054✔
47
        free(o->value);
6,054✔
48

49
        return mfree(o);
6,054✔
50
}
51

52
DEFINE_TRIVIAL_CLEANUP_FUNC(Option*, option_free);
12,108✔
53
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
6,054✔
54
                option_hash_ops,
55
                char, string_hash_func, string_compare_func,
56
                Option, option_free);
57

58
static bool test_prefix(const char *p) {
25,812✔
59
        if (strv_isempty(arg_prefixes))
25,812✔
60
                return true;
61

62
        return path_startswith_strv(p, arg_prefixes);
22,992✔
63
}
64

65
static Option* option_new(
6,054✔
66
                const char *key,
67
                const char *value,
68
                bool ignore_failure) {
69

70
        _cleanup_(option_freep) Option *o = NULL;
6,054✔
71

72
        assert(key);
6,054✔
73

74
        o = new(Option, 1);
6,054✔
75
        if (!o)
6,054✔
76
                return NULL;
77

78
        *o = (Option) {
12,108✔
79
                .key = strdup(key),
6,054✔
80
                .value = value ? strdup(value) : NULL,
6,054✔
81
                .ignore_failure = ignore_failure,
82
        };
83

84
        if (!o->key)
6,054✔
85
                return NULL;
86
        if (value && !o->value)
6,054✔
87
                return NULL;
88

89
        return TAKE_PTR(o);
6,054✔
90
}
91

92
static int sysctl_write_or_warn(const char *key, const char *value, bool ignore_failure, bool ignore_enoent) {
5,700✔
93
        int r;
5,700✔
94

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

112
        return 0;
113
}
114

115
static int apply_glob_option_with_prefix(OrderedHashmap *sysctl_options, Option *option, const char *prefix) {
11,856✔
UNCOV
116
        _cleanup_strv_free_ char **paths = NULL;
×
117
        _cleanup_free_ char *pattern = NULL;
11,856✔
118
        int r;
11,856✔
119

120
        assert(sysctl_options);
11,856✔
121
        assert(option);
11,856✔
122

123
        if (prefix) {
11,856✔
124
                _cleanup_free_ char *key = NULL;
11,505✔
125

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

135
                log_debug("The glob '%s' is prefixed with '%s': '%s'", option->key, prefix, key);
2,880✔
136

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

144
                        return sysctl_write_or_warn(key, option->value,
2,880✔
145
                                                    /* ignore_failure = */ option->ignore_failure,
2,880✔
146
                                                    /* ignore_enoent = */ true);
147
                }
148

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

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

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

169
        STRV_FOREACH(s, paths) {
1,404✔
170
                const char *key = ASSERT_SE_PTR(path_startswith(*s, "/proc/sys"));
1,053✔
171

172
                if (ordered_hashmap_contains(sysctl_options, key)) {
1,053✔
173
                        log_debug("Not setting %s (explicit setting exists).", key);
702✔
174
                        continue;
702✔
175
                }
176

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

183
        return r;
184
}
185

186
static int apply_glob_option(OrderedHashmap *sysctl_options, Option *option) {
3,234✔
187
        int r = 0;
3,234✔
188

189
        if (strv_isempty(arg_prefixes))
3,234✔
190
                return apply_glob_option_with_prefix(sysctl_options, option, NULL);
351✔
191

192
        STRV_FOREACH(i, arg_prefixes)
14,388✔
193
                RET_GATHER(r, apply_glob_option_with_prefix(sysctl_options, option, *i));
11,505✔
194
        return r;
195
}
196

197
static int apply_all(OrderedHashmap *sysctl_options) {
1,090✔
198
        Option *option;
1,090✔
199
        int r = 0;
1,090✔
200

201
        ORDERED_HASHMAP_FOREACH(option, sysctl_options) {
7,144✔
202
                int k;
6,054✔
203

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

208
                if (string_is_glob(option->key))
5,703✔
209
                        k = apply_glob_option(sysctl_options, option);
3,234✔
210
                else
211
                        k = sysctl_write_or_warn(option->key, option->value,
2,469✔
212
                                                 /* ignore_failure = */ option->ignore_failure,
2,469✔
213
                                                 /* ignore_enoent = */ !arg_strict);
2,469✔
214
                RET_GATHER(r, k);
5,703✔
215
        }
216

217
        return r;
1,090✔
218
}
219

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

226
        const char *eq = strchr(buffer, '=');
29,046✔
227
        if (eq) {
29,046✔
228
                if (buffer[0] == '-') {
25,821✔
229
                        ignore_failure = true;
2,156✔
230
                        buffer++;
2,156✔
231
                }
232

233
                k = strndup(buffer, eq - buffer);
25,821✔
234
                if (!k)
25,821✔
UNCOV
235
                        return log_oom();
×
236

237
                v = strdup(eq + 1);
25,821✔
238
                if (!v)
25,821✔
UNCOV
239
                        return log_oom();
×
240

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

249
                k = strdup(buffer);
3,225✔
250
                if (!k)
3,225✔
UNCOV
251
                        return log_oom();
×
252
        }
253

254
        const char *key = sysctl_normalize(strstrip(k)), *value = strstrip(v);
29,046✔
255

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

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

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

272
        _cleanup_(option_freep) Option *option = option_new(key, value, ignore_failure);
12,108✔
273
        if (!option)
6,054✔
UNCOV
274
                return log_oom();
×
275

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

280
        TAKE_PTR(option);
6,054✔
281
        return 0;
6,054✔
282
}
283

284
static int parse_file(OrderedHashmap **sysctl_options, const char *path, bool ignore_enoent, const char **search_paths) {
5,502✔
285
        return conf_file_read(
5,502✔
286
                        /* root = */ NULL,
287
                        search_paths,
288
                        path,
289
                        parse_line,
290
                        sysctl_options,
291
                        ignore_enoent,
292
                        /* invalid_config = */ NULL);
293
}
294

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

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

306
        j = path_join(d, "sysctl.extra");
117✔
307
        if (!j)
117✔
UNCOV
308
                return log_oom();
×
309

310
        return parse_file(sysctl_options, j, /* ignore_enoent= */ true, /* search_paths= */ NULL);
117✔
311
}
312

UNCOV
313
static int cat_config(char **files) {
×
314
        pager_open(arg_pager_flags);
×
315

UNCOV
316
        return cat_files(NULL, files, arg_cat_flags);
×
317
}
318

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

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

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

346
        return 0;
347
}
348

349
static int parse_argv(int argc, char *argv[]) {
1,090✔
350

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

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

373
        int c;
1,090✔
374

375
        assert(argc >= 0);
1,090✔
376
        assert(argv);
1,090✔
377

378
        while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
4,936✔
379

380
                switch (c) {
3,846✔
381

UNCOV
382
                case 'h':
×
383
                        return help();
×
384

UNCOV
385
                case ARG_VERSION:
×
386
                        return version();
×
387

388
                case ARG_CAT_CONFIG:
×
UNCOV
389
                        arg_cat_flags = CAT_CONFIG_ON;
×
390
                        break;
×
391

392
                case ARG_TLDR:
×
UNCOV
393
                        arg_cat_flags = CAT_TLDR;
×
UNCOV
394
                        break;
×
395

396
                case ARG_PREFIX: {
3,835✔
397
                        const char *s;
3,835✔
398
                        char *p;
3,835✔
399

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

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

411
                        if (strv_consume(&arg_prefixes, p) < 0)
3,835✔
UNCOV
412
                                return log_oom();
×
413

414
                        break;
415
                }
416

417
                case ARG_NO_PAGER:
×
UNCOV
418
                        arg_pager_flags |= PAGER_DISABLE;
×
UNCOV
419
                        break;
×
420

421
                case ARG_STRICT:
6✔
422
                        arg_strict = true;
6✔
423
                        break;
6✔
424

425
                case ARG_INLINE:
5✔
426
                        arg_inline = true;
5✔
427
                        break;
5✔
428

429
                case '?':
430
                        return -EINVAL;
431

UNCOV
432
                default:
×
UNCOV
433
                        assert_not_reached();
×
434
                }
435

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

440
        return 1;
441
}
442

443
static int run(int argc, char *argv[]) {
1,090✔
444
        _cleanup_ordered_hashmap_free_ OrderedHashmap *sysctl_options = NULL;
1,090✔
445
        _cleanup_strv_free_ char **search_paths = NULL;
1,090✔
446
        int r;
1,090✔
447

448
        r = sd_path_lookup_strv(SD_PATH_SEARCH_SYSCTL, /* suffix= */ NULL, &search_paths);
1,090✔
449
        if (r < 0)
1,090✔
UNCOV
450
               return log_error_errno(r, "Failed to get sysctl.d/ search paths: %m");
×
451

452
        r = parse_argv(argc, argv);
1,090✔
453
        if (r <= 0)
1,090✔
454
                return r;
455

456
        log_setup();
1,090✔
457

458
        umask(0022);
1,090✔
459

460
        if (argc > optind) {
1,090✔
461
                unsigned pos = 0;
15✔
462

463
                STRV_FOREACH(arg, strv_skip(argv, optind)) {
32✔
464
                        if (arg_inline)
17✔
465
                                /* Use (argument):n, where n==1 for the first positional arg */
466
                                RET_GATHER(r, parse_line("(argument)", ++pos, *arg, /* invalid_config = */ NULL, &sysctl_options));
7✔
467
                        else
468
                                RET_GATHER(r, parse_file(&sysctl_options, *arg, /* ignore_enoent= */ false, (const char**) search_paths));
10✔
469
                }
470
        } else {
471
                _cleanup_strv_free_ char **files = NULL;
×
472

473
                r = conf_files_list_strv(&files, ".conf", NULL, 0, (const char**) search_paths);
1,075✔
474
                if (r < 0)
1,075✔
UNCOV
475
                        return log_error_errno(r, "Failed to enumerate sysctl.d files: %m");
×
476

477
                if (arg_cat_flags != CAT_CONFIG_OFF)
1,075✔
UNCOV
478
                        return cat_config(files);
×
479

480
                STRV_FOREACH(f, files)
6,450✔
481
                        RET_GATHER(r, parse_file(&sysctl_options, *f, /* ignore_enoent= */ true, /* search_paths= */ NULL));
5,375✔
482

483
                RET_GATHER(r, read_credential_lines(&sysctl_options));
1,075✔
484
        }
485

486
        return RET_GATHER(r, apply_all(sysctl_options));
1,090✔
487
}
488

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