2 * processes.c: Process handles
5 * Dick Porter (dick@ximian.com)
7 * (C) 2002 Ximian, Inc.
12 #include <mono/os/gc_wrapper.h>
13 #include "mono/utils/mono-hash.h"
21 #include <sys/types.h>
24 #include <mono/io-layer/wapi.h>
25 #include <mono/io-layer/unicode.h>
26 #include <mono/io-layer/wapi-private.h>
27 #include <mono/io-layer/handles-private.h>
28 #include <mono/io-layer/misc-private.h>
29 #include <mono/io-layer/mono-mutex.h>
30 #include <mono/io-layer/process-private.h>
31 #include <mono/io-layer/threads.h>
33 /* The process' environment strings */
34 extern char **environ;
38 static void process_close_shared (gpointer handle);
40 struct _WapiHandleOps _wapi_process_ops = {
41 process_close_shared, /* close_shared */
42 NULL, /* close_private */
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);
59 static void process_close_shared (gpointer handle G_GNUC_UNUSED)
61 struct _WapiHandle_process *process_handle;
64 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_PROCESS,
65 (gpointer *)&process_handle, NULL);
67 g_warning (G_GNUC_PRETTY_FUNCTION
68 ": error looking up process handle %p", handle);
73 g_message (G_GNUC_PRETTY_FUNCTION
74 ": closing process handle %p with id %d", handle,
78 if(process_handle->proc_name!=0) {
79 _wapi_handle_scratch_delete (process_handle->proc_name);
80 process_handle->proc_name=0;
84 gboolean CreateProcess (const gunichar2 *appname, gunichar2 *cmdline,
85 WapiSecurityAttributes *process_attrs G_GNUC_UNUSED,
86 WapiSecurityAttributes *thread_attrs G_GNUC_UNUSED,
87 gboolean inherit_handles, guint32 create_flags,
88 gpointer new_environ, const gunichar2 *cwd,
89 WapiStartupInfo *startup,
90 WapiProcessInformation *process_info)
92 gchar *cmd=NULL, *prog, *full_prog, *args=NULL, *args_after_prog=NULL, *dir=NULL;
93 guint32 env=0, stored_dir=0, stored_prog=0, i;
95 gpointer stdin_handle, stdout_handle, stderr_handle;
97 gpointer process_handle, thread_handle;
99 mono_once (&process_ops_once, process_ops_init);
101 /* appname and cmdline specify the executable and its args:
103 * If appname is not NULL, it is the name of the executable.
104 * Otherwise the executable is the first token in cmdline.
106 * Executable searching:
108 * If appname is not NULL, it can specify the full path and
109 * file name, or else a partial name and the current directory
110 * will be used. There is no additional searching.
112 * If appname is NULL, the first whitespace-delimited token in
113 * cmdline is used. If the name does not contain a full
114 * directory path, the search sequence is:
116 * 1) The directory containing the current process
117 * 2) The current working directory
118 * 3) The windows system directory (Ignored)
119 * 4) The windows directory (Ignored)
122 * Just to make things more interesting, tokens can contain
123 * white space if they are surrounded by quotation marks. I'm
124 * beginning to understand just why windows apps are generally
125 * so crap, with an API like this :-(
128 cmd=_wapi_unicode_to_utf8 (appname);
131 g_message (G_GNUC_PRETTY_FUNCTION
132 ": unicode conversion returned NULL");
138 /* Turn all the slashes round the right way */
139 for(i=0; i<strlen (cmd); i++) {
147 args=_wapi_unicode_to_utf8 (cmdline);
150 g_message (G_GNUC_PRETTY_FUNCTION
151 ": unicode conversion returned NULL");
157 /* Turn all the slashes round the right way */
158 for(i=0; i<strlen (args); i++) {
166 dir=_wapi_unicode_to_utf8 (cwd);
169 g_message (G_GNUC_PRETTY_FUNCTION
170 ": unicode conversion returned NULL");
176 /* Turn all the slashes round the right way */
177 for(i=0; i<strlen (dir); i++) {
183 dir=g_get_current_dir ();
185 stored_dir=_wapi_handle_scratch_store (dir, strlen (dir));
188 /* new_environ is a block of NULL-terminated strings, which
189 * is itself NULL-terminated. Of course, passing an array of
190 * string pointers would have made things too easy :-(
192 * If new_environ is not NULL it specifies the entire set of
193 * environment variables in the new process. Otherwise the
194 * new process inherits the same environment.
196 if(new_environ!=NULL) {
199 gunichar2 *new_environp;
201 /* Count the number of strings */
202 for(new_environp=(gunichar2 *)new_environ; *new_environp;
205 while(*new_environp) {
209 strings=g_new0 (gchar *, count);
211 /* Copy each environ string into 'strings' turning it
212 * into utf8 at the same time
215 for(new_environp=(gunichar2 *)new_environ; *new_environp;
217 strings[count]=g_utf16_to_utf8 (new_environp, -1, NULL,
220 while(*new_environp) {
225 env=_wapi_handle_scratch_store_string_array (strings);
227 g_strfreev (strings);
229 /* Use the existing environment */
230 env=_wapi_handle_scratch_store_string_array (environ);
233 /* We can't put off locating the executable any longer :-( */
235 if(g_ascii_isalpha (cmd[0]) && (cmd[1]==':')) {
236 /* Strip off the drive letter. I can't
237 * believe that CP/M holdover is still
240 memmove (cmd, cmd+2, strlen (cmd)-2);
241 cmd[strlen (cmd)-2]='\0';
245 /* Assume full path given */
248 /* Search for file named by cmd in the current
251 char *curdir=g_get_current_dir ();
253 prog=g_strdup_printf ("%s/%s", curdir, cmd);
257 args_after_prog=args;
261 /* Dig out the first token from args, taking quotation
265 /* First, strip off all leading whitespace */
266 args=g_strchug (args);
268 /* args_after_prog points to the contents of args
269 * after token has been set (otherwise argv[0] is
272 args_after_prog=args;
274 /* Assume the opening quote will always be the first
278 for(i=1; args[i]!='\0' && args[i]!='\"'; i++);
279 if(g_ascii_isspace (args[i+1])) {
280 /* We found the first token */
281 token=g_strndup (args+1, i-1);
282 args_after_prog=args+i;
284 /* Quotation mark appeared in the
285 * middle of the token. Just give the
286 * whole first token, quotes and all,
293 /* No quote mark, or malformed */
294 for(i=0; args[i]!='\0'; i++) {
295 if(g_ascii_isspace (args[i])) {
296 token=g_strndup (args, i);
297 args_after_prog=args+i;
303 if(token==NULL && args[0]!='\0') {
304 /* Must be just one token in the string */
305 token=g_strdup (args);
306 args_after_prog=NULL;
312 g_message (G_GNUC_PRETTY_FUNCTION
313 ": Couldn't find what to exec");
319 if(g_ascii_isalpha (token[0]) && (token[1]==':')) {
320 /* Strip off the drive letter. I can't
321 * believe that CP/M holdover is still
324 memmove (token, token+2, strlen (token)-2);
325 token[strlen (token)-2]='\0';
329 /* Assume full path given */
330 prog=g_strdup (token);
332 char *curdir=g_get_current_dir ();
334 /* FIXME: Need to record the directory
335 * containing the current process, and check
336 * that for the new executable as the first
340 prog=g_strdup_printf ("%s/%s", curdir, token);
343 /* I assume X_OK is the criterion to use,
346 if(access (prog, X_OK)!=0) {
348 prog=g_find_program_in_path (token);
351 g_message (G_GNUC_PRETTY_FUNCTION ": Couldn't find executable %s", token);
364 g_message (G_GNUC_PRETTY_FUNCTION ": Exec prog [%s] args [%s]", prog,
368 if(args_after_prog!=NULL) {
369 full_prog=g_strconcat (prog, " ", args_after_prog, NULL);
371 full_prog=g_strdup (prog);
374 stored_prog=_wapi_handle_scratch_store (full_prog, strlen (full_prog));
376 if(startup!=NULL && startup->dwFlags & STARTF_USESTDHANDLES) {
377 stdin_handle=startup->hStdInput;
378 stdout_handle=startup->hStdOutput;
379 stderr_handle=startup->hStdError;
381 stdin_handle=GetStdHandle (STD_INPUT_HANDLE);
382 stdout_handle=GetStdHandle (STD_OUTPUT_HANDLE);
383 stderr_handle=GetStdHandle (STD_ERROR_HANDLE);
386 ret=_wapi_handle_process_fork (stored_prog, env, stored_dir,
387 inherit_handles, create_flags,
388 stdin_handle, stdout_handle,
389 stderr_handle, &process_handle,
390 &thread_handle, &pid, &tid);
392 if(ret==TRUE && process_info!=NULL) {
393 process_info->hProcess=process_handle;
394 process_info->hThread=thread_handle;
395 process_info->dwProcessId=pid;
396 process_info->dwThreadId=tid;
403 if(full_prog!=NULL) {
407 _wapi_handle_scratch_delete (stored_prog);
416 _wapi_handle_scratch_delete (stored_dir);
419 _wapi_handle_scratch_delete_string_array (env);
425 static gboolean process_compare (gpointer handle, gpointer user_data)
427 struct _WapiHandle_process *process_handle;
431 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_PROCESS,
432 (gpointer *)&process_handle, NULL);
434 g_warning (G_GNUC_PRETTY_FUNCTION
435 ": error looking up process handle %p", handle);
439 pid=GPOINTER_TO_UINT (user_data);
440 if(process_handle->id==pid) {
447 static void process_set_current (void)
449 struct _WapiHandle_process *process_handle;
453 current_process=_wapi_search_handle (WAPI_HANDLE_PROCESS,
455 GUINT_TO_POINTER (pid),
456 (gpointer *)&process_handle,
458 if(current_process==0) {
459 gchar *progname, *slash;
462 g_message (G_GNUC_PRETTY_FUNCTION
463 ": Need to create my own process handle");
466 current_process=_wapi_handle_new (WAPI_HANDLE_PROCESS);
467 if(current_process==_WAPI_HANDLE_INVALID) {
468 g_warning (G_GNUC_PRETTY_FUNCTION
469 ": error creating process handle");
473 ok=_wapi_lookup_handle (current_process, WAPI_HANDLE_PROCESS,
474 (gpointer *)&process_handle, NULL);
476 g_warning (G_GNUC_PRETTY_FUNCTION
477 ": error looking up process handle %p",
482 process_handle->id=pid;
484 /* These seem to be the defaults on w2k */
485 process_handle->min_working_set=204800;
486 process_handle->max_working_set=1413120;
488 progname=g_get_prgname ();
491 g_message (G_GNUC_PRETTY_FUNCTION ": using [%s] as prog name",
496 slash=strrchr (progname, '/');
498 process_handle->proc_name=_wapi_handle_scratch_store (slash+1, strlen (slash+1));
500 process_handle->proc_name=_wapi_handle_scratch_store (progname, strlen (progname));
504 /* Make sure the new handle has a reference so it wont go away
505 * until this process exits
507 _wapi_handle_ref (current_process);
510 g_message (G_GNUC_PRETTY_FUNCTION ": Found my process handle");
515 /* Returns a pseudo handle that doesn't need to be closed afterwards */
516 gpointer GetCurrentProcess (void)
518 mono_once (&process_current_once, process_set_current);
520 return((gpointer)-1);
523 guint32 GetCurrentProcessId (void)
525 mono_once (&process_current_once, process_set_current);
530 static gboolean process_enum (gpointer handle, gpointer user_data)
532 GPtrArray *processes=user_data;
534 /* Ignore processes that have already exited (ie they are signalled) */
535 if(_wapi_handle_issignalled (handle)==FALSE) {
536 g_ptr_array_add (processes, handle);
539 /* Return false to keep searching */
543 gboolean EnumProcesses (guint32 *pids, guint32 len, guint32 *needed)
545 GPtrArray *processes=g_ptr_array_new ();
548 mono_once (&process_current_once, process_set_current);
550 _wapi_search_handle (WAPI_HANDLE_PROCESS, process_enum, processes,
553 fit=len/sizeof(guint32);
554 for(i=0; i<fit && i<processes->len; i++) {
555 struct _WapiHandle_process *process_handle;
558 ok=_wapi_lookup_handle (g_ptr_array_index (processes, i),
560 (gpointer *)&process_handle, NULL);
562 g_warning (G_GNUC_PRETTY_FUNCTION ": error looking up process handle %p", g_ptr_array_index (processes, i));
563 g_ptr_array_free (processes, FALSE);
567 pids[i]=process_handle->id;
570 g_ptr_array_free (processes, FALSE);
572 *needed=i*sizeof(guint32);
577 static gboolean process_open_compare (gpointer handle, gpointer user_data)
579 struct _WapiHandle_process *process_handle;
583 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_PROCESS,
584 (gpointer *)&process_handle, NULL);
586 g_warning (G_GNUC_PRETTY_FUNCTION
587 ": error looking up process handle %p", handle);
591 pid=GPOINTER_TO_UINT (user_data);
593 /* It's possible to have more than one process handle with the
594 * same pid, but only the one running process can be
597 if(process_handle->id==pid &&
598 _wapi_handle_issignalled (handle)==FALSE) {
605 gpointer OpenProcess (guint32 access G_GNUC_UNUSED, gboolean inherit G_GNUC_UNUSED, guint32 pid)
607 /* Find the process handle that corresponds to pid */
610 mono_once (&process_current_once, process_set_current);
612 handle=_wapi_search_handle (WAPI_HANDLE_PROCESS, process_open_compare,
613 GUINT_TO_POINTER (pid), NULL, NULL);
616 g_message (G_GNUC_PRETTY_FUNCTION ": Can't find pid %d", pid);
619 /* Set an error code */
624 _wapi_handle_ref (handle);
629 gboolean GetExitCodeProcess (gpointer process, guint32 *code)
631 struct _WapiHandle_process *process_handle;
634 mono_once (&process_current_once, process_set_current);
640 ok=_wapi_lookup_handle (process, WAPI_HANDLE_PROCESS,
641 (gpointer *)&process_handle, NULL);
644 g_message (G_GNUC_PRETTY_FUNCTION ": Can't find process %p",
651 /* A process handle is only signalled if the process has exited */
652 if(_wapi_handle_issignalled (process)==TRUE) {
653 *code=process_handle->exitstatus;
661 gboolean GetProcessTimes (gpointer process, WapiFileTime *create_time,
662 WapiFileTime *exit_time, WapiFileTime *kernel_time,
663 WapiFileTime *user_time)
665 struct _WapiHandle_process *process_handle;
668 mono_once (&process_current_once, process_set_current);
670 if(create_time==NULL || exit_time==NULL || kernel_time==NULL ||
672 /* Not sure if w32 allows NULLs here or not */
676 ok=_wapi_lookup_handle (process, WAPI_HANDLE_PROCESS,
677 (gpointer *)&process_handle, NULL);
680 g_message (G_GNUC_PRETTY_FUNCTION ": Can't find process %p",
687 *create_time=process_handle->create_time;
689 /* A process handle is only signalled if the process has
690 * exited. Otherwise exit_time isn't set
692 if(_wapi_handle_issignalled (process)==TRUE) {
693 *exit_time=process_handle->exit_time;
699 gboolean EnumProcessModules (gpointer process, gpointer *modules,
700 guint32 size, guint32 *needed)
702 /* Store modules in an array of pointers (main module as
703 * modules[0]), using the load address for each module as a
704 * token. (Use 'NULL' as an alternative for the main module
705 * so that the simple implementation can just return one item
706 * for now.) Get the info from /proc/<pid>/maps on linux,
707 * other systems will have to implement /dev/kmem reading or
708 * whatever other horrid technique is needed.
710 if(size<sizeof(gpointer)) {
716 *needed=sizeof(gpointer);
719 *needed=sizeof(gpointer);
725 guint32 GetModuleBaseName (gpointer process, gpointer module,
726 gunichar2 *basename, guint32 size)
728 struct _WapiHandle_process *process_handle;
731 mono_once (&process_current_once, process_set_current);
734 g_message (G_GNUC_PRETTY_FUNCTION
735 ": Getting module base name, process handle %p module %p",
739 if(basename==NULL || size==0) {
743 ok=_wapi_lookup_handle (process, WAPI_HANDLE_PROCESS,
744 (gpointer *)&process_handle, NULL);
747 g_message (G_GNUC_PRETTY_FUNCTION ": Can't find process %p",
755 /* Shorthand for the main module, which has the
756 * process name recorded in the handle data
760 guchar *procname_utf8;
764 g_message (G_GNUC_PRETTY_FUNCTION
765 ": Returning main module name");
768 pid=process_handle->id;
769 procname_utf8=_wapi_handle_scratch_lookup_as_string (process_handle->proc_name);
772 g_message (G_GNUC_PRETTY_FUNCTION ": Process name is [%s]",
776 procname=g_utf8_to_utf16 (procname_utf8, -1, NULL, &len, NULL);
779 g_free (procname_utf8);
783 /* Add the terminator, and convert chars to bytes */
788 g_message (G_GNUC_PRETTY_FUNCTION ": Size %d smaller than needed (%ld); truncating", size, bytes);
791 memcpy (basename, procname, size);
794 g_message (G_GNUC_PRETTY_FUNCTION
795 ": Size %d larger than needed (%ld)",
799 memcpy (basename, procname, bytes);
802 g_free (procname_utf8);
807 /* Look up the address in /proc/<pid>/maps */
813 gboolean GetProcessWorkingSetSize (gpointer process, size_t *min, size_t *max)
815 struct _WapiHandle_process *process_handle;
818 mono_once (&process_current_once, process_set_current);
820 if(min==NULL || max==NULL) {
821 /* Not sure if w32 allows NULLs here or not */
825 ok=_wapi_lookup_handle (process, WAPI_HANDLE_PROCESS,
826 (gpointer *)&process_handle, NULL);
829 g_message (G_GNUC_PRETTY_FUNCTION ": Can't find process %p",
836 *min=process_handle->min_working_set;
837 *max=process_handle->max_working_set;
842 gboolean SetProcessWorkingSetSize (gpointer process, size_t min, size_t max)
844 struct _WapiHandle_process *process_handle;
847 mono_once (&process_current_once, process_set_current);
849 ok=_wapi_lookup_handle (process, WAPI_HANDLE_PROCESS,
850 (gpointer *)&process_handle, NULL);
853 g_message (G_GNUC_PRETTY_FUNCTION ": Can't find process %p",
860 process_handle->min_working_set=min;
861 process_handle->max_working_set=max;