ifupdown: strlen(NULL) is a no-no, fixed. Also few micro-optimizations

This commit is contained in:
Denis Vlasenko 2006-12-18 21:02:00 +00:00
parent 6dca70a049
commit fcfe834d50

View File

@ -25,11 +25,11 @@
#define EUNDEFVAR 10002
#define EUNBALPER 10000
#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
#if ENABLE_FEATURE_IFUPDOWN_MAPPING
#define MAX_INTERFACE_LENGTH 10
#endif
#define debug_noise(fmt, args...)
#define debug_noise(args...) /*fprintf(stderr, args)*/
/* Forward declaration */
struct interface_defn_t;
@ -109,7 +109,7 @@ static char *startup_PATH;
#if ENABLE_FEATURE_IFUPDOWN_IPV4 || ENABLE_FEATURE_IFUPDOWN_IPV6
#ifdef CONFIG_FEATURE_IFUPDOWN_IP
#if ENABLE_FEATURE_IFUPDOWN_IP
static unsigned count_bits(unsigned a)
{
@ -184,10 +184,10 @@ static char *get_var(const char *id, size_t idlen, struct interface_defn_t *ifd)
static char *parse(const char *command, struct interface_defn_t *ifd)
{
char *result = NULL;
size_t old_pos[MAX_OPT_DEPTH] = { 0 };
int okay[MAX_OPT_DEPTH] = { 1 };
int opt_depth = 1;
char *result = xstrdup("");
while (*command) {
switch (*command) {
@ -245,7 +245,7 @@ static char *parse(const char *command, struct interface_defn_t *ifd)
if (varvalue) {
addstr(&result, varvalue, strlen(varvalue));
} else {
#ifdef CONFIG_FEATURE_IFUPDOWN_IP
#if ENABLE_FEATURE_IFUPDOWN_IP
/* Sigh... Add a special case for 'ip' to convert from
* dotted quad to bit count style netmasks. */
if (strncmp(command, "bnmask", 6) == 0) {
@ -290,7 +290,7 @@ static int execute(const char *command, struct interface_defn_t *ifd, execfn *ex
int ret;
out = parse(command, ifd);
if (!out) {
if (!out || !out[0]) {
return 0;
}
ret = (*exec)(out);
@ -303,10 +303,10 @@ static int execute(const char *command, struct interface_defn_t *ifd, execfn *ex
}
#endif
#ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
#if ENABLE_FEATURE_IFUPDOWN_IPV6
static int loopback_up6(struct interface_defn_t *ifd, execfn *exec)
{
#ifdef CONFIG_FEATURE_IFUPDOWN_IP
#if ENABLE_FEATURE_IFUPDOWN_IP
int result;
result = execute("ip addr add ::1 dev %iface%", ifd, exec);
result += execute("ip link set %iface% up", ifd, exec);
@ -318,7 +318,7 @@ static int loopback_up6(struct interface_defn_t *ifd, execfn *exec)
static int loopback_down6(struct interface_defn_t *ifd, execfn *exec)
{
#ifdef CONFIG_FEATURE_IFUPDOWN_IP
#if ENABLE_FEATURE_IFUPDOWN_IP
return execute("ip link set %iface% down", ifd, exec);
#else
return execute("ifconfig %iface% del ::1", ifd, exec);
@ -328,9 +328,10 @@ static int loopback_down6(struct interface_defn_t *ifd, execfn *exec)
static int static_up6(struct interface_defn_t *ifd, execfn *exec)
{
int result;
#ifdef CONFIG_FEATURE_IFUPDOWN_IP
#if ENABLE_FEATURE_IFUPDOWN_IP
result = execute("ip addr add %address%/%netmask% dev %iface%[[ label %label%]]", ifd, exec);
result += execute("ip link set[[ mtu %mtu%]][[ address %hwaddress%]] %iface% up", ifd, exec);
/* Was: "[[ ip ....%gateway% ]]". Removed extra spaces w/o checking */
result += execute("[[ip route add ::/0 via %gateway%]]", ifd, exec);
#else
result = execute("ifconfig %iface%[[ media %media%]][[ hw %hwaddress%]][[ mtu %mtu%]] up", ifd, exec);
@ -342,14 +343,14 @@ static int static_up6(struct interface_defn_t *ifd, execfn *exec)
static int static_down6(struct interface_defn_t *ifd, execfn *exec)
{
#ifdef CONFIG_FEATURE_IFUPDOWN_IP
#if ENABLE_FEATURE_IFUPDOWN_IP
return execute("ip link set %iface% down", ifd, exec);
#else
return execute("ifconfig %iface% down", ifd, exec);
#endif
}
#ifdef CONFIG_FEATURE_IFUPDOWN_IP
#if ENABLE_FEATURE_IFUPDOWN_IP
static int v4tunnel_up(struct interface_defn_t *ifd, execfn *exec)
{
int result;
@ -368,7 +369,7 @@ static int v4tunnel_down(struct interface_defn_t * ifd, execfn * exec)
#endif
static const struct method_t methods6[] = {
#ifdef CONFIG_FEATURE_IFUPDOWN_IP
#if ENABLE_FEATURE_IFUPDOWN_IP
{ "v4tunnel", v4tunnel_up, v4tunnel_down, },
#endif
{ "static", static_up6, static_down6, },
@ -380,12 +381,12 @@ static const struct address_family_t addr_inet6 = {
sizeof(methods6) / sizeof(struct method_t),
methods6
};
#endif /* CONFIG_FEATURE_IFUPDOWN_IPV6 */
#endif /* FEATURE_IFUPDOWN_IPV6 */
#ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
#if ENABLE_FEATURE_IFUPDOWN_IPV4
static int loopback_up(struct interface_defn_t *ifd, execfn *exec)
{
#ifdef CONFIG_FEATURE_IFUPDOWN_IP
#if ENABLE_FEATURE_IFUPDOWN_IP
int result;
result = execute("ip addr add 127.0.0.1/8 dev %iface%", ifd, exec);
result += execute("ip link set %iface% up", ifd, exec);
@ -397,7 +398,7 @@ static int loopback_up(struct interface_defn_t *ifd, execfn *exec)
static int loopback_down(struct interface_defn_t *ifd, execfn *exec)
{
#ifdef CONFIG_FEATURE_IFUPDOWN_IP
#if ENABLE_FEATURE_IFUPDOWN_IP
int result;
result = execute("ip addr flush dev %iface%", ifd, exec);
result += execute("ip link set %iface% down", ifd, exec);
@ -410,7 +411,7 @@ static int loopback_down(struct interface_defn_t *ifd, execfn *exec)
static int static_up(struct interface_defn_t *ifd, execfn *exec)
{
int result;
#ifdef CONFIG_FEATURE_IFUPDOWN_IP
#if ENABLE_FEATURE_IFUPDOWN_IP
result = execute("ip addr add %address%/%bnmask%[[ broadcast %broadcast%]] "
"dev %iface%[[ peer %pointopoint%]][[ label %label%]]", ifd, exec);
result += execute("ip link set[[ mtu %mtu%]][[ address %hwaddress%]] %iface% up", ifd, exec);
@ -432,7 +433,7 @@ static int static_up(struct interface_defn_t *ifd, execfn *exec)
static int static_down(struct interface_defn_t *ifd, execfn *exec)
{
int result;
#ifdef CONFIG_FEATURE_IFUPDOWN_IP
#if ENABLE_FEATURE_IFUPDOWN_IP
result = execute("ip addr flush dev %iface%", ifd, exec);
result += execute("ip link set %iface% down", ifd, exec);
#else
@ -442,7 +443,7 @@ static int static_down(struct interface_defn_t *ifd, execfn *exec)
return ((result == 2) ? 2 : 0);
}
#ifndef CONFIG_APP_UDHCPC
#if !ENABLE_APP_UDHCPC
struct dhcp_client_t
{
const char *name;
@ -472,7 +473,7 @@ static const struct dhcp_client_t ext_dhcp_clients[] = {
static int dhcp_up(struct interface_defn_t *ifd, execfn *exec)
{
#ifdef CONFIG_APP_UDHCPC
#if ENABLE_APP_UDHCPC
return execute("udhcpc -R -n -p /var/run/udhcpc.%iface%.pid "
"-i %iface%[[ -H %hostname%]][[ -c %clientid%]][[ -s %script%]]",
ifd, exec);
@ -489,7 +490,7 @@ static int dhcp_up(struct interface_defn_t *ifd, execfn *exec)
static int dhcp_down(struct interface_defn_t *ifd, execfn *exec)
{
#ifdef CONFIG_APP_UDHCPC
#if ENABLE_APP_UDHCPC
return execute("kill -TERM "
"`cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
#else
@ -527,13 +528,13 @@ static int ppp_down(struct interface_defn_t *ifd, execfn *exec)
static int wvdial_up(struct interface_defn_t *ifd, execfn *exec)
{
return execute("/sbin/start-stop-daemon --start -x /usr/bin/wvdial "
return execute("start-stop-daemon --start -x wvdial "
"-p /var/run/wvdial.%iface% -b -m --[[ %provider%]]", ifd, exec);
}
static int wvdial_down(struct interface_defn_t *ifd, execfn *exec)
{
return execute("/sbin/start-stop-daemon --stop -x /usr/bin/wvdial "
return execute("start-stop-daemon --stop -x wvdial "
"-p /var/run/wvdial.%iface% -s 2", ifd, exec);
}
@ -553,7 +554,7 @@ static const struct address_family_t addr_inet = {
methods
};
#endif /* ifdef CONFIG_FEATURE_IFUPDOWN_IPV4 */
#endif /* if ENABLE_FEATURE_IFUPDOWN_IPV4 */
static char *next_word(char **buf)
{
@ -633,7 +634,7 @@ static const llist_t *find_list_string(const llist_t *list, const char *string)
static struct interfaces_file_t *read_interfaces(const char *filename)
{
#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
#if ENABLE_FEATURE_IFUPDOWN_MAPPING
struct mapping_defn_t *currmap = NULL;
#endif
struct interface_defn_t *currif = NULL;
@ -658,7 +659,7 @@ static struct interfaces_file_t *read_interfaces(const char *filename)
}
if (strcmp(firstword, "mapping") == 0) {
#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
#if ENABLE_FEATURE_IFUPDOWN_MAPPING
currmap = xzalloc(sizeof(struct mapping_defn_t));
while ((firstword = next_word(&buf_ptr)) != NULL) {
@ -686,10 +687,10 @@ static struct interfaces_file_t *read_interfaces(const char *filename)
currently_processing = MAPPING;
} else if (strcmp(firstword, "iface") == 0) {
static const struct address_family_t *const addr_fams[] = {
#ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
#if ENABLE_FEATURE_IFUPDOWN_IPV4
&addr_inet,
#endif
#ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
#if ENABLE_FEATURE_IFUPDOWN_IPV6
&addr_inet6,
#endif
NULL
@ -802,7 +803,7 @@ static struct interfaces_file_t *read_interfaces(const char *filename)
currif->n_options++;
break;
case MAPPING:
#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
#if ENABLE_FEATURE_IFUPDOWN_MAPPING
if (strcmp(firstword, "script") == 0) {
if (currmap->script != NULL) {
bb_error_msg("duplicate script in mapping \"%s\"", buf);
@ -941,7 +942,8 @@ static int execute_all(struct interface_defn_t *ifd, const char *opt)
return 1;
}
static int check(char *str) {
static int check(char *str)
{
return str != NULL;
}
@ -965,7 +967,7 @@ static int iface_down(struct interface_defn_t *iface)
return 1;
}
#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
#if ENABLE_FEATURE_IFUPDOWN_MAPPING
static int popen2(FILE **in, FILE **out, char *command, ...)
{
va_list ap;
@ -1069,7 +1071,7 @@ static char *run_mapping(char *physical, struct mapping_defn_t * map)
return logical;
}
#endif /* CONFIG_FEATURE_IFUPDOWN_MAPPING */
#endif /* FEATURE_IFUPDOWN_MAPPING */
static llist_t *find_iface_state(llist_t *state_list, const char *iface)
{
@ -1088,8 +1090,6 @@ static llist_t *find_iface_state(llist_t *state_list, const char *iface)
int ifupdown_main(int argc, char **argv)
{
static const char statefile[] = "/var/run/ifstate";
int (*cmds)(struct interface_defn_t *) = NULL;
struct interfaces_file_t *defn;
llist_t *state_list = NULL;
@ -1097,26 +1097,25 @@ int ifupdown_main(int argc, char **argv)
const char *interfaces = "/etc/network/interfaces";
int any_failures = 0;
cmds = iface_down;
if (applet_name[2] == 'u') {
/* ifup command */
cmds = iface_up;
} else {
/* ifdown command */
cmds = iface_down;
}
getopt32(argc, argv, OPTION_STR, &interfaces);
if (argc - optind > 0) {
if (DO_ALL) bb_show_usage();
} else
} else {
if (!DO_ALL) bb_show_usage();
}
debug_noise("reading %s file:\n", interfaces);
defn = read_interfaces(interfaces);
debug_noise("\ndone reading %s\n\n", interfaces);
if (!defn) {
exit(EXIT_FAILURE);
return EXIT_FAILURE;
}
startup_PATH = getenv("PATH");
@ -1179,7 +1178,7 @@ int ifupdown_main(int argc, char **argv)
}
}
#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
#if ENABLE_FEATURE_IFUPDOWN_MAPPING
if ((cmds == iface_up) && !NO_MAPPINGS) {
struct mapping_defn_t *currmap;
@ -1198,7 +1197,6 @@ int ifupdown_main(int argc, char **argv)
}
#endif
iface_list = defn->ifaces;
while (iface_list) {
currif = (struct interface_defn_t *) iface_list->data;
@ -1208,16 +1206,16 @@ int ifupdown_main(int argc, char **argv)
okay = 1;
currif->iface = iface;
debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name);
debug_noise("\nZ Configuring interface %s (%s)\n", liface, currif->address_family->name);
/* Call the cmds function pointer, does either iface_up() or iface_down() */
cmds_ret = cmds(currif);
if (cmds_ret == -1) {
bb_error_msg("don't seem to have all the variables for %s/%s",
liface, currif->address_family->name);
any_failures += 1;
any_failures = 1;
} else if (cmds_ret == 0) {
any_failures += 1;
any_failures = 1;
}
currif->iface = oldiface;
@ -1230,7 +1228,7 @@ int ifupdown_main(int argc, char **argv)
if (!okay && !FORCE) {
bb_error_msg("ignoring unknown interface %s", liface);
any_failures += 1;
any_failures = 1;
} else {
llist_t *iface_state = find_iface_state(state_list, iface);
@ -1251,9 +1249,9 @@ int ifupdown_main(int argc, char **argv)
/* Actually write the new state */
if (!NO_ACT) {
FILE *state_fp = NULL;
FILE *state_fp;
state_fp = xfopen(statefile, "w");
state_fp = xfopen("/var/run/ifstate", "w");
while (state_list) {
if (state_list->data) {
fputs(state_list->data, state_fp);
@ -1264,7 +1262,5 @@ int ifupdown_main(int argc, char **argv)
fclose(state_fp);
}
if (any_failures)
return 1;
return 0;
return any_failures;
}