blob: 8ca20d83d7be1b5da7b6681cf7256c8980a054ed [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
Jonathan Ernst360a3f92006-05-18 14:49:52 +020018 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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"
Alexandre Julliardbfce1512003-12-10 04:08:06 +000049#include "user.h"
Robert Shearmand2ea92d2005-04-22 21:17:15 +000050#include "security.h"
Alexandre Julliard642d3131998-07-12 19:29:36 +000051
Alexandre Julliardf692d441999-03-21 19:23:54 +000052/* process structure */
Alexandre Julliard43c190e1999-05-15 10:48:19 +000053
Alexandre Julliard48c0d512005-02-25 19:31:26 +000054static struct list process_list = LIST_INIT(process_list);
Alexandre Julliard156b2052007-06-06 20:33:13 +020055static int running_processes, user_processes;
56static struct event *user_process_event; /* signaled when all user processes have exited */
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 */
Rob Shearmanc1707d82007-10-03 13:10:37 +010077 default_get_sd, /* get_sd */
78 default_set_sd, /* set_sd */
Vitaliy Margolenbaffcb92005-11-22 14:55:42 +000079 no_lookup_name, /* lookup_name */
Alexandre Julliard7e71c1d2007-03-22 11:44:29 +010080 no_open_file, /* open_file */
Alexandre Julliardb9b1ea92005-06-09 15:39:52 +000081 no_close_handle, /* close_handle */
Alexandre Julliard863637b2003-01-30 00:26:44 +000082 process_destroy /* destroy */
83};
84
85static const struct fd_ops process_fd_ops =
86{
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000087 NULL, /* get_poll_events */
Alexandre Julliardf5242402001-02-28 21:45:23 +000088 process_poll_event, /* poll_event */
Alexandre Julliard3f057592007-04-12 20:21:53 +020089 NULL, /* flush */
90 NULL, /* get_fd_type */
Alexandre Julliard63571432007-04-16 14:45:03 +020091 NULL, /* ioctl */
Alexandre Julliard3f057592007-04-12 20:21:53 +020092 NULL, /* queue_async */
Alexandre Julliard72bff2e2007-04-10 17:07:27 +020093 NULL, /* reselect_async */
Alexandre Julliard3f057592007-04-12 20:21:53 +020094 NULL /* cancel async */
Alexandre Julliard642d3131998-07-12 19:29:36 +000095};
96
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +000097/* process startup info */
98
99struct startup_info
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000100{
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000101 struct object obj; /* object header */
Alexandre Julliard51885742002-05-30 20:12:58 +0000102 obj_handle_t hstdin; /* handle for stdin */
103 obj_handle_t hstdout; /* handle for stdout */
104 obj_handle_t hstderr; /* handle for stderr */
Alexandre Julliardd27624b2000-05-03 18:42:40 +0000105 struct file *exe_file; /* file handle for main exe */
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000106 struct process *process; /* created process */
Alexandre Julliard0f273c12006-07-26 10:43:25 +0200107 data_size_t data_size; /* size of startup data */
Alexandre Julliard841f8982003-10-04 04:09:41 +0000108 void *data; /* data for startup info */
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000109};
110
111static void startup_info_dump( struct object *obj, int verbose );
112static int startup_info_signaled( struct object *obj, struct thread *thread );
113static void startup_info_destroy( struct object *obj );
114
115static const struct object_ops startup_info_ops =
116{
117 sizeof(struct startup_info), /* size */
118 startup_info_dump, /* dump */
119 add_queue, /* add_queue */
120 remove_queue, /* remove_queue */
121 startup_info_signaled, /* signaled */
122 no_satisfied, /* satisfied */
Mike McCormackf92fff62005-04-24 17:35:52 +0000123 no_signal, /* signal */
Alexandre Julliard1ab243b2000-12-19 02:12:45 +0000124 no_get_fd, /* get_fd */
Alexandre Julliard28beba32005-12-12 14:57:40 +0100125 no_map_access, /* map_access */
Rob Shearmanc1707d82007-10-03 13:10:37 +0100126 default_get_sd, /* get_sd */
127 default_set_sd, /* set_sd */
Vitaliy Margolenbaffcb92005-11-22 14:55:42 +0000128 no_lookup_name, /* lookup_name */
Alexandre Julliard7e71c1d2007-03-22 11:44:29 +0100129 no_open_file, /* open_file */
Alexandre Julliardb9b1ea92005-06-09 15:39:52 +0000130 no_close_handle, /* close_handle */
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000131 startup_info_destroy /* destroy */
132};
133
Alexandre Julliardf692d441999-03-21 19:23:54 +0000134
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 Julliard68e850e2006-08-14 20:19:42 +0200228/* final cleanup once we are sure a process is really dead */
229static void process_died( struct process *process )
230{
231 if (debug_level) fprintf( stderr, "%04x: *process killed*\n", process->id );
Alexandre Julliard156b2052007-06-06 20:33:13 +0200232 if (!process->is_system)
233 {
234 if (!--user_processes && user_process_event)
235 set_event( user_process_event );
236 }
Alexandre Julliard68e850e2006-08-14 20:19:42 +0200237 release_object( process );
238 if (!--running_processes) close_master_socket();
239}
240
241/* callback for process sigkill timeout */
242static void process_sigkill( void *private )
243{
244 struct process *process = private;
245
246 process->sigkill_timeout = NULL;
247 kill( process->unix_pid, SIGKILL );
248 process_died( process );
249}
250
251/* start the sigkill timer for a process upon exit */
252static void start_sigkill_timer( struct process *process )
253{
254 grab_object( process );
255 if (process->unix_pid != -1 && process->msg_fd)
Alexandre Julliardaaf477f2007-04-17 20:08:59 +0200256 process->sigkill_timeout = add_timeout_user( -TICKS_PER_SEC, process_sigkill, process );
257 else
258 process_died( process );
Alexandre Julliard68e850e2006-08-14 20:19:42 +0200259}
260
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000261/* create a new process and its main thread */
Alexandre Julliardc316f0e2006-07-19 14:00:10 +0200262/* if the function fails the fd is closed */
263struct thread *create_process( int fd, struct thread *parent_thread, int inherit_all )
Alexandre Julliard642d3131998-07-12 19:29:36 +0000264{
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000265 struct process *process;
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000266 struct thread *thread = NULL;
Alexandre Julliard8859d772001-03-01 22:13:49 +0000267 int request_pipe[2];
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000268
Alexandre Julliardc316f0e2006-07-19 14:00:10 +0200269 if (!(process = alloc_object( &process_ops )))
270 {
271 close( fd );
272 goto error;
273 }
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000274 process->parent = NULL;
Alexandre Julliard3da5f841999-05-16 16:54:54 +0000275 process->debugger = NULL;
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000276 process->handles = NULL;
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000277 process->msg_fd = NULL;
Alexandre Julliard68e850e2006-08-14 20:19:42 +0200278 process->sigkill_timeout = NULL;
279 process->unix_pid = -1;
Dave Pickles717bf7e2000-02-13 16:04:14 +0000280 process->exit_code = STILL_ACTIVE;
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000281 process->running_threads = 0;
Eric Pouechb3badc72005-09-06 10:25:11 +0000282 process->priority = PROCESS_PRIOCLASS_NORMAL;
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000283 process->affinity = 1;
Alexandre Julliard3da5f841999-05-16 16:54:54 +0000284 process->suspend = 0;
Alexandre Julliard156b2052007-06-06 20:33:13 +0200285 process->is_system = 0;
Alexandre Julliardd083a7b1999-11-29 02:10:56 +0000286 process->create_flags = 0;
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000287 process->console = NULL;
Alexandre Julliardca96de32002-05-25 21:15:03 +0000288 process->startup_state = STARTUP_IN_PROGRESS;
Alexandre Julliard9d802152002-05-24 21:20:27 +0000289 process->startup_info = NULL;
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000290 process->idle_event = NULL;
291 process->queue = NULL;
Alexandre Julliarde55d5932003-10-14 01:30:42 +0000292 process->peb = NULL;
Alexandre Julliard0a7c1f62000-01-27 02:54:17 +0000293 process->ldt_copy = NULL;
Alexandre Julliard1bf96e02005-06-08 18:44:50 +0000294 process->winstation = 0;
Alexandre Julliard78a3e632005-06-09 12:07:12 +0000295 process->desktop = 0;
Rob Shearmanec862432007-05-28 18:41:51 +0100296 process->token = NULL;
Alexandre Julliardcd1c7fc2006-12-29 16:56:11 +0100297 process->trace_data = 0;
Alexandre Julliard05026382005-03-01 10:56:18 +0000298 list_init( &process->thread_list );
Alexandre Julliardce613492003-03-18 05:04:33 +0000299 list_init( &process->locks );
Alexandre Julliardbfce1512003-12-10 04:08:06 +0000300 list_init( &process->classes );
Alexandre Julliarda9e0fb12005-03-02 10:20:09 +0000301 list_init( &process->dlls );
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000302
Alexandre Julliard753c8702006-08-10 16:42:09 +0200303 process->start_time = current_time;
Alexandre Julliardaaf477f2007-04-17 20:08:59 +0200304 process->end_time = 0;
Alexandre Julliard48c0d512005-02-25 19:31:26 +0000305 list_add_head( &process_list, &process->entry );
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000306
Alexandre Julliardc316f0e2006-07-19 14:00:10 +0200307 if (!(process->id = process->group_id = alloc_ptid( process )))
308 {
309 close( fd );
310 goto error;
311 }
Alexandre Julliardf85437c2007-04-10 22:25:07 +0200312 if (!(process->msg_fd = create_anonymous_fd( &process_fd_ops, fd, &process->obj, 0 ))) goto error;
Alexandre Julliard91befe12003-02-01 01:38:40 +0000313
Alexandre Julliardc316f0e2006-07-19 14:00:10 +0200314 /* create the handle table */
Rob Shearmanec862432007-05-28 18:41:51 +0100315 if (!parent_thread)
316 {
317 process->handles = alloc_handle_table( process, 0 );
318 process->token = token_create_admin();
319 }
Alexandre Julliardc316f0e2006-07-19 14:00:10 +0200320 else
321 {
322 struct process *parent = parent_thread->process;
323 process->parent = (struct process *)grab_object( parent );
324 process->handles = inherit_all ? copy_handle_table( process, parent )
325 : alloc_handle_table( process, 0 );
Rob Shearmanec862432007-05-28 18:41:51 +0100326 /* Note: for security reasons, starting a new process does not attempt
327 * to use the current impersonation token for the new process */
328 process->token = token_duplicate( parent->token, TRUE, 0 );
Alexandre Julliardc316f0e2006-07-19 14:00:10 +0200329 }
Rob Shearmanec862432007-05-28 18:41:51 +0100330 if (!process->handles || !process->token) goto error;
Alexandre Julliardc316f0e2006-07-19 14:00:10 +0200331
Alexandre Julliard7f748242000-12-26 00:30:30 +0000332 /* create the main thread */
Alexandre Julliard8859d772001-03-01 22:13:49 +0000333 if (pipe( request_pipe ) == -1)
334 {
335 file_set_error();
336 goto error;
337 }
Alexandre Julliard4144b5b2002-06-20 23:21:27 +0000338 if (send_client_fd( process, request_pipe[1], 0 ) == -1)
339 {
340 close( request_pipe[0] );
341 close( request_pipe[1] );
342 goto error;
343 }
Alexandre Julliard8859d772001-03-01 22:13:49 +0000344 close( request_pipe[1] );
345 if (!(thread = create_thread( request_pipe[0], process ))) goto error;
Alexandre Julliard7f748242000-12-26 00:30:30 +0000346
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000347 set_fd_events( process->msg_fd, POLLIN ); /* start listening to events */
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000348 release_object( process );
349 return thread;
Alexandre Julliardf692d441999-03-21 19:23:54 +0000350
351 error:
Alexandre Julliard4144b5b2002-06-20 23:21:27 +0000352 if (process) release_object( process );
353 /* if we failed to start our first process, close everything down */
354 if (!running_processes) close_master_socket();
Alexandre Julliardf692d441999-03-21 19:23:54 +0000355 return NULL;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000356}
357
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000358/* initialize the current process and fill in the request */
Alexandre Julliard0f273c12006-07-26 10:43:25 +0200359data_size_t init_process( struct thread *thread )
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000360{
Alexandre Julliard0424f382005-07-13 12:12:43 +0000361 struct process *process = thread->process;
Alexandre Julliardc316f0e2006-07-19 14:00:10 +0200362 struct startup_info *info = process->startup_info;
Alexandre Julliard0424f382005-07-13 12:12:43 +0000363
Alexandre Julliardcd1c7fc2006-12-29 16:56:11 +0100364 init_process_tracing( process );
Alexandre Julliard11ad6a02005-07-13 19:43:35 +0000365 if (!info) return 0;
Alexandre Julliard11ad6a02005-07-13 19:43:35 +0000366 return info->data_size;
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000367}
368
Alexandre Julliard642d3131998-07-12 19:29:36 +0000369/* destroy a process when its refcount is 0 */
Alexandre Julliard338e7571998-12-27 15:28:54 +0000370static void process_destroy( struct object *obj )
Alexandre Julliard642d3131998-07-12 19:29:36 +0000371{
372 struct process *process = (struct process *)obj;
373 assert( obj->ops == &process_ops );
374
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000375 /* we can't have a thread remaining */
Alexandre Julliard05026382005-03-01 10:56:18 +0000376 assert( list_empty( &process->thread_list ));
Alexandre Julliard9d802152002-05-24 21:20:27 +0000377
Alexandre Julliard68e850e2006-08-14 20:19:42 +0200378 assert( !process->sigkill_timeout ); /* timeout should hold a reference to the process */
379
Alexandre Julliard9d802152002-05-24 21:20:27 +0000380 set_process_startup_state( process, STARTUP_ABORTED );
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000381 if (process->console) release_object( process->console );
382 if (process->parent) release_object( process->parent );
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000383 if (process->msg_fd) release_object( process->msg_fd );
Alexandre Julliard48c0d512005-02-25 19:31:26 +0000384 list_remove( &process->entry );
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000385 if (process->idle_event) release_object( process->idle_event );
386 if (process->queue) release_object( process->queue );
Alexandre Julliard91befe12003-02-01 01:38:40 +0000387 if (process->id) free_ptid( process->id );
Mike McCormack36cd6f52003-07-24 00:07:00 +0000388 if (process->token) release_object( process->token );
Alexandre Julliard642d3131998-07-12 19:29:36 +0000389}
390
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000391/* dump a process on stdout for debugging purposes */
Alexandre Julliard338e7571998-12-27 15:28:54 +0000392static void process_dump( struct object *obj, int verbose )
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000393{
394 struct process *process = (struct process *)obj;
395 assert( obj->ops == &process_ops );
396
Alexandre Julliard48c0d512005-02-25 19:31:26 +0000397 fprintf( stderr, "Process id=%04x handles=%p\n", process->id, process->handles );
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000398}
399
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000400static int process_signaled( struct object *obj, struct thread *thread )
401{
402 struct process *process = (struct process *)obj;
Alexandre Julliarddbc033a1999-03-13 08:58:24 +0000403 return !process->running_threads;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000404}
405
Alexandre Julliard46d1b3e2005-12-12 15:03:07 +0100406static unsigned int process_map_access( struct object *obj, unsigned int access )
407{
408 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | SYNCHRONIZE;
409 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | SYNCHRONIZE;
410 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
411 if (access & GENERIC_ALL) access |= PROCESS_ALL_ACCESS;
412 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
413}
414
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000415static void process_poll_event( struct fd *fd, int event )
Alexandre Julliardf5242402001-02-28 21:45:23 +0000416{
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000417 struct process *process = get_fd_user( fd );
418 assert( process->obj.ops == &process_ops );
Alexandre Julliardf5242402001-02-28 21:45:23 +0000419
Alexandre Julliard68e850e2006-08-14 20:19:42 +0200420 if (event & (POLLERR | POLLHUP))
421 {
422 release_object( process->msg_fd );
423 process->msg_fd = NULL;
424 if (process->sigkill_timeout) /* already waiting for it to die */
425 {
426 remove_timeout_user( process->sigkill_timeout );
427 process->sigkill_timeout = NULL;
428 process_died( process );
429 }
Alexandre Julliard81977b72006-09-21 11:14:45 +0200430 else kill_process( process, 0 );
Alexandre Julliard68e850e2006-08-14 20:19:42 +0200431 }
Alexandre Julliardf5242402001-02-28 21:45:23 +0000432 else if (event & POLLIN) receive_fd( process );
433}
434
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000435static void startup_info_destroy( struct object *obj )
436{
437 struct startup_info *info = (struct startup_info *)obj;
438 assert( obj->ops == &startup_info_ops );
Michael Stefaniuc5ceccec2006-10-09 23:34:36 +0200439 free( info->data );
Alexandre Julliardd27624b2000-05-03 18:42:40 +0000440 if (info->exe_file) release_object( info->exe_file );
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000441 if (info->process) release_object( info->process );
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000442}
443
444static void startup_info_dump( struct object *obj, int verbose )
445{
446 struct startup_info *info = (struct startup_info *)obj;
447 assert( obj->ops == &startup_info_ops );
448
Alexandre Julliardc316f0e2006-07-19 14:00:10 +0200449 fprintf( stderr, "Startup info in=%p out=%p err=%p\n",
450 info->hstdin, info->hstdout, info->hstderr );
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000451}
452
453static int startup_info_signaled( struct object *obj, struct thread *thread )
454{
455 struct startup_info *info = (struct startup_info *)obj;
Alexandre Julliard49266102006-02-21 19:30:17 +0100456 return info->process && info->process->startup_state != STARTUP_IN_PROGRESS;
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000457}
458
Alexandre Julliard642d3131998-07-12 19:29:36 +0000459/* get a process from an id (and increment the refcount) */
Alexandre Julliard54f22872002-10-03 19:54:57 +0000460struct process *get_process_from_id( process_id_t id )
Alexandre Julliard642d3131998-07-12 19:29:36 +0000461{
Alexandre Julliard91befe12003-02-01 01:38:40 +0000462 struct object *obj = get_ptid_entry( id );
463
464 if (obj && obj->ops == &process_ops) return (struct process *)grab_object( obj );
465 set_error( STATUS_INVALID_PARAMETER );
466 return NULL;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000467}
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000468
469/* get a process from a handle (and increment the refcount) */
Alexandre Julliard51885742002-05-30 20:12:58 +0000470struct process *get_process_from_handle( obj_handle_t handle, unsigned int access )
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000471{
472 return (struct process *)get_handle_obj( current->process, handle,
473 access, &process_ops );
474}
475
Alexandre Julliarda9e0fb12005-03-02 10:20:09 +0000476/* find a dll from its base address */
477static inline struct process_dll *find_process_dll( struct process *process, void *base )
478{
479 struct process_dll *dll;
480
Alexandre Julliarda9e0fb12005-03-02 10:20:09 +0000481 LIST_FOR_EACH_ENTRY( dll, &process->dlls, struct process_dll, entry )
482 {
483 if (dll->base == base) return dll;
484 }
485 return NULL;
486}
487
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000488/* add a dll to a process list */
489static struct process_dll *process_load_dll( struct process *process, struct file *file,
Alexandre Julliard0f273c12006-07-26 10:43:25 +0200490 void *base, const WCHAR *filename, data_size_t name_len )
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000491{
492 struct process_dll *dll;
493
494 /* make sure we don't already have one with the same base address */
Alexandre Julliarda9e0fb12005-03-02 10:20:09 +0000495 if (find_process_dll( process, base ))
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000496 {
497 set_error( STATUS_INVALID_PARAMETER );
498 return NULL;
499 }
500
501 if ((dll = mem_alloc( sizeof(*dll) )))
502 {
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000503 dll->file = NULL;
504 dll->base = base;
Alexandre Julliardaeb56602002-03-22 00:21:23 +0000505 dll->filename = NULL;
506 dll->namelen = name_len;
507 if (name_len && !(dll->filename = memdup( filename, name_len )))
508 {
509 free( dll );
510 return NULL;
511 }
Alexandre Julliard5bd51362007-01-12 14:42:43 +0100512 if (file) dll->file = grab_file_unless_removable( file );
Alexandre Julliarde6374cb2006-02-16 12:13:01 +0100513 list_add_tail( &process->dlls, &dll->entry );
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000514 }
515 return dll;
516}
517
518/* remove a dll from a process list */
519static void process_unload_dll( struct process *process, void *base )
520{
Alexandre Julliarda9e0fb12005-03-02 10:20:09 +0000521 struct process_dll *dll = find_process_dll( process, base );
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000522
Alexandre Julliarde6374cb2006-02-16 12:13:01 +0100523 if (dll && (&dll->entry != list_head( &process->dlls ))) /* main exe can't be unloaded */
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000524 {
Alexandre Julliarda9e0fb12005-03-02 10:20:09 +0000525 if (dll->file) release_object( dll->file );
Michael Stefaniuc5ceccec2006-10-09 23:34:36 +0200526 free( dll->filename );
Alexandre Julliarda9e0fb12005-03-02 10:20:09 +0000527 list_remove( &dll->entry );
528 free( dll );
529 generate_debug_event( current, UNLOAD_DLL_DEBUG_EVENT, base );
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000530 }
Alexandre Julliarda9e0fb12005-03-02 10:20:09 +0000531 else set_error( STATUS_INVALID_PARAMETER );
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000532}
533
Alexandre Julliard81977b72006-09-21 11:14:45 +0200534/* terminate a process with the given exit code */
535static void terminate_process( struct process *process, struct thread *skip, int exit_code )
536{
Alexandre Julliard110a6fe2006-11-10 12:18:54 +0100537 struct list *ptr;
Alexandre Julliard81977b72006-09-21 11:14:45 +0200538
Alexandre Julliardd6f82842006-11-13 12:53:09 +0100539 if (skip && skip->process == process) /* move it to the end of the list */
Alexandre Julliard110a6fe2006-11-10 12:18:54 +0100540 {
541 assert( skip->state != TERMINATED );
542 list_remove( &skip->proc_entry );
543 list_add_tail( &process->thread_list, &skip->proc_entry );
544 }
545
546 grab_object( process ); /* make sure it doesn't get freed when threads die */
547 while ((ptr = list_head( &process->thread_list )))
Alexandre Julliard81977b72006-09-21 11:14:45 +0200548 {
549 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
550
551 if (exit_code) thread->exit_code = exit_code;
Alexandre Julliard110a6fe2006-11-10 12:18:54 +0100552 if (thread == skip) break;
553 kill_thread( thread, 1 );
Alexandre Julliard81977b72006-09-21 11:14:45 +0200554 }
Alexandre Julliard110a6fe2006-11-10 12:18:54 +0100555 release_object( process );
Alexandre Julliard81977b72006-09-21 11:14:45 +0200556}
557
Alexandre Julliard40043ed2002-08-16 20:02:15 +0000558/* kill all processes */
559void kill_all_processes( struct process *skip, int exit_code )
560{
561 for (;;)
562 {
Alexandre Julliard48c0d512005-02-25 19:31:26 +0000563 struct process *process;
Alexandre Julliard40043ed2002-08-16 20:02:15 +0000564
Alexandre Julliard48c0d512005-02-25 19:31:26 +0000565 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
566 {
567 if (process == skip) continue;
568 if (process->running_threads) break;
569 }
570 if (&process->entry == &process_list) break; /* no process found */
Alexandre Julliard81977b72006-09-21 11:14:45 +0200571 terminate_process( process, NULL, exit_code );
Alexandre Julliard40043ed2002-08-16 20:02:15 +0000572 }
573}
574
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000575/* kill all processes being attached to a console renderer */
Eric Pouech3940d8a2001-12-04 20:17:43 +0000576void kill_console_processes( struct thread *renderer, int exit_code )
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000577{
578 for (;;) /* restart from the beginning of the list every time */
579 {
Alexandre Julliard48c0d512005-02-25 19:31:26 +0000580 struct process *process;
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000581
582 /* find the first process being attached to 'renderer' and still running */
Alexandre Julliard48c0d512005-02-25 19:31:26 +0000583 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000584 {
Alexandre Julliard48c0d512005-02-25 19:31:26 +0000585 if (process == renderer->process) continue;
586 if (!process->running_threads) continue;
Alexandre Julliard627ca402007-05-11 12:46:32 +0200587 if (process->console && console_get_renderer( process->console ) == renderer) break;
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000588 }
Alexandre Julliard48c0d512005-02-25 19:31:26 +0000589 if (&process->entry == &process_list) break; /* no process found */
Alexandre Julliard81977b72006-09-21 11:14:45 +0200590 terminate_process( process, NULL, exit_code );
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000591 }
592}
593
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000594/* a process has been killed (i.e. its last thread died) */
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000595static void process_killed( struct process *process )
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000596{
Alexandre Julliard36b85d02005-06-29 19:29:15 +0000597 struct handle_table *handles;
Alexandre Julliarda9e0fb12005-03-02 10:20:09 +0000598 struct list *ptr;
599
Alexandre Julliard05026382005-03-01 10:56:18 +0000600 assert( list_empty( &process->thread_list ));
Alexandre Julliard753c8702006-08-10 16:42:09 +0200601 process->end_time = current_time;
Alexandre Julliardf978f612006-03-06 21:02:24 +0100602 close_process_desktop( process );
Alexandre Julliard36b85d02005-06-29 19:29:15 +0000603 handles = process->handles;
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000604 process->handles = NULL;
Alexandre Julliard36b85d02005-06-29 19:29:15 +0000605 if (handles) release_object( handles );
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000606
607 /* close the console attached to this process, if any */
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000608 free_console( process );
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000609
Alexandre Julliarda9e0fb12005-03-02 10:20:09 +0000610 while ((ptr = list_head( &process->dlls )))
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000611 {
Alexandre Julliarda9e0fb12005-03-02 10:20:09 +0000612 struct process_dll *dll = LIST_ENTRY( ptr, struct process_dll, entry );
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000613 if (dll->file) release_object( dll->file );
Michael Stefaniuc5ceccec2006-10-09 23:34:36 +0200614 free( dll->filename );
Alexandre Julliarda9e0fb12005-03-02 10:20:09 +0000615 list_remove( &dll->entry );
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000616 free( dll );
617 }
Alexandre Julliardbfce1512003-12-10 04:08:06 +0000618 destroy_process_classes( process );
Alexandre Julliard5f22e7c2005-06-14 19:23:56 +0000619 remove_process_locks( process );
Alexandre Julliard9d802152002-05-24 21:20:27 +0000620 set_process_startup_state( process, STARTUP_ABORTED );
Alexandre Julliardcd1c7fc2006-12-29 16:56:11 +0100621 finish_process_tracing( process );
Alexandre Julliard68e850e2006-08-14 20:19:42 +0200622 start_sigkill_timer( process );
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000623 wake_up( &process->obj, 0 );
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000624}
625
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000626/* add a thread to a process running threads list */
627void add_process_thread( struct process *process, struct thread *thread )
628{
Thomas Kho4ff4ba32006-07-20 22:44:36 -0500629 list_add_tail( &process->thread_list, &thread->proc_entry );
Alexandre Julliard156b2052007-06-06 20:33:13 +0200630 if (!process->running_threads++)
631 {
632 running_processes++;
633 if (!process->is_system)
634 {
635 if (!user_processes++ && user_process_event)
636 reset_event( user_process_event );
637 }
638 }
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000639 grab_object( thread );
640}
641
642/* remove a thread from a process running threads list */
643void remove_process_thread( struct process *process, struct thread *thread )
644{
645 assert( process->running_threads > 0 );
Alexandre Julliard05026382005-03-01 10:56:18 +0000646 assert( !list_empty( &process->thread_list ));
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000647
Alexandre Julliard05026382005-03-01 10:56:18 +0000648 list_remove( &thread->proc_entry );
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000649
650 if (!--process->running_threads)
651 {
652 /* we have removed the last running thread, exit the process */
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000653 process->exit_code = thread->exit_code;
654 generate_debug_event( thread, EXIT_PROCESS_DEBUG_EVENT, process );
655 process_killed( process );
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000656 }
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000657 else generate_debug_event( thread, EXIT_THREAD_DEBUG_EVENT, thread );
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000658 release_object( thread );
659}
660
Alexandre Julliard3da5f841999-05-16 16:54:54 +0000661/* suspend all the threads of a process */
662void suspend_process( struct process *process )
663{
664 if (!process->suspend++)
665 {
Alexandre Julliard05026382005-03-01 10:56:18 +0000666 struct list *ptr, *next;
667
668 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
Alexandre Julliard3da5f841999-05-16 16:54:54 +0000669 {
Alexandre Julliard05026382005-03-01 10:56:18 +0000670 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
Alexandre Julliard0a707831999-11-08 05:31:47 +0000671 if (!thread->suspend) stop_thread( thread );
Alexandre Julliard3da5f841999-05-16 16:54:54 +0000672 }
673 }
674}
675
676/* resume all the threads of a process */
677void resume_process( struct process *process )
678{
679 assert (process->suspend > 0);
680 if (!--process->suspend)
681 {
Alexandre Julliard05026382005-03-01 10:56:18 +0000682 struct list *ptr, *next;
683
684 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
Alexandre Julliard3da5f841999-05-16 16:54:54 +0000685 {
Alexandre Julliard05026382005-03-01 10:56:18 +0000686 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
Alexandre Julliardd04ccb82003-03-04 22:18:43 +0000687 if (!thread->suspend) wake_thread( thread );
Alexandre Julliard3da5f841999-05-16 16:54:54 +0000688 }
689 }
690}
691
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000692/* kill a process on the spot */
Alexandre Julliard81977b72006-09-21 11:14:45 +0200693void kill_process( struct process *process, int violent_death )
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000694{
Alexandre Julliard81977b72006-09-21 11:14:45 +0200695 if (violent_death) terminate_process( process, NULL, 1 );
696 else
Alexandre Julliard12f29b52000-03-17 15:16:57 +0000697 {
Alexandre Julliard110a6fe2006-11-10 12:18:54 +0100698 struct list *ptr;
Alexandre Julliard05026382005-03-01 10:56:18 +0000699
Alexandre Julliard110a6fe2006-11-10 12:18:54 +0100700 grab_object( process ); /* make sure it doesn't get freed when threads die */
701 while ((ptr = list_head( &process->thread_list )))
Alexandre Julliard81977b72006-09-21 11:14:45 +0200702 {
703 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
704 kill_thread( thread, 0 );
705 }
Alexandre Julliard110a6fe2006-11-10 12:18:54 +0100706 release_object( process );
Alexandre Julliard12f29b52000-03-17 15:16:57 +0000707 }
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000708}
709
Alexandre Julliard17cf8101999-11-24 01:22:14 +0000710/* kill all processes being debugged by a given thread */
711void kill_debugged_processes( struct thread *debugger, int exit_code )
712{
713 for (;;) /* restart from the beginning of the list every time */
714 {
Alexandre Julliard48c0d512005-02-25 19:31:26 +0000715 struct process *process;
716
Alexandre Julliard17cf8101999-11-24 01:22:14 +0000717 /* find the first process being debugged by 'debugger' and still running */
Alexandre Julliard48c0d512005-02-25 19:31:26 +0000718 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
719 {
720 if (!process->running_threads) continue;
721 if (process->debugger == debugger) break;
722 }
723 if (&process->entry == &process_list) break; /* no process found */
Alexandre Julliard17cf8101999-11-24 01:22:14 +0000724 process->debugger = NULL;
Alexandre Julliard81977b72006-09-21 11:14:45 +0200725 terminate_process( process, NULL, exit_code );
Alexandre Julliard17cf8101999-11-24 01:22:14 +0000726 }
727}
728
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000729
Alexandre Julliard17de8292006-04-19 19:45:39 +0200730/* trigger a breakpoint event in a given process */
731void break_process( struct process *process )
732{
733 struct thread *thread;
734
735 suspend_process( process );
736
737 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
738 {
739 if (thread->context) /* inside an exception event already */
740 {
741 break_thread( thread );
742 goto done;
743 }
744 }
745 if ((thread = get_process_first_thread( process ))) thread->debug_break = 1;
746 else set_error( STATUS_ACCESS_DENIED );
747done:
748 resume_process( process );
749}
750
751
Eric Pouechfbccb382002-02-27 01:28:30 +0000752/* detach a debugger from all its debuggees */
753void detach_debugged_processes( struct thread *debugger )
754{
755 struct process *process;
Alexandre Julliard48c0d512005-02-25 19:31:26 +0000756
757 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
Eric Pouechfbccb382002-02-27 01:28:30 +0000758 {
759 if (process->debugger == debugger && process->running_threads)
760 {
761 debugger_detach( process, debugger );
762 }
763 }
764}
765
766
Eric Pouech93bfa0d2002-06-02 21:22:22 +0000767void enum_processes( int (*cb)(struct process*, void*), void *user )
768{
Alexandre Julliard48c0d512005-02-25 19:31:26 +0000769 struct list *ptr, *next;
770
771 LIST_FOR_EACH_SAFE( ptr, next, &process_list )
Eric Pouech93bfa0d2002-06-02 21:22:22 +0000772 {
Alexandre Julliard48c0d512005-02-25 19:31:26 +0000773 struct process *process = LIST_ENTRY( ptr, struct process, entry );
Eric Pouech93bfa0d2002-06-02 21:22:22 +0000774 if ((cb)(process, user)) break;
775 }
776}
777
Alexandre Julliarde55d5932003-10-14 01:30:42 +0000778/* set the debugged flag in the process PEB */
779int set_process_debug_flag( struct process *process, int flag )
780{
Alexandre Julliard959bbf82006-04-07 18:41:58 +0200781 char data = (flag != 0);
Alexandre Julliarde55d5932003-10-14 01:30:42 +0000782
783 /* BeingDebugged flag is the byte at offset 2 in the PEB */
Alexandre Julliard959bbf82006-04-07 18:41:58 +0200784 return write_process_memory( process, (char *)process->peb + 2, 1, &data );
Alexandre Julliard8b8828f1999-11-12 21:39:14 +0000785}
786
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +0000787/* take a snapshot of currently running processes */
788struct process_snapshot *process_snap( int *count )
789{
790 struct process_snapshot *snapshot, *ptr;
791 struct process *process;
Alexandre Julliard498b7812006-08-15 20:31:54 +0200792
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +0000793 if (!running_processes) return NULL;
794 if (!(snapshot = mem_alloc( sizeof(*snapshot) * running_processes )))
795 return NULL;
796 ptr = snapshot;
Alexandre Julliard48c0d512005-02-25 19:31:26 +0000797 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +0000798 {
799 if (!process->running_threads) continue;
800 ptr->process = process;
801 ptr->threads = process->running_threads;
Alexandre Julliard07d84462000-04-16 19:45:05 +0000802 ptr->count = process->obj.refcount;
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +0000803 ptr->priority = process->priority;
Eric Pouech9fd54b22003-09-16 01:07:21 +0000804 ptr->handles = get_handle_table_count(process);
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +0000805 grab_object( process );
806 ptr++;
807 }
Alexandre Julliard498b7812006-08-15 20:31:54 +0200808
809 if (!(*count = ptr - snapshot))
810 {
811 free( snapshot );
812 snapshot = NULL;
813 }
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +0000814 return snapshot;
815}
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000816
Alexandre Julliard07d84462000-04-16 19:45:05 +0000817/* take a snapshot of the modules of a process */
818struct module_snapshot *module_snap( struct process *process, int *count )
819{
820 struct module_snapshot *snapshot, *ptr;
821 struct process_dll *dll;
Alexandre Julliarde6374cb2006-02-16 12:13:01 +0100822 int total = 0;
Alexandre Julliard07d84462000-04-16 19:45:05 +0000823
Alexandre Julliarda9e0fb12005-03-02 10:20:09 +0000824 LIST_FOR_EACH_ENTRY( dll, &process->dlls, struct process_dll, entry ) total++;
Alexandre Julliard07d84462000-04-16 19:45:05 +0000825 if (!(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
826
Alexandre Julliarde6374cb2006-02-16 12:13:01 +0100827 ptr = snapshot;
Alexandre Julliarda9e0fb12005-03-02 10:20:09 +0000828 LIST_FOR_EACH_ENTRY( dll, &process->dlls, struct process_dll, entry )
Alexandre Julliard07d84462000-04-16 19:45:05 +0000829 {
Alexandre Julliardaeb56602002-03-22 00:21:23 +0000830 ptr->base = dll->base;
831 ptr->size = dll->size;
832 ptr->namelen = dll->namelen;
833 ptr->filename = memdup( dll->filename, dll->namelen );
Alexandre Julliarda9e0fb12005-03-02 10:20:09 +0000834 ptr++;
Alexandre Julliard07d84462000-04-16 19:45:05 +0000835 }
836 *count = total;
837 return snapshot;
838}
839
840
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000841/* create a new process */
842DECL_HANDLER(new_process)
843{
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000844 struct startup_info *info;
Alexandre Julliardc316f0e2006-07-19 14:00:10 +0200845 struct thread *thread;
846 struct process *process;
847 struct process *parent = current->process;
848 int socket_fd = thread_get_inflight_fd( current, req->socket_fd );
849
850 if (socket_fd == -1)
851 {
852 set_error( STATUS_INVALID_PARAMETER );
853 return;
854 }
855 if (fcntl( socket_fd, F_SETFL, O_NONBLOCK ) == -1)
856 {
857 set_error( STATUS_INVALID_HANDLE );
858 close( socket_fd );
859 return;
860 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000861
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000862 /* build the startup info for a new process */
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000863 if (!(info = alloc_object( &startup_info_ops ))) return;
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000864 info->hstdin = req->hstdin;
865 info->hstdout = req->hstdout;
866 info->hstderr = req->hstderr;
Alexandre Julliardd27624b2000-05-03 18:42:40 +0000867 info->exe_file = NULL;
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000868 info->process = NULL;
Alexandre Julliard6543a652002-03-29 18:28:56 +0000869 info->data_size = get_req_data_size();
870 info->data = NULL;
Alexandre Julliard6a72dc52000-04-14 13:42:00 +0000871
Alexandre Julliard8081e5a2001-01-05 04:08:07 +0000872 if (req->exe_file &&
Alexandre Julliarda510a7e2005-12-12 16:46:17 +0100873 !(info->exe_file = get_file_obj( current->process, req->exe_file, FILE_READ_DATA )))
Alexandre Julliarde9936d92001-01-26 00:22:26 +0000874 goto done;
Alexandre Julliardd27624b2000-05-03 18:42:40 +0000875
Alexandre Julliard841f8982003-10-04 04:09:41 +0000876 if (!(info->data = memdup( get_req_data(), info->data_size ))) goto done;
Alexandre Julliardc316f0e2006-07-19 14:00:10 +0200877
878 if (!(thread = create_process( socket_fd, current, req->inherit_all ))) goto done;
879 process = thread->process;
880 process->create_flags = req->create_flags;
881 process->startup_info = (struct startup_info *)grab_object( info );
882
883 /* connect to the window station */
884 connect_process_winstation( process, current );
885
886 /* thread will be actually suspended in init_done */
887 if (req->create_flags & CREATE_SUSPENDED) thread->suspend++;
888
889 /* set the process console */
890 if (!(req->create_flags & (DETACHED_PROCESS | CREATE_NEW_CONSOLE)))
891 {
892 /* FIXME: some better error checking should be done...
893 * like if hConOut and hConIn are console handles, then they should be on the same
894 * physical console
895 */
896 inherit_console( current, process, req->inherit_all ? req->hstdin : 0 );
897 }
898
899 if (!req->inherit_all && !(req->create_flags & CREATE_NEW_CONSOLE))
900 {
901 info->hstdin = duplicate_handle( parent, req->hstdin, process,
902 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
903 info->hstdout = duplicate_handle( parent, req->hstdout, process,
904 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
905 info->hstderr = duplicate_handle( parent, req->hstderr, process,
906 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
907 /* some handles above may have been invalid; this is not an error */
908 if (get_error() == STATUS_INVALID_HANDLE ||
909 get_error() == STATUS_OBJECT_TYPE_MISMATCH) clear_error();
910 }
911
912 /* attach to the debugger if requested */
913 if (req->create_flags & (DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS))
914 set_process_debugger( process, current );
915 else if (parent->debugger && !(parent->create_flags & DEBUG_ONLY_THIS_PROCESS))
916 set_process_debugger( process, parent->debugger );
917
918 if (!(req->create_flags & CREATE_NEW_PROCESS_GROUP))
919 process->group_id = parent->group_id;
920
921 info->process = (struct process *)grab_object( process );
Alexandre Julliard24560e72005-12-09 13:58:25 +0100922 reply->info = alloc_handle( current->process, info, SYNCHRONIZE, 0 );
Alexandre Julliardc316f0e2006-07-19 14:00:10 +0200923 reply->pid = get_process_id( process );
924 reply->tid = get_thread_id( thread );
925 reply->phandle = alloc_handle( parent, process, req->process_access, req->process_attr );
926 reply->thandle = alloc_handle( parent, thread, req->thread_access, req->thread_attr );
Alexandre Julliarde9936d92001-01-26 00:22:26 +0000927
928 done:
929 release_object( info );
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000930}
Alexandre Julliard6a72dc52000-04-14 13:42:00 +0000931
Alexandre Julliarde9936d92001-01-26 00:22:26 +0000932/* Retrieve information about a newly started process */
933DECL_HANDLER(get_new_process_info)
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000934{
Alexandre Julliarde9936d92001-01-26 00:22:26 +0000935 struct startup_info *info;
936
Alexandre Julliarde9936d92001-01-26 00:22:26 +0000937 if ((info = (struct startup_info *)get_handle_obj( current->process, req->info,
938 0, &startup_info_ops )))
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000939 {
Alexandre Julliardca96de32002-05-25 21:15:03 +0000940 reply->success = is_process_init_done( info->process );
Alexandre Julliardc316f0e2006-07-19 14:00:10 +0200941 reply->exit_code = info->process->exit_code;
Alexandre Julliarde9936d92001-01-26 00:22:26 +0000942 release_object( info );
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000943 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000944}
945
Alexandre Julliard6543a652002-03-29 18:28:56 +0000946/* Retrieve the new process startup info */
947DECL_HANDLER(get_startup_info)
948{
Alexandre Julliard01caa5e2005-07-12 20:27:09 +0000949 struct process *process = current->process;
950 struct startup_info *info = process->startup_info;
Alexandre Julliard0f273c12006-07-26 10:43:25 +0200951 data_size_t size;
Alexandre Julliard6543a652002-03-29 18:28:56 +0000952
Alexandre Julliard01caa5e2005-07-12 20:27:09 +0000953 if (!info) return;
954
Alexandre Julliard01caa5e2005-07-12 20:27:09 +0000955 if (info->exe_file &&
956 !(reply->exe_file = alloc_handle( process, info->exe_file, GENERIC_READ, 0 ))) return;
957
Alexandre Julliardc316f0e2006-07-19 14:00:10 +0200958 reply->hstdin = info->hstdin;
959 reply->hstdout = info->hstdout;
960 reply->hstderr = info->hstderr;
Alexandre Julliard01caa5e2005-07-12 20:27:09 +0000961
962 /* we return the data directly without making a copy so this can only be called once */
963 size = info->data_size;
964 if (size > get_reply_max_size()) size = get_reply_max_size();
965 set_reply_data_ptr( info->data, size );
966 info->data = NULL;
967 info->data_size = 0;
Alexandre Julliard6543a652002-03-29 18:28:56 +0000968}
969
Alexandre Julliardec7bb231999-11-12 03:35:25 +0000970/* signal the end of the process initialization */
971DECL_HANDLER(init_process_done)
972{
Alexandre Julliard2b0033d2006-02-08 15:07:16 +0100973 struct process_dll *dll;
Alexandre Julliardec7bb231999-11-12 03:35:25 +0000974 struct process *process = current->process;
Alexandre Julliardac2e4f12001-10-25 19:52:12 +0000975
Alexandre Julliard9d802152002-05-24 21:20:27 +0000976 if (is_process_init_done(process))
Alexandre Julliardec7bb231999-11-12 03:35:25 +0000977 {
Alexandre Julliard55586522006-06-08 10:26:02 +0200978 set_error( STATUS_INVALID_PARAMETER );
Alexandre Julliard9d802152002-05-24 21:20:27 +0000979 return;
980 }
Alexandre Julliarde6374cb2006-02-16 12:13:01 +0100981 if (!(dll = find_process_dll( process, req->module )))
Alexandre Julliard2b0033d2006-02-08 15:07:16 +0100982 {
Alexandre Julliarde27358e2006-02-21 20:08:19 +0100983 set_error( STATUS_DLL_NOT_FOUND );
984 return;
Alexandre Julliard2b0033d2006-02-08 15:07:16 +0100985 }
Alexandre Julliarde6374cb2006-02-16 12:13:01 +0100986
987 /* main exe is the first in the dll list */
988 list_remove( &dll->entry );
989 list_add_head( &process->dlls, &dll->entry );
Alexandre Julliardaeb56602002-03-22 00:21:23 +0000990
Alexandre Julliard9d802152002-05-24 21:20:27 +0000991 generate_startup_debug_events( process, req->entry );
992 set_process_startup_state( process, STARTUP_DONE );
993
Vitaliy Margolenf676bc82005-12-02 15:55:48 +0100994 if (req->gui) process->idle_event = create_event( NULL, NULL, 0, 1, 0 );
Alexandre Julliardca96de32002-05-25 21:15:03 +0000995 if (current->suspend + process->suspend > 0) stop_thread( current );
Alexandre Julliarde55d5932003-10-14 01:30:42 +0000996 if (process->debugger) set_process_debug_flag( process, 1 );
Alexandre Julliardec7bb231999-11-12 03:35:25 +0000997}
998
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000999/* open a handle to a process */
1000DECL_HANDLER(open_process)
1001{
Alexandre Julliard43c190e1999-05-15 10:48:19 +00001002 struct process *process = get_process_from_id( req->pid );
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001003 reply->handle = 0;
Alexandre Julliard43c190e1999-05-15 10:48:19 +00001004 if (process)
1005 {
Alexandre Julliard24560e72005-12-09 13:58:25 +01001006 reply->handle = alloc_handle( current->process, process, req->access, req->attributes );
Alexandre Julliard43c190e1999-05-15 10:48:19 +00001007 release_object( process );
1008 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +00001009}
1010
1011/* terminate a process */
1012DECL_HANDLER(terminate_process)
1013{
1014 struct process *process;
1015
1016 if ((process = get_process_from_handle( req->handle, PROCESS_TERMINATE )))
1017 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001018 reply->self = (current->process == process);
Alexandre Julliard81977b72006-09-21 11:14:45 +02001019 terminate_process( process, current, req->exit_code );
Alexandre Julliard43c190e1999-05-15 10:48:19 +00001020 release_object( process );
1021 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +00001022}
1023
1024/* fetch information about a process */
1025DECL_HANDLER(get_process_info)
1026{
1027 struct process *process;
Alexandre Julliard43c190e1999-05-15 10:48:19 +00001028
1029 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1030 {
Alexandre Julliarde55d5932003-10-14 01:30:42 +00001031 reply->pid = get_process_id( process );
Eric Pouechb0fd2ad2004-06-14 17:02:00 +00001032 reply->ppid = process->parent ? get_process_id( process->parent ) : 0;
Alexandre Julliarde55d5932003-10-14 01:30:42 +00001033 reply->exit_code = process->exit_code;
1034 reply->priority = process->priority;
Eric Pouechb09582a2005-09-27 10:52:10 +00001035 reply->affinity = process->affinity;
Eric Pouechb0fd2ad2004-06-14 17:02:00 +00001036 reply->peb = process->peb;
Alexandre Julliardaaf477f2007-04-17 20:08:59 +02001037 reply->start_time = process->start_time;
1038 reply->end_time = process->end_time;
Alexandre Julliard43c190e1999-05-15 10:48:19 +00001039 release_object( process );
1040 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +00001041}
1042
1043/* set information about a process */
1044DECL_HANDLER(set_process_info)
1045{
1046 struct process *process;
1047
1048 if ((process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION )))
1049 {
Alexandre Julliarde55d5932003-10-14 01:30:42 +00001050 if (req->mask & SET_PROCESS_INFO_PRIORITY) process->priority = req->priority;
1051 if (req->mask & SET_PROCESS_INFO_AFFINITY)
1052 {
1053 if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER );
1054 else process->affinity = req->affinity;
1055 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +00001056 release_object( process );
1057 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +00001058}
Alexandre Julliard8b8828f1999-11-12 21:39:14 +00001059
1060/* read data from a process address space */
1061DECL_HANDLER(read_process_memory)
1062{
1063 struct process *process;
Alexandre Julliard0f273c12006-07-26 10:43:25 +02001064 data_size_t len = get_reply_max_size();
Alexandre Julliard8b8828f1999-11-12 21:39:14 +00001065
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001066 if (!(process = get_process_from_handle( req->handle, PROCESS_VM_READ ))) return;
1067
1068 if (len)
Alexandre Julliard8b8828f1999-11-12 21:39:14 +00001069 {
Alexandre Julliard0b492c72006-04-07 19:43:44 +02001070 char *buffer = mem_alloc( len );
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001071 if (buffer)
1072 {
Alexandre Julliard0b492c72006-04-07 19:43:44 +02001073 if (read_process_memory( process, req->addr, len, buffer ))
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001074 set_reply_data_ptr( buffer, len );
Alexandre Julliard0b492c72006-04-07 19:43:44 +02001075 else
1076 free( buffer );
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001077 }
Alexandre Julliard8b8828f1999-11-12 21:39:14 +00001078 }
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001079 release_object( process );
Alexandre Julliard8b8828f1999-11-12 21:39:14 +00001080}
Alexandre Julliardeef70251999-11-13 19:54:54 +00001081
1082/* write data to a process address space */
1083DECL_HANDLER(write_process_memory)
1084{
1085 struct process *process;
1086
1087 if ((process = get_process_from_handle( req->handle, PROCESS_VM_WRITE )))
1088 {
Alexandre Julliard0f273c12006-07-26 10:43:25 +02001089 data_size_t len = get_req_data_size();
Alexandre Julliard959bbf82006-04-07 18:41:58 +02001090 if (len) write_process_memory( process, req->addr, len, get_req_data() );
1091 else set_error( STATUS_INVALID_PARAMETER );
Alexandre Julliardeef70251999-11-13 19:54:54 +00001092 release_object( process );
1093 }
1094}
Alexandre Julliard05f0b712000-03-09 18:18:41 +00001095
1096/* notify the server that a dll has been loaded */
1097DECL_HANDLER(load_dll)
1098{
1099 struct process_dll *dll;
1100 struct file *file = NULL;
1101
Alexandre Julliarda510a7e2005-12-12 16:46:17 +01001102 if (req->handle && !(file = get_file_obj( current->process, req->handle, FILE_READ_DATA )))
Alexandre Julliard670711e2004-04-06 23:13:47 +00001103 return;
Alexandre Julliard8081e5a2001-01-05 04:08:07 +00001104
Alexandre Julliardaeb56602002-03-22 00:21:23 +00001105 if ((dll = process_load_dll( current->process, file, req->base,
1106 get_req_data(), get_req_data_size() )))
Alexandre Julliard05f0b712000-03-09 18:18:41 +00001107 {
Alexandre Julliardaeb56602002-03-22 00:21:23 +00001108 dll->size = req->size;
Alexandre Julliard05f0b712000-03-09 18:18:41 +00001109 dll->dbg_offset = req->dbg_offset;
1110 dll->dbg_size = req->dbg_size;
1111 dll->name = req->name;
1112 /* only generate event if initialization is done */
Alexandre Julliard9d802152002-05-24 21:20:27 +00001113 if (is_process_init_done( current->process ))
Alexandre Julliard05f0b712000-03-09 18:18:41 +00001114 generate_debug_event( current, LOAD_DLL_DEBUG_EVENT, dll );
1115 }
1116 if (file) release_object( file );
1117}
1118
1119/* notify the server that a dll is being unloaded */
1120DECL_HANDLER(unload_dll)
1121{
1122 process_unload_dll( current->process, req->base );
1123}
Alexandre Julliardc5e433a2000-05-30 19:48:18 +00001124
Eric Pouech2359b572003-01-09 00:01:28 +00001125/* retrieve information about a module in a process */
1126DECL_HANDLER(get_dll_info)
1127{
1128 struct process *process;
1129
1130 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1131 {
Alexandre Julliarda9e0fb12005-03-02 10:20:09 +00001132 struct process_dll *dll = find_process_dll( process, req->base_address );
Eric Pouech2359b572003-01-09 00:01:28 +00001133
Alexandre Julliarda9e0fb12005-03-02 10:20:09 +00001134 if (dll)
Eric Pouech2359b572003-01-09 00:01:28 +00001135 {
Alexandre Julliarda9e0fb12005-03-02 10:20:09 +00001136 reply->size = dll->size;
Robert Shearmanc5165712005-05-25 18:41:09 +00001137 reply->entry_point = NULL; /* FIXME */
Alexandre Julliarda9e0fb12005-03-02 10:20:09 +00001138 if (dll->filename)
Eric Pouech2359b572003-01-09 00:01:28 +00001139 {
Alexandre Julliard0f273c12006-07-26 10:43:25 +02001140 data_size_t len = min( dll->namelen, get_reply_max_size() );
Alexandre Julliarda9e0fb12005-03-02 10:20:09 +00001141 set_reply_data( dll->filename, len );
Eric Pouech2359b572003-01-09 00:01:28 +00001142 }
1143 }
Alexandre Julliarda9e0fb12005-03-02 10:20:09 +00001144 else
1145 set_error( STATUS_DLL_NOT_FOUND );
1146
Eric Pouech2359b572003-01-09 00:01:28 +00001147 release_object( process );
1148 }
1149}
1150
Alexandre Julliardce40ef12006-07-26 11:48:25 +02001151/* retrieve the process idle event */
1152DECL_HANDLER(get_process_idle_event)
Alexandre Julliardc5e433a2000-05-30 19:48:18 +00001153{
1154 struct process *process;
1155
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001156 reply->event = 0;
Alexandre Julliardc5e433a2000-05-30 19:48:18 +00001157 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1158 {
Alexandre Julliard27909232005-10-12 21:10:52 +00001159 if (process->idle_event && process != current->process)
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001160 reply->event = alloc_handle( current->process, process->idle_event,
1161 EVENT_ALL_ACCESS, 0 );
Alexandre Julliardc5e433a2000-05-30 19:48:18 +00001162 release_object( process );
1163 }
1164}
Alexandre Julliard156b2052007-06-06 20:33:13 +02001165
1166/* make the current process a system process */
1167DECL_HANDLER(make_process_system)
1168{
1169 struct process *process = current->process;
1170
1171 if (!user_process_event)
1172 {
1173 if (!(user_process_event = create_event( NULL, NULL, 0, 1, 0 ))) return;
1174 make_object_static( (struct object *)user_process_event );
1175 }
1176
1177 if (!(reply->event = alloc_handle( current->process, user_process_event, EVENT_ALL_ACCESS, 0 )))
1178 return;
1179
1180 if (!process->is_system)
1181 {
1182 process->is_system = 1;
1183 if (!--user_processes) set_event( user_process_event );
1184 }
1185}