Install the wine server in $(bindir) and exec it from there.
[wine] / server / process.c
1 /*
2  * Server-side process management
3  *
4  * Copyright (C) 1998 Alexandre Julliard
5  */
6
7 #include <assert.h>
8 #include <limits.h>
9 #include <signal.h>
10 #include <string.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <sys/time.h>
14 #include <unistd.h>
15
16 #include "winerror.h"
17 #include "winbase.h"
18 #include "winnt.h"
19
20 #include "handle.h"
21 #include "process.h"
22 #include "thread.h"
23 #include "request.h"
24
25 /* process structure */
26
27 static struct process *first_process;
28 static int running_processes;
29
30 /* process operations */
31
32 static void process_dump( struct object *obj, int verbose );
33 static int process_signaled( struct object *obj, struct thread *thread );
34 static void process_destroy( struct object *obj );
35
36 static const struct object_ops process_ops =
37 {
38     sizeof(struct process),
39     process_dump,
40     add_queue,
41     remove_queue,
42     process_signaled,
43     no_satisfied,
44     no_read_fd,
45     no_write_fd,
46     no_flush,
47     no_get_file_info,
48     process_destroy
49 };
50
51
52 /* create a new process */
53 static struct process *create_process( struct process *parent, struct new_process_request *req,
54                                        const char *cmd_line, size_t len )
55 {
56     struct process *process;
57
58     if (!(process = alloc_object( &process_ops ))) return NULL;
59     process->next            = NULL;
60     process->prev            = NULL;
61     process->thread_list     = NULL;
62     process->debug_next      = NULL;
63     process->debug_prev      = NULL;
64     process->debugger        = NULL;
65     process->handles         = NULL;
66     process->exit_code       = 0x103;  /* STILL_ACTIVE */
67     process->running_threads = 0;
68     process->priority        = NORMAL_PRIORITY_CLASS;
69     process->affinity        = 1;
70     process->suspend         = 0;
71     process->console_in      = NULL;
72     process->console_out     = NULL;
73     process->info            = NULL;
74     gettimeofday( &process->start_time, NULL );
75
76     if (req->inherit_all)
77         process->handles = copy_handle_table( process, parent );
78     else
79         process->handles = alloc_handle_table( process, 0 );
80     if (!process->handles) goto error;
81
82     /* alloc a handle for the process itself */
83     alloc_handle( process, process, PROCESS_ALL_ACCESS, 0 );
84
85     if (!(process->info = mem_alloc( sizeof(*process->info) + len ))) goto error;
86     memcpy( process->info, req, sizeof(*req) );
87     memcpy( process->info->cmdline, cmd_line, len );
88     process->info->cmdline[len] = 0;
89
90     /* set the process console */
91     if (req->create_flags & CREATE_NEW_CONSOLE)
92     {
93         if (!alloc_console( process )) goto error;
94     }
95     else if (!(req->create_flags & DETACHED_PROCESS))
96     {
97         if (parent->console_in) process->console_in = grab_object( parent->console_in );
98         if (parent->console_out) process->console_out = grab_object( parent->console_out );
99     }
100
101     if (!req->inherit_all && !(req->start_flags & STARTF_USESTDHANDLES))
102     {
103         process->info->hstdin  = duplicate_handle( parent, req->hstdin, process,
104                                                    0, TRUE, DUPLICATE_SAME_ACCESS );
105         process->info->hstdout = duplicate_handle( parent, req->hstdout, process,
106                                                    0, TRUE, DUPLICATE_SAME_ACCESS );
107         process->info->hstderr = duplicate_handle( parent, req->hstderr, process,
108                                                    0, TRUE, DUPLICATE_SAME_ACCESS );
109     }
110
111     /* attach to the debugger if requested */
112     if (req->create_flags & DEBUG_PROCESS)
113         debugger_attach( process, current );
114     else if (parent && parent->debugger && !(req->create_flags & DEBUG_ONLY_THIS_PROCESS))
115         debugger_attach( process, parent->debugger );
116
117     if ((process->next = first_process) != NULL) process->next->prev = process;
118     first_process = process;
119     return process;
120
121  error:
122     free_console( process );
123     if (process->info) free( process->info );
124     if (process->handles) release_object( process->handles );
125     release_object( process );
126     return NULL;
127 }
128
129 /* create the initial process */
130 struct process *create_initial_process(void)
131 {
132     struct process *process;
133     struct new_process_request req;
134
135     req.inherit      = 0;
136     req.inherit_all  = 0;
137     req.create_flags = CREATE_NEW_CONSOLE;
138     req.start_flags  = STARTF_USESTDHANDLES;
139     req.hstdin       = -1;
140     req.hstdout      = -1;
141     req.hstderr      = -1;
142     req.cmd_show     = 0;
143     req.env_ptr      = NULL;
144     if ((process = create_process( NULL, &req, "", 1 )))
145     {
146         process->info->hstdin  = alloc_handle( process, process->console_in,
147                                                GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, 1 );
148         process->info->hstdout = alloc_handle( process, process->console_out,
149                                                GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, 1 );
150         process->info->hstderr = alloc_handle( process, process->console_out,
151                                                GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, 1 );
152     }
153     return process;
154 }
155
156 /* destroy a process when its refcount is 0 */
157 static void process_destroy( struct object *obj )
158 {
159     struct process *process = (struct process *)obj;
160     assert( obj->ops == &process_ops );
161
162     /* we can't have a thread remaining */
163     assert( !process->thread_list );
164     if (process->next) process->next->prev = process->prev;
165     if (process->prev) process->prev->next = process->next;
166     else first_process = process->next;
167     if (process->info) free( process->info );
168 }
169
170 /* dump a process on stdout for debugging purposes */
171 static void process_dump( struct object *obj, int verbose )
172 {
173     struct process *process = (struct process *)obj;
174     assert( obj->ops == &process_ops );
175
176     fprintf( stderr, "Process next=%p prev=%p console=%p/%p handles=%p\n",
177              process->next, process->prev, process->console_in, process->console_out,
178              process->handles );
179 }
180
181 static int process_signaled( struct object *obj, struct thread *thread )
182 {
183     struct process *process = (struct process *)obj;
184     return !process->running_threads;
185 }
186
187
188 /* get a process from an id (and increment the refcount) */
189 struct process *get_process_from_id( void *id )
190 {
191     struct process *p = first_process;
192     while (p && (p != id)) p = p->next;
193     if (p) grab_object( p );
194     else set_error( ERROR_INVALID_PARAMETER );
195     return p;
196 }
197
198 /* get a process from a handle (and increment the refcount) */
199 struct process *get_process_from_handle( int handle, unsigned int access )
200 {
201     return (struct process *)get_handle_obj( current->process, handle,
202                                              access, &process_ops );
203 }
204
205 /* a process has been killed (i.e. its last thread died) */
206 static void process_killed( struct process *process, int exit_code )
207 {
208     assert( !process->thread_list );
209     process->exit_code = exit_code;
210     gettimeofday( &process->end_time, NULL );
211     release_object( process->handles );
212     process->handles = NULL;
213     free_console( process );
214     wake_up( &process->obj, 0 );
215     if (!--running_processes)
216     {
217         /* last process died, close global handles */
218         close_global_handles();
219     }
220 }
221
222 /* add a thread to a process running threads list */
223 void add_process_thread( struct process *process, struct thread *thread )
224 {
225     thread->proc_next = process->thread_list;
226     thread->proc_prev = NULL;
227     if (thread->proc_next) thread->proc_next->proc_prev = thread;
228     process->thread_list = thread;
229     if (!process->running_threads++) running_processes++;
230     grab_object( thread );
231 }
232
233 /* remove a thread from a process running threads list */
234 void remove_process_thread( struct process *process, struct thread *thread )
235 {
236     assert( process->running_threads > 0 );
237     assert( process->thread_list );
238
239     if (thread->proc_next) thread->proc_next->proc_prev = thread->proc_prev;
240     if (thread->proc_prev) thread->proc_prev->proc_next = thread->proc_next;
241     else process->thread_list = thread->proc_next;
242
243     if (!--process->running_threads)
244     {
245         /* we have removed the last running thread, exit the process */
246         process_killed( process, thread->exit_code );
247     }
248     release_object( thread );
249 }
250
251 /* suspend all the threads of a process */
252 void suspend_process( struct process *process )
253 {
254     if (!process->suspend++)
255     {
256         struct thread *thread = process->thread_list;
257         for (; thread; thread = thread->proc_next)
258         {
259             if (!thread->suspend && thread->unix_pid)
260                 kill( thread->unix_pid, SIGSTOP );
261         }
262     }
263 }
264
265 /* resume all the threads of a process */
266 void resume_process( struct process *process )
267 {
268     assert (process->suspend > 0);
269     if (!--process->suspend)
270     {
271         struct thread *thread = process->thread_list;
272         for (; thread; thread = thread->proc_next)
273         {
274             if (!thread->suspend && thread->unix_pid)
275                 kill( thread->unix_pid, SIGCONT );
276         }
277     }
278 }
279
280 /* kill a process on the spot */
281 void kill_process( struct process *process, int exit_code )
282 {
283     while (process->thread_list)
284         kill_thread( process->thread_list, exit_code );
285 }
286
287 /* get all information about a process */
288 static void get_process_info( struct process *process, struct get_process_info_request *req )
289 {
290     req->pid              = process;
291     req->exit_code        = process->exit_code;
292     req->priority         = process->priority;
293     req->process_affinity = process->affinity;
294     req->system_affinity  = 1;
295 }
296
297 /* set all information about a process */
298 static void set_process_info( struct process *process,
299                               struct set_process_info_request *req )
300 {
301     if (req->mask & SET_PROCESS_INFO_PRIORITY)
302         process->priority = req->priority;
303     if (req->mask & SET_PROCESS_INFO_AFFINITY)
304     {
305         if (req->affinity != 1) set_error( ERROR_INVALID_PARAMETER );
306         else process->affinity = req->affinity;
307     }
308 }
309
310 /* take a snapshot of currently running processes */
311 struct process_snapshot *process_snap( int *count )
312 {
313     struct process_snapshot *snapshot, *ptr;
314     struct process *process;
315     if (!running_processes) return NULL;
316     if (!(snapshot = mem_alloc( sizeof(*snapshot) * running_processes )))
317         return NULL;
318     ptr = snapshot;
319     for (process = first_process; process; process = process->next)
320     {
321         if (!process->running_threads) continue;
322         ptr->process  = process;
323         ptr->threads  = process->running_threads;
324         ptr->priority = process->priority;
325         grab_object( process );
326         ptr++;
327     }
328     *count = running_processes;
329     return snapshot;
330 }
331
332 /* create a new process */
333 DECL_HANDLER(new_process)
334 {
335     size_t len = get_req_strlen( req->cmdline );
336     struct process *process;
337
338     req->handle = -1;
339     req->pid    = NULL;
340     if ((process = create_process( current->process, req, req->cmdline, len )))
341     {
342         req->handle = alloc_handle( current->process, process, PROCESS_ALL_ACCESS, req->inherit );
343         req->pid    = process;
344         release_object( process );
345     }
346 }
347
348 /* initialize a new process */
349 DECL_HANDLER(init_process)
350 {
351     struct new_process_request *info;
352
353     if (current->state == STARTING)
354     {
355         fatal_protocol_error( current, "init_process: init_thread not called yet\n" );
356         return;
357     }
358     if (!(info = current->process->info))
359     {
360         fatal_protocol_error( current, "init_process: called twice\n" );
361         return;
362     }
363     current->process->info = NULL;
364     req->start_flags = info->start_flags;
365     req->hstdin      = info->hstdin;
366     req->hstdout     = info->hstdout;
367     req->hstderr     = info->hstderr;
368     req->cmd_show    = info->cmd_show;
369     req->env_ptr     = info->env_ptr;
370     strcpy( req->cmdline, info->cmdline );
371     free( info );
372 }
373
374 /* open a handle to a process */
375 DECL_HANDLER(open_process)
376 {
377     struct process *process = get_process_from_id( req->pid );
378     req->handle = -1;
379     if (process)
380     {
381         req->handle = alloc_handle( current->process, process, req->access, req->inherit );
382         release_object( process );
383     }
384 }
385
386 /* terminate a process */
387 DECL_HANDLER(terminate_process)
388 {
389     struct process *process;
390
391     if ((process = get_process_from_handle( req->handle, PROCESS_TERMINATE )))
392     {
393         kill_process( process, req->exit_code );
394         release_object( process );
395     }
396 }
397
398 /* fetch information about a process */
399 DECL_HANDLER(get_process_info)
400 {
401     struct process *process;
402
403     if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
404     {
405         get_process_info( process, req );
406         release_object( process );
407     }
408 }
409
410 /* set information about a process */
411 DECL_HANDLER(set_process_info)
412 {
413     struct process *process;
414
415     if ((process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION )))
416     {
417         set_process_info( process, req );
418         release_object( process );
419     }
420 }