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 */
74 req
->create_flags
= CREATE_NEW_CONSOLE
;
75 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
->handles
= NULL
;
147 process
->exit_code
= STILL_ACTIVE
;
148 process
->running_threads
= 0;
149 process
->priority
= NORMAL_PRIORITY_CLASS
;
150 process
->affinity
= 1;
151 process
->suspend
= 0;
152 process
->create_flags
= 0;
153 process
->console_in
= NULL
;
154 process
->console_out
= NULL
;
155 process
->init_event
= NULL
;
156 process
->info
= NULL
;
157 process
->ldt_copy
= NULL
;
158 process
->ldt_flags
= NULL
;
159 process
->exe
.next
= NULL
;
160 process
->exe
.prev
= NULL
;
161 process
->exe
.file
= NULL
;
162 process
->exe
.dbg_offset
= 0;
163 process
->exe
.dbg_size
= 0;
164 gettimeofday( &process
->start_time
, NULL
);
165 if ((process
->next
= first_process
) != NULL
) process
->next
->prev
= process
;
166 first_process
= process
;
168 /* copy the request structure */
169 if (!set_creation_info( process
, req
, cmd_line
, len
)) goto error
;
171 if (process
->info
->inherit_all
== 2) /* HACK! */
172 process
->handles
= grab_object( parent
->handles
);
173 else if (process
->info
->inherit_all
)
174 process
->handles
= copy_handle_table( process
, parent
);
176 process
->handles
= alloc_handle_table( process
, 0 );
177 if (!process
->handles
) goto error
;
179 /* retrieve the main exe file */
180 if (process
->info
->exe_file
!= -1)
182 if (!(process
->exe
.file
= get_file_obj( parent
, process
->info
->exe_file
,
183 GENERIC_READ
))) goto error
;
184 process
->info
->exe_file
= -1;
187 /* create the main thread */
188 if (!(thread
= create_thread( fd
, process
, (process
->create_flags
& CREATE_SUSPENDED
) != 0)))
191 /* create the init done event */
192 if (!(process
->init_event
= create_event( NULL
, 0, 1, 0 ))) goto error
;
194 /* set the process console */
195 if (!set_process_console( process
, parent
)) goto error
;
197 /* attach to the debugger if requested */
198 if (process
->create_flags
& (DEBUG_PROCESS
| DEBUG_ONLY_THIS_PROCESS
))
199 set_process_debugger( process
, current
);
200 else if (parent
&& parent
->debugger
&& !(parent
->create_flags
& DEBUG_ONLY_THIS_PROCESS
))
201 set_process_debugger( process
, parent
->debugger
);
203 add_process_thread( process
, thread
);
204 release_object( process
);
209 free_console( process
);
210 if (process
->handles
) release_object( process
->handles
);
211 if (thread
) release_object( thread
);
212 release_object( process
);
216 /* destroy a process when its refcount is 0 */
217 static void process_destroy( struct object
*obj
)
219 struct process
*process
= (struct process
*)obj
;
220 assert( obj
->ops
== &process_ops
);
222 /* we can't have a thread remaining */
223 assert( !process
->thread_list
);
224 if (process
->next
) process
->next
->prev
= process
->prev
;
225 if (process
->prev
) process
->prev
->next
= process
->next
;
226 else first_process
= process
->next
;
227 if (process
->info
) free( process
->info
);
228 if (process
->init_event
) release_object( process
->init_event
);
229 if (process
->exe
.file
) release_object( process
->exe
.file
);
232 /* dump a process on stdout for debugging purposes */
233 static void process_dump( struct object
*obj
, int verbose
)
235 struct process
*process
= (struct process
*)obj
;
236 assert( obj
->ops
== &process_ops
);
238 fprintf( stderr
, "Process next=%p prev=%p console=%p/%p handles=%p\n",
239 process
->next
, process
->prev
, process
->console_in
, process
->console_out
,
243 static int process_signaled( struct object
*obj
, struct thread
*thread
)
245 struct process
*process
= (struct process
*)obj
;
246 return !process
->running_threads
;
250 /* get a process from an id (and increment the refcount) */
251 struct process
*get_process_from_id( void *id
)
253 struct process
*p
= first_process
;
254 while (p
&& (p
!= id
)) p
= p
->next
;
255 if (p
) grab_object( p
);
256 else set_error( STATUS_INVALID_PARAMETER
);
260 /* get a process from a handle (and increment the refcount) */
261 struct process
*get_process_from_handle( int handle
, unsigned int access
)
263 return (struct process
*)get_handle_obj( current
->process
, handle
,
264 access
, &process_ops
);
267 /* add a dll to a process list */
268 static struct process_dll
*process_load_dll( struct process
*process
, struct file
*file
,
271 struct process_dll
*dll
;
273 /* make sure we don't already have one with the same base address */
274 for (dll
= process
->exe
.next
; dll
; dll
= dll
->next
) if (dll
->base
== base
)
276 set_error( STATUS_INVALID_PARAMETER
);
280 if ((dll
= mem_alloc( sizeof(*dll
) )))
282 dll
->prev
= &process
->exe
;
285 if (file
) dll
->file
= (struct file
*)grab_object( file
);
286 if ((dll
->next
= process
->exe
.next
)) dll
->next
->prev
= dll
;
287 process
->exe
.next
= dll
;
292 /* remove a dll from a process list */
293 static void process_unload_dll( struct process
*process
, void *base
)
295 struct process_dll
*dll
;
297 for (dll
= process
->exe
.next
; dll
; dll
= dll
->next
)
299 if (dll
->base
== base
)
301 if (dll
->file
) release_object( dll
->file
);
302 if (dll
->next
) dll
->next
->prev
= dll
->prev
;
303 if (dll
->prev
) dll
->prev
->next
= dll
->next
;
305 generate_debug_event( current
, UNLOAD_DLL_DEBUG_EVENT
, base
);
309 set_error( STATUS_INVALID_PARAMETER
);
312 /* a process has been killed (i.e. its last thread died) */
313 static void process_killed( struct process
*process
)
315 assert( !process
->thread_list
);
316 gettimeofday( &process
->end_time
, NULL
);
317 release_object( process
->handles
);
318 process
->handles
= NULL
;
319 free_console( process
);
320 while (process
->exe
.next
)
322 struct process_dll
*dll
= process
->exe
.next
;
323 process
->exe
.next
= dll
->next
;
324 if (dll
->file
) release_object( dll
->file
);
327 if (process
->exe
.file
) release_object( process
->exe
.file
);
328 process
->exe
.file
= NULL
;
329 wake_up( &process
->obj
, 0 );
330 if (!--running_processes
)
332 /* last process died, close global handles */
333 close_global_handles();
334 /* this will cause the select loop to terminate */
335 if (!persistent_server
) close_master_socket();
339 /* add a thread to a process running threads list */
340 void add_process_thread( struct process
*process
, struct thread
*thread
)
342 thread
->proc_next
= process
->thread_list
;
343 thread
->proc_prev
= NULL
;
344 if (thread
->proc_next
) thread
->proc_next
->proc_prev
= thread
;
345 process
->thread_list
= thread
;
346 if (!process
->running_threads
++) running_processes
++;
347 grab_object( thread
);
350 /* remove a thread from a process running threads list */
351 void remove_process_thread( struct process
*process
, struct thread
*thread
)
353 assert( process
->running_threads
> 0 );
354 assert( process
->thread_list
);
356 if (thread
->proc_next
) thread
->proc_next
->proc_prev
= thread
->proc_prev
;
357 if (thread
->proc_prev
) thread
->proc_prev
->proc_next
= thread
->proc_next
;
358 else process
->thread_list
= thread
->proc_next
;
360 if (!--process
->running_threads
)
362 /* we have removed the last running thread, exit the process */
363 process
->exit_code
= thread
->exit_code
;
364 generate_debug_event( thread
, EXIT_PROCESS_DEBUG_EVENT
, process
);
365 process_killed( process
);
367 else generate_debug_event( thread
, EXIT_THREAD_DEBUG_EVENT
, thread
);
368 release_object( thread
);
371 /* suspend all the threads of a process */
372 void suspend_process( struct process
*process
)
374 if (!process
->suspend
++)
376 struct thread
*thread
= process
->thread_list
;
377 for (; thread
; thread
= thread
->proc_next
)
379 if (!thread
->suspend
) stop_thread( thread
);
384 /* resume all the threads of a process */
385 void resume_process( struct process
*process
)
387 assert (process
->suspend
> 0);
388 if (!--process
->suspend
)
390 struct thread
*thread
= process
->thread_list
;
391 for (; thread
; thread
= thread
->proc_next
)
393 if (!thread
->suspend
) continue_thread( thread
);
398 /* kill a process on the spot */
399 static void kill_process( struct process
*process
, struct thread
*skip
, int exit_code
)
401 struct thread
*thread
= process
->thread_list
;
404 struct thread
*next
= thread
->proc_next
;
405 thread
->exit_code
= exit_code
;
406 if (thread
!= skip
) kill_thread( thread
, 1 );
411 /* kill all processes being debugged by a given thread */
412 void kill_debugged_processes( struct thread
*debugger
, int exit_code
)
414 for (;;) /* restart from the beginning of the list every time */
416 struct process
*process
= first_process
;
417 /* find the first process being debugged by 'debugger' and still running */
418 while (process
&& (process
->debugger
!= debugger
|| !process
->running_threads
))
419 process
= process
->next
;
420 if (!process
) return;
421 process
->debugger
= NULL
;
422 kill_process( process
, NULL
, exit_code
);
426 /* get all information about a process */
427 static void get_process_info( struct process
*process
, struct get_process_info_request
*req
)
430 req
->debugged
= (process
->debugger
!= 0);
431 req
->exit_code
= process
->exit_code
;
432 req
->priority
= process
->priority
;
433 req
->process_affinity
= process
->affinity
;
434 req
->system_affinity
= 1;
437 /* set all information about a process */
438 static void set_process_info( struct process
*process
,
439 struct set_process_info_request
*req
)
441 if (req
->mask
& SET_PROCESS_INFO_PRIORITY
)
442 process
->priority
= req
->priority
;
443 if (req
->mask
& SET_PROCESS_INFO_AFFINITY
)
445 if (req
->affinity
!= 1) set_error( STATUS_INVALID_PARAMETER
);
446 else process
->affinity
= req
->affinity
;
450 /* read data from a process memory space */
451 /* len is the total size (in ints), max is the size we can actually store in the output buffer */
452 /* we read the total size in all cases to check for permissions */
453 static void read_process_memory( struct process
*process
, const int *addr
,
454 size_t len
, size_t max
, int *dest
)
456 struct thread
*thread
= process
->thread_list
;
458 if ((unsigned int)addr
% sizeof(int)) /* address must be aligned */
460 set_error( STATUS_INVALID_PARAMETER
);
463 if (!thread
) /* process is dead */
465 set_error( STATUS_ACCESS_DENIED
);
468 if (suspend_for_ptrace( thread
))
470 while (len
> 0 && max
)
472 if (read_thread_int( thread
, addr
++, dest
++ ) == -1) goto done
;
476 /* check the rest for read permission */
479 int dummy
, page
= get_page_size() / sizeof(int);
484 if (read_thread_int( thread
, addr
- 1, &dummy
) == -1) goto done
;
486 if (len
&& (read_thread_int( thread
, addr
+ len
- 1, &dummy
) == -1)) goto done
;
489 resume_thread( thread
);
493 /* write data to a process memory space */
494 /* len is the total size (in ints), max is the size we can actually read from the input buffer */
495 /* we check the total size for write permissions */
496 static void write_process_memory( struct process
*process
, int *addr
, size_t len
,
497 size_t max
, unsigned int first_mask
,
498 unsigned int last_mask
, const int *src
)
500 struct thread
*thread
= process
->thread_list
;
502 if (!len
|| ((unsigned int)addr
% sizeof(int))) /* address must be aligned */
504 set_error( STATUS_INVALID_PARAMETER
);
507 if (!thread
) /* process is dead */
509 set_error( STATUS_ACCESS_DENIED
);
512 if (suspend_for_ptrace( thread
))
514 /* first word is special */
517 if (write_thread_int( thread
, addr
++, *src
++, first_mask
) == -1) goto done
;
521 else last_mask
&= first_mask
;
523 while (len
> 1 && max
)
525 if (write_thread_int( thread
, addr
++, *src
++, ~0 ) == -1) goto done
;
532 /* last word is special too */
533 if (write_thread_int( thread
, addr
, *src
, last_mask
) == -1) goto done
;
537 /* check the rest for write permission */
538 int page
= get_page_size() / sizeof(int);
543 if (write_thread_int( thread
, addr
- 1, 0, 0 ) == -1) goto done
;
545 if (len
&& (write_thread_int( thread
, addr
+ len
- 1, 0, 0 ) == -1)) goto done
;
548 resume_thread( thread
);
552 /* take a snapshot of currently running processes */
553 struct process_snapshot
*process_snap( int *count
)
555 struct process_snapshot
*snapshot
, *ptr
;
556 struct process
*process
;
557 if (!running_processes
) return NULL
;
558 if (!(snapshot
= mem_alloc( sizeof(*snapshot
) * running_processes
)))
561 for (process
= first_process
; process
; process
= process
->next
)
563 if (!process
->running_threads
) continue;
564 ptr
->process
= process
;
565 ptr
->threads
= process
->running_threads
;
566 ptr
->count
= process
->obj
.refcount
;
567 ptr
->priority
= process
->priority
;
568 grab_object( process
);
571 *count
= running_processes
;
575 /* take a snapshot of the modules of a process */
576 struct module_snapshot
*module_snap( struct process
*process
, int *count
)
578 struct module_snapshot
*snapshot
, *ptr
;
579 struct process_dll
*dll
;
582 for (dll
= &process
->exe
; dll
; dll
= dll
->next
) total
++;
583 if (!(snapshot
= mem_alloc( sizeof(*snapshot
) * total
))) return NULL
;
585 for (ptr
= snapshot
, dll
= &process
->exe
; dll
; dll
= dll
->next
, ptr
++)
587 ptr
->base
= dll
->base
;
594 /* create a new process */
595 DECL_HANDLER(new_process
)
597 size_t len
= get_req_strlen( req
, req
->cmdline
);
598 struct thread
*thread
;
600 int event
= -1, phandle
= -1;
608 if (socketpair( AF_UNIX
, SOCK_STREAM
, 0, sock
) == -1)
614 if (!(thread
= create_process( sock
[0], current
->process
, req
, req
->cmdline
, len
)))
617 if ((event
= alloc_handle( current
->process
, thread
->process
->init_event
,
618 EVENT_ALL_ACCESS
, 0 )) == -1)
621 if ((phandle
= alloc_handle( current
->process
, thread
->process
,
622 PROCESS_ALL_ACCESS
, req
->pinherit
)) == -1)
625 if ((req
->thandle
= alloc_handle( current
->process
, thread
,
626 THREAD_ALL_ACCESS
, req
->tinherit
)) == -1)
629 /* thread object will be released when the thread gets killed */
630 set_reply_fd( current
, sock
[1] );
631 req
->pid
= get_process_id( thread
->process
);
632 req
->tid
= get_thread_id( thread
);
633 req
->phandle
= phandle
;
638 if (phandle
!= -1) close_handle( current
->process
, phandle
);
639 if (event
!= -1) close_handle( current
->process
, event
);
640 if (thread
) release_object( thread
);
644 /* initialize a new process */
645 DECL_HANDLER(init_process
)
647 struct new_process_request
*info
;
649 if (!current
->unix_pid
)
651 fatal_protocol_error( current
, "init_process: init_thread not called yet\n" );
654 if (!(info
= current
->process
->info
))
656 fatal_protocol_error( current
, "init_process: called twice\n" );
659 current
->process
->ldt_copy
= req
->ldt_copy
;
660 current
->process
->ldt_flags
= req
->ldt_flags
;
661 current
->process
->info
= NULL
;
663 req
->start_flags
= info
->start_flags
;
664 req
->hstdin
= info
->hstdin
;
665 req
->hstdout
= info
->hstdout
;
666 req
->hstderr
= info
->hstderr
;
667 req
->cmd_show
= info
->cmd_show
;
668 req
->env_ptr
= info
->env_ptr
;
669 strcpy( req
->cmdline
, info
->cmdline
);
670 if (current
->process
->exe
.file
)
671 req
->exe_file
= alloc_handle( current
->process
, current
->process
->exe
.file
,
676 /* signal the end of the process initialization */
677 DECL_HANDLER(init_process_done
)
679 struct process
*process
= current
->process
;
680 if (!process
->init_event
)
682 fatal_protocol_error( current
, "init_process_done: no event\n" );
685 process
->exe
.base
= req
->module
;
686 generate_startup_debug_events( current
->process
, req
->entry
);
687 set_event( process
->init_event
);
688 release_object( process
->init_event
);
689 process
->init_event
= NULL
;
690 if (current
->suspend
+ current
->process
->suspend
> 0) stop_thread( current
);
691 req
->debugged
= (current
->process
->debugger
!= 0);
694 /* open a handle to a process */
695 DECL_HANDLER(open_process
)
697 struct process
*process
= get_process_from_id( req
->pid
);
701 req
->handle
= alloc_handle( current
->process
, process
, req
->access
, req
->inherit
);
702 release_object( process
);
706 /* terminate a process */
707 DECL_HANDLER(terminate_process
)
709 struct process
*process
;
711 if ((process
= get_process_from_handle( req
->handle
, PROCESS_TERMINATE
)))
713 req
->self
= (current
->process
== process
);
714 kill_process( process
, current
, req
->exit_code
);
715 release_object( process
);
719 /* fetch information about a process */
720 DECL_HANDLER(get_process_info
)
722 struct process
*process
;
724 if ((process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_INFORMATION
)))
726 get_process_info( process
, req
);
727 release_object( process
);
731 /* set information about a process */
732 DECL_HANDLER(set_process_info
)
734 struct process
*process
;
736 if ((process
= get_process_from_handle( req
->handle
, PROCESS_SET_INFORMATION
)))
738 set_process_info( process
, req
);
739 release_object( process
);
743 /* read data from a process address space */
744 DECL_HANDLER(read_process_memory
)
746 struct process
*process
;
748 if ((process
= get_process_from_handle( req
->handle
, PROCESS_VM_READ
)))
750 read_process_memory( process
, req
->addr
, req
->len
,
751 get_req_size( req
, req
->data
, sizeof(int) ), req
->data
);
752 release_object( process
);
756 /* write data to a process address space */
757 DECL_HANDLER(write_process_memory
)
759 struct process
*process
;
761 if ((process
= get_process_from_handle( req
->handle
, PROCESS_VM_WRITE
)))
763 write_process_memory( process
, req
->addr
, req
->len
,
764 get_req_size( req
, req
->data
, sizeof(int) ),
765 req
->first_mask
, req
->last_mask
, req
->data
);
766 release_object( process
);
770 /* notify the server that a dll has been loaded */
771 DECL_HANDLER(load_dll
)
773 struct process_dll
*dll
;
774 struct file
*file
= NULL
;
776 if ((req
->handle
!= -1) &&
777 !(file
= get_file_obj( current
->process
, req
->handle
, GENERIC_READ
))) return;
779 if ((dll
= process_load_dll( current
->process
, file
, req
->base
)))
781 dll
->dbg_offset
= req
->dbg_offset
;
782 dll
->dbg_size
= req
->dbg_size
;
783 dll
->name
= req
->name
;
784 /* only generate event if initialization is done */
785 if (!current
->process
->init_event
)
786 generate_debug_event( current
, LOAD_DLL_DEBUG_EVENT
, dll
);
788 if (file
) release_object( file
);
791 /* notify the server that a dll is being unloaded */
792 DECL_HANDLER(unload_dll
)
794 process_unload_dll( current
->process
, req
->base
);