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

systemd / systemd / 14554080340

19 Apr 2025 11:46AM UTC coverage: 72.101% (-0.03%) from 72.13%
14554080340

push

github

web-flow
Add two new paragraphs to coding style about header files (#37188)

296880 of 411754 relevant lines covered (72.1%)

687547.52 hits per line

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

86.0
/src/core/kmod-setup.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <unistd.h>
4

5
#include "alloc-util.h"
6
#include "bus-util.h"
7
#include "capability-util.h"
8
#include "efi-api.h"
9
#include "fileio.h"
10
#include "kmod-setup.h"
11
#include "log.h"
12
#include "macro.h"
13
#include "module-util.h"
14
#include "recurse-dir.h"
15
#include "string-util.h"
16
#include "strv.h"
17
#include "virt.h"
18

19
#if HAVE_KMOD
20
static int match_modalias_recurse_dir_cb(
20,713✔
21
                RecurseDirEvent event,
22
                const char *path,
23
                int dir_fd,
24
                int inode_fd,
25
                const struct dirent *de,
26
                const struct statx *sx,
27
                void *userdata) {
28

29
        _cleanup_free_ char *alias = NULL;
20,713✔
30
        char **modaliases = ASSERT_PTR(userdata);
20,713✔
31
        int r;
20,713✔
32

33
        if (event != RECURSE_DIR_ENTRY)
20,713✔
34
                return RECURSE_DIR_CONTINUE;
35

36
        if (de->d_type != DT_REG)
15,093✔
37
                return RECURSE_DIR_CONTINUE;
38

39
        if (!streq(de->d_name, "modalias"))
13,608✔
40
                return RECURSE_DIR_CONTINUE;
41

42
        r = read_one_line_file(path, &alias);
565✔
43
        if (r < 0) {
565✔
44
                log_debug_errno(r, "Failed to read %s, ignoring: %m", path);
×
45
                return RECURSE_DIR_LEAVE_DIRECTORY;
×
46
        }
47

48
        if (startswith_strv(alias, modaliases))
565✔
49
                return 1;
45✔
50

51
        return RECURSE_DIR_LEAVE_DIRECTORY;
52
}
53

54
static bool has_virtio_feature(const char *name, char **modaliases) {
60✔
55
        int r;
60✔
56

57
        /* Directory traversal might be slow, hence let's do a cheap check first if it's even worth it */
58
        if (detect_vm() == VIRTUALIZATION_NONE)
60✔
59
                return false;
60

61
        r = recurse_dir_at(
60✔
62
                        AT_FDCWD,
63
                        "/sys/devices/pci0000:00",
64
                        /* statx_mask= */ 0,
65
                        /* n_depth_max= */ 3,
66
                        RECURSE_DIR_ENSURE_TYPE,
67
                        match_modalias_recurse_dir_cb,
68
                        modaliases);
69
        if (r < 0)
60✔
70
                log_debug_errno(r, "Failed to determine whether host has %s device, ignoring: %m", name);
×
71

72
        return r > 0;
60✔
73
}
74

75
static bool has_virtio_rng(void) {
15✔
76
        return has_virtio_feature("virtio-rng", STRV_MAKE("pci:v00001AF4d00001005", "pci:v00001AF4d00001044"));
15✔
77
}
78

79
static bool has_virtio_console(void) {
15✔
80
        return has_virtio_feature("virtio-console", STRV_MAKE("virtio:d00000003v", "virtio:d0000000Bv"));
15✔
81
}
82

83
static bool has_virtio_vsock(void) {
15✔
84
        return has_virtio_feature("virtio-vsock", STRV_MAKE("virtio:d00000013v"));
15✔
85
}
86

87
static bool has_virtiofs(void) {
15✔
88
        return has_virtio_feature("virtiofs", STRV_MAKE("virtio:d0000001Av"));
15✔
89
}
90

91
static bool has_virtio_pci(void) {
×
92
        return has_virtio_feature("virtio-pci", STRV_MAKE("pci:v00001AF4d"));
×
93
}
94

95
static bool in_qemu(void) {
12✔
96
        return IN_SET(detect_vm(), VIRTUALIZATION_KVM, VIRTUALIZATION_QEMU);
12✔
97
}
98
#endif
99

100
int kmod_setup(void) {
33✔
101
#if HAVE_KMOD
102
        static const struct {
33✔
103
                const char *module;
104
                const char *path;
105
                bool warn_if_unavailable;
106
                bool warn_if_module;
107
                bool (*condition_fn)(void);
108
        } kmod_table[] = {
109
                /* This one we need to load explicitly, since auto-loading on use doesn't work
110
                 * before udev created the ghost device nodes, and we need it earlier than that. */
111
                { "autofs4",                    "/sys/class/misc/autofs",    true,  false, NULL               },
112

113
                /* This one we need to load explicitly, since auto-loading of IPv6 is not done when
114
                 * we try to configure ::1 on the loopback device. */
115
                { "ipv6",                       "/sys/module/ipv6",          false, true,  NULL               },
116

117
                /* This should never be a module */
118
                { "unix",                       "/proc/net/unix",            true,  true,  NULL               },
119

120
#if HAVE_LIBIPTC
121
                /* netfilter is needed by networkd, nspawn among others, and cannot be autoloaded */
122
                { "ip_tables",                  "/proc/net/ip_tables_names", false, false, NULL               },
123
#endif
124
                /* virtio_rng would be loaded by udev later, but real entropy might be needed very early */
125
                { "virtio_rng",                 NULL,                        false, false, has_virtio_rng     },
126

127
                /* we want early logging to hvc consoles if possible, and make sure systemd-getty-generator
128
                 * can rely on all consoles being probed already. */
129
                { "virtio_console",             NULL,                        false, false, has_virtio_console },
130

131
                /* Make sure we can send sd-notify messages over vsock as early as possible. */
132
                { "vmw_vsock_virtio_transport", NULL,                        false, false, has_virtio_vsock   },
133

134
                /* We can't wait for specific virtiofs tags to show up as device nodes so we have to load the
135
                 * virtiofs and virtio_pci modules early to make sure the virtiofs tags are found when
136
                 * sysroot.mount is started.
137
                 *
138
                 * TODO: Remove these again once https://gitlab.com/virtio-fs/virtiofsd/-/issues/128 is
139
                 * resolved and the kernel fix is widely available. */
140
                { "virtiofs",                   "/sys/module/virtiofs",      false, false, has_virtiofs       },
141
                { "virtio_pci",                 "/sys/module/virtio_pci",    false, false, has_virtio_pci     },
142

143
                /* qemu_fw_cfg would be loaded by udev later, but we want to import credentials from it super early */
144
                { "qemu_fw_cfg",                "/sys/firmware/qemu_fw_cfg", false, false, in_qemu            },
145

146
                /* dmi-sysfs is needed to import credentials from it super early */
147
                { "dmi-sysfs",                  "/sys/firmware/dmi/entries", false, false, NULL               },
148

149
#if HAVE_TPM2
150
                /* Make sure the tpm subsystem is available which ConditionSecurity=tpm2 depends on. */
151
                { "tpm",                        "/sys/class/tpmrm",          false, false, efi_has_tpm2       },
152
#endif
153
        };
154

155
        int r;
33✔
156

157
        if (have_effective_cap(CAP_SYS_MODULE) <= 0)
33✔
158
                return 0;
33✔
159

160
        _cleanup_(sym_kmod_unrefp) struct kmod_ctx *ctx = NULL;
15✔
161
        FOREACH_ELEMENT(kmod, kmod_table) {
195✔
162
                if (kmod->path && access(kmod->path, F_OK) >= 0)
180✔
163
                        continue;
93✔
164

165
                if (kmod->condition_fn && !kmod->condition_fn())
87✔
166
                        continue;
15✔
167

168
                if (kmod->warn_if_module)
72✔
169
                        log_debug("Your kernel apparently lacks built-in %s support. Might be "
×
170
                                  "a good idea to compile it in. We'll now try to work around "
171
                                  "this by loading the module...", kmod->module);
172

173
                if (!ctx) {
72✔
174
                        r = module_setup_context(&ctx);
15✔
175
                        if (r < 0)
15✔
176
                                return log_error_errno(r, "Failed to initialize kmod context: %m");
×
177
                }
178

179
                (void) module_load_and_warn(ctx, kmod->module, kmod->warn_if_unavailable);
72✔
180
        }
181

182
#endif
183
        return 0;
184
}
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