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
166 *passed_fd
= pass_fd
;
170 if (len
) *len
= ret
- sizeof(head
);
171 if (pass_fd
!= -1) close( pass_fd
);
172 remaining
= head
.len
- ret
;
173 while (remaining
> 0) /* get remaining data */
175 char *bufp
, buffer
[1024];
176 int addlen
, i
, iovtot
= 0;
178 /* see if any iovs are still incomplete, otherwise drop the rest */
179 for (i
= 0; i
< veclen
&& remaining
> 0; i
++)
181 if (iovtot
+ vec
[i
].iov_len
> head
.len
- remaining
)
183 addlen
= iovtot
+ vec
[i
].iov_len
- (head
.len
- remaining
);
184 bufp
= (char *)vec
[i
].iov_base
+ (vec
[i
].iov_len
- addlen
);
185 if (addlen
> remaining
) addlen
= remaining
;
186 if ((addlen
= recv( thdb
->socket
, bufp
, addlen
, 0 )) == -1)
189 CLIENT_ProtocolError( "recv\n" );
191 if (!addlen
) ExitThread(1); /* the server closed the connection; time to die... */
192 if (len
) *len
+= addlen
;
195 iovtot
+= vec
[i
].iov_len
;
199 addlen
= remaining
< sizeof(buffer
) ? remaining
: sizeof(buffer
);
203 if ((addlen
= recv( thdb
->socket
, buffer
, addlen
, 0 )) == -1)
206 CLIENT_ProtocolError( "recv\n" );
208 if (!addlen
) ExitThread(1); /* the server closed the connection; time to die... */
212 SetLastError( head
.type
);
213 return head
.type
; /* error code */
217 /***********************************************************************
220 * Wait for a reply from the server.
222 unsigned int CLIENT_WaitReply( int *len
, int *passed_fd
,
223 int n
, ... /* arg_1, len_1, etc. */ )
225 struct iovec vec
[16];
229 n
++; /* for vec[0] */
232 for (i
= 1; i
< n
; i
++)
234 vec
[i
].iov_base
= va_arg( args
, void * );
235 vec
[i
].iov_len
= va_arg( args
, int );
238 return CLIENT_WaitReply_v( len
, passed_fd
, vec
, n
);
242 /***********************************************************************
243 * CLIENT_WaitSimpleReply
245 * Wait for a simple fixed-length reply from the server.
247 unsigned int CLIENT_WaitSimpleReply( void *reply
, int len
, int *passed_fd
)
253 vec
[1].iov_base
= reply
;
254 vec
[1].iov_len
= len
;
255 ret
= CLIENT_WaitReply_v( &got
, passed_fd
, vec
, 2 );
257 CLIENT_ProtocolError( "WaitSimpleReply: len %d != %d\n", len
, got
);
262 /***********************************************************************
265 * Send a new thread request.
267 int CLIENT_NewThread( THDB
*thdb
, int *thandle
, int *phandle
)
269 struct new_thread_request request
;
270 struct new_thread_reply reply
;
272 extern BOOL32 THREAD_InitDone
;
273 extern void server_init( int fd
);
274 extern void select_loop(void);
276 if (!THREAD_InitDone
) /* first thread -> start the server */
281 if (socketpair( AF_UNIX
, SOCK_STREAM
, 0, tmpfd
) == -1)
283 perror("socketpair");
293 sprintf( buffer
, "%d", tmpfd
[1] );
294 /*#define EXEC_SERVER*/
296 execlp( "wineserver", "wineserver", buffer
, NULL
);
297 execl( "/usr/local/bin/wineserver", "wineserver", buffer
, NULL
);
298 execl( "./server/wineserver", "wineserver", buffer
, NULL
);
300 server_init( tmpfd
[1] );
303 default: /* parent */
305 SET_CUR_THREAD( thdb
);
306 THREAD_InitDone
= TRUE
;
307 thdb
->socket
= tmpfd
[0];
312 if (socketpair( AF_UNIX
, SOCK_STREAM
, 0, fd
) == -1)
314 SetLastError( ERROR_TOO_MANY_OPEN_FILES
); /* FIXME */
318 request
.pid
= thdb
->process
->server_pid
;
319 CLIENT_SendRequest( REQ_NEW_THREAD
, fd
[1], 1, &request
, sizeof(request
) );
320 if (CLIENT_WaitSimpleReply( &reply
, sizeof(reply
), NULL
)) goto error
;
321 thdb
->server_tid
= reply
.tid
;
322 thdb
->process
->server_pid
= reply
.pid
;
323 if (thdb
->socket
!= -1) close( thdb
->socket
);
324 thdb
->socket
= fd
[0];
325 thdb
->seq
= 0; /* reset the sequence number for the new fd */
326 fcntl( fd
[0], F_SETFD
, 1 ); /* set close on exec flag */
328 if (thandle
) *thandle
= reply
.thandle
;
329 else if (reply
.thandle
!= -1) CLIENT_CloseHandle( reply
.thandle
);
330 if (phandle
) *phandle
= reply
.phandle
;
331 else if (reply
.phandle
!= -1) CLIENT_CloseHandle( reply
.phandle
);
340 /***********************************************************************
343 * Send an init thread request. Return 0 if OK.
345 int CLIENT_InitThread(void)
347 THDB
*thdb
= THREAD_Current();
348 struct init_thread_request init
;
349 int len
= strlen( thdb
->process
->env_db
->cmd_line
);
351 init
.unix_pid
= getpid();
352 len
= MIN( len
, MAX_MSG_LENGTH
- sizeof(init
) );
354 CLIENT_SendRequest( REQ_INIT_THREAD
, -1, 2,
356 thdb
->process
->env_db
->cmd_line
, len
);
357 return CLIENT_WaitReply( NULL
, NULL
, 0 );
361 /***********************************************************************
364 * Send a set debug level request. Return 0 if OK.
366 int CLIENT_SetDebug( int level
)
368 CLIENT_SendRequest( REQ_SET_DEBUG
, -1, 1, &level
, sizeof(level
) );
369 return CLIENT_WaitReply( NULL
, NULL
, 0 );
372 /***********************************************************************
375 * Send a close handle request. Return 0 if OK.
377 int CLIENT_CloseHandle( int handle
)
379 CLIENT_SendRequest( REQ_CLOSE_HANDLE
, -1, 1, &handle
, sizeof(handle
) );
380 return CLIENT_WaitReply( NULL
, NULL
, 0 );
383 /***********************************************************************
384 * CLIENT_DuplicateHandle
386 * Send a duplicate handle request. Return 0 if OK.
388 int CLIENT_DuplicateHandle( int src_process
, int src_handle
, int dst_process
, int dst_handle
,
389 DWORD access
, BOOL32 inherit
, DWORD options
)
391 struct dup_handle_request req
;
392 struct dup_handle_reply reply
;
394 req
.src_process
= src_process
;
395 req
.src_handle
= src_handle
;
396 req
.dst_process
= dst_process
;
397 req
.dst_handle
= dst_handle
;
399 req
.inherit
= inherit
;
400 req
.options
= options
;
402 CLIENT_SendRequest( REQ_DUP_HANDLE
, -1, 1, &req
, sizeof(req
) );
403 CLIENT_WaitSimpleReply( &reply
, sizeof(reply
), NULL
);
408 /***********************************************************************
411 * Open a handle to a process.
413 int CLIENT_OpenProcess( void *pid
, DWORD access
, BOOL32 inherit
)
415 struct open_process_request req
;
416 struct open_process_reply reply
;
420 req
.inherit
= inherit
;
422 CLIENT_SendRequest( REQ_OPEN_PROCESS
, -1, 1, &req
, sizeof(req
) );
423 CLIENT_WaitSimpleReply( &reply
, sizeof(reply
), NULL
);