2 * Client part of the client/server communication
4 * Copyright (C) 1998 Alexandre Julliard
12 #include <sys/types.h>
13 #include <sys/socket.h>
19 #include "server/request.h"
23 /* Some versions of glibc don't define this */
29 /***********************************************************************
30 * CLIENT_ProtocolError
32 static void CLIENT_ProtocolError( const char *err, ... )
34 THDB *thdb = THREAD_Current();
37 va_start( args, err );
38 fprintf( stderr, "Client protocol error:%p: ", thdb->server_tid );
39 vfprintf( stderr, err, args );
45 /***********************************************************************
46 * CLIENT_SendRequest_v
48 * Send a request to the server.
50 static void CLIENT_SendRequest_v( enum request req, int pass_fd,
51 struct iovec *vec, int veclen )
53 THDB *thdb = THREAD_Current();
54 #ifndef HAVE_MSGHDR_ACCRIGHTS
55 struct cmsg_fd cmsg = { sizeof(cmsg), SOL_SOCKET, SCM_RIGHTS, pass_fd };
57 struct msghdr msghdr = { NULL, 0, vec, veclen, };
62 vec[0].iov_base = &head;
63 vec[0].iov_len = sizeof(head);
64 for (i = len = 0; i < veclen; i++) len += vec[i].iov_len;
66 assert( len <= MAX_MSG_LENGTH );
69 head.seq = thdb->seq++;
71 if (pass_fd != -1) /* we have an fd to send */
73 #ifdef HAVE_MSGHDR_ACCRIGHTS
74 msghdr.msg_accrights = (void *)&pass_fd;
75 msghdr.msg_accrightslen = sizeof(pass_fd);
77 msghdr.msg_control = &cmsg;
78 msghdr.msg_controllen = sizeof(cmsg);
82 if ((ret = sendmsg( thdb->socket, &msghdr, 0 )) < len)
84 if (ret == -1) perror( "sendmsg" );
85 CLIENT_ProtocolError( "partial msg sent %d/%d\n", ret, len );
87 /* we passed the fd now we can close it */
88 if (pass_fd != -1) close( pass_fd );
92 /***********************************************************************
95 * Send a request to the server.
97 void CLIENT_SendRequest( enum request req, int pass_fd,
98 int n, ... /* arg_1, len_1, etc. */ )
100 struct iovec vec[16];
104 n++; /* for vec[0] */
107 for (i = 1; i < n; i++)
109 vec[i].iov_base = va_arg( args, void * );
110 vec[i].iov_len = va_arg( args, int );
113 return CLIENT_SendRequest_v( req, pass_fd, vec, n );
117 /***********************************************************************
120 * Wait for a reply from the server.
121 * Returns the error code (or 0 if OK).
123 static unsigned int CLIENT_WaitReply_v( int *len, int *passed_fd,
124 struct iovec *vec, int veclen )
126 THDB *thdb = THREAD_Current();
128 #ifdef HAVE_MSGHDR_ACCRIGHTS
129 struct msghdr msghdr = { NULL, 0, vec, veclen, (void*)&pass_fd, sizeof(int) };
131 struct cmsg_fd cmsg = { sizeof(cmsg), SOL_SOCKET, SCM_RIGHTS, -1 };
132 struct msghdr msghdr = { NULL, 0, vec, veclen, &cmsg, sizeof(cmsg), 0 };
137 assert( veclen > 0 );
138 vec[0].iov_base = &head;
139 vec[0].iov_len = sizeof(head);
141 while ((ret = recvmsg( thdb->socket, &msghdr, 0 )) == -1)
143 if (errno == EINTR) continue;
145 CLIENT_ProtocolError( "recvmsg\n" );
147 if (!ret) ExitThread(1); /* the server closed the connection; time to die... */
151 if (ret < sizeof(head))
152 CLIENT_ProtocolError( "partial header received %d/%d\n", ret, sizeof(head) );
154 if ((head.len < sizeof(head)) || (head.len > MAX_MSG_LENGTH))
155 CLIENT_ProtocolError( "header length %d\n", head.len );
157 if (head.seq != thdb->seq++)
158 CLIENT_ProtocolError( "sequence %08x instead of %08x\n", head.seq, thdb->seq - 1 );
160 #ifndef HAVE_MSGHDR_ACCRIGHTS
164 if (head.type != ERROR_SUCCESS)
166 SetLastError( head.type );
170 *passed_fd = pass_fd;
174 if (len) *len = ret - sizeof(head);
175 if (pass_fd != -1) close( pass_fd );
176 remaining = head.len - ret;
177 while (remaining > 0) /* drop remaining data */
180 int len = remaining < sizeof(buffer) ? remaining : sizeof(buffer);
181 if ((len = recv( thdb->socket, buffer, len, 0 )) == -1)
184 CLIENT_ProtocolError( "recv\n" );
186 if (!len) ExitThread(1); /* the server closed the connection; time to die... */
190 return head.type; /* error code */
194 /***********************************************************************
197 * Wait for a reply from the server.
199 unsigned int CLIENT_WaitReply( int *len, int *passed_fd,
200 int n, ... /* arg_1, len_1, etc. */ )
202 struct iovec vec[16];
206 n++; /* for vec[0] */
209 for (i = 1; i < n; i++)
211 vec[i].iov_base = va_arg( args, void * );
212 vec[i].iov_len = va_arg( args, int );
215 return CLIENT_WaitReply_v( len, passed_fd, vec, n );
219 /***********************************************************************
220 * CLIENT_WaitSimpleReply
222 * Wait for a simple fixed-length reply from the server.
224 unsigned int CLIENT_WaitSimpleReply( void *reply, int len, int *passed_fd )
230 vec[1].iov_base = reply;
231 vec[1].iov_len = len;
232 ret = CLIENT_WaitReply_v( &got, passed_fd, vec, 2 );
234 CLIENT_ProtocolError( "WaitSimpleReply: len %d != %d\n", len, got );
239 /***********************************************************************
242 * Send a new thread request.
244 int CLIENT_NewThread( THDB *thdb, int *thandle, int *phandle )
246 struct new_thread_request request;
247 struct new_thread_reply reply;
249 extern BOOL32 THREAD_InitDone;
250 extern void server_init( int fd );
251 extern void select_loop(void);
253 if (!THREAD_InitDone) /* first thread -> start the server */
258 if (socketpair( AF_UNIX, SOCK_STREAM, 0, tmpfd ) == -1)
260 perror("socketpair");
270 sprintf( buffer, "%d", tmpfd[1] );
271 /*#define EXEC_SERVER*/
273 execlp( "wineserver", "wineserver", buffer, NULL );
274 execl( "/usr/local/bin/wineserver", "wineserver", buffer, NULL );
275 execl( "./server/wineserver", "wineserver", buffer, NULL );
277 server_init( tmpfd[1] );
280 default: /* parent */
282 SET_CUR_THREAD( thdb );
283 THREAD_InitDone = TRUE;
284 thdb->socket = tmpfd[0];
289 if (socketpair( AF_UNIX, SOCK_STREAM, 0, fd ) == -1)
291 SetLastError( ERROR_TOO_MANY_OPEN_FILES ); /* FIXME */
295 request.pid = thdb->process->server_pid;
296 CLIENT_SendRequest( REQ_NEW_THREAD, fd[1], 1, &request, sizeof(request) );
297 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL )) goto error;
298 thdb->server_tid = reply.tid;
299 thdb->process->server_pid = reply.pid;
300 if (thdb->socket != -1) close( thdb->socket );
301 thdb->socket = fd[0];
302 thdb->seq = 0; /* reset the sequence number for the new fd */
303 fcntl( fd[0], F_SETFD, 1 ); /* set close on exec flag */
305 if (thandle) *thandle = reply.thandle;
306 else if (reply.thandle != -1) CLIENT_CloseHandle( reply.thandle );
307 if (phandle) *phandle = reply.phandle;
308 else if (reply.phandle != -1) CLIENT_CloseHandle( reply.phandle );
317 /***********************************************************************
320 * Send an init thread request. Return 0 if OK.
322 int CLIENT_InitThread(void)
324 THDB *thdb = THREAD_Current();
325 struct init_thread_request init;
326 int len = strlen( thdb->process->env_db->cmd_line );
328 init.unix_pid = getpid();
329 len = MIN( len, MAX_MSG_LENGTH - sizeof(init) );
331 CLIENT_SendRequest( REQ_INIT_THREAD, -1, 2,
333 thdb->process->env_db->cmd_line, len );
334 return CLIENT_WaitReply( NULL, NULL, 0 );
338 /***********************************************************************
341 * Send a set debug level request. Return 0 if OK.
343 int CLIENT_SetDebug( int level )
345 CLIENT_SendRequest( REQ_SET_DEBUG, -1, 1, &level, sizeof(level) );
346 return CLIENT_WaitReply( NULL, NULL, 0 );
349 /***********************************************************************
352 * Send a close handle request. Return 0 if OK.
354 int CLIENT_CloseHandle( int handle )
356 CLIENT_SendRequest( REQ_CLOSE_HANDLE, -1, 1, &handle, sizeof(handle) );
357 return CLIENT_WaitReply( NULL, NULL, 0 );
360 /***********************************************************************
361 * CLIENT_DuplicateHandle
363 * Send a duplicate handle request. Return 0 if OK.
365 int CLIENT_DuplicateHandle( int src_process, int src_handle, int dst_process, int dst_handle,
366 DWORD access, BOOL32 inherit, DWORD options )
368 struct dup_handle_request req;
369 struct dup_handle_reply reply;
371 req.src_process = src_process;
372 req.src_handle = src_handle;
373 req.dst_process = dst_process;
374 req.dst_handle = dst_handle;
376 req.inherit = inherit;
377 req.options = options;
379 CLIENT_SendRequest( REQ_DUP_HANDLE, -1, 1, &req, sizeof(req) );
380 CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL );
385 /***********************************************************************
388 * Open a handle to a process.
390 int CLIENT_OpenProcess( void *pid, DWORD access, BOOL32 inherit )
392 struct open_process_request req;
393 struct open_process_reply reply;
397 req.inherit = inherit;
399 CLIENT_SendRequest( REQ_OPEN_PROCESS, -1, 1, &req, sizeof(req) );
400 CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL );