inetcomm: Use BOOL type where appropriate.
[wine.git] / dlls / ntdll / server.c
blob9b3aeef14cc3ed946b716354062445a10f3b3913
1 /*
2 * Wine server communication
4 * Copyright (C) 1998 Alexandre Julliard
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.
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.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <ctype.h>
26 #ifdef HAVE_DIRENT_H
27 # include <dirent.h>
28 #endif
29 #include <errno.h>
30 #include <fcntl.h>
31 #ifdef HAVE_LWP_H
32 #include <lwp.h>
33 #endif
34 #ifdef HAVE_PTHREAD_NP_H
35 # include <pthread_np.h>
36 #endif
37 #include <signal.h>
38 #include <stdarg.h>
39 #include <stdio.h>
40 #include <string.h>
41 #include <sys/types.h>
42 #ifdef HAVE_SYS_SOCKET_H
43 # include <sys/socket.h>
44 #endif
45 #ifdef HAVE_SYS_WAIT_H
46 #include <sys/wait.h>
47 #endif
48 #ifdef HAVE_SYS_UN_H
49 #include <sys/un.h>
50 #endif
51 #ifdef HAVE_SYS_MMAN_H
52 #include <sys/mman.h>
53 #endif
54 #ifdef HAVE_SYS_PRCTL_H
55 # include <sys/prctl.h>
56 #endif
57 #ifdef HAVE_SYS_STAT_H
58 # include <sys/stat.h>
59 #endif
60 #ifdef HAVE_SYS_SYSCALL_H
61 # include <sys/syscall.h>
62 #endif
63 #ifdef HAVE_SYS_UIO_H
64 #include <sys/uio.h>
65 #endif
66 #ifdef HAVE_SYS_THR_H
67 #include <sys/ucontext.h>
68 #include <sys/thr.h>
69 #endif
70 #ifdef HAVE_UNISTD_H
71 # include <unistd.h>
72 #endif
74 #include "ntstatus.h"
75 #define WIN32_NO_STATUS
76 #include "wine/library.h"
77 #include "wine/server.h"
78 #include "wine/debug.h"
79 #include "ntdll_misc.h"
81 WINE_DEFAULT_DEBUG_CHANNEL(server);
83 /* Some versions of glibc don't define this */
84 #ifndef SCM_RIGHTS
85 #define SCM_RIGHTS 1
86 #endif
88 #ifndef MSG_CMSG_CLOEXEC
89 #define MSG_CMSG_CLOEXEC 0
90 #endif
92 #define SOCKETNAME "socket" /* name of the socket file */
93 #define LOCKNAME "lock" /* name of the lock file */
95 #ifdef __i386__
96 static const enum cpu_type client_cpu = CPU_x86;
97 #elif defined(__x86_64__)
98 static const enum cpu_type client_cpu = CPU_x86_64;
99 #elif defined(__powerpc__)
100 static const enum cpu_type client_cpu = CPU_POWERPC;
101 #elif defined(__arm__)
102 static const enum cpu_type client_cpu = CPU_ARM;
103 #elif defined(__aarch64__)
104 static const enum cpu_type client_cpu = CPU_ARM64;
105 #else
106 #error Unsupported CPU
107 #endif
109 unsigned int server_cpus = 0;
110 int is_wow64 = FALSE;
112 timeout_t server_start_time = 0; /* time of server startup */
114 sigset_t server_block_set; /* signals to block during server calls */
115 static int fd_socket = -1; /* socket to exchange file descriptors with the server */
116 static pid_t server_pid;
118 static RTL_CRITICAL_SECTION fd_cache_section;
119 static RTL_CRITICAL_SECTION_DEBUG critsect_debug =
121 0, 0, &fd_cache_section,
122 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
123 0, 0, { (DWORD_PTR)(__FILE__ ": fd_cache_section") }
125 static RTL_CRITICAL_SECTION fd_cache_section = { &critsect_debug, -1, 0, 0, 0, 0 };
128 #ifdef __GNUC__
129 static void fatal_error( const char *err, ... ) __attribute__((noreturn, format(printf,1,2)));
130 static void fatal_perror( const char *err, ... ) __attribute__((noreturn, format(printf,1,2)));
131 static void server_connect_error( const char *serverdir ) __attribute__((noreturn));
132 #endif
134 /* die on a fatal error; use only during initialization */
135 static void fatal_error( const char *err, ... )
137 va_list args;
139 va_start( args, err );
140 fprintf( stderr, "wine: " );
141 vfprintf( stderr, err, args );
142 va_end( args );
143 exit(1);
146 /* die on a fatal error; use only during initialization */
147 static void fatal_perror( const char *err, ... )
149 va_list args;
151 va_start( args, err );
152 fprintf( stderr, "wine: " );
153 vfprintf( stderr, err, args );
154 perror( " " );
155 va_end( args );
156 exit(1);
160 /***********************************************************************
161 * server_protocol_error
163 static DECLSPEC_NORETURN void server_protocol_error( const char *err, ... )
165 va_list args;
167 va_start( args, err );
168 fprintf( stderr, "wine client error:%x: ", GetCurrentThreadId() );
169 vfprintf( stderr, err, args );
170 va_end( args );
171 abort_thread(1);
175 /***********************************************************************
176 * server_protocol_perror
178 static DECLSPEC_NORETURN void server_protocol_perror( const char *err )
180 fprintf( stderr, "wine client error:%x: ", GetCurrentThreadId() );
181 perror( err );
182 abort_thread(1);
186 /***********************************************************************
187 * send_request
189 * Send a request to the server.
191 static unsigned int send_request( const struct __server_request_info *req )
193 unsigned int i;
194 int ret;
196 if (!req->u.req.request_header.request_size)
198 if ((ret = write( ntdll_get_thread_data()->request_fd, &req->u.req,
199 sizeof(req->u.req) )) == sizeof(req->u.req)) return STATUS_SUCCESS;
202 else
204 struct iovec vec[__SERVER_MAX_DATA+1];
206 vec[0].iov_base = (void *)&req->u.req;
207 vec[0].iov_len = sizeof(req->u.req);
208 for (i = 0; i < req->data_count; i++)
210 vec[i+1].iov_base = (void *)req->data[i].ptr;
211 vec[i+1].iov_len = req->data[i].size;
213 if ((ret = writev( ntdll_get_thread_data()->request_fd, vec, i+1 )) ==
214 req->u.req.request_header.request_size + sizeof(req->u.req)) return STATUS_SUCCESS;
217 if (ret >= 0) server_protocol_error( "partial write %d\n", ret );
218 if (errno == EPIPE) abort_thread(0);
219 if (errno == EFAULT) return STATUS_ACCESS_VIOLATION;
220 server_protocol_perror( "write" );
224 /***********************************************************************
225 * read_reply_data
227 * Read data from the reply buffer; helper for wait_reply.
229 static void read_reply_data( void *buffer, size_t size )
231 int ret;
233 for (;;)
235 if ((ret = read( ntdll_get_thread_data()->reply_fd, buffer, size )) > 0)
237 if (!(size -= ret)) return;
238 buffer = (char *)buffer + ret;
239 continue;
241 if (!ret) break;
242 if (errno == EINTR) continue;
243 if (errno == EPIPE) break;
244 server_protocol_perror("read");
246 /* the server closed the connection; time to die... */
247 abort_thread(0);
251 /***********************************************************************
252 * wait_reply
254 * Wait for a reply from the server.
256 static inline unsigned int wait_reply( struct __server_request_info *req )
258 read_reply_data( &req->u.reply, sizeof(req->u.reply) );
259 if (req->u.reply.reply_header.reply_size)
260 read_reply_data( req->reply_data, req->u.reply.reply_header.reply_size );
261 return req->u.reply.reply_header.error;
265 /***********************************************************************
266 * wine_server_call (NTDLL.@)
268 * Perform a server call.
270 * PARAMS
271 * req_ptr [I/O] Function dependent data
273 * RETURNS
274 * Depends on server function being called, but usually an NTSTATUS code.
276 * NOTES
277 * Use the SERVER_START_REQ and SERVER_END_REQ to help you fill out the
278 * server request structure for the particular call. E.g:
279 *| SERVER_START_REQ( event_op )
280 *| {
281 *| req->handle = handle;
282 *| req->op = SET_EVENT;
283 *| ret = wine_server_call( req );
284 *| }
285 *| SERVER_END_REQ;
287 unsigned int wine_server_call( void *req_ptr )
289 struct __server_request_info * const req = req_ptr;
290 sigset_t old_set;
291 unsigned int ret;
293 pthread_sigmask( SIG_BLOCK, &server_block_set, &old_set );
294 ret = send_request( req );
295 if (!ret) ret = wait_reply( req );
296 pthread_sigmask( SIG_SETMASK, &old_set, NULL );
297 return ret;
301 /***********************************************************************
302 * server_enter_uninterrupted_section
304 void server_enter_uninterrupted_section( RTL_CRITICAL_SECTION *cs, sigset_t *sigset )
306 pthread_sigmask( SIG_BLOCK, &server_block_set, sigset );
307 RtlEnterCriticalSection( cs );
311 /***********************************************************************
312 * server_leave_uninterrupted_section
314 void server_leave_uninterrupted_section( RTL_CRITICAL_SECTION *cs, sigset_t *sigset )
316 RtlLeaveCriticalSection( cs );
317 pthread_sigmask( SIG_SETMASK, sigset, NULL );
321 /***********************************************************************
322 * wait_select_reply
324 * Wait for a reply on the waiting pipe of the current thread.
326 static int wait_select_reply( void *cookie )
328 int signaled;
329 struct wake_up_reply reply;
330 for (;;)
332 int ret;
333 ret = read( ntdll_get_thread_data()->wait_fd[0], &reply, sizeof(reply) );
334 if (ret == sizeof(reply))
336 if (!reply.cookie) abort_thread( reply.signaled ); /* thread got killed */
337 if (wine_server_get_ptr(reply.cookie) == cookie) return reply.signaled;
338 /* we stole another reply, wait for the real one */
339 signaled = wait_select_reply( cookie );
340 /* and now put the wrong one back in the pipe */
341 for (;;)
343 ret = write( ntdll_get_thread_data()->wait_fd[1], &reply, sizeof(reply) );
344 if (ret == sizeof(reply)) break;
345 if (ret >= 0) server_protocol_error( "partial wakeup write %d\n", ret );
346 if (errno == EINTR) continue;
347 server_protocol_perror("wakeup write");
349 return signaled;
351 if (ret >= 0) server_protocol_error( "partial wakeup read %d\n", ret );
352 if (errno == EINTR) continue;
353 server_protocol_perror("wakeup read");
358 /***********************************************************************
359 * invoke_apc
361 * Invoke a single APC. Return TRUE if a user APC has been run.
363 static BOOL invoke_apc( const apc_call_t *call, apc_result_t *result )
365 BOOL user_apc = FALSE;
366 SIZE_T size;
367 void *addr;
369 memset( result, 0, sizeof(*result) );
371 switch (call->type)
373 case APC_NONE:
374 break;
375 case APC_USER:
377 void (WINAPI *func)(ULONG_PTR,ULONG_PTR,ULONG_PTR) = wine_server_get_ptr( call->user.func );
378 func( call->user.args[0], call->user.args[1], call->user.args[2] );
379 user_apc = TRUE;
380 break;
382 case APC_TIMER:
384 void (WINAPI *func)(void*, unsigned int, unsigned int) = wine_server_get_ptr( call->timer.func );
385 func( wine_server_get_ptr( call->timer.arg ),
386 (DWORD)call->timer.time, (DWORD)(call->timer.time >> 32) );
387 user_apc = TRUE;
388 break;
390 case APC_ASYNC_IO:
392 void *apc = NULL;
393 IO_STATUS_BLOCK *iosb = wine_server_get_ptr( call->async_io.sb );
394 NTSTATUS (*func)(void *, IO_STATUS_BLOCK *, NTSTATUS, void **) = wine_server_get_ptr( call->async_io.func );
395 result->type = call->type;
396 result->async_io.status = func( wine_server_get_ptr( call->async_io.user ),
397 iosb, call->async_io.status, &apc );
398 if (result->async_io.status != STATUS_PENDING)
400 result->async_io.total = iosb->Information;
401 result->async_io.apc = wine_server_client_ptr( apc );
403 break;
405 case APC_VIRTUAL_ALLOC:
406 result->type = call->type;
407 addr = wine_server_get_ptr( call->virtual_alloc.addr );
408 size = call->virtual_alloc.size;
409 if ((ULONG_PTR)addr == call->virtual_alloc.addr && size == call->virtual_alloc.size)
411 result->virtual_alloc.status = NtAllocateVirtualMemory( NtCurrentProcess(), &addr,
412 call->virtual_alloc.zero_bits, &size,
413 call->virtual_alloc.op_type,
414 call->virtual_alloc.prot );
415 result->virtual_alloc.addr = wine_server_client_ptr( addr );
416 result->virtual_alloc.size = size;
418 else result->virtual_alloc.status = STATUS_WORKING_SET_LIMIT_RANGE;
419 break;
420 case APC_VIRTUAL_FREE:
421 result->type = call->type;
422 addr = wine_server_get_ptr( call->virtual_free.addr );
423 size = call->virtual_free.size;
424 if ((ULONG_PTR)addr == call->virtual_free.addr && size == call->virtual_free.size)
426 result->virtual_free.status = NtFreeVirtualMemory( NtCurrentProcess(), &addr, &size,
427 call->virtual_free.op_type );
428 result->virtual_free.addr = wine_server_client_ptr( addr );
429 result->virtual_free.size = size;
431 else result->virtual_free.status = STATUS_INVALID_PARAMETER;
432 break;
433 case APC_VIRTUAL_QUERY:
435 MEMORY_BASIC_INFORMATION info;
436 result->type = call->type;
437 addr = wine_server_get_ptr( call->virtual_query.addr );
438 if ((ULONG_PTR)addr == call->virtual_query.addr)
439 result->virtual_query.status = NtQueryVirtualMemory( NtCurrentProcess(),
440 addr, MemoryBasicInformation, &info,
441 sizeof(info), NULL );
442 else
443 result->virtual_query.status = STATUS_WORKING_SET_LIMIT_RANGE;
445 if (result->virtual_query.status == STATUS_SUCCESS)
447 result->virtual_query.base = wine_server_client_ptr( info.BaseAddress );
448 result->virtual_query.alloc_base = wine_server_client_ptr( info.AllocationBase );
449 result->virtual_query.size = info.RegionSize;
450 result->virtual_query.prot = info.Protect;
451 result->virtual_query.alloc_prot = info.AllocationProtect;
452 result->virtual_query.state = info.State >> 12;
453 result->virtual_query.alloc_type = info.Type >> 16;
455 break;
457 case APC_VIRTUAL_PROTECT:
458 result->type = call->type;
459 addr = wine_server_get_ptr( call->virtual_protect.addr );
460 size = call->virtual_protect.size;
461 if ((ULONG_PTR)addr == call->virtual_protect.addr && size == call->virtual_protect.size)
463 result->virtual_protect.status = NtProtectVirtualMemory( NtCurrentProcess(), &addr, &size,
464 call->virtual_protect.prot,
465 &result->virtual_protect.prot );
466 result->virtual_protect.addr = wine_server_client_ptr( addr );
467 result->virtual_protect.size = size;
469 else result->virtual_protect.status = STATUS_INVALID_PARAMETER;
470 break;
471 case APC_VIRTUAL_FLUSH:
472 result->type = call->type;
473 addr = wine_server_get_ptr( call->virtual_flush.addr );
474 size = call->virtual_flush.size;
475 if ((ULONG_PTR)addr == call->virtual_flush.addr && size == call->virtual_flush.size)
477 result->virtual_flush.status = NtFlushVirtualMemory( NtCurrentProcess(),
478 (const void **)&addr, &size, 0 );
479 result->virtual_flush.addr = wine_server_client_ptr( addr );
480 result->virtual_flush.size = size;
482 else result->virtual_flush.status = STATUS_INVALID_PARAMETER;
483 break;
484 case APC_VIRTUAL_LOCK:
485 result->type = call->type;
486 addr = wine_server_get_ptr( call->virtual_lock.addr );
487 size = call->virtual_lock.size;
488 if ((ULONG_PTR)addr == call->virtual_lock.addr && size == call->virtual_lock.size)
490 result->virtual_lock.status = NtLockVirtualMemory( NtCurrentProcess(), &addr, &size, 0 );
491 result->virtual_lock.addr = wine_server_client_ptr( addr );
492 result->virtual_lock.size = size;
494 else result->virtual_lock.status = STATUS_INVALID_PARAMETER;
495 break;
496 case APC_VIRTUAL_UNLOCK:
497 result->type = call->type;
498 addr = wine_server_get_ptr( call->virtual_unlock.addr );
499 size = call->virtual_unlock.size;
500 if ((ULONG_PTR)addr == call->virtual_unlock.addr && size == call->virtual_unlock.size)
502 result->virtual_unlock.status = NtUnlockVirtualMemory( NtCurrentProcess(), &addr, &size, 0 );
503 result->virtual_unlock.addr = wine_server_client_ptr( addr );
504 result->virtual_unlock.size = size;
506 else result->virtual_unlock.status = STATUS_INVALID_PARAMETER;
507 break;
508 case APC_MAP_VIEW:
509 result->type = call->type;
510 addr = wine_server_get_ptr( call->map_view.addr );
511 size = call->map_view.size;
512 if ((ULONG_PTR)addr == call->map_view.addr && size == call->map_view.size)
514 LARGE_INTEGER offset;
515 offset.QuadPart = call->map_view.offset;
516 result->map_view.status = NtMapViewOfSection( wine_server_ptr_handle(call->map_view.handle),
517 NtCurrentProcess(), &addr,
518 call->map_view.zero_bits, 0,
519 &offset, &size, ViewShare,
520 call->map_view.alloc_type, call->map_view.prot );
521 result->map_view.addr = wine_server_client_ptr( addr );
522 result->map_view.size = size;
524 else result->map_view.status = STATUS_INVALID_PARAMETER;
525 NtClose( wine_server_ptr_handle(call->map_view.handle) );
526 break;
527 case APC_UNMAP_VIEW:
528 result->type = call->type;
529 addr = wine_server_get_ptr( call->unmap_view.addr );
530 if ((ULONG_PTR)addr == call->unmap_view.addr)
531 result->unmap_view.status = NtUnmapViewOfSection( NtCurrentProcess(), addr );
532 else
533 result->unmap_view.status = STATUS_INVALID_PARAMETER;
534 break;
535 case APC_CREATE_THREAD:
537 CLIENT_ID id;
538 HANDLE handle;
539 SIZE_T reserve = call->create_thread.reserve;
540 SIZE_T commit = call->create_thread.commit;
541 void *func = wine_server_get_ptr( call->create_thread.func );
542 void *arg = wine_server_get_ptr( call->create_thread.arg );
544 result->type = call->type;
545 if (reserve == call->create_thread.reserve && commit == call->create_thread.commit &&
546 (ULONG_PTR)func == call->create_thread.func && (ULONG_PTR)arg == call->create_thread.arg)
548 result->create_thread.status = RtlCreateUserThread( NtCurrentProcess(), NULL,
549 call->create_thread.suspend, NULL,
550 reserve, commit, func, arg, &handle, &id );
551 result->create_thread.handle = wine_server_obj_handle( handle );
552 result->create_thread.tid = HandleToULong(id.UniqueThread);
554 else result->create_thread.status = STATUS_INVALID_PARAMETER;
555 break;
557 default:
558 server_protocol_error( "get_apc_request: bad type %d\n", call->type );
559 break;
561 return user_apc;
565 /***********************************************************************
566 * server_select
568 unsigned int server_select( const select_op_t *select_op, data_size_t size, UINT flags,
569 const LARGE_INTEGER *timeout )
571 unsigned int ret;
572 int cookie;
573 BOOL user_apc = FALSE;
574 obj_handle_t apc_handle = 0;
575 apc_call_t call;
576 apc_result_t result;
577 timeout_t abs_timeout = timeout ? timeout->QuadPart : TIMEOUT_INFINITE;
579 memset( &result, 0, sizeof(result) );
581 for (;;)
583 SERVER_START_REQ( select )
585 req->flags = flags;
586 req->cookie = wine_server_client_ptr( &cookie );
587 req->prev_apc = apc_handle;
588 req->timeout = abs_timeout;
589 wine_server_add_data( req, &result, sizeof(result) );
590 wine_server_add_data( req, select_op, size );
591 ret = wine_server_call( req );
592 abs_timeout = reply->timeout;
593 apc_handle = reply->apc_handle;
594 call = reply->call;
596 SERVER_END_REQ;
597 if (ret == STATUS_PENDING) ret = wait_select_reply( &cookie );
598 if (ret != STATUS_USER_APC) break;
599 if (invoke_apc( &call, &result ))
601 /* if we ran a user apc we have to check once more if an object got signaled,
602 * but we don't want to wait */
603 abs_timeout = 0;
604 user_apc = TRUE;
607 /* don't signal multiple times */
608 if (size >= sizeof(select_op->signal_and_wait) && select_op->op == SELECT_SIGNAL_AND_WAIT)
609 size = offsetof( select_op_t, signal_and_wait.signal );
612 if (ret == STATUS_TIMEOUT && user_apc) ret = STATUS_USER_APC;
614 /* A test on Windows 2000 shows that Windows always yields during
615 a wait, but a wait that is hit by an event gets a priority
616 boost as well. This seems to model that behavior the closest. */
617 if (ret == STATUS_TIMEOUT) NtYieldExecution();
619 return ret;
623 /***********************************************************************
624 * server_queue_process_apc
626 unsigned int server_queue_process_apc( HANDLE process, const apc_call_t *call, apc_result_t *result )
628 for (;;)
630 unsigned int ret;
631 HANDLE handle = 0;
632 BOOL self = FALSE;
634 SERVER_START_REQ( queue_apc )
636 req->handle = wine_server_obj_handle( process );
637 req->call = *call;
638 if (!(ret = wine_server_call( req )))
640 handle = wine_server_ptr_handle( reply->handle );
641 self = reply->self;
644 SERVER_END_REQ;
645 if (ret != STATUS_SUCCESS) return ret;
647 if (self)
649 invoke_apc( call, result );
651 else
653 NtWaitForSingleObject( handle, FALSE, NULL );
655 SERVER_START_REQ( get_apc_result )
657 req->handle = wine_server_obj_handle( handle );
658 if (!(ret = wine_server_call( req ))) *result = reply->result;
660 SERVER_END_REQ;
662 if (!ret && result->type == APC_NONE) continue; /* APC didn't run, try again */
663 if (ret) NtClose( handle );
665 return ret;
670 /***********************************************************************
671 * wine_server_send_fd (NTDLL.@)
673 * Send a file descriptor to the server.
675 * PARAMS
676 * fd [I] file descriptor to send
678 * RETURNS
679 * nothing
681 void CDECL wine_server_send_fd( int fd )
683 struct send_fd data;
684 struct msghdr msghdr;
685 struct iovec vec;
686 int ret;
688 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
689 msghdr.msg_accrights = (void *)&fd;
690 msghdr.msg_accrightslen = sizeof(fd);
691 #else /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
692 char cmsg_buffer[256];
693 struct cmsghdr *cmsg;
694 msghdr.msg_control = cmsg_buffer;
695 msghdr.msg_controllen = sizeof(cmsg_buffer);
696 msghdr.msg_flags = 0;
697 cmsg = CMSG_FIRSTHDR( &msghdr );
698 cmsg->cmsg_len = CMSG_LEN( sizeof(fd) );
699 cmsg->cmsg_level = SOL_SOCKET;
700 cmsg->cmsg_type = SCM_RIGHTS;
701 *(int *)CMSG_DATA(cmsg) = fd;
702 msghdr.msg_controllen = cmsg->cmsg_len;
703 #endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
705 msghdr.msg_name = NULL;
706 msghdr.msg_namelen = 0;
707 msghdr.msg_iov = &vec;
708 msghdr.msg_iovlen = 1;
710 vec.iov_base = (void *)&data;
711 vec.iov_len = sizeof(data);
713 data.tid = GetCurrentThreadId();
714 data.fd = fd;
716 for (;;)
718 if ((ret = sendmsg( fd_socket, &msghdr, 0 )) == sizeof(data)) return;
719 if (ret >= 0) server_protocol_error( "partial write %d\n", ret );
720 if (errno == EINTR) continue;
721 if (errno == EPIPE) abort_thread(0);
722 server_protocol_perror( "sendmsg" );
727 /***********************************************************************
728 * receive_fd
730 * Receive a file descriptor passed from the server.
732 static int receive_fd( obj_handle_t *handle )
734 struct iovec vec;
735 struct msghdr msghdr;
736 int ret, fd = -1;
738 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
739 msghdr.msg_accrights = (void *)&fd;
740 msghdr.msg_accrightslen = sizeof(fd);
741 #else /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
742 char cmsg_buffer[256];
743 msghdr.msg_control = cmsg_buffer;
744 msghdr.msg_controllen = sizeof(cmsg_buffer);
745 msghdr.msg_flags = 0;
746 #endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
748 msghdr.msg_name = NULL;
749 msghdr.msg_namelen = 0;
750 msghdr.msg_iov = &vec;
751 msghdr.msg_iovlen = 1;
752 vec.iov_base = (void *)handle;
753 vec.iov_len = sizeof(*handle);
755 for (;;)
757 if ((ret = recvmsg( fd_socket, &msghdr, MSG_CMSG_CLOEXEC )) > 0)
759 #ifndef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
760 struct cmsghdr *cmsg;
761 for (cmsg = CMSG_FIRSTHDR( &msghdr ); cmsg; cmsg = CMSG_NXTHDR( &msghdr, cmsg ))
763 if (cmsg->cmsg_level != SOL_SOCKET) continue;
764 if (cmsg->cmsg_type == SCM_RIGHTS) fd = *(int *)CMSG_DATA(cmsg);
765 #ifdef SCM_CREDENTIALS
766 else if (cmsg->cmsg_type == SCM_CREDENTIALS)
768 struct ucred *ucred = (struct ucred *)CMSG_DATA(cmsg);
769 server_pid = ucred->pid;
771 #endif
773 #endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
774 if (fd != -1) fcntl( fd, F_SETFD, FD_CLOEXEC ); /* in case MSG_CMSG_CLOEXEC is not supported */
775 return fd;
777 if (!ret) break;
778 if (errno == EINTR) continue;
779 if (errno == EPIPE) break;
780 server_protocol_perror("recvmsg");
782 /* the server closed the connection; time to die... */
783 abort_thread(0);
787 /***********************************************************************/
788 /* fd cache support */
790 struct fd_cache_entry
792 int fd;
793 enum server_fd_type type : 5;
794 unsigned int access : 3;
795 unsigned int options : 24;
798 #define FD_CACHE_BLOCK_SIZE (65536 / sizeof(struct fd_cache_entry))
799 #define FD_CACHE_ENTRIES 128
801 static struct fd_cache_entry *fd_cache[FD_CACHE_ENTRIES];
802 static struct fd_cache_entry fd_cache_initial_block[FD_CACHE_BLOCK_SIZE];
804 static inline unsigned int handle_to_index( HANDLE handle, unsigned int *entry )
806 unsigned int idx = (wine_server_obj_handle(handle) >> 2) - 1;
807 *entry = idx / FD_CACHE_BLOCK_SIZE;
808 return idx % FD_CACHE_BLOCK_SIZE;
812 /***********************************************************************
813 * add_fd_to_cache
815 * Caller must hold fd_cache_section.
817 static int add_fd_to_cache( HANDLE handle, int fd, enum server_fd_type type,
818 unsigned int access, unsigned int options )
820 unsigned int entry, idx = handle_to_index( handle, &entry );
821 int prev_fd;
823 if (entry >= FD_CACHE_ENTRIES)
825 FIXME( "too many allocated handles, not caching %p\n", handle );
826 return 0;
829 if (!fd_cache[entry]) /* do we need to allocate a new block of entries? */
831 if (!entry) fd_cache[0] = fd_cache_initial_block;
832 else
834 void *ptr = wine_anon_mmap( NULL, FD_CACHE_BLOCK_SIZE * sizeof(struct fd_cache_entry),
835 PROT_READ | PROT_WRITE, 0 );
836 if (ptr == MAP_FAILED) return 0;
837 fd_cache[entry] = ptr;
840 /* store fd+1 so that 0 can be used as the unset value */
841 prev_fd = interlocked_xchg( &fd_cache[entry][idx].fd, fd + 1 ) - 1;
842 fd_cache[entry][idx].type = type;
843 fd_cache[entry][idx].access = access;
844 fd_cache[entry][idx].options = options;
845 if (prev_fd != -1) close( prev_fd );
846 return 1;
850 /***********************************************************************
851 * get_cached_fd
853 * Caller must hold fd_cache_section.
855 static inline int get_cached_fd( HANDLE handle, enum server_fd_type *type,
856 unsigned int *access, unsigned int *options )
858 unsigned int entry, idx = handle_to_index( handle, &entry );
859 int fd = -1;
861 if (entry < FD_CACHE_ENTRIES && fd_cache[entry])
863 fd = fd_cache[entry][idx].fd - 1;
864 if (type) *type = fd_cache[entry][idx].type;
865 if (access) *access = fd_cache[entry][idx].access;
866 if (options) *options = fd_cache[entry][idx].options;
868 return fd;
872 /***********************************************************************
873 * server_remove_fd_from_cache
875 int server_remove_fd_from_cache( HANDLE handle )
877 unsigned int entry, idx = handle_to_index( handle, &entry );
878 int fd = -1;
880 if (entry < FD_CACHE_ENTRIES && fd_cache[entry])
881 fd = interlocked_xchg( &fd_cache[entry][idx].fd, 0 ) - 1;
883 return fd;
887 /***********************************************************************
888 * server_get_unix_fd
890 * The returned unix_fd should be closed iff needs_close is non-zero.
892 int server_get_unix_fd( HANDLE handle, unsigned int wanted_access, int *unix_fd,
893 int *needs_close, enum server_fd_type *type, unsigned int *options )
895 sigset_t sigset;
896 obj_handle_t fd_handle;
897 int ret = 0, fd;
898 unsigned int access = 0;
900 *unix_fd = -1;
901 *needs_close = 0;
902 wanted_access &= FILE_READ_DATA | FILE_WRITE_DATA | FILE_APPEND_DATA;
904 server_enter_uninterrupted_section( &fd_cache_section, &sigset );
906 fd = get_cached_fd( handle, type, &access, options );
907 if (fd != -1) goto done;
909 SERVER_START_REQ( get_handle_fd )
911 req->handle = wine_server_obj_handle( handle );
912 if (!(ret = wine_server_call( req )))
914 if (type) *type = reply->type;
915 if (options) *options = reply->options;
916 access = reply->access;
917 if ((fd = receive_fd( &fd_handle )) != -1)
919 assert( wine_server_ptr_handle(fd_handle) == handle );
920 *needs_close = (!reply->cacheable ||
921 !add_fd_to_cache( handle, fd, reply->type,
922 reply->access, reply->options ));
924 else ret = STATUS_TOO_MANY_OPENED_FILES;
927 SERVER_END_REQ;
929 done:
930 server_leave_uninterrupted_section( &fd_cache_section, &sigset );
931 if (!ret && ((access & wanted_access) != wanted_access))
933 ret = STATUS_ACCESS_DENIED;
934 if (*needs_close) close( fd );
936 if (!ret) *unix_fd = fd;
937 return ret;
941 /***********************************************************************
942 * wine_server_fd_to_handle (NTDLL.@)
944 * Allocate a file handle for a Unix file descriptor.
946 * PARAMS
947 * fd [I] Unix file descriptor.
948 * access [I] Win32 access flags.
949 * attributes [I] Object attributes.
950 * handle [O] Address where Wine file handle will be stored.
952 * RETURNS
953 * NTSTATUS code
955 int CDECL wine_server_fd_to_handle( int fd, unsigned int access, unsigned int attributes, HANDLE *handle )
957 int ret;
959 *handle = 0;
960 wine_server_send_fd( fd );
962 SERVER_START_REQ( alloc_file_handle )
964 req->access = access;
965 req->attributes = attributes;
966 req->fd = fd;
967 if (!(ret = wine_server_call( req ))) *handle = wine_server_ptr_handle( reply->handle );
969 SERVER_END_REQ;
970 return ret;
974 /***********************************************************************
975 * wine_server_handle_to_fd (NTDLL.@)
977 * Retrieve the file descriptor corresponding to a file handle.
979 * PARAMS
980 * handle [I] Wine file handle.
981 * access [I] Win32 file access rights requested.
982 * unix_fd [O] Address where Unix file descriptor will be stored.
983 * options [O] Address where the file open options will be stored. Optional.
985 * RETURNS
986 * NTSTATUS code
988 int CDECL wine_server_handle_to_fd( HANDLE handle, unsigned int access, int *unix_fd,
989 unsigned int *options )
991 int needs_close, ret = server_get_unix_fd( handle, access, unix_fd, &needs_close, NULL, options );
993 if (!ret && !needs_close)
995 if ((*unix_fd = dup(*unix_fd)) == -1) ret = FILE_GetNtStatus();
997 return ret;
1001 /***********************************************************************
1002 * wine_server_release_fd (NTDLL.@)
1004 * Release the Unix file descriptor returned by wine_server_handle_to_fd.
1006 * PARAMS
1007 * handle [I] Wine file handle.
1008 * unix_fd [I] Unix file descriptor to release.
1010 * RETURNS
1011 * nothing
1013 void CDECL wine_server_release_fd( HANDLE handle, int unix_fd )
1015 close( unix_fd );
1019 /***********************************************************************
1020 * server_pipe
1022 * Create a pipe for communicating with the server.
1024 int server_pipe( int fd[2] )
1026 int ret;
1027 #ifdef HAVE_PIPE2
1028 static int have_pipe2 = 1;
1030 if (have_pipe2)
1032 if (!(ret = pipe2( fd, O_CLOEXEC ))) return ret;
1033 if (errno == ENOSYS || errno == EINVAL) have_pipe2 = 0; /* don't try again */
1035 #endif
1036 if (!(ret = pipe( fd )))
1038 fcntl( fd[0], F_SETFD, FD_CLOEXEC );
1039 fcntl( fd[1], F_SETFD, FD_CLOEXEC );
1041 return ret;
1045 /***********************************************************************
1046 * start_server
1048 * Start a new wine server.
1050 static void start_server(void)
1052 static int started; /* we only try once */
1053 char *argv[3];
1054 static char wineserver[] = "server/wineserver";
1055 static char debug[] = "-d";
1057 if (!started)
1059 int status;
1060 int pid = fork();
1061 if (pid == -1) fatal_perror( "fork" );
1062 if (!pid)
1064 argv[0] = wineserver;
1065 argv[1] = TRACE_ON(server) ? debug : NULL;
1066 argv[2] = NULL;
1067 wine_exec_wine_binary( argv[0], argv, getenv("WINESERVER") );
1068 fatal_error( "could not exec wineserver\n" );
1070 waitpid( pid, &status, 0 );
1071 status = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1072 if (status == 2) return; /* server lock held by someone else, will retry later */
1073 if (status) exit(status); /* server failed */
1074 started = 1;
1079 /***********************************************************************
1080 * setup_config_dir
1082 * Setup the wine configuration dir.
1084 static void setup_config_dir(void)
1086 const char *p, *config_dir = wine_get_config_dir();
1088 if (chdir( config_dir ) == -1)
1090 if (errno != ENOENT) fatal_perror( "chdir to %s\n", config_dir );
1092 if ((p = strrchr( config_dir, '/' )) && p != config_dir)
1094 struct stat st;
1095 char *tmp_dir;
1097 if (!(tmp_dir = malloc( p + 1 - config_dir ))) fatal_error( "out of memory\n" );
1098 memcpy( tmp_dir, config_dir, p - config_dir );
1099 tmp_dir[p - config_dir] = 0;
1100 if (!stat( tmp_dir, &st ) && st.st_uid != getuid())
1101 fatal_error( "'%s' is not owned by you, refusing to create a configuration directory there\n",
1102 tmp_dir );
1103 free( tmp_dir );
1106 mkdir( config_dir, 0777 );
1107 if (chdir( config_dir ) == -1) fatal_perror( "chdir to %s\n", config_dir );
1109 if ((p = getenv( "WINEARCH" )) && !strcmp( p, "win32" ))
1111 /* force creation of a 32-bit prefix */
1112 int fd = open( "system.reg", O_WRONLY | O_CREAT | O_EXCL, 0666 );
1113 if (fd != -1)
1115 static const char regfile[] = "WINE REGISTRY Version 2\n\n#arch=win32\n";
1116 write( fd, regfile, sizeof(regfile) - 1 );
1117 close( fd );
1120 MESSAGE( "wine: created the configuration directory '%s'\n", config_dir );
1123 if (mkdir( "dosdevices", 0777 ) == -1)
1125 if (errno == EEXIST) return;
1126 fatal_perror( "cannot create %s/dosdevices\n", config_dir );
1129 /* create the drive symlinks */
1131 mkdir( "drive_c", 0777 );
1132 symlink( "../drive_c", "dosdevices/c:" );
1133 symlink( "/", "dosdevices/z:" );
1137 /***********************************************************************
1138 * server_connect_error
1140 * Try to display a meaningful explanation of why we couldn't connect
1141 * to the server.
1143 static void server_connect_error( const char *serverdir )
1145 int fd;
1146 struct flock fl;
1148 if ((fd = open( LOCKNAME, O_WRONLY )) == -1)
1149 fatal_error( "for some mysterious reason, the wine server never started.\n" );
1151 fl.l_type = F_WRLCK;
1152 fl.l_whence = SEEK_SET;
1153 fl.l_start = 0;
1154 fl.l_len = 1;
1155 if (fcntl( fd, F_GETLK, &fl ) != -1)
1157 if (fl.l_type == F_WRLCK) /* the file is locked */
1158 fatal_error( "a wine server seems to be running, but I cannot connect to it.\n"
1159 " You probably need to kill that process (it might be pid %d).\n",
1160 (int)fl.l_pid );
1161 fatal_error( "for some mysterious reason, the wine server failed to run.\n" );
1163 fatal_error( "the file system of '%s' doesn't support locks,\n"
1164 " and there is a 'socket' file in that directory that prevents wine from starting.\n"
1165 " You should make sure no wine server is running, remove that file and try again.\n",
1166 serverdir );
1170 /***********************************************************************
1171 * server_connect
1173 * Attempt to connect to an existing server socket.
1174 * We need to be in the server directory already.
1176 static int server_connect(void)
1178 const char *serverdir;
1179 struct sockaddr_un addr;
1180 struct stat st;
1181 int s, slen, retry, fd_cwd;
1183 /* retrieve the current directory */
1184 fd_cwd = open( ".", O_RDONLY );
1185 if (fd_cwd != -1) fcntl( fd_cwd, F_SETFD, 1 ); /* set close on exec flag */
1187 setup_config_dir();
1188 serverdir = wine_get_server_dir();
1190 /* chdir to the server directory */
1191 if (chdir( serverdir ) == -1)
1193 if (errno != ENOENT) fatal_perror( "chdir to %s", serverdir );
1194 start_server();
1195 if (chdir( serverdir ) == -1) fatal_perror( "chdir to %s", serverdir );
1198 /* make sure we are at the right place */
1199 if (stat( ".", &st ) == -1) fatal_perror( "stat %s", serverdir );
1200 if (st.st_uid != getuid()) fatal_error( "'%s' is not owned by you\n", serverdir );
1201 if (st.st_mode & 077) fatal_error( "'%s' must not be accessible by other users\n", serverdir );
1203 for (retry = 0; retry < 6; retry++)
1205 /* if not the first try, wait a bit to leave the previous server time to exit */
1206 if (retry)
1208 usleep( 100000 * retry * retry );
1209 start_server();
1210 if (lstat( SOCKETNAME, &st ) == -1) continue; /* still no socket, wait a bit more */
1212 else if (lstat( SOCKETNAME, &st ) == -1) /* check for an already existing socket */
1214 if (errno != ENOENT) fatal_perror( "lstat %s/%s", serverdir, SOCKETNAME );
1215 start_server();
1216 if (lstat( SOCKETNAME, &st ) == -1) continue; /* still no socket, wait a bit more */
1219 /* make sure the socket is sane (ISFIFO needed for Solaris) */
1220 if (!S_ISSOCK(st.st_mode) && !S_ISFIFO(st.st_mode))
1221 fatal_error( "'%s/%s' is not a socket\n", serverdir, SOCKETNAME );
1222 if (st.st_uid != getuid())
1223 fatal_error( "'%s/%s' is not owned by you\n", serverdir, SOCKETNAME );
1225 /* try to connect to it */
1226 addr.sun_family = AF_UNIX;
1227 strcpy( addr.sun_path, SOCKETNAME );
1228 slen = sizeof(addr) - sizeof(addr.sun_path) + strlen(addr.sun_path) + 1;
1229 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
1230 addr.sun_len = slen;
1231 #endif
1232 if ((s = socket( AF_UNIX, SOCK_STREAM, 0 )) == -1) fatal_perror( "socket" );
1233 #ifdef SO_PASSCRED
1234 else
1236 int enable = 1;
1237 setsockopt( s, SOL_SOCKET, SO_PASSCRED, &enable, sizeof(enable) );
1239 #endif
1240 if (connect( s, (struct sockaddr *)&addr, slen ) != -1)
1242 /* switch back to the starting directory */
1243 if (fd_cwd != -1)
1245 fchdir( fd_cwd );
1246 close( fd_cwd );
1248 fcntl( s, F_SETFD, 1 ); /* set close on exec flag */
1249 return s;
1251 close( s );
1253 server_connect_error( serverdir );
1257 #ifdef __APPLE__
1258 #include <mach/mach.h>
1259 #include <mach/mach_error.h>
1260 #include <servers/bootstrap.h>
1262 /* send our task port to the server */
1263 static void send_server_task_port(void)
1265 mach_port_t bootstrap_port, wineserver_port;
1266 kern_return_t kret;
1268 struct {
1269 mach_msg_header_t header;
1270 mach_msg_body_t body;
1271 mach_msg_port_descriptor_t task_port;
1272 } msg;
1274 if (task_get_bootstrap_port(mach_task_self(), &bootstrap_port) != KERN_SUCCESS) return;
1276 kret = bootstrap_look_up(bootstrap_port, (char*)wine_get_server_dir(), &wineserver_port);
1277 if (kret != KERN_SUCCESS)
1278 fatal_error( "cannot find the server port: 0x%08x\n", kret );
1280 mach_port_deallocate(mach_task_self(), bootstrap_port);
1282 msg.header.msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, 0) | MACH_MSGH_BITS_COMPLEX;
1283 msg.header.msgh_size = sizeof(msg);
1284 msg.header.msgh_remote_port = wineserver_port;
1285 msg.header.msgh_local_port = MACH_PORT_NULL;
1287 msg.body.msgh_descriptor_count = 1;
1288 msg.task_port.name = mach_task_self();
1289 msg.task_port.disposition = MACH_MSG_TYPE_COPY_SEND;
1290 msg.task_port.type = MACH_MSG_PORT_DESCRIPTOR;
1292 kret = mach_msg_send(&msg.header);
1293 if (kret != KERN_SUCCESS)
1294 server_protocol_error( "mach_msg_send failed: 0x%08x\n", kret );
1296 mach_port_deallocate(mach_task_self(), wineserver_port);
1298 #endif /* __APPLE__ */
1301 /***********************************************************************
1302 * get_unix_tid
1304 * Retrieve the Unix tid to use on the server side for the current thread.
1306 static int get_unix_tid(void)
1308 int ret = -1;
1309 #ifdef HAVE_PTHREAD_GETTHREADID_NP
1310 ret = pthread_getthreadid_np();
1311 #elif defined(linux)
1312 ret = syscall( __NR_gettid );
1313 #elif defined(__sun)
1314 ret = pthread_self();
1315 #elif defined(__APPLE__)
1316 ret = mach_thread_self();
1317 mach_port_deallocate(mach_task_self(), ret);
1318 #elif defined(__NetBSD__)
1319 ret = _lwp_self();
1320 #elif defined(__FreeBSD__)
1321 long lwpid;
1322 thr_self( &lwpid );
1323 ret = lwpid;
1324 #elif defined(__DragonFly__)
1325 ret = lwp_gettid();
1326 #endif
1327 return ret;
1331 /***********************************************************************
1332 * server_init_process
1334 * Start the server and create the initial socket pair.
1336 void server_init_process(void)
1338 obj_handle_t version;
1339 const char *env_socket = getenv( "WINESERVERSOCKET" );
1341 server_pid = -1;
1342 if (env_socket)
1344 fd_socket = atoi( env_socket );
1345 if (fcntl( fd_socket, F_SETFD, 1 ) == -1)
1346 fatal_perror( "Bad server socket %d", fd_socket );
1347 unsetenv( "WINESERVERSOCKET" );
1349 else fd_socket = server_connect();
1351 /* setup the signal mask */
1352 sigemptyset( &server_block_set );
1353 sigaddset( &server_block_set, SIGALRM );
1354 sigaddset( &server_block_set, SIGIO );
1355 sigaddset( &server_block_set, SIGINT );
1356 sigaddset( &server_block_set, SIGHUP );
1357 sigaddset( &server_block_set, SIGUSR1 );
1358 sigaddset( &server_block_set, SIGUSR2 );
1359 sigaddset( &server_block_set, SIGCHLD );
1360 pthread_sigmask( SIG_BLOCK, &server_block_set, NULL );
1362 /* receive the first thread request fd on the main socket */
1363 ntdll_get_thread_data()->request_fd = receive_fd( &version );
1365 #ifdef SO_PASSCRED
1366 /* now that we hopefully received the server_pid, disable SO_PASSCRED */
1368 int enable = 0;
1369 setsockopt( fd_socket, SOL_SOCKET, SO_PASSCRED, &enable, sizeof(enable) );
1371 #endif
1373 if (version != SERVER_PROTOCOL_VERSION)
1374 server_protocol_error( "version mismatch %d/%d.\n"
1375 "Your %s binary was not upgraded correctly,\n"
1376 "or you have an older one somewhere in your PATH.\n"
1377 "Or maybe the wrong wineserver is still running?\n",
1378 version, SERVER_PROTOCOL_VERSION,
1379 (version > SERVER_PROTOCOL_VERSION) ? "wine" : "wineserver" );
1380 #ifdef __APPLE__
1381 send_server_task_port();
1382 #endif
1383 #if defined(__linux__) && defined(HAVE_PRCTL)
1384 /* work around Ubuntu's ptrace breakage */
1385 if (server_pid != -1) prctl( 0x59616d61 /* PR_SET_PTRACER */, server_pid );
1386 #endif
1390 /***********************************************************************
1391 * server_init_process_done
1393 NTSTATUS server_init_process_done(void)
1395 PEB *peb = NtCurrentTeb()->Peb;
1396 IMAGE_NT_HEADERS *nt = RtlImageNtHeader( peb->ImageBaseAddress );
1397 NTSTATUS status;
1399 /* Install signal handlers; this cannot be done earlier, since we cannot
1400 * send exceptions to the debugger before the create process event that
1401 * is sent by REQ_INIT_PROCESS_DONE.
1402 * We do need the handlers in place by the time the request is over, so
1403 * we set them up here. If we segfault between here and the server call
1404 * something is very wrong... */
1405 signal_init_process();
1407 /* Signal the parent process to continue */
1408 SERVER_START_REQ( init_process_done )
1410 req->module = wine_server_client_ptr( peb->ImageBaseAddress );
1411 #ifdef __i386__
1412 req->ldt_copy = wine_server_client_ptr( &wine_ldt_copy );
1413 #endif
1414 req->entry = wine_server_client_ptr( (char *)peb->ImageBaseAddress + nt->OptionalHeader.AddressOfEntryPoint );
1415 req->gui = (nt->OptionalHeader.Subsystem != IMAGE_SUBSYSTEM_WINDOWS_CUI);
1416 status = wine_server_call( req );
1418 SERVER_END_REQ;
1420 return status;
1424 /***********************************************************************
1425 * server_init_thread
1427 * Send an init thread request. Return 0 if OK.
1429 size_t server_init_thread( void *entry_point )
1431 static const int is_win64 = (sizeof(void *) > sizeof(int));
1432 const char *arch = getenv( "WINEARCH" );
1433 int ret;
1434 int reply_pipe[2];
1435 struct sigaction sig_act;
1436 size_t info_size;
1438 sig_act.sa_handler = SIG_IGN;
1439 sig_act.sa_flags = 0;
1440 sigemptyset( &sig_act.sa_mask );
1442 /* ignore SIGPIPE so that we get an EPIPE error instead */
1443 sigaction( SIGPIPE, &sig_act, NULL );
1445 /* create the server->client communication pipes */
1446 if (server_pipe( reply_pipe ) == -1) server_protocol_perror( "pipe" );
1447 if (server_pipe( ntdll_get_thread_data()->wait_fd ) == -1) server_protocol_perror( "pipe" );
1448 wine_server_send_fd( reply_pipe[1] );
1449 wine_server_send_fd( ntdll_get_thread_data()->wait_fd[1] );
1450 ntdll_get_thread_data()->reply_fd = reply_pipe[0];
1451 close( reply_pipe[1] );
1453 SERVER_START_REQ( init_thread )
1455 req->unix_pid = getpid();
1456 req->unix_tid = get_unix_tid();
1457 req->teb = wine_server_client_ptr( NtCurrentTeb() );
1458 req->entry = wine_server_client_ptr( entry_point );
1459 req->reply_fd = reply_pipe[1];
1460 req->wait_fd = ntdll_get_thread_data()->wait_fd[1];
1461 req->debug_level = (TRACE_ON(server) != 0);
1462 req->cpu = client_cpu;
1463 ret = wine_server_call( req );
1464 NtCurrentTeb()->ClientId.UniqueProcess = ULongToHandle(reply->pid);
1465 NtCurrentTeb()->ClientId.UniqueThread = ULongToHandle(reply->tid);
1466 info_size = reply->info_size;
1467 server_start_time = reply->server_start;
1468 server_cpus = reply->all_cpus;
1470 SERVER_END_REQ;
1472 is_wow64 = !is_win64 && (server_cpus & (1 << CPU_x86_64)) != 0;
1473 ntdll_get_thread_data()->wow64_redir = is_wow64;
1475 switch (ret)
1477 case STATUS_SUCCESS:
1478 if (arch)
1480 if (!strcmp( arch, "win32" ) && (is_win64 || is_wow64))
1481 fatal_error( "WINEARCH set to win32 but '%s' is a 64-bit installation.\n",
1482 wine_get_config_dir() );
1483 if (!strcmp( arch, "win64" ) && !is_win64 && !is_wow64)
1484 fatal_error( "WINEARCH set to win64 but '%s' is a 32-bit installation.\n",
1485 wine_get_config_dir() );
1487 return info_size;
1488 case STATUS_NOT_REGISTRY_FILE:
1489 fatal_error( "'%s' is a 32-bit installation, it cannot support 64-bit applications.\n",
1490 wine_get_config_dir() );
1491 case STATUS_NOT_SUPPORTED:
1492 if (is_win64)
1493 fatal_error( "wineserver is 32-bit, it cannot support 64-bit applications.\n" );
1494 else
1495 fatal_error( "'%s' is a 64-bit installation, it cannot be used with a 32-bit wineserver.\n",
1496 wine_get_config_dir() );
1497 default:
1498 server_protocol_error( "init_thread failed with status %x\n", ret );