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 /* die on a fatal error */
133 void fatal_error( const char *err, ... )
137 va_start( args, err );
138 fprintf( stderr, "wineserver: " );
139 vfprintf( stderr, err, args );
144 /* die on a fatal error */
145 void fatal_perror( const char *err, ... )
149 va_start( args, err );
150 fprintf( stderr, "wineserver: " );
151 vfprintf( stderr, err, args );
157 /* call a request handler */
158 static inline void call_req_handler( struct thread *thread )
164 req = ((struct request_header *)current->buffer)->req;
166 if (debug_level) trace_request( req );
168 if (req < REQ_NB_REQUESTS)
170 req_handlers[req]( current->buffer );
171 if (current && !current->wait) send_reply( current );
175 fatal_protocol_error( current, "bad request %d\n", req );
178 /* send a reply to a thread */
179 void send_reply( struct thread *thread )
181 assert( !thread->wait );
182 if (debug_level) trace_reply( thread );
183 if (!write_request( thread )) set_select_events( &thread->obj, POLLOUT );
186 /* read a message from a client that has something to say */
187 void read_request( struct thread *thread )
192 #ifdef HAVE_MSGHDR_ACCRIGHTS
193 msghdr.msg_accrightslen = sizeof(int);
194 msghdr.msg_accrights = (void *)&thread->pass_fd;
195 #else /* HAVE_MSGHDR_ACCRIGHTS */
196 msghdr.msg_control = &cmsg;
197 msghdr.msg_controllen = sizeof(cmsg);
199 #endif /* HAVE_MSGHDR_ACCRIGHTS */
201 assert( thread->pass_fd == -1 );
203 myiovec.iov_base = dummy;
206 ret = recvmsg( thread->obj.fd, &msghdr, 0 );
207 #ifndef HAVE_MSGHDR_ACCRIGHTS
208 thread->pass_fd = cmsg.fd;
213 call_req_handler( thread );
214 thread->pass_fd = -1;
217 if (!ret) /* closed pipe */
219 kill_thread( thread, 0 );
223 thread->exit_code = 1;
224 kill_thread( thread, 1 );
227 /* send a message to a client that is ready to receive something */
228 int write_request( struct thread *thread )
231 struct request_header *header = thread->buffer;
233 header->error = thread->error;
235 assert (thread->pass_fd == -1);
237 ret = write( thread->reply_fd, header, 1 );
240 set_select_events( &thread->obj, POLLIN );
243 if (errno == EWOULDBLOCK) return 0; /* not a fatal error */
246 kill_thread( thread, 0 ); /* normal death */
251 thread->exit_code = 1;
252 kill_thread( thread, 1 );
257 /* send an fd to a client */
258 int send_client_fd( struct thread *thread, int fd, handle_t handle )
263 fprintf( stderr, "%08x: *fd* %d = %d\n", (unsigned int)thread, handle, fd );
265 #ifdef HAVE_MSGHDR_ACCRIGHTS
266 msghdr.msg_accrightslen = sizeof(fd);
267 msghdr.msg_accrights = (void *)&fd;
268 #else /* HAVE_MSGHDR_ACCRIGHTS */
269 msghdr.msg_control = &cmsg;
270 msghdr.msg_controllen = sizeof(cmsg);
272 #endif /* HAVE_MSGHDR_ACCRIGHTS */
274 myiovec.iov_base = (void *)&handle;
275 myiovec.iov_len = sizeof(handle);
277 ret = sendmsg( thread->obj.fd, &msghdr, 0 );
279 if (ret > 0) return 0;
282 kill_thread( thread, 0 ); /* normal death */
287 thread->exit_code = 1;
288 kill_thread( thread, 1 );
293 static void master_socket_dump( struct object *obj, int verbose )
295 struct master_socket *sock = (struct master_socket *)obj;
296 assert( obj->ops == &master_socket_ops );
297 fprintf( stderr, "Master socket fd=%d\n", sock->obj.fd );
300 /* handle a socket event */
301 static void master_socket_poll_event( struct object *obj, int event )
303 struct master_socket *sock = (struct master_socket *)obj;
304 assert( obj->ops == &master_socket_ops );
306 assert( sock == master_socket ); /* there is only one master socket */
308 if (event & (POLLERR | POLLHUP))
310 /* this is not supposed to happen */
311 fprintf( stderr, "wineserver: Error on master socket\n" );
312 release_object( obj );
314 else if (event & POLLIN)
316 struct sockaddr_un dummy;
317 int len = sizeof(dummy);
318 int client = accept( master_socket->obj.fd, (struct sockaddr *) &dummy, &len );
319 if (client != -1) create_process( client );
323 /* remove the socket upon exit */
324 static void socket_cleanup(void)
326 static int do_it_once;
327 if (!do_it_once++) unlink( SOCKETNAME );
330 static void master_socket_destroy( struct object *obj )
335 static void request_socket_dump( struct object *obj, int verbose )
337 struct request_socket *sock = (struct request_socket *)obj;
338 assert( obj->ops == &request_socket_ops );
339 fprintf( stderr, "Request socket fd=%d thread=%p\n", sock->obj.fd, sock->thread );
342 /* handle a request socket event */
343 static void request_socket_poll_event( struct object *obj, int event )
345 struct request_socket *sock = (struct request_socket *)obj;
346 assert( obj->ops == &request_socket_ops );
348 if (event & (POLLERR | POLLHUP)) kill_thread( sock->thread, 0 );
349 else if (event & POLLIN)
351 struct thread *thread = sock->thread;
355 ret = read( sock->obj.fd, &dummy, 1 );
358 call_req_handler( thread );
361 if (!ret) /* closed pipe */
363 kill_thread( thread, 0 );
367 thread->exit_code = 1;
368 kill_thread( thread, 1 );
372 /* create a request socket and send the fd to the client thread */
373 struct object *create_request_socket( struct thread *thread )
375 struct request_socket *sock;
378 if (pipe( fd )) return NULL;
379 if (!(sock = alloc_object( &request_socket_ops, fd[0] )))
384 sock->thread = thread;
385 send_client_fd( thread, fd[1], 0 );
387 set_select_events( &sock->obj, POLLIN );
391 /* return the configuration directory ($WINEPREFIX or $HOME/.wine) */
392 const char *get_config_dir(void)
394 static char *confdir;
397 const char *prefix = getenv( "WINEPREFIX" );
400 int len = strlen(prefix);
401 if (!(confdir = strdup( prefix ))) fatal_error( "out of memory\n" );
402 if (len > 1 && confdir[len-1] == '/') confdir[len-1] = 0;
406 const char *home = getenv( "HOME" );
409 struct passwd *pwd = getpwuid( getuid() );
410 if (!pwd) fatal_error( "could not find your home directory\n" );
413 if (!(confdir = malloc( strlen(home) + strlen(CONFDIR) + 1 )))
414 fatal_error( "out of memory\n" );
415 strcpy( confdir, home );
416 strcat( confdir, CONFDIR );
418 mkdir( confdir, 0755 ); /* just in case */
423 /* create the server directory and chdir to it */
424 static void create_server_dir(void)
428 const char *confdir = get_config_dir();
431 if (gethostname( hostname, sizeof(hostname) ) == -1) fatal_perror( "gethostname" );
433 if (!(serverdir = malloc( strlen(confdir) + strlen(SERVERDIR) + strlen(hostname) + 1 )))
434 fatal_error( "out of memory\n" );
436 strcpy( serverdir, confdir );
437 strcat( serverdir, SERVERDIR );
438 strcat( serverdir, hostname );
440 if (chdir( serverdir ) == -1)
442 if (errno != ENOENT) fatal_perror( "chdir %s", serverdir );
443 if (mkdir( serverdir, 0700 ) == -1) fatal_perror( "mkdir %s", serverdir );
444 if (chdir( serverdir ) == -1) fatal_perror( "chdir %s", serverdir );
446 if (stat( ".", &st ) == -1) fatal_perror( "stat %s", serverdir );
447 if (!S_ISDIR(st.st_mode)) fatal_error( "%s is not a directory\n", serverdir );
448 if (st.st_uid != getuid()) fatal_error( "%s is not owned by you\n", serverdir );
449 if (st.st_mode & 077) fatal_error( "%s must not be accessible by other users\n", serverdir );
452 /* open the master server socket and start waiting for new clients */
453 void open_master_socket(void)
455 struct sockaddr_un addr;
458 /* make sure no request is larger than the maximum size */
459 assert( sizeof(union generic_request) == sizeof(struct request_max_size) );
462 if ((fd = socket( AF_UNIX, SOCK_STREAM, 0 )) == -1) fatal_perror( "socket" );
463 addr.sun_family = AF_UNIX;
464 strcpy( addr.sun_path, SOCKETNAME );
465 slen = sizeof(addr) - sizeof(addr.sun_path) + strlen(addr.sun_path) + 1;
466 #ifdef HAVE_SOCKADDR_SUN_LEN
469 if (bind( fd, (struct sockaddr *)&addr, slen ) == -1)
471 if ((errno == EEXIST) || (errno == EADDRINUSE))
472 exit(0); /* pretend we succeeded to start */
474 fatal_perror( "bind" );
476 atexit( socket_cleanup );
478 chmod( SOCKETNAME, 0600 ); /* make sure no other user can connect */
479 if (listen( fd, 5 ) == -1) fatal_perror( "listen" );
481 if (!(master_socket = alloc_object( &master_socket_ops, fd )))
482 fatal_error( "out of memory\n" );
483 set_select_events( &master_socket->obj, POLLIN );
485 /* setup msghdr structure constant fields */
486 msghdr.msg_name = NULL;
487 msghdr.msg_namelen = 0;
488 msghdr.msg_iov = &myiovec;
489 msghdr.msg_iovlen = 1;
491 /* go in the background */
495 fatal_perror( "fork" );
500 _exit(0); /* do not call atexit functions */
504 /* close the master socket and stop waiting for new clients */
505 void close_master_socket(void)
507 /* if a new client is waiting, we keep on running */
508 if (!check_select_events( master_socket->obj.fd, POLLIN ))
509 release_object( master_socket );
512 /* lock/unlock the master socket to stop accepting new clients */
513 void lock_master_socket( int locked )
515 set_select_events( &master_socket->obj, locked ? 0 : POLLIN );