Bernhard Reutner-Fischer | 55e547e | 2008-05-26 12:01:49 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * depmod - generate modules.dep |
| 4 | * Copyright (c) 2008 Bernhard Fischer |
| 5 | * |
| 6 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
| 7 | */ |
| 8 | |
| 9 | #undef _GNU_SOURCE |
| 10 | #define _GNU_SOURCE |
| 11 | #include <libbb.h> |
| 12 | #include <sys/utsname.h> /* uname() */ |
| 13 | |
Bernhard Reutner-Fischer | cf18010 | 2008-05-26 15:12:01 +0000 | [diff] [blame] | 14 | /* |
| 15 | * Theory of operation: |
| 16 | * - iterate over all modules and record their full path |
| 17 | * - iterate over all modules looking for "depends=" entries |
| 18 | * for each depends, look through our list of full paths and emit if found |
| 19 | */ |
Bernhard Reutner-Fischer | 55e547e | 2008-05-26 12:01:49 +0000 | [diff] [blame] | 20 | struct globals { |
Bernhard Reutner-Fischer | 634b022 | 2008-05-28 14:20:20 +0000 | [diff] [blame] | 21 | llist_t *lst; /* modules without their corresponding extension */ |
| 22 | size_t moddir_base_len; /* length of the "-b basedir" */ |
Bernhard Reutner-Fischer | 55e547e | 2008-05-26 12:01:49 +0000 | [diff] [blame] | 23 | }; |
| 24 | #define G (*(struct globals*)&bb_common_bufsiz1) |
| 25 | /* We have to zero it out because of NOEXEC */ |
| 26 | #define INIT_G() memset(&G, 0, sizeof(G)) |
| 27 | |
| 28 | static int fill_lst(const char *modulename, struct stat ATTRIBUTE_UNUSED *sb, |
| 29 | void ATTRIBUTE_UNUSED *data, int ATTRIBUTE_UNUSED depth) |
| 30 | { |
Bernhard Reutner-Fischer | 634b022 | 2008-05-28 14:20:20 +0000 | [diff] [blame] | 31 | /* We get a file here. If the file does not have ".ko" but an |
| 32 | * intermittent dentry has, it's just their fault. |
| 33 | */ |
| 34 | if (strrstr(modulename, ".ko") != NULL) |
Bernhard Reutner-Fischer | c21d9c7 | 2008-05-28 10:35:51 +0000 | [diff] [blame] | 35 | llist_add_to(&G.lst, xstrdup(modulename + G.moddir_base_len)); |
Bernhard Reutner-Fischer | 55e547e | 2008-05-26 12:01:49 +0000 | [diff] [blame] | 36 | return TRUE; |
| 37 | } |
| 38 | |
Bernhard Reutner-Fischer | cf18010 | 2008-05-26 15:12:01 +0000 | [diff] [blame] | 39 | static int fileAction(const char *fname, struct stat *sb, |
Bernhard Reutner-Fischer | 55e547e | 2008-05-26 12:01:49 +0000 | [diff] [blame] | 40 | void *data, int ATTRIBUTE_UNUSED depth) |
| 41 | { |
Bernhard Reutner-Fischer | cf18010 | 2008-05-26 15:12:01 +0000 | [diff] [blame] | 42 | size_t len = sb->st_size; |
Bernhard Reutner-Fischer | 634b022 | 2008-05-28 14:20:20 +0000 | [diff] [blame] | 43 | void *the_module; |
| 44 | char *ptr; |
Bernhard Reutner-Fischer | cf18010 | 2008-05-26 15:12:01 +0000 | [diff] [blame] | 45 | int fd; |
| 46 | char *depends, *deps; |
Bernhard Reutner-Fischer | 55e547e | 2008-05-26 12:01:49 +0000 | [diff] [blame] | 47 | |
Bernhard Reutner-Fischer | 634b022 | 2008-05-28 14:20:20 +0000 | [diff] [blame] | 48 | if (strrstr(fname, ".ko") == NULL) /* not a module */ |
| 49 | goto skip; |
| 50 | |
Bernhard Reutner-Fischer | cf18010 | 2008-05-26 15:12:01 +0000 | [diff] [blame] | 51 | /*XXX: FIXME: does not handle compressed modules! |
| 52 | * There should be a function that looks at the extension and sets up |
| 53 | * open_transformer for us. |
| 54 | */ |
Bernhard Reutner-Fischer | cf18010 | 2008-05-26 15:12:01 +0000 | [diff] [blame] | 55 | fd = xopen(fname, O_RDONLY); |
| 56 | the_module = mmap(NULL, len, PROT_READ, MAP_SHARED |
| 57 | #if defined MAP_POPULATE |
| 58 | |MAP_POPULATE |
| 59 | #endif |
| 60 | , fd, 0); |
| 61 | close(fd); |
| 62 | if (the_module == MAP_FAILED) |
| 63 | bb_perror_msg_and_die("mmap"); |
| 64 | |
| 65 | ptr = the_module; |
| 66 | |
Bernhard Reutner-Fischer | c21d9c7 | 2008-05-28 10:35:51 +0000 | [diff] [blame] | 67 | fprintf((FILE*)data, "%s:", fname + G.moddir_base_len); |
| 68 | //bb_info_msg("fname='%s'", fname + G.moddir_base_len); |
Bernhard Reutner-Fischer | 55e547e | 2008-05-26 12:01:49 +0000 | [diff] [blame] | 69 | do { |
| 70 | /* search for a 'd' */ |
Bernhard Reutner-Fischer | 634b022 | 2008-05-28 14:20:20 +0000 | [diff] [blame] | 71 | ptr = memchr(ptr, 'd', len - (ptr - (char*)the_module)); |
Bernhard Reutner-Fischer | 55e547e | 2008-05-26 12:01:49 +0000 | [diff] [blame] | 72 | if (ptr == NULL) /* no d left, done */ |
Bernhard Reutner-Fischer | 634b022 | 2008-05-28 14:20:20 +0000 | [diff] [blame] | 73 | goto none; |
| 74 | if (strncmp(ptr, "depends=", sizeof("depends=")-1) == 0) |
Bernhard Reutner-Fischer | 55e547e | 2008-05-26 12:01:49 +0000 | [diff] [blame] | 75 | break; |
Bernhard Reutner-Fischer | c21d9c7 | 2008-05-28 10:35:51 +0000 | [diff] [blame] | 76 | ++ptr; |
Bernhard Reutner-Fischer | 55e547e | 2008-05-26 12:01:49 +0000 | [diff] [blame] | 77 | } while (1); |
Bernhard Reutner-Fischer | 6bb55cf | 2008-05-26 17:04:01 +0000 | [diff] [blame] | 78 | deps = depends = xstrdup (ptr + sizeof("depends=")-1); |
Bernhard Reutner-Fischer | 55e547e | 2008-05-26 12:01:49 +0000 | [diff] [blame] | 79 | //bb_info_msg(" depends='%s'", depends); |
Bernhard Reutner-Fischer | 634b022 | 2008-05-28 14:20:20 +0000 | [diff] [blame] | 80 | while (deps) { |
Bernhard Reutner-Fischer | 55e547e | 2008-05-26 12:01:49 +0000 | [diff] [blame] | 81 | llist_t * _lst = G.lst; |
Bernhard Reutner-Fischer | cf18010 | 2008-05-26 15:12:01 +0000 | [diff] [blame] | 82 | |
Bernhard Reutner-Fischer | 634b022 | 2008-05-28 14:20:20 +0000 | [diff] [blame] | 83 | ptr = strsep(&deps, ","); |
Bernhard Reutner-Fischer | 55e547e | 2008-05-26 12:01:49 +0000 | [diff] [blame] | 84 | while (_lst) { |
Bernhard Reutner-Fischer | 634b022 | 2008-05-28 14:20:20 +0000 | [diff] [blame] | 85 | /* Compare the recorded filenames ignoring ".ko*" at the end. */ |
| 86 | char *tmp = bb_get_last_path_component_nostrip(_lst->data); |
| 87 | if (strncmp(ptr, tmp, strrstr(tmp, ".ko") - tmp) == 0) |
Bernhard Reutner-Fischer | 55e547e | 2008-05-26 12:01:49 +0000 | [diff] [blame] | 88 | break; /* found it */ |
| 89 | _lst = _lst->link; |
| 90 | } |
Bernhard Reutner-Fischer | 634b022 | 2008-05-28 14:20:20 +0000 | [diff] [blame] | 91 | if (_lst) { |
| 92 | //bb_info_msg("[%s] -> '%s'", (char*)ptr, _lst->data); |
Bernhard Reutner-Fischer | 423465c | 2008-05-26 12:19:35 +0000 | [diff] [blame] | 93 | fprintf((FILE*)data, " %s", _lst->data); |
Bernhard Reutner-Fischer | 55e547e | 2008-05-26 12:01:49 +0000 | [diff] [blame] | 94 | } |
| 95 | } |
Bernhard Reutner-Fischer | cf18010 | 2008-05-26 15:12:01 +0000 | [diff] [blame] | 96 | free(depends); |
Bernhard Reutner-Fischer | c21d9c7 | 2008-05-28 10:35:51 +0000 | [diff] [blame] | 97 | fprintf((FILE*)data, "\n"); |
Bernhard Reutner-Fischer | 634b022 | 2008-05-28 14:20:20 +0000 | [diff] [blame] | 98 | none: |
Bernhard Reutner-Fischer | cf18010 | 2008-05-26 15:12:01 +0000 | [diff] [blame] | 99 | munmap(the_module, sb->st_size); |
Bernhard Reutner-Fischer | 634b022 | 2008-05-28 14:20:20 +0000 | [diff] [blame] | 100 | skip: |
Bernhard Reutner-Fischer | 55e547e | 2008-05-26 12:01:49 +0000 | [diff] [blame] | 101 | return TRUE; |
| 102 | } |
| 103 | |
| 104 | int depmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 105 | int depmod_main(int ATTRIBUTE_UNUSED argc, char **argv) |
| 106 | { |
Bernhard Reutner-Fischer | c21d9c7 | 2008-05-28 10:35:51 +0000 | [diff] [blame] | 107 | int ret; |
Bernhard Reutner-Fischer | 1ea2568 | 2008-05-26 21:33:05 +0000 | [diff] [blame] | 108 | char *moddir_base = NULL, *moddir, *system_map, *chp; |
Bernhard Reutner-Fischer | 6bb55cf | 2008-05-26 17:04:01 +0000 | [diff] [blame] | 109 | FILE *filedes = stdout; |
| 110 | enum { |
| 111 | ARG_a = (1<<0), /* All modules, ignore mods in argv */ |
| 112 | ARG_A = (1<<1), /* Only emit .ko that are newer than modules.dep file */ |
| 113 | ARG_b = (1<<2), /* not /lib/modules/$(uname -r)/ but this base-dir */ |
| 114 | ARG_e = (1<<3), /* with -F, print unresolved symbols */ |
| 115 | ARG_F = (1<<4), /* System.map that contains the symbols */ |
| 116 | ARG_n = (1<<5) /* dry-run, print to stdout only */ |
| 117 | }; |
Bernhard Reutner-Fischer | 55e547e | 2008-05-26 12:01:49 +0000 | [diff] [blame] | 118 | |
Bernhard Reutner-Fischer | 6bb55cf | 2008-05-26 17:04:01 +0000 | [diff] [blame] | 119 | getopt32(argv, "aAb:eF:n", &moddir_base, &system_map); |
| 120 | argv += optind; |
Bernhard Reutner-Fischer | c21d9c7 | 2008-05-28 10:35:51 +0000 | [diff] [blame] | 121 | |
| 122 | /* got a version to use? */ |
Bernhard Reutner-Fischer | 634b022 | 2008-05-28 14:20:20 +0000 | [diff] [blame] | 123 | if (*argv && (sscanf(*argv, "%d.%d.%d", &ret, &ret, &ret) == 3)) { |
Bernhard Reutner-Fischer | c21d9c7 | 2008-05-28 10:35:51 +0000 | [diff] [blame] | 124 | moddir = concat_path_file(CONFIG_DEFAULT_MODULES_DIR, *argv++); |
Bernhard Reutner-Fischer | 1ea2568 | 2008-05-26 21:33:05 +0000 | [diff] [blame] | 125 | } else { |
Bernhard Reutner-Fischer | 6bb55cf | 2008-05-26 17:04:01 +0000 | [diff] [blame] | 126 | struct utsname uts; |
| 127 | if (uname(&uts) < 0) |
| 128 | bb_simple_perror_msg_and_die("uname"); |
Bernhard Reutner-Fischer | c21d9c7 | 2008-05-28 10:35:51 +0000 | [diff] [blame] | 129 | moddir = concat_path_file(CONFIG_DEFAULT_MODULES_DIR, uts.release); |
Bernhard Reutner-Fischer | 6bb55cf | 2008-05-26 17:04:01 +0000 | [diff] [blame] | 130 | } |
Bernhard Reutner-Fischer | 634b022 | 2008-05-28 14:20:20 +0000 | [diff] [blame] | 131 | /* If no modules are given on the command-line, -a is on per default. */ |
Bernhard Reutner-Fischer | 1ea2568 | 2008-05-26 21:33:05 +0000 | [diff] [blame] | 132 | option_mask32 |= *argv == NULL; |
| 133 | |
Bernhard Reutner-Fischer | 1ea2568 | 2008-05-26 21:33:05 +0000 | [diff] [blame] | 134 | if (option_mask32 & ARG_b) { |
Bernhard Reutner-Fischer | c21d9c7 | 2008-05-28 10:35:51 +0000 | [diff] [blame] | 135 | G.moddir_base_len = strlen(moddir_base); |
| 136 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 137 | chp = moddir; |
| 138 | moddir = concat_path_file(moddir_base, moddir); |
| 139 | free (chp); |
| 140 | } else |
| 141 | moddir = concat_path_file(moddir_base, moddir); |
Bernhard Reutner-Fischer | 6bb55cf | 2008-05-26 17:04:01 +0000 | [diff] [blame] | 142 | } |
Bernhard Reutner-Fischer | 1ea2568 | 2008-05-26 21:33:05 +0000 | [diff] [blame] | 143 | |
| 144 | if (!(option_mask32 & ARG_n)) { /* --dry-run */ |
Bernhard Reutner-Fischer | b85fb69 | 2008-05-27 10:55:34 +0000 | [diff] [blame] | 145 | chp = concat_path_file(moddir, CONFIG_DEFAULT_DEPMOD_FILE); |
Bernhard Reutner-Fischer | 1ea2568 | 2008-05-26 21:33:05 +0000 | [diff] [blame] | 146 | filedes = xfopen(chp, "w"); |
| 147 | if (ENABLE_FEATURE_CLEAN_UP) |
| 148 | free(chp); |
| 149 | } |
Bernhard Reutner-Fischer | c21d9c7 | 2008-05-28 10:35:51 +0000 | [diff] [blame] | 150 | ret = EXIT_SUCCESS; |
Bernhard Reutner-Fischer | 634b022 | 2008-05-28 14:20:20 +0000 | [diff] [blame] | 151 | /* We have to do a full walk to collect all needed data. */ |
Bernhard Reutner-Fischer | 1ea2568 | 2008-05-26 21:33:05 +0000 | [diff] [blame] | 152 | if (!recursive_action(moddir, |
| 153 | ACTION_RECURSE, /* flags */ |
| 154 | fill_lst, /* file action */ |
| 155 | NULL, /* dir action */ |
| 156 | NULL, /* user data */ |
Bernhard Reutner-Fischer | 634b022 | 2008-05-28 14:20:20 +0000 | [diff] [blame] | 157 | 0)) { /* depth */ |
| 158 | if (ENABLE_FEATURE_CLEAN_UP) |
| 159 | ret = EXIT_FAILURE; |
| 160 | else |
| 161 | return EXIT_FAILURE; |
| 162 | } |
Bernhard Reutner-Fischer | 55e547e | 2008-05-26 12:01:49 +0000 | [diff] [blame] | 163 | do { |
Bernhard Reutner-Fischer | c21d9c7 | 2008-05-28 10:35:51 +0000 | [diff] [blame] | 164 | chp = option_mask32 & ARG_a ? moddir : *argv++; |
Bernhard Reutner-Fischer | 6bb55cf | 2008-05-26 17:04:01 +0000 | [diff] [blame] | 165 | |
| 166 | if (!recursive_action(chp, |
Bernhard Reutner-Fischer | 55e547e | 2008-05-26 12:01:49 +0000 | [diff] [blame] | 167 | ACTION_RECURSE, /* flags */ |
Bernhard Reutner-Fischer | 55e547e | 2008-05-26 12:01:49 +0000 | [diff] [blame] | 168 | fileAction, /* file action */ |
| 169 | NULL, /* dir action */ |
Bernhard Reutner-Fischer | 423465c | 2008-05-26 12:19:35 +0000 | [diff] [blame] | 170 | (void*)filedes, /* user data */ |
Bernhard Reutner-Fischer | 55e547e | 2008-05-26 12:01:49 +0000 | [diff] [blame] | 171 | 0)) { /* depth */ |
Bernhard Reutner-Fischer | c21d9c7 | 2008-05-28 10:35:51 +0000 | [diff] [blame] | 172 | ret = EXIT_FAILURE; |
Bernhard Reutner-Fischer | 55e547e | 2008-05-26 12:01:49 +0000 | [diff] [blame] | 173 | } |
Bernhard Reutner-Fischer | 1ea2568 | 2008-05-26 21:33:05 +0000 | [diff] [blame] | 174 | } while (!(option_mask32 & ARG_a) && *argv); |
Bernhard Reutner-Fischer | 55e547e | 2008-05-26 12:01:49 +0000 | [diff] [blame] | 175 | |
Bernhard Reutner-Fischer | dc5d7fe | 2008-05-26 13:30:41 +0000 | [diff] [blame] | 176 | if (ENABLE_FEATURE_CLEAN_UP) { |
Bernhard Reutner-Fischer | c21d9c7 | 2008-05-28 10:35:51 +0000 | [diff] [blame] | 177 | fclose_if_not_stdin(filedes); |
Bernhard Reutner-Fischer | dc5d7fe | 2008-05-26 13:30:41 +0000 | [diff] [blame] | 178 | llist_free(G.lst, free); |
Bernhard Reutner-Fischer | 1ea2568 | 2008-05-26 21:33:05 +0000 | [diff] [blame] | 179 | free(moddir); |
Bernhard Reutner-Fischer | dc5d7fe | 2008-05-26 13:30:41 +0000 | [diff] [blame] | 180 | } |
Bernhard Reutner-Fischer | c21d9c7 | 2008-05-28 10:35:51 +0000 | [diff] [blame] | 181 | return ret; |
Bernhard Reutner-Fischer | 55e547e | 2008-05-26 12:01:49 +0000 | [diff] [blame] | 182 | } |