2 * Server-side request handling
4 * Copyright (C) 1998 Alexandre Julliard
21 #include <sys/types.h>
22 #ifdef HAVE_SYS_SOCKET_H
23 # 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_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 */
75 unsigned int server_start_ticks = 0; /* tick count offset from server startup */
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 /* allocate the reply data */
146 void *set_reply_data_size( size_t size )
148 assert( size <= get_reply_max_size() );
149 if (size && !(current->reply_data = mem_alloc( size ))) size = 0;
150 current->reply_size = size;
151 return current->reply_data;
154 /* write the remaining part of the reply */
155 void write_reply( struct thread *thread )
159 if ((ret = write( thread->reply_fd,
160 (char *)thread->reply_data + thread->reply_size - thread->reply_towrite,
161 thread->reply_towrite )) >= 0)
163 if (!(thread->reply_towrite -= ret))
165 free( thread->reply_data );
166 thread->reply_data = NULL;
167 /* sent everything, can go back to waiting for requests */
168 change_select_fd( &thread->obj, thread->request_fd, POLLIN );
173 kill_thread( thread, 0 ); /* normal death */
174 else if (errno != EWOULDBLOCK && errno != EAGAIN)
175 fatal_protocol_perror( thread, "reply write" );
178 /* send a reply to the current thread */
179 static void send_reply( union generic_reply *reply )
183 if (!current->reply_size)
185 if ((ret = write( current->reply_fd, reply, sizeof(*reply) )) != sizeof(*reply)) goto error;
191 vec[0].iov_base = reply;
192 vec[0].iov_len = sizeof(*reply);
193 vec[1].iov_base = current->reply_data;
194 vec[1].iov_len = current->reply_size;
196 if ((ret = writev( current->reply_fd, vec, 2 )) < sizeof(*reply)) goto error;
198 if ((current->reply_towrite = current->reply_size - (ret - sizeof(*reply))))
200 /* couldn't write it all, wait for POLLOUT */
201 change_select_fd( ¤t->obj, current->reply_fd, POLLOUT );
205 if (current->reply_data)
207 free( current->reply_data );
208 current->reply_data = NULL;
214 fatal_protocol_error( current, "partial write %d\n", ret );
215 else if (errno == EPIPE)
216 kill_thread( current, 0 ); /* normal death */
218 fatal_protocol_perror( current, "reply write" );
221 /* call a request handler */
222 static void call_req_handler( struct thread *thread )
224 union generic_reply reply;
225 enum request req = thread->req.request_header.req;
228 current->reply_size = 0;
230 memset( &reply, 0, sizeof(reply) );
232 if (debug_level) trace_request();
234 if (req < REQ_NB_REQUESTS)
236 req_handlers[req]( ¤t->req, &reply );
239 reply.reply_header.error = current->error;
240 reply.reply_header.reply_size = current->reply_size;
241 if (debug_level) trace_reply( req, &reply );
242 send_reply( &reply );
247 fatal_protocol_error( current, "bad request %d\n", req );
250 /* read a request from a thread */
251 void read_request( struct thread *thread )
255 if (!thread->req_toread) /* no pending request */
257 if ((ret = read( thread->obj.fd, &thread->req,
258 sizeof(thread->req) )) != sizeof(thread->req)) goto error;
259 if (!(thread->req_toread = thread->req.request_header.request_size))
261 /* no data, handle request at once */
262 call_req_handler( thread );
265 if (!(thread->req_data = malloc( thread->req_toread )))
266 fatal_protocol_error( thread, "no memory for %d bytes request\n", thread->req_toread );
269 /* read the variable sized data */
272 ret = read( thread->obj.fd, ((char *)thread->req_data +
273 thread->req.request_header.request_size - thread->req_toread),
274 thread->req_toread );
276 if (!(thread->req_toread -= ret))
278 call_req_handler( thread );
279 free( thread->req_data );
280 thread->req_data = NULL;
286 if (!ret) /* closed pipe */
287 kill_thread( thread, 0 );
289 fatal_protocol_error( thread, "partial read %d\n", ret );
290 else if (errno != EWOULDBLOCK && errno != EAGAIN)
291 fatal_protocol_perror( thread, "read" );
294 /* receive a file descriptor on the process socket */
295 int receive_fd( struct process *process )
300 #ifdef HAVE_MSGHDR_ACCRIGHTS
301 msghdr.msg_accrightslen = sizeof(int);
302 msghdr.msg_accrights = (void *)&fd;
303 #else /* HAVE_MSGHDR_ACCRIGHTS */
304 msghdr.msg_control = &cmsg;
305 msghdr.msg_controllen = sizeof(cmsg);
307 #endif /* HAVE_MSGHDR_ACCRIGHTS */
309 myiovec.iov_base = &data;
310 myiovec.iov_len = sizeof(data);
312 ret = recvmsg( process->obj.fd, &msghdr, 0 );
313 #ifndef HAVE_MSGHDR_ACCRIGHTS
317 if (ret == sizeof(data))
319 struct thread *thread;
321 if (data.tid) thread = get_thread_from_id( data.tid );
322 else thread = (struct thread *)grab_object( process->thread_list );
324 if (!thread || thread->process != process)
327 fprintf( stderr, "%08x: *fd* %d <- %d bad thread id\n",
328 (unsigned int)data.tid, data.fd, fd );
334 fprintf( stderr, "%08x: *fd* %d <- %d\n",
335 (unsigned int)thread, data.fd, fd );
336 thread_add_inflight_fd( thread, data.fd, fd );
338 if (thread) release_object( thread );
345 fprintf( stderr, "Protocol error: process %p: partial recvmsg %d for fd\n",
347 kill_process( process, NULL, 1 );
351 if (errno != EWOULDBLOCK && errno != EAGAIN)
353 fprintf( stderr, "Protocol error: process %p: ", process );
355 kill_process( process, NULL, 1 );
361 /* send an fd to a client */
362 int send_client_fd( struct process *process, int fd, handle_t handle )
367 fprintf( stderr, "%08x: *fd* %d -> %d\n", (unsigned int)current, handle, fd );
369 #ifdef HAVE_MSGHDR_ACCRIGHTS
370 msghdr.msg_accrightslen = sizeof(fd);
371 msghdr.msg_accrights = (void *)&fd;
372 #else /* HAVE_MSGHDR_ACCRIGHTS */
373 msghdr.msg_control = &cmsg;
374 msghdr.msg_controllen = sizeof(cmsg);
376 #endif /* HAVE_MSGHDR_ACCRIGHTS */
378 myiovec.iov_base = (void *)&handle;
379 myiovec.iov_len = sizeof(handle);
381 ret = sendmsg( process->obj.fd, &msghdr, 0 );
383 if (ret == sizeof(handle)) return 0;
388 fprintf( stderr, "Protocol error: process %p: partial sendmsg %d\n", process, ret );
389 kill_process( process, NULL, 1 );
393 fprintf( stderr, "Protocol error: process %p: ", process );
395 kill_process( process, NULL, 1 );
400 /* get current tick count to return to client */
401 unsigned int get_tick_count(void)
404 gettimeofday( &t, NULL );
405 return (t.tv_sec * 1000) + (t.tv_usec / 1000) - server_start_ticks;
408 static void master_socket_dump( struct object *obj, int verbose )
410 struct master_socket *sock = (struct master_socket *)obj;
411 assert( obj->ops == &master_socket_ops );
412 fprintf( stderr, "Master socket fd=%d\n", sock->obj.fd );
415 /* handle a socket event */
416 static void master_socket_poll_event( struct object *obj, int event )
418 struct master_socket *sock = (struct master_socket *)obj;
419 assert( obj->ops == &master_socket_ops );
421 assert( sock == master_socket ); /* there is only one master socket */
423 if (event & (POLLERR | POLLHUP))
425 /* this is not supposed to happen */
426 fprintf( stderr, "wineserver: Error on master socket\n" );
427 release_object( obj );
429 else if (event & POLLIN)
431 struct sockaddr_un dummy;
432 int len = sizeof(dummy);
433 int client = accept( master_socket->obj.fd, (struct sockaddr *) &dummy, &len );
434 if (client == -1) return;
435 fcntl( client, F_SETFL, O_NONBLOCK );
436 create_process( client );
440 /* remove the socket upon exit */
441 static void socket_cleanup(void)
443 static int do_it_once;
444 if (!do_it_once++) unlink( SOCKETNAME );
447 static void master_socket_destroy( struct object *obj )
452 /* return the configuration directory ($WINEPREFIX or $HOME/.wine) */
453 const char *get_config_dir(void)
455 static char *confdir;
458 const char *prefix = getenv( "WINEPREFIX" );
461 int len = strlen(prefix);
462 if (!(confdir = strdup( prefix ))) fatal_error( "out of memory\n" );
463 if (len > 1 && confdir[len-1] == '/') confdir[len-1] = 0;
467 const char *home = getenv( "HOME" );
470 struct passwd *pwd = getpwuid( getuid() );
471 if (!pwd) fatal_error( "could not find your home directory\n" );
474 if (!(confdir = malloc( strlen(home) + strlen(CONFDIR) + 1 )))
475 fatal_error( "out of memory\n" );
476 strcpy( confdir, home );
477 strcat( confdir, CONFDIR );
483 /* create the server directory and chdir to it */
484 static void create_server_dir(void)
488 const char *confdir = get_config_dir();
491 if (gethostname( hostname, sizeof(hostname) ) == -1) fatal_perror( "gethostname" );
493 if (!(serverdir = malloc( strlen(SERVERDIR) + strlen(hostname) + 1 )))
494 fatal_error( "out of memory\n" );
496 if (chdir( confdir ) == -1) fatal_perror( "chdir %s", confdir );
498 strcpy( serverdir, SERVERDIR );
499 strcat( serverdir, hostname );
501 if (chdir( serverdir ) == -1)
503 if (errno != ENOENT) fatal_perror( "chdir %s", serverdir );
504 if (mkdir( serverdir, 0700 ) == -1) fatal_perror( "mkdir %s", serverdir );
505 if (chdir( serverdir ) == -1) fatal_perror( "chdir %s", serverdir );
507 if (stat( ".", &st ) == -1) fatal_perror( "stat %s", serverdir );
508 if (!S_ISDIR(st.st_mode)) fatal_error( "%s is not a directory\n", serverdir );
509 if (st.st_uid != getuid()) fatal_error( "%s is not owned by you\n", serverdir );
510 if (st.st_mode & 077) fatal_error( "%s must not be accessible by other users\n", serverdir );
513 /* open the master server socket and start waiting for new clients */
514 void open_master_socket(void)
516 struct sockaddr_un addr;
519 /* make sure no request is larger than the maximum size */
520 assert( sizeof(union generic_request) == sizeof(struct request_max_size) );
521 assert( sizeof(union generic_reply) == sizeof(struct request_max_size) );
524 if ((fd = socket( AF_UNIX, SOCK_STREAM, 0 )) == -1) fatal_perror( "socket" );
525 addr.sun_family = AF_UNIX;
526 strcpy( addr.sun_path, SOCKETNAME );
527 slen = sizeof(addr) - sizeof(addr.sun_path) + strlen(addr.sun_path) + 1;
528 #ifdef HAVE_SOCKADDR_SUN_LEN
531 if (bind( fd, (struct sockaddr *)&addr, slen ) == -1)
533 if ((errno == EEXIST) || (errno == EADDRINUSE))
534 exit(0); /* pretend we succeeded to start */
536 fatal_perror( "bind" );
538 atexit( socket_cleanup );
540 chmod( SOCKETNAME, 0600 ); /* make sure no other user can connect */
541 if (listen( fd, 5 ) == -1) fatal_perror( "listen" );
543 if (!(master_socket = alloc_object( &master_socket_ops, fd )))
544 fatal_error( "out of memory\n" );
545 set_select_events( &master_socket->obj, POLLIN );
547 /* setup msghdr structure constant fields */
548 msghdr.msg_name = NULL;
549 msghdr.msg_namelen = 0;
550 msghdr.msg_iov = &myiovec;
551 msghdr.msg_iovlen = 1;
553 /* init startup ticks */
554 server_start_ticks = get_tick_count();
556 /* go in the background */
560 fatal_perror( "fork" );
565 _exit(0); /* do not call atexit functions */
569 /* close the master socket and stop waiting for new clients */
570 void close_master_socket(void)
572 /* if a new client is waiting, we keep on running */
573 if (!check_select_events( master_socket->obj.fd, POLLIN ))
574 release_object( master_socket );
577 /* lock/unlock the master socket to stop accepting new clients */
578 void lock_master_socket( int locked )
580 set_select_events( &master_socket->obj, locked ? 0 : POLLIN );