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

systemd / systemd / 25705508282

11 May 2026 11:07PM UTC coverage: 72.65% (+0.1%) from 72.511%
25705508282

push

github

bluca
firstboot,sysinstall,hostnamed: always show FANCY_NAME=

This makes sure that whenever we want to show the OS name we can show
the fancy name. Thus this moves the escaping/validation of the fancy
name out of hostnamed into generic code, and then makes use of it in
sysinstall,firstboot,prompt-util.

17 of 36 new or added lines in 6 files covered. (47.22%)

2673 existing lines in 72 files now uncovered.

327104 of 450245 relevant lines covered (72.65%)

1200575.51 hits per line

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

54.36
/src/modules-load/modules-load.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <limits.h>
4
#include <pthread.h>
5
#include <signal.h>
6
#include <sys/socket.h>
7
#include <unistd.h>
8

9
#include "alloc-util.h"
10
#include "assert.h"
11
#include "build.h"
12
#include "conf-files.h"
13
#include "constants.h"
14
#include "errno-util.h"
15
#include "fd-util.h"
16
#include "fileio.h"
17
#include "format-table.h"
18
#include "log.h"
19
#include "macro.h"
20
#include "main-func.h"
21
#include "module-util.h"
22
#include "options.h"
23
#include "ordered-set.h"
24
#include "parse-util.h"
25
#include "pretty-print.h"
26
#include "proc-cmdline.h"
27
#include "string-util.h"
28
#include "strv.h"
29

30
#define MODULE_NAME_MAX_LEN (4096UL)
31

32
static char **arg_proc_cmdline_modules = NULL;
33
static const char conf_file_dirs[] = CONF_PATHS_NULSTR("modules-load.d");
34

35
STATIC_DESTRUCTOR_REGISTER(arg_proc_cmdline_modules, strv_freep);
75✔
36

37
static int modules_list_append_suffix(OrderedSet **module_set, char *modp) {
90✔
38
        /* take possession of string no matter what */
39
        _cleanup_free_ char *mod = modp;
90✔
40
        int r;
90✔
41

42
        assert(module_set);
90✔
43
        assert(mod);
90✔
44

45
        if (strlen(mod) > MODULE_NAME_MAX_LEN)
90✔
46
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Module name max length exceeded (%lu): %s",
×
47
                                       MODULE_NAME_MAX_LEN, mod);
48

49
        /* kmod will do it anyway later, so replace now dashes with
50
           underscores to detect duplications due to different spelling. */
51
        string_replace_char(mod, '-', '_');
90✔
52

53
        r = ordered_set_ensure_put(module_set, &string_hash_ops_free, mod);
90✔
54
        if (r == -EEXIST)
90✔
55
                return 0;
56
        if (r < 0)
82✔
57
                return log_error_errno(r, "Failed to enqueue module '%s': %m", mod);
×
58

59
        TAKE_PTR(mod);
60

61
        return 0;
62
}
63

64
static int modules_list_append_dup(OrderedSet **module_set, const char *module) {
2✔
65
        _cleanup_free_ char *m = NULL;
2✔
66

67
        assert(module);
2✔
68

69
        if (strdup_to(&m, module) < 0)
2✔
70
                return log_oom();
×
71

72
        return modules_list_append_suffix(module_set, TAKE_PTR(m));
2✔
73
}
74

75
static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
3,146✔
76
        int r;
3,146✔
77

78
        if (proc_cmdline_key_streq(key, "modules_load")) {
3,146✔
79

80
                if (proc_cmdline_value_missing(key, value))
4✔
81
                        return 0;
82

83
                r = strv_split_and_extend(&arg_proc_cmdline_modules, value, ",", /* filter_duplicates= */ true);
4✔
84
                if (r < 0)
4✔
85
                        return log_error_errno(r, "Failed to parse modules_load= kernel command line option: %m");
×
86
        }
87

88
        return 0;
89
}
90

91
static int apply_file(FILE *f, const char *filename, OrderedSet **module_set) {
73✔
92
        int ret = 0, r;
73✔
93

94
        assert(f);
73✔
95

96
        log_debug("apply: %s", filename);
73✔
97
        for (;;) {
794✔
98
                _cleanup_free_ char *line = NULL;
721✔
99

100
                r = read_stripped_line(f, LONG_LINE_MAX, &line);
794✔
101
                if (r < 0)
794✔
102
                        return log_error_errno(r, "Failed to read file '%s': %m", filename);
×
103
                if (r == 0)
794✔
104
                        break;
105

106
                if (isempty(line))
721✔
107
                        continue;
2✔
108
                if (strchr(COMMENTS, *line))
719✔
109
                        continue;
631✔
110

111
                RET_GATHER(ret, modules_list_append_suffix(module_set, TAKE_PTR(line)));
88✔
112
        }
113

114
        return ret;
73✔
115
}
116

117
static int apply_file_from_path(const char *path, OrderedSet **module_set) {
2✔
118
        _cleanup_fclose_ FILE *f = NULL;
2✔
119
        _cleanup_free_ char *pp = NULL;
2✔
120
        int r;
2✔
121

122
        assert(path);
2✔
123

124
        r = search_and_fopen_nulstr(path, "re", NULL, conf_file_dirs, &f, &pp);
2✔
125
        if (r < 0)
2✔
126
                return log_error_errno(r, "Failed to open %s: %m", path);
1✔
127

128
        return apply_file(f, pp, module_set);
1✔
129
}
130

131
static int apply_conf_file(ConfFile *c, OrderedSet **module_set) {
72✔
132
        _cleanup_fclose_ FILE *f = NULL;
72✔
133

134
        f = fopen(FORMAT_PROC_FD_PATH(c->fd), "re");
72✔
135
        if (!f) {
72✔
136
                if (errno == ENOENT)
×
137
                        return 0;
138

139
                return log_error_errno(errno, "Failed to open %s: %m", c->original_path);
×
140
        }
141

142
        return apply_file(f, c->original_path, module_set);
72✔
143
}
144

145
static int do_direct_probe(OrderedSet *module_set) {
72✔
146
        _cleanup_(kmod_unrefp) struct kmod_ctx *ctx = NULL;
72✔
147
        char *module;
72✔
148
        int ret = 0, r;
72✔
149

150
        r = module_setup_context(&ctx);
72✔
151
        if (r < 0)
72✔
152
                return log_error_errno(r, "Failed to initialize libkmod context: %m");
×
153

154
        ORDERED_SET_FOREACH(module, module_set) {
154✔
155
                r = module_load_and_warn(ctx, module, /* verbose= */ true);
82✔
156
                if (r != -ENOENT)
82✔
157
                        RET_GATHER(ret, r);
74✔
158
        }
159

160
        return ret;
72✔
161
}
162

UNCOV
163
static int enqueue_module_to_load(int sock, const char *module) {
×
UNCOV
164
        ssize_t bytes;
×
165

UNCOV
166
        assert(sock >= 0);
×
UNCOV
167
        assert(module);
×
168

UNCOV
169
        bytes = send(sock, module, strlen(module), /* flags= */ 0);
×
UNCOV
170
        if (bytes < 0)
×
171
                return log_error_errno(errno, "Failed to send '%s' to thread pool: %m", module);
×
172

173
        return 0;
174
}
175

UNCOV
176
static int dequeue_module_to_load(int sock, char *buffer, size_t buffer_len) {
×
UNCOV
177
        ssize_t bytes;
×
178

UNCOV
179
        assert(sock >= 0);
×
UNCOV
180
        assert(buffer);
×
181

182
        /* Dequeue one module to be loaded from the socket pair. In case no more
183
         * modules are present, recv() will return 0. */
UNCOV
184
        bytes = recv(sock, buffer, buffer_len, /* flags= */ 0);
×
UNCOV
185
        if (bytes == 0)
×
186
                return 0;
UNCOV
187
        if (bytes < 0)
×
188
                return negative_errno();
×
UNCOV
189
        if ((size_t)bytes == buffer_len)
×
190
                return -E2BIG;
191

UNCOV
192
        buffer[bytes] = '\0';
×
193

UNCOV
194
        return 1;
×
195
}
196

UNCOV
197
static int run_prober(int sock) {
×
UNCOV
198
        _cleanup_(kmod_unrefp) struct kmod_ctx *ctx = NULL;
×
UNCOV
199
        char buffer[MODULE_NAME_MAX_LEN + 1];
×
UNCOV
200
        int ret = 0, r;
×
201

UNCOV
202
        r = module_setup_context(&ctx);
×
UNCOV
203
        if (r < 0)
×
204
                return log_error_errno(r, "Failed to initialize libkmod context: %m");
×
205

UNCOV
206
        for (;;) {
×
UNCOV
207
                r = dequeue_module_to_load(sock, buffer, sizeof(buffer));
×
UNCOV
208
                if (ERRNO_IS_NEG_TRANSIENT(r))
×
209
                        continue;
×
UNCOV
210
                if (r == -E2BIG) {
×
211
                        log_warning_errno(SYNTHETIC_ERRNO(E2BIG), "Dequeued module name too long, proceeding anyway");
×
212
                        continue;
×
213
                }
UNCOV
214
                if (r < 0)
×
215
                        return log_error_errno(r, "Failed to receive from module queue: %m");
×
UNCOV
216
                if (r == 0) {
×
UNCOV
217
                        log_debug("No more queued modules, terminate thread");
×
218
                        break;
219
                }
220

UNCOV
221
                r = module_load_and_warn(ctx, buffer, /* verbose= */ true);
×
UNCOV
222
                if (r != -ENOENT)
×
UNCOV
223
                        RET_GATHER(ret, r);
×
224
        }
225

226
        return ret;
227
}
228

UNCOV
229
static void *prober_thread(void *arg) {
×
UNCOV
230
        int sock = PTR_TO_FD(arg);
×
UNCOV
231
        return INT_TO_PTR(run_prober(sock));
×
232
}
233

UNCOV
234
static int create_worker_threads(size_t n_threads, void *arg, pthread_t **ret_threads, size_t *ret_n_threads) {
×
UNCOV
235
        _cleanup_free_ pthread_t *new_threads = NULL;
×
UNCOV
236
        size_t created_threads;
×
UNCOV
237
        sigset_t ss, saved_ss;
×
UNCOV
238
        int r;
×
239

UNCOV
240
        assert(ret_threads);
×
UNCOV
241
        assert(ret_n_threads);
×
242

UNCOV
243
        if (n_threads == 0) {
×
244
                *ret_threads = NULL;
×
245
                *ret_n_threads = 0;
×
246
                return 0;
×
247
        }
248

249
        /* Create worker threads with masked signals */
UNCOV
250
        new_threads = new(pthread_t, n_threads);
×
UNCOV
251
        if (!new_threads)
×
252
                return log_oom();
×
253

254
        /* No signals in worker threads. */
UNCOV
255
        assert_se(sigfillset(&ss) >= 0);
×
UNCOV
256
        r = pthread_sigmask(SIG_BLOCK, &ss, &saved_ss);
×
UNCOV
257
        if (r != 0)
×
258
                return log_error_errno(r, "Failed to mask signals for workers: %m");
×
259

UNCOV
260
        for (created_threads = 0; created_threads < n_threads; ++created_threads) {
×
UNCOV
261
                r = pthread_create(&new_threads[created_threads], /* attr= */ NULL, prober_thread, arg);
×
UNCOV
262
                if (r != 0) {
×
263
                        log_error_errno(r, "Failed to create worker thread %zu: %m", created_threads);
×
264
                        break;
265
                }
266
        }
267

268
        /* Restore the signal mask */
UNCOV
269
        r = pthread_sigmask(SIG_SETMASK, &saved_ss, NULL);
×
UNCOV
270
        if (r != 0)
×
271
                log_warning_errno(r, "Failed to restore signal mask, ignoring: %m");
×
272

UNCOV
273
        *ret_threads = TAKE_PTR(new_threads);
×
UNCOV
274
        *ret_n_threads = created_threads;
×
275

UNCOV
276
        return 0;
×
277
}
278

UNCOV
279
static int destroy_worker_threads(pthread_t **threads, size_t n_threads) {
×
UNCOV
280
        int ret = 0, r;
×
281

UNCOV
282
        assert(threads);
×
UNCOV
283
        assert(n_threads == 0 || *threads);
×
284

UNCOV
285
        for (size_t i = 0; i < n_threads; ++i) {
×
UNCOV
286
                void *p;
×
UNCOV
287
                r = pthread_join((*threads)[i], &p);
×
UNCOV
288
                if (r != 0)
×
289
                        RET_GATHER(ret, log_warning_errno(r, "Failed to join worker thread, proceeding anyway: %m"));
×
290
                else
UNCOV
291
                        RET_GATHER(ret, PTR_TO_INT(p));
×
292
        }
293

UNCOV
294
        *threads = mfree(*threads);
×
295

UNCOV
296
        return ret;
×
297
}
298

299
/* Determine number of workers, either from env or from online CPUs */
300
static unsigned determine_num_worker_threads(unsigned n_modules) {
72✔
301
        unsigned n_threads = UINT_MAX;
72✔
302
        int r;
72✔
303

304
        if (n_modules == 0)
72✔
305
                return 0;
72✔
306

307
        const char *e = secure_getenv("SYSTEMD_MODULES_LOAD_NUM_THREADS");
71✔
308
        if (e) {
71✔
309
                r = safe_atou(e, &n_threads);
×
310
                if (r < 0)
×
311
                        log_debug("Invalid value in $SYSTEMD_MODULES_LOAD_NUM_THREADS, ignoring: %s", e);
×
312
        }
313

314
        if (n_threads == UINT_MAX) {
71✔
315
                /* By default, use a number of worker threads equal the number of online CPUs,
316
                 * but clamp it to avoid a probing storm on machines with many CPUs. */
317
                long ncpus = sysconf(_SC_NPROCESSORS_ONLN);
71✔
318
                if (ncpus < 0) {
71✔
319
                        log_warning_errno(errno, "Failed to get number of online CPUs, ignoring: %m");
×
320
                        ncpus = 1;
321
                }
322

323
                n_threads = CLAMP((unsigned)ncpus, 1U, 16U);
71✔
324
        }
325

326
        /* There's no reason to spawn more threads than the modules that need to be loaded */
327
        n_threads = CLAMP(n_threads, 1U, n_modules);
71✔
328

329
        /* One of the probe threads is the main process */
330
        return n_threads - 1U;
71✔
331
}
332

333
static int help(void) {
1✔
334
        _cleanup_free_ char *link = NULL;
1✔
335
        _cleanup_(table_unrefp) Table *options = NULL;
1✔
336
        int r;
1✔
337

338
        if (terminal_urlify_man("systemd-modules-load.service", "8", &link) < 0)
1✔
339
                return log_oom();
×
340

341
        r = option_parser_get_help_table(&options);
1✔
342
        if (r < 0)
1✔
343
                return r;
344

345
        printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
1✔
346
               "Loads statically configured kernel modules.\n\n",
347
               program_invocation_short_name);
348

349
        r = table_print_or_warn(options);
1✔
350
        if (r < 0)
1✔
351
                return r;
352

353
        printf("\nSee the %s for details.\n", link);
1✔
354
        return 0;
355
}
356

357
static int parse_argv(int argc, char *argv[], char ***ret_args) {
75✔
358
        assert(argc >= 0);
75✔
359
        assert(argv);
75✔
360
        assert(ret_args);
75✔
361

362
        OptionParser opts = { argc, argv };
75✔
363

364
        FOREACH_OPTION_OR_RETURN(c, &opts)
75✔
365
                switch (c) {
2✔
366

367
                OPTION_COMMON_HELP:
1✔
368
                        return help();
1✔
369

370
                OPTION_COMMON_VERSION:
1✔
371
                        return version();
1✔
372
                }
373

374
        *ret_args = option_parser_get_args(&opts);
72✔
375
        return 1;
72✔
376
}
377

378
static int run(int argc, char *argv[]) {
75✔
379
        /* A OrderedSet instead of a plain Set is used here to make debug easier:
380
         * in case a race condition is observed during module probing, the threading
381
         * can be disabled through the SYSTEMD_MODULES_LOAD_NUM_THREADS environment
382
         * variable and the modules reordered at will by the user that is debugging it.
383
         * In that case, the probing order would be the same in which the modules
384
         * appear inside the modules-load.d files (this wouldn't be true with a Set). */
385
        _cleanup_ordered_set_free_ OrderedSet *module_set = NULL;
×
386
        _cleanup_close_pair_ int pair[2] = EBADF_PAIR;
75✔
387
        _cleanup_free_ pthread_t *threads = NULL;
75✔
388
        size_t n_threads = 0;
75✔
389
        char *module;
75✔
390
        int ret = 0, r;
75✔
391

392
        char **args = NULL;
75✔
393
        r = parse_argv(argc, argv, &args);
75✔
394
        if (r <= 0)
75✔
395
                return r;
396

397
        log_setup();
72✔
398

399
        umask(0022);
72✔
400

401
        r = proc_cmdline_parse(parse_proc_cmdline_item, /* userdata= */ NULL, PROC_CMDLINE_STRIP_RD_PREFIX);
72✔
402
        if (r < 0)
72✔
403
                log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
×
404

405
        if (!strv_isempty(args)) {
72✔
406
                STRV_FOREACH(i, args) {
4✔
407
                        r = apply_file_from_path(*i, &module_set);
2✔
408
                        if (r < 0)
2✔
409
                                RET_GATHER(ret, r);
1✔
410
                }
411
        } else {
412
                ConfFile **files = NULL;
70✔
413
                size_t n_files = 0;
70✔
414

415
                CLEANUP_ARRAY(files, n_files, conf_file_free_array);
70✔
416

417
                STRV_FOREACH(i, arg_proc_cmdline_modules)
72✔
418
                        RET_GATHER(ret, modules_list_append_dup(&module_set, *i));
2✔
419

420
                r = conf_files_list_nulstr_full(".conf", /* root= */ NULL,
70✔
421
                                                CONF_FILES_REGULAR | CONF_FILES_FILTER_MASKED | CONF_FILES_WARN,
422
                                                conf_file_dirs, &files, &n_files);
423
                if (r < 0)
70✔
424
                        RET_GATHER(ret, log_error_errno(r, "Failed to enumerate modules-load.d files: %m"));
×
425
                else
426
                        FOREACH_ARRAY(cf, files, n_files)
142✔
427
                                RET_GATHER(ret, apply_conf_file(*cf, &module_set));
72✔
428
        }
429

430
        n_threads = determine_num_worker_threads((size_t) ordered_set_size(module_set));
72✔
431

432
        /* If no additional thread is required, there is no need to create the
433
         * thread pool or the mean to communicate with its members. */
434
        if (n_threads == 0) {
72✔
435
                log_debug("Single-threaded probe");
72✔
436
                return RET_GATHER(ret, do_direct_probe(module_set));
72✔
437
        }
438

439
        /* Create a socketpair for communication with probe workers */
UNCOV
440
        r = RET_NERRNO(socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC, /* protocol= */ 0, pair));
×
441
        if (r < 0)
×
442
                return log_error_errno(r, "Failed to create socket pair: %m");
×
443

444
        /* Create threads, which will then wait for modules to probe. */
UNCOV
445
        log_info("Using %zu probe threads", n_threads + 1);
×
UNCOV
446
        r = create_worker_threads(n_threads, FD_TO_PTR(pair[1]), &threads, &n_threads);
×
UNCOV
447
        if (r < 0)
×
448
                log_warning_errno(r, "Failed to create probe threads, continuing as single-threaded probe");
×
449

450
        /* Send modules to be probed */
UNCOV
451
        ORDERED_SET_FOREACH(module, module_set)
×
UNCOV
452
                RET_GATHER(ret, enqueue_module_to_load(pair[0], module));
×
453

454
        /* Close one end of the socketpair; workers will run until the queue is empty */
UNCOV
455
        pair[0] = safe_close(pair[0]);
×
456

457
        /* Run the prober function also in original thread */
UNCOV
458
        RET_GATHER(ret, run_prober(pair[1]));
×
459

460
        /* Wait for all threads (if any) to finish and gather errors */
UNCOV
461
        RET_GATHER(ret, destroy_worker_threads(&threads, n_threads));
×
462

463
        return ret;
464
}
465

466
DEFINE_MAIN_FUNCTION(run);
75✔
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