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
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_read_fd
, /* get_read_fd */
67 no_write_fd
, /* get_write_fd */
69 no_get_file_info
, /* get_file_info */
70 master_socket_destroy
/* destroy */
74 struct thread
*current
= NULL
; /* thread handling the current request */
76 static struct master_socket
*master_socket
; /* the master socket object */
78 /* socket communication static structures */
79 static struct iovec myiovec
;
80 static struct msghdr msghdr
= { NULL
, 0, &myiovec
, 1, };
81 #ifndef HAVE_MSGHDR_ACCRIGHTS
84 int len
; /* sizeof structure */
85 int level
; /* SOL_SOCKET */
86 int type
; /* SCM_RIGHTS */
87 int fd
; /* fd to pass */
89 static struct cmsg_fd cmsg
= { sizeof(cmsg
), SOL_SOCKET
, SCM_RIGHTS
, -1 };
90 #endif /* HAVE_MSGHDR_ACCRIGHTS */
92 /* complain about a protocol error and terminate the client connection */
93 void fatal_protocol_error( struct thread
*thread
, const char *err
, ... )
97 va_start( args
, err
);
98 fprintf( stderr
, "Protocol error:%p: ", thread
);
99 vfprintf( stderr
, err
, args
);
101 thread
->exit_code
= 1;
102 kill_thread( thread
, 1 );
105 /* die on a fatal error */
106 static void fatal_error( const char *err
, ... )
110 va_start( args
, err
);
111 fprintf( stderr
, "wineserver: " );
112 vfprintf( stderr
, err
, args
);
117 /* die on a fatal error */
118 static void fatal_perror( const char *err
, ... )
122 va_start( args
, err
);
123 fprintf( stderr
, "wineserver: " );
124 vfprintf( stderr
, err
, args
);
130 /* call a request handler */
131 static inline void call_req_handler( struct thread
*thread
, enum request req
)
136 if (debug_level
) trace_request( req
);
138 if (req
< REQ_NB_REQUESTS
)
140 req_handlers
[req
]( current
->buffer
);
141 if (current
&& !current
->wait
) send_reply( current
);
145 fatal_protocol_error( current
, "bad request %d\n", req
);
148 /* set the fd to pass to the thread */
149 void set_reply_fd( struct thread
*thread
, int pass_fd
)
151 assert( thread
->pass_fd
== -1 );
152 thread
->pass_fd
= pass_fd
;
155 /* send a reply to a thread */
156 void send_reply( struct thread
*thread
)
158 assert( !thread
->wait
);
159 if (debug_level
) trace_reply( thread
);
160 if (!write_request( thread
)) set_select_events( &thread
->obj
, POLLOUT
);
163 /* read a message from a client that has something to say */
164 void read_request( struct thread
*thread
)
169 #ifdef HAVE_MSGHDR_ACCRIGHTS
170 msghdr
.msg_accrightslen
= sizeof(int);
171 msghdr
.msg_accrights
= (void *)&thread
->pass_fd
;
172 #else /* HAVE_MSGHDR_ACCRIGHTS */
173 msghdr
.msg_control
= &cmsg
;
174 msghdr
.msg_controllen
= sizeof(cmsg
);
176 #endif /* HAVE_MSGHDR_ACCRIGHTS */
178 assert( thread
->pass_fd
== -1 );
180 myiovec
.iov_base
= (void *)&req
;
181 myiovec
.iov_len
= sizeof(req
);
183 ret
= recvmsg( thread
->obj
.fd
, &msghdr
, 0 );
184 #ifndef HAVE_MSGHDR_ACCRIGHTS
185 thread
->pass_fd
= cmsg
.fd
;
188 if (ret
== sizeof(req
))
190 call_req_handler( thread
, req
);
191 thread
->pass_fd
= -1;
197 thread
->exit_code
= 1;
198 kill_thread( thread
, 1 );
201 if (!ret
) /* closed pipe */
203 kill_thread( thread
, 0 );
206 fatal_protocol_error( thread
, "partial message received %d/%d\n", ret
, sizeof(req
) );
209 /* send a message to a client that is ready to receive something */
210 int write_request( struct thread
*thread
)
214 if (thread
->pass_fd
== -1)
216 ret
= write( thread
->obj
.fd
, &thread
->error
, sizeof(thread
->error
) );
218 else /* we have an fd to send */
220 #ifdef HAVE_MSGHDR_ACCRIGHTS
221 msghdr
.msg_accrightslen
= sizeof(int);
222 msghdr
.msg_accrights
= (void *)&thread
->pass_fd
;
223 #else /* HAVE_MSGHDR_ACCRIGHTS */
224 msghdr
.msg_control
= &cmsg
;
225 msghdr
.msg_controllen
= sizeof(cmsg
);
226 cmsg
.fd
= thread
->pass_fd
;
227 #endif /* HAVE_MSGHDR_ACCRIGHTS */
229 myiovec
.iov_base
= (void *)&thread
->error
;
230 myiovec
.iov_len
= sizeof(thread
->error
);
232 ret
= sendmsg( thread
->obj
.fd
, &msghdr
, 0 );
233 close( thread
->pass_fd
);
234 thread
->pass_fd
= -1;
236 if (ret
== sizeof(thread
->error
))
238 set_select_events( &thread
->obj
, POLLIN
);
243 if (errno
== EWOULDBLOCK
) return 0; /* not a fatal error */
246 kill_thread( thread
, 0 ); /* normal death */
251 thread
->exit_code
= 1;
252 kill_thread( thread
, 1 );
257 thread
->exit_code
= 1;
258 kill_thread( thread
, 1 );
259 fprintf( stderr
, "Partial message sent %d/%d\n", ret
, sizeof(thread
->error
) );
264 static void master_socket_dump( struct object
*obj
, int verbose
)
266 struct master_socket
*sock
= (struct master_socket
*)obj
;
267 assert( obj
->ops
== &master_socket_ops
);
268 fprintf( stderr
, "Master socket fd=%d\n", sock
->obj
.fd
);
271 /* handle a socket event */
272 static void master_socket_poll_event( struct object
*obj
, int event
)
274 struct master_socket
*sock
= (struct master_socket
*)obj
;
275 assert( obj
->ops
== &master_socket_ops
);
277 assert( sock
== master_socket
); /* there is only one master socket */
279 if (event
& (POLLERR
| POLLHUP
))
281 /* this is not supposed to happen */
282 fprintf( stderr
, "wineserver: Error on master socket\n" );
283 release_object( obj
);
285 else if (event
& POLLIN
)
287 struct sockaddr_un dummy
;
288 int len
= sizeof(dummy
);
289 int client
= accept( master_socket
->obj
.fd
, (struct sockaddr
*) &dummy
, &len
);
290 if (client
!= -1) create_process( client
, NULL
, NULL
, "", 1 );
294 /* remove the socket upon exit */
295 static void socket_cleanup(void)
297 unlink( SOCKETNAME
);
300 static void master_socket_destroy( struct object
*obj
)
305 /* return the configuration directory ($HOME/.wine) */
306 static const char *get_config_dir(void)
308 static char *confdir
;
311 const char *home
= getenv( "HOME" );
314 struct passwd
*pwd
= getpwuid( getuid() );
315 if (!pwd
) fatal_error( "could not find your home directory\n" );
318 if (!(confdir
= malloc( strlen(home
) + strlen(CONFDIR
) + 1 )))
319 fatal_error( "out of memory\n" );
320 strcpy( confdir
, home
);
321 strcat( confdir
, CONFDIR
);
322 mkdir( confdir
, 0755 ); /* just in case */
327 /* create the server directory and chdir to it */
328 static void create_server_dir(void)
332 const char *confdir
= get_config_dir();
335 if (gethostname( hostname
, sizeof(hostname
) ) == -1) fatal_perror( "gethostname" );
337 if (!(serverdir
= malloc( strlen(confdir
) + strlen(SERVERDIR
) + strlen(hostname
) + 1 )))
338 fatal_error( "out of memory\n" );
340 strcpy( serverdir
, confdir
);
341 strcat( serverdir
, SERVERDIR
);
342 strcat( serverdir
, hostname
);
344 if (chdir( serverdir
) == -1)
346 if (errno
!= ENOENT
) fatal_perror( "chdir %s", serverdir
);
347 if (mkdir( serverdir
, 0700 ) == -1) fatal_perror( "mkdir %s", serverdir
);
348 if (chdir( serverdir
) == -1) fatal_perror( "chdir %s", serverdir
);
350 if (stat( ".", &st
) == -1) fatal_perror( "stat %s", serverdir
);
351 if (!S_ISDIR(st
.st_mode
)) fatal_error( "%s is not a directory\n", serverdir
);
352 if (st
.st_uid
!= getuid()) fatal_error( "%s is not owned by you\n", serverdir
);
353 if (st
.st_mode
& 077) fatal_error( "%s must not be accessible by other users\n", serverdir
);
356 /* open the master server socket and start waiting for new clients */
357 void open_master_socket(void)
359 struct sockaddr_un addr
;
363 if ((fd
= socket( AF_UNIX
, SOCK_STREAM
, 0 )) == -1) fatal_perror( "socket" );
364 addr
.sun_family
= AF_UNIX
;
365 strcpy( addr
.sun_path
, SOCKETNAME
);
366 slen
= sizeof(addr
) - sizeof(addr
.sun_path
) + strlen(addr
.sun_path
) + 1;
367 #ifdef HAVE_SOCKADDR_SUN_LEN
370 if (bind( fd
, (struct sockaddr
*)&addr
, slen
) == -1)
372 if ((errno
== EEXIST
) || (errno
== EADDRINUSE
))
373 fatal_error( "another server is already running\n" );
375 fatal_perror( "bind" );
377 atexit( socket_cleanup
);
379 chmod( SOCKETNAME
, 0600 ); /* make sure no other user can connect */
380 if (listen( fd
, 5 ) == -1) fatal_perror( "listen" );
382 if (!(master_socket
= alloc_object( &master_socket_ops
, fd
)))
383 fatal_error( "out of memory\n" );
384 set_select_events( &master_socket
->obj
, POLLIN
);
387 /* close the master socket and stop waiting for new clients */
388 void close_master_socket(void)
390 release_object( master_socket
);
393 /* lock/unlock the master socket to stop accepting new clients */
394 void lock_master_socket( int locked
)
396 set_select_events( &master_socket
->obj
, locked
? 0 : POLLIN
);