diff options
author | Glenn L McGrath <bug1@ihug.co.nz> | 2002-12-06 11:51:46 +0000 |
---|---|---|
committer | Glenn L McGrath <bug1@ihug.co.nz> | 2002-12-06 11:51:46 +0000 |
commit | 398ff9d9817ecfb77c3810fb9a03d866c1059b95 (patch) | |
tree | fa66044be1368a8cb7d58272e6d3964a41665a1f /networking/ifupdown.c | |
parent | cdbe5e5d4c79ab1fc47e3f7f05704b11b7945707 (diff) | |
download | busybox-w32-398ff9d9817ecfb77c3810fb9a03d866c1059b95.tar.gz busybox-w32-398ff9d9817ecfb77c3810fb9a03d866c1059b95.tar.bz2 busybox-w32-398ff9d9817ecfb77c3810fb9a03d866c1059b95.zip |
Use libbb/get_line_from_file, change default state file location
Diffstat (limited to 'networking/ifupdown.c')
-rw-r--r-- | networking/ifupdown.c | 167 |
1 files changed, 39 insertions, 128 deletions
diff --git a/networking/ifupdown.c b/networking/ifupdown.c index 36a12d77c..b230e1a6f 100644 --- a/networking/ifupdown.c +++ b/networking/ifupdown.c | |||
@@ -3,7 +3,10 @@ | |||
3 | * Based on ifupdown by Anthony Towns | 3 | * Based on ifupdown by Anthony Towns |
4 | * Copyright (c) 1999 Anthony Towns <aj@azure.humbug.org.au> | 4 | * Copyright (c) 1999 Anthony Towns <aj@azure.humbug.org.au> |
5 | * | 5 | * |
6 | * Changes to upstream version | ||
6 | * Remove checks for kernel version, assume kernel version 2.2.0 or better | 7 | * Remove checks for kernel version, assume kernel version 2.2.0 or better |
8 | * Lines in the interfaces file cannot wrap. | ||
9 | * The default state file is moved to /var/run/ifstate | ||
7 | * | 10 | * |
8 | * This program is free software; you can redistribute it and/or modify | 11 | * This program is free software; you can redistribute it and/or modify |
9 | * it under the terms of the GNU General Public License as published by | 12 | * it under the terms of the GNU General Public License as published by |
@@ -605,77 +608,6 @@ static int duplicate_if(interface_defn *ifa, interface_defn *ifb) | |||
605 | return(1); | 608 | return(1); |
606 | } | 609 | } |
607 | 610 | ||
608 | static int get_line(char **result, size_t * result_len, FILE * f, int *line) | ||
609 | { | ||
610 | size_t pos; | ||
611 | |||
612 | do { | ||
613 | pos = 0; | ||
614 | do { | ||
615 | if (*result_len - pos < 10) { | ||
616 | char *newstr = xrealloc(*result, *result_len * 2 + 80); | ||
617 | *result = newstr; | ||
618 | *result_len = *result_len * 2 + 80; | ||
619 | } | ||
620 | |||
621 | if (!fgets(*result + pos, *result_len - pos, f)) { | ||
622 | if (ferror(f) == 0 && pos == 0) | ||
623 | return 0; | ||
624 | if (ferror(f) != 0) | ||
625 | return 0; | ||
626 | } | ||
627 | pos += xstrlen(*result + pos); | ||
628 | } while (pos == *result_len - 1 && (*result)[pos - 1] != '\n'); | ||
629 | |||
630 | if (pos != 0 && (*result)[pos - 1] == '\n') { | ||
631 | (*result)[--pos] = '\0'; | ||
632 | } | ||
633 | |||
634 | (*line)++; | ||
635 | { | ||
636 | int first = 0; | ||
637 | |||
638 | while (isspace((*result)[first]) && (*result)[first]) { | ||
639 | first++; | ||
640 | } | ||
641 | |||
642 | memmove(*result, *result + first, pos - first + 1); | ||
643 | pos -= first; | ||
644 | } | ||
645 | } while ((*result)[0] == '#'); | ||
646 | |||
647 | while ((*result)[pos - 1] == '\\') { | ||
648 | (*result)[--pos] = '\0'; | ||
649 | do { | ||
650 | if (*result_len - pos < 10) { | ||
651 | char *newstr = xrealloc(*result, *result_len * 2 + 80); | ||
652 | *result = newstr; | ||
653 | *result_len = *result_len * 2 + 80; | ||
654 | } | ||
655 | |||
656 | if (!fgets(*result + pos, *result_len - pos, f)) { | ||
657 | if (ferror(f) == 0 && pos == 0) | ||
658 | return 0; | ||
659 | if (ferror(f) != 0) | ||
660 | return 0; | ||
661 | } | ||
662 | pos += xstrlen(*result + pos); | ||
663 | } while (pos == *result_len - 1 && (*result)[pos - 1] != '\n'); | ||
664 | |||
665 | if (pos != 0 && (*result)[pos - 1] == '\n') { | ||
666 | (*result)[--pos] = '\0'; | ||
667 | } | ||
668 | (*line)++; | ||
669 | } | ||
670 | |||
671 | while (isspace((*result)[pos - 1])) { /* remove trailing whitespace */ | ||
672 | pos--; | ||
673 | } | ||
674 | (*result)[pos] = '\0'; | ||
675 | |||
676 | return 1; | ||
677 | } | ||
678 | |||
679 | static interfaces_file *read_interfaces(char *filename) | 611 | static interfaces_file *read_interfaces(char *filename) |
680 | { | 612 | { |
681 | interface_defn *currif = NULL; | 613 | interface_defn *currif = NULL; |
@@ -687,8 +619,7 @@ static interfaces_file *read_interfaces(char *filename) | |||
687 | char firstword[80]; | 619 | char firstword[80]; |
688 | char *buf = NULL; | 620 | char *buf = NULL; |
689 | char *rest; | 621 | char *rest; |
690 | int line; | 622 | // int line; |
691 | size_t buf_len = 0; | ||
692 | 623 | ||
693 | enum { NONE, IFACE, MAPPING } currently_processing = NONE; | 624 | enum { NONE, IFACE, MAPPING } currently_processing = NONE; |
694 | 625 | ||
@@ -701,9 +632,20 @@ static interfaces_file *read_interfaces(char *filename) | |||
701 | if (f == NULL) { | 632 | if (f == NULL) { |
702 | return NULL; | 633 | return NULL; |
703 | } | 634 | } |
704 | line = 0; | ||
705 | 635 | ||
706 | while (get_line(&buf, &buf_len, f, &line)) { | 636 | while ((buf = get_line_from_file(f)) != NULL) { |
637 | char *end; | ||
638 | |||
639 | if (buf[0] == '#') { | ||
640 | continue; | ||
641 | } | ||
642 | end = last_char_is(buf, '\n'); | ||
643 | if (end) { | ||
644 | *end = '\0'; | ||
645 | } | ||
646 | while ((end = last_char_is(buf, ' ')) != NULL) { | ||
647 | *end = '\0'; | ||
648 | } | ||
707 | rest = next_word(buf, firstword, 80); | 649 | rest = next_word(buf, firstword, 80); |
708 | if (rest == NULL) { | 650 | if (rest == NULL) { |
709 | continue; /* blank line */ | 651 | continue; /* blank line */ |
@@ -763,12 +705,12 @@ static interfaces_file *read_interfaces(char *filename) | |||
763 | rest = next_word(rest, method_name, 80); | 705 | rest = next_word(rest, method_name, 80); |
764 | 706 | ||
765 | if (rest == NULL) { | 707 | if (rest == NULL) { |
766 | error_msg("%s:%d: too few parameters for iface line", filename, line); | 708 | error_msg("too few parameters for line \"%s\"", buf); |
767 | return NULL; | 709 | return NULL; |
768 | } | 710 | } |
769 | 711 | ||
770 | if (rest[0] != '\0') { | 712 | if (rest[0] != '\0') { |
771 | error_msg("%s:%d: too many parameters for iface line", filename, line); | 713 | error_msg("too many parameters \"%s\"", buf); |
772 | return NULL; | 714 | return NULL; |
773 | } | 715 | } |
774 | 716 | ||
@@ -776,13 +718,13 @@ static interfaces_file *read_interfaces(char *filename) | |||
776 | 718 | ||
777 | currif->address_family = get_address_family(addr_fams, address_family_name); | 719 | currif->address_family = get_address_family(addr_fams, address_family_name); |
778 | if (!currif->address_family) { | 720 | if (!currif->address_family) { |
779 | error_msg("%s:%d: unknown address type", filename, line); | 721 | error_msg("unknown address type \"%s\"", buf); |
780 | return NULL; | 722 | return NULL; |
781 | } | 723 | } |
782 | 724 | ||
783 | currif->method = get_method(currif->address_family, method_name); | 725 | currif->method = get_method(currif->address_family, method_name); |
784 | if (!currif->method) { | 726 | if (!currif->method) { |
785 | error_msg("%s:%d: unknown method", filename, line); | 727 | error_msg("unknown method \"%s\"", buf); |
786 | return NULL; | 728 | return NULL; |
787 | } | 729 | } |
788 | 730 | ||
@@ -797,7 +739,7 @@ static interfaces_file *read_interfaces(char *filename) | |||
797 | 739 | ||
798 | while (*where != NULL) { | 740 | while (*where != NULL) { |
799 | if (duplicate_if(*where, currif)) { | 741 | if (duplicate_if(*where, currif)) { |
800 | error_msg("%s:%d: duplicate interface", filename, line); | 742 | error_msg("duplicate interface \"%s\"", buf); |
801 | return NULL; | 743 | return NULL; |
802 | } | 744 | } |
803 | where = &(*where)->next; | 745 | where = &(*where)->next; |
@@ -814,7 +756,7 @@ static interfaces_file *read_interfaces(char *filename) | |||
814 | 756 | ||
815 | for (i = 0; i < defn->n_autointerfaces; i++) { | 757 | for (i = 0; i < defn->n_autointerfaces; i++) { |
816 | if (strcmp(firstword, defn->autointerfaces[i]) == 0) { | 758 | if (strcmp(firstword, defn->autointerfaces[i]) == 0) { |
817 | perror_msg("%s:%d: interface declared auto twice", filename, line); | 759 | perror_msg("interface declared auto twice \"%s\"", buf); |
818 | return NULL; | 760 | return NULL; |
819 | } | 761 | } |
820 | } | 762 | } |
@@ -839,7 +781,7 @@ static interfaces_file *read_interfaces(char *filename) | |||
839 | int i; | 781 | int i; |
840 | 782 | ||
841 | if (xstrlen(rest) == 0) { | 783 | if (xstrlen(rest) == 0) { |
842 | error_msg("%s:%d: option with empty value", filename, line); | 784 | error_msg("option with empty value \"%s\"", buf); |
843 | return NULL; | 785 | return NULL; |
844 | } | 786 | } |
845 | 787 | ||
@@ -849,7 +791,7 @@ static interfaces_file *read_interfaces(char *filename) | |||
849 | && strcmp(firstword, "post-down") != 0) { | 791 | && strcmp(firstword, "post-down") != 0) { |
850 | for (i = 0; i < currif->n_options; i++) { | 792 | for (i = 0; i < currif->n_options; i++) { |
851 | if (strcmp(currif->option[i].name, firstword) == 0) { | 793 | if (strcmp(currif->option[i].name, firstword) == 0) { |
852 | error_msg("%s:%d: duplicate option", filename, line); | 794 | error_msg("duplicate option \"%s\"", buf); |
853 | return NULL; | 795 | return NULL; |
854 | } | 796 | } |
855 | } | 797 | } |
@@ -878,7 +820,7 @@ static interfaces_file *read_interfaces(char *filename) | |||
878 | #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING | 820 | #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING |
879 | if (strcmp(firstword, "script") == 0) { | 821 | if (strcmp(firstword, "script") == 0) { |
880 | if (currmap->script != NULL) { | 822 | if (currmap->script != NULL) { |
881 | error_msg("%s:%d: duplicate script in mapping", filename, line); | 823 | error_msg("duplicate script in mapping \"%s\"", buf); |
882 | return NULL; | 824 | return NULL; |
883 | } else { | 825 | } else { |
884 | currmap->script = xstrdup(rest); | 826 | currmap->script = xstrdup(rest); |
@@ -891,14 +833,14 @@ static interfaces_file *read_interfaces(char *filename) | |||
891 | currmap->mapping[currmap->n_mappings] = xstrdup(rest); | 833 | currmap->mapping[currmap->n_mappings] = xstrdup(rest); |
892 | currmap->n_mappings++; | 834 | currmap->n_mappings++; |
893 | } else { | 835 | } else { |
894 | error_msg("%s:%d: misplaced option", filename, line); | 836 | error_msg("misplaced option \"%s\"", buf); |
895 | return NULL; | 837 | return NULL; |
896 | } | 838 | } |
897 | #endif | 839 | #endif |
898 | break; | 840 | break; |
899 | case NONE: | 841 | case NONE: |
900 | default: | 842 | default: |
901 | error_msg("%s:%d: misplaced option", filename, line); | 843 | error_msg("misplaced option \"%s\"", buf); |
902 | return NULL; | 844 | return NULL; |
903 | } | 845 | } |
904 | } | 846 | } |
@@ -908,7 +850,6 @@ static interfaces_file *read_interfaces(char *filename) | |||
908 | return NULL; | 850 | return NULL; |
909 | } | 851 | } |
910 | fclose(f); | 852 | fclose(f); |
911 | line = -1; | ||
912 | 853 | ||
913 | return defn; | 854 | return defn; |
914 | } | 855 | } |
@@ -1187,10 +1128,12 @@ extern int ifupdown_main(int argc, char **argv) | |||
1187 | char **target_iface = NULL; | 1128 | char **target_iface = NULL; |
1188 | char **state = NULL; /* list of iface=liface */ | 1129 | char **state = NULL; /* list of iface=liface */ |
1189 | char *interfaces = "/etc/network/interfaces"; | 1130 | char *interfaces = "/etc/network/interfaces"; |
1190 | char *statefile = "/etc/network/ifstate"; | 1131 | char *statefile = "/var/run/ifstate"; |
1191 | 1132 | ||
1192 | int do_all = 0; | 1133 | #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING |
1193 | int run_mappings = 1; | 1134 | int run_mappings = 1; |
1135 | #endif | ||
1136 | int do_all = 0; | ||
1194 | int force = 0; | 1137 | int force = 0; |
1195 | int n_target_ifaces = 0; | 1138 | int n_target_ifaces = 0; |
1196 | int n_state = 0; | 1139 | int n_state = 0; |
@@ -1258,45 +1201,13 @@ extern int ifupdown_main(int argc, char **argv) | |||
1258 | } | 1201 | } |
1259 | 1202 | ||
1260 | if (state_fp != NULL) { | 1203 | if (state_fp != NULL) { |
1261 | char buf[80]; | 1204 | char *start; |
1262 | char *p; | 1205 | while ((start = get_line_from_file(state_fp)) != NULL) { |
1263 | #if 0 | 1206 | char *end_ptr; |
1264 | if (!no_act) { | 1207 | /* We should only need to check for a single character */ |
1265 | int flags; | 1208 | end_ptr = start + strcspn(start, " \t\n"); |
1266 | struct flock lock; | 1209 | *end_ptr = '\0'; |
1267 | const int state_fd = fileno(state_fp); | 1210 | add_to_state(&state, &n_state, &max_state, start); |
1268 | |||
1269 | flags = fcntl(state_fd, F_GETFD); | ||
1270 | if ((flags < 0) || (fcntl(state_fd, F_SETFD, flags | FD_CLOEXEC) < 0)) { | ||
1271 | perror_msg_and_die("failed to set FD_CLOEXEC on statefile %s", statefile); | ||
1272 | } | ||
1273 | |||
1274 | lock.l_type = F_WRLCK; | ||
1275 | lock.l_whence = SEEK_SET; | ||
1276 | lock.l_start = 0; | ||
1277 | lock.l_len = 0; | ||
1278 | |||
1279 | if (fcntl(state_fd, F_SETLKW, &lock) < 0) { | ||
1280 | perror_msg_and_die("failed to lock statefile %s", statefile); | ||
1281 | } | ||
1282 | } | ||
1283 | #endif | ||
1284 | rewind(state_fp); | ||
1285 | while ((p = fgets(buf, sizeof buf, state_fp)) != NULL) { | ||
1286 | char *pch; | ||
1287 | |||
1288 | pch = buf + xstrlen(buf) - 1; | ||
1289 | while (pch > buf && isspace(*pch)) { | ||
1290 | pch--; | ||
1291 | } | ||
1292 | *(pch + 1) = '\0'; | ||
1293 | |||
1294 | pch = buf; | ||
1295 | while (isspace(*pch)) { | ||
1296 | pch++; | ||
1297 | } | ||
1298 | |||
1299 | add_to_state(&state, &n_state, &max_state, xstrdup(pch)); | ||
1300 | } | 1211 | } |
1301 | } | 1212 | } |
1302 | 1213 | ||