Added support for FreeBSD 3.0 to DEBUG_checkmap_bad().
[wine.git] / server / process.c
blobde97ff4eecf373d2ce5fa545e7d8a5f0fa08a737
1 /*
2 * Server-side process management
4 * Copyright (C) 1998 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include <limits.h>
9 #include <signal.h>
10 #include <string.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <sys/time.h>
14 #include <unistd.h>
16 #include "winerror.h"
17 #include "winbase.h"
18 #include "winnt.h"
20 #include "server.h"
21 #include "handle.h"
22 #include "process.h"
23 #include "thread.h"
25 /* process structure */
27 static struct process initial_process;
28 static struct process *first_process = &initial_process;
29 static int running_processes;
31 /* process operations */
33 static void process_dump( struct object *obj, int verbose );
34 static int process_signaled( struct object *obj, struct thread *thread );
35 static void process_destroy( struct object *obj );
37 static const struct object_ops process_ops =
39 process_dump,
40 add_queue,
41 remove_queue,
42 process_signaled,
43 no_satisfied,
44 no_read_fd,
45 no_write_fd,
46 no_flush,
47 no_get_file_info,
48 process_destroy
52 /* initialization of a process structure */
53 static void init_process( struct process *process )
55 init_object( &process->obj, &process_ops, NULL );
56 process->next = NULL;
57 process->prev = NULL;
58 process->thread_list = NULL;
59 process->debug_next = NULL;
60 process->debug_prev = NULL;
61 process->debugger = NULL;
62 process->exit_code = 0x103; /* STILL_ACTIVE */
63 process->running_threads = 0;
64 process->priority = NORMAL_PRIORITY_CLASS;
65 process->affinity = 1;
66 process->suspend = 0;
67 process->console_in = NULL;
68 process->console_out = NULL;
69 process->info = NULL;
70 gettimeofday( &process->start_time, NULL );
71 /* alloc a handle for the process itself */
72 alloc_handle( process, process, PROCESS_ALL_ACCESS, 0 );
75 /* create the initial process */
76 struct process *create_initial_process(void)
78 struct new_process_request *info;
80 copy_handle_table( &initial_process, NULL );
81 init_process( &initial_process );
83 if (!alloc_console( &initial_process )) return NULL;
84 if (!(info = mem_alloc( sizeof(*info) ))) return NULL;
85 info->start_flags = STARTF_USESTDHANDLES;
86 info->hstdin = alloc_handle( &initial_process, initial_process.console_in,
87 GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, 1 );
88 info->hstdout = alloc_handle( &initial_process, initial_process.console_out,
89 GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, 1 );
90 info->hstderr = alloc_handle( &initial_process, initial_process.console_out,
91 GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, 1 );
92 info->env_ptr = NULL;
93 initial_process.info = info;
94 grab_object( &initial_process ); /* so that we never free it */
95 return &initial_process;
99 /* create a new process */
100 static struct process *create_process( struct new_process_request *req )
102 struct process *process = NULL;
103 struct process *parent = current->process;
105 if (!(process = mem_alloc( sizeof(*process) ))) return NULL;
106 if (!copy_handle_table( process, req->inherit_all ? parent : NULL ))
108 free( process );
109 return NULL;
111 init_process( process );
112 if (parent->console_in) process->console_in = grab_object( parent->console_in );
113 if (parent->console_out) process->console_out = grab_object( parent->console_out );
115 if (!(process->info = mem_alloc( sizeof(*process->info) ))) goto error;
116 memcpy( process->info, req, sizeof(*req) );
118 if (!req->inherit_all && !(req->start_flags & STARTF_USESTDHANDLES))
120 process->info->hstdin = duplicate_handle( parent, req->hstdin, process,
121 0, TRUE, DUPLICATE_SAME_ACCESS );
122 process->info->hstdout = duplicate_handle( parent, req->hstdout, process,
123 0, TRUE, DUPLICATE_SAME_ACCESS );
124 process->info->hstderr = duplicate_handle( parent, req->hstderr, process,
125 0, TRUE, DUPLICATE_SAME_ACCESS );
128 process->next = first_process;
129 first_process->prev = process;
130 first_process = process;
131 return process;
133 error:
134 release_object( process );
135 return NULL;
138 /* destroy a process when its refcount is 0 */
139 static void process_destroy( struct object *obj )
141 struct process *process = (struct process *)obj;
142 assert( obj->ops == &process_ops );
143 assert( process != &initial_process );
145 /* we can't have a thread remaining */
146 assert( !process->thread_list );
147 if (process->next) process->next->prev = process->prev;
148 if (process->prev) process->prev->next = process->next;
149 else first_process = process->next;
150 free_console( process );
151 free_handles( process );
152 if (process->info) free( process->info );
153 if (debug_level) memset( process, 0xbb, sizeof(process) ); /* catch errors */
154 free( process );
157 /* dump a process on stdout for debugging purposes */
158 static void process_dump( struct object *obj, int verbose )
160 struct process *process = (struct process *)obj;
161 assert( obj->ops == &process_ops );
163 printf( "Process next=%p prev=%p\n", process->next, process->prev );
166 static int process_signaled( struct object *obj, struct thread *thread )
168 struct process *process = (struct process *)obj;
169 return !process->running_threads;
173 /* get a process from an id (and increment the refcount) */
174 struct process *get_process_from_id( void *id )
176 struct process *p = first_process;
177 while (p && (p != id)) p = p->next;
178 if (p) grab_object( p );
179 else SET_ERROR( ERROR_INVALID_PARAMETER );
180 return p;
183 /* get a process from a handle (and increment the refcount) */
184 struct process *get_process_from_handle( int handle, unsigned int access )
186 return (struct process *)get_handle_obj( current->process, handle,
187 access, &process_ops );
190 /* retrieve the initialization info for a new process */
191 static int get_process_init_info( struct process *process, struct init_process_reply *reply )
193 struct new_process_request *info;
194 if (!(info = process->info)) return 0;
195 process->info = NULL;
196 reply->start_flags = info->start_flags;
197 reply->hstdin = info->hstdin;
198 reply->hstdout = info->hstdout;
199 reply->hstderr = info->hstderr;
200 reply->env_ptr = info->env_ptr;
201 free( info );
202 return 1;
205 /* a process has been killed (i.e. its last thread died) */
206 static void process_killed( struct process *process, int exit_code )
208 assert( !process->thread_list );
209 process->exit_code = exit_code;
210 gettimeofday( &process->end_time, NULL );
211 wake_up( &process->obj, 0 );
212 free_handles( process );
215 /* add a thread to a process running threads list */
216 void add_process_thread( struct process *process, struct thread *thread )
218 thread->proc_next = process->thread_list;
219 thread->proc_prev = NULL;
220 if (thread->proc_next) thread->proc_next->proc_prev = thread;
221 process->thread_list = thread;
222 if (!process->running_threads++) running_processes++;
223 grab_object( thread );
226 /* remove a thread from a process running threads list */
227 void remove_process_thread( struct process *process, struct thread *thread )
229 assert( process->running_threads > 0 );
230 assert( process->thread_list );
232 if (thread->proc_next) thread->proc_next->proc_prev = thread->proc_prev;
233 if (thread->proc_prev) thread->proc_prev->proc_next = thread->proc_next;
234 else process->thread_list = thread->proc_next;
236 if (!--process->running_threads)
238 /* we have removed the last running thread, exit the process */
239 running_processes--;
240 process_killed( process, thread->exit_code );
242 release_object( thread );
245 /* suspend all the threads of a process */
246 void suspend_process( struct process *process )
248 if (!process->suspend++)
250 struct thread *thread = process->thread_list;
251 for (; thread; thread = thread->proc_next)
253 if (!thread->suspend && thread->unix_pid)
254 kill( thread->unix_pid, SIGSTOP );
259 /* resume all the threads of a process */
260 void resume_process( struct process *process )
262 assert (process->suspend > 0);
263 if (!--process->suspend)
265 struct thread *thread = process->thread_list;
266 for (; thread; thread = thread->proc_next)
268 if (!thread->suspend && thread->unix_pid)
269 kill( thread->unix_pid, SIGCONT );
274 /* kill a process on the spot */
275 void kill_process( struct process *process, int exit_code )
277 while (process->thread_list)
278 kill_thread( process->thread_list, exit_code );
281 /* get all information about a process */
282 static void get_process_info( struct process *process,
283 struct get_process_info_reply *reply )
285 reply->pid = process;
286 reply->exit_code = process->exit_code;
287 reply->priority = process->priority;
288 reply->process_affinity = process->affinity;
289 reply->system_affinity = 1;
292 /* set all information about a process */
293 static void set_process_info( struct process *process,
294 struct set_process_info_request *req )
296 if (req->mask & SET_PROCESS_INFO_PRIORITY)
297 process->priority = req->priority;
298 if (req->mask & SET_PROCESS_INFO_AFFINITY)
300 if (req->affinity != 1) SET_ERROR( ERROR_INVALID_PARAMETER );
301 else process->affinity = req->affinity;
305 /* allocate a console for this process */
306 int alloc_console( struct process *process )
308 struct object *obj[2];
309 if (process->console_in || process->console_out)
311 SET_ERROR( ERROR_ACCESS_DENIED );
312 return 0;
314 if (!create_console( -1, obj )) return 0;
315 process->console_in = obj[0];
316 process->console_out = obj[1];
317 return 1;
320 /* free the console for this process */
321 int free_console( struct process *process )
323 if (process->console_in) release_object( process->console_in );
324 if (process->console_out) release_object( process->console_out );
325 process->console_in = process->console_out = NULL;
326 return 1;
329 /* get the process console */
330 struct object *get_console( struct process *process, int output )
332 struct object *obj;
333 if (!(obj = output ? process->console_out : process->console_in))
334 return NULL;
335 return grab_object( obj );
338 /* take a snapshot of currently running processes */
339 struct process_snapshot *process_snap( int *count )
341 struct process_snapshot *snapshot, *ptr;
342 struct process *process;
343 if (!running_processes) return NULL;
344 if (!(snapshot = mem_alloc( sizeof(*snapshot) * running_processes )))
345 return NULL;
346 ptr = snapshot;
347 for (process = first_process; process; process = process->next)
349 if (!process->running_threads) continue;
350 ptr->process = process;
351 ptr->threads = process->running_threads;
352 ptr->priority = process->priority;
353 grab_object( process );
354 ptr++;
356 *count = running_processes;
357 return snapshot;
360 /* create a new process */
361 DECL_HANDLER(new_process)
363 struct new_process_reply reply;
364 struct process *process;
366 if ((process = create_process( req )))
368 reply.pid = process;
369 reply.handle = alloc_handle( current->process, process,
370 PROCESS_ALL_ACCESS, req->inherit );
371 release_object( process );
373 else
375 reply.handle = -1;
376 reply.pid = NULL;
378 send_reply( current, -1, 1, &reply, sizeof(reply) );
381 /* initialize a new process */
382 DECL_HANDLER(init_process)
384 struct init_process_reply reply;
385 if (current->state != RUNNING)
387 fatal_protocol_error( "init_process: init_thread not called yet\n" );
388 return;
390 if (!get_process_init_info( current->process, &reply ))
392 fatal_protocol_error( "init_process: called twice\n" );
393 return;
395 send_reply( current, -1, 1, &reply, sizeof(reply) );
398 /* open a handle to a process */
399 DECL_HANDLER(open_process)
401 struct open_process_reply reply = { -1 };
402 struct process *process = get_process_from_id( req->pid );
403 if (process)
405 reply.handle = alloc_handle( current->process, process,
406 req->access, req->inherit );
407 release_object( process );
409 send_reply( current, -1, 1, &reply, sizeof(reply) );
412 /* terminate a process */
413 DECL_HANDLER(terminate_process)
415 struct process *process;
417 if ((process = get_process_from_handle( req->handle, PROCESS_TERMINATE )))
419 kill_process( process, req->exit_code );
420 release_object( process );
422 if (current) send_reply( current, -1, 0 );
425 /* fetch information about a process */
426 DECL_HANDLER(get_process_info)
428 struct process *process;
429 struct get_process_info_reply reply = { 0, 0, 0 };
431 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
433 get_process_info( process, &reply );
434 release_object( process );
436 send_reply( current, -1, 1, &reply, sizeof(reply) );
439 /* set information about a process */
440 DECL_HANDLER(set_process_info)
442 struct process *process;
444 if ((process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION )))
446 set_process_info( process, req );
447 release_object( process );
449 send_reply( current, -1, 0 );