svn commit: trunk/busybox: include libbb networking/udhcp

vda at busybox.net vda at busybox.net
Thu Aug 2 22:31:06 UTC 2007


Author: vda
Date: 2007-08-02 15:31:05 -0700 (Thu, 02 Aug 2007)
New Revision: 19399

Log:
udhcp: slight shrink
udhcpd_main                                         1171    1208     +37
udhcpc_main                                         2363    2387     +24
dhcprelay_main                                      1145    1146      +1
dhcprelay_stopflag                                     4       1      -3
dhcprelay_signal_handler                              11       8      -3
client_background                                     46      42      -4
udhcp_read_interface                                 230     211     -19
udhcp_make_pidfile                                    76       -     -76
------------------------------------------------------------------------------
(add/remove: 0/1 grow/shrink: 3/4 up/down: 62/-105)           Total: -43 bytes
   text    data     bss     dec     hex filename
 770052    1096   11228  782376   bf028 busybox_old
 769980    1096   11228  782304   befe0 busybox_unstripped



Modified:
   trunk/busybox/include/libbb.h
   trunk/busybox/libbb/pidfile.c
   trunk/busybox/networking/udhcp/common.c
   trunk/busybox/networking/udhcp/common.h
   trunk/busybox/networking/udhcp/dhcpc.c
   trunk/busybox/networking/udhcp/dhcpd.c
   trunk/busybox/networking/udhcp/dhcprelay.c
   trunk/busybox/networking/udhcp/socket.c


Changeset:
Modified: trunk/busybox/include/libbb.h
===================================================================
--- trunk/busybox/include/libbb.h	2007-08-02 22:23:47 UTC (rev 19398)
+++ trunk/busybox/include/libbb.h	2007-08-02 22:31:05 UTC (rev 19399)
@@ -619,8 +619,8 @@
  *   llist_t *llist_add_to(llist_t *old_head, void *data)
  * etc does not result in smaller code... */
 
-/* start_stop_daemon and (udhcpc with ifupdown) are special - they want to
- * create pidfiles regardless of FEATURE_PIDFILE. */
+/* start_stop_daemon and udhcpc are special - they want
+ * to create pidfiles regardless of FEATURE_PIDFILE */
 #if ENABLE_FEATURE_PIDFILE || defined(WANT_PIDFILE)
 int write_pidfile(const char *path);
 #define remove_pidfile(f) ((void)unlink(f))

Modified: trunk/busybox/libbb/pidfile.c
===================================================================
--- trunk/busybox/libbb/pidfile.c	2007-08-02 22:23:47 UTC (rev 19398)
+++ trunk/busybox/libbb/pidfile.c	2007-08-02 22:31:05 UTC (rev 19399)
@@ -17,13 +17,15 @@
 	char *end;
 	char buf[sizeof(int)*3 + 2];
 
+	if (!path)
+		return 1;
 	/* we will overwrite stale pidfile */
 	pid_fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, 0666);
 	if (pid_fd < 0)
 		return 0;
 	/* few bytes larger, but doesn't use stdio */
 	end = utoa_to_buf(getpid(), buf, sizeof(buf));
-	end[0] = '\n';
+	*end = '\n';
 	full_write(pid_fd, buf, end - buf + 1);
 	close(pid_fd);
 	return 1;

Modified: trunk/busybox/networking/udhcp/common.c
===================================================================
--- trunk/busybox/networking/udhcp/common.c	2007-08-02 22:23:47 UTC (rev 19398)
+++ trunk/busybox/networking/udhcp/common.c	2007-08-02 22:31:05 UTC (rev 19399)
@@ -1,30 +1,9 @@
 /* vi: set sw=4 ts=4: */
 /* common.c
  *
- * Functions for debugging and logging as well as some other
- * simple helper functions.
- *
- * Russ Dill <Russ.Dill at asu.edu> 2001-2003
- * Rewritten by Vladimir Oleynik <dzo at simtreas.ru> (C) 2003
- *
  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  */
 
 #include "common.h"
 
 const uint8_t MAC_BCAST_ADDR[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
-
-void udhcp_make_pidfile(const char *pidfile)
-{
-	/* Make sure fd 0,1,2 are open */
-	bb_sanitize_stdio();
-
-	/* Equivalent of doing a fflush after every \n */
-	setlinebuf(stdout);
-
-	/* Create pidfile */
-	if (pidfile && !write_pidfile(pidfile))
-		bb_perror_msg("cannot create pidfile %s", pidfile);
-
-	bb_info_msg("%s (v%s) started", applet_name, BB_VER);
-}

Modified: trunk/busybox/networking/udhcp/common.h
===================================================================
--- trunk/busybox/networking/udhcp/common.h	2007-08-02 22:23:47 UTC (rev 19398)
+++ trunk/busybox/networking/udhcp/common.h	2007-08-02 22:31:05 UTC (rev 19399)
@@ -60,8 +60,6 @@
 
 /**/
 
-void udhcp_make_pidfile(const char *pidfile);
-
 void udhcp_run_script(struct dhcpMessage *packet, const char *name);
 
 // Still need to clean these up...

Modified: trunk/busybox/networking/udhcp/dhcpc.c
===================================================================
--- trunk/busybox/networking/udhcp/dhcpc.c	2007-08-02 22:23:47 UTC (rev 19398)
+++ trunk/busybox/networking/udhcp/dhcpc.c	2007-08-02 22:31:05 UTC (rev 19399)
@@ -109,7 +109,7 @@
 
 static void client_background(void)
 {
-#ifdef __uClinux__
+#if !BB_MMU
 	bb_error_msg("cannot background in uclinux (yet)");
 /* ... mainly because udhcpc calls client_background()
  * in _the _middle _of _udhcpc _run_, not at the start!
@@ -119,8 +119,7 @@
 	bb_daemonize(0);
 	logmode &= ~LOGMODE_STDIO;
 	/* rewrite pidfile, as our pid is different now */
-	if (client_config.pidfile)
-		write_pidfile(client_config.pidfile);
+	write_pidfile(client_config.pidfile);
 #endif
 	/* Do not fork again. */
 	client_config.foreground = 1;
@@ -148,19 +147,17 @@
 	char *str_c, *str_V, *str_h, *str_F, *str_r, *str_T, *str_t;
 	uint32_t xid = 0;
 	uint32_t lease = 0; /* can be given as 32-bit quantity */
-	unsigned t1 = 0, t2 = 0;
+	unsigned t1 = 0, t2 = 0; /* what a wonderful names */
 	unsigned start = 0;
 	unsigned now;
 	unsigned opt;
 	int max_fd;
-	int sig;
 	int retval;
 	int len;
-	int no_clientid = 0;
-	fd_set rfds;
 	struct timeval tv;
+	struct in_addr temp_addr;
 	struct dhcpMessage packet;
-	struct in_addr temp_addr;
+	fd_set rfds;
 
 	enum {
 		OPT_c = 1 << 0,
@@ -224,8 +221,7 @@
 
 	if (opt & OPT_c)
 		client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, str_c, 0);
-	if (opt & OPT_C)
-		no_clientid = 1;
+	//if (opt & OPT_C)
 	if (opt & OPT_V)
 		client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, str_V, 0);
 	if (opt & OPT_f)
@@ -262,7 +258,7 @@
 	if (opt & OPT_t)
 		client_config.retries = xatoi_u(str_t);
 	if (opt & OPT_v) {
-		printf("version %s\n\n", BB_VER);
+		printf("version %s\n", BB_VER);
 		return 0;
 	}
 
@@ -272,14 +268,23 @@
 	}
 
 	if (read_interface(client_config.interface, &client_config.ifindex,
-			   NULL, client_config.arp) < 0)
+			   NULL, client_config.arp))
 		return 1;
 
-	/* Sanitize fd's and write pidfile */
-	udhcp_make_pidfile(client_config.pidfile);
+	/* Make sure fd 0,1,2 are open */
+	bb_sanitize_stdio();
+	/* Equivalent of doing a fflush after every \n */
+	setlinebuf(stdout);
 
+	/* Create pidfile */
+	write_pidfile(client_config.pidfile);
+	/* if (!..) bb_perror_msg("cannot create pidfile %s", pidfile); */
+
+	/* Goes to stdout and possibly syslog */
+	bb_info_msg("%s (v%s) started", applet_name, BB_VER);
+
 	/* if not set, and not suppressed, setup the default client ID */
-	if (!client_config.clientid && !no_clientid) {
+	if (!client_config.clientid && !(opt & OPT_C)) {
 		client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, "", 7);
 		client_config.clientid[OPT_DATA] = 1;
 		memcpy(client_config.clientid + OPT_DATA+1, client_config.arp, 6);
@@ -294,9 +299,12 @@
 	state = INIT_SELECTING;
 	udhcp_run_script(NULL, "deconfig");
 	change_mode(LISTEN_RAW);
+	tv.tv_sec = 0;
+	goto jump_in;
 
 	for (;;) {
 		tv.tv_sec = timeout - monotonic_sec();
+ jump_in:
 		tv.tv_usec = 0;
 
 		if (listen_mode != LISTEN_NONE && sockfd < 0) {
@@ -307,13 +315,20 @@
 		}
 		max_fd = udhcp_sp_fd_set(&rfds, sockfd);
 
+		retval = 0; /* If we already timed out, fall through, else... */
 		if (tv.tv_sec > 0) {
 			DEBUG("Waiting on select...");
 			retval = select(max_fd + 1, &rfds, NULL, NULL, &tv);
-		} else retval = 0; /* If we already timed out, fall through */
+		}
 
 		now = monotonic_sec();
-		if (retval == 0) {
+		if (retval < 0) {
+			/* EINTR? signal was caught, don't panic */
+			if (errno != EINTR) {
+				/* Else: an error occured, panic! */
+				bb_perror_msg_and_die("select");
+			}
+		} else if (retval == 0) {
 			/* timeout dropped to zero */
 			switch (state) {
 			case INIT_SELECTING:
@@ -377,9 +392,8 @@
 				} else {
 					/* send a request packet */
 					send_renew(xid, server_addr, requested_ip); /* unicast */
-
 					t1 = (t2 - t1) / 2 + t1;
-					timeout = t1 + start;
+					timeout = start + t1;
 				}
 				break;
 			case REBINDING:
@@ -395,9 +409,8 @@
 				} else {
 					/* send a request packet */
 					send_renew(xid, 0, requested_ip); /* broadcast */
-
 					t2 = (lease - t2) / 2 + t2;
-					timeout = t2 + start;
+					timeout = start + t2;
 				}
 				break;
 			case RELEASED:
@@ -405,7 +418,7 @@
 				timeout = INT_MAX;
 				break;
 			}
-		} else if (retval > 0 && listen_mode != LISTEN_NONE && FD_ISSET(sockfd, &rfds)) {
+		} else if (listen_mode != LISTEN_NONE && FD_ISSET(sockfd, &rfds)) {
 			/* a packet is ready, read it */
 
 			if (listen_mode == LISTEN_KERNEL)
@@ -480,7 +493,7 @@
 					bb_info_msg("Lease of %s obtained, lease time %u",
 						inet_ntoa(temp_addr), (unsigned)lease);
 					start = now;
-					timeout = t1 + start;
+					timeout = start + t1;
 					requested_ip = packet.yiaddr;
 					udhcp_run_script(&packet,
 						   ((state == RENEWING || state == REBINDING) ? "renew" : "bound"));
@@ -511,8 +524,9 @@
 				break;
 			/* case BOUND, RELEASED: - ignore all packets */
 			}
-		} else if (retval > 0 && (sig = udhcp_sp_read(&rfds))) {
-			switch (sig) {
+		} else {
+			int signo = udhcp_sp_read(&rfds);
+			switch (signo) {
 			case SIGUSR1:
 				perform_renew();
 				break;
@@ -525,11 +539,6 @@
 					perform_release();
 				goto ret0;
 			}
-		} else if (retval == -1 && errno == EINTR) {
-			/* a signal was caught */
-		} else {
-			/* An error occured */
-			bb_perror_msg("select");
 		}
 	} /* for (;;) */
  ret0:

Modified: trunk/busybox/networking/udhcp/dhcpd.c
===================================================================
--- trunk/busybox/networking/udhcp/dhcpd.c	2007-08-02 22:23:47 UTC (rev 19398)
+++ trunk/busybox/networking/udhcp/dhcpd.c	2007-08-02 22:31:05 UTC (rev 19399)
@@ -51,8 +51,17 @@
 	 * otherwise NOMMU machines will parse config twice */
 	read_config(argv[1] ? argv[1] : DHCPD_CONF_FILE);
 
-	udhcp_make_pidfile(server_config.pidfile);
+	/* Make sure fd 0,1,2 are open */
+	bb_sanitize_stdio();
+	/* Equivalent of doing a fflush after every \n */
+	setlinebuf(stdout);
 
+	/* Create pidfile */
+	write_pidfile(server_config.pidfile);
+	/* if (!..) bb_perror_msg("cannot create pidfile %s", pidfile); */
+
+	bb_info_msg("%s (v%s) started", applet_name, BB_VER);
+
 	option = find_option(server_config.options, DHCP_LEASE_TIME);
 	server_config.lease = LEASE_TIME;
 	if (option) {
@@ -72,7 +81,7 @@
 	read_leases(server_config.lease_file);
 
 	if (read_interface(server_config.interface, &server_config.ifindex,
-			   &server_config.server, server_config.arp) < 0) {
+			   &server_config.server, server_config.arp)) {
 		retval = 1;
 		goto ret;
 	}

Modified: trunk/busybox/networking/udhcp/dhcprelay.c
===================================================================
--- trunk/busybox/networking/udhcp/dhcprelay.c	2007-08-02 22:23:47 UTC (rev 19398)
+++ trunk/busybox/networking/udhcp/dhcprelay.c	2007-08-02 22:31:05 UTC (rev 19399)
@@ -30,7 +30,7 @@
 } dhcprelay_xid_list = {0, {0}, 0, 0, NULL};
 
 
-static struct xid_item * xid_add(uint32_t xid, struct sockaddr_in *ip, int client)
+static struct xid_item *xid_add(uint32_t xid, struct sockaddr_in *ip, int client)
 {
 	struct xid_item *item;
 
@@ -48,7 +48,6 @@
 	return item;
 }
 
-
 static void xid_expire(void)
 {
 	struct xid_item *item = dhcprelay_xid_list.next;
@@ -56,7 +55,7 @@
 	time_t current_time = time(NULL);
 
 	while (item != NULL) {
-		if ((current_time-item->timestamp) > MAX_LIFETIME) {
+		if ((current_time - item->timestamp) > MAX_LIFETIME) {
 			last->next = item->next;
 			free(item);
 			item = last->next;
@@ -67,7 +66,7 @@
 	}
 }
 
-static struct xid_item * xid_find(uint32_t xid)
+static struct xid_item *xid_find(uint32_t xid)
 {
 	struct xid_item *item = dhcprelay_xid_list.next;
 	while (item != NULL) {
@@ -95,7 +94,6 @@
 	}
 }
 
-
 /**
  * get_dhcp_packet_type - gets the message type of a dhcp packet
  * p - pointer to the dhcp packet
@@ -119,7 +117,8 @@
  * signal_handler - handles signals ;-)
  * sig - sent signal
  */
-static int dhcprelay_stopflag;
+static smallint dhcprelay_stopflag;
+
 static void dhcprelay_signal_handler(int sig)
 {
 	dhcprelay_stopflag = 1;
@@ -130,7 +129,7 @@
  * dev_list - comma separated list of devices
  * returns array
  */
-static char ** get_client_devices(char *dev_list, int *client_number)
+static char **get_client_devices(char *dev_list, int *client_number)
 {
 	char *s, *list, **client_dev;
 	int i, cn;
@@ -286,7 +285,7 @@
 							(struct sockaddr *)(&client_addr), &addr_size);
 				if (packlen <= 0)
 					continue;
-				if (read_interface(clients[i-1], NULL, &dhcp_msg.giaddr, NULL) < 0)
+				if (read_interface(clients[i-1], NULL, &dhcp_msg.giaddr, NULL))
 					dhcp_msg.giaddr = gw_ip;
 				pass_on(&dhcp_msg, packlen, i, fds, &client_addr, server_addr);
 			}
@@ -322,7 +321,7 @@
 
 	num_sockets = init_sockets(clients, num_sockets, argv[2], fds, &max_socket);
 
-	if (read_interface(argv[2], NULL, &gw_ip, NULL) == -1)
+	if (read_interface(argv[2], NULL, &gw_ip, NULL))
 		return 1;
 
 	dhcprelay_loop(fds, num_sockets, max_socket, clients, &server_addr, gw_ip);

Modified: trunk/busybox/networking/udhcp/socket.c
===================================================================
--- trunk/busybox/networking/udhcp/socket.c	2007-08-02 22:23:47 UTC (rev 19398)
+++ trunk/busybox/networking/udhcp/socket.c	2007-08-02 22:31:05 UTC (rev 19399)
@@ -44,11 +44,11 @@
 	struct sockaddr_in *our_ip;
 
 	memset(&ifr, 0, sizeof(ifr));
-	fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
-	if (fd < 0) {
-		bb_perror_msg("socket failed");
-		return -1;
-	}
+	fd = xsocket(AF_INET, SOCK_RAW, IPPROTO_RAW);
+//	if (fd < 0) {
+//		bb_perror_msg("socket failed");
+//		return -1;
+//	}
 
 	ifr.ifr_addr.sa_family = AF_INET;
 	strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name));
@@ -83,6 +83,7 @@
 			arp[0], arp[1], arp[2], arp[3], arp[4], arp[5]);
 	}
 
+	close(fd);
 	return 0;
 }
 




More information about the busybox-cvs mailing list