Added configure check for the unix domain sockaddr length.
[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->hstdin       = -1;
76         req->hstdout      = -1;
77         req->hstderr      = -1;
78         req->event        = -1;
79         req->cmd_show     = 0;
80         req->env_ptr      = NULL;
81     }
82     memcpy( process->info->cmdline, cmd_line, len );
83     process->info->cmdline[len] = 0;
84     process->create_flags = process->info->create_flags;
85     return 1;
86 }
87
88 /* set the console and stdio handles for a newly created process */
89 static int set_process_console( struct process *process, struct process *parent )
90 {
91     struct new_process_request *info = process->info;
92
93     if (process->create_flags & CREATE_NEW_CONSOLE)
94     {
95         if (!alloc_console( process )) return 0;
96     }
97     else if (!(process->create_flags & DETACHED_PROCESS))
98     {
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 );
101     }
102     if (parent)
103     {
104         if (!info->inherit_all && !(info->start_flags & STARTF_USESTDHANDLES))
105         {
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 );
113         }
114     }
115     else
116     {
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 );
124     }
125     return 1;
126 }
127
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 )
132 {
133     struct process *process;
134     struct thread *thread = NULL;
135
136     if (!(process = alloc_object( &process_ops, -1 )))
137     {
138         close( fd );
139         return NULL;
140     }
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;
161
162     /* copy the request structure */
163     if (!set_creation_info( process, req, cmd_line, len )) goto error;
164
165     if (process->info->inherit_all)
166         process->handles = copy_handle_table( process, parent );
167     else
168         process->handles = alloc_handle_table( process, 0 );
169     if (!process->handles) goto error;
170
171     /* alloc a handle for the process itself */
172     alloc_handle( process, process, PROCESS_ALL_ACCESS, 0 );
173
174     /* get the init done event */
175     if (process->info->event != -1)
176     {
177         if (!(process->init_event = get_event_obj( parent, process->info->event,
178                                                    EVENT_MODIFY_STATE ))) goto error;
179     }
180
181     /* set the process console */
182     if (!set_process_console( process, parent )) goto error;
183
184     /* create the main thread */
185     if (!(thread = create_thread( fd, process, (process->create_flags & CREATE_SUSPENDED) != 0)))
186         goto error;
187
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 );
193
194     release_object( process );
195     return thread;
196
197  error:
198     close( fd );
199     free_console( process );
200     if (process->handles) release_object( process->handles );
201     release_object( process );
202     return NULL;
203 }
204
205 /* destroy a process when its refcount is 0 */
206 static void process_destroy( struct object *obj )
207 {
208     struct process *process = (struct process *)obj;
209     assert( obj->ops == &process_ops );
210
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 );
218 }
219
220 /* dump a process on stdout for debugging purposes */
221 static void process_dump( struct object *obj, int verbose )
222 {
223     struct process *process = (struct process *)obj;
224     assert( obj->ops == &process_ops );
225
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,
228              process->handles );
229 }
230
231 static int process_signaled( struct object *obj, struct thread *thread )
232 {
233     struct process *process = (struct process *)obj;
234     return !process->running_threads;
235 }
236
237
238 /* get a process from an id (and increment the refcount) */
239 struct process *get_process_from_id( void *id )
240 {
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 );
245     return p;
246 }
247
248 /* get a process from a handle (and increment the refcount) */
249 struct process *get_process_from_handle( int handle, unsigned int access )
250 {
251     return (struct process *)get_handle_obj( current->process, handle,
252                                              access, &process_ops );
253 }
254
255 /* a process has been killed (i.e. its last thread died) */
256 static void process_killed( struct process *process, int exit_code )
257 {
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)
266     {
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();
271     }
272 }
273
274 /* add a thread to a process running threads list */
275 void add_process_thread( struct process *process, struct thread *thread )
276 {
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 );
283 }
284
285 /* remove a thread from a process running threads list */
286 void remove_process_thread( struct process *process, struct thread *thread )
287 {
288     assert( process->running_threads > 0 );
289     assert( process->thread_list );
290
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;
294
295     if (!--process->running_threads)
296     {
297         /* we have removed the last running thread, exit the process */
298         process_killed( process, thread->exit_code );
299     }
300     release_object( thread );
301 }
302
303 /* suspend all the threads of a process */
304 void suspend_process( struct process *process )
305 {
306     if (!process->suspend++)
307     {
308         struct thread *thread = process->thread_list;
309         for (; thread; thread = thread->proc_next)
310         {
311             if (!thread->suspend) stop_thread( thread );
312         }
313     }
314 }
315
316 /* resume all the threads of a process */
317 void resume_process( struct process *process )
318 {
319     assert (process->suspend > 0);
320     if (!--process->suspend)
321     {
322         struct thread *thread = process->thread_list;
323         for (; thread; thread = thread->proc_next)
324         {
325             if (!thread->suspend) continue_thread( thread );
326         }
327     }
328 }
329
330 /* kill a process on the spot */
331 void kill_process( struct process *process, int exit_code )
332 {
333     while (process->thread_list)
334         kill_thread( process->thread_list, exit_code );
335 }
336
337 /* kill all processes being debugged by a given thread */
338 void kill_debugged_processes( struct thread *debugger, int exit_code )
339 {
340     for (;;)  /* restart from the beginning of the list every time */
341     {
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 );
349     }
350 }
351
352 /* get all information about a process */
353 static void get_process_info( struct process *process, struct get_process_info_request *req )
354 {
355     req->pid              = process;
356     req->exit_code        = process->exit_code;
357     req->priority         = process->priority;
358     req->process_affinity = process->affinity;
359     req->system_affinity  = 1;
360 }
361
362 /* set all information about a process */
363 static void set_process_info( struct process *process,
364                               struct set_process_info_request *req )
365 {
366     if (req->mask & SET_PROCESS_INFO_PRIORITY)
367         process->priority = req->priority;
368     if (req->mask & SET_PROCESS_INFO_AFFINITY)
369     {
370         if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER );
371         else process->affinity = req->affinity;
372     }
373 }
374
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 )
380 {
381     struct thread *thread = process->thread_list;
382
383     if ((unsigned int)addr % sizeof(int))  /* address must be aligned */
384     {
385         set_error( STATUS_INVALID_PARAMETER );
386         return;
387     }
388     suspend_thread( thread, 0 );
389     if (thread->attached)
390     {
391         while (len > 0 && max)
392         {
393             if (read_thread_int( thread, addr++, dest++ ) == -1) goto done;
394             max--;
395             len--;
396         }
397         /* check the rest for read permission */
398         if (len > 0)
399         {
400             int dummy, page = get_page_size() / sizeof(int);
401             while (len >= page)
402             {
403                 addr += page;
404                 len -= page;
405                 if (read_thread_int( thread, addr - 1, &dummy ) == -1) goto done;
406             }
407             if (len && (read_thread_int( thread, addr + len - 1, &dummy ) == -1)) goto done;
408         }
409     }
410     else set_error( STATUS_ACCESS_DENIED );
411  done:
412     resume_thread( thread );
413 }
414
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 )
421 {
422     struct thread *thread = process->thread_list;
423
424     if (!len || ((unsigned int)addr % sizeof(int)))  /* address must be aligned */
425     {
426         set_error( STATUS_INVALID_PARAMETER );
427         return;
428     }
429     suspend_thread( thread, 0 );
430     if (thread->attached)
431     {
432         /* first word is special */
433         if (len > 1)
434         {
435             if (write_thread_int( thread, addr++, *src++, first_mask ) == -1) goto done;
436             len--;
437             max--;
438         }
439         else last_mask &= first_mask;
440
441         while (len > 1 && max)
442         {
443             if (write_thread_int( thread, addr++, *src++, ~0 ) == -1) goto done;
444             max--;
445             len--;
446         }
447
448         if (max)
449         {
450             /* last word is special too */
451             if (write_thread_int( thread, addr, *src, last_mask ) == -1) goto done;
452         }
453         else
454         {
455             /* check the rest for write permission */
456             int page = get_page_size() / sizeof(int);
457             while (len >= page)
458             {
459                 addr += page;
460                 len -= page;
461                 if (write_thread_int( thread, addr - 1, 0, 0 ) == -1) goto done;
462             }
463             if (len && (write_thread_int( thread, addr + len - 1, 0, 0 ) == -1)) goto done;
464         }
465     }
466     else set_error( STATUS_ACCESS_DENIED );
467  done:
468     resume_thread( thread );
469 }
470
471 /* take a snapshot of currently running processes */
472 struct process_snapshot *process_snap( int *count )
473 {
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 )))
478         return NULL;
479     ptr = snapshot;
480     for (process = first_process; process; process = process->next)
481     {
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 );
487         ptr++;
488     }
489     *count = running_processes;
490     return snapshot;
491 }
492
493 /* create a new process */
494 DECL_HANDLER(new_process)
495 {
496     size_t len = get_req_strlen( req->cmdline );
497     struct thread *thread;
498     int sock[2];
499
500     req->phandle = -1;
501     req->thandle = -1;
502     req->pid     = NULL;
503     req->tid     = NULL;
504
505     if (socketpair( AF_UNIX, SOCK_STREAM, 0, sock ) == -1)
506     {
507         file_set_error();
508         return;
509     }
510
511     if ((thread = create_process( sock[0], current->process, req, req->cmdline, len )))
512     {
513         int phandle = alloc_handle( current->process, thread->process,
514                                     PROCESS_ALL_ACCESS, req->inherit );
515         if ((req->phandle = phandle) != -1)
516         {
517             if ((req->thandle = alloc_handle( current->process, thread,
518                                               THREAD_ALL_ACCESS, req->inherit )) != -1)
519             {
520                 /* thread object will be released when the thread gets killed */
521                 set_reply_fd( current, sock[1] );
522                 req->pid = thread->process;
523                 req->tid = thread;
524                 return;
525             }
526             close_handle( current->process, phandle );
527         }
528         release_object( thread );
529     }
530     close( sock[1] );
531 }
532
533 /* initialize a new process */
534 DECL_HANDLER(init_process)
535 {
536     struct new_process_request *info;
537
538     if (!current->unix_pid)
539     {
540         fatal_protocol_error( current, "init_process: init_thread not called yet\n" );
541         return;
542     }
543     if (!(info = current->process->info))
544     {
545         fatal_protocol_error( current, "init_process: called twice\n" );
546         return;
547     }
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 );
558     free( info );
559 }
560
561 /* signal the end of the process initialization */
562 DECL_HANDLER(init_process_done)
563 {
564     struct process *process = current->process;
565     if (!process->init_event)
566     {
567         fatal_protocol_error( current, "init_process_done: no event\n" );
568         return;
569     }
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 );
574 }
575
576 /* open a handle to a process */
577 DECL_HANDLER(open_process)
578 {
579     struct process *process = get_process_from_id( req->pid );
580     req->handle = -1;
581     if (process)
582     {
583         req->handle = alloc_handle( current->process, process, req->access, req->inherit );
584         release_object( process );
585     }
586 }
587
588 /* terminate a process */
589 DECL_HANDLER(terminate_process)
590 {
591     struct process *process;
592
593     if ((process = get_process_from_handle( req->handle, PROCESS_TERMINATE )))
594     {
595         kill_process( process, req->exit_code );
596         release_object( process );
597     }
598 }
599
600 /* fetch information about a process */
601 DECL_HANDLER(get_process_info)
602 {
603     struct process *process;
604
605     if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
606     {
607         get_process_info( process, req );
608         release_object( process );
609     }
610 }
611
612 /* set information about a process */
613 DECL_HANDLER(set_process_info)
614 {
615     struct process *process;
616
617     if ((process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION )))
618     {
619         set_process_info( process, req );
620         release_object( process );
621     }
622 }
623
624 /* read data from a process address space */
625 DECL_HANDLER(read_process_memory)
626 {
627     struct process *process;
628
629     if ((process = get_process_from_handle( req->handle, PROCESS_VM_READ )))
630     {
631         read_process_memory( process, req->addr, req->len,
632                              get_req_size( req->data, sizeof(int) ), req->data );
633         release_object( process );
634     }
635 }
636
637 /* write data to a process address space */
638 DECL_HANDLER(write_process_memory)
639 {
640     struct process *process;
641
642     if ((process = get_process_from_handle( req->handle, PROCESS_VM_WRITE )))
643     {
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 );
647     }
648 }