2 * Server-side process management
4 * Copyright (C) 1998 Alexandre Julliard
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.
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.
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
22 #include "wine/port.h"
31 #ifdef HAVE_SYS_SOCKET_H
32 # include <sys/socket.h>
46 /* process structure */
48 static struct process
*first_process
;
49 static int running_processes
;
51 /* process operations */
53 static void process_dump( struct object
*obj
, int verbose
);
54 static int process_signaled( struct object
*obj
, struct thread
*thread
);
55 static void process_poll_event( struct fd
*fd
, int event
);
56 static void process_destroy( struct object
*obj
);
58 static const struct object_ops process_ops
=
60 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 */
66 no_get_fd
, /* get_fd */
67 process_destroy
/* destroy */
70 static const struct fd_ops process_fd_ops
=
72 NULL
, /* get_poll_events */
73 process_poll_event
, /* poll_event */
75 no_get_file_info
, /* get_file_info */
76 no_queue_async
/* queue_async */
79 /* process startup info */
83 struct object obj
; /* object header */
84 struct list entry
; /* entry in list of startup infos */
85 int inherit_all
; /* inherit all handles from parent */
86 int create_flags
; /* creation flags */
87 int unix_pid
; /* Unix pid of new process */
88 obj_handle_t hstdin
; /* handle for stdin */
89 obj_handle_t hstdout
; /* handle for stdout */
90 obj_handle_t hstderr
; /* handle for stderr */
91 struct file
*exe_file
; /* file handle for main exe */
92 struct thread
*owner
; /* owner thread (the one that created the new process) */
93 struct process
*process
; /* created process */
94 struct thread
*thread
; /* created thread */
95 size_t data_size
; /* size of startup data */
96 startup_info_t
*data
; /* data for startup info */
99 static void startup_info_dump( struct object
*obj
, int verbose
);
100 static int startup_info_signaled( struct object
*obj
, struct thread
*thread
);
101 static void startup_info_destroy( struct object
*obj
);
103 static const struct object_ops startup_info_ops
=
105 sizeof(struct startup_info
), /* size */
106 startup_info_dump
, /* dump */
107 add_queue
, /* add_queue */
108 remove_queue
, /* remove_queue */
109 startup_info_signaled
, /* signaled */
110 no_satisfied
, /* satisfied */
111 no_get_fd
, /* get_fd */
112 startup_info_destroy
/* destroy */
116 static struct list startup_info_list
= LIST_INIT(startup_info_list
);
120 void *ptr
; /* entry ptr */
121 unsigned int next
; /* next free entry */
124 static struct ptid_entry
*ptid_entries
; /* array of ptid entries */
125 static unsigned int used_ptid_entries
; /* number of entries in use */
126 static unsigned int alloc_ptid_entries
; /* number of allocated entries */
127 static unsigned int next_free_ptid
; /* next free entry */
128 static unsigned int last_free_ptid
; /* last free entry */
130 #define PTID_OFFSET 8 /* offset for first ptid value */
132 /* allocate a new process or thread id */
133 unsigned int alloc_ptid( void *ptr
)
135 struct ptid_entry
*entry
;
138 if (used_ptid_entries
< alloc_ptid_entries
)
140 id
= used_ptid_entries
+ PTID_OFFSET
;
141 entry
= &ptid_entries
[used_ptid_entries
++];
143 else if (next_free_ptid
)
146 entry
= &ptid_entries
[id
- PTID_OFFSET
];
147 if (!(next_free_ptid
= entry
->next
)) last_free_ptid
= 0;
149 else /* need to grow the array */
151 unsigned int count
= alloc_ptid_entries
+ (alloc_ptid_entries
/ 2);
152 if (!count
) count
= 64;
153 if (!(entry
= realloc( ptid_entries
, count
* sizeof(*entry
) )))
155 set_error( STATUS_NO_MEMORY
);
158 ptid_entries
= entry
;
159 alloc_ptid_entries
= count
;
160 id
= used_ptid_entries
+ PTID_OFFSET
;
161 entry
= &ptid_entries
[used_ptid_entries
++];
168 /* free a process or thread id */
169 void free_ptid( unsigned int id
)
171 struct ptid_entry
*entry
= &ptid_entries
[id
- PTID_OFFSET
];
176 /* append to end of free list so that we don't reuse it too early */
177 if (last_free_ptid
) ptid_entries
[last_free_ptid
- PTID_OFFSET
].next
= id
;
178 else next_free_ptid
= id
;
183 /* retrieve the pointer corresponding to a process or thread id */
184 void *get_ptid_entry( unsigned int id
)
186 if (id
< PTID_OFFSET
) return NULL
;
187 if (id
- PTID_OFFSET
>= used_ptid_entries
) return NULL
;
188 return ptid_entries
[id
- PTID_OFFSET
].ptr
;
191 /* set the state of the process startup info */
192 static void set_process_startup_state( struct process
*process
, enum startup_state state
)
194 if (process
->startup_state
== STARTUP_IN_PROGRESS
) process
->startup_state
= state
;
195 if (process
->startup_info
)
197 wake_up( &process
->startup_info
->obj
, 0 );
198 release_object( process
->startup_info
);
199 process
->startup_info
= NULL
;
203 /* set the console and stdio handles for a newly created process */
204 static int set_process_console( struct process
*process
, struct thread
*parent_thread
,
205 struct startup_info
*info
, struct init_process_reply
*reply
)
207 if (process
->create_flags
& CREATE_NEW_CONSOLE
)
209 /* let the process init do the allocation */
212 else if (info
&& !(process
->create_flags
& DETACHED_PROCESS
))
214 /* FIXME: some better error checking should be done...
215 * like if hConOut and hConIn are console handles, then they should be on the same
218 inherit_console( parent_thread
, process
, info
->inherit_all
? info
->hstdin
: 0 );
222 if (!info
->inherit_all
)
224 reply
->hstdin
= duplicate_handle( parent_thread
->process
, info
->hstdin
, process
,
225 0, TRUE
, DUPLICATE_SAME_ACCESS
);
226 reply
->hstdout
= duplicate_handle( parent_thread
->process
, info
->hstdout
, process
,
227 0, TRUE
, DUPLICATE_SAME_ACCESS
);
228 reply
->hstderr
= duplicate_handle( parent_thread
->process
, info
->hstderr
, process
,
229 0, TRUE
, DUPLICATE_SAME_ACCESS
);
233 reply
->hstdin
= info
->hstdin
;
234 reply
->hstdout
= info
->hstdout
;
235 reply
->hstderr
= info
->hstderr
;
238 else reply
->hstdin
= reply
->hstdout
= reply
->hstderr
= 0;
239 /* some handles above may have been invalid; this is not an error */
240 if (get_error() == STATUS_INVALID_HANDLE
||
241 get_error() == STATUS_OBJECT_TYPE_MISMATCH
) clear_error();
245 /* create a new process and its main thread */
246 struct thread
*create_process( int fd
)
248 struct process
*process
;
249 struct thread
*thread
= NULL
;
252 if (!(process
= alloc_object( &process_ops
))) goto error
;
253 process
->next
= NULL
;
254 process
->prev
= NULL
;
255 process
->parent
= NULL
;
256 process
->thread_list
= NULL
;
257 process
->debugger
= NULL
;
258 process
->handles
= NULL
;
259 process
->msg_fd
= NULL
;
260 process
->exit_code
= STILL_ACTIVE
;
261 process
->running_threads
= 0;
262 process
->priority
= NORMAL_PRIORITY_CLASS
;
263 process
->affinity
= 1;
264 process
->suspend
= 0;
265 process
->create_flags
= 0;
266 process
->console
= NULL
;
267 process
->startup_state
= STARTUP_IN_PROGRESS
;
268 process
->startup_info
= NULL
;
269 process
->idle_event
= NULL
;
270 process
->queue
= NULL
;
271 process
->atom_table
= NULL
;
272 process
->ldt_copy
= NULL
;
273 process
->exe
.next
= NULL
;
274 process
->exe
.prev
= NULL
;
275 process
->exe
.file
= NULL
;
276 process
->exe
.dbg_offset
= 0;
277 process
->exe
.dbg_size
= 0;
278 process
->exe
.namelen
= 0;
279 process
->exe
.filename
= NULL
;
280 process
->group_id
= 0;
281 process
->token
= create_token();
282 list_init( &process
->locks
);
284 gettimeofday( &process
->start_time
, NULL
);
285 if ((process
->next
= first_process
) != NULL
) process
->next
->prev
= process
;
286 first_process
= process
;
288 if (!(process
->id
= alloc_ptid( process
))) goto error
;
289 if (!(process
->msg_fd
= create_anonymous_fd( &process_fd_ops
, fd
, &process
->obj
))) goto error
;
291 /* create the main thread */
292 if (pipe( request_pipe
) == -1)
297 if (send_client_fd( process
, request_pipe
[1], 0 ) == -1)
299 close( request_pipe
[0] );
300 close( request_pipe
[1] );
303 close( request_pipe
[1] );
304 if (!(thread
= create_thread( request_pipe
[0], process
))) goto error
;
306 set_fd_events( process
->msg_fd
, POLLIN
); /* start listening to events */
307 release_object( process
);
311 if (process
) release_object( process
);
312 /* if we failed to start our first process, close everything down */
313 if (!running_processes
) close_master_socket();
317 /* find the startup info for a given Unix process */
318 inline static struct startup_info
*find_startup_info( int unix_pid
)
322 LIST_FOR_EACH( ptr
, &startup_info_list
)
324 struct startup_info
*info
= LIST_ENTRY( ptr
, struct startup_info
, entry
);
325 if (info
->unix_pid
== unix_pid
) return info
;
330 /* initialize the current process and fill in the request */
331 static struct startup_info
*init_process( struct init_process_reply
*reply
)
333 struct process
*process
= current
->process
;
334 struct thread
*parent_thread
= NULL
;
335 struct process
*parent
= NULL
;
336 struct startup_info
*info
= find_startup_info( current
->unix_pid
);
342 fatal_protocol_error( current
, "init_process: called twice?\n" );
345 parent_thread
= info
->owner
;
346 parent
= parent_thread
->process
;
347 process
->parent
= (struct process
*)grab_object( parent
);
350 /* set the process flags */
351 process
->create_flags
= info
? info
->create_flags
: 0;
353 /* create the handle table */
354 if (info
&& info
->inherit_all
)
355 process
->handles
= copy_handle_table( process
, parent
);
357 process
->handles
= alloc_handle_table( process
, 0 );
358 if (!process
->handles
) return NULL
;
360 /* retrieve the main exe file */
362 if (info
&& info
->exe_file
)
364 process
->exe
.file
= (struct file
*)grab_object( info
->exe_file
);
365 if (!(reply
->exe_file
= alloc_handle( process
, process
->exe
.file
, GENERIC_READ
, 0 )))
369 /* set the process console */
370 if (!set_process_console( process
, parent_thread
, info
, reply
)) return NULL
;
372 process
->group_id
= get_process_id( process
);
375 /* attach to the debugger if requested */
376 if (process
->create_flags
& (DEBUG_PROCESS
| DEBUG_ONLY_THIS_PROCESS
))
377 set_process_debugger( process
, parent_thread
);
378 else if (parent
->debugger
&& !(parent
->create_flags
& DEBUG_ONLY_THIS_PROCESS
))
379 set_process_debugger( process
, parent
->debugger
);
380 if (!(process
->create_flags
& CREATE_NEW_PROCESS_GROUP
))
381 process
->group_id
= parent
->group_id
;
384 /* thread will be actually suspended in init_done */
385 if (process
->create_flags
& CREATE_SUSPENDED
) current
->suspend
++;
389 reply
->info_size
= info
->data_size
;
390 info
->process
= (struct process
*)grab_object( process
);
391 info
->thread
= (struct thread
*)grab_object( current
);
393 reply
->create_flags
= process
->create_flags
;
394 reply
->server_start
= server_start_ticks
;
395 return info
? (struct startup_info
*)grab_object( info
) : NULL
;
398 /* destroy a process when its refcount is 0 */
399 static void process_destroy( struct object
*obj
)
401 struct process
*process
= (struct process
*)obj
;
402 assert( obj
->ops
== &process_ops
);
404 /* we can't have a thread remaining */
405 assert( !process
->thread_list
);
407 set_process_startup_state( process
, STARTUP_ABORTED
);
408 if (process
->console
) release_object( process
->console
);
409 if (process
->parent
) release_object( process
->parent
);
410 if (process
->msg_fd
) release_object( process
->msg_fd
);
411 if (process
->next
) process
->next
->prev
= process
->prev
;
412 if (process
->prev
) process
->prev
->next
= process
->next
;
413 else first_process
= process
->next
;
414 if (process
->idle_event
) release_object( process
->idle_event
);
415 if (process
->queue
) release_object( process
->queue
);
416 if (process
->atom_table
) release_object( process
->atom_table
);
417 if (process
->exe
.file
) release_object( process
->exe
.file
);
418 if (process
->exe
.filename
) free( process
->exe
.filename
);
419 if (process
->id
) free_ptid( process
->id
);
420 if (process
->token
) release_object( process
->token
);
423 /* dump a process on stdout for debugging purposes */
424 static void process_dump( struct object
*obj
, int verbose
)
426 struct process
*process
= (struct process
*)obj
;
427 assert( obj
->ops
== &process_ops
);
429 fprintf( stderr
, "Process id=%04x next=%p prev=%p handles=%p\n",
430 process
->id
, process
->next
, process
->prev
, process
->handles
);
433 static int process_signaled( struct object
*obj
, struct thread
*thread
)
435 struct process
*process
= (struct process
*)obj
;
436 return !process
->running_threads
;
439 static void process_poll_event( struct fd
*fd
, int event
)
441 struct process
*process
= get_fd_user( fd
);
442 assert( process
->obj
.ops
== &process_ops
);
444 if (event
& (POLLERR
| POLLHUP
)) set_fd_events( fd
, -1 );
445 else if (event
& POLLIN
) receive_fd( process
);
448 static void startup_info_destroy( struct object
*obj
)
450 struct startup_info
*info
= (struct startup_info
*)obj
;
451 assert( obj
->ops
== &startup_info_ops
);
452 list_remove( &info
->entry
);
453 if (info
->data
) free( info
->data
);
454 if (info
->exe_file
) release_object( info
->exe_file
);
455 if (info
->process
) release_object( info
->process
);
456 if (info
->thread
) release_object( info
->thread
);
457 if (info
->owner
) release_object( info
->owner
);
460 static void startup_info_dump( struct object
*obj
, int verbose
)
462 struct startup_info
*info
= (struct startup_info
*)obj
;
463 assert( obj
->ops
== &startup_info_ops
);
465 fprintf( stderr
, "Startup info flags=%x in=%p out=%p err=%p\n",
466 info
->create_flags
, info
->hstdin
, info
->hstdout
, info
->hstderr
);
469 static int startup_info_signaled( struct object
*obj
, struct thread
*thread
)
471 struct startup_info
*info
= (struct startup_info
*)obj
;
472 return info
->process
&& is_process_init_done(info
->process
);
475 /* get a process from an id (and increment the refcount) */
476 struct process
*get_process_from_id( process_id_t id
)
478 struct object
*obj
= get_ptid_entry( id
);
480 if (obj
&& obj
->ops
== &process_ops
) return (struct process
*)grab_object( obj
);
481 set_error( STATUS_INVALID_PARAMETER
);
485 /* get a process from a handle (and increment the refcount) */
486 struct process
*get_process_from_handle( obj_handle_t handle
, unsigned int access
)
488 return (struct process
*)get_handle_obj( current
->process
, handle
,
489 access
, &process_ops
);
492 /* add a dll to a process list */
493 static struct process_dll
*process_load_dll( struct process
*process
, struct file
*file
,
494 void *base
, const char *filename
, size_t name_len
)
496 struct process_dll
*dll
;
498 /* make sure we don't already have one with the same base address */
499 for (dll
= process
->exe
.next
; dll
; dll
= dll
->next
) if (dll
->base
== base
)
501 set_error( STATUS_INVALID_PARAMETER
);
505 if ((dll
= mem_alloc( sizeof(*dll
) )))
507 dll
->prev
= &process
->exe
;
510 dll
->filename
= NULL
;
511 dll
->namelen
= name_len
;
512 if (name_len
&& !(dll
->filename
= memdup( filename
, name_len
)))
517 if (file
) dll
->file
= (struct file
*)grab_object( file
);
518 if ((dll
->next
= process
->exe
.next
)) dll
->next
->prev
= dll
;
519 process
->exe
.next
= dll
;
524 /* remove a dll from a process list */
525 static void process_unload_dll( struct process
*process
, void *base
)
527 struct process_dll
*dll
;
529 for (dll
= process
->exe
.next
; dll
; dll
= dll
->next
)
531 if (dll
->base
== base
)
533 if (dll
->file
) release_object( dll
->file
);
534 if (dll
->next
) dll
->next
->prev
= dll
->prev
;
535 if (dll
->prev
) dll
->prev
->next
= dll
->next
;
536 if (dll
->filename
) free( dll
->filename
);
538 generate_debug_event( current
, UNLOAD_DLL_DEBUG_EVENT
, base
);
542 set_error( STATUS_INVALID_PARAMETER
);
545 /* kill all processes */
546 void kill_all_processes( struct process
*skip
, int exit_code
)
550 struct process
*process
= first_process
;
552 while (process
&& (!process
->running_threads
|| process
== skip
))
553 process
= process
->next
;
555 kill_process( process
, NULL
, exit_code
);
559 /* kill all processes being attached to a console renderer */
560 void kill_console_processes( struct thread
*renderer
, int exit_code
)
562 for (;;) /* restart from the beginning of the list every time */
564 struct process
*process
= first_process
;
566 /* find the first process being attached to 'renderer' and still running */
568 (process
== renderer
->process
|| !process
->console
||
569 process
->console
->renderer
!= renderer
|| !process
->running_threads
))
571 process
= process
->next
;
574 kill_process( process
, NULL
, exit_code
);
578 /* a process has been killed (i.e. its last thread died) */
579 static void process_killed( struct process
*process
)
581 assert( !process
->thread_list
);
582 gettimeofday( &process
->end_time
, NULL
);
583 if (process
->handles
) release_object( process
->handles
);
584 process
->handles
= NULL
;
586 /* close the console attached to this process, if any */
587 free_console( process
);
589 while (process
->exe
.next
)
591 struct process_dll
*dll
= process
->exe
.next
;
592 process
->exe
.next
= dll
->next
;
593 if (dll
->file
) release_object( dll
->file
);
594 if (dll
->filename
) free( dll
->filename
);
597 set_process_startup_state( process
, STARTUP_ABORTED
);
598 if (process
->exe
.file
) release_object( process
->exe
.file
);
599 process
->exe
.file
= NULL
;
600 wake_up( &process
->obj
, 0 );
601 if (!--running_processes
) close_master_socket();
604 /* add a thread to a process running threads list */
605 void add_process_thread( struct process
*process
, struct thread
*thread
)
607 thread
->proc_next
= process
->thread_list
;
608 thread
->proc_prev
= NULL
;
609 if (thread
->proc_next
) thread
->proc_next
->proc_prev
= thread
;
610 process
->thread_list
= thread
;
611 if (!process
->running_threads
++) running_processes
++;
612 grab_object( thread
);
615 /* remove a thread from a process running threads list */
616 void remove_process_thread( struct process
*process
, struct thread
*thread
)
618 assert( process
->running_threads
> 0 );
619 assert( process
->thread_list
);
621 if (thread
->proc_next
) thread
->proc_next
->proc_prev
= thread
->proc_prev
;
622 if (thread
->proc_prev
) thread
->proc_prev
->proc_next
= thread
->proc_next
;
623 else process
->thread_list
= thread
->proc_next
;
625 if (!--process
->running_threads
)
627 /* we have removed the last running thread, exit the process */
628 process
->exit_code
= thread
->exit_code
;
629 generate_debug_event( thread
, EXIT_PROCESS_DEBUG_EVENT
, process
);
630 process_killed( process
);
632 else generate_debug_event( thread
, EXIT_THREAD_DEBUG_EVENT
, thread
);
633 release_object( thread
);
636 /* suspend all the threads of a process */
637 void suspend_process( struct process
*process
)
639 if (!process
->suspend
++)
641 struct thread
*thread
= process
->thread_list
;
644 struct thread
*next
= thread
->proc_next
;
645 if (!thread
->suspend
) stop_thread( thread
);
651 /* resume all the threads of a process */
652 void resume_process( struct process
*process
)
654 assert (process
->suspend
> 0);
655 if (!--process
->suspend
)
657 struct thread
*thread
= process
->thread_list
;
660 struct thread
*next
= thread
->proc_next
;
661 if (!thread
->suspend
) wake_thread( thread
);
667 /* kill a process on the spot */
668 void kill_process( struct process
*process
, struct thread
*skip
, int exit_code
)
670 struct thread
*thread
= process
->thread_list
;
674 struct thread
*next
= thread
->proc_next
;
675 thread
->exit_code
= exit_code
;
676 if (thread
!= skip
) kill_thread( thread
, 1 );
681 /* kill all processes being debugged by a given thread */
682 void kill_debugged_processes( struct thread
*debugger
, int exit_code
)
684 for (;;) /* restart from the beginning of the list every time */
686 struct process
*process
= first_process
;
687 /* find the first process being debugged by 'debugger' and still running */
688 while (process
&& (process
->debugger
!= debugger
|| !process
->running_threads
))
689 process
= process
->next
;
690 if (!process
) return;
691 process
->debugger
= NULL
;
692 kill_process( process
, NULL
, exit_code
);
697 /* detach a debugger from all its debuggees */
698 void detach_debugged_processes( struct thread
*debugger
)
700 struct process
*process
;
701 for (process
= first_process
; process
; process
= process
->next
)
703 if (process
->debugger
== debugger
&& process
->running_threads
)
705 debugger_detach( process
, debugger
);
711 void enum_processes( int (*cb
)(struct process
*, void*), void *user
)
713 struct process
*process
;
714 for (process
= first_process
; process
; process
= process
->next
)
716 if ((cb
)(process
, user
)) break;
721 /* get all information about a process */
722 static void get_process_info( struct process
*process
, struct get_process_info_reply
*reply
)
724 reply
->pid
= get_process_id( process
);
725 reply
->debugged
= (process
->debugger
!= 0);
726 reply
->exit_code
= process
->exit_code
;
727 reply
->priority
= process
->priority
;
728 reply
->process_affinity
= process
->affinity
;
729 reply
->system_affinity
= 1;
732 /* set all information about a process */
733 static void set_process_info( struct process
*process
,
734 const struct set_process_info_request
*req
)
736 if (req
->mask
& SET_PROCESS_INFO_PRIORITY
)
737 process
->priority
= req
->priority
;
738 if (req
->mask
& SET_PROCESS_INFO_AFFINITY
)
740 if (req
->affinity
!= 1) set_error( STATUS_INVALID_PARAMETER
);
741 else process
->affinity
= req
->affinity
;
745 /* read data from a process memory space */
746 /* len is the total size (in ints) */
747 static int read_process_memory( struct process
*process
, const int *addr
, size_t len
, int *dest
)
749 struct thread
*thread
= process
->thread_list
;
751 assert( !((unsigned int)addr
% sizeof(int)) ); /* address must be aligned */
753 if (!thread
) /* process is dead */
755 set_error( STATUS_ACCESS_DENIED
);
758 if (suspend_for_ptrace( thread
))
762 if (read_thread_int( thread
, addr
++, dest
++ ) == -1) break;
765 resume_after_ptrace( thread
);
770 /* make sure we can write to the whole address range */
771 /* len is the total size (in ints) */
772 static int check_process_write_access( struct thread
*thread
, int *addr
, size_t len
)
774 int page
= get_page_size() / sizeof(int);
778 if (write_thread_int( thread
, addr
, 0, 0 ) == -1) return 0;
779 if (len
<= page
) break;
783 return (write_thread_int( thread
, addr
+ len
- 1, 0, 0 ) != -1);
786 /* write data to a process memory space */
787 /* len is the total size (in ints), max is the size we can actually read from the input buffer */
788 /* we check the total size for write permissions */
789 static void write_process_memory( struct process
*process
, int *addr
, size_t len
,
790 unsigned int first_mask
, unsigned int last_mask
, const int *src
)
792 struct thread
*thread
= process
->thread_list
;
794 assert( !((unsigned int)addr
% sizeof(int) )); /* address must be aligned */
796 if (!thread
) /* process is dead */
798 set_error( STATUS_ACCESS_DENIED
);
801 if (suspend_for_ptrace( thread
))
803 if (!check_process_write_access( thread
, addr
, len
))
805 set_error( STATUS_ACCESS_DENIED
);
808 /* first word is special */
811 if (write_thread_int( thread
, addr
++, *src
++, first_mask
) == -1) goto done
;
814 else last_mask
&= first_mask
;
818 if (write_thread_int( thread
, addr
++, *src
++, ~0 ) == -1) goto done
;
822 /* last word is special too */
823 if (write_thread_int( thread
, addr
, *src
, last_mask
) == -1) goto done
;
826 resume_after_ptrace( thread
);
830 /* take a snapshot of currently running processes */
831 struct process_snapshot
*process_snap( int *count
)
833 struct process_snapshot
*snapshot
, *ptr
;
834 struct process
*process
;
835 if (!running_processes
) return NULL
;
836 if (!(snapshot
= mem_alloc( sizeof(*snapshot
) * running_processes
)))
839 for (process
= first_process
; process
; process
= process
->next
)
841 if (!process
->running_threads
) continue;
842 ptr
->process
= process
;
843 ptr
->threads
= process
->running_threads
;
844 ptr
->count
= process
->obj
.refcount
;
845 ptr
->priority
= process
->priority
;
846 grab_object( process
);
849 *count
= running_processes
;
853 /* take a snapshot of the modules of a process */
854 struct module_snapshot
*module_snap( struct process
*process
, int *count
)
856 struct module_snapshot
*snapshot
, *ptr
;
857 struct process_dll
*dll
;
860 for (dll
= &process
->exe
; dll
; dll
= dll
->next
) total
++;
861 if (!(snapshot
= mem_alloc( sizeof(*snapshot
) * total
))) return NULL
;
863 for (ptr
= snapshot
, dll
= &process
->exe
; dll
; dll
= dll
->next
, ptr
++)
865 ptr
->base
= dll
->base
;
866 ptr
->size
= dll
->size
;
867 ptr
->namelen
= dll
->namelen
;
868 ptr
->filename
= memdup( dll
->filename
, dll
->namelen
);
875 /* create a new process */
876 DECL_HANDLER(new_process
)
878 struct startup_info
*info
;
880 /* build the startup info for a new process */
881 if (!(info
= alloc_object( &startup_info_ops
))) return;
882 list_add_head( &startup_info_list
, &info
->entry
);
883 info
->inherit_all
= req
->inherit_all
;
884 info
->create_flags
= req
->create_flags
;
885 info
->unix_pid
= req
->unix_pid
;
886 info
->hstdin
= req
->hstdin
;
887 info
->hstdout
= req
->hstdout
;
888 info
->hstderr
= req
->hstderr
;
889 info
->exe_file
= NULL
;
890 info
->owner
= (struct thread
*)grab_object( current
);
891 info
->process
= NULL
;
893 info
->data_size
= get_req_data_size();
897 !(info
->exe_file
= get_file_obj( current
->process
, req
->exe_file
, GENERIC_READ
)))
900 if (!(info
->data
= mem_alloc( info
->data_size
))) goto done
;
901 memcpy( info
->data
, get_req_data(), info
->data_size
);
902 reply
->info
= alloc_handle( current
->process
, info
, SYNCHRONIZE
, FALSE
);
905 release_object( info
);
908 /* Retrieve information about a newly started process */
909 DECL_HANDLER(get_new_process_info
)
911 struct startup_info
*info
;
913 if ((info
= (struct startup_info
*)get_handle_obj( current
->process
, req
->info
,
914 0, &startup_info_ops
)))
916 reply
->pid
= get_process_id( info
->process
);
917 reply
->tid
= get_thread_id( info
->thread
);
918 reply
->phandle
= alloc_handle( current
->process
, info
->process
,
919 PROCESS_ALL_ACCESS
, req
->pinherit
);
920 reply
->thandle
= alloc_handle( current
->process
, info
->thread
,
921 THREAD_ALL_ACCESS
, req
->tinherit
);
922 reply
->success
= is_process_init_done( info
->process
);
923 release_object( info
);
935 /* Retrieve the new process startup info */
936 DECL_HANDLER(get_startup_info
)
938 struct startup_info
*info
;
940 if ((info
= current
->process
->startup_info
))
942 size_t size
= info
->data_size
;
943 if (size
> get_reply_max_size()) size
= get_reply_max_size();
945 /* we return the data directly without making a copy so this can only be called once */
946 set_reply_data_ptr( info
->data
, size
);
953 /* initialize a new process */
954 DECL_HANDLER(init_process
)
956 if (current
->unix_pid
== -1)
958 fatal_protocol_error( current
, "init_process: init_thread not called yet\n" );
961 if (current
->process
->startup_info
)
963 fatal_protocol_error( current
, "init_process: called twice\n" );
966 reply
->info_size
= 0;
967 current
->process
->ldt_copy
= req
->ldt_copy
;
968 current
->process
->startup_info
= init_process( reply
);
971 /* signal the end of the process initialization */
972 DECL_HANDLER(init_process_done
)
974 struct file
*file
= NULL
;
975 struct process
*process
= current
->process
;
977 if (is_process_init_done(process
))
979 fatal_protocol_error( current
, "init_process_done: called twice\n" );
984 fatal_protocol_error( current
, "init_process_done: module base address cannot be 0\n" );
987 process
->exe
.base
= req
->module
;
988 process
->exe
.size
= req
->module_size
;
989 process
->exe
.name
= req
->name
;
991 if (req
->exe_file
) file
= get_file_obj( process
, req
->exe_file
, GENERIC_READ
);
992 if (process
->exe
.file
) release_object( process
->exe
.file
);
993 process
->exe
.file
= file
;
995 if ((process
->exe
.namelen
= get_req_data_size()))
996 process
->exe
.filename
= memdup( get_req_data(), process
->exe
.namelen
);
998 generate_startup_debug_events( process
, req
->entry
);
999 set_process_startup_state( process
, STARTUP_DONE
);
1001 if (req
->gui
) process
->idle_event
= create_event( NULL
, 0, 1, 0 );
1002 if (current
->suspend
+ process
->suspend
> 0) stop_thread( current
);
1003 reply
->debugged
= (process
->debugger
!= 0);
1006 /* open a handle to a process */
1007 DECL_HANDLER(open_process
)
1009 struct process
*process
= get_process_from_id( req
->pid
);
1013 reply
->handle
= alloc_handle( current
->process
, process
, req
->access
, req
->inherit
);
1014 release_object( process
);
1018 /* terminate a process */
1019 DECL_HANDLER(terminate_process
)
1021 struct process
*process
;
1023 if ((process
= get_process_from_handle( req
->handle
, PROCESS_TERMINATE
)))
1025 reply
->self
= (current
->process
== process
);
1026 kill_process( process
, current
, req
->exit_code
);
1027 release_object( process
);
1031 /* fetch information about a process */
1032 DECL_HANDLER(get_process_info
)
1034 struct process
*process
;
1036 if ((process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_INFORMATION
)))
1038 get_process_info( process
, reply
);
1039 release_object( process
);
1043 /* set information about a process */
1044 DECL_HANDLER(set_process_info
)
1046 struct process
*process
;
1048 if ((process
= get_process_from_handle( req
->handle
, PROCESS_SET_INFORMATION
)))
1050 set_process_info( process
, req
);
1051 release_object( process
);
1055 /* read data from a process address space */
1056 DECL_HANDLER(read_process_memory
)
1058 struct process
*process
;
1059 size_t len
= get_reply_max_size();
1061 if (!(process
= get_process_from_handle( req
->handle
, PROCESS_VM_READ
))) return;
1065 unsigned int start_offset
= (unsigned int)req
->addr
% sizeof(int);
1066 unsigned int nb_ints
= (len
+ start_offset
+ sizeof(int) - 1) / sizeof(int);
1067 const int *start
= (int *)((char *)req
->addr
- start_offset
);
1068 int *buffer
= mem_alloc( nb_ints
* sizeof(int) );
1071 if (read_process_memory( process
, start
, nb_ints
, buffer
))
1073 /* move start of requested data to start of buffer */
1074 if (start_offset
) memmove( buffer
, (char *)buffer
+ start_offset
, len
);
1075 set_reply_data_ptr( buffer
, len
);
1080 release_object( process
);
1083 /* write data to a process address space */
1084 DECL_HANDLER(write_process_memory
)
1086 struct process
*process
;
1088 if ((process
= get_process_from_handle( req
->handle
, PROCESS_VM_WRITE
)))
1090 size_t len
= get_req_data_size();
1091 if ((len
% sizeof(int)) || ((unsigned int)req
->addr
% sizeof(int)))
1092 set_error( STATUS_INVALID_PARAMETER
);
1095 if (len
) write_process_memory( process
, req
->addr
, len
/ sizeof(int),
1096 req
->first_mask
, req
->last_mask
, get_req_data() );
1098 release_object( process
);
1102 /* notify the server that a dll has been loaded */
1103 DECL_HANDLER(load_dll
)
1105 struct process_dll
*dll
;
1106 struct file
*file
= NULL
;
1109 !(file
= get_file_obj( current
->process
, req
->handle
, GENERIC_READ
))) return;
1111 if ((dll
= process_load_dll( current
->process
, file
, req
->base
,
1112 get_req_data(), get_req_data_size() )))
1114 dll
->size
= req
->size
;
1115 dll
->dbg_offset
= req
->dbg_offset
;
1116 dll
->dbg_size
= req
->dbg_size
;
1117 dll
->name
= req
->name
;
1118 /* only generate event if initialization is done */
1119 if (is_process_init_done( current
->process
))
1120 generate_debug_event( current
, LOAD_DLL_DEBUG_EVENT
, dll
);
1122 if (file
) release_object( file
);
1125 /* notify the server that a dll is being unloaded */
1126 DECL_HANDLER(unload_dll
)
1128 process_unload_dll( current
->process
, req
->base
);
1131 /* retrieve information about a module in a process */
1132 DECL_HANDLER(get_dll_info
)
1134 struct process
*process
;
1136 if ((process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_INFORMATION
)))
1138 struct process_dll
*dll
;
1140 for (dll
= &process
->exe
; dll
; dll
= dll
->next
)
1142 if (dll
->base
== req
->base_address
)
1144 reply
->size
= dll
->size
;
1145 reply
->entry_point
= 0; /* FIXME */
1148 size_t len
= min( dll
->namelen
, get_reply_max_size() );
1149 set_reply_data( dll
->filename
, len
);
1155 set_error(STATUS_DLL_NOT_FOUND
);
1156 release_object( process
);
1160 /* wait for a process to start waiting on input */
1161 /* FIXME: only returns event for now, wait is done in the client */
1162 DECL_HANDLER(wait_input_idle
)
1164 struct process
*process
;
1167 if ((process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_INFORMATION
)))
1169 if (process
->idle_event
&& process
!= current
->process
&& process
->queue
!= current
->queue
)
1170 reply
->event
= alloc_handle( current
->process
, process
->idle_event
,
1171 EVENT_ALL_ACCESS
, 0 );
1172 release_object( process
);