[Mauiusers] Patch to fix a possible double free

Josh Butikofer josh at clusterresources.com
Mon Aug 13 15:37:13 MDT 2007


I've implemented something very similar to the TORQUE source code:

Index: src/server/OServer.c
===================================================================
--- src/server/OServer.c        (revision 91)
+++ src/server/OServer.c        (working copy)
@@ -149,6 +149,8 @@
   int   pid;
 #endif /* __NT */

+  FILE *fp;
+
   const char *FName = "ServerDemonize";

   DBG(2,fALL) DPrint("%s()\n",
@@ -179,6 +181,8 @@
       {
       /* only background if not in debug mode */

+      /* NOTE:  setsid() disconnects from controlling-terminal */
+
 #ifndef __NT

       if ((pid = fork()) == -1)
@@ -205,6 +209,28 @@
         DBG(3,fALL) DPrint("INFO:     child process in background\n");
         }

+      if (setsid() == -1)
+        {
+        MDB(3,fALL) MLog("INFO:     could not disconnect from controlling-terminal, errno=%d - %s\n",
+          errno,
+          strerror(errno));
+        }
+
+      /* disconnect stdin */
+
+      fclose(stdin);
+      fp = fopen("/dev/null","r");
+
+      /* disconnect stdout */
+
+      fclose(stdout);
+      fp = fopen("/dev/null","w");
+
+      /* disconnect stderr */
+
+      fclose(stderr);
+      fp = fopen("/dev/null","w");
+
 #endif /* __NT */
       }
     }    /* END if (MSched.Mode != msmSim) */

-- 
Joshua Butikofer
Cluster Resources, Inc.

josh at clusterresources.com
Voice: (801) 717-3707
Fax:   (801) 717-3738
--------------------------


Garrick Staples wrote:
> On Mon, Aug 13, 2007 at 02:03:04PM -0700, Garrick Staples alleged:
>>>> I have a (possibly naive) snippet of code from another type of
>>>> daemon process where the files are properly closed:
>>>>
>>>>         /* Become a daemon */
>>>>         pid = fork();
>>>>         if (pid > 0) {          /* Parent */
>>>>                 /* Close parent's streams */
>>>>                fclose (stdin);
>>>>                fclose (stdout);
>>>>                fclose (stderr);
>>>>                 return(0);
>>>>         } else if (pid < 0) {   /* Error */
>>>>                 printf("%s: Could not fork child process\n", argv[0]);
>>>>                 return(-1);
>>>>         }
>>>>
>>>> The Maui code in src/server/OServer.c doesn't close file descriptors,
>>>> which IMHO it ought to do.  Ronny has even more detailed suggestions
>>>> above which I don't claim to understand fully.
>> *closing* is a bad idea because you don't want those descriptors to be reused by future function calls.  Imagine some random debug printf() going to some all-important network socket that happened to get fd 2.
>>
>> You really want them to be associated with /dev/null.
>>
> 
> Here's what pbs_server does:
> 
>     fclose(stdin);
>     fclose(stdout);
>     fclose(stderr);
> 
>     dummyfile = fopen("/dev/null","r");
>     assert((dummyfile != 0) && (fileno(dummyfile) == 0));
> 
>     dummyfile = fopen("/dev/null","w");
>     assert((dummyfile != 0) && (fileno(dummyfile) == 1));
> 
>     dummyfile = fopen("/dev/null","w");
>     assert((dummyfile != 0) && (fileno(dummyfile) == 2));
> 
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> mauiusers mailing list
> mauiusers at supercluster.org
> http://www.supercluster.org/mailman/listinfo/mauiusers


More information about the mauiusers mailing list