From 874eca3c042a42f7bd5de245a76f79d6ff4f5dcf Mon Sep 17 00:00:00 2001 From: nobu Date: Wed, 21 May 2003 17:42:56 +0000 Subject: [PATCH] * eval.c (rb_thread_fd_close): raise for writing threads. [ruby-dev:20269] * io.c (rb_io_close, io_reopen): ditto. * io.c (io_reopen): keep stdio objects for stdin, stdout, and stderr. [ruby-dev:19442] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3844 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 10 ++++++++++ eval.c | 6 +++++- io.c | 25 +++++++++++++++++-------- 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/ChangeLog b/ChangeLog index de294994d7..e902af53d2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +Thu May 22 02:42:49 2003 Nobuyoshi Nakada + + * eval.c (rb_thread_fd_close): raise for writing threads. + [ruby-dev:20269] + + * io.c (rb_io_close, io_reopen): ditto. + + * io.c (io_reopen): keep stdio objects for stdin, stdout, + and stderr. [ruby-dev:19442] + Thu May 22 01:11:15 2003 Nobuyoshi Nakada * parse.y (strings, word_list): must create new instance always. diff --git a/eval.c b/eval.c index 3b8986aa8d..ffd3032907 100644 --- a/eval.c +++ b/eval.c @@ -8170,7 +8170,11 @@ rb_thread_fd_close(fd) rb_thread_t th; FOREACH_THREAD(th) { - if ((th->wait_for & WAIT_FD) && fd == th->fd) { + if (((th->wait_for & WAIT_FD) && fd == th->fd) || + ((th->wait_for & WAIT_SELECT) && (fd < th->fd) && + (FD_ISSET(fd, &th->readfds) || + FD_ISSET(fd, &th->writefds) || + FD_ISSET(fd, &th->exceptfds)))) { VALUE exc = rb_exc_new2(rb_eIOError, "stream closed"); rb_thread_raise(1, &exc, th); } diff --git a/io.c b/io.c index 76e7c1e550..2db69b939e 100644 --- a/io.c +++ b/io.c @@ -710,7 +710,7 @@ rb_io_fread(ptr, len, f) #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN case EWOULDBLOCK: #endif - if (len - n >= 0) { + if (len - n >= 0) { clearerr(f); return len - n; } @@ -1385,15 +1385,22 @@ rb_io_close(io) VALUE io; { OpenFile *fptr; - int fd; + int fd, fd2; fptr = RFILE(io)->fptr; if (!fptr) return Qnil; - if (!fptr->f && !fptr->f2) return Qnil; + if (fptr->f2) { + fd2 = fileno(fptr->f2); + } + else { + if (!fptr->f) return Qnil; + fd2 = -1; + } fd = fileno(fptr->f); rb_io_fptr_cleanup(fptr, Qfalse); rb_thread_fd_close(fd); + if (fd2 >= 0) rb_thread_fd_close(fd2); if (fptr->pid) { rb_syswait(fptr->pid); @@ -1953,7 +1960,6 @@ pipe_del_fptr(fptr) } } -#if defined (_WIN32) || defined(DJGPP) || defined(__CYGWIN__) || defined(__human68k__) || defined(__VMS) static void pipe_atexit _((void)) { @@ -1966,7 +1972,6 @@ pipe_atexit _((void)) list = tmp; } } -#endif static void pipe_finalize _((OpenFile *fptr,int)); @@ -2351,7 +2356,9 @@ io_reopen(io, nfile) else if (orig->mode & FMODE_WRITABLE) { io_fflush(orig->f, orig); } - rb_thread_fd_close(fileno(fptr->f)); + if (fptr->mode & FMODE_WRITABLE) { + io_fflush(GetWriteFile(fptr), fptr); + } /* copy OpenFile structure */ fptr->mode = orig->mode; @@ -2364,7 +2371,7 @@ io_reopen(io, nfile) mode = rb_io_mode_string(fptr); fd = fileno(fptr->f); - if (fd < 3) { + if (fd == fileno(stdin) || fd == fileno(stdout) || fd == fileno(stderr)) { clearerr(fptr->f); /* need to keep stdio objects */ if (dup2(fileno(orig->f), fd) < 0) @@ -2376,14 +2383,16 @@ io_reopen(io, nfile) rb_sys_fail(orig->path); fptr->f = rb_fdopen(fd, mode); } + rb_thread_fd_close(fd); if ((orig->mode & FMODE_READABLE) && pos >= 0) { io_seek(fptr, pos, SEEK_SET); io_seek(orig, pos, SEEK_SET); } - if (fptr->f2) { + if (fptr->f2 && fd != fileno(fptr->f2)) { fd = fileno(fptr->f2); fclose(fptr->f2); + rb_thread_fd_close(fd); if (orig->f2) { if (dup2(fileno(orig->f2), fd) < 0) rb_sys_fail(orig->path);