ifupdown: style cleanup, no code changes

This commit is contained in:
Denis Vlasenko 2006-09-26 20:35:30 +00:00
parent e175ff252f
commit 0534125ccc

View File

@ -15,8 +15,6 @@
* Licensed under the GPL v2 or later, see the file LICENSE in this tarball. * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
*/ */
/* TODO: standardise execute() return codes to return 0 for success and 1 for failure */
#include "busybox.h" #include "busybox.h"
#include <sys/utsname.h> #include <sys/utsname.h>
#include <fnmatch.h> #include <fnmatch.h>
@ -49,7 +47,7 @@ struct address_family_t
{ {
char *name; char *name;
int n_methods; int n_methods;
struct method_t *method; const struct method_t *method;
}; };
struct mapping_defn_t struct mapping_defn_t
@ -75,8 +73,8 @@ struct variable_t
struct interface_defn_t struct interface_defn_t
{ {
struct address_family_t *address_family; const struct address_family_t *address_family;
struct method_t *method; const struct method_t *method;
char *iface; char *iface;
int max_options; int max_options;
@ -106,7 +104,7 @@ enum {
#define FORCE (option_mask & OPT_force) #define FORCE (option_mask & OPT_force)
#define NO_MAPPINGS (option_mask & OPT_no_mappings) #define NO_MAPPINGS (option_mask & OPT_no_mappings)
static char **__myenviron = NULL; static char **__myenviron;
#if ENABLE_FEATURE_IFUPDOWN_IPV4 || ENABLE_FEATURE_IFUPDOWN_IPV6 #if ENABLE_FEATURE_IFUPDOWN_IPV4 || ENABLE_FEATURE_IFUPDOWN_IPV6
@ -117,7 +115,7 @@ static unsigned int count_bits(unsigned int a)
unsigned int result; unsigned int result;
result = (a & 0x55) + ((a >> 1) & 0x55); result = (a & 0x55) + ((a >> 1) & 0x55);
result = (result & 0x33) + ((result >> 2) & 0x33); result = (result & 0x33) + ((result >> 2) & 0x33);
return((result & 0x0F) + ((result >> 4) & 0x0F)); return ((result & 0x0F) + ((result >> 4) & 0x0F));
} }
static int count_netmask_bits(char *dotted_quad) static int count_netmask_bits(char *dotted_quad)
@ -130,7 +128,7 @@ static int count_netmask_bits(char *dotted_quad)
result += count_bits(b); result += count_bits(b);
result += count_bits(c); result += count_bits(c);
result += count_bits(d); result += count_bits(d);
return ((int)result); return (int)result;
} }
#endif #endif
@ -156,9 +154,9 @@ static int strncmpz(char *l, 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 -r[llen];
} else { } else {
return(i); return i;
} }
} }
@ -175,18 +173,18 @@ static char *get_var(char *id, size_t idlen, struct interface_defn_t *ifd)
if (result) { if (result) {
*result=0; *result=0;
} }
return( label_buf); return label_buf;
} else if (strncmpz(id, "label", idlen) == 0) { } else if (strncmpz(id, "label", idlen) == 0) {
return (ifd->iface); return ifd->iface;
} else { } else {
for (i = 0; i < ifd->n_options; i++) { for (i = 0; i < ifd->n_options; i++) {
if (strncmpz(id, ifd->option[i].name, idlen) == 0) { if (strncmpz(id, ifd->option[i].name, idlen) == 0) {
return (ifd->option[i].value); return ifd->option[i].value;
} }
} }
} }
return(NULL); return NULL;
} }
static char *parse(char *command, struct interface_defn_t *ifd) static char *parse(char *command, struct interface_defn_t *ifd)
@ -200,7 +198,6 @@ static char *parse(char *command, struct interface_defn_t *ifd)
while (*command) { while (*command) {
switch (*command) { switch (*command) {
default: default:
addstr(&result, &len, &pos, command, 1); addstr(&result, &len, &pos, command, 1);
command++; command++;
@ -248,7 +245,7 @@ static char *parse(char *command, struct interface_defn_t *ifd)
if (!nextpercent) { if (!nextpercent) {
errno = EUNBALPER; errno = EUNBALPER;
free(result); free(result);
return (NULL); return NULL;
} }
varvalue = get_var(command, nextpercent - command, ifd); varvalue = get_var(command, nextpercent - command, ifd);
@ -283,18 +280,19 @@ static char *parse(char *command, struct interface_defn_t *ifd)
if (opt_depth > 1) { if (opt_depth > 1) {
errno = EUNBALBRACK; errno = EUNBALBRACK;
free(result); free(result);
return(NULL); return NULL;
} }
if (!okay[0]) { if (!okay[0]) {
errno = EUNDEFVAR; errno = EUNDEFVAR;
free(result); free(result);
return(NULL); return NULL;
} }
return(result); return result;
} }
/* execute() returns 1 for success and 0 for failure */
static int execute(char *command, struct interface_defn_t *ifd, execfn *exec) static int execute(char *command, struct interface_defn_t *ifd, execfn *exec)
{ {
char *out; char *out;
@ -302,15 +300,15 @@ static int execute(char *command, struct interface_defn_t *ifd, execfn *exec)
out = parse(command, ifd); out = parse(command, ifd);
if (!out) { if (!out) {
return(0); return 0;
} }
ret = (*exec) (out); ret = (*exec)(out);
free(out); free(out);
if (ret != 1) { if (ret != 1) {
return(0); return 0;
} }
return(1); return 1;
} }
#endif #endif
@ -319,20 +317,20 @@ static int loopback_up6(struct interface_defn_t *ifd, execfn *exec)
{ {
#ifdef CONFIG_FEATURE_IFUPDOWN_IP #ifdef CONFIG_FEATURE_IFUPDOWN_IP
int result; int result;
result =execute("ip addr add ::1 dev %iface%", ifd, exec); result = execute("ip addr add ::1 dev %iface%", ifd, exec);
result += execute("ip link set %iface% up", ifd, exec); result += execute("ip link set %iface% up", ifd, exec);
return ((result == 2) ? 2 : 0); return ((result == 2) ? 2 : 0);
#else #else
return( execute("ifconfig %iface% add ::1", ifd, exec)); return execute("ifconfig %iface% add ::1", ifd, exec);
#endif #endif
} }
static int loopback_down6(struct interface_defn_t *ifd, execfn *exec) static int loopback_down6(struct interface_defn_t *ifd, execfn *exec)
{ {
#ifdef CONFIG_FEATURE_IFUPDOWN_IP #ifdef CONFIG_FEATURE_IFUPDOWN_IP
return(execute("ip link set %iface% down", ifd, exec)); return execute("ip link set %iface% down", ifd, exec);
#else #else
return(execute("ifconfig %iface% del ::1", ifd, exec)); return execute("ifconfig %iface% del ::1", ifd, exec);
#endif #endif
} }
@ -354,9 +352,9 @@ static int static_up6(struct interface_defn_t *ifd, execfn *exec)
static int static_down6(struct interface_defn_t *ifd, execfn *exec) static int static_down6(struct interface_defn_t *ifd, execfn *exec)
{ {
#ifdef CONFIG_FEATURE_IFUPDOWN_IP #ifdef CONFIG_FEATURE_IFUPDOWN_IP
return(execute("ip link set %iface% down", ifd, exec)); return execute("ip link set %iface% down", ifd, exec);
#else #else
return(execute("ifconfig %iface% down", ifd, exec)); return execute("ifconfig %iface% down", ifd, exec);
#endif #endif
} }
@ -374,11 +372,11 @@ static int v4tunnel_up(struct interface_defn_t *ifd, execfn *exec)
static int v4tunnel_down(struct interface_defn_t * ifd, execfn * exec) static int v4tunnel_down(struct interface_defn_t * ifd, execfn * exec)
{ {
return( execute("ip tunnel del %iface%", ifd, exec)); return execute("ip tunnel del %iface%", ifd, exec);
} }
#endif #endif
static struct method_t methods6[] = { static const struct method_t methods6[] = {
#ifdef CONFIG_FEATURE_IFUPDOWN_IP #ifdef CONFIG_FEATURE_IFUPDOWN_IP
{ "v4tunnel", v4tunnel_up, v4tunnel_down, }, { "v4tunnel", v4tunnel_up, v4tunnel_down, },
#endif #endif
@ -386,7 +384,7 @@ static struct method_t methods6[] = {
{ "loopback", loopback_up6, loopback_down6, }, { "loopback", loopback_up6, loopback_down6, },
}; };
static struct address_family_t addr_inet6 = { static const struct address_family_t addr_inet6 = {
"inet6", "inet6",
sizeof(methods6) / sizeof(struct method_t), sizeof(methods6) / sizeof(struct method_t),
methods6 methods6
@ -402,7 +400,7 @@ static int loopback_up(struct interface_defn_t *ifd, execfn *exec)
result += execute("ip link set %iface% up", ifd, exec); result += execute("ip link set %iface% up", ifd, exec);
return ((result == 2) ? 2 : 0); return ((result == 2) ? 2 : 0);
#else #else
return( execute("ifconfig %iface% 127.0.0.1 up", ifd, exec)); return execute("ifconfig %iface% 127.0.0.1 up", ifd, exec);
#endif #endif
} }
@ -414,7 +412,7 @@ static int loopback_down(struct interface_defn_t *ifd, execfn *exec)
result += execute("ip link set %iface% down", ifd, exec); result += execute("ip link set %iface% down", ifd, exec);
return ((result == 2) ? 2 : 0); return ((result == 2) ? 2 : 0);
#else #else
return( execute("ifconfig %iface% 127.0.0.1 down", ifd, exec)); return execute("ifconfig %iface% 127.0.0.1 down", ifd, exec);
#endif #endif
} }
@ -498,35 +496,34 @@ static int dhcp_down(struct interface_defn_t *ifd, execfn *exec)
static int bootp_up(struct interface_defn_t *ifd, execfn *exec) static int bootp_up(struct interface_defn_t *ifd, execfn *exec)
{ {
return( execute("bootpc [[--bootfile %bootfile%]] --dev %iface% " return execute("bootpc [[--bootfile %bootfile%]] --dev %iface% "
"[[--server %server%]] [[--hwaddr %hwaddr%]] " "[[--server %server%]] [[--hwaddr %hwaddr%]] "
"--returniffail --serverbcast", ifd, exec)); "--returniffail --serverbcast", ifd, exec);
} }
static int ppp_up(struct interface_defn_t *ifd, execfn *exec) static int ppp_up(struct interface_defn_t *ifd, execfn *exec)
{ {
return( execute("pon [[%provider%]]", ifd, exec)); return execute("pon [[%provider%]]", ifd, exec);
} }
static int ppp_down(struct interface_defn_t *ifd, execfn *exec) static int ppp_down(struct interface_defn_t *ifd, execfn *exec)
{ {
return( execute("poff [[%provider%]]", ifd, exec)); return execute("poff [[%provider%]]", ifd, exec);
} }
static int wvdial_up(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("/sbin/start-stop-daemon --start -x /usr/bin/wvdial "
"-p /var/run/wvdial.%iface% -b -m -- [[ %provider% ]]", ifd, exec)); "-p /var/run/wvdial.%iface% -b -m -- [[ %provider% ]]", ifd, exec);
} }
static int wvdial_down(struct interface_defn_t *ifd, execfn *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("/sbin/start-stop-daemon --stop -x /usr/bin/wvdial "
"-p /var/run/wvdial.%iface% -s 2", ifd, exec)); "-p /var/run/wvdial.%iface% -s 2", ifd, exec);
} }
static struct method_t methods[] = static const struct method_t methods[] = {
{
{ "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, },
@ -535,8 +532,7 @@ static struct method_t methods[] =
{ "loopback", loopback_up, loopback_down, }, { "loopback", loopback_up, loopback_down, },
}; };
static struct address_family_t addr_inet = static const struct address_family_t addr_inet = {
{
"inet", "inet",
sizeof(methods) / sizeof(struct method_t), sizeof(methods) / sizeof(struct method_t),
methods methods
@ -561,13 +557,13 @@ static char *next_word(char **buf)
/* Skip over comments */ /* Skip over comments */
if (*word == '#') { if (*word == '#') {
return(NULL); return NULL;
} }
/* Find the length of this word */ /* Find the length of this word */
length = strcspn(word, " \t\n"); length = strcspn(word, " \t\n");
if (length == 0) { if (length == 0) {
return(NULL); return NULL;
} }
*buf = word + length; *buf = word + length;
/*DBU:[dave@cray.com] if we are already at EOL dont't increment beyond it */ /*DBU:[dave@cray.com] if we are already at EOL dont't increment beyond it */
@ -579,7 +575,7 @@ static char *next_word(char **buf)
return word; return word;
} }
static struct address_family_t *get_address_family(struct address_family_t *af[], char *name) static const struct address_family_t *get_address_family(const struct address_family_t *const af[], char *name)
{ {
int i; int i;
@ -591,7 +587,7 @@ static struct address_family_t *get_address_family(struct address_family_t *af[]
return NULL; return NULL;
} }
static struct method_t *get_method(struct address_family_t *af, char *name) static const struct method_t *get_method(const struct address_family_t *af, char *name)
{ {
int i; int i;
@ -600,18 +596,18 @@ static struct method_t *get_method(struct address_family_t *af, char *name)
return &af->method[i]; return &af->method[i];
} }
} }
return(NULL); return NULL;
} }
static const llist_t *find_list_string(const llist_t *list, const char *string) static const llist_t *find_list_string(const llist_t *list, const char *string)
{ {
while (list) { while (list) {
if (strcmp(list->data, string) == 0) { if (strcmp(list->data, string) == 0) {
return(list); return list;
} }
list = list->link; list = list->link;
} }
return(NULL); return NULL;
} }
static struct interfaces_file_t *read_interfaces(const char *filename) static struct interfaces_file_t *read_interfaces(const char *filename)
@ -668,11 +664,7 @@ static struct interfaces_file_t *read_interfaces(const char *filename)
#endif #endif
currently_processing = MAPPING; currently_processing = MAPPING;
} else if (strcmp(firstword, "iface") == 0) { } else if (strcmp(firstword, "iface") == 0) {
{ static const struct address_family_t *const addr_fams[] = {
char *iface_name;
char *address_family_name;
char *method_name;
struct address_family_t *addr_fams[] = {
#ifdef CONFIG_FEATURE_IFUPDOWN_IPV4 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
&addr_inet, &addr_inet,
#endif #endif
@ -682,6 +674,11 @@ static struct interfaces_file_t *read_interfaces(const char *filename)
NULL NULL
}; };
char *iface_name;
char *address_family_name;
char *method_name;
llist_t *iface_list;
currif = xzalloc(sizeof(struct interface_defn_t)); currif = xzalloc(sizeof(struct interface_defn_t));
iface_name = next_word(&buf_ptr); iface_name = next_word(&buf_ptr);
address_family_name = next_word(&buf_ptr); address_family_name = next_word(&buf_ptr);
@ -716,9 +713,6 @@ static struct interfaces_file_t *read_interfaces(const char *filename)
return NULL; return NULL;
} }
{
llist_t *iface_list;
for (iface_list = defn->ifaces; iface_list; iface_list = iface_list->link) { for (iface_list = defn->ifaces; iface_list; iface_list = iface_list->link) {
struct interface_defn_t *tmp = (struct interface_defn_t *) iface_list->data; struct interface_defn_t *tmp = (struct interface_defn_t *) iface_list->data;
if ((strcmp(tmp->iface, currif->iface) == 0) && if ((strcmp(tmp->iface, currif->iface) == 0) &&
@ -727,11 +721,9 @@ static struct interfaces_file_t *read_interfaces(const char *filename)
return NULL; return NULL;
} }
} }
llist_add_to_end(&(defn->ifaces), (char*)currif); llist_add_to_end(&(defn->ifaces), (char*)currif);
}
debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name); debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
}
currently_processing = IFACE; currently_processing = IFACE;
} else if (strcmp(firstword, "auto") == 0) { } else if (strcmp(firstword, "auto") == 0) {
while ((firstword = next_word(&buf_ptr)) != NULL) { while ((firstword = next_word(&buf_ptr)) != NULL) {
@ -883,13 +875,15 @@ static void set_environ(struct interface_defn_t *iface, const char *mode)
*(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name); *(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
*(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name); *(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name);
*(environend++) = setlocalenv("%s=%s", "MODE", mode); *(environend++) = setlocalenv("%s=%s", "MODE", mode);
/* FIXME: we must not impose our own PATH, it is admin's job!
Use PATH from parent env */
*(environend++) = setlocalenv("%s=%s", "PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"); *(environend++) = setlocalenv("%s=%s", "PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin");
} }
static int doit(char *str) static int doit(char *str)
{ {
if (option_mask & (OPT_no_act|OPT_verbose)) { if (option_mask & (OPT_no_act|OPT_verbose)) {
printf("%s\n", str); puts(str);
} }
if (!(option_mask & OPT_no_act)) { if (!(option_mask & OPT_no_act)) {
pid_t child; pid_t child;
@ -1068,21 +1062,22 @@ static llist_t *find_iface_state(llist_t *state_list, const char *iface)
while (search) { while (search) {
if ((strncmp(search->data, iface, iface_len) == 0) && if ((strncmp(search->data, iface, iface_len) == 0) &&
(search->data[iface_len] == '=')) { (search->data[iface_len] == '=')) {
return(search); return search;
} }
search = search->link; search = search->link;
} }
return(NULL); return NULL;
} }
int ifupdown_main(int argc, char **argv) int ifupdown_main(int argc, char **argv)
{ {
int (*cmds) (struct interface_defn_t *) = NULL; static const char statefile[] = "/var/run/ifstate";
int (*cmds)(struct interface_defn_t *) = NULL;
struct interfaces_file_t *defn; struct interfaces_file_t *defn;
llist_t *state_list = NULL; llist_t *state_list = NULL;
llist_t *target_list = NULL; llist_t *target_list = NULL;
const char *interfaces = "/etc/network/interfaces"; const char *interfaces = "/etc/network/interfaces";
const char *statefile = "/var/run/ifstate";
int any_failures = 0; int any_failures = 0;
int i; int i;
@ -1170,7 +1165,6 @@ int ifupdown_main(int argc, char **argv)
struct mapping_defn_t *currmap; struct mapping_defn_t *currmap;
for (currmap = defn->mappings; currmap; currmap = currmap->next) { for (currmap = defn->mappings; currmap; currmap = currmap->next) {
for (i = 0; i < currmap->n_matches; i++) { for (i = 0; i < currmap->n_matches; i++) {
if (fnmatch(currmap->match[i], liface, 0) != 0) if (fnmatch(currmap->match[i], liface, 0) != 0)
continue; continue;
@ -1199,7 +1193,7 @@ int ifupdown_main(int argc, char **argv)
/* Call the cmds function pointer, does either iface_up() or iface_down() */ /* Call the cmds function pointer, does either iface_up() or iface_down() */
cmds_ret = cmds(currif); cmds_ret = cmds(currif);
if (cmds_ret == -1) { if (cmds_ret == -1) {
bb_error_msg("Don't seem to have all the variables for %s/%s.", bb_error_msg("don't seem to have all the variables for %s/%s",
liface, currif->address_family->name); liface, currif->address_family->name);
any_failures += 1; any_failures += 1;
} else if (cmds_ret == 0) { } else if (cmds_ret == 0) {
@ -1211,11 +1205,11 @@ int ifupdown_main(int argc, char **argv)
iface_list = iface_list->link; iface_list = iface_list->link;
} }
if (VERBOSE) { if (VERBOSE) {
printf("\n"); puts("");
} }
if (!okay && !FORCE) { if (!okay && !FORCE) {
bb_error_msg("Ignoring unknown interface %s", liface); bb_error_msg("ignoring unknown interface %s", liface);
any_failures += 1; any_failures += 1;
} else { } else {
llist_t *iface_state = find_iface_state(state_list, iface); llist_t *iface_state = find_iface_state(state_list, iface);