Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 4 Sep 2003 01:42:34 -0400 (EDT)
From:      Mikhail Teterin <mi@aldan.algebra.com>
To:        FreeBSD-gnats-submit@FreeBSD.org
Subject:   bin/56398: new option for daemon(8) -- pidfile [patch]
Message-ID:  <200309040542.h845gYXK023319@aldan.algebra.com>
Resent-Message-ID: <200309040550.h845oEuG096288@freefall.freebsd.org>

next in thread | raw e-mail | index | archive | help

>Number:         56398
>Category:       bin
>Synopsis:       new option for daemon(8) -- pidfile [patch]
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Wed Sep 03 22:50:14 PDT 2003
>Closed-Date:
>Last-Modified:
>Originator:     Mikhail Teterin
>Release:        FreeBSD 5.1-CURRENT i386
>Organization:
Virtual Estates, Inc.
>Environment:
System: FreeBSD aldan.algebra.com 5.1-CURRENT FreeBSD 5.1-CURRENT #5: Sun Jul 13 20:28:24 EDT 2003 mi@aldan.algebra.com:/ccd/obj/ccd/src/sys/DEBUG i386

>Description:

	The daemon(8) is a nifty utility. However, its usability
	in rc.d/ scripts is limited, because there is no easy way
	to get the process id of the started daemon.

	Once added and MFCed, the feature can be exploited by the
	port-maintainers and others.

>How-To-Repeat:

>Fix:

Index: daemon.8
===================================================================
RCS file: /home/ncvs/src/usr.sbin/daemon/daemon.8,v
retrieving revision 1.3
diff -U2 -r1.3 daemon.8
--- daemon.8	5 Feb 2003 19:16:18 -0000	1.3
+++ daemon.8	4 Sep 2003 05:41:40 -0000
@@ -36,4 +36,5 @@
 .Nm
 .Op Fl cf
+.Op Fl p Ar pidfile
 .Ar command arguments ...
 .Sh DESCRIPTION
@@ -51,4 +52,10 @@
 Redirect standard input, standard output and standard error to
 .Pa /dev/null .
+.It Fl p Ar file
+Write the id of the created process into the
+.Ar file .
+Note, that the file will be created shortly before the process is
+actually executed, and will remain after the process exits (although
+it will be removed if the execution fails).
 .El
 .Sh DIAGNOSTICS
@@ -57,5 +64,6 @@
 utility exits 1 if an error is returned by the
 .Xr daemon 3
-library routine, otherwise 0.
+library routine, 2 if the pid-file is requested, but can not be opened,
+otherwise 0.
 If the command cannot be executed, an error message is displayed on
 standard error unless the
Index: daemon.c
===================================================================
RCS file: /home/ncvs/src/usr.sbin/daemon/daemon.c,v
retrieving revision 1.2
diff -U2 -r1.2 daemon.c
--- daemon.c	6 Jul 2003 12:44:11 -0000	1.2
+++ daemon.c	4 Sep 2003 05:41:40 -0000
@@ -35,4 +35,5 @@
 
 #include <err.h>
+#include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -44,8 +45,11 @@
 main(int argc, char *argv[])
 {
-	int ch, nochdir, noclose;
+	int ch, nochdir, noclose, errcode;
+	FILE *pidf;
+	const char *pidfile;
 
 	nochdir = noclose = 1;
-	while ((ch = getopt(argc, argv, "-cf")) != -1) {
+	pidfile = NULL;
+	while ((ch = getopt(argc, argv, "-cfp:")) != -1) {
 		switch (ch) {
 		case 'c':
@@ -55,5 +59,7 @@
 			noclose = 0;
 			break;
-		case '?':
+		case 'p':
+			pidfile = optarg;
+			break;
 		default:
 			usage();
@@ -65,10 +71,35 @@
 	if (argc == 0)
 		usage();
+	/*
+	 * Try to open the pidfile before calling daemon(3),
+	 * to be able to report the error intelligently
+	 */
+	if (pidfile) {
+		pidf = fopen(pidfile, "w");
+		if (pidf == NULL)
+			err(2, "pidfile ``%s''", pidfile);
+	}
+
 	if (daemon(nochdir, noclose) == -1)
 		err(1, NULL);
+
+	/* Now that we are the child, write out the pid */
+	if (pidfile) {
+		fprintf(pidf, "%lu\n", (unsigned long)getpid());
+		fclose(pidf);
+	}
+
 	execvp(argv[0], argv);
 
+	/*
+	 * execvp() failed -- unlink pidfile if any, and
+	 * report the error
+	 */
+	errcode = errno; /* Preserve errcode -- unlink may reset it */
+	if (pidfile)
+		unlink(pidfile);
+
 	/* The child is now running, so the exit status doesn't matter. */
-	err(1, "%s", argv[0]);
+	errc(1, errcode, "%s", argv[0]);
 }
 
@@ -76,5 +107,6 @@
 usage(void)
 {
-	(void)fprintf(stderr, "usage: daemon [-cf] command arguments ...\n");
+	(void)fprintf(stderr,
+	    "usage: daemon [-cf] [-p pidfile] command arguments ...\n");
 	exit(1);
 }
>Release-Note:
>Audit-Trail:
>Unformatted:



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200309040542.h845gYXK023319>