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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
32 #ifdef HAVE_SYS_SOCKET_H
33 # include <sys/socket.h>
41 #define WIN32_NO_STATUS
52 /* process structure */
54 static struct list process_list
= LIST_INIT(process_list
);
55 static int running_processes
, user_processes
;
56 static struct event
*user_process_event
; /* signaled when all user processes have exited */
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 default_get_sd
, /* get_sd */
78 default_set_sd
, /* set_sd */
79 no_lookup_name
, /* lookup_name */
80 no_open_file
, /* open_file */
81 no_close_handle
, /* close_handle */
82 process_destroy
/* destroy */
85 static const struct fd_ops process_fd_ops
=
87 NULL
, /* get_poll_events */
88 process_poll_event
, /* poll_event */
90 NULL
, /* get_fd_type */
92 NULL
, /* queue_async */
93 NULL
, /* reselect_async */
94 NULL
/* cancel async */
97 /* process startup info */
101 struct object obj
; /* object header */
102 obj_handle_t hstdin
; /* handle for stdin */
103 obj_handle_t hstdout
; /* handle for stdout */
104 obj_handle_t hstderr
; /* handle for stderr */
105 struct file
*exe_file
; /* file handle for main exe */
106 struct process
*process
; /* created process */
107 data_size_t data_size
; /* size of startup data */
108 void *data
; /* data for startup info */
111 static void startup_info_dump( struct object
*obj
, int verbose
);
112 static int startup_info_signaled( struct object
*obj
, struct thread
*thread
);
113 static void startup_info_destroy( struct object
*obj
);
115 static const struct object_ops startup_info_ops
=
117 sizeof(struct startup_info
), /* size */
118 startup_info_dump
, /* dump */
119 add_queue
, /* add_queue */
120 remove_queue
, /* remove_queue */
121 startup_info_signaled
, /* signaled */
122 no_satisfied
, /* satisfied */
123 no_signal
, /* signal */
124 no_get_fd
, /* get_fd */
125 no_map_access
, /* map_access */
126 default_get_sd
, /* get_sd */
127 default_set_sd
, /* set_sd */
128 no_lookup_name
, /* lookup_name */
129 no_open_file
, /* open_file */
130 no_close_handle
, /* close_handle */
131 startup_info_destroy
/* destroy */
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 /* final cleanup once we are sure a process is really dead */
229 static void process_died( struct process
*process
)
231 if (debug_level
) fprintf( stderr
, "%04x: *process killed*\n", process
->id
);
232 if (!process
->is_system
)
234 if (!--user_processes
&& user_process_event
)
235 set_event( user_process_event
);
237 release_object( process
);
238 if (!--running_processes
) close_master_socket();
241 /* callback for process sigkill timeout */
242 static void process_sigkill( void *private )
244 struct process
*process
= private;
246 process
->sigkill_timeout
= NULL
;
247 kill( process
->unix_pid
, SIGKILL
);
248 process_died( process
);
251 /* start the sigkill timer for a process upon exit */
252 static void start_sigkill_timer( struct process
*process
)
254 grab_object( process
);
255 if (process
->unix_pid
!= -1 && process
->msg_fd
)
256 process
->sigkill_timeout
= add_timeout_user( -TICKS_PER_SEC
, process_sigkill
, process
);
258 process_died( process
);
261 /* create a new process and its main thread */
262 /* if the function fails the fd is closed */
263 struct thread
*create_process( int fd
, struct thread
*parent_thread
, int inherit_all
)
265 struct process
*process
;
266 struct thread
*thread
= NULL
;
269 if (!(process
= alloc_object( &process_ops
)))
274 process
->parent
= NULL
;
275 process
->debugger
= NULL
;
276 process
->handles
= NULL
;
277 process
->msg_fd
= NULL
;
278 process
->sigkill_timeout
= NULL
;
279 process
->unix_pid
= -1;
280 process
->exit_code
= STILL_ACTIVE
;
281 process
->running_threads
= 0;
282 process
->priority
= PROCESS_PRIOCLASS_NORMAL
;
283 process
->affinity
= 1;
284 process
->suspend
= 0;
285 process
->is_system
= 0;
286 process
->create_flags
= 0;
287 process
->console
= NULL
;
288 process
->startup_state
= STARTUP_IN_PROGRESS
;
289 process
->startup_info
= NULL
;
290 process
->idle_event
= NULL
;
291 process
->queue
= NULL
;
293 process
->ldt_copy
= NULL
;
294 process
->winstation
= 0;
295 process
->desktop
= 0;
296 process
->token
= NULL
;
297 process
->trace_data
= 0;
298 list_init( &process
->thread_list
);
299 list_init( &process
->locks
);
300 list_init( &process
->classes
);
301 list_init( &process
->dlls
);
303 process
->start_time
= current_time
;
304 process
->end_time
= 0;
305 list_add_head( &process_list
, &process
->entry
);
307 if (!(process
->id
= process
->group_id
= alloc_ptid( process
)))
312 if (!(process
->msg_fd
= create_anonymous_fd( &process_fd_ops
, fd
, &process
->obj
, 0 ))) goto error
;
314 /* create the handle table */
317 process
->handles
= alloc_handle_table( process
, 0 );
318 process
->token
= token_create_admin();
322 struct process
*parent
= parent_thread
->process
;
323 process
->parent
= (struct process
*)grab_object( parent
);
324 process
->handles
= inherit_all
? copy_handle_table( process
, parent
)
325 : alloc_handle_table( process
, 0 );
326 /* Note: for security reasons, starting a new process does not attempt
327 * to use the current impersonation token for the new process */
328 process
->token
= token_duplicate( parent
->token
, TRUE
, 0 );
330 if (!process
->handles
|| !process
->token
) goto error
;
332 /* create the main thread */
333 if (pipe( request_pipe
) == -1)
338 if (send_client_fd( process
, request_pipe
[1], 0 ) == -1)
340 close( request_pipe
[0] );
341 close( request_pipe
[1] );
344 close( request_pipe
[1] );
345 if (!(thread
= create_thread( request_pipe
[0], process
))) goto error
;
347 set_fd_events( process
->msg_fd
, POLLIN
); /* start listening to events */
348 release_object( process
);
352 if (process
) release_object( process
);
353 /* if we failed to start our first process, close everything down */
354 if (!running_processes
) close_master_socket();
358 /* initialize the current process and fill in the request */
359 data_size_t
init_process( struct thread
*thread
)
361 struct process
*process
= thread
->process
;
362 struct startup_info
*info
= process
->startup_info
;
364 init_process_tracing( process
);
366 return info
->data_size
;
369 /* destroy a process when its refcount is 0 */
370 static void process_destroy( struct object
*obj
)
372 struct process
*process
= (struct process
*)obj
;
373 assert( obj
->ops
== &process_ops
);
375 /* we can't have a thread remaining */
376 assert( list_empty( &process
->thread_list
));
378 assert( !process
->sigkill_timeout
); /* timeout should hold a reference to the process */
380 set_process_startup_state( process
, STARTUP_ABORTED
);
381 if (process
->console
) release_object( process
->console
);
382 if (process
->parent
) release_object( process
->parent
);
383 if (process
->msg_fd
) release_object( process
->msg_fd
);
384 list_remove( &process
->entry
);
385 if (process
->idle_event
) release_object( process
->idle_event
);
386 if (process
->queue
) release_object( process
->queue
);
387 if (process
->id
) free_ptid( process
->id
);
388 if (process
->token
) release_object( process
->token
);
391 /* dump a process on stdout for debugging purposes */
392 static void process_dump( struct object
*obj
, int verbose
)
394 struct process
*process
= (struct process
*)obj
;
395 assert( obj
->ops
== &process_ops
);
397 fprintf( stderr
, "Process id=%04x handles=%p\n", process
->id
, process
->handles
);
400 static int process_signaled( struct object
*obj
, struct thread
*thread
)
402 struct process
*process
= (struct process
*)obj
;
403 return !process
->running_threads
;
406 static unsigned int process_map_access( struct object
*obj
, unsigned int access
)
408 if (access
& GENERIC_READ
) access
|= STANDARD_RIGHTS_READ
| SYNCHRONIZE
;
409 if (access
& GENERIC_WRITE
) access
|= STANDARD_RIGHTS_WRITE
| SYNCHRONIZE
;
410 if (access
& GENERIC_EXECUTE
) access
|= STANDARD_RIGHTS_EXECUTE
;
411 if (access
& GENERIC_ALL
) access
|= PROCESS_ALL_ACCESS
;
412 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
415 static void process_poll_event( struct fd
*fd
, int event
)
417 struct process
*process
= get_fd_user( fd
);
418 assert( process
->obj
.ops
== &process_ops
);
420 if (event
& (POLLERR
| POLLHUP
))
422 release_object( process
->msg_fd
);
423 process
->msg_fd
= NULL
;
424 if (process
->sigkill_timeout
) /* already waiting for it to die */
426 remove_timeout_user( process
->sigkill_timeout
);
427 process
->sigkill_timeout
= NULL
;
428 process_died( process
);
430 else kill_process( process
, 0 );
432 else if (event
& POLLIN
) receive_fd( process
);
435 static void startup_info_destroy( struct object
*obj
)
437 struct startup_info
*info
= (struct startup_info
*)obj
;
438 assert( obj
->ops
== &startup_info_ops
);
440 if (info
->exe_file
) release_object( info
->exe_file
);
441 if (info
->process
) release_object( info
->process
);
444 static void startup_info_dump( struct object
*obj
, int verbose
)
446 struct startup_info
*info
= (struct startup_info
*)obj
;
447 assert( obj
->ops
== &startup_info_ops
);
449 fprintf( stderr
, "Startup info in=%p out=%p err=%p\n",
450 info
->hstdin
, info
->hstdout
, info
->hstderr
);
453 static int startup_info_signaled( struct object
*obj
, struct thread
*thread
)
455 struct startup_info
*info
= (struct startup_info
*)obj
;
456 return info
->process
&& info
->process
->startup_state
!= STARTUP_IN_PROGRESS
;
459 /* get a process from an id (and increment the refcount) */
460 struct process
*get_process_from_id( process_id_t id
)
462 struct object
*obj
= get_ptid_entry( id
);
464 if (obj
&& obj
->ops
== &process_ops
) return (struct process
*)grab_object( obj
);
465 set_error( STATUS_INVALID_PARAMETER
);
469 /* get a process from a handle (and increment the refcount) */
470 struct process
*get_process_from_handle( obj_handle_t handle
, unsigned int access
)
472 return (struct process
*)get_handle_obj( current
->process
, handle
,
473 access
, &process_ops
);
476 /* find a dll from its base address */
477 static inline struct process_dll
*find_process_dll( struct process
*process
, void *base
)
479 struct process_dll
*dll
;
481 LIST_FOR_EACH_ENTRY( dll
, &process
->dlls
, struct process_dll
, entry
)
483 if (dll
->base
== base
) return dll
;
488 /* add a dll to a process list */
489 static struct process_dll
*process_load_dll( struct process
*process
, struct file
*file
,
490 void *base
, const WCHAR
*filename
, data_size_t name_len
)
492 struct process_dll
*dll
;
494 /* make sure we don't already have one with the same base address */
495 if (find_process_dll( process
, base
))
497 set_error( STATUS_INVALID_PARAMETER
);
501 if ((dll
= mem_alloc( sizeof(*dll
) )))
505 dll
->filename
= NULL
;
506 dll
->namelen
= name_len
;
507 if (name_len
&& !(dll
->filename
= memdup( filename
, name_len
)))
512 if (file
) dll
->file
= grab_file_unless_removable( file
);
513 list_add_tail( &process
->dlls
, &dll
->entry
);
518 /* remove a dll from a process list */
519 static void process_unload_dll( struct process
*process
, void *base
)
521 struct process_dll
*dll
= find_process_dll( process
, base
);
523 if (dll
&& (&dll
->entry
!= list_head( &process
->dlls
))) /* main exe can't be unloaded */
525 if (dll
->file
) release_object( dll
->file
);
526 free( dll
->filename
);
527 list_remove( &dll
->entry
);
529 generate_debug_event( current
, UNLOAD_DLL_DEBUG_EVENT
, base
);
531 else set_error( STATUS_INVALID_PARAMETER
);
534 /* terminate a process with the given exit code */
535 static void terminate_process( struct process
*process
, struct thread
*skip
, int exit_code
)
539 if (skip
&& skip
->process
== process
) /* move it to the end of the list */
541 assert( skip
->state
!= TERMINATED
);
542 list_remove( &skip
->proc_entry
);
543 list_add_tail( &process
->thread_list
, &skip
->proc_entry
);
546 grab_object( process
); /* make sure it doesn't get freed when threads die */
547 while ((ptr
= list_head( &process
->thread_list
)))
549 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
551 if (exit_code
) thread
->exit_code
= exit_code
;
552 if (thread
== skip
) break;
553 kill_thread( thread
, 1 );
555 release_object( process
);
558 /* kill all processes */
559 void kill_all_processes( struct process
*skip
, int exit_code
)
563 struct process
*process
;
565 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
567 if (process
== skip
) continue;
568 if (process
->running_threads
) break;
570 if (&process
->entry
== &process_list
) break; /* no process found */
571 terminate_process( process
, NULL
, exit_code
);
575 /* kill all processes being attached to a console renderer */
576 void kill_console_processes( struct thread
*renderer
, int exit_code
)
578 for (;;) /* restart from the beginning of the list every time */
580 struct process
*process
;
582 /* find the first process being attached to 'renderer' and still running */
583 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
585 if (process
== renderer
->process
) continue;
586 if (!process
->running_threads
) continue;
587 if (process
->console
&& console_get_renderer( process
->console
) == renderer
) break;
589 if (&process
->entry
== &process_list
) break; /* no process found */
590 terminate_process( process
, NULL
, exit_code
);
594 /* a process has been killed (i.e. its last thread died) */
595 static void process_killed( struct process
*process
)
597 struct handle_table
*handles
;
600 assert( list_empty( &process
->thread_list
));
601 process
->end_time
= current_time
;
602 close_process_desktop( process
);
603 handles
= process
->handles
;
604 process
->handles
= NULL
;
605 if (handles
) release_object( handles
);
607 /* close the console attached to this process, if any */
608 free_console( process
);
610 while ((ptr
= list_head( &process
->dlls
)))
612 struct process_dll
*dll
= LIST_ENTRY( ptr
, struct process_dll
, entry
);
613 if (dll
->file
) release_object( dll
->file
);
614 free( dll
->filename
);
615 list_remove( &dll
->entry
);
618 destroy_process_classes( process
);
619 remove_process_locks( process
);
620 set_process_startup_state( process
, STARTUP_ABORTED
);
621 finish_process_tracing( process
);
622 start_sigkill_timer( process
);
623 wake_up( &process
->obj
, 0 );
626 /* add a thread to a process running threads list */
627 void add_process_thread( struct process
*process
, struct thread
*thread
)
629 list_add_tail( &process
->thread_list
, &thread
->proc_entry
);
630 if (!process
->running_threads
++)
633 if (!process
->is_system
)
635 if (!user_processes
++ && user_process_event
)
636 reset_event( user_process_event
);
639 grab_object( thread
);
642 /* remove a thread from a process running threads list */
643 void remove_process_thread( struct process
*process
, struct thread
*thread
)
645 assert( process
->running_threads
> 0 );
646 assert( !list_empty( &process
->thread_list
));
648 list_remove( &thread
->proc_entry
);
650 if (!--process
->running_threads
)
652 /* we have removed the last running thread, exit the process */
653 process
->exit_code
= thread
->exit_code
;
654 generate_debug_event( thread
, EXIT_PROCESS_DEBUG_EVENT
, process
);
655 process_killed( process
);
657 else generate_debug_event( thread
, EXIT_THREAD_DEBUG_EVENT
, thread
);
658 release_object( thread
);
661 /* suspend all the threads of a process */
662 void suspend_process( struct process
*process
)
664 if (!process
->suspend
++)
666 struct list
*ptr
, *next
;
668 LIST_FOR_EACH_SAFE( ptr
, next
, &process
->thread_list
)
670 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
671 if (!thread
->suspend
) stop_thread( thread
);
676 /* resume all the threads of a process */
677 void resume_process( struct process
*process
)
679 assert (process
->suspend
> 0);
680 if (!--process
->suspend
)
682 struct list
*ptr
, *next
;
684 LIST_FOR_EACH_SAFE( ptr
, next
, &process
->thread_list
)
686 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
687 if (!thread
->suspend
) wake_thread( thread
);
692 /* kill a process on the spot */
693 void kill_process( struct process
*process
, int violent_death
)
695 if (violent_death
) terminate_process( process
, NULL
, 1 );
700 grab_object( process
); /* make sure it doesn't get freed when threads die */
701 while ((ptr
= list_head( &process
->thread_list
)))
703 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
704 kill_thread( thread
, 0 );
706 release_object( process
);
710 /* kill all processes being debugged by a given thread */
711 void kill_debugged_processes( struct thread
*debugger
, int exit_code
)
713 for (;;) /* restart from the beginning of the list every time */
715 struct process
*process
;
717 /* find the first process being debugged by 'debugger' and still running */
718 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
720 if (!process
->running_threads
) continue;
721 if (process
->debugger
== debugger
) break;
723 if (&process
->entry
== &process_list
) break; /* no process found */
724 process
->debugger
= NULL
;
725 terminate_process( process
, NULL
, exit_code
);
730 /* trigger a breakpoint event in a given process */
731 void break_process( struct process
*process
)
733 struct thread
*thread
;
735 suspend_process( process
);
737 LIST_FOR_EACH_ENTRY( thread
, &process
->thread_list
, struct thread
, proc_entry
)
739 if (thread
->context
) /* inside an exception event already */
741 break_thread( thread
);
745 if ((thread
= get_process_first_thread( process
))) thread
->debug_break
= 1;
746 else set_error( STATUS_ACCESS_DENIED
);
748 resume_process( process
);
752 /* detach a debugger from all its debuggees */
753 void detach_debugged_processes( struct thread
*debugger
)
755 struct process
*process
;
757 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
759 if (process
->debugger
== debugger
&& process
->running_threads
)
761 debugger_detach( process
, debugger
);
767 void enum_processes( int (*cb
)(struct process
*, void*), void *user
)
769 struct list
*ptr
, *next
;
771 LIST_FOR_EACH_SAFE( ptr
, next
, &process_list
)
773 struct process
*process
= LIST_ENTRY( ptr
, struct process
, entry
);
774 if ((cb
)(process
, user
)) break;
778 /* set the debugged flag in the process PEB */
779 int set_process_debug_flag( struct process
*process
, int flag
)
781 char data
= (flag
!= 0);
783 /* BeingDebugged flag is the byte at offset 2 in the PEB */
784 return write_process_memory( process
, (char *)process
->peb
+ 2, 1, &data
);
787 /* take a snapshot of currently running processes */
788 struct process_snapshot
*process_snap( int *count
)
790 struct process_snapshot
*snapshot
, *ptr
;
791 struct process
*process
;
793 if (!running_processes
) return NULL
;
794 if (!(snapshot
= mem_alloc( sizeof(*snapshot
) * running_processes
)))
797 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
799 if (!process
->running_threads
) continue;
800 ptr
->process
= process
;
801 ptr
->threads
= process
->running_threads
;
802 ptr
->count
= process
->obj
.refcount
;
803 ptr
->priority
= process
->priority
;
804 ptr
->handles
= get_handle_table_count(process
);
805 grab_object( process
);
809 if (!(*count
= ptr
- snapshot
))
817 /* take a snapshot of the modules of a process */
818 struct module_snapshot
*module_snap( struct process
*process
, int *count
)
820 struct module_snapshot
*snapshot
, *ptr
;
821 struct process_dll
*dll
;
824 LIST_FOR_EACH_ENTRY( dll
, &process
->dlls
, struct process_dll
, entry
) total
++;
825 if (!(snapshot
= mem_alloc( sizeof(*snapshot
) * total
))) return NULL
;
828 LIST_FOR_EACH_ENTRY( dll
, &process
->dlls
, struct process_dll
, entry
)
830 ptr
->base
= dll
->base
;
831 ptr
->size
= dll
->size
;
832 ptr
->namelen
= dll
->namelen
;
833 ptr
->filename
= memdup( dll
->filename
, dll
->namelen
);
841 /* create a new process */
842 DECL_HANDLER(new_process
)
844 struct startup_info
*info
;
845 struct thread
*thread
;
846 struct process
*process
;
847 struct process
*parent
= current
->process
;
848 int socket_fd
= thread_get_inflight_fd( current
, req
->socket_fd
);
852 set_error( STATUS_INVALID_PARAMETER
);
855 if (fcntl( socket_fd
, F_SETFL
, O_NONBLOCK
) == -1)
857 set_error( STATUS_INVALID_HANDLE
);
862 /* build the startup info for a new process */
863 if (!(info
= alloc_object( &startup_info_ops
))) return;
864 info
->hstdin
= req
->hstdin
;
865 info
->hstdout
= req
->hstdout
;
866 info
->hstderr
= req
->hstderr
;
867 info
->exe_file
= NULL
;
868 info
->process
= NULL
;
869 info
->data_size
= get_req_data_size();
873 !(info
->exe_file
= get_file_obj( current
->process
, req
->exe_file
, FILE_READ_DATA
)))
876 if (!(info
->data
= memdup( get_req_data(), info
->data_size
))) goto done
;
878 if (!(thread
= create_process( socket_fd
, current
, req
->inherit_all
))) goto done
;
879 process
= thread
->process
;
880 process
->create_flags
= req
->create_flags
;
881 process
->startup_info
= (struct startup_info
*)grab_object( info
);
883 /* connect to the window station */
884 connect_process_winstation( process
, current
);
886 /* thread will be actually suspended in init_done */
887 if (req
->create_flags
& CREATE_SUSPENDED
) thread
->suspend
++;
889 /* set the process console */
890 if (!(req
->create_flags
& (DETACHED_PROCESS
| CREATE_NEW_CONSOLE
)))
892 /* FIXME: some better error checking should be done...
893 * like if hConOut and hConIn are console handles, then they should be on the same
896 inherit_console( current
, process
, req
->inherit_all
? req
->hstdin
: 0 );
899 if (!req
->inherit_all
&& !(req
->create_flags
& CREATE_NEW_CONSOLE
))
901 info
->hstdin
= duplicate_handle( parent
, req
->hstdin
, process
,
902 0, OBJ_INHERIT
, DUPLICATE_SAME_ACCESS
);
903 info
->hstdout
= duplicate_handle( parent
, req
->hstdout
, process
,
904 0, OBJ_INHERIT
, DUPLICATE_SAME_ACCESS
);
905 info
->hstderr
= duplicate_handle( parent
, req
->hstderr
, process
,
906 0, OBJ_INHERIT
, DUPLICATE_SAME_ACCESS
);
907 /* some handles above may have been invalid; this is not an error */
908 if (get_error() == STATUS_INVALID_HANDLE
||
909 get_error() == STATUS_OBJECT_TYPE_MISMATCH
) clear_error();
912 /* attach to the debugger if requested */
913 if (req
->create_flags
& (DEBUG_PROCESS
| DEBUG_ONLY_THIS_PROCESS
))
914 set_process_debugger( process
, current
);
915 else if (parent
->debugger
&& !(parent
->create_flags
& DEBUG_ONLY_THIS_PROCESS
))
916 set_process_debugger( process
, parent
->debugger
);
918 if (!(req
->create_flags
& CREATE_NEW_PROCESS_GROUP
))
919 process
->group_id
= parent
->group_id
;
921 info
->process
= (struct process
*)grab_object( process
);
922 reply
->info
= alloc_handle( current
->process
, info
, SYNCHRONIZE
, 0 );
923 reply
->pid
= get_process_id( process
);
924 reply
->tid
= get_thread_id( thread
);
925 reply
->phandle
= alloc_handle( parent
, process
, req
->process_access
, req
->process_attr
);
926 reply
->thandle
= alloc_handle( parent
, thread
, req
->thread_access
, req
->thread_attr
);
929 release_object( info
);
932 /* Retrieve information about a newly started process */
933 DECL_HANDLER(get_new_process_info
)
935 struct startup_info
*info
;
937 if ((info
= (struct startup_info
*)get_handle_obj( current
->process
, req
->info
,
938 0, &startup_info_ops
)))
940 reply
->success
= is_process_init_done( info
->process
);
941 reply
->exit_code
= info
->process
->exit_code
;
942 release_object( info
);
946 /* Retrieve the new process startup info */
947 DECL_HANDLER(get_startup_info
)
949 struct process
*process
= current
->process
;
950 struct startup_info
*info
= process
->startup_info
;
955 if (info
->exe_file
&&
956 !(reply
->exe_file
= alloc_handle( process
, info
->exe_file
, GENERIC_READ
, 0 ))) return;
958 reply
->hstdin
= info
->hstdin
;
959 reply
->hstdout
= info
->hstdout
;
960 reply
->hstderr
= info
->hstderr
;
962 /* we return the data directly without making a copy so this can only be called once */
963 size
= info
->data_size
;
964 if (size
> get_reply_max_size()) size
= get_reply_max_size();
965 set_reply_data_ptr( info
->data
, size
);
970 /* signal the end of the process initialization */
971 DECL_HANDLER(init_process_done
)
973 struct process_dll
*dll
;
974 struct process
*process
= current
->process
;
976 if (is_process_init_done(process
))
978 set_error( STATUS_INVALID_PARAMETER
);
981 if (!(dll
= find_process_dll( process
, req
->module
)))
983 set_error( STATUS_DLL_NOT_FOUND
);
987 /* main exe is the first in the dll list */
988 list_remove( &dll
->entry
);
989 list_add_head( &process
->dlls
, &dll
->entry
);
991 generate_startup_debug_events( process
, req
->entry
);
992 set_process_startup_state( process
, STARTUP_DONE
);
994 if (req
->gui
) process
->idle_event
= create_event( NULL
, NULL
, 0, 1, 0, NULL
);
995 if (current
->suspend
+ process
->suspend
> 0) stop_thread( current
);
996 if (process
->debugger
) set_process_debug_flag( process
, 1 );
999 /* open a handle to a process */
1000 DECL_HANDLER(open_process
)
1002 struct process
*process
= get_process_from_id( req
->pid
);
1006 reply
->handle
= alloc_handle( current
->process
, process
, req
->access
, req
->attributes
);
1007 release_object( process
);
1011 /* terminate a process */
1012 DECL_HANDLER(terminate_process
)
1014 struct process
*process
;
1016 if ((process
= get_process_from_handle( req
->handle
, PROCESS_TERMINATE
)))
1018 reply
->self
= (current
->process
== process
);
1019 terminate_process( process
, current
, req
->exit_code
);
1020 release_object( process
);
1024 /* fetch information about a process */
1025 DECL_HANDLER(get_process_info
)
1027 struct process
*process
;
1029 if ((process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_INFORMATION
)))
1031 reply
->pid
= get_process_id( process
);
1032 reply
->ppid
= process
->parent
? get_process_id( process
->parent
) : 0;
1033 reply
->exit_code
= process
->exit_code
;
1034 reply
->priority
= process
->priority
;
1035 reply
->affinity
= process
->affinity
;
1036 reply
->peb
= process
->peb
;
1037 reply
->start_time
= process
->start_time
;
1038 reply
->end_time
= process
->end_time
;
1039 release_object( process
);
1043 /* set information about a process */
1044 DECL_HANDLER(set_process_info
)
1046 struct process
*process
;
1048 if ((process
= get_process_from_handle( req
->handle
, PROCESS_SET_INFORMATION
)))
1050 if (req
->mask
& SET_PROCESS_INFO_PRIORITY
) process
->priority
= req
->priority
;
1051 if (req
->mask
& SET_PROCESS_INFO_AFFINITY
)
1053 if (req
->affinity
!= 1) set_error( STATUS_INVALID_PARAMETER
);
1054 else process
->affinity
= req
->affinity
;
1056 release_object( process
);
1060 /* read data from a process address space */
1061 DECL_HANDLER(read_process_memory
)
1063 struct process
*process
;
1064 data_size_t len
= get_reply_max_size();
1066 if (!(process
= get_process_from_handle( req
->handle
, PROCESS_VM_READ
))) return;
1070 char *buffer
= mem_alloc( len
);
1073 if (read_process_memory( process
, req
->addr
, len
, buffer
))
1074 set_reply_data_ptr( buffer
, len
);
1079 release_object( process
);
1082 /* write data to a process address space */
1083 DECL_HANDLER(write_process_memory
)
1085 struct process
*process
;
1087 if ((process
= get_process_from_handle( req
->handle
, PROCESS_VM_WRITE
)))
1089 data_size_t len
= get_req_data_size();
1090 if (len
) write_process_memory( process
, req
->addr
, len
, get_req_data() );
1091 else set_error( STATUS_INVALID_PARAMETER
);
1092 release_object( process
);
1096 /* notify the server that a dll has been loaded */
1097 DECL_HANDLER(load_dll
)
1099 struct process_dll
*dll
;
1100 struct file
*file
= NULL
;
1102 if (req
->handle
&& !(file
= get_file_obj( current
->process
, req
->handle
, FILE_READ_DATA
)))
1105 if ((dll
= process_load_dll( current
->process
, file
, req
->base
,
1106 get_req_data(), get_req_data_size() )))
1108 dll
->size
= req
->size
;
1109 dll
->dbg_offset
= req
->dbg_offset
;
1110 dll
->dbg_size
= req
->dbg_size
;
1111 dll
->name
= req
->name
;
1112 /* only generate event if initialization is done */
1113 if (is_process_init_done( current
->process
))
1114 generate_debug_event( current
, LOAD_DLL_DEBUG_EVENT
, dll
);
1116 if (file
) release_object( file
);
1119 /* notify the server that a dll is being unloaded */
1120 DECL_HANDLER(unload_dll
)
1122 process_unload_dll( current
->process
, req
->base
);
1125 /* retrieve information about a module in a process */
1126 DECL_HANDLER(get_dll_info
)
1128 struct process
*process
;
1130 if ((process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_INFORMATION
)))
1132 struct process_dll
*dll
= find_process_dll( process
, req
->base_address
);
1136 reply
->size
= dll
->size
;
1137 reply
->entry_point
= NULL
; /* FIXME */
1140 data_size_t len
= min( dll
->namelen
, get_reply_max_size() );
1141 set_reply_data( dll
->filename
, len
);
1145 set_error( STATUS_DLL_NOT_FOUND
);
1147 release_object( process
);
1151 /* retrieve the process idle event */
1152 DECL_HANDLER(get_process_idle_event
)
1154 struct process
*process
;
1157 if ((process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_INFORMATION
)))
1159 if (process
->idle_event
&& process
!= current
->process
)
1160 reply
->event
= alloc_handle( current
->process
, process
->idle_event
,
1161 EVENT_ALL_ACCESS
, 0 );
1162 release_object( process
);
1166 /* make the current process a system process */
1167 DECL_HANDLER(make_process_system
)
1169 struct process
*process
= current
->process
;
1171 if (!user_process_event
)
1173 if (!(user_process_event
= create_event( NULL
, NULL
, 0, 1, 0, NULL
))) return;
1174 make_object_static( (struct object
*)user_process_event
);
1177 if (!(reply
->event
= alloc_handle( current
->process
, user_process_event
, EVENT_ALL_ACCESS
, 0 )))
1180 if (!process
->is_system
)
1182 process
->is_system
= 1;
1183 if (!--user_processes
) set_event( user_process_event
);