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);
85 g_message ("%s: Setting handle %p signalled", __func__, handle);
88 _wapi_shared_handle_set_signal_state (handle, TRUE);
90 _wapi_handle_unlock_shared_handles ();
92 /* Drop the reference we hold so we have somewhere to store
93 * the exit details, now the process has in fact exited
95 _wapi_handle_unref (handle);
100 /* See if any child processes have terminated and wait() for them,
101 * updating process handle info. This function is called from the
102 * collection thread every few seconds.
104 static gboolean waitfor_pid (gpointer test, gpointer user_data)
106 struct _WapiHandle_process *process;
111 if (_wapi_handle_issignalled (test)) {
112 /* We've already done this one */
116 ok = _wapi_lookup_handle (test, WAPI_HANDLE_PROCESS,
117 (gpointer *)&process);
119 /* The handle must have been too old and was reaped */
124 ret = waitpid (process->id, &status, WNOHANG);
125 } while (errno == EINTR);
128 /* Process not ready for wait */
130 g_message ("%s: Process %d not ready for waiting for: %s",
131 __func__, process->id, g_strerror (errno));
138 g_message ("%s: Process %d finished", __func__, ret);
141 *(int *)user_data = status;
146 void _wapi_process_reap (void)
152 g_message ("%s: Reaping child processes", __func__);
156 proc = _wapi_search_handle (WAPI_HANDLE_PROCESS, waitfor_pid,
157 &status, NULL, FALSE);
160 g_message ("%s: process handle %p exit code %d",
161 __func__, proc, status);
164 process_set_termination_details (proc, status);
166 } while (proc != NULL);
169 /* Limitations: This can only wait for processes that are our own
170 * children. Fixing this means resurrecting a daemon helper to manage
173 static guint32 process_wait (gpointer handle, guint32 timeout)
175 struct _WapiHandle_process *process_handle;
181 g_message ("%s: Waiting for process %p", __func__, handle);
184 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_PROCESS,
185 (gpointer *)&process_handle);
187 g_warning ("%s: error looking up process handle %p", __func__,
192 pid = process_handle->id;
195 g_message ("%s: PID is %d, timeout %d", __func__, pid, timeout);
198 if (timeout == INFINITE) {
199 while ((ret = waitpid (pid, &status, 0)) != pid) {
200 if (ret == (pid_t)-1 && errno != EINTR) {
204 } else if (timeout == 0) {
206 ret = waitpid (pid, &status, WNOHANG);
208 return (WAIT_TIMEOUT);
213 ret = waitpid (pid, &status, WNOHANG);
216 } else if (ret == (pid_t)-1 && errno != EINTR) {
220 _wapi_handle_spin (100);
222 } while (timeout > 0);
225 return(WAIT_TIMEOUT);
229 /* Process must have exited */
231 g_message ("%s: Wait done, status %d", __func__, status);
234 ok = process_set_termination_details (handle, status);
236 SetLastError (ERROR_OUTOFMEMORY);
237 return (WAIT_FAILED);
240 return(WAIT_OBJECT_0);
243 void _wapi_process_signal_self ()
245 if (current_process != NULL) {
246 process_set_termination_details (current_process, 0);
251 static void process_set_defaults (struct _WapiHandle_process *process_handle)
253 /* These seem to be the defaults on w2k */
254 process_handle->min_working_set = 204800;
255 process_handle->max_working_set = 1413120;
257 _wapi_time_t_to_filetime (time (NULL), &process_handle->create_time);
260 /* Implemented as just a wrapper around CreateProcess () */
261 gboolean ShellExecuteEx (WapiShellExecuteInfo *sei)
264 WapiProcessInformation process_info;
266 gchar *u8file, *u8params, *u8args;
269 /* w2k just segfaults here, but we can do better than
272 SetLastError (ERROR_INVALID_PARAMETER);
276 if (sei->lpFile == NULL) {
277 /* w2k returns TRUE for this, for some reason. */
281 /* Put both executable and parameters into the second argument
282 * to CreateProcess (), so it searches $PATH. The conversion
283 * into and back out of utf8 is because there is no
284 * g_strdup_printf () equivalent for gunichar2 :-(
286 u8file = g_utf16_to_utf8 (sei->lpFile, -1, NULL, NULL, NULL);
287 if (u8file == NULL) {
288 SetLastError (ERROR_INVALID_DATA);
292 if (sei->lpParameters != NULL) {
293 u8params = g_utf16_to_utf8 (sei->lpParameters, -1, NULL, NULL, NULL);
294 if (u8params == NULL) {
295 SetLastError (ERROR_INVALID_DATA);
300 u8args = g_strdup_printf ("%s %s", u8file, u8params);
301 if (u8args == NULL) {
302 SetLastError (ERROR_INVALID_DATA);
308 args = g_utf8_to_utf16 (u8args, -1, NULL, NULL, NULL);
314 args = g_utf8_to_utf16 (u8file, -1, NULL, NULL, NULL);
318 SetLastError (ERROR_INVALID_DATA);
322 ret = CreateProcess (NULL, args, NULL, NULL, TRUE,
323 CREATE_UNICODE_ENVIRONMENT, NULL,
324 sei->lpDirectory, NULL, &process_info);
331 if (sei->fMask & SEE_MASK_NOCLOSEPROCESS) {
332 sei->hProcess = process_info.hProcess;
334 CloseHandle (process_info.hProcess);
340 gboolean CreateProcess (const gunichar2 *appname, const gunichar2 *cmdline,
341 WapiSecurityAttributes *process_attrs G_GNUC_UNUSED,
342 WapiSecurityAttributes *thread_attrs G_GNUC_UNUSED,
343 gboolean inherit_handles, guint32 create_flags,
344 gpointer new_environ, const gunichar2 *cwd,
345 WapiStartupInfo *startup,
346 WapiProcessInformation *process_info)
348 gchar *cmd=NULL, *prog = NULL, *full_prog = NULL, *args = NULL, *args_after_prog = NULL, *dir = NULL, **env_strings = NULL, **argv;
349 guint32 i, env_count = 0;
350 gboolean ret = FALSE;
352 struct _WapiHandle_process process_handle = {0}, *process_handle_data;
354 int in_fd, out_fd, err_fd;
358 mono_once (&process_ops_once, process_ops_init);
360 /* appname and cmdline specify the executable and its args:
362 * If appname is not NULL, it is the name of the executable.
363 * Otherwise the executable is the first token in cmdline.
365 * Executable searching:
367 * If appname is not NULL, it can specify the full path and
368 * file name, or else a partial name and the current directory
369 * will be used. There is no additional searching.
371 * If appname is NULL, the first whitespace-delimited token in
372 * cmdline is used. If the name does not contain a full
373 * directory path, the search sequence is:
375 * 1) The directory containing the current process
376 * 2) The current working directory
377 * 3) The windows system directory (Ignored)
378 * 4) The windows directory (Ignored)
381 * Just to make things more interesting, tokens can contain
382 * white space if they are surrounded by quotation marks. I'm
383 * beginning to understand just why windows apps are generally
384 * so crap, with an API like this :-(
386 if (appname != NULL) {
387 cmd = mono_unicode_to_external (appname);
390 g_message ("%s: unicode conversion returned NULL",
394 SetLastError (ERROR_PATH_NOT_FOUND);
398 /* Turn all the slashes round the right way */
399 for (i = 0; i < strlen (cmd); i++) {
400 if (cmd[i] == '\\') {
406 if (cmdline != NULL) {
407 args = mono_unicode_to_external (cmdline);
410 g_message ("%s: unicode conversion returned NULL", __func__);
413 SetLastError (ERROR_PATH_NOT_FOUND);
419 dir = mono_unicode_to_external (cwd);
422 g_message ("%s: unicode conversion returned NULL", __func__);
425 SetLastError (ERROR_PATH_NOT_FOUND);
429 /* Turn all the slashes round the right way */
430 for (i = 0; i < strlen (dir); i++) {
431 if (dir[i] == '\\') {
438 /* We can't put off locating the executable any longer :-( */
441 if (g_ascii_isalpha (cmd[0]) && (cmd[1] == ':')) {
442 /* Strip off the drive letter. I can't
443 * believe that CP/M holdover is still
446 g_memmove (cmd, cmd+2, strlen (cmd)-2);
447 cmd[strlen (cmd)-2] = '\0';
450 unquoted = g_shell_unquote (cmd, NULL);
451 if (unquoted[0] == '/') {
452 /* Assume full path given */
453 prog = g_strdup (unquoted);
455 /* Executable existing ? */
456 if (access (prog, X_OK) != 0) {
458 g_message ("%s: Couldn't find executable %s",
463 SetLastError (ERROR_FILE_NOT_FOUND);
467 /* Search for file named by cmd in the current
470 char *curdir = g_get_current_dir ();
472 prog = g_strdup_printf ("%s/%s", curdir, unquoted);
476 /* And make sure it's executable */
477 if (access (prog, X_OK) != 0) {
479 g_message ("%s: Couldn't find executable %s",
483 SetLastError (ERROR_FILE_NOT_FOUND);
488 args_after_prog = args;
493 /* Dig out the first token from args, taking quotation
497 /* First, strip off all leading whitespace */
498 args = g_strchug (args);
500 /* args_after_prog points to the contents of args
501 * after token has been set (otherwise argv[0] is
504 args_after_prog = args;
506 /* Assume the opening quote will always be the first
509 if (args[0] == '\"' || args [0] == '\'') {
511 for (i = 1; args[i] != '\0' && args[i] != quote; i++);
512 if (g_ascii_isspace (args[i+1])) {
513 /* We found the first token */
514 token = g_strndup (args+1, i-1);
515 args_after_prog = args + i;
517 /* Quotation mark appeared in the
518 * middle of the token. Just give the
519 * whole first token, quotes and all,
526 /* No quote mark, or malformed */
527 for (i = 0; args[i] != '\0'; i++) {
528 if (g_ascii_isspace (args[i])) {
529 token = g_strndup (args, i);
530 args_after_prog = args + i + 1;
536 if (token == NULL && args[0] != '\0') {
537 /* Must be just one token in the string */
538 token = g_strdup (args);
539 args_after_prog = NULL;
545 g_message ("%s: Couldn't find what to exec", __func__);
548 SetLastError (ERROR_PATH_NOT_FOUND);
552 /* Turn all the slashes round the right way. Only for
555 for (i = 0; i < strlen (token); i++) {
556 if (token[i] == '\\') {
561 if (g_ascii_isalpha (token[0]) && (token[1] == ':')) {
562 /* Strip off the drive letter. I can't
563 * believe that CP/M holdover is still
566 g_memmove (token, token+2, strlen (token)-2);
567 token[strlen (token)-2] = '\0';
570 if (token[0] == '/') {
571 /* Assume full path given */
572 prog = g_strdup (token);
574 /* Executable existing ? */
575 if (access (prog, X_OK) != 0) {
578 g_message ("%s: Couldn't find executable %s",
582 SetLastError (ERROR_FILE_NOT_FOUND);
587 char *curdir = g_get_current_dir ();
589 /* FIXME: Need to record the directory
590 * containing the current process, and check
591 * that for the new executable as the first
595 prog = g_strdup_printf ("%s/%s", curdir, token);
598 /* I assume X_OK is the criterion to use,
601 if (access (prog, X_OK) != 0) {
603 prog = g_find_program_in_path (token);
606 g_message ("%s: Couldn't find executable %s", __func__, token);
610 SetLastError (ERROR_FILE_NOT_FOUND);
620 g_message ("%s: Exec prog [%s] args [%s]", __func__, prog,
624 if (args_after_prog != NULL && *args_after_prog) {
627 qprog = g_shell_quote (prog);
628 full_prog = g_strconcat (qprog, " ", args_after_prog, NULL);
631 full_prog = g_shell_quote (prog);
634 ret = g_shell_parse_argv (full_prog, NULL, &argv, &gerr);
636 /* FIXME: Could do something with the GError here
640 if (startup != NULL && startup->dwFlags & STARTF_USESTDHANDLES) {
641 in_fd = GPOINTER_TO_UINT (startup->hStdInput);
642 out_fd = GPOINTER_TO_UINT (startup->hStdOutput);
643 err_fd = GPOINTER_TO_UINT (startup->hStdError);
645 in_fd = GPOINTER_TO_UINT (GetStdHandle (STD_INPUT_HANDLE));
646 out_fd = GPOINTER_TO_UINT (GetStdHandle (STD_OUTPUT_HANDLE));
647 err_fd = GPOINTER_TO_UINT (GetStdHandle (STD_ERROR_HANDLE));
650 g_strlcpy (process_handle.proc_name, prog,
651 _WAPI_PROC_NAME_MAX_LEN - 1);
653 process_set_defaults (&process_handle);
655 handle = _wapi_handle_new (WAPI_HANDLE_PROCESS, &process_handle);
656 if (handle == _WAPI_HANDLE_INVALID) {
657 g_warning ("%s: error creating process handle", __func__);
659 SetLastError (ERROR_PATH_NOT_FOUND);
663 /* Hold another reference so the process has somewhere to
664 * store its exit data even if we drop this handle
666 _wapi_handle_ref (handle);
668 /* new_environ is a block of NULL-terminated strings, which
669 * is itself NULL-terminated. Of course, passing an array of
670 * string pointers would have made things too easy :-(
672 * If new_environ is not NULL it specifies the entire set of
673 * environment variables in the new process. Otherwise the
674 * new process inherits the same environment.
676 if (new_environ != NULL) {
677 gunichar2 *new_environp;
679 /* Count the number of strings */
680 for (new_environp = (gunichar2 *)new_environ; *new_environp;
683 while (*new_environp) {
688 /* +2: one for the process handle value, and the last
691 env_strings = g_new0 (gchar *, env_count + 2);
693 /* Copy each environ string into 'strings' turning it
694 * into utf8 (or the requested encoding) at the same
698 for (new_environp = (gunichar2 *)new_environ; *new_environp;
700 env_strings[env_count] = mono_unicode_to_external (new_environp);
702 while (*new_environp) {
707 for (i = 0; environ[i] != NULL; i++) {
711 /* +2: one for the process handle value, and the last
714 env_strings = g_new0 (gchar *, env_count + 2);
716 /* Copy each environ string into 'strings' turning it
717 * into utf8 (or the requested encoding) at the same
721 for (i = 0; environ[i] != NULL; i++) {
722 env_strings[env_count] = g_strdup (environ[i]);
726 /* pass process handle info to the child, so it doesn't have
727 * to do an expensive search over the whole list
729 if (env_strings != NULL) {
730 struct _WapiHandleUnshared *handle_data;
731 struct _WapiHandle_shared_ref *ref;
733 handle_data = &_WAPI_PRIVATE_HANDLES(GPOINTER_TO_UINT(handle));
734 ref = &handle_data->u.shared;
736 env_strings[env_count] = g_strdup_printf ("_WAPI_PROCESS_HANDLE_OFFSET=%d", ref->offset);
739 thr_ret = _wapi_handle_lock_shared_handles ();
740 g_assert (thr_ret == 0);
745 SetLastError (ERROR_OUTOFMEMORY);
746 _wapi_handle_unref (handle);
748 } else if (pid == 0) {
751 /* Wait for the parent to finish setting up the
752 * handle. The semaphore lock is safe because the
753 * sem_undo structures of a semaphore aren't inherited
756 thr_ret = _wapi_handle_lock_shared_handles ();
757 g_assert (thr_ret == 0);
759 _wapi_handle_unlock_shared_handles ();
761 /* should we detach from the process group? */
763 /* Connect stdin, stdout and stderr */
768 if (inherit_handles != TRUE) {
769 /* FIXME: do something here */
772 /* Close all file descriptors */
773 for (i = getdtablesize () - 1; i > 2; i--) {
778 g_message ("%s: exec()ing [%s] in dir [%s]", __func__, cmd,
780 for (i = 0; argv[i] != NULL; i++) {
781 g_message ("arg %d: [%s]", i, argv[i]);
784 for (i = 0; env_strings[i] != NULL; i++) {
785 g_message ("env %d: [%s]", i, env_strings[i]);
790 if (dir != NULL && chdir (dir) == -1) {
796 execve (argv[0], argv, env_strings);
803 ret = _wapi_lookup_handle (handle, WAPI_HANDLE_PROCESS,
804 (gpointer *)&process_handle_data);
806 g_warning ("%s: error looking up process handle %p", __func__,
808 _wapi_handle_unref (handle);
812 process_handle_data->id = pid;
814 if (process_info != NULL) {
815 process_info->hProcess = handle;
816 process_info->dwProcessId = pid;
818 /* FIXME: we might need to handle the thread info some
821 process_info->hThread = NULL;
822 process_info->dwThreadId = 0;
826 _wapi_handle_unlock_shared_handles ();
831 if (full_prog != NULL) {
840 if(env_strings != NULL) {
841 g_strfreev (env_strings);
847 static void process_set_name (struct _WapiHandle_process *process_handle)
849 gchar *progname, *utf8_progname, *slash;
851 progname=g_get_prgname ();
852 utf8_progname=mono_utf8_from_external (progname);
855 g_message ("%s: using [%s] as prog name", __func__, progname);
858 if(utf8_progname!=NULL) {
859 slash=strrchr (utf8_progname, '/');
861 g_strlcpy (process_handle->proc_name, slash+1,
862 _WAPI_PROC_NAME_MAX_LEN - 1);
864 g_strlcpy (process_handle->proc_name, utf8_progname,
865 _WAPI_PROC_NAME_MAX_LEN - 1);
868 g_free (utf8_progname);
872 extern void _wapi_time_t_to_filetime (time_t timeval, WapiFileTime *filetime);
874 #if !GLIB_CHECK_VERSION (2,4,0)
875 #define g_setenv(a,b,c) setenv(a,b,c)
876 #define g_unsetenv(a) unsetenv(a)
879 static void process_set_current (void)
881 pid_t pid = _wapi_getpid ();
882 const char *handle_env;
883 struct _WapiHandle_process process_handle = {0};
885 handle_env = g_getenv ("_WAPI_PROCESS_HANDLE_OFFSET");
886 g_unsetenv ("_WAPI_PROCESS_HANDLE_OFFSET");
888 if (handle_env != NULL) {
889 struct _WapiHandle_process *process_handlep;
890 gchar *procname = NULL;
893 current_process = _wapi_handle_new_from_offset (WAPI_HANDLE_PROCESS, atoi (handle_env), TRUE);
896 g_message ("%s: Found my process handle: %p (offset %d 0x%x)",
897 __func__, current_process, atoi (handle_env),
901 ok = _wapi_lookup_handle (current_process, WAPI_HANDLE_PROCESS,
902 (gpointer *)&process_handlep);
904 /* This test will probably break on linuxthreads, but
905 * that should be ancient history on all distros we
908 if (process_handlep->id == pid) {
909 procname = process_handlep->proc_name;
910 if (!strcmp (procname, "mono")) {
911 /* Set a better process name */
913 g_message ("%s: Setting better process name", __func__);
916 process_set_name (process_handlep);
919 g_message ("%s: Leaving process name: %s", __func__, procname);
926 /* Wrong pid, so drop this handle and fall through to
929 _wapi_handle_unref (current_process);
933 /* We get here if the handle wasn't specified in the
934 * environment, or if the process ID was wrong, or if the
935 * handle lookup failed (eg if the parent process forked and
936 * quit immediately, and deleted the shared data before the
937 * child got a chance to attach it.)
941 g_message ("%s: Need to create my own process handle", __func__);
944 process_handle.id = pid;
946 process_set_defaults (&process_handle);
947 process_set_name (&process_handle);
949 current_process = _wapi_handle_new (WAPI_HANDLE_PROCESS,
951 if (current_process == _WAPI_HANDLE_INVALID) {
952 g_warning ("%s: error creating process handle", __func__);
957 gpointer _wapi_process_duplicate ()
959 mono_once (&process_current_once, process_set_current);
961 _wapi_handle_ref (current_process);
963 return(current_process);
966 /* Returns a pseudo handle that doesn't need to be closed afterwards */
967 gpointer GetCurrentProcess (void)
969 mono_once (&process_current_once, process_set_current);
971 return(_WAPI_PROCESS_CURRENT);
974 guint32 GetProcessId (gpointer handle)
976 struct _WapiHandle_process *process_handle;
979 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_PROCESS,
980 (gpointer *)&process_handle);
982 SetLastError (ERROR_INVALID_HANDLE);
986 return (process_handle->id);
989 guint32 GetCurrentProcessId (void)
991 mono_once (&process_current_once, process_set_current);
993 return (GetProcessId (current_process));
996 /* Returns the process id as a convenience to the functions that call this */
997 static pid_t signal_process_if_gone (gpointer handle)
999 struct _WapiHandle_process *process_handle;
1002 /* Make sure the process is signalled if it has exited - if
1003 * the parent process didn't wait for it then it won't be
1005 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_PROCESS,
1006 (gpointer *)&process_handle);
1008 /* It's possible that the handle has vanished during
1009 * the _wapi_search_handle before it gets here, so
1010 * don't spam the console with warnings.
1012 /* g_warning ("%s: error looking up process handle %p",
1013 __func__, handle);*/
1019 g_message ("%s: looking at process %d", __func__, process_handle->id);
1022 if (kill (process_handle->id, 0) == -1 &&
1025 /* The process is dead, (EPERM tells us a new process
1026 * has that ID, but as it's owned by someone else it
1027 * can't be the one listed in our shared memory file)
1029 _wapi_shared_handle_set_signal_state (handle, TRUE);
1032 return (process_handle->id);
1035 static gboolean process_enum (gpointer handle, gpointer user_data)
1037 GArray *processes=user_data;
1038 pid_t pid = signal_process_if_gone (handle);
1045 /* Ignore processes that have already exited (ie they are signalled) */
1046 if (_wapi_handle_issignalled (handle) == FALSE) {
1048 g_message ("%s: process %d added to array", __func__, pid);
1051 /* This ensures that duplicates aren't returned (see
1052 * the comment above _wapi_search_handle () for why
1055 for (i = 0; i < processes->len; i++) {
1056 if (g_array_index (processes, pid_t, i) == pid) {
1057 /* We've already got this one, return
1058 * FALSE to keep searching
1064 g_array_append_val (processes, pid);
1067 /* Return false to keep searching */
1071 gboolean EnumProcesses (guint32 *pids, guint32 len, guint32 *needed)
1073 GArray *processes = g_array_new (FALSE, FALSE, sizeof(pid_t));
1076 mono_once (&process_current_once, process_set_current);
1078 _wapi_search_handle (WAPI_HANDLE_PROCESS, process_enum, processes,
1081 fit=len/sizeof(guint32);
1082 for (i = 0, j = 0; j < fit && i < processes->len; i++) {
1083 pids[j++] = g_array_index (processes, pid_t, i);
1086 g_array_free (processes, TRUE);
1088 *needed = j * sizeof(guint32);
1093 static gboolean process_open_compare (gpointer handle, gpointer user_data)
1096 pid_t checking_pid = signal_process_if_gone (handle);
1098 if (checking_pid == 0) {
1102 wanted_pid = GPOINTER_TO_UINT (user_data);
1104 /* It's possible to have more than one process handle with the
1105 * same pid, but only the one running process can be
1108 if (checking_pid == wanted_pid &&
1109 _wapi_handle_issignalled (handle) == FALSE) {
1110 /* If the handle is blown away in the window between
1111 * returning TRUE here and _wapi_search_handle pinging
1112 * the timestamp, the search will continue
1120 gpointer OpenProcess (guint32 access G_GNUC_UNUSED, gboolean inherit G_GNUC_UNUSED, guint32 pid)
1122 /* Find the process handle that corresponds to pid */
1125 mono_once (&process_current_once, process_set_current);
1128 g_message ("%s: looking for process %d", __func__, pid);
1131 handle = _wapi_search_handle (WAPI_HANDLE_PROCESS,
1132 process_open_compare,
1133 GUINT_TO_POINTER (pid), NULL, TRUE);
1136 g_message ("%s: Can't find pid %d", __func__, pid);
1139 SetLastError (ERROR_PROC_NOT_FOUND);
1144 _wapi_handle_ref (handle);
1149 gboolean GetExitCodeProcess (gpointer process, guint32 *code)
1151 struct _WapiHandle_process *process_handle;
1154 mono_once (&process_current_once, process_set_current);
1160 ok=_wapi_lookup_handle (process, WAPI_HANDLE_PROCESS,
1161 (gpointer *)&process_handle);
1164 g_message ("%s: Can't find process %p", __func__, process);
1170 /* A process handle is only signalled if the process has exited
1171 * and has been waited for */
1172 if (_wapi_handle_issignalled (process) == TRUE ||
1173 process_wait (process, 0) == WAIT_OBJECT_0) {
1174 *code = process_handle->exitstatus;
1176 *code = STILL_ACTIVE;
1182 gboolean GetProcessTimes (gpointer process, WapiFileTime *create_time,
1183 WapiFileTime *exit_time, WapiFileTime *kernel_time,
1184 WapiFileTime *user_time)
1186 struct _WapiHandle_process *process_handle;
1189 mono_once (&process_current_once, process_set_current);
1191 if(create_time==NULL || exit_time==NULL || kernel_time==NULL ||
1193 /* Not sure if w32 allows NULLs here or not */
1197 ok=_wapi_lookup_handle (process, WAPI_HANDLE_PROCESS,
1198 (gpointer *)&process_handle);
1201 g_message ("%s: Can't find process %p", __func__, process);
1207 *create_time=process_handle->create_time;
1209 /* A process handle is only signalled if the process has
1210 * exited. Otherwise exit_time isn't set
1212 if(_wapi_handle_issignalled (process)==TRUE) {
1213 *exit_time=process_handle->exit_time;
1219 gboolean EnumProcessModules (gpointer process, gpointer *modules,
1220 guint32 size, guint32 *needed)
1222 /* Store modules in an array of pointers (main module as
1223 * modules[0]), using the load address for each module as a
1224 * token. (Use 'NULL' as an alternative for the main module
1225 * so that the simple implementation can just return one item
1226 * for now.) Get the info from /proc/<pid>/maps on linux,
1227 * other systems will have to implement /dev/kmem reading or
1228 * whatever other horrid technique is needed.
1230 if(size<sizeof(gpointer)) {
1236 *needed=sizeof(gpointer);
1239 *needed=sizeof(gpointer);
1245 guint32 GetModuleBaseName (gpointer process, gpointer module,
1246 gunichar2 *basename, guint32 size)
1248 struct _WapiHandle_process *process_handle;
1251 mono_once (&process_current_once, process_set_current);
1254 g_message ("%s: Getting module base name, process handle %p module %p",
1255 __func__, process, module);
1258 if(basename==NULL || size==0) {
1262 ok=_wapi_lookup_handle (process, WAPI_HANDLE_PROCESS,
1263 (gpointer *)&process_handle);
1266 g_message ("%s: Can't find process %p", __func__, process);
1273 /* Shorthand for the main module, which has the
1274 * process name recorded in the handle data
1277 gunichar2 *procname;
1278 gchar *procname_utf8 = NULL;
1282 g_message ("%s: Returning main module name", __func__);
1285 pid=process_handle->id;
1286 procname_utf8 = process_handle->proc_name;
1289 g_message ("%s: Process name is [%s]", __func__,
1293 procname = g_utf8_to_utf16 (procname_utf8, -1, NULL, &len,
1295 if (procname == NULL) {
1300 /* Add the terminator, and convert chars to bytes */
1301 bytes = (len + 1) * 2;
1305 g_message ("%s: Size %d smaller than needed (%ld); truncating", __func__, size, bytes);
1308 memcpy (basename, procname, size);
1311 g_message ("%s: Size %d larger than needed (%ld)",
1312 __func__, size, bytes);
1315 memcpy (basename, procname, bytes);
1322 /* Look up the address in /proc/<pid>/maps */
1328 gboolean GetProcessWorkingSetSize (gpointer process, size_t *min, size_t *max)
1330 struct _WapiHandle_process *process_handle;
1333 mono_once (&process_current_once, process_set_current);
1335 if(min==NULL || max==NULL) {
1336 /* Not sure if w32 allows NULLs here or not */
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 *min=process_handle->min_working_set;
1351 *max=process_handle->max_working_set;
1356 gboolean SetProcessWorkingSetSize (gpointer process, size_t min, size_t max)
1358 struct _WapiHandle_process *process_handle;
1361 mono_once (&process_current_once, process_set_current);
1363 ok=_wapi_lookup_handle (process, WAPI_HANDLE_PROCESS,
1364 (gpointer *)&process_handle);
1367 g_message ("%s: Can't find process %p", __func__, process);
1373 process_handle->min_working_set=min;
1374 process_handle->max_working_set=max;
1381 TerminateProcess (gpointer process, gint32 exitCode)
1383 struct _WapiHandle_process *process_handle;
1388 ok = _wapi_lookup_handle (process, WAPI_HANDLE_PROCESS,
1389 (gpointer *) &process_handle);
1393 g_message ("%s: Can't find process %p", __func__, process);
1395 SetLastError (ERROR_INVALID_HANDLE);
1399 signo = (exitCode == -1) ? SIGKILL : SIGTERM;
1400 ret = kill (process_handle->id, signo);
1404 SetLastError (ERROR_INVALID_PARAMETER);
1407 SetLastError (ERROR_ACCESS_DENIED);
1410 SetLastError (ERROR_PROC_NOT_FOUND);
1413 SetLastError (ERROR_GEN_FAILURE);