Re: A question to lib/libc/gen/daemon.c
Thanks. I've also read the chapter about dup's from the Stevens book and realized, that you always have to check the fd number you're going to close() after a dup2() But how can the argc be less than 1? When is it the case? After an exec()? 2005/8/22, Todd C. Miller [EMAIL PROTECTED]: 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...
A question to lib/libc/gen/daemon.c
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? 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? Thanks Alex
Re: A question to lib/libc/gen/daemon.c
Hello! On Sun, Aug 21, 2005 at 09:54:06AM +0200, Alexander Farber wrote: 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? 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? What if any of the descriptors = 2 are closed before invocation of daemon? Then fd will be = 2, and if you close it, the desired state (/dev/null open on 0, 1, and 2) will not be achieved. I.e. safety. Thanks Alex Kind regards, Hannah.
Re: A question to lib/libc/gen/daemon.c
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