Return-Path: Delivered-To: apmail-apr-commits-archive@www.apache.org Received: (qmail 91699 invoked from network); 12 Oct 2007 01:53:34 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 12 Oct 2007 01:53:34 -0000 Received: (qmail 11727 invoked by uid 500); 12 Oct 2007 01:53:22 -0000 Delivered-To: apmail-apr-commits-archive@apr.apache.org Received: (qmail 11694 invoked by uid 500); 12 Oct 2007 01:53:22 -0000 Mailing-List: contact commits-help@apr.apache.org; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: Reply-To: dev@apr.apache.org List-Id: Delivered-To: mailing list commits@apr.apache.org Received: (qmail 11683 invoked by uid 99); 12 Oct 2007 01:53:22 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 11 Oct 2007 18:53:22 -0700 X-ASF-Spam-Status: No, hits=-100.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 12 Oct 2007 01:53:33 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 2A4961A9832; Thu, 11 Oct 2007 18:53:13 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r584024 - in /apr/apr/trunk/file_io/win32: filedup.c open.c Date: Fri, 12 Oct 2007 01:53:12 -0000 To: commits@apr.apache.org From: wrowe@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20071012015313.2A4961A9832@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: wrowe Date: Thu Oct 11 18:53:12 2007 New Revision: 584024 URL: http://svn.apache.org/viewvc?rev=584024&view=rev Log: Group of two changes; we must keep file->flags in sync when we are apr_file_dup2()'ing into a standard handle (it has to remain marked as a standard handle), remove a redundant CloseFile() (win faux-posix _dup2 does this for us) and also close the msvcrt faux-posix file when we close std streams. Modified: apr/apr/trunk/file_io/win32/filedup.c apr/apr/trunk/file_io/win32/open.c Modified: apr/apr/trunk/file_io/win32/filedup.c URL: http://svn.apache.org/viewvc/apr/apr/trunk/file_io/win32/filedup.c?rev=584024&r1=584023&r2=584024&view=diff ============================================================================== --- apr/apr/trunk/file_io/win32/filedup.c (original) +++ apr/apr/trunk/file_io/win32/filedup.c Thu Oct 11 18:53:12 2007 @@ -40,7 +40,7 @@ (*new_file) = (apr_file_t *) apr_pcalloc(p, sizeof(apr_file_t)); (*new_file)->filehand = newhand; - (*new_file)->flags = old_file->flags & ~APR_INHERIT; + (*new_file)->flags = old_file->flags & ~(APR_STD_FLAGS | APR_INHERIT); (*new_file)->pool = p; (*new_file)->fname = apr_pstrdup(p, old_file->fname); (*new_file)->append = old_file->append; @@ -77,21 +77,10 @@ apr_int32_t newflags; int fd; - if (new_file->flags & APR_STD_FLAGS) { - /* if (!DuplicateHandle(hproc, old_file->filehand, - * hproc, &newhand, 0, - * TRUE, DUPLICATE_SAME_ACCESS)) { - * return apr_get_os_error(); - * } - * if (((stdhandle & stderr_handle) && !SetStdHandle(STD_ERROR_HANDLE, newhand)) || - * ((stdhandle & stdout_handle) && !SetStdHandle(STD_OUTPUT_HANDLE, newhand)) || - * ((stdhandle & stdin_handle) && !SetStdHandle(STD_INPUT_HANDLE, newhand))) { - * return apr_get_os_error(); - * } - * newflags = old_file->flags | APR_INHERIT; - */ - - if ((new_file->flags & APR_STD_FLAGS) == APR_STDERR_FLAG) { + if (new_file->flags & APR_STD_FLAGS) + { + if ((new_file->flags & APR_STD_FLAGS) == APR_STDERR_FLAG) + { /* Flush stderr and unset its buffer, then commit the fd-based buffer. * This is typically a noop for Win2K/XP since services with NULL std * handles [but valid FILE *'s, oddly enough], but is required @@ -106,6 +95,7 @@ * and close our temporary pseudo-fd once it's been duplicated. * This will incidently keep the FILE-based stderr in sync. * Note the apparently redundant _O_BINARY coersions are required. + * Note the _dup2 will close the previous std Win32 handle. */ if (!DuplicateHandle(hproc, old_file->filehand, hproc, &newhand, 0, FALSE, DUPLICATE_SAME_ACCESS)) { @@ -124,8 +114,7 @@ */ newhand = (HANDLE)_get_osfhandle(2); } - - if ((new_file->flags & APR_STD_FLAGS) == APR_STDOUT_FLAG) { + else if ((new_file->flags & APR_STD_FLAGS) == APR_STDOUT_FLAG) { /* For the process flow see the stderr case above */ fflush(stdout); setvbuf(stdout, NULL, _IONBF, 0); @@ -141,8 +130,7 @@ _setmode(1, _O_BINARY); newhand = (HANDLE)_get_osfhandle(1); } - - if ((new_file->flags & APR_STD_FLAGS) == APR_STDIN_FLAG) { + else if ((new_file->flags & APR_STD_FLAGS) == APR_STDIN_FLAG) { /* For the process flow see the stderr case above */ fflush(stdin); setvbuf(stdin, NULL, _IONBF, 0); @@ -158,7 +146,10 @@ _setmode(0, _O_BINARY); newhand = (HANDLE)_get_osfhandle(0); } - newflags = old_file->flags | APR_INHERIT; + newflags = (new_file->flags & APR_STD_FLAGS) + | (old_file->flags & ~APR_STD_FLAGS) | APR_INHERIT; + + /* No need to close the old file, _dup2() above did that for us */ } else { if (!DuplicateHandle(hproc, old_file->filehand, @@ -166,11 +157,12 @@ FALSE, DUPLICATE_SAME_ACCESS)) { return apr_get_os_error(); } - newflags = old_file->flags & ~APR_INHERIT; - } + newflags = old_file->flags & ~(APR_STD_FLAGS | APR_INHERIT); - if (new_file->filehand && (new_file->filehand != INVALID_HANDLE_VALUE)) { - CloseHandle(new_file->filehand); + if (new_file->filehand + && (new_file->filehand != INVALID_HANDLE_VALUE)) { + CloseHandle(new_file->filehand); + } } new_file->flags = newflags; Modified: apr/apr/trunk/file_io/win32/open.c URL: http://svn.apache.org/viewvc/apr/apr/trunk/file_io/win32/open.c?rev=584024&r1=584023&r2=584024&view=diff ============================================================================== --- apr/apr/trunk/file_io/win32/open.c (original) +++ apr/apr/trunk/file_io/win32/open.c Thu Oct 11 18:53:12 2007 @@ -31,6 +31,7 @@ #endif #include "apr_arch_misc.h" #include "apr_arch_inherit.h" +#include #if APR_HAS_UNICODE_FS apr_status_t utf8_to_unicode_path(apr_wchar_t* retstr, apr_size_t retlen, @@ -227,25 +228,34 @@ if (file->filehand != INVALID_HANDLE_VALUE) { + if (file->buffered) { + /* XXX: flush here is not mutex protected */ + flush_rv = apr_file_flush((apr_file_t *)thefile); + } + /* In order to avoid later segfaults with handle 'reuse', * we must protect against the case that a dup2'ed handle * is being closed, and invalidate the corresponding StdHandle + * We also tell msvcrt when stdhandles are closed. */ - if (file->filehand == GetStdHandle(STD_ERROR_HANDLE)) { - SetStdHandle(STD_ERROR_HANDLE, INVALID_HANDLE_VALUE); - } - if (file->filehand == GetStdHandle(STD_OUTPUT_HANDLE)) { - SetStdHandle(STD_OUTPUT_HANDLE, INVALID_HANDLE_VALUE); - } - if (file->filehand == GetStdHandle(STD_INPUT_HANDLE)) { - SetStdHandle(STD_INPUT_HANDLE, INVALID_HANDLE_VALUE); + if (file->flags & APR_STD_FLAGS) + { + if ((file->flags & APR_STD_FLAGS) == APR_STDERR_FLAG) { + _close(2); + SetStdHandle(STD_ERROR_HANDLE, INVALID_HANDLE_VALUE); + } + else if ((file->flags & APR_STD_FLAGS) == APR_STDOUT_FLAG) { + _close(1); + SetStdHandle(STD_OUTPUT_HANDLE, INVALID_HANDLE_VALUE); + } + else if ((file->flags & APR_STD_FLAGS) == APR_STDIN_FLAG) { + _close(0); + SetStdHandle(STD_INPUT_HANDLE, INVALID_HANDLE_VALUE); + } } + else + CloseHandle(file->filehand); - if (file->buffered) { - /* XXX: flush here is not mutex protected */ - flush_rv = apr_file_flush((apr_file_t *)thefile); - } - CloseHandle(file->filehand); file->filehand = INVALID_HANDLE_VALUE; } if (file->pOverlapped && file->pOverlapped->hEvent) {