Removed all implementation aspects.
[wine.git] / scheduler / client.c
blobd08652b7eebcd7bf6a4366461f02dff0d2331610
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 static 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 = { sizeof(cmsg), SOL_SOCKET, SCM_RIGHTS, pass_fd };
68 #endif
69 struct msghdr msghdr = { NULL, 0, vec, veclen, };
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 if (pass_fd != -1) /* we have an fd to send */
85 #ifdef HAVE_MSGHDR_ACCRIGHTS
86 msghdr.msg_accrights = (void *)&pass_fd;
87 msghdr.msg_accrightslen = sizeof(pass_fd);
88 #else
89 msghdr.msg_control = &cmsg;
90 msghdr.msg_controllen = sizeof(cmsg);
91 #endif
94 if ((ret = sendmsg( thdb->socket, &msghdr, 0 )) < len)
96 if (ret == -1) perror( "sendmsg" );
97 CLIENT_ProtocolError( "partial msg sent %d/%d\n", ret, len );
99 /* we passed the fd now we can close it */
100 if (pass_fd != -1) close( pass_fd );
104 /***********************************************************************
105 * CLIENT_SendRequest
107 * Send a request to the server.
109 void CLIENT_SendRequest( enum request req, int pass_fd,
110 int n, ... /* arg_1, len_1, etc. */ )
112 struct iovec vec[16];
113 va_list args;
114 int i;
116 n++; /* for vec[0] */
117 assert( n < 16 );
118 va_start( args, n );
119 for (i = 1; i < n; i++)
121 vec[i].iov_base = va_arg( args, void * );
122 vec[i].iov_len = va_arg( args, int );
124 va_end( args );
125 return CLIENT_SendRequest_v( req, pass_fd, vec, n );
129 /***********************************************************************
130 * CLIENT_WaitReply_v
132 * Wait for a reply from the server.
133 * Returns the error code (or 0 if OK).
135 static unsigned int CLIENT_WaitReply_v( int *len, int *passed_fd,
136 struct iovec *vec, int veclen )
138 THDB *thdb = THREAD_Current();
139 int pass_fd = -1;
140 #ifdef HAVE_MSGHDR_ACCRIGHTS
141 struct msghdr msghdr = { NULL, 0, vec, veclen, (void*)&pass_fd, sizeof(int) };
142 #else
143 struct cmsg_fd cmsg = { sizeof(cmsg), SOL_SOCKET, SCM_RIGHTS, -1 };
144 struct msghdr msghdr = { NULL, 0, vec, veclen, &cmsg, sizeof(cmsg), 0 };
145 #endif
146 struct header head;
147 int ret, remaining;
149 assert( veclen > 0 );
150 vec[0].iov_base = &head;
151 vec[0].iov_len = sizeof(head);
153 while ((ret = recvmsg( thdb->socket, &msghdr, 0 )) == -1)
155 if (errno == EINTR) continue;
156 perror("recvmsg");
157 CLIENT_ProtocolError( "recvmsg\n" );
159 if (!ret) CLIENT_Die( thdb ); /* the server closed the connection; time to die... */
161 /* sanity checks */
163 if (ret < sizeof(head))
164 CLIENT_ProtocolError( "partial header received %d/%d\n", ret, sizeof(head) );
166 if ((head.len < sizeof(head)) || (head.len > MAX_MSG_LENGTH))
167 CLIENT_ProtocolError( "header length %d\n", head.len );
169 if (head.seq != thdb->seq++)
170 CLIENT_ProtocolError( "sequence %08x instead of %08x\n", head.seq, thdb->seq - 1 );
172 #ifndef HAVE_MSGHDR_ACCRIGHTS
173 pass_fd = cmsg.fd;
174 #endif
176 if (passed_fd)
178 *passed_fd = pass_fd;
179 pass_fd = -1;
182 if (len) *len = ret - sizeof(head);
183 if (pass_fd != -1) close( pass_fd );
184 remaining = head.len - ret;
185 while (remaining > 0) /* get remaining data */
187 char *bufp, buffer[1024];
188 int addlen, i, iovtot = 0;
190 /* see if any iovs are still incomplete, otherwise drop the rest */
191 for (i = 0; i < veclen && remaining > 0; i++)
193 if (iovtot + vec[i].iov_len > head.len - remaining)
195 addlen = iovtot + vec[i].iov_len - (head.len - remaining);
196 bufp = (char *)vec[i].iov_base + (vec[i].iov_len - addlen);
197 if (addlen > remaining) addlen = remaining;
198 if ((addlen = recv( thdb->socket, bufp, addlen, 0 )) == -1)
200 perror( "recv" );
201 CLIENT_ProtocolError( "recv\n" );
203 if (!addlen) CLIENT_Die( thdb ); /* the server closed the connection; time to die... */
204 if (len) *len += addlen;
205 remaining -= addlen;
207 iovtot += vec[i].iov_len;
210 if (remaining > 0)
211 addlen = remaining < sizeof(buffer) ? remaining : sizeof(buffer);
212 else
213 break;
215 if ((addlen = recv( thdb->socket, buffer, addlen, 0 )) == -1)
217 perror( "recv" );
218 CLIENT_ProtocolError( "recv\n" );
220 if (!addlen) CLIENT_Die( thdb ); /* the server closed the connection; time to die... */
221 remaining -= addlen;
224 if (head.type) SetLastError( head.type );
225 return head.type; /* error code */
229 /***********************************************************************
230 * CLIENT_WaitReply
232 * Wait for a reply from the server.
234 unsigned int CLIENT_WaitReply( int *len, int *passed_fd,
235 int n, ... /* arg_1, len_1, etc. */ )
237 struct iovec vec[16];
238 va_list args;
239 int i;
241 n++; /* for vec[0] */
242 assert( n < 16 );
243 va_start( args, n );
244 for (i = 1; i < n; i++)
246 vec[i].iov_base = va_arg( args, void * );
247 vec[i].iov_len = va_arg( args, int );
249 va_end( args );
250 return CLIENT_WaitReply_v( len, passed_fd, vec, n );
254 /***********************************************************************
255 * CLIENT_WaitSimpleReply
257 * Wait for a simple fixed-length reply from the server.
259 unsigned int CLIENT_WaitSimpleReply( void *reply, int len, int *passed_fd )
261 struct iovec vec[2];
262 unsigned int ret;
263 int got;
265 vec[1].iov_base = reply;
266 vec[1].iov_len = len;
267 ret = CLIENT_WaitReply_v( &got, passed_fd, vec, 2 );
268 if (got != len)
269 CLIENT_ProtocolError( "WaitSimpleReply: len %d != %d\n", len, got );
270 return ret;
274 /***********************************************************************
275 * CLIENT_InitServer
277 * Start the server and create the initial socket pair.
279 int CLIENT_InitServer(void)
281 int fd[2];
282 char buffer[16];
283 extern void create_initial_thread( int fd );
285 if (socketpair( AF_UNIX, SOCK_STREAM, 0, fd ) == -1)
287 perror("socketpair");
288 exit(1);
290 switch(fork())
292 case -1: /* error */
293 perror("fork");
294 exit(1);
295 case 0: /* child */
296 close( fd[0] );
297 sprintf( buffer, "%d", fd[1] );
298 /*#define EXEC_SERVER*/
299 #ifdef EXEC_SERVER
300 execlp( "wineserver", "wineserver", buffer, NULL );
301 execl( "/usr/local/bin/wineserver", "wineserver", buffer, NULL );
302 execl( "./server/wineserver", "wineserver", buffer, NULL );
303 #endif
304 create_initial_thread( fd[1] );
305 exit(0);
306 default: /* parent */
307 close( fd[1] );
308 break;
310 return fd[0];
314 /***********************************************************************
315 * CLIENT_InitThread
317 * Send an init thread request. Return 0 if OK.
319 int CLIENT_InitThread(void)
321 THDB *thdb = THREAD_Current();
322 struct init_thread_request req;
323 struct init_thread_reply reply;
325 req.unix_pid = getpid();
326 CLIENT_SendRequest( REQ_INIT_THREAD, -1, 1, &req, sizeof(req) );
327 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL )) return -1;
328 thdb->process->server_pid = reply.pid;
329 thdb->server_tid = reply.tid;
330 return 0;
334 /***********************************************************************
335 * CLIENT_SetDebug
337 * Send a set debug level request. Return 0 if OK.
339 int CLIENT_SetDebug( int level )
341 CLIENT_SendRequest( REQ_SET_DEBUG, -1, 1, &level, sizeof(level) );
342 return CLIENT_WaitReply( NULL, NULL, 0 );