pidfiles again

Stephane Billiart stephane.billiart at gmail.com
Tue Mar 27 06:10:20 PDT 2007


This is a corrected patch, without O_EXCL in open and with a file mode.

-- 
Stéphane Billiart                      http://perso.orange.fr/billiart/
-------------- next part --------------
diff -ruN busybox.orig/Config.in busybox/Config.in
--- busybox.orig/Config.in	2007-03-27 08:58:06.000000000 -0400
+++ busybox/Config.in	2007-03-26 21:04:34.000000000 -0400
@@ -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
diff -ruN busybox.orig/include/libbb.h busybox/include/libbb.h
--- busybox.orig/include/libbb.h	2007-03-27 08:58:06.000000000 -0400
+++ busybox/include/libbb.h	2007-03-26 21:04:34.000000000 -0400
@@ -542,6 +542,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 writepidfile(const char *path);
+#define removepidfile(f) ((void)unlink(f))
+#else
+#define writepidfile(f) ((void)0)
+#define removepidfile(f) ((void)0)
+#endif
+
 enum {
 	LOGMODE_NONE = 0,
 	LOGMODE_STDIO = 1<<0,
diff -ruN busybox.orig/libbb/Kbuild busybox/libbb/Kbuild
--- busybox.orig/libbb/Kbuild	2007-03-27 08:58:06.000000000 -0400
+++ busybox/libbb/Kbuild	2007-03-26 21:04:34.000000000 -0400
@@ -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
diff -ruN busybox.orig/libbb/pidfile.c busybox/libbb/pidfile.c
--- busybox.orig/libbb/pidfile.c	1969-12-31 19:00:00.000000000 -0500
+++ busybox/libbb/pidfile.c	2007-03-27 09:07:26.000000000 -0400
@@ -0,0 +1,22 @@
+/* vi: set sw=4 ts=4: */
+/*
+ * pid file routines
+ *
+ * Copyright (C) 2007 by Stephane Billiart <stephane.billiart at gmail.com>
+ *
+ * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
+ */
+#include "libbb.h"
+
+#if ENABLE_FEATURE_PIDFILE
+int writepidfile(const char *path)
+{
+	int pid_fd = open(path, O_WRONLY|O_CREAT, 0600;
+	if (pid_fd == -1) {
+		return 1;
+	}
+	fdprintf(pid_fd, "%d\n", getpid());
+	close(pid_fd);
+	return 0;
+}
+#endif
diff -ruN busybox.orig/miscutils/crond.c busybox/miscutils/crond.c
--- busybox.orig/miscutils/crond.c	2007-03-27 08:58:06.000000000 -0400
+++ busybox/miscutils/crond.c	2007-03-26 21:04:34.000000000 -0400
@@ -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));
 
diff -ruN busybox.orig/networking/inetd.c busybox/networking/inetd.c
--- busybox.orig/networking/inetd.c	2007-03-27 08:58:12.000000000 -0400
+++ busybox/networking/inetd.c	2007-03-26 21:04:34.000000000 -0400
@@ -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");
diff -ruN busybox.orig/sysklogd/syslogd.c busybox/sysklogd/syslogd.c
--- busybox.orig/sysklogd/syslogd.c	2007-03-27 08:58:06.000000000 -0400
+++ busybox/sysklogd/syslogd.c	2007-03-26 21:04:34.000000000 -0400
@@ -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; */
 }


More information about the busybox mailing list