From owner-freebsd-bugs@FreeBSD.ORG Wed Sep 3 22:50:19 2003 Return-Path: Delivered-To: freebsd-bugs@hub.freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 716B516A4BF for ; Wed, 3 Sep 2003 22:50:19 -0700 (PDT) Received: from freefall.freebsd.org (freefall.freebsd.org [216.136.204.21]) by mx1.FreeBSD.org (Postfix) with ESMTP id F3FC743FF9 for ; Wed, 3 Sep 2003 22:50:14 -0700 (PDT) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (gnats@localhost [127.0.0.1]) by freefall.freebsd.org (8.12.9/8.12.9) with ESMTP id h845oEUp096289 for ; Wed, 3 Sep 2003 22:50:14 -0700 (PDT) (envelope-from gnats@freefall.freebsd.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.12.9/8.12.9/Submit) id h845oEuG096288; Wed, 3 Sep 2003 22:50:14 -0700 (PDT) Resent-Date: Wed, 3 Sep 2003 22:50:14 -0700 (PDT) Resent-Message-Id: <200309040550.h845oEuG096288@freefall.freebsd.org> Resent-From: FreeBSD-gnats-submit@FreeBSD.org (GNATS Filer) Resent-To: freebsd-bugs@FreeBSD.org Resent-Reply-To: FreeBSD-gnats-submit@FreeBSD.org, Mikhail Teterin Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id B55BB16A4BF for ; Wed, 3 Sep 2003 22:42:38 -0700 (PDT) Received: from aldan.algebra.com (aldan.algebra.com [216.254.65.224]) by mx1.FreeBSD.org (Postfix) with ESMTP id 9B78943FF5 for ; Wed, 3 Sep 2003 22:42:37 -0700 (PDT) (envelope-from mi@aldan.algebra.com) Received: from aldan.algebra.com (mi@localhost [127.0.0.1]) by aldan.algebra.com (8.12.9/8.12.9) with ESMTP id h845gYYI023320 for ; Thu, 4 Sep 2003 01:42:35 -0400 (EDT) (envelope-from mi@aldan.algebra.com) Received: (from mi@localhost) by aldan.algebra.com (8.12.9/8.12.9/Submit) id h845gYXK023319; Thu, 4 Sep 2003 01:42:34 -0400 (EDT) Message-Id: <200309040542.h845gYXK023319@aldan.algebra.com> Date: Thu, 4 Sep 2003 01:42:34 -0400 (EDT) From: Mikhail Teterin To: FreeBSD-gnats-submit@FreeBSD.org X-Send-Pr-Version: 3.113 Subject: bin/56398: new option for daemon(8) -- pidfile [patch] X-BeenThere: freebsd-bugs@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list Reply-To: Mikhail Teterin List-Id: Bug reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 04 Sep 2003 05:50:19 -0000 >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 +#include #include #include @@ -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: