From owner-svn-src-all@FreeBSD.ORG Mon Feb 6 21:46:30 2012 Return-Path: Delivered-To: svn-src-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id CAACC106564A; Mon, 6 Feb 2012 21:46:30 +0000 (UTC) (envelope-from to.my.trociny@gmail.com) Received: from mail-bk0-f54.google.com (mail-bk0-f54.google.com [209.85.214.54]) by mx1.freebsd.org (Postfix) with ESMTP id 8D2528FC08; Mon, 6 Feb 2012 21:46:29 +0000 (UTC) Received: by bkbzx1 with SMTP id zx1so6972502bkb.13 for ; Mon, 06 Feb 2012 13:46:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=from:to:cc:subject:references:x-comment-to:sender:date:in-reply-to :message-id:user-agent:mime-version:content-type; bh=/dzajcqNUoDpCICZErPnX9IbH1VrVyDp4My+97pQ+qM=; b=q7rDN8IlKZey5E+/AdNDlIbpdW2Tpy2Hx4UXZUYD9oW/kw4T+TPEOwgRqWmWB9afMr +Xx8ZRpz5yKNar/D/Yb74dc8rOCIZT11KLTKrZLnIBGKuR7AFaC1G9D/FkfWOi2yzdlU aPwa4AsUqWf66fSNHSWtl9rLYEqTfnfBJ7zyE= Received: by 10.204.154.86 with SMTP id n22mr8978731bkw.85.1328564788645; Mon, 06 Feb 2012 13:46:28 -0800 (PST) Received: from localhost ([95.69.173.122]) by mx.google.com with ESMTPS id cg2sm49215788bkb.12.2012.02.06.13.46.26 (version=TLSv1/SSLv3 cipher=OTHER); Mon, 06 Feb 2012 13:46:27 -0800 (PST) From: Mikolaj Golub To: Pawel Jakub Dawidek References: <201202011641.q11Gf0j6095461@svn.freebsd.org> <20120204074201.GA1694@garage.freebsd.pl> <4F2CEB1D.10607@zonov.org> <27A0A960-F767-4D2C-BF3E-31F73FBF4E28@palisadesystems.com> <86zkcy5ur9.fsf@kopusha.home.net> <20120205093938.GC30033@garage.freebsd.pl> <86lioh7yz5.fsf@kopusha.home.net> <20120205214647.GI30033@garage.freebsd.pl> <86sjiov29o.fsf@in138.ua3> <20120206082706.GA1324@garage.freebsd.pl> X-Comment-To: Pawel Jakub Dawidek Sender: Mikolaj Golub Date: Mon, 06 Feb 2012 23:46:24 +0200 In-Reply-To: <20120206082706.GA1324@garage.freebsd.pl> (Pawel Jakub Dawidek's message of "Mon, 6 Feb 2012 09:27:06 +0100") Message-ID: <86wr7zmy8f.fsf@kopusha.home.net> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.3 (berkeley-unix) MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Cc: svn-src-head@FreeBSD.org, Guy Helmer , svn-src-all@FreeBSD.org, Andrey Zonov , src-committers@FreeBSD.org Subject: Re: svn commit: r230869 - head/usr.sbin/daemon X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 06 Feb 2012 21:46:30 -0000 --=-=-= On Mon, 6 Feb 2012 09:27:06 +0100 Pawel Jakub Dawidek wrote: PJD> For the patch itself. PJD> You don't have to have two separate cases depending on request for PJD> pidfile. You can specify NULL pfh to the pidfile functions. PJD> Even in example from the manual page when pfh is NULL there is a case PJD> where we warn, but continue execution and call pidfile functions. PJD> This should simplify the code. PJD> If you do that (actually even if you don't), remember to either use PJD> warn(3) before pidfile_remove(3) and exit(3) after or preserve errno PJD> before calling pidfile_remove(3), as pidfile_remove(3) can modify it if PJD> unlink(2) is unsuccessful or pfh is NULL. Thanks. The updated version is attached. -- Mikolaj Golub --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=daemon.c.2.patch Index: usr.sbin/daemon/daemon.c =================================================================== --- usr.sbin/daemon/daemon.c (revision 231014) +++ usr.sbin/daemon/daemon.c (working copy) @@ -32,6 +32,7 @@ __FBSDID("$FreeBSD$"); #include +#include #include #include @@ -43,15 +44,16 @@ __FBSDID("$FreeBSD$"); #include static void restrict_process(const char *); +static void wait_child(pid_t pid); static void usage(void); int main(int argc, char *argv[]) { struct pidfh *pfh = NULL; - int ch, nochdir, noclose, errcode; + int ch, nochdir, noclose; const char *pidfile, *user; - pid_t otherpid; + pid_t otherpid, pid; nochdir = noclose = 1; pidfile = user = NULL; @@ -79,9 +81,7 @@ main(int argc, char *argv[]) if (argc == 0) usage(); - if (user != NULL) - restrict_process(user); - + pfh = NULL; /* * Try to open the pidfile before calling daemon(3), * to be able to report the error intelligently @@ -100,22 +100,36 @@ main(int argc, char *argv[]) if (daemon(nochdir, noclose) == -1) err(1, NULL); - /* Now that we are the child, write out the pid */ - if (pidfile) + pid = 0; + if (pidfile) { + /* + * Spawn a child to exec the command, so in the parent + * we could wait for it to exit and remove pidfile. + */ + pid = fork(); + if (pid == -1) { + pidfile_remove(pfh); + err(1, "fork"); + } + } + if (pid == 0) { + /* Now that we are the child, write out the pid. */ pidfile_write(pfh); - execvp(argv[0], argv); + if (user != NULL) + restrict_process(user); - /* - * execvp() failed -- unlink pidfile if any, and - * report the error - */ - errcode = errno; /* Preserve errcode -- unlink may reset it */ - if (pidfile) - pidfile_remove(pfh); + execvp(argv[0], argv); - /* The child is now running, so the exit status doesn't matter. */ - errc(1, errcode, "%s", argv[0]); + /* + * execvp() failed -- report the error. The child is + * now running, so the exit status doesn't matter. + */ + err(1, "%s", argv[0]); + } + wait_child(pid); + pidfile_remove(pfh); + exit(0); /* Exit status does not metter. */ } static void @@ -132,6 +146,19 @@ restrict_process(const char *user) } static void +wait_child(pid_t pid) +{ + int status; + + while (waitpid(pid, &status, 0) == -1) { + if (errno != EINTR) { + warn("waitpid"); + break; + } + } +} + +static void usage(void) { (void)fprintf(stderr, --=-=-=--