2 * Server-side process management
4 * Copyright (C) 1998 Alexandre Julliard
17 #ifdef HAVE_SYS_SOCKET_H
18 # include <sys/socket.h>
30 /* process structure */
32 static struct process
*first_process
;
33 static int running_processes
;
35 /* process operations */
37 static void process_dump( struct object
*obj
, int verbose
);
38 static int process_signaled( struct object
*obj
, struct thread
*thread
);
39 static void process_destroy( struct object
*obj
);
41 static const struct object_ops process_ops
=
43 sizeof(struct process
), /* size */
44 process_dump
, /* dump */
45 add_queue
, /* add_queue */
46 remove_queue
, /* remove_queue */
47 process_signaled
, /* signaled */
48 no_satisfied
, /* satisfied */
49 NULL
, /* get_poll_events */
50 NULL
, /* poll_event */
51 no_read_fd
, /* get_read_fd */
52 no_write_fd
, /* get_write_fd */
54 no_get_file_info
, /* get_file_info */
55 process_destroy
/* destroy */
58 /* set the process creation info */
59 static int set_creation_info( struct process
*process
, struct new_process_request
*req
,
60 const char *cmd_line
, size_t len
)
62 if (!(process
->info
= mem_alloc( sizeof(*process
->info
) + len
))) return 0;
65 /* copy the request structure */
66 memcpy( process
->info
, req
, sizeof(*req
) );
68 else /* no request, use defaults */
73 req
->create_flags
= CREATE_NEW_CONSOLE
;
74 req
->start_flags
= STARTF_USESTDHANDLES
;
82 memcpy( process
->info
->cmdline
, cmd_line
, len
);
83 process
->info
->cmdline
[len
] = 0;
84 process
->create_flags
= process
->info
->create_flags
;
88 /* set the console and stdio handles for a newly created process */
89 static int set_process_console( struct process
*process
, struct process
*parent
)
91 struct new_process_request
*info
= process
->info
;
93 if (process
->create_flags
& CREATE_NEW_CONSOLE
)
95 if (!alloc_console( process
)) return 0;
97 else if (!(process
->create_flags
& DETACHED_PROCESS
))
99 if (parent
->console_in
) process
->console_in
= grab_object( parent
->console_in
);
100 if (parent
->console_out
) process
->console_out
= grab_object( parent
->console_out
);
104 if (!info
->inherit_all
&& !(info
->start_flags
& STARTF_USESTDHANDLES
))
106 /* duplicate the handle from the parent into this process */
107 info
->hstdin
= duplicate_handle( parent
, info
->hstdin
, process
,
108 0, TRUE
, DUPLICATE_SAME_ACCESS
);
109 info
->hstdout
= duplicate_handle( parent
, info
->hstdout
, process
,
110 0, TRUE
, DUPLICATE_SAME_ACCESS
);
111 info
->hstderr
= duplicate_handle( parent
, info
->hstderr
, process
,
112 0, TRUE
, DUPLICATE_SAME_ACCESS
);
117 /* no parent, use handles to the console for stdio */
118 info
->hstdin
= alloc_handle( process
, process
->console_in
,
119 GENERIC_READ
| GENERIC_WRITE
| SYNCHRONIZE
, 1 );
120 info
->hstdout
= alloc_handle( process
, process
->console_out
,
121 GENERIC_READ
| GENERIC_WRITE
| SYNCHRONIZE
, 1 );
122 info
->hstderr
= alloc_handle( process
, process
->console_out
,
123 GENERIC_READ
| GENERIC_WRITE
| SYNCHRONIZE
, 1 );
128 /* create a new process and its main thread */
129 struct thread
*create_process( int fd
, struct process
*parent
,
130 struct new_process_request
*req
,
131 const char *cmd_line
, size_t len
)
133 struct process
*process
;
134 struct thread
*thread
= NULL
;
136 if (!(process
= alloc_object( &process_ops
, -1 )))
141 process
->next
= NULL
;
142 process
->prev
= NULL
;
143 process
->thread_list
= NULL
;
144 process
->debugger
= NULL
;
145 process
->handles
= NULL
;
146 process
->exit_code
= 0x103; /* STILL_ACTIVE */
147 process
->running_threads
= 0;
148 process
->priority
= NORMAL_PRIORITY_CLASS
;
149 process
->affinity
= 1;
150 process
->suspend
= 0;
151 process
->create_flags
= 0;
152 process
->console_in
= NULL
;
153 process
->console_out
= NULL
;
154 process
->init_event
= NULL
;
155 process
->info
= NULL
;
156 process
->ldt_copy
= NULL
;
157 process
->ldt_flags
= NULL
;
158 gettimeofday( &process
->start_time
, NULL
);
159 if ((process
->next
= first_process
) != NULL
) process
->next
->prev
= process
;
160 first_process
= process
;
162 /* copy the request structure */
163 if (!set_creation_info( process
, req
, cmd_line
, len
)) goto error
;
165 if (process
->info
->inherit_all
)
166 process
->handles
= copy_handle_table( process
, parent
);
168 process
->handles
= alloc_handle_table( process
, 0 );
169 if (!process
->handles
) goto error
;
171 /* alloc a handle for the process itself */
172 alloc_handle( process
, process
, PROCESS_ALL_ACCESS
, 0 );
174 /* get the init done event */
175 if (process
->info
->event
!= -1)
177 if (!(process
->init_event
= get_event_obj( parent
, process
->info
->event
,
178 EVENT_MODIFY_STATE
))) goto error
;
181 /* set the process console */
182 if (!set_process_console( process
, parent
)) goto error
;
184 /* create the main thread */
185 if (!(thread
= create_thread( fd
, process
, (process
->create_flags
& CREATE_SUSPENDED
) != 0)))
188 /* attach to the debugger if requested */
189 if (process
->create_flags
& (DEBUG_PROCESS
| DEBUG_ONLY_THIS_PROCESS
))
190 debugger_attach( process
, current
);
191 else if (parent
&& parent
->debugger
&& !(parent
->create_flags
& DEBUG_ONLY_THIS_PROCESS
))
192 debugger_attach( process
, parent
->debugger
);
194 release_object( process
);
199 free_console( process
);
200 if (process
->handles
) release_object( process
->handles
);
201 release_object( process
);
205 /* destroy a process when its refcount is 0 */
206 static void process_destroy( struct object
*obj
)
208 struct process
*process
= (struct process
*)obj
;
209 assert( obj
->ops
== &process_ops
);
211 /* we can't have a thread remaining */
212 assert( !process
->thread_list
);
213 if (process
->next
) process
->next
->prev
= process
->prev
;
214 if (process
->prev
) process
->prev
->next
= process
->next
;
215 else first_process
= process
->next
;
216 if (process
->info
) free( process
->info
);
217 if (process
->init_event
) release_object( process
->init_event
);
220 /* dump a process on stdout for debugging purposes */
221 static void process_dump( struct object
*obj
, int verbose
)
223 struct process
*process
= (struct process
*)obj
;
224 assert( obj
->ops
== &process_ops
);
226 fprintf( stderr
, "Process next=%p prev=%p console=%p/%p handles=%p\n",
227 process
->next
, process
->prev
, process
->console_in
, process
->console_out
,
231 static int process_signaled( struct object
*obj
, struct thread
*thread
)
233 struct process
*process
= (struct process
*)obj
;
234 return !process
->running_threads
;
238 /* get a process from an id (and increment the refcount) */
239 struct process
*get_process_from_id( void *id
)
241 struct process
*p
= first_process
;
242 while (p
&& (p
!= id
)) p
= p
->next
;
243 if (p
) grab_object( p
);
244 else set_error( STATUS_INVALID_PARAMETER
);
248 /* get a process from a handle (and increment the refcount) */
249 struct process
*get_process_from_handle( int handle
, unsigned int access
)
251 return (struct process
*)get_handle_obj( current
->process
, handle
,
252 access
, &process_ops
);
255 /* a process has been killed (i.e. its last thread died) */
256 static void process_killed( struct process
*process
, int exit_code
)
258 assert( !process
->thread_list
);
259 process
->exit_code
= exit_code
;
260 gettimeofday( &process
->end_time
, NULL
);
261 release_object( process
->handles
);
262 process
->handles
= NULL
;
263 free_console( process
);
264 wake_up( &process
->obj
, 0 );
265 if (!--running_processes
)
267 /* last process died, close global handles */
268 close_global_handles();
269 /* this will cause the select loop to terminate */
270 if (!persistent_server
) close_master_socket();
274 /* add a thread to a process running threads list */
275 void add_process_thread( struct process
*process
, struct thread
*thread
)
277 thread
->proc_next
= process
->thread_list
;
278 thread
->proc_prev
= NULL
;
279 if (thread
->proc_next
) thread
->proc_next
->proc_prev
= thread
;
280 process
->thread_list
= thread
;
281 if (!process
->running_threads
++) running_processes
++;
282 grab_object( thread
);
285 /* remove a thread from a process running threads list */
286 void remove_process_thread( struct process
*process
, struct thread
*thread
)
288 assert( process
->running_threads
> 0 );
289 assert( process
->thread_list
);
291 if (thread
->proc_next
) thread
->proc_next
->proc_prev
= thread
->proc_prev
;
292 if (thread
->proc_prev
) thread
->proc_prev
->proc_next
= thread
->proc_next
;
293 else process
->thread_list
= thread
->proc_next
;
295 if (!--process
->running_threads
)
297 /* we have removed the last running thread, exit the process */
298 process_killed( process
, thread
->exit_code
);
300 release_object( thread
);
303 /* suspend all the threads of a process */
304 void suspend_process( struct process
*process
)
306 if (!process
->suspend
++)
308 struct thread
*thread
= process
->thread_list
;
309 for (; thread
; thread
= thread
->proc_next
)
311 if (!thread
->suspend
) stop_thread( thread
);
316 /* resume all the threads of a process */
317 void resume_process( struct process
*process
)
319 assert (process
->suspend
> 0);
320 if (!--process
->suspend
)
322 struct thread
*thread
= process
->thread_list
;
323 for (; thread
; thread
= thread
->proc_next
)
325 if (!thread
->suspend
) continue_thread( thread
);
330 /* kill a process on the spot */
331 void kill_process( struct process
*process
, int exit_code
)
333 while (process
->thread_list
)
334 kill_thread( process
->thread_list
, exit_code
);
337 /* kill all processes being debugged by a given thread */
338 void kill_debugged_processes( struct thread
*debugger
, int exit_code
)
340 for (;;) /* restart from the beginning of the list every time */
342 struct process
*process
= first_process
;
343 /* find the first process being debugged by 'debugger' and still running */
344 while (process
&& (process
->debugger
!= debugger
|| !process
->running_threads
))
345 process
= process
->next
;
346 if (!process
) return;
347 process
->debugger
= NULL
;
348 kill_process( process
, exit_code
);
352 /* get all information about a process */
353 static void get_process_info( struct process
*process
, struct get_process_info_request
*req
)
356 req
->exit_code
= process
->exit_code
;
357 req
->priority
= process
->priority
;
358 req
->process_affinity
= process
->affinity
;
359 req
->system_affinity
= 1;
362 /* set all information about a process */
363 static void set_process_info( struct process
*process
,
364 struct set_process_info_request
*req
)
366 if (req
->mask
& SET_PROCESS_INFO_PRIORITY
)
367 process
->priority
= req
->priority
;
368 if (req
->mask
& SET_PROCESS_INFO_AFFINITY
)
370 if (req
->affinity
!= 1) set_error( STATUS_INVALID_PARAMETER
);
371 else process
->affinity
= req
->affinity
;
375 /* read data from a process memory space */
376 /* len is the total size (in ints), max is the size we can actually store in the output buffer */
377 /* we read the total size in all cases to check for permissions */
378 static void read_process_memory( struct process
*process
, const int *addr
,
379 size_t len
, size_t max
, int *dest
)
381 struct thread
*thread
= process
->thread_list
;
383 if ((unsigned int)addr
% sizeof(int)) /* address must be aligned */
385 set_error( STATUS_INVALID_PARAMETER
);
388 suspend_thread( thread
, 0 );
389 if (thread
->attached
)
391 while (len
> 0 && max
)
393 if (read_thread_int( thread
, addr
++, dest
++ ) == -1) goto done
;
397 /* check the rest for read permission */
400 int dummy
, page
= get_page_size() / sizeof(int);
405 if (read_thread_int( thread
, addr
- 1, &dummy
) == -1) goto done
;
407 if (len
&& (read_thread_int( thread
, addr
+ len
- 1, &dummy
) == -1)) goto done
;
410 else set_error( STATUS_ACCESS_DENIED
);
412 resume_thread( thread
);
415 /* write data to a process memory space */
416 /* len is the total size (in ints), max is the size we can actually read from the input buffer */
417 /* we check the total size for write permissions */
418 static void write_process_memory( struct process
*process
, int *addr
, size_t len
,
419 size_t max
, unsigned int first_mask
,
420 unsigned int last_mask
, const int *src
)
422 struct thread
*thread
= process
->thread_list
;
424 if (!len
|| ((unsigned int)addr
% sizeof(int))) /* address must be aligned */
426 set_error( STATUS_INVALID_PARAMETER
);
429 suspend_thread( thread
, 0 );
430 if (thread
->attached
)
432 /* first word is special */
435 if (write_thread_int( thread
, addr
++, *src
++, first_mask
) == -1) goto done
;
439 else last_mask
&= first_mask
;
441 while (len
> 1 && max
)
443 if (write_thread_int( thread
, addr
++, *src
++, ~0 ) == -1) goto done
;
450 /* last word is special too */
451 if (write_thread_int( thread
, addr
, *src
, last_mask
) == -1) goto done
;
455 /* check the rest for write permission */
456 int page
= get_page_size() / sizeof(int);
461 if (write_thread_int( thread
, addr
- 1, 0, 0 ) == -1) goto done
;
463 if (len
&& (write_thread_int( thread
, addr
+ len
- 1, 0, 0 ) == -1)) goto done
;
466 else set_error( STATUS_ACCESS_DENIED
);
468 resume_thread( thread
);
471 /* take a snapshot of currently running processes */
472 struct process_snapshot
*process_snap( int *count
)
474 struct process_snapshot
*snapshot
, *ptr
;
475 struct process
*process
;
476 if (!running_processes
) return NULL
;
477 if (!(snapshot
= mem_alloc( sizeof(*snapshot
) * running_processes
)))
480 for (process
= first_process
; process
; process
= process
->next
)
482 if (!process
->running_threads
) continue;
483 ptr
->process
= process
;
484 ptr
->threads
= process
->running_threads
;
485 ptr
->priority
= process
->priority
;
486 grab_object( process
);
489 *count
= running_processes
;
493 /* create a new process */
494 DECL_HANDLER(new_process
)
496 size_t len
= get_req_strlen( req
->cmdline
);
497 struct thread
*thread
;
505 if (socketpair( AF_UNIX
, SOCK_STREAM
, 0, sock
) == -1)
511 if ((thread
= create_process( sock
[0], current
->process
, req
, req
->cmdline
, len
)))
513 int phandle
= alloc_handle( current
->process
, thread
->process
,
514 PROCESS_ALL_ACCESS
, req
->inherit
);
515 if ((req
->phandle
= phandle
) != -1)
517 if ((req
->thandle
= alloc_handle( current
->process
, thread
,
518 THREAD_ALL_ACCESS
, req
->inherit
)) != -1)
520 /* thread object will be released when the thread gets killed */
521 set_reply_fd( current
, sock
[1] );
522 req
->pid
= thread
->process
;
526 close_handle( current
->process
, phandle
);
528 release_object( thread
);
533 /* initialize a new process */
534 DECL_HANDLER(init_process
)
536 struct new_process_request
*info
;
538 if (!current
->unix_pid
)
540 fatal_protocol_error( current
, "init_process: init_thread not called yet\n" );
543 if (!(info
= current
->process
->info
))
545 fatal_protocol_error( current
, "init_process: called twice\n" );
548 current
->process
->ldt_copy
= req
->ldt_copy
;
549 current
->process
->ldt_flags
= req
->ldt_flags
;
550 current
->process
->info
= NULL
;
551 req
->start_flags
= info
->start_flags
;
552 req
->hstdin
= info
->hstdin
;
553 req
->hstdout
= info
->hstdout
;
554 req
->hstderr
= info
->hstderr
;
555 req
->cmd_show
= info
->cmd_show
;
556 req
->env_ptr
= info
->env_ptr
;
557 strcpy( req
->cmdline
, info
->cmdline
);
561 /* signal the end of the process initialization */
562 DECL_HANDLER(init_process_done
)
564 struct process
*process
= current
->process
;
565 if (!process
->init_event
)
567 fatal_protocol_error( current
, "init_process_done: no event\n" );
570 set_event( process
->init_event
);
571 release_object( process
->init_event
);
572 process
->init_event
= NULL
;
573 if (current
->suspend
+ current
->process
->suspend
> 0) stop_thread( current
);
576 /* open a handle to a process */
577 DECL_HANDLER(open_process
)
579 struct process
*process
= get_process_from_id( req
->pid
);
583 req
->handle
= alloc_handle( current
->process
, process
, req
->access
, req
->inherit
);
584 release_object( process
);
588 /* terminate a process */
589 DECL_HANDLER(terminate_process
)
591 struct process
*process
;
593 if ((process
= get_process_from_handle( req
->handle
, PROCESS_TERMINATE
)))
595 kill_process( process
, req
->exit_code
);
596 release_object( process
);
600 /* fetch information about a process */
601 DECL_HANDLER(get_process_info
)
603 struct process
*process
;
605 if ((process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_INFORMATION
)))
607 get_process_info( process
, req
);
608 release_object( process
);
612 /* set information about a process */
613 DECL_HANDLER(set_process_info
)
615 struct process
*process
;
617 if ((process
= get_process_from_handle( req
->handle
, PROCESS_SET_INFORMATION
)))
619 set_process_info( process
, req
);
620 release_object( process
);
624 /* read data from a process address space */
625 DECL_HANDLER(read_process_memory
)
627 struct process
*process
;
629 if ((process
= get_process_from_handle( req
->handle
, PROCESS_VM_READ
)))
631 read_process_memory( process
, req
->addr
, req
->len
,
632 get_req_size( req
->data
, sizeof(int) ), req
->data
);
633 release_object( process
);
637 /* write data to a process address space */
638 DECL_HANDLER(write_process_memory
)
640 struct process
*process
;
642 if ((process
= get_process_from_handle( req
->handle
, PROCESS_VM_WRITE
)))
644 write_process_memory( process
, req
->addr
, req
->len
, get_req_size( req
->data
, sizeof(int) ),
645 req
->first_mask
, req
->last_mask
, req
->data
);
646 release_object( process
);