blob: 8d7cc01b9b06714d736fb9def6e8497874fc2bab [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 Julliard642d3131998-07-12 19:29:36 +00009#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>
18
Alexandre Julliard829fe321998-07-26 14:27:39 +000019#include "config.h"
Alexandre Julliard642d3131998-07-12 19:29:36 +000020#include "server.h"
Alexandre Julliard767e6f61998-08-09 12:47:43 +000021
22#include "server/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{
41 enum state state; /* client state */
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) */
Alexandre Julliard642d3131998-07-12 19:29:36 +000048};
49
Alexandre Julliard642d3131998-07-12 19:29:36 +000050static int initial_client_fd; /* fd of the first client */
Alexandre Julliard642d3131998-07-12 19:29:36 +000051
Alexandre Julliard767e6f61998-08-09 12:47:43 +000052/* exit code passed to remove_client */
53#define OUT_OF_MEMORY -1
54#define BROKEN_PIPE -2
55#define PROTOCOL_ERROR -3
56
Alexandre Julliard642d3131998-07-12 19:29:36 +000057
58/* signal a client protocol error */
59static void protocol_error( int client_fd, const char *err, ... )
60{
61 va_list args;
62
63 va_start( args, err );
64 fprintf( stderr, "Protocol error:%d: ", client_fd );
65 vfprintf( stderr, err, args );
66 va_end( args );
67}
68
Alexandre Julliard642d3131998-07-12 19:29:36 +000069/* send a message to a client that is ready to receive something */
Alexandre Julliardc6e45ed1998-12-27 08:35:39 +000070static void do_write( struct client *client, int client_fd )
Alexandre Julliard642d3131998-07-12 19:29:36 +000071{
Alexandre Julliard642d3131998-07-12 19:29:36 +000072 struct iovec vec[2];
Alexandre Julliard829fe321998-07-26 14:27:39 +000073#ifndef HAVE_MSGHDR_ACCRIGHTS
74 struct cmsg_fd cmsg = { sizeof(cmsg), SOL_SOCKET, SCM_RIGHTS,
75 client->pass_fd };
76#endif
77 struct msghdr msghdr = { NULL, 0, vec, 2, };
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
85 if (client->count < sizeof(client->head))
86 {
87 vec[0].iov_base = (char *)&client->head + client->count;
88 vec[0].iov_len = sizeof(client->head) - client->count;
89 vec[1].iov_base = client->data;
90 vec[1].iov_len = client->head.len - sizeof(client->head);
91 }
92 else
93 {
94 vec[0].iov_base = client->data + client->count - sizeof(client->head);
95 vec[0].iov_len = client->head.len - client->count;
96 msghdr.msg_iovlen = 1;
97 }
Alexandre Julliard829fe321998-07-26 14:27:39 +000098 if (client->pass_fd != -1) /* we have an fd to send */
Alexandre Julliard642d3131998-07-12 19:29:36 +000099 {
Alexandre Julliard829fe321998-07-26 14:27:39 +0000100#ifdef HAVE_MSGHDR_ACCRIGHTS
101 msghdr.msg_accrights = (void *)&client->pass_fd;
102 msghdr.msg_accrightslen = sizeof(client->pass_fd);
103#else
Alexandre Julliard642d3131998-07-12 19:29:36 +0000104 msghdr.msg_control = &cmsg;
105 msghdr.msg_controllen = sizeof(cmsg);
Alexandre Julliard829fe321998-07-26 14:27:39 +0000106#endif
Alexandre Julliard642d3131998-07-12 19:29:36 +0000107 }
108 ret = sendmsg( client_fd, &msghdr, 0 );
109 if (ret == -1)
110 {
111 if (errno != EPIPE) perror("sendmsg");
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000112 remove_client( client_fd, BROKEN_PIPE );
113 return;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000114 }
115 if (client->pass_fd != -1) /* We sent the fd, now we can close it */
116 {
117 close( client->pass_fd );
118 client->pass_fd = -1;
119 }
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000120 if ((client->count += ret) < client->head.len) return;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000121
122 /* we have finished with this message */
123 if (client->data) free( client->data );
124 client->data = NULL;
125 client->count = 0;
126 client->state = RUNNING;
127 client->seq++;
Alexandre Julliardc6e45ed1998-12-27 08:35:39 +0000128 set_select_events( client_fd, READ_EVENT );
Alexandre Julliard642d3131998-07-12 19:29:36 +0000129}
130
131
132/* read a message from a client that has something to say */
Alexandre Julliardc6e45ed1998-12-27 08:35:39 +0000133static void do_read( struct client *client, int client_fd )
Alexandre Julliard642d3131998-07-12 19:29:36 +0000134{
Alexandre Julliard642d3131998-07-12 19:29:36 +0000135 struct iovec vec;
Alexandre Julliard829fe321998-07-26 14:27:39 +0000136 int pass_fd = -1;
137#ifdef HAVE_MSGHDR_ACCRIGHTS
138 struct msghdr msghdr = { NULL, 0, &vec, 1, (void*)&pass_fd, sizeof(int) };
139#else
140 struct cmsg_fd cmsg = { sizeof(cmsg), SOL_SOCKET, SCM_RIGHTS, -1 };
Alexandre Julliard642d3131998-07-12 19:29:36 +0000141 struct msghdr msghdr = { NULL, 0, &vec, 1, &cmsg, sizeof(cmsg), 0 };
Alexandre Julliard829fe321998-07-26 14:27:39 +0000142#endif
Alexandre Julliard642d3131998-07-12 19:29:36 +0000143 int ret;
144
Alexandre Julliard642d3131998-07-12 19:29:36 +0000145 if (client->count < sizeof(client->head))
146 {
147 vec.iov_base = (char *)&client->head + client->count;
148 vec.iov_len = sizeof(client->head) - client->count;
149 }
150 else
151 {
152 if (!client->data &&
153 !(client->data = malloc(client->head.len-sizeof(client->head))))
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000154 {
155 remove_client( client_fd, OUT_OF_MEMORY );
156 return;
157 }
Alexandre Julliard642d3131998-07-12 19:29:36 +0000158 vec.iov_base = client->data + client->count - sizeof(client->head);
159 vec.iov_len = client->head.len - client->count;
160 }
161
162 ret = recvmsg( client_fd, &msghdr, 0 );
163 if (ret == -1)
164 {
165 perror("recvmsg");
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000166 remove_client( client_fd, BROKEN_PIPE );
167 return;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000168 }
Alexandre Julliard829fe321998-07-26 14:27:39 +0000169#ifndef HAVE_MSGHDR_ACCRIGHTS
170 pass_fd = cmsg.fd;
171#endif
172 if (pass_fd != -1)
Alexandre Julliard642d3131998-07-12 19:29:36 +0000173 {
174 /* can only receive one fd per message */
175 if (client->pass_fd != -1) close( client->pass_fd );
Alexandre Julliard829fe321998-07-26 14:27:39 +0000176 client->pass_fd = pass_fd;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000177 }
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000178 else if (!ret) /* closed pipe */
179 {
180 remove_client( client_fd, BROKEN_PIPE );
181 return;
182 }
Alexandre Julliard642d3131998-07-12 19:29:36 +0000183
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000184 if (client->state == RUNNING) client->state = SENDING;
185 assert( client->state == SENDING );
186
Alexandre Julliard642d3131998-07-12 19:29:36 +0000187 client->count += ret;
188
189 /* received the complete header yet? */
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000190 if (client->count < sizeof(client->head)) return;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000191
192 /* sanity checks */
193 if (client->head.seq != client->seq)
194 {
195 protocol_error( client_fd, "bad sequence %08x instead of %08x\n",
196 client->head.seq, client->seq );
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000197 remove_client( client_fd, PROTOCOL_ERROR );
198 return;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000199 }
200 if ((client->head.len < sizeof(client->head)) ||
201 (client->head.len > MAX_MSG_LENGTH + sizeof(client->head)))
202 {
203 protocol_error( client_fd, "bad header length %08x\n",
204 client->head.len );
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000205 remove_client( client_fd, PROTOCOL_ERROR );
206 return;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000207 }
208
209 /* received the whole message? */
210 if (client->count == client->head.len)
211 {
212 /* done reading the data, call the callback function */
213
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000214 int len = client->head.len - sizeof(client->head);
Alexandre Julliard642d3131998-07-12 19:29:36 +0000215 char *data = client->data;
216 int passed_fd = client->pass_fd;
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000217 enum request type = client->head.type;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000218
219 /* clear the info now, as the client may be deleted by the callback */
220 client->head.len = 0;
221 client->head.type = 0;
222 client->count = 0;
223 client->data = NULL;
224 client->pass_fd = -1;
225 client->state = WAITING;
226 client->seq++;
227
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000228 call_req_handler( client->self, type, data, len, passed_fd );
Alexandre Julliard642d3131998-07-12 19:29:36 +0000229 if (passed_fd != -1) close( passed_fd );
230 if (data) free( data );
231 }
Alexandre Julliard642d3131998-07-12 19:29:36 +0000232}
233
Alexandre Julliard642d3131998-07-12 19:29:36 +0000234/* handle a client timeout */
Alexandre Julliardc6e45ed1998-12-27 08:35:39 +0000235static void client_timeout( int client_fd, void *private )
Alexandre Julliard642d3131998-07-12 19:29:36 +0000236{
Alexandre Julliardc6e45ed1998-12-27 08:35:39 +0000237 struct client *client = (struct client *)private;
238 set_select_timeout( client_fd, 0 ); /* Remove the timeout */
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000239 call_timeout_handler( client->self );
Alexandre Julliard642d3131998-07-12 19:29:36 +0000240}
241
Alexandre Julliardc6e45ed1998-12-27 08:35:39 +0000242/* handle a client event */
243static void client_event( int client_fd, int event, void *private )
Alexandre Julliard642d3131998-07-12 19:29:36 +0000244{
Alexandre Julliardc6e45ed1998-12-27 08:35:39 +0000245 struct client *client = (struct client *)private;
246 if (event & WRITE_EVENT)
247 do_write( client, client_fd );
248 if (event & READ_EVENT)
249 do_read( client, client_fd );
Alexandre Julliard642d3131998-07-12 19:29:36 +0000250}
251
Alexandre Julliardc6e45ed1998-12-27 08:35:39 +0000252static const struct select_ops client_ops =
253{
254 client_event,
255 client_timeout
256};
Alexandre Julliard642d3131998-07-12 19:29:36 +0000257
258/*******************************************************************/
259/* server-side exported functions */
260
Alexandre Julliardc6e45ed1998-12-27 08:35:39 +0000261/* server initialization */
262void server_init( int fd )
263{
264 /* special magic to create the initial thread */
265 initial_client_fd = fd;
266 add_client( initial_client_fd, NULL );
267}
268
269
Alexandre Julliard642d3131998-07-12 19:29:36 +0000270/* add a client */
271int add_client( int client_fd, struct thread *self )
272{
Alexandre Julliard642d3131998-07-12 19:29:36 +0000273 struct client *client = malloc( sizeof(*client) );
274 if (!client) return -1;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000275
276 client->state = RUNNING;
277 client->seq = 0;
278 client->head.len = 0;
279 client->head.type = 0;
280 client->count = 0;
281 client->data = NULL;
282 client->self = self;
283 client->pass_fd = -1;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000284
Alexandre Julliardc6e45ed1998-12-27 08:35:39 +0000285 if (add_select_user( client_fd, READ_EVENT, &client_ops, client ) == -1)
286 {
287 free( client );
288 return -1;
289 }
Alexandre Julliard642d3131998-07-12 19:29:36 +0000290 return client_fd;
291}
292
293/* remove a client */
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000294void remove_client( int client_fd, int exit_code )
Alexandre Julliard642d3131998-07-12 19:29:36 +0000295{
Alexandre Julliardc6e45ed1998-12-27 08:35:39 +0000296 struct client *client = (struct client *)get_select_private_data( &client_ops, client_fd );
Alexandre Julliard642d3131998-07-12 19:29:36 +0000297 assert( client );
298
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000299 call_kill_handler( client->self, exit_code );
Alexandre Julliard642d3131998-07-12 19:29:36 +0000300
Alexandre Julliardc6e45ed1998-12-27 08:35:39 +0000301 remove_select_user( client_fd );
Alexandre Julliard642d3131998-07-12 19:29:36 +0000302 if (initial_client_fd == client_fd) initial_client_fd = -1;
303 close( client_fd );
Alexandre Julliard642d3131998-07-12 19:29:36 +0000304
305 /* Purge messages */
306 if (client->data) free( client->data );
307 if (client->pass_fd != -1) close( client->pass_fd );
308 free( client );
309}
310
311/* return the fd of the initial client */
312int get_initial_client_fd(void)
313{
314 assert( initial_client_fd != -1 );
315 return initial_client_fd;
316}
317
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000318/* send a reply to a client */
319int send_reply_v( int client_fd, int type, int pass_fd,
320 struct iovec *vec, int veclen )
321{
322 int i;
323 unsigned int len;
324 char *p;
Alexandre Julliardc6e45ed1998-12-27 08:35:39 +0000325 struct client *client = (struct client *)get_select_private_data( &client_ops, client_fd );
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000326
327 assert( client );
328 assert( client->state == WAITING );
329 assert( !client->data );
330
331 if (debug_level) trace_reply( client->self, type, pass_fd, vec, veclen );
332
333 for (i = len = 0; i < veclen; i++) len += vec[i].iov_len;
334 assert( len < MAX_MSG_LENGTH );
335
336 if (len && !(client->data = malloc( len ))) return -1;
337 client->count = 0;
338 client->head.len = len + sizeof(client->head);
339 client->head.type = type;
340 client->head.seq = client->seq;
341 client->pass_fd = pass_fd;
342
343 for (i = 0, p = client->data; i < veclen; i++)
Alexandre Julliard642d3131998-07-12 19:29:36 +0000344 {
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000345 memcpy( p, vec[i].iov_base, vec[i].iov_len );
346 p += vec[i].iov_len;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000347 }
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000348
349 client->state = READING;
Alexandre Julliardc6e45ed1998-12-27 08:35:39 +0000350 set_select_events( client_fd, WRITE_EVENT );
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000351 return 0;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000352}