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
;
57 /* process operations */
59 static void process_dump( struct object
*obj
, int verbose
);
60 static int process_signaled( struct object
*obj
, struct thread
*thread
);
61 static unsigned int process_map_access( struct object
*obj
, unsigned int access
);
62 static void process_poll_event( struct fd
*fd
, int event
);
63 static void process_destroy( struct object
*obj
);
65 static const struct object_ops process_ops
=
67 sizeof(struct process
), /* size */
68 process_dump
, /* dump */
69 add_queue
, /* add_queue */
70 remove_queue
, /* remove_queue */
71 process_signaled
, /* signaled */
72 no_satisfied
, /* satisfied */
73 no_signal
, /* signal */
74 no_get_fd
, /* get_fd */
75 process_map_access
, /* map_access */
76 no_lookup_name
, /* lookup_name */
77 no_open_file
, /* open_file */
78 no_close_handle
, /* close_handle */
79 process_destroy
/* destroy */
82 static const struct fd_ops process_fd_ops
=
84 NULL
, /* get_poll_events */
85 process_poll_event
, /* poll_event */
87 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
)
246 process
->sigkill_timeout
= add_timeout_user( -TICKS_PER_SEC
, process_sigkill
, process
);
248 process_died( process
);
251 /* create a new process and its main thread */
252 /* if the function fails the fd is closed */
253 struct thread
*create_process( int fd
, struct thread
*parent_thread
, int inherit_all
)
255 struct process
*process
;
256 struct thread
*thread
= NULL
;
259 if (!(process
= alloc_object( &process_ops
)))
264 process
->parent
= NULL
;
265 process
->debugger
= NULL
;
266 process
->handles
= NULL
;
267 process
->msg_fd
= NULL
;
268 process
->sigkill_timeout
= NULL
;
269 process
->unix_pid
= -1;
270 process
->exit_code
= STILL_ACTIVE
;
271 process
->running_threads
= 0;
272 process
->priority
= PROCESS_PRIOCLASS_NORMAL
;
273 process
->affinity
= 1;
274 process
->suspend
= 0;
275 process
->create_flags
= 0;
276 process
->console
= NULL
;
277 process
->startup_state
= STARTUP_IN_PROGRESS
;
278 process
->startup_info
= NULL
;
279 process
->idle_event
= NULL
;
280 process
->queue
= NULL
;
282 process
->ldt_copy
= NULL
;
283 process
->winstation
= 0;
284 process
->desktop
= 0;
285 process
->token
= token_create_admin();
286 process
->trace_data
= 0;
287 list_init( &process
->thread_list
);
288 list_init( &process
->locks
);
289 list_init( &process
->classes
);
290 list_init( &process
->dlls
);
292 process
->start_time
= current_time
;
293 process
->end_time
= 0;
294 list_add_head( &process_list
, &process
->entry
);
296 if (!(process
->id
= process
->group_id
= alloc_ptid( process
)))
301 if (!(process
->msg_fd
= create_anonymous_fd( &process_fd_ops
, fd
, &process
->obj
, 0 ))) goto error
;
303 /* create the handle table */
304 if (!parent_thread
) process
->handles
= alloc_handle_table( process
, 0 );
307 struct process
*parent
= parent_thread
->process
;
308 process
->parent
= (struct process
*)grab_object( parent
);
309 process
->handles
= inherit_all
? copy_handle_table( process
, parent
)
310 : alloc_handle_table( process
, 0 );
312 if (!process
->handles
) goto error
;
314 /* create the main thread */
315 if (pipe( request_pipe
) == -1)
320 if (send_client_fd( process
, request_pipe
[1], 0 ) == -1)
322 close( request_pipe
[0] );
323 close( request_pipe
[1] );
326 close( request_pipe
[1] );
327 if (!(thread
= create_thread( request_pipe
[0], process
))) goto error
;
329 set_fd_events( process
->msg_fd
, POLLIN
); /* start listening to events */
330 release_object( process
);
334 if (process
) release_object( process
);
335 /* if we failed to start our first process, close everything down */
336 if (!running_processes
) close_master_socket();
340 /* initialize the current process and fill in the request */
341 data_size_t
init_process( struct thread
*thread
)
343 struct process
*process
= thread
->process
;
344 struct startup_info
*info
= process
->startup_info
;
346 init_process_tracing( process
);
348 return info
->data_size
;
351 /* destroy a process when its refcount is 0 */
352 static void process_destroy( struct object
*obj
)
354 struct process
*process
= (struct process
*)obj
;
355 assert( obj
->ops
== &process_ops
);
357 /* we can't have a thread remaining */
358 assert( list_empty( &process
->thread_list
));
360 assert( !process
->sigkill_timeout
); /* timeout should hold a reference to the process */
362 set_process_startup_state( process
, STARTUP_ABORTED
);
363 if (process
->console
) release_object( process
->console
);
364 if (process
->parent
) release_object( process
->parent
);
365 if (process
->msg_fd
) release_object( process
->msg_fd
);
366 list_remove( &process
->entry
);
367 if (process
->idle_event
) release_object( process
->idle_event
);
368 if (process
->queue
) release_object( process
->queue
);
369 if (process
->id
) free_ptid( process
->id
);
370 if (process
->token
) release_object( process
->token
);
373 /* dump a process on stdout for debugging purposes */
374 static void process_dump( struct object
*obj
, int verbose
)
376 struct process
*process
= (struct process
*)obj
;
377 assert( obj
->ops
== &process_ops
);
379 fprintf( stderr
, "Process id=%04x handles=%p\n", process
->id
, process
->handles
);
382 static int process_signaled( struct object
*obj
, struct thread
*thread
)
384 struct process
*process
= (struct process
*)obj
;
385 return !process
->running_threads
;
388 static unsigned int process_map_access( struct object
*obj
, unsigned int access
)
390 if (access
& GENERIC_READ
) access
|= STANDARD_RIGHTS_READ
| SYNCHRONIZE
;
391 if (access
& GENERIC_WRITE
) access
|= STANDARD_RIGHTS_WRITE
| SYNCHRONIZE
;
392 if (access
& GENERIC_EXECUTE
) access
|= STANDARD_RIGHTS_EXECUTE
;
393 if (access
& GENERIC_ALL
) access
|= PROCESS_ALL_ACCESS
;
394 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
397 static void process_poll_event( struct fd
*fd
, int event
)
399 struct process
*process
= get_fd_user( fd
);
400 assert( process
->obj
.ops
== &process_ops
);
402 if (event
& (POLLERR
| POLLHUP
))
404 release_object( process
->msg_fd
);
405 process
->msg_fd
= NULL
;
406 if (process
->sigkill_timeout
) /* already waiting for it to die */
408 remove_timeout_user( process
->sigkill_timeout
);
409 process
->sigkill_timeout
= NULL
;
410 process_died( process
);
412 else kill_process( process
, 0 );
414 else if (event
& POLLIN
) receive_fd( process
);
417 static void startup_info_destroy( struct object
*obj
)
419 struct startup_info
*info
= (struct startup_info
*)obj
;
420 assert( obj
->ops
== &startup_info_ops
);
422 if (info
->exe_file
) release_object( info
->exe_file
);
423 if (info
->process
) release_object( info
->process
);
426 static void startup_info_dump( struct object
*obj
, int verbose
)
428 struct startup_info
*info
= (struct startup_info
*)obj
;
429 assert( obj
->ops
== &startup_info_ops
);
431 fprintf( stderr
, "Startup info in=%p out=%p err=%p\n",
432 info
->hstdin
, info
->hstdout
, info
->hstderr
);
435 static int startup_info_signaled( struct object
*obj
, struct thread
*thread
)
437 struct startup_info
*info
= (struct startup_info
*)obj
;
438 return info
->process
&& info
->process
->startup_state
!= STARTUP_IN_PROGRESS
;
441 /* get a process from an id (and increment the refcount) */
442 struct process
*get_process_from_id( process_id_t id
)
444 struct object
*obj
= get_ptid_entry( id
);
446 if (obj
&& obj
->ops
== &process_ops
) return (struct process
*)grab_object( obj
);
447 set_error( STATUS_INVALID_PARAMETER
);
451 /* get a process from a handle (and increment the refcount) */
452 struct process
*get_process_from_handle( obj_handle_t handle
, unsigned int access
)
454 return (struct process
*)get_handle_obj( current
->process
, handle
,
455 access
, &process_ops
);
458 /* find a dll from its base address */
459 static inline struct process_dll
*find_process_dll( struct process
*process
, void *base
)
461 struct process_dll
*dll
;
463 LIST_FOR_EACH_ENTRY( dll
, &process
->dlls
, struct process_dll
, entry
)
465 if (dll
->base
== base
) return dll
;
470 /* add a dll to a process list */
471 static struct process_dll
*process_load_dll( struct process
*process
, struct file
*file
,
472 void *base
, const WCHAR
*filename
, data_size_t name_len
)
474 struct process_dll
*dll
;
476 /* make sure we don't already have one with the same base address */
477 if (find_process_dll( process
, base
))
479 set_error( STATUS_INVALID_PARAMETER
);
483 if ((dll
= mem_alloc( sizeof(*dll
) )))
487 dll
->filename
= NULL
;
488 dll
->namelen
= name_len
;
489 if (name_len
&& !(dll
->filename
= memdup( filename
, name_len
)))
494 if (file
) dll
->file
= grab_file_unless_removable( file
);
495 list_add_tail( &process
->dlls
, &dll
->entry
);
500 /* remove a dll from a process list */
501 static void process_unload_dll( struct process
*process
, void *base
)
503 struct process_dll
*dll
= find_process_dll( process
, base
);
505 if (dll
&& (&dll
->entry
!= list_head( &process
->dlls
))) /* main exe can't be unloaded */
507 if (dll
->file
) release_object( dll
->file
);
508 free( dll
->filename
);
509 list_remove( &dll
->entry
);
511 generate_debug_event( current
, UNLOAD_DLL_DEBUG_EVENT
, base
);
513 else set_error( STATUS_INVALID_PARAMETER
);
516 /* terminate a process with the given exit code */
517 static void terminate_process( struct process
*process
, struct thread
*skip
, int exit_code
)
521 if (skip
&& skip
->process
== process
) /* move it to the end of the list */
523 assert( skip
->state
!= TERMINATED
);
524 list_remove( &skip
->proc_entry
);
525 list_add_tail( &process
->thread_list
, &skip
->proc_entry
);
528 grab_object( process
); /* make sure it doesn't get freed when threads die */
529 while ((ptr
= list_head( &process
->thread_list
)))
531 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
533 if (exit_code
) thread
->exit_code
= exit_code
;
534 if (thread
== skip
) break;
535 kill_thread( thread
, 1 );
537 release_object( process
);
540 /* kill all processes */
541 void kill_all_processes( struct process
*skip
, int exit_code
)
545 struct process
*process
;
547 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
549 if (process
== skip
) continue;
550 if (process
->running_threads
) break;
552 if (&process
->entry
== &process_list
) break; /* no process found */
553 terminate_process( process
, NULL
, exit_code
);
557 /* kill all processes being attached to a console renderer */
558 void kill_console_processes( struct thread
*renderer
, int exit_code
)
560 for (;;) /* restart from the beginning of the list every time */
562 struct process
*process
;
564 /* find the first process being attached to 'renderer' and still running */
565 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
567 if (process
== renderer
->process
) continue;
568 if (!process
->running_threads
) continue;
569 if (process
->console
&& console_get_renderer( process
->console
) == renderer
) break;
571 if (&process
->entry
== &process_list
) break; /* no process found */
572 terminate_process( process
, NULL
, exit_code
);
576 /* a process has been killed (i.e. its last thread died) */
577 static void process_killed( struct process
*process
)
579 struct handle_table
*handles
;
582 assert( list_empty( &process
->thread_list
));
583 process
->end_time
= current_time
;
584 close_process_desktop( process
);
585 handles
= process
->handles
;
586 process
->handles
= NULL
;
587 if (handles
) release_object( handles
);
589 /* close the console attached to this process, if any */
590 free_console( process
);
592 while ((ptr
= list_head( &process
->dlls
)))
594 struct process_dll
*dll
= LIST_ENTRY( ptr
, struct process_dll
, entry
);
595 if (dll
->file
) release_object( dll
->file
);
596 free( dll
->filename
);
597 list_remove( &dll
->entry
);
600 destroy_process_classes( process
);
601 remove_process_locks( process
);
602 set_process_startup_state( process
, STARTUP_ABORTED
);
603 finish_process_tracing( process
);
604 start_sigkill_timer( process
);
605 wake_up( &process
->obj
, 0 );
608 /* add a thread to a process running threads list */
609 void add_process_thread( struct process
*process
, struct thread
*thread
)
611 list_add_tail( &process
->thread_list
, &thread
->proc_entry
);
612 if (!process
->running_threads
++) running_processes
++;
613 grab_object( thread
);
616 /* remove a thread from a process running threads list */
617 void remove_process_thread( struct process
*process
, struct thread
*thread
)
619 assert( process
->running_threads
> 0 );
620 assert( !list_empty( &process
->thread_list
));
622 list_remove( &thread
->proc_entry
);
624 if (!--process
->running_threads
)
626 /* we have removed the last running thread, exit the process */
627 process
->exit_code
= thread
->exit_code
;
628 generate_debug_event( thread
, EXIT_PROCESS_DEBUG_EVENT
, process
);
629 process_killed( process
);
631 else generate_debug_event( thread
, EXIT_THREAD_DEBUG_EVENT
, thread
);
632 release_object( thread
);
635 /* suspend all the threads of a process */
636 void suspend_process( struct process
*process
)
638 if (!process
->suspend
++)
640 struct list
*ptr
, *next
;
642 LIST_FOR_EACH_SAFE( ptr
, next
, &process
->thread_list
)
644 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
645 if (!thread
->suspend
) stop_thread( thread
);
650 /* resume all the threads of a process */
651 void resume_process( struct process
*process
)
653 assert (process
->suspend
> 0);
654 if (!--process
->suspend
)
656 struct list
*ptr
, *next
;
658 LIST_FOR_EACH_SAFE( ptr
, next
, &process
->thread_list
)
660 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
661 if (!thread
->suspend
) wake_thread( thread
);
666 /* kill a process on the spot */
667 void kill_process( struct process
*process
, int violent_death
)
669 if (violent_death
) terminate_process( process
, NULL
, 1 );
674 grab_object( process
); /* make sure it doesn't get freed when threads die */
675 while ((ptr
= list_head( &process
->thread_list
)))
677 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
678 kill_thread( thread
, 0 );
680 release_object( process
);
684 /* kill all processes being debugged by a given thread */
685 void kill_debugged_processes( struct thread
*debugger
, int exit_code
)
687 for (;;) /* restart from the beginning of the list every time */
689 struct process
*process
;
691 /* find the first process being debugged by 'debugger' and still running */
692 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
694 if (!process
->running_threads
) continue;
695 if (process
->debugger
== debugger
) break;
697 if (&process
->entry
== &process_list
) break; /* no process found */
698 process
->debugger
= NULL
;
699 terminate_process( process
, NULL
, exit_code
);
704 /* trigger a breakpoint event in a given process */
705 void break_process( struct process
*process
)
707 struct thread
*thread
;
709 suspend_process( process
);
711 LIST_FOR_EACH_ENTRY( thread
, &process
->thread_list
, struct thread
, proc_entry
)
713 if (thread
->context
) /* inside an exception event already */
715 break_thread( thread
);
719 if ((thread
= get_process_first_thread( process
))) thread
->debug_break
= 1;
720 else set_error( STATUS_ACCESS_DENIED
);
722 resume_process( process
);
726 /* detach a debugger from all its debuggees */
727 void detach_debugged_processes( struct thread
*debugger
)
729 struct process
*process
;
731 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
733 if (process
->debugger
== debugger
&& process
->running_threads
)
735 debugger_detach( process
, debugger
);
741 void enum_processes( int (*cb
)(struct process
*, void*), void *user
)
743 struct list
*ptr
, *next
;
745 LIST_FOR_EACH_SAFE( ptr
, next
, &process_list
)
747 struct process
*process
= LIST_ENTRY( ptr
, struct process
, entry
);
748 if ((cb
)(process
, user
)) break;
752 /* set the debugged flag in the process PEB */
753 int set_process_debug_flag( struct process
*process
, int flag
)
755 char data
= (flag
!= 0);
757 /* BeingDebugged flag is the byte at offset 2 in the PEB */
758 return write_process_memory( process
, (char *)process
->peb
+ 2, 1, &data
);
761 /* take a snapshot of currently running processes */
762 struct process_snapshot
*process_snap( int *count
)
764 struct process_snapshot
*snapshot
, *ptr
;
765 struct process
*process
;
767 if (!running_processes
) return NULL
;
768 if (!(snapshot
= mem_alloc( sizeof(*snapshot
) * running_processes
)))
771 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
773 if (!process
->running_threads
) continue;
774 ptr
->process
= process
;
775 ptr
->threads
= process
->running_threads
;
776 ptr
->count
= process
->obj
.refcount
;
777 ptr
->priority
= process
->priority
;
778 ptr
->handles
= get_handle_table_count(process
);
779 grab_object( process
);
783 if (!(*count
= ptr
- snapshot
))
791 /* take a snapshot of the modules of a process */
792 struct module_snapshot
*module_snap( struct process
*process
, int *count
)
794 struct module_snapshot
*snapshot
, *ptr
;
795 struct process_dll
*dll
;
798 LIST_FOR_EACH_ENTRY( dll
, &process
->dlls
, struct process_dll
, entry
) total
++;
799 if (!(snapshot
= mem_alloc( sizeof(*snapshot
) * total
))) return NULL
;
802 LIST_FOR_EACH_ENTRY( dll
, &process
->dlls
, struct process_dll
, entry
)
804 ptr
->base
= dll
->base
;
805 ptr
->size
= dll
->size
;
806 ptr
->namelen
= dll
->namelen
;
807 ptr
->filename
= memdup( dll
->filename
, dll
->namelen
);
815 /* create a new process */
816 DECL_HANDLER(new_process
)
818 struct startup_info
*info
;
819 struct thread
*thread
;
820 struct process
*process
;
821 struct process
*parent
= current
->process
;
822 int socket_fd
= thread_get_inflight_fd( current
, req
->socket_fd
);
826 set_error( STATUS_INVALID_PARAMETER
);
829 if (fcntl( socket_fd
, F_SETFL
, O_NONBLOCK
) == -1)
831 set_error( STATUS_INVALID_HANDLE
);
836 /* build the startup info for a new process */
837 if (!(info
= alloc_object( &startup_info_ops
))) return;
838 info
->hstdin
= req
->hstdin
;
839 info
->hstdout
= req
->hstdout
;
840 info
->hstderr
= req
->hstderr
;
841 info
->exe_file
= NULL
;
842 info
->process
= NULL
;
843 info
->data_size
= get_req_data_size();
847 !(info
->exe_file
= get_file_obj( current
->process
, req
->exe_file
, FILE_READ_DATA
)))
850 if (!(info
->data
= memdup( get_req_data(), info
->data_size
))) goto done
;
852 if (!(thread
= create_process( socket_fd
, current
, req
->inherit_all
))) goto done
;
853 process
= thread
->process
;
854 process
->create_flags
= req
->create_flags
;
855 process
->startup_info
= (struct startup_info
*)grab_object( info
);
857 /* connect to the window station */
858 connect_process_winstation( process
, current
);
860 /* thread will be actually suspended in init_done */
861 if (req
->create_flags
& CREATE_SUSPENDED
) thread
->suspend
++;
863 /* set the process console */
864 if (!(req
->create_flags
& (DETACHED_PROCESS
| CREATE_NEW_CONSOLE
)))
866 /* FIXME: some better error checking should be done...
867 * like if hConOut and hConIn are console handles, then they should be on the same
870 inherit_console( current
, process
, req
->inherit_all
? req
->hstdin
: 0 );
873 if (!req
->inherit_all
&& !(req
->create_flags
& CREATE_NEW_CONSOLE
))
875 info
->hstdin
= duplicate_handle( parent
, req
->hstdin
, process
,
876 0, OBJ_INHERIT
, DUPLICATE_SAME_ACCESS
);
877 info
->hstdout
= duplicate_handle( parent
, req
->hstdout
, process
,
878 0, OBJ_INHERIT
, DUPLICATE_SAME_ACCESS
);
879 info
->hstderr
= duplicate_handle( parent
, req
->hstderr
, process
,
880 0, OBJ_INHERIT
, DUPLICATE_SAME_ACCESS
);
881 /* some handles above may have been invalid; this is not an error */
882 if (get_error() == STATUS_INVALID_HANDLE
||
883 get_error() == STATUS_OBJECT_TYPE_MISMATCH
) clear_error();
886 /* attach to the debugger if requested */
887 if (req
->create_flags
& (DEBUG_PROCESS
| DEBUG_ONLY_THIS_PROCESS
))
888 set_process_debugger( process
, current
);
889 else if (parent
->debugger
&& !(parent
->create_flags
& DEBUG_ONLY_THIS_PROCESS
))
890 set_process_debugger( process
, parent
->debugger
);
892 if (!(req
->create_flags
& CREATE_NEW_PROCESS_GROUP
))
893 process
->group_id
= parent
->group_id
;
895 info
->process
= (struct process
*)grab_object( process
);
896 reply
->info
= alloc_handle( current
->process
, info
, SYNCHRONIZE
, 0 );
897 reply
->pid
= get_process_id( process
);
898 reply
->tid
= get_thread_id( thread
);
899 reply
->phandle
= alloc_handle( parent
, process
, req
->process_access
, req
->process_attr
);
900 reply
->thandle
= alloc_handle( parent
, thread
, req
->thread_access
, req
->thread_attr
);
903 release_object( info
);
906 /* Retrieve information about a newly started process */
907 DECL_HANDLER(get_new_process_info
)
909 struct startup_info
*info
;
911 if ((info
= (struct startup_info
*)get_handle_obj( current
->process
, req
->info
,
912 0, &startup_info_ops
)))
914 reply
->success
= is_process_init_done( info
->process
);
915 reply
->exit_code
= info
->process
->exit_code
;
916 release_object( info
);
920 /* Retrieve the new process startup info */
921 DECL_HANDLER(get_startup_info
)
923 struct process
*process
= current
->process
;
924 struct startup_info
*info
= process
->startup_info
;
929 if (info
->exe_file
&&
930 !(reply
->exe_file
= alloc_handle( process
, info
->exe_file
, GENERIC_READ
, 0 ))) return;
932 reply
->hstdin
= info
->hstdin
;
933 reply
->hstdout
= info
->hstdout
;
934 reply
->hstderr
= info
->hstderr
;
936 /* we return the data directly without making a copy so this can only be called once */
937 size
= info
->data_size
;
938 if (size
> get_reply_max_size()) size
= get_reply_max_size();
939 set_reply_data_ptr( info
->data
, size
);
944 /* signal the end of the process initialization */
945 DECL_HANDLER(init_process_done
)
947 struct process_dll
*dll
;
948 struct process
*process
= current
->process
;
950 if (is_process_init_done(process
))
952 set_error( STATUS_INVALID_PARAMETER
);
955 if (!(dll
= find_process_dll( process
, req
->module
)))
957 set_error( STATUS_DLL_NOT_FOUND
);
961 /* main exe is the first in the dll list */
962 list_remove( &dll
->entry
);
963 list_add_head( &process
->dlls
, &dll
->entry
);
965 generate_startup_debug_events( process
, req
->entry
);
966 set_process_startup_state( process
, STARTUP_DONE
);
968 if (req
->gui
) process
->idle_event
= create_event( NULL
, NULL
, 0, 1, 0 );
969 if (current
->suspend
+ process
->suspend
> 0) stop_thread( current
);
970 if (process
->debugger
) set_process_debug_flag( process
, 1 );
973 /* open a handle to a process */
974 DECL_HANDLER(open_process
)
976 struct process
*process
= get_process_from_id( req
->pid
);
980 reply
->handle
= alloc_handle( current
->process
, process
, req
->access
, req
->attributes
);
981 release_object( process
);
985 /* terminate a process */
986 DECL_HANDLER(terminate_process
)
988 struct process
*process
;
990 if ((process
= get_process_from_handle( req
->handle
, PROCESS_TERMINATE
)))
992 reply
->self
= (current
->process
== process
);
993 terminate_process( process
, current
, req
->exit_code
);
994 release_object( process
);
998 /* fetch information about a process */
999 DECL_HANDLER(get_process_info
)
1001 struct process
*process
;
1003 if ((process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_INFORMATION
)))
1005 reply
->pid
= get_process_id( process
);
1006 reply
->ppid
= process
->parent
? get_process_id( process
->parent
) : 0;
1007 reply
->exit_code
= process
->exit_code
;
1008 reply
->priority
= process
->priority
;
1009 reply
->affinity
= process
->affinity
;
1010 reply
->peb
= process
->peb
;
1011 reply
->start_time
= process
->start_time
;
1012 reply
->end_time
= process
->end_time
;
1013 release_object( process
);
1017 /* set information about a process */
1018 DECL_HANDLER(set_process_info
)
1020 struct process
*process
;
1022 if ((process
= get_process_from_handle( req
->handle
, PROCESS_SET_INFORMATION
)))
1024 if (req
->mask
& SET_PROCESS_INFO_PRIORITY
) process
->priority
= req
->priority
;
1025 if (req
->mask
& SET_PROCESS_INFO_AFFINITY
)
1027 if (req
->affinity
!= 1) set_error( STATUS_INVALID_PARAMETER
);
1028 else process
->affinity
= req
->affinity
;
1030 release_object( process
);
1034 /* read data from a process address space */
1035 DECL_HANDLER(read_process_memory
)
1037 struct process
*process
;
1038 data_size_t len
= get_reply_max_size();
1040 if (!(process
= get_process_from_handle( req
->handle
, PROCESS_VM_READ
))) return;
1044 char *buffer
= mem_alloc( len
);
1047 if (read_process_memory( process
, req
->addr
, len
, buffer
))
1048 set_reply_data_ptr( buffer
, len
);
1053 release_object( process
);
1056 /* write data to a process address space */
1057 DECL_HANDLER(write_process_memory
)
1059 struct process
*process
;
1061 if ((process
= get_process_from_handle( req
->handle
, PROCESS_VM_WRITE
)))
1063 data_size_t len
= get_req_data_size();
1064 if (len
) write_process_memory( process
, req
->addr
, len
, get_req_data() );
1065 else set_error( STATUS_INVALID_PARAMETER
);
1066 release_object( process
);
1070 /* notify the server that a dll has been loaded */
1071 DECL_HANDLER(load_dll
)
1073 struct process_dll
*dll
;
1074 struct file
*file
= NULL
;
1076 if (req
->handle
&& !(file
= get_file_obj( current
->process
, req
->handle
, FILE_READ_DATA
)))
1079 if ((dll
= process_load_dll( current
->process
, file
, req
->base
,
1080 get_req_data(), get_req_data_size() )))
1082 dll
->size
= req
->size
;
1083 dll
->dbg_offset
= req
->dbg_offset
;
1084 dll
->dbg_size
= req
->dbg_size
;
1085 dll
->name
= req
->name
;
1086 /* only generate event if initialization is done */
1087 if (is_process_init_done( current
->process
))
1088 generate_debug_event( current
, LOAD_DLL_DEBUG_EVENT
, dll
);
1090 if (file
) release_object( file
);
1093 /* notify the server that a dll is being unloaded */
1094 DECL_HANDLER(unload_dll
)
1096 process_unload_dll( current
->process
, req
->base
);
1099 /* retrieve information about a module in a process */
1100 DECL_HANDLER(get_dll_info
)
1102 struct process
*process
;
1104 if ((process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_INFORMATION
)))
1106 struct process_dll
*dll
= find_process_dll( process
, req
->base_address
);
1110 reply
->size
= dll
->size
;
1111 reply
->entry_point
= NULL
; /* FIXME */
1114 data_size_t len
= min( dll
->namelen
, get_reply_max_size() );
1115 set_reply_data( dll
->filename
, len
);
1119 set_error( STATUS_DLL_NOT_FOUND
);
1121 release_object( process
);
1125 /* retrieve the process idle event */
1126 DECL_HANDLER(get_process_idle_event
)
1128 struct process
*process
;
1131 if ((process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_INFORMATION
)))
1133 if (process
->idle_event
&& process
!= current
->process
)
1134 reply
->event
= alloc_handle( current
->process
, process
->idle_event
,
1135 EVENT_ALL_ACCESS
, 0 );
1136 release_object( process
);