blob: 915de42a18c343feef1a064edd156e4e21284a6a [file] [log] [blame]
Alexandre Julliard642d3131998-07-12 19:29:36 +00001/*
2 * Server-side socket communication functions
3 *
4 * Copyright (C) 1998 Alexandre Julliard
5 */
6
7#include <assert.h>
8#include <errno.h>
Alexandre Julliardf2616a21999-05-20 16:40:23 +00009#include <fcntl.h>
Alexandre Julliard642d3131998-07-12 19:29:36 +000010#include <stdio.h>
11#include <stdlib.h>
12#include <stdarg.h>
13#include <string.h>
14#include <sys/time.h>
15#include <sys/types.h>
16#include <sys/socket.h>
17#include <sys/uio.h>
18#include <unistd.h>
19
Alexandre Julliard829fe321998-07-26 14:27:39 +000020#include "config.h"
Alexandre Julliard642d3131998-07-12 19:29:36 +000021#include "server.h"
Alexandre Julliard43c190e1999-05-15 10:48:19 +000022#include "object.h"
Alexandre Julliard642d3131998-07-12 19:29:36 +000023
Alexandre Julliard829fe321998-07-26 14:27:39 +000024/* Some versions of glibc don't define this */
25#ifndef SCM_RIGHTS
26#define SCM_RIGHTS 1
27#endif
28
Alexandre Julliard642d3131998-07-12 19:29:36 +000029/* client state */
30enum state
31{
32 RUNNING, /* running normally */
33 SENDING, /* sending us a request */
34 WAITING, /* waiting for us to reply */
35 READING /* reading our reply */
36};
37
Alexandre Julliard642d3131998-07-12 19:29:36 +000038/* client structure */
39struct client
40{
Alexandre Julliard88de35c1999-05-16 16:57:49 +000041 enum state state; /* client state */
42 struct select_user select; /* select user */
43 unsigned int seq; /* current sequence number */
44 struct header head; /* current msg header */
45 char *data; /* current msg data */
46 int count; /* bytes sent/received so far */
47 int pass_fd; /* fd to pass to and from the client */
48 struct thread *self; /* client thread (opaque pointer) */
49 struct timeout_user *timeout; /* current timeout (opaque pointer) */
Alexandre Julliard642d3131998-07-12 19:29:36 +000050};
51
Alexandre Julliard642d3131998-07-12 19:29:36 +000052
Alexandre Julliard767e6f61998-08-09 12:47:43 +000053/* exit code passed to remove_client */
54#define OUT_OF_MEMORY -1
55#define BROKEN_PIPE -2
56#define PROTOCOL_ERROR -3
57
Alexandre Julliard642d3131998-07-12 19:29:36 +000058
59/* signal a client protocol error */
Alexandre Julliard88de35c1999-05-16 16:57:49 +000060static void protocol_error( struct client *client, const char *err, ... )
Alexandre Julliard642d3131998-07-12 19:29:36 +000061{
62 va_list args;
63
64 va_start( args, err );
Alexandre Julliard88de35c1999-05-16 16:57:49 +000065 fprintf( stderr, "Protocol error:%d: ", client->select.fd );
Alexandre Julliard642d3131998-07-12 19:29:36 +000066 vfprintf( stderr, err, args );
67 va_end( args );
68}
69
Alexandre Julliard642d3131998-07-12 19:29:36 +000070/* send a message to a client that is ready to receive something */
Alexandre Julliardc6e45ed1998-12-27 08:35:39 +000071static void do_write( struct client *client, int client_fd )
Alexandre Julliard642d3131998-07-12 19:29:36 +000072{
Alexandre Julliard642d3131998-07-12 19:29:36 +000073 struct iovec vec[2];
Alexandre Julliard829fe321998-07-26 14:27:39 +000074#ifndef HAVE_MSGHDR_ACCRIGHTS
Patrik Stridvall1bb94031999-05-08 15:47:44 +000075 struct cmsg_fd cmsg;
Alexandre Julliard829fe321998-07-26 14:27:39 +000076#endif
Patrik Stridvall1bb94031999-05-08 15:47:44 +000077 struct msghdr msghdr;
Alexandre Julliard642d3131998-07-12 19:29:36 +000078 int ret;
79
80 /* make sure we have something to send */
81 assert( client->count < client->head.len );
82 /* make sure the client is listening */
83 assert( client->state == READING );
84
Patrik Stridvall1bb94031999-05-08 15:47:44 +000085 msghdr.msg_name = NULL;
86 msghdr.msg_namelen = 0;
87 msghdr.msg_iov = vec;
88
Alexandre Julliard642d3131998-07-12 19:29:36 +000089 if (client->count < sizeof(client->head))
90 {
91 vec[0].iov_base = (char *)&client->head + client->count;
92 vec[0].iov_len = sizeof(client->head) - client->count;
93 vec[1].iov_base = client->data;
94 vec[1].iov_len = client->head.len - sizeof(client->head);
Patrik Stridvall1bb94031999-05-08 15:47:44 +000095 msghdr.msg_iovlen = 2;
Alexandre Julliard642d3131998-07-12 19:29:36 +000096 }
97 else
98 {
99 vec[0].iov_base = client->data + client->count - sizeof(client->head);
100 vec[0].iov_len = client->head.len - client->count;
101 msghdr.msg_iovlen = 1;
102 }
Patrik Stridvall1bb94031999-05-08 15:47:44 +0000103
104#ifdef HAVE_MSGHDR_ACCRIGHTS
Alexandre Julliard829fe321998-07-26 14:27:39 +0000105 if (client->pass_fd != -1) /* we have an fd to send */
Alexandre Julliard642d3131998-07-12 19:29:36 +0000106 {
Alexandre Julliard829fe321998-07-26 14:27:39 +0000107 msghdr.msg_accrights = (void *)&client->pass_fd;
108 msghdr.msg_accrightslen = sizeof(client->pass_fd);
Patrik Stridvall1bb94031999-05-08 15:47:44 +0000109 }
110 else
111 {
112 msghdr.msg_accrights = NULL;
113 msghdr.msg_accrightslen = 0;
114 }
115#else /* HAVE_MSGHDR_ACCRIGHTS */
116 if (client->pass_fd != -1) /* we have an fd to send */
117 {
118 cmsg.len = sizeof(cmsg);
119 cmsg.level = SOL_SOCKET;
120 cmsg.type = SCM_RIGHTS;
121 cmsg.fd = client->pass_fd;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000122 msghdr.msg_control = &cmsg;
123 msghdr.msg_controllen = sizeof(cmsg);
Alexandre Julliard642d3131998-07-12 19:29:36 +0000124 }
Patrik Stridvall1bb94031999-05-08 15:47:44 +0000125 else
126 {
127 msghdr.msg_control = NULL;
128 msghdr.msg_controllen = 0;
129 }
130 msghdr.msg_flags = 0;
131#endif /* HAVE_MSGHDR_ACCRIGHTS */
132
Alexandre Julliard642d3131998-07-12 19:29:36 +0000133 ret = sendmsg( client_fd, &msghdr, 0 );
134 if (ret == -1)
135 {
136 if (errno != EPIPE) perror("sendmsg");
Alexandre Julliard88de35c1999-05-16 16:57:49 +0000137 remove_client( client, BROKEN_PIPE );
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000138 return;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000139 }
140 if (client->pass_fd != -1) /* We sent the fd, now we can close it */
141 {
142 close( client->pass_fd );
143 client->pass_fd = -1;
144 }
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000145 if ((client->count += ret) < client->head.len) return;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000146
147 /* we have finished with this message */
148 if (client->data) free( client->data );
149 client->data = NULL;
150 client->count = 0;
151 client->state = RUNNING;
152 client->seq++;
Alexandre Julliard88de35c1999-05-16 16:57:49 +0000153 set_select_events( &client->select, READ_EVENT );
Alexandre Julliard642d3131998-07-12 19:29:36 +0000154}
155
156
157/* read a message from a client that has something to say */
Alexandre Julliardc6e45ed1998-12-27 08:35:39 +0000158static void do_read( struct client *client, int client_fd )
Alexandre Julliard642d3131998-07-12 19:29:36 +0000159{
Alexandre Julliard642d3131998-07-12 19:29:36 +0000160 struct iovec vec;
Alexandre Julliard829fe321998-07-26 14:27:39 +0000161 int pass_fd = -1;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000162 int ret;
163
Patrik Stridvall1bb94031999-05-08 15:47:44 +0000164#ifdef HAVE_MSGHDR_ACCRIGHTS
165 struct msghdr msghdr;
166
167 msghdr.msg_accrights = (void *)&pass_fd;
168 msghdr.msg_accrightslen = sizeof(int);
169#else /* HAVE_MSGHDR_ACCRIGHTS */
170 struct msghdr msghdr;
171 struct cmsg_fd cmsg;
172
173 cmsg.len = sizeof(cmsg);
174 cmsg.level = SOL_SOCKET;
175 cmsg.type = SCM_RIGHTS;
176 cmsg.fd = -1;
177 msghdr.msg_control = &cmsg;
178 msghdr.msg_controllen = sizeof(cmsg);
179 msghdr.msg_flags = 0;
180#endif /* HAVE_MSGHDR_ACCRIGHTS */
181
182 msghdr.msg_name = NULL;
183 msghdr.msg_namelen = 0;
184 msghdr.msg_iov = &vec;
185 msghdr.msg_iovlen = 1;
186
Alexandre Julliard642d3131998-07-12 19:29:36 +0000187 if (client->count < sizeof(client->head))
188 {
189 vec.iov_base = (char *)&client->head + client->count;
190 vec.iov_len = sizeof(client->head) - client->count;
191 }
192 else
193 {
194 if (!client->data &&
195 !(client->data = malloc(client->head.len-sizeof(client->head))))
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000196 {
Alexandre Julliard88de35c1999-05-16 16:57:49 +0000197 remove_client( client, OUT_OF_MEMORY );
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000198 return;
199 }
Alexandre Julliard642d3131998-07-12 19:29:36 +0000200 vec.iov_base = client->data + client->count - sizeof(client->head);
201 vec.iov_len = client->head.len - client->count;
202 }
203
204 ret = recvmsg( client_fd, &msghdr, 0 );
205 if (ret == -1)
206 {
207 perror("recvmsg");
Alexandre Julliard88de35c1999-05-16 16:57:49 +0000208 remove_client( client, BROKEN_PIPE );
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000209 return;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000210 }
Alexandre Julliard829fe321998-07-26 14:27:39 +0000211#ifndef HAVE_MSGHDR_ACCRIGHTS
212 pass_fd = cmsg.fd;
213#endif
214 if (pass_fd != -1)
Alexandre Julliard642d3131998-07-12 19:29:36 +0000215 {
216 /* can only receive one fd per message */
217 if (client->pass_fd != -1) close( client->pass_fd );
Alexandre Julliard829fe321998-07-26 14:27:39 +0000218 client->pass_fd = pass_fd;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000219 }
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000220 else if (!ret) /* closed pipe */
221 {
Alexandre Julliard88de35c1999-05-16 16:57:49 +0000222 remove_client( client, BROKEN_PIPE );
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000223 return;
224 }
Alexandre Julliard642d3131998-07-12 19:29:36 +0000225
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000226 if (client->state == RUNNING) client->state = SENDING;
227 assert( client->state == SENDING );
228
Alexandre Julliard642d3131998-07-12 19:29:36 +0000229 client->count += ret;
230
231 /* received the complete header yet? */
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000232 if (client->count < sizeof(client->head)) return;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000233
234 /* sanity checks */
235 if (client->head.seq != client->seq)
236 {
Alexandre Julliard88de35c1999-05-16 16:57:49 +0000237 protocol_error( client, "bad sequence %08x instead of %08x\n",
Alexandre Julliard642d3131998-07-12 19:29:36 +0000238 client->head.seq, client->seq );
Alexandre Julliard88de35c1999-05-16 16:57:49 +0000239 remove_client( client, PROTOCOL_ERROR );
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000240 return;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000241 }
242 if ((client->head.len < sizeof(client->head)) ||
243 (client->head.len > MAX_MSG_LENGTH + sizeof(client->head)))
244 {
Alexandre Julliard88de35c1999-05-16 16:57:49 +0000245 protocol_error( client, "bad header length %08x\n",
Alexandre Julliard642d3131998-07-12 19:29:36 +0000246 client->head.len );
Alexandre Julliard88de35c1999-05-16 16:57:49 +0000247 remove_client( client, PROTOCOL_ERROR );
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000248 return;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000249 }
250
251 /* received the whole message? */
252 if (client->count == client->head.len)
253 {
254 /* done reading the data, call the callback function */
255
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000256 int len = client->head.len - sizeof(client->head);
Alexandre Julliard642d3131998-07-12 19:29:36 +0000257 char *data = client->data;
258 int passed_fd = client->pass_fd;
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000259 enum request type = client->head.type;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000260
261 /* clear the info now, as the client may be deleted by the callback */
262 client->head.len = 0;
263 client->head.type = 0;
264 client->count = 0;
265 client->data = NULL;
266 client->pass_fd = -1;
267 client->state = WAITING;
268 client->seq++;
269
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000270 call_req_handler( client->self, type, data, len, passed_fd );
Alexandre Julliard642d3131998-07-12 19:29:36 +0000271 if (passed_fd != -1) close( passed_fd );
272 if (data) free( data );
273 }
Alexandre Julliard642d3131998-07-12 19:29:36 +0000274}
275
Alexandre Julliardc6e45ed1998-12-27 08:35:39 +0000276/* handle a client event */
Alexandre Julliard88de35c1999-05-16 16:57:49 +0000277static void client_event( int event, void *private )
Alexandre Julliard642d3131998-07-12 19:29:36 +0000278{
Alexandre Julliardc6e45ed1998-12-27 08:35:39 +0000279 struct client *client = (struct client *)private;
Alexandre Julliard88de35c1999-05-16 16:57:49 +0000280 if (event & WRITE_EVENT) do_write( client, client->select.fd );
281 if (event & READ_EVENT) do_read( client, client->select.fd );
Alexandre Julliard642d3131998-07-12 19:29:36 +0000282}
283
Alexandre Julliard642d3131998-07-12 19:29:36 +0000284
285/*******************************************************************/
286/* server-side exported functions */
287
288/* add a client */
Alexandre Julliard88de35c1999-05-16 16:57:49 +0000289struct client *add_client( int fd, struct thread *self )
Alexandre Julliard642d3131998-07-12 19:29:36 +0000290{
Alexandre Julliardf2616a21999-05-20 16:40:23 +0000291 int flags;
Alexandre Julliard88de35c1999-05-16 16:57:49 +0000292 struct client *client = mem_alloc( sizeof(*client) );
293 if (!client) return NULL;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000294
Alexandre Julliardf2616a21999-05-20 16:40:23 +0000295 flags = fcntl( fd, F_GETFL, 0 );
296 fcntl( fd, F_SETFL, flags | O_NONBLOCK );
297
Alexandre Julliard642d3131998-07-12 19:29:36 +0000298 client->state = RUNNING;
Alexandre Julliard88de35c1999-05-16 16:57:49 +0000299 client->select.fd = fd;
300 client->select.func = client_event;
301 client->select.private = client;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000302 client->seq = 0;
303 client->head.len = 0;
304 client->head.type = 0;
305 client->count = 0;
306 client->data = NULL;
307 client->self = self;
Alexandre Julliard88de35c1999-05-16 16:57:49 +0000308 client->timeout = NULL;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000309 client->pass_fd = -1;
Alexandre Julliard88de35c1999-05-16 16:57:49 +0000310 register_select_user( &client->select );
311 set_select_events( &client->select, READ_EVENT );
312 return client;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000313}
314
315/* remove a client */
Alexandre Julliard88de35c1999-05-16 16:57:49 +0000316void remove_client( struct client *client, int exit_code )
Alexandre Julliard642d3131998-07-12 19:29:36 +0000317{
Alexandre Julliard642d3131998-07-12 19:29:36 +0000318 assert( client );
319
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000320 call_kill_handler( client->self, exit_code );
Alexandre Julliard642d3131998-07-12 19:29:36 +0000321
Alexandre Julliard88de35c1999-05-16 16:57:49 +0000322 if (client->timeout) remove_timeout_user( client->timeout );
323 unregister_select_user( &client->select );
324 close( client->select.fd );
Alexandre Julliard642d3131998-07-12 19:29:36 +0000325
326 /* Purge messages */
327 if (client->data) free( client->data );
328 if (client->pass_fd != -1) close( client->pass_fd );
329 free( client );
330}
331
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000332/* send a reply to a client */
Alexandre Julliard88de35c1999-05-16 16:57:49 +0000333int send_reply_v( struct client *client, int type, int pass_fd,
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000334 struct iovec *vec, int veclen )
335{
336 int i;
337 unsigned int len;
338 char *p;
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000339
340 assert( client );
341 assert( client->state == WAITING );
342 assert( !client->data );
343
344 if (debug_level) trace_reply( client->self, type, pass_fd, vec, veclen );
345
346 for (i = len = 0; i < veclen; i++) len += vec[i].iov_len;
347 assert( len < MAX_MSG_LENGTH );
348
349 if (len && !(client->data = malloc( len ))) return -1;
350 client->count = 0;
351 client->head.len = len + sizeof(client->head);
352 client->head.type = type;
353 client->head.seq = client->seq;
354 client->pass_fd = pass_fd;
355
356 for (i = 0, p = client->data; i < veclen; i++)
Alexandre Julliard642d3131998-07-12 19:29:36 +0000357 {
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000358 memcpy( p, vec[i].iov_base, vec[i].iov_len );
359 p += vec[i].iov_len;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000360 }
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000361
362 client->state = READING;
Alexandre Julliard88de35c1999-05-16 16:57:49 +0000363 set_select_events( &client->select, WRITE_EVENT );
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000364 return 0;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000365}