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

systemd / systemd / 14872145375

06 May 2025 09:07PM UTC coverage: 72.232% (+0.02%) from 72.214%
14872145375

push

github

DaanDeMeyer
string-table: annotate _to_string and _from_string with _const_ and _pure_, respectively

Follow-up for c94f6ab1b

297286 of 411572 relevant lines covered (72.23%)

695615.99 hits per line

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

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

3
#include <errno.h>
4
#include <getopt.h>
5
#include <limits.h>
6
#include <sys/stat.h>
7

8
#include "build.h"
9
#include "conf-files.h"
10
#include "constants.h"
11
#include "errno-util.h"
12
#include "fd-util.h"
13
#include "fileio.h"
14
#include "log.h"
15
#include "main-func.h"
16
#include "module-util.h"
17
#include "pretty-print.h"
18
#include "proc-cmdline.h"
19
#include "string-util.h"
20
#include "strv.h"
21

22
static char **arg_proc_cmdline_modules = NULL;
23
static const char conf_file_dirs[] = CONF_PATHS_NULSTR("modules-load.d");
24

25
STATIC_DESTRUCTOR_REGISTER(arg_proc_cmdline_modules, strv_freep);
69✔
26

27
static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
2,867✔
28
        int r;
2,867✔
29

30
        if (proc_cmdline_key_streq(key, "modules_load")) {
2,867✔
31

32
                if (proc_cmdline_value_missing(key, value))
4✔
33
                        return 0;
34

35
                r = strv_split_and_extend(&arg_proc_cmdline_modules, value, ",", /* filter_duplicates = */ true);
4✔
36
                if (r < 0)
4✔
37
                        return log_error_errno(r, "Failed to parse modules_load= kernel command line option: %m");
×
38
        }
39

40
        return 0;
41
}
42

43
static int apply_file(struct kmod_ctx *ctx, const char *path, bool ignore_enoent) {
68✔
44
        _cleanup_fclose_ FILE *f = NULL;
68✔
45
        _cleanup_free_ char *pp = NULL;
68✔
46
        int r;
68✔
47

48
        assert(ctx);
68✔
49
        assert(path);
68✔
50

51
        r = search_and_fopen_nulstr(path, "re", NULL, conf_file_dirs, &f, &pp);
68✔
52
        if (r < 0) {
68✔
53
                if (ignore_enoent && r == -ENOENT)
1✔
54
                        return 0;
55

56
                return log_error_errno(r, "Failed to open %s: %m", path);
1✔
57
        }
58

59
        log_debug("apply: %s", pp);
67✔
60
        for (;;) {
150✔
61
                _cleanup_free_ char *line = NULL;
83✔
62
                int k;
150✔
63

64
                k = read_stripped_line(f, LONG_LINE_MAX, &line);
150✔
65
                if (k < 0)
150✔
66
                        return log_error_errno(k, "Failed to read file '%s': %m", pp);
×
67
                if (k == 0)
150✔
68
                        break;
69

70
                if (isempty(line))
83✔
71
                        continue;
2✔
72
                if (strchr(COMMENTS, *line))
81✔
73
                        continue;
65✔
74

75
                k = module_load_and_warn(ctx, line, true);
16✔
76
                if (k == -ENOENT)
16✔
77
                        continue;
7✔
78
                RET_GATHER(r, k);
9✔
79
        }
80

81
        return r;
67✔
82
}
83

84
static int help(void) {
1✔
85
        _cleanup_free_ char *link = NULL;
1✔
86
        int r;
1✔
87

88
        r = terminal_urlify_man("systemd-modules-load.service", "8", &link);
1✔
89
        if (r < 0)
1✔
90
                return log_oom();
×
91

92
        printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
1✔
93
               "Loads statically configured kernel modules.\n\n"
94
               "  -h --help             Show this help\n"
95
               "     --version          Show package version\n"
96
               "\nSee the %s for details.\n",
97
               program_invocation_short_name,
98
               link);
99

100
        return 0;
101
}
102

103
static int parse_argv(int argc, char *argv[]) {
69✔
104
        enum {
69✔
105
                ARG_VERSION = 0x100,
106
        };
107

108
        static const struct option options[] = {
69✔
109
                { "help",      no_argument,       NULL, 'h'           },
110
                { "version",   no_argument,       NULL, ARG_VERSION   },
111
                {}
112
        };
113

114
        int c;
69✔
115

116
        assert(argc >= 0);
69✔
117
        assert(argv);
69✔
118

119
        while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
69✔
120
                switch (c) {
3✔
121

122
                case 'h':
1✔
123
                        return help();
1✔
124

125
                case ARG_VERSION:
1✔
126
                        return version();
1✔
127

128
                case '?':
129
                        return -EINVAL;
130

131
                default:
×
132
                        assert_not_reached();
×
133
                }
134

135
        return 1;
136
}
137

138
static int run(int argc, char *argv[]) {
69✔
139
        _cleanup_(sym_kmod_unrefp) struct kmod_ctx *ctx = NULL;
69✔
140
        int r, k;
69✔
141

142
        r = parse_argv(argc, argv);
69✔
143
        if (r <= 0)
69✔
144
                return r;
145

146
        log_setup();
66✔
147

148
        umask(0022);
66✔
149

150
        r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX);
66✔
151
        if (r < 0)
66✔
152
                log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
×
153

154
        r = module_setup_context(&ctx);
66✔
155
        if (r < 0)
66✔
156
                return log_error_errno(r, "Failed to initialize libkmod context: %m");
×
157

158
        r = 0;
66✔
159

160
        if (argc > optind) {
66✔
161
                for (int i = optind; i < argc; i++)
4✔
162
                        RET_GATHER(r, apply_file(ctx, argv[i], false));
2✔
163

164
        } else {
165
                _cleanup_strv_free_ char **files = NULL;
64✔
166

167
                STRV_FOREACH(i, arg_proc_cmdline_modules) {
66✔
168
                        k = module_load_and_warn(ctx, *i, true);
2✔
169
                        if (k == -ENOENT)
2✔
170
                                continue;
1✔
171
                        RET_GATHER(r, k);
1✔
172
                }
173

174
                k = conf_files_list_nulstr(&files, ".conf", NULL, 0, conf_file_dirs);
64✔
175
                if (k < 0)
64✔
176
                        return log_error_errno(k, "Failed to enumerate modules-load.d files: %m");
×
177

178
                STRV_FOREACH(fn, files)
130✔
179
                        RET_GATHER(r, apply_file(ctx, *fn, true));
66✔
180
        }
181

182
        return r;
183
}
184

185
DEFINE_MAIN_FUNCTION(run);
69✔
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