New mechanism to transfer file descriptors from client to server.
[wine/multimedia.git] / server / request.c
blob96bb71114714bd20228d83672290ea9fd159e4cc
1 /*
2 * Server-side request handling
4 * Copyright (C) 1998 Alexandre Julliard
5 */
7 #include "config.h"
9 #include <assert.h>
10 #include <errno.h>
11 #include <fcntl.h>
12 #include <pwd.h>
13 #include <signal.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <stdarg.h>
17 #include <string.h>
18 #include <sys/stat.h>
19 #include <sys/time.h>
20 #include <sys/types.h>
21 #ifdef HAVE_SYS_SOCKET_H
22 # include <sys/socket.h>
23 #endif
24 #include <sys/uio.h>
25 #include <sys/un.h>
26 #include <unistd.h>
28 #include "winnt.h"
29 #include "winbase.h"
30 #include "wincon.h"
31 #include "thread.h"
32 #include "process.h"
33 #include "server.h"
34 #define WANT_REQUEST_HANDLERS
35 #include "request.h"
36 #include "wine/port.h"
38 /* Some versions of glibc don't define this */
39 #ifndef SCM_RIGHTS
40 #define SCM_RIGHTS 1
41 #endif
43 /* path names for server master Unix socket */
44 #define CONFDIR "/.wine" /* directory for Wine config relative to $HOME */
45 #define SERVERDIR "/wineserver-" /* server socket directory (hostname appended) */
46 #define SOCKETNAME "socket" /* name of the socket file */
48 struct master_socket
50 struct object obj; /* object header */
53 static void master_socket_dump( struct object *obj, int verbose );
54 static void master_socket_poll_event( struct object *obj, int event );
55 static void master_socket_destroy( struct object *obj );
57 static const struct object_ops master_socket_ops =
59 sizeof(struct master_socket), /* size */
60 master_socket_dump, /* dump */
61 no_add_queue, /* add_queue */
62 NULL, /* remove_queue */
63 NULL, /* signaled */
64 NULL, /* satisfied */
65 NULL, /* get_poll_events */
66 master_socket_poll_event, /* poll_event */
67 no_get_fd, /* get_fd */
68 no_flush, /* flush */
69 no_get_file_info, /* get_file_info */
70 master_socket_destroy /* destroy */
74 struct request_socket
76 struct object obj; /* object header */
77 struct thread *thread; /* owning thread */
80 static void request_socket_dump( struct object *obj, int verbose );
81 static void request_socket_poll_event( struct object *obj, int event );
83 static const struct object_ops request_socket_ops =
85 sizeof(struct request_socket), /* size */
86 request_socket_dump, /* dump */
87 no_add_queue, /* add_queue */
88 NULL, /* remove_queue */
89 NULL, /* signaled */
90 NULL, /* satisfied */
91 NULL, /* get_poll_events */
92 request_socket_poll_event, /* poll_event */
93 no_get_fd, /* get_fd */
94 no_flush, /* flush */
95 no_get_file_info, /* get_file_info */
96 no_destroy /* destroy */
100 struct thread *current = NULL; /* thread handling the current request */
101 unsigned int global_error = 0; /* global error code for when no thread is current */
103 static struct master_socket *master_socket; /* the master socket object */
105 /* socket communication static structures */
106 static struct iovec myiovec;
107 static struct msghdr msghdr;
108 #ifndef HAVE_MSGHDR_ACCRIGHTS
109 struct cmsg_fd
111 int len; /* sizeof structure */
112 int level; /* SOL_SOCKET */
113 int type; /* SCM_RIGHTS */
114 int fd; /* fd to pass */
116 static struct cmsg_fd cmsg = { sizeof(cmsg), SOL_SOCKET, SCM_RIGHTS, -1 };
117 #endif /* HAVE_MSGHDR_ACCRIGHTS */
119 /* complain about a protocol error and terminate the client connection */
120 void fatal_protocol_error( struct thread *thread, const char *err, ... )
122 va_list args;
124 va_start( args, err );
125 fprintf( stderr, "Protocol error:%p: ", thread );
126 vfprintf( stderr, err, args );
127 va_end( args );
128 thread->exit_code = 1;
129 kill_thread( thread, 1 );
132 /* complain about a protocol error and terminate the client connection */
133 void fatal_protocol_perror( struct thread *thread, const char *err, ... )
135 va_list args;
137 va_start( args, err );
138 fprintf( stderr, "Protocol error:%p: ", thread );
139 vfprintf( stderr, err, args );
140 perror( " " );
141 va_end( args );
142 thread->exit_code = 1;
143 kill_thread( thread, 1 );
146 /* die on a fatal error */
147 void fatal_error( const char *err, ... )
149 va_list args;
151 va_start( args, err );
152 fprintf( stderr, "wineserver: " );
153 vfprintf( stderr, err, args );
154 va_end( args );
155 exit(1);
158 /* die on a fatal error */
159 void fatal_perror( const char *err, ... )
161 va_list args;
163 va_start( args, err );
164 fprintf( stderr, "wineserver: " );
165 vfprintf( stderr, err, args );
166 perror( " " );
167 va_end( args );
168 exit(1);
171 /* call a request handler */
172 static inline void call_req_handler( struct thread *thread, union generic_request *request )
174 enum request req = request->header.req;
176 current = thread;
177 clear_error();
179 if (debug_level) trace_request( thread, request );
181 if (request->header.var_size)
183 if ((unsigned int)request->header.var_offset +
184 request->header.var_size > MAX_REQUEST_LENGTH)
186 fatal_protocol_error( current, "bad request offset/size %d/%d\n",
187 request->header.var_offset, request->header.var_size );
188 return;
192 if (req < REQ_NB_REQUESTS)
194 req_handlers[req]( request );
195 if (current) send_reply( current, request );
196 current = NULL;
197 return;
199 fatal_protocol_error( current, "bad request %d\n", req );
202 /* send a reply to a thread */
203 void send_reply( struct thread *thread, union generic_request *request )
205 int ret;
207 if (debug_level) trace_reply( thread, request );
209 request->header.error = thread->error;
211 if ((ret = write( thread->reply_fd, request, sizeof(*request) )) != sizeof(*request))
213 if (ret >= 0)
214 fatal_protocol_error( thread, "partial write %d\n", ret );
215 else if (errno == EPIPE)
216 kill_thread( thread, 0 ); /* normal death */
217 else
218 fatal_protocol_perror( thread, "sendmsg" );
222 /* receive a file descriptor on the process socket */
223 int receive_fd( struct process *process )
225 struct send_fd data;
226 int fd, ret;
228 #ifdef HAVE_MSGHDR_ACCRIGHTS
229 msghdr.msg_accrightslen = sizeof(int);
230 msghdr.msg_accrights = (void *)&fd;
231 #else /* HAVE_MSGHDR_ACCRIGHTS */
232 msghdr.msg_control = &cmsg;
233 msghdr.msg_controllen = sizeof(cmsg);
234 cmsg.fd = -1;
235 #endif /* HAVE_MSGHDR_ACCRIGHTS */
237 myiovec.iov_base = &data;
238 myiovec.iov_len = sizeof(data);
240 ret = recvmsg( process->obj.fd, &msghdr, 0 );
241 #ifndef HAVE_MSGHDR_ACCRIGHTS
242 fd = cmsg.fd;
243 #endif
245 if (ret == sizeof(data))
247 struct thread *thread = get_thread_from_id( data.tid );
248 if (!thread || thread->process != process)
250 if (debug_level)
251 fprintf( stderr, "%08x: *fd* %d <- %d bad thread id\n",
252 (unsigned int)data.tid, data.fd, fd );
253 close( fd );
255 else
257 if (debug_level)
258 fprintf( stderr, "%08x: *fd* %d <- %d\n",
259 (unsigned int)thread, data.fd, fd );
260 thread_add_inflight_fd( thread, data.fd, fd );
262 return 0;
265 if (!ret)
267 set_select_events( &process->obj, -1 ); /* stop waiting on it */
269 else if (ret > 0)
271 fprintf( stderr, "Protocol error: process %p: partial recvmsg %d for fd\n", process, ret );
272 kill_process( process, NULL, 1 );
274 else if (ret < 0)
276 if (errno != EWOULDBLOCK && errno != EAGAIN)
278 fprintf( stderr, "Protocol error: process %p: ", process );
279 perror( "recvmsg" );
280 kill_process( process, NULL, 1 );
283 return -1;
286 /* send the wakeup signal to a thread */
287 int send_thread_wakeup( struct thread *thread, int signaled )
289 int ret = write( thread->wait_fd, &signaled, sizeof(signaled) );
290 if (ret == sizeof(signaled)) return 0;
291 if (ret >= 0)
292 fatal_protocol_error( thread, "partial wakeup write %d\n", ret );
293 else if (errno == EPIPE)
294 kill_thread( thread, 0 ); /* normal death */
295 else
296 fatal_protocol_perror( thread, "write" );
297 return -1;
300 /* send an fd to a client */
301 int send_client_fd( struct thread *thread, int fd, handle_t handle )
303 int ret;
305 if (debug_level)
306 fprintf( stderr, "%08x: *fd* %d -> %d\n", (unsigned int)thread, handle, fd );
308 #ifdef HAVE_MSGHDR_ACCRIGHTS
309 msghdr.msg_accrightslen = sizeof(fd);
310 msghdr.msg_accrights = (void *)&fd;
311 #else /* HAVE_MSGHDR_ACCRIGHTS */
312 msghdr.msg_control = &cmsg;
313 msghdr.msg_controllen = sizeof(cmsg);
314 cmsg.fd = fd;
315 #endif /* HAVE_MSGHDR_ACCRIGHTS */
317 myiovec.iov_base = (void *)&handle;
318 myiovec.iov_len = sizeof(handle);
320 ret = sendmsg( thread->obj.fd, &msghdr, 0 );
322 if (ret > 0) return 0;
323 if (errno == EPIPE)
325 kill_thread( thread, 0 ); /* normal death */
327 else
329 fatal_protocol_perror( thread, "sendmsg" );
331 return -1;
334 static void master_socket_dump( struct object *obj, int verbose )
336 struct master_socket *sock = (struct master_socket *)obj;
337 assert( obj->ops == &master_socket_ops );
338 fprintf( stderr, "Master socket fd=%d\n", sock->obj.fd );
341 /* handle a socket event */
342 static void master_socket_poll_event( struct object *obj, int event )
344 struct master_socket *sock = (struct master_socket *)obj;
345 assert( obj->ops == &master_socket_ops );
347 assert( sock == master_socket ); /* there is only one master socket */
349 if (event & (POLLERR | POLLHUP))
351 /* this is not supposed to happen */
352 fprintf( stderr, "wineserver: Error on master socket\n" );
353 release_object( obj );
355 else if (event & POLLIN)
357 struct sockaddr_un dummy;
358 int len = sizeof(dummy);
359 int client = accept( master_socket->obj.fd, (struct sockaddr *) &dummy, &len );
360 if (client != -1) create_process( client );
364 /* remove the socket upon exit */
365 static void socket_cleanup(void)
367 static int do_it_once;
368 if (!do_it_once++) unlink( SOCKETNAME );
371 static void master_socket_destroy( struct object *obj )
373 socket_cleanup();
376 static void request_socket_dump( struct object *obj, int verbose )
378 struct request_socket *sock = (struct request_socket *)obj;
379 assert( obj->ops == &request_socket_ops );
380 fprintf( stderr, "Request socket fd=%d thread=%p\n", sock->obj.fd, sock->thread );
383 /* handle a request socket event */
384 static void request_socket_poll_event( struct object *obj, int event )
386 struct request_socket *sock = (struct request_socket *)obj;
387 assert( obj->ops == &request_socket_ops );
389 if (event & (POLLERR | POLLHUP)) kill_thread( sock->thread, 0 );
390 else if (event & POLLIN)
392 struct thread *thread = sock->thread;
393 union generic_request req;
394 int ret;
396 if ((ret = read( sock->obj.fd, &req, sizeof(req) )) == sizeof(req))
398 call_req_handler( thread, &req );
399 return;
401 if (!ret) /* closed pipe */
402 kill_thread( thread, 0 );
403 else if (ret > 0)
404 fatal_protocol_error( thread, "partial read %d\n", ret );
405 else
406 fatal_protocol_perror( thread, "read" );
410 /* create a request socket and send the fd to the client thread */
411 struct object *create_request_socket( struct thread *thread )
413 struct request_socket *sock;
414 int fd[2];
416 if (pipe( fd )) return NULL;
417 if (!(sock = alloc_object( &request_socket_ops, fd[0] )))
419 close( fd[1] );
420 return NULL;
422 sock->thread = thread;
423 send_client_fd( thread, fd[1], 0 );
424 close( fd[1] );
425 fcntl( fd[0], F_SETFL, O_NONBLOCK );
426 set_select_events( &sock->obj, POLLIN );
427 return &sock->obj;
430 /* return the configuration directory ($WINEPREFIX or $HOME/.wine) */
431 const char *get_config_dir(void)
433 static char *confdir;
434 if (!confdir)
436 const char *prefix = getenv( "WINEPREFIX" );
437 if (prefix)
439 int len = strlen(prefix);
440 if (!(confdir = strdup( prefix ))) fatal_error( "out of memory\n" );
441 if (len > 1 && confdir[len-1] == '/') confdir[len-1] = 0;
443 else
445 const char *home = getenv( "HOME" );
446 if (!home)
448 struct passwd *pwd = getpwuid( getuid() );
449 if (!pwd) fatal_error( "could not find your home directory\n" );
450 home = pwd->pw_dir;
452 if (!(confdir = malloc( strlen(home) + strlen(CONFDIR) + 1 )))
453 fatal_error( "out of memory\n" );
454 strcpy( confdir, home );
455 strcat( confdir, CONFDIR );
457 mkdir( confdir, 0755 ); /* just in case */
459 return confdir;
462 /* create the server directory and chdir to it */
463 static void create_server_dir(void)
465 char hostname[64];
466 char *serverdir;
467 const char *confdir = get_config_dir();
468 struct stat st;
470 if (gethostname( hostname, sizeof(hostname) ) == -1) fatal_perror( "gethostname" );
472 if (!(serverdir = malloc( strlen(confdir) + strlen(SERVERDIR) + strlen(hostname) + 1 )))
473 fatal_error( "out of memory\n" );
475 strcpy( serverdir, confdir );
476 strcat( serverdir, SERVERDIR );
477 strcat( serverdir, hostname );
479 if (chdir( serverdir ) == -1)
481 if (errno != ENOENT) fatal_perror( "chdir %s", serverdir );
482 if (mkdir( serverdir, 0700 ) == -1) fatal_perror( "mkdir %s", serverdir );
483 if (chdir( serverdir ) == -1) fatal_perror( "chdir %s", serverdir );
485 if (stat( ".", &st ) == -1) fatal_perror( "stat %s", serverdir );
486 if (!S_ISDIR(st.st_mode)) fatal_error( "%s is not a directory\n", serverdir );
487 if (st.st_uid != getuid()) fatal_error( "%s is not owned by you\n", serverdir );
488 if (st.st_mode & 077) fatal_error( "%s must not be accessible by other users\n", serverdir );
491 /* open the master server socket and start waiting for new clients */
492 void open_master_socket(void)
494 struct sockaddr_un addr;
495 int fd, slen;
497 /* make sure no request is larger than the maximum size */
498 assert( sizeof(union generic_request) == sizeof(struct request_max_size) );
500 create_server_dir();
501 if ((fd = socket( AF_UNIX, SOCK_STREAM, 0 )) == -1) fatal_perror( "socket" );
502 addr.sun_family = AF_UNIX;
503 strcpy( addr.sun_path, SOCKETNAME );
504 slen = sizeof(addr) - sizeof(addr.sun_path) + strlen(addr.sun_path) + 1;
505 #ifdef HAVE_SOCKADDR_SUN_LEN
506 addr.sun_len = slen;
507 #endif
508 if (bind( fd, (struct sockaddr *)&addr, slen ) == -1)
510 if ((errno == EEXIST) || (errno == EADDRINUSE))
511 exit(0); /* pretend we succeeded to start */
512 else
513 fatal_perror( "bind" );
515 atexit( socket_cleanup );
517 chmod( SOCKETNAME, 0600 ); /* make sure no other user can connect */
518 if (listen( fd, 5 ) == -1) fatal_perror( "listen" );
520 if (!(master_socket = alloc_object( &master_socket_ops, fd )))
521 fatal_error( "out of memory\n" );
522 set_select_events( &master_socket->obj, POLLIN );
524 /* setup msghdr structure constant fields */
525 msghdr.msg_name = NULL;
526 msghdr.msg_namelen = 0;
527 msghdr.msg_iov = &myiovec;
528 msghdr.msg_iovlen = 1;
530 /* go in the background */
531 switch(fork())
533 case -1:
534 fatal_perror( "fork" );
535 case 0:
536 setsid();
537 break;
538 default:
539 _exit(0); /* do not call atexit functions */
543 /* close the master socket and stop waiting for new clients */
544 void close_master_socket(void)
546 /* if a new client is waiting, we keep on running */
547 if (!check_select_events( master_socket->obj.fd, POLLIN ))
548 release_object( master_socket );
551 /* lock/unlock the master socket to stop accepting new clients */
552 void lock_master_socket( int locked )
554 set_select_events( &master_socket->obj, locked ? 0 : POLLIN );