2 * Server-side request handling
4 * Copyright (C) 1998 Alexandre Julliard
10 #include <sys/types.h>
17 #define WANT_REQUEST_HANDLERS
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) ); \
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, ... )
35 va_start( args, err );
36 fprintf( stderr, "Protocol error:%p: ", current );
37 vfprintf( stderr, err, 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];
50 if ((req < 0) || (req >= REQ_NB_REQUESTS))
52 fatal_protocol_error( "unknown request %d\n", req );
56 if (len < handler->min_size)
58 fatal_protocol_error( "req %d bad length %d < %d)\n", req, len, handler->min_size );
62 /* now call the handler */
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 );
74 /* handle a client timeout (unused for now) */
75 void call_timeout_handler( struct thread *thread )
78 if (debug_level) trace_timeout();
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;
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;
106 if ((new_fd = dup(fd)) == -1)
109 err = ERROR_TOO_MANY_OPEN_FILES;
112 if (!(new_thread = create_thread( new_fd, req->pid, &reply.thandle,
116 err = ERROR_OUTOFMEMORY;
119 reply.tid = new_thread;
120 reply.pid = new_thread->process;
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 );
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" );
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';
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 );
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 );
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 */
255 if (len != req->count * sizeof(int))
256 fatal_protocol_error( "select: bad length %d for %d handles\n",
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 };
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 );
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)
285 pulse_event( req->handle );
288 set_event( req->handle );
291 reset_event( req->handle );
294 fatal_protocol_error( "event_op: invalid operation %d\n", req->op );
296 send_reply( current, -1, 0 );
300 DECL_HANDLER(create_mutex)
302 struct create_mutex_reply reply = { -1 };
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 );
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 };
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 );
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 );
361 reply.handle = open_event( req->access, req->inherit, name );
364 reply.handle = open_mutex( req->access, req->inherit, name );
367 reply.handle = open_semaphore( req->access, req->inherit, name );
370 fatal_protocol_error( "open_named_obj: invalid type %d\n", req->type );
372 send_reply( current, -1, 1, &reply, sizeof(reply) );