2 * Client part of the client/server communication
4 * Copyright (C) 1998 Alexandre Julliard
16 #include <sys/types.h>
17 #ifdef HAVE_SYS_SOCKET_H
18 # include <sys/socket.h>
20 #ifdef HAVE_SYS_WAIT_H
24 #ifdef HAVE_SYS_MMAN_H
38 /* Some versions of glibc don't define this */
43 #define CONFDIR "/.wine" /* directory for Wine config relative to $HOME */
44 #define SERVERDIR "/wineserver-" /* server socket directory (hostname appended) */
45 #define SOCKETNAME "socket" /* name of the socket file */
47 /* data structure used to pass an fd with sendmsg/recvmsg */
50 int len; /* sizeof structure */
51 int level; /* SOL_SOCKET */
52 int type; /* SCM_RIGHTS */
53 int fd; /* fd to pass */
56 static void *boot_thread_id;
59 /* die on a fatal error; use only during initialization */
60 static void fatal_error( const char *err, ... ) WINE_NORETURN;
61 static void fatal_error( const char *err, ... )
65 va_start( args, err );
66 fprintf( stderr, "wine: " );
67 vfprintf( stderr, err, args );
72 /* die on a fatal error; use only during initialization */
73 static void fatal_perror( const char *err, ... ) WINE_NORETURN;
74 static void fatal_perror( const char *err, ... )
78 va_start( args, err );
79 fprintf( stderr, "wine: " );
80 vfprintf( stderr, err, args );
86 /***********************************************************************
87 * server_protocol_error
89 void server_protocol_error( const char *err, ... )
93 va_start( args, err );
94 fprintf( stderr, "Client protocol error:%p: ", NtCurrentTeb()->tid );
95 vfprintf( stderr, err, args );
97 SYSDEPS_ExitThread(1);
101 /***********************************************************************
104 static void server_perror( const char *err )
106 fprintf( stderr, "Client protocol error:%p: ", NtCurrentTeb()->tid );
108 SYSDEPS_ExitThread(1);
112 /***********************************************************************
115 * Send a request to the server.
117 static void send_request( enum request req )
120 if ((ret = write( NtCurrentTeb()->socket, &req, sizeof(req) )) == sizeof(req))
124 if (errno == EPIPE) SYSDEPS_ExitThread(0);
125 server_perror( "sendmsg" );
127 server_protocol_error( "partial msg sent %d/%d\n", ret, sizeof(req) );
130 /***********************************************************************
133 * Send a request to the server, passing a file descriptor.
135 static void send_request_fd( enum request req, int fd )
138 #ifndef HAVE_MSGHDR_ACCRIGHTS
141 struct msghdr msghdr;
144 vec.iov_base = (void *)&req;
145 vec.iov_len = sizeof(req);
147 msghdr.msg_name = NULL;
148 msghdr.msg_namelen = 0;
149 msghdr.msg_iov = &vec;
150 msghdr.msg_iovlen = 1;
152 #ifdef HAVE_MSGHDR_ACCRIGHTS
153 msghdr.msg_accrights = (void *)&fd;
154 msghdr.msg_accrightslen = sizeof(fd);
155 #else /* HAVE_MSGHDR_ACCRIGHTS */
156 cmsg.len = sizeof(cmsg);
157 cmsg.level = SOL_SOCKET;
158 cmsg.type = SCM_RIGHTS;
160 msghdr.msg_control = &cmsg;
161 msghdr.msg_controllen = sizeof(cmsg);
162 msghdr.msg_flags = 0;
163 #endif /* HAVE_MSGHDR_ACCRIGHTS */
165 if ((ret = sendmsg( NtCurrentTeb()->socket, &msghdr, 0 )) == sizeof(req)) return;
168 if (errno == EPIPE) SYSDEPS_ExitThread(0);
169 server_perror( "sendmsg" );
171 server_protocol_error( "partial msg sent %d/%d\n", ret, sizeof(req) );
174 /***********************************************************************
177 * Wait for a reply from the server.
179 static unsigned int wait_reply(void)
186 if ((ret = read( NtCurrentTeb()->socket, &res, sizeof(res) )) == sizeof(res))
191 if (errno == EINTR) continue;
192 if (errno == EPIPE) break;
193 server_perror("read");
195 server_protocol_error( "partial msg received %d/%d\n", ret, sizeof(res) );
197 /* the server closed the connection; time to die... */
198 SYSDEPS_ExitThread(0);
202 /***********************************************************************
205 * Wait for a reply from the server, when a file descriptor is passed.
207 static unsigned int wait_reply_fd( int *fd )
213 #ifdef HAVE_MSGHDR_ACCRIGHTS
214 struct msghdr msghdr;
217 msghdr.msg_accrights = (void *)fd;
218 msghdr.msg_accrightslen = sizeof(*fd);
219 #else /* HAVE_MSGHDR_ACCRIGHTS */
220 struct msghdr msghdr;
223 cmsg.len = sizeof(cmsg);
224 cmsg.level = SOL_SOCKET;
225 cmsg.type = SCM_RIGHTS;
227 msghdr.msg_control = &cmsg;
228 msghdr.msg_controllen = sizeof(cmsg);
229 msghdr.msg_flags = 0;
230 #endif /* HAVE_MSGHDR_ACCRIGHTS */
232 msghdr.msg_name = NULL;
233 msghdr.msg_namelen = 0;
234 msghdr.msg_iov = &vec;
235 msghdr.msg_iovlen = 1;
236 vec.iov_base = (void *)&res;
237 vec.iov_len = sizeof(res);
241 if ((ret = recvmsg( NtCurrentTeb()->socket, &msghdr, 0 )) == sizeof(res))
243 #ifndef HAVE_MSGHDR_ACCRIGHTS
251 if (errno == EINTR) continue;
252 if (errno == EPIPE) break;
253 server_perror("recvmsg");
255 server_protocol_error( "partial seq received %d/%d\n", ret, sizeof(res) );
257 /* the server closed the connection; time to die... */
258 SYSDEPS_ExitThread(0);
262 /***********************************************************************
265 * Perform a server call.
267 unsigned int server_call_noerr( enum request req )
274 /***********************************************************************
277 * Perform a server call, passing a file descriptor.
278 * If *fd is != -1, it will be passed to the server.
279 * If the server passes an fd, it will be stored into *fd.
281 unsigned int server_call_fd( enum request req, int fd_out, int *fd_in )
285 if (fd_out == -1) send_request( req );
286 else send_request_fd( req, fd_out );
288 if (fd_in) res = wait_reply_fd( fd_in );
289 else res = wait_reply();
290 if (res) SetLastError( RtlNtStatusToDosError(res) );
291 return res; /* error code */
295 /***********************************************************************
298 * Return the configuration directory ($WINEPREFIX or $HOME/.wine)
300 const char *get_config_dir(void)
302 static char *confdir;
305 const char *prefix = getenv( "WINEPREFIX" );
308 int len = strlen(prefix);
309 if (!(confdir = strdup( prefix ))) fatal_error( "out of memory\n" );
310 if (len > 1 && confdir[len-1] == '/') confdir[len-1] = 0;
314 const char *home = getenv( "HOME" );
317 struct passwd *pwd = getpwuid( getuid() );
318 if (!pwd) fatal_error( "could not find your home directory\n" );
321 if (!(confdir = malloc( strlen(home) + strlen(CONFDIR) + 1 )))
322 fatal_error( "out of memory\n" );
323 strcpy( confdir, home );
324 strcat( confdir, CONFDIR );
326 mkdir( confdir, 0755 ); /* just in case */
332 /***********************************************************************
335 * Start a new wine server.
337 static void start_server( const char *oldcwd )
339 static int started; /* we only try once */
344 if (pid == -1) fatal_perror( "fork" );
348 /* first try the installation dir */
349 execl( BINDIR "/wineserver", "wineserver", NULL );
350 if (oldcwd) chdir( oldcwd );
351 /* now try the dir we were launched from */
352 if (!(path = malloc( strlen(argv0) + 20 )))
353 fatal_error( "out of memory\n" );
354 if ((p = strrchr( strcpy( path, argv0 ), '/' )))
356 strcpy( p, "/wineserver" );
357 execl( path, "wineserver", NULL );
358 strcpy( p, "/server/wineserver" );
359 execl( path, "wineserver", NULL );
361 /* now try the path */
362 execlp( "wineserver", "wineserver", NULL );
363 /* and finally the current dir */
364 execl( "./server/wineserver", "wineserver", NULL );
365 fatal_error( "could not exec wineserver\n" );
368 waitpid( pid, &status, 0 );
369 status = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
370 if (status) exit(status); /* server failed */
374 /***********************************************************************
377 * Attempt to connect to an existing server socket.
378 * We need to be in the server directory already.
380 static int server_connect( const char *oldcwd, const char *serverdir )
382 struct sockaddr_un addr;
386 /* chdir to the server directory */
387 if (chdir( serverdir ) == -1)
389 if (errno != ENOENT) fatal_perror( "chdir to %s", serverdir );
390 start_server( NULL );
391 if (chdir( serverdir ) == -1) fatal_perror( "chdir to %s", serverdir );
394 /* make sure we are at the right place */
395 if (stat( ".", &st ) == -1) fatal_perror( "stat %s", serverdir );
396 if (st.st_uid != getuid()) fatal_error( "'%s' is not owned by you\n", serverdir );
397 if (st.st_mode & 077) fatal_error( "'%s' must not be accessible by other users\n", serverdir );
399 /* check for an existing socket */
400 if (lstat( SOCKETNAME, &st ) == -1)
402 if (errno != ENOENT) fatal_perror( "lstat %s/%s", serverdir, SOCKETNAME );
403 start_server( oldcwd );
404 if (lstat( SOCKETNAME, &st ) == -1) fatal_perror( "lstat %s/%s", serverdir, SOCKETNAME );
407 /* make sure the socket is sane */
408 if (!S_ISSOCK(st.st_mode))
409 fatal_error( "'%s/%s' is not a socket\n", serverdir, SOCKETNAME );
410 if (st.st_uid != getuid())
411 fatal_error( "'%s/%s' is not owned by you\n", serverdir, SOCKETNAME );
413 /* try to connect to it */
414 if ((s = socket( AF_UNIX, SOCK_STREAM, 0 )) == -1) fatal_perror( "socket" );
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 (connect( s, (struct sockaddr *)&addr, slen ) == -1)
423 usleep( 50000 ); /* in case the server was starting right now */
424 if (connect( s, (struct sockaddr *)&addr, slen ) == -1)
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 fcntl( s, F_SETFD, 1 ); /* set close on exec flag */
435 /***********************************************************************
438 * Start the server and create the initial socket pair.
440 int CLIENT_InitServer(void)
445 char *oldcwd, *serverdir;
446 const char *configdir;
448 /* first check if we inherited the socket fd */
449 if ((env_fd = getenv( "__WINE_FD" )) && isdigit(env_fd[0]))
452 if (fcntl( fd, F_SETFD, 1 ) != -1) return fd; /* set close on exec flag */
455 /* retrieve the current directory */
456 for (size = 512; ; size *= 2)
458 if (!(oldcwd = malloc( size ))) break;
459 if (getcwd( oldcwd, size )) break;
461 if (errno == ERANGE) continue;
466 /* get the server directory name */
467 if (gethostname( hostname, sizeof(hostname) ) == -1) fatal_perror( "gethostname" );
468 configdir = get_config_dir();
469 serverdir = malloc( strlen(configdir) + strlen(SERVERDIR) + strlen(hostname) + 1 );
470 if (!serverdir) fatal_error( "out of memory\n" );
471 strcpy( serverdir, configdir );
472 strcat( serverdir, SERVERDIR );
473 strcat( serverdir, hostname );
475 /* connect to the server */
476 fd = server_connect( oldcwd, serverdir );
478 /* switch back to the starting directory */
488 /***********************************************************************
491 * Send an init thread request. Return 0 if OK.
493 int CLIENT_InitThread(void)
495 struct get_thread_buffer_request *first_req;
496 struct init_thread_request *req;
497 TEB *teb = NtCurrentTeb();
500 if (wait_reply_fd( &fd ) || (fd == -1))
501 server_protocol_error( "no fd passed on first request\n" );
502 if ((teb->buffer_size = lseek( fd, 0, SEEK_END )) == -1) server_perror( "lseek" );
503 teb->buffer = mmap( 0, teb->buffer_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 );
505 if (teb->buffer == (void*)-1) server_perror( "mmap" );
506 first_req = teb->buffer;
507 teb->process->server_pid = first_req->pid;
508 teb->pid = first_req->pid;
509 teb->tid = first_req->tid;
510 if (first_req->version != SERVER_PROTOCOL_VERSION)
511 server_protocol_error( "version mismatch %d/%d.\n"
512 "Your %s binary was not upgraded correctly,\n"
513 "or you have an older one somewhere in your PATH.\n",
514 first_req->version, SERVER_PROTOCOL_VERSION,
515 (first_req->version > SERVER_PROTOCOL_VERSION) ? "wine" : "wineserver" );
516 if (first_req->boot) boot_thread_id = teb->tid;
517 else if (boot_thread_id == teb->tid) boot_thread_id = 0;
520 req->unix_pid = getpid();
522 req->entry = teb->entry_point;
523 return server_call_noerr( REQ_INIT_THREAD );
526 /***********************************************************************
529 * Signal that we have finished booting, and set debug level.
531 int CLIENT_BootDone( int debug_level )
533 struct boot_done_request *req = get_req_buffer();
534 req->debug_level = debug_level;
535 return server_call( REQ_BOOT_DONE );
539 /***********************************************************************
540 * CLIENT_IsBootThread
542 * Return TRUE if current thread is the boot thread.
544 int CLIENT_IsBootThread(void)
546 return (GetCurrentThreadId() == (DWORD)boot_thread_id);