Re: Symlinks and Cryogenic Sleep

From: Mikael Olsson (mikael.olssonat_private)
Date: Wed Jan 05 2000 - 00:34:01 PST

  • Next message: Olaf Kirch: "Re: Symlinks and Cryogenic Sleep"

    I think I see a flaw with this...
    
    Goetz Babin-Ebell wrote:
    >
    > I did something that way:
    >
    > FILE *DoOpen(const char *cpFile, long bAppend)
    > {
    >    FILE *spNew;
    >    FILE *spTest;
    >    struct stat sStat;
    >
    >    spTest = fopen(cpFile,"a");
    >    if (!spTest)
    >    {
    >       Log("ERR FILE OPEN",cpFile);
    >       return NULL;
    >    }
    >    if (lstat(cpFile,&sStat))
    >    {
    >       Log("ERR STAT",cpFile);
    >       return NULL;
    >    }
    >    if ((sStat.st_mode & S_IFMT) == S_IFLNK)
    >    {
    >       fclose(spTest);
    >       Log("ERR ISLINK",cpFile);
    >       return NULL;
    >    }
    >    if (bAppend)
    >       spNew = spTest;
    >    else
    >    {
    >       spNew = freopen(cpFile,"w",spTest);
    >       fclose(spTest);
    >    }
    >    if (!spNew)
    >    {
    >       Log("ERR FILE OPEN",cpFile);
    >       return NULL;
    >    }
    >    return spFile;
    > }
    >
    
    
    In my tired state, I get the feeling that you open
    yourself up to an inverted race situation here.
    
    In this situation, the file that you open may
    be a link, but before it is stat()ed, it may
    be deleted (yes you can do this even though
    it is open) and replaced with a normal file,
    so that stat() won't complain.
    
    I'd suggest that you don't stat the file by
    name, but rather by the file descriptor that
    you already have (_fstat()?); this way you know
    that you are stat()ing the same file that you
    actually opened (I hope!).
    
    Oh, and the freopen() call opens you up to
    another race situation (I think).
    AFAIK, freopen() is just a shorthand for
    fclose() followed by fopen(), so that leaves
    room for a race situation.
    (I might be talking out of my arse here tho)
    If it were me, I'd move the file pointer to 0
    and set the file length to 0; this way the file
    remains open all the time, and you still get the
    desired effect.
    
    <flame shield>
    I'm _REALLY_ tired
    Just so you know :-)
    </flame shield>
    
    /Mike
    
    --
    Mikael Olsson, EnterNet Sweden AB, Box 393, S-891 28 ÖRNSKÖLDSVIK
    Phone: +46-(0)660-105 50           Fax: +46-(0)660-122 50
    Mobile: +46-(0)70-248 00 33
    WWW: http://www.enternet.se        E-mail: mikael.olssonat_private
    



    This archive was generated by hypermail 2b30 : Fri Apr 13 2001 - 15:26:22 PDT