Added change notifications.
[wine/multimedia.git] / server / request.c
blob4913cf19898c424144d86036a2074ee7e6ec4bd7
1 /*
2 * Server-side request handling
4 * Copyright (C) 1998 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include <stdarg.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <sys/types.h>
12 #include <sys/uio.h>
13 #include <unistd.h>
15 #include "winerror.h"
16 #include "winnt.h"
17 #include "winbase.h"
18 #define WANT_REQUEST_HANDLERS
19 #include "server.h"
20 #include "server/request.h"
21 #include "server/thread.h"
23 /* check that the string is NULL-terminated and that the len is correct */
24 #define CHECK_STRING(func,str,len) \
25 do { if (((str)[(len)-1] || strlen(str) != (len)-1)) \
26 fatal_protocol_error( "%s: invalid string '.*s'\n", (func), (len), (str) ); \
27 } while(0)
29 struct thread *current = NULL; /* thread handling the current request */
31 /* complain about a protocol error and terminate the client connection */
32 static void fatal_protocol_error( const char *err, ... )
34 va_list args;
36 va_start( args, err );
37 fprintf( stderr, "Protocol error:%p: ", current );
38 vfprintf( stderr, err, args );
39 va_end( args );
40 remove_client( current->client_fd, -2 );
43 /* call a request handler */
44 void call_req_handler( struct thread *thread, enum request req,
45 void *data, int len, int fd )
47 const struct handler *handler = &req_handlers[req];
48 char *ptr;
50 current = thread;
51 if ((req < 0) || (req >= REQ_NB_REQUESTS))
53 fatal_protocol_error( "unknown request %d\n", req );
54 return;
57 if (len < handler->min_size)
59 fatal_protocol_error( "req %d bad length %d < %d)\n", req, len, handler->min_size );
60 return;
63 /* now call the handler */
64 if (current)
66 CLEAR_ERROR();
67 if (debug_level) trace_request( req, data, len, fd );
69 len -= handler->min_size;
70 ptr = (char *)data + handler->min_size;
71 handler->handler( data, ptr, len, fd );
72 current = NULL;
75 /* handle a client timeout (unused for now) */
76 void call_timeout_handler( struct thread *thread )
78 current = thread;
79 if (debug_level) trace_timeout();
80 CLEAR_ERROR();
81 thread_timeout();
82 current = NULL;
85 /* a thread has been killed */
86 void call_kill_handler( struct thread *thread, int exit_code )
88 /* must be reentrant WRT call_req_handler */
89 struct thread *old_current = current;
90 current = thread;
91 if (current)
93 if (debug_level) trace_kill( exit_code );
94 thread_killed( current, exit_code );
96 current = (old_current != thread) ? old_current : NULL;
100 /* create a new thread */
101 DECL_HANDLER(new_thread)
103 struct new_thread_reply reply;
104 struct thread *new_thread;
105 int new_fd, err;
107 if ((new_fd = dup(fd)) == -1)
109 new_thread = NULL;
110 err = ERROR_TOO_MANY_OPEN_FILES;
111 goto done;
113 if (!(new_thread = create_thread( new_fd, req->pid, &reply.thandle,
114 &reply.phandle )))
116 close( new_fd );
117 err = ERROR_OUTOFMEMORY;
118 goto done;
120 reply.tid = new_thread;
121 reply.pid = new_thread->process;
122 err = ERROR_SUCCESS;
124 done:
125 if (!current)
127 /* first client doesn't have a current */
128 struct iovec vec = { &reply, sizeof(reply) };
129 send_reply_v( get_initial_client_fd(), err, -1, &vec, 1 );
131 else
133 SET_ERROR( err );
134 send_reply( current, -1, 1, &reply, sizeof(reply) );
138 /* create a new thread */
139 DECL_HANDLER(init_thread)
141 if (current->state != STARTING)
143 fatal_protocol_error( "init_thread: already running\n" );
144 return;
146 current->state = RUNNING;
147 current->unix_pid = req->unix_pid;
148 if (!(current->name = mem_alloc( len + 1 ))) goto done;
149 memcpy( current->name, data, len );
150 current->name[len] = '\0';
151 CLEAR_ERROR();
152 done:
153 send_reply( current, -1, 0 );
156 /* set the debug level */
157 DECL_HANDLER(set_debug)
159 debug_level = req->level;
160 /* Make sure last_req is initialized */
161 current->last_req = REQ_SET_DEBUG;
162 CLEAR_ERROR();
163 send_reply( current, -1, 0 );
166 /* terminate a process */
167 DECL_HANDLER(terminate_process)
169 struct process *process;
171 if ((process = get_process_from_handle( req->handle, PROCESS_TERMINATE )))
173 kill_process( process, req->exit_code );
174 release_object( process );
176 if (current) send_reply( current, -1, 0 );
179 /* terminate a thread */
180 DECL_HANDLER(terminate_thread)
182 struct thread *thread;
184 if ((thread = get_thread_from_handle( req->handle, THREAD_TERMINATE )))
186 kill_thread( thread, req->exit_code );
187 release_object( thread );
189 if (current) send_reply( current, -1, 0 );
192 /* close a handle */
193 DECL_HANDLER(close_handle)
195 close_handle( current->process, req->handle );
196 send_reply( current, -1, 0 );
199 /* duplicate a handle */
200 DECL_HANDLER(dup_handle)
202 struct dup_handle_reply reply = { -1 };
203 struct process *src, *dst;
205 if ((src = get_process_from_handle( req->src_process, PROCESS_DUP_HANDLE )))
207 if (req->options & DUP_HANDLE_MAKE_GLOBAL)
209 reply.handle = duplicate_handle( src, req->src_handle, NULL, -1,
210 req->access, req->inherit, req->options );
212 else if ((dst = get_process_from_handle( req->dst_process, PROCESS_DUP_HANDLE )))
214 reply.handle = duplicate_handle( src, req->src_handle, dst, req->dst_handle,
215 req->access, req->inherit, req->options );
216 release_object( dst );
218 /* close the handle no matter what happened */
219 if (req->options & DUP_HANDLE_CLOSE_SOURCE)
220 close_handle( src, req->src_handle );
221 release_object( src );
223 send_reply( current, -1, 1, &reply, sizeof(reply) );
226 /* fetch information about a process */
227 DECL_HANDLER(get_process_info)
229 struct process *process;
230 struct get_process_info_reply reply = { 0, 0 };
232 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
234 get_process_info( process, &reply );
235 release_object( process );
237 send_reply( current, -1, 1, &reply, sizeof(reply) );
240 /* fetch information about a thread */
241 DECL_HANDLER(get_thread_info)
243 struct thread *thread;
244 struct get_thread_info_reply reply = { 0, 0 };
246 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
248 get_thread_info( thread, &reply );
249 release_object( thread );
251 send_reply( current, -1, 1, &reply, sizeof(reply) );
254 /* open a handle to a process */
255 DECL_HANDLER(open_process)
257 struct open_process_reply reply = { -1 };
258 struct process *process = get_process_from_id( req->pid );
259 if (process)
261 reply.handle = alloc_handle( current->process, process,
262 req->access, req->inherit );
263 release_object( process );
265 send_reply( current, -1, 1, &reply, sizeof(reply) );
268 /* select on a handle list */
269 DECL_HANDLER(select)
271 if (len != req->count * sizeof(int))
272 fatal_protocol_error( "select: bad length %d for %d handles\n",
273 len, req->count );
274 sleep_on( current, req->count, (int *)data, req->flags, req->timeout );
277 /* create an event */
278 DECL_HANDLER(create_event)
280 struct create_event_reply reply = { -1 };
281 struct object *obj;
282 char *name = (char *)data;
283 if (!len) name = NULL;
284 else CHECK_STRING( "create_event", name, len );
286 obj = create_event( name, req->manual_reset, req->initial_state );
287 if (obj)
289 reply.handle = alloc_handle( current->process, obj, EVENT_ALL_ACCESS, req->inherit );
290 release_object( obj );
292 send_reply( current, -1, 1, &reply, sizeof(reply) );
295 /* do an event operation */
296 DECL_HANDLER(event_op)
298 switch(req->op)
300 case PULSE_EVENT:
301 pulse_event( req->handle );
302 break;
303 case SET_EVENT:
304 set_event( req->handle );
305 break;
306 case RESET_EVENT:
307 reset_event( req->handle );
308 break;
309 default:
310 fatal_protocol_error( "event_op: invalid operation %d\n", req->op );
312 send_reply( current, -1, 0 );
315 /* create a mutex */
316 DECL_HANDLER(create_mutex)
318 struct create_mutex_reply reply = { -1 };
319 struct object *obj;
320 char *name = (char *)data;
321 if (!len) name = NULL;
322 else CHECK_STRING( "create_mutex", name, len );
324 obj = create_mutex( name, req->owned );
325 if (obj)
327 reply.handle = alloc_handle( current->process, obj, MUTEX_ALL_ACCESS, req->inherit );
328 release_object( obj );
330 send_reply( current, -1, 1, &reply, sizeof(reply) );
333 /* release a mutex */
334 DECL_HANDLER(release_mutex)
336 if (release_mutex( req->handle )) CLEAR_ERROR();
337 send_reply( current, -1, 0 );
340 /* create a semaphore */
341 DECL_HANDLER(create_semaphore)
343 struct create_semaphore_reply reply = { -1 };
344 struct object *obj;
345 char *name = (char *)data;
346 if (!len) name = NULL;
347 else CHECK_STRING( "create_semaphore", name, len );
349 obj = create_semaphore( name, req->initial, req->max );
350 if (obj)
352 reply.handle = alloc_handle( current->process, obj, SEMAPHORE_ALL_ACCESS, req->inherit );
353 release_object( obj );
355 send_reply( current, -1, 1, &reply, sizeof(reply) );
358 /* release a semaphore */
359 DECL_HANDLER(release_semaphore)
361 struct release_semaphore_reply reply;
362 if (release_semaphore( req->handle, req->count, &reply.prev_count )) CLEAR_ERROR();
363 send_reply( current, -1, 1, &reply, sizeof(reply) );
366 /* open a handle to a named object (event, mutex, semaphore) */
367 DECL_HANDLER(open_named_obj)
369 struct open_named_obj_reply reply;
370 char *name = (char *)data;
371 if (!len) name = NULL;
372 else CHECK_STRING( "open_named_obj", name, len );
374 switch(req->type)
376 case OPEN_EVENT:
377 reply.handle = open_event( req->access, req->inherit, name );
378 break;
379 case OPEN_MUTEX:
380 reply.handle = open_mutex( req->access, req->inherit, name );
381 break;
382 case OPEN_SEMAPHORE:
383 reply.handle = open_semaphore( req->access, req->inherit, name );
384 break;
385 default:
386 fatal_protocol_error( "open_named_obj: invalid type %d\n", req->type );
388 send_reply( current, -1, 1, &reply, sizeof(reply) );
391 /* create a file */
392 DECL_HANDLER(create_file)
394 struct create_file_reply reply = { -1 };
395 struct object *obj;
396 int new_fd;
398 if ((new_fd = dup(fd)) == -1)
400 SET_ERROR( ERROR_TOO_MANY_OPEN_FILES );
401 goto done;
403 if ((obj = create_file( new_fd )) != NULL)
405 reply.handle = alloc_handle( current->process, obj, req->access, req->inherit );
406 release_object( obj );
408 done:
409 send_reply( current, -1, 1, &reply, sizeof(reply) );
412 /* get a Unix handle to a file */
413 DECL_HANDLER(get_unix_handle)
415 int handle = file_get_unix_handle( req->handle, req->access );
416 send_reply( current, handle, 0 );
419 /* get a Unix fd to read from a file */
420 DECL_HANDLER(get_read_fd)
422 struct object *obj;
423 int read_fd;
425 if ((obj = get_handle_obj( current->process, req->handle, GENERIC_READ, NULL )))
427 read_fd = obj->ops->get_read_fd( obj );
428 release_object( obj );
430 else read_fd = -1;
431 send_reply( current, read_fd, 0 );
434 /* get a Unix fd to write to a file */
435 DECL_HANDLER(get_write_fd)
437 struct object *obj;
438 int write_fd;
440 if ((obj = get_handle_obj( current->process, req->handle, GENERIC_WRITE, NULL )))
442 write_fd = obj->ops->get_write_fd( obj );
443 release_object( obj );
445 else write_fd = -1;
446 send_reply( current, write_fd, 0 );
449 /* set a file current position */
450 DECL_HANDLER(set_file_pointer)
452 struct set_file_pointer_reply reply = { req->low, req->high };
453 set_file_pointer( req->handle, &reply.low, &reply.high, req->whence );
454 send_reply( current, -1, 1, &reply, sizeof(reply) );
457 /* truncate (or extend) a file */
458 DECL_HANDLER(truncate_file)
460 truncate_file( req->handle );
461 send_reply( current, -1, 0 );
464 /* flush a file buffers */
465 DECL_HANDLER(flush_file)
467 struct object *obj;
469 if ((obj = get_handle_obj( current->process, req->handle, GENERIC_WRITE, NULL )))
471 obj->ops->flush( obj );
472 release_object( obj );
474 send_reply( current, -1, 0 );
477 /* get a file information */
478 DECL_HANDLER(get_file_info)
480 struct get_file_info_reply reply;
481 get_file_info( req->handle, &reply );
482 send_reply( current, -1, 1, &reply, sizeof(reply) );
485 /* create an anonymous pipe */
486 DECL_HANDLER(create_pipe)
488 struct create_pipe_reply reply = { -1, -1 };
489 struct object *obj[2];
490 if (create_pipe( obj ))
492 reply.handle_read = alloc_handle( current->process, obj[0],
493 STANDARD_RIGHTS_REQUIRED|SYNCHRONIZE|GENERIC_READ,
494 req->inherit );
495 if (reply.handle_read != -1)
497 reply.handle_write = alloc_handle( current->process, obj[1],
498 STANDARD_RIGHTS_REQUIRED|SYNCHRONIZE|GENERIC_WRITE,
499 req->inherit );
500 if (reply.handle_write == -1)
501 close_handle( current->process, reply.handle_read );
503 release_object( obj[0] );
504 release_object( obj[1] );
506 send_reply( current, -1, 1, &reply, sizeof(reply) );
509 /* create a console */
510 DECL_HANDLER(create_console)
512 struct create_console_reply reply = { -1, -1 };
513 struct object *obj[2];
514 if (create_console( fd, obj ))
516 reply.handle_read = alloc_handle( current->process, obj[0],
517 STANDARD_RIGHTS_REQUIRED|SYNCHRONIZE|GENERIC_READ,
518 req->inherit );
519 if (reply.handle_read != -1)
521 reply.handle_write = alloc_handle( current->process, obj[1],
522 STANDARD_RIGHTS_REQUIRED|SYNCHRONIZE|GENERIC_WRITE,
523 req->inherit );
524 if (reply.handle_write == -1)
525 close_handle( current->process, reply.handle_read );
527 release_object( obj[0] );
528 release_object( obj[1] );
530 send_reply( current, -1, 1, &reply, sizeof(reply) );
533 /* set a console fd */
534 DECL_HANDLER(set_console_fd)
536 set_console_fd( req->handle, fd );
537 send_reply( current, -1, 0 );
540 /* create a change notification */
541 DECL_HANDLER(create_change_notification)
543 struct object *obj;
544 struct create_change_notification_reply reply = { -1 };
546 if ((obj = create_change_notification( req->subtree, req->filter )))
548 reply.handle = alloc_handle( current->process, obj,
549 STANDARD_RIGHTS_REQUIRED|SYNCHRONIZE, 0 );
550 release_object( obj );
552 send_reply( current, -1, 1, &reply, sizeof(reply) );