2 * processes.c: Process handles
5 * Dick Porter (dick@ximian.com)
7 * (C) 2002-2011 Novell, Inc.
8 * Copyright 2011 Xamarin Inc
19 #include <sys/types.h>
25 #include <sys/resource.h>
27 #ifdef HAVE_SYS_PARAM_H
28 #include <sys/param.h>
32 #ifdef HAVE_SYS_MKDEV_H
33 #include <sys/mkdev.h>
36 /* sys/resource.h (for rusage) is required when using osx 10.3 (but not 10.4) */
38 #include <TargetConditionals.h>
39 #include <sys/resource.h>
46 #if defined(PLATFORM_MACOSX) || defined(__OpenBSD__)
48 #include <sys/sysctl.h>
49 # if !defined(__OpenBSD__)
50 # include <sys/utsname.h>
54 #ifdef PLATFORM_SOLARIS
55 /* procfs.h cannot be included if this define is set, but it seems to work fine if it is undefined */
56 #if _FILE_OFFSET_BITS == 64
57 #undef _FILE_OFFSET_BITS
59 #define _FILE_OFFSET_BITS 64
66 #include <KernelKit.h>
69 #include <mono/io-layer/wapi.h>
70 #include <mono/io-layer/wapi-private.h>
71 #include <mono/io-layer/handles-private.h>
72 #include <mono/io-layer/misc-private.h>
73 #include <mono/io-layer/process-private.h>
74 #include <mono/io-layer/threads.h>
75 #include <mono/utils/strenc.h>
76 #include <mono/utils/mono-path.h>
77 #include <mono/io-layer/timefuncs-private.h>
78 #include <mono/utils/mono-time.h>
79 #include <mono/utils/mono-membar.h>
80 #include <mono/utils/mono-mutex.h>
81 #include <mono/utils/mono-signal-handler.h>
83 /* The process' environment strings */
84 #if defined(__APPLE__) && !defined (__arm__)
85 /* Apple defines this in crt_externs.h but doesn't provide that header for
86 * arm-apple-darwin9. We'll manually define the symbol on Apple as it does
87 * in fact exist on all implementations (so far)
89 char ***_NSGetEnviron(void);
90 #define environ (*_NSGetEnviron())
92 extern char **environ;
96 #define DEBUG(...) g_message(__VA_ARGS__)
97 #define DEBUG_ENABLED 1
102 static guint32 process_wait (gpointer handle, guint32 timeout, gboolean alertable);
103 static void process_close (gpointer handle, gpointer data);
104 static gboolean is_pid_valid (pid_t pid);
106 #if !defined(__OpenBSD__)
108 open_process_map (int pid, const char *mode);
111 struct _WapiHandleOps _wapi_process_ops = {
112 process_close, /* close_shared */
116 process_wait, /* special_wait */
121 static struct sigaction previous_chld_sa;
123 static mono_once_t process_sig_chld_once = MONO_ONCE_INIT;
124 static void process_add_sigchld_handler (void);
126 /* The signal-safe logic to use mono_processes goes like this:
127 * - The list must be safe to traverse for the signal handler at all times.
128 * It's safe to: prepend an entry (which is a single store to 'mono_processes'),
129 * unlink an entry (assuming the unlinked entry isn't freed and doesn't
130 * change its 'next' pointer so that it can still be traversed).
131 * When cleaning up we first unlink an entry, then we verify that
132 * the read lock isn't locked. Then we can free the entry, since
133 * we know that nobody is using the old version of the list (including
134 * the unlinked entry).
135 * We also need to lock when adding and cleaning up so that those two
136 * operations don't mess with eachother. (This lock is not used in the
139 static struct MonoProcess *mono_processes = NULL;
140 static volatile gint32 mono_processes_read_lock = 0;
141 static volatile gint32 mono_processes_cleaning_up = 0;
142 static mono_mutex_t mono_processes_mutex;
143 static void mono_processes_cleanup (void);
145 static gpointer current_process;
146 static char *cli_launcher;
148 static WapiHandle_process *
149 lookup_process_handle (gpointer handle)
151 WapiHandle_process *process_data;
154 ret = _wapi_lookup_handle (handle, WAPI_HANDLE_PROCESS,
155 (gpointer *)&process_data);
161 /* Check if a pid is valid - i.e. if a process exists with this pid. */
163 is_pid_valid (pid_t pid)
165 gboolean result = FALSE;
167 #if defined(PLATFORM_MACOSX) || defined(__OpenBSD__)
168 if (((kill(pid, 0) == 0) || (errno == EPERM)) && pid != 0)
170 #elif defined(__HAIKU__)
172 if (get_team_info ((team_id)pid, &teamInfo) == B_OK)
175 char *dir = g_strdup_printf ("/proc/%d", pid);
176 if (!access (dir, F_OK))
185 process_set_defaults (WapiHandle_process *process_handle)
187 /* These seem to be the defaults on w2k */
188 process_handle->min_working_set = 204800;
189 process_handle->max_working_set = 1413120;
191 _wapi_time_t_to_filetime (time (NULL), &process_handle->create_time);
195 len16 (const gunichar2 *str)
206 utf16_concat (const gunichar2 *first, ...)
213 va_start (args, first);
214 total += len16 (first);
215 for (s = va_arg (args, gunichar2 *); s != NULL; s = va_arg(args, gunichar2 *)){
220 ret = g_new (gunichar2, total + 1);
226 for (s = first; *s != 0; s++)
228 va_start (args, first);
229 for (s = va_arg (args, gunichar2 *); s != NULL; s = va_arg (args, gunichar2 *)){
232 for (p = s; *p != 0; p++)
240 static const gunichar2 utf16_space_bytes [2] = { 0x20, 0 };
241 static const gunichar2 *utf16_space = utf16_space_bytes;
242 static const gunichar2 utf16_quote_bytes [2] = { 0x22, 0 };
243 static const gunichar2 *utf16_quote = utf16_quote_bytes;
248 print_utf16 (gunichar2 *str)
252 res = g_utf16_to_utf8 (str, -1, NULL, NULL, NULL);
253 g_print ("%s\n", res);
258 /* Implemented as just a wrapper around CreateProcess () */
260 ShellExecuteEx (WapiShellExecuteInfo *sei)
263 WapiProcessInformation process_info;
267 /* w2k just segfaults here, but we can do better than
270 SetLastError (ERROR_INVALID_PARAMETER);
274 if (sei->lpFile == NULL)
275 /* w2k returns TRUE for this, for some reason. */
278 /* Put both executable and parameters into the second argument
279 * to CreateProcess (), so it searches $PATH. The conversion
280 * into and back out of utf8 is because there is no
281 * g_strdup_printf () equivalent for gunichar2 :-(
283 args = utf16_concat (utf16_quote, sei->lpFile, utf16_quote, sei->lpParameters == NULL ? NULL : utf16_space, sei->lpParameters, NULL);
285 SetLastError (ERROR_INVALID_DATA);
288 ret = CreateProcess (NULL, args, NULL, NULL, TRUE,
289 CREATE_UNICODE_ENVIRONMENT, NULL,
290 sei->lpDirectory, NULL, &process_info);
293 if (!ret && GetLastError () == ERROR_OUTOFMEMORY)
297 static char *handler;
298 static gunichar2 *handler_utf16;
300 if (handler_utf16 == (gunichar2 *)-1)
303 #ifdef PLATFORM_MACOSX
304 handler = g_strdup ("/usr/bin/open");
307 * On Linux, try: xdg-open, the FreeDesktop standard way of doing it,
308 * if that fails, try to use gnome-open, then kfmclient
310 handler = g_find_program_in_path ("xdg-open");
311 if (handler == NULL){
312 handler = g_find_program_in_path ("gnome-open");
313 if (handler == NULL){
314 handler = g_find_program_in_path ("kfmclient");
315 if (handler == NULL){
316 handler_utf16 = (gunichar2 *) -1;
319 /* kfmclient needs exec argument */
321 handler = g_strconcat (old, " exec",
328 handler_utf16 = g_utf8_to_utf16 (handler, -1, NULL, NULL, NULL);
331 /* Put quotes around the filename, in case it's a url
332 * that contains #'s (CreateProcess() calls
333 * g_shell_parse_argv(), which deliberately throws
334 * away anything after an unquoted #). Fixes bug
337 args = utf16_concat (handler_utf16, utf16_space, utf16_quote,
338 sei->lpFile, utf16_quote,
339 sei->lpParameters == NULL ? NULL : utf16_space,
340 sei->lpParameters, NULL);
342 SetLastError (ERROR_INVALID_DATA);
345 ret = CreateProcess (NULL, args, NULL, NULL, TRUE,
346 CREATE_UNICODE_ENVIRONMENT, NULL,
347 sei->lpDirectory, NULL, &process_info);
350 if (GetLastError () != ERROR_OUTOFMEMORY)
351 SetLastError (ERROR_INVALID_DATA);
354 /* Shell exec should not return a process handle when it spawned a GUI thing, like a browser. */
355 CloseHandle (process_info.hProcess);
356 process_info.hProcess = NULL;
359 if (sei->fMask & SEE_MASK_NOCLOSEPROCESS)
360 sei->hProcess = process_info.hProcess;
362 CloseHandle (process_info.hProcess);
368 is_managed_binary (const char *filename)
370 int original_errno = errno;
371 #if defined(HAVE_LARGE_FILE_SUPPORT) && defined(O_LARGEFILE)
372 int file = open (filename, O_RDONLY | O_LARGEFILE);
374 int file = open (filename, O_RDONLY);
377 unsigned char buffer[8];
378 off_t file_size, optional_header_offset;
379 off_t pe_header_offset;
380 gboolean managed = FALSE;
382 guint32 first_word, second_word;
384 /* If we are unable to open the file, then we definitely
385 * can't say that it is managed. The child mono process
386 * probably wouldn't be able to open it anyway.
389 errno = original_errno;
393 /* Retrieve the length of the file for future sanity checks. */
394 file_size = lseek (file, 0, SEEK_END);
395 lseek (file, 0, SEEK_SET);
397 /* We know we need to read a header field at offset 60. */
401 num_read = read (file, buffer, 2);
403 if ((num_read != 2) || (buffer[0] != 'M') || (buffer[1] != 'Z'))
406 new_offset = lseek (file, 60, SEEK_SET);
408 if (new_offset != 60)
411 num_read = read (file, buffer, 4);
415 pe_header_offset = buffer[0]
420 if (pe_header_offset + 24 > file_size)
423 new_offset = lseek (file, pe_header_offset, SEEK_SET);
425 if (new_offset != pe_header_offset)
428 num_read = read (file, buffer, 4);
430 if ((num_read != 4) || (buffer[0] != 'P') || (buffer[1] != 'E') || (buffer[2] != 0) || (buffer[3] != 0))
434 * Verify that the header we want in the optional header data
435 * is present in this binary.
437 new_offset = lseek (file, pe_header_offset + 20, SEEK_SET);
439 if (new_offset != pe_header_offset + 20)
442 num_read = read (file, buffer, 2);
444 if ((num_read != 2) || ((buffer[0] | (buffer[1] << 8)) < 216))
447 /* Read the CLR header address and size fields. These will be
448 * zero if the binary is not managed.
450 optional_header_offset = pe_header_offset + 24;
451 new_offset = lseek (file, optional_header_offset + 208, SEEK_SET);
453 if (new_offset != optional_header_offset + 208)
456 num_read = read (file, buffer, 8);
458 /* We are not concerned with endianness, only with
459 * whether it is zero or not.
461 first_word = *(guint32 *)&buffer[0];
462 second_word = *(guint32 *)&buffer[4];
464 if ((num_read != 8) || (first_word == 0) || (second_word == 0))
471 errno = original_errno;
476 CreateProcessWithLogonW (const gunichar2 *username,
477 const gunichar2 *domain,
478 const gunichar2 *password,
479 const guint32 logonFlags,
480 const gunichar2 *appname,
481 const gunichar2 *cmdline,
482 guint32 create_flags,
484 const gunichar2 *cwd,
485 WapiStartupInfo *startup,
486 WapiProcessInformation *process_info)
488 /* FIXME: use user information */
489 return CreateProcess (appname, cmdline, NULL, NULL, FALSE, create_flags, env, cwd, startup, process_info);
493 is_executable (const char *prog)
496 if (access (prog, X_OK) != 0)
498 if (stat (prog, &buf))
500 if (S_ISREG (buf.st_mode))
506 switch_dir_separators (char *path)
508 size_t i, pathLength = strlen(path);
510 /* Turn all the slashes round the right way, except for \' */
511 /* There are probably other characters that need to be excluded as well. */
512 for (i = 0; i < pathLength; i++) {
513 if (path[i] == '\\' && i < pathLength - 1 && path[i+1] != '\'' )
518 gboolean CreateProcess (const gunichar2 *appname, const gunichar2 *cmdline,
519 WapiSecurityAttributes *process_attrs G_GNUC_UNUSED,
520 WapiSecurityAttributes *thread_attrs G_GNUC_UNUSED,
521 gboolean inherit_handles, guint32 create_flags,
522 gpointer new_environ, const gunichar2 *cwd,
523 WapiStartupInfo *startup,
524 WapiProcessInformation *process_info)
526 char *cmd = NULL, *prog = NULL, *full_prog = NULL, *args = NULL, *args_after_prog = NULL;
527 char *dir = NULL, **env_strings = NULL, **argv = NULL;
528 guint32 i, env_count = 0;
529 gboolean ret = FALSE;
531 WapiHandle_process process_handle = {0}, *process_handle_data;
533 int in_fd, out_fd, err_fd;
536 int startup_pipe [2] = {-1, -1};
538 struct MonoProcess *mono_process;
539 gboolean fork_failed = FALSE;
541 mono_once (&process_sig_chld_once, process_add_sigchld_handler);
543 /* appname and cmdline specify the executable and its args:
545 * If appname is not NULL, it is the name of the executable.
546 * Otherwise the executable is the first token in cmdline.
548 * Executable searching:
550 * If appname is not NULL, it can specify the full path and
551 * file name, or else a partial name and the current directory
552 * will be used. There is no additional searching.
554 * If appname is NULL, the first whitespace-delimited token in
555 * cmdline is used. If the name does not contain a full
556 * directory path, the search sequence is:
558 * 1) The directory containing the current process
559 * 2) The current working directory
560 * 3) The windows system directory (Ignored)
561 * 4) The windows directory (Ignored)
564 * Just to make things more interesting, tokens can contain
565 * white space if they are surrounded by quotation marks. I'm
566 * beginning to understand just why windows apps are generally
567 * so crap, with an API like this :-(
569 if (appname != NULL) {
570 cmd = mono_unicode_to_external (appname);
572 DEBUG ("%s: unicode conversion returned NULL",
575 SetLastError (ERROR_PATH_NOT_FOUND);
579 switch_dir_separators(cmd);
582 if (cmdline != NULL) {
583 args = mono_unicode_to_external (cmdline);
585 DEBUG ("%s: unicode conversion returned NULL", __func__);
587 SetLastError (ERROR_PATH_NOT_FOUND);
593 dir = mono_unicode_to_external (cwd);
595 DEBUG ("%s: unicode conversion returned NULL", __func__);
597 SetLastError (ERROR_PATH_NOT_FOUND);
601 /* Turn all the slashes round the right way */
602 switch_dir_separators(dir);
606 /* We can't put off locating the executable any longer :-( */
609 if (g_ascii_isalpha (cmd[0]) && (cmd[1] == ':')) {
610 /* Strip off the drive letter. I can't
611 * believe that CP/M holdover is still
614 g_memmove (cmd, cmd+2, strlen (cmd)-2);
615 cmd[strlen (cmd)-2] = '\0';
618 unquoted = g_shell_unquote (cmd, NULL);
619 if (unquoted[0] == '/') {
620 /* Assume full path given */
621 prog = g_strdup (unquoted);
623 /* Executable existing ? */
624 if (!is_executable (prog)) {
625 DEBUG ("%s: Couldn't find executable %s",
628 SetLastError (ERROR_FILE_NOT_FOUND);
632 /* Search for file named by cmd in the current
635 char *curdir = g_get_current_dir ();
637 prog = g_strdup_printf ("%s/%s", curdir, unquoted);
640 /* And make sure it's executable */
641 if (!is_executable (prog)) {
642 DEBUG ("%s: Couldn't find executable %s",
645 SetLastError (ERROR_FILE_NOT_FOUND);
651 args_after_prog = args;
656 /* Dig out the first token from args, taking quotation
660 /* First, strip off all leading whitespace */
661 args = g_strchug (args);
663 /* args_after_prog points to the contents of args
664 * after token has been set (otherwise argv[0] is
667 args_after_prog = args;
669 /* Assume the opening quote will always be the first
672 if (args[0] == '\"' || args [0] == '\'') {
674 for (i = 1; args[i] != '\0' && args[i] != quote; i++);
675 if (args [i + 1] == '\0' || g_ascii_isspace (args[i+1])) {
676 /* We found the first token */
677 token = g_strndup (args+1, i-1);
678 args_after_prog = g_strchug (args + i + 1);
680 /* Quotation mark appeared in the
681 * middle of the token. Just give the
682 * whole first token, quotes and all,
689 /* No quote mark, or malformed */
690 for (i = 0; args[i] != '\0'; i++) {
691 if (g_ascii_isspace (args[i])) {
692 token = g_strndup (args, i);
693 args_after_prog = args + i + 1;
699 if (token == NULL && args[0] != '\0') {
700 /* Must be just one token in the string */
701 token = g_strdup (args);
702 args_after_prog = NULL;
707 DEBUG ("%s: Couldn't find what to exec", __func__);
709 SetLastError (ERROR_PATH_NOT_FOUND);
713 /* Turn all the slashes round the right way. Only for
716 switch_dir_separators(token);
718 if (g_ascii_isalpha (token[0]) && (token[1] == ':')) {
719 /* Strip off the drive letter. I can't
720 * believe that CP/M holdover is still
723 g_memmove (token, token+2, strlen (token)-2);
724 token[strlen (token)-2] = '\0';
727 if (token[0] == '/') {
728 /* Assume full path given */
729 prog = g_strdup (token);
731 /* Executable existing ? */
732 if (!is_executable (prog)) {
733 DEBUG ("%s: Couldn't find executable %s",
736 SetLastError (ERROR_FILE_NOT_FOUND);
740 char *curdir = g_get_current_dir ();
742 /* FIXME: Need to record the directory
743 * containing the current process, and check
744 * that for the new executable as the first
748 prog = g_strdup_printf ("%s/%s", curdir, token);
751 /* I assume X_OK is the criterion to use,
754 if (!is_executable (prog)) {
756 prog = g_find_program_in_path (token);
758 DEBUG ("%s: Couldn't find executable %s", __func__, token);
761 SetLastError (ERROR_FILE_NOT_FOUND);
770 DEBUG ("%s: Exec prog [%s] args [%s]", __func__, prog,
773 /* Check for CLR binaries; if found, we will try to invoke
774 * them using the same mono binary that started us.
776 if (is_managed_binary (prog)) {
777 gunichar2 *newapp = NULL, *newcmd;
781 newapp = mono_unicode_from_external (cli_launcher, &bytes_ignored);
783 newapp = mono_unicode_from_external ("mono", &bytes_ignored);
785 if (newapp != NULL) {
786 if (appname != NULL) {
787 newcmd = utf16_concat (newapp, utf16_space,
788 appname, utf16_space,
791 newcmd = utf16_concat (newapp, utf16_space,
795 g_free ((gunichar2 *)newapp);
797 if (newcmd != NULL) {
798 ret = CreateProcess (NULL, newcmd,
802 create_flags, new_environ,
806 g_free ((gunichar2 *)newcmd);
813 if (args_after_prog != NULL && *args_after_prog) {
816 qprog = g_shell_quote (prog);
817 full_prog = g_strconcat (qprog, " ", args_after_prog, NULL);
820 full_prog = g_shell_quote (prog);
823 ret = g_shell_parse_argv (full_prog, NULL, &argv, &gerr);
825 g_message ("CreateProcess: %s\n", gerr->message);
831 if (startup != NULL && startup->dwFlags & STARTF_USESTDHANDLES) {
832 in_fd = GPOINTER_TO_UINT (startup->hStdInput);
833 out_fd = GPOINTER_TO_UINT (startup->hStdOutput);
834 err_fd = GPOINTER_TO_UINT (startup->hStdError);
836 in_fd = GPOINTER_TO_UINT (GetStdHandle (STD_INPUT_HANDLE));
837 out_fd = GPOINTER_TO_UINT (GetStdHandle (STD_OUTPUT_HANDLE));
838 err_fd = GPOINTER_TO_UINT (GetStdHandle (STD_ERROR_HANDLE));
841 process_handle.proc_name = g_strdup (prog);
843 process_set_defaults (&process_handle);
845 handle = _wapi_handle_new (WAPI_HANDLE_PROCESS, &process_handle);
846 if (handle == _WAPI_HANDLE_INVALID) {
847 g_warning ("%s: error creating process handle", __func__);
850 SetLastError (ERROR_OUTOFMEMORY);
854 /* new_environ is a block of NULL-terminated strings, which
855 * is itself NULL-terminated. Of course, passing an array of
856 * string pointers would have made things too easy :-(
858 * If new_environ is not NULL it specifies the entire set of
859 * environment variables in the new process. Otherwise the
860 * new process inherits the same environment.
863 gunichar2 *new_environp;
865 /* Count the number of strings */
866 for (new_environp = (gunichar2 *)new_environ; *new_environp;
869 while (*new_environp) {
874 /* +2: one for the process handle value, and the last
877 env_strings = g_new0 (char *, env_count + 2);
879 /* Copy each environ string into 'strings' turning it
880 * into utf8 (or the requested encoding) at the same
884 for (new_environp = (gunichar2 *)new_environ; *new_environp;
886 env_strings[env_count] = mono_unicode_to_external (new_environp);
888 while (*new_environp) {
893 for (i = 0; environ[i] != NULL; i++)
896 /* +2: one for the process handle value, and the last
899 env_strings = g_new0 (char *, env_count + 2);
901 /* Copy each environ string into 'strings' turning it
902 * into utf8 (or the requested encoding) at the same
906 for (i = 0; environ[i] != NULL; i++) {
907 env_strings[env_count] = g_strdup (environ[i]);
912 /* Create a pipe to make sure the child doesn't exit before
913 * we can add the process to the linked list of mono_processes */
914 if (pipe (startup_pipe) == -1) {
915 /* Could not create the pipe to synchroniz process startup. We'll just not synchronize.
916 * This is just for a very hard to hit race condition in the first place */
917 startup_pipe [0] = startup_pipe [1] = -1;
918 DEBUG ("%s: new process startup not synchronized. We may not notice if the newly created process exits immediately.", __func__);
921 thr_ret = _wapi_handle_lock_shared_handles ();
922 g_assert (thr_ret == 0);
927 SetLastError (ERROR_OUTOFMEMORY);
931 } else if (pid == 0) {
934 if (startup_pipe [0] != -1) {
935 /* Wait until the parent has updated it's internal data */
936 ssize_t _i G_GNUC_UNUSED = read (startup_pipe [0], &dummy, 1);
937 DEBUG ("%s: child: parent has completed its setup", __func__);
938 close (startup_pipe [0]);
939 close (startup_pipe [1]);
942 /* should we detach from the process group? */
944 /* Connect stdin, stdout and stderr */
949 if (inherit_handles != TRUE) {
950 /* FIXME: do something here */
953 /* Close all file descriptors */
954 for (i = wapi_getdtablesize () - 1; i > 2; i--)
958 DEBUG ("%s: exec()ing [%s] in dir [%s]", __func__, cmd,
959 dir == NULL?".":dir);
960 for (i = 0; argv[i] != NULL; i++)
961 g_message ("arg %d: [%s]", i, argv[i]);
963 for (i = 0; env_strings[i] != NULL; i++)
964 g_message ("env %d: [%s]", i, env_strings[i]);
968 if (dir != NULL && chdir (dir) == -1) {
974 execve (argv[0], argv, env_strings);
981 process_handle_data = lookup_process_handle (handle);
982 if (!process_handle_data) {
983 g_warning ("%s: error looking up process handle %p", __func__,
985 _wapi_handle_unref (handle);
989 process_handle_data->id = pid;
991 /* Add our mono_process into the linked list of mono_processes */
992 mono_process = (struct MonoProcess *) g_malloc0 (sizeof (struct MonoProcess));
993 mono_process->pid = pid;
994 mono_process->handle_count = 1;
995 if (MONO_SEM_INIT (&mono_process->exit_sem, 0) != 0) {
996 /* If we can't create the exit semaphore, we just don't add anything
997 * to our list of mono processes. Waiting on the process will return
999 g_warning ("%s: could not create exit semaphore for process.", strerror (errno));
1000 g_free (mono_process);
1002 /* Keep the process handle artificially alive until the process
1003 * exits so that the information in the handle isn't lost. */
1004 _wapi_handle_ref (handle);
1005 mono_process->handle = handle;
1007 process_handle_data->mono_process = mono_process;
1009 mono_mutex_lock (&mono_processes_mutex);
1010 mono_process->next = mono_processes;
1011 mono_processes = mono_process;
1012 mono_mutex_unlock (&mono_processes_mutex);
1015 if (process_info != NULL) {
1016 process_info->hProcess = handle;
1017 process_info->dwProcessId = pid;
1019 /* FIXME: we might need to handle the thread info some
1022 process_info->hThread = INVALID_HANDLE_VALUE;
1023 process_info->dwThreadId = 0;
1027 _wapi_handle_unlock_shared_handles ();
1030 _wapi_handle_unref (handle);
1032 if (startup_pipe [1] != -1) {
1033 /* Write 1 byte, doesn't matter what */
1034 ssize_t _i G_GNUC_UNUSED = write (startup_pipe [1], startup_pipe, 1);
1035 close (startup_pipe [0]);
1036 close (startup_pipe [1]);
1051 g_strfreev (env_strings);
1055 DEBUG ("%s: returning handle %p for pid %d", __func__, handle,
1058 /* Check if something needs to be cleaned up. */
1059 mono_processes_cleanup ();
1065 process_set_name (WapiHandle_process *process_handle)
1067 char *progname, *utf8_progname, *slash;
1069 progname = g_get_prgname ();
1070 utf8_progname = mono_utf8_from_external (progname);
1072 DEBUG ("%s: using [%s] as prog name", __func__, progname);
1074 if (utf8_progname) {
1075 slash = strrchr (utf8_progname, '/');
1077 process_handle->proc_name = g_strdup (slash+1);
1079 process_handle->proc_name = g_strdup (utf8_progname);
1080 g_free (utf8_progname);
1085 wapi_processes_init (void)
1087 pid_t pid = _wapi_getpid ();
1088 WapiHandle_process process_handle = {0};
1090 _wapi_handle_register_capabilities (WAPI_HANDLE_PROCESS,
1091 WAPI_HANDLE_CAP_WAIT |
1092 WAPI_HANDLE_CAP_SPECIAL_WAIT);
1094 process_handle.id = pid;
1096 process_set_defaults (&process_handle);
1097 process_set_name (&process_handle);
1099 current_process = _wapi_handle_new (WAPI_HANDLE_PROCESS,
1101 g_assert (current_process);
1105 _wapi_process_duplicate (void)
1107 _wapi_handle_ref (current_process);
1109 return current_process;
1112 /* Returns a pseudo handle that doesn't need to be closed afterwards */
1114 GetCurrentProcess (void)
1116 return _WAPI_PROCESS_CURRENT;
1120 GetProcessId (gpointer handle)
1122 WapiHandle_process *process_handle;
1124 if (WAPI_IS_PSEUDO_PROCESS_HANDLE (handle))
1125 /* This is a pseudo handle */
1126 return WAPI_HANDLE_TO_PID (handle);
1128 process_handle = lookup_process_handle (handle);
1129 if (!process_handle) {
1130 SetLastError (ERROR_INVALID_HANDLE);
1134 return process_handle->id;
1138 process_open_compare (gpointer handle, gpointer user_data)
1141 WapiHandle_process *process_handle;
1144 g_assert (!WAPI_IS_PSEUDO_PROCESS_HANDLE (handle));
1146 process_handle = lookup_process_handle (handle);
1147 g_assert (process_handle);
1149 DEBUG ("%s: looking at process %d", __func__, process_handle->id);
1151 checking_pid = process_handle->id;
1153 if (checking_pid == 0)
1156 wanted_pid = GPOINTER_TO_UINT (user_data);
1158 /* It's possible to have more than one process handle with the
1159 * same pid, but only the one running process can be
1162 if (checking_pid == wanted_pid &&
1163 !_wapi_handle_issignalled (handle)) {
1164 /* If the handle is blown away in the window between
1165 * returning TRUE here and _wapi_search_handle pinging
1166 * the timestamp, the search will continue
1175 CloseProcess (gpointer handle)
1177 if (WAPI_IS_PSEUDO_PROCESS_HANDLE (handle))
1179 return CloseHandle (handle);
1183 * The caller owns the returned handle and must call CloseProcess () on it to clean it up.
1186 OpenProcess (guint32 req_access G_GNUC_UNUSED, gboolean inherit G_GNUC_UNUSED, guint32 pid)
1188 /* Find the process handle that corresponds to pid */
1189 gpointer handle = NULL;
1191 DEBUG ("%s: looking for process %d", __func__, pid);
1193 handle = _wapi_search_handle (WAPI_HANDLE_PROCESS,
1194 process_open_compare,
1195 GUINT_TO_POINTER (pid), NULL, TRUE);
1197 if (is_pid_valid (pid)) {
1198 /* Return a pseudo handle for processes we
1199 * don't have handles for
1201 return WAPI_PID_TO_HANDLE (pid);
1203 DEBUG ("%s: Can't find pid %d", __func__, pid);
1205 SetLastError (ERROR_PROC_NOT_FOUND);
1211 /* _wapi_search_handle () already added a ref */
1216 GetExitCodeProcess (gpointer process, guint32 *code)
1218 WapiHandle_process *process_handle;
1224 if (WAPI_IS_PSEUDO_PROCESS_HANDLE (process)) {
1225 pid = WAPI_HANDLE_TO_PID (process);
1226 /* This is a pseudo handle, so we don't know what the
1227 * exit code was, but we can check whether it's alive or not
1229 if (is_pid_valid (pid)) {
1230 *code = STILL_ACTIVE;
1237 process_handle = lookup_process_handle (process);
1238 if (!process_handle) {
1239 DEBUG ("%s: Can't find process %p", __func__, process);
1244 /* A process handle is only signalled if the process has exited
1245 * and has been waited for */
1247 /* Make sure any process exit has been noticed, before
1248 * checking if the process is signalled. Fixes bug 325463.
1250 process_wait (process, 0, TRUE);
1252 if (_wapi_handle_issignalled (process))
1253 *code = process_handle->exitstatus;
1255 *code = STILL_ACTIVE;
1261 GetProcessTimes (gpointer process, WapiFileTime *create_time,
1262 WapiFileTime *exit_time, WapiFileTime *kernel_time,
1263 WapiFileTime *user_time)
1265 WapiHandle_process *process_handle;
1266 gboolean ku_times_set = FALSE;
1268 if (create_time == NULL || exit_time == NULL || kernel_time == NULL ||
1270 /* Not sure if w32 allows NULLs here or not */
1273 if (WAPI_IS_PSEUDO_PROCESS_HANDLE (process))
1274 /* This is a pseudo handle, so just fail for now
1278 process_handle = lookup_process_handle (process);
1279 if (!process_handle) {
1280 DEBUG ("%s: Can't find process %p", __func__, process);
1285 *create_time = process_handle->create_time;
1287 /* A process handle is only signalled if the process has
1288 * exited. Otherwise exit_time isn't set
1290 if (_wapi_handle_issignalled (process))
1291 *exit_time = process_handle->exit_time;
1293 #ifdef HAVE_GETRUSAGE
1294 if (process_handle->id == getpid ()) {
1295 struct rusage time_data;
1296 if (getrusage (RUSAGE_SELF, &time_data) == 0) {
1298 ku_times_set = TRUE;
1299 tick_val = (guint64)time_data.ru_utime.tv_sec * 10000000 + (guint64)time_data.ru_utime.tv_usec * 10;
1300 _wapi_guint64_to_filetime (tick_val, user_time);
1301 tick_val = (guint64)time_data.ru_stime.tv_sec * 10000000 + (guint64)time_data.ru_stime.tv_usec * 10;
1302 _wapi_guint64_to_filetime (tick_val, kernel_time);
1306 if (!ku_times_set) {
1307 memset (kernel_time, 0, sizeof (WapiFileTime));
1308 memset (user_time, 0, sizeof (WapiFileTime));
1316 gpointer address_start;
1317 gpointer address_end;
1319 gpointer address_offset;
1325 static void free_procmodule (WapiProcModule *mod)
1327 if (mod->perms != NULL) {
1328 g_free (mod->perms);
1330 if (mod->filename != NULL) {
1331 g_free (mod->filename);
1336 static gint find_procmodule (gconstpointer a, gconstpointer b)
1338 WapiProcModule *want = (WapiProcModule *)a;
1339 WapiProcModule *compare = (WapiProcModule *)b;
1341 if ((want->device == compare->device) &&
1342 (want->inode == compare->inode)) {
1349 #ifdef PLATFORM_MACOSX
1350 #include <mach-o/dyld.h>
1351 #include <mach-o/getsect.h>
1353 static GSList *load_modules (void)
1356 WapiProcModule *mod;
1357 uint32_t count = _dyld_image_count ();
1360 for (i = 0; i < count; i++) {
1361 #if SIZEOF_VOID_P == 8
1362 const struct mach_header_64 *hdr;
1363 const struct section_64 *sec;
1365 const struct mach_header *hdr;
1366 const struct section *sec;
1370 name = _dyld_get_image_name (i);
1371 #if SIZEOF_VOID_P == 8
1372 hdr = (const struct mach_header_64*)_dyld_get_image_header (i);
1373 sec = getsectbynamefromheader_64 (hdr, SEG_DATA, SECT_DATA);
1375 hdr = _dyld_get_image_header (i);
1376 sec = getsectbynamefromheader (hdr, SEG_DATA, SECT_DATA);
1379 /* Some dynlibs do not have data sections on osx (#533893) */
1384 mod = g_new0 (WapiProcModule, 1);
1385 mod->address_start = GINT_TO_POINTER (sec->addr);
1386 mod->address_end = GINT_TO_POINTER (sec->addr+sec->size);
1387 mod->perms = g_strdup ("r--p");
1388 mod->address_offset = 0;
1389 mod->device = makedev (0, 0);
1390 mod->inode = (ino_t) i;
1391 mod->filename = g_strdup (name);
1393 if (g_slist_find_custom (ret, mod, find_procmodule) == NULL) {
1394 ret = g_slist_prepend (ret, mod);
1396 free_procmodule (mod);
1400 ret = g_slist_reverse (ret);
1404 #elif defined(__OpenBSD__)
1406 static int load_modules_callback (struct dl_phdr_info *info, size_t size, void *ptr)
1408 if (size < offsetof (struct dl_phdr_info, dlpi_phnum)
1409 + sizeof (info->dlpi_phnum))
1412 struct dl_phdr_info *cpy = calloc(1, sizeof(struct dl_phdr_info));
1416 memcpy(cpy, info, sizeof(*info));
1418 g_ptr_array_add ((GPtrArray *)ptr, cpy);
1423 static GSList *load_modules (void)
1426 WapiProcModule *mod;
1427 GPtrArray *dlarray = g_ptr_array_new();
1430 if (dl_iterate_phdr(load_modules_callback, dlarray) < 0)
1433 for (i = 0; i < dlarray->len; i++) {
1434 struct dl_phdr_info *info = g_ptr_array_index (dlarray, i);
1436 mod = g_new0 (WapiProcModule, 1);
1437 mod->address_start = (gpointer)(info->dlpi_addr + info->dlpi_phdr[0].p_vaddr);
1438 mod->address_end = (gpointer)(info->dlpi_addr +
1439 info->dlpi_phdr[info->dlpi_phnum - 1].p_vaddr);
1440 mod->perms = g_strdup ("r--p");
1441 mod->address_offset = 0;
1442 mod->inode = (ino_t) i;
1443 mod->filename = g_strdup (info->dlpi_name);
1445 DEBUG ("%s: inode=%d, filename=%s, address_start=%p, address_end=%p", __func__,
1446 mod->inode, mod->filename, mod->address_start, mod->address_end);
1450 if (g_slist_find_custom (ret, mod, find_procmodule) == NULL) {
1451 ret = g_slist_prepend (ret, mod);
1453 free_procmodule (mod);
1457 g_ptr_array_free (dlarray, TRUE);
1459 ret = g_slist_reverse (ret);
1463 #elif defined(__HAIKU__)
1465 static GSList *load_modules (void)
1468 WapiProcModule *mod;
1470 image_info imageInfo;
1472 while (get_next_image_info (B_CURRENT_TEAM, &cookie, &imageInfo) == B_OK) {
1473 mod = g_new0 (WapiProcModule, 1);
1474 mod->device = imageInfo.device;
1475 mod->inode = imageInfo.node;
1476 mod->filename = g_strdup (imageInfo.name);
1477 mod->address_start = MIN (imageInfo.text, imageInfo.data);
1478 mod->address_end = MAX ((uint8_t*)imageInfo.text + imageInfo.text_size,
1479 (uint8_t*)imageInfo.data + imageInfo.data_size);
1480 mod->perms = g_strdup ("r--p");
1481 mod->address_offset = 0;
1483 if (g_slist_find_custom (ret, mod, find_procmodule) == NULL) {
1484 ret = g_slist_prepend (ret, mod);
1486 free_procmodule (mod);
1490 ret = g_slist_reverse (ret);
1495 static GSList *load_modules (FILE *fp)
1498 WapiProcModule *mod;
1499 char buf[MAXPATHLEN + 1], *p, *endp;
1500 char *start_start, *end_start, *prot_start, *offset_start;
1501 char *maj_dev_start, *min_dev_start, *inode_start, prot_buf[5];
1502 gpointer address_start, address_end, address_offset;
1503 guint32 maj_dev, min_dev;
1507 while (fgets (buf, sizeof(buf), fp)) {
1509 while (g_ascii_isspace (*p)) ++p;
1511 if (!g_ascii_isxdigit (*start_start)) {
1514 address_start = (gpointer)strtoul (start_start, &endp, 16);
1522 if (!g_ascii_isxdigit (*end_start)) {
1525 address_end = (gpointer)strtoul (end_start, &endp, 16);
1527 if (!g_ascii_isspace (*p)) {
1531 while (g_ascii_isspace (*p)) ++p;
1533 if (*prot_start != 'r' && *prot_start != '-') {
1536 memcpy (prot_buf, prot_start, 4);
1538 while (!g_ascii_isspace (*p)) ++p;
1540 while (g_ascii_isspace (*p)) ++p;
1542 if (!g_ascii_isxdigit (*offset_start)) {
1545 address_offset = (gpointer)strtoul (offset_start, &endp, 16);
1547 if (!g_ascii_isspace (*p)) {
1551 while(g_ascii_isspace (*p)) ++p;
1553 if (!g_ascii_isxdigit (*maj_dev_start)) {
1556 maj_dev = strtoul (maj_dev_start, &endp, 16);
1564 if (!g_ascii_isxdigit (*min_dev_start)) {
1567 min_dev = strtoul (min_dev_start, &endp, 16);
1569 if (!g_ascii_isspace (*p)) {
1573 while (g_ascii_isspace (*p)) ++p;
1575 if (!g_ascii_isxdigit (*inode_start)) {
1578 inode = (ino_t)strtol (inode_start, &endp, 10);
1580 if (!g_ascii_isspace (*p)) {
1584 device = makedev ((int)maj_dev, (int)min_dev);
1585 if ((device == 0) &&
1590 while(g_ascii_isspace (*p)) ++p;
1591 /* p now points to the filename */
1593 mod = g_new0 (WapiProcModule, 1);
1594 mod->address_start = address_start;
1595 mod->address_end = address_end;
1596 mod->perms = g_strdup (prot_buf);
1597 mod->address_offset = address_offset;
1598 mod->device = device;
1600 mod->filename = g_strdup (g_strstrip (p));
1602 if (g_slist_find_custom (ret, mod, find_procmodule) == NULL) {
1603 ret = g_slist_prepend (ret, mod);
1605 free_procmodule (mod);
1609 ret = g_slist_reverse (ret);
1615 static gboolean match_procname_to_modulename (char *procname, char *modulename)
1617 char* lastsep = NULL;
1618 char* lastsep2 = NULL;
1621 gboolean result = FALSE;
1623 if (procname == NULL || modulename == NULL)
1626 pname = mono_path_resolve_symlinks (procname);
1627 mname = mono_path_resolve_symlinks (modulename);
1629 if (!strcmp (pname, mname))
1633 lastsep = strrchr (mname, '/');
1635 if (!strcmp (lastsep+1, pname))
1638 lastsep2 = strrchr (pname, '/');
1641 if (!strcmp (lastsep+1, lastsep2+1))
1644 if (!strcmp (mname, lastsep2+1))
1657 #if !defined(__OpenBSD__)
1659 open_process_map (int pid, const char *mode)
1662 const char *proc_path[] = {
1663 "/proc/%d/maps", /* GNU/Linux */
1664 "/proc/%d/map", /* FreeBSD */
1670 for (i = 0; fp == NULL && proc_path [i]; i++) {
1671 filename = g_strdup_printf (proc_path[i], pid);
1672 fp = fopen (filename, mode);
1680 gboolean EnumProcessModules (gpointer process, gpointer *modules,
1681 guint32 size, guint32 *needed)
1683 WapiHandle_process *process_handle;
1684 #if !defined(__OpenBSD__) && !defined(PLATFORM_MACOSX)
1687 GSList *mods = NULL;
1688 WapiProcModule *module;
1689 guint32 count, avail = size / sizeof(gpointer);
1692 char *proc_name = NULL;
1694 /* Store modules in an array of pointers (main module as
1695 * modules[0]), using the load address for each module as a
1696 * token. (Use 'NULL' as an alternative for the main module
1697 * so that the simple implementation can just return one item
1698 * for now.) Get the info from /proc/<pid>/maps on linux,
1699 * /proc/<pid>/map on FreeBSD, other systems will have to
1700 * implement /dev/kmem reading or whatever other horrid
1701 * technique is needed.
1703 if (size < sizeof(gpointer))
1706 if (WAPI_IS_PSEUDO_PROCESS_HANDLE (process)) {
1707 pid = WAPI_HANDLE_TO_PID (process);
1709 process_handle = lookup_process_handle (process);
1710 if (!process_handle) {
1711 DEBUG ("%s: Can't find process %p", __func__, process);
1715 pid = process_handle->id;
1716 proc_name = process_handle->proc_name;
1719 #if defined(PLATFORM_MACOSX) || defined(__OpenBSD__) || defined(__HAIKU__)
1720 mods = load_modules ();
1723 *needed = sizeof(gpointer);
1727 fp = open_process_map (pid, "r");
1729 /* No /proc/<pid>/maps so just return the main module
1733 *needed = sizeof(gpointer);
1736 mods = load_modules (fp);
1739 count = g_slist_length (mods);
1741 /* count + 1 to leave slot 0 for the main module */
1742 *needed = sizeof(gpointer) * (count + 1);
1745 * Use the NULL shortcut, as the first line in
1746 * /proc/<pid>/maps isn't the executable, and we need
1747 * that first in the returned list. Check the module name
1748 * to see if it ends with the proc name and substitute
1749 * the first entry with it. FIXME if this turns out to
1753 for (i = 0; i < (avail - 1) && i < count; i++) {
1754 module = (WapiProcModule *)g_slist_nth_data (mods, i);
1755 if (modules[0] != NULL)
1756 modules[i] = module->address_start;
1757 else if (match_procname_to_modulename (proc_name, module->filename))
1758 modules[0] = module->address_start;
1760 modules[i + 1] = module->address_start;
1763 for (i = 0; i < count; i++) {
1764 free_procmodule (g_slist_nth_data (mods, i));
1766 g_slist_free (mods);
1772 get_process_name_from_proc (pid_t pid)
1774 #if defined(__OpenBSD__)
1777 struct kinfo_proc *pi;
1778 #elif defined(PLATFORM_MACOSX)
1779 #if !(!defined (__mono_ppc__) && defined (TARGET_OSX))
1781 struct kinfo_proc *pi;
1782 int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, pid };
1786 char *filename = NULL;
1791 #if defined(PLATFORM_SOLARIS)
1792 filename = g_strdup_printf ("/proc/%d/psinfo", pid);
1793 if ((fp = fopen (filename, "r")) != NULL) {
1797 nread = fread (&info, sizeof (info), 1, fp);
1799 ret = g_strdup (info.pr_fname);
1805 #elif defined(PLATFORM_MACOSX)
1806 #if !defined (__mono_ppc__) && defined (TARGET_OSX)
1807 /* No proc name on OSX < 10.5 nor ppc nor iOS */
1808 memset (buf, '\0', sizeof(buf));
1809 proc_name (pid, buf, sizeof(buf));
1810 if (strlen (buf) > 0)
1811 ret = g_strdup (buf);
1813 if (sysctl(mib, 4, NULL, &size, NULL, 0) < 0)
1816 if ((pi = malloc(size)) == NULL)
1819 if (sysctl (mib, 4, pi, &size, NULL, 0) < 0) {
1820 if (errno == ENOMEM) {
1822 DEBUG ("%s: Didn't allocate enough memory for kproc info", __func__);
1827 if (strlen (pi->kp_proc.p_comm) > 0)
1828 ret = g_strdup (pi->kp_proc.p_comm);
1832 #elif defined(__OpenBSD__)
1834 mib [1] = KERN_PROC;
1835 mib [2] = KERN_PROC_PID;
1837 mib [4] = sizeof(struct kinfo_proc);
1841 if (sysctl(mib, 6, NULL, &size, NULL, 0) < 0)
1844 if ((pi = malloc(size)) == NULL)
1847 mib[5] = (int)(size / sizeof(struct kinfo_proc));
1849 if ((sysctl (mib, 6, pi, &size, NULL, 0) < 0) ||
1850 (size != sizeof (struct kinfo_proc))) {
1851 if (errno == ENOMEM) {
1858 if (strlen (pi->p_comm) > 0)
1859 ret = g_strdup (pi->p_comm);
1862 #elif defined(__HAIKU__)
1863 image_info imageInfo;
1866 if (get_next_image_info ((team_id)pid, &cookie, &imageInfo) == B_OK) {
1867 ret = g_strdup (imageInfo.name);
1870 memset (buf, '\0', sizeof(buf));
1871 filename = g_strdup_printf ("/proc/%d/exe", pid);
1872 if (readlink (filename, buf, 255) > 0) {
1873 ret = g_strdup (buf);
1881 filename = g_strdup_printf ("/proc/%d/cmdline", pid);
1882 if ((fp = fopen (filename, "r")) != NULL) {
1883 if (fgets (buf, 256, fp) != NULL) {
1884 ret = g_strdup (buf);
1895 filename = g_strdup_printf ("/proc/%d/stat", pid);
1896 if ((fp = fopen (filename, "r")) != NULL) {
1897 if (fgets (buf, 256, fp) != NULL) {
1900 start = strchr (buf, '(');
1901 if (start != NULL) {
1902 end = strchr (start + 1, ')');
1905 ret = g_strndup (start + 1,
1920 * wapi_process_get_path:
1922 * Return the full path of the executable of the process PID, or NULL if it cannot be determined.
1923 * Returns malloc-ed memory.
1926 wapi_process_get_path (pid_t pid)
1928 #if defined(PLATFORM_MACOSX) && !defined(__mono_ppc__) && defined(TARGET_OSX)
1929 char buf [PROC_PIDPATHINFO_MAXSIZE];
1932 res = proc_pidpath (pid, buf, sizeof (buf));
1935 if (buf [0] == '\0')
1937 return g_strdup (buf);
1939 return get_process_name_from_proc (pid);
1944 * wapi_process_set_cli_launcher:
1946 * Set the full path of the runtime executable used to launch managed exe's.
1949 wapi_process_set_cli_launcher (char *path)
1951 g_free (cli_launcher);
1952 cli_launcher = path ? g_strdup (path) : NULL;
1956 get_module_name (gpointer process, gpointer module,
1957 gunichar2 *basename, guint32 size,
1960 WapiHandle_process *process_handle;
1962 gunichar2 *procname;
1963 char *procname_ext = NULL;
1966 #if !defined(__OpenBSD__) && !defined(PLATFORM_MACOSX)
1969 GSList *mods = NULL;
1970 WapiProcModule *found_module;
1973 char *proc_name = NULL;
1975 DEBUG ("%s: Getting module base name, process handle %p module %p",
1976 __func__, process, module);
1978 size = size * sizeof (gunichar2); /* adjust for unicode characters */
1980 if (basename == NULL || size == 0)
1983 if (WAPI_IS_PSEUDO_PROCESS_HANDLE (process)) {
1984 /* This is a pseudo handle */
1985 pid = (pid_t)WAPI_HANDLE_TO_PID (process);
1986 proc_name = get_process_name_from_proc (pid);
1988 process_handle = lookup_process_handle (process);
1989 if (!process_handle) {
1990 DEBUG ("%s: Can't find process %p", __func__,
1995 pid = process_handle->id;
1996 proc_name = g_strdup (process_handle->proc_name);
1999 /* Look up the address in /proc/<pid>/maps */
2000 #if defined(PLATFORM_MACOSX) || defined(__OpenBSD__) || defined(__HAIKU__)
2001 mods = load_modules ();
2003 fp = open_process_map (pid, "r");
2005 if (errno == EACCES && module == NULL && base == TRUE) {
2006 procname_ext = get_process_name_from_proc (pid);
2008 /* No /proc/<pid>/maps, so just return failure
2015 mods = load_modules (fp);
2019 count = g_slist_length (mods);
2021 /* If module != NULL compare the address.
2022 * If module == NULL we are looking for the main module.
2023 * The best we can do for now check it the module name end with the process name.
2025 for (i = 0; i < count; i++) {
2026 found_module = (WapiProcModule *)g_slist_nth_data (mods, i);
2027 if (procname_ext == NULL &&
2028 ((module == NULL && match_procname_to_modulename (proc_name, found_module->filename)) ||
2029 (module != NULL && found_module->address_start == module))) {
2031 procname_ext = g_path_get_basename (found_module->filename);
2033 procname_ext = g_strdup (found_module->filename);
2036 free_procmodule (found_module);
2039 if (procname_ext == NULL) {
2040 /* If it's *still* null, we might have hit the
2041 * case where reading /proc/$pid/maps gives an
2042 * empty file for this user.
2044 procname_ext = get_process_name_from_proc (pid);
2047 g_slist_free (mods);
2051 DEBUG ("%s: Process name is [%s]", __func__,
2054 procname = mono_unicode_from_external (procname_ext, &bytes);
2055 if (procname == NULL) {
2057 g_free (procname_ext);
2063 /* Add the terminator */
2067 DEBUG ("%s: Size %d smaller than needed (%ld); truncating", __func__, size, bytes);
2069 memcpy (basename, procname, size);
2071 DEBUG ("%s: Size %d larger than needed (%ld)",
2072 __func__, size, bytes);
2074 memcpy (basename, procname, bytes);
2078 g_free (procname_ext);
2087 GetModuleBaseName (gpointer process, gpointer module,
2088 gunichar2 *basename, guint32 size)
2090 return get_module_name (process, module, basename, size, TRUE);
2094 GetModuleFileNameEx (gpointer process, gpointer module,
2095 gunichar2 *filename, guint32 size)
2097 return get_module_name (process, module, filename, size, FALSE);
2101 GetModuleInformation (gpointer process, gpointer module,
2102 WapiModuleInfo *modinfo, guint32 size)
2104 WapiHandle_process *process_handle;
2106 #if !defined(__OpenBSD__) && !defined(PLATFORM_MACOSX)
2109 GSList *mods = NULL;
2110 WapiProcModule *found_module;
2113 gboolean ret = FALSE;
2114 char *proc_name = NULL;
2116 DEBUG ("%s: Getting module info, process handle %p module %p",
2117 __func__, process, module);
2119 if (modinfo == NULL || size < sizeof (WapiModuleInfo))
2122 if (WAPI_IS_PSEUDO_PROCESS_HANDLE (process)) {
2123 pid = (pid_t)WAPI_HANDLE_TO_PID (process);
2124 proc_name = get_process_name_from_proc (pid);
2126 process_handle = lookup_process_handle (process);
2127 if (!process_handle) {
2128 DEBUG ("%s: Can't find process %p", __func__,
2133 pid = process_handle->id;
2134 proc_name = g_strdup (process_handle->proc_name);
2137 #if defined(PLATFORM_MACOSX) || defined(__OpenBSD__) || defined(__HAIKU__)
2138 mods = load_modules ();
2140 /* Look up the address in /proc/<pid>/maps */
2141 if ((fp = open_process_map (pid, "r")) == NULL) {
2142 /* No /proc/<pid>/maps, so just return failure
2148 mods = load_modules (fp);
2151 count = g_slist_length (mods);
2153 /* If module != NULL compare the address.
2154 * If module == NULL we are looking for the main module.
2155 * The best we can do for now check it the module name end with the process name.
2157 for (i = 0; i < count; i++) {
2158 found_module = (WapiProcModule *)g_slist_nth_data (mods, i);
2160 ((module == NULL && match_procname_to_modulename (proc_name, found_module->filename)) ||
2161 (module != NULL && found_module->address_start == module))) {
2162 modinfo->lpBaseOfDll = found_module->address_start;
2163 modinfo->SizeOfImage = (gsize)(found_module->address_end) - (gsize)(found_module->address_start);
2164 modinfo->EntryPoint = found_module->address_offset;
2168 free_procmodule (found_module);
2171 g_slist_free (mods);
2178 GetProcessWorkingSetSize (gpointer process, size_t *min, size_t *max)
2180 WapiHandle_process *process_handle;
2182 if (min == NULL || max == NULL)
2183 /* Not sure if w32 allows NULLs here or not */
2186 if (WAPI_IS_PSEUDO_PROCESS_HANDLE (process))
2187 /* This is a pseudo handle, so just fail for now */
2190 process_handle = lookup_process_handle (process);
2191 if (!process_handle) {
2192 DEBUG ("%s: Can't find process %p", __func__, process);
2197 *min = process_handle->min_working_set;
2198 *max = process_handle->max_working_set;
2204 SetProcessWorkingSetSize (gpointer process, size_t min, size_t max)
2206 WapiHandle_process *process_handle;
2208 if (WAPI_IS_PSEUDO_PROCESS_HANDLE (process))
2209 /* This is a pseudo handle, so just fail for now
2213 process_handle = lookup_process_handle (process);
2214 if (!process_handle) {
2215 DEBUG ("%s: Can't find process %p", __func__, process);
2220 process_handle->min_working_set = min;
2221 process_handle->max_working_set = max;
2228 TerminateProcess (gpointer process, gint32 exitCode)
2230 WapiHandle_process *process_handle;
2235 if (WAPI_IS_PSEUDO_PROCESS_HANDLE (process)) {
2236 /* This is a pseudo handle */
2237 pid = (pid_t)WAPI_HANDLE_TO_PID (process);
2239 process_handle = lookup_process_handle (process);
2240 if (!process_handle) {
2241 DEBUG ("%s: Can't find process %p", __func__, process);
2242 SetLastError (ERROR_INVALID_HANDLE);
2245 pid = process_handle->id;
2248 signo = (exitCode == -1) ? SIGKILL : SIGTERM;
2249 ret = kill (pid, signo);
2253 SetLastError (ERROR_INVALID_PARAMETER);
2256 SetLastError (ERROR_ACCESS_DENIED);
2259 SetLastError (ERROR_PROC_NOT_FOUND);
2262 SetLastError (ERROR_GEN_FAILURE);
2270 GetPriorityClass (gpointer process)
2272 #ifdef HAVE_GETPRIORITY
2273 WapiHandle_process *process_handle;
2277 if (WAPI_IS_PSEUDO_PROCESS_HANDLE (process)) {
2278 /* This is a pseudo handle */
2279 pid = (pid_t)WAPI_HANDLE_TO_PID (process);
2281 process_handle = lookup_process_handle (process);
2282 if (!process_handle) {
2283 SetLastError (ERROR_INVALID_HANDLE);
2286 pid = process_handle->id;
2290 ret = getpriority (PRIO_PROCESS, pid);
2291 if (ret == -1 && errno != 0) {
2295 SetLastError (ERROR_ACCESS_DENIED);
2298 SetLastError (ERROR_PROC_NOT_FOUND);
2301 SetLastError (ERROR_GEN_FAILURE);
2307 return NORMAL_PRIORITY_CLASS;
2309 return REALTIME_PRIORITY_CLASS;
2311 return HIGH_PRIORITY_CLASS;
2313 return ABOVE_NORMAL_PRIORITY_CLASS;
2315 return IDLE_PRIORITY_CLASS;
2317 return BELOW_NORMAL_PRIORITY_CLASS;
2319 return NORMAL_PRIORITY_CLASS;
2321 SetLastError (ERROR_NOT_SUPPORTED);
2327 SetPriorityClass (gpointer process, guint32 priority_class)
2329 #ifdef HAVE_SETPRIORITY
2330 WapiHandle_process *process_handle;
2335 if (WAPI_IS_PSEUDO_PROCESS_HANDLE (process)) {
2336 /* This is a pseudo handle */
2337 pid = (pid_t)WAPI_HANDLE_TO_PID (process);
2339 process_handle = lookup_process_handle (process);
2340 if (!process_handle) {
2341 SetLastError (ERROR_INVALID_HANDLE);
2344 pid = process_handle->id;
2347 switch (priority_class) {
2348 case IDLE_PRIORITY_CLASS:
2351 case BELOW_NORMAL_PRIORITY_CLASS:
2354 case NORMAL_PRIORITY_CLASS:
2357 case ABOVE_NORMAL_PRIORITY_CLASS:
2360 case HIGH_PRIORITY_CLASS:
2363 case REALTIME_PRIORITY_CLASS:
2367 SetLastError (ERROR_INVALID_PARAMETER);
2371 ret = setpriority (PRIO_PROCESS, pid, prio);
2376 SetLastError (ERROR_ACCESS_DENIED);
2379 SetLastError (ERROR_PROC_NOT_FOUND);
2382 SetLastError (ERROR_GEN_FAILURE);
2388 SetLastError (ERROR_NOT_SUPPORTED);
2394 mono_processes_cleanup (void)
2396 struct MonoProcess *mp;
2397 struct MonoProcess *prev = NULL;
2398 struct MonoProcess *candidate = NULL;
2399 gpointer unref_handle;
2402 DEBUG ("%s", __func__);
2404 /* Ensure we're not in here in multiple threads at once, nor recursive. */
2405 if (InterlockedCompareExchange (&mono_processes_cleaning_up, 1, 0) != 0)
2408 mp = mono_processes;
2409 while (mp != NULL) {
2410 if (mp->pid == 0 && mp->handle != NULL) {
2411 /* This process has exited and we need to remove the artifical ref
2413 mono_mutex_lock (&mono_processes_mutex);
2414 unref_handle = mp->handle;
2416 mono_mutex_unlock (&mono_processes_mutex);
2418 _wapi_handle_unref (unref_handle);
2425 * Remove processes which exited from the mono_processes list.
2426 * We need to synchronize with the sigchld handler here, which runs
2427 * asynchronously. The handler requires that the mono_processes list
2430 mp = mono_processes;
2432 while (mp != NULL) {
2433 if ((mp->handle_count == 0 && mp->pid == 0) || candidate != NULL) {
2435 _wapi_handle_spin (spin);
2439 /* We've found a candidate */
2440 mono_mutex_lock (&mono_processes_mutex);
2442 * This code can run parallel with the sigchld handler, but the
2443 * modifications it makes are safe.
2445 if (candidate == NULL) {
2447 if (mp == mono_processes) {
2448 mono_processes = mp->next;
2450 prev->next = mp->next;
2455 /* It's still safe to traverse the structure.*/
2456 mono_memory_barrier ();
2458 if (mono_processes_read_lock != 0) {
2459 /* The sigchld handler is watching us. Spin a bit and try again */
2462 } else if (spin >= 8) {
2463 /* Just give up for now */
2464 mono_mutex_unlock (&mono_processes_mutex);
2468 /* We've modified the list of processes, and we know the sigchld handler
2469 * isn't executing, so even if it executes at any moment, it'll see the
2470 * new version of the list. So now we can free the candidate. */
2471 DEBUG ("%s: freeing candidate %p", __func__, candidate);
2472 mp = candidate->next;
2473 MONO_SEM_DESTROY (&candidate->exit_sem);
2478 mono_mutex_unlock (&mono_processes_mutex);
2487 DEBUG ("%s done", __func__);
2489 InterlockedDecrement (&mono_processes_cleaning_up);
2493 process_close (gpointer handle, gpointer data)
2495 WapiHandle_process *process_handle;
2497 DEBUG ("%s", __func__);
2499 process_handle = (WapiHandle_process *) data;
2500 g_free (process_handle->proc_name);
2501 process_handle->proc_name = NULL;
2502 if (process_handle->mono_process)
2503 InterlockedDecrement (&process_handle->mono_process->handle_count);
2504 mono_processes_cleanup ();
2508 MONO_SIGNAL_HANDLER_FUNC (static, mono_sigchld_signal_handler, (int _dummy, siginfo_t *info, void *context))
2512 struct MonoProcess *p;
2514 DEBUG ("SIG CHILD handler for pid: %i\n", info->si_pid);
2516 InterlockedIncrement (&mono_processes_read_lock);
2520 pid = waitpid (-1, &status, WNOHANG);
2521 } while (pid == -1 && errno == EINTR);
2526 DEBUG ("child ended: %i", pid);
2529 if (p->pid == pid) {
2530 p->pid = 0; /* this pid doesn't exist anymore, clear it */
2532 MONO_SEM_POST (&p->exit_sem);
2539 InterlockedDecrement (&mono_processes_read_lock);
2541 DEBUG ("SIG CHILD handler: done looping.");
2547 process_add_sigchld_handler (void)
2550 struct sigaction sa;
2552 sa.sa_sigaction = mono_sigchld_signal_handler;
2553 sigemptyset (&sa.sa_mask);
2554 sa.sa_flags = SA_NOCLDSTOP | SA_SIGINFO;
2555 g_assert (sigaction (SIGCHLD, &sa, &previous_chld_sa) != -1);
2556 DEBUG ("Added SIGCHLD handler");
2561 process_wait (gpointer handle, guint32 timeout, gboolean alertable)
2563 WapiHandle_process *process_handle;
2568 struct MonoProcess *mp;
2570 /* FIXME: We can now easily wait on processes that aren't our own children,
2571 * but WaitFor*Object won't call us for pseudo handles. */
2572 g_assert ((GPOINTER_TO_UINT (handle) & _WAPI_PROCESS_UNHANDLED) != _WAPI_PROCESS_UNHANDLED);
2574 DEBUG ("%s (%p, %u)", __func__, handle, timeout);
2576 process_handle = lookup_process_handle (handle);
2577 if (!process_handle) {
2578 g_warning ("%s: error looking up process handle %p", __func__, handle);
2582 if (process_handle->exited) {
2583 /* We've already done this one */
2584 DEBUG ("%s (%p, %u): Process already exited", __func__, handle, timeout);
2585 return WAIT_OBJECT_0;
2588 pid = process_handle->id;
2590 DEBUG ("%s (%p, %u): PID: %d", __func__, handle, timeout, pid);
2592 /* We don't need to lock mono_processes here, the entry
2593 * has a handle_count > 0 which means it will not be freed. */
2594 mp = process_handle->mono_process;
2597 start = mono_msec_ticks ();
2601 if (timeout != INFINITE) {
2602 DEBUG ("%s (%p, %u): waiting on semaphore for %li ms...",
2603 __func__, handle, timeout, (timeout - (now - start)));
2605 ret = MONO_SEM_TIMEDWAIT_ALERTABLE (&mp->exit_sem, (timeout - (now - start)), alertable);
2607 DEBUG ("%s (%p, %u): waiting on semaphore forever...",
2608 __func__, handle, timeout);
2609 ret = MONO_SEM_WAIT_ALERTABLE (&mp->exit_sem, alertable);
2612 if (ret == -1 && errno != EINTR && errno != ETIMEDOUT) {
2613 DEBUG ("%s (%p, %u): sem_timedwait failure: %s",
2614 __func__, handle, timeout, g_strerror (errno));
2615 /* Should we return a failure here? */
2619 /* Success, process has exited */
2620 MONO_SEM_POST (&mp->exit_sem);
2625 DEBUG ("%s (%p, %u): WAIT_TIMEOUT (timeout = 0)", __func__, handle, timeout);
2626 return WAIT_TIMEOUT;
2629 now = mono_msec_ticks ();
2630 if (now - start >= timeout) {
2631 DEBUG ("%s (%p, %u): WAIT_TIMEOUT", __func__, handle, timeout);
2632 return WAIT_TIMEOUT;
2635 if (alertable && _wapi_thread_cur_apc_pending ()) {
2636 DEBUG ("%s (%p, %u): WAIT_IO_COMPLETION", __func__, handle, timeout);
2637 return WAIT_IO_COMPLETION;
2641 /* Process must have exited */
2642 DEBUG ("%s (%p, %u): Waited successfully", __func__, handle, timeout);
2644 ret = _wapi_handle_lock_shared_handles ();
2645 g_assert (ret == 0);
2647 status = mp ? mp->status : 0;
2648 if (WIFSIGNALED (status))
2649 process_handle->exitstatus = 128 + WTERMSIG (status);
2651 process_handle->exitstatus = WEXITSTATUS (status);
2652 _wapi_time_t_to_filetime (time (NULL), &process_handle->exit_time);
2654 process_handle->exited = TRUE;
2656 DEBUG ("%s (%p, %u): Setting pid %d signalled, exit status %d",
2657 __func__, handle, timeout, process_handle->id, process_handle->exitstatus);
2659 _wapi_handle_set_signal_state (handle, TRUE, TRUE);
2661 _wapi_handle_unlock_shared_handles ();
2663 return WAIT_OBJECT_0;
2667 wapi_processes_cleanup (void)
2669 g_free (cli_launcher);