2 * Client part of the client/server communication
4 * Copyright (C) 1998 Alexandre Julliard
15 #include <sys/types.h>
16 #ifdef HAVE_SYS_SOCKET_H
17 # include <sys/socket.h>
20 #ifdef HAVE_SYS_MMAN_H
34 /* Some versions of glibc don't define this */
39 #define SERVERDIR "/wineserver-" /* server socket directory (hostname appended) */
40 #define SOCKETNAME "socket" /* name of the socket file */
42 /* data structure used to pass an fd with sendmsg/recvmsg */
45 int len
; /* sizeof structure */
46 int level
; /* SOL_SOCKET */
47 int type
; /* SCM_RIGHTS */
48 int fd
; /* fd to pass */
51 static void *boot_thread_id
;
54 /* die on a fatal error; use only during initialization */
55 static void fatal_error( const char *err
, ... )
59 va_start( args
, err
);
60 fprintf( stderr
, "wine: " );
61 vfprintf( stderr
, err
, args
);
66 /* die on a fatal error; use only during initialization */
67 static void fatal_perror( const char *err
, ... )
71 va_start( args
, err
);
72 fprintf( stderr
, "wine: " );
73 vfprintf( stderr
, err
, args
);
79 /***********************************************************************
80 * server_protocol_error
82 void server_protocol_error( const char *err
, ... )
86 va_start( args
, err
);
87 fprintf( stderr
, "Client protocol error:%p: ", NtCurrentTeb()->tid
);
88 vfprintf( stderr
, err
, args
);
90 SYSDEPS_ExitThread(1);
94 /***********************************************************************
97 static void server_perror( const char *err
)
99 fprintf( stderr
, "Client protocol error:%p: ", NtCurrentTeb()->tid
);
101 SYSDEPS_ExitThread(1);
105 /***********************************************************************
108 * Send a request to the server.
110 static void send_request( enum request req
)
113 if ((ret
= write( NtCurrentTeb()->socket
, &req
, sizeof(req
) )) == sizeof(req
))
117 if (errno
== EPIPE
) SYSDEPS_ExitThread(0);
118 server_perror( "sendmsg" );
120 server_protocol_error( "partial msg sent %d/%d\n", ret
, sizeof(req
) );
123 /***********************************************************************
126 * Send a request to the server, passing a file descriptor.
128 static void send_request_fd( enum request req
, int fd
)
131 #ifndef HAVE_MSGHDR_ACCRIGHTS
134 struct msghdr msghdr
;
137 vec
.iov_base
= (void *)&req
;
138 vec
.iov_len
= sizeof(req
);
140 msghdr
.msg_name
= NULL
;
141 msghdr
.msg_namelen
= 0;
142 msghdr
.msg_iov
= &vec
;
143 msghdr
.msg_iovlen
= 1;
145 #ifdef HAVE_MSGHDR_ACCRIGHTS
146 msghdr
.msg_accrights
= (void *)&fd
;
147 msghdr
.msg_accrightslen
= sizeof(fd
);
148 #else /* HAVE_MSGHDR_ACCRIGHTS */
149 cmsg
.len
= sizeof(cmsg
);
150 cmsg
.level
= SOL_SOCKET
;
151 cmsg
.type
= SCM_RIGHTS
;
153 msghdr
.msg_control
= &cmsg
;
154 msghdr
.msg_controllen
= sizeof(cmsg
);
155 msghdr
.msg_flags
= 0;
156 #endif /* HAVE_MSGHDR_ACCRIGHTS */
158 if ((ret
= sendmsg( NtCurrentTeb()->socket
, &msghdr
, 0 )) == sizeof(req
)) return;
161 if (errno
== EPIPE
) SYSDEPS_ExitThread(0);
162 server_perror( "sendmsg" );
164 server_protocol_error( "partial msg sent %d/%d\n", ret
, sizeof(req
) );
167 /***********************************************************************
170 * Wait for a reply from the server.
172 static unsigned int wait_reply(void)
179 if ((ret
= read( NtCurrentTeb()->socket
, &res
, sizeof(res
) )) == sizeof(res
))
184 if (errno
== EINTR
) continue;
185 if (errno
== EPIPE
) break;
186 server_perror("read");
188 server_protocol_error( "partial msg received %d/%d\n", ret
, sizeof(res
) );
190 /* the server closed the connection; time to die... */
191 SYSDEPS_ExitThread(0);
195 /***********************************************************************
198 * Wait for a reply from the server, when a file descriptor is passed.
200 static unsigned int wait_reply_fd( int *fd
)
206 #ifdef HAVE_MSGHDR_ACCRIGHTS
207 struct msghdr msghdr
;
210 msghdr
.msg_accrights
= (void *)fd
;
211 msghdr
.msg_accrightslen
= sizeof(*fd
);
212 #else /* HAVE_MSGHDR_ACCRIGHTS */
213 struct msghdr msghdr
;
216 cmsg
.len
= sizeof(cmsg
);
217 cmsg
.level
= SOL_SOCKET
;
218 cmsg
.type
= SCM_RIGHTS
;
220 msghdr
.msg_control
= &cmsg
;
221 msghdr
.msg_controllen
= sizeof(cmsg
);
222 msghdr
.msg_flags
= 0;
223 #endif /* HAVE_MSGHDR_ACCRIGHTS */
225 msghdr
.msg_name
= NULL
;
226 msghdr
.msg_namelen
= 0;
227 msghdr
.msg_iov
= &vec
;
228 msghdr
.msg_iovlen
= 1;
229 vec
.iov_base
= (void *)&res
;
230 vec
.iov_len
= sizeof(res
);
234 if ((ret
= recvmsg( NtCurrentTeb()->socket
, &msghdr
, 0 )) == sizeof(res
))
236 #ifndef HAVE_MSGHDR_ACCRIGHTS
244 if (errno
== EINTR
) continue;
245 if (errno
== EPIPE
) break;
246 server_perror("recvmsg");
248 server_protocol_error( "partial seq received %d/%d\n", ret
, sizeof(res
) );
250 /* the server closed the connection; time to die... */
251 SYSDEPS_ExitThread(0);
255 /***********************************************************************
258 * Perform a server call.
260 unsigned int server_call_noerr( enum request req
)
267 /***********************************************************************
270 * Perform a server call, passing a file descriptor.
271 * If *fd is != -1, it will be passed to the server.
272 * If the server passes an fd, it will be stored into *fd.
274 unsigned int server_call_fd( enum request req
, int fd_out
, int *fd_in
)
278 if (fd_out
== -1) send_request( req
);
279 else send_request_fd( req
, fd_out
);
281 if (fd_in
) res
= wait_reply_fd( fd_in
);
282 else res
= wait_reply();
283 if (res
) SetLastError( RtlNtStatusToDosError(res
) );
284 return res
; /* error code */
288 /***********************************************************************
291 * Start a new wine server.
293 static void start_server( const char *oldcwd
)
295 static int started
; /* we only try once */
299 if (pid
== -1) fatal_perror( "fork" );
303 /* first try the installation dir */
304 execl( BINDIR
"/wineserver", "wineserver", NULL
);
305 if (oldcwd
) chdir( oldcwd
);
306 /* now try the dir we were launched from */
307 if (!(path
= malloc( strlen(argv0
) + 20 )))
308 fatal_error( "out of memory\n" );
309 if ((p
= strrchr( strcpy( path
, argv0
), '/' )))
311 strcpy( p
, "/wineserver" );
312 execl( path
, "wineserver", NULL
);
313 strcpy( p
, "/server/wineserver" );
314 execl( path
, "wineserver", NULL
);
316 /* now try the path */
317 execlp( "wineserver", "wineserver", NULL
);
318 /* and finally the current dir */
319 execl( "./server/wineserver", "wineserver", NULL
);
320 fatal_error( "could not exec wineserver\n" );
326 /***********************************************************************
329 * Attempt to connect to an existing server socket.
330 * We need to be in the server directory already.
332 static int server_connect( const char *oldcwd
, const char *serverdir
)
334 struct sockaddr_un addr
;
338 if (chdir( serverdir
) == -1)
340 if (errno
!= ENOENT
) fatal_perror( "chdir to %s", serverdir
);
341 start_server( NULL
);
344 if (stat( ".", &st
) == -1) fatal_perror( "stat %s", serverdir
);
345 if (st
.st_uid
!= getuid()) fatal_error( "'%s' is not owned by you\n", serverdir
);
346 if (st
.st_mode
& 077) fatal_error( "'%s' must not be accessible by other users\n", serverdir
);
348 if (lstat( SOCKETNAME
, &st
) == -1)
350 if (errno
!= ENOENT
) fatal_perror( "lstat %s/%s", serverdir
, SOCKETNAME
);
351 start_server( oldcwd
);
354 if (!S_ISSOCK(st
.st_mode
))
355 fatal_error( "'%s/%s' is not a socket\n", serverdir
, SOCKETNAME
);
356 if (st
.st_uid
!= getuid())
357 fatal_error( "'%s/%s' is not owned by you\n", serverdir
, SOCKETNAME
);
359 if ((s
= socket( AF_UNIX
, SOCK_STREAM
, 0 )) == -1) fatal_perror( "socket" );
360 addr
.sun_family
= AF_UNIX
;
361 strcpy( addr
.sun_path
, SOCKETNAME
);
362 slen
= sizeof(addr
) - sizeof(addr
.sun_path
) + strlen(addr
.sun_path
) + 1;
363 #ifdef HAVE_SOCKADDR_SUN_LEN
366 if (connect( s
, (struct sockaddr
*)&addr
, slen
) == -1)
371 fcntl( s
, F_SETFD
, 1 ); /* set close on exec flag */
376 /***********************************************************************
379 * Start the server and create the initial socket pair.
381 int CLIENT_InitServer(void)
386 char *oldcwd
, *serverdir
;
387 const char *configdir
;
389 /* first check if we inherited the socket fd */
390 if ((env_fd
= getenv( "__WINE_FD" )) && isdigit(env_fd
[0]))
393 if (fcntl( fd
, F_GETFL
, 0 ) != -1) return fd
;
396 /* retrieve the current directory */
397 for (size
= 512; ; size
*= 2)
399 if (!(oldcwd
= malloc( size
))) break;
400 if (getcwd( oldcwd
, size
)) break;
402 if (errno
== ERANGE
) continue;
407 /* get the server directory name */
408 if (gethostname( hostname
, sizeof(hostname
) ) == -1) fatal_perror( "gethostname" );
409 configdir
= PROFILE_GetConfigDir();
410 serverdir
= malloc( strlen(configdir
) + strlen(SERVERDIR
) + strlen(hostname
) + 1 );
411 if (!serverdir
) fatal_error( "out of memory\n" );
412 strcpy( serverdir
, configdir
);
413 strcat( serverdir
, SERVERDIR
);
414 strcat( serverdir
, hostname
);
416 /* try to connect, leaving some time for the server to start up */
417 for (delay
= 10000; delay
< 500000; delay
+= delay
/ 2)
419 if ((fd
= server_connect( oldcwd
, serverdir
)) >= 0) goto done
;
425 fatal_error( "'%s/%s' exists,\n"
426 " but I cannot connect to it; maybe the server has crashed?\n"
427 " If this is the case, you should remove the socket file and try again.\n",
428 serverdir
, SOCKETNAME
);
430 fatal_error( "could not start wineserver, giving up\n" );
433 /* switch back to the starting directory */
443 /***********************************************************************
446 * Send an init thread request. Return 0 if OK.
448 int CLIENT_InitThread(void)
450 struct get_thread_buffer_request
*first_req
;
451 struct init_thread_request
*req
;
452 TEB
*teb
= NtCurrentTeb();
455 if (wait_reply_fd( &fd
) || (fd
== -1))
456 server_protocol_error( "no fd passed on first request\n" );
457 if ((teb
->buffer_size
= lseek( fd
, 0, SEEK_END
)) == -1) server_perror( "lseek" );
458 teb
->buffer
= mmap( 0, teb
->buffer_size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0 );
460 if (teb
->buffer
== (void*)-1) server_perror( "mmap" );
461 first_req
= teb
->buffer
;
462 teb
->process
->server_pid
= first_req
->pid
;
463 teb
->tid
= first_req
->tid
;
464 if (first_req
->version
!= SERVER_PROTOCOL_VERSION
)
465 server_protocol_error( "version mismatch %d/%d.\n"
466 "Your %s binary was not upgraded correctly,\n"
467 "or you have an older one somewhere in your PATH.\n",
468 first_req
->version
, SERVER_PROTOCOL_VERSION
,
469 (first_req
->version
> SERVER_PROTOCOL_VERSION
) ? "wine" : "wineserver" );
470 if (first_req
->boot
) boot_thread_id
= teb
->tid
;
471 else if (boot_thread_id
== teb
->tid
) boot_thread_id
= 0;
474 req
->unix_pid
= getpid();
476 req
->entry
= teb
->entry_point
;
477 return server_call_noerr( REQ_INIT_THREAD
);
480 /***********************************************************************
483 * Signal that we have finished booting, and set debug level.
485 int CLIENT_BootDone( int debug_level
)
487 struct boot_done_request
*req
= get_req_buffer();
488 req
->debug_level
= debug_level
;
489 return server_call( REQ_BOOT_DONE
);
493 /***********************************************************************
494 * CLIENT_IsBootThread
496 * Return TRUE if current thread is the boot thread.
498 int CLIENT_IsBootThread(void)
500 return (GetCurrentThreadId() == (DWORD
)boot_thread_id
);