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
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
;
60 /* die on a fatal error; use only during initialization */
61 static void fatal_error( const char *err
, ... ) WINE_NORETURN
;
62 static void fatal_error( const char *err
, ... )
66 va_start( args
, err
);
67 fprintf( stderr
, "wine: " );
68 vfprintf( stderr
, err
, args
);
73 /* die on a fatal error; use only during initialization */
74 static void fatal_perror( const char *err
, ... ) WINE_NORETURN
;
75 static void fatal_perror( const char *err
, ... )
79 va_start( args
, err
);
80 fprintf( stderr
, "wine: " );
81 vfprintf( stderr
, err
, args
);
87 /***********************************************************************
88 * server_protocol_error
90 void server_protocol_error( const char *err
, ... )
94 va_start( args
, err
);
95 fprintf( stderr
, "Client protocol error:%p: ", NtCurrentTeb()->tid
);
96 vfprintf( stderr
, err
, args
);
98 SYSDEPS_ExitThread(1);
102 /***********************************************************************
105 static void server_perror( const char *err
)
107 fprintf( stderr
, "Client protocol error:%p: ", NtCurrentTeb()->tid
);
109 SYSDEPS_ExitThread(1);
113 /***********************************************************************
116 * Send a request to the server.
118 static void send_request( enum request req
)
121 if ((ret
= write( NtCurrentTeb()->socket
, &req
, sizeof(req
) )) == sizeof(req
))
125 if (errno
== EPIPE
) SYSDEPS_ExitThread(0);
126 server_perror( "sendmsg" );
128 server_protocol_error( "partial msg sent %d/%d\n", ret
, sizeof(req
) );
131 /***********************************************************************
134 * Send a request to the server, passing a file descriptor.
136 static void send_request_fd( enum request req
, int fd
)
139 #ifndef HAVE_MSGHDR_ACCRIGHTS
142 struct msghdr msghdr
;
145 vec
.iov_base
= (void *)&req
;
146 vec
.iov_len
= sizeof(req
);
148 msghdr
.msg_name
= NULL
;
149 msghdr
.msg_namelen
= 0;
150 msghdr
.msg_iov
= &vec
;
151 msghdr
.msg_iovlen
= 1;
153 #ifdef HAVE_MSGHDR_ACCRIGHTS
154 msghdr
.msg_accrights
= (void *)&fd
;
155 msghdr
.msg_accrightslen
= sizeof(fd
);
156 #else /* HAVE_MSGHDR_ACCRIGHTS */
157 cmsg
.len
= sizeof(cmsg
);
158 cmsg
.level
= SOL_SOCKET
;
159 cmsg
.type
= SCM_RIGHTS
;
161 msghdr
.msg_control
= &cmsg
;
162 msghdr
.msg_controllen
= sizeof(cmsg
);
163 msghdr
.msg_flags
= 0;
164 #endif /* HAVE_MSGHDR_ACCRIGHTS */
166 if ((ret
= sendmsg( NtCurrentTeb()->socket
, &msghdr
, 0 )) == sizeof(req
)) return;
169 if (errno
== EPIPE
) SYSDEPS_ExitThread(0);
170 server_perror( "sendmsg" );
172 server_protocol_error( "partial msg sent %d/%d\n", ret
, sizeof(req
) );
175 /***********************************************************************
178 * Wait for a reply from the server.
180 static unsigned int wait_reply(void)
187 if ((ret
= read( NtCurrentTeb()->socket
, &res
, sizeof(res
) )) == sizeof(res
))
192 if (errno
== EINTR
) continue;
193 if (errno
== EPIPE
) break;
194 server_perror("read");
196 server_protocol_error( "partial msg received %d/%d\n", ret
, sizeof(res
) );
198 /* the server closed the connection; time to die... */
199 SYSDEPS_ExitThread(0);
203 /***********************************************************************
206 * Wait for a reply from the server, when a file descriptor is passed.
208 static unsigned int wait_reply_fd( int *fd
)
214 #ifdef HAVE_MSGHDR_ACCRIGHTS
215 struct msghdr msghdr
;
218 msghdr
.msg_accrights
= (void *)fd
;
219 msghdr
.msg_accrightslen
= sizeof(*fd
);
220 #else /* HAVE_MSGHDR_ACCRIGHTS */
221 struct msghdr msghdr
;
224 cmsg
.len
= sizeof(cmsg
);
225 cmsg
.level
= SOL_SOCKET
;
226 cmsg
.type
= SCM_RIGHTS
;
228 msghdr
.msg_control
= &cmsg
;
229 msghdr
.msg_controllen
= sizeof(cmsg
);
230 msghdr
.msg_flags
= 0;
231 #endif /* HAVE_MSGHDR_ACCRIGHTS */
233 msghdr
.msg_name
= NULL
;
234 msghdr
.msg_namelen
= 0;
235 msghdr
.msg_iov
= &vec
;
236 msghdr
.msg_iovlen
= 1;
237 vec
.iov_base
= (void *)&res
;
238 vec
.iov_len
= sizeof(res
);
242 if ((ret
= recvmsg( NtCurrentTeb()->socket
, &msghdr
, 0 )) == sizeof(res
))
244 #ifndef HAVE_MSGHDR_ACCRIGHTS
252 if (errno
== EINTR
) continue;
253 if (errno
== EPIPE
) break;
254 server_perror("recvmsg");
256 server_protocol_error( "partial seq received %d/%d\n", ret
, sizeof(res
) );
258 /* the server closed the connection; time to die... */
259 SYSDEPS_ExitThread(0);
263 /***********************************************************************
266 * Perform a server call.
268 unsigned int server_call_noerr( enum request req
)
275 /***********************************************************************
278 * Perform a server call, passing a file descriptor.
279 * If *fd is != -1, it will be passed to the server.
280 * If the server passes an fd, it will be stored into *fd.
282 unsigned int server_call_fd( enum request req
, int fd_out
, int *fd_in
)
286 if (fd_out
== -1) send_request( req
);
287 else send_request_fd( req
, fd_out
);
289 if (fd_in
) res
= wait_reply_fd( fd_in
);
290 else res
= wait_reply();
291 if (res
) SetLastError( RtlNtStatusToDosError(res
) );
292 return res
; /* error code */
296 /***********************************************************************
299 * Return the configuration directory ($WINEPREFIX or $HOME/.wine)
301 const char *get_config_dir(void)
303 static char *confdir
;
306 const char *prefix
= getenv( "WINEPREFIX" );
309 int len
= strlen(prefix
);
310 if (!(confdir
= strdup( prefix
))) fatal_error( "out of memory\n" );
311 if (len
> 1 && confdir
[len
-1] == '/') confdir
[len
-1] = 0;
315 const char *home
= getenv( "HOME" );
318 struct passwd
*pwd
= getpwuid( getuid() );
319 if (!pwd
) fatal_error( "could not find your home directory\n" );
322 if (!(confdir
= malloc( strlen(home
) + strlen(CONFDIR
) + 1 )))
323 fatal_error( "out of memory\n" );
324 strcpy( confdir
, home
);
325 strcat( confdir
, CONFDIR
);
327 mkdir( confdir
, 0755 ); /* just in case */
333 /***********************************************************************
336 * Start a new wine server.
338 static void start_server( const char *oldcwd
)
340 static int started
; /* we only try once */
345 if (pid
== -1) fatal_perror( "fork" );
349 /* first try the installation dir */
350 execl( BINDIR
"/wineserver", "wineserver", NULL
);
351 if (oldcwd
) chdir( oldcwd
);
352 /* now try the dir we were launched from */
353 if (!(path
= malloc( strlen(argv0
) + 20 )))
354 fatal_error( "out of memory\n" );
355 if ((p
= strrchr( strcpy( path
, argv0
), '/' )))
357 strcpy( p
, "/wineserver" );
358 execl( path
, "wineserver", NULL
);
359 strcpy( p
, "/server/wineserver" );
360 execl( path
, "wineserver", NULL
);
362 /* now try the path */
363 execlp( "wineserver", "wineserver", NULL
);
364 /* and finally the current dir */
365 execl( "./server/wineserver", "wineserver", NULL
);
366 fatal_error( "could not exec wineserver\n" );
369 waitpid( pid
, &status
, 0 );
370 status
= WIFEXITED(status
) ? WEXITSTATUS(status
) : 1;
371 if (status
) exit(status
); /* server failed */
375 /***********************************************************************
378 * Attempt to connect to an existing server socket.
379 * We need to be in the server directory already.
381 static int server_connect( const char *oldcwd
, const char *serverdir
)
383 struct sockaddr_un addr
;
387 /* chdir to the server directory */
388 if (chdir( serverdir
) == -1)
390 if (errno
!= ENOENT
) fatal_perror( "chdir to %s", serverdir
);
391 start_server( NULL
);
392 if (chdir( serverdir
) == -1) fatal_perror( "chdir to %s", serverdir
);
395 /* make sure we are at the right place */
396 if (stat( ".", &st
) == -1) fatal_perror( "stat %s", serverdir
);
397 if (st
.st_uid
!= getuid()) fatal_error( "'%s' is not owned by you\n", serverdir
);
398 if (st
.st_mode
& 077) fatal_error( "'%s' must not be accessible by other users\n", serverdir
);
400 /* check for an existing socket */
401 if (lstat( SOCKETNAME
, &st
) == -1)
403 if (errno
!= ENOENT
) fatal_perror( "lstat %s/%s", serverdir
, SOCKETNAME
);
404 start_server( oldcwd
);
405 if (lstat( SOCKETNAME
, &st
) == -1) fatal_perror( "lstat %s/%s", serverdir
, SOCKETNAME
);
408 /* make sure the socket is sane */
409 if (!S_ISSOCK(st
.st_mode
))
410 fatal_error( "'%s/%s' is not a socket\n", serverdir
, SOCKETNAME
);
411 if (st
.st_uid
!= getuid())
412 fatal_error( "'%s/%s' is not owned by you\n", serverdir
, SOCKETNAME
);
414 /* try to connect to it */
415 addr
.sun_family
= AF_UNIX
;
416 strcpy( addr
.sun_path
, SOCKETNAME
);
417 slen
= sizeof(addr
) - sizeof(addr
.sun_path
) + strlen(addr
.sun_path
) + 1;
418 #ifdef HAVE_SOCKADDR_SUN_LEN
421 if ((s
= socket( AF_UNIX
, SOCK_STREAM
, 0 )) == -1) fatal_perror( "socket" );
422 if (connect( s
, (struct sockaddr
*)&addr
, slen
) == -1)
425 /* wait a bit and retry with a new socket */
427 if ((s
= socket( AF_UNIX
, SOCK_STREAM
, 0 )) == -1) fatal_perror( "socket" );
428 if (connect( s
, (struct sockaddr
*)&addr
, slen
) == -1)
429 fatal_error( "'%s/%s' exists,\n"
430 " but I cannot connect to it; maybe the server has crashed?\n"
431 " If this is the case, you should remove the socket file and try again.\n",
432 serverdir
, SOCKETNAME
);
434 fcntl( s
, F_SETFD
, 1 ); /* set close on exec flag */
439 /***********************************************************************
442 * Start the server and create the initial socket pair.
444 int CLIENT_InitServer(void)
448 char *oldcwd
, *serverdir
;
449 const char *configdir
;
451 /* retrieve the current directory */
452 for (size
= 512; ; size
*= 2)
454 if (!(oldcwd
= malloc( size
))) break;
455 if (getcwd( oldcwd
, size
)) break;
457 if (errno
== ERANGE
) continue;
462 /* get the server directory name */
463 if (gethostname( hostname
, sizeof(hostname
) ) == -1) fatal_perror( "gethostname" );
464 configdir
= get_config_dir();
465 serverdir
= malloc( strlen(configdir
) + strlen(SERVERDIR
) + strlen(hostname
) + 1 );
466 if (!serverdir
) fatal_error( "out of memory\n" );
467 strcpy( serverdir
, configdir
);
468 strcat( serverdir
, SERVERDIR
);
469 strcat( serverdir
, hostname
);
471 /* connect to the server */
472 fd
= server_connect( oldcwd
, serverdir
);
474 /* switch back to the starting directory */
484 /***********************************************************************
487 * Send an init thread request. Return 0 if OK.
489 int CLIENT_InitThread(void)
491 struct get_thread_buffer_request
*first_req
;
492 struct init_thread_request
*req
;
493 TEB
*teb
= NtCurrentTeb();
496 /* ignore SIGPIPE so that we get a EPIPE error instead */
497 signal( SIGPIPE
, SIG_IGN
);
499 if (wait_reply_fd( &fd
) || (fd
== -1))
500 server_protocol_error( "no fd passed on first request\n" );
501 if ((teb
->buffer_size
= lseek( fd
, 0, SEEK_END
)) == -1) server_perror( "lseek" );
502 teb
->buffer
= mmap( 0, teb
->buffer_size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0 );
504 if (teb
->buffer
== (void*)-1) server_perror( "mmap" );
505 first_req
= teb
->buffer
;
506 teb
->pid
= first_req
->pid
;
507 teb
->tid
= first_req
->tid
;
508 if (first_req
->version
!= SERVER_PROTOCOL_VERSION
)
509 server_protocol_error( "version mismatch %d/%d.\n"
510 "Your %s binary was not upgraded correctly,\n"
511 "or you have an older one somewhere in your PATH.\n",
512 first_req
->version
, SERVER_PROTOCOL_VERSION
,
513 (first_req
->version
> SERVER_PROTOCOL_VERSION
) ? "wine" : "wineserver" );
514 if (first_req
->boot
) boot_thread_id
= teb
->tid
;
515 else if (boot_thread_id
== teb
->tid
) boot_thread_id
= 0;
518 req
->unix_pid
= getpid();
520 req
->entry
= teb
->entry_point
;
521 return server_call_noerr( REQ_INIT_THREAD
);
524 /***********************************************************************
527 * Signal that we have finished booting, and set debug level.
529 int CLIENT_BootDone( int debug_level
)
531 struct boot_done_request
*req
= get_req_buffer();
532 req
->debug_level
= debug_level
;
533 return server_call( REQ_BOOT_DONE
);
537 /***********************************************************************
538 * CLIENT_IsBootThread
540 * Return TRUE if current thread is the boot thread.
542 int CLIENT_IsBootThread(void)
544 return (GetCurrentThreadId() == (DWORD
)boot_thread_id
);