Author: hbelusca
Date: Mon Jul 17 16:11:18 2017
New Revision: 75365

URL: http://svn.reactos.org/svn/reactos?rev=75365&view=rev
Log:
[MSVCRT][CRT]: Improvements/fixes over popen(), from Wine code and ported by 
Andreas Maier: the popen_handles array, which is used in _pclose, was never 
filled. _popen correctly fills it now.
Should fix returned codes by popen() and pclose(), which are functions that are 
called by windres, and this latter expects them to succeed. This was not the 
case before, in ROS, and therefore
trying to e.g. compile ROS within ROS failed from time to time with windres 
throwing the error that "preprocessing failed".
CORE-11568 #resolve

Added:
    trunk/reactos/sdk/lib/crt/include/internal/popen.h   (with props)
Modified:
    trunk/reactos/dll/win32/crtdll/dllmain.c
    trunk/reactos/dll/win32/msvcrt/dllmain.c
    trunk/reactos/dll/win32/msvcrt20/msvcrt20.c
    trunk/reactos/dll/win32/msvcrt40/msvcrt40.c
    trunk/reactos/sdk/lib/crt/include/internal/wine/msvcrt.h
    trunk/reactos/sdk/lib/crt/precomp.h
    trunk/reactos/sdk/lib/crt/stdio/popen.c

Modified: trunk/reactos/dll/win32/crtdll/dllmain.c
URL: 
http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/crtdll/dllmain.c?rev=75365&r1=75364&r2=75365&view=diff
==============================================================================
--- trunk/reactos/dll/win32/crtdll/dllmain.c    [iso-8859-1] (original)
+++ trunk/reactos/dll/win32/crtdll/dllmain.c    [iso-8859-1] Mon Jul 17 
16:11:18 2017
@@ -161,6 +161,7 @@
         /* Deinit of the WINE code */
         msvcrt_free_io();
         if (reserved) break;
+        msvcrt_free_popen_data();
         msvcrt_free_mt_locks();
         //msvcrt_free_console();
         //msvcrt_free_args();

Modified: trunk/reactos/dll/win32/msvcrt/dllmain.c
URL: 
http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/msvcrt/dllmain.c?rev=75365&r1=75364&r2=75365&view=diff
==============================================================================
--- trunk/reactos/dll/win32/msvcrt/dllmain.c    [iso-8859-1] (original)
+++ trunk/reactos/dll/win32/msvcrt/dllmain.c    [iso-8859-1] Mon Jul 17 
16:11:18 2017
@@ -98,6 +98,7 @@
         /* Deinit of the WINE code */
         msvcrt_free_io();
         if (reserved) break;
+        msvcrt_free_popen_data();
         msvcrt_free_mt_locks();
         //msvcrt_free_console();
         //msvcrt_free_args();

Modified: trunk/reactos/dll/win32/msvcrt20/msvcrt20.c
URL: 
http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/msvcrt20/msvcrt20.c?rev=75365&r1=75364&r2=75365&view=diff
==============================================================================
--- trunk/reactos/dll/win32/msvcrt20/msvcrt20.c [iso-8859-1] (original)
+++ trunk/reactos/dll/win32/msvcrt20/msvcrt20.c [iso-8859-1] Mon Jul 17 
16:11:18 2017
@@ -117,6 +117,7 @@
         /* Deinit of the WINE code */
         msvcrt_free_io();
         if (reserved) break;
+        msvcrt_free_popen_data();
         msvcrt_free_mt_locks();
         //msvcrt_free_console();
         //msvcrt_free_args();

Modified: trunk/reactos/dll/win32/msvcrt40/msvcrt40.c
URL: 
http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/msvcrt40/msvcrt40.c?rev=75365&r1=75364&r2=75365&view=diff
==============================================================================
--- trunk/reactos/dll/win32/msvcrt40/msvcrt40.c [iso-8859-1] (original)
+++ trunk/reactos/dll/win32/msvcrt40/msvcrt40.c [iso-8859-1] Mon Jul 17 
16:11:18 2017
@@ -113,6 +113,7 @@
         /* Deinit of the WINE code */
         msvcrt_free_io();
         if (reserved) break;
+        msvcrt_free_popen_data();
         msvcrt_free_mt_locks();
         //msvcrt_free_console();
         //msvcrt_free_args();

Added: trunk/reactos/sdk/lib/crt/include/internal/popen.h
URL: 
http://svn.reactos.org/svn/reactos/trunk/reactos/sdk/lib/crt/include/internal/popen.h?rev=75365
==============================================================================
--- trunk/reactos/sdk/lib/crt/include/internal/popen.h  (added)
+++ trunk/reactos/sdk/lib/crt/include/internal/popen.h  [iso-8859-1] Mon Jul 17 
16:11:18 2017
@@ -0,0 +1,15 @@
+#ifndef __CRT_INTERNAL_POPEN_H
+#define __CRT_INTERNAL_POPEN_H
+
+#ifndef _CRT_PRECOMP_H
+#error DO NOT INCLUDE THIS HEADER DIRECTLY
+#endif
+
+struct popen_handle {
+    FILE *f;
+    HANDLE proc;
+};
+extern struct popen_handle *popen_handles;
+extern DWORD popen_handles_size;
+
+#endif

Propchange: trunk/reactos/sdk/lib/crt/include/internal/popen.h
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: trunk/reactos/sdk/lib/crt/include/internal/wine/msvcrt.h
URL: 
http://svn.reactos.org/svn/reactos/trunk/reactos/sdk/lib/crt/include/internal/wine/msvcrt.h?rev=75365&r1=75364&r2=75365&view=diff
==============================================================================
--- trunk/reactos/sdk/lib/crt/include/internal/wine/msvcrt.h    [iso-8859-1] 
(original)
+++ trunk/reactos/sdk/lib/crt/include/internal/wine/msvcrt.h    [iso-8859-1] 
Mon Jul 17 16:11:18 2017
@@ -95,6 +95,7 @@
 extern void msvcrt_free_args(void);
 extern void msvcrt_init_signals(void);
 extern void msvcrt_free_signals(void);
+extern void msvcrt_free_popen_data(void);
 
 extern unsigned create_io_inherit_block(WORD*, BYTE**);
 

Modified: trunk/reactos/sdk/lib/crt/precomp.h
URL: 
http://svn.reactos.org/svn/reactos/trunk/reactos/sdk/lib/crt/precomp.h?rev=75365&r1=75364&r2=75365&view=diff
==============================================================================
--- trunk/reactos/sdk/lib/crt/precomp.h [iso-8859-1] (original)
+++ trunk/reactos/sdk/lib/crt/precomp.h [iso-8859-1] Mon Jul 17 16:11:18 2017
@@ -73,6 +73,7 @@
 #include <internal/mbstring.h>
 #include <internal/misc.h>
 #include <internal/mtdll.h>
+#include <internal/popen.h>
 #include <internal/rterror.h>
 #include <internal/safecrt.h>
 #include <internal/time.h>

Modified: trunk/reactos/sdk/lib/crt/stdio/popen.c
URL: 
http://svn.reactos.org/svn/reactos/trunk/reactos/sdk/lib/crt/stdio/popen.c?rev=75365&r1=75364&r2=75365&view=diff
==============================================================================
--- trunk/reactos/sdk/lib/crt/stdio/popen.c     [iso-8859-1] (original)
+++ trunk/reactos/sdk/lib/crt/stdio/popen.c     [iso-8859-1] Mon Jul 17 
16:11:18 2017
@@ -1,11 +1,12 @@
 /*
-* COPYRIGHT:   See COPYING in the top level directory
-* PROJECT:     ReactOS C runtime library
-* FILE:        lib/sdk/crt/stdio/popen.c
-* PURPOSE:     Pipe Functions
-* PROGRAMERS:  Eric Kohl
-               Hartmut Birr
-*/
+ * COPYRIGHT:       See COPYING in the top level directory
+ * PROJECT:         ReactOS C runtime library
+ * FILE:            lib/sdk/crt/stdio/popen.c
+ * PURPOSE:         Pipe Functions
+ * PROGRAMMERS:     Eric Kohl
+ *                  Hartmut Birr
+ *                  Also adapted from Wine team code by Andreas Maier.
+ */
 
 #include <precomp.h>
 #include <tchar.h>
@@ -22,11 +23,13 @@
 unsigned split_oflags(unsigned oflags); //FIXME: Remove
 
 #ifndef _UNICODE
-static struct popen_handle {
-    FILE *f;
-    HANDLE proc;
-} *popen_handles;
-static DWORD popen_handles_size;
+struct popen_handle *popen_handles = NULL;
+DWORD popen_handles_size = 0;
+
+void msvcrt_free_popen_data(void)
+{
+    free(popen_handles);
+}
 #endif
 
 /*
@@ -37,12 +40,14 @@
     _TCHAR *szCmdLine=NULL;
     _TCHAR *szComSpec=NULL;
     _TCHAR *s;
-    FILE *pf;
+    FILE *ret;
     HANDLE hReadPipe, hWritePipe;
     BOOL result;
     STARTUPINFO StartupInfo;
     PROCESS_INFORMATION ProcessInformation;
     SECURITY_ATTRIBUTES sa = {sizeof(SECURITY_ATTRIBUTES), NULL, TRUE};
+    struct popen_handle *container;
+    DWORD i;
 
     TRACE(MK_STR(_tpopen)"('%"sT"', '%"sT"')\n", cm, md);
 
@@ -80,6 +85,7 @@
         return NULL;
     }
 
+    memset(&ProcessInformation, 0, sizeof(ProcessInformation));
     memset(&StartupInfo, 0, sizeof(STARTUPINFO));
     StartupInfo.cb = sizeof(STARTUPINFO);
 
@@ -117,20 +123,47 @@
     }
 
     CloseHandle(ProcessInformation.hThread);
-    CloseHandle(ProcessInformation.hProcess);
+
+    _mlock(_POPEN_LOCK);
+    for(i=0; i<popen_handles_size; i++)
+    {
+        if (!popen_handles[i].f)
+            break;
+    }
+    if (i==popen_handles_size)
+    {
+        i = (popen_handles_size ? popen_handles_size*2 : 8);
+        container = realloc(popen_handles, i*sizeof(*container));
+        if (!container) goto error;
+
+        popen_handles = container;
+        container = popen_handles+popen_handles_size;
+        memset(container, 0, (i-popen_handles_size)*sizeof(*container));
+        popen_handles_size = i;
+    }
+    else container = popen_handles+i;
 
     if ( *md == 'r' )
     {
-        pf = _tfdopen(msvcrt_alloc_fd(hReadPipe,  split_oflags(_fmode)) , 
_T("r"));
+        ret = _tfdopen(msvcrt_alloc_fd(hReadPipe,  split_oflags(_fmode)) , 
_T("r"));
         CloseHandle(hWritePipe);
     }
     else
     {
-        pf = _tfdopen( msvcrt_alloc_fd(hWritePipe, split_oflags(_fmode)) , 
_T("w"));
+        ret = _tfdopen( msvcrt_alloc_fd(hWritePipe, split_oflags(_fmode)) , 
_T("w"));
         CloseHandle(hReadPipe);
     }
 
-    return( pf );
+    container->f = ret;
+    container->proc = ProcessInformation.hProcess;
+    _munlock(_POPEN_LOCK);
+
+    return ret;
+
+error:
+    if (ProcessInformation.hProcess != 0)
+        CloseHandle(ProcessInformation.hProcess);
+    return NULL;
 }
 
 #ifndef _UNICODE


Reply via email to