svn commit: trunk/busybox/networking/udhcp

vda at busybox.net vda at busybox.net
Mon Mar 26 13:22:35 UTC 2007


Author: vda
Date: 2007-03-26 06:22:35 -0700 (Mon, 26 Mar 2007)
New Revision: 18243

Log:
Move udhcp to new NOMMU helpers.
Fix server part to compile under NOMMU.
Client is not compilable yet.
On MMU everything compiles (and maybe even works :)


Modified:
   trunk/busybox/networking/udhcp/Kbuild
   trunk/busybox/networking/udhcp/common.c
   trunk/busybox/networking/udhcp/common.h
   trunk/busybox/networking/udhcp/dhcpc.c
   trunk/busybox/networking/udhcp/dhcpd.c


Changeset:
Modified: trunk/busybox/networking/udhcp/Kbuild
===================================================================
--- trunk/busybox/networking/udhcp/Kbuild	2007-03-26 13:20:54 UTC (rev 18242)
+++ trunk/busybox/networking/udhcp/Kbuild	2007-03-26 13:22:35 UTC (rev 18243)
@@ -6,9 +6,9 @@
 #
 
 lib-y:=
-lib-$(CONFIG_APP_UDHCPC)        += common.o options.o packet.o pidfile.o \
+lib-$(CONFIG_APP_UDHCPC)        += common.o options.o packet.o \
                                    signalpipe.o socket.o
-lib-$(CONFIG_APP_UDHCPD)        += common.o options.o packet.o pidfile.o \
+lib-$(CONFIG_APP_UDHCPD)        += common.o options.o packet.o \
                                    signalpipe.o socket.o
 lib-$(CONFIG_APP_UDHCPC)        += dhcpc.o clientpacket.o clientsocket.o \
                                    script.o

Modified: trunk/busybox/networking/udhcp/common.c
===================================================================
--- trunk/busybox/networking/udhcp/common.c	2007-03-26 13:20:54 UTC (rev 18242)
+++ trunk/busybox/networking/udhcp/common.c	2007-03-26 13:22:35 UTC (rev 18243)
@@ -22,40 +22,56 @@
 	return info.uptime;
 }
 
-void udhcp_background(const char *pidfile)
+
+static const char *saved_pidfile;
+
+static void pidfile_delete(void)
 {
-#ifdef __uClinux__
-	bb_error_msg("cannot background in uclinux (yet)");
-#else /* __uClinux__ */
+	if (saved_pidfile)
+		unlink(saved_pidfile);
+}
+
+static int pidfile_acquire(const char *pidfile)
+{
 	int pid_fd;
+	if (!pidfile) return -1;
 
-	/* hold lock during fork. */
-	pid_fd = pidfile_acquire(pidfile);
-	setsid();
-	xdaemon(0, 0);
-	logmode &= ~LOGMODE_STDIO;
-	pidfile_write_release(pid_fd);
-#endif /* __uClinux__ */
+	pid_fd = open(pidfile, O_CREAT|O_WRONLY|O_TRUNC, 0644);
+	if (pid_fd < 0) {
+		bb_perror_msg("cannot open pidfile %s", pidfile);
+	} else {
+		/* lockf(pid_fd, F_LOCK, 0); */
+		if (!saved_pidfile)
+			atexit(pidfile_delete);
+		saved_pidfile = pidfile;
+	}
+
+	return pid_fd;
 }
 
-void udhcp_start_log_and_pid(const char *pidfile)
+static void pidfile_write_release(int pid_fd)
 {
+	if (pid_fd < 0) return;
+
+	fdprintf(pid_fd, "%d\n", getpid());
+	/* lockf(pid_fd, F_UNLCK, 0); */
+	close(pid_fd);
+}
+
+
+void udhcp_make_pidfile(const char *pidfile)
+{
 	int pid_fd;
 
-	/* Make sure our syslog fd isn't overwritten */
+	/* Make sure fd 0,1,2 are open */
 	bb_sanitize_stdio();
 
-	/* do some other misc startup stuff while we are here to save bytes */
+	/* Equivalent of doing a fflush after every \n */
+	setlinebuf(stdout);
+
+	/* Create pidfile */
 	pid_fd = pidfile_acquire(pidfile);
 	pidfile_write_release(pid_fd);
 
-	/* equivelent of doing a fflush after every \n */
-	setlinebuf(stdout);
-
-	if (ENABLE_FEATURE_UDHCP_SYSLOG) {
-		openlog(applet_name, LOG_PID, LOG_LOCAL0);
-		logmode |= LOGMODE_SYSLOG;
-	}
-
 	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-03-26 13:20:54 UTC (rev 18242)
+++ trunk/busybox/networking/udhcp/common.h	2007-03-26 13:22:35 UTC (rev 18243)
@@ -60,16 +60,12 @@
 
 /**/
 
-void udhcp_background(const char *pidfile);
-void udhcp_start_log_and_pid(const char *pidfile);
+void udhcp_make_pidfile(const char *pidfile);
 
 void udhcp_run_script(struct dhcpMessage *packet, const char *name);
 
 // Still need to clean these up...
 
-/* from pidfile.h */
-#define pidfile_acquire		udhcp_pidfile_acquire
-#define pidfile_write_release	udhcp_pidfile_write_release
 /* from options.h */
 #define get_option		udhcp_get_option
 #define end_option		udhcp_end_option
@@ -91,8 +87,6 @@
 int raw_socket(int ifindex);
 int read_interface(const char *interface, int *ifindex, uint32_t *addr, uint8_t *arp);
 int listen_socket(uint32_t ip, int port, const char *inf);
-int pidfile_acquire(const char *pidfile);
-void pidfile_write_release(int pid_fd);
 int arpping(uint32_t yiaddr, uint32_t ip, uint8_t *arp, char *interface);
 
 #if ENABLE_FEATURE_UDHCP_DEBUG

Modified: trunk/busybox/networking/udhcp/dhcpc.c
===================================================================
--- trunk/busybox/networking/udhcp/dhcpc.c	2007-03-26 13:20:54 UTC (rev 18242)
+++ trunk/busybox/networking/udhcp/dhcpc.c	2007-03-26 13:22:35 UTC (rev 18243)
@@ -9,6 +9,7 @@
  */
 
 #include <getopt.h>
+#include <syslog.h>
 
 #include "common.h"
 #include "dhcpd.h"
@@ -103,7 +104,16 @@
 
 static void client_background(void)
 {
-	udhcp_background(client_config.pidfile);
+#ifdef __uClinux__
+	bb_error_msg("cannot background in uclinux (yet)");
+/* ... mainly because udhcpc calls client_background()
+ * in _the _middle _of _udhcpc _run_, not at the start!
+ * If that will be properly disabled for NOMMU, client_background()
+ * will work on NOMMU too */
+#else
+	bb_daemonize(DAEMON_CHDIR_ROOT);
+	logmode &= ~LOGMODE_STDIO;
+#endif
 	client_config.foreground = 1; /* Do not fork again. */
 	client_config.background_if_no_lease = 0;
 }
@@ -246,13 +256,18 @@
 		return 0;
 	}
 
-	/* Start the log, sanitize fd's, and write a pid file */
-	udhcp_start_log_and_pid(client_config.pidfile);
+	if (ENABLE_FEATURE_UDHCP_SYSLOG) {
+		openlog(applet_name, LOG_PID, LOG_LOCAL0);
+		logmode |= LOGMODE_SYSLOG;
+	}
 
 	if (read_interface(client_config.interface, &client_config.ifindex,
 			   NULL, client_config.arp) < 0)
 		return 1;
 
+	/* Sanitize fd's and write pidfile */
+	udhcp_make_pidfile(client_config.pidfile);
+
 	/* if not set, and not suppressed, setup the default client ID */
 	if (!client_config.clientid && !no_clientid) {
 		client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, "", 7);

Modified: trunk/busybox/networking/udhcp/dhcpd.c
===================================================================
--- trunk/busybox/networking/udhcp/dhcpd.c	2007-03-26 13:20:54 UTC (rev 18242)
+++ trunk/busybox/networking/udhcp/dhcpd.c	2007-03-26 13:22:35 UTC (rev 18243)
@@ -10,6 +10,7 @@
  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  */
 
+#include <syslog.h>
 #include "common.h"
 #include "dhcpd.h"
 #include "options.h"
@@ -33,16 +34,30 @@
 	struct option_set *option;
 	struct dhcpOfferedAddr *lease, static_lease;
 
+//Huh, dhcpd don't have --foreground, --syslog options?? TODO
+
+	if (!ENABLE_FEATURE_UDHCP_DEBUG) {
+    		bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
+    		logmode &= ~LOGMODE_STDIO;
+	}
+
+	if (ENABLE_FEATURE_UDHCP_SYSLOG) {
+		openlog(applet_name, LOG_PID, LOG_LOCAL0);
+		logmode |= LOGMODE_SYSLOG;
+	}
+
+	/* Would rather not do read_config before daemonization -
+	 * otherwise NOMMU machines will parse config twice */
 	read_config(argc < 2 ? DHCPD_CONF_FILE : argv[1]);
 
-	/* Start the log, sanitize fd's, and write a pid file */
-	udhcp_start_log_and_pid(server_config.pidfile);
+	udhcp_make_pidfile(server_config.pidfile);
 
-	if ((option = find_option(server_config.options, DHCP_LEASE_TIME))) {
+	option = find_option(server_config.options, DHCP_LEASE_TIME);
+	if (option) {
 		memcpy(&server_config.lease, option->data + 2, 4);
 		server_config.lease = ntohl(server_config.lease);
-	}
-	else server_config.lease = LEASE_TIME;
+	} else
+		server_config.lease = LEASE_TIME;
 
 	/* Sanity check */
 	num_ips = ntohl(server_config.end) - ntohl(server_config.start) + 1;
@@ -60,9 +75,6 @@
 			   &server_config.server, server_config.arp) < 0)
 		return 1;
 
-	if (!ENABLE_FEATURE_UDHCP_DEBUG)
-		udhcp_background(server_config.pidfile); /* hold lock during fork. */
-
 	/* Setup the signal pipe */
 	udhcp_sp_setup();
 
@@ -106,7 +118,8 @@
 		default: continue;	/* signal or error (probably EINTR) */
 		}
 
-		if ((bytes = udhcp_get_packet(&packet, server_socket)) < 0) { /* this waits for a packet - idle */
+		bytes = udhcp_get_packet(&packet, server_socket); /* this waits for a packet - idle */
+		if (bytes < 0) {
 			if (bytes == -1 && errno != EINTR) {
 				DEBUG("error on read, %s, reopening socket", strerror(errno));
 				close(server_socket);
@@ -115,7 +128,8 @@
 			continue;
 		}
 
-		if ((state = get_option(&packet, DHCP_MESSAGE_TYPE)) == NULL) {
+		state = get_option(&packet, DHCP_MESSAGE_TYPE);
+		if (state == NULL) {
 			bb_error_msg("cannot get option from packet, ignoring");
 			continue;
 		}
@@ -131,7 +145,6 @@
 			static_lease.expires = 0;
 
 			lease = &static_lease;
-
 		} else {
 			lease = find_lease_by_chaddr(packet.chaddr);
 		}
@@ -157,25 +170,23 @@
 				if (server_id) {
 					/* SELECTING State */
 					DEBUG("server_id = %08x", ntohl(server_id_align));
-					if (server_id_align == server_config.server && requested &&
-					    requested_align == lease->yiaddr) {
+					if (server_id_align == server_config.server && requested
+					 && requested_align == lease->yiaddr
+					) {
 						sendACK(&packet, lease->yiaddr);
 					}
+				} else if (requested) {
+					/* INIT-REBOOT State */
+					if (lease->yiaddr == requested_align)
+						sendACK(&packet, lease->yiaddr);
+					else
+						sendNAK(&packet);
+				} else if (lease->yiaddr == packet.ciaddr) {
+					/* RENEWING or REBINDING State */
+					sendACK(&packet, lease->yiaddr);
 				} else {
-					if (requested) {
-						/* INIT-REBOOT State */
-						if (lease->yiaddr == requested_align)
-							sendACK(&packet, lease->yiaddr);
-						else sendNAK(&packet);
-					} else {
-						/* RENEWING or REBINDING State */
-						if (lease->yiaddr == packet.ciaddr)
-							sendACK(&packet, lease->yiaddr);
-						else {
-							/* don't know what to do!!!! */
-							sendNAK(&packet);
-						}
-					}
+					/* don't know what to do!!!! */
+					sendNAK(&packet);
 				}
 
 			/* what to do if we have no record of the client */
@@ -184,19 +195,22 @@
 
 			} else if (requested) {
 				/* INIT-REBOOT State */
-				if ((lease = find_lease_by_yiaddr(requested_align))) {
+				lease = find_lease_by_yiaddr(requested_align);
+				if (lease) {
 					if (lease_expired(lease)) {
 						/* probably best if we drop this lease */
 						memset(lease->chaddr, 0, 16);
 					/* make some contention for this address */
-					} else sendNAK(&packet);
-				} else if (requested_align < server_config.start ||
-					   requested_align > server_config.end) {
+					} else
+						sendNAK(&packet);
+				} else if (requested_align < server_config.start
+				        || requested_align > server_config.end
+				) {
 					sendNAK(&packet);
 				} /* else remain silent */
 
 			} else {
-				 /* RENEWING or REBINDING State */
+				/* RENEWING or REBINDING State */
 			}
 			break;
 		case DHCPDECLINE:




More information about the busybox-cvs mailing list