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
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_read_fd, /* get_read_fd */
67 no_write_fd, /* get_write_fd */
69 no_get_file_info, /* get_file_info */
70 master_socket_destroy /* destroy */
74 struct thread *current = NULL; /* thread handling the current request */
75 int global_error = 0; /* global error code for when no thread is current */
77 static struct master_socket *master_socket; /* the master socket object */
79 /* socket communication static structures */
80 static struct iovec myiovec;
81 static struct msghdr msghdr = { NULL, 0, &myiovec, 1, };
82 #ifndef HAVE_MSGHDR_ACCRIGHTS
85 int len; /* sizeof structure */
86 int level; /* SOL_SOCKET */
87 int type; /* SCM_RIGHTS */
88 int fd; /* fd to pass */
90 static struct cmsg_fd cmsg = { sizeof(cmsg), SOL_SOCKET, SCM_RIGHTS, -1 };
91 #endif /* HAVE_MSGHDR_ACCRIGHTS */
93 /* complain about a protocol error and terminate the client connection */
94 void fatal_protocol_error( struct thread *thread, const char *err, ... )
98 va_start( args, err );
99 fprintf( stderr, "Protocol error:%p: ", thread );
100 vfprintf( stderr, err, args );
102 thread->exit_code = 1;
103 kill_thread( thread, 1 );
106 /* die on a fatal error */
107 void fatal_error( const char *err, ... )
111 va_start( args, err );
112 fprintf( stderr, "wineserver: " );
113 vfprintf( stderr, err, args );
118 /* die on a fatal error */
119 void fatal_perror( const char *err, ... )
123 va_start( args, err );
124 fprintf( stderr, "wineserver: " );
125 vfprintf( stderr, err, args );
131 /* call a request handler */
132 static inline void call_req_handler( struct thread *thread, enum request req )
137 if (debug_level) trace_request( req );
139 if (req < REQ_NB_REQUESTS)
141 req_handlers[req]( current->buffer );
142 if (current && !current->wait) send_reply( current );
146 fatal_protocol_error( current, "bad request %d\n", req );
149 /* set the fd to pass to the thread */
150 void set_reply_fd( struct thread *thread, int pass_fd )
152 assert( thread->pass_fd == -1 );
153 thread->pass_fd = pass_fd;
156 /* send a reply to a thread */
157 void send_reply( struct thread *thread )
159 assert( !thread->wait );
160 if (debug_level) trace_reply( thread );
161 if (!write_request( thread )) set_select_events( &thread->obj, POLLOUT );
164 /* read a message from a client that has something to say */
165 void read_request( struct thread *thread )
170 #ifdef HAVE_MSGHDR_ACCRIGHTS
171 msghdr.msg_accrightslen = sizeof(int);
172 msghdr.msg_accrights = (void *)&thread->pass_fd;
173 #else /* HAVE_MSGHDR_ACCRIGHTS */
174 msghdr.msg_control = &cmsg;
175 msghdr.msg_controllen = sizeof(cmsg);
177 #endif /* HAVE_MSGHDR_ACCRIGHTS */
179 assert( thread->pass_fd == -1 );
181 myiovec.iov_base = (void *)&req;
182 myiovec.iov_len = sizeof(req);
184 ret = recvmsg( thread->obj.fd, &msghdr, 0 );
185 #ifndef HAVE_MSGHDR_ACCRIGHTS
186 thread->pass_fd = cmsg.fd;
189 if (ret == sizeof(req))
191 call_req_handler( thread, req );
192 thread->pass_fd = -1;
198 thread->exit_code = 1;
199 kill_thread( thread, 1 );
202 if (!ret) /* closed pipe */
204 kill_thread( thread, 0 );
207 fatal_protocol_error( thread, "partial message received %d/%d\n", ret, sizeof(req) );
210 /* send a message to a client that is ready to receive something */
211 int write_request( struct thread *thread )
215 if (thread->pass_fd == -1)
217 ret = write( thread->obj.fd, &thread->error, sizeof(thread->error) );
219 else /* we have an fd to send */
221 #ifdef HAVE_MSGHDR_ACCRIGHTS
222 msghdr.msg_accrightslen = sizeof(int);
223 msghdr.msg_accrights = (void *)&thread->pass_fd;
224 #else /* HAVE_MSGHDR_ACCRIGHTS */
225 msghdr.msg_control = &cmsg;
226 msghdr.msg_controllen = sizeof(cmsg);
227 cmsg.fd = thread->pass_fd;
228 #endif /* HAVE_MSGHDR_ACCRIGHTS */
230 myiovec.iov_base = (void *)&thread->error;
231 myiovec.iov_len = sizeof(thread->error);
233 ret = sendmsg( thread->obj.fd, &msghdr, 0 );
234 close( thread->pass_fd );
235 thread->pass_fd = -1;
237 if (ret == sizeof(thread->error))
239 set_select_events( &thread->obj, POLLIN );
244 if (errno == EWOULDBLOCK) return 0; /* not a fatal error */
247 kill_thread( thread, 0 ); /* normal death */
252 thread->exit_code = 1;
253 kill_thread( thread, 1 );
258 thread->exit_code = 1;
259 kill_thread( thread, 1 );
260 fprintf( stderr, "Partial message sent %d/%d\n", ret, sizeof(thread->error) );
265 static void master_socket_dump( struct object *obj, int verbose )
267 struct master_socket *sock = (struct master_socket *)obj;
268 assert( obj->ops == &master_socket_ops );
269 fprintf( stderr, "Master socket fd=%d\n", sock->obj.fd );
272 /* handle a socket event */
273 static void master_socket_poll_event( struct object *obj, int event )
275 struct master_socket *sock = (struct master_socket *)obj;
276 assert( obj->ops == &master_socket_ops );
278 assert( sock == master_socket ); /* there is only one master socket */
280 if (event & (POLLERR | POLLHUP))
282 /* this is not supposed to happen */
283 fprintf( stderr, "wineserver: Error on master socket\n" );
284 release_object( obj );
286 else if (event & POLLIN)
288 struct sockaddr_un dummy;
289 int len = sizeof(dummy);
290 int client = accept( master_socket->obj.fd, (struct sockaddr *) &dummy, &len );
291 if (client != -1) create_process( client );
295 /* remove the socket upon exit */
296 static void socket_cleanup(void)
298 static int do_it_once;
299 if (!do_it_once++) unlink( SOCKETNAME );
302 static void master_socket_destroy( struct object *obj )
307 /* return the configuration directory ($WINEPREFIX or $HOME/.wine) */
308 const char *get_config_dir(void)
310 static char *confdir;
313 const char *prefix = getenv( "WINEPREFIX" );
316 int len = strlen(prefix);
317 if (!(confdir = strdup( prefix ))) fatal_error( "out of memory\n" );
318 if (len > 1 && confdir[len-1] == '/') confdir[len-1] = 0;
322 const char *home = getenv( "HOME" );
325 struct passwd *pwd = getpwuid( getuid() );
326 if (!pwd) fatal_error( "could not find your home directory\n" );
329 if (!(confdir = malloc( strlen(home) + strlen(CONFDIR) + 1 )))
330 fatal_error( "out of memory\n" );
331 strcpy( confdir, home );
332 strcat( confdir, CONFDIR );
334 mkdir( confdir, 0755 ); /* just in case */
339 /* create the server directory and chdir to it */
340 static void create_server_dir(void)
344 const char *confdir = get_config_dir();
347 if (gethostname( hostname, sizeof(hostname) ) == -1) fatal_perror( "gethostname" );
349 if (!(serverdir = malloc( strlen(confdir) + strlen(SERVERDIR) + strlen(hostname) + 1 )))
350 fatal_error( "out of memory\n" );
352 strcpy( serverdir, confdir );
353 strcat( serverdir, SERVERDIR );
354 strcat( serverdir, hostname );
356 if (chdir( serverdir ) == -1)
358 if (errno != ENOENT) fatal_perror( "chdir %s", serverdir );
359 if (mkdir( serverdir, 0700 ) == -1) fatal_perror( "mkdir %s", serverdir );
360 if (chdir( serverdir ) == -1) fatal_perror( "chdir %s", serverdir );
362 if (stat( ".", &st ) == -1) fatal_perror( "stat %s", serverdir );
363 if (!S_ISDIR(st.st_mode)) fatal_error( "%s is not a directory\n", serverdir );
364 if (st.st_uid != getuid()) fatal_error( "%s is not owned by you\n", serverdir );
365 if (st.st_mode & 077) fatal_error( "%s must not be accessible by other users\n", serverdir );
368 /* open the master server socket and start waiting for new clients */
369 void open_master_socket(void)
371 struct sockaddr_un addr;
375 if ((fd = socket( AF_UNIX, SOCK_STREAM, 0 )) == -1) fatal_perror( "socket" );
376 addr.sun_family = AF_UNIX;
377 strcpy( addr.sun_path, SOCKETNAME );
378 slen = sizeof(addr) - sizeof(addr.sun_path) + strlen(addr.sun_path) + 1;
379 #ifdef HAVE_SOCKADDR_SUN_LEN
382 if (bind( fd, (struct sockaddr *)&addr, slen ) == -1)
384 if ((errno == EEXIST) || (errno == EADDRINUSE))
385 exit(0); /* pretend we succeeded to start */
387 fatal_perror( "bind" );
389 atexit( socket_cleanup );
391 chmod( SOCKETNAME, 0600 ); /* make sure no other user can connect */
392 if (listen( fd, 5 ) == -1) fatal_perror( "listen" );
394 if (!(master_socket = alloc_object( &master_socket_ops, fd )))
395 fatal_error( "out of memory\n" );
396 set_select_events( &master_socket->obj, POLLIN );
398 /* go in the background */
402 fatal_perror( "fork" );
407 _exit(0); /* do not call atexit functions */
411 /* close the master socket and stop waiting for new clients */
412 void close_master_socket(void)
414 /* if a new client is waiting, we keep on running */
415 if (!check_select_events( master_socket->obj.fd, POLLIN ))
416 release_object( master_socket );
419 /* lock/unlock the master socket to stop accepting new clients */
420 void lock_master_socket( int locked )
422 set_select_events( &master_socket->obj, locked ? 0 : POLLIN );