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 assert (thread
->pass_fd
== -1);
209 if (debug_level
) trace_reply( thread
, request
);
211 request
->header
.error
= thread
->error
;
213 if ((ret
= write( thread
->reply_fd
, request
, sizeof(*request
) )) != sizeof(*request
))
216 fatal_protocol_error( thread
, "partial write %d\n", ret
);
217 else if (errno
== EPIPE
)
218 kill_thread( thread
, 0 ); /* normal death */
220 fatal_protocol_perror( thread
, "sendmsg" );
224 /* read a message from a client that has something to say */
225 void read_request( struct thread
*thread
)
227 union generic_request req
;
230 #ifdef HAVE_MSGHDR_ACCRIGHTS
231 msghdr
.msg_accrightslen
= sizeof(int);
232 msghdr
.msg_accrights
= (void *)&thread
->pass_fd
;
233 #else /* HAVE_MSGHDR_ACCRIGHTS */
234 msghdr
.msg_control
= &cmsg
;
235 msghdr
.msg_controllen
= sizeof(cmsg
);
237 #endif /* HAVE_MSGHDR_ACCRIGHTS */
239 assert( thread
->pass_fd
== -1 );
241 myiovec
.iov_base
= &req
;
242 myiovec
.iov_len
= sizeof(req
);
244 ret
= recvmsg( thread
->obj
.fd
, &msghdr
, 0 );
245 #ifndef HAVE_MSGHDR_ACCRIGHTS
246 thread
->pass_fd
= cmsg
.fd
;
249 if (ret
== sizeof(req
))
251 call_req_handler( thread
, &req
);
252 thread
->pass_fd
= -1;
255 if (!ret
) /* closed pipe */
256 kill_thread( thread
, 0 );
258 fatal_protocol_error( thread
, "partial recvmsg %d\n", ret
);
260 fatal_protocol_perror( thread
, "recvmsg" );
263 /* send the wakeup signal to a thread */
264 int send_thread_wakeup( struct thread
*thread
, int signaled
)
266 int ret
= write( thread
->wait_fd
, &signaled
, sizeof(signaled
) );
267 if (ret
== sizeof(signaled
)) return 0;
269 fatal_protocol_error( thread
, "partial wakeup write %d\n", ret
);
270 else if (errno
== EPIPE
)
271 kill_thread( thread
, 0 ); /* normal death */
273 fatal_protocol_perror( thread
, "write" );
277 /* send an fd to a client */
278 int send_client_fd( struct thread
*thread
, int fd
, handle_t handle
)
283 fprintf( stderr
, "%08x: *fd* %d = %d\n", (unsigned int)thread
, handle
, fd
);
285 #ifdef HAVE_MSGHDR_ACCRIGHTS
286 msghdr
.msg_accrightslen
= sizeof(fd
);
287 msghdr
.msg_accrights
= (void *)&fd
;
288 #else /* HAVE_MSGHDR_ACCRIGHTS */
289 msghdr
.msg_control
= &cmsg
;
290 msghdr
.msg_controllen
= sizeof(cmsg
);
292 #endif /* HAVE_MSGHDR_ACCRIGHTS */
294 myiovec
.iov_base
= (void *)&handle
;
295 myiovec
.iov_len
= sizeof(handle
);
297 ret
= sendmsg( thread
->obj
.fd
, &msghdr
, 0 );
299 if (ret
> 0) return 0;
302 kill_thread( thread
, 0 ); /* normal death */
306 fatal_protocol_perror( thread
, "sendmsg" );
311 static void master_socket_dump( struct object
*obj
, int verbose
)
313 struct master_socket
*sock
= (struct master_socket
*)obj
;
314 assert( obj
->ops
== &master_socket_ops
);
315 fprintf( stderr
, "Master socket fd=%d\n", sock
->obj
.fd
);
318 /* handle a socket event */
319 static void master_socket_poll_event( struct object
*obj
, int event
)
321 struct master_socket
*sock
= (struct master_socket
*)obj
;
322 assert( obj
->ops
== &master_socket_ops
);
324 assert( sock
== master_socket
); /* there is only one master socket */
326 if (event
& (POLLERR
| POLLHUP
))
328 /* this is not supposed to happen */
329 fprintf( stderr
, "wineserver: Error on master socket\n" );
330 release_object( obj
);
332 else if (event
& POLLIN
)
334 struct sockaddr_un dummy
;
335 int len
= sizeof(dummy
);
336 int client
= accept( master_socket
->obj
.fd
, (struct sockaddr
*) &dummy
, &len
);
337 if (client
!= -1) create_process( client
);
341 /* remove the socket upon exit */
342 static void socket_cleanup(void)
344 static int do_it_once
;
345 if (!do_it_once
++) unlink( SOCKETNAME
);
348 static void master_socket_destroy( struct object
*obj
)
353 static void request_socket_dump( struct object
*obj
, int verbose
)
355 struct request_socket
*sock
= (struct request_socket
*)obj
;
356 assert( obj
->ops
== &request_socket_ops
);
357 fprintf( stderr
, "Request socket fd=%d thread=%p\n", sock
->obj
.fd
, sock
->thread
);
360 /* handle a request socket event */
361 static void request_socket_poll_event( struct object
*obj
, int event
)
363 struct request_socket
*sock
= (struct request_socket
*)obj
;
364 assert( obj
->ops
== &request_socket_ops
);
366 if (event
& (POLLERR
| POLLHUP
)) kill_thread( sock
->thread
, 0 );
367 else if (event
& POLLIN
)
369 struct thread
*thread
= sock
->thread
;
370 union generic_request req
;
373 if ((ret
= read( sock
->obj
.fd
, &req
, sizeof(req
) )) == sizeof(req
))
375 call_req_handler( thread
, &req
);
378 if (!ret
) /* closed pipe */
379 kill_thread( thread
, 0 );
381 fatal_protocol_error( thread
, "partial read %d\n", ret
);
383 fatal_protocol_perror( thread
, "read" );
387 /* create a request socket and send the fd to the client thread */
388 struct object
*create_request_socket( struct thread
*thread
)
390 struct request_socket
*sock
;
393 if (pipe( fd
)) return NULL
;
394 if (!(sock
= alloc_object( &request_socket_ops
, fd
[0] )))
399 sock
->thread
= thread
;
400 send_client_fd( thread
, fd
[1], 0 );
402 fcntl( fd
[0], F_SETFL
, O_NONBLOCK
);
403 set_select_events( &sock
->obj
, POLLIN
);
407 /* return the configuration directory ($WINEPREFIX or $HOME/.wine) */
408 const char *get_config_dir(void)
410 static char *confdir
;
413 const char *prefix
= getenv( "WINEPREFIX" );
416 int len
= strlen(prefix
);
417 if (!(confdir
= strdup( prefix
))) fatal_error( "out of memory\n" );
418 if (len
> 1 && confdir
[len
-1] == '/') confdir
[len
-1] = 0;
422 const char *home
= getenv( "HOME" );
425 struct passwd
*pwd
= getpwuid( getuid() );
426 if (!pwd
) fatal_error( "could not find your home directory\n" );
429 if (!(confdir
= malloc( strlen(home
) + strlen(CONFDIR
) + 1 )))
430 fatal_error( "out of memory\n" );
431 strcpy( confdir
, home
);
432 strcat( confdir
, CONFDIR
);
434 mkdir( confdir
, 0755 ); /* just in case */
439 /* create the server directory and chdir to it */
440 static void create_server_dir(void)
444 const char *confdir
= get_config_dir();
447 if (gethostname( hostname
, sizeof(hostname
) ) == -1) fatal_perror( "gethostname" );
449 if (!(serverdir
= malloc( strlen(confdir
) + strlen(SERVERDIR
) + strlen(hostname
) + 1 )))
450 fatal_error( "out of memory\n" );
452 strcpy( serverdir
, confdir
);
453 strcat( serverdir
, SERVERDIR
);
454 strcat( serverdir
, hostname
);
456 if (chdir( serverdir
) == -1)
458 if (errno
!= ENOENT
) fatal_perror( "chdir %s", serverdir
);
459 if (mkdir( serverdir
, 0700 ) == -1) fatal_perror( "mkdir %s", serverdir
);
460 if (chdir( serverdir
) == -1) fatal_perror( "chdir %s", serverdir
);
462 if (stat( ".", &st
) == -1) fatal_perror( "stat %s", serverdir
);
463 if (!S_ISDIR(st
.st_mode
)) fatal_error( "%s is not a directory\n", serverdir
);
464 if (st
.st_uid
!= getuid()) fatal_error( "%s is not owned by you\n", serverdir
);
465 if (st
.st_mode
& 077) fatal_error( "%s must not be accessible by other users\n", serverdir
);
468 /* open the master server socket and start waiting for new clients */
469 void open_master_socket(void)
471 struct sockaddr_un addr
;
474 /* make sure no request is larger than the maximum size */
475 assert( sizeof(union generic_request
) == sizeof(struct request_max_size
) );
478 if ((fd
= socket( AF_UNIX
, SOCK_STREAM
, 0 )) == -1) fatal_perror( "socket" );
479 addr
.sun_family
= AF_UNIX
;
480 strcpy( addr
.sun_path
, SOCKETNAME
);
481 slen
= sizeof(addr
) - sizeof(addr
.sun_path
) + strlen(addr
.sun_path
) + 1;
482 #ifdef HAVE_SOCKADDR_SUN_LEN
485 if (bind( fd
, (struct sockaddr
*)&addr
, slen
) == -1)
487 if ((errno
== EEXIST
) || (errno
== EADDRINUSE
))
488 exit(0); /* pretend we succeeded to start */
490 fatal_perror( "bind" );
492 atexit( socket_cleanup
);
494 chmod( SOCKETNAME
, 0600 ); /* make sure no other user can connect */
495 if (listen( fd
, 5 ) == -1) fatal_perror( "listen" );
497 if (!(master_socket
= alloc_object( &master_socket_ops
, fd
)))
498 fatal_error( "out of memory\n" );
499 set_select_events( &master_socket
->obj
, POLLIN
);
501 /* setup msghdr structure constant fields */
502 msghdr
.msg_name
= NULL
;
503 msghdr
.msg_namelen
= 0;
504 msghdr
.msg_iov
= &myiovec
;
505 msghdr
.msg_iovlen
= 1;
507 /* go in the background */
511 fatal_perror( "fork" );
516 _exit(0); /* do not call atexit functions */
520 /* close the master socket and stop waiting for new clients */
521 void close_master_socket(void)
523 /* if a new client is waiting, we keep on running */
524 if (!check_select_events( master_socket
->obj
.fd
, POLLIN
))
525 release_object( master_socket
);
528 /* lock/unlock the master socket to stop accepting new clients */
529 void lock_master_socket( int locked
)
531 set_select_events( &master_socket
->obj
, locked
? 0 : POLLIN
);