2024-07-24 01:59:19 +07:00
|
|
|
// SPDX-License-Identifier: LGPL-2.1-or-later
|
2011-11-30 03:07:43 +07:00
|
|
|
/*
|
2013-01-16 20:27:21 +07:00
|
|
|
* Copyright (C) 2011-2013 ProFUSION embedded systems
|
2013-11-14 09:33:28 +07:00
|
|
|
* Copyright (C) 2013 Intel Corporation. All rights reserved.
|
2011-11-30 03:07:43 +07:00
|
|
|
*/
|
|
|
|
|
2014-10-03 11:41:42 +07:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stddef.h>
|
2011-11-30 03:07:43 +07:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-10-03 11:41:42 +07:00
|
|
|
#include <unistd.h>
|
2011-11-30 03:07:43 +07:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2014-10-03 10:01:35 +07:00
|
|
|
#include <shared/util.h>
|
|
|
|
|
2011-11-30 03:07:43 +07:00
|
|
|
#include "libkmod.h"
|
2013-07-05 02:13:11 +07:00
|
|
|
#include "libkmod-internal.h"
|
2011-11-30 03:07:43 +07:00
|
|
|
|
|
|
|
struct kmod_alias {
|
|
|
|
char *name;
|
2011-12-05 02:32:44 +07:00
|
|
|
char modname[];
|
2011-11-30 03:07:43 +07:00
|
|
|
};
|
|
|
|
|
2011-12-07 12:18:57 +07:00
|
|
|
struct kmod_options {
|
|
|
|
char *options;
|
|
|
|
char modname[];
|
|
|
|
};
|
|
|
|
|
2011-12-07 20:31:28 +07:00
|
|
|
struct kmod_command {
|
|
|
|
char *command;
|
|
|
|
char modname[];
|
|
|
|
};
|
|
|
|
|
2011-12-17 06:18:10 +07:00
|
|
|
struct kmod_softdep {
|
|
|
|
char *name;
|
|
|
|
const char **pre;
|
|
|
|
const char **post;
|
|
|
|
unsigned int n_pre;
|
|
|
|
unsigned int n_post;
|
|
|
|
};
|
|
|
|
|
libkmod: add weak dependecies
It has been seen that for some network mac drivers (i.e. lan78xx) the
related module for the phy is loaded dynamically depending on the current
hardware. In this case, the associated phy is read using mdio bus and then
the associated phy module is loaded during runtime (kernel function
phy_request_driver_module). However, no software dependency is defined, so
the user tools will no be able to get this dependency. For example, if
dracut is used and the hardware is present, lan78xx will be included but no
phy module will be added, and in the next restart the device will not work
from boot because no related phy will be found during initramfs stage.
In order to solve this, we could define a normal 'pre' software dependency
in lan78xx module with all the possible phy modules (there may be some),
but proceeding in that way, all the possible phy modules would be loaded
while only one is necessary.
The idea is to create a new type of dependency, that we are going to call
'weak' to be used only by the user tools that need to detect this situation.
In that way, for example, dracut could check the 'weak' dependency of the
modules involved in order to install these dependencies in initramfs too.
That is, for the commented lan78xx module, defining the 'weak' dependency
with the possible phy modules list, only the necessary phy would be loaded
on demand keeping the same behavior, but all the possible phy modules would
be available from initramfs.
A new function 'kmod_module_get_weakdeps' in libkmod will be added for
this to avoid breaking the API and maintain backward compatibility. This
general procedure could be useful for other similar cases (not only for
dynamic phy loading).
Signed-off-by: Jose Ignacio Tornos Martinez <jtornosm@redhat.com>
Link: https://lore.kernel.org/r/20240327141116.97587-1-jtornosm@redhat.com
2024-03-27 21:11:16 +07:00
|
|
|
struct kmod_weakdep {
|
|
|
|
char *name;
|
|
|
|
const char **weak;
|
|
|
|
unsigned int n_weak;
|
|
|
|
};
|
|
|
|
|
2011-12-24 19:50:47 +07:00
|
|
|
const char *kmod_blacklist_get_modname(const struct kmod_list *l)
|
|
|
|
{
|
|
|
|
return l->data;
|
|
|
|
}
|
|
|
|
|
2011-12-01 03:18:13 +07:00
|
|
|
const char *kmod_alias_get_name(const struct kmod_list *l) {
|
2011-12-03 05:24:07 +07:00
|
|
|
const struct kmod_alias *alias = l->data;
|
2011-12-01 03:18:13 +07:00
|
|
|
return alias->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *kmod_alias_get_modname(const struct kmod_list *l) {
|
2011-12-03 05:24:07 +07:00
|
|
|
const struct kmod_alias *alias = l->data;
|
2011-12-01 03:18:13 +07:00
|
|
|
return alias->modname;
|
|
|
|
}
|
|
|
|
|
2011-12-11 05:47:01 +07:00
|
|
|
const char *kmod_option_get_options(const struct kmod_list *l) {
|
|
|
|
const struct kmod_options *alias = l->data;
|
|
|
|
return alias->options;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *kmod_option_get_modname(const struct kmod_list *l) {
|
|
|
|
const struct kmod_options *alias = l->data;
|
|
|
|
return alias->modname;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *kmod_command_get_command(const struct kmod_list *l) {
|
|
|
|
const struct kmod_command *alias = l->data;
|
|
|
|
return alias->command;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *kmod_command_get_modname(const struct kmod_list *l) {
|
|
|
|
const struct kmod_command *alias = l->data;
|
|
|
|
return alias->modname;
|
|
|
|
}
|
|
|
|
|
2011-12-17 06:18:10 +07:00
|
|
|
const char *kmod_softdep_get_name(const struct kmod_list *l) {
|
|
|
|
const struct kmod_softdep *dep = l->data;
|
|
|
|
return dep->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char * const *kmod_softdep_get_pre(const struct kmod_list *l, unsigned int *count) {
|
|
|
|
const struct kmod_softdep *dep = l->data;
|
|
|
|
*count = dep->n_pre;
|
|
|
|
return dep->pre;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char * const *kmod_softdep_get_post(const struct kmod_list *l, unsigned int *count) {
|
|
|
|
const struct kmod_softdep *dep = l->data;
|
|
|
|
*count = dep->n_post;
|
|
|
|
return dep->post;
|
|
|
|
}
|
|
|
|
|
libkmod: add weak dependecies
It has been seen that for some network mac drivers (i.e. lan78xx) the
related module for the phy is loaded dynamically depending on the current
hardware. In this case, the associated phy is read using mdio bus and then
the associated phy module is loaded during runtime (kernel function
phy_request_driver_module). However, no software dependency is defined, so
the user tools will no be able to get this dependency. For example, if
dracut is used and the hardware is present, lan78xx will be included but no
phy module will be added, and in the next restart the device will not work
from boot because no related phy will be found during initramfs stage.
In order to solve this, we could define a normal 'pre' software dependency
in lan78xx module with all the possible phy modules (there may be some),
but proceeding in that way, all the possible phy modules would be loaded
while only one is necessary.
The idea is to create a new type of dependency, that we are going to call
'weak' to be used only by the user tools that need to detect this situation.
In that way, for example, dracut could check the 'weak' dependency of the
modules involved in order to install these dependencies in initramfs too.
That is, for the commented lan78xx module, defining the 'weak' dependency
with the possible phy modules list, only the necessary phy would be loaded
on demand keeping the same behavior, but all the possible phy modules would
be available from initramfs.
A new function 'kmod_module_get_weakdeps' in libkmod will be added for
this to avoid breaking the API and maintain backward compatibility. This
general procedure could be useful for other similar cases (not only for
dynamic phy loading).
Signed-off-by: Jose Ignacio Tornos Martinez <jtornosm@redhat.com>
Link: https://lore.kernel.org/r/20240327141116.97587-1-jtornosm@redhat.com
2024-03-27 21:11:16 +07:00
|
|
|
const char *kmod_weakdep_get_name(const struct kmod_list *l) {
|
|
|
|
const struct kmod_weakdep *dep = l->data;
|
|
|
|
return dep->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char * const *kmod_weakdep_get_weak(const struct kmod_list *l, unsigned int *count) {
|
|
|
|
const struct kmod_weakdep *dep = l->data;
|
|
|
|
*count = dep->n_weak;
|
|
|
|
return dep->weak;
|
|
|
|
}
|
2011-12-07 20:31:28 +07:00
|
|
|
static int kmod_config_add_command(struct kmod_config *config,
|
|
|
|
const char *modname,
|
|
|
|
const char *command,
|
|
|
|
const char *command_name,
|
|
|
|
struct kmod_list **list)
|
|
|
|
{
|
2013-11-14 09:33:28 +07:00
|
|
|
_cleanup_free_ struct kmod_command *cmd;
|
2011-12-07 20:31:28 +07:00
|
|
|
struct kmod_list *l;
|
|
|
|
size_t modnamelen = strlen(modname) + 1;
|
|
|
|
size_t commandlen = strlen(command) + 1;
|
|
|
|
|
2011-12-19 23:36:23 +07:00
|
|
|
DBG(config->ctx, "modname='%s' cmd='%s %s'\n", modname, command_name,
|
2011-12-07 20:31:28 +07:00
|
|
|
command);
|
|
|
|
|
|
|
|
cmd = malloc(sizeof(*cmd) + modnamelen + commandlen);
|
2013-11-14 09:33:28 +07:00
|
|
|
if (!cmd)
|
|
|
|
return -ENOMEM;
|
2011-12-07 20:31:28 +07:00
|
|
|
|
|
|
|
cmd->command = sizeof(*cmd) + modnamelen + (char *)cmd;
|
|
|
|
memcpy(cmd->modname, modname, modnamelen);
|
|
|
|
memcpy(cmd->command, command, commandlen);
|
|
|
|
|
|
|
|
l = kmod_list_append(*list, cmd);
|
2013-11-14 09:33:28 +07:00
|
|
|
if (!l)
|
|
|
|
return -ENOMEM;
|
2011-12-07 20:31:28 +07:00
|
|
|
|
|
|
|
*list = l;
|
2013-11-14 09:33:28 +07:00
|
|
|
cmd = NULL;
|
2011-12-07 20:31:28 +07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void kmod_config_free_command(struct kmod_config *config,
|
|
|
|
struct kmod_list *l,
|
|
|
|
struct kmod_list **list)
|
|
|
|
{
|
|
|
|
struct kmod_command *cmd = l->data;
|
|
|
|
|
|
|
|
free(cmd);
|
|
|
|
*list = kmod_list_remove(l);
|
|
|
|
}
|
|
|
|
|
2011-12-07 12:18:57 +07:00
|
|
|
static int kmod_config_add_options(struct kmod_config *config,
|
|
|
|
const char *modname, const char *options)
|
|
|
|
{
|
2013-11-14 09:33:28 +07:00
|
|
|
_cleanup_free_ struct kmod_options *opt;
|
2011-12-07 12:18:57 +07:00
|
|
|
struct kmod_list *list;
|
|
|
|
size_t modnamelen = strlen(modname) + 1;
|
|
|
|
size_t optionslen = strlen(options) + 1;
|
|
|
|
|
2011-12-15 02:21:46 +07:00
|
|
|
DBG(config->ctx, "modname='%s' options='%s'\n", modname, options);
|
2011-12-07 12:18:57 +07:00
|
|
|
|
|
|
|
opt = malloc(sizeof(*opt) + modnamelen + optionslen);
|
2013-11-14 09:33:28 +07:00
|
|
|
if (!opt)
|
|
|
|
return -ENOMEM;
|
2011-12-07 12:18:57 +07:00
|
|
|
|
|
|
|
opt->options = sizeof(*opt) + modnamelen + (char *)opt;
|
|
|
|
|
|
|
|
memcpy(opt->modname, modname, modnamelen);
|
|
|
|
memcpy(opt->options, options, optionslen);
|
|
|
|
strchr_replace(opt->options, '\t', ' ');
|
|
|
|
|
|
|
|
list = kmod_list_append(config->options, opt);
|
2013-11-14 09:33:28 +07:00
|
|
|
if (!list)
|
|
|
|
return -ENOMEM;
|
2011-12-07 12:18:57 +07:00
|
|
|
|
2013-11-14 09:33:28 +07:00
|
|
|
opt = NULL;
|
2011-12-07 12:18:57 +07:00
|
|
|
config->options = list;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-12-12 19:48:02 +07:00
|
|
|
static void kmod_config_free_options(struct kmod_config *config,
|
|
|
|
struct kmod_list *l)
|
2011-12-07 12:18:57 +07:00
|
|
|
{
|
|
|
|
struct kmod_options *opt = l->data;
|
|
|
|
|
|
|
|
free(opt);
|
|
|
|
|
|
|
|
config->options = kmod_list_remove(l);
|
|
|
|
}
|
|
|
|
|
2011-12-03 06:40:22 +07:00
|
|
|
static int kmod_config_add_alias(struct kmod_config *config,
|
2011-12-12 19:48:02 +07:00
|
|
|
const char *name, const char *modname)
|
2011-11-30 03:07:43 +07:00
|
|
|
{
|
2013-11-14 09:33:28 +07:00
|
|
|
_cleanup_free_ struct kmod_alias *alias;
|
2011-12-03 06:40:22 +07:00
|
|
|
struct kmod_list *list;
|
2011-12-05 02:32:44 +07:00
|
|
|
size_t namelen = strlen(name) + 1, modnamelen = strlen(modname) + 1;
|
2011-11-30 03:07:43 +07:00
|
|
|
|
2011-12-03 06:40:22 +07:00
|
|
|
DBG(config->ctx, "name=%s modname=%s\n", name, modname);
|
2011-11-30 03:07:43 +07:00
|
|
|
|
2011-12-05 02:32:44 +07:00
|
|
|
alias = malloc(sizeof(*alias) + namelen + modnamelen);
|
2011-12-03 06:40:22 +07:00
|
|
|
if (!alias)
|
2013-11-14 09:33:28 +07:00
|
|
|
return -ENOMEM;
|
2011-12-12 20:52:59 +07:00
|
|
|
|
2011-12-05 02:32:44 +07:00
|
|
|
alias->name = sizeof(*alias) + modnamelen + (char *)alias;
|
|
|
|
|
|
|
|
memcpy(alias->modname, modname, modnamelen);
|
|
|
|
memcpy(alias->name, name, namelen);
|
2011-11-30 03:07:43 +07:00
|
|
|
|
2011-12-03 06:40:22 +07:00
|
|
|
list = kmod_list_append(config->aliases, alias);
|
|
|
|
if (!list)
|
2013-11-14 09:33:28 +07:00
|
|
|
return -ENOMEM;
|
2011-12-12 20:52:59 +07:00
|
|
|
|
2013-11-14 09:33:28 +07:00
|
|
|
alias = NULL;
|
2011-12-03 06:40:22 +07:00
|
|
|
config->aliases = list;
|
|
|
|
return 0;
|
2011-11-30 03:07:43 +07:00
|
|
|
}
|
|
|
|
|
2011-12-12 19:48:02 +07:00
|
|
|
static void kmod_config_free_alias(struct kmod_config *config,
|
|
|
|
struct kmod_list *l)
|
2011-11-30 03:07:43 +07:00
|
|
|
{
|
|
|
|
struct kmod_alias *alias = l->data;
|
|
|
|
|
|
|
|
free(alias);
|
|
|
|
|
2011-12-03 06:40:22 +07:00
|
|
|
config->aliases = kmod_list_remove(l);
|
2011-11-30 03:07:43 +07:00
|
|
|
}
|
|
|
|
|
2011-12-03 06:40:22 +07:00
|
|
|
static int kmod_config_add_blacklist(struct kmod_config *config,
|
2011-12-12 19:48:02 +07:00
|
|
|
const char *modname)
|
2011-11-30 03:48:02 +07:00
|
|
|
{
|
2013-11-14 09:33:28 +07:00
|
|
|
_cleanup_free_ char *p;
|
2011-12-03 06:40:22 +07:00
|
|
|
struct kmod_list *list;
|
2011-11-30 03:48:02 +07:00
|
|
|
|
2011-12-03 06:40:22 +07:00
|
|
|
DBG(config->ctx, "modname=%s\n", modname);
|
2011-11-30 03:48:02 +07:00
|
|
|
|
|
|
|
p = strdup(modname);
|
2011-12-03 06:40:22 +07:00
|
|
|
if (!p)
|
2013-11-14 09:33:28 +07:00
|
|
|
return -ENOMEM;
|
2011-12-03 06:40:22 +07:00
|
|
|
|
|
|
|
list = kmod_list_append(config->blacklists, p);
|
|
|
|
if (!list)
|
2013-11-14 09:33:28 +07:00
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
p = NULL;
|
2011-12-03 06:40:22 +07:00
|
|
|
config->blacklists = list;
|
|
|
|
return 0;
|
2011-11-30 03:48:02 +07:00
|
|
|
}
|
|
|
|
|
2011-12-03 06:40:22 +07:00
|
|
|
static void kmod_config_free_blacklist(struct kmod_config *config,
|
2011-11-30 03:48:02 +07:00
|
|
|
struct kmod_list *l)
|
|
|
|
{
|
|
|
|
free(l->data);
|
2011-12-03 06:40:22 +07:00
|
|
|
config->blacklists = kmod_list_remove(l);
|
2011-11-30 03:48:02 +07:00
|
|
|
}
|
|
|
|
|
2011-12-17 06:18:10 +07:00
|
|
|
static int kmod_config_add_softdep(struct kmod_config *config,
|
|
|
|
const char *modname,
|
|
|
|
const char *line)
|
|
|
|
{
|
|
|
|
struct kmod_list *list;
|
|
|
|
struct kmod_softdep *dep;
|
|
|
|
const char *s, *p;
|
|
|
|
char *itr;
|
|
|
|
unsigned int n_pre = 0, n_post = 0;
|
|
|
|
size_t modnamelen = strlen(modname) + 1;
|
|
|
|
size_t buflen = 0;
|
|
|
|
bool was_space = false;
|
|
|
|
enum { S_NONE, S_PRE, S_POST } mode = S_NONE;
|
|
|
|
|
|
|
|
DBG(config->ctx, "modname=%s\n", modname);
|
|
|
|
|
|
|
|
/* analyze and count */
|
|
|
|
for (p = s = line; ; s++) {
|
|
|
|
size_t plen;
|
|
|
|
|
|
|
|
if (*s != '\0') {
|
|
|
|
if (!isspace(*s)) {
|
|
|
|
was_space = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (was_space) {
|
|
|
|
p = s + 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
was_space = true;
|
|
|
|
|
|
|
|
if (p >= s)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
plen = s - p;
|
|
|
|
|
|
|
|
if (plen == sizeof("pre:") - 1 &&
|
|
|
|
memcmp(p, "pre:", sizeof("pre:") - 1) == 0)
|
|
|
|
mode = S_PRE;
|
|
|
|
else if (plen == sizeof("post:") - 1 &&
|
|
|
|
memcmp(p, "post:", sizeof("post:") - 1) == 0)
|
|
|
|
mode = S_POST;
|
|
|
|
else if (*s != '\0' || (*s == '\0' && !was_space)) {
|
|
|
|
if (mode == S_PRE) {
|
|
|
|
buflen += plen + 1;
|
|
|
|
n_pre++;
|
|
|
|
} else if (mode == S_POST) {
|
|
|
|
buflen += plen + 1;
|
|
|
|
n_post++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p = s + 1;
|
|
|
|
if (*s == '\0')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
DBG(config->ctx, "%u pre, %u post\n", n_pre, n_post);
|
|
|
|
|
|
|
|
dep = malloc(sizeof(struct kmod_softdep) + modnamelen +
|
|
|
|
n_pre * sizeof(const char *) +
|
|
|
|
n_post * sizeof(const char *) +
|
|
|
|
buflen);
|
|
|
|
if (dep == NULL) {
|
|
|
|
ERR(config->ctx, "out-of-memory modname=%s\n", modname);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
dep->n_pre = n_pre;
|
|
|
|
dep->n_post = n_post;
|
|
|
|
dep->pre = (const char **)((char *)dep + sizeof(struct kmod_softdep));
|
|
|
|
dep->post = dep->pre + n_pre;
|
|
|
|
dep->name = (char *)(dep->post + n_post);
|
|
|
|
|
|
|
|
memcpy(dep->name, modname, modnamelen);
|
|
|
|
|
|
|
|
/* copy strings */
|
|
|
|
itr = dep->name + modnamelen;
|
|
|
|
n_pre = 0;
|
|
|
|
n_post = 0;
|
|
|
|
mode = S_NONE;
|
2020-01-21 02:15:38 +07:00
|
|
|
was_space = false;
|
2011-12-17 06:18:10 +07:00
|
|
|
for (p = s = line; ; s++) {
|
|
|
|
size_t plen;
|
|
|
|
|
|
|
|
if (*s != '\0') {
|
|
|
|
if (!isspace(*s)) {
|
|
|
|
was_space = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (was_space) {
|
|
|
|
p = s + 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
was_space = true;
|
|
|
|
|
|
|
|
if (p >= s)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
plen = s - p;
|
|
|
|
|
|
|
|
if (plen == sizeof("pre:") - 1 &&
|
|
|
|
memcmp(p, "pre:", sizeof("pre:") - 1) == 0)
|
|
|
|
mode = S_PRE;
|
|
|
|
else if (plen == sizeof("post:") - 1 &&
|
|
|
|
memcmp(p, "post:", sizeof("post:") - 1) == 0)
|
|
|
|
mode = S_POST;
|
|
|
|
else if (*s != '\0' || (*s == '\0' && !was_space)) {
|
|
|
|
if (mode == S_PRE) {
|
|
|
|
dep->pre[n_pre] = itr;
|
|
|
|
memcpy(itr, p, plen);
|
|
|
|
itr[plen] = '\0';
|
|
|
|
itr += plen + 1;
|
|
|
|
n_pre++;
|
|
|
|
} else if (mode == S_POST) {
|
|
|
|
dep->post[n_post] = itr;
|
|
|
|
memcpy(itr, p, plen);
|
|
|
|
itr[plen] = '\0';
|
|
|
|
itr += plen + 1;
|
|
|
|
n_post++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p = s + 1;
|
|
|
|
if (*s == '\0')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
list = kmod_list_append(config->softdeps, dep);
|
|
|
|
if (list == NULL) {
|
|
|
|
free(dep);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
config->softdeps = list;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
libkmod: add weak dependecies
It has been seen that for some network mac drivers (i.e. lan78xx) the
related module for the phy is loaded dynamically depending on the current
hardware. In this case, the associated phy is read using mdio bus and then
the associated phy module is loaded during runtime (kernel function
phy_request_driver_module). However, no software dependency is defined, so
the user tools will no be able to get this dependency. For example, if
dracut is used and the hardware is present, lan78xx will be included but no
phy module will be added, and in the next restart the device will not work
from boot because no related phy will be found during initramfs stage.
In order to solve this, we could define a normal 'pre' software dependency
in lan78xx module with all the possible phy modules (there may be some),
but proceeding in that way, all the possible phy modules would be loaded
while only one is necessary.
The idea is to create a new type of dependency, that we are going to call
'weak' to be used only by the user tools that need to detect this situation.
In that way, for example, dracut could check the 'weak' dependency of the
modules involved in order to install these dependencies in initramfs too.
That is, for the commented lan78xx module, defining the 'weak' dependency
with the possible phy modules list, only the necessary phy would be loaded
on demand keeping the same behavior, but all the possible phy modules would
be available from initramfs.
A new function 'kmod_module_get_weakdeps' in libkmod will be added for
this to avoid breaking the API and maintain backward compatibility. This
general procedure could be useful for other similar cases (not only for
dynamic phy loading).
Signed-off-by: Jose Ignacio Tornos Martinez <jtornosm@redhat.com>
Link: https://lore.kernel.org/r/20240327141116.97587-1-jtornosm@redhat.com
2024-03-27 21:11:16 +07:00
|
|
|
static int kmod_config_add_weakdep(struct kmod_config *config,
|
|
|
|
const char *modname,
|
|
|
|
const char *line)
|
|
|
|
{
|
|
|
|
struct kmod_list *list;
|
|
|
|
struct kmod_weakdep *dep;
|
|
|
|
const char *s, *p;
|
|
|
|
char *itr;
|
|
|
|
unsigned int n_weak = 0;
|
|
|
|
size_t modnamelen = strlen(modname) + 1;
|
|
|
|
size_t buflen = 0;
|
|
|
|
bool was_space = false;
|
|
|
|
|
|
|
|
DBG(config->ctx, "modname=%s\n", modname);
|
|
|
|
|
|
|
|
/* analyze and count */
|
|
|
|
for (p = s = line; ; s++) {
|
|
|
|
size_t plen;
|
|
|
|
|
|
|
|
if (*s != '\0') {
|
|
|
|
if (!isspace(*s)) {
|
|
|
|
was_space = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (was_space) {
|
|
|
|
p = s + 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
was_space = true;
|
|
|
|
|
|
|
|
if (p >= s)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
plen = s - p;
|
|
|
|
|
|
|
|
if (*s != '\0' || (*s == '\0' && !was_space)) {
|
|
|
|
buflen += plen + 1;
|
|
|
|
n_weak++;
|
|
|
|
}
|
|
|
|
p = s + 1;
|
|
|
|
if (*s == '\0')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
DBG(config->ctx, "%u weak\n", n_weak);
|
|
|
|
|
|
|
|
dep = malloc(sizeof(struct kmod_weakdep) + modnamelen +
|
|
|
|
n_weak * sizeof(const char *) +
|
|
|
|
buflen);
|
|
|
|
if (dep == NULL) {
|
|
|
|
ERR(config->ctx, "out-of-memory modname=%s\n", modname);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
dep->n_weak = n_weak;
|
|
|
|
dep->weak = (const char **)((char *)dep + sizeof(struct kmod_weakdep));
|
|
|
|
dep->name = (char *)(dep->weak + n_weak);
|
|
|
|
|
|
|
|
memcpy(dep->name, modname, modnamelen);
|
|
|
|
|
|
|
|
/* copy strings */
|
|
|
|
itr = dep->name + modnamelen;
|
|
|
|
n_weak = 0;
|
|
|
|
was_space = false;
|
|
|
|
for (p = s = line; ; s++) {
|
|
|
|
size_t plen;
|
|
|
|
|
|
|
|
if (*s != '\0') {
|
|
|
|
if (!isspace(*s)) {
|
|
|
|
was_space = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (was_space) {
|
|
|
|
p = s + 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
was_space = true;
|
|
|
|
|
|
|
|
if (p >= s)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
plen = s - p;
|
|
|
|
|
|
|
|
if (*s != '\0' || (*s == '\0' && !was_space)) {
|
|
|
|
dep->weak[n_weak] = itr;
|
|
|
|
memcpy(itr, p, plen);
|
|
|
|
itr[plen] = '\0';
|
|
|
|
itr += plen + 1;
|
|
|
|
n_weak++;
|
|
|
|
}
|
|
|
|
p = s + 1;
|
|
|
|
if (*s == '\0')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
list = kmod_list_append(config->weakdeps, dep);
|
|
|
|
if (list == NULL) {
|
|
|
|
free(dep);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
config->weakdeps = list;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-13 19:49:31 +07:00
|
|
|
static char *softdep_to_char(struct kmod_softdep *dep) {
|
|
|
|
const size_t sz_preprefix = sizeof("pre: ") - 1;
|
|
|
|
const size_t sz_postprefix = sizeof("post: ") - 1;
|
|
|
|
size_t sz = 1; /* at least '\0' */
|
|
|
|
size_t sz_pre, sz_post;
|
|
|
|
const char *start, *end;
|
|
|
|
char *s, *itr;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Rely on the fact that dep->pre[] and dep->post[] are strv's that
|
|
|
|
* point to a contiguous buffer
|
|
|
|
*/
|
|
|
|
if (dep->n_pre > 0) {
|
|
|
|
start = dep->pre[0];
|
|
|
|
end = dep->pre[dep->n_pre - 1]
|
|
|
|
+ strlen(dep->pre[dep->n_pre - 1]);
|
|
|
|
sz_pre = end - start;
|
|
|
|
sz += sz_pre + sz_preprefix;
|
|
|
|
} else
|
|
|
|
sz_pre = 0;
|
|
|
|
|
|
|
|
if (dep->n_post > 0) {
|
|
|
|
start = dep->post[0];
|
|
|
|
end = dep->post[dep->n_post - 1]
|
|
|
|
+ strlen(dep->post[dep->n_post - 1]);
|
|
|
|
sz_post = end - start;
|
|
|
|
sz += sz_post + sz_postprefix;
|
|
|
|
} else
|
|
|
|
sz_post = 0;
|
|
|
|
|
|
|
|
itr = s = malloc(sz);
|
|
|
|
if (s == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (sz_pre) {
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
memcpy(itr, "pre: ", sz_preprefix);
|
|
|
|
itr += sz_preprefix;
|
|
|
|
|
|
|
|
/* include last '\0' */
|
|
|
|
memcpy(itr, dep->pre[0], sz_pre + 1);
|
|
|
|
for (p = itr; p < itr + sz_pre; p++) {
|
|
|
|
if (*p == '\0')
|
|
|
|
*p = ' ';
|
|
|
|
}
|
|
|
|
itr = p;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sz_post) {
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
memcpy(itr, "post: ", sz_postprefix);
|
|
|
|
itr += sz_postprefix;
|
|
|
|
|
|
|
|
/* include last '\0' */
|
|
|
|
memcpy(itr, dep->post[0], sz_post + 1);
|
|
|
|
for (p = itr; p < itr + sz_post; p++) {
|
|
|
|
if (*p == '\0')
|
|
|
|
*p = ' ';
|
|
|
|
}
|
|
|
|
itr = p;
|
|
|
|
}
|
|
|
|
|
|
|
|
*itr = '\0';
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
libkmod: add weak dependecies
It has been seen that for some network mac drivers (i.e. lan78xx) the
related module for the phy is loaded dynamically depending on the current
hardware. In this case, the associated phy is read using mdio bus and then
the associated phy module is loaded during runtime (kernel function
phy_request_driver_module). However, no software dependency is defined, so
the user tools will no be able to get this dependency. For example, if
dracut is used and the hardware is present, lan78xx will be included but no
phy module will be added, and in the next restart the device will not work
from boot because no related phy will be found during initramfs stage.
In order to solve this, we could define a normal 'pre' software dependency
in lan78xx module with all the possible phy modules (there may be some),
but proceeding in that way, all the possible phy modules would be loaded
while only one is necessary.
The idea is to create a new type of dependency, that we are going to call
'weak' to be used only by the user tools that need to detect this situation.
In that way, for example, dracut could check the 'weak' dependency of the
modules involved in order to install these dependencies in initramfs too.
That is, for the commented lan78xx module, defining the 'weak' dependency
with the possible phy modules list, only the necessary phy would be loaded
on demand keeping the same behavior, but all the possible phy modules would
be available from initramfs.
A new function 'kmod_module_get_weakdeps' in libkmod will be added for
this to avoid breaking the API and maintain backward compatibility. This
general procedure could be useful for other similar cases (not only for
dynamic phy loading).
Signed-off-by: Jose Ignacio Tornos Martinez <jtornosm@redhat.com>
Link: https://lore.kernel.org/r/20240327141116.97587-1-jtornosm@redhat.com
2024-03-27 21:11:16 +07:00
|
|
|
static char *weakdep_to_char(struct kmod_weakdep *dep) {
|
|
|
|
size_t sz;
|
|
|
|
const char *start, *end;
|
|
|
|
char *s, *itr;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Rely on the fact that dep->weak[] and are strv's that point to a
|
|
|
|
* contiguous buffer
|
|
|
|
*/
|
|
|
|
if (dep->n_weak > 0) {
|
|
|
|
start = dep->weak[0];
|
|
|
|
end = dep->weak[dep->n_weak - 1]
|
|
|
|
+ strlen(dep->weak[dep->n_weak - 1]);
|
|
|
|
sz = end - start;
|
|
|
|
} else
|
|
|
|
sz = 0;
|
|
|
|
|
|
|
|
itr = s = malloc(sz);
|
|
|
|
if (s == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (sz) {
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
/* include last '\0' */
|
|
|
|
memcpy(itr, dep->weak[0], sz + 1);
|
|
|
|
for (p = itr; p < itr + sz; p++) {
|
|
|
|
if (*p == '\0')
|
|
|
|
*p = ' ';
|
|
|
|
}
|
|
|
|
itr = p;
|
|
|
|
}
|
|
|
|
|
|
|
|
*itr = '\0';
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2011-12-17 06:18:10 +07:00
|
|
|
static void kmod_config_free_softdep(struct kmod_config *config,
|
|
|
|
struct kmod_list *l)
|
|
|
|
{
|
|
|
|
free(l->data);
|
|
|
|
config->softdeps = kmod_list_remove(l);
|
|
|
|
}
|
|
|
|
|
libkmod: add weak dependecies
It has been seen that for some network mac drivers (i.e. lan78xx) the
related module for the phy is loaded dynamically depending on the current
hardware. In this case, the associated phy is read using mdio bus and then
the associated phy module is loaded during runtime (kernel function
phy_request_driver_module). However, no software dependency is defined, so
the user tools will no be able to get this dependency. For example, if
dracut is used and the hardware is present, lan78xx will be included but no
phy module will be added, and in the next restart the device will not work
from boot because no related phy will be found during initramfs stage.
In order to solve this, we could define a normal 'pre' software dependency
in lan78xx module with all the possible phy modules (there may be some),
but proceeding in that way, all the possible phy modules would be loaded
while only one is necessary.
The idea is to create a new type of dependency, that we are going to call
'weak' to be used only by the user tools that need to detect this situation.
In that way, for example, dracut could check the 'weak' dependency of the
modules involved in order to install these dependencies in initramfs too.
That is, for the commented lan78xx module, defining the 'weak' dependency
with the possible phy modules list, only the necessary phy would be loaded
on demand keeping the same behavior, but all the possible phy modules would
be available from initramfs.
A new function 'kmod_module_get_weakdeps' in libkmod will be added for
this to avoid breaking the API and maintain backward compatibility. This
general procedure could be useful for other similar cases (not only for
dynamic phy loading).
Signed-off-by: Jose Ignacio Tornos Martinez <jtornosm@redhat.com>
Link: https://lore.kernel.org/r/20240327141116.97587-1-jtornosm@redhat.com
2024-03-27 21:11:16 +07:00
|
|
|
static void kmod_config_free_weakdep(struct kmod_config *config,
|
|
|
|
struct kmod_list *l)
|
|
|
|
{
|
|
|
|
free(l->data);
|
|
|
|
config->weakdeps = kmod_list_remove(l);
|
|
|
|
}
|
|
|
|
|
2011-12-15 02:19:19 +07:00
|
|
|
static void kcmdline_parse_result(struct kmod_config *config, char *modname,
|
|
|
|
char *param, char *value)
|
|
|
|
{
|
2013-08-14 08:04:46 +07:00
|
|
|
if (modname == NULL || param == NULL)
|
2011-12-15 02:19:19 +07:00
|
|
|
return;
|
|
|
|
|
|
|
|
DBG(config->ctx, "%s %s\n", modname, param);
|
|
|
|
|
|
|
|
if (streq(modname, "modprobe") && !strncmp(param, "blacklist=", 10)) {
|
|
|
|
for (;;) {
|
|
|
|
char *t = strsep(&value, ",");
|
|
|
|
if (t == NULL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
kmod_config_add_blacklist(config, t);
|
|
|
|
}
|
|
|
|
} else {
|
2014-10-09 20:59:08 +07:00
|
|
|
if (underscores(modname) < 0) {
|
|
|
|
ERR(config->ctx, "Ignoring bad option on kernel command line while parsing module name: '%s'\n",
|
|
|
|
modname);
|
2020-03-10 15:06:39 +07:00
|
|
|
} else {
|
|
|
|
kmod_config_add_options(config, modname, param);
|
2014-10-09 20:59:08 +07:00
|
|
|
}
|
2011-12-15 02:19:19 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int kmod_config_parse_kcmdline(struct kmod_config *config)
|
|
|
|
{
|
|
|
|
char buf[KCMD_LINE_SIZE];
|
|
|
|
int fd, err;
|
2021-02-12 16:45:22 +07:00
|
|
|
char *p, *p_quote_start, *modname, *param = NULL, *value = NULL;
|
2021-02-12 16:45:21 +07:00
|
|
|
bool is_quoted = false, iter = true;
|
|
|
|
enum state {
|
|
|
|
STATE_IGNORE,
|
|
|
|
STATE_MODNAME,
|
|
|
|
STATE_PARAM,
|
|
|
|
STATE_VALUE,
|
|
|
|
STATE_COMPLETE,
|
|
|
|
} state;
|
2011-12-15 02:19:19 +07:00
|
|
|
|
2011-12-16 12:49:54 +07:00
|
|
|
fd = open("/proc/cmdline", O_RDONLY|O_CLOEXEC);
|
2012-01-07 04:22:41 +07:00
|
|
|
if (fd < 0) {
|
|
|
|
err = -errno;
|
|
|
|
DBG(config->ctx, "could not open '/proc/cmdline' for reading: %m\n");
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2011-12-15 02:19:19 +07:00
|
|
|
err = read_str_safe(fd, buf, sizeof(buf));
|
|
|
|
close(fd);
|
|
|
|
if (err < 0) {
|
|
|
|
ERR(config->ctx, "could not read from '/proc/cmdline': %s\n",
|
|
|
|
strerror(-err));
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2021-02-12 16:45:21 +07:00
|
|
|
state = STATE_MODNAME;
|
2021-02-12 16:45:22 +07:00
|
|
|
p_quote_start = NULL;
|
2021-02-12 16:45:21 +07:00
|
|
|
for (p = buf, modname = buf; iter; p++) {
|
|
|
|
switch (*p) {
|
|
|
|
case '"':
|
2017-01-21 06:15:50 +07:00
|
|
|
is_quoted = !is_quoted;
|
2021-02-12 16:45:22 +07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* only allow starting quote as first char when looking
|
|
|
|
* for a modname: anything else is considered ill-formed
|
|
|
|
*/
|
|
|
|
if (is_quoted && state == STATE_MODNAME && p == modname) {
|
|
|
|
p_quote_start = p;
|
|
|
|
modname = p + 1;
|
|
|
|
} else if (state != STATE_VALUE) {
|
|
|
|
state = STATE_IGNORE;
|
|
|
|
}
|
|
|
|
|
2021-02-12 16:45:21 +07:00
|
|
|
break;
|
|
|
|
case '\0':
|
|
|
|
iter = false;
|
|
|
|
/* fall-through */
|
|
|
|
case ' ':
|
2021-03-10 23:33:01 +07:00
|
|
|
case '\n':
|
|
|
|
case '\t':
|
|
|
|
case '\v':
|
|
|
|
case '\f':
|
|
|
|
case '\r':
|
2021-02-12 16:45:21 +07:00
|
|
|
if (is_quoted && state == STATE_VALUE) {
|
|
|
|
/* no state change*/;
|
|
|
|
} else if (is_quoted) {
|
|
|
|
/* spaces are only allowed in the value part */
|
|
|
|
state = STATE_IGNORE;
|
|
|
|
} else if (state == STATE_VALUE || state == STATE_PARAM) {
|
|
|
|
*p = '\0';
|
|
|
|
state = STATE_COMPLETE;
|
|
|
|
} else {
|
2017-02-17 00:18:35 +07:00
|
|
|
/*
|
2021-02-12 16:45:21 +07:00
|
|
|
* go to next option, ignoring any possible
|
|
|
|
* partial match we have
|
2017-02-17 00:18:35 +07:00
|
|
|
*/
|
2021-02-12 16:45:21 +07:00
|
|
|
modname = p + 1;
|
|
|
|
state = STATE_MODNAME;
|
2021-02-12 16:45:22 +07:00
|
|
|
p_quote_start = NULL;
|
2017-02-17 00:18:35 +07:00
|
|
|
}
|
2011-12-15 02:19:19 +07:00
|
|
|
break;
|
|
|
|
case '.':
|
2021-02-12 16:45:21 +07:00
|
|
|
if (state == STATE_MODNAME) {
|
2012-10-09 05:04:16 +07:00
|
|
|
*p = '\0';
|
|
|
|
param = p + 1;
|
2021-02-12 16:45:21 +07:00
|
|
|
state = STATE_PARAM;
|
|
|
|
} else if (state == STATE_PARAM) {
|
|
|
|
state = STATE_IGNORE;
|
2012-10-09 05:04:16 +07:00
|
|
|
}
|
2011-12-15 02:19:19 +07:00
|
|
|
break;
|
|
|
|
case '=':
|
2021-02-12 16:45:21 +07:00
|
|
|
if (state == STATE_PARAM) {
|
|
|
|
/*
|
|
|
|
* Don't set *p to '\0': the value var shadows
|
|
|
|
* param
|
|
|
|
*/
|
2011-12-20 22:25:24 +07:00
|
|
|
value = p + 1;
|
2021-02-12 16:45:21 +07:00
|
|
|
state = STATE_VALUE;
|
|
|
|
} else if (state == STATE_MODNAME) {
|
|
|
|
state = STATE_IGNORE;
|
|
|
|
}
|
2011-12-15 02:19:19 +07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-02-12 16:45:21 +07:00
|
|
|
if (state == STATE_COMPLETE) {
|
2021-02-12 16:45:22 +07:00
|
|
|
/*
|
|
|
|
* We may need to re-quote to unmangle what the
|
|
|
|
* bootloader passed. Example: grub passes the option as
|
|
|
|
* "parport.dyndbg=file drivers/parport/ieee1284_ops.c +mpf"
|
|
|
|
* instead of
|
|
|
|
* parport.dyndbg="file drivers/parport/ieee1284_ops.c +mpf"
|
|
|
|
*/
|
|
|
|
if (p_quote_start && p_quote_start < modname) {
|
|
|
|
/*
|
|
|
|
* p_quote_start
|
|
|
|
* |
|
|
|
|
* |modname param value
|
|
|
|
* || | |
|
|
|
|
* vv v v
|
|
|
|
* "parport\0dyndbg=file drivers/parport/ieee1284_ops.c +mpf" */
|
|
|
|
memmove(p_quote_start, modname, value - modname);
|
|
|
|
value--; modname--; param--;
|
|
|
|
*value = '"';
|
|
|
|
}
|
2021-02-12 16:45:21 +07:00
|
|
|
kcmdline_parse_result(config, modname, param, value);
|
|
|
|
/* start over on next iteration */
|
|
|
|
modname = p + 1;
|
|
|
|
state = STATE_MODNAME;
|
2021-02-12 16:45:22 +07:00
|
|
|
p_quote_start = NULL;
|
2021-02-12 16:45:21 +07:00
|
|
|
}
|
|
|
|
}
|
2011-12-15 02:19:19 +07:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-12-06 11:26:22 +07:00
|
|
|
/*
|
|
|
|
* Take an fd and own it. It will be closed on return. filename is used only
|
|
|
|
* for debug messages
|
|
|
|
*/
|
|
|
|
static int kmod_config_parse(struct kmod_config *config, int fd,
|
|
|
|
const char *filename)
|
2011-11-30 03:07:43 +07:00
|
|
|
{
|
2011-12-03 06:40:22 +07:00
|
|
|
struct kmod_ctx *ctx = config->ctx;
|
2011-11-30 03:07:43 +07:00
|
|
|
char *line;
|
|
|
|
FILE *fp;
|
2011-12-22 10:30:40 +07:00
|
|
|
unsigned int linenum = 0;
|
2011-12-06 11:26:22 +07:00
|
|
|
int err;
|
2011-11-30 03:07:43 +07:00
|
|
|
|
2011-12-06 11:26:22 +07:00
|
|
|
fp = fdopen(fd, "r");
|
|
|
|
if (fp == NULL) {
|
|
|
|
err = -errno;
|
2012-02-18 12:56:21 +07:00
|
|
|
ERR(config->ctx, "fd %d: %m\n", fd);
|
2011-12-06 11:26:22 +07:00
|
|
|
close(fd);
|
|
|
|
return err;
|
|
|
|
}
|
2011-11-30 03:07:43 +07:00
|
|
|
|
2014-10-03 13:25:06 +07:00
|
|
|
while ((line = freadline_wrapped(fp, &linenum)) != NULL) {
|
2011-12-02 03:59:54 +07:00
|
|
|
char *cmd, *saveptr;
|
2011-11-30 03:07:43 +07:00
|
|
|
|
|
|
|
if (line[0] == '\0' || line[0] == '#')
|
|
|
|
goto done_next;
|
|
|
|
|
2011-12-02 03:59:54 +07:00
|
|
|
cmd = strtok_r(line, "\t ", &saveptr);
|
2011-11-30 03:07:43 +07:00
|
|
|
if (cmd == NULL)
|
|
|
|
goto done_next;
|
|
|
|
|
2011-12-07 11:26:31 +07:00
|
|
|
if (streq(cmd, "alias")) {
|
2011-12-02 03:59:54 +07:00
|
|
|
char *alias = strtok_r(NULL, "\t ", &saveptr);
|
|
|
|
char *modname = strtok_r(NULL, "\t ", &saveptr);
|
2011-11-30 03:07:43 +07:00
|
|
|
|
2014-10-09 20:59:08 +07:00
|
|
|
if (underscores(alias) < 0 || underscores(modname) < 0)
|
2011-11-30 03:07:43 +07:00
|
|
|
goto syntax_error;
|
|
|
|
|
2014-10-09 20:59:08 +07:00
|
|
|
kmod_config_add_alias(config, alias, modname);
|
2011-12-07 11:26:31 +07:00
|
|
|
} else if (streq(cmd, "blacklist")) {
|
2011-12-02 03:59:54 +07:00
|
|
|
char *modname = strtok_r(NULL, "\t ", &saveptr);
|
2011-11-30 03:48:02 +07:00
|
|
|
|
2014-10-09 20:59:08 +07:00
|
|
|
if (underscores(modname) < 0)
|
2011-11-30 03:48:02 +07:00
|
|
|
goto syntax_error;
|
|
|
|
|
2014-10-09 20:59:08 +07:00
|
|
|
kmod_config_add_blacklist(config, modname);
|
2011-12-07 12:18:57 +07:00
|
|
|
} else if (streq(cmd, "options")) {
|
|
|
|
char *modname = strtok_r(NULL, "\t ", &saveptr);
|
2012-01-11 00:00:07 +07:00
|
|
|
char *options = strtok_r(NULL, "\0", &saveptr);
|
2011-12-07 12:18:57 +07:00
|
|
|
|
2014-10-09 20:59:08 +07:00
|
|
|
if (underscores(modname) < 0 || options == NULL)
|
2011-12-07 12:18:57 +07:00
|
|
|
goto syntax_error;
|
|
|
|
|
2014-10-09 20:59:08 +07:00
|
|
|
kmod_config_add_options(config, modname, options);
|
2011-12-29 00:01:16 +07:00
|
|
|
} else if (streq(cmd, "install")) {
|
2011-12-07 20:31:28 +07:00
|
|
|
char *modname = strtok_r(NULL, "\t ", &saveptr);
|
2012-01-11 00:00:07 +07:00
|
|
|
char *installcmd = strtok_r(NULL, "\0", &saveptr);
|
2011-12-07 20:31:28 +07:00
|
|
|
|
2014-10-09 20:59:08 +07:00
|
|
|
if (underscores(modname) < 0 || installcmd == NULL)
|
2011-12-07 20:31:28 +07:00
|
|
|
goto syntax_error;
|
|
|
|
|
2014-10-09 20:59:08 +07:00
|
|
|
kmod_config_add_command(config, modname, installcmd,
|
2011-12-07 20:31:28 +07:00
|
|
|
cmd, &config->install_commands);
|
2011-12-29 00:01:16 +07:00
|
|
|
} else if (streq(cmd, "remove")) {
|
2011-12-07 20:31:28 +07:00
|
|
|
char *modname = strtok_r(NULL, "\t ", &saveptr);
|
2012-01-11 00:00:07 +07:00
|
|
|
char *removecmd = strtok_r(NULL, "\0", &saveptr);
|
2011-12-07 20:31:28 +07:00
|
|
|
|
2014-10-09 20:59:08 +07:00
|
|
|
if (underscores(modname) < 0 || removecmd == NULL)
|
2011-12-07 20:31:28 +07:00
|
|
|
goto syntax_error;
|
|
|
|
|
2014-10-09 20:59:08 +07:00
|
|
|
kmod_config_add_command(config, modname, removecmd,
|
2011-12-07 20:31:28 +07:00
|
|
|
cmd, &config->remove_commands);
|
2011-12-29 00:01:16 +07:00
|
|
|
} else if (streq(cmd, "softdep")) {
|
2011-12-17 06:18:10 +07:00
|
|
|
char *modname = strtok_r(NULL, "\t ", &saveptr);
|
2012-01-11 00:00:07 +07:00
|
|
|
char *softdeps = strtok_r(NULL, "\0", &saveptr);
|
2011-12-17 06:18:10 +07:00
|
|
|
|
2014-10-09 20:59:08 +07:00
|
|
|
if (underscores(modname) < 0 || softdeps == NULL)
|
2011-12-17 06:18:10 +07:00
|
|
|
goto syntax_error;
|
|
|
|
|
2014-10-09 20:59:08 +07:00
|
|
|
kmod_config_add_softdep(config, modname, softdeps);
|
libkmod: add weak dependecies
It has been seen that for some network mac drivers (i.e. lan78xx) the
related module for the phy is loaded dynamically depending on the current
hardware. In this case, the associated phy is read using mdio bus and then
the associated phy module is loaded during runtime (kernel function
phy_request_driver_module). However, no software dependency is defined, so
the user tools will no be able to get this dependency. For example, if
dracut is used and the hardware is present, lan78xx will be included but no
phy module will be added, and in the next restart the device will not work
from boot because no related phy will be found during initramfs stage.
In order to solve this, we could define a normal 'pre' software dependency
in lan78xx module with all the possible phy modules (there may be some),
but proceeding in that way, all the possible phy modules would be loaded
while only one is necessary.
The idea is to create a new type of dependency, that we are going to call
'weak' to be used only by the user tools that need to detect this situation.
In that way, for example, dracut could check the 'weak' dependency of the
modules involved in order to install these dependencies in initramfs too.
That is, for the commented lan78xx module, defining the 'weak' dependency
with the possible phy modules list, only the necessary phy would be loaded
on demand keeping the same behavior, but all the possible phy modules would
be available from initramfs.
A new function 'kmod_module_get_weakdeps' in libkmod will be added for
this to avoid breaking the API and maintain backward compatibility. This
general procedure could be useful for other similar cases (not only for
dynamic phy loading).
Signed-off-by: Jose Ignacio Tornos Martinez <jtornosm@redhat.com>
Link: https://lore.kernel.org/r/20240327141116.97587-1-jtornosm@redhat.com
2024-03-27 21:11:16 +07:00
|
|
|
} else if (streq(cmd, "weakdep")) {
|
|
|
|
char *modname = strtok_r(NULL, "\t ", &saveptr);
|
|
|
|
char *weakdeps = strtok_r(NULL, "\0", &saveptr);
|
|
|
|
|
|
|
|
if (underscores(modname) < 0 || weakdeps == NULL)
|
|
|
|
goto syntax_error;
|
|
|
|
|
|
|
|
kmod_config_add_weakdep(config, modname, weakdeps);
|
2011-12-07 12:18:57 +07:00
|
|
|
} else if (streq(cmd, "include")
|
2011-12-07 11:26:31 +07:00
|
|
|
|| streq(cmd, "config")) {
|
2012-01-11 09:28:12 +07:00
|
|
|
ERR(ctx, "%s: command %s is deprecated and not parsed anymore\n",
|
2011-11-30 03:48:02 +07:00
|
|
|
filename, cmd);
|
2011-11-30 03:07:43 +07:00
|
|
|
} else {
|
|
|
|
syntax_error:
|
|
|
|
ERR(ctx, "%s line %u: ignoring bad line starting with '%s'\n",
|
|
|
|
filename, linenum, cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
done_next:
|
|
|
|
free(line);
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-12-03 06:40:22 +07:00
|
|
|
void kmod_config_free(struct kmod_config *config)
|
2011-11-30 03:07:43 +07:00
|
|
|
{
|
|
|
|
while (config->aliases)
|
2011-12-03 06:40:22 +07:00
|
|
|
kmod_config_free_alias(config, config->aliases);
|
2011-11-30 03:48:02 +07:00
|
|
|
|
|
|
|
while (config->blacklists)
|
2011-12-03 06:40:22 +07:00
|
|
|
kmod_config_free_blacklist(config, config->blacklists);
|
|
|
|
|
2011-12-07 12:18:57 +07:00
|
|
|
while (config->options)
|
|
|
|
kmod_config_free_options(config, config->options);
|
|
|
|
|
2011-12-07 20:31:28 +07:00
|
|
|
while (config->install_commands) {
|
|
|
|
kmod_config_free_command(config, config->install_commands,
|
|
|
|
&config->install_commands);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (config->remove_commands) {
|
|
|
|
kmod_config_free_command(config, config->remove_commands,
|
|
|
|
&config->remove_commands);
|
|
|
|
}
|
|
|
|
|
2011-12-17 06:18:10 +07:00
|
|
|
while (config->softdeps)
|
|
|
|
kmod_config_free_softdep(config, config->softdeps);
|
|
|
|
|
libkmod: add weak dependecies
It has been seen that for some network mac drivers (i.e. lan78xx) the
related module for the phy is loaded dynamically depending on the current
hardware. In this case, the associated phy is read using mdio bus and then
the associated phy module is loaded during runtime (kernel function
phy_request_driver_module). However, no software dependency is defined, so
the user tools will no be able to get this dependency. For example, if
dracut is used and the hardware is present, lan78xx will be included but no
phy module will be added, and in the next restart the device will not work
from boot because no related phy will be found during initramfs stage.
In order to solve this, we could define a normal 'pre' software dependency
in lan78xx module with all the possible phy modules (there may be some),
but proceeding in that way, all the possible phy modules would be loaded
while only one is necessary.
The idea is to create a new type of dependency, that we are going to call
'weak' to be used only by the user tools that need to detect this situation.
In that way, for example, dracut could check the 'weak' dependency of the
modules involved in order to install these dependencies in initramfs too.
That is, for the commented lan78xx module, defining the 'weak' dependency
with the possible phy modules list, only the necessary phy would be loaded
on demand keeping the same behavior, but all the possible phy modules would
be available from initramfs.
A new function 'kmod_module_get_weakdeps' in libkmod will be added for
this to avoid breaking the API and maintain backward compatibility. This
general procedure could be useful for other similar cases (not only for
dynamic phy loading).
Signed-off-by: Jose Ignacio Tornos Martinez <jtornosm@redhat.com>
Link: https://lore.kernel.org/r/20240327141116.97587-1-jtornosm@redhat.com
2024-03-27 21:11:16 +07:00
|
|
|
while (config->weakdeps)
|
|
|
|
kmod_config_free_weakdep(config, config->weakdeps);
|
|
|
|
|
2012-01-01 03:48:05 +07:00
|
|
|
for (; config->paths != NULL;
|
|
|
|
config->paths = kmod_list_remove(config->paths))
|
|
|
|
free(config->paths->data);
|
|
|
|
|
2011-12-03 06:40:22 +07:00
|
|
|
free(config);
|
2011-11-30 03:07:43 +07:00
|
|
|
}
|
|
|
|
|
2011-12-13 00:23:03 +07:00
|
|
|
static bool conf_files_filter_out(struct kmod_ctx *ctx, DIR *d,
|
|
|
|
const char *path, const char *fn)
|
2011-11-30 03:07:43 +07:00
|
|
|
{
|
|
|
|
size_t len = strlen(fn);
|
2011-12-13 00:23:03 +07:00
|
|
|
struct stat st;
|
2011-11-30 03:07:43 +07:00
|
|
|
|
|
|
|
if (fn[0] == '.')
|
2011-12-13 00:27:57 +07:00
|
|
|
return true;
|
2011-11-30 03:07:43 +07:00
|
|
|
|
2024-07-18 00:26:45 +07:00
|
|
|
if (len < 6 || !streq(&fn[len - 5], ".conf"))
|
2011-12-13 00:27:57 +07:00
|
|
|
return true;
|
2011-11-30 03:07:43 +07:00
|
|
|
|
2011-12-13 00:23:03 +07:00
|
|
|
fstatat(dirfd(d), fn, &st, 0);
|
|
|
|
|
|
|
|
if (S_ISDIR(st.st_mode)) {
|
|
|
|
ERR(ctx, "Directories inside directories are not supported: "
|
|
|
|
"%s/%s\n", path, fn);
|
2011-12-13 00:27:57 +07:00
|
|
|
return true;
|
2011-12-13 00:23:03 +07:00
|
|
|
}
|
|
|
|
|
2011-12-13 00:27:57 +07:00
|
|
|
return false;
|
2011-11-30 03:07:43 +07:00
|
|
|
}
|
|
|
|
|
2011-12-21 00:56:31 +07:00
|
|
|
struct conf_file {
|
|
|
|
const char *path;
|
|
|
|
bool is_single;
|
|
|
|
char name[];
|
|
|
|
};
|
|
|
|
|
|
|
|
static int conf_files_insert_sorted(struct kmod_ctx *ctx,
|
|
|
|
struct kmod_list **list,
|
|
|
|
const char *path, const char *name)
|
|
|
|
{
|
|
|
|
struct kmod_list *lpos, *tmp;
|
|
|
|
struct conf_file *cf;
|
|
|
|
size_t namelen;
|
|
|
|
int cmp = -1;
|
|
|
|
bool is_single = false;
|
|
|
|
|
|
|
|
if (name == NULL) {
|
|
|
|
name = basename(path);
|
|
|
|
is_single = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
kmod_list_foreach(lpos, *list) {
|
|
|
|
cf = lpos->data;
|
|
|
|
|
|
|
|
if ((cmp = strcmp(name, cf->name)) <= 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmp == 0) {
|
|
|
|
DBG(ctx, "Ignoring duplicate config file: %s/%s\n", path,
|
|
|
|
name);
|
|
|
|
return -EEXIST;
|
|
|
|
}
|
|
|
|
|
|
|
|
namelen = strlen(name);
|
|
|
|
cf = malloc(sizeof(*cf) + namelen + 1);
|
|
|
|
if (cf == NULL)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
memcpy(cf->name, name, namelen + 1);
|
|
|
|
cf->path = path;
|
|
|
|
cf->is_single = is_single;
|
|
|
|
|
|
|
|
if (lpos == NULL)
|
|
|
|
tmp = kmod_list_append(*list, cf);
|
|
|
|
else if (lpos == *list)
|
|
|
|
tmp = kmod_list_prepend(*list, cf);
|
|
|
|
else
|
|
|
|
tmp = kmod_list_insert_before(lpos, cf);
|
|
|
|
|
|
|
|
if (tmp == NULL) {
|
|
|
|
free(cf);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lpos == NULL || lpos == *list)
|
|
|
|
*list = tmp;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-12-13 00:00:51 +07:00
|
|
|
/*
|
2011-12-21 00:56:31 +07:00
|
|
|
* Insert configuration files in @list, ignoring duplicates
|
2011-12-13 00:00:51 +07:00
|
|
|
*/
|
2011-12-21 00:56:31 +07:00
|
|
|
static int conf_files_list(struct kmod_ctx *ctx, struct kmod_list **list,
|
2012-01-01 03:48:05 +07:00
|
|
|
const char *path,
|
|
|
|
unsigned long long *path_stamp)
|
2011-11-30 03:07:43 +07:00
|
|
|
{
|
|
|
|
DIR *d;
|
|
|
|
int err;
|
2011-12-21 00:56:31 +07:00
|
|
|
struct stat st;
|
2013-08-28 09:29:47 +07:00
|
|
|
struct dirent *dent;
|
2011-11-30 03:07:43 +07:00
|
|
|
|
2011-12-21 00:56:31 +07:00
|
|
|
if (stat(path, &st) != 0) {
|
|
|
|
err = -errno;
|
|
|
|
DBG(ctx, "could not stat '%s': %m\n", path);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2012-01-17 21:10:42 +07:00
|
|
|
*path_stamp = stat_mstamp(&st);
|
2012-01-01 03:48:05 +07:00
|
|
|
|
2014-03-04 22:51:25 +07:00
|
|
|
if (!S_ISDIR(st.st_mode)) {
|
2011-12-21 00:56:31 +07:00
|
|
|
conf_files_insert_sorted(ctx, list, path, NULL);
|
|
|
|
return 0;
|
|
|
|
}
|
2011-12-13 03:36:27 +07:00
|
|
|
|
2011-11-30 03:07:43 +07:00
|
|
|
d = opendir(path);
|
|
|
|
if (d == NULL) {
|
2012-01-08 04:37:37 +07:00
|
|
|
ERR(ctx, "opendir(%s): %m\n", path);
|
2011-12-21 00:56:31 +07:00
|
|
|
return -EINVAL;
|
2011-11-30 03:07:43 +07:00
|
|
|
}
|
|
|
|
|
2013-08-28 09:29:47 +07:00
|
|
|
for (dent = readdir(d); dent != NULL; dent = readdir(d)) {
|
|
|
|
if (conf_files_filter_out(ctx, d, path, dent->d_name))
|
2011-11-30 03:07:43 +07:00
|
|
|
continue;
|
|
|
|
|
2013-08-28 09:29:47 +07:00
|
|
|
conf_files_insert_sorted(ctx, list, path, dent->d_name);
|
2011-12-06 11:26:22 +07:00
|
|
|
}
|
2011-11-30 03:07:43 +07:00
|
|
|
|
2011-12-21 00:56:31 +07:00
|
|
|
closedir(d);
|
|
|
|
return 0;
|
2011-11-30 03:07:43 +07:00
|
|
|
}
|
|
|
|
|
2011-12-12 19:48:02 +07:00
|
|
|
int kmod_config_new(struct kmod_ctx *ctx, struct kmod_config **p_config,
|
|
|
|
const char * const *config_paths)
|
2011-11-30 03:07:43 +07:00
|
|
|
{
|
2011-12-03 06:40:22 +07:00
|
|
|
struct kmod_config *config;
|
2011-12-21 00:56:31 +07:00
|
|
|
struct kmod_list *list = NULL;
|
2012-01-01 03:48:05 +07:00
|
|
|
struct kmod_list *path_list = NULL;
|
2011-12-06 11:26:22 +07:00
|
|
|
size_t i;
|
2011-11-30 03:07:43 +07:00
|
|
|
|
2014-03-31 20:18:51 +07:00
|
|
|
conf_files_insert_sorted(ctx, &list, kmod_get_dirname(ctx), "modules.softdep");
|
libkmod: add weak dependecies
It has been seen that for some network mac drivers (i.e. lan78xx) the
related module for the phy is loaded dynamically depending on the current
hardware. In this case, the associated phy is read using mdio bus and then
the associated phy module is loaded during runtime (kernel function
phy_request_driver_module). However, no software dependency is defined, so
the user tools will no be able to get this dependency. For example, if
dracut is used and the hardware is present, lan78xx will be included but no
phy module will be added, and in the next restart the device will not work
from boot because no related phy will be found during initramfs stage.
In order to solve this, we could define a normal 'pre' software dependency
in lan78xx module with all the possible phy modules (there may be some),
but proceeding in that way, all the possible phy modules would be loaded
while only one is necessary.
The idea is to create a new type of dependency, that we are going to call
'weak' to be used only by the user tools that need to detect this situation.
In that way, for example, dracut could check the 'weak' dependency of the
modules involved in order to install these dependencies in initramfs too.
That is, for the commented lan78xx module, defining the 'weak' dependency
with the possible phy modules list, only the necessary phy would be loaded
on demand keeping the same behavior, but all the possible phy modules would
be available from initramfs.
A new function 'kmod_module_get_weakdeps' in libkmod will be added for
this to avoid breaking the API and maintain backward compatibility. This
general procedure could be useful for other similar cases (not only for
dynamic phy loading).
Signed-off-by: Jose Ignacio Tornos Martinez <jtornosm@redhat.com>
Link: https://lore.kernel.org/r/20240327141116.97587-1-jtornosm@redhat.com
2024-03-27 21:11:16 +07:00
|
|
|
conf_files_insert_sorted(ctx, &list, kmod_get_dirname(ctx), "modules.weakdep");
|
2014-03-31 20:18:51 +07:00
|
|
|
|
2011-12-12 05:37:01 +07:00
|
|
|
for (i = 0; config_paths[i] != NULL; i++) {
|
|
|
|
const char *path = config_paths[i];
|
2012-01-01 03:48:05 +07:00
|
|
|
unsigned long long path_stamp = 0;
|
|
|
|
size_t pathlen;
|
|
|
|
struct kmod_list *tmp;
|
|
|
|
struct kmod_config_path *cf;
|
|
|
|
|
|
|
|
if (conf_files_list(ctx, &list, path, &path_stamp) < 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
pathlen = strlen(path) + 1;
|
|
|
|
cf = malloc(sizeof(*cf) + pathlen);
|
|
|
|
if (cf == NULL)
|
|
|
|
goto oom;
|
2011-11-30 03:07:43 +07:00
|
|
|
|
2012-01-01 03:48:05 +07:00
|
|
|
cf->stamp = path_stamp;
|
|
|
|
memcpy(cf->path, path, pathlen);
|
|
|
|
|
|
|
|
tmp = kmod_list_append(path_list, cf);
|
2021-04-09 16:44:23 +07:00
|
|
|
if (tmp == NULL) {
|
|
|
|
free(cf);
|
2012-01-01 03:48:05 +07:00
|
|
|
goto oom;
|
2021-04-09 16:44:23 +07:00
|
|
|
}
|
2012-01-01 03:48:05 +07:00
|
|
|
path_list = tmp;
|
2011-12-21 00:56:31 +07:00
|
|
|
}
|
2011-12-12 05:37:01 +07:00
|
|
|
|
2012-01-01 03:48:05 +07:00
|
|
|
*p_config = config = calloc(1, sizeof(struct kmod_config));
|
|
|
|
if (config == NULL)
|
|
|
|
goto oom;
|
|
|
|
|
|
|
|
config->paths = path_list;
|
2012-01-04 17:14:54 +07:00
|
|
|
config->ctx = ctx;
|
2012-01-01 03:48:05 +07:00
|
|
|
|
2011-12-21 00:56:31 +07:00
|
|
|
for (; list != NULL; list = kmod_list_remove(list)) {
|
2016-08-08 21:42:52 +07:00
|
|
|
char buf[PATH_MAX];
|
|
|
|
const char *fn = buf;
|
2011-12-21 00:56:31 +07:00
|
|
|
struct conf_file *cf = list->data;
|
|
|
|
int fd;
|
2011-12-12 05:37:01 +07:00
|
|
|
|
2016-08-08 21:42:52 +07:00
|
|
|
if (cf->is_single) {
|
|
|
|
fn = cf->path;
|
|
|
|
} else if (snprintf(buf, sizeof(buf), "%s/%s",
|
|
|
|
cf->path, cf->name) >= (int)sizeof(buf)) {
|
|
|
|
ERR(ctx, "Error parsing %s/%s: path too long\n",
|
|
|
|
cf->path, cf->name);
|
|
|
|
free(cf);
|
|
|
|
continue;
|
|
|
|
}
|
2011-11-30 03:07:43 +07:00
|
|
|
|
2011-12-21 00:56:31 +07:00
|
|
|
fd = open(fn, O_RDONLY|O_CLOEXEC);
|
|
|
|
DBG(ctx, "parsing file '%s' fd=%d\n", fn, fd);
|
2011-11-30 03:07:43 +07:00
|
|
|
|
2011-12-21 00:56:31 +07:00
|
|
|
if (fd >= 0)
|
|
|
|
kmod_config_parse(config, fd, fn);
|
2011-11-30 03:07:43 +07:00
|
|
|
|
2011-12-21 00:56:31 +07:00
|
|
|
free(cf);
|
2011-11-30 03:07:43 +07:00
|
|
|
}
|
|
|
|
|
2011-12-15 02:19:19 +07:00
|
|
|
kmod_config_parse_kcmdline(config);
|
|
|
|
|
2011-12-06 11:26:22 +07:00
|
|
|
return 0;
|
2012-01-01 03:48:05 +07:00
|
|
|
|
|
|
|
oom:
|
|
|
|
for (; list != NULL; list = kmod_list_remove(list))
|
|
|
|
free(list->data);
|
|
|
|
|
|
|
|
for (; path_list != NULL; path_list = kmod_list_remove(path_list))
|
|
|
|
free(path_list->data);
|
|
|
|
|
|
|
|
return -ENOMEM;
|
2011-11-30 03:07:43 +07:00
|
|
|
}
|
2012-01-13 11:24:18 +07:00
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
* struct kmod_config_iter functions
|
|
|
|
**********************************************************************/
|
|
|
|
|
|
|
|
enum config_type {
|
|
|
|
CONFIG_TYPE_BLACKLIST = 0,
|
|
|
|
CONFIG_TYPE_INSTALL,
|
|
|
|
CONFIG_TYPE_REMOVE,
|
|
|
|
CONFIG_TYPE_ALIAS,
|
|
|
|
CONFIG_TYPE_OPTION,
|
|
|
|
CONFIG_TYPE_SOFTDEP,
|
libkmod: add weak dependecies
It has been seen that for some network mac drivers (i.e. lan78xx) the
related module for the phy is loaded dynamically depending on the current
hardware. In this case, the associated phy is read using mdio bus and then
the associated phy module is loaded during runtime (kernel function
phy_request_driver_module). However, no software dependency is defined, so
the user tools will no be able to get this dependency. For example, if
dracut is used and the hardware is present, lan78xx will be included but no
phy module will be added, and in the next restart the device will not work
from boot because no related phy will be found during initramfs stage.
In order to solve this, we could define a normal 'pre' software dependency
in lan78xx module with all the possible phy modules (there may be some),
but proceeding in that way, all the possible phy modules would be loaded
while only one is necessary.
The idea is to create a new type of dependency, that we are going to call
'weak' to be used only by the user tools that need to detect this situation.
In that way, for example, dracut could check the 'weak' dependency of the
modules involved in order to install these dependencies in initramfs too.
That is, for the commented lan78xx module, defining the 'weak' dependency
with the possible phy modules list, only the necessary phy would be loaded
on demand keeping the same behavior, but all the possible phy modules would
be available from initramfs.
A new function 'kmod_module_get_weakdeps' in libkmod will be added for
this to avoid breaking the API and maintain backward compatibility. This
general procedure could be useful for other similar cases (not only for
dynamic phy loading).
Signed-off-by: Jose Ignacio Tornos Martinez <jtornosm@redhat.com>
Link: https://lore.kernel.org/r/20240327141116.97587-1-jtornosm@redhat.com
2024-03-27 21:11:16 +07:00
|
|
|
CONFIG_TYPE_WEAKDEP,
|
2012-01-13 11:24:18 +07:00
|
|
|
};
|
|
|
|
|
|
|
|
struct kmod_config_iter {
|
|
|
|
enum config_type type;
|
2012-01-13 19:49:31 +07:00
|
|
|
bool intermediate;
|
2012-01-13 11:24:18 +07:00
|
|
|
const struct kmod_list *list;
|
|
|
|
const struct kmod_list *curr;
|
2012-01-13 19:49:31 +07:00
|
|
|
void *data;
|
2012-01-13 11:24:18 +07:00
|
|
|
const char *(*get_key)(const struct kmod_list *l);
|
|
|
|
const char *(*get_value)(const struct kmod_list *l);
|
|
|
|
};
|
|
|
|
|
2012-01-13 19:49:31 +07:00
|
|
|
static const char *softdep_get_plain_softdep(const struct kmod_list *l)
|
|
|
|
{
|
|
|
|
char *s = softdep_to_char(l->data);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
libkmod: add weak dependecies
It has been seen that for some network mac drivers (i.e. lan78xx) the
related module for the phy is loaded dynamically depending on the current
hardware. In this case, the associated phy is read using mdio bus and then
the associated phy module is loaded during runtime (kernel function
phy_request_driver_module). However, no software dependency is defined, so
the user tools will no be able to get this dependency. For example, if
dracut is used and the hardware is present, lan78xx will be included but no
phy module will be added, and in the next restart the device will not work
from boot because no related phy will be found during initramfs stage.
In order to solve this, we could define a normal 'pre' software dependency
in lan78xx module with all the possible phy modules (there may be some),
but proceeding in that way, all the possible phy modules would be loaded
while only one is necessary.
The idea is to create a new type of dependency, that we are going to call
'weak' to be used only by the user tools that need to detect this situation.
In that way, for example, dracut could check the 'weak' dependency of the
modules involved in order to install these dependencies in initramfs too.
That is, for the commented lan78xx module, defining the 'weak' dependency
with the possible phy modules list, only the necessary phy would be loaded
on demand keeping the same behavior, but all the possible phy modules would
be available from initramfs.
A new function 'kmod_module_get_weakdeps' in libkmod will be added for
this to avoid breaking the API and maintain backward compatibility. This
general procedure could be useful for other similar cases (not only for
dynamic phy loading).
Signed-off-by: Jose Ignacio Tornos Martinez <jtornosm@redhat.com>
Link: https://lore.kernel.org/r/20240327141116.97587-1-jtornosm@redhat.com
2024-03-27 21:11:16 +07:00
|
|
|
static const char *weakdep_get_plain_weakdep(const struct kmod_list *l)
|
|
|
|
{
|
|
|
|
char *s = weakdep_to_char(l->data);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2012-01-13 11:24:18 +07:00
|
|
|
static struct kmod_config_iter *kmod_config_iter_new(const struct kmod_ctx* ctx,
|
|
|
|
enum config_type type)
|
|
|
|
{
|
|
|
|
struct kmod_config_iter *iter = calloc(1, sizeof(*iter));
|
2012-06-12 11:43:46 +07:00
|
|
|
const struct kmod_config *config = kmod_get_config(ctx);
|
2012-01-13 11:24:18 +07:00
|
|
|
|
|
|
|
if (iter == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
iter->type = type;
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case CONFIG_TYPE_BLACKLIST:
|
2012-06-12 11:43:46 +07:00
|
|
|
iter->list = config->blacklists;
|
2012-01-13 11:24:18 +07:00
|
|
|
iter->get_key = kmod_blacklist_get_modname;
|
|
|
|
break;
|
|
|
|
case CONFIG_TYPE_INSTALL:
|
2012-06-12 11:43:46 +07:00
|
|
|
iter->list = config->install_commands;
|
2012-01-13 11:24:18 +07:00
|
|
|
iter->get_key = kmod_command_get_modname;
|
|
|
|
iter->get_value = kmod_command_get_command;
|
|
|
|
break;
|
|
|
|
case CONFIG_TYPE_REMOVE:
|
2012-06-12 11:43:46 +07:00
|
|
|
iter->list = config->remove_commands;
|
2012-01-13 11:24:18 +07:00
|
|
|
iter->get_key = kmod_command_get_modname;
|
|
|
|
iter->get_value = kmod_command_get_command;
|
|
|
|
break;
|
|
|
|
case CONFIG_TYPE_ALIAS:
|
2012-06-12 11:43:46 +07:00
|
|
|
iter->list = config->aliases;
|
2012-01-13 11:24:18 +07:00
|
|
|
iter->get_key = kmod_alias_get_name;
|
|
|
|
iter->get_value = kmod_alias_get_modname;
|
|
|
|
break;
|
|
|
|
case CONFIG_TYPE_OPTION:
|
2012-06-12 11:43:46 +07:00
|
|
|
iter->list = config->options;
|
2012-01-13 11:24:18 +07:00
|
|
|
iter->get_key = kmod_option_get_modname;
|
|
|
|
iter->get_value = kmod_option_get_options;
|
|
|
|
break;
|
|
|
|
case CONFIG_TYPE_SOFTDEP:
|
2012-06-12 11:43:46 +07:00
|
|
|
iter->list = config->softdeps;
|
2012-01-13 11:24:18 +07:00
|
|
|
iter->get_key = kmod_softdep_get_name;
|
2012-01-13 19:49:31 +07:00
|
|
|
iter->get_value = softdep_get_plain_softdep;
|
|
|
|
iter->intermediate = true;
|
2012-01-13 11:24:18 +07:00
|
|
|
break;
|
libkmod: add weak dependecies
It has been seen that for some network mac drivers (i.e. lan78xx) the
related module for the phy is loaded dynamically depending on the current
hardware. In this case, the associated phy is read using mdio bus and then
the associated phy module is loaded during runtime (kernel function
phy_request_driver_module). However, no software dependency is defined, so
the user tools will no be able to get this dependency. For example, if
dracut is used and the hardware is present, lan78xx will be included but no
phy module will be added, and in the next restart the device will not work
from boot because no related phy will be found during initramfs stage.
In order to solve this, we could define a normal 'pre' software dependency
in lan78xx module with all the possible phy modules (there may be some),
but proceeding in that way, all the possible phy modules would be loaded
while only one is necessary.
The idea is to create a new type of dependency, that we are going to call
'weak' to be used only by the user tools that need to detect this situation.
In that way, for example, dracut could check the 'weak' dependency of the
modules involved in order to install these dependencies in initramfs too.
That is, for the commented lan78xx module, defining the 'weak' dependency
with the possible phy modules list, only the necessary phy would be loaded
on demand keeping the same behavior, but all the possible phy modules would
be available from initramfs.
A new function 'kmod_module_get_weakdeps' in libkmod will be added for
this to avoid breaking the API and maintain backward compatibility. This
general procedure could be useful for other similar cases (not only for
dynamic phy loading).
Signed-off-by: Jose Ignacio Tornos Martinez <jtornosm@redhat.com>
Link: https://lore.kernel.org/r/20240327141116.97587-1-jtornosm@redhat.com
2024-03-27 21:11:16 +07:00
|
|
|
case CONFIG_TYPE_WEAKDEP:
|
|
|
|
iter->list = config->weakdeps;
|
|
|
|
iter->get_key = kmod_weakdep_get_name;
|
|
|
|
iter->get_value = weakdep_get_plain_weakdep;
|
|
|
|
iter->intermediate = true;
|
|
|
|
break;
|
2012-01-13 11:24:18 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
return iter;
|
|
|
|
}
|
|
|
|
|
2012-01-14 21:09:34 +07:00
|
|
|
/**
|
|
|
|
* SECTION:libkmod-config
|
|
|
|
* @short_description: retrieve current libkmod configuration
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* kmod_config_get_blacklists:
|
|
|
|
* @ctx: kmod library context
|
|
|
|
*
|
|
|
|
* Retrieve an iterator to deal with the blacklist maintained inside the
|
|
|
|
* library. See kmod_config_iter_get_key(), kmod_config_iter_get_value() and
|
|
|
|
* kmod_config_iter_next(). At least one call to kmod_config_iter_next() must
|
|
|
|
* be made to initialize the iterator and check if it's valid.
|
|
|
|
*
|
2012-04-19 21:08:24 +07:00
|
|
|
* Returns: a new iterator over the blacklists or NULL on failure. Free it
|
2012-01-14 21:09:34 +07:00
|
|
|
* with kmod_config_iter_free_iter().
|
|
|
|
*/
|
2012-01-13 11:24:18 +07:00
|
|
|
KMOD_EXPORT struct kmod_config_iter *kmod_config_get_blacklists(const struct kmod_ctx *ctx)
|
|
|
|
{
|
|
|
|
if (ctx == NULL)
|
|
|
|
return NULL;;
|
|
|
|
|
|
|
|
return kmod_config_iter_new(ctx, CONFIG_TYPE_BLACKLIST);
|
|
|
|
}
|
|
|
|
|
2012-01-14 21:09:34 +07:00
|
|
|
/**
|
|
|
|
* kmod_config_get_install_commands:
|
|
|
|
* @ctx: kmod library context
|
|
|
|
*
|
|
|
|
* Retrieve an iterator to deal with the install commands maintained inside the
|
|
|
|
* library. See kmod_config_iter_get_key(), kmod_config_iter_get_value() and
|
|
|
|
* kmod_config_iter_next(). At least one call to kmod_config_iter_next() must
|
|
|
|
* be made to initialize the iterator and check if it's valid.
|
|
|
|
*
|
2012-04-19 21:08:24 +07:00
|
|
|
* Returns: a new iterator over the install commands or NULL on failure. Free
|
2012-01-14 21:09:34 +07:00
|
|
|
* it with kmod_config_iter_free_iter().
|
|
|
|
*/
|
2012-01-13 11:24:18 +07:00
|
|
|
KMOD_EXPORT struct kmod_config_iter *kmod_config_get_install_commands(const struct kmod_ctx *ctx)
|
|
|
|
{
|
|
|
|
if (ctx == NULL)
|
|
|
|
return NULL;;
|
|
|
|
|
|
|
|
return kmod_config_iter_new(ctx, CONFIG_TYPE_INSTALL);
|
|
|
|
}
|
|
|
|
|
2012-01-14 21:09:34 +07:00
|
|
|
/**
|
|
|
|
* kmod_config_get_remove_commands:
|
|
|
|
* @ctx: kmod library context
|
|
|
|
*
|
|
|
|
* Retrieve an iterator to deal with the remove commands maintained inside the
|
|
|
|
* library. See kmod_config_iter_get_key(), kmod_config_iter_get_value() and
|
|
|
|
* kmod_config_iter_next(). At least one call to kmod_config_iter_next() must
|
|
|
|
* be made to initialize the iterator and check if it's valid.
|
|
|
|
*
|
2012-04-19 21:08:24 +07:00
|
|
|
* Returns: a new iterator over the remove commands or NULL on failure. Free
|
2012-01-14 21:09:34 +07:00
|
|
|
* it with kmod_config_iter_free_iter().
|
|
|
|
*/
|
2012-01-13 11:24:18 +07:00
|
|
|
KMOD_EXPORT struct kmod_config_iter *kmod_config_get_remove_commands(const struct kmod_ctx *ctx)
|
|
|
|
{
|
|
|
|
if (ctx == NULL)
|
|
|
|
return NULL;;
|
|
|
|
|
|
|
|
return kmod_config_iter_new(ctx, CONFIG_TYPE_REMOVE);
|
|
|
|
}
|
|
|
|
|
2012-01-14 21:09:34 +07:00
|
|
|
/**
|
|
|
|
* kmod_config_get_aliases:
|
|
|
|
* @ctx: kmod library context
|
|
|
|
*
|
|
|
|
* Retrieve an iterator to deal with the aliases maintained inside the
|
|
|
|
* library. See kmod_config_iter_get_key(), kmod_config_iter_get_value() and
|
|
|
|
* kmod_config_iter_next(). At least one call to kmod_config_iter_next() must
|
|
|
|
* be made to initialize the iterator and check if it's valid.
|
|
|
|
*
|
2012-04-19 21:08:24 +07:00
|
|
|
* Returns: a new iterator over the aliases or NULL on failure. Free it with
|
2012-01-14 21:09:34 +07:00
|
|
|
* kmod_config_iter_free_iter().
|
|
|
|
*/
|
2012-01-13 11:24:18 +07:00
|
|
|
KMOD_EXPORT struct kmod_config_iter *kmod_config_get_aliases(const struct kmod_ctx *ctx)
|
|
|
|
{
|
|
|
|
if (ctx == NULL)
|
|
|
|
return NULL;;
|
|
|
|
|
|
|
|
return kmod_config_iter_new(ctx, CONFIG_TYPE_ALIAS);
|
|
|
|
}
|
|
|
|
|
2012-01-14 21:09:34 +07:00
|
|
|
/**
|
|
|
|
* kmod_config_get_options:
|
|
|
|
* @ctx: kmod library context
|
|
|
|
*
|
|
|
|
* Retrieve an iterator to deal with the options maintained inside the
|
|
|
|
* library. See kmod_config_iter_get_key(), kmod_config_iter_get_value() and
|
|
|
|
* kmod_config_iter_next(). At least one call to kmod_config_iter_next() must
|
|
|
|
* be made to initialize the iterator and check if it's valid.
|
|
|
|
*
|
2012-04-19 21:08:24 +07:00
|
|
|
* Returns: a new iterator over the options or NULL on failure. Free it with
|
2012-01-14 21:09:34 +07:00
|
|
|
* kmod_config_iter_free_iter().
|
|
|
|
*/
|
2012-01-13 11:24:18 +07:00
|
|
|
KMOD_EXPORT struct kmod_config_iter *kmod_config_get_options(const struct kmod_ctx *ctx)
|
|
|
|
{
|
|
|
|
if (ctx == NULL)
|
|
|
|
return NULL;;
|
|
|
|
|
|
|
|
return kmod_config_iter_new(ctx, CONFIG_TYPE_OPTION);
|
|
|
|
}
|
|
|
|
|
2012-01-14 21:09:34 +07:00
|
|
|
/**
|
|
|
|
* kmod_config_get_softdeps:
|
|
|
|
* @ctx: kmod library context
|
|
|
|
*
|
|
|
|
* Retrieve an iterator to deal with the softdeps maintained inside the
|
|
|
|
* library. See kmod_config_iter_get_key(), kmod_config_iter_get_value() and
|
|
|
|
* kmod_config_iter_next(). At least one call to kmod_config_iter_next() must
|
|
|
|
* be made to initialize the iterator and check if it's valid.
|
|
|
|
*
|
2012-04-19 21:08:24 +07:00
|
|
|
* Returns: a new iterator over the softdeps or NULL on failure. Free it with
|
2012-01-14 21:09:34 +07:00
|
|
|
* kmod_config_iter_free_iter().
|
|
|
|
*/
|
2012-01-13 11:24:18 +07:00
|
|
|
KMOD_EXPORT struct kmod_config_iter *kmod_config_get_softdeps(const struct kmod_ctx *ctx)
|
|
|
|
{
|
|
|
|
if (ctx == NULL)
|
|
|
|
return NULL;;
|
|
|
|
|
|
|
|
return kmod_config_iter_new(ctx, CONFIG_TYPE_SOFTDEP);
|
|
|
|
}
|
|
|
|
|
libkmod: add weak dependecies
It has been seen that for some network mac drivers (i.e. lan78xx) the
related module for the phy is loaded dynamically depending on the current
hardware. In this case, the associated phy is read using mdio bus and then
the associated phy module is loaded during runtime (kernel function
phy_request_driver_module). However, no software dependency is defined, so
the user tools will no be able to get this dependency. For example, if
dracut is used and the hardware is present, lan78xx will be included but no
phy module will be added, and in the next restart the device will not work
from boot because no related phy will be found during initramfs stage.
In order to solve this, we could define a normal 'pre' software dependency
in lan78xx module with all the possible phy modules (there may be some),
but proceeding in that way, all the possible phy modules would be loaded
while only one is necessary.
The idea is to create a new type of dependency, that we are going to call
'weak' to be used only by the user tools that need to detect this situation.
In that way, for example, dracut could check the 'weak' dependency of the
modules involved in order to install these dependencies in initramfs too.
That is, for the commented lan78xx module, defining the 'weak' dependency
with the possible phy modules list, only the necessary phy would be loaded
on demand keeping the same behavior, but all the possible phy modules would
be available from initramfs.
A new function 'kmod_module_get_weakdeps' in libkmod will be added for
this to avoid breaking the API and maintain backward compatibility. This
general procedure could be useful for other similar cases (not only for
dynamic phy loading).
Signed-off-by: Jose Ignacio Tornos Martinez <jtornosm@redhat.com>
Link: https://lore.kernel.org/r/20240327141116.97587-1-jtornosm@redhat.com
2024-03-27 21:11:16 +07:00
|
|
|
/**
|
|
|
|
* kmod_config_get_weakdeps:
|
|
|
|
* @ctx: kmod library context
|
|
|
|
*
|
|
|
|
* Retrieve an iterator to deal with the weakdeps maintained inside the
|
|
|
|
* library. See kmod_config_iter_get_key(), kmod_config_iter_get_value() and
|
|
|
|
* kmod_config_iter_next(). At least one call to kmod_config_iter_next() must
|
|
|
|
* be made to initialize the iterator and check if it's valid.
|
|
|
|
*
|
|
|
|
* Returns: a new iterator over the weakdeps or NULL on failure. Free it with
|
|
|
|
* kmod_config_iter_free_iter().
|
|
|
|
*/
|
|
|
|
KMOD_EXPORT struct kmod_config_iter *kmod_config_get_weakdeps(const struct kmod_ctx *ctx)
|
|
|
|
{
|
|
|
|
if (ctx == NULL)
|
|
|
|
return NULL;;
|
|
|
|
|
|
|
|
return kmod_config_iter_new(ctx, CONFIG_TYPE_WEAKDEP);
|
|
|
|
}
|
|
|
|
|
2012-01-14 21:09:34 +07:00
|
|
|
/**
|
|
|
|
* kmod_config_iter_get_key:
|
|
|
|
* @iter: iterator over a certain configuration
|
|
|
|
*
|
|
|
|
* When using a new allocated iterator, user must perform a call to
|
|
|
|
* kmod_config_iter_next() to initialize iterator's position and check if it's
|
|
|
|
* valid.
|
|
|
|
*
|
|
|
|
* Returns: the key of the current configuration pointed by @iter.
|
|
|
|
*/
|
2012-01-13 11:24:18 +07:00
|
|
|
KMOD_EXPORT const char *kmod_config_iter_get_key(const struct kmod_config_iter *iter)
|
|
|
|
{
|
|
|
|
if (iter == NULL || iter->curr == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return iter->get_key(iter->curr);
|
|
|
|
}
|
|
|
|
|
2012-01-14 21:09:34 +07:00
|
|
|
/**
|
|
|
|
* kmod_config_iter_get_value:
|
|
|
|
* @iter: iterator over a certain configuration
|
|
|
|
*
|
|
|
|
* When using a new allocated iterator, user must perform a call to
|
|
|
|
* kmod_config_iter_next() to initialize iterator's position and check if it's
|
|
|
|
* valid.
|
|
|
|
*
|
|
|
|
* Returns: the value of the current configuration pointed by @iter.
|
|
|
|
*/
|
2012-01-13 11:24:18 +07:00
|
|
|
KMOD_EXPORT const char *kmod_config_iter_get_value(const struct kmod_config_iter *iter)
|
|
|
|
{
|
2012-01-13 19:49:31 +07:00
|
|
|
const char *s;
|
|
|
|
|
2012-01-13 11:24:18 +07:00
|
|
|
if (iter == NULL || iter->curr == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (iter->get_value == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2012-01-13 19:49:31 +07:00
|
|
|
if (iter->intermediate) {
|
|
|
|
struct kmod_config_iter *i = (struct kmod_config_iter *)iter;
|
|
|
|
|
|
|
|
free(i->data);
|
|
|
|
s = i->data = (void *) iter->get_value(iter->curr);
|
|
|
|
} else
|
|
|
|
s = iter->get_value(iter->curr);
|
|
|
|
|
|
|
|
return s;
|
2012-01-13 11:24:18 +07:00
|
|
|
}
|
|
|
|
|
2012-01-14 21:09:34 +07:00
|
|
|
/**
|
|
|
|
* kmod_config_iter_next:
|
|
|
|
* @iter: iterator over a certain configuration
|
|
|
|
*
|
|
|
|
* Make @iter point to the next item of a certain configuration. It's an
|
|
|
|
* automatically recycling iterator. When it reaches the end, false is
|
|
|
|
* returned; then if user wants to iterate again, it's sufficient to call this
|
|
|
|
* function once more.
|
|
|
|
*
|
|
|
|
* Returns: true if next position of @iter is valid or false if its end is
|
|
|
|
* reached.
|
|
|
|
*/
|
2012-01-13 11:24:18 +07:00
|
|
|
KMOD_EXPORT bool kmod_config_iter_next(struct kmod_config_iter *iter)
|
|
|
|
{
|
|
|
|
if (iter == NULL)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (iter->curr == NULL) {
|
|
|
|
iter->curr = iter->list;
|
|
|
|
return iter->curr != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
iter->curr = kmod_list_next(iter->list, iter->curr);
|
|
|
|
|
|
|
|
return iter->curr != NULL;
|
|
|
|
}
|
|
|
|
|
2012-01-14 21:09:34 +07:00
|
|
|
/**
|
|
|
|
* kmod_config_iter_free_iter:
|
|
|
|
* @iter: iterator over a certain configuration
|
|
|
|
*
|
|
|
|
* Free resources used by the iterator.
|
|
|
|
*/
|
2012-01-13 11:24:18 +07:00
|
|
|
KMOD_EXPORT void kmod_config_iter_free_iter(struct kmod_config_iter *iter)
|
|
|
|
{
|
2012-01-13 19:49:31 +07:00
|
|
|
free(iter->data);
|
2012-01-13 11:24:18 +07:00
|
|
|
free(iter);
|
|
|
|
}
|