Changed select interface, separated timeouts from file descriptors.
[wine/hacks.git] / server / socket.c
blob98c1f6db20925e3f194f9c8bdecce94370bf6c46
1 /*
2 * Server-side socket communication functions
4 * Copyright (C) 1998 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include <errno.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <stdarg.h>
12 #include <string.h>
13 #include <sys/time.h>
14 #include <sys/types.h>
15 #include <sys/socket.h>
16 #include <sys/uio.h>
17 #include <unistd.h>
19 #include "config.h"
20 #include "server.h"
21 #include "object.h"
23 /* Some versions of glibc don't define this */
24 #ifndef SCM_RIGHTS
25 #define SCM_RIGHTS 1
26 #endif
28 /* client state */
29 enum state
31 RUNNING, /* running normally */
32 SENDING, /* sending us a request */
33 WAITING, /* waiting for us to reply */
34 READING /* reading our reply */
37 /* client structure */
38 struct client
40 enum state state; /* client state */
41 struct select_user select; /* select user */
42 unsigned int seq; /* current sequence number */
43 struct header head; /* current msg header */
44 char *data; /* current msg data */
45 int count; /* bytes sent/received so far */
46 int pass_fd; /* fd to pass to and from the client */
47 struct thread *self; /* client thread (opaque pointer) */
48 struct timeout_user *timeout; /* current timeout (opaque pointer) */
52 /* exit code passed to remove_client */
53 #define OUT_OF_MEMORY -1
54 #define BROKEN_PIPE -2
55 #define PROTOCOL_ERROR -3
58 /* signal a client protocol error */
59 static void protocol_error( struct client *client, const char *err, ... )
61 va_list args;
63 va_start( args, err );
64 fprintf( stderr, "Protocol error:%d: ", client->select.fd );
65 vfprintf( stderr, err, args );
66 va_end( args );
69 /* send a message to a client that is ready to receive something */
70 static void do_write( struct client *client, int client_fd )
72 struct iovec vec[2];
73 #ifndef HAVE_MSGHDR_ACCRIGHTS
74 struct cmsg_fd cmsg;
75 #endif
76 struct msghdr msghdr;
77 int ret;
79 /* make sure we have something to send */
80 assert( client->count < client->head.len );
81 /* make sure the client is listening */
82 assert( client->state == READING );
84 msghdr.msg_name = NULL;
85 msghdr.msg_namelen = 0;
86 msghdr.msg_iov = vec;
88 if (client->count < sizeof(client->head))
90 vec[0].iov_base = (char *)&client->head + client->count;
91 vec[0].iov_len = sizeof(client->head) - client->count;
92 vec[1].iov_base = client->data;
93 vec[1].iov_len = client->head.len - sizeof(client->head);
94 msghdr.msg_iovlen = 2;
96 else
98 vec[0].iov_base = client->data + client->count - sizeof(client->head);
99 vec[0].iov_len = client->head.len - client->count;
100 msghdr.msg_iovlen = 1;
103 #ifdef HAVE_MSGHDR_ACCRIGHTS
104 if (client->pass_fd != -1) /* we have an fd to send */
106 msghdr.msg_accrights = (void *)&client->pass_fd;
107 msghdr.msg_accrightslen = sizeof(client->pass_fd);
109 else
111 msghdr.msg_accrights = NULL;
112 msghdr.msg_accrightslen = 0;
114 #else /* HAVE_MSGHDR_ACCRIGHTS */
115 if (client->pass_fd != -1) /* we have an fd to send */
117 cmsg.len = sizeof(cmsg);
118 cmsg.level = SOL_SOCKET;
119 cmsg.type = SCM_RIGHTS;
120 cmsg.fd = client->pass_fd;
121 msghdr.msg_control = &cmsg;
122 msghdr.msg_controllen = sizeof(cmsg);
124 else
126 msghdr.msg_control = NULL;
127 msghdr.msg_controllen = 0;
129 msghdr.msg_flags = 0;
130 #endif /* HAVE_MSGHDR_ACCRIGHTS */
132 ret = sendmsg( client_fd, &msghdr, 0 );
133 if (ret == -1)
135 if (errno != EPIPE) perror("sendmsg");
136 remove_client( client, BROKEN_PIPE );
137 return;
139 if (client->pass_fd != -1) /* We sent the fd, now we can close it */
141 close( client->pass_fd );
142 client->pass_fd = -1;
144 if ((client->count += ret) < client->head.len) return;
146 /* we have finished with this message */
147 if (client->data) free( client->data );
148 client->data = NULL;
149 client->count = 0;
150 client->state = RUNNING;
151 client->seq++;
152 set_select_events( &client->select, READ_EVENT );
156 /* read a message from a client that has something to say */
157 static void do_read( struct client *client, int client_fd )
159 struct iovec vec;
160 int pass_fd = -1;
161 int ret;
163 #ifdef HAVE_MSGHDR_ACCRIGHTS
164 struct msghdr msghdr;
166 msghdr.msg_accrights = (void *)&pass_fd;
167 msghdr.msg_accrightslen = sizeof(int);
168 #else /* HAVE_MSGHDR_ACCRIGHTS */
169 struct msghdr msghdr;
170 struct cmsg_fd cmsg;
172 cmsg.len = sizeof(cmsg);
173 cmsg.level = SOL_SOCKET;
174 cmsg.type = SCM_RIGHTS;
175 cmsg.fd = -1;
176 msghdr.msg_control = &cmsg;
177 msghdr.msg_controllen = sizeof(cmsg);
178 msghdr.msg_flags = 0;
179 #endif /* HAVE_MSGHDR_ACCRIGHTS */
181 msghdr.msg_name = NULL;
182 msghdr.msg_namelen = 0;
183 msghdr.msg_iov = &vec;
184 msghdr.msg_iovlen = 1;
186 if (client->count < sizeof(client->head))
188 vec.iov_base = (char *)&client->head + client->count;
189 vec.iov_len = sizeof(client->head) - client->count;
191 else
193 if (!client->data &&
194 !(client->data = malloc(client->head.len-sizeof(client->head))))
196 remove_client( client, OUT_OF_MEMORY );
197 return;
199 vec.iov_base = client->data + client->count - sizeof(client->head);
200 vec.iov_len = client->head.len - client->count;
203 ret = recvmsg( client_fd, &msghdr, 0 );
204 if (ret == -1)
206 perror("recvmsg");
207 remove_client( client, BROKEN_PIPE );
208 return;
210 #ifndef HAVE_MSGHDR_ACCRIGHTS
211 pass_fd = cmsg.fd;
212 #endif
213 if (pass_fd != -1)
215 /* can only receive one fd per message */
216 if (client->pass_fd != -1) close( client->pass_fd );
217 client->pass_fd = pass_fd;
219 else if (!ret) /* closed pipe */
221 remove_client( client, BROKEN_PIPE );
222 return;
225 if (client->state == RUNNING) client->state = SENDING;
226 assert( client->state == SENDING );
228 client->count += ret;
230 /* received the complete header yet? */
231 if (client->count < sizeof(client->head)) return;
233 /* sanity checks */
234 if (client->head.seq != client->seq)
236 protocol_error( client, "bad sequence %08x instead of %08x\n",
237 client->head.seq, client->seq );
238 remove_client( client, PROTOCOL_ERROR );
239 return;
241 if ((client->head.len < sizeof(client->head)) ||
242 (client->head.len > MAX_MSG_LENGTH + sizeof(client->head)))
244 protocol_error( client, "bad header length %08x\n",
245 client->head.len );
246 remove_client( client, PROTOCOL_ERROR );
247 return;
250 /* received the whole message? */
251 if (client->count == client->head.len)
253 /* done reading the data, call the callback function */
255 int len = client->head.len - sizeof(client->head);
256 char *data = client->data;
257 int passed_fd = client->pass_fd;
258 enum request type = client->head.type;
260 /* clear the info now, as the client may be deleted by the callback */
261 client->head.len = 0;
262 client->head.type = 0;
263 client->count = 0;
264 client->data = NULL;
265 client->pass_fd = -1;
266 client->state = WAITING;
267 client->seq++;
269 call_req_handler( client->self, type, data, len, passed_fd );
270 if (passed_fd != -1) close( passed_fd );
271 if (data) free( data );
275 /* handle a client event */
276 static void client_event( int event, void *private )
278 struct client *client = (struct client *)private;
279 if (event & WRITE_EVENT) do_write( client, client->select.fd );
280 if (event & READ_EVENT) do_read( client, client->select.fd );
284 /*******************************************************************/
285 /* server-side exported functions */
287 /* add a client */
288 struct client *add_client( int fd, struct thread *self )
290 struct client *client = mem_alloc( sizeof(*client) );
291 if (!client) return NULL;
293 client->state = RUNNING;
294 client->select.fd = fd;
295 client->select.func = client_event;
296 client->select.private = client;
297 client->seq = 0;
298 client->head.len = 0;
299 client->head.type = 0;
300 client->count = 0;
301 client->data = NULL;
302 client->self = self;
303 client->timeout = NULL;
304 client->pass_fd = -1;
305 register_select_user( &client->select );
306 set_select_events( &client->select, READ_EVENT );
307 return client;
310 /* remove a client */
311 void remove_client( struct client *client, int exit_code )
313 assert( client );
315 call_kill_handler( client->self, exit_code );
317 if (client->timeout) remove_timeout_user( client->timeout );
318 unregister_select_user( &client->select );
319 close( client->select.fd );
321 /* Purge messages */
322 if (client->data) free( client->data );
323 if (client->pass_fd != -1) close( client->pass_fd );
324 free( client );
327 /* send a reply to a client */
328 int send_reply_v( struct client *client, int type, int pass_fd,
329 struct iovec *vec, int veclen )
331 int i;
332 unsigned int len;
333 char *p;
335 assert( client );
336 assert( client->state == WAITING );
337 assert( !client->data );
339 if (debug_level) trace_reply( client->self, type, pass_fd, vec, veclen );
341 for (i = len = 0; i < veclen; i++) len += vec[i].iov_len;
342 assert( len < MAX_MSG_LENGTH );
344 if (len && !(client->data = malloc( len ))) return -1;
345 client->count = 0;
346 client->head.len = len + sizeof(client->head);
347 client->head.type = type;
348 client->head.seq = client->seq;
349 client->pass_fd = pass_fd;
351 for (i = 0, p = client->data; i < veclen; i++)
353 memcpy( p, vec[i].iov_base, vec[i].iov_len );
354 p += vec[i].iov_len;
357 client->state = READING;
358 set_select_events( &client->select, WRITE_EVENT );
359 return 0;