2 * Client part of the client/server communication
4 * Copyright (C) 1998 Alexandre Julliard
12 #include <sys/types.h>
13 #include <sys/socket.h>
20 #include "server/request.h"
24 /* Some versions of glibc don't define this */
30 /***********************************************************************
33 * Die on protocol errors or socket close
35 static void CLIENT_Die( THDB
*thdb
)
37 close( thdb
->socket
);
41 /***********************************************************************
42 * CLIENT_ProtocolError
44 void CLIENT_ProtocolError( const char *err
, ... )
46 THDB
*thdb
= THREAD_Current();
49 va_start( args
, err
);
50 fprintf( stderr
, "Client protocol error:%p: ", thdb
->server_tid
);
51 vfprintf( stderr
, err
, args
);
57 /***********************************************************************
58 * CLIENT_SendRequest_v
60 * Send a request to the server.
62 static void CLIENT_SendRequest_v( enum request req
, int pass_fd
,
63 struct iovec
*vec
, int veclen
)
65 THDB
*thdb
= THREAD_Current();
66 #ifndef HAVE_MSGHDR_ACCRIGHTS
74 vec
[0].iov_base
= &head
;
75 vec
[0].iov_len
= sizeof(head
);
76 for (i
= len
= 0; i
< veclen
; i
++) len
+= vec
[i
].iov_len
;
78 assert( len
<= MAX_MSG_LENGTH
);
81 head
.seq
= thdb
->seq
++;
83 msghdr
.msg_name
= NULL
;
84 msghdr
.msg_namelen
= 0;
86 msghdr
.msg_iovlen
= veclen
;
88 #ifdef HAVE_MSGHDR_ACCRIGHTS
89 if (pass_fd
!= -1) /* we have an fd to send */
91 msghdr
.msg_accrights
= (void *)&pass_fd
;
92 msghdr
.msg_accrightslen
= sizeof(pass_fd
);
96 msghdr
.msg_accrights
= NULL
;
97 msghdr
.msg_accrightslen
= 0;
99 #else /* HAVE_MSGHDR_ACCRIGHTS */
100 if (pass_fd
!= -1) /* we have an fd to send */
102 cmsg
.len
= sizeof(cmsg
);
103 cmsg
.level
= SOL_SOCKET
;
104 cmsg
.type
= SCM_RIGHTS
;
106 msghdr
.msg_control
= &cmsg
;
107 msghdr
.msg_controllen
= sizeof(cmsg
);
111 msghdr
.msg_control
= NULL
;
112 msghdr
.msg_controllen
= 0;
114 msghdr
.msg_flags
= 0;
115 #endif /* HAVE_MSGHDR_ACCRIGHTS */
117 if ((ret
= sendmsg( thdb
->socket
, &msghdr
, 0 )) < len
)
119 if (ret
== -1) perror( "sendmsg" );
120 CLIENT_ProtocolError( "partial msg sent %d/%d\n", ret
, len
);
122 /* we passed the fd now we can close it */
123 if (pass_fd
!= -1) close( pass_fd
);
127 /***********************************************************************
130 * Send a request to the server.
132 void CLIENT_SendRequest( enum request req
, int pass_fd
,
133 int n
, ... /* arg_1, len_1, etc. */ )
135 struct iovec vec
[16];
139 n
++; /* for vec[0] */
142 for (i
= 1; i
< n
; i
++)
144 vec
[i
].iov_base
= va_arg( args
, void * );
145 vec
[i
].iov_len
= va_arg( args
, int );
148 CLIENT_SendRequest_v( req
, pass_fd
, vec
, n
);
152 /***********************************************************************
155 * Wait for a reply from the server.
156 * Returns the error code (or 0 if OK).
158 static unsigned int CLIENT_WaitReply_v( int *len
, int *passed_fd
,
159 struct iovec
*vec
, int veclen
)
161 THDB
*thdb
= THREAD_Current();
166 #ifdef HAVE_MSGHDR_ACCRIGHTS
167 struct msghdr msghdr
;
169 msghdr
.msg_accrights
= (void *)&pass_fd
;
170 msghdr
.msg_accrightslen
= sizeof(int);
171 #else /* HAVE_MSGHDR_ACCRIGHTS */
172 struct msghdr msghdr
;
175 cmsg
.len
= sizeof(cmsg
);
176 cmsg
.level
= SOL_SOCKET
;
177 cmsg
.type
= SCM_RIGHTS
;
179 msghdr
.msg_control
= &cmsg
;
180 msghdr
.msg_controllen
= sizeof(cmsg
);
181 msghdr
.msg_flags
= 0;
182 #endif /* HAVE_MSGHDR_ACCRIGHTS */
184 msghdr
.msg_name
= NULL
;
185 msghdr
.msg_namelen
= 0;
186 msghdr
.msg_iov
= vec
;
187 msghdr
.msg_iovlen
= veclen
;
189 assert( veclen
> 0 );
190 vec
[0].iov_base
= &head
;
191 vec
[0].iov_len
= sizeof(head
);
193 while ((ret
= recvmsg( thdb
->socket
, &msghdr
, 0 )) == -1)
195 if (errno
== EINTR
) continue;
197 CLIENT_ProtocolError( "recvmsg\n" );
199 if (!ret
) CLIENT_Die( thdb
); /* the server closed the connection; time to die... */
203 if (ret
< sizeof(head
))
204 CLIENT_ProtocolError( "partial header received %d/%d\n", ret
, sizeof(head
) );
206 if ((head
.len
< sizeof(head
)) || (head
.len
> MAX_MSG_LENGTH
))
207 CLIENT_ProtocolError( "header length %d\n", head
.len
);
209 if (head
.seq
!= thdb
->seq
++)
210 CLIENT_ProtocolError( "sequence %08x instead of %08x\n", head
.seq
, thdb
->seq
- 1 );
212 #ifndef HAVE_MSGHDR_ACCRIGHTS
218 *passed_fd
= pass_fd
;
222 if (len
) *len
= ret
- sizeof(head
);
223 if (pass_fd
!= -1) close( pass_fd
);
224 remaining
= head
.len
- ret
;
225 while (remaining
> 0) /* get remaining data */
227 char *bufp
, buffer
[1024];
228 int addlen
, i
, iovtot
= 0;
230 /* see if any iovs are still incomplete, otherwise drop the rest */
231 for (i
= 0; i
< veclen
&& remaining
> 0; i
++)
233 if (iovtot
+ vec
[i
].iov_len
> head
.len
- remaining
)
235 addlen
= iovtot
+ vec
[i
].iov_len
- (head
.len
- remaining
);
236 bufp
= (char *)vec
[i
].iov_base
+ (vec
[i
].iov_len
- addlen
);
237 if (addlen
> remaining
) addlen
= remaining
;
238 if ((addlen
= recv( thdb
->socket
, bufp
, addlen
, 0 )) == -1)
241 CLIENT_ProtocolError( "recv\n" );
243 if (!addlen
) CLIENT_Die( thdb
); /* the server closed the connection; time to die... */
244 if (len
) *len
+= addlen
;
247 iovtot
+= vec
[i
].iov_len
;
251 addlen
= remaining
< sizeof(buffer
) ? remaining
: sizeof(buffer
);
255 if ((addlen
= recv( thdb
->socket
, buffer
, addlen
, 0 )) == -1)
258 CLIENT_ProtocolError( "recv\n" );
260 if (!addlen
) CLIENT_Die( thdb
); /* the server closed the connection; time to die... */
264 if (head
.type
) SetLastError( head
.type
);
265 return head
.type
; /* error code */
269 /***********************************************************************
272 * Wait for a reply from the server.
274 unsigned int CLIENT_WaitReply( int *len
, int *passed_fd
,
275 int n
, ... /* arg_1, len_1, etc. */ )
277 struct iovec vec
[16];
281 n
++; /* for vec[0] */
284 for (i
= 1; i
< n
; i
++)
286 vec
[i
].iov_base
= va_arg( args
, void * );
287 vec
[i
].iov_len
= va_arg( args
, int );
290 return CLIENT_WaitReply_v( len
, passed_fd
, vec
, n
);
294 /***********************************************************************
295 * CLIENT_WaitSimpleReply
297 * Wait for a simple fixed-length reply from the server.
299 unsigned int CLIENT_WaitSimpleReply( void *reply
, int len
, int *passed_fd
)
305 vec
[1].iov_base
= reply
;
306 vec
[1].iov_len
= len
;
307 ret
= CLIENT_WaitReply_v( &got
, passed_fd
, vec
, 2 );
309 CLIENT_ProtocolError( "WaitSimpleReply: len %d != %d\n", len
, got
);
314 /***********************************************************************
317 * Start the server and create the initial socket pair.
319 int CLIENT_InitServer(void)
323 extern void create_initial_thread( int fd
);
325 if (socketpair( AF_UNIX
, SOCK_STREAM
, 0, fd
) == -1)
327 perror("socketpair");
338 default: /* parent */
340 sprintf( buffer
, "%d", fd
[1] );
341 /*#define EXEC_SERVER*/
343 execlp( "wineserver", "wineserver", buffer
, NULL
);
344 execl( "/usr/local/bin/wineserver", "wineserver", buffer
, NULL
);
345 execl( "./server/wineserver", "wineserver", buffer
, NULL
);
347 create_initial_thread( fd
[1] );
354 /***********************************************************************
357 * Send an init thread request. Return 0 if OK.
359 int CLIENT_InitThread(void)
361 THDB
*thdb
= THREAD_Current();
362 struct init_thread_request req
;
363 struct init_thread_reply reply
;
365 req
.unix_pid
= getpid();
366 req
.teb
= &thdb
->teb
;
367 CLIENT_SendRequest( REQ_INIT_THREAD
, -1, 1, &req
, sizeof(req
) );
368 if (CLIENT_WaitSimpleReply( &reply
, sizeof(reply
), NULL
)) return -1;
369 thdb
->process
->server_pid
= reply
.pid
;
370 thdb
->server_tid
= reply
.tid
;
375 /***********************************************************************
378 * Send a set debug level request. Return 0 if OK.
380 int CLIENT_SetDebug( int level
)
382 CLIENT_SendRequest( REQ_SET_DEBUG
, -1, 1, &level
, sizeof(level
) );
383 return CLIENT_WaitReply( NULL
, NULL
, 0 );
386 /***********************************************************************
387 * CLIENT_DebuggerRequest
389 * Send a debugger support request. Return 0 if OK.
391 int CLIENT_DebuggerRequest( int op
)
393 struct debugger_request req
;
395 CLIENT_SendRequest( REQ_DEBUGGER
, -1, 1, &req
, sizeof(req
) );
396 return CLIENT_WaitReply( NULL
, NULL
, 0 );