From a62c12b2724d29abb0425a47b8bc25d5a6498ab1 Mon Sep 17 00:00:00 2001 From: Alexey Gladkov Date: Wed, 14 Aug 2024 12:40:15 +0200 Subject: [PATCH] depinfo: Add support of weakdeps Weak dependencies are similar to "pre softdep" and are also optional, but unlike them kmod will not load such modules. Such dependencies are intended to indicate optional dependencies between modules. Link: https://politreco.com/2024/08/linux-module-dependencies/ Link: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=61842868de13aa7fd7391c626e889f4d6f1450bf Signed-off-by: Alexey Gladkov --- utils/depinfo/kmod-depinfo.c | 41 +++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/utils/depinfo/kmod-depinfo.c b/utils/depinfo/kmod-depinfo.c index d9eb0045..98843d57 100644 --- a/utils/depinfo/kmod-depinfo.c +++ b/utils/depinfo/kmod-depinfo.c @@ -352,14 +352,14 @@ static int depinfo_alias(struct kmod_ctx *ctx, const char *alias, enum alias_need req); static int -process_depends(struct kmod_ctx *ctx, const char *depends) +__process_depends(struct kmod_ctx *ctx, const char *depends, const char *delim, enum alias_need req) { int ret = 0; char *s, *str, *token, *saveptr = NULL; s = str = strdup(depends); - while ((token = strtok_r(str, ",", &saveptr)) != NULL) { - if (depinfo_alias(ctx, token, ALIAS_REQUIRED) < 0) + while ((token = strtok_r(str, delim, &saveptr)) != NULL) { + if (depinfo_alias(ctx, token, req) < 0) ret = -1; str = NULL; } @@ -367,26 +367,30 @@ process_depends(struct kmod_ctx *ctx, const char *depends) return ret; } + +static int +process_depends(struct kmod_ctx *ctx, const char *depends) +{ + return __process_depends(ctx, depends, ",", ALIAS_REQUIRED); +} + static int process_soft_depends(struct kmod_ctx *ctx, const char *depends) { - int ret = 0; - char *s, *str, *token, *saveptr = NULL; size_t len = strlen(depends); - s = str = strdup(depends); - if (len > 5 && !strncmp("pre: ", s, 5)) - str += 5; - else if (len > 6 && !strncmp("post: ", s, 6)) - str += 6; + if (len > 5 && !strncmp("pre: ", depends, 5)) + depends += 5; + else if (len > 6 && !strncmp("post: ", depends, 6)) + depends += 6; - while ((token = strtok_r(str, " ", &saveptr)) != NULL) { - if (depinfo_alias(ctx, token, ALIAS_OPTIONAL) < 0) - ret = -1; - str = NULL; - } - free(s); - return ret; + return __process_depends(ctx, depends, " ", ALIAS_OPTIONAL); +} + +static int +process_weak_depends(struct kmod_ctx *ctx, const char *depends) +{ + return __process_depends(ctx, depends, " ", ALIAS_OPTIONAL); } static int @@ -440,6 +444,9 @@ depinfo(struct kmod_ctx *ctx, struct kmod_module *mod) } else if (!strcmp("softdep", key)) { if (process_soft_depends(ctx, val) < 0) ret = -1; + } else if (!strcmp("weakdep", key)) { + if (process_weak_depends(ctx, val) < 0) + ret = -1; } } if ((opts & SHOW_FIRMWARE) && !strcmp("firmware", key))