Once again, here is my patch that add pidfile support to crond and
syslogd and simplify the pidfile code of inetd.
Code is only enabled if CONFIG_FEATURE_PIDFILE is selected.

With the patch, people who don't need pidfiles at all, because they're
using runsv or simply don't care can get a smaller busybox with no pidfile
at all and those, like me, who want them can enable them so that crond
and syslogd behave like the traditional versions.

The patch does not address udhcpc/udhcpd which still have a distinct
pidfile writing code.

Could this be merged in the trunk?

Thank you,

-- 
Stéphane Billiart                      http://perso.orange.fr/billiart/
Index: networking/inetd.c
===================================================================
--- networking/inetd.c	(revision 18251)
+++ networking/inetd.c	(working copy)
@@ -1212,7 +1212,7 @@
 		}
 		(void) close(sep->se_fd);
 	}
-	(void) unlink(_PATH_INETDPID);
+	removepidfile(_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);
-		}
-	}
+	writepidfile(_PATH_INETDPID);
 
 	if (getrlimit(RLIMIT_NOFILE, &rlim_ofile) < 0) {
 		bb_perror_msg("getrlimit");
Index: sysklogd/syslogd.c
===================================================================
--- sysklogd/syslogd.c	(revision 18251)
+++ sysklogd/syslogd.c	(working copy)
@@ -519,6 +519,7 @@
 	signal(SIGALRM, do_mark);
 	alarm(G.markInterval);
 #endif
+	removepidfile("/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);
+    writepidfile("/var/run/syslogd.pid");
 	do_syslogd();
 	/* return EXIT_SUCCESS; */
 }
Index: libbb/pidfile.c
===================================================================
--- libbb/pidfile.c	(revision 0)
+++ libbb/pidfile.c	(revision 0)
@@ -0,0 +1,25 @@
+/* vi: set sw=4 ts=4: */
+/*
+ * pid file routines
+ *
+ * Copyright (C) 2006 by Stephane Billiart <[EMAIL PROTECTED]>
+ *
+ * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
+ */
+#include <stdio.h>
+#include <errno.h>
+#include "libbb.h"
+
+#ifdef CONFIG_FEATURE_PIDFILE
+extern int writepidfile(const char *path)
+{
+	FILE *pidf;
+
+	if ((pidf = fopen_or_warn(path, "w")) != NULL) {
+		fprintf(pidf, "%u\n", getpid());
+		fclose(pidf);
+		return 0;
+	}
+	return 1;
+}
+#endif
Index: libbb/Kbuild
===================================================================
--- libbb/Kbuild	(revision 18251)
+++ libbb/Kbuild	(working copy)
@@ -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
Index: miscutils/crond.c
===================================================================
--- miscutils/crond.c	(revision 18251)
+++ miscutils/crond.c	(working copy)
@@ -185,6 +185,7 @@
 		int rescan = 60;
 		short sleep_time = 60;
 
+		writepidfile("/var/run/crond.pid");
 		for (;;) {
 			sleep((sleep_time + 1) - (short) (time(NULL) % sleep_time));
 
Index: include/libbb.h
===================================================================
--- include/libbb.h	(revision 18251)
+++ include/libbb.h	(working copy)
@@ -542,6 +542,14 @@
 extern void llist_free(llist_t *elm, void (*freeit)(void *data));
 extern llist_t* llist_rev(llist_t *list);
 
+#ifdef CONFIG_FEATURE_PIDFILE
+extern int writepidfile(const char *path);
+#define removepidfile(f) remove_file(f, FILEUTILS_FORCE)
+#else
+#define writepidfile(f) ((void)0)
+#define removepidfile(f) ((void)0)
+#endif
+
 enum {
 	LOGMODE_NONE = 0,
 	LOGMODE_STDIO = 1<<0,
Index: Config.in
===================================================================
--- Config.in	(revision 18251)
+++ Config.in	(working copy)
@@ -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
_______________________________________________
busybox mailing list
[email protected]
http://busybox.net/cgi-bin/mailman/listinfo/busybox

Reply via email to