d23c9f70aedd713b173526ceff2edcabc7cdd809
[oweals/busybox.git] / networking / ifupdown.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  *  ifupdown for busybox
4  *  Copyright (c) 2002 Glenn McGrath <bug1@iinet.net.au>
5  *  Copyright (c) 2003-2004 Erik Andersen <andersen@codepoet.org>
6  *
7  *  Based on ifupdown v 0.6.4 by Anthony Towns
8  *  Copyright (c) 1999 Anthony Towns <aj@azure.humbug.org.au>
9  *
10  *  Changes to upstream version
11  *  Remove checks for kernel version, assume kernel version 2.2.0 or better.
12  *  Lines in the interfaces file cannot wrap.
13  *  To adhere to the FHS, the default state file is /var/run/ifstate.
14  *
15  *  This program is free software; you can redistribute it and/or modify
16  *  it under the terms of the GNU General Public License as published by
17  *  the Free Software Foundation; either version 2 of the License, or
18  *  (at your option) any later version.
19  *
20  *  This program is distributed in the hope that it will be useful,
21  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  *  GNU General Public License for more details.
24  *
25  *  You should have received a copy of the GNU General Public License
26  *  along with this program; if not, write to the Free Software
27  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28  */
29
30 /* TODO: standardise execute() return codes to return 0 for success and 1 for failure */
31
32 #include <sys/stat.h>
33 #include <sys/utsname.h>
34 #include <sys/wait.h>
35
36 #include <ctype.h>
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <fnmatch.h>
40 #include <getopt.h>
41 #include <stdarg.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46
47 #include "libbb.h"
48
49 #define MAX_OPT_DEPTH 10
50 #define EUNBALBRACK 10001
51 #define EUNDEFVAR   10002
52 #define EUNBALPER   10000
53
54 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
55 #define MAX_INTERFACE_LENGTH 10
56 #endif
57
58 #if 0
59 #define debug_noise(fmt, args...) printf(fmt, ## args)
60 #else
61 #define debug_noise(fmt, args...)
62 #endif
63
64 /* Forward declaration */
65 struct interface_defn_t;
66
67 typedef int (execfn)(char *command);
68 typedef int (command_set)(struct interface_defn_t *ifd, execfn *e);
69
70 struct method_t
71 {
72         char *name;
73         command_set *up;
74         command_set *down;
75 };
76
77 struct address_family_t
78 {
79         char *name;
80         int n_methods;
81         struct method_t *method;
82 };
83
84 struct mapping_defn_t
85 {
86         struct mapping_defn_t *next;
87
88         int max_matches;
89         int n_matches;
90         char **match;
91
92         char *script;
93
94         int max_mappings;
95         int n_mappings;
96         char **mapping;
97 };
98
99 struct variable_t
100 {
101         char *name;
102         char *value;
103 };
104
105 struct interface_defn_t
106 {
107         struct interface_defn_t *prev;
108         struct interface_defn_t *next;
109
110         char *iface;
111         struct address_family_t *address_family;
112         struct method_t *method;
113
114         int automatic;
115
116         int max_options;
117         int n_options;
118         struct variable_t *option;
119 };
120
121 struct interfaces_file_t
122 {
123         llist_t *autointerfaces;
124         llist_t *ifaces;
125         struct mapping_defn_t *mappings;
126 };
127
128 static char no_act = 0;
129 static char verbose = 0;
130 static char **__myenviron = NULL;
131
132 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
133
134 static unsigned int count_bits(unsigned int a)
135 {
136         unsigned int result;
137         result = (a & 0x55) + ((a >> 1) & 0x55);
138         result = (result & 0x33) + ((result >> 2) & 0x33);
139         return((result & 0x0F) + ((result >> 4) & 0x0F));
140 }
141
142 static int count_netmask_bits(char *dotted_quad)
143 {
144         unsigned int result, a, b, c, d;
145         /* Found a netmask...  Check if it is dotted quad */
146         if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
147                 return -1;
148         result = count_bits(a);
149         result += count_bits(b);
150         result += count_bits(c);
151         result += count_bits(d);
152         return ((int)result);
153 }
154 #endif
155
156 static void addstr(char **buf, size_t *len, size_t *pos, char *str, size_t str_length)
157 {
158         if (*pos + str_length >= *len) {
159                 char *newbuf;
160
161                 newbuf = xrealloc(*buf, *len * 2 + str_length + 1);
162                 *buf = newbuf;
163                 *len = *len * 2 + str_length + 1;
164         }
165
166         while (str_length-- >= 1) {
167                 (*buf)[(*pos)++] = *str;
168                 str++;
169         }
170         (*buf)[*pos] = '\0';
171 }
172
173 static int strncmpz(char *l, char *r, size_t llen)
174 {
175         int i = strncmp(l, r, llen);
176
177         if (i == 0) {
178                 return(-r[llen]);
179         } else {
180                 return(i);
181         }
182 }
183
184 static char *get_var(char *id, size_t idlen, struct interface_defn_t *ifd)
185 {
186         int i;
187
188         if (strncmpz(id, "iface", idlen) == 0) {
189                 char *result;
190                 static char label_buf[20];
191                 strncpy(label_buf, ifd->iface, 19);
192                 label_buf[19]=0;
193                 result = strchr(label_buf, ':');
194                 if (result) {
195                         *result=0;
196                 }
197                 return( label_buf);
198         } else if (strncmpz(id, "label", idlen) == 0) {
199                 return (ifd->iface);
200         } else {
201                 for (i = 0; i < ifd->n_options; i++) {
202                         if (strncmpz(id, ifd->option[i].name, idlen) == 0) {
203                                 return (ifd->option[i].value);
204                         }
205                 }
206         }
207
208         return(NULL);
209 }
210
211 static char *parse(char *command, struct interface_defn_t *ifd)
212 {
213
214         char *result = NULL;
215         size_t pos = 0, len = 0;
216         size_t old_pos[MAX_OPT_DEPTH] = { 0 };
217         int okay[MAX_OPT_DEPTH] = { 1 };
218         int opt_depth = 1;
219
220         while (*command) {
221                 switch (*command) {
222
223                         default:
224                                 addstr(&result, &len, &pos, command, 1);
225                                 command++;
226                                 break;
227                         case '\\':
228                                 if (command[1]) {
229                                         addstr(&result, &len, &pos, command + 1, 1);
230                                         command += 2;
231                                 } else {
232                                         addstr(&result, &len, &pos, command, 1);
233                                         command++;
234                                 }
235                                 break;
236                         case '[':
237                                 if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) {
238                                         old_pos[opt_depth] = pos;
239                                         okay[opt_depth] = 1;
240                                         opt_depth++;
241                                         command += 2;
242                                 } else {
243                                         addstr(&result, &len, &pos, "[", 1);
244                                         command++;
245                                 }
246                                 break;
247                         case ']':
248                                 if (command[1] == ']' && opt_depth > 1) {
249                                         opt_depth--;
250                                         if (!okay[opt_depth]) {
251                                                 pos = old_pos[opt_depth];
252                                                 result[pos] = '\0';
253                                         }
254                                         command += 2;
255                                 } else {
256                                         addstr(&result, &len, &pos, "]", 1);
257                                         command++;
258                                 }
259                                 break;
260                         case '%':
261                                 {
262                                         char *nextpercent;
263                                         char *varvalue;
264
265                                         command++;
266                                         nextpercent = strchr(command, '%');
267                                         if (!nextpercent) {
268                                                 errno = EUNBALPER;
269                                                 free(result);
270                                                 return (NULL);
271                                         }
272
273                                         varvalue = get_var(command, nextpercent - command, ifd);
274
275                                         if (varvalue) {
276                                                 addstr(&result, &len, &pos, varvalue, bb_strlen(varvalue));
277                                         } else {
278 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
279                                                 /* Sigh...  Add a special case for 'ip' to convert from
280                                                  * dotted quad to bit count style netmasks.  */
281                                                 if (strncmp(command, "bnmask", 6)==0) {
282                                                         int res;
283                                                         varvalue = get_var("netmask", 7, ifd);
284                                                         if (varvalue && (res=count_netmask_bits(varvalue)) > 0) {
285                                                                 char argument[255];
286                                                                 sprintf(argument, "%d", res);
287                                                                 addstr(&result, &len, &pos, argument, bb_strlen(argument));
288                                                                 command = nextpercent + 1;
289                                                                 break;
290                                                         }
291                                                 }
292 #endif
293                                                 okay[opt_depth - 1] = 0;
294                                         }
295
296                                         command = nextpercent + 1;
297                                 }
298                                 break;
299                 }
300         }
301
302         if (opt_depth > 1) {
303                 errno = EUNBALBRACK;
304                 free(result);
305                 return(NULL);
306         }
307
308         if (!okay[0]) {
309                 errno = EUNDEFVAR;
310                 free(result);
311                 return(NULL);
312         }
313
314         return(result);
315 }
316
317 static int execute(char *command, struct interface_defn_t *ifd, execfn *exec)
318 {
319         char *out;
320         int ret;
321
322         out = parse(command, ifd);
323         if (!out) {
324                 return(0);
325         }
326         ret = (*exec) (out);
327
328         free(out);
329         if (ret != 1) {
330                 return(0);
331         }
332         return(1);
333 }
334
335 #ifdef CONFIG_FEATURE_IFUPDOWN_IPX
336 static int static_up_ipx(struct interface_defn_t *ifd, execfn *exec)
337 {
338         return(execute("ipx_interface add %iface% %frame% %netnum%", ifd, exec));
339 }
340
341 static int static_down_ipx(struct interface_defn_t *ifd, execfn *exec)
342 {
343         return(execute("ipx_interface del %iface% %frame%", ifd, exec));
344 }
345
346 static int dynamic_up(struct interface_defn_t *ifd, execfn *exec)
347 {
348         return(execute("ipx_interface add %iface% %frame%", ifd, exec));
349 }
350
351 static int dynamic_down(struct interface_defn_t *ifd, execfn *exec)
352 {
353         return(execute("ipx_interface del %iface% %frame%", ifd, exec));
354 }
355
356 static struct method_t methods_ipx[] = {
357         { "dynamic", dynamic_up, dynamic_down, },
358         { "static", static_up_ipx, static_down_ipx, },
359 };
360
361 static struct address_family_t addr_ipx = {
362         "ipx",
363         sizeof(methods_ipx) / sizeof(struct method_t),
364         methods_ipx
365 };
366 #endif /* IFUP_FEATURE_IPX */
367
368 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
369 static int loopback_up6(struct interface_defn_t *ifd, execfn *exec)
370 {
371 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
372         int result;
373         result =execute("ip addr add ::1 dev %iface%", ifd, exec);
374         result += execute("ip link set %iface% up", ifd, exec);
375         return ((result == 2) ? 2 : 0);
376 #else
377         return( execute("ifconfig %iface% add ::1", ifd, exec));
378 #endif
379 }
380
381 static int loopback_down6(struct interface_defn_t *ifd, execfn *exec)
382 {
383 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
384         return(execute("ip link set %iface% down", ifd, exec));
385 #else
386         return(execute("ifconfig %iface% del ::1", ifd, exec));
387 #endif
388 }
389
390 static int static_up6(struct interface_defn_t *ifd, execfn *exec)
391 {
392         int result;
393 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
394         result = execute("ip addr add %address%/%netmask% dev %iface% [[label %label%]]", ifd, exec);
395         result += execute("ip link set [[mtu %mtu%]] [[address %hwaddress%]] %iface% up", ifd, exec);
396         result += execute("[[ ip route add ::/0 via %gateway% ]]", ifd, exec);
397 #else
398         result = execute("ifconfig %iface% [[media %media%]] [[hw %hwaddress%]] [[mtu %mtu%]] up", ifd, exec);
399         result += execute("ifconfig %iface% add %address%/%netmask%", ifd, exec);
400         result += execute("[[ route -A inet6 add ::/0 gw %gateway% ]]", ifd, exec);
401 #endif
402         return ((result == 3) ? 3 : 0);
403 }
404
405 static int static_down6(struct interface_defn_t *ifd, execfn *exec)
406 {
407 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
408         return(execute("ip link set %iface% down", ifd, exec));
409 #else
410         return(execute("ifconfig %iface% down", ifd, exec));
411 #endif
412 }
413
414 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
415 static int v4tunnel_up(struct interface_defn_t *ifd, execfn *exec)
416 {
417         int result;
418         result = execute("ip tunnel add %iface% mode sit remote "
419                                 "%endpoint% [[local %local%]] [[ttl %ttl%]]", ifd, exec);
420         result += execute("ip link set %iface% up", ifd, exec);
421         result += execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec);
422         result += execute("[[ ip route add ::/0 via %gateway% ]]", ifd, exec);
423         return ((result == 4) ? 4 : 0);
424 }
425
426 static int v4tunnel_down(struct interface_defn_t * ifd, execfn * exec)
427 {
428         return( execute("ip tunnel del %iface%", ifd, exec));
429 }
430 #endif
431
432 static struct method_t methods6[] = {
433 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
434         { "v4tunnel", v4tunnel_up, v4tunnel_down, },
435 #endif
436         { "static", static_up6, static_down6, },
437         { "loopback", loopback_up6, loopback_down6, },
438 };
439
440 static struct address_family_t addr_inet6 = {
441         "inet6",
442         sizeof(methods6) / sizeof(struct method_t),
443         methods6
444 };
445 #endif /* CONFIG_FEATURE_IFUPDOWN_IPV6 */
446
447 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
448 static int loopback_up(struct interface_defn_t *ifd, execfn *exec)
449 {
450 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
451         int result;
452         result = execute("ip addr add 127.0.0.1/8 dev %iface%", ifd, exec);
453         result += execute("ip link set %iface% up", ifd, exec);
454         return ((result == 2) ? 2 : 0);
455 #else
456         return( execute("ifconfig %iface% 127.0.0.1 up", ifd, exec));
457 #endif
458 }
459
460 static int loopback_down(struct interface_defn_t *ifd, execfn *exec)
461 {
462 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
463         int result;
464         result = execute("ip addr flush dev %iface%", ifd, exec);
465         result += execute("ip link set %iface% down", ifd, exec);
466         return ((result == 2) ? 2 : 0);
467 #else
468         return( execute("ifconfig %iface% 127.0.0.1 down", ifd, exec));
469 #endif
470 }
471
472 static int static_up(struct interface_defn_t *ifd, execfn *exec)
473 {
474         int result;
475 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
476         result = execute("ip addr add %address%/%bnmask% [[broadcast %broadcast%]] "
477                         "dev %iface% [[peer %pointopoint%]] [[label %label%]]", ifd, exec);
478         result += execute("ip link set [[mtu %mtu%]] [[address %hwaddress%]] %iface% up", ifd, exec);
479         result += execute("[[ ip route add default via %gateway% dev %iface% ]]", ifd, exec);
480         return ((result == 3) ? 3 : 0);
481 #else
482         result = execute("ifconfig %iface% %address% netmask %netmask% "
483                                 "[[broadcast %broadcast%]]      [[pointopoint %pointopoint%]] "
484                                 "[[media %media%]] [[mtu %mtu%]]        [[hw %hwaddress%]] up",
485                                 ifd, exec);
486         result += execute("[[ route add default gw %gateway% %iface% ]]", ifd, exec);
487         return ((result == 2) ? 2 : 0);
488 #endif
489 }
490
491 static int static_down(struct interface_defn_t *ifd, execfn *exec)
492 {
493         int result;
494 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
495         result = execute("ip addr flush dev %iface%", ifd, exec);
496         result += execute("ip link set %iface% down", ifd, exec);
497 #else
498         result = execute("[[ route del default gw %gateway% %iface% ]]", ifd, exec);
499         result += execute("ifconfig %iface% down", ifd, exec);
500 #endif
501         return ((result == 2) ? 2 : 0);
502 }
503
504 static int execable(char *program)
505 {
506         struct stat buf;
507         if (0 == stat(program, &buf)) {
508                 if (S_ISREG(buf.st_mode) && (S_IXUSR & buf.st_mode)) {
509                         return(1);
510                 }
511         }
512         return(0);
513 }
514
515 static int dhcp_up(struct interface_defn_t *ifd, execfn *exec)
516 {
517         if (execable("/sbin/udhcpc")) {
518                 return( execute("udhcpc -n -p /var/run/udhcpc.%iface%.pid -i "
519                                         "%iface% [[-H %hostname%]] [[-c %clientid%]]", ifd, exec));
520         } else if (execable("/sbin/pump")) {
521                 return( execute("pump -i %iface% [[-h %hostname%]] [[-l %leasehours%]]", ifd, exec));
522         } else if (execable("/sbin/dhclient")) {
523                 return( execute("dhclient -pf /var/run/dhclient.%iface%.pid %iface%", ifd, exec));
524         } else if (execable("/sbin/dhcpcd")) {
525                 return( execute("dhcpcd [[-h %hostname%]] [[-i %vendor%]] [[-I %clientid%]] "
526                                         "[[-l %leasetime%]] %iface%", ifd, exec));
527         }
528         return(0);
529 }
530
531 static int dhcp_down(struct interface_defn_t *ifd, execfn *exec)
532 {
533         int result = 0;
534         if (execable("/sbin/udhcpc")) {
535                 /* SIGUSR2 forces udhcpc to release the current lease and go inactive,
536                  * and SIGTERM causes udhcpc to exit.  Signals are queued and processed
537                  * sequentially so we don't need to sleep */
538                 result = execute("kill -USR2 `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
539                 result += execute("kill -TERM `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
540         } else if (execable("/sbin/pump")) {
541                 result = execute("pump -i %iface% -k", ifd, exec);
542         } else if (execable("/sbin/dhclient")) {
543                 result = execute("kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null", ifd, exec);
544         } else if (execable("/sbin/dhcpcd")) {
545                 result = execute("dhcpcd -k %iface%", ifd, exec);
546         }
547         return (result || static_down(ifd, exec));
548 }
549
550 static int bootp_up(struct interface_defn_t *ifd, execfn *exec)
551 {
552         return( execute("bootpc [[--bootfile %bootfile%]] --dev %iface% "
553                                 "[[--server %server%]] [[--hwaddr %hwaddr%]] "
554                                 "--returniffail --serverbcast", ifd, exec));
555 }
556
557 static int ppp_up(struct interface_defn_t *ifd, execfn *exec)
558 {
559         return( execute("pon [[%provider%]]", ifd, exec));
560 }
561
562 static int ppp_down(struct interface_defn_t *ifd, execfn *exec)
563 {
564         return( execute("poff [[%provider%]]", ifd, exec));
565 }
566
567 static int wvdial_up(struct interface_defn_t *ifd, execfn *exec)
568 {
569         return( execute("/sbin/start-stop-daemon --start -x /usr/bin/wvdial "
570                                 "-p /var/run/wvdial.%iface% -b -m -- [[ %provider% ]]", ifd, exec));
571 }
572
573 static int wvdial_down(struct interface_defn_t *ifd, execfn *exec)
574 {
575         return( execute("/sbin/start-stop-daemon --stop -x /usr/bin/wvdial "
576                                 "-p /var/run/wvdial.%iface% -s 2", ifd, exec));
577 }
578
579 static struct method_t methods[] =
580 {
581         { "wvdial", wvdial_up, wvdial_down, },
582         { "ppp", ppp_up, ppp_down, },
583         { "static", static_up, static_down, },
584         { "bootp", bootp_up, static_down, },
585         { "dhcp", dhcp_up, dhcp_down, },
586         { "loopback", loopback_up, loopback_down, },
587 };
588
589 static struct address_family_t addr_inet =
590 {
591         "inet",
592         sizeof(methods) / sizeof(struct method_t),
593         methods
594 };
595
596 #endif  /* ifdef CONFIG_FEATURE_IFUPDOWN_IPV4 */
597
598 static char *next_word(char **buf)
599 {
600         unsigned short length;
601         char *word;
602
603         if ((buf == NULL) || (*buf == NULL) || (**buf == '\0')) {
604                 return NULL;
605         }
606
607         /* Skip over leading whitespace */
608         word = *buf;
609         while (isspace(*word)) {
610                 ++word;
611         }
612
613         /* Skip over comments */
614         if (*word == '#') {
615                 return(NULL);
616         }
617
618         /* Find the length of this word */
619         length = strcspn(word, " \t\n");
620         if (length == 0) {
621                 return(NULL);
622         }
623         *buf = word + length;
624         /*DBU:[dave@cray.com] if we are already at EOL dont't increment beyond it */
625         if (**buf) {
626                 **buf = '\0';
627                 (*buf)++;
628         }
629
630         return word;
631 }
632
633 static struct address_family_t *get_address_family(struct address_family_t *af[], char *name)
634 {
635         int i;
636
637         for (i = 0; af[i]; i++) {
638                 if (strcmp(af[i]->name, name) == 0) {
639                         return af[i];
640                 }
641         }
642         return NULL;
643 }
644
645 static struct method_t *get_method(struct address_family_t *af, char *name)
646 {
647         int i;
648
649         for (i = 0; i < af->n_methods; i++) {
650                 if (strcmp(af->method[i].name, name) == 0) {
651                         return &af->method[i];
652                 }
653         }
654         return(NULL);
655 }
656
657 static int duplicate_if(struct interface_defn_t *ifa, struct interface_defn_t *ifb)
658 {
659         if (strcmp(ifa->iface, ifb->iface) != 0) {
660                 return(0);
661         }
662         if (ifa->address_family != ifb->address_family) {
663                 return(0);
664         }
665         return(1);
666 }
667
668 static const llist_t *find_list_string(const llist_t *list, const char *string)
669 {
670         while (list) {
671                 if (strcmp(list->data, string) == 0) {
672                         return(list);
673                 }
674                 list = list->link;
675         }
676         return(NULL);
677 }
678
679 static struct interfaces_file_t *read_interfaces(const char *filename)
680 {
681 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
682         struct mapping_defn_t *currmap = NULL;
683 #endif
684         struct interface_defn_t *currif = NULL;
685         struct interfaces_file_t *defn;
686         FILE *f;
687         char *firstword;
688         char *buf;
689
690         enum { NONE, IFACE, MAPPING } currently_processing = NONE;
691
692         defn = xmalloc(sizeof(struct interfaces_file_t));
693         defn->autointerfaces = NULL;
694         defn->mappings = NULL;
695         defn->ifaces = NULL;
696
697         f = bb_xfopen(filename, "r");
698
699         while ((buf = bb_get_chomped_line_from_file(f)) != NULL) {
700                 char *buf_ptr = buf;
701
702                 firstword = next_word(&buf_ptr);
703                 if (firstword == NULL) {
704                         free(buf);
705                         continue;       /* blank line */
706                 }
707
708                 if (strcmp(firstword, "mapping") == 0) {
709 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
710                         currmap = xmalloc(sizeof(struct mapping_defn_t));
711                         currmap->max_matches = 0;
712                         currmap->n_matches = 0;
713                         currmap->match = NULL;
714
715                         while ((firstword = next_word(&buf_ptr)) != NULL) {
716                                 if (currmap->max_matches == currmap->n_matches) {
717                                         currmap->max_matches = currmap->max_matches * 2 + 1;
718                                         currmap->match = xrealloc(currmap->match, sizeof(currmap->match) * currmap->max_matches);
719                                 }
720
721                                 currmap->match[currmap->n_matches++] = bb_xstrdup(firstword);
722                         }
723                         currmap->max_mappings = 0;
724                         currmap->n_mappings = 0;
725                         currmap->mapping = NULL;
726                         currmap->script = NULL;
727                         {
728                                 struct mapping_defn_t **where = &defn->mappings;
729                                 while (*where != NULL) {
730                                         where = &(*where)->next;
731                                 }
732                                 *where = currmap;
733                                 currmap->next = NULL;
734                         }
735                         debug_noise("Added mapping\n");
736 #endif
737                         currently_processing = MAPPING;
738                 } else if (strcmp(firstword, "iface") == 0) {
739                         {
740                                 char *iface_name;
741                                 char *address_family_name;
742                                 char *method_name;
743                                 struct address_family_t *addr_fams[] = {
744 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
745                                         &addr_inet,
746 #endif
747 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
748                                         &addr_inet6,
749 #endif
750 #ifdef CONFIG_FEATURE_IFUPDOWN_IPX
751                                         &addr_ipx,
752 #endif
753                                         NULL
754                                 };
755
756                                 currif = xmalloc(sizeof(struct interface_defn_t));
757                                 iface_name = next_word(&buf_ptr);
758                                 address_family_name = next_word(&buf_ptr);
759                                 method_name = next_word(&buf_ptr);
760
761                                 if (buf_ptr == NULL) {
762                                         bb_error_msg("too few parameters for line \"%s\"", buf);
763                                         return NULL;
764                                 }
765
766                                 /* ship any trailing whitespace */
767                                 while (isspace(*buf_ptr)) {
768                                         ++buf_ptr;
769                                 }
770
771                                 if (buf_ptr[0] != '\0') {
772                                         bb_error_msg("too many parameters \"%s\"", buf);
773                                         return NULL;
774                                 }
775
776                                 currif->iface = bb_xstrdup(iface_name);
777
778                                 currif->address_family = get_address_family(addr_fams, address_family_name);
779                                 if (!currif->address_family) {
780                                         bb_error_msg("unknown address type \"%s\"", address_family_name);
781                                         return NULL;
782                                 }
783
784                                 currif->method = get_method(currif->address_family, method_name);
785                                 if (!currif->method) {
786                                         bb_error_msg("unknown method \"%s\"", method_name);
787                                         return NULL;
788                                 }
789
790                                 currif->automatic = 1;
791                                 currif->max_options = 0;
792                                 currif->n_options = 0;
793                                 currif->option = NULL;
794
795                                 {
796                                         struct interface_defn_t *tmp;
797                                         llist_t *iface_list;
798                                         iface_list = defn->ifaces;
799                                         while (iface_list) {
800                                                 tmp = (struct interface_defn_t *) iface_list->data;
801                                                 if (duplicate_if(tmp, currif)) {
802                                                         bb_error_msg("duplicate interface \"%s\"", tmp->iface);
803                                                         return NULL;
804                                                 }
805                                                 iface_list = iface_list->link;
806                                         }
807
808                                         defn->ifaces = llist_add_to_end(defn->ifaces, (char*)currif);
809                                 }
810                                 debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
811                         }
812                         currently_processing = IFACE;
813                 } else if (strcmp(firstword, "auto") == 0) {
814                         while ((firstword = next_word(&buf_ptr)) != NULL) {
815
816                                 /* Check the interface isnt already listed */
817                                 if (find_list_string(defn->autointerfaces, firstword)) {
818                                         bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
819                                 }
820
821                                 /* Add the interface to the list */
822                                 defn->autointerfaces = llist_add_to_end(defn->autointerfaces, strdup(firstword));
823                                 debug_noise("\nauto %s\n", firstword);
824                         }
825                         currently_processing = NONE;
826                 } else {
827                         switch (currently_processing) {
828                                 case IFACE:
829                                         {
830                                                 int i;
831
832                                                 if (bb_strlen(buf_ptr) == 0) {
833                                                         bb_error_msg("option with empty value \"%s\"", buf);
834                                                         return NULL;
835                                                 }
836
837                                                 if (strcmp(firstword, "up") != 0
838                                                                 && strcmp(firstword, "down") != 0
839                                                                 && strcmp(firstword, "pre-up") != 0
840                                                                 && strcmp(firstword, "post-down") != 0) {
841                                                         for (i = 0; i < currif->n_options; i++) {
842                                                                 if (strcmp(currif->option[i].name, firstword) == 0) {
843                                                                         bb_error_msg("duplicate option \"%s\"", buf);
844                                                                         return NULL;
845                                                                 }
846                                                         }
847                                                 }
848                                         }
849                                         if (currif->n_options >= currif->max_options) {
850                                                 struct variable_t *opt;
851
852                                                 currif->max_options = currif->max_options + 10;
853                                                 opt = xrealloc(currif->option, sizeof(*opt) * currif->max_options);
854                                                 currif->option = opt;
855                                         }
856                                         currif->option[currif->n_options].name = bb_xstrdup(firstword);
857                                         currif->option[currif->n_options].value = bb_xstrdup(buf_ptr);
858                                         if (!currif->option[currif->n_options].name) {
859                                                 perror(filename);
860                                                 return NULL;
861                                         }
862                                         if (!currif->option[currif->n_options].value) {
863                                                 perror(filename);
864                                                 return NULL;
865                                         }
866                                         debug_noise("\t%s=%s\n", currif->option[currif->n_options].name,
867                                                         currif->option[currif->n_options].value);
868                                         currif->n_options++;
869                                         break;
870                                 case MAPPING:
871 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
872                                         if (strcmp(firstword, "script") == 0) {
873                                                 if (currmap->script != NULL) {
874                                                         bb_error_msg("duplicate script in mapping \"%s\"", buf);
875                                                         return NULL;
876                                                 } else {
877                                                         currmap->script = bb_xstrdup(next_word(&buf_ptr));
878                                                 }
879                                         } else if (strcmp(firstword, "map") == 0) {
880                                                 if (currmap->max_mappings == currmap->n_mappings) {
881                                                         currmap->max_mappings = currmap->max_mappings * 2 + 1;
882                                                         currmap->mapping = xrealloc(currmap->mapping, sizeof(char *) * currmap->max_mappings);
883                                                 }
884                                                 currmap->mapping[currmap->n_mappings] = bb_xstrdup(next_word(&buf_ptr));
885                                                 currmap->n_mappings++;
886                                         } else {
887                                                 bb_error_msg("misplaced option \"%s\"", buf);
888                                                 return NULL;
889                                         }
890 #endif
891                                         break;
892                                 case NONE:
893                                 default:
894                                         bb_error_msg("misplaced option \"%s\"", buf);
895                                         return NULL;
896                         }
897                 }
898                 free(buf);
899         }
900         if (ferror(f) != 0) {
901                 bb_perror_msg_and_die("%s", filename);
902         }
903         fclose(f);
904
905         return defn;
906 }
907
908 static char *setlocalenv(char *format, char *name, char *value)
909 {
910         char *result;
911         char *here;
912         char *there;
913
914         result = xmalloc(bb_strlen(format) + bb_strlen(name) + bb_strlen(value) + 1);
915
916         sprintf(result, format, name, value);
917
918         for (here = there = result; *there != '=' && *there; there++) {
919                 if (*there == '-')
920                         *there = '_';
921                 if (isalpha(*there))
922                         *there = toupper(*there);
923
924                 if (isalnum(*there) || *there == '_') {
925                         *here = *there;
926                         here++;
927                 }
928         }
929         memmove(here, there, bb_strlen(there) + 1);
930
931         return result;
932 }
933
934 static void set_environ(struct interface_defn_t *iface, char *mode)
935 {
936         char **environend;
937         int i;
938         const int n_env_entries = iface->n_options + 5;
939         char **ppch;
940
941         if (__myenviron != NULL) {
942                 for (ppch = __myenviron; *ppch; ppch++) {
943                         free(*ppch);
944                         *ppch = NULL;
945                 }
946                 free(__myenviron);
947                 __myenviron = NULL;
948         }
949         __myenviron = xmalloc(sizeof(char *) * (n_env_entries + 1 /* for final NULL */ ));
950         environend = __myenviron;
951         *environend = NULL;
952
953         for (i = 0; i < iface->n_options; i++) {
954                 if (strcmp(iface->option[i].name, "up") == 0
955                                 || strcmp(iface->option[i].name, "down") == 0
956                                 || strcmp(iface->option[i].name, "pre-up") == 0
957                                 || strcmp(iface->option[i].name, "post-down") == 0) {
958                         continue;
959                 }
960                 *(environend++) = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
961                 *environend = NULL;
962         }
963
964         *(environend++) = setlocalenv("%s=%s", "IFACE", iface->iface);
965         *environend = NULL;
966         *(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
967         *environend = NULL;
968         *(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name);
969         *environend = NULL;
970         *(environend++) = setlocalenv("%s=%s", "MODE", mode);
971         *environend = NULL;
972         *(environend++) = setlocalenv("%s=%s", "PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin");
973         *environend = NULL;
974 }
975
976 static int doit(char *str)
977 {
978         if (verbose || no_act) {
979                 printf("%s\n", str);
980         }
981         if (!no_act) {
982                 pid_t child;
983                 int status;
984
985                 fflush(NULL);
986                 switch (child = fork()) {
987                         case -1:                /* failure */
988                                 return 0;
989                         case 0:         /* child */
990                                 execle(DEFAULT_SHELL, DEFAULT_SHELL, "-c", str, NULL, __myenviron);
991                                 exit(127);
992                 }
993                 waitpid(child, &status, 0);
994                 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
995                         return 0;
996                 }
997         }
998         return (1);
999 }
1000
1001 static int execute_all(struct interface_defn_t *ifd, execfn *exec, const char *opt)
1002 {
1003         int i;
1004         char *buf;
1005         for (i = 0; i < ifd->n_options; i++) {
1006                 if (strcmp(ifd->option[i].name, opt) == 0) {
1007                         if (!(*exec) (ifd->option[i].value)) {
1008                                 return 0;
1009                         }
1010                 }
1011         }
1012
1013         buf = bb_xasprintf("run-parts /etc/network/if-%s.d", opt);
1014         if ((*exec)(buf) != 1) {
1015                 return 0;
1016         }
1017         return 1;
1018 }
1019
1020 static int check(char *str) {
1021         return str != NULL;
1022 }
1023
1024 static int iface_up(struct interface_defn_t *iface)
1025 {
1026         if (!iface->method->up(iface,check)) return -1;
1027         set_environ(iface, "start");
1028         if (!execute_all(iface, doit, "pre-up")) return 0;
1029         if (!iface->method->up(iface, doit)) return 0;
1030         if (!execute_all(iface, doit, "up")) return 0;
1031         return 1;
1032 }
1033
1034 static int iface_down(struct interface_defn_t *iface)
1035 {
1036         if (!iface->method->down(iface,check)) return -1;
1037         set_environ(iface, "stop");
1038         if (!execute_all(iface, doit, "down")) return 0;
1039         if (!iface->method->down(iface, doit)) return 0;
1040         if (!execute_all(iface, doit, "post-down")) return 0;
1041         return 1;
1042 }
1043
1044 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1045 static int popen2(FILE **in, FILE **out, char *command, ...)
1046 {
1047         va_list ap;
1048         char *argv[11] = { command };
1049         int argc;
1050         int infd[2], outfd[2];
1051         pid_t pid;
1052
1053         argc = 1;
1054         va_start(ap, command);
1055         while ((argc < 10) && (argv[argc] = va_arg(ap, char *))) {
1056                 argc++;
1057         }
1058         argv[argc] = NULL;      /* make sure */
1059         va_end(ap);
1060
1061         if (pipe(infd) != 0) {
1062                 return 0;
1063         }
1064
1065         if (pipe(outfd) != 0) {
1066                 close(infd[0]);
1067                 close(infd[1]);
1068                 return 0;
1069         }
1070
1071         fflush(NULL);
1072         switch (pid = fork()) {
1073                 case -1:                        /* failure */
1074                         close(infd[0]);
1075                         close(infd[1]);
1076                         close(outfd[0]);
1077                         close(outfd[1]);
1078                         return 0;
1079                 case 0:                 /* child */
1080                         dup2(infd[0], 0);
1081                         dup2(outfd[1], 1);
1082                         close(infd[0]);
1083                         close(infd[1]);
1084                         close(outfd[0]);
1085                         close(outfd[1]);
1086                         execvp(command, argv);
1087                         exit(127);
1088                 default:                        /* parent */
1089                         *in = fdopen(infd[1], "w");
1090                         *out = fdopen(outfd[0], "r");
1091                         close(infd[0]);
1092                         close(outfd[1]);
1093                         return pid;
1094         }
1095         /* unreached */
1096 }
1097
1098 static char *run_mapping(char *physical, struct mapping_defn_t * map)
1099 {
1100         FILE *in, *out;
1101         int i, status;
1102         pid_t pid;
1103
1104         char *logical = bb_xstrdup(physical);
1105
1106         /* Run the mapping script. */
1107         pid = popen2(&in, &out, map->script, physical, NULL);
1108
1109         /* popen2() returns 0 on failure. */
1110         if (pid == 0)
1111                 return logical;
1112
1113         /* Write mappings to stdin of mapping script. */
1114         for (i = 0; i < map->n_mappings; i++) {
1115                 fprintf(in, "%s\n", map->mapping[i]);
1116         }
1117         fclose(in);
1118         waitpid(pid, &status, 0);
1119
1120         if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
1121                 /* If the mapping script exited successfully, try to
1122                  * grab a line of output and use that as the name of the
1123                  * logical interface. */
1124                 char *new_logical = (char *)xmalloc(MAX_INTERFACE_LENGTH);
1125
1126                 if (fgets(new_logical, MAX_INTERFACE_LENGTH, out)) {
1127                         /* If we are able to read a line of output from the script,
1128                          * remove any trailing whitespace and use this value
1129                          * as the name of the logical interface. */
1130                         char *pch = new_logical + bb_strlen(new_logical) - 1;
1131
1132                         while (pch >= new_logical && isspace(*pch))
1133                                 *(pch--) = '\0';
1134
1135                         free(logical);
1136                         logical = new_logical;
1137                 } else {
1138                         /* If we are UNABLE to read a line of output, discard are
1139                          * freshly allocated memory. */
1140                         free(new_logical);
1141                 }
1142         }
1143
1144         fclose(out);
1145
1146         return logical;
1147 }
1148 #endif /* CONFIG_FEATURE_IFUPDOWN_MAPPING */
1149
1150 static llist_t *find_iface_state(llist_t *state_list, const char *iface)
1151 {
1152         unsigned short iface_len = bb_strlen(iface);
1153         llist_t *search = state_list;
1154
1155         while (search) {
1156                 if ((strncmp(search->data, iface, iface_len) == 0) &&
1157                                 (search->data[iface_len] == '=')) {
1158                         return(search);
1159                 }
1160                 search = search->link;
1161         }
1162         return(NULL);
1163 }
1164
1165 extern int ifupdown_main(int argc, char **argv)
1166 {
1167         int (*cmds) (struct interface_defn_t *) = NULL;
1168         struct interfaces_file_t *defn;
1169         FILE *state_fp = NULL;
1170         llist_t *state_list = NULL;
1171         llist_t *target_list = NULL;
1172         const char *interfaces = "/etc/network/interfaces";
1173         const char *statefile = "/var/run/ifstate";
1174
1175 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1176         int run_mappings = 1;
1177 #endif
1178         int do_all = 0;
1179         int force = 0;
1180         int any_failures = 0;
1181         int i;
1182
1183         if (bb_applet_name[2] == 'u') {
1184                 /* ifup command */
1185                 cmds = iface_up;
1186         } else {
1187                 /* ifdown command */
1188                 cmds = iface_down;
1189         }
1190
1191 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1192         while ((i = getopt(argc, argv, "i:hvnamf")) != -1)
1193 #else
1194                 while ((i = getopt(argc, argv, "i:hvnaf")) != -1)
1195 #endif
1196                 {
1197                         switch (i) {
1198                                 case 'i':       /* interfaces */
1199                                         interfaces = optarg;
1200                                         break;
1201                                 case 'v':       /* verbose */
1202                                         verbose = 1;
1203                                         break;
1204                                 case 'a':       /* all */
1205                                         do_all = 1;
1206                                         break;
1207                                 case 'n':       /* no-act */
1208                                         no_act = 1;
1209                                         break;
1210 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1211                                 case 'm':       /* no-mappings */
1212                                         run_mappings = 0;
1213                                         break;
1214 #endif
1215                                 case 'f':       /* force */
1216                                         force = 1;
1217                                         break;
1218                                 default:
1219                                         bb_show_usage();
1220                                         break;
1221                         }
1222                 }
1223
1224         if (argc - optind > 0) {
1225                 if (do_all) {
1226                         bb_show_usage();
1227                 }
1228         } else {
1229                 if (!do_all) {
1230                         bb_show_usage();
1231                 }
1232         }
1233
1234         debug_noise("reading %s file:\n", interfaces);
1235         defn = read_interfaces(interfaces);
1236         debug_noise("\ndone reading %s\n\n", interfaces);
1237
1238         if (!defn) {
1239                 exit(EXIT_FAILURE);
1240         }
1241
1242         if (no_act) {
1243                 state_fp = fopen(statefile, "r");
1244         }
1245
1246         /* Create a list of interfaces to work on */
1247         if (do_all) {
1248                 if (cmds == iface_up) {
1249                         target_list = defn->autointerfaces;
1250                 } else {
1251 #if 0
1252                         /* iface_down */
1253                         llist_t *new_item;
1254                         const llist_t *list = state_list;
1255                         while (list) {
1256                                 new_item = xmalloc(sizeof(llist_t));
1257                                 new_item->data = strdup(list->data);
1258                                 new_item->link = NULL;
1259                                 list = target_list;
1260                                 if (list == NULL)
1261                                         target_list = new_item;
1262                                 else {
1263                                         while (list->link) {
1264                                                 list = list->link;
1265                                         }
1266                                         list = new_item;
1267                                 }
1268                                 list = list->link;
1269                         }
1270                         target_list = defn->autointerfaces;
1271 #else
1272
1273                         /* iface_down */
1274                         const llist_t *list = state_list;
1275                         while (list) {
1276                                 target_list = llist_add_to_end(target_list, strdup(list->data));
1277                                 list = list->link;
1278                         }
1279                         target_list = defn->autointerfaces;
1280 #endif
1281                 }
1282         } else {
1283                 target_list = llist_add_to_end(target_list, argv[optind]);
1284         }
1285
1286
1287         /* Update the interfaces */
1288         while (target_list) {
1289                 llist_t *iface_list;
1290                 struct interface_defn_t *currif;
1291                 char *iface;
1292                 char *liface;
1293                 char *pch;
1294                 int okay = 0;
1295                 int cmds_ret;
1296
1297                 iface = strdup(target_list->data);
1298                 target_list = target_list->link;
1299
1300                 pch = strchr(iface, '=');
1301                 if (pch) {
1302                         *pch = '\0';
1303                         liface = strdup(pch + 1);
1304                 } else {
1305                         liface = strdup(iface);
1306                 }
1307
1308                 if (!force) {
1309                         const llist_t *iface_state = find_iface_state(state_list, iface);
1310
1311                         if (cmds == iface_up) {
1312                                 /* ifup */
1313                                 if (iface_state) {
1314                                         bb_error_msg("interface %s already configured", iface);
1315                                         continue;
1316                                 }
1317                         } else {
1318                                 /* ifdown */
1319                                 if (iface_state) {
1320                                         bb_error_msg("interface %s not configured", iface);
1321                                         continue;
1322                                 }
1323                         }
1324                 }
1325
1326 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1327                 if ((cmds == iface_up) && run_mappings) {
1328                         struct mapping_defn_t *currmap;
1329
1330                         for (currmap = defn->mappings; currmap; currmap = currmap->next) {
1331
1332                                 for (i = 0; i < currmap->n_matches; i++) {
1333                                         if (fnmatch(currmap->match[i], liface, 0) != 0)
1334                                                 continue;
1335                                         if (verbose) {
1336                                                 printf("Running mapping script %s on %s\n", currmap->script, liface);
1337                                         }
1338                                         liface = run_mapping(iface, currmap);
1339                                         break;
1340                                 }
1341                         }
1342                 }
1343 #endif
1344
1345
1346                 iface_list = defn->ifaces;
1347                 while (iface_list) {
1348                         currif = (struct interface_defn_t *) iface_list->data;
1349                         if (strcmp(liface, currif->iface) == 0) {
1350                                 char *oldiface = currif->iface;
1351
1352                                 okay = 1;
1353                                 currif->iface = iface;
1354
1355                                 debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name);
1356
1357                                 /* Call the cmds function pointer, does either iface_up() or iface_down() */
1358                                 cmds_ret = cmds(currif);
1359                                 if (cmds_ret == -1) {
1360                                         bb_error_msg("Don't seem to have all the variables for %s/%s.",
1361                                                         liface, currif->address_family->name);
1362                                         any_failures += 1;
1363                                 } else if (cmds_ret == 0) {
1364                                         any_failures += 1;
1365                                 }
1366
1367                                 currif->iface = oldiface;
1368                         }
1369                         iface_list = iface_list->link;
1370                 }
1371                 if (verbose) {
1372                         printf("\n");
1373                 }
1374
1375                 if (!okay && !force) {
1376                         bb_error_msg("Ignoring unknown interface %s", liface);
1377                         any_failures += 1;
1378                 } else {
1379                         llist_t *iface_state = find_iface_state(state_list, iface);
1380
1381                         if (cmds == iface_up) {
1382                                 char *newiface = xmalloc(bb_strlen(iface) + 1 + bb_strlen(liface) + 1);
1383                                 sprintf(newiface, "%s=%s", iface, liface);
1384                                 if (iface_state == NULL) {
1385                                         state_list = llist_add_to_end(state_list, newiface);
1386                                 } else {
1387                                         free(iface_state->data);
1388                                         iface_state->data = newiface;
1389                                 }
1390                         } else if (cmds == iface_down) {
1391                                 /* Remove an interface from the linked list */
1392                                 if (iface_state) {
1393                                         /* This needs to be done better */
1394                                         free(iface_state->data);
1395                                         free(iface_state->link);
1396                                         if (iface_state->link) {
1397                                                 iface_state->data = iface_state->link->data;
1398                                                 iface_state->link = iface_state->link->link;
1399                                         } else {
1400                                                 iface_state->data = NULL;
1401                                                 iface_state->link = NULL;
1402                                         }
1403                                 }
1404                         }
1405                 }
1406         }
1407
1408         /* Actually write the new state */
1409         if (!no_act) {
1410
1411                 if (state_fp)
1412                         fclose(state_fp);
1413                 state_fp = bb_xfopen(statefile, "a+");
1414
1415                 if (ftruncate(fileno(state_fp), 0) < 0) {
1416                         bb_error_msg_and_die("failed to truncate statefile %s: %s", statefile, strerror(errno));
1417                 }
1418
1419                 rewind(state_fp);
1420
1421                 while (state_list) {
1422                         if (state_list->data) {
1423                                 fputs(state_list->data, state_fp);
1424                                 fputc('\n', state_fp);
1425                         }
1426                         state_list = state_list->link;
1427                 }
1428                 fflush(state_fp);
1429         }
1430
1431         /* Cleanup */
1432         if (state_fp != NULL) {
1433                 fclose(state_fp);
1434                 state_fp = NULL;
1435         }
1436
1437         if (any_failures)
1438                 return 1;
1439         return 0;
1440 }