2 * Server-side request handling
4 * Copyright (C) 1998 Alexandre Julliard
21 #include <sys/types.h>
22 #ifdef HAVE_SYS_SOCKET_H
23 # 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_get_fd
, /* get_fd */
68 no_get_file_info
, /* get_file_info */
69 NULL
, /* queue_async */
70 master_socket_destroy
/* destroy */
74 struct thread
*current
= NULL
; /* thread handling the current request */
75 unsigned int global_error
= 0; /* global error code for when no thread is current */
76 unsigned int server_start_ticks
= 0; /* tick count offset from server startup */
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 /* complain about a protocol error and terminate the client connection */
108 void fatal_protocol_perror( struct thread
*thread
, const char *err
, ... )
112 va_start( args
, err
);
113 fprintf( stderr
, "Protocol error:%p: ", thread
);
114 vfprintf( stderr
, err
, args
);
117 thread
->exit_code
= 1;
118 kill_thread( thread
, 1 );
121 /* die on a fatal error */
122 void fatal_error( const char *err
, ... )
126 va_start( args
, err
);
127 fprintf( stderr
, "wineserver: " );
128 vfprintf( stderr
, err
, args
);
133 /* die on a fatal error */
134 void fatal_perror( const char *err
, ... )
138 va_start( args
, err
);
139 fprintf( stderr
, "wineserver: " );
140 vfprintf( stderr
, err
, args
);
146 /* allocate the reply data */
147 void *set_reply_data_size( size_t size
)
149 assert( size
<= get_reply_max_size() );
150 if (size
&& !(current
->reply_data
= mem_alloc( size
))) size
= 0;
151 current
->reply_size
= size
;
152 return current
->reply_data
;
155 /* write the remaining part of the reply */
156 void write_reply( struct thread
*thread
)
160 if ((ret
= write( thread
->reply_fd
,
161 (char *)thread
->reply_data
+ thread
->reply_size
- thread
->reply_towrite
,
162 thread
->reply_towrite
)) >= 0)
164 if (!(thread
->reply_towrite
-= ret
))
166 free( thread
->reply_data
);
167 thread
->reply_data
= NULL
;
168 /* sent everything, can go back to waiting for requests */
169 change_select_fd( &thread
->obj
, thread
->request_fd
, POLLIN
);
174 kill_thread( thread
, 0 ); /* normal death */
175 else if (errno
!= EWOULDBLOCK
&& errno
!= EAGAIN
)
176 fatal_protocol_perror( thread
, "reply write" );
179 /* send a reply to the current thread */
180 static void send_reply( union generic_reply
*reply
)
184 if (!current
->reply_size
)
186 if ((ret
= write( current
->reply_fd
, reply
, sizeof(*reply
) )) != sizeof(*reply
)) goto error
;
192 vec
[0].iov_base
= reply
;
193 vec
[0].iov_len
= sizeof(*reply
);
194 vec
[1].iov_base
= current
->reply_data
;
195 vec
[1].iov_len
= current
->reply_size
;
197 if ((ret
= writev( current
->reply_fd
, vec
, 2 )) < sizeof(*reply
)) goto error
;
199 if ((current
->reply_towrite
= current
->reply_size
- (ret
- sizeof(*reply
))))
201 /* couldn't write it all, wait for POLLOUT */
202 change_select_fd( ¤t
->obj
, current
->reply_fd
, POLLOUT
);
206 if (current
->reply_data
)
208 free( current
->reply_data
);
209 current
->reply_data
= NULL
;
215 fatal_protocol_error( current
, "partial write %d\n", ret
);
216 else if (errno
== EPIPE
)
217 kill_thread( current
, 0 ); /* normal death */
219 fatal_protocol_perror( current
, "reply write" );
222 /* call a request handler */
223 static void call_req_handler( struct thread
*thread
)
225 union generic_reply reply
;
226 enum request req
= thread
->req
.request_header
.req
;
229 current
->reply_size
= 0;
231 memset( &reply
, 0, sizeof(reply
) );
233 if (debug_level
) trace_request();
235 if (req
< REQ_NB_REQUESTS
)
237 req_handlers
[req
]( ¤t
->req
, &reply
);
240 reply
.reply_header
.error
= current
->error
;
241 reply
.reply_header
.reply_size
= current
->reply_size
;
242 if (debug_level
) trace_reply( req
, &reply
);
243 send_reply( &reply
);
248 fatal_protocol_error( current
, "bad request %d\n", req
);
251 /* read a request from a thread */
252 void read_request( struct thread
*thread
)
256 if (!thread
->req_toread
) /* no pending request */
258 if ((ret
= read( thread
->obj
.fd
, &thread
->req
,
259 sizeof(thread
->req
) )) != sizeof(thread
->req
)) goto error
;
260 if (!(thread
->req_toread
= thread
->req
.request_header
.request_size
))
262 /* no data, handle request at once */
263 call_req_handler( thread
);
266 if (!(thread
->req_data
= malloc( thread
->req_toread
)))
267 fatal_protocol_error( thread
, "no memory for %d bytes request\n", thread
->req_toread
);
270 /* read the variable sized data */
273 ret
= read( thread
->obj
.fd
, ((char *)thread
->req_data
+
274 thread
->req
.request_header
.request_size
- thread
->req_toread
),
275 thread
->req_toread
);
277 if (!(thread
->req_toread
-= ret
))
279 call_req_handler( thread
);
280 free( thread
->req_data
);
281 thread
->req_data
= NULL
;
287 if (!ret
) /* closed pipe */
288 kill_thread( thread
, 0 );
290 fatal_protocol_error( thread
, "partial read %d\n", ret
);
291 else if (errno
!= EWOULDBLOCK
&& errno
!= EAGAIN
)
292 fatal_protocol_perror( thread
, "read" );
295 /* receive a file descriptor on the process socket */
296 int receive_fd( struct process
*process
)
301 #ifdef HAVE_MSGHDR_ACCRIGHTS
302 msghdr
.msg_accrightslen
= sizeof(int);
303 msghdr
.msg_accrights
= (void *)&fd
;
304 #else /* HAVE_MSGHDR_ACCRIGHTS */
305 msghdr
.msg_control
= &cmsg
;
306 msghdr
.msg_controllen
= sizeof(cmsg
);
308 #endif /* HAVE_MSGHDR_ACCRIGHTS */
310 myiovec
.iov_base
= &data
;
311 myiovec
.iov_len
= sizeof(data
);
313 ret
= recvmsg( process
->obj
.fd
, &msghdr
, 0 );
314 #ifndef HAVE_MSGHDR_ACCRIGHTS
318 if (ret
== sizeof(data
))
320 struct thread
*thread
;
322 if (data
.tid
) thread
= get_thread_from_id( data
.tid
);
323 else thread
= (struct thread
*)grab_object( process
->thread_list
);
325 if (!thread
|| thread
->process
!= process
)
328 fprintf( stderr
, "%08x: *fd* %d <- %d bad thread id\n",
329 (unsigned int)data
.tid
, data
.fd
, fd
);
335 fprintf( stderr
, "%08x: *fd* %d <- %d\n",
336 (unsigned int)thread
, data
.fd
, fd
);
337 thread_add_inflight_fd( thread
, data
.fd
, fd
);
339 if (thread
) release_object( thread
);
346 fprintf( stderr
, "Protocol error: process %p: partial recvmsg %d for fd\n",
348 kill_process( process
, NULL
, 1 );
352 if (errno
!= EWOULDBLOCK
&& errno
!= EAGAIN
)
354 fprintf( stderr
, "Protocol error: process %p: ", process
);
356 kill_process( process
, NULL
, 1 );
362 /* send an fd to a client */
363 int send_client_fd( struct process
*process
, int fd
, handle_t handle
)
368 fprintf( stderr
, "%08x: *fd* %d -> %d\n", (unsigned int)current
, handle
, fd
);
370 #ifdef HAVE_MSGHDR_ACCRIGHTS
371 msghdr
.msg_accrightslen
= sizeof(fd
);
372 msghdr
.msg_accrights
= (void *)&fd
;
373 #else /* HAVE_MSGHDR_ACCRIGHTS */
374 msghdr
.msg_control
= &cmsg
;
375 msghdr
.msg_controllen
= sizeof(cmsg
);
377 #endif /* HAVE_MSGHDR_ACCRIGHTS */
379 myiovec
.iov_base
= (void *)&handle
;
380 myiovec
.iov_len
= sizeof(handle
);
382 ret
= sendmsg( process
->obj
.fd
, &msghdr
, 0 );
384 if (ret
== sizeof(handle
)) return 0;
389 fprintf( stderr
, "Protocol error: process %p: partial sendmsg %d\n", process
, ret
);
390 kill_process( process
, NULL
, 1 );
394 fprintf( stderr
, "Protocol error: process %p: ", process
);
396 kill_process( process
, NULL
, 1 );
401 /* get current tick count to return to client */
402 unsigned int get_tick_count(void)
405 gettimeofday( &t
, NULL
);
406 return (t
.tv_sec
* 1000) + (t
.tv_usec
/ 1000) - server_start_ticks
;
409 static void master_socket_dump( struct object
*obj
, int verbose
)
411 struct master_socket
*sock
= (struct master_socket
*)obj
;
412 assert( obj
->ops
== &master_socket_ops
);
413 fprintf( stderr
, "Master socket fd=%d\n", sock
->obj
.fd
);
416 /* handle a socket event */
417 static void master_socket_poll_event( struct object
*obj
, int event
)
419 struct master_socket
*sock
= (struct master_socket
*)obj
;
420 assert( obj
->ops
== &master_socket_ops
);
422 assert( sock
== master_socket
); /* there is only one master socket */
424 if (event
& (POLLERR
| POLLHUP
))
426 /* this is not supposed to happen */
427 fprintf( stderr
, "wineserver: Error on master socket\n" );
428 release_object( obj
);
430 else if (event
& POLLIN
)
432 struct sockaddr_un dummy
;
433 int len
= sizeof(dummy
);
434 int client
= accept( master_socket
->obj
.fd
, (struct sockaddr
*) &dummy
, &len
);
435 if (client
== -1) return;
436 fcntl( client
, F_SETFL
, O_NONBLOCK
);
437 create_process( client
);
441 /* remove the socket upon exit */
442 static void socket_cleanup(void)
444 static int do_it_once
;
445 if (!do_it_once
++) unlink( SOCKETNAME
);
448 static void master_socket_destroy( struct object
*obj
)
453 /* return the configuration directory ($WINEPREFIX or $HOME/.wine) */
454 const char *get_config_dir(void)
456 static char *confdir
;
459 const char *prefix
= getenv( "WINEPREFIX" );
462 int len
= strlen(prefix
);
463 if (!(confdir
= strdup( prefix
))) fatal_error( "out of memory\n" );
464 if (len
> 1 && confdir
[len
-1] == '/') confdir
[len
-1] = 0;
468 const char *home
= getenv( "HOME" );
471 struct passwd
*pwd
= getpwuid( getuid() );
472 if (!pwd
) fatal_error( "could not find your home directory\n" );
475 if (!(confdir
= malloc( strlen(home
) + strlen(CONFDIR
) + 1 )))
476 fatal_error( "out of memory\n" );
477 strcpy( confdir
, home
);
478 strcat( confdir
, CONFDIR
);
484 /* create the server directory and chdir to it */
485 static void create_server_dir(void)
489 const char *confdir
= get_config_dir();
492 if (gethostname( hostname
, sizeof(hostname
) ) == -1) fatal_perror( "gethostname" );
494 if (!(serverdir
= malloc( strlen(SERVERDIR
) + strlen(hostname
) + 1 )))
495 fatal_error( "out of memory\n" );
497 if (chdir( confdir
) == -1) fatal_perror( "chdir %s", confdir
);
499 strcpy( serverdir
, SERVERDIR
);
500 strcat( serverdir
, hostname
);
502 if (chdir( serverdir
) == -1)
504 if (errno
!= ENOENT
) fatal_perror( "chdir %s", serverdir
);
505 if (mkdir( serverdir
, 0700 ) == -1) fatal_perror( "mkdir %s", serverdir
);
506 if (chdir( serverdir
) == -1) fatal_perror( "chdir %s", serverdir
);
508 if (stat( ".", &st
) == -1) fatal_perror( "stat %s", serverdir
);
509 if (!S_ISDIR(st
.st_mode
)) fatal_error( "%s is not a directory\n", serverdir
);
510 if (st
.st_uid
!= getuid()) fatal_error( "%s is not owned by you\n", serverdir
);
511 if (st
.st_mode
& 077) fatal_error( "%s must not be accessible by other users\n", serverdir
);
514 /* open the master server socket and start waiting for new clients */
515 void open_master_socket(void)
517 struct sockaddr_un addr
;
520 /* make sure no request is larger than the maximum size */
521 assert( sizeof(union generic_request
) == sizeof(struct request_max_size
) );
522 assert( sizeof(union generic_reply
) == sizeof(struct request_max_size
) );
525 if ((fd
= socket( AF_UNIX
, SOCK_STREAM
, 0 )) == -1) fatal_perror( "socket" );
526 addr
.sun_family
= AF_UNIX
;
527 strcpy( addr
.sun_path
, SOCKETNAME
);
528 slen
= sizeof(addr
) - sizeof(addr
.sun_path
) + strlen(addr
.sun_path
) + 1;
529 #ifdef HAVE_SOCKADDR_SUN_LEN
532 if (bind( fd
, (struct sockaddr
*)&addr
, slen
) == -1)
534 if ((errno
== EEXIST
) || (errno
== EADDRINUSE
))
535 exit(0); /* pretend we succeeded to start */
537 fatal_perror( "bind" );
539 atexit( socket_cleanup
);
541 chmod( SOCKETNAME
, 0600 ); /* make sure no other user can connect */
542 if (listen( fd
, 5 ) == -1) fatal_perror( "listen" );
544 if (!(master_socket
= alloc_object( &master_socket_ops
, fd
)))
545 fatal_error( "out of memory\n" );
546 set_select_events( &master_socket
->obj
, POLLIN
);
548 /* setup msghdr structure constant fields */
549 msghdr
.msg_name
= NULL
;
550 msghdr
.msg_namelen
= 0;
551 msghdr
.msg_iov
= &myiovec
;
552 msghdr
.msg_iovlen
= 1;
554 /* init startup ticks */
555 server_start_ticks
= get_tick_count();
557 /* go in the background */
561 fatal_perror( "fork" );
566 _exit(0); /* do not call atexit functions */
570 /* close the master socket and stop waiting for new clients */
571 void close_master_socket(void)
573 /* if a new client is waiting, we keep on running */
574 if (!check_select_events( master_socket
->obj
.fd
, POLLIN
))
575 release_object( master_socket
);
578 /* lock/unlock the master socket to stop accepting new clients */
579 void lock_master_socket( int locked
)
581 set_select_events( &master_socket
->obj
, locked
? 0 : POLLIN
);