2 * Server-side request handling
4 * Copyright (C) 1998 Alexandre Julliard
20 #include <sys/types.h>
21 #ifdef HAVE_SYS_SOCKET_H
22 # include <sys/socket.h>
34 #define WANT_REQUEST_HANDLERS
36 #include "wine/port.h"
38 /* Some versions of glibc don't define this */
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 */
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 */
65 NULL, /* get_poll_events */
66 master_socket_poll_event, /* poll_event */
67 no_get_fd, /* get_fd */
69 no_get_file_info, /* get_file_info */
70 master_socket_destroy /* destroy */
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 */
91 NULL, /* get_poll_events */
92 request_socket_poll_event, /* poll_event */
93 no_get_fd, /* get_fd */
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
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, ... )
124 va_start( args, err );
125 fprintf( stderr, "Protocol error:%p: ", thread );
126 vfprintf( stderr, err, 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, ... )
137 va_start( args, err );
138 fprintf( stderr, "Protocol error:%p: ", thread );
139 vfprintf( stderr, err, args );
142 thread->exit_code = 1;
143 kill_thread( thread, 1 );
146 /* die on a fatal error */
147 void fatal_error( const char *err, ... )
151 va_start( args, err );
152 fprintf( stderr, "wineserver: " );
153 vfprintf( stderr, err, args );
158 /* die on a fatal error */
159 void fatal_perror( const char *err, ... )
163 va_start( args, err );
164 fprintf( stderr, "wineserver: " );
165 vfprintf( stderr, err, args );
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;
179 if (debug_level) trace_request( thread, request );
181 if ((unsigned int)request->header.var_offset + request->header.var_size > MAX_REQUEST_LENGTH)
183 fatal_protocol_error( current, "bad request offset/size %d/%d\n",
184 request->header.var_offset, request->header.var_size );
186 else if (req < REQ_NB_REQUESTS)
188 req_handlers[req]( request );
189 if (current) send_reply( current, request );
193 fatal_protocol_error( current, "bad request %d\n", req );
196 /* send a reply to a thread */
197 void send_reply( struct thread *thread, union generic_request *request )
201 assert (thread->pass_fd == -1);
203 if (debug_level) trace_reply( thread, request );
205 request->header.error = thread->error;
207 if ((ret = write( thread->reply_fd, request, sizeof(*request) )) != sizeof(*request))
210 fatal_protocol_error( thread, "partial write %d\n", ret );
211 else if (errno == EPIPE)
212 kill_thread( thread, 0 ); /* normal death */
214 fatal_protocol_perror( thread, "sendmsg" );
218 /* read a message from a client that has something to say */
219 void read_request( struct thread *thread )
221 union generic_request req;
224 #ifdef HAVE_MSGHDR_ACCRIGHTS
225 msghdr.msg_accrightslen = sizeof(int);
226 msghdr.msg_accrights = (void *)&thread->pass_fd;
227 #else /* HAVE_MSGHDR_ACCRIGHTS */
228 msghdr.msg_control = &cmsg;
229 msghdr.msg_controllen = sizeof(cmsg);
231 #endif /* HAVE_MSGHDR_ACCRIGHTS */
233 assert( thread->pass_fd == -1 );
235 myiovec.iov_base = &req;
236 myiovec.iov_len = sizeof(req);
238 ret = recvmsg( thread->obj.fd, &msghdr, 0 );
239 #ifndef HAVE_MSGHDR_ACCRIGHTS
240 thread->pass_fd = cmsg.fd;
243 if (ret == sizeof(req))
245 call_req_handler( thread, &req );
246 thread->pass_fd = -1;
249 if (!ret) /* closed pipe */
250 kill_thread( thread, 0 );
252 fatal_protocol_error( thread, "partial recvmsg %d\n", ret );
254 fatal_protocol_perror( thread, "recvmsg" );
257 /* send the wakeup signal to a thread */
258 int send_thread_wakeup( struct thread *thread, int signaled )
260 int ret = write( thread->wait_fd, &signaled, sizeof(signaled) );
261 if (ret == sizeof(signaled)) return 0;
263 fatal_protocol_error( thread, "partial wakeup write %d\n", ret );
264 else if (errno == EPIPE)
265 kill_thread( thread, 0 ); /* normal death */
267 fatal_protocol_perror( thread, "write" );
271 /* send an fd to a client */
272 int send_client_fd( struct thread *thread, int fd, handle_t handle )
277 fprintf( stderr, "%08x: *fd* %d = %d\n", (unsigned int)thread, handle, fd );
279 #ifdef HAVE_MSGHDR_ACCRIGHTS
280 msghdr.msg_accrightslen = sizeof(fd);
281 msghdr.msg_accrights = (void *)&fd;
282 #else /* HAVE_MSGHDR_ACCRIGHTS */
283 msghdr.msg_control = &cmsg;
284 msghdr.msg_controllen = sizeof(cmsg);
286 #endif /* HAVE_MSGHDR_ACCRIGHTS */
288 myiovec.iov_base = (void *)&handle;
289 myiovec.iov_len = sizeof(handle);
291 ret = sendmsg( thread->obj.fd, &msghdr, 0 );
293 if (ret > 0) return 0;
296 kill_thread( thread, 0 ); /* normal death */
300 fatal_protocol_perror( thread, "sendmsg" );
305 static void master_socket_dump( struct object *obj, int verbose )
307 struct master_socket *sock = (struct master_socket *)obj;
308 assert( obj->ops == &master_socket_ops );
309 fprintf( stderr, "Master socket fd=%d\n", sock->obj.fd );
312 /* handle a socket event */
313 static void master_socket_poll_event( struct object *obj, int event )
315 struct master_socket *sock = (struct master_socket *)obj;
316 assert( obj->ops == &master_socket_ops );
318 assert( sock == master_socket ); /* there is only one master socket */
320 if (event & (POLLERR | POLLHUP))
322 /* this is not supposed to happen */
323 fprintf( stderr, "wineserver: Error on master socket\n" );
324 release_object( obj );
326 else if (event & POLLIN)
328 struct sockaddr_un dummy;
329 int len = sizeof(dummy);
330 int client = accept( master_socket->obj.fd, (struct sockaddr *) &dummy, &len );
331 if (client != -1) create_process( client );
335 /* remove the socket upon exit */
336 static void socket_cleanup(void)
338 static int do_it_once;
339 if (!do_it_once++) unlink( SOCKETNAME );
342 static void master_socket_destroy( struct object *obj )
347 static void request_socket_dump( struct object *obj, int verbose )
349 struct request_socket *sock = (struct request_socket *)obj;
350 assert( obj->ops == &request_socket_ops );
351 fprintf( stderr, "Request socket fd=%d thread=%p\n", sock->obj.fd, sock->thread );
354 /* handle a request socket event */
355 static void request_socket_poll_event( struct object *obj, int event )
357 struct request_socket *sock = (struct request_socket *)obj;
358 assert( obj->ops == &request_socket_ops );
360 if (event & (POLLERR | POLLHUP)) kill_thread( sock->thread, 0 );
361 else if (event & POLLIN)
363 struct thread *thread = sock->thread;
364 union generic_request req;
367 if ((ret = read( sock->obj.fd, &req, sizeof(req) )) == sizeof(req))
369 call_req_handler( thread, &req );
372 if (!ret) /* closed pipe */
373 kill_thread( thread, 0 );
375 fatal_protocol_error( thread, "partial read %d\n", ret );
377 fatal_protocol_perror( thread, "read" );
381 /* create a request socket and send the fd to the client thread */
382 struct object *create_request_socket( struct thread *thread )
384 struct request_socket *sock;
387 if (pipe( fd )) return NULL;
388 if (!(sock = alloc_object( &request_socket_ops, fd[0] )))
393 sock->thread = thread;
394 send_client_fd( thread, fd[1], 0 );
396 fcntl( fd[0], F_SETFL, O_NONBLOCK );
397 set_select_events( &sock->obj, POLLIN );
401 /* return the configuration directory ($WINEPREFIX or $HOME/.wine) */
402 const char *get_config_dir(void)
404 static char *confdir;
407 const char *prefix = getenv( "WINEPREFIX" );
410 int len = strlen(prefix);
411 if (!(confdir = strdup( prefix ))) fatal_error( "out of memory\n" );
412 if (len > 1 && confdir[len-1] == '/') confdir[len-1] = 0;
416 const char *home = getenv( "HOME" );
419 struct passwd *pwd = getpwuid( getuid() );
420 if (!pwd) fatal_error( "could not find your home directory\n" );
423 if (!(confdir = malloc( strlen(home) + strlen(CONFDIR) + 1 )))
424 fatal_error( "out of memory\n" );
425 strcpy( confdir, home );
426 strcat( confdir, CONFDIR );
428 mkdir( confdir, 0755 ); /* just in case */
433 /* create the server directory and chdir to it */
434 static void create_server_dir(void)
438 const char *confdir = get_config_dir();
441 if (gethostname( hostname, sizeof(hostname) ) == -1) fatal_perror( "gethostname" );
443 if (!(serverdir = malloc( strlen(confdir) + strlen(SERVERDIR) + strlen(hostname) + 1 )))
444 fatal_error( "out of memory\n" );
446 strcpy( serverdir, confdir );
447 strcat( serverdir, SERVERDIR );
448 strcat( serverdir, hostname );
450 if (chdir( serverdir ) == -1)
452 if (errno != ENOENT) fatal_perror( "chdir %s", serverdir );
453 if (mkdir( serverdir, 0700 ) == -1) fatal_perror( "mkdir %s", serverdir );
454 if (chdir( serverdir ) == -1) fatal_perror( "chdir %s", serverdir );
456 if (stat( ".", &st ) == -1) fatal_perror( "stat %s", serverdir );
457 if (!S_ISDIR(st.st_mode)) fatal_error( "%s is not a directory\n", serverdir );
458 if (st.st_uid != getuid()) fatal_error( "%s is not owned by you\n", serverdir );
459 if (st.st_mode & 077) fatal_error( "%s must not be accessible by other users\n", serverdir );
462 /* open the master server socket and start waiting for new clients */
463 void open_master_socket(void)
465 struct sockaddr_un addr;
468 /* make sure no request is larger than the maximum size */
469 assert( sizeof(union generic_request) == sizeof(struct request_max_size) );
472 if ((fd = socket( AF_UNIX, SOCK_STREAM, 0 )) == -1) fatal_perror( "socket" );
473 addr.sun_family = AF_UNIX;
474 strcpy( addr.sun_path, SOCKETNAME );
475 slen = sizeof(addr) - sizeof(addr.sun_path) + strlen(addr.sun_path) + 1;
476 #ifdef HAVE_SOCKADDR_SUN_LEN
479 if (bind( fd, (struct sockaddr *)&addr, slen ) == -1)
481 if ((errno == EEXIST) || (errno == EADDRINUSE))
482 exit(0); /* pretend we succeeded to start */
484 fatal_perror( "bind" );
486 atexit( socket_cleanup );
488 chmod( SOCKETNAME, 0600 ); /* make sure no other user can connect */
489 if (listen( fd, 5 ) == -1) fatal_perror( "listen" );
491 if (!(master_socket = alloc_object( &master_socket_ops, fd )))
492 fatal_error( "out of memory\n" );
493 set_select_events( &master_socket->obj, POLLIN );
495 /* setup msghdr structure constant fields */
496 msghdr.msg_name = NULL;
497 msghdr.msg_namelen = 0;
498 msghdr.msg_iov = &myiovec;
499 msghdr.msg_iovlen = 1;
501 /* go in the background */
505 fatal_perror( "fork" );
510 _exit(0); /* do not call atexit functions */
514 /* close the master socket and stop waiting for new clients */
515 void close_master_socket(void)
517 /* if a new client is waiting, we keep on running */
518 if (!check_select_events( master_socket->obj.fd, POLLIN ))
519 release_object( master_socket );
522 /* lock/unlock the master socket to stop accepting new clients */
523 void lock_master_socket( int locked )
525 set_select_events( &master_socket->obj, locked ? 0 : POLLIN );