diff options
author | Rob Landley <rob@landley.net> | 2006-02-28 03:53:14 +0000 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2006-02-28 03:53:14 +0000 |
commit | e813ddba18d5f619fc406a7ff6551ec470552144 (patch) | |
tree | 2c1936d98b01cef5c1afb9d44b2d9432abd13425 | |
parent | 0817313255aad1d029da25b264e3ad27ecabb24f (diff) | |
download | busybox-w32-e813ddba18d5f619fc406a7ff6551ec470552144.tar.gz busybox-w32-e813ddba18d5f619fc406a7ff6551ec470552144.tar.bz2 busybox-w32-e813ddba18d5f619fc406a7ff6551ec470552144.zip |
Cleanup from Glenn McGrath.
-rw-r--r-- | networking/ifupdown.c | 64 |
1 files changed, 18 insertions, 46 deletions
diff --git a/networking/ifupdown.c b/networking/ifupdown.c index ad9b99b55..699fa7d06 100644 --- a/networking/ifupdown.c +++ b/networking/ifupdown.c | |||
@@ -53,13 +53,12 @@ | |||
53 | struct interface_defn_t; | 53 | struct interface_defn_t; |
54 | 54 | ||
55 | typedef int (execfn)(char *command); | 55 | typedef int (execfn)(char *command); |
56 | typedef int (command_set)(struct interface_defn_t *ifd, execfn *e); | ||
57 | 56 | ||
58 | struct method_t | 57 | struct method_t |
59 | { | 58 | { |
60 | char *name; | 59 | char *name; |
61 | command_set *up; | 60 | int (*up)(struct interface_defn_t *ifd, execfn *e); |
62 | command_set *down; | 61 | int (*down)(struct interface_defn_t *ifd, execfn *e); |
63 | }; | 62 | }; |
64 | 63 | ||
65 | struct address_family_t | 64 | struct address_family_t |
@@ -92,15 +91,10 @@ struct variable_t | |||
92 | 91 | ||
93 | struct interface_defn_t | 92 | struct interface_defn_t |
94 | { | 93 | { |
95 | struct interface_defn_t *prev; | ||
96 | struct interface_defn_t *next; | ||
97 | |||
98 | char *iface; | ||
99 | struct address_family_t *address_family; | 94 | struct address_family_t *address_family; |
100 | struct method_t *method; | 95 | struct method_t *method; |
101 | 96 | ||
102 | int automatic; | 97 | char *iface; |
103 | |||
104 | int max_options; | 98 | int max_options; |
105 | int n_options; | 99 | int n_options; |
106 | struct variable_t *option; | 100 | struct variable_t *option; |
@@ -645,17 +639,6 @@ static struct method_t *get_method(struct address_family_t *af, char *name) | |||
645 | return(NULL); | 639 | return(NULL); |
646 | } | 640 | } |
647 | 641 | ||
648 | static int duplicate_if(struct interface_defn_t *ifa, struct interface_defn_t *ifb) | ||
649 | { | ||
650 | if (strcmp(ifa->iface, ifb->iface) != 0) { | ||
651 | return(0); | ||
652 | } | ||
653 | if (ifa->address_family != ifb->address_family) { | ||
654 | return(0); | ||
655 | } | ||
656 | return(1); | ||
657 | } | ||
658 | |||
659 | static const llist_t *find_list_string(const llist_t *list, const char *string) | 642 | static const llist_t *find_list_string(const llist_t *list, const char *string) |
660 | { | 643 | { |
661 | while (list) { | 644 | while (list) { |
@@ -778,22 +761,19 @@ static struct interfaces_file_t *read_interfaces(const char *filename) | |||
778 | return NULL; | 761 | return NULL; |
779 | } | 762 | } |
780 | 763 | ||
781 | currif->automatic = 1; | ||
782 | currif->max_options = 0; | 764 | currif->max_options = 0; |
783 | currif->n_options = 0; | 765 | currif->n_options = 0; |
784 | currif->option = NULL; | 766 | currif->option = NULL; |
785 | 767 | ||
786 | { | 768 | { |
787 | struct interface_defn_t *tmp; | ||
788 | llist_t *iface_list; | 769 | llist_t *iface_list; |
789 | iface_list = defn->ifaces; | 770 | for (iface_list = defn->ifaces; iface_list; iface_list = iface_list->link) { |
790 | while (iface_list) { | 771 | struct interface_defn_t *tmp = (struct interface_defn_t *) iface_list->data; |
791 | tmp = (struct interface_defn_t *) iface_list->data; | 772 | if ((strcmp(tmp->iface, currif->iface) == 0) || |
792 | if (duplicate_if(tmp, currif)) { | 773 | (tmp->address_family == currif->address_family)) { |
793 | bb_error_msg("duplicate interface \"%s\"", tmp->iface); | 774 | bb_error_msg("duplicate interface \"%s\"", tmp->iface); |
794 | return NULL; | 775 | return NULL; |
795 | } | 776 | } |
796 | iface_list = iface_list->link; | ||
797 | } | 777 | } |
798 | 778 | ||
799 | defn->ifaces = llist_add_to_end(defn->ifaces, (char*)currif); | 779 | defn->ifaces = llist_add_to_end(defn->ifaces, (char*)currif); |
@@ -896,7 +876,7 @@ static struct interfaces_file_t *read_interfaces(const char *filename) | |||
896 | return defn; | 876 | return defn; |
897 | } | 877 | } |
898 | 878 | ||
899 | static char *setlocalenv(char *format, char *name, char *value) | 879 | static char *setlocalenv(char *format, const char *name, const char *value) |
900 | { | 880 | { |
901 | char *result; | 881 | char *result; |
902 | char *here; | 882 | char *here; |
@@ -922,7 +902,7 @@ static char *setlocalenv(char *format, char *name, char *value) | |||
922 | return result; | 902 | return result; |
923 | } | 903 | } |
924 | 904 | ||
925 | static void set_environ(struct interface_defn_t *iface, char *mode) | 905 | static void set_environ(struct interface_defn_t *iface, const char *mode) |
926 | { | 906 | { |
927 | char **environend; | 907 | char **environend; |
928 | int i; | 908 | int i; |
@@ -989,20 +969,20 @@ static int doit(char *str) | |||
989 | return (1); | 969 | return (1); |
990 | } | 970 | } |
991 | 971 | ||
992 | static int execute_all(struct interface_defn_t *ifd, execfn *exec, const char *opt) | 972 | static int execute_all(struct interface_defn_t *ifd, const char *opt) |
993 | { | 973 | { |
994 | int i; | 974 | int i; |
995 | char *buf; | 975 | char *buf; |
996 | for (i = 0; i < ifd->n_options; i++) { | 976 | for (i = 0; i < ifd->n_options; i++) { |
997 | if (strcmp(ifd->option[i].name, opt) == 0) { | 977 | if (strcmp(ifd->option[i].name, opt) == 0) { |
998 | if (!(*exec) (ifd->option[i].value)) { | 978 | if (!doit(ifd->option[i].value)) { |
999 | return 0; | 979 | return 0; |
1000 | } | 980 | } |
1001 | } | 981 | } |
1002 | } | 982 | } |
1003 | 983 | ||
1004 | buf = bb_xasprintf("run-parts /etc/network/if-%s.d", opt); | 984 | buf = bb_xasprintf("run-parts /etc/network/if-%s.d", opt); |
1005 | if ((*exec)(buf) != 1) { | 985 | if (doit(buf) != 1) { |
1006 | return 0; | 986 | return 0; |
1007 | } | 987 | } |
1008 | return 1; | 988 | return 1; |
@@ -1016,9 +996,9 @@ static int iface_up(struct interface_defn_t *iface) | |||
1016 | { | 996 | { |
1017 | if (!iface->method->up(iface,check)) return -1; | 997 | if (!iface->method->up(iface,check)) return -1; |
1018 | set_environ(iface, "start"); | 998 | set_environ(iface, "start"); |
1019 | if (!execute_all(iface, doit, "pre-up")) return 0; | 999 | if (!execute_all(iface, "pre-up")) return 0; |
1020 | if (!iface->method->up(iface, doit)) return 0; | 1000 | if (!iface->method->up(iface, doit)) return 0; |
1021 | if (!execute_all(iface, doit, "up")) return 0; | 1001 | if (!execute_all(iface, "up")) return 0; |
1022 | return 1; | 1002 | return 1; |
1023 | } | 1003 | } |
1024 | 1004 | ||
@@ -1026,9 +1006,9 @@ static int iface_down(struct interface_defn_t *iface) | |||
1026 | { | 1006 | { |
1027 | if (!iface->method->down(iface,check)) return -1; | 1007 | if (!iface->method->down(iface,check)) return -1; |
1028 | set_environ(iface, "stop"); | 1008 | set_environ(iface, "stop"); |
1029 | if (!execute_all(iface, doit, "down")) return 0; | 1009 | if (!execute_all(iface, "down")) return 0; |
1030 | if (!iface->method->down(iface, doit)) return 0; | 1010 | if (!iface->method->down(iface, doit)) return 0; |
1031 | if (!execute_all(iface, doit, "post-down")) return 0; | 1011 | if (!execute_all(iface, "post-down")) return 0; |
1032 | return 1; | 1012 | return 1; |
1033 | } | 1013 | } |
1034 | 1014 | ||
@@ -1373,7 +1353,7 @@ extern int ifupdown_main(int argc, char **argv) | |||
1373 | free(iface_state->data); | 1353 | free(iface_state->data); |
1374 | iface_state->data = newiface; | 1354 | iface_state->data = newiface; |
1375 | } | 1355 | } |
1376 | } else if (cmds == iface_down) { | 1356 | } else { |
1377 | /* Remove an interface from the linked list */ | 1357 | /* Remove an interface from the linked list */ |
1378 | if (iface_state) { | 1358 | if (iface_state) { |
1379 | /* This needs to be done better */ | 1359 | /* This needs to be done better */ |
@@ -1395,14 +1375,7 @@ extern int ifupdown_main(int argc, char **argv) | |||
1395 | if (!no_act) { | 1375 | if (!no_act) { |
1396 | FILE *state_fp = NULL; | 1376 | FILE *state_fp = NULL; |
1397 | 1377 | ||
1398 | state_fp = bb_xfopen(statefile, "a+"); | 1378 | state_fp = bb_xfopen(statefile, "w"); |
1399 | |||
1400 | if (ftruncate(fileno(state_fp), 0) < 0) { | ||
1401 | bb_error_msg_and_die("failed to truncate statefile %s: %s", statefile, strerror(errno)); | ||
1402 | } | ||
1403 | |||
1404 | rewind(state_fp); | ||
1405 | |||
1406 | while (state_list) { | 1379 | while (state_list) { |
1407 | if (state_list->data) { | 1380 | if (state_list->data) { |
1408 | fputs(state_list->data, state_fp); | 1381 | fputs(state_list->data, state_fp); |
@@ -1410,7 +1383,6 @@ extern int ifupdown_main(int argc, char **argv) | |||
1410 | } | 1383 | } |
1411 | state_list = state_list->link; | 1384 | state_list = state_list->link; |
1412 | } | 1385 | } |
1413 | fflush(state_fp); | ||
1414 | fclose(state_fp); | 1386 | fclose(state_fp); |
1415 | } | 1387 | } |
1416 | 1388 | ||