You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@apr.apache.org by wr...@apache.org on 2007/10/12 21:25:31 UTC

svn commit: r584239 - in /apr/apr/branches/0.9.x/file_io/win32: filedup.c open.c

Author: wrowe
Date: Fri Oct 12 12:25:30 2007
New Revision: 584239

URL: http://svn.apache.org/viewvc?rev=584239&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.

Backports: 584024

Modified:
    apr/apr/branches/0.9.x/file_io/win32/filedup.c
    apr/apr/branches/0.9.x/file_io/win32/open.c

Modified: apr/apr/branches/0.9.x/file_io/win32/filedup.c
URL: http://svn.apache.org/viewvc/apr/apr/branches/0.9.x/file_io/win32/filedup.c?rev=584239&r1=584238&r2=584239&view=diff
==============================================================================
--- apr/apr/branches/0.9.x/file_io/win32/filedup.c (original)
+++ apr/apr/branches/0.9.x/file_io/win32/filedup.c Fri Oct 12 12:25:30 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;
@@ -73,21 +73,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
@@ -102,6 +91,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)) {
@@ -120,8 +110,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);
@@ -137,8 +126,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);
@@ -154,7 +142,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, 
@@ -162,11 +153,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/branches/0.9.x/file_io/win32/open.c
URL: http://svn.apache.org/viewvc/apr/apr/branches/0.9.x/file_io/win32/open.c?rev=584239&r1=584238&r2=584239&view=diff
==============================================================================
--- apr/apr/branches/0.9.x/file_io/win32/open.c (original)
+++ apr/apr/branches/0.9.x/file_io/win32/open.c Fri Oct 12 12:25:30 2007
@@ -31,6 +31,7 @@
 #endif
 #include "apr_arch_misc.h"
 #include "apr_arch_inherit.h"
+#include <io.h>
 
 #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) {