modprobe: semi-trivial code shrink

function                                             old     new   delta
build_dep                                            870     859     -11
already_loaded                                       134     112     -22
modprobe_main                                        449     368     -81
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/3 up/down: 0/-114)           Total: -114 bytes
This commit is contained in:
Denis Vlasenko 2008-08-06 00:51:43 +00:00
parent 2afd5ab62c
commit 9ddc0045ec

View File

@ -410,8 +410,7 @@ static struct dep_t *build_dep(void)
int continuation_line = 0; int continuation_line = 0;
int k_version; int k_version;
if (uname(&un)) uname(&un); /* never fails */
bb_error_msg_and_die("can't determine kernel version");
k_version = 0; k_version = 0;
if (un.release[0] == '2') { if (un.release[0] == '2') {
@ -426,13 +425,13 @@ static struct dep_t *build_dep(void)
/* Ok, that didn't work. Fall back to looking in /lib/modules */ /* Ok, that didn't work. Fall back to looking in /lib/modules */
f = fopen_for_read(CONFIG_DEFAULT_MODULES_DIR"/"CONFIG_DEFAULT_DEPMOD_FILE); f = fopen_for_read(CONFIG_DEFAULT_MODULES_DIR"/"CONFIG_DEFAULT_DEPMOD_FILE);
if (f == NULL) { if (f == NULL) {
bb_error_msg_and_die("cannot parse " CONFIG_DEFAULT_DEPMOD_FILE); bb_error_msg_and_die("cannot parse "CONFIG_DEFAULT_DEPMOD_FILE);
} }
} }
while (fgets(line_buffer, sizeof(line_buffer), f)) { while (fgets(line_buffer, sizeof(line_buffer), f)) {
int l = strlen(line_buffer); int l = strlen(line_buffer);
char *p = 0; char *p = NULL;
while (l > 0 && isspace(line_buffer[l-1])) { while (l > 0 && isspace(line_buffer[l-1])) {
line_buffer[l-1] = '\0'; line_buffer[l-1] = '\0';
@ -531,7 +530,7 @@ static struct dep_t *build_dep(void)
ext = 2; ext = 2;
/* Cope with blank lines */ /* Cope with blank lines */
if ((next-deps-ext+1) <= 0) if ((next - deps - ext + 1) <= 0)
continue; continue;
dep = xstrndup(deps, next - deps - ext + 1); dep = xstrndup(deps, next - deps - ext + 1);
@ -595,36 +594,33 @@ static struct dep_t *build_dep(void)
static int already_loaded(const char *name) static int already_loaded(const char *name)
{ {
FILE *f; FILE *f;
int ret = 0; int ret;
f = fopen_for_read("/proc/modules"); f = fopen_for_read("/proc/modules");
if (f == NULL) if (f == NULL)
return -1; return -1;
ret = 0;
while (fgets(line_buffer, sizeof(line_buffer), f)) { while (fgets(line_buffer, sizeof(line_buffer), f)) {
char *p; char *p = line_buffer;
const char *n = name;
p = strchr(line_buffer, ' '); while (1) {
if (p) { char cn = *n;
const char *n; char cp = *p;
if (cp == ' ' || cp == '\0') {
// Truncate buffer at first space and check for matches, with if (cn == '\0') {
// the idiosyncrasy that _ and - are interchangeable because the ret = 1; /* match! */
// 2.6 kernel does weird things.
*p = '\0';
for (p = line_buffer, n = name; ; p++, n++) {
if (*p != *n) {
if ((*p == '_' || *p == '-') && (*n == '_' || *n == '-'))
continue;
break;
}
// If we made it to the end, that's a match.
if (!*p) {
ret = 1;
goto done; goto done;
} }
break; /* no match on this line, take next one */
} }
if (cn == '-') cn = '_';
if (cp == '-') cp = '_';
if (cp != cn)
break; /* no match on this line, take next one */
n++;
p++;
} }
} }
done: done:
@ -648,7 +644,7 @@ static int mod_process(const struct mod_list_t *list, int do_insert)
* each time we allocate memory for argv. * each time we allocate memory for argv.
* But it is (quite) small amounts of memory that leak each * But it is (quite) small amounts of memory that leak each
* time a module is loaded, and it is reclaimed when modprobe * time a module is loaded, and it is reclaimed when modprobe
* exits anyway (even when standalone shell?). * exits anyway (even when standalone shell? Yes --vda).
* This could become a problem when loading a module with LOTS of * This could become a problem when loading a module with LOTS of
* dependencies, with LOTS of options for each dependencies, with * dependencies, with LOTS of options for each dependencies, with
* very little memory on the target... But in that case, the module * very little memory on the target... But in that case, the module
@ -862,23 +858,20 @@ static void check_dep(char *mod, struct mod_list_t **head, struct mod_list_t **t
} }
} }
static int mod_insert(char *mod, int argc, char **argv) static int mod_insert(char **argv)
{ {
struct mod_list_t *tail = NULL; struct mod_list_t *tail = NULL;
struct mod_list_t *head = NULL; struct mod_list_t *head = NULL;
char *modname = *argv++;
int rc; int rc;
// get dep list for module mod // get dep list for module mod
check_dep(mod, &head, &tail); check_dep(modname, &head, &tail);
rc = 1; rc = 1;
if (head && tail) { if (head && tail) {
if (argc) { while (*argv)
int i; head->m_options = append_option(head->m_options, *argv++);
// append module args
for (i = 0; i < argc; i++)
head->m_options = append_option(head->m_options, argv[i]);
}
// process tail ---> head // process tail ---> head
rc = mod_process(tail, 1); rc = mod_process(tail, 1);
@ -889,23 +882,23 @@ static int mod_insert(char *mod, int argc, char **argv)
* for example; or cold-plugging at boot time). Thus we shouldn't * for example; or cold-plugging at boot time). Thus we shouldn't
* fail if the module was loaded, and not by us. * fail if the module was loaded, and not by us.
*/ */
if (already_loaded(mod)) if (already_loaded(modname))
rc = 0; rc = 0;
} }
} }
return rc; return rc;
} }
static int mod_remove(char *mod) static int mod_remove(char *modname)
{ {
int rc;
static const struct mod_list_t rm_a_dummy = { "-a", NULL, NULL, NULL, NULL }; static const struct mod_list_t rm_a_dummy = { "-a", NULL, NULL, NULL, NULL };
int rc;
struct mod_list_t *head = NULL; struct mod_list_t *head = NULL;
struct mod_list_t *tail = NULL; struct mod_list_t *tail = NULL;
if (mod) if (modname)
check_dep(mod, &head, &tail); check_dep(modname, &head, &tail);
else // autoclean else // autoclean
head = tail = (struct mod_list_t*) &rm_a_dummy; head = tail = (struct mod_list_t*) &rm_a_dummy;
@ -916,17 +909,19 @@ static int mod_remove(char *mod)
} }
int modprobe_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int modprobe_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int modprobe_main(int argc, char **argv) int modprobe_main(int argc UNUSED_PARAM, char **argv)
{ {
int rc = EXIT_SUCCESS; int rc = EXIT_SUCCESS;
unsigned opt;
char *unused; char *unused;
opt_complementary = "q-v:v-q"; opt_complementary = "q-v:v-q";
getopt32(argv, MAIN_OPT_STR, &unused, &unused); opt = getopt32(argv, MAIN_OPT_STR, &unused, &unused);
argv += optind;
if (option_mask32 & (DUMP_CONF_EXIT | LIST_ALL)) if (opt & (DUMP_CONF_EXIT | LIST_ALL))
return EXIT_SUCCESS; return EXIT_SUCCESS;
if (option_mask32 & (RESTRICT_DIR | CONFIG_FILE)) if (opt & (RESTRICT_DIR | CONFIG_FILE))
bb_error_msg_and_die("-t and -C not supported"); bb_error_msg_and_die("-t and -C not supported");
depend = build_dep(); depend = build_dep();
@ -936,19 +931,19 @@ int modprobe_main(int argc, char **argv)
if (remove_opt) { if (remove_opt) {
do { do {
/* argv[optind] can be NULL here */ /* (*argv) can be NULL here */
if (mod_remove(argv[optind])) { if (mod_remove(*argv)) {
bb_error_msg("failed to %s module %s", "remove", bb_perror_msg("failed to %s module %s", "remove",
argv[optind]); *argv);
rc = EXIT_FAILURE; rc = EXIT_FAILURE;
} }
} while (++optind < argc); } while (*argv && *++argv);
} else { } else {
if (optind >= argc) if (!*argv)
bb_error_msg_and_die("no module or pattern provided"); bb_error_msg_and_die("no module or pattern provided");
if (mod_insert(argv[optind], argc - optind - 1, argv + optind + 1)) if (mod_insert(argv))
bb_error_msg_and_die("failed to %s module %s", "load", argv[optind]); bb_perror_msg_and_die("failed to %s module %s", "load", *argv);
} }
/* Here would be a good place to free up memory allocated during the dependencies build. */ /* Here would be a good place to free up memory allocated during the dependencies build. */