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
35 /* Some versions of glibc don't define this */
40 #define SERVERDIR "/wineserver-" /* server socket directory (hostname appended) */
41 #define SOCKETNAME "socket" /* name of the socket file */
43 /* data structure used to pass an fd with sendmsg/recvmsg */
46 int len; /* sizeof structure */
47 int level; /* SOL_SOCKET */
48 int type; /* SCM_RIGHTS */
49 int fd; /* fd to pass */
52 static void *boot_thread_id;
55 /* die on a fatal error; use only during initialization */
56 static void fatal_error( const char *err, ... )
60 va_start( args, err );
61 fprintf( stderr, "wine: " );
62 vfprintf( stderr, err, args );
67 /* die on a fatal error; use only during initialization */
68 static void fatal_perror( const char *err, ... )
72 va_start( args, err );
73 fprintf( stderr, "wine: " );
74 vfprintf( stderr, err, args );
80 /***********************************************************************
83 * Die on protocol errors or socket close
85 static void CLIENT_Die(void)
87 close( NtCurrentTeb()->socket );
91 /***********************************************************************
92 * server_protocol_error
94 void server_protocol_error( const char *err, ... )
98 va_start( args, err );
99 fprintf( stderr, "Client protocol error:%p: ", NtCurrentTeb()->tid );
100 vfprintf( stderr, err, args );
106 /***********************************************************************
109 static void server_perror( const char *err )
111 fprintf( stderr, "Client protocol error:%p: ", NtCurrentTeb()->tid );
117 /***********************************************************************
120 * Send a request to the server.
122 static void send_request( enum request req )
125 if ((ret = write( NtCurrentTeb()->socket, &req, sizeof(req) )) == sizeof(req))
129 if (errno == EPIPE) CLIENT_Die();
130 server_perror( "sendmsg" );
132 server_protocol_error( "partial msg sent %d/%d\n", ret, sizeof(req) );
135 /***********************************************************************
138 * Send a request to the server, passing a file descriptor.
140 static void send_request_fd( enum request req, int fd )
143 #ifndef HAVE_MSGHDR_ACCRIGHTS
146 struct msghdr msghdr;
149 vec.iov_base = (void *)&req;
150 vec.iov_len = sizeof(req);
152 msghdr.msg_name = NULL;
153 msghdr.msg_namelen = 0;
154 msghdr.msg_iov = &vec;
155 msghdr.msg_iovlen = 1;
157 #ifdef HAVE_MSGHDR_ACCRIGHTS
158 msghdr.msg_accrights = (void *)&fd;
159 msghdr.msg_accrightslen = sizeof(fd);
160 #else /* HAVE_MSGHDR_ACCRIGHTS */
161 cmsg.len = sizeof(cmsg);
162 cmsg.level = SOL_SOCKET;
163 cmsg.type = SCM_RIGHTS;
165 msghdr.msg_control = &cmsg;
166 msghdr.msg_controllen = sizeof(cmsg);
167 msghdr.msg_flags = 0;
168 #endif /* HAVE_MSGHDR_ACCRIGHTS */
170 if ((ret = sendmsg( NtCurrentTeb()->socket, &msghdr, 0 )) == sizeof(req)) return;
173 if (errno == EPIPE) CLIENT_Die();
174 server_perror( "sendmsg" );
176 server_protocol_error( "partial msg sent %d/%d\n", ret, sizeof(req) );
179 /***********************************************************************
182 * Wait for a reply from the server.
184 static unsigned int wait_reply(void)
191 if ((ret = read( NtCurrentTeb()->socket, &res, sizeof(res) )) == sizeof(res))
195 if (errno == EINTR) continue;
196 if (errno == EPIPE) CLIENT_Die();
197 server_perror("read");
199 if (!ret) CLIENT_Die(); /* the server closed the connection; time to die... */
200 server_protocol_error( "partial msg received %d/%d\n", ret, sizeof(res) );
205 /***********************************************************************
208 * Wait for a reply from the server, when a file descriptor is passed.
210 static unsigned int wait_reply_fd( int *fd )
216 #ifdef HAVE_MSGHDR_ACCRIGHTS
217 struct msghdr msghdr;
220 msghdr.msg_accrights = (void *)fd;
221 msghdr.msg_accrightslen = sizeof(*fd);
222 #else /* HAVE_MSGHDR_ACCRIGHTS */
223 struct msghdr msghdr;
226 cmsg.len = sizeof(cmsg);
227 cmsg.level = SOL_SOCKET;
228 cmsg.type = SCM_RIGHTS;
230 msghdr.msg_control = &cmsg;
231 msghdr.msg_controllen = sizeof(cmsg);
232 msghdr.msg_flags = 0;
233 #endif /* HAVE_MSGHDR_ACCRIGHTS */
235 msghdr.msg_name = NULL;
236 msghdr.msg_namelen = 0;
237 msghdr.msg_iov = &vec;
238 msghdr.msg_iovlen = 1;
239 vec.iov_base = (void *)&res;
240 vec.iov_len = sizeof(res);
244 if ((ret = recvmsg( NtCurrentTeb()->socket, &msghdr, 0 )) == sizeof(res))
246 #ifndef HAVE_MSGHDR_ACCRIGHTS
253 if (errno == EINTR) continue;
254 if (errno == EPIPE) CLIENT_Die();
255 server_perror("recvmsg");
257 if (!ret) CLIENT_Die(); /* the server closed the connection; time to die... */
258 server_protocol_error( "partial seq received %d/%d\n", ret, sizeof(res) );
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 * Start a new wine server.
301 static void start_server( const char *oldcwd )
303 static int started; /* we only try once */
307 if (pid == -1) fatal_perror( "fork" );
311 /* first try the installation dir */
312 execl( BINDIR "/wineserver", "wineserver", NULL );
313 if (oldcwd) chdir( oldcwd );
314 /* now try the dir we were launched from */
315 path = xmalloc( strlen(argv0) + 20 );
316 if ((p = strrchr( strcpy( path, argv0 ), '/' )))
318 strcpy( p, "/wineserver" );
319 execl( path, "wineserver", NULL );
320 strcpy( p, "/server/wineserver" );
321 execl( path, "wineserver", NULL );
323 /* now try the path */
324 execlp( "wineserver", "wineserver", NULL );
325 /* and finally the current dir */
326 execl( "./server/wineserver", "wineserver", NULL );
327 fatal_error( "could not exec wineserver\n" );
333 /***********************************************************************
336 * Attempt to connect to an existing server socket.
337 * We need to be in the server directory already.
339 static int server_connect( const char *oldcwd, const char *serverdir )
341 struct sockaddr_un addr;
345 if (chdir( serverdir ) == -1)
347 if (errno != ENOENT) fatal_perror( "chdir to %s", serverdir );
348 start_server( NULL );
351 if (stat( ".", &st ) == -1) fatal_perror( "stat %s", serverdir );
352 if (st.st_uid != getuid()) fatal_error( "'%s' is not owned by you\n", serverdir );
353 if (st.st_mode & 077) fatal_error( "'%s' must not be accessible by other users\n", serverdir );
355 if (lstat( SOCKETNAME, &st ) == -1)
357 if (errno != ENOENT) fatal_perror( "lstat %s/%s", serverdir, SOCKETNAME );
358 start_server( oldcwd );
361 if (!S_ISSOCK(st.st_mode))
362 fatal_error( "'%s/%s' is not a socket\n", serverdir, SOCKETNAME );
363 if (st.st_uid != getuid())
364 fatal_error( "'%s/%s' is not owned by you\n", serverdir, SOCKETNAME );
366 if ((s = socket( AF_UNIX, SOCK_STREAM, 0 )) == -1) fatal_perror( "socket" );
367 addr.sun_family = AF_UNIX;
368 strcpy( addr.sun_path, SOCKETNAME );
369 slen = sizeof(addr) - sizeof(addr.sun_path) + strlen(addr.sun_path) + 1;
370 #ifdef HAVE_SOCKADDR_SUN_LEN
373 if (connect( s, (struct sockaddr *)&addr, slen ) == -1)
378 fcntl( s, F_SETFD, 1 ); /* set close on exec flag */
383 /***********************************************************************
386 * Start the server and create the initial socket pair.
388 int CLIENT_InitServer(void)
393 char *oldcwd, *serverdir;
394 const char *configdir;
396 /* first check if we inherited the socket fd */
397 if ((env_fd = getenv( "__WINE_FD" )) && isdigit(env_fd[0]))
400 if (fcntl( fd, F_GETFL, 0 ) != -1) return fd;
403 /* retrieve the current directory */
404 for (size = 512; ; size *= 2)
406 oldcwd = xmalloc( size );
407 if (getcwd( oldcwd, size )) break;
409 if (errno == ERANGE) continue;
414 /* get the server directory name */
415 if (gethostname( hostname, sizeof(hostname) ) == -1) fatal_perror( "gethostname" );
416 configdir = PROFILE_GetConfigDir();
417 serverdir = xmalloc( strlen(configdir) + strlen(SERVERDIR) + strlen(hostname) + 1 );
418 strcpy( serverdir, configdir );
419 strcat( serverdir, SERVERDIR );
420 strcat( serverdir, hostname );
422 /* try to connect, leaving some time for the server to start up */
423 for (delay = 10000; delay < 500000; delay += delay / 2)
425 if ((fd = server_connect( oldcwd, serverdir )) >= 0) goto done;
431 fatal_error( "'%s/%s' exists,\n"
432 " but I cannot connect to it; maybe the server has crashed?\n"
433 " If this is the case, you should remove the socket file and try again.\n",
434 serverdir, SOCKETNAME );
436 fatal_error( "could not start wineserver, giving up\n" );
439 /* switch back to the starting directory */
449 /***********************************************************************
452 * Send an init thread request. Return 0 if OK.
454 int CLIENT_InitThread(void)
456 struct get_thread_buffer_request *first_req;
457 struct init_thread_request *req;
458 TEB *teb = NtCurrentTeb();
461 if (wait_reply_fd( &fd ) || (fd == -1))
462 server_protocol_error( "no fd passed on first request\n" );
463 if ((teb->buffer_size = lseek( fd, 0, SEEK_END )) == -1) server_perror( "lseek" );
464 teb->buffer = mmap( 0, teb->buffer_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 );
466 if (teb->buffer == (void*)-1) server_perror( "mmap" );
467 first_req = teb->buffer;
468 teb->process->server_pid = first_req->pid;
469 teb->tid = first_req->tid;
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);
503 /***********************************************************************
504 * CLIENT_DebuggerRequest
506 * Send a debugger support request. Return 0 if OK.
508 int CLIENT_DebuggerRequest( int op )
510 struct debugger_request *req = get_req_buffer();
512 return server_call( REQ_DEBUGGER );