2 * Server-side request handling
4 * Copyright (C) 1998 Alexandre Julliard
11 #include <sys/types.h>
18 #define WANT_REQUEST_HANDLERS
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) ); \
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
, ... )
36 va_start( args
, err
);
37 fprintf( stderr
, "Protocol error:%p: ", current
);
38 vfprintf( stderr
, err
, 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
];
51 if ((req
< 0) || (req
>= REQ_NB_REQUESTS
))
53 fatal_protocol_error( "unknown request %d\n", req
);
57 if (len
< handler
->min_size
)
59 fatal_protocol_error( "req %d bad length %d < %d)\n", req
, len
, handler
->min_size
);
63 /* now call the handler */
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
);
75 /* handle a client timeout (unused for now) */
76 void call_timeout_handler( struct thread
*thread
)
79 if (debug_level
) trace_timeout();
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
;
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
;
107 if ((new_fd
= dup(fd
)) == -1)
110 err
= ERROR_TOO_MANY_OPEN_FILES
;
113 if (!(new_thread
= create_thread( new_fd
, req
->pid
, &reply
.thandle
,
117 err
= ERROR_OUTOFMEMORY
;
120 reply
.tid
= new_thread
;
121 reply
.pid
= new_thread
->process
;
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 );
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" );
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';
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
;
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 );
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
);
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 */
271 if (len
!= req
->count
* sizeof(int))
272 fatal_protocol_error( "select: bad length %d for %d handles\n",
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 };
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
);
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
)
301 pulse_event( req
->handle
);
304 set_event( req
->handle
);
307 reset_event( req
->handle
);
310 fatal_protocol_error( "event_op: invalid operation %d\n", req
->op
);
312 send_reply( current
, -1, 0 );
316 DECL_HANDLER(create_mutex
)
318 struct create_mutex_reply reply
= { -1 };
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
);
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 };
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
);
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
);
377 reply
.handle
= open_event( req
->access
, req
->inherit
, name
);
380 reply
.handle
= open_mutex( req
->access
, req
->inherit
, name
);
383 reply
.handle
= open_semaphore( req
->access
, req
->inherit
, name
);
386 reply
.handle
= open_mapping( req
->access
, req
->inherit
, name
);
389 fatal_protocol_error( "open_named_obj: invalid type %d\n", req
->type
);
391 send_reply( current
, -1, 1, &reply
, sizeof(reply
) );
395 DECL_HANDLER(create_file
)
397 struct create_file_reply reply
= { -1 };
399 char *name
= (char *)data
;
400 if (!len
) name
= NULL
;
401 else CHECK_STRING( "create_file", name
, len
);
403 if ((obj
= create_file( fd
, name
, req
->access
,
404 req
->sharing
, req
->create
, req
->attrs
)) != NULL
)
406 reply
.handle
= alloc_handle( current
->process
, obj
, req
->access
, req
->inherit
);
407 release_object( obj
);
409 send_reply( current
, -1, 1, &reply
, sizeof(reply
) );
412 /* get a Unix fd to read from a file */
413 DECL_HANDLER(get_read_fd
)
418 if ((obj
= get_handle_obj( current
->process
, req
->handle
, GENERIC_READ
, NULL
)))
420 read_fd
= obj
->ops
->get_read_fd( obj
);
421 release_object( obj
);
424 send_reply( current
, read_fd
, 0 );
427 /* get a Unix fd to write to a file */
428 DECL_HANDLER(get_write_fd
)
433 if ((obj
= get_handle_obj( current
->process
, req
->handle
, GENERIC_WRITE
, NULL
)))
435 write_fd
= obj
->ops
->get_write_fd( obj
);
436 release_object( obj
);
439 send_reply( current
, write_fd
, 0 );
442 /* set a file current position */
443 DECL_HANDLER(set_file_pointer
)
445 struct set_file_pointer_reply reply
= { req
->low
, req
->high
};
446 set_file_pointer( req
->handle
, &reply
.low
, &reply
.high
, req
->whence
);
447 send_reply( current
, -1, 1, &reply
, sizeof(reply
) );
450 /* truncate (or extend) a file */
451 DECL_HANDLER(truncate_file
)
453 truncate_file( req
->handle
);
454 send_reply( current
, -1, 0 );
457 /* flush a file buffers */
458 DECL_HANDLER(flush_file
)
462 if ((obj
= get_handle_obj( current
->process
, req
->handle
, GENERIC_WRITE
, NULL
)))
464 obj
->ops
->flush( obj
);
465 release_object( obj
);
467 send_reply( current
, -1, 0 );
470 /* set a file access and modification times */
471 DECL_HANDLER(set_file_time
)
473 set_file_time( req
->handle
, req
->access_time
, req
->write_time
);
474 send_reply( current
, -1, 0 );
477 /* get a file information */
478 DECL_HANDLER(get_file_info
)
481 struct get_file_info_reply reply
;
483 if ((obj
= get_handle_obj( current
->process
, req
->handle
, 0, NULL
)))
485 obj
->ops
->get_file_info( obj
, &reply
);
486 release_object( obj
);
488 send_reply( current
, -1, 1, &reply
, sizeof(reply
) );
491 /* create an anonymous pipe */
492 DECL_HANDLER(create_pipe
)
494 struct create_pipe_reply reply
= { -1, -1 };
495 struct object
*obj
[2];
496 if (create_pipe( obj
))
498 reply
.handle_read
= alloc_handle( current
->process
, obj
[0],
499 STANDARD_RIGHTS_REQUIRED
|SYNCHRONIZE
|GENERIC_READ
,
501 if (reply
.handle_read
!= -1)
503 reply
.handle_write
= alloc_handle( current
->process
, obj
[1],
504 STANDARD_RIGHTS_REQUIRED
|SYNCHRONIZE
|GENERIC_WRITE
,
506 if (reply
.handle_write
== -1)
507 close_handle( current
->process
, reply
.handle_read
);
509 release_object( obj
[0] );
510 release_object( obj
[1] );
512 send_reply( current
, -1, 1, &reply
, sizeof(reply
) );
515 /* create a console */
516 DECL_HANDLER(create_console
)
518 struct create_console_reply reply
= { -1, -1 };
519 struct object
*obj
[2];
520 if (create_console( fd
, obj
))
522 reply
.handle_read
= alloc_handle( current
->process
, obj
[0],
523 STANDARD_RIGHTS_REQUIRED
|SYNCHRONIZE
|GENERIC_READ
,
525 if (reply
.handle_read
!= -1)
527 reply
.handle_write
= alloc_handle( current
->process
, obj
[1],
528 STANDARD_RIGHTS_REQUIRED
|SYNCHRONIZE
|GENERIC_WRITE
,
530 if (reply
.handle_write
== -1)
531 close_handle( current
->process
, reply
.handle_read
);
533 release_object( obj
[0] );
534 release_object( obj
[1] );
536 send_reply( current
, -1, 1, &reply
, sizeof(reply
) );
539 /* set a console fd */
540 DECL_HANDLER(set_console_fd
)
542 set_console_fd( req
->handle
, fd
);
543 send_reply( current
, -1, 0 );
546 /* create a change notification */
547 DECL_HANDLER(create_change_notification
)
550 struct create_change_notification_reply reply
= { -1 };
552 if ((obj
= create_change_notification( req
->subtree
, req
->filter
)))
554 reply
.handle
= alloc_handle( current
->process
, obj
,
555 STANDARD_RIGHTS_REQUIRED
|SYNCHRONIZE
, 0 );
556 release_object( obj
);
558 send_reply( current
, -1, 1, &reply
, sizeof(reply
) );
561 /* create a file mapping */
562 DECL_HANDLER(create_mapping
)
565 struct create_mapping_reply reply
= { -1 };
566 char *name
= (char *)data
;
567 if (!len
) name
= NULL
;
568 else CHECK_STRING( "create_mapping", name
, len
);
570 if ((obj
= create_mapping( req
->size_high
, req
->size_low
,
571 req
->protect
, req
->handle
, name
)))
573 int access
= FILE_MAP_ALL_ACCESS
;
574 if (!(req
->protect
& VPROT_WRITE
)) access
&= ~FILE_MAP_WRITE
;
575 reply
.handle
= alloc_handle( current
->process
, obj
, access
, 0 );
576 release_object( obj
);
578 send_reply( current
, -1, 1, &reply
, sizeof(reply
) );
581 /* get a mapping information */
582 DECL_HANDLER(get_mapping_info
)
584 struct get_mapping_info_reply reply
;
585 int map_fd
= get_mapping_info( req
->handle
, &reply
);
586 send_reply( current
, map_fd
, 1, &reply
, sizeof(reply
) );