blob: c8523eeb408ca1bf2ea89a99ec9f534ec6112bca [file] [log] [blame]
Alexandre Julliard642d3131998-07-12 19:29:36 +00001/*
2 * Client part of the client/server communication
3 *
4 * Copyright (C) 1998 Alexandre Julliard
Alexandre Julliard0799c1a2002-03-09 23:29:33 +00005 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Alexandre Julliard642d3131998-07-12 19:29:36 +000019 */
20
François Gouget14259412001-11-06 20:57:11 +000021#include "config.h"
Francois Gouget386cf6e2001-10-14 16:25:47 +000022#include "wine/port.h"
Howard Abrams13277481999-07-10 13:16:29 +000023
Alexandre Julliard642d3131998-07-12 19:29:36 +000024#include <assert.h>
Alexandre Julliard2fe57772000-01-25 01:40:27 +000025#include <ctype.h>
Alexandre Julliard642d3131998-07-12 19:29:36 +000026#include <errno.h>
Alexandre Julliard62a8b431999-01-19 17:48:23 +000027#include <fcntl.h>
Alexandre Julliardde1d5ad2000-04-06 20:36:17 +000028#include <pwd.h>
Alexandre Julliard42cc2bd2000-06-07 03:49:41 +000029#include <signal.h>
Alexandre Julliard642d3131998-07-12 19:29:36 +000030#include <stdio.h>
31#include <string.h>
Alexandre Julliard829fe321998-07-26 14:27:39 +000032#include <sys/types.h>
Patrik Stridvall96336321999-10-24 22:13:47 +000033#ifdef HAVE_SYS_SOCKET_H
34# include <sys/socket.h>
35#endif
Alexandre Julliardd8041112000-04-14 14:42:41 +000036#ifdef HAVE_SYS_WAIT_H
37#include <sys/wait.h>
38#endif
Alexandre Julliard2fe57772000-01-25 01:40:27 +000039#include <sys/un.h>
Howard Abrams13277481999-07-10 13:16:29 +000040#ifdef HAVE_SYS_MMAN_H
Alexandre Julliard5bc78081999-06-22 17:26:53 +000041#include <sys/mman.h>
Howard Abrams13277481999-07-10 13:16:29 +000042#endif
Alexandre Julliard2fe57772000-01-25 01:40:27 +000043#include <sys/stat.h>
Alexandre Julliard642d3131998-07-12 19:29:36 +000044#include <sys/uio.h>
45#include <unistd.h>
Marcus Meissner317af321999-02-17 13:51:06 +000046#include <stdarg.h>
Alexandre Julliard642d3131998-07-12 19:29:36 +000047
Alexandre Julliard642d3131998-07-12 19:29:36 +000048#include "thread.h"
Alexandre Julliard37e95032001-07-19 00:39:09 +000049#include "wine/server.h"
Alexandre Julliard642d3131998-07-12 19:29:36 +000050#include "winerror.h"
Alexandre Julliard2fe57772000-01-25 01:40:27 +000051#include "options.h"
Alexandre Julliard642d3131998-07-12 19:29:36 +000052
Alexandre Julliard829fe321998-07-26 14:27:39 +000053/* Some versions of glibc don't define this */
54#ifndef SCM_RIGHTS
55#define SCM_RIGHTS 1
56#endif
Alexandre Julliard642d3131998-07-12 19:29:36 +000057
Alexandre Julliardde1d5ad2000-04-06 20:36:17 +000058#define CONFDIR "/.wine" /* directory for Wine config relative to $HOME */
Alexandre Julliard2fe57772000-01-25 01:40:27 +000059#define SERVERDIR "/wineserver-" /* server socket directory (hostname appended) */
60#define SOCKETNAME "socket" /* name of the socket file */
61
Dmitry Timoshkovb3eaa8662001-09-11 00:29:24 +000062#ifndef HAVE_MSGHDR_ACCRIGHTS
Alexandre Julliardebe29ef1999-06-26 08:43:26 +000063/* data structure used to pass an fd with sendmsg/recvmsg */
64struct cmsg_fd
65{
66 int len; /* sizeof structure */
67 int level; /* SOL_SOCKET */
68 int type; /* SCM_RIGHTS */
69 int fd; /* fd to pass */
70};
Dmitry Timoshkovb3eaa8662001-09-11 00:29:24 +000071#endif /* HAVE_MSGHDR_ACCRIGHTS */
Alexandre Julliard767e6f61998-08-09 12:47:43 +000072
Alexandre Julliard2fe57772000-01-25 01:40:27 +000073static void *boot_thread_id;
Alexandre Julliardf5242402001-02-28 21:45:23 +000074static sigset_t block_set; /* signals to block during server calls */
75static int fd_socket; /* socket to exchange file descriptors with the server */
Alexandre Julliard2fe57772000-01-25 01:40:27 +000076
77/* die on a fatal error; use only during initialization */
Alexandre Julliardd8041112000-04-14 14:42:41 +000078static void fatal_error( const char *err, ... ) WINE_NORETURN;
Alexandre Julliard2fe57772000-01-25 01:40:27 +000079static void fatal_error( const char *err, ... )
80{
81 va_list args;
82
83 va_start( args, err );
84 fprintf( stderr, "wine: " );
85 vfprintf( stderr, err, args );
86 va_end( args );
87 exit(1);
88}
89
90/* die on a fatal error; use only during initialization */
Alexandre Julliardd8041112000-04-14 14:42:41 +000091static void fatal_perror( const char *err, ... ) WINE_NORETURN;
Alexandre Julliard2fe57772000-01-25 01:40:27 +000092static void fatal_perror( const char *err, ... )
93{
94 va_list args;
95
96 va_start( args, err );
97 fprintf( stderr, "wine: " );
98 vfprintf( stderr, err, args );
99 perror( " " );
100 va_end( args );
101 exit(1);
102}
103
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000104/***********************************************************************
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000105 * server_protocol_error
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000106 */
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000107void server_protocol_error( const char *err, ... )
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000108{
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000109 va_list args;
110
111 va_start( args, err );
Alexandre Julliard8859d772001-03-01 22:13:49 +0000112 fprintf( stderr, "wine client error:%p: ", NtCurrentTeb()->tid );
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000113 vfprintf( stderr, err, args );
114 va_end( args );
Alexandre Julliard5016e922002-01-07 18:04:07 +0000115 SYSDEPS_AbortThread(1);
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000116}
117
118
Alexandre Julliard642d3131998-07-12 19:29:36 +0000119/***********************************************************************
Alexandre Julliard5f195f82001-02-20 23:45:07 +0000120 * server_protocol_perror
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000121 */
Alexandre Julliard5f195f82001-02-20 23:45:07 +0000122void server_protocol_perror( const char *err )
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000123{
Alexandre Julliard8859d772001-03-01 22:13:49 +0000124 fprintf( stderr, "wine client error:%p: ", NtCurrentTeb()->tid );
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000125 perror( err );
Alexandre Julliard5016e922002-01-07 18:04:07 +0000126 SYSDEPS_AbortThread(1);
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000127}
128
129
130/***********************************************************************
131 * send_request
Alexandre Julliard642d3131998-07-12 19:29:36 +0000132 *
133 * Send a request to the server.
134 */
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000135static void send_request( const struct __server_request_info *req )
Alexandre Julliard642d3131998-07-12 19:29:36 +0000136{
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000137 int i, ret;
Alexandre Julliardd90e9642001-02-21 04:21:50 +0000138
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000139 if (!req->u.req.request_header.request_size)
140 {
141 if ((ret = write( NtCurrentTeb()->request_fd, &req->u.req,
142 sizeof(req->u.req) )) == sizeof(req->u.req)) return;
143
144 }
145 else
146 {
147 struct iovec vec[__SERVER_MAX_DATA+1];
148
149 vec[0].iov_base = (void *)&req->u.req;
150 vec[0].iov_len = sizeof(req->u.req);
151 for (i = 0; i < req->data_count; i++)
152 {
153 vec[i+1].iov_base = (void *)req->data[i].ptr;
154 vec[i+1].iov_len = req->data[i].size;
155 }
156 if ((ret = writev( NtCurrentTeb()->request_fd, vec, i+1 )) ==
157 req->u.req.request_header.request_size + sizeof(req->u.req)) return;
158 }
159
Alexandre Julliardd90e9642001-02-21 04:21:50 +0000160 if (ret >= 0) server_protocol_error( "partial write %d\n", ret );
Alexandre Julliard5016e922002-01-07 18:04:07 +0000161 if (errno == EPIPE) SYSDEPS_AbortThread(0);
Alexandre Julliardd90e9642001-02-21 04:21:50 +0000162 server_protocol_perror( "sendmsg" );
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000163}
164
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000165
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000166/***********************************************************************
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000167 * read_reply_data
Alexandre Julliard642d3131998-07-12 19:29:36 +0000168 *
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000169 * Read data from the reply buffer; helper for wait_reply.
Alexandre Julliard642d3131998-07-12 19:29:36 +0000170 */
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000171static void read_reply_data( void *buffer, size_t size )
Alexandre Julliard642d3131998-07-12 19:29:36 +0000172{
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000173 int ret;
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000174
175 for (;;)
176 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000177 if ((ret = read( NtCurrentTeb()->reply_fd, buffer, size )) > 0)
178 {
179 if (!(size -= ret)) return;
180 buffer = (char *)buffer + ret;
181 continue;
182 }
Alexandre Julliard12f29b52000-03-17 15:16:57 +0000183 if (!ret) break;
Alexandre Julliard86113532000-08-29 03:54:30 +0000184 if (errno == EINTR) continue;
185 if (errno == EPIPE) break;
Alexandre Julliard5f195f82001-02-20 23:45:07 +0000186 server_protocol_perror("read");
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000187 }
Alexandre Julliard12f29b52000-03-17 15:16:57 +0000188 /* the server closed the connection; time to die... */
Alexandre Julliard5016e922002-01-07 18:04:07 +0000189 SYSDEPS_AbortThread(0);
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000190}
191
192
193/***********************************************************************
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000194 * wait_reply
195 *
196 * Wait for a reply from the server.
197 */
198inline static void wait_reply( struct __server_request_info *req )
199{
200 read_reply_data( &req->u.reply, sizeof(req->u.reply) );
201 if (req->u.reply.reply_header.reply_size)
202 read_reply_data( req->reply_data, req->u.reply.reply_header.reply_size );
203}
204
205
206/***********************************************************************
Patrik Stridvalld0a41772001-02-14 23:11:17 +0000207 * wine_server_call (NTDLL.@)
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000208 *
209 * Perform a server call.
210 */
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000211unsigned int wine_server_call( void *req_ptr )
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000212{
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000213 struct __server_request_info * const req = req_ptr;
Alexandre Julliardf5242402001-02-28 21:45:23 +0000214 sigset_t old_set;
215
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000216 memset( (char *)&req->u.req + req->size, 0, sizeof(req->u.req) - req->size );
Alexandre Julliardf5242402001-02-28 21:45:23 +0000217 sigprocmask( SIG_BLOCK, &block_set, &old_set );
Alexandre Julliard67a74992001-02-27 02:09:16 +0000218 send_request( req );
219 wait_reply( req );
Alexandre Julliardf5242402001-02-28 21:45:23 +0000220 sigprocmask( SIG_SETMASK, &old_set, NULL );
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000221 return req->u.reply.reply_header.error;
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000222}
223
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000224
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000225/***********************************************************************
Alexandre Julliardf5242402001-02-28 21:45:23 +0000226 * wine_server_send_fd
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000227 *
Alexandre Julliardf5242402001-02-28 21:45:23 +0000228 * Send a file descriptor to the server.
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000229 */
Alexandre Julliardf5242402001-02-28 21:45:23 +0000230void wine_server_send_fd( int fd )
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000231{
Alexandre Julliardf5242402001-02-28 21:45:23 +0000232#ifndef HAVE_MSGHDR_ACCRIGHTS
233 struct cmsg_fd cmsg;
234#endif
235 struct send_fd data;
236 struct msghdr msghdr;
237 struct iovec vec;
238 int ret;
239
240 vec.iov_base = (void *)&data;
241 vec.iov_len = sizeof(data);
242
243 msghdr.msg_name = NULL;
244 msghdr.msg_namelen = 0;
245 msghdr.msg_iov = &vec;
246 msghdr.msg_iovlen = 1;
247
248#ifdef HAVE_MSGHDR_ACCRIGHTS
249 msghdr.msg_accrights = (void *)&fd;
250 msghdr.msg_accrightslen = sizeof(fd);
251#else /* HAVE_MSGHDR_ACCRIGHTS */
252 cmsg.len = sizeof(cmsg);
253 cmsg.level = SOL_SOCKET;
254 cmsg.type = SCM_RIGHTS;
255 cmsg.fd = fd;
256 msghdr.msg_control = &cmsg;
257 msghdr.msg_controllen = sizeof(cmsg);
258 msghdr.msg_flags = 0;
259#endif /* HAVE_MSGHDR_ACCRIGHTS */
260
261 data.tid = (void *)GetCurrentThreadId();
262 data.fd = fd;
263
264 for (;;)
265 {
266 if ((ret = sendmsg( fd_socket, &msghdr, 0 )) == sizeof(data)) return;
267 if (ret >= 0) server_protocol_error( "partial write %d\n", ret );
268 if (errno == EINTR) continue;
Alexandre Julliard5016e922002-01-07 18:04:07 +0000269 if (errno == EPIPE) SYSDEPS_AbortThread(0);
Alexandre Julliardf5242402001-02-28 21:45:23 +0000270 server_protocol_perror( "sendmsg" );
271 }
Alexandre Julliard6ebbe3c1999-01-01 17:04:00 +0000272}
273
274
275/***********************************************************************
Alexandre Julliard8859d772001-03-01 22:13:49 +0000276 * receive_fd
Alexandre Julliardd549f692000-12-22 02:04:15 +0000277 *
278 * Receive a file descriptor passed from the server.
Alexandre Julliardd549f692000-12-22 02:04:15 +0000279 */
Alexandre Julliard8859d772001-03-01 22:13:49 +0000280static int receive_fd( handle_t *handle )
Alexandre Julliardd549f692000-12-22 02:04:15 +0000281{
282 struct iovec vec;
Alexandre Julliard8859d772001-03-01 22:13:49 +0000283 int ret, fd;
Alexandre Julliardd549f692000-12-22 02:04:15 +0000284
285#ifdef HAVE_MSGHDR_ACCRIGHTS
286 struct msghdr msghdr;
287
288 fd = -1;
289 msghdr.msg_accrights = (void *)&fd;
290 msghdr.msg_accrightslen = sizeof(fd);
291#else /* HAVE_MSGHDR_ACCRIGHTS */
292 struct msghdr msghdr;
293 struct cmsg_fd cmsg;
294
295 cmsg.len = sizeof(cmsg);
296 cmsg.level = SOL_SOCKET;
297 cmsg.type = SCM_RIGHTS;
298 cmsg.fd = -1;
299 msghdr.msg_control = &cmsg;
300 msghdr.msg_controllen = sizeof(cmsg);
301 msghdr.msg_flags = 0;
302#endif /* HAVE_MSGHDR_ACCRIGHTS */
303
304 msghdr.msg_name = NULL;
305 msghdr.msg_namelen = 0;
306 msghdr.msg_iov = &vec;
307 msghdr.msg_iovlen = 1;
Alexandre Julliard8859d772001-03-01 22:13:49 +0000308 vec.iov_base = (void *)handle;
309 vec.iov_len = sizeof(*handle);
Alexandre Julliardd549f692000-12-22 02:04:15 +0000310
311 for (;;)
312 {
Alexandre Julliard8859d772001-03-01 22:13:49 +0000313 if ((ret = recvmsg( fd_socket, &msghdr, 0 )) > 0)
Alexandre Julliardd549f692000-12-22 02:04:15 +0000314 {
315#ifndef HAVE_MSGHDR_ACCRIGHTS
316 fd = cmsg.fd;
317#endif
Alexandre Julliard8859d772001-03-01 22:13:49 +0000318 if (fd == -1) server_protocol_error( "no fd received for handle %d\n", *handle );
319 fcntl( fd, F_SETFD, 1 ); /* set close on exec flag */
Alexandre Julliardd549f692000-12-22 02:04:15 +0000320 return fd;
321 }
322 if (!ret) break;
323 if (errno == EINTR) continue;
324 if (errno == EPIPE) break;
Alexandre Julliard5f195f82001-02-20 23:45:07 +0000325 server_protocol_perror("recvmsg");
Alexandre Julliardd549f692000-12-22 02:04:15 +0000326 }
327 /* the server closed the connection; time to die... */
Alexandre Julliard5016e922002-01-07 18:04:07 +0000328 SYSDEPS_AbortThread(0);
Alexandre Julliardd549f692000-12-22 02:04:15 +0000329}
330
331
332/***********************************************************************
Alexandre Julliard8d1550d2002-03-23 18:48:12 +0000333 * store_cached_fd
Alexandre Julliard8859d772001-03-01 22:13:49 +0000334 *
Alexandre Julliard8d1550d2002-03-23 18:48:12 +0000335 * Store the cached fd value for a given handle back into the server.
336 * Returns the new fd, which can be different if there was already an
337 * fd in the cache for that handle.
Alexandre Julliard8859d772001-03-01 22:13:49 +0000338 */
Alexandre Julliard8d1550d2002-03-23 18:48:12 +0000339inline static int store_cached_fd( int fd, handle_t handle )
Alexandre Julliard8859d772001-03-01 22:13:49 +0000340{
Alexandre Julliard8859d772001-03-01 22:13:49 +0000341 SERVER_START_REQ( set_handle_info )
342 {
Alexandre Julliard8d1550d2002-03-23 18:48:12 +0000343 req->handle = handle;
Alexandre Julliard8859d772001-03-01 22:13:49 +0000344 req->flags = 0;
345 req->mask = 0;
346 req->fd = fd;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000347 if (!wine_server_call( req ))
Alexandre Julliard8859d772001-03-01 22:13:49 +0000348 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000349 if (reply->cur_fd != fd)
Alexandre Julliard8859d772001-03-01 22:13:49 +0000350 {
351 /* someone was here before us */
352 close( fd );
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000353 fd = reply->cur_fd;
Alexandre Julliard8859d772001-03-01 22:13:49 +0000354 }
355 }
356 else
357 {
358 close( fd );
359 fd = -1;
360 }
361 }
362 SERVER_END_REQ;
Alexandre Julliard8859d772001-03-01 22:13:49 +0000363 return fd;
364}
365
366
367/***********************************************************************
Alexandre Julliard8d1550d2002-03-23 18:48:12 +0000368 * wine_server_handle_to_fd (NTDLL.@)
369 *
370 * Retrieve the Unix fd corresponding to a file handle.
371 */
372int wine_server_handle_to_fd( handle_t handle, unsigned int access, int *unix_fd,
373 enum fd_type *type, int *flags )
374{
375 handle_t fd_handle;
376 int ret, fd = -1;
377
378 *unix_fd = -1;
379 for (;;)
380 {
381 SERVER_START_REQ( get_handle_fd )
382 {
383 req->handle = handle;
384 req->access = access;
385 if (!(ret = wine_server_call( req ))) fd = reply->fd;
386 if (type) *type = reply->type;
387 if (flags) *flags = reply->flags;
388 }
389 SERVER_END_REQ;
390 if (ret) return ret;
391
392 if (fd != -1) break;
393
394 /* it wasn't in the cache, get it from the server */
395 fd = receive_fd( &fd_handle );
396 /* and store it back into the cache */
397 fd = store_cached_fd( fd, fd_handle );
398
399 if (fd_handle == handle) break;
400 /* if we received a different handle this means there was
401 * a race with another thread; we restart everything from
402 * scratch in this case.
403 */
404 }
405
406 if ((fd != -1) && ((fd = dup(fd)) == -1)) return STATUS_TOO_MANY_OPENED_FILES;
407 *unix_fd = fd;
408 return STATUS_SUCCESS;
409}
410
411
412/***********************************************************************
Alexandre Julliardde1d5ad2000-04-06 20:36:17 +0000413 * get_config_dir
414 *
415 * Return the configuration directory ($WINEPREFIX or $HOME/.wine)
416 */
417const char *get_config_dir(void)
418{
419 static char *confdir;
420 if (!confdir)
421 {
422 const char *prefix = getenv( "WINEPREFIX" );
423 if (prefix)
424 {
425 int len = strlen(prefix);
426 if (!(confdir = strdup( prefix ))) fatal_error( "out of memory\n" );
427 if (len > 1 && confdir[len-1] == '/') confdir[len-1] = 0;
428 }
429 else
430 {
431 const char *home = getenv( "HOME" );
432 if (!home)
433 {
434 struct passwd *pwd = getpwuid( getuid() );
435 if (!pwd) fatal_error( "could not find your home directory\n" );
436 home = pwd->pw_dir;
437 }
438 if (!(confdir = malloc( strlen(home) + strlen(CONFDIR) + 1 )))
439 fatal_error( "out of memory\n" );
440 strcpy( confdir, home );
441 strcat( confdir, CONFDIR );
442 }
Alexandre Julliardde1d5ad2000-04-06 20:36:17 +0000443 }
444 return confdir;
445}
446
447
448/***********************************************************************
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000449 * start_server
450 *
451 * Start a new wine server.
452 */
453static void start_server( const char *oldcwd )
454{
455 static int started; /* we only try once */
Alexandre Julliardc10c9ef2000-08-11 21:16:53 +0000456 char *path, *p;
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000457 if (!started)
458 {
Alexandre Julliardd8041112000-04-14 14:42:41 +0000459 int status;
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000460 int pid = fork();
461 if (pid == -1) fatal_perror( "fork" );
462 if (!pid)
463 {
Alexandre Julliard50097a02000-09-10 03:24:28 +0000464 /* if server is explicitly specified, use this */
465 if ((p = getenv("WINESERVER")))
466 {
Alexandre Julliard61d26d72001-05-09 19:46:39 +0000467 if (p[0] != '/' && oldcwd[0] == '/') /* make it an absolute path */
468 {
469 if (!(path = malloc( strlen(oldcwd) + strlen(p) + 1 )))
470 fatal_error( "out of memory\n" );
471 sprintf( path, "%s/%s", oldcwd, p );
472 p = path;
473 }
Alexandre Julliard50097a02000-09-10 03:24:28 +0000474 execl( p, "wineserver", NULL );
475 fatal_perror( "could not exec the server '%s'\n"
476 " specified in the WINESERVER environment variable", p );
477 }
478
Alexandre Julliardeafa3912000-01-25 21:19:58 +0000479 /* first try the installation dir */
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000480 execl( BINDIR "/wineserver", "wineserver", NULL );
Alexandre Julliardc10c9ef2000-08-11 21:16:53 +0000481
Alexandre Julliardeafa3912000-01-25 21:19:58 +0000482 /* now try the dir we were launched from */
Alexandre Julliardc10c9ef2000-08-11 21:16:53 +0000483 if (full_argv0)
484 {
485 if (!(path = malloc( strlen(full_argv0) + 20 )))
486 fatal_error( "out of memory\n" );
487 if ((p = strrchr( strcpy( path, full_argv0 ), '/' )))
488 {
489 strcpy( p, "/wineserver" );
490 execl( path, "wineserver", NULL );
491 strcpy( p, "/server/wineserver" );
492 execl( path, "wineserver", NULL );
493 }
Huw D M Davies94237de2000-08-14 13:26:30 +0000494 free(path);
Alexandre Julliardc10c9ef2000-08-11 21:16:53 +0000495 }
496
497 /* now try the path */
498 execlp( "wineserver", "wineserver", NULL );
499
500 /* and finally the current dir */
501 if (!(path = malloc( strlen(oldcwd) + 20 )))
Dimitrie O. Paun9ad96362000-03-19 14:29:50 +0000502 fatal_error( "out of memory\n" );
Huw D M Davies94237de2000-08-14 13:26:30 +0000503 p = strcpy( path, oldcwd ) + strlen( oldcwd );
504 strcpy( p, "/wineserver" );
505 execl( path, "wineserver", NULL );
506 strcpy( p, "/server/wineserver" );
507 execl( path, "wineserver", NULL );
508 free(path);
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000509 fatal_error( "could not exec wineserver\n" );
510 }
511 started = 1;
Alexandre Julliardd8041112000-04-14 14:42:41 +0000512 waitpid( pid, &status, 0 );
513 status = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
514 if (status) exit(status); /* server failed */
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000515 }
516}
517
518/***********************************************************************
519 * server_connect
520 *
521 * Attempt to connect to an existing server socket.
522 * We need to be in the server directory already.
523 */
524static int server_connect( const char *oldcwd, const char *serverdir )
525{
526 struct sockaddr_un addr;
527 struct stat st;
Alexandre Julliardc10c9ef2000-08-11 21:16:53 +0000528 int s, slen, retry;
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000529
Alexandre Julliardd8041112000-04-14 14:42:41 +0000530 /* chdir to the server directory */
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000531 if (chdir( serverdir ) == -1)
532 {
533 if (errno != ENOENT) fatal_perror( "chdir to %s", serverdir );
Alexandre Julliardc10c9ef2000-08-11 21:16:53 +0000534 start_server( "." );
Alexandre Julliardd8041112000-04-14 14:42:41 +0000535 if (chdir( serverdir ) == -1) fatal_perror( "chdir to %s", serverdir );
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000536 }
Alexandre Julliardd8041112000-04-14 14:42:41 +0000537
538 /* make sure we are at the right place */
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000539 if (stat( ".", &st ) == -1) fatal_perror( "stat %s", serverdir );
540 if (st.st_uid != getuid()) fatal_error( "'%s' is not owned by you\n", serverdir );
541 if (st.st_mode & 077) fatal_error( "'%s' must not be accessible by other users\n", serverdir );
542
Alexandre Julliardc10c9ef2000-08-11 21:16:53 +0000543 for (retry = 0; retry < 3; retry++)
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000544 {
Alexandre Julliardc10c9ef2000-08-11 21:16:53 +0000545 /* if not the first try, wait a bit to leave the server time to exit */
546 if (retry) usleep( 100000 * retry * retry );
Alexandre Julliardd8041112000-04-14 14:42:41 +0000547
Alexandre Julliardc10c9ef2000-08-11 21:16:53 +0000548 /* check for an existing socket */
549 if (lstat( SOCKETNAME, &st ) == -1)
550 {
551 if (errno != ENOENT) fatal_perror( "lstat %s/%s", serverdir, SOCKETNAME );
552 start_server( oldcwd );
553 if (lstat( SOCKETNAME, &st ) == -1) fatal_perror( "lstat %s/%s", serverdir, SOCKETNAME );
554 }
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000555
Roberto Augusto Pungartnik19f8dda2000-09-22 22:19:58 +0000556 /* make sure the socket is sane (ISFIFO needed for Solaris) */
557 if (!S_ISSOCK(st.st_mode) && !S_ISFIFO(st.st_mode))
Alexandre Julliardc10c9ef2000-08-11 21:16:53 +0000558 fatal_error( "'%s/%s' is not a socket\n", serverdir, SOCKETNAME );
559 if (st.st_uid != getuid())
560 fatal_error( "'%s/%s' is not owned by you\n", serverdir, SOCKETNAME );
561
562 /* try to connect to it */
563 addr.sun_family = AF_UNIX;
564 strcpy( addr.sun_path, SOCKETNAME );
565 slen = sizeof(addr) - sizeof(addr.sun_path) + strlen(addr.sun_path) + 1;
Juergen Lock2d33ab92000-02-13 16:03:29 +0000566#ifdef HAVE_SOCKADDR_SUN_LEN
Alexandre Julliardc10c9ef2000-08-11 21:16:53 +0000567 addr.sun_len = slen;
Juergen Lock2d33ab92000-02-13 16:03:29 +0000568#endif
Alexandre Julliard42cc2bd2000-06-07 03:49:41 +0000569 if ((s = socket( AF_UNIX, SOCK_STREAM, 0 )) == -1) fatal_perror( "socket" );
Alexandre Julliardc10c9ef2000-08-11 21:16:53 +0000570 if (connect( s, (struct sockaddr *)&addr, slen ) != -1)
571 {
572 fcntl( s, F_SETFD, 1 ); /* set close on exec flag */
573 return s;
574 }
575 close( s );
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000576 }
Alexandre Julliardc10c9ef2000-08-11 21:16:53 +0000577 fatal_error( "file '%s/%s' exists,\n"
Andreas Mohra6d83eb2000-12-27 04:02:46 +0000578 " but I cannot connect to it; maybe the wineserver has crashed?\n"
Alexandre Julliardc10c9ef2000-08-11 21:16:53 +0000579 " If this is the case, you should remove this socket file and try again.\n",
580 serverdir, SOCKETNAME );
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000581}
582
583
584/***********************************************************************
Alexandre Julliardf0167521999-03-21 19:26:25 +0000585 * CLIENT_InitServer
Alexandre Julliard642d3131998-07-12 19:29:36 +0000586 *
Alexandre Julliardf0167521999-03-21 19:26:25 +0000587 * Start the server and create the initial socket pair.
Alexandre Julliard642d3131998-07-12 19:29:36 +0000588 */
Alexandre Julliard8859d772001-03-01 22:13:49 +0000589void CLIENT_InitServer(void)
Alexandre Julliard642d3131998-07-12 19:29:36 +0000590{
Alexandre Julliard8859d772001-03-01 22:13:49 +0000591 int size;
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000592 char hostname[64];
593 char *oldcwd, *serverdir;
594 const char *configdir;
Alexandre Julliard8859d772001-03-01 22:13:49 +0000595 handle_t dummy_handle;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000596
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000597 /* retrieve the current directory */
598 for (size = 512; ; size *= 2)
Alexandre Julliardf0167521999-03-21 19:26:25 +0000599 {
Dimitrie O. Paun9ad96362000-03-19 14:29:50 +0000600 if (!(oldcwd = malloc( size ))) break;
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000601 if (getcwd( oldcwd, size )) break;
602 free( oldcwd );
603 if (errno == ERANGE) continue;
604 oldcwd = NULL;
Ulrich Weigand371fd751999-04-11 17:13:03 +0000605 break;
Alexandre Julliardf0167521999-03-21 19:26:25 +0000606 }
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000607
Alexandre Julliarda3e0cfc2000-07-16 18:21:34 +0000608 /* if argv[0] is a relative path, make it absolute */
609 full_argv0 = argv0;
610 if (oldcwd && argv0[0] != '/' && strchr( argv0, '/' ))
611 {
612 char *new_argv0 = malloc( strlen(oldcwd) + strlen(argv0) + 2 );
613 if (new_argv0)
614 {
615 strcpy( new_argv0, oldcwd );
616 strcat( new_argv0, "/" );
617 strcat( new_argv0, argv0 );
618 full_argv0 = new_argv0;
619 }
620 }
621
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000622 /* get the server directory name */
623 if (gethostname( hostname, sizeof(hostname) ) == -1) fatal_perror( "gethostname" );
Alexandre Julliardde1d5ad2000-04-06 20:36:17 +0000624 configdir = get_config_dir();
Dimitrie O. Paun9ad96362000-03-19 14:29:50 +0000625 serverdir = malloc( strlen(configdir) + strlen(SERVERDIR) + strlen(hostname) + 1 );
626 if (!serverdir) fatal_error( "out of memory\n" );
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000627 strcpy( serverdir, configdir );
628 strcat( serverdir, SERVERDIR );
629 strcat( serverdir, hostname );
630
Alexandre Julliardd8041112000-04-14 14:42:41 +0000631 /* connect to the server */
Alexandre Julliard8859d772001-03-01 22:13:49 +0000632 fd_socket = server_connect( oldcwd, serverdir );
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000633
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000634 /* switch back to the starting directory */
635 if (oldcwd)
636 {
637 chdir( oldcwd );
638 free( oldcwd );
639 }
Alexandre Julliardf5242402001-02-28 21:45:23 +0000640
641 /* setup the signal mask */
642 sigemptyset( &block_set );
643 sigaddset( &block_set, SIGALRM );
644 sigaddset( &block_set, SIGIO );
645 sigaddset( &block_set, SIGINT );
646 sigaddset( &block_set, SIGHUP );
647
Alexandre Julliard8859d772001-03-01 22:13:49 +0000648 /* receive the first thread request fd on the main socket */
649 NtCurrentTeb()->request_fd = receive_fd( &dummy_handle );
650
651 CLIENT_InitThread();
652}
653
654
655/***********************************************************************
Alexandre Julliard642d3131998-07-12 19:29:36 +0000656 * CLIENT_InitThread
657 *
658 * Send an init thread request. Return 0 if OK.
659 */
Alexandre Julliard8859d772001-03-01 22:13:49 +0000660void CLIENT_InitThread(void)
Alexandre Julliard642d3131998-07-12 19:29:36 +0000661{
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000662 TEB *teb = NtCurrentTeb();
Alexandre Julliard8859d772001-03-01 22:13:49 +0000663 int version, ret;
Alexandre Julliarde5dedb12001-03-08 01:16:41 +0000664 int reply_pipe[2];
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000665
Alexandre Julliard42cc2bd2000-06-07 03:49:41 +0000666 /* ignore SIGPIPE so that we get a EPIPE error instead */
667 signal( SIGPIPE, SIG_IGN );
668
Alexandre Julliard8859d772001-03-01 22:13:49 +0000669 /* create the server->client communication pipes */
670 if (pipe( reply_pipe ) == -1) server_protocol_perror( "pipe" );
Alexandre Julliarde5dedb12001-03-08 01:16:41 +0000671 if (pipe( teb->wait_fd ) == -1) server_protocol_perror( "pipe" );
Alexandre Julliard8859d772001-03-01 22:13:49 +0000672 wine_server_send_fd( reply_pipe[1] );
Alexandre Julliarde5dedb12001-03-08 01:16:41 +0000673 wine_server_send_fd( teb->wait_fd[1] );
Alexandre Julliard8859d772001-03-01 22:13:49 +0000674 teb->reply_fd = reply_pipe[0];
Alexandre Julliardd549f692000-12-22 02:04:15 +0000675
Alexandre Julliard8859d772001-03-01 22:13:49 +0000676 /* set close on exec flag */
677 fcntl( teb->reply_fd, F_SETFD, 1 );
Alexandre Julliarde5dedb12001-03-08 01:16:41 +0000678 fcntl( teb->wait_fd[0], F_SETFD, 1 );
679 fcntl( teb->wait_fd[1], F_SETFD, 1 );
Alexandre Julliardd549f692000-12-22 02:04:15 +0000680
Alexandre Julliard8859d772001-03-01 22:13:49 +0000681 SERVER_START_REQ( init_thread )
682 {
683 req->unix_pid = getpid();
684 req->teb = teb;
685 req->entry = teb->entry_point;
686 req->reply_fd = reply_pipe[1];
Alexandre Julliarde5dedb12001-03-08 01:16:41 +0000687 req->wait_fd = teb->wait_fd[1];
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000688 ret = wine_server_call( req );
689 teb->pid = reply->pid;
690 teb->tid = reply->tid;
691 version = reply->version;
692 if (reply->boot) boot_thread_id = teb->tid;
Alexandre Julliard8859d772001-03-01 22:13:49 +0000693 else if (boot_thread_id == teb->tid) boot_thread_id = 0;
694 close( reply_pipe[1] );
Alexandre Julliard8859d772001-03-01 22:13:49 +0000695 }
696 SERVER_END_REQ;
Alexandre Julliardd90e9642001-02-21 04:21:50 +0000697
Alexandre Julliard8859d772001-03-01 22:13:49 +0000698 if (ret) server_protocol_error( "init_thread failed with status %x\n", ret );
699 if (version != SERVER_PROTOCOL_VERSION)
Alexandre Julliard5fb54562000-03-08 22:01:02 +0000700 server_protocol_error( "version mismatch %d/%d.\n"
701 "Your %s binary was not upgraded correctly,\n"
Alexandre Julliardd90e9642001-02-21 04:21:50 +0000702 "or you have an older one somewhere in your PATH.\n"
703 "Or maybe the wrong wineserver is still running?\n",
Alexandre Julliard8859d772001-03-01 22:13:49 +0000704 version, SERVER_PROTOCOL_VERSION,
705 (version > SERVER_PROTOCOL_VERSION) ? "wine" : "wineserver" );
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000706}
707
Alexandre Julliardd549f692000-12-22 02:04:15 +0000708
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000709/***********************************************************************
710 * CLIENT_BootDone
711 *
712 * Signal that we have finished booting, and set debug level.
713 */
Alexandre Julliard8859d772001-03-01 22:13:49 +0000714void CLIENT_BootDone( int debug_level )
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000715{
Alexandre Julliard67a74992001-02-27 02:09:16 +0000716 SERVER_START_REQ( boot_done )
Alexandre Julliard86113532000-08-29 03:54:30 +0000717 {
Alexandre Julliard86113532000-08-29 03:54:30 +0000718 req->debug_level = debug_level;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000719 wine_server_call( req );
Alexandre Julliard86113532000-08-29 03:54:30 +0000720 }
721 SERVER_END_REQ;
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000722}
723
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000724
725/***********************************************************************
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000726 * CLIENT_IsBootThread
Alexandre Julliard338e7571998-12-27 15:28:54 +0000727 *
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000728 * Return TRUE if current thread is the boot thread.
Alexandre Julliard338e7571998-12-27 15:28:54 +0000729 */
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000730int CLIENT_IsBootThread(void)
Alexandre Julliard338e7571998-12-27 15:28:54 +0000731{
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000732 return (GetCurrentThreadId() == (DWORD)boot_thread_id);
Alexandre Julliard338e7571998-12-27 15:28:54 +0000733}