tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: /bin/sh redirection in 'while' loop not closed afterwards?



On Tuesday, at 08:26, David Laight wrote:
| >again:
| >                     if ((i = fcntl(fd, F_DUPFD, 10)) == -1) {
| >                             switch (errno) {
| >                             case EBADF:
| >                                     if (!try) {
| >                                             openredirect(n, memory, flags);
| >                                             try++;
| >                                             goto again;
| >                                     }
| > [...]
| >                     }
| >                     if (!try) {
| >                             sv->renamed[fd] = i;
| >                             close(fd);
| >                     }

| 
| I don't think that loop should be used - it looks like it is there
| for the case when all fd >= 10 are already in use.

Trying to track down what EBADF means for F_DUPFD, I have the feeling that 
it's something related to 'fd' itself. The manual says "not a valid open file
descriptor", and browing the code points to "fd_getfile(fd) == NULL".
Please correct me if I am wrong...

Since fd is the descriptor being redirected (#9 in my test), it is normally
closed initially, so the test is probably here to first open it
(openredirect() does this) before duping it in #10+. So I think that what is
simply missing is the "sv->renamed[fd] = i" bit in this case.


Home | Main Index | Thread Index | Old Index