2 * processes.c: Process handles
5 * Dick Porter (dick@ximian.com)
7 * (C) 2002-2006 Novell, Inc.
17 #include <sys/types.h>
22 #include <mono/io-layer/wapi.h>
23 #include <mono/io-layer/wapi-private.h>
24 #include <mono/io-layer/handles-private.h>
25 #include <mono/io-layer/misc-private.h>
26 #include <mono/io-layer/mono-mutex.h>
27 #include <mono/io-layer/process-private.h>
28 #include <mono/io-layer/threads.h>
29 #include <mono/utils/strenc.h>
30 #include <mono/io-layer/timefuncs-private.h>
32 /* The process' environment strings */
33 extern char **environ;
37 static guint32 process_wait (gpointer handle, guint32 timeout);
39 struct _WapiHandleOps _wapi_process_ops = {
40 NULL, /* close_shared */
44 process_wait, /* special_wait */
48 static mono_once_t process_current_once=MONO_ONCE_INIT;
49 static gpointer current_process=NULL;
51 static mono_once_t process_ops_once=MONO_ONCE_INIT;
53 static void process_ops_init (void)
55 _wapi_handle_register_capabilities (WAPI_HANDLE_PROCESS,
56 WAPI_HANDLE_CAP_WAIT |
57 WAPI_HANDLE_CAP_SPECIAL_WAIT);
60 static gboolean process_set_termination_details (gpointer handle, int status)
62 struct _WapiHandle_process *process_handle;
66 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_PROCESS,
67 (gpointer *)&process_handle);
69 g_warning ("%s: error looking up process handle %p",
74 thr_ret = _wapi_handle_lock_shared_handles ();
75 g_assert (thr_ret == 0);
77 if (WIFSIGNALED(status)) {
78 process_handle->exitstatus = 128 + WTERMSIG(status);
80 process_handle->exitstatus = WEXITSTATUS(status);
82 _wapi_time_t_to_filetime (time(NULL), &process_handle->exit_time);
84 _wapi_shared_handle_set_signal_state (handle, TRUE);
86 _wapi_handle_unlock_shared_handles ();
91 /* See if any child processes have terminated and wait() for them,
92 * updating process handle info. This function is called from the
93 * collection thread every few seconds.
95 static gboolean waitfor_pid (gpointer test, gpointer user_data G_GNUC_UNUSED)
97 struct _WapiHandle_process *process;
102 if (_wapi_handle_issignalled (test)) {
103 /* We've already done this one */
107 ok = _wapi_lookup_handle (test, WAPI_HANDLE_PROCESS,
108 (gpointer *)&process);
110 /* The handle must have been too old and was reaped */
115 ret = waitpid (process->id, &status, WNOHANG);
116 } while (errno == EINTR);
119 /* Process not ready for wait */
121 g_message ("%s: Process %d not ready for waiting for: %s",
122 __func__, process->id, g_strerror (errno));
129 g_message ("%s: Process %d finished", __func__, ret);
132 process_set_termination_details (test, status);
134 /* return FALSE to keep searching */
138 void _wapi_process_reap (void)
141 g_message ("%s: Reaping child processes", __func__);
144 _wapi_search_handle (WAPI_HANDLE_PROCESS, waitfor_pid, NULL, NULL,
148 /* Limitations: This can only wait for processes that are our own
149 * children. Fixing this means resurrecting a daemon helper to manage
152 static guint32 process_wait (gpointer handle, guint32 timeout)
154 struct _WapiHandle_process *process_handle;
160 g_message ("%s: Waiting for process %p", __func__, handle);
163 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_PROCESS,
164 (gpointer *)&process_handle);
166 g_warning ("%s: error looking up process handle %p", __func__,
171 pid = process_handle->id;
174 g_message ("%s: PID is %d", __func__, pid);
177 if (timeout == INFINITE) {
178 while ((ret = waitpid (pid, &status, 0)) != pid) {
179 if (ret == (pid_t)-1 && errno != EINTR) {
183 } else if (timeout == 0) {
185 ret = waitpid (pid, &status, WNOHANG);
187 return (WAIT_TIMEOUT);
192 ret = waitpid (pid, &status, WNOHANG);
195 } else if (ret == (pid_t)-1 && errno != EINTR) {
199 _wapi_handle_spin (100);
201 } while (timeout > 0);
204 return(WAIT_TIMEOUT);
208 /* Process must have exited */
210 g_message ("%s: Wait done", __func__);
213 ok = process_set_termination_details (handle, status);
215 SetLastError (ERROR_OUTOFMEMORY);
216 return (WAIT_FAILED);
219 return(WAIT_OBJECT_0);
222 void _wapi_process_signal_self ()
224 if (current_process != NULL) {
225 process_set_termination_details (current_process, 0);
230 static void process_set_defaults (struct _WapiHandle_process *process_handle)
232 /* These seem to be the defaults on w2k */
233 process_handle->min_working_set = 204800;
234 process_handle->max_working_set = 1413120;
236 _wapi_time_t_to_filetime (time (NULL), &process_handle->create_time);
239 /* Implemented as just a wrapper around CreateProcess () */
240 gboolean ShellExecuteEx (WapiShellExecuteInfo *sei)
243 WapiProcessInformation process_info;
245 gchar *u8file, *u8params, *u8args;
248 /* w2k just segfaults here, but we can do better than
251 SetLastError (ERROR_INVALID_PARAMETER);
255 if (sei->lpFile == NULL) {
256 /* w2k returns TRUE for this, for some reason. */
260 /* Put both executable and parameters into the second argument
261 * to CreateProcess (), so it searches $PATH. The conversion
262 * into and back out of utf8 is because there is no
263 * g_strdup_printf () equivalent for gunichar2 :-(
265 u8file = g_utf16_to_utf8 (sei->lpFile, -1, NULL, NULL, NULL);
266 if (u8file == NULL) {
267 SetLastError (ERROR_INVALID_DATA);
271 if (sei->lpParameters != NULL) {
272 u8params = g_utf16_to_utf8 (sei->lpParameters, -1, NULL, NULL, NULL);
273 if (u8params == NULL) {
274 SetLastError (ERROR_INVALID_DATA);
279 u8args = g_strdup_printf ("%s %s", u8file, u8params);
280 if (u8args == NULL) {
281 SetLastError (ERROR_INVALID_DATA);
287 args = g_utf8_to_utf16 (u8args, -1, NULL, NULL, NULL);
293 args = g_utf8_to_utf16 (u8file, -1, NULL, NULL, NULL);
297 SetLastError (ERROR_INVALID_DATA);
301 ret = CreateProcess (NULL, args, NULL, NULL, TRUE,
302 CREATE_UNICODE_ENVIRONMENT, NULL,
303 sei->lpDirectory, NULL, &process_info);
310 if (sei->fMask & SEE_MASK_NOCLOSEPROCESS) {
311 sei->hProcess = process_info.hProcess;
313 CloseHandle (process_info.hProcess);
319 gboolean CreateProcess (const gunichar2 *appname, const gunichar2 *cmdline,
320 WapiSecurityAttributes *process_attrs G_GNUC_UNUSED,
321 WapiSecurityAttributes *thread_attrs G_GNUC_UNUSED,
322 gboolean inherit_handles, guint32 create_flags,
323 gpointer new_environ, const gunichar2 *cwd,
324 WapiStartupInfo *startup,
325 WapiProcessInformation *process_info)
327 gchar *cmd=NULL, *prog = NULL, *full_prog = NULL, *args = NULL, *args_after_prog = NULL, *dir = NULL, **env_strings = NULL, **argv;
328 guint32 i, env_count = 0;
329 gboolean ret = FALSE;
331 struct _WapiHandle_process process_handle = {0}, *process_handle_data;
333 int in_fd, out_fd, err_fd;
337 mono_once (&process_ops_once, process_ops_init);
339 /* appname and cmdline specify the executable and its args:
341 * If appname is not NULL, it is the name of the executable.
342 * Otherwise the executable is the first token in cmdline.
344 * Executable searching:
346 * If appname is not NULL, it can specify the full path and
347 * file name, or else a partial name and the current directory
348 * will be used. There is no additional searching.
350 * If appname is NULL, the first whitespace-delimited token in
351 * cmdline is used. If the name does not contain a full
352 * directory path, the search sequence is:
354 * 1) The directory containing the current process
355 * 2) The current working directory
356 * 3) The windows system directory (Ignored)
357 * 4) The windows directory (Ignored)
360 * Just to make things more interesting, tokens can contain
361 * white space if they are surrounded by quotation marks. I'm
362 * beginning to understand just why windows apps are generally
363 * so crap, with an API like this :-(
365 if (appname != NULL) {
366 cmd = mono_unicode_to_external (appname);
369 g_message ("%s: unicode conversion returned NULL",
373 SetLastError (ERROR_PATH_NOT_FOUND);
377 /* Turn all the slashes round the right way */
378 for (i = 0; i < strlen (cmd); i++) {
379 if (cmd[i] == '\\') {
385 if (cmdline != NULL) {
386 args = mono_unicode_to_external (cmdline);
389 g_message ("%s: unicode conversion returned NULL", __func__);
392 SetLastError (ERROR_PATH_NOT_FOUND);
398 dir = mono_unicode_to_external (cwd);
401 g_message ("%s: unicode conversion returned NULL", __func__);
404 SetLastError (ERROR_PATH_NOT_FOUND);
408 /* Turn all the slashes round the right way */
409 for (i = 0; i < strlen (dir); i++) {
410 if (dir[i] == '\\') {
415 dir = g_get_current_dir ();
419 /* We can't put off locating the executable any longer :-( */
422 if (g_ascii_isalpha (cmd[0]) && (cmd[1] == ':')) {
423 /* Strip off the drive letter. I can't
424 * believe that CP/M holdover is still
427 g_memmove (cmd, cmd+2, strlen (cmd)-2);
428 cmd[strlen (cmd)-2] = '\0';
431 unquoted = g_shell_unquote (cmd, NULL);
432 if (unquoted[0] == '/') {
433 /* Assume full path given */
434 prog = g_strdup (unquoted);
436 /* Executable existing ? */
437 if (access (prog, X_OK) != 0) {
439 g_message ("%s: Couldn't find executable %s",
444 SetLastError (ERROR_FILE_NOT_FOUND);
448 /* Search for file named by cmd in the current
451 char *curdir = g_get_current_dir ();
453 prog = g_strdup_printf ("%s/%s", curdir, unquoted);
457 /* And make sure it's executable */
458 if (access (prog, X_OK) != 0) {
460 g_message ("%s: Couldn't find executable %s",
464 SetLastError (ERROR_FILE_NOT_FOUND);
469 args_after_prog = args;
474 /* Dig out the first token from args, taking quotation
478 /* First, strip off all leading whitespace */
479 args = g_strchug (args);
481 /* args_after_prog points to the contents of args
482 * after token has been set (otherwise argv[0] is
485 args_after_prog = args;
487 /* Assume the opening quote will always be the first
490 if (args[0] == '\"' || args [0] == '\'') {
492 for (i = 1; args[i] != '\0' && args[i] != quote; i++);
493 if (g_ascii_isspace (args[i+1])) {
494 /* We found the first token */
495 token = g_strndup (args+1, i-1);
496 args_after_prog = args + i;
498 /* Quotation mark appeared in the
499 * middle of the token. Just give the
500 * whole first token, quotes and all,
507 /* No quote mark, or malformed */
508 for (i = 0; args[i] != '\0'; i++) {
509 if (g_ascii_isspace (args[i])) {
510 token = g_strndup (args, i);
511 args_after_prog = args + i + 1;
517 if (token == NULL && args[0] != '\0') {
518 /* Must be just one token in the string */
519 token = g_strdup (args);
520 args_after_prog = NULL;
526 g_message ("%s: Couldn't find what to exec", __func__);
529 SetLastError (ERROR_PATH_NOT_FOUND);
533 /* Turn all the slashes round the right way. Only for
536 for (i = 0; i < strlen (token); i++) {
537 if (token[i] == '\\') {
542 if (g_ascii_isalpha (token[0]) && (token[1] == ':')) {
543 /* Strip off the drive letter. I can't
544 * believe that CP/M holdover is still
547 g_memmove (token, token+2, strlen (token)-2);
548 token[strlen (token)-2] = '\0';
551 if (token[0] == '/') {
552 /* Assume full path given */
553 prog = g_strdup (token);
555 /* Executable existing ? */
556 if (access (prog, X_OK) != 0) {
559 g_message ("%s: Couldn't find executable %s",
563 SetLastError (ERROR_FILE_NOT_FOUND);
568 char *curdir = g_get_current_dir ();
570 /* FIXME: Need to record the directory
571 * containing the current process, and check
572 * that for the new executable as the first
576 prog = g_strdup_printf ("%s/%s", curdir, token);
579 /* I assume X_OK is the criterion to use,
582 if (access (prog, X_OK) != 0) {
584 prog = g_find_program_in_path (token);
587 g_message ("%s: Couldn't find executable %s", __func__, token);
591 SetLastError (ERROR_FILE_NOT_FOUND);
601 g_message ("%s: Exec prog [%s] args [%s]", __func__, prog,
605 if (args_after_prog != NULL && *args_after_prog) {
608 qprog = g_shell_quote (prog);
609 full_prog = g_strconcat (qprog, " ", args_after_prog, NULL);
612 full_prog = g_shell_quote (prog);
615 ret = g_shell_parse_argv (full_prog, NULL, &argv, &gerr);
617 /* FIXME: Could do something with the GError here
621 if (startup != NULL && startup->dwFlags & STARTF_USESTDHANDLES) {
622 in_fd = GPOINTER_TO_UINT (startup->hStdInput);
623 out_fd = GPOINTER_TO_UINT (startup->hStdOutput);
624 err_fd = GPOINTER_TO_UINT (startup->hStdError);
626 in_fd = GPOINTER_TO_UINT (GetStdHandle (STD_INPUT_HANDLE));
627 out_fd = GPOINTER_TO_UINT (GetStdHandle (STD_OUTPUT_HANDLE));
628 err_fd = GPOINTER_TO_UINT (GetStdHandle (STD_ERROR_HANDLE));
631 g_strlcpy (process_handle.proc_name, prog,
632 _WAPI_PROC_NAME_MAX_LEN - 1);
634 process_set_defaults (&process_handle);
636 handle = _wapi_handle_new (WAPI_HANDLE_PROCESS, &process_handle);
637 if (handle == _WAPI_HANDLE_INVALID) {
638 g_warning ("%s: error creating process handle", __func__);
640 SetLastError (ERROR_PATH_NOT_FOUND);
644 /* Hold another reference so the process has somewhere to
645 * store its exit data even if we drop this handle
647 _wapi_handle_ref (handle);
649 /* new_environ is a block of NULL-terminated strings, which
650 * is itself NULL-terminated. Of course, passing an array of
651 * string pointers would have made things too easy :-(
653 * If new_environ is not NULL it specifies the entire set of
654 * environment variables in the new process. Otherwise the
655 * new process inherits the same environment.
657 if (new_environ != NULL) {
658 gunichar2 *new_environp;
660 /* Count the number of strings */
661 for (new_environp = (gunichar2 *)new_environ; *new_environp;
664 while (*new_environp) {
669 /* +2: one for the process handle value, and the last
672 env_strings = g_new0 (gchar *, env_count + 2);
674 /* Copy each environ string into 'strings' turning it
675 * into utf8 (or the requested encoding) at the same
679 for (new_environp = (gunichar2 *)new_environ; *new_environp;
681 env_strings[env_count] = mono_unicode_to_external (new_environp);
683 while (*new_environp) {
688 for (i = 0; environ[i] != NULL; i++) {
692 /* +2: one for the process handle value, and the last
695 env_strings = g_new0 (gchar *, env_count + 2);
697 /* Copy each environ string into 'strings' turning it
698 * into utf8 (or the requested encoding) at the same
702 for (i = 0; environ[i] != NULL; i++) {
703 env_strings[env_count] = g_strdup (environ[i]);
707 /* pass process handle info to the child, so it doesn't have
708 * to do an expensive search over the whole list
710 if (env_strings != NULL) {
711 struct _WapiHandleUnshared *handle_data;
712 struct _WapiHandle_shared_ref *ref;
714 handle_data = &_WAPI_PRIVATE_HANDLES(GPOINTER_TO_UINT(handle));
715 ref = &handle_data->u.shared;
717 env_strings[env_count] = g_strdup_printf ("_WAPI_PROCESS_HANDLE_OFFSET=%d", ref->offset);
720 thr_ret = _wapi_handle_lock_shared_handles ();
721 g_assert (thr_ret == 0);
726 SetLastError (ERROR_OUTOFMEMORY);
727 _wapi_handle_unref (handle);
729 } else if (pid == 0) {
732 /* Wait for the parent to finish setting up the
733 * handle. The semaphore lock is safe because the
734 * sem_undo structures of a semaphore aren't inherited
737 thr_ret = _wapi_handle_lock_shared_handles ();
738 g_assert (thr_ret == 0);
740 _wapi_handle_unlock_shared_handles ();
742 /* should we detach from the process group? */
744 /* Connect stdin, stdout and stderr */
749 if (inherit_handles != TRUE) {
750 /* FIXME: do something here */
753 /* Close all file descriptors */
754 for (i = getdtablesize () - 1; i > 2; i--) {
759 g_message ("%s: exec()ing [%s] in dir [%s]", __func__, cmd,
761 for (i = 0; argv[i] != NULL; i++) {
762 g_message ("arg %d: [%s]", i, argv[i]);
765 for (i = 0; env_strings[i] != NULL; i++) {
766 g_message ("env %d: [%s]", i, env_strings[i]);
771 if (chdir (dir) == -1) {
777 execve (argv[0], argv, env_strings);
784 ret = _wapi_lookup_handle (handle, WAPI_HANDLE_PROCESS,
785 (gpointer *)&process_handle_data);
787 g_warning ("%s: error looking up process handle %p", __func__,
789 _wapi_handle_unref (handle);
793 process_handle_data->id = pid;
795 if (process_info != NULL) {
796 process_info->hProcess = handle;
797 process_info->dwProcessId = pid;
799 /* FIXME: we might need to handle the thread info some
802 process_info->hThread = NULL;
803 process_info->dwThreadId = 0;
807 _wapi_handle_unlock_shared_handles ();
812 if (full_prog != NULL) {
821 if(env_strings != NULL) {
822 g_strfreev (env_strings);
828 static void process_set_name (struct _WapiHandle_process *process_handle)
830 gchar *progname, *utf8_progname, *slash;
832 progname=g_get_prgname ();
833 utf8_progname=mono_utf8_from_external (progname);
836 g_message ("%s: using [%s] as prog name", __func__, progname);
839 if(utf8_progname!=NULL) {
840 slash=strrchr (utf8_progname, '/');
842 g_strlcpy (process_handle->proc_name, slash+1,
843 _WAPI_PROC_NAME_MAX_LEN - 1);
845 g_strlcpy (process_handle->proc_name, utf8_progname,
846 _WAPI_PROC_NAME_MAX_LEN - 1);
849 g_free (utf8_progname);
853 extern void _wapi_time_t_to_filetime (time_t timeval, WapiFileTime *filetime);
855 #if !GLIB_CHECK_VERSION (2,4,0)
856 #define g_setenv(a,b,c) setenv(a,b,c)
857 #define g_unsetenv(a) unsetenv(a)
860 static void process_set_current (void)
862 pid_t pid = _wapi_getpid ();
863 const char *handle_env;
864 struct _WapiHandle_process process_handle = {0};
866 handle_env = g_getenv ("_WAPI_PROCESS_HANDLE_OFFSET");
867 g_unsetenv ("_WAPI_PROCESS_HANDLE_OFFSET");
869 if (handle_env != NULL) {
870 struct _WapiHandle_process *process_handlep;
871 guchar *procname = NULL;
874 current_process = _wapi_handle_new_from_offset (WAPI_HANDLE_PROCESS, atoi (handle_env), TRUE);
877 g_message ("%s: Found my process handle: %p (offset %d 0x%x)",
878 __func__, current_process, atoi (handle_env),
882 ok = _wapi_lookup_handle (current_process, WAPI_HANDLE_PROCESS,
883 (gpointer *)&process_handlep);
885 /* This test will probably break on linuxthreads, but
886 * that should be ancient history on all distros we
889 if (process_handlep->id == pid) {
890 procname = process_handlep->proc_name;
891 if (!strcmp (procname, "mono")) {
892 /* Set a better process name */
894 g_message ("%s: Setting better process name", __func__);
897 process_set_name (process_handlep);
900 g_message ("%s: Leaving process name: %s", __func__, procname);
907 /* Wrong pid, so drop this handle and fall through to
910 _wapi_handle_unref (current_process);
914 /* We get here if the handle wasn't specified in the
915 * environment, or if the process ID was wrong, or if the
916 * handle lookup failed (eg if the parent process forked and
917 * quit immediately, and deleted the shared data before the
918 * child got a chance to attach it.)
922 g_message ("%s: Need to create my own process handle", __func__);
925 process_handle.id = pid;
927 process_set_defaults (&process_handle);
928 process_set_name (&process_handle);
930 current_process = _wapi_handle_new (WAPI_HANDLE_PROCESS,
932 if (current_process == _WAPI_HANDLE_INVALID) {
933 g_warning ("%s: error creating process handle", __func__);
937 /* Make sure the new handle has a reference so it wont go away
938 * until this process exits
940 _wapi_handle_ref (current_process);
943 /* Returns a pseudo handle that doesn't need to be closed afterwards */
944 gpointer GetCurrentProcess (void)
946 mono_once (&process_current_once, process_set_current);
948 return((gpointer)-1);
951 guint32 GetProcessId (gpointer handle)
953 struct _WapiHandle_process *process_handle;
956 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_PROCESS,
957 (gpointer *)&process_handle);
959 SetLastError (ERROR_INVALID_HANDLE);
963 return (process_handle->id);
966 guint32 GetCurrentProcessId (void)
968 mono_once (&process_current_once, process_set_current);
970 return (GetProcessId (current_process));
973 /* Returns the process id as a convenience to the functions that call this */
974 static pid_t signal_process_if_gone (gpointer handle)
976 struct _WapiHandle_process *process_handle;
979 /* Make sure the process is signalled if it has exited - if
980 * the parent process didn't wait for it then it won't be
982 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_PROCESS,
983 (gpointer *)&process_handle);
985 /* It's possible that the handle has vanished during
986 * the _wapi_search_handle before it gets here, so
987 * don't spam the console with warnings.
989 /* g_warning ("%s: error looking up process handle %p",
996 g_message ("%s: looking at process %d", __func__, process_handle->id);
999 if (kill (process_handle->id, 0) == -1 &&
1002 /* The process is dead, (EPERM tells us a new process
1003 * has that ID, but as it's owned by someone else it
1004 * can't be the one listed in our shared memory file)
1006 _wapi_shared_handle_set_signal_state (handle, TRUE);
1009 return (process_handle->id);
1012 static gboolean process_enum (gpointer handle, gpointer user_data)
1014 GArray *processes=user_data;
1015 pid_t pid = signal_process_if_gone (handle);
1022 /* Ignore processes that have already exited (ie they are signalled) */
1023 if (_wapi_handle_issignalled (handle) == FALSE) {
1025 g_message ("%s: process %d added to array", __func__, pid);
1028 /* This ensures that duplicates aren't returned (see
1029 * the comment above _wapi_search_handle () for why
1032 for (i = 0; i < processes->len; i++) {
1033 if (g_array_index (processes, pid_t, i) == pid) {
1034 /* We've already got this one, return
1035 * FALSE to keep searching
1041 g_array_append_val (processes, pid);
1044 /* Return false to keep searching */
1048 gboolean EnumProcesses (guint32 *pids, guint32 len, guint32 *needed)
1050 GArray *processes = g_array_new (FALSE, FALSE, sizeof(pid_t));
1053 mono_once (&process_current_once, process_set_current);
1055 _wapi_search_handle (WAPI_HANDLE_PROCESS, process_enum, processes,
1058 fit=len/sizeof(guint32);
1059 for (i = 0, j = 0; j < fit && i < processes->len; i++) {
1060 pids[j++] = g_array_index (processes, pid_t, i);
1063 g_array_free (processes, TRUE);
1065 *needed = j * sizeof(guint32);
1070 static gboolean process_open_compare (gpointer handle, gpointer user_data)
1073 pid_t checking_pid = signal_process_if_gone (handle);
1075 if (checking_pid == 0) {
1079 wanted_pid = GPOINTER_TO_UINT (user_data);
1081 /* It's possible to have more than one process handle with the
1082 * same pid, but only the one running process can be
1085 if (checking_pid == wanted_pid &&
1086 _wapi_handle_issignalled (handle) == FALSE) {
1087 /* If the handle is blown away in the window between
1088 * returning TRUE here and _wapi_search_handle pinging
1089 * the timestamp, the search will continue
1097 gpointer OpenProcess (guint32 access G_GNUC_UNUSED, gboolean inherit G_GNUC_UNUSED, guint32 pid)
1099 /* Find the process handle that corresponds to pid */
1102 mono_once (&process_current_once, process_set_current);
1105 g_message ("%s: looking for process %d", __func__, pid);
1108 handle = _wapi_search_handle (WAPI_HANDLE_PROCESS,
1109 process_open_compare,
1110 GUINT_TO_POINTER (pid), NULL, TRUE);
1113 g_message ("%s: Can't find pid %d", __func__, pid);
1116 SetLastError (ERROR_PROC_NOT_FOUND);
1121 _wapi_handle_ref (handle);
1126 gboolean GetExitCodeProcess (gpointer process, guint32 *code)
1128 struct _WapiHandle_process *process_handle;
1131 mono_once (&process_current_once, process_set_current);
1137 ok=_wapi_lookup_handle (process, WAPI_HANDLE_PROCESS,
1138 (gpointer *)&process_handle);
1141 g_message ("%s: Can't find process %p", __func__, process);
1147 /* A process handle is only signalled if the process has exited
1148 * and has been waited for */
1149 if (_wapi_handle_issignalled (process) == TRUE ||
1150 process_wait (process, 0) == WAIT_OBJECT_0) {
1151 *code = process_handle->exitstatus;
1153 *code = STILL_ACTIVE;
1159 gboolean GetProcessTimes (gpointer process, WapiFileTime *create_time,
1160 WapiFileTime *exit_time, WapiFileTime *kernel_time,
1161 WapiFileTime *user_time)
1163 struct _WapiHandle_process *process_handle;
1166 mono_once (&process_current_once, process_set_current);
1168 if(create_time==NULL || exit_time==NULL || kernel_time==NULL ||
1170 /* Not sure if w32 allows NULLs here or not */
1174 ok=_wapi_lookup_handle (process, WAPI_HANDLE_PROCESS,
1175 (gpointer *)&process_handle);
1178 g_message ("%s: Can't find process %p", __func__, process);
1184 *create_time=process_handle->create_time;
1186 /* A process handle is only signalled if the process has
1187 * exited. Otherwise exit_time isn't set
1189 if(_wapi_handle_issignalled (process)==TRUE) {
1190 *exit_time=process_handle->exit_time;
1196 gboolean EnumProcessModules (gpointer process, gpointer *modules,
1197 guint32 size, guint32 *needed)
1199 /* Store modules in an array of pointers (main module as
1200 * modules[0]), using the load address for each module as a
1201 * token. (Use 'NULL' as an alternative for the main module
1202 * so that the simple implementation can just return one item
1203 * for now.) Get the info from /proc/<pid>/maps on linux,
1204 * other systems will have to implement /dev/kmem reading or
1205 * whatever other horrid technique is needed.
1207 if(size<sizeof(gpointer)) {
1213 *needed=sizeof(gpointer);
1216 *needed=sizeof(gpointer);
1222 guint32 GetModuleBaseName (gpointer process, gpointer module,
1223 gunichar2 *basename, guint32 size)
1225 struct _WapiHandle_process *process_handle;
1228 mono_once (&process_current_once, process_set_current);
1231 g_message ("%s: Getting module base name, process handle %p module %p",
1232 __func__, process, module);
1235 if(basename==NULL || size==0) {
1239 ok=_wapi_lookup_handle (process, WAPI_HANDLE_PROCESS,
1240 (gpointer *)&process_handle);
1243 g_message ("%s: Can't find process %p", __func__, process);
1250 /* Shorthand for the main module, which has the
1251 * process name recorded in the handle data
1254 gunichar2 *procname;
1255 guchar *procname_utf8 = NULL;
1259 g_message ("%s: Returning main module name", __func__);
1262 pid=process_handle->id;
1263 procname_utf8 = process_handle->proc_name;
1266 g_message ("%s: Process name is [%s]", __func__,
1270 procname = g_utf8_to_utf16 (procname_utf8, -1, NULL, &len,
1272 if (procname == NULL) {
1277 /* Add the terminator, and convert chars to bytes */
1278 bytes = (len + 1) * 2;
1282 g_message ("%s: Size %d smaller than needed (%ld); truncating", __func__, size, bytes);
1285 memcpy (basename, procname, size);
1288 g_message ("%s: Size %d larger than needed (%ld)",
1289 __func__, size, bytes);
1292 memcpy (basename, procname, bytes);
1299 /* Look up the address in /proc/<pid>/maps */
1305 gboolean GetProcessWorkingSetSize (gpointer process, size_t *min, size_t *max)
1307 struct _WapiHandle_process *process_handle;
1310 mono_once (&process_current_once, process_set_current);
1312 if(min==NULL || max==NULL) {
1313 /* Not sure if w32 allows NULLs here or not */
1317 ok=_wapi_lookup_handle (process, WAPI_HANDLE_PROCESS,
1318 (gpointer *)&process_handle);
1321 g_message ("%s: Can't find process %p", __func__, process);
1327 *min=process_handle->min_working_set;
1328 *max=process_handle->max_working_set;
1333 gboolean SetProcessWorkingSetSize (gpointer process, size_t min, size_t max)
1335 struct _WapiHandle_process *process_handle;
1338 mono_once (&process_current_once, process_set_current);
1340 ok=_wapi_lookup_handle (process, WAPI_HANDLE_PROCESS,
1341 (gpointer *)&process_handle);
1344 g_message ("%s: Can't find process %p", __func__, process);
1350 process_handle->min_working_set=min;
1351 process_handle->max_working_set=max;
1358 TerminateProcess (gpointer process, gint32 exitCode)
1360 struct _WapiHandle_process *process_handle;
1365 ok = _wapi_lookup_handle (process, WAPI_HANDLE_PROCESS,
1366 (gpointer *) &process_handle);
1370 g_message ("%s: Can't find process %p", __func__, process);
1372 SetLastError (ERROR_INVALID_HANDLE);
1376 signo = (exitCode == -1) ? SIGKILL : SIGTERM;
1377 ret = kill (process_handle->id, signo);
1381 SetLastError (ERROR_INVALID_PARAMETER);
1384 SetLastError (ERROR_ACCESS_DENIED);
1387 SetLastError (ERROR_PROC_NOT_FOUND);
1390 SetLastError (ERROR_GEN_FAILURE);