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
22 #include "wine/port.h"
35 #include <sys/types.h>
36 #ifdef HAVE_SYS_SOCKET_H
37 # include <sys/socket.h>
39 #ifdef HAVE_SYS_WAIT_H
45 #ifdef HAVE_SYS_MMAN_H
48 #ifdef HAVE_SYS_STAT_H
49 # include <sys/stat.h>
55 #include <sys/ucontext.h>
63 #define WIN32_NO_STATUS
64 #include "wine/library.h"
65 #include "wine/server.h"
66 #include "wine/debug.h"
67 #include "ntdll_misc.h"
69 WINE_DEFAULT_DEBUG_CHANNEL(server
);
71 /* Some versions of glibc don't define this */
76 #define SOCKETNAME "socket" /* name of the socket file */
77 #define LOCKNAME "lock" /* name of the lock file */
80 static const enum cpu_type client_cpu
= CPU_x86
;
81 #elif defined(__x86_64__)
82 static const enum cpu_type client_cpu
= CPU_x86_64
;
83 #elif defined(__ALPHA__)
84 static const enum cpu_type client_cpu
= CPU_ALPHA
;
85 #elif defined(__powerpc__)
86 static const enum cpu_type client_cpu
= CPU_POWERPC
;
87 #elif defined(__sparc__)
88 static const enum cpu_type client_cpu
= CPU_SPARC
;
90 #error Unsupported CPU
93 #ifndef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
94 /* data structure used to pass an fd with sendmsg/recvmsg */
99 size_t len
; /* size of structure */
100 int level
; /* SOL_SOCKET */
101 int type
; /* SCM_RIGHTS */
103 int fd
; /* fd to pass */
105 #endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
107 timeout_t server_start_time
= 0; /* time of server startup */
109 sigset_t server_block_set
; /* signals to block during server calls */
110 static int fd_socket
= -1; /* socket to exchange file descriptors with the server */
112 static RTL_CRITICAL_SECTION fd_cache_section
;
113 static RTL_CRITICAL_SECTION_DEBUG critsect_debug
=
115 0, 0, &fd_cache_section
,
116 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
117 0, 0, { (DWORD_PTR
)(__FILE__
": fd_cache_section") }
119 static RTL_CRITICAL_SECTION fd_cache_section
= { &critsect_debug
, -1, 0, 0, 0, 0 };
123 static void fatal_error( const char *err
, ... ) __attribute__((noreturn
, format(printf
,1,2)));
124 static void fatal_perror( const char *err
, ... ) __attribute__((noreturn
, format(printf
,1,2)));
125 static void server_connect_error( const char *serverdir
) __attribute__((noreturn
));
128 /* die on a fatal error; use only during initialization */
129 static void fatal_error( const char *err
, ... )
133 va_start( args
, err
);
134 fprintf( stderr
, "wine: " );
135 vfprintf( stderr
, err
, args
);
140 /* die on a fatal error; use only during initialization */
141 static void fatal_perror( const char *err
, ... )
145 va_start( args
, err
);
146 fprintf( stderr
, "wine: " );
147 vfprintf( stderr
, err
, args
);
154 /***********************************************************************
155 * server_protocol_error
157 void server_protocol_error( const char *err
, ... )
161 va_start( args
, err
);
162 fprintf( stderr
, "wine client error:%x: ", GetCurrentThreadId() );
163 vfprintf( stderr
, err
, args
);
169 /***********************************************************************
170 * server_protocol_perror
172 void server_protocol_perror( const char *err
)
174 fprintf( stderr
, "wine client error:%x: ", GetCurrentThreadId() );
180 /***********************************************************************
183 * Send a request to the server.
185 static unsigned int send_request( const struct __server_request_info
*req
)
190 if (!req
->u
.req
.request_header
.request_size
)
192 if ((ret
= write( ntdll_get_thread_data()->request_fd
, &req
->u
.req
,
193 sizeof(req
->u
.req
) )) == sizeof(req
->u
.req
)) return STATUS_SUCCESS
;
198 struct iovec vec
[__SERVER_MAX_DATA
+1];
200 vec
[0].iov_base
= (void *)&req
->u
.req
;
201 vec
[0].iov_len
= sizeof(req
->u
.req
);
202 for (i
= 0; i
< req
->data_count
; i
++)
204 vec
[i
+1].iov_base
= (void *)req
->data
[i
].ptr
;
205 vec
[i
+1].iov_len
= req
->data
[i
].size
;
207 if ((ret
= writev( ntdll_get_thread_data()->request_fd
, vec
, i
+1 )) ==
208 req
->u
.req
.request_header
.request_size
+ sizeof(req
->u
.req
)) return STATUS_SUCCESS
;
211 if (ret
>= 0) server_protocol_error( "partial write %d\n", ret
);
212 if (errno
== EPIPE
) abort_thread(0);
213 if (errno
== EFAULT
) return STATUS_ACCESS_VIOLATION
;
214 server_protocol_perror( "write" );
218 /***********************************************************************
221 * Read data from the reply buffer; helper for wait_reply.
223 static void read_reply_data( void *buffer
, size_t size
)
229 if ((ret
= read( ntdll_get_thread_data()->reply_fd
, buffer
, size
)) > 0)
231 if (!(size
-= ret
)) return;
232 buffer
= (char *)buffer
+ ret
;
236 if (errno
== EINTR
) continue;
237 if (errno
== EPIPE
) break;
238 server_protocol_perror("read");
240 /* the server closed the connection; time to die... */
245 /***********************************************************************
248 * Wait for a reply from the server.
250 static inline unsigned int wait_reply( struct __server_request_info
*req
)
252 read_reply_data( &req
->u
.reply
, sizeof(req
->u
.reply
) );
253 if (req
->u
.reply
.reply_header
.reply_size
)
254 read_reply_data( req
->reply_data
, req
->u
.reply
.reply_header
.reply_size
);
255 return req
->u
.reply
.reply_header
.error
;
259 /***********************************************************************
260 * wine_server_call (NTDLL.@)
262 * Perform a server call.
265 * req_ptr [I/O] Function dependent data
268 * Depends on server function being called, but usually an NTSTATUS code.
271 * Use the SERVER_START_REQ and SERVER_END_REQ to help you fill out the
272 * server request structure for the particular call. E.g:
273 *| SERVER_START_REQ( event_op )
275 *| req->handle = handle;
276 *| req->op = SET_EVENT;
277 *| ret = wine_server_call( req );
281 unsigned int wine_server_call( void *req_ptr
)
283 struct __server_request_info
* const req
= req_ptr
;
287 pthread_sigmask( SIG_BLOCK
, &server_block_set
, &old_set
);
288 ret
= send_request( req
);
289 if (!ret
) ret
= wait_reply( req
);
290 pthread_sigmask( SIG_SETMASK
, &old_set
, NULL
);
295 /***********************************************************************
296 * server_enter_uninterrupted_section
298 void server_enter_uninterrupted_section( RTL_CRITICAL_SECTION
*cs
, sigset_t
*sigset
)
300 pthread_sigmask( SIG_BLOCK
, &server_block_set
, sigset
);
301 RtlEnterCriticalSection( cs
);
305 /***********************************************************************
306 * server_leave_uninterrupted_section
308 void server_leave_uninterrupted_section( RTL_CRITICAL_SECTION
*cs
, sigset_t
*sigset
)
310 RtlLeaveCriticalSection( cs
);
311 pthread_sigmask( SIG_SETMASK
, sigset
, NULL
);
315 /***********************************************************************
316 * wine_server_send_fd (NTDLL.@)
318 * Send a file descriptor to the server.
321 * fd [I] file descriptor to send
326 void CDECL
wine_server_send_fd( int fd
)
328 #ifndef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
332 struct msghdr msghdr
;
336 vec
.iov_base
= (void *)&data
;
337 vec
.iov_len
= sizeof(data
);
339 msghdr
.msg_name
= NULL
;
340 msghdr
.msg_namelen
= 0;
341 msghdr
.msg_iov
= &vec
;
342 msghdr
.msg_iovlen
= 1;
344 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
345 msghdr
.msg_accrights
= (void *)&fd
;
346 msghdr
.msg_accrightslen
= sizeof(fd
);
347 #else /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
348 cmsg
.header
.len
= sizeof(cmsg
.header
) + sizeof(fd
);
349 cmsg
.header
.level
= SOL_SOCKET
;
350 cmsg
.header
.type
= SCM_RIGHTS
;
352 msghdr
.msg_control
= &cmsg
;
353 msghdr
.msg_controllen
= sizeof(cmsg
.header
) + sizeof(fd
);
354 msghdr
.msg_flags
= 0;
355 #endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
357 data
.tid
= GetCurrentThreadId();
362 if ((ret
= sendmsg( fd_socket
, &msghdr
, 0 )) == sizeof(data
)) return;
363 if (ret
>= 0) server_protocol_error( "partial write %d\n", ret
);
364 if (errno
== EINTR
) continue;
365 if (errno
== EPIPE
) abort_thread(0);
366 server_protocol_perror( "sendmsg" );
371 /***********************************************************************
374 * Receive a file descriptor passed from the server.
376 static int receive_fd( obj_handle_t
*handle
)
381 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
382 struct msghdr msghdr
;
385 msghdr
.msg_accrights
= (void *)&fd
;
386 msghdr
.msg_accrightslen
= sizeof(fd
);
387 #else /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
388 struct msghdr msghdr
;
391 cmsg
.header
.len
= sizeof(cmsg
.header
) + sizeof(fd
);
392 cmsg
.header
.level
= SOL_SOCKET
;
393 cmsg
.header
.type
= SCM_RIGHTS
;
395 msghdr
.msg_control
= &cmsg
;
396 msghdr
.msg_controllen
= sizeof(cmsg
.header
) + sizeof(fd
);
397 msghdr
.msg_flags
= 0;
398 #endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
400 msghdr
.msg_name
= NULL
;
401 msghdr
.msg_namelen
= 0;
402 msghdr
.msg_iov
= &vec
;
403 msghdr
.msg_iovlen
= 1;
404 vec
.iov_base
= (void *)handle
;
405 vec
.iov_len
= sizeof(*handle
);
409 if ((ret
= recvmsg( fd_socket
, &msghdr
, 0 )) > 0)
411 #ifndef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
414 if (fd
!= -1) fcntl( fd
, F_SETFD
, 1 ); /* set close on exec flag */
418 if (errno
== EINTR
) continue;
419 if (errno
== EPIPE
) break;
420 server_protocol_perror("recvmsg");
422 /* the server closed the connection; time to die... */
427 /***********************************************************************/
428 /* fd cache support */
430 struct fd_cache_entry
433 enum server_fd_type type
: 6;
434 unsigned int access
: 2;
435 unsigned int options
: 24;
438 #define FD_CACHE_BLOCK_SIZE (65536 / sizeof(struct fd_cache_entry))
439 #define FD_CACHE_ENTRIES 128
441 static struct fd_cache_entry
*fd_cache
[FD_CACHE_ENTRIES
];
442 static struct fd_cache_entry fd_cache_initial_block
[FD_CACHE_BLOCK_SIZE
];
444 static inline unsigned int handle_to_index( HANDLE handle
, unsigned int *entry
)
446 unsigned int idx
= (wine_server_obj_handle(handle
) >> 2) - 1;
447 *entry
= idx
/ FD_CACHE_BLOCK_SIZE
;
448 return idx
% FD_CACHE_BLOCK_SIZE
;
452 /***********************************************************************
455 * Caller must hold fd_cache_section.
457 static int add_fd_to_cache( HANDLE handle
, int fd
, enum server_fd_type type
,
458 unsigned int access
, unsigned int options
)
460 unsigned int entry
, idx
= handle_to_index( handle
, &entry
);
463 if (entry
>= FD_CACHE_ENTRIES
)
465 FIXME( "too many allocated handles, not caching %p\n", handle
);
469 if (!fd_cache
[entry
]) /* do we need to allocate a new block of entries? */
471 if (!entry
) fd_cache
[0] = fd_cache_initial_block
;
474 void *ptr
= wine_anon_mmap( NULL
, FD_CACHE_BLOCK_SIZE
* sizeof(struct fd_cache_entry
),
475 PROT_READ
| PROT_WRITE
, 0 );
476 if (ptr
== MAP_FAILED
) return 0;
477 fd_cache
[entry
] = ptr
;
480 /* store fd+1 so that 0 can be used as the unset value */
481 prev_fd
= interlocked_xchg( &fd_cache
[entry
][idx
].fd
, fd
+ 1 ) - 1;
482 fd_cache
[entry
][idx
].type
= type
;
483 fd_cache
[entry
][idx
].access
= access
;
484 fd_cache
[entry
][idx
].options
= options
;
485 if (prev_fd
!= -1) close( prev_fd
);
490 /***********************************************************************
493 * Caller must hold fd_cache_section.
495 static inline int get_cached_fd( HANDLE handle
, enum server_fd_type
*type
,
496 unsigned int *access
, unsigned int *options
)
498 unsigned int entry
, idx
= handle_to_index( handle
, &entry
);
501 if (entry
< FD_CACHE_ENTRIES
&& fd_cache
[entry
])
503 fd
= fd_cache
[entry
][idx
].fd
- 1;
504 if (type
) *type
= fd_cache
[entry
][idx
].type
;
505 if (access
) *access
= fd_cache
[entry
][idx
].access
;
506 if (options
) *options
= fd_cache
[entry
][idx
].options
;
512 /***********************************************************************
513 * server_remove_fd_from_cache
515 int server_remove_fd_from_cache( HANDLE handle
)
517 unsigned int entry
, idx
= handle_to_index( handle
, &entry
);
520 if (entry
< FD_CACHE_ENTRIES
&& fd_cache
[entry
])
521 fd
= interlocked_xchg( &fd_cache
[entry
][idx
].fd
, 0 ) - 1;
527 /***********************************************************************
530 * The returned unix_fd should be closed iff needs_close is non-zero.
532 int server_get_unix_fd( HANDLE handle
, unsigned int wanted_access
, int *unix_fd
,
533 int *needs_close
, enum server_fd_type
*type
, unsigned int *options
)
536 obj_handle_t fd_handle
;
538 unsigned int access
= 0;
542 wanted_access
&= FILE_READ_DATA
| FILE_WRITE_DATA
;
544 server_enter_uninterrupted_section( &fd_cache_section
, &sigset
);
546 fd
= get_cached_fd( handle
, type
, &access
, options
);
547 if (fd
!= -1) goto done
;
549 SERVER_START_REQ( get_handle_fd
)
551 req
->handle
= wine_server_obj_handle( handle
);
552 if (!(ret
= wine_server_call( req
)))
554 if (type
) *type
= reply
->type
;
555 if (options
) *options
= reply
->options
;
556 access
= reply
->access
;
557 if ((fd
= receive_fd( &fd_handle
)) != -1)
559 assert( wine_server_ptr_handle(fd_handle
) == handle
);
560 *needs_close
= (reply
->removable
||
561 !add_fd_to_cache( handle
, fd
, reply
->type
,
562 reply
->access
, reply
->options
));
564 else ret
= STATUS_TOO_MANY_OPENED_FILES
;
570 server_leave_uninterrupted_section( &fd_cache_section
, &sigset
);
571 if (!ret
&& ((access
& wanted_access
) != wanted_access
))
573 ret
= STATUS_ACCESS_DENIED
;
574 if (*needs_close
) close( fd
);
576 if (!ret
) *unix_fd
= fd
;
581 /***********************************************************************
582 * wine_server_fd_to_handle (NTDLL.@)
584 * Allocate a file handle for a Unix file descriptor.
587 * fd [I] Unix file descriptor.
588 * access [I] Win32 access flags.
589 * attributes [I] Object attributes.
590 * handle [O] Address where Wine file handle will be stored.
595 int CDECL
wine_server_fd_to_handle( int fd
, unsigned int access
, unsigned int attributes
, HANDLE
*handle
)
600 wine_server_send_fd( fd
);
602 SERVER_START_REQ( alloc_file_handle
)
604 req
->access
= access
;
605 req
->attributes
= attributes
;
607 if (!(ret
= wine_server_call( req
))) *handle
= wine_server_ptr_handle( reply
->handle
);
614 /***********************************************************************
615 * wine_server_handle_to_fd (NTDLL.@)
617 * Retrieve the file descriptor corresponding to a file handle.
620 * handle [I] Wine file handle.
621 * access [I] Win32 file access rights requested.
622 * unix_fd [O] Address where Unix file descriptor will be stored.
623 * options [O] Address where the file open options will be stored. Optional.
628 int CDECL
wine_server_handle_to_fd( HANDLE handle
, unsigned int access
, int *unix_fd
,
629 unsigned int *options
)
631 int needs_close
, ret
= server_get_unix_fd( handle
, access
, unix_fd
, &needs_close
, NULL
, options
);
633 if (!ret
&& !needs_close
)
635 if ((*unix_fd
= dup(*unix_fd
)) == -1) ret
= FILE_GetNtStatus();
641 /***********************************************************************
642 * wine_server_release_fd (NTDLL.@)
644 * Release the Unix file descriptor returned by wine_server_handle_to_fd.
647 * handle [I] Wine file handle.
648 * unix_fd [I] Unix file descriptor to release.
653 void CDECL
wine_server_release_fd( HANDLE handle
, int unix_fd
)
659 /***********************************************************************
662 * Start a new wine server.
664 static void start_server(void)
666 static int started
; /* we only try once */
668 static char wineserver
[] = "server/wineserver";
669 static char debug
[] = "-d";
675 if (pid
== -1) fatal_perror( "fork" );
678 argv
[0] = wineserver
;
679 argv
[1] = TRACE_ON(server
) ? debug
: NULL
;
681 wine_exec_wine_binary( argv
[0], argv
, getenv("WINESERVER") );
682 fatal_error( "could not exec wineserver\n" );
684 waitpid( pid
, &status
, 0 );
685 status
= WIFEXITED(status
) ? WEXITSTATUS(status
) : 1;
686 if (status
== 2) return; /* server lock held by someone else, will retry later */
687 if (status
) exit(status
); /* server failed */
693 /***********************************************************************
696 * Setup the wine configuration dir.
698 static void setup_config_dir(void)
700 const char *p
, *config_dir
= wine_get_config_dir();
702 if (chdir( config_dir
) == -1)
704 if (errno
!= ENOENT
) fatal_perror( "chdir to %s\n", config_dir
);
706 if ((p
= strrchr( config_dir
, '/' )) && p
!= config_dir
)
711 if (!(tmp_dir
= malloc( p
+ 1 - config_dir
))) fatal_error( "out of memory\n" );
712 memcpy( tmp_dir
, config_dir
, p
- config_dir
);
713 tmp_dir
[p
- config_dir
] = 0;
714 if (!stat( tmp_dir
, &st
) && st
.st_uid
!= getuid())
715 fatal_error( "'%s' is not owned by you, refusing to create a configuration directory there\n",
720 mkdir( config_dir
, 0777 );
721 if (chdir( config_dir
) == -1) fatal_perror( "chdir to %s\n", config_dir
);
722 MESSAGE( "wine: created the configuration directory '%s'\n", config_dir
);
725 if (mkdir( "dosdevices", 0777 ) == -1)
727 if (errno
== EEXIST
) return;
728 fatal_perror( "cannot create %s/dosdevices\n", config_dir
);
731 /* create the drive symlinks */
733 mkdir( "drive_c", 0777 );
734 symlink( "../drive_c", "dosdevices/c:" );
735 symlink( "/", "dosdevices/z:" );
739 /***********************************************************************
740 * server_connect_error
742 * Try to display a meaningful explanation of why we couldn't connect
745 static void server_connect_error( const char *serverdir
)
750 if ((fd
= open( LOCKNAME
, O_WRONLY
)) == -1)
751 fatal_error( "for some mysterious reason, the wine server never started.\n" );
754 fl
.l_whence
= SEEK_SET
;
757 if (fcntl( fd
, F_GETLK
, &fl
) != -1)
759 if (fl
.l_type
== F_WRLCK
) /* the file is locked */
760 fatal_error( "a wine server seems to be running, but I cannot connect to it.\n"
761 " You probably need to kill that process (it might be pid %d).\n",
763 fatal_error( "for some mysterious reason, the wine server failed to run.\n" );
765 fatal_error( "the file system of '%s' doesn't support locks,\n"
766 " and there is a 'socket' file in that directory that prevents wine from starting.\n"
767 " You should make sure no wine server is running, remove that file and try again.\n",
772 /***********************************************************************
775 * Attempt to connect to an existing server socket.
776 * We need to be in the server directory already.
778 static int server_connect(void)
780 const char *serverdir
;
781 struct sockaddr_un addr
;
783 int s
, slen
, retry
, fd_cwd
;
785 /* retrieve the current directory */
786 fd_cwd
= open( ".", O_RDONLY
);
787 if (fd_cwd
!= -1) fcntl( fd_cwd
, F_SETFD
, 1 ); /* set close on exec flag */
790 serverdir
= wine_get_server_dir();
792 /* chdir to the server directory */
793 if (chdir( serverdir
) == -1)
795 if (errno
!= ENOENT
) fatal_perror( "chdir to %s", serverdir
);
797 if (chdir( serverdir
) == -1) fatal_perror( "chdir to %s", serverdir
);
800 /* make sure we are at the right place */
801 if (stat( ".", &st
) == -1) fatal_perror( "stat %s", serverdir
);
802 if (st
.st_uid
!= getuid()) fatal_error( "'%s' is not owned by you\n", serverdir
);
803 if (st
.st_mode
& 077) fatal_error( "'%s' must not be accessible by other users\n", serverdir
);
805 for (retry
= 0; retry
< 6; retry
++)
807 /* if not the first try, wait a bit to leave the previous server time to exit */
810 usleep( 100000 * retry
* retry
);
812 if (lstat( SOCKETNAME
, &st
) == -1) continue; /* still no socket, wait a bit more */
814 else if (lstat( SOCKETNAME
, &st
) == -1) /* check for an already existing socket */
816 if (errno
!= ENOENT
) fatal_perror( "lstat %s/%s", serverdir
, SOCKETNAME
);
818 if (lstat( SOCKETNAME
, &st
) == -1) continue; /* still no socket, wait a bit more */
821 /* make sure the socket is sane (ISFIFO needed for Solaris) */
822 if (!S_ISSOCK(st
.st_mode
) && !S_ISFIFO(st
.st_mode
))
823 fatal_error( "'%s/%s' is not a socket\n", serverdir
, SOCKETNAME
);
824 if (st
.st_uid
!= getuid())
825 fatal_error( "'%s/%s' is not owned by you\n", serverdir
, SOCKETNAME
);
827 /* try to connect to it */
828 addr
.sun_family
= AF_UNIX
;
829 strcpy( addr
.sun_path
, SOCKETNAME
);
830 slen
= sizeof(addr
) - sizeof(addr
.sun_path
) + strlen(addr
.sun_path
) + 1;
831 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
834 if ((s
= socket( AF_UNIX
, SOCK_STREAM
, 0 )) == -1) fatal_perror( "socket" );
835 if (connect( s
, (struct sockaddr
*)&addr
, slen
) != -1)
837 /* switch back to the starting directory */
843 fcntl( s
, F_SETFD
, 1 ); /* set close on exec flag */
848 server_connect_error( serverdir
);
853 #include <mach/mach.h>
854 #include <mach/mach_error.h>
855 #include <servers/bootstrap.h>
857 /* send our task port to the server */
858 static void send_server_task_port(void)
860 mach_port_t bootstrap_port
, wineserver_port
;
864 mach_msg_header_t header
;
865 mach_msg_body_t body
;
866 mach_msg_port_descriptor_t task_port
;
869 if (task_get_bootstrap_port(mach_task_self(), &bootstrap_port
) != KERN_SUCCESS
) return;
871 kret
= bootstrap_look_up(bootstrap_port
, (char*)wine_get_server_dir(), &wineserver_port
);
872 if (kret
!= KERN_SUCCESS
)
873 fatal_error( "cannot find the server port: 0x%08x\n", kret
);
875 mach_port_deallocate(mach_task_self(), bootstrap_port
);
877 msg
.header
.msgh_bits
= MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND
, 0) | MACH_MSGH_BITS_COMPLEX
;
878 msg
.header
.msgh_size
= sizeof(msg
);
879 msg
.header
.msgh_remote_port
= wineserver_port
;
880 msg
.header
.msgh_local_port
= MACH_PORT_NULL
;
882 msg
.body
.msgh_descriptor_count
= 1;
883 msg
.task_port
.name
= mach_task_self();
884 msg
.task_port
.disposition
= MACH_MSG_TYPE_COPY_SEND
;
885 msg
.task_port
.type
= MACH_MSG_PORT_DESCRIPTOR
;
887 kret
= mach_msg_send(&msg
.header
);
888 if (kret
!= KERN_SUCCESS
)
889 server_protocol_error( "mach_msg_send failed: 0x%08x\n", kret
);
891 mach_port_deallocate(mach_task_self(), wineserver_port
);
893 #endif /* __APPLE__ */
896 /***********************************************************************
899 * Retrieve the Unix tid to use on the server side for the current thread.
901 static int get_unix_tid(void)
904 #if defined(linux) && defined(__i386__)
905 __asm__("int $0x80" : "=a" (ret
) : "0" (224) /* SYS_gettid */);
906 #elif defined(linux) && defined(__x86_64__)
907 __asm__("syscall" : "=a" (ret
) : "0" (186) /* SYS_gettid */);
909 ret
= pthread_self();
910 #elif defined(__APPLE__)
911 ret
= mach_thread_self();
912 #elif defined(__FreeBSD__)
921 /***********************************************************************
922 * server_init_process
924 * Start the server and create the initial socket pair.
926 void server_init_process(void)
928 obj_handle_t version
;
929 const char *env_socket
= getenv( "WINESERVERSOCKET" );
933 fd_socket
= atoi( env_socket
);
934 if (fcntl( fd_socket
, F_SETFD
, 1 ) == -1)
935 fatal_perror( "Bad server socket %d", fd_socket
);
936 unsetenv( "WINESERVERSOCKET" );
938 else fd_socket
= server_connect();
940 /* setup the signal mask */
941 sigemptyset( &server_block_set
);
942 sigaddset( &server_block_set
, SIGALRM
);
943 sigaddset( &server_block_set
, SIGIO
);
944 sigaddset( &server_block_set
, SIGINT
);
945 sigaddset( &server_block_set
, SIGHUP
);
946 sigaddset( &server_block_set
, SIGUSR1
);
947 sigaddset( &server_block_set
, SIGUSR2
);
948 sigaddset( &server_block_set
, SIGCHLD
);
949 pthread_sigmask( SIG_BLOCK
, &server_block_set
, NULL
);
951 /* receive the first thread request fd on the main socket */
952 ntdll_get_thread_data()->request_fd
= receive_fd( &version
);
954 if (version
!= SERVER_PROTOCOL_VERSION
)
955 server_protocol_error( "version mismatch %d/%d.\n"
956 "Your %s binary was not upgraded correctly,\n"
957 "or you have an older one somewhere in your PATH.\n"
958 "Or maybe the wrong wineserver is still running?\n",
959 version
, SERVER_PROTOCOL_VERSION
,
960 (version
> SERVER_PROTOCOL_VERSION
) ? "wine" : "wineserver" );
962 send_server_task_port();
967 /***********************************************************************
968 * server_init_process_done
970 NTSTATUS
server_init_process_done(void)
972 PEB
*peb
= NtCurrentTeb()->Peb
;
973 IMAGE_NT_HEADERS
*nt
= RtlImageNtHeader( peb
->ImageBaseAddress
);
976 /* Install signal handlers; this cannot be done earlier, since we cannot
977 * send exceptions to the debugger before the create process event that
978 * is sent by REQ_INIT_PROCESS_DONE.
979 * We do need the handlers in place by the time the request is over, so
980 * we set them up here. If we segfault between here and the server call
981 * something is very wrong... */
982 signal_init_process();
984 /* Signal the parent process to continue */
985 SERVER_START_REQ( init_process_done
)
987 req
->module
= wine_server_client_ptr( peb
->ImageBaseAddress
);
989 req
->ldt_copy
= wine_server_client_ptr( &wine_ldt_copy
);
991 req
->entry
= wine_server_client_ptr( (char *)peb
->ImageBaseAddress
+ nt
->OptionalHeader
.AddressOfEntryPoint
);
992 req
->gui
= (nt
->OptionalHeader
.Subsystem
!= IMAGE_SUBSYSTEM_WINDOWS_CUI
);
993 status
= wine_server_call( req
);
1001 /***********************************************************************
1002 * server_init_thread
1004 * Send an init thread request. Return 0 if OK.
1006 size_t server_init_thread( void *entry_point
)
1010 struct sigaction sig_act
;
1013 sig_act
.sa_handler
= SIG_IGN
;
1014 sig_act
.sa_flags
= 0;
1015 sigemptyset( &sig_act
.sa_mask
);
1017 /* ignore SIGPIPE so that we get an EPIPE error instead */
1018 sigaction( SIGPIPE
, &sig_act
, NULL
);
1019 /* automatic child reaping to avoid zombies */
1021 sig_act
.sa_flags
|= SA_NOCLDWAIT
;
1023 sigaction( SIGCHLD
, &sig_act
, NULL
);
1025 /* create the server->client communication pipes */
1026 if (pipe( reply_pipe
) == -1) server_protocol_perror( "pipe" );
1027 if (pipe( ntdll_get_thread_data()->wait_fd
) == -1) server_protocol_perror( "pipe" );
1028 wine_server_send_fd( reply_pipe
[1] );
1029 wine_server_send_fd( ntdll_get_thread_data()->wait_fd
[1] );
1030 ntdll_get_thread_data()->reply_fd
= reply_pipe
[0];
1031 close( reply_pipe
[1] );
1033 /* set close on exec flag */
1034 fcntl( ntdll_get_thread_data()->reply_fd
, F_SETFD
, 1 );
1035 fcntl( ntdll_get_thread_data()->wait_fd
[0], F_SETFD
, 1 );
1036 fcntl( ntdll_get_thread_data()->wait_fd
[1], F_SETFD
, 1 );
1038 SERVER_START_REQ( init_thread
)
1040 req
->unix_pid
= getpid();
1041 req
->unix_tid
= get_unix_tid();
1042 req
->teb
= wine_server_client_ptr( NtCurrentTeb() );
1043 req
->entry
= wine_server_client_ptr( entry_point
);
1044 req
->reply_fd
= reply_pipe
[1];
1045 req
->wait_fd
= ntdll_get_thread_data()->wait_fd
[1];
1046 req
->debug_level
= (TRACE_ON(server
) != 0);
1047 req
->cpu
= client_cpu
;
1048 ret
= wine_server_call( req
);
1049 NtCurrentTeb()->ClientId
.UniqueProcess
= ULongToHandle(reply
->pid
);
1050 NtCurrentTeb()->ClientId
.UniqueThread
= ULongToHandle(reply
->tid
);
1051 info_size
= reply
->info_size
;
1052 server_start_time
= reply
->server_start
;
1056 if (ret
) server_protocol_error( "init_thread failed with status %x\n", ret
);