alicelinux

A lightweight musl + clang/llvm + libressl + busybox distro
git clone https://codeberg.org/emmett1/alicelinux
Log | Files | Refs | README | LICENSE

0006-modinfo-add-k-option-for-kernel-version.patch (4906B)


      1 From daed0a98b3d116d3fabb51340497273b5b9ce995 Mon Sep 17 00:00:00 2001
      2 From: Natanael Copa <ncopa@alpinelinux.org>
      3 Date: Thu, 28 Apr 2022 23:04:01 +0200
      4 Subject: [PATCH] modinfo: add -k option for kernel version
      5 
      6 It is useful to be able to specify kernel version when generating
      7 initramfs and similar for a kernel version that might not be the running
      8 one.
      9 
     10 bloatcheck on x86_64:
     11 
     12 function                                             old     new   delta
     13 packed_usage                                       26193   26218     +25
     14 modinfo_main                                         391     414     +23
     15 .rodata                                            80296   80298      +2
     16 ------------------------------------------------------------------------------
     17 (add/remove: 0/0 grow/shrink: 3/0 up/down: 50/0)               Total: 50
     18 bytes
     19    text    data     bss     dec     hex filename
     20    834606   14124    2008  850738   cfb32 busybox_old
     21    834657   14124    2008  850789   cfb65 busybox_unstripped
     22 
     23 Signed-off-by: Natanael Copa <ncopa@alpinelinux.org>
     24 ---
     25  modutils/modinfo.c | 30 ++++++++++++++++++------------
     26  1 file changed, 18 insertions(+), 12 deletions(-)
     27 
     28 diff --git a/modutils/modinfo.c b/modutils/modinfo.c
     29 index 0a86c3296..53bc02880 100644
     30 --- a/modutils/modinfo.c
     31 +++ b/modutils/modinfo.c
     32 @@ -38,17 +38,18 @@ static const char *const shortcuts[] ALIGN_PTR = {
     33  
     34  enum {
     35  	OPT_0 = (1 << 0), /* \0 as separator */
     36 -	OPT_F = (1 << 1), /* field name */
     37 +	OPT_k = (1 << 1), /* kernel version */
     38 +	OPT_F = (1 << 2), /* field name */
     39  	/* first bits are for -nadlp options, the rest are for
     40  	 * fields not selectable with "shortcut" options
     41  	 */
     42 -	OPT_n = (1 << 2),
     43 -	OPT_TAGS = ((1 << ARRAY_SIZE(shortcuts)) - 1) << 2,
     44 +	OPT_n = (1 << 3),
     45 +	OPT_TAGS = ((1 << ARRAY_SIZE(shortcuts)) - 1) << 3,
     46  };
     47  
     48  static void display(const char *data, const char *pattern)
     49  {
     50 -	int flag = option_mask32 >> 1; /* shift out -0 bit */
     51 +	int flag = option_mask32 >> 2; /* shift out -0 and -k bits */
     52  	if (flag & (flag-1)) {
     53  		/* more than one field to show: print "FIELD:" pfx */
     54  		int n = printf("%s:", pattern);
     55 @@ -82,7 +83,8 @@ static void modinfo(const char *path, const char *version,
     56  		}
     57  	}
     58  
     59 -	for (j = 1; (1<<j) & (OPT_TAGS|OPT_F); j++) {
     60 +	/* skip initial -0 and -k option bits */
     61 +	for (j = 2; (1<<j) & (OPT_TAGS|OPT_F); j++) {
     62  		const char *pattern;
     63  
     64  		if (!((1<<j) & tags))
     65 @@ -90,7 +92,7 @@ static void modinfo(const char *path, const char *version,
     66  
     67  		pattern = field;
     68  		if ((1<<j) & OPT_TAGS)
     69 -			pattern = shortcuts[j-2];
     70 +			pattern = shortcuts[j-3];
     71  
     72  		if (strcmp(pattern, shortcuts[0]) == 0) {
     73  			/* "-n" or "-F filename" */
     74 @@ -123,7 +125,7 @@ static void modinfo(const char *path, const char *version,
     75  }
     76  
     77  //usage:#define modinfo_trivial_usage
     78 -//usage:       "[-adlpn0] [-F keyword] MODULE"
     79 +//usage:       "[-adlpn0] [-F keyword] [-k kernel] MODULE"
     80  //usage:#define modinfo_full_usage "\n\n"
     81  //usage:       "	-a		Shortcut for '-F author'"
     82  //usage:     "\n	-d		Shortcut for '-F description'"
     83 @@ -131,6 +133,7 @@ static void modinfo(const char *path, const char *version,
     84  //usage:     "\n	-p		Shortcut for '-F parm'"
     85  ////usage:     "\n	-n		Shortcut for '-F filename'"
     86  //usage:     "\n	-F keyword	Keyword to look for"
     87 +//usage:     "\n	-k kernel	kernel version"
     88  //usage:     "\n	-0		NUL terminated output"
     89  //usage:#define modinfo_example_usage
     90  //usage:       "$ modinfo -F vermagic loop\n"
     91 @@ -139,6 +142,7 @@ int modinfo_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
     92  int modinfo_main(int argc UNUSED_PARAM, char **argv)
     93  {
     94  	const char *field;
     95 +	const char *kernel;
     96  	char name[MODULE_NAME_LEN];
     97  	struct utsname uts;
     98  	parser_t *parser;
     99 @@ -147,15 +151,17 @@ int modinfo_main(int argc UNUSED_PARAM, char **argv)
    100  	unsigned i;
    101  
    102  	field = NULL;
    103 -	opts = getopt32(argv, "^" "0F:nadlp" "\0" "-1"/*minimum one arg*/, &field);
    104 +	uname(&uts);
    105 +	kernel = uts.release;
    106 +	opts = getopt32(argv, "^" "0k:F:nadlp" "\0" "-1"/*minimum one arg*/, &kernel, &field);
    107  	/* If no field selected, show all */
    108  	if (!(opts & (OPT_TAGS|OPT_F)))
    109  		option_mask32 |= OPT_TAGS;
    110 +
    111  	argv += optind;
    112  
    113 -	uname(&uts);
    114  	parser = config_open2(
    115 -		xasprintf("%s/%s/%s", CONFIG_DEFAULT_MODULES_DIR, uts.release, CONFIG_DEFAULT_DEPMOD_FILE),
    116 +		xasprintf("%s/%s/%s", CONFIG_DEFAULT_MODULES_DIR, kernel, CONFIG_DEFAULT_DEPMOD_FILE),
    117  		xfopen_for_read
    118  	);
    119  
    120 @@ -167,7 +173,7 @@ int modinfo_main(int argc UNUSED_PARAM, char **argv)
    121  		filename2modname(bb_basename(tokens[0]), name);
    122  		for (i = 0; argv[i]; i++) {
    123  			if (fnmatch(argv[i], name, 0) == 0) {
    124 -				modinfo(tokens[0], uts.release, field);
    125 +				modinfo(tokens[0], kernel, field);
    126  				argv[i] = (char *) "";
    127  			}
    128  		}
    129 @@ -177,7 +183,7 @@ int modinfo_main(int argc UNUSED_PARAM, char **argv)
    130  
    131  	for (i = 0; argv[i]; i++) {
    132  		if (argv[i][0]) {
    133 -			modinfo(argv[i], uts.release, field);
    134 +			modinfo(argv[i], kernel, field);
    135  		}
    136  	}
    137