This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH] nscd: don't fork twice


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."


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]