#include <config.h>
#include <glib.h>
+#include <string.h>
#include <mono/metadata/object.h>
#include <mono/metadata/process.h>
#include <mono/metadata/appdomain.h>
#include <mono/metadata/image.h>
#include <mono/metadata/cil-coff.h>
+#include <mono/metadata/exception.h>
#include <mono/io-layer/io-layer.h>
#undef DEBUG
-HANDLE ves_icall_System_Diagnostics_Process_GetCurrentProcess_internal (void)
+HANDLE ves_icall_System_Diagnostics_Process_GetProcess_internal (guint32 pid)
{
HANDLE handle;
+ MONO_ARCH_SAVE_REGS;
+
/* GetCurrentProcess returns a pseudo-handle, so use
* OpenProcess instead
*/
- handle=OpenProcess (PROCESS_ALL_ACCESS, TRUE, GetCurrentProcessId ());
+ handle=OpenProcess (PROCESS_ALL_ACCESS, TRUE, pid);
if(handle==NULL) {
/* FIXME: Throw an exception */
guint32 ves_icall_System_Diagnostics_Process_GetPid_internal (void)
{
+ MONO_ARCH_SAVE_REGS;
+
return(GetCurrentProcessId ());
}
void ves_icall_System_Diagnostics_Process_Process_free_internal (MonoObject *this,
HANDLE process)
{
+ MONO_ARCH_SAVE_REGS;
+
#ifdef THREAD_DEBUG
g_message (G_GNUC_PRETTY_FUNCTION ": Closing process %p, handle %p",
this, process);
static gpointer process_get_versioninfo_block (gpointer data,
version_data *block)
{
- block->data_len=*(((guint16 *)data)++);
- block->value_len=*(((guint16 *)data)++);
+ block->data_len=*((guint16 *)data);
+ data = (char *)data + sizeof(guint16);
+ block->value_len=*((guint16 *)data);
+ data = (char *)data + sizeof(guint16);
/* No idea what the type is supposed to indicate */
- block->type=*(((guint16 *)data)++);
+ block->type=*((guint16 *)data);
+ data = (char *)data + sizeof(guint16);
block->key=((gunichar2 *)data);
/* skip over the key (including the terminator) */
return(data_ptr);
}
-/* Returns a pointer to the byte following the String block */
+/* Returns a pointer to the byte following the String block, or NULL
+ * if the data read hits padding. We can't recover from this because
+ * the data length does not include padding bytes, so it's not
+ * possible to just return the start position + length.
+ */
static gpointer process_read_string_block (MonoObject *filever,
gpointer data_ptr,
guint16 data_len,
data_ptr=(gpointer)(((unsigned)data_ptr+3) & (~3));
data_ptr=process_get_versioninfo_block (data_ptr, &block);
+ if(block.data_len==0) {
+ /* We must have hit padding, so give up
+ * processing now
+ */
+#ifdef DEBUG
+ g_message (G_GNUC_PRETTY_FUNCTION
+ ": Hit 0-length block, giving up");
+#endif
+ return(NULL);
+ }
+
string_len=string_len+block.data_len;
value=(gunichar2 *)data_ptr;
/* Skip over the value */
return(data_ptr);
}
-/* returns a pointer to the byte following the Stringtable block */
+/* returns a pointer to the byte following the Stringtable block, or
+ * NULL if the data read hits padding. We can't recover from this
+ * because the data length does not include padding bytes, so it's not
+ * possible to just return the start position + length
+ */
static gpointer process_read_stringtable_block (MonoObject *filever,
gpointer data_ptr,
guint16 data_len)
data_ptr=(gpointer)(((unsigned)data_ptr+3) & (~3));
data_ptr=process_get_versioninfo_block (data_ptr, &block);
+ if(block.data_len==0) {
+ /* We must have hit padding, so give up
+ * processing now
+ */
+#ifdef DEBUG
+ g_message (G_GNUC_PRETTY_FUNCTION
+ ": Hit 0-length block, giving up");
+#endif
+ return(NULL);
+ }
string_len=string_len+block.data_len;
if(!memcmp (block.key, &uni_key, unicode_bytes (block.key)) ||
block.data_len,
FALSE);
}
+
+ if(data_ptr==NULL) {
+ /* Child block hit padding */
+#ifdef DEBUG
+ g_message (G_GNUC_PRETTY_FUNCTION ": Child block hit 0-length block, giving up");
+#endif
+ return(NULL);
+ }
}
return(data_ptr);
return;
}
- ffi=(((VS_FIXEDFILEINFO *)data_ptr)++);
+ ffi=((VS_FIXEDFILEINFO *)data_ptr);
+ data_ptr = (char *)data_ptr + sizeof(VS_FIXEDFILEINFO);
if((ffi->dwSignature!=VS_FFI_SIGNATURE) ||
(ffi->dwStrucVersion!=VS_FFI_STRUCVERSION)) {
#ifdef DEBUG
data_ptr=(gpointer)(((unsigned)data_ptr+3) & (~3));
data_ptr=process_get_versioninfo_block (data_ptr, &block);
+ if(block.data_len==0) {
+ /* We must have hit padding, so give up
+ * processing now
+ */
+#ifdef DEBUG
+ g_message (G_GNUC_PRETTY_FUNCTION
+ ": Hit 0-length block, giving up");
+#endif
+ return;
+ }
+
data_len=data_len-block.data_len;
if(!memcmp (block.key, &var_key, unicode_bytes (block.key))) {
return;
#endif
}
+
+ if(data_ptr==NULL) {
+ /* Child block hit padding */
+#ifdef DEBUG
+ g_message (G_GNUC_PRETTY_FUNCTION ": Child block hit 0-length block, giving up");
+#endif
+ return;
+ }
}
}
MonoArray *arr;
guint32 i;
+ MONO_ARCH_SAVE_REGS;
+
STASH_SYS_ASS (this);
/* Make sure the first entry is the main module */
MonoImage *image;
guchar *filename_utf8;
+ MONO_ARCH_SAVE_REGS;
+
STASH_SYS_ASS (this);
filename_utf8=mono_string_to_utf8 (filename);
mono_image_close (image);
}
-MonoBoolean ves_icall_System_Diagnostics_Process_Start_internal (MonoString *filename, MonoString *args, HANDLE stdin_handle, HANDLE stdout_handle, HANDLE stderr_handle, MonoProcInfo *process_info)
+MonoBoolean ves_icall_System_Diagnostics_Process_Start_internal (MonoString *cmd, MonoString *dirname, HANDLE stdin_handle, HANDLE stdout_handle, HANDLE stderr_handle, MonoProcInfo *process_info)
{
gboolean ret;
+ gunichar2 *dir;
STARTUPINFO startinfo={0};
PROCESS_INFORMATION procinfo;
+ MONO_ARCH_SAVE_REGS;
+
startinfo.cb=sizeof(STARTUPINFO);
startinfo.dwFlags=STARTF_USESTDHANDLES;
startinfo.hStdInput=stdin_handle;
startinfo.hStdOutput=stdout_handle;
startinfo.hStdError=stderr_handle;
- ret=CreateProcess (mono_string_chars (filename), mono_string_chars (args), NULL, NULL, TRUE, CREATE_UNICODE_ENVIRONMENT, NULL, NULL, &startinfo, &procinfo);
+ /* The default dir name is "". Turn that into NULL to mean
+ * "current directory"
+ */
+ if(mono_string_length (dirname)==0) {
+ dir=NULL;
+ } else {
+ dir=mono_string_chars (dirname);
+ }
+
+ ret=CreateProcess (NULL, mono_string_chars (cmd), NULL, NULL, TRUE, CREATE_UNICODE_ENVIRONMENT, NULL, dir, &startinfo, &procinfo);
- if(ret==TRUE) {
+ if(ret) {
process_info->process_handle=procinfo.hProcess;
process_info->thread_handle=procinfo.hThread;
process_info->pid=procinfo.dwProcessId;
{
guint32 ret;
+ MONO_ARCH_SAVE_REGS;
+
if(ms<0) {
/* Wait forever */
ret=WaitForSingleObject (process, INFINITE);
gint64 ticks;
FILETIME create_time, exit_time, kernel_time, user_time;
+ MONO_ARCH_SAVE_REGS;
+
ret=GetProcessTimes (process, &create_time, &exit_time, &kernel_time,
&user_time);
if(ret==TRUE) {
gint64 ticks;
FILETIME create_time, exit_time, kernel_time, user_time;
+ MONO_ARCH_SAVE_REGS;
+
ret=GetProcessTimes (process, &create_time, &exit_time, &kernel_time,
&user_time);
if(ret==TRUE) {
{
guint32 code;
+ MONO_ARCH_SAVE_REGS;
+
GetExitCodeProcess (process, &code);
#ifdef DEBUG
return(code);
}
+
+MonoString *ves_icall_System_Diagnostics_Process_ProcessName_internal (HANDLE process)
+{
+ MonoString *string;
+ gboolean ok;
+ HMODULE mod;
+ gunichar2 name[MAX_PATH];
+ guint32 needed;
+ guint32 len;
+
+ MONO_ARCH_SAVE_REGS;
+
+ ok=EnumProcessModules (process, &mod, sizeof(mod), &needed);
+ if(ok==FALSE) {
+ return(NULL);
+ }
+
+ len=GetModuleBaseName (process, mod, name, sizeof(name));
+ if(len==0) {
+ return(NULL);
+ }
+
+#ifdef DEBUG
+ g_message (G_GNUC_PRETTY_FUNCTION ": process name is [%s]",
+ g_utf16_to_utf8 (name, -1, NULL, NULL, NULL));
+#endif
+
+ string=mono_string_new_utf16 (mono_domain_get (), name, len);
+
+ return(string);
+}
+
+/* Returns an array of pids */
+MonoArray *ves_icall_System_Diagnostics_Process_GetProcesses_internal (void)
+{
+ MonoArray *procs;
+ gboolean ret;
+ guint32 needed, count, i;
+ guint32 pids[1024];
+
+ MONO_ARCH_SAVE_REGS;
+
+ ret=EnumProcesses (pids, sizeof(pids), &needed);
+ if(ret==FALSE) {
+ /* FIXME: throw an exception */
+ return(NULL);
+ }
+
+ count=needed/sizeof(guint32);
+ procs=mono_array_new (mono_domain_get (), mono_defaults.int_class,
+ count);
+ for(i=0; i<count; i++) {
+ mono_array_set (procs, guint32, i, pids[i]);
+ }
+
+ return(procs);
+}
+
+MonoBoolean ves_icall_System_Diagnostics_Process_GetWorkingSet_internal (HANDLE process, guint32 *min, guint32 *max)
+{
+ gboolean ret;
+
+ MONO_ARCH_SAVE_REGS;
+
+ ret=GetProcessWorkingSetSize (process, min, max);
+
+ return(ret);
+}
+
+MonoBoolean ves_icall_System_Diagnostics_Process_SetWorkingSet_internal (HANDLE process, guint32 min, guint32 max, MonoBoolean use_min)
+{
+ gboolean ret;
+ guint32 ws_min;
+ guint32 ws_max;
+
+ MONO_ARCH_SAVE_REGS;
+
+ ret=GetProcessWorkingSetSize (process, &ws_min, &ws_max);
+ if(ret==FALSE) {
+ return(FALSE);
+ }
+
+ if(use_min==TRUE) {
+ max=ws_max;
+ } else {
+ min=ws_min;
+ }
+
+ ret=SetProcessWorkingSetSize (process, min, max);
+
+ return(ret);
+}