Random fixes + modinfo --modname
cover Random fixes + modinfo --modname
First commits are some random fixes and refactors while preparing the code base for a new API: kmod_module_new_from_name_lookup(). This new function is used by modinfo to allow handling the argument as a module name instead of alias or file. Main motivation is to allow to bypass aliases set by kernel or user configuration that would otherwise forbid to get the info from a particular module. Although for modules it's possible to just find the correspondent file, this is not possible for builtin modules.
Lucas De Marchi (8):
test-initstate: Check for negative value on error
libkmod-module: Fix return code for kmod_module_new_from_lookup()
gitignore: ignore gtk-doc.make
libkmod: Add helper function to iterate lookup options
libkmod: Update docs about indexes order
libkmod: Add lookup from module name
modinfo: Update help message with "modulename"
modinfo: Allow to force arg as module name
.gitignore | 1 + libkmod/libkmod-module.c | 131 +++++++++++++++++++++++++------------ libkmod/libkmod.h | 3 + libkmod/libkmod.sym | 1 + testsuite/test-initstate.c | 2 +- tools/modinfo.c | 32 ++++++++- 6 files changed, 125 insertions(+), 45 deletions(-)
Signature · 1 line
2.35.1
PATCH 1/8 test-initstate: Check for negative value on error
Documentation says kmod_module_new_from_lookup() returns < 0 on error and 0 otherwise. There are bugs in libkmod however making it return a positive value in some situations, that need to be fixed. However it's best to check for the error explicitly like is done in the rest of the library to avoid this kind of issues.
--- testsuite/test-initstate.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
testsuite/test-initstate.c +1 -1
@@ -45,7 +45,7 @@ static noreturn int test_initstate_from_lookup(const struct test *t)exit (EXIT_FAILURE ); err =kmod_module_new_from_lookup (ctx,"fake-builtin" , &list); -if (err !=0 ) { +if (err <0 ) {ERR ("could not create module from lookup: %s\n" ,strerror (-err));exit (EXIT_FAILURE ); }
Signature · 1 line
2.35.1
PATCH 2/8 libkmod-module: Fix return code for kmod_module_new_from_lookup()
In reply to Lucas De MarchiWhen kmod_module_new_from_lookup() resolves to an alias, err will be set to a positive value from the lookup function. Do not return a positive value to follow the behavior when it matches a module name and the documentation.
--- libkmod/libkmod-module.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
libkmod/libkmod-module.c +2 -2
@@ -589,8 +589,8 @@ KMOD_EXPORT int kmod_module_new_from_lookup(struct kmod_ctx *ctx, finish: -DBG (ctx,"lookup %s=%d, list=%p\n" , alias, err, *list); -return err; +DBG (ctx,"lookup %s matches=%d, list=%p\n" , alias, err, *list); +return err >0 ?0 : err; fail:DBG (ctx,"Failed to lookup %s\n" , alias);kmod_module_unref_list (*list);
Signature · 1 line
2.35.1
PATCH 3/8 gitignore: ignore gtk-doc.make
In reply to Lucas De Marchi--- .gitignore | 1 + 1 file changed, 1 insertion(+)
.gitignore +1 -0
@@ -20,6 +20,7 @@ /configure /cov-int /coverage +/gtk-doc.make /kmod-*.tar.* /libtool /stamp-h1
Signature · 1 line
2.35.1
PATCH 4/8 libkmod: Add helper function to iterate lookup options
In reply to Lucas De MarchiThe CHECK_ERR_AND_FINISH macro with conditional code flow changes has been a source of bugs. Get rid of it replacing with a helper function to iterate an array of lookup functions. This helper may also be useful in future to create different lookup APIs in libkmod.
--- libkmod/libkmod-module.c | 82 ++++++++++++++++++---------------------- 1 file changed, 37 insertions(+), 45 deletions(-)
libkmod/libkmod-module.c +37 -45
@@ -499,13 +499,26 @@ KMOD_EXPORT struct kmod_module *kmod_module_ref(struct kmod_module *mod)return mod; } -#define CHECK_ERR_AND_FINISH (_err, _label_err, _list, label_finish) \ - do { \ - if ((_err) < 0) \ - goto _label_err; \ - if (*(_list) != NULL) \ - goto finish; \ - } while (0) +typedef int (*lookup_func )(struct kmod_ctx *ctx,const char *name,struct kmod_list **list) __attribute__((nonnull (1 ,2 ,3 ))); + +static int __kmod_module_new_from_lookup (struct kmod_ctx *ctx,const lookup_func lookup[], +size_t lookup_count,const char *s, +struct kmod_list **list) +{ +unsigned int i; + +for (i =0 ; i < lookup_count; i++) { +int err; + + err = lookup[i](ctx, s, list); +if (err <0 && err != -ENOSYS ) +return err; +else if (*list !=NULL ) +return 0 ; + } + +return 0 ; +}/** * kmod_module_new_from_lookup:
@@ -538,8 +551,17 @@ KMOD_EXPORT int kmod_module_new_from_lookup(struct kmod_ctx *ctx,const char *given_alias, struct kmod_list **list) { - int err; + const lookup_func lookup[] = { + kmod_lookup_alias_from_config, + kmod_lookup_alias_from_moddep_file, + kmod_lookup_alias_from_symbols_file, + kmod_lookup_alias_from_commands, + kmod_lookup_alias_from_aliases_file, + kmod_lookup_alias_from_builtin_file, + kmod_lookup_alias_from_kernel_builtin_file, + };char alias[PATH_MAX ]; +int err;if (ctx ==NULL || given_alias ==NULL )return -ENOENT ;
@@ -556,48 +578,18 @@ KMOD_EXPORT int kmod_module_new_from_lookup(struct kmod_ctx *ctx,DBG (ctx,"input alias=%s, normalized=%s\n" , given_alias, alias); -/* Aliases from config file override all the others */ - err =kmod_lookup_alias_from_config (ctx, alias, list); -CHECK_ERR_AND_FINISH (err, fail, list, finish); + err =__kmod_module_new_from_lookup (ctx, lookup,sizeof (lookup), + alias, list); -DBG (ctx,"lookup modules.dep %s\n" , alias); - err =kmod_lookup_alias_from_moddep_file (ctx, alias, list); -CHECK_ERR_AND_FINISH (err, fail, list, finish); - -DBG (ctx,"lookup modules.symbols %s\n" , alias); - err =kmod_lookup_alias_from_symbols_file (ctx, alias, list); -CHECK_ERR_AND_FINISH (err, fail, list, finish); - -DBG (ctx,"lookup install and remove commands %s\n" , alias); - err =kmod_lookup_alias_from_commands (ctx, alias, list); -CHECK_ERR_AND_FINISH (err, fail, list, finish); - -DBG (ctx,"lookup modules.aliases %s\n" , alias); - err =kmod_lookup_alias_from_aliases_file (ctx, alias, list); -CHECK_ERR_AND_FINISH (err, fail, list, finish); - -DBG (ctx,"lookup modules.builtin %s\n" , alias); - err =kmod_lookup_alias_from_builtin_file (ctx, alias, list); -CHECK_ERR_AND_FINISH (err, fail, list, finish); - -DBG (ctx,"lookup modules.builtin.modinfo %s\n" , alias); - err =kmod_lookup_alias_from_kernel_builtin_file (ctx, alias, list); -/* Optional index missing, ignore */ -if (err == -ENOSYS ) - err =0 ; -CHECK_ERR_AND_FINISH (err, fail, list, finish); +DBG (ctx,"lookup=%s found=%d\n" , alias, err >=0 && *list); +if (err <0 ) { +kmod_module_unref_list (*list); + *list =NULL ; + } -finish: -DBG (ctx,"lookup %s matches=%d, list=%p\n" , alias, err, *list); -return err >0 ?0 : err; -fail: -DBG (ctx,"Failed to lookup %s\n" , alias); -kmod_module_unref_list (*list); - *list =NULL ;return err; } -#undef CHECK_ERR_AND_FINISH/** * kmod_module_unref_list:
Signature · 1 line
2.35.1
PATCH 5/8 libkmod: Update docs about indexes order
In reply to Lucas De MarchiNew indexes were created without updating the documentation about the order in kmod_module_new_from_lookup(). Add them to the documentation.
--- libkmod/libkmod-module.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
libkmod/libkmod-module.c +1 -1
@@ -534,7 +534,7 @@ static int __kmod_module_new_from_lookup(struct kmod_ctx *ctx, const lookup_func* * The search order is: 1. aliases in configuration file; 2. module names in * modules.dep index; 3. symbol aliases in modules.symbols index; 4. aliases -* in modules.alias index. +* from install commands; 5. builtin indexes from kernel. * * The initial refcount is 1, and needs to be decremented to release the * resources of the kmod_module. The returned @list must be released by
Signature · 1 line
2.35.1
PATCH 6/8 libkmod: Add lookup from module name
In reply to Lucas De MarchiSlightly different than kmod_module_new_from_lookup(): it doesn't consider aliases, only module names. This is useful for cases we want to force a tool to handle something as the module name, without trying to interpret it as an alias.
--- libkmod/libkmod-module.c | 57 ++++++++++++++++++++++++++++++++++++++++ libkmod/libkmod.h | 3 +++ libkmod/libkmod.sym | 1 + 3 files changed, 61 insertions(+)
libkmod/libkmod-module.c +57 -0
@@ -591,6 +591,63 @@ KMOD_EXPORT int kmod_module_new_from_lookup(struct kmod_ctx *ctx,return err; } +/** +* kmod_module_new_from_name_lookup: +* @ctx: kmod library context +* @modname: module name to look for +* @mod: returned module on success +* +* Lookup by module name, without considering possible aliases. This is similar +* to kmod_module_new_from_lookup(), but don't consider as source indexes and +* configurations that work with aliases. When succesful, this always resolves +* to one and only one module. +* +* The search order is: 1. module names in modules.dep index; +* 2. builtin indexes from kernel. +* +* The initial refcount is 1, and needs to be decremented to release the +* resources of the kmod_module. Since libkmod keeps track of all +* kmod_modules created, they are all released upon @ctx destruction too. Do +* not unref @ctx before all the desired operations with the returned list are +* completed. +* +* Returns: 0 on success or < 0 otherwise. It fails if any of the lookup +* methods failed, which is basically due to memory allocation failure. If +* module is not found, it still returns 0, but @mod is left untouched. +*/ +KMOD_EXPORT int kmod_module_new_from_name_lookup(struct kmod_ctx *ctx, +const char *modname, +struct kmod_module **mod) +{ +const lookup_func lookup[] = { + kmod_lookup_alias_from_moddep_file, + kmod_lookup_alias_from_builtin_file, + kmod_lookup_alias_from_kernel_builtin_file, + }; +char name_norm[PATH_MAX ]; +struct kmod_list *list =NULL ; +int err; + +if (ctx ==NULL || modname ==NULL || mod ==NULL ) +return -ENOENT ; + +modname_normalize (modname, name_norm,NULL ); + +DBG (ctx,"input modname=%s, normalized=%s\n" , modname, name_norm); + + err =__kmod_module_new_from_lookup (ctx, lookup,sizeof (lookup), + name_norm, &list); + +DBG (ctx,"lookup=%s found=%d\n" , name_norm, err >=0 && list); + +if (err >=0 && list !=NULL ) + *mod =kmod_module_get_module (list); + +kmod_module_unref_list (list); + +return err; +} +/** * kmod_module_unref_list: * @list: list of kmod modules
libkmod/libkmod.h +3 -0
@@ -129,6 +129,9 @@ int kmod_module_new_from_path(struct kmod_ctx *ctx, const char *path, struct kmod_module **mod);int kmod_module_new_from_lookup (struct kmod_ctx *ctx,const char *given_alias,struct kmod_list **list); +int kmod_module_new_from_name_lookup (struct kmod_ctx *ctx, +const char *modname, +struct kmod_module **mod);int kmod_module_new_from_loaded (struct kmod_ctx *ctx,struct kmod_list **list);
libkmod/libkmod.sym +1 -0
@@ -30,6 +30,7 @@ global: kmod_module_new_from_name; kmod_module_new_from_path; kmod_module_new_from_lookup; + kmod_module_new_from_name_lookup; kmod_module_new_from_loaded; kmod_module_ref; kmod_module_unref;
Signature · 1 line
2.35.1
PATCH 7/8 modinfo: Update help message with "modulename"
In reply to Lucas De Marchiman page correctly states the a module name can be used in place of a file name:
modinfo [-0] [-F field] [-k kernel] [modulename|filename...]
Update the help message accordingly.
--- tools/modinfo.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/modinfo.c +1 -1
@@ -337,7 +337,7 @@ static const struct option cmdopts[] = {static void help (void ) {printf ("Usage:\n" -"\t%s [options] filename [args]\n" +"\t%s [options] <modulename|filename> [args]\n" "Options:\n" "\t-a, --author Print only 'author'\n" "\t-d, --description Print only 'description'\n"
Signature · 1 line
2.35.1
PATCH 8/8 modinfo: Allow to force arg as module name
In reply to Lucas De MarchiIf the Linux kernel or userspace sets an alias with the same name as a module, they force the tools to use that. However in some situations it may be desired to query the module itself. Getting the module information through modinfo is one such situation. So, add a option to modinfo to explicitly instruct it to handle the argument as a module name.
Example, when trying to output information about the crc32 module that is builtin:
$ modinfo crc32 filename: /lib/modules/5.15.19-1-MANJARO/kernel/arch/x86/crypto/crc32-pclmul.ko.zst alias: crypto-crc32-pclmul alias: crc32-pclmul alias: crypto-crc32 alias: crc32 license: GPL author: Alexander Boyko <[email protected]> srcversion: B6B2FF9236731E69418A2E5 alias: cpu:type:x86,ven*fam*mod*:feature:*0081* depends: retpoline: Y intree: Y name: crc32_pclmul vermagic: 5.15.19-1-MANJARO SMP preempt mod_unload sig_id: PKCS#7 signer: Build time autogenerated kernel key sig_key: 77:FB:AA:BD:48:78:A4:C6:56:18:9A:7E:A6:F3:29:3E:C5:6B:E9:37 sig_hashalgo: sha512 signature: 30:65:02:31:00:B0:D4:49:9D:1D:F1:71:4C:3C:BB:70:B2:3E:46:5D: 38:5A:F1:00:95:FD:7A:96:C4:2C:24:35:A2:1B:0B:A8:1C:29:6F:02: 7A:68:EE:BA:A4:1C:01:4B:86:39:15:3E:66:02:30:7F:7A:66:5E:F2: 2F:98:73:3D:AD:96:66:81:8B:94:6E:F3:3F:44:0F:85:E1:73:3A:9E: F9:C4:BE:9B:88:02:BD:83:04:B9:2E:72:0B:93:BC:82:B6:A1:1B:6A: C2:ED:8C filename: /lib/modules/5.15.19-1-MANJARO/kernel/crypto/crc32_generic.ko.zst alias: crypto-crc32-generic alias: crc32-generic alias: crypto-crc32 alias: crc32 license: GPL description: CRC32 calculations wrapper for lib/crc32 author: Alexander Boyko <[email protected]> srcversion: F08036C38DDB06BCD1E6091 depends: retpoline: Y intree: Y name: crc32_generic vermagic: 5.15.19-1-MANJARO SMP preempt mod_unload sig_id: PKCS#7 signer: Build time autogenerated kernel key sig_key: 77:FB:AA:BD:48:78:A4:C6:56:18:9A:7E:A6:F3:29:3E:C5:6B:E9:37 sig_hashalgo: sha512 signature: 30:65:02:31:00:E3:9E:C8:80:15:0E:D7:74:96:B5:25:EA:32:F7:DF: E9:FC:3C:82:D9:B9:B9:37:C5:20:8D:06:31:02:62:B3:54:E8:DF:F2: 7E:E2:7C:A4:CF:49:17:CB:75:DF:2C:7A:2F:02:30:25:DE:7C:2A:2C: 97:3F:65:16:76:B3:71:FB:62:DB:8F:F3:33:65:77:98:F3:57:ED:D7: 87:78:FF:C2:04:55:70:00:10:63:1E:B2:FE:22:D8:E5:6D:5F:95:4E: 7D:2C:6B
That is because the Linux kernel exports "crc32" as an alias to those modules, besides being a module itself:
$ grep crc32 /lib/modules/$(uname -r)/modules.builtin kernel/lib/crc32.ko $ $ grep "alias crc32 " /lib/modules/$(uname -r)/modules.alias alias crc32 crc32_pclmul alias crc32 crc32_generic
With the new -m|--modname option it's possible to query the information about this (builtin):
$ modinfo --modname crc32 module explicitly: name: crc32 filename: (builtin) license: GPL file: lib/crc32 description: Various CRC32 calculations author: Matt Domsch <[email protected]> --- tools/modinfo.c | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-)
tools/modinfo.c +28 -2
@@ -293,6 +293,24 @@ static int modinfo_path_do(struct kmod_ctx *ctx, const char *path)return err; } +static int modinfo_name_do (struct kmod_ctx *ctx,const char *name) +{ +struct kmod_module *mod =NULL ; +int err; + + err =kmod_module_new_from_name_lookup (ctx, name, &mod); +if (err <0 || mod ==NULL ) { +ERR ("Module name %s not found.\n" , name); +return err <0 ? err : -ENOENT ; + } + + err =modinfo_do (mod); +kmod_module_unref (mod); + +return err; +} + +static int modinfo_alias_do (struct kmod_ctx *ctx,const char *alias) {struct kmod_list *l, *list =NULL ;
@@ -318,7 +336,7 @@ static int modinfo_alias_do(struct kmod_ctx *ctx, const char *alias)return err; } -static const char cmdopts_s[] ="adlpn0F:k:b:Vh" ; +static const char cmdopts_s[] ="adlpn0mF:k:b:Vh" ;static const struct option cmdopts[] = { {"author" , no_argument,0 ,'a' }, {"description" , no_argument,0 ,'d' },
@@ -326,6 +344,7 @@ static const struct option cmdopts[] = { {"parameters", no_argument, 0, 'p'}, {"filename", no_argument, 0, 'n'}, {"null", no_argument, 0, '0'}, + {"modname", no_argument, 0, 'm'}, {"field", required_argument, 0, 'F'}, {"set-version", required_argument, 0, 'k'}, {"basedir", required_argument, 0, 'b'},
@@ -345,6 +364,7 @@ static void help(void)"\t-p, --parameters Print only 'parm'\n" "\t-n, --filename Print only 'filename'\n" "\t-0, --null Use \\0 instead of \\n\n" +"\t-m, --modname Handle argument as module name instead of alias or filename\n" "\t-F, --field=FIELD Print only provided FIELD\n" "\t-k, --set-version=VERSION Use VERSION instead of `uname -r`\n" "\t-b, --basedir=DIR Use DIR as filesystem root for /lib/modules\n"
@@ -372,6 +392,7 @@ static int do_modinfo(int argc, char *argv[])const char *kversion =NULL ;const char *root =NULL ;const char *null_config =NULL ; +bool arg_is_modname = false;int i, err;for (;;) {
@@ -398,6 +419,9 @@ static int do_modinfo(int argc, char *argv[])case '0' : separator ='\0' ;break ; +case 'm' : + arg_is_modname = true; +break ;case 'F' : field = optarg;break ;
@@ -454,7 +478,9 @@ static int do_modinfo(int argc, char *argv[])const char *name = argv[i];int r; -if (is_module_filename (name)) +if (arg_is_modname) + r =modinfo_name_do (ctx, name); +else if (is_module_filename (name)) r =modinfo_path_do (ctx, name);else r =modinfo_alias_do (ctx, name);
Signature · 1 line
2.35.1
Re: [PATCH 8/8] modinfo: Allow to force arg as module name
Quoted 1 line · Lucas De Marchi
With the new -m|--modname option it's possible to query the information about this (builtin):
Query what information?
Quoted 2 lines
$ modinfo --modname crc32 module explicitly:
explicitly seems a bit odd, its not clear if the information below is because its a module or what. Maybe module details?
Quoted 2 lines
name: crc32
filename: (builtin)
You know a --is-built-in option might be nice while you're at it. Unless we already have something like this on another tool.
Other than that:
Luis
Re: [PATCH 8/8] modinfo: Allow to force arg as module name
Quoted 4 lines · Luis Chamberlain
Quoted 1 line · Lucas De Marchi
With the new -m|--modname option it's possible to query the information about this (builtin):
Query what information?
any of the fields exposed by the module or builtin. The phrase got truncated by wrong copy and paste. See below
Quoted 6 lines
Quoted 3 lines
$ modinfo --modname crc32 module explicitly:explicitly seems a bit odd, its not clear if the information below is because its a module or what. Maybe module details?
ugh... this was a wrong copy/paste in the commit message. That "module explicitely" is not in the output at all, and was rather part of the previous paragraph. It should had been something like:
With the new -m|--modname option it's possible to query the information about this (builtin) module explicitly:
$ ./build.new/tools/modinfo --modname crc32 name: crc32 filename: (builtin) license: GPL file: lib/crc32 description: Various CRC32 calculations author: Matt Domsch <[email protected]>
Quoted 5 lines
Quoted 2 lines
name: crc32
filename: (builtin)You know a --is-built-in option might be nice while you're at it. Unless we already have something like this on another tool.
to just check if a module is built-in or not? Not sure if it would be in modinfo or modprobe (since modprobe historically got the -R to resolve an alias).
Quoted 5 lines
Other than that:
Reviewed-by Luis Chamberlain <[email protected]>
thanks
Lucas De Marchi
Re: [PATCH 8/8] modinfo: Allow to force arg as module name
Quoted 6 lines · Lucas De Marchi
Quoted 3 lines · Luis Chamberlain
You know a --is-built-in option might be nice while you're at it. Unless we already have something like this on another tool.
to just check if a module is built-in or not?
Yup.
Quoted 2 lines
Not sure if it would be in modinfo or modprobe (since modprobe historically got the -R to resolve an alias).
You would know better. But having that would be wonderful.
Luis
Quoted 8 lines · Lucas De Marchi
First commits are some random fixes and refactors while preparing the code base for a new API:
kmod_module_new_from_name_lookup(). This new function is used by modinfo to allow handling the argument as a module name instead of alias or file. Main motivation is to allow to bypass aliases set by kernel or user configuration that would otherwise forbid to get the info from a particular module. Although for modules it's possible to just find the correspondent file, this is not possible for builtin modules.
For 1-7:
Luis