2 * processes.c: Process handles
5 * Dick Porter (dick@ximian.com)
7 * (C) 2002-2011 Novell, Inc.
8 * Copyright 2011 Xamarin Inc
9 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
20 #include <sys/types.h>
28 #ifdef HAVE_SYS_PARAM_H
29 #include <sys/param.h>
33 #ifdef HAVE_SYS_WAIT_H
36 #ifdef HAVE_SYS_RESOURCE_H
37 #include <sys/resource.h>
40 #ifdef HAVE_SYS_MKDEV_H
41 #include <sys/mkdev.h>
48 /* sys/resource.h (for rusage) is required when using osx 10.3 (but not 10.4) */
50 #include <TargetConditionals.h>
51 #include <sys/resource.h>
58 #if defined(PLATFORM_MACOSX)
59 #define USE_OSX_LOADER
62 #if ( defined(__OpenBSD__) || defined(__FreeBSD__) ) && defined(HAVE_LINK_H)
63 #define USE_BSD_LOADER
66 #if defined(__HAIKU__)
67 #define USE_HAIKU_LOADER
70 #if defined(USE_OSX_LOADER) || defined(USE_BSD_LOADER)
72 #include <sys/sysctl.h>
73 # if !defined(__OpenBSD__)
74 # include <sys/utsname.h>
76 # if defined(__FreeBSD__)
77 # include <sys/user.h> /* struct kinfo_proc */
81 #ifdef PLATFORM_SOLARIS
82 /* procfs.h cannot be included if this define is set, but it seems to work fine if it is undefined */
83 #if _FILE_OFFSET_BITS == 64
84 #undef _FILE_OFFSET_BITS
86 #define _FILE_OFFSET_BITS 64
93 #include <KernelKit.h>
96 #include <mono/io-layer/wapi.h>
97 #include <mono/io-layer/wapi-private.h>
98 #include <mono/io-layer/process-private.h>
99 #include <mono/io-layer/io-trace.h>
100 #include <mono/utils/strenc.h>
101 #include <mono/utils/mono-path.h>
102 #include <mono/io-layer/timefuncs-private.h>
103 #include <mono/utils/mono-time.h>
104 #include <mono/utils/mono-membar.h>
105 #include <mono/utils/mono-os-mutex.h>
106 #include <mono/utils/mono-signal-handler.h>
107 #include <mono/utils/mono-proclib.h>
108 #include <mono/utils/mono-once.h>
109 #include <mono/utils/mono-logger-internals.h>
110 #include <mono/metadata/w32handle.h>
112 #define STILL_ACTIVE STATUS_PENDING
114 /* The process' environment strings */
115 #if defined(__APPLE__)
116 #if defined (TARGET_OSX)
117 /* Apple defines this in crt_externs.h but doesn't provide that header for
118 * arm-apple-darwin9. We'll manually define the symbol on Apple as it does
119 * in fact exist on all implementations (so far)
121 gchar ***_NSGetEnviron(void);
122 #define environ (*_NSGetEnviron())
124 static char *mono_environ[1] = { NULL };
125 #define environ mono_environ
126 #endif /* defined (TARGET_OSX) */
128 extern char **environ;
131 static guint32 process_wait (gpointer handle, guint32 timeout, gboolean *alerted);
132 static void process_close (gpointer handle, gpointer data);
133 static void process_details (gpointer data);
134 static const gchar* process_typename (void);
135 static gsize process_typesize (void);
136 static gboolean is_pid_valid (pid_t pid);
138 #if !(defined(USE_OSX_LOADER) || defined(USE_BSD_LOADER) || defined(USE_HAIKU_LOADER))
140 open_process_map (int pid, const char *mode);
143 static MonoW32HandleOps _wapi_process_ops = {
144 process_close, /* close_shared */
148 process_wait, /* special_wait */
150 process_details, /* details */
151 process_typename, /* typename */
152 process_typesize, /* typesize */
156 static struct sigaction previous_chld_sa;
158 static mono_once_t process_sig_chld_once = MONO_ONCE_INIT;
159 static void process_add_sigchld_handler (void);
161 /* The signal-safe logic to use mono_processes goes like this:
162 * - The list must be safe to traverse for the signal handler at all times.
163 * It's safe to: prepend an entry (which is a single store to 'mono_processes'),
164 * unlink an entry (assuming the unlinked entry isn't freed and doesn't
165 * change its 'next' pointer so that it can still be traversed).
166 * When cleaning up we first unlink an entry, then we verify that
167 * the read lock isn't locked. Then we can free the entry, since
168 * we know that nobody is using the old version of the list (including
169 * the unlinked entry).
170 * We also need to lock when adding and cleaning up so that those two
171 * operations don't mess with eachother. (This lock is not used in the
174 static struct MonoProcess *mono_processes = NULL;
175 static volatile gint32 mono_processes_cleaning_up = 0;
176 static mono_mutex_t mono_processes_mutex;
177 static void mono_processes_cleanup (void);
179 static gpointer current_process;
180 static char *cli_launcher;
182 static WapiHandle_process *
183 lookup_process_handle (gpointer handle)
185 WapiHandle_process *process_data;
188 ret = mono_w32handle_lookup (handle, MONO_W32HANDLE_PROCESS,
189 (gpointer *)&process_data);
195 /* Check if a pid is valid - i.e. if a process exists with this pid. */
197 is_pid_valid (pid_t pid)
199 gboolean result = FALSE;
201 #if defined(HOST_WATCHOS)
202 result = TRUE; // TODO: Rewrite using sysctl
203 #elif defined(PLATFORM_MACOSX) || defined(__OpenBSD__) || defined(__FreeBSD__)
204 if (((kill(pid, 0) == 0) || (errno == EPERM)) && pid != 0)
206 #elif defined(__HAIKU__)
208 if (get_team_info ((team_id)pid, &teamInfo) == B_OK)
211 char *dir = g_strdup_printf ("/proc/%d", pid);
212 if (!access (dir, F_OK))
221 process_set_defaults (WapiHandle_process *process_handle)
223 /* These seem to be the defaults on w2k */
224 process_handle->min_working_set = 204800;
225 process_handle->max_working_set = 1413120;
227 _wapi_time_t_to_filetime (time (NULL), &process_handle->create_time);
231 len16 (const gunichar2 *str)
242 utf16_concat (const gunichar2 *first, ...)
249 va_start (args, first);
250 total += len16 (first);
251 for (s = va_arg (args, gunichar2 *); s != NULL; s = va_arg(args, gunichar2 *)){
256 ret = g_new (gunichar2, total + 1);
262 for (s = first; *s != 0; s++)
264 va_start (args, first);
265 for (s = va_arg (args, gunichar2 *); s != NULL; s = va_arg (args, gunichar2 *)){
268 for (p = s; *p != 0; p++)
276 static const gunichar2 utf16_space_bytes [2] = { 0x20, 0 };
277 static const gunichar2 *utf16_space = utf16_space_bytes;
278 static const gunichar2 utf16_quote_bytes [2] = { 0x22, 0 };
279 static const gunichar2 *utf16_quote = utf16_quote_bytes;
284 print_utf16 (gunichar2 *str)
288 res = g_utf16_to_utf8 (str, -1, NULL, NULL, NULL);
289 g_print ("%s\n", res);
294 /* Implemented as just a wrapper around CreateProcess () */
296 ShellExecuteEx (WapiShellExecuteInfo *sei)
299 WapiProcessInformation process_info;
303 /* w2k just segfaults here, but we can do better than
306 SetLastError (ERROR_INVALID_PARAMETER);
310 if (sei->lpFile == NULL)
311 /* w2k returns TRUE for this, for some reason. */
314 /* Put both executable and parameters into the second argument
315 * to CreateProcess (), so it searches $PATH. The conversion
316 * into and back out of utf8 is because there is no
317 * g_strdup_printf () equivalent for gunichar2 :-(
319 args = utf16_concat (utf16_quote, sei->lpFile, utf16_quote, sei->lpParameters == NULL ? NULL : utf16_space, sei->lpParameters, NULL);
321 SetLastError (ERROR_INVALID_DATA);
324 ret = CreateProcess (NULL, args, NULL, NULL, TRUE,
325 CREATE_UNICODE_ENVIRONMENT, NULL,
326 sei->lpDirectory, NULL, &process_info);
329 if (!ret && GetLastError () == ERROR_OUTOFMEMORY)
333 static char *handler;
334 static gunichar2 *handler_utf16;
336 if (handler_utf16 == (gunichar2 *)-1)
339 #ifdef PLATFORM_MACOSX
340 handler = g_strdup ("/usr/bin/open");
343 * On Linux, try: xdg-open, the FreeDesktop standard way of doing it,
344 * if that fails, try to use gnome-open, then kfmclient
346 handler = g_find_program_in_path ("xdg-open");
347 if (handler == NULL){
348 handler = g_find_program_in_path ("gnome-open");
349 if (handler == NULL){
350 handler = g_find_program_in_path ("kfmclient");
351 if (handler == NULL){
352 handler_utf16 = (gunichar2 *) -1;
355 /* kfmclient needs exec argument */
357 handler = g_strconcat (old, " exec",
364 handler_utf16 = g_utf8_to_utf16 (handler, -1, NULL, NULL, NULL);
367 /* Put quotes around the filename, in case it's a url
368 * that contains #'s (CreateProcess() calls
369 * g_shell_parse_argv(), which deliberately throws
370 * away anything after an unquoted #). Fixes bug
373 args = utf16_concat (handler_utf16, utf16_space, utf16_quote,
374 sei->lpFile, utf16_quote,
375 sei->lpParameters == NULL ? NULL : utf16_space,
376 sei->lpParameters, NULL);
378 SetLastError (ERROR_INVALID_DATA);
381 ret = CreateProcess (NULL, args, NULL, NULL, TRUE,
382 CREATE_UNICODE_ENVIRONMENT, NULL,
383 sei->lpDirectory, NULL, &process_info);
386 if (GetLastError () != ERROR_OUTOFMEMORY)
387 SetLastError (ERROR_INVALID_DATA);
390 /* Shell exec should not return a process handle when it spawned a GUI thing, like a browser. */
391 CloseHandle (process_info.hProcess);
392 process_info.hProcess = NULL;
395 if (sei->fMask & SEE_MASK_NOCLOSEPROCESS)
396 sei->hProcess = process_info.hProcess;
398 CloseHandle (process_info.hProcess);
404 is_managed_binary (const char *filename)
406 int original_errno = errno;
407 #if defined(HAVE_LARGE_FILE_SUPPORT) && defined(O_LARGEFILE)
408 int file = open (filename, O_RDONLY | O_LARGEFILE);
410 int file = open (filename, O_RDONLY);
413 unsigned char buffer[8];
414 off_t file_size, optional_header_offset;
415 off_t pe_header_offset, clr_header_offset;
416 gboolean managed = FALSE;
418 guint32 first_word, second_word, magic_number;
420 /* If we are unable to open the file, then we definitely
421 * can't say that it is managed. The child mono process
422 * probably wouldn't be able to open it anyway.
425 errno = original_errno;
429 /* Retrieve the length of the file for future sanity checks. */
430 file_size = lseek (file, 0, SEEK_END);
431 lseek (file, 0, SEEK_SET);
433 /* We know we need to read a header field at offset 60. */
437 num_read = read (file, buffer, 2);
439 if ((num_read != 2) || (buffer[0] != 'M') || (buffer[1] != 'Z'))
442 new_offset = lseek (file, 60, SEEK_SET);
444 if (new_offset != 60)
447 num_read = read (file, buffer, 4);
451 pe_header_offset = buffer[0]
456 if (pe_header_offset + 24 > file_size)
459 new_offset = lseek (file, pe_header_offset, SEEK_SET);
461 if (new_offset != pe_header_offset)
464 num_read = read (file, buffer, 4);
466 if ((num_read != 4) || (buffer[0] != 'P') || (buffer[1] != 'E') || (buffer[2] != 0) || (buffer[3] != 0))
470 * Verify that the header we want in the optional header data
471 * is present in this binary.
473 new_offset = lseek (file, pe_header_offset + 20, SEEK_SET);
475 if (new_offset != pe_header_offset + 20)
478 num_read = read (file, buffer, 2);
480 if ((num_read != 2) || ((buffer[0] | (buffer[1] << 8)) < 216))
483 optional_header_offset = pe_header_offset + 24;
485 /* Read the PE magic number */
486 new_offset = lseek (file, optional_header_offset, SEEK_SET);
488 if (new_offset != optional_header_offset)
491 num_read = read (file, buffer, 2);
496 magic_number = (buffer[0] | (buffer[1] << 8));
498 if (magic_number == 0x10B) // PE32
499 clr_header_offset = 208;
500 else if (magic_number == 0x20B) // PE32+
501 clr_header_offset = 224;
505 /* Read the CLR header address and size fields. These will be
506 * zero if the binary is not managed.
508 new_offset = lseek (file, optional_header_offset + clr_header_offset, SEEK_SET);
510 if (new_offset != optional_header_offset + clr_header_offset)
513 num_read = read (file, buffer, 8);
515 /* We are not concerned with endianness, only with
516 * whether it is zero or not.
518 first_word = *(guint32 *)&buffer[0];
519 second_word = *(guint32 *)&buffer[4];
521 if ((num_read != 8) || (first_word == 0) || (second_word == 0))
528 errno = original_errno;
533 CreateProcessWithLogonW (const gunichar2 *username,
534 const gunichar2 *domain,
535 const gunichar2 *password,
536 const guint32 logonFlags,
537 const gunichar2 *appname,
538 const gunichar2 *cmdline,
539 guint32 create_flags,
541 const gunichar2 *cwd,
542 WapiStartupInfo *startup,
543 WapiProcessInformation *process_info)
545 /* FIXME: use user information */
546 return CreateProcess (appname, cmdline, NULL, NULL, FALSE, create_flags, env, cwd, startup, process_info);
550 is_readable_or_executable (const char *prog)
553 int a = access (prog, R_OK);
554 int b = access (prog, X_OK);
555 if (a != 0 && b != 0)
557 if (stat (prog, &buf))
559 if (S_ISREG (buf.st_mode))
565 is_executable (const char *prog)
568 if (access (prog, X_OK) != 0)
570 if (stat (prog, &buf))
572 if (S_ISREG (buf.st_mode))
578 switch_dir_separators (char *path)
580 size_t i, pathLength = strlen(path);
582 /* Turn all the slashes round the right way, except for \' */
583 /* There are probably other characters that need to be excluded as well. */
584 for (i = 0; i < pathLength; i++) {
585 if (path[i] == '\\' && i < pathLength - 1 && path[i+1] != '\'' )
590 gboolean CreateProcess (const gunichar2 *appname, const gunichar2 *cmdline,
591 WapiSecurityAttributes *process_attrs G_GNUC_UNUSED,
592 WapiSecurityAttributes *thread_attrs G_GNUC_UNUSED,
593 gboolean inherit_handles, guint32 create_flags,
594 gpointer new_environ, const gunichar2 *cwd,
595 WapiStartupInfo *startup,
596 WapiProcessInformation *process_info)
598 #if defined (HAVE_FORK) && defined (HAVE_EXECVE)
599 char *cmd = NULL, *prog = NULL, *full_prog = NULL, *args = NULL, *args_after_prog = NULL;
600 char *dir = NULL, **env_strings = NULL, **argv = NULL;
601 guint32 i, env_count = 0;
602 gboolean ret = FALSE;
603 gpointer handle = NULL;
604 WapiHandle_process process_handle = {0}, *process_handle_data;
606 int in_fd, out_fd, err_fd;
608 int startup_pipe [2] = {-1, -1};
610 struct MonoProcess *mono_process;
611 gboolean fork_failed = FALSE;
613 mono_once (&process_sig_chld_once, process_add_sigchld_handler);
615 /* appname and cmdline specify the executable and its args:
617 * If appname is not NULL, it is the name of the executable.
618 * Otherwise the executable is the first token in cmdline.
620 * Executable searching:
622 * If appname is not NULL, it can specify the full path and
623 * file name, or else a partial name and the current directory
624 * will be used. There is no additional searching.
626 * If appname is NULL, the first whitespace-delimited token in
627 * cmdline is used. If the name does not contain a full
628 * directory path, the search sequence is:
630 * 1) The directory containing the current process
631 * 2) The current working directory
632 * 3) The windows system directory (Ignored)
633 * 4) The windows directory (Ignored)
636 * Just to make things more interesting, tokens can contain
637 * white space if they are surrounded by quotation marks. I'm
638 * beginning to understand just why windows apps are generally
639 * so crap, with an API like this :-(
641 if (appname != NULL) {
642 cmd = mono_unicode_to_external (appname);
644 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: unicode conversion returned NULL",
647 SetLastError (ERROR_PATH_NOT_FOUND);
651 switch_dir_separators(cmd);
654 if (cmdline != NULL) {
655 args = mono_unicode_to_external (cmdline);
657 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: unicode conversion returned NULL", __func__);
659 SetLastError (ERROR_PATH_NOT_FOUND);
665 dir = mono_unicode_to_external (cwd);
667 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: unicode conversion returned NULL", __func__);
669 SetLastError (ERROR_PATH_NOT_FOUND);
673 /* Turn all the slashes round the right way */
674 switch_dir_separators(dir);
678 /* We can't put off locating the executable any longer :-( */
681 if (g_ascii_isalpha (cmd[0]) && (cmd[1] == ':')) {
682 /* Strip off the drive letter. I can't
683 * believe that CP/M holdover is still
686 g_memmove (cmd, cmd+2, strlen (cmd)-2);
687 cmd[strlen (cmd)-2] = '\0';
690 unquoted = g_shell_unquote (cmd, NULL);
691 if (unquoted[0] == '/') {
692 /* Assume full path given */
693 prog = g_strdup (unquoted);
695 /* Executable existing ? */
696 if (!is_readable_or_executable (prog)) {
697 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Couldn't find executable %s",
700 SetLastError (ERROR_FILE_NOT_FOUND);
704 /* Search for file named by cmd in the current
707 char *curdir = g_get_current_dir ();
709 prog = g_strdup_printf ("%s/%s", curdir, unquoted);
712 /* And make sure it's readable */
713 if (!is_readable_or_executable (prog)) {
714 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Couldn't find executable %s",
717 SetLastError (ERROR_FILE_NOT_FOUND);
723 args_after_prog = args;
728 /* Dig out the first token from args, taking quotation
732 /* First, strip off all leading whitespace */
733 args = g_strchug (args);
735 /* args_after_prog points to the contents of args
736 * after token has been set (otherwise argv[0] is
739 args_after_prog = args;
741 /* Assume the opening quote will always be the first
744 if (args[0] == '\"' || args [0] == '\'') {
746 for (i = 1; args[i] != '\0' && args[i] != quote; i++);
747 if (args [i + 1] == '\0' || g_ascii_isspace (args[i+1])) {
748 /* We found the first token */
749 token = g_strndup (args+1, i-1);
750 args_after_prog = g_strchug (args + i + 1);
752 /* Quotation mark appeared in the
753 * middle of the token. Just give the
754 * whole first token, quotes and all,
761 /* No quote mark, or malformed */
762 for (i = 0; args[i] != '\0'; i++) {
763 if (g_ascii_isspace (args[i])) {
764 token = g_strndup (args, i);
765 args_after_prog = args + i + 1;
771 if (token == NULL && args[0] != '\0') {
772 /* Must be just one token in the string */
773 token = g_strdup (args);
774 args_after_prog = NULL;
779 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Couldn't find what to exec", __func__);
781 SetLastError (ERROR_PATH_NOT_FOUND);
785 /* Turn all the slashes round the right way. Only for
788 switch_dir_separators(token);
790 if (g_ascii_isalpha (token[0]) && (token[1] == ':')) {
791 /* Strip off the drive letter. I can't
792 * believe that CP/M holdover is still
795 g_memmove (token, token+2, strlen (token)-2);
796 token[strlen (token)-2] = '\0';
799 if (token[0] == '/') {
800 /* Assume full path given */
801 prog = g_strdup (token);
803 /* Executable existing ? */
804 if (!is_readable_or_executable (prog)) {
805 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Couldn't find executable %s",
808 SetLastError (ERROR_FILE_NOT_FOUND);
812 char *curdir = g_get_current_dir ();
814 /* FIXME: Need to record the directory
815 * containing the current process, and check
816 * that for the new executable as the first
820 prog = g_strdup_printf ("%s/%s", curdir, token);
823 /* I assume X_OK is the criterion to use,
826 * X_OK is too strict *if* the target is a CLR binary
828 if (!is_readable_or_executable (prog)) {
830 prog = g_find_program_in_path (token);
832 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Couldn't find executable %s", __func__, token);
835 SetLastError (ERROR_FILE_NOT_FOUND);
844 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Exec prog [%s] args [%s]", __func__, prog,
847 /* Check for CLR binaries; if found, we will try to invoke
848 * them using the same mono binary that started us.
850 if (is_managed_binary (prog)) {
851 gunichar2 *newapp = NULL, *newcmd;
855 newapp = mono_unicode_from_external (cli_launcher, &bytes_ignored);
857 newapp = mono_unicode_from_external ("mono", &bytes_ignored);
859 if (newapp != NULL) {
860 if (appname != NULL) {
861 newcmd = utf16_concat (utf16_quote, newapp, utf16_quote, utf16_space,
862 appname, utf16_space,
865 newcmd = utf16_concat (utf16_quote, newapp, utf16_quote, utf16_space,
869 g_free ((gunichar2 *)newapp);
871 if (newcmd != NULL) {
872 ret = CreateProcess (NULL, newcmd,
876 create_flags, new_environ,
880 g_free ((gunichar2 *)newcmd);
886 if (!is_executable (prog)) {
887 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Executable permisson not set on %s", __func__, prog);
888 SetLastError (ERROR_ACCESS_DENIED);
893 if (args_after_prog != NULL && *args_after_prog) {
896 qprog = g_shell_quote (prog);
897 full_prog = g_strconcat (qprog, " ", args_after_prog, NULL);
900 full_prog = g_shell_quote (prog);
903 ret = g_shell_parse_argv (full_prog, NULL, &argv, &gerr);
905 g_message ("CreateProcess: %s\n", gerr->message);
911 if (startup != NULL && startup->dwFlags & STARTF_USESTDHANDLES) {
912 in_fd = GPOINTER_TO_UINT (startup->hStdInput);
913 out_fd = GPOINTER_TO_UINT (startup->hStdOutput);
914 err_fd = GPOINTER_TO_UINT (startup->hStdError);
916 in_fd = GPOINTER_TO_UINT (GetStdHandle (STD_INPUT_HANDLE));
917 out_fd = GPOINTER_TO_UINT (GetStdHandle (STD_OUTPUT_HANDLE));
918 err_fd = GPOINTER_TO_UINT (GetStdHandle (STD_ERROR_HANDLE));
921 process_handle.proc_name = g_strdup (prog);
923 process_set_defaults (&process_handle);
925 handle = mono_w32handle_new (MONO_W32HANDLE_PROCESS, &process_handle);
926 if (handle == INVALID_HANDLE_VALUE) {
927 g_warning ("%s: error creating process handle", __func__);
930 SetLastError (ERROR_OUTOFMEMORY);
934 /* new_environ is a block of NULL-terminated strings, which
935 * is itself NULL-terminated. Of course, passing an array of
936 * string pointers would have made things too easy :-(
938 * If new_environ is not NULL it specifies the entire set of
939 * environment variables in the new process. Otherwise the
940 * new process inherits the same environment.
943 gunichar2 *new_environp;
945 /* Count the number of strings */
946 for (new_environp = (gunichar2 *)new_environ; *new_environp;
949 while (*new_environp) {
954 /* +2: one for the process handle value, and the last
957 env_strings = g_new0 (char *, env_count + 2);
959 /* Copy each environ string into 'strings' turning it
960 * into utf8 (or the requested encoding) at the same
964 for (new_environp = (gunichar2 *)new_environ; *new_environp;
966 env_strings[env_count] = mono_unicode_to_external (new_environp);
968 while (*new_environp) {
973 for (i = 0; environ[i] != NULL; i++)
976 /* +2: one for the process handle value, and the last
979 env_strings = g_new0 (char *, env_count + 2);
981 /* Copy each environ string into 'strings' turning it
982 * into utf8 (or the requested encoding) at the same
986 for (i = 0; environ[i] != NULL; i++) {
987 env_strings[env_count] = g_strdup (environ[i]);
992 /* Create a pipe to make sure the child doesn't exit before
993 * we can add the process to the linked list of mono_processes */
994 if (pipe (startup_pipe) == -1) {
995 /* Could not create the pipe to synchroniz process startup. We'll just not synchronize.
996 * This is just for a very hard to hit race condition in the first place */
997 startup_pipe [0] = startup_pipe [1] = -1;
998 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: new process startup not synchronized. We may not notice if the newly created process exits immediately.", __func__);
1001 switch (pid = fork ()) {
1002 case -1: /* Error */ {
1003 SetLastError (ERROR_OUTOFMEMORY);
1008 case 0: /* Child */ {
1009 if (startup_pipe [0] != -1) {
1010 /* Wait until the parent has updated it's internal data */
1011 ssize_t _i G_GNUC_UNUSED = read (startup_pipe [0], &dummy, 1);
1012 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: child: parent has completed its setup", __func__);
1013 close (startup_pipe [0]);
1014 close (startup_pipe [1]);
1017 /* should we detach from the process group? */
1019 /* Connect stdin, stdout and stderr */
1024 if (inherit_handles != TRUE) {
1025 /* FIXME: do something here */
1028 /* Close all file descriptors */
1029 for (i = mono_w32handle_fd_reserve - 1; i > 2; i--)
1032 #ifdef DEBUG_ENABLED
1033 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: exec()ing [%s] in dir [%s]", __func__, cmd,
1034 dir == NULL?".":dir);
1035 for (i = 0; argv[i] != NULL; i++)
1036 g_message ("arg %d: [%s]", i, argv[i]);
1038 for (i = 0; env_strings[i] != NULL; i++)
1039 g_message ("env %d: [%s]", i, env_strings[i]);
1043 if (dir != NULL && chdir (dir) == -1) {
1049 execve (argv[0], argv, env_strings);
1056 default: /* Parent */ {
1057 process_handle_data = lookup_process_handle (handle);
1058 if (!process_handle_data) {
1059 g_warning ("%s: error looking up process handle %p", __func__, handle);
1060 mono_w32handle_unref (handle);
1062 process_handle_data->id = pid;
1064 /* Add our mono_process into the linked list of mono_processes */
1065 mono_process = (struct MonoProcess *) g_malloc0 (sizeof (struct MonoProcess));
1066 mono_process->pid = pid;
1067 mono_process->handle_count = 1;
1068 mono_os_sem_init (&mono_process->exit_sem, 0);
1070 /* Keep the process handle artificially alive until the process
1071 * exits so that the information in the handle isn't lost. */
1072 mono_w32handle_ref (handle);
1073 mono_process->handle = handle;
1075 process_handle_data->mono_process = mono_process;
1077 mono_os_mutex_lock (&mono_processes_mutex);
1078 mono_process->next = mono_processes;
1079 mono_processes = mono_process;
1080 mono_os_mutex_unlock (&mono_processes_mutex);
1082 if (process_info != NULL) {
1083 process_info->hProcess = handle;
1084 process_info->dwProcessId = pid;
1086 /* FIXME: we might need to handle the thread info some day */
1087 process_info->hThread = INVALID_HANDLE_VALUE;
1088 process_info->dwThreadId = 0;
1097 mono_w32handle_unref (handle);
1099 if (startup_pipe [1] != -1) {
1100 /* Write 1 byte, doesn't matter what */
1101 ssize_t _i G_GNUC_UNUSED = write (startup_pipe [1], startup_pipe, 1);
1102 close (startup_pipe [0]);
1103 close (startup_pipe [1]);
1118 g_strfreev (env_strings);
1122 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: returning handle %p for pid %d", __func__, handle, pid);
1124 /* Check if something needs to be cleaned up. */
1125 mono_processes_cleanup ();
1129 SetLastError (ERROR_NOT_SUPPORTED);
1131 #endif // defined (HAVE_FORK) && defined (HAVE_EXECVE)
1135 process_set_name (WapiHandle_process *process_handle)
1137 char *progname, *utf8_progname, *slash;
1139 progname = g_get_prgname ();
1140 utf8_progname = mono_utf8_from_external (progname);
1142 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: using [%s] as prog name", __func__, progname);
1144 if (utf8_progname) {
1145 slash = strrchr (utf8_progname, '/');
1147 process_handle->proc_name = g_strdup (slash+1);
1149 process_handle->proc_name = g_strdup (utf8_progname);
1150 g_free (utf8_progname);
1155 _wapi_processes_init (void)
1157 pid_t pid = wapi_getpid ();
1158 WapiHandle_process process_handle = {0};
1160 mono_w32handle_register_ops (MONO_W32HANDLE_PROCESS, &_wapi_process_ops);
1162 mono_w32handle_register_capabilities (MONO_W32HANDLE_PROCESS,
1163 (MonoW32HandleCapability)(MONO_W32HANDLE_CAP_WAIT | MONO_W32HANDLE_CAP_SPECIAL_WAIT));
1165 process_handle.id = pid;
1167 process_set_defaults (&process_handle);
1168 process_set_name (&process_handle);
1170 current_process = mono_w32handle_new (MONO_W32HANDLE_PROCESS,
1172 g_assert (current_process);
1174 mono_os_mutex_init (&mono_processes_mutex);
1178 _wapi_process_duplicate (void)
1180 mono_w32handle_ref (current_process);
1182 return current_process;
1185 /* Returns a pseudo handle that doesn't need to be closed afterwards */
1187 GetCurrentProcess (void)
1189 return _WAPI_PROCESS_CURRENT;
1193 GetProcessId (gpointer handle)
1195 WapiHandle_process *process_handle;
1197 if (WAPI_IS_PSEUDO_PROCESS_HANDLE (handle))
1198 /* This is a pseudo handle */
1199 return WAPI_HANDLE_TO_PID (handle);
1201 process_handle = lookup_process_handle (handle);
1202 if (!process_handle) {
1203 SetLastError (ERROR_INVALID_HANDLE);
1207 return process_handle->id;
1211 process_open_compare (gpointer handle, gpointer user_data)
1214 WapiHandle_process *process_handle;
1217 g_assert (!WAPI_IS_PSEUDO_PROCESS_HANDLE (handle));
1219 process_handle = lookup_process_handle (handle);
1220 g_assert (process_handle);
1222 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: looking at process %d", __func__, process_handle->id);
1224 checking_pid = process_handle->id;
1226 if (checking_pid == 0)
1229 wanted_pid = GPOINTER_TO_UINT (user_data);
1231 /* It's possible to have more than one process handle with the
1232 * same pid, but only the one running process can be
1235 if (checking_pid == wanted_pid &&
1236 !mono_w32handle_issignalled (handle)) {
1237 /* If the handle is blown away in the window between
1238 * returning TRUE here and mono_w32handle_search pinging
1239 * the timestamp, the search will continue
1248 CloseProcess (gpointer handle)
1250 if (WAPI_IS_PSEUDO_PROCESS_HANDLE (handle))
1252 return CloseHandle (handle);
1256 * The caller owns the returned handle and must call CloseProcess () on it to clean it up.
1259 OpenProcess (guint32 req_access G_GNUC_UNUSED, gboolean inherit G_GNUC_UNUSED, guint32 pid)
1261 /* Find the process handle that corresponds to pid */
1262 gpointer handle = NULL;
1264 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: looking for process %d", __func__, pid);
1266 handle = mono_w32handle_search (MONO_W32HANDLE_PROCESS,
1267 process_open_compare,
1268 GUINT_TO_POINTER (pid), NULL, TRUE);
1270 if (is_pid_valid (pid)) {
1271 /* Return a pseudo handle for processes we
1272 * don't have handles for
1274 return WAPI_PID_TO_HANDLE (pid);
1276 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Can't find pid %d", __func__, pid);
1278 SetLastError (ERROR_PROC_NOT_FOUND);
1284 /* mono_w32handle_search () already added a ref */
1289 GetExitCodeProcess (gpointer process, guint32 *code)
1291 WapiHandle_process *process_handle;
1298 if (WAPI_IS_PSEUDO_PROCESS_HANDLE (process)) {
1299 pid = WAPI_HANDLE_TO_PID (process);
1300 /* This is a pseudo handle, so we don't know what the
1301 * exit code was, but we can check whether it's alive or not
1303 if (is_pid_valid (pid)) {
1304 *code = STILL_ACTIVE;
1312 process_handle = lookup_process_handle (process);
1313 if (!process_handle) {
1314 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Can't find process %p", __func__, process);
1319 if (process_handle->id == wapi_getpid ()) {
1320 *code = STILL_ACTIVE;
1324 /* A process handle is only signalled if the process has exited
1325 * and has been waited for */
1327 /* Make sure any process exit has been noticed, before
1328 * checking if the process is signalled. Fixes bug 325463.
1330 process_wait (process, 0, &alerted);
1332 if (mono_w32handle_issignalled (process))
1333 *code = process_handle->exitstatus;
1335 *code = STILL_ACTIVE;
1341 GetProcessTimes (gpointer process, WapiFileTime *create_time,
1342 WapiFileTime *exit_time, WapiFileTime *kernel_time,
1343 WapiFileTime *user_time)
1345 WapiHandle_process *process_handle;
1346 gboolean ku_times_set = FALSE;
1348 if (create_time == NULL || exit_time == NULL || kernel_time == NULL ||
1350 /* Not sure if w32 allows NULLs here or not */
1353 if (WAPI_IS_PSEUDO_PROCESS_HANDLE (process)) {
1354 gpointer pid = GINT_TO_POINTER (WAPI_HANDLE_TO_PID(process));
1355 gint64 start_ticks, user_ticks, kernel_ticks;
1357 mono_process_get_times (pid, &start_ticks, &user_ticks, &kernel_ticks);
1359 _wapi_guint64_to_filetime (start_ticks, create_time);
1360 _wapi_guint64_to_filetime (user_ticks, kernel_time);
1361 _wapi_guint64_to_filetime (kernel_ticks, user_time);
1366 process_handle = lookup_process_handle (process);
1367 if (!process_handle) {
1368 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Can't find process %p", __func__, process);
1373 *create_time = process_handle->create_time;
1375 /* A process handle is only signalled if the process has
1376 * exited. Otherwise exit_time isn't set
1378 if (mono_w32handle_issignalled (process))
1379 *exit_time = process_handle->exit_time;
1381 #ifdef HAVE_GETRUSAGE
1382 if (process_handle->id == getpid ()) {
1383 struct rusage time_data;
1384 if (getrusage (RUSAGE_SELF, &time_data) == 0) {
1386 ku_times_set = TRUE;
1387 tick_val = (guint64)time_data.ru_utime.tv_sec * 10000000 + (guint64)time_data.ru_utime.tv_usec * 10;
1388 _wapi_guint64_to_filetime (tick_val, user_time);
1389 tick_val = (guint64)time_data.ru_stime.tv_sec * 10000000 + (guint64)time_data.ru_stime.tv_usec * 10;
1390 _wapi_guint64_to_filetime (tick_val, kernel_time);
1394 if (!ku_times_set) {
1395 memset (kernel_time, 0, sizeof (WapiFileTime));
1396 memset (user_time, 0, sizeof (WapiFileTime));
1404 gpointer address_start;
1405 gpointer address_end;
1407 gpointer address_offset;
1413 static void free_procmodule (WapiProcModule *mod)
1415 if (mod->perms != NULL) {
1416 g_free (mod->perms);
1418 if (mod->filename != NULL) {
1419 g_free (mod->filename);
1424 static gint find_procmodule (gconstpointer a, gconstpointer b)
1426 WapiProcModule *want = (WapiProcModule *)a;
1427 WapiProcModule *compare = (WapiProcModule *)b;
1429 if ((want->device == compare->device) &&
1430 (want->inode == compare->inode)) {
1437 #if defined(USE_OSX_LOADER)
1438 #include <mach-o/dyld.h>
1439 #include <mach-o/getsect.h>
1441 static GSList *load_modules (void)
1444 WapiProcModule *mod;
1445 uint32_t count = _dyld_image_count ();
1448 for (i = 0; i < count; i++) {
1449 #if SIZEOF_VOID_P == 8
1450 const struct mach_header_64 *hdr;
1451 const struct section_64 *sec;
1453 const struct mach_header *hdr;
1454 const struct section *sec;
1458 name = _dyld_get_image_name (i);
1459 #if SIZEOF_VOID_P == 8
1460 hdr = (const struct mach_header_64*)_dyld_get_image_header (i);
1461 sec = getsectbynamefromheader_64 (hdr, SEG_DATA, SECT_DATA);
1463 hdr = _dyld_get_image_header (i);
1464 sec = getsectbynamefromheader (hdr, SEG_DATA, SECT_DATA);
1467 /* Some dynlibs do not have data sections on osx (#533893) */
1472 mod = g_new0 (WapiProcModule, 1);
1473 mod->address_start = GINT_TO_POINTER (sec->addr);
1474 mod->address_end = GINT_TO_POINTER (sec->addr+sec->size);
1475 mod->perms = g_strdup ("r--p");
1476 mod->address_offset = 0;
1477 mod->device = makedev (0, 0);
1479 mod->filename = g_strdup (name);
1481 if (g_slist_find_custom (ret, mod, find_procmodule) == NULL) {
1482 ret = g_slist_prepend (ret, mod);
1484 free_procmodule (mod);
1488 ret = g_slist_reverse (ret);
1492 #elif defined(USE_BSD_LOADER)
1494 static int load_modules_callback (struct dl_phdr_info *info, size_t size, void *ptr)
1496 if (size < offsetof (struct dl_phdr_info, dlpi_phnum)
1497 + sizeof (info->dlpi_phnum))
1500 struct dl_phdr_info *cpy = g_calloc (1, sizeof(struct dl_phdr_info));
1504 memcpy(cpy, info, sizeof(*info));
1506 g_ptr_array_add ((GPtrArray *)ptr, cpy);
1511 static GSList *load_modules (void)
1514 WapiProcModule *mod;
1515 GPtrArray *dlarray = g_ptr_array_new();
1518 if (dl_iterate_phdr(load_modules_callback, dlarray) < 0)
1521 for (i = 0; i < dlarray->len; i++) {
1522 struct dl_phdr_info *info = g_ptr_array_index (dlarray, i);
1524 mod = g_new0 (WapiProcModule, 1);
1525 mod->address_start = (gpointer)(info->dlpi_addr + info->dlpi_phdr[0].p_vaddr);
1526 mod->address_end = (gpointer)(info->dlpi_addr +
1527 info->dlpi_phdr[info->dlpi_phnum - 1].p_vaddr);
1528 mod->perms = g_strdup ("r--p");
1529 mod->address_offset = 0;
1531 mod->filename = g_strdup (info->dlpi_name);
1533 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: inode=%d, filename=%s, address_start=%p, address_end=%p", __func__,
1534 mod->inode, mod->filename, mod->address_start, mod->address_end);
1538 if (g_slist_find_custom (ret, mod, find_procmodule) == NULL) {
1539 ret = g_slist_prepend (ret, mod);
1541 free_procmodule (mod);
1545 g_ptr_array_free (dlarray, TRUE);
1547 ret = g_slist_reverse (ret);
1551 #elif defined(USE_HAIKU_LOADER)
1553 static GSList *load_modules (void)
1556 WapiProcModule *mod;
1558 image_info imageInfo;
1560 while (get_next_image_info (B_CURRENT_TEAM, &cookie, &imageInfo) == B_OK) {
1561 mod = g_new0 (WapiProcModule, 1);
1562 mod->device = imageInfo.device;
1563 mod->inode = imageInfo.node;
1564 mod->filename = g_strdup (imageInfo.name);
1565 mod->address_start = MIN (imageInfo.text, imageInfo.data);
1566 mod->address_end = MAX ((uint8_t*)imageInfo.text + imageInfo.text_size,
1567 (uint8_t*)imageInfo.data + imageInfo.data_size);
1568 mod->perms = g_strdup ("r--p");
1569 mod->address_offset = 0;
1571 if (g_slist_find_custom (ret, mod, find_procmodule) == NULL) {
1572 ret = g_slist_prepend (ret, mod);
1574 free_procmodule (mod);
1578 ret = g_slist_reverse (ret);
1583 static GSList *load_modules (FILE *fp)
1586 WapiProcModule *mod;
1587 char buf[MAXPATHLEN + 1], *p, *endp;
1588 char *start_start, *end_start, *prot_start, *offset_start;
1589 char *maj_dev_start, *min_dev_start, *inode_start, prot_buf[5];
1590 gpointer address_start, address_end, address_offset;
1591 guint32 maj_dev, min_dev;
1595 while (fgets (buf, sizeof(buf), fp)) {
1597 while (g_ascii_isspace (*p)) ++p;
1599 if (!g_ascii_isxdigit (*start_start)) {
1602 address_start = (gpointer)strtoul (start_start, &endp, 16);
1610 if (!g_ascii_isxdigit (*end_start)) {
1613 address_end = (gpointer)strtoul (end_start, &endp, 16);
1615 if (!g_ascii_isspace (*p)) {
1619 while (g_ascii_isspace (*p)) ++p;
1621 if (*prot_start != 'r' && *prot_start != '-') {
1624 memcpy (prot_buf, prot_start, 4);
1626 while (!g_ascii_isspace (*p)) ++p;
1628 while (g_ascii_isspace (*p)) ++p;
1630 if (!g_ascii_isxdigit (*offset_start)) {
1633 address_offset = (gpointer)strtoul (offset_start, &endp, 16);
1635 if (!g_ascii_isspace (*p)) {
1639 while(g_ascii_isspace (*p)) ++p;
1641 if (!g_ascii_isxdigit (*maj_dev_start)) {
1644 maj_dev = strtoul (maj_dev_start, &endp, 16);
1652 if (!g_ascii_isxdigit (*min_dev_start)) {
1655 min_dev = strtoul (min_dev_start, &endp, 16);
1657 if (!g_ascii_isspace (*p)) {
1661 while (g_ascii_isspace (*p)) ++p;
1663 if (!g_ascii_isxdigit (*inode_start)) {
1666 inode = (guint64)strtol (inode_start, &endp, 10);
1668 if (!g_ascii_isspace (*p)) {
1672 device = makedev ((int)maj_dev, (int)min_dev);
1673 if ((device == 0) &&
1678 while(g_ascii_isspace (*p)) ++p;
1679 /* p now points to the filename */
1681 mod = g_new0 (WapiProcModule, 1);
1682 mod->address_start = address_start;
1683 mod->address_end = address_end;
1684 mod->perms = g_strdup (prot_buf);
1685 mod->address_offset = address_offset;
1686 mod->device = device;
1688 mod->filename = g_strdup (g_strstrip (p));
1690 if (g_slist_find_custom (ret, mod, find_procmodule) == NULL) {
1691 ret = g_slist_prepend (ret, mod);
1693 free_procmodule (mod);
1697 ret = g_slist_reverse (ret);
1703 static gboolean match_procname_to_modulename (char *procname, char *modulename)
1705 char* lastsep = NULL;
1706 char* lastsep2 = NULL;
1709 gboolean result = FALSE;
1711 if (procname == NULL || modulename == NULL)
1714 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: procname=\"%s\", modulename=\"%s\"", __func__, procname, modulename);
1715 pname = mono_path_resolve_symlinks (procname);
1716 mname = mono_path_resolve_symlinks (modulename);
1718 if (!strcmp (pname, mname))
1722 lastsep = strrchr (mname, '/');
1724 if (!strcmp (lastsep+1, pname))
1727 lastsep2 = strrchr (pname, '/');
1730 if (!strcmp (lastsep+1, lastsep2+1))
1733 if (!strcmp (mname, lastsep2+1))
1743 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: result is %d", __func__, result);
1747 #if !(defined(USE_OSX_LOADER) || defined(USE_BSD_LOADER) || defined(USE_HAIKU_LOADER))
1749 open_process_map (int pid, const char *mode)
1752 const char *proc_path[] = {
1753 "/proc/%d/maps", /* GNU/Linux */
1754 "/proc/%d/map", /* FreeBSD */
1760 for (i = 0; fp == NULL && proc_path [i]; i++) {
1761 filename = g_strdup_printf (proc_path[i], pid);
1762 fp = fopen (filename, mode);
1770 static char *get_process_name_from_proc (pid_t pid);
1772 gboolean EnumProcessModules (gpointer process, gpointer *modules,
1773 guint32 size, guint32 *needed)
1775 WapiHandle_process *process_handle;
1776 #if !defined(USE_OSX_LOADER) && !defined(USE_BSD_LOADER)
1779 GSList *mods = NULL;
1780 WapiProcModule *module;
1781 guint32 count, avail = size / sizeof(gpointer);
1784 char *proc_name = NULL;
1786 /* Store modules in an array of pointers (main module as
1787 * modules[0]), using the load address for each module as a
1788 * token. (Use 'NULL' as an alternative for the main module
1789 * so that the simple implementation can just return one item
1790 * for now.) Get the info from /proc/<pid>/maps on linux,
1791 * /proc/<pid>/map on FreeBSD, other systems will have to
1792 * implement /dev/kmem reading or whatever other horrid
1793 * technique is needed.
1795 if (size < sizeof(gpointer))
1798 if (WAPI_IS_PSEUDO_PROCESS_HANDLE (process)) {
1799 pid = WAPI_HANDLE_TO_PID (process);
1800 proc_name = get_process_name_from_proc (pid);
1802 process_handle = lookup_process_handle (process);
1803 if (!process_handle) {
1804 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Can't find process %p", __func__, process);
1808 pid = process_handle->id;
1809 proc_name = g_strdup (process_handle->proc_name);
1812 #if defined(USE_OSX_LOADER) || defined(USE_BSD_LOADER) || defined(USE_HAIKU_LOADER)
1813 mods = load_modules ();
1816 *needed = sizeof(gpointer);
1820 fp = open_process_map (pid, "r");
1822 /* No /proc/<pid>/maps so just return the main module
1826 *needed = sizeof(gpointer);
1830 mods = load_modules (fp);
1833 count = g_slist_length (mods);
1835 /* count + 1 to leave slot 0 for the main module */
1836 *needed = sizeof(gpointer) * (count + 1);
1839 * Use the NULL shortcut, as the first line in
1840 * /proc/<pid>/maps isn't the executable, and we need
1841 * that first in the returned list. Check the module name
1842 * to see if it ends with the proc name and substitute
1843 * the first entry with it. FIXME if this turns out to
1847 for (i = 0; i < (avail - 1) && i < count; i++) {
1848 module = (WapiProcModule *)g_slist_nth_data (mods, i);
1849 if (modules[0] != NULL)
1850 modules[i] = module->address_start;
1851 else if (match_procname_to_modulename (proc_name, module->filename))
1852 modules[0] = module->address_start;
1854 modules[i + 1] = module->address_start;
1857 for (i = 0; i < count; i++) {
1858 free_procmodule ((WapiProcModule *)g_slist_nth_data (mods, i));
1860 g_slist_free (mods);
1867 get_process_name_from_proc (pid_t pid)
1869 #if defined(USE_BSD_LOADER)
1872 struct kinfo_proc *pi;
1873 #elif defined(USE_OSX_LOADER)
1874 #if !(!defined (__mono_ppc__) && defined (TARGET_OSX))
1876 struct kinfo_proc *pi;
1877 int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, pid };
1881 char *filename = NULL;
1886 #if defined(PLATFORM_SOLARIS)
1887 filename = g_strdup_printf ("/proc/%d/psinfo", pid);
1888 if ((fp = fopen (filename, "r")) != NULL) {
1892 nread = fread (&info, sizeof (info), 1, fp);
1894 ret = g_strdup (info.pr_fname);
1900 #elif defined(USE_OSX_LOADER)
1901 #if !defined (__mono_ppc__) && defined (TARGET_OSX)
1902 /* No proc name on OSX < 10.5 nor ppc nor iOS */
1903 memset (buf, '\0', sizeof(buf));
1904 proc_name (pid, buf, sizeof(buf));
1906 // Fixes proc_name triming values to 15 characters #32539
1907 if (strlen (buf) >= MAXCOMLEN - 1) {
1908 char path_buf [PROC_PIDPATHINFO_MAXSIZE];
1912 memset (path_buf, '\0', sizeof(path_buf));
1913 path_len = proc_pidpath (pid, path_buf, sizeof(path_buf));
1915 if (path_len > 0 && path_len < sizeof(path_buf)) {
1916 name_buf = path_buf + path_len;
1917 for(;name_buf > path_buf; name_buf--) {
1918 if (name_buf [0] == '/') {
1924 if (memcmp (buf, name_buf, MAXCOMLEN - 1) == 0)
1925 ret = g_strdup (name_buf);
1929 if (ret == NULL && strlen (buf) > 0)
1930 ret = g_strdup (buf);
1932 if (sysctl(mib, 4, NULL, &size, NULL, 0) < 0)
1935 if ((pi = g_malloc (size)) == NULL)
1938 if (sysctl (mib, 4, pi, &size, NULL, 0) < 0) {
1939 if (errno == ENOMEM) {
1941 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Didn't allocate enough memory for kproc info", __func__);
1946 if (strlen (pi->kp_proc.p_comm) > 0)
1947 ret = g_strdup (pi->kp_proc.p_comm);
1951 #elif defined(USE_BSD_LOADER)
1952 #if defined(__FreeBSD__)
1954 mib [1] = KERN_PROC;
1955 mib [2] = KERN_PROC_PID;
1957 if (sysctl(mib, 4, NULL, &size, NULL, 0) < 0) {
1958 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: sysctl() failed: %d", __func__, errno);
1962 if ((pi = g_malloc (size)) == NULL)
1965 if (sysctl (mib, 4, pi, &size, NULL, 0) < 0) {
1966 if (errno == ENOMEM) {
1968 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Didn't allocate enough memory for kproc info", __func__);
1973 if (strlen (pi->ki_comm) > 0)
1974 ret = g_strdup (pi->ki_comm);
1976 #elif defined(__OpenBSD__)
1978 mib [1] = KERN_PROC;
1979 mib [2] = KERN_PROC_PID;
1981 mib [4] = sizeof(struct kinfo_proc);
1985 if (sysctl(mib, 6, NULL, &size, NULL, 0) < 0) {
1986 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: sysctl() failed: %d", __func__, errno);
1990 if ((pi = g_malloc (size)) == NULL)
1993 mib[5] = (int)(size / sizeof(struct kinfo_proc));
1995 if ((sysctl (mib, 6, pi, &size, NULL, 0) < 0) ||
1996 (size != sizeof (struct kinfo_proc))) {
1997 if (errno == ENOMEM) {
2004 if (strlen (pi->p_comm) > 0)
2005 ret = g_strdup (pi->p_comm);
2009 #elif defined(USE_HAIKU_LOADER)
2010 image_info imageInfo;
2013 if (get_next_image_info ((team_id)pid, &cookie, &imageInfo) == B_OK) {
2014 ret = g_strdup (imageInfo.name);
2017 memset (buf, '\0', sizeof(buf));
2018 filename = g_strdup_printf ("/proc/%d/exe", pid);
2019 if (readlink (filename, buf, 255) > 0) {
2020 ret = g_strdup (buf);
2028 filename = g_strdup_printf ("/proc/%d/cmdline", pid);
2029 if ((fp = fopen (filename, "r")) != NULL) {
2030 if (fgets (buf, 256, fp) != NULL) {
2031 ret = g_strdup (buf);
2042 filename = g_strdup_printf ("/proc/%d/stat", pid);
2043 if ((fp = fopen (filename, "r")) != NULL) {
2044 if (fgets (buf, 256, fp) != NULL) {
2047 start = strchr (buf, '(');
2048 if (start != NULL) {
2049 end = strchr (start + 1, ')');
2052 ret = g_strndup (start + 1,
2067 * wapi_process_get_path:
2069 * Return the full path of the executable of the process PID, or NULL if it cannot be determined.
2070 * Returns malloc-ed memory.
2073 wapi_process_get_path (pid_t pid)
2075 #if defined(PLATFORM_MACOSX) && !defined(__mono_ppc__) && defined(TARGET_OSX)
2076 char buf [PROC_PIDPATHINFO_MAXSIZE];
2079 res = proc_pidpath (pid, buf, sizeof (buf));
2082 if (buf [0] == '\0')
2084 return g_strdup (buf);
2086 return get_process_name_from_proc (pid);
2091 * wapi_process_set_cli_launcher:
2093 * Set the full path of the runtime executable used to launch managed exe's.
2096 wapi_process_set_cli_launcher (char *path)
2098 g_free (cli_launcher);
2099 cli_launcher = path ? g_strdup (path) : NULL;
2103 get_module_name (gpointer process, gpointer module,
2104 gunichar2 *basename, guint32 size,
2107 WapiHandle_process *process_handle;
2109 gunichar2 *procname;
2110 char *procname_ext = NULL;
2113 #if !defined(USE_OSX_LOADER) && !defined(USE_BSD_LOADER)
2116 GSList *mods = NULL;
2117 WapiProcModule *found_module;
2120 char *proc_name = NULL;
2122 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Getting module base name, process handle %p module %p",
2123 __func__, process, module);
2125 size = size * sizeof (gunichar2); /* adjust for unicode characters */
2127 if (basename == NULL || size == 0)
2130 if (WAPI_IS_PSEUDO_PROCESS_HANDLE (process)) {
2131 /* This is a pseudo handle */
2132 pid = (pid_t)WAPI_HANDLE_TO_PID (process);
2133 proc_name = get_process_name_from_proc (pid);
2135 process_handle = lookup_process_handle (process);
2136 if (!process_handle) {
2137 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Can't find process %p", __func__,
2142 pid = process_handle->id;
2143 proc_name = g_strdup (process_handle->proc_name);
2146 /* Look up the address in /proc/<pid>/maps */
2147 #if defined(USE_OSX_LOADER) || defined(USE_BSD_LOADER) || defined(USE_HAIKU_LOADER)
2148 mods = load_modules ();
2150 fp = open_process_map (pid, "r");
2152 if (errno == EACCES && module == NULL && base == TRUE) {
2153 procname_ext = get_process_name_from_proc (pid);
2155 /* No /proc/<pid>/maps, so just return failure
2162 mods = load_modules (fp);
2166 count = g_slist_length (mods);
2168 /* If module != NULL compare the address.
2169 * If module == NULL we are looking for the main module.
2170 * The best we can do for now check it the module name end with the process name.
2172 for (i = 0; i < count; i++) {
2173 found_module = (WapiProcModule *)g_slist_nth_data (mods, i);
2174 if (procname_ext == NULL &&
2175 ((module == NULL && match_procname_to_modulename (proc_name, found_module->filename)) ||
2176 (module != NULL && found_module->address_start == module))) {
2178 procname_ext = g_path_get_basename (found_module->filename);
2180 procname_ext = g_strdup (found_module->filename);
2183 free_procmodule (found_module);
2186 if (procname_ext == NULL) {
2187 /* If it's *still* null, we might have hit the
2188 * case where reading /proc/$pid/maps gives an
2189 * empty file for this user.
2191 procname_ext = get_process_name_from_proc (pid);
2194 g_slist_free (mods);
2198 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Process name is [%s]", __func__,
2201 procname = mono_unicode_from_external (procname_ext, &bytes);
2202 if (procname == NULL) {
2204 g_free (procname_ext);
2210 /* Add the terminator */
2214 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Size %d smaller than needed (%ld); truncating", __func__, size, bytes);
2216 memcpy (basename, procname, size);
2218 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Size %d larger than needed (%ld)",
2219 __func__, size, bytes);
2221 memcpy (basename, procname, bytes);
2225 g_free (procname_ext);
2234 get_module_filename (gpointer process, gpointer module,
2235 gunichar2 *basename, guint32 size)
2240 gunichar2 *proc_path;
2242 size *= sizeof (gunichar2); /* adjust for unicode characters */
2244 if (basename == NULL || size == 0)
2247 pid = GetProcessId (process);
2249 path = wapi_process_get_path (pid);
2253 proc_path = mono_unicode_from_external (path, &bytes);
2256 if (proc_path == NULL)
2261 /* Add the terminator */
2265 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Size %d smaller than needed (%ld); truncating", __func__, size, bytes);
2267 memcpy (basename, proc_path, size);
2269 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Size %d larger than needed (%ld)",
2270 __func__, size, bytes);
2272 memcpy (basename, proc_path, bytes);
2281 GetModuleBaseName (gpointer process, gpointer module,
2282 gunichar2 *basename, guint32 size)
2284 return get_module_name (process, module, basename, size, TRUE);
2288 GetModuleFileNameEx (gpointer process, gpointer module,
2289 gunichar2 *filename, guint32 size)
2291 return get_module_filename (process, module, filename, size);
2295 GetModuleInformation (gpointer process, gpointer module,
2296 WapiModuleInfo *modinfo, guint32 size)
2298 WapiHandle_process *process_handle;
2300 #if !defined(USE_OSX_LOADER) && !defined(USE_BSD_LOADER)
2303 GSList *mods = NULL;
2304 WapiProcModule *found_module;
2307 gboolean ret = FALSE;
2308 char *proc_name = NULL;
2310 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Getting module info, process handle %p module %p",
2311 __func__, process, module);
2313 if (modinfo == NULL || size < sizeof (WapiModuleInfo))
2316 if (WAPI_IS_PSEUDO_PROCESS_HANDLE (process)) {
2317 pid = (pid_t)WAPI_HANDLE_TO_PID (process);
2318 proc_name = get_process_name_from_proc (pid);
2320 process_handle = lookup_process_handle (process);
2321 if (!process_handle) {
2322 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Can't find process %p", __func__,
2327 pid = process_handle->id;
2328 proc_name = g_strdup (process_handle->proc_name);
2331 #if defined(USE_OSX_LOADER) || defined(USE_BSD_LOADER) || defined(USE_HAIKU_LOADER)
2332 mods = load_modules ();
2334 /* Look up the address in /proc/<pid>/maps */
2335 if ((fp = open_process_map (pid, "r")) == NULL) {
2336 /* No /proc/<pid>/maps, so just return failure
2342 mods = load_modules (fp);
2345 count = g_slist_length (mods);
2347 /* If module != NULL compare the address.
2348 * If module == NULL we are looking for the main module.
2349 * The best we can do for now check it the module name end with the process name.
2351 for (i = 0; i < count; i++) {
2352 found_module = (WapiProcModule *)g_slist_nth_data (mods, i);
2354 ((module == NULL && match_procname_to_modulename (proc_name, found_module->filename)) ||
2355 (module != NULL && found_module->address_start == module))) {
2356 modinfo->lpBaseOfDll = found_module->address_start;
2357 modinfo->SizeOfImage = (gsize)(found_module->address_end) - (gsize)(found_module->address_start);
2358 modinfo->EntryPoint = found_module->address_offset;
2362 free_procmodule (found_module);
2365 g_slist_free (mods);
2372 GetProcessWorkingSetSize (gpointer process, size_t *min, size_t *max)
2374 WapiHandle_process *process_handle;
2376 if (min == NULL || max == NULL)
2377 /* Not sure if w32 allows NULLs here or not */
2380 if (WAPI_IS_PSEUDO_PROCESS_HANDLE (process))
2381 /* This is a pseudo handle, so just fail for now */
2384 process_handle = lookup_process_handle (process);
2385 if (!process_handle) {
2386 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Can't find process %p", __func__, process);
2391 *min = process_handle->min_working_set;
2392 *max = process_handle->max_working_set;
2398 SetProcessWorkingSetSize (gpointer process, size_t min, size_t max)
2400 WapiHandle_process *process_handle;
2402 if (WAPI_IS_PSEUDO_PROCESS_HANDLE (process))
2403 /* This is a pseudo handle, so just fail for now
2407 process_handle = lookup_process_handle (process);
2408 if (!process_handle) {
2409 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Can't find process %p", __func__, process);
2414 process_handle->min_working_set = min;
2415 process_handle->max_working_set = max;
2422 TerminateProcess (gpointer process, gint32 exitCode)
2424 #if defined(HAVE_KILL)
2425 WapiHandle_process *process_handle;
2430 if (WAPI_IS_PSEUDO_PROCESS_HANDLE (process)) {
2431 /* This is a pseudo handle */
2432 pid = (pid_t)WAPI_HANDLE_TO_PID (process);
2434 process_handle = lookup_process_handle (process);
2435 if (!process_handle) {
2436 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Can't find process %p", __func__, process);
2437 SetLastError (ERROR_INVALID_HANDLE);
2440 pid = process_handle->id;
2443 signo = (exitCode == -1) ? SIGKILL : SIGTERM;
2444 ret = kill (pid, signo);
2448 SetLastError (ERROR_INVALID_PARAMETER);
2451 SetLastError (ERROR_ACCESS_DENIED);
2454 SetLastError (ERROR_PROC_NOT_FOUND);
2457 SetLastError (ERROR_GEN_FAILURE);
2463 g_error ("kill() is not supported by this platform");
2469 GetPriorityClass (gpointer process)
2471 #ifdef HAVE_GETPRIORITY
2472 WapiHandle_process *process_handle;
2476 if (WAPI_IS_PSEUDO_PROCESS_HANDLE (process)) {
2477 /* This is a pseudo handle */
2478 pid = (pid_t)WAPI_HANDLE_TO_PID (process);
2480 process_handle = lookup_process_handle (process);
2481 if (!process_handle) {
2482 SetLastError (ERROR_INVALID_HANDLE);
2485 pid = process_handle->id;
2489 ret = getpriority (PRIO_PROCESS, pid);
2490 if (ret == -1 && errno != 0) {
2494 SetLastError (ERROR_ACCESS_DENIED);
2497 SetLastError (ERROR_PROC_NOT_FOUND);
2500 SetLastError (ERROR_GEN_FAILURE);
2506 return NORMAL_PRIORITY_CLASS;
2508 return REALTIME_PRIORITY_CLASS;
2510 return HIGH_PRIORITY_CLASS;
2512 return ABOVE_NORMAL_PRIORITY_CLASS;
2514 return IDLE_PRIORITY_CLASS;
2516 return BELOW_NORMAL_PRIORITY_CLASS;
2518 return NORMAL_PRIORITY_CLASS;
2520 SetLastError (ERROR_NOT_SUPPORTED);
2526 SetPriorityClass (gpointer process, guint32 priority_class)
2528 #ifdef HAVE_SETPRIORITY
2529 WapiHandle_process *process_handle;
2534 if (WAPI_IS_PSEUDO_PROCESS_HANDLE (process)) {
2535 /* This is a pseudo handle */
2536 pid = (pid_t)WAPI_HANDLE_TO_PID (process);
2538 process_handle = lookup_process_handle (process);
2539 if (!process_handle) {
2540 SetLastError (ERROR_INVALID_HANDLE);
2543 pid = process_handle->id;
2546 switch (priority_class) {
2547 case IDLE_PRIORITY_CLASS:
2550 case BELOW_NORMAL_PRIORITY_CLASS:
2553 case NORMAL_PRIORITY_CLASS:
2556 case ABOVE_NORMAL_PRIORITY_CLASS:
2559 case HIGH_PRIORITY_CLASS:
2562 case REALTIME_PRIORITY_CLASS:
2566 SetLastError (ERROR_INVALID_PARAMETER);
2570 ret = setpriority (PRIO_PROCESS, pid, prio);
2575 SetLastError (ERROR_ACCESS_DENIED);
2578 SetLastError (ERROR_PROC_NOT_FOUND);
2581 SetLastError (ERROR_GEN_FAILURE);
2587 SetLastError (ERROR_NOT_SUPPORTED);
2593 mono_processes_cleanup (void)
2595 struct MonoProcess *mp;
2596 struct MonoProcess *prev = NULL;
2597 GSList *finished = NULL;
2599 gpointer unref_handle;
2601 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s", __func__);
2603 /* Ensure we're not in here in multiple threads at once, nor recursive. */
2604 if (InterlockedCompareExchange (&mono_processes_cleaning_up, 1, 0) != 0)
2607 for (mp = mono_processes; mp; mp = mp->next) {
2608 if (mp->pid == 0 && mp->handle) {
2609 /* This process has exited and we need to remove the artifical ref
2611 mono_os_mutex_lock (&mono_processes_mutex);
2612 unref_handle = mp->handle;
2614 mono_os_mutex_unlock (&mono_processes_mutex);
2616 mono_w32handle_unref (unref_handle);
2621 * Remove processes which exited from the mono_processes list.
2622 * We need to synchronize with the sigchld handler here, which runs
2623 * asynchronously. The handler requires that the mono_processes list
2626 mono_os_mutex_lock (&mono_processes_mutex);
2628 mp = mono_processes;
2630 if (mp->handle_count == 0 && mp->freeable) {
2633 * This code can run parallel with the sigchld handler, but the
2634 * modifications it makes are safe.
2636 if (mp == mono_processes)
2637 mono_processes = mp->next;
2639 prev->next = mp->next;
2640 finished = g_slist_prepend (finished, mp);
2649 mono_memory_barrier ();
2651 for (l = finished; l; l = l->next) {
2653 * All the entries in the finished list are unlinked from mono_processes, and
2654 * they have the 'finished' flag set, which means the sigchld handler is done
2657 mp = (MonoProcess *)l->data;
2658 mono_os_sem_destroy (&mp->exit_sem);
2661 g_slist_free (finished);
2663 mono_os_mutex_unlock (&mono_processes_mutex);
2665 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s done", __func__);
2667 InterlockedDecrement (&mono_processes_cleaning_up);
2671 process_close (gpointer handle, gpointer data)
2673 WapiHandle_process *process_handle;
2675 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s", __func__);
2677 process_handle = (WapiHandle_process *) data;
2678 g_free (process_handle->proc_name);
2679 process_handle->proc_name = NULL;
2680 if (process_handle->mono_process)
2681 InterlockedDecrement (&process_handle->mono_process->handle_count);
2682 mono_processes_cleanup ();
2685 static void process_details (gpointer data)
2687 WapiHandle_process *process_handle = (WapiHandle_process *) data;
2688 g_print ("id: %d, exited: %s, exitstatus: %d",
2689 process_handle->id, process_handle->exited ? "true" : "false", process_handle->exitstatus);
2692 static const gchar* process_typename (void)
2697 static gsize process_typesize (void)
2699 return sizeof (WapiHandle_process);
2703 MONO_SIGNAL_HANDLER_FUNC (static, mono_sigchld_signal_handler, (int _dummy, siginfo_t *info, void *context))
2707 struct MonoProcess *p;
2711 pid = waitpid (-1, &status, WNOHANG);
2712 } while (pid == -1 && errno == EINTR);
2718 * This can run concurrently with the code in the rest of this module.
2720 for (p = mono_processes; p; p = p->next) {
2721 if (p->pid == pid) {
2726 p->pid = 0; /* this pid doesn't exist anymore, clear it */
2728 mono_os_sem_post (&p->exit_sem);
2729 mono_memory_barrier ();
2730 /* Mark this as freeable, the pointer becomes invalid afterwards */
2739 process_add_sigchld_handler (void)
2742 struct sigaction sa;
2744 sa.sa_sigaction = mono_sigchld_signal_handler;
2745 sigemptyset (&sa.sa_mask);
2746 sa.sa_flags = SA_NOCLDSTOP | SA_SIGINFO;
2747 g_assert (sigaction (SIGCHLD, &sa, &previous_chld_sa) != -1);
2748 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "Added SIGCHLD handler");
2753 process_wait (gpointer handle, guint32 timeout, gboolean *alerted)
2755 WapiHandle_process *process_handle;
2756 pid_t pid G_GNUC_UNUSED, ret;
2759 struct MonoProcess *mp;
2761 /* FIXME: We can now easily wait on processes that aren't our own children,
2762 * but WaitFor*Object won't call us for pseudo handles. */
2763 g_assert ((GPOINTER_TO_UINT (handle) & _WAPI_PROCESS_UNHANDLED) != _WAPI_PROCESS_UNHANDLED);
2765 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s (%p, %u)", __func__, handle, timeout);
2770 process_handle = lookup_process_handle (handle);
2771 if (!process_handle) {
2772 g_warning ("%s: error looking up process handle %p", __func__, handle);
2776 if (process_handle->exited) {
2777 /* We've already done this one */
2778 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s (%p, %u): Process already exited", __func__, handle, timeout);
2779 return WAIT_OBJECT_0;
2782 pid = process_handle->id;
2784 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s (%p, %u): PID: %d", __func__, handle, timeout, pid);
2786 /* We don't need to lock mono_processes here, the entry
2787 * has a handle_count > 0 which means it will not be freed. */
2788 mp = process_handle->mono_process;
2792 if (pid == mono_process_current_pid ()) {
2793 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s (%p, %u): waiting on current process", __func__, handle, timeout);
2794 return WAIT_TIMEOUT;
2797 /* This path is used when calling Process.HasExited, so
2798 * it is only used to poll the state of the process, not
2799 * to actually wait on it to exit */
2800 g_assert (timeout == 0);
2802 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s (%p, %u): waiting on non-child process", __func__, handle, timeout);
2804 res = waitpid (pid, &status, WNOHANG);
2806 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s (%p, %u): non-child process WAIT_TIMEOUT", __func__, handle, timeout);
2807 return WAIT_TIMEOUT;
2810 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s (%p, %u): non-child process waited successfully", __func__, handle, timeout);
2811 return WAIT_OBJECT_0;
2814 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s (%p, %u): non-child process WAIT_FAILED, error : %s (%d))", __func__, handle, timeout, g_strerror (errno), errno);
2818 start = mono_msec_ticks ();
2822 if (timeout != INFINITE) {
2823 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s (%p, %u): waiting on semaphore for %li ms...",
2824 __func__, handle, timeout, (long)(timeout - (now - start)));
2825 ret = mono_os_sem_timedwait (&mp->exit_sem, (timeout - (now - start)), alerted ? MONO_SEM_FLAGS_ALERTABLE : MONO_SEM_FLAGS_NONE);
2827 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s (%p, %u): waiting on semaphore forever...",
2828 __func__, handle, timeout);
2829 ret = mono_os_sem_wait (&mp->exit_sem, alerted ? MONO_SEM_FLAGS_ALERTABLE : MONO_SEM_FLAGS_NONE);
2832 if (ret == MONO_SEM_TIMEDWAIT_RET_SUCCESS) {
2833 /* Success, process has exited */
2834 mono_os_sem_post (&mp->exit_sem);
2838 if (ret == MONO_SEM_TIMEDWAIT_RET_TIMEDOUT) {
2839 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s (%p, %u): WAIT_TIMEOUT (timeout = 0)", __func__, handle, timeout);
2840 return WAIT_TIMEOUT;
2843 now = mono_msec_ticks ();
2844 if (now - start >= timeout) {
2845 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s (%p, %u): WAIT_TIMEOUT", __func__, handle, timeout);
2846 return WAIT_TIMEOUT;
2849 if (alerted && ret == MONO_SEM_TIMEDWAIT_RET_ALERTED) {
2850 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s (%p, %u): WAIT_IO_COMPLETION", __func__, handle, timeout);
2852 return WAIT_IO_COMPLETION;
2856 /* Process must have exited */
2857 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s (%p, %u): Waited successfully", __func__, handle, timeout);
2859 status = mp ? mp->status : 0;
2860 if (WIFSIGNALED (status))
2861 process_handle->exitstatus = 128 + WTERMSIG (status);
2863 process_handle->exitstatus = WEXITSTATUS (status);
2864 _wapi_time_t_to_filetime (time (NULL), &process_handle->exit_time);
2866 process_handle->exited = TRUE;
2868 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s (%p, %u): Setting pid %d signalled, exit status %d",
2869 __func__, handle, timeout, process_handle->id, process_handle->exitstatus);
2871 mono_w32handle_set_signal_state (handle, TRUE, TRUE);
2873 return WAIT_OBJECT_0;
2877 wapi_processes_cleanup (void)
2879 g_free (cli_launcher);