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 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.

The patch here is minimal, but FreeBSD's version of these couple of lines is a
little bit cleaner IMHO. Since there is no need to loop on the fnctl(), the
logic can be simplified.

--- redir.c~    2011-09-27 22:42:32.000000000 +0200
+++ redir.c     2011-09-27 22:47:18.000000000 +0200
@@ -67,6 +67,7 @@
 
 
 #define EMPTY -2               /* marks an unused slot in redirtab */
+#define CLOSED -1              /* fd was not open before redir */
 #ifndef PIPE_BUF
 # define PIPESIZE 4096         /* amount of buffering in a pipe */
 #else
@@ -134,14 +135,14 @@
 
                if ((flags & REDIR_PUSH) && sv->renamed[fd] == EMPTY) {
                        INTOFF;
-again:
                        if ((i = fcntl(fd, F_DUPFD, 10)) == -1) {
                                switch (errno) {
                                case EBADF:
                                        if (!try) {
                                                openredirect(n, memory, flags);
+                                               sv->renamed[fd] = CLOSED;
                                                try++;
-                                               goto again;
+                                               break;
                                        }
                                        /* FALLTHROUGH*/
                                default:


Home | Main Index | Thread Index | Old Index