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 */
74 struct thread *current = NULL; /* thread handling the current request */
75 unsigned 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;
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 /* complain about a protocol error and terminate the client connection */
107 void fatal_protocol_perror( struct thread *thread, const char *err, ... )
111 va_start( args, err );
112 fprintf( stderr, "Protocol error:%p: ", thread );
113 vfprintf( stderr, err, args );
116 thread->exit_code = 1;
117 kill_thread( thread, 1 );
120 /* die on a fatal error */
121 void fatal_error( const char *err, ... )
125 va_start( args, err );
126 fprintf( stderr, "wineserver: " );
127 vfprintf( stderr, err, args );
132 /* die on a fatal error */
133 void fatal_perror( const char *err, ... )
137 va_start( args, err );
138 fprintf( stderr, "wineserver: " );
139 vfprintf( stderr, err, args );
145 /* call a request handler */
146 static inline void call_req_handler( struct thread *thread, union generic_request *request )
148 enum request req = request->header.req;
153 if (debug_level) trace_request( thread, request );
155 if (request->header.var_size)
157 if ((unsigned int)request->header.var_offset +
158 request->header.var_size > MAX_REQUEST_LENGTH)
160 fatal_protocol_error( current, "bad request offset/size %d/%d\n",
161 request->header.var_offset, request->header.var_size );
166 if (req < REQ_NB_REQUESTS)
168 req_handlers[req]( request );
169 if (current) send_reply( current, request );
173 fatal_protocol_error( current, "bad request %d\n", req );
176 /* read a request from a thread */
177 void read_request( struct thread *thread )
179 union generic_request req;
182 if ((ret = read( thread->obj.fd, &req, sizeof(req) )) == sizeof(req))
184 call_req_handler( thread, &req );
187 if (!ret) /* closed pipe */
188 kill_thread( thread, 0 );
190 fatal_protocol_error( thread, "partial read %d\n", ret );
192 fatal_protocol_perror( thread, "read" );
195 /* send a reply to a thread */
196 void send_reply( struct thread *thread, union generic_request *request )
200 if (debug_level) trace_reply( thread, request );
202 request->header.error = thread->error;
204 if ((ret = write( thread->reply_fd, request, sizeof(*request) )) != sizeof(*request))
207 fatal_protocol_error( thread, "partial write %d\n", ret );
208 else if (errno == EPIPE)
209 kill_thread( thread, 0 ); /* normal death */
211 fatal_protocol_perror( thread, "reply write" );
215 /* receive a file descriptor on the process socket */
216 int receive_fd( struct process *process )
221 #ifdef HAVE_MSGHDR_ACCRIGHTS
222 msghdr.msg_accrightslen = sizeof(int);
223 msghdr.msg_accrights = (void *)&fd;
224 #else /* HAVE_MSGHDR_ACCRIGHTS */
225 msghdr.msg_control = &cmsg;
226 msghdr.msg_controllen = sizeof(cmsg);
228 #endif /* HAVE_MSGHDR_ACCRIGHTS */
230 myiovec.iov_base = &data;
231 myiovec.iov_len = sizeof(data);
233 ret = recvmsg( process->obj.fd, &msghdr, 0 );
234 #ifndef HAVE_MSGHDR_ACCRIGHTS
238 if (ret == sizeof(data))
240 struct thread *thread;
242 if (data.tid) thread = get_thread_from_id( data.tid );
243 else thread = (struct thread *)grab_object( process->thread_list );
245 if (!thread || thread->process != process)
248 fprintf( stderr, "%08x: *fd* %d <- %d bad thread id\n",
249 (unsigned int)data.tid, data.fd, fd );
255 fprintf( stderr, "%08x: *fd* %d <- %d\n",
256 (unsigned int)thread, data.fd, fd );
257 thread_add_inflight_fd( thread, data.fd, fd );
259 if (thread) release_object( thread );
265 fprintf( stderr, "Protocol error: process %p: partial recvmsg %d for fd\n", process, ret );
266 kill_process( process, NULL, 1 );
270 if (errno != EWOULDBLOCK && errno != EAGAIN)
272 fprintf( stderr, "Protocol error: process %p: ", process );
274 kill_process( process, NULL, 1 );
280 /* send the wakeup signal to a thread */
281 int send_thread_wakeup( struct thread *thread, int signaled )
283 int ret = write( thread->wait_fd, &signaled, sizeof(signaled) );
284 if (ret == sizeof(signaled)) return 0;
286 fatal_protocol_error( thread, "partial wakeup write %d\n", ret );
287 else if (errno == EPIPE)
288 kill_thread( thread, 0 ); /* normal death */
290 fatal_protocol_perror( thread, "write" );
294 /* send an fd to a client */
295 int send_client_fd( struct process *process, int fd, handle_t handle )
300 fprintf( stderr, "%08x: *fd* %d -> %d\n", (unsigned int)current, handle, fd );
302 #ifdef HAVE_MSGHDR_ACCRIGHTS
303 msghdr.msg_accrightslen = sizeof(fd);
304 msghdr.msg_accrights = (void *)&fd;
305 #else /* HAVE_MSGHDR_ACCRIGHTS */
306 msghdr.msg_control = &cmsg;
307 msghdr.msg_controllen = sizeof(cmsg);
309 #endif /* HAVE_MSGHDR_ACCRIGHTS */
311 myiovec.iov_base = (void *)&handle;
312 myiovec.iov_len = sizeof(handle);
314 ret = sendmsg( process->obj.fd, &msghdr, 0 );
316 if (ret == sizeof(handle)) return 0;
320 fprintf( stderr, "Protocol error: process %p: partial sendmsg %d\n", process, ret );
321 kill_process( process, NULL, 1 );
325 fprintf( stderr, "Protocol error: process %p: ", process );
327 kill_process( process, NULL, 1 );
332 static void master_socket_dump( struct object *obj, int verbose )
334 struct master_socket *sock = (struct master_socket *)obj;
335 assert( obj->ops == &master_socket_ops );
336 fprintf( stderr, "Master socket fd=%d\n", sock->obj.fd );
339 /* handle a socket event */
340 static void master_socket_poll_event( struct object *obj, int event )
342 struct master_socket *sock = (struct master_socket *)obj;
343 assert( obj->ops == &master_socket_ops );
345 assert( sock == master_socket ); /* there is only one master socket */
347 if (event & (POLLERR | POLLHUP))
349 /* this is not supposed to happen */
350 fprintf( stderr, "wineserver: Error on master socket\n" );
351 release_object( obj );
353 else if (event & POLLIN)
355 struct sockaddr_un dummy;
356 int len = sizeof(dummy);
357 int client = accept( master_socket->obj.fd, (struct sockaddr *) &dummy, &len );
358 if (client != -1) create_process( client );
362 /* remove the socket upon exit */
363 static void socket_cleanup(void)
365 static int do_it_once;
366 if (!do_it_once++) unlink( SOCKETNAME );
369 static void master_socket_destroy( struct object *obj )
374 /* return the configuration directory ($WINEPREFIX or $HOME/.wine) */
375 const char *get_config_dir(void)
377 static char *confdir;
380 const char *prefix = getenv( "WINEPREFIX" );
383 int len = strlen(prefix);
384 if (!(confdir = strdup( prefix ))) fatal_error( "out of memory\n" );
385 if (len > 1 && confdir[len-1] == '/') confdir[len-1] = 0;
389 const char *home = getenv( "HOME" );
392 struct passwd *pwd = getpwuid( getuid() );
393 if (!pwd) fatal_error( "could not find your home directory\n" );
396 if (!(confdir = malloc( strlen(home) + strlen(CONFDIR) + 1 )))
397 fatal_error( "out of memory\n" );
398 strcpy( confdir, home );
399 strcat( confdir, CONFDIR );
401 mkdir( confdir, 0755 ); /* just in case */
406 /* create the server directory and chdir to it */
407 static void create_server_dir(void)
411 const char *confdir = get_config_dir();
414 if (gethostname( hostname, sizeof(hostname) ) == -1) fatal_perror( "gethostname" );
416 if (!(serverdir = malloc( strlen(confdir) + strlen(SERVERDIR) + strlen(hostname) + 1 )))
417 fatal_error( "out of memory\n" );
419 strcpy( serverdir, confdir );
420 strcat( serverdir, SERVERDIR );
421 strcat( serverdir, hostname );
423 if (chdir( serverdir ) == -1)
425 if (errno != ENOENT) fatal_perror( "chdir %s", serverdir );
426 if (mkdir( serverdir, 0700 ) == -1) fatal_perror( "mkdir %s", serverdir );
427 if (chdir( serverdir ) == -1) fatal_perror( "chdir %s", serverdir );
429 if (stat( ".", &st ) == -1) fatal_perror( "stat %s", serverdir );
430 if (!S_ISDIR(st.st_mode)) fatal_error( "%s is not a directory\n", serverdir );
431 if (st.st_uid != getuid()) fatal_error( "%s is not owned by you\n", serverdir );
432 if (st.st_mode & 077) fatal_error( "%s must not be accessible by other users\n", serverdir );
435 /* open the master server socket and start waiting for new clients */
436 void open_master_socket(void)
438 struct sockaddr_un addr;
441 /* make sure no request is larger than the maximum size */
442 assert( sizeof(union generic_request) == sizeof(struct request_max_size) );
445 if ((fd = socket( AF_UNIX, SOCK_STREAM, 0 )) == -1) fatal_perror( "socket" );
446 addr.sun_family = AF_UNIX;
447 strcpy( addr.sun_path, SOCKETNAME );
448 slen = sizeof(addr) - sizeof(addr.sun_path) + strlen(addr.sun_path) + 1;
449 #ifdef HAVE_SOCKADDR_SUN_LEN
452 if (bind( fd, (struct sockaddr *)&addr, slen ) == -1)
454 if ((errno == EEXIST) || (errno == EADDRINUSE))
455 exit(0); /* pretend we succeeded to start */
457 fatal_perror( "bind" );
459 atexit( socket_cleanup );
461 chmod( SOCKETNAME, 0600 ); /* make sure no other user can connect */
462 if (listen( fd, 5 ) == -1) fatal_perror( "listen" );
464 if (!(master_socket = alloc_object( &master_socket_ops, fd )))
465 fatal_error( "out of memory\n" );
466 set_select_events( &master_socket->obj, POLLIN );
468 /* setup msghdr structure constant fields */
469 msghdr.msg_name = NULL;
470 msghdr.msg_namelen = 0;
471 msghdr.msg_iov = &myiovec;
472 msghdr.msg_iovlen = 1;
474 /* go in the background */
478 fatal_perror( "fork" );
483 _exit(0); /* do not call atexit functions */
487 /* close the master socket and stop waiting for new clients */
488 void close_master_socket(void)
490 /* if a new client is waiting, we keep on running */
491 if (!check_select_events( master_socket->obj.fd, POLLIN ))
492 release_object( master_socket );
495 /* lock/unlock the master socket to stop accepting new clients */
496 void lock_master_socket( int locked )
498 set_select_events( &master_socket->obj, locked ? 0 : POLLIN );