Better context menus.
[wine.git] / server / process.c
blob0dd779df0ac987cec256777cb3d907de245b3450
1 /*
2 * Server-side process management
4 * Copyright (C) 1998 Alexandre Julliard
5 */
7 #include "config.h"
9 #include <assert.h>
10 #include <errno.h>
11 #include <limits.h>
12 #include <signal.h>
13 #include <string.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <sys/time.h>
17 #ifdef HAVE_SYS_SOCKET_H
18 # include <sys/socket.h>
19 #endif
20 #include <unistd.h>
22 #include "winbase.h"
23 #include "winnt.h"
25 #include "handle.h"
26 #include "process.h"
27 #include "thread.h"
28 #include "request.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 */
53 no_flush, /* flush */
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;
63 if (req)
65 /* copy the request structure */
66 memcpy( process->info, req, sizeof(*req) );
68 else /* no request, use defaults */
70 req = process->info;
71 req->inherit = 0;
72 req->inherit_all = 0;
73 req->create_flags = CREATE_NEW_CONSOLE;
74 req->start_flags = STARTF_USESTDHANDLES;
75 req->exe_file = -1;
76 req->hstdin = -1;
77 req->hstdout = -1;
78 req->hstderr = -1;
79 req->event = -1;
80 req->cmd_show = 0;
81 req->env_ptr = NULL;
83 memcpy( process->info->cmdline, cmd_line, len );
84 process->info->cmdline[len] = 0;
85 process->create_flags = process->info->create_flags;
86 return 1;
89 /* set the console and stdio handles for a newly created process */
90 static int set_process_console( struct process *process, struct process *parent )
92 struct new_process_request *info = process->info;
94 if (process->create_flags & CREATE_NEW_CONSOLE)
96 if (!alloc_console( process )) return 0;
98 else if (!(process->create_flags & DETACHED_PROCESS))
100 if (parent->console_in) process->console_in = grab_object( parent->console_in );
101 if (parent->console_out) process->console_out = grab_object( parent->console_out );
103 if (parent)
105 if (!info->inherit_all && !(info->start_flags & STARTF_USESTDHANDLES))
107 /* duplicate the handle from the parent into this process */
108 info->hstdin = duplicate_handle( parent, info->hstdin, process,
109 0, TRUE, DUPLICATE_SAME_ACCESS );
110 info->hstdout = duplicate_handle( parent, info->hstdout, process,
111 0, TRUE, DUPLICATE_SAME_ACCESS );
112 info->hstderr = duplicate_handle( parent, info->hstderr, process,
113 0, TRUE, DUPLICATE_SAME_ACCESS );
116 else
118 /* no parent, use handles to the console for stdio */
119 info->hstdin = alloc_handle( process, process->console_in,
120 GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, 1 );
121 info->hstdout = alloc_handle( process, process->console_out,
122 GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, 1 );
123 info->hstderr = alloc_handle( process, process->console_out,
124 GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, 1 );
126 return 1;
129 /* create a new process and its main thread */
130 struct thread *create_process( int fd, struct process *parent,
131 struct new_process_request *req,
132 const char *cmd_line, size_t len )
134 struct process *process;
135 struct thread *thread = NULL;
137 if (!(process = alloc_object( &process_ops, -1 )))
139 close( fd );
140 return NULL;
142 process->next = NULL;
143 process->prev = NULL;
144 process->thread_list = NULL;
145 process->debugger = NULL;
146 process->exe_file = NULL;
147 process->handles = NULL;
148 process->exit_code = STILL_ACTIVE;
149 process->running_threads = 0;
150 process->priority = NORMAL_PRIORITY_CLASS;
151 process->affinity = 1;
152 process->suspend = 0;
153 process->create_flags = 0;
154 process->console_in = NULL;
155 process->console_out = NULL;
156 process->init_event = NULL;
157 process->info = NULL;
158 process->ldt_copy = NULL;
159 process->ldt_flags = NULL;
160 gettimeofday( &process->start_time, NULL );
161 if ((process->next = first_process) != NULL) process->next->prev = process;
162 first_process = process;
164 /* copy the request structure */
165 if (!set_creation_info( process, req, cmd_line, len )) goto error;
167 if (process->info->inherit_all)
168 process->handles = copy_handle_table( process, parent );
169 else
170 process->handles = alloc_handle_table( process, 0 );
171 if (!process->handles) goto error;
173 /* alloc a handle for the process itself */
174 alloc_handle( process, process, PROCESS_ALL_ACCESS, 0 );
176 /* retrieve the main exe file */
177 if (process->info->exe_file != -1)
179 if (!(process->exe_file = get_file_obj( parent, process->info->exe_file,
180 GENERIC_READ ))) goto error;
181 process->info->exe_file = -1;
184 /* get the init done event */
185 if (process->info->event != -1)
187 if (!(process->init_event = get_event_obj( parent, process->info->event,
188 EVENT_MODIFY_STATE ))) goto error;
191 /* set the process console */
192 if (!set_process_console( process, parent )) goto error;
194 /* create the main thread */
195 if (!(thread = create_thread( fd, process, (process->create_flags & CREATE_SUSPENDED) != 0)))
196 goto error;
198 /* attach to the debugger if requested */
199 if (process->create_flags & (DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS))
200 debugger_attach( process, current );
201 else if (parent && parent->debugger && !(parent->create_flags & DEBUG_ONLY_THIS_PROCESS))
202 debugger_attach( process, parent->debugger );
204 release_object( process );
205 return thread;
207 error:
208 close( fd );
209 free_console( process );
210 if (process->handles) release_object( process->handles );
211 release_object( process );
212 return NULL;
215 /* destroy a process when its refcount is 0 */
216 static void process_destroy( struct object *obj )
218 struct process *process = (struct process *)obj;
219 assert( obj->ops == &process_ops );
221 /* we can't have a thread remaining */
222 assert( !process->thread_list );
223 if (process->next) process->next->prev = process->prev;
224 if (process->prev) process->prev->next = process->next;
225 else first_process = process->next;
226 if (process->info) free( process->info );
227 if (process->init_event) release_object( process->init_event );
228 if (process->exe_file) release_object( process->exe_file );
231 /* dump a process on stdout for debugging purposes */
232 static void process_dump( struct object *obj, int verbose )
234 struct process *process = (struct process *)obj;
235 assert( obj->ops == &process_ops );
237 fprintf( stderr, "Process next=%p prev=%p console=%p/%p handles=%p\n",
238 process->next, process->prev, process->console_in, process->console_out,
239 process->handles );
242 static int process_signaled( struct object *obj, struct thread *thread )
244 struct process *process = (struct process *)obj;
245 return !process->running_threads;
249 /* get a process from an id (and increment the refcount) */
250 struct process *get_process_from_id( void *id )
252 struct process *p = first_process;
253 while (p && (p != id)) p = p->next;
254 if (p) grab_object( p );
255 else set_error( STATUS_INVALID_PARAMETER );
256 return p;
259 /* get a process from a handle (and increment the refcount) */
260 struct process *get_process_from_handle( int handle, unsigned int access )
262 return (struct process *)get_handle_obj( current->process, handle,
263 access, &process_ops );
266 /* a process has been killed (i.e. its last thread died) */
267 static void process_killed( struct process *process, int exit_code )
269 assert( !process->thread_list );
270 process->exit_code = exit_code;
271 gettimeofday( &process->end_time, NULL );
272 release_object( process->handles );
273 process->handles = NULL;
274 free_console( process );
275 if (process->exe_file) release_object( process->exe_file );
276 process->exe_file = NULL;
277 wake_up( &process->obj, 0 );
278 if (!--running_processes)
280 /* last process died, close global handles */
281 close_global_handles();
282 /* this will cause the select loop to terminate */
283 if (!persistent_server) close_master_socket();
287 /* add a thread to a process running threads list */
288 void add_process_thread( struct process *process, struct thread *thread )
290 thread->proc_next = process->thread_list;
291 thread->proc_prev = NULL;
292 if (thread->proc_next) thread->proc_next->proc_prev = thread;
293 process->thread_list = thread;
294 if (!process->running_threads++) running_processes++;
295 grab_object( thread );
298 /* remove a thread from a process running threads list */
299 void remove_process_thread( struct process *process, struct thread *thread )
301 assert( process->running_threads > 0 );
302 assert( process->thread_list );
304 if (thread->proc_next) thread->proc_next->proc_prev = thread->proc_prev;
305 if (thread->proc_prev) thread->proc_prev->proc_next = thread->proc_next;
306 else process->thread_list = thread->proc_next;
308 if (!--process->running_threads)
310 /* we have removed the last running thread, exit the process */
311 process_killed( process, thread->exit_code );
313 release_object( thread );
316 /* suspend all the threads of a process */
317 void suspend_process( struct process *process )
319 if (!process->suspend++)
321 struct thread *thread = process->thread_list;
322 for (; thread; thread = thread->proc_next)
324 if (!thread->suspend) stop_thread( thread );
329 /* resume all the threads of a process */
330 void resume_process( struct process *process )
332 assert (process->suspend > 0);
333 if (!--process->suspend)
335 struct thread *thread = process->thread_list;
336 for (; thread; thread = thread->proc_next)
338 if (!thread->suspend) continue_thread( thread );
343 /* kill a process on the spot */
344 void kill_process( struct process *process, int exit_code )
346 while (process->thread_list)
347 kill_thread( process->thread_list, exit_code );
350 /* kill all processes being debugged by a given thread */
351 void kill_debugged_processes( struct thread *debugger, int exit_code )
353 for (;;) /* restart from the beginning of the list every time */
355 struct process *process = first_process;
356 /* find the first process being debugged by 'debugger' and still running */
357 while (process && (process->debugger != debugger || !process->running_threads))
358 process = process->next;
359 if (!process) return;
360 process->debugger = NULL;
361 kill_process( process, exit_code );
365 /* get all information about a process */
366 static void get_process_info( struct process *process, struct get_process_info_request *req )
368 req->pid = process;
369 req->exit_code = process->exit_code;
370 req->priority = process->priority;
371 req->process_affinity = process->affinity;
372 req->system_affinity = 1;
375 /* set all information about a process */
376 static void set_process_info( struct process *process,
377 struct set_process_info_request *req )
379 if (req->mask & SET_PROCESS_INFO_PRIORITY)
380 process->priority = req->priority;
381 if (req->mask & SET_PROCESS_INFO_AFFINITY)
383 if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER );
384 else process->affinity = req->affinity;
388 /* read data from a process memory space */
389 /* len is the total size (in ints), max is the size we can actually store in the output buffer */
390 /* we read the total size in all cases to check for permissions */
391 static void read_process_memory( struct process *process, const int *addr,
392 size_t len, size_t max, int *dest )
394 struct thread *thread = process->thread_list;
396 if ((unsigned int)addr % sizeof(int)) /* address must be aligned */
398 set_error( STATUS_INVALID_PARAMETER );
399 return;
401 suspend_thread( thread, 0 );
402 if (thread->attached)
404 while (len > 0 && max)
406 if (read_thread_int( thread, addr++, dest++ ) == -1) goto done;
407 max--;
408 len--;
410 /* check the rest for read permission */
411 if (len > 0)
413 int dummy, page = get_page_size() / sizeof(int);
414 while (len >= page)
416 addr += page;
417 len -= page;
418 if (read_thread_int( thread, addr - 1, &dummy ) == -1) goto done;
420 if (len && (read_thread_int( thread, addr + len - 1, &dummy ) == -1)) goto done;
423 else set_error( STATUS_ACCESS_DENIED );
424 done:
425 resume_thread( thread );
428 /* write data to a process memory space */
429 /* len is the total size (in ints), max is the size we can actually read from the input buffer */
430 /* we check the total size for write permissions */
431 static void write_process_memory( struct process *process, int *addr, size_t len,
432 size_t max, unsigned int first_mask,
433 unsigned int last_mask, const int *src )
435 struct thread *thread = process->thread_list;
437 if (!len || ((unsigned int)addr % sizeof(int))) /* address must be aligned */
439 set_error( STATUS_INVALID_PARAMETER );
440 return;
442 suspend_thread( thread, 0 );
443 if (thread->attached)
445 /* first word is special */
446 if (len > 1)
448 if (write_thread_int( thread, addr++, *src++, first_mask ) == -1) goto done;
449 len--;
450 max--;
452 else last_mask &= first_mask;
454 while (len > 1 && max)
456 if (write_thread_int( thread, addr++, *src++, ~0 ) == -1) goto done;
457 max--;
458 len--;
461 if (max)
463 /* last word is special too */
464 if (write_thread_int( thread, addr, *src, last_mask ) == -1) goto done;
466 else
468 /* check the rest for write permission */
469 int page = get_page_size() / sizeof(int);
470 while (len >= page)
472 addr += page;
473 len -= page;
474 if (write_thread_int( thread, addr - 1, 0, 0 ) == -1) goto done;
476 if (len && (write_thread_int( thread, addr + len - 1, 0, 0 ) == -1)) goto done;
479 else set_error( STATUS_ACCESS_DENIED );
480 done:
481 resume_thread( thread );
484 /* take a snapshot of currently running processes */
485 struct process_snapshot *process_snap( int *count )
487 struct process_snapshot *snapshot, *ptr;
488 struct process *process;
489 if (!running_processes) return NULL;
490 if (!(snapshot = mem_alloc( sizeof(*snapshot) * running_processes )))
491 return NULL;
492 ptr = snapshot;
493 for (process = first_process; process; process = process->next)
495 if (!process->running_threads) continue;
496 ptr->process = process;
497 ptr->threads = process->running_threads;
498 ptr->priority = process->priority;
499 grab_object( process );
500 ptr++;
502 *count = running_processes;
503 return snapshot;
506 /* create a new process */
507 DECL_HANDLER(new_process)
509 size_t len = get_req_strlen( req->cmdline );
510 struct thread *thread;
511 int sock[2];
513 req->phandle = -1;
514 req->thandle = -1;
515 req->pid = NULL;
516 req->tid = NULL;
518 if (socketpair( AF_UNIX, SOCK_STREAM, 0, sock ) == -1)
520 file_set_error();
521 return;
524 if ((thread = create_process( sock[0], current->process, req, req->cmdline, len )))
526 int phandle = alloc_handle( current->process, thread->process,
527 PROCESS_ALL_ACCESS, req->inherit );
528 if ((req->phandle = phandle) != -1)
530 if ((req->thandle = alloc_handle( current->process, thread,
531 THREAD_ALL_ACCESS, req->inherit )) != -1)
533 /* thread object will be released when the thread gets killed */
534 set_reply_fd( current, sock[1] );
535 req->pid = thread->process;
536 req->tid = thread;
537 return;
539 close_handle( current->process, phandle );
541 release_object( thread );
543 close( sock[1] );
546 /* initialize a new process */
547 DECL_HANDLER(init_process)
549 struct new_process_request *info;
551 if (!current->unix_pid)
553 fatal_protocol_error( current, "init_process: init_thread not called yet\n" );
554 return;
556 if (!(info = current->process->info))
558 fatal_protocol_error( current, "init_process: called twice\n" );
559 return;
561 current->process->ldt_copy = req->ldt_copy;
562 current->process->ldt_flags = req->ldt_flags;
563 current->process->info = NULL;
564 req->exe_file = -1;
565 req->start_flags = info->start_flags;
566 req->hstdin = info->hstdin;
567 req->hstdout = info->hstdout;
568 req->hstderr = info->hstderr;
569 req->cmd_show = info->cmd_show;
570 req->env_ptr = info->env_ptr;
571 strcpy( req->cmdline, info->cmdline );
572 if (current->process->exe_file)
573 req->exe_file = alloc_handle( current->process, current->process->exe_file,
574 GENERIC_READ, 0 );
575 free( info );
578 /* signal the end of the process initialization */
579 DECL_HANDLER(init_process_done)
581 struct process *process = current->process;
582 if (!process->init_event)
584 fatal_protocol_error( current, "init_process_done: no event\n" );
585 return;
587 set_event( process->init_event );
588 release_object( process->init_event );
589 process->init_event = NULL;
590 if (current->suspend + current->process->suspend > 0) stop_thread( current );
593 /* open a handle to a process */
594 DECL_HANDLER(open_process)
596 struct process *process = get_process_from_id( req->pid );
597 req->handle = -1;
598 if (process)
600 req->handle = alloc_handle( current->process, process, req->access, req->inherit );
601 release_object( process );
605 /* terminate a process */
606 DECL_HANDLER(terminate_process)
608 struct process *process;
610 if ((process = get_process_from_handle( req->handle, PROCESS_TERMINATE )))
612 kill_process( process, req->exit_code );
613 release_object( process );
617 /* fetch information about a process */
618 DECL_HANDLER(get_process_info)
620 struct process *process;
622 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
624 get_process_info( process, req );
625 release_object( process );
629 /* set information about a process */
630 DECL_HANDLER(set_process_info)
632 struct process *process;
634 if ((process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION )))
636 set_process_info( process, req );
637 release_object( process );
641 /* read data from a process address space */
642 DECL_HANDLER(read_process_memory)
644 struct process *process;
646 if ((process = get_process_from_handle( req->handle, PROCESS_VM_READ )))
648 read_process_memory( process, req->addr, req->len,
649 get_req_size( req->data, sizeof(int) ), req->data );
650 release_object( process );
654 /* write data to a process address space */
655 DECL_HANDLER(write_process_memory)
657 struct process *process;
659 if ((process = get_process_from_handle( req->handle, PROCESS_VM_WRITE )))
661 write_process_memory( process, req->addr, req->len, get_req_size( req->data, sizeof(int) ),
662 req->first_mask, req->last_mask, req->data );
663 release_object( process );