Added new_process and init_process request.
[wine] / server / socket.c
1 /*
2  * Server-side socket communication functions
3  *
4  * Copyright (C) 1998 Alexandre Julliard
5  */
6
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>
18
19 #include "config.h"
20 #include "server.h"
21
22 #include "server/object.h"
23
24 /* Some versions of glibc don't define this */
25 #ifndef SCM_RIGHTS
26 #define SCM_RIGHTS 1
27 #endif
28
29 /* client state */
30 enum 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
38 /* client structure */
39 struct 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) */
48 };
49
50
51 /* exit code passed to remove_client */
52 #define OUT_OF_MEMORY  -1
53 #define BROKEN_PIPE    -2
54 #define PROTOCOL_ERROR -3
55
56
57 /* signal a client protocol error */
58 static void protocol_error( int client_fd, const char *err, ... )
59 {
60     va_list args;
61
62     va_start( args, err );
63     fprintf( stderr, "Protocol error:%d: ", client_fd );
64     vfprintf( stderr, err, args );
65     va_end( args );
66 }
67
68 /* send a message to a client that is ready to receive something */
69 static void do_write( struct client *client, int client_fd )
70 {
71     struct iovec vec[2];
72 #ifndef HAVE_MSGHDR_ACCRIGHTS
73     struct cmsg_fd cmsg  = { sizeof(cmsg), SOL_SOCKET, SCM_RIGHTS,
74                              client->pass_fd };
75 #endif
76     struct msghdr msghdr = { NULL, 0, vec, 2, };
77     int ret;
78
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 );
83
84     if (client->count < sizeof(client->head))
85     {
86         vec[0].iov_base = (char *)&client->head + client->count;
87         vec[0].iov_len  = sizeof(client->head) - client->count;
88         vec[1].iov_base = client->data;
89         vec[1].iov_len  = client->head.len - sizeof(client->head);
90     }
91     else
92     {
93         vec[0].iov_base = client->data + client->count - sizeof(client->head);
94         vec[0].iov_len  = client->head.len - client->count;
95         msghdr.msg_iovlen = 1;
96     }
97     if (client->pass_fd != -1)  /* we have an fd to send */
98     {
99 #ifdef HAVE_MSGHDR_ACCRIGHTS
100         msghdr.msg_accrights = (void *)&client->pass_fd;
101         msghdr.msg_accrightslen = sizeof(client->pass_fd);
102 #else
103         msghdr.msg_control = &cmsg;
104         msghdr.msg_controllen = sizeof(cmsg);
105 #endif
106     }
107     ret = sendmsg( client_fd, &msghdr, 0 );
108     if (ret == -1)
109     {
110         if (errno != EPIPE) perror("sendmsg");
111         remove_client( client_fd, BROKEN_PIPE );
112         return;
113     }
114     if (client->pass_fd != -1)  /* We sent the fd, now we can close it */
115     {
116         close( client->pass_fd );
117         client->pass_fd = -1;
118     }
119     if ((client->count += ret) < client->head.len) return;
120
121     /* we have finished with this message */
122     if (client->data) free( client->data );
123     client->data  = NULL;
124     client->count = 0;
125     client->state = RUNNING;
126     client->seq++;
127     set_select_events( client_fd, READ_EVENT );
128 }
129
130
131 /* read a message from a client that has something to say */
132 static void do_read( struct client *client, int client_fd )
133 {
134     struct iovec vec;
135     int pass_fd = -1;
136 #ifdef HAVE_MSGHDR_ACCRIGHTS
137     struct msghdr msghdr = { NULL, 0, &vec, 1, (void*)&pass_fd, sizeof(int) };
138 #else
139     struct cmsg_fd cmsg  = { sizeof(cmsg), SOL_SOCKET, SCM_RIGHTS, -1 };
140     struct msghdr msghdr = { NULL, 0, &vec, 1, &cmsg, sizeof(cmsg), 0 };
141 #endif
142     int ret;
143
144     if (client->count < sizeof(client->head))
145     {
146         vec.iov_base = (char *)&client->head + client->count;
147         vec.iov_len  = sizeof(client->head) - client->count;
148     }
149     else
150     {
151         if (!client->data &&
152             !(client->data = malloc(client->head.len-sizeof(client->head))))
153         {
154             remove_client( client_fd, OUT_OF_MEMORY );
155             return;
156         }
157         vec.iov_base = client->data + client->count - sizeof(client->head);
158         vec.iov_len  = client->head.len - client->count;
159     }
160
161     ret = recvmsg( client_fd, &msghdr, 0 );
162     if (ret == -1)
163     {
164         perror("recvmsg");
165         remove_client( client_fd, BROKEN_PIPE );
166         return;
167     }
168 #ifndef HAVE_MSGHDR_ACCRIGHTS
169     pass_fd = cmsg.fd;
170 #endif
171     if (pass_fd != -1)
172     {
173         /* can only receive one fd per message */
174         if (client->pass_fd != -1) close( client->pass_fd );
175         client->pass_fd = pass_fd;
176     }
177     else if (!ret)  /* closed pipe */
178     {
179         remove_client( client_fd, BROKEN_PIPE );
180         return;
181     }
182
183     if (client->state == RUNNING) client->state = SENDING;
184     assert( client->state == SENDING );
185
186     client->count += ret;
187
188     /* received the complete header yet? */
189     if (client->count < sizeof(client->head)) return;
190
191     /* sanity checks */
192     if (client->head.seq != client->seq)
193     {
194         protocol_error( client_fd, "bad sequence %08x instead of %08x\n",
195                         client->head.seq, client->seq );
196         remove_client( client_fd, PROTOCOL_ERROR );
197         return;
198     }
199     if ((client->head.len < sizeof(client->head)) ||
200         (client->head.len > MAX_MSG_LENGTH + sizeof(client->head)))
201     {
202         protocol_error( client_fd, "bad header length %08x\n",
203                         client->head.len );
204         remove_client( client_fd, PROTOCOL_ERROR );
205         return;
206     }
207
208     /* received the whole message? */
209     if (client->count == client->head.len)
210     {
211         /* done reading the data, call the callback function */
212
213         int len = client->head.len - sizeof(client->head);
214         char *data = client->data;
215         int passed_fd = client->pass_fd;
216         enum request type = client->head.type;
217
218         /* clear the info now, as the client may be deleted by the callback */
219         client->head.len  = 0;
220         client->head.type = 0;
221         client->count     = 0;
222         client->data      = NULL;
223         client->pass_fd   = -1;
224         client->state     = WAITING;
225         client->seq++;
226
227         call_req_handler( client->self, type, data, len, passed_fd );
228         if (passed_fd != -1) close( passed_fd );
229         if (data) free( data );
230     }
231 }
232
233 /* handle a client timeout */
234 static void client_timeout( int client_fd, void *private )
235 {
236     struct client *client = (struct client *)private;
237     set_select_timeout( client_fd, 0 );  /* Remove the timeout */
238     call_timeout_handler( client->self );
239 }
240
241 /* handle a client event */
242 static void client_event( int client_fd, int event, void *private )
243 {
244     struct client *client = (struct client *)private;
245     if (event & WRITE_EVENT)
246         do_write( client, client_fd );
247     if (event & READ_EVENT)
248         do_read( client, client_fd );
249 }
250
251 static const struct select_ops client_ops =
252 {
253     client_event,
254     client_timeout
255 };
256
257 /*******************************************************************/
258 /* server-side exported functions                                  */
259
260 /* add a client */
261 int add_client( int client_fd, struct thread *self )
262 {
263     struct client *client = malloc( sizeof(*client) );
264     if (!client) return -1;
265
266     client->state                = RUNNING;
267     client->seq                  = 0;
268     client->head.len             = 0;
269     client->head.type            = 0;
270     client->count                = 0;
271     client->data                 = NULL;
272     client->self                 = self;
273     client->pass_fd              = -1;
274
275     if (add_select_user( client_fd, READ_EVENT, &client_ops, client ) == -1)
276     {
277         free( client );
278         return -1;
279     }
280     return client_fd;
281 }
282
283 /* remove a client */
284 void remove_client( int client_fd, int exit_code )
285 {
286     struct client *client = (struct client *)get_select_private_data( &client_ops, client_fd );
287     assert( client );
288
289     call_kill_handler( client->self, exit_code );
290
291     remove_select_user( client_fd );
292     close( client_fd );
293
294     /* Purge messages */
295     if (client->data) free( client->data );
296     if (client->pass_fd != -1) close( client->pass_fd );
297     free( client );
298 }
299
300 /* send a reply to a client */
301 int send_reply_v( int client_fd, int type, int pass_fd,
302                   struct iovec *vec, int veclen )
303 {
304     int i;
305     unsigned int len;
306     char *p;
307     struct client *client = (struct client *)get_select_private_data( &client_ops, client_fd );
308
309     assert( client );
310     assert( client->state == WAITING );
311     assert( !client->data );
312
313     if (debug_level) trace_reply( client->self, type, pass_fd, vec, veclen );
314
315     for (i = len = 0; i < veclen; i++) len += vec[i].iov_len;
316     assert( len < MAX_MSG_LENGTH );
317
318     if (len && !(client->data = malloc( len ))) return -1;
319     client->count     = 0;
320     client->head.len  = len + sizeof(client->head);
321     client->head.type = type;
322     client->head.seq  = client->seq;
323     client->pass_fd   = pass_fd;
324
325     for (i = 0, p = client->data; i < veclen; i++)
326     {
327         memcpy( p, vec[i].iov_base, vec[i].iov_len );
328         p += vec[i].iov_len;
329     }
330
331     client->state = READING;
332     set_select_events( client_fd, WRITE_EVENT );
333     return 0;
334 }