#include <mono/metadata/w32process.h>
#include <mono/metadata/w32process-internals.h>
#include <mono/metadata/w32process-unix-internals.h>
+#include <mono/metadata/w32error.h>
#include <mono/metadata/class.h>
#include <mono/metadata/class-internals.h>
#include <mono/metadata/object.h>
#include <mono/metadata/metadata.h>
#include <mono/metadata/metadata-internals.h>
#include <mono/metadata/exception.h>
-#include <mono/io-layer/io-layer.h>
#include <mono/metadata/w32handle.h>
#include <mono/metadata/w32file.h>
#include <mono/utils/mono-membar.h>
#include <mono/utils/mono-mmap.h>
#include <mono/utils/strenc.h>
#include <mono/utils/mono-io-portability.h>
+#include <mono/utils/w32api.h>
#ifndef MAXPATHLEN
#define MAXPATHLEN 242
static Process *processes;
static mono_mutex_t processes_mutex;
+static pid_t current_pid;
static gpointer current_process;
static const gunichar2 utf16_space_bytes [2] = { 0x20, 0 };
mono_w32handle_register_capabilities (MONO_W32HANDLE_PROCESS,
(MonoW32HandleCapability)(MONO_W32HANDLE_CAP_WAIT | MONO_W32HANDLE_CAP_SPECIAL_WAIT));
+ current_pid = getpid ();
+
memset (&process_handle, 0, sizeof (process_handle));
- process_handle.pid = wapi_getpid ();
+ process_handle.pid = current_pid;
process_set_defaults (&process_handle);
process_set_name (&process_handle);
res = mono_w32handle_lookup (handle, MONO_W32HANDLE_PROCESS, (gpointer*) &process_handle);
if (!res) {
- SetLastError (ERROR_INVALID_HANDLE);
+ mono_w32error_set_last (ERROR_INVALID_HANDLE);
return 0;
}
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Can't find pid %d", __func__, pid);
- SetLastError (ERROR_PROC_NOT_FOUND);
+ mono_w32error_set_last (ERROR_PROC_NOT_FOUND);
return NULL;
}
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: unicode conversion returned NULL",
__func__);
- SetLastError (ERROR_PATH_NOT_FOUND);
+ mono_w32error_set_last (ERROR_PATH_NOT_FOUND);
goto free_strings;
}
if (args == NULL) {
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: unicode conversion returned NULL", __func__);
- SetLastError (ERROR_PATH_NOT_FOUND);
+ mono_w32error_set_last (ERROR_PATH_NOT_FOUND);
goto free_strings;
}
}
if (dir == NULL) {
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: unicode conversion returned NULL", __func__);
- SetLastError (ERROR_PATH_NOT_FOUND);
+ mono_w32error_set_last (ERROR_PATH_NOT_FOUND);
goto free_strings;
}
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Couldn't find executable %s",
__func__, prog);
g_free (unquoted);
- SetLastError (ERROR_FILE_NOT_FOUND);
+ mono_w32error_set_last (ERROR_FILE_NOT_FOUND);
goto free_strings;
}
} else {
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Couldn't find executable %s",
__func__, prog);
g_free (unquoted);
- SetLastError (ERROR_FILE_NOT_FOUND);
+ mono_w32error_set_last (ERROR_FILE_NOT_FOUND);
goto free_strings;
}
}
/* Give up */
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Couldn't find what to exec", __func__);
- SetLastError (ERROR_PATH_NOT_FOUND);
+ mono_w32error_set_last (ERROR_PATH_NOT_FOUND);
goto free_strings;
}
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Couldn't find executable %s",
__func__, token);
g_free (token);
- SetLastError (ERROR_FILE_NOT_FOUND);
+ mono_w32error_set_last (ERROR_FILE_NOT_FOUND);
goto free_strings;
}
} else {
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Couldn't find executable %s", __func__, token);
g_free (token);
- SetLastError (ERROR_FILE_NOT_FOUND);
+ mono_w32error_set_last (ERROR_FILE_NOT_FOUND);
goto free_strings;
}
}
} else {
if (!is_executable (prog)) {
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Executable permisson not set on %s", __func__, prog);
- SetLastError (ERROR_ACCESS_DENIED);
+ mono_w32error_set_last (ERROR_ACCESS_DENIED);
goto free_strings;
}
}
switch (pid = fork ()) {
case -1: /* Error */ {
- SetLastError (ERROR_OUTOFMEMORY);
+ mono_w32error_set_last (ERROR_OUTOFMEMORY);
ret = FALSE;
break;
}
mono_os_sem_destroy (&process->exit_sem);
g_free (process);
- SetLastError (ERROR_OUTOFMEMORY);
+ mono_w32error_set_last (ERROR_OUTOFMEMORY);
ret = FALSE;
break;
}
return ret;
#else
- SetLastError (ERROR_NOT_SUPPORTED);
+ mono_w32error_set_last (ERROR_NOT_SUPPORTED);
return FALSE;
#endif // defined (HAVE_FORK) && defined (HAVE_EXECVE)
}
*/
args = utf16_concat (utf16_quote, lpFile, utf16_quote, lpParameters == NULL ? NULL : utf16_space, lpParameters, NULL);
if (args == NULL) {
- SetLastError (ERROR_INVALID_DATA);
+ mono_w32error_set_last (ERROR_INVALID_DATA);
ret = FALSE;
goto done;
}
ret = process_create (NULL, args, lpDirectory, NULL, process_info);
g_free (args);
- if (!ret && GetLastError () == ERROR_OUTOFMEMORY)
+ if (!ret && mono_w32error_get_last () == ERROR_OUTOFMEMORY)
goto done;
if (!ret) {
args = utf16_concat (handler_utf16, utf16_space, utf16_quote, lpFile, utf16_quote,
lpParameters == NULL ? NULL : utf16_space, lpParameters, NULL);
if (args == NULL) {
- SetLastError (ERROR_INVALID_DATA);
+ mono_w32error_set_last (ERROR_INVALID_DATA);
ret = FALSE;
goto done;
}
ret = process_create (NULL, args, lpDirectory, NULL, process_info);
g_free (args);
if (!ret) {
- if (GetLastError () != ERROR_OUTOFMEMORY)
- SetLastError (ERROR_INVALID_DATA);
+ if (mono_w32error_get_last () != ERROR_OUTOFMEMORY)
+ mono_w32error_set_last (ERROR_INVALID_DATA);
ret = FALSE;
goto done;
}
/* Shell exec should not return a process handle when it spawned a GUI thing, like a browser. */
- CloseHandle (process_info->process_handle);
+ mono_w32handle_close (process_info->process_handle);
process_info->process_handle = NULL;
}
done:
if (ret == FALSE) {
- process_info->pid = -GetLastError ();
+ process_info->pid = -mono_w32error_get_last ();
} else {
process_info->thread_handle = NULL;
#if !defined(MONO_CROSS_COMPILE)
g_free (shell_path);
if (!ret)
- process_info->pid = -GetLastError ();
+ process_info->pid = -mono_w32error_get_last ();
return ret;
}
return FALSE;
}
- if (process_handle->pid == wapi_getpid ()) {
+ if (process_handle->pid == current_pid) {
*exitcode = STILL_ACTIVE;
return TRUE;
}
{
if (WAPI_IS_PSEUDO_PROCESS_HANDLE (handle))
return TRUE;
- return CloseHandle (handle);
+ return mono_w32handle_close (handle);
}
MonoBoolean
res = mono_w32handle_lookup (handle, MONO_W32HANDLE_PROCESS, (gpointer*) &process_handle);
if (!res) {
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Can't find process %p", __func__, handle);
- SetLastError (ERROR_INVALID_HANDLE);
+ mono_w32error_set_last (ERROR_INVALID_HANDLE);
return FALSE;
}
return TRUE;
switch (errno) {
- case EINVAL: SetLastError (ERROR_INVALID_PARAMETER); break;
- case EPERM: SetLastError (ERROR_ACCESS_DENIED); break;
- case ESRCH: SetLastError (ERROR_PROC_NOT_FOUND); break;
- default: SetLastError (ERROR_GEN_FAILURE); break;
+ case EINVAL: mono_w32error_set_last (ERROR_INVALID_PARAMETER); break;
+ case EPERM: mono_w32error_set_last (ERROR_ACCESS_DENIED); break;
+ case ESRCH: mono_w32error_set_last (ERROR_PROC_NOT_FOUND); break;
+ default: mono_w32error_set_last (ERROR_GEN_FAILURE); break;
}
return FALSE;
res = mono_w32handle_lookup (handle, MONO_W32HANDLE_PROCESS, (gpointer*) &process_handle);
if (!res) {
- SetLastError (ERROR_INVALID_HANDLE);
+ mono_w32error_set_last (ERROR_INVALID_HANDLE);
return 0;
}
switch (errno) {
case EPERM:
case EACCES:
- SetLastError (ERROR_ACCESS_DENIED);
+ mono_w32error_set_last (ERROR_ACCESS_DENIED);
break;
case ESRCH:
- SetLastError (ERROR_PROC_NOT_FOUND);
+ mono_w32error_set_last (ERROR_PROC_NOT_FOUND);
break;
default:
- SetLastError (ERROR_GEN_FAILURE);
+ mono_w32error_set_last (ERROR_GEN_FAILURE);
}
return 0;
}
return MONO_W32PROCESS_PRIORITY_CLASS_NORMAL;
#else
- SetLastError (ERROR_NOT_SUPPORTED);
+ mono_w32error_set_last (ERROR_NOT_SUPPORTED);
return 0;
#endif
}
res = mono_w32handle_lookup (handle, MONO_W32HANDLE_PROCESS, (gpointer*) &process_handle);
if (!res) {
- SetLastError (ERROR_INVALID_HANDLE);
+ mono_w32error_set_last (ERROR_INVALID_HANDLE);
return FALSE;
}
prio = -20;
break;
default:
- SetLastError (ERROR_INVALID_PARAMETER);
+ mono_w32error_set_last (ERROR_INVALID_PARAMETER);
return FALSE;
}
switch (errno) {
case EPERM:
case EACCES:
- SetLastError (ERROR_ACCESS_DENIED);
+ mono_w32error_set_last (ERROR_ACCESS_DENIED);
break;
case ESRCH:
- SetLastError (ERROR_PROC_NOT_FOUND);
+ mono_w32error_set_last (ERROR_PROC_NOT_FOUND);
break;
default:
- SetLastError (ERROR_GEN_FAILURE);
+ mono_w32error_set_last (ERROR_GEN_FAILURE);
}
}
return ret == 0;
#else
- SetLastError (ERROR_NOT_SUPPORTED);
+ mono_w32error_set_last (ERROR_NOT_SUPPORTED);
return FALSE;
#endif
}
if (dos_header->e_magic != IMAGE_DOS_SIGNATURE) {
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Bad dos signature 0x%x", __func__, dos_header->e_magic);
- SetLastError (ERROR_INVALID_DATA);
+ mono_w32error_set_last (ERROR_INVALID_DATA);
return(NULL);
}
if (map_size < sizeof(IMAGE_NT_HEADERS32) + GUINT32_FROM_LE (dos_header->e_lfanew)) {
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: File is too small: %d", __func__, map_size);
- SetLastError (ERROR_BAD_LENGTH);
+ mono_w32error_set_last (ERROR_BAD_LENGTH);
return(NULL);
}
if (nt_headers->Signature != IMAGE_NT_SIGNATURE) {
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Bad NT signature 0x%x", __func__, nt_headers->Signature);
- SetLastError (ERROR_INVALID_DATA);
+ mono_w32error_set_last (ERROR_INVALID_DATA);
return(NULL);
}
if (resource_rva == 0) {
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: No resources in file!", __func__);
- SetLastError (ERROR_INVALID_DATA);
+ mono_w32error_set_last (ERROR_INVALID_DATA);
return(NULL);
}
if (resource_dir == NULL) {
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Can't find resource directory", __func__);
- SetLastError (ERROR_INVALID_DATA);
+ mono_w32error_set_last (ERROR_INVALID_DATA);
return(NULL);
}
if (dos_header->e_magic != IMAGE_DOS_SIGNATURE) {
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Bad dos signature 0x%x", __func__, dos_header->e_magic);
- SetLastError (ERROR_INVALID_DATA);
+ mono_w32error_set_last (ERROR_INVALID_DATA);
return(NULL);
}
if (map_size < sizeof(IMAGE_NT_HEADERS64) + GUINT32_FROM_LE (dos_header->e_lfanew)) {
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: File is too small: %d", __func__, map_size);
- SetLastError (ERROR_BAD_LENGTH);
+ mono_w32error_set_last (ERROR_BAD_LENGTH);
return(NULL);
}
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Bad NT signature 0x%x", __func__,
nt_headers->Signature);
- SetLastError (ERROR_INVALID_DATA);
+ mono_w32error_set_last (ERROR_INVALID_DATA);
return(NULL);
}
if (resource_rva == 0) {
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: No resources in file!", __func__);
- SetLastError (ERROR_INVALID_DATA);
+ mono_w32error_set_last (ERROR_INVALID_DATA);
return(NULL);
}
if (resource_dir == NULL) {
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Can't find resource directory", __func__);
- SetLastError (ERROR_INVALID_DATA);
+ mono_w32error_set_last (ERROR_INVALID_DATA);
return(NULL);
}
if (filename_ext == NULL) {
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: unicode conversion returned NULL", __func__);
- SetLastError (ERROR_INVALID_NAME);
+ mono_w32error_set_last (ERROR_INVALID_NAME);
return(NULL);
}
g_free (filename_ext);
- SetLastError (_wapi_get_win32_file_error (errno));
+ mono_w32error_set_last (mono_w32error_unix_to_win32 (errno));
return NULL;
}
g_free (filename_ext);
g_free (located_filename);
- SetLastError (_wapi_get_win32_file_error (errno));
+ mono_w32error_set_last (mono_w32error_unix_to_win32 (errno));
return NULL;
}
if (fstat (fd, &statbuf) == -1) {
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Error stat()ing file %s: %s", __func__, filename_ext, strerror (errno));
- SetLastError (_wapi_get_win32_file_error (errno));
+ mono_w32error_set_last (mono_w32error_unix_to_win32 (errno));
g_free (filename_ext);
close (fd);
return(NULL);
if (statbuf.st_size < sizeof(IMAGE_DOS_HEADER)) {
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: File %s is too small: %lld", __func__, filename_ext, statbuf.st_size);
- SetLastError (ERROR_BAD_LENGTH);
+ mono_w32error_set_last (ERROR_BAD_LENGTH);
g_free (filename_ext);
close (fd);
return(NULL);
if (file_map == NULL) {
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Error mmap()int file %s: %s", __func__, filename_ext, strerror (errno));
- SetLastError (_wapi_get_win32_file_error (errno));
+ mono_w32error_set_last (mono_w32error_unix_to_win32 (errno));
g_free (filename_ext);
close (fd);
return(NULL);