start-stop-daemon incorrect pid

Stephane Billiart stephane.billiart at gmail.com
Wed Sep 6 19:46:36 UTC 2006


On 06/09/06 ? ? 11:11, Natanael Copa wrote:
> Creating pids in several applets sounds like bloat.

I've already posted this patch I've been using for months on my system
to create pidfiles for monitoring purposes but it never made it to the
SVN tree.

I split the patch in two parts, the first adds writepidfile/removepidfile
to libbb and the second modifies inetd, syslogd and crond to use it.
fakeidentd is another daemon that could use this but it also does a
fchown on the pidfile so I did not modify it here (I don't use it anyway)

-- 
Stéphane Billiart                      http://perso.orange.fr/billiart/
-------------- next part --------------
Index: libbb/Makefile.in
===================================================================
--- libbb/Makefile.in	(revision 16059)
+++ libbb/Makefile.in	(working copy)
@@ -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 get_console.c \
+	perror_msg_and_die.c pidfile.c get_console.c \
 	process_escape_sequence.c procps.c \
 	recursive_action.c remove_file.c \
 	restricted_shell.c run_parts.c run_shell.c safe_read.c safe_write.c \
Index: libbb/pidfile.c
===================================================================
--- libbb/pidfile.c	(revision 0)
+++ libbb/pidfile.c	(revision 0)
@@ -0,0 +1,47 @@
+/* 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"
+
+#ifdef CONFIG_FEATURE_PIDFILE
+extern int writepidfile(const char *path)
+{
+	FILE *pidf;
+
+	if ((pidf = bb_wfopen(path, "w")) != NULL) {
+		fprintf(pidf, "%u\n", getpid());
+		fclose(pidf);
+		return 0;
+	}
+	return 1;
+}
+#endif
+
+/* END CODE */
+/*
+Local Variables:
+c-file-style: "linux"
+c-basic-offset: 4
+tab-width: 4
+End:
+*/
Index: include/libbb.h
===================================================================
--- include/libbb.h	(revision 16059)
+++ include/libbb.h	(working copy)
@@ -120,6 +120,13 @@
 };
 extern int logmode;
 
+#ifdef CONFIG_FEATURE_PIDFILE
+extern int writepidfile(const char *path);
+#define removepidfile(f) remove_file(f, FILEUTILS_FORCE)
+#else
+#define writepidfile(f)
+#define removepidfile(f)
+#endif
 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)));
Index: Config.in
===================================================================
--- Config.in	(revision 16059)
+++ Config.in	(working copy)
@@ -135,6 +135,12 @@
 	  Don't enable this unless you have a really good reason to clean
 	  things up manually.
 
+config CONFIG_FEATURE_PIDFILE
+	bool "Support for pidfile writing"
+	default n
+	help
+	  Enable code to write pidfiles (crond and syslogd)
+
 config CONFIG_FEATURE_SUID
 	bool "Support for SUID/SGID handling"
 	default n
-------------- next part --------------
Index: networking/inetd.c
===================================================================
--- networking/inetd.c	(revision 16059)
+++ networking/inetd.c	(working copy)
@@ -1191,7 +1191,7 @@
 	}
 	(void) close (sep->se_fd);
   }
-  (void) unlink (_PATH_INETDPID);
+  removepidfile (_PATH_INETDPID);
   exit (0);
 }
 
@@ -1294,14 +1294,7 @@
 	/* If run by hand, ensure groups vector gets trashed */
 	setgroups (1, &gid);
   }
-
-  {
-	FILE *fp;
-
-	if ((fp = fopen (_PATH_INETDPID, "w")) != NULL) {
-		fprintf (fp, "%u\n", getpid ());
-		(void) fclose (fp);
-	}
+  writepidfile (_PATH_INETDPID);
   }
 
   if (getrlimit (RLIMIT_NOFILE, &rlim_ofile) < 0) {
Index: sysklogd/syslogd.c
===================================================================
--- sysklogd/syslogd.c	(revision 16059)
+++ sysklogd/syslogd.c	(working copy)
@@ -425,6 +425,7 @@
 	unlink(lfile);
 	if (ENABLE_FEATURE_IPC_SYSLOG)
 		ipcsyslog_cleanup();
+	removepidfile("/var/run/syslogd.pid");
 
 	exit(TRUE);
 }
@@ -641,6 +642,7 @@
 		xdaemon(0, 1);
 #endif
 	}
+	writepidfile("/var/run/syslogd.pid");
 	doSyslogd();
 
 	return EXIT_SUCCESS;
Index: miscutils/crond.c
===================================================================
--- miscutils/crond.c	(revision 16059)
+++ miscutils/crond.c	(working copy)
@@ -216,6 +216,7 @@
 		int rescan = 60;
 		short sleep_time = 60;
 
+		writepidfile("/var/run/cron.pid");
 		for (;;) {
 			sleep((sleep_time + 1) - (short) (time(NULL) % sleep_time));
 


More information about the busybox mailing list