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 Tue, Sep 27, 2011 at 11:02:31PM +0200, Anthony Mallet wrote:
Content-Description: message body text
> On Tuesday, at 14:11, Christos Zoulas wrote:
> | Maybe the vfork() changes broke it a while ago. I would see if the FreeBSD
> | copy that does not have the vfork() changes suffers from the same issue.
> No vfork() issue. Looking at FreeBSD's version was still interesting since it
> confirmed the origin of the problem. The attached patch fix the problem for me
> - however I did not run intensive tests.

Ah - actually looked at the full function.....
Something like:

        for (n = redir ; n ; n = n-> {
                fd = n->nfile.fd;
                if ((n->nfile.type == NTOFD || n->nfile.type == NFROMFD) &&
                    n->ndup.dupfd == fd)
                        continue; /* redirect from/to same file descriptor */

                if ((flags & REDIR_PUSH) && sv->renamed[fd] == EMPTY) {
                        sv_fd = fcntl(fd, F_DUPFD, 10);
                        if (sv_fd != -1)
                                sv->renamed[fd] = sv_fd;
                        else {
                                if (errno != EBADF) {
                                        error("%d: %s", fd, strerror(errno));
                                        /* NOTREACHED */
                if (fd == 0)
                openredirect(n, memory, flags);

makes the logic clearer and probably works.

Of couse, there should be a check the reason the F_DUPFD failed is a
problem with the source fd, not something else (eg RLIMIT_NOFILES < 10),
not sure which system call has no side effects! The ioctl() isatty() uses
is probably safe.


David Laight:

Home | Main Index | Thread Index | Old Index