Ignore:
Timestamp:
Jan 1, 2014, 12:47:38 AM (10 years ago)
Author:
Bruno Cornec
Message:
  • Update mindi-busybox to 1.21.1
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/3.2/mindi-busybox/networking/ifupdown.c

    r2725 r3232  
    1818 */
    1919
     20//usage:#define ifup_trivial_usage
     21//usage:       "[-an"IF_FEATURE_IFUPDOWN_MAPPING("m")"vf] [-i FILE] IFACE..."
     22//usage:#define ifup_full_usage "\n\n"
     23//usage:       "    -a  De/configure all interfaces automatically"
     24//usage:     "\n    -i FILE Use FILE for interface definitions"
     25//usage:     "\n    -n  Print out what would happen, but don't do it"
     26//usage:    IF_FEATURE_IFUPDOWN_MAPPING(
     27//usage:     "\n        (note: doesn't disable mappings)"
     28//usage:     "\n    -m  Don't run any mappings"
     29//usage:    )
     30//usage:     "\n    -v  Print out what would happen before doing it"
     31//usage:     "\n    -f  Force de/configuration"
     32//usage:
     33//usage:#define ifdown_trivial_usage
     34//usage:       "[-an"IF_FEATURE_IFUPDOWN_MAPPING("m")"vf] [-i FILE] IFACE..."
     35//usage:#define ifdown_full_usage "\n\n"
     36//usage:       "    -a  De/configure all interfaces automatically"
     37//usage:     "\n    -i FILE Use FILE for interface definitions"
     38//usage:     "\n    -n  Print out what would happen, but don't do it"
     39//usage:    IF_FEATURE_IFUPDOWN_MAPPING(
     40//usage:     "\n        (note: doesn't disable mappings)"
     41//usage:     "\n    -m  Don't run any mappings"
     42//usage:    )
     43//usage:     "\n    -v  Print out what would happen before doing it"
     44//usage:     "\n    -f  Force de/configuration"
     45
    2046#include "libbb.h"
    2147/* After libbb.h, since it needs sys/types.h on some systems */
     
    2450
    2551#define MAX_OPT_DEPTH 10
    26 #define EUNBALBRACK 10001
    27 #define EUNDEFVAR   10002
    28 #define EUNBALPER   10000
    2952
    3053#if ENABLE_FEATURE_IFUPDOWN_MAPPING
     
    6285    char *script;
    6386
    64     int max_mappings;
    6587    int n_mappings;
    6688    char **mapping;
     
    7799
    78100    char *iface;
    79     int max_options;
    80101    int n_options;
    81102    struct variable_t *option;
     
    107128    char **my_environ;
    108129    const char *startup_PATH;
     130    char *shell;
    109131} FIX_ALIASING;
    110132#define G (*(struct globals*)&bb_common_bufsiz1)
    111133#define INIT_G() do { } while (0)
     134
     135
     136static const char keywords_up_down[] ALIGN1 =
     137    "up\0"
     138    "down\0"
     139    "pre-up\0"
     140    "post-down\0"
     141;
    112142
    113143
     
    201231{
    202232    size_t old_pos[MAX_OPT_DEPTH] = { 0 };
    203     int okay[MAX_OPT_DEPTH] = { 1 };
     233    smallint okay[MAX_OPT_DEPTH] = { 1 };
    204234    int opt_depth = 1;
    205235    char *result = NULL;
     
    212242            break;
    213243        case '\\':
    214             if (command[1]) {
    215                 addstr(&result, command + 1, 1);
    216                 command += 2;
    217             } else {
    218                 addstr(&result, command, 1);
     244            if (command[1])
    219245                command++;
    220             }
     246            addstr(&result, command, 1);
     247            command++;
    221248            break;
    222249        case '[':
     
    227254                command += 2;
    228255            } else {
    229                 addstr(&result, "[", 1);
     256                addstr(&result, command, 1);
    230257                command++;
    231258            }
     
    239266                command += 2;
    240267            } else {
    241                 addstr(&result, "]", 1);
     268                addstr(&result, command, 1);
    242269                command++;
    243270            }
     
    251278                nextpercent = strchr(command, '%');
    252279                if (!nextpercent) {
    253                     errno = EUNBALPER;
     280                    /* Unterminated %var% */
    254281                    free(result);
    255282                    return NULL;
     
    296323
    297324    if (opt_depth > 1) {
    298         errno = EUNBALBRACK;
     325        /* Unbalanced bracket */
    299326        free(result);
    300327        return NULL;
     
    302329
    303330    if (!okay[0]) {
    304         errno = EUNDEFVAR;
     331        /* Undefined variable and we aren't in a bracket */
    305332        free(result);
    306333        return NULL;
     
    369396    result += execute("ip link set[[ mtu %mtu%]][[ addr %hwaddress%]] %iface% up", ifd, exec);
    370397    /* Was: "[[ ip ....%gateway% ]]". Removed extra spaces w/o checking */
    371     result += execute("[[ip route add ::/0 via %gateway%]]", ifd, exec);
     398    result += execute("[[ip route add ::/0 via %gateway%]][[ prio %metric%]]", ifd, exec);
    372399# else
    373400    result = execute("ifconfig %iface%[[ media %media%]][[ hw %hwaddress%]][[ mtu %mtu%]] up", ifd, exec);
    374401    result += execute("ifconfig %iface% add %address%/%netmask%", ifd, exec);
    375     result += execute("[[route -A inet6 add ::/0 gw %gateway%]]", ifd, exec);
     402    result += execute("[[route -A inet6 add ::/0 gw %gateway%[[ metric %metric%]]]]", ifd, exec);
    376403# endif
    377404    return ((result == 3) ? 3 : 0);
     
    456483            "dev %iface%[[ peer %pointopoint%]][[ label %label%]]", ifd, exec);
    457484    result += execute("ip link set[[ mtu %mtu%]][[ addr %hwaddress%]] %iface% up", ifd, exec);
    458     result += execute("[[ip route add default via %gateway% dev %iface%]]", ifd, exec);
     485    result += execute("[[ip route add default via %gateway% dev %iface%[[ prio %metric%]]]]", ifd, exec);
    459486    return ((result == 3) ? 3 : 0);
    460487# else
     
    466493                "[[ broadcast %broadcast%]][[ pointopoint %pointopoint%]] ",
    467494                ifd, exec);
    468     result += execute("[[route add default gw %gateway% %iface%]]", ifd, exec);
     495    result += execute("[[route add default gw %gateway%[[ metric %metric%]] %iface%]]", ifd, exec);
    469496    return ((result == 3) ? 3 : 0);
    470497# endif
     
    777804                currmap->match[currmap->n_matches++] = xstrdup(first_word);
    778805            }
    779             /*currmap->max_mappings = 0; - done by xzalloc */
    780806            /*currmap->n_mappings = 0;*/
    781807            /*currmap->mapping = NULL;*/
     
    862888                    bb_error_msg_and_die("option with empty value \"%s\"", buf);
    863889
    864                 if (strcmp(first_word, "up") != 0
    865                  && strcmp(first_word, "down") != 0
    866                  && strcmp(first_word, "pre-up") != 0
    867                  && strcmp(first_word, "post-down") != 0
    868                 ) {
     890                if (strcmp(first_word, "post-up") == 0)
     891                    first_word += 5; /* "up" */
     892                else if (strcmp(first_word, "pre-down") == 0)
     893                    first_word += 4; /* "down" */
     894
     895                /* If not one of "up", "down",... words... */
     896                if (index_in_strings(keywords_up_down, first_word) < 0) {
    869897                    int i;
    870898                    for (i = 0; i < currif->n_options; i++) {
     
    873901                    }
    874902                }
    875                 if (currif->n_options >= currif->max_options) {
    876                     currif->max_options += 10;
    877                     currif->option = xrealloc(currif->option,
    878                         sizeof(*currif->option) * currif->max_options);
    879                 }
    880903                debug_noise("\t%s=%s\n", first_word, rest_of_line);
     904                currif->option = xrealloc_vector(currif->option, 4, currif->n_options);
    881905                currif->option[currif->n_options].name = xstrdup(first_word);
    882906                currif->option[currif->n_options].value = xstrdup(rest_of_line);
     
    890914                    currmap->script = xstrdup(next_word(&rest_of_line));
    891915                } else if (strcmp(first_word, "map") == 0) {
    892                     if (currmap->n_mappings >= currmap->max_mappings) {
    893                         currmap->max_mappings = currmap->max_mappings * 2 + 1;
    894                         currmap->mapping = xrealloc(currmap->mapping,
    895                             sizeof(char *) * currmap->max_mappings);
    896                     }
     916                    currmap->mapping = xrealloc_vector(currmap->mapping, 2, currmap->n_mappings);
    897917                    currmap->mapping[currmap->n_mappings] = xstrdup(next_word(&rest_of_line));
    898918                    currmap->n_mappings++;
     
    941961}
    942962
    943 static void set_environ(struct interface_defn_t *iface, const char *mode)
     963static void set_environ(struct interface_defn_t *iface, const char *mode, const char *opt)
    944964{
    945965    int i;
     
    954974
    955975    /* note: last element will stay NULL: */
    956     G.my_environ = xzalloc(sizeof(char *) * (iface->n_options + 6));
     976    G.my_environ = xzalloc(sizeof(char *) * (iface->n_options + 7));
    957977    pp = G.my_environ;
    958978
    959979    for (i = 0; i < iface->n_options; i++) {
    960         if (strcmp(iface->option[i].name, "up") == 0
    961          || strcmp(iface->option[i].name, "down") == 0
    962          || strcmp(iface->option[i].name, "pre-up") == 0
    963          || strcmp(iface->option[i].name, "post-down") == 0
    964         ) {
     980        if (index_in_strings(keywords_up_down, iface->option[i].name) >= 0) {
    965981            continue;
    966982        }
     
    972988    *pp++ = setlocalenv("%s=%s", "METHOD", iface->method->name);
    973989    *pp++ = setlocalenv("%s=%s", "MODE", mode);
     990    *pp++ = setlocalenv("%s=%s", "PHASE", opt);
    974991    if (G.startup_PATH)
    975992        *pp++ = setlocalenv("%s=%s", "PATH", G.startup_PATH);
     
    9871004        fflush_all();
    9881005        child = vfork();
    989         switch (child) {
    990         case -1: /* failure */
     1006        if (child < 0) /* failure */
    9911007            return 0;
    992         case 0: /* child */
    993             execle(DEFAULT_SHELL, DEFAULT_SHELL, "-c", str, (char *) NULL, G.my_environ);
     1008        if (child == 0) { /* child */
     1009            execle(G.shell, G.shell, "-c", str, (char *) NULL, G.my_environ);
    9941010            _exit(127);
    9951011        }
     
    10271043{
    10281044    if (!iface->method->up(iface, check)) return -1;
    1029     set_environ(iface, "start");
     1045    set_environ(iface, "start", "pre-up");
    10301046    if (!execute_all(iface, "pre-up")) return 0;
    10311047    if (!iface->method->up(iface, doit)) return 0;
     1048    set_environ(iface, "start", "post-up");
    10321049    if (!execute_all(iface, "up")) return 0;
    10331050    return 1;
     
    10361053static int iface_down(struct interface_defn_t *iface)
    10371054{
    1038     if (!iface->method->down(iface,check)) return -1;
    1039     set_environ(iface, "stop");
     1055    if (!iface->method->down(iface, check)) return -1;
     1056    set_environ(iface, "stop", "pre-down");
    10401057    if (!execute_all(iface, "down")) return 0;
    10411058    if (!iface->method->down(iface, doit)) return 0;
     1059    set_environ(iface, "stop", "post-down");
    10421060    if (!execute_all(iface, "post-down")) return 0;
    10431061    return 1;
     
    11661184
    11671185    G.startup_PATH = getenv("PATH");
     1186    G.shell = xstrdup(get_shell_name());
    11681187
    11691188    cmds = iface_down;
     
    12211240                if (iface_state) {
    12221241                    bb_error_msg("interface %s already configured", iface);
    1223                     continue;
     1242                    goto next;
    12241243                }
    12251244            } else {
     
    12271246                if (!iface_state) {
    12281247                    bb_error_msg("interface %s not configured", iface);
    1229                     continue;
     1248                    goto next;
    12301249                }
    12311250            }
     
    12911310            llist_t *iface_state = find_iface_state(state_list, iface);
    12921311
    1293             if (cmds == iface_up) {
    1294                 char * const newiface = xasprintf("%s=%s", iface, liface);
    1295                 if (iface_state == NULL) {
     1312            if (cmds == iface_up && !any_failures) {
     1313                char *newiface = xasprintf("%s=%s", iface, liface);
     1314                if (!iface_state) {
    12961315                    llist_add_to_end(&state_list, newiface);
    12971316                } else {
     
    13171336            llist_free(state_list, free);
    13181337        }
     1338 next:
     1339        free(iface);
     1340        free(liface);
    13191341    }
    13201342
Note: See TracChangeset for help on using the changeset viewer.