Nicolas Williams wrote:
> On Wed, Sep 02, 2009 at 07:10:35PM +0200, Vladimir Kotal wrote:
>>   http://cr.opensolaris.org/~vkotal/daemon_libc-4471189.onnv/
> 
>  - Could you make daemon() replace fildes 0, 1 and 2 the way the ssh
>    version (from OpenBSD) did it: open /dev/null, dup2() into 0, 1 and
>    2, then close the /dev/null fildes.  Also, please check for errors
>    when opening /dev/null for this.
> 
>    Something like:
> 
>       if (noclose == 0) {
>               int fd = open("/dev/null", O_RDWR, 0);
> 
>               if (fd == -1)
>                       return (-1);  /* XXX or maybe 0 would be fine too */
> 
>               (void) dup2(fd, STDIN_FILENO);
>               (void) dup2(fd, STDOUT_FILENO);
>               (void) dup2(fd, STDERR_FILENO);
> 
>               if (fd > STDERR_FILENO)
>                       (void) close(fd);
>       }
>       return (0);

I think that's slightly inferior: it requires an extra fd for no reason
at all and thus adds an unnecessary failure mode.  I like the close(0),
open, dup2(0,1), dup2(0,2) model better.  And closing 1 and 2 first is
even better for (implausible) security reasons.

> Aside: interesting that sshd's daemon() didn't fork() again after
> setsid().

Yeah.  It was coded naively.

-- 
James Carlson         42.703N 71.076W         <carlsonj at workingcode.com>

Reply via email to