2007-04-06 Andreas Faerber <andreas.faerber@web.de>
[mono.git] / mono / metadata / process.c
blobd0101e3bcf669c2898a4fac481bd887f50c1b514
1 /*
2 * process.c: System.Diagnostics.Process support
4 * Author:
5 * Dick Porter (dick@ximian.com)
7 * (C) 2002 Ximian, Inc.
8 * Copyright (c) 2002-2006 Novell, Inc.
9 */
11 #include <config.h>
13 #include <glib.h>
14 #include <string.h>
16 #include <mono/metadata/object.h>
17 #include <mono/metadata/process.h>
18 #include <mono/metadata/assembly.h>
19 #include <mono/metadata/appdomain.h>
20 #include <mono/metadata/image.h>
21 #include <mono/metadata/cil-coff.h>
22 #include <mono/metadata/exception.h>
23 #include <mono/utils/strenc.h>
24 #include <mono/io-layer/io-layer.h>
25 /* FIXME: fix this code to not depend so much on the inetrnals */
26 #include <mono/metadata/class-internals.h>
28 #undef DEBUG
30 HANDLE ves_icall_System_Diagnostics_Process_GetProcess_internal (guint32 pid)
32 HANDLE handle;
34 MONO_ARCH_SAVE_REGS;
36 /* GetCurrentProcess returns a pseudo-handle, so use
37 * OpenProcess instead
39 handle=OpenProcess (PROCESS_ALL_ACCESS, TRUE, pid);
41 if(handle==NULL) {
42 /* FIXME: Throw an exception */
43 return(NULL);
46 return(handle);
49 guint32 ves_icall_System_Diagnostics_Process_GetPid_internal (void)
51 MONO_ARCH_SAVE_REGS;
53 return(GetCurrentProcessId ());
56 void ves_icall_System_Diagnostics_Process_Process_free_internal (MonoObject *this,
57 HANDLE process)
59 MONO_ARCH_SAVE_REGS;
61 #ifdef THREAD_DEBUG
62 g_message (G_GNUC_PRETTY_FUNCTION ": Closing process %p, handle %p",
63 this, process);
64 #endif
66 CloseHandle (process);
69 #define STASH_SYS_ASS(this) \
70 if(system_assembly == NULL) { \
71 system_assembly=this->vtable->klass->image; \
74 static MonoImage *system_assembly=NULL;
76 static guint32 unicode_chars (const gunichar2 *str)
78 guint32 len=0;
80 do {
81 if(str[len]=='\0') {
82 return(len);
84 len++;
85 } while(1);
88 static guint32 unicode_bytes (const gunichar2 *str)
90 guint32 len=0;
92 do {
93 if(str[len]=='\0') {
94 /* Include the terminators */
95 return((len*2)+2);
97 len++;
98 } while(1);
101 static gunichar2*
102 unicode_get (const gunichar2 *str)
104 gunichar2 *swapped;
105 int i, len;
107 len = unicode_bytes (str);
108 swapped = g_malloc0 (len);
109 i = 0;
110 while (str [i]) {
111 swapped [i] = GUINT16_FROM_LE (str [i]);
112 i ++;
115 return swapped;
119 * compare a little-endian null-terminated utf16 string and a normal string.
120 * Can be used only for ascii or latin1 chars.
122 static gboolean
123 unicode_string_equals (const gunichar2 *str1, const gchar *str2)
125 while (*str1 && *str2) {
126 if (GUINT16_TO_LE (*str1) != *str2)
127 return FALSE;
128 ++str1;
129 ++str2;
131 return *str1 == *str2;
134 static void process_set_field_object (MonoObject *obj, const gchar *fieldname,
135 MonoObject *data)
137 MonoClassField *field;
139 #ifdef DEBUG
140 g_message (G_GNUC_PRETTY_FUNCTION ": Setting field %s to object at %p",
141 fieldname, data);
142 #endif
144 field=mono_class_get_field_from_name (mono_object_class (obj),
145 fieldname);
146 /* FIXME: moving GC */
147 *(MonoObject **)(((char *)obj) + field->offset)=data;
150 static void process_set_field_string (MonoObject *obj, const gchar *fieldname,
151 const gunichar2 *val, guint32 len)
153 MonoClassField *field;
154 MonoString *string;
156 #ifdef DEBUG
157 g_message (G_GNUC_PRETTY_FUNCTION ": Setting field %s to [%s]",
158 fieldname, g_utf16_to_utf8 (val, len, NULL, NULL, NULL));
159 #endif
161 string=mono_string_new_utf16 (mono_object_domain (obj), val, len);
163 field=mono_class_get_field_from_name (mono_object_class (obj),
164 fieldname);
165 /* FIXME: moving GC */
166 *(MonoString **)(((char *)obj) + field->offset)=string;
169 static void process_set_field_string_utf8 (MonoObject *obj,
170 const gchar *fieldname,
171 const gchar *val)
173 MonoClassField *field;
174 MonoString *string;
176 #ifdef DEBUG
177 g_message (G_GNUC_PRETTY_FUNCTION ": Setting field %s to [%s]",
178 fieldname, val);
179 #endif
181 string=mono_string_new (mono_object_domain (obj), val);
183 field=mono_class_get_field_from_name (mono_object_class (obj),
184 fieldname);
185 /* FIXME: moving GC */
186 *(MonoString **)(((char *)obj) + field->offset)=string;
189 static void process_set_field_int (MonoObject *obj, const gchar *fieldname,
190 guint32 val)
192 MonoClassField *field;
194 #ifdef DEBUG
195 g_message (G_GNUC_PRETTY_FUNCTION ": Setting field %s to %d",
196 fieldname, val);
197 #endif
199 field=mono_class_get_field_from_name (mono_object_class (obj),
200 fieldname);
201 *(guint32 *)(((char *)obj) + field->offset)=val;
204 static void process_set_field_bool (MonoObject *obj, const gchar *fieldname,
205 gboolean val)
207 MonoClassField *field;
209 #ifdef DEBUG
210 g_message (G_GNUC_PRETTY_FUNCTION ": Setting field %s to %s",
211 fieldname, val?"TRUE":"FALSE");
212 #endif
214 field=mono_class_get_field_from_name (mono_object_class (obj),
215 fieldname);
216 *(guint8 *)(((char *)obj) + field->offset)=val;
219 typedef struct {
220 guint16 data_len;
221 guint16 value_len;
222 guint16 type;
223 gunichar2 *key;
224 } version_data;
226 /* Returns a pointer to the value data, because theres no way to know
227 * how big that data is (value_len is set to zero for most blocks :-()
229 static gpointer process_get_versioninfo_block (gpointer data,
230 version_data *block)
232 block->data_len=GUINT16_TO_LE (*((guint16 *)data));
233 data = (char *)data + sizeof(guint16);
234 block->value_len=GUINT16_TO_LE (*((guint16 *)data));
235 data = (char *)data + sizeof(guint16);
237 /* No idea what the type is supposed to indicate */
238 block->type=GUINT16_TO_LE (*((guint16 *)data));
239 data = (char *)data + sizeof(guint16);
240 block->key=((gunichar2 *)data);
242 /* skip over the key (including the terminator) */
243 data=((gunichar2 *)data)+(unicode_chars (block->key)+1);
245 /* align on a 32-bit boundary */
246 data=(gpointer)((char *)data + 3);
247 data=(gpointer)((char *)data - (GPOINTER_TO_INT(data) & 3));
249 return(data);
252 /* Returns a pointer to the byte following the Var block */
253 static gpointer process_read_var_block (MonoObject *filever, gpointer data_ptr,
254 guint16 data_len)
256 /* Not currently interested in the VarFileInfo block. This
257 * might change if language support is needed for file version
258 * strings (VarFileInfo contains lists of supported
259 * languages.)
261 version_data block;
263 /* data_ptr is pointing at a Var block of length data_len */
264 data_ptr=process_get_versioninfo_block (data_ptr, &block);
265 data_ptr=((guchar *)data_ptr)+block.value_len;
267 return(data_ptr);
270 /* Returns a pointer to the byte following the String block, or NULL
271 * if the data read hits padding. We can't recover from this because
272 * the data length does not include padding bytes, so it's not
273 * possible to just return the start position + length.
275 static gpointer process_read_string_block (MonoObject *filever,
276 gpointer data_ptr,
277 guint16 data_len,
278 gboolean store)
280 version_data block;
281 guint16 string_len=28; /* Length of the StringTable block */
283 /* data_ptr is pointing at an array of one or more String
284 * blocks with total length (not including alignment padding)
285 * of data_len.
287 while(string_len<data_len) {
288 gunichar2 *value;
290 /* align on a 32-bit boundary */
291 data_ptr=(gpointer)((char *)data_ptr + 3);
292 data_ptr=(gpointer)((char *)data_ptr -
293 (GPOINTER_TO_INT(data_ptr) & 3));
295 data_ptr=process_get_versioninfo_block (data_ptr, &block);
296 if(block.data_len==0) {
297 /* We must have hit padding, so give up
298 * processing now
300 #ifdef DEBUG
301 g_message (G_GNUC_PRETTY_FUNCTION
302 ": Hit 0-length block, giving up");
303 #endif
304 return(NULL);
307 string_len=string_len+block.data_len;
308 value=unicode_get ((gunichar2 *)data_ptr);
309 /* Skip over the value */
310 data_ptr=((gunichar2 *)data_ptr)+block.value_len;
312 if(store==TRUE) {
313 if (unicode_string_equals (block.key, "Comments")) {
314 process_set_field_string (filever, "comments", value, unicode_chars (value));
315 } else if (unicode_string_equals (block.key, "CompanyName")) {
316 process_set_field_string (filever, "companyname", value, unicode_chars (value));
317 } else if (unicode_string_equals (block.key, "FileDescription")) {
318 process_set_field_string (filever, "filedescription", value, unicode_chars (value));
319 } else if (unicode_string_equals (block.key, "FileVersion")) {
320 process_set_field_string (filever, "fileversion", value, unicode_chars (value));
321 } else if (unicode_string_equals (block.key, "InternalName")) {
322 process_set_field_string (filever, "internalname", value, unicode_chars (value));
323 } else if (unicode_string_equals (block.key, "LegalCopyright")) {
324 process_set_field_string (filever, "legalcopyright", value, unicode_chars (value));
325 } else if (unicode_string_equals (block.key, "LegalTrademarks")) {
326 process_set_field_string (filever, "legaltrademarks", value, unicode_chars (value));
327 } else if (unicode_string_equals (block.key, "OriginalFilename")) {
328 process_set_field_string (filever, "originalfilename", value, unicode_chars (value));
329 } else if (unicode_string_equals (block.key, "PrivateBuild")) {
330 process_set_field_string (filever, "privatebuild", value, unicode_chars (value));
331 } else if (unicode_string_equals (block.key, "ProductName")) {
332 process_set_field_string (filever, "productname", value, unicode_chars (value));
333 } else if (unicode_string_equals (block.key, "ProductVersion")) {
334 process_set_field_string (filever, "productversion", value, unicode_chars (value));
335 } else if (unicode_string_equals (block.key, "SpecialBuild")) {
336 process_set_field_string (filever, "specialbuild", value, unicode_chars (value));
337 } else {
338 /* Not an error, just not interesting
339 * in this case
343 g_free (value);
346 return(data_ptr);
349 /* returns a pointer to the byte following the Stringtable block, or
350 * NULL if the data read hits padding. We can't recover from this
351 * because the data length does not include padding bytes, so it's not
352 * possible to just return the start position + length
354 static gpointer process_read_stringtable_block (MonoObject *filever,
355 gpointer data_ptr,
356 guint16 data_len)
358 version_data block;
359 gunichar2 *value;
360 gchar *language;
361 guint16 string_len=36; /* length of the StringFileInfo block */
363 /* data_ptr is pointing at an array of StringTable blocks,
364 * with total length (not including alignment padding) of
365 * data_len.
368 while(string_len<data_len) {
369 /* align on a 32-bit boundary */
370 data_ptr=(gpointer)((char *)data_ptr + 3);
371 data_ptr=(gpointer)((char *)data_ptr -
372 (GPOINTER_TO_INT(data_ptr) & 3));
374 data_ptr=process_get_versioninfo_block (data_ptr, &block);
375 if(block.data_len==0) {
376 /* We must have hit padding, so give up
377 * processing now
379 #ifdef DEBUG
380 g_message (G_GNUC_PRETTY_FUNCTION
381 ": Hit 0-length block, giving up");
382 #endif
383 return(NULL);
385 string_len=string_len+block.data_len;
387 value = unicode_get (block.key);
388 language = g_utf16_to_utf8 (value, unicode_bytes (block.key), NULL, NULL, NULL);
389 g_strdown (language);
391 /* Kludge: treat en_US as neutral too */
392 if (!strcmp (language, "007f04b0") ||
393 !strcmp (language, "000004b0") ||
394 !strcmp (language, "040904b0")) {
395 /* Got the one we're interested in */
396 process_set_field_string_utf8 (filever, "language",
397 "Language Neutral");
399 data_ptr=process_read_string_block (filever, data_ptr,
400 block.data_len,
401 TRUE);
402 } else {
403 /* Some other language. We might want to do
404 * something with this in the future.
406 data_ptr=process_read_string_block (filever, data_ptr,
407 block.data_len,
408 FALSE);
410 g_free (language);
411 g_free (value);
413 if(data_ptr==NULL) {
414 /* Child block hit padding */
415 #ifdef DEBUG
416 g_message (G_GNUC_PRETTY_FUNCTION ": Child block hit 0-length block, giving up");
417 #endif
418 return(NULL);
422 return(data_ptr);
425 static void process_read_fixedfileinfo_block (MonoObject *filever,
426 VS_FIXEDFILEINFO *ffi)
428 #ifdef DEBUG
429 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);
430 #endif
432 process_set_field_int (filever, "filemajorpart",
433 HIWORD (ffi->dwFileVersionMS));
434 process_set_field_int (filever, "fileminorpart",
435 LOWORD (ffi->dwFileVersionMS));
436 process_set_field_int (filever, "filebuildpart",
437 HIWORD (ffi->dwFileVersionLS));
438 process_set_field_int (filever, "fileprivatepart",
439 LOWORD (ffi->dwFileVersionLS));
441 process_set_field_int (filever, "productmajorpart",
442 HIWORD (ffi->dwProductVersionMS));
443 process_set_field_int (filever, "productminorpart",
444 LOWORD (ffi->dwProductVersionMS));
445 process_set_field_int (filever, "productbuildpart",
446 HIWORD (ffi->dwProductVersionLS));
447 process_set_field_int (filever, "productprivatepart",
448 LOWORD (ffi->dwProductVersionLS));
450 process_set_field_bool (filever, "isdebug",
451 ffi->dwFileFlags&VS_FF_DEBUG);
452 process_set_field_bool (filever, "isprerelease",
453 ffi->dwFileFlags&VS_FF_PRERELEASE);
454 process_set_field_bool (filever, "ispatched",
455 ffi->dwFileFlags&VS_FF_PATCHED);
456 process_set_field_bool (filever, "isprivatebuild",
457 ffi->dwFileFlags&VS_FF_PRIVATEBUILD);
458 process_set_field_bool (filever, "isspecialbuild",
459 ffi->dwFileFlags&VS_FF_SPECIALBUILD);
462 static void process_get_fileversion (MonoObject *filever, MonoImage *image)
464 MonoPEResourceDataEntry *version_info;
465 gpointer data;
466 VS_FIXEDFILEINFO *ffi;
467 gpointer data_ptr;
468 version_data block;
469 gint32 data_len; /* signed to guard against underflow */
471 version_info=mono_image_lookup_resource (image,
472 MONO_PE_RESOURCE_ID_VERSION,
473 0, NULL);
474 #ifdef DEBUG
475 g_message (G_GNUC_PRETTY_FUNCTION ": image_lookup returned %p",
476 version_info);
477 #endif
479 if(version_info==NULL) {
480 return;
483 data=mono_image_rva_map (image,
484 version_info->rde_data_offset);
485 g_free (version_info);
486 if(data==NULL) {
487 return;
490 /* See io-layer/versioninfo.h for the gory details on how this
491 * data is laid out. (data should be pointing to
492 * VS_VERSIONINFO data).
495 data_ptr=process_get_versioninfo_block (data, &block);
497 data_len=block.data_len;
499 if(block.value_len!=sizeof(VS_FIXEDFILEINFO)) {
500 #ifdef DEBUG
501 g_message (G_GNUC_PRETTY_FUNCTION
502 ": FIXEDFILEINFO size mismatch");
503 #endif
504 return;
507 if (!unicode_string_equals (block.key, "VS_VERSION_INFO")) {
508 #ifdef DEBUG
509 g_message (G_GNUC_PRETTY_FUNCTION
510 ": VS_VERSION_INFO mismatch");
511 #endif
512 return;
515 ffi=((VS_FIXEDFILEINFO *)data_ptr);
516 data_ptr = (char *)data_ptr + sizeof(VS_FIXEDFILEINFO);
517 if((ffi->dwSignature!=VS_FFI_SIGNATURE) ||
518 (ffi->dwStrucVersion!=VS_FFI_STRUCVERSION)) {
519 #ifdef DEBUG
520 g_message (G_GNUC_PRETTY_FUNCTION
521 ": FIXEDFILEINFO bad signature");
522 #endif
523 return;
525 process_read_fixedfileinfo_block (filever, ffi);
527 /* Subtract the 92 bytes we've already seen */
528 data_len -= 92;
530 /* There now follow zero or one StringFileInfo blocks and zero
531 * or one VarFileInfo blocks
533 while(data_len > 0) {
534 /* align on a 32-bit boundary */
535 data_ptr=(gpointer)((char *)data_ptr + 3);
536 data_ptr=(gpointer)((char *)data_ptr -
537 (GPOINTER_TO_INT(data_ptr) & 3));
539 data_ptr=process_get_versioninfo_block (data_ptr, &block);
540 if(block.data_len==0) {
541 /* We must have hit padding, so give up
542 * processing now
544 #ifdef DEBUG
545 g_message (G_GNUC_PRETTY_FUNCTION
546 ": Hit 0-length block, giving up");
547 #endif
548 return;
551 data_len=data_len-block.data_len;
553 if (unicode_string_equals (block.key, "VarFileInfo")) {
554 data_ptr=process_read_var_block (filever, data_ptr,
555 block.data_len);
556 } else if (unicode_string_equals (block.key, "StringFileInfo")) {
557 data_ptr=process_read_stringtable_block (filever, data_ptr, block.data_len);
558 } else {
559 /* Bogus data */
560 #ifdef DEBUG
561 g_message (G_GNUC_PRETTY_FUNCTION
562 ": Not a valid VERSIONINFO child block");
563 return;
564 #endif
567 if(data_ptr==NULL) {
568 /* Child block hit padding */
569 #ifdef DEBUG
570 g_message (G_GNUC_PRETTY_FUNCTION ": Child block hit 0-length block, giving up");
571 #endif
572 return;
577 static void process_add_module (GPtrArray *modules, MonoAssembly *ass)
579 MonoClass *proc_class, *filever_class;
580 MonoObject *item, *filever;
581 MonoDomain *domain=mono_domain_get ();
582 gchar *modulename;
583 const char* filename;
585 /* Build a System.Diagnostics.ProcessModule with the data.
586 * Leave BaseAddress and EntryPointAddress set to NULL,
587 * FileName is ass->image->name, FileVersionInfo is an object
588 * constructed from the PE image data referenced by
589 * ass->image, ModuleMemorySize set to 0, ModuleName the last
590 * component of FileName.
592 proc_class=mono_class_from_name (system_assembly, "System.Diagnostics",
593 "ProcessModule");
594 item=mono_object_new (domain, proc_class);
596 filever_class=mono_class_from_name (system_assembly,
597 "System.Diagnostics",
598 "FileVersionInfo");
599 filever=mono_object_new (domain, filever_class);
601 #ifdef DEBUG
602 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);
603 #endif
605 process_get_fileversion (filever, mono_assembly_get_image (ass));
607 filename = mono_image_get_filename (mono_assembly_get_image (ass));
608 process_set_field_string_utf8 (filever, "filename", filename);
609 process_set_field_string_utf8 (item, "filename", filename);
610 process_set_field_object (item, "version_info", filever);
612 modulename=g_path_get_basename (filename);
613 process_set_field_string_utf8 (item, "modulename", modulename);
614 g_free (modulename);
616 /* FIXME: moving GC */
617 g_ptr_array_add (modules, item);
620 static void process_scan_modules (gpointer data, gpointer user_data)
622 MonoAssembly *ass=data;
623 GPtrArray *modules=user_data;
625 /* The main assembly is already in the list */
626 if(mono_assembly_get_main () != ass) {
627 process_add_module (modules, ass);
632 /* Returns an array of System.Diagnostics.ProcessModule */
633 MonoArray *ves_icall_System_Diagnostics_Process_GetModules_internal (MonoObject *this)
635 /* I was going to use toolhelp for this, but then realised I
636 * was being an idiot :)
638 * (Toolhelp would give shared libraries open by the runtime,
639 * as well as open assemblies. On windows my tests didnt find
640 * the assemblies loaded by mono either.)
642 GPtrArray *modules_list=g_ptr_array_new ();
643 MonoArray *arr;
644 guint32 i;
646 MONO_ARCH_SAVE_REGS;
648 STASH_SYS_ASS (this);
650 /* Make sure the first entry is the main module */
651 process_add_module (modules_list, mono_assembly_get_main ());
653 mono_assembly_foreach (process_scan_modules, modules_list);
655 /* Build a MonoArray out of modules_list */
656 arr=mono_array_new (mono_domain_get (), mono_get_object_class (),
657 modules_list->len);
659 for(i=0; i<modules_list->len; i++) {
660 mono_array_setref (arr, i, g_ptr_array_index (modules_list, i));
663 g_ptr_array_free (modules_list, TRUE);
665 return(arr);
668 void ves_icall_System_Diagnostics_FileVersionInfo_GetVersionInfo_internal (MonoObject *this, MonoString *filename)
670 MonoImage *image;
671 gchar *filename_utf8;
673 MONO_ARCH_SAVE_REGS;
675 STASH_SYS_ASS (this);
677 filename_utf8 = mono_string_to_utf8 (filename);
678 image = mono_pe_file_open (filename_utf8, NULL);
679 g_free (filename_utf8);
681 if(image==NULL) {
682 /* FIXME: an exception might be appropriate here */
683 #ifdef DEBUG
684 g_message (G_GNUC_PRETTY_FUNCTION ": Failed to load image");
685 #endif
687 return;
690 process_get_fileversion (this, image);
691 process_set_field_string_utf8 (this, "filename", mono_image_get_filename (image));
693 mono_image_close (image);
696 /* Only used when UseShellExecute is false */
697 static gchar *
698 quote_path (const gchar *path)
700 gchar *res = g_shell_quote (path);
701 #ifdef PLATFORM_WIN32
703 gchar *q = res;
704 while (*q) {
705 if (*q == '\'')
706 *q = '\"';
707 q++;
710 #endif
711 return res;
714 /* Only used when UseShellExecute is false */
715 static gboolean
716 complete_path (const gunichar2 *appname, gchar **completed)
718 gchar *utf8app;
719 gchar *found;
721 utf8app = g_utf16_to_utf8 (appname, -1, NULL, NULL, NULL);
722 if (g_path_is_absolute (utf8app)) {
723 *completed = quote_path (utf8app);
724 g_free (utf8app);
725 return TRUE;
728 if (g_file_test (utf8app, G_FILE_TEST_IS_EXECUTABLE) && !g_file_test (utf8app, G_FILE_TEST_IS_DIR)) {
729 *completed = quote_path (utf8app);
730 g_free (utf8app);
731 return TRUE;
734 found = g_find_program_in_path (utf8app);
735 if (found == NULL) {
736 *completed = NULL;
737 g_free (utf8app);
738 return FALSE;
741 *completed = quote_path (found);
742 g_free (found);
743 g_free (utf8app);
744 return TRUE;
747 MonoBoolean ves_icall_System_Diagnostics_Process_ShellExecuteEx_internal (MonoProcessStartInfo *proc_start_info, MonoProcInfo *process_info)
749 SHELLEXECUTEINFO shellex = {0};
750 gboolean ret;
752 shellex.cbSize = sizeof(SHELLEXECUTEINFO);
753 shellex.fMask = SEE_MASK_FLAG_DDEWAIT | SEE_MASK_NOCLOSEPROCESS | SEE_MASK_UNICODE;
754 shellex.nShow = SW_SHOWNORMAL;
756 if (proc_start_info->filename != NULL) {
757 shellex.lpFile = mono_string_chars (proc_start_info->filename);
760 if (proc_start_info->arguments != NULL) {
761 shellex.lpParameters = mono_string_chars (proc_start_info->arguments);
764 if (proc_start_info->verb != NULL &&
765 mono_string_length (proc_start_info->verb) != 0) {
766 shellex.lpVerb = mono_string_chars (proc_start_info->verb);
769 if (proc_start_info->working_directory != NULL &&
770 mono_string_length (proc_start_info->working_directory) != 0) {
771 shellex.lpDirectory = mono_string_chars (proc_start_info->working_directory);
774 ret = ShellExecuteEx (&shellex);
775 if (ret == FALSE) {
776 process_info->pid = -GetLastError ();
777 } else {
778 process_info->process_handle = shellex.hProcess;
779 process_info->thread_handle = NULL;
780 /* It appears that there's no way to get the pid from a
781 * process handle before windows xp. Really.
783 #ifdef HAVE_GETPROCESSID
784 process_info->pid = GetProcessId (shellex.hProcess);
785 #else
786 process_info->pid = 0;
787 #endif
788 process_info->tid = 0;
791 return (ret);
794 MonoBoolean ves_icall_System_Diagnostics_Process_CreateProcess_internal (MonoProcessStartInfo *proc_start_info, HANDLE stdin_handle, HANDLE stdout_handle, HANDLE stderr_handle, MonoProcInfo *process_info)
796 gboolean ret;
797 gunichar2 *dir;
798 STARTUPINFO startinfo={0};
799 PROCESS_INFORMATION procinfo;
800 gunichar2 *shell_path = NULL;
801 gchar *env_vars = NULL;
802 gboolean free_shell_path = TRUE;
803 gchar *spath = NULL;
804 MonoString *cmd = proc_start_info->arguments;
806 startinfo.cb=sizeof(STARTUPINFO);
807 startinfo.dwFlags=STARTF_USESTDHANDLES;
808 startinfo.hStdInput=stdin_handle;
809 startinfo.hStdOutput=stdout_handle;
810 startinfo.hStdError=stderr_handle;
812 shell_path = mono_string_chars (proc_start_info->filename);
813 complete_path (shell_path, &spath);
814 if (spath == NULL) {
815 process_info->pid = -ERROR_FILE_NOT_FOUND;
816 return FALSE;
818 #ifdef PLATFORM_WIN32
819 /* Seems like our CreateProcess does not work as the windows one.
820 * This hack is needed to deal with paths containing spaces */
821 shell_path = NULL;
822 free_shell_path = FALSE;
823 if (cmd) {
824 gchar *newcmd, *tmp;
825 tmp = mono_string_to_utf8 (cmd);
826 newcmd = g_strdup_printf ("%s %s", spath, tmp);
827 cmd = mono_string_new_wrapper (newcmd);
828 g_free (tmp);
829 g_free (newcmd);
831 else {
832 cmd = mono_string_new_wrapper (spath);
834 #else
835 shell_path = g_utf8_to_utf16 (spath, -1, NULL, NULL, NULL);
836 #endif
837 g_free (spath);
839 if (process_info->env_keys != NULL) {
840 gint i, len;
841 MonoString *ms;
842 MonoString *key, *value;
843 gunichar2 *str, *ptr;
844 gunichar2 *equals16;
846 for (len = 0, i = 0; i < mono_array_length (process_info->env_keys); i++) {
847 ms = mono_array_get (process_info->env_values, MonoString *, i);
848 if (ms == NULL)
849 continue;
851 len += mono_string_length (ms) * sizeof (gunichar2);
852 ms = mono_array_get (process_info->env_keys, MonoString *, i);
853 len += mono_string_length (ms) * sizeof (gunichar2);
854 len += 2 * sizeof (gunichar2);
857 equals16 = g_utf8_to_utf16 ("=", 1, NULL, NULL, NULL);
858 ptr = str = g_new0 (gunichar2, len + 1);
859 for (i = 0; i < mono_array_length (process_info->env_keys); i++) {
860 value = mono_array_get (process_info->env_values, MonoString *, i);
861 if (value == NULL)
862 continue;
864 key = mono_array_get (process_info->env_keys, MonoString *, i);
865 memcpy (ptr, mono_string_chars (key), mono_string_length (key) * sizeof (gunichar2));
866 ptr += mono_string_length (key);
868 memcpy (ptr, equals16, sizeof (gunichar2));
869 ptr++;
871 memcpy (ptr, mono_string_chars (value), mono_string_length (value) * sizeof (gunichar2));
872 ptr += mono_string_length (value);
873 ptr++;
876 g_free (equals16);
877 env_vars = (gchar *) str;
880 /* The default dir name is "". Turn that into NULL to mean
881 * "current directory"
883 if(mono_string_length (proc_start_info->working_directory)==0) {
884 dir=NULL;
885 } else {
886 dir=mono_string_chars (proc_start_info->working_directory);
889 ret=CreateProcess (shell_path, cmd? mono_string_chars (cmd): NULL, NULL, NULL, TRUE, CREATE_UNICODE_ENVIRONMENT, env_vars, dir, &startinfo, &procinfo);
891 g_free (env_vars);
892 if (free_shell_path)
893 g_free (shell_path);
895 if(ret) {
896 process_info->process_handle=procinfo.hProcess;
897 /*process_info->thread_handle=procinfo.hThread;*/
898 process_info->thread_handle=NULL;
899 if (procinfo.hThread != NULL && procinfo.hThread != INVALID_HANDLE_VALUE)
900 CloseHandle(procinfo.hThread);
901 process_info->pid=procinfo.dwProcessId;
902 process_info->tid=procinfo.dwThreadId;
903 } else {
904 process_info->pid = -GetLastError ();
907 return(ret);
910 MonoBoolean ves_icall_System_Diagnostics_Process_WaitForExit_internal (MonoObject *this, HANDLE process, gint32 ms)
912 guint32 ret;
914 MONO_ARCH_SAVE_REGS;
916 if(ms<0) {
917 /* Wait forever */
918 ret=WaitForSingleObjectEx (process, INFINITE, TRUE);
919 } else {
920 ret=WaitForSingleObjectEx (process, ms, TRUE);
922 if(ret==WAIT_OBJECT_0) {
923 return(TRUE);
924 } else {
925 return(FALSE);
929 gint64 ves_icall_System_Diagnostics_Process_ExitTime_internal (HANDLE process)
931 gboolean ret;
932 gint64 ticks;
933 FILETIME create_time, exit_time, kernel_time, user_time;
935 MONO_ARCH_SAVE_REGS;
937 ret=GetProcessTimes (process, &create_time, &exit_time, &kernel_time,
938 &user_time);
939 if(ret==TRUE) {
940 ticks=((guint64)exit_time.dwHighDateTime << 32) +
941 exit_time.dwLowDateTime;
943 return(ticks);
944 } else {
945 return(0);
949 gint64 ves_icall_System_Diagnostics_Process_StartTime_internal (HANDLE process)
951 gboolean ret;
952 gint64 ticks;
953 FILETIME create_time, exit_time, kernel_time, user_time;
955 MONO_ARCH_SAVE_REGS;
957 ret=GetProcessTimes (process, &create_time, &exit_time, &kernel_time,
958 &user_time);
959 if(ret==TRUE) {
960 ticks=((guint64)create_time.dwHighDateTime << 32) +
961 create_time.dwLowDateTime;
963 return(ticks);
964 } else {
965 return(0);
969 gint32 ves_icall_System_Diagnostics_Process_ExitCode_internal (HANDLE process)
971 guint32 code;
973 MONO_ARCH_SAVE_REGS;
975 GetExitCodeProcess (process, &code);
977 #ifdef DEBUG
978 g_message (G_GNUC_PRETTY_FUNCTION ": process exit code is %d", code);
979 #endif
981 return(code);
984 MonoString *ves_icall_System_Diagnostics_Process_ProcessName_internal (HANDLE process)
986 MonoString *string;
987 gboolean ok;
988 HMODULE mod;
989 gunichar2 name[MAX_PATH];
990 guint32 needed;
991 guint32 len;
993 MONO_ARCH_SAVE_REGS;
995 ok=EnumProcessModules (process, &mod, sizeof(mod), &needed);
996 if(ok==FALSE) {
997 return(NULL);
1000 len=GetModuleBaseName (process, mod, name, MAX_PATH);
1001 if(len==0) {
1002 return(NULL);
1005 #ifdef DEBUG
1006 g_message (G_GNUC_PRETTY_FUNCTION ": process name is [%s]",
1007 g_utf16_to_utf8 (name, -1, NULL, NULL, NULL));
1008 #endif
1010 string=mono_string_new_utf16 (mono_domain_get (), name, len);
1012 return(string);
1015 /* Returns an array of pids */
1016 MonoArray *ves_icall_System_Diagnostics_Process_GetProcesses_internal (void)
1018 MonoArray *procs;
1019 gboolean ret;
1020 guint32 needed, count, i;
1021 guint32 pids[1024];
1023 MONO_ARCH_SAVE_REGS;
1025 ret=EnumProcesses (pids, sizeof(pids), &needed);
1026 if(ret==FALSE) {
1027 /* FIXME: throw an exception */
1028 return(NULL);
1031 count=needed/sizeof(guint32);
1032 procs=mono_array_new (mono_domain_get (), mono_get_int32_class (),
1033 count);
1034 for(i=0; i<count; i++) {
1035 mono_array_set (procs, guint32, i, pids[i]);
1038 return(procs);
1041 MonoBoolean ves_icall_System_Diagnostics_Process_GetWorkingSet_internal (HANDLE process, guint32 *min, guint32 *max)
1043 gboolean ret;
1044 size_t ws_min, ws_max;
1046 MONO_ARCH_SAVE_REGS;
1048 ret=GetProcessWorkingSetSize (process, &ws_min, &ws_max);
1049 *min=(guint32)ws_min;
1050 *max=(guint32)ws_max;
1052 return(ret);
1055 MonoBoolean ves_icall_System_Diagnostics_Process_SetWorkingSet_internal (HANDLE process, guint32 min, guint32 max, MonoBoolean use_min)
1057 gboolean ret;
1058 size_t ws_min;
1059 size_t ws_max;
1061 MONO_ARCH_SAVE_REGS;
1063 ret=GetProcessWorkingSetSize (process, &ws_min, &ws_max);
1064 if(ret==FALSE) {
1065 return(FALSE);
1068 if(use_min==TRUE) {
1069 ws_min=min;
1070 } else {
1071 ws_max=max;
1074 ret=SetProcessWorkingSetSize (process, ws_min, ws_max);
1076 return(ret);
1079 MonoBoolean
1080 ves_icall_System_Diagnostics_Process_Kill_internal (HANDLE process, gint32 sig)
1082 MONO_ARCH_SAVE_REGS;
1084 /* sig == 1 -> Kill, sig == 2 -> CloseMainWindow */
1086 return TerminateProcess (process, -sig);