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>
41 #define WIN32_NO_STATUS
53 /* process structure */
55 static struct list process_list
= LIST_INIT(process_list
);
56 static int running_processes
;
58 /* process operations */
60 static void process_dump( struct object
*obj
, int verbose
);
61 static int process_signaled( struct object
*obj
, struct thread
*thread
);
62 static unsigned int process_map_access( struct object
*obj
, unsigned int access
);
63 static void process_poll_event( struct fd
*fd
, int event
);
64 static void process_destroy( struct object
*obj
);
66 static const struct object_ops process_ops
=
68 sizeof(struct process
), /* size */
69 process_dump
, /* dump */
70 add_queue
, /* add_queue */
71 remove_queue
, /* remove_queue */
72 process_signaled
, /* signaled */
73 no_satisfied
, /* satisfied */
74 no_signal
, /* signal */
75 no_get_fd
, /* get_fd */
76 process_map_access
, /* map_access */
77 no_lookup_name
, /* lookup_name */
78 no_close_handle
, /* close_handle */
79 process_destroy
/* destroy */
82 static const struct fd_ops process_fd_ops
=
84 NULL
, /* get_poll_events */
85 process_poll_event
, /* poll_event */
87 no_get_file_info
, /* get_file_info */
88 no_queue_async
, /* queue_async */
89 no_cancel_async
/* cancel async */
92 /* process startup info */
96 struct object obj
; /* object header */
97 struct list entry
; /* entry in list of startup infos */
98 int inherit_all
; /* inherit all handles from parent */
99 unsigned int create_flags
; /* creation flags */
100 int unix_pid
; /* Unix pid of new process */
101 obj_handle_t hstdin
; /* handle for stdin */
102 obj_handle_t hstdout
; /* handle for stdout */
103 obj_handle_t hstderr
; /* handle for stderr */
104 struct file
*exe_file
; /* file handle for main exe */
105 struct thread
*owner
; /* owner thread (the one that created the new process) */
106 struct process
*process
; /* created process */
107 struct thread
*thread
; /* created thread */
108 size_t data_size
; /* size of startup data */
109 void *data
; /* data for startup info */
112 static void startup_info_dump( struct object
*obj
, int verbose
);
113 static int startup_info_signaled( struct object
*obj
, struct thread
*thread
);
114 static void startup_info_destroy( struct object
*obj
);
116 static const struct object_ops startup_info_ops
=
118 sizeof(struct startup_info
), /* size */
119 startup_info_dump
, /* dump */
120 add_queue
, /* add_queue */
121 remove_queue
, /* remove_queue */
122 startup_info_signaled
, /* signaled */
123 no_satisfied
, /* satisfied */
124 no_signal
, /* signal */
125 no_get_fd
, /* get_fd */
126 no_map_access
, /* map_access */
127 no_lookup_name
, /* lookup_name */
128 no_close_handle
, /* close_handle */
129 startup_info_destroy
/* destroy */
133 static struct list startup_info_list
= LIST_INIT(startup_info_list
);
137 void *ptr
; /* entry ptr */
138 unsigned int next
; /* next free entry */
141 static struct ptid_entry
*ptid_entries
; /* array of ptid entries */
142 static unsigned int used_ptid_entries
; /* number of entries in use */
143 static unsigned int alloc_ptid_entries
; /* number of allocated entries */
144 static unsigned int next_free_ptid
; /* next free entry */
145 static unsigned int last_free_ptid
; /* last free entry */
147 #define PTID_OFFSET 8 /* offset for first ptid value */
149 /* allocate a new process or thread id */
150 unsigned int alloc_ptid( void *ptr
)
152 struct ptid_entry
*entry
;
155 if (used_ptid_entries
< alloc_ptid_entries
)
157 id
= used_ptid_entries
+ PTID_OFFSET
;
158 entry
= &ptid_entries
[used_ptid_entries
++];
160 else if (next_free_ptid
)
163 entry
= &ptid_entries
[id
- PTID_OFFSET
];
164 if (!(next_free_ptid
= entry
->next
)) last_free_ptid
= 0;
166 else /* need to grow the array */
168 unsigned int count
= alloc_ptid_entries
+ (alloc_ptid_entries
/ 2);
169 if (!count
) count
= 64;
170 if (!(entry
= realloc( ptid_entries
, count
* sizeof(*entry
) )))
172 set_error( STATUS_NO_MEMORY
);
175 ptid_entries
= entry
;
176 alloc_ptid_entries
= count
;
177 id
= used_ptid_entries
+ PTID_OFFSET
;
178 entry
= &ptid_entries
[used_ptid_entries
++];
185 /* free a process or thread id */
186 void free_ptid( unsigned int id
)
188 struct ptid_entry
*entry
= &ptid_entries
[id
- PTID_OFFSET
];
193 /* append to end of free list so that we don't reuse it too early */
194 if (last_free_ptid
) ptid_entries
[last_free_ptid
- PTID_OFFSET
].next
= id
;
195 else next_free_ptid
= id
;
200 /* retrieve the pointer corresponding to a process or thread id */
201 void *get_ptid_entry( unsigned int id
)
203 if (id
< PTID_OFFSET
) return NULL
;
204 if (id
- PTID_OFFSET
>= used_ptid_entries
) return NULL
;
205 return ptid_entries
[id
- PTID_OFFSET
].ptr
;
208 /* return the main thread of the process */
209 struct thread
*get_process_first_thread( struct process
*process
)
211 struct list
*ptr
= list_head( &process
->thread_list
);
212 if (!ptr
) return NULL
;
213 return LIST_ENTRY( ptr
, struct thread
, proc_entry
);
216 /* set the state of the process startup info */
217 static void set_process_startup_state( struct process
*process
, enum startup_state state
)
219 if (process
->startup_state
== STARTUP_IN_PROGRESS
) process
->startup_state
= state
;
220 if (process
->startup_info
)
222 wake_up( &process
->startup_info
->obj
, 0 );
223 release_object( process
->startup_info
);
224 process
->startup_info
= NULL
;
228 /* create a new process and its main thread */
229 struct thread
*create_process( int fd
)
231 struct process
*process
;
232 struct thread
*thread
= NULL
;
235 if (!(process
= alloc_object( &process_ops
))) goto error
;
236 process
->parent
= NULL
;
237 process
->debugger
= NULL
;
238 process
->handles
= NULL
;
239 process
->msg_fd
= NULL
;
240 process
->exit_code
= STILL_ACTIVE
;
241 process
->running_threads
= 0;
242 process
->priority
= PROCESS_PRIOCLASS_NORMAL
;
243 process
->affinity
= 1;
244 process
->suspend
= 0;
245 process
->create_flags
= 0;
246 process
->console
= NULL
;
247 process
->startup_state
= STARTUP_IN_PROGRESS
;
248 process
->startup_info
= NULL
;
249 process
->idle_event
= NULL
;
250 process
->queue
= NULL
;
252 process
->ldt_copy
= NULL
;
253 process
->winstation
= 0;
254 process
->desktop
= 0;
255 process
->exe
.file
= NULL
;
256 process
->exe
.dbg_offset
= 0;
257 process
->exe
.dbg_size
= 0;
258 process
->exe
.namelen
= 0;
259 process
->exe
.filename
= NULL
;
260 process
->token
= token_create_admin();
261 list_init( &process
->thread_list
);
262 list_init( &process
->locks
);
263 list_init( &process
->classes
);
264 list_init( &process
->dlls
);
266 gettimeofday( &process
->start_time
, NULL
);
267 list_add_head( &process_list
, &process
->entry
);
269 if (!(process
->id
= process
->group_id
= alloc_ptid( process
))) goto error
;
270 if (!(process
->msg_fd
= create_anonymous_fd( &process_fd_ops
, fd
, &process
->obj
))) goto error
;
272 /* create the main thread */
273 if (pipe( request_pipe
) == -1)
278 if (send_client_fd( process
, request_pipe
[1], 0 ) == -1)
280 close( request_pipe
[0] );
281 close( request_pipe
[1] );
284 close( request_pipe
[1] );
285 if (!(thread
= create_thread( request_pipe
[0], process
))) goto error
;
287 set_fd_events( process
->msg_fd
, POLLIN
); /* start listening to events */
288 release_object( process
);
292 if (process
) release_object( process
);
293 /* if we failed to start our first process, close everything down */
294 if (!running_processes
) close_master_socket();
298 /* find the startup info for a given Unix process */
299 inline static struct startup_info
*find_startup_info( int unix_pid
)
303 LIST_FOR_EACH( ptr
, &startup_info_list
)
305 struct startup_info
*info
= LIST_ENTRY( ptr
, struct startup_info
, entry
);
306 if (info
->unix_pid
== unix_pid
) return info
;
311 /* initialize the current process and fill in the request */
312 size_t init_process( struct thread
*thread
)
314 struct process
*process
= thread
->process
;
315 struct thread
*parent_thread
= NULL
;
316 struct process
*parent
= NULL
;
317 struct startup_info
*info
;
319 if (process
->startup_info
) return process
->startup_info
->data_size
; /* already initialized */
321 if ((info
= find_startup_info( thread
->unix_pid
)))
323 if (info
->thread
) return info
->data_size
; /* already initialized */
325 info
->thread
= (struct thread
*)grab_object( thread
);
326 info
->process
= (struct process
*)grab_object( process
);
327 process
->startup_info
= (struct startup_info
*)grab_object( info
);
329 parent_thread
= info
->owner
;
330 parent
= parent_thread
->process
;
331 process
->parent
= (struct process
*)grab_object( parent
);
333 /* set the process flags */
334 process
->create_flags
= info
->create_flags
;
336 if (info
->inherit_all
) process
->handles
= copy_handle_table( process
, parent
);
339 /* create the handle table */
340 if (!process
->handles
) process
->handles
= alloc_handle_table( process
, 0 );
341 if (!process
->handles
)
343 fatal_protocol_error( thread
, "Failed to allocate handle table\n" );
347 /* connect to the window station and desktop */
348 connect_process_winstation( process
, NULL
);
349 connect_process_desktop( process
, NULL
);
350 thread
->desktop
= process
->desktop
;
354 /* thread will be actually suspended in init_done */
355 if (info
->create_flags
& CREATE_SUSPENDED
) thread
->suspend
++;
357 /* set the process console */
358 if (!(info
->create_flags
& (DETACHED_PROCESS
| CREATE_NEW_CONSOLE
)))
360 /* FIXME: some better error checking should be done...
361 * like if hConOut and hConIn are console handles, then they should be on the same
364 inherit_console( parent_thread
, process
, info
->inherit_all
? info
->hstdin
: 0 );
367 /* attach to the debugger if requested */
368 if (process
->create_flags
& (DEBUG_PROCESS
| DEBUG_ONLY_THIS_PROCESS
))
369 set_process_debugger( process
, parent_thread
);
370 else if (parent
->debugger
&& !(parent
->create_flags
& DEBUG_ONLY_THIS_PROCESS
))
371 set_process_debugger( process
, parent
->debugger
);
373 if (!(process
->create_flags
& CREATE_NEW_PROCESS_GROUP
))
374 process
->group_id
= parent
->group_id
;
376 return info
->data_size
;
379 /* destroy a process when its refcount is 0 */
380 static void process_destroy( struct object
*obj
)
382 struct process
*process
= (struct process
*)obj
;
383 assert( obj
->ops
== &process_ops
);
385 /* we can't have a thread remaining */
386 assert( list_empty( &process
->thread_list
));
388 set_process_startup_state( process
, STARTUP_ABORTED
);
389 if (process
->console
) release_object( process
->console
);
390 if (process
->parent
) release_object( process
->parent
);
391 if (process
->msg_fd
) release_object( process
->msg_fd
);
392 list_remove( &process
->entry
);
393 if (process
->idle_event
) release_object( process
->idle_event
);
394 if (process
->queue
) release_object( process
->queue
);
395 if (process
->exe
.file
) release_object( process
->exe
.file
);
396 if (process
->exe
.filename
) free( process
->exe
.filename
);
397 if (process
->id
) free_ptid( process
->id
);
398 if (process
->token
) release_object( process
->token
);
401 /* dump a process on stdout for debugging purposes */
402 static void process_dump( struct object
*obj
, int verbose
)
404 struct process
*process
= (struct process
*)obj
;
405 assert( obj
->ops
== &process_ops
);
407 fprintf( stderr
, "Process id=%04x handles=%p\n", process
->id
, process
->handles
);
410 static int process_signaled( struct object
*obj
, struct thread
*thread
)
412 struct process
*process
= (struct process
*)obj
;
413 return !process
->running_threads
;
416 static unsigned int process_map_access( struct object
*obj
, unsigned int access
)
418 if (access
& GENERIC_READ
) access
|= STANDARD_RIGHTS_READ
| SYNCHRONIZE
;
419 if (access
& GENERIC_WRITE
) access
|= STANDARD_RIGHTS_WRITE
| SYNCHRONIZE
;
420 if (access
& GENERIC_EXECUTE
) access
|= STANDARD_RIGHTS_EXECUTE
;
421 if (access
& GENERIC_ALL
) access
|= PROCESS_ALL_ACCESS
;
422 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
425 static void process_poll_event( struct fd
*fd
, int event
)
427 struct process
*process
= get_fd_user( fd
);
428 assert( process
->obj
.ops
== &process_ops
);
430 if (event
& (POLLERR
| POLLHUP
)) set_fd_events( fd
, -1 );
431 else if (event
& POLLIN
) receive_fd( process
);
434 static void startup_info_destroy( struct object
*obj
)
436 struct startup_info
*info
= (struct startup_info
*)obj
;
437 assert( obj
->ops
== &startup_info_ops
);
438 list_remove( &info
->entry
);
439 if (info
->data
) free( info
->data
);
440 if (info
->exe_file
) release_object( info
->exe_file
);
441 if (info
->process
) release_object( info
->process
);
442 if (info
->thread
) release_object( info
->thread
);
443 if (info
->owner
) release_object( info
->owner
);
446 static void startup_info_dump( struct object
*obj
, int verbose
)
448 struct startup_info
*info
= (struct startup_info
*)obj
;
449 assert( obj
->ops
== &startup_info_ops
);
451 fprintf( stderr
, "Startup info flags=%x in=%p out=%p err=%p\n",
452 info
->create_flags
, info
->hstdin
, info
->hstdout
, info
->hstderr
);
455 static int startup_info_signaled( struct object
*obj
, struct thread
*thread
)
457 struct startup_info
*info
= (struct startup_info
*)obj
;
458 return info
->process
&& is_process_init_done(info
->process
);
461 /* get a process from an id (and increment the refcount) */
462 struct process
*get_process_from_id( process_id_t id
)
464 struct object
*obj
= get_ptid_entry( id
);
466 if (obj
&& obj
->ops
== &process_ops
) return (struct process
*)grab_object( obj
);
467 set_error( STATUS_INVALID_PARAMETER
);
471 /* get a process from a handle (and increment the refcount) */
472 struct process
*get_process_from_handle( obj_handle_t handle
, unsigned int access
)
474 return (struct process
*)get_handle_obj( current
->process
, handle
,
475 access
, &process_ops
);
478 /* find a dll from its base address */
479 static inline struct process_dll
*find_process_dll( struct process
*process
, void *base
)
481 struct process_dll
*dll
;
483 if (process
->exe
.base
== base
) return &process
->exe
;
485 LIST_FOR_EACH_ENTRY( dll
, &process
->dlls
, struct process_dll
, entry
)
487 if (dll
->base
== base
) return dll
;
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 WCHAR
*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 if (find_process_dll( process
, base
))
501 set_error( STATUS_INVALID_PARAMETER
);
505 if ((dll
= mem_alloc( sizeof(*dll
) )))
509 dll
->filename
= NULL
;
510 dll
->namelen
= name_len
;
511 if (name_len
&& !(dll
->filename
= memdup( filename
, name_len
)))
516 if (file
) dll
->file
= (struct file
*)grab_object( file
);
517 list_add_head( &process
->dlls
, &dll
->entry
);
522 /* remove a dll from a process list */
523 static void process_unload_dll( struct process
*process
, void *base
)
525 struct process_dll
*dll
= find_process_dll( process
, base
);
527 if (dll
&& dll
!= &process
->exe
)
529 if (dll
->file
) release_object( dll
->file
);
530 if (dll
->filename
) free( dll
->filename
);
531 list_remove( &dll
->entry
);
533 generate_debug_event( current
, UNLOAD_DLL_DEBUG_EVENT
, base
);
535 else set_error( STATUS_INVALID_PARAMETER
);
538 /* kill all processes */
539 void kill_all_processes( struct process
*skip
, int exit_code
)
543 struct process
*process
;
545 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
547 if (process
== skip
) continue;
548 if (process
->running_threads
) break;
550 if (&process
->entry
== &process_list
) break; /* no process found */
551 kill_process( process
, NULL
, exit_code
);
555 /* kill all processes being attached to a console renderer */
556 void kill_console_processes( struct thread
*renderer
, int exit_code
)
558 for (;;) /* restart from the beginning of the list every time */
560 struct process
*process
;
562 /* find the first process being attached to 'renderer' and still running */
563 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
565 if (process
== renderer
->process
) continue;
566 if (!process
->running_threads
) continue;
567 if (process
->console
&& process
->console
->renderer
== renderer
) break;
569 if (&process
->entry
== &process_list
) break; /* no process found */
570 kill_process( process
, NULL
, exit_code
);
574 /* a process has been killed (i.e. its last thread died) */
575 static void process_killed( struct process
*process
)
577 struct handle_table
*handles
;
580 assert( list_empty( &process
->thread_list
));
581 gettimeofday( &process
->end_time
, NULL
);
582 handles
= process
->handles
;
583 process
->handles
= NULL
;
584 if (handles
) release_object( handles
);
586 /* close the console attached to this process, if any */
587 free_console( process
);
589 while ((ptr
= list_head( &process
->dlls
)))
591 struct process_dll
*dll
= LIST_ENTRY( ptr
, struct process_dll
, entry
);
592 if (dll
->file
) release_object( dll
->file
);
593 if (dll
->filename
) free( dll
->filename
);
594 list_remove( &dll
->entry
);
597 destroy_process_classes( process
);
598 remove_process_locks( process
);
599 set_process_startup_state( process
, STARTUP_ABORTED
);
600 if (process
->exe
.file
) release_object( process
->exe
.file
);
601 process
->exe
.file
= NULL
;
602 wake_up( &process
->obj
, 0 );
603 if (!--running_processes
) close_master_socket();
606 /* add a thread to a process running threads list */
607 void add_process_thread( struct process
*process
, struct thread
*thread
)
609 list_add_head( &process
->thread_list
, &thread
->proc_entry
);
610 if (!process
->running_threads
++) running_processes
++;
611 grab_object( thread
);
614 /* remove a thread from a process running threads list */
615 void remove_process_thread( struct process
*process
, struct thread
*thread
)
617 assert( process
->running_threads
> 0 );
618 assert( !list_empty( &process
->thread_list
));
620 list_remove( &thread
->proc_entry
);
622 if (!--process
->running_threads
)
624 /* we have removed the last running thread, exit the process */
625 process
->exit_code
= thread
->exit_code
;
626 generate_debug_event( thread
, EXIT_PROCESS_DEBUG_EVENT
, process
);
627 process_killed( process
);
629 else generate_debug_event( thread
, EXIT_THREAD_DEBUG_EVENT
, thread
);
630 release_object( thread
);
633 /* suspend all the threads of a process */
634 void suspend_process( struct process
*process
)
636 if (!process
->suspend
++)
638 struct list
*ptr
, *next
;
640 LIST_FOR_EACH_SAFE( ptr
, next
, &process
->thread_list
)
642 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
643 if (!thread
->suspend
) stop_thread( thread
);
648 /* resume all the threads of a process */
649 void resume_process( struct process
*process
)
651 assert (process
->suspend
> 0);
652 if (!--process
->suspend
)
654 struct list
*ptr
, *next
;
656 LIST_FOR_EACH_SAFE( ptr
, next
, &process
->thread_list
)
658 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
659 if (!thread
->suspend
) wake_thread( thread
);
664 /* kill a process on the spot */
665 void kill_process( struct process
*process
, struct thread
*skip
, int exit_code
)
667 struct list
*ptr
, *next
;
669 LIST_FOR_EACH_SAFE( ptr
, next
, &process
->thread_list
)
671 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
673 thread
->exit_code
= exit_code
;
674 if (thread
!= skip
) kill_thread( thread
, 1 );
678 /* kill all processes being debugged by a given thread */
679 void kill_debugged_processes( struct thread
*debugger
, int exit_code
)
681 for (;;) /* restart from the beginning of the list every time */
683 struct process
*process
;
685 /* find the first process being debugged by 'debugger' and still running */
686 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
688 if (!process
->running_threads
) continue;
689 if (process
->debugger
== debugger
) break;
691 if (&process
->entry
== &process_list
) break; /* no process found */
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
;
703 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
705 if (process
->debugger
== debugger
&& process
->running_threads
)
707 debugger_detach( process
, debugger
);
713 void enum_processes( int (*cb
)(struct process
*, void*), void *user
)
715 struct list
*ptr
, *next
;
717 LIST_FOR_EACH_SAFE( ptr
, next
, &process_list
)
719 struct process
*process
= LIST_ENTRY( ptr
, struct process
, entry
);
720 if ((cb
)(process
, user
)) break;
725 /* read data from a process memory space */
726 /* len is the total size (in ints) */
727 static int read_process_memory( struct process
*process
, const int *addr
, size_t len
, int *dest
)
729 struct thread
*thread
= get_process_first_thread( process
);
731 assert( !((unsigned int)addr
% sizeof(int)) ); /* address must be aligned */
733 if (!thread
) /* process is dead */
735 set_error( STATUS_ACCESS_DENIED
);
738 if (suspend_for_ptrace( thread
))
742 if (read_thread_int( thread
, addr
++, dest
++ ) == -1) break;
745 resume_after_ptrace( thread
);
750 /* make sure we can write to the whole address range */
751 /* len is the total size (in ints) */
752 static int check_process_write_access( struct thread
*thread
, int *addr
, size_t len
)
754 int page
= get_page_size() / sizeof(int);
758 if (write_thread_int( thread
, addr
, 0, 0 ) == -1) return 0;
759 if (len
<= page
) break;
763 return (write_thread_int( thread
, addr
+ len
- 1, 0, 0 ) != -1);
766 /* write data to a process memory space */
767 /* len is the total size (in ints), max is the size we can actually read from the input buffer */
768 /* we check the total size for write permissions */
769 static int write_process_memory( struct process
*process
, int *addr
, size_t len
,
770 unsigned int first_mask
, unsigned int last_mask
, const int *src
)
772 struct thread
*thread
= get_process_first_thread( process
);
775 assert( !((unsigned int)addr
% sizeof(int) )); /* address must be aligned */
777 if (!thread
) /* process is dead */
779 set_error( STATUS_ACCESS_DENIED
);
782 if (suspend_for_ptrace( thread
))
784 if (!check_process_write_access( thread
, addr
, len
))
786 set_error( STATUS_ACCESS_DENIED
);
789 /* first word is special */
792 if (write_thread_int( thread
, addr
++, *src
++, first_mask
) == -1) goto done
;
795 else last_mask
&= first_mask
;
799 if (write_thread_int( thread
, addr
++, *src
++, ~0 ) == -1) goto done
;
803 /* last word is special too */
804 if (write_thread_int( thread
, addr
, *src
, last_mask
) == -1) goto done
;
808 resume_after_ptrace( thread
);
813 /* set the debugged flag in the process PEB */
814 int set_process_debug_flag( struct process
*process
, int flag
)
816 int mask
= 0, data
= 0;
818 /* BeingDebugged flag is the byte at offset 2 in the PEB */
819 memset( (char *)&mask
+ 2, 0xff, 1 );
820 memset( (char *)&data
+ 2, flag
, 1 );
821 return write_process_memory( process
, process
->peb
, 1, mask
, mask
, &data
);
824 /* take a snapshot of currently running processes */
825 struct process_snapshot
*process_snap( int *count
)
827 struct process_snapshot
*snapshot
, *ptr
;
828 struct process
*process
;
829 if (!running_processes
) return NULL
;
830 if (!(snapshot
= mem_alloc( sizeof(*snapshot
) * running_processes
)))
833 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
835 if (!process
->running_threads
) continue;
836 ptr
->process
= process
;
837 ptr
->threads
= process
->running_threads
;
838 ptr
->count
= process
->obj
.refcount
;
839 ptr
->priority
= process
->priority
;
840 ptr
->handles
= get_handle_table_count(process
);
841 grab_object( process
);
844 *count
= running_processes
;
848 /* take a snapshot of the modules of a process */
849 struct module_snapshot
*module_snap( struct process
*process
, int *count
)
851 struct module_snapshot
*snapshot
, *ptr
;
852 struct process_dll
*dll
;
855 LIST_FOR_EACH_ENTRY( dll
, &process
->dlls
, struct process_dll
, entry
) total
++;
856 if (!(snapshot
= mem_alloc( sizeof(*snapshot
) * total
))) return NULL
;
858 /* first entry is main exe */
859 snapshot
->base
= process
->exe
.base
;
860 snapshot
->size
= process
->exe
.size
;
861 snapshot
->namelen
= process
->exe
.namelen
;
862 snapshot
->filename
= memdup( process
->exe
.filename
, process
->exe
.namelen
);
865 LIST_FOR_EACH_ENTRY( dll
, &process
->dlls
, struct process_dll
, entry
)
867 ptr
->base
= dll
->base
;
868 ptr
->size
= dll
->size
;
869 ptr
->namelen
= dll
->namelen
;
870 ptr
->filename
= memdup( dll
->filename
, dll
->namelen
);
878 /* create a new process */
879 DECL_HANDLER(new_process
)
881 struct startup_info
*info
;
883 /* build the startup info for a new process */
884 if (!(info
= alloc_object( &startup_info_ops
))) return;
885 list_add_head( &startup_info_list
, &info
->entry
);
886 info
->inherit_all
= req
->inherit_all
;
887 info
->create_flags
= req
->create_flags
;
888 info
->unix_pid
= req
->unix_pid
;
889 info
->hstdin
= req
->hstdin
;
890 info
->hstdout
= req
->hstdout
;
891 info
->hstderr
= req
->hstderr
;
892 info
->exe_file
= NULL
;
893 info
->owner
= (struct thread
*)grab_object( current
);
894 info
->process
= NULL
;
896 info
->data_size
= get_req_data_size();
900 !(info
->exe_file
= get_file_obj( current
->process
, req
->exe_file
, FILE_READ_DATA
)))
903 if (!(info
->data
= memdup( get_req_data(), info
->data_size
))) goto done
;
904 reply
->info
= alloc_handle( current
->process
, info
, SYNCHRONIZE
, 0 );
907 release_object( info
);
910 /* Retrieve information about a newly started process */
911 DECL_HANDLER(get_new_process_info
)
913 struct startup_info
*info
;
915 if ((info
= (struct startup_info
*)get_handle_obj( current
->process
, req
->info
,
916 0, &startup_info_ops
)))
918 reply
->pid
= get_process_id( info
->process
);
919 reply
->tid
= get_thread_id( info
->thread
);
920 reply
->phandle
= alloc_handle( current
->process
, info
->process
,
921 req
->process_access
, req
->process_attr
);
922 reply
->thandle
= alloc_handle( current
->process
, info
->thread
,
923 req
->thread_access
, req
->thread_attr
);
924 reply
->success
= is_process_init_done( info
->process
);
925 release_object( info
);
937 /* Retrieve the new process startup info */
938 DECL_HANDLER(get_startup_info
)
940 struct process
*process
= current
->process
;
941 struct startup_info
*info
= process
->startup_info
;
946 if (info
->exe_file
&&
947 !(reply
->exe_file
= alloc_handle( process
, info
->exe_file
, GENERIC_READ
, 0 ))) return;
949 if (!info
->inherit_all
&& !(info
->create_flags
& CREATE_NEW_CONSOLE
))
951 struct process
*parent_process
= info
->owner
->process
;
952 reply
->hstdin
= duplicate_handle( parent_process
, info
->hstdin
, process
,
953 0, OBJ_INHERIT
, DUPLICATE_SAME_ACCESS
);
954 reply
->hstdout
= duplicate_handle( parent_process
, info
->hstdout
, process
,
955 0, OBJ_INHERIT
, DUPLICATE_SAME_ACCESS
);
956 reply
->hstderr
= duplicate_handle( parent_process
, info
->hstderr
, process
,
957 0, OBJ_INHERIT
, DUPLICATE_SAME_ACCESS
);
958 /* some handles above may have been invalid; this is not an error */
959 if (get_error() == STATUS_INVALID_HANDLE
||
960 get_error() == STATUS_OBJECT_TYPE_MISMATCH
) clear_error();
964 reply
->hstdin
= info
->hstdin
;
965 reply
->hstdout
= info
->hstdout
;
966 reply
->hstderr
= info
->hstderr
;
969 /* we return the data directly without making a copy so this can only be called once */
970 size
= info
->data_size
;
971 if (size
> get_reply_max_size()) size
= get_reply_max_size();
972 set_reply_data_ptr( info
->data
, size
);
977 /* signal the end of the process initialization */
978 DECL_HANDLER(init_process_done
)
980 struct process_dll
*dll
;
981 struct process
*process
= current
->process
;
983 if (is_process_init_done(process
))
985 fatal_protocol_error( current
, "init_process_done: called twice\n" );
990 fatal_protocol_error( current
, "init_process_done: module base address cannot be 0\n" );
994 /* check if main exe has been registered as a dll already */
995 if ((dll
= find_process_dll( process
, req
->module
)))
997 list_remove( &dll
->entry
);
998 memcpy( &process
->exe
, dll
, sizeof(*dll
) );
999 list_init( &process
->exe
.entry
);
1004 process
->exe
.base
= req
->module
;
1005 process
->exe
.size
= req
->module_size
;
1006 process
->exe
.name
= req
->name
;
1007 if ((process
->exe
.namelen
= get_req_data_size()))
1008 process
->exe
.filename
= memdup( get_req_data(), process
->exe
.namelen
);
1011 generate_startup_debug_events( process
, req
->entry
);
1012 set_process_startup_state( process
, STARTUP_DONE
);
1014 if (req
->gui
) process
->idle_event
= create_event( NULL
, NULL
, 0, 1, 0 );
1015 if (current
->suspend
+ process
->suspend
> 0) stop_thread( current
);
1016 if (process
->debugger
) set_process_debug_flag( process
, 1 );
1019 /* open a handle to a process */
1020 DECL_HANDLER(open_process
)
1022 struct process
*process
= get_process_from_id( req
->pid
);
1026 reply
->handle
= alloc_handle( current
->process
, process
, req
->access
, req
->attributes
);
1027 release_object( process
);
1031 /* terminate a process */
1032 DECL_HANDLER(terminate_process
)
1034 struct process
*process
;
1036 if ((process
= get_process_from_handle( req
->handle
, PROCESS_TERMINATE
)))
1038 reply
->self
= (current
->process
== process
);
1039 kill_process( process
, current
, req
->exit_code
);
1040 release_object( process
);
1044 /* fetch information about a process */
1045 DECL_HANDLER(get_process_info
)
1047 struct process
*process
;
1049 if ((process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_INFORMATION
)))
1051 reply
->pid
= get_process_id( process
);
1052 reply
->ppid
= process
->parent
? get_process_id( process
->parent
) : 0;
1053 reply
->exit_code
= process
->exit_code
;
1054 reply
->priority
= process
->priority
;
1055 reply
->affinity
= process
->affinity
;
1056 reply
->peb
= process
->peb
;
1057 release_object( process
);
1061 /* set information about a process */
1062 DECL_HANDLER(set_process_info
)
1064 struct process
*process
;
1066 if ((process
= get_process_from_handle( req
->handle
, PROCESS_SET_INFORMATION
)))
1068 if (req
->mask
& SET_PROCESS_INFO_PRIORITY
) process
->priority
= req
->priority
;
1069 if (req
->mask
& SET_PROCESS_INFO_AFFINITY
)
1071 if (req
->affinity
!= 1) set_error( STATUS_INVALID_PARAMETER
);
1072 else process
->affinity
= req
->affinity
;
1074 release_object( process
);
1078 /* read data from a process address space */
1079 DECL_HANDLER(read_process_memory
)
1081 struct process
*process
;
1082 size_t len
= get_reply_max_size();
1084 if (!(process
= get_process_from_handle( req
->handle
, PROCESS_VM_READ
))) return;
1088 unsigned int start_offset
= (unsigned int)req
->addr
% sizeof(int);
1089 unsigned int nb_ints
= (len
+ start_offset
+ sizeof(int) - 1) / sizeof(int);
1090 const int *start
= (int *)((char *)req
->addr
- start_offset
);
1091 int *buffer
= mem_alloc( nb_ints
* sizeof(int) );
1094 if (read_process_memory( process
, start
, nb_ints
, buffer
))
1096 /* move start of requested data to start of buffer */
1097 if (start_offset
) memmove( buffer
, (char *)buffer
+ start_offset
, len
);
1098 set_reply_data_ptr( buffer
, len
);
1103 release_object( process
);
1106 /* write data to a process address space */
1107 DECL_HANDLER(write_process_memory
)
1109 struct process
*process
;
1111 if ((process
= get_process_from_handle( req
->handle
, PROCESS_VM_WRITE
)))
1113 size_t len
= get_req_data_size();
1114 if ((len
% sizeof(int)) || ((unsigned int)req
->addr
% sizeof(int)))
1115 set_error( STATUS_INVALID_PARAMETER
);
1118 if (len
) write_process_memory( process
, req
->addr
, len
/ sizeof(int),
1119 req
->first_mask
, req
->last_mask
, get_req_data() );
1121 release_object( process
);
1125 /* notify the server that a dll has been loaded */
1126 DECL_HANDLER(load_dll
)
1128 struct process_dll
*dll
;
1129 struct file
*file
= NULL
;
1131 if (req
->handle
&& !(file
= get_file_obj( current
->process
, req
->handle
, FILE_READ_DATA
)))
1134 if ((dll
= process_load_dll( current
->process
, file
, req
->base
,
1135 get_req_data(), get_req_data_size() )))
1137 dll
->size
= req
->size
;
1138 dll
->dbg_offset
= req
->dbg_offset
;
1139 dll
->dbg_size
= req
->dbg_size
;
1140 dll
->name
= req
->name
;
1141 /* only generate event if initialization is done */
1142 if (is_process_init_done( current
->process
))
1143 generate_debug_event( current
, LOAD_DLL_DEBUG_EVENT
, dll
);
1145 if (file
) release_object( file
);
1148 /* notify the server that a dll is being unloaded */
1149 DECL_HANDLER(unload_dll
)
1151 process_unload_dll( current
->process
, req
->base
);
1154 /* retrieve information about a module in a process */
1155 DECL_HANDLER(get_dll_info
)
1157 struct process
*process
;
1159 if ((process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_INFORMATION
)))
1161 struct process_dll
*dll
= find_process_dll( process
, req
->base_address
);
1165 reply
->size
= dll
->size
;
1166 reply
->entry_point
= NULL
; /* FIXME */
1169 size_t len
= min( dll
->namelen
, get_reply_max_size() );
1170 set_reply_data( dll
->filename
, len
);
1174 set_error( STATUS_DLL_NOT_FOUND
);
1176 release_object( process
);
1180 /* wait for a process to start waiting on input */
1181 /* FIXME: only returns event for now, wait is done in the client */
1182 DECL_HANDLER(wait_input_idle
)
1184 struct process
*process
;
1187 if ((process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_INFORMATION
)))
1189 if (process
->idle_event
&& process
!= current
->process
)
1190 reply
->event
= alloc_handle( current
->process
, process
->idle_event
,
1191 EVENT_ALL_ACCESS
, 0 );
1192 release_object( process
);