Index: src/utils.c =================================================================== --- src/utils.c (revision 2280) +++ src/utils.c (working copy) @@ -1849,7 +1849,7 @@ *std_err = NULL; #ifdef G_OS_WIN32 - result = win32_spawn(dir, argv, env, flags, std_out, std_err, exit_status); + result = win32_spawn(dir, argv, env, flags, std_out, std_err, exit_status, error); /** TODO create error messages in win32_spawn with appropriate error message text **/ if (! result) *error = g_error_new(G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED, _("Process could not be created.")); @@ -1892,7 +1892,7 @@ } #ifdef G_OS_WIN32 - result = win32_spawn(dir, argv, env, flags, NULL, NULL, NULL); + result = win32_spawn(dir, argv, env, flags, NULL, NULL, NULL, error); /** TODO create error messages in win32_spawn with appropriate error message text **/ if (! result) *error = g_error_new(G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED, _("Process could not be created.")); Index: src/win32.c =================================================================== --- src/win32.c (revision 2280) +++ src/win32.c (working copy) @@ -67,16 +67,17 @@ HANDLE hInputFile; HANDLE hStdout; HANDLE hStderr; + HANDLE processId; + DWORD dwExitCode; }; typedef struct _geany_win32_spawn geany_win32_spawn; +static gboolean GetContentFromHandle(HANDLE hFile, gchar **content, GError **error); +static HANDLE GetTempFileHandle(GError **error); +static gboolean CreateChildProcess(geany_win32_spawn *gw_spawn, TCHAR *szCmdline, const TCHAR *dir, GError **error); +static VOID ReadFromPipe(HANDLE hRead, HANDLE hWrite, HANDLE hFile, GError **error); -static gboolean GetContentFromHandle(HANDLE hFile, gchar **content); -static HANDLE GetTempFileHandle(void); -static gboolean CreateChildProcess(geany_win32_spawn *gw_spawn, TCHAR *szCmdline, const TCHAR *dir); -static VOID ReadFromPipe(HANDLE hRead, HANDLE hWrite, HANDLE hFile); - static gchar *get_file_filters() { gchar *string; @@ -737,7 +738,7 @@ /* Process spawning implementation for Windows, by Pierre Joye. * Don't call this function directly, use utils_spawn_[a]sync() instead. */ gboolean win32_spawn(const gchar *dir, gchar **argv, gchar **env, GSpawnFlags flags, - gchar **std_out, gchar **std_err, gint *exit_status) + gchar **std_out, gchar **std_err, gint *exit_status, GError **error) { TCHAR buffer[MAX_PATH]=TEXT(""); TCHAR cmdline[MAX_PATH] = TEXT(""); @@ -782,17 +783,18 @@ if (std_err != NULL) { - hStderrTempFile = GetTempFileHandle(); + hStderrTempFile = GetTempFileHandle(error); if (hStderrTempFile == INVALID_HANDLE_VALUE) { geany_debug("win32_spawn: Second CreateFile failed (%d)\n", (gint) GetLastError()); + *error = g_error_new(G_SPAWN_ERROR, G_FILE_ERROR, g_win32_error_message (GetLastError())); return FALSE; } } if (std_out != NULL) { - hStdoutTempFile = GetTempFileHandle(); + hStdoutTempFile = GetTempFileHandle(error); if (hStdoutTempFile == INVALID_HANDLE_VALUE) { geany_debug("win32_spawn: Second CreateFile failed (%d)\n", (gint) GetLastError()); @@ -808,11 +810,13 @@ /* Get the handle to the current STDOUT and STDERR. */ gw_spawn.hStdout = GetStdHandle(STD_OUTPUT_HANDLE); gw_spawn.hStderr = GetStdHandle(STD_ERROR_HANDLE); + gw_spawn.dwExitCode = 0; /* Create a pipe for the child process's STDOUT. */ if (! CreatePipe(&(gw_spawn.hChildStdoutRd), &(gw_spawn.hChildStdoutWr), &saAttr, 0)) { geany_debug("win32_spawn: Stdout pipe creation failed\n"); + *error = g_error_new(G_SPAWN_ERROR, G_FILE_ERROR_PIPE, g_win32_error_message (GetLastError())); return FALSE; } @@ -823,6 +827,7 @@ if (!CreatePipe(&(gw_spawn.hChildStderrRd), &(gw_spawn.hChildStderrWr), &saAttr, 0)) { geany_debug("win32_spawn: Stdout pipe creation failed\n"); + *error = g_error_new(G_SPAWN_ERROR, G_FILE_ERROR_PIPE, g_win32_error_message (GetLastError())); return FALSE; } @@ -833,15 +838,20 @@ if (!CreatePipe(&(gw_spawn.hChildStdinRd), &(gw_spawn.hChildStdinWr), &saAttr, 0)) { geany_debug("win32_spawn: Stdin pipe creation failed\n"); + *error = g_error_new(G_SPAWN_ERROR, G_FILE_ERROR_PIPE, g_win32_error_message (GetLastError())); return FALSE; } /* Ensure that the write handle to the child process's pipe for STDIN is not inherited. */ SetHandleInformation(gw_spawn.hChildStdinWr, HANDLE_FLAG_INHERIT, 0); + /* Now create the child process. */ + fSuccess = CreateChildProcess(&gw_spawn, cmdline, dir, error); + if (exit_status) + { + *exit_status = gw_spawn.dwExitCode; + } - /* Now create the child process. */ - fSuccess = CreateChildProcess(&gw_spawn, cmdline, dir); if (!fSuccess) { geany_debug("win32_spawn: Create process failed with"); @@ -851,23 +861,28 @@ /* Read from pipe that is the standard output for child process. */ if (std_out != NULL) { - ReadFromPipe(gw_spawn.hChildStdoutRd, gw_spawn.hChildStdoutWr, hStdoutTempFile); - GetContentFromHandle(hStdoutTempFile, &stdout_content); + ReadFromPipe(gw_spawn.hChildStdoutRd, gw_spawn.hChildStdoutWr, hStdoutTempFile, error); + if (!GetContentFromHandle(hStdoutTempFile, &stdout_content, error)) + { + return FALSE; + } *std_out = stdout_content; } if (std_err != NULL) { - ReadFromPipe(gw_spawn.hChildStderrRd, gw_spawn.hChildStderrWr, hStderrTempFile); - GetContentFromHandle(hStderrTempFile, &stderr_content); + ReadFromPipe(gw_spawn.hChildStderrRd, gw_spawn.hChildStderrWr, hStderrTempFile, error); + if (!GetContentFromHandle(hStderrTempFile, &stderr_content, error)) + { + return FALSE; + } *std_err = stderr_content; } - return TRUE; } -static gboolean GetContentFromHandle(HANDLE hFile, gchar **content) +static gboolean GetContentFromHandle(HANDLE hFile, gchar **content, GError **error) { DWORD filesize; gchar * buffer; @@ -884,6 +899,7 @@ if (!buffer) { geany_debug("GetContentFromHandle: Alloc failed"); + *error = g_error_new(G_SPAWN_ERROR, G_SPAWN_ERROR, g_win32_error_message (GetLastError())); return FALSE; } @@ -892,12 +908,14 @@ NULL) || dwRead == 0) { geany_debug("GetContentFromHandle: Cannot read tempfile"); + *error = g_error_new(G_SPAWN_ERROR, G_FILE_ERROR_FAILED, g_win32_error_message (GetLastError())); return FALSE; } if (!CloseHandle (hFile)) { geany_debug("GetContentFromHandle: CloseHandle failed (%d)\n", (gint) GetLastError()); + *error = g_error_new(G_SPAWN_ERROR, G_FILE_ERROR_FAILED, g_win32_error_message (GetLastError())); g_free(buffer); *content = NULL; return FALSE; @@ -907,7 +925,7 @@ } -static gboolean CreateChildProcess(geany_win32_spawn *gw_spawn, TCHAR *szCmdline, const TCHAR *dir) +static gboolean CreateChildProcess(geany_win32_spawn *gw_spawn, TCHAR *szCmdline, const TCHAR *dir, GError **error) { PROCESS_INFORMATION piProcInfo; STARTUPINFO siStartInfo; @@ -940,10 +958,24 @@ if (bFuncRetn == 0) { geany_debug("CreateChildProcess: CreateProcess failed\n"); + *error = g_error_new(G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED, g_win32_error_message (GetLastError())); return FALSE; } else { + int i; + DWORD dwStatus; + + for (i = 0; i < 2 && (dwStatus = WaitForSingleObject(piProcInfo.hProcess, 30*1000)) == WAIT_TIMEOUT; i++) + { + geany_debug("CreateChildProcess: CreateProcess failed\n"); + TerminateProcess(piProcInfo.hProcess, WAIT_TIMEOUT); /* NOTE: This will not kill grandkids. */ + } + + if (GetExitCodeProcess(piProcInfo.hProcess, &gw_spawn->dwExitCode) != 0) { + geany_debug("GetExitCodeProcess failed"); + *error = g_error_new(G_SPAWN_ERROR, G_FILE_ERROR_FAILED, g_win32_error_message (GetLastError())); + } CloseHandle(piProcInfo.hProcess); CloseHandle(piProcInfo.hThread); return bFuncRetn; @@ -952,7 +984,7 @@ } -static VOID ReadFromPipe(HANDLE hRead, HANDLE hWrite, HANDLE hFile) +static VOID ReadFromPipe(HANDLE hRead, HANDLE hWrite, HANDLE hFile, GError **error) { DWORD dwRead, dwWritten; CHAR chBuf[BUFSIZE]; @@ -962,6 +994,7 @@ if (!CloseHandle(hWrite)) { geany_debug("ReadFromPipe: Closing handle failed"); + *error = g_error_new(G_SPAWN_ERROR, G_FILE_ERROR_PIPE, g_win32_error_message (GetLastError())); return; } @@ -977,7 +1010,7 @@ } -static HANDLE GetTempFileHandle(void) +static HANDLE GetTempFileHandle(GError **error) { /* Temp file */ DWORD dwBufSize=BUFSIZE; @@ -994,6 +1027,7 @@ if (dwRetVal > dwBufSize || (dwRetVal == 0)) { geany_debug("GetTempFileHandle: GetTempPath failed (%d)\n", (gint) GetLastError()); + *error = g_error_new(G_SPAWN_ERROR, G_FILE_ERROR, g_win32_error_message (GetLastError())); return NULL; } @@ -1005,6 +1039,7 @@ if (uRetVal == 0) { geany_debug("GetTempFileName failed (%d)\n", (gint) GetLastError()); + *error = g_error_new(G_SPAWN_ERROR, G_FILE_ERROR, g_win32_error_message (GetLastError())); return NULL; } @@ -1019,6 +1054,7 @@ if (hTempFile == INVALID_HANDLE_VALUE) { geany_debug("GetTempFileHandle: Second CreateFile failed (%d)\n", (gint) GetLastError()); + *error = g_error_new(G_SPAWN_ERROR, G_FILE_ERROR, g_win32_error_message (GetLastError())); return NULL; } return hTempFile; Index: src/win32.h =================================================================== --- src/win32.h (revision 2280) +++ src/win32.h (working copy) @@ -62,6 +62,6 @@ gchar *win32_get_hostname(); gboolean win32_spawn(const gchar *dir, gchar **argv, gchar **env, GSpawnFlags flags, - gchar **std_out, gchar **std_err, gint *exit_status); + gchar **std_out, gchar **std_err, gint *exit_status, GError **error); #endif