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 _WapiHandle_process *process_handle;
65 thr_ret = _wapi_handle_lock_shared_handles ();
66 g_assert (thr_ret == 0);
68 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_PROCESS,
69 (gpointer *)&process_handle);
71 g_warning ("%s: error looking up process handle %p",
76 if (WIFSIGNALED(status)) {
77 process_handle->exitstatus = 128 + WTERMSIG(status);
79 process_handle->exitstatus = WEXITSTATUS(status);
81 _wapi_time_t_to_filetime (time(NULL), &process_handle->exit_time);
83 _wapi_shared_handle_set_signal_state (handle, TRUE);
85 _wapi_handle_unlock_shared_handles ();
90 /* See if any child processes have terminated and wait() for them,
91 * updating process handle info. This function is called from the
92 * collection thread every few seconds.
94 static gboolean waitfor_pid (gpointer test, gpointer user_data G_GNUC_UNUSED)
96 struct _WapiHandle_process *process;
101 if (_wapi_handle_issignalled (test)) {
102 /* We've already done this one */
106 ok = _wapi_lookup_handle (test, WAPI_HANDLE_PROCESS,
107 (gpointer *)&process);
113 ret = waitpid (process->id, &status, WNOHANG);
114 } while (errno == EINTR);
117 /* Process not ready for wait */
119 g_message ("%s: Process %d not ready for waiting for: %s",
120 __func__, process->id, g_strerror (errno));
127 g_message ("%s: Process %d finished", __func__, ret);
130 process_set_termination_details (test, status);
132 /* return FALSE to keep searching */
136 void _wapi_process_reap (void)
139 g_message ("%s: Reaping child processes", __func__);
142 _wapi_search_handle (WAPI_HANDLE_PROCESS, waitfor_pid, NULL, NULL);
145 /* Limitations: This can only wait for processes that are our own
146 * children. Fixing this means resurrecting a daemon helper to manage
149 static guint32 process_wait (gpointer handle, guint32 timeout)
151 struct _WapiHandle_process *process_handle;
157 g_message ("%s: Waiting for process %p", __func__, handle);
160 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_PROCESS,
161 (gpointer *)&process_handle);
163 g_warning ("%s: error looking up process handle %p", __func__,
168 pid = process_handle->id;
171 g_message ("%s: PID is %d", __func__, pid);
174 if (timeout == INFINITE) {
175 while ((ret = waitpid (pid, &status, 0)) != pid) {
176 if (ret == (pid_t)-1 && errno != EINTR) {
180 } else if (timeout == 0) {
182 ret = waitpid (pid, &status, WNOHANG);
184 return (WAIT_TIMEOUT);
189 ret = waitpid (pid, &status, WNOHANG);
192 } else if (ret == (pid_t)-1 && errno != EINTR) {
196 _wapi_handle_spin (100);
198 } while (timeout > 0);
201 return(WAIT_TIMEOUT);
205 /* Process must have exited */
207 g_message ("%s: Wait done", __func__);
210 ok = process_set_termination_details (handle, status);
212 SetLastError (ERROR_OUTOFMEMORY);
213 return (WAIT_FAILED);
216 return(WAIT_OBJECT_0);
219 static void process_set_defaults (struct _WapiHandle_process *process_handle)
221 /* These seem to be the defaults on w2k */
222 process_handle->min_working_set = 204800;
223 process_handle->max_working_set = 1413120;
225 _wapi_time_t_to_filetime (time (NULL), &process_handle->create_time);
228 gboolean CreateProcess (const gunichar2 *appname, gunichar2 *cmdline,
229 WapiSecurityAttributes *process_attrs G_GNUC_UNUSED,
230 WapiSecurityAttributes *thread_attrs G_GNUC_UNUSED,
231 gboolean inherit_handles, guint32 create_flags,
232 gpointer new_environ, const gunichar2 *cwd,
233 WapiStartupInfo *startup,
234 WapiProcessInformation *process_info)
236 gchar *cmd=NULL, *prog = NULL, *full_prog = NULL, *args = NULL, *args_after_prog = NULL, *dir = NULL, **env_strings = NULL, **argv;
237 guint32 i, env_count = 0;
238 gboolean ret = FALSE;
240 struct _WapiHandle_process process_handle = {0}, *process_handle_data;
242 int in_fd, out_fd, err_fd;
247 mono_once (&process_ops_once, process_ops_init);
249 /* appname and cmdline specify the executable and its args:
251 * If appname is not NULL, it is the name of the executable.
252 * Otherwise the executable is the first token in cmdline.
254 * Executable searching:
256 * If appname is not NULL, it can specify the full path and
257 * file name, or else a partial name and the current directory
258 * will be used. There is no additional searching.
260 * If appname is NULL, the first whitespace-delimited token in
261 * cmdline is used. If the name does not contain a full
262 * directory path, the search sequence is:
264 * 1) The directory containing the current process
265 * 2) The current working directory
266 * 3) The windows system directory (Ignored)
267 * 4) The windows directory (Ignored)
270 * Just to make things more interesting, tokens can contain
271 * white space if they are surrounded by quotation marks. I'm
272 * beginning to understand just why windows apps are generally
273 * so crap, with an API like this :-(
275 if (appname != NULL) {
276 cmd = mono_unicode_to_external (appname);
279 g_message ("%s: unicode conversion returned NULL",
283 SetLastError (ERROR_PATH_NOT_FOUND);
287 /* Turn all the slashes round the right way */
288 for (i = 0; i < strlen (cmd); i++) {
289 if (cmd[i] == '\\') {
295 if (cmdline != NULL) {
296 args = mono_unicode_to_external (cmdline);
299 g_message ("%s: unicode conversion returned NULL", __func__);
302 SetLastError (ERROR_PATH_NOT_FOUND);
308 dir = mono_unicode_to_external (cwd);
311 g_message ("%s: unicode conversion returned NULL", __func__);
314 SetLastError (ERROR_PATH_NOT_FOUND);
318 /* Turn all the slashes round the right way */
319 for (i = 0; i < strlen (dir); i++) {
320 if (dir[i] == '\\') {
325 dir = g_get_current_dir ();
329 /* We can't put off locating the executable any longer :-( */
332 if (g_ascii_isalpha (cmd[0]) && (cmd[1] == ':')) {
333 /* Strip off the drive letter. I can't
334 * believe that CP/M holdover is still
337 g_memmove (cmd, cmd+2, strlen (cmd)-2);
338 cmd[strlen (cmd)-2] = '\0';
341 unquoted = g_shell_unquote (cmd, NULL);
342 if (unquoted[0] == '/') {
343 /* Assume full path given */
344 prog = g_strdup (unquoted);
346 /* Executable existing ? */
347 if (access (prog, X_OK) != 0) {
349 g_message ("%s: Couldn't find executable %s",
354 SetLastError (ERROR_FILE_NOT_FOUND);
358 /* Search for file named by cmd in the current
361 char *curdir = g_get_current_dir ();
363 prog = g_strdup_printf ("%s/%s", curdir, unquoted);
368 args_after_prog = args;
373 /* Dig out the first token from args, taking quotation
377 /* First, strip off all leading whitespace */
378 args = g_strchug (args);
380 /* args_after_prog points to the contents of args
381 * after token has been set (otherwise argv[0] is
384 args_after_prog = args;
386 /* Assume the opening quote will always be the first
389 if (args[0] == '\"' || args [0] == '\'') {
391 for (i = 1; args[i] != '\0' && args[i] != quote; i++);
392 if (g_ascii_isspace (args[i+1])) {
393 /* We found the first token */
394 token = g_strndup (args+1, i-1);
395 args_after_prog = args + i;
397 /* Quotation mark appeared in the
398 * middle of the token. Just give the
399 * whole first token, quotes and all,
406 /* No quote mark, or malformed */
407 for (i = 0; args[i] != '\0'; i++) {
408 if (g_ascii_isspace (args[i])) {
409 token = g_strndup (args, i);
410 args_after_prog = args + i + 1;
416 if (token == NULL && args[0] != '\0') {
417 /* Must be just one token in the string */
418 token = g_strdup (args);
419 args_after_prog = NULL;
425 g_message ("%s: Couldn't find what to exec", __func__);
428 SetLastError (ERROR_PATH_NOT_FOUND);
432 /* Turn all the slashes round the right way. Only for
435 for (i = 0; i < strlen (token); i++) {
436 if (token[i] == '\\') {
441 if (g_ascii_isalpha (token[0]) && (token[1] == ':')) {
442 /* Strip off the drive letter. I can't
443 * believe that CP/M holdover is still
446 g_memmove (token, token+2, strlen (token)-2);
447 token[strlen (token)-2] = '\0';
450 if (token[0] == '/') {
451 /* Assume full path given */
452 prog = g_strdup (token);
454 /* Executable existing ? */
455 if (access (prog, X_OK) != 0) {
458 g_message ("%s: Couldn't find executable %s",
462 SetLastError (ERROR_FILE_NOT_FOUND);
467 char *curdir = g_get_current_dir ();
469 /* FIXME: Need to record the directory
470 * containing the current process, and check
471 * that for the new executable as the first
475 prog = g_strdup_printf ("%s/%s", curdir, token);
478 /* I assume X_OK is the criterion to use,
481 if (access (prog, X_OK) != 0) {
483 prog = g_find_program_in_path (token);
486 g_message ("%s: Couldn't find executable %s", __func__, token);
490 SetLastError (ERROR_FILE_NOT_FOUND);
500 g_message ("%s: Exec prog [%s] args [%s]", __func__, prog,
504 if (args_after_prog != NULL && *args_after_prog) {
507 qprog = g_shell_quote (prog);
508 full_prog = g_strconcat (qprog, " ", args_after_prog, NULL);
511 full_prog = g_shell_quote (prog);
514 ret = g_shell_parse_argv (full_prog, NULL, &argv, &gerr);
516 /* FIXME: Could do something with the GError here
520 if (startup != NULL && startup->dwFlags & STARTF_USESTDHANDLES) {
521 in_fd = GPOINTER_TO_UINT (startup->hStdInput);
522 out_fd = GPOINTER_TO_UINT (startup->hStdOutput);
523 err_fd = GPOINTER_TO_UINT (startup->hStdError);
525 in_fd = GPOINTER_TO_UINT (GetStdHandle (STD_INPUT_HANDLE));
526 out_fd = GPOINTER_TO_UINT (GetStdHandle (STD_OUTPUT_HANDLE));
527 err_fd = GPOINTER_TO_UINT (GetStdHandle (STD_ERROR_HANDLE));
530 g_strlcpy (process_handle.proc_name, prog,
531 _WAPI_PROC_NAME_MAX_LEN - 1);
533 process_set_defaults (&process_handle);
535 handle = _wapi_handle_new (WAPI_HANDLE_PROCESS, &process_handle);
536 if (handle == _WAPI_HANDLE_INVALID) {
537 g_warning ("%s: error creating process handle", __func__);
539 SetLastError (ERROR_PATH_NOT_FOUND);
543 /* new_environ is a block of NULL-terminated strings, which
544 * is itself NULL-terminated. Of course, passing an array of
545 * string pointers would have made things too easy :-(
547 * If new_environ is not NULL it specifies the entire set of
548 * environment variables in the new process. Otherwise the
549 * new process inherits the same environment.
551 if (new_environ != NULL) {
552 gunichar2 *new_environp;
554 /* Count the number of strings */
555 for (new_environp = (gunichar2 *)new_environ; *new_environp;
558 while (*new_environp) {
563 /* +2: one for the process handle value, and the last
566 env_strings = g_new0 (gchar *, env_count + 2);
568 /* Copy each environ string into 'strings' turning it
569 * into utf8 (or the requested encoding) at the same
573 for (new_environp = (gunichar2 *)new_environ; *new_environp;
575 env_strings[env_count] = mono_unicode_to_external (new_environp);
577 while (*new_environp) {
582 for (i = 0; environ[i] != NULL; i++) {
586 /* +2: one for the process handle value, and the last
589 env_strings = g_new0 (gchar *, env_count + 2);
591 /* Copy each environ string into 'strings' turning it
592 * into utf8 (or the requested encoding) at the same
596 for (i = 0; environ[i] != NULL; i++) {
597 env_strings[env_count] = g_strdup (environ[i]);
601 /* pass process handle info to the child, so it doesn't have
602 * to do an expensive search over the whole list
604 if (env_strings != NULL) {
605 struct _WapiHandleUnshared *handle_data;
606 struct _WapiHandle_shared_ref *ref;
608 handle_data = &_WAPI_PRIVATE_HANDLES(GPOINTER_TO_UINT(handle));
609 ref = &handle_data->u.shared;
611 env_strings[env_count] = g_strdup_printf ("_WAPI_PROCESS_HANDLE_OFFSET=%d", ref->offset);
619 SetLastError (ERROR_OUTOFMEMORY);
620 _wapi_handle_unref (handle);
622 } else if (pid == 0) {
626 /* Wait for the parent to finish setting up the handle */
627 close (lockpipe [1]);
628 while (read (lockpipe [0], c, 1) == -1 &&
630 close (lockpipe [0]);
632 /* should we detach from the process group? */
634 /* Connect stdin, stdout and stderr */
639 if (inherit_handles != TRUE) {
640 /* FIXME: do something here */
643 /* Close all file descriptors */
644 for (i = getdtablesize () - 1; i > 2; i--) {
649 g_message ("%s: exec()ing [%s] in dir [%s]", __func__, cmd,
651 for (i = 0; argv[i] != NULL; i++) {
652 g_message ("arg %d: [%s]", i, argv[i]);
655 for (i = 0; env_strings[i] != NULL; i++) {
656 g_message ("env %d: [%s]", i, env_strings[i]);
661 if (chdir (dir) == -1) {
667 execve (argv[0], argv, env_strings);
674 thr_ret = _wapi_handle_lock_shared_handles ();
675 g_assert (thr_ret == 0);
677 ret = _wapi_lookup_handle (handle, WAPI_HANDLE_PROCESS,
678 (gpointer *)&process_handle_data);
680 g_warning ("%s: error looking up process handle %p", __func__,
682 _wapi_handle_unref (handle);
686 process_handle_data->id = pid;
688 _wapi_handle_unlock_shared_handles ();
690 write (lockpipe [1], cmd, 1);
692 if (process_info != NULL) {
693 process_info->hProcess = handle;
694 process_info->dwProcessId = pid;
696 /* FIXME: we might need to handle the thread info some
699 process_info->hThread = NULL;
700 process_info->dwThreadId = 0;
704 close (lockpipe [0]);
705 close (lockpipe [1]);
710 if (full_prog != NULL) {
719 if(env_strings != NULL) {
720 g_strfreev (env_strings);
726 static void process_set_name (struct _WapiHandle_process *process_handle)
728 gchar *progname, *utf8_progname, *slash;
730 progname=g_get_prgname ();
731 utf8_progname=mono_utf8_from_external (progname);
734 g_message ("%s: using [%s] as prog name", __func__, progname);
737 if(utf8_progname!=NULL) {
738 slash=strrchr (utf8_progname, '/');
740 g_strlcpy (process_handle->proc_name, slash+1,
741 _WAPI_PROC_NAME_MAX_LEN - 1);
743 g_strlcpy (process_handle->proc_name, utf8_progname,
744 _WAPI_PROC_NAME_MAX_LEN - 1);
747 g_free (utf8_progname);
751 extern void _wapi_time_t_to_filetime (time_t timeval, WapiFileTime *filetime);
753 static void process_set_current (void)
755 pid_t pid = getpid ();
757 struct _WapiHandle_process process_handle = {0};
759 handle_env = getenv ("_WAPI_PROCESS_HANDLE_OFFSET");
760 if (handle_env != NULL) {
761 struct _WapiHandle_process *process_handlep;
762 guchar *procname = NULL;
765 current_process = _wapi_handle_new_from_offset (WAPI_HANDLE_PROCESS, atoi (handle_env), TRUE);
768 g_message ("%s: Found my process handle: %p (offset %d)",
769 __func__, current_process, atoi (handle_env));
772 ok = _wapi_lookup_handle (current_process, WAPI_HANDLE_PROCESS,
773 (gpointer *)&process_handlep);
775 g_warning ("%s: error looking up process handle %p",
776 __func__, current_process);
780 /* This test will break on linuxthreads, but that
781 * should be ancient history on all distros we care
784 if (process_handlep->id == pid) {
785 procname = process_handlep->proc_name;
786 if (!strcmp (procname, "mono")) {
787 /* Set a better process name */
789 g_message ("%s: Setting better process name",
793 process_set_name (process_handlep);
796 g_message ("%s: Leaving process name: %s",
804 /* Wrong pid, so drop this handle and fall through to
807 _wapi_handle_unref (current_process);
811 g_message ("%s: Need to create my own process handle", __func__);
814 process_handle.id = pid;
816 process_set_defaults (&process_handle);
817 process_set_name (&process_handle);
819 current_process = _wapi_handle_new (WAPI_HANDLE_PROCESS,
821 if (current_process == _WAPI_HANDLE_INVALID) {
822 g_warning ("%s: error creating process handle", __func__);
826 /* Make sure the new handle has a reference so it wont go away
827 * until this process exits
829 _wapi_handle_ref (current_process);
832 /* Returns a pseudo handle that doesn't need to be closed afterwards */
833 gpointer GetCurrentProcess (void)
835 mono_once (&process_current_once, process_set_current);
837 return((gpointer)-1);
840 guint32 GetCurrentProcessId (void)
842 struct _WapiHandle_process *current_process_handle;
845 mono_once (&process_current_once, process_set_current);
847 ok=_wapi_lookup_handle (current_process, WAPI_HANDLE_PROCESS,
848 (gpointer *)¤t_process_handle);
850 g_warning ("%s: error looking up current process handle %p",
851 __func__, current_process);
852 /* No failure return is defined. PID 0 is invalid.
853 * This should only be reached when something else has
854 * gone badly wrong anyway.
859 return(current_process_handle->id);
862 /* Returns the process id as a convenience to the functions that call this */
863 static pid_t signal_process_if_gone (gpointer handle)
865 struct _WapiHandle_process *process_handle;
868 /* Make sure the process is signalled if it has exited - if
869 * the parent process didn't wait for it then it won't be
871 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_PROCESS,
872 (gpointer *)&process_handle);
874 g_warning ("%s: error looking up process handle %p",
881 g_message ("%s: looking at process %d", __func__, process_handle->id);
884 if (kill (process_handle->id, 0) == -1 &&
887 /* The process is dead, (EPERM tells us a new process
888 * has that ID, but as it's owned by someone else it
889 * can't be the one listed in our shared memory file)
891 _wapi_shared_handle_set_signal_state (handle, TRUE);
894 return (process_handle->id);
897 static gboolean process_enum (gpointer handle, gpointer user_data)
899 GArray *processes=user_data;
900 pid_t pid = signal_process_if_gone (handle);
907 /* Ignore processes that have already exited (ie they are signalled) */
908 if (_wapi_handle_issignalled (handle) == FALSE) {
910 g_message ("%s: process %d added to array", __func__, pid);
913 /* This ensures that duplicates aren't returned (see
914 * the comment above _wapi_search_handle () for why
917 for (i = 0; i < processes->len; i++) {
918 if (g_array_index (processes, pid_t, i) == pid) {
919 /* We've already got this one, return
920 * FALSE to keep searching
926 g_array_append_val (processes, pid);
929 /* Return false to keep searching */
933 gboolean EnumProcesses (guint32 *pids, guint32 len, guint32 *needed)
935 GArray *processes = g_array_new (FALSE, FALSE, sizeof(pid_t));
938 mono_once (&process_current_once, process_set_current);
940 _wapi_search_handle (WAPI_HANDLE_PROCESS, process_enum, processes,
943 fit=len/sizeof(guint32);
944 for (i = 0, j = 0; j < fit && i < processes->len; i++) {
945 pids[j++] = g_array_index (processes, pid_t, i);
948 g_array_free (processes, TRUE);
950 *needed = j * sizeof(guint32);
955 static gboolean process_open_compare (gpointer handle, gpointer user_data)
958 pid_t checking_pid = signal_process_if_gone (handle);
960 if (checking_pid == 0) {
964 wanted_pid = GPOINTER_TO_UINT (user_data);
966 /* It's possible to have more than one process handle with the
967 * same pid, but only the one running process can be
970 if (checking_pid == wanted_pid &&
971 _wapi_handle_issignalled (handle) == FALSE) {
978 gpointer OpenProcess (guint32 access G_GNUC_UNUSED, gboolean inherit G_GNUC_UNUSED, guint32 pid)
980 /* Find the process handle that corresponds to pid */
983 mono_once (&process_current_once, process_set_current);
986 g_message ("%s: looking for process %d", __func__, pid);
989 handle = _wapi_search_handle (WAPI_HANDLE_PROCESS,
990 process_open_compare,
991 GUINT_TO_POINTER (pid), NULL);
994 g_message ("%s: Can't find pid %d", __func__, pid);
997 SetLastError (ERROR_PROC_NOT_FOUND);
1002 _wapi_handle_ref (handle);
1007 gboolean GetExitCodeProcess (gpointer process, guint32 *code)
1009 struct _WapiHandle_process *process_handle;
1012 mono_once (&process_current_once, process_set_current);
1018 ok=_wapi_lookup_handle (process, WAPI_HANDLE_PROCESS,
1019 (gpointer *)&process_handle);
1022 g_message ("%s: Can't find process %p", __func__, process);
1028 /* A process handle is only signalled if the process has exited
1029 * and has been waited for */
1030 if (_wapi_handle_issignalled (process) == TRUE ||
1031 process_wait (process, 0) == WAIT_OBJECT_0) {
1032 *code = process_handle->exitstatus;
1034 *code = STILL_ACTIVE;
1040 gboolean GetProcessTimes (gpointer process, WapiFileTime *create_time,
1041 WapiFileTime *exit_time, WapiFileTime *kernel_time,
1042 WapiFileTime *user_time)
1044 struct _WapiHandle_process *process_handle;
1047 mono_once (&process_current_once, process_set_current);
1049 if(create_time==NULL || exit_time==NULL || kernel_time==NULL ||
1051 /* Not sure if w32 allows NULLs here or not */
1055 ok=_wapi_lookup_handle (process, WAPI_HANDLE_PROCESS,
1056 (gpointer *)&process_handle);
1059 g_message ("%s: Can't find process %p", __func__, process);
1065 *create_time=process_handle->create_time;
1067 /* A process handle is only signalled if the process has
1068 * exited. Otherwise exit_time isn't set
1070 if(_wapi_handle_issignalled (process)==TRUE) {
1071 *exit_time=process_handle->exit_time;
1077 gboolean EnumProcessModules (gpointer process, gpointer *modules,
1078 guint32 size, guint32 *needed)
1080 /* Store modules in an array of pointers (main module as
1081 * modules[0]), using the load address for each module as a
1082 * token. (Use 'NULL' as an alternative for the main module
1083 * so that the simple implementation can just return one item
1084 * for now.) Get the info from /proc/<pid>/maps on linux,
1085 * other systems will have to implement /dev/kmem reading or
1086 * whatever other horrid technique is needed.
1088 if(size<sizeof(gpointer)) {
1094 *needed=sizeof(gpointer);
1097 *needed=sizeof(gpointer);
1103 guint32 GetModuleBaseName (gpointer process, gpointer module,
1104 gunichar2 *basename, guint32 size)
1106 struct _WapiHandle_process *process_handle;
1109 mono_once (&process_current_once, process_set_current);
1112 g_message ("%s: Getting module base name, process handle %p module %p",
1113 __func__, process, module);
1116 if(basename==NULL || size==0) {
1120 ok=_wapi_lookup_handle (process, WAPI_HANDLE_PROCESS,
1121 (gpointer *)&process_handle);
1124 g_message ("%s: Can't find process %p", __func__, process);
1131 /* Shorthand for the main module, which has the
1132 * process name recorded in the handle data
1135 gunichar2 *procname;
1136 guchar *procname_utf8 = NULL;
1140 g_message ("%s: Returning main module name", __func__);
1143 pid=process_handle->id;
1144 procname_utf8 = process_handle->proc_name;
1147 g_message ("%s: Process name is [%s]", __func__,
1151 procname = g_utf8_to_utf16 (procname_utf8, -1, NULL, &len,
1153 if (procname == NULL) {
1158 /* Add the terminator, and convert chars to bytes */
1159 bytes = (len + 1) * 2;
1163 g_message ("%s: Size %d smaller than needed (%ld); truncating", __func__, size, bytes);
1166 memcpy (basename, procname, size);
1169 g_message ("%s: Size %d larger than needed (%ld)",
1170 __func__, size, bytes);
1173 memcpy (basename, procname, bytes);
1180 /* Look up the address in /proc/<pid>/maps */
1186 gboolean GetProcessWorkingSetSize (gpointer process, size_t *min, size_t *max)
1188 struct _WapiHandle_process *process_handle;
1191 mono_once (&process_current_once, process_set_current);
1193 if(min==NULL || max==NULL) {
1194 /* Not sure if w32 allows NULLs here or not */
1198 ok=_wapi_lookup_handle (process, WAPI_HANDLE_PROCESS,
1199 (gpointer *)&process_handle);
1202 g_message ("%s: Can't find process %p", __func__, process);
1208 *min=process_handle->min_working_set;
1209 *max=process_handle->max_working_set;
1214 gboolean SetProcessWorkingSetSize (gpointer process, size_t min, size_t max)
1216 struct _WapiHandle_process *process_handle;
1219 mono_once (&process_current_once, process_set_current);
1221 ok=_wapi_lookup_handle (process, WAPI_HANDLE_PROCESS,
1222 (gpointer *)&process_handle);
1225 g_message ("%s: Can't find process %p", __func__, process);
1231 process_handle->min_working_set=min;
1232 process_handle->max_working_set=max;
1239 TerminateProcess (gpointer process, gint32 exitCode)
1241 struct _WapiHandle_process *process_handle;
1246 ok = _wapi_lookup_handle (process, WAPI_HANDLE_PROCESS,
1247 (gpointer *) &process_handle);
1251 g_message ("%s: Can't find process %p", __func__, process);
1253 SetLastError (ERROR_INVALID_HANDLE);
1257 signo = (exitCode == -1) ? SIGKILL : SIGTERM;
1258 ret = kill (process_handle->id, signo);
1262 SetLastError (ERROR_INVALID_PARAMETER);
1265 SetLastError (ERROR_ACCESS_DENIED);
1268 SetLastError (ERROR_PROC_NOT_FOUND);
1271 SetLastError (ERROR_GEN_FAILURE);