bug-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[bug #4938] gdnc should close file descriptors


From: nobody
Subject: [bug #4938] gdnc should close file descriptors
Date: Sat, 23 Aug 2003 10:31:41 -0400
User-agent: Mozilla/5.0 Galeon/1.2.5 (X11; Linux i686; U; ) Gecko/20020623 Debian/1.2.5-0.woody.1

=================== BUG #4938: FULL BUG SNAPSHOT ===================
http://savannah.gnu.org/bugs/?func=detailbug&bug_id=4938&group_id=99

Submitted by: ehz                     Project: GNUstep                      
Submitted on: Sat 08/23/2003 at 16:31
Category:  None                       Severity:  5 - Major                  
Bug Group:  None                      Resolution:  None                     
Assigned to:  None                    Status:  Open                         

Summary:  gdnc should close file descriptors

Original Submission:  Like gdomap and gnustep_sndd, gdnc should close any open 
file descriptors to properly daemonize.

Try something like that :

--- gnustep-base-1.7.2.orig/Tools/gdnc.m
+++ gnustep-base-1.7.2/Tools/gdnc.m
@@ -23,6 +23,7 @@
 
 #include        <stdio.h>
 #include       <unistd.h>
+#include       <fcntl.h>
 
 #ifdef __MINGW__
 #include       "process.h"
@@ -909,8 +910,10 @@
 int
 main(int argc, char** argv, char** env)
 {
+  int                  desc;
   GDNCServer           *server;
   NSString             *str;
+  BOOL                 isDaemon = NO;
   BOOL                 shouldFork = YES;
   BOOL                 debugging = NO;
   CREATE_AUTORELEASE_POOL(pool);
@@ -948,6 +951,7 @@
 #else
   if (shouldFork)
     {
+      isDaemon = YES;
       switch (fork())
        {
          case -1:
@@ -969,6 +973,43 @@
            exit(EXIT_SUCCESS);
        }
     }
+
+  /*
+   *   Ensure we don't have any open file descriptors which may refer
+   *   to sockets bound to ports we may try to use.
+   *
+   *   Use '/dev/null' for stdin and stdout.  Assume stderr is ok.
+   */
+  for (desc = 0; desc < FD_SETSIZE; desc++)
+    {
+      if (isDaemon /*|| (desc != 2)*/)
+       {
+         (void)close(desc);
+       }
+    }
+  if (open("/dev/null", O_RDONLY) != 0)
+    {
+      NSLog(@"gdnc - failed to open stdin from /dev/null (%s)",
+           strerror(errno));
+      exit(EXIT_FAILURE);
+    }
+  if (open("/dev/null", O_WRONLY) != 1)
+    {
+      NSLog(@"gdnc - failed to open stdout from /dev/null (%s)",
+           strerror(errno));
+      exit(EXIT_FAILURE);
+    }
+  if (isDaemon && open("/dev/null", O_WRONLY) != 2)
+    {
+      NSLog(@"gdnc - failed to open stderr from /dev/null (%s)",
+           strerror(errno));
+      exit(EXIT_FAILURE);
+    }
+  if (0)
+    {
+      NSLog(@"gdnc - Closed descriptors");
+    }
+
 #endif /* !MINGW */
 
   {


-----------
P.S.
Is it better to start gdnc at boot time (as recommended by the GNUstep Unix 
Build Guide ) or at login time (as suggested by the gdnc manpage) ?

Eric Heintzmann and the Debian GNUstep maintainers



No Followups Have Been Posted


CC list is empty


File Attachments
****************

-------------------------------------------------------
Date: Sat 08/23/2003 at 16:31  Name: gdnc.m.diff  Size: 1KB   By: ehz
patch for gdnc (close file descriptors)
http://savannah.gnu.org/bugs/download.php?group_id=99&amp;bug_id=4938&amp;bug_file_id=621


For detailed info, follow this link:
http://savannah.gnu.org/bugs/?func=detailbug&bug_id=4938&group_id=99

_______________________________________________
  Message sent via/by Savannah
  http://savannah.gnu.org/





reply via email to

[Prev in Thread] Current Thread [Next in Thread]