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 static 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
67 struct cmsg_fd cmsg
= { sizeof(cmsg
), SOL_SOCKET
, SCM_RIGHTS
, pass_fd
};
69 struct msghdr msghdr
= { NULL
, 0, vec
, veclen
, };
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 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
);
89 msghdr
.msg_control
= &cmsg
;
90 msghdr
.msg_controllen
= sizeof(cmsg
);
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 /***********************************************************************
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];
116 n
++; /* for vec[0] */
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 );
125 return CLIENT_SendRequest_v( req
, pass_fd
, vec
, n
);
129 /***********************************************************************
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();
140 #ifdef HAVE_MSGHDR_ACCRIGHTS
141 struct msghdr msghdr
= { NULL
, 0, vec
, veclen
, (void*)&pass_fd
, sizeof(int) };
143 struct cmsg_fd cmsg
= { sizeof(cmsg
), SOL_SOCKET
, SCM_RIGHTS
, -1 };
144 struct msghdr msghdr
= { NULL
, 0, vec
, veclen
, &cmsg
, sizeof(cmsg
), 0 };
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;
157 CLIENT_ProtocolError( "recvmsg\n" );
159 if (!ret
) CLIENT_Die( thdb
); /* the server closed the connection; time to die... */
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
178 *passed_fd
= pass_fd
;
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)
201 CLIENT_ProtocolError( "recv\n" );
203 if (!addlen
) CLIENT_Die( thdb
); /* the server closed the connection; time to die... */
204 if (len
) *len
+= addlen
;
207 iovtot
+= vec
[i
].iov_len
;
211 addlen
= remaining
< sizeof(buffer
) ? remaining
: sizeof(buffer
);
215 if ((addlen
= recv( thdb
->socket
, buffer
, addlen
, 0 )) == -1)
218 CLIENT_ProtocolError( "recv\n" );
220 if (!addlen
) CLIENT_Die( thdb
); /* the server closed the connection; time to die... */
224 if (head
.type
) SetLastError( head
.type
);
225 return head
.type
; /* error code */
229 /***********************************************************************
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];
241 n
++; /* for vec[0] */
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 );
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
)
265 vec
[1].iov_base
= reply
;
266 vec
[1].iov_len
= len
;
267 ret
= CLIENT_WaitReply_v( &got
, passed_fd
, vec
, 2 );
269 CLIENT_ProtocolError( "WaitSimpleReply: len %d != %d\n", len
, got
);
274 /***********************************************************************
277 * Start the server and create the initial socket pair.
279 int CLIENT_InitServer(void)
283 extern void create_initial_thread( int fd
);
285 if (socketpair( AF_UNIX
, SOCK_STREAM
, 0, fd
) == -1)
287 perror("socketpair");
297 sprintf( buffer
, "%d", fd
[1] );
298 /*#define EXEC_SERVER*/
300 execlp( "wineserver", "wineserver", buffer
, NULL
);
301 execl( "/usr/local/bin/wineserver", "wineserver", buffer
, NULL
);
302 execl( "./server/wineserver", "wineserver", buffer
, NULL
);
304 create_initial_thread( fd
[1] );
306 default: /* parent */
314 /***********************************************************************
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
;
334 /***********************************************************************
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 );