server: Allow passing an argument for the user APC async I/O callbacks.
[wine.git] / dlls / ntdll / server.c
blobe3f1694872ea634a1ec3973a94e5996e429c9038
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_UCONTEXT_H
67 # include <sys/ucontext.h>
68 #endif
69 #ifdef HAVE_SYS_THR_H
70 #include <sys/thr.h>
71 #endif
72 #ifdef HAVE_UNISTD_H
73 # include <unistd.h>
74 #endif
76 #include "ntstatus.h"
77 #define WIN32_NO_STATUS
78 #include "wine/library.h"
79 #include "wine/server.h"
80 #include "wine/debug.h"
81 #include "ntdll_misc.h"
83 WINE_DEFAULT_DEBUG_CHANNEL(server);
85 /* Some versions of glibc don't define this */
86 #ifndef SCM_RIGHTS
87 #define SCM_RIGHTS 1
88 #endif
90 #ifndef MSG_CMSG_CLOEXEC
91 #define MSG_CMSG_CLOEXEC 0
92 #endif
94 #define SOCKETNAME "socket" /* name of the socket file */
95 #define LOCKNAME "lock" /* name of the lock file */
97 #ifdef __i386__
98 static const enum cpu_type client_cpu = CPU_x86;
99 #elif defined(__x86_64__)
100 static const enum cpu_type client_cpu = CPU_x86_64;
101 #elif defined(__powerpc__)
102 static const enum cpu_type client_cpu = CPU_POWERPC;
103 #elif defined(__arm__)
104 static const enum cpu_type client_cpu = CPU_ARM;
105 #elif defined(__aarch64__)
106 static const enum cpu_type client_cpu = CPU_ARM64;
107 #else
108 #error Unsupported CPU
109 #endif
111 unsigned int server_cpus = 0;
112 BOOL is_wow64 = FALSE;
114 timeout_t server_start_time = 0; /* time of server startup */
116 sigset_t server_block_set; /* signals to block during server calls */
117 static int fd_socket = -1; /* socket to exchange file descriptors with the server */
118 static pid_t server_pid;
120 static RTL_CRITICAL_SECTION fd_cache_section;
121 static RTL_CRITICAL_SECTION_DEBUG critsect_debug =
123 0, 0, &fd_cache_section,
124 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
125 0, 0, { (DWORD_PTR)(__FILE__ ": fd_cache_section") }
127 static RTL_CRITICAL_SECTION fd_cache_section = { &critsect_debug, -1, 0, 0, 0, 0 };
130 #ifdef __GNUC__
131 static void fatal_error( const char *err, ... ) __attribute__((noreturn, format(printf,1,2)));
132 static void fatal_perror( const char *err, ... ) __attribute__((noreturn, format(printf,1,2)));
133 static void server_connect_error( const char *serverdir ) __attribute__((noreturn));
134 #endif
136 /* die on a fatal error; use only during initialization */
137 static void fatal_error( const char *err, ... )
139 va_list args;
141 va_start( args, err );
142 fprintf( stderr, "wine: " );
143 vfprintf( stderr, err, args );
144 va_end( args );
145 exit(1);
148 /* die on a fatal error; use only during initialization */
149 static void fatal_perror( const char *err, ... )
151 va_list args;
153 va_start( args, err );
154 fprintf( stderr, "wine: " );
155 vfprintf( stderr, err, args );
156 perror( " " );
157 va_end( args );
158 exit(1);
162 /***********************************************************************
163 * server_protocol_error
165 static DECLSPEC_NORETURN void server_protocol_error( const char *err, ... )
167 va_list args;
169 va_start( args, err );
170 fprintf( stderr, "wine client error:%x: ", GetCurrentThreadId() );
171 vfprintf( stderr, err, args );
172 va_end( args );
173 abort_thread(1);
177 /***********************************************************************
178 * server_protocol_perror
180 static DECLSPEC_NORETURN void server_protocol_perror( const char *err )
182 fprintf( stderr, "wine client error:%x: ", GetCurrentThreadId() );
183 perror( err );
184 abort_thread(1);
188 /***********************************************************************
189 * send_request
191 * Send a request to the server.
193 static unsigned int send_request( const struct __server_request_info *req )
195 unsigned int i;
196 int ret;
198 if (!req->u.req.request_header.request_size)
200 if ((ret = write( ntdll_get_thread_data()->request_fd, &req->u.req,
201 sizeof(req->u.req) )) == sizeof(req->u.req)) return STATUS_SUCCESS;
204 else
206 struct iovec vec[__SERVER_MAX_DATA+1];
208 vec[0].iov_base = (void *)&req->u.req;
209 vec[0].iov_len = sizeof(req->u.req);
210 for (i = 0; i < req->data_count; i++)
212 vec[i+1].iov_base = (void *)req->data[i].ptr;
213 vec[i+1].iov_len = req->data[i].size;
215 if ((ret = writev( ntdll_get_thread_data()->request_fd, vec, i+1 )) ==
216 req->u.req.request_header.request_size + sizeof(req->u.req)) return STATUS_SUCCESS;
219 if (ret >= 0) server_protocol_error( "partial write %d\n", ret );
220 if (errno == EPIPE) abort_thread(0);
221 if (errno == EFAULT) return STATUS_ACCESS_VIOLATION;
222 server_protocol_perror( "write" );
226 /***********************************************************************
227 * read_reply_data
229 * Read data from the reply buffer; helper for wait_reply.
231 static void read_reply_data( void *buffer, size_t size )
233 int ret;
235 for (;;)
237 if ((ret = read( ntdll_get_thread_data()->reply_fd, buffer, size )) > 0)
239 if (!(size -= ret)) return;
240 buffer = (char *)buffer + ret;
241 continue;
243 if (!ret) break;
244 if (errno == EINTR) continue;
245 if (errno == EPIPE) break;
246 server_protocol_perror("read");
248 /* the server closed the connection; time to die... */
249 abort_thread(0);
253 /***********************************************************************
254 * wait_reply
256 * Wait for a reply from the server.
258 static inline unsigned int wait_reply( struct __server_request_info *req )
260 read_reply_data( &req->u.reply, sizeof(req->u.reply) );
261 if (req->u.reply.reply_header.reply_size)
262 read_reply_data( req->reply_data, req->u.reply.reply_header.reply_size );
263 return req->u.reply.reply_header.error;
267 /***********************************************************************
268 * wine_server_call (NTDLL.@)
270 * Perform a server call.
272 * PARAMS
273 * req_ptr [I/O] Function dependent data
275 * RETURNS
276 * Depends on server function being called, but usually an NTSTATUS code.
278 * NOTES
279 * Use the SERVER_START_REQ and SERVER_END_REQ to help you fill out the
280 * server request structure for the particular call. E.g:
281 *| SERVER_START_REQ( event_op )
282 *| {
283 *| req->handle = handle;
284 *| req->op = SET_EVENT;
285 *| ret = wine_server_call( req );
286 *| }
287 *| SERVER_END_REQ;
289 unsigned int wine_server_call( void *req_ptr )
291 struct __server_request_info * const req = req_ptr;
292 sigset_t old_set;
293 unsigned int ret;
295 pthread_sigmask( SIG_BLOCK, &server_block_set, &old_set );
296 ret = send_request( req );
297 if (!ret) ret = wait_reply( req );
298 pthread_sigmask( SIG_SETMASK, &old_set, NULL );
299 return ret;
303 /***********************************************************************
304 * server_enter_uninterrupted_section
306 void server_enter_uninterrupted_section( RTL_CRITICAL_SECTION *cs, sigset_t *sigset )
308 pthread_sigmask( SIG_BLOCK, &server_block_set, sigset );
309 RtlEnterCriticalSection( cs );
313 /***********************************************************************
314 * server_leave_uninterrupted_section
316 void server_leave_uninterrupted_section( RTL_CRITICAL_SECTION *cs, sigset_t *sigset )
318 RtlLeaveCriticalSection( cs );
319 pthread_sigmask( SIG_SETMASK, sigset, NULL );
323 /***********************************************************************
324 * wait_select_reply
326 * Wait for a reply on the waiting pipe of the current thread.
328 static int wait_select_reply( void *cookie )
330 int signaled;
331 struct wake_up_reply reply;
332 for (;;)
334 int ret;
335 ret = read( ntdll_get_thread_data()->wait_fd[0], &reply, sizeof(reply) );
336 if (ret == sizeof(reply))
338 if (!reply.cookie) abort_thread( reply.signaled ); /* thread got killed */
339 if (wine_server_get_ptr(reply.cookie) == cookie) return reply.signaled;
340 /* we stole another reply, wait for the real one */
341 signaled = wait_select_reply( cookie );
342 /* and now put the wrong one back in the pipe */
343 for (;;)
345 ret = write( ntdll_get_thread_data()->wait_fd[1], &reply, sizeof(reply) );
346 if (ret == sizeof(reply)) break;
347 if (ret >= 0) server_protocol_error( "partial wakeup write %d\n", ret );
348 if (errno == EINTR) continue;
349 server_protocol_perror("wakeup write");
351 return signaled;
353 if (ret >= 0) server_protocol_error( "partial wakeup read %d\n", ret );
354 if (errno == EINTR) continue;
355 server_protocol_perror("wakeup read");
360 /***********************************************************************
361 * invoke_apc
363 * Invoke a single APC. Return TRUE if a user APC has been run.
365 static BOOL invoke_apc( const apc_call_t *call, apc_result_t *result )
367 BOOL user_apc = FALSE;
368 SIZE_T size;
369 void *addr;
371 memset( result, 0, sizeof(*result) );
373 switch (call->type)
375 case APC_NONE:
376 break;
377 case APC_USER:
379 void (WINAPI *func)(ULONG_PTR,ULONG_PTR,ULONG_PTR) = wine_server_get_ptr( call->user.func );
380 func( call->user.args[0], call->user.args[1], call->user.args[2] );
381 user_apc = TRUE;
382 break;
384 case APC_TIMER:
386 void (WINAPI *func)(void*, unsigned int, unsigned int) = wine_server_get_ptr( call->timer.func );
387 func( wine_server_get_ptr( call->timer.arg ),
388 (DWORD)call->timer.time, (DWORD)(call->timer.time >> 32) );
389 user_apc = TRUE;
390 break;
392 case APC_ASYNC_IO:
394 void *apc = NULL;
395 IO_STATUS_BLOCK *iosb = wine_server_get_ptr( call->async_io.sb );
396 NTSTATUS (*func)(void *, IO_STATUS_BLOCK *, NTSTATUS, void **) = wine_server_get_ptr( call->async_io.func );
397 result->type = call->type;
398 result->async_io.status = func( wine_server_get_ptr( call->async_io.user ),
399 iosb, call->async_io.status, &apc );
400 if (result->async_io.status != STATUS_PENDING)
402 result->async_io.total = iosb->Information;
403 result->async_io.apc = wine_server_client_ptr( apc );
404 result->async_io.arg = call->async_io.user;
406 break;
408 case APC_VIRTUAL_ALLOC:
409 result->type = call->type;
410 addr = wine_server_get_ptr( call->virtual_alloc.addr );
411 size = call->virtual_alloc.size;
412 if ((ULONG_PTR)addr == call->virtual_alloc.addr && size == call->virtual_alloc.size)
414 result->virtual_alloc.status = NtAllocateVirtualMemory( NtCurrentProcess(), &addr,
415 call->virtual_alloc.zero_bits, &size,
416 call->virtual_alloc.op_type,
417 call->virtual_alloc.prot );
418 result->virtual_alloc.addr = wine_server_client_ptr( addr );
419 result->virtual_alloc.size = size;
421 else result->virtual_alloc.status = STATUS_WORKING_SET_LIMIT_RANGE;
422 break;
423 case APC_VIRTUAL_FREE:
424 result->type = call->type;
425 addr = wine_server_get_ptr( call->virtual_free.addr );
426 size = call->virtual_free.size;
427 if ((ULONG_PTR)addr == call->virtual_free.addr && size == call->virtual_free.size)
429 result->virtual_free.status = NtFreeVirtualMemory( NtCurrentProcess(), &addr, &size,
430 call->virtual_free.op_type );
431 result->virtual_free.addr = wine_server_client_ptr( addr );
432 result->virtual_free.size = size;
434 else result->virtual_free.status = STATUS_INVALID_PARAMETER;
435 break;
436 case APC_VIRTUAL_QUERY:
438 MEMORY_BASIC_INFORMATION info;
439 result->type = call->type;
440 addr = wine_server_get_ptr( call->virtual_query.addr );
441 if ((ULONG_PTR)addr == call->virtual_query.addr)
442 result->virtual_query.status = NtQueryVirtualMemory( NtCurrentProcess(),
443 addr, MemoryBasicInformation, &info,
444 sizeof(info), NULL );
445 else
446 result->virtual_query.status = STATUS_WORKING_SET_LIMIT_RANGE;
448 if (result->virtual_query.status == STATUS_SUCCESS)
450 result->virtual_query.base = wine_server_client_ptr( info.BaseAddress );
451 result->virtual_query.alloc_base = wine_server_client_ptr( info.AllocationBase );
452 result->virtual_query.size = info.RegionSize;
453 result->virtual_query.prot = info.Protect;
454 result->virtual_query.alloc_prot = info.AllocationProtect;
455 result->virtual_query.state = info.State >> 12;
456 result->virtual_query.alloc_type = info.Type >> 16;
458 break;
460 case APC_VIRTUAL_PROTECT:
461 result->type = call->type;
462 addr = wine_server_get_ptr( call->virtual_protect.addr );
463 size = call->virtual_protect.size;
464 if ((ULONG_PTR)addr == call->virtual_protect.addr && size == call->virtual_protect.size)
466 result->virtual_protect.status = NtProtectVirtualMemory( NtCurrentProcess(), &addr, &size,
467 call->virtual_protect.prot,
468 &result->virtual_protect.prot );
469 result->virtual_protect.addr = wine_server_client_ptr( addr );
470 result->virtual_protect.size = size;
472 else result->virtual_protect.status = STATUS_INVALID_PARAMETER;
473 break;
474 case APC_VIRTUAL_FLUSH:
475 result->type = call->type;
476 addr = wine_server_get_ptr( call->virtual_flush.addr );
477 size = call->virtual_flush.size;
478 if ((ULONG_PTR)addr == call->virtual_flush.addr && size == call->virtual_flush.size)
480 result->virtual_flush.status = NtFlushVirtualMemory( NtCurrentProcess(),
481 (const void **)&addr, &size, 0 );
482 result->virtual_flush.addr = wine_server_client_ptr( addr );
483 result->virtual_flush.size = size;
485 else result->virtual_flush.status = STATUS_INVALID_PARAMETER;
486 break;
487 case APC_VIRTUAL_LOCK:
488 result->type = call->type;
489 addr = wine_server_get_ptr( call->virtual_lock.addr );
490 size = call->virtual_lock.size;
491 if ((ULONG_PTR)addr == call->virtual_lock.addr && size == call->virtual_lock.size)
493 result->virtual_lock.status = NtLockVirtualMemory( NtCurrentProcess(), &addr, &size, 0 );
494 result->virtual_lock.addr = wine_server_client_ptr( addr );
495 result->virtual_lock.size = size;
497 else result->virtual_lock.status = STATUS_INVALID_PARAMETER;
498 break;
499 case APC_VIRTUAL_UNLOCK:
500 result->type = call->type;
501 addr = wine_server_get_ptr( call->virtual_unlock.addr );
502 size = call->virtual_unlock.size;
503 if ((ULONG_PTR)addr == call->virtual_unlock.addr && size == call->virtual_unlock.size)
505 result->virtual_unlock.status = NtUnlockVirtualMemory( NtCurrentProcess(), &addr, &size, 0 );
506 result->virtual_unlock.addr = wine_server_client_ptr( addr );
507 result->virtual_unlock.size = size;
509 else result->virtual_unlock.status = STATUS_INVALID_PARAMETER;
510 break;
511 case APC_MAP_VIEW:
512 result->type = call->type;
513 addr = wine_server_get_ptr( call->map_view.addr );
514 size = call->map_view.size;
515 if ((ULONG_PTR)addr == call->map_view.addr && size == call->map_view.size)
517 LARGE_INTEGER offset;
518 offset.QuadPart = call->map_view.offset;
519 result->map_view.status = NtMapViewOfSection( wine_server_ptr_handle(call->map_view.handle),
520 NtCurrentProcess(), &addr,
521 call->map_view.zero_bits, 0,
522 &offset, &size, ViewShare,
523 call->map_view.alloc_type, call->map_view.prot );
524 result->map_view.addr = wine_server_client_ptr( addr );
525 result->map_view.size = size;
527 else result->map_view.status = STATUS_INVALID_PARAMETER;
528 NtClose( wine_server_ptr_handle(call->map_view.handle) );
529 break;
530 case APC_UNMAP_VIEW:
531 result->type = call->type;
532 addr = wine_server_get_ptr( call->unmap_view.addr );
533 if ((ULONG_PTR)addr == call->unmap_view.addr)
534 result->unmap_view.status = NtUnmapViewOfSection( NtCurrentProcess(), addr );
535 else
536 result->unmap_view.status = STATUS_INVALID_PARAMETER;
537 break;
538 case APC_CREATE_THREAD:
540 CLIENT_ID id;
541 HANDLE handle;
542 SIZE_T reserve = call->create_thread.reserve;
543 SIZE_T commit = call->create_thread.commit;
544 void *func = wine_server_get_ptr( call->create_thread.func );
545 void *arg = wine_server_get_ptr( call->create_thread.arg );
547 result->type = call->type;
548 if (reserve == call->create_thread.reserve && commit == call->create_thread.commit &&
549 (ULONG_PTR)func == call->create_thread.func && (ULONG_PTR)arg == call->create_thread.arg)
551 result->create_thread.status = RtlCreateUserThread( NtCurrentProcess(), NULL,
552 call->create_thread.suspend, NULL,
553 reserve, commit, func, arg, &handle, &id );
554 result->create_thread.handle = wine_server_obj_handle( handle );
555 result->create_thread.tid = HandleToULong(id.UniqueThread);
557 else result->create_thread.status = STATUS_INVALID_PARAMETER;
558 break;
560 default:
561 server_protocol_error( "get_apc_request: bad type %d\n", call->type );
562 break;
564 return user_apc;
568 /***********************************************************************
569 * server_select
571 unsigned int server_select( const select_op_t *select_op, data_size_t size, UINT flags,
572 const LARGE_INTEGER *timeout )
574 unsigned int ret;
575 int cookie;
576 BOOL user_apc = FALSE;
577 obj_handle_t apc_handle = 0;
578 apc_call_t call;
579 apc_result_t result;
580 timeout_t abs_timeout = timeout ? timeout->QuadPart : TIMEOUT_INFINITE;
582 memset( &result, 0, sizeof(result) );
584 for (;;)
586 SERVER_START_REQ( select )
588 req->flags = flags;
589 req->cookie = wine_server_client_ptr( &cookie );
590 req->prev_apc = apc_handle;
591 req->timeout = abs_timeout;
592 wine_server_add_data( req, &result, sizeof(result) );
593 wine_server_add_data( req, select_op, size );
594 ret = wine_server_call( req );
595 abs_timeout = reply->timeout;
596 apc_handle = reply->apc_handle;
597 call = reply->call;
599 SERVER_END_REQ;
600 if (ret == STATUS_PENDING) ret = wait_select_reply( &cookie );
601 if (ret != STATUS_USER_APC) break;
602 if (invoke_apc( &call, &result ))
604 /* if we ran a user apc we have to check once more if an object got signaled,
605 * but we don't want to wait */
606 abs_timeout = 0;
607 user_apc = TRUE;
610 /* don't signal multiple times */
611 if (size >= sizeof(select_op->signal_and_wait) && select_op->op == SELECT_SIGNAL_AND_WAIT)
612 size = offsetof( select_op_t, signal_and_wait.signal );
615 if (ret == STATUS_TIMEOUT && user_apc) ret = STATUS_USER_APC;
617 /* A test on Windows 2000 shows that Windows always yields during
618 a wait, but a wait that is hit by an event gets a priority
619 boost as well. This seems to model that behavior the closest. */
620 if (ret == STATUS_TIMEOUT) NtYieldExecution();
622 return ret;
626 /***********************************************************************
627 * server_queue_process_apc
629 unsigned int server_queue_process_apc( HANDLE process, const apc_call_t *call, apc_result_t *result )
631 for (;;)
633 unsigned int ret;
634 HANDLE handle = 0;
635 BOOL self = FALSE;
637 SERVER_START_REQ( queue_apc )
639 req->handle = wine_server_obj_handle( process );
640 req->call = *call;
641 if (!(ret = wine_server_call( req )))
643 handle = wine_server_ptr_handle( reply->handle );
644 self = reply->self;
647 SERVER_END_REQ;
648 if (ret != STATUS_SUCCESS) return ret;
650 if (self)
652 invoke_apc( call, result );
654 else
656 NtWaitForSingleObject( handle, FALSE, NULL );
658 SERVER_START_REQ( get_apc_result )
660 req->handle = wine_server_obj_handle( handle );
661 if (!(ret = wine_server_call( req ))) *result = reply->result;
663 SERVER_END_REQ;
665 if (!ret && result->type == APC_NONE) continue; /* APC didn't run, try again */
666 if (ret) NtClose( handle );
668 return ret;
673 /***********************************************************************
674 * wine_server_send_fd (NTDLL.@)
676 * Send a file descriptor to the server.
678 * PARAMS
679 * fd [I] file descriptor to send
681 * RETURNS
682 * nothing
684 void CDECL wine_server_send_fd( int fd )
686 struct send_fd data;
687 struct msghdr msghdr;
688 struct iovec vec;
689 int ret;
691 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
692 msghdr.msg_accrights = (void *)&fd;
693 msghdr.msg_accrightslen = sizeof(fd);
694 #else /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
695 char cmsg_buffer[256];
696 struct cmsghdr *cmsg;
697 msghdr.msg_control = cmsg_buffer;
698 msghdr.msg_controllen = sizeof(cmsg_buffer);
699 msghdr.msg_flags = 0;
700 cmsg = CMSG_FIRSTHDR( &msghdr );
701 cmsg->cmsg_len = CMSG_LEN( sizeof(fd) );
702 cmsg->cmsg_level = SOL_SOCKET;
703 cmsg->cmsg_type = SCM_RIGHTS;
704 *(int *)CMSG_DATA(cmsg) = fd;
705 msghdr.msg_controllen = cmsg->cmsg_len;
706 #endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
708 msghdr.msg_name = NULL;
709 msghdr.msg_namelen = 0;
710 msghdr.msg_iov = &vec;
711 msghdr.msg_iovlen = 1;
713 vec.iov_base = (void *)&data;
714 vec.iov_len = sizeof(data);
716 data.tid = GetCurrentThreadId();
717 data.fd = fd;
719 for (;;)
721 if ((ret = sendmsg( fd_socket, &msghdr, 0 )) == sizeof(data)) return;
722 if (ret >= 0) server_protocol_error( "partial write %d\n", ret );
723 if (errno == EINTR) continue;
724 if (errno == EPIPE) abort_thread(0);
725 server_protocol_perror( "sendmsg" );
730 /***********************************************************************
731 * receive_fd
733 * Receive a file descriptor passed from the server.
735 static int receive_fd( obj_handle_t *handle )
737 struct iovec vec;
738 struct msghdr msghdr;
739 int ret, fd = -1;
741 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
742 msghdr.msg_accrights = (void *)&fd;
743 msghdr.msg_accrightslen = sizeof(fd);
744 #else /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
745 char cmsg_buffer[256];
746 msghdr.msg_control = cmsg_buffer;
747 msghdr.msg_controllen = sizeof(cmsg_buffer);
748 msghdr.msg_flags = 0;
749 #endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
751 msghdr.msg_name = NULL;
752 msghdr.msg_namelen = 0;
753 msghdr.msg_iov = &vec;
754 msghdr.msg_iovlen = 1;
755 vec.iov_base = (void *)handle;
756 vec.iov_len = sizeof(*handle);
758 for (;;)
760 if ((ret = recvmsg( fd_socket, &msghdr, MSG_CMSG_CLOEXEC )) > 0)
762 #ifndef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
763 struct cmsghdr *cmsg;
764 for (cmsg = CMSG_FIRSTHDR( &msghdr ); cmsg; cmsg = CMSG_NXTHDR( &msghdr, cmsg ))
766 if (cmsg->cmsg_level != SOL_SOCKET) continue;
767 if (cmsg->cmsg_type == SCM_RIGHTS) fd = *(int *)CMSG_DATA(cmsg);
768 #ifdef SCM_CREDENTIALS
769 else if (cmsg->cmsg_type == SCM_CREDENTIALS)
771 struct ucred *ucred = (struct ucred *)CMSG_DATA(cmsg);
772 server_pid = ucred->pid;
774 #endif
776 #endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
777 if (fd != -1) fcntl( fd, F_SETFD, FD_CLOEXEC ); /* in case MSG_CMSG_CLOEXEC is not supported */
778 return fd;
780 if (!ret) break;
781 if (errno == EINTR) continue;
782 if (errno == EPIPE) break;
783 server_protocol_perror("recvmsg");
785 /* the server closed the connection; time to die... */
786 abort_thread(0);
790 /***********************************************************************/
791 /* fd cache support */
793 struct fd_cache_entry
795 int fd;
796 enum server_fd_type type : 5;
797 unsigned int access : 3;
798 unsigned int options : 24;
801 #define FD_CACHE_BLOCK_SIZE (65536 / sizeof(struct fd_cache_entry))
802 #define FD_CACHE_ENTRIES 128
804 static struct fd_cache_entry *fd_cache[FD_CACHE_ENTRIES];
805 static struct fd_cache_entry fd_cache_initial_block[FD_CACHE_BLOCK_SIZE];
807 static inline unsigned int handle_to_index( HANDLE handle, unsigned int *entry )
809 unsigned int idx = (wine_server_obj_handle(handle) >> 2) - 1;
810 *entry = idx / FD_CACHE_BLOCK_SIZE;
811 return idx % FD_CACHE_BLOCK_SIZE;
815 /***********************************************************************
816 * add_fd_to_cache
818 * Caller must hold fd_cache_section.
820 static BOOL add_fd_to_cache( HANDLE handle, int fd, enum server_fd_type type,
821 unsigned int access, unsigned int options )
823 unsigned int entry, idx = handle_to_index( handle, &entry );
824 int prev_fd;
826 if (entry >= FD_CACHE_ENTRIES)
828 FIXME( "too many allocated handles, not caching %p\n", handle );
829 return FALSE;
832 if (!fd_cache[entry]) /* do we need to allocate a new block of entries? */
834 if (!entry) fd_cache[0] = fd_cache_initial_block;
835 else
837 void *ptr = wine_anon_mmap( NULL, FD_CACHE_BLOCK_SIZE * sizeof(struct fd_cache_entry),
838 PROT_READ | PROT_WRITE, 0 );
839 if (ptr == MAP_FAILED) return FALSE;
840 fd_cache[entry] = ptr;
843 /* store fd+1 so that 0 can be used as the unset value */
844 prev_fd = interlocked_xchg( &fd_cache[entry][idx].fd, fd + 1 ) - 1;
845 fd_cache[entry][idx].type = type;
846 fd_cache[entry][idx].access = access;
847 fd_cache[entry][idx].options = options;
848 if (prev_fd != -1) close( prev_fd );
849 return TRUE;
853 /***********************************************************************
854 * get_cached_fd
856 * Caller must hold fd_cache_section.
858 static inline int get_cached_fd( HANDLE handle, enum server_fd_type *type,
859 unsigned int *access, unsigned int *options )
861 unsigned int entry, idx = handle_to_index( handle, &entry );
862 int fd = -1;
864 if (entry < FD_CACHE_ENTRIES && fd_cache[entry])
866 fd = fd_cache[entry][idx].fd - 1;
867 if (type) *type = fd_cache[entry][idx].type;
868 if (access) *access = fd_cache[entry][idx].access;
869 if (options) *options = fd_cache[entry][idx].options;
871 return fd;
875 /***********************************************************************
876 * server_remove_fd_from_cache
878 int server_remove_fd_from_cache( HANDLE handle )
880 unsigned int entry, idx = handle_to_index( handle, &entry );
881 int fd = -1;
883 if (entry < FD_CACHE_ENTRIES && fd_cache[entry])
884 fd = interlocked_xchg( &fd_cache[entry][idx].fd, 0 ) - 1;
886 return fd;
890 /***********************************************************************
891 * server_get_unix_fd
893 * The returned unix_fd should be closed iff needs_close is non-zero.
895 int server_get_unix_fd( HANDLE handle, unsigned int wanted_access, int *unix_fd,
896 int *needs_close, enum server_fd_type *type, unsigned int *options )
898 sigset_t sigset;
899 obj_handle_t fd_handle;
900 int ret = 0, fd;
901 unsigned int access = 0;
903 *unix_fd = -1;
904 *needs_close = 0;
905 wanted_access &= FILE_READ_DATA | FILE_WRITE_DATA | FILE_APPEND_DATA;
907 server_enter_uninterrupted_section( &fd_cache_section, &sigset );
909 fd = get_cached_fd( handle, type, &access, options );
910 if (fd != -1) goto done;
912 SERVER_START_REQ( get_handle_fd )
914 req->handle = wine_server_obj_handle( handle );
915 if (!(ret = wine_server_call( req )))
917 if (type) *type = reply->type;
918 if (options) *options = reply->options;
919 access = reply->access;
920 if ((fd = receive_fd( &fd_handle )) != -1)
922 assert( wine_server_ptr_handle(fd_handle) == handle );
923 *needs_close = (!reply->cacheable ||
924 !add_fd_to_cache( handle, fd, reply->type,
925 reply->access, reply->options ));
927 else ret = STATUS_TOO_MANY_OPENED_FILES;
930 SERVER_END_REQ;
932 done:
933 server_leave_uninterrupted_section( &fd_cache_section, &sigset );
934 if (!ret && ((access & wanted_access) != wanted_access))
936 ret = STATUS_ACCESS_DENIED;
937 if (*needs_close) close( fd );
939 if (!ret) *unix_fd = fd;
940 return ret;
944 /***********************************************************************
945 * wine_server_fd_to_handle (NTDLL.@)
947 * Allocate a file handle for a Unix file descriptor.
949 * PARAMS
950 * fd [I] Unix file descriptor.
951 * access [I] Win32 access flags.
952 * attributes [I] Object attributes.
953 * handle [O] Address where Wine file handle will be stored.
955 * RETURNS
956 * NTSTATUS code
958 int CDECL wine_server_fd_to_handle( int fd, unsigned int access, unsigned int attributes, HANDLE *handle )
960 int ret;
962 *handle = 0;
963 wine_server_send_fd( fd );
965 SERVER_START_REQ( alloc_file_handle )
967 req->access = access;
968 req->attributes = attributes;
969 req->fd = fd;
970 if (!(ret = wine_server_call( req ))) *handle = wine_server_ptr_handle( reply->handle );
972 SERVER_END_REQ;
973 return ret;
977 /***********************************************************************
978 * wine_server_handle_to_fd (NTDLL.@)
980 * Retrieve the file descriptor corresponding to a file handle.
982 * PARAMS
983 * handle [I] Wine file handle.
984 * access [I] Win32 file access rights requested.
985 * unix_fd [O] Address where Unix file descriptor will be stored.
986 * options [O] Address where the file open options will be stored. Optional.
988 * RETURNS
989 * NTSTATUS code
991 int CDECL wine_server_handle_to_fd( HANDLE handle, unsigned int access, int *unix_fd,
992 unsigned int *options )
994 int needs_close, ret = server_get_unix_fd( handle, access, unix_fd, &needs_close, NULL, options );
996 if (!ret && !needs_close)
998 if ((*unix_fd = dup(*unix_fd)) == -1) ret = FILE_GetNtStatus();
1000 return ret;
1004 /***********************************************************************
1005 * wine_server_release_fd (NTDLL.@)
1007 * Release the Unix file descriptor returned by wine_server_handle_to_fd.
1009 * PARAMS
1010 * handle [I] Wine file handle.
1011 * unix_fd [I] Unix file descriptor to release.
1013 * RETURNS
1014 * nothing
1016 void CDECL wine_server_release_fd( HANDLE handle, int unix_fd )
1018 close( unix_fd );
1022 /***********************************************************************
1023 * server_pipe
1025 * Create a pipe for communicating with the server.
1027 int server_pipe( int fd[2] )
1029 int ret;
1030 #ifdef HAVE_PIPE2
1031 static BOOL have_pipe2 = TRUE;
1033 if (have_pipe2)
1035 if (!(ret = pipe2( fd, O_CLOEXEC ))) return ret;
1036 if (errno == ENOSYS || errno == EINVAL) have_pipe2 = FALSE; /* don't try again */
1038 #endif
1039 if (!(ret = pipe( fd )))
1041 fcntl( fd[0], F_SETFD, FD_CLOEXEC );
1042 fcntl( fd[1], F_SETFD, FD_CLOEXEC );
1044 return ret;
1048 /***********************************************************************
1049 * start_server
1051 * Start a new wine server.
1053 static void start_server(void)
1055 static BOOL started; /* we only try once */
1056 char *argv[3];
1057 static char wineserver[] = "server/wineserver";
1058 static char debug[] = "-d";
1060 if (!started)
1062 int status;
1063 int pid = fork();
1064 if (pid == -1) fatal_perror( "fork" );
1065 if (!pid)
1067 argv[0] = wineserver;
1068 argv[1] = TRACE_ON(server) ? debug : NULL;
1069 argv[2] = NULL;
1070 wine_exec_wine_binary( argv[0], argv, getenv("WINESERVER") );
1071 fatal_error( "could not exec wineserver\n" );
1073 waitpid( pid, &status, 0 );
1074 status = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1075 if (status == 2) return; /* server lock held by someone else, will retry later */
1076 if (status) exit(status); /* server failed */
1077 started = TRUE;
1082 /***********************************************************************
1083 * setup_config_dir
1085 * Setup the wine configuration dir.
1087 static void setup_config_dir(void)
1089 const char *p, *config_dir = wine_get_config_dir();
1091 if (chdir( config_dir ) == -1)
1093 if (errno != ENOENT) fatal_perror( "chdir to %s\n", config_dir );
1095 if ((p = strrchr( config_dir, '/' )) && p != config_dir)
1097 struct stat st;
1098 char *tmp_dir;
1100 if (!(tmp_dir = malloc( p + 1 - config_dir ))) fatal_error( "out of memory\n" );
1101 memcpy( tmp_dir, config_dir, p - config_dir );
1102 tmp_dir[p - config_dir] = 0;
1103 if (!stat( tmp_dir, &st ) && st.st_uid != getuid())
1104 fatal_error( "'%s' is not owned by you, refusing to create a configuration directory there\n",
1105 tmp_dir );
1106 free( tmp_dir );
1109 mkdir( config_dir, 0777 );
1110 if (chdir( config_dir ) == -1) fatal_perror( "chdir to %s\n", config_dir );
1112 if ((p = getenv( "WINEARCH" )) && !strcmp( p, "win32" ))
1114 /* force creation of a 32-bit prefix */
1115 int fd = open( "system.reg", O_WRONLY | O_CREAT | O_EXCL, 0666 );
1116 if (fd != -1)
1118 static const char regfile[] = "WINE REGISTRY Version 2\n\n#arch=win32\n";
1119 write( fd, regfile, sizeof(regfile) - 1 );
1120 close( fd );
1123 MESSAGE( "wine: created the configuration directory '%s'\n", config_dir );
1126 if (mkdir( "dosdevices", 0777 ) == -1)
1128 if (errno == EEXIST) return;
1129 fatal_perror( "cannot create %s/dosdevices\n", config_dir );
1132 /* create the drive symlinks */
1134 mkdir( "drive_c", 0777 );
1135 symlink( "../drive_c", "dosdevices/c:" );
1136 symlink( "/", "dosdevices/z:" );
1140 /***********************************************************************
1141 * server_connect_error
1143 * Try to display a meaningful explanation of why we couldn't connect
1144 * to the server.
1146 static void server_connect_error( const char *serverdir )
1148 int fd;
1149 struct flock fl;
1151 if ((fd = open( LOCKNAME, O_WRONLY )) == -1)
1152 fatal_error( "for some mysterious reason, the wine server never started.\n" );
1154 fl.l_type = F_WRLCK;
1155 fl.l_whence = SEEK_SET;
1156 fl.l_start = 0;
1157 fl.l_len = 1;
1158 if (fcntl( fd, F_GETLK, &fl ) != -1)
1160 if (fl.l_type == F_WRLCK) /* the file is locked */
1161 fatal_error( "a wine server seems to be running, but I cannot connect to it.\n"
1162 " You probably need to kill that process (it might be pid %d).\n",
1163 (int)fl.l_pid );
1164 fatal_error( "for some mysterious reason, the wine server failed to run.\n" );
1166 fatal_error( "the file system of '%s' doesn't support locks,\n"
1167 " and there is a 'socket' file in that directory that prevents wine from starting.\n"
1168 " You should make sure no wine server is running, remove that file and try again.\n",
1169 serverdir );
1173 /***********************************************************************
1174 * server_connect
1176 * Attempt to connect to an existing server socket.
1177 * We need to be in the server directory already.
1179 static int server_connect(void)
1181 const char *serverdir;
1182 struct sockaddr_un addr;
1183 struct stat st;
1184 int s, slen, retry, fd_cwd;
1186 /* retrieve the current directory */
1187 fd_cwd = open( ".", O_RDONLY );
1188 if (fd_cwd != -1) fcntl( fd_cwd, F_SETFD, 1 ); /* set close on exec flag */
1190 setup_config_dir();
1191 serverdir = wine_get_server_dir();
1193 /* chdir to the server directory */
1194 if (chdir( serverdir ) == -1)
1196 if (errno != ENOENT) fatal_perror( "chdir to %s", serverdir );
1197 start_server();
1198 if (chdir( serverdir ) == -1) fatal_perror( "chdir to %s", serverdir );
1201 /* make sure we are at the right place */
1202 if (stat( ".", &st ) == -1) fatal_perror( "stat %s", serverdir );
1203 if (st.st_uid != getuid()) fatal_error( "'%s' is not owned by you\n", serverdir );
1204 if (st.st_mode & 077) fatal_error( "'%s' must not be accessible by other users\n", serverdir );
1206 for (retry = 0; retry < 6; retry++)
1208 /* if not the first try, wait a bit to leave the previous server time to exit */
1209 if (retry)
1211 usleep( 100000 * retry * retry );
1212 start_server();
1213 if (lstat( SOCKETNAME, &st ) == -1) continue; /* still no socket, wait a bit more */
1215 else if (lstat( SOCKETNAME, &st ) == -1) /* check for an already existing socket */
1217 if (errno != ENOENT) fatal_perror( "lstat %s/%s", serverdir, SOCKETNAME );
1218 start_server();
1219 if (lstat( SOCKETNAME, &st ) == -1) continue; /* still no socket, wait a bit more */
1222 /* make sure the socket is sane (ISFIFO needed for Solaris) */
1223 if (!S_ISSOCK(st.st_mode) && !S_ISFIFO(st.st_mode))
1224 fatal_error( "'%s/%s' is not a socket\n", serverdir, SOCKETNAME );
1225 if (st.st_uid != getuid())
1226 fatal_error( "'%s/%s' is not owned by you\n", serverdir, SOCKETNAME );
1228 /* try to connect to it */
1229 addr.sun_family = AF_UNIX;
1230 strcpy( addr.sun_path, SOCKETNAME );
1231 slen = sizeof(addr) - sizeof(addr.sun_path) + strlen(addr.sun_path) + 1;
1232 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
1233 addr.sun_len = slen;
1234 #endif
1235 if ((s = socket( AF_UNIX, SOCK_STREAM, 0 )) == -1) fatal_perror( "socket" );
1236 #ifdef SO_PASSCRED
1237 else
1239 int enable = 1;
1240 setsockopt( s, SOL_SOCKET, SO_PASSCRED, &enable, sizeof(enable) );
1242 #endif
1243 if (connect( s, (struct sockaddr *)&addr, slen ) != -1)
1245 /* switch back to the starting directory */
1246 if (fd_cwd != -1)
1248 fchdir( fd_cwd );
1249 close( fd_cwd );
1251 fcntl( s, F_SETFD, 1 ); /* set close on exec flag */
1252 return s;
1254 close( s );
1256 server_connect_error( serverdir );
1260 #ifdef __APPLE__
1261 #include <mach/mach.h>
1262 #include <mach/mach_error.h>
1263 #include <servers/bootstrap.h>
1265 /* send our task port to the server */
1266 static void send_server_task_port(void)
1268 mach_port_t bootstrap_port, wineserver_port;
1269 kern_return_t kret;
1271 struct {
1272 mach_msg_header_t header;
1273 mach_msg_body_t body;
1274 mach_msg_port_descriptor_t task_port;
1275 } msg;
1277 if (task_get_bootstrap_port(mach_task_self(), &bootstrap_port) != KERN_SUCCESS) return;
1279 kret = bootstrap_look_up(bootstrap_port, (char*)wine_get_server_dir(), &wineserver_port);
1280 if (kret != KERN_SUCCESS)
1281 fatal_error( "cannot find the server port: 0x%08x\n", kret );
1283 mach_port_deallocate(mach_task_self(), bootstrap_port);
1285 msg.header.msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, 0) | MACH_MSGH_BITS_COMPLEX;
1286 msg.header.msgh_size = sizeof(msg);
1287 msg.header.msgh_remote_port = wineserver_port;
1288 msg.header.msgh_local_port = MACH_PORT_NULL;
1290 msg.body.msgh_descriptor_count = 1;
1291 msg.task_port.name = mach_task_self();
1292 msg.task_port.disposition = MACH_MSG_TYPE_COPY_SEND;
1293 msg.task_port.type = MACH_MSG_PORT_DESCRIPTOR;
1295 kret = mach_msg_send(&msg.header);
1296 if (kret != KERN_SUCCESS)
1297 server_protocol_error( "mach_msg_send failed: 0x%08x\n", kret );
1299 mach_port_deallocate(mach_task_self(), wineserver_port);
1301 #endif /* __APPLE__ */
1304 /***********************************************************************
1305 * get_unix_tid
1307 * Retrieve the Unix tid to use on the server side for the current thread.
1309 static int get_unix_tid(void)
1311 int ret = -1;
1312 #ifdef HAVE_PTHREAD_GETTHREADID_NP
1313 ret = pthread_getthreadid_np();
1314 #elif defined(linux)
1315 ret = syscall( __NR_gettid );
1316 #elif defined(__sun)
1317 ret = pthread_self();
1318 #elif defined(__APPLE__)
1319 ret = mach_thread_self();
1320 mach_port_deallocate(mach_task_self(), ret);
1321 #elif defined(__NetBSD__)
1322 ret = _lwp_self();
1323 #elif defined(__FreeBSD__)
1324 long lwpid;
1325 thr_self( &lwpid );
1326 ret = lwpid;
1327 #elif defined(__DragonFly__)
1328 ret = lwp_gettid();
1329 #endif
1330 return ret;
1334 /***********************************************************************
1335 * server_init_process
1337 * Start the server and create the initial socket pair.
1339 void server_init_process(void)
1341 obj_handle_t version;
1342 const char *env_socket = getenv( "WINESERVERSOCKET" );
1344 server_pid = -1;
1345 if (env_socket)
1347 fd_socket = atoi( env_socket );
1348 if (fcntl( fd_socket, F_SETFD, 1 ) == -1)
1349 fatal_perror( "Bad server socket %d", fd_socket );
1350 unsetenv( "WINESERVERSOCKET" );
1352 else fd_socket = server_connect();
1354 /* setup the signal mask */
1355 sigemptyset( &server_block_set );
1356 sigaddset( &server_block_set, SIGALRM );
1357 sigaddset( &server_block_set, SIGIO );
1358 sigaddset( &server_block_set, SIGINT );
1359 sigaddset( &server_block_set, SIGHUP );
1360 sigaddset( &server_block_set, SIGUSR1 );
1361 sigaddset( &server_block_set, SIGUSR2 );
1362 sigaddset( &server_block_set, SIGCHLD );
1363 pthread_sigmask( SIG_BLOCK, &server_block_set, NULL );
1365 /* receive the first thread request fd on the main socket */
1366 ntdll_get_thread_data()->request_fd = receive_fd( &version );
1368 #ifdef SO_PASSCRED
1369 /* now that we hopefully received the server_pid, disable SO_PASSCRED */
1371 int enable = 0;
1372 setsockopt( fd_socket, SOL_SOCKET, SO_PASSCRED, &enable, sizeof(enable) );
1374 #endif
1376 if (version != SERVER_PROTOCOL_VERSION)
1377 server_protocol_error( "version mismatch %d/%d.\n"
1378 "Your %s binary was not upgraded correctly,\n"
1379 "or you have an older one somewhere in your PATH.\n"
1380 "Or maybe the wrong wineserver is still running?\n",
1381 version, SERVER_PROTOCOL_VERSION,
1382 (version > SERVER_PROTOCOL_VERSION) ? "wine" : "wineserver" );
1383 #ifdef __APPLE__
1384 send_server_task_port();
1385 #endif
1386 #if defined(__linux__) && defined(HAVE_PRCTL)
1387 /* work around Ubuntu's ptrace breakage */
1388 if (server_pid != -1) prctl( 0x59616d61 /* PR_SET_PTRACER */, server_pid );
1389 #endif
1393 /***********************************************************************
1394 * server_init_process_done
1396 NTSTATUS server_init_process_done(void)
1398 PEB *peb = NtCurrentTeb()->Peb;
1399 IMAGE_NT_HEADERS *nt = RtlImageNtHeader( peb->ImageBaseAddress );
1400 NTSTATUS status;
1402 /* Install signal handlers; this cannot be done earlier, since we cannot
1403 * send exceptions to the debugger before the create process event that
1404 * is sent by REQ_INIT_PROCESS_DONE.
1405 * We do need the handlers in place by the time the request is over, so
1406 * we set them up here. If we segfault between here and the server call
1407 * something is very wrong... */
1408 signal_init_process();
1410 /* Signal the parent process to continue */
1411 SERVER_START_REQ( init_process_done )
1413 req->module = wine_server_client_ptr( peb->ImageBaseAddress );
1414 #ifdef __i386__
1415 req->ldt_copy = wine_server_client_ptr( &wine_ldt_copy );
1416 #endif
1417 req->entry = wine_server_client_ptr( (char *)peb->ImageBaseAddress + nt->OptionalHeader.AddressOfEntryPoint );
1418 req->gui = (nt->OptionalHeader.Subsystem != IMAGE_SUBSYSTEM_WINDOWS_CUI);
1419 status = wine_server_call( req );
1421 SERVER_END_REQ;
1423 return status;
1427 /***********************************************************************
1428 * server_init_thread
1430 * Send an init thread request. Return 0 if OK.
1432 size_t server_init_thread( void *entry_point )
1434 static const char *cpu_names[] = { "x86", "x86_64", "PowerPC", "ARM", "ARM64" };
1435 static const BOOL is_win64 = (sizeof(void *) > sizeof(int));
1436 const char *arch = getenv( "WINEARCH" );
1437 int ret;
1438 int reply_pipe[2];
1439 struct sigaction sig_act;
1440 size_t info_size;
1442 sig_act.sa_handler = SIG_IGN;
1443 sig_act.sa_flags = 0;
1444 sigemptyset( &sig_act.sa_mask );
1446 /* ignore SIGPIPE so that we get an EPIPE error instead */
1447 sigaction( SIGPIPE, &sig_act, NULL );
1449 /* create the server->client communication pipes */
1450 if (server_pipe( reply_pipe ) == -1) server_protocol_perror( "pipe" );
1451 if (server_pipe( ntdll_get_thread_data()->wait_fd ) == -1) server_protocol_perror( "pipe" );
1452 wine_server_send_fd( reply_pipe[1] );
1453 wine_server_send_fd( ntdll_get_thread_data()->wait_fd[1] );
1454 ntdll_get_thread_data()->reply_fd = reply_pipe[0];
1455 close( reply_pipe[1] );
1457 SERVER_START_REQ( init_thread )
1459 req->unix_pid = getpid();
1460 req->unix_tid = get_unix_tid();
1461 req->teb = wine_server_client_ptr( NtCurrentTeb() );
1462 req->entry = wine_server_client_ptr( entry_point );
1463 req->reply_fd = reply_pipe[1];
1464 req->wait_fd = ntdll_get_thread_data()->wait_fd[1];
1465 req->debug_level = (TRACE_ON(server) != 0);
1466 req->cpu = client_cpu;
1467 ret = wine_server_call( req );
1468 NtCurrentTeb()->ClientId.UniqueProcess = ULongToHandle(reply->pid);
1469 NtCurrentTeb()->ClientId.UniqueThread = ULongToHandle(reply->tid);
1470 info_size = reply->info_size;
1471 server_start_time = reply->server_start;
1472 server_cpus = reply->all_cpus;
1474 SERVER_END_REQ;
1476 is_wow64 = !is_win64 && (server_cpus & (1 << CPU_x86_64)) != 0;
1477 ntdll_get_thread_data()->wow64_redir = is_wow64;
1479 switch (ret)
1481 case STATUS_SUCCESS:
1482 if (arch)
1484 if (!strcmp( arch, "win32" ) && (is_win64 || is_wow64))
1485 fatal_error( "WINEARCH set to win32 but '%s' is a 64-bit installation.\n",
1486 wine_get_config_dir() );
1487 if (!strcmp( arch, "win64" ) && !is_win64 && !is_wow64)
1488 fatal_error( "WINEARCH set to win64 but '%s' is a 32-bit installation.\n",
1489 wine_get_config_dir() );
1491 return info_size;
1492 case STATUS_INVALID_IMAGE_WIN_64:
1493 fatal_error( "'%s' is a 32-bit installation, it cannot support 64-bit applications.\n",
1494 wine_get_config_dir() );
1495 case STATUS_NOT_SUPPORTED:
1496 fatal_error( "'%s' is a 64-bit installation, it cannot be used with a 32-bit wineserver.\n",
1497 wine_get_config_dir() );
1498 case STATUS_INVALID_IMAGE_FORMAT:
1499 fatal_error( "wineserver doesn't support the %s architecture\n", cpu_names[client_cpu] );
1500 default:
1501 server_protocol_error( "init_thread failed with status %x\n", ret );