Patch for BusyBox httpd

Stephane Billiart stephane.billiart at gmail.com
Tue Jul 11 13:00:27 UTC 2006


On 11/07/06 ? ? 14:14, Richard Braun wrote:
> On Tue, Jul 11, 2006 at 02:00:07PM +0200, Bernhard Fischer wrote:
> > I believe that you should use start-stop-daemon -m /var/run/httpd.pid -x
> > ..... to create pidfiles. To me, it doesn't make sense to equip the
> > individual daemons with redundant functionality (introducing
> > x{read,write}_pidfile and using that also sounds like a bad idea to me).
> 
> The problem with start-stop-daemon is that it relies on the daemon to
> create its PID file. When the daemon doesn't, it usually provides an
> option to force running in the foreground, so that you can use
> start-stop-daemon with --background and --make-pidfile. The problem with
> busybox httpd is that it doesn't provide such an option. So when
> using start-stop-daemon, you never get the right PID, only the PID of
> the parent httpd process, before daemon() is called...
> 
> -- 
> Richard Braun

As an alternative, here is a patch I personnally use and tried to submit
to the list a while back that creates a writepidfile function in libbb
to write PID files, remove redundancy where applicable (fakeidentd,
inetd) and add the pidfile feature to crond and syslogd.
To me, it makes sense to have library function for writing pidfiles.

The patch applies to 1.1.3 but also works with the SVN head.

-- 
Stéphane Billiart                      http://perso.orange.fr/billiart/
-------------- next part --------------
diff -ruN busybox-1.1.3/include/libbb.h busybox-1.1.3.new/include/libbb.h
--- busybox-1.1.3/include/libbb.h	2006-05-17 20:51:46.000000000 +0200
+++ busybox-1.1.3.new/include/libbb.h	2006-05-20 21:37:39.000000000 +0200
@@ -59,6 +59,8 @@
 #define	MAX(a,b) (((a)>(b))?(a):(b))
 #endif
 
+extern int bb_writepidfile(const char *path);
+#define bb_removepidfile(f) remove_file(f, FILEUTILS_FORCE)
 extern void bb_show_usage(void) ATTRIBUTE_NORETURN ATTRIBUTE_EXTERNALLY_VISIBLE;
 extern void bb_error_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2)));
 extern void bb_error_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2)));
diff -ruN busybox-1.1.3/libbb/Makefile.in busybox-1.1.3.new/libbb/Makefile.in
--- busybox-1.1.3/libbb/Makefile.in	2006-04-10 21:45:46.000000000 +0200
+++ busybox-1.1.3.new/libbb/Makefile.in	2006-05-20 21:27:16.000000000 +0200
@@ -22,7 +22,7 @@
 	kernel_version.c last_char_is.c login.c \
 	make_directory.c md5.c mode_string.c mtab_file.c \
 	obscure.c parse_mode.c parse_number.c perror_msg.c \
-	perror_msg_and_die.c print_file.c get_console.c \
+	perror_msg_and_die.c pidfile.c print_file.c get_console.c \
 	process_escape_sequence.c procps.c qmodule.c \
 	read_package_field.c recursive_action.c remove_file.c \
 	restricted_shell.c run_parts.c run_shell.c safe_read.c safe_write.c \
diff -ruN busybox-1.1.3/libbb/pidfile.c busybox-1.1.3.new/libbb/pidfile.c
--- busybox-1.1.3/libbb/pidfile.c	1970-01-01 01:00:00.000000000 +0100
+++ busybox-1.1.3.new/libbb/pidfile.c	2006-05-20 21:38:18.000000000 +0200
@@ -0,0 +1,45 @@
+/* vi: set sw=4 ts=4: */
+/*
+ * Utility routines.
+ *
+ * Copyright (C) 1999-2004 by Erik Andersen <andersen at codepoet.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include <stdio.h>
+#include <errno.h>
+#include "libbb.h"
+
+extern int bb_writepidfile(const char *path)
+{
+	FILE *pidf;
+
+	if ((pidf = bb_wfopen(path, "w")) != NULL) {
+		fprintf(pidf, "%u\n", getpid());
+		fclose(pidf);
+		return 0;
+	}
+	return 1;
+}
+
+/* END CODE */
+/*
+Local Variables:
+c-file-style: "linux"
+c-basic-offset: 4
+tab-width: 4
+End:
+*/
diff -ruN busybox-1.1.3/miscutils/crond.c busybox-1.1.3.new/miscutils/crond.c
--- busybox-1.1.3/miscutils/crond.c	2006-03-22 22:16:24.000000000 +0100
+++ busybox-1.1.3.new/miscutils/crond.c	2006-05-20 21:28:43.000000000 +0200
@@ -238,6 +238,7 @@
 		int rescan = 60;
 		short sleep_time = 60;
 
+		bb_writepidfile("/var/run/crond.pid");
 		for (;;) {
 			sleep((sleep_time + 1) - (short) (time(NULL) % sleep_time));
 
diff -ruN busybox-1.1.3/networking/fakeidentd.c busybox-1.1.3.new/networking/fakeidentd.c
--- busybox-1.1.3/networking/fakeidentd.c	2006-03-22 22:16:19.000000000 +0100
+++ busybox-1.1.3.new/networking/fakeidentd.c	2006-05-20 21:33:20.000000000 +0200
@@ -138,31 +138,10 @@
 
 static void handlexitsigs(int signum)
 {
-	if (unlink(PIDFILE) < 0)
-		close(open(PIDFILE, O_WRONLY|O_CREAT|O_TRUNC, 0644));
+	bb_removepidfile(PIDFILE);
 	exit(0);
 }
 
-/* May succeed. If not, won't care. */
-static inline void writepid(uid_t nobody, uid_t nogrp)
-{
-	char buf[24];
-	int fd = open(PIDFILE, O_WRONLY|O_CREAT|O_TRUNC, 0664);
-
-	if (fd < 0)
-		return;
-
-	snprintf(buf, 23, "%d\n", getpid());
-	write(fd, buf, strlen(buf));
-	fchown(fd, nobody, nogrp);
-	close(fd);
-
-	/* should this handle ILL, ... (see signal(7)) */
-	signal(SIGTERM, handlexitsigs);
-	signal(SIGINT,  handlexitsigs);
-	signal(SIGQUIT, handlexitsigs);
-}
-
 /* return 0 as parent, 1 as child */
 static int godaemon(void)
 {
@@ -179,7 +158,11 @@
 			bb_error_msg_and_die("Cannot find uid/gid of user '%s'", nobodystr);
 		nobody = pw->pw_uid;
 		nogrp = pw->pw_gid;
-		writepid(nobody, nogrp);
+		bb_writepidfile(PIDFILE);
+		/* should this handle KILL, ... (see signal(7)) */
+		signal(SIGTERM, handlexitsigs);
+		signal(SIGINT,  handlexitsigs);
+		signal(SIGQUIT, handlexitsigs);
 
 		close(0);
 		inetbind();
diff -ruN busybox-1.1.3/networking/inetd.c busybox-1.1.3.new/networking/inetd.c
--- busybox-1.1.3/networking/inetd.c	2006-03-22 22:16:19.000000000 +0100
+++ busybox-1.1.3.new/networking/inetd.c	2006-05-20 21:34:33.000000000 +0200
@@ -1224,7 +1224,7 @@
 	}
 	(void) close (sep->se_fd);
   }
-  (void) unlink (_PATH_INETDPID);
+  bb_removepidfile (_PATH_INETDPID);
   exit (0);
 }
 
@@ -1327,14 +1327,7 @@
 	setgroups (1, &gid);
   }
 
-  {
-	FILE *fp;
-
-	if ((fp = fopen (_PATH_INETDPID, "w")) != NULL) {
-		fprintf (fp, "%u\n", getpid ());
-		(void) fclose (fp);
-	}
-  }
+  bb_writepidfile (_PATH_INETDPID);
 
   if (getrlimit (RLIMIT_NOFILE, &rlim_ofile) < 0) {
 	syslog (LOG_ERR, "getrlimit: %m");
diff -ruN busybox-1.1.3/sysklogd/syslogd.c busybox-1.1.3.new/sysklogd/syslogd.c
--- busybox-1.1.3/sysklogd/syslogd.c	2006-03-22 22:16:20.000000000 +0100
+++ busybox-1.1.3.new/sysklogd/syslogd.c	2006-05-20 21:30:32.000000000 +0200
@@ -459,6 +459,7 @@
 #ifdef CONFIG_FEATURE_IPC_SYSLOG
 	ipcsyslog_cleanup();
 #endif
+	bb_removepidfile("/var/run/syslogd.pid");
 
 	exit(TRUE);
 }
@@ -686,6 +687,7 @@
 			bb_perror_msg_and_die("daemon");
 #endif /* __uClinux__ */
 	}
+	bb_writepidfile("/var/run/syslogd.pid");
 	doSyslogd();
 
 	return EXIT_SUCCESS;


More information about the busybox mailing list