2 * Server-side process management
4 * Copyright (C) 1998 Alexandre Julliard
17 #ifdef HAVE_SYS_SOCKET_H
18 # include <sys/socket.h>
30 /* process structure */
32 static struct process *first_process;
33 static int running_processes;
35 /* process operations */
37 static void process_dump( struct object *obj, int verbose );
38 static int process_signaled( struct object *obj, struct thread *thread );
39 static void process_destroy( struct object *obj );
41 static const struct object_ops process_ops =
43 sizeof(struct process), /* size */
44 process_dump, /* dump */
45 add_queue, /* add_queue */
46 remove_queue, /* remove_queue */
47 process_signaled, /* signaled */
48 no_satisfied, /* satisfied */
49 NULL, /* get_poll_events */
50 NULL, /* poll_event */
51 no_read_fd, /* get_read_fd */
52 no_write_fd, /* get_write_fd */
54 no_get_file_info, /* get_file_info */
55 process_destroy /* destroy */
58 /* set the process creation info */
59 static int set_creation_info( struct process *process, struct new_process_request *req,
60 const char *cmd_line, size_t len )
62 if (!(process->info = mem_alloc( sizeof(*process->info) + len ))) return 0;
65 /* copy the request structure */
66 memcpy( process->info, req, sizeof(*req) );
68 else /* no request, use defaults */
73 req->create_flags = CREATE_NEW_CONSOLE;
74 req->start_flags = STARTF_USESTDHANDLES;
83 memcpy( process->info->cmdline, cmd_line, len );
84 process->info->cmdline[len] = 0;
85 process->create_flags = process->info->create_flags;
89 /* set the console and stdio handles for a newly created process */
90 static int set_process_console( struct process *process, struct process *parent )
92 struct new_process_request *info = process->info;
94 if (process->create_flags & CREATE_NEW_CONSOLE)
96 if (!alloc_console( process )) return 0;
98 else if (!(process->create_flags & DETACHED_PROCESS))
100 if (parent->console_in) process->console_in = grab_object( parent->console_in );
101 if (parent->console_out) process->console_out = grab_object( parent->console_out );
105 if (!info->inherit_all && !(info->start_flags & STARTF_USESTDHANDLES))
107 /* duplicate the handle from the parent into this process */
108 info->hstdin = duplicate_handle( parent, info->hstdin, process,
109 0, TRUE, DUPLICATE_SAME_ACCESS );
110 info->hstdout = duplicate_handle( parent, info->hstdout, process,
111 0, TRUE, DUPLICATE_SAME_ACCESS );
112 info->hstderr = duplicate_handle( parent, info->hstderr, process,
113 0, TRUE, DUPLICATE_SAME_ACCESS );
118 /* no parent, use handles to the console for stdio */
119 info->hstdin = alloc_handle( process, process->console_in,
120 GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, 1 );
121 info->hstdout = alloc_handle( process, process->console_out,
122 GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, 1 );
123 info->hstderr = alloc_handle( process, process->console_out,
124 GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, 1 );
129 /* create a new process and its main thread */
130 struct thread *create_process( int fd, struct process *parent,
131 struct new_process_request *req,
132 const char *cmd_line, size_t len )
134 struct process *process;
135 struct thread *thread = NULL;
137 if (!(process = alloc_object( &process_ops, -1 )))
142 process->next = NULL;
143 process->prev = NULL;
144 process->thread_list = NULL;
145 process->debugger = NULL;
146 process->exe_file = NULL;
147 process->handles = NULL;
148 process->exit_code = STILL_ACTIVE;
149 process->running_threads = 0;
150 process->priority = NORMAL_PRIORITY_CLASS;
151 process->affinity = 1;
152 process->suspend = 0;
153 process->create_flags = 0;
154 process->console_in = NULL;
155 process->console_out = NULL;
156 process->init_event = NULL;
157 process->info = NULL;
158 process->ldt_copy = NULL;
159 process->ldt_flags = NULL;
160 gettimeofday( &process->start_time, NULL );
161 if ((process->next = first_process) != NULL) process->next->prev = process;
162 first_process = process;
164 /* copy the request structure */
165 if (!set_creation_info( process, req, cmd_line, len )) goto error;
167 if (process->info->inherit_all)
168 process->handles = copy_handle_table( process, parent );
170 process->handles = alloc_handle_table( process, 0 );
171 if (!process->handles) goto error;
173 /* alloc a handle for the process itself */
174 alloc_handle( process, process, PROCESS_ALL_ACCESS, 0 );
176 /* retrieve the main exe file */
177 if (process->info->exe_file != -1)
179 if (!(process->exe_file = get_file_obj( parent, process->info->exe_file,
180 GENERIC_READ ))) goto error;
181 process->info->exe_file = -1;
184 /* get the init done event */
185 if (process->info->event != -1)
187 if (!(process->init_event = get_event_obj( parent, process->info->event,
188 EVENT_MODIFY_STATE ))) goto error;
191 /* set the process console */
192 if (!set_process_console( process, parent )) goto error;
194 /* create the main thread */
195 if (!(thread = create_thread( fd, process, (process->create_flags & CREATE_SUSPENDED) != 0)))
198 /* attach to the debugger if requested */
199 if (process->create_flags & (DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS))
200 debugger_attach( process, current );
201 else if (parent && parent->debugger && !(parent->create_flags & DEBUG_ONLY_THIS_PROCESS))
202 debugger_attach( process, parent->debugger );
204 release_object( process );
209 free_console( process );
210 if (process->handles) release_object( process->handles );
211 release_object( process );
215 /* destroy a process when its refcount is 0 */
216 static void process_destroy( struct object *obj )
218 struct process *process = (struct process *)obj;
219 assert( obj->ops == &process_ops );
221 /* we can't have a thread remaining */
222 assert( !process->thread_list );
223 if (process->next) process->next->prev = process->prev;
224 if (process->prev) process->prev->next = process->next;
225 else first_process = process->next;
226 if (process->info) free( process->info );
227 if (process->init_event) release_object( process->init_event );
228 if (process->exe_file) release_object( process->exe_file );
231 /* dump a process on stdout for debugging purposes */
232 static void process_dump( struct object *obj, int verbose )
234 struct process *process = (struct process *)obj;
235 assert( obj->ops == &process_ops );
237 fprintf( stderr, "Process next=%p prev=%p console=%p/%p handles=%p\n",
238 process->next, process->prev, process->console_in, process->console_out,
242 static int process_signaled( struct object *obj, struct thread *thread )
244 struct process *process = (struct process *)obj;
245 return !process->running_threads;
249 /* get a process from an id (and increment the refcount) */
250 struct process *get_process_from_id( void *id )
252 struct process *p = first_process;
253 while (p && (p != id)) p = p->next;
254 if (p) grab_object( p );
255 else set_error( STATUS_INVALID_PARAMETER );
259 /* get a process from a handle (and increment the refcount) */
260 struct process *get_process_from_handle( int handle, unsigned int access )
262 return (struct process *)get_handle_obj( current->process, handle,
263 access, &process_ops );
266 /* a process has been killed (i.e. its last thread died) */
267 static void process_killed( struct process *process, int exit_code )
269 assert( !process->thread_list );
270 process->exit_code = exit_code;
271 gettimeofday( &process->end_time, NULL );
272 release_object( process->handles );
273 process->handles = NULL;
274 free_console( process );
275 if (process->exe_file) release_object( process->exe_file );
276 process->exe_file = NULL;
277 wake_up( &process->obj, 0 );
278 if (!--running_processes)
280 /* last process died, close global handles */
281 close_global_handles();
282 /* this will cause the select loop to terminate */
283 if (!persistent_server) close_master_socket();
287 /* add a thread to a process running threads list */
288 void add_process_thread( struct process *process, struct thread *thread )
290 thread->proc_next = process->thread_list;
291 thread->proc_prev = NULL;
292 if (thread->proc_next) thread->proc_next->proc_prev = thread;
293 process->thread_list = thread;
294 if (!process->running_threads++) running_processes++;
295 grab_object( thread );
298 /* remove a thread from a process running threads list */
299 void remove_process_thread( struct process *process, struct thread *thread )
301 assert( process->running_threads > 0 );
302 assert( process->thread_list );
304 if (thread->proc_next) thread->proc_next->proc_prev = thread->proc_prev;
305 if (thread->proc_prev) thread->proc_prev->proc_next = thread->proc_next;
306 else process->thread_list = thread->proc_next;
308 if (!--process->running_threads)
310 /* we have removed the last running thread, exit the process */
311 process_killed( process, thread->exit_code );
313 release_object( thread );
316 /* suspend all the threads of a process */
317 void suspend_process( struct process *process )
319 if (!process->suspend++)
321 struct thread *thread = process->thread_list;
322 for (; thread; thread = thread->proc_next)
324 if (!thread->suspend) stop_thread( thread );
329 /* resume all the threads of a process */
330 void resume_process( struct process *process )
332 assert (process->suspend > 0);
333 if (!--process->suspend)
335 struct thread *thread = process->thread_list;
336 for (; thread; thread = thread->proc_next)
338 if (!thread->suspend) continue_thread( thread );
343 /* kill a process on the spot */
344 void kill_process( struct process *process, int exit_code )
346 while (process->thread_list)
347 kill_thread( process->thread_list, exit_code );
350 /* kill all processes being debugged by a given thread */
351 void kill_debugged_processes( struct thread *debugger, int exit_code )
353 for (;;) /* restart from the beginning of the list every time */
355 struct process *process = first_process;
356 /* find the first process being debugged by 'debugger' and still running */
357 while (process && (process->debugger != debugger || !process->running_threads))
358 process = process->next;
359 if (!process) return;
360 process->debugger = NULL;
361 kill_process( process, exit_code );
365 /* get all information about a process */
366 static void get_process_info( struct process *process, struct get_process_info_request *req )
369 req->debugged = (process->debugger != 0);
370 req->exit_code = process->exit_code;
371 req->priority = process->priority;
372 req->process_affinity = process->affinity;
373 req->system_affinity = 1;
376 /* set all information about a process */
377 static void set_process_info( struct process *process,
378 struct set_process_info_request *req )
380 if (req->mask & SET_PROCESS_INFO_PRIORITY)
381 process->priority = req->priority;
382 if (req->mask & SET_PROCESS_INFO_AFFINITY)
384 if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER );
385 else process->affinity = req->affinity;
389 /* read data from a process memory space */
390 /* len is the total size (in ints), max is the size we can actually store in the output buffer */
391 /* we read the total size in all cases to check for permissions */
392 static void read_process_memory( struct process *process, const int *addr,
393 size_t len, size_t max, int *dest )
395 struct thread *thread = process->thread_list;
397 if ((unsigned int)addr % sizeof(int)) /* address must be aligned */
399 set_error( STATUS_INVALID_PARAMETER );
402 if (!thread) /* process is dead */
404 set_error( STATUS_ACCESS_DENIED );
407 suspend_thread( thread, 0 );
408 if (thread->attached)
410 while (len > 0 && max)
412 if (read_thread_int( thread, addr++, dest++ ) == -1) goto done;
416 /* check the rest for read permission */
419 int dummy, page = get_page_size() / sizeof(int);
424 if (read_thread_int( thread, addr - 1, &dummy ) == -1) goto done;
426 if (len && (read_thread_int( thread, addr + len - 1, &dummy ) == -1)) goto done;
429 else set_error( STATUS_ACCESS_DENIED );
431 resume_thread( thread );
434 /* write data to a process memory space */
435 /* len is the total size (in ints), max is the size we can actually read from the input buffer */
436 /* we check the total size for write permissions */
437 static void write_process_memory( struct process *process, int *addr, size_t len,
438 size_t max, unsigned int first_mask,
439 unsigned int last_mask, const int *src )
441 struct thread *thread = process->thread_list;
443 if (!len || ((unsigned int)addr % sizeof(int))) /* address must be aligned */
445 set_error( STATUS_INVALID_PARAMETER );
448 if (!thread) /* process is dead */
450 set_error( STATUS_ACCESS_DENIED );
453 suspend_thread( thread, 0 );
454 if (thread->attached)
456 /* first word is special */
459 if (write_thread_int( thread, addr++, *src++, first_mask ) == -1) goto done;
463 else last_mask &= first_mask;
465 while (len > 1 && max)
467 if (write_thread_int( thread, addr++, *src++, ~0 ) == -1) goto done;
474 /* last word is special too */
475 if (write_thread_int( thread, addr, *src, last_mask ) == -1) goto done;
479 /* check the rest for write permission */
480 int page = get_page_size() / sizeof(int);
485 if (write_thread_int( thread, addr - 1, 0, 0 ) == -1) goto done;
487 if (len && (write_thread_int( thread, addr + len - 1, 0, 0 ) == -1)) goto done;
490 else set_error( STATUS_ACCESS_DENIED );
492 resume_thread( thread );
495 /* take a snapshot of currently running processes */
496 struct process_snapshot *process_snap( int *count )
498 struct process_snapshot *snapshot, *ptr;
499 struct process *process;
500 if (!running_processes) return NULL;
501 if (!(snapshot = mem_alloc( sizeof(*snapshot) * running_processes )))
504 for (process = first_process; process; process = process->next)
506 if (!process->running_threads) continue;
507 ptr->process = process;
508 ptr->threads = process->running_threads;
509 ptr->priority = process->priority;
510 grab_object( process );
513 *count = running_processes;
517 /* create a new process */
518 DECL_HANDLER(new_process)
520 size_t len = get_req_strlen( req->cmdline );
521 struct thread *thread;
529 if (socketpair( AF_UNIX, SOCK_STREAM, 0, sock ) == -1)
535 if ((thread = create_process( sock[0], current->process, req, req->cmdline, len )))
537 int phandle = alloc_handle( current->process, thread->process,
538 PROCESS_ALL_ACCESS, req->inherit );
539 if ((req->phandle = phandle) != -1)
541 if ((req->thandle = alloc_handle( current->process, thread,
542 THREAD_ALL_ACCESS, req->inherit )) != -1)
544 /* thread object will be released when the thread gets killed */
545 set_reply_fd( current, sock[1] );
546 req->pid = thread->process;
550 close_handle( current->process, phandle );
552 release_object( thread );
557 /* initialize a new process */
558 DECL_HANDLER(init_process)
560 struct new_process_request *info;
562 if (!current->unix_pid)
564 fatal_protocol_error( current, "init_process: init_thread not called yet\n" );
567 if (!(info = current->process->info))
569 fatal_protocol_error( current, "init_process: called twice\n" );
572 current->process->ldt_copy = req->ldt_copy;
573 current->process->ldt_flags = req->ldt_flags;
574 current->process->info = NULL;
576 req->start_flags = info->start_flags;
577 req->hstdin = info->hstdin;
578 req->hstdout = info->hstdout;
579 req->hstderr = info->hstderr;
580 req->cmd_show = info->cmd_show;
581 req->env_ptr = info->env_ptr;
582 strcpy( req->cmdline, info->cmdline );
583 if (current->process->exe_file)
584 req->exe_file = alloc_handle( current->process, current->process->exe_file,
589 /* signal the end of the process initialization */
590 DECL_HANDLER(init_process_done)
592 struct process *process = current->process;
593 if (!process->init_event)
595 fatal_protocol_error( current, "init_process_done: no event\n" );
598 current->entry = req->entry;
599 process->module = req->module;
600 generate_debug_event( current, CREATE_PROCESS_DEBUG_EVENT );
601 set_event( process->init_event );
602 release_object( process->init_event );
603 process->init_event = NULL;
604 if (current->suspend + current->process->suspend > 0) stop_thread( current );
605 req->debugged = (current->process->debugger != 0);
608 /* open a handle to a process */
609 DECL_HANDLER(open_process)
611 struct process *process = get_process_from_id( req->pid );
615 req->handle = alloc_handle( current->process, process, req->access, req->inherit );
616 release_object( process );
620 /* terminate a process */
621 DECL_HANDLER(terminate_process)
623 struct process *process;
625 if ((process = get_process_from_handle( req->handle, PROCESS_TERMINATE )))
627 kill_process( process, req->exit_code );
628 release_object( process );
632 /* fetch information about a process */
633 DECL_HANDLER(get_process_info)
635 struct process *process;
637 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
639 get_process_info( process, req );
640 release_object( process );
644 /* set information about a process */
645 DECL_HANDLER(set_process_info)
647 struct process *process;
649 if ((process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION )))
651 set_process_info( process, req );
652 release_object( process );
656 /* read data from a process address space */
657 DECL_HANDLER(read_process_memory)
659 struct process *process;
661 if ((process = get_process_from_handle( req->handle, PROCESS_VM_READ )))
663 read_process_memory( process, req->addr, req->len,
664 get_req_size( req->data, sizeof(int) ), req->data );
665 release_object( process );
669 /* write data to a process address space */
670 DECL_HANDLER(write_process_memory)
672 struct process *process;
674 if ((process = get_process_from_handle( req->handle, PROCESS_VM_WRITE )))
676 write_process_memory( process, req->addr, req->len, get_req_size( req->data, sizeof(int) ),
677 req->first_mask, req->last_mask, req->data );
678 release_object( process );