svn commit: trunk/busybox: debianutils networking/udhcp

vda at busybox.net vda at busybox.net
Sat Nov 18 22:03:27 UTC 2006


Author: vda
Date: 2006-11-18 14:03:26 -0800 (Sat, 18 Nov 2006)
New Revision: 16579

Log:
small fixes: using fd-based io instead of FILE*-based,
missed O_TRUNC, etc


Modified:
   trunk/busybox/debianutils/start_stop_daemon.c
   trunk/busybox/networking/udhcp/common.c
   trunk/busybox/networking/udhcp/dumpleases.c
   trunk/busybox/networking/udhcp/files.c
   trunk/busybox/networking/udhcp/pidfile.c


Changeset:
Modified: trunk/busybox/debianutils/start_stop_daemon.c
===================================================================
--- trunk/busybox/debianutils/start_stop_daemon.c	2006-11-18 19:51:32 UTC (rev 16578)
+++ trunk/busybox/debianutils/start_stop_daemon.c	2006-11-18 22:03:26 UTC (rev 16579)
@@ -40,7 +40,8 @@
 
 static int pid_is_exec(pid_t pid, const char *name)
 {
-	char buf[32], *execbuf;
+	char buf[sizeof("/proc//exe") + sizeof(int)*3];
+	char *execbuf;
 	int equal;
 
 	sprintf(buf, "/proc/%d/exe", pid);
@@ -56,7 +57,7 @@
 static int pid_is_user(int pid, int uid)
 {
 	struct stat sb;
-	char buf[32];
+	char buf[sizeof("/proc/") + sizeof(int)*3];
 
 	sprintf(buf, "/proc/%d", pid);
 	if (stat(buf, &sb) != 0)
@@ -66,7 +67,7 @@
 
 static int pid_is_cmd(pid_t pid, const char *name)
 {
-	char buf[32];
+	char buf[sizeof("/proc//stat") + sizeof(int)*3];
 	FILE *f;
 	int c;
 
@@ -115,7 +116,6 @@
 		fclose(f);
 	} else if (errno != ENOENT)
 		bb_perror_msg_and_die("open pidfile %s", pidfile);
-
 }
 
 static void do_procinit(void)
@@ -146,28 +146,28 @@
 
 static int do_stop(void)
 {
-	RESERVE_CONFIG_BUFFER(what, 1024);
+	char *what;
 	struct pid_list *p;
 	int killed = 0;
 
 	do_procinit();
 
 	if (cmdname)
-		strcpy(what, cmdname);
+		what = xstrdup(cmdname);
 	else if (execname)
-		strcpy(what, execname);
+		what = xstrdup(execname);
 	else if (pidfile)
-		sprintf(what, "process in pidfile `%.200s'", pidfile);
+		what = xasprintf("process in pidfile '%s'", pidfile);
 	else if (userspec)
-		sprintf(what, "process(es) owned by `%s'", userspec);
+		what = xasprintf("process(es) owned by '%s'", userspec);
 	else
-		bb_error_msg_and_die ("internal error, please report");
+		bb_error_msg_and_die("internal error, please report");
 
 	if (!found) {
 		if (!quiet)
-			printf("no %s found; none killed.\n", what);
+			printf("no %s found; none killed\n", what);
 		if (ENABLE_FEATURE_CLEAN_UP)
-			RELEASE_CONFIG_BUFFER(what);
+			free(what);
 		return -1;
 	}
 	for (p = found; p; p = p->next) {
@@ -183,10 +183,10 @@
 		for (p = found; p; p = p->next)
 			if(p->pid < 0)
 				printf(" %d", -p->pid);
-		printf(").\n");
+		puts(")");
 	}
 	if (ENABLE_FEATURE_CLEAN_UP)
-		RELEASE_CONFIG_BUFFER(what);
+		free(what);
 	return killed;
 }
 

Modified: trunk/busybox/networking/udhcp/common.c
===================================================================
--- trunk/busybox/networking/udhcp/common.c	2006-11-18 19:51:32 UTC (rev 16578)
+++ trunk/busybox/networking/udhcp/common.c	2006-11-18 22:03:26 UTC (rev 16579)
@@ -29,9 +29,7 @@
  */
 static inline void sanitize_fds(void)
 {
-	int fd = open(bb_dev_null, O_RDWR, 0);
-	if (fd < 0)
-		return;
+	int fd = xopen(bb_dev_null, O_RDWR);
 	while (fd < 3)
 		fd = dup(fd);
 	close(fd);

Modified: trunk/busybox/networking/udhcp/dumpleases.c
===================================================================
--- trunk/busybox/networking/udhcp/dumpleases.c	2006-11-18 19:51:32 UTC (rev 16578)
+++ trunk/busybox/networking/udhcp/dumpleases.c	2006-11-18 22:03:26 UTC (rev 16579)
@@ -13,9 +13,9 @@
 
 int dumpleases_main(int argc, char *argv[])
 {
-	FILE *fp;
+	int fp;
 	int i, c, mode = REMAINING;
-	long expires;
+	unsigned long expires;
 	const char *file = LEASES_FILE;
 	struct dhcpOfferedAddr lease;
 	struct in_addr addr;
@@ -43,11 +43,11 @@
 		}
 	}
 
-	fp = xfopen(file, "r");
+	fp = xopen(file, O_RDONLY);
 
 	printf("Mac Address       IP-Address      Expires %s\n", mode == REMAINING ? "in" : "at");
 	/*     "00:00:00:00:00:00 255.255.255.255 Wed Jun 30 21:49:08 1993" */
-	while (fread(&lease, sizeof(lease), 1, fp)) {
+	while (full_read(fp, &lease, sizeof(lease)) == sizeof(lease)) {
 		printf(":%02x"+1, lease.chaddr[0]);
 		for (i = 1; i < 6; i++) {
 			printf(":%02x", lease.chaddr[i]);
@@ -59,23 +59,16 @@
 			if (!expires)
 				printf("expired\n");
 			else {
-				if (expires > 60*60*24) {
-					printf("%ld days, ", expires / (60*60*24));
-					expires %= 60*60*24;
-				}
-				if (expires > 60*60) {
-					printf("%ld hours, ", expires / (60*60));
-					expires %= 60*60;
-				}
-				if (expires > 60) {
-					printf("%ld minutes, ", expires / 60);
-					expires %= 60;
-				}
-				printf("%ld seconds\n", expires);
+				unsigned d, h, m;
+				d = expires / (24*60*60); expires %= (24*60*60);
+				h = expires / (60*60); expires %= (60*60);
+				m = expires / 60; expires %= 60;
+				if (d) printf("%u days ", d);
+				printf("%02u:%02u:%02u\n", h, m, (unsigned)expires);
 			}
-		} else printf("%s", ctime(&expires));
+		} else fputs(ctime(&expires), stdout);
 	}
-	fclose(fp);
+	/* close(fp); */
 
 	return 0;
 }

Modified: trunk/busybox/networking/udhcp/files.c
===================================================================
--- trunk/busybox/networking/udhcp/files.c	2006-11-18 19:51:32 UTC (rev 16578)
+++ trunk/busybox/networking/udhcp/files.c	2006-11-18 22:03:26 UTC (rev 16579)
@@ -272,27 +272,32 @@
 		if (keywords[i].def[0])
 			keywords[i].handler(keywords[i].def, keywords[i].var);
 
-	if (!(in = fopen(file, "r"))) {
-		bb_error_msg("unable to open config file: %s", file);
+	in = fopen(file, "r");
+	if (!in) {
+		bb_error_msg("cannot open config file: %s", file);
 		return 0;
 	}
 
 	while (fgets(buffer, READ_CONFIG_BUF_SIZE, in)) {
 		char debug_orig[READ_CONFIG_BUF_SIZE];
+		char *p;
 
 		lm++;
-		if (strchr(buffer, '\n')) *(strchr(buffer, '\n')) = '\0';
+		p = strchr(buffer, '\n');
+		if (p) *p = '\0';
 		if (ENABLE_FEATURE_UDHCP_DEBUG) strcpy(debug_orig, buffer);
-		if (strchr(buffer, '#')) *(strchr(buffer, '#')) = '\0';
+		p = strchr(buffer, '#');
+		if (p) *p = '\0';
 
 		if (!(token = strtok(buffer, " \t"))) continue;
 		if (!(line = strtok(NULL, ""))) continue;
 
 		/* eat leading whitespace */
-		line = line + strspn(line, " \t=");
+		line = skip_whitespace(line);
 		/* eat trailing whitespace */
-		for (i = strlen(line); i > 0 && isspace(line[i - 1]); i--);
-		line[i] = '\0';
+		i = strlen(line) - 1;
+		while (i >= 0 && isspace(line[i]))
+			line[i--] = '\0';
 
 		for (i = 0; keywords[i].keyword[0]; i++)
 			if (!strcasecmp(token, keywords[i].keyword))
@@ -311,14 +316,14 @@
 
 void write_leases(void)
 {
-	FILE *fp;
-	unsigned int i;
-	char buf[255];
+	int fp;
+	unsigned i;
 	time_t curr = time(0);
 	unsigned long tmp_time;
 
-	if (!(fp = fopen(server_config.lease_file, "w"))) {
-		bb_error_msg("unable to open %s for writing", server_config.lease_file);
+	fp = open(server_config.lease_file, O_WRONLY|O_CREAT|O_TRUNC, 0666);
+	if (fp < 0) {
+		bb_error_msg("cannot open %s for writing", server_config.lease_file);
 		return;
 	}
 
@@ -334,33 +339,38 @@
 				else leases[i].expires -= curr;
 			} /* else stick with the time we got */
 			leases[i].expires = htonl(leases[i].expires);
-			fwrite(&leases[i], sizeof(struct dhcpOfferedAddr), 1, fp);
+			// FIXME: error check??
+			full_write(fp, &leases[i], sizeof(leases[i]));
 
-			/* Then restore it when done. */
+			/* then restore it when done */
 			leases[i].expires = tmp_time;
 		}
 	}
-	fclose(fp);
+	close(fp);
 
 	if (server_config.notify_file) {
-		sprintf(buf, "%s %s", server_config.notify_file, server_config.lease_file);
-		system(buf);
+		char *cmd = xasprintf("%s %s", server_config.notify_file, server_config.lease_file);
+		system(cmd);
+		free(cmd);
 	}
 }
 
 
 void read_leases(const char *file)
 {
-	FILE *fp;
+	int fp;
 	unsigned int i = 0;
 	struct dhcpOfferedAddr lease;
 
-	if (!(fp = fopen(file, "r"))) {
-		bb_error_msg("unable to open %s for reading", file);
+	fp = open(file, O_RDONLY);
+	if (fp < 0) {
+		bb_error_msg("cannot open %s for reading", file);
 		return;
 	}
 
-	while (i < server_config.max_leases && (fread(&lease, sizeof lease, 1, fp) == 1)) {
+	while (i < server_config.max_leases
+	 && full_read(fp, &lease, sizeof(lease)) == sizeof(lease)
+	) {
 		/* ADDME: is it a static lease */
 		if (lease.yiaddr >= server_config.start && lease.yiaddr <= server_config.end) {
 			lease.expires = ntohl(lease.expires);
@@ -373,5 +383,5 @@
 		}
 	}
 	DEBUG("Read %d leases", i);
-	fclose(fp);
+	close(fp);
 }

Modified: trunk/busybox/networking/udhcp/pidfile.c
===================================================================
--- trunk/busybox/networking/udhcp/pidfile.c	2006-11-18 19:51:32 UTC (rev 16578)
+++ trunk/busybox/networking/udhcp/pidfile.c	2006-11-18 22:03:26 UTC (rev 16579)
@@ -23,7 +23,7 @@
 #include "common.h"
 
 
-static char *saved_pidfile;
+static const char *saved_pidfile;
 
 static void pidfile_delete(void)
 {
@@ -36,14 +36,14 @@
 	int pid_fd;
 	if (!pidfile) return -1;
 
-	pid_fd = open(pidfile, O_CREAT | O_WRONLY, 0644);
+	pid_fd = open(pidfile, O_CREAT|O_WRONLY|O_TRUNC, 0644);
 	if (pid_fd < 0) {
-		bb_perror_msg("unable to open pidfile %s", pidfile);
+		bb_perror_msg("cannot open pidfile %s", pidfile);
 	} else {
 		lockf(pid_fd, F_LOCK, 0);
 		if (!saved_pidfile)
 			atexit(pidfile_delete);
-		saved_pidfile = (char *) pidfile;
+		saved_pidfile = pidfile;
 	}
 
 	return pid_fd;




More information about the busybox-cvs mailing list