In message <[EMAIL PROTECTED]>
        so spake Alexander Farber (alexander.farber):

> In the file /usr/src/lib/libc/gen/daemon.c
> 
>        if (!noclose && (fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
>                 (void)dup2(fd, STDIN_FILENO);
>                 (void)dup2(fd, STDOUT_FILENO);
>                 (void)dup2(fd, STDERR_FILENO);
>                 if (fd > 2)
>                         (void)close (fd);
>         }
> 
> is same as:
> 
>        if (!noclose && (fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
>                 (void)dup2(fd, 0);
>                 (void)dup2(fd, 1);
>                 (void)dup2(fd, 2);
>                 if (fd > 2)
>                         (void)close (fd);
>         }
> 
> right?

Right.

> What is this last check (fd > 2) needed for? Isn't fd always > 2, 
> because the first 3 are already taken by the STDxxx streams at the
> program start?

Nope.  There is no guarantee that fds 0-2 are open when a program
starts.  In that case, fd will fall in the range 0-2 and without
the check we can close one of the descriptors 0-2.

Bonus trivia:  There's also no guarantee that argc > 0 when a program
starts.  Lots of programs make bad assumptions...

 - todd

Reply via email to