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_read_fd
, /* get_read_fd */
68 no_write_fd
, /* get_write_fd */
70 no_get_file_info
, /* get_file_info */
71 master_socket_destroy
/* destroy */
75 struct thread
*current
= NULL
; /* thread handling the current request */
76 unsigned int global_error
= 0; /* global error code for when no thread is current */
78 static struct master_socket
*master_socket
; /* the master socket object */
80 /* socket communication static structures */
81 static struct iovec myiovec
;
82 static struct msghdr msghdr
;
83 #ifndef HAVE_MSGHDR_ACCRIGHTS
86 int len
; /* sizeof structure */
87 int level
; /* SOL_SOCKET */
88 int type
; /* SCM_RIGHTS */
89 int fd
; /* fd to pass */
91 static struct cmsg_fd cmsg
= { sizeof(cmsg
), SOL_SOCKET
, SCM_RIGHTS
, -1 };
92 #endif /* HAVE_MSGHDR_ACCRIGHTS */
94 /* complain about a protocol error and terminate the client connection */
95 void fatal_protocol_error( struct thread
*thread
, const char *err
, ... )
99 va_start( args
, err
);
100 fprintf( stderr
, "Protocol error:%p: ", thread
);
101 vfprintf( stderr
, err
, args
);
103 thread
->exit_code
= 1;
104 kill_thread( thread
, 1 );
107 /* die on a fatal error */
108 void fatal_error( const char *err
, ... )
112 va_start( args
, err
);
113 fprintf( stderr
, "wineserver: " );
114 vfprintf( stderr
, err
, args
);
119 /* die on a fatal error */
120 void fatal_perror( const char *err
, ... )
124 va_start( args
, err
);
125 fprintf( stderr
, "wineserver: " );
126 vfprintf( stderr
, err
, args
);
132 /* call a request handler */
133 static inline void call_req_handler( struct thread
*thread
)
139 req
= ((struct request_header
*)current
->buffer
)->req
;
141 if (debug_level
) trace_request( req
);
143 if (req
< REQ_NB_REQUESTS
)
145 req_handlers
[req
]( current
->buffer
);
146 if (current
&& !current
->wait
) send_reply( current
);
150 fatal_protocol_error( current
, "bad request %d\n", req
);
153 /* set the fd to pass to the thread */
154 void set_reply_fd( struct thread
*thread
, int pass_fd
)
156 assert( thread
->pass_fd
== -1 );
157 thread
->pass_fd
= pass_fd
;
160 /* send a reply to a thread */
161 void send_reply( struct thread
*thread
)
163 assert( !thread
->wait
);
164 if (debug_level
) trace_reply( thread
);
165 if (!write_request( thread
)) set_select_events( &thread
->obj
, POLLOUT
);
168 /* read a message from a client that has something to say */
169 void read_request( struct thread
*thread
)
174 #ifdef HAVE_MSGHDR_ACCRIGHTS
175 msghdr
.msg_accrightslen
= sizeof(int);
176 msghdr
.msg_accrights
= (void *)&thread
->pass_fd
;
177 #else /* HAVE_MSGHDR_ACCRIGHTS */
178 msghdr
.msg_control
= &cmsg
;
179 msghdr
.msg_controllen
= sizeof(cmsg
);
181 #endif /* HAVE_MSGHDR_ACCRIGHTS */
183 assert( thread
->pass_fd
== -1 );
185 myiovec
.iov_base
= dummy
;
188 ret
= recvmsg( thread
->obj
.fd
, &msghdr
, 0 );
189 #ifndef HAVE_MSGHDR_ACCRIGHTS
190 thread
->pass_fd
= cmsg
.fd
;
195 call_req_handler( thread
);
196 thread
->pass_fd
= -1;
199 if (!ret
) /* closed pipe */
201 kill_thread( thread
, 0 );
205 thread
->exit_code
= 1;
206 kill_thread( thread
, 1 );
209 /* send a message to a client that is ready to receive something */
210 int write_request( struct thread
*thread
)
213 struct request_header
*header
= thread
->buffer
;
215 header
->error
= thread
->error
;
217 if (thread
->pass_fd
== -1)
219 /* write a single byte; the value is ignored anyway */
220 ret
= write( thread
->obj
.fd
, header
, 1 );
222 else /* we have an fd to send */
224 #ifdef HAVE_MSGHDR_ACCRIGHTS
225 msghdr
.msg_accrightslen
= sizeof(int);
226 msghdr
.msg_accrights
= (void *)&thread
->pass_fd
;
227 #else /* HAVE_MSGHDR_ACCRIGHTS */
228 msghdr
.msg_control
= &cmsg
;
229 msghdr
.msg_controllen
= sizeof(cmsg
);
230 cmsg
.fd
= thread
->pass_fd
;
231 #endif /* HAVE_MSGHDR_ACCRIGHTS */
233 myiovec
.iov_base
= (void *)header
;
236 ret
= sendmsg( thread
->obj
.fd
, &msghdr
, 0 );
237 close( thread
->pass_fd
);
238 thread
->pass_fd
= -1;
242 set_select_events( &thread
->obj
, POLLIN
);
245 if (errno
== EWOULDBLOCK
) return 0; /* not a fatal error */
248 kill_thread( thread
, 0 ); /* normal death */
253 thread
->exit_code
= 1;
254 kill_thread( thread
, 1 );
259 static void master_socket_dump( struct object
*obj
, int verbose
)
261 struct master_socket
*sock
= (struct master_socket
*)obj
;
262 assert( obj
->ops
== &master_socket_ops
);
263 fprintf( stderr
, "Master socket fd=%d\n", sock
->obj
.fd
);
266 /* handle a socket event */
267 static void master_socket_poll_event( struct object
*obj
, int event
)
269 struct master_socket
*sock
= (struct master_socket
*)obj
;
270 assert( obj
->ops
== &master_socket_ops
);
272 assert( sock
== master_socket
); /* there is only one master socket */
274 if (event
& (POLLERR
| POLLHUP
))
276 /* this is not supposed to happen */
277 fprintf( stderr
, "wineserver: Error on master socket\n" );
278 release_object( obj
);
280 else if (event
& POLLIN
)
282 struct sockaddr_un dummy
;
283 int len
= sizeof(dummy
);
284 int client
= accept( master_socket
->obj
.fd
, (struct sockaddr
*) &dummy
, &len
);
285 if (client
!= -1) create_process( client
);
289 /* remove the socket upon exit */
290 static void socket_cleanup(void)
292 static int do_it_once
;
293 if (!do_it_once
++) unlink( SOCKETNAME
);
296 static void master_socket_destroy( struct object
*obj
)
301 /* return the configuration directory ($WINEPREFIX or $HOME/.wine) */
302 const char *get_config_dir(void)
304 static char *confdir
;
307 const char *prefix
= getenv( "WINEPREFIX" );
310 int len
= strlen(prefix
);
311 if (!(confdir
= strdup( prefix
))) fatal_error( "out of memory\n" );
312 if (len
> 1 && confdir
[len
-1] == '/') confdir
[len
-1] = 0;
316 const char *home
= getenv( "HOME" );
319 struct passwd
*pwd
= getpwuid( getuid() );
320 if (!pwd
) fatal_error( "could not find your home directory\n" );
323 if (!(confdir
= malloc( strlen(home
) + strlen(CONFDIR
) + 1 )))
324 fatal_error( "out of memory\n" );
325 strcpy( confdir
, home
);
326 strcat( confdir
, CONFDIR
);
328 mkdir( confdir
, 0755 ); /* just in case */
333 /* create the server directory and chdir to it */
334 static void create_server_dir(void)
338 const char *confdir
= get_config_dir();
341 if (gethostname( hostname
, sizeof(hostname
) ) == -1) fatal_perror( "gethostname" );
343 if (!(serverdir
= malloc( strlen(confdir
) + strlen(SERVERDIR
) + strlen(hostname
) + 1 )))
344 fatal_error( "out of memory\n" );
346 strcpy( serverdir
, confdir
);
347 strcat( serverdir
, SERVERDIR
);
348 strcat( serverdir
, hostname
);
350 if (chdir( serverdir
) == -1)
352 if (errno
!= ENOENT
) fatal_perror( "chdir %s", serverdir
);
353 if (mkdir( serverdir
, 0700 ) == -1) fatal_perror( "mkdir %s", serverdir
);
354 if (chdir( serverdir
) == -1) fatal_perror( "chdir %s", serverdir
);
356 if (stat( ".", &st
) == -1) fatal_perror( "stat %s", serverdir
);
357 if (!S_ISDIR(st
.st_mode
)) fatal_error( "%s is not a directory\n", serverdir
);
358 if (st
.st_uid
!= getuid()) fatal_error( "%s is not owned by you\n", serverdir
);
359 if (st
.st_mode
& 077) fatal_error( "%s must not be accessible by other users\n", serverdir
);
362 /* open the master server socket and start waiting for new clients */
363 void open_master_socket(void)
365 struct sockaddr_un addr
;
369 if ((fd
= socket( AF_UNIX
, SOCK_STREAM
, 0 )) == -1) fatal_perror( "socket" );
370 addr
.sun_family
= AF_UNIX
;
371 strcpy( addr
.sun_path
, SOCKETNAME
);
372 slen
= sizeof(addr
) - sizeof(addr
.sun_path
) + strlen(addr
.sun_path
) + 1;
373 #ifdef HAVE_SOCKADDR_SUN_LEN
376 if (bind( fd
, (struct sockaddr
*)&addr
, slen
) == -1)
378 if ((errno
== EEXIST
) || (errno
== EADDRINUSE
))
379 exit(0); /* pretend we succeeded to start */
381 fatal_perror( "bind" );
383 atexit( socket_cleanup
);
385 chmod( SOCKETNAME
, 0600 ); /* make sure no other user can connect */
386 if (listen( fd
, 5 ) == -1) fatal_perror( "listen" );
388 if (!(master_socket
= alloc_object( &master_socket_ops
, fd
)))
389 fatal_error( "out of memory\n" );
390 set_select_events( &master_socket
->obj
, POLLIN
);
392 /* setup msghdr structure constant fields */
393 msghdr
.msg_name
= NULL
;
394 msghdr
.msg_namelen
= 0;
395 msghdr
.msg_iov
= &myiovec
;
396 msghdr
.msg_iovlen
= 1;
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
);