Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Server-side process management |
| 3 | * |
| 4 | * Copyright (C) 1998 Alexandre Julliard |
Alexandre Julliard | 0799c1a | 2002-03-09 23:29:33 +0000 | [diff] [blame] | 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License as published by the Free Software Foundation; either |
| 9 | * version 2.1 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
Patrik Stridvall | fb32c7b | 2000-01-29 21:02:19 +0000 | [diff] [blame] | 21 | #include "config.h" |
Alexandre Julliard | 5769d1d | 2002-04-26 19:05:15 +0000 | [diff] [blame] | 22 | #include "wine/port.h" |
Patrik Stridvall | fb32c7b | 2000-01-29 21:02:19 +0000 | [diff] [blame] | 23 | |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 24 | #include <assert.h> |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 25 | #include <limits.h> |
Alexandre Julliard | 3da5f84 | 1999-05-16 16:54:54 +0000 | [diff] [blame] | 26 | #include <signal.h> |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 27 | #include <string.h> |
Alexandre Julliard | e37c6e1 | 2003-09-05 23:08:26 +0000 | [diff] [blame] | 28 | #include <stdarg.h> |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 29 | #include <stdio.h> |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 30 | #include <stdlib.h> |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 31 | #include <sys/time.h> |
Patrik Stridvall | fb32c7b | 2000-01-29 21:02:19 +0000 | [diff] [blame] | 32 | #ifdef HAVE_SYS_SOCKET_H |
| 33 | # include <sys/socket.h> |
| 34 | #endif |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 35 | #include <unistd.h> |
Steven Edwards | 5727918 | 2005-03-04 12:38:36 +0000 | [diff] [blame] | 36 | #ifdef HAVE_POLL_H |
| 37 | #include <poll.h> |
| 38 | #endif |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 39 | |
Alexandre Julliard | e37c6e1 | 2003-09-05 23:08:26 +0000 | [diff] [blame] | 40 | #include "windef.h" |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 41 | #include "winnt.h" |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 42 | |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 43 | #include "file.h" |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 44 | #include "handle.h" |
| 45 | #include "process.h" |
| 46 | #include "thread.h" |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 47 | #include "request.h" |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 48 | #include "console.h" |
Alexandre Julliard | bfce151 | 2003-12-10 04:08:06 +0000 | [diff] [blame] | 49 | #include "user.h" |
Robert Shearman | d2ea92d | 2005-04-22 21:17:15 +0000 | [diff] [blame] | 50 | #include "security.h" |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 51 | |
Alexandre Julliard | f692d44 | 1999-03-21 19:23:54 +0000 | [diff] [blame] | 52 | /* process structure */ |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 53 | |
Alexandre Julliard | 48c0d51 | 2005-02-25 19:31:26 +0000 | [diff] [blame] | 54 | static struct list process_list = LIST_INIT(process_list); |
Alexandre Julliard | fdc92ba | 1999-02-14 18:03:15 +0000 | [diff] [blame] | 55 | static int running_processes; |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 56 | |
| 57 | /* process operations */ |
| 58 | |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 59 | static void process_dump( struct object *obj, int verbose ); |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 60 | static int process_signaled( struct object *obj, struct thread *thread ); |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 61 | static void process_poll_event( struct fd *fd, int event ); |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 62 | static void process_destroy( struct object *obj ); |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 63 | |
| 64 | static const struct object_ops process_ops = |
| 65 | { |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 66 | sizeof(struct process), /* size */ |
| 67 | process_dump, /* dump */ |
| 68 | add_queue, /* add_queue */ |
| 69 | remove_queue, /* remove_queue */ |
| 70 | process_signaled, /* signaled */ |
| 71 | no_satisfied, /* satisfied */ |
Alexandre Julliard | b9b1ea9 | 2005-06-09 15:39:52 +0000 | [diff] [blame] | 72 | no_signal, /* signal */ |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 73 | no_get_fd, /* get_fd */ |
Alexandre Julliard | b9b1ea9 | 2005-06-09 15:39:52 +0000 | [diff] [blame] | 74 | no_close_handle, /* close_handle */ |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 75 | process_destroy /* destroy */ |
| 76 | }; |
| 77 | |
| 78 | static const struct fd_ops process_fd_ops = |
| 79 | { |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 80 | NULL, /* get_poll_events */ |
Alexandre Julliard | f524240 | 2001-02-28 21:45:23 +0000 | [diff] [blame] | 81 | process_poll_event, /* poll_event */ |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 82 | no_flush, /* flush */ |
| 83 | no_get_file_info, /* get_file_info */ |
Eric Pouech | 4634447 | 2005-01-14 19:54:38 +0000 | [diff] [blame] | 84 | no_queue_async, /* queue_async */ |
| 85 | no_cancel_async /* cancel async */ |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 86 | }; |
| 87 | |
Alexandre Julliard | 5b4f3e8 | 2000-05-01 16:24:22 +0000 | [diff] [blame] | 88 | /* process startup info */ |
| 89 | |
| 90 | struct startup_info |
Alexandre Julliard | 2fe5777 | 2000-01-25 01:40:27 +0000 | [diff] [blame] | 91 | { |
Alexandre Julliard | 5b4f3e8 | 2000-05-01 16:24:22 +0000 | [diff] [blame] | 92 | struct object obj; /* object header */ |
Alexandre Julliard | 77c8b1d | 2003-02-24 20:51:50 +0000 | [diff] [blame] | 93 | struct list entry; /* entry in list of startup infos */ |
Alexandre Julliard | 5b4f3e8 | 2000-05-01 16:24:22 +0000 | [diff] [blame] | 94 | int inherit_all; /* inherit all handles from parent */ |
Alexandre Julliard | 01caa5e | 2005-07-12 20:27:09 +0000 | [diff] [blame] | 95 | unsigned int create_flags; /* creation flags */ |
Alexandre Julliard | baf0a06 | 2003-03-11 01:48:53 +0000 | [diff] [blame] | 96 | int unix_pid; /* Unix pid of new process */ |
Alexandre Julliard | 5188574 | 2002-05-30 20:12:58 +0000 | [diff] [blame] | 97 | obj_handle_t hstdin; /* handle for stdin */ |
| 98 | obj_handle_t hstdout; /* handle for stdout */ |
| 99 | obj_handle_t hstderr; /* handle for stderr */ |
Alexandre Julliard | d27624b | 2000-05-03 18:42:40 +0000 | [diff] [blame] | 100 | struct file *exe_file; /* file handle for main exe */ |
Alexandre Julliard | e9936d9 | 2001-01-26 00:22:26 +0000 | [diff] [blame] | 101 | struct thread *owner; /* owner thread (the one that created the new process) */ |
Alexandre Julliard | 5b4f3e8 | 2000-05-01 16:24:22 +0000 | [diff] [blame] | 102 | struct process *process; /* created process */ |
| 103 | struct thread *thread; /* created thread */ |
Alexandre Julliard | 6543a65 | 2002-03-29 18:28:56 +0000 | [diff] [blame] | 104 | size_t data_size; /* size of startup data */ |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 105 | void *data; /* data for startup info */ |
Alexandre Julliard | 5b4f3e8 | 2000-05-01 16:24:22 +0000 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | static void startup_info_dump( struct object *obj, int verbose ); |
| 109 | static int startup_info_signaled( struct object *obj, struct thread *thread ); |
| 110 | static void startup_info_destroy( struct object *obj ); |
| 111 | |
| 112 | static const struct object_ops startup_info_ops = |
| 113 | { |
| 114 | sizeof(struct startup_info), /* size */ |
| 115 | startup_info_dump, /* dump */ |
| 116 | add_queue, /* add_queue */ |
| 117 | remove_queue, /* remove_queue */ |
| 118 | startup_info_signaled, /* signaled */ |
| 119 | no_satisfied, /* satisfied */ |
Mike McCormack | f92fff6 | 2005-04-24 17:35:52 +0000 | [diff] [blame] | 120 | no_signal, /* signal */ |
Alexandre Julliard | 1ab243b | 2000-12-19 02:12:45 +0000 | [diff] [blame] | 121 | no_get_fd, /* get_fd */ |
Alexandre Julliard | b9b1ea9 | 2005-06-09 15:39:52 +0000 | [diff] [blame] | 122 | no_close_handle, /* close_handle */ |
Alexandre Julliard | 5b4f3e8 | 2000-05-01 16:24:22 +0000 | [diff] [blame] | 123 | startup_info_destroy /* destroy */ |
| 124 | }; |
| 125 | |
Alexandre Julliard | f692d44 | 1999-03-21 19:23:54 +0000 | [diff] [blame] | 126 | |
Alexandre Julliard | 77c8b1d | 2003-02-24 20:51:50 +0000 | [diff] [blame] | 127 | static struct list startup_info_list = LIST_INIT(startup_info_list); |
| 128 | |
Alexandre Julliard | 91befe1 | 2003-02-01 01:38:40 +0000 | [diff] [blame] | 129 | struct ptid_entry |
| 130 | { |
| 131 | void *ptr; /* entry ptr */ |
| 132 | unsigned int next; /* next free entry */ |
| 133 | }; |
| 134 | |
| 135 | static struct ptid_entry *ptid_entries; /* array of ptid entries */ |
| 136 | static unsigned int used_ptid_entries; /* number of entries in use */ |
| 137 | static unsigned int alloc_ptid_entries; /* number of allocated entries */ |
| 138 | static unsigned int next_free_ptid; /* next free entry */ |
| 139 | static unsigned int last_free_ptid; /* last free entry */ |
| 140 | |
| 141 | #define PTID_OFFSET 8 /* offset for first ptid value */ |
| 142 | |
| 143 | /* allocate a new process or thread id */ |
| 144 | unsigned int alloc_ptid( void *ptr ) |
| 145 | { |
| 146 | struct ptid_entry *entry; |
| 147 | unsigned int id; |
| 148 | |
| 149 | if (used_ptid_entries < alloc_ptid_entries) |
| 150 | { |
| 151 | id = used_ptid_entries + PTID_OFFSET; |
| 152 | entry = &ptid_entries[used_ptid_entries++]; |
| 153 | } |
| 154 | else if (next_free_ptid) |
| 155 | { |
| 156 | id = next_free_ptid; |
| 157 | entry = &ptid_entries[id - PTID_OFFSET]; |
| 158 | if (!(next_free_ptid = entry->next)) last_free_ptid = 0; |
| 159 | } |
| 160 | else /* need to grow the array */ |
| 161 | { |
| 162 | unsigned int count = alloc_ptid_entries + (alloc_ptid_entries / 2); |
| 163 | if (!count) count = 64; |
| 164 | if (!(entry = realloc( ptid_entries, count * sizeof(*entry) ))) |
| 165 | { |
| 166 | set_error( STATUS_NO_MEMORY ); |
| 167 | return 0; |
| 168 | } |
| 169 | ptid_entries = entry; |
| 170 | alloc_ptid_entries = count; |
| 171 | id = used_ptid_entries + PTID_OFFSET; |
| 172 | entry = &ptid_entries[used_ptid_entries++]; |
| 173 | } |
| 174 | |
| 175 | entry->ptr = ptr; |
| 176 | return id; |
| 177 | } |
| 178 | |
| 179 | /* free a process or thread id */ |
| 180 | void free_ptid( unsigned int id ) |
| 181 | { |
| 182 | struct ptid_entry *entry = &ptid_entries[id - PTID_OFFSET]; |
| 183 | |
| 184 | entry->ptr = NULL; |
| 185 | entry->next = 0; |
| 186 | |
| 187 | /* append to end of free list so that we don't reuse it too early */ |
| 188 | if (last_free_ptid) ptid_entries[last_free_ptid - PTID_OFFSET].next = id; |
| 189 | else next_free_ptid = id; |
| 190 | |
| 191 | last_free_ptid = id; |
| 192 | } |
| 193 | |
| 194 | /* retrieve the pointer corresponding to a process or thread id */ |
| 195 | void *get_ptid_entry( unsigned int id ) |
| 196 | { |
| 197 | if (id < PTID_OFFSET) return NULL; |
| 198 | if (id - PTID_OFFSET >= used_ptid_entries) return NULL; |
| 199 | return ptid_entries[id - PTID_OFFSET].ptr; |
| 200 | } |
| 201 | |
Alexandre Julliard | 0502638 | 2005-03-01 10:56:18 +0000 | [diff] [blame] | 202 | /* return the main thread of the process */ |
| 203 | struct thread *get_process_first_thread( struct process *process ) |
| 204 | { |
| 205 | struct list *ptr = list_head( &process->thread_list ); |
| 206 | if (!ptr) return NULL; |
| 207 | return LIST_ENTRY( ptr, struct thread, proc_entry ); |
| 208 | } |
| 209 | |
Alexandre Julliard | 9d80215 | 2002-05-24 21:20:27 +0000 | [diff] [blame] | 210 | /* set the state of the process startup info */ |
| 211 | static void set_process_startup_state( struct process *process, enum startup_state state ) |
| 212 | { |
Alexandre Julliard | ca96de3 | 2002-05-25 21:15:03 +0000 | [diff] [blame] | 213 | if (process->startup_state == STARTUP_IN_PROGRESS) process->startup_state = state; |
| 214 | if (process->startup_info) |
| 215 | { |
| 216 | wake_up( &process->startup_info->obj, 0 ); |
| 217 | release_object( process->startup_info ); |
| 218 | process->startup_info = NULL; |
| 219 | } |
Alexandre Julliard | 9d80215 | 2002-05-24 21:20:27 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Alexandre Julliard | 2fe5777 | 2000-01-25 01:40:27 +0000 | [diff] [blame] | 222 | /* create a new process and its main thread */ |
Alexandre Julliard | 5b4f3e8 | 2000-05-01 16:24:22 +0000 | [diff] [blame] | 223 | struct thread *create_process( int fd ) |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 224 | { |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 225 | struct process *process; |
Alexandre Julliard | 2fe5777 | 2000-01-25 01:40:27 +0000 | [diff] [blame] | 226 | struct thread *thread = NULL; |
Alexandre Julliard | 8859d77 | 2001-03-01 22:13:49 +0000 | [diff] [blame] | 227 | int request_pipe[2]; |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 228 | |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 229 | if (!(process = alloc_object( &process_ops ))) goto error; |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 230 | process->parent = NULL; |
Alexandre Julliard | 3da5f84 | 1999-05-16 16:54:54 +0000 | [diff] [blame] | 231 | process->debugger = NULL; |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 232 | process->handles = NULL; |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 233 | process->msg_fd = NULL; |
Dave Pickles | 717bf7e | 2000-02-13 16:04:14 +0000 | [diff] [blame] | 234 | process->exit_code = STILL_ACTIVE; |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 235 | process->running_threads = 0; |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 236 | process->priority = NORMAL_PRIORITY_CLASS; |
| 237 | process->affinity = 1; |
Alexandre Julliard | 3da5f84 | 1999-05-16 16:54:54 +0000 | [diff] [blame] | 238 | process->suspend = 0; |
Alexandre Julliard | d083a7b | 1999-11-29 02:10:56 +0000 | [diff] [blame] | 239 | process->create_flags = 0; |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 240 | process->console = NULL; |
Alexandre Julliard | ca96de3 | 2002-05-25 21:15:03 +0000 | [diff] [blame] | 241 | process->startup_state = STARTUP_IN_PROGRESS; |
Alexandre Julliard | 9d80215 | 2002-05-24 21:20:27 +0000 | [diff] [blame] | 242 | process->startup_info = NULL; |
Alexandre Julliard | c5e433a | 2000-05-30 19:48:18 +0000 | [diff] [blame] | 243 | process->idle_event = NULL; |
| 244 | process->queue = NULL; |
Alexandre Julliard | e55d593 | 2003-10-14 01:30:42 +0000 | [diff] [blame] | 245 | process->peb = NULL; |
Alexandre Julliard | 0a7c1f6 | 2000-01-27 02:54:17 +0000 | [diff] [blame] | 246 | process->ldt_copy = NULL; |
Alexandre Julliard | 1bf96e0 | 2005-06-08 18:44:50 +0000 | [diff] [blame] | 247 | process->winstation = 0; |
Alexandre Julliard | 78a3e63 | 2005-06-09 12:07:12 +0000 | [diff] [blame] | 248 | process->desktop = 0; |
Alexandre Julliard | 05f0b71 | 2000-03-09 18:18:41 +0000 | [diff] [blame] | 249 | process->exe.file = NULL; |
| 250 | process->exe.dbg_offset = 0; |
| 251 | process->exe.dbg_size = 0; |
Alexandre Julliard | aeb5660 | 2002-03-22 00:21:23 +0000 | [diff] [blame] | 252 | process->exe.namelen = 0; |
| 253 | process->exe.filename = NULL; |
Robert Shearman | d2ea92d | 2005-04-22 21:17:15 +0000 | [diff] [blame] | 254 | process->token = token_create_admin(); |
Alexandre Julliard | 0502638 | 2005-03-01 10:56:18 +0000 | [diff] [blame] | 255 | list_init( &process->thread_list ); |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 256 | list_init( &process->locks ); |
Alexandre Julliard | bfce151 | 2003-12-10 04:08:06 +0000 | [diff] [blame] | 257 | list_init( &process->classes ); |
Alexandre Julliard | a9e0fb1 | 2005-03-02 10:20:09 +0000 | [diff] [blame] | 258 | list_init( &process->dlls ); |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 259 | |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 260 | gettimeofday( &process->start_time, NULL ); |
Alexandre Julliard | 48c0d51 | 2005-02-25 19:31:26 +0000 | [diff] [blame] | 261 | list_add_head( &process_list, &process->entry ); |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 262 | |
Alexandre Julliard | 01caa5e | 2005-07-12 20:27:09 +0000 | [diff] [blame] | 263 | if (!(process->id = process->group_id = alloc_ptid( process ))) goto error; |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 264 | if (!(process->msg_fd = create_anonymous_fd( &process_fd_ops, fd, &process->obj ))) goto error; |
Alexandre Julliard | 91befe1 | 2003-02-01 01:38:40 +0000 | [diff] [blame] | 265 | |
Alexandre Julliard | 7f74824 | 2000-12-26 00:30:30 +0000 | [diff] [blame] | 266 | /* create the main thread */ |
Alexandre Julliard | 8859d77 | 2001-03-01 22:13:49 +0000 | [diff] [blame] | 267 | if (pipe( request_pipe ) == -1) |
| 268 | { |
| 269 | file_set_error(); |
| 270 | goto error; |
| 271 | } |
Alexandre Julliard | 4144b5b | 2002-06-20 23:21:27 +0000 | [diff] [blame] | 272 | if (send_client_fd( process, request_pipe[1], 0 ) == -1) |
| 273 | { |
| 274 | close( request_pipe[0] ); |
| 275 | close( request_pipe[1] ); |
| 276 | goto error; |
| 277 | } |
Alexandre Julliard | 8859d77 | 2001-03-01 22:13:49 +0000 | [diff] [blame] | 278 | close( request_pipe[1] ); |
| 279 | if (!(thread = create_thread( request_pipe[0], process ))) goto error; |
Alexandre Julliard | 7f74824 | 2000-12-26 00:30:30 +0000 | [diff] [blame] | 280 | |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 281 | set_fd_events( process->msg_fd, POLLIN ); /* start listening to events */ |
Alexandre Julliard | 2fe5777 | 2000-01-25 01:40:27 +0000 | [diff] [blame] | 282 | release_object( process ); |
| 283 | return thread; |
Alexandre Julliard | f692d44 | 1999-03-21 19:23:54 +0000 | [diff] [blame] | 284 | |
| 285 | error: |
Alexandre Julliard | 4144b5b | 2002-06-20 23:21:27 +0000 | [diff] [blame] | 286 | if (process) release_object( process ); |
| 287 | /* if we failed to start our first process, close everything down */ |
| 288 | if (!running_processes) close_master_socket(); |
Alexandre Julliard | f692d44 | 1999-03-21 19:23:54 +0000 | [diff] [blame] | 289 | return NULL; |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Alexandre Julliard | 77c8b1d | 2003-02-24 20:51:50 +0000 | [diff] [blame] | 292 | /* find the startup info for a given Unix process */ |
Alexandre Julliard | baf0a06 | 2003-03-11 01:48:53 +0000 | [diff] [blame] | 293 | inline static struct startup_info *find_startup_info( int unix_pid ) |
Alexandre Julliard | 77c8b1d | 2003-02-24 20:51:50 +0000 | [diff] [blame] | 294 | { |
| 295 | struct list *ptr; |
| 296 | |
| 297 | LIST_FOR_EACH( ptr, &startup_info_list ) |
| 298 | { |
| 299 | struct startup_info *info = LIST_ENTRY( ptr, struct startup_info, entry ); |
| 300 | if (info->unix_pid == unix_pid) return info; |
| 301 | } |
| 302 | return NULL; |
| 303 | } |
| 304 | |
Alexandre Julliard | 5b4f3e8 | 2000-05-01 16:24:22 +0000 | [diff] [blame] | 305 | /* initialize the current process and fill in the request */ |
Alexandre Julliard | 11ad6a0 | 2005-07-13 19:43:35 +0000 | [diff] [blame] | 306 | size_t init_process( struct thread *thread ) |
Alexandre Julliard | 5b4f3e8 | 2000-05-01 16:24:22 +0000 | [diff] [blame] | 307 | { |
Alexandre Julliard | 0424f38 | 2005-07-13 12:12:43 +0000 | [diff] [blame] | 308 | struct process *process = thread->process; |
Alexandre Julliard | 77c8b1d | 2003-02-24 20:51:50 +0000 | [diff] [blame] | 309 | struct thread *parent_thread = NULL; |
Alexandre Julliard | 5b4f3e8 | 2000-05-01 16:24:22 +0000 | [diff] [blame] | 310 | struct process *parent = NULL; |
Alexandre Julliard | 0424f38 | 2005-07-13 12:12:43 +0000 | [diff] [blame] | 311 | struct startup_info *info; |
Alexandre Julliard | 5b4f3e8 | 2000-05-01 16:24:22 +0000 | [diff] [blame] | 312 | |
Alexandre Julliard | 11ad6a0 | 2005-07-13 19:43:35 +0000 | [diff] [blame] | 313 | if (process->startup_info) return process->startup_info->data_size; /* already initialized */ |
Alexandre Julliard | 0424f38 | 2005-07-13 12:12:43 +0000 | [diff] [blame] | 314 | |
| 315 | if ((info = find_startup_info( thread->unix_pid ))) |
Alexandre Julliard | 5b4f3e8 | 2000-05-01 16:24:22 +0000 | [diff] [blame] | 316 | { |
Alexandre Julliard | 11ad6a0 | 2005-07-13 19:43:35 +0000 | [diff] [blame] | 317 | if (info->thread) return info->data_size; /* already initialized */ |
Alexandre Julliard | 0424f38 | 2005-07-13 12:12:43 +0000 | [diff] [blame] | 318 | |
| 319 | info->thread = (struct thread *)grab_object( thread ); |
| 320 | info->process = (struct process *)grab_object( process ); |
| 321 | process->startup_info = (struct startup_info *)grab_object( info ); |
| 322 | |
Alexandre Julliard | 77c8b1d | 2003-02-24 20:51:50 +0000 | [diff] [blame] | 323 | parent_thread = info->owner; |
| 324 | parent = parent_thread->process; |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 325 | process->parent = (struct process *)grab_object( parent ); |
Alexandre Julliard | 0424f38 | 2005-07-13 12:12:43 +0000 | [diff] [blame] | 326 | |
| 327 | /* set the process flags */ |
| 328 | process->create_flags = info->create_flags; |
| 329 | |
| 330 | if (info->inherit_all) process->handles = copy_handle_table( process, parent ); |
Alexandre Julliard | 5b4f3e8 | 2000-05-01 16:24:22 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Alexandre Julliard | 5b4f3e8 | 2000-05-01 16:24:22 +0000 | [diff] [blame] | 333 | /* create the handle table */ |
Alexandre Julliard | 0424f38 | 2005-07-13 12:12:43 +0000 | [diff] [blame] | 334 | if (!process->handles) process->handles = alloc_handle_table( process, 0 ); |
| 335 | if (!process->handles) |
| 336 | { |
| 337 | fatal_protocol_error( thread, "Failed to allocate handle table\n" ); |
Alexandre Julliard | 11ad6a0 | 2005-07-13 19:43:35 +0000 | [diff] [blame] | 338 | return 0; |
Alexandre Julliard | 0424f38 | 2005-07-13 12:12:43 +0000 | [diff] [blame] | 339 | } |
Alexandre Julliard | 5b4f3e8 | 2000-05-01 16:24:22 +0000 | [diff] [blame] | 340 | |
Alexandre Julliard | 1bf96e0 | 2005-06-08 18:44:50 +0000 | [diff] [blame] | 341 | /* connect to the window station and desktop */ |
| 342 | connect_process_winstation( process, NULL, 0 ); |
Alexandre Julliard | 78a3e63 | 2005-06-09 12:07:12 +0000 | [diff] [blame] | 343 | connect_process_desktop( process, NULL, 0 ); |
Alexandre Julliard | 0424f38 | 2005-07-13 12:12:43 +0000 | [diff] [blame] | 344 | thread->desktop = process->desktop; |
| 345 | |
Alexandre Julliard | 11ad6a0 | 2005-07-13 19:43:35 +0000 | [diff] [blame] | 346 | if (!info) return 0; |
Alexandre Julliard | 0424f38 | 2005-07-13 12:12:43 +0000 | [diff] [blame] | 347 | |
| 348 | /* retrieve the main exe file */ |
| 349 | if (info->exe_file) process->exe.file = (struct file *)grab_object( info->exe_file ); |
| 350 | |
| 351 | /* thread will be actually suspended in init_done */ |
| 352 | if (info->create_flags & CREATE_SUSPENDED) thread->suspend++; |
Alexandre Julliard | 1bf96e0 | 2005-06-08 18:44:50 +0000 | [diff] [blame] | 353 | |
Alexandre Julliard | 5b4f3e8 | 2000-05-01 16:24:22 +0000 | [diff] [blame] | 354 | /* set the process console */ |
Alexandre Julliard | 0424f38 | 2005-07-13 12:12:43 +0000 | [diff] [blame] | 355 | if (!(info->create_flags & (DETACHED_PROCESS | CREATE_NEW_CONSOLE))) |
Alexandre Julliard | 01caa5e | 2005-07-12 20:27:09 +0000 | [diff] [blame] | 356 | { |
| 357 | /* FIXME: some better error checking should be done... |
| 358 | * like if hConOut and hConIn are console handles, then they should be on the same |
| 359 | * physical console |
| 360 | */ |
| 361 | inherit_console( parent_thread, process, info->inherit_all ? info->hstdin : 0 ); |
| 362 | } |
Alexandre Julliard | 5b4f3e8 | 2000-05-01 16:24:22 +0000 | [diff] [blame] | 363 | |
Alexandre Julliard | 0424f38 | 2005-07-13 12:12:43 +0000 | [diff] [blame] | 364 | /* attach to the debugger if requested */ |
| 365 | if (process->create_flags & (DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS)) |
| 366 | set_process_debugger( process, parent_thread ); |
| 367 | else if (parent->debugger && !(parent->create_flags & DEBUG_ONLY_THIS_PROCESS)) |
| 368 | set_process_debugger( process, parent->debugger ); |
Alexandre Julliard | 5b4f3e8 | 2000-05-01 16:24:22 +0000 | [diff] [blame] | 369 | |
Alexandre Julliard | 0424f38 | 2005-07-13 12:12:43 +0000 | [diff] [blame] | 370 | if (!(process->create_flags & CREATE_NEW_PROCESS_GROUP)) |
| 371 | process->group_id = parent->group_id; |
Alexandre Julliard | 11ad6a0 | 2005-07-13 19:43:35 +0000 | [diff] [blame] | 372 | |
| 373 | return info->data_size; |
Alexandre Julliard | 5b4f3e8 | 2000-05-01 16:24:22 +0000 | [diff] [blame] | 374 | } |
| 375 | |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 376 | /* destroy a process when its refcount is 0 */ |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 377 | static void process_destroy( struct object *obj ) |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 378 | { |
| 379 | struct process *process = (struct process *)obj; |
| 380 | assert( obj->ops == &process_ops ); |
| 381 | |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 382 | /* we can't have a thread remaining */ |
Alexandre Julliard | 0502638 | 2005-03-01 10:56:18 +0000 | [diff] [blame] | 383 | assert( list_empty( &process->thread_list )); |
Alexandre Julliard | 9d80215 | 2002-05-24 21:20:27 +0000 | [diff] [blame] | 384 | |
| 385 | set_process_startup_state( process, STARTUP_ABORTED ); |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 386 | if (process->console) release_object( process->console ); |
| 387 | if (process->parent) release_object( process->parent ); |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 388 | if (process->msg_fd) release_object( process->msg_fd ); |
Alexandre Julliard | 48c0d51 | 2005-02-25 19:31:26 +0000 | [diff] [blame] | 389 | list_remove( &process->entry ); |
Alexandre Julliard | c5e433a | 2000-05-30 19:48:18 +0000 | [diff] [blame] | 390 | if (process->idle_event) release_object( process->idle_event ); |
| 391 | if (process->queue) release_object( process->queue ); |
Alexandre Julliard | 05f0b71 | 2000-03-09 18:18:41 +0000 | [diff] [blame] | 392 | if (process->exe.file) release_object( process->exe.file ); |
Alexandre Julliard | aeb5660 | 2002-03-22 00:21:23 +0000 | [diff] [blame] | 393 | if (process->exe.filename) free( process->exe.filename ); |
Alexandre Julliard | 91befe1 | 2003-02-01 01:38:40 +0000 | [diff] [blame] | 394 | if (process->id) free_ptid( process->id ); |
Mike McCormack | 36cd6f5 | 2003-07-24 00:07:00 +0000 | [diff] [blame] | 395 | if (process->token) release_object( process->token ); |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 396 | } |
| 397 | |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 398 | /* dump a process on stdout for debugging purposes */ |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 399 | static void process_dump( struct object *obj, int verbose ) |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 400 | { |
| 401 | struct process *process = (struct process *)obj; |
| 402 | assert( obj->ops == &process_ops ); |
| 403 | |
Alexandre Julliard | 48c0d51 | 2005-02-25 19:31:26 +0000 | [diff] [blame] | 404 | fprintf( stderr, "Process id=%04x handles=%p\n", process->id, process->handles ); |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 407 | static int process_signaled( struct object *obj, struct thread *thread ) |
| 408 | { |
| 409 | struct process *process = (struct process *)obj; |
Alexandre Julliard | dbc033a | 1999-03-13 08:58:24 +0000 | [diff] [blame] | 410 | return !process->running_threads; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 413 | static void process_poll_event( struct fd *fd, int event ) |
Alexandre Julliard | f524240 | 2001-02-28 21:45:23 +0000 | [diff] [blame] | 414 | { |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 415 | struct process *process = get_fd_user( fd ); |
| 416 | assert( process->obj.ops == &process_ops ); |
Alexandre Julliard | f524240 | 2001-02-28 21:45:23 +0000 | [diff] [blame] | 417 | |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 418 | if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 ); |
Alexandre Julliard | f524240 | 2001-02-28 21:45:23 +0000 | [diff] [blame] | 419 | else if (event & POLLIN) receive_fd( process ); |
| 420 | } |
| 421 | |
Alexandre Julliard | 5b4f3e8 | 2000-05-01 16:24:22 +0000 | [diff] [blame] | 422 | static void startup_info_destroy( struct object *obj ) |
| 423 | { |
| 424 | struct startup_info *info = (struct startup_info *)obj; |
| 425 | assert( obj->ops == &startup_info_ops ); |
Alexandre Julliard | 77c8b1d | 2003-02-24 20:51:50 +0000 | [diff] [blame] | 426 | list_remove( &info->entry ); |
Alexandre Julliard | 6543a65 | 2002-03-29 18:28:56 +0000 | [diff] [blame] | 427 | if (info->data) free( info->data ); |
Alexandre Julliard | d27624b | 2000-05-03 18:42:40 +0000 | [diff] [blame] | 428 | if (info->exe_file) release_object( info->exe_file ); |
Alexandre Julliard | 5b4f3e8 | 2000-05-01 16:24:22 +0000 | [diff] [blame] | 429 | if (info->process) release_object( info->process ); |
| 430 | if (info->thread) release_object( info->thread ); |
Alexandre Julliard | 77c8b1d | 2003-02-24 20:51:50 +0000 | [diff] [blame] | 431 | if (info->owner) release_object( info->owner ); |
Alexandre Julliard | 5b4f3e8 | 2000-05-01 16:24:22 +0000 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | static void startup_info_dump( struct object *obj, int verbose ) |
| 435 | { |
| 436 | struct startup_info *info = (struct startup_info *)obj; |
| 437 | assert( obj->ops == &startup_info_ops ); |
| 438 | |
Alexandre Julliard | b3332d7 | 2002-10-19 01:00:59 +0000 | [diff] [blame] | 439 | fprintf( stderr, "Startup info flags=%x in=%p out=%p err=%p\n", |
Alexandre Julliard | ca96de3 | 2002-05-25 21:15:03 +0000 | [diff] [blame] | 440 | info->create_flags, info->hstdin, info->hstdout, info->hstderr ); |
Alexandre Julliard | 5b4f3e8 | 2000-05-01 16:24:22 +0000 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | static int startup_info_signaled( struct object *obj, struct thread *thread ) |
| 444 | { |
| 445 | struct startup_info *info = (struct startup_info *)obj; |
Alexandre Julliard | ca96de3 | 2002-05-25 21:15:03 +0000 | [diff] [blame] | 446 | return info->process && is_process_init_done(info->process); |
Alexandre Julliard | 5b4f3e8 | 2000-05-01 16:24:22 +0000 | [diff] [blame] | 447 | } |
| 448 | |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 449 | /* get a process from an id (and increment the refcount) */ |
Alexandre Julliard | 54f2287 | 2002-10-03 19:54:57 +0000 | [diff] [blame] | 450 | struct process *get_process_from_id( process_id_t id ) |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 451 | { |
Alexandre Julliard | 91befe1 | 2003-02-01 01:38:40 +0000 | [diff] [blame] | 452 | struct object *obj = get_ptid_entry( id ); |
| 453 | |
| 454 | if (obj && obj->ops == &process_ops) return (struct process *)grab_object( obj ); |
| 455 | set_error( STATUS_INVALID_PARAMETER ); |
| 456 | return NULL; |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 457 | } |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 458 | |
| 459 | /* get a process from a handle (and increment the refcount) */ |
Alexandre Julliard | 5188574 | 2002-05-30 20:12:58 +0000 | [diff] [blame] | 460 | struct process *get_process_from_handle( obj_handle_t handle, unsigned int access ) |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 461 | { |
| 462 | return (struct process *)get_handle_obj( current->process, handle, |
| 463 | access, &process_ops ); |
| 464 | } |
| 465 | |
Alexandre Julliard | a9e0fb1 | 2005-03-02 10:20:09 +0000 | [diff] [blame] | 466 | /* find a dll from its base address */ |
| 467 | static inline struct process_dll *find_process_dll( struct process *process, void *base ) |
| 468 | { |
| 469 | struct process_dll *dll; |
| 470 | |
| 471 | if (process->exe.base == base) return &process->exe; |
| 472 | |
| 473 | LIST_FOR_EACH_ENTRY( dll, &process->dlls, struct process_dll, entry ) |
| 474 | { |
| 475 | if (dll->base == base) return dll; |
| 476 | } |
| 477 | return NULL; |
| 478 | } |
| 479 | |
Alexandre Julliard | 05f0b71 | 2000-03-09 18:18:41 +0000 | [diff] [blame] | 480 | /* add a dll to a process list */ |
| 481 | static struct process_dll *process_load_dll( struct process *process, struct file *file, |
Alexandre Julliard | c30cefb | 2003-09-30 01:04:19 +0000 | [diff] [blame] | 482 | void *base, const WCHAR *filename, size_t name_len ) |
Alexandre Julliard | 05f0b71 | 2000-03-09 18:18:41 +0000 | [diff] [blame] | 483 | { |
| 484 | struct process_dll *dll; |
| 485 | |
| 486 | /* make sure we don't already have one with the same base address */ |
Alexandre Julliard | a9e0fb1 | 2005-03-02 10:20:09 +0000 | [diff] [blame] | 487 | if (find_process_dll( process, base )) |
Alexandre Julliard | 05f0b71 | 2000-03-09 18:18:41 +0000 | [diff] [blame] | 488 | { |
| 489 | set_error( STATUS_INVALID_PARAMETER ); |
| 490 | return NULL; |
| 491 | } |
| 492 | |
| 493 | if ((dll = mem_alloc( sizeof(*dll) ))) |
| 494 | { |
Alexandre Julliard | 05f0b71 | 2000-03-09 18:18:41 +0000 | [diff] [blame] | 495 | dll->file = NULL; |
| 496 | dll->base = base; |
Alexandre Julliard | aeb5660 | 2002-03-22 00:21:23 +0000 | [diff] [blame] | 497 | dll->filename = NULL; |
| 498 | dll->namelen = name_len; |
| 499 | if (name_len && !(dll->filename = memdup( filename, name_len ))) |
| 500 | { |
| 501 | free( dll ); |
| 502 | return NULL; |
| 503 | } |
Alexandre Julliard | 05f0b71 | 2000-03-09 18:18:41 +0000 | [diff] [blame] | 504 | if (file) dll->file = (struct file *)grab_object( file ); |
Alexandre Julliard | a9e0fb1 | 2005-03-02 10:20:09 +0000 | [diff] [blame] | 505 | list_add_head( &process->dlls, &dll->entry ); |
Alexandre Julliard | 05f0b71 | 2000-03-09 18:18:41 +0000 | [diff] [blame] | 506 | } |
| 507 | return dll; |
| 508 | } |
| 509 | |
| 510 | /* remove a dll from a process list */ |
| 511 | static void process_unload_dll( struct process *process, void *base ) |
| 512 | { |
Alexandre Julliard | a9e0fb1 | 2005-03-02 10:20:09 +0000 | [diff] [blame] | 513 | struct process_dll *dll = find_process_dll( process, base ); |
Alexandre Julliard | 05f0b71 | 2000-03-09 18:18:41 +0000 | [diff] [blame] | 514 | |
Alexandre Julliard | a9e0fb1 | 2005-03-02 10:20:09 +0000 | [diff] [blame] | 515 | if (dll && dll != &process->exe) |
Alexandre Julliard | 05f0b71 | 2000-03-09 18:18:41 +0000 | [diff] [blame] | 516 | { |
Alexandre Julliard | a9e0fb1 | 2005-03-02 10:20:09 +0000 | [diff] [blame] | 517 | if (dll->file) release_object( dll->file ); |
| 518 | if (dll->filename) free( dll->filename ); |
| 519 | list_remove( &dll->entry ); |
| 520 | free( dll ); |
| 521 | generate_debug_event( current, UNLOAD_DLL_DEBUG_EVENT, base ); |
Alexandre Julliard | 05f0b71 | 2000-03-09 18:18:41 +0000 | [diff] [blame] | 522 | } |
Alexandre Julliard | a9e0fb1 | 2005-03-02 10:20:09 +0000 | [diff] [blame] | 523 | else set_error( STATUS_INVALID_PARAMETER ); |
Alexandre Julliard | 05f0b71 | 2000-03-09 18:18:41 +0000 | [diff] [blame] | 524 | } |
| 525 | |
Alexandre Julliard | 40043ed | 2002-08-16 20:02:15 +0000 | [diff] [blame] | 526 | /* kill all processes */ |
| 527 | void kill_all_processes( struct process *skip, int exit_code ) |
| 528 | { |
| 529 | for (;;) |
| 530 | { |
Alexandre Julliard | 48c0d51 | 2005-02-25 19:31:26 +0000 | [diff] [blame] | 531 | struct process *process; |
Alexandre Julliard | 40043ed | 2002-08-16 20:02:15 +0000 | [diff] [blame] | 532 | |
Alexandre Julliard | 48c0d51 | 2005-02-25 19:31:26 +0000 | [diff] [blame] | 533 | LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry ) |
| 534 | { |
| 535 | if (process == skip) continue; |
| 536 | if (process->running_threads) break; |
| 537 | } |
| 538 | if (&process->entry == &process_list) break; /* no process found */ |
Alexandre Julliard | 40043ed | 2002-08-16 20:02:15 +0000 | [diff] [blame] | 539 | kill_process( process, NULL, exit_code ); |
| 540 | } |
| 541 | } |
| 542 | |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 543 | /* kill all processes being attached to a console renderer */ |
Eric Pouech | 3940d8a | 2001-12-04 20:17:43 +0000 | [diff] [blame] | 544 | void kill_console_processes( struct thread *renderer, int exit_code ) |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 545 | { |
| 546 | for (;;) /* restart from the beginning of the list every time */ |
| 547 | { |
Alexandre Julliard | 48c0d51 | 2005-02-25 19:31:26 +0000 | [diff] [blame] | 548 | struct process *process; |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 549 | |
| 550 | /* find the first process being attached to 'renderer' and still running */ |
Alexandre Julliard | 48c0d51 | 2005-02-25 19:31:26 +0000 | [diff] [blame] | 551 | LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry ) |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 552 | { |
Alexandre Julliard | 48c0d51 | 2005-02-25 19:31:26 +0000 | [diff] [blame] | 553 | if (process == renderer->process) continue; |
| 554 | if (!process->running_threads) continue; |
| 555 | if (process->console && process->console->renderer == renderer) break; |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 556 | } |
Alexandre Julliard | 48c0d51 | 2005-02-25 19:31:26 +0000 | [diff] [blame] | 557 | if (&process->entry == &process_list) break; /* no process found */ |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 558 | kill_process( process, NULL, exit_code ); |
| 559 | } |
| 560 | } |
| 561 | |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 562 | /* a process has been killed (i.e. its last thread died) */ |
Alexandre Julliard | 05f0b71 | 2000-03-09 18:18:41 +0000 | [diff] [blame] | 563 | static void process_killed( struct process *process ) |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 564 | { |
Alexandre Julliard | 36b85d0 | 2005-06-29 19:29:15 +0000 | [diff] [blame] | 565 | struct handle_table *handles; |
Alexandre Julliard | a9e0fb1 | 2005-03-02 10:20:09 +0000 | [diff] [blame] | 566 | struct list *ptr; |
| 567 | |
Alexandre Julliard | 0502638 | 2005-03-01 10:56:18 +0000 | [diff] [blame] | 568 | assert( list_empty( &process->thread_list )); |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 569 | gettimeofday( &process->end_time, NULL ); |
Alexandre Julliard | 36b85d0 | 2005-06-29 19:29:15 +0000 | [diff] [blame] | 570 | handles = process->handles; |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 571 | process->handles = NULL; |
Alexandre Julliard | 36b85d0 | 2005-06-29 19:29:15 +0000 | [diff] [blame] | 572 | if (handles) release_object( handles ); |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 573 | |
| 574 | /* close the console attached to this process, if any */ |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 575 | free_console( process ); |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 576 | |
Alexandre Julliard | a9e0fb1 | 2005-03-02 10:20:09 +0000 | [diff] [blame] | 577 | while ((ptr = list_head( &process->dlls ))) |
Alexandre Julliard | 05f0b71 | 2000-03-09 18:18:41 +0000 | [diff] [blame] | 578 | { |
Alexandre Julliard | a9e0fb1 | 2005-03-02 10:20:09 +0000 | [diff] [blame] | 579 | struct process_dll *dll = LIST_ENTRY( ptr, struct process_dll, entry ); |
Alexandre Julliard | 05f0b71 | 2000-03-09 18:18:41 +0000 | [diff] [blame] | 580 | if (dll->file) release_object( dll->file ); |
Alexandre Julliard | aeb5660 | 2002-03-22 00:21:23 +0000 | [diff] [blame] | 581 | if (dll->filename) free( dll->filename ); |
Alexandre Julliard | a9e0fb1 | 2005-03-02 10:20:09 +0000 | [diff] [blame] | 582 | list_remove( &dll->entry ); |
Alexandre Julliard | 05f0b71 | 2000-03-09 18:18:41 +0000 | [diff] [blame] | 583 | free( dll ); |
| 584 | } |
Alexandre Julliard | bfce151 | 2003-12-10 04:08:06 +0000 | [diff] [blame] | 585 | destroy_process_classes( process ); |
Alexandre Julliard | 5f22e7c | 2005-06-14 19:23:56 +0000 | [diff] [blame] | 586 | remove_process_locks( process ); |
Alexandre Julliard | 9d80215 | 2002-05-24 21:20:27 +0000 | [diff] [blame] | 587 | set_process_startup_state( process, STARTUP_ABORTED ); |
Alexandre Julliard | 05f0b71 | 2000-03-09 18:18:41 +0000 | [diff] [blame] | 588 | if (process->exe.file) release_object( process->exe.file ); |
| 589 | process->exe.file = NULL; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 590 | wake_up( &process->obj, 0 ); |
Alexandre Julliard | 4144b5b | 2002-06-20 23:21:27 +0000 | [diff] [blame] | 591 | if (!--running_processes) close_master_socket(); |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 594 | /* add a thread to a process running threads list */ |
| 595 | void add_process_thread( struct process *process, struct thread *thread ) |
| 596 | { |
Alexandre Julliard | 0502638 | 2005-03-01 10:56:18 +0000 | [diff] [blame] | 597 | list_add_head( &process->thread_list, &thread->proc_entry ); |
Alexandre Julliard | fdc92ba | 1999-02-14 18:03:15 +0000 | [diff] [blame] | 598 | if (!process->running_threads++) running_processes++; |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 599 | grab_object( thread ); |
| 600 | } |
| 601 | |
| 602 | /* remove a thread from a process running threads list */ |
| 603 | void remove_process_thread( struct process *process, struct thread *thread ) |
| 604 | { |
| 605 | assert( process->running_threads > 0 ); |
Alexandre Julliard | 0502638 | 2005-03-01 10:56:18 +0000 | [diff] [blame] | 606 | assert( !list_empty( &process->thread_list )); |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 607 | |
Alexandre Julliard | 0502638 | 2005-03-01 10:56:18 +0000 | [diff] [blame] | 608 | list_remove( &thread->proc_entry ); |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 609 | |
| 610 | if (!--process->running_threads) |
| 611 | { |
| 612 | /* we have removed the last running thread, exit the process */ |
Alexandre Julliard | 05f0b71 | 2000-03-09 18:18:41 +0000 | [diff] [blame] | 613 | process->exit_code = thread->exit_code; |
| 614 | generate_debug_event( thread, EXIT_PROCESS_DEBUG_EVENT, process ); |
| 615 | process_killed( process ); |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 616 | } |
Alexandre Julliard | 05f0b71 | 2000-03-09 18:18:41 +0000 | [diff] [blame] | 617 | else generate_debug_event( thread, EXIT_THREAD_DEBUG_EVENT, thread ); |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 618 | release_object( thread ); |
| 619 | } |
| 620 | |
Alexandre Julliard | 3da5f84 | 1999-05-16 16:54:54 +0000 | [diff] [blame] | 621 | /* suspend all the threads of a process */ |
| 622 | void suspend_process( struct process *process ) |
| 623 | { |
| 624 | if (!process->suspend++) |
| 625 | { |
Alexandre Julliard | 0502638 | 2005-03-01 10:56:18 +0000 | [diff] [blame] | 626 | struct list *ptr, *next; |
| 627 | |
| 628 | LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list ) |
Alexandre Julliard | 3da5f84 | 1999-05-16 16:54:54 +0000 | [diff] [blame] | 629 | { |
Alexandre Julliard | 0502638 | 2005-03-01 10:56:18 +0000 | [diff] [blame] | 630 | struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry ); |
Alexandre Julliard | 0a70783 | 1999-11-08 05:31:47 +0000 | [diff] [blame] | 631 | if (!thread->suspend) stop_thread( thread ); |
Alexandre Julliard | 3da5f84 | 1999-05-16 16:54:54 +0000 | [diff] [blame] | 632 | } |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | /* resume all the threads of a process */ |
| 637 | void resume_process( struct process *process ) |
| 638 | { |
| 639 | assert (process->suspend > 0); |
| 640 | if (!--process->suspend) |
| 641 | { |
Alexandre Julliard | 0502638 | 2005-03-01 10:56:18 +0000 | [diff] [blame] | 642 | struct list *ptr, *next; |
| 643 | |
| 644 | LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list ) |
Alexandre Julliard | 3da5f84 | 1999-05-16 16:54:54 +0000 | [diff] [blame] | 645 | { |
Alexandre Julliard | 0502638 | 2005-03-01 10:56:18 +0000 | [diff] [blame] | 646 | struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry ); |
Alexandre Julliard | d04ccb8 | 2003-03-04 22:18:43 +0000 | [diff] [blame] | 647 | if (!thread->suspend) wake_thread( thread ); |
Alexandre Julliard | 3da5f84 | 1999-05-16 16:54:54 +0000 | [diff] [blame] | 648 | } |
| 649 | } |
| 650 | } |
| 651 | |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 652 | /* kill a process on the spot */ |
Alexandre Julliard | f524240 | 2001-02-28 21:45:23 +0000 | [diff] [blame] | 653 | void kill_process( struct process *process, struct thread *skip, int exit_code ) |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 654 | { |
Alexandre Julliard | 0502638 | 2005-03-01 10:56:18 +0000 | [diff] [blame] | 655 | struct list *ptr, *next; |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 656 | |
Alexandre Julliard | 0502638 | 2005-03-01 10:56:18 +0000 | [diff] [blame] | 657 | LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list ) |
Alexandre Julliard | 12f29b5 | 2000-03-17 15:16:57 +0000 | [diff] [blame] | 658 | { |
Alexandre Julliard | 0502638 | 2005-03-01 10:56:18 +0000 | [diff] [blame] | 659 | struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry ); |
| 660 | |
Alexandre Julliard | 12f29b5 | 2000-03-17 15:16:57 +0000 | [diff] [blame] | 661 | thread->exit_code = exit_code; |
| 662 | if (thread != skip) kill_thread( thread, 1 ); |
Alexandre Julliard | 12f29b5 | 2000-03-17 15:16:57 +0000 | [diff] [blame] | 663 | } |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 664 | } |
| 665 | |
Alexandre Julliard | 17cf810 | 1999-11-24 01:22:14 +0000 | [diff] [blame] | 666 | /* kill all processes being debugged by a given thread */ |
| 667 | void kill_debugged_processes( struct thread *debugger, int exit_code ) |
| 668 | { |
| 669 | for (;;) /* restart from the beginning of the list every time */ |
| 670 | { |
Alexandre Julliard | 48c0d51 | 2005-02-25 19:31:26 +0000 | [diff] [blame] | 671 | struct process *process; |
| 672 | |
Alexandre Julliard | 17cf810 | 1999-11-24 01:22:14 +0000 | [diff] [blame] | 673 | /* find the first process being debugged by 'debugger' and still running */ |
Alexandre Julliard | 48c0d51 | 2005-02-25 19:31:26 +0000 | [diff] [blame] | 674 | LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry ) |
| 675 | { |
| 676 | if (!process->running_threads) continue; |
| 677 | if (process->debugger == debugger) break; |
| 678 | } |
| 679 | if (&process->entry == &process_list) break; /* no process found */ |
Alexandre Julliard | 17cf810 | 1999-11-24 01:22:14 +0000 | [diff] [blame] | 680 | process->debugger = NULL; |
Alexandre Julliard | 12f29b5 | 2000-03-17 15:16:57 +0000 | [diff] [blame] | 681 | kill_process( process, NULL, exit_code ); |
Alexandre Julliard | 17cf810 | 1999-11-24 01:22:14 +0000 | [diff] [blame] | 682 | } |
| 683 | } |
| 684 | |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 685 | |
Eric Pouech | fbccb38 | 2002-02-27 01:28:30 +0000 | [diff] [blame] | 686 | /* detach a debugger from all its debuggees */ |
| 687 | void detach_debugged_processes( struct thread *debugger ) |
| 688 | { |
| 689 | struct process *process; |
Alexandre Julliard | 48c0d51 | 2005-02-25 19:31:26 +0000 | [diff] [blame] | 690 | |
| 691 | LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry ) |
Eric Pouech | fbccb38 | 2002-02-27 01:28:30 +0000 | [diff] [blame] | 692 | { |
| 693 | if (process->debugger == debugger && process->running_threads) |
| 694 | { |
| 695 | debugger_detach( process, debugger ); |
| 696 | } |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | |
Eric Pouech | 93bfa0d | 2002-06-02 21:22:22 +0000 | [diff] [blame] | 701 | void enum_processes( int (*cb)(struct process*, void*), void *user ) |
| 702 | { |
Alexandre Julliard | 48c0d51 | 2005-02-25 19:31:26 +0000 | [diff] [blame] | 703 | struct list *ptr, *next; |
| 704 | |
| 705 | LIST_FOR_EACH_SAFE( ptr, next, &process_list ) |
Eric Pouech | 93bfa0d | 2002-06-02 21:22:22 +0000 | [diff] [blame] | 706 | { |
Alexandre Julliard | 48c0d51 | 2005-02-25 19:31:26 +0000 | [diff] [blame] | 707 | struct process *process = LIST_ENTRY( ptr, struct process, entry ); |
Eric Pouech | 93bfa0d | 2002-06-02 21:22:22 +0000 | [diff] [blame] | 708 | if ((cb)(process, user)) break; |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | |
Alexandre Julliard | 8b8828f | 1999-11-12 21:39:14 +0000 | [diff] [blame] | 713 | /* read data from a process memory space */ |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 714 | /* len is the total size (in ints) */ |
| 715 | static int read_process_memory( struct process *process, const int *addr, size_t len, int *dest ) |
Alexandre Julliard | 8b8828f | 1999-11-12 21:39:14 +0000 | [diff] [blame] | 716 | { |
Alexandre Julliard | 0502638 | 2005-03-01 10:56:18 +0000 | [diff] [blame] | 717 | struct thread *thread = get_process_first_thread( process ); |
Alexandre Julliard | 8b8828f | 1999-11-12 21:39:14 +0000 | [diff] [blame] | 718 | |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 719 | assert( !((unsigned int)addr % sizeof(int)) ); /* address must be aligned */ |
| 720 | |
Alexandre Julliard | 00641d5 | 2000-03-08 16:41:37 +0000 | [diff] [blame] | 721 | if (!thread) /* process is dead */ |
| 722 | { |
| 723 | set_error( STATUS_ACCESS_DENIED ); |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 724 | return 0; |
Alexandre Julliard | 00641d5 | 2000-03-08 16:41:37 +0000 | [diff] [blame] | 725 | } |
Alexandre Julliard | 98aacc7 | 2000-03-15 19:46:14 +0000 | [diff] [blame] | 726 | if (suspend_for_ptrace( thread )) |
Alexandre Julliard | 8b8828f | 1999-11-12 21:39:14 +0000 | [diff] [blame] | 727 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 728 | while (len > 0) |
Alexandre Julliard | 8b8828f | 1999-11-12 21:39:14 +0000 | [diff] [blame] | 729 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 730 | if (read_thread_int( thread, addr++, dest++ ) == -1) break; |
Alexandre Julliard | eef7025 | 1999-11-13 19:54:54 +0000 | [diff] [blame] | 731 | len--; |
| 732 | } |
Alexandre Julliard | d04ccb8 | 2003-03-04 22:18:43 +0000 | [diff] [blame] | 733 | resume_after_ptrace( thread ); |
Alexandre Julliard | 8b8828f | 1999-11-12 21:39:14 +0000 | [diff] [blame] | 734 | } |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 735 | return !len; |
| 736 | } |
| 737 | |
| 738 | /* make sure we can write to the whole address range */ |
| 739 | /* len is the total size (in ints) */ |
| 740 | static int check_process_write_access( struct thread *thread, int *addr, size_t len ) |
| 741 | { |
| 742 | int page = get_page_size() / sizeof(int); |
| 743 | |
| 744 | for (;;) |
| 745 | { |
| 746 | if (write_thread_int( thread, addr, 0, 0 ) == -1) return 0; |
| 747 | if (len <= page) break; |
| 748 | addr += page; |
| 749 | len -= page; |
| 750 | } |
| 751 | return (write_thread_int( thread, addr + len - 1, 0, 0 ) != -1); |
Alexandre Julliard | eef7025 | 1999-11-13 19:54:54 +0000 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | /* write data to a process memory space */ |
| 755 | /* len is the total size (in ints), max is the size we can actually read from the input buffer */ |
| 756 | /* we check the total size for write permissions */ |
Alexandre Julliard | e55d593 | 2003-10-14 01:30:42 +0000 | [diff] [blame] | 757 | static int write_process_memory( struct process *process, int *addr, size_t len, |
| 758 | unsigned int first_mask, unsigned int last_mask, const int *src ) |
Alexandre Julliard | eef7025 | 1999-11-13 19:54:54 +0000 | [diff] [blame] | 759 | { |
Alexandre Julliard | 0502638 | 2005-03-01 10:56:18 +0000 | [diff] [blame] | 760 | struct thread *thread = get_process_first_thread( process ); |
Alexandre Julliard | e55d593 | 2003-10-14 01:30:42 +0000 | [diff] [blame] | 761 | int ret = 0; |
Alexandre Julliard | eef7025 | 1999-11-13 19:54:54 +0000 | [diff] [blame] | 762 | |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 763 | assert( !((unsigned int)addr % sizeof(int) )); /* address must be aligned */ |
| 764 | |
Alexandre Julliard | 00641d5 | 2000-03-08 16:41:37 +0000 | [diff] [blame] | 765 | if (!thread) /* process is dead */ |
| 766 | { |
| 767 | set_error( STATUS_ACCESS_DENIED ); |
Alexandre Julliard | e55d593 | 2003-10-14 01:30:42 +0000 | [diff] [blame] | 768 | return 0; |
Alexandre Julliard | 00641d5 | 2000-03-08 16:41:37 +0000 | [diff] [blame] | 769 | } |
Alexandre Julliard | 98aacc7 | 2000-03-15 19:46:14 +0000 | [diff] [blame] | 770 | if (suspend_for_ptrace( thread )) |
Alexandre Julliard | eef7025 | 1999-11-13 19:54:54 +0000 | [diff] [blame] | 771 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 772 | if (!check_process_write_access( thread, addr, len )) |
| 773 | { |
| 774 | set_error( STATUS_ACCESS_DENIED ); |
Peter Hunnisett | a3c5ad4 | 2003-02-28 21:50:47 +0000 | [diff] [blame] | 775 | goto done; |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 776 | } |
Alexandre Julliard | eef7025 | 1999-11-13 19:54:54 +0000 | [diff] [blame] | 777 | /* first word is special */ |
| 778 | if (len > 1) |
| 779 | { |
Alexandre Julliard | 578c100 | 1999-11-14 21:23:21 +0000 | [diff] [blame] | 780 | if (write_thread_int( thread, addr++, *src++, first_mask ) == -1) goto done; |
Alexandre Julliard | eef7025 | 1999-11-13 19:54:54 +0000 | [diff] [blame] | 781 | len--; |
Alexandre Julliard | eef7025 | 1999-11-13 19:54:54 +0000 | [diff] [blame] | 782 | } |
| 783 | else last_mask &= first_mask; |
| 784 | |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 785 | while (len > 1) |
Alexandre Julliard | eef7025 | 1999-11-13 19:54:54 +0000 | [diff] [blame] | 786 | { |
Alexandre Julliard | 578c100 | 1999-11-14 21:23:21 +0000 | [diff] [blame] | 787 | if (write_thread_int( thread, addr++, *src++, ~0 ) == -1) goto done; |
Alexandre Julliard | eef7025 | 1999-11-13 19:54:54 +0000 | [diff] [blame] | 788 | len--; |
| 789 | } |
| 790 | |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 791 | /* last word is special too */ |
| 792 | if (write_thread_int( thread, addr, *src, last_mask ) == -1) goto done; |
Alexandre Julliard | e55d593 | 2003-10-14 01:30:42 +0000 | [diff] [blame] | 793 | ret = 1; |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 794 | |
Alexandre Julliard | 98aacc7 | 2000-03-15 19:46:14 +0000 | [diff] [blame] | 795 | done: |
Alexandre Julliard | d04ccb8 | 2003-03-04 22:18:43 +0000 | [diff] [blame] | 796 | resume_after_ptrace( thread ); |
Alexandre Julliard | eef7025 | 1999-11-13 19:54:54 +0000 | [diff] [blame] | 797 | } |
Alexandre Julliard | e55d593 | 2003-10-14 01:30:42 +0000 | [diff] [blame] | 798 | return ret; |
| 799 | } |
| 800 | |
| 801 | /* set the debugged flag in the process PEB */ |
| 802 | int set_process_debug_flag( struct process *process, int flag ) |
| 803 | { |
| 804 | int mask = 0, data = 0; |
| 805 | |
| 806 | /* BeingDebugged flag is the byte at offset 2 in the PEB */ |
| 807 | memset( (char *)&mask + 2, 0xff, 1 ); |
| 808 | memset( (char *)&data + 2, flag, 1 ); |
| 809 | return write_process_memory( process, process->peb, 1, mask, mask, &data ); |
Alexandre Julliard | 8b8828f | 1999-11-12 21:39:14 +0000 | [diff] [blame] | 810 | } |
| 811 | |
Alexandre Julliard | fdc92ba | 1999-02-14 18:03:15 +0000 | [diff] [blame] | 812 | /* take a snapshot of currently running processes */ |
| 813 | struct process_snapshot *process_snap( int *count ) |
| 814 | { |
| 815 | struct process_snapshot *snapshot, *ptr; |
| 816 | struct process *process; |
| 817 | if (!running_processes) return NULL; |
| 818 | if (!(snapshot = mem_alloc( sizeof(*snapshot) * running_processes ))) |
| 819 | return NULL; |
| 820 | ptr = snapshot; |
Alexandre Julliard | 48c0d51 | 2005-02-25 19:31:26 +0000 | [diff] [blame] | 821 | LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry ) |
Alexandre Julliard | fdc92ba | 1999-02-14 18:03:15 +0000 | [diff] [blame] | 822 | { |
| 823 | if (!process->running_threads) continue; |
| 824 | ptr->process = process; |
| 825 | ptr->threads = process->running_threads; |
Alexandre Julliard | 07d8446 | 2000-04-16 19:45:05 +0000 | [diff] [blame] | 826 | ptr->count = process->obj.refcount; |
Alexandre Julliard | fdc92ba | 1999-02-14 18:03:15 +0000 | [diff] [blame] | 827 | ptr->priority = process->priority; |
Eric Pouech | 9fd54b2 | 2003-09-16 01:07:21 +0000 | [diff] [blame] | 828 | ptr->handles = get_handle_table_count(process); |
Alexandre Julliard | fdc92ba | 1999-02-14 18:03:15 +0000 | [diff] [blame] | 829 | grab_object( process ); |
| 830 | ptr++; |
| 831 | } |
| 832 | *count = running_processes; |
| 833 | return snapshot; |
| 834 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 835 | |
Alexandre Julliard | 07d8446 | 2000-04-16 19:45:05 +0000 | [diff] [blame] | 836 | /* take a snapshot of the modules of a process */ |
| 837 | struct module_snapshot *module_snap( struct process *process, int *count ) |
| 838 | { |
| 839 | struct module_snapshot *snapshot, *ptr; |
| 840 | struct process_dll *dll; |
Alexandre Julliard | a9e0fb1 | 2005-03-02 10:20:09 +0000 | [diff] [blame] | 841 | int total = 1; |
Alexandre Julliard | 07d8446 | 2000-04-16 19:45:05 +0000 | [diff] [blame] | 842 | |
Alexandre Julliard | a9e0fb1 | 2005-03-02 10:20:09 +0000 | [diff] [blame] | 843 | LIST_FOR_EACH_ENTRY( dll, &process->dlls, struct process_dll, entry ) total++; |
Alexandre Julliard | 07d8446 | 2000-04-16 19:45:05 +0000 | [diff] [blame] | 844 | if (!(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL; |
| 845 | |
Alexandre Julliard | a9e0fb1 | 2005-03-02 10:20:09 +0000 | [diff] [blame] | 846 | /* first entry is main exe */ |
| 847 | snapshot->base = process->exe.base; |
| 848 | snapshot->size = process->exe.size; |
| 849 | snapshot->namelen = process->exe.namelen; |
| 850 | snapshot->filename = memdup( process->exe.filename, process->exe.namelen ); |
| 851 | ptr = snapshot + 1; |
| 852 | |
| 853 | LIST_FOR_EACH_ENTRY( dll, &process->dlls, struct process_dll, entry ) |
Alexandre Julliard | 07d8446 | 2000-04-16 19:45:05 +0000 | [diff] [blame] | 854 | { |
Alexandre Julliard | aeb5660 | 2002-03-22 00:21:23 +0000 | [diff] [blame] | 855 | ptr->base = dll->base; |
| 856 | ptr->size = dll->size; |
| 857 | ptr->namelen = dll->namelen; |
| 858 | ptr->filename = memdup( dll->filename, dll->namelen ); |
Alexandre Julliard | a9e0fb1 | 2005-03-02 10:20:09 +0000 | [diff] [blame] | 859 | ptr++; |
Alexandre Julliard | 07d8446 | 2000-04-16 19:45:05 +0000 | [diff] [blame] | 860 | } |
| 861 | *count = total; |
| 862 | return snapshot; |
| 863 | } |
| 864 | |
| 865 | |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 866 | /* create a new process */ |
| 867 | DECL_HANDLER(new_process) |
| 868 | { |
Alexandre Julliard | 5b4f3e8 | 2000-05-01 16:24:22 +0000 | [diff] [blame] | 869 | struct startup_info *info; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 870 | |
Alexandre Julliard | 5b4f3e8 | 2000-05-01 16:24:22 +0000 | [diff] [blame] | 871 | /* build the startup info for a new process */ |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 872 | if (!(info = alloc_object( &startup_info_ops ))) return; |
Alexandre Julliard | 77c8b1d | 2003-02-24 20:51:50 +0000 | [diff] [blame] | 873 | list_add_head( &startup_info_list, &info->entry ); |
Alexandre Julliard | 5b4f3e8 | 2000-05-01 16:24:22 +0000 | [diff] [blame] | 874 | info->inherit_all = req->inherit_all; |
| 875 | info->create_flags = req->create_flags; |
Alexandre Julliard | 77c8b1d | 2003-02-24 20:51:50 +0000 | [diff] [blame] | 876 | info->unix_pid = req->unix_pid; |
Alexandre Julliard | 5b4f3e8 | 2000-05-01 16:24:22 +0000 | [diff] [blame] | 877 | info->hstdin = req->hstdin; |
| 878 | info->hstdout = req->hstdout; |
| 879 | info->hstderr = req->hstderr; |
Alexandre Julliard | d27624b | 2000-05-03 18:42:40 +0000 | [diff] [blame] | 880 | info->exe_file = NULL; |
Alexandre Julliard | e9936d9 | 2001-01-26 00:22:26 +0000 | [diff] [blame] | 881 | info->owner = (struct thread *)grab_object( current ); |
Alexandre Julliard | 5b4f3e8 | 2000-05-01 16:24:22 +0000 | [diff] [blame] | 882 | info->process = NULL; |
| 883 | info->thread = NULL; |
Alexandre Julliard | 6543a65 | 2002-03-29 18:28:56 +0000 | [diff] [blame] | 884 | info->data_size = get_req_data_size(); |
| 885 | info->data = NULL; |
Alexandre Julliard | 6a72dc5 | 2000-04-14 13:42:00 +0000 | [diff] [blame] | 886 | |
Alexandre Julliard | 8081e5a | 2001-01-05 04:08:07 +0000 | [diff] [blame] | 887 | if (req->exe_file && |
Alexandre Julliard | d27624b | 2000-05-03 18:42:40 +0000 | [diff] [blame] | 888 | !(info->exe_file = get_file_obj( current->process, req->exe_file, GENERIC_READ ))) |
Alexandre Julliard | e9936d9 | 2001-01-26 00:22:26 +0000 | [diff] [blame] | 889 | goto done; |
Alexandre Julliard | d27624b | 2000-05-03 18:42:40 +0000 | [diff] [blame] | 890 | |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 891 | if (!(info->data = memdup( get_req_data(), info->data_size ))) goto done; |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 892 | reply->info = alloc_handle( current->process, info, SYNCHRONIZE, FALSE ); |
Alexandre Julliard | e9936d9 | 2001-01-26 00:22:26 +0000 | [diff] [blame] | 893 | |
| 894 | done: |
| 895 | release_object( info ); |
Alexandre Julliard | 5b4f3e8 | 2000-05-01 16:24:22 +0000 | [diff] [blame] | 896 | } |
Alexandre Julliard | 6a72dc5 | 2000-04-14 13:42:00 +0000 | [diff] [blame] | 897 | |
Alexandre Julliard | e9936d9 | 2001-01-26 00:22:26 +0000 | [diff] [blame] | 898 | /* Retrieve information about a newly started process */ |
| 899 | DECL_HANDLER(get_new_process_info) |
Alexandre Julliard | 5b4f3e8 | 2000-05-01 16:24:22 +0000 | [diff] [blame] | 900 | { |
Alexandre Julliard | e9936d9 | 2001-01-26 00:22:26 +0000 | [diff] [blame] | 901 | struct startup_info *info; |
| 902 | |
Alexandre Julliard | e9936d9 | 2001-01-26 00:22:26 +0000 | [diff] [blame] | 903 | if ((info = (struct startup_info *)get_handle_obj( current->process, req->info, |
| 904 | 0, &startup_info_ops ))) |
Alexandre Julliard | 5b4f3e8 | 2000-05-01 16:24:22 +0000 | [diff] [blame] | 905 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 906 | reply->pid = get_process_id( info->process ); |
| 907 | reply->tid = get_thread_id( info->thread ); |
| 908 | reply->phandle = alloc_handle( current->process, info->process, |
| 909 | PROCESS_ALL_ACCESS, req->pinherit ); |
| 910 | reply->thandle = alloc_handle( current->process, info->thread, |
| 911 | THREAD_ALL_ACCESS, req->tinherit ); |
Alexandre Julliard | ca96de3 | 2002-05-25 21:15:03 +0000 | [diff] [blame] | 912 | reply->success = is_process_init_done( info->process ); |
Alexandre Julliard | e9936d9 | 2001-01-26 00:22:26 +0000 | [diff] [blame] | 913 | release_object( info ); |
Alexandre Julliard | 5b4f3e8 | 2000-05-01 16:24:22 +0000 | [diff] [blame] | 914 | } |
| 915 | else |
| 916 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 917 | reply->pid = 0; |
| 918 | reply->tid = 0; |
| 919 | reply->phandle = 0; |
| 920 | reply->thandle = 0; |
Alexandre Julliard | 9d80215 | 2002-05-24 21:20:27 +0000 | [diff] [blame] | 921 | reply->success = 0; |
Alexandre Julliard | 5b4f3e8 | 2000-05-01 16:24:22 +0000 | [diff] [blame] | 922 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 923 | } |
| 924 | |
Alexandre Julliard | 6543a65 | 2002-03-29 18:28:56 +0000 | [diff] [blame] | 925 | /* Retrieve the new process startup info */ |
| 926 | DECL_HANDLER(get_startup_info) |
| 927 | { |
Alexandre Julliard | 01caa5e | 2005-07-12 20:27:09 +0000 | [diff] [blame] | 928 | struct process *process = current->process; |
| 929 | struct startup_info *info = process->startup_info; |
| 930 | size_t size; |
Alexandre Julliard | 6543a65 | 2002-03-29 18:28:56 +0000 | [diff] [blame] | 931 | |
Alexandre Julliard | 01caa5e | 2005-07-12 20:27:09 +0000 | [diff] [blame] | 932 | if (!info) return; |
| 933 | |
| 934 | reply->create_flags = info->create_flags; |
| 935 | |
| 936 | if (info->exe_file && |
| 937 | !(reply->exe_file = alloc_handle( process, info->exe_file, GENERIC_READ, 0 ))) return; |
| 938 | |
| 939 | if (!info->inherit_all && !(info->create_flags & CREATE_NEW_CONSOLE)) |
Alexandre Julliard | 6543a65 | 2002-03-29 18:28:56 +0000 | [diff] [blame] | 940 | { |
Alexandre Julliard | 01caa5e | 2005-07-12 20:27:09 +0000 | [diff] [blame] | 941 | struct process *parent_process = info->owner->process; |
| 942 | reply->hstdin = duplicate_handle( parent_process, info->hstdin, process, |
| 943 | 0, TRUE, DUPLICATE_SAME_ACCESS ); |
| 944 | reply->hstdout = duplicate_handle( parent_process, info->hstdout, process, |
| 945 | 0, TRUE, DUPLICATE_SAME_ACCESS ); |
| 946 | reply->hstderr = duplicate_handle( parent_process, info->hstderr, process, |
| 947 | 0, TRUE, DUPLICATE_SAME_ACCESS ); |
| 948 | /* some handles above may have been invalid; this is not an error */ |
| 949 | if (get_error() == STATUS_INVALID_HANDLE || |
| 950 | get_error() == STATUS_OBJECT_TYPE_MISMATCH) clear_error(); |
Alexandre Julliard | 6543a65 | 2002-03-29 18:28:56 +0000 | [diff] [blame] | 951 | } |
Alexandre Julliard | 01caa5e | 2005-07-12 20:27:09 +0000 | [diff] [blame] | 952 | else |
| 953 | { |
| 954 | reply->hstdin = info->hstdin; |
| 955 | reply->hstdout = info->hstdout; |
| 956 | reply->hstderr = info->hstderr; |
| 957 | } |
| 958 | |
| 959 | /* we return the data directly without making a copy so this can only be called once */ |
| 960 | size = info->data_size; |
| 961 | if (size > get_reply_max_size()) size = get_reply_max_size(); |
| 962 | set_reply_data_ptr( info->data, size ); |
| 963 | info->data = NULL; |
| 964 | info->data_size = 0; |
Alexandre Julliard | 6543a65 | 2002-03-29 18:28:56 +0000 | [diff] [blame] | 965 | } |
| 966 | |
Alexandre Julliard | ec7bb23 | 1999-11-12 03:35:25 +0000 | [diff] [blame] | 967 | /* signal the end of the process initialization */ |
| 968 | DECL_HANDLER(init_process_done) |
| 969 | { |
Alexandre Julliard | ac2e4f1 | 2001-10-25 19:52:12 +0000 | [diff] [blame] | 970 | struct file *file = NULL; |
Alexandre Julliard | ec7bb23 | 1999-11-12 03:35:25 +0000 | [diff] [blame] | 971 | struct process *process = current->process; |
Alexandre Julliard | ac2e4f1 | 2001-10-25 19:52:12 +0000 | [diff] [blame] | 972 | |
Alexandre Julliard | 9d80215 | 2002-05-24 21:20:27 +0000 | [diff] [blame] | 973 | if (is_process_init_done(process)) |
Alexandre Julliard | ec7bb23 | 1999-11-12 03:35:25 +0000 | [diff] [blame] | 974 | { |
Alexandre Julliard | 9d80215 | 2002-05-24 21:20:27 +0000 | [diff] [blame] | 975 | fatal_protocol_error( current, "init_process_done: called twice\n" ); |
| 976 | return; |
| 977 | } |
| 978 | if (!req->module) |
| 979 | { |
| 980 | fatal_protocol_error( current, "init_process_done: module base address cannot be 0\n" ); |
Alexandre Julliard | ec7bb23 | 1999-11-12 03:35:25 +0000 | [diff] [blame] | 981 | return; |
| 982 | } |
Alexandre Julliard | 05f0b71 | 2000-03-09 18:18:41 +0000 | [diff] [blame] | 983 | process->exe.base = req->module; |
Alexandre Julliard | aeb5660 | 2002-03-22 00:21:23 +0000 | [diff] [blame] | 984 | process->exe.size = req->module_size; |
Alexandre Julliard | a37dec0 | 2000-06-08 00:57:24 +0000 | [diff] [blame] | 985 | process->exe.name = req->name; |
Alexandre Julliard | ad29b90 | 2001-01-05 22:24:15 +0000 | [diff] [blame] | 986 | |
Alexandre Julliard | ca96de3 | 2002-05-25 21:15:03 +0000 | [diff] [blame] | 987 | if (req->exe_file) file = get_file_obj( process, req->exe_file, GENERIC_READ ); |
Alexandre Julliard | ac2e4f1 | 2001-10-25 19:52:12 +0000 | [diff] [blame] | 988 | if (process->exe.file) release_object( process->exe.file ); |
| 989 | process->exe.file = file; |
| 990 | |
Alexandre Julliard | aeb5660 | 2002-03-22 00:21:23 +0000 | [diff] [blame] | 991 | if ((process->exe.namelen = get_req_data_size())) |
| 992 | process->exe.filename = memdup( get_req_data(), process->exe.namelen ); |
| 993 | |
Alexandre Julliard | 9d80215 | 2002-05-24 21:20:27 +0000 | [diff] [blame] | 994 | generate_startup_debug_events( process, req->entry ); |
| 995 | set_process_startup_state( process, STARTUP_DONE ); |
| 996 | |
Alexandre Julliard | c5e433a | 2000-05-30 19:48:18 +0000 | [diff] [blame] | 997 | if (req->gui) process->idle_event = create_event( NULL, 0, 1, 0 ); |
Alexandre Julliard | ca96de3 | 2002-05-25 21:15:03 +0000 | [diff] [blame] | 998 | if (current->suspend + process->suspend > 0) stop_thread( current ); |
Alexandre Julliard | e55d593 | 2003-10-14 01:30:42 +0000 | [diff] [blame] | 999 | if (process->debugger) set_process_debug_flag( process, 1 ); |
Alexandre Julliard | ec7bb23 | 1999-11-12 03:35:25 +0000 | [diff] [blame] | 1000 | } |
| 1001 | |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 1002 | /* open a handle to a process */ |
| 1003 | DECL_HANDLER(open_process) |
| 1004 | { |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 1005 | struct process *process = get_process_from_id( req->pid ); |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 1006 | reply->handle = 0; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 1007 | if (process) |
| 1008 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 1009 | reply->handle = alloc_handle( current->process, process, req->access, req->inherit ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 1010 | release_object( process ); |
| 1011 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 1012 | } |
| 1013 | |
| 1014 | /* terminate a process */ |
| 1015 | DECL_HANDLER(terminate_process) |
| 1016 | { |
| 1017 | struct process *process; |
| 1018 | |
| 1019 | if ((process = get_process_from_handle( req->handle, PROCESS_TERMINATE ))) |
| 1020 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 1021 | reply->self = (current->process == process); |
Alexandre Julliard | 12f29b5 | 2000-03-17 15:16:57 +0000 | [diff] [blame] | 1022 | kill_process( process, current, req->exit_code ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 1023 | release_object( process ); |
| 1024 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 1025 | } |
| 1026 | |
| 1027 | /* fetch information about a process */ |
| 1028 | DECL_HANDLER(get_process_info) |
| 1029 | { |
| 1030 | struct process *process; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 1031 | |
| 1032 | if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION ))) |
| 1033 | { |
Alexandre Julliard | e55d593 | 2003-10-14 01:30:42 +0000 | [diff] [blame] | 1034 | reply->pid = get_process_id( process ); |
Eric Pouech | b0fd2ad | 2004-06-14 17:02:00 +0000 | [diff] [blame] | 1035 | reply->ppid = process->parent ? get_process_id( process->parent ) : 0; |
Alexandre Julliard | e55d593 | 2003-10-14 01:30:42 +0000 | [diff] [blame] | 1036 | reply->exit_code = process->exit_code; |
| 1037 | reply->priority = process->priority; |
| 1038 | reply->process_affinity = process->affinity; |
| 1039 | reply->system_affinity = 1; |
Eric Pouech | b0fd2ad | 2004-06-14 17:02:00 +0000 | [diff] [blame] | 1040 | reply->peb = process->peb; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 1041 | release_object( process ); |
| 1042 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | /* set information about a process */ |
| 1046 | DECL_HANDLER(set_process_info) |
| 1047 | { |
| 1048 | struct process *process; |
| 1049 | |
| 1050 | if ((process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION ))) |
| 1051 | { |
Alexandre Julliard | e55d593 | 2003-10-14 01:30:42 +0000 | [diff] [blame] | 1052 | if (req->mask & SET_PROCESS_INFO_PRIORITY) process->priority = req->priority; |
| 1053 | if (req->mask & SET_PROCESS_INFO_AFFINITY) |
| 1054 | { |
| 1055 | if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER ); |
| 1056 | else process->affinity = req->affinity; |
| 1057 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 1058 | release_object( process ); |
| 1059 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 1060 | } |
Alexandre Julliard | 8b8828f | 1999-11-12 21:39:14 +0000 | [diff] [blame] | 1061 | |
| 1062 | /* read data from a process address space */ |
| 1063 | DECL_HANDLER(read_process_memory) |
| 1064 | { |
| 1065 | struct process *process; |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 1066 | size_t len = get_reply_max_size(); |
Alexandre Julliard | 8b8828f | 1999-11-12 21:39:14 +0000 | [diff] [blame] | 1067 | |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 1068 | if (!(process = get_process_from_handle( req->handle, PROCESS_VM_READ ))) return; |
| 1069 | |
| 1070 | if (len) |
Alexandre Julliard | 8b8828f | 1999-11-12 21:39:14 +0000 | [diff] [blame] | 1071 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 1072 | unsigned int start_offset = (unsigned int)req->addr % sizeof(int); |
| 1073 | unsigned int nb_ints = (len + start_offset + sizeof(int) - 1) / sizeof(int); |
| 1074 | const int *start = (int *)((char *)req->addr - start_offset); |
| 1075 | int *buffer = mem_alloc( nb_ints * sizeof(int) ); |
| 1076 | if (buffer) |
| 1077 | { |
| 1078 | if (read_process_memory( process, start, nb_ints, buffer )) |
| 1079 | { |
| 1080 | /* move start of requested data to start of buffer */ |
| 1081 | if (start_offset) memmove( buffer, (char *)buffer + start_offset, len ); |
| 1082 | set_reply_data_ptr( buffer, len ); |
| 1083 | } |
| 1084 | else len = 0; |
| 1085 | } |
Alexandre Julliard | 8b8828f | 1999-11-12 21:39:14 +0000 | [diff] [blame] | 1086 | } |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 1087 | release_object( process ); |
Alexandre Julliard | 8b8828f | 1999-11-12 21:39:14 +0000 | [diff] [blame] | 1088 | } |
Alexandre Julliard | eef7025 | 1999-11-13 19:54:54 +0000 | [diff] [blame] | 1089 | |
| 1090 | /* write data to a process address space */ |
| 1091 | DECL_HANDLER(write_process_memory) |
| 1092 | { |
| 1093 | struct process *process; |
| 1094 | |
| 1095 | if ((process = get_process_from_handle( req->handle, PROCESS_VM_WRITE ))) |
| 1096 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 1097 | size_t len = get_req_data_size(); |
| 1098 | if ((len % sizeof(int)) || ((unsigned int)req->addr % sizeof(int))) |
| 1099 | set_error( STATUS_INVALID_PARAMETER ); |
| 1100 | else |
| 1101 | { |
| 1102 | if (len) write_process_memory( process, req->addr, len / sizeof(int), |
| 1103 | req->first_mask, req->last_mask, get_req_data() ); |
| 1104 | } |
Alexandre Julliard | eef7025 | 1999-11-13 19:54:54 +0000 | [diff] [blame] | 1105 | release_object( process ); |
| 1106 | } |
| 1107 | } |
Alexandre Julliard | 05f0b71 | 2000-03-09 18:18:41 +0000 | [diff] [blame] | 1108 | |
| 1109 | /* notify the server that a dll has been loaded */ |
| 1110 | DECL_HANDLER(load_dll) |
| 1111 | { |
| 1112 | struct process_dll *dll; |
| 1113 | struct file *file = NULL; |
| 1114 | |
Alexandre Julliard | 670711e | 2004-04-06 23:13:47 +0000 | [diff] [blame] | 1115 | if (req->handle && !(file = get_file_obj( current->process, req->handle, GENERIC_READ ))) |
| 1116 | return; |
Alexandre Julliard | 8081e5a | 2001-01-05 04:08:07 +0000 | [diff] [blame] | 1117 | |
Alexandre Julliard | aeb5660 | 2002-03-22 00:21:23 +0000 | [diff] [blame] | 1118 | if ((dll = process_load_dll( current->process, file, req->base, |
| 1119 | get_req_data(), get_req_data_size() ))) |
Alexandre Julliard | 05f0b71 | 2000-03-09 18:18:41 +0000 | [diff] [blame] | 1120 | { |
Alexandre Julliard | aeb5660 | 2002-03-22 00:21:23 +0000 | [diff] [blame] | 1121 | dll->size = req->size; |
Alexandre Julliard | 05f0b71 | 2000-03-09 18:18:41 +0000 | [diff] [blame] | 1122 | dll->dbg_offset = req->dbg_offset; |
| 1123 | dll->dbg_size = req->dbg_size; |
| 1124 | dll->name = req->name; |
| 1125 | /* only generate event if initialization is done */ |
Alexandre Julliard | 9d80215 | 2002-05-24 21:20:27 +0000 | [diff] [blame] | 1126 | if (is_process_init_done( current->process )) |
Alexandre Julliard | 05f0b71 | 2000-03-09 18:18:41 +0000 | [diff] [blame] | 1127 | generate_debug_event( current, LOAD_DLL_DEBUG_EVENT, dll ); |
| 1128 | } |
| 1129 | if (file) release_object( file ); |
| 1130 | } |
| 1131 | |
| 1132 | /* notify the server that a dll is being unloaded */ |
| 1133 | DECL_HANDLER(unload_dll) |
| 1134 | { |
| 1135 | process_unload_dll( current->process, req->base ); |
| 1136 | } |
Alexandre Julliard | c5e433a | 2000-05-30 19:48:18 +0000 | [diff] [blame] | 1137 | |
Eric Pouech | 2359b57 | 2003-01-09 00:01:28 +0000 | [diff] [blame] | 1138 | /* retrieve information about a module in a process */ |
| 1139 | DECL_HANDLER(get_dll_info) |
| 1140 | { |
| 1141 | struct process *process; |
| 1142 | |
| 1143 | if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION ))) |
| 1144 | { |
Alexandre Julliard | a9e0fb1 | 2005-03-02 10:20:09 +0000 | [diff] [blame] | 1145 | struct process_dll *dll = find_process_dll( process, req->base_address ); |
Eric Pouech | 2359b57 | 2003-01-09 00:01:28 +0000 | [diff] [blame] | 1146 | |
Alexandre Julliard | a9e0fb1 | 2005-03-02 10:20:09 +0000 | [diff] [blame] | 1147 | if (dll) |
Eric Pouech | 2359b57 | 2003-01-09 00:01:28 +0000 | [diff] [blame] | 1148 | { |
Alexandre Julliard | a9e0fb1 | 2005-03-02 10:20:09 +0000 | [diff] [blame] | 1149 | reply->size = dll->size; |
Robert Shearman | c516571 | 2005-05-25 18:41:09 +0000 | [diff] [blame] | 1150 | reply->entry_point = NULL; /* FIXME */ |
Alexandre Julliard | a9e0fb1 | 2005-03-02 10:20:09 +0000 | [diff] [blame] | 1151 | if (dll->filename) |
Eric Pouech | 2359b57 | 2003-01-09 00:01:28 +0000 | [diff] [blame] | 1152 | { |
Alexandre Julliard | a9e0fb1 | 2005-03-02 10:20:09 +0000 | [diff] [blame] | 1153 | size_t len = min( dll->namelen, get_reply_max_size() ); |
| 1154 | set_reply_data( dll->filename, len ); |
Eric Pouech | 2359b57 | 2003-01-09 00:01:28 +0000 | [diff] [blame] | 1155 | } |
| 1156 | } |
Alexandre Julliard | a9e0fb1 | 2005-03-02 10:20:09 +0000 | [diff] [blame] | 1157 | else |
| 1158 | set_error( STATUS_DLL_NOT_FOUND ); |
| 1159 | |
Eric Pouech | 2359b57 | 2003-01-09 00:01:28 +0000 | [diff] [blame] | 1160 | release_object( process ); |
| 1161 | } |
| 1162 | } |
| 1163 | |
Alexandre Julliard | c5e433a | 2000-05-30 19:48:18 +0000 | [diff] [blame] | 1164 | /* wait for a process to start waiting on input */ |
| 1165 | /* FIXME: only returns event for now, wait is done in the client */ |
| 1166 | DECL_HANDLER(wait_input_idle) |
| 1167 | { |
| 1168 | struct process *process; |
| 1169 | |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 1170 | reply->event = 0; |
Alexandre Julliard | c5e433a | 2000-05-30 19:48:18 +0000 | [diff] [blame] | 1171 | if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION ))) |
| 1172 | { |
| 1173 | if (process->idle_event && process != current->process && process->queue != current->queue) |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 1174 | reply->event = alloc_handle( current->process, process->idle_event, |
| 1175 | EVENT_ALL_ACCESS, 0 ); |
Alexandre Julliard | c5e433a | 2000-05-30 19:48:18 +0000 | [diff] [blame] | 1176 | release_object( process ); |
| 1177 | } |
| 1178 | } |