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 /* complain about a protocol error and terminate the client connection */
133 void fatal_protocol_perror( struct thread
*thread
, const char *err
, ... )
137 va_start( args
, err
);
138 fprintf( stderr
, "Protocol error:%p: ", thread
);
139 vfprintf( stderr
, err
, args
);
142 thread
->exit_code
= 1;
143 kill_thread( thread
, 1 );
146 /* die on a fatal error */
147 void fatal_error( const char *err
, ... )
151 va_start( args
, err
);
152 fprintf( stderr
, "wineserver: " );
153 vfprintf( stderr
, err
, args
);
158 /* die on a fatal error */
159 void fatal_perror( const char *err
, ... )
163 va_start( args
, err
);
164 fprintf( stderr
, "wineserver: " );
165 vfprintf( stderr
, err
, args
);
171 /* call a request handler */
172 static inline void call_req_handler( struct thread
*thread
, union generic_request
*request
)
174 enum request req
= request
->header
.req
;
179 if (debug_level
) trace_request( thread
, request
);
181 if (request
->header
.var_size
)
183 if ((unsigned int)request
->header
.var_offset
+
184 request
->header
.var_size
> MAX_REQUEST_LENGTH
)
186 fatal_protocol_error( current
, "bad request offset/size %d/%d\n",
187 request
->header
.var_offset
, request
->header
.var_size
);
192 if (req
< REQ_NB_REQUESTS
)
194 req_handlers
[req
]( request
);
195 if (current
) send_reply( current
, request
);
199 fatal_protocol_error( current
, "bad request %d\n", req
);
202 /* send a reply to a thread */
203 void send_reply( struct thread
*thread
, union generic_request
*request
)
207 if (debug_level
) trace_reply( thread
, request
);
209 request
->header
.error
= thread
->error
;
211 if ((ret
= write( thread
->reply_fd
, request
, sizeof(*request
) )) != sizeof(*request
))
214 fatal_protocol_error( thread
, "partial write %d\n", ret
);
215 else if (errno
== EPIPE
)
216 kill_thread( thread
, 0 ); /* normal death */
218 fatal_protocol_perror( thread
, "sendmsg" );
222 /* receive a file descriptor on the process socket */
223 int receive_fd( struct process
*process
)
228 #ifdef HAVE_MSGHDR_ACCRIGHTS
229 msghdr
.msg_accrightslen
= sizeof(int);
230 msghdr
.msg_accrights
= (void *)&fd
;
231 #else /* HAVE_MSGHDR_ACCRIGHTS */
232 msghdr
.msg_control
= &cmsg
;
233 msghdr
.msg_controllen
= sizeof(cmsg
);
235 #endif /* HAVE_MSGHDR_ACCRIGHTS */
237 myiovec
.iov_base
= &data
;
238 myiovec
.iov_len
= sizeof(data
);
240 ret
= recvmsg( process
->obj
.fd
, &msghdr
, 0 );
241 #ifndef HAVE_MSGHDR_ACCRIGHTS
245 if (ret
== sizeof(data
))
247 struct thread
*thread
= get_thread_from_id( data
.tid
);
248 if (!thread
|| thread
->process
!= process
)
251 fprintf( stderr
, "%08x: *fd* %d <- %d bad thread id\n",
252 (unsigned int)data
.tid
, data
.fd
, fd
);
258 fprintf( stderr
, "%08x: *fd* %d <- %d\n",
259 (unsigned int)thread
, data
.fd
, fd
);
260 thread_add_inflight_fd( thread
, data
.fd
, fd
);
267 set_select_events( &process
->obj
, -1 ); /* stop waiting on it */
271 fprintf( stderr
, "Protocol error: process %p: partial recvmsg %d for fd\n", process
, ret
);
272 kill_process( process
, NULL
, 1 );
276 if (errno
!= EWOULDBLOCK
&& errno
!= EAGAIN
)
278 fprintf( stderr
, "Protocol error: process %p: ", process
);
280 kill_process( process
, NULL
, 1 );
286 /* send the wakeup signal to a thread */
287 int send_thread_wakeup( struct thread
*thread
, int signaled
)
289 int ret
= write( thread
->wait_fd
, &signaled
, sizeof(signaled
) );
290 if (ret
== sizeof(signaled
)) return 0;
292 fatal_protocol_error( thread
, "partial wakeup write %d\n", ret
);
293 else if (errno
== EPIPE
)
294 kill_thread( thread
, 0 ); /* normal death */
296 fatal_protocol_perror( thread
, "write" );
300 /* send an fd to a client */
301 int send_client_fd( struct thread
*thread
, int fd
, handle_t handle
)
306 fprintf( stderr
, "%08x: *fd* %d -> %d\n", (unsigned int)thread
, handle
, fd
);
308 #ifdef HAVE_MSGHDR_ACCRIGHTS
309 msghdr
.msg_accrightslen
= sizeof(fd
);
310 msghdr
.msg_accrights
= (void *)&fd
;
311 #else /* HAVE_MSGHDR_ACCRIGHTS */
312 msghdr
.msg_control
= &cmsg
;
313 msghdr
.msg_controllen
= sizeof(cmsg
);
315 #endif /* HAVE_MSGHDR_ACCRIGHTS */
317 myiovec
.iov_base
= (void *)&handle
;
318 myiovec
.iov_len
= sizeof(handle
);
320 ret
= sendmsg( thread
->obj
.fd
, &msghdr
, 0 );
322 if (ret
> 0) return 0;
325 kill_thread( thread
, 0 ); /* normal death */
329 fatal_protocol_perror( thread
, "sendmsg" );
334 static void master_socket_dump( struct object
*obj
, int verbose
)
336 struct master_socket
*sock
= (struct master_socket
*)obj
;
337 assert( obj
->ops
== &master_socket_ops
);
338 fprintf( stderr
, "Master socket fd=%d\n", sock
->obj
.fd
);
341 /* handle a socket event */
342 static void master_socket_poll_event( struct object
*obj
, int event
)
344 struct master_socket
*sock
= (struct master_socket
*)obj
;
345 assert( obj
->ops
== &master_socket_ops
);
347 assert( sock
== master_socket
); /* there is only one master socket */
349 if (event
& (POLLERR
| POLLHUP
))
351 /* this is not supposed to happen */
352 fprintf( stderr
, "wineserver: Error on master socket\n" );
353 release_object( obj
);
355 else if (event
& POLLIN
)
357 struct sockaddr_un dummy
;
358 int len
= sizeof(dummy
);
359 int client
= accept( master_socket
->obj
.fd
, (struct sockaddr
*) &dummy
, &len
);
360 if (client
!= -1) create_process( client
);
364 /* remove the socket upon exit */
365 static void socket_cleanup(void)
367 static int do_it_once
;
368 if (!do_it_once
++) unlink( SOCKETNAME
);
371 static void master_socket_destroy( struct object
*obj
)
376 static void request_socket_dump( struct object
*obj
, int verbose
)
378 struct request_socket
*sock
= (struct request_socket
*)obj
;
379 assert( obj
->ops
== &request_socket_ops
);
380 fprintf( stderr
, "Request socket fd=%d thread=%p\n", sock
->obj
.fd
, sock
->thread
);
383 /* handle a request socket event */
384 static void request_socket_poll_event( struct object
*obj
, int event
)
386 struct request_socket
*sock
= (struct request_socket
*)obj
;
387 assert( obj
->ops
== &request_socket_ops
);
389 if (event
& (POLLERR
| POLLHUP
)) kill_thread( sock
->thread
, 0 );
390 else if (event
& POLLIN
)
392 struct thread
*thread
= sock
->thread
;
393 union generic_request req
;
396 if ((ret
= read( sock
->obj
.fd
, &req
, sizeof(req
) )) == sizeof(req
))
398 call_req_handler( thread
, &req
);
401 if (!ret
) /* closed pipe */
402 kill_thread( thread
, 0 );
404 fatal_protocol_error( thread
, "partial read %d\n", ret
);
406 fatal_protocol_perror( thread
, "read" );
410 /* create a request socket and send the fd to the client thread */
411 struct object
*create_request_socket( struct thread
*thread
)
413 struct request_socket
*sock
;
416 if (pipe( fd
)) return NULL
;
417 if (!(sock
= alloc_object( &request_socket_ops
, fd
[0] )))
422 sock
->thread
= thread
;
423 send_client_fd( thread
, fd
[1], 0 );
425 fcntl( fd
[0], F_SETFL
, O_NONBLOCK
);
426 set_select_events( &sock
->obj
, POLLIN
);
430 /* return the configuration directory ($WINEPREFIX or $HOME/.wine) */
431 const char *get_config_dir(void)
433 static char *confdir
;
436 const char *prefix
= getenv( "WINEPREFIX" );
439 int len
= strlen(prefix
);
440 if (!(confdir
= strdup( prefix
))) fatal_error( "out of memory\n" );
441 if (len
> 1 && confdir
[len
-1] == '/') confdir
[len
-1] = 0;
445 const char *home
= getenv( "HOME" );
448 struct passwd
*pwd
= getpwuid( getuid() );
449 if (!pwd
) fatal_error( "could not find your home directory\n" );
452 if (!(confdir
= malloc( strlen(home
) + strlen(CONFDIR
) + 1 )))
453 fatal_error( "out of memory\n" );
454 strcpy( confdir
, home
);
455 strcat( confdir
, CONFDIR
);
457 mkdir( confdir
, 0755 ); /* just in case */
462 /* create the server directory and chdir to it */
463 static void create_server_dir(void)
467 const char *confdir
= get_config_dir();
470 if (gethostname( hostname
, sizeof(hostname
) ) == -1) fatal_perror( "gethostname" );
472 if (!(serverdir
= malloc( strlen(confdir
) + strlen(SERVERDIR
) + strlen(hostname
) + 1 )))
473 fatal_error( "out of memory\n" );
475 strcpy( serverdir
, confdir
);
476 strcat( serverdir
, SERVERDIR
);
477 strcat( serverdir
, hostname
);
479 if (chdir( serverdir
) == -1)
481 if (errno
!= ENOENT
) fatal_perror( "chdir %s", serverdir
);
482 if (mkdir( serverdir
, 0700 ) == -1) fatal_perror( "mkdir %s", serverdir
);
483 if (chdir( serverdir
) == -1) fatal_perror( "chdir %s", serverdir
);
485 if (stat( ".", &st
) == -1) fatal_perror( "stat %s", serverdir
);
486 if (!S_ISDIR(st
.st_mode
)) fatal_error( "%s is not a directory\n", serverdir
);
487 if (st
.st_uid
!= getuid()) fatal_error( "%s is not owned by you\n", serverdir
);
488 if (st
.st_mode
& 077) fatal_error( "%s must not be accessible by other users\n", serverdir
);
491 /* open the master server socket and start waiting for new clients */
492 void open_master_socket(void)
494 struct sockaddr_un addr
;
497 /* make sure no request is larger than the maximum size */
498 assert( sizeof(union generic_request
) == sizeof(struct request_max_size
) );
501 if ((fd
= socket( AF_UNIX
, SOCK_STREAM
, 0 )) == -1) fatal_perror( "socket" );
502 addr
.sun_family
= AF_UNIX
;
503 strcpy( addr
.sun_path
, SOCKETNAME
);
504 slen
= sizeof(addr
) - sizeof(addr
.sun_path
) + strlen(addr
.sun_path
) + 1;
505 #ifdef HAVE_SOCKADDR_SUN_LEN
508 if (bind( fd
, (struct sockaddr
*)&addr
, slen
) == -1)
510 if ((errno
== EEXIST
) || (errno
== EADDRINUSE
))
511 exit(0); /* pretend we succeeded to start */
513 fatal_perror( "bind" );
515 atexit( socket_cleanup
);
517 chmod( SOCKETNAME
, 0600 ); /* make sure no other user can connect */
518 if (listen( fd
, 5 ) == -1) fatal_perror( "listen" );
520 if (!(master_socket
= alloc_object( &master_socket_ops
, fd
)))
521 fatal_error( "out of memory\n" );
522 set_select_events( &master_socket
->obj
, POLLIN
);
524 /* setup msghdr structure constant fields */
525 msghdr
.msg_name
= NULL
;
526 msghdr
.msg_namelen
= 0;
527 msghdr
.msg_iov
= &myiovec
;
528 msghdr
.msg_iovlen
= 1;
530 /* go in the background */
534 fatal_perror( "fork" );
539 _exit(0); /* do not call atexit functions */
543 /* close the master socket and stop waiting for new clients */
544 void close_master_socket(void)
546 /* if a new client is waiting, we keep on running */
547 if (!check_select_events( master_socket
->obj
.fd
, POLLIN
))
548 release_object( master_socket
);
551 /* lock/unlock the master socket to stop accepting new clients */
552 void lock_master_socket( int locked
)
554 set_select_events( &master_socket
->obj
, locked
? 0 : POLLIN
);