No longer directly accessing debuggee memory.
[wine] / server / process.c
1 /*
2  * Server-side process management
3  *
4  * Copyright (C) 1998 Alexandre Julliard
5  */
6
7 #include "config.h"
8
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>
21
22 #include "winbase.h"
23 #include "winnt.h"
24
25 #include "handle.h"
26 #include "process.h"
27 #include "thread.h"
28 #include "request.h"
29
30 /* process structure */
31
32 static struct process *first_process;
33 static int running_processes;
34
35 /* process operations */
36
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 );
40
41 static const struct object_ops process_ops =
42 {
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 */
56 };
57
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 )
61 {
62     if (!(process->info = mem_alloc( sizeof(*process->info) + len ))) return 0;
63     if (req)
64     {
65         /* copy the request structure */
66         memcpy( process->info, req, sizeof(*req) );
67     }
68     else  /* no request, use defaults */
69     {
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;
82     }
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;
87 }
88
89 /* set the console and stdio handles for a newly created process */
90 static int set_process_console( struct process *process, struct process *parent )
91 {
92     struct new_process_request *info = process->info;
93
94     if (process->create_flags & CREATE_NEW_CONSOLE)
95     {
96         if (!alloc_console( process )) return 0;
97     }
98     else if (!(process->create_flags & DETACHED_PROCESS))
99     {
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 );
102     }
103     if (parent)
104     {
105         if (!info->inherit_all && !(info->start_flags & STARTF_USESTDHANDLES))
106         {
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 );
114         }
115     }
116     else
117     {
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 );
125     }
126     return 1;
127 }
128
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 )
133 {
134     struct process *process;
135     struct thread *thread = NULL;
136
137     if (!(process = alloc_object( &process_ops, -1 )))
138     {
139         close( fd );
140         return NULL;
141     }
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;
163
164     /* copy the request structure */
165     if (!set_creation_info( process, req, cmd_line, len )) goto error;
166
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;
172
173     /* alloc a handle for the process itself */
174     alloc_handle( process, process, PROCESS_ALL_ACCESS, 0 );
175
176     /* retrieve the main exe file */
177     if (process->info->exe_file != -1)
178     {
179         if (!(process->exe_file = get_file_obj( parent, process->info->exe_file,
180                                                 GENERIC_READ ))) goto error;
181         process->info->exe_file = -1;
182     }
183
184     /* get the init done event */
185     if (process->info->event != -1)
186     {
187         if (!(process->init_event = get_event_obj( parent, process->info->event,
188                                                    EVENT_MODIFY_STATE ))) goto error;
189     }
190
191     /* set the process console */
192     if (!set_process_console( process, parent )) goto error;
193
194     /* create the main thread */
195     if (!(thread = create_thread( fd, process, (process->create_flags & CREATE_SUSPENDED) != 0)))
196         goto error;
197
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 );
203
204     release_object( process );
205     return thread;
206
207  error:
208     close( fd );
209     free_console( process );
210     if (process->handles) release_object( process->handles );
211     release_object( process );
212     return NULL;
213 }
214
215 /* destroy a process when its refcount is 0 */
216 static void process_destroy( struct object *obj )
217 {
218     struct process *process = (struct process *)obj;
219     assert( obj->ops == &process_ops );
220
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 );
229 }
230
231 /* dump a process on stdout for debugging purposes */
232 static void process_dump( struct object *obj, int verbose )
233 {
234     struct process *process = (struct process *)obj;
235     assert( obj->ops == &process_ops );
236
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 );
240 }
241
242 static int process_signaled( struct object *obj, struct thread *thread )
243 {
244     struct process *process = (struct process *)obj;
245     return !process->running_threads;
246 }
247
248
249 /* get a process from an id (and increment the refcount) */
250 struct process *get_process_from_id( void *id )
251 {
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;
257 }
258
259 /* get a process from a handle (and increment the refcount) */
260 struct process *get_process_from_handle( int handle, unsigned int access )
261 {
262     return (struct process *)get_handle_obj( current->process, handle,
263                                              access, &process_ops );
264 }
265
266 /* a process has been killed (i.e. its last thread died) */
267 static void process_killed( struct process *process, int exit_code )
268 {
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)
279     {
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();
284     }
285 }
286
287 /* add a thread to a process running threads list */
288 void add_process_thread( struct process *process, struct thread *thread )
289 {
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 );
296 }
297
298 /* remove a thread from a process running threads list */
299 void remove_process_thread( struct process *process, struct thread *thread )
300 {
301     assert( process->running_threads > 0 );
302     assert( process->thread_list );
303
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;
307
308     if (!--process->running_threads)
309     {
310         /* we have removed the last running thread, exit the process */
311         process_killed( process, thread->exit_code );
312     }
313     release_object( thread );
314 }
315
316 /* suspend all the threads of a process */
317 void suspend_process( struct process *process )
318 {
319     if (!process->suspend++)
320     {
321         struct thread *thread = process->thread_list;
322         for (; thread; thread = thread->proc_next)
323         {
324             if (!thread->suspend) stop_thread( thread );
325         }
326     }
327 }
328
329 /* resume all the threads of a process */
330 void resume_process( struct process *process )
331 {
332     assert (process->suspend > 0);
333     if (!--process->suspend)
334     {
335         struct thread *thread = process->thread_list;
336         for (; thread; thread = thread->proc_next)
337         {
338             if (!thread->suspend) continue_thread( thread );
339         }
340     }
341 }
342
343 /* kill a process on the spot */
344 void kill_process( struct process *process, int exit_code )
345 {
346     while (process->thread_list)
347         kill_thread( process->thread_list, exit_code );
348 }
349
350 /* kill all processes being debugged by a given thread */
351 void kill_debugged_processes( struct thread *debugger, int exit_code )
352 {
353     for (;;)  /* restart from the beginning of the list every time */
354     {
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 );
362     }
363 }
364
365 /* get all information about a process */
366 static void get_process_info( struct process *process, struct get_process_info_request *req )
367 {
368     req->pid              = process;
369     req->debugged         = (process->debugger != 0);
370     req->exit_code        = process->exit_code;
371     req->priority         = process->priority;
372     req->process_affinity = process->affinity;
373     req->system_affinity  = 1;
374 }
375
376 /* set all information about a process */
377 static void set_process_info( struct process *process,
378                               struct set_process_info_request *req )
379 {
380     if (req->mask & SET_PROCESS_INFO_PRIORITY)
381         process->priority = req->priority;
382     if (req->mask & SET_PROCESS_INFO_AFFINITY)
383     {
384         if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER );
385         else process->affinity = req->affinity;
386     }
387 }
388
389 /* read data from a process memory space */
390 /* len is the total size (in ints), max is the size we can actually store in the output buffer */
391 /* we read the total size in all cases to check for permissions */
392 static void read_process_memory( struct process *process, const int *addr,
393                                  size_t len, size_t max, int *dest )
394 {
395     struct thread *thread = process->thread_list;
396
397     if ((unsigned int)addr % sizeof(int))  /* address must be aligned */
398     {
399         set_error( STATUS_INVALID_PARAMETER );
400         return;
401     }
402     if (!thread)  /* process is dead */
403     {
404         set_error( STATUS_ACCESS_DENIED );
405         return;
406     }
407     suspend_thread( thread, 0 );
408     if (thread->attached)
409     {
410         while (len > 0 && max)
411         {
412             if (read_thread_int( thread, addr++, dest++ ) == -1) goto done;
413             max--;
414             len--;
415         }
416         /* check the rest for read permission */
417         if (len > 0)
418         {
419             int dummy, page = get_page_size() / sizeof(int);
420             while (len >= page)
421             {
422                 addr += page;
423                 len -= page;
424                 if (read_thread_int( thread, addr - 1, &dummy ) == -1) goto done;
425             }
426             if (len && (read_thread_int( thread, addr + len - 1, &dummy ) == -1)) goto done;
427         }
428     }
429     else set_error( STATUS_ACCESS_DENIED );
430  done:
431     resume_thread( thread );
432 }
433
434 /* write data to a process memory space */
435 /* len is the total size (in ints), max is the size we can actually read from the input buffer */
436 /* we check the total size for write permissions */
437 static void write_process_memory( struct process *process, int *addr, size_t len,
438                                   size_t max, unsigned int first_mask,
439                                   unsigned int last_mask, const int *src )
440 {
441     struct thread *thread = process->thread_list;
442
443     if (!len || ((unsigned int)addr % sizeof(int)))  /* address must be aligned */
444     {
445         set_error( STATUS_INVALID_PARAMETER );
446         return;
447     }
448     if (!thread)  /* process is dead */
449     {
450         set_error( STATUS_ACCESS_DENIED );
451         return;
452     }
453     suspend_thread( thread, 0 );
454     if (thread->attached)
455     {
456         /* first word is special */
457         if (len > 1)
458         {
459             if (write_thread_int( thread, addr++, *src++, first_mask ) == -1) goto done;
460             len--;
461             max--;
462         }
463         else last_mask &= first_mask;
464
465         while (len > 1 && max)
466         {
467             if (write_thread_int( thread, addr++, *src++, ~0 ) == -1) goto done;
468             max--;
469             len--;
470         }
471
472         if (max)
473         {
474             /* last word is special too */
475             if (write_thread_int( thread, addr, *src, last_mask ) == -1) goto done;
476         }
477         else
478         {
479             /* check the rest for write permission */
480             int page = get_page_size() / sizeof(int);
481             while (len >= page)
482             {
483                 addr += page;
484                 len -= page;
485                 if (write_thread_int( thread, addr - 1, 0, 0 ) == -1) goto done;
486             }
487             if (len && (write_thread_int( thread, addr + len - 1, 0, 0 ) == -1)) goto done;
488         }
489     }
490     else set_error( STATUS_ACCESS_DENIED );
491  done:
492     resume_thread( thread );
493 }
494
495 /* take a snapshot of currently running processes */
496 struct process_snapshot *process_snap( int *count )
497 {
498     struct process_snapshot *snapshot, *ptr;
499     struct process *process;
500     if (!running_processes) return NULL;
501     if (!(snapshot = mem_alloc( sizeof(*snapshot) * running_processes )))
502         return NULL;
503     ptr = snapshot;
504     for (process = first_process; process; process = process->next)
505     {
506         if (!process->running_threads) continue;
507         ptr->process  = process;
508         ptr->threads  = process->running_threads;
509         ptr->priority = process->priority;
510         grab_object( process );
511         ptr++;
512     }
513     *count = running_processes;
514     return snapshot;
515 }
516
517 /* create a new process */
518 DECL_HANDLER(new_process)
519 {
520     size_t len = get_req_strlen( req->cmdline );
521     struct thread *thread;
522     int sock[2];
523
524     req->phandle = -1;
525     req->thandle = -1;
526     req->pid     = NULL;
527     req->tid     = NULL;
528
529     if (socketpair( AF_UNIX, SOCK_STREAM, 0, sock ) == -1)
530     {
531         file_set_error();
532         return;
533     }
534
535     if ((thread = create_process( sock[0], current->process, req, req->cmdline, len )))
536     {
537         int phandle = alloc_handle( current->process, thread->process,
538                                     PROCESS_ALL_ACCESS, req->inherit );
539         if ((req->phandle = phandle) != -1)
540         {
541             if ((req->thandle = alloc_handle( current->process, thread,
542                                               THREAD_ALL_ACCESS, req->inherit )) != -1)
543             {
544                 /* thread object will be released when the thread gets killed */
545                 set_reply_fd( current, sock[1] );
546                 req->pid = thread->process;
547                 req->tid = thread;
548                 return;
549             }
550             close_handle( current->process, phandle );
551         }
552         release_object( thread );
553     }
554     close( sock[1] );
555 }
556
557 /* initialize a new process */
558 DECL_HANDLER(init_process)
559 {
560     struct new_process_request *info;
561
562     if (!current->unix_pid)
563     {
564         fatal_protocol_error( current, "init_process: init_thread not called yet\n" );
565         return;
566     }
567     if (!(info = current->process->info))
568     {
569         fatal_protocol_error( current, "init_process: called twice\n" );
570         return;
571     }
572     current->process->ldt_copy  = req->ldt_copy;
573     current->process->ldt_flags = req->ldt_flags;
574     current->process->info = NULL;
575     req->exe_file    = -1;
576     req->start_flags = info->start_flags;
577     req->hstdin      = info->hstdin;
578     req->hstdout     = info->hstdout;
579     req->hstderr     = info->hstderr;
580     req->cmd_show    = info->cmd_show;
581     req->env_ptr     = info->env_ptr;
582     strcpy( req->cmdline, info->cmdline );
583     if (current->process->exe_file)
584         req->exe_file = alloc_handle( current->process, current->process->exe_file,
585                                       GENERIC_READ, 0 );
586     free( info );
587 }
588
589 /* signal the end of the process initialization */
590 DECL_HANDLER(init_process_done)
591 {
592     struct process *process = current->process;
593     if (!process->init_event)
594     {
595         fatal_protocol_error( current, "init_process_done: no event\n" );
596         return;
597     }
598     current->entry  = req->entry;
599     process->module = req->module;
600     generate_debug_event( current, CREATE_PROCESS_DEBUG_EVENT );
601     set_event( process->init_event );
602     release_object( process->init_event );
603     process->init_event = NULL;
604     if (current->suspend + current->process->suspend > 0) stop_thread( current );
605     req->debugged = (current->process->debugger != 0);
606 }
607
608 /* open a handle to a process */
609 DECL_HANDLER(open_process)
610 {
611     struct process *process = get_process_from_id( req->pid );
612     req->handle = -1;
613     if (process)
614     {
615         req->handle = alloc_handle( current->process, process, req->access, req->inherit );
616         release_object( process );
617     }
618 }
619
620 /* terminate a process */
621 DECL_HANDLER(terminate_process)
622 {
623     struct process *process;
624
625     if ((process = get_process_from_handle( req->handle, PROCESS_TERMINATE )))
626     {
627         kill_process( process, req->exit_code );
628         release_object( process );
629     }
630 }
631
632 /* fetch information about a process */
633 DECL_HANDLER(get_process_info)
634 {
635     struct process *process;
636
637     if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
638     {
639         get_process_info( process, req );
640         release_object( process );
641     }
642 }
643
644 /* set information about a process */
645 DECL_HANDLER(set_process_info)
646 {
647     struct process *process;
648
649     if ((process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION )))
650     {
651         set_process_info( process, req );
652         release_object( process );
653     }
654 }
655
656 /* read data from a process address space */
657 DECL_HANDLER(read_process_memory)
658 {
659     struct process *process;
660
661     if ((process = get_process_from_handle( req->handle, PROCESS_VM_READ )))
662     {
663         read_process_memory( process, req->addr, req->len,
664                              get_req_size( req->data, sizeof(int) ), req->data );
665         release_object( process );
666     }
667 }
668
669 /* write data to a process address space */
670 DECL_HANDLER(write_process_memory)
671 {
672     struct process *process;
673
674     if ((process = get_process_from_handle( req->handle, PROCESS_VM_WRITE )))
675     {
676         write_process_memory( process, req->addr, req->len, get_req_size( req->data, sizeof(int) ),
677                               req->first_mask, req->last_mask, req->data );
678         release_object( process );
679     }
680 }