blob: 333e11c2dd8d5e646b634b99b27582cae92f02eb [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 Julliard767e6f61998-08-09 12:47:43 +000028#include <stdio.h>
Alexandre Julliard642d3131998-07-12 19:29:36 +000029#include <stdlib.h>
Alexandre Julliard767e6f61998-08-09 12:47:43 +000030#include <sys/time.h>
Patrik Stridvallfb32c7b2000-01-29 21:02:19 +000031#ifdef HAVE_SYS_SOCKET_H
32# include <sys/socket.h>
33#endif
Alexandre Julliard767e6f61998-08-09 12:47:43 +000034#include <unistd.h>
35
Alexandre Julliard767e6f61998-08-09 12:47:43 +000036#include "winbase.h"
37#include "winnt.h"
Alexandre Julliard642d3131998-07-12 19:29:36 +000038
Alexandre Julliard863637b2003-01-30 00:26:44 +000039#include "file.h"
Alexandre Julliard43c190e1999-05-15 10:48:19 +000040#include "handle.h"
41#include "process.h"
42#include "thread.h"
Alexandre Julliard5bc78081999-06-22 17:26:53 +000043#include "request.h"
Eric Pouech0b83d4c2001-11-23 23:04:58 +000044#include "console.h"
Alexandre Julliard642d3131998-07-12 19:29:36 +000045
Alexandre Julliardf692d441999-03-21 19:23:54 +000046/* process structure */
Alexandre Julliard43c190e1999-05-15 10:48:19 +000047
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +000048static struct process *first_process;
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +000049static int running_processes;
Alexandre Julliard642d3131998-07-12 19:29:36 +000050
51/* process operations */
52
Alexandre Julliard338e7571998-12-27 15:28:54 +000053static void process_dump( struct object *obj, int verbose );
Alexandre Julliard85ed45e1998-08-22 19:03:56 +000054static int process_signaled( struct object *obj, struct thread *thread );
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +000055static void process_poll_event( struct fd *fd, int event );
Alexandre Julliard338e7571998-12-27 15:28:54 +000056static void process_destroy( struct object *obj );
Alexandre Julliard642d3131998-07-12 19:29:36 +000057
58static const struct object_ops process_ops =
59{
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000060 sizeof(struct process), /* size */
61 process_dump, /* dump */
62 add_queue, /* add_queue */
63 remove_queue, /* remove_queue */
64 process_signaled, /* signaled */
65 no_satisfied, /* satisfied */
Alexandre Julliard863637b2003-01-30 00:26:44 +000066 no_get_fd, /* get_fd */
Alexandre Julliard863637b2003-01-30 00:26:44 +000067 process_destroy /* destroy */
68};
69
70static const struct fd_ops process_fd_ops =
71{
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000072 NULL, /* get_poll_events */
Alexandre Julliardf5242402001-02-28 21:45:23 +000073 process_poll_event, /* poll_event */
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000074 no_flush, /* flush */
75 no_get_file_info, /* get_file_info */
Alexandre Julliard863637b2003-01-30 00:26:44 +000076 no_queue_async /* queue_async */
Alexandre Julliard642d3131998-07-12 19:29:36 +000077};
78
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +000079/* process startup info */
80
81struct startup_info
Alexandre Julliard2fe57772000-01-25 01:40:27 +000082{
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +000083 struct object obj; /* object header */
Alexandre Julliard77c8b1d2003-02-24 20:51:50 +000084 struct list entry; /* entry in list of startup infos */
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +000085 int inherit_all; /* inherit all handles from parent */
Alexandre Julliard6543a652002-03-29 18:28:56 +000086 int use_handles; /* use stdio handles */
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +000087 int create_flags; /* creation flags */
Alexandre Julliardbaf0a062003-03-11 01:48:53 +000088 int unix_pid; /* Unix pid of new process */
Alexandre Julliard51885742002-05-30 20:12:58 +000089 obj_handle_t hstdin; /* handle for stdin */
90 obj_handle_t hstdout; /* handle for stdout */
91 obj_handle_t hstderr; /* handle for stderr */
Alexandre Julliardd27624b2000-05-03 18:42:40 +000092 struct file *exe_file; /* file handle for main exe */
Alexandre Julliarde9936d92001-01-26 00:22:26 +000093 struct thread *owner; /* owner thread (the one that created the new process) */
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +000094 struct process *process; /* created process */
95 struct thread *thread; /* created thread */
Alexandre Julliard6543a652002-03-29 18:28:56 +000096 size_t data_size; /* size of startup data */
97 startup_info_t *data; /* data for startup info */
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +000098};
99
100static void startup_info_dump( struct object *obj, int verbose );
101static int startup_info_signaled( struct object *obj, struct thread *thread );
102static void startup_info_destroy( struct object *obj );
103
104static const struct object_ops startup_info_ops =
105{
106 sizeof(struct startup_info), /* size */
107 startup_info_dump, /* dump */
108 add_queue, /* add_queue */
109 remove_queue, /* remove_queue */
110 startup_info_signaled, /* signaled */
111 no_satisfied, /* satisfied */
Alexandre Julliard1ab243b2000-12-19 02:12:45 +0000112 no_get_fd, /* get_fd */
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000113 startup_info_destroy /* destroy */
114};
115
Alexandre Julliardf692d441999-03-21 19:23:54 +0000116
Alexandre Julliard77c8b1d2003-02-24 20:51:50 +0000117static struct list startup_info_list = LIST_INIT(startup_info_list);
118
Alexandre Julliard91befe12003-02-01 01:38:40 +0000119struct ptid_entry
120{
121 void *ptr; /* entry ptr */
122 unsigned int next; /* next free entry */
123};
124
125static struct ptid_entry *ptid_entries; /* array of ptid entries */
126static unsigned int used_ptid_entries; /* number of entries in use */
127static unsigned int alloc_ptid_entries; /* number of allocated entries */
128static unsigned int next_free_ptid; /* next free entry */
129static unsigned int last_free_ptid; /* last free entry */
130
131#define PTID_OFFSET 8 /* offset for first ptid value */
132
133/* allocate a new process or thread id */
134unsigned int alloc_ptid( void *ptr )
135{
136 struct ptid_entry *entry;
137 unsigned int id;
138
139 if (used_ptid_entries < alloc_ptid_entries)
140 {
141 id = used_ptid_entries + PTID_OFFSET;
142 entry = &ptid_entries[used_ptid_entries++];
143 }
144 else if (next_free_ptid)
145 {
146 id = next_free_ptid;
147 entry = &ptid_entries[id - PTID_OFFSET];
148 if (!(next_free_ptid = entry->next)) last_free_ptid = 0;
149 }
150 else /* need to grow the array */
151 {
152 unsigned int count = alloc_ptid_entries + (alloc_ptid_entries / 2);
153 if (!count) count = 64;
154 if (!(entry = realloc( ptid_entries, count * sizeof(*entry) )))
155 {
156 set_error( STATUS_NO_MEMORY );
157 return 0;
158 }
159 ptid_entries = entry;
160 alloc_ptid_entries = count;
161 id = used_ptid_entries + PTID_OFFSET;
162 entry = &ptid_entries[used_ptid_entries++];
163 }
164
165 entry->ptr = ptr;
166 return id;
167}
168
169/* free a process or thread id */
170void free_ptid( unsigned int id )
171{
172 struct ptid_entry *entry = &ptid_entries[id - PTID_OFFSET];
173
174 entry->ptr = NULL;
175 entry->next = 0;
176
177 /* append to end of free list so that we don't reuse it too early */
178 if (last_free_ptid) ptid_entries[last_free_ptid - PTID_OFFSET].next = id;
179 else next_free_ptid = id;
180
181 last_free_ptid = id;
182}
183
184/* retrieve the pointer corresponding to a process or thread id */
185void *get_ptid_entry( unsigned int id )
186{
187 if (id < PTID_OFFSET) return NULL;
188 if (id - PTID_OFFSET >= used_ptid_entries) return NULL;
189 return ptid_entries[id - PTID_OFFSET].ptr;
190}
191
Alexandre Julliard9d802152002-05-24 21:20:27 +0000192/* set the state of the process startup info */
193static void set_process_startup_state( struct process *process, enum startup_state state )
194{
Alexandre Julliardca96de32002-05-25 21:15:03 +0000195 if (process->startup_state == STARTUP_IN_PROGRESS) process->startup_state = state;
196 if (process->startup_info)
197 {
198 wake_up( &process->startup_info->obj, 0 );
199 release_object( process->startup_info );
200 process->startup_info = NULL;
201 }
Alexandre Julliard9d802152002-05-24 21:20:27 +0000202}
203
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000204/* set the console and stdio handles for a newly created process */
Eric Pouech3940d8a2001-12-04 20:17:43 +0000205static int set_process_console( struct process *process, struct thread *parent_thread,
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000206 struct startup_info *info, struct init_process_reply *reply )
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000207{
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000208 if (process->create_flags & CREATE_NEW_CONSOLE)
209 {
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000210 /* let the process init do the allocation */
211 return 1;
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000212 }
Alexandre Julliard9d802152002-05-24 21:20:27 +0000213 else if (info && !(process->create_flags & DETACHED_PROCESS))
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000214 {
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000215 /* FIXME: some better error checking should be done...
216 * like if hConOut and hConIn are console handles, then they should be on the same
217 * physical console
218 */
Eric Pouech3940d8a2001-12-04 20:17:43 +0000219 inherit_console( parent_thread, process,
Alexandre Julliard6543a652002-03-29 18:28:56 +0000220 (info->inherit_all || info->use_handles) ? info->hstdin : 0 );
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000221 }
Alexandre Julliard9d802152002-05-24 21:20:27 +0000222 if (info)
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000223 {
Eric Pouech412d37f2003-06-21 02:07:10 +0000224 reply->hstdin = info->hstdin;
225 reply->hstdout = info->hstdout;
226 reply->hstderr = info->hstderr;
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000227 }
Eric Pouech412d37f2003-06-21 02:07:10 +0000228 else reply->hstdin = reply->hstdout = reply->hstderr = 0;
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000229 /* some handles above may have been invalid; this is not an error */
230 if (get_error() == STATUS_INVALID_HANDLE) clear_error();
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000231 return 1;
232}
233
234/* create a new process and its main thread */
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000235struct thread *create_process( int fd )
Alexandre Julliard642d3131998-07-12 19:29:36 +0000236{
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000237 struct process *process;
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000238 struct thread *thread = NULL;
Alexandre Julliard8859d772001-03-01 22:13:49 +0000239 int request_pipe[2];
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000240
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000241 if (!(process = alloc_object( &process_ops ))) goto error;
Alexandre Julliardf692d441999-03-21 19:23:54 +0000242 process->next = NULL;
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000243 process->prev = NULL;
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000244 process->parent = NULL;
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000245 process->thread_list = NULL;
Alexandre Julliard3da5f841999-05-16 16:54:54 +0000246 process->debugger = NULL;
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000247 process->handles = NULL;
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000248 process->msg_fd = NULL;
Dave Pickles717bf7e2000-02-13 16:04:14 +0000249 process->exit_code = STILL_ACTIVE;
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000250 process->running_threads = 0;
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000251 process->priority = NORMAL_PRIORITY_CLASS;
252 process->affinity = 1;
Alexandre Julliard3da5f841999-05-16 16:54:54 +0000253 process->suspend = 0;
Alexandre Julliardd083a7b1999-11-29 02:10:56 +0000254 process->create_flags = 0;
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000255 process->console = NULL;
Alexandre Julliardca96de32002-05-25 21:15:03 +0000256 process->startup_state = STARTUP_IN_PROGRESS;
Alexandre Julliard9d802152002-05-24 21:20:27 +0000257 process->startup_info = NULL;
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000258 process->idle_event = NULL;
259 process->queue = NULL;
Turchanov Sergei43a27e32000-05-30 20:32:06 +0000260 process->atom_table = NULL;
Alexandre Julliard0a7c1f62000-01-27 02:54:17 +0000261 process->ldt_copy = NULL;
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000262 process->exe.next = NULL;
263 process->exe.prev = NULL;
264 process->exe.file = NULL;
265 process->exe.dbg_offset = 0;
266 process->exe.dbg_size = 0;
Alexandre Julliardaeb56602002-03-22 00:21:23 +0000267 process->exe.namelen = 0;
268 process->exe.filename = NULL;
Alexandre Julliard54f22872002-10-03 19:54:57 +0000269 process->group_id = 0;
Mike McCormack36cd6f52003-07-24 00:07:00 +0000270 process->token = create_token();
Alexandre Julliardce613492003-03-18 05:04:33 +0000271 list_init( &process->locks );
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000272
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000273 gettimeofday( &process->start_time, NULL );
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000274 if ((process->next = first_process) != NULL) process->next->prev = process;
275 first_process = process;
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000276
Alexandre Julliard91befe12003-02-01 01:38:40 +0000277 if (!(process->id = alloc_ptid( process ))) goto error;
Alexandre Julliard580da242003-03-12 22:38:14 +0000278 if (!(process->msg_fd = create_anonymous_fd( &process_fd_ops, fd, &process->obj ))) goto error;
Alexandre Julliard91befe12003-02-01 01:38:40 +0000279
Alexandre Julliard7f748242000-12-26 00:30:30 +0000280 /* create the main thread */
Alexandre Julliard8859d772001-03-01 22:13:49 +0000281 if (pipe( request_pipe ) == -1)
282 {
283 file_set_error();
284 goto error;
285 }
Alexandre Julliard4144b5b2002-06-20 23:21:27 +0000286 if (send_client_fd( process, request_pipe[1], 0 ) == -1)
287 {
288 close( request_pipe[0] );
289 close( request_pipe[1] );
290 goto error;
291 }
Alexandre Julliard8859d772001-03-01 22:13:49 +0000292 close( request_pipe[1] );
293 if (!(thread = create_thread( request_pipe[0], process ))) goto error;
Alexandre Julliard7f748242000-12-26 00:30:30 +0000294
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000295 set_fd_events( process->msg_fd, POLLIN ); /* start listening to events */
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000296 release_object( process );
297 return thread;
Alexandre Julliardf692d441999-03-21 19:23:54 +0000298
299 error:
Alexandre Julliard4144b5b2002-06-20 23:21:27 +0000300 if (process) release_object( process );
301 /* if we failed to start our first process, close everything down */
302 if (!running_processes) close_master_socket();
Alexandre Julliardf692d441999-03-21 19:23:54 +0000303 return NULL;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000304}
305
Alexandre Julliard77c8b1d2003-02-24 20:51:50 +0000306/* find the startup info for a given Unix process */
Alexandre Julliardbaf0a062003-03-11 01:48:53 +0000307inline static struct startup_info *find_startup_info( int unix_pid )
Alexandre Julliard77c8b1d2003-02-24 20:51:50 +0000308{
309 struct list *ptr;
310
311 LIST_FOR_EACH( ptr, &startup_info_list )
312 {
313 struct startup_info *info = LIST_ENTRY( ptr, struct startup_info, entry );
314 if (info->unix_pid == unix_pid) return info;
315 }
316 return NULL;
317}
318
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000319/* initialize the current process and fill in the request */
Alexandre Julliard77c8b1d2003-02-24 20:51:50 +0000320static struct startup_info *init_process( struct init_process_reply *reply )
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000321{
322 struct process *process = current->process;
Alexandre Julliard77c8b1d2003-02-24 20:51:50 +0000323 struct thread *parent_thread = NULL;
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000324 struct process *parent = NULL;
Alexandre Julliard77c8b1d2003-02-24 20:51:50 +0000325 struct startup_info *info = find_startup_info( current->unix_pid );
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000326
Alexandre Julliard77c8b1d2003-02-24 20:51:50 +0000327 if (info)
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000328 {
Alexandre Julliard77c8b1d2003-02-24 20:51:50 +0000329 if (info->thread)
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000330 {
331 fatal_protocol_error( current, "init_process: called twice?\n" );
Alexandre Julliard6543a652002-03-29 18:28:56 +0000332 return NULL;
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000333 }
Alexandre Julliard77c8b1d2003-02-24 20:51:50 +0000334 parent_thread = info->owner;
335 parent = parent_thread->process;
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000336 process->parent = (struct process *)grab_object( parent );
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000337 }
338
339 /* set the process flags */
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000340 process->create_flags = info ? info->create_flags : 0;
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000341
342 /* create the handle table */
Alexandre Julliard9d802152002-05-24 21:20:27 +0000343 if (info && info->inherit_all)
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000344 process->handles = copy_handle_table( process, parent );
345 else
346 process->handles = alloc_handle_table( process, 0 );
Alexandre Julliard6543a652002-03-29 18:28:56 +0000347 if (!process->handles) return NULL;
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000348
349 /* retrieve the main exe file */
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000350 reply->exe_file = 0;
Alexandre Julliard9d802152002-05-24 21:20:27 +0000351 if (info && info->exe_file)
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000352 {
Alexandre Julliardd27624b2000-05-03 18:42:40 +0000353 process->exe.file = (struct file *)grab_object( info->exe_file );
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000354 if (!(reply->exe_file = alloc_handle( process, process->exe.file, GENERIC_READ, 0 )))
Alexandre Julliard6543a652002-03-29 18:28:56 +0000355 return NULL;
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000356 }
357
358 /* set the process console */
Alexandre Julliard6543a652002-03-29 18:28:56 +0000359 if (!set_process_console( process, parent_thread, info, reply )) return NULL;
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000360
Alexandre Julliard54f22872002-10-03 19:54:57 +0000361 process->group_id = get_process_id( process );
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000362 if (parent)
363 {
364 /* attach to the debugger if requested */
365 if (process->create_flags & (DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS))
366 set_process_debugger( process, parent_thread );
Alexandre Julliard9d802152002-05-24 21:20:27 +0000367 else if (parent->debugger && !(parent->create_flags & DEBUG_ONLY_THIS_PROCESS))
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000368 set_process_debugger( process, parent->debugger );
Eric Pouech93bfa0d2002-06-02 21:22:22 +0000369 if (!(process->create_flags & CREATE_NEW_PROCESS_GROUP))
370 process->group_id = parent->group_id;
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000371 }
372
373 /* thread will be actually suspended in init_done */
374 if (process->create_flags & CREATE_SUSPENDED) current->suspend++;
375
376 if (info)
377 {
Alexandre Julliard6543a652002-03-29 18:28:56 +0000378 reply->info_size = info->data_size;
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000379 info->process = (struct process *)grab_object( process );
380 info->thread = (struct thread *)grab_object( current );
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000381 }
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000382 reply->create_flags = process->create_flags;
383 reply->server_start = server_start_ticks;
Alexandre Julliard6543a652002-03-29 18:28:56 +0000384 return info ? (struct startup_info *)grab_object( info ) : NULL;
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000385}
386
Alexandre Julliard642d3131998-07-12 19:29:36 +0000387/* destroy a process when its refcount is 0 */
Alexandre Julliard338e7571998-12-27 15:28:54 +0000388static void process_destroy( struct object *obj )
Alexandre Julliard642d3131998-07-12 19:29:36 +0000389{
390 struct process *process = (struct process *)obj;
391 assert( obj->ops == &process_ops );
392
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000393 /* we can't have a thread remaining */
394 assert( !process->thread_list );
Alexandre Julliard9d802152002-05-24 21:20:27 +0000395
396 set_process_startup_state( process, STARTUP_ABORTED );
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000397 if (process->console) release_object( process->console );
398 if (process->parent) release_object( process->parent );
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000399 if (process->msg_fd) release_object( process->msg_fd );
Alexandre Julliard642d3131998-07-12 19:29:36 +0000400 if (process->next) process->next->prev = process->prev;
401 if (process->prev) process->prev->next = process->next;
402 else first_process = process->next;
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000403 if (process->idle_event) release_object( process->idle_event );
404 if (process->queue) release_object( process->queue );
Turchanov Sergei43a27e32000-05-30 20:32:06 +0000405 if (process->atom_table) release_object( process->atom_table );
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000406 if (process->exe.file) release_object( process->exe.file );
Alexandre Julliardaeb56602002-03-22 00:21:23 +0000407 if (process->exe.filename) free( process->exe.filename );
Alexandre Julliard91befe12003-02-01 01:38:40 +0000408 if (process->id) free_ptid( process->id );
Mike McCormack36cd6f52003-07-24 00:07:00 +0000409 if (process->token) release_object( process->token );
Alexandre Julliard642d3131998-07-12 19:29:36 +0000410}
411
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000412/* dump a process on stdout for debugging purposes */
Alexandre Julliard338e7571998-12-27 15:28:54 +0000413static void process_dump( struct object *obj, int verbose )
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000414{
415 struct process *process = (struct process *)obj;
416 assert( obj->ops == &process_ops );
417
Alexandre Julliard91befe12003-02-01 01:38:40 +0000418 fprintf( stderr, "Process id=%04x next=%p prev=%p handles=%p\n",
419 process->id, process->next, process->prev, process->handles );
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000420}
421
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000422static int process_signaled( struct object *obj, struct thread *thread )
423{
424 struct process *process = (struct process *)obj;
Alexandre Julliarddbc033a1999-03-13 08:58:24 +0000425 return !process->running_threads;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000426}
427
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000428static void process_poll_event( struct fd *fd, int event )
Alexandre Julliardf5242402001-02-28 21:45:23 +0000429{
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000430 struct process *process = get_fd_user( fd );
431 assert( process->obj.ops == &process_ops );
Alexandre Julliardf5242402001-02-28 21:45:23 +0000432
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000433 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
Alexandre Julliardf5242402001-02-28 21:45:23 +0000434 else if (event & POLLIN) receive_fd( process );
435}
436
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000437static void startup_info_destroy( struct object *obj )
438{
439 struct startup_info *info = (struct startup_info *)obj;
440 assert( obj->ops == &startup_info_ops );
Alexandre Julliard77c8b1d2003-02-24 20:51:50 +0000441 list_remove( &info->entry );
Alexandre Julliard6543a652002-03-29 18:28:56 +0000442 if (info->data) free( info->data );
Alexandre Julliardd27624b2000-05-03 18:42:40 +0000443 if (info->exe_file) release_object( info->exe_file );
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000444 if (info->process) release_object( info->process );
445 if (info->thread) release_object( info->thread );
Alexandre Julliard77c8b1d2003-02-24 20:51:50 +0000446 if (info->owner) release_object( info->owner );
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000447}
448
449static void startup_info_dump( struct object *obj, int verbose )
450{
451 struct startup_info *info = (struct startup_info *)obj;
452 assert( obj->ops == &startup_info_ops );
453
Alexandre Julliardb3332d72002-10-19 01:00:59 +0000454 fprintf( stderr, "Startup info flags=%x in=%p out=%p err=%p\n",
Alexandre Julliardca96de32002-05-25 21:15:03 +0000455 info->create_flags, info->hstdin, info->hstdout, info->hstderr );
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000456}
457
458static int startup_info_signaled( struct object *obj, struct thread *thread )
459{
460 struct startup_info *info = (struct startup_info *)obj;
Alexandre Julliardca96de32002-05-25 21:15:03 +0000461 return info->process && is_process_init_done(info->process);
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000462}
463
Alexandre Julliard642d3131998-07-12 19:29:36 +0000464/* get a process from an id (and increment the refcount) */
Alexandre Julliard54f22872002-10-03 19:54:57 +0000465struct process *get_process_from_id( process_id_t id )
Alexandre Julliard642d3131998-07-12 19:29:36 +0000466{
Alexandre Julliard91befe12003-02-01 01:38:40 +0000467 struct object *obj = get_ptid_entry( id );
468
469 if (obj && obj->ops == &process_ops) return (struct process *)grab_object( obj );
470 set_error( STATUS_INVALID_PARAMETER );
471 return NULL;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000472}
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000473
474/* get a process from a handle (and increment the refcount) */
Alexandre Julliard51885742002-05-30 20:12:58 +0000475struct process *get_process_from_handle( obj_handle_t handle, unsigned int access )
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000476{
477 return (struct process *)get_handle_obj( current->process, handle,
478 access, &process_ops );
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 Julliardaeb56602002-03-22 00:21:23 +0000483 void *base, const char *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 */
488 for (dll = process->exe.next; dll; dll = dll->next) if (dll->base == base)
489 {
490 set_error( STATUS_INVALID_PARAMETER );
491 return NULL;
492 }
493
494 if ((dll = mem_alloc( sizeof(*dll) )))
495 {
496 dll->prev = &process->exe;
497 dll->file = NULL;
498 dll->base = base;
Alexandre Julliardaeb56602002-03-22 00:21:23 +0000499 dll->filename = NULL;
500 dll->namelen = name_len;
501 if (name_len && !(dll->filename = memdup( filename, name_len )))
502 {
503 free( dll );
504 return NULL;
505 }
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000506 if (file) dll->file = (struct file *)grab_object( file );
507 if ((dll->next = process->exe.next)) dll->next->prev = dll;
508 process->exe.next = dll;
509 }
510 return dll;
511}
512
513/* remove a dll from a process list */
514static void process_unload_dll( struct process *process, void *base )
515{
516 struct process_dll *dll;
517
518 for (dll = process->exe.next; dll; dll = dll->next)
519 {
520 if (dll->base == base)
521 {
522 if (dll->file) release_object( dll->file );
523 if (dll->next) dll->next->prev = dll->prev;
524 if (dll->prev) dll->prev->next = dll->next;
Alexandre Julliardaeb56602002-03-22 00:21:23 +0000525 if (dll->filename) free( dll->filename );
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000526 free( dll );
527 generate_debug_event( current, UNLOAD_DLL_DEBUG_EVENT, base );
528 return;
529 }
530 }
531 set_error( STATUS_INVALID_PARAMETER );
532}
533
Alexandre Julliard40043ed2002-08-16 20:02:15 +0000534/* kill all processes */
535void kill_all_processes( struct process *skip, int exit_code )
536{
537 for (;;)
538 {
539 struct process *process = first_process;
540
541 while (process && (!process->running_threads || process == skip))
542 process = process->next;
543 if (!process) break;
544 kill_process( process, NULL, exit_code );
545 }
546}
547
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000548/* kill all processes being attached to a console renderer */
Eric Pouech3940d8a2001-12-04 20:17:43 +0000549void kill_console_processes( struct thread *renderer, int exit_code )
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000550{
551 for (;;) /* restart from the beginning of the list every time */
552 {
553 struct process *process = first_process;
554
555 /* find the first process being attached to 'renderer' and still running */
556 while (process &&
Eric Pouech3940d8a2001-12-04 20:17:43 +0000557 (process == renderer->process || !process->console ||
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000558 process->console->renderer != renderer || !process->running_threads))
559 {
560 process = process->next;
561 }
562 if (!process) break;
563 kill_process( process, NULL, exit_code );
564 }
565}
566
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000567/* a process has been killed (i.e. its last thread died) */
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000568static void process_killed( struct process *process )
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000569{
570 assert( !process->thread_list );
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000571 gettimeofday( &process->end_time, NULL );
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000572 if (process->handles) release_object( process->handles );
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000573 process->handles = NULL;
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000574
575 /* close the console attached to this process, if any */
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000576 free_console( process );
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000577
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000578 while (process->exe.next)
579 {
580 struct process_dll *dll = process->exe.next;
581 process->exe.next = dll->next;
582 if (dll->file) release_object( dll->file );
Alexandre Julliardaeb56602002-03-22 00:21:23 +0000583 if (dll->filename) free( dll->filename );
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000584 free( dll );
585 }
Alexandre Julliard9d802152002-05-24 21:20:27 +0000586 set_process_startup_state( process, STARTUP_ABORTED );
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000587 if (process->exe.file) release_object( process->exe.file );
588 process->exe.file = NULL;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000589 wake_up( &process->obj, 0 );
Alexandre Julliard4144b5b2002-06-20 23:21:27 +0000590 if (!--running_processes) close_master_socket();
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000591}
592
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000593/* add a thread to a process running threads list */
594void add_process_thread( struct process *process, struct thread *thread )
595{
596 thread->proc_next = process->thread_list;
597 thread->proc_prev = NULL;
598 if (thread->proc_next) thread->proc_next->proc_prev = thread;
599 process->thread_list = thread;
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +0000600 if (!process->running_threads++) running_processes++;
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000601 grab_object( thread );
602}
603
604/* remove a thread from a process running threads list */
605void remove_process_thread( struct process *process, struct thread *thread )
606{
607 assert( process->running_threads > 0 );
608 assert( process->thread_list );
609
610 if (thread->proc_next) thread->proc_next->proc_prev = thread->proc_prev;
611 if (thread->proc_prev) thread->proc_prev->proc_next = thread->proc_next;
612 else process->thread_list = thread->proc_next;
613
614 if (!--process->running_threads)
615 {
616 /* we have removed the last running thread, exit the process */
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000617 process->exit_code = thread->exit_code;
618 generate_debug_event( thread, EXIT_PROCESS_DEBUG_EVENT, process );
619 process_killed( process );
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000620 }
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000621 else generate_debug_event( thread, EXIT_THREAD_DEBUG_EVENT, thread );
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000622 release_object( thread );
623}
624
Alexandre Julliard3da5f841999-05-16 16:54:54 +0000625/* suspend all the threads of a process */
626void suspend_process( struct process *process )
627{
628 if (!process->suspend++)
629 {
630 struct thread *thread = process->thread_list;
Alexandre Julliardad16fc92002-04-06 00:03:33 +0000631 while (thread)
Alexandre Julliard3da5f841999-05-16 16:54:54 +0000632 {
Alexandre Julliardad16fc92002-04-06 00:03:33 +0000633 struct thread *next = thread->proc_next;
Alexandre Julliard0a707831999-11-08 05:31:47 +0000634 if (!thread->suspend) stop_thread( thread );
Alexandre Julliardad16fc92002-04-06 00:03:33 +0000635 thread = next;
Alexandre Julliard3da5f841999-05-16 16:54:54 +0000636 }
637 }
638}
639
640/* resume all the threads of a process */
641void resume_process( struct process *process )
642{
643 assert (process->suspend > 0);
644 if (!--process->suspend)
645 {
646 struct thread *thread = process->thread_list;
Alexandre Julliardad16fc92002-04-06 00:03:33 +0000647 while (thread)
Alexandre Julliard3da5f841999-05-16 16:54:54 +0000648 {
Alexandre Julliardad16fc92002-04-06 00:03:33 +0000649 struct thread *next = thread->proc_next;
Alexandre Julliardd04ccb82003-03-04 22:18:43 +0000650 if (!thread->suspend) wake_thread( thread );
Alexandre Julliardad16fc92002-04-06 00:03:33 +0000651 thread = next;
Alexandre Julliard3da5f841999-05-16 16:54:54 +0000652 }
653 }
654}
655
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000656/* kill a process on the spot */
Alexandre Julliardf5242402001-02-28 21:45:23 +0000657void kill_process( struct process *process, struct thread *skip, int exit_code )
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000658{
Alexandre Julliard12f29b52000-03-17 15:16:57 +0000659 struct thread *thread = process->thread_list;
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000660
Alexandre Julliard12f29b52000-03-17 15:16:57 +0000661 while (thread)
662 {
663 struct thread *next = thread->proc_next;
664 thread->exit_code = exit_code;
665 if (thread != skip) kill_thread( thread, 1 );
666 thread = next;
667 }
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000668}
669
Alexandre Julliard17cf8101999-11-24 01:22:14 +0000670/* kill all processes being debugged by a given thread */
671void kill_debugged_processes( struct thread *debugger, int exit_code )
672{
673 for (;;) /* restart from the beginning of the list every time */
674 {
675 struct process *process = first_process;
676 /* find the first process being debugged by 'debugger' and still running */
677 while (process && (process->debugger != debugger || !process->running_threads))
678 process = process->next;
679 if (!process) return;
680 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
Eric Pouechfbccb382002-02-27 01:28:30 +0000686/* detach a debugger from all its debuggees */
687void detach_debugged_processes( struct thread *debugger )
688{
689 struct process *process;
690 for (process = first_process; process; process = process->next)
691 {
692 if (process->debugger == debugger && process->running_threads)
693 {
694 debugger_detach( process, debugger );
695 }
696 }
697}
698
699
Eric Pouech93bfa0d2002-06-02 21:22:22 +0000700void enum_processes( int (*cb)(struct process*, void*), void *user )
701{
702 struct process *process;
703 for (process = first_process; process; process = process->next)
704 {
705 if ((cb)(process, user)) break;
706 }
707}
708
709
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000710/* get all information about a process */
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000711static void get_process_info( struct process *process, struct get_process_info_reply *reply )
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000712{
Eric Pouechfbccb382002-02-27 01:28:30 +0000713 reply->pid = get_process_id( process );
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000714 reply->debugged = (process->debugger != 0);
715 reply->exit_code = process->exit_code;
716 reply->priority = process->priority;
717 reply->process_affinity = process->affinity;
718 reply->system_affinity = 1;
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000719}
720
721/* set all information about a process */
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000722static void set_process_info( struct process *process,
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000723 const struct set_process_info_request *req )
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000724{
725 if (req->mask & SET_PROCESS_INFO_PRIORITY)
726 process->priority = req->priority;
727 if (req->mask & SET_PROCESS_INFO_AFFINITY)
728 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000729 if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER );
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000730 else process->affinity = req->affinity;
731 }
732}
733
Alexandre Julliard8b8828f1999-11-12 21:39:14 +0000734/* read data from a process memory space */
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000735/* len is the total size (in ints) */
736static int read_process_memory( struct process *process, const int *addr, size_t len, int *dest )
Alexandre Julliard8b8828f1999-11-12 21:39:14 +0000737{
738 struct thread *thread = process->thread_list;
Alexandre Julliard8b8828f1999-11-12 21:39:14 +0000739
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000740 assert( !((unsigned int)addr % sizeof(int)) ); /* address must be aligned */
741
Alexandre Julliard00641d52000-03-08 16:41:37 +0000742 if (!thread) /* process is dead */
743 {
744 set_error( STATUS_ACCESS_DENIED );
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000745 return 0;
Alexandre Julliard00641d52000-03-08 16:41:37 +0000746 }
Alexandre Julliard98aacc72000-03-15 19:46:14 +0000747 if (suspend_for_ptrace( thread ))
Alexandre Julliard8b8828f1999-11-12 21:39:14 +0000748 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000749 while (len > 0)
Alexandre Julliard8b8828f1999-11-12 21:39:14 +0000750 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000751 if (read_thread_int( thread, addr++, dest++ ) == -1) break;
Alexandre Julliardeef70251999-11-13 19:54:54 +0000752 len--;
753 }
Alexandre Julliardd04ccb82003-03-04 22:18:43 +0000754 resume_after_ptrace( thread );
Alexandre Julliard8b8828f1999-11-12 21:39:14 +0000755 }
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000756 return !len;
757}
758
759/* make sure we can write to the whole address range */
760/* len is the total size (in ints) */
761static int check_process_write_access( struct thread *thread, int *addr, size_t len )
762{
763 int page = get_page_size() / sizeof(int);
764
765 for (;;)
766 {
767 if (write_thread_int( thread, addr, 0, 0 ) == -1) return 0;
768 if (len <= page) break;
769 addr += page;
770 len -= page;
771 }
772 return (write_thread_int( thread, addr + len - 1, 0, 0 ) != -1);
Alexandre Julliardeef70251999-11-13 19:54:54 +0000773}
774
775/* write data to a process memory space */
776/* len is the total size (in ints), max is the size we can actually read from the input buffer */
777/* we check the total size for write permissions */
778static void write_process_memory( struct process *process, int *addr, size_t len,
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000779 unsigned int first_mask, unsigned int last_mask, const int *src )
Alexandre Julliardeef70251999-11-13 19:54:54 +0000780{
781 struct thread *thread = process->thread_list;
Alexandre Julliardeef70251999-11-13 19:54:54 +0000782
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000783 assert( !((unsigned int)addr % sizeof(int) )); /* address must be aligned */
784
Alexandre Julliard00641d52000-03-08 16:41:37 +0000785 if (!thread) /* process is dead */
786 {
787 set_error( STATUS_ACCESS_DENIED );
788 return;
789 }
Alexandre Julliard98aacc72000-03-15 19:46:14 +0000790 if (suspend_for_ptrace( thread ))
Alexandre Julliardeef70251999-11-13 19:54:54 +0000791 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000792 if (!check_process_write_access( thread, addr, len ))
793 {
794 set_error( STATUS_ACCESS_DENIED );
Peter Hunnisetta3c5ad42003-02-28 21:50:47 +0000795 goto done;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000796 }
Alexandre Julliardeef70251999-11-13 19:54:54 +0000797 /* first word is special */
798 if (len > 1)
799 {
Alexandre Julliard578c1001999-11-14 21:23:21 +0000800 if (write_thread_int( thread, addr++, *src++, first_mask ) == -1) goto done;
Alexandre Julliardeef70251999-11-13 19:54:54 +0000801 len--;
Alexandre Julliardeef70251999-11-13 19:54:54 +0000802 }
803 else last_mask &= first_mask;
804
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000805 while (len > 1)
Alexandre Julliardeef70251999-11-13 19:54:54 +0000806 {
Alexandre Julliard578c1001999-11-14 21:23:21 +0000807 if (write_thread_int( thread, addr++, *src++, ~0 ) == -1) goto done;
Alexandre Julliardeef70251999-11-13 19:54:54 +0000808 len--;
809 }
810
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000811 /* last word is special too */
812 if (write_thread_int( thread, addr, *src, last_mask ) == -1) goto done;
813
Alexandre Julliard98aacc72000-03-15 19:46:14 +0000814 done:
Alexandre Julliardd04ccb82003-03-04 22:18:43 +0000815 resume_after_ptrace( thread );
Alexandre Julliardeef70251999-11-13 19:54:54 +0000816 }
Alexandre Julliard8b8828f1999-11-12 21:39:14 +0000817}
818
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +0000819/* take a snapshot of currently running processes */
820struct process_snapshot *process_snap( int *count )
821{
822 struct process_snapshot *snapshot, *ptr;
823 struct process *process;
824 if (!running_processes) return NULL;
825 if (!(snapshot = mem_alloc( sizeof(*snapshot) * running_processes )))
826 return NULL;
827 ptr = snapshot;
828 for (process = first_process; process; process = process->next)
829 {
830 if (!process->running_threads) continue;
831 ptr->process = process;
832 ptr->threads = process->running_threads;
Alexandre Julliard07d84462000-04-16 19:45:05 +0000833 ptr->count = process->obj.refcount;
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +0000834 ptr->priority = process->priority;
835 grab_object( process );
836 ptr++;
837 }
838 *count = running_processes;
839 return snapshot;
840}
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000841
Alexandre Julliard07d84462000-04-16 19:45:05 +0000842/* take a snapshot of the modules of a process */
843struct module_snapshot *module_snap( struct process *process, int *count )
844{
845 struct module_snapshot *snapshot, *ptr;
846 struct process_dll *dll;
847 int total = 0;
848
849 for (dll = &process->exe; dll; dll = dll->next) total++;
850 if (!(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
851
852 for (ptr = snapshot, dll = &process->exe; dll; dll = dll->next, ptr++)
853 {
Alexandre Julliardaeb56602002-03-22 00:21:23 +0000854 ptr->base = dll->base;
855 ptr->size = dll->size;
856 ptr->namelen = dll->namelen;
857 ptr->filename = memdup( dll->filename, dll->namelen );
Alexandre Julliard07d84462000-04-16 19:45:05 +0000858 }
859 *count = total;
860 return snapshot;
861}
862
863
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000864/* create a new process */
865DECL_HANDLER(new_process)
866{
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000867 struct startup_info *info;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000868
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000869 /* build the startup info for a new process */
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000870 if (!(info = alloc_object( &startup_info_ops ))) return;
Alexandre Julliard77c8b1d2003-02-24 20:51:50 +0000871 list_add_head( &startup_info_list, &info->entry );
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000872 info->inherit_all = req->inherit_all;
Alexandre Julliard6543a652002-03-29 18:28:56 +0000873 info->use_handles = req->use_handles;
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000874 info->create_flags = req->create_flags;
Alexandre Julliard77c8b1d2003-02-24 20:51:50 +0000875 info->unix_pid = req->unix_pid;
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000876 info->hstdin = req->hstdin;
877 info->hstdout = req->hstdout;
878 info->hstderr = req->hstderr;
Alexandre Julliardd27624b2000-05-03 18:42:40 +0000879 info->exe_file = NULL;
Alexandre Julliarde9936d92001-01-26 00:22:26 +0000880 info->owner = (struct thread *)grab_object( current );
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000881 info->process = NULL;
882 info->thread = NULL;
Alexandre Julliard6543a652002-03-29 18:28:56 +0000883 info->data_size = get_req_data_size();
884 info->data = NULL;
Alexandre Julliard6a72dc52000-04-14 13:42:00 +0000885
Alexandre Julliard8081e5a2001-01-05 04:08:07 +0000886 if (req->exe_file &&
Alexandre Julliardd27624b2000-05-03 18:42:40 +0000887 !(info->exe_file = get_file_obj( current->process, req->exe_file, GENERIC_READ )))
Alexandre Julliarde9936d92001-01-26 00:22:26 +0000888 goto done;
Alexandre Julliardd27624b2000-05-03 18:42:40 +0000889
Alexandre Julliard6543a652002-03-29 18:28:56 +0000890 if (!(info->data = mem_alloc( info->data_size ))) goto done;
891 memcpy( info->data, get_req_data(), info->data_size );
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000892 reply->info = alloc_handle( current->process, info, SYNCHRONIZE, FALSE );
Alexandre Julliarde9936d92001-01-26 00:22:26 +0000893
894 done:
895 release_object( info );
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000896}
Alexandre Julliard6a72dc52000-04-14 13:42:00 +0000897
Alexandre Julliarde9936d92001-01-26 00:22:26 +0000898/* Retrieve information about a newly started process */
899DECL_HANDLER(get_new_process_info)
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000900{
Alexandre Julliarde9936d92001-01-26 00:22:26 +0000901 struct startup_info *info;
902
Alexandre Julliarde9936d92001-01-26 00:22:26 +0000903 if ((info = (struct startup_info *)get_handle_obj( current->process, req->info,
904 0, &startup_info_ops )))
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000905 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000906 reply->pid = get_process_id( info->process );
907 reply->tid = get_thread_id( info->thread );
908 reply->phandle = alloc_handle( current->process, info->process,
909 PROCESS_ALL_ACCESS, req->pinherit );
910 reply->thandle = alloc_handle( current->process, info->thread,
911 THREAD_ALL_ACCESS, req->tinherit );
Alexandre Julliardca96de32002-05-25 21:15:03 +0000912 reply->success = is_process_init_done( info->process );
Alexandre Julliarde9936d92001-01-26 00:22:26 +0000913 release_object( info );
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000914 }
915 else
916 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000917 reply->pid = 0;
918 reply->tid = 0;
919 reply->phandle = 0;
920 reply->thandle = 0;
Alexandre Julliard9d802152002-05-24 21:20:27 +0000921 reply->success = 0;
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000922 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000923}
924
Alexandre Julliard6543a652002-03-29 18:28:56 +0000925/* Retrieve the new process startup info */
926DECL_HANDLER(get_startup_info)
927{
928 struct startup_info *info;
929
Alexandre Julliard9d802152002-05-24 21:20:27 +0000930 if ((info = current->process->startup_info))
Alexandre Julliard6543a652002-03-29 18:28:56 +0000931 {
932 size_t size = info->data_size;
933 if (size > get_reply_max_size()) size = get_reply_max_size();
Alexandre Julliard9d802152002-05-24 21:20:27 +0000934
935 /* we return the data directly without making a copy so this can only be called once */
936 set_reply_data_ptr( info->data, size );
937 info->data = NULL;
938 info->data_size = 0;
Alexandre Julliard6543a652002-03-29 18:28:56 +0000939 }
940}
941
942
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000943/* initialize a new process */
944DECL_HANDLER(init_process)
945{
Alexandre Julliard02a53c12003-02-25 04:17:22 +0000946 if (current->unix_pid == -1)
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000947 {
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000948 fatal_protocol_error( current, "init_process: init_thread not called yet\n" );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000949 return;
950 }
Alexandre Julliard9d802152002-05-24 21:20:27 +0000951 if (current->process->startup_info)
Alexandre Julliard6543a652002-03-29 18:28:56 +0000952 {
Alexandre Julliard9d802152002-05-24 21:20:27 +0000953 fatal_protocol_error( current, "init_process: called twice\n" );
954 return;
Alexandre Julliard6543a652002-03-29 18:28:56 +0000955 }
Alexandre Julliard9d802152002-05-24 21:20:27 +0000956 reply->info_size = 0;
957 current->process->ldt_copy = req->ldt_copy;
Alexandre Julliard77c8b1d2003-02-24 20:51:50 +0000958 current->process->startup_info = init_process( reply );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000959}
960
Alexandre Julliardec7bb231999-11-12 03:35:25 +0000961/* signal the end of the process initialization */
962DECL_HANDLER(init_process_done)
963{
Alexandre Julliardac2e4f12001-10-25 19:52:12 +0000964 struct file *file = NULL;
Alexandre Julliardec7bb231999-11-12 03:35:25 +0000965 struct process *process = current->process;
Alexandre Julliardac2e4f12001-10-25 19:52:12 +0000966
Alexandre Julliard9d802152002-05-24 21:20:27 +0000967 if (is_process_init_done(process))
Alexandre Julliardec7bb231999-11-12 03:35:25 +0000968 {
Alexandre Julliard9d802152002-05-24 21:20:27 +0000969 fatal_protocol_error( current, "init_process_done: called twice\n" );
970 return;
971 }
972 if (!req->module)
973 {
974 fatal_protocol_error( current, "init_process_done: module base address cannot be 0\n" );
Alexandre Julliardec7bb231999-11-12 03:35:25 +0000975 return;
976 }
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000977 process->exe.base = req->module;
Alexandre Julliardaeb56602002-03-22 00:21:23 +0000978 process->exe.size = req->module_size;
Alexandre Julliarda37dec02000-06-08 00:57:24 +0000979 process->exe.name = req->name;
Alexandre Julliardad29b902001-01-05 22:24:15 +0000980
Alexandre Julliardca96de32002-05-25 21:15:03 +0000981 if (req->exe_file) file = get_file_obj( process, req->exe_file, GENERIC_READ );
Alexandre Julliardac2e4f12001-10-25 19:52:12 +0000982 if (process->exe.file) release_object( process->exe.file );
983 process->exe.file = file;
984
Alexandre Julliardaeb56602002-03-22 00:21:23 +0000985 if ((process->exe.namelen = get_req_data_size()))
986 process->exe.filename = memdup( get_req_data(), process->exe.namelen );
987
Alexandre Julliard9d802152002-05-24 21:20:27 +0000988 generate_startup_debug_events( process, req->entry );
989 set_process_startup_state( process, STARTUP_DONE );
990
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000991 if (req->gui) process->idle_event = create_event( NULL, 0, 1, 0 );
Alexandre Julliardca96de32002-05-25 21:15:03 +0000992 if (current->suspend + process->suspend > 0) stop_thread( current );
993 reply->debugged = (process->debugger != 0);
Alexandre Julliardec7bb231999-11-12 03:35:25 +0000994}
995
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000996/* open a handle to a process */
997DECL_HANDLER(open_process)
998{
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000999 struct process *process = get_process_from_id( req->pid );
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001000 reply->handle = 0;
Alexandre Julliard43c190e1999-05-15 10:48:19 +00001001 if (process)
1002 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001003 reply->handle = alloc_handle( current->process, process, req->access, req->inherit );
Alexandre Julliard43c190e1999-05-15 10:48:19 +00001004 release_object( process );
1005 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +00001006}
1007
1008/* terminate a process */
1009DECL_HANDLER(terminate_process)
1010{
1011 struct process *process;
1012
1013 if ((process = get_process_from_handle( req->handle, PROCESS_TERMINATE )))
1014 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001015 reply->self = (current->process == process);
Alexandre Julliard12f29b52000-03-17 15:16:57 +00001016 kill_process( process, current, req->exit_code );
Alexandre Julliard43c190e1999-05-15 10:48:19 +00001017 release_object( process );
1018 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +00001019}
1020
1021/* fetch information about a process */
1022DECL_HANDLER(get_process_info)
1023{
1024 struct process *process;
Alexandre Julliard43c190e1999-05-15 10:48:19 +00001025
1026 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1027 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001028 get_process_info( process, reply );
Alexandre Julliard43c190e1999-05-15 10:48:19 +00001029 release_object( process );
1030 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +00001031}
1032
1033/* set information about a process */
1034DECL_HANDLER(set_process_info)
1035{
1036 struct process *process;
1037
1038 if ((process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION )))
1039 {
1040 set_process_info( process, req );
1041 release_object( process );
1042 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +00001043}
Alexandre Julliard8b8828f1999-11-12 21:39:14 +00001044
1045/* read data from a process address space */
1046DECL_HANDLER(read_process_memory)
1047{
1048 struct process *process;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001049 size_t len = get_reply_max_size();
Alexandre Julliard8b8828f1999-11-12 21:39:14 +00001050
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001051 if (!(process = get_process_from_handle( req->handle, PROCESS_VM_READ ))) return;
1052
1053 if (len)
Alexandre Julliard8b8828f1999-11-12 21:39:14 +00001054 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001055 unsigned int start_offset = (unsigned int)req->addr % sizeof(int);
1056 unsigned int nb_ints = (len + start_offset + sizeof(int) - 1) / sizeof(int);
1057 const int *start = (int *)((char *)req->addr - start_offset);
1058 int *buffer = mem_alloc( nb_ints * sizeof(int) );
1059 if (buffer)
1060 {
1061 if (read_process_memory( process, start, nb_ints, buffer ))
1062 {
1063 /* move start of requested data to start of buffer */
1064 if (start_offset) memmove( buffer, (char *)buffer + start_offset, len );
1065 set_reply_data_ptr( buffer, len );
1066 }
1067 else len = 0;
1068 }
Alexandre Julliard8b8828f1999-11-12 21:39:14 +00001069 }
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001070 release_object( process );
Alexandre Julliard8b8828f1999-11-12 21:39:14 +00001071}
Alexandre Julliardeef70251999-11-13 19:54:54 +00001072
1073/* write data to a process address space */
1074DECL_HANDLER(write_process_memory)
1075{
1076 struct process *process;
1077
1078 if ((process = get_process_from_handle( req->handle, PROCESS_VM_WRITE )))
1079 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001080 size_t len = get_req_data_size();
1081 if ((len % sizeof(int)) || ((unsigned int)req->addr % sizeof(int)))
1082 set_error( STATUS_INVALID_PARAMETER );
1083 else
1084 {
1085 if (len) write_process_memory( process, req->addr, len / sizeof(int),
1086 req->first_mask, req->last_mask, get_req_data() );
1087 }
Alexandre Julliardeef70251999-11-13 19:54:54 +00001088 release_object( process );
1089 }
1090}
Alexandre Julliard05f0b712000-03-09 18:18:41 +00001091
1092/* notify the server that a dll has been loaded */
1093DECL_HANDLER(load_dll)
1094{
1095 struct process_dll *dll;
1096 struct file *file = NULL;
1097
Alexandre Julliard8081e5a2001-01-05 04:08:07 +00001098 if (req->handle &&
Alexandre Julliard05f0b712000-03-09 18:18:41 +00001099 !(file = get_file_obj( current->process, req->handle, GENERIC_READ ))) return;
Alexandre Julliard8081e5a2001-01-05 04:08:07 +00001100
Alexandre Julliardaeb56602002-03-22 00:21:23 +00001101 if ((dll = process_load_dll( current->process, file, req->base,
1102 get_req_data(), get_req_data_size() )))
Alexandre Julliard05f0b712000-03-09 18:18:41 +00001103 {
Alexandre Julliardaeb56602002-03-22 00:21:23 +00001104 dll->size = req->size;
Alexandre Julliard05f0b712000-03-09 18:18:41 +00001105 dll->dbg_offset = req->dbg_offset;
1106 dll->dbg_size = req->dbg_size;
1107 dll->name = req->name;
1108 /* only generate event if initialization is done */
Alexandre Julliard9d802152002-05-24 21:20:27 +00001109 if (is_process_init_done( current->process ))
Alexandre Julliard05f0b712000-03-09 18:18:41 +00001110 generate_debug_event( current, LOAD_DLL_DEBUG_EVENT, dll );
1111 }
1112 if (file) release_object( file );
1113}
1114
1115/* notify the server that a dll is being unloaded */
1116DECL_HANDLER(unload_dll)
1117{
1118 process_unload_dll( current->process, req->base );
1119}
Alexandre Julliardc5e433a2000-05-30 19:48:18 +00001120
Eric Pouech2359b572003-01-09 00:01:28 +00001121/* retrieve information about a module in a process */
1122DECL_HANDLER(get_dll_info)
1123{
1124 struct process *process;
1125
1126 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1127 {
1128 struct process_dll *dll;
1129
1130 for (dll = &process->exe; dll; dll = dll->next)
1131 {
1132 if (dll->base == req->base_address)
1133 {
1134 reply->size = dll->size;
1135 reply->entry_point = 0; /* FIXME */
1136 if (dll->filename)
1137 {
1138 size_t len = min( dll->namelen, get_reply_max_size() );
1139 set_reply_data( dll->filename, len );
1140 }
1141 break;
1142 }
1143 }
1144 if (!dll)
1145 set_error(STATUS_DLL_NOT_FOUND);
1146 release_object( process );
1147 }
1148}
1149
Alexandre Julliardc5e433a2000-05-30 19:48:18 +00001150/* wait for a process to start waiting on input */
1151/* FIXME: only returns event for now, wait is done in the client */
1152DECL_HANDLER(wait_input_idle)
1153{
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 {
1159 if (process->idle_event && process != current->process && process->queue != current->queue)
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}