diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2008-08-06 00:51:43 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2008-08-06 00:51:43 +0000 |
commit | 9ddc0045ec1ae648cfd510b95621d1f7a729a85c (patch) | |
tree | a37765197e6ce4e8b686277e7763f9b3ac9d7dac /modutils/modprobe.c | |
parent | 2afd5ab62ca5f2438a3953f37e738f73553595da (diff) | |
download | busybox-w32-9ddc0045ec1ae648cfd510b95621d1f7a729a85c.tar.gz busybox-w32-9ddc0045ec1ae648cfd510b95621d1f7a729a85c.tar.bz2 busybox-w32-9ddc0045ec1ae648cfd510b95621d1f7a729a85c.zip |
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
Diffstat (limited to 'modutils/modprobe.c')
-rw-r--r-- | modutils/modprobe.c | 99 |
1 files changed, 47 insertions, 52 deletions
diff --git a/modutils/modprobe.c b/modutils/modprobe.c index 01f8bb89b..412e71d16 100644 --- a/modutils/modprobe.c +++ b/modutils/modprobe.c | |||
@@ -410,8 +410,7 @@ static struct dep_t *build_dep(void) | |||
410 | int continuation_line = 0; | 410 | int continuation_line = 0; |
411 | int k_version; | 411 | int k_version; |
412 | 412 | ||
413 | if (uname(&un)) | 413 | uname(&un); /* never fails */ |
414 | bb_error_msg_and_die("can't determine kernel version"); | ||
415 | 414 | ||
416 | k_version = 0; | 415 | k_version = 0; |
417 | if (un.release[0] == '2') { | 416 | if (un.release[0] == '2') { |
@@ -426,13 +425,13 @@ static struct dep_t *build_dep(void) | |||
426 | /* Ok, that didn't work. Fall back to looking in /lib/modules */ | 425 | /* Ok, that didn't work. Fall back to looking in /lib/modules */ |
427 | f = fopen_for_read(CONFIG_DEFAULT_MODULES_DIR"/"CONFIG_DEFAULT_DEPMOD_FILE); | 426 | f = fopen_for_read(CONFIG_DEFAULT_MODULES_DIR"/"CONFIG_DEFAULT_DEPMOD_FILE); |
428 | if (f == NULL) { | 427 | if (f == NULL) { |
429 | bb_error_msg_and_die("cannot parse " CONFIG_DEFAULT_DEPMOD_FILE); | 428 | bb_error_msg_and_die("cannot parse "CONFIG_DEFAULT_DEPMOD_FILE); |
430 | } | 429 | } |
431 | } | 430 | } |
432 | 431 | ||
433 | while (fgets(line_buffer, sizeof(line_buffer), f)) { | 432 | while (fgets(line_buffer, sizeof(line_buffer), f)) { |
434 | int l = strlen(line_buffer); | 433 | int l = strlen(line_buffer); |
435 | char *p = 0; | 434 | char *p = NULL; |
436 | 435 | ||
437 | while (l > 0 && isspace(line_buffer[l-1])) { | 436 | while (l > 0 && isspace(line_buffer[l-1])) { |
438 | line_buffer[l-1] = '\0'; | 437 | line_buffer[l-1] = '\0'; |
@@ -531,7 +530,7 @@ static struct dep_t *build_dep(void) | |||
531 | ext = 2; | 530 | ext = 2; |
532 | 531 | ||
533 | /* Cope with blank lines */ | 532 | /* Cope with blank lines */ |
534 | if ((next-deps-ext+1) <= 0) | 533 | if ((next - deps - ext + 1) <= 0) |
535 | continue; | 534 | continue; |
536 | dep = xstrndup(deps, next - deps - ext + 1); | 535 | dep = xstrndup(deps, next - deps - ext + 1); |
537 | 536 | ||
@@ -595,36 +594,33 @@ static struct dep_t *build_dep(void) | |||
595 | static int already_loaded(const char *name) | 594 | static int already_loaded(const char *name) |
596 | { | 595 | { |
597 | FILE *f; | 596 | FILE *f; |
598 | int ret = 0; | 597 | int ret; |
599 | 598 | ||
600 | f = fopen_for_read("/proc/modules"); | 599 | f = fopen_for_read("/proc/modules"); |
601 | if (f == NULL) | 600 | if (f == NULL) |
602 | return -1; | 601 | return -1; |
603 | 602 | ||
603 | ret = 0; | ||
604 | while (fgets(line_buffer, sizeof(line_buffer), f)) { | 604 | while (fgets(line_buffer, sizeof(line_buffer), f)) { |
605 | char *p; | 605 | char *p = line_buffer; |
606 | 606 | const char *n = name; | |
607 | p = strchr(line_buffer, ' '); | 607 | |
608 | if (p) { | 608 | while (1) { |
609 | const char *n; | 609 | char cn = *n; |
610 | 610 | char cp = *p; | |
611 | // Truncate buffer at first space and check for matches, with | 611 | if (cp == ' ' || cp == '\0') { |
612 | // the idiosyncrasy that _ and - are interchangeable because the | 612 | if (cn == '\0') { |
613 | // 2.6 kernel does weird things. | 613 | ret = 1; /* match! */ |
614 | |||
615 | *p = '\0'; | ||
616 | for (p = line_buffer, n = name; ; p++, n++) { | ||
617 | if (*p != *n) { | ||
618 | if ((*p == '_' || *p == '-') && (*n == '_' || *n == '-')) | ||
619 | continue; | ||
620 | break; | ||
621 | } | ||
622 | // If we made it to the end, that's a match. | ||
623 | if (!*p) { | ||
624 | ret = 1; | ||
625 | goto done; | 614 | goto done; |
626 | } | 615 | } |
616 | break; /* no match on this line, take next one */ | ||
627 | } | 617 | } |
618 | if (cn == '-') cn = '_'; | ||
619 | if (cp == '-') cp = '_'; | ||
620 | if (cp != cn) | ||
621 | break; /* no match on this line, take next one */ | ||
622 | n++; | ||
623 | p++; | ||
628 | } | 624 | } |
629 | } | 625 | } |
630 | done: | 626 | done: |
@@ -648,7 +644,7 @@ static int mod_process(const struct mod_list_t *list, int do_insert) | |||
648 | * each time we allocate memory for argv. | 644 | * each time we allocate memory for argv. |
649 | * But it is (quite) small amounts of memory that leak each | 645 | * But it is (quite) small amounts of memory that leak each |
650 | * time a module is loaded, and it is reclaimed when modprobe | 646 | * time a module is loaded, and it is reclaimed when modprobe |
651 | * exits anyway (even when standalone shell?). | 647 | * exits anyway (even when standalone shell? Yes --vda). |
652 | * This could become a problem when loading a module with LOTS of | 648 | * This could become a problem when loading a module with LOTS of |
653 | * dependencies, with LOTS of options for each dependencies, with | 649 | * dependencies, with LOTS of options for each dependencies, with |
654 | * very little memory on the target... But in that case, the module | 650 | * 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 | |||
862 | } | 858 | } |
863 | } | 859 | } |
864 | 860 | ||
865 | static int mod_insert(char *mod, int argc, char **argv) | 861 | static int mod_insert(char **argv) |
866 | { | 862 | { |
867 | struct mod_list_t *tail = NULL; | 863 | struct mod_list_t *tail = NULL; |
868 | struct mod_list_t *head = NULL; | 864 | struct mod_list_t *head = NULL; |
865 | char *modname = *argv++; | ||
869 | int rc; | 866 | int rc; |
870 | 867 | ||
871 | // get dep list for module mod | 868 | // get dep list for module mod |
872 | check_dep(mod, &head, &tail); | 869 | check_dep(modname, &head, &tail); |
873 | 870 | ||
874 | rc = 1; | 871 | rc = 1; |
875 | if (head && tail) { | 872 | if (head && tail) { |
876 | if (argc) { | 873 | while (*argv) |
877 | int i; | 874 | head->m_options = append_option(head->m_options, *argv++); |
878 | // append module args | ||
879 | for (i = 0; i < argc; i++) | ||
880 | head->m_options = append_option(head->m_options, argv[i]); | ||
881 | } | ||
882 | 875 | ||
883 | // process tail ---> head | 876 | // process tail ---> head |
884 | rc = mod_process(tail, 1); | 877 | rc = mod_process(tail, 1); |
@@ -889,23 +882,23 @@ static int mod_insert(char *mod, int argc, char **argv) | |||
889 | * for example; or cold-plugging at boot time). Thus we shouldn't | 882 | * for example; or cold-plugging at boot time). Thus we shouldn't |
890 | * fail if the module was loaded, and not by us. | 883 | * fail if the module was loaded, and not by us. |
891 | */ | 884 | */ |
892 | if (already_loaded(mod)) | 885 | if (already_loaded(modname)) |
893 | rc = 0; | 886 | rc = 0; |
894 | } | 887 | } |
895 | } | 888 | } |
896 | return rc; | 889 | return rc; |
897 | } | 890 | } |
898 | 891 | ||
899 | static int mod_remove(char *mod) | 892 | static int mod_remove(char *modname) |
900 | { | 893 | { |
901 | int rc; | ||
902 | static const struct mod_list_t rm_a_dummy = { "-a", NULL, NULL, NULL, NULL }; | 894 | static const struct mod_list_t rm_a_dummy = { "-a", NULL, NULL, NULL, NULL }; |
903 | 895 | ||
896 | int rc; | ||
904 | struct mod_list_t *head = NULL; | 897 | struct mod_list_t *head = NULL; |
905 | struct mod_list_t *tail = NULL; | 898 | struct mod_list_t *tail = NULL; |
906 | 899 | ||
907 | if (mod) | 900 | if (modname) |
908 | check_dep(mod, &head, &tail); | 901 | check_dep(modname, &head, &tail); |
909 | else // autoclean | 902 | else // autoclean |
910 | head = tail = (struct mod_list_t*) &rm_a_dummy; | 903 | head = tail = (struct mod_list_t*) &rm_a_dummy; |
911 | 904 | ||
@@ -916,17 +909,19 @@ static int mod_remove(char *mod) | |||
916 | } | 909 | } |
917 | 910 | ||
918 | int modprobe_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 911 | int modprobe_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
919 | int modprobe_main(int argc, char **argv) | 912 | int modprobe_main(int argc UNUSED_PARAM, char **argv) |
920 | { | 913 | { |
921 | int rc = EXIT_SUCCESS; | 914 | int rc = EXIT_SUCCESS; |
915 | unsigned opt; | ||
922 | char *unused; | 916 | char *unused; |
923 | 917 | ||
924 | opt_complementary = "q-v:v-q"; | 918 | opt_complementary = "q-v:v-q"; |
925 | getopt32(argv, MAIN_OPT_STR, &unused, &unused); | 919 | opt = getopt32(argv, MAIN_OPT_STR, &unused, &unused); |
920 | argv += optind; | ||
926 | 921 | ||
927 | if (option_mask32 & (DUMP_CONF_EXIT | LIST_ALL)) | 922 | if (opt & (DUMP_CONF_EXIT | LIST_ALL)) |
928 | return EXIT_SUCCESS; | 923 | return EXIT_SUCCESS; |
929 | if (option_mask32 & (RESTRICT_DIR | CONFIG_FILE)) | 924 | if (opt & (RESTRICT_DIR | CONFIG_FILE)) |
930 | bb_error_msg_and_die("-t and -C not supported"); | 925 | bb_error_msg_and_die("-t and -C not supported"); |
931 | 926 | ||
932 | depend = build_dep(); | 927 | depend = build_dep(); |
@@ -936,19 +931,19 @@ int modprobe_main(int argc, char **argv) | |||
936 | 931 | ||
937 | if (remove_opt) { | 932 | if (remove_opt) { |
938 | do { | 933 | do { |
939 | /* argv[optind] can be NULL here */ | 934 | /* (*argv) can be NULL here */ |
940 | if (mod_remove(argv[optind])) { | 935 | if (mod_remove(*argv)) { |
941 | bb_error_msg("failed to %s module %s", "remove", | 936 | bb_perror_msg("failed to %s module %s", "remove", |
942 | argv[optind]); | 937 | *argv); |
943 | rc = EXIT_FAILURE; | 938 | rc = EXIT_FAILURE; |
944 | } | 939 | } |
945 | } while (++optind < argc); | 940 | } while (*argv && *++argv); |
946 | } else { | 941 | } else { |
947 | if (optind >= argc) | 942 | if (!*argv) |
948 | bb_error_msg_and_die("no module or pattern provided"); | 943 | bb_error_msg_and_die("no module or pattern provided"); |
949 | 944 | ||
950 | if (mod_insert(argv[optind], argc - optind - 1, argv + optind + 1)) | 945 | if (mod_insert(argv)) |
951 | bb_error_msg_and_die("failed to %s module %s", "load", argv[optind]); | 946 | bb_perror_msg_and_die("failed to %s module %s", "load", *argv); |
952 | } | 947 | } |
953 | 948 | ||
954 | /* Here would be a good place to free up memory allocated during the dependencies build. */ | 949 | /* Here would be a good place to free up memory allocated during the dependencies build. */ |