2 * Server-side request handling
4 * Copyright (C) 1998 Alexandre Julliard
11 #include <sys/types.h>
19 #define WANT_REQUEST_HANDLERS
21 #include "server/request.h"
22 #include "server/process.h"
23 #include "server/thread.h"
25 /* check that the string is NULL-terminated and that the len is correct */
26 #define CHECK_STRING(func,str,len) \
27 do { if (((str)[(len)-1] || strlen(str) != (len)-1)) \
28 fatal_protocol_error( "%s: invalid string '%.*s'\n", (func), (len), (str) ); \
31 struct thread
*current
= NULL
; /* thread handling the current request */
33 /* complain about a protocol error and terminate the client connection */
34 static void fatal_protocol_error( const char *err
, ... )
38 va_start( args
, err
);
39 fprintf( stderr
, "Protocol error:%p: ", current
);
40 vfprintf( stderr
, err
, args
);
42 remove_client( current
->client_fd
, -2 );
45 /* call a request handler */
46 void call_req_handler( struct thread
*thread
, enum request req
,
47 void *data
, int len
, int fd
)
49 const struct handler
*handler
= &req_handlers
[req
];
53 if ((req
< 0) || (req
>= REQ_NB_REQUESTS
))
55 fatal_protocol_error( "unknown request %d\n", req
);
59 if (len
< handler
->min_size
)
61 fatal_protocol_error( "req %d bad length %d < %d)\n", req
, len
, handler
->min_size
);
65 /* now call the handler */
69 if (debug_level
) trace_request( req
, data
, len
, fd
);
71 len
-= handler
->min_size
;
72 ptr
= (char *)data
+ handler
->min_size
;
73 handler
->handler( data
, ptr
, len
, fd
);
77 /* handle a client timeout (unused for now) */
78 void call_timeout_handler( struct thread
*thread
)
81 if (debug_level
) trace_timeout();
87 /* a thread has been killed */
88 void call_kill_handler( struct thread
*thread
, int exit_code
)
90 /* must be reentrant WRT call_req_handler */
91 struct thread
*old_current
= current
;
95 if (debug_level
) trace_kill( exit_code
);
96 thread_killed( current
, exit_code
);
98 current
= (old_current
!= thread
) ? old_current
: NULL
;
102 /* create a new thread */
103 DECL_HANDLER(new_thread
)
105 struct new_thread_reply reply
;
106 struct thread
*new_thread
;
109 if ((new_fd
= dup(fd
)) == -1)
112 err
= ERROR_TOO_MANY_OPEN_FILES
;
115 if (!(new_thread
= create_thread( new_fd
, req
->pid
, &reply
.thandle
,
119 err
= ERROR_OUTOFMEMORY
;
122 reply
.tid
= new_thread
;
123 reply
.pid
= new_thread
->process
;
129 /* first client doesn't have a current */
130 struct iovec vec
= { &reply
, sizeof(reply
) };
131 send_reply_v( get_initial_client_fd(), err
, -1, &vec
, 1 );
136 send_reply( current
, -1, 1, &reply
, sizeof(reply
) );
140 /* create a new thread */
141 DECL_HANDLER(init_thread
)
143 if (current
->state
!= STARTING
)
145 fatal_protocol_error( "init_thread: already running\n" );
148 current
->state
= RUNNING
;
149 current
->unix_pid
= req
->unix_pid
;
150 if (!(current
->name
= mem_alloc( len
+ 1 ))) goto done
;
151 memcpy( current
->name
, data
, len
);
152 current
->name
[len
] = '\0';
155 send_reply( current
, -1, 0 );
158 /* set the debug level */
159 DECL_HANDLER(set_debug
)
161 debug_level
= req
->level
;
162 /* Make sure last_req is initialized */
163 current
->last_req
= REQ_SET_DEBUG
;
165 send_reply( current
, -1, 0 );
168 /* terminate a process */
169 DECL_HANDLER(terminate_process
)
171 struct process
*process
;
173 if ((process
= get_process_from_handle( req
->handle
, PROCESS_TERMINATE
)))
175 kill_process( process
, req
->exit_code
);
176 release_object( process
);
178 if (current
) send_reply( current
, -1, 0 );
181 /* terminate a thread */
182 DECL_HANDLER(terminate_thread
)
184 struct thread
*thread
;
186 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_TERMINATE
)))
188 kill_thread( thread
, req
->exit_code
);
189 release_object( thread
);
191 if (current
) send_reply( current
, -1, 0 );
195 DECL_HANDLER(close_handle
)
197 close_handle( current
->process
, req
->handle
);
198 send_reply( current
, -1, 0 );
201 /* duplicate a handle */
202 DECL_HANDLER(dup_handle
)
204 struct dup_handle_reply reply
= { -1 };
205 struct process
*src
, *dst
;
207 if ((src
= get_process_from_handle( req
->src_process
, PROCESS_DUP_HANDLE
)))
209 if (req
->options
& DUP_HANDLE_MAKE_GLOBAL
)
211 reply
.handle
= duplicate_handle( src
, req
->src_handle
, NULL
, -1,
212 req
->access
, req
->inherit
, req
->options
);
214 else if ((dst
= get_process_from_handle( req
->dst_process
, PROCESS_DUP_HANDLE
)))
216 reply
.handle
= duplicate_handle( src
, req
->src_handle
, dst
, req
->dst_handle
,
217 req
->access
, req
->inherit
, req
->options
);
218 release_object( dst
);
220 /* close the handle no matter what happened */
221 if (req
->options
& DUP_HANDLE_CLOSE_SOURCE
)
222 close_handle( src
, req
->src_handle
);
223 release_object( src
);
225 send_reply( current
, -1, 1, &reply
, sizeof(reply
) );
228 /* fetch information about a process */
229 DECL_HANDLER(get_process_info
)
231 struct process
*process
;
232 struct get_process_info_reply reply
= { 0, 0, 0 };
234 if ((process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_INFORMATION
)))
236 get_process_info( process
, &reply
);
237 release_object( process
);
239 send_reply( current
, -1, 1, &reply
, sizeof(reply
) );
242 /* set information about a process */
243 DECL_HANDLER(set_process_info
)
245 struct process
*process
;
247 if ((process
= get_process_from_handle( req
->handle
, PROCESS_SET_INFORMATION
)))
249 set_process_info( process
, req
);
250 release_object( process
);
252 send_reply( current
, -1, 0 );
255 /* fetch information about a thread */
256 DECL_HANDLER(get_thread_info
)
258 struct thread
*thread
;
259 struct get_thread_info_reply reply
= { 0, 0 };
261 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_INFORMATION
)))
263 get_thread_info( thread
, &reply
);
264 release_object( thread
);
266 send_reply( current
, -1, 1, &reply
, sizeof(reply
) );
269 /* set information about a thread */
270 DECL_HANDLER(set_thread_info
)
272 struct thread
*thread
;
274 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SET_INFORMATION
)))
276 set_thread_info( thread
, req
);
277 release_object( thread
);
279 send_reply( current
, -1, 0 );
282 /* suspend a thread */
283 DECL_HANDLER(suspend_thread
)
285 struct thread
*thread
;
286 struct suspend_thread_reply reply
= { -1 };
287 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
289 reply
.count
= suspend_thread( thread
);
290 release_object( thread
);
292 send_reply( current
, -1, 1, &reply
, sizeof(reply
) );
296 /* resume a thread */
297 DECL_HANDLER(resume_thread
)
299 struct thread
*thread
;
300 struct resume_thread_reply reply
= { -1 };
301 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
303 reply
.count
= resume_thread( thread
);
304 release_object( thread
);
306 send_reply( current
, -1, 1, &reply
, sizeof(reply
) );
310 /* queue an APC for a thread */
311 DECL_HANDLER(queue_apc
)
313 struct thread
*thread
;
314 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SET_CONTEXT
)))
316 thread_queue_apc( thread
, req
->func
, req
->param
);
317 release_object( thread
);
319 send_reply( current
, -1, 0 );
322 /* open a handle to a process */
323 DECL_HANDLER(open_process
)
325 struct open_process_reply reply
= { -1 };
326 struct process
*process
= get_process_from_id( req
->pid
);
329 reply
.handle
= alloc_handle( current
->process
, process
,
330 req
->access
, req
->inherit
);
331 release_object( process
);
333 send_reply( current
, -1, 1, &reply
, sizeof(reply
) );
336 /* select on a handle list */
339 if (len
!= req
->count
* sizeof(int))
340 fatal_protocol_error( "select: bad length %d for %d handles\n",
342 sleep_on( current
, req
->count
, (int *)data
, req
->flags
, req
->timeout
);
345 /* create an event */
346 DECL_HANDLER(create_event
)
348 struct create_event_reply reply
= { -1 };
350 char *name
= (char *)data
;
351 if (!len
) name
= NULL
;
352 else CHECK_STRING( "create_event", name
, len
);
354 obj
= create_event( name
, req
->manual_reset
, req
->initial_state
);
357 reply
.handle
= alloc_handle( current
->process
, obj
, EVENT_ALL_ACCESS
, req
->inherit
);
358 release_object( obj
);
360 send_reply( current
, -1, 1, &reply
, sizeof(reply
) );
363 /* do an event operation */
364 DECL_HANDLER(event_op
)
369 pulse_event( req
->handle
);
372 set_event( req
->handle
);
375 reset_event( req
->handle
);
378 fatal_protocol_error( "event_op: invalid operation %d\n", req
->op
);
380 send_reply( current
, -1, 0 );
384 DECL_HANDLER(create_mutex
)
386 struct create_mutex_reply reply
= { -1 };
388 char *name
= (char *)data
;
389 if (!len
) name
= NULL
;
390 else CHECK_STRING( "create_mutex", name
, len
);
392 obj
= create_mutex( name
, req
->owned
);
395 reply
.handle
= alloc_handle( current
->process
, obj
, MUTEX_ALL_ACCESS
, req
->inherit
);
396 release_object( obj
);
398 send_reply( current
, -1, 1, &reply
, sizeof(reply
) );
401 /* release a mutex */
402 DECL_HANDLER(release_mutex
)
404 if (release_mutex( req
->handle
)) CLEAR_ERROR();
405 send_reply( current
, -1, 0 );
408 /* create a semaphore */
409 DECL_HANDLER(create_semaphore
)
411 struct create_semaphore_reply reply
= { -1 };
413 char *name
= (char *)data
;
414 if (!len
) name
= NULL
;
415 else CHECK_STRING( "create_semaphore", name
, len
);
417 obj
= create_semaphore( name
, req
->initial
, req
->max
);
420 reply
.handle
= alloc_handle( current
->process
, obj
, SEMAPHORE_ALL_ACCESS
, req
->inherit
);
421 release_object( obj
);
423 send_reply( current
, -1, 1, &reply
, sizeof(reply
) );
426 /* release a semaphore */
427 DECL_HANDLER(release_semaphore
)
429 struct release_semaphore_reply reply
;
430 if (release_semaphore( req
->handle
, req
->count
, &reply
.prev_count
)) CLEAR_ERROR();
431 send_reply( current
, -1, 1, &reply
, sizeof(reply
) );
434 /* open a handle to a named object (event, mutex, semaphore) */
435 DECL_HANDLER(open_named_obj
)
437 struct open_named_obj_reply reply
;
438 char *name
= (char *)data
;
439 if (!len
) name
= NULL
;
440 else CHECK_STRING( "open_named_obj", name
, len
);
445 reply
.handle
= open_event( req
->access
, req
->inherit
, name
);
448 reply
.handle
= open_mutex( req
->access
, req
->inherit
, name
);
451 reply
.handle
= open_semaphore( req
->access
, req
->inherit
, name
);
454 reply
.handle
= open_mapping( req
->access
, req
->inherit
, name
);
457 fatal_protocol_error( "open_named_obj: invalid type %d\n", req
->type
);
459 send_reply( current
, -1, 1, &reply
, sizeof(reply
) );
463 DECL_HANDLER(create_file
)
465 struct create_file_reply reply
= { -1 };
467 char *name
= (char *)data
;
468 if (!len
) name
= NULL
;
469 else CHECK_STRING( "create_file", name
, len
);
471 if ((obj
= create_file( fd
, name
, req
->access
,
472 req
->sharing
, req
->create
, req
->attrs
)) != NULL
)
474 reply
.handle
= alloc_handle( current
->process
, obj
, req
->access
, req
->inherit
);
475 release_object( obj
);
477 send_reply( current
, -1, 1, &reply
, sizeof(reply
) );
480 /* get a Unix fd to read from a file */
481 DECL_HANDLER(get_read_fd
)
486 if ((obj
= get_handle_obj( current
->process
, req
->handle
, GENERIC_READ
, NULL
)))
488 read_fd
= obj
->ops
->get_read_fd( obj
);
489 release_object( obj
);
492 send_reply( current
, read_fd
, 0 );
495 /* get a Unix fd to write to a file */
496 DECL_HANDLER(get_write_fd
)
501 if ((obj
= get_handle_obj( current
->process
, req
->handle
, GENERIC_WRITE
, NULL
)))
503 write_fd
= obj
->ops
->get_write_fd( obj
);
504 release_object( obj
);
507 send_reply( current
, write_fd
, 0 );
510 /* set a file current position */
511 DECL_HANDLER(set_file_pointer
)
513 struct set_file_pointer_reply reply
= { req
->low
, req
->high
};
514 set_file_pointer( req
->handle
, &reply
.low
, &reply
.high
, req
->whence
);
515 send_reply( current
, -1, 1, &reply
, sizeof(reply
) );
518 /* truncate (or extend) a file */
519 DECL_HANDLER(truncate_file
)
521 truncate_file( req
->handle
);
522 send_reply( current
, -1, 0 );
525 /* flush a file buffers */
526 DECL_HANDLER(flush_file
)
530 if ((obj
= get_handle_obj( current
->process
, req
->handle
, GENERIC_WRITE
, NULL
)))
532 obj
->ops
->flush( obj
);
533 release_object( obj
);
535 send_reply( current
, -1, 0 );
538 /* set a file access and modification times */
539 DECL_HANDLER(set_file_time
)
541 set_file_time( req
->handle
, req
->access_time
, req
->write_time
);
542 send_reply( current
, -1, 0 );
545 /* get a file information */
546 DECL_HANDLER(get_file_info
)
549 struct get_file_info_reply reply
;
551 if ((obj
= get_handle_obj( current
->process
, req
->handle
, 0, NULL
)))
553 obj
->ops
->get_file_info( obj
, &reply
);
554 release_object( obj
);
556 send_reply( current
, -1, 1, &reply
, sizeof(reply
) );
559 /* lock a region of a file */
560 DECL_HANDLER(lock_file
)
564 if ((file
= get_file_obj( current
->process
, req
->handle
, 0 )))
566 file_lock( file
, req
->offset_high
, req
->offset_low
,
567 req
->count_high
, req
->count_low
);
568 release_object( file
);
570 send_reply( current
, -1, 0 );
574 /* unlock a region of a file */
575 DECL_HANDLER(unlock_file
)
579 if ((file
= get_file_obj( current
->process
, req
->handle
, 0 )))
581 file_unlock( file
, req
->offset_high
, req
->offset_low
,
582 req
->count_high
, req
->count_low
);
583 release_object( file
);
585 send_reply( current
, -1, 0 );
589 /* create an anonymous pipe */
590 DECL_HANDLER(create_pipe
)
592 struct create_pipe_reply reply
= { -1, -1 };
593 struct object
*obj
[2];
594 if (create_pipe( obj
))
596 reply
.handle_read
= alloc_handle( current
->process
, obj
[0],
597 STANDARD_RIGHTS_REQUIRED
|SYNCHRONIZE
|GENERIC_READ
,
599 if (reply
.handle_read
!= -1)
601 reply
.handle_write
= alloc_handle( current
->process
, obj
[1],
602 STANDARD_RIGHTS_REQUIRED
|SYNCHRONIZE
|GENERIC_WRITE
,
604 if (reply
.handle_write
== -1)
605 close_handle( current
->process
, reply
.handle_read
);
607 release_object( obj
[0] );
608 release_object( obj
[1] );
610 send_reply( current
, -1, 1, &reply
, sizeof(reply
) );
613 /* allocate a console for the current process */
614 DECL_HANDLER(alloc_console
)
616 alloc_console( current
->process
);
617 send_reply( current
, -1, 0 );
620 /* free the console of the current process */
621 DECL_HANDLER(free_console
)
623 free_console( current
->process
);
624 send_reply( current
, -1, 0 );
627 /* open a handle to the process console */
628 DECL_HANDLER(open_console
)
631 struct open_console_reply reply
= { -1 };
632 if ((obj
= get_console( current
->process
, req
->output
)))
634 reply
.handle
= alloc_handle( current
->process
, obj
, req
->access
, req
->inherit
);
635 release_object( obj
);
637 send_reply( current
, -1, 1, &reply
, sizeof(reply
) );
640 /* set info about a console (output only) */
641 DECL_HANDLER(set_console_info
)
643 char *name
= (char *)data
;
644 if (!len
) name
= NULL
;
645 else CHECK_STRING( "set_console_info", name
, len
);
646 set_console_info( req
->handle
, req
, name
);
647 send_reply( current
, -1, 0 );
650 /* get info about a console (output only) */
651 DECL_HANDLER(get_console_info
)
653 struct get_console_info_reply reply
;
655 get_console_info( req
->handle
, &reply
, &title
);
656 send_reply( current
, -1, 2, &reply
, sizeof(reply
),
657 title
, title
? strlen(title
)+1 : 0 );
660 /* set a console fd */
661 DECL_HANDLER(set_console_fd
)
663 set_console_fd( req
->handle
, fd
, req
->pid
);
664 send_reply( current
, -1, 0 );
667 /* get a console mode (input or output) */
668 DECL_HANDLER(get_console_mode
)
670 struct get_console_mode_reply reply
;
671 get_console_mode( req
->handle
, &reply
.mode
);
672 send_reply( current
, -1, 1, &reply
, sizeof(reply
) );
675 /* set a console mode (input or output) */
676 DECL_HANDLER(set_console_mode
)
678 set_console_mode( req
->handle
, req
->mode
);
679 send_reply( current
, -1, 0 );
682 /* add input records to a console input queue */
683 DECL_HANDLER(write_console_input
)
685 struct write_console_input_reply reply
;
686 INPUT_RECORD
*records
= (INPUT_RECORD
*)data
;
688 if (len
!= req
->count
* sizeof(INPUT_RECORD
))
689 fatal_protocol_error( "write_console_input: bad length %d for %d records\n",
691 reply
.written
= write_console_input( req
->handle
, req
->count
, records
);
692 send_reply( current
, -1, 1, &reply
, sizeof(reply
) );
695 /* fetch input records from a console input queue */
696 DECL_HANDLER(read_console_input
)
698 read_console_input( req
->handle
, req
->count
, req
->flush
);
701 /* create a change notification */
702 DECL_HANDLER(create_change_notification
)
705 struct create_change_notification_reply reply
= { -1 };
707 if ((obj
= create_change_notification( req
->subtree
, req
->filter
)))
709 reply
.handle
= alloc_handle( current
->process
, obj
,
710 STANDARD_RIGHTS_REQUIRED
|SYNCHRONIZE
, 0 );
711 release_object( obj
);
713 send_reply( current
, -1, 1, &reply
, sizeof(reply
) );
716 /* create a file mapping */
717 DECL_HANDLER(create_mapping
)
720 struct create_mapping_reply reply
= { -1 };
721 char *name
= (char *)data
;
722 if (!len
) name
= NULL
;
723 else CHECK_STRING( "create_mapping", name
, len
);
725 if ((obj
= create_mapping( req
->size_high
, req
->size_low
,
726 req
->protect
, req
->handle
, name
)))
728 int access
= FILE_MAP_ALL_ACCESS
;
729 if (!(req
->protect
& VPROT_WRITE
)) access
&= ~FILE_MAP_WRITE
;
730 reply
.handle
= alloc_handle( current
->process
, obj
, access
, 0 );
731 release_object( obj
);
733 send_reply( current
, -1, 1, &reply
, sizeof(reply
) );
736 /* get a mapping information */
737 DECL_HANDLER(get_mapping_info
)
739 struct get_mapping_info_reply reply
;
740 int map_fd
= get_mapping_info( req
->handle
, &reply
);
741 send_reply( current
, map_fd
, 1, &reply
, sizeof(reply
) );
744 /* create a device */
745 DECL_HANDLER(create_device
)
748 struct create_device_reply reply
= { -1 };
750 if ((obj
= create_device( req
->id
)))
752 reply
.handle
= alloc_handle( current
->process
, obj
,
753 req
->access
, req
->inherit
);
754 release_object( obj
);
756 send_reply( current
, -1, 1, &reply
, sizeof(reply
) );
759 /* create a snapshot */
760 DECL_HANDLER(create_snapshot
)
763 struct create_snapshot_reply reply
= { -1 };
765 if ((obj
= create_snapshot( req
->flags
)))
767 reply
.handle
= alloc_handle( current
->process
, obj
, 0, req
->inherit
);
768 release_object( obj
);
770 send_reply( current
, -1, 1, &reply
, sizeof(reply
) );
773 /* get the next process from a snapshot */
774 DECL_HANDLER(next_process
)
776 struct next_process_reply reply
;
777 snapshot_next_process( req
->handle
, req
->reset
, &reply
);
778 send_reply( current
, -1, 1, &reply
, sizeof(reply
) );