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 object
*obj
, 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 no_get_file_info
, /* get_file_info */
68 process_destroy
/* destroy */
71 static const struct fd_ops process_fd_ops
=
73 NULL
, /* get_poll_events */
74 process_poll_event
, /* poll_event */
76 no_get_file_info
, /* get_file_info */
77 no_queue_async
/* queue_async */
80 /* process startup info */
84 struct object obj
; /* object header */
85 int inherit_all
; /* inherit all handles from parent */
86 int use_handles
; /* use stdio handles */
87 int create_flags
; /* creation flags */
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 no_get_file_info
, /* get_file_info */
113 startup_info_destroy
/* destroy */
119 void *ptr
; /* entry ptr */
120 unsigned int next
; /* next free entry */
123 static struct ptid_entry
*ptid_entries
; /* array of ptid entries */
124 static unsigned int used_ptid_entries
; /* number of entries in use */
125 static unsigned int alloc_ptid_entries
; /* number of allocated entries */
126 static unsigned int next_free_ptid
; /* next free entry */
127 static unsigned int last_free_ptid
; /* last free entry */
129 #define PTID_OFFSET 8 /* offset for first ptid value */
131 /* allocate a new process or thread id */
132 unsigned int alloc_ptid( void *ptr
)
134 struct ptid_entry
*entry
;
137 if (used_ptid_entries
< alloc_ptid_entries
)
139 id
= used_ptid_entries
+ PTID_OFFSET
;
140 entry
= &ptid_entries
[used_ptid_entries
++];
142 else if (next_free_ptid
)
145 entry
= &ptid_entries
[id
- PTID_OFFSET
];
146 if (!(next_free_ptid
= entry
->next
)) last_free_ptid
= 0;
148 else /* need to grow the array */
150 unsigned int count
= alloc_ptid_entries
+ (alloc_ptid_entries
/ 2);
151 if (!count
) count
= 64;
152 if (!(entry
= realloc( ptid_entries
, count
* sizeof(*entry
) )))
154 set_error( STATUS_NO_MEMORY
);
157 ptid_entries
= entry
;
158 alloc_ptid_entries
= count
;
159 id
= used_ptid_entries
+ PTID_OFFSET
;
160 entry
= &ptid_entries
[used_ptid_entries
++];
167 /* free a process or thread id */
168 void free_ptid( unsigned int id
)
170 struct ptid_entry
*entry
= &ptid_entries
[id
- PTID_OFFSET
];
175 /* append to end of free list so that we don't reuse it too early */
176 if (last_free_ptid
) ptid_entries
[last_free_ptid
- PTID_OFFSET
].next
= id
;
177 else next_free_ptid
= id
;
182 /* retrieve the pointer corresponding to a process or thread id */
183 void *get_ptid_entry( unsigned int id
)
185 if (id
< PTID_OFFSET
) return NULL
;
186 if (id
- PTID_OFFSET
>= used_ptid_entries
) return NULL
;
187 return ptid_entries
[id
- PTID_OFFSET
].ptr
;
190 /* set the state of the process startup info */
191 static void set_process_startup_state( struct process
*process
, enum startup_state state
)
193 if (process
->startup_state
== STARTUP_IN_PROGRESS
) process
->startup_state
= state
;
194 if (process
->startup_info
)
196 wake_up( &process
->startup_info
->obj
, 0 );
197 release_object( process
->startup_info
);
198 process
->startup_info
= NULL
;
202 /* set the console and stdio handles for a newly created process */
203 static int set_process_console( struct process
*process
, struct thread
*parent_thread
,
204 struct startup_info
*info
, struct init_process_reply
*reply
)
206 if (process
->create_flags
& CREATE_NEW_CONSOLE
)
208 /* let the process init do the allocation */
211 else if (info
&& !(process
->create_flags
& DETACHED_PROCESS
))
213 /* FIXME: some better error checking should be done...
214 * like if hConOut and hConIn are console handles, then they should be on the same
217 inherit_console( parent_thread
, process
,
218 (info
->inherit_all
|| info
->use_handles
) ? info
->hstdin
: 0 );
222 if (!info
->inherit_all
&& !info
->use_handles
)
224 /* duplicate the handle from the parent into this process */
225 reply
->hstdin
= duplicate_handle( parent_thread
->process
, info
->hstdin
, process
,
226 0, TRUE
, DUPLICATE_SAME_ACCESS
);
227 reply
->hstdout
= duplicate_handle( parent_thread
->process
, info
->hstdout
, process
,
228 0, TRUE
, DUPLICATE_SAME_ACCESS
);
229 reply
->hstderr
= duplicate_handle( parent_thread
->process
, info
->hstderr
, process
,
230 0, TRUE
, DUPLICATE_SAME_ACCESS
);
234 reply
->hstdin
= info
->hstdin
;
235 reply
->hstdout
= info
->hstdout
;
236 reply
->hstderr
= info
->hstderr
;
241 if (process
->console
)
243 reply
->hstdin
= alloc_handle( process
, process
->console
,
244 GENERIC_READ
| GENERIC_WRITE
| SYNCHRONIZE
, 1 );
245 reply
->hstdout
= alloc_handle( process
, process
->console
->active
,
246 GENERIC_READ
| GENERIC_WRITE
, 1 );
247 reply
->hstderr
= alloc_handle( process
, process
->console
->active
,
248 GENERIC_READ
| GENERIC_WRITE
, 1 );
252 /* no parent, let the caller decide what to do */
253 reply
->hstdin
= reply
->hstdout
= reply
->hstderr
= 0;
256 /* some handles above may have been invalid; this is not an error */
257 if (get_error() == STATUS_INVALID_HANDLE
) clear_error();
261 /* create a new process and its main thread */
262 struct thread
*create_process( int fd
)
264 struct process
*process
;
265 struct thread
*thread
= NULL
;
268 if (!(process
= alloc_fd_object( &process_ops
, &process_fd_ops
, fd
))) goto error
;
269 process
->next
= NULL
;
270 process
->prev
= NULL
;
271 process
->parent
= NULL
;
272 process
->thread_list
= NULL
;
273 process
->debugger
= NULL
;
274 process
->handles
= NULL
;
275 process
->exit_code
= STILL_ACTIVE
;
276 process
->running_threads
= 0;
277 process
->priority
= NORMAL_PRIORITY_CLASS
;
278 process
->affinity
= 1;
279 process
->suspend
= 0;
280 process
->create_flags
= 0;
281 process
->console
= NULL
;
282 process
->startup_state
= STARTUP_IN_PROGRESS
;
283 process
->startup_info
= NULL
;
284 process
->idle_event
= NULL
;
285 process
->queue
= NULL
;
286 process
->atom_table
= NULL
;
287 process
->ldt_copy
= NULL
;
288 process
->exe
.next
= NULL
;
289 process
->exe
.prev
= NULL
;
290 process
->exe
.file
= NULL
;
291 process
->exe
.dbg_offset
= 0;
292 process
->exe
.dbg_size
= 0;
293 process
->exe
.namelen
= 0;
294 process
->exe
.filename
= NULL
;
295 process
->group_id
= 0;
297 gettimeofday( &process
->start_time
, NULL
);
298 if ((process
->next
= first_process
) != NULL
) process
->next
->prev
= process
;
299 first_process
= process
;
301 if (!(process
->id
= alloc_ptid( process
))) goto error
;
303 /* create the main thread */
304 if (pipe( request_pipe
) == -1)
309 if (send_client_fd( process
, request_pipe
[1], 0 ) == -1)
311 close( request_pipe
[0] );
312 close( request_pipe
[1] );
315 close( request_pipe
[1] );
316 if (!(thread
= create_thread( request_pipe
[0], process
))) goto error
;
318 set_select_events( &process
->obj
, POLLIN
); /* start listening to events */
319 release_object( process
);
323 if (process
) release_object( process
);
324 /* if we failed to start our first process, close everything down */
325 if (!running_processes
) close_master_socket();
329 /* initialize the current process and fill in the request */
330 static struct startup_info
*init_process( int ppid
, struct init_process_reply
*reply
)
332 struct process
*process
= current
->process
;
333 struct thread
*parent_thread
= get_thread_from_pid( ppid
);
334 struct process
*parent
= NULL
;
335 struct startup_info
*info
= NULL
;
339 parent
= parent_thread
->process
;
340 info
= parent_thread
->info
;
341 if (info
&& info
->thread
)
343 fatal_protocol_error( current
, "init_process: called twice?\n" );
346 process
->parent
= (struct process
*)grab_object( parent
);
349 /* set the process flags */
350 process
->create_flags
= info
? info
->create_flags
: 0;
352 /* create the handle table */
353 if (info
&& info
->inherit_all
)
354 process
->handles
= copy_handle_table( process
, parent
);
356 process
->handles
= alloc_handle_table( process
, 0 );
357 if (!process
->handles
) return NULL
;
359 /* retrieve the main exe file */
361 if (info
&& info
->exe_file
)
363 process
->exe
.file
= (struct file
*)grab_object( info
->exe_file
);
364 if (!(reply
->exe_file
= alloc_handle( process
, process
->exe
.file
, GENERIC_READ
, 0 )))
368 /* set the process console */
369 if (!set_process_console( process
, parent_thread
, info
, reply
)) return NULL
;
371 process
->group_id
= get_process_id( process
);
374 /* attach to the debugger if requested */
375 if (process
->create_flags
& (DEBUG_PROCESS
| DEBUG_ONLY_THIS_PROCESS
))
376 set_process_debugger( process
, parent_thread
);
377 else if (parent
->debugger
&& !(parent
->create_flags
& DEBUG_ONLY_THIS_PROCESS
))
378 set_process_debugger( process
, parent
->debugger
);
379 if (!(process
->create_flags
& CREATE_NEW_PROCESS_GROUP
))
380 process
->group_id
= parent
->group_id
;
383 /* thread will be actually suspended in init_done */
384 if (process
->create_flags
& CREATE_SUSPENDED
) current
->suspend
++;
388 reply
->info_size
= info
->data_size
;
389 info
->process
= (struct process
*)grab_object( process
);
390 info
->thread
= (struct thread
*)grab_object( current
);
392 reply
->create_flags
= process
->create_flags
;
393 reply
->server_start
= server_start_ticks
;
394 return info
? (struct startup_info
*)grab_object( info
) : NULL
;
397 /* destroy a process when its refcount is 0 */
398 static void process_destroy( struct object
*obj
)
400 struct process
*process
= (struct process
*)obj
;
401 assert( obj
->ops
== &process_ops
);
403 /* we can't have a thread remaining */
404 assert( !process
->thread_list
);
406 set_process_startup_state( process
, STARTUP_ABORTED
);
407 if (process
->console
) release_object( process
->console
);
408 if (process
->parent
) release_object( process
->parent
);
409 if (process
->next
) process
->next
->prev
= process
->prev
;
410 if (process
->prev
) process
->prev
->next
= process
->next
;
411 else first_process
= process
->next
;
412 if (process
->idle_event
) release_object( process
->idle_event
);
413 if (process
->queue
) release_object( process
->queue
);
414 if (process
->atom_table
) release_object( process
->atom_table
);
415 if (process
->exe
.file
) release_object( process
->exe
.file
);
416 if (process
->exe
.filename
) free( process
->exe
.filename
);
417 if (process
->id
) free_ptid( process
->id
);
420 /* dump a process on stdout for debugging purposes */
421 static void process_dump( struct object
*obj
, int verbose
)
423 struct process
*process
= (struct process
*)obj
;
424 assert( obj
->ops
== &process_ops
);
426 fprintf( stderr
, "Process id=%04x next=%p prev=%p handles=%p\n",
427 process
->id
, process
->next
, process
->prev
, process
->handles
);
430 static int process_signaled( struct object
*obj
, struct thread
*thread
)
432 struct process
*process
= (struct process
*)obj
;
433 return !process
->running_threads
;
437 static void process_poll_event( struct object
*obj
, int event
)
439 struct process
*process
= (struct process
*)obj
;
440 assert( obj
->ops
== &process_ops
);
442 if (event
& (POLLERR
| POLLHUP
)) set_select_events( obj
, -1 );
443 else if (event
& POLLIN
) receive_fd( process
);
446 static void startup_info_destroy( struct object
*obj
)
448 struct startup_info
*info
= (struct startup_info
*)obj
;
449 assert( obj
->ops
== &startup_info_ops
);
450 if (info
->data
) free( info
->data
);
451 if (info
->exe_file
) release_object( info
->exe_file
);
452 if (info
->process
) release_object( info
->process
);
453 if (info
->thread
) release_object( info
->thread
);
456 info
->owner
->info
= NULL
;
457 release_object( info
->owner
);
461 static void startup_info_dump( struct object
*obj
, int verbose
)
463 struct startup_info
*info
= (struct startup_info
*)obj
;
464 assert( obj
->ops
== &startup_info_ops
);
466 fprintf( stderr
, "Startup info flags=%x in=%p out=%p err=%p\n",
467 info
->create_flags
, info
->hstdin
, info
->hstdout
, info
->hstderr
);
470 static int startup_info_signaled( struct object
*obj
, struct thread
*thread
)
472 struct startup_info
*info
= (struct startup_info
*)obj
;
473 return info
->process
&& is_process_init_done(info
->process
);
476 /* get a process from an id (and increment the refcount) */
477 struct process
*get_process_from_id( process_id_t id
)
479 struct object
*obj
= get_ptid_entry( id
);
481 if (obj
&& obj
->ops
== &process_ops
) return (struct process
*)grab_object( obj
);
482 set_error( STATUS_INVALID_PARAMETER
);
486 /* get a process from a handle (and increment the refcount) */
487 struct process
*get_process_from_handle( obj_handle_t handle
, unsigned int access
)
489 return (struct process
*)get_handle_obj( current
->process
, handle
,
490 access
, &process_ops
);
493 /* add a dll to a process list */
494 static struct process_dll
*process_load_dll( struct process
*process
, struct file
*file
,
495 void *base
, const char *filename
, size_t name_len
)
497 struct process_dll
*dll
;
499 /* make sure we don't already have one with the same base address */
500 for (dll
= process
->exe
.next
; dll
; dll
= dll
->next
) if (dll
->base
== base
)
502 set_error( STATUS_INVALID_PARAMETER
);
506 if ((dll
= mem_alloc( sizeof(*dll
) )))
508 dll
->prev
= &process
->exe
;
511 dll
->filename
= NULL
;
512 dll
->namelen
= name_len
;
513 if (name_len
&& !(dll
->filename
= memdup( filename
, name_len
)))
518 if (file
) dll
->file
= (struct file
*)grab_object( file
);
519 if ((dll
->next
= process
->exe
.next
)) dll
->next
->prev
= dll
;
520 process
->exe
.next
= dll
;
525 /* remove a dll from a process list */
526 static void process_unload_dll( struct process
*process
, void *base
)
528 struct process_dll
*dll
;
530 for (dll
= process
->exe
.next
; dll
; dll
= dll
->next
)
532 if (dll
->base
== base
)
534 if (dll
->file
) release_object( dll
->file
);
535 if (dll
->next
) dll
->next
->prev
= dll
->prev
;
536 if (dll
->prev
) dll
->prev
->next
= dll
->next
;
537 if (dll
->filename
) free( dll
->filename
);
539 generate_debug_event( current
, UNLOAD_DLL_DEBUG_EVENT
, base
);
543 set_error( STATUS_INVALID_PARAMETER
);
546 /* kill all processes */
547 void kill_all_processes( struct process
*skip
, int exit_code
)
551 struct process
*process
= first_process
;
553 while (process
&& (!process
->running_threads
|| process
== skip
))
554 process
= process
->next
;
556 kill_process( process
, NULL
, exit_code
);
560 /* kill all processes being attached to a console renderer */
561 void kill_console_processes( struct thread
*renderer
, int exit_code
)
563 for (;;) /* restart from the beginning of the list every time */
565 struct process
*process
= first_process
;
567 /* find the first process being attached to 'renderer' and still running */
569 (process
== renderer
->process
|| !process
->console
||
570 process
->console
->renderer
!= renderer
|| !process
->running_threads
))
572 process
= process
->next
;
575 kill_process( process
, NULL
, exit_code
);
579 /* a process has been killed (i.e. its last thread died) */
580 static void process_killed( struct process
*process
)
582 assert( !process
->thread_list
);
583 gettimeofday( &process
->end_time
, NULL
);
584 if (process
->handles
) release_object( process
->handles
);
585 process
->handles
= NULL
;
587 /* close the console attached to this process, if any */
588 free_console( process
);
590 while (process
->exe
.next
)
592 struct process_dll
*dll
= process
->exe
.next
;
593 process
->exe
.next
= dll
->next
;
594 if (dll
->file
) release_object( dll
->file
);
595 if (dll
->filename
) free( dll
->filename
);
598 set_process_startup_state( process
, STARTUP_ABORTED
);
599 if (process
->exe
.file
) release_object( process
->exe
.file
);
600 process
->exe
.file
= NULL
;
601 wake_up( &process
->obj
, 0 );
602 if (!--running_processes
) close_master_socket();
605 /* add a thread to a process running threads list */
606 void add_process_thread( struct process
*process
, struct thread
*thread
)
608 thread
->proc_next
= process
->thread_list
;
609 thread
->proc_prev
= NULL
;
610 if (thread
->proc_next
) thread
->proc_next
->proc_prev
= thread
;
611 process
->thread_list
= thread
;
612 if (!process
->running_threads
++) running_processes
++;
613 grab_object( thread
);
616 /* remove a thread from a process running threads list */
617 void remove_process_thread( struct process
*process
, struct thread
*thread
)
619 assert( process
->running_threads
> 0 );
620 assert( process
->thread_list
);
622 if (thread
->proc_next
) thread
->proc_next
->proc_prev
= thread
->proc_prev
;
623 if (thread
->proc_prev
) thread
->proc_prev
->proc_next
= thread
->proc_next
;
624 else process
->thread_list
= thread
->proc_next
;
626 if (!--process
->running_threads
)
628 /* we have removed the last running thread, exit the process */
629 process
->exit_code
= thread
->exit_code
;
630 generate_debug_event( thread
, EXIT_PROCESS_DEBUG_EVENT
, process
);
631 process_killed( process
);
633 else generate_debug_event( thread
, EXIT_THREAD_DEBUG_EVENT
, thread
);
634 release_object( thread
);
637 /* suspend all the threads of a process */
638 void suspend_process( struct process
*process
)
640 if (!process
->suspend
++)
642 struct thread
*thread
= process
->thread_list
;
645 struct thread
*next
= thread
->proc_next
;
646 if (!thread
->suspend
) stop_thread( thread
);
652 /* resume all the threads of a process */
653 void resume_process( struct process
*process
)
655 assert (process
->suspend
> 0);
656 if (!--process
->suspend
)
658 struct thread
*thread
= process
->thread_list
;
661 struct thread
*next
= thread
->proc_next
;
662 if (!thread
->suspend
) continue_thread( thread
);
668 /* kill a process on the spot */
669 void kill_process( struct process
*process
, struct thread
*skip
, int exit_code
)
671 struct thread
*thread
= process
->thread_list
;
675 struct thread
*next
= thread
->proc_next
;
676 thread
->exit_code
= exit_code
;
677 if (thread
!= skip
) kill_thread( thread
, 1 );
682 /* kill all processes being debugged by a given thread */
683 void kill_debugged_processes( struct thread
*debugger
, int exit_code
)
685 for (;;) /* restart from the beginning of the list every time */
687 struct process
*process
= first_process
;
688 /* find the first process being debugged by 'debugger' and still running */
689 while (process
&& (process
->debugger
!= debugger
|| !process
->running_threads
))
690 process
= process
->next
;
691 if (!process
) return;
692 process
->debugger
= NULL
;
693 kill_process( process
, NULL
, exit_code
);
698 /* detach a debugger from all its debuggees */
699 void detach_debugged_processes( struct thread
*debugger
)
701 struct process
*process
;
702 for (process
= first_process
; process
; process
= process
->next
)
704 if (process
->debugger
== debugger
&& process
->running_threads
)
706 debugger_detach( process
, debugger
);
712 void enum_processes( int (*cb
)(struct process
*, void*), void *user
)
714 struct process
*process
;
715 for (process
= first_process
; process
; process
= process
->next
)
717 if ((cb
)(process
, user
)) break;
722 /* get all information about a process */
723 static void get_process_info( struct process
*process
, struct get_process_info_reply
*reply
)
725 reply
->pid
= get_process_id( process
);
726 reply
->debugged
= (process
->debugger
!= 0);
727 reply
->exit_code
= process
->exit_code
;
728 reply
->priority
= process
->priority
;
729 reply
->process_affinity
= process
->affinity
;
730 reply
->system_affinity
= 1;
733 /* set all information about a process */
734 static void set_process_info( struct process
*process
,
735 const struct set_process_info_request
*req
)
737 if (req
->mask
& SET_PROCESS_INFO_PRIORITY
)
738 process
->priority
= req
->priority
;
739 if (req
->mask
& SET_PROCESS_INFO_AFFINITY
)
741 if (req
->affinity
!= 1) set_error( STATUS_INVALID_PARAMETER
);
742 else process
->affinity
= req
->affinity
;
746 /* read data from a process memory space */
747 /* len is the total size (in ints) */
748 static int read_process_memory( struct process
*process
, const int *addr
, size_t len
, int *dest
)
750 struct thread
*thread
= process
->thread_list
;
752 assert( !((unsigned int)addr
% sizeof(int)) ); /* address must be aligned */
754 if (!thread
) /* process is dead */
756 set_error( STATUS_ACCESS_DENIED
);
759 if (suspend_for_ptrace( thread
))
763 if (read_thread_int( thread
, addr
++, dest
++ ) == -1) break;
766 resume_thread( thread
);
771 /* make sure we can write to the whole address range */
772 /* len is the total size (in ints) */
773 static int check_process_write_access( struct thread
*thread
, int *addr
, size_t len
)
775 int page
= get_page_size() / sizeof(int);
779 if (write_thread_int( thread
, addr
, 0, 0 ) == -1) return 0;
780 if (len
<= page
) break;
784 return (write_thread_int( thread
, addr
+ len
- 1, 0, 0 ) != -1);
787 /* write data to a process memory space */
788 /* len is the total size (in ints), max is the size we can actually read from the input buffer */
789 /* we check the total size for write permissions */
790 static void write_process_memory( struct process
*process
, int *addr
, size_t len
,
791 unsigned int first_mask
, unsigned int last_mask
, const int *src
)
793 struct thread
*thread
= process
->thread_list
;
795 assert( !((unsigned int)addr
% sizeof(int) )); /* address must be aligned */
797 if (!thread
) /* process is dead */
799 set_error( STATUS_ACCESS_DENIED
);
802 if (suspend_for_ptrace( thread
))
804 if (!check_process_write_access( thread
, addr
, len
))
806 set_error( STATUS_ACCESS_DENIED
);
809 /* first word is special */
812 if (write_thread_int( thread
, addr
++, *src
++, first_mask
) == -1) goto done
;
815 else last_mask
&= first_mask
;
819 if (write_thread_int( thread
, addr
++, *src
++, ~0 ) == -1) goto done
;
823 /* last word is special too */
824 if (write_thread_int( thread
, addr
, *src
, last_mask
) == -1) goto done
;
827 resume_thread( thread
);
831 /* take a snapshot of currently running processes */
832 struct process_snapshot
*process_snap( int *count
)
834 struct process_snapshot
*snapshot
, *ptr
;
835 struct process
*process
;
836 if (!running_processes
) return NULL
;
837 if (!(snapshot
= mem_alloc( sizeof(*snapshot
) * running_processes
)))
840 for (process
= first_process
; process
; process
= process
->next
)
842 if (!process
->running_threads
) continue;
843 ptr
->process
= process
;
844 ptr
->threads
= process
->running_threads
;
845 ptr
->count
= process
->obj
.refcount
;
846 ptr
->priority
= process
->priority
;
847 grab_object( process
);
850 *count
= running_processes
;
854 /* take a snapshot of the modules of a process */
855 struct module_snapshot
*module_snap( struct process
*process
, int *count
)
857 struct module_snapshot
*snapshot
, *ptr
;
858 struct process_dll
*dll
;
861 for (dll
= &process
->exe
; dll
; dll
= dll
->next
) total
++;
862 if (!(snapshot
= mem_alloc( sizeof(*snapshot
) * total
))) return NULL
;
864 for (ptr
= snapshot
, dll
= &process
->exe
; dll
; dll
= dll
->next
, ptr
++)
866 ptr
->base
= dll
->base
;
867 ptr
->size
= dll
->size
;
868 ptr
->namelen
= dll
->namelen
;
869 ptr
->filename
= memdup( dll
->filename
, dll
->namelen
);
876 /* create a new process */
877 DECL_HANDLER(new_process
)
879 struct startup_info
*info
;
883 fatal_protocol_error( current
, "new_process: another process is being created\n" );
887 /* build the startup info for a new process */
888 if (!(info
= alloc_object( &startup_info_ops
, -1 ))) return;
889 info
->inherit_all
= req
->inherit_all
;
890 info
->use_handles
= req
->use_handles
;
891 info
->create_flags
= req
->create_flags
;
892 info
->hstdin
= req
->hstdin
;
893 info
->hstdout
= req
->hstdout
;
894 info
->hstderr
= req
->hstderr
;
895 info
->exe_file
= NULL
;
896 info
->owner
= (struct thread
*)grab_object( current
);
897 info
->process
= NULL
;
899 info
->data_size
= get_req_data_size();
903 !(info
->exe_file
= get_file_obj( current
->process
, req
->exe_file
, GENERIC_READ
)))
906 if (!(info
->data
= mem_alloc( info
->data_size
))) goto done
;
907 memcpy( info
->data
, get_req_data(), info
->data_size
);
908 current
->info
= info
;
909 reply
->info
= alloc_handle( current
->process
, info
, SYNCHRONIZE
, FALSE
);
912 release_object( info
);
915 /* Retrieve information about a newly started process */
916 DECL_HANDLER(get_new_process_info
)
918 struct startup_info
*info
;
920 if ((info
= (struct startup_info
*)get_handle_obj( current
->process
, req
->info
,
921 0, &startup_info_ops
)))
923 reply
->pid
= get_process_id( info
->process
);
924 reply
->tid
= get_thread_id( info
->thread
);
925 reply
->phandle
= alloc_handle( current
->process
, info
->process
,
926 PROCESS_ALL_ACCESS
, req
->pinherit
);
927 reply
->thandle
= alloc_handle( current
->process
, info
->thread
,
928 THREAD_ALL_ACCESS
, req
->tinherit
);
929 reply
->success
= is_process_init_done( info
->process
);
930 release_object( info
);
942 /* Retrieve the new process startup info */
943 DECL_HANDLER(get_startup_info
)
945 struct startup_info
*info
;
947 if ((info
= current
->process
->startup_info
))
949 size_t size
= info
->data_size
;
950 if (size
> get_reply_max_size()) size
= get_reply_max_size();
952 /* we return the data directly without making a copy so this can only be called once */
953 set_reply_data_ptr( info
->data
, size
);
960 /* initialize a new process */
961 DECL_HANDLER(init_process
)
963 if (!current
->unix_pid
)
965 fatal_protocol_error( current
, "init_process: init_thread not called yet\n" );
968 if (current
->process
->startup_info
)
970 fatal_protocol_error( current
, "init_process: called twice\n" );
973 reply
->info_size
= 0;
974 current
->process
->ldt_copy
= req
->ldt_copy
;
975 current
->process
->startup_info
= init_process( req
->ppid
, 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 reply
->debugged
= (process
->debugger
!= 0);
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 get_process_info( process
, reply
);
1046 release_object( process
);
1050 /* set information about a process */
1051 DECL_HANDLER(set_process_info
)
1053 struct process
*process
;
1055 if ((process
= get_process_from_handle( req
->handle
, PROCESS_SET_INFORMATION
)))
1057 set_process_info( process
, req
);
1058 release_object( process
);
1062 /* read data from a process address space */
1063 DECL_HANDLER(read_process_memory
)
1065 struct process
*process
;
1066 size_t len
= get_reply_max_size();
1068 if (!(process
= get_process_from_handle( req
->handle
, PROCESS_VM_READ
))) return;
1072 unsigned int start_offset
= (unsigned int)req
->addr
% sizeof(int);
1073 unsigned int nb_ints
= (len
+ start_offset
+ sizeof(int) - 1) / sizeof(int);
1074 const int *start
= (int *)((char *)req
->addr
- start_offset
);
1075 int *buffer
= mem_alloc( nb_ints
* sizeof(int) );
1078 if (read_process_memory( process
, start
, nb_ints
, buffer
))
1080 /* move start of requested data to start of buffer */
1081 if (start_offset
) memmove( buffer
, (char *)buffer
+ start_offset
, len
);
1082 set_reply_data_ptr( buffer
, len
);
1087 release_object( process
);
1090 /* write data to a process address space */
1091 DECL_HANDLER(write_process_memory
)
1093 struct process
*process
;
1095 if ((process
= get_process_from_handle( req
->handle
, PROCESS_VM_WRITE
)))
1097 size_t len
= get_req_data_size();
1098 if ((len
% sizeof(int)) || ((unsigned int)req
->addr
% sizeof(int)))
1099 set_error( STATUS_INVALID_PARAMETER
);
1102 if (len
) write_process_memory( process
, req
->addr
, len
/ sizeof(int),
1103 req
->first_mask
, req
->last_mask
, get_req_data() );
1105 release_object( process
);
1109 /* notify the server that a dll has been loaded */
1110 DECL_HANDLER(load_dll
)
1112 struct process_dll
*dll
;
1113 struct file
*file
= NULL
;
1116 !(file
= get_file_obj( current
->process
, req
->handle
, GENERIC_READ
))) return;
1118 if ((dll
= process_load_dll( current
->process
, file
, req
->base
,
1119 get_req_data(), get_req_data_size() )))
1121 dll
->size
= req
->size
;
1122 dll
->dbg_offset
= req
->dbg_offset
;
1123 dll
->dbg_size
= req
->dbg_size
;
1124 dll
->name
= req
->name
;
1125 /* only generate event if initialization is done */
1126 if (is_process_init_done( current
->process
))
1127 generate_debug_event( current
, LOAD_DLL_DEBUG_EVENT
, dll
);
1129 if (file
) release_object( file
);
1132 /* notify the server that a dll is being unloaded */
1133 DECL_HANDLER(unload_dll
)
1135 process_unload_dll( current
->process
, req
->base
);
1138 /* retrieve information about a module in a process */
1139 DECL_HANDLER(get_dll_info
)
1141 struct process
*process
;
1143 if ((process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_INFORMATION
)))
1145 struct process_dll
*dll
;
1147 for (dll
= &process
->exe
; dll
; dll
= dll
->next
)
1149 if (dll
->base
== req
->base_address
)
1151 reply
->size
= dll
->size
;
1152 reply
->entry_point
= 0; /* FIXME */
1155 size_t len
= min( dll
->namelen
, get_reply_max_size() );
1156 set_reply_data( dll
->filename
, len
);
1162 set_error(STATUS_DLL_NOT_FOUND
);
1163 release_object( process
);
1167 /* wait for a process to start waiting on input */
1168 /* FIXME: only returns event for now, wait is done in the client */
1169 DECL_HANDLER(wait_input_idle
)
1171 struct process
*process
;
1174 if ((process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_INFORMATION
)))
1176 if (process
->idle_event
&& process
!= current
->process
&& process
->queue
!= current
->queue
)
1177 reply
->event
= alloc_handle( current
->process
, process
->idle_event
,
1178 EVENT_ALL_ACCESS
, 0 );
1179 release_object( process
);