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 */
47 static mono_once_t process_current_once=MONO_ONCE_INIT;
48 static gpointer current_process=NULL;
50 static mono_once_t process_ops_once=MONO_ONCE_INIT;
52 static void process_ops_init (void)
54 _wapi_handle_register_capabilities (WAPI_HANDLE_PROCESS,
55 WAPI_HANDLE_CAP_WAIT |
56 WAPI_HANDLE_CAP_SPECIAL_WAIT);
59 static gboolean process_set_termination_details (gpointer handle, int status)
61 struct _WapiHandleShared shared_handle;
62 struct _WapiHandle_process *process_handle;
65 ok = _wapi_copy_handle (handle, WAPI_HANDLE_PROCESS, &shared_handle);
67 g_warning ("%s: error looking up process handle %p",
72 process_handle = &shared_handle.u.process;
74 if (WIFSIGNALED(status)) {
75 process_handle->exitstatus = 128 + WTERMSIG(status);
77 process_handle->exitstatus = WEXITSTATUS(status);
79 _wapi_time_t_to_filetime (time(NULL), &process_handle->exit_time);
81 ok = _wapi_replace_handle (handle, WAPI_HANDLE_PROCESS,
84 _wapi_shared_handle_set_signal_state (handle, TRUE);
89 /* See if any child processes have terminated and wait() for them,
90 * updating process handle info. This function is called from the
91 * collection thread every few seconds.
93 static gboolean waitfor_pid (gpointer test, gpointer user_data G_GNUC_UNUSED)
95 struct _WapiHandle_process *process;
100 ok = _wapi_lookup_handle (test, WAPI_HANDLE_PROCESS,
101 (gpointer *)&process);
107 ret == waitpid (process->id, &status, WNOHANG);
108 } while (errno == EINTR);
111 /* Process not ready for wait */
113 g_message ("%s: Process %d not ready for waiting for",
121 g_message ("%s: Process %d finished", __func__, ret);
124 process_set_termination_details (test, status);
126 /* return FALSE to keep searching */
130 void _wapi_process_reap (void)
133 g_message ("%s: Reaping child processes", __func__);
136 _wapi_search_handle (WAPI_HANDLE_PROCESS, waitfor_pid, NULL, NULL);
139 /* Limitations: This can only wait for processes that are our own
140 * children. Fixing this means resurrecting a daemon helper to manage
143 static guint32 process_wait (gpointer handle, guint32 timeout)
145 struct _WapiHandle_process *process_handle;
151 g_message ("%s: Waiting for process %p", __func__, handle);
154 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_PROCESS,
155 (gpointer *)&process_handle);
157 g_warning ("%s: error looking up process handle %p", __func__,
162 pid = process_handle->id;
165 g_message ("%s: PID is %d", __func__, pid);
168 if (timeout == INFINITE) {
169 while ((ret = waitpid (pid, &status, 0)) != pid) {
170 if (ret == (pid_t)-1 && errno != EINTR) {
174 } else if (timeout == 0) {
176 ret = waitpid (pid, &status, WNOHANG);
178 return (WAIT_TIMEOUT);
183 ret = waitpid (pid, &status, WNOHANG);
186 } else if (ret == (pid_t)-1 && errno != EINTR) {
190 _wapi_handle_spin (100);
192 } while (timeout > 0);
195 return(WAIT_TIMEOUT);
199 /* Process must have exited */
201 g_message ("%s: Wait done", __func__);
204 ok = process_set_termination_details (handle, status);
206 SetLastError (ERROR_OUTOFMEMORY);
207 return (WAIT_FAILED);
210 return(WAIT_OBJECT_0);
213 static void process_set_defaults (struct _WapiHandle_process *process_handle)
215 /* These seem to be the defaults on w2k */
216 process_handle->min_working_set = 204800;
217 process_handle->max_working_set = 1413120;
219 _wapi_time_t_to_filetime (time (NULL), &process_handle->create_time);
222 gboolean CreateProcess (const gunichar2 *appname, gunichar2 *cmdline,
223 WapiSecurityAttributes *process_attrs G_GNUC_UNUSED,
224 WapiSecurityAttributes *thread_attrs G_GNUC_UNUSED,
225 gboolean inherit_handles, guint32 create_flags,
226 gpointer new_environ, const gunichar2 *cwd,
227 WapiStartupInfo *startup,
228 WapiProcessInformation *process_info)
230 gchar *cmd=NULL, *prog = NULL, *full_prog = NULL, *args = NULL, *args_after_prog = NULL, *dir = NULL, **env_strings = NULL, **argv;
231 guint32 i, env_count = 0;
232 gboolean ret = FALSE;
234 struct _WapiHandleShared shared_handle;
235 struct _WapiHandle_process process_handle = {0};
237 int in_fd, out_fd, err_fd;
241 mono_once (&process_ops_once, process_ops_init);
243 /* appname and cmdline specify the executable and its args:
245 * If appname is not NULL, it is the name of the executable.
246 * Otherwise the executable is the first token in cmdline.
248 * Executable searching:
250 * If appname is not NULL, it can specify the full path and
251 * file name, or else a partial name and the current directory
252 * will be used. There is no additional searching.
254 * If appname is NULL, the first whitespace-delimited token in
255 * cmdline is used. If the name does not contain a full
256 * directory path, the search sequence is:
258 * 1) The directory containing the current process
259 * 2) The current working directory
260 * 3) The windows system directory (Ignored)
261 * 4) The windows directory (Ignored)
264 * Just to make things more interesting, tokens can contain
265 * white space if they are surrounded by quotation marks. I'm
266 * beginning to understand just why windows apps are generally
267 * so crap, with an API like this :-(
269 if (appname != NULL) {
270 cmd = mono_unicode_to_external (appname);
273 g_message ("%s: unicode conversion returned NULL",
277 SetLastError (ERROR_PATH_NOT_FOUND);
281 /* Turn all the slashes round the right way */
282 for (i = 0; i < strlen (cmd); i++) {
283 if (cmd[i] == '\\') {
289 if (cmdline != NULL) {
290 args = mono_unicode_to_external (cmdline);
293 g_message ("%s: unicode conversion returned NULL", __func__);
296 SetLastError (ERROR_PATH_NOT_FOUND);
302 dir = mono_unicode_to_external (cwd);
305 g_message ("%s: unicode conversion returned NULL", __func__);
308 SetLastError (ERROR_PATH_NOT_FOUND);
312 /* Turn all the slashes round the right way */
313 for (i = 0; i < strlen (dir); i++) {
314 if (dir[i] == '\\') {
319 dir = g_get_current_dir ();
323 /* We can't put off locating the executable any longer :-( */
326 if (g_ascii_isalpha (cmd[0]) && (cmd[1] == ':')) {
327 /* Strip off the drive letter. I can't
328 * believe that CP/M holdover is still
331 g_memmove (cmd, cmd+2, strlen (cmd)-2);
332 cmd[strlen (cmd)-2] = '\0';
335 unquoted = g_shell_unquote (cmd, NULL);
336 if (unquoted[0] == '/') {
337 /* Assume full path given */
338 prog = g_strdup (unquoted);
340 /* Executable existing ? */
341 if (access (prog, X_OK) != 0) {
343 g_message ("%s: Couldn't find executable %s",
348 SetLastError (ERROR_FILE_NOT_FOUND);
352 /* Search for file named by cmd in the current
355 char *curdir = g_get_current_dir ();
357 prog = g_strdup_printf ("%s/%s", curdir, unquoted);
362 args_after_prog = args;
367 /* Dig out the first token from args, taking quotation
371 /* First, strip off all leading whitespace */
372 args = g_strchug (args);
374 /* args_after_prog points to the contents of args
375 * after token has been set (otherwise argv[0] is
378 args_after_prog = args;
380 /* Assume the opening quote will always be the first
383 if (args[0] == '\"' || args [0] == '\'') {
385 for (i = 1; args[i] != '\0' && args[i] != quote; i++);
386 if (g_ascii_isspace (args[i+1])) {
387 /* We found the first token */
388 token = g_strndup (args+1, i-1);
389 args_after_prog = args + i;
391 /* Quotation mark appeared in the
392 * middle of the token. Just give the
393 * whole first token, quotes and all,
400 /* No quote mark, or malformed */
401 for (i = 0; args[i] != '\0'; i++) {
402 if (g_ascii_isspace (args[i])) {
403 token = g_strndup (args, i);
404 args_after_prog = args + i + 1;
410 if (token == NULL && args[0] != '\0') {
411 /* Must be just one token in the string */
412 token = g_strdup (args);
413 args_after_prog = NULL;
419 g_message ("%s: Couldn't find what to exec", __func__);
422 SetLastError (ERROR_PATH_NOT_FOUND);
426 /* Turn all the slashes round the right way. Only for
429 for (i = 0; i < strlen (token); i++) {
430 if (token[i] == '\\') {
435 if (g_ascii_isalpha (token[0]) && (token[1] == ':')) {
436 /* Strip off the drive letter. I can't
437 * believe that CP/M holdover is still
440 g_memmove (token, token+2, strlen (token)-2);
441 token[strlen (token)-2] = '\0';
444 if (token[0] == '/') {
445 /* Assume full path given */
446 prog = g_strdup (token);
448 /* Executable existing ? */
449 if (access (prog, X_OK) != 0) {
452 g_message ("%s: Couldn't find executable %s",
456 SetLastError (ERROR_FILE_NOT_FOUND);
461 char *curdir = g_get_current_dir ();
463 /* FIXME: Need to record the directory
464 * containing the current process, and check
465 * that for the new executable as the first
469 prog = g_strdup_printf ("%s/%s", curdir, token);
472 /* I assume X_OK is the criterion to use,
475 if (access (prog, X_OK) != 0) {
477 prog = g_find_program_in_path (token);
480 g_message ("%s: Couldn't find executable %s", __func__, token);
484 SetLastError (ERROR_FILE_NOT_FOUND);
494 g_message ("%s: Exec prog [%s] args [%s]", __func__, prog,
498 if (args_after_prog != NULL && *args_after_prog) {
501 qprog = g_shell_quote (prog);
502 full_prog = g_strconcat (qprog, " ", args_after_prog, NULL);
505 full_prog = g_shell_quote (prog);
508 ret = g_shell_parse_argv (full_prog, NULL, &argv, &gerr);
510 /* FIXME: Could do something with the GError here
514 if (startup != NULL && startup->dwFlags & STARTF_USESTDHANDLES) {
515 in_fd = GPOINTER_TO_UINT (startup->hStdInput);
516 out_fd = GPOINTER_TO_UINT (startup->hStdOutput);
517 err_fd = GPOINTER_TO_UINT (startup->hStdError);
519 in_fd = GPOINTER_TO_UINT (GetStdHandle (STD_INPUT_HANDLE));
520 out_fd = GPOINTER_TO_UINT (GetStdHandle (STD_OUTPUT_HANDLE));
521 err_fd = GPOINTER_TO_UINT (GetStdHandle (STD_ERROR_HANDLE));
524 g_strlcpy (process_handle.proc_name, prog, _POSIX_PATH_MAX-1);
526 process_set_defaults (&process_handle);
528 handle = _wapi_handle_new (WAPI_HANDLE_PROCESS, &process_handle);
529 if (handle == _WAPI_HANDLE_INVALID) {
530 g_warning ("%s: error creating process handle", __func__);
532 SetLastError (ERROR_PATH_NOT_FOUND);
536 /* new_environ is a block of NULL-terminated strings, which
537 * is itself NULL-terminated. Of course, passing an array of
538 * string pointers would have made things too easy :-(
540 * If new_environ is not NULL it specifies the entire set of
541 * environment variables in the new process. Otherwise the
542 * new process inherits the same environment.
544 if (new_environ != NULL) {
545 gunichar2 *new_environp;
547 /* Count the number of strings */
548 for (new_environp = (gunichar2 *)new_environ; *new_environp;
551 while (*new_environp) {
556 /* +2: one for the process handle value, and the last
559 env_strings = g_new0 (gchar *, env_count + 2);
561 /* Copy each environ string into 'strings' turning it
562 * into utf8 (or the requested encoding) at the same
566 for (new_environp = (gunichar2 *)new_environ; *new_environp;
568 env_strings[env_count] = mono_unicode_to_external (new_environp);
570 while (*new_environp) {
575 for (i = 0; environ[i] != NULL; i++) {
579 /* +2: one for the process handle value, and the last
582 env_strings = g_new0 (gchar *, env_count + 2);
584 /* Copy each environ string into 'strings' turning it
585 * into utf8 (or the requested encoding) at the same
589 for (i = 0; environ[i] != NULL; i++) {
590 env_strings[env_count] = g_strdup (environ[i]);
594 /* pass process handle info to the child, so it doesn't have
595 * to do an expensive search over the whole list
597 if (env_strings != NULL) {
598 struct _WapiHandleUnshared *handle_data;
599 struct _WapiHandle_shared_ref *ref;
601 handle_data = &_WAPI_PRIVATE_HANDLES(GPOINTER_TO_UINT(handle));
602 ref = &handle_data->u.shared;
604 env_strings[env_count] = g_strdup_printf ("_WAPI_PROCESS_HANDLE_OFFSET=%d", ref->offset);
612 SetLastError (ERROR_OUTOFMEMORY);
613 _wapi_handle_unref (handle);
615 } else if (pid == 0) {
619 /* Wait for the parent to finish setting up the handle */
620 close (lockpipe [1]);
621 while (read (lockpipe [0], c, 1) == -1 &&
623 close (lockpipe [0]);
625 /* should we detach from the process group? */
627 /* Connect stdin, stdout and stderr */
632 if (inherit_handles != TRUE) {
633 /* FIXME: do something here */
636 /* Close all file descriptors */
637 for (i = getdtablesize () - 1; i > 2; i--) {
642 g_message ("%s: exec()ing [%s] in dir [%s]", __func__, cmd,
644 for (i = 0; argv[i] != NULL; i++) {
645 g_message ("arg %d: [%s]", i, argv[i]);
648 for (i = 0; env_strings[i] != NULL; i++) {
649 g_message ("env %d: [%s]", i, env_strings[i]);
654 if (chdir (dir) == -1) {
660 execve (argv[0], argv, env_strings);
667 ret = _wapi_copy_handle (handle, WAPI_HANDLE_PROCESS, &shared_handle);
669 g_warning ("%s: error copying process handle %p", __func__,
671 _wapi_handle_unref (handle);
675 shared_handle.u.process.id = pid;
677 ret = _wapi_replace_handle (handle, WAPI_HANDLE_PROCESS,
680 SetLastError (ERROR_OUTOFMEMORY);
681 _wapi_handle_unref (handle);
685 write (lockpipe [1], cmd, 1);
687 if (process_info != NULL) {
688 process_info->hProcess = handle;
689 process_info->dwProcessId = pid;
691 /* FIXME: we might need to handle the thread info some
694 process_info->hThread = NULL;
695 process_info->dwThreadId = 0;
699 close (lockpipe [0]);
700 close (lockpipe [1]);
705 if (full_prog != NULL) {
714 if(env_strings != NULL) {
715 g_strfreev (env_strings);
721 static void process_set_name (struct _WapiHandle_process *process_handle)
723 gchar *progname, *utf8_progname, *slash;
725 progname=g_get_prgname ();
726 utf8_progname=mono_utf8_from_external (progname);
729 g_message ("%s: using [%s] as prog name", __func__, progname);
732 if(utf8_progname!=NULL) {
733 slash=strrchr (utf8_progname, '/');
735 g_strlcpy (process_handle->proc_name, slash+1,
738 g_strlcpy (process_handle->proc_name, utf8_progname,
742 g_free (utf8_progname);
746 extern void _wapi_time_t_to_filetime (time_t timeval, WapiFileTime *filetime);
748 static void process_set_current (void)
750 pid_t pid = getpid ();
753 handle_env = getenv ("_WAPI_PROCESS_HANDLE_OFFSET");
754 if (handle_env == NULL) {
755 struct _WapiHandle_process process_handle = {0};
758 g_message ("%s: Need to create my own process handle",
762 process_handle.id = pid;
764 process_set_defaults (&process_handle);
765 process_set_name (&process_handle);
767 current_process = _wapi_handle_new (WAPI_HANDLE_PROCESS,
769 if (current_process == _WAPI_HANDLE_INVALID) {
770 g_warning ("%s: error creating process handle",
775 /* Make sure the new handle has a reference so it wont go away
776 * until this process exits
778 _wapi_handle_ref (current_process);
780 struct _WapiHandle_process *process_handle;
781 guchar *procname = NULL;
784 current_process = _wapi_handle_new_from_offset (WAPI_HANDLE_PROCESS, atoi (handle_env));
787 g_message ("%s: Found my process handle: %p (offset %d)",
788 __func__, current_process, atoi (handle_env));
791 ok = _wapi_lookup_handle (current_process, WAPI_HANDLE_PROCESS,
792 (gpointer *)&process_handle);
794 g_warning ("%s: error looking up process handle %p",
795 __func__, current_process);
799 procname = process_handle->proc_name;
800 if (!strcmp (procname, "mono")) {
801 /* Set a better process name */
803 g_message ("%s: Setting better process name",
807 process_set_name (process_handle);
810 g_message ("%s: Leaving process name: %s", __func__,
817 /* Returns a pseudo handle that doesn't need to be closed afterwards */
818 gpointer GetCurrentProcess (void)
820 mono_once (&process_current_once, process_set_current);
822 return((gpointer)-1);
825 guint32 GetCurrentProcessId (void)
827 struct _WapiHandle_process *current_process_handle;
830 mono_once (&process_current_once, process_set_current);
832 ok=_wapi_lookup_handle (current_process, WAPI_HANDLE_PROCESS,
833 (gpointer *)¤t_process_handle);
835 g_warning ("%s: error looking up current process handle %p",
836 __func__, current_process);
837 /* No failure return is defined. PID 0 is invalid.
838 * This should only be reached when something else has
839 * gone badly wrong anyway.
844 return(current_process_handle->id);
847 /* Returns the process id as a convenience to the functions that call this */
848 static pid_t signal_process_if_gone (gpointer handle)
850 struct _WapiHandle_process *process_handle;
853 /* Make sure the process is signalled if it has exited - if
854 * the parent process didn't wait for it then it won't be
856 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_PROCESS,
857 (gpointer *)&process_handle);
859 g_warning ("%s: error looking up process handle %p",
866 g_message ("%s: looking at process %d", __func__, process_handle->id);
869 if (kill (process_handle->id, 0) == -1 &&
872 /* The process is dead, (EPERM tells us a new process
873 * has that ID, but as it's owned by someone else it
874 * can't be the one listed in our shared memory file)
876 _wapi_shared_handle_set_signal_state (handle, TRUE);
879 return (process_handle->id);
882 static gboolean process_enum (gpointer handle, gpointer user_data)
884 GArray *processes=user_data;
885 pid_t pid = signal_process_if_gone (handle);
892 /* Ignore processes that have already exited (ie they are signalled) */
893 if (_wapi_handle_issignalled (handle) == FALSE) {
895 g_message ("%s: process %d added to array", __func__, pid);
898 /* This ensures that duplicates aren't returned (see
899 * the comment above _wapi_search_handle () for why
902 for (i = 0; i < processes->len; i++) {
903 if (g_array_index (processes, pid_t, i) == pid) {
904 /* We've already got this one, return
905 * FALSE to keep searching
911 g_array_append_val (processes, pid);
914 /* Return false to keep searching */
918 gboolean EnumProcesses (guint32 *pids, guint32 len, guint32 *needed)
920 GArray *processes = g_array_new (FALSE, FALSE, sizeof(pid_t));
923 mono_once (&process_current_once, process_set_current);
925 _wapi_search_handle (WAPI_HANDLE_PROCESS, process_enum, processes,
928 fit=len/sizeof(guint32);
929 for (i = 0, j = 0; j < fit && i < processes->len; i++) {
930 pids[j++] = g_array_index (processes, pid_t, i);
933 g_array_free (processes, TRUE);
935 *needed = j * sizeof(guint32);
940 static gboolean process_open_compare (gpointer handle, gpointer user_data)
943 pid_t checking_pid = signal_process_if_gone (handle);
945 if (checking_pid == 0) {
949 wanted_pid = GPOINTER_TO_UINT (user_data);
951 /* It's possible to have more than one process handle with the
952 * same pid, but only the one running process can be
955 if (checking_pid == wanted_pid &&
956 _wapi_handle_issignalled (handle) == FALSE) {
963 gpointer OpenProcess (guint32 access G_GNUC_UNUSED, gboolean inherit G_GNUC_UNUSED, guint32 pid)
965 /* Find the process handle that corresponds to pid */
968 mono_once (&process_current_once, process_set_current);
971 g_message ("%s: looking for process %d", __func__, pid);
974 handle = _wapi_search_handle (WAPI_HANDLE_PROCESS,
975 process_open_compare,
976 GUINT_TO_POINTER (pid), NULL);
979 g_message ("%s: Can't find pid %d", __func__, pid);
982 SetLastError (ERROR_PROC_NOT_FOUND);
987 _wapi_handle_ref (handle);
992 gboolean GetExitCodeProcess (gpointer process, guint32 *code)
994 struct _WapiHandle_process *process_handle;
997 mono_once (&process_current_once, process_set_current);
1003 ok=_wapi_lookup_handle (process, WAPI_HANDLE_PROCESS,
1004 (gpointer *)&process_handle);
1007 g_message ("%s: Can't find process %p", __func__, process);
1013 /* A process handle is only signalled if the process has exited
1014 * and has been waited for */
1015 if (_wapi_handle_issignalled (process) == TRUE ||
1016 process_wait (process, 0) == WAIT_OBJECT_0) {
1017 *code = process_handle->exitstatus;
1019 *code = STILL_ACTIVE;
1025 gboolean GetProcessTimes (gpointer process, WapiFileTime *create_time,
1026 WapiFileTime *exit_time, WapiFileTime *kernel_time,
1027 WapiFileTime *user_time)
1029 struct _WapiHandle_process *process_handle;
1032 mono_once (&process_current_once, process_set_current);
1034 if(create_time==NULL || exit_time==NULL || kernel_time==NULL ||
1036 /* Not sure if w32 allows NULLs here or not */
1040 ok=_wapi_lookup_handle (process, WAPI_HANDLE_PROCESS,
1041 (gpointer *)&process_handle);
1044 g_message ("%s: Can't find process %p", __func__, process);
1050 *create_time=process_handle->create_time;
1052 /* A process handle is only signalled if the process has
1053 * exited. Otherwise exit_time isn't set
1055 if(_wapi_handle_issignalled (process)==TRUE) {
1056 *exit_time=process_handle->exit_time;
1062 gboolean EnumProcessModules (gpointer process, gpointer *modules,
1063 guint32 size, guint32 *needed)
1065 /* Store modules in an array of pointers (main module as
1066 * modules[0]), using the load address for each module as a
1067 * token. (Use 'NULL' as an alternative for the main module
1068 * so that the simple implementation can just return one item
1069 * for now.) Get the info from /proc/<pid>/maps on linux,
1070 * other systems will have to implement /dev/kmem reading or
1071 * whatever other horrid technique is needed.
1073 if(size<sizeof(gpointer)) {
1079 *needed=sizeof(gpointer);
1082 *needed=sizeof(gpointer);
1088 guint32 GetModuleBaseName (gpointer process, gpointer module,
1089 gunichar2 *basename, guint32 size)
1091 struct _WapiHandle_process *process_handle;
1094 mono_once (&process_current_once, process_set_current);
1097 g_message ("%s: Getting module base name, process handle %p module %p",
1098 __func__, process, module);
1101 if(basename==NULL || size==0) {
1105 ok=_wapi_lookup_handle (process, WAPI_HANDLE_PROCESS,
1106 (gpointer *)&process_handle);
1109 g_message ("%s: Can't find process %p", __func__, process);
1116 /* Shorthand for the main module, which has the
1117 * process name recorded in the handle data
1120 gunichar2 *procname;
1121 guchar *procname_utf8 = NULL;
1125 g_message ("%s: Returning main module name", __func__);
1128 pid=process_handle->id;
1129 procname_utf8 = process_handle->proc_name;
1132 g_message ("%s: Process name is [%s]", __func__,
1136 procname = g_utf8_to_utf16 (procname_utf8, -1, NULL, &len,
1138 if (procname == NULL) {
1143 /* Add the terminator, and convert chars to bytes */
1144 bytes = (len + 1) * 2;
1148 g_message ("%s: Size %d smaller than needed (%ld); truncating", __func__, size, bytes);
1151 memcpy (basename, procname, size);
1154 g_message ("%s: Size %d larger than needed (%ld)",
1155 __func__, size, bytes);
1158 memcpy (basename, procname, bytes);
1165 /* Look up the address in /proc/<pid>/maps */
1171 gboolean GetProcessWorkingSetSize (gpointer process, size_t *min, size_t *max)
1173 struct _WapiHandle_process *process_handle;
1176 mono_once (&process_current_once, process_set_current);
1178 if(min==NULL || max==NULL) {
1179 /* Not sure if w32 allows NULLs here or not */
1183 ok=_wapi_lookup_handle (process, WAPI_HANDLE_PROCESS,
1184 (gpointer *)&process_handle);
1187 g_message ("%s: Can't find process %p", __func__, process);
1193 *min=process_handle->min_working_set;
1194 *max=process_handle->max_working_set;
1199 gboolean SetProcessWorkingSetSize (gpointer process, size_t min, size_t max)
1201 struct _WapiHandle_process *process_handle;
1204 mono_once (&process_current_once, process_set_current);
1206 ok=_wapi_lookup_handle (process, WAPI_HANDLE_PROCESS,
1207 (gpointer *)&process_handle);
1210 g_message ("%s: Can't find process %p", __func__, process);
1216 process_handle->min_working_set=min;
1217 process_handle->max_working_set=max;
1224 TerminateProcess (gpointer process, gint32 exitCode)
1226 struct _WapiHandle_process *process_handle;
1231 ok = _wapi_lookup_handle (process, WAPI_HANDLE_PROCESS,
1232 (gpointer *) &process_handle);
1236 g_message ("%s: Can't find process %p", __func__, process);
1238 SetLastError (ERROR_INVALID_HANDLE);
1242 signo = (exitCode == -1) ? SIGKILL : SIGTERM;
1243 ret = kill (process_handle->id, signo);
1247 SetLastError (ERROR_INVALID_PARAMETER);
1250 SetLastError (ERROR_ACCESS_DENIED);
1253 SetLastError (ERROR_PROC_NOT_FOUND);
1256 SetLastError (ERROR_GEN_FAILURE);