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_PRCTL_H
49 # include <sys/prctl.h>
51 #ifdef HAVE_SYS_STAT_H
52 # include <sys/stat.h>
54 #ifdef HAVE_SYS_SYSCALL_H
55 # include <sys/syscall.h>
61 #include <sys/ucontext.h>
69 #define WIN32_NO_STATUS
70 #include "wine/library.h"
71 #include "wine/server.h"
72 #include "wine/debug.h"
73 #include "ntdll_misc.h"
75 WINE_DEFAULT_DEBUG_CHANNEL(server
);
77 /* Some versions of glibc don't define this */
82 #ifndef MSG_CMSG_CLOEXEC
83 #define MSG_CMSG_CLOEXEC 0
86 #define SOCKETNAME "socket" /* name of the socket file */
87 #define LOCKNAME "lock" /* name of the lock file */
90 static const enum cpu_type client_cpu
= CPU_x86
;
91 #elif defined(__x86_64__)
92 static const enum cpu_type client_cpu
= CPU_x86_64
;
93 #elif defined(__powerpc__)
94 static const enum cpu_type client_cpu
= CPU_POWERPC
;
95 #elif defined(__sparc__)
96 static const enum cpu_type client_cpu
= CPU_SPARC
;
97 #elif defined(__arm__)
98 static const enum cpu_type client_cpu
= CPU_ARM
;
100 #error Unsupported CPU
103 unsigned int server_cpus
= 0;
104 int is_wow64
= FALSE
;
106 timeout_t server_start_time
= 0; /* time of server startup */
108 sigset_t server_block_set
; /* signals to block during server calls */
109 static int fd_socket
= -1; /* socket to exchange file descriptors with the server */
110 static pid_t server_pid
;
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
)
329 struct msghdr msghdr
;
333 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
334 msghdr
.msg_accrights
= (void *)&fd
;
335 msghdr
.msg_accrightslen
= sizeof(fd
);
336 #else /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
337 char cmsg_buffer
[256];
338 struct cmsghdr
*cmsg
;
339 msghdr
.msg_control
= cmsg_buffer
;
340 msghdr
.msg_controllen
= sizeof(cmsg_buffer
);
341 msghdr
.msg_flags
= 0;
342 cmsg
= CMSG_FIRSTHDR( &msghdr
);
343 cmsg
->cmsg_len
= CMSG_LEN( sizeof(fd
) );
344 cmsg
->cmsg_level
= SOL_SOCKET
;
345 cmsg
->cmsg_type
= SCM_RIGHTS
;
346 *(int *)CMSG_DATA(cmsg
) = fd
;
347 msghdr
.msg_controllen
= cmsg
->cmsg_len
;
348 #endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
350 msghdr
.msg_name
= NULL
;
351 msghdr
.msg_namelen
= 0;
352 msghdr
.msg_iov
= &vec
;
353 msghdr
.msg_iovlen
= 1;
355 vec
.iov_base
= (void *)&data
;
356 vec
.iov_len
= sizeof(data
);
358 data
.tid
= GetCurrentThreadId();
363 if ((ret
= sendmsg( fd_socket
, &msghdr
, 0 )) == sizeof(data
)) return;
364 if (ret
>= 0) server_protocol_error( "partial write %d\n", ret
);
365 if (errno
== EINTR
) continue;
366 if (errno
== EPIPE
) abort_thread(0);
367 server_protocol_perror( "sendmsg" );
372 /***********************************************************************
375 * Receive a file descriptor passed from the server.
377 static int receive_fd( obj_handle_t
*handle
)
380 struct msghdr msghdr
;
383 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
384 msghdr
.msg_accrights
= (void *)&fd
;
385 msghdr
.msg_accrightslen
= sizeof(fd
);
386 #else /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
387 char cmsg_buffer
[256];
388 msghdr
.msg_control
= cmsg_buffer
;
389 msghdr
.msg_controllen
= sizeof(cmsg_buffer
);
390 msghdr
.msg_flags
= 0;
391 #endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
393 msghdr
.msg_name
= NULL
;
394 msghdr
.msg_namelen
= 0;
395 msghdr
.msg_iov
= &vec
;
396 msghdr
.msg_iovlen
= 1;
397 vec
.iov_base
= (void *)handle
;
398 vec
.iov_len
= sizeof(*handle
);
402 if ((ret
= recvmsg( fd_socket
, &msghdr
, MSG_CMSG_CLOEXEC
)) > 0)
404 #ifndef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
405 struct cmsghdr
*cmsg
;
406 for (cmsg
= CMSG_FIRSTHDR( &msghdr
); cmsg
; cmsg
= CMSG_NXTHDR( &msghdr
, cmsg
))
408 if (cmsg
->cmsg_level
!= SOL_SOCKET
) continue;
409 if (cmsg
->cmsg_type
== SCM_RIGHTS
) fd
= *(int *)CMSG_DATA(cmsg
);
410 #ifdef SCM_CREDENTIALS
411 else if (cmsg
->cmsg_type
== SCM_CREDENTIALS
)
413 struct ucred
*ucred
= (struct ucred
*)CMSG_DATA(cmsg
);
414 server_pid
= ucred
->pid
;
418 #endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
419 if (fd
!= -1) fcntl( fd
, F_SETFD
, FD_CLOEXEC
); /* in case MSG_CMSG_CLOEXEC is not supported */
423 if (errno
== EINTR
) continue;
424 if (errno
== EPIPE
) break;
425 server_protocol_perror("recvmsg");
427 /* the server closed the connection; time to die... */
432 /***********************************************************************/
433 /* fd cache support */
435 struct fd_cache_entry
438 enum server_fd_type type
: 6;
439 unsigned int access
: 2;
440 unsigned int options
: 24;
443 #define FD_CACHE_BLOCK_SIZE (65536 / sizeof(struct fd_cache_entry))
444 #define FD_CACHE_ENTRIES 128
446 static struct fd_cache_entry
*fd_cache
[FD_CACHE_ENTRIES
];
447 static struct fd_cache_entry fd_cache_initial_block
[FD_CACHE_BLOCK_SIZE
];
449 static inline unsigned int handle_to_index( HANDLE handle
, unsigned int *entry
)
451 unsigned int idx
= (wine_server_obj_handle(handle
) >> 2) - 1;
452 *entry
= idx
/ FD_CACHE_BLOCK_SIZE
;
453 return idx
% FD_CACHE_BLOCK_SIZE
;
457 /***********************************************************************
460 * Caller must hold fd_cache_section.
462 static int add_fd_to_cache( HANDLE handle
, int fd
, enum server_fd_type type
,
463 unsigned int access
, unsigned int options
)
465 unsigned int entry
, idx
= handle_to_index( handle
, &entry
);
468 if (entry
>= FD_CACHE_ENTRIES
)
470 FIXME( "too many allocated handles, not caching %p\n", handle
);
474 if (!fd_cache
[entry
]) /* do we need to allocate a new block of entries? */
476 if (!entry
) fd_cache
[0] = fd_cache_initial_block
;
479 void *ptr
= wine_anon_mmap( NULL
, FD_CACHE_BLOCK_SIZE
* sizeof(struct fd_cache_entry
),
480 PROT_READ
| PROT_WRITE
, 0 );
481 if (ptr
== MAP_FAILED
) return 0;
482 fd_cache
[entry
] = ptr
;
485 /* store fd+1 so that 0 can be used as the unset value */
486 prev_fd
= interlocked_xchg( &fd_cache
[entry
][idx
].fd
, fd
+ 1 ) - 1;
487 fd_cache
[entry
][idx
].type
= type
;
488 fd_cache
[entry
][idx
].access
= access
;
489 fd_cache
[entry
][idx
].options
= options
;
490 if (prev_fd
!= -1) close( prev_fd
);
495 /***********************************************************************
498 * Caller must hold fd_cache_section.
500 static inline int get_cached_fd( HANDLE handle
, enum server_fd_type
*type
,
501 unsigned int *access
, unsigned int *options
)
503 unsigned int entry
, idx
= handle_to_index( handle
, &entry
);
506 if (entry
< FD_CACHE_ENTRIES
&& fd_cache
[entry
])
508 fd
= fd_cache
[entry
][idx
].fd
- 1;
509 if (type
) *type
= fd_cache
[entry
][idx
].type
;
510 if (access
) *access
= fd_cache
[entry
][idx
].access
;
511 if (options
) *options
= fd_cache
[entry
][idx
].options
;
517 /***********************************************************************
518 * server_remove_fd_from_cache
520 int server_remove_fd_from_cache( HANDLE handle
)
522 unsigned int entry
, idx
= handle_to_index( handle
, &entry
);
525 if (entry
< FD_CACHE_ENTRIES
&& fd_cache
[entry
])
526 fd
= interlocked_xchg( &fd_cache
[entry
][idx
].fd
, 0 ) - 1;
532 /***********************************************************************
535 * The returned unix_fd should be closed iff needs_close is non-zero.
537 int server_get_unix_fd( HANDLE handle
, unsigned int wanted_access
, int *unix_fd
,
538 int *needs_close
, enum server_fd_type
*type
, unsigned int *options
)
541 obj_handle_t fd_handle
;
543 unsigned int access
= 0;
547 wanted_access
&= FILE_READ_DATA
| FILE_WRITE_DATA
;
549 server_enter_uninterrupted_section( &fd_cache_section
, &sigset
);
551 fd
= get_cached_fd( handle
, type
, &access
, options
);
552 if (fd
!= -1) goto done
;
554 SERVER_START_REQ( get_handle_fd
)
556 req
->handle
= wine_server_obj_handle( handle
);
557 if (!(ret
= wine_server_call( req
)))
559 if (type
) *type
= reply
->type
;
560 if (options
) *options
= reply
->options
;
561 access
= reply
->access
;
562 if ((fd
= receive_fd( &fd_handle
)) != -1)
564 assert( wine_server_ptr_handle(fd_handle
) == handle
);
565 *needs_close
= (!reply
->cacheable
||
566 !add_fd_to_cache( handle
, fd
, reply
->type
,
567 reply
->access
, reply
->options
));
569 else ret
= STATUS_TOO_MANY_OPENED_FILES
;
575 server_leave_uninterrupted_section( &fd_cache_section
, &sigset
);
576 if (!ret
&& ((access
& wanted_access
) != wanted_access
))
578 ret
= STATUS_ACCESS_DENIED
;
579 if (*needs_close
) close( fd
);
581 if (!ret
) *unix_fd
= fd
;
586 /***********************************************************************
587 * wine_server_fd_to_handle (NTDLL.@)
589 * Allocate a file handle for a Unix file descriptor.
592 * fd [I] Unix file descriptor.
593 * access [I] Win32 access flags.
594 * attributes [I] Object attributes.
595 * handle [O] Address where Wine file handle will be stored.
600 int CDECL
wine_server_fd_to_handle( int fd
, unsigned int access
, unsigned int attributes
, HANDLE
*handle
)
605 wine_server_send_fd( fd
);
607 SERVER_START_REQ( alloc_file_handle
)
609 req
->access
= access
;
610 req
->attributes
= attributes
;
612 if (!(ret
= wine_server_call( req
))) *handle
= wine_server_ptr_handle( reply
->handle
);
619 /***********************************************************************
620 * wine_server_handle_to_fd (NTDLL.@)
622 * Retrieve the file descriptor corresponding to a file handle.
625 * handle [I] Wine file handle.
626 * access [I] Win32 file access rights requested.
627 * unix_fd [O] Address where Unix file descriptor will be stored.
628 * options [O] Address where the file open options will be stored. Optional.
633 int CDECL
wine_server_handle_to_fd( HANDLE handle
, unsigned int access
, int *unix_fd
,
634 unsigned int *options
)
636 int needs_close
, ret
= server_get_unix_fd( handle
, access
, unix_fd
, &needs_close
, NULL
, options
);
638 if (!ret
&& !needs_close
)
640 if ((*unix_fd
= dup(*unix_fd
)) == -1) ret
= FILE_GetNtStatus();
646 /***********************************************************************
647 * wine_server_release_fd (NTDLL.@)
649 * Release the Unix file descriptor returned by wine_server_handle_to_fd.
652 * handle [I] Wine file handle.
653 * unix_fd [I] Unix file descriptor to release.
658 void CDECL
wine_server_release_fd( HANDLE handle
, int unix_fd
)
664 /***********************************************************************
667 * Create a pipe for communicating with the server.
669 int server_pipe( int fd
[2] )
673 static int have_pipe2
= 1;
677 if (!(ret
= pipe2( fd
, O_CLOEXEC
))) return ret
;
678 if (errno
== ENOSYS
|| errno
== EINVAL
) have_pipe2
= 0; /* don't try again */
681 if (!(ret
= pipe( fd
)))
683 fcntl( fd
[0], F_SETFD
, FD_CLOEXEC
);
684 fcntl( fd
[1], F_SETFD
, FD_CLOEXEC
);
690 /***********************************************************************
693 * Start a new wine server.
695 static void start_server(void)
697 static int started
; /* we only try once */
699 static char wineserver
[] = "server/wineserver";
700 static char debug
[] = "-d";
706 if (pid
== -1) fatal_perror( "fork" );
709 argv
[0] = wineserver
;
710 argv
[1] = TRACE_ON(server
) ? debug
: NULL
;
712 wine_exec_wine_binary( argv
[0], argv
, getenv("WINESERVER") );
713 fatal_error( "could not exec wineserver\n" );
715 waitpid( pid
, &status
, 0 );
716 status
= WIFEXITED(status
) ? WEXITSTATUS(status
) : 1;
717 if (status
== 2) return; /* server lock held by someone else, will retry later */
718 if (status
) exit(status
); /* server failed */
724 /***********************************************************************
727 * Setup the wine configuration dir.
729 static void setup_config_dir(void)
731 const char *p
, *config_dir
= wine_get_config_dir();
733 if (chdir( config_dir
) == -1)
735 if (errno
!= ENOENT
) fatal_perror( "chdir to %s\n", config_dir
);
737 if ((p
= strrchr( config_dir
, '/' )) && p
!= config_dir
)
742 if (!(tmp_dir
= malloc( p
+ 1 - config_dir
))) fatal_error( "out of memory\n" );
743 memcpy( tmp_dir
, config_dir
, p
- config_dir
);
744 tmp_dir
[p
- config_dir
] = 0;
745 if (!stat( tmp_dir
, &st
) && st
.st_uid
!= getuid())
746 fatal_error( "'%s' is not owned by you, refusing to create a configuration directory there\n",
751 mkdir( config_dir
, 0777 );
752 if (chdir( config_dir
) == -1) fatal_perror( "chdir to %s\n", config_dir
);
754 if ((p
= getenv( "WINEARCH" )) && !strcmp( p
, "win32" ))
756 /* force creation of a 32-bit prefix */
757 int fd
= open( "system.reg", O_WRONLY
| O_CREAT
| O_EXCL
, 0666 );
760 static const char regfile
[] = "WINE REGISTRY Version 2\n\n#arch=win32\n";
761 write( fd
, regfile
, sizeof(regfile
) - 1 );
765 MESSAGE( "wine: created the configuration directory '%s'\n", config_dir
);
768 if (mkdir( "dosdevices", 0777 ) == -1)
770 if (errno
== EEXIST
) return;
771 fatal_perror( "cannot create %s/dosdevices\n", config_dir
);
774 /* create the drive symlinks */
776 mkdir( "drive_c", 0777 );
777 symlink( "../drive_c", "dosdevices/c:" );
778 symlink( "/", "dosdevices/z:" );
782 /***********************************************************************
783 * server_connect_error
785 * Try to display a meaningful explanation of why we couldn't connect
788 static void server_connect_error( const char *serverdir
)
793 if ((fd
= open( LOCKNAME
, O_WRONLY
)) == -1)
794 fatal_error( "for some mysterious reason, the wine server never started.\n" );
797 fl
.l_whence
= SEEK_SET
;
800 if (fcntl( fd
, F_GETLK
, &fl
) != -1)
802 if (fl
.l_type
== F_WRLCK
) /* the file is locked */
803 fatal_error( "a wine server seems to be running, but I cannot connect to it.\n"
804 " You probably need to kill that process (it might be pid %d).\n",
806 fatal_error( "for some mysterious reason, the wine server failed to run.\n" );
808 fatal_error( "the file system of '%s' doesn't support locks,\n"
809 " and there is a 'socket' file in that directory that prevents wine from starting.\n"
810 " You should make sure no wine server is running, remove that file and try again.\n",
815 /***********************************************************************
818 * Attempt to connect to an existing server socket.
819 * We need to be in the server directory already.
821 static int server_connect(void)
823 const char *serverdir
;
824 struct sockaddr_un addr
;
826 int s
, slen
, retry
, fd_cwd
;
828 /* retrieve the current directory */
829 fd_cwd
= open( ".", O_RDONLY
);
830 if (fd_cwd
!= -1) fcntl( fd_cwd
, F_SETFD
, 1 ); /* set close on exec flag */
833 serverdir
= wine_get_server_dir();
835 /* chdir to the server directory */
836 if (chdir( serverdir
) == -1)
838 if (errno
!= ENOENT
) fatal_perror( "chdir to %s", serverdir
);
840 if (chdir( serverdir
) == -1) fatal_perror( "chdir to %s", serverdir
);
843 /* make sure we are at the right place */
844 if (stat( ".", &st
) == -1) fatal_perror( "stat %s", serverdir
);
845 if (st
.st_uid
!= getuid()) fatal_error( "'%s' is not owned by you\n", serverdir
);
846 if (st
.st_mode
& 077) fatal_error( "'%s' must not be accessible by other users\n", serverdir
);
848 for (retry
= 0; retry
< 6; retry
++)
850 /* if not the first try, wait a bit to leave the previous server time to exit */
853 usleep( 100000 * retry
* retry
);
855 if (lstat( SOCKETNAME
, &st
) == -1) continue; /* still no socket, wait a bit more */
857 else if (lstat( SOCKETNAME
, &st
) == -1) /* check for an already existing socket */
859 if (errno
!= ENOENT
) fatal_perror( "lstat %s/%s", serverdir
, SOCKETNAME
);
861 if (lstat( SOCKETNAME
, &st
) == -1) continue; /* still no socket, wait a bit more */
864 /* make sure the socket is sane (ISFIFO needed for Solaris) */
865 if (!S_ISSOCK(st
.st_mode
) && !S_ISFIFO(st
.st_mode
))
866 fatal_error( "'%s/%s' is not a socket\n", serverdir
, SOCKETNAME
);
867 if (st
.st_uid
!= getuid())
868 fatal_error( "'%s/%s' is not owned by you\n", serverdir
, SOCKETNAME
);
870 /* try to connect to it */
871 addr
.sun_family
= AF_UNIX
;
872 strcpy( addr
.sun_path
, SOCKETNAME
);
873 slen
= sizeof(addr
) - sizeof(addr
.sun_path
) + strlen(addr
.sun_path
) + 1;
874 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
877 if ((s
= socket( AF_UNIX
, SOCK_STREAM
, 0 )) == -1) fatal_perror( "socket" );
878 if (connect( s
, (struct sockaddr
*)&addr
, slen
) != -1)
880 /* switch back to the starting directory */
886 fcntl( s
, F_SETFD
, 1 ); /* set close on exec flag */
891 server_connect_error( serverdir
);
896 #include <mach/mach.h>
897 #include <mach/mach_error.h>
898 #include <servers/bootstrap.h>
900 /* send our task port to the server */
901 static void send_server_task_port(void)
903 mach_port_t bootstrap_port
, wineserver_port
;
907 mach_msg_header_t header
;
908 mach_msg_body_t body
;
909 mach_msg_port_descriptor_t task_port
;
912 if (task_get_bootstrap_port(mach_task_self(), &bootstrap_port
) != KERN_SUCCESS
) return;
914 kret
= bootstrap_look_up(bootstrap_port
, (char*)wine_get_server_dir(), &wineserver_port
);
915 if (kret
!= KERN_SUCCESS
)
916 fatal_error( "cannot find the server port: 0x%08x\n", kret
);
918 mach_port_deallocate(mach_task_self(), bootstrap_port
);
920 msg
.header
.msgh_bits
= MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND
, 0) | MACH_MSGH_BITS_COMPLEX
;
921 msg
.header
.msgh_size
= sizeof(msg
);
922 msg
.header
.msgh_remote_port
= wineserver_port
;
923 msg
.header
.msgh_local_port
= MACH_PORT_NULL
;
925 msg
.body
.msgh_descriptor_count
= 1;
926 msg
.task_port
.name
= mach_task_self();
927 msg
.task_port
.disposition
= MACH_MSG_TYPE_COPY_SEND
;
928 msg
.task_port
.type
= MACH_MSG_PORT_DESCRIPTOR
;
930 kret
= mach_msg_send(&msg
.header
);
931 if (kret
!= KERN_SUCCESS
)
932 server_protocol_error( "mach_msg_send failed: 0x%08x\n", kret
);
934 mach_port_deallocate(mach_task_self(), wineserver_port
);
936 #endif /* __APPLE__ */
939 /***********************************************************************
942 * Retrieve the Unix tid to use on the server side for the current thread.
944 static int get_unix_tid(void)
948 ret
= syscall( SYS_gettid
);
950 ret
= pthread_self();
951 #elif defined(__APPLE__)
952 ret
= mach_thread_self();
953 mach_port_deallocate(mach_task_self(), ret
);
954 #elif defined(__FreeBSD__)
963 /***********************************************************************
964 * server_init_process
966 * Start the server and create the initial socket pair.
968 void server_init_process(void)
970 obj_handle_t version
;
971 const char *env_socket
= getenv( "WINESERVERSOCKET" );
976 fd_socket
= atoi( env_socket
);
977 if (fcntl( fd_socket
, F_SETFD
, 1 ) == -1)
978 fatal_perror( "Bad server socket %d", fd_socket
);
979 unsetenv( "WINESERVERSOCKET" );
981 else fd_socket
= server_connect();
983 /* setup the signal mask */
984 sigemptyset( &server_block_set
);
985 sigaddset( &server_block_set
, SIGALRM
);
986 sigaddset( &server_block_set
, SIGIO
);
987 sigaddset( &server_block_set
, SIGINT
);
988 sigaddset( &server_block_set
, SIGHUP
);
989 sigaddset( &server_block_set
, SIGUSR1
);
990 sigaddset( &server_block_set
, SIGUSR2
);
991 sigaddset( &server_block_set
, SIGCHLD
);
992 pthread_sigmask( SIG_BLOCK
, &server_block_set
, NULL
);
994 /* receive the first thread request fd on the main socket */
996 if (server_pid
== -1)
999 setsockopt( fd_socket
, SOL_SOCKET
, SO_PASSCRED
, &enable
, sizeof(enable
) );
1000 ntdll_get_thread_data()->request_fd
= receive_fd( &version
);
1002 setsockopt( fd_socket
, SOL_SOCKET
, SO_PASSCRED
, &enable
, sizeof(enable
) );
1006 ntdll_get_thread_data()->request_fd
= receive_fd( &version
);
1008 if (version
!= SERVER_PROTOCOL_VERSION
)
1009 server_protocol_error( "version mismatch %d/%d.\n"
1010 "Your %s binary was not upgraded correctly,\n"
1011 "or you have an older one somewhere in your PATH.\n"
1012 "Or maybe the wrong wineserver is still running?\n",
1013 version
, SERVER_PROTOCOL_VERSION
,
1014 (version
> SERVER_PROTOCOL_VERSION
) ? "wine" : "wineserver" );
1016 send_server_task_port();
1018 #if defined(__linux__) && defined(HAVE_PRCTL)
1019 /* work around Ubuntu's ptrace breakage */
1020 if (server_pid
!= -1) prctl( 0x59616d61 /* PR_SET_PTRACER */, server_pid
);
1025 /***********************************************************************
1026 * server_init_process_done
1028 NTSTATUS
server_init_process_done(void)
1030 PEB
*peb
= NtCurrentTeb()->Peb
;
1031 IMAGE_NT_HEADERS
*nt
= RtlImageNtHeader( peb
->ImageBaseAddress
);
1034 /* Install signal handlers; this cannot be done earlier, since we cannot
1035 * send exceptions to the debugger before the create process event that
1036 * is sent by REQ_INIT_PROCESS_DONE.
1037 * We do need the handlers in place by the time the request is over, so
1038 * we set them up here. If we segfault between here and the server call
1039 * something is very wrong... */
1040 signal_init_process();
1042 /* Signal the parent process to continue */
1043 SERVER_START_REQ( init_process_done
)
1045 req
->module
= wine_server_client_ptr( peb
->ImageBaseAddress
);
1047 req
->ldt_copy
= wine_server_client_ptr( &wine_ldt_copy
);
1049 req
->entry
= wine_server_client_ptr( (char *)peb
->ImageBaseAddress
+ nt
->OptionalHeader
.AddressOfEntryPoint
);
1050 req
->gui
= (nt
->OptionalHeader
.Subsystem
!= IMAGE_SUBSYSTEM_WINDOWS_CUI
);
1051 status
= wine_server_call( req
);
1059 /***********************************************************************
1060 * server_init_thread
1062 * Send an init thread request. Return 0 if OK.
1064 size_t server_init_thread( void *entry_point
)
1066 static const int is_win64
= (sizeof(void *) > sizeof(int));
1067 const char *arch
= getenv( "WINEARCH" );
1070 struct sigaction sig_act
;
1073 sig_act
.sa_handler
= SIG_IGN
;
1074 sig_act
.sa_flags
= 0;
1075 sigemptyset( &sig_act
.sa_mask
);
1077 /* ignore SIGPIPE so that we get an EPIPE error instead */
1078 sigaction( SIGPIPE
, &sig_act
, NULL
);
1080 /* create the server->client communication pipes */
1081 if (server_pipe( reply_pipe
) == -1) server_protocol_perror( "pipe" );
1082 if (server_pipe( ntdll_get_thread_data()->wait_fd
) == -1) server_protocol_perror( "pipe" );
1083 wine_server_send_fd( reply_pipe
[1] );
1084 wine_server_send_fd( ntdll_get_thread_data()->wait_fd
[1] );
1085 ntdll_get_thread_data()->reply_fd
= reply_pipe
[0];
1086 close( reply_pipe
[1] );
1088 SERVER_START_REQ( init_thread
)
1090 req
->unix_pid
= getpid();
1091 req
->unix_tid
= get_unix_tid();
1092 req
->teb
= wine_server_client_ptr( NtCurrentTeb() );
1093 req
->entry
= wine_server_client_ptr( entry_point
);
1094 req
->reply_fd
= reply_pipe
[1];
1095 req
->wait_fd
= ntdll_get_thread_data()->wait_fd
[1];
1096 req
->debug_level
= (TRACE_ON(server
) != 0);
1097 req
->cpu
= client_cpu
;
1098 ret
= wine_server_call( req
);
1099 NtCurrentTeb()->ClientId
.UniqueProcess
= ULongToHandle(reply
->pid
);
1100 NtCurrentTeb()->ClientId
.UniqueThread
= ULongToHandle(reply
->tid
);
1101 info_size
= reply
->info_size
;
1102 server_start_time
= reply
->server_start
;
1103 server_cpus
= reply
->all_cpus
;
1107 is_wow64
= !is_win64
&& (server_cpus
& (1 << CPU_x86_64
)) != 0;
1108 ntdll_get_thread_data()->wow64_redir
= is_wow64
;
1112 case STATUS_SUCCESS
:
1115 if (!strcmp( arch
, "win32" ) && (is_win64
|| is_wow64
))
1116 fatal_error( "WINEARCH set to win32 but '%s' is a 64-bit installation.\n",
1117 wine_get_config_dir() );
1118 if (!strcmp( arch
, "win64" ) && !is_win64
&& !is_wow64
)
1119 fatal_error( "WINEARCH set to win64 but '%s' is a 32-bit installation.\n",
1120 wine_get_config_dir() );
1123 case STATUS_NOT_REGISTRY_FILE
:
1124 fatal_error( "'%s' is a 32-bit installation, it cannot support 64-bit applications.\n",
1125 wine_get_config_dir() );
1126 case STATUS_NOT_SUPPORTED
:
1128 fatal_error( "wineserver is 32-bit, it cannot support 64-bit applications.\n" );
1130 fatal_error( "'%s' is a 64-bit installation, it cannot be used with a 32-bit wineserver.\n",
1131 wine_get_config_dir() );
1133 server_protocol_error( "init_thread failed with status %x\n", ret
);