2 * processes.c: Process handles
5 * Dick Porter (dick@ximian.com)
7 * (C) 2002 Ximian, 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);
147 /* Limitations: This can only wait for processes that are our own
148 * children. Fixing this means resurrecting a daemon helper to manage
151 static guint32 process_wait (gpointer handle, guint32 timeout)
153 struct _WapiHandle_process *process_handle;
159 g_message ("%s: Waiting for process %p", __func__, handle);
162 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_PROCESS,
163 (gpointer *)&process_handle);
165 g_warning ("%s: error looking up process handle %p", __func__,
170 pid = process_handle->id;
173 g_message ("%s: PID is %d", __func__, pid);
176 if (timeout == INFINITE) {
177 while ((ret = waitpid (pid, &status, 0)) != pid) {
178 if (ret == (pid_t)-1 && errno != EINTR) {
182 } else if (timeout == 0) {
184 ret = waitpid (pid, &status, WNOHANG);
186 return (WAIT_TIMEOUT);
191 ret = waitpid (pid, &status, WNOHANG);
194 } else if (ret == (pid_t)-1 && errno != EINTR) {
198 _wapi_handle_spin (100);
200 } while (timeout > 0);
203 return(WAIT_TIMEOUT);
207 /* Process must have exited */
209 g_message ("%s: Wait done", __func__);
212 ok = process_set_termination_details (handle, status);
214 SetLastError (ERROR_OUTOFMEMORY);
215 return (WAIT_FAILED);
218 return(WAIT_OBJECT_0);
221 static void process_set_defaults (struct _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);
230 /* Implemented as just a wrapper around CreateProcess () */
231 gboolean ShellExecuteEx (WapiShellExecuteInfo *sei)
234 WapiProcessInformation process_info;
236 gchar *u8file, *u8params, *u8args;
239 /* w2k just segfaults here, but we can do better than
242 SetLastError (ERROR_INVALID_PARAMETER);
246 if (sei->lpFile == NULL) {
247 /* w2k returns TRUE for this, for some reason. */
251 /* Put both executable and parameters into the second argument
252 * to CreateProcess (), so it searches $PATH. The conversion
253 * into and back out of utf8 is because there is no
254 * g_strdup_printf () equivalent for gunichar2 :-(
256 u8file = g_utf16_to_utf8 (sei->lpFile, -1, NULL, NULL, NULL);
257 if (u8file == NULL) {
258 SetLastError (ERROR_INVALID_DATA);
262 if (sei->lpParameters != NULL) {
263 u8params = g_utf16_to_utf8 (sei->lpParameters, -1, NULL, NULL, NULL);
264 if (u8params == NULL) {
265 SetLastError (ERROR_INVALID_DATA);
270 u8args = g_strdup_printf ("%s %s", u8file, u8params);
271 if (u8args == NULL) {
272 SetLastError (ERROR_INVALID_DATA);
278 args = g_utf8_to_utf16 (u8args, -1, NULL, NULL, NULL);
284 args = g_utf8_to_utf16 (u8file, -1, NULL, NULL, NULL);
288 SetLastError (ERROR_INVALID_DATA);
292 ret = CreateProcess (NULL, args, NULL, NULL, TRUE,
293 CREATE_UNICODE_ENVIRONMENT, NULL,
294 sei->lpDirectory, NULL, &process_info);
301 if (sei->fMask & SEE_MASK_NOCLOSEPROCESS) {
302 sei->hProcess = process_info.hProcess;
304 CloseHandle (process_info.hProcess);
310 gboolean CreateProcess (const gunichar2 *appname, const gunichar2 *cmdline,
311 WapiSecurityAttributes *process_attrs G_GNUC_UNUSED,
312 WapiSecurityAttributes *thread_attrs G_GNUC_UNUSED,
313 gboolean inherit_handles, guint32 create_flags,
314 gpointer new_environ, const gunichar2 *cwd,
315 WapiStartupInfo *startup,
316 WapiProcessInformation *process_info)
318 gchar *cmd=NULL, *prog = NULL, *full_prog = NULL, *args = NULL, *args_after_prog = NULL, *dir = NULL, **env_strings = NULL, **argv;
319 guint32 i, env_count = 0;
320 gboolean ret = FALSE;
322 struct _WapiHandle_process process_handle = {0}, *process_handle_data;
324 int in_fd, out_fd, err_fd;
328 mono_once (&process_ops_once, process_ops_init);
330 /* appname and cmdline specify the executable and its args:
332 * If appname is not NULL, it is the name of the executable.
333 * Otherwise the executable is the first token in cmdline.
335 * Executable searching:
337 * If appname is not NULL, it can specify the full path and
338 * file name, or else a partial name and the current directory
339 * will be used. There is no additional searching.
341 * If appname is NULL, the first whitespace-delimited token in
342 * cmdline is used. If the name does not contain a full
343 * directory path, the search sequence is:
345 * 1) The directory containing the current process
346 * 2) The current working directory
347 * 3) The windows system directory (Ignored)
348 * 4) The windows directory (Ignored)
351 * Just to make things more interesting, tokens can contain
352 * white space if they are surrounded by quotation marks. I'm
353 * beginning to understand just why windows apps are generally
354 * so crap, with an API like this :-(
356 if (appname != NULL) {
357 cmd = mono_unicode_to_external (appname);
360 g_message ("%s: unicode conversion returned NULL",
364 SetLastError (ERROR_PATH_NOT_FOUND);
368 /* Turn all the slashes round the right way */
369 for (i = 0; i < strlen (cmd); i++) {
370 if (cmd[i] == '\\') {
376 if (cmdline != NULL) {
377 args = mono_unicode_to_external (cmdline);
380 g_message ("%s: unicode conversion returned NULL", __func__);
383 SetLastError (ERROR_PATH_NOT_FOUND);
389 dir = mono_unicode_to_external (cwd);
392 g_message ("%s: unicode conversion returned NULL", __func__);
395 SetLastError (ERROR_PATH_NOT_FOUND);
399 /* Turn all the slashes round the right way */
400 for (i = 0; i < strlen (dir); i++) {
401 if (dir[i] == '\\') {
406 dir = g_get_current_dir ();
410 /* We can't put off locating the executable any longer :-( */
413 if (g_ascii_isalpha (cmd[0]) && (cmd[1] == ':')) {
414 /* Strip off the drive letter. I can't
415 * believe that CP/M holdover is still
418 g_memmove (cmd, cmd+2, strlen (cmd)-2);
419 cmd[strlen (cmd)-2] = '\0';
422 unquoted = g_shell_unquote (cmd, NULL);
423 if (unquoted[0] == '/') {
424 /* Assume full path given */
425 prog = g_strdup (unquoted);
427 /* Executable existing ? */
428 if (access (prog, X_OK) != 0) {
430 g_message ("%s: Couldn't find executable %s",
435 SetLastError (ERROR_FILE_NOT_FOUND);
439 /* Search for file named by cmd in the current
442 char *curdir = g_get_current_dir ();
444 prog = g_strdup_printf ("%s/%s", curdir, unquoted);
448 /* And make sure it's executable */
449 if (access (prog, X_OK) != 0) {
451 g_message ("%s: Couldn't find executable %s",
455 SetLastError (ERROR_FILE_NOT_FOUND);
460 args_after_prog = args;
465 /* Dig out the first token from args, taking quotation
469 /* First, strip off all leading whitespace */
470 args = g_strchug (args);
472 /* args_after_prog points to the contents of args
473 * after token has been set (otherwise argv[0] is
476 args_after_prog = args;
478 /* Assume the opening quote will always be the first
481 if (args[0] == '\"' || args [0] == '\'') {
483 for (i = 1; args[i] != '\0' && args[i] != quote; i++);
484 if (g_ascii_isspace (args[i+1])) {
485 /* We found the first token */
486 token = g_strndup (args+1, i-1);
487 args_after_prog = args + i;
489 /* Quotation mark appeared in the
490 * middle of the token. Just give the
491 * whole first token, quotes and all,
498 /* No quote mark, or malformed */
499 for (i = 0; args[i] != '\0'; i++) {
500 if (g_ascii_isspace (args[i])) {
501 token = g_strndup (args, i);
502 args_after_prog = args + i + 1;
508 if (token == NULL && args[0] != '\0') {
509 /* Must be just one token in the string */
510 token = g_strdup (args);
511 args_after_prog = NULL;
517 g_message ("%s: Couldn't find what to exec", __func__);
520 SetLastError (ERROR_PATH_NOT_FOUND);
524 /* Turn all the slashes round the right way. Only for
527 for (i = 0; i < strlen (token); i++) {
528 if (token[i] == '\\') {
533 if (g_ascii_isalpha (token[0]) && (token[1] == ':')) {
534 /* Strip off the drive letter. I can't
535 * believe that CP/M holdover is still
538 g_memmove (token, token+2, strlen (token)-2);
539 token[strlen (token)-2] = '\0';
542 if (token[0] == '/') {
543 /* Assume full path given */
544 prog = g_strdup (token);
546 /* Executable existing ? */
547 if (access (prog, X_OK) != 0) {
550 g_message ("%s: Couldn't find executable %s",
554 SetLastError (ERROR_FILE_NOT_FOUND);
559 char *curdir = g_get_current_dir ();
561 /* FIXME: Need to record the directory
562 * containing the current process, and check
563 * that for the new executable as the first
567 prog = g_strdup_printf ("%s/%s", curdir, token);
570 /* I assume X_OK is the criterion to use,
573 if (access (prog, X_OK) != 0) {
575 prog = g_find_program_in_path (token);
578 g_message ("%s: Couldn't find executable %s", __func__, token);
582 SetLastError (ERROR_FILE_NOT_FOUND);
592 g_message ("%s: Exec prog [%s] args [%s]", __func__, prog,
596 if (args_after_prog != NULL && *args_after_prog) {
599 qprog = g_shell_quote (prog);
600 full_prog = g_strconcat (qprog, " ", args_after_prog, NULL);
603 full_prog = g_shell_quote (prog);
606 ret = g_shell_parse_argv (full_prog, NULL, &argv, &gerr);
608 /* FIXME: Could do something with the GError here
612 if (startup != NULL && startup->dwFlags & STARTF_USESTDHANDLES) {
613 in_fd = GPOINTER_TO_UINT (startup->hStdInput);
614 out_fd = GPOINTER_TO_UINT (startup->hStdOutput);
615 err_fd = GPOINTER_TO_UINT (startup->hStdError);
617 in_fd = GPOINTER_TO_UINT (GetStdHandle (STD_INPUT_HANDLE));
618 out_fd = GPOINTER_TO_UINT (GetStdHandle (STD_OUTPUT_HANDLE));
619 err_fd = GPOINTER_TO_UINT (GetStdHandle (STD_ERROR_HANDLE));
622 g_strlcpy (process_handle.proc_name, prog,
623 _WAPI_PROC_NAME_MAX_LEN - 1);
625 process_set_defaults (&process_handle);
627 handle = _wapi_handle_new (WAPI_HANDLE_PROCESS, &process_handle);
628 if (handle == _WAPI_HANDLE_INVALID) {
629 g_warning ("%s: error creating process handle", __func__);
631 SetLastError (ERROR_PATH_NOT_FOUND);
635 /* new_environ is a block of NULL-terminated strings, which
636 * is itself NULL-terminated. Of course, passing an array of
637 * string pointers would have made things too easy :-(
639 * If new_environ is not NULL it specifies the entire set of
640 * environment variables in the new process. Otherwise the
641 * new process inherits the same environment.
643 if (new_environ != NULL) {
644 gunichar2 *new_environp;
646 /* Count the number of strings */
647 for (new_environp = (gunichar2 *)new_environ; *new_environp;
650 while (*new_environp) {
655 /* +2: one for the process handle value, and the last
658 env_strings = g_new0 (gchar *, env_count + 2);
660 /* Copy each environ string into 'strings' turning it
661 * into utf8 (or the requested encoding) at the same
665 for (new_environp = (gunichar2 *)new_environ; *new_environp;
667 env_strings[env_count] = mono_unicode_to_external (new_environp);
669 while (*new_environp) {
674 for (i = 0; environ[i] != NULL; i++) {
678 /* +2: one for the process handle value, and the last
681 env_strings = g_new0 (gchar *, env_count + 2);
683 /* Copy each environ string into 'strings' turning it
684 * into utf8 (or the requested encoding) at the same
688 for (i = 0; environ[i] != NULL; i++) {
689 env_strings[env_count] = g_strdup (environ[i]);
693 /* pass process handle info to the child, so it doesn't have
694 * to do an expensive search over the whole list
696 if (env_strings != NULL) {
697 struct _WapiHandleUnshared *handle_data;
698 struct _WapiHandle_shared_ref *ref;
700 handle_data = &_WAPI_PRIVATE_HANDLES(GPOINTER_TO_UINT(handle));
701 ref = &handle_data->u.shared;
703 env_strings[env_count] = g_strdup_printf ("_WAPI_PROCESS_HANDLE_OFFSET=%d", ref->offset);
706 thr_ret = _wapi_handle_lock_shared_handles ();
707 g_assert (thr_ret == 0);
712 SetLastError (ERROR_OUTOFMEMORY);
713 _wapi_handle_unref (handle);
715 } else if (pid == 0) {
718 /* Wait for the parent to finish setting up the
719 * handle. The semaphore lock is safe because the
720 * sem_undo structures of a semaphore aren't inherited
723 thr_ret = _wapi_handle_lock_shared_handles ();
724 g_assert (thr_ret == 0);
726 _wapi_handle_unlock_shared_handles ();
728 /* should we detach from the process group? */
730 /* Connect stdin, stdout and stderr */
735 if (inherit_handles != TRUE) {
736 /* FIXME: do something here */
739 /* Close all file descriptors */
740 for (i = getdtablesize () - 1; i > 2; i--) {
745 g_message ("%s: exec()ing [%s] in dir [%s]", __func__, cmd,
747 for (i = 0; argv[i] != NULL; i++) {
748 g_message ("arg %d: [%s]", i, argv[i]);
751 for (i = 0; env_strings[i] != NULL; i++) {
752 g_message ("env %d: [%s]", i, env_strings[i]);
757 if (chdir (dir) == -1) {
763 execve (argv[0], argv, env_strings);
770 ret = _wapi_lookup_handle (handle, WAPI_HANDLE_PROCESS,
771 (gpointer *)&process_handle_data);
773 g_warning ("%s: error looking up process handle %p", __func__,
775 _wapi_handle_unref (handle);
779 process_handle_data->id = pid;
781 if (process_info != NULL) {
782 process_info->hProcess = handle;
783 process_info->dwProcessId = pid;
785 /* FIXME: we might need to handle the thread info some
788 process_info->hThread = NULL;
789 process_info->dwThreadId = 0;
793 _wapi_handle_unlock_shared_handles ();
798 if (full_prog != NULL) {
807 if(env_strings != NULL) {
808 g_strfreev (env_strings);
814 static void process_set_name (struct _WapiHandle_process *process_handle)
816 gchar *progname, *utf8_progname, *slash;
818 progname=g_get_prgname ();
819 utf8_progname=mono_utf8_from_external (progname);
822 g_message ("%s: using [%s] as prog name", __func__, progname);
825 if(utf8_progname!=NULL) {
826 slash=strrchr (utf8_progname, '/');
828 g_strlcpy (process_handle->proc_name, slash+1,
829 _WAPI_PROC_NAME_MAX_LEN - 1);
831 g_strlcpy (process_handle->proc_name, utf8_progname,
832 _WAPI_PROC_NAME_MAX_LEN - 1);
835 g_free (utf8_progname);
839 extern void _wapi_time_t_to_filetime (time_t timeval, WapiFileTime *filetime);
841 static void process_set_current (void)
843 pid_t pid = _wapi_getpid ();
845 struct _WapiHandle_process process_handle = {0};
847 handle_env = getenv ("_WAPI_PROCESS_HANDLE_OFFSET");
848 if (handle_env != NULL) {
849 struct _WapiHandle_process *process_handlep;
850 guchar *procname = NULL;
853 current_process = _wapi_handle_new_from_offset (WAPI_HANDLE_PROCESS, atoi (handle_env), TRUE);
856 g_message ("%s: Found my process handle: %p (offset %d)",
857 __func__, current_process, atoi (handle_env));
860 ok = _wapi_lookup_handle (current_process, WAPI_HANDLE_PROCESS,
861 (gpointer *)&process_handlep);
863 g_warning ("%s: error looking up process handle %p",
864 __func__, current_process);
868 /* This test will probably break on linuxthreads, but
869 * that should be ancient history on all distros we
872 if (process_handlep->id == pid) {
873 procname = process_handlep->proc_name;
874 if (!strcmp (procname, "mono")) {
875 /* Set a better process name */
877 g_message ("%s: Setting better process name",
881 process_set_name (process_handlep);
884 g_message ("%s: Leaving process name: %s",
892 /* Wrong pid, so drop this handle and fall through to
895 _wapi_handle_unref (current_process);
899 g_message ("%s: Need to create my own process handle", __func__);
902 process_handle.id = pid;
904 process_set_defaults (&process_handle);
905 process_set_name (&process_handle);
907 current_process = _wapi_handle_new (WAPI_HANDLE_PROCESS,
909 if (current_process == _WAPI_HANDLE_INVALID) {
910 g_warning ("%s: error creating process handle", __func__);
914 /* Make sure the new handle has a reference so it wont go away
915 * until this process exits
917 _wapi_handle_ref (current_process);
920 /* Returns a pseudo handle that doesn't need to be closed afterwards */
921 gpointer GetCurrentProcess (void)
923 mono_once (&process_current_once, process_set_current);
925 return((gpointer)-1);
928 guint32 GetProcessId (gpointer handle)
930 struct _WapiHandle_process *process_handle;
933 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_PROCESS,
934 (gpointer *)&process_handle);
936 SetLastError (ERROR_INVALID_HANDLE);
940 return (process_handle->id);
943 guint32 GetCurrentProcessId (void)
945 mono_once (&process_current_once, process_set_current);
947 return (GetProcessId (current_process));
950 /* Returns the process id as a convenience to the functions that call this */
951 static pid_t signal_process_if_gone (gpointer handle)
953 struct _WapiHandle_process *process_handle;
956 /* Make sure the process is signalled if it has exited - if
957 * the parent process didn't wait for it then it won't be
959 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_PROCESS,
960 (gpointer *)&process_handle);
962 /* It's possible that the handle has vanished during
963 * the _wapi_search_handle before it gets here, so
964 * don't spam the console with warnings.
966 /* g_warning ("%s: error looking up process handle %p",
973 g_message ("%s: looking at process %d", __func__, process_handle->id);
976 if (kill (process_handle->id, 0) == -1 &&
979 /* The process is dead, (EPERM tells us a new process
980 * has that ID, but as it's owned by someone else it
981 * can't be the one listed in our shared memory file)
983 _wapi_shared_handle_set_signal_state (handle, TRUE);
986 return (process_handle->id);
989 static gboolean process_enum (gpointer handle, gpointer user_data)
991 GArray *processes=user_data;
992 pid_t pid = signal_process_if_gone (handle);
999 /* Ignore processes that have already exited (ie they are signalled) */
1000 if (_wapi_handle_issignalled (handle) == FALSE) {
1002 g_message ("%s: process %d added to array", __func__, pid);
1005 /* This ensures that duplicates aren't returned (see
1006 * the comment above _wapi_search_handle () for why
1009 for (i = 0; i < processes->len; i++) {
1010 if (g_array_index (processes, pid_t, i) == pid) {
1011 /* We've already got this one, return
1012 * FALSE to keep searching
1018 g_array_append_val (processes, pid);
1021 /* Return false to keep searching */
1025 gboolean EnumProcesses (guint32 *pids, guint32 len, guint32 *needed)
1027 GArray *processes = g_array_new (FALSE, FALSE, sizeof(pid_t));
1030 mono_once (&process_current_once, process_set_current);
1032 _wapi_search_handle (WAPI_HANDLE_PROCESS, process_enum, processes,
1035 fit=len/sizeof(guint32);
1036 for (i = 0, j = 0; j < fit && i < processes->len; i++) {
1037 pids[j++] = g_array_index (processes, pid_t, i);
1040 g_array_free (processes, TRUE);
1042 *needed = j * sizeof(guint32);
1047 static gboolean process_open_compare (gpointer handle, gpointer user_data)
1050 pid_t checking_pid = signal_process_if_gone (handle);
1052 if (checking_pid == 0) {
1056 wanted_pid = GPOINTER_TO_UINT (user_data);
1058 /* It's possible to have more than one process handle with the
1059 * same pid, but only the one running process can be
1062 if (checking_pid == wanted_pid &&
1063 _wapi_handle_issignalled (handle) == FALSE) {
1064 /* If the handle is blown away in the window between
1065 * returning TRUE here and _wapi_search_handle pinging
1066 * the timestamp, the search will continue
1074 gpointer OpenProcess (guint32 access G_GNUC_UNUSED, gboolean inherit G_GNUC_UNUSED, guint32 pid)
1076 /* Find the process handle that corresponds to pid */
1079 mono_once (&process_current_once, process_set_current);
1082 g_message ("%s: looking for process %d", __func__, pid);
1085 handle = _wapi_search_handle (WAPI_HANDLE_PROCESS,
1086 process_open_compare,
1087 GUINT_TO_POINTER (pid), NULL);
1090 g_message ("%s: Can't find pid %d", __func__, pid);
1093 SetLastError (ERROR_PROC_NOT_FOUND);
1098 _wapi_handle_ref (handle);
1103 gboolean GetExitCodeProcess (gpointer process, guint32 *code)
1105 struct _WapiHandle_process *process_handle;
1108 mono_once (&process_current_once, process_set_current);
1114 ok=_wapi_lookup_handle (process, WAPI_HANDLE_PROCESS,
1115 (gpointer *)&process_handle);
1118 g_message ("%s: Can't find process %p", __func__, process);
1124 /* A process handle is only signalled if the process has exited
1125 * and has been waited for */
1126 if (_wapi_handle_issignalled (process) == TRUE ||
1127 process_wait (process, 0) == WAIT_OBJECT_0) {
1128 *code = process_handle->exitstatus;
1130 *code = STILL_ACTIVE;
1136 gboolean GetProcessTimes (gpointer process, WapiFileTime *create_time,
1137 WapiFileTime *exit_time, WapiFileTime *kernel_time,
1138 WapiFileTime *user_time)
1140 struct _WapiHandle_process *process_handle;
1143 mono_once (&process_current_once, process_set_current);
1145 if(create_time==NULL || exit_time==NULL || kernel_time==NULL ||
1147 /* Not sure if w32 allows NULLs here or not */
1151 ok=_wapi_lookup_handle (process, WAPI_HANDLE_PROCESS,
1152 (gpointer *)&process_handle);
1155 g_message ("%s: Can't find process %p", __func__, process);
1161 *create_time=process_handle->create_time;
1163 /* A process handle is only signalled if the process has
1164 * exited. Otherwise exit_time isn't set
1166 if(_wapi_handle_issignalled (process)==TRUE) {
1167 *exit_time=process_handle->exit_time;
1173 gboolean EnumProcessModules (gpointer process, gpointer *modules,
1174 guint32 size, guint32 *needed)
1176 /* Store modules in an array of pointers (main module as
1177 * modules[0]), using the load address for each module as a
1178 * token. (Use 'NULL' as an alternative for the main module
1179 * so that the simple implementation can just return one item
1180 * for now.) Get the info from /proc/<pid>/maps on linux,
1181 * other systems will have to implement /dev/kmem reading or
1182 * whatever other horrid technique is needed.
1184 if(size<sizeof(gpointer)) {
1190 *needed=sizeof(gpointer);
1193 *needed=sizeof(gpointer);
1199 guint32 GetModuleBaseName (gpointer process, gpointer module,
1200 gunichar2 *basename, guint32 size)
1202 struct _WapiHandle_process *process_handle;
1205 mono_once (&process_current_once, process_set_current);
1208 g_message ("%s: Getting module base name, process handle %p module %p",
1209 __func__, process, module);
1212 if(basename==NULL || size==0) {
1216 ok=_wapi_lookup_handle (process, WAPI_HANDLE_PROCESS,
1217 (gpointer *)&process_handle);
1220 g_message ("%s: Can't find process %p", __func__, process);
1227 /* Shorthand for the main module, which has the
1228 * process name recorded in the handle data
1231 gunichar2 *procname;
1232 guchar *procname_utf8 = NULL;
1236 g_message ("%s: Returning main module name", __func__);
1239 pid=process_handle->id;
1240 procname_utf8 = process_handle->proc_name;
1243 g_message ("%s: Process name is [%s]", __func__,
1247 procname = g_utf8_to_utf16 (procname_utf8, -1, NULL, &len,
1249 if (procname == NULL) {
1254 /* Add the terminator, and convert chars to bytes */
1255 bytes = (len + 1) * 2;
1259 g_message ("%s: Size %d smaller than needed (%ld); truncating", __func__, size, bytes);
1262 memcpy (basename, procname, size);
1265 g_message ("%s: Size %d larger than needed (%ld)",
1266 __func__, size, bytes);
1269 memcpy (basename, procname, bytes);
1276 /* Look up the address in /proc/<pid>/maps */
1282 gboolean GetProcessWorkingSetSize (gpointer process, size_t *min, size_t *max)
1284 struct _WapiHandle_process *process_handle;
1287 mono_once (&process_current_once, process_set_current);
1289 if(min==NULL || max==NULL) {
1290 /* Not sure if w32 allows NULLs here or not */
1294 ok=_wapi_lookup_handle (process, WAPI_HANDLE_PROCESS,
1295 (gpointer *)&process_handle);
1298 g_message ("%s: Can't find process %p", __func__, process);
1304 *min=process_handle->min_working_set;
1305 *max=process_handle->max_working_set;
1310 gboolean SetProcessWorkingSetSize (gpointer process, size_t min, size_t max)
1312 struct _WapiHandle_process *process_handle;
1315 mono_once (&process_current_once, process_set_current);
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 process_handle->min_working_set=min;
1328 process_handle->max_working_set=max;
1335 TerminateProcess (gpointer process, gint32 exitCode)
1337 struct _WapiHandle_process *process_handle;
1342 ok = _wapi_lookup_handle (process, WAPI_HANDLE_PROCESS,
1343 (gpointer *) &process_handle);
1347 g_message ("%s: Can't find process %p", __func__, process);
1349 SetLastError (ERROR_INVALID_HANDLE);
1353 signo = (exitCode == -1) ? SIGKILL : SIGTERM;
1354 ret = kill (process_handle->id, signo);
1358 SetLastError (ERROR_INVALID_PARAMETER);
1361 SetLastError (ERROR_ACCESS_DENIED);
1364 SetLastError (ERROR_PROC_NOT_FOUND);
1367 SetLastError (ERROR_GEN_FAILURE);