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>
33 #define WANT_REQUEST_HANDLERS
35 #include "wine/port.h"
37 /* Some versions of glibc don't define this */
42 /* path names for server master Unix socket */
43 #define CONFDIR "/.wine" /* directory for Wine config relative to $HOME */
44 #define SERVERDIR "wineserver-" /* server socket directory (hostname appended) */
45 #define SOCKETNAME "socket" /* name of the socket file */
49 struct object obj; /* object header */
52 static void master_socket_dump( struct object *obj, int verbose );
53 static void master_socket_poll_event( struct object *obj, int event );
54 static void master_socket_destroy( struct object *obj );
56 static const struct object_ops master_socket_ops =
58 sizeof(struct master_socket), /* size */
59 master_socket_dump, /* dump */
60 no_add_queue, /* add_queue */
61 NULL, /* remove_queue */
64 NULL, /* get_poll_events */
65 master_socket_poll_event, /* poll_event */
66 no_get_fd, /* get_fd */
68 no_get_file_info, /* get_file_info */
69 master_socket_destroy /* destroy */
73 struct thread *current = NULL; /* thread handling the current request */
74 unsigned int global_error = 0; /* global error code for when no thread is current */
76 static struct master_socket *master_socket; /* the master socket object */
78 /* socket communication static structures */
79 static struct iovec myiovec;
80 static struct msghdr msghdr;
81 #ifndef HAVE_MSGHDR_ACCRIGHTS
84 int len; /* sizeof structure */
85 int level; /* SOL_SOCKET */
86 int type; /* SCM_RIGHTS */
87 int fd; /* fd to pass */
89 static struct cmsg_fd cmsg = { sizeof(cmsg), SOL_SOCKET, SCM_RIGHTS, -1 };
90 #endif /* HAVE_MSGHDR_ACCRIGHTS */
92 /* complain about a protocol error and terminate the client connection */
93 void fatal_protocol_error( struct thread *thread, const char *err, ... )
97 va_start( args, err );
98 fprintf( stderr, "Protocol error:%p: ", thread );
99 vfprintf( stderr, err, args );
101 thread->exit_code = 1;
102 kill_thread( thread, 1 );
105 /* complain about a protocol error and terminate the client connection */
106 void fatal_protocol_perror( struct thread *thread, const char *err, ... )
110 va_start( args, err );
111 fprintf( stderr, "Protocol error:%p: ", thread );
112 vfprintf( stderr, err, args );
115 thread->exit_code = 1;
116 kill_thread( thread, 1 );
119 /* die on a fatal error */
120 void fatal_error( const char *err, ... )
124 va_start( args, err );
125 fprintf( stderr, "wineserver: " );
126 vfprintf( stderr, err, args );
131 /* die on a fatal error */
132 void fatal_perror( const char *err, ... )
136 va_start( args, err );
137 fprintf( stderr, "wineserver: " );
138 vfprintf( stderr, err, args );
144 /* call a request handler */
145 static inline void call_req_handler( struct thread *thread, union generic_request *request )
147 enum request req = request->header.req;
152 if (debug_level) trace_request( thread, request );
154 if (request->header.var_size)
156 if ((unsigned int)request->header.var_offset +
157 request->header.var_size > MAX_REQUEST_LENGTH)
159 fatal_protocol_error( current, "bad request offset/size %d/%d\n",
160 request->header.var_offset, request->header.var_size );
165 if (req < REQ_NB_REQUESTS)
167 req_handlers[req]( request );
168 if (current) send_reply( current, request );
172 fatal_protocol_error( current, "bad request %d\n", req );
175 /* read a request from a thread */
176 void read_request( struct thread *thread )
178 union generic_request req;
181 if ((ret = read( thread->obj.fd, &req, sizeof(req) )) == sizeof(req))
183 call_req_handler( thread, &req );
186 if (!ret) /* closed pipe */
187 kill_thread( thread, 0 );
189 fatal_protocol_error( thread, "partial read %d\n", ret );
190 else if (errno != EWOULDBLOCK && errno != EAGAIN)
191 fatal_protocol_perror( thread, "read" );
194 /* send a reply to a thread */
195 void send_reply( struct thread *thread, union generic_request *request )
199 if (debug_level) trace_reply( thread, request );
201 request->header.error = thread->error;
203 if ((ret = write( thread->reply_fd, request, sizeof(*request) )) != sizeof(*request))
206 fatal_protocol_error( thread, "partial write %d\n", ret );
207 else if (errno == EPIPE)
208 kill_thread( thread, 0 ); /* normal death */
210 fatal_protocol_perror( thread, "reply write" );
214 /* receive a file descriptor on the process socket */
215 int receive_fd( struct process *process )
220 #ifdef HAVE_MSGHDR_ACCRIGHTS
221 msghdr.msg_accrightslen = sizeof(int);
222 msghdr.msg_accrights = (void *)&fd;
223 #else /* HAVE_MSGHDR_ACCRIGHTS */
224 msghdr.msg_control = &cmsg;
225 msghdr.msg_controllen = sizeof(cmsg);
227 #endif /* HAVE_MSGHDR_ACCRIGHTS */
229 myiovec.iov_base = &data;
230 myiovec.iov_len = sizeof(data);
232 ret = recvmsg( process->obj.fd, &msghdr, 0 );
233 #ifndef HAVE_MSGHDR_ACCRIGHTS
237 if (ret == sizeof(data))
239 struct thread *thread;
241 if (data.tid) thread = get_thread_from_id( data.tid );
242 else thread = (struct thread *)grab_object( process->thread_list );
244 if (!thread || thread->process != process)
247 fprintf( stderr, "%08x: *fd* %d <- %d bad thread id\n",
248 (unsigned int)data.tid, data.fd, fd );
254 fprintf( stderr, "%08x: *fd* %d <- %d\n",
255 (unsigned int)thread, data.fd, fd );
256 thread_add_inflight_fd( thread, data.fd, fd );
258 if (thread) release_object( thread );
265 fprintf( stderr, "Protocol error: process %p: partial recvmsg %d for fd\n",
267 kill_process( process, NULL, 1 );
271 if (errno != EWOULDBLOCK && errno != EAGAIN)
273 fprintf( stderr, "Protocol error: process %p: ", process );
275 kill_process( process, NULL, 1 );
281 /* send an fd to a client */
282 int send_client_fd( struct process *process, int fd, handle_t handle )
287 fprintf( stderr, "%08x: *fd* %d -> %d\n", (unsigned int)current, handle, fd );
289 #ifdef HAVE_MSGHDR_ACCRIGHTS
290 msghdr.msg_accrightslen = sizeof(fd);
291 msghdr.msg_accrights = (void *)&fd;
292 #else /* HAVE_MSGHDR_ACCRIGHTS */
293 msghdr.msg_control = &cmsg;
294 msghdr.msg_controllen = sizeof(cmsg);
296 #endif /* HAVE_MSGHDR_ACCRIGHTS */
298 myiovec.iov_base = (void *)&handle;
299 myiovec.iov_len = sizeof(handle);
301 ret = sendmsg( process->obj.fd, &msghdr, 0 );
303 if (ret == sizeof(handle)) return 0;
308 fprintf( stderr, "Protocol error: process %p: partial sendmsg %d\n", process, ret );
309 kill_process( process, NULL, 1 );
313 fprintf( stderr, "Protocol error: process %p: ", process );
315 kill_process( process, NULL, 1 );
320 static void master_socket_dump( struct object *obj, int verbose )
322 struct master_socket *sock = (struct master_socket *)obj;
323 assert( obj->ops == &master_socket_ops );
324 fprintf( stderr, "Master socket fd=%d\n", sock->obj.fd );
327 /* handle a socket event */
328 static void master_socket_poll_event( struct object *obj, int event )
330 struct master_socket *sock = (struct master_socket *)obj;
331 assert( obj->ops == &master_socket_ops );
333 assert( sock == master_socket ); /* there is only one master socket */
335 if (event & (POLLERR | POLLHUP))
337 /* this is not supposed to happen */
338 fprintf( stderr, "wineserver: Error on master socket\n" );
339 release_object( obj );
341 else if (event & POLLIN)
343 struct sockaddr_un dummy;
344 int len = sizeof(dummy);
345 int client = accept( master_socket->obj.fd, (struct sockaddr *) &dummy, &len );
346 if (client == -1) return;
347 fcntl( client, F_SETFL, O_NONBLOCK );
348 create_process( client );
352 /* remove the socket upon exit */
353 static void socket_cleanup(void)
355 static int do_it_once;
356 if (!do_it_once++) unlink( SOCKETNAME );
359 static void master_socket_destroy( struct object *obj )
364 /* return the configuration directory ($WINEPREFIX or $HOME/.wine) */
365 const char *get_config_dir(void)
367 static char *confdir;
370 const char *prefix = getenv( "WINEPREFIX" );
373 int len = strlen(prefix);
374 if (!(confdir = strdup( prefix ))) fatal_error( "out of memory\n" );
375 if (len > 1 && confdir[len-1] == '/') confdir[len-1] = 0;
379 const char *home = getenv( "HOME" );
382 struct passwd *pwd = getpwuid( getuid() );
383 if (!pwd) fatal_error( "could not find your home directory\n" );
386 if (!(confdir = malloc( strlen(home) + strlen(CONFDIR) + 1 )))
387 fatal_error( "out of memory\n" );
388 strcpy( confdir, home );
389 strcat( confdir, CONFDIR );
395 /* create the server directory and chdir to it */
396 static void create_server_dir(void)
400 const char *confdir = get_config_dir();
403 if (gethostname( hostname, sizeof(hostname) ) == -1) fatal_perror( "gethostname" );
405 if (!(serverdir = malloc( strlen(SERVERDIR) + strlen(hostname) + 1 )))
406 fatal_error( "out of memory\n" );
408 if (chdir( confdir ) == -1) fatal_perror( "chdir %s", confdir );
410 strcpy( serverdir, SERVERDIR );
411 strcat( serverdir, hostname );
413 if (chdir( serverdir ) == -1)
415 if (errno != ENOENT) fatal_perror( "chdir %s", serverdir );
416 if (mkdir( serverdir, 0700 ) == -1) fatal_perror( "mkdir %s", serverdir );
417 if (chdir( serverdir ) == -1) fatal_perror( "chdir %s", serverdir );
419 if (stat( ".", &st ) == -1) fatal_perror( "stat %s", serverdir );
420 if (!S_ISDIR(st.st_mode)) fatal_error( "%s is not a directory\n", serverdir );
421 if (st.st_uid != getuid()) fatal_error( "%s is not owned by you\n", serverdir );
422 if (st.st_mode & 077) fatal_error( "%s must not be accessible by other users\n", serverdir );
425 /* open the master server socket and start waiting for new clients */
426 void open_master_socket(void)
428 struct sockaddr_un addr;
431 /* make sure no request is larger than the maximum size */
432 assert( sizeof(union generic_request) == sizeof(struct request_max_size) );
435 if ((fd = socket( AF_UNIX, SOCK_STREAM, 0 )) == -1) fatal_perror( "socket" );
436 addr.sun_family = AF_UNIX;
437 strcpy( addr.sun_path, SOCKETNAME );
438 slen = sizeof(addr) - sizeof(addr.sun_path) + strlen(addr.sun_path) + 1;
439 #ifdef HAVE_SOCKADDR_SUN_LEN
442 if (bind( fd, (struct sockaddr *)&addr, slen ) == -1)
444 if ((errno == EEXIST) || (errno == EADDRINUSE))
445 exit(0); /* pretend we succeeded to start */
447 fatal_perror( "bind" );
449 atexit( socket_cleanup );
451 chmod( SOCKETNAME, 0600 ); /* make sure no other user can connect */
452 if (listen( fd, 5 ) == -1) fatal_perror( "listen" );
454 if (!(master_socket = alloc_object( &master_socket_ops, fd )))
455 fatal_error( "out of memory\n" );
456 set_select_events( &master_socket->obj, POLLIN );
458 /* setup msghdr structure constant fields */
459 msghdr.msg_name = NULL;
460 msghdr.msg_namelen = 0;
461 msghdr.msg_iov = &myiovec;
462 msghdr.msg_iovlen = 1;
464 /* go in the background */
468 fatal_perror( "fork" );
473 _exit(0); /* do not call atexit functions */
477 /* close the master socket and stop waiting for new clients */
478 void close_master_socket(void)
480 /* if a new client is waiting, we keep on running */
481 if (!check_select_events( master_socket->obj.fd, POLLIN ))
482 release_object( master_socket );
485 /* lock/unlock the master socket to stop accepting new clients */
486 void lock_master_socket( int locked )
488 set_select_events( &master_socket->obj, locked ? 0 : POLLIN );