This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: [PATCH] nscd: don't fork twice
- From: Andreas Schwab <schwab at linux-m68k dot org>
- To: Andreas Jaeger <aj at suse dot com>
- Cc: Roland McGrath <roland at hack dot frob dot com>, libc-alpha at sourceware dot org, Alexandre Oliva <aoliva at redhat dot com>
- Date: Fri, 12 Apr 2013 11:23:49 +0200
- Subject: Re: [PATCH] nscd: don't fork twice
- References: <mvmip3uzitx dot fsf at hawking dot suse dot de> <20130411222609 dot 0D9412C08F at topped-with-meat dot com> <5167B8B7 dot 7010302 at suse dot com>
Andreas Jaeger <aj@suse.com> writes:
> On 04/12/2013 12:26 AM, Roland McGrath wrote:
>> Is there a BZ# about this? Does the extra fork cause any known harm?
>> Alex and AJ did the change that added it. Perhaps they can confirm
>> that it was a mistake, or else explain the rationale for adding it.
>
> Alex and myself just rearranged the code - the double fork was always there,
It was add here:
commit 2a40b7d
Author: Ulrich Drepper <drepper@redhat.com>
Date: Fri Jul 24 12:58:06 1998 +0000
Don't use daemon(), set signal handler after fork.
diff --git a/nscd/nscd.c b/nscd/nscd.c
index e1c6367..9ddbb5f 100644
--- a/nscd/nscd.c
+++ b/nscd/nscd.c
@@ -116,11 +116,6 @@ main (int argc, char **argv)
exit (EXIT_FAILURE);
}
- signal (SIGINT, termination_handler);
- signal (SIGQUIT, termination_handler);
- signal (SIGTERM, termination_handler);
- signal (SIGPIPE, SIG_IGN);
-
/* Check if we are already running. */
if (check_pid (_PATH_NSCDPID))
{
@@ -131,14 +126,21 @@ main (int argc, char **argv)
/* Behave like a daemon. */
if (go_background)
{
+ int i;
+
+ if (fork ())
+ exit (0);
+
+ for (i = 0; i < getdtablesize (); i++)
+ close (i);
+
+ if (fork ())
+ exit (0);
+
+ chdir ("/");
+
openlog ("nscd", LOG_CONS | LOG_ODELAY, LOG_DAEMON);
- if (daemon (0, 0) < 0)
- {
- fprintf (stderr, _("connot auto-background: %s\n"),
- strerror (errno));
- exit (EXIT_FAILURE);
- }
if (write_pid (_PATH_NSCDPID) < 0)
dbg_log ("%s: %s", _PATH_NSCDPID, strerror (errno));
@@ -147,6 +149,12 @@ main (int argc, char **argv)
signal (SIGTTIN, SIG_IGN);
signal (SIGTSTP, SIG_IGN);
}
+
+ signal (SIGINT, termination_handler);
+ signal (SIGQUIT, termination_handler);
+ signal (SIGTERM, termination_handler);
+ signal (SIGPIPE, SIG_IGN);
+
/* Cleanup files created by a previous `bind' */
unlink (_PATH_NSCDSOCKET);
Andreas.
--
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756 01D3 44D5 214B 8276 4ED5
"And now for something completely different."