2 * Client part of the client/server communication
4 * Copyright (C) 1998 Alexandre Julliard
17 #include <sys/types.h>
18 #ifdef HAVE_SYS_SOCKET_H
19 # include <sys/socket.h>
21 #ifdef HAVE_SYS_WAIT_H
25 #ifdef HAVE_SYS_MMAN_H
33 #include "wine/port.h"
40 /* Some versions of glibc don't define this */
45 #define CONFDIR "/.wine" /* directory for Wine config relative to $HOME */
46 #define SERVERDIR "/wineserver-" /* server socket directory (hostname appended) */
47 #define SOCKETNAME "socket" /* name of the socket file */
49 #undef server_alloc_req
51 /* data structure used to pass an fd with sendmsg/recvmsg */
54 int len
; /* sizeof structure */
55 int level
; /* SOL_SOCKET */
56 int type
; /* SCM_RIGHTS */
57 int fd
; /* fd to pass */
60 static void *boot_thread_id
;
63 /* die on a fatal error; use only during initialization */
64 static void fatal_error( const char *err
, ... ) WINE_NORETURN
;
65 static void fatal_error( const char *err
, ... )
69 va_start( args
, err
);
70 fprintf( stderr
, "wine: " );
71 vfprintf( stderr
, err
, args
);
76 /* die on a fatal error; use only during initialization */
77 static void fatal_perror( const char *err
, ... ) WINE_NORETURN
;
78 static void fatal_perror( const char *err
, ... )
82 va_start( args
, err
);
83 fprintf( stderr
, "wine: " );
84 vfprintf( stderr
, err
, args
);
90 /***********************************************************************
91 * server_protocol_error
93 void server_protocol_error( const char *err
, ... )
97 va_start( args
, err
);
98 fprintf( stderr
, "Client protocol error:%p: ", NtCurrentTeb()->tid
);
99 vfprintf( stderr
, err
, args
);
101 SYSDEPS_ExitThread(1);
105 /***********************************************************************
108 static void server_perror( const char *err
)
110 fprintf( stderr
, "Client protocol error:%p: ", NtCurrentTeb()->tid
);
112 SYSDEPS_ExitThread(1);
116 /***********************************************************************
117 * __wine_server_exception_handler
119 DWORD
__wine_server_exception_handler( PEXCEPTION_RECORD record
, EXCEPTION_FRAME
*frame
,
120 CONTEXT
*context
, EXCEPTION_FRAME
**pdispatcher
)
122 struct __server_exception_frame
*server_frame
= (struct __server_exception_frame
*)frame
;
123 if ((record
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
)))
124 *NtCurrentTeb()->buffer_info
= server_frame
->info
;
125 return ExceptionContinueSearch
;
129 /***********************************************************************
130 * wine_server_alloc_req
132 void *wine_server_alloc_req( size_t fixed_size
, size_t var_size
)
134 unsigned int pos
= NtCurrentTeb()->buffer_info
->cur_pos
;
135 union generic_request
*req
= (union generic_request
*)((char *)NtCurrentTeb()->buffer
+ pos
);
136 size_t size
= sizeof(*req
) + var_size
;
138 assert( fixed_size
<= sizeof(*req
) );
139 assert( var_size
<= REQUEST_MAX_VAR_SIZE
);
141 if ((char *)req
+ size
> (char *)NtCurrentTeb()->buffer_info
)
142 server_protocol_error( "buffer overflow %d bytes\n",
143 (char *)req
+ size
- (char *)NtCurrentTeb()->buffer_info
);
144 NtCurrentTeb()->buffer_info
->cur_pos
= pos
+ size
;
145 req
->header
.fixed_size
= fixed_size
;
146 req
->header
.var_size
= var_size
;
151 /***********************************************************************
154 * Send a request to the server.
156 static void send_request( enum request req
, struct request_header
*header
)
159 NtCurrentTeb()->buffer_info
->cur_req
= (char *)header
- (char *)NtCurrentTeb()->buffer
;
160 /* write a single byte; the value is ignored anyway */
161 if (write( NtCurrentTeb()->socket
, header
, 1 ) == -1)
163 if (errno
== EPIPE
) SYSDEPS_ExitThread(0);
164 server_perror( "sendmsg" );
168 /***********************************************************************
171 * Send a request to the server, passing a file descriptor.
173 static void send_request_fd( enum request req
, struct request_header
*header
, int fd
)
175 #ifndef HAVE_MSGHDR_ACCRIGHTS
178 struct msghdr msghdr
;
181 /* write a single byte; the value is ignored anyway */
182 vec
.iov_base
= (void *)header
;
185 msghdr
.msg_name
= NULL
;
186 msghdr
.msg_namelen
= 0;
187 msghdr
.msg_iov
= &vec
;
188 msghdr
.msg_iovlen
= 1;
190 #ifdef HAVE_MSGHDR_ACCRIGHTS
191 msghdr
.msg_accrights
= (void *)&fd
;
192 msghdr
.msg_accrightslen
= sizeof(fd
);
193 #else /* HAVE_MSGHDR_ACCRIGHTS */
194 cmsg
.len
= sizeof(cmsg
);
195 cmsg
.level
= SOL_SOCKET
;
196 cmsg
.type
= SCM_RIGHTS
;
198 msghdr
.msg_control
= &cmsg
;
199 msghdr
.msg_controllen
= sizeof(cmsg
);
200 msghdr
.msg_flags
= 0;
201 #endif /* HAVE_MSGHDR_ACCRIGHTS */
205 if (sendmsg( NtCurrentTeb()->socket
, &msghdr
, 0 ) == -1)
207 if (errno
== EPIPE
) SYSDEPS_ExitThread(0);
208 server_perror( "sendmsg" );
212 /***********************************************************************
215 * Wait for a reply from the server.
217 static void wait_reply(void)
224 if ((ret
= read( NtCurrentTeb()->socket
, dummy
, 1 )) > 0) return;
226 if (errno
== EINTR
) continue;
227 if (errno
== EPIPE
) break;
228 server_perror("read");
230 /* the server closed the connection; time to die... */
231 SYSDEPS_ExitThread(0);
235 /***********************************************************************
238 * Wait for a reply from the server, when a file descriptor is passed.
240 static void wait_reply_fd( int *fd
)
246 #ifdef HAVE_MSGHDR_ACCRIGHTS
247 struct msghdr msghdr
;
250 msghdr
.msg_accrights
= (void *)fd
;
251 msghdr
.msg_accrightslen
= sizeof(*fd
);
252 #else /* HAVE_MSGHDR_ACCRIGHTS */
253 struct msghdr msghdr
;
256 cmsg
.len
= sizeof(cmsg
);
257 cmsg
.level
= SOL_SOCKET
;
258 cmsg
.type
= SCM_RIGHTS
;
260 msghdr
.msg_control
= &cmsg
;
261 msghdr
.msg_controllen
= sizeof(cmsg
);
262 msghdr
.msg_flags
= 0;
263 #endif /* HAVE_MSGHDR_ACCRIGHTS */
265 msghdr
.msg_name
= NULL
;
266 msghdr
.msg_namelen
= 0;
267 msghdr
.msg_iov
= &vec
;
268 msghdr
.msg_iovlen
= 1;
269 vec
.iov_base
= (void *)dummy
;
274 if ((ret
= recvmsg( NtCurrentTeb()->socket
, &msghdr
, 0 )) > 0)
276 #ifndef HAVE_MSGHDR_ACCRIGHTS
282 if (errno
== EINTR
) continue;
283 if (errno
== EPIPE
) break;
284 server_perror("recvmsg");
286 /* the server closed the connection; time to die... */
287 SYSDEPS_ExitThread(0);
291 /***********************************************************************
294 * Perform a server call.
296 unsigned int wine_server_call( enum request req
)
298 void *req_ptr
= get_req_buffer();
299 send_request( req
, req_ptr
);
301 return ((struct request_header
*)req_ptr
)->error
;
305 /***********************************************************************
308 * Perform a server call, passing a file descriptor.
309 * If *fd is != -1, it will be passed to the server.
310 * If the server passes an fd, it will be stored into *fd.
312 unsigned int server_call_fd( enum request req
, int fd_out
, int *fd_in
)
315 void *req_ptr
= get_req_buffer();
317 if (fd_out
== -1) send_request( req
, req_ptr
);
318 else send_request_fd( req
, req_ptr
, fd_out
);
320 if (fd_in
) wait_reply_fd( fd_in
);
323 if ((res
= ((struct request_header
*)req_ptr
)->error
))
324 SetLastError( RtlNtStatusToDosError(res
) );
325 return res
; /* error code */
329 /***********************************************************************
332 * Return the configuration directory ($WINEPREFIX or $HOME/.wine)
334 const char *get_config_dir(void)
336 static char *confdir
;
339 const char *prefix
= getenv( "WINEPREFIX" );
342 int len
= strlen(prefix
);
343 if (!(confdir
= strdup( prefix
))) fatal_error( "out of memory\n" );
344 if (len
> 1 && confdir
[len
-1] == '/') confdir
[len
-1] = 0;
348 const char *home
= getenv( "HOME" );
351 struct passwd
*pwd
= getpwuid( getuid() );
352 if (!pwd
) fatal_error( "could not find your home directory\n" );
355 if (!(confdir
= malloc( strlen(home
) + strlen(CONFDIR
) + 1 )))
356 fatal_error( "out of memory\n" );
357 strcpy( confdir
, home
);
358 strcat( confdir
, CONFDIR
);
360 mkdir( confdir
, 0755 ); /* just in case */
366 /***********************************************************************
369 * Start a new wine server.
371 static void start_server( const char *oldcwd
)
373 static int started
; /* we only try once */
379 if (pid
== -1) fatal_perror( "fork" );
382 /* if server is explicitly specified, use this */
383 if ((p
= getenv("WINESERVER")))
385 execl( p
, "wineserver", NULL
);
386 fatal_perror( "could not exec the server '%s'\n"
387 " specified in the WINESERVER environment variable", p
);
390 /* first try the installation dir */
391 execl( BINDIR
"/wineserver", "wineserver", NULL
);
393 /* now try the dir we were launched from */
396 if (!(path
= malloc( strlen(full_argv0
) + 20 )))
397 fatal_error( "out of memory\n" );
398 if ((p
= strrchr( strcpy( path
, full_argv0
), '/' )))
400 strcpy( p
, "/wineserver" );
401 execl( path
, "wineserver", NULL
);
402 strcpy( p
, "/server/wineserver" );
403 execl( path
, "wineserver", NULL
);
408 /* now try the path */
409 execlp( "wineserver", "wineserver", NULL
);
411 /* and finally the current dir */
412 if (!(path
= malloc( strlen(oldcwd
) + 20 )))
413 fatal_error( "out of memory\n" );
414 p
= strcpy( path
, oldcwd
) + strlen( oldcwd
);
415 strcpy( p
, "/wineserver" );
416 execl( path
, "wineserver", NULL
);
417 strcpy( p
, "/server/wineserver" );
418 execl( path
, "wineserver", NULL
);
420 fatal_error( "could not exec wineserver\n" );
423 waitpid( pid
, &status
, 0 );
424 status
= WIFEXITED(status
) ? WEXITSTATUS(status
) : 1;
425 if (status
) exit(status
); /* server failed */
429 /***********************************************************************
432 * Attempt to connect to an existing server socket.
433 * We need to be in the server directory already.
435 static int server_connect( const char *oldcwd
, const char *serverdir
)
437 struct sockaddr_un addr
;
441 /* chdir to the server directory */
442 if (chdir( serverdir
) == -1)
444 if (errno
!= ENOENT
) fatal_perror( "chdir to %s", serverdir
);
446 if (chdir( serverdir
) == -1) fatal_perror( "chdir to %s", serverdir
);
449 /* make sure we are at the right place */
450 if (stat( ".", &st
) == -1) fatal_perror( "stat %s", serverdir
);
451 if (st
.st_uid
!= getuid()) fatal_error( "'%s' is not owned by you\n", serverdir
);
452 if (st
.st_mode
& 077) fatal_error( "'%s' must not be accessible by other users\n", serverdir
);
454 for (retry
= 0; retry
< 3; retry
++)
456 /* if not the first try, wait a bit to leave the server time to exit */
457 if (retry
) usleep( 100000 * retry
* retry
);
459 /* check for an existing socket */
460 if (lstat( SOCKETNAME
, &st
) == -1)
462 if (errno
!= ENOENT
) fatal_perror( "lstat %s/%s", serverdir
, SOCKETNAME
);
463 start_server( oldcwd
);
464 if (lstat( SOCKETNAME
, &st
) == -1) fatal_perror( "lstat %s/%s", serverdir
, SOCKETNAME
);
467 /* make sure the socket is sane (ISFIFO needed for Solaris) */
468 if (!S_ISSOCK(st
.st_mode
) && !S_ISFIFO(st
.st_mode
))
469 fatal_error( "'%s/%s' is not a socket\n", serverdir
, SOCKETNAME
);
470 if (st
.st_uid
!= getuid())
471 fatal_error( "'%s/%s' is not owned by you\n", serverdir
, SOCKETNAME
);
473 /* try to connect to it */
474 addr
.sun_family
= AF_UNIX
;
475 strcpy( addr
.sun_path
, SOCKETNAME
);
476 slen
= sizeof(addr
) - sizeof(addr
.sun_path
) + strlen(addr
.sun_path
) + 1;
477 #ifdef HAVE_SOCKADDR_SUN_LEN
480 if ((s
= socket( AF_UNIX
, SOCK_STREAM
, 0 )) == -1) fatal_perror( "socket" );
481 if (connect( s
, (struct sockaddr
*)&addr
, slen
) != -1)
483 fcntl( s
, F_SETFD
, 1 ); /* set close on exec flag */
488 fatal_error( "file '%s/%s' exists,\n"
489 " but I cannot connect to it; maybe the server has crashed?\n"
490 " If this is the case, you should remove this socket file and try again.\n",
491 serverdir
, SOCKETNAME
);
495 /***********************************************************************
498 * Start the server and create the initial socket pair.
500 int CLIENT_InitServer(void)
504 char *oldcwd
, *serverdir
;
505 const char *configdir
;
507 /* retrieve the current directory */
508 for (size
= 512; ; size
*= 2)
510 if (!(oldcwd
= malloc( size
))) break;
511 if (getcwd( oldcwd
, size
)) break;
513 if (errno
== ERANGE
) continue;
518 /* if argv[0] is a relative path, make it absolute */
520 if (oldcwd
&& argv0
[0] != '/' && strchr( argv0
, '/' ))
522 char *new_argv0
= malloc( strlen(oldcwd
) + strlen(argv0
) + 2 );
525 strcpy( new_argv0
, oldcwd
);
526 strcat( new_argv0
, "/" );
527 strcat( new_argv0
, argv0
);
528 full_argv0
= new_argv0
;
532 /* get the server directory name */
533 if (gethostname( hostname
, sizeof(hostname
) ) == -1) fatal_perror( "gethostname" );
534 configdir
= get_config_dir();
535 serverdir
= malloc( strlen(configdir
) + strlen(SERVERDIR
) + strlen(hostname
) + 1 );
536 if (!serverdir
) fatal_error( "out of memory\n" );
537 strcpy( serverdir
, configdir
);
538 strcat( serverdir
, SERVERDIR
);
539 strcat( serverdir
, hostname
);
541 /* connect to the server */
542 fd
= server_connect( oldcwd
, serverdir
);
544 /* switch back to the starting directory */
554 /***********************************************************************
557 * Send an init thread request. Return 0 if OK.
559 int CLIENT_InitThread(void)
561 struct get_thread_buffer_request
*req
;
562 TEB
*teb
= NtCurrentTeb();
565 /* ignore SIGPIPE so that we get a EPIPE error instead */
566 signal( SIGPIPE
, SIG_IGN
);
568 wait_reply_fd( &fd
);
569 if (fd
== -1) server_protocol_error( "no fd passed on first request\n" );
571 if ((size
= lseek( fd
, 0, SEEK_END
)) == -1) server_perror( "lseek" );
572 teb
->buffer
= mmap( 0, size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0 );
574 if (teb
->buffer
== (void*)-1) server_perror( "mmap" );
575 teb
->buffer_info
= (struct server_buffer_info
*)((char *)teb
->buffer
+ size
) - 1;
577 req
= (struct get_thread_buffer_request
*)teb
->buffer
;
580 if (req
->version
!= SERVER_PROTOCOL_VERSION
)
581 server_protocol_error( "version mismatch %d/%d.\n"
582 "Your %s binary was not upgraded correctly,\n"
583 "or you have an older one somewhere in your PATH.\n",
584 req
->version
, SERVER_PROTOCOL_VERSION
,
585 (req
->version
> SERVER_PROTOCOL_VERSION
) ? "wine" : "wineserver" );
586 if (req
->boot
) boot_thread_id
= teb
->tid
;
587 else if (boot_thread_id
== teb
->tid
) boot_thread_id
= 0;
591 struct init_thread_request
*req
= wine_server_alloc_req( sizeof(*req
), 0 );
592 req
->unix_pid
= getpid();
594 req
->entry
= teb
->entry_point
;
595 ret
= wine_server_call( REQ_INIT_THREAD
);
601 /***********************************************************************
604 * Signal that we have finished booting, and set debug level.
606 int CLIENT_BootDone( int debug_level
)
611 struct boot_done_request
*req
= wine_server_alloc_req( sizeof(*req
), 0 );
612 req
->debug_level
= debug_level
;
613 ret
= server_call( REQ_BOOT_DONE
);
620 /***********************************************************************
621 * CLIENT_IsBootThread
623 * Return TRUE if current thread is the boot thread.
625 int CLIENT_IsBootThread(void)
627 return (GetCurrentThreadId() == (DWORD
)boot_thread_id
);