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

systemd / systemd / 21084785420

16 Jan 2026 11:47PM UTC coverage: 72.748% (+0.2%) from 72.522%
21084785420

push

github

yuwata
socket: turn of loud logging when setting up sockopts in container fails due to privs

Various socktops will fail if we run in a container, due to lack of
privs (for example SO_RECVFORCE as used by the journald sockets). That's
typically not a big issue. Hence downgrade the log level.

Follow-up for: f7df0eab8

310553 of 426888 relevant lines covered (72.75%)

1128365.0 hits per line

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

74.21
/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 "glob-util.h"
14
#include "hashmap.h"
15
#include "log.h"
16
#include "main-func.h"
17
#include "pager.h"
18
#include "path-util.h"
19
#include "pretty-print.h"
20
#include "string-util.h"
21
#include "strv.h"
22
#include "sysctl-util.h"
23

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

30
STATIC_DESTRUCTOR_REGISTER(arg_prefixes, strv_freep);
1,113✔
31

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

38
static Option* option_free(Option *o) {
6,147✔
39
        if (!o)
6,147✔
40
                return NULL;
41

42
        free(o->key);
6,147✔
43
        free(o->value);
6,147✔
44

45
        return mfree(o);
6,147✔
46
}
47

48
DEFINE_TRIVIAL_CLEANUP_FUNC(Option*, option_free);
12,294✔
49
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
6,147✔
50
                option_hash_ops,
51
                char, string_hash_func, string_compare_func,
52
                Option, option_free);
53

54
static bool test_prefix(const char *p) {
26,364✔
55
        if (strv_isempty(arg_prefixes))
26,364✔
56
                return true;
57

58
        return path_startswith_strv(p, arg_prefixes);
23,520✔
59
}
60

61
static Option* option_new(
6,147✔
62
                const char *key,
63
                const char *value,
64
                bool ignore_failure) {
65

66
        _cleanup_(option_freep) Option *o = NULL;
6,147✔
67

68
        assert(key);
6,147✔
69

70
        o = new(Option, 1);
6,147✔
71
        if (!o)
6,147✔
72
                return NULL;
73

74
        *o = (Option) {
12,294✔
75
                .key = strdup(key),
6,147✔
76
                .value = value ? strdup(value) : NULL,
6,147✔
77
                .ignore_failure = ignore_failure,
78
        };
79

80
        if (!o->key)
6,147✔
81
                return NULL;
82
        if (value && !o->value)
6,147✔
83
                return NULL;
84

85
        return TAKE_PTR(o);
6,147✔
86
}
87

88
static int sysctl_write_or_warn(const char *key, const char *value, bool ignore_failure, bool ignore_enoent) {
5,790✔
89
        int r;
5,790✔
90

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

108
        return 0;
109
}
110

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

116
        assert(sysctl_options);
12,123✔
117
        assert(option);
12,123✔
118

119
        if (prefix) {
12,123✔
120
                _cleanup_free_ char *key = NULL;
11,769✔
121

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

131
                log_debug("The glob '%s' is prefixed with '%s': '%s'", option->key, prefix, key);
2,946✔
132

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

140
                        return sysctl_write_or_warn(key, option->value,
2,946✔
141
                                                    /* ignore_failure= */ option->ignore_failure,
2,946✔
142
                                                    /* ignore_enoent= */ true);
143
                }
144

145
                pattern = path_join("/proc/sys", key);
×
146
        } else
147
                pattern = path_join("/proc/sys", option->key);
354✔
148
        if (!pattern)
354✔
149
                return log_oom();
×
150

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

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

165
        STRV_FOREACH(s, paths) {
1,416✔
166
                const char *key = ASSERT_SE_PTR(path_startswith(*s, "/proc/sys"));
1,062✔
167

168
                if (ordered_hashmap_contains(sysctl_options, key)) {
1,062✔
169
                        log_debug("Not setting %s (explicit setting exists).", key);
708✔
170
                        continue;
708✔
171
                }
172

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

179
        return r;
180
}
181

182
static int apply_glob_option(OrderedHashmap *sysctl_options, Option *option) {
3,303✔
183
        int r = 0;
3,303✔
184

185
        if (strv_isempty(arg_prefixes))
3,303✔
186
                return apply_glob_option_with_prefix(sysctl_options, option, NULL);
354✔
187

188
        STRV_FOREACH(i, arg_prefixes)
14,718✔
189
                RET_GATHER(r, apply_glob_option_with_prefix(sysctl_options, option, *i));
11,769✔
190
        return r;
191
}
192

193
static int apply_all(OrderedHashmap *sysctl_options) {
1,113✔
194
        Option *option;
1,113✔
195
        int r = 0;
1,113✔
196

197
        ORDERED_HASHMAP_FOREACH(option, sysctl_options) {
7,260✔
198
                int k;
6,147✔
199

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

204
                if (string_is_glob(option->key))
5,793✔
205
                        k = apply_glob_option(sysctl_options, option);
3,303✔
206
                else
207
                        k = sysctl_write_or_warn(option->key, option->value,
2,490✔
208
                                                 /* ignore_failure= */ option->ignore_failure,
2,490✔
209
                                                 /* ignore_enoent= */ !arg_strict);
2,490✔
210
                RET_GATHER(r, k);
5,793✔
211
        }
212

213
        return r;
1,113✔
214
}
215

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

222
        const char *eq = strchr(buffer, '=');
29,667✔
223
        if (eq) {
29,667✔
224
                if (buffer[0] == '-') {
26,373✔
225
                        ignore_failure = true;
2,202✔
226
                        buffer++;
2,202✔
227
                }
228

229
                k = strndup(buffer, eq - buffer);
26,373✔
230
                if (!k)
26,373✔
231
                        return log_oom();
×
232

233
                v = strdup(eq + 1);
26,373✔
234
                if (!v)
26,373✔
235
                        return log_oom();
×
236

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

245
                k = strdup(buffer);
3,294✔
246
                if (!k)
3,294✔
247
                        return log_oom();
×
248
        }
249

250
        const char *key = sysctl_normalize(strstrip(k)), *value = strstrip(v);
29,667✔
251

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

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

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

268
        _cleanup_(option_freep) Option *option = option_new(key, value, ignore_failure);
12,294✔
269
        if (!option)
6,147✔
270
                return log_oom();
×
271

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

276
        TAKE_PTR(option);
6,147✔
277
        return 0;
6,147✔
278
}
279

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

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

296
        r = get_credentials_dir(&d);
1,098✔
297
        if (r == -ENXIO)
1,098✔
298
                return 0;
299
        if (r < 0)
118✔
300
                return log_error_errno(r, "Failed to get credentials directory: %m");
×
301

302
        j = path_join(d, "sysctl.extra");
118✔
303
        if (!j)
118✔
304
                return log_oom();
×
305

306
        return parse_file(sysctl_options, j, /* ignore_enoent= */ true);
118✔
307
}
308

309
static int cat_config(char **files) {
×
310
        pager_open(arg_pager_flags);
×
311

312
        return cat_files(NULL, files, arg_cat_flags);
×
313
}
314

315
static int help(void) {
×
316
        _cleanup_free_ char *link = NULL;
×
317
        int r;
×
318

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

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

342
        return 0;
343
}
344

345
static int parse_argv(int argc, char *argv[]) {
1,113✔
346

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

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

369
        int c;
1,113✔
370

371
        assert(argc >= 0);
1,113✔
372
        assert(argv);
1,113✔
373

374
        while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
5,047✔
375

376
                switch (c) {
3,934✔
377

378
                case 'h':
×
379
                        return help();
×
380

381
                case ARG_VERSION:
×
382
                        return version();
×
383

384
                case ARG_CAT_CONFIG:
×
385
                        arg_cat_flags = CAT_CONFIG_ON;
×
386
                        break;
×
387

388
                case ARG_TLDR:
×
389
                        arg_cat_flags = CAT_TLDR;
×
390
                        break;
×
391

392
                case ARG_PREFIX: {
3,923✔
393
                        const char *s;
3,923✔
394
                        char *p;
3,923✔
395

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

402
                        s = path_startswith(optarg, "/proc/sys");
3,923✔
403
                        p = strdup(s ?: optarg);
3,923✔
404
                        if (!p)
3,923✔
405
                                return log_oom();
×
406

407
                        if (strv_consume(&arg_prefixes, p) < 0)
3,923✔
408
                                return log_oom();
×
409

410
                        break;
411
                }
412

413
                case ARG_NO_PAGER:
×
414
                        arg_pager_flags |= PAGER_DISABLE;
×
415
                        break;
×
416

417
                case ARG_STRICT:
6✔
418
                        arg_strict = true;
6✔
419
                        break;
6✔
420

421
                case ARG_INLINE:
5✔
422
                        arg_inline = true;
5✔
423
                        break;
5✔
424

425
                case '?':
426
                        return -EINVAL;
427

428
                default:
×
429
                        assert_not_reached();
×
430
                }
431

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

436
        return 1;
437
}
438

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

443
        r = parse_argv(argc, argv);
1,113✔
444
        if (r <= 0)
1,113✔
445
                return r;
446

447
        log_setup();
1,113✔
448

449
        umask(0022);
1,113✔
450

451
        if (argc > optind) {
1,113✔
452
                unsigned pos = 0;
15✔
453

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

464
                r = conf_files_list_strv(&files, ".conf", /* root= */ NULL, CONF_FILES_WARN,
2,196✔
465
                                         (const char**) CONF_PATHS_STRV("sysctl.d"));
1,098✔
466
                if (r < 0)
1,098✔
467
                        return log_error_errno(r, "Failed to enumerate sysctl.d files: %m");
×
468

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

472
                STRV_FOREACH(f, files)
6,588✔
473
                        RET_GATHER(r, parse_file(&sysctl_options, *f, true));
5,490✔
474

475
                RET_GATHER(r, read_credential_lines(&sysctl_options));
1,098✔
476
        }
477

478
        return RET_GATHER(r, apply_all(sysctl_options));
1,113✔
479
}
480

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