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
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_open_file
, /* open_file */
79 no_close_handle
, /* close_handle */
80 process_destroy
/* destroy */
83 static const struct fd_ops process_fd_ops
=
85 NULL
, /* get_poll_events */
86 process_poll_event
, /* poll_event */
88 NULL
, /* get_fd_type */
89 NULL
, /* queue_async */
90 NULL
, /* reselect_async */
91 NULL
/* cancel async */
94 /* process startup info */
98 struct object obj
; /* object header */
99 obj_handle_t hstdin
; /* handle for stdin */
100 obj_handle_t hstdout
; /* handle for stdout */
101 obj_handle_t hstderr
; /* handle for stderr */
102 struct file
*exe_file
; /* file handle for main exe */
103 struct process
*process
; /* created process */
104 data_size_t data_size
; /* size of startup data */
105 void *data
; /* data for startup info */
108 static void startup_info_dump( struct object
*obj
, int verbose
);
109 static int startup_info_signaled( struct object
*obj
, struct thread
*thread
);
110 static void startup_info_destroy( struct object
*obj
);
112 static const struct object_ops startup_info_ops
=
114 sizeof(struct startup_info
), /* size */
115 startup_info_dump
, /* dump */
116 add_queue
, /* add_queue */
117 remove_queue
, /* remove_queue */
118 startup_info_signaled
, /* signaled */
119 no_satisfied
, /* satisfied */
120 no_signal
, /* signal */
121 no_get_fd
, /* get_fd */
122 no_map_access
, /* map_access */
123 no_lookup_name
, /* lookup_name */
124 no_open_file
, /* open_file */
125 no_close_handle
, /* close_handle */
126 startup_info_destroy
/* destroy */
132 void *ptr
; /* entry ptr */
133 unsigned int next
; /* next free entry */
136 static struct ptid_entry
*ptid_entries
; /* array of ptid entries */
137 static unsigned int used_ptid_entries
; /* number of entries in use */
138 static unsigned int alloc_ptid_entries
; /* number of allocated entries */
139 static unsigned int next_free_ptid
; /* next free entry */
140 static unsigned int last_free_ptid
; /* last free entry */
142 #define PTID_OFFSET 8 /* offset for first ptid value */
144 /* allocate a new process or thread id */
145 unsigned int alloc_ptid( void *ptr
)
147 struct ptid_entry
*entry
;
150 if (used_ptid_entries
< alloc_ptid_entries
)
152 id
= used_ptid_entries
+ PTID_OFFSET
;
153 entry
= &ptid_entries
[used_ptid_entries
++];
155 else if (next_free_ptid
)
158 entry
= &ptid_entries
[id
- PTID_OFFSET
];
159 if (!(next_free_ptid
= entry
->next
)) last_free_ptid
= 0;
161 else /* need to grow the array */
163 unsigned int count
= alloc_ptid_entries
+ (alloc_ptid_entries
/ 2);
164 if (!count
) count
= 64;
165 if (!(entry
= realloc( ptid_entries
, count
* sizeof(*entry
) )))
167 set_error( STATUS_NO_MEMORY
);
170 ptid_entries
= entry
;
171 alloc_ptid_entries
= count
;
172 id
= used_ptid_entries
+ PTID_OFFSET
;
173 entry
= &ptid_entries
[used_ptid_entries
++];
180 /* free a process or thread id */
181 void free_ptid( unsigned int id
)
183 struct ptid_entry
*entry
= &ptid_entries
[id
- PTID_OFFSET
];
188 /* append to end of free list so that we don't reuse it too early */
189 if (last_free_ptid
) ptid_entries
[last_free_ptid
- PTID_OFFSET
].next
= id
;
190 else next_free_ptid
= id
;
195 /* retrieve the pointer corresponding to a process or thread id */
196 void *get_ptid_entry( unsigned int id
)
198 if (id
< PTID_OFFSET
) return NULL
;
199 if (id
- PTID_OFFSET
>= used_ptid_entries
) return NULL
;
200 return ptid_entries
[id
- PTID_OFFSET
].ptr
;
203 /* return the main thread of the process */
204 struct thread
*get_process_first_thread( struct process
*process
)
206 struct list
*ptr
= list_head( &process
->thread_list
);
207 if (!ptr
) return NULL
;
208 return LIST_ENTRY( ptr
, struct thread
, proc_entry
);
211 /* set the state of the process startup info */
212 static void set_process_startup_state( struct process
*process
, enum startup_state state
)
214 if (process
->startup_state
== STARTUP_IN_PROGRESS
) process
->startup_state
= state
;
215 if (process
->startup_info
)
217 wake_up( &process
->startup_info
->obj
, 0 );
218 release_object( process
->startup_info
);
219 process
->startup_info
= NULL
;
223 /* final cleanup once we are sure a process is really dead */
224 static void process_died( struct process
*process
)
226 if (debug_level
) fprintf( stderr
, "%04x: *process killed*\n", process
->id
);
227 release_object( process
);
228 if (!--running_processes
) close_master_socket();
231 /* callback for process sigkill timeout */
232 static void process_sigkill( void *private )
234 struct process
*process
= private;
236 process
->sigkill_timeout
= NULL
;
237 kill( process
->unix_pid
, SIGKILL
);
238 process_died( process
);
241 /* start the sigkill timer for a process upon exit */
242 static void start_sigkill_timer( struct process
*process
)
244 grab_object( process
);
245 if (process
->unix_pid
!= -1 && process
->msg_fd
)
247 struct timeval when
= current_time
;
249 add_timeout( &when
, 1000 );
250 process
->sigkill_timeout
= add_timeout_user( &when
, process_sigkill
, process
);
252 else process_died( process
);
255 /* create a new process and its main thread */
256 /* if the function fails the fd is closed */
257 struct thread
*create_process( int fd
, struct thread
*parent_thread
, int inherit_all
)
259 struct process
*process
;
260 struct thread
*thread
= NULL
;
263 if (!(process
= alloc_object( &process_ops
)))
268 process
->parent
= NULL
;
269 process
->debugger
= NULL
;
270 process
->handles
= NULL
;
271 process
->msg_fd
= NULL
;
272 process
->sigkill_timeout
= NULL
;
273 process
->unix_pid
= -1;
274 process
->exit_code
= STILL_ACTIVE
;
275 process
->running_threads
= 0;
276 process
->priority
= PROCESS_PRIOCLASS_NORMAL
;
277 process
->affinity
= 1;
278 process
->suspend
= 0;
279 process
->create_flags
= 0;
280 process
->console
= NULL
;
281 process
->startup_state
= STARTUP_IN_PROGRESS
;
282 process
->startup_info
= NULL
;
283 process
->idle_event
= NULL
;
284 process
->queue
= NULL
;
286 process
->ldt_copy
= NULL
;
287 process
->winstation
= 0;
288 process
->desktop
= 0;
289 process
->token
= token_create_admin();
290 process
->trace_data
= 0;
291 list_init( &process
->thread_list
);
292 list_init( &process
->locks
);
293 list_init( &process
->classes
);
294 list_init( &process
->dlls
);
296 process
->start_time
= current_time
;
297 process
->end_time
.tv_sec
= process
->end_time
.tv_usec
= 0;
298 list_add_head( &process_list
, &process
->entry
);
300 if (!(process
->id
= process
->group_id
= alloc_ptid( process
)))
305 if (!(process
->msg_fd
= create_anonymous_fd( &process_fd_ops
, fd
, &process
->obj
, 0 ))) goto error
;
307 /* create the handle table */
308 if (!parent_thread
) process
->handles
= alloc_handle_table( process
, 0 );
311 struct process
*parent
= parent_thread
->process
;
312 process
->parent
= (struct process
*)grab_object( parent
);
313 process
->handles
= inherit_all
? copy_handle_table( process
, parent
)
314 : alloc_handle_table( process
, 0 );
316 if (!process
->handles
) goto error
;
318 /* create the main thread */
319 if (pipe( request_pipe
) == -1)
324 if (send_client_fd( process
, request_pipe
[1], 0 ) == -1)
326 close( request_pipe
[0] );
327 close( request_pipe
[1] );
330 close( request_pipe
[1] );
331 if (!(thread
= create_thread( request_pipe
[0], process
))) goto error
;
333 set_fd_events( process
->msg_fd
, POLLIN
); /* start listening to events */
334 release_object( process
);
338 if (process
) release_object( process
);
339 /* if we failed to start our first process, close everything down */
340 if (!running_processes
) close_master_socket();
344 /* initialize the current process and fill in the request */
345 data_size_t
init_process( struct thread
*thread
)
347 struct process
*process
= thread
->process
;
348 struct startup_info
*info
= process
->startup_info
;
350 init_process_tracing( process
);
352 return info
->data_size
;
355 /* destroy a process when its refcount is 0 */
356 static void process_destroy( struct object
*obj
)
358 struct process
*process
= (struct process
*)obj
;
359 assert( obj
->ops
== &process_ops
);
361 /* we can't have a thread remaining */
362 assert( list_empty( &process
->thread_list
));
364 assert( !process
->sigkill_timeout
); /* timeout should hold a reference to the process */
366 set_process_startup_state( process
, STARTUP_ABORTED
);
367 if (process
->console
) release_object( process
->console
);
368 if (process
->parent
) release_object( process
->parent
);
369 if (process
->msg_fd
) release_object( process
->msg_fd
);
370 list_remove( &process
->entry
);
371 if (process
->idle_event
) release_object( process
->idle_event
);
372 if (process
->queue
) release_object( process
->queue
);
373 if (process
->id
) free_ptid( process
->id
);
374 if (process
->token
) release_object( process
->token
);
377 /* dump a process on stdout for debugging purposes */
378 static void process_dump( struct object
*obj
, int verbose
)
380 struct process
*process
= (struct process
*)obj
;
381 assert( obj
->ops
== &process_ops
);
383 fprintf( stderr
, "Process id=%04x handles=%p\n", process
->id
, process
->handles
);
386 static int process_signaled( struct object
*obj
, struct thread
*thread
)
388 struct process
*process
= (struct process
*)obj
;
389 return !process
->running_threads
;
392 static unsigned int process_map_access( struct object
*obj
, unsigned int access
)
394 if (access
& GENERIC_READ
) access
|= STANDARD_RIGHTS_READ
| SYNCHRONIZE
;
395 if (access
& GENERIC_WRITE
) access
|= STANDARD_RIGHTS_WRITE
| SYNCHRONIZE
;
396 if (access
& GENERIC_EXECUTE
) access
|= STANDARD_RIGHTS_EXECUTE
;
397 if (access
& GENERIC_ALL
) access
|= PROCESS_ALL_ACCESS
;
398 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
401 static void process_poll_event( struct fd
*fd
, int event
)
403 struct process
*process
= get_fd_user( fd
);
404 assert( process
->obj
.ops
== &process_ops
);
406 if (event
& (POLLERR
| POLLHUP
))
408 release_object( process
->msg_fd
);
409 process
->msg_fd
= NULL
;
410 if (process
->sigkill_timeout
) /* already waiting for it to die */
412 remove_timeout_user( process
->sigkill_timeout
);
413 process
->sigkill_timeout
= NULL
;
414 process_died( process
);
416 else kill_process( process
, 0 );
418 else if (event
& POLLIN
) receive_fd( process
);
421 static void startup_info_destroy( struct object
*obj
)
423 struct startup_info
*info
= (struct startup_info
*)obj
;
424 assert( obj
->ops
== &startup_info_ops
);
426 if (info
->exe_file
) release_object( info
->exe_file
);
427 if (info
->process
) release_object( info
->process
);
430 static void startup_info_dump( struct object
*obj
, int verbose
)
432 struct startup_info
*info
= (struct startup_info
*)obj
;
433 assert( obj
->ops
== &startup_info_ops
);
435 fprintf( stderr
, "Startup info in=%p out=%p err=%p\n",
436 info
->hstdin
, info
->hstdout
, info
->hstderr
);
439 static int startup_info_signaled( struct object
*obj
, struct thread
*thread
)
441 struct startup_info
*info
= (struct startup_info
*)obj
;
442 return info
->process
&& info
->process
->startup_state
!= STARTUP_IN_PROGRESS
;
445 /* get a process from an id (and increment the refcount) */
446 struct process
*get_process_from_id( process_id_t id
)
448 struct object
*obj
= get_ptid_entry( id
);
450 if (obj
&& obj
->ops
== &process_ops
) return (struct process
*)grab_object( obj
);
451 set_error( STATUS_INVALID_PARAMETER
);
455 /* get a process from a handle (and increment the refcount) */
456 struct process
*get_process_from_handle( obj_handle_t handle
, unsigned int access
)
458 return (struct process
*)get_handle_obj( current
->process
, handle
,
459 access
, &process_ops
);
462 /* find a dll from its base address */
463 static inline struct process_dll
*find_process_dll( struct process
*process
, void *base
)
465 struct process_dll
*dll
;
467 LIST_FOR_EACH_ENTRY( dll
, &process
->dlls
, struct process_dll
, entry
)
469 if (dll
->base
== base
) return dll
;
474 /* add a dll to a process list */
475 static struct process_dll
*process_load_dll( struct process
*process
, struct file
*file
,
476 void *base
, const WCHAR
*filename
, data_size_t name_len
)
478 struct process_dll
*dll
;
480 /* make sure we don't already have one with the same base address */
481 if (find_process_dll( process
, base
))
483 set_error( STATUS_INVALID_PARAMETER
);
487 if ((dll
= mem_alloc( sizeof(*dll
) )))
491 dll
->filename
= NULL
;
492 dll
->namelen
= name_len
;
493 if (name_len
&& !(dll
->filename
= memdup( filename
, name_len
)))
498 if (file
) dll
->file
= grab_file_unless_removable( file
);
499 list_add_tail( &process
->dlls
, &dll
->entry
);
504 /* remove a dll from a process list */
505 static void process_unload_dll( struct process
*process
, void *base
)
507 struct process_dll
*dll
= find_process_dll( process
, base
);
509 if (dll
&& (&dll
->entry
!= list_head( &process
->dlls
))) /* main exe can't be unloaded */
511 if (dll
->file
) release_object( dll
->file
);
512 free( dll
->filename
);
513 list_remove( &dll
->entry
);
515 generate_debug_event( current
, UNLOAD_DLL_DEBUG_EVENT
, base
);
517 else set_error( STATUS_INVALID_PARAMETER
);
520 /* terminate a process with the given exit code */
521 static void terminate_process( struct process
*process
, struct thread
*skip
, int exit_code
)
525 if (skip
&& skip
->process
== process
) /* move it to the end of the list */
527 assert( skip
->state
!= TERMINATED
);
528 list_remove( &skip
->proc_entry
);
529 list_add_tail( &process
->thread_list
, &skip
->proc_entry
);
532 grab_object( process
); /* make sure it doesn't get freed when threads die */
533 while ((ptr
= list_head( &process
->thread_list
)))
535 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
537 if (exit_code
) thread
->exit_code
= exit_code
;
538 if (thread
== skip
) break;
539 kill_thread( thread
, 1 );
541 release_object( process
);
544 /* kill all processes */
545 void kill_all_processes( struct process
*skip
, int exit_code
)
549 struct process
*process
;
551 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
553 if (process
== skip
) continue;
554 if (process
->running_threads
) break;
556 if (&process
->entry
== &process_list
) break; /* no process found */
557 terminate_process( process
, NULL
, exit_code
);
561 /* kill all processes being attached to a console renderer */
562 void kill_console_processes( struct thread
*renderer
, int exit_code
)
564 for (;;) /* restart from the beginning of the list every time */
566 struct process
*process
;
568 /* find the first process being attached to 'renderer' and still running */
569 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
571 if (process
== renderer
->process
) continue;
572 if (!process
->running_threads
) continue;
573 if (process
->console
&& process
->console
->renderer
== renderer
) break;
575 if (&process
->entry
== &process_list
) break; /* no process found */
576 terminate_process( process
, NULL
, exit_code
);
580 /* a process has been killed (i.e. its last thread died) */
581 static void process_killed( struct process
*process
)
583 struct handle_table
*handles
;
586 assert( list_empty( &process
->thread_list
));
587 process
->end_time
= current_time
;
588 close_process_desktop( process
);
589 handles
= process
->handles
;
590 process
->handles
= NULL
;
591 if (handles
) release_object( handles
);
593 /* close the console attached to this process, if any */
594 free_console( process
);
596 while ((ptr
= list_head( &process
->dlls
)))
598 struct process_dll
*dll
= LIST_ENTRY( ptr
, struct process_dll
, entry
);
599 if (dll
->file
) release_object( dll
->file
);
600 free( dll
->filename
);
601 list_remove( &dll
->entry
);
604 destroy_process_classes( process
);
605 remove_process_locks( process
);
606 set_process_startup_state( process
, STARTUP_ABORTED
);
607 finish_process_tracing( process
);
608 start_sigkill_timer( process
);
609 wake_up( &process
->obj
, 0 );
612 /* add a thread to a process running threads list */
613 void add_process_thread( struct process
*process
, struct thread
*thread
)
615 list_add_tail( &process
->thread_list
, &thread
->proc_entry
);
616 if (!process
->running_threads
++) running_processes
++;
617 grab_object( thread
);
620 /* remove a thread from a process running threads list */
621 void remove_process_thread( struct process
*process
, struct thread
*thread
)
623 assert( process
->running_threads
> 0 );
624 assert( !list_empty( &process
->thread_list
));
626 list_remove( &thread
->proc_entry
);
628 if (!--process
->running_threads
)
630 /* we have removed the last running thread, exit the process */
631 process
->exit_code
= thread
->exit_code
;
632 generate_debug_event( thread
, EXIT_PROCESS_DEBUG_EVENT
, process
);
633 process_killed( process
);
635 else generate_debug_event( thread
, EXIT_THREAD_DEBUG_EVENT
, thread
);
636 release_object( thread
);
639 /* suspend all the threads of a process */
640 void suspend_process( struct process
*process
)
642 if (!process
->suspend
++)
644 struct list
*ptr
, *next
;
646 LIST_FOR_EACH_SAFE( ptr
, next
, &process
->thread_list
)
648 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
649 if (!thread
->suspend
) stop_thread( thread
);
654 /* resume all the threads of a process */
655 void resume_process( struct process
*process
)
657 assert (process
->suspend
> 0);
658 if (!--process
->suspend
)
660 struct list
*ptr
, *next
;
662 LIST_FOR_EACH_SAFE( ptr
, next
, &process
->thread_list
)
664 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
665 if (!thread
->suspend
) wake_thread( thread
);
670 /* kill a process on the spot */
671 void kill_process( struct process
*process
, int violent_death
)
673 if (violent_death
) terminate_process( process
, NULL
, 1 );
678 grab_object( process
); /* make sure it doesn't get freed when threads die */
679 while ((ptr
= list_head( &process
->thread_list
)))
681 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
682 kill_thread( thread
, 0 );
684 release_object( process
);
688 /* kill all processes being debugged by a given thread */
689 void kill_debugged_processes( struct thread
*debugger
, int exit_code
)
691 for (;;) /* restart from the beginning of the list every time */
693 struct process
*process
;
695 /* find the first process being debugged by 'debugger' and still running */
696 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
698 if (!process
->running_threads
) continue;
699 if (process
->debugger
== debugger
) break;
701 if (&process
->entry
== &process_list
) break; /* no process found */
702 process
->debugger
= NULL
;
703 terminate_process( process
, NULL
, exit_code
);
708 /* trigger a breakpoint event in a given process */
709 void break_process( struct process
*process
)
711 struct thread
*thread
;
713 suspend_process( process
);
715 LIST_FOR_EACH_ENTRY( thread
, &process
->thread_list
, struct thread
, proc_entry
)
717 if (thread
->context
) /* inside an exception event already */
719 break_thread( thread
);
723 if ((thread
= get_process_first_thread( process
))) thread
->debug_break
= 1;
724 else set_error( STATUS_ACCESS_DENIED
);
726 resume_process( process
);
730 /* detach a debugger from all its debuggees */
731 void detach_debugged_processes( struct thread
*debugger
)
733 struct process
*process
;
735 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
737 if (process
->debugger
== debugger
&& process
->running_threads
)
739 debugger_detach( process
, debugger
);
745 void enum_processes( int (*cb
)(struct process
*, void*), void *user
)
747 struct list
*ptr
, *next
;
749 LIST_FOR_EACH_SAFE( ptr
, next
, &process_list
)
751 struct process
*process
= LIST_ENTRY( ptr
, struct process
, entry
);
752 if ((cb
)(process
, user
)) break;
756 /* set the debugged flag in the process PEB */
757 int set_process_debug_flag( struct process
*process
, int flag
)
759 char data
= (flag
!= 0);
761 /* BeingDebugged flag is the byte at offset 2 in the PEB */
762 return write_process_memory( process
, (char *)process
->peb
+ 2, 1, &data
);
765 /* take a snapshot of currently running processes */
766 struct process_snapshot
*process_snap( int *count
)
768 struct process_snapshot
*snapshot
, *ptr
;
769 struct process
*process
;
771 if (!running_processes
) return NULL
;
772 if (!(snapshot
= mem_alloc( sizeof(*snapshot
) * running_processes
)))
775 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
777 if (!process
->running_threads
) continue;
778 ptr
->process
= process
;
779 ptr
->threads
= process
->running_threads
;
780 ptr
->count
= process
->obj
.refcount
;
781 ptr
->priority
= process
->priority
;
782 ptr
->handles
= get_handle_table_count(process
);
783 grab_object( process
);
787 if (!(*count
= ptr
- snapshot
))
795 /* take a snapshot of the modules of a process */
796 struct module_snapshot
*module_snap( struct process
*process
, int *count
)
798 struct module_snapshot
*snapshot
, *ptr
;
799 struct process_dll
*dll
;
802 LIST_FOR_EACH_ENTRY( dll
, &process
->dlls
, struct process_dll
, entry
) total
++;
803 if (!(snapshot
= mem_alloc( sizeof(*snapshot
) * total
))) return NULL
;
806 LIST_FOR_EACH_ENTRY( dll
, &process
->dlls
, struct process_dll
, entry
)
808 ptr
->base
= dll
->base
;
809 ptr
->size
= dll
->size
;
810 ptr
->namelen
= dll
->namelen
;
811 ptr
->filename
= memdup( dll
->filename
, dll
->namelen
);
819 /* create a new process */
820 DECL_HANDLER(new_process
)
822 struct startup_info
*info
;
823 struct thread
*thread
;
824 struct process
*process
;
825 struct process
*parent
= current
->process
;
826 int socket_fd
= thread_get_inflight_fd( current
, req
->socket_fd
);
830 set_error( STATUS_INVALID_PARAMETER
);
833 if (fcntl( socket_fd
, F_SETFL
, O_NONBLOCK
) == -1)
835 set_error( STATUS_INVALID_HANDLE
);
840 /* build the startup info for a new process */
841 if (!(info
= alloc_object( &startup_info_ops
))) return;
842 info
->hstdin
= req
->hstdin
;
843 info
->hstdout
= req
->hstdout
;
844 info
->hstderr
= req
->hstderr
;
845 info
->exe_file
= NULL
;
846 info
->process
= NULL
;
847 info
->data_size
= get_req_data_size();
851 !(info
->exe_file
= get_file_obj( current
->process
, req
->exe_file
, FILE_READ_DATA
)))
854 if (!(info
->data
= memdup( get_req_data(), info
->data_size
))) goto done
;
856 if (!(thread
= create_process( socket_fd
, current
, req
->inherit_all
))) goto done
;
857 process
= thread
->process
;
858 process
->create_flags
= req
->create_flags
;
859 process
->startup_info
= (struct startup_info
*)grab_object( info
);
861 /* connect to the window station */
862 connect_process_winstation( process
, current
);
864 /* thread will be actually suspended in init_done */
865 if (req
->create_flags
& CREATE_SUSPENDED
) thread
->suspend
++;
867 /* set the process console */
868 if (!(req
->create_flags
& (DETACHED_PROCESS
| CREATE_NEW_CONSOLE
)))
870 /* FIXME: some better error checking should be done...
871 * like if hConOut and hConIn are console handles, then they should be on the same
874 inherit_console( current
, process
, req
->inherit_all
? req
->hstdin
: 0 );
877 if (!req
->inherit_all
&& !(req
->create_flags
& CREATE_NEW_CONSOLE
))
879 info
->hstdin
= duplicate_handle( parent
, req
->hstdin
, process
,
880 0, OBJ_INHERIT
, DUPLICATE_SAME_ACCESS
);
881 info
->hstdout
= duplicate_handle( parent
, req
->hstdout
, process
,
882 0, OBJ_INHERIT
, DUPLICATE_SAME_ACCESS
);
883 info
->hstderr
= duplicate_handle( parent
, req
->hstderr
, process
,
884 0, OBJ_INHERIT
, DUPLICATE_SAME_ACCESS
);
885 /* some handles above may have been invalid; this is not an error */
886 if (get_error() == STATUS_INVALID_HANDLE
||
887 get_error() == STATUS_OBJECT_TYPE_MISMATCH
) clear_error();
890 /* attach to the debugger if requested */
891 if (req
->create_flags
& (DEBUG_PROCESS
| DEBUG_ONLY_THIS_PROCESS
))
892 set_process_debugger( process
, current
);
893 else if (parent
->debugger
&& !(parent
->create_flags
& DEBUG_ONLY_THIS_PROCESS
))
894 set_process_debugger( process
, parent
->debugger
);
896 if (!(req
->create_flags
& CREATE_NEW_PROCESS_GROUP
))
897 process
->group_id
= parent
->group_id
;
899 info
->process
= (struct process
*)grab_object( process
);
900 reply
->info
= alloc_handle( current
->process
, info
, SYNCHRONIZE
, 0 );
901 reply
->pid
= get_process_id( process
);
902 reply
->tid
= get_thread_id( thread
);
903 reply
->phandle
= alloc_handle( parent
, process
, req
->process_access
, req
->process_attr
);
904 reply
->thandle
= alloc_handle( parent
, thread
, req
->thread_access
, req
->thread_attr
);
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
->success
= is_process_init_done( info
->process
);
919 reply
->exit_code
= info
->process
->exit_code
;
920 release_object( info
);
924 /* Retrieve the new process startup info */
925 DECL_HANDLER(get_startup_info
)
927 struct process
*process
= current
->process
;
928 struct startup_info
*info
= process
->startup_info
;
933 if (info
->exe_file
&&
934 !(reply
->exe_file
= alloc_handle( process
, info
->exe_file
, GENERIC_READ
, 0 ))) return;
936 reply
->hstdin
= info
->hstdin
;
937 reply
->hstdout
= info
->hstdout
;
938 reply
->hstderr
= info
->hstderr
;
940 /* we return the data directly without making a copy so this can only be called once */
941 size
= info
->data_size
;
942 if (size
> get_reply_max_size()) size
= get_reply_max_size();
943 set_reply_data_ptr( info
->data
, size
);
948 /* signal the end of the process initialization */
949 DECL_HANDLER(init_process_done
)
951 struct process_dll
*dll
;
952 struct process
*process
= current
->process
;
954 if (is_process_init_done(process
))
956 set_error( STATUS_INVALID_PARAMETER
);
959 if (!(dll
= find_process_dll( process
, req
->module
)))
961 set_error( STATUS_DLL_NOT_FOUND
);
965 /* main exe is the first in the dll list */
966 list_remove( &dll
->entry
);
967 list_add_head( &process
->dlls
, &dll
->entry
);
969 generate_startup_debug_events( process
, req
->entry
);
970 set_process_startup_state( process
, STARTUP_DONE
);
972 if (req
->gui
) process
->idle_event
= create_event( NULL
, NULL
, 0, 1, 0 );
973 if (current
->suspend
+ process
->suspend
> 0) stop_thread( current
);
974 if (process
->debugger
) set_process_debug_flag( process
, 1 );
977 /* open a handle to a process */
978 DECL_HANDLER(open_process
)
980 struct process
*process
= get_process_from_id( req
->pid
);
984 reply
->handle
= alloc_handle( current
->process
, process
, req
->access
, req
->attributes
);
985 release_object( process
);
989 /* terminate a process */
990 DECL_HANDLER(terminate_process
)
992 struct process
*process
;
994 if ((process
= get_process_from_handle( req
->handle
, PROCESS_TERMINATE
)))
996 reply
->self
= (current
->process
== process
);
997 terminate_process( process
, current
, req
->exit_code
);
998 release_object( process
);
1002 /* fetch information about a process */
1003 DECL_HANDLER(get_process_info
)
1005 struct process
*process
;
1007 if ((process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_INFORMATION
)))
1009 reply
->pid
= get_process_id( process
);
1010 reply
->ppid
= process
->parent
? get_process_id( process
->parent
) : 0;
1011 reply
->exit_code
= process
->exit_code
;
1012 reply
->priority
= process
->priority
;
1013 reply
->affinity
= process
->affinity
;
1014 reply
->peb
= process
->peb
;
1015 reply
->start_time
.sec
= process
->start_time
.tv_sec
;
1016 reply
->start_time
.usec
= process
->start_time
.tv_usec
;
1017 reply
->end_time
.sec
= process
->end_time
.tv_sec
;
1018 reply
->end_time
.usec
= process
->end_time
.tv_usec
;
1019 release_object( process
);
1023 /* set information about a process */
1024 DECL_HANDLER(set_process_info
)
1026 struct process
*process
;
1028 if ((process
= get_process_from_handle( req
->handle
, PROCESS_SET_INFORMATION
)))
1030 if (req
->mask
& SET_PROCESS_INFO_PRIORITY
) process
->priority
= req
->priority
;
1031 if (req
->mask
& SET_PROCESS_INFO_AFFINITY
)
1033 if (req
->affinity
!= 1) set_error( STATUS_INVALID_PARAMETER
);
1034 else process
->affinity
= req
->affinity
;
1036 release_object( process
);
1040 /* read data from a process address space */
1041 DECL_HANDLER(read_process_memory
)
1043 struct process
*process
;
1044 data_size_t len
= get_reply_max_size();
1046 if (!(process
= get_process_from_handle( req
->handle
, PROCESS_VM_READ
))) return;
1050 char *buffer
= mem_alloc( len
);
1053 if (read_process_memory( process
, req
->addr
, len
, buffer
))
1054 set_reply_data_ptr( buffer
, len
);
1059 release_object( process
);
1062 /* write data to a process address space */
1063 DECL_HANDLER(write_process_memory
)
1065 struct process
*process
;
1067 if ((process
= get_process_from_handle( req
->handle
, PROCESS_VM_WRITE
)))
1069 data_size_t len
= get_req_data_size();
1070 if (len
) write_process_memory( process
, req
->addr
, len
, get_req_data() );
1071 else set_error( STATUS_INVALID_PARAMETER
);
1072 release_object( process
);
1076 /* notify the server that a dll has been loaded */
1077 DECL_HANDLER(load_dll
)
1079 struct process_dll
*dll
;
1080 struct file
*file
= NULL
;
1082 if (req
->handle
&& !(file
= get_file_obj( current
->process
, req
->handle
, FILE_READ_DATA
)))
1085 if ((dll
= process_load_dll( current
->process
, file
, req
->base
,
1086 get_req_data(), get_req_data_size() )))
1088 dll
->size
= req
->size
;
1089 dll
->dbg_offset
= req
->dbg_offset
;
1090 dll
->dbg_size
= req
->dbg_size
;
1091 dll
->name
= req
->name
;
1092 /* only generate event if initialization is done */
1093 if (is_process_init_done( current
->process
))
1094 generate_debug_event( current
, LOAD_DLL_DEBUG_EVENT
, dll
);
1096 if (file
) release_object( file
);
1099 /* notify the server that a dll is being unloaded */
1100 DECL_HANDLER(unload_dll
)
1102 process_unload_dll( current
->process
, req
->base
);
1105 /* retrieve information about a module in a process */
1106 DECL_HANDLER(get_dll_info
)
1108 struct process
*process
;
1110 if ((process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_INFORMATION
)))
1112 struct process_dll
*dll
= find_process_dll( process
, req
->base_address
);
1116 reply
->size
= dll
->size
;
1117 reply
->entry_point
= NULL
; /* FIXME */
1120 data_size_t len
= min( dll
->namelen
, get_reply_max_size() );
1121 set_reply_data( dll
->filename
, len
);
1125 set_error( STATUS_DLL_NOT_FOUND
);
1127 release_object( process
);
1131 /* retrieve the process idle event */
1132 DECL_HANDLER(get_process_idle_event
)
1134 struct process
*process
;
1137 if ((process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_INFORMATION
)))
1139 if (process
->idle_event
&& process
!= current
->process
)
1140 reply
->event
= alloc_handle( current
->process
, process
->idle_event
,
1141 EVENT_ALL_ACCESS
, 0 );
1142 release_object( process
);