Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Client part of the client/server communication |
| 3 | * |
| 4 | * Copyright (C) 1998 Alexandre Julliard |
Alexandre Julliard | 0799c1a | 2002-03-09 23:29:33 +0000 | [diff] [blame] | 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License as published by the Free Software Foundation; either |
| 9 | * version 2.1 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
François Gouget | 1425941 | 2001-11-06 20:57:11 +0000 | [diff] [blame] | 21 | #include "config.h" |
Francois Gouget | 386cf6e | 2001-10-14 16:25:47 +0000 | [diff] [blame] | 22 | #include "wine/port.h" |
Howard Abrams | 1327748 | 1999-07-10 13:16:29 +0000 | [diff] [blame] | 23 | |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 24 | #include <assert.h> |
Alexandre Julliard | 2fe5777 | 2000-01-25 01:40:27 +0000 | [diff] [blame] | 25 | #include <ctype.h> |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 26 | #include <errno.h> |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 27 | #include <fcntl.h> |
Alexandre Julliard | 42cc2bd | 2000-06-07 03:49:41 +0000 | [diff] [blame] | 28 | #include <signal.h> |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 29 | #include <stdio.h> |
| 30 | #include <string.h> |
Alexandre Julliard | 829fe32 | 1998-07-26 14:27:39 +0000 | [diff] [blame] | 31 | #include <sys/types.h> |
Patrik Stridvall | 9633632 | 1999-10-24 22:13:47 +0000 | [diff] [blame] | 32 | #ifdef HAVE_SYS_SOCKET_H |
| 33 | # include <sys/socket.h> |
| 34 | #endif |
Alexandre Julliard | d804111 | 2000-04-14 14:42:41 +0000 | [diff] [blame] | 35 | #ifdef HAVE_SYS_WAIT_H |
| 36 | #include <sys/wait.h> |
| 37 | #endif |
Steven Edwards | 45e5681 | 2002-10-31 03:41:56 +0000 | [diff] [blame] | 38 | #ifdef HAVE_SYS_UN_H |
Alexandre Julliard | 2fe5777 | 2000-01-25 01:40:27 +0000 | [diff] [blame] | 39 | #include <sys/un.h> |
Steven Edwards | 45e5681 | 2002-10-31 03:41:56 +0000 | [diff] [blame] | 40 | #endif |
Howard Abrams | 1327748 | 1999-07-10 13:16:29 +0000 | [diff] [blame] | 41 | #ifdef HAVE_SYS_MMAN_H |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 42 | #include <sys/mman.h> |
Howard Abrams | 1327748 | 1999-07-10 13:16:29 +0000 | [diff] [blame] | 43 | #endif |
Alexandre Julliard | 2fe5777 | 2000-01-25 01:40:27 +0000 | [diff] [blame] | 44 | #include <sys/stat.h> |
Steven Edwards | 45e5681 | 2002-10-31 03:41:56 +0000 | [diff] [blame] | 45 | #ifdef HAVE_SYS_UIO_H |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 46 | #include <sys/uio.h> |
Steven Edwards | 45e5681 | 2002-10-31 03:41:56 +0000 | [diff] [blame] | 47 | #endif |
Patrik Stridvall | d016f81 | 2002-08-17 00:43:16 +0000 | [diff] [blame] | 48 | #ifdef HAVE_UNISTD_H |
| 49 | # include <unistd.h> |
| 50 | #endif |
Marcus Meissner | 317af32 | 1999-02-17 13:51:06 +0000 | [diff] [blame] | 51 | #include <stdarg.h> |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 52 | |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 53 | #include "thread.h" |
Alexandre Julliard | 4144b5b | 2002-06-20 23:21:27 +0000 | [diff] [blame] | 54 | #include "wine/library.h" |
Alexandre Julliard | 37e9503 | 2001-07-19 00:39:09 +0000 | [diff] [blame] | 55 | #include "wine/server.h" |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 56 | #include "winerror.h" |
Alexandre Julliard | 2fe5777 | 2000-01-25 01:40:27 +0000 | [diff] [blame] | 57 | #include "options.h" |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 58 | |
Alexandre Julliard | 829fe32 | 1998-07-26 14:27:39 +0000 | [diff] [blame] | 59 | /* Some versions of glibc don't define this */ |
| 60 | #ifndef SCM_RIGHTS |
| 61 | #define SCM_RIGHTS 1 |
| 62 | #endif |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 63 | |
Alexandre Julliard | 2fe5777 | 2000-01-25 01:40:27 +0000 | [diff] [blame] | 64 | #define SOCKETNAME "socket" /* name of the socket file */ |
Alexandre Julliard | 4144b5b | 2002-06-20 23:21:27 +0000 | [diff] [blame] | 65 | #define LOCKNAME "lock" /* name of the lock file */ |
Alexandre Julliard | 2fe5777 | 2000-01-25 01:40:27 +0000 | [diff] [blame] | 66 | |
Dmitry Timoshkov | b3eaa866 | 2001-09-11 00:29:24 +0000 | [diff] [blame] | 67 | #ifndef HAVE_MSGHDR_ACCRIGHTS |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 68 | /* data structure used to pass an fd with sendmsg/recvmsg */ |
| 69 | struct cmsg_fd |
| 70 | { |
| 71 | int len; /* sizeof structure */ |
| 72 | int level; /* SOL_SOCKET */ |
| 73 | int type; /* SCM_RIGHTS */ |
| 74 | int fd; /* fd to pass */ |
| 75 | }; |
Dmitry Timoshkov | b3eaa866 | 2001-09-11 00:29:24 +0000 | [diff] [blame] | 76 | #endif /* HAVE_MSGHDR_ACCRIGHTS */ |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 77 | |
Alexandre Julliard | 54f2287 | 2002-10-03 19:54:57 +0000 | [diff] [blame] | 78 | static DWORD boot_thread_id; |
Alexandre Julliard | f524240 | 2001-02-28 21:45:23 +0000 | [diff] [blame] | 79 | static sigset_t block_set; /* signals to block during server calls */ |
| 80 | static int fd_socket; /* socket to exchange file descriptors with the server */ |
Alexandre Julliard | 2fe5777 | 2000-01-25 01:40:27 +0000 | [diff] [blame] | 81 | |
Alexandre Julliard | 4144b5b | 2002-06-20 23:21:27 +0000 | [diff] [blame] | 82 | #ifdef __GNUC__ |
| 83 | static void fatal_error( const char *err, ... ) __attribute__((noreturn, format(printf,1,2))); |
| 84 | static void fatal_perror( const char *err, ... ) __attribute__((noreturn, format(printf,1,2))); |
| 85 | static void server_connect_error( const char *serverdir ) __attribute__((noreturn)); |
| 86 | #endif |
| 87 | |
Alexandre Julliard | 2fe5777 | 2000-01-25 01:40:27 +0000 | [diff] [blame] | 88 | /* die on a fatal error; use only during initialization */ |
| 89 | static void fatal_error( const char *err, ... ) |
| 90 | { |
| 91 | va_list args; |
| 92 | |
| 93 | va_start( args, err ); |
| 94 | fprintf( stderr, "wine: " ); |
| 95 | vfprintf( stderr, err, args ); |
| 96 | va_end( args ); |
| 97 | exit(1); |
| 98 | } |
| 99 | |
| 100 | /* die on a fatal error; use only during initialization */ |
| 101 | static void fatal_perror( const char *err, ... ) |
| 102 | { |
| 103 | va_list args; |
| 104 | |
| 105 | va_start( args, err ); |
| 106 | fprintf( stderr, "wine: " ); |
| 107 | vfprintf( stderr, err, args ); |
| 108 | perror( " " ); |
| 109 | va_end( args ); |
| 110 | exit(1); |
| 111 | } |
| 112 | |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 113 | /*********************************************************************** |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 114 | * server_protocol_error |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 115 | */ |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 116 | void server_protocol_error( const char *err, ... ) |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 117 | { |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 118 | va_list args; |
| 119 | |
| 120 | va_start( args, err ); |
Alexandre Julliard | 54f2287 | 2002-10-03 19:54:57 +0000 | [diff] [blame] | 121 | fprintf( stderr, "wine client error:%lx: ", NtCurrentTeb()->tid ); |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 122 | vfprintf( stderr, err, args ); |
| 123 | va_end( args ); |
Alexandre Julliard | 5016e92 | 2002-01-07 18:04:07 +0000 | [diff] [blame] | 124 | SYSDEPS_AbortThread(1); |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 128 | /*********************************************************************** |
Alexandre Julliard | 5f195f8 | 2001-02-20 23:45:07 +0000 | [diff] [blame] | 129 | * server_protocol_perror |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 130 | */ |
Alexandre Julliard | 5f195f8 | 2001-02-20 23:45:07 +0000 | [diff] [blame] | 131 | void server_protocol_perror( const char *err ) |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 132 | { |
Alexandre Julliard | 54f2287 | 2002-10-03 19:54:57 +0000 | [diff] [blame] | 133 | fprintf( stderr, "wine client error:%lx: ", NtCurrentTeb()->tid ); |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 134 | perror( err ); |
Alexandre Julliard | 5016e92 | 2002-01-07 18:04:07 +0000 | [diff] [blame] | 135 | SYSDEPS_AbortThread(1); |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | |
| 139 | /*********************************************************************** |
| 140 | * send_request |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 141 | * |
| 142 | * Send a request to the server. |
| 143 | */ |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 144 | static void send_request( const struct __server_request_info *req ) |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 145 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 146 | int i, ret; |
Alexandre Julliard | d90e964 | 2001-02-21 04:21:50 +0000 | [diff] [blame] | 147 | |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 148 | if (!req->u.req.request_header.request_size) |
| 149 | { |
| 150 | if ((ret = write( NtCurrentTeb()->request_fd, &req->u.req, |
| 151 | sizeof(req->u.req) )) == sizeof(req->u.req)) return; |
| 152 | |
| 153 | } |
| 154 | else |
| 155 | { |
| 156 | struct iovec vec[__SERVER_MAX_DATA+1]; |
| 157 | |
| 158 | vec[0].iov_base = (void *)&req->u.req; |
| 159 | vec[0].iov_len = sizeof(req->u.req); |
| 160 | for (i = 0; i < req->data_count; i++) |
| 161 | { |
| 162 | vec[i+1].iov_base = (void *)req->data[i].ptr; |
| 163 | vec[i+1].iov_len = req->data[i].size; |
| 164 | } |
| 165 | if ((ret = writev( NtCurrentTeb()->request_fd, vec, i+1 )) == |
| 166 | req->u.req.request_header.request_size + sizeof(req->u.req)) return; |
| 167 | } |
| 168 | |
Alexandre Julliard | d90e964 | 2001-02-21 04:21:50 +0000 | [diff] [blame] | 169 | if (ret >= 0) server_protocol_error( "partial write %d\n", ret ); |
Alexandre Julliard | 5016e92 | 2002-01-07 18:04:07 +0000 | [diff] [blame] | 170 | if (errno == EPIPE) SYSDEPS_AbortThread(0); |
Alexandre Julliard | d90e964 | 2001-02-21 04:21:50 +0000 | [diff] [blame] | 171 | server_protocol_perror( "sendmsg" ); |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 174 | |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 175 | /*********************************************************************** |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 176 | * read_reply_data |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 177 | * |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 178 | * Read data from the reply buffer; helper for wait_reply. |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 179 | */ |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 180 | static void read_reply_data( void *buffer, size_t size ) |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 181 | { |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 182 | int ret; |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 183 | |
| 184 | for (;;) |
| 185 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 186 | if ((ret = read( NtCurrentTeb()->reply_fd, buffer, size )) > 0) |
| 187 | { |
| 188 | if (!(size -= ret)) return; |
| 189 | buffer = (char *)buffer + ret; |
| 190 | continue; |
| 191 | } |
Alexandre Julliard | 12f29b5 | 2000-03-17 15:16:57 +0000 | [diff] [blame] | 192 | if (!ret) break; |
Alexandre Julliard | 8611353 | 2000-08-29 03:54:30 +0000 | [diff] [blame] | 193 | if (errno == EINTR) continue; |
| 194 | if (errno == EPIPE) break; |
Alexandre Julliard | 5f195f8 | 2001-02-20 23:45:07 +0000 | [diff] [blame] | 195 | server_protocol_perror("read"); |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 196 | } |
Alexandre Julliard | 12f29b5 | 2000-03-17 15:16:57 +0000 | [diff] [blame] | 197 | /* the server closed the connection; time to die... */ |
Alexandre Julliard | 5016e92 | 2002-01-07 18:04:07 +0000 | [diff] [blame] | 198 | SYSDEPS_AbortThread(0); |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | |
| 202 | /*********************************************************************** |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 203 | * wait_reply |
| 204 | * |
| 205 | * Wait for a reply from the server. |
| 206 | */ |
| 207 | inline static void wait_reply( struct __server_request_info *req ) |
| 208 | { |
| 209 | read_reply_data( &req->u.reply, sizeof(req->u.reply) ); |
| 210 | if (req->u.reply.reply_header.reply_size) |
| 211 | read_reply_data( req->reply_data, req->u.reply.reply_header.reply_size ); |
| 212 | } |
| 213 | |
| 214 | |
| 215 | /*********************************************************************** |
Patrik Stridvall | d0a4177 | 2001-02-14 23:11:17 +0000 | [diff] [blame] | 216 | * wine_server_call (NTDLL.@) |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 217 | * |
| 218 | * Perform a server call. |
| 219 | */ |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 220 | unsigned int wine_server_call( void *req_ptr ) |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 221 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 222 | struct __server_request_info * const req = req_ptr; |
Alexandre Julliard | f524240 | 2001-02-28 21:45:23 +0000 | [diff] [blame] | 223 | sigset_t old_set; |
| 224 | |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 225 | memset( (char *)&req->u.req + req->size, 0, sizeof(req->u.req) - req->size ); |
Alexandre Julliard | f524240 | 2001-02-28 21:45:23 +0000 | [diff] [blame] | 226 | sigprocmask( SIG_BLOCK, &block_set, &old_set ); |
Alexandre Julliard | 67a7499 | 2001-02-27 02:09:16 +0000 | [diff] [blame] | 227 | send_request( req ); |
| 228 | wait_reply( req ); |
Alexandre Julliard | f524240 | 2001-02-28 21:45:23 +0000 | [diff] [blame] | 229 | sigprocmask( SIG_SETMASK, &old_set, NULL ); |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 230 | return req->u.reply.reply_header.error; |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 233 | |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 234 | /*********************************************************************** |
Alexandre Julliard | f524240 | 2001-02-28 21:45:23 +0000 | [diff] [blame] | 235 | * wine_server_send_fd |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 236 | * |
Alexandre Julliard | f524240 | 2001-02-28 21:45:23 +0000 | [diff] [blame] | 237 | * Send a file descriptor to the server. |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 238 | */ |
Alexandre Julliard | f524240 | 2001-02-28 21:45:23 +0000 | [diff] [blame] | 239 | void wine_server_send_fd( int fd ) |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 240 | { |
Alexandre Julliard | f524240 | 2001-02-28 21:45:23 +0000 | [diff] [blame] | 241 | #ifndef HAVE_MSGHDR_ACCRIGHTS |
| 242 | struct cmsg_fd cmsg; |
| 243 | #endif |
| 244 | struct send_fd data; |
| 245 | struct msghdr msghdr; |
| 246 | struct iovec vec; |
| 247 | int ret; |
| 248 | |
| 249 | vec.iov_base = (void *)&data; |
| 250 | vec.iov_len = sizeof(data); |
| 251 | |
| 252 | msghdr.msg_name = NULL; |
| 253 | msghdr.msg_namelen = 0; |
| 254 | msghdr.msg_iov = &vec; |
| 255 | msghdr.msg_iovlen = 1; |
| 256 | |
| 257 | #ifdef HAVE_MSGHDR_ACCRIGHTS |
| 258 | msghdr.msg_accrights = (void *)&fd; |
| 259 | msghdr.msg_accrightslen = sizeof(fd); |
| 260 | #else /* HAVE_MSGHDR_ACCRIGHTS */ |
| 261 | cmsg.len = sizeof(cmsg); |
| 262 | cmsg.level = SOL_SOCKET; |
| 263 | cmsg.type = SCM_RIGHTS; |
| 264 | cmsg.fd = fd; |
| 265 | msghdr.msg_control = &cmsg; |
| 266 | msghdr.msg_controllen = sizeof(cmsg); |
| 267 | msghdr.msg_flags = 0; |
| 268 | #endif /* HAVE_MSGHDR_ACCRIGHTS */ |
| 269 | |
Alexandre Julliard | 54f2287 | 2002-10-03 19:54:57 +0000 | [diff] [blame] | 270 | data.tid = GetCurrentThreadId(); |
Alexandre Julliard | f524240 | 2001-02-28 21:45:23 +0000 | [diff] [blame] | 271 | data.fd = fd; |
| 272 | |
| 273 | for (;;) |
| 274 | { |
| 275 | if ((ret = sendmsg( fd_socket, &msghdr, 0 )) == sizeof(data)) return; |
| 276 | if (ret >= 0) server_protocol_error( "partial write %d\n", ret ); |
| 277 | if (errno == EINTR) continue; |
Alexandre Julliard | 5016e92 | 2002-01-07 18:04:07 +0000 | [diff] [blame] | 278 | if (errno == EPIPE) SYSDEPS_AbortThread(0); |
Alexandre Julliard | f524240 | 2001-02-28 21:45:23 +0000 | [diff] [blame] | 279 | server_protocol_perror( "sendmsg" ); |
| 280 | } |
Alexandre Julliard | 6ebbe3c | 1999-01-01 17:04:00 +0000 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | |
| 284 | /*********************************************************************** |
Alexandre Julliard | 8859d77 | 2001-03-01 22:13:49 +0000 | [diff] [blame] | 285 | * receive_fd |
Alexandre Julliard | d549f69 | 2000-12-22 02:04:15 +0000 | [diff] [blame] | 286 | * |
| 287 | * Receive a file descriptor passed from the server. |
Alexandre Julliard | d549f69 | 2000-12-22 02:04:15 +0000 | [diff] [blame] | 288 | */ |
Alexandre Julliard | 5188574 | 2002-05-30 20:12:58 +0000 | [diff] [blame] | 289 | static int receive_fd( obj_handle_t *handle ) |
Alexandre Julliard | d549f69 | 2000-12-22 02:04:15 +0000 | [diff] [blame] | 290 | { |
| 291 | struct iovec vec; |
Alexandre Julliard | 8859d77 | 2001-03-01 22:13:49 +0000 | [diff] [blame] | 292 | int ret, fd; |
Alexandre Julliard | d549f69 | 2000-12-22 02:04:15 +0000 | [diff] [blame] | 293 | |
| 294 | #ifdef HAVE_MSGHDR_ACCRIGHTS |
| 295 | struct msghdr msghdr; |
| 296 | |
| 297 | fd = -1; |
| 298 | msghdr.msg_accrights = (void *)&fd; |
| 299 | msghdr.msg_accrightslen = sizeof(fd); |
| 300 | #else /* HAVE_MSGHDR_ACCRIGHTS */ |
| 301 | struct msghdr msghdr; |
| 302 | struct cmsg_fd cmsg; |
| 303 | |
| 304 | cmsg.len = sizeof(cmsg); |
| 305 | cmsg.level = SOL_SOCKET; |
| 306 | cmsg.type = SCM_RIGHTS; |
| 307 | cmsg.fd = -1; |
| 308 | msghdr.msg_control = &cmsg; |
| 309 | msghdr.msg_controllen = sizeof(cmsg); |
| 310 | msghdr.msg_flags = 0; |
| 311 | #endif /* HAVE_MSGHDR_ACCRIGHTS */ |
| 312 | |
| 313 | msghdr.msg_name = NULL; |
| 314 | msghdr.msg_namelen = 0; |
| 315 | msghdr.msg_iov = &vec; |
| 316 | msghdr.msg_iovlen = 1; |
Alexandre Julliard | 8859d77 | 2001-03-01 22:13:49 +0000 | [diff] [blame] | 317 | vec.iov_base = (void *)handle; |
| 318 | vec.iov_len = sizeof(*handle); |
Alexandre Julliard | d549f69 | 2000-12-22 02:04:15 +0000 | [diff] [blame] | 319 | |
| 320 | for (;;) |
| 321 | { |
Alexandre Julliard | 8859d77 | 2001-03-01 22:13:49 +0000 | [diff] [blame] | 322 | if ((ret = recvmsg( fd_socket, &msghdr, 0 )) > 0) |
Alexandre Julliard | d549f69 | 2000-12-22 02:04:15 +0000 | [diff] [blame] | 323 | { |
| 324 | #ifndef HAVE_MSGHDR_ACCRIGHTS |
| 325 | fd = cmsg.fd; |
| 326 | #endif |
Alexandre Julliard | 8859d77 | 2001-03-01 22:13:49 +0000 | [diff] [blame] | 327 | if (fd == -1) server_protocol_error( "no fd received for handle %d\n", *handle ); |
| 328 | fcntl( fd, F_SETFD, 1 ); /* set close on exec flag */ |
Alexandre Julliard | d549f69 | 2000-12-22 02:04:15 +0000 | [diff] [blame] | 329 | return fd; |
| 330 | } |
| 331 | if (!ret) break; |
| 332 | if (errno == EINTR) continue; |
| 333 | if (errno == EPIPE) break; |
Alexandre Julliard | 5f195f8 | 2001-02-20 23:45:07 +0000 | [diff] [blame] | 334 | server_protocol_perror("recvmsg"); |
Alexandre Julliard | d549f69 | 2000-12-22 02:04:15 +0000 | [diff] [blame] | 335 | } |
| 336 | /* the server closed the connection; time to die... */ |
Alexandre Julliard | 5016e92 | 2002-01-07 18:04:07 +0000 | [diff] [blame] | 337 | SYSDEPS_AbortThread(0); |
Alexandre Julliard | d549f69 | 2000-12-22 02:04:15 +0000 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | |
| 341 | /*********************************************************************** |
Alexandre Julliard | 8d1550d | 2002-03-23 18:48:12 +0000 | [diff] [blame] | 342 | * store_cached_fd |
Alexandre Julliard | 8859d77 | 2001-03-01 22:13:49 +0000 | [diff] [blame] | 343 | * |
Alexandre Julliard | 8d1550d | 2002-03-23 18:48:12 +0000 | [diff] [blame] | 344 | * Store the cached fd value for a given handle back into the server. |
| 345 | * Returns the new fd, which can be different if there was already an |
| 346 | * fd in the cache for that handle. |
Alexandre Julliard | 8859d77 | 2001-03-01 22:13:49 +0000 | [diff] [blame] | 347 | */ |
Alexandre Julliard | 5188574 | 2002-05-30 20:12:58 +0000 | [diff] [blame] | 348 | inline static int store_cached_fd( int fd, obj_handle_t handle ) |
Alexandre Julliard | 8859d77 | 2001-03-01 22:13:49 +0000 | [diff] [blame] | 349 | { |
Alexandre Julliard | 8859d77 | 2001-03-01 22:13:49 +0000 | [diff] [blame] | 350 | SERVER_START_REQ( set_handle_info ) |
| 351 | { |
Alexandre Julliard | 8d1550d | 2002-03-23 18:48:12 +0000 | [diff] [blame] | 352 | req->handle = handle; |
Alexandre Julliard | 8859d77 | 2001-03-01 22:13:49 +0000 | [diff] [blame] | 353 | req->flags = 0; |
| 354 | req->mask = 0; |
| 355 | req->fd = fd; |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 356 | if (!wine_server_call( req )) |
Alexandre Julliard | 8859d77 | 2001-03-01 22:13:49 +0000 | [diff] [blame] | 357 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 358 | if (reply->cur_fd != fd) |
Alexandre Julliard | 8859d77 | 2001-03-01 22:13:49 +0000 | [diff] [blame] | 359 | { |
| 360 | /* someone was here before us */ |
| 361 | close( fd ); |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 362 | fd = reply->cur_fd; |
Alexandre Julliard | 8859d77 | 2001-03-01 22:13:49 +0000 | [diff] [blame] | 363 | } |
| 364 | } |
| 365 | else |
| 366 | { |
| 367 | close( fd ); |
| 368 | fd = -1; |
| 369 | } |
| 370 | } |
| 371 | SERVER_END_REQ; |
Alexandre Julliard | 8859d77 | 2001-03-01 22:13:49 +0000 | [diff] [blame] | 372 | return fd; |
| 373 | } |
| 374 | |
| 375 | |
| 376 | /*********************************************************************** |
Alexandre Julliard | be367c7 | 2002-05-30 20:40:02 +0000 | [diff] [blame] | 377 | * wine_server_fd_to_handle (NTDLL.@) |
| 378 | * |
| 379 | * Allocate a file handle for a Unix fd. |
| 380 | */ |
| 381 | int wine_server_fd_to_handle( int fd, unsigned int access, int inherit, obj_handle_t *handle ) |
| 382 | { |
| 383 | int ret; |
| 384 | |
| 385 | *handle = 0; |
| 386 | wine_server_send_fd( fd ); |
| 387 | |
| 388 | SERVER_START_REQ( alloc_file_handle ) |
| 389 | { |
| 390 | req->access = access; |
| 391 | req->inherit = inherit; |
| 392 | req->fd = fd; |
| 393 | if (!(ret = wine_server_call( req ))) *handle = reply->handle; |
| 394 | } |
| 395 | SERVER_END_REQ; |
| 396 | return ret; |
| 397 | } |
| 398 | |
| 399 | |
| 400 | /*********************************************************************** |
Alexandre Julliard | 8d1550d | 2002-03-23 18:48:12 +0000 | [diff] [blame] | 401 | * wine_server_handle_to_fd (NTDLL.@) |
| 402 | * |
| 403 | * Retrieve the Unix fd corresponding to a file handle. |
| 404 | */ |
Alexandre Julliard | 5188574 | 2002-05-30 20:12:58 +0000 | [diff] [blame] | 405 | int wine_server_handle_to_fd( obj_handle_t handle, unsigned int access, int *unix_fd, |
Alexandre Julliard | 8d1550d | 2002-03-23 18:48:12 +0000 | [diff] [blame] | 406 | enum fd_type *type, int *flags ) |
| 407 | { |
Alexandre Julliard | 5188574 | 2002-05-30 20:12:58 +0000 | [diff] [blame] | 408 | obj_handle_t fd_handle; |
Alexandre Julliard | 8d1550d | 2002-03-23 18:48:12 +0000 | [diff] [blame] | 409 | int ret, fd = -1; |
| 410 | |
| 411 | *unix_fd = -1; |
| 412 | for (;;) |
| 413 | { |
| 414 | SERVER_START_REQ( get_handle_fd ) |
| 415 | { |
| 416 | req->handle = handle; |
| 417 | req->access = access; |
| 418 | if (!(ret = wine_server_call( req ))) fd = reply->fd; |
| 419 | if (type) *type = reply->type; |
| 420 | if (flags) *flags = reply->flags; |
| 421 | } |
| 422 | SERVER_END_REQ; |
| 423 | if (ret) return ret; |
| 424 | |
| 425 | if (fd != -1) break; |
| 426 | |
| 427 | /* it wasn't in the cache, get it from the server */ |
| 428 | fd = receive_fd( &fd_handle ); |
| 429 | /* and store it back into the cache */ |
| 430 | fd = store_cached_fd( fd, fd_handle ); |
| 431 | |
| 432 | if (fd_handle == handle) break; |
| 433 | /* if we received a different handle this means there was |
| 434 | * a race with another thread; we restart everything from |
| 435 | * scratch in this case. |
| 436 | */ |
| 437 | } |
| 438 | |
| 439 | if ((fd != -1) && ((fd = dup(fd)) == -1)) return STATUS_TOO_MANY_OPENED_FILES; |
| 440 | *unix_fd = fd; |
| 441 | return STATUS_SUCCESS; |
| 442 | } |
| 443 | |
| 444 | |
| 445 | /*********************************************************************** |
Alexandre Julliard | 2fe5777 | 2000-01-25 01:40:27 +0000 | [diff] [blame] | 446 | * start_server |
| 447 | * |
| 448 | * Start a new wine server. |
| 449 | */ |
| 450 | static void start_server( const char *oldcwd ) |
| 451 | { |
| 452 | static int started; /* we only try once */ |
Alexandre Julliard | c10c9ef | 2000-08-11 21:16:53 +0000 | [diff] [blame] | 453 | char *path, *p; |
Alexandre Julliard | 2fe5777 | 2000-01-25 01:40:27 +0000 | [diff] [blame] | 454 | if (!started) |
| 455 | { |
Alexandre Julliard | d804111 | 2000-04-14 14:42:41 +0000 | [diff] [blame] | 456 | int status; |
Alexandre Julliard | 2fe5777 | 2000-01-25 01:40:27 +0000 | [diff] [blame] | 457 | int pid = fork(); |
| 458 | if (pid == -1) fatal_perror( "fork" ); |
| 459 | if (!pid) |
| 460 | { |
Alexandre Julliard | 50097a0 | 2000-09-10 03:24:28 +0000 | [diff] [blame] | 461 | /* if server is explicitly specified, use this */ |
| 462 | if ((p = getenv("WINESERVER"))) |
| 463 | { |
Alexandre Julliard | 61d26d7 | 2001-05-09 19:46:39 +0000 | [diff] [blame] | 464 | if (p[0] != '/' && oldcwd[0] == '/') /* make it an absolute path */ |
| 465 | { |
| 466 | if (!(path = malloc( strlen(oldcwd) + strlen(p) + 1 ))) |
| 467 | fatal_error( "out of memory\n" ); |
| 468 | sprintf( path, "%s/%s", oldcwd, p ); |
| 469 | p = path; |
| 470 | } |
Alexandre Julliard | 4144b5b | 2002-06-20 23:21:27 +0000 | [diff] [blame] | 471 | execl( p, p, NULL ); |
Alexandre Julliard | 50097a0 | 2000-09-10 03:24:28 +0000 | [diff] [blame] | 472 | fatal_perror( "could not exec the server '%s'\n" |
| 473 | " specified in the WINESERVER environment variable", p ); |
| 474 | } |
| 475 | |
Alexandre Julliard | eafa391 | 2000-01-25 21:19:58 +0000 | [diff] [blame] | 476 | /* first try the installation dir */ |
Alexandre Julliard | 2fe5777 | 2000-01-25 01:40:27 +0000 | [diff] [blame] | 477 | execl( BINDIR "/wineserver", "wineserver", NULL ); |
Alexandre Julliard | c10c9ef | 2000-08-11 21:16:53 +0000 | [diff] [blame] | 478 | |
Alexandre Julliard | eafa391 | 2000-01-25 21:19:58 +0000 | [diff] [blame] | 479 | /* now try the dir we were launched from */ |
Alexandre Julliard | c10c9ef | 2000-08-11 21:16:53 +0000 | [diff] [blame] | 480 | if (full_argv0) |
| 481 | { |
| 482 | if (!(path = malloc( strlen(full_argv0) + 20 ))) |
| 483 | fatal_error( "out of memory\n" ); |
| 484 | if ((p = strrchr( strcpy( path, full_argv0 ), '/' ))) |
| 485 | { |
| 486 | strcpy( p, "/wineserver" ); |
Alexandre Julliard | 4144b5b | 2002-06-20 23:21:27 +0000 | [diff] [blame] | 487 | execl( path, path, NULL ); |
Alexandre Julliard | c10c9ef | 2000-08-11 21:16:53 +0000 | [diff] [blame] | 488 | } |
Alexandre Julliard | 3536316 | 2002-05-22 21:32:49 +0000 | [diff] [blame] | 489 | free(path); |
Alexandre Julliard | c10c9ef | 2000-08-11 21:16:53 +0000 | [diff] [blame] | 490 | } |
| 491 | |
Alexandre Julliard | 3536316 | 2002-05-22 21:32:49 +0000 | [diff] [blame] | 492 | /* finally try the path */ |
Alexandre Julliard | c10c9ef | 2000-08-11 21:16:53 +0000 | [diff] [blame] | 493 | execlp( "wineserver", "wineserver", NULL ); |
Alexandre Julliard | 2fe5777 | 2000-01-25 01:40:27 +0000 | [diff] [blame] | 494 | fatal_error( "could not exec wineserver\n" ); |
| 495 | } |
Alexandre Julliard | d804111 | 2000-04-14 14:42:41 +0000 | [diff] [blame] | 496 | waitpid( pid, &status, 0 ); |
| 497 | status = WIFEXITED(status) ? WEXITSTATUS(status) : 1; |
Alexandre Julliard | 4144b5b | 2002-06-20 23:21:27 +0000 | [diff] [blame] | 498 | if (status == 2) return; /* server lock held by someone else, will retry later */ |
Alexandre Julliard | d804111 | 2000-04-14 14:42:41 +0000 | [diff] [blame] | 499 | if (status) exit(status); /* server failed */ |
Alexandre Julliard | 4144b5b | 2002-06-20 23:21:27 +0000 | [diff] [blame] | 500 | started = 1; |
Alexandre Julliard | 2fe5777 | 2000-01-25 01:40:27 +0000 | [diff] [blame] | 501 | } |
| 502 | } |
| 503 | |
Alexandre Julliard | 4144b5b | 2002-06-20 23:21:27 +0000 | [diff] [blame] | 504 | |
| 505 | /*********************************************************************** |
| 506 | * server_connect_error |
| 507 | * |
| 508 | * Try to display a meaningful explanation of why we couldn't connect |
| 509 | * to the server. |
| 510 | */ |
| 511 | static void server_connect_error( const char *serverdir ) |
| 512 | { |
| 513 | int fd; |
| 514 | struct flock fl; |
| 515 | |
| 516 | if ((fd = open( LOCKNAME, O_WRONLY )) == -1) |
| 517 | fatal_error( "for some mysterious reason, the wine server never started.\n" ); |
| 518 | |
| 519 | fl.l_type = F_WRLCK; |
| 520 | fl.l_whence = SEEK_SET; |
| 521 | fl.l_start = 0; |
| 522 | fl.l_len = 1; |
| 523 | if (fcntl( fd, F_GETLK, &fl ) != -1) |
| 524 | { |
| 525 | if (fl.l_type == F_WRLCK) /* the file is locked */ |
| 526 | fatal_error( "a wine server seems to be running, but I cannot connect to it.\n" |
| 527 | " You probably need to kill that process (it might be pid %d).\n", |
| 528 | (int)fl.l_pid ); |
| 529 | fatal_error( "for some mysterious reason, the wine server failed to run.\n" ); |
| 530 | } |
| 531 | fatal_error( "the file system of '%s' doesn't support locks,\n" |
| 532 | " and there is a 'socket' file in that directory that prevents wine from starting.\n" |
| 533 | " You should make sure no wine server is running, remove that file and try again.\n", |
| 534 | serverdir ); |
| 535 | } |
| 536 | |
| 537 | |
Alexandre Julliard | 2fe5777 | 2000-01-25 01:40:27 +0000 | [diff] [blame] | 538 | /*********************************************************************** |
| 539 | * server_connect |
| 540 | * |
| 541 | * Attempt to connect to an existing server socket. |
| 542 | * We need to be in the server directory already. |
| 543 | */ |
| 544 | static int server_connect( const char *oldcwd, const char *serverdir ) |
| 545 | { |
| 546 | struct sockaddr_un addr; |
| 547 | struct stat st; |
Alexandre Julliard | c10c9ef | 2000-08-11 21:16:53 +0000 | [diff] [blame] | 548 | int s, slen, retry; |
Alexandre Julliard | 2fe5777 | 2000-01-25 01:40:27 +0000 | [diff] [blame] | 549 | |
Alexandre Julliard | d804111 | 2000-04-14 14:42:41 +0000 | [diff] [blame] | 550 | /* chdir to the server directory */ |
Alexandre Julliard | 2fe5777 | 2000-01-25 01:40:27 +0000 | [diff] [blame] | 551 | if (chdir( serverdir ) == -1) |
| 552 | { |
| 553 | if (errno != ENOENT) fatal_perror( "chdir to %s", serverdir ); |
Alexandre Julliard | c10c9ef | 2000-08-11 21:16:53 +0000 | [diff] [blame] | 554 | start_server( "." ); |
Alexandre Julliard | d804111 | 2000-04-14 14:42:41 +0000 | [diff] [blame] | 555 | if (chdir( serverdir ) == -1) fatal_perror( "chdir to %s", serverdir ); |
Alexandre Julliard | 2fe5777 | 2000-01-25 01:40:27 +0000 | [diff] [blame] | 556 | } |
Alexandre Julliard | d804111 | 2000-04-14 14:42:41 +0000 | [diff] [blame] | 557 | |
| 558 | /* make sure we are at the right place */ |
Alexandre Julliard | 2fe5777 | 2000-01-25 01:40:27 +0000 | [diff] [blame] | 559 | if (stat( ".", &st ) == -1) fatal_perror( "stat %s", serverdir ); |
| 560 | if (st.st_uid != getuid()) fatal_error( "'%s' is not owned by you\n", serverdir ); |
| 561 | if (st.st_mode & 077) fatal_error( "'%s' must not be accessible by other users\n", serverdir ); |
| 562 | |
Alexandre Julliard | 4144b5b | 2002-06-20 23:21:27 +0000 | [diff] [blame] | 563 | for (retry = 0; retry < 6; retry++) |
Alexandre Julliard | 2fe5777 | 2000-01-25 01:40:27 +0000 | [diff] [blame] | 564 | { |
Alexandre Julliard | 4144b5b | 2002-06-20 23:21:27 +0000 | [diff] [blame] | 565 | /* if not the first try, wait a bit to leave the previous server time to exit */ |
| 566 | if (retry) |
| 567 | { |
| 568 | usleep( 100000 * retry * retry ); |
| 569 | start_server( oldcwd ); |
| 570 | if (lstat( SOCKETNAME, &st ) == -1) continue; /* still no socket, wait a bit more */ |
| 571 | } |
| 572 | else if (lstat( SOCKETNAME, &st ) == -1) /* check for an already existing socket */ |
Alexandre Julliard | c10c9ef | 2000-08-11 21:16:53 +0000 | [diff] [blame] | 573 | { |
| 574 | if (errno != ENOENT) fatal_perror( "lstat %s/%s", serverdir, SOCKETNAME ); |
| 575 | start_server( oldcwd ); |
Alexandre Julliard | 4144b5b | 2002-06-20 23:21:27 +0000 | [diff] [blame] | 576 | if (lstat( SOCKETNAME, &st ) == -1) continue; /* still no socket, wait a bit more */ |
Alexandre Julliard | c10c9ef | 2000-08-11 21:16:53 +0000 | [diff] [blame] | 577 | } |
Alexandre Julliard | 2fe5777 | 2000-01-25 01:40:27 +0000 | [diff] [blame] | 578 | |
Roberto Augusto Pungartnik | 19f8dda | 2000-09-22 22:19:58 +0000 | [diff] [blame] | 579 | /* make sure the socket is sane (ISFIFO needed for Solaris) */ |
| 580 | if (!S_ISSOCK(st.st_mode) && !S_ISFIFO(st.st_mode)) |
Alexandre Julliard | c10c9ef | 2000-08-11 21:16:53 +0000 | [diff] [blame] | 581 | fatal_error( "'%s/%s' is not a socket\n", serverdir, SOCKETNAME ); |
| 582 | if (st.st_uid != getuid()) |
| 583 | fatal_error( "'%s/%s' is not owned by you\n", serverdir, SOCKETNAME ); |
| 584 | |
| 585 | /* try to connect to it */ |
| 586 | addr.sun_family = AF_UNIX; |
| 587 | strcpy( addr.sun_path, SOCKETNAME ); |
| 588 | slen = sizeof(addr) - sizeof(addr.sun_path) + strlen(addr.sun_path) + 1; |
Juergen Lock | 2d33ab9 | 2000-02-13 16:03:29 +0000 | [diff] [blame] | 589 | #ifdef HAVE_SOCKADDR_SUN_LEN |
Alexandre Julliard | c10c9ef | 2000-08-11 21:16:53 +0000 | [diff] [blame] | 590 | addr.sun_len = slen; |
Juergen Lock | 2d33ab9 | 2000-02-13 16:03:29 +0000 | [diff] [blame] | 591 | #endif |
Alexandre Julliard | 42cc2bd | 2000-06-07 03:49:41 +0000 | [diff] [blame] | 592 | if ((s = socket( AF_UNIX, SOCK_STREAM, 0 )) == -1) fatal_perror( "socket" ); |
Alexandre Julliard | c10c9ef | 2000-08-11 21:16:53 +0000 | [diff] [blame] | 593 | if (connect( s, (struct sockaddr *)&addr, slen ) != -1) |
| 594 | { |
| 595 | fcntl( s, F_SETFD, 1 ); /* set close on exec flag */ |
| 596 | return s; |
| 597 | } |
| 598 | close( s ); |
Alexandre Julliard | 2fe5777 | 2000-01-25 01:40:27 +0000 | [diff] [blame] | 599 | } |
Alexandre Julliard | 4144b5b | 2002-06-20 23:21:27 +0000 | [diff] [blame] | 600 | server_connect_error( serverdir ); |
Alexandre Julliard | 2fe5777 | 2000-01-25 01:40:27 +0000 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | |
| 604 | /*********************************************************************** |
Alexandre Julliard | f016752 | 1999-03-21 19:26:25 +0000 | [diff] [blame] | 605 | * CLIENT_InitServer |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 606 | * |
Alexandre Julliard | f016752 | 1999-03-21 19:26:25 +0000 | [diff] [blame] | 607 | * Start the server and create the initial socket pair. |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 608 | */ |
Alexandre Julliard | 8859d77 | 2001-03-01 22:13:49 +0000 | [diff] [blame] | 609 | void CLIENT_InitServer(void) |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 610 | { |
Alexandre Julliard | 8859d77 | 2001-03-01 22:13:49 +0000 | [diff] [blame] | 611 | int size; |
Alexandre Julliard | 4144b5b | 2002-06-20 23:21:27 +0000 | [diff] [blame] | 612 | char *oldcwd; |
Alexandre Julliard | 5188574 | 2002-05-30 20:12:58 +0000 | [diff] [blame] | 613 | obj_handle_t dummy_handle; |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 614 | |
Alexandre Julliard | 2fe5777 | 2000-01-25 01:40:27 +0000 | [diff] [blame] | 615 | /* retrieve the current directory */ |
| 616 | for (size = 512; ; size *= 2) |
Alexandre Julliard | f016752 | 1999-03-21 19:26:25 +0000 | [diff] [blame] | 617 | { |
Dimitrie O. Paun | 9ad9636 | 2000-03-19 14:29:50 +0000 | [diff] [blame] | 618 | if (!(oldcwd = malloc( size ))) break; |
Alexandre Julliard | 2fe5777 | 2000-01-25 01:40:27 +0000 | [diff] [blame] | 619 | if (getcwd( oldcwd, size )) break; |
| 620 | free( oldcwd ); |
| 621 | if (errno == ERANGE) continue; |
| 622 | oldcwd = NULL; |
Ulrich Weigand | 371fd75 | 1999-04-11 17:13:03 +0000 | [diff] [blame] | 623 | break; |
Alexandre Julliard | f016752 | 1999-03-21 19:26:25 +0000 | [diff] [blame] | 624 | } |
Alexandre Julliard | 2fe5777 | 2000-01-25 01:40:27 +0000 | [diff] [blame] | 625 | |
Alexandre Julliard | a3e0cfc | 2000-07-16 18:21:34 +0000 | [diff] [blame] | 626 | /* if argv[0] is a relative path, make it absolute */ |
| 627 | full_argv0 = argv0; |
| 628 | if (oldcwd && argv0[0] != '/' && strchr( argv0, '/' )) |
| 629 | { |
| 630 | char *new_argv0 = malloc( strlen(oldcwd) + strlen(argv0) + 2 ); |
| 631 | if (new_argv0) |
| 632 | { |
| 633 | strcpy( new_argv0, oldcwd ); |
| 634 | strcat( new_argv0, "/" ); |
| 635 | strcat( new_argv0, argv0 ); |
| 636 | full_argv0 = new_argv0; |
| 637 | } |
| 638 | } |
| 639 | |
Alexandre Julliard | d804111 | 2000-04-14 14:42:41 +0000 | [diff] [blame] | 640 | /* connect to the server */ |
Alexandre Julliard | 4144b5b | 2002-06-20 23:21:27 +0000 | [diff] [blame] | 641 | fd_socket = server_connect( oldcwd, wine_get_server_dir() ); |
Alexandre Julliard | 2fe5777 | 2000-01-25 01:40:27 +0000 | [diff] [blame] | 642 | |
Alexandre Julliard | 2fe5777 | 2000-01-25 01:40:27 +0000 | [diff] [blame] | 643 | /* switch back to the starting directory */ |
| 644 | if (oldcwd) |
| 645 | { |
| 646 | chdir( oldcwd ); |
| 647 | free( oldcwd ); |
| 648 | } |
Alexandre Julliard | f524240 | 2001-02-28 21:45:23 +0000 | [diff] [blame] | 649 | |
| 650 | /* setup the signal mask */ |
| 651 | sigemptyset( &block_set ); |
| 652 | sigaddset( &block_set, SIGALRM ); |
| 653 | sigaddset( &block_set, SIGIO ); |
| 654 | sigaddset( &block_set, SIGINT ); |
| 655 | sigaddset( &block_set, SIGHUP ); |
Alexandre Julliard | d04ccb8 | 2003-03-04 22:18:43 +0000 | [diff] [blame] | 656 | sigaddset( &block_set, SIGUSR1 ); |
| 657 | sigaddset( &block_set, SIGUSR2 ); |
Alexandre Julliard | f524240 | 2001-02-28 21:45:23 +0000 | [diff] [blame] | 658 | |
Alexandre Julliard | 8859d77 | 2001-03-01 22:13:49 +0000 | [diff] [blame] | 659 | /* receive the first thread request fd on the main socket */ |
| 660 | NtCurrentTeb()->request_fd = receive_fd( &dummy_handle ); |
| 661 | |
| 662 | CLIENT_InitThread(); |
| 663 | } |
| 664 | |
| 665 | |
| 666 | /*********************************************************************** |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 667 | * CLIENT_InitThread |
| 668 | * |
| 669 | * Send an init thread request. Return 0 if OK. |
| 670 | */ |
Alexandre Julliard | 8859d77 | 2001-03-01 22:13:49 +0000 | [diff] [blame] | 671 | void CLIENT_InitThread(void) |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 672 | { |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 673 | TEB *teb = NtCurrentTeb(); |
Alexandre Julliard | 8859d77 | 2001-03-01 22:13:49 +0000 | [diff] [blame] | 674 | int version, ret; |
Alexandre Julliard | e5dedb1 | 2001-03-08 01:16:41 +0000 | [diff] [blame] | 675 | int reply_pipe[2]; |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 676 | |
Alexandre Julliard | 42cc2bd | 2000-06-07 03:49:41 +0000 | [diff] [blame] | 677 | /* ignore SIGPIPE so that we get a EPIPE error instead */ |
| 678 | signal( SIGPIPE, SIG_IGN ); |
François Gouget | 103295c | 2002-06-13 21:57:38 +0000 | [diff] [blame] | 679 | /* automatic child reaping to avoid zombies */ |
| 680 | signal( SIGCHLD, SIG_IGN ); |
Alexandre Julliard | 42cc2bd | 2000-06-07 03:49:41 +0000 | [diff] [blame] | 681 | |
Alexandre Julliard | 8859d77 | 2001-03-01 22:13:49 +0000 | [diff] [blame] | 682 | /* create the server->client communication pipes */ |
| 683 | if (pipe( reply_pipe ) == -1) server_protocol_perror( "pipe" ); |
Alexandre Julliard | e5dedb1 | 2001-03-08 01:16:41 +0000 | [diff] [blame] | 684 | if (pipe( teb->wait_fd ) == -1) server_protocol_perror( "pipe" ); |
Alexandre Julliard | 8859d77 | 2001-03-01 22:13:49 +0000 | [diff] [blame] | 685 | wine_server_send_fd( reply_pipe[1] ); |
Alexandre Julliard | e5dedb1 | 2001-03-08 01:16:41 +0000 | [diff] [blame] | 686 | wine_server_send_fd( teb->wait_fd[1] ); |
Alexandre Julliard | 8859d77 | 2001-03-01 22:13:49 +0000 | [diff] [blame] | 687 | teb->reply_fd = reply_pipe[0]; |
Alexandre Julliard | 714156d | 2002-08-13 18:24:27 +0000 | [diff] [blame] | 688 | close( reply_pipe[1] ); |
Alexandre Julliard | d549f69 | 2000-12-22 02:04:15 +0000 | [diff] [blame] | 689 | |
Alexandre Julliard | 8859d77 | 2001-03-01 22:13:49 +0000 | [diff] [blame] | 690 | /* set close on exec flag */ |
| 691 | fcntl( teb->reply_fd, F_SETFD, 1 ); |
Alexandre Julliard | e5dedb1 | 2001-03-08 01:16:41 +0000 | [diff] [blame] | 692 | fcntl( teb->wait_fd[0], F_SETFD, 1 ); |
| 693 | fcntl( teb->wait_fd[1], F_SETFD, 1 ); |
Alexandre Julliard | d549f69 | 2000-12-22 02:04:15 +0000 | [diff] [blame] | 694 | |
Alexandre Julliard | 8859d77 | 2001-03-01 22:13:49 +0000 | [diff] [blame] | 695 | SERVER_START_REQ( init_thread ) |
| 696 | { |
| 697 | req->unix_pid = getpid(); |
| 698 | req->teb = teb; |
| 699 | req->entry = teb->entry_point; |
| 700 | req->reply_fd = reply_pipe[1]; |
Alexandre Julliard | e5dedb1 | 2001-03-08 01:16:41 +0000 | [diff] [blame] | 701 | req->wait_fd = teb->wait_fd[1]; |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 702 | ret = wine_server_call( req ); |
| 703 | teb->pid = reply->pid; |
| 704 | teb->tid = reply->tid; |
| 705 | version = reply->version; |
| 706 | if (reply->boot) boot_thread_id = teb->tid; |
Alexandre Julliard | 8859d77 | 2001-03-01 22:13:49 +0000 | [diff] [blame] | 707 | else if (boot_thread_id == teb->tid) boot_thread_id = 0; |
Alexandre Julliard | 8859d77 | 2001-03-01 22:13:49 +0000 | [diff] [blame] | 708 | } |
| 709 | SERVER_END_REQ; |
Alexandre Julliard | d90e964 | 2001-02-21 04:21:50 +0000 | [diff] [blame] | 710 | |
Alexandre Julliard | 8859d77 | 2001-03-01 22:13:49 +0000 | [diff] [blame] | 711 | if (ret) server_protocol_error( "init_thread failed with status %x\n", ret ); |
| 712 | if (version != SERVER_PROTOCOL_VERSION) |
Alexandre Julliard | 5fb5456 | 2000-03-08 22:01:02 +0000 | [diff] [blame] | 713 | server_protocol_error( "version mismatch %d/%d.\n" |
| 714 | "Your %s binary was not upgraded correctly,\n" |
Alexandre Julliard | d90e964 | 2001-02-21 04:21:50 +0000 | [diff] [blame] | 715 | "or you have an older one somewhere in your PATH.\n" |
| 716 | "Or maybe the wrong wineserver is still running?\n", |
Alexandre Julliard | 8859d77 | 2001-03-01 22:13:49 +0000 | [diff] [blame] | 717 | version, SERVER_PROTOCOL_VERSION, |
| 718 | (version > SERVER_PROTOCOL_VERSION) ? "wine" : "wineserver" ); |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 719 | } |
| 720 | |
Alexandre Julliard | d549f69 | 2000-12-22 02:04:15 +0000 | [diff] [blame] | 721 | |
Alexandre Julliard | 2fe5777 | 2000-01-25 01:40:27 +0000 | [diff] [blame] | 722 | /*********************************************************************** |
| 723 | * CLIENT_BootDone |
| 724 | * |
| 725 | * Signal that we have finished booting, and set debug level. |
| 726 | */ |
Alexandre Julliard | 8859d77 | 2001-03-01 22:13:49 +0000 | [diff] [blame] | 727 | void CLIENT_BootDone( int debug_level ) |
Alexandre Julliard | 2fe5777 | 2000-01-25 01:40:27 +0000 | [diff] [blame] | 728 | { |
Alexandre Julliard | 67a7499 | 2001-02-27 02:09:16 +0000 | [diff] [blame] | 729 | SERVER_START_REQ( boot_done ) |
Alexandre Julliard | 8611353 | 2000-08-29 03:54:30 +0000 | [diff] [blame] | 730 | { |
Alexandre Julliard | 8611353 | 2000-08-29 03:54:30 +0000 | [diff] [blame] | 731 | req->debug_level = debug_level; |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 732 | wine_server_call( req ); |
Alexandre Julliard | 8611353 | 2000-08-29 03:54:30 +0000 | [diff] [blame] | 733 | } |
| 734 | SERVER_END_REQ; |
Alexandre Julliard | 2fe5777 | 2000-01-25 01:40:27 +0000 | [diff] [blame] | 735 | } |
| 736 | |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 737 | |
| 738 | /*********************************************************************** |
Alexandre Julliard | 2fe5777 | 2000-01-25 01:40:27 +0000 | [diff] [blame] | 739 | * CLIENT_IsBootThread |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 740 | * |
Alexandre Julliard | 2fe5777 | 2000-01-25 01:40:27 +0000 | [diff] [blame] | 741 | * Return TRUE if current thread is the boot thread. |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 742 | */ |
Alexandre Julliard | 2fe5777 | 2000-01-25 01:40:27 +0000 | [diff] [blame] | 743 | int CLIENT_IsBootThread(void) |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 744 | { |
Alexandre Julliard | 2fe5777 | 2000-01-25 01:40:27 +0000 | [diff] [blame] | 745 | return (GetCurrentThreadId() == (DWORD)boot_thread_id); |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 746 | } |