opendreambox 2.0
[opendreambox.git] / meta-opendreambox / recipes-core / busybox / busybox-1.19.4 / 0001-ifupdown-backport-from-master.patch
1 From 56871fb364862abc3812394f66a462b680fe6e84 Mon Sep 17 00:00:00 2001
2 From: Andreas Oberritter <obi@opendreambox.org>
3 Date: Thu, 12 Apr 2012 03:13:27 +0200
4 Subject: [PATCH] ifupdown: backport from master
5
6 Signed-off-by: Andreas Oberritter <obi@opendreambox.org>
7 ---
8  networking/ifupdown.c |   66 +++++++++++++++++++++++-------------------------
9  1 files changed, 32 insertions(+), 34 deletions(-)
10
11 diff --git a/networking/ifupdown.c b/networking/ifupdown.c
12 index 3820330..abfdf64 100644
13 --- a/networking/ifupdown.c
14 +++ b/networking/ifupdown.c
15 @@ -87,7 +87,6 @@ struct mapping_defn_t {
16  
17         char *script;
18  
19 -       int max_mappings;
20         int n_mappings;
21         char **mapping;
22  };
23 @@ -102,7 +101,6 @@ struct interface_defn_t {
24         const struct method_t *method;
25  
26         char *iface;
27 -       int max_options;
28         int n_options;
29         struct variable_t *option;
30  };
31 @@ -138,6 +136,14 @@ struct globals {
32  #define INIT_G() do { } while (0)
33  
34  
35 +static const char keywords_up_down[] ALIGN1 =
36 +       "up\0"
37 +       "down\0"
38 +       "pre-up\0"
39 +       "post-down\0"
40 +;
41 +
42 +
43  #if ENABLE_FEATURE_IFUPDOWN_IPV4 || ENABLE_FEATURE_IFUPDOWN_IPV6
44  
45  static void addstr(char **bufp, const char *str, size_t str_length)
46 @@ -395,11 +401,11 @@ static int FAST_FUNC static_up6(struct interface_defn_t *ifd, execfn *exec)
47         result = execute("ip addr add %address%/%netmask% dev %iface%[[ label %label%]]", ifd, exec);
48         result += execute("ip link set[[ mtu %mtu%]][[ addr %hwaddress%]] %iface% up", ifd, exec);
49         /* Was: "[[ ip ....%gateway% ]]". Removed extra spaces w/o checking */
50 -       result += execute("[[ip route add ::/0 via %gateway%]]", ifd, exec);
51 +       result += execute("[[ip route add ::/0 via %gateway%]][[ prio %metric%]]", ifd, exec);
52  # else
53         result = execute("ifconfig %iface%[[ media %media%]][[ hw %hwaddress%]][[ mtu %mtu%]] up", ifd, exec);
54         result += execute("ifconfig %iface% add %address%/%netmask%", ifd, exec);
55 -       result += execute("[[route -A inet6 add ::/0 gw %gateway%]]", ifd, exec);
56 +       result += execute("[[route -A inet6 add ::/0 gw %gateway%[[ metric %metric%]]]]", ifd, exec);
57  # endif
58         return ((result == 3) ? 3 : 0);
59  }
60 @@ -482,7 +488,7 @@ static int FAST_FUNC static_up(struct interface_defn_t *ifd, execfn *exec)
61         result = execute("ip addr add %address%/%bnmask%[[ broadcast %broadcast%]] "
62                         "dev %iface%[[ peer %pointopoint%]][[ label %label%]]", ifd, exec);
63         result += execute("ip link set[[ mtu %mtu%]][[ addr %hwaddress%]] %iface% up", ifd, exec);
64 -       result += execute("[[ip route add default via %gateway% dev %iface%]]", ifd, exec);
65 +       result += execute("[[ip route add default via %gateway% dev %iface%[[ prio %metric%]]]]", ifd, exec);
66         return ((result == 3) ? 3 : 0);
67  # else
68         /* ifconfig said to set iface up before it processes hw %hwaddress%,
69 @@ -492,7 +498,7 @@ static int FAST_FUNC static_up(struct interface_defn_t *ifd, execfn *exec)
70         result += execute("ifconfig %iface% %address% netmask %netmask%"
71                                 "[[ broadcast %broadcast%]][[ pointopoint %pointopoint%]] ",
72                                 ifd, exec);
73 -       result += execute("[[route add default gw %gateway% %iface%]]", ifd, exec);
74 +       result += execute("[[route add default gw %gateway%[[ metric %metric%]] %iface%]]", ifd, exec);
75         return ((result == 3) ? 3 : 0);
76  # endif
77  }
78 @@ -803,7 +809,6 @@ static struct interfaces_file_t *read_interfaces(const char *filename)
79                                 currmap->match = xrealloc_vector(currmap->match, 4, currmap->n_matches);
80                                 currmap->match[currmap->n_matches++] = xstrdup(first_word);
81                         }
82 -                       /*currmap->max_mappings = 0; - done by xzalloc */
83                         /*currmap->n_mappings = 0;*/
84                         /*currmap->mapping = NULL;*/
85                         /*currmap->script = NULL;*/
86 @@ -888,23 +893,21 @@ static struct interfaces_file_t *read_interfaces(const char *filename)
87                                 if (rest_of_line[0] == '\0')
88                                         bb_error_msg_and_die("option with empty value \"%s\"", buf);
89  
90 -                               if (strcmp(first_word, "up") != 0
91 -                                && strcmp(first_word, "down") != 0
92 -                                && strcmp(first_word, "pre-up") != 0
93 -                                && strcmp(first_word, "post-down") != 0
94 -                               ) {
95 +                               if (strcmp(first_word, "post-up") == 0)
96 +                                       strcpy(first_word, "up");
97 +                               else if (strcmp(first_word, "pre-down") == 0)
98 +                                       strcpy(first_word, "down");
99 +
100 +                               /* If not one of "up", "down",... words... */
101 +                               if (index_in_strings(keywords_up_down, first_word) < 0) {
102                                         int i;
103                                         for (i = 0; i < currif->n_options; i++) {
104                                                 if (strcmp(currif->option[i].name, first_word) == 0)
105                                                         bb_error_msg_and_die("duplicate option \"%s\"", buf);
106                                         }
107                                 }
108 -                               if (currif->n_options >= currif->max_options) {
109 -                                       currif->max_options += 10;
110 -                                       currif->option = xrealloc(currif->option,
111 -                                               sizeof(*currif->option) * currif->max_options);
112 -                               }
113                                 debug_noise("\t%s=%s\n", first_word, rest_of_line);
114 +                               currif->option = xrealloc_vector(currif->option, 4, currif->n_options);
115                                 currif->option[currif->n_options].name = xstrdup(first_word);
116                                 currif->option[currif->n_options].value = xstrdup(rest_of_line);
117                                 currif->n_options++;
118 @@ -916,11 +919,7 @@ static struct interfaces_file_t *read_interfaces(const char *filename)
119                                                 bb_error_msg_and_die("duplicate script in mapping \"%s\"", buf);
120                                         currmap->script = xstrdup(next_word(&rest_of_line));
121                                 } else if (strcmp(first_word, "map") == 0) {
122 -                                       if (currmap->n_mappings >= currmap->max_mappings) {
123 -                                               currmap->max_mappings = currmap->max_mappings * 2 + 1;
124 -                                               currmap->mapping = xrealloc(currmap->mapping,
125 -                                                       sizeof(char *) * currmap->max_mappings);
126 -                                       }
127 +                                       currmap->mapping = xrealloc_vector(currmap->mapping, 2, currmap->n_mappings);
128                                         currmap->mapping[currmap->n_mappings] = xstrdup(next_word(&rest_of_line));
129                                         currmap->n_mappings++;
130                                 } else {
131 @@ -967,7 +966,7 @@ static char *setlocalenv(const char *format, const char *name, const char *value
132         return result;
133  }
134  
135 -static void set_environ(struct interface_defn_t *iface, const char *mode)
136 +static void set_environ(struct interface_defn_t *iface, const char *mode, const char *opt)
137  {
138         int i;
139         char **pp;
140 @@ -980,15 +979,11 @@ static void set_environ(struct interface_defn_t *iface, const char *mode)
141         }
142  
143         /* note: last element will stay NULL: */
144 -       G.my_environ = xzalloc(sizeof(char *) * (iface->n_options + 6));
145 +       G.my_environ = xzalloc(sizeof(char *) * (iface->n_options + 7));
146         pp = G.my_environ;
147  
148         for (i = 0; i < iface->n_options; i++) {
149 -               if (strcmp(iface->option[i].name, "up") == 0
150 -                || strcmp(iface->option[i].name, "down") == 0
151 -                || strcmp(iface->option[i].name, "pre-up") == 0
152 -                || strcmp(iface->option[i].name, "post-down") == 0
153 -               ) {
154 +               if (index_in_strings(keywords_up_down, iface->option[i].name) >= 0) {
155                         continue;
156                 }
157                 *pp++ = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
158 @@ -998,6 +993,7 @@ static void set_environ(struct interface_defn_t *iface, const char *mode)
159         *pp++ = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
160         *pp++ = setlocalenv("%s=%s", "METHOD", iface->method->name);
161         *pp++ = setlocalenv("%s=%s", "MODE", mode);
162 +       *pp++ = setlocalenv("%s=%s", "PHASE", opt);
163         if (G.startup_PATH)
164                 *pp++ = setlocalenv("%s=%s", "PATH", G.startup_PATH);
165  }
166 @@ -1052,9 +1048,10 @@ static int check(char *str)
167  static int iface_up(struct interface_defn_t *iface)
168  {
169         if (!iface->method->up(iface, check)) return -1;
170 -       set_environ(iface, "start");
171 +       set_environ(iface, "start", "pre-up");
172         if (!execute_all(iface, "pre-up")) return 0;
173         if (!iface->method->up(iface, doit)) return 0;
174 +       set_environ(iface, "start", "post-up");
175         if (!execute_all(iface, "up")) return 0;
176         return 1;
177  }
178 @@ -1062,9 +1059,10 @@ static int iface_up(struct interface_defn_t *iface)
179  static int iface_down(struct interface_defn_t *iface)
180  {
181         if (!iface->method->down(iface,check)) return -1;
182 -       set_environ(iface, "stop");
183 +       set_environ(iface, "stop", "pre-down");
184         if (!execute_all(iface, "down")) return 0;
185         if (!iface->method->down(iface, doit)) return 0;
186 +       set_environ(iface, "stop", "post-down");
187         if (!execute_all(iface, "post-down")) return 0;
188         return 1;
189  }
190 @@ -1317,9 +1315,9 @@ int ifupdown_main(int argc UNUSED_PARAM, char **argv)
191                         llist_t *state_list = read_iface_state();
192                         llist_t *iface_state = find_iface_state(state_list, iface);
193  
194 -                       if (cmds == iface_up) {
195 -                               char * const newiface = xasprintf("%s=%s", iface, liface);
196 -                               if (iface_state == NULL) {
197 +                       if (cmds == iface_up && !any_failures) {
198 +                               char *newiface = xasprintf("%s=%s", iface, liface);
199 +                               if (!iface_state) {
200                                         llist_add_to_end(&state_list, newiface);
201                                 } else {
202                                         free(iface_state->data);
203 -- 
204 1.7.5.4
205