Moved X11 DIB stuff to x11drv.
[wine.git] / server / request.c
blob6ce5b35e6edf7f858e594716bda2df8bef2bc0d9
1 /*
2 * Server-side request handling
4 * Copyright (C) 1998 Alexandre Julliard
5 */
7 #include <stdarg.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <sys/types.h>
11 #include <sys/uio.h>
12 #include <unistd.h>
14 #include "winerror.h"
15 #include "winnt.h"
16 #include "winbase.h"
17 #define WANT_REQUEST_HANDLERS
18 #include "server.h"
19 #include "server/request.h"
20 #include "server/thread.h"
22 /* check that the string is NULL-terminated and that the len is correct */
23 #define CHECK_STRING(func,str,len) \
24 do { if (((str)[(len)-1] || strlen(str) != (len)-1)) \
25 fatal_protocol_error( "%s: invalid string '.*s'\n", (func), (len), (str) ); \
26 } while(0)
28 struct thread *current = NULL; /* thread handling the current request */
30 /* complain about a protocol error and terminate the client connection */
31 static void fatal_protocol_error( const char *err, ... )
33 va_list args;
35 va_start( args, err );
36 fprintf( stderr, "Protocol error:%p: ", current );
37 vfprintf( stderr, err, args );
38 va_end( args );
39 remove_client( current->client_fd, -2 );
42 /* call a request handler */
43 void call_req_handler( struct thread *thread, enum request req,
44 void *data, int len, int fd )
46 const struct handler *handler = &req_handlers[req];
47 char *ptr;
49 current = thread;
50 if ((req < 0) || (req >= REQ_NB_REQUESTS))
52 fatal_protocol_error( "unknown request %d\n", req );
53 return;
56 if (len < handler->min_size)
58 fatal_protocol_error( "req %d bad length %d < %d)\n", req, len, handler->min_size );
59 return;
62 /* now call the handler */
63 if (current)
65 CLEAR_ERROR();
66 if (debug_level) trace_request( req, data, len, fd );
68 len -= handler->min_size;
69 ptr = (char *)data + handler->min_size;
70 handler->handler( data, ptr, len, fd );
71 current = NULL;
74 /* handle a client timeout (unused for now) */
75 void call_timeout_handler( struct thread *thread )
77 current = thread;
78 if (debug_level) trace_timeout();
79 CLEAR_ERROR();
80 thread_timeout();
81 current = NULL;
84 /* a thread has been killed */
85 void call_kill_handler( struct thread *thread, int exit_code )
87 /* must be reentrant WRT call_req_handler */
88 struct thread *old_current = current;
89 current = thread;
90 if (current)
92 if (debug_level) trace_kill( exit_code );
93 thread_killed( current, exit_code );
95 current = (old_current != thread) ? old_current : NULL;
99 /* create a new thread */
100 DECL_HANDLER(new_thread)
102 struct new_thread_reply reply;
103 struct thread *new_thread;
104 int new_fd, err;
106 if ((new_fd = dup(fd)) == -1)
108 new_thread = NULL;
109 err = ERROR_TOO_MANY_OPEN_FILES;
110 goto done;
112 if (!(new_thread = create_thread( new_fd, req->pid, &reply.thandle,
113 &reply.phandle )))
115 close( new_fd );
116 err = ERROR_OUTOFMEMORY;
117 goto done;
119 reply.tid = new_thread;
120 reply.pid = new_thread->process;
121 err = ERROR_SUCCESS;
123 done:
124 if (!current)
126 /* first client doesn't have a current */
127 struct iovec vec = { &reply, sizeof(reply) };
128 send_reply_v( get_initial_client_fd(), err, -1, &vec, 1 );
130 else
132 SET_ERROR( err );
133 send_reply( current, -1, 1, &reply, sizeof(reply) );
137 /* create a new thread */
138 DECL_HANDLER(init_thread)
140 if (current->state != STARTING)
142 fatal_protocol_error( "init_thread: already running\n" );
143 return;
145 current->state = RUNNING;
146 current->unix_pid = req->unix_pid;
147 if (!(current->name = mem_alloc( len + 1 ))) goto done;
148 memcpy( current->name, data, len );
149 current->name[len] = '\0';
150 CLEAR_ERROR();
151 done:
152 send_reply( current, -1, 0 );
155 /* terminate a process */
156 DECL_HANDLER(terminate_process)
158 struct process *process;
160 if ((process = get_process_from_handle( req->handle, PROCESS_TERMINATE )))
162 kill_process( process, req->exit_code );
163 release_object( process );
165 if (current) send_reply( current, -1, 0 );
168 /* terminate a thread */
169 DECL_HANDLER(terminate_thread)
171 struct thread *thread;
173 if ((thread = get_thread_from_handle( req->handle, THREAD_TERMINATE )))
175 kill_thread( thread, req->exit_code );
176 release_object( thread );
178 if (current) send_reply( current, -1, 0 );
181 /* close a handle */
182 DECL_HANDLER(close_handle)
184 close_handle( current->process, req->handle );
185 send_reply( current, -1, 0 );
188 /* duplicate a handle */
189 DECL_HANDLER(dup_handle)
191 struct dup_handle_reply reply = { -1 };
192 struct process *src, *dst;
194 if ((src = get_process_from_handle( req->src_process, PROCESS_DUP_HANDLE )))
196 if ((dst = get_process_from_handle( req->dst_process, PROCESS_DUP_HANDLE )))
198 reply.handle = duplicate_handle( src, req->src_handle, dst, req->dst_handle,
199 req->access, req->inherit, req->options );
200 release_object( dst );
202 /* close the handle no matter what happened */
203 if (req->options & DUPLICATE_CLOSE_SOURCE)
204 close_handle( src, req->src_handle );
205 release_object( src );
207 send_reply( current, -1, 1, &reply, sizeof(reply) );
210 /* fetch information about a process */
211 DECL_HANDLER(get_process_info)
213 struct process *process;
214 struct get_process_info_reply reply = { 0, 0 };
216 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
218 get_process_info( process, &reply );
219 release_object( process );
221 send_reply( current, -1, 1, &reply, sizeof(reply) );
224 /* fetch information about a thread */
225 DECL_HANDLER(get_thread_info)
227 struct thread *thread;
228 struct get_thread_info_reply reply = { 0, 0 };
230 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
232 get_thread_info( thread, &reply );
233 release_object( thread );
235 send_reply( current, -1, 1, &reply, sizeof(reply) );
238 /* open a handle to a process */
239 DECL_HANDLER(open_process)
241 struct open_process_reply reply = { -1 };
242 struct process *process = get_process_from_id( req->pid );
243 if (process)
245 reply.handle = alloc_handle( current->process, process,
246 req->access, req->inherit );
247 release_object( process );
249 send_reply( current, -1, 1, &reply, sizeof(reply) );
252 /* select on a handle list */
253 DECL_HANDLER(select)
255 if (len != req->count * sizeof(int))
256 fatal_protocol_error( "select: bad length %d for %d handles\n",
257 len, req->count );
258 sleep_on( current, req->count, (int *)data, req->flags, req->timeout );
261 /* create an event */
262 DECL_HANDLER(create_event)
264 struct create_event_reply reply = { -1 };
265 struct object *obj;
266 char *name = (char *)data;
267 if (!len) name = NULL;
268 else CHECK_STRING( "create_event", name, len );
270 obj = create_event( name, req->manual_reset, req->initial_state );
271 if (obj)
273 reply.handle = alloc_handle( current->process, obj, EVENT_ALL_ACCESS, req->inherit );
274 release_object( obj );
276 send_reply( current, -1, 1, &reply, sizeof(reply) );
279 /* do an event operation */
280 DECL_HANDLER(event_op)
282 switch(req->op)
284 case PULSE_EVENT:
285 pulse_event( req->handle );
286 break;
287 case SET_EVENT:
288 set_event( req->handle );
289 break;
290 case RESET_EVENT:
291 reset_event( req->handle );
292 break;
293 default:
294 fatal_protocol_error( "event_op: invalid operation %d\n", req->op );
296 send_reply( current, -1, 0 );
299 /* create a mutex */
300 DECL_HANDLER(create_mutex)
302 struct create_mutex_reply reply = { -1 };
303 struct object *obj;
304 char *name = (char *)data;
305 if (!len) name = NULL;
306 else CHECK_STRING( "create_mutex", name, len );
308 obj = create_mutex( name, req->owned );
309 if (obj)
311 reply.handle = alloc_handle( current->process, obj, MUTEX_ALL_ACCESS, req->inherit );
312 release_object( obj );
314 send_reply( current, -1, 1, &reply, sizeof(reply) );
317 /* release a mutex */
318 DECL_HANDLER(release_mutex)
320 if (release_mutex( req->handle )) CLEAR_ERROR();
321 send_reply( current, -1, 0 );
324 /* create a semaphore */
325 DECL_HANDLER(create_semaphore)
327 struct create_semaphore_reply reply = { -1 };
328 struct object *obj;
329 char *name = (char *)data;
330 if (!len) name = NULL;
331 else CHECK_STRING( "create_semaphore", name, len );
333 obj = create_semaphore( name, req->initial, req->max );
334 if (obj)
336 reply.handle = alloc_handle( current->process, obj, SEMAPHORE_ALL_ACCESS, req->inherit );
337 release_object( obj );
339 send_reply( current, -1, 1, &reply, sizeof(reply) );
342 /* release a semaphore */
343 DECL_HANDLER(release_semaphore)
345 struct release_semaphore_reply reply;
346 if (release_semaphore( req->handle, req->count, &reply.prev_count )) CLEAR_ERROR();
347 send_reply( current, -1, 1, &reply, sizeof(reply) );
350 /* open a handle to a named object (event, mutex, semaphore) */
351 DECL_HANDLER(open_named_obj)
353 struct open_named_obj_reply reply;
354 char *name = (char *)data;
355 if (!len) name = NULL;
356 else CHECK_STRING( "open_named_obj", name, len );
358 switch(req->type)
360 case OPEN_EVENT:
361 reply.handle = open_event( req->access, req->inherit, name );
362 break;
363 case OPEN_MUTEX:
364 reply.handle = open_mutex( req->access, req->inherit, name );
365 break;
366 case OPEN_SEMAPHORE:
367 reply.handle = open_semaphore( req->access, req->inherit, name );
368 break;
369 default:
370 fatal_protocol_error( "open_named_obj: invalid type %d\n", req->type );
372 send_reply( current, -1, 1, &reply, sizeof(reply) );