Fixed CHECK_STRING display.
[wine/multimedia.git] / scheduler / client.c
blob24fdfa4ebce47ff32e5c9a4cbf8f4cfbde1343a2
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 <stdio.h>
10 #include <string.h>
11 #include <sys/types.h>
12 #include <sys/socket.h>
13 #include <sys/uio.h>
14 #include <unistd.h>
16 #include "process.h"
17 #include "thread.h"
18 #include "server/request.h"
19 #include "server.h"
20 #include "winerror.h"
22 /* Some versions of glibc don't define this */
23 #ifndef SCM_RIGHTS
24 #define SCM_RIGHTS 1
25 #endif
28 /***********************************************************************
29 * CLIENT_ProtocolError
31 static void CLIENT_ProtocolError( const char *err, ... )
33 THDB *thdb = THREAD_Current();
34 va_list args;
36 va_start( args, err );
37 fprintf( stderr, "Client protocol error:%p: ", thdb->server_tid );
38 vfprintf( stderr, err, args );
39 va_end( args );
40 ExitThread(1);
44 /***********************************************************************
45 * CLIENT_SendRequest_v
47 * Send a request to the server.
49 static void CLIENT_SendRequest_v( enum request req, int pass_fd,
50 struct iovec *vec, int veclen )
52 THDB *thdb = THREAD_Current();
53 #ifndef HAVE_MSGHDR_ACCRIGHTS
54 struct cmsg_fd cmsg = { sizeof(cmsg), SOL_SOCKET, SCM_RIGHTS, pass_fd };
55 #endif
56 struct msghdr msghdr = { NULL, 0, vec, veclen, };
57 struct header head;
58 int i, ret, len;
60 assert( veclen > 0 );
61 vec[0].iov_base = &head;
62 vec[0].iov_len = sizeof(head);
63 for (i = len = 0; i < veclen; i++) len += vec[i].iov_len;
65 assert( len <= MAX_MSG_LENGTH );
66 head.type = req;
67 head.len = len;
68 head.seq = thdb->seq++;
70 if (pass_fd != -1) /* we have an fd to send */
72 #ifdef HAVE_MSGHDR_ACCRIGHTS
73 msghdr.msg_accrights = (void *)&pass_fd;
74 msghdr.msg_accrightslen = sizeof(pass_fd);
75 #else
76 msghdr.msg_control = &cmsg;
77 msghdr.msg_controllen = sizeof(cmsg);
78 #endif
81 if ((ret = sendmsg( thdb->socket, &msghdr, 0 )) < len)
83 if (ret == -1) perror( "sendmsg" );
84 CLIENT_ProtocolError( "partial msg sent %d/%d\n", ret, len );
86 /* we passed the fd now we can close it */
87 if (pass_fd != -1) close( pass_fd );
91 /***********************************************************************
92 * CLIENT_SendRequest
94 * Send a request to the server.
96 void CLIENT_SendRequest( enum request req, int pass_fd,
97 int n, ... /* arg_1, len_1, etc. */ )
99 struct iovec vec[16];
100 va_list args;
101 int i;
103 n++; /* for vec[0] */
104 assert( n < 16 );
105 va_start( args, n );
106 for (i = 1; i < n; i++)
108 vec[i].iov_base = va_arg( args, void * );
109 vec[i].iov_len = va_arg( args, int );
111 va_end( args );
112 return CLIENT_SendRequest_v( req, pass_fd, vec, n );
116 /***********************************************************************
117 * CLIENT_WaitReply_v
119 * Wait for a reply from the server.
120 * Returns the error code (or 0 if OK).
122 static unsigned int CLIENT_WaitReply_v( int *len, int *passed_fd,
123 struct iovec *vec, int veclen )
125 THDB *thdb = THREAD_Current();
126 int pass_fd = -1;
127 #ifdef HAVE_MSGHDR_ACCRIGHTS
128 struct msghdr msghdr = { NULL, 0, vec, veclen, (void*)&pass_fd, sizeof(int) };
129 #else
130 struct cmsg_fd cmsg = { sizeof(cmsg), SOL_SOCKET, SCM_RIGHTS, -1 };
131 struct msghdr msghdr = { NULL, 0, vec, veclen, &cmsg, sizeof(cmsg), 0 };
132 #endif
133 struct header head;
134 int ret, remaining;
136 assert( veclen > 0 );
137 vec[0].iov_base = &head;
138 vec[0].iov_len = sizeof(head);
140 while ((ret = recvmsg( thdb->socket, &msghdr, 0 )) == -1)
142 if (errno == EINTR) continue;
143 perror("recvmsg");
144 CLIENT_ProtocolError( "recvmsg\n" );
146 if (!ret) ExitThread(1); /* the server closed the connection; time to die... */
148 /* sanity checks */
150 if (ret < sizeof(head))
151 CLIENT_ProtocolError( "partial header received %d/%d\n", ret, sizeof(head) );
153 if ((head.len < sizeof(head)) || (head.len > MAX_MSG_LENGTH))
154 CLIENT_ProtocolError( "header length %d\n", head.len );
156 if (head.seq != thdb->seq++)
157 CLIENT_ProtocolError( "sequence %08x instead of %08x\n", head.seq, thdb->seq - 1 );
159 #ifndef HAVE_MSGHDR_ACCRIGHTS
160 pass_fd = cmsg.fd;
161 #endif
163 if (head.type != ERROR_SUCCESS)
165 SetLastError( head.type );
167 else if (passed_fd)
169 *passed_fd = pass_fd;
170 pass_fd = -1;
173 if (len) *len = ret - sizeof(head);
174 if (pass_fd != -1) close( pass_fd );
175 remaining = head.len - ret;
176 while (remaining > 0) /* drop remaining data */
178 char buffer[1024];
179 int len = remaining < sizeof(buffer) ? remaining : sizeof(buffer);
180 if ((len = recv( thdb->socket, buffer, len, 0 )) == -1)
182 perror( "recv" );
183 CLIENT_ProtocolError( "recv\n" );
185 if (!len) ExitThread(1); /* the server closed the connection; time to die... */
186 remaining -= len;
189 return head.type; /* error code */
193 /***********************************************************************
194 * CLIENT_WaitReply
196 * Wait for a reply from the server.
198 unsigned int CLIENT_WaitReply( int *len, int *passed_fd,
199 int n, ... /* arg_1, len_1, etc. */ )
201 struct iovec vec[16];
202 va_list args;
203 int i;
205 n++; /* for vec[0] */
206 assert( n < 16 );
207 va_start( args, n );
208 for (i = 1; i < n; i++)
210 vec[i].iov_base = va_arg( args, void * );
211 vec[i].iov_len = va_arg( args, int );
213 va_end( args );
214 return CLIENT_WaitReply_v( len, passed_fd, vec, n );
218 /***********************************************************************
219 * CLIENT_WaitSimpleReply
221 * Wait for a simple fixed-length reply from the server.
223 unsigned int CLIENT_WaitSimpleReply( void *reply, int len, int *passed_fd )
225 struct iovec vec[2];
226 unsigned int ret;
227 int got;
229 vec[1].iov_base = reply;
230 vec[1].iov_len = len;
231 ret = CLIENT_WaitReply_v( &got, passed_fd, vec, 2 );
232 if (got != len)
233 CLIENT_ProtocolError( "WaitSimpleReply: len %d != %d\n", len, got );
234 return ret;
238 /***********************************************************************
239 * CLIENT_NewThread
241 * Send a new thread request.
243 int CLIENT_NewThread( THDB *thdb, int *thandle, int *phandle )
245 struct new_thread_request request;
246 struct new_thread_reply reply;
247 int fd[2];
248 extern BOOL32 THREAD_InitDone;
249 extern void server_init( int fd );
250 extern void select_loop(void);
252 if (!THREAD_InitDone) /* first thread -> start the server */
254 int tmpfd[2];
255 char buffer[16];
257 if (socketpair( AF_UNIX, SOCK_STREAM, 0, tmpfd ) == -1)
259 perror("socketpair");
260 exit(1);
262 switch(fork())
264 case -1: /* error */
265 perror("fork");
266 exit(1);
267 case 0: /* child */
268 close( tmpfd[0] );
269 sprintf( buffer, "%d", tmpfd[1] );
270 /*#define EXEC_SERVER*/
271 #ifdef EXEC_SERVER
272 execlp( "wineserver", "wineserver", buffer, NULL );
273 execl( "/usr/local/bin/wineserver", "wineserver", buffer, NULL );
274 execl( "./server/wineserver", "wineserver", buffer, NULL );
275 #endif
276 server_init( tmpfd[1] );
277 select_loop();
278 exit(0);
279 default: /* parent */
280 close( tmpfd[1] );
281 SET_CUR_THREAD( thdb );
282 THREAD_InitDone = TRUE;
283 thdb->socket = tmpfd[0];
284 break;
288 if (socketpair( AF_UNIX, SOCK_STREAM, 0, fd ) == -1)
290 SetLastError( ERROR_TOO_MANY_OPEN_FILES ); /* FIXME */
291 return -1;
294 request.pid = thdb->process->server_pid;
295 CLIENT_SendRequest( REQ_NEW_THREAD, fd[1], 1, &request, sizeof(request) );
296 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL )) goto error;
297 thdb->server_tid = reply.tid;
298 thdb->process->server_pid = reply.pid;
299 if (thdb->socket != -1) close( thdb->socket );
300 thdb->socket = fd[0];
301 thdb->seq = 0; /* reset the sequence number for the new fd */
303 if (thandle) *thandle = reply.thandle;
304 else if (reply.thandle != -1) CLIENT_CloseHandle( reply.thandle );
305 if (phandle) *phandle = reply.phandle;
306 else if (reply.phandle != -1) CLIENT_CloseHandle( reply.phandle );
307 return 0;
309 error:
310 close( fd[0] );
311 return -1;
315 /***********************************************************************
316 * CLIENT_InitThread
318 * Send an init thread request. Return 0 if OK.
320 int CLIENT_InitThread(void)
322 THDB *thdb = THREAD_Current();
323 struct init_thread_request init;
324 int len = strlen( thdb->process->env_db->cmd_line );
326 init.unix_pid = getpid();
327 len = MIN( len, MAX_MSG_LENGTH - sizeof(init) );
329 CLIENT_SendRequest( REQ_INIT_THREAD, -1, 2,
330 &init, sizeof(init),
331 thdb->process->env_db->cmd_line, len );
332 return CLIENT_WaitReply( NULL, NULL, 0 );
336 /***********************************************************************
337 * CLIENT_SetDebug
339 * Send a set debug level request. Return 0 if OK.
341 int CLIENT_SetDebug( int level )
343 CLIENT_SendRequest( REQ_SET_DEBUG, -1, 1, &level, sizeof(level) );
344 return CLIENT_WaitReply( NULL, NULL, 0 );
347 /***********************************************************************
348 * CLIENT_CloseHandle
350 * Send a close handle request. Return 0 if OK.
352 int CLIENT_CloseHandle( int handle )
354 CLIENT_SendRequest( REQ_CLOSE_HANDLE, -1, 1, &handle, sizeof(handle) );
355 return CLIENT_WaitReply( NULL, NULL, 0 );
358 /***********************************************************************
359 * CLIENT_DuplicateHandle
361 * Send a duplicate handle request. Return 0 if OK.
363 int CLIENT_DuplicateHandle( int src_process, int src_handle, int dst_process, int dst_handle,
364 DWORD access, BOOL32 inherit, DWORD options )
366 struct dup_handle_request req;
367 struct dup_handle_reply reply;
368 int len;
370 req.src_process = src_process;
371 req.src_handle = src_handle;
372 req.dst_process = dst_process;
373 req.dst_handle = dst_handle;
374 req.access = access;
375 req.inherit = inherit;
376 req.options = options;
378 CLIENT_SendRequest( REQ_DUP_HANDLE, -1, 1, &req, sizeof(req) );
379 CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL );
380 return reply.handle;
384 /***********************************************************************
385 * CLIENT_OpenProcess
387 * Open a handle to a process.
389 int CLIENT_OpenProcess( void *pid, DWORD access, BOOL32 inherit )
391 struct open_process_request req;
392 struct open_process_reply reply;
394 req.pid = pid;
395 req.access = access;
396 req.inherit = inherit;
398 CLIENT_SendRequest( REQ_OPEN_PROCESS, -1, 1, &req, sizeof(req) );
399 CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL );
400 return reply.handle;
404 /***********************************************************************
405 * CLIENT_Select
407 int CLIENT_Select( int count, int *handles, int flags, int timeout )
409 struct select_request req;
410 struct select_reply reply;
411 int len;
413 req.count = count;
414 req.flags = flags;
415 req.timeout = timeout;
417 CLIENT_SendRequest( REQ_SELECT, -1, 2,
418 &req, sizeof(req),
419 handles, count * sizeof(int) );
420 CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL );
421 return reply.signaled;