blob: 6afc0b294d0e660f3b1965f2adcb5e6f712de44e [file] [log] [blame]
Alexandre Julliard642d3131998-07-12 19:29:36 +00001/*
2 * Server-side process management
3 *
4 * Copyright (C) 1998 Alexandre Julliard
Alexandre Julliard0799c1a2002-03-09 23:29:33 +00005 *
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 Julliard642d3131998-07-12 19:29:36 +000019 */
20
Patrik Stridvallfb32c7b2000-01-29 21:02:19 +000021#include "config.h"
Alexandre Julliard5769d1d2002-04-26 19:05:15 +000022#include "wine/port.h"
Patrik Stridvallfb32c7b2000-01-29 21:02:19 +000023
Alexandre Julliard642d3131998-07-12 19:29:36 +000024#include <assert.h>
Alexandre Julliard767e6f61998-08-09 12:47:43 +000025#include <limits.h>
Alexandre Julliard3da5f841999-05-16 16:54:54 +000026#include <signal.h>
Alexandre Julliard85ed45e1998-08-22 19:03:56 +000027#include <string.h>
Alexandre Julliarde37c6e12003-09-05 23:08:26 +000028#include <stdarg.h>
Alexandre Julliard767e6f61998-08-09 12:47:43 +000029#include <stdio.h>
Alexandre Julliard642d3131998-07-12 19:29:36 +000030#include <stdlib.h>
Alexandre Julliard767e6f61998-08-09 12:47:43 +000031#include <sys/time.h>
Patrik Stridvallfb32c7b2000-01-29 21:02:19 +000032#ifdef HAVE_SYS_SOCKET_H
33# include <sys/socket.h>
34#endif
Alexandre Julliard767e6f61998-08-09 12:47:43 +000035#include <unistd.h>
36
Alexandre Julliarde37c6e12003-09-05 23:08:26 +000037#include "windef.h"
Alexandre Julliard767e6f61998-08-09 12:47:43 +000038#include "winbase.h"
39#include "winnt.h"
Alexandre Julliard642d3131998-07-12 19:29:36 +000040
Alexandre Julliard863637b2003-01-30 00:26:44 +000041#include "file.h"
Alexandre Julliard43c190e1999-05-15 10:48:19 +000042#include "handle.h"
43#include "process.h"
44#include "thread.h"
Alexandre Julliard5bc78081999-06-22 17:26:53 +000045#include "request.h"
Eric Pouech0b83d4c2001-11-23 23:04:58 +000046#include "console.h"
Alexandre Julliardbfce1512003-12-10 04:08:06 +000047#include "user.h"
Alexandre Julliard642d3131998-07-12 19:29:36 +000048
Alexandre Julliardf692d441999-03-21 19:23:54 +000049/* process structure */
Alexandre Julliard43c190e1999-05-15 10:48:19 +000050
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +000051static struct process *first_process;
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +000052static int running_processes;
Alexandre Julliard642d3131998-07-12 19:29:36 +000053
54/* process operations */
55
Alexandre Julliard338e7571998-12-27 15:28:54 +000056static void process_dump( struct object *obj, int verbose );
Alexandre Julliard85ed45e1998-08-22 19:03:56 +000057static int process_signaled( struct object *obj, struct thread *thread );
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +000058static void process_poll_event( struct fd *fd, int event );
Alexandre Julliard338e7571998-12-27 15:28:54 +000059static void process_destroy( struct object *obj );
Alexandre Julliard642d3131998-07-12 19:29:36 +000060
61static const struct object_ops process_ops =
62{
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000063 sizeof(struct process), /* size */
64 process_dump, /* dump */
65 add_queue, /* add_queue */
66 remove_queue, /* remove_queue */
67 process_signaled, /* signaled */
68 no_satisfied, /* satisfied */
Alexandre Julliard863637b2003-01-30 00:26:44 +000069 no_get_fd, /* get_fd */
Alexandre Julliard863637b2003-01-30 00:26:44 +000070 process_destroy /* destroy */
71};
72
73static const struct fd_ops process_fd_ops =
74{
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000075 NULL, /* get_poll_events */
Alexandre Julliardf5242402001-02-28 21:45:23 +000076 process_poll_event, /* poll_event */
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000077 no_flush, /* flush */
78 no_get_file_info, /* get_file_info */
Alexandre Julliard863637b2003-01-30 00:26:44 +000079 no_queue_async /* queue_async */
Alexandre Julliard642d3131998-07-12 19:29:36 +000080};
81
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +000082/* process startup info */
83
84struct startup_info
Alexandre Julliard2fe57772000-01-25 01:40:27 +000085{
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +000086 struct object obj; /* object header */
Alexandre Julliard77c8b1d2003-02-24 20:51:50 +000087 struct list entry; /* entry in list of startup infos */
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +000088 int inherit_all; /* inherit all handles from parent */
89 int create_flags; /* creation flags */
Alexandre Julliardbaf0a062003-03-11 01:48:53 +000090 int unix_pid; /* Unix pid of new process */
Alexandre Julliard51885742002-05-30 20:12:58 +000091 obj_handle_t hstdin; /* handle for stdin */
92 obj_handle_t hstdout; /* handle for stdout */
93 obj_handle_t hstderr; /* handle for stderr */
Alexandre Julliardd27624b2000-05-03 18:42:40 +000094 struct file *exe_file; /* file handle for main exe */
Alexandre Julliarde9936d92001-01-26 00:22:26 +000095 struct thread *owner; /* owner thread (the one that created the new process) */
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +000096 struct process *process; /* created process */
97 struct thread *thread; /* created thread */
Alexandre Julliard6543a652002-03-29 18:28:56 +000098 size_t data_size; /* size of startup data */
Alexandre Julliard841f8982003-10-04 04:09:41 +000099 void *data; /* data for startup info */
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000100};
101
102static void startup_info_dump( struct object *obj, int verbose );
103static int startup_info_signaled( struct object *obj, struct thread *thread );
104static void startup_info_destroy( struct object *obj );
105
106static const struct object_ops startup_info_ops =
107{
108 sizeof(struct startup_info), /* size */
109 startup_info_dump, /* dump */
110 add_queue, /* add_queue */
111 remove_queue, /* remove_queue */
112 startup_info_signaled, /* signaled */
113 no_satisfied, /* satisfied */
Alexandre Julliard1ab243b2000-12-19 02:12:45 +0000114 no_get_fd, /* get_fd */
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000115 startup_info_destroy /* destroy */
116};
117
Alexandre Julliardf692d441999-03-21 19:23:54 +0000118
Alexandre Julliard77c8b1d2003-02-24 20:51:50 +0000119static struct list startup_info_list = LIST_INIT(startup_info_list);
120
Alexandre Julliard91befe12003-02-01 01:38:40 +0000121struct ptid_entry
122{
123 void *ptr; /* entry ptr */
124 unsigned int next; /* next free entry */
125};
126
127static struct ptid_entry *ptid_entries; /* array of ptid entries */
128static unsigned int used_ptid_entries; /* number of entries in use */
129static unsigned int alloc_ptid_entries; /* number of allocated entries */
130static unsigned int next_free_ptid; /* next free entry */
131static unsigned int last_free_ptid; /* last free entry */
132
133#define PTID_OFFSET 8 /* offset for first ptid value */
134
135/* allocate a new process or thread id */
136unsigned int alloc_ptid( void *ptr )
137{
138 struct ptid_entry *entry;
139 unsigned int id;
140
141 if (used_ptid_entries < alloc_ptid_entries)
142 {
143 id = used_ptid_entries + PTID_OFFSET;
144 entry = &ptid_entries[used_ptid_entries++];
145 }
146 else if (next_free_ptid)
147 {
148 id = next_free_ptid;
149 entry = &ptid_entries[id - PTID_OFFSET];
150 if (!(next_free_ptid = entry->next)) last_free_ptid = 0;
151 }
152 else /* need to grow the array */
153 {
154 unsigned int count = alloc_ptid_entries + (alloc_ptid_entries / 2);
155 if (!count) count = 64;
156 if (!(entry = realloc( ptid_entries, count * sizeof(*entry) )))
157 {
158 set_error( STATUS_NO_MEMORY );
159 return 0;
160 }
161 ptid_entries = entry;
162 alloc_ptid_entries = count;
163 id = used_ptid_entries + PTID_OFFSET;
164 entry = &ptid_entries[used_ptid_entries++];
165 }
166
167 entry->ptr = ptr;
168 return id;
169}
170
171/* free a process or thread id */
172void free_ptid( unsigned int id )
173{
174 struct ptid_entry *entry = &ptid_entries[id - PTID_OFFSET];
175
176 entry->ptr = NULL;
177 entry->next = 0;
178
179 /* append to end of free list so that we don't reuse it too early */
180 if (last_free_ptid) ptid_entries[last_free_ptid - PTID_OFFSET].next = id;
181 else next_free_ptid = id;
182
183 last_free_ptid = id;
184}
185
186/* retrieve the pointer corresponding to a process or thread id */
187void *get_ptid_entry( unsigned int id )
188{
189 if (id < PTID_OFFSET) return NULL;
190 if (id - PTID_OFFSET >= used_ptid_entries) return NULL;
191 return ptid_entries[id - PTID_OFFSET].ptr;
192}
193
Alexandre Julliard9d802152002-05-24 21:20:27 +0000194/* set the state of the process startup info */
195static void set_process_startup_state( struct process *process, enum startup_state state )
196{
Alexandre Julliardca96de32002-05-25 21:15:03 +0000197 if (process->startup_state == STARTUP_IN_PROGRESS) process->startup_state = state;
198 if (process->startup_info)
199 {
200 wake_up( &process->startup_info->obj, 0 );
201 release_object( process->startup_info );
202 process->startup_info = NULL;
203 }
Alexandre Julliard9d802152002-05-24 21:20:27 +0000204}
205
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000206/* set the console and stdio handles for a newly created process */
Eric Pouech3940d8a2001-12-04 20:17:43 +0000207static int set_process_console( struct process *process, struct thread *parent_thread,
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000208 struct startup_info *info, struct init_process_reply *reply )
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000209{
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000210 if (process->create_flags & CREATE_NEW_CONSOLE)
211 {
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000212 /* let the process init do the allocation */
213 return 1;
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000214 }
Alexandre Julliard9d802152002-05-24 21:20:27 +0000215 else if (info && !(process->create_flags & DETACHED_PROCESS))
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000216 {
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000217 /* FIXME: some better error checking should be done...
218 * like if hConOut and hConIn are console handles, then they should be on the same
219 * physical console
220 */
Eric Pouech449d10f2003-08-19 01:05:17 +0000221 inherit_console( parent_thread, process, info->inherit_all ? info->hstdin : 0 );
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000222 }
Alexandre Julliard9d802152002-05-24 21:20:27 +0000223 if (info)
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000224 {
Eric Pouech449d10f2003-08-19 01:05:17 +0000225 if (!info->inherit_all)
226 {
227 reply->hstdin = duplicate_handle( parent_thread->process, info->hstdin, process,
228 0, TRUE, DUPLICATE_SAME_ACCESS );
229 reply->hstdout = duplicate_handle( parent_thread->process, info->hstdout, process,
230 0, TRUE, DUPLICATE_SAME_ACCESS );
231 reply->hstderr = duplicate_handle( parent_thread->process, info->hstderr, process,
232 0, TRUE, DUPLICATE_SAME_ACCESS );
233 }
234 else
235 {
236 reply->hstdin = info->hstdin;
237 reply->hstdout = info->hstdout;
238 reply->hstderr = info->hstderr;
239 }
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000240 }
Eric Pouech412d37f2003-06-21 02:07:10 +0000241 else reply->hstdin = reply->hstdout = reply->hstderr = 0;
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000242 /* some handles above may have been invalid; this is not an error */
Alexandre Julliard9e9e3a92003-08-19 03:46:24 +0000243 if (get_error() == STATUS_INVALID_HANDLE ||
244 get_error() == STATUS_OBJECT_TYPE_MISMATCH) clear_error();
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000245 return 1;
246}
247
248/* create a new process and its main thread */
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000249struct thread *create_process( int fd )
Alexandre Julliard642d3131998-07-12 19:29:36 +0000250{
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000251 struct process *process;
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000252 struct thread *thread = NULL;
Alexandre Julliard8859d772001-03-01 22:13:49 +0000253 int request_pipe[2];
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000254
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000255 if (!(process = alloc_object( &process_ops ))) goto error;
Alexandre Julliardf692d441999-03-21 19:23:54 +0000256 process->next = NULL;
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000257 process->prev = NULL;
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000258 process->parent = NULL;
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000259 process->thread_list = NULL;
Alexandre Julliard3da5f841999-05-16 16:54:54 +0000260 process->debugger = NULL;
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000261 process->handles = NULL;
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000262 process->msg_fd = NULL;
Dave Pickles717bf7e2000-02-13 16:04:14 +0000263 process->exit_code = STILL_ACTIVE;
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000264 process->running_threads = 0;
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000265 process->priority = NORMAL_PRIORITY_CLASS;
266 process->affinity = 1;
Alexandre Julliard3da5f841999-05-16 16:54:54 +0000267 process->suspend = 0;
Alexandre Julliardd083a7b1999-11-29 02:10:56 +0000268 process->create_flags = 0;
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000269 process->console = NULL;
Alexandre Julliardca96de32002-05-25 21:15:03 +0000270 process->startup_state = STARTUP_IN_PROGRESS;
Alexandre Julliard9d802152002-05-24 21:20:27 +0000271 process->startup_info = NULL;
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000272 process->idle_event = NULL;
273 process->queue = NULL;
Turchanov Sergei43a27e32000-05-30 20:32:06 +0000274 process->atom_table = NULL;
Alexandre Julliarde55d5932003-10-14 01:30:42 +0000275 process->peb = NULL;
Alexandre Julliard0a7c1f62000-01-27 02:54:17 +0000276 process->ldt_copy = NULL;
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000277 process->exe.next = NULL;
278 process->exe.prev = NULL;
279 process->exe.file = NULL;
280 process->exe.dbg_offset = 0;
281 process->exe.dbg_size = 0;
Alexandre Julliardaeb56602002-03-22 00:21:23 +0000282 process->exe.namelen = 0;
283 process->exe.filename = NULL;
Alexandre Julliard54f22872002-10-03 19:54:57 +0000284 process->group_id = 0;
Mike McCormack36cd6f52003-07-24 00:07:00 +0000285 process->token = create_token();
Alexandre Julliardce613492003-03-18 05:04:33 +0000286 list_init( &process->locks );
Alexandre Julliardbfce1512003-12-10 04:08:06 +0000287 list_init( &process->classes );
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000288
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000289 gettimeofday( &process->start_time, NULL );
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000290 if ((process->next = first_process) != NULL) process->next->prev = process;
291 first_process = process;
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000292
Alexandre Julliard91befe12003-02-01 01:38:40 +0000293 if (!(process->id = alloc_ptid( process ))) goto error;
Alexandre Julliard580da242003-03-12 22:38:14 +0000294 if (!(process->msg_fd = create_anonymous_fd( &process_fd_ops, fd, &process->obj ))) goto error;
Alexandre Julliard91befe12003-02-01 01:38:40 +0000295
Alexandre Julliard7f748242000-12-26 00:30:30 +0000296 /* create the main thread */
Alexandre Julliard8859d772001-03-01 22:13:49 +0000297 if (pipe( request_pipe ) == -1)
298 {
299 file_set_error();
300 goto error;
301 }
Alexandre Julliard4144b5b2002-06-20 23:21:27 +0000302 if (send_client_fd( process, request_pipe[1], 0 ) == -1)
303 {
304 close( request_pipe[0] );
305 close( request_pipe[1] );
306 goto error;
307 }
Alexandre Julliard8859d772001-03-01 22:13:49 +0000308 close( request_pipe[1] );
309 if (!(thread = create_thread( request_pipe[0], process ))) goto error;
Alexandre Julliard7f748242000-12-26 00:30:30 +0000310
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000311 set_fd_events( process->msg_fd, POLLIN ); /* start listening to events */
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000312 release_object( process );
313 return thread;
Alexandre Julliardf692d441999-03-21 19:23:54 +0000314
315 error:
Alexandre Julliard4144b5b2002-06-20 23:21:27 +0000316 if (process) release_object( process );
317 /* if we failed to start our first process, close everything down */
318 if (!running_processes) close_master_socket();
Alexandre Julliardf692d441999-03-21 19:23:54 +0000319 return NULL;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000320}
321
Alexandre Julliard77c8b1d2003-02-24 20:51:50 +0000322/* find the startup info for a given Unix process */
Alexandre Julliardbaf0a062003-03-11 01:48:53 +0000323inline static struct startup_info *find_startup_info( int unix_pid )
Alexandre Julliard77c8b1d2003-02-24 20:51:50 +0000324{
325 struct list *ptr;
326
327 LIST_FOR_EACH( ptr, &startup_info_list )
328 {
329 struct startup_info *info = LIST_ENTRY( ptr, struct startup_info, entry );
330 if (info->unix_pid == unix_pid) return info;
331 }
332 return NULL;
333}
334
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000335/* initialize the current process and fill in the request */
Alexandre Julliard77c8b1d2003-02-24 20:51:50 +0000336static struct startup_info *init_process( struct init_process_reply *reply )
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000337{
338 struct process *process = current->process;
Alexandre Julliard77c8b1d2003-02-24 20:51:50 +0000339 struct thread *parent_thread = NULL;
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000340 struct process *parent = NULL;
Alexandre Julliard77c8b1d2003-02-24 20:51:50 +0000341 struct startup_info *info = find_startup_info( current->unix_pid );
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000342
Alexandre Julliard77c8b1d2003-02-24 20:51:50 +0000343 if (info)
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000344 {
Alexandre Julliard77c8b1d2003-02-24 20:51:50 +0000345 if (info->thread)
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000346 {
347 fatal_protocol_error( current, "init_process: called twice?\n" );
Alexandre Julliard6543a652002-03-29 18:28:56 +0000348 return NULL;
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000349 }
Alexandre Julliard77c8b1d2003-02-24 20:51:50 +0000350 parent_thread = info->owner;
351 parent = parent_thread->process;
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000352 process->parent = (struct process *)grab_object( parent );
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000353 }
354
355 /* set the process flags */
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000356 process->create_flags = info ? info->create_flags : 0;
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000357
358 /* create the handle table */
Alexandre Julliard9d802152002-05-24 21:20:27 +0000359 if (info && info->inherit_all)
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000360 process->handles = copy_handle_table( process, parent );
361 else
362 process->handles = alloc_handle_table( process, 0 );
Alexandre Julliard6543a652002-03-29 18:28:56 +0000363 if (!process->handles) return NULL;
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000364
365 /* retrieve the main exe file */
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000366 reply->exe_file = 0;
Alexandre Julliard9d802152002-05-24 21:20:27 +0000367 if (info && info->exe_file)
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000368 {
Alexandre Julliardd27624b2000-05-03 18:42:40 +0000369 process->exe.file = (struct file *)grab_object( info->exe_file );
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000370 if (!(reply->exe_file = alloc_handle( process, process->exe.file, GENERIC_READ, 0 )))
Alexandre Julliard6543a652002-03-29 18:28:56 +0000371 return NULL;
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000372 }
373
374 /* set the process console */
Alexandre Julliard6543a652002-03-29 18:28:56 +0000375 if (!set_process_console( process, parent_thread, info, reply )) return NULL;
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000376
Alexandre Julliard54f22872002-10-03 19:54:57 +0000377 process->group_id = get_process_id( process );
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000378 if (parent)
379 {
380 /* attach to the debugger if requested */
381 if (process->create_flags & (DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS))
382 set_process_debugger( process, parent_thread );
Alexandre Julliard9d802152002-05-24 21:20:27 +0000383 else if (parent->debugger && !(parent->create_flags & DEBUG_ONLY_THIS_PROCESS))
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000384 set_process_debugger( process, parent->debugger );
Eric Pouech93bfa0d2002-06-02 21:22:22 +0000385 if (!(process->create_flags & CREATE_NEW_PROCESS_GROUP))
386 process->group_id = parent->group_id;
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000387 }
388
389 /* thread will be actually suspended in init_done */
390 if (process->create_flags & CREATE_SUSPENDED) current->suspend++;
391
392 if (info)
393 {
Alexandre Julliard6543a652002-03-29 18:28:56 +0000394 reply->info_size = info->data_size;
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000395 info->process = (struct process *)grab_object( process );
396 info->thread = (struct thread *)grab_object( current );
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000397 }
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000398 reply->create_flags = process->create_flags;
399 reply->server_start = server_start_ticks;
Alexandre Julliard6543a652002-03-29 18:28:56 +0000400 return info ? (struct startup_info *)grab_object( info ) : NULL;
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000401}
402
Alexandre Julliard642d3131998-07-12 19:29:36 +0000403/* destroy a process when its refcount is 0 */
Alexandre Julliard338e7571998-12-27 15:28:54 +0000404static void process_destroy( struct object *obj )
Alexandre Julliard642d3131998-07-12 19:29:36 +0000405{
406 struct process *process = (struct process *)obj;
407 assert( obj->ops == &process_ops );
408
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000409 /* we can't have a thread remaining */
410 assert( !process->thread_list );
Alexandre Julliard9d802152002-05-24 21:20:27 +0000411
412 set_process_startup_state( process, STARTUP_ABORTED );
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000413 if (process->console) release_object( process->console );
414 if (process->parent) release_object( process->parent );
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000415 if (process->msg_fd) release_object( process->msg_fd );
Alexandre Julliard642d3131998-07-12 19:29:36 +0000416 if (process->next) process->next->prev = process->prev;
417 if (process->prev) process->prev->next = process->next;
418 else first_process = process->next;
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000419 if (process->idle_event) release_object( process->idle_event );
420 if (process->queue) release_object( process->queue );
Turchanov Sergei43a27e32000-05-30 20:32:06 +0000421 if (process->atom_table) release_object( process->atom_table );
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000422 if (process->exe.file) release_object( process->exe.file );
Alexandre Julliardaeb56602002-03-22 00:21:23 +0000423 if (process->exe.filename) free( process->exe.filename );
Alexandre Julliard91befe12003-02-01 01:38:40 +0000424 if (process->id) free_ptid( process->id );
Mike McCormack36cd6f52003-07-24 00:07:00 +0000425 if (process->token) release_object( process->token );
Alexandre Julliard642d3131998-07-12 19:29:36 +0000426}
427
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000428/* dump a process on stdout for debugging purposes */
Alexandre Julliard338e7571998-12-27 15:28:54 +0000429static void process_dump( struct object *obj, int verbose )
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000430{
431 struct process *process = (struct process *)obj;
432 assert( obj->ops == &process_ops );
433
Alexandre Julliard91befe12003-02-01 01:38:40 +0000434 fprintf( stderr, "Process id=%04x next=%p prev=%p handles=%p\n",
435 process->id, process->next, process->prev, process->handles );
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000436}
437
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000438static int process_signaled( struct object *obj, struct thread *thread )
439{
440 struct process *process = (struct process *)obj;
Alexandre Julliarddbc033a1999-03-13 08:58:24 +0000441 return !process->running_threads;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000442}
443
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000444static void process_poll_event( struct fd *fd, int event )
Alexandre Julliardf5242402001-02-28 21:45:23 +0000445{
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000446 struct process *process = get_fd_user( fd );
447 assert( process->obj.ops == &process_ops );
Alexandre Julliardf5242402001-02-28 21:45:23 +0000448
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000449 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
Alexandre Julliardf5242402001-02-28 21:45:23 +0000450 else if (event & POLLIN) receive_fd( process );
451}
452
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000453static void startup_info_destroy( struct object *obj )
454{
455 struct startup_info *info = (struct startup_info *)obj;
456 assert( obj->ops == &startup_info_ops );
Alexandre Julliard77c8b1d2003-02-24 20:51:50 +0000457 list_remove( &info->entry );
Alexandre Julliard6543a652002-03-29 18:28:56 +0000458 if (info->data) free( info->data );
Alexandre Julliardd27624b2000-05-03 18:42:40 +0000459 if (info->exe_file) release_object( info->exe_file );
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000460 if (info->process) release_object( info->process );
461 if (info->thread) release_object( info->thread );
Alexandre Julliard77c8b1d2003-02-24 20:51:50 +0000462 if (info->owner) release_object( info->owner );
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000463}
464
465static void startup_info_dump( struct object *obj, int verbose )
466{
467 struct startup_info *info = (struct startup_info *)obj;
468 assert( obj->ops == &startup_info_ops );
469
Alexandre Julliardb3332d72002-10-19 01:00:59 +0000470 fprintf( stderr, "Startup info flags=%x in=%p out=%p err=%p\n",
Alexandre Julliardca96de32002-05-25 21:15:03 +0000471 info->create_flags, info->hstdin, info->hstdout, info->hstderr );
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000472}
473
474static int startup_info_signaled( struct object *obj, struct thread *thread )
475{
476 struct startup_info *info = (struct startup_info *)obj;
Alexandre Julliardca96de32002-05-25 21:15:03 +0000477 return info->process && is_process_init_done(info->process);
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000478}
479
Alexandre Julliard642d3131998-07-12 19:29:36 +0000480/* get a process from an id (and increment the refcount) */
Alexandre Julliard54f22872002-10-03 19:54:57 +0000481struct process *get_process_from_id( process_id_t id )
Alexandre Julliard642d3131998-07-12 19:29:36 +0000482{
Alexandre Julliard91befe12003-02-01 01:38:40 +0000483 struct object *obj = get_ptid_entry( id );
484
485 if (obj && obj->ops == &process_ops) return (struct process *)grab_object( obj );
486 set_error( STATUS_INVALID_PARAMETER );
487 return NULL;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000488}
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000489
490/* get a process from a handle (and increment the refcount) */
Alexandre Julliard51885742002-05-30 20:12:58 +0000491struct process *get_process_from_handle( obj_handle_t handle, unsigned int access )
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000492{
493 return (struct process *)get_handle_obj( current->process, handle,
494 access, &process_ops );
495}
496
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000497/* add a dll to a process list */
498static struct process_dll *process_load_dll( struct process *process, struct file *file,
Alexandre Julliardc30cefb2003-09-30 01:04:19 +0000499 void *base, const WCHAR *filename, size_t name_len )
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000500{
501 struct process_dll *dll;
502
503 /* make sure we don't already have one with the same base address */
504 for (dll = process->exe.next; dll; dll = dll->next) if (dll->base == base)
505 {
506 set_error( STATUS_INVALID_PARAMETER );
507 return NULL;
508 }
509
510 if ((dll = mem_alloc( sizeof(*dll) )))
511 {
512 dll->prev = &process->exe;
513 dll->file = NULL;
514 dll->base = base;
Alexandre Julliardaeb56602002-03-22 00:21:23 +0000515 dll->filename = NULL;
516 dll->namelen = name_len;
517 if (name_len && !(dll->filename = memdup( filename, name_len )))
518 {
519 free( dll );
520 return NULL;
521 }
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000522 if (file) dll->file = (struct file *)grab_object( file );
523 if ((dll->next = process->exe.next)) dll->next->prev = dll;
524 process->exe.next = dll;
525 }
526 return dll;
527}
528
529/* remove a dll from a process list */
530static void process_unload_dll( struct process *process, void *base )
531{
532 struct process_dll *dll;
533
534 for (dll = process->exe.next; dll; dll = dll->next)
535 {
536 if (dll->base == base)
537 {
538 if (dll->file) release_object( dll->file );
539 if (dll->next) dll->next->prev = dll->prev;
540 if (dll->prev) dll->prev->next = dll->next;
Alexandre Julliardaeb56602002-03-22 00:21:23 +0000541 if (dll->filename) free( dll->filename );
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000542 free( dll );
543 generate_debug_event( current, UNLOAD_DLL_DEBUG_EVENT, base );
544 return;
545 }
546 }
547 set_error( STATUS_INVALID_PARAMETER );
548}
549
Alexandre Julliard40043ed2002-08-16 20:02:15 +0000550/* kill all processes */
551void kill_all_processes( struct process *skip, int exit_code )
552{
553 for (;;)
554 {
555 struct process *process = first_process;
556
557 while (process && (!process->running_threads || process == skip))
558 process = process->next;
559 if (!process) break;
560 kill_process( process, NULL, exit_code );
561 }
562}
563
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000564/* kill all processes being attached to a console renderer */
Eric Pouech3940d8a2001-12-04 20:17:43 +0000565void kill_console_processes( struct thread *renderer, int exit_code )
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000566{
567 for (;;) /* restart from the beginning of the list every time */
568 {
569 struct process *process = first_process;
570
571 /* find the first process being attached to 'renderer' and still running */
572 while (process &&
Eric Pouech3940d8a2001-12-04 20:17:43 +0000573 (process == renderer->process || !process->console ||
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000574 process->console->renderer != renderer || !process->running_threads))
575 {
576 process = process->next;
577 }
578 if (!process) break;
579 kill_process( process, NULL, exit_code );
580 }
581}
582
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000583/* a process has been killed (i.e. its last thread died) */
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000584static void process_killed( struct process *process )
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000585{
586 assert( !process->thread_list );
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000587 gettimeofday( &process->end_time, NULL );
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000588 if (process->handles) release_object( process->handles );
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000589 process->handles = NULL;
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000590
591 /* close the console attached to this process, if any */
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000592 free_console( process );
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000593
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000594 while (process->exe.next)
595 {
596 struct process_dll *dll = process->exe.next;
597 process->exe.next = dll->next;
598 if (dll->file) release_object( dll->file );
Alexandre Julliardaeb56602002-03-22 00:21:23 +0000599 if (dll->filename) free( dll->filename );
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000600 free( dll );
601 }
Alexandre Julliardbfce1512003-12-10 04:08:06 +0000602 destroy_process_classes( process );
Alexandre Julliard9d802152002-05-24 21:20:27 +0000603 set_process_startup_state( process, STARTUP_ABORTED );
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000604 if (process->exe.file) release_object( process->exe.file );
605 process->exe.file = NULL;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000606 wake_up( &process->obj, 0 );
Alexandre Julliard4144b5b2002-06-20 23:21:27 +0000607 if (!--running_processes) close_master_socket();
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000608}
609
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000610/* add a thread to a process running threads list */
611void add_process_thread( struct process *process, struct thread *thread )
612{
613 thread->proc_next = process->thread_list;
614 thread->proc_prev = NULL;
615 if (thread->proc_next) thread->proc_next->proc_prev = thread;
616 process->thread_list = thread;
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +0000617 if (!process->running_threads++) running_processes++;
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000618 grab_object( thread );
619}
620
621/* remove a thread from a process running threads list */
622void remove_process_thread( struct process *process, struct thread *thread )
623{
624 assert( process->running_threads > 0 );
625 assert( process->thread_list );
626
627 if (thread->proc_next) thread->proc_next->proc_prev = thread->proc_prev;
628 if (thread->proc_prev) thread->proc_prev->proc_next = thread->proc_next;
629 else process->thread_list = thread->proc_next;
630
631 if (!--process->running_threads)
632 {
633 /* we have removed the last running thread, exit the process */
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000634 process->exit_code = thread->exit_code;
635 generate_debug_event( thread, EXIT_PROCESS_DEBUG_EVENT, process );
636 process_killed( process );
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000637 }
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000638 else generate_debug_event( thread, EXIT_THREAD_DEBUG_EVENT, thread );
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000639 release_object( thread );
640}
641
Alexandre Julliard3da5f841999-05-16 16:54:54 +0000642/* suspend all the threads of a process */
643void suspend_process( struct process *process )
644{
645 if (!process->suspend++)
646 {
647 struct thread *thread = process->thread_list;
Alexandre Julliardad16fc92002-04-06 00:03:33 +0000648 while (thread)
Alexandre Julliard3da5f841999-05-16 16:54:54 +0000649 {
Alexandre Julliardad16fc92002-04-06 00:03:33 +0000650 struct thread *next = thread->proc_next;
Alexandre Julliard0a707831999-11-08 05:31:47 +0000651 if (!thread->suspend) stop_thread( thread );
Alexandre Julliardad16fc92002-04-06 00:03:33 +0000652 thread = next;
Alexandre Julliard3da5f841999-05-16 16:54:54 +0000653 }
654 }
655}
656
657/* resume all the threads of a process */
658void resume_process( struct process *process )
659{
660 assert (process->suspend > 0);
661 if (!--process->suspend)
662 {
663 struct thread *thread = process->thread_list;
Alexandre Julliardad16fc92002-04-06 00:03:33 +0000664 while (thread)
Alexandre Julliard3da5f841999-05-16 16:54:54 +0000665 {
Alexandre Julliardad16fc92002-04-06 00:03:33 +0000666 struct thread *next = thread->proc_next;
Alexandre Julliardd04ccb82003-03-04 22:18:43 +0000667 if (!thread->suspend) wake_thread( thread );
Alexandre Julliardad16fc92002-04-06 00:03:33 +0000668 thread = next;
Alexandre Julliard3da5f841999-05-16 16:54:54 +0000669 }
670 }
671}
672
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000673/* kill a process on the spot */
Alexandre Julliardf5242402001-02-28 21:45:23 +0000674void kill_process( struct process *process, struct thread *skip, int exit_code )
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000675{
Alexandre Julliard12f29b52000-03-17 15:16:57 +0000676 struct thread *thread = process->thread_list;
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000677
Alexandre Julliard12f29b52000-03-17 15:16:57 +0000678 while (thread)
679 {
680 struct thread *next = thread->proc_next;
681 thread->exit_code = exit_code;
682 if (thread != skip) kill_thread( thread, 1 );
683 thread = next;
684 }
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000685}
686
Alexandre Julliard17cf8101999-11-24 01:22:14 +0000687/* kill all processes being debugged by a given thread */
688void kill_debugged_processes( struct thread *debugger, int exit_code )
689{
690 for (;;) /* restart from the beginning of the list every time */
691 {
692 struct process *process = first_process;
693 /* find the first process being debugged by 'debugger' and still running */
694 while (process && (process->debugger != debugger || !process->running_threads))
695 process = process->next;
696 if (!process) return;
697 process->debugger = NULL;
Alexandre Julliard12f29b52000-03-17 15:16:57 +0000698 kill_process( process, NULL, exit_code );
Alexandre Julliard17cf8101999-11-24 01:22:14 +0000699 }
700}
701
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000702
Eric Pouechfbccb382002-02-27 01:28:30 +0000703/* detach a debugger from all its debuggees */
704void detach_debugged_processes( struct thread *debugger )
705{
706 struct process *process;
707 for (process = first_process; process; process = process->next)
708 {
709 if (process->debugger == debugger && process->running_threads)
710 {
711 debugger_detach( process, debugger );
712 }
713 }
714}
715
716
Eric Pouech93bfa0d2002-06-02 21:22:22 +0000717void enum_processes( int (*cb)(struct process*, void*), void *user )
718{
719 struct process *process;
720 for (process = first_process; process; process = process->next)
721 {
722 if ((cb)(process, user)) break;
723 }
724}
725
726
Alexandre Julliard8b8828f1999-11-12 21:39:14 +0000727/* read data from a process memory space */
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000728/* len is the total size (in ints) */
729static int read_process_memory( struct process *process, const int *addr, size_t len, int *dest )
Alexandre Julliard8b8828f1999-11-12 21:39:14 +0000730{
731 struct thread *thread = process->thread_list;
Alexandre Julliard8b8828f1999-11-12 21:39:14 +0000732
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000733 assert( !((unsigned int)addr % sizeof(int)) ); /* address must be aligned */
734
Alexandre Julliard00641d52000-03-08 16:41:37 +0000735 if (!thread) /* process is dead */
736 {
737 set_error( STATUS_ACCESS_DENIED );
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000738 return 0;
Alexandre Julliard00641d52000-03-08 16:41:37 +0000739 }
Alexandre Julliard98aacc72000-03-15 19:46:14 +0000740 if (suspend_for_ptrace( thread ))
Alexandre Julliard8b8828f1999-11-12 21:39:14 +0000741 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000742 while (len > 0)
Alexandre Julliard8b8828f1999-11-12 21:39:14 +0000743 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000744 if (read_thread_int( thread, addr++, dest++ ) == -1) break;
Alexandre Julliardeef70251999-11-13 19:54:54 +0000745 len--;
746 }
Alexandre Julliardd04ccb82003-03-04 22:18:43 +0000747 resume_after_ptrace( thread );
Alexandre Julliard8b8828f1999-11-12 21:39:14 +0000748 }
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000749 return !len;
750}
751
752/* make sure we can write to the whole address range */
753/* len is the total size (in ints) */
754static int check_process_write_access( struct thread *thread, int *addr, size_t len )
755{
756 int page = get_page_size() / sizeof(int);
757
758 for (;;)
759 {
760 if (write_thread_int( thread, addr, 0, 0 ) == -1) return 0;
761 if (len <= page) break;
762 addr += page;
763 len -= page;
764 }
765 return (write_thread_int( thread, addr + len - 1, 0, 0 ) != -1);
Alexandre Julliardeef70251999-11-13 19:54:54 +0000766}
767
768/* write data to a process memory space */
769/* len is the total size (in ints), max is the size we can actually read from the input buffer */
770/* we check the total size for write permissions */
Alexandre Julliarde55d5932003-10-14 01:30:42 +0000771static int write_process_memory( struct process *process, int *addr, size_t len,
772 unsigned int first_mask, unsigned int last_mask, const int *src )
Alexandre Julliardeef70251999-11-13 19:54:54 +0000773{
774 struct thread *thread = process->thread_list;
Alexandre Julliarde55d5932003-10-14 01:30:42 +0000775 int ret = 0;
Alexandre Julliardeef70251999-11-13 19:54:54 +0000776
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000777 assert( !((unsigned int)addr % sizeof(int) )); /* address must be aligned */
778
Alexandre Julliard00641d52000-03-08 16:41:37 +0000779 if (!thread) /* process is dead */
780 {
781 set_error( STATUS_ACCESS_DENIED );
Alexandre Julliarde55d5932003-10-14 01:30:42 +0000782 return 0;
Alexandre Julliard00641d52000-03-08 16:41:37 +0000783 }
Alexandre Julliard98aacc72000-03-15 19:46:14 +0000784 if (suspend_for_ptrace( thread ))
Alexandre Julliardeef70251999-11-13 19:54:54 +0000785 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000786 if (!check_process_write_access( thread, addr, len ))
787 {
788 set_error( STATUS_ACCESS_DENIED );
Peter Hunnisetta3c5ad42003-02-28 21:50:47 +0000789 goto done;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000790 }
Alexandre Julliardeef70251999-11-13 19:54:54 +0000791 /* first word is special */
792 if (len > 1)
793 {
Alexandre Julliard578c1001999-11-14 21:23:21 +0000794 if (write_thread_int( thread, addr++, *src++, first_mask ) == -1) goto done;
Alexandre Julliardeef70251999-11-13 19:54:54 +0000795 len--;
Alexandre Julliardeef70251999-11-13 19:54:54 +0000796 }
797 else last_mask &= first_mask;
798
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000799 while (len > 1)
Alexandre Julliardeef70251999-11-13 19:54:54 +0000800 {
Alexandre Julliard578c1001999-11-14 21:23:21 +0000801 if (write_thread_int( thread, addr++, *src++, ~0 ) == -1) goto done;
Alexandre Julliardeef70251999-11-13 19:54:54 +0000802 len--;
803 }
804
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000805 /* last word is special too */
806 if (write_thread_int( thread, addr, *src, last_mask ) == -1) goto done;
Alexandre Julliarde55d5932003-10-14 01:30:42 +0000807 ret = 1;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000808
Alexandre Julliard98aacc72000-03-15 19:46:14 +0000809 done:
Alexandre Julliardd04ccb82003-03-04 22:18:43 +0000810 resume_after_ptrace( thread );
Alexandre Julliardeef70251999-11-13 19:54:54 +0000811 }
Alexandre Julliarde55d5932003-10-14 01:30:42 +0000812 return ret;
813}
814
815/* set the debugged flag in the process PEB */
816int set_process_debug_flag( struct process *process, int flag )
817{
818 int mask = 0, data = 0;
819
820 /* BeingDebugged flag is the byte at offset 2 in the PEB */
821 memset( (char *)&mask + 2, 0xff, 1 );
822 memset( (char *)&data + 2, flag, 1 );
823 return write_process_memory( process, process->peb, 1, mask, mask, &data );
Alexandre Julliard8b8828f1999-11-12 21:39:14 +0000824}
825
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +0000826/* take a snapshot of currently running processes */
827struct process_snapshot *process_snap( int *count )
828{
829 struct process_snapshot *snapshot, *ptr;
830 struct process *process;
831 if (!running_processes) return NULL;
832 if (!(snapshot = mem_alloc( sizeof(*snapshot) * running_processes )))
833 return NULL;
834 ptr = snapshot;
835 for (process = first_process; process; process = process->next)
836 {
837 if (!process->running_threads) continue;
838 ptr->process = process;
839 ptr->threads = process->running_threads;
Alexandre Julliard07d84462000-04-16 19:45:05 +0000840 ptr->count = process->obj.refcount;
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +0000841 ptr->priority = process->priority;
Eric Pouech9fd54b22003-09-16 01:07:21 +0000842 ptr->handles = get_handle_table_count(process);
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +0000843 grab_object( process );
844 ptr++;
845 }
846 *count = running_processes;
847 return snapshot;
848}
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000849
Alexandre Julliard07d84462000-04-16 19:45:05 +0000850/* take a snapshot of the modules of a process */
851struct module_snapshot *module_snap( struct process *process, int *count )
852{
853 struct module_snapshot *snapshot, *ptr;
854 struct process_dll *dll;
855 int total = 0;
856
857 for (dll = &process->exe; dll; dll = dll->next) total++;
858 if (!(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
859
860 for (ptr = snapshot, dll = &process->exe; dll; dll = dll->next, ptr++)
861 {
Alexandre Julliardaeb56602002-03-22 00:21:23 +0000862 ptr->base = dll->base;
863 ptr->size = dll->size;
864 ptr->namelen = dll->namelen;
865 ptr->filename = memdup( dll->filename, dll->namelen );
Alexandre Julliard07d84462000-04-16 19:45:05 +0000866 }
867 *count = total;
868 return snapshot;
869}
870
871
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000872/* create a new process */
873DECL_HANDLER(new_process)
874{
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000875 struct startup_info *info;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000876
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000877 /* build the startup info for a new process */
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000878 if (!(info = alloc_object( &startup_info_ops ))) return;
Alexandre Julliard77c8b1d2003-02-24 20:51:50 +0000879 list_add_head( &startup_info_list, &info->entry );
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000880 info->inherit_all = req->inherit_all;
881 info->create_flags = req->create_flags;
Alexandre Julliard77c8b1d2003-02-24 20:51:50 +0000882 info->unix_pid = req->unix_pid;
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000883 info->hstdin = req->hstdin;
884 info->hstdout = req->hstdout;
885 info->hstderr = req->hstderr;
Alexandre Julliardd27624b2000-05-03 18:42:40 +0000886 info->exe_file = NULL;
Alexandre Julliarde9936d92001-01-26 00:22:26 +0000887 info->owner = (struct thread *)grab_object( current );
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000888 info->process = NULL;
889 info->thread = NULL;
Alexandre Julliard6543a652002-03-29 18:28:56 +0000890 info->data_size = get_req_data_size();
891 info->data = NULL;
Alexandre Julliard6a72dc52000-04-14 13:42:00 +0000892
Alexandre Julliard8081e5a2001-01-05 04:08:07 +0000893 if (req->exe_file &&
Alexandre Julliardd27624b2000-05-03 18:42:40 +0000894 !(info->exe_file = get_file_obj( current->process, req->exe_file, GENERIC_READ )))
Alexandre Julliarde9936d92001-01-26 00:22:26 +0000895 goto done;
Alexandre Julliardd27624b2000-05-03 18:42:40 +0000896
Alexandre Julliard841f8982003-10-04 04:09:41 +0000897 if (!(info->data = memdup( get_req_data(), info->data_size ))) goto done;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000898 reply->info = alloc_handle( current->process, info, SYNCHRONIZE, FALSE );
Alexandre Julliarde9936d92001-01-26 00:22:26 +0000899
900 done:
901 release_object( info );
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000902}
Alexandre Julliard6a72dc52000-04-14 13:42:00 +0000903
Alexandre Julliarde9936d92001-01-26 00:22:26 +0000904/* Retrieve information about a newly started process */
905DECL_HANDLER(get_new_process_info)
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000906{
Alexandre Julliarde9936d92001-01-26 00:22:26 +0000907 struct startup_info *info;
908
Alexandre Julliarde9936d92001-01-26 00:22:26 +0000909 if ((info = (struct startup_info *)get_handle_obj( current->process, req->info,
910 0, &startup_info_ops )))
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000911 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000912 reply->pid = get_process_id( info->process );
913 reply->tid = get_thread_id( info->thread );
914 reply->phandle = alloc_handle( current->process, info->process,
915 PROCESS_ALL_ACCESS, req->pinherit );
916 reply->thandle = alloc_handle( current->process, info->thread,
917 THREAD_ALL_ACCESS, req->tinherit );
Alexandre Julliardca96de32002-05-25 21:15:03 +0000918 reply->success = is_process_init_done( info->process );
Alexandre Julliarde9936d92001-01-26 00:22:26 +0000919 release_object( info );
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000920 }
921 else
922 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000923 reply->pid = 0;
924 reply->tid = 0;
925 reply->phandle = 0;
926 reply->thandle = 0;
Alexandre Julliard9d802152002-05-24 21:20:27 +0000927 reply->success = 0;
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000928 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000929}
930
Alexandre Julliard6543a652002-03-29 18:28:56 +0000931/* Retrieve the new process startup info */
932DECL_HANDLER(get_startup_info)
933{
934 struct startup_info *info;
935
Alexandre Julliard9d802152002-05-24 21:20:27 +0000936 if ((info = current->process->startup_info))
Alexandre Julliard6543a652002-03-29 18:28:56 +0000937 {
938 size_t size = info->data_size;
939 if (size > get_reply_max_size()) size = get_reply_max_size();
Alexandre Julliard9d802152002-05-24 21:20:27 +0000940
941 /* we return the data directly without making a copy so this can only be called once */
942 set_reply_data_ptr( info->data, size );
943 info->data = NULL;
944 info->data_size = 0;
Alexandre Julliard6543a652002-03-29 18:28:56 +0000945 }
946}
947
948
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000949/* initialize a new process */
950DECL_HANDLER(init_process)
951{
Alexandre Julliard02a53c12003-02-25 04:17:22 +0000952 if (current->unix_pid == -1)
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000953 {
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000954 fatal_protocol_error( current, "init_process: init_thread not called yet\n" );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000955 return;
956 }
Alexandre Julliard9d802152002-05-24 21:20:27 +0000957 if (current->process->startup_info)
Alexandre Julliard6543a652002-03-29 18:28:56 +0000958 {
Alexandre Julliard9d802152002-05-24 21:20:27 +0000959 fatal_protocol_error( current, "init_process: called twice\n" );
960 return;
Alexandre Julliard6543a652002-03-29 18:28:56 +0000961 }
Alexandre Julliarde55d5932003-10-14 01:30:42 +0000962 if (!req->peb || (unsigned int)req->peb % sizeof(int))
963 {
964 fatal_protocol_error( current, "init_process: bad peb address\n" );
965 return;
966 }
967 if (!req->ldt_copy || (unsigned int)req->ldt_copy % sizeof(int))
968 {
969 fatal_protocol_error( current, "init_process: bad ldt_copy address\n" );
970 return;
971 }
Alexandre Julliard9d802152002-05-24 21:20:27 +0000972 reply->info_size = 0;
Alexandre Julliarde55d5932003-10-14 01:30:42 +0000973 current->process->peb = req->peb;
Alexandre Julliard9d802152002-05-24 21:20:27 +0000974 current->process->ldt_copy = req->ldt_copy;
Alexandre Julliard77c8b1d2003-02-24 20:51:50 +0000975 current->process->startup_info = init_process( reply );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000976}
977
Alexandre Julliardec7bb231999-11-12 03:35:25 +0000978/* signal the end of the process initialization */
979DECL_HANDLER(init_process_done)
980{
Alexandre Julliardac2e4f12001-10-25 19:52:12 +0000981 struct file *file = NULL;
Alexandre Julliardec7bb231999-11-12 03:35:25 +0000982 struct process *process = current->process;
Alexandre Julliardac2e4f12001-10-25 19:52:12 +0000983
Alexandre Julliard9d802152002-05-24 21:20:27 +0000984 if (is_process_init_done(process))
Alexandre Julliardec7bb231999-11-12 03:35:25 +0000985 {
Alexandre Julliard9d802152002-05-24 21:20:27 +0000986 fatal_protocol_error( current, "init_process_done: called twice\n" );
987 return;
988 }
989 if (!req->module)
990 {
991 fatal_protocol_error( current, "init_process_done: module base address cannot be 0\n" );
Alexandre Julliardec7bb231999-11-12 03:35:25 +0000992 return;
993 }
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000994 process->exe.base = req->module;
Alexandre Julliardaeb56602002-03-22 00:21:23 +0000995 process->exe.size = req->module_size;
Alexandre Julliarda37dec02000-06-08 00:57:24 +0000996 process->exe.name = req->name;
Alexandre Julliardad29b902001-01-05 22:24:15 +0000997
Alexandre Julliardca96de32002-05-25 21:15:03 +0000998 if (req->exe_file) file = get_file_obj( process, req->exe_file, GENERIC_READ );
Alexandre Julliardac2e4f12001-10-25 19:52:12 +0000999 if (process->exe.file) release_object( process->exe.file );
1000 process->exe.file = file;
1001
Alexandre Julliardaeb56602002-03-22 00:21:23 +00001002 if ((process->exe.namelen = get_req_data_size()))
1003 process->exe.filename = memdup( get_req_data(), process->exe.namelen );
1004
Alexandre Julliard9d802152002-05-24 21:20:27 +00001005 generate_startup_debug_events( process, req->entry );
1006 set_process_startup_state( process, STARTUP_DONE );
1007
Alexandre Julliardc5e433a2000-05-30 19:48:18 +00001008 if (req->gui) process->idle_event = create_event( NULL, 0, 1, 0 );
Alexandre Julliardca96de32002-05-25 21:15:03 +00001009 if (current->suspend + process->suspend > 0) stop_thread( current );
Alexandre Julliarde55d5932003-10-14 01:30:42 +00001010 if (process->debugger) set_process_debug_flag( process, 1 );
Alexandre Julliardec7bb231999-11-12 03:35:25 +00001011}
1012
Alexandre Julliard43c190e1999-05-15 10:48:19 +00001013/* open a handle to a process */
1014DECL_HANDLER(open_process)
1015{
Alexandre Julliard43c190e1999-05-15 10:48:19 +00001016 struct process *process = get_process_from_id( req->pid );
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001017 reply->handle = 0;
Alexandre Julliard43c190e1999-05-15 10:48:19 +00001018 if (process)
1019 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001020 reply->handle = alloc_handle( current->process, process, req->access, req->inherit );
Alexandre Julliard43c190e1999-05-15 10:48:19 +00001021 release_object( process );
1022 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +00001023}
1024
1025/* terminate a process */
1026DECL_HANDLER(terminate_process)
1027{
1028 struct process *process;
1029
1030 if ((process = get_process_from_handle( req->handle, PROCESS_TERMINATE )))
1031 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001032 reply->self = (current->process == process);
Alexandre Julliard12f29b52000-03-17 15:16:57 +00001033 kill_process( process, current, req->exit_code );
Alexandre Julliard43c190e1999-05-15 10:48:19 +00001034 release_object( process );
1035 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +00001036}
1037
1038/* fetch information about a process */
1039DECL_HANDLER(get_process_info)
1040{
1041 struct process *process;
Alexandre Julliard43c190e1999-05-15 10:48:19 +00001042
1043 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1044 {
Alexandre Julliarde55d5932003-10-14 01:30:42 +00001045 reply->pid = get_process_id( process );
1046 reply->exit_code = process->exit_code;
1047 reply->priority = process->priority;
1048 reply->process_affinity = process->affinity;
1049 reply->system_affinity = 1;
Alexandre Julliard43c190e1999-05-15 10:48:19 +00001050 release_object( process );
1051 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +00001052}
1053
1054/* set information about a process */
1055DECL_HANDLER(set_process_info)
1056{
1057 struct process *process;
1058
1059 if ((process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION )))
1060 {
Alexandre Julliarde55d5932003-10-14 01:30:42 +00001061 if (req->mask & SET_PROCESS_INFO_PRIORITY) process->priority = req->priority;
1062 if (req->mask & SET_PROCESS_INFO_AFFINITY)
1063 {
1064 if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER );
1065 else process->affinity = req->affinity;
1066 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +00001067 release_object( process );
1068 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +00001069}
Alexandre Julliard8b8828f1999-11-12 21:39:14 +00001070
1071/* read data from a process address space */
1072DECL_HANDLER(read_process_memory)
1073{
1074 struct process *process;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001075 size_t len = get_reply_max_size();
Alexandre Julliard8b8828f1999-11-12 21:39:14 +00001076
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001077 if (!(process = get_process_from_handle( req->handle, PROCESS_VM_READ ))) return;
1078
1079 if (len)
Alexandre Julliard8b8828f1999-11-12 21:39:14 +00001080 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001081 unsigned int start_offset = (unsigned int)req->addr % sizeof(int);
1082 unsigned int nb_ints = (len + start_offset + sizeof(int) - 1) / sizeof(int);
1083 const int *start = (int *)((char *)req->addr - start_offset);
1084 int *buffer = mem_alloc( nb_ints * sizeof(int) );
1085 if (buffer)
1086 {
1087 if (read_process_memory( process, start, nb_ints, buffer ))
1088 {
1089 /* move start of requested data to start of buffer */
1090 if (start_offset) memmove( buffer, (char *)buffer + start_offset, len );
1091 set_reply_data_ptr( buffer, len );
1092 }
1093 else len = 0;
1094 }
Alexandre Julliard8b8828f1999-11-12 21:39:14 +00001095 }
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001096 release_object( process );
Alexandre Julliard8b8828f1999-11-12 21:39:14 +00001097}
Alexandre Julliardeef70251999-11-13 19:54:54 +00001098
1099/* write data to a process address space */
1100DECL_HANDLER(write_process_memory)
1101{
1102 struct process *process;
1103
1104 if ((process = get_process_from_handle( req->handle, PROCESS_VM_WRITE )))
1105 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001106 size_t len = get_req_data_size();
1107 if ((len % sizeof(int)) || ((unsigned int)req->addr % sizeof(int)))
1108 set_error( STATUS_INVALID_PARAMETER );
1109 else
1110 {
1111 if (len) write_process_memory( process, req->addr, len / sizeof(int),
1112 req->first_mask, req->last_mask, get_req_data() );
1113 }
Alexandre Julliardeef70251999-11-13 19:54:54 +00001114 release_object( process );
1115 }
1116}
Alexandre Julliard05f0b712000-03-09 18:18:41 +00001117
1118/* notify the server that a dll has been loaded */
1119DECL_HANDLER(load_dll)
1120{
1121 struct process_dll *dll;
1122 struct file *file = NULL;
1123
Alexandre Julliard670711e2004-04-06 23:13:47 +00001124 if (req->handle && !(file = get_file_obj( current->process, req->handle, GENERIC_READ )))
1125 return;
Alexandre Julliard8081e5a2001-01-05 04:08:07 +00001126
Alexandre Julliardaeb56602002-03-22 00:21:23 +00001127 if ((dll = process_load_dll( current->process, file, req->base,
1128 get_req_data(), get_req_data_size() )))
Alexandre Julliard05f0b712000-03-09 18:18:41 +00001129 {
Alexandre Julliardaeb56602002-03-22 00:21:23 +00001130 dll->size = req->size;
Alexandre Julliard05f0b712000-03-09 18:18:41 +00001131 dll->dbg_offset = req->dbg_offset;
1132 dll->dbg_size = req->dbg_size;
1133 dll->name = req->name;
1134 /* only generate event if initialization is done */
Alexandre Julliard9d802152002-05-24 21:20:27 +00001135 if (is_process_init_done( current->process ))
Alexandre Julliard05f0b712000-03-09 18:18:41 +00001136 generate_debug_event( current, LOAD_DLL_DEBUG_EVENT, dll );
1137 }
1138 if (file) release_object( file );
1139}
1140
1141/* notify the server that a dll is being unloaded */
1142DECL_HANDLER(unload_dll)
1143{
1144 process_unload_dll( current->process, req->base );
1145}
Alexandre Julliardc5e433a2000-05-30 19:48:18 +00001146
Eric Pouech2359b572003-01-09 00:01:28 +00001147/* retrieve information about a module in a process */
1148DECL_HANDLER(get_dll_info)
1149{
1150 struct process *process;
1151
1152 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1153 {
1154 struct process_dll *dll;
1155
1156 for (dll = &process->exe; dll; dll = dll->next)
1157 {
1158 if (dll->base == req->base_address)
1159 {
1160 reply->size = dll->size;
1161 reply->entry_point = 0; /* FIXME */
1162 if (dll->filename)
1163 {
1164 size_t len = min( dll->namelen, get_reply_max_size() );
1165 set_reply_data( dll->filename, len );
1166 }
1167 break;
1168 }
1169 }
1170 if (!dll)
1171 set_error(STATUS_DLL_NOT_FOUND);
1172 release_object( process );
1173 }
1174}
1175
Alexandre Julliardc5e433a2000-05-30 19:48:18 +00001176/* wait for a process to start waiting on input */
1177/* FIXME: only returns event for now, wait is done in the client */
1178DECL_HANDLER(wait_input_idle)
1179{
1180 struct process *process;
1181
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001182 reply->event = 0;
Alexandre Julliardc5e433a2000-05-30 19:48:18 +00001183 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1184 {
1185 if (process->idle_event && process != current->process && process->queue != current->queue)
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001186 reply->event = alloc_handle( current->process, process->idle_event,
1187 EVENT_ALL_ACCESS, 0 );
Alexandre Julliardc5e433a2000-05-30 19:48:18 +00001188 release_object( process );
1189 }
1190}