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"
35 #include "wine/server.h"
39 /* Some versions of glibc don't define this */
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 */
48 /* data structure used to pass an fd with sendmsg/recvmsg */
51 int len
; /* sizeof structure */
52 int level
; /* SOL_SOCKET */
53 int type
; /* SCM_RIGHTS */
54 int fd
; /* fd to pass */
57 static void *boot_thread_id
;
58 static sigset_t block_set
; /* signals to block during server calls */
59 static int fd_socket
; /* socket to exchange file descriptors with the server */
61 /* die on a fatal error; use only during initialization */
62 static void fatal_error( const char *err
, ... ) WINE_NORETURN
;
63 static void fatal_error( const char *err
, ... )
67 va_start( args
, err
);
68 fprintf( stderr
, "wine: " );
69 vfprintf( stderr
, err
, args
);
74 /* die on a fatal error; use only during initialization */
75 static void fatal_perror( const char *err
, ... ) WINE_NORETURN
;
76 static void fatal_perror( const char *err
, ... )
80 va_start( args
, err
);
81 fprintf( stderr
, "wine: " );
82 vfprintf( stderr
, err
, args
);
88 /***********************************************************************
89 * server_protocol_error
91 void server_protocol_error( const char *err
, ... )
95 va_start( args
, err
);
96 fprintf( stderr
, "wine client error:%p: ", NtCurrentTeb()->tid
);
97 vfprintf( stderr
, err
, args
);
99 SYSDEPS_ExitThread(1);
103 /***********************************************************************
104 * server_protocol_perror
106 void server_protocol_perror( const char *err
)
108 fprintf( stderr
, "wine client error:%p: ", NtCurrentTeb()->tid
);
110 SYSDEPS_ExitThread(1);
114 /***********************************************************************
115 * __wine_server_exception_handler (NTDLL.@)
117 DWORD
__wine_server_exception_handler( PEXCEPTION_RECORD record
, EXCEPTION_FRAME
*frame
,
118 CONTEXT
*context
, EXCEPTION_FRAME
**pdispatcher
)
120 struct __server_exception_frame
*server_frame
= (struct __server_exception_frame
*)frame
;
121 if ((record
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
)))
122 NtCurrentTeb()->buffer_pos
= server_frame
->buffer_pos
;
123 return ExceptionContinueSearch
;
127 /***********************************************************************
128 * wine_server_alloc_req (NTDLL.@)
130 void wine_server_alloc_req( union generic_request
*req
, size_t size
)
132 unsigned int pos
= NtCurrentTeb()->buffer_pos
;
134 assert( size
<= REQUEST_MAX_VAR_SIZE
);
136 if (pos
+ size
> NtCurrentTeb()->buffer_size
)
137 server_protocol_error( "buffer overflow %d bytes\n",
138 pos
+ size
- NtCurrentTeb()->buffer_pos
);
140 NtCurrentTeb()->buffer_pos
= pos
+ size
;
141 req
->header
.var_offset
= pos
;
142 req
->header
.var_size
= size
;
146 /***********************************************************************
149 * Send a request to the server.
151 static void send_request( union generic_request
*request
)
155 if ((ret
= write( NtCurrentTeb()->request_fd
, request
, sizeof(*request
) )) == sizeof(*request
))
157 if (ret
>= 0) server_protocol_error( "partial write %d\n", ret
);
158 if (errno
== EPIPE
) SYSDEPS_ExitThread(0);
159 server_protocol_perror( "sendmsg" );
162 /***********************************************************************
165 * Wait for a reply from the server.
167 static void wait_reply( union generic_request
*req
)
173 if ((ret
= read( NtCurrentTeb()->reply_fd
, req
, sizeof(*req
) )) == sizeof(*req
))
176 if (ret
> 0) server_protocol_error( "partial read %d\n", ret
);
177 if (errno
== EINTR
) continue;
178 if (errno
== EPIPE
) break;
179 server_protocol_perror("read");
181 /* the server closed the connection; time to die... */
182 SYSDEPS_ExitThread(0);
186 /***********************************************************************
187 * wine_server_call (NTDLL.@)
189 * Perform a server call.
191 unsigned int wine_server_call( union generic_request
*req
, size_t size
)
195 memset( (char *)req
+ size
, 0, sizeof(*req
) - size
);
196 sigprocmask( SIG_BLOCK
, &block_set
, &old_set
);
199 sigprocmask( SIG_SETMASK
, &old_set
, NULL
);
200 return req
->header
.error
;
204 /***********************************************************************
205 * wine_server_send_fd
207 * Send a file descriptor to the server.
209 void wine_server_send_fd( int fd
)
211 #ifndef HAVE_MSGHDR_ACCRIGHTS
215 struct msghdr msghdr
;
219 vec
.iov_base
= (void *)&data
;
220 vec
.iov_len
= sizeof(data
);
222 msghdr
.msg_name
= NULL
;
223 msghdr
.msg_namelen
= 0;
224 msghdr
.msg_iov
= &vec
;
225 msghdr
.msg_iovlen
= 1;
227 #ifdef HAVE_MSGHDR_ACCRIGHTS
228 msghdr
.msg_accrights
= (void *)&fd
;
229 msghdr
.msg_accrightslen
= sizeof(fd
);
230 #else /* HAVE_MSGHDR_ACCRIGHTS */
231 cmsg
.len
= sizeof(cmsg
);
232 cmsg
.level
= SOL_SOCKET
;
233 cmsg
.type
= SCM_RIGHTS
;
235 msghdr
.msg_control
= &cmsg
;
236 msghdr
.msg_controllen
= sizeof(cmsg
);
237 msghdr
.msg_flags
= 0;
238 #endif /* HAVE_MSGHDR_ACCRIGHTS */
240 data
.tid
= (void *)GetCurrentThreadId();
245 if ((ret
= sendmsg( fd_socket
, &msghdr
, 0 )) == sizeof(data
)) return;
246 if (ret
>= 0) server_protocol_error( "partial write %d\n", ret
);
247 if (errno
== EINTR
) continue;
248 if (errno
== EPIPE
) SYSDEPS_ExitThread(0);
249 server_protocol_perror( "sendmsg" );
254 /***********************************************************************
257 * Receive a file descriptor passed from the server.
259 static int receive_fd( handle_t
*handle
)
264 #ifdef HAVE_MSGHDR_ACCRIGHTS
265 struct msghdr msghdr
;
268 msghdr
.msg_accrights
= (void *)&fd
;
269 msghdr
.msg_accrightslen
= sizeof(fd
);
270 #else /* HAVE_MSGHDR_ACCRIGHTS */
271 struct msghdr msghdr
;
274 cmsg
.len
= sizeof(cmsg
);
275 cmsg
.level
= SOL_SOCKET
;
276 cmsg
.type
= SCM_RIGHTS
;
278 msghdr
.msg_control
= &cmsg
;
279 msghdr
.msg_controllen
= sizeof(cmsg
);
280 msghdr
.msg_flags
= 0;
281 #endif /* HAVE_MSGHDR_ACCRIGHTS */
283 msghdr
.msg_name
= NULL
;
284 msghdr
.msg_namelen
= 0;
285 msghdr
.msg_iov
= &vec
;
286 msghdr
.msg_iovlen
= 1;
287 vec
.iov_base
= (void *)handle
;
288 vec
.iov_len
= sizeof(*handle
);
292 if ((ret
= recvmsg( fd_socket
, &msghdr
, 0 )) > 0)
294 #ifndef HAVE_MSGHDR_ACCRIGHTS
297 if (fd
== -1) server_protocol_error( "no fd received for handle %d\n", *handle
);
298 fcntl( fd
, F_SETFD
, 1 ); /* set close on exec flag */
302 if (errno
== EINTR
) continue;
303 if (errno
== EPIPE
) break;
304 server_protocol_perror("recvmsg");
306 /* the server closed the connection; time to die... */
307 SYSDEPS_ExitThread(0);
311 /***********************************************************************
312 * wine_server_recv_fd
314 * Receive a file descriptor passed from the server.
315 * The file descriptor must not be closed.
316 * Return -2 if a race condition stole our file descriptor.
318 int wine_server_recv_fd( handle_t handle
)
322 int fd
= receive_fd( &fd_handle
);
324 /* now store it in the server fd cache for this handle */
326 SERVER_START_REQ( set_handle_info
)
328 req
->handle
= fd_handle
;
334 if (req
->cur_fd
!= fd
)
336 /* someone was here before us */
349 if (handle
!= fd_handle
) fd
= -2; /* not the one we expected */
354 /***********************************************************************
357 * Return the configuration directory ($WINEPREFIX or $HOME/.wine)
359 const char *get_config_dir(void)
361 static char *confdir
;
364 const char *prefix
= getenv( "WINEPREFIX" );
367 int len
= strlen(prefix
);
368 if (!(confdir
= strdup( prefix
))) fatal_error( "out of memory\n" );
369 if (len
> 1 && confdir
[len
-1] == '/') confdir
[len
-1] = 0;
373 const char *home
= getenv( "HOME" );
376 struct passwd
*pwd
= getpwuid( getuid() );
377 if (!pwd
) fatal_error( "could not find your home directory\n" );
380 if (!(confdir
= malloc( strlen(home
) + strlen(CONFDIR
) + 1 )))
381 fatal_error( "out of memory\n" );
382 strcpy( confdir
, home
);
383 strcat( confdir
, CONFDIR
);
390 /***********************************************************************
393 * Start a new wine server.
395 static void start_server( const char *oldcwd
)
397 static int started
; /* we only try once */
403 if (pid
== -1) fatal_perror( "fork" );
406 /* if server is explicitly specified, use this */
407 if ((p
= getenv("WINESERVER")))
409 if (p
[0] != '/' && oldcwd
[0] == '/') /* make it an absolute path */
411 if (!(path
= malloc( strlen(oldcwd
) + strlen(p
) + 1 )))
412 fatal_error( "out of memory\n" );
413 sprintf( path
, "%s/%s", oldcwd
, p
);
416 execl( p
, "wineserver", NULL
);
417 fatal_perror( "could not exec the server '%s'\n"
418 " specified in the WINESERVER environment variable", p
);
421 /* first try the installation dir */
422 execl( BINDIR
"/wineserver", "wineserver", NULL
);
424 /* now try the dir we were launched from */
427 if (!(path
= malloc( strlen(full_argv0
) + 20 )))
428 fatal_error( "out of memory\n" );
429 if ((p
= strrchr( strcpy( path
, full_argv0
), '/' )))
431 strcpy( p
, "/wineserver" );
432 execl( path
, "wineserver", NULL
);
433 strcpy( p
, "/server/wineserver" );
434 execl( path
, "wineserver", NULL
);
439 /* now try the path */
440 execlp( "wineserver", "wineserver", NULL
);
442 /* and finally the current dir */
443 if (!(path
= malloc( strlen(oldcwd
) + 20 )))
444 fatal_error( "out of memory\n" );
445 p
= strcpy( path
, oldcwd
) + strlen( oldcwd
);
446 strcpy( p
, "/wineserver" );
447 execl( path
, "wineserver", NULL
);
448 strcpy( p
, "/server/wineserver" );
449 execl( path
, "wineserver", NULL
);
451 fatal_error( "could not exec wineserver\n" );
454 waitpid( pid
, &status
, 0 );
455 status
= WIFEXITED(status
) ? WEXITSTATUS(status
) : 1;
456 if (status
) exit(status
); /* server failed */
460 /***********************************************************************
463 * Attempt to connect to an existing server socket.
464 * We need to be in the server directory already.
466 static int server_connect( const char *oldcwd
, const char *serverdir
)
468 struct sockaddr_un addr
;
472 /* chdir to the server directory */
473 if (chdir( serverdir
) == -1)
475 if (errno
!= ENOENT
) fatal_perror( "chdir to %s", serverdir
);
477 if (chdir( serverdir
) == -1) fatal_perror( "chdir to %s", serverdir
);
480 /* make sure we are at the right place */
481 if (stat( ".", &st
) == -1) fatal_perror( "stat %s", serverdir
);
482 if (st
.st_uid
!= getuid()) fatal_error( "'%s' is not owned by you\n", serverdir
);
483 if (st
.st_mode
& 077) fatal_error( "'%s' must not be accessible by other users\n", serverdir
);
485 for (retry
= 0; retry
< 3; retry
++)
487 /* if not the first try, wait a bit to leave the server time to exit */
488 if (retry
) usleep( 100000 * retry
* retry
);
490 /* check for an existing socket */
491 if (lstat( SOCKETNAME
, &st
) == -1)
493 if (errno
!= ENOENT
) fatal_perror( "lstat %s/%s", serverdir
, SOCKETNAME
);
494 start_server( oldcwd
);
495 if (lstat( SOCKETNAME
, &st
) == -1) fatal_perror( "lstat %s/%s", serverdir
, SOCKETNAME
);
498 /* make sure the socket is sane (ISFIFO needed for Solaris) */
499 if (!S_ISSOCK(st
.st_mode
) && !S_ISFIFO(st
.st_mode
))
500 fatal_error( "'%s/%s' is not a socket\n", serverdir
, SOCKETNAME
);
501 if (st
.st_uid
!= getuid())
502 fatal_error( "'%s/%s' is not owned by you\n", serverdir
, SOCKETNAME
);
504 /* try to connect to it */
505 addr
.sun_family
= AF_UNIX
;
506 strcpy( addr
.sun_path
, SOCKETNAME
);
507 slen
= sizeof(addr
) - sizeof(addr
.sun_path
) + strlen(addr
.sun_path
) + 1;
508 #ifdef HAVE_SOCKADDR_SUN_LEN
511 if ((s
= socket( AF_UNIX
, SOCK_STREAM
, 0 )) == -1) fatal_perror( "socket" );
512 if (connect( s
, (struct sockaddr
*)&addr
, slen
) != -1)
514 fcntl( s
, F_SETFD
, 1 ); /* set close on exec flag */
519 fatal_error( "file '%s/%s' exists,\n"
520 " but I cannot connect to it; maybe the wineserver has crashed?\n"
521 " If this is the case, you should remove this socket file and try again.\n",
522 serverdir
, SOCKETNAME
);
526 /***********************************************************************
529 * Start the server and create the initial socket pair.
531 void CLIENT_InitServer(void)
535 char *oldcwd
, *serverdir
;
536 const char *configdir
;
537 handle_t dummy_handle
;
539 /* retrieve the current directory */
540 for (size
= 512; ; size
*= 2)
542 if (!(oldcwd
= malloc( size
))) break;
543 if (getcwd( oldcwd
, size
)) break;
545 if (errno
== ERANGE
) continue;
550 /* if argv[0] is a relative path, make it absolute */
552 if (oldcwd
&& argv0
[0] != '/' && strchr( argv0
, '/' ))
554 char *new_argv0
= malloc( strlen(oldcwd
) + strlen(argv0
) + 2 );
557 strcpy( new_argv0
, oldcwd
);
558 strcat( new_argv0
, "/" );
559 strcat( new_argv0
, argv0
);
560 full_argv0
= new_argv0
;
564 /* get the server directory name */
565 if (gethostname( hostname
, sizeof(hostname
) ) == -1) fatal_perror( "gethostname" );
566 configdir
= get_config_dir();
567 serverdir
= malloc( strlen(configdir
) + strlen(SERVERDIR
) + strlen(hostname
) + 1 );
568 if (!serverdir
) fatal_error( "out of memory\n" );
569 strcpy( serverdir
, configdir
);
570 strcat( serverdir
, SERVERDIR
);
571 strcat( serverdir
, hostname
);
573 /* connect to the server */
574 fd_socket
= server_connect( oldcwd
, serverdir
);
576 /* switch back to the starting directory */
583 /* setup the signal mask */
584 sigemptyset( &block_set
);
585 sigaddset( &block_set
, SIGALRM
);
586 sigaddset( &block_set
, SIGIO
);
587 sigaddset( &block_set
, SIGINT
);
588 sigaddset( &block_set
, SIGHUP
);
590 /* receive the first thread request fd on the main socket */
591 NtCurrentTeb()->request_fd
= receive_fd( &dummy_handle
);
597 /***********************************************************************
600 inline static void set_request_buffer(void)
604 unsigned int offset
, size
;
606 /* create a temporary file */
609 if (!(name
= tmpnam(NULL
))) server_protocol_perror( "tmpnam" );
610 fd
= open( name
, O_CREAT
| O_EXCL
| O_RDWR
, 0600 );
611 } while ((fd
== -1) && (errno
== EEXIST
));
613 if (fd
== -1) server_protocol_perror( "create" );
616 wine_server_send_fd( fd
);
618 SERVER_START_REQ( set_thread_buffer
)
622 offset
= req
->offset
;
626 if (ret
) server_protocol_error( "set_thread_buffer failed with status %x\n", ret
);
628 if ((NtCurrentTeb()->buffer
= mmap( 0, size
, PROT_READ
| PROT_WRITE
,
629 MAP_SHARED
, fd
, offset
)) == (void*)-1)
630 server_protocol_perror( "mmap" );
633 NtCurrentTeb()->buffer_pos
= 0;
634 NtCurrentTeb()->buffer_size
= size
;
638 /***********************************************************************
641 * Send an init thread request. Return 0 if OK.
643 void CLIENT_InitThread(void)
645 TEB
*teb
= NtCurrentTeb();
649 /* ignore SIGPIPE so that we get a EPIPE error instead */
650 signal( SIGPIPE
, SIG_IGN
);
652 /* create the server->client communication pipes */
653 if (pipe( reply_pipe
) == -1) server_protocol_perror( "pipe" );
654 if (pipe( teb
->wait_fd
) == -1) server_protocol_perror( "pipe" );
655 wine_server_send_fd( reply_pipe
[1] );
656 wine_server_send_fd( teb
->wait_fd
[1] );
657 teb
->reply_fd
= reply_pipe
[0];
659 /* set close on exec flag */
660 fcntl( teb
->reply_fd
, F_SETFD
, 1 );
661 fcntl( teb
->wait_fd
[0], F_SETFD
, 1 );
662 fcntl( teb
->wait_fd
[1], F_SETFD
, 1 );
664 SERVER_START_REQ( init_thread
)
666 req
->unix_pid
= getpid();
668 req
->entry
= teb
->entry_point
;
669 req
->reply_fd
= reply_pipe
[1];
670 req
->wait_fd
= teb
->wait_fd
[1];
674 version
= req
->version
;
675 if (req
->boot
) boot_thread_id
= teb
->tid
;
676 else if (boot_thread_id
== teb
->tid
) boot_thread_id
= 0;
677 close( reply_pipe
[1] );
681 if (ret
) server_protocol_error( "init_thread failed with status %x\n", ret
);
682 if (version
!= SERVER_PROTOCOL_VERSION
)
683 server_protocol_error( "version mismatch %d/%d.\n"
684 "Your %s binary was not upgraded correctly,\n"
685 "or you have an older one somewhere in your PATH.\n"
686 "Or maybe the wrong wineserver is still running?\n",
687 version
, SERVER_PROTOCOL_VERSION
,
688 (version
> SERVER_PROTOCOL_VERSION
) ? "wine" : "wineserver" );
689 set_request_buffer();
693 /***********************************************************************
696 * Signal that we have finished booting, and set debug level.
698 void CLIENT_BootDone( int debug_level
)
700 SERVER_START_REQ( boot_done
)
702 req
->debug_level
= debug_level
;
709 /***********************************************************************
710 * CLIENT_IsBootThread
712 * Return TRUE if current thread is the boot thread.
714 int CLIENT_IsBootThread(void)
716 return (GetCurrentThreadId() == (DWORD
)boot_thread_id
);