commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mt...@apache.org
Subject svn commit: r921814 - /commons/sandbox/runtime/trunk/src/main/native/support/win32/suexec.c
Date Thu, 11 Mar 2010 12:00:14 GMT
Author: mturk
Date: Thu Mar 11 12:00:13 2010
New Revision: 921814

URL: http://svn.apache.org/viewvc?rev=921814&view=rev
Log:
Various fixes. Note that --i=- will have to be checked

Modified:
    commons/sandbox/runtime/trunk/src/main/native/support/win32/suexec.c

Modified: commons/sandbox/runtime/trunk/src/main/native/support/win32/suexec.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/support/win32/suexec.c?rev=921814&r1=921813&r2=921814&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/support/win32/suexec.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/support/win32/suexec.c Thu Mar 11 12:00:13
2010
@@ -207,6 +207,44 @@ static LPCWSTR szPassPkey = L"Software\\
 static LPCWSTR szSaltGuid = L"e165d93c-eece-455c-bdcd-d98911e8faf6";
 
 #if defined(DEBUG) || defined(_DEBUG)
+
+static char *_win32_strerr(int statcode)
+{
+    static char buf[1024];
+    DWORD  len = 0;
+
+    statcode = statcode ? statcode : GetLastError();
+
+    len = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM |
+                         FORMAT_MESSAGE_IGNORE_INSERTS,
+                         NULL,
+                         statcode,
+                         MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default language */
+                         buf,
+                         (DWORD)sizeof(buf),
+                         NULL);
+    if (len) {
+        /* FormatMessage put the message in the buffer, but it may
+         * have embedded a newline (\r\n), and possible more than one.
+         * Remove the newlines replacing them with a space. This is not
+         * as visually perfect as moving all the remaining message over,
+         * but more efficient.
+         */
+        while (len--) {
+            if ((buf[len] == '\r') || (buf[len] == '\n'))
+                buf[len] = ' ';
+        }
+    }
+    else {
+        /* Windows didn't provide us with a message.  Even stuff like
+         * WSAECONNREFUSED won't get a message.
+         */
+        snprintf(buf, sizeof(buf), "Unrecognized Win32 error code %d", statcode);
+    }
+
+    return buf;
+}
+
 static void _dbg_printf(int line, const char *format, ...)
 {
     char    buff[ACR_HBUFF_SIZ];
@@ -215,6 +253,7 @@ static void _dbg_printf(int line, const 
     time_t  timevar;
     struct  tm *lt;
     va_list ap;
+    int     ec = GetLastError();
 
     time(&timevar);
     lt = localtime(&timevar);
@@ -225,14 +264,20 @@ static void _dbg_printf(int line, const 
              lt->tm_year + 1900, lt->tm_mon + 1, lt->tm_mday,
              lt->tm_hour, lt->tm_min, lt->tm_sec, line);
     bp += strlen(buff);
-    va_start(ap, format);
-    _vsnprintf(bp, (size_t)(ep - bp), format, ap);
-    va_end(ap);
+    if (format) {
+        va_start(ap, format);
+        _vsnprintf(bp, (size_t)(ep - bp), format, ap);
+        va_end(ap);
+    }
+    else {
+        const char *ed = _win32_strerr(ec);
+        strncpy(bp, ed, (size_t)(ep - bp));
+    }
     OutputDebugStringA(buff);
-#if 0
+#ifdef HAVE_SUEXEC_LOG
     {
         FILE *log;
-        log = fopen("wsuexec.log", "a+");
+        log = fopen("suexec.log", "a+");
         if (log) {
             fputs(buff, log);
             fputc('\n', log);
@@ -602,8 +647,14 @@ PSECURITY_ATTRIBUTES GetSaWithNullDacl(B
     PSECURITY_DESCRIPTOR pSD;
 
 
-    if (pNullSA)
+    if (pNullSA) {
+        pNullSA->bInheritHandle = bInherit;
         return pNullSA;
+    }
+    else {
+        stEmptySA.nLength = sizeof(SECURITY_ATTRIBUTES);
+        stEmptySA.lpSecurityDescriptor = NULL;
+    }
 
     if (!(pNullSA = LocalAlloc(LPTR, sizeof(SECURITY_ATTRIBUTES)))) {
         rc = GetLastError();
@@ -638,6 +689,7 @@ cleanup:
         LocalFree(pNullSA);
 
     pNullSA = &stEmptySA;
+    pNullSA->bInheritHandle = bInherit;
 
     SetLastError(rc);
     return pNullSA;
@@ -1237,10 +1289,10 @@ static LPVOID OpenSharedSegment(DWORD cb
 }
 
 typedef enum {
-    PIPE_FULL_BLOCK,
-    PIPE_FULL_NONBLOCK,
-    PIPE_READ_BLOCK,
-    PIPE_WRITE_BLOCK
+    PIPE_FULL_BLOCK     = 1,
+    PIPE_FULL_NONBLOCK  = 2,
+    PIPE_READ_BLOCK     = 3,
+    PIPE_WRITE_BLOCK    = 4
 } PipeBlockingMode;
 
 static BOOL CreatePipePair(LPHANDLE lphRd, LPHANDLE lphWr,
@@ -1253,9 +1305,9 @@ static BOOL CreatePipePair(LPHANDLE lphR
     HANDLE hWpipe = INVALID_HANDLE_VALUE;
     BOOL   bRstat = TRUE;
 
-    if (!szPipeName && !PipeNameGenerate(&szPipeName, 0))
+    if (!szPipeName && !PipeNameGenerate(&szPipeName, 0)) {
         return FALSE;
-
+    }
     if (lphRd) {
         dwOpenMode = 0;
         if (dwPipeMode == PIPE_WRITE_BLOCK ||
@@ -1263,8 +1315,8 @@ static BOOL CreatePipePair(LPHANDLE lphR
             dwOpenMode |= FILE_FLAG_OVERLAPPED;
 
         hRpipe = CreateNamedPipeW(szPipeName,
-                                  PIPE_ACCESS_INBOUND,
-                                  dwOpenMode,
+                                  PIPE_ACCESS_INBOUND | dwOpenMode,
+                                  0,
                                   1,
                                   0,
                                   65536,
@@ -1278,6 +1330,7 @@ static BOOL CreatePipePair(LPHANDLE lphR
         if (dwPipeMode == PIPE_READ_BLOCK ||
             dwPipeMode == PIPE_FULL_NONBLOCK)
             dwOpenMode |= FILE_FLAG_OVERLAPPED;
+        OutputDebugStringW(szPipeName);
         hWpipe = CreateFileW(szPipeName,
                              GENERIC_WRITE,
                              0,
@@ -1312,6 +1365,7 @@ static BOOL CreatePipePair(LPHANDLE lphR
                                      DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS);
         else
             *lphWr = hWpipe;
+
     }
     return bRstat;
 }
@@ -1954,7 +2008,7 @@ int WINAPI wWinMain(HINSTANCE hInstance,
                 if (val)
                     wcslcpy(tmp, val, RESOURCE_NAME_LEN);
                 else
-                    wcslcpy(tmp, L"SubMainW", RESOURCE_NAME_LEN);
+                    wcslcpy(tmp, L"SuexecMainW", RESOURCE_NAME_LEN);
                 /* A quick and dirty Wide -> Ascii conversion.
                  * Module names can contain only ASCII chars which
                  * are the same for Unicode strings.
@@ -1966,7 +2020,8 @@ int WINAPI wWinMain(HINSTANCE hInstance,
                 /* Where the child stdin should be redirected
                  */
                 if (val) {
-                    wcslcpy(lpForkData->szStdInpName, pfx, RESOURCE_NAME_LEN);
+                    if (*val != L'-')
+                        wcslcpy(lpForkData->szStdInpName, pfx, RESOURCE_NAME_LEN);
                     wcslcat(lpForkData->szStdInpName, val, RESOURCE_NAME_LEN);
                 }
                 else
@@ -1976,7 +2031,8 @@ int WINAPI wWinMain(HINSTANCE hInstance,
                 /* Where the child stdout should be redirected
                  */
                 if (val) {
-                    wcslcpy(lpForkData->szStdOutName, pfx, RESOURCE_NAME_LEN);
+                    if (*val != L'-')
+                        wcslcpy(lpForkData->szStdOutName, pfx, RESOURCE_NAME_LEN);
                     wcslcat(lpForkData->szStdOutName, val, RESOURCE_NAME_LEN);
                 }
                 else
@@ -1986,7 +2042,8 @@ int WINAPI wWinMain(HINSTANCE hInstance,
                 /* Where the child stdout should be redirected
                  */
                 if (val) {
-                    wcslcpy(lpForkData->szStdErrName, pfx, RESOURCE_NAME_LEN);
+                    if (*val != L'-')
+                        wcslcpy(lpForkData->szStdErrName, pfx, RESOURCE_NAME_LEN);
                     wcslcat(lpForkData->szStdErrName, val, RESOURCE_NAME_LEN);
                 }
                 else
@@ -2119,7 +2176,7 @@ int WINAPI wWinMain(HINSTANCE hInstance,
         /* This is needed cause CreateProcessAsUser Fails without
          * any error code.
          */
-        DBG_PRINTF((__LINE__, "[INFO] CreateProcessAsUserW %S", cmdline));
+        DBG_PRINTF((__LINE__, "[INFO] Fork CreateProcessAsUserW %S", cmdline));
         rs = CreateProcessAsUserW(hToken,
                                   argv[0],
                                   cmdline,
@@ -2187,27 +2244,33 @@ int WINAPI wWinMain(HINSTANCE hInstance,
         /* Create child pipes
          */
         if (lpForkData->szStdInpName[0]) {
-            /* Open the handle to the parent stdin
-             * We need to create the named pipe here so that
-             * our parent can connect to us.
-             *
-             * This is blocking pipe like all our parent pipes.
-             */
-            if (!CreatePipePair(&hPpipe[PIPE_STDINP_RPC],
-                                NULL,
-                                lpForkData->szStdInpName,
-                                PIPE_FULL_BLOCK)) {
-                rc = GWEXITERROR();
-                DBG_PRINTF((__LINE__, "[ERROR] CreatePipePair pipe=%S err=%d",
-                            lpForkData->szStdInpName, GetLastError()));
-                goto cleanup;
+            if (lpForkData->szStdInpName[0] == L'-') {
+                /* Get the handle to stdin
+                 */
+                hPpipe[PIPE_STDINP_RPC] = GetStdHandle(STD_INPUT_HANDLE);
+            }
+            else {
+                /* Open the handle to the parent stdin
+                 * We need to create the named pipe here so that
+                 * our parent can connect to us.
+                 *
+                 * This is blocking pipe like all our parent pipes.
+                 */
+                if (!CreatePipePair(&hPpipe[PIPE_STDINP_RPC],
+                                    NULL,
+                                    lpForkData->szStdInpName,
+                                    PIPE_FULL_BLOCK)) {
+                    rc = GWEXITERROR();
+                    DBG_PRINTF((__LINE__, "[ERROR] CreatePipePair pipe=%S err=%d",
+                                lpForkData->szStdInpName, GetLastError()));
+                    goto cleanup;
+                }
+                /* Our parent is already waiting on the pipe
+                 * XXX: Possible dead lock if parent closes before
+                 * we create a pipe.
+                 */
+                ConnectNamedPipe(hPpipe[PIPE_STDINP_RPC], NULL);
             }
-            /* Our parent is already waiting on the pipe
-             * XXX: Possible dead lock if parent closes before
-             * we create a pipe.
-             */
-            ConnectNamedPipe(hPpipe[PIPE_STDINP_RPC], NULL);
-
             /* Next create the child pipe if needed
              */
             if (!lpForkData->szDllEntry[0] &&
@@ -2232,18 +2295,25 @@ int WINAPI wWinMain(HINSTANCE hInstance,
             }
         }
         if (lpForkData->szStdOutName[0]) {
-            /* Open the handle to the parent stdout
-             *
-             * This is blocking pipe like all our parent pipes.
-             */
-            if (!CreatePipePair(NULL,
-                                &hPpipe[PIPE_STDOUT_RPC],
-                                lpForkData->szStdOutName,
-                                PIPE_FULL_BLOCK)) {
-                rc = GWEXITERROR();
-                DBG_PRINTF((__LINE__, "[ERROR] CreatePipePair pipe=%S err=%d",
-                            lpForkData->szStdOutName, GetLastError()));
-                goto cleanup;
+            if (lpForkData->szStdOutName[0] == L'-') {
+                /* Get the handle to stdin
+                 */
+                hPpipe[PIPE_STDOUT_RPC] = GetStdHandle(STD_OUTPUT_HANDLE);
+            }
+            else {
+                /* Open the handle to the parent stdout
+                 *
+                 * This is blocking pipe like all our parent pipes.
+                 */
+                if (!CreatePipePair(NULL,
+                                    &hPpipe[PIPE_STDOUT_RPC],
+                                    lpForkData->szStdOutName,
+                                    PIPE_FULL_BLOCK)) {
+                    rc = GWEXITERROR();
+                    DBG_PRINTF((__LINE__, "[ERROR] CreatePipePair pipe=%S err=%d",
+                                lpForkData->szStdOutName, GetLastError()));
+                    goto cleanup;
+                }
             }
             /* Next create the child pipe if needed
              */
@@ -2269,18 +2339,25 @@ int WINAPI wWinMain(HINSTANCE hInstance,
             }
         }
         if (lpForkData->szStdErrName[0]) {
-            /* Open the handle to the parent stderr
-             *
-             * This is blocking pipe like all our parent pipes.
-             */
-            if (!CreatePipePair(NULL,
-                                &hPpipe[PIPE_STDERR_RPC],
-                                lpForkData->szStdErrName,
-                                PIPE_FULL_BLOCK)) {
-                rc = GWEXITERROR();
-                DBG_PRINTF((__LINE__, "[ERROR] CreatePipePair pipe=%S err=%d",
-                            lpForkData->szStdErrName, GetLastError()));
-                goto cleanup;
+            if (lpForkData->szStdErrName[0] == L'-') {
+                /* Get the handle to stdin
+                 */
+                hPpipe[PIPE_STDERR_RPC] = GetStdHandle(STD_ERROR_HANDLE);
+            }
+            else {
+                /* Open the handle to the parent stderr
+                 *
+                 * This is blocking pipe like all our parent pipes.
+                 */
+                if (!CreatePipePair(NULL,
+                                    &hPpipe[PIPE_STDERR_RPC],
+                                    lpForkData->szStdErrName,
+                                    PIPE_FULL_BLOCK)) {
+                    rc = GWEXITERROR();
+                    DBG_PRINTF((__LINE__, "[ERROR] CreatePipePair pipe=%S err=%d",
+                                lpForkData->szStdErrName, GetLastError()));
+                    goto cleanup;
+                }
             }
             /* Next create the child pipe if needed
              */
@@ -2488,7 +2565,7 @@ int WINAPI wWinMain(HINSTANCE hInstance,
                                       cmdline,
                                       NULL,
                                       NULL,            /* Proc & thread security attributes
*/
-                                      FALSE,           /* Inherit handles */
+                                      TRUE,            /* Inherit handles */
                                       dwCreationFlags, /* Creation flags */
                                       lpEnv,           /* Safe Environment block */
                                       NULL,            /* Current directory name */
@@ -2520,7 +2597,7 @@ int WINAPI wWinMain(HINSTANCE hInstance,
              */
             if (IS_VALID_HANDLE(hPipes[PIPE_STDOUT_RDS])) {
                 sbOvlp[0].o.hEvent = CreateEvent(NULL, TRUE, TRUE, NULL);
-                hWaitHandle[nWaitHandle++] = sbOvlp[1].o.hEvent;
+                hWaitHandle[nWaitHandle++] = sbOvlp[0].o.hEvent;
             }
             if (IS_VALID_HANDLE(hPipes[PIPE_STDERR_RDS]) &&
                 hPipes[PIPE_STDERR_RDS] != hPipes[PIPE_STDOUT_RDS]) {
@@ -2733,7 +2810,7 @@ int WINAPI wWinMain(HINSTANCE hInstance,
                     break;
                     default:
                         DBG_PRINTF((__LINE__, "[ERROR] WaitForMultipleObjects ws=%d", ws));
-                        rc = ERROR_INVALID_DATA ;
+                        rc = ERROR_INVALID_DATA;
                     break;
                 }
                 if (GetTimeRunning() > lpForkData->dwTimeout) {



Mime
View raw message