ifupdown: if $PATH is not set, do not set it to "" in children
authorDenys Vlasenko <vda.linux@googlemail.com>
Fri, 5 Jun 2009 19:53:11 +0000 (21:53 +0200)
committerDenys Vlasenko <vda.linux@googlemail.com>
Fri, 5 Jun 2009 19:53:11 +0000 (21:53 +0200)
Also random code shrink:

function                                             old     new   delta
set_environ                                          328     330      +2
static.label_buf                                       4       -      -4
startup_PATH                                           4       -      -4
my_environ                                             4       -      -4
ifupdown_main                                       2148    2134     -14
setlocalenv                                          117      90     -27
get_var                                              139     112     -27
------------------------------------------------------------------------------
(add/remove: 0/3 grow/shrink: 1/3 up/down: 2/-80)             Total: -78 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
networking/ifupdown.c

index 6d608105e0223389382bc4193e0b376c7844162f..7a9709e403bc557de85f0963c494cbc06bc3c169 100644 (file)
@@ -87,23 +87,29 @@ struct interfaces_file_t {
        struct mapping_defn_t *mappings;
 };
 
+
 #define OPTION_STR "anvf" IF_FEATURE_IFUPDOWN_MAPPING("m") "i:"
 enum {
-       OPT_do_all = 0x1,
-       OPT_no_act = 0x2,
-       OPT_verbose = 0x4,
-       OPT_force = 0x8,
+       OPT_do_all      = 0x1,
+       OPT_no_act      = 0x2,
+       OPT_verbose     = 0x4,
+       OPT_force       = 0x8,
        OPT_no_mappings = 0x10,
 };
-#define DO_ALL (option_mask32 & OPT_do_all)
-#define NO_ACT (option_mask32 & OPT_no_act)
-#define VERBOSE (option_mask32 & OPT_verbose)
-#define FORCE (option_mask32 & OPT_force)
+#define DO_ALL      (option_mask32 & OPT_do_all)
+#define NO_ACT      (option_mask32 & OPT_no_act)
+#define VERBOSE     (option_mask32 & OPT_verbose)
+#define FORCE       (option_mask32 & OPT_force)
 #define NO_MAPPINGS (option_mask32 & OPT_no_mappings)
 
-static char **my_environ;
 
-static const char *startup_PATH;
+struct globals {
+       char **my_environ;
+       const char *startup_PATH;
+};
+#define G (*(struct globals*)&bb_common_bufsiz1)
+#define INIT_G() do { } while (0)
+
 
 #if ENABLE_FEATURE_IFUPDOWN_IPV4 || ENABLE_FEATURE_IFUPDOWN_IPV6
 
@@ -126,7 +132,7 @@ static int strncmpz(const char *l, const char *r, size_t llen)
        int i = strncmp(l, r, llen);
 
        if (i == 0)
-               return -r[llen];
+               return - (unsigned char)r[llen];
        return i;
 }
 
@@ -135,16 +141,17 @@ static char *get_var(const char *id, size_t idlen, struct interface_defn_t *ifd)
        int i;
 
        if (strncmpz(id, "iface", idlen) == 0) {
-               static char *label_buf;
+               // ubuntu's ifup doesn't do this:
+               //static char *label_buf;
                //char *result;
-
-               free(label_buf);
-               label_buf = xstrdup(ifd->iface);
-               // Remove virtual iface suffix - why?
-               // ubuntu's ifup doesn't do this
+               //free(label_buf);
+               //label_buf = xstrdup(ifd->iface);
+               // Remove virtual iface suffix
                //result = strchrnul(label_buf, ':');
                //*result = '\0';
-               return label_buf;
+               //return label_buf;
+
+               return ifd->iface;
        }
        if (strncmpz(id, "label", idlen) == 0) {
                return ifd->iface;
@@ -547,7 +554,7 @@ static int FAST_FUNC dhcp_down(struct interface_defn_t *ifd, execfn *exec)
 
        for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) {
                if (exists_execable(ext_dhcp_clients[i].name)) {
-                       result += execute(ext_dhcp_clients[i].stopcmd, ifd, exec);
+                       result = execute(ext_dhcp_clients[i].stopcmd, ifd, exec);
                        if (result)
                                break;
                }
@@ -620,13 +627,13 @@ static int FAST_FUNC wvdial_down(struct interface_defn_t *ifd, execfn *exec)
 }
 
 static const struct method_t methods[] = {
-       { "manual", manual_up_down, manual_up_down, },
-       { "wvdial", wvdial_up, wvdial_down, },
-       { "ppp", ppp_up, ppp_down, },
-       { "static", static_up, static_down, },
-       { "bootp", bootp_up, static_down, },
-       { "dhcp", dhcp_up, dhcp_down, },
-       { "loopback", loopback_up, loopback_down, },
+       { "manual"  , manual_up_down, manual_up_down, },
+       { "wvdial"  , wvdial_up     , wvdial_down   , },
+       { "ppp"     , ppp_up        , ppp_down      , },
+       { "static"  , static_up     , static_down   , },
+       { "bootp"   , bootp_up      , static_down   , },
+       { "dhcp"    , dhcp_up       , dhcp_down     , },
+       { "loopback", loopback_up   , loopback_down , },
 };
 
 static const struct address_family_t addr_inet = {
@@ -896,43 +903,40 @@ static struct interfaces_file_t *read_interfaces(const char *filename)
 static char *setlocalenv(const char *format, const char *name, const char *value)
 {
        char *result;
-       char *here;
-       char *there;
+       char *dst;
+       char *src;
+       char c;
 
        result = xasprintf(format, name, value);
 
-       for (here = there = result; *there != '=' && *there; there++) {
-               if (*there == '-')
-                       *there = '_';
-               if (isalpha(*there))
-                       *there = toupper(*there);
-
-               if (isalnum(*there) || *there == '_') {
-                       *here = *there;
-                       here++;
-               }
+       for (dst = src = result; (c = *src) != '=' && c; src++) {
+               if (c == '-')
+                       c = '_';
+               if (c >= 'a' && c <= 'z')
+                       c -= ('a' - 'A');
+               if (isalnum(c) || c == '_')
+                       *dst++ = c;
        }
-       memmove(here, there, strlen(there) + 1);
+       overlapping_strcpy(dst, src);
 
        return result;
 }
 
 static void set_environ(struct interface_defn_t *iface, const char *mode)
 {
-       char **environend;
        int i;
-       const int n_env_entries = iface->n_options + 5;
-       char **ppch;
+       char **pp;
 
-       if (my_environ != NULL) {
-               for (ppch = my_environ; *ppch; ppch++) {
-                       free(*ppch);
-                       *ppch = NULL;
+       if (G.my_environ != NULL) {
+               for (pp = G.my_environ; *pp; pp++) {
+                       free(*pp);
                }
-               free(my_environ);
+               free(G.my_environ);
        }
-       my_environ = xzalloc(sizeof(char *) * (n_env_entries + 1 /* for final NULL */ ));
-       environend = my_environ;
+
+       /* note: last element will stay NULL: */
+       G.my_environ = xzalloc(sizeof(char *) * (iface->n_options + 6));
+       pp = G.my_environ;
 
        for (i = 0; i < iface->n_options; i++) {
                if (strcmp(iface->option[i].name, "up") == 0
@@ -942,14 +946,15 @@ static void set_environ(struct interface_defn_t *iface, const char *mode)
                ) {
                        continue;
                }
-               *(environend++) = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
+               *pp++ = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
        }
 
-       *(environend++) = setlocalenv("%s=%s", "IFACE", iface->iface);
-       *(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
-       *(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name);
-       *(environend++) = setlocalenv("%s=%s", "MODE", mode);
-       *(environend++) = setlocalenv("%s=%s", "PATH", startup_PATH);
+       *pp++ = setlocalenv("%s=%s", "IFACE", iface->iface);
+       *pp++ = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
+       *pp++ = setlocalenv("%s=%s", "METHOD", iface->method->name);
+       *pp++ = setlocalenv("%s=%s", "MODE", mode);
+       if (G.startup_PATH)
+               *pp++ = setlocalenv("%s=%s", "PATH", G.startup_PATH);
 }
 
 static int doit(char *str)
@@ -967,7 +972,7 @@ static int doit(char *str)
                case -1: /* failure */
                        return 0;
                case 0: /* child */
-                       execle(DEFAULT_SHELL, DEFAULT_SHELL, "-c", str, (char *) NULL, my_environ);
+                       execle(DEFAULT_SHELL, DEFAULT_SHELL, "-c", str, (char *) NULL, G.my_environ);
                        _exit(127);
                }
                safe_waitpid(child, &status, 0);
@@ -1142,6 +1147,10 @@ int ifupdown_main(int argc, char **argv)
        const char *interfaces = "/etc/network/interfaces";
        bool any_failures = 0;
 
+       INIT_G();
+
+       G.startup_PATH = getenv("PATH");
+
        cmds = iface_down;
        if (applet_name[2] == 'u') {
                /* ifup command */
@@ -1159,9 +1168,6 @@ int ifupdown_main(int argc, char **argv)
        defn = read_interfaces(interfaces);
        debug_noise("\ndone reading %s\n\n", interfaces);
 
-       startup_PATH = getenv("PATH");
-       if (!startup_PATH) startup_PATH = "";
-
        /* Create a list of interfaces to work on */
        if (DO_ALL) {
                target_list = defn->autointerfaces;