Implemeneted imprecise.
[mono-project.git] / mono / metadata / w32process-unix.c
blobfcb893543ef92eda19d5f78f2f43150dddd9901f
1 /**
2 * \file
3 * System.Diagnostics.Process support
5 * Author:
6 * Dick Porter (dick@ximian.com)
8 * Copyright 2002 Ximian, Inc.
9 * Copyright 2002-2006 Novell, Inc.
10 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
13 #include <config.h>
14 #include <glib.h>
16 #include <stdio.h>
17 #include <string.h>
18 #include <pthread.h>
19 #include <sched.h>
20 #include <sys/time.h>
21 #include <errno.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <unistd.h>
25 #ifdef HAVE_SIGNAL_H
26 #include <signal.h>
27 #endif
28 #include <sys/time.h>
29 #include <fcntl.h>
30 #ifdef HAVE_SYS_PARAM_H
31 #include <sys/param.h>
32 #endif
33 #include <ctype.h>
35 #ifdef HAVE_SYS_WAIT_H
36 #include <sys/wait.h>
37 #endif
38 #ifdef HAVE_SYS_RESOURCE_H
39 #include <sys/resource.h>
40 #endif
42 #ifdef HAVE_SYS_MKDEV_H
43 #include <sys/mkdev.h>
44 #endif
46 #ifdef HAVE_UTIME_H
47 #include <utime.h>
48 #endif
50 // For close_my_fds
51 #if defined (_AIX)
52 #include <procinfo.h>
53 #elif defined (__FreeBSD__)
54 #include <sys/sysctl.h>
55 #include <sys/user.h>
56 #include <libutil.h>
57 #elif defined(__linux__)
58 #include <dirent.h>
59 #endif
61 #include <mono/metadata/object-internals.h>
62 #include <mono/metadata/w32process.h>
63 #include <mono/metadata/w32process-internals.h>
64 #include <mono/metadata/w32process-unix-internals.h>
65 #include <mono/metadata/w32error.h>
66 #include <mono/metadata/class.h>
67 #include <mono/metadata/class-internals.h>
68 #include <mono/metadata/object.h>
69 #include <mono/metadata/metadata.h>
70 #include <mono/metadata/metadata-internals.h>
71 #include <mono/metadata/exception.h>
72 #include <mono/metadata/w32handle.h>
73 #include <mono/metadata/w32file.h>
74 #include <mono/utils/mono-membar.h>
75 #include <mono/utils/mono-logger-internals.h>
76 #include <mono/utils/strenc-internals.h>
77 #include <mono/utils/strenc.h>
78 #include <mono/utils/mono-proclib.h>
79 #include <mono/utils/mono-path.h>
80 #include <mono/utils/mono-lazy-init.h>
81 #include <mono/utils/mono-signal-handler.h>
82 #include <mono/utils/mono-time.h>
83 #include <mono/utils/mono-mmap.h>
84 #include <mono/utils/strenc.h>
85 #include <mono/utils/mono-io-portability.h>
86 #include <mono/utils/w32api.h>
87 #include <mono/utils/mono-errno.h>
88 #include <mono/utils/mono-error-internals.h>
89 #include <mono/utils/mono-threads-coop.h>
90 #include "object-internals.h"
91 #include "icall-decl.h"
93 #ifndef ENABLE_NETCORE
95 #ifndef MAXPATHLEN
96 #define MAXPATHLEN 242
97 #endif
99 #define STILL_ACTIVE ((int) 0x00000103)
101 #define LOGDEBUG(...)
102 /* define LOGDEBUG(...) g_message(__VA_ARGS__) */
104 /* The process' environment strings */
105 #if defined (HAVE_FORK) && defined (HAVE_EXECVE)
106 #if defined(__APPLE__)
107 #if defined (TARGET_OSX)
108 /* Apple defines this in crt_externs.h but doesn't provide that header for
109 * arm-apple-darwin9. We'll manually define the symbol on Apple as it does
110 * in fact exist on all implementations (so far)
112 G_BEGIN_DECLS
113 gchar ***_NSGetEnviron(void);
114 G_END_DECLS
115 #define environ (*_NSGetEnviron())
116 #else
117 static char *mono_environ[1] = { NULL };
118 #define environ mono_environ
119 #endif /* defined (TARGET_OSX) */
120 #else
121 G_BEGIN_DECLS
122 extern char **environ;
123 G_END_DECLS
124 #endif
125 #endif
127 typedef enum {
128 STARTF_USESHOWWINDOW=0x001,
129 STARTF_USESIZE=0x002,
130 STARTF_USEPOSITION=0x004,
131 STARTF_USECOUNTCHARS=0x008,
132 STARTF_USEFILLATTRIBUTE=0x010,
133 STARTF_RUNFULLSCREEN=0x020,
134 STARTF_FORCEONFEEDBACK=0x040,
135 STARTF_FORCEOFFFEEDBACK=0x080,
136 STARTF_USESTDHANDLES=0x100
137 } StartupFlags;
139 typedef struct {
140 gpointer input;
141 gpointer output;
142 gpointer error;
143 } StartupHandles;
145 typedef struct {
146 #if G_BYTE_ORDER == G_BIG_ENDIAN
147 guint32 highDateTime;
148 guint32 lowDateTime;
149 #else
150 guint32 lowDateTime;
151 guint32 highDateTime;
152 #endif
153 } ProcessTime;
156 * Process describes processes we create.
157 * It contains a semaphore that can be waited on in order to wait
158 * for process termination.
160 typedef struct _Process {
161 pid_t pid; /* the pid of the process. This value is only valid until the process has exited. */
162 MonoCoopSem exit_sem; /* this semaphore will be released when the process exits */
163 int status; /* the exit status */
164 gint32 handle_count; /* the number of handles to this process instance */
165 /* we keep a ref to the creating _WapiHandle_process handle until
166 * the process has exited, so that the information there isn't lost.
168 gpointer handle;
169 gboolean signalled;
170 struct _Process *next;
171 } Process;
173 /* MonoW32HandleProcess is a structure containing all the required information for process handling. */
174 typedef struct {
175 pid_t pid;
176 gboolean child;
177 guint32 exitstatus;
178 gpointer main_thread;
179 guint64 create_time;
180 guint64 exit_time;
181 char *pname;
182 size_t min_working_set;
183 size_t max_working_set;
184 gboolean exited;
185 Process *process;
186 } MonoW32HandleProcess;
189 * VS_VERSIONINFO:
191 * 2 bytes: Length in bytes (this block, and all child blocks. does _not_ include alignment padding between blocks)
192 * 2 bytes: Length in bytes of VS_FIXEDFILEINFO struct
193 * 2 bytes: Type (contains 1 if version resource contains text data and 0 if version resource contains binary data)
194 * Variable length unicode string (null terminated): Key (currently "VS_VERSION_INFO")
195 * Variable length padding to align VS_FIXEDFILEINFO on a 32-bit boundary
196 * VS_FIXEDFILEINFO struct
197 * Variable length padding to align Child struct on a 32-bit boundary
198 * Child struct (zero or one StringFileInfo structs, zero or one VarFileInfo structs)
202 * StringFileInfo:
204 * 2 bytes: Length in bytes (includes this block, as well as all Child blocks)
205 * 2 bytes: Value length (always zero)
206 * 2 bytes: Type (contains 1 if version resource contains text data and 0 if version resource contains binary data)
207 * Variable length unicode string: Key (currently "StringFileInfo")
208 * Variable length padding to align Child struct on a 32-bit boundary
209 * Child structs ( one or more StringTable structs. Each StringTable struct's Key member indicates the appropriate language and code page for displaying the text in that StringTable struct.)
213 * StringTable:
215 * 2 bytes: Length in bytes (includes this block as well as all Child blocks, but excludes any padding between String blocks)
216 * 2 bytes: Value length (always zero)
217 * 2 bytes: Type (contains 1 if version resource contains text data and 0 if version resource contains binary data)
218 * Variable length unicode string: Key. An 8-digit hex number stored as a unicode string. The four most significant digits represent the language identifier. The four least significant digits represent the code page for which the data is formatted.
219 * Variable length padding to align Child struct on a 32-bit boundary
220 * Child structs (an array of one or more String structs (each aligned on a 32-bit boundary)
224 * String:
226 * 2 bytes: Length in bytes (of this block)
227 * 2 bytes: Value length (the length in words of the Value member)
228 * 2 bytes: Type (contains 1 if version resource contains text data and 0 if version resource contains binary data)
229 * Variable length unicode string: Key. arbitrary string, identifies data.
230 * Variable length padding to align Value on a 32-bit boundary
231 * Value: Variable length unicode string, holding data.
235 * VarFileInfo:
237 * 2 bytes: Length in bytes (includes this block, as well as all Child blocks)
238 * 2 bytes: Value length (always zero)
239 * 2 bytes: Type (contains 1 if version resource contains text data and 0 if version resource contains binary data)
240 * Variable length unicode string: Key (currently "VarFileInfo")
241 * Variable length padding to align Child struct on a 32-bit boundary
242 * Child structs (a Var struct)
246 * Var:
248 * 2 bytes: Length in bytes of this block
249 * 2 bytes: Value length in bytes of the Value
250 * 2 bytes: Type (contains 1 if version resource contains text data and 0 if version resource contains binary data)
251 * Variable length unicode string: Key ("Translation")
252 * Variable length padding to align Value on a 32-bit boundary
253 * Value: an array of one or more 4 byte values that are language and code page identifier pairs, low-order word containing a language identifier, and the high-order word containing a code page number. Either word can be zero, indicating that the file is language or code page independent.
256 #if G_BYTE_ORDER == G_BIG_ENDIAN
257 #define VS_FFI_SIGNATURE 0xbd04effe
258 #define VS_FFI_STRUCVERSION 0x00000100
259 #else
260 #define VS_FFI_SIGNATURE 0xfeef04bd
261 #define VS_FFI_STRUCVERSION 0x00010000
262 #endif
264 #define VOS_UNKNOWN 0x00000000
265 #define VOS_DOS 0x00010000
266 #define VOS_OS216 0x00020000
267 #define VOS_OS232 0x00030000
268 #define VOS_NT 0x00040000
269 #define VOS__BASE 0x00000000
270 #define VOS__WINDOWS16 0x00000001
271 #define VOS__PM16 0x00000002
272 #define VOS__PM32 0x00000003
273 #define VOS__WINDOWS32 0x00000004
274 /* Should "embrace and extend" here with some entries for linux etc */
276 #define VOS_DOS_WINDOWS16 0x00010001
277 #define VOS_DOS_WINDOWS32 0x00010004
278 #define VOS_OS216_PM16 0x00020002
279 #define VOS_OS232_PM32 0x00030003
280 #define VOS_NT_WINDOWS32 0x00040004
282 #define VFT_UNKNOWN 0x0000
283 #define VFT_APP 0x0001
284 #define VFT_DLL 0x0002
285 #define VFT_DRV 0x0003
286 #define VFT_FONT 0x0004
287 #define VFT_VXD 0x0005
288 #define VFT_STATIC_LIB 0x0007
290 #define VFT2_UNKNOWN 0x0000
291 #define VFT2_DRV_PRINTER 0x0001
292 #define VFT2_DRV_KEYBOARD 0x0002
293 #define VFT2_DRV_LANGUAGE 0x0003
294 #define VFT2_DRV_DISPLAY 0x0004
295 #define VFT2_DRV_MOUSE 0x0005
296 #define VFT2_DRV_NETWORK 0x0006
297 #define VFT2_DRV_SYSTEM 0x0007
298 #define VFT2_DRV_INSTALLABLE 0x0008
299 #define VFT2_DRV_SOUND 0x0009
300 #define VFT2_DRV_COMM 0x000a
301 #define VFT2_DRV_INPUTMETHOD 0x000b
302 #define VFT2_FONT_RASTER 0x0001
303 #define VFT2_FONT_VECTOR 0x0002
304 #define VFT2_FONT_TRUETYPE 0x0003
306 #define MAKELANGID(primary,secondary) ((guint16)((secondary << 10) | (primary)))
308 #define ALIGN32(ptr) ptr = (gpointer)((char *)ptr + 3); ptr = (gpointer)((char *)ptr - ((gsize)ptr & 3));
310 #if HAVE_SIGACTION
311 static mono_lazy_init_t process_sig_chld_once = MONO_LAZY_INIT_STATUS_NOT_INITIALIZED;
312 #endif
314 static gchar *cli_launcher;
316 static Process *processes;
317 static MonoCoopMutex processes_mutex;
319 static pid_t current_pid;
320 static gpointer current_process;
322 static const gunichar2 utf16_space [2] = { 0x20, 0 };
323 static const gunichar2 utf16_quote [2] = { 0x22, 0 };
325 static MonoBoolean
326 mono_get_exit_code_process (gpointer handle, gint32 *exitcode);
328 /* Check if a pid is valid - i.e. if a process exists with this pid. */
329 static gboolean
330 process_is_alive (pid_t pid)
332 #if defined(HOST_WATCHOS)
333 return TRUE; // TODO: Rewrite using sysctl
334 #elif defined(HOST_DARWIN) || defined(__OpenBSD__) || defined(__FreeBSD__) || defined(_AIX)
335 if (pid == 0)
336 return FALSE;
337 if (kill (pid, 0) == 0)
338 return TRUE;
339 if (errno == EPERM)
340 return TRUE;
341 return FALSE;
342 #elif defined(__HAIKU__)
343 team_info teamInfo;
344 if (get_team_info ((team_id)pid, &teamInfo) == B_OK)
345 return TRUE;
346 return FALSE;
347 #else
348 gchar *dir = g_strdup_printf ("/proc/%d", pid);
349 gboolean result = access (dir, F_OK) == 0;
350 g_free (dir);
351 return result;
352 #endif
355 static void
356 process_details (MonoW32Handle *handle_data)
358 MonoW32HandleProcess *process_handle = (MonoW32HandleProcess *) handle_data->specific;
359 g_print ("pid: %d, exited: %s, exitstatus: %d",
360 process_handle->pid, process_handle->exited ? "true" : "false", process_handle->exitstatus);
363 static const gchar*
364 process_typename (void)
366 return "Process";
369 static gsize
370 process_typesize (void)
372 return sizeof (MonoW32HandleProcess);
375 static MonoW32HandleWaitRet
376 process_wait (MonoW32Handle *handle_data, guint32 timeout, gboolean *alerted)
378 MonoW32HandleProcess *process_handle;
379 pid_t pid G_GNUC_UNUSED, ret;
380 int status;
381 gint64 start, now;
382 Process *process;
384 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s (%p, %" G_GUINT32_FORMAT ")", __func__, handle_data, timeout);
386 if (alerted)
387 *alerted = FALSE;
389 process_handle = (MonoW32HandleProcess*) handle_data->specific;
391 if (process_handle->exited) {
392 /* We've already done this one */
393 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s (%p, %" G_GUINT32_FORMAT "): Process already exited", __func__, handle_data, timeout);
394 return MONO_W32HANDLE_WAIT_RET_SUCCESS_0;
397 pid = process_handle->pid;
399 if (pid == mono_process_current_pid ()) {
400 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s (%p, %" G_GUINT32_FORMAT "): waiting on current process", __func__, handle_data, timeout);
401 return MONO_W32HANDLE_WAIT_RET_TIMEOUT;
404 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s (%p, %" G_GUINT32_FORMAT "): PID: %d", __func__, handle_data, timeout, pid);
406 if (!process_handle->child) {
407 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s (%p, %" G_GUINT32_FORMAT "): waiting on non-child process", __func__, handle_data, timeout);
409 if (!process_is_alive (pid)) {
410 /* assume the process has exited */
411 process_handle->exited = TRUE;
412 process_handle->exitstatus = -1;
413 mono_w32handle_set_signal_state (handle_data, TRUE, TRUE);
415 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s (%p, %" G_GUINT32_FORMAT "): non-child process is not alive anymore (2)", __func__, handle_data, timeout);
416 return MONO_W32HANDLE_WAIT_RET_SUCCESS_0;
419 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s (%p, %" G_GUINT32_FORMAT "): non-child process wait failed, error : %s (%d))", __func__, handle_data, timeout, g_strerror (errno), errno);
420 return MONO_W32HANDLE_WAIT_RET_FAILED;
423 /* We don't need to lock processes here, the entry
424 * has a handle_count > 0 which means it will not be freed. */
425 process = process_handle->process;
426 g_assert (process);
428 start = mono_msec_ticks ();
429 now = start;
431 while (1) {
432 if (timeout != MONO_INFINITE_WAIT) {
433 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s (%p, %" G_GUINT32_FORMAT "): waiting on semaphore for %" G_GINT64_FORMAT " ms...",
434 __func__, handle_data, timeout, timeout - (now - start));
435 ret = mono_coop_sem_timedwait (&process->exit_sem, (timeout - (now - start)), alerted ? MONO_SEM_FLAGS_ALERTABLE : MONO_SEM_FLAGS_NONE);
436 } else {
437 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s (%p, %" G_GUINT32_FORMAT "): waiting on semaphore forever...",
438 __func__, handle_data, timeout);
439 ret = mono_coop_sem_wait (&process->exit_sem, alerted ? MONO_SEM_FLAGS_ALERTABLE : MONO_SEM_FLAGS_NONE);
442 if (ret == MONO_SEM_TIMEDWAIT_RET_SUCCESS) {
443 /* Success, process has exited */
444 mono_coop_sem_post (&process->exit_sem);
445 break;
448 if (ret == MONO_SEM_TIMEDWAIT_RET_TIMEDOUT) {
449 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s (%p, %" G_GUINT32_FORMAT "): wait timeout (timeout = 0)", __func__, handle_data, timeout);
450 return MONO_W32HANDLE_WAIT_RET_TIMEOUT;
453 now = mono_msec_ticks ();
454 if (now - start >= timeout) {
455 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s (%p, %" G_GUINT32_FORMAT "): wait timeout", __func__, handle_data, timeout);
456 return MONO_W32HANDLE_WAIT_RET_TIMEOUT;
459 if (alerted && ret == MONO_SEM_TIMEDWAIT_RET_ALERTED) {
460 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s (%p, %" G_GUINT32_FORMAT "): wait alerted", __func__, handle_data, timeout);
461 *alerted = TRUE;
462 return MONO_W32HANDLE_WAIT_RET_ALERTED;
466 /* Process must have exited */
467 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s (%p, %" G_GUINT32_FORMAT "): Waited successfully", __func__, handle_data, timeout);
469 status = process->status;
470 if (WIFSIGNALED (status))
471 process_handle->exitstatus = 128 + WTERMSIG (status);
472 else
473 process_handle->exitstatus = WEXITSTATUS (status);
475 process_handle->exit_time = mono_100ns_datetime ();
477 process_handle->exited = TRUE;
479 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s (%p, %" G_GUINT32_FORMAT "): Setting pid %d signalled, exit status %d",
480 __func__, handle_data, timeout, process_handle->pid, process_handle->exitstatus);
482 mono_w32handle_set_signal_state (handle_data, TRUE, TRUE);
484 return MONO_W32HANDLE_WAIT_RET_SUCCESS_0;
487 static void
488 processes_cleanup (void)
490 static gint32 cleaning_up;
491 Process *process;
492 Process *prev = NULL;
494 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s", __func__);
496 /* Ensure we're not in here in multiple threads at once, nor recursive. */
497 if (mono_atomic_cas_i32 (&cleaning_up, 1, 0) != 0)
498 return;
501 * This needs to be done outside the lock but atomically, hence the CAS above.
503 for (process = processes; process; process = process->next) {
504 if (process->signalled && process->handle) {
505 /* This process has exited and we need to remove the artifical ref
506 * on the handle */
507 mono_w32handle_close (process->handle);
508 process->handle = NULL;
512 mono_coop_mutex_lock (&processes_mutex);
514 for (process = processes; process;) {
515 Process *next = process->next;
516 if (process->handle_count == 0 && process->signalled) {
518 * Unlink the entry.
520 if (process == processes)
521 processes = process->next;
522 else
523 prev->next = process->next;
525 mono_coop_sem_destroy (&process->exit_sem);
526 g_free (process);
527 } else {
528 prev = process;
530 process = next;
533 mono_coop_mutex_unlock (&processes_mutex);
535 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s done", __func__);
537 mono_atomic_xchg_i32 (&cleaning_up, 0);
540 static void
541 process_close (gpointer data)
543 MonoW32HandleProcess *process_handle;
545 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s", __func__);
547 process_handle = (MonoW32HandleProcess *) data;
548 g_free (process_handle->pname);
549 process_handle->pname = NULL;
550 if (process_handle->process)
551 mono_atomic_dec_i32 (&process_handle->process->handle_count);
552 processes_cleanup ();
555 static const MonoW32HandleOps process_ops = {
556 process_close, /* close_shared */
557 NULL, /* signal */
558 NULL, /* own */
559 NULL, /* is_owned */
560 process_wait, /* special_wait */
561 NULL, /* prewait */
562 process_details, /* details */
563 process_typename, /* typename */
564 process_typesize, /* typesize */
567 static void
568 process_set_defaults (MonoW32HandleProcess *process_handle)
570 /* These seem to be the defaults on w2k */
571 process_handle->min_working_set = 204800;
572 process_handle->max_working_set = 1413120;
574 process_handle->create_time = mono_100ns_datetime ();
577 static void
578 process_set_name (MonoW32HandleProcess *process_handle)
580 char *progname, *utf8_progname, *slash;
582 progname = g_get_prgname ();
583 utf8_progname = mono_utf8_from_external (progname);
585 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: using [%s] as prog name", __func__, progname);
587 if (utf8_progname) {
588 slash = strrchr (utf8_progname, '/');
589 if (slash)
590 process_handle->pname = g_strdup (slash+1);
591 else
592 process_handle->pname = g_strdup (utf8_progname);
593 g_free (utf8_progname);
597 void
598 mono_w32process_init (void)
600 MonoW32HandleProcess process_handle;
602 mono_w32handle_register_ops (MONO_W32TYPE_PROCESS, &process_ops);
604 mono_w32handle_register_capabilities (MONO_W32TYPE_PROCESS,
605 (MonoW32HandleCapability)(MONO_W32HANDLE_CAP_WAIT | MONO_W32HANDLE_CAP_SPECIAL_WAIT));
607 current_pid = getpid ();
609 memset (&process_handle, 0, sizeof (process_handle));
610 process_handle.pid = current_pid;
611 process_set_defaults (&process_handle);
612 process_set_name (&process_handle);
614 current_process = mono_w32handle_new (MONO_W32TYPE_PROCESS, &process_handle);
615 g_assert (current_process != INVALID_HANDLE_VALUE);
617 mono_coop_mutex_init (&processes_mutex);
620 void
621 mono_w32process_cleanup (void)
623 g_free (cli_launcher);
626 static int
627 len16 (const gunichar2 *str)
629 int len = 0;
631 while (*str++ != 0)
632 len++;
634 return len;
637 static gunichar2 *
638 utf16_concat (const gunichar2 *first, ...)
640 va_list args;
641 int total = 0, i;
642 const gunichar2 *s;
643 const gunichar2 *p;
644 gunichar2 *ret;
646 va_start (args, first);
647 total += len16 (first);
648 for (s = va_arg (args, gunichar2 *); s != NULL; s = va_arg(args, gunichar2 *))
649 total += len16 (s);
650 va_end (args);
652 ret = g_new (gunichar2, total + 1);
653 if (ret == NULL)
654 return NULL;
656 ret [total] = 0;
657 i = 0;
658 for (s = first; *s != 0; s++)
659 ret [i++] = *s;
660 va_start (args, first);
661 for (s = va_arg (args, gunichar2 *); s != NULL; s = va_arg (args, gunichar2 *)){
662 for (p = s; *p != 0; p++)
663 ret [i++] = *p;
665 va_end (args);
667 return ret;
670 guint32
671 mono_w32process_get_pid (gpointer handle)
673 MonoW32Handle *handle_data;
674 guint32 ret;
676 if (!mono_w32handle_lookup_and_ref (handle, &handle_data)) {
677 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: unknown handle %p", __func__, handle);
678 mono_w32error_set_last (ERROR_INVALID_HANDLE);
679 return 0;
682 if (handle_data->type != MONO_W32TYPE_PROCESS) {
683 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: unknown process handle %p", __func__, handle);
684 mono_w32error_set_last (ERROR_INVALID_HANDLE);
685 mono_w32handle_unref (handle_data);
686 return 0;
689 ret = ((MonoW32HandleProcess*) handle_data->specific)->pid;
691 mono_w32handle_unref (handle_data);
693 return ret;
696 typedef struct {
697 guint32 pid;
698 gpointer handle;
699 } GetProcessForeachData;
701 static gboolean
702 get_process_foreach_callback (MonoW32Handle *handle_data, gpointer user_data)
704 GetProcessForeachData *foreach_data;
705 MonoW32HandleProcess *process_handle;
706 pid_t pid;
708 if (handle_data->type != MONO_W32TYPE_PROCESS)
709 return FALSE;
711 process_handle = (MonoW32HandleProcess*) handle_data->specific;
713 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: looking at process %d", __func__, process_handle->pid);
715 pid = process_handle->pid;
716 if (pid == 0)
717 return FALSE;
719 foreach_data = (GetProcessForeachData*) user_data;
721 /* It's possible to have more than one process handle with the
722 * same pid, but only the one running process can be
723 * unsignalled. */
724 if (foreach_data->pid != pid)
725 return FALSE;
726 if (mono_w32handle_issignalled (handle_data))
727 return FALSE;
729 foreach_data->handle = mono_w32handle_duplicate (handle_data);
730 return TRUE;
733 HANDLE
734 ves_icall_System_Diagnostics_Process_GetProcess_internal (guint32 pid, MonoError *error)
736 GetProcessForeachData foreach_data;
737 gpointer handle;
739 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: looking for process %d", __func__, pid);
741 memset (&foreach_data, 0, sizeof (foreach_data));
742 foreach_data.pid = pid;
743 mono_w32handle_foreach (get_process_foreach_callback, &foreach_data);
744 handle = foreach_data.handle;
745 if (handle) {
746 /* get_process_foreach_callback already added a ref */
747 return handle;
750 if (process_is_alive (pid)) {
751 /* non-child process */
752 MonoW32HandleProcess process_handle;
754 memset (&process_handle, 0, sizeof (process_handle));
755 process_handle.pid = pid;
756 process_handle.pname = mono_w32process_get_name (pid);
758 handle = mono_w32handle_new (MONO_W32TYPE_PROCESS, &process_handle);
759 if (handle == INVALID_HANDLE_VALUE) {
760 g_warning ("%s: error creating process handle", __func__);
762 mono_w32error_set_last (ERROR_OUTOFMEMORY);
763 return NULL;
766 return handle;
769 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: Can't find pid %d", __func__, pid);
771 mono_w32error_set_last (ERROR_PROC_NOT_FOUND);
772 return NULL;
775 static gboolean
776 match_procname_to_modulename (char *procname, char *modulename)
778 char* lastsep = NULL;
779 char* lastsep2 = NULL;
780 char* pname = NULL;
781 char* mname = NULL;
782 gboolean result = FALSE;
784 if (procname == NULL || modulename == NULL)
785 return (FALSE);
787 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: procname=\"%s\", modulename=\"%s\"", __func__, procname, modulename);
788 pname = mono_path_resolve_symlinks (procname);
789 mname = mono_path_resolve_symlinks (modulename);
791 if (!strcmp (pname, mname))
792 result = TRUE;
794 if (!result) {
795 lastsep = strrchr (mname, '/');
796 if (lastsep)
797 if (!strcmp (lastsep+1, pname))
798 result = TRUE;
799 if (!result) {
800 lastsep2 = strrchr (pname, '/');
801 if (lastsep2){
802 if (lastsep) {
803 if (!strcmp (lastsep+1, lastsep2+1))
804 result = TRUE;
805 } else {
806 if (!strcmp (mname, lastsep2+1))
807 result = TRUE;
813 g_free (pname);
814 g_free (mname);
816 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: result is %" G_GINT32_FORMAT, __func__, result);
817 return result;
820 gboolean
821 mono_w32process_try_get_modules (gpointer handle, gpointer *modules, guint32 size, guint32 *needed)
823 MonoW32Handle *handle_data;
824 MonoW32HandleProcess *process_handle;
825 GSList *mods = NULL, *mods_iter;
826 MonoW32ProcessModule *module;
827 guint32 count, avail = size / sizeof(gpointer);
828 int i;
829 pid_t pid;
830 char *pname = NULL;
832 /* Store modules in an array of pointers (main module as
833 * modules[0]), using the load address for each module as a
834 * token. (Use 'NULL' as an alternative for the main module
835 * so that the simple implementation can just return one item
836 * for now.) Get the info from /proc/<pid>/maps on linux,
837 * /proc/<pid>/map on FreeBSD, other systems will have to
838 * implement /dev/kmem reading or whatever other horrid
839 * technique is needed.
841 if (size < sizeof(gpointer))
842 return FALSE;
844 if (!mono_w32handle_lookup_and_ref (handle, &handle_data)) {
845 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: unknown handle %p", __func__, handle);
846 mono_w32error_set_last (ERROR_INVALID_HANDLE);
847 return FALSE;
850 if (handle_data->type != MONO_W32TYPE_PROCESS) {
851 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: unknown process handle %p", __func__, handle);
852 mono_w32error_set_last (ERROR_INVALID_HANDLE);
853 mono_w32handle_unref (handle_data);
854 return FALSE;
857 process_handle = (MonoW32HandleProcess*) handle_data->specific;
859 pid = process_handle->pid;
860 pname = g_strdup (process_handle->pname);
862 if (!pname) {
863 modules[0] = NULL;
864 *needed = sizeof(gpointer);
865 mono_w32handle_unref (handle_data);
866 return TRUE;
869 mods = mono_w32process_get_modules (pid);
870 if (!mods) {
871 modules[0] = NULL;
872 *needed = sizeof(gpointer);
873 g_free (pname);
874 mono_w32handle_unref (handle_data);
875 return TRUE;
878 count = 0;
881 * Use the NULL shortcut, as the first line in
882 * /proc/<pid>/maps isn't the executable, and we need
883 * that first in the returned list. Check the module name
884 * to see if it ends with the proc name and substitute
885 * the first entry with it. FIXME if this turns out to
886 * be a problem.
888 modules[0] = NULL;
889 mods_iter = mods;
890 for (i = 0; mods_iter; i++) {
891 if (i < avail - 1) {
892 module = (MonoW32ProcessModule *)mods_iter->data;
893 if (modules[0] != NULL)
894 modules[i] = module->address_start;
895 else if (match_procname_to_modulename (pname, module->filename))
896 modules[0] = module->address_start;
897 else
898 modules[i + 1] = module->address_start;
900 mono_w32process_module_free ((MonoW32ProcessModule *)mods_iter->data);
901 mods_iter = g_slist_next (mods_iter);
902 count++;
905 /* count + 1 to leave slot 0 for the main module */
906 *needed = sizeof(gpointer) * (count + 1);
908 g_slist_free (mods);
909 g_free (pname);
910 mono_w32handle_unref (handle_data);
911 return TRUE;
914 guint32
915 mono_w32process_module_get_filename (gpointer handle, gpointer module, gunichar2 *basename, guint32 size)
917 gint pid, len;
918 gsize bytes;
919 gchar *path;
920 gunichar2 *proc_path;
922 size *= sizeof (gunichar2); /* adjust for unicode characters */
924 if (basename == NULL || size == 0)
925 return 0;
927 pid = mono_w32process_get_pid (handle);
929 path = mono_w32process_get_path (pid);
930 if (path == NULL)
931 return 0;
933 proc_path = mono_unicode_from_external (path, &bytes);
934 g_free (path);
936 if (proc_path == NULL)
937 return 0;
939 len = (bytes / 2);
941 /* Add the terminator */
942 bytes += 2;
944 if (size < bytes) {
945 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: Size %" G_GUINT32_FORMAT " smaller than needed (%zd); truncating", __func__, size, bytes);
946 memcpy (basename, proc_path, size);
947 } else {
948 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: Size %" G_GUINT32_FORMAT " larger than needed (%zd)", __func__, size, bytes);
949 memcpy (basename, proc_path, bytes);
952 g_free (proc_path);
954 return len;
957 guint32
958 mono_w32process_module_get_name (gpointer handle, gpointer module, gunichar2 *basename, guint32 size)
960 MonoW32Handle *handle_data;
961 MonoW32HandleProcess *process_handle;
962 pid_t pid;
963 gunichar2 *procname;
964 char *procname_ext = NULL;
965 glong len;
966 gsize bytes;
967 GSList *mods = NULL, *mods_iter;
968 MonoW32ProcessModule *found_module;
969 char *pname = NULL;
971 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: Getting module base name, process handle %p module %p basename %p size %" G_GUINT32_FORMAT,
972 __func__, handle, module, basename, size);
974 size = size * sizeof (gunichar2); /* adjust for unicode characters */
976 if (basename == NULL || size == 0)
977 return 0;
979 if (!mono_w32handle_lookup_and_ref (handle, &handle_data)) {
980 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: unknown handle %p", __func__, handle);
981 mono_w32error_set_last (ERROR_INVALID_HANDLE);
982 return 0;
985 if (handle_data->type != MONO_W32TYPE_PROCESS) {
986 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: unknown process handle %p", __func__, handle);
987 mono_w32error_set_last (ERROR_INVALID_HANDLE);
988 mono_w32handle_unref (handle_data);
989 return 0;
992 process_handle = (MonoW32HandleProcess*) handle_data->specific;
994 pid = process_handle->pid;
995 pname = g_strdup (process_handle->pname);
997 mods = mono_w32process_get_modules (pid);
998 if (!mods && module != NULL) {
999 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: Can't get modules %p", __func__, handle);
1000 g_free (pname);
1001 mono_w32handle_unref (handle_data);
1002 return 0;
1005 /* If module != NULL compare the address.
1006 * If module == NULL we are looking for the main module.
1007 * The best we can do for now check it the module name end with the process name.
1009 for (mods_iter = mods; mods_iter; mods_iter = g_slist_next (mods_iter)) {
1010 found_module = (MonoW32ProcessModule *)mods_iter->data;
1011 if (procname_ext == NULL &&
1012 ((module == NULL && match_procname_to_modulename (pname, found_module->filename)) ||
1013 (module != NULL && found_module->address_start == module))) {
1014 procname_ext = g_path_get_basename (found_module->filename);
1017 mono_w32process_module_free (found_module);
1020 if (procname_ext == NULL) {
1021 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: Can't find procname_ext from procmods %p", __func__, handle);
1022 /* If it's *still* null, we might have hit the
1023 * case where reading /proc/$pid/maps gives an
1024 * empty file for this user.
1026 procname_ext = mono_w32process_get_name (pid);
1027 if (!procname_ext)
1028 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: Can't find procname_ext from proc_get_name %p pid %d", __func__, handle, pid);
1031 g_slist_free (mods);
1032 g_free (pname);
1034 if (procname_ext) {
1035 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: Process name is [%s]", __func__,
1036 procname_ext);
1038 procname = mono_unicode_from_external (procname_ext, &bytes);
1039 if (procname == NULL) {
1040 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: Can't get procname %p", __func__, handle);
1041 /* bugger */
1042 g_free (procname_ext);
1043 mono_w32handle_unref (handle_data);
1044 return 0;
1047 len = (bytes / 2);
1049 /* Add the terminator */
1050 bytes += 2;
1052 if (size < bytes) {
1053 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: Size %" G_GUINT32_FORMAT " smaller than needed (%zd); truncating", __func__, size, bytes);
1055 memcpy (basename, procname, size);
1056 } else {
1057 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: Size %" G_GUINT32_FORMAT " larger than needed (%zd)",
1058 __func__, size, bytes);
1060 memcpy (basename, procname, bytes);
1063 g_free (procname);
1064 g_free (procname_ext);
1066 mono_w32handle_unref (handle_data);
1067 return len;
1070 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: Can't find procname_ext %p", __func__, handle);
1071 mono_w32handle_unref (handle_data);
1072 return 0;
1075 gboolean
1076 mono_w32process_module_get_information (gpointer handle, gpointer module, MODULEINFO *modinfo, guint32 size)
1078 MonoW32Handle *handle_data;
1079 MonoW32HandleProcess *process_handle;
1080 pid_t pid;
1081 GSList *mods = NULL, *mods_iter;
1082 MonoW32ProcessModule *found_module;
1083 gboolean ret = FALSE;
1084 char *pname = NULL;
1086 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: Getting module info, process handle %p module %p",
1087 __func__, handle, module);
1089 if (modinfo == NULL || size < sizeof (MODULEINFO))
1090 return FALSE;
1092 if (!mono_w32handle_lookup_and_ref (handle, &handle_data)) {
1093 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: unknown handle %p", __func__, handle);
1094 mono_w32error_set_last (ERROR_INVALID_HANDLE);
1095 return FALSE;
1098 if (handle_data->type != MONO_W32TYPE_PROCESS) {
1099 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: unknown process handle %p", __func__, handle);
1100 mono_w32error_set_last (ERROR_INVALID_HANDLE);
1101 mono_w32handle_unref (handle_data);
1102 return FALSE;
1105 process_handle = (MonoW32HandleProcess*) handle_data->specific;
1107 pid = process_handle->pid;
1108 pname = g_strdup (process_handle->pname);
1110 mods = mono_w32process_get_modules (pid);
1111 if (!mods) {
1112 g_free (pname);
1113 mono_w32handle_unref (handle_data);
1114 return FALSE;
1117 /* If module != NULL compare the address.
1118 * If module == NULL we are looking for the main module.
1119 * The best we can do for now check it the module name end with the process name.
1121 for (mods_iter = mods; mods_iter; mods_iter = g_slist_next (mods_iter)) {
1122 found_module = (MonoW32ProcessModule *)mods_iter->data;
1123 if (ret == FALSE &&
1124 ((module == NULL && match_procname_to_modulename (pname, found_module->filename)) ||
1125 (module != NULL && found_module->address_start == module))) {
1126 modinfo->lpBaseOfDll = found_module->address_start;
1127 modinfo->SizeOfImage = (gsize)(found_module->address_end) - (gsize)(found_module->address_start);
1128 modinfo->EntryPoint = found_module->address_offset;
1129 ret = TRUE;
1132 mono_w32process_module_free (found_module);
1135 g_slist_free (mods);
1136 g_free (pname);
1137 mono_w32handle_unref (handle_data);
1138 return ret;
1141 static void
1142 switch_dir_separators (char *path)
1144 size_t i, pathLength = strlen(path);
1146 /* Turn all the slashes round the right way, except for \' */
1147 /* There are probably other characters that need to be excluded as well. */
1148 for (i = 0; i < pathLength; i++) {
1149 if (path[i] == '\\' && i < pathLength - 1 && path[i+1] != '\'' )
1150 path[i] = '/';
1154 #if HAVE_SIGACTION
1156 MONO_SIGNAL_HANDLER_FUNC (static, mono_sigchld_signal_handler, (int _dummy, siginfo_t *info, void *context))
1159 * Don't want to do any complicated processing here so just wake up the finalizer thread which will call
1160 * mono_w32process_signal_finished ().
1162 int old_errno = errno;
1164 mono_gc_finalize_notify ();
1166 mono_set_errno (old_errno);
1169 static void
1170 process_add_sigchld_handler (void)
1172 struct sigaction sa;
1174 sa.sa_sigaction = mono_sigchld_signal_handler;
1175 sigemptyset (&sa.sa_mask);
1176 sa.sa_flags = SA_NOCLDSTOP | SA_SIGINFO | SA_RESTART;
1177 g_assert (sigaction (SIGCHLD, &sa, NULL) != -1);
1178 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "Added SIGCHLD handler");
1181 #endif
1184 * mono_w32process_signal_finished:
1186 * Signal the exit semaphore for processes which have finished.
1188 void
1189 mono_w32process_signal_finished (void)
1191 mono_coop_mutex_lock (&processes_mutex);
1193 for (Process* process = processes; process; process = process->next) {
1194 int status = -1;
1195 int pid;
1197 do {
1198 pid = waitpid (process->pid, &status, WNOHANG);
1199 } while (pid == -1 && errno == EINTR);
1201 // possible values of 'pid':
1202 // process->pid : the status changed for this child
1203 // 0 : status unchanged for this PID
1204 // ECHILD : process has been reaped elsewhere (or never existed)
1205 // EINVAL : invalid PID or other argument
1207 // Therefore, we ignore status unchanged (nothing to do) and error
1208 // events (process is cleaned up later).
1209 if (pid <= 0)
1210 continue;
1211 if (process->signalled)
1212 continue;
1214 process->signalled = TRUE;
1215 process->status = status;
1216 mono_coop_sem_post (&process->exit_sem);
1219 mono_coop_mutex_unlock (&processes_mutex);
1222 static gboolean
1223 is_readable_or_executable (const char *prog)
1225 struct stat buf;
1226 int a = access (prog, R_OK);
1227 int b = access (prog, X_OK);
1228 if (a != 0 && b != 0)
1229 return FALSE;
1230 if (stat (prog, &buf))
1231 return FALSE;
1232 if (S_ISREG (buf.st_mode))
1233 return TRUE;
1234 return FALSE;
1237 static gboolean
1238 is_executable (const char *prog)
1240 struct stat buf;
1241 if (access (prog, X_OK) != 0)
1242 return FALSE;
1243 if (stat (prog, &buf))
1244 return FALSE;
1245 if (S_ISREG (buf.st_mode))
1246 return TRUE;
1247 return FALSE;
1250 static gboolean
1251 is_managed_binary (const char *filename)
1253 int original_errno = errno;
1254 #if defined(HAVE_LARGE_FILE_SUPPORT) && defined(O_LARGEFILE)
1255 int file = open (filename, O_RDONLY | O_LARGEFILE);
1256 #else
1257 int file = open (filename, O_RDONLY);
1258 #endif
1259 off_t new_offset;
1260 unsigned char buffer[8];
1261 off_t file_size, optional_header_offset;
1262 off_t pe_header_offset, clr_header_offset;
1263 gboolean managed = FALSE;
1264 int num_read;
1265 guint32 first_word, second_word, magic_number;
1267 /* If we are unable to open the file, then we definitely
1268 * can't say that it is managed. The child mono process
1269 * probably wouldn't be able to open it anyway.
1271 if (file < 0) {
1272 mono_set_errno (original_errno);
1273 return FALSE;
1276 /* Retrieve the length of the file for future sanity checks. */
1277 file_size = lseek (file, 0, SEEK_END);
1278 lseek (file, 0, SEEK_SET);
1280 /* We know we need to read a header field at offset 60. */
1281 if (file_size < 64)
1282 goto leave;
1284 num_read = read (file, buffer, 2);
1286 if ((num_read != 2) || (buffer[0] != 'M') || (buffer[1] != 'Z'))
1287 goto leave;
1289 new_offset = lseek (file, 60, SEEK_SET);
1291 if (new_offset != 60)
1292 goto leave;
1294 num_read = read (file, buffer, 4);
1296 if (num_read != 4)
1297 goto leave;
1298 pe_header_offset = buffer[0]
1299 | (buffer[1] << 8)
1300 | (buffer[2] << 16)
1301 | (buffer[3] << 24);
1303 if (pe_header_offset + 24 > file_size)
1304 goto leave;
1306 new_offset = lseek (file, pe_header_offset, SEEK_SET);
1308 if (new_offset != pe_header_offset)
1309 goto leave;
1311 num_read = read (file, buffer, 4);
1313 if ((num_read != 4) || (buffer[0] != 'P') || (buffer[1] != 'E') || (buffer[2] != 0) || (buffer[3] != 0))
1314 goto leave;
1317 * Verify that the header we want in the optional header data
1318 * is present in this binary.
1320 new_offset = lseek (file, pe_header_offset + 20, SEEK_SET);
1322 if (new_offset != pe_header_offset + 20)
1323 goto leave;
1325 num_read = read (file, buffer, 2);
1327 if ((num_read != 2) || ((buffer[0] | (buffer[1] << 8)) < 216))
1328 goto leave;
1330 optional_header_offset = pe_header_offset + 24;
1332 /* Read the PE magic number */
1333 new_offset = lseek (file, optional_header_offset, SEEK_SET);
1335 if (new_offset != optional_header_offset)
1336 goto leave;
1338 num_read = read (file, buffer, 2);
1340 if (num_read != 2)
1341 goto leave;
1343 magic_number = (buffer[0] | (buffer[1] << 8));
1345 if (magic_number == 0x10B) // PE32
1346 clr_header_offset = 208;
1347 else if (magic_number == 0x20B) // PE32+
1348 clr_header_offset = 224;
1349 else
1350 goto leave;
1352 /* Read the CLR header address and size fields. These will be
1353 * zero if the binary is not managed.
1355 new_offset = lseek (file, optional_header_offset + clr_header_offset, SEEK_SET);
1357 if (new_offset != optional_header_offset + clr_header_offset)
1358 goto leave;
1360 num_read = read (file, buffer, 8);
1362 /* We are not concerned with endianness, only with
1363 * whether it is zero or not.
1365 first_word = *(guint32 *)&buffer[0];
1366 second_word = *(guint32 *)&buffer[4];
1368 if ((num_read != 8) || (first_word == 0) || (second_word == 0))
1369 goto leave;
1371 managed = TRUE;
1373 leave:
1374 close (file);
1375 mono_set_errno (original_errno);
1376 return managed;
1380 * Gets the biggest numbered file descriptor for the current process; failing
1381 * that, the system's file descriptor limit. This is called by the fork child
1382 * in close_my_fds.
1384 static inline guint32
1385 max_fd_count (void)
1387 #if defined (_AIX)
1388 struct procentry64 pe;
1389 pid_t p;
1390 p = getpid ();
1391 if (getprocs64 (&pe, sizeof (pe), NULL, 0, &p, 1) != -1) {
1392 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS,
1393 "%s: maximum returned fd in child is %u",
1394 __func__, pe.pi_maxofile);
1395 return pe.pi_maxofile; // biggest + 1
1397 #endif
1398 // fallback to user/system limit if unsupported/error
1399 return eg_getdtablesize ();
1403 * Closes all of the process' opened file descriptors, applying a strategy
1404 * appropriate for the target system. This is called by the fork child in
1405 * process_create.
1407 static void
1408 close_my_fds (void)
1410 // TODO: Other platforms.
1411 // * On macOS, use proc_pidinfo + PROC_PIDLISTFDS? See:
1412 // http://blog.palominolabs.com/2012/06/19/getting-the-files-being-used-by-a-process-on-mac-os-x/
1413 // (I have no idea how this plays out on i/watch/tvOS.)
1414 // * On the other BSDs, there's likely a sysctl for this.
1415 // * On Solaris, there exists posix_spawn_file_actions_addclosefrom_np,
1416 // but that assumes we're using posix_spawn; we aren't, as we do some
1417 // complex stuff between fork and exec. There's likely a way to get
1418 // the FD list/count though (maybe look at addclosefrom source in
1419 // illumos?) or just walk /proc/pid/fd like Linux?
1420 #if defined (__linux__)
1421 /* Walk the file descriptors in /proc/self/fd/. Linux has no other API,
1422 * as far as I'm aware. Opening a directory won't create an FD. */
1423 struct dirent *dp;
1424 DIR *d;
1425 int fd;
1426 d = opendir ("/proc/self/fd/");
1427 if (d) {
1428 while ((dp = readdir (d)) != NULL) {
1429 if (dp->d_name [0] == '.')
1430 continue;
1431 fd = atoi (dp->d_name);
1432 if (fd > 2)
1433 close (fd);
1435 closedir (d);
1436 return;
1437 } else {
1438 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS,
1439 "%s: opening fd dir failed, using fallback",
1440 __func__);
1442 #elif defined (__FreeBSD__)
1443 /* FreeBSD lets us get a list of FDs. There's a MIB to access them
1444 * directly, but it uses a lot of nasty variable length structures. The
1445 * system library libutil provides a nicer way to get a fixed length
1446 * version instead. */
1447 struct kinfo_file *kif;
1448 int count, i;
1449 /* this is malloced but we won't need to free once we exec/exit */
1450 kif = kinfo_getfile (getpid (), &count);
1451 if (kif) {
1452 for (i = 0; i < count; i++) {
1453 /* negative FDs look to be used by the OS */
1454 if (kif [i].kf_fd > 2) /* no neg + no stdio */
1455 close (kif [i].kf_fd);
1457 return;
1458 } else {
1459 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS,
1460 "%s: kinfo_getfile failed, using fallback",
1461 __func__);
1463 #elif defined (_AIX)
1464 struct procentry64 pe;
1465 /* this array struct is 1 MB, we're NOT putting it on the stack.
1466 * likewise no need to free; getprocs will fail if we use the smalller
1467 * versions if we have a lot of FDs (is it worth it?)
1469 struct fdsinfo_100K *fds;
1470 pid_t p;
1471 p = getpid ();
1472 fds = (struct fdsinfo_100K *) g_malloc0 (sizeof (struct fdsinfo_100K));
1473 if (!fds) {
1474 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS,
1475 "%s: fdsinfo alloc failed, using fallback",
1476 __func__);
1477 goto fallback;
1480 if (getprocs64 (&pe, sizeof (pe), fds, sizeof (struct fdsinfo_100K), &p, 1) != -1) {
1481 for (int i = 3; i < pe.pi_maxofile; i++) {
1482 if (fds->pi_ufd [i].fp != 0)
1483 close (fds->pi_ufd [i].fp);
1485 return;
1486 } else {
1487 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS,
1488 "%s: getprocs64 failed, using fallback",
1489 __func__);
1491 fallback:
1492 #endif
1493 /* Fallback: Close FDs blindly, according to an FD limit */
1494 for (guint32 i = max_fd_count () - 1; i > 2; i--)
1495 close (i);
1498 static gboolean
1499 process_create (const gunichar2 *appname, const gunichar2 *cmdline,
1500 const gunichar2 *cwd, StartupHandles *startup_handles, MonoW32ProcessInfo *process_info)
1502 #if defined (HAVE_FORK) && defined (HAVE_EXECVE)
1503 char *cmd = NULL, *prog = NULL, *full_prog = NULL, *args = NULL, *args_after_prog = NULL;
1504 char *dir = NULL, **env_strings = NULL, **argv = NULL;
1505 guint32 i;
1506 gboolean ret = FALSE;
1507 gpointer handle = NULL;
1508 GError *gerr = NULL;
1509 int in_fd, out_fd, err_fd;
1510 pid_t pid = 0;
1511 int startup_pipe [2] = {-1, -1};
1512 int dummy;
1513 Process *process;
1514 ERROR_DECL (error);
1516 #if HAVE_SIGACTION
1517 mono_lazy_initialize (&process_sig_chld_once, process_add_sigchld_handler);
1518 #endif
1520 /* appname and cmdline specify the executable and its args:
1522 * If appname is not NULL, it is the name of the executable.
1523 * Otherwise the executable is the first token in cmdline.
1525 * Executable searching:
1527 * If appname is not NULL, it can specify the full path and
1528 * file name, or else a partial name and the current directory
1529 * will be used. There is no additional searching.
1531 * If appname is NULL, the first whitespace-delimited token in
1532 * cmdline is used. If the name does not contain a full
1533 * directory path, the search sequence is:
1535 * 1) The directory containing the current process
1536 * 2) The current working directory
1537 * 3) The windows system directory (Ignored)
1538 * 4) The windows directory (Ignored)
1539 * 5) $PATH
1541 * Just to make things more interesting, tokens can contain
1542 * white space if they are surrounded by quotation marks. I'm
1543 * beginning to understand just why windows apps are generally
1544 * so crap, with an API like this :-(
1546 if (appname != NULL) {
1547 cmd = mono_unicode_to_external_checked (appname, error);
1548 if (cmd == NULL) {
1549 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: unicode conversion returned NULL; %s",
1550 __func__, mono_error_get_message (error));
1552 mono_error_cleanup (error);
1553 mono_w32error_set_last (ERROR_PATH_NOT_FOUND);
1554 goto free_strings;
1557 switch_dir_separators(cmd);
1560 if (cmdline != NULL) {
1561 args = mono_unicode_to_external_checked (cmdline, error);
1562 if (args == NULL) {
1563 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: unicode conversion returned NULL; %s", __func__, mono_error_get_message (error));
1565 mono_error_cleanup (error);
1566 mono_w32error_set_last (ERROR_PATH_NOT_FOUND);
1567 goto free_strings;
1571 if (cwd != NULL) {
1572 dir = mono_unicode_to_external_checked (cwd, error);
1573 if (dir == NULL) {
1574 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: unicode conversion returned NULL; %s", __func__, mono_error_get_message (error));
1576 mono_error_cleanup (error);
1577 mono_w32error_set_last (ERROR_PATH_NOT_FOUND);
1578 goto free_strings;
1581 /* Turn all the slashes round the right way */
1582 switch_dir_separators(dir);
1586 /* We can't put off locating the executable any longer :-( */
1587 if (cmd != NULL) {
1588 char *unquoted;
1589 if (g_ascii_isalpha (cmd[0]) && (cmd[1] == ':')) {
1590 /* Strip off the drive letter. I can't
1591 * believe that CP/M holdover is still
1592 * visible...
1594 g_memmove (cmd, cmd+2, strlen (cmd)-2);
1595 cmd[strlen (cmd)-2] = '\0';
1598 unquoted = g_shell_unquote (cmd, NULL);
1599 if (unquoted[0] == '/') {
1600 /* Assume full path given */
1601 prog = g_strdup (unquoted);
1603 /* Executable existing ? */
1604 if (!is_readable_or_executable (prog)) {
1605 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: Couldn't find executable %s",
1606 __func__, prog);
1607 g_free (unquoted);
1608 mono_w32error_set_last (ERROR_FILE_NOT_FOUND);
1609 goto free_strings;
1611 } else {
1612 /* Search for file named by cmd in the current
1613 * directory
1615 char *curdir = g_get_current_dir ();
1617 prog = g_strdup_printf ("%s/%s", curdir, unquoted);
1618 g_free (curdir);
1620 /* And make sure it's readable */
1621 if (!is_readable_or_executable (prog)) {
1622 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: Couldn't find executable %s",
1623 __func__, prog);
1624 g_free (unquoted);
1625 mono_w32error_set_last (ERROR_FILE_NOT_FOUND);
1626 goto free_strings;
1629 g_free (unquoted);
1631 args_after_prog = args;
1632 } else {
1633 char *token = NULL;
1634 char quote;
1636 /* Dig out the first token from args, taking quotation
1637 * marks into account
1640 /* First, strip off all leading whitespace */
1641 args = g_strchug (args);
1643 /* args_after_prog points to the contents of args
1644 * after token has been set (otherwise argv[0] is
1645 * duplicated)
1647 args_after_prog = args;
1649 /* Assume the opening quote will always be the first
1650 * character
1652 if (args[0] == '\"' || args [0] == '\'') {
1653 quote = args [0];
1654 for (i = 1; args[i] != '\0' && args[i] != quote; i++);
1655 if (args [i + 1] == '\0' || g_ascii_isspace (args[i+1])) {
1656 /* We found the first token */
1657 token = g_strndup (args+1, i-1);
1658 args_after_prog = g_strchug (args + i + 1);
1659 } else {
1660 /* Quotation mark appeared in the
1661 * middle of the token. Just give the
1662 * whole first token, quotes and all,
1663 * to exec.
1668 if (token == NULL) {
1669 /* No quote mark, or malformed */
1670 for (i = 0; args[i] != '\0'; i++) {
1671 if (g_ascii_isspace (args[i])) {
1672 token = g_strndup (args, i);
1673 args_after_prog = args + i + 1;
1674 break;
1679 if (token == NULL && args[0] != '\0') {
1680 /* Must be just one token in the string */
1681 token = g_strdup (args);
1682 args_after_prog = NULL;
1685 if (token == NULL) {
1686 /* Give up */
1687 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: Couldn't find what to exec", __func__);
1689 mono_w32error_set_last (ERROR_PATH_NOT_FOUND);
1690 goto free_strings;
1693 /* Turn all the slashes round the right way. Only for
1694 * the prg. name
1696 switch_dir_separators(token);
1698 if (g_ascii_isalpha (token[0]) && (token[1] == ':')) {
1699 /* Strip off the drive letter. I can't
1700 * believe that CP/M holdover is still
1701 * visible...
1703 g_memmove (token, token+2, strlen (token)-2);
1704 token[strlen (token)-2] = '\0';
1707 if (token[0] == '/') {
1708 /* Assume full path given */
1709 prog = g_strdup (token);
1711 /* Executable existing ? */
1712 if (!is_readable_or_executable (prog)) {
1713 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: Couldn't find executable %s",
1714 __func__, token);
1715 g_free (token);
1716 mono_w32error_set_last (ERROR_FILE_NOT_FOUND);
1717 goto free_strings;
1719 } else {
1720 char *curdir = g_get_current_dir ();
1722 /* FIXME: Need to record the directory
1723 * containing the current process, and check
1724 * that for the new executable as the first
1725 * place to look
1728 prog = g_strdup_printf ("%s/%s", curdir, token);
1729 g_free (curdir);
1731 /* I assume X_OK is the criterion to use,
1732 * rather than F_OK
1734 * X_OK is too strict *if* the target is a CLR binary
1736 if (!is_readable_or_executable (prog)) {
1737 g_free (prog);
1738 prog = g_find_program_in_path (token);
1739 if (prog == NULL) {
1740 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: Couldn't find executable %s", __func__, token);
1742 g_free (token);
1743 mono_w32error_set_last (ERROR_FILE_NOT_FOUND);
1744 goto free_strings;
1749 g_free (token);
1752 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: Exec prog [%s] args [%s]",
1753 __func__, prog, args_after_prog);
1755 /* Check for CLR binaries; if found, we will try to invoke
1756 * them using the same mono binary that started us.
1758 if (is_managed_binary (prog)) {
1759 gunichar2 *newapp, *newcmd;
1760 gsize bytes_ignored;
1762 newapp = mono_unicode_from_external (cli_launcher ? cli_launcher : "mono", &bytes_ignored);
1763 if (newapp) {
1764 if (appname)
1765 newcmd = utf16_concat (utf16_quote, newapp, utf16_quote, utf16_space, appname, utf16_space, cmdline, (const gunichar2 *)NULL);
1766 else
1767 newcmd = utf16_concat (utf16_quote, newapp, utf16_quote, utf16_space, cmdline, (const gunichar2 *)NULL);
1769 g_free (newapp);
1771 if (newcmd) {
1772 ret = process_create (NULL, newcmd, cwd, startup_handles, process_info);
1774 g_free (newcmd);
1776 goto free_strings;
1779 } else {
1780 if (!is_executable (prog)) {
1781 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: Executable permisson not set on %s", __func__, prog);
1782 mono_w32error_set_last (ERROR_ACCESS_DENIED);
1783 goto free_strings;
1787 if (args_after_prog != NULL && *args_after_prog) {
1788 char *qprog;
1790 qprog = g_shell_quote (prog);
1791 full_prog = g_strconcat (qprog, " ", args_after_prog, NULL);
1792 g_free (qprog);
1793 } else {
1794 full_prog = g_shell_quote (prog);
1797 ret = g_shell_parse_argv (full_prog, NULL, &argv, &gerr);
1798 if (ret == FALSE) {
1799 g_message ("process_create: %s\n", gerr->message);
1800 g_error_free (gerr);
1801 gerr = NULL;
1802 goto free_strings;
1805 if (startup_handles) {
1806 in_fd = GPOINTER_TO_UINT (startup_handles->input);
1807 out_fd = GPOINTER_TO_UINT (startup_handles->output);
1808 err_fd = GPOINTER_TO_UINT (startup_handles->error);
1809 } else {
1810 in_fd = GPOINTER_TO_UINT (mono_w32file_get_console_input ());
1811 out_fd = GPOINTER_TO_UINT (mono_w32file_get_console_output ());
1812 err_fd = GPOINTER_TO_UINT (mono_w32file_get_console_error ());
1816 * process->env_variables is a an array of MonoString*
1818 * If new_environ is not NULL it specifies the entire set of
1819 * environment variables in the new process. Otherwise the
1820 * new process inherits the same environment.
1822 if (process_info->env_variables) {
1823 MonoArrayHandle array = MONO_HANDLE_NEW (MonoArray, process_info->env_variables);
1824 MonoStringHandle var = MONO_HANDLE_NEW (MonoString, NULL);
1825 gsize const array_length = mono_array_handle_length (array);
1827 /* +2: one for the process handle value, and the last one is NULL */
1828 // What "process handle value"?
1829 env_strings = g_new0 (gchar*, array_length + 2);
1831 /* Copy each environ string into 'strings' turning it into utf8 (or the requested encoding) at the same time */
1832 for (gsize i = 0; i < array_length; ++i) {
1833 MONO_HANDLE_ARRAY_GETREF (var, array, i);
1834 gchandle_t gchandle = 0;
1835 env_strings [i] = mono_unicode_to_external (mono_string_handle_pin_chars (var, &gchandle));
1836 mono_gchandle_free_internal (gchandle);
1838 } else {
1839 gsize env_count = 0;
1840 for (i = 0; environ[i] != NULL; i++)
1841 env_count++;
1843 /* +2: one for the process handle value, and the last one is NULL */
1844 // What "process handle value"?
1845 env_strings = g_new0 (gchar*, env_count + 2);
1847 /* Copy each environ string into 'strings' turning it into utf8 (or the requested encoding) at the same time */
1848 for (i = 0; i < env_count; i++)
1849 env_strings [i] = g_strdup (environ[i]);
1852 /* Create a pipe to make sure the child doesn't exit before
1853 * we can add the process to the linked list of processes */
1854 if (pipe (startup_pipe) == -1) {
1855 /* Could not create the pipe to synchroniz process startup. We'll just not synchronize.
1856 * This is just for a very hard to hit race condition in the first place */
1857 startup_pipe [0] = startup_pipe [1] = -1;
1858 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: new process startup not synchronized. We may not notice if the newly created process exits immediately.", __func__);
1861 switch (pid = fork ()) {
1862 case -1: /* Error */ {
1863 mono_w32error_set_last (ERROR_OUTOFMEMORY);
1864 ret = FALSE;
1865 break;
1867 case 0: /* Child */ {
1868 if (startup_pipe [0] != -1) {
1869 /* Wait until the parent has updated it's internal data */
1870 ssize_t _i G_GNUC_UNUSED = read (startup_pipe [0], &dummy, 1);
1871 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: child: parent has completed its setup", __func__);
1872 close (startup_pipe [0]);
1873 close (startup_pipe [1]);
1876 /* should we detach from the process group? */
1878 /* Connect stdin, stdout and stderr */
1879 dup2 (in_fd, 0);
1880 dup2 (out_fd, 1);
1881 dup2 (err_fd, 2);
1883 /* Close this child's file handles. */
1884 close_my_fds ();
1886 #ifdef DEBUG_ENABLED
1887 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: exec()ing [%s] in dir [%s]", __func__, cmd,
1888 dir == NULL?".":dir);
1889 for (i = 0; argv[i] != NULL; i++)
1890 g_message ("arg %" G_GUINT32_FORMAT ": [%s]", i, argv[i]);
1892 for (i = 0; env_strings[i] != NULL; i++)
1893 g_message ("env %" G_GUINT32_FORMAT ": [%s]", i, env_strings[i]);
1894 #endif
1896 /* set cwd */
1897 if (dir != NULL && chdir (dir) == -1) {
1898 /* set error */
1899 _exit (-1);
1902 /* exec */
1903 execve (argv[0], argv, env_strings);
1905 /* set error */
1906 _exit (-1);
1908 break;
1910 default: /* Parent */ {
1911 MonoW32Handle *handle_data;
1912 MonoW32HandleProcess process_handle;
1914 memset (&process_handle, 0, sizeof (process_handle));
1915 process_handle.pid = pid;
1916 process_handle.child = TRUE;
1917 process_handle.pname = g_strdup (prog);
1918 process_set_defaults (&process_handle);
1920 /* Add our process into the linked list of processes */
1921 process = (Process *) g_malloc0 (sizeof (Process));
1922 process->pid = pid;
1923 process->handle_count = 1;
1924 mono_coop_sem_init (&process->exit_sem, 0);
1926 process_handle.process = process;
1928 handle = mono_w32handle_new (MONO_W32TYPE_PROCESS, &process_handle);
1929 if (handle == INVALID_HANDLE_VALUE) {
1930 g_warning ("%s: error creating process handle", __func__);
1932 mono_coop_sem_destroy (&process->exit_sem);
1933 g_free (process);
1935 mono_w32error_set_last (ERROR_OUTOFMEMORY);
1936 ret = FALSE;
1937 break;
1940 if (!mono_w32handle_lookup_and_ref (handle, &handle_data))
1941 g_error ("%s: unknown handle %p", __func__, handle);
1943 if (handle_data->type != MONO_W32TYPE_PROCESS)
1944 g_error ("%s: unknown process handle %p", __func__, handle);
1946 /* Keep the process handle artificially alive until the process
1947 * exits so that the information in the handle isn't lost. */
1948 process->handle = mono_w32handle_duplicate (handle_data);
1950 mono_coop_mutex_lock (&processes_mutex);
1951 process->next = processes;
1952 mono_memory_barrier ();
1953 processes = process;
1954 mono_coop_mutex_unlock (&processes_mutex);
1956 if (process_info != NULL) {
1957 process_info->process_handle = handle;
1958 process_info->pid = pid;
1961 mono_w32handle_unref (handle_data);
1963 break;
1967 if (startup_pipe [1] != -1) {
1968 /* Write 1 byte, doesn't matter what */
1969 ssize_t _i G_GNUC_UNUSED = write (startup_pipe [1], startup_pipe, 1);
1970 close (startup_pipe [0]);
1971 close (startup_pipe [1]);
1974 free_strings:
1975 g_free (cmd);
1976 g_free (full_prog);
1977 g_free (prog);
1978 g_free (args);
1979 g_free (dir);
1980 g_strfreev (env_strings);
1981 g_strfreev (argv);
1983 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: returning handle %p for pid %d", __func__, handle, pid);
1985 /* Check if something needs to be cleaned up. */
1986 processes_cleanup ();
1988 return ret;
1989 #else
1990 mono_w32error_set_last (ERROR_NOT_SUPPORTED);
1991 return FALSE;
1992 #endif // defined (HAVE_FORK) && defined (HAVE_EXECVE)
1995 MonoBoolean
1996 ves_icall_System_Diagnostics_Process_ShellExecuteEx_internal (MonoW32ProcessStartInfoHandle proc_start_info, MonoW32ProcessInfo *process_info, MonoError *error)
1998 MonoCreateProcessCoop coop;
1999 mono_createprocess_coop_init (&coop, proc_start_info, process_info);
2001 gboolean ret;
2002 gboolean handler_needswait = FALSE;
2004 if (!coop.filename) {
2005 /* w2k returns TRUE for this, for some reason. */
2006 ret = TRUE;
2007 goto done;
2010 const gunichar2 *lpFile;
2011 lpFile = coop.filename;
2012 const gunichar2 *lpParameters;
2013 lpParameters = coop.arguments;
2014 const gunichar2 *lpDirectory;
2015 lpDirectory = coop.length.working_directory ? coop.working_directory : NULL;
2017 /* Put both executable and parameters into the second argument
2018 * to process_create (), so it searches $PATH. The conversion
2019 * into and back out of utf8 is because there is no
2020 * g_strdup_printf () equivalent for gunichar2 :-(
2022 gunichar2 *args;
2023 args = utf16_concat (utf16_quote, lpFile, utf16_quote, lpParameters ? utf16_space : NULL, lpParameters, (const gunichar2 *)NULL);
2024 if (args == NULL) {
2025 mono_w32error_set_last (ERROR_INVALID_DATA);
2026 ret = FALSE;
2027 goto done;
2029 ret = process_create (NULL, args, lpDirectory, NULL, process_info);
2030 g_free (args);
2032 if (!ret && mono_w32error_get_last () == ERROR_OUTOFMEMORY)
2033 goto done;
2035 if (!ret) {
2037 #if defined(TARGET_IOS) || defined(TARGET_ANDROID)
2038 // don't try the "open" handlers on iOS/Android, they don't exist there anyway
2039 goto done;
2040 #endif
2042 static char *handler;
2043 static gunichar2 *handler_utf16;
2045 if (handler_utf16 == (gunichar2 *)-1) {
2046 ret = FALSE;
2047 goto done;
2050 #ifdef HOST_DARWIN
2051 handler = g_strdup ("/usr/bin/open");
2052 handler_needswait = TRUE;
2053 #else
2055 * On Linux, try: xdg-open, the FreeDesktop standard way of doing it,
2056 * if that fails, try to use gnome-open, then kfmclient
2058 MONO_ENTER_GC_SAFE;
2059 handler = g_find_program_in_path ("xdg-open");
2060 if (handler != NULL)
2061 handler_needswait = TRUE;
2062 else {
2063 handler = g_find_program_in_path ("gnome-open");
2064 if (handler == NULL){
2065 handler = g_find_program_in_path ("kfmclient");
2066 if (handler == NULL){
2067 handler_utf16 = (gunichar2 *) -1;
2068 ret = FALSE;
2069 } else {
2070 /* kfmclient needs exec argument */
2071 char *old = handler;
2072 handler = g_strconcat (old, " exec",
2073 NULL);
2074 g_free (old);
2078 MONO_EXIT_GC_SAFE;
2079 if (ret == FALSE){
2080 goto done;
2082 #endif
2083 handler_utf16 = g_utf8_to_utf16 (handler, -1, NULL, NULL, NULL);
2084 g_free (handler);
2086 /* Put quotes around the filename, in case it's a url
2087 * that contains #'s (process_create() calls
2088 * g_shell_parse_argv(), which deliberately throws
2089 * away anything after an unquoted #). Fixes bug
2090 * 371567.
2092 args = utf16_concat (handler_utf16, utf16_space, utf16_quote, lpFile, utf16_quote,
2093 lpParameters ? utf16_space : NULL, lpParameters, (const gunichar2 *)NULL);
2094 if (args == NULL) {
2095 mono_w32error_set_last (ERROR_INVALID_DATA);
2096 ret = FALSE;
2097 goto done;
2099 ret = process_create (NULL, args, lpDirectory, NULL, process_info);
2100 g_free (args);
2101 if (!ret) {
2102 if (mono_w32error_get_last () != ERROR_OUTOFMEMORY)
2103 mono_w32error_set_last (ERROR_INVALID_DATA);
2104 ret = FALSE;
2105 goto done;
2108 if (handler_needswait) {
2109 gint32 exitcode;
2110 MonoW32HandleWaitRet waitret;
2111 waitret = process_wait ((MonoW32Handle*)process_info->process_handle, MONO_INFINITE_WAIT, NULL);
2112 (void)waitret;
2113 mono_get_exit_code_process (process_info->process_handle, &exitcode);
2114 if (exitcode != 0)
2115 ret = FALSE;
2117 /* Shell exec should not return a process handle when it spawned a GUI thing, like a browser. */
2118 mono_w32handle_close (process_info->process_handle);
2119 process_info->process_handle = INVALID_HANDLE_VALUE;
2122 done:
2123 if (ret == FALSE) {
2124 process_info->pid = -mono_w32error_get_last ();
2125 } else {
2126 #if !defined(MONO_CROSS_COMPILE)
2127 process_info->pid = mono_w32process_get_pid (process_info->process_handle);
2128 #else
2129 process_info->pid = 0;
2130 #endif
2133 mono_createprocess_coop_cleanup (&coop);
2135 return ret;
2138 /* Only used when UseShellExecute is false */
2139 static gboolean
2140 process_get_complete_path (const gunichar2 *appname, gchar **completed)
2142 char *found = NULL;
2143 gboolean result = FALSE;
2145 char *utf8app = g_utf16_to_utf8 (appname, -1, NULL, NULL, NULL);
2147 if (g_path_is_absolute (utf8app)) {
2148 *completed = g_shell_quote (utf8app);
2149 result = TRUE;
2150 goto exit;
2153 if (g_file_test (utf8app, G_FILE_TEST_IS_EXECUTABLE) && !g_file_test (utf8app, G_FILE_TEST_IS_DIR)) {
2154 *completed = g_shell_quote (utf8app);
2155 result = TRUE;
2156 goto exit;
2159 found = g_find_program_in_path (utf8app);
2160 if (found == NULL) {
2161 *completed = NULL;
2162 result = FALSE;
2163 goto exit;
2166 *completed = g_shell_quote (found);
2167 result = TRUE;
2168 exit:
2169 g_free (found);
2170 g_free (utf8app);
2171 return result;
2174 static gboolean
2175 process_get_shell_arguments (MonoCreateProcessCoop *coop, gunichar2 **shell_path)
2177 gchar *complete_path = NULL;
2179 *shell_path = NULL;
2181 if (process_get_complete_path (coop->filename, &complete_path)) {
2182 *shell_path = g_utf8_to_utf16 (complete_path, -1, NULL, NULL, NULL);
2183 g_free (complete_path);
2186 return *shell_path != NULL;
2189 MonoBoolean
2190 ves_icall_System_Diagnostics_Process_CreateProcess_internal (MonoW32ProcessStartInfoHandle proc_start_info,
2191 HANDLE stdin_handle, HANDLE stdout_handle, HANDLE stderr_handle, MonoW32ProcessInfo *process_info, MonoError *error)
2193 MonoCreateProcessCoop coop;
2194 mono_createprocess_coop_init (&coop, proc_start_info, process_info);
2196 gboolean ret;
2197 StartupHandles startup_handles;
2198 gunichar2 *shell_path = NULL;
2200 memset (&startup_handles, 0, sizeof (startup_handles));
2201 startup_handles.input = stdin_handle;
2202 startup_handles.output = stdout_handle;
2203 startup_handles.error = stderr_handle;
2205 if (!process_get_shell_arguments (&coop, &shell_path)) {
2206 process_info->pid = -ERROR_FILE_NOT_FOUND;
2207 ret = FALSE;
2208 goto exit;
2211 gunichar2 *args;
2212 args = coop.length.arguments ? coop.arguments : NULL;
2214 /* The default dir name is "". Turn that into NULL to mean "current directory" */
2215 gunichar2 *dir;
2216 dir = coop.length.working_directory ? coop.working_directory : NULL;
2218 ret = process_create (shell_path, args, dir, &startup_handles, process_info);
2220 if (!ret)
2221 process_info->pid = -mono_w32error_get_last ();
2223 exit:
2224 g_free (shell_path);
2225 mono_createprocess_coop_cleanup (&coop);
2226 return ret;
2229 /* Returns an array of pids */
2230 MonoArrayHandle
2231 ves_icall_System_Diagnostics_Process_GetProcesses_internal (MonoError *error)
2233 int count = 0;
2234 guint32 *raw = 0;
2235 gpointer *pidarray = 0;
2236 MonoArrayHandle procs = NULL_HANDLE_ARRAY;
2238 // FIXME mono_process_list should probably return array of int
2239 // as all of the users of the elements truncate to that.
2241 MONO_ENTER_GC_SAFE;
2242 pidarray = mono_process_list (&count);
2243 MONO_EXIT_GC_SAFE;
2244 if (!pidarray) {
2245 mono_error_set_not_supported (error, "This system does not support EnumProcesses");
2246 goto exit;
2248 procs = mono_array_new_handle (mono_domain_get (), mono_get_int32_class (), count, error);
2249 if (!is_ok (error)) {
2250 procs = NULL_HANDLE_ARRAY;
2251 goto exit;
2254 MONO_ENTER_NO_SAFEPOINTS;
2256 raw = mono_array_addr_internal (MONO_HANDLE_RAW (procs), guint32, 0);
2257 if (sizeof (guint32) == sizeof (gpointer)) {
2258 memcpy (raw, pidarray, count * sizeof (gint32));
2259 } else {
2260 for (int i = 0; i < count; ++i)
2261 raw [i] = GPOINTER_TO_UINT (pidarray [i]);
2264 MONO_EXIT_NO_SAFEPOINTS;
2266 exit:
2267 g_free (pidarray);
2268 return procs;
2271 void
2272 mono_w32process_set_cli_launcher (gchar *path)
2274 g_free (cli_launcher);
2275 cli_launcher = g_strdup (path);
2278 gpointer
2279 ves_icall_Microsoft_Win32_NativeMethods_GetCurrentProcess (MonoError *error)
2281 return current_process;
2284 MonoBoolean
2285 ves_icall_Microsoft_Win32_NativeMethods_GetExitCodeProcess (gpointer handle, gint32 *exitcode, MonoError *error)
2287 return mono_get_exit_code_process (handle, exitcode);
2290 static MonoBoolean
2291 mono_get_exit_code_process (gpointer handle, gint32 *exitcode)
2293 MonoW32Handle *handle_data;
2294 MonoW32HandleProcess *process_handle;
2296 if (!exitcode)
2297 return FALSE;
2299 if (!mono_w32handle_lookup_and_ref (handle, &handle_data)) {
2300 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: unknown handle %p", __func__, handle);
2301 mono_w32error_set_last (ERROR_INVALID_HANDLE);
2302 return FALSE;
2305 if (handle_data->type != MONO_W32TYPE_PROCESS) {
2306 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: unknown process handle %p", __func__, handle);
2307 mono_w32error_set_last (ERROR_INVALID_HANDLE);
2308 mono_w32handle_unref (handle_data);
2309 return FALSE;
2312 process_handle = (MonoW32HandleProcess*) handle_data->specific;
2314 if (process_handle->pid == current_pid) {
2315 *exitcode = STILL_ACTIVE;
2316 mono_w32handle_unref (handle_data);
2317 return TRUE;
2320 /* A process handle is only signalled if the process has exited
2321 * and has been waited for. Make sure any process exit has been
2322 * noticed before checking if the process is signalled.
2323 * Fixes bug 325463. */
2324 mono_w32handle_wait_one (handle, 0, TRUE);
2326 *exitcode = mono_w32handle_issignalled (handle_data) ? process_handle->exitstatus : STILL_ACTIVE;
2328 mono_w32handle_unref (handle_data);
2330 return TRUE;
2333 MonoBoolean
2334 ves_icall_Microsoft_Win32_NativeMethods_CloseProcess (gpointer handle, MonoError *error)
2336 return mono_w32handle_close (handle);
2339 MonoBoolean
2340 ves_icall_Microsoft_Win32_NativeMethods_TerminateProcess (gpointer handle, gint32 exitcode, MonoError *error)
2342 #ifdef HAVE_KILL
2343 MonoW32Handle *handle_data;
2344 int ret;
2345 pid_t pid;
2347 if (!mono_w32handle_lookup_and_ref (handle, &handle_data)) {
2348 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: unknown handle %p", __func__, handle);
2349 mono_w32error_set_last (ERROR_INVALID_HANDLE);
2350 return FALSE;
2353 if (handle_data->type != MONO_W32TYPE_PROCESS) {
2354 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: unknown process handle %p", __func__, handle);
2355 mono_w32error_set_last (ERROR_INVALID_HANDLE);
2356 mono_w32handle_unref (handle_data);
2357 return FALSE;
2360 pid = ((MonoW32HandleProcess*) handle_data->specific)->pid;
2362 ret = kill (pid, exitcode == -1 ? SIGKILL : SIGTERM);
2363 if (ret == 0) {
2364 mono_w32handle_unref (handle_data);
2365 return TRUE;
2368 switch (errno) {
2369 case EINVAL: mono_w32error_set_last (ERROR_INVALID_PARAMETER); break;
2370 case EPERM: mono_w32error_set_last (ERROR_ACCESS_DENIED); break;
2371 case ESRCH: mono_w32error_set_last (ERROR_PROC_NOT_FOUND); break;
2372 default: mono_w32error_set_last (ERROR_GEN_FAILURE); break;
2375 mono_w32handle_unref (handle_data);
2376 return FALSE;
2377 #else
2378 g_error ("kill() is not supported by this platform");
2379 #endif
2382 MonoBoolean
2383 ves_icall_Microsoft_Win32_NativeMethods_GetProcessWorkingSetSize (gpointer handle, gsize *min, gsize *max, MonoError *error)
2385 MonoW32Handle *handle_data;
2386 MonoW32HandleProcess *process_handle;
2388 if (!min || !max)
2389 return FALSE;
2391 if (!mono_w32handle_lookup_and_ref (handle, &handle_data)) {
2392 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: unknown handle %p", __func__, handle);
2393 mono_w32error_set_last (ERROR_INVALID_HANDLE);
2394 return FALSE;
2397 if (handle_data->type != MONO_W32TYPE_PROCESS) {
2398 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: unknown process handle %p", __func__, handle);
2399 mono_w32error_set_last (ERROR_INVALID_HANDLE);
2400 mono_w32handle_unref (handle_data);
2401 return FALSE;
2404 process_handle = (MonoW32HandleProcess*) handle_data->specific;
2406 if (!process_handle->child) {
2407 mono_w32handle_unref (handle_data);
2408 return FALSE;
2411 *min = process_handle->min_working_set;
2412 *max = process_handle->max_working_set;
2414 mono_w32handle_unref (handle_data);
2415 return TRUE;
2418 MonoBoolean
2419 ves_icall_Microsoft_Win32_NativeMethods_SetProcessWorkingSetSize (gpointer handle, gsize min, gsize max, MonoError *error)
2421 MonoW32Handle *handle_data;
2422 MonoW32HandleProcess *process_handle;
2424 if (!mono_w32handle_lookup_and_ref (handle, &handle_data)) {
2425 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: unknown handle %p", __func__, handle);
2426 mono_w32error_set_last (ERROR_INVALID_HANDLE);
2427 return FALSE;
2430 if (handle_data->type != MONO_W32TYPE_PROCESS) {
2431 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: unknown process handle %p", __func__, handle);
2432 mono_w32error_set_last (ERROR_INVALID_HANDLE);
2433 mono_w32handle_unref (handle_data);
2434 return FALSE;
2437 process_handle = (MonoW32HandleProcess*) handle_data->specific;
2439 if (!process_handle->child) {
2440 mono_w32handle_unref (handle_data);
2441 return FALSE;
2444 process_handle->min_working_set = min;
2445 process_handle->max_working_set = max;
2447 mono_w32handle_unref (handle_data);
2448 return TRUE;
2451 gint32
2452 ves_icall_Microsoft_Win32_NativeMethods_GetPriorityClass (gpointer handle, MonoError *error)
2454 #ifdef HAVE_GETPRIORITY
2455 MonoW32Handle *handle_data;
2456 gint res;
2457 gint32 ret;
2458 pid_t pid;
2460 if (!mono_w32handle_lookup_and_ref (handle, &handle_data)) {
2461 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: unknown handle %p", __func__, handle);
2462 mono_w32error_set_last (ERROR_INVALID_HANDLE);
2463 return 0;
2466 if (handle_data->type != MONO_W32TYPE_PROCESS) {
2467 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: unknown process handle %p", __func__, handle);
2468 mono_w32error_set_last (ERROR_INVALID_HANDLE);
2469 mono_w32handle_unref (handle_data);
2470 return 0;
2473 pid = ((MonoW32HandleProcess*) handle_data->specific)->pid;
2475 mono_set_errno (0);
2476 res = getpriority (PRIO_PROCESS, pid);
2477 if (res == -1 && errno != 0) {
2478 switch (errno) {
2479 case EPERM:
2480 case EACCES:
2481 mono_w32error_set_last (ERROR_ACCESS_DENIED);
2482 break;
2483 case ESRCH:
2484 mono_w32error_set_last (ERROR_PROC_NOT_FOUND);
2485 break;
2486 default:
2487 mono_w32error_set_last (ERROR_GEN_FAILURE);
2490 mono_w32handle_unref (handle_data);
2491 return 0;
2494 if (res == 0)
2495 ret = MONO_W32PROCESS_PRIORITY_CLASS_NORMAL;
2496 else if (res < -15)
2497 ret = MONO_W32PROCESS_PRIORITY_CLASS_REALTIME;
2498 else if (res < -10)
2499 ret = MONO_W32PROCESS_PRIORITY_CLASS_HIGH;
2500 else if (res < 0)
2501 ret = MONO_W32PROCESS_PRIORITY_CLASS_ABOVE_NORMAL;
2502 else if (res > 10)
2503 ret = MONO_W32PROCESS_PRIORITY_CLASS_IDLE;
2504 else if (res > 0)
2505 ret = MONO_W32PROCESS_PRIORITY_CLASS_BELOW_NORMAL;
2506 else
2507 ret = MONO_W32PROCESS_PRIORITY_CLASS_NORMAL;
2509 mono_w32handle_unref (handle_data);
2510 return ret;
2511 #else
2512 mono_w32error_set_last (ERROR_NOT_SUPPORTED);
2513 return 0;
2514 #endif
2517 MonoBoolean
2518 ves_icall_Microsoft_Win32_NativeMethods_SetPriorityClass (gpointer handle, gint32 priorityClass, MonoError *error)
2520 #ifdef HAVE_SETPRIORITY
2521 MonoW32Handle *handle_data;
2522 int ret;
2523 int prio;
2524 pid_t pid;
2526 if (!mono_w32handle_lookup_and_ref (handle, &handle_data)) {
2527 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: unknown handle %p", __func__, handle);
2528 mono_w32error_set_last (ERROR_INVALID_HANDLE);
2529 return FALSE;
2532 if (handle_data->type != MONO_W32TYPE_PROCESS) {
2533 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: unknown process handle %p", __func__, handle);
2534 mono_w32error_set_last (ERROR_INVALID_HANDLE);
2535 mono_w32handle_unref (handle_data);
2536 return FALSE;
2539 pid = ((MonoW32HandleProcess*) handle_data->specific)->pid;
2541 switch (priorityClass) {
2542 case MONO_W32PROCESS_PRIORITY_CLASS_IDLE:
2543 prio = 19;
2544 break;
2545 case MONO_W32PROCESS_PRIORITY_CLASS_BELOW_NORMAL:
2546 prio = 10;
2547 break;
2548 case MONO_W32PROCESS_PRIORITY_CLASS_NORMAL:
2549 prio = 0;
2550 break;
2551 case MONO_W32PROCESS_PRIORITY_CLASS_ABOVE_NORMAL:
2552 prio = -5;
2553 break;
2554 case MONO_W32PROCESS_PRIORITY_CLASS_HIGH:
2555 prio = -11;
2556 break;
2557 case MONO_W32PROCESS_PRIORITY_CLASS_REALTIME:
2558 prio = -20;
2559 break;
2560 default:
2561 mono_w32error_set_last (ERROR_INVALID_PARAMETER);
2562 mono_w32handle_unref (handle_data);
2563 return FALSE;
2566 ret = setpriority (PRIO_PROCESS, pid, prio);
2567 if (ret == -1) {
2568 switch (errno) {
2569 case EPERM:
2570 case EACCES:
2571 mono_w32error_set_last (ERROR_ACCESS_DENIED);
2572 break;
2573 case ESRCH:
2574 mono_w32error_set_last (ERROR_PROC_NOT_FOUND);
2575 break;
2576 default:
2577 mono_w32error_set_last (ERROR_GEN_FAILURE);
2581 mono_w32handle_unref (handle_data);
2582 return ret == 0;
2583 #else
2584 mono_w32error_set_last (ERROR_NOT_SUPPORTED);
2585 return FALSE;
2586 #endif
2589 static void
2590 ticks_to_processtime (guint64 ticks, ProcessTime *processtime)
2592 processtime->lowDateTime = ticks & 0xFFFFFFFF;
2593 processtime->highDateTime = ticks >> 32;
2596 MonoBoolean
2597 ves_icall_Microsoft_Win32_NativeMethods_GetProcessTimes (gpointer handle, gint64 *creation_time, gint64 *exit_time, gint64 *kernel_time, gint64 *user_time, MonoError *error)
2599 MonoW32Handle *handle_data;
2600 MonoW32HandleProcess *process_handle;
2601 ProcessTime *creation_processtime, *exit_processtime, *kernel_processtime, *user_processtime;
2603 if (!creation_time || !exit_time || !kernel_time || !user_time) {
2604 /* Not sure if w32 allows NULLs here or not */
2605 return FALSE;
2608 creation_processtime = (ProcessTime*) creation_time;
2609 exit_processtime = (ProcessTime*) exit_time;
2610 kernel_processtime = (ProcessTime*) kernel_time;
2611 user_processtime = (ProcessTime*) user_time;
2613 memset (creation_processtime, 0, sizeof (ProcessTime));
2614 memset (exit_processtime, 0, sizeof (ProcessTime));
2615 memset (kernel_processtime, 0, sizeof (ProcessTime));
2616 memset (user_processtime, 0, sizeof (ProcessTime));
2618 if (!mono_w32handle_lookup_and_ref (handle, &handle_data)) {
2619 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: unknown handle %p", __func__, handle);
2620 mono_w32error_set_last (ERROR_INVALID_HANDLE);
2621 return FALSE;
2624 if (handle_data->type != MONO_W32TYPE_PROCESS) {
2625 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: unknown process handle %p", __func__, handle);
2626 mono_w32error_set_last (ERROR_INVALID_HANDLE);
2627 mono_w32handle_unref (handle_data);
2628 return FALSE;
2631 process_handle = (MonoW32HandleProcess*) handle_data->specific;
2633 if (!process_handle->child) {
2634 gint64 start_ticks, user_ticks, kernel_ticks;
2636 mono_process_get_times (GINT_TO_POINTER (process_handle->pid),
2637 &start_ticks, &user_ticks, &kernel_ticks);
2639 ticks_to_processtime (start_ticks, creation_processtime);
2640 ticks_to_processtime (kernel_ticks, kernel_processtime);
2641 ticks_to_processtime (user_ticks, user_processtime);
2643 mono_w32handle_unref (handle_data);
2644 return TRUE;
2647 ticks_to_processtime (process_handle->create_time, creation_processtime);
2649 /* A process handle is only signalled if the process has
2650 * exited, otherwise exit_processtime isn't set */
2651 if (mono_w32handle_issignalled (handle_data))
2652 ticks_to_processtime (process_handle->exit_time, exit_processtime);
2654 #ifdef HAVE_GETRUSAGE
2655 if (process_handle->pid == getpid ()) {
2656 struct rusage time_data;
2657 if (getrusage (RUSAGE_SELF, &time_data) == 0) {
2658 ticks_to_processtime ((guint64)time_data.ru_utime.tv_sec * 10000000 + (guint64)time_data.ru_utime.tv_usec * 10, user_processtime);
2659 ticks_to_processtime ((guint64)time_data.ru_stime.tv_sec * 10000000 + (guint64)time_data.ru_stime.tv_usec * 10, kernel_processtime);
2662 #endif
2664 mono_w32handle_unref (handle_data);
2665 return TRUE;
2668 static IMAGE_SECTION_HEADER *
2669 get_enclosing_section_header (guint32 rva, IMAGE_NT_HEADERS32 *nt_headers)
2671 IMAGE_SECTION_HEADER *section = IMAGE_FIRST_SECTION32 (nt_headers);
2672 guint32 i;
2674 for (i = 0; i < GUINT16_FROM_LE (nt_headers->FileHeader.NumberOfSections); i++, section++) {
2675 guint32 size = GUINT32_FROM_LE (section->Misc.VirtualSize);
2676 if (size == 0) {
2677 size = GUINT32_FROM_LE (section->SizeOfRawData);
2680 if ((rva >= GUINT32_FROM_LE (section->VirtualAddress)) &&
2681 (rva < (GUINT32_FROM_LE (section->VirtualAddress) + size))) {
2682 return(section);
2686 return(NULL);
2689 /* This works for both 32bit and 64bit files, as the differences are
2690 * all after the section header block
2692 static gpointer
2693 get_ptr_from_rva (guint32 rva, IMAGE_NT_HEADERS32 *ntheaders, gpointer file_map)
2695 IMAGE_SECTION_HEADER *section_header;
2696 guint32 delta;
2698 section_header = get_enclosing_section_header (rva, ntheaders);
2699 if (section_header == NULL) {
2700 return(NULL);
2703 delta = (guint32)(GUINT32_FROM_LE (section_header->VirtualAddress) -
2704 GUINT32_FROM_LE (section_header->PointerToRawData));
2706 return((guint8 *)file_map + rva - delta);
2709 static gpointer
2710 scan_resource_dir (IMAGE_RESOURCE_DIRECTORY *root, IMAGE_NT_HEADERS32 *nt_headers, gpointer file_map,
2711 IMAGE_RESOURCE_DIRECTORY_ENTRY *entry, int level, guint32 res_id, guint32 lang_id, gsize *size)
2713 IMAGE_RESOURCE_DIRECTORY_ENTRY swapped_entry;
2714 gboolean is_string, is_dir;
2715 guint32 name_offset, dir_offset, data_offset;
2717 swapped_entry.Name = GUINT32_FROM_LE (entry->Name);
2718 swapped_entry.OffsetToData = GUINT32_FROM_LE (entry->OffsetToData);
2720 is_string = swapped_entry.NameIsString;
2721 is_dir = swapped_entry.DataIsDirectory;
2722 name_offset = swapped_entry.NameOffset;
2723 dir_offset = swapped_entry.OffsetToDirectory;
2724 data_offset = swapped_entry.OffsetToData;
2726 if (level == 0) {
2727 /* Normally holds a directory entry for each type of
2728 * resource
2730 if ((is_string == FALSE &&
2731 name_offset != res_id) ||
2732 (is_string == TRUE)) {
2733 return(NULL);
2735 } else if (level == 1) {
2736 /* Normally holds a directory entry for each resource
2737 * item
2739 } else if (level == 2) {
2740 /* Normally holds a directory entry for each language
2742 if ((is_string == FALSE &&
2743 name_offset != lang_id &&
2744 lang_id != 0) ||
2745 (is_string == TRUE)) {
2746 return(NULL);
2748 } else {
2749 g_assert_not_reached ();
2752 if (is_dir == TRUE) {
2753 IMAGE_RESOURCE_DIRECTORY *res_dir = (IMAGE_RESOURCE_DIRECTORY *)((guint8 *)root + dir_offset);
2754 IMAGE_RESOURCE_DIRECTORY_ENTRY *sub_entries = (IMAGE_RESOURCE_DIRECTORY_ENTRY *)(res_dir + 1);
2755 guint32 entries, i;
2757 entries = GUINT16_FROM_LE (res_dir->NumberOfNamedEntries) + GUINT16_FROM_LE (res_dir->NumberOfIdEntries);
2759 for (i = 0; i < entries; i++) {
2760 IMAGE_RESOURCE_DIRECTORY_ENTRY *sub_entry = &sub_entries[i];
2761 gpointer ret;
2763 ret = scan_resource_dir (root, nt_headers, file_map,
2764 sub_entry, level + 1, res_id,
2765 lang_id, size);
2766 if (ret != NULL) {
2767 return(ret);
2771 return(NULL);
2772 } else {
2773 IMAGE_RESOURCE_DATA_ENTRY *data_entry = (IMAGE_RESOURCE_DATA_ENTRY *)((guint8 *)root + data_offset);
2774 *size = GUINT32_FROM_LE (data_entry->Size);
2776 return(get_ptr_from_rva (GUINT32_FROM_LE (data_entry->OffsetToData), nt_headers, file_map));
2780 static gpointer
2781 find_pe_file_resources32 (gpointer file_map, guint32 map_size, guint32 res_id, guint32 lang_id, gsize *size)
2783 IMAGE_DOS_HEADER *dos_header;
2784 IMAGE_NT_HEADERS32 *nt_headers;
2785 IMAGE_RESOURCE_DIRECTORY *resource_dir;
2786 IMAGE_RESOURCE_DIRECTORY_ENTRY *resource_dir_entry;
2787 guint32 resource_rva, entries, i;
2788 gpointer ret = NULL;
2790 dos_header = (IMAGE_DOS_HEADER *)file_map;
2791 if (dos_header->e_magic != IMAGE_DOS_SIGNATURE) {
2792 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: Bad dos signature 0x%x", __func__, dos_header->e_magic);
2794 mono_w32error_set_last (ERROR_INVALID_DATA);
2795 return(NULL);
2798 if (map_size < sizeof(IMAGE_NT_HEADERS32) + GUINT32_FROM_LE (dos_header->e_lfanew)) {
2799 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: File is too small: %" G_GUINT32_FORMAT, __func__, map_size);
2801 mono_w32error_set_last (ERROR_BAD_LENGTH);
2802 return(NULL);
2805 nt_headers = (IMAGE_NT_HEADERS32 *)((guint8 *)file_map + GUINT32_FROM_LE (dos_header->e_lfanew));
2806 if (nt_headers->Signature != IMAGE_NT_SIGNATURE) {
2807 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: Bad NT signature 0x%x", __func__, nt_headers->Signature);
2809 mono_w32error_set_last (ERROR_INVALID_DATA);
2810 return(NULL);
2813 if (nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
2814 /* Do 64-bit stuff */
2815 resource_rva = GUINT32_FROM_LE (((IMAGE_NT_HEADERS64 *)nt_headers)->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress);
2816 } else {
2817 resource_rva = GUINT32_FROM_LE (nt_headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress);
2820 if (resource_rva == 0) {
2821 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: No resources in file!", __func__);
2823 mono_w32error_set_last (ERROR_INVALID_DATA);
2824 return(NULL);
2827 resource_dir = (IMAGE_RESOURCE_DIRECTORY *)get_ptr_from_rva (resource_rva, (IMAGE_NT_HEADERS32 *)nt_headers, file_map);
2828 if (resource_dir == NULL) {
2829 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: Can't find resource directory", __func__);
2831 mono_w32error_set_last (ERROR_INVALID_DATA);
2832 return(NULL);
2835 entries = GUINT16_FROM_LE (resource_dir->NumberOfNamedEntries) + GUINT16_FROM_LE (resource_dir->NumberOfIdEntries);
2836 resource_dir_entry = (IMAGE_RESOURCE_DIRECTORY_ENTRY *)(resource_dir + 1);
2838 for (i = 0; i < entries; i++) {
2839 IMAGE_RESOURCE_DIRECTORY_ENTRY *direntry = &resource_dir_entry[i];
2840 ret = scan_resource_dir (resource_dir,
2841 (IMAGE_NT_HEADERS32 *)nt_headers,
2842 file_map, direntry, 0, res_id,
2843 lang_id, size);
2844 if (ret != NULL) {
2845 return(ret);
2849 return(NULL);
2852 static gpointer
2853 find_pe_file_resources64 (gpointer file_map, guint32 map_size, guint32 res_id, guint32 lang_id, gsize *size)
2855 IMAGE_DOS_HEADER *dos_header;
2856 IMAGE_NT_HEADERS64 *nt_headers;
2857 IMAGE_RESOURCE_DIRECTORY *resource_dir;
2858 IMAGE_RESOURCE_DIRECTORY_ENTRY *resource_dir_entry;
2859 guint32 resource_rva, entries, i;
2860 gpointer ret = NULL;
2862 dos_header = (IMAGE_DOS_HEADER *)file_map;
2863 if (dos_header->e_magic != IMAGE_DOS_SIGNATURE) {
2864 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: Bad dos signature 0x%x", __func__, dos_header->e_magic);
2866 mono_w32error_set_last (ERROR_INVALID_DATA);
2867 return(NULL);
2870 if (map_size < sizeof(IMAGE_NT_HEADERS64) + GUINT32_FROM_LE (dos_header->e_lfanew)) {
2871 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: File is too small: %" G_GUINT32_FORMAT, __func__, map_size);
2873 mono_w32error_set_last (ERROR_BAD_LENGTH);
2874 return(NULL);
2877 nt_headers = (IMAGE_NT_HEADERS64 *)((guint8 *)file_map + GUINT32_FROM_LE (dos_header->e_lfanew));
2878 if (nt_headers->Signature != IMAGE_NT_SIGNATURE) {
2879 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: Bad NT signature 0x%x", __func__,
2880 nt_headers->Signature);
2882 mono_w32error_set_last (ERROR_INVALID_DATA);
2883 return(NULL);
2886 if (nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
2887 /* Do 64-bit stuff */
2888 resource_rva = GUINT32_FROM_LE (((IMAGE_NT_HEADERS64 *)nt_headers)->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress);
2889 } else {
2890 resource_rva = GUINT32_FROM_LE (nt_headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress);
2893 if (resource_rva == 0) {
2894 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: No resources in file!", __func__);
2896 mono_w32error_set_last (ERROR_INVALID_DATA);
2897 return(NULL);
2900 resource_dir = (IMAGE_RESOURCE_DIRECTORY *)get_ptr_from_rva (resource_rva, (IMAGE_NT_HEADERS32 *)nt_headers, file_map);
2901 if (resource_dir == NULL) {
2902 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: Can't find resource directory", __func__);
2904 mono_w32error_set_last (ERROR_INVALID_DATA);
2905 return(NULL);
2908 entries = GUINT16_FROM_LE (resource_dir->NumberOfNamedEntries) + GUINT16_FROM_LE (resource_dir->NumberOfIdEntries);
2909 resource_dir_entry = (IMAGE_RESOURCE_DIRECTORY_ENTRY *)(resource_dir + 1);
2911 for (i = 0; i < entries; i++) {
2912 IMAGE_RESOURCE_DIRECTORY_ENTRY *direntry = &resource_dir_entry[i];
2913 ret = scan_resource_dir (resource_dir,
2914 (IMAGE_NT_HEADERS32 *)nt_headers,
2915 file_map, direntry, 0, res_id,
2916 lang_id, size);
2917 if (ret != NULL) {
2918 return(ret);
2922 return(NULL);
2925 static gpointer
2926 find_pe_file_resources (gpointer file_map, guint32 map_size, guint32 res_id, guint32 lang_id, gsize *size)
2928 /* Figure this out when we support 64bit PE files */
2929 if (1) {
2930 return find_pe_file_resources32 (file_map, map_size, res_id,
2931 lang_id, size);
2932 } else {
2933 return find_pe_file_resources64 (file_map, map_size, res_id,
2934 lang_id, size);
2938 static guint32
2939 unicode_chars (const gunichar2 *str)
2941 guint32 len = 0;
2943 do {
2944 if (str[len] == '\0') {
2945 return(len);
2947 len++;
2948 } while(1);
2951 static gboolean
2952 unicode_compare (const gunichar2 *str1, const gunichar2 *str2)
2954 while (*str1 && *str2) {
2955 if (*str1 != *str2) {
2956 return(FALSE);
2958 ++str1;
2959 ++str2;
2962 return(*str1 == *str2);
2965 /* compare a little-endian null-terminated utf16 string and a normal string.
2966 * Can be used only for ascii or latin1 chars.
2968 static gboolean
2969 unicode_string_equals (const gunichar2 *str1, const gchar *str2)
2971 while (*str1 && *str2) {
2972 if (GUINT16_TO_LE (*str1) != *str2) {
2973 return(FALSE);
2975 ++str1;
2976 ++str2;
2979 return(*str1 == *str2);
2982 typedef struct {
2983 guint16 data_len;
2984 guint16 value_len;
2985 guint16 type;
2986 gunichar2 *key;
2987 } version_data;
2989 /* Returns a pointer to the value data, because there's no way to know
2990 * how big that data is (value_len is set to zero for most blocks :-( )
2992 static gconstpointer
2993 get_versioninfo_block (gconstpointer data, version_data *block)
2995 block->data_len = GUINT16_FROM_LE (*((guint16 *)data));
2996 data = (char *)data + sizeof(guint16);
2997 block->value_len = GUINT16_FROM_LE (*((guint16 *)data));
2998 data = (char *)data + sizeof(guint16);
3000 /* No idea what the type is supposed to indicate */
3001 block->type = GUINT16_FROM_LE (*((guint16 *)data));
3002 data = (char *)data + sizeof(guint16);
3003 block->key = ((gunichar2 *)data);
3005 /* Skip over the key (including the terminator) */
3006 data = ((gunichar2 *)data) + (unicode_chars (block->key) + 1);
3008 /* align on a 32-bit boundary */
3009 ALIGN32 (data);
3011 return(data);
3014 static gconstpointer
3015 get_fixedfileinfo_block (gconstpointer data, version_data *block)
3017 gconstpointer data_ptr;
3018 VS_FIXEDFILEINFO *ffi;
3020 data_ptr = get_versioninfo_block (data, block);
3022 if (block->value_len != sizeof(VS_FIXEDFILEINFO)) {
3023 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: FIXEDFILEINFO size mismatch", __func__);
3024 return(NULL);
3027 if (!unicode_string_equals (block->key, "VS_VERSION_INFO")) {
3028 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: VS_VERSION_INFO mismatch", __func__);
3030 return(NULL);
3033 ffi = ((VS_FIXEDFILEINFO *)data_ptr);
3034 if ((ffi->dwSignature != VS_FFI_SIGNATURE) ||
3035 (ffi->dwStrucVersion != VS_FFI_STRUCVERSION)) {
3036 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: FIXEDFILEINFO bad signature", __func__);
3038 return(NULL);
3041 return(data_ptr);
3044 static gconstpointer
3045 get_varfileinfo_block (gconstpointer data_ptr, version_data *block)
3047 /* data is pointing at a Var block
3049 data_ptr = get_versioninfo_block (data_ptr, block);
3051 return(data_ptr);
3054 static gconstpointer
3055 get_string_block (gconstpointer data_ptr, const gunichar2 *string_key, gpointer *string_value,
3056 guint32 *string_value_len, version_data *block)
3058 guint16 data_len = block->data_len;
3059 guint16 string_len = 28; /* Length of the StringTable block */
3060 char *orig_data_ptr = (char *)data_ptr - 28;
3062 /* data_ptr is pointing at an array of one or more String blocks
3063 * with total length (not including alignment padding) of
3064 * data_len
3066 while (((char *)data_ptr - (char *)orig_data_ptr) < data_len) {
3067 /* align on a 32-bit boundary */
3068 ALIGN32 (data_ptr);
3070 data_ptr = get_versioninfo_block (data_ptr, block);
3071 if (block->data_len == 0) {
3072 /* We must have hit padding, so give up
3073 * processing now
3075 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: Hit 0-length block, giving up", __func__);
3077 return(NULL);
3080 string_len = string_len + block->data_len;
3082 if (string_key != NULL &&
3083 string_value != NULL &&
3084 string_value_len != NULL &&
3085 unicode_compare (string_key, block->key) == TRUE) {
3086 *string_value = (gpointer)data_ptr;
3087 *string_value_len = block->value_len;
3090 /* Skip over the value */
3091 data_ptr = ((gunichar2 *)data_ptr) + block->value_len;
3094 return(data_ptr);
3097 /* Returns a pointer to the byte following the Stringtable block, or
3098 * NULL if the data read hits padding. We can't recover from this
3099 * because the data length does not include padding bytes, so it's not
3100 * possible to just return the start position + length
3102 * If lang == NULL it means we're just stepping through this block
3104 static gconstpointer
3105 get_stringtable_block (gconstpointer data_ptr, gchar *lang, const gunichar2 *string_key, gpointer *string_value,
3106 guint32 *string_value_len, version_data *block)
3108 guint16 data_len = block->data_len;
3109 guint16 string_len = 36; /* length of the StringFileInfo block */
3110 gchar *found_lang;
3111 gchar *lowercase_lang;
3113 /* data_ptr is pointing at an array of StringTable blocks,
3114 * with total length (not including alignment padding) of
3115 * data_len
3118 while(string_len < data_len) {
3119 /* align on a 32-bit boundary */
3120 ALIGN32 (data_ptr);
3122 data_ptr = get_versioninfo_block (data_ptr, block);
3123 if (block->data_len == 0) {
3124 /* We must have hit padding, so give up
3125 * processing now
3127 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: Hit 0-length block, giving up", __func__);
3128 return(NULL);
3131 string_len = string_len + block->data_len;
3133 found_lang = g_utf16_to_utf8 (block->key, 8, NULL, NULL, NULL);
3134 if (found_lang == NULL) {
3135 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: Didn't find a valid language key, giving up", __func__);
3136 return(NULL);
3139 lowercase_lang = g_utf8_strdown (found_lang, -1);
3140 g_free (found_lang);
3141 found_lang = lowercase_lang;
3142 lowercase_lang = NULL;
3144 if (lang != NULL && !strcmp (found_lang, lang)) {
3145 /* Got the one we're interested in */
3146 data_ptr = get_string_block (data_ptr, string_key,
3147 string_value,
3148 string_value_len, block);
3149 } else {
3150 data_ptr = get_string_block (data_ptr, NULL, NULL,
3151 NULL, block);
3154 g_free (found_lang);
3156 if (data_ptr == NULL) {
3157 /* Child block hit padding */
3158 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: Child block hit 0-length block, giving up", __func__);
3159 return(NULL);
3163 return(data_ptr);
3166 #if G_BYTE_ORDER == G_BIG_ENDIAN
3167 static gconstpointer
3168 big_up_string_block (gconstpointer data_ptr, version_data *block)
3170 guint16 data_len = block->data_len;
3171 guint16 string_len = 28; /* Length of the StringTable block */
3172 gchar *big_value;
3173 char *orig_data_ptr = (char *)data_ptr - 28;
3175 /* data_ptr is pointing at an array of one or more String
3176 * blocks with total length (not including alignment padding)
3177 * of data_len
3179 while (((char *)data_ptr - (char *)orig_data_ptr) < data_len) {
3180 /* align on a 32-bit boundary */
3181 ALIGN32 (data_ptr);
3183 data_ptr = get_versioninfo_block (data_ptr, block);
3184 if (block->data_len == 0) {
3185 /* We must have hit padding, so give up
3186 * processing now
3188 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: Hit 0-length block, giving up", __func__);
3189 return(NULL);
3192 string_len = string_len + block->data_len;
3194 big_value = g_convert ((gchar *)block->key,
3195 unicode_chars (block->key) * 2,
3196 "UTF-16BE", "UTF-16LE", NULL, NULL,
3197 NULL);
3198 if (big_value == NULL) {
3199 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: Didn't find a valid string, giving up", __func__);
3200 return(NULL);
3203 /* The swapped string should be exactly the same
3204 * length as the original little-endian one, but only
3205 * copy the number of original chars just to be on the
3206 * safe side
3208 memcpy (block->key, big_value, unicode_chars (block->key) * 2);
3209 g_free (big_value);
3211 big_value = g_convert ((gchar *)data_ptr,
3212 unicode_chars (data_ptr) * 2,
3213 "UTF-16BE", "UTF-16LE", NULL, NULL,
3214 NULL);
3215 if (big_value == NULL) {
3216 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: Didn't find a valid data string, giving up", __func__);
3217 return(NULL);
3219 memcpy ((gpointer)data_ptr, big_value,
3220 unicode_chars (data_ptr) * 2);
3221 g_free (big_value);
3223 data_ptr = ((gunichar2 *)data_ptr) + block->value_len;
3226 return(data_ptr);
3229 /* Returns a pointer to the byte following the Stringtable block, or
3230 * NULL if the data read hits padding. We can't recover from this
3231 * because the data length does not include padding bytes, so it's not
3232 * possible to just return the start position + length
3234 static gconstpointer
3235 big_up_stringtable_block (gconstpointer data_ptr, version_data *block)
3237 guint16 data_len = block->data_len;
3238 guint16 string_len = 36; /* length of the StringFileInfo block */
3239 gchar *big_value;
3241 /* data_ptr is pointing at an array of StringTable blocks,
3242 * with total length (not including alignment padding) of
3243 * data_len
3246 while(string_len < data_len) {
3247 /* align on a 32-bit boundary */
3248 ALIGN32 (data_ptr);
3250 data_ptr = get_versioninfo_block (data_ptr, block);
3251 if (block->data_len == 0) {
3252 /* We must have hit padding, so give up
3253 * processing now
3255 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: Hit 0-length block, giving up", __func__);
3256 return(NULL);
3259 string_len = string_len + block->data_len;
3261 big_value = g_convert ((gchar *)block->key, 16, "UTF-16BE",
3262 "UTF-16LE", NULL, NULL, NULL);
3263 if (big_value == NULL) {
3264 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: Didn't find a valid string, giving up", __func__);
3265 return(NULL);
3268 memcpy (block->key, big_value, 16);
3269 g_free (big_value);
3271 data_ptr = big_up_string_block (data_ptr, block);
3273 if (data_ptr == NULL) {
3274 /* Child block hit padding */
3275 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: Child block hit 0-length block, giving up", __func__);
3276 return(NULL);
3280 return(data_ptr);
3283 /* Follows the data structures and turns all UTF-16 strings from the
3284 * LE found in the resource section into UTF-16BE
3286 static void
3287 big_up (gconstpointer datablock, guint32 size)
3289 gconstpointer data_ptr;
3290 gint32 data_len; /* signed to guard against underflow */
3291 version_data block;
3293 data_ptr = get_fixedfileinfo_block (datablock, &block);
3294 if (data_ptr != NULL) {
3295 VS_FIXEDFILEINFO *ffi = (VS_FIXEDFILEINFO *)data_ptr;
3297 /* Byteswap all the fields */
3298 ffi->dwFileVersionMS = GUINT32_SWAP_LE_BE (ffi->dwFileVersionMS);
3299 ffi->dwFileVersionLS = GUINT32_SWAP_LE_BE (ffi->dwFileVersionLS);
3300 ffi->dwProductVersionMS = GUINT32_SWAP_LE_BE (ffi->dwProductVersionMS);
3301 ffi->dwProductVersionLS = GUINT32_SWAP_LE_BE (ffi->dwProductVersionLS);
3302 ffi->dwFileFlagsMask = GUINT32_SWAP_LE_BE (ffi->dwFileFlagsMask);
3303 ffi->dwFileFlags = GUINT32_SWAP_LE_BE (ffi->dwFileFlags);
3304 ffi->dwFileOS = GUINT32_SWAP_LE_BE (ffi->dwFileOS);
3305 ffi->dwFileType = GUINT32_SWAP_LE_BE (ffi->dwFileType);
3306 ffi->dwFileSubtype = GUINT32_SWAP_LE_BE (ffi->dwFileSubtype);
3307 ffi->dwFileDateMS = GUINT32_SWAP_LE_BE (ffi->dwFileDateMS);
3308 ffi->dwFileDateLS = GUINT32_SWAP_LE_BE (ffi->dwFileDateLS);
3310 /* The FFI and header occupies the first 92 bytes
3312 data_ptr = (char *)data_ptr + sizeof(VS_FIXEDFILEINFO);
3313 data_len = block.data_len - 92;
3315 /* There now follow zero or one StringFileInfo blocks
3316 * and zero or one VarFileInfo blocks
3318 while (data_len > 0) {
3319 /* align on a 32-bit boundary */
3320 ALIGN32 (data_ptr);
3322 data_ptr = get_versioninfo_block (data_ptr, &block);
3323 if (block.data_len == 0) {
3324 /* We must have hit padding, so give
3325 * up processing now
3327 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: Hit 0-length block, giving up", __func__);
3328 return;
3331 data_len = data_len - block.data_len;
3333 if (unicode_string_equals (block.key, "VarFileInfo")) {
3334 data_ptr = get_varfileinfo_block (data_ptr,
3335 &block);
3336 data_ptr = ((guchar *)data_ptr) + block.value_len;
3337 } else if (unicode_string_equals (block.key,
3338 "StringFileInfo")) {
3339 data_ptr = big_up_stringtable_block (data_ptr,
3340 &block);
3341 } else {
3342 /* Bogus data */
3343 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: Not a valid VERSIONINFO child block", __func__);
3344 return;
3347 if (data_ptr == NULL) {
3348 /* Child block hit padding */
3349 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: Child block hit 0-length block, giving up", __func__);
3350 return;
3355 #endif
3357 gboolean
3358 mono_w32process_get_fileversion_info (const gunichar2 *filename, gpointer *data)
3360 gpointer file_map;
3361 gpointer versioninfo;
3362 void *map_handle;
3363 gint32 map_size;
3364 gsize datasize;
3366 g_assert (data);
3367 *data = NULL;
3369 file_map = mono_pe_file_map (filename, &map_size, &map_handle);
3370 if (!file_map)
3371 return FALSE;
3373 versioninfo = find_pe_file_resources (file_map, map_size, RT_VERSION, 0, &datasize);
3374 if (!versioninfo) {
3375 mono_pe_file_unmap (file_map, map_handle);
3376 return FALSE;
3379 *data = g_malloc0 (datasize);
3381 /* This could probably process the data so that mono_w32process_ver_query_value() doesn't have to follow the
3382 * data blocks every time. But hey, these functions aren't likely to appear in many profiles. */
3383 memcpy (*data, versioninfo, datasize);
3385 #if G_BYTE_ORDER == G_BIG_ENDIAN
3386 big_up (*data, datasize);
3387 #endif
3389 mono_pe_file_unmap (file_map, map_handle);
3391 return TRUE;
3394 gboolean
3395 mono_w32process_ver_query_value (gconstpointer datablock, const gunichar2 *subblock, gpointer *buffer, guint32 *len)
3397 gchar *subblock_utf8, *lang_utf8 = NULL;
3398 gboolean ret = FALSE;
3399 version_data block;
3400 gconstpointer data_ptr;
3401 gint32 data_len; /* signed to guard against underflow */
3402 gboolean want_var = FALSE;
3403 gboolean want_string = FALSE;
3404 gunichar2 lang[8];
3405 const gunichar2 *string_key = NULL;
3406 gpointer string_value = NULL;
3407 guint32 string_value_len = 0;
3408 gchar *lowercase_lang;
3410 subblock_utf8 = g_utf16_to_utf8 (subblock, -1, NULL, NULL, NULL);
3411 if (subblock_utf8 == NULL) {
3412 return(FALSE);
3415 if (!strcmp (subblock_utf8, "\\VarFileInfo\\Translation")) {
3416 want_var = TRUE;
3417 } else if (!strncmp (subblock_utf8, "\\StringFileInfo\\", 16)) {
3418 want_string = TRUE;
3419 memcpy (lang, subblock + 16, 8 * sizeof(gunichar2));
3420 lang_utf8 = g_utf16_to_utf8 (lang, 8, NULL, NULL, NULL);
3421 lowercase_lang = g_utf8_strdown (lang_utf8, -1);
3422 g_free (lang_utf8);
3423 lang_utf8 = lowercase_lang;
3424 lowercase_lang = NULL;
3425 string_key = subblock + 25;
3428 if (!strcmp (subblock_utf8, "\\")) {
3429 data_ptr = get_fixedfileinfo_block (datablock, &block);
3430 if (data_ptr != NULL) {
3431 *buffer = (gpointer)data_ptr;
3432 *len = block.value_len;
3434 ret = TRUE;
3436 } else if (want_var || want_string) {
3437 data_ptr = get_fixedfileinfo_block (datablock, &block);
3438 if (data_ptr != NULL) {
3439 /* The FFI and header occupies the first 92
3440 * bytes
3442 data_ptr = (char *)data_ptr + sizeof(VS_FIXEDFILEINFO);
3443 data_len = block.data_len - 92;
3445 /* There now follow zero or one StringFileInfo
3446 * blocks and zero or one VarFileInfo blocks
3448 while (data_len > 0) {
3449 /* align on a 32-bit boundary */
3450 ALIGN32 (data_ptr);
3452 data_ptr = get_versioninfo_block (data_ptr,
3453 &block);
3454 if (block.data_len == 0) {
3455 /* We must have hit padding,
3456 * so give up processing now
3458 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: Hit 0-length block, giving up", __func__);
3459 goto done;
3462 data_len = data_len - block.data_len;
3464 if (unicode_string_equals (block.key, "VarFileInfo")) {
3465 data_ptr = get_varfileinfo_block (data_ptr, &block);
3466 if (want_var) {
3467 *buffer = (gpointer)data_ptr;
3468 *len = block.value_len;
3469 ret = TRUE;
3470 goto done;
3471 } else {
3472 /* Skip over the Var block */
3473 data_ptr = ((guchar *)data_ptr) + block.value_len;
3475 } else if (unicode_string_equals (block.key, "StringFileInfo")) {
3476 data_ptr = get_stringtable_block (data_ptr, lang_utf8, string_key, &string_value, &string_value_len, &block);
3477 if (want_string &&
3478 string_value != NULL &&
3479 string_value_len != 0) {
3480 *buffer = string_value;
3481 *len = unicode_chars ((const gunichar2 *)string_value) + 1; /* Include trailing null */
3482 ret = TRUE;
3483 goto done;
3485 } else {
3486 /* Bogus data */
3487 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: Not a valid VERSIONINFO child block", __func__);
3488 goto done;
3491 if (data_ptr == NULL) {
3492 /* Child block hit padding */
3493 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: Child block hit 0-length block, giving up", __func__);
3494 goto done;
3500 done:
3501 if (lang_utf8) {
3502 g_free (lang_utf8);
3505 g_free (subblock_utf8);
3506 return(ret);
3509 static guint32
3510 copy_lang (gunichar2 *lang_out, guint32 lang_len, const gchar *text)
3512 gunichar2 *unitext;
3513 int chars = strlen (text);
3514 int ret;
3516 unitext = g_utf8_to_utf16 (text, -1, NULL, NULL, NULL);
3517 g_assert (unitext != NULL);
3519 if (chars < (lang_len - 1)) {
3520 memcpy (lang_out, (gpointer)unitext, chars * 2);
3521 lang_out[chars] = '\0';
3522 ret = chars;
3523 } else {
3524 memcpy (lang_out, (gpointer)unitext, (lang_len - 1) * 2);
3525 lang_out[lang_len] = '\0';
3526 ret = lang_len;
3529 g_free (unitext);
3531 return(ret);
3534 guint32
3535 mono_w32process_ver_language_name (guint32 lang, gunichar2 *lang_out, guint32 lang_len)
3537 int primary, secondary;
3538 const char *name = NULL;
3540 primary = lang & 0x3FF;
3541 secondary = (lang >> 10) & 0x3F;
3543 switch(primary) {
3544 case 0x00:
3545 switch (secondary) {
3546 case 0x01: name = "Process Default Language"; break;
3548 break;
3549 case 0x01:
3550 switch (secondary) {
3551 case 0x00:
3552 case 0x01: name = "Arabic (Saudi Arabia)"; break;
3553 case 0x02: name = "Arabic (Iraq)"; break;
3554 case 0x03: name = "Arabic (Egypt)"; break;
3555 case 0x04: name = "Arabic (Libya)"; break;
3556 case 0x05: name = "Arabic (Algeria)"; break;
3557 case 0x06: name = "Arabic (Morocco)"; break;
3558 case 0x07: name = "Arabic (Tunisia)"; break;
3559 case 0x08: name = "Arabic (Oman)"; break;
3560 case 0x09: name = "Arabic (Yemen)"; break;
3561 case 0x0a: name = "Arabic (Syria)"; break;
3562 case 0x0b: name = "Arabic (Jordan)"; break;
3563 case 0x0c: name = "Arabic (Lebanon)"; break;
3564 case 0x0d: name = "Arabic (Kuwait)"; break;
3565 case 0x0e: name = "Arabic (U.A.E.)"; break;
3566 case 0x0f: name = "Arabic (Bahrain)"; break;
3567 case 0x10: name = "Arabic (Qatar)"; break;
3569 break;
3570 case 0x02:
3571 switch (secondary) {
3572 case 0x00: name = "Bulgarian (Bulgaria)"; break;
3573 case 0x01: name = "Bulgarian"; break;
3575 break;
3576 case 0x03:
3577 switch (secondary) {
3578 case 0x00: name = "Catalan (Spain)"; break;
3579 case 0x01: name = "Catalan"; break;
3581 break;
3582 case 0x04:
3583 switch (secondary) {
3584 case 0x00:
3585 case 0x01: name = "Chinese (Taiwan)"; break;
3586 case 0x02: name = "Chinese (PRC)"; break;
3587 case 0x03: name = "Chinese (Hong Kong S.A.R.)"; break;
3588 case 0x04: name = "Chinese (Singapore)"; break;
3589 case 0x05: name = "Chinese (Macau S.A.R.)"; break;
3591 break;
3592 case 0x05:
3593 switch (secondary) {
3594 case 0x00: name = "Czech (Czech Republic)"; break;
3595 case 0x01: name = "Czech"; break;
3597 break;
3598 case 0x06:
3599 switch (secondary) {
3600 case 0x00: name = "Danish (Denmark)"; break;
3601 case 0x01: name = "Danish"; break;
3603 break;
3604 case 0x07:
3605 switch (secondary) {
3606 case 0x00:
3607 case 0x01: name = "German (Germany)"; break;
3608 case 0x02: name = "German (Switzerland)"; break;
3609 case 0x03: name = "German (Austria)"; break;
3610 case 0x04: name = "German (Luxembourg)"; break;
3611 case 0x05: name = "German (Liechtenstein)"; break;
3613 break;
3614 case 0x08:
3615 switch (secondary) {
3616 case 0x00: name = "Greek (Greece)"; break;
3617 case 0x01: name = "Greek"; break;
3619 break;
3620 case 0x09:
3621 switch (secondary) {
3622 case 0x00:
3623 case 0x01: name = "English (United States)"; break;
3624 case 0x02: name = "English (United Kingdom)"; break;
3625 case 0x03: name = "English (Australia)"; break;
3626 case 0x04: name = "English (Canada)"; break;
3627 case 0x05: name = "English (New Zealand)"; break;
3628 case 0x06: name = "English (Ireland)"; break;
3629 case 0x07: name = "English (South Africa)"; break;
3630 case 0x08: name = "English (Jamaica)"; break;
3631 case 0x09: name = "English (Caribbean)"; break;
3632 case 0x0a: name = "English (Belize)"; break;
3633 case 0x0b: name = "English (Trinidad and Tobago)"; break;
3634 case 0x0c: name = "English (Zimbabwe)"; break;
3635 case 0x0d: name = "English (Philippines)"; break;
3636 case 0x10: name = "English (India)"; break;
3637 case 0x11: name = "English (Malaysia)"; break;
3638 case 0x12: name = "English (Singapore)"; break;
3640 break;
3641 case 0x0a:
3642 switch (secondary) {
3643 case 0x00: name = "Spanish (Spain)"; break;
3644 case 0x01: name = "Spanish (Traditional Sort)"; break;
3645 case 0x02: name = "Spanish (Mexico)"; break;
3646 case 0x03: name = "Spanish (International Sort)"; break;
3647 case 0x04: name = "Spanish (Guatemala)"; break;
3648 case 0x05: name = "Spanish (Costa Rica)"; break;
3649 case 0x06: name = "Spanish (Panama)"; break;
3650 case 0x07: name = "Spanish (Dominican Republic)"; break;
3651 case 0x08: name = "Spanish (Venezuela)"; break;
3652 case 0x09: name = "Spanish (Colombia)"; break;
3653 case 0x0a: name = "Spanish (Peru)"; break;
3654 case 0x0b: name = "Spanish (Argentina)"; break;
3655 case 0x0c: name = "Spanish (Ecuador)"; break;
3656 case 0x0d: name = "Spanish (Chile)"; break;
3657 case 0x0e: name = "Spanish (Uruguay)"; break;
3658 case 0x0f: name = "Spanish (Paraguay)"; break;
3659 case 0x10: name = "Spanish (Bolivia)"; break;
3660 case 0x11: name = "Spanish (El Salvador)"; break;
3661 case 0x12: name = "Spanish (Honduras)"; break;
3662 case 0x13: name = "Spanish (Nicaragua)"; break;
3663 case 0x14: name = "Spanish (Puerto Rico)"; break;
3664 case 0x15: name = "Spanish (United States)"; break;
3666 break;
3667 case 0x0b:
3668 switch (secondary) {
3669 case 0x00: name = "Finnish (Finland)"; break;
3670 case 0x01: name = "Finnish"; break;
3672 break;
3673 case 0x0c:
3674 switch (secondary) {
3675 case 0x00:
3676 case 0x01: name = "French (France)"; break;
3677 case 0x02: name = "French (Belgium)"; break;
3678 case 0x03: name = "French (Canada)"; break;
3679 case 0x04: name = "French (Switzerland)"; break;
3680 case 0x05: name = "French (Luxembourg)"; break;
3681 case 0x06: name = "French (Monaco)"; break;
3683 break;
3684 case 0x0d:
3685 switch (secondary) {
3686 case 0x00: name = "Hebrew (Israel)"; break;
3687 case 0x01: name = "Hebrew"; break;
3689 break;
3690 case 0x0e:
3691 switch (secondary) {
3692 case 0x00: name = "Hungarian (Hungary)"; break;
3693 case 0x01: name = "Hungarian"; break;
3695 break;
3696 case 0x0f:
3697 switch (secondary) {
3698 case 0x00: name = "Icelandic (Iceland)"; break;
3699 case 0x01: name = "Icelandic"; break;
3701 break;
3702 case 0x10:
3703 switch (secondary) {
3704 case 0x00:
3705 case 0x01: name = "Italian (Italy)"; break;
3706 case 0x02: name = "Italian (Switzerland)"; break;
3708 break;
3709 case 0x11:
3710 switch (secondary) {
3711 case 0x00: name = "Japanese (Japan)"; break;
3712 case 0x01: name = "Japanese"; break;
3714 break;
3715 case 0x12:
3716 switch (secondary) {
3717 case 0x00: name = "Korean (Korea)"; break;
3718 case 0x01: name = "Korean"; break;
3720 break;
3721 case 0x13:
3722 switch (secondary) {
3723 case 0x00:
3724 case 0x01: name = "Dutch (Netherlands)"; break;
3725 case 0x02: name = "Dutch (Belgium)"; break;
3727 break;
3728 case 0x14:
3729 switch (secondary) {
3730 case 0x00:
3731 case 0x01: name = "Norwegian (Bokmal)"; break;
3732 case 0x02: name = "Norwegian (Nynorsk)"; break;
3734 break;
3735 case 0x15:
3736 switch (secondary) {
3737 case 0x00: name = "Polish (Poland)"; break;
3738 case 0x01: name = "Polish"; break;
3740 break;
3741 case 0x16:
3742 switch (secondary) {
3743 case 0x00:
3744 case 0x01: name = "Portuguese (Brazil)"; break;
3745 case 0x02: name = "Portuguese (Portugal)"; break;
3747 break;
3748 case 0x17:
3749 switch (secondary) {
3750 case 0x01: name = "Romansh (Switzerland)"; break;
3752 break;
3753 case 0x18:
3754 switch (secondary) {
3755 case 0x00: name = "Romanian (Romania)"; break;
3756 case 0x01: name = "Romanian"; break;
3758 break;
3759 case 0x19:
3760 switch (secondary) {
3761 case 0x00: name = "Russian (Russia)"; break;
3762 case 0x01: name = "Russian"; break;
3764 break;
3765 case 0x1a:
3766 switch (secondary) {
3767 case 0x00: name = "Croatian (Croatia)"; break;
3768 case 0x01: name = "Croatian"; break;
3769 case 0x02: name = "Serbian (Latin)"; break;
3770 case 0x03: name = "Serbian (Cyrillic)"; break;
3771 case 0x04: name = "Croatian (Bosnia and Herzegovina)"; break;
3772 case 0x05: name = "Bosnian (Latin, Bosnia and Herzegovina)"; break;
3773 case 0x06: name = "Serbian (Latin, Bosnia and Herzegovina)"; break;
3774 case 0x07: name = "Serbian (Cyrillic, Bosnia and Herzegovina)"; break;
3775 case 0x08: name = "Bosnian (Cyrillic, Bosnia and Herzegovina)"; break;
3777 break;
3778 case 0x1b:
3779 switch (secondary) {
3780 case 0x00: name = "Slovak (Slovakia)"; break;
3781 case 0x01: name = "Slovak"; break;
3783 break;
3784 case 0x1c:
3785 switch (secondary) {
3786 case 0x00: name = "Albanian (Albania)"; break;
3787 case 0x01: name = "Albanian"; break;
3789 break;
3790 case 0x1d:
3791 switch (secondary) {
3792 case 0x00: name = "Swedish (Sweden)"; break;
3793 case 0x01: name = "Swedish"; break;
3794 case 0x02: name = "Swedish (Finland)"; break;
3796 break;
3797 case 0x1e:
3798 switch (secondary) {
3799 case 0x00: name = "Thai (Thailand)"; break;
3800 case 0x01: name = "Thai"; break;
3802 break;
3803 case 0x1f:
3804 switch (secondary) {
3805 case 0x00: name = "Turkish (Turkey)"; break;
3806 case 0x01: name = "Turkish"; break;
3808 break;
3809 case 0x20:
3810 switch (secondary) {
3811 case 0x00: name = "Urdu (Islamic Republic of Pakistan)"; break;
3812 case 0x01: name = "Urdu"; break;
3814 break;
3815 case 0x21:
3816 switch (secondary) {
3817 case 0x00: name = "Indonesian (Indonesia)"; break;
3818 case 0x01: name = "Indonesian"; break;
3820 break;
3821 case 0x22:
3822 switch (secondary) {
3823 case 0x00: name = "Ukrainian (Ukraine)"; break;
3824 case 0x01: name = "Ukrainian"; break;
3826 break;
3827 case 0x23:
3828 switch (secondary) {
3829 case 0x00: name = "Belarusian (Belarus)"; break;
3830 case 0x01: name = "Belarusian"; break;
3832 break;
3833 case 0x24:
3834 switch (secondary) {
3835 case 0x00: name = "Slovenian (Slovenia)"; break;
3836 case 0x01: name = "Slovenian"; break;
3838 break;
3839 case 0x25:
3840 switch (secondary) {
3841 case 0x00: name = "Estonian (Estonia)"; break;
3842 case 0x01: name = "Estonian"; break;
3844 break;
3845 case 0x26:
3846 switch (secondary) {
3847 case 0x00: name = "Latvian (Latvia)"; break;
3848 case 0x01: name = "Latvian"; break;
3850 break;
3851 case 0x27:
3852 switch (secondary) {
3853 case 0x00: name = "Lithuanian (Lithuania)"; break;
3854 case 0x01: name = "Lithuanian"; break;
3856 break;
3857 case 0x28:
3858 switch (secondary) {
3859 case 0x01: name = "Tajik (Tajikistan)"; break;
3861 break;
3862 case 0x29:
3863 switch (secondary) {
3864 case 0x00: name = "Farsi (Iran)"; break;
3865 case 0x01: name = "Farsi"; break;
3867 break;
3868 case 0x2a:
3869 switch (secondary) {
3870 case 0x00: name = "Vietnamese (Viet Nam)"; break;
3871 case 0x01: name = "Vietnamese"; break;
3873 break;
3874 case 0x2b:
3875 switch (secondary) {
3876 case 0x00: name = "Armenian (Armenia)"; break;
3877 case 0x01: name = "Armenian"; break;
3879 break;
3880 case 0x2c:
3881 switch (secondary) {
3882 case 0x00: name = "Azeri (Latin) (Azerbaijan)"; break;
3883 case 0x01: name = "Azeri (Latin)"; break;
3884 case 0x02: name = "Azeri (Cyrillic)"; break;
3886 break;
3887 case 0x2d:
3888 switch (secondary) {
3889 case 0x00: name = "Basque (Spain)"; break;
3890 case 0x01: name = "Basque"; break;
3892 break;
3893 case 0x2e:
3894 switch (secondary) {
3895 case 0x01: name = "Upper Sorbian (Germany)"; break;
3896 case 0x02: name = "Lower Sorbian (Germany)"; break;
3898 break;
3899 case 0x2f:
3900 switch (secondary) {
3901 case 0x00: name = "FYRO Macedonian (Former Yugoslav Republic of Macedonia)"; break;
3902 case 0x01: name = "FYRO Macedonian"; break;
3904 break;
3905 case 0x32:
3906 switch (secondary) {
3907 case 0x00: name = "Tswana (South Africa)"; break;
3908 case 0x01: name = "Tswana"; break;
3910 break;
3911 case 0x34:
3912 switch (secondary) {
3913 case 0x00: name = "Xhosa (South Africa)"; break;
3914 case 0x01: name = "Xhosa"; break;
3916 break;
3917 case 0x35:
3918 switch (secondary) {
3919 case 0x00: name = "Zulu (South Africa)"; break;
3920 case 0x01: name = "Zulu"; break;
3922 break;
3923 case 0x36:
3924 switch (secondary) {
3925 case 0x00: name = "Afrikaans (South Africa)"; break;
3926 case 0x01: name = "Afrikaans"; break;
3928 break;
3929 case 0x37:
3930 switch (secondary) {
3931 case 0x00: name = "Georgian (Georgia)"; break;
3932 case 0x01: name = "Georgian"; break;
3934 break;
3935 case 0x38:
3936 switch (secondary) {
3937 case 0x00: name = "Faroese (Faroe Islands)"; break;
3938 case 0x01: name = "Faroese"; break;
3940 break;
3941 case 0x39:
3942 switch (secondary) {
3943 case 0x00: name = "Hindi (India)"; break;
3944 case 0x01: name = "Hindi"; break;
3946 break;
3947 case 0x3a:
3948 switch (secondary) {
3949 case 0x00: name = "Maltese (Malta)"; break;
3950 case 0x01: name = "Maltese"; break;
3952 break;
3953 case 0x3b:
3954 switch (secondary) {
3955 case 0x00: name = "Sami (Northern) (Norway)"; break;
3956 case 0x01: name = "Sami, Northern (Norway)"; break;
3957 case 0x02: name = "Sami, Northern (Sweden)"; break;
3958 case 0x03: name = "Sami, Northern (Finland)"; break;
3959 case 0x04: name = "Sami, Lule (Norway)"; break;
3960 case 0x05: name = "Sami, Lule (Sweden)"; break;
3961 case 0x06: name = "Sami, Southern (Norway)"; break;
3962 case 0x07: name = "Sami, Southern (Sweden)"; break;
3963 case 0x08: name = "Sami, Skolt (Finland)"; break;
3964 case 0x09: name = "Sami, Inari (Finland)"; break;
3966 break;
3967 case 0x3c:
3968 switch (secondary) {
3969 case 0x02: name = "Irish (Ireland)"; break;
3971 break;
3972 case 0x3e:
3973 switch (secondary) {
3974 case 0x00:
3975 case 0x01: name = "Malay (Malaysia)"; break;
3976 case 0x02: name = "Malay (Brunei Darussalam)"; break;
3978 break;
3979 case 0x3f:
3980 switch (secondary) {
3981 case 0x00: name = "Kazakh (Kazakhstan)"; break;
3982 case 0x01: name = "Kazakh"; break;
3984 break;
3985 case 0x40:
3986 switch (secondary) {
3987 case 0x00: name = "Kyrgyz (Kyrgyzstan)"; break;
3988 case 0x01: name = "Kyrgyz (Cyrillic)"; break;
3990 break;
3991 case 0x41:
3992 switch (secondary) {
3993 case 0x00: name = "Swahili (Kenya)"; break;
3994 case 0x01: name = "Swahili"; break;
3996 break;
3997 case 0x42:
3998 switch (secondary) {
3999 case 0x01: name = "Turkmen (Turkmenistan)"; break;
4001 break;
4002 case 0x43:
4003 switch (secondary) {
4004 case 0x00: name = "Uzbek (Latin) (Uzbekistan)"; break;
4005 case 0x01: name = "Uzbek (Latin)"; break;
4006 case 0x02: name = "Uzbek (Cyrillic)"; break;
4008 break;
4009 case 0x44:
4010 switch (secondary) {
4011 case 0x00: name = "Tatar (Russia)"; break;
4012 case 0x01: name = "Tatar"; break;
4014 break;
4015 case 0x45:
4016 switch (secondary) {
4017 case 0x00:
4018 case 0x01: name = "Bengali (India)"; break;
4020 break;
4021 case 0x46:
4022 switch (secondary) {
4023 case 0x00: name = "Punjabi (India)"; break;
4024 case 0x01: name = "Punjabi"; break;
4026 break;
4027 case 0x47:
4028 switch (secondary) {
4029 case 0x00: name = "Gujarati (India)"; break;
4030 case 0x01: name = "Gujarati"; break;
4032 break;
4033 case 0x49:
4034 switch (secondary) {
4035 case 0x00: name = "Tamil (India)"; break;
4036 case 0x01: name = "Tamil"; break;
4038 break;
4039 case 0x4a:
4040 switch (secondary) {
4041 case 0x00: name = "Telugu (India)"; break;
4042 case 0x01: name = "Telugu"; break;
4044 break;
4045 case 0x4b:
4046 switch (secondary) {
4047 case 0x00: name = "Kannada (India)"; break;
4048 case 0x01: name = "Kannada"; break;
4050 break;
4051 case 0x4c:
4052 switch (secondary) {
4053 case 0x00:
4054 case 0x01: name = "Malayalam (India)"; break;
4056 break;
4057 case 0x4d:
4058 switch (secondary) {
4059 case 0x01: name = "Assamese (India)"; break;
4061 break;
4062 case 0x4e:
4063 switch (secondary) {
4064 case 0x00: name = "Marathi (India)"; break;
4065 case 0x01: name = "Marathi"; break;
4067 break;
4068 case 0x4f:
4069 switch (secondary) {
4070 case 0x00: name = "Sanskrit (India)"; break;
4071 case 0x01: name = "Sanskrit"; break;
4073 break;
4074 case 0x50:
4075 switch (secondary) {
4076 case 0x00: name = "Mongolian (Mongolia)"; break;
4077 case 0x01: name = "Mongolian (Cyrillic)"; break;
4078 case 0x02: name = "Mongolian (PRC)"; break;
4080 break;
4081 case 0x51:
4082 switch (secondary) {
4083 case 0x01: name = "Tibetan (PRC)"; break;
4084 case 0x02: name = "Tibetan (Bhutan)"; break;
4086 break;
4087 case 0x52:
4088 switch (secondary) {
4089 case 0x00: name = "Welsh (United Kingdom)"; break;
4090 case 0x01: name = "Welsh"; break;
4092 break;
4093 case 0x53:
4094 switch (secondary) {
4095 case 0x01: name = "Khmer (Cambodia)"; break;
4097 break;
4098 case 0x54:
4099 switch (secondary) {
4100 case 0x01: name = "Lao (Lao PDR)"; break;
4102 break;
4103 case 0x56:
4104 switch (secondary) {
4105 case 0x00: name = "Galician (Spain)"; break;
4106 case 0x01: name = "Galician"; break;
4108 break;
4109 case 0x57:
4110 switch (secondary) {
4111 case 0x00: name = "Konkani (India)"; break;
4112 case 0x01: name = "Konkani"; break;
4114 break;
4115 case 0x5a:
4116 switch (secondary) {
4117 case 0x00: name = "Syriac (Syria)"; break;
4118 case 0x01: name = "Syriac"; break;
4120 break;
4121 case 0x5b:
4122 switch (secondary) {
4123 case 0x01: name = "Sinhala (Sri Lanka)"; break;
4125 break;
4126 case 0x5d:
4127 switch (secondary) {
4128 case 0x01: name = "Inuktitut (Syllabics, Canada)"; break;
4129 case 0x02: name = "Inuktitut (Latin, Canada)"; break;
4131 break;
4132 case 0x5e:
4133 switch (secondary) {
4134 case 0x01: name = "Amharic (Ethiopia)"; break;
4136 break;
4137 case 0x5f:
4138 switch (secondary) {
4139 case 0x02: name = "Tamazight (Algeria, Latin)"; break;
4141 break;
4142 case 0x61:
4143 switch (secondary) {
4144 case 0x01: name = "Nepali (Nepal)"; break;
4146 break;
4147 case 0x62:
4148 switch (secondary) {
4149 case 0x01: name = "Frisian (Netherlands)"; break;
4151 break;
4152 case 0x63:
4153 switch (secondary) {
4154 case 0x01: name = "Pashto (Afghanistan)"; break;
4156 break;
4157 case 0x64:
4158 switch (secondary) {
4159 case 0x01: name = "Filipino (Philippines)"; break;
4161 break;
4162 case 0x65:
4163 switch (secondary) {
4164 case 0x00: name = "Divehi (Maldives)"; break;
4165 case 0x01: name = "Divehi"; break;
4167 break;
4168 case 0x68:
4169 switch (secondary) {
4170 case 0x01: name = "Hausa (Nigeria, Latin)"; break;
4172 break;
4173 case 0x6a:
4174 switch (secondary) {
4175 case 0x01: name = "Yoruba (Nigeria)"; break;
4177 break;
4178 case 0x6b:
4179 switch (secondary) {
4180 case 0x00:
4181 case 0x01: name = "Quechua (Bolivia)"; break;
4182 case 0x02: name = "Quechua (Ecuador)"; break;
4183 case 0x03: name = "Quechua (Peru)"; break;
4185 break;
4186 case 0x6c:
4187 switch (secondary) {
4188 case 0x00: name = "Northern Sotho (South Africa)"; break;
4189 case 0x01: name = "Northern Sotho"; break;
4191 break;
4192 case 0x6d:
4193 switch (secondary) {
4194 case 0x01: name = "Bashkir (Russia)"; break;
4196 break;
4197 case 0x6e:
4198 switch (secondary) {
4199 case 0x01: name = "Luxembourgish (Luxembourg)"; break;
4201 break;
4202 case 0x6f:
4203 switch (secondary) {
4204 case 0x01: name = "Greenlandic (Greenland)"; break;
4206 break;
4207 case 0x78:
4208 switch (secondary) {
4209 case 0x01: name = "Yi (PRC)"; break;
4211 break;
4212 case 0x7a:
4213 switch (secondary) {
4214 case 0x01: name = "Mapudungun (Chile)"; break;
4216 break;
4217 case 0x7c:
4218 switch (secondary) {
4219 case 0x01: name = "Mohawk (Mohawk)"; break;
4221 break;
4222 case 0x7e:
4223 switch (secondary) {
4224 case 0x01: name = "Breton (France)"; break;
4226 break;
4227 case 0x7f:
4228 switch (secondary) {
4229 case 0x00: name = "Invariant Language (Invariant Country)"; break;
4231 break;
4232 case 0x80:
4233 switch (secondary) {
4234 case 0x01: name = "Uighur (PRC)"; break;
4236 break;
4237 case 0x81:
4238 switch (secondary) {
4239 case 0x00: name = "Maori (New Zealand)"; break;
4240 case 0x01: name = "Maori"; break;
4242 break;
4243 case 0x83:
4244 switch (secondary) {
4245 case 0x01: name = "Corsican (France)"; break;
4247 break;
4248 case 0x84:
4249 switch (secondary) {
4250 case 0x01: name = "Alsatian (France)"; break;
4252 break;
4253 case 0x85:
4254 switch (secondary) {
4255 case 0x01: name = "Yakut (Russia)"; break;
4257 break;
4258 case 0x86:
4259 switch (secondary) {
4260 case 0x01: name = "K'iche (Guatemala)"; break;
4262 break;
4263 case 0x87:
4264 switch (secondary) {
4265 case 0x01: name = "Kinyarwanda (Rwanda)"; break;
4267 break;
4268 case 0x88:
4269 switch (secondary) {
4270 case 0x01: name = "Wolof (Senegal)"; break;
4272 break;
4273 case 0x8c:
4274 switch (secondary) {
4275 case 0x01: name = "Dari (Afghanistan)"; break;
4277 break;
4279 default:
4280 name = "Language Neutral";
4284 if (!name)
4285 name = "Language Neutral";
4287 return copy_lang (lang_out, lang_len, name);
4290 #else /* ENABLE_NETCORE */
4292 void
4293 mono_w32process_init (void)
4297 void
4298 mono_w32process_cleanup (void)
4302 void
4303 mono_w32process_set_cli_launcher (gchar *path)
4307 void
4308 mono_w32process_signal_finished (void)
4312 #endif /* ENABLE_NETCORE */