blob: e82cc38f3c6dbf93404b4e57e010ebe594aac563 [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>
Steven Edwards57279182005-03-04 12:38:36 +000036#ifdef HAVE_POLL_H
37#include <poll.h>
38#endif
Alexandre Julliard767e6f61998-08-09 12:47:43 +000039
Ge van Geldorp1a1583a2005-11-28 17:32:54 +010040#include "ntstatus.h"
41#define WIN32_NO_STATUS
Eric Pouechb3badc72005-09-06 10:25:11 +000042#include "winternl.h"
Alexandre Julliard642d3131998-07-12 19:29:36 +000043
Alexandre Julliard863637b2003-01-30 00:26:44 +000044#include "file.h"
Alexandre Julliard43c190e1999-05-15 10:48:19 +000045#include "handle.h"
46#include "process.h"
47#include "thread.h"
Alexandre Julliard5bc78081999-06-22 17:26:53 +000048#include "request.h"
Eric Pouech0b83d4c2001-11-23 23:04:58 +000049#include "console.h"
Alexandre Julliardbfce1512003-12-10 04:08:06 +000050#include "user.h"
Robert Shearmand2ea92d2005-04-22 21:17:15 +000051#include "security.h"
Alexandre Julliard642d3131998-07-12 19:29:36 +000052
Alexandre Julliardf692d441999-03-21 19:23:54 +000053/* process structure */
Alexandre Julliard43c190e1999-05-15 10:48:19 +000054
Alexandre Julliard48c0d512005-02-25 19:31:26 +000055static struct list process_list = LIST_INIT(process_list);
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +000056static int running_processes;
Alexandre Julliard642d3131998-07-12 19:29:36 +000057
58/* process operations */
59
Alexandre Julliard338e7571998-12-27 15:28:54 +000060static void process_dump( struct object *obj, int verbose );
Alexandre Julliard85ed45e1998-08-22 19:03:56 +000061static int process_signaled( struct object *obj, struct thread *thread );
Alexandre Julliard46d1b3e2005-12-12 15:03:07 +010062static unsigned int process_map_access( struct object *obj, unsigned int access );
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +000063static void process_poll_event( struct fd *fd, int event );
Alexandre Julliard338e7571998-12-27 15:28:54 +000064static void process_destroy( struct object *obj );
Alexandre Julliard642d3131998-07-12 19:29:36 +000065
66static const struct object_ops process_ops =
67{
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000068 sizeof(struct process), /* size */
69 process_dump, /* dump */
70 add_queue, /* add_queue */
71 remove_queue, /* remove_queue */
72 process_signaled, /* signaled */
73 no_satisfied, /* satisfied */
Alexandre Julliardb9b1ea92005-06-09 15:39:52 +000074 no_signal, /* signal */
Alexandre Julliard863637b2003-01-30 00:26:44 +000075 no_get_fd, /* get_fd */
Alexandre Julliard46d1b3e2005-12-12 15:03:07 +010076 process_map_access, /* map_access */
Vitaliy Margolenbaffcb92005-11-22 14:55:42 +000077 no_lookup_name, /* lookup_name */
Alexandre Julliardb9b1ea92005-06-09 15:39:52 +000078 no_close_handle, /* close_handle */
Alexandre Julliard863637b2003-01-30 00:26:44 +000079 process_destroy /* destroy */
80};
81
82static const struct fd_ops process_fd_ops =
83{
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000084 NULL, /* get_poll_events */
Alexandre Julliardf5242402001-02-28 21:45:23 +000085 process_poll_event, /* poll_event */
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000086 no_flush, /* flush */
87 no_get_file_info, /* get_file_info */
Eric Pouech46344472005-01-14 19:54:38 +000088 no_queue_async, /* queue_async */
89 no_cancel_async /* cancel async */
Alexandre Julliard642d3131998-07-12 19:29:36 +000090};
91
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +000092/* process startup info */
93
94struct startup_info
Alexandre Julliard2fe57772000-01-25 01:40:27 +000095{
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +000096 struct object obj; /* object header */
Alexandre Julliard77c8b1d2003-02-24 20:51:50 +000097 struct list entry; /* entry in list of startup infos */
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +000098 int inherit_all; /* inherit all handles from parent */
Alexandre Julliard01caa5e2005-07-12 20:27:09 +000099 unsigned int create_flags; /* creation flags */
Alexandre Julliardbaf0a062003-03-11 01:48:53 +0000100 int unix_pid; /* Unix pid of new process */
Alexandre Julliard51885742002-05-30 20:12:58 +0000101 obj_handle_t hstdin; /* handle for stdin */
102 obj_handle_t hstdout; /* handle for stdout */
103 obj_handle_t hstderr; /* handle for stderr */
Alexandre Julliardd27624b2000-05-03 18:42:40 +0000104 struct file *exe_file; /* file handle for main exe */
Alexandre Julliarde9936d92001-01-26 00:22:26 +0000105 struct thread *owner; /* owner thread (the one that created the new process) */
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000106 struct process *process; /* created process */
107 struct thread *thread; /* created thread */
Alexandre Julliard6543a652002-03-29 18:28:56 +0000108 size_t data_size; /* size of startup data */
Alexandre Julliard841f8982003-10-04 04:09:41 +0000109 void *data; /* data for startup info */
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000110};
111
112static void startup_info_dump( struct object *obj, int verbose );
113static int startup_info_signaled( struct object *obj, struct thread *thread );
114static void startup_info_destroy( struct object *obj );
115
116static const struct object_ops startup_info_ops =
117{
118 sizeof(struct startup_info), /* size */
119 startup_info_dump, /* dump */
120 add_queue, /* add_queue */
121 remove_queue, /* remove_queue */
122 startup_info_signaled, /* signaled */
123 no_satisfied, /* satisfied */
Mike McCormackf92fff62005-04-24 17:35:52 +0000124 no_signal, /* signal */
Alexandre Julliard1ab243b2000-12-19 02:12:45 +0000125 no_get_fd, /* get_fd */
Alexandre Julliard28beba32005-12-12 14:57:40 +0100126 no_map_access, /* map_access */
Vitaliy Margolenbaffcb92005-11-22 14:55:42 +0000127 no_lookup_name, /* lookup_name */
Alexandre Julliardb9b1ea92005-06-09 15:39:52 +0000128 no_close_handle, /* close_handle */
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000129 startup_info_destroy /* destroy */
130};
131
Alexandre Julliardf692d441999-03-21 19:23:54 +0000132
Alexandre Julliard77c8b1d2003-02-24 20:51:50 +0000133static struct list startup_info_list = LIST_INIT(startup_info_list);
134
Alexandre Julliard91befe12003-02-01 01:38:40 +0000135struct ptid_entry
136{
137 void *ptr; /* entry ptr */
138 unsigned int next; /* next free entry */
139};
140
141static struct ptid_entry *ptid_entries; /* array of ptid entries */
142static unsigned int used_ptid_entries; /* number of entries in use */
143static unsigned int alloc_ptid_entries; /* number of allocated entries */
144static unsigned int next_free_ptid; /* next free entry */
145static unsigned int last_free_ptid; /* last free entry */
146
147#define PTID_OFFSET 8 /* offset for first ptid value */
148
149/* allocate a new process or thread id */
150unsigned int alloc_ptid( void *ptr )
151{
152 struct ptid_entry *entry;
153 unsigned int id;
154
155 if (used_ptid_entries < alloc_ptid_entries)
156 {
157 id = used_ptid_entries + PTID_OFFSET;
158 entry = &ptid_entries[used_ptid_entries++];
159 }
160 else if (next_free_ptid)
161 {
162 id = next_free_ptid;
163 entry = &ptid_entries[id - PTID_OFFSET];
164 if (!(next_free_ptid = entry->next)) last_free_ptid = 0;
165 }
166 else /* need to grow the array */
167 {
168 unsigned int count = alloc_ptid_entries + (alloc_ptid_entries / 2);
169 if (!count) count = 64;
170 if (!(entry = realloc( ptid_entries, count * sizeof(*entry) )))
171 {
172 set_error( STATUS_NO_MEMORY );
173 return 0;
174 }
175 ptid_entries = entry;
176 alloc_ptid_entries = count;
177 id = used_ptid_entries + PTID_OFFSET;
178 entry = &ptid_entries[used_ptid_entries++];
179 }
180
181 entry->ptr = ptr;
182 return id;
183}
184
185/* free a process or thread id */
186void free_ptid( unsigned int id )
187{
188 struct ptid_entry *entry = &ptid_entries[id - PTID_OFFSET];
189
190 entry->ptr = NULL;
191 entry->next = 0;
192
193 /* append to end of free list so that we don't reuse it too early */
194 if (last_free_ptid) ptid_entries[last_free_ptid - PTID_OFFSET].next = id;
195 else next_free_ptid = id;
196
197 last_free_ptid = id;
198}
199
200/* retrieve the pointer corresponding to a process or thread id */
201void *get_ptid_entry( unsigned int id )
202{
203 if (id < PTID_OFFSET) return NULL;
204 if (id - PTID_OFFSET >= used_ptid_entries) return NULL;
205 return ptid_entries[id - PTID_OFFSET].ptr;
206}
207
Alexandre Julliard05026382005-03-01 10:56:18 +0000208/* return the main thread of the process */
209struct thread *get_process_first_thread( struct process *process )
210{
211 struct list *ptr = list_head( &process->thread_list );
212 if (!ptr) return NULL;
213 return LIST_ENTRY( ptr, struct thread, proc_entry );
214}
215
Alexandre Julliard9d802152002-05-24 21:20:27 +0000216/* set the state of the process startup info */
217static void set_process_startup_state( struct process *process, enum startup_state state )
218{
Alexandre Julliardca96de32002-05-25 21:15:03 +0000219 if (process->startup_state == STARTUP_IN_PROGRESS) process->startup_state = state;
220 if (process->startup_info)
221 {
222 wake_up( &process->startup_info->obj, 0 );
223 release_object( process->startup_info );
224 process->startup_info = NULL;
225 }
Alexandre Julliard9d802152002-05-24 21:20:27 +0000226}
227
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000228/* create a new process and its main thread */
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000229struct thread *create_process( int fd )
Alexandre Julliard642d3131998-07-12 19:29:36 +0000230{
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000231 struct process *process;
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000232 struct thread *thread = NULL;
Alexandre Julliard8859d772001-03-01 22:13:49 +0000233 int request_pipe[2];
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000234
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000235 if (!(process = alloc_object( &process_ops ))) goto error;
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000236 process->parent = NULL;
Alexandre Julliard3da5f841999-05-16 16:54:54 +0000237 process->debugger = NULL;
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000238 process->handles = NULL;
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000239 process->msg_fd = NULL;
Dave Pickles717bf7e2000-02-13 16:04:14 +0000240 process->exit_code = STILL_ACTIVE;
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000241 process->running_threads = 0;
Eric Pouechb3badc72005-09-06 10:25:11 +0000242 process->priority = PROCESS_PRIOCLASS_NORMAL;
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000243 process->affinity = 1;
Alexandre Julliard3da5f841999-05-16 16:54:54 +0000244 process->suspend = 0;
Alexandre Julliardd083a7b1999-11-29 02:10:56 +0000245 process->create_flags = 0;
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000246 process->console = NULL;
Alexandre Julliardca96de32002-05-25 21:15:03 +0000247 process->startup_state = STARTUP_IN_PROGRESS;
Alexandre Julliard9d802152002-05-24 21:20:27 +0000248 process->startup_info = NULL;
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000249 process->idle_event = NULL;
250 process->queue = NULL;
Alexandre Julliarde55d5932003-10-14 01:30:42 +0000251 process->peb = NULL;
Alexandre Julliard0a7c1f62000-01-27 02:54:17 +0000252 process->ldt_copy = NULL;
Alexandre Julliard1bf96e02005-06-08 18:44:50 +0000253 process->winstation = 0;
Alexandre Julliard78a3e632005-06-09 12:07:12 +0000254 process->desktop = 0;
Robert Shearmand2ea92d2005-04-22 21:17:15 +0000255 process->token = token_create_admin();
Alexandre Julliard05026382005-03-01 10:56:18 +0000256 list_init( &process->thread_list );
Alexandre Julliardce613492003-03-18 05:04:33 +0000257 list_init( &process->locks );
Alexandre Julliardbfce1512003-12-10 04:08:06 +0000258 list_init( &process->classes );
Alexandre Julliarda9e0fb12005-03-02 10:20:09 +0000259 list_init( &process->dlls );
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000260
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000261 gettimeofday( &process->start_time, NULL );
Alexandre Julliard48c0d512005-02-25 19:31:26 +0000262 list_add_head( &process_list, &process->entry );
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000263
Alexandre Julliard01caa5e2005-07-12 20:27:09 +0000264 if (!(process->id = process->group_id = alloc_ptid( process ))) goto error;
Alexandre Julliard580da242003-03-12 22:38:14 +0000265 if (!(process->msg_fd = create_anonymous_fd( &process_fd_ops, fd, &process->obj ))) goto error;
Alexandre Julliard91befe12003-02-01 01:38:40 +0000266
Alexandre Julliard7f748242000-12-26 00:30:30 +0000267 /* create the main thread */
Alexandre Julliard8859d772001-03-01 22:13:49 +0000268 if (pipe( request_pipe ) == -1)
269 {
270 file_set_error();
271 goto error;
272 }
Alexandre Julliard4144b5b2002-06-20 23:21:27 +0000273 if (send_client_fd( process, request_pipe[1], 0 ) == -1)
274 {
275 close( request_pipe[0] );
276 close( request_pipe[1] );
277 goto error;
278 }
Alexandre Julliard8859d772001-03-01 22:13:49 +0000279 close( request_pipe[1] );
280 if (!(thread = create_thread( request_pipe[0], process ))) goto error;
Alexandre Julliard7f748242000-12-26 00:30:30 +0000281
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000282 set_fd_events( process->msg_fd, POLLIN ); /* start listening to events */
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000283 release_object( process );
284 return thread;
Alexandre Julliardf692d441999-03-21 19:23:54 +0000285
286 error:
Alexandre Julliard4144b5b2002-06-20 23:21:27 +0000287 if (process) release_object( process );
288 /* if we failed to start our first process, close everything down */
289 if (!running_processes) close_master_socket();
Alexandre Julliardf692d441999-03-21 19:23:54 +0000290 return NULL;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000291}
292
Alexandre Julliard77c8b1d2003-02-24 20:51:50 +0000293/* find the startup info for a given Unix process */
Alexandre Julliardbaf0a062003-03-11 01:48:53 +0000294inline static struct startup_info *find_startup_info( int unix_pid )
Alexandre Julliard77c8b1d2003-02-24 20:51:50 +0000295{
296 struct list *ptr;
297
298 LIST_FOR_EACH( ptr, &startup_info_list )
299 {
300 struct startup_info *info = LIST_ENTRY( ptr, struct startup_info, entry );
301 if (info->unix_pid == unix_pid) return info;
302 }
303 return NULL;
304}
305
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000306/* initialize the current process and fill in the request */
Alexandre Julliard11ad6a02005-07-13 19:43:35 +0000307size_t init_process( struct thread *thread )
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000308{
Alexandre Julliard0424f382005-07-13 12:12:43 +0000309 struct process *process = thread->process;
Alexandre Julliard77c8b1d2003-02-24 20:51:50 +0000310 struct thread *parent_thread = NULL;
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000311 struct process *parent = NULL;
Alexandre Julliard0424f382005-07-13 12:12:43 +0000312 struct startup_info *info;
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000313
Alexandre Julliard11ad6a02005-07-13 19:43:35 +0000314 if (process->startup_info) return process->startup_info->data_size; /* already initialized */
Alexandre Julliard0424f382005-07-13 12:12:43 +0000315
316 if ((info = find_startup_info( thread->unix_pid )))
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000317 {
Alexandre Julliard11ad6a02005-07-13 19:43:35 +0000318 if (info->thread) return info->data_size; /* already initialized */
Alexandre Julliard0424f382005-07-13 12:12:43 +0000319
320 info->thread = (struct thread *)grab_object( thread );
321 info->process = (struct process *)grab_object( process );
322 process->startup_info = (struct startup_info *)grab_object( info );
323
Alexandre Julliard77c8b1d2003-02-24 20:51:50 +0000324 parent_thread = info->owner;
325 parent = parent_thread->process;
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000326 process->parent = (struct process *)grab_object( parent );
Alexandre Julliard0424f382005-07-13 12:12:43 +0000327
328 /* set the process flags */
329 process->create_flags = info->create_flags;
330
331 if (info->inherit_all) process->handles = copy_handle_table( process, parent );
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000332 }
333
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000334 /* create the handle table */
Alexandre Julliard0424f382005-07-13 12:12:43 +0000335 if (!process->handles) process->handles = alloc_handle_table( process, 0 );
336 if (!process->handles)
337 {
338 fatal_protocol_error( thread, "Failed to allocate handle table\n" );
Alexandre Julliard11ad6a02005-07-13 19:43:35 +0000339 return 0;
Alexandre Julliard0424f382005-07-13 12:12:43 +0000340 }
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000341
Alexandre Julliard90af5a02006-03-27 12:57:17 +0200342 /* connect to the window station */
343 connect_process_winstation( process, parent_thread );
Alexandre Julliard0424f382005-07-13 12:12:43 +0000344
Alexandre Julliard11ad6a02005-07-13 19:43:35 +0000345 if (!info) return 0;
Alexandre Julliard0424f382005-07-13 12:12:43 +0000346
Alexandre Julliard0424f382005-07-13 12:12:43 +0000347 /* thread will be actually suspended in init_done */
348 if (info->create_flags & CREATE_SUSPENDED) thread->suspend++;
Alexandre Julliard1bf96e02005-06-08 18:44:50 +0000349
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000350 /* set the process console */
Alexandre Julliard0424f382005-07-13 12:12:43 +0000351 if (!(info->create_flags & (DETACHED_PROCESS | CREATE_NEW_CONSOLE)))
Alexandre Julliard01caa5e2005-07-12 20:27:09 +0000352 {
353 /* FIXME: some better error checking should be done...
354 * like if hConOut and hConIn are console handles, then they should be on the same
355 * physical console
356 */
357 inherit_console( parent_thread, process, info->inherit_all ? info->hstdin : 0 );
358 }
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000359
Alexandre Julliard0424f382005-07-13 12:12:43 +0000360 /* attach to the debugger if requested */
361 if (process->create_flags & (DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS))
362 set_process_debugger( process, parent_thread );
363 else if (parent->debugger && !(parent->create_flags & DEBUG_ONLY_THIS_PROCESS))
364 set_process_debugger( process, parent->debugger );
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000365
Alexandre Julliard0424f382005-07-13 12:12:43 +0000366 if (!(process->create_flags & CREATE_NEW_PROCESS_GROUP))
367 process->group_id = parent->group_id;
Alexandre Julliard11ad6a02005-07-13 19:43:35 +0000368
369 return info->data_size;
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000370}
371
Alexandre Julliard642d3131998-07-12 19:29:36 +0000372/* destroy a process when its refcount is 0 */
Alexandre Julliard338e7571998-12-27 15:28:54 +0000373static void process_destroy( struct object *obj )
Alexandre Julliard642d3131998-07-12 19:29:36 +0000374{
375 struct process *process = (struct process *)obj;
376 assert( obj->ops == &process_ops );
377
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000378 /* we can't have a thread remaining */
Alexandre Julliard05026382005-03-01 10:56:18 +0000379 assert( list_empty( &process->thread_list ));
Alexandre Julliard9d802152002-05-24 21:20:27 +0000380
381 set_process_startup_state( process, STARTUP_ABORTED );
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000382 if (process->console) release_object( process->console );
383 if (process->parent) release_object( process->parent );
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000384 if (process->msg_fd) release_object( process->msg_fd );
Alexandre Julliard48c0d512005-02-25 19:31:26 +0000385 list_remove( &process->entry );
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000386 if (process->idle_event) release_object( process->idle_event );
387 if (process->queue) release_object( process->queue );
Alexandre Julliard91befe12003-02-01 01:38:40 +0000388 if (process->id) free_ptid( process->id );
Mike McCormack36cd6f52003-07-24 00:07:00 +0000389 if (process->token) release_object( process->token );
Alexandre Julliard642d3131998-07-12 19:29:36 +0000390}
391
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000392/* dump a process on stdout for debugging purposes */
Alexandre Julliard338e7571998-12-27 15:28:54 +0000393static void process_dump( struct object *obj, int verbose )
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000394{
395 struct process *process = (struct process *)obj;
396 assert( obj->ops == &process_ops );
397
Alexandre Julliard48c0d512005-02-25 19:31:26 +0000398 fprintf( stderr, "Process id=%04x handles=%p\n", process->id, process->handles );
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000399}
400
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000401static int process_signaled( struct object *obj, struct thread *thread )
402{
403 struct process *process = (struct process *)obj;
Alexandre Julliarddbc033a1999-03-13 08:58:24 +0000404 return !process->running_threads;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000405}
406
Alexandre Julliard46d1b3e2005-12-12 15:03:07 +0100407static unsigned int process_map_access( struct object *obj, unsigned int access )
408{
409 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | SYNCHRONIZE;
410 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | SYNCHRONIZE;
411 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
412 if (access & GENERIC_ALL) access |= PROCESS_ALL_ACCESS;
413 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
414}
415
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000416static void process_poll_event( struct fd *fd, int event )
Alexandre Julliardf5242402001-02-28 21:45:23 +0000417{
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000418 struct process *process = get_fd_user( fd );
419 assert( process->obj.ops == &process_ops );
Alexandre Julliardf5242402001-02-28 21:45:23 +0000420
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000421 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
Alexandre Julliardf5242402001-02-28 21:45:23 +0000422 else if (event & POLLIN) receive_fd( process );
423}
424
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000425static void startup_info_destroy( struct object *obj )
426{
427 struct startup_info *info = (struct startup_info *)obj;
428 assert( obj->ops == &startup_info_ops );
Alexandre Julliard77c8b1d2003-02-24 20:51:50 +0000429 list_remove( &info->entry );
Alexandre Julliard6543a652002-03-29 18:28:56 +0000430 if (info->data) free( info->data );
Alexandre Julliardd27624b2000-05-03 18:42:40 +0000431 if (info->exe_file) release_object( info->exe_file );
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000432 if (info->process) release_object( info->process );
433 if (info->thread) release_object( info->thread );
Alexandre Julliard77c8b1d2003-02-24 20:51:50 +0000434 if (info->owner) release_object( info->owner );
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000435}
436
437static void startup_info_dump( struct object *obj, int verbose )
438{
439 struct startup_info *info = (struct startup_info *)obj;
440 assert( obj->ops == &startup_info_ops );
441
Alexandre Julliardb3332d72002-10-19 01:00:59 +0000442 fprintf( stderr, "Startup info flags=%x in=%p out=%p err=%p\n",
Alexandre Julliardca96de32002-05-25 21:15:03 +0000443 info->create_flags, info->hstdin, info->hstdout, info->hstderr );
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000444}
445
446static int startup_info_signaled( struct object *obj, struct thread *thread )
447{
448 struct startup_info *info = (struct startup_info *)obj;
Alexandre Julliard49266102006-02-21 19:30:17 +0100449 return info->process && info->process->startup_state != STARTUP_IN_PROGRESS;
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000450}
451
Alexandre Julliard642d3131998-07-12 19:29:36 +0000452/* get a process from an id (and increment the refcount) */
Alexandre Julliard54f22872002-10-03 19:54:57 +0000453struct process *get_process_from_id( process_id_t id )
Alexandre Julliard642d3131998-07-12 19:29:36 +0000454{
Alexandre Julliard91befe12003-02-01 01:38:40 +0000455 struct object *obj = get_ptid_entry( id );
456
457 if (obj && obj->ops == &process_ops) return (struct process *)grab_object( obj );
458 set_error( STATUS_INVALID_PARAMETER );
459 return NULL;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000460}
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000461
462/* get a process from a handle (and increment the refcount) */
Alexandre Julliard51885742002-05-30 20:12:58 +0000463struct process *get_process_from_handle( obj_handle_t handle, unsigned int access )
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000464{
465 return (struct process *)get_handle_obj( current->process, handle,
466 access, &process_ops );
467}
468
Alexandre Julliarda9e0fb12005-03-02 10:20:09 +0000469/* find a dll from its base address */
470static inline struct process_dll *find_process_dll( struct process *process, void *base )
471{
472 struct process_dll *dll;
473
Alexandre Julliarda9e0fb12005-03-02 10:20:09 +0000474 LIST_FOR_EACH_ENTRY( dll, &process->dlls, struct process_dll, entry )
475 {
476 if (dll->base == base) return dll;
477 }
478 return NULL;
479}
480
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000481/* add a dll to a process list */
482static struct process_dll *process_load_dll( struct process *process, struct file *file,
Alexandre Julliardc30cefb2003-09-30 01:04:19 +0000483 void *base, const WCHAR *filename, size_t name_len )
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000484{
485 struct process_dll *dll;
486
487 /* make sure we don't already have one with the same base address */
Alexandre Julliarda9e0fb12005-03-02 10:20:09 +0000488 if (find_process_dll( process, base ))
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000489 {
490 set_error( STATUS_INVALID_PARAMETER );
491 return NULL;
492 }
493
494 if ((dll = mem_alloc( sizeof(*dll) )))
495 {
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000496 dll->file = NULL;
497 dll->base = base;
Alexandre Julliardaeb56602002-03-22 00:21:23 +0000498 dll->filename = NULL;
499 dll->namelen = name_len;
500 if (name_len && !(dll->filename = memdup( filename, name_len )))
501 {
502 free( dll );
503 return NULL;
504 }
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000505 if (file) dll->file = (struct file *)grab_object( file );
Alexandre Julliarde6374cb2006-02-16 12:13:01 +0100506 list_add_tail( &process->dlls, &dll->entry );
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000507 }
508 return dll;
509}
510
511/* remove a dll from a process list */
512static void process_unload_dll( struct process *process, void *base )
513{
Alexandre Julliarda9e0fb12005-03-02 10:20:09 +0000514 struct process_dll *dll = find_process_dll( process, base );
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000515
Alexandre Julliarde6374cb2006-02-16 12:13:01 +0100516 if (dll && (&dll->entry != list_head( &process->dlls ))) /* main exe can't be unloaded */
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000517 {
Alexandre Julliarda9e0fb12005-03-02 10:20:09 +0000518 if (dll->file) release_object( dll->file );
519 if (dll->filename) free( dll->filename );
520 list_remove( &dll->entry );
521 free( dll );
522 generate_debug_event( current, UNLOAD_DLL_DEBUG_EVENT, base );
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000523 }
Alexandre Julliarda9e0fb12005-03-02 10:20:09 +0000524 else set_error( STATUS_INVALID_PARAMETER );
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000525}
526
Alexandre Julliard40043ed2002-08-16 20:02:15 +0000527/* kill all processes */
528void kill_all_processes( struct process *skip, int exit_code )
529{
530 for (;;)
531 {
Alexandre Julliard48c0d512005-02-25 19:31:26 +0000532 struct process *process;
Alexandre Julliard40043ed2002-08-16 20:02:15 +0000533
Alexandre Julliard48c0d512005-02-25 19:31:26 +0000534 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
535 {
536 if (process == skip) continue;
537 if (process->running_threads) break;
538 }
539 if (&process->entry == &process_list) break; /* no process found */
Alexandre Julliard40043ed2002-08-16 20:02:15 +0000540 kill_process( process, NULL, exit_code );
541 }
542}
543
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000544/* kill all processes being attached to a console renderer */
Eric Pouech3940d8a2001-12-04 20:17:43 +0000545void kill_console_processes( struct thread *renderer, int exit_code )
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000546{
547 for (;;) /* restart from the beginning of the list every time */
548 {
Alexandre Julliard48c0d512005-02-25 19:31:26 +0000549 struct process *process;
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000550
551 /* find the first process being attached to 'renderer' and still running */
Alexandre Julliard48c0d512005-02-25 19:31:26 +0000552 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000553 {
Alexandre Julliard48c0d512005-02-25 19:31:26 +0000554 if (process == renderer->process) continue;
555 if (!process->running_threads) continue;
556 if (process->console && process->console->renderer == renderer) break;
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000557 }
Alexandre Julliard48c0d512005-02-25 19:31:26 +0000558 if (&process->entry == &process_list) break; /* no process found */
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000559 kill_process( process, NULL, exit_code );
560 }
561}
562
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000563/* a process has been killed (i.e. its last thread died) */
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000564static void process_killed( struct process *process )
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000565{
Alexandre Julliard36b85d02005-06-29 19:29:15 +0000566 struct handle_table *handles;
Alexandre Julliarda9e0fb12005-03-02 10:20:09 +0000567 struct list *ptr;
568
Alexandre Julliard05026382005-03-01 10:56:18 +0000569 assert( list_empty( &process->thread_list ));
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000570 gettimeofday( &process->end_time, NULL );
Alexandre Julliardf978f612006-03-06 21:02:24 +0100571 close_process_desktop( process );
Alexandre Julliard36b85d02005-06-29 19:29:15 +0000572 handles = process->handles;
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000573 process->handles = NULL;
Alexandre Julliard36b85d02005-06-29 19:29:15 +0000574 if (handles) release_object( handles );
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000575
576 /* close the console attached to this process, if any */
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000577 free_console( process );
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000578
Alexandre Julliarda9e0fb12005-03-02 10:20:09 +0000579 while ((ptr = list_head( &process->dlls )))
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000580 {
Alexandre Julliarda9e0fb12005-03-02 10:20:09 +0000581 struct process_dll *dll = LIST_ENTRY( ptr, struct process_dll, entry );
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000582 if (dll->file) release_object( dll->file );
Alexandre Julliardaeb56602002-03-22 00:21:23 +0000583 if (dll->filename) free( dll->filename );
Alexandre Julliarda9e0fb12005-03-02 10:20:09 +0000584 list_remove( &dll->entry );
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000585 free( dll );
586 }
Alexandre Julliardbfce1512003-12-10 04:08:06 +0000587 destroy_process_classes( process );
Alexandre Julliard5f22e7c2005-06-14 19:23:56 +0000588 remove_process_locks( process );
Alexandre Julliard9d802152002-05-24 21:20:27 +0000589 set_process_startup_state( process, STARTUP_ABORTED );
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000590 wake_up( &process->obj, 0 );
Alexandre Julliard4144b5b2002-06-20 23:21:27 +0000591 if (!--running_processes) close_master_socket();
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000592}
593
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000594/* add a thread to a process running threads list */
595void add_process_thread( struct process *process, struct thread *thread )
596{
Alexandre Julliard05026382005-03-01 10:56:18 +0000597 list_add_head( &process->thread_list, &thread->proc_entry );
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +0000598 if (!process->running_threads++) running_processes++;
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000599 grab_object( thread );
600}
601
602/* remove a thread from a process running threads list */
603void remove_process_thread( struct process *process, struct thread *thread )
604{
605 assert( process->running_threads > 0 );
Alexandre Julliard05026382005-03-01 10:56:18 +0000606 assert( !list_empty( &process->thread_list ));
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000607
Alexandre Julliard05026382005-03-01 10:56:18 +0000608 list_remove( &thread->proc_entry );
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000609
610 if (!--process->running_threads)
611 {
612 /* we have removed the last running thread, exit the process */
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000613 process->exit_code = thread->exit_code;
614 generate_debug_event( thread, EXIT_PROCESS_DEBUG_EVENT, process );
615 process_killed( process );
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000616 }
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000617 else generate_debug_event( thread, EXIT_THREAD_DEBUG_EVENT, thread );
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000618 release_object( thread );
619}
620
Alexandre Julliard3da5f841999-05-16 16:54:54 +0000621/* suspend all the threads of a process */
622void suspend_process( struct process *process )
623{
624 if (!process->suspend++)
625 {
Alexandre Julliard05026382005-03-01 10:56:18 +0000626 struct list *ptr, *next;
627
628 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
Alexandre Julliard3da5f841999-05-16 16:54:54 +0000629 {
Alexandre Julliard05026382005-03-01 10:56:18 +0000630 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
Alexandre Julliard0a707831999-11-08 05:31:47 +0000631 if (!thread->suspend) stop_thread( thread );
Alexandre Julliard3da5f841999-05-16 16:54:54 +0000632 }
633 }
634}
635
636/* resume all the threads of a process */
637void resume_process( struct process *process )
638{
639 assert (process->suspend > 0);
640 if (!--process->suspend)
641 {
Alexandre Julliard05026382005-03-01 10:56:18 +0000642 struct list *ptr, *next;
643
644 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
Alexandre Julliard3da5f841999-05-16 16:54:54 +0000645 {
Alexandre Julliard05026382005-03-01 10:56:18 +0000646 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
Alexandre Julliardd04ccb82003-03-04 22:18:43 +0000647 if (!thread->suspend) wake_thread( thread );
Alexandre Julliard3da5f841999-05-16 16:54:54 +0000648 }
649 }
650}
651
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000652/* kill a process on the spot */
Alexandre Julliardf5242402001-02-28 21:45:23 +0000653void kill_process( struct process *process, struct thread *skip, int exit_code )
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000654{
Alexandre Julliard05026382005-03-01 10:56:18 +0000655 struct list *ptr, *next;
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000656
Alexandre Julliard05026382005-03-01 10:56:18 +0000657 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
Alexandre Julliard12f29b52000-03-17 15:16:57 +0000658 {
Alexandre Julliard05026382005-03-01 10:56:18 +0000659 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
660
Alexandre Julliard0a364622006-03-27 12:14:24 +0200661 if (exit_code) thread->exit_code = exit_code;
Alexandre Julliard12f29b52000-03-17 15:16:57 +0000662 if (thread != skip) kill_thread( thread, 1 );
Alexandre Julliard12f29b52000-03-17 15:16:57 +0000663 }
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000664}
665
Alexandre Julliard17cf8101999-11-24 01:22:14 +0000666/* kill all processes being debugged by a given thread */
667void kill_debugged_processes( struct thread *debugger, int exit_code )
668{
669 for (;;) /* restart from the beginning of the list every time */
670 {
Alexandre Julliard48c0d512005-02-25 19:31:26 +0000671 struct process *process;
672
Alexandre Julliard17cf8101999-11-24 01:22:14 +0000673 /* find the first process being debugged by 'debugger' and still running */
Alexandre Julliard48c0d512005-02-25 19:31:26 +0000674 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 Julliard17cf8101999-11-24 01:22:14 +0000680 process->debugger = NULL;
Alexandre Julliard12f29b52000-03-17 15:16:57 +0000681 kill_process( process, NULL, exit_code );
Alexandre Julliard17cf8101999-11-24 01:22:14 +0000682 }
683}
684
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000685
Alexandre Julliard17de8292006-04-19 19:45:39 +0200686/* trigger a breakpoint event in a given process */
687void break_process( struct process *process )
688{
689 struct thread *thread;
690
691 suspend_process( process );
692
693 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
694 {
695 if (thread->context) /* inside an exception event already */
696 {
697 break_thread( thread );
698 goto done;
699 }
700 }
701 if ((thread = get_process_first_thread( process ))) thread->debug_break = 1;
702 else set_error( STATUS_ACCESS_DENIED );
703done:
704 resume_process( process );
705}
706
707
Eric Pouechfbccb382002-02-27 01:28:30 +0000708/* detach a debugger from all its debuggees */
709void detach_debugged_processes( struct thread *debugger )
710{
711 struct process *process;
Alexandre Julliard48c0d512005-02-25 19:31:26 +0000712
713 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
Eric Pouechfbccb382002-02-27 01:28:30 +0000714 {
715 if (process->debugger == debugger && process->running_threads)
716 {
717 debugger_detach( process, debugger );
718 }
719 }
720}
721
722
Eric Pouech93bfa0d2002-06-02 21:22:22 +0000723void enum_processes( int (*cb)(struct process*, void*), void *user )
724{
Alexandre Julliard48c0d512005-02-25 19:31:26 +0000725 struct list *ptr, *next;
726
727 LIST_FOR_EACH_SAFE( ptr, next, &process_list )
Eric Pouech93bfa0d2002-06-02 21:22:22 +0000728 {
Alexandre Julliard48c0d512005-02-25 19:31:26 +0000729 struct process *process = LIST_ENTRY( ptr, struct process, entry );
Eric Pouech93bfa0d2002-06-02 21:22:22 +0000730 if ((cb)(process, user)) break;
731 }
732}
733
Alexandre Julliarde55d5932003-10-14 01:30:42 +0000734/* set the debugged flag in the process PEB */
735int set_process_debug_flag( struct process *process, int flag )
736{
Alexandre Julliard959bbf82006-04-07 18:41:58 +0200737 char data = (flag != 0);
Alexandre Julliarde55d5932003-10-14 01:30:42 +0000738
739 /* BeingDebugged flag is the byte at offset 2 in the PEB */
Alexandre Julliard959bbf82006-04-07 18:41:58 +0200740 return write_process_memory( process, (char *)process->peb + 2, 1, &data );
Alexandre Julliard8b8828f1999-11-12 21:39:14 +0000741}
742
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +0000743/* take a snapshot of currently running processes */
744struct process_snapshot *process_snap( int *count )
745{
746 struct process_snapshot *snapshot, *ptr;
747 struct process *process;
748 if (!running_processes) return NULL;
749 if (!(snapshot = mem_alloc( sizeof(*snapshot) * running_processes )))
750 return NULL;
751 ptr = snapshot;
Alexandre Julliard48c0d512005-02-25 19:31:26 +0000752 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +0000753 {
754 if (!process->running_threads) continue;
755 ptr->process = process;
756 ptr->threads = process->running_threads;
Alexandre Julliard07d84462000-04-16 19:45:05 +0000757 ptr->count = process->obj.refcount;
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +0000758 ptr->priority = process->priority;
Eric Pouech9fd54b22003-09-16 01:07:21 +0000759 ptr->handles = get_handle_table_count(process);
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +0000760 grab_object( process );
761 ptr++;
762 }
763 *count = running_processes;
764 return snapshot;
765}
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000766
Alexandre Julliard07d84462000-04-16 19:45:05 +0000767/* take a snapshot of the modules of a process */
768struct module_snapshot *module_snap( struct process *process, int *count )
769{
770 struct module_snapshot *snapshot, *ptr;
771 struct process_dll *dll;
Alexandre Julliarde6374cb2006-02-16 12:13:01 +0100772 int total = 0;
Alexandre Julliard07d84462000-04-16 19:45:05 +0000773
Alexandre Julliarda9e0fb12005-03-02 10:20:09 +0000774 LIST_FOR_EACH_ENTRY( dll, &process->dlls, struct process_dll, entry ) total++;
Alexandre Julliard07d84462000-04-16 19:45:05 +0000775 if (!(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
776
Alexandre Julliarde6374cb2006-02-16 12:13:01 +0100777 ptr = snapshot;
Alexandre Julliarda9e0fb12005-03-02 10:20:09 +0000778 LIST_FOR_EACH_ENTRY( dll, &process->dlls, struct process_dll, entry )
Alexandre Julliard07d84462000-04-16 19:45:05 +0000779 {
Alexandre Julliardaeb56602002-03-22 00:21:23 +0000780 ptr->base = dll->base;
781 ptr->size = dll->size;
782 ptr->namelen = dll->namelen;
783 ptr->filename = memdup( dll->filename, dll->namelen );
Alexandre Julliarda9e0fb12005-03-02 10:20:09 +0000784 ptr++;
Alexandre Julliard07d84462000-04-16 19:45:05 +0000785 }
786 *count = total;
787 return snapshot;
788}
789
790
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000791/* create a new process */
792DECL_HANDLER(new_process)
793{
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000794 struct startup_info *info;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000795
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000796 /* build the startup info for a new process */
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000797 if (!(info = alloc_object( &startup_info_ops ))) return;
Alexandre Julliard77c8b1d2003-02-24 20:51:50 +0000798 list_add_head( &startup_info_list, &info->entry );
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000799 info->inherit_all = req->inherit_all;
800 info->create_flags = req->create_flags;
Alexandre Julliard77c8b1d2003-02-24 20:51:50 +0000801 info->unix_pid = req->unix_pid;
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000802 info->hstdin = req->hstdin;
803 info->hstdout = req->hstdout;
804 info->hstderr = req->hstderr;
Alexandre Julliardd27624b2000-05-03 18:42:40 +0000805 info->exe_file = NULL;
Alexandre Julliarde9936d92001-01-26 00:22:26 +0000806 info->owner = (struct thread *)grab_object( current );
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000807 info->process = NULL;
808 info->thread = NULL;
Alexandre Julliard6543a652002-03-29 18:28:56 +0000809 info->data_size = get_req_data_size();
810 info->data = NULL;
Alexandre Julliard6a72dc52000-04-14 13:42:00 +0000811
Alexandre Julliard8081e5a2001-01-05 04:08:07 +0000812 if (req->exe_file &&
Alexandre Julliarda510a7e2005-12-12 16:46:17 +0100813 !(info->exe_file = get_file_obj( current->process, req->exe_file, FILE_READ_DATA )))
Alexandre Julliarde9936d92001-01-26 00:22:26 +0000814 goto done;
Alexandre Julliardd27624b2000-05-03 18:42:40 +0000815
Alexandre Julliard841f8982003-10-04 04:09:41 +0000816 if (!(info->data = memdup( get_req_data(), info->data_size ))) goto done;
Alexandre Julliard24560e72005-12-09 13:58:25 +0100817 reply->info = alloc_handle( current->process, info, SYNCHRONIZE, 0 );
Alexandre Julliarde9936d92001-01-26 00:22:26 +0000818
819 done:
820 release_object( info );
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000821}
Alexandre Julliard6a72dc52000-04-14 13:42:00 +0000822
Alexandre Julliarde9936d92001-01-26 00:22:26 +0000823/* Retrieve information about a newly started process */
824DECL_HANDLER(get_new_process_info)
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000825{
Alexandre Julliarde9936d92001-01-26 00:22:26 +0000826 struct startup_info *info;
827
Alexandre Julliarde9936d92001-01-26 00:22:26 +0000828 if ((info = (struct startup_info *)get_handle_obj( current->process, req->info,
829 0, &startup_info_ops )))
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000830 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000831 reply->pid = get_process_id( info->process );
832 reply->tid = get_thread_id( info->thread );
833 reply->phandle = alloc_handle( current->process, info->process,
Alexandre Julliard24560e72005-12-09 13:58:25 +0100834 req->process_access, req->process_attr );
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000835 reply->thandle = alloc_handle( current->process, info->thread,
Alexandre Julliard24560e72005-12-09 13:58:25 +0100836 req->thread_access, req->thread_attr );
Alexandre Julliardca96de32002-05-25 21:15:03 +0000837 reply->success = is_process_init_done( info->process );
Alexandre Julliarde9936d92001-01-26 00:22:26 +0000838 release_object( info );
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000839 }
840 else
841 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000842 reply->pid = 0;
843 reply->tid = 0;
844 reply->phandle = 0;
845 reply->thandle = 0;
Alexandre Julliard9d802152002-05-24 21:20:27 +0000846 reply->success = 0;
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000847 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000848}
849
Alexandre Julliard6543a652002-03-29 18:28:56 +0000850/* Retrieve the new process startup info */
851DECL_HANDLER(get_startup_info)
852{
Alexandre Julliard01caa5e2005-07-12 20:27:09 +0000853 struct process *process = current->process;
854 struct startup_info *info = process->startup_info;
855 size_t size;
Alexandre Julliard6543a652002-03-29 18:28:56 +0000856
Alexandre Julliard01caa5e2005-07-12 20:27:09 +0000857 if (!info) return;
858
Alexandre Julliard01caa5e2005-07-12 20:27:09 +0000859 if (info->exe_file &&
860 !(reply->exe_file = alloc_handle( process, info->exe_file, GENERIC_READ, 0 ))) return;
861
862 if (!info->inherit_all && !(info->create_flags & CREATE_NEW_CONSOLE))
Alexandre Julliard6543a652002-03-29 18:28:56 +0000863 {
Alexandre Julliard01caa5e2005-07-12 20:27:09 +0000864 struct process *parent_process = info->owner->process;
865 reply->hstdin = duplicate_handle( parent_process, info->hstdin, process,
Alexandre Julliard24560e72005-12-09 13:58:25 +0100866 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
Alexandre Julliard01caa5e2005-07-12 20:27:09 +0000867 reply->hstdout = duplicate_handle( parent_process, info->hstdout, process,
Alexandre Julliard24560e72005-12-09 13:58:25 +0100868 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
Alexandre Julliard01caa5e2005-07-12 20:27:09 +0000869 reply->hstderr = duplicate_handle( parent_process, info->hstderr, process,
Alexandre Julliard24560e72005-12-09 13:58:25 +0100870 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
Alexandre Julliard01caa5e2005-07-12 20:27:09 +0000871 /* some handles above may have been invalid; this is not an error */
872 if (get_error() == STATUS_INVALID_HANDLE ||
873 get_error() == STATUS_OBJECT_TYPE_MISMATCH) clear_error();
Alexandre Julliard6543a652002-03-29 18:28:56 +0000874 }
Alexandre Julliard01caa5e2005-07-12 20:27:09 +0000875 else
876 {
877 reply->hstdin = info->hstdin;
878 reply->hstdout = info->hstdout;
879 reply->hstderr = info->hstderr;
880 }
881
882 /* we return the data directly without making a copy so this can only be called once */
883 size = info->data_size;
884 if (size > get_reply_max_size()) size = get_reply_max_size();
885 set_reply_data_ptr( info->data, size );
886 info->data = NULL;
887 info->data_size = 0;
Alexandre Julliard6543a652002-03-29 18:28:56 +0000888}
889
Alexandre Julliardec7bb231999-11-12 03:35:25 +0000890/* signal the end of the process initialization */
891DECL_HANDLER(init_process_done)
892{
Alexandre Julliard2b0033d2006-02-08 15:07:16 +0100893 struct process_dll *dll;
Alexandre Julliardec7bb231999-11-12 03:35:25 +0000894 struct process *process = current->process;
Alexandre Julliardac2e4f12001-10-25 19:52:12 +0000895
Alexandre Julliard9d802152002-05-24 21:20:27 +0000896 if (is_process_init_done(process))
Alexandre Julliardec7bb231999-11-12 03:35:25 +0000897 {
Alexandre Julliard9d802152002-05-24 21:20:27 +0000898 fatal_protocol_error( current, "init_process_done: called twice\n" );
899 return;
900 }
Alexandre Julliarde6374cb2006-02-16 12:13:01 +0100901 if (!(dll = find_process_dll( process, req->module )))
Alexandre Julliard2b0033d2006-02-08 15:07:16 +0100902 {
Alexandre Julliarde27358e2006-02-21 20:08:19 +0100903 set_error( STATUS_DLL_NOT_FOUND );
904 return;
Alexandre Julliard2b0033d2006-02-08 15:07:16 +0100905 }
Alexandre Julliarde6374cb2006-02-16 12:13:01 +0100906
907 /* main exe is the first in the dll list */
908 list_remove( &dll->entry );
909 list_add_head( &process->dlls, &dll->entry );
Alexandre Julliardaeb56602002-03-22 00:21:23 +0000910
Alexandre Julliard9d802152002-05-24 21:20:27 +0000911 generate_startup_debug_events( process, req->entry );
912 set_process_startup_state( process, STARTUP_DONE );
913
Vitaliy Margolenf676bc82005-12-02 15:55:48 +0100914 if (req->gui) process->idle_event = create_event( NULL, NULL, 0, 1, 0 );
Alexandre Julliardca96de32002-05-25 21:15:03 +0000915 if (current->suspend + process->suspend > 0) stop_thread( current );
Alexandre Julliarde55d5932003-10-14 01:30:42 +0000916 if (process->debugger) set_process_debug_flag( process, 1 );
Alexandre Julliardec7bb231999-11-12 03:35:25 +0000917}
918
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000919/* open a handle to a process */
920DECL_HANDLER(open_process)
921{
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000922 struct process *process = get_process_from_id( req->pid );
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000923 reply->handle = 0;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000924 if (process)
925 {
Alexandre Julliard24560e72005-12-09 13:58:25 +0100926 reply->handle = alloc_handle( current->process, process, req->access, req->attributes );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000927 release_object( process );
928 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000929}
930
931/* terminate a process */
932DECL_HANDLER(terminate_process)
933{
934 struct process *process;
935
936 if ((process = get_process_from_handle( req->handle, PROCESS_TERMINATE )))
937 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000938 reply->self = (current->process == process);
Alexandre Julliard12f29b52000-03-17 15:16:57 +0000939 kill_process( process, current, req->exit_code );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000940 release_object( process );
941 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000942}
943
944/* fetch information about a process */
945DECL_HANDLER(get_process_info)
946{
947 struct process *process;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000948
949 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
950 {
Alexandre Julliarde55d5932003-10-14 01:30:42 +0000951 reply->pid = get_process_id( process );
Eric Pouechb0fd2ad2004-06-14 17:02:00 +0000952 reply->ppid = process->parent ? get_process_id( process->parent ) : 0;
Alexandre Julliarde55d5932003-10-14 01:30:42 +0000953 reply->exit_code = process->exit_code;
954 reply->priority = process->priority;
Eric Pouechb09582a2005-09-27 10:52:10 +0000955 reply->affinity = process->affinity;
Eric Pouechb0fd2ad2004-06-14 17:02:00 +0000956 reply->peb = process->peb;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000957 release_object( process );
958 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000959}
960
961/* set information about a process */
962DECL_HANDLER(set_process_info)
963{
964 struct process *process;
965
966 if ((process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION )))
967 {
Alexandre Julliarde55d5932003-10-14 01:30:42 +0000968 if (req->mask & SET_PROCESS_INFO_PRIORITY) process->priority = req->priority;
969 if (req->mask & SET_PROCESS_INFO_AFFINITY)
970 {
971 if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER );
972 else process->affinity = req->affinity;
973 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000974 release_object( process );
975 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000976}
Alexandre Julliard8b8828f1999-11-12 21:39:14 +0000977
978/* read data from a process address space */
979DECL_HANDLER(read_process_memory)
980{
981 struct process *process;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000982 size_t len = get_reply_max_size();
Alexandre Julliard8b8828f1999-11-12 21:39:14 +0000983
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000984 if (!(process = get_process_from_handle( req->handle, PROCESS_VM_READ ))) return;
985
986 if (len)
Alexandre Julliard8b8828f1999-11-12 21:39:14 +0000987 {
Alexandre Julliard0b492c72006-04-07 19:43:44 +0200988 char *buffer = mem_alloc( len );
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000989 if (buffer)
990 {
Alexandre Julliard0b492c72006-04-07 19:43:44 +0200991 if (read_process_memory( process, req->addr, len, buffer ))
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000992 set_reply_data_ptr( buffer, len );
Alexandre Julliard0b492c72006-04-07 19:43:44 +0200993 else
994 free( buffer );
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000995 }
Alexandre Julliard8b8828f1999-11-12 21:39:14 +0000996 }
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000997 release_object( process );
Alexandre Julliard8b8828f1999-11-12 21:39:14 +0000998}
Alexandre Julliardeef70251999-11-13 19:54:54 +0000999
1000/* write data to a process address space */
1001DECL_HANDLER(write_process_memory)
1002{
1003 struct process *process;
1004
1005 if ((process = get_process_from_handle( req->handle, PROCESS_VM_WRITE )))
1006 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001007 size_t len = get_req_data_size();
Alexandre Julliard959bbf82006-04-07 18:41:58 +02001008 if (len) write_process_memory( process, req->addr, len, get_req_data() );
1009 else set_error( STATUS_INVALID_PARAMETER );
Alexandre Julliardeef70251999-11-13 19:54:54 +00001010 release_object( process );
1011 }
1012}
Alexandre Julliard05f0b712000-03-09 18:18:41 +00001013
1014/* notify the server that a dll has been loaded */
1015DECL_HANDLER(load_dll)
1016{
1017 struct process_dll *dll;
1018 struct file *file = NULL;
1019
Alexandre Julliarda510a7e2005-12-12 16:46:17 +01001020 if (req->handle && !(file = get_file_obj( current->process, req->handle, FILE_READ_DATA )))
Alexandre Julliard670711e2004-04-06 23:13:47 +00001021 return;
Alexandre Julliard8081e5a2001-01-05 04:08:07 +00001022
Alexandre Julliardaeb56602002-03-22 00:21:23 +00001023 if ((dll = process_load_dll( current->process, file, req->base,
1024 get_req_data(), get_req_data_size() )))
Alexandre Julliard05f0b712000-03-09 18:18:41 +00001025 {
Alexandre Julliardaeb56602002-03-22 00:21:23 +00001026 dll->size = req->size;
Alexandre Julliard05f0b712000-03-09 18:18:41 +00001027 dll->dbg_offset = req->dbg_offset;
1028 dll->dbg_size = req->dbg_size;
1029 dll->name = req->name;
1030 /* only generate event if initialization is done */
Alexandre Julliard9d802152002-05-24 21:20:27 +00001031 if (is_process_init_done( current->process ))
Alexandre Julliard05f0b712000-03-09 18:18:41 +00001032 generate_debug_event( current, LOAD_DLL_DEBUG_EVENT, dll );
1033 }
1034 if (file) release_object( file );
1035}
1036
1037/* notify the server that a dll is being unloaded */
1038DECL_HANDLER(unload_dll)
1039{
1040 process_unload_dll( current->process, req->base );
1041}
Alexandre Julliardc5e433a2000-05-30 19:48:18 +00001042
Eric Pouech2359b572003-01-09 00:01:28 +00001043/* retrieve information about a module in a process */
1044DECL_HANDLER(get_dll_info)
1045{
1046 struct process *process;
1047
1048 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1049 {
Alexandre Julliarda9e0fb12005-03-02 10:20:09 +00001050 struct process_dll *dll = find_process_dll( process, req->base_address );
Eric Pouech2359b572003-01-09 00:01:28 +00001051
Alexandre Julliarda9e0fb12005-03-02 10:20:09 +00001052 if (dll)
Eric Pouech2359b572003-01-09 00:01:28 +00001053 {
Alexandre Julliarda9e0fb12005-03-02 10:20:09 +00001054 reply->size = dll->size;
Robert Shearmanc5165712005-05-25 18:41:09 +00001055 reply->entry_point = NULL; /* FIXME */
Alexandre Julliarda9e0fb12005-03-02 10:20:09 +00001056 if (dll->filename)
Eric Pouech2359b572003-01-09 00:01:28 +00001057 {
Alexandre Julliarda9e0fb12005-03-02 10:20:09 +00001058 size_t len = min( dll->namelen, get_reply_max_size() );
1059 set_reply_data( dll->filename, len );
Eric Pouech2359b572003-01-09 00:01:28 +00001060 }
1061 }
Alexandre Julliarda9e0fb12005-03-02 10:20:09 +00001062 else
1063 set_error( STATUS_DLL_NOT_FOUND );
1064
Eric Pouech2359b572003-01-09 00:01:28 +00001065 release_object( process );
1066 }
1067}
1068
Alexandre Julliardc5e433a2000-05-30 19:48:18 +00001069/* wait for a process to start waiting on input */
1070/* FIXME: only returns event for now, wait is done in the client */
1071DECL_HANDLER(wait_input_idle)
1072{
1073 struct process *process;
1074
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001075 reply->event = 0;
Alexandre Julliardc5e433a2000-05-30 19:48:18 +00001076 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1077 {
Alexandre Julliard27909232005-10-12 21:10:52 +00001078 if (process->idle_event && process != current->process)
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001079 reply->event = alloc_handle( current->process, process->idle_event,
1080 EVENT_ALL_ACCESS, 0 );
Alexandre Julliardc5e433a2000-05-30 19:48:18 +00001081 release_object( process );
1082 }
1083}