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 no_get_file_info
, /* get_file_info */
89 no_queue_async
, /* queue_async */
90 no_cancel_async
/* cancel async */
93 /* process startup info */
97 struct object obj
; /* object header */
98 obj_handle_t hstdin
; /* handle for stdin */
99 obj_handle_t hstdout
; /* handle for stdout */
100 obj_handle_t hstderr
; /* handle for stderr */
101 struct file
*exe_file
; /* file handle for main exe */
102 struct process
*process
; /* created process */
103 data_size_t data_size
; /* size of startup data */
104 void *data
; /* data for startup info */
107 static void startup_info_dump( struct object
*obj
, int verbose
);
108 static int startup_info_signaled( struct object
*obj
, struct thread
*thread
);
109 static void startup_info_destroy( struct object
*obj
);
111 static const struct object_ops startup_info_ops
=
113 sizeof(struct startup_info
), /* size */
114 startup_info_dump
, /* dump */
115 add_queue
, /* add_queue */
116 remove_queue
, /* remove_queue */
117 startup_info_signaled
, /* signaled */
118 no_satisfied
, /* satisfied */
119 no_signal
, /* signal */
120 no_get_fd
, /* get_fd */
121 no_map_access
, /* map_access */
122 no_lookup_name
, /* lookup_name */
123 no_open_file
, /* open_file */
124 no_close_handle
, /* close_handle */
125 startup_info_destroy
/* destroy */
131 void *ptr
; /* entry ptr */
132 unsigned int next
; /* next free entry */
135 static struct ptid_entry
*ptid_entries
; /* array of ptid entries */
136 static unsigned int used_ptid_entries
; /* number of entries in use */
137 static unsigned int alloc_ptid_entries
; /* number of allocated entries */
138 static unsigned int next_free_ptid
; /* next free entry */
139 static unsigned int last_free_ptid
; /* last free entry */
141 #define PTID_OFFSET 8 /* offset for first ptid value */
143 /* allocate a new process or thread id */
144 unsigned int alloc_ptid( void *ptr
)
146 struct ptid_entry
*entry
;
149 if (used_ptid_entries
< alloc_ptid_entries
)
151 id
= used_ptid_entries
+ PTID_OFFSET
;
152 entry
= &ptid_entries
[used_ptid_entries
++];
154 else if (next_free_ptid
)
157 entry
= &ptid_entries
[id
- PTID_OFFSET
];
158 if (!(next_free_ptid
= entry
->next
)) last_free_ptid
= 0;
160 else /* need to grow the array */
162 unsigned int count
= alloc_ptid_entries
+ (alloc_ptid_entries
/ 2);
163 if (!count
) count
= 64;
164 if (!(entry
= realloc( ptid_entries
, count
* sizeof(*entry
) )))
166 set_error( STATUS_NO_MEMORY
);
169 ptid_entries
= entry
;
170 alloc_ptid_entries
= count
;
171 id
= used_ptid_entries
+ PTID_OFFSET
;
172 entry
= &ptid_entries
[used_ptid_entries
++];
179 /* free a process or thread id */
180 void free_ptid( unsigned int id
)
182 struct ptid_entry
*entry
= &ptid_entries
[id
- PTID_OFFSET
];
187 /* append to end of free list so that we don't reuse it too early */
188 if (last_free_ptid
) ptid_entries
[last_free_ptid
- PTID_OFFSET
].next
= id
;
189 else next_free_ptid
= id
;
194 /* retrieve the pointer corresponding to a process or thread id */
195 void *get_ptid_entry( unsigned int id
)
197 if (id
< PTID_OFFSET
) return NULL
;
198 if (id
- PTID_OFFSET
>= used_ptid_entries
) return NULL
;
199 return ptid_entries
[id
- PTID_OFFSET
].ptr
;
202 /* return the main thread of the process */
203 struct thread
*get_process_first_thread( struct process
*process
)
205 struct list
*ptr
= list_head( &process
->thread_list
);
206 if (!ptr
) return NULL
;
207 return LIST_ENTRY( ptr
, struct thread
, proc_entry
);
210 /* set the state of the process startup info */
211 static void set_process_startup_state( struct process
*process
, enum startup_state state
)
213 if (process
->startup_state
== STARTUP_IN_PROGRESS
) process
->startup_state
= state
;
214 if (process
->startup_info
)
216 wake_up( &process
->startup_info
->obj
, 0 );
217 release_object( process
->startup_info
);
218 process
->startup_info
= NULL
;
222 /* final cleanup once we are sure a process is really dead */
223 static void process_died( struct process
*process
)
225 if (debug_level
) fprintf( stderr
, "%04x: *process killed*\n", process
->id
);
226 release_object( process
);
227 if (!--running_processes
) close_master_socket();
230 /* callback for process sigkill timeout */
231 static void process_sigkill( void *private )
233 struct process
*process
= private;
235 process
->sigkill_timeout
= NULL
;
236 kill( process
->unix_pid
, SIGKILL
);
237 process_died( process
);
240 /* start the sigkill timer for a process upon exit */
241 static void start_sigkill_timer( struct process
*process
)
243 grab_object( process
);
244 if (process
->unix_pid
!= -1 && process
->msg_fd
)
246 struct timeval when
= current_time
;
248 add_timeout( &when
, 1000 );
249 process
->sigkill_timeout
= add_timeout_user( &when
, process_sigkill
, process
);
251 else process_died( process
);
254 /* create a new process and its main thread */
255 /* if the function fails the fd is closed */
256 struct thread
*create_process( int fd
, struct thread
*parent_thread
, int inherit_all
)
258 struct process
*process
;
259 struct thread
*thread
= NULL
;
262 if (!(process
= alloc_object( &process_ops
)))
267 process
->parent
= NULL
;
268 process
->debugger
= NULL
;
269 process
->handles
= NULL
;
270 process
->msg_fd
= NULL
;
271 process
->sigkill_timeout
= NULL
;
272 process
->unix_pid
= -1;
273 process
->exit_code
= STILL_ACTIVE
;
274 process
->running_threads
= 0;
275 process
->priority
= PROCESS_PRIOCLASS_NORMAL
;
276 process
->affinity
= 1;
277 process
->suspend
= 0;
278 process
->create_flags
= 0;
279 process
->console
= NULL
;
280 process
->startup_state
= STARTUP_IN_PROGRESS
;
281 process
->startup_info
= NULL
;
282 process
->idle_event
= NULL
;
283 process
->queue
= NULL
;
285 process
->ldt_copy
= NULL
;
286 process
->winstation
= 0;
287 process
->desktop
= 0;
288 process
->token
= token_create_admin();
289 process
->trace_data
= 0;
290 list_init( &process
->thread_list
);
291 list_init( &process
->locks
);
292 list_init( &process
->classes
);
293 list_init( &process
->dlls
);
295 process
->start_time
= current_time
;
296 process
->end_time
.tv_sec
= process
->end_time
.tv_usec
= 0;
297 list_add_head( &process_list
, &process
->entry
);
299 if (!(process
->id
= process
->group_id
= alloc_ptid( process
)))
304 if (!(process
->msg_fd
= create_anonymous_fd( &process_fd_ops
, fd
, &process
->obj
))) goto error
;
306 /* create the handle table */
307 if (!parent_thread
) process
->handles
= alloc_handle_table( process
, 0 );
310 struct process
*parent
= parent_thread
->process
;
311 process
->parent
= (struct process
*)grab_object( parent
);
312 process
->handles
= inherit_all
? copy_handle_table( process
, parent
)
313 : alloc_handle_table( process
, 0 );
315 if (!process
->handles
) goto error
;
317 /* create the main thread */
318 if (pipe( request_pipe
) == -1)
323 if (send_client_fd( process
, request_pipe
[1], 0 ) == -1)
325 close( request_pipe
[0] );
326 close( request_pipe
[1] );
329 close( request_pipe
[1] );
330 if (!(thread
= create_thread( request_pipe
[0], process
))) goto error
;
332 set_fd_events( process
->msg_fd
, POLLIN
); /* start listening to events */
333 release_object( process
);
337 if (process
) release_object( process
);
338 /* if we failed to start our first process, close everything down */
339 if (!running_processes
) close_master_socket();
343 /* initialize the current process and fill in the request */
344 data_size_t
init_process( struct thread
*thread
)
346 struct process
*process
= thread
->process
;
347 struct startup_info
*info
= process
->startup_info
;
349 init_process_tracing( process
);
351 return info
->data_size
;
354 /* destroy a process when its refcount is 0 */
355 static void process_destroy( struct object
*obj
)
357 struct process
*process
= (struct process
*)obj
;
358 assert( obj
->ops
== &process_ops
);
360 /* we can't have a thread remaining */
361 assert( list_empty( &process
->thread_list
));
363 assert( !process
->sigkill_timeout
); /* timeout should hold a reference to the process */
365 set_process_startup_state( process
, STARTUP_ABORTED
);
366 if (process
->console
) release_object( process
->console
);
367 if (process
->parent
) release_object( process
->parent
);
368 if (process
->msg_fd
) release_object( process
->msg_fd
);
369 list_remove( &process
->entry
);
370 if (process
->idle_event
) release_object( process
->idle_event
);
371 if (process
->queue
) release_object( process
->queue
);
372 if (process
->id
) free_ptid( process
->id
);
373 if (process
->token
) release_object( process
->token
);
376 /* dump a process on stdout for debugging purposes */
377 static void process_dump( struct object
*obj
, int verbose
)
379 struct process
*process
= (struct process
*)obj
;
380 assert( obj
->ops
== &process_ops
);
382 fprintf( stderr
, "Process id=%04x handles=%p\n", process
->id
, process
->handles
);
385 static int process_signaled( struct object
*obj
, struct thread
*thread
)
387 struct process
*process
= (struct process
*)obj
;
388 return !process
->running_threads
;
391 static unsigned int process_map_access( struct object
*obj
, unsigned int access
)
393 if (access
& GENERIC_READ
) access
|= STANDARD_RIGHTS_READ
| SYNCHRONIZE
;
394 if (access
& GENERIC_WRITE
) access
|= STANDARD_RIGHTS_WRITE
| SYNCHRONIZE
;
395 if (access
& GENERIC_EXECUTE
) access
|= STANDARD_RIGHTS_EXECUTE
;
396 if (access
& GENERIC_ALL
) access
|= PROCESS_ALL_ACCESS
;
397 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
400 static void process_poll_event( struct fd
*fd
, int event
)
402 struct process
*process
= get_fd_user( fd
);
403 assert( process
->obj
.ops
== &process_ops
);
405 if (event
& (POLLERR
| POLLHUP
))
407 release_object( process
->msg_fd
);
408 process
->msg_fd
= NULL
;
409 if (process
->sigkill_timeout
) /* already waiting for it to die */
411 remove_timeout_user( process
->sigkill_timeout
);
412 process
->sigkill_timeout
= NULL
;
413 process_died( process
);
415 else kill_process( process
, 0 );
417 else if (event
& POLLIN
) receive_fd( process
);
420 static void startup_info_destroy( struct object
*obj
)
422 struct startup_info
*info
= (struct startup_info
*)obj
;
423 assert( obj
->ops
== &startup_info_ops
);
425 if (info
->exe_file
) release_object( info
->exe_file
);
426 if (info
->process
) release_object( info
->process
);
429 static void startup_info_dump( struct object
*obj
, int verbose
)
431 struct startup_info
*info
= (struct startup_info
*)obj
;
432 assert( obj
->ops
== &startup_info_ops
);
434 fprintf( stderr
, "Startup info in=%p out=%p err=%p\n",
435 info
->hstdin
, info
->hstdout
, info
->hstderr
);
438 static int startup_info_signaled( struct object
*obj
, struct thread
*thread
)
440 struct startup_info
*info
= (struct startup_info
*)obj
;
441 return info
->process
&& info
->process
->startup_state
!= STARTUP_IN_PROGRESS
;
444 /* get a process from an id (and increment the refcount) */
445 struct process
*get_process_from_id( process_id_t id
)
447 struct object
*obj
= get_ptid_entry( id
);
449 if (obj
&& obj
->ops
== &process_ops
) return (struct process
*)grab_object( obj
);
450 set_error( STATUS_INVALID_PARAMETER
);
454 /* get a process from a handle (and increment the refcount) */
455 struct process
*get_process_from_handle( obj_handle_t handle
, unsigned int access
)
457 return (struct process
*)get_handle_obj( current
->process
, handle
,
458 access
, &process_ops
);
461 /* find a dll from its base address */
462 static inline struct process_dll
*find_process_dll( struct process
*process
, void *base
)
464 struct process_dll
*dll
;
466 LIST_FOR_EACH_ENTRY( dll
, &process
->dlls
, struct process_dll
, entry
)
468 if (dll
->base
== base
) return dll
;
473 /* add a dll to a process list */
474 static struct process_dll
*process_load_dll( struct process
*process
, struct file
*file
,
475 void *base
, const WCHAR
*filename
, data_size_t name_len
)
477 struct process_dll
*dll
;
479 /* make sure we don't already have one with the same base address */
480 if (find_process_dll( process
, base
))
482 set_error( STATUS_INVALID_PARAMETER
);
486 if ((dll
= mem_alloc( sizeof(*dll
) )))
490 dll
->filename
= NULL
;
491 dll
->namelen
= name_len
;
492 if (name_len
&& !(dll
->filename
= memdup( filename
, name_len
)))
497 if (file
) dll
->file
= grab_file_unless_removable( file
);
498 list_add_tail( &process
->dlls
, &dll
->entry
);
503 /* remove a dll from a process list */
504 static void process_unload_dll( struct process
*process
, void *base
)
506 struct process_dll
*dll
= find_process_dll( process
, base
);
508 if (dll
&& (&dll
->entry
!= list_head( &process
->dlls
))) /* main exe can't be unloaded */
510 if (dll
->file
) release_object( dll
->file
);
511 free( dll
->filename
);
512 list_remove( &dll
->entry
);
514 generate_debug_event( current
, UNLOAD_DLL_DEBUG_EVENT
, base
);
516 else set_error( STATUS_INVALID_PARAMETER
);
519 /* terminate a process with the given exit code */
520 static void terminate_process( struct process
*process
, struct thread
*skip
, int exit_code
)
524 if (skip
&& skip
->process
== process
) /* move it to the end of the list */
526 assert( skip
->state
!= TERMINATED
);
527 list_remove( &skip
->proc_entry
);
528 list_add_tail( &process
->thread_list
, &skip
->proc_entry
);
531 grab_object( process
); /* make sure it doesn't get freed when threads die */
532 while ((ptr
= list_head( &process
->thread_list
)))
534 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
536 if (exit_code
) thread
->exit_code
= exit_code
;
537 if (thread
== skip
) break;
538 kill_thread( thread
, 1 );
540 release_object( process
);
543 /* kill all processes */
544 void kill_all_processes( struct process
*skip
, int exit_code
)
548 struct process
*process
;
550 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
552 if (process
== skip
) continue;
553 if (process
->running_threads
) break;
555 if (&process
->entry
== &process_list
) break; /* no process found */
556 terminate_process( process
, NULL
, exit_code
);
560 /* kill all processes being attached to a console renderer */
561 void kill_console_processes( struct thread
*renderer
, int exit_code
)
563 for (;;) /* restart from the beginning of the list every time */
565 struct process
*process
;
567 /* find the first process being attached to 'renderer' and still running */
568 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
570 if (process
== renderer
->process
) continue;
571 if (!process
->running_threads
) continue;
572 if (process
->console
&& process
->console
->renderer
== renderer
) break;
574 if (&process
->entry
== &process_list
) break; /* no process found */
575 terminate_process( process
, NULL
, exit_code
);
579 /* a process has been killed (i.e. its last thread died) */
580 static void process_killed( struct process
*process
)
582 struct handle_table
*handles
;
585 assert( list_empty( &process
->thread_list
));
586 process
->end_time
= current_time
;
587 close_process_desktop( process
);
588 handles
= process
->handles
;
589 process
->handles
= NULL
;
590 if (handles
) release_object( handles
);
592 /* close the console attached to this process, if any */
593 free_console( process
);
595 while ((ptr
= list_head( &process
->dlls
)))
597 struct process_dll
*dll
= LIST_ENTRY( ptr
, struct process_dll
, entry
);
598 if (dll
->file
) release_object( dll
->file
);
599 free( dll
->filename
);
600 list_remove( &dll
->entry
);
603 destroy_process_classes( process
);
604 remove_process_locks( process
);
605 set_process_startup_state( process
, STARTUP_ABORTED
);
606 finish_process_tracing( process
);
607 start_sigkill_timer( process
);
608 wake_up( &process
->obj
, 0 );
611 /* add a thread to a process running threads list */
612 void add_process_thread( struct process
*process
, struct thread
*thread
)
614 list_add_tail( &process
->thread_list
, &thread
->proc_entry
);
615 if (!process
->running_threads
++) running_processes
++;
616 grab_object( thread
);
619 /* remove a thread from a process running threads list */
620 void remove_process_thread( struct process
*process
, struct thread
*thread
)
622 assert( process
->running_threads
> 0 );
623 assert( !list_empty( &process
->thread_list
));
625 list_remove( &thread
->proc_entry
);
627 if (!--process
->running_threads
)
629 /* we have removed the last running thread, exit the process */
630 process
->exit_code
= thread
->exit_code
;
631 generate_debug_event( thread
, EXIT_PROCESS_DEBUG_EVENT
, process
);
632 process_killed( process
);
634 else generate_debug_event( thread
, EXIT_THREAD_DEBUG_EVENT
, thread
);
635 release_object( thread
);
638 /* suspend all the threads of a process */
639 void suspend_process( struct process
*process
)
641 if (!process
->suspend
++)
643 struct list
*ptr
, *next
;
645 LIST_FOR_EACH_SAFE( ptr
, next
, &process
->thread_list
)
647 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
648 if (!thread
->suspend
) stop_thread( thread
);
653 /* resume all the threads of a process */
654 void resume_process( struct process
*process
)
656 assert (process
->suspend
> 0);
657 if (!--process
->suspend
)
659 struct list
*ptr
, *next
;
661 LIST_FOR_EACH_SAFE( ptr
, next
, &process
->thread_list
)
663 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
664 if (!thread
->suspend
) wake_thread( thread
);
669 /* kill a process on the spot */
670 void kill_process( struct process
*process
, int violent_death
)
672 if (violent_death
) terminate_process( process
, NULL
, 1 );
677 grab_object( process
); /* make sure it doesn't get freed when threads die */
678 while ((ptr
= list_head( &process
->thread_list
)))
680 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
681 kill_thread( thread
, 0 );
683 release_object( process
);
687 /* kill all processes being debugged by a given thread */
688 void kill_debugged_processes( struct thread
*debugger
, int exit_code
)
690 for (;;) /* restart from the beginning of the list every time */
692 struct process
*process
;
694 /* find the first process being debugged by 'debugger' and still running */
695 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
697 if (!process
->running_threads
) continue;
698 if (process
->debugger
== debugger
) break;
700 if (&process
->entry
== &process_list
) break; /* no process found */
701 process
->debugger
= NULL
;
702 terminate_process( process
, NULL
, exit_code
);
707 /* trigger a breakpoint event in a given process */
708 void break_process( struct process
*process
)
710 struct thread
*thread
;
712 suspend_process( process
);
714 LIST_FOR_EACH_ENTRY( thread
, &process
->thread_list
, struct thread
, proc_entry
)
716 if (thread
->context
) /* inside an exception event already */
718 break_thread( thread
);
722 if ((thread
= get_process_first_thread( process
))) thread
->debug_break
= 1;
723 else set_error( STATUS_ACCESS_DENIED
);
725 resume_process( process
);
729 /* detach a debugger from all its debuggees */
730 void detach_debugged_processes( struct thread
*debugger
)
732 struct process
*process
;
734 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
736 if (process
->debugger
== debugger
&& process
->running_threads
)
738 debugger_detach( process
, debugger
);
744 void enum_processes( int (*cb
)(struct process
*, void*), void *user
)
746 struct list
*ptr
, *next
;
748 LIST_FOR_EACH_SAFE( ptr
, next
, &process_list
)
750 struct process
*process
= LIST_ENTRY( ptr
, struct process
, entry
);
751 if ((cb
)(process
, user
)) break;
755 /* set the debugged flag in the process PEB */
756 int set_process_debug_flag( struct process
*process
, int flag
)
758 char data
= (flag
!= 0);
760 /* BeingDebugged flag is the byte at offset 2 in the PEB */
761 return write_process_memory( process
, (char *)process
->peb
+ 2, 1, &data
);
764 /* take a snapshot of currently running processes */
765 struct process_snapshot
*process_snap( int *count
)
767 struct process_snapshot
*snapshot
, *ptr
;
768 struct process
*process
;
770 if (!running_processes
) return NULL
;
771 if (!(snapshot
= mem_alloc( sizeof(*snapshot
) * running_processes
)))
774 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
776 if (!process
->running_threads
) continue;
777 ptr
->process
= process
;
778 ptr
->threads
= process
->running_threads
;
779 ptr
->count
= process
->obj
.refcount
;
780 ptr
->priority
= process
->priority
;
781 ptr
->handles
= get_handle_table_count(process
);
782 grab_object( process
);
786 if (!(*count
= ptr
- snapshot
))
794 /* take a snapshot of the modules of a process */
795 struct module_snapshot
*module_snap( struct process
*process
, int *count
)
797 struct module_snapshot
*snapshot
, *ptr
;
798 struct process_dll
*dll
;
801 LIST_FOR_EACH_ENTRY( dll
, &process
->dlls
, struct process_dll
, entry
) total
++;
802 if (!(snapshot
= mem_alloc( sizeof(*snapshot
) * total
))) return NULL
;
805 LIST_FOR_EACH_ENTRY( dll
, &process
->dlls
, struct process_dll
, entry
)
807 ptr
->base
= dll
->base
;
808 ptr
->size
= dll
->size
;
809 ptr
->namelen
= dll
->namelen
;
810 ptr
->filename
= memdup( dll
->filename
, dll
->namelen
);
818 /* create a new process */
819 DECL_HANDLER(new_process
)
821 struct startup_info
*info
;
822 struct thread
*thread
;
823 struct process
*process
;
824 struct process
*parent
= current
->process
;
825 int socket_fd
= thread_get_inflight_fd( current
, req
->socket_fd
);
829 set_error( STATUS_INVALID_PARAMETER
);
832 if (fcntl( socket_fd
, F_SETFL
, O_NONBLOCK
) == -1)
834 set_error( STATUS_INVALID_HANDLE
);
839 /* build the startup info for a new process */
840 if (!(info
= alloc_object( &startup_info_ops
))) return;
841 info
->hstdin
= req
->hstdin
;
842 info
->hstdout
= req
->hstdout
;
843 info
->hstderr
= req
->hstderr
;
844 info
->exe_file
= NULL
;
845 info
->process
= NULL
;
846 info
->data_size
= get_req_data_size();
850 !(info
->exe_file
= get_file_obj( current
->process
, req
->exe_file
, FILE_READ_DATA
)))
853 if (!(info
->data
= memdup( get_req_data(), info
->data_size
))) goto done
;
855 if (!(thread
= create_process( socket_fd
, current
, req
->inherit_all
))) goto done
;
856 process
= thread
->process
;
857 process
->create_flags
= req
->create_flags
;
858 process
->startup_info
= (struct startup_info
*)grab_object( info
);
860 /* connect to the window station */
861 connect_process_winstation( process
, current
);
863 /* thread will be actually suspended in init_done */
864 if (req
->create_flags
& CREATE_SUSPENDED
) thread
->suspend
++;
866 /* set the process console */
867 if (!(req
->create_flags
& (DETACHED_PROCESS
| CREATE_NEW_CONSOLE
)))
869 /* FIXME: some better error checking should be done...
870 * like if hConOut and hConIn are console handles, then they should be on the same
873 inherit_console( current
, process
, req
->inherit_all
? req
->hstdin
: 0 );
876 if (!req
->inherit_all
&& !(req
->create_flags
& CREATE_NEW_CONSOLE
))
878 info
->hstdin
= duplicate_handle( parent
, req
->hstdin
, process
,
879 0, OBJ_INHERIT
, DUPLICATE_SAME_ACCESS
);
880 info
->hstdout
= duplicate_handle( parent
, req
->hstdout
, process
,
881 0, OBJ_INHERIT
, DUPLICATE_SAME_ACCESS
);
882 info
->hstderr
= duplicate_handle( parent
, req
->hstderr
, process
,
883 0, OBJ_INHERIT
, DUPLICATE_SAME_ACCESS
);
884 /* some handles above may have been invalid; this is not an error */
885 if (get_error() == STATUS_INVALID_HANDLE
||
886 get_error() == STATUS_OBJECT_TYPE_MISMATCH
) clear_error();
889 /* attach to the debugger if requested */
890 if (req
->create_flags
& (DEBUG_PROCESS
| DEBUG_ONLY_THIS_PROCESS
))
891 set_process_debugger( process
, current
);
892 else if (parent
->debugger
&& !(parent
->create_flags
& DEBUG_ONLY_THIS_PROCESS
))
893 set_process_debugger( process
, parent
->debugger
);
895 if (!(req
->create_flags
& CREATE_NEW_PROCESS_GROUP
))
896 process
->group_id
= parent
->group_id
;
898 info
->process
= (struct process
*)grab_object( process
);
899 reply
->info
= alloc_handle( current
->process
, info
, SYNCHRONIZE
, 0 );
900 reply
->pid
= get_process_id( process
);
901 reply
->tid
= get_thread_id( thread
);
902 reply
->phandle
= alloc_handle( parent
, process
, req
->process_access
, req
->process_attr
);
903 reply
->thandle
= alloc_handle( parent
, thread
, req
->thread_access
, req
->thread_attr
);
906 release_object( info
);
909 /* Retrieve information about a newly started process */
910 DECL_HANDLER(get_new_process_info
)
912 struct startup_info
*info
;
914 if ((info
= (struct startup_info
*)get_handle_obj( current
->process
, req
->info
,
915 0, &startup_info_ops
)))
917 reply
->success
= is_process_init_done( info
->process
);
918 reply
->exit_code
= info
->process
->exit_code
;
919 release_object( info
);
923 /* Retrieve the new process startup info */
924 DECL_HANDLER(get_startup_info
)
926 struct process
*process
= current
->process
;
927 struct startup_info
*info
= process
->startup_info
;
932 if (info
->exe_file
&&
933 !(reply
->exe_file
= alloc_handle( process
, info
->exe_file
, GENERIC_READ
, 0 ))) return;
935 reply
->hstdin
= info
->hstdin
;
936 reply
->hstdout
= info
->hstdout
;
937 reply
->hstderr
= info
->hstderr
;
939 /* we return the data directly without making a copy so this can only be called once */
940 size
= info
->data_size
;
941 if (size
> get_reply_max_size()) size
= get_reply_max_size();
942 set_reply_data_ptr( info
->data
, size
);
947 /* signal the end of the process initialization */
948 DECL_HANDLER(init_process_done
)
950 struct process_dll
*dll
;
951 struct process
*process
= current
->process
;
953 if (is_process_init_done(process
))
955 set_error( STATUS_INVALID_PARAMETER
);
958 if (!(dll
= find_process_dll( process
, req
->module
)))
960 set_error( STATUS_DLL_NOT_FOUND
);
964 /* main exe is the first in the dll list */
965 list_remove( &dll
->entry
);
966 list_add_head( &process
->dlls
, &dll
->entry
);
968 generate_startup_debug_events( process
, req
->entry
);
969 set_process_startup_state( process
, STARTUP_DONE
);
971 if (req
->gui
) process
->idle_event
= create_event( NULL
, NULL
, 0, 1, 0 );
972 if (current
->suspend
+ process
->suspend
> 0) stop_thread( current
);
973 if (process
->debugger
) set_process_debug_flag( process
, 1 );
976 /* open a handle to a process */
977 DECL_HANDLER(open_process
)
979 struct process
*process
= get_process_from_id( req
->pid
);
983 reply
->handle
= alloc_handle( current
->process
, process
, req
->access
, req
->attributes
);
984 release_object( process
);
988 /* terminate a process */
989 DECL_HANDLER(terminate_process
)
991 struct process
*process
;
993 if ((process
= get_process_from_handle( req
->handle
, PROCESS_TERMINATE
)))
995 reply
->self
= (current
->process
== process
);
996 terminate_process( process
, current
, req
->exit_code
);
997 release_object( process
);
1001 /* fetch information about a process */
1002 DECL_HANDLER(get_process_info
)
1004 struct process
*process
;
1006 if ((process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_INFORMATION
)))
1008 reply
->pid
= get_process_id( process
);
1009 reply
->ppid
= process
->parent
? get_process_id( process
->parent
) : 0;
1010 reply
->exit_code
= process
->exit_code
;
1011 reply
->priority
= process
->priority
;
1012 reply
->affinity
= process
->affinity
;
1013 reply
->peb
= process
->peb
;
1014 reply
->start_time
.sec
= process
->start_time
.tv_sec
;
1015 reply
->start_time
.usec
= process
->start_time
.tv_usec
;
1016 reply
->end_time
.sec
= process
->end_time
.tv_sec
;
1017 reply
->end_time
.usec
= process
->end_time
.tv_usec
;
1018 release_object( process
);
1022 /* set information about a process */
1023 DECL_HANDLER(set_process_info
)
1025 struct process
*process
;
1027 if ((process
= get_process_from_handle( req
->handle
, PROCESS_SET_INFORMATION
)))
1029 if (req
->mask
& SET_PROCESS_INFO_PRIORITY
) process
->priority
= req
->priority
;
1030 if (req
->mask
& SET_PROCESS_INFO_AFFINITY
)
1032 if (req
->affinity
!= 1) set_error( STATUS_INVALID_PARAMETER
);
1033 else process
->affinity
= req
->affinity
;
1035 release_object( process
);
1039 /* read data from a process address space */
1040 DECL_HANDLER(read_process_memory
)
1042 struct process
*process
;
1043 data_size_t len
= get_reply_max_size();
1045 if (!(process
= get_process_from_handle( req
->handle
, PROCESS_VM_READ
))) return;
1049 char *buffer
= mem_alloc( len
);
1052 if (read_process_memory( process
, req
->addr
, len
, buffer
))
1053 set_reply_data_ptr( buffer
, len
);
1058 release_object( process
);
1061 /* write data to a process address space */
1062 DECL_HANDLER(write_process_memory
)
1064 struct process
*process
;
1066 if ((process
= get_process_from_handle( req
->handle
, PROCESS_VM_WRITE
)))
1068 data_size_t len
= get_req_data_size();
1069 if (len
) write_process_memory( process
, req
->addr
, len
, get_req_data() );
1070 else set_error( STATUS_INVALID_PARAMETER
);
1071 release_object( process
);
1075 /* notify the server that a dll has been loaded */
1076 DECL_HANDLER(load_dll
)
1078 struct process_dll
*dll
;
1079 struct file
*file
= NULL
;
1081 if (req
->handle
&& !(file
= get_file_obj( current
->process
, req
->handle
, FILE_READ_DATA
)))
1084 if ((dll
= process_load_dll( current
->process
, file
, req
->base
,
1085 get_req_data(), get_req_data_size() )))
1087 dll
->size
= req
->size
;
1088 dll
->dbg_offset
= req
->dbg_offset
;
1089 dll
->dbg_size
= req
->dbg_size
;
1090 dll
->name
= req
->name
;
1091 /* only generate event if initialization is done */
1092 if (is_process_init_done( current
->process
))
1093 generate_debug_event( current
, LOAD_DLL_DEBUG_EVENT
, dll
);
1095 if (file
) release_object( file
);
1098 /* notify the server that a dll is being unloaded */
1099 DECL_HANDLER(unload_dll
)
1101 process_unload_dll( current
->process
, req
->base
);
1104 /* retrieve information about a module in a process */
1105 DECL_HANDLER(get_dll_info
)
1107 struct process
*process
;
1109 if ((process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_INFORMATION
)))
1111 struct process_dll
*dll
= find_process_dll( process
, req
->base_address
);
1115 reply
->size
= dll
->size
;
1116 reply
->entry_point
= NULL
; /* FIXME */
1119 data_size_t len
= min( dll
->namelen
, get_reply_max_size() );
1120 set_reply_data( dll
->filename
, len
);
1124 set_error( STATUS_DLL_NOT_FOUND
);
1126 release_object( process
);
1130 /* retrieve the process idle event */
1131 DECL_HANDLER(get_process_idle_event
)
1133 struct process
*process
;
1136 if ((process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_INFORMATION
)))
1138 if (process
->idle_event
&& process
!= current
->process
)
1139 reply
->event
= alloc_handle( current
->process
, process
->idle_event
,
1140 EVENT_ALL_ACCESS
, 0 );
1141 release_object( process
);