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"
32 #ifdef HAVE_SYS_SOCKET_H
33 # include <sys/socket.h>
49 /* process structure */
51 static struct process
*first_process
;
52 static int running_processes
;
54 /* process operations */
56 static void process_dump( struct object
*obj
, int verbose
);
57 static int process_signaled( struct object
*obj
, struct thread
*thread
);
58 static void process_poll_event( struct fd
*fd
, int event
);
59 static void process_destroy( struct object
*obj
);
61 static const struct object_ops process_ops
=
63 sizeof(struct process
), /* size */
64 process_dump
, /* dump */
65 add_queue
, /* add_queue */
66 remove_queue
, /* remove_queue */
67 process_signaled
, /* signaled */
68 no_satisfied
, /* satisfied */
69 no_get_fd
, /* get_fd */
70 process_destroy
/* destroy */
73 static const struct fd_ops process_fd_ops
=
75 NULL
, /* get_poll_events */
76 process_poll_event
, /* poll_event */
78 no_get_file_info
, /* get_file_info */
79 no_queue_async
/* queue_async */
82 /* process startup info */
86 struct object obj
; /* object header */
87 struct list entry
; /* entry in list of startup infos */
88 int inherit_all
; /* inherit all handles from parent */
89 int create_flags
; /* creation flags */
90 int unix_pid
; /* Unix pid of new process */
91 obj_handle_t hstdin
; /* handle for stdin */
92 obj_handle_t hstdout
; /* handle for stdout */
93 obj_handle_t hstderr
; /* handle for stderr */
94 struct file
*exe_file
; /* file handle for main exe */
95 struct thread
*owner
; /* owner thread (the one that created the new process) */
96 struct process
*process
; /* created process */
97 struct thread
*thread
; /* created thread */
98 size_t data_size
; /* size of startup data */
99 void *data
; /* data for startup info */
102 static void startup_info_dump( struct object
*obj
, int verbose
);
103 static int startup_info_signaled( struct object
*obj
, struct thread
*thread
);
104 static void startup_info_destroy( struct object
*obj
);
106 static const struct object_ops startup_info_ops
=
108 sizeof(struct startup_info
), /* size */
109 startup_info_dump
, /* dump */
110 add_queue
, /* add_queue */
111 remove_queue
, /* remove_queue */
112 startup_info_signaled
, /* signaled */
113 no_satisfied
, /* satisfied */
114 no_get_fd
, /* get_fd */
115 startup_info_destroy
/* destroy */
119 static struct list startup_info_list
= LIST_INIT(startup_info_list
);
123 void *ptr
; /* entry ptr */
124 unsigned int next
; /* next free entry */
127 static struct ptid_entry
*ptid_entries
; /* array of ptid entries */
128 static unsigned int used_ptid_entries
; /* number of entries in use */
129 static unsigned int alloc_ptid_entries
; /* number of allocated entries */
130 static unsigned int next_free_ptid
; /* next free entry */
131 static unsigned int last_free_ptid
; /* last free entry */
133 #define PTID_OFFSET 8 /* offset for first ptid value */
135 /* allocate a new process or thread id */
136 unsigned int alloc_ptid( void *ptr
)
138 struct ptid_entry
*entry
;
141 if (used_ptid_entries
< alloc_ptid_entries
)
143 id
= used_ptid_entries
+ PTID_OFFSET
;
144 entry
= &ptid_entries
[used_ptid_entries
++];
146 else if (next_free_ptid
)
149 entry
= &ptid_entries
[id
- PTID_OFFSET
];
150 if (!(next_free_ptid
= entry
->next
)) last_free_ptid
= 0;
152 else /* need to grow the array */
154 unsigned int count
= alloc_ptid_entries
+ (alloc_ptid_entries
/ 2);
155 if (!count
) count
= 64;
156 if (!(entry
= realloc( ptid_entries
, count
* sizeof(*entry
) )))
158 set_error( STATUS_NO_MEMORY
);
161 ptid_entries
= entry
;
162 alloc_ptid_entries
= count
;
163 id
= used_ptid_entries
+ PTID_OFFSET
;
164 entry
= &ptid_entries
[used_ptid_entries
++];
171 /* free a process or thread id */
172 void free_ptid( unsigned int id
)
174 struct ptid_entry
*entry
= &ptid_entries
[id
- PTID_OFFSET
];
179 /* append to end of free list so that we don't reuse it too early */
180 if (last_free_ptid
) ptid_entries
[last_free_ptid
- PTID_OFFSET
].next
= id
;
181 else next_free_ptid
= id
;
186 /* retrieve the pointer corresponding to a process or thread id */
187 void *get_ptid_entry( unsigned int id
)
189 if (id
< PTID_OFFSET
) return NULL
;
190 if (id
- PTID_OFFSET
>= used_ptid_entries
) return NULL
;
191 return ptid_entries
[id
- PTID_OFFSET
].ptr
;
194 /* set the state of the process startup info */
195 static void set_process_startup_state( struct process
*process
, enum startup_state state
)
197 if (process
->startup_state
== STARTUP_IN_PROGRESS
) process
->startup_state
= state
;
198 if (process
->startup_info
)
200 wake_up( &process
->startup_info
->obj
, 0 );
201 release_object( process
->startup_info
);
202 process
->startup_info
= NULL
;
206 /* set the console and stdio handles for a newly created process */
207 static int set_process_console( struct process
*process
, struct thread
*parent_thread
,
208 struct startup_info
*info
, struct init_process_reply
*reply
)
210 if (process
->create_flags
& CREATE_NEW_CONSOLE
)
212 /* let the process init do the allocation */
215 else if (info
&& !(process
->create_flags
& DETACHED_PROCESS
))
217 /* FIXME: some better error checking should be done...
218 * like if hConOut and hConIn are console handles, then they should be on the same
221 inherit_console( parent_thread
, process
, info
->inherit_all
? info
->hstdin
: 0 );
225 if (!info
->inherit_all
)
227 reply
->hstdin
= duplicate_handle( parent_thread
->process
, info
->hstdin
, process
,
228 0, TRUE
, DUPLICATE_SAME_ACCESS
);
229 reply
->hstdout
= duplicate_handle( parent_thread
->process
, info
->hstdout
, process
,
230 0, TRUE
, DUPLICATE_SAME_ACCESS
);
231 reply
->hstderr
= duplicate_handle( parent_thread
->process
, info
->hstderr
, process
,
232 0, TRUE
, DUPLICATE_SAME_ACCESS
);
236 reply
->hstdin
= info
->hstdin
;
237 reply
->hstdout
= info
->hstdout
;
238 reply
->hstderr
= info
->hstderr
;
241 else reply
->hstdin
= reply
->hstdout
= reply
->hstderr
= 0;
242 /* some handles above may have been invalid; this is not an error */
243 if (get_error() == STATUS_INVALID_HANDLE
||
244 get_error() == STATUS_OBJECT_TYPE_MISMATCH
) clear_error();
248 /* create a new process and its main thread */
249 struct thread
*create_process( int fd
)
251 struct process
*process
;
252 struct thread
*thread
= NULL
;
255 if (!(process
= alloc_object( &process_ops
))) goto error
;
256 process
->next
= NULL
;
257 process
->prev
= NULL
;
258 process
->parent
= NULL
;
259 process
->thread_list
= NULL
;
260 process
->debugger
= NULL
;
261 process
->handles
= NULL
;
262 process
->msg_fd
= NULL
;
263 process
->exit_code
= STILL_ACTIVE
;
264 process
->running_threads
= 0;
265 process
->priority
= NORMAL_PRIORITY_CLASS
;
266 process
->affinity
= 1;
267 process
->suspend
= 0;
268 process
->create_flags
= 0;
269 process
->console
= NULL
;
270 process
->startup_state
= STARTUP_IN_PROGRESS
;
271 process
->startup_info
= NULL
;
272 process
->idle_event
= NULL
;
273 process
->queue
= NULL
;
274 process
->atom_table
= NULL
;
276 process
->ldt_copy
= NULL
;
277 process
->exe
.next
= NULL
;
278 process
->exe
.prev
= NULL
;
279 process
->exe
.file
= NULL
;
280 process
->exe
.dbg_offset
= 0;
281 process
->exe
.dbg_size
= 0;
282 process
->exe
.namelen
= 0;
283 process
->exe
.filename
= NULL
;
284 process
->group_id
= 0;
285 process
->token
= create_token();
286 list_init( &process
->locks
);
287 list_init( &process
->classes
);
289 gettimeofday( &process
->start_time
, NULL
);
290 if ((process
->next
= first_process
) != NULL
) process
->next
->prev
= process
;
291 first_process
= process
;
293 if (!(process
->id
= alloc_ptid( process
))) goto error
;
294 if (!(process
->msg_fd
= create_anonymous_fd( &process_fd_ops
, fd
, &process
->obj
))) goto error
;
296 /* create the main thread */
297 if (pipe( request_pipe
) == -1)
302 if (send_client_fd( process
, request_pipe
[1], 0 ) == -1)
304 close( request_pipe
[0] );
305 close( request_pipe
[1] );
308 close( request_pipe
[1] );
309 if (!(thread
= create_thread( request_pipe
[0], process
))) goto error
;
311 set_fd_events( process
->msg_fd
, POLLIN
); /* start listening to events */
312 release_object( process
);
316 if (process
) release_object( process
);
317 /* if we failed to start our first process, close everything down */
318 if (!running_processes
) close_master_socket();
322 /* find the startup info for a given Unix process */
323 inline static struct startup_info
*find_startup_info( int unix_pid
)
327 LIST_FOR_EACH( ptr
, &startup_info_list
)
329 struct startup_info
*info
= LIST_ENTRY( ptr
, struct startup_info
, entry
);
330 if (info
->unix_pid
== unix_pid
) return info
;
335 /* initialize the current process and fill in the request */
336 static struct startup_info
*init_process( struct init_process_reply
*reply
)
338 struct process
*process
= current
->process
;
339 struct thread
*parent_thread
= NULL
;
340 struct process
*parent
= NULL
;
341 struct startup_info
*info
= find_startup_info( current
->unix_pid
);
347 fatal_protocol_error( current
, "init_process: called twice?\n" );
350 parent_thread
= info
->owner
;
351 parent
= parent_thread
->process
;
352 process
->parent
= (struct process
*)grab_object( parent
);
355 /* set the process flags */
356 process
->create_flags
= info
? info
->create_flags
: 0;
358 /* create the handle table */
359 if (info
&& info
->inherit_all
)
360 process
->handles
= copy_handle_table( process
, parent
);
362 process
->handles
= alloc_handle_table( process
, 0 );
363 if (!process
->handles
) return NULL
;
365 /* retrieve the main exe file */
367 if (info
&& info
->exe_file
)
369 process
->exe
.file
= (struct file
*)grab_object( info
->exe_file
);
370 if (!(reply
->exe_file
= alloc_handle( process
, process
->exe
.file
, GENERIC_READ
, 0 )))
374 /* set the process console */
375 if (!set_process_console( process
, parent_thread
, info
, reply
)) return NULL
;
377 process
->group_id
= get_process_id( process
);
380 /* attach to the debugger if requested */
381 if (process
->create_flags
& (DEBUG_PROCESS
| DEBUG_ONLY_THIS_PROCESS
))
382 set_process_debugger( process
, parent_thread
);
383 else if (parent
->debugger
&& !(parent
->create_flags
& DEBUG_ONLY_THIS_PROCESS
))
384 set_process_debugger( process
, parent
->debugger
);
385 if (!(process
->create_flags
& CREATE_NEW_PROCESS_GROUP
))
386 process
->group_id
= parent
->group_id
;
389 /* thread will be actually suspended in init_done */
390 if (process
->create_flags
& CREATE_SUSPENDED
) current
->suspend
++;
394 reply
->info_size
= info
->data_size
;
395 info
->process
= (struct process
*)grab_object( process
);
396 info
->thread
= (struct thread
*)grab_object( current
);
398 reply
->create_flags
= process
->create_flags
;
399 reply
->server_start
= server_start_ticks
;
400 return info
? (struct startup_info
*)grab_object( info
) : NULL
;
403 /* destroy a process when its refcount is 0 */
404 static void process_destroy( struct object
*obj
)
406 struct process
*process
= (struct process
*)obj
;
407 assert( obj
->ops
== &process_ops
);
409 /* we can't have a thread remaining */
410 assert( !process
->thread_list
);
412 set_process_startup_state( process
, STARTUP_ABORTED
);
413 if (process
->console
) release_object( process
->console
);
414 if (process
->parent
) release_object( process
->parent
);
415 if (process
->msg_fd
) release_object( process
->msg_fd
);
416 if (process
->next
) process
->next
->prev
= process
->prev
;
417 if (process
->prev
) process
->prev
->next
= process
->next
;
418 else first_process
= process
->next
;
419 if (process
->idle_event
) release_object( process
->idle_event
);
420 if (process
->queue
) release_object( process
->queue
);
421 if (process
->atom_table
) release_object( process
->atom_table
);
422 if (process
->exe
.file
) release_object( process
->exe
.file
);
423 if (process
->exe
.filename
) free( process
->exe
.filename
);
424 if (process
->id
) free_ptid( process
->id
);
425 if (process
->token
) release_object( process
->token
);
428 /* dump a process on stdout for debugging purposes */
429 static void process_dump( struct object
*obj
, int verbose
)
431 struct process
*process
= (struct process
*)obj
;
432 assert( obj
->ops
== &process_ops
);
434 fprintf( stderr
, "Process id=%04x next=%p prev=%p handles=%p\n",
435 process
->id
, process
->next
, process
->prev
, process
->handles
);
438 static int process_signaled( struct object
*obj
, struct thread
*thread
)
440 struct process
*process
= (struct process
*)obj
;
441 return !process
->running_threads
;
444 static void process_poll_event( struct fd
*fd
, int event
)
446 struct process
*process
= get_fd_user( fd
);
447 assert( process
->obj
.ops
== &process_ops
);
449 if (event
& (POLLERR
| POLLHUP
)) set_fd_events( fd
, -1 );
450 else if (event
& POLLIN
) receive_fd( process
);
453 static void startup_info_destroy( struct object
*obj
)
455 struct startup_info
*info
= (struct startup_info
*)obj
;
456 assert( obj
->ops
== &startup_info_ops
);
457 list_remove( &info
->entry
);
458 if (info
->data
) free( info
->data
);
459 if (info
->exe_file
) release_object( info
->exe_file
);
460 if (info
->process
) release_object( info
->process
);
461 if (info
->thread
) release_object( info
->thread
);
462 if (info
->owner
) release_object( info
->owner
);
465 static void startup_info_dump( struct object
*obj
, int verbose
)
467 struct startup_info
*info
= (struct startup_info
*)obj
;
468 assert( obj
->ops
== &startup_info_ops
);
470 fprintf( stderr
, "Startup info flags=%x in=%p out=%p err=%p\n",
471 info
->create_flags
, info
->hstdin
, info
->hstdout
, info
->hstderr
);
474 static int startup_info_signaled( struct object
*obj
, struct thread
*thread
)
476 struct startup_info
*info
= (struct startup_info
*)obj
;
477 return info
->process
&& is_process_init_done(info
->process
);
480 /* get a process from an id (and increment the refcount) */
481 struct process
*get_process_from_id( process_id_t id
)
483 struct object
*obj
= get_ptid_entry( id
);
485 if (obj
&& obj
->ops
== &process_ops
) return (struct process
*)grab_object( obj
);
486 set_error( STATUS_INVALID_PARAMETER
);
490 /* get a process from a handle (and increment the refcount) */
491 struct process
*get_process_from_handle( obj_handle_t handle
, unsigned int access
)
493 return (struct process
*)get_handle_obj( current
->process
, handle
,
494 access
, &process_ops
);
497 /* add a dll to a process list */
498 static struct process_dll
*process_load_dll( struct process
*process
, struct file
*file
,
499 void *base
, const WCHAR
*filename
, size_t name_len
)
501 struct process_dll
*dll
;
503 /* make sure we don't already have one with the same base address */
504 for (dll
= process
->exe
.next
; dll
; dll
= dll
->next
) if (dll
->base
== base
)
506 set_error( STATUS_INVALID_PARAMETER
);
510 if ((dll
= mem_alloc( sizeof(*dll
) )))
512 dll
->prev
= &process
->exe
;
515 dll
->filename
= NULL
;
516 dll
->namelen
= name_len
;
517 if (name_len
&& !(dll
->filename
= memdup( filename
, name_len
)))
522 if (file
) dll
->file
= (struct file
*)grab_object( file
);
523 if ((dll
->next
= process
->exe
.next
)) dll
->next
->prev
= dll
;
524 process
->exe
.next
= dll
;
529 /* remove a dll from a process list */
530 static void process_unload_dll( struct process
*process
, void *base
)
532 struct process_dll
*dll
;
534 for (dll
= process
->exe
.next
; dll
; dll
= dll
->next
)
536 if (dll
->base
== base
)
538 if (dll
->file
) release_object( dll
->file
);
539 if (dll
->next
) dll
->next
->prev
= dll
->prev
;
540 if (dll
->prev
) dll
->prev
->next
= dll
->next
;
541 if (dll
->filename
) free( dll
->filename
);
543 generate_debug_event( current
, UNLOAD_DLL_DEBUG_EVENT
, base
);
547 set_error( STATUS_INVALID_PARAMETER
);
550 /* kill all processes */
551 void kill_all_processes( struct process
*skip
, int exit_code
)
555 struct process
*process
= first_process
;
557 while (process
&& (!process
->running_threads
|| process
== skip
))
558 process
= process
->next
;
560 kill_process( process
, NULL
, exit_code
);
564 /* kill all processes being attached to a console renderer */
565 void kill_console_processes( struct thread
*renderer
, int exit_code
)
567 for (;;) /* restart from the beginning of the list every time */
569 struct process
*process
= first_process
;
571 /* find the first process being attached to 'renderer' and still running */
573 (process
== renderer
->process
|| !process
->console
||
574 process
->console
->renderer
!= renderer
|| !process
->running_threads
))
576 process
= process
->next
;
579 kill_process( process
, NULL
, exit_code
);
583 /* a process has been killed (i.e. its last thread died) */
584 static void process_killed( struct process
*process
)
586 assert( !process
->thread_list
);
587 gettimeofday( &process
->end_time
, NULL
);
588 if (process
->handles
) release_object( process
->handles
);
589 process
->handles
= NULL
;
591 /* close the console attached to this process, if any */
592 free_console( process
);
594 while (process
->exe
.next
)
596 struct process_dll
*dll
= process
->exe
.next
;
597 process
->exe
.next
= dll
->next
;
598 if (dll
->file
) release_object( dll
->file
);
599 if (dll
->filename
) free( dll
->filename
);
602 destroy_process_classes( process
);
603 set_process_startup_state( process
, STARTUP_ABORTED
);
604 if (process
->exe
.file
) release_object( process
->exe
.file
);
605 process
->exe
.file
= NULL
;
606 wake_up( &process
->obj
, 0 );
607 if (!--running_processes
) close_master_socket();
610 /* add a thread to a process running threads list */
611 void add_process_thread( struct process
*process
, struct thread
*thread
)
613 thread
->proc_next
= process
->thread_list
;
614 thread
->proc_prev
= NULL
;
615 if (thread
->proc_next
) thread
->proc_next
->proc_prev
= thread
;
616 process
->thread_list
= thread
;
617 if (!process
->running_threads
++) running_processes
++;
618 grab_object( thread
);
621 /* remove a thread from a process running threads list */
622 void remove_process_thread( struct process
*process
, struct thread
*thread
)
624 assert( process
->running_threads
> 0 );
625 assert( process
->thread_list
);
627 if (thread
->proc_next
) thread
->proc_next
->proc_prev
= thread
->proc_prev
;
628 if (thread
->proc_prev
) thread
->proc_prev
->proc_next
= thread
->proc_next
;
629 else process
->thread_list
= thread
->proc_next
;
631 if (!--process
->running_threads
)
633 /* we have removed the last running thread, exit the process */
634 process
->exit_code
= thread
->exit_code
;
635 generate_debug_event( thread
, EXIT_PROCESS_DEBUG_EVENT
, process
);
636 process_killed( process
);
638 else generate_debug_event( thread
, EXIT_THREAD_DEBUG_EVENT
, thread
);
639 release_object( thread
);
642 /* suspend all the threads of a process */
643 void suspend_process( struct process
*process
)
645 if (!process
->suspend
++)
647 struct thread
*thread
= process
->thread_list
;
650 struct thread
*next
= thread
->proc_next
;
651 if (!thread
->suspend
) stop_thread( thread
);
657 /* resume all the threads of a process */
658 void resume_process( struct process
*process
)
660 assert (process
->suspend
> 0);
661 if (!--process
->suspend
)
663 struct thread
*thread
= process
->thread_list
;
666 struct thread
*next
= thread
->proc_next
;
667 if (!thread
->suspend
) wake_thread( thread
);
673 /* kill a process on the spot */
674 void kill_process( struct process
*process
, struct thread
*skip
, int exit_code
)
676 struct thread
*thread
= process
->thread_list
;
680 struct thread
*next
= thread
->proc_next
;
681 thread
->exit_code
= exit_code
;
682 if (thread
!= skip
) kill_thread( thread
, 1 );
687 /* kill all processes being debugged by a given thread */
688 void kill_debugged_processes( struct thread
*debugger
, int exit_code
)
690 for (;;) /* restart from the beginning of the list every time */
692 struct process
*process
= first_process
;
693 /* find the first process being debugged by 'debugger' and still running */
694 while (process
&& (process
->debugger
!= debugger
|| !process
->running_threads
))
695 process
= process
->next
;
696 if (!process
) return;
697 process
->debugger
= NULL
;
698 kill_process( process
, NULL
, exit_code
);
703 /* detach a debugger from all its debuggees */
704 void detach_debugged_processes( struct thread
*debugger
)
706 struct process
*process
;
707 for (process
= first_process
; process
; process
= process
->next
)
709 if (process
->debugger
== debugger
&& process
->running_threads
)
711 debugger_detach( process
, debugger
);
717 void enum_processes( int (*cb
)(struct process
*, void*), void *user
)
719 struct process
*process
;
720 for (process
= first_process
; process
; process
= process
->next
)
722 if ((cb
)(process
, user
)) break;
727 /* read data from a process memory space */
728 /* len is the total size (in ints) */
729 static int read_process_memory( struct process
*process
, const int *addr
, size_t len
, int *dest
)
731 struct thread
*thread
= process
->thread_list
;
733 assert( !((unsigned int)addr
% sizeof(int)) ); /* address must be aligned */
735 if (!thread
) /* process is dead */
737 set_error( STATUS_ACCESS_DENIED
);
740 if (suspend_for_ptrace( thread
))
744 if (read_thread_int( thread
, addr
++, dest
++ ) == -1) break;
747 resume_after_ptrace( thread
);
752 /* make sure we can write to the whole address range */
753 /* len is the total size (in ints) */
754 static int check_process_write_access( struct thread
*thread
, int *addr
, size_t len
)
756 int page
= get_page_size() / sizeof(int);
760 if (write_thread_int( thread
, addr
, 0, 0 ) == -1) return 0;
761 if (len
<= page
) break;
765 return (write_thread_int( thread
, addr
+ len
- 1, 0, 0 ) != -1);
768 /* write data to a process memory space */
769 /* len is the total size (in ints), max is the size we can actually read from the input buffer */
770 /* we check the total size for write permissions */
771 static int write_process_memory( struct process
*process
, int *addr
, size_t len
,
772 unsigned int first_mask
, unsigned int last_mask
, const int *src
)
774 struct thread
*thread
= process
->thread_list
;
777 assert( !((unsigned int)addr
% sizeof(int) )); /* address must be aligned */
779 if (!thread
) /* process is dead */
781 set_error( STATUS_ACCESS_DENIED
);
784 if (suspend_for_ptrace( thread
))
786 if (!check_process_write_access( thread
, addr
, len
))
788 set_error( STATUS_ACCESS_DENIED
);
791 /* first word is special */
794 if (write_thread_int( thread
, addr
++, *src
++, first_mask
) == -1) goto done
;
797 else last_mask
&= first_mask
;
801 if (write_thread_int( thread
, addr
++, *src
++, ~0 ) == -1) goto done
;
805 /* last word is special too */
806 if (write_thread_int( thread
, addr
, *src
, last_mask
) == -1) goto done
;
810 resume_after_ptrace( thread
);
815 /* set the debugged flag in the process PEB */
816 int set_process_debug_flag( struct process
*process
, int flag
)
818 int mask
= 0, data
= 0;
820 /* BeingDebugged flag is the byte at offset 2 in the PEB */
821 memset( (char *)&mask
+ 2, 0xff, 1 );
822 memset( (char *)&data
+ 2, flag
, 1 );
823 return write_process_memory( process
, process
->peb
, 1, mask
, mask
, &data
);
826 /* take a snapshot of currently running processes */
827 struct process_snapshot
*process_snap( int *count
)
829 struct process_snapshot
*snapshot
, *ptr
;
830 struct process
*process
;
831 if (!running_processes
) return NULL
;
832 if (!(snapshot
= mem_alloc( sizeof(*snapshot
) * running_processes
)))
835 for (process
= first_process
; process
; process
= process
->next
)
837 if (!process
->running_threads
) continue;
838 ptr
->process
= process
;
839 ptr
->threads
= process
->running_threads
;
840 ptr
->count
= process
->obj
.refcount
;
841 ptr
->priority
= process
->priority
;
842 ptr
->handles
= get_handle_table_count(process
);
843 grab_object( process
);
846 *count
= running_processes
;
850 /* take a snapshot of the modules of a process */
851 struct module_snapshot
*module_snap( struct process
*process
, int *count
)
853 struct module_snapshot
*snapshot
, *ptr
;
854 struct process_dll
*dll
;
857 for (dll
= &process
->exe
; dll
; dll
= dll
->next
) total
++;
858 if (!(snapshot
= mem_alloc( sizeof(*snapshot
) * total
))) return NULL
;
860 for (ptr
= snapshot
, dll
= &process
->exe
; dll
; dll
= dll
->next
, ptr
++)
862 ptr
->base
= dll
->base
;
863 ptr
->size
= dll
->size
;
864 ptr
->namelen
= dll
->namelen
;
865 ptr
->filename
= memdup( dll
->filename
, dll
->namelen
);
872 /* create a new process */
873 DECL_HANDLER(new_process
)
875 struct startup_info
*info
;
877 /* build the startup info for a new process */
878 if (!(info
= alloc_object( &startup_info_ops
))) return;
879 list_add_head( &startup_info_list
, &info
->entry
);
880 info
->inherit_all
= req
->inherit_all
;
881 info
->create_flags
= req
->create_flags
;
882 info
->unix_pid
= req
->unix_pid
;
883 info
->hstdin
= req
->hstdin
;
884 info
->hstdout
= req
->hstdout
;
885 info
->hstderr
= req
->hstderr
;
886 info
->exe_file
= NULL
;
887 info
->owner
= (struct thread
*)grab_object( current
);
888 info
->process
= NULL
;
890 info
->data_size
= get_req_data_size();
894 !(info
->exe_file
= get_file_obj( current
->process
, req
->exe_file
, GENERIC_READ
)))
897 if (!(info
->data
= memdup( get_req_data(), info
->data_size
))) goto done
;
898 reply
->info
= alloc_handle( current
->process
, info
, SYNCHRONIZE
, FALSE
);
901 release_object( info
);
904 /* Retrieve information about a newly started process */
905 DECL_HANDLER(get_new_process_info
)
907 struct startup_info
*info
;
909 if ((info
= (struct startup_info
*)get_handle_obj( current
->process
, req
->info
,
910 0, &startup_info_ops
)))
912 reply
->pid
= get_process_id( info
->process
);
913 reply
->tid
= get_thread_id( info
->thread
);
914 reply
->phandle
= alloc_handle( current
->process
, info
->process
,
915 PROCESS_ALL_ACCESS
, req
->pinherit
);
916 reply
->thandle
= alloc_handle( current
->process
, info
->thread
,
917 THREAD_ALL_ACCESS
, req
->tinherit
);
918 reply
->success
= is_process_init_done( info
->process
);
919 release_object( info
);
931 /* Retrieve the new process startup info */
932 DECL_HANDLER(get_startup_info
)
934 struct startup_info
*info
;
936 if ((info
= current
->process
->startup_info
))
938 size_t size
= info
->data_size
;
939 if (size
> get_reply_max_size()) size
= get_reply_max_size();
941 /* we return the data directly without making a copy so this can only be called once */
942 set_reply_data_ptr( info
->data
, size
);
949 /* initialize a new process */
950 DECL_HANDLER(init_process
)
952 if (current
->unix_pid
== -1)
954 fatal_protocol_error( current
, "init_process: init_thread not called yet\n" );
957 if (current
->process
->startup_info
)
959 fatal_protocol_error( current
, "init_process: called twice\n" );
962 if (!req
->peb
|| (unsigned int)req
->peb
% sizeof(int))
964 fatal_protocol_error( current
, "init_process: bad peb address\n" );
967 if (!req
->ldt_copy
|| (unsigned int)req
->ldt_copy
% sizeof(int))
969 fatal_protocol_error( current
, "init_process: bad ldt_copy address\n" );
972 reply
->info_size
= 0;
973 current
->process
->peb
= req
->peb
;
974 current
->process
->ldt_copy
= req
->ldt_copy
;
975 current
->process
->startup_info
= init_process( reply
);
978 /* signal the end of the process initialization */
979 DECL_HANDLER(init_process_done
)
981 struct file
*file
= NULL
;
982 struct process
*process
= current
->process
;
984 if (is_process_init_done(process
))
986 fatal_protocol_error( current
, "init_process_done: called twice\n" );
991 fatal_protocol_error( current
, "init_process_done: module base address cannot be 0\n" );
994 process
->exe
.base
= req
->module
;
995 process
->exe
.size
= req
->module_size
;
996 process
->exe
.name
= req
->name
;
998 if (req
->exe_file
) file
= get_file_obj( process
, req
->exe_file
, GENERIC_READ
);
999 if (process
->exe
.file
) release_object( process
->exe
.file
);
1000 process
->exe
.file
= file
;
1002 if ((process
->exe
.namelen
= get_req_data_size()))
1003 process
->exe
.filename
= memdup( get_req_data(), process
->exe
.namelen
);
1005 generate_startup_debug_events( process
, req
->entry
);
1006 set_process_startup_state( process
, STARTUP_DONE
);
1008 if (req
->gui
) process
->idle_event
= create_event( NULL
, 0, 1, 0 );
1009 if (current
->suspend
+ process
->suspend
> 0) stop_thread( current
);
1010 if (process
->debugger
) set_process_debug_flag( process
, 1 );
1013 /* open a handle to a process */
1014 DECL_HANDLER(open_process
)
1016 struct process
*process
= get_process_from_id( req
->pid
);
1020 reply
->handle
= alloc_handle( current
->process
, process
, req
->access
, req
->inherit
);
1021 release_object( process
);
1025 /* terminate a process */
1026 DECL_HANDLER(terminate_process
)
1028 struct process
*process
;
1030 if ((process
= get_process_from_handle( req
->handle
, PROCESS_TERMINATE
)))
1032 reply
->self
= (current
->process
== process
);
1033 kill_process( process
, current
, req
->exit_code
);
1034 release_object( process
);
1038 /* fetch information about a process */
1039 DECL_HANDLER(get_process_info
)
1041 struct process
*process
;
1043 if ((process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_INFORMATION
)))
1045 reply
->pid
= get_process_id( process
);
1046 reply
->exit_code
= process
->exit_code
;
1047 reply
->priority
= process
->priority
;
1048 reply
->process_affinity
= process
->affinity
;
1049 reply
->system_affinity
= 1;
1050 release_object( process
);
1054 /* set information about a process */
1055 DECL_HANDLER(set_process_info
)
1057 struct process
*process
;
1059 if ((process
= get_process_from_handle( req
->handle
, PROCESS_SET_INFORMATION
)))
1061 if (req
->mask
& SET_PROCESS_INFO_PRIORITY
) process
->priority
= req
->priority
;
1062 if (req
->mask
& SET_PROCESS_INFO_AFFINITY
)
1064 if (req
->affinity
!= 1) set_error( STATUS_INVALID_PARAMETER
);
1065 else process
->affinity
= req
->affinity
;
1067 release_object( process
);
1071 /* read data from a process address space */
1072 DECL_HANDLER(read_process_memory
)
1074 struct process
*process
;
1075 size_t len
= get_reply_max_size();
1077 if (!(process
= get_process_from_handle( req
->handle
, PROCESS_VM_READ
))) return;
1081 unsigned int start_offset
= (unsigned int)req
->addr
% sizeof(int);
1082 unsigned int nb_ints
= (len
+ start_offset
+ sizeof(int) - 1) / sizeof(int);
1083 const int *start
= (int *)((char *)req
->addr
- start_offset
);
1084 int *buffer
= mem_alloc( nb_ints
* sizeof(int) );
1087 if (read_process_memory( process
, start
, nb_ints
, buffer
))
1089 /* move start of requested data to start of buffer */
1090 if (start_offset
) memmove( buffer
, (char *)buffer
+ start_offset
, len
);
1091 set_reply_data_ptr( buffer
, len
);
1096 release_object( process
);
1099 /* write data to a process address space */
1100 DECL_HANDLER(write_process_memory
)
1102 struct process
*process
;
1104 if ((process
= get_process_from_handle( req
->handle
, PROCESS_VM_WRITE
)))
1106 size_t len
= get_req_data_size();
1107 if ((len
% sizeof(int)) || ((unsigned int)req
->addr
% sizeof(int)))
1108 set_error( STATUS_INVALID_PARAMETER
);
1111 if (len
) write_process_memory( process
, req
->addr
, len
/ sizeof(int),
1112 req
->first_mask
, req
->last_mask
, get_req_data() );
1114 release_object( process
);
1118 /* notify the server that a dll has been loaded */
1119 DECL_HANDLER(load_dll
)
1121 struct process_dll
*dll
;
1122 struct file
*file
= NULL
;
1124 if (req
->handle
&& !(file
= get_file_obj( current
->process
, req
->handle
, GENERIC_READ
)))
1127 if ((dll
= process_load_dll( current
->process
, file
, req
->base
,
1128 get_req_data(), get_req_data_size() )))
1130 dll
->size
= req
->size
;
1131 dll
->dbg_offset
= req
->dbg_offset
;
1132 dll
->dbg_size
= req
->dbg_size
;
1133 dll
->name
= req
->name
;
1134 /* only generate event if initialization is done */
1135 if (is_process_init_done( current
->process
))
1136 generate_debug_event( current
, LOAD_DLL_DEBUG_EVENT
, dll
);
1138 if (file
) release_object( file
);
1141 /* notify the server that a dll is being unloaded */
1142 DECL_HANDLER(unload_dll
)
1144 process_unload_dll( current
->process
, req
->base
);
1147 /* retrieve information about a module in a process */
1148 DECL_HANDLER(get_dll_info
)
1150 struct process
*process
;
1152 if ((process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_INFORMATION
)))
1154 struct process_dll
*dll
;
1156 for (dll
= &process
->exe
; dll
; dll
= dll
->next
)
1158 if (dll
->base
== req
->base_address
)
1160 reply
->size
= dll
->size
;
1161 reply
->entry_point
= 0; /* FIXME */
1164 size_t len
= min( dll
->namelen
, get_reply_max_size() );
1165 set_reply_data( dll
->filename
, len
);
1171 set_error(STATUS_DLL_NOT_FOUND
);
1172 release_object( process
);
1176 /* wait for a process to start waiting on input */
1177 /* FIXME: only returns event for now, wait is done in the client */
1178 DECL_HANDLER(wait_input_idle
)
1180 struct process
*process
;
1183 if ((process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_INFORMATION
)))
1185 if (process
->idle_event
&& process
!= current
->process
&& process
->queue
!= current
->queue
)
1186 reply
->event
= alloc_handle( current
->process
, process
->idle_event
,
1187 EVENT_ALL_ACCESS
, 0 );
1188 release_object( process
);