2 * Server-side process management
4 * Copyright (C) 1998 Alexandre Julliard
25 /* process structure */
27 static struct process *first_process;
28 static int running_processes;
30 /* process operations */
32 static void process_dump( struct object *obj, int verbose );
33 static int process_signaled( struct object *obj, struct thread *thread );
34 static void process_destroy( struct object *obj );
36 static const struct object_ops process_ops =
38 sizeof(struct process),
52 /* create a new process */
53 static struct process *create_process( struct process *parent, struct new_process_request *req,
54 const char *cmd_line, size_t len )
56 struct process *process;
58 if (!(process = alloc_object( &process_ops ))) return NULL;
61 process->thread_list = NULL;
62 process->debug_next = NULL;
63 process->debug_prev = NULL;
64 process->debugger = NULL;
65 process->handles = NULL;
66 process->exit_code = 0x103; /* STILL_ACTIVE */
67 process->running_threads = 0;
68 process->priority = NORMAL_PRIORITY_CLASS;
69 process->affinity = 1;
71 process->console_in = NULL;
72 process->console_out = NULL;
74 gettimeofday( &process->start_time, NULL );
77 process->handles = copy_handle_table( process, parent );
79 process->handles = alloc_handle_table( process, 0 );
80 if (!process->handles) goto error;
82 /* alloc a handle for the process itself */
83 alloc_handle( process, process, PROCESS_ALL_ACCESS, 0 );
85 if (!(process->info = mem_alloc( sizeof(*process->info) + len ))) goto error;
86 memcpy( process->info, req, sizeof(*req) );
87 memcpy( process->info->cmdline, cmd_line, len );
88 process->info->cmdline[len] = 0;
90 /* set the process console */
91 if (req->create_flags & CREATE_NEW_CONSOLE)
93 if (!alloc_console( process )) goto error;
95 else if (!(req->create_flags & DETACHED_PROCESS))
97 if (parent->console_in) process->console_in = grab_object( parent->console_in );
98 if (parent->console_out) process->console_out = grab_object( parent->console_out );
101 if (!req->inherit_all && !(req->start_flags & STARTF_USESTDHANDLES))
103 process->info->hstdin = duplicate_handle( parent, req->hstdin, process,
104 0, TRUE, DUPLICATE_SAME_ACCESS );
105 process->info->hstdout = duplicate_handle( parent, req->hstdout, process,
106 0, TRUE, DUPLICATE_SAME_ACCESS );
107 process->info->hstderr = duplicate_handle( parent, req->hstderr, process,
108 0, TRUE, DUPLICATE_SAME_ACCESS );
111 /* attach to the debugger if requested */
112 if (req->create_flags & DEBUG_PROCESS)
113 debugger_attach( process, current );
114 else if (parent && parent->debugger && !(req->create_flags & DEBUG_ONLY_THIS_PROCESS))
115 debugger_attach( process, parent->debugger );
117 if ((process->next = first_process) != NULL) process->next->prev = process;
118 first_process = process;
122 free_console( process );
123 if (process->info) free( process->info );
124 if (process->handles) release_object( process->handles );
125 release_object( process );
129 /* create the initial process */
130 struct process *create_initial_process(void)
132 struct process *process;
133 struct new_process_request req;
137 req.create_flags = CREATE_NEW_CONSOLE;
138 req.start_flags = STARTF_USESTDHANDLES;
144 if ((process = create_process( NULL, &req, "", 1 )))
146 process->info->hstdin = alloc_handle( process, process->console_in,
147 GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, 1 );
148 process->info->hstdout = alloc_handle( process, process->console_out,
149 GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, 1 );
150 process->info->hstderr = alloc_handle( process, process->console_out,
151 GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, 1 );
156 /* destroy a process when its refcount is 0 */
157 static void process_destroy( struct object *obj )
159 struct process *process = (struct process *)obj;
160 assert( obj->ops == &process_ops );
162 /* we can't have a thread remaining */
163 assert( !process->thread_list );
164 if (process->next) process->next->prev = process->prev;
165 if (process->prev) process->prev->next = process->next;
166 else first_process = process->next;
167 if (process->info) free( process->info );
170 /* dump a process on stdout for debugging purposes */
171 static void process_dump( struct object *obj, int verbose )
173 struct process *process = (struct process *)obj;
174 assert( obj->ops == &process_ops );
176 fprintf( stderr, "Process next=%p prev=%p console=%p/%p handles=%p\n",
177 process->next, process->prev, process->console_in, process->console_out,
181 static int process_signaled( struct object *obj, struct thread *thread )
183 struct process *process = (struct process *)obj;
184 return !process->running_threads;
188 /* get a process from an id (and increment the refcount) */
189 struct process *get_process_from_id( void *id )
191 struct process *p = first_process;
192 while (p && (p != id)) p = p->next;
193 if (p) grab_object( p );
194 else set_error( ERROR_INVALID_PARAMETER );
198 /* get a process from a handle (and increment the refcount) */
199 struct process *get_process_from_handle( int handle, unsigned int access )
201 return (struct process *)get_handle_obj( current->process, handle,
202 access, &process_ops );
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 release_object( process->handles );
212 process->handles = NULL;
213 free_console( process );
214 wake_up( &process->obj, 0 );
215 if (!--running_processes)
217 /* last process died, close global handles */
218 close_global_handles();
222 /* add a thread to a process running threads list */
223 void add_process_thread( struct process *process, struct thread *thread )
225 thread->proc_next = process->thread_list;
226 thread->proc_prev = NULL;
227 if (thread->proc_next) thread->proc_next->proc_prev = thread;
228 process->thread_list = thread;
229 if (!process->running_threads++) running_processes++;
230 grab_object( thread );
233 /* remove a thread from a process running threads list */
234 void remove_process_thread( struct process *process, struct thread *thread )
236 assert( process->running_threads > 0 );
237 assert( process->thread_list );
239 if (thread->proc_next) thread->proc_next->proc_prev = thread->proc_prev;
240 if (thread->proc_prev) thread->proc_prev->proc_next = thread->proc_next;
241 else process->thread_list = thread->proc_next;
243 if (!--process->running_threads)
245 /* we have removed the last running thread, exit the process */
246 process_killed( process, thread->exit_code );
248 release_object( thread );
251 /* suspend all the threads of a process */
252 void suspend_process( struct process *process )
254 if (!process->suspend++)
256 struct thread *thread = process->thread_list;
257 for (; thread; thread = thread->proc_next)
259 if (!thread->suspend && thread->unix_pid)
260 kill( thread->unix_pid, SIGSTOP );
265 /* resume all the threads of a process */
266 void resume_process( struct process *process )
268 assert (process->suspend > 0);
269 if (!--process->suspend)
271 struct thread *thread = process->thread_list;
272 for (; thread; thread = thread->proc_next)
274 if (!thread->suspend && thread->unix_pid)
275 kill( thread->unix_pid, SIGCONT );
280 /* kill a process on the spot */
281 void kill_process( struct process *process, int exit_code )
283 while (process->thread_list)
284 kill_thread( process->thread_list, exit_code );
287 /* get all information about a process */
288 static void get_process_info( struct process *process, struct get_process_info_request *req )
291 req->exit_code = process->exit_code;
292 req->priority = process->priority;
293 req->process_affinity = process->affinity;
294 req->system_affinity = 1;
297 /* set all information about a process */
298 static void set_process_info( struct process *process,
299 struct set_process_info_request *req )
301 if (req->mask & SET_PROCESS_INFO_PRIORITY)
302 process->priority = req->priority;
303 if (req->mask & SET_PROCESS_INFO_AFFINITY)
305 if (req->affinity != 1) set_error( ERROR_INVALID_PARAMETER );
306 else process->affinity = req->affinity;
310 /* take a snapshot of currently running processes */
311 struct process_snapshot *process_snap( int *count )
313 struct process_snapshot *snapshot, *ptr;
314 struct process *process;
315 if (!running_processes) return NULL;
316 if (!(snapshot = mem_alloc( sizeof(*snapshot) * running_processes )))
319 for (process = first_process; process; process = process->next)
321 if (!process->running_threads) continue;
322 ptr->process = process;
323 ptr->threads = process->running_threads;
324 ptr->priority = process->priority;
325 grab_object( process );
328 *count = running_processes;
332 /* create a new process */
333 DECL_HANDLER(new_process)
335 size_t len = get_req_strlen( req->cmdline );
336 struct process *process;
340 if ((process = create_process( current->process, req, req->cmdline, len )))
342 req->handle = alloc_handle( current->process, process, PROCESS_ALL_ACCESS, req->inherit );
344 release_object( process );
348 /* initialize a new process */
349 DECL_HANDLER(init_process)
351 struct new_process_request *info;
353 if (current->state == STARTING)
355 fatal_protocol_error( current, "init_process: init_thread not called yet\n" );
358 if (!(info = current->process->info))
360 fatal_protocol_error( current, "init_process: called twice\n" );
363 current->process->info = NULL;
364 req->start_flags = info->start_flags;
365 req->hstdin = info->hstdin;
366 req->hstdout = info->hstdout;
367 req->hstderr = info->hstderr;
368 req->cmd_show = info->cmd_show;
369 req->env_ptr = info->env_ptr;
370 strcpy( req->cmdline, info->cmdline );
374 /* open a handle to a process */
375 DECL_HANDLER(open_process)
377 struct process *process = get_process_from_id( req->pid );
381 req->handle = alloc_handle( current->process, process, req->access, req->inherit );
382 release_object( process );
386 /* terminate a process */
387 DECL_HANDLER(terminate_process)
389 struct process *process;
391 if ((process = get_process_from_handle( req->handle, PROCESS_TERMINATE )))
393 kill_process( process, req->exit_code );
394 release_object( process );
398 /* fetch information about a process */
399 DECL_HANDLER(get_process_info)
401 struct process *process;
403 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
405 get_process_info( process, req );
406 release_object( process );
410 /* set information about a process */
411 DECL_HANDLER(set_process_info)
413 struct process *process;
415 if ((process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION )))
417 set_process_info( process, req );
418 release_object( process );