blob: 466fef741000da6f66233f36b3b8e203737bc7ef [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 Julliard42cc2bd2000-06-07 03:49:41 +000028#include <signal.h>
Alexandre Julliard642d3131998-07-12 19:29:36 +000029#include <stdio.h>
30#include <string.h>
Alexandre Julliard829fe321998-07-26 14:27:39 +000031#include <sys/types.h>
Patrik Stridvall96336321999-10-24 22:13:47 +000032#ifdef HAVE_SYS_SOCKET_H
33# include <sys/socket.h>
34#endif
Alexandre Julliardd8041112000-04-14 14:42:41 +000035#ifdef HAVE_SYS_WAIT_H
36#include <sys/wait.h>
37#endif
Steven Edwards45e56812002-10-31 03:41:56 +000038#ifdef HAVE_SYS_UN_H
Alexandre Julliard2fe57772000-01-25 01:40:27 +000039#include <sys/un.h>
Steven Edwards45e56812002-10-31 03:41:56 +000040#endif
Howard Abrams13277481999-07-10 13:16:29 +000041#ifdef HAVE_SYS_MMAN_H
Alexandre Julliard5bc78081999-06-22 17:26:53 +000042#include <sys/mman.h>
Howard Abrams13277481999-07-10 13:16:29 +000043#endif
Alexandre Julliard2fe57772000-01-25 01:40:27 +000044#include <sys/stat.h>
Steven Edwards45e56812002-10-31 03:41:56 +000045#ifdef HAVE_SYS_UIO_H
Alexandre Julliard642d3131998-07-12 19:29:36 +000046#include <sys/uio.h>
Steven Edwards45e56812002-10-31 03:41:56 +000047#endif
Patrik Stridvalld016f812002-08-17 00:43:16 +000048#ifdef HAVE_UNISTD_H
49# include <unistd.h>
50#endif
Marcus Meissner317af321999-02-17 13:51:06 +000051#include <stdarg.h>
Alexandre Julliard642d3131998-07-12 19:29:36 +000052
Alexandre Julliard642d3131998-07-12 19:29:36 +000053#include "thread.h"
Alexandre Julliard4144b5b2002-06-20 23:21:27 +000054#include "wine/library.h"
Alexandre Julliard37e95032001-07-19 00:39:09 +000055#include "wine/server.h"
Alexandre Julliard642d3131998-07-12 19:29:36 +000056#include "winerror.h"
Alexandre Julliard2fe57772000-01-25 01:40:27 +000057#include "options.h"
Alexandre Julliard642d3131998-07-12 19:29:36 +000058
Alexandre Julliard829fe321998-07-26 14:27:39 +000059/* Some versions of glibc don't define this */
60#ifndef SCM_RIGHTS
61#define SCM_RIGHTS 1
62#endif
Alexandre Julliard642d3131998-07-12 19:29:36 +000063
Alexandre Julliard2fe57772000-01-25 01:40:27 +000064#define SOCKETNAME "socket" /* name of the socket file */
Alexandre Julliard4144b5b2002-06-20 23:21:27 +000065#define LOCKNAME "lock" /* name of the lock file */
Alexandre Julliard2fe57772000-01-25 01:40:27 +000066
Dmitry Timoshkovb3eaa8662001-09-11 00:29:24 +000067#ifndef HAVE_MSGHDR_ACCRIGHTS
Alexandre Julliardebe29ef1999-06-26 08:43:26 +000068/* data structure used to pass an fd with sendmsg/recvmsg */
69struct cmsg_fd
70{
71 int len; /* sizeof structure */
72 int level; /* SOL_SOCKET */
73 int type; /* SCM_RIGHTS */
74 int fd; /* fd to pass */
75};
Dmitry Timoshkovb3eaa8662001-09-11 00:29:24 +000076#endif /* HAVE_MSGHDR_ACCRIGHTS */
Alexandre Julliard767e6f61998-08-09 12:47:43 +000077
Alexandre Julliard54f22872002-10-03 19:54:57 +000078static DWORD boot_thread_id;
Alexandre Julliardf5242402001-02-28 21:45:23 +000079static sigset_t block_set; /* signals to block during server calls */
80static int fd_socket; /* socket to exchange file descriptors with the server */
Alexandre Julliard2fe57772000-01-25 01:40:27 +000081
Alexandre Julliard4144b5b2002-06-20 23:21:27 +000082#ifdef __GNUC__
83static void fatal_error( const char *err, ... ) __attribute__((noreturn, format(printf,1,2)));
84static void fatal_perror( const char *err, ... ) __attribute__((noreturn, format(printf,1,2)));
85static void server_connect_error( const char *serverdir ) __attribute__((noreturn));
86#endif
87
Alexandre Julliard2fe57772000-01-25 01:40:27 +000088/* die on a fatal error; use only during initialization */
89static void fatal_error( const char *err, ... )
90{
91 va_list args;
92
93 va_start( args, err );
94 fprintf( stderr, "wine: " );
95 vfprintf( stderr, err, args );
96 va_end( args );
97 exit(1);
98}
99
100/* die on a fatal error; use only during initialization */
101static void fatal_perror( const char *err, ... )
102{
103 va_list args;
104
105 va_start( args, err );
106 fprintf( stderr, "wine: " );
107 vfprintf( stderr, err, args );
108 perror( " " );
109 va_end( args );
110 exit(1);
111}
112
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000113/***********************************************************************
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000114 * server_protocol_error
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000115 */
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000116void server_protocol_error( const char *err, ... )
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000117{
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000118 va_list args;
119
120 va_start( args, err );
Alexandre Julliard54f22872002-10-03 19:54:57 +0000121 fprintf( stderr, "wine client error:%lx: ", NtCurrentTeb()->tid );
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000122 vfprintf( stderr, err, args );
123 va_end( args );
Alexandre Julliard5016e922002-01-07 18:04:07 +0000124 SYSDEPS_AbortThread(1);
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000125}
126
127
Alexandre Julliard642d3131998-07-12 19:29:36 +0000128/***********************************************************************
Alexandre Julliard5f195f82001-02-20 23:45:07 +0000129 * server_protocol_perror
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000130 */
Alexandre Julliard5f195f82001-02-20 23:45:07 +0000131void server_protocol_perror( const char *err )
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000132{
Alexandre Julliard54f22872002-10-03 19:54:57 +0000133 fprintf( stderr, "wine client error:%lx: ", NtCurrentTeb()->tid );
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000134 perror( err );
Alexandre Julliard5016e922002-01-07 18:04:07 +0000135 SYSDEPS_AbortThread(1);
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000136}
137
138
139/***********************************************************************
140 * send_request
Alexandre Julliard642d3131998-07-12 19:29:36 +0000141 *
142 * Send a request to the server.
143 */
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000144static void send_request( const struct __server_request_info *req )
Alexandre Julliard642d3131998-07-12 19:29:36 +0000145{
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000146 int i, ret;
Alexandre Julliardd90e9642001-02-21 04:21:50 +0000147
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000148 if (!req->u.req.request_header.request_size)
149 {
150 if ((ret = write( NtCurrentTeb()->request_fd, &req->u.req,
151 sizeof(req->u.req) )) == sizeof(req->u.req)) return;
152
153 }
154 else
155 {
156 struct iovec vec[__SERVER_MAX_DATA+1];
157
158 vec[0].iov_base = (void *)&req->u.req;
159 vec[0].iov_len = sizeof(req->u.req);
160 for (i = 0; i < req->data_count; i++)
161 {
162 vec[i+1].iov_base = (void *)req->data[i].ptr;
163 vec[i+1].iov_len = req->data[i].size;
164 }
165 if ((ret = writev( NtCurrentTeb()->request_fd, vec, i+1 )) ==
166 req->u.req.request_header.request_size + sizeof(req->u.req)) return;
167 }
168
Alexandre Julliardd90e9642001-02-21 04:21:50 +0000169 if (ret >= 0) server_protocol_error( "partial write %d\n", ret );
Alexandre Julliard5016e922002-01-07 18:04:07 +0000170 if (errno == EPIPE) SYSDEPS_AbortThread(0);
Alexandre Julliardd90e9642001-02-21 04:21:50 +0000171 server_protocol_perror( "sendmsg" );
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000172}
173
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000174
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000175/***********************************************************************
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000176 * read_reply_data
Alexandre Julliard642d3131998-07-12 19:29:36 +0000177 *
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000178 * Read data from the reply buffer; helper for wait_reply.
Alexandre Julliard642d3131998-07-12 19:29:36 +0000179 */
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000180static void read_reply_data( void *buffer, size_t size )
Alexandre Julliard642d3131998-07-12 19:29:36 +0000181{
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000182 int ret;
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000183
184 for (;;)
185 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000186 if ((ret = read( NtCurrentTeb()->reply_fd, buffer, size )) > 0)
187 {
188 if (!(size -= ret)) return;
189 buffer = (char *)buffer + ret;
190 continue;
191 }
Alexandre Julliard12f29b52000-03-17 15:16:57 +0000192 if (!ret) break;
Alexandre Julliard86113532000-08-29 03:54:30 +0000193 if (errno == EINTR) continue;
194 if (errno == EPIPE) break;
Alexandre Julliard5f195f82001-02-20 23:45:07 +0000195 server_protocol_perror("read");
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000196 }
Alexandre Julliard12f29b52000-03-17 15:16:57 +0000197 /* the server closed the connection; time to die... */
Alexandre Julliard5016e922002-01-07 18:04:07 +0000198 SYSDEPS_AbortThread(0);
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000199}
200
201
202/***********************************************************************
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000203 * wait_reply
204 *
205 * Wait for a reply from the server.
206 */
207inline static void wait_reply( struct __server_request_info *req )
208{
209 read_reply_data( &req->u.reply, sizeof(req->u.reply) );
210 if (req->u.reply.reply_header.reply_size)
211 read_reply_data( req->reply_data, req->u.reply.reply_header.reply_size );
212}
213
214
215/***********************************************************************
Patrik Stridvalld0a41772001-02-14 23:11:17 +0000216 * wine_server_call (NTDLL.@)
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000217 *
218 * Perform a server call.
219 */
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000220unsigned int wine_server_call( void *req_ptr )
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000221{
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000222 struct __server_request_info * const req = req_ptr;
Alexandre Julliardf5242402001-02-28 21:45:23 +0000223 sigset_t old_set;
224
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000225 memset( (char *)&req->u.req + req->size, 0, sizeof(req->u.req) - req->size );
Alexandre Julliardf5242402001-02-28 21:45:23 +0000226 sigprocmask( SIG_BLOCK, &block_set, &old_set );
Alexandre Julliard67a74992001-02-27 02:09:16 +0000227 send_request( req );
228 wait_reply( req );
Alexandre Julliardf5242402001-02-28 21:45:23 +0000229 sigprocmask( SIG_SETMASK, &old_set, NULL );
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000230 return req->u.reply.reply_header.error;
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000231}
232
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000233
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000234/***********************************************************************
Alexandre Julliardf5242402001-02-28 21:45:23 +0000235 * wine_server_send_fd
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000236 *
Alexandre Julliardf5242402001-02-28 21:45:23 +0000237 * Send a file descriptor to the server.
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000238 */
Alexandre Julliardf5242402001-02-28 21:45:23 +0000239void wine_server_send_fd( int fd )
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000240{
Alexandre Julliardf5242402001-02-28 21:45:23 +0000241#ifndef HAVE_MSGHDR_ACCRIGHTS
242 struct cmsg_fd cmsg;
243#endif
244 struct send_fd data;
245 struct msghdr msghdr;
246 struct iovec vec;
247 int ret;
248
249 vec.iov_base = (void *)&data;
250 vec.iov_len = sizeof(data);
251
252 msghdr.msg_name = NULL;
253 msghdr.msg_namelen = 0;
254 msghdr.msg_iov = &vec;
255 msghdr.msg_iovlen = 1;
256
257#ifdef HAVE_MSGHDR_ACCRIGHTS
258 msghdr.msg_accrights = (void *)&fd;
259 msghdr.msg_accrightslen = sizeof(fd);
260#else /* HAVE_MSGHDR_ACCRIGHTS */
261 cmsg.len = sizeof(cmsg);
262 cmsg.level = SOL_SOCKET;
263 cmsg.type = SCM_RIGHTS;
264 cmsg.fd = fd;
265 msghdr.msg_control = &cmsg;
266 msghdr.msg_controllen = sizeof(cmsg);
267 msghdr.msg_flags = 0;
268#endif /* HAVE_MSGHDR_ACCRIGHTS */
269
Alexandre Julliard54f22872002-10-03 19:54:57 +0000270 data.tid = GetCurrentThreadId();
Alexandre Julliardf5242402001-02-28 21:45:23 +0000271 data.fd = fd;
272
273 for (;;)
274 {
275 if ((ret = sendmsg( fd_socket, &msghdr, 0 )) == sizeof(data)) return;
276 if (ret >= 0) server_protocol_error( "partial write %d\n", ret );
277 if (errno == EINTR) continue;
Alexandre Julliard5016e922002-01-07 18:04:07 +0000278 if (errno == EPIPE) SYSDEPS_AbortThread(0);
Alexandre Julliardf5242402001-02-28 21:45:23 +0000279 server_protocol_perror( "sendmsg" );
280 }
Alexandre Julliard6ebbe3c1999-01-01 17:04:00 +0000281}
282
283
284/***********************************************************************
Alexandre Julliard8859d772001-03-01 22:13:49 +0000285 * receive_fd
Alexandre Julliardd549f692000-12-22 02:04:15 +0000286 *
287 * Receive a file descriptor passed from the server.
Alexandre Julliardd549f692000-12-22 02:04:15 +0000288 */
Alexandre Julliard51885742002-05-30 20:12:58 +0000289static int receive_fd( obj_handle_t *handle )
Alexandre Julliardd549f692000-12-22 02:04:15 +0000290{
291 struct iovec vec;
Alexandre Julliard8859d772001-03-01 22:13:49 +0000292 int ret, fd;
Alexandre Julliardd549f692000-12-22 02:04:15 +0000293
294#ifdef HAVE_MSGHDR_ACCRIGHTS
295 struct msghdr msghdr;
296
297 fd = -1;
298 msghdr.msg_accrights = (void *)&fd;
299 msghdr.msg_accrightslen = sizeof(fd);
300#else /* HAVE_MSGHDR_ACCRIGHTS */
301 struct msghdr msghdr;
302 struct cmsg_fd cmsg;
303
304 cmsg.len = sizeof(cmsg);
305 cmsg.level = SOL_SOCKET;
306 cmsg.type = SCM_RIGHTS;
307 cmsg.fd = -1;
308 msghdr.msg_control = &cmsg;
309 msghdr.msg_controllen = sizeof(cmsg);
310 msghdr.msg_flags = 0;
311#endif /* HAVE_MSGHDR_ACCRIGHTS */
312
313 msghdr.msg_name = NULL;
314 msghdr.msg_namelen = 0;
315 msghdr.msg_iov = &vec;
316 msghdr.msg_iovlen = 1;
Alexandre Julliard8859d772001-03-01 22:13:49 +0000317 vec.iov_base = (void *)handle;
318 vec.iov_len = sizeof(*handle);
Alexandre Julliardd549f692000-12-22 02:04:15 +0000319
320 for (;;)
321 {
Alexandre Julliard8859d772001-03-01 22:13:49 +0000322 if ((ret = recvmsg( fd_socket, &msghdr, 0 )) > 0)
Alexandre Julliardd549f692000-12-22 02:04:15 +0000323 {
324#ifndef HAVE_MSGHDR_ACCRIGHTS
325 fd = cmsg.fd;
326#endif
Alexandre Julliard8859d772001-03-01 22:13:49 +0000327 if (fd == -1) server_protocol_error( "no fd received for handle %d\n", *handle );
328 fcntl( fd, F_SETFD, 1 ); /* set close on exec flag */
Alexandre Julliardd549f692000-12-22 02:04:15 +0000329 return fd;
330 }
331 if (!ret) break;
332 if (errno == EINTR) continue;
333 if (errno == EPIPE) break;
Alexandre Julliard5f195f82001-02-20 23:45:07 +0000334 server_protocol_perror("recvmsg");
Alexandre Julliardd549f692000-12-22 02:04:15 +0000335 }
336 /* the server closed the connection; time to die... */
Alexandre Julliard5016e922002-01-07 18:04:07 +0000337 SYSDEPS_AbortThread(0);
Alexandre Julliardd549f692000-12-22 02:04:15 +0000338}
339
340
341/***********************************************************************
Alexandre Julliard8d1550d2002-03-23 18:48:12 +0000342 * store_cached_fd
Alexandre Julliard8859d772001-03-01 22:13:49 +0000343 *
Alexandre Julliard8d1550d2002-03-23 18:48:12 +0000344 * Store the cached fd value for a given handle back into the server.
345 * Returns the new fd, which can be different if there was already an
346 * fd in the cache for that handle.
Alexandre Julliard8859d772001-03-01 22:13:49 +0000347 */
Alexandre Julliard51885742002-05-30 20:12:58 +0000348inline static int store_cached_fd( int fd, obj_handle_t handle )
Alexandre Julliard8859d772001-03-01 22:13:49 +0000349{
Alexandre Julliard8859d772001-03-01 22:13:49 +0000350 SERVER_START_REQ( set_handle_info )
351 {
Alexandre Julliard8d1550d2002-03-23 18:48:12 +0000352 req->handle = handle;
Alexandre Julliard8859d772001-03-01 22:13:49 +0000353 req->flags = 0;
354 req->mask = 0;
355 req->fd = fd;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000356 if (!wine_server_call( req ))
Alexandre Julliard8859d772001-03-01 22:13:49 +0000357 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000358 if (reply->cur_fd != fd)
Alexandre Julliard8859d772001-03-01 22:13:49 +0000359 {
360 /* someone was here before us */
361 close( fd );
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000362 fd = reply->cur_fd;
Alexandre Julliard8859d772001-03-01 22:13:49 +0000363 }
364 }
365 else
366 {
367 close( fd );
368 fd = -1;
369 }
370 }
371 SERVER_END_REQ;
Alexandre Julliard8859d772001-03-01 22:13:49 +0000372 return fd;
373}
374
375
376/***********************************************************************
Alexandre Julliardbe367c72002-05-30 20:40:02 +0000377 * wine_server_fd_to_handle (NTDLL.@)
378 *
379 * Allocate a file handle for a Unix fd.
380 */
381int wine_server_fd_to_handle( int fd, unsigned int access, int inherit, obj_handle_t *handle )
382{
383 int ret;
384
385 *handle = 0;
386 wine_server_send_fd( fd );
387
388 SERVER_START_REQ( alloc_file_handle )
389 {
390 req->access = access;
391 req->inherit = inherit;
392 req->fd = fd;
393 if (!(ret = wine_server_call( req ))) *handle = reply->handle;
394 }
395 SERVER_END_REQ;
396 return ret;
397}
398
399
400/***********************************************************************
Alexandre Julliard8d1550d2002-03-23 18:48:12 +0000401 * wine_server_handle_to_fd (NTDLL.@)
402 *
403 * Retrieve the Unix fd corresponding to a file handle.
404 */
Alexandre Julliard51885742002-05-30 20:12:58 +0000405int wine_server_handle_to_fd( obj_handle_t handle, unsigned int access, int *unix_fd,
Alexandre Julliard8d1550d2002-03-23 18:48:12 +0000406 enum fd_type *type, int *flags )
407{
Alexandre Julliard51885742002-05-30 20:12:58 +0000408 obj_handle_t fd_handle;
Alexandre Julliard8d1550d2002-03-23 18:48:12 +0000409 int ret, fd = -1;
410
411 *unix_fd = -1;
412 for (;;)
413 {
414 SERVER_START_REQ( get_handle_fd )
415 {
416 req->handle = handle;
417 req->access = access;
418 if (!(ret = wine_server_call( req ))) fd = reply->fd;
419 if (type) *type = reply->type;
420 if (flags) *flags = reply->flags;
421 }
422 SERVER_END_REQ;
423 if (ret) return ret;
424
425 if (fd != -1) break;
426
427 /* it wasn't in the cache, get it from the server */
428 fd = receive_fd( &fd_handle );
429 /* and store it back into the cache */
430 fd = store_cached_fd( fd, fd_handle );
431
432 if (fd_handle == handle) break;
433 /* if we received a different handle this means there was
434 * a race with another thread; we restart everything from
435 * scratch in this case.
436 */
437 }
438
439 if ((fd != -1) && ((fd = dup(fd)) == -1)) return STATUS_TOO_MANY_OPENED_FILES;
440 *unix_fd = fd;
441 return STATUS_SUCCESS;
442}
443
444
445/***********************************************************************
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000446 * start_server
447 *
448 * Start a new wine server.
449 */
450static void start_server( const char *oldcwd )
451{
452 static int started; /* we only try once */
Alexandre Julliardc10c9ef2000-08-11 21:16:53 +0000453 char *path, *p;
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000454 if (!started)
455 {
Alexandre Julliardd8041112000-04-14 14:42:41 +0000456 int status;
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000457 int pid = fork();
458 if (pid == -1) fatal_perror( "fork" );
459 if (!pid)
460 {
Alexandre Julliard50097a02000-09-10 03:24:28 +0000461 /* if server is explicitly specified, use this */
462 if ((p = getenv("WINESERVER")))
463 {
Alexandre Julliard61d26d72001-05-09 19:46:39 +0000464 if (p[0] != '/' && oldcwd[0] == '/') /* make it an absolute path */
465 {
466 if (!(path = malloc( strlen(oldcwd) + strlen(p) + 1 )))
467 fatal_error( "out of memory\n" );
468 sprintf( path, "%s/%s", oldcwd, p );
469 p = path;
470 }
Alexandre Julliard4144b5b2002-06-20 23:21:27 +0000471 execl( p, p, NULL );
Alexandre Julliard50097a02000-09-10 03:24:28 +0000472 fatal_perror( "could not exec the server '%s'\n"
473 " specified in the WINESERVER environment variable", p );
474 }
475
Alexandre Julliardeafa3912000-01-25 21:19:58 +0000476 /* first try the installation dir */
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000477 execl( BINDIR "/wineserver", "wineserver", NULL );
Alexandre Julliardc10c9ef2000-08-11 21:16:53 +0000478
Alexandre Julliardeafa3912000-01-25 21:19:58 +0000479 /* now try the dir we were launched from */
Alexandre Julliardc10c9ef2000-08-11 21:16:53 +0000480 if (full_argv0)
481 {
482 if (!(path = malloc( strlen(full_argv0) + 20 )))
483 fatal_error( "out of memory\n" );
484 if ((p = strrchr( strcpy( path, full_argv0 ), '/' )))
485 {
486 strcpy( p, "/wineserver" );
Alexandre Julliard4144b5b2002-06-20 23:21:27 +0000487 execl( path, path, NULL );
Alexandre Julliardc10c9ef2000-08-11 21:16:53 +0000488 }
Alexandre Julliard35363162002-05-22 21:32:49 +0000489 free(path);
Alexandre Julliardc10c9ef2000-08-11 21:16:53 +0000490 }
491
Alexandre Julliard35363162002-05-22 21:32:49 +0000492 /* finally try the path */
Alexandre Julliardc10c9ef2000-08-11 21:16:53 +0000493 execlp( "wineserver", "wineserver", NULL );
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000494 fatal_error( "could not exec wineserver\n" );
495 }
Alexandre Julliardd8041112000-04-14 14:42:41 +0000496 waitpid( pid, &status, 0 );
497 status = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
Alexandre Julliard4144b5b2002-06-20 23:21:27 +0000498 if (status == 2) return; /* server lock held by someone else, will retry later */
Alexandre Julliardd8041112000-04-14 14:42:41 +0000499 if (status) exit(status); /* server failed */
Alexandre Julliard4144b5b2002-06-20 23:21:27 +0000500 started = 1;
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000501 }
502}
503
Alexandre Julliard4144b5b2002-06-20 23:21:27 +0000504
505/***********************************************************************
506 * server_connect_error
507 *
508 * Try to display a meaningful explanation of why we couldn't connect
509 * to the server.
510 */
511static void server_connect_error( const char *serverdir )
512{
513 int fd;
514 struct flock fl;
515
516 if ((fd = open( LOCKNAME, O_WRONLY )) == -1)
517 fatal_error( "for some mysterious reason, the wine server never started.\n" );
518
519 fl.l_type = F_WRLCK;
520 fl.l_whence = SEEK_SET;
521 fl.l_start = 0;
522 fl.l_len = 1;
523 if (fcntl( fd, F_GETLK, &fl ) != -1)
524 {
525 if (fl.l_type == F_WRLCK) /* the file is locked */
526 fatal_error( "a wine server seems to be running, but I cannot connect to it.\n"
527 " You probably need to kill that process (it might be pid %d).\n",
528 (int)fl.l_pid );
529 fatal_error( "for some mysterious reason, the wine server failed to run.\n" );
530 }
531 fatal_error( "the file system of '%s' doesn't support locks,\n"
532 " and there is a 'socket' file in that directory that prevents wine from starting.\n"
533 " You should make sure no wine server is running, remove that file and try again.\n",
534 serverdir );
535}
536
537
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000538/***********************************************************************
539 * server_connect
540 *
541 * Attempt to connect to an existing server socket.
542 * We need to be in the server directory already.
543 */
544static int server_connect( const char *oldcwd, const char *serverdir )
545{
546 struct sockaddr_un addr;
547 struct stat st;
Alexandre Julliardc10c9ef2000-08-11 21:16:53 +0000548 int s, slen, retry;
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000549
Alexandre Julliardd8041112000-04-14 14:42:41 +0000550 /* chdir to the server directory */
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000551 if (chdir( serverdir ) == -1)
552 {
553 if (errno != ENOENT) fatal_perror( "chdir to %s", serverdir );
Alexandre Julliardc10c9ef2000-08-11 21:16:53 +0000554 start_server( "." );
Alexandre Julliardd8041112000-04-14 14:42:41 +0000555 if (chdir( serverdir ) == -1) fatal_perror( "chdir to %s", serverdir );
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000556 }
Alexandre Julliardd8041112000-04-14 14:42:41 +0000557
558 /* make sure we are at the right place */
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000559 if (stat( ".", &st ) == -1) fatal_perror( "stat %s", serverdir );
560 if (st.st_uid != getuid()) fatal_error( "'%s' is not owned by you\n", serverdir );
561 if (st.st_mode & 077) fatal_error( "'%s' must not be accessible by other users\n", serverdir );
562
Alexandre Julliard4144b5b2002-06-20 23:21:27 +0000563 for (retry = 0; retry < 6; retry++)
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000564 {
Alexandre Julliard4144b5b2002-06-20 23:21:27 +0000565 /* if not the first try, wait a bit to leave the previous server time to exit */
566 if (retry)
567 {
568 usleep( 100000 * retry * retry );
569 start_server( oldcwd );
570 if (lstat( SOCKETNAME, &st ) == -1) continue; /* still no socket, wait a bit more */
571 }
572 else if (lstat( SOCKETNAME, &st ) == -1) /* check for an already existing socket */
Alexandre Julliardc10c9ef2000-08-11 21:16:53 +0000573 {
574 if (errno != ENOENT) fatal_perror( "lstat %s/%s", serverdir, SOCKETNAME );
575 start_server( oldcwd );
Alexandre Julliard4144b5b2002-06-20 23:21:27 +0000576 if (lstat( SOCKETNAME, &st ) == -1) continue; /* still no socket, wait a bit more */
Alexandre Julliardc10c9ef2000-08-11 21:16:53 +0000577 }
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000578
Roberto Augusto Pungartnik19f8dda2000-09-22 22:19:58 +0000579 /* make sure the socket is sane (ISFIFO needed for Solaris) */
580 if (!S_ISSOCK(st.st_mode) && !S_ISFIFO(st.st_mode))
Alexandre Julliardc10c9ef2000-08-11 21:16:53 +0000581 fatal_error( "'%s/%s' is not a socket\n", serverdir, SOCKETNAME );
582 if (st.st_uid != getuid())
583 fatal_error( "'%s/%s' is not owned by you\n", serverdir, SOCKETNAME );
584
585 /* try to connect to it */
586 addr.sun_family = AF_UNIX;
587 strcpy( addr.sun_path, SOCKETNAME );
588 slen = sizeof(addr) - sizeof(addr.sun_path) + strlen(addr.sun_path) + 1;
Juergen Lock2d33ab92000-02-13 16:03:29 +0000589#ifdef HAVE_SOCKADDR_SUN_LEN
Alexandre Julliardc10c9ef2000-08-11 21:16:53 +0000590 addr.sun_len = slen;
Juergen Lock2d33ab92000-02-13 16:03:29 +0000591#endif
Alexandre Julliard42cc2bd2000-06-07 03:49:41 +0000592 if ((s = socket( AF_UNIX, SOCK_STREAM, 0 )) == -1) fatal_perror( "socket" );
Alexandre Julliardc10c9ef2000-08-11 21:16:53 +0000593 if (connect( s, (struct sockaddr *)&addr, slen ) != -1)
594 {
595 fcntl( s, F_SETFD, 1 ); /* set close on exec flag */
596 return s;
597 }
598 close( s );
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000599 }
Alexandre Julliard4144b5b2002-06-20 23:21:27 +0000600 server_connect_error( serverdir );
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000601}
602
603
604/***********************************************************************
Alexandre Julliardf0167521999-03-21 19:26:25 +0000605 * CLIENT_InitServer
Alexandre Julliard642d3131998-07-12 19:29:36 +0000606 *
Alexandre Julliardf0167521999-03-21 19:26:25 +0000607 * Start the server and create the initial socket pair.
Alexandre Julliard642d3131998-07-12 19:29:36 +0000608 */
Alexandre Julliard8859d772001-03-01 22:13:49 +0000609void CLIENT_InitServer(void)
Alexandre Julliard642d3131998-07-12 19:29:36 +0000610{
Alexandre Julliard8859d772001-03-01 22:13:49 +0000611 int size;
Alexandre Julliard4144b5b2002-06-20 23:21:27 +0000612 char *oldcwd;
Alexandre Julliard51885742002-05-30 20:12:58 +0000613 obj_handle_t dummy_handle;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000614
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000615 /* retrieve the current directory */
616 for (size = 512; ; size *= 2)
Alexandre Julliardf0167521999-03-21 19:26:25 +0000617 {
Dimitrie O. Paun9ad96362000-03-19 14:29:50 +0000618 if (!(oldcwd = malloc( size ))) break;
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000619 if (getcwd( oldcwd, size )) break;
620 free( oldcwd );
621 if (errno == ERANGE) continue;
622 oldcwd = NULL;
Ulrich Weigand371fd751999-04-11 17:13:03 +0000623 break;
Alexandre Julliardf0167521999-03-21 19:26:25 +0000624 }
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000625
Alexandre Julliarda3e0cfc2000-07-16 18:21:34 +0000626 /* if argv[0] is a relative path, make it absolute */
627 full_argv0 = argv0;
628 if (oldcwd && argv0[0] != '/' && strchr( argv0, '/' ))
629 {
630 char *new_argv0 = malloc( strlen(oldcwd) + strlen(argv0) + 2 );
631 if (new_argv0)
632 {
633 strcpy( new_argv0, oldcwd );
634 strcat( new_argv0, "/" );
635 strcat( new_argv0, argv0 );
636 full_argv0 = new_argv0;
637 }
638 }
639
Alexandre Julliardd8041112000-04-14 14:42:41 +0000640 /* connect to the server */
Alexandre Julliard4144b5b2002-06-20 23:21:27 +0000641 fd_socket = server_connect( oldcwd, wine_get_server_dir() );
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000642
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000643 /* switch back to the starting directory */
644 if (oldcwd)
645 {
646 chdir( oldcwd );
647 free( oldcwd );
648 }
Alexandre Julliardf5242402001-02-28 21:45:23 +0000649
650 /* setup the signal mask */
651 sigemptyset( &block_set );
652 sigaddset( &block_set, SIGALRM );
653 sigaddset( &block_set, SIGIO );
654 sigaddset( &block_set, SIGINT );
655 sigaddset( &block_set, SIGHUP );
Alexandre Julliardd04ccb82003-03-04 22:18:43 +0000656 sigaddset( &block_set, SIGUSR1 );
657 sigaddset( &block_set, SIGUSR2 );
Alexandre Julliardf5242402001-02-28 21:45:23 +0000658
Alexandre Julliard8859d772001-03-01 22:13:49 +0000659 /* receive the first thread request fd on the main socket */
660 NtCurrentTeb()->request_fd = receive_fd( &dummy_handle );
661
662 CLIENT_InitThread();
663}
664
665
666/***********************************************************************
Alexandre Julliard642d3131998-07-12 19:29:36 +0000667 * CLIENT_InitThread
668 *
669 * Send an init thread request. Return 0 if OK.
670 */
Alexandre Julliard8859d772001-03-01 22:13:49 +0000671void CLIENT_InitThread(void)
Alexandre Julliard642d3131998-07-12 19:29:36 +0000672{
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000673 TEB *teb = NtCurrentTeb();
Alexandre Julliard8859d772001-03-01 22:13:49 +0000674 int version, ret;
Alexandre Julliarde5dedb12001-03-08 01:16:41 +0000675 int reply_pipe[2];
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000676
Alexandre Julliard42cc2bd2000-06-07 03:49:41 +0000677 /* ignore SIGPIPE so that we get a EPIPE error instead */
678 signal( SIGPIPE, SIG_IGN );
François Gouget103295c2002-06-13 21:57:38 +0000679 /* automatic child reaping to avoid zombies */
680 signal( SIGCHLD, SIG_IGN );
Alexandre Julliard42cc2bd2000-06-07 03:49:41 +0000681
Alexandre Julliard8859d772001-03-01 22:13:49 +0000682 /* create the server->client communication pipes */
683 if (pipe( reply_pipe ) == -1) server_protocol_perror( "pipe" );
Alexandre Julliarde5dedb12001-03-08 01:16:41 +0000684 if (pipe( teb->wait_fd ) == -1) server_protocol_perror( "pipe" );
Alexandre Julliard8859d772001-03-01 22:13:49 +0000685 wine_server_send_fd( reply_pipe[1] );
Alexandre Julliarde5dedb12001-03-08 01:16:41 +0000686 wine_server_send_fd( teb->wait_fd[1] );
Alexandre Julliard8859d772001-03-01 22:13:49 +0000687 teb->reply_fd = reply_pipe[0];
Alexandre Julliard714156d2002-08-13 18:24:27 +0000688 close( reply_pipe[1] );
Alexandre Julliardd549f692000-12-22 02:04:15 +0000689
Alexandre Julliard8859d772001-03-01 22:13:49 +0000690 /* set close on exec flag */
691 fcntl( teb->reply_fd, F_SETFD, 1 );
Alexandre Julliarde5dedb12001-03-08 01:16:41 +0000692 fcntl( teb->wait_fd[0], F_SETFD, 1 );
693 fcntl( teb->wait_fd[1], F_SETFD, 1 );
Alexandre Julliardd549f692000-12-22 02:04:15 +0000694
Alexandre Julliard8859d772001-03-01 22:13:49 +0000695 SERVER_START_REQ( init_thread )
696 {
697 req->unix_pid = getpid();
698 req->teb = teb;
699 req->entry = teb->entry_point;
700 req->reply_fd = reply_pipe[1];
Alexandre Julliarde5dedb12001-03-08 01:16:41 +0000701 req->wait_fd = teb->wait_fd[1];
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000702 ret = wine_server_call( req );
703 teb->pid = reply->pid;
704 teb->tid = reply->tid;
705 version = reply->version;
706 if (reply->boot) boot_thread_id = teb->tid;
Alexandre Julliard8859d772001-03-01 22:13:49 +0000707 else if (boot_thread_id == teb->tid) boot_thread_id = 0;
Alexandre Julliard8859d772001-03-01 22:13:49 +0000708 }
709 SERVER_END_REQ;
Alexandre Julliardd90e9642001-02-21 04:21:50 +0000710
Alexandre Julliard8859d772001-03-01 22:13:49 +0000711 if (ret) server_protocol_error( "init_thread failed with status %x\n", ret );
712 if (version != SERVER_PROTOCOL_VERSION)
Alexandre Julliard5fb54562000-03-08 22:01:02 +0000713 server_protocol_error( "version mismatch %d/%d.\n"
714 "Your %s binary was not upgraded correctly,\n"
Alexandre Julliardd90e9642001-02-21 04:21:50 +0000715 "or you have an older one somewhere in your PATH.\n"
716 "Or maybe the wrong wineserver is still running?\n",
Alexandre Julliard8859d772001-03-01 22:13:49 +0000717 version, SERVER_PROTOCOL_VERSION,
718 (version > SERVER_PROTOCOL_VERSION) ? "wine" : "wineserver" );
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000719}
720
Alexandre Julliardd549f692000-12-22 02:04:15 +0000721
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000722/***********************************************************************
723 * CLIENT_BootDone
724 *
725 * Signal that we have finished booting, and set debug level.
726 */
Alexandre Julliard8859d772001-03-01 22:13:49 +0000727void CLIENT_BootDone( int debug_level )
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000728{
Alexandre Julliard67a74992001-02-27 02:09:16 +0000729 SERVER_START_REQ( boot_done )
Alexandre Julliard86113532000-08-29 03:54:30 +0000730 {
Alexandre Julliard86113532000-08-29 03:54:30 +0000731 req->debug_level = debug_level;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000732 wine_server_call( req );
Alexandre Julliard86113532000-08-29 03:54:30 +0000733 }
734 SERVER_END_REQ;
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000735}
736
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000737
738/***********************************************************************
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000739 * CLIENT_IsBootThread
Alexandre Julliard338e7571998-12-27 15:28:54 +0000740 *
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000741 * Return TRUE if current thread is the boot thread.
Alexandre Julliard338e7571998-12-27 15:28:54 +0000742 */
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000743int CLIENT_IsBootThread(void)
Alexandre Julliard338e7571998-12-27 15:28:54 +0000744{
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000745 return (GetCurrentThreadId() == (DWORD)boot_thread_id);
Alexandre Julliard338e7571998-12-27 15:28:54 +0000746}