Check when backtracking the stack if frames are correct (readable).
[wine/testsucceed.git] / scheduler / client.c
blob411427f5354143094daaf7adb8446b3be25f3b49
1 /*
2 * Client part of the client/server communication
4 * Copyright (C) 1998 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include <errno.h>
9 #include <fcntl.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include <sys/types.h>
13 #include <sys/socket.h>
14 #include <sys/uio.h>
15 #include <unistd.h>
16 #include <stdarg.h>
18 #include "process.h"
19 #include "thread.h"
20 #include "server/request.h"
21 #include "server.h"
22 #include "winerror.h"
24 /* Some versions of glibc don't define this */
25 #ifndef SCM_RIGHTS
26 #define SCM_RIGHTS 1
27 #endif
30 /***********************************************************************
31 * CLIENT_Die
33 * Die on protocol errors or socket close
35 static void CLIENT_Die( THDB *thdb )
37 close( thdb->socket );
38 SYSDEPS_ExitThread();
41 /***********************************************************************
42 * CLIENT_ProtocolError
44 void CLIENT_ProtocolError( const char *err, ... )
46 THDB *thdb = THREAD_Current();
47 va_list args;
49 va_start( args, err );
50 fprintf( stderr, "Client protocol error:%p: ", thdb->server_tid );
51 vfprintf( stderr, err, args );
52 va_end( args );
53 CLIENT_Die( thdb );
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
67 struct cmsg_fd cmsg;
68 #endif
69 struct msghdr msghdr;
70 struct header head;
71 int i, ret, len;
73 assert( veclen > 0 );
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 );
79 head.type = req;
80 head.len = len;
81 head.seq = thdb->seq++;
83 msghdr.msg_name = NULL;
84 msghdr.msg_namelen = 0;
85 msghdr.msg_iov = vec;
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);
94 else
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;
105 cmsg.fd = pass_fd;
106 msghdr.msg_control = &cmsg;
107 msghdr.msg_controllen = sizeof(cmsg);
109 else
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 /***********************************************************************
128 * CLIENT_SendRequest
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];
136 va_list args;
137 int i;
139 n++; /* for vec[0] */
140 assert( n < 16 );
141 va_start( args, n );
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 );
147 va_end( args );
148 CLIENT_SendRequest_v( req, pass_fd, vec, n );
152 /***********************************************************************
153 * CLIENT_WaitReply_v
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();
162 int pass_fd = -1;
163 struct header head;
164 int ret, remaining;
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;
173 struct cmsg_fd cmsg;
175 cmsg.len = sizeof(cmsg);
176 cmsg.level = SOL_SOCKET;
177 cmsg.type = SCM_RIGHTS;
178 cmsg.fd = -1;
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;
196 perror("recvmsg");
197 CLIENT_ProtocolError( "recvmsg\n" );
199 if (!ret) CLIENT_Die( thdb ); /* the server closed the connection; time to die... */
201 /* sanity checks */
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
213 pass_fd = cmsg.fd;
214 #endif
216 if (passed_fd)
218 *passed_fd = pass_fd;
219 pass_fd = -1;
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)
240 perror( "recv" );
241 CLIENT_ProtocolError( "recv\n" );
243 if (!addlen) CLIENT_Die( thdb ); /* the server closed the connection; time to die... */
244 if (len) *len += addlen;
245 remaining -= addlen;
247 iovtot += vec[i].iov_len;
250 if (remaining > 0)
251 addlen = remaining < sizeof(buffer) ? remaining : sizeof(buffer);
252 else
253 break;
255 if ((addlen = recv( thdb->socket, buffer, addlen, 0 )) == -1)
257 perror( "recv" );
258 CLIENT_ProtocolError( "recv\n" );
260 if (!addlen) CLIENT_Die( thdb ); /* the server closed the connection; time to die... */
261 remaining -= addlen;
264 if (head.type) SetLastError( head.type );
265 return head.type; /* error code */
269 /***********************************************************************
270 * CLIENT_WaitReply
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];
278 va_list args;
279 int i;
281 n++; /* for vec[0] */
282 assert( n < 16 );
283 va_start( args, n );
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 );
289 va_end( args );
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 )
301 struct iovec vec[2];
302 unsigned int ret;
303 int got;
305 vec[1].iov_base = reply;
306 vec[1].iov_len = len;
307 ret = CLIENT_WaitReply_v( &got, passed_fd, vec, 2 );
308 if (got != len)
309 CLIENT_ProtocolError( "WaitSimpleReply: len %d != %d\n", len, got );
310 return ret;
314 /***********************************************************************
315 * CLIENT_InitServer
317 * Start the server and create the initial socket pair.
319 int CLIENT_InitServer(void)
321 int fd[2];
322 char buffer[16];
323 extern void create_initial_thread( int fd );
325 if (socketpair( AF_UNIX, SOCK_STREAM, 0, fd ) == -1)
327 perror("socketpair");
328 exit(1);
330 switch(fork())
332 case -1: /* error */
333 perror("fork");
334 exit(1);
335 case 0: /* child */
336 close( fd[1] );
337 break;
338 default: /* parent */
339 close( fd[0] );
340 sprintf( buffer, "%d", fd[1] );
341 /*#define EXEC_SERVER*/
342 #ifdef EXEC_SERVER
343 execlp( "wineserver", "wineserver", buffer, NULL );
344 execl( "/usr/local/bin/wineserver", "wineserver", buffer, NULL );
345 execl( "./server/wineserver", "wineserver", buffer, NULL );
346 #endif
347 create_initial_thread( fd[1] );
348 exit(0);
350 return fd[0];
354 /***********************************************************************
355 * CLIENT_InitThread
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;
371 return 0;
375 /***********************************************************************
376 * CLIENT_SetDebug
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;
394 req.op = op;
395 CLIENT_SendRequest( REQ_DEBUGGER, -1, 1, &req, sizeof(req) );
396 return CLIENT_WaitReply( NULL, NULL, 0 );