ifupdown: if $PATH is not set, do not set it to "" in children

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>
This commit is contained in:
Denys Vlasenko 2009-06-05 21:53:11 +02:00
parent 035b4d77dc
commit 62152da892

View File

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