[Mauiusers] Patch to fix a possible double free

Garrick Staples garrick at usc.edu
Mon Aug 13 15:05:00 MDT 2007


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));

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://www.supercluster.org/pipermail/mauiusers/attachments/20070813/b95ff024/attachment.bin


More information about the mauiusers mailing list