svn commit: trunk/busybox: include libbb miscutils networking netw etc...

vda at busybox.net vda at busybox.net
Tue Mar 27 22:01:34 UTC 2007


Author: vda
Date: 2007-03-27 15:01:31 -0700 (Tue, 27 Mar 2007)
New Revision: 18254

Log:
make pidfile writing configurable.
[ui]toa_to_buf: change API. No users yet.


Modified:
   trunk/busybox/Config.in
   trunk/busybox/include/libbb.h
   trunk/busybox/libbb/Kbuild
   trunk/busybox/libbb/xfuncs.c
   trunk/busybox/miscutils/crond.c
   trunk/busybox/networking/inetd.c
   trunk/busybox/networking/udhcp/common.c
   trunk/busybox/shell/msh.c
   trunk/busybox/sysklogd/syslogd.c


Changeset:
Modified: trunk/busybox/Config.in
===================================================================
--- trunk/busybox/Config.in	2007-03-26 23:14:38 UTC (rev 18253)
+++ trunk/busybox/Config.in	2007-03-27 22:01:31 UTC (rev 18254)
@@ -141,6 +141,13 @@
 	  Don't enable this unless you have a really good reason to clean
 	  things up manually.
 
+config FEATURE_PIDFILE
+	bool "Support writing pidfiles"
+	default n
+	help
+	  This option makes some applets (crond, syslogd and inetd) write
+	  a pidfile in /var/run. Some applications rely on them
+
 config FEATURE_SUID
 	bool "Support for SUID/SGID handling"
 	default n

Modified: trunk/busybox/include/libbb.h
===================================================================
--- trunk/busybox/include/libbb.h	2007-03-26 23:14:38 UTC (rev 18253)
+++ trunk/busybox/include/libbb.h	2007-03-27 22:01:31 UTC (rev 18254)
@@ -408,10 +408,11 @@
 extern FILE *fopen_or_warn_stdin(const char *filename);
 
 
-extern void utoa_to_buf(unsigned n, char *buf, unsigned buflen);
 extern char *utoa(unsigned n);
-extern void itoa_to_buf(int n, char *buf, unsigned buflen);
 extern char *itoa(int n);
+/* Returns a pointer past the formatted number, does NOT null-terminate */
+extern char *utoa_to_buf(unsigned n, char *buf, unsigned buflen);
+extern char *itoa_to_buf(int n, char *buf, unsigned buflen);
 extern void smart_ulltoa5(unsigned long long ul, char buf[5]);
 /* Put a string of hex bytes (ala "1b"), return advanced pointer */
 extern char *bin2hex(char *buf, const char *cp, int count);
@@ -542,6 +543,14 @@
 extern void llist_free(llist_t *elm, void (*freeit)(void *data));
 extern llist_t* llist_rev(llist_t *list);
 
+#if ENABLE_FEATURE_PIDFILE
+int write_pidfile(const char *path);
+#define remove_pidfile(f) ((void)unlink(f))
+#else
+#define write_pidfile(f)  1
+#define remove_pidfile(f) ((void)0)
+#endif
+
 enum {
 	LOGMODE_NONE = 0,
 	LOGMODE_STDIO = 1<<0,

Modified: trunk/busybox/libbb/Kbuild
===================================================================
--- trunk/busybox/libbb/Kbuild	2007-03-26 23:14:38 UTC (rev 18253)
+++ trunk/busybox/libbb/Kbuild	2007-03-27 22:01:31 UTC (rev 18254)
@@ -61,6 +61,7 @@
 lib-y += perror_msg_and_die.o
 lib-y += perror_nomsg.o
 lib-y += perror_nomsg_and_die.o
+lib-y += pidfile.o
 lib-y += process_escape_sequence.o
 lib-y += procps.o
 lib-y += read.o

Modified: trunk/busybox/libbb/xfuncs.c
===================================================================
--- trunk/busybox/libbb/xfuncs.c	2007-03-26 23:14:38 UTC (rev 18253)
+++ trunk/busybox/libbb/xfuncs.c	2007-03-27 22:01:31 UTC (rev 18254)
@@ -257,7 +257,7 @@
 // truncated result is always null terminated (unless buflen is 0), and
 // contains the first few digits of the result ala strncpy.
 void BUG_sizeof_unsigned_not_4(void);
-void utoa_to_buf(unsigned n, char *buf, unsigned buflen)
+char *utoa_to_buf(unsigned n, char *buf, unsigned buflen)
 {
 	unsigned i, out, res;
 	if (sizeof(unsigned) != 4)
@@ -273,19 +273,19 @@
 				*buf++ = '0' + res;
 			}
 		}
-		*buf = '\0';
 	}
+	return buf;
 }
 
 // Convert signed integer to ascii, like utoa_to_buf()
-void itoa_to_buf(int n, char *buf, unsigned buflen)
+char *itoa_to_buf(int n, char *buf, unsigned buflen)
 {
 	if (buflen && n<0) {
 		n = -n;
 		*buf++ = '-';
 		buflen--;
 	}
-	utoa_to_buf((unsigned)n, buf, buflen);
+	return utoa_to_buf((unsigned)n, buf, buflen);
 }
 
 // The following two functions use a static buffer, so calling either one a
@@ -300,7 +300,7 @@
 // Convert unsigned integer to ascii using a static buffer (returned).
 char *utoa(unsigned n)
 {
-	utoa_to_buf(n, local_buf, sizeof(local_buf));
+	*(utoa_to_buf(n, local_buf, sizeof(local_buf))) = '\0';
 
 	return local_buf;
 }
@@ -308,7 +308,7 @@
 // Convert signed integer to ascii using a static buffer (returned).
 char *itoa(int n)
 {
-	itoa_to_buf(n, local_buf, sizeof(local_buf));
+	*(itoa_to_buf(n, local_buf, sizeof(local_buf))) = '\0';
 
 	return local_buf;
 }

Modified: trunk/busybox/miscutils/crond.c
===================================================================
--- trunk/busybox/miscutils/crond.c	2007-03-26 23:14:38 UTC (rev 18253)
+++ trunk/busybox/miscutils/crond.c	2007-03-27 22:01:31 UTC (rev 18254)
@@ -185,6 +185,7 @@
 		int rescan = 60;
 		short sleep_time = 60;
 
+		write_pidfile("/var/run/crond.pid");
 		for (;;) {
 			sleep((sleep_time + 1) - (short) (time(NULL) % sleep_time));
 

Modified: trunk/busybox/networking/inetd.c
===================================================================
--- trunk/busybox/networking/inetd.c	2007-03-26 23:14:38 UTC (rev 18253)
+++ trunk/busybox/networking/inetd.c	2007-03-27 22:01:31 UTC (rev 18254)
@@ -1212,7 +1212,7 @@
 		}
 		(void) close(sep->se_fd);
 	}
-	(void) unlink(_PATH_INETDPID);
+	remove_pidfile(_PATH_INETDPID);
 	exit(0);
 }
 
@@ -1301,13 +1301,7 @@
 		setgroups(1, &gid);
 	}
 
-	{
-		FILE *fp = fopen(_PATH_INETDPID, "w");
-		if (fp != NULL) {
-			fprintf(fp, "%u\n", getpid());
-			fclose(fp);
-		}
-	}
+	write_pidfile(_PATH_INETDPID);
 
 	if (getrlimit(RLIMIT_NOFILE, &rlim_ofile) < 0) {
 		bb_perror_msg("getrlimit");

Modified: trunk/busybox/networking/udhcp/common.c
===================================================================
--- trunk/busybox/networking/udhcp/common.c	2007-03-26 23:14:38 UTC (rev 18253)
+++ trunk/busybox/networking/udhcp/common.c	2007-03-27 22:01:31 UTC (rev 18254)
@@ -22,47 +22,35 @@
 	return info.uptime;
 }
 
-
+#if ENABLE_FEATURE_PIDFILE
 static const char *saved_pidfile;
 
 static void pidfile_delete(void)
 {
 	if (saved_pidfile)
-		unlink(saved_pidfile);
+		remove_pidfile(saved_pidfile);
 }
+#endif
 
-static int pidfile_acquire(const char *pidfile)
+static void create_pidfile(const char *pidfile)
 {
-	int pid_fd;
-	if (!pidfile) return -1;
+	if (!pidfile)
+		return;
 
-	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;
+	if (!write_pidfile(pidfile)) {
+		bb_perror_msg("cannot create pidfile %s", pidfile);
+		return;
 	}
-
-	return pid_fd;
+#if ENABLE_FEATURE_PIDFILE
+	/* lockf(pid_fd, F_LOCK, 0); */
+	if (!saved_pidfile)
+		atexit(pidfile_delete);
+	saved_pidfile = pidfile;
+#endif
 }
 
-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 fd 0,1,2 are open */
 	bb_sanitize_stdio();
 
@@ -70,8 +58,7 @@
 	setlinebuf(stdout);
 
 	/* Create pidfile */
-	pid_fd = pidfile_acquire(pidfile);
-	pidfile_write_release(pid_fd);
+	create_pidfile(pidfile);
 
 	bb_info_msg("%s (v%s) started", applet_name, BB_VER);
 }

Modified: trunk/busybox/shell/msh.c
===================================================================
--- trunk/busybox/shell/msh.c	2007-03-26 23:14:38 UTC (rev 18253)
+++ trunk/busybox/shell/msh.c	2007-03-27 22:01:31 UTC (rev 18254)
@@ -49,7 +49,7 @@
 {
 	return NULL;
 }
-static void utoa_to_buf(unsigned n, char *buf, unsigned buflen)
+static char *utoa_to_buf(unsigned n, char *buf, unsigned buflen)
 {
 	unsigned i, out, res;
 	assert(sizeof(unsigned) == 4);
@@ -64,22 +64,22 @@
 	    			*buf++ = '0' + res;
 			}
 		}
-		*buf = '\0';
 	}
+	return buf;
 }
-static void itoa_to_buf(int n, char *buf, unsigned buflen)
+static char *itoa_to_buf(int n, char *buf, unsigned buflen)
 {
 	if (buflen && n < 0) {
 		n = -n;
 		*buf++ = '-';
 		buflen--;
 	}
-	utoa_to_buf((unsigned)n, buf, buflen);
+	return utoa_to_buf((unsigned)n, buf, buflen);
 }
 static char local_buf[12];
 static char *itoa(int n)
 {
-	itoa_to_buf(n, local_buf, sizeof(local_buf));
+	*(itoa_to_buf(n, local_buf, sizeof(local_buf))) = '\0';
 	return local_buf;
 }
 #else

Modified: trunk/busybox/sysklogd/syslogd.c
===================================================================
--- trunk/busybox/sysklogd/syslogd.c	2007-03-26 23:14:38 UTC (rev 18253)
+++ trunk/busybox/sysklogd/syslogd.c	2007-03-27 22:01:31 UTC (rev 18254)
@@ -519,6 +519,7 @@
 	signal(SIGALRM, do_mark);
 	alarm(G.markInterval);
 #endif
+	remove_pidfile("/var/run/syslogd.pid");
 
 	memset(&sunx, 0, sizeof(sunx));
 	sunx.sun_family = AF_UNIX;
@@ -645,6 +646,7 @@
 		bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
 	}
 	umask(0);
+	write_pidfile("/var/run/syslogd.pid");
 	do_syslogd();
 	/* return EXIT_SUCCESS; */
 }




More information about the busybox-cvs mailing list