diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2006-09-26 20:35:30 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2006-09-26 20:35:30 +0000 |
commit | 0534125ccc727ce3e44ea10619f8c4ab1f1a96d8 (patch) | |
tree | 76737ad154169c442c5a725f3cc9c313d0a3a8fc | |
parent | e175ff252f9abb7267000571207c9d612728e1b9 (diff) | |
download | busybox-w32-0534125ccc727ce3e44ea10619f8c4ab1f1a96d8.tar.gz busybox-w32-0534125ccc727ce3e44ea10619f8c4ab1f1a96d8.tar.bz2 busybox-w32-0534125ccc727ce3e44ea10619f8c4ab1f1a96d8.zip |
ifupdown: style cleanup, no code changes
-rw-r--r-- | networking/ifupdown.c | 526 |
1 files changed, 260 insertions, 266 deletions
diff --git a/networking/ifupdown.c b/networking/ifupdown.c index 88bdc521f..9841b5903 100644 --- a/networking/ifupdown.c +++ b/networking/ifupdown.c | |||
@@ -15,8 +15,6 @@ | |||
15 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. | 15 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
16 | */ | 16 | */ |
17 | 17 | ||
18 | /* TODO: standardise execute() return codes to return 0 for success and 1 for failure */ | ||
19 | |||
20 | #include "busybox.h" | 18 | #include "busybox.h" |
21 | #include <sys/utsname.h> | 19 | #include <sys/utsname.h> |
22 | #include <fnmatch.h> | 20 | #include <fnmatch.h> |
@@ -49,7 +47,7 @@ struct address_family_t | |||
49 | { | 47 | { |
50 | char *name; | 48 | char *name; |
51 | int n_methods; | 49 | int n_methods; |
52 | struct method_t *method; | 50 | const struct method_t *method; |
53 | }; | 51 | }; |
54 | 52 | ||
55 | struct mapping_defn_t | 53 | struct mapping_defn_t |
@@ -75,8 +73,8 @@ struct variable_t | |||
75 | 73 | ||
76 | struct interface_defn_t | 74 | struct interface_defn_t |
77 | { | 75 | { |
78 | struct address_family_t *address_family; | 76 | const struct address_family_t *address_family; |
79 | struct method_t *method; | 77 | const struct method_t *method; |
80 | 78 | ||
81 | char *iface; | 79 | char *iface; |
82 | int max_options; | 80 | int max_options; |
@@ -106,7 +104,7 @@ enum { | |||
106 | #define FORCE (option_mask & OPT_force) | 104 | #define FORCE (option_mask & OPT_force) |
107 | #define NO_MAPPINGS (option_mask & OPT_no_mappings) | 105 | #define NO_MAPPINGS (option_mask & OPT_no_mappings) |
108 | 106 | ||
109 | static char **__myenviron = NULL; | 107 | static char **__myenviron; |
110 | 108 | ||
111 | #if ENABLE_FEATURE_IFUPDOWN_IPV4 || ENABLE_FEATURE_IFUPDOWN_IPV6 | 109 | #if ENABLE_FEATURE_IFUPDOWN_IPV4 || ENABLE_FEATURE_IFUPDOWN_IPV6 |
112 | 110 | ||
@@ -117,7 +115,7 @@ static unsigned int count_bits(unsigned int a) | |||
117 | unsigned int result; | 115 | unsigned int result; |
118 | result = (a & 0x55) + ((a >> 1) & 0x55); | 116 | result = (a & 0x55) + ((a >> 1) & 0x55); |
119 | result = (result & 0x33) + ((result >> 2) & 0x33); | 117 | result = (result & 0x33) + ((result >> 2) & 0x33); |
120 | return((result & 0x0F) + ((result >> 4) & 0x0F)); | 118 | return ((result & 0x0F) + ((result >> 4) & 0x0F)); |
121 | } | 119 | } |
122 | 120 | ||
123 | static int count_netmask_bits(char *dotted_quad) | 121 | static int count_netmask_bits(char *dotted_quad) |
@@ -130,7 +128,7 @@ static int count_netmask_bits(char *dotted_quad) | |||
130 | result += count_bits(b); | 128 | result += count_bits(b); |
131 | result += count_bits(c); | 129 | result += count_bits(c); |
132 | result += count_bits(d); | 130 | result += count_bits(d); |
133 | return ((int)result); | 131 | return (int)result; |
134 | } | 132 | } |
135 | #endif | 133 | #endif |
136 | 134 | ||
@@ -156,9 +154,9 @@ static int strncmpz(char *l, char *r, size_t llen) | |||
156 | int i = strncmp(l, r, llen); | 154 | int i = strncmp(l, r, llen); |
157 | 155 | ||
158 | if (i == 0) { | 156 | if (i == 0) { |
159 | return(-r[llen]); | 157 | return -r[llen]; |
160 | } else { | 158 | } else { |
161 | return(i); | 159 | return i; |
162 | } | 160 | } |
163 | } | 161 | } |
164 | 162 | ||
@@ -175,18 +173,18 @@ static char *get_var(char *id, size_t idlen, struct interface_defn_t *ifd) | |||
175 | if (result) { | 173 | if (result) { |
176 | *result=0; | 174 | *result=0; |
177 | } | 175 | } |
178 | return( label_buf); | 176 | return label_buf; |
179 | } else if (strncmpz(id, "label", idlen) == 0) { | 177 | } else if (strncmpz(id, "label", idlen) == 0) { |
180 | return (ifd->iface); | 178 | return ifd->iface; |
181 | } else { | 179 | } else { |
182 | for (i = 0; i < ifd->n_options; i++) { | 180 | for (i = 0; i < ifd->n_options; i++) { |
183 | if (strncmpz(id, ifd->option[i].name, idlen) == 0) { | 181 | if (strncmpz(id, ifd->option[i].name, idlen) == 0) { |
184 | return (ifd->option[i].value); | 182 | return ifd->option[i].value; |
185 | } | 183 | } |
186 | } | 184 | } |
187 | } | 185 | } |
188 | 186 | ||
189 | return(NULL); | 187 | return NULL; |
190 | } | 188 | } |
191 | 189 | ||
192 | static char *parse(char *command, struct interface_defn_t *ifd) | 190 | static char *parse(char *command, struct interface_defn_t *ifd) |
@@ -200,101 +198,101 @@ static char *parse(char *command, struct interface_defn_t *ifd) | |||
200 | 198 | ||
201 | while (*command) { | 199 | while (*command) { |
202 | switch (*command) { | 200 | switch (*command) { |
203 | 201 | default: | |
204 | default: | 202 | addstr(&result, &len, &pos, command, 1); |
203 | command++; | ||
204 | break; | ||
205 | case '\\': | ||
206 | if (command[1]) { | ||
207 | addstr(&result, &len, &pos, command + 1, 1); | ||
208 | command += 2; | ||
209 | } else { | ||
205 | addstr(&result, &len, &pos, command, 1); | 210 | addstr(&result, &len, &pos, command, 1); |
206 | command++; | 211 | command++; |
207 | break; | 212 | } |
208 | case '\\': | 213 | break; |
209 | if (command[1]) { | 214 | case '[': |
210 | addstr(&result, &len, &pos, command + 1, 1); | 215 | if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) { |
211 | command += 2; | 216 | old_pos[opt_depth] = pos; |
212 | } else { | 217 | okay[opt_depth] = 1; |
213 | addstr(&result, &len, &pos, command, 1); | 218 | opt_depth++; |
214 | command++; | 219 | command += 2; |
215 | } | 220 | } else { |
216 | break; | 221 | addstr(&result, &len, &pos, "[", 1); |
217 | case '[': | 222 | command++; |
218 | if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) { | 223 | } |
219 | old_pos[opt_depth] = pos; | 224 | break; |
220 | okay[opt_depth] = 1; | 225 | case ']': |
221 | opt_depth++; | 226 | if (command[1] == ']' && opt_depth > 1) { |
222 | command += 2; | 227 | opt_depth--; |
223 | } else { | 228 | if (!okay[opt_depth]) { |
224 | addstr(&result, &len, &pos, "[", 1); | 229 | pos = old_pos[opt_depth]; |
225 | command++; | 230 | result[pos] = '\0'; |
226 | } | 231 | } |
227 | break; | 232 | command += 2; |
228 | case ']': | 233 | } else { |
229 | if (command[1] == ']' && opt_depth > 1) { | 234 | addstr(&result, &len, &pos, "]", 1); |
230 | opt_depth--; | 235 | command++; |
231 | if (!okay[opt_depth]) { | 236 | } |
232 | pos = old_pos[opt_depth]; | 237 | break; |
233 | result[pos] = '\0'; | 238 | case '%': |
234 | } | 239 | { |
235 | command += 2; | 240 | char *nextpercent; |
236 | } else { | 241 | char *varvalue; |
237 | addstr(&result, &len, &pos, "]", 1); | 242 | |
238 | command++; | 243 | command++; |
244 | nextpercent = strchr(command, '%'); | ||
245 | if (!nextpercent) { | ||
246 | errno = EUNBALPER; | ||
247 | free(result); | ||
248 | return NULL; | ||
239 | } | 249 | } |
240 | break; | ||
241 | case '%': | ||
242 | { | ||
243 | char *nextpercent; | ||
244 | char *varvalue; | ||
245 | |||
246 | command++; | ||
247 | nextpercent = strchr(command, '%'); | ||
248 | if (!nextpercent) { | ||
249 | errno = EUNBALPER; | ||
250 | free(result); | ||
251 | return (NULL); | ||
252 | } | ||
253 | 250 | ||
254 | varvalue = get_var(command, nextpercent - command, ifd); | 251 | varvalue = get_var(command, nextpercent - command, ifd); |
255 | 252 | ||
256 | if (varvalue) { | 253 | if (varvalue) { |
257 | addstr(&result, &len, &pos, varvalue, strlen(varvalue)); | 254 | addstr(&result, &len, &pos, varvalue, strlen(varvalue)); |
258 | } else { | 255 | } else { |
259 | #ifdef CONFIG_FEATURE_IFUPDOWN_IP | 256 | #ifdef CONFIG_FEATURE_IFUPDOWN_IP |
260 | /* Sigh... Add a special case for 'ip' to convert from | 257 | /* Sigh... Add a special case for 'ip' to convert from |
261 | * dotted quad to bit count style netmasks. */ | 258 | * dotted quad to bit count style netmasks. */ |
262 | if (strncmp(command, "bnmask", 6)==0) { | 259 | if (strncmp(command, "bnmask", 6)==0) { |
263 | int res; | 260 | int res; |
264 | varvalue = get_var("netmask", 7, ifd); | 261 | varvalue = get_var("netmask", 7, ifd); |
265 | if (varvalue && (res=count_netmask_bits(varvalue)) > 0) { | 262 | if (varvalue && (res=count_netmask_bits(varvalue)) > 0) { |
266 | char argument[255]; | 263 | char argument[255]; |
267 | sprintf(argument, "%d", res); | 264 | sprintf(argument, "%d", res); |
268 | addstr(&result, &len, &pos, argument, strlen(argument)); | 265 | addstr(&result, &len, &pos, argument, strlen(argument)); |
269 | command = nextpercent + 1; | 266 | command = nextpercent + 1; |
270 | break; | 267 | break; |
271 | } | ||
272 | } | 268 | } |
273 | #endif | ||
274 | okay[opt_depth - 1] = 0; | ||
275 | } | 269 | } |
276 | 270 | #endif | |
277 | command = nextpercent + 1; | 271 | okay[opt_depth - 1] = 0; |
278 | } | 272 | } |
279 | break; | 273 | |
274 | command = nextpercent + 1; | ||
275 | } | ||
276 | break; | ||
280 | } | 277 | } |
281 | } | 278 | } |
282 | 279 | ||
283 | if (opt_depth > 1) { | 280 | if (opt_depth > 1) { |
284 | errno = EUNBALBRACK; | 281 | errno = EUNBALBRACK; |
285 | free(result); | 282 | free(result); |
286 | return(NULL); | 283 | return NULL; |
287 | } | 284 | } |
288 | 285 | ||
289 | if (!okay[0]) { | 286 | if (!okay[0]) { |
290 | errno = EUNDEFVAR; | 287 | errno = EUNDEFVAR; |
291 | free(result); | 288 | free(result); |
292 | return(NULL); | 289 | return NULL; |
293 | } | 290 | } |
294 | 291 | ||
295 | return(result); | 292 | return result; |
296 | } | 293 | } |
297 | 294 | ||
295 | /* execute() returns 1 for success and 0 for failure */ | ||
298 | static int execute(char *command, struct interface_defn_t *ifd, execfn *exec) | 296 | static int execute(char *command, struct interface_defn_t *ifd, execfn *exec) |
299 | { | 297 | { |
300 | char *out; | 298 | char *out; |
@@ -302,15 +300,15 @@ static int execute(char *command, struct interface_defn_t *ifd, execfn *exec) | |||
302 | 300 | ||
303 | out = parse(command, ifd); | 301 | out = parse(command, ifd); |
304 | if (!out) { | 302 | if (!out) { |
305 | return(0); | 303 | return 0; |
306 | } | 304 | } |
307 | ret = (*exec) (out); | 305 | ret = (*exec)(out); |
308 | 306 | ||
309 | free(out); | 307 | free(out); |
310 | if (ret != 1) { | 308 | if (ret != 1) { |
311 | return(0); | 309 | return 0; |
312 | } | 310 | } |
313 | return(1); | 311 | return 1; |
314 | } | 312 | } |
315 | #endif | 313 | #endif |
316 | 314 | ||
@@ -319,20 +317,20 @@ static int loopback_up6(struct interface_defn_t *ifd, execfn *exec) | |||
319 | { | 317 | { |
320 | #ifdef CONFIG_FEATURE_IFUPDOWN_IP | 318 | #ifdef CONFIG_FEATURE_IFUPDOWN_IP |
321 | int result; | 319 | int result; |
322 | result =execute("ip addr add ::1 dev %iface%", ifd, exec); | 320 | result = execute("ip addr add ::1 dev %iface%", ifd, exec); |
323 | result += execute("ip link set %iface% up", ifd, exec); | 321 | result += execute("ip link set %iface% up", ifd, exec); |
324 | return ((result == 2) ? 2 : 0); | 322 | return ((result == 2) ? 2 : 0); |
325 | #else | 323 | #else |
326 | return( execute("ifconfig %iface% add ::1", ifd, exec)); | 324 | return execute("ifconfig %iface% add ::1", ifd, exec); |
327 | #endif | 325 | #endif |
328 | } | 326 | } |
329 | 327 | ||
330 | static int loopback_down6(struct interface_defn_t *ifd, execfn *exec) | 328 | static int loopback_down6(struct interface_defn_t *ifd, execfn *exec) |
331 | { | 329 | { |
332 | #ifdef CONFIG_FEATURE_IFUPDOWN_IP | 330 | #ifdef CONFIG_FEATURE_IFUPDOWN_IP |
333 | return(execute("ip link set %iface% down", ifd, exec)); | 331 | return execute("ip link set %iface% down", ifd, exec); |
334 | #else | 332 | #else |
335 | return(execute("ifconfig %iface% del ::1", ifd, exec)); | 333 | return execute("ifconfig %iface% del ::1", ifd, exec); |
336 | #endif | 334 | #endif |
337 | } | 335 | } |
338 | 336 | ||
@@ -354,9 +352,9 @@ static int static_up6(struct interface_defn_t *ifd, execfn *exec) | |||
354 | static int static_down6(struct interface_defn_t *ifd, execfn *exec) | 352 | static int static_down6(struct interface_defn_t *ifd, execfn *exec) |
355 | { | 353 | { |
356 | #ifdef CONFIG_FEATURE_IFUPDOWN_IP | 354 | #ifdef CONFIG_FEATURE_IFUPDOWN_IP |
357 | return(execute("ip link set %iface% down", ifd, exec)); | 355 | return execute("ip link set %iface% down", ifd, exec); |
358 | #else | 356 | #else |
359 | return(execute("ifconfig %iface% down", ifd, exec)); | 357 | return execute("ifconfig %iface% down", ifd, exec); |
360 | #endif | 358 | #endif |
361 | } | 359 | } |
362 | 360 | ||
@@ -365,7 +363,7 @@ static int v4tunnel_up(struct interface_defn_t *ifd, execfn *exec) | |||
365 | { | 363 | { |
366 | int result; | 364 | int result; |
367 | result = execute("ip tunnel add %iface% mode sit remote " | 365 | result = execute("ip tunnel add %iface% mode sit remote " |
368 | "%endpoint% [[local %local%]] [[ttl %ttl%]]", ifd, exec); | 366 | "%endpoint% [[local %local%]] [[ttl %ttl%]]", ifd, exec); |
369 | result += execute("ip link set %iface% up", ifd, exec); | 367 | result += execute("ip link set %iface% up", ifd, exec); |
370 | result += execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec); | 368 | result += execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec); |
371 | result += execute("[[ ip route add ::/0 via %gateway% ]]", ifd, exec); | 369 | result += execute("[[ ip route add ::/0 via %gateway% ]]", ifd, exec); |
@@ -374,11 +372,11 @@ static int v4tunnel_up(struct interface_defn_t *ifd, execfn *exec) | |||
374 | 372 | ||
375 | static int v4tunnel_down(struct interface_defn_t * ifd, execfn * exec) | 373 | static int v4tunnel_down(struct interface_defn_t * ifd, execfn * exec) |
376 | { | 374 | { |
377 | return( execute("ip tunnel del %iface%", ifd, exec)); | 375 | return execute("ip tunnel del %iface%", ifd, exec); |
378 | } | 376 | } |
379 | #endif | 377 | #endif |
380 | 378 | ||
381 | static struct method_t methods6[] = { | 379 | static const struct method_t methods6[] = { |
382 | #ifdef CONFIG_FEATURE_IFUPDOWN_IP | 380 | #ifdef CONFIG_FEATURE_IFUPDOWN_IP |
383 | { "v4tunnel", v4tunnel_up, v4tunnel_down, }, | 381 | { "v4tunnel", v4tunnel_up, v4tunnel_down, }, |
384 | #endif | 382 | #endif |
@@ -386,7 +384,7 @@ static struct method_t methods6[] = { | |||
386 | { "loopback", loopback_up6, loopback_down6, }, | 384 | { "loopback", loopback_up6, loopback_down6, }, |
387 | }; | 385 | }; |
388 | 386 | ||
389 | static struct address_family_t addr_inet6 = { | 387 | static const struct address_family_t addr_inet6 = { |
390 | "inet6", | 388 | "inet6", |
391 | sizeof(methods6) / sizeof(struct method_t), | 389 | sizeof(methods6) / sizeof(struct method_t), |
392 | methods6 | 390 | methods6 |
@@ -402,7 +400,7 @@ static int loopback_up(struct interface_defn_t *ifd, execfn *exec) | |||
402 | result += execute("ip link set %iface% up", ifd, exec); | 400 | result += execute("ip link set %iface% up", ifd, exec); |
403 | return ((result == 2) ? 2 : 0); | 401 | return ((result == 2) ? 2 : 0); |
404 | #else | 402 | #else |
405 | return( execute("ifconfig %iface% 127.0.0.1 up", ifd, exec)); | 403 | return execute("ifconfig %iface% 127.0.0.1 up", ifd, exec); |
406 | #endif | 404 | #endif |
407 | } | 405 | } |
408 | 406 | ||
@@ -414,7 +412,7 @@ static int loopback_down(struct interface_defn_t *ifd, execfn *exec) | |||
414 | result += execute("ip link set %iface% down", ifd, exec); | 412 | result += execute("ip link set %iface% down", ifd, exec); |
415 | return ((result == 2) ? 2 : 0); | 413 | return ((result == 2) ? 2 : 0); |
416 | #else | 414 | #else |
417 | return( execute("ifconfig %iface% 127.0.0.1 down", ifd, exec)); | 415 | return execute("ifconfig %iface% 127.0.0.1 down", ifd, exec); |
418 | #endif | 416 | #endif |
419 | } | 417 | } |
420 | 418 | ||
@@ -429,9 +427,9 @@ static int static_up(struct interface_defn_t *ifd, execfn *exec) | |||
429 | return ((result == 3) ? 3 : 0); | 427 | return ((result == 3) ? 3 : 0); |
430 | #else | 428 | #else |
431 | result = execute("ifconfig %iface% %address% netmask %netmask% " | 429 | result = execute("ifconfig %iface% %address% netmask %netmask% " |
432 | "[[broadcast %broadcast%]] [[pointopoint %pointopoint%]] " | 430 | "[[broadcast %broadcast%]] [[pointopoint %pointopoint%]] " |
433 | "[[media %media%]] [[mtu %mtu%]] [[hw %hwaddress%]] up", | 431 | "[[media %media%]] [[mtu %mtu%]] [[hw %hwaddress%]] up", |
434 | ifd, exec); | 432 | ifd, exec); |
435 | result += execute("[[ route add default gw %gateway% %iface% ]]", ifd, exec); | 433 | result += execute("[[ route add default gw %gateway% %iface% ]]", ifd, exec); |
436 | return ((result == 2) ? 2 : 0); | 434 | return ((result == 2) ? 2 : 0); |
437 | #endif | 435 | #endif |
@@ -498,35 +496,34 @@ static int dhcp_down(struct interface_defn_t *ifd, execfn *exec) | |||
498 | 496 | ||
499 | static int bootp_up(struct interface_defn_t *ifd, execfn *exec) | 497 | static int bootp_up(struct interface_defn_t *ifd, execfn *exec) |
500 | { | 498 | { |
501 | return( execute("bootpc [[--bootfile %bootfile%]] --dev %iface% " | 499 | return execute("bootpc [[--bootfile %bootfile%]] --dev %iface% " |
502 | "[[--server %server%]] [[--hwaddr %hwaddr%]] " | 500 | "[[--server %server%]] [[--hwaddr %hwaddr%]] " |
503 | "--returniffail --serverbcast", ifd, exec)); | 501 | "--returniffail --serverbcast", ifd, exec); |
504 | } | 502 | } |
505 | 503 | ||
506 | static int ppp_up(struct interface_defn_t *ifd, execfn *exec) | 504 | static int ppp_up(struct interface_defn_t *ifd, execfn *exec) |
507 | { | 505 | { |
508 | return( execute("pon [[%provider%]]", ifd, exec)); | 506 | return execute("pon [[%provider%]]", ifd, exec); |
509 | } | 507 | } |
510 | 508 | ||
511 | static int ppp_down(struct interface_defn_t *ifd, execfn *exec) | 509 | static int ppp_down(struct interface_defn_t *ifd, execfn *exec) |
512 | { | 510 | { |
513 | return( execute("poff [[%provider%]]", ifd, exec)); | 511 | return execute("poff [[%provider%]]", ifd, exec); |
514 | } | 512 | } |
515 | 513 | ||
516 | static int wvdial_up(struct interface_defn_t *ifd, execfn *exec) | 514 | static int wvdial_up(struct interface_defn_t *ifd, execfn *exec) |
517 | { | 515 | { |
518 | return( execute("/sbin/start-stop-daemon --start -x /usr/bin/wvdial " | 516 | return execute("/sbin/start-stop-daemon --start -x /usr/bin/wvdial " |
519 | "-p /var/run/wvdial.%iface% -b -m -- [[ %provider% ]]", ifd, exec)); | 517 | "-p /var/run/wvdial.%iface% -b -m -- [[ %provider% ]]", ifd, exec); |
520 | } | 518 | } |
521 | 519 | ||
522 | static int wvdial_down(struct interface_defn_t *ifd, execfn *exec) | 520 | static int wvdial_down(struct interface_defn_t *ifd, execfn *exec) |
523 | { | 521 | { |
524 | return( execute("/sbin/start-stop-daemon --stop -x /usr/bin/wvdial " | 522 | return execute("/sbin/start-stop-daemon --stop -x /usr/bin/wvdial " |
525 | "-p /var/run/wvdial.%iface% -s 2", ifd, exec)); | 523 | "-p /var/run/wvdial.%iface% -s 2", ifd, exec); |
526 | } | 524 | } |
527 | 525 | ||
528 | static struct method_t methods[] = | 526 | static const struct method_t methods[] = { |
529 | { | ||
530 | { "wvdial", wvdial_up, wvdial_down, }, | 527 | { "wvdial", wvdial_up, wvdial_down, }, |
531 | { "ppp", ppp_up, ppp_down, }, | 528 | { "ppp", ppp_up, ppp_down, }, |
532 | { "static", static_up, static_down, }, | 529 | { "static", static_up, static_down, }, |
@@ -535,8 +532,7 @@ static struct method_t methods[] = | |||
535 | { "loopback", loopback_up, loopback_down, }, | 532 | { "loopback", loopback_up, loopback_down, }, |
536 | }; | 533 | }; |
537 | 534 | ||
538 | static struct address_family_t addr_inet = | 535 | static const struct address_family_t addr_inet = { |
539 | { | ||
540 | "inet", | 536 | "inet", |
541 | sizeof(methods) / sizeof(struct method_t), | 537 | sizeof(methods) / sizeof(struct method_t), |
542 | methods | 538 | methods |
@@ -561,13 +557,13 @@ static char *next_word(char **buf) | |||
561 | 557 | ||
562 | /* Skip over comments */ | 558 | /* Skip over comments */ |
563 | if (*word == '#') { | 559 | if (*word == '#') { |
564 | return(NULL); | 560 | return NULL; |
565 | } | 561 | } |
566 | 562 | ||
567 | /* Find the length of this word */ | 563 | /* Find the length of this word */ |
568 | length = strcspn(word, " \t\n"); | 564 | length = strcspn(word, " \t\n"); |
569 | if (length == 0) { | 565 | if (length == 0) { |
570 | return(NULL); | 566 | return NULL; |
571 | } | 567 | } |
572 | *buf = word + length; | 568 | *buf = word + length; |
573 | /*DBU:[dave@cray.com] if we are already at EOL dont't increment beyond it */ | 569 | /*DBU:[dave@cray.com] if we are already at EOL dont't increment beyond it */ |
@@ -579,7 +575,7 @@ static char *next_word(char **buf) | |||
579 | return word; | 575 | return word; |
580 | } | 576 | } |
581 | 577 | ||
582 | static struct address_family_t *get_address_family(struct address_family_t *af[], char *name) | 578 | static const struct address_family_t *get_address_family(const struct address_family_t *const af[], char *name) |
583 | { | 579 | { |
584 | int i; | 580 | int i; |
585 | 581 | ||
@@ -591,7 +587,7 @@ static struct address_family_t *get_address_family(struct address_family_t *af[] | |||
591 | return NULL; | 587 | return NULL; |
592 | } | 588 | } |
593 | 589 | ||
594 | static struct method_t *get_method(struct address_family_t *af, char *name) | 590 | static const struct method_t *get_method(const struct address_family_t *af, char *name) |
595 | { | 591 | { |
596 | int i; | 592 | int i; |
597 | 593 | ||
@@ -600,18 +596,18 @@ static struct method_t *get_method(struct address_family_t *af, char *name) | |||
600 | return &af->method[i]; | 596 | return &af->method[i]; |
601 | } | 597 | } |
602 | } | 598 | } |
603 | return(NULL); | 599 | return NULL; |
604 | } | 600 | } |
605 | 601 | ||
606 | static const llist_t *find_list_string(const llist_t *list, const char *string) | 602 | static const llist_t *find_list_string(const llist_t *list, const char *string) |
607 | { | 603 | { |
608 | while (list) { | 604 | while (list) { |
609 | if (strcmp(list->data, string) == 0) { | 605 | if (strcmp(list->data, string) == 0) { |
610 | return(list); | 606 | return list; |
611 | } | 607 | } |
612 | list = list->link; | 608 | list = list->link; |
613 | } | 609 | } |
614 | return(NULL); | 610 | return NULL; |
615 | } | 611 | } |
616 | 612 | ||
617 | static struct interfaces_file_t *read_interfaces(const char *filename) | 613 | static struct interfaces_file_t *read_interfaces(const char *filename) |
@@ -668,70 +664,66 @@ static struct interfaces_file_t *read_interfaces(const char *filename) | |||
668 | #endif | 664 | #endif |
669 | currently_processing = MAPPING; | 665 | currently_processing = MAPPING; |
670 | } else if (strcmp(firstword, "iface") == 0) { | 666 | } else if (strcmp(firstword, "iface") == 0) { |
671 | { | 667 | static const struct address_family_t *const addr_fams[] = { |
672 | char *iface_name; | ||
673 | char *address_family_name; | ||
674 | char *method_name; | ||
675 | struct address_family_t *addr_fams[] = { | ||
676 | #ifdef CONFIG_FEATURE_IFUPDOWN_IPV4 | 668 | #ifdef CONFIG_FEATURE_IFUPDOWN_IPV4 |
677 | &addr_inet, | 669 | &addr_inet, |
678 | #endif | 670 | #endif |
679 | #ifdef CONFIG_FEATURE_IFUPDOWN_IPV6 | 671 | #ifdef CONFIG_FEATURE_IFUPDOWN_IPV6 |
680 | &addr_inet6, | 672 | &addr_inet6, |
681 | #endif | 673 | #endif |
682 | NULL | 674 | NULL |
683 | }; | 675 | }; |
676 | |||
677 | char *iface_name; | ||
678 | char *address_family_name; | ||
679 | char *method_name; | ||
680 | llist_t *iface_list; | ||
681 | |||
682 | currif = xzalloc(sizeof(struct interface_defn_t)); | ||
683 | iface_name = next_word(&buf_ptr); | ||
684 | address_family_name = next_word(&buf_ptr); | ||
685 | method_name = next_word(&buf_ptr); | ||
686 | |||
687 | if (buf_ptr == NULL) { | ||
688 | bb_error_msg("too few parameters for line \"%s\"", buf); | ||
689 | return NULL; | ||
690 | } | ||
684 | 691 | ||
685 | currif = xzalloc(sizeof(struct interface_defn_t)); | 692 | /* ship any trailing whitespace */ |
686 | iface_name = next_word(&buf_ptr); | 693 | while (isspace(*buf_ptr)) { |
687 | address_family_name = next_word(&buf_ptr); | 694 | ++buf_ptr; |
688 | method_name = next_word(&buf_ptr); | 695 | } |
689 | 696 | ||
690 | if (buf_ptr == NULL) { | 697 | if (buf_ptr[0] != '\0') { |
691 | bb_error_msg("too few parameters for line \"%s\"", buf); | 698 | bb_error_msg("too many parameters \"%s\"", buf); |
692 | return NULL; | 699 | return NULL; |
693 | } | 700 | } |
694 | 701 | ||
695 | /* ship any trailing whitespace */ | 702 | currif->iface = xstrdup(iface_name); |
696 | while (isspace(*buf_ptr)) { | ||
697 | ++buf_ptr; | ||
698 | } | ||
699 | 703 | ||
700 | if (buf_ptr[0] != '\0') { | 704 | currif->address_family = get_address_family(addr_fams, address_family_name); |
701 | bb_error_msg("too many parameters \"%s\"", buf); | 705 | if (!currif->address_family) { |
702 | return NULL; | 706 | bb_error_msg("unknown address type \"%s\"", address_family_name); |
703 | } | 707 | return NULL; |
704 | 708 | } | |
705 | currif->iface = xstrdup(iface_name); | ||
706 | 709 | ||
707 | currif->address_family = get_address_family(addr_fams, address_family_name); | 710 | currif->method = get_method(currif->address_family, method_name); |
708 | if (!currif->address_family) { | 711 | if (!currif->method) { |
709 | bb_error_msg("unknown address type \"%s\"", address_family_name); | 712 | bb_error_msg("unknown method \"%s\"", method_name); |
710 | return NULL; | 713 | return NULL; |
711 | } | 714 | } |
712 | 715 | ||
713 | currif->method = get_method(currif->address_family, method_name); | 716 | for (iface_list = defn->ifaces; iface_list; iface_list = iface_list->link) { |
714 | if (!currif->method) { | 717 | struct interface_defn_t *tmp = (struct interface_defn_t *) iface_list->data; |
715 | bb_error_msg("unknown method \"%s\"", method_name); | 718 | if ((strcmp(tmp->iface, currif->iface) == 0) && |
719 | (tmp->address_family == currif->address_family)) { | ||
720 | bb_error_msg("duplicate interface \"%s\"", tmp->iface); | ||
716 | return NULL; | 721 | return NULL; |
717 | } | 722 | } |
718 | |||
719 | |||
720 | { | ||
721 | llist_t *iface_list; | ||
722 | for (iface_list = defn->ifaces; iface_list; iface_list = iface_list->link) { | ||
723 | struct interface_defn_t *tmp = (struct interface_defn_t *) iface_list->data; | ||
724 | if ((strcmp(tmp->iface, currif->iface) == 0) && | ||
725 | (tmp->address_family == currif->address_family)) { | ||
726 | bb_error_msg("duplicate interface \"%s\"", tmp->iface); | ||
727 | return NULL; | ||
728 | } | ||
729 | } | ||
730 | |||
731 | llist_add_to_end(&(defn->ifaces), (char*)currif); | ||
732 | } | ||
733 | debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name); | ||
734 | } | 723 | } |
724 | llist_add_to_end(&(defn->ifaces), (char*)currif); | ||
725 | |||
726 | debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name); | ||
735 | currently_processing = IFACE; | 727 | currently_processing = IFACE; |
736 | } else if (strcmp(firstword, "auto") == 0) { | 728 | } else if (strcmp(firstword, "auto") == 0) { |
737 | while ((firstword = next_word(&buf_ptr)) != NULL) { | 729 | while ((firstword = next_word(&buf_ptr)) != NULL) { |
@@ -748,74 +740,74 @@ static struct interfaces_file_t *read_interfaces(const char *filename) | |||
748 | currently_processing = NONE; | 740 | currently_processing = NONE; |
749 | } else { | 741 | } else { |
750 | switch (currently_processing) { | 742 | switch (currently_processing) { |
751 | case IFACE: | 743 | case IFACE: |
752 | { | 744 | { |
753 | int i; | 745 | int i; |
754 | 746 | ||
755 | if (strlen(buf_ptr) == 0) { | 747 | if (strlen(buf_ptr) == 0) { |
756 | bb_error_msg("option with empty value \"%s\"", buf); | 748 | bb_error_msg("option with empty value \"%s\"", buf); |
757 | return NULL; | 749 | return NULL; |
758 | } | 750 | } |
759 | 751 | ||
760 | if (strcmp(firstword, "up") != 0 | 752 | if (strcmp(firstword, "up") != 0 |
761 | && strcmp(firstword, "down") != 0 | 753 | && strcmp(firstword, "down") != 0 |
762 | && strcmp(firstword, "pre-up") != 0 | 754 | && strcmp(firstword, "pre-up") != 0 |
763 | && strcmp(firstword, "post-down") != 0) { | 755 | && strcmp(firstword, "post-down") != 0) { |
764 | for (i = 0; i < currif->n_options; i++) { | 756 | for (i = 0; i < currif->n_options; i++) { |
765 | if (strcmp(currif->option[i].name, firstword) == 0) { | 757 | if (strcmp(currif->option[i].name, firstword) == 0) { |
766 | bb_error_msg("duplicate option \"%s\"", buf); | 758 | bb_error_msg("duplicate option \"%s\"", buf); |
767 | return NULL; | 759 | return NULL; |
768 | } | ||
769 | } | 760 | } |
770 | } | 761 | } |
771 | } | 762 | } |
772 | if (currif->n_options >= currif->max_options) { | 763 | } |
773 | struct variable_t *opt; | 764 | if (currif->n_options >= currif->max_options) { |
765 | struct variable_t *opt; | ||
774 | 766 | ||
775 | currif->max_options = currif->max_options + 10; | 767 | currif->max_options = currif->max_options + 10; |
776 | opt = xrealloc(currif->option, sizeof(*opt) * currif->max_options); | 768 | opt = xrealloc(currif->option, sizeof(*opt) * currif->max_options); |
777 | currif->option = opt; | 769 | currif->option = opt; |
778 | } | 770 | } |
779 | currif->option[currif->n_options].name = xstrdup(firstword); | 771 | currif->option[currif->n_options].name = xstrdup(firstword); |
780 | currif->option[currif->n_options].value = xstrdup(buf_ptr); | 772 | currif->option[currif->n_options].value = xstrdup(buf_ptr); |
781 | if (!currif->option[currif->n_options].name) { | 773 | if (!currif->option[currif->n_options].name) { |
782 | perror(filename); | 774 | perror(filename); |
783 | return NULL; | 775 | return NULL; |
784 | } | 776 | } |
785 | if (!currif->option[currif->n_options].value) { | 777 | if (!currif->option[currif->n_options].value) { |
786 | perror(filename); | 778 | perror(filename); |
787 | return NULL; | 779 | return NULL; |
788 | } | 780 | } |
789 | debug_noise("\t%s=%s\n", currif->option[currif->n_options].name, | 781 | debug_noise("\t%s=%s\n", currif->option[currif->n_options].name, |
790 | currif->option[currif->n_options].value); | 782 | currif->option[currif->n_options].value); |
791 | currif->n_options++; | 783 | currif->n_options++; |
792 | break; | 784 | break; |
793 | case MAPPING: | 785 | case MAPPING: |
794 | #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING | 786 | #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING |
795 | if (strcmp(firstword, "script") == 0) { | 787 | if (strcmp(firstword, "script") == 0) { |
796 | if (currmap->script != NULL) { | 788 | if (currmap->script != NULL) { |
797 | bb_error_msg("duplicate script in mapping \"%s\"", buf); | 789 | bb_error_msg("duplicate script in mapping \"%s\"", buf); |
798 | return NULL; | ||
799 | } else { | ||
800 | currmap->script = xstrdup(next_word(&buf_ptr)); | ||
801 | } | ||
802 | } else if (strcmp(firstword, "map") == 0) { | ||
803 | if (currmap->max_mappings == currmap->n_mappings) { | ||
804 | currmap->max_mappings = currmap->max_mappings * 2 + 1; | ||
805 | currmap->mapping = xrealloc(currmap->mapping, sizeof(char *) * currmap->max_mappings); | ||
806 | } | ||
807 | currmap->mapping[currmap->n_mappings] = xstrdup(next_word(&buf_ptr)); | ||
808 | currmap->n_mappings++; | ||
809 | } else { | ||
810 | bb_error_msg("misplaced option \"%s\"", buf); | ||
811 | return NULL; | 790 | return NULL; |
791 | } else { | ||
792 | currmap->script = xstrdup(next_word(&buf_ptr)); | ||
812 | } | 793 | } |
813 | #endif | 794 | } else if (strcmp(firstword, "map") == 0) { |
814 | break; | 795 | if (currmap->max_mappings == currmap->n_mappings) { |
815 | case NONE: | 796 | currmap->max_mappings = currmap->max_mappings * 2 + 1; |
816 | default: | 797 | currmap->mapping = xrealloc(currmap->mapping, sizeof(char *) * currmap->max_mappings); |
798 | } | ||
799 | currmap->mapping[currmap->n_mappings] = xstrdup(next_word(&buf_ptr)); | ||
800 | currmap->n_mappings++; | ||
801 | } else { | ||
817 | bb_error_msg("misplaced option \"%s\"", buf); | 802 | bb_error_msg("misplaced option \"%s\"", buf); |
818 | return NULL; | 803 | return NULL; |
804 | } | ||
805 | #endif | ||
806 | break; | ||
807 | case NONE: | ||
808 | default: | ||
809 | bb_error_msg("misplaced option \"%s\"", buf); | ||
810 | return NULL; | ||
819 | } | 811 | } |
820 | } | 812 | } |
821 | free(buf); | 813 | free(buf); |
@@ -883,13 +875,15 @@ static void set_environ(struct interface_defn_t *iface, const char *mode) | |||
883 | *(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name); | 875 | *(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name); |
884 | *(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name); | 876 | *(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name); |
885 | *(environend++) = setlocalenv("%s=%s", "MODE", mode); | 877 | *(environend++) = setlocalenv("%s=%s", "MODE", mode); |
878 | /* FIXME: we must not impose our own PATH, it is admin's job! | ||
879 | Use PATH from parent env */ | ||
886 | *(environend++) = setlocalenv("%s=%s", "PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"); | 880 | *(environend++) = setlocalenv("%s=%s", "PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"); |
887 | } | 881 | } |
888 | 882 | ||
889 | static int doit(char *str) | 883 | static int doit(char *str) |
890 | { | 884 | { |
891 | if (option_mask & (OPT_no_act|OPT_verbose)) { | 885 | if (option_mask & (OPT_no_act|OPT_verbose)) { |
892 | printf("%s\n", str); | 886 | puts(str); |
893 | } | 887 | } |
894 | if (!(option_mask & OPT_no_act)) { | 888 | if (!(option_mask & OPT_no_act)) { |
895 | pid_t child; | 889 | pid_t child; |
@@ -897,11 +891,11 @@ static int doit(char *str) | |||
897 | 891 | ||
898 | fflush(NULL); | 892 | fflush(NULL); |
899 | switch (child = fork()) { | 893 | switch (child = fork()) { |
900 | case -1: /* failure */ | 894 | case -1: /* failure */ |
901 | return 0; | 895 | return 0; |
902 | case 0: /* child */ | 896 | case 0: /* child */ |
903 | execle(DEFAULT_SHELL, DEFAULT_SHELL, "-c", str, NULL, __myenviron); | 897 | execle(DEFAULT_SHELL, DEFAULT_SHELL, "-c", str, NULL, __myenviron); |
904 | exit(127); | 898 | exit(127); |
905 | } | 899 | } |
906 | waitpid(child, &status, 0); | 900 | waitpid(child, &status, 0); |
907 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { | 901 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
@@ -983,27 +977,27 @@ static int popen2(FILE **in, FILE **out, char *command, ...) | |||
983 | 977 | ||
984 | fflush(NULL); | 978 | fflush(NULL); |
985 | switch (pid = fork()) { | 979 | switch (pid = fork()) { |
986 | case -1: /* failure */ | 980 | case -1: /* failure */ |
987 | close(infd[0]); | 981 | close(infd[0]); |
988 | close(infd[1]); | 982 | close(infd[1]); |
989 | close(outfd[0]); | 983 | close(outfd[0]); |
990 | close(outfd[1]); | 984 | close(outfd[1]); |
991 | return 0; | 985 | return 0; |
992 | case 0: /* child */ | 986 | case 0: /* child */ |
993 | dup2(infd[0], 0); | 987 | dup2(infd[0], 0); |
994 | dup2(outfd[1], 1); | 988 | dup2(outfd[1], 1); |
995 | close(infd[0]); | 989 | close(infd[0]); |
996 | close(infd[1]); | 990 | close(infd[1]); |
997 | close(outfd[0]); | 991 | close(outfd[0]); |
998 | close(outfd[1]); | 992 | close(outfd[1]); |
999 | execvp(command, argv); | 993 | execvp(command, argv); |
1000 | exit(127); | 994 | exit(127); |
1001 | default: /* parent */ | 995 | default: /* parent */ |
1002 | *in = fdopen(infd[1], "w"); | 996 | *in = fdopen(infd[1], "w"); |
1003 | *out = fdopen(outfd[0], "r"); | 997 | *out = fdopen(outfd[0], "r"); |
1004 | close(infd[0]); | 998 | close(infd[0]); |
1005 | close(outfd[1]); | 999 | close(outfd[1]); |
1006 | return pid; | 1000 | return pid; |
1007 | } | 1001 | } |
1008 | /* unreached */ | 1002 | /* unreached */ |
1009 | } | 1003 | } |
@@ -1068,21 +1062,22 @@ static llist_t *find_iface_state(llist_t *state_list, const char *iface) | |||
1068 | while (search) { | 1062 | while (search) { |
1069 | if ((strncmp(search->data, iface, iface_len) == 0) && | 1063 | if ((strncmp(search->data, iface, iface_len) == 0) && |
1070 | (search->data[iface_len] == '=')) { | 1064 | (search->data[iface_len] == '=')) { |
1071 | return(search); | 1065 | return search; |
1072 | } | 1066 | } |
1073 | search = search->link; | 1067 | search = search->link; |
1074 | } | 1068 | } |
1075 | return(NULL); | 1069 | return NULL; |
1076 | } | 1070 | } |
1077 | 1071 | ||
1078 | int ifupdown_main(int argc, char **argv) | 1072 | int ifupdown_main(int argc, char **argv) |
1079 | { | 1073 | { |
1080 | int (*cmds) (struct interface_defn_t *) = NULL; | 1074 | static const char statefile[] = "/var/run/ifstate"; |
1075 | |||
1076 | int (*cmds)(struct interface_defn_t *) = NULL; | ||
1081 | struct interfaces_file_t *defn; | 1077 | struct interfaces_file_t *defn; |
1082 | llist_t *state_list = NULL; | 1078 | llist_t *state_list = NULL; |
1083 | llist_t *target_list = NULL; | 1079 | llist_t *target_list = NULL; |
1084 | const char *interfaces = "/etc/network/interfaces"; | 1080 | const char *interfaces = "/etc/network/interfaces"; |
1085 | const char *statefile = "/var/run/ifstate"; | ||
1086 | int any_failures = 0; | 1081 | int any_failures = 0; |
1087 | int i; | 1082 | int i; |
1088 | 1083 | ||
@@ -1170,7 +1165,6 @@ int ifupdown_main(int argc, char **argv) | |||
1170 | struct mapping_defn_t *currmap; | 1165 | struct mapping_defn_t *currmap; |
1171 | 1166 | ||
1172 | for (currmap = defn->mappings; currmap; currmap = currmap->next) { | 1167 | for (currmap = defn->mappings; currmap; currmap = currmap->next) { |
1173 | |||
1174 | for (i = 0; i < currmap->n_matches; i++) { | 1168 | for (i = 0; i < currmap->n_matches; i++) { |
1175 | if (fnmatch(currmap->match[i], liface, 0) != 0) | 1169 | if (fnmatch(currmap->match[i], liface, 0) != 0) |
1176 | continue; | 1170 | continue; |
@@ -1199,7 +1193,7 @@ int ifupdown_main(int argc, char **argv) | |||
1199 | /* Call the cmds function pointer, does either iface_up() or iface_down() */ | 1193 | /* Call the cmds function pointer, does either iface_up() or iface_down() */ |
1200 | cmds_ret = cmds(currif); | 1194 | cmds_ret = cmds(currif); |
1201 | if (cmds_ret == -1) { | 1195 | if (cmds_ret == -1) { |
1202 | bb_error_msg("Don't seem to have all the variables for %s/%s.", | 1196 | bb_error_msg("don't seem to have all the variables for %s/%s", |
1203 | liface, currif->address_family->name); | 1197 | liface, currif->address_family->name); |
1204 | any_failures += 1; | 1198 | any_failures += 1; |
1205 | } else if (cmds_ret == 0) { | 1199 | } else if (cmds_ret == 0) { |
@@ -1211,11 +1205,11 @@ int ifupdown_main(int argc, char **argv) | |||
1211 | iface_list = iface_list->link; | 1205 | iface_list = iface_list->link; |
1212 | } | 1206 | } |
1213 | if (VERBOSE) { | 1207 | if (VERBOSE) { |
1214 | printf("\n"); | 1208 | puts(""); |
1215 | } | 1209 | } |
1216 | 1210 | ||
1217 | if (!okay && !FORCE) { | 1211 | if (!okay && !FORCE) { |
1218 | bb_error_msg("Ignoring unknown interface %s", liface); | 1212 | bb_error_msg("ignoring unknown interface %s", liface); |
1219 | any_failures += 1; | 1213 | any_failures += 1; |
1220 | } else { | 1214 | } else { |
1221 | llist_t *iface_state = find_iface_state(state_list, iface); | 1215 | llist_t *iface_state = find_iface_state(state_list, iface); |