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 kill_thread( thread
, PROTOCOL_ERROR
);
104 /* die on a fatal error */
105 static void fatal_error( const char *err
, ... )
109 va_start( args
, err
);
110 fprintf( stderr
, "wineserver: " );
111 vfprintf( stderr
, err
, args
);
116 /* die on a fatal error */
117 static void fatal_perror( const char *err
, ... )
121 va_start( args
, err
);
122 fprintf( stderr
, "wineserver: " );
123 vfprintf( stderr
, err
, args
);
129 /* call a request handler */
130 static void call_req_handler( struct thread
*thread
, enum request req
, int fd
)
135 if (debug_level
) trace_request( req
, fd
);
137 if (req
< REQ_NB_REQUESTS
)
139 req_handlers
[req
].handler( current
->buffer
, fd
);
140 if (current
&& !current
->wait
) send_reply( current
);
144 fatal_protocol_error( current
, "bad request %d\n", req
);
147 /* set the fd to pass to the thread */
148 void set_reply_fd( struct thread
*thread
, int pass_fd
)
150 assert( thread
->pass_fd
== -1 );
151 thread
->pass_fd
= pass_fd
;
154 /* send a reply to a thread */
155 void send_reply( struct thread
*thread
)
157 assert( !thread
->wait
);
158 if (debug_level
) trace_reply( thread
);
159 if (!write_request( thread
)) set_select_events( &thread
->obj
, POLLOUT
);
162 /* read a message from a client that has something to say */
163 void read_request( struct thread
*thread
)
168 #ifdef HAVE_MSGHDR_ACCRIGHTS
169 msghdr
.msg_accrightslen
= sizeof(int);
170 msghdr
.msg_accrights
= (void *)&thread
->pass_fd
;
171 #else /* HAVE_MSGHDR_ACCRIGHTS */
172 msghdr
.msg_control
= &cmsg
;
173 msghdr
.msg_controllen
= sizeof(cmsg
);
175 #endif /* HAVE_MSGHDR_ACCRIGHTS */
177 assert( thread
->pass_fd
== -1 );
179 myiovec
.iov_base
= (void *)&req
;
180 myiovec
.iov_len
= sizeof(req
);
182 ret
= recvmsg( thread
->obj
.fd
, &msghdr
, 0 );
183 #ifndef HAVE_MSGHDR_ACCRIGHTS
184 thread
->pass_fd
= cmsg
.fd
;
187 if (ret
== sizeof(req
))
189 int pass_fd
= thread
->pass_fd
;
190 thread
->pass_fd
= -1;
191 call_req_handler( thread
, req
, pass_fd
);
192 if (pass_fd
!= -1) close( pass_fd
);
198 kill_thread( thread
, BROKEN_PIPE
);
201 if (!ret
) /* closed pipe */
203 kill_thread( thread
, BROKEN_PIPE
);
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
) );
217 if (ret
== sizeof(thread
->error
)) goto ok
;
219 else /* we have an fd to send */
221 #ifdef HAVE_MSGHDR_ACCRIGHTS
222 msghdr
.msg_accrightslen
= sizeof(int);
223 msghdr
.msg_accrights
= (void *)&thread
->pass_fd
;
224 #else /* HAVE_MSGHDR_ACCRIGHTS */
225 msghdr
.msg_control
= &cmsg
;
226 msghdr
.msg_controllen
= sizeof(cmsg
);
227 cmsg
.fd
= thread
->pass_fd
;
228 #endif /* HAVE_MSGHDR_ACCRIGHTS */
230 myiovec
.iov_base
= (void *)&thread
->error
;
231 myiovec
.iov_len
= sizeof(thread
->error
);
233 ret
= sendmsg( thread
->obj
.fd
, &msghdr
, 0 );
234 close( thread
->pass_fd
);
235 thread
->pass_fd
= -1;
236 if (ret
== sizeof(thread
->error
)) goto ok
;
240 if (errno
== EWOULDBLOCK
) return 0; /* not a fatal error */
241 if (errno
!= EPIPE
) perror("sendmsg");
243 else fprintf( stderr
, "Partial message sent %d/%d\n", ret
, sizeof(thread
->error
) );
244 kill_thread( thread
, BROKEN_PIPE
);
248 set_select_events( &thread
->obj
, POLLIN
);
252 static void master_socket_dump( struct object
*obj
, int verbose
)
254 struct master_socket
*sock
= (struct master_socket
*)obj
;
255 assert( obj
->ops
== &master_socket_ops
);
256 fprintf( stderr
, "Master socket fd=%d\n", sock
->obj
.fd
);
259 /* handle a socket event */
260 static void master_socket_poll_event( struct object
*obj
, int event
)
262 struct master_socket
*sock
= (struct master_socket
*)obj
;
263 assert( obj
->ops
== &master_socket_ops
);
265 assert( sock
== master_socket
); /* there is only one master socket */
267 if (event
& (POLLERR
| POLLHUP
))
269 /* this is not supposed to happen */
270 fprintf( stderr
, "wineserver: Error on master socket\n" );
271 release_object( obj
);
273 else if (event
& POLLIN
)
275 struct sockaddr_un dummy
;
276 int len
= sizeof(dummy
);
277 int client
= accept( master_socket
->obj
.fd
, &dummy
, &len
);
278 if (client
!= -1) create_process( client
, NULL
, NULL
, "", 1 );
282 /* remove the socket upon exit */
283 static void socket_cleanup(void)
285 unlink( SOCKETNAME
);
288 static void master_socket_destroy( struct object
*obj
)
293 /* return the configuration directory ($HOME/.wine) */
294 static const char *get_config_dir(void)
296 static char *confdir
;
299 const char *home
= getenv( "HOME" );
302 struct passwd
*pwd
= getpwuid( getuid() );
303 if (!pwd
) fatal_error( "could not find your home directory\n" );
306 if (!(confdir
= malloc( strlen(home
) + strlen(CONFDIR
) + 1 )))
307 fatal_error( "out of memory\n" );
308 strcpy( confdir
, home
);
309 strcat( confdir
, CONFDIR
);
310 mkdir( confdir
, 0755 ); /* just in case */
315 /* create the server directory and chdir to it */
316 static void create_server_dir(void)
320 const char *confdir
= get_config_dir();
323 if (gethostname( hostname
, sizeof(hostname
) ) == -1) fatal_perror( "gethostname" );
325 if (!(serverdir
= malloc( strlen(confdir
) + strlen(SERVERDIR
) + strlen(hostname
) + 1 )))
326 fatal_error( "out of memory\n" );
328 strcpy( serverdir
, confdir
);
329 strcat( serverdir
, SERVERDIR
);
330 strcat( serverdir
, hostname
);
332 if (chdir( serverdir
) == -1)
334 if (errno
!= ENOENT
) fatal_perror( "chdir %s", serverdir
);
335 if (mkdir( serverdir
, 0700 ) == -1) fatal_perror( "mkdir %s", serverdir
);
336 if (chdir( serverdir
) == -1) fatal_perror( "chdir %s", serverdir
);
338 if (stat( ".", &st
) == -1) fatal_perror( "stat %s", serverdir
);
339 if (!S_ISDIR(st
.st_mode
)) fatal_error( "%s is not a directory\n", serverdir
);
340 if (st
.st_uid
!= getuid()) fatal_error( "%s is not owned by you\n", serverdir
);
341 if (st
.st_mode
& 077) fatal_error( "%s must not be accessible by other users\n", serverdir
);
344 /* open the master server socket and start waiting for new clients */
345 void open_master_socket(void)
347 struct sockaddr_un addr
;
351 if ((fd
= socket( AF_UNIX
, SOCK_STREAM
, 0 )) == -1) fatal_perror( "socket" );
352 addr
.sun_family
= AF_UNIX
;
353 strcpy( addr
.sun_path
, SOCKETNAME
);
354 slen
= sizeof(addr
) - sizeof(addr
.sun_path
) + strlen(addr
.sun_path
) + 1;
355 #ifdef HAVE_SOCKADDR_SUN_LEN
358 if (bind( fd
, (struct sockaddr
*)&addr
, slen
) == -1)
360 if ((errno
== EEXIST
) || (errno
== EADDRINUSE
))
361 fatal_error( "another server is already running\n" );
363 fatal_perror( "bind" );
365 atexit( socket_cleanup
);
367 chmod( SOCKETNAME
, 0600 ); /* make sure no other user can connect */
368 if (listen( fd
, 5 ) == -1) fatal_perror( "listen" );
370 if (!(master_socket
= alloc_object( &master_socket_ops
, fd
)))
371 fatal_error( "out of memory\n" );
372 set_select_events( &master_socket
->obj
, POLLIN
);
375 /* close the master socket and stop waiting for new clients */
376 void close_master_socket(void)
378 release_object( master_socket
);
381 /* lock/unlock the master socket to stop accepting new clients */
382 void lock_master_socket( int locked
)
384 set_select_events( &master_socket
->obj
, locked
? 0 : POLLIN
);
387 /* debugger support operations */
388 DECL_HANDLER(debugger
)
392 case DEBUGGER_FREEZE_ALL
:
393 suspend_all_threads();
396 case DEBUGGER_UNFREEZE_ALL
:
397 resume_all_threads();