2005-01-24 Ben Maurer <bmaurer@ximian.com>
[mono-project.git] / mono / metadata / process.c
blob5f8357295f6bacdd47433fb9185988b924e62c4b
1 /*
2 * process.c: System.Diagnostics.Process support
4 * Author:
5 * Dick Porter (dick@ximian.com)
7 * (C) 2002 Ximian, Inc.
8 */
10 #include <config.h>
12 #include <glib.h>
13 #include <string.h>
15 #include <mono/metadata/object.h>
16 #include <mono/metadata/process.h>
17 #include <mono/metadata/assembly.h>
18 #include <mono/metadata/appdomain.h>
19 #include <mono/metadata/image.h>
20 #include <mono/metadata/cil-coff.h>
21 #include <mono/metadata/exception.h>
22 #include <mono/utils/strenc.h>
23 #include <mono/io-layer/io-layer.h>
24 /* FIXME: fix this code to not depend so much on the inetrnals */
25 #include <mono/metadata/class-internals.h>
27 #undef DEBUG
29 HANDLE ves_icall_System_Diagnostics_Process_GetProcess_internal (guint32 pid)
31 HANDLE handle;
33 MONO_ARCH_SAVE_REGS;
35 /* GetCurrentProcess returns a pseudo-handle, so use
36 * OpenProcess instead
38 handle=OpenProcess (PROCESS_ALL_ACCESS, TRUE, pid);
40 if(handle==NULL) {
41 /* FIXME: Throw an exception */
42 return(NULL);
45 return(handle);
48 guint32 ves_icall_System_Diagnostics_Process_GetPid_internal (void)
50 MONO_ARCH_SAVE_REGS;
52 return(GetCurrentProcessId ());
55 void ves_icall_System_Diagnostics_Process_Process_free_internal (MonoObject *this,
56 HANDLE process)
58 MONO_ARCH_SAVE_REGS;
60 #ifdef THREAD_DEBUG
61 g_message (G_GNUC_PRETTY_FUNCTION ": Closing process %p, handle %p",
62 this, process);
63 #endif
65 CloseHandle (process);
68 #define STASH_SYS_ASS(this) \
69 if(system_assembly == NULL) { \
70 system_assembly=this->vtable->klass->image; \
73 static MonoImage *system_assembly=NULL;
75 static guint32 unicode_chars (const gunichar2 *str)
77 guint32 len=0;
79 do {
80 if(str[len]=='\0') {
81 return(len);
83 len++;
84 } while(1);
87 static guint32 unicode_bytes (const gunichar2 *str)
89 guint32 len=0;
91 do {
92 if(str[len]=='\0') {
93 /* Include the terminators */
94 return((len*2)+2);
96 len++;
97 } while(1);
101 * compare a null-terminated utf16 string and a normal string.
102 * Can be used only for ascii or latin1 chars.
104 static gboolean
105 unicode_string_equals (const gunichar2 *str1, const guchar *str2)
107 while (*str1 && *str2) {
108 if (*str1 != *str2)
109 return FALSE;
110 ++str1;
111 ++str2;
113 return *str1 == *str2;
116 static void process_set_field_object (MonoObject *obj, const guchar *fieldname,
117 MonoObject *data)
119 MonoClassField *field;
121 #ifdef DEBUG
122 g_message (G_GNUC_PRETTY_FUNCTION ": Setting field %s to object at %p",
123 fieldname, data);
124 #endif
126 field=mono_class_get_field_from_name (mono_object_class (obj),
127 fieldname);
128 *(MonoObject **)(((char *)obj) + field->offset)=data;
131 static void process_set_field_string (MonoObject *obj, const guchar *fieldname,
132 const gunichar2 *val, guint32 len)
134 MonoClassField *field;
135 MonoString *string;
137 #ifdef DEBUG
138 g_message (G_GNUC_PRETTY_FUNCTION ": Setting field %s to [%s]",
139 fieldname, g_utf16_to_utf8 (val, len, NULL, NULL, NULL));
140 #endif
142 string=mono_string_new_utf16 (mono_object_domain (obj), val, len);
144 field=mono_class_get_field_from_name (mono_object_class (obj),
145 fieldname);
146 *(MonoString **)(((char *)obj) + field->offset)=string;
149 static void process_set_field_string_utf8 (MonoObject *obj,
150 const guchar *fieldname,
151 const guchar *val)
153 MonoClassField *field;
154 MonoString *string;
156 #ifdef DEBUG
157 g_message (G_GNUC_PRETTY_FUNCTION ": Setting field %s to [%s]",
158 fieldname, val);
159 #endif
161 string=mono_string_new (mono_object_domain (obj), val);
163 field=mono_class_get_field_from_name (mono_object_class (obj),
164 fieldname);
165 *(MonoString **)(((char *)obj) + field->offset)=string;
168 static void process_set_field_int (MonoObject *obj, const guchar *fieldname,
169 guint32 val)
171 MonoClassField *field;
173 #ifdef DEBUG
174 g_message (G_GNUC_PRETTY_FUNCTION ": Setting field %s to %d",
175 fieldname, val);
176 #endif
178 field=mono_class_get_field_from_name (mono_object_class (obj),
179 fieldname);
180 *(guint32 *)(((char *)obj) + field->offset)=val;
183 static void process_set_field_bool (MonoObject *obj, const guchar *fieldname,
184 gboolean val)
186 MonoClassField *field;
188 #ifdef DEBUG
189 g_message (G_GNUC_PRETTY_FUNCTION ": Setting field %s to %s",
190 fieldname, val?"TRUE":"FALSE");
191 #endif
193 field=mono_class_get_field_from_name (mono_object_class (obj),
194 fieldname);
195 *(guint8 *)(((char *)obj) + field->offset)=val;
198 typedef struct {
199 guint16 data_len;
200 guint16 value_len;
201 guint16 type;
202 gunichar2 *key;
203 } version_data;
205 /* Returns a pointer to the value data, because theres no way to know
206 * how big that data is (value_len is set to zero for most blocks :-()
208 static gpointer process_get_versioninfo_block (gpointer data,
209 version_data *block)
211 block->data_len=*((guint16 *)data);
212 data = (char *)data + sizeof(guint16);
213 block->value_len=*((guint16 *)data);
214 data = (char *)data + sizeof(guint16);
216 /* No idea what the type is supposed to indicate */
217 block->type=*((guint16 *)data);
218 data = (char *)data + sizeof(guint16);
219 block->key=((gunichar2 *)data);
221 /* skip over the key (including the terminator) */
222 data=((gunichar2 *)data)+(unicode_chars (block->key)+1);
224 /* align on a 32-bit boundary */
225 data=(gpointer)((char *)data + 3);
226 data=(gpointer)((char *)data - (GPOINTER_TO_INT(data) & 3));
228 return(data);
231 /* Returns a pointer to the byte following the Var block */
232 static gpointer process_read_var_block (MonoObject *filever, gpointer data_ptr,
233 guint16 data_len)
235 /* Not currently interested in the VarFileInfo block. This
236 * might change if language support is needed for file version
237 * strings (VarFileInfo contains lists of supported
238 * languages.)
240 version_data block;
242 /* data_ptr is pointing at a Var block of length data_len */
243 data_ptr=process_get_versioninfo_block (data_ptr, &block);
244 data_ptr=((guchar *)data_ptr)+block.value_len;
246 return(data_ptr);
249 /* Returns a pointer to the byte following the String block, or NULL
250 * if the data read hits padding. We can't recover from this because
251 * the data length does not include padding bytes, so it's not
252 * possible to just return the start position + length.
254 static gpointer process_read_string_block (MonoObject *filever,
255 gpointer data_ptr,
256 guint16 data_len,
257 gboolean store)
259 version_data block;
260 guint16 string_len=28; /* Length of the StringTable block */
262 /* data_ptr is pointing at an array of one or more String
263 * blocks with total length (not including alignment padding)
264 * of data_len.
266 while(string_len<data_len) {
267 gunichar2 *value;
269 /* align on a 32-bit boundary */
270 data_ptr=(gpointer)((char *)data_ptr + 3);
271 data_ptr=(gpointer)((char *)data_ptr -
272 (GPOINTER_TO_INT(data_ptr) & 3));
274 data_ptr=process_get_versioninfo_block (data_ptr, &block);
275 if(block.data_len==0) {
276 /* We must have hit padding, so give up
277 * processing now
279 #ifdef DEBUG
280 g_message (G_GNUC_PRETTY_FUNCTION
281 ": Hit 0-length block, giving up");
282 #endif
283 return(NULL);
286 string_len=string_len+block.data_len;
287 value=(gunichar2 *)data_ptr;
288 /* Skip over the value */
289 data_ptr=((gunichar2 *)data_ptr)+block.value_len;
291 if(store==TRUE) {
292 if (unicode_string_equals (block.key, "Comments")) {
293 process_set_field_string (filever, "comments", value, unicode_chars (value));
294 } else if (unicode_string_equals (block.key, "CompanyName")) {
295 process_set_field_string (filever, "companyname", value, unicode_chars (value));
296 } else if (unicode_string_equals (block.key, "FileDescription")) {
297 process_set_field_string (filever, "filedescription", value, unicode_chars (value));
298 } else if (unicode_string_equals (block.key, "FileVersion")) {
299 process_set_field_string (filever, "fileversion", value, unicode_chars (value));
300 } else if (unicode_string_equals (block.key, "InternalName")) {
301 process_set_field_string (filever, "internalname", value, unicode_chars (value));
302 } else if (unicode_string_equals (block.key, "LegalCopyright")) {
303 process_set_field_string (filever, "legalcopyright", value, unicode_chars (value));
304 } else if (unicode_string_equals (block.key, "LegalTrademarks")) {
305 process_set_field_string (filever, "legaltrademarks", value, unicode_chars (value));
306 } else if (unicode_string_equals (block.key, "OriginalFilename")) {
307 process_set_field_string (filever, "originalfilename", value, unicode_chars (value));
308 } else if (unicode_string_equals (block.key, "PrivateBuild")) {
309 process_set_field_string (filever, "privatebuild", value, unicode_chars (value));
310 } else if (unicode_string_equals (block.key, "ProductName")) {
311 process_set_field_string (filever, "productname", value, unicode_chars (value));
312 } else if (unicode_string_equals (block.key, "ProductVersion")) {
313 process_set_field_string (filever, "productversion", value, unicode_chars (value));
314 } else if (unicode_string_equals (block.key, "SpecialBuild")) {
315 process_set_field_string (filever, "specialbuild", value, unicode_chars (value));
316 } else {
317 /* Not an error, just not interesting
318 * in this case
324 return(data_ptr);
327 /* returns a pointer to the byte following the Stringtable block, or
328 * NULL if the data read hits padding. We can't recover from this
329 * because the data length does not include padding bytes, so it's not
330 * possible to just return the start position + length
332 static gpointer process_read_stringtable_block (MonoObject *filever,
333 gpointer data_ptr,
334 guint16 data_len)
336 version_data block;
337 gchar *language;
338 guint16 string_len=36; /* length of the StringFileInfo block */
340 /* data_ptr is pointing at an array of StringTable blocks,
341 * with total length (not including alignment padding) of
342 * data_len.
345 while(string_len<data_len) {
346 /* align on a 32-bit boundary */
347 data_ptr=(gpointer)((char *)data_ptr + 3);
348 data_ptr=(gpointer)((char *)data_ptr -
349 (GPOINTER_TO_INT(data_ptr) & 3));
351 data_ptr=process_get_versioninfo_block (data_ptr, &block);
352 if(block.data_len==0) {
353 /* We must have hit padding, so give up
354 * processing now
356 #ifdef DEBUG
357 g_message (G_GNUC_PRETTY_FUNCTION
358 ": Hit 0-length block, giving up");
359 #endif
360 return(NULL);
362 string_len=string_len+block.data_len;
364 language = g_utf16_to_utf8 (block.key, unicode_bytes (block.key), NULL, NULL, NULL);
365 g_strdown (language);
367 /* Kludge: treat en_US as neutral too */
368 if (!strcmp (language, "007f04b0") ||
369 !strcmp (language, "000004b0") ||
370 !strcmp (language, "040904b0")) {
371 /* Got the one we're interested in */
372 process_set_field_string_utf8 (filever, "language",
373 "Language Neutral");
375 data_ptr=process_read_string_block (filever, data_ptr,
376 block.data_len,
377 TRUE);
378 } else {
379 /* Some other language. We might want to do
380 * something with this in the future.
382 data_ptr=process_read_string_block (filever, data_ptr,
383 block.data_len,
384 FALSE);
386 g_free (language);
388 if(data_ptr==NULL) {
389 /* Child block hit padding */
390 #ifdef DEBUG
391 g_message (G_GNUC_PRETTY_FUNCTION ": Child block hit 0-length block, giving up");
392 #endif
393 return(NULL);
397 return(data_ptr);
400 static void process_read_fixedfileinfo_block (MonoObject *filever,
401 VS_FIXEDFILEINFO *ffi)
403 #ifdef DEBUG
404 g_message (G_GNUC_PRETTY_FUNCTION ": ffi: sig 0x%x, strucver 0x%x, fileverm 0x%x, fileverl 0x%x, prodverm 0x%x, prodverl 0x%x, ffmask 0x%x, ff 0x%x, os 0x%x, type 0x%x, subtype 0x%x, datem 0x%x, datel 0x%x", ffi->dwSignature, ffi->dwStrucVersion, ffi->dwFileVersionMS, ffi->dwFileVersionLS, ffi->dwProductVersionMS, ffi->dwProductVersionLS, ffi->dwFileFlagsMask, ffi->dwFileFlags, ffi->dwFileOS, ffi->dwFileType, ffi->dwFileSubtype, ffi->dwFileDateMS, ffi->dwFileDateLS);
405 #endif
407 process_set_field_int (filever, "filemajorpart",
408 HIWORD (ffi->dwFileVersionMS));
409 process_set_field_int (filever, "fileminorpart",
410 LOWORD (ffi->dwFileVersionMS));
411 process_set_field_int (filever, "filebuildpart",
412 HIWORD (ffi->dwFileVersionLS));
413 process_set_field_int (filever, "fileprivatepart",
414 LOWORD (ffi->dwFileVersionLS));
416 process_set_field_int (filever, "productmajorpart",
417 HIWORD (ffi->dwProductVersionMS));
418 process_set_field_int (filever, "productminorpart",
419 LOWORD (ffi->dwProductVersionMS));
420 process_set_field_int (filever, "productbuildpart",
421 HIWORD (ffi->dwProductVersionLS));
422 process_set_field_int (filever, "productprivatepart",
423 LOWORD (ffi->dwProductVersionLS));
425 process_set_field_bool (filever, "isdebug",
426 ffi->dwFileFlags&VS_FF_DEBUG);
427 process_set_field_bool (filever, "isprerelease",
428 ffi->dwFileFlags&VS_FF_PRERELEASE);
429 process_set_field_bool (filever, "ispatched",
430 ffi->dwFileFlags&VS_FF_PATCHED);
431 process_set_field_bool (filever, "isprivatebuild",
432 ffi->dwFileFlags&VS_FF_PRIVATEBUILD);
433 process_set_field_bool (filever, "isspecialbuild",
434 ffi->dwFileFlags&VS_FF_SPECIALBUILD);
437 static void process_get_fileversion (MonoObject *filever, MonoImage *image)
439 MonoPEResourceDataEntry *version_info;
440 gpointer data;
441 VS_FIXEDFILEINFO *ffi;
442 gpointer data_ptr;
443 version_data block;
444 gint32 data_len; /* signed to guard against underflow */
446 version_info=mono_image_lookup_resource (image,
447 MONO_PE_RESOURCE_ID_VERSION,
448 0, NULL);
449 #ifdef DEBUG
450 g_message (G_GNUC_PRETTY_FUNCTION ": image_lookup returned %p",
451 version_info);
452 #endif
454 if(version_info==NULL) {
455 return;
458 data=mono_image_rva_map (image,
459 version_info->rde_data_offset);
460 if(data==NULL) {
461 return;
464 /* See io-layer/versioninfo.h for the gory details on how this
465 * data is laid out. (data should be pointing to
466 * VS_VERSIONINFO data).
469 data_ptr=process_get_versioninfo_block (data, &block);
471 data_len=block.data_len;
473 if(block.value_len!=sizeof(VS_FIXEDFILEINFO)) {
474 #ifdef DEBUG
475 g_message (G_GNUC_PRETTY_FUNCTION
476 ": FIXEDFILEINFO size mismatch");
477 #endif
478 return;
481 if (!unicode_string_equals (block.key, "VS_VERSION_INFO")) {
482 #ifdef DEBUG
483 g_message (G_GNUC_PRETTY_FUNCTION
484 ": VS_VERSION_INFO mismatch");
485 #endif
486 return;
489 ffi=((VS_FIXEDFILEINFO *)data_ptr);
490 data_ptr = (char *)data_ptr + sizeof(VS_FIXEDFILEINFO);
491 if((ffi->dwSignature!=VS_FFI_SIGNATURE) ||
492 (ffi->dwStrucVersion!=VS_FFI_STRUCVERSION)) {
493 #ifdef DEBUG
494 g_message (G_GNUC_PRETTY_FUNCTION
495 ": FIXEDFILEINFO bad signature");
496 #endif
497 return;
499 process_read_fixedfileinfo_block (filever, ffi);
501 /* Subtract the 92 bytes we've already seen */
502 data_len -= 92;
504 /* There now follow zero or one StringFileInfo blocks and zero
505 * or one VarFileInfo blocks
507 while(data_len > 0) {
508 /* align on a 32-bit boundary */
509 data_ptr=(gpointer)((char *)data_ptr + 3);
510 data_ptr=(gpointer)((char *)data_ptr -
511 (GPOINTER_TO_INT(data_ptr) & 3));
513 data_ptr=process_get_versioninfo_block (data_ptr, &block);
514 if(block.data_len==0) {
515 /* We must have hit padding, so give up
516 * processing now
518 #ifdef DEBUG
519 g_message (G_GNUC_PRETTY_FUNCTION
520 ": Hit 0-length block, giving up");
521 #endif
522 return;
525 data_len=data_len-block.data_len;
527 if (unicode_string_equals (block.key, "VarFileInfo")) {
528 data_ptr=process_read_var_block (filever, data_ptr,
529 block.data_len);
530 } else if (unicode_string_equals (block.key, "StringFileInfo")) {
531 data_ptr=process_read_stringtable_block (filever, data_ptr, block.data_len);
532 } else {
533 /* Bogus data */
534 #ifdef DEBUG
535 g_message (G_GNUC_PRETTY_FUNCTION
536 ": Not a valid VERSIONINFO child block");
537 return;
538 #endif
541 if(data_ptr==NULL) {
542 /* Child block hit padding */
543 #ifdef DEBUG
544 g_message (G_GNUC_PRETTY_FUNCTION ": Child block hit 0-length block, giving up");
545 #endif
546 return;
551 static void process_add_module (GPtrArray *modules, MonoAssembly *ass)
553 MonoClass *proc_class, *filever_class;
554 MonoObject *item, *filever;
555 MonoDomain *domain=mono_domain_get ();
556 gchar *modulename;
557 const char* filename;
559 /* Build a System.Diagnostics.ProcessModule with the data.
560 * Leave BaseAddress and EntryPointAddress set to NULL,
561 * FileName is ass->image->name, FileVersionInfo is an object
562 * constructed from the PE image data referenced by
563 * ass->image, ModuleMemorySize set to 0, ModuleName the last
564 * component of FileName.
566 proc_class=mono_class_from_name (system_assembly, "System.Diagnostics",
567 "ProcessModule");
568 item=mono_object_new (domain, proc_class);
570 filever_class=mono_class_from_name (system_assembly,
571 "System.Diagnostics",
572 "FileVersionInfo");
573 filever=mono_object_new (domain, filever_class);
575 #ifdef DEBUG
576 g_message (G_GNUC_PRETTY_FUNCTION ": recording assembly: FileName [%s] FileVersionInfo [%d.%d.%d.%d], ModuleName [%s]", ass->image->name, ass->aname.major, ass->aname.minor, ass->aname.build, ass->aname.revision, ass->image->name);
577 #endif
579 process_get_fileversion (filever, mono_assembly_get_image (ass));
581 filename = mono_image_get_filename (mono_assembly_get_image (ass));
582 process_set_field_string_utf8 (filever, "filename", filename);
583 process_set_field_string_utf8 (item, "filename", filename);
584 process_set_field_object (item, "version_info", filever);
586 modulename=g_path_get_basename (filename);
587 process_set_field_string_utf8 (item, "modulename", modulename);
588 g_free (modulename);
590 g_ptr_array_add (modules, item);
593 static void process_scan_modules (gpointer data, gpointer user_data)
595 MonoAssembly *ass=data;
596 GPtrArray *modules=user_data;
598 /* The main assembly is already in the list */
599 if(mono_assembly_get_main () != ass) {
600 process_add_module (modules, ass);
605 /* Returns an array of System.Diagnostics.ProcessModule */
606 MonoArray *ves_icall_System_Diagnostics_Process_GetModules_internal (MonoObject *this)
608 /* I was going to use toolhelp for this, but then realised I
609 * was being an idiot :)
611 * (Toolhelp would give shared libraries open by the runtime,
612 * as well as open assemblies. On windows my tests didnt find
613 * the assemblies loaded by mono either.)
615 GPtrArray *modules_list=g_ptr_array_new ();
616 MonoArray *arr;
617 guint32 i;
619 MONO_ARCH_SAVE_REGS;
621 STASH_SYS_ASS (this);
623 /* Make sure the first entry is the main module */
624 process_add_module (modules_list, mono_assembly_get_main ());
626 mono_assembly_foreach (process_scan_modules, modules_list);
628 /* Build a MonoArray out of modules_list */
629 arr=mono_array_new (mono_domain_get (), mono_get_object_class (),
630 modules_list->len);
632 for(i=0; i<modules_list->len; i++) {
633 mono_array_set (arr, MonoObject *, i,
634 g_ptr_array_index (modules_list, i));
637 g_ptr_array_free (modules_list, FALSE);
639 return(arr);
642 void ves_icall_System_Diagnostics_FileVersionInfo_GetVersionInfo_internal (MonoObject *this, MonoString *filename)
644 MonoImage *image;
645 guchar *filename_utf8;
647 MONO_ARCH_SAVE_REGS;
649 STASH_SYS_ASS (this);
651 filename_utf8 = mono_string_to_utf8 (filename);
652 image = mono_pe_file_open (filename_utf8, NULL);
653 g_free (filename_utf8);
655 if(image==NULL) {
656 /* FIXME: an exception might be appropriate here */
657 #ifdef DEBUG
658 g_message (G_GNUC_PRETTY_FUNCTION ": Failed to load image");
659 #endif
661 return;
664 process_get_fileversion (this, image);
665 process_set_field_string_utf8 (this, "filename", mono_image_get_filename (image));
667 mono_image_close (image);
670 /* Only used when UseShellExecute is false */
671 static gchar *
672 quote_path (const gchar *path)
674 gchar *res = g_shell_quote (path);
675 #ifdef PLATFORM_WIN32
677 gchar *q = res;
678 while (*q) {
679 if (*q == '\'')
680 *q = '\"';
681 q++;
684 #endif
685 return res;
688 /* Only used when UseShellExecute is false */
689 static gboolean
690 complete_path (const gunichar2 *appname, gchar **completed)
692 gchar *utf8app;
693 gchar *found;
695 utf8app = g_utf16_to_utf8 (appname, -1, NULL, NULL, NULL);
696 if (g_path_is_absolute (utf8app)) {
697 *completed = quote_path (utf8app);
698 g_free (utf8app);
699 return TRUE;
702 if (g_file_test (utf8app, G_FILE_TEST_IS_EXECUTABLE) && !g_file_test (utf8app, G_FILE_TEST_IS_DIR)) {
703 *completed = quote_path (utf8app);
704 g_free (utf8app);
705 return TRUE;
708 found = g_find_program_in_path (utf8app);
709 if (found == NULL) {
710 *completed = NULL;
711 g_free (utf8app);
712 return FALSE;
715 *completed = quote_path (found);
716 g_free (found);
717 g_free (utf8app);
718 return TRUE;
721 MonoBoolean ves_icall_System_Diagnostics_Process_Start_internal (MonoString *appname, MonoString *cmd, MonoString *dirname, HANDLE stdin_handle, HANDLE stdout_handle, HANDLE stderr_handle, MonoProcInfo *process_info)
723 gboolean ret;
724 gunichar2 *dir;
725 STARTUPINFO startinfo={0};
726 PROCESS_INFORMATION procinfo;
727 gunichar2 *shell_path = NULL;
728 gchar *env_vars = NULL;
729 gboolean free_shell_path = TRUE;
730 gchar *newcmd, *tmp;
732 MONO_ARCH_SAVE_REGS;
734 startinfo.cb=sizeof(STARTUPINFO);
735 startinfo.dwFlags=STARTF_USESTDHANDLES;
736 startinfo.hStdInput=stdin_handle;
737 startinfo.hStdOutput=stdout_handle;
738 startinfo.hStdError=stderr_handle;
740 if (process_info->use_shell) {
741 const gchar *spath;
742 const gchar *shell_args;
743 #ifdef PLATFORM_WIN32
744 spath = g_getenv ("COMSPEC");
745 shell_args = "/c %s";
746 #else
747 spath = g_getenv ("SHELL");
748 shell_args = "-c %s";
749 #endif
750 if (spath != NULL) {
751 gint dummy;
752 gchar *quoted;
754 shell_path = mono_unicode_from_external (spath, &dummy);
755 tmp = mono_string_to_utf8 (cmd);
756 quoted = g_shell_quote (tmp);
757 #ifdef PLATFORM_WIN32
759 gchar *q = quoted;
760 while (*q) {
761 if (*q == '\'')
762 *q = '\"';
763 q++;
766 #endif
767 newcmd = g_strdup_printf (shell_args, quoted);
768 g_free (quoted);
769 g_free (tmp);
770 cmd = mono_string_new (mono_domain_get (), newcmd);
771 g_free (newcmd);
773 } else {
774 gchar *spath = NULL;
775 shell_path = mono_string_chars (appname);
776 complete_path (shell_path, &spath);
777 if (spath == NULL) {
778 process_info->pid = -ERROR_FILE_NOT_FOUND;
779 return FALSE;
781 #ifdef PLATFORM_WIN32
782 /* Seems like our CreateProcess does not work as the windows one.
783 * This hack is needed to deal with paths containing spaces */
784 shell_path = NULL;
785 free_shell_path = FALSE;
786 tmp = mono_string_to_utf8 (cmd);
787 newcmd = g_strdup_printf ("%s %s", spath, tmp);
788 cmd = mono_string_new_wrapper (newcmd);
789 g_free (newcmd);
790 g_free (tmp);
791 #else
792 shell_path = g_utf8_to_utf16 (spath, -1, NULL, NULL, NULL);
793 #endif
794 g_free (spath);
797 if (process_info->env_keys != NULL) {
798 gint i, len;
799 MonoString *ms;
800 MonoString *key, *value;
801 gunichar2 *str, *ptr;
802 gunichar2 *equals16;
804 for (len = 0, i = 0; i < mono_array_length (process_info->env_keys); i++) {
805 ms = mono_array_get (process_info->env_values, MonoString *, i);
806 if (ms == NULL)
807 continue;
809 len += mono_string_length (ms) * sizeof (gunichar2);
810 ms = mono_array_get (process_info->env_keys, MonoString *, i);
811 len += mono_string_length (ms) * sizeof (gunichar2);
812 len += 2 * sizeof (gunichar2);
815 equals16 = g_utf8_to_utf16 ("=", 1, NULL, NULL, NULL);
816 ptr = str = g_new0 (gunichar2, len + 1);
817 for (i = 0; i < mono_array_length (process_info->env_keys); i++) {
818 value = mono_array_get (process_info->env_values, MonoString *, i);
819 if (value == NULL)
820 continue;
822 key = mono_array_get (process_info->env_keys, MonoString *, i);
823 memcpy (ptr, mono_string_chars (key), mono_string_length (key) * sizeof (gunichar2));
824 ptr += mono_string_length (key);
826 memcpy (ptr, equals16, sizeof (gunichar2));
827 ptr++;
829 memcpy (ptr, mono_string_chars (value), mono_string_length (value) * sizeof (gunichar2));
830 ptr += mono_string_length (value);
831 ptr++;
834 g_free (equals16);
835 env_vars = (gchar *) str;
838 /* The default dir name is "". Turn that into NULL to mean
839 * "current directory"
841 if(mono_string_length (dirname)==0) {
842 dir=NULL;
843 } else {
844 dir=mono_string_chars (dirname);
847 ret=CreateProcess (shell_path, mono_string_chars (cmd), NULL, NULL, TRUE, CREATE_UNICODE_ENVIRONMENT, env_vars, dir, &startinfo, &procinfo);
849 g_free (env_vars);
850 if (free_shell_path)
851 g_free (shell_path);
853 if(ret) {
854 process_info->process_handle=procinfo.hProcess;
855 /*process_info->thread_handle=procinfo.hThread;*/
856 process_info->thread_handle=NULL;
857 CloseHandle(procinfo.hThread);
858 process_info->pid=procinfo.dwProcessId;
859 process_info->tid=procinfo.dwThreadId;
860 } else {
861 process_info->pid = -GetLastError ();
864 return(ret);
867 MonoBoolean ves_icall_System_Diagnostics_Process_WaitForExit_internal (MonoObject *this, HANDLE process, gint32 ms)
869 guint32 ret;
871 MONO_ARCH_SAVE_REGS;
873 if(ms<0) {
874 /* Wait forever */
875 ret=WaitForSingleObjectEx (process, INFINITE, TRUE);
876 } else {
877 ret=WaitForSingleObjectEx (process, ms, TRUE);
879 if(ret==WAIT_OBJECT_0) {
880 return(TRUE);
881 } else {
882 return(FALSE);
886 gint64 ves_icall_System_Diagnostics_Process_ExitTime_internal (HANDLE process)
888 gboolean ret;
889 gint64 ticks;
890 FILETIME create_time, exit_time, kernel_time, user_time;
892 MONO_ARCH_SAVE_REGS;
894 ret=GetProcessTimes (process, &create_time, &exit_time, &kernel_time,
895 &user_time);
896 if(ret==TRUE) {
897 ticks=((guint64)exit_time.dwHighDateTime << 32) +
898 exit_time.dwLowDateTime;
900 return(ticks);
901 } else {
902 return(0);
906 gint64 ves_icall_System_Diagnostics_Process_StartTime_internal (HANDLE process)
908 gboolean ret;
909 gint64 ticks;
910 FILETIME create_time, exit_time, kernel_time, user_time;
912 MONO_ARCH_SAVE_REGS;
914 ret=GetProcessTimes (process, &create_time, &exit_time, &kernel_time,
915 &user_time);
916 if(ret==TRUE) {
917 ticks=((guint64)create_time.dwHighDateTime << 32) +
918 create_time.dwLowDateTime;
920 return(ticks);
921 } else {
922 return(0);
926 gint32 ves_icall_System_Diagnostics_Process_ExitCode_internal (HANDLE process)
928 guint32 code;
930 MONO_ARCH_SAVE_REGS;
932 GetExitCodeProcess (process, &code);
934 #ifdef DEBUG
935 g_message (G_GNUC_PRETTY_FUNCTION ": process exit code is %d", code);
936 #endif
938 return(code);
941 MonoString *ves_icall_System_Diagnostics_Process_ProcessName_internal (HANDLE process)
943 MonoString *string;
944 gboolean ok;
945 HMODULE mod;
946 gunichar2 name[MAX_PATH];
947 guint32 needed;
948 guint32 len;
950 MONO_ARCH_SAVE_REGS;
952 ok=EnumProcessModules (process, &mod, sizeof(mod), &needed);
953 if(ok==FALSE) {
954 return(NULL);
957 len=GetModuleBaseName (process, mod, name, sizeof(name));
958 if(len==0) {
959 return(NULL);
962 #ifdef DEBUG
963 g_message (G_GNUC_PRETTY_FUNCTION ": process name is [%s]",
964 g_utf16_to_utf8 (name, -1, NULL, NULL, NULL));
965 #endif
967 string=mono_string_new_utf16 (mono_domain_get (), name, len);
969 return(string);
972 /* Returns an array of pids */
973 MonoArray *ves_icall_System_Diagnostics_Process_GetProcesses_internal (void)
975 MonoArray *procs;
976 gboolean ret;
977 guint32 needed, count, i;
978 guint32 pids[1024];
980 MONO_ARCH_SAVE_REGS;
982 ret=EnumProcesses (pids, sizeof(pids), &needed);
983 if(ret==FALSE) {
984 /* FIXME: throw an exception */
985 return(NULL);
988 count=needed/sizeof(guint32);
989 procs=mono_array_new (mono_domain_get (), mono_get_int32_class (),
990 count);
991 for(i=0; i<count; i++) {
992 mono_array_set (procs, guint32, i, pids[i]);
995 return(procs);
998 MonoBoolean ves_icall_System_Diagnostics_Process_GetWorkingSet_internal (HANDLE process, guint32 *min, guint32 *max)
1000 gboolean ret;
1001 size_t ws_min, ws_max;
1003 MONO_ARCH_SAVE_REGS;
1005 ret=GetProcessWorkingSetSize (process, &ws_min, &ws_max);
1006 *min=(guint32)ws_min;
1007 *max=(guint32)ws_max;
1009 return(ret);
1012 MonoBoolean ves_icall_System_Diagnostics_Process_SetWorkingSet_internal (HANDLE process, guint32 min, guint32 max, MonoBoolean use_min)
1014 gboolean ret;
1015 size_t ws_min;
1016 size_t ws_max;
1018 MONO_ARCH_SAVE_REGS;
1020 ret=GetProcessWorkingSetSize (process, &ws_min, &ws_max);
1021 if(ret==FALSE) {
1022 return(FALSE);
1025 if(use_min==TRUE) {
1026 ws_min=min;
1027 } else {
1028 ws_max=max;
1031 ret=SetProcessWorkingSetSize (process, ws_min, ws_max);
1033 return(ret);
1036 MonoBoolean
1037 ves_icall_System_Diagnostics_Process_Kill_internal (HANDLE process, gint32 sig)
1039 MONO_ARCH_SAVE_REGS;
1041 /* sig == 1 -> Kill, sig == 2 -> CloseMainWindow */
1043 return TerminateProcess (process, -sig);