Move module-util.h to src/shared/ and load_module() to libshared

Unfortunately this needs libshared to link to libkmod. Before it was linked
into systemd-udevd, udevadm, and systemd each seperately. On most systems this
doesn't make much difference, because at least systemd would be installed, but
it might not be in small chroots. It is a small library, so I hope this is not
a big issue.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2018-07-06 11:57:54 +02:00
parent 6755bb5548
commit 3cb9b42af3
5 changed files with 71 additions and 62 deletions

View File

@ -121,7 +121,6 @@ basic_sources = files('''
mkdir-label.c
mkdir.c
mkdir.h
module-util.h
mount-util.c
mount-util.h
nss-util.h

View File

@ -59,65 +59,6 @@ static int parse_proc_cmdline_item(const char *key, const char *value, void *dat
return 0;
}
static int load_module(struct kmod_ctx *ctx, const char *m) {
const int probe_flags = KMOD_PROBE_APPLY_BLACKLIST;
struct kmod_list *itr;
_cleanup_(kmod_module_unref_listp) struct kmod_list *modlist = NULL;
int r = 0;
log_debug("load: %s", m);
r = kmod_module_new_from_lookup(ctx, m, &modlist);
if (r < 0)
return log_error_errno(r, "Failed to lookup alias '%s': %m", m);
if (!modlist) {
log_error("Failed to find module '%s'", m);
return -ENOENT;
}
kmod_list_foreach(itr, modlist) {
_cleanup_(kmod_module_unrefp) struct kmod_module *mod = NULL;
int state, err;
mod = kmod_module_get_module(itr);
state = kmod_module_get_initstate(mod);
switch (state) {
case KMOD_MODULE_BUILTIN:
log_info("Module '%s' is builtin", kmod_module_get_name(mod));
break;
case KMOD_MODULE_LIVE:
log_debug("Module '%s' is already loaded", kmod_module_get_name(mod));
break;
default:
err = kmod_module_probe_insert_module(mod, probe_flags,
NULL, NULL, NULL, NULL);
if (err == 0)
log_info("Inserted module '%s'", kmod_module_get_name(mod));
else if (err == KMOD_PROBE_APPLY_BLACKLIST)
log_info("Module '%s' is blacklisted", kmod_module_get_name(mod));
else {
assert(err < 0);
log_full_errno(err == ENODEV ? LOG_NOTICE :
err == ENOENT ? LOG_WARNING :
LOG_ERR,
err,
"Failed to insert '%s': %m",
kmod_module_get_name(mod));
if (!IN_SET(err, ENODEV, ENOENT))
r = err;
}
}
}
return r;
}
static int apply_file(struct kmod_ctx *ctx, const char *path, bool ignore_enoent) {
_cleanup_fclose_ FILE *f = NULL;
int r;
@ -151,7 +92,7 @@ static int apply_file(struct kmod_ctx *ctx, const char *path, bool ignore_enoent
if (strchr(COMMENTS "\n", *l))
continue;
k = load_module(ctx, l);
k = module_load_and_warn(ctx, l);
if (k < 0 && r == 0)
r = k;
}
@ -248,7 +189,7 @@ int main(int argc, char *argv[]) {
char **fn, **i;
STRV_FOREACH(i, arg_proc_cmdline_modules) {
k = load_module(ctx, *i);
k = module_load_and_warn(ctx, *i);
if (k < 0 && r == 0)
r = k;
}

View File

@ -63,6 +63,8 @@ shared_sources = files('''
machine-image.h
machine-pool.c
machine-pool.h
module-util.h
module-util.c
nsflags.c
nsflags.h
output-mode.c
@ -132,6 +134,7 @@ libshared_deps = [threads,
libcryptsetup,
libgcrypt,
libiptc,
libkmod,
libseccomp,
libselinux,
libidn,

64
src/shared/module-util.c Normal file
View File

@ -0,0 +1,64 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#include <errno.h>
#include "module-util.h"
int module_load_and_warn(struct kmod_ctx *ctx, const char *module) {
const int probe_flags = KMOD_PROBE_APPLY_BLACKLIST;
struct kmod_list *itr;
_cleanup_(kmod_module_unref_listp) struct kmod_list *modlist = NULL;
int r = 0;
log_debug("Loading module: %s", module);
r = kmod_module_new_from_lookup(ctx, module, &modlist);
if (r < 0)
return log_error_errno(r, "Failed to lookup module alias '%s': %m", module);
if (!modlist) {
log_error("Failed to find module '%s'", module);
return -ENOENT;
}
kmod_list_foreach(itr, modlist) {
_cleanup_(kmod_module_unrefp) struct kmod_module *mod = NULL;
int state, err;
mod = kmod_module_get_module(itr);
state = kmod_module_get_initstate(mod);
switch (state) {
case KMOD_MODULE_BUILTIN:
log_info("Module '%s' is builtin", kmod_module_get_name(mod));
break;
case KMOD_MODULE_LIVE:
log_debug("Module '%s' is already loaded", kmod_module_get_name(mod));
break;
default:
err = kmod_module_probe_insert_module(mod, probe_flags,
NULL, NULL, NULL, NULL);
if (err == 0)
log_info("Inserted module '%s'", kmod_module_get_name(mod));
else if (err == KMOD_PROBE_APPLY_BLACKLIST)
log_info("Module '%s' is blacklisted", kmod_module_get_name(mod));
else {
assert(err < 0);
log_full_errno(err == ENODEV ? LOG_NOTICE :
err == ENOENT ? LOG_WARNING :
LOG_ERR,
err,
"Failed to insert module '%s': %m",
kmod_module_get_name(mod));
if (!IN_SET(err, ENODEV, ENOENT))
r = err;
}
}
}
return r;
}

View File

@ -8,3 +8,5 @@
DEFINE_TRIVIAL_CLEANUP_FUNC(struct kmod_ctx*, kmod_unref);
DEFINE_TRIVIAL_CLEANUP_FUNC(struct kmod_module*, kmod_module_unref);
DEFINE_TRIVIAL_CLEANUP_FUNC(struct kmod_list*, kmod_module_unref_list);
int module_load_and_warn(struct kmod_ctx *ctx, const char *module);