--- trunk/mkinitrd-magellan/klibc/usr/kinit/ipconfig/main.c 2010/08/18 21:11:40 1122 +++ trunk/mkinitrd-magellan/klibc/usr/kinit/ipconfig/main.c 2011/05/27 15:12:11 1297 @@ -1,4 +1,3 @@ -#include #include #include #include @@ -96,6 +95,30 @@ dev->hostname, dev->name); } +/* + * Escape shell varialbes in git style: + * Always start with a single quote ('), then leave all characters + * except ' and ! unchanged. + */ +static void write_option(FILE* f, const char* name, const char* chr) +{ + + fprintf(f, "%s='", name); + while (*chr) { + switch (*chr) { + case '!': + case '\'': + fprintf(f, "'\\%c'", *chr); + break; + default: + fprintf(f, "%c", *chr); + break; + } + ++chr; + } + fprintf(f, "'\n"); +} + static void dump_device_config(struct netdev *dev) { char fn[40]; @@ -104,22 +127,26 @@ snprintf(fn, sizeof(fn), "/tmp/net-%s.conf", dev->name); f = fopen(fn, "w"); if (f) { - fprintf(f, "DEVICE=%s\n", dev->name); - fprintf(f, "IPV4ADDR=%s\n", my_inet_ntoa(dev->ip_addr)); - fprintf(f, "IPV4BROADCAST=%s\n", - my_inet_ntoa(dev->ip_broadcast)); - fprintf(f, "IPV4NETMASK=%s\n", my_inet_ntoa(dev->ip_netmask)); - fprintf(f, "IPV4GATEWAY=%s\n", my_inet_ntoa(dev->ip_gateway)); - fprintf(f, "IPV4DNS0=%s\n", - my_inet_ntoa(dev->ip_nameserver[0])); - fprintf(f, "IPV4DNS1=%s\n", - my_inet_ntoa(dev->ip_nameserver[1])); - fprintf(f, "HOSTNAME=%s\n", dev->hostname); - fprintf(f, "DNSDOMAIN=%s\n", dev->dnsdomainname); - fprintf(f, "NISDOMAIN=%s\n", dev->nisdomainname); - fprintf(f, "ROOTSERVER=%s\n", my_inet_ntoa(dev->ip_server)); - fprintf(f, "ROOTPATH=%s\n", dev->bootpath); - fprintf(f, "filename=\"%s\"\n", dev->filename); + write_option(f, "DEVICE", dev->name); + write_option(f, "IPV4ADDR", + my_inet_ntoa(dev->ip_addr)); + write_option(f, "IPV4BROADCAST", + my_inet_ntoa(dev->ip_broadcast)); + write_option(f, "IPV4NETMASK", + my_inet_ntoa(dev->ip_netmask)); + write_option(f, "IPV4GATEWAY", + my_inet_ntoa(dev->ip_gateway)); + write_option(f, "IPV4DNS0", + my_inet_ntoa(dev->ip_nameserver[0])); + write_option(f, "IPV4DNS1", + my_inet_ntoa(dev->ip_nameserver[1])); + write_option(f, "HOSTNAME", dev->hostname); + write_option(f, "DNSDOMAIN", dev->dnsdomainname); + write_option(f, "NISDOMAIN", dev->nisdomainname); + write_option(f, "ROOTSERVER", + my_inet_ntoa(dev->ip_server)); + write_option(f, "ROOTPATH", dev->bootpath); + write_option(f, "filename", dev->filename); fclose(f); } } @@ -171,7 +198,7 @@ /* * Returns: - * 0 = Not handled, the packet is still in the queue + * 0 = Not handled, try again later * 1 = Handled */ static int process_receive_event(struct state *s, time_t now) @@ -179,12 +206,20 @@ int handled = 1; switch (s->state) { + case DEVST_ERROR: + return 0; /* Not handled */ + case DEVST_COMPLETE: + return 0; /* Not handled as already configured */ + case DEVST_BOOTP: s->restart_state = DEVST_BOOTP; switch (bootp_recv_reply(s->dev)) { case -1: s->state = DEVST_ERROR; break; + case 0: + handled = 0; + break; case 1: s->state = DEVST_COMPLETE; dprintf("\n bootp reply\n"); @@ -198,6 +233,9 @@ case -1: s->state = DEVST_ERROR; break; + case 0: + handled = 0; + break; case DHCPOFFER: /* Offer received */ s->state = DEVST_DHCPREQ; dhcp_send_request(s->dev); @@ -211,6 +249,9 @@ case -1: /* error */ s->state = DEVST_ERROR; break; + case 0: + handled = 0; + break; case DHCPACK: /* ACK received */ s->state = DEVST_COMPLETE; break; @@ -297,28 +338,17 @@ /* * Returns: - * 0 = Error, packet not received or discarded + * 0 = No dhcp/bootp packet was received * 1 = A packet was received and handled */ static int do_pkt_recv(int pkt_fd, time_t now) { - int ifindex, ret; + int ret; struct state *s; - ret = packet_peek(&ifindex); - if (ret == 0) - return ret; - for (s = slist; s; s = s->next) { - if (s->dev->ifindex == ifindex) { - ret = process_receive_event(s, now); - break; - } + ret |= process_receive_event(s, now); } - - if (ret == 0) - packet_discard(); - return ret; } @@ -328,7 +358,7 @@ struct pollfd fds[NR_FDS]; struct state *s; int pkt_fd; - int nr = 0; + int nr = 0, rc = 0; struct timeval now, prev; time_t start; @@ -393,6 +423,7 @@ now.tv_sec - start >= loop_timeout) { printf("IP-Config: no response after %d " "secs - giving up\n", loop_timeout); + rc = -1; goto bail; } @@ -407,7 +438,7 @@ bail: packet_close(); - return 0; + return rc; } static int add_one_dev(struct netdev *dev) @@ -480,7 +511,7 @@ static int parse_device(struct netdev *dev, const char *ip) { char *cp; - int i, opt; + int opt; int is_ip = 0; dprintf("IP-Config: parse_device: \"%s\"\n", ip); @@ -502,7 +533,7 @@ dev->name = ip; } } else { - for (i = opt = 0; ip && *ip; ip = cp, opt++) { + for (opt = 0; ip && *ip; ip = cp, opt++) { if ((cp = strchr(ip, ':'))) { *cp++ = '\0'; } @@ -533,7 +564,7 @@ case 4: strncpy(dev->hostname, ip, SYS_NMLN - 1); dev->hostname[SYS_NMLN - 1] = '\0'; - memcpy(dev->reqhostname, dev->hostname, + memcpy(dev->reqhostname, dev->hostname, SYS_NMLN); break; case 5: @@ -721,7 +752,7 @@ { struct netdev *dev; int c, port; - int err; + int err = 0; /* If progname is set we're invoked from another program */ if (!progname) { @@ -799,8 +830,8 @@ "dest to %d\n", cfg_local_port, cfg_remote_port); } - loop(); + err = loop(); } - return 0; + return err; }