socket.c 9.81 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Server-side socket communication functions
 *
 * Copyright (C) 1998 Alexandre Julliard
 */

#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <unistd.h>

Alexandre Julliard's avatar
Alexandre Julliard committed
19
#include "config.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
20
#include "server.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
21 22

#include "server/object.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
23

Alexandre Julliard's avatar
Alexandre Julliard committed
24 25 26 27 28
/* Some versions of glibc don't define this */
#ifndef SCM_RIGHTS
#define SCM_RIGHTS 1
#endif

Alexandre Julliard's avatar
Alexandre Julliard committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
/* client state */
enum state
{
    RUNNING,   /* running normally */
    SENDING,   /* sending us a request */
    WAITING,   /* waiting for us to reply */
    READING    /* reading our reply */
};

/* client structure */
struct client
{
    enum state         state;        /* client state */
    unsigned int       seq;          /* current sequence number */
    struct header      head;         /* current msg header */
    char              *data;         /* current msg data */
    int                count;        /* bytes sent/received so far */
    int                pass_fd;      /* fd to pass to and from the client */
    struct thread     *self;         /* client thread (opaque pointer) */
};

static int initial_client_fd;               /* fd of the first client */

Alexandre Julliard's avatar
Alexandre Julliard committed
52 53 54 55 56
/* exit code passed to remove_client */
#define OUT_OF_MEMORY  -1
#define BROKEN_PIPE    -2
#define PROTOCOL_ERROR -3

Alexandre Julliard's avatar
Alexandre Julliard committed
57 58 59 60 61 62 63 64 65 66 67 68 69

/* signal a client protocol error */
static void protocol_error( int client_fd, const char *err, ... )
{
    va_list args;

    va_start( args, err );
    fprintf( stderr, "Protocol error:%d: ", client_fd );
    vfprintf( stderr, err, args );
    va_end( args );
}

/* send a message to a client that is ready to receive something */
70
static void do_write( struct client *client, int client_fd )
Alexandre Julliard's avatar
Alexandre Julliard committed
71 72
{
    struct iovec vec[2];
Alexandre Julliard's avatar
Alexandre Julliard committed
73 74 75 76 77
#ifndef HAVE_MSGHDR_ACCRIGHTS
    struct cmsg_fd cmsg  = { sizeof(cmsg), SOL_SOCKET, SCM_RIGHTS,
                             client->pass_fd };
#endif
    struct msghdr msghdr = { NULL, 0, vec, 2, };
Alexandre Julliard's avatar
Alexandre Julliard committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    int ret;

    /* make sure we have something to send */
    assert( client->count < client->head.len );
    /* make sure the client is listening */
    assert( client->state == READING );

    if (client->count < sizeof(client->head))
    {
        vec[0].iov_base = (char *)&client->head + client->count;
        vec[0].iov_len  = sizeof(client->head) - client->count;
        vec[1].iov_base = client->data;
        vec[1].iov_len  = client->head.len - sizeof(client->head);
    }
    else
    {
        vec[0].iov_base = client->data + client->count - sizeof(client->head);
        vec[0].iov_len  = client->head.len - client->count;
        msghdr.msg_iovlen = 1;
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
98
    if (client->pass_fd != -1)  /* we have an fd to send */
Alexandre Julliard's avatar
Alexandre Julliard committed
99
    {
Alexandre Julliard's avatar
Alexandre Julliard committed
100 101 102 103
#ifdef HAVE_MSGHDR_ACCRIGHTS
        msghdr.msg_accrights = (void *)&client->pass_fd;
        msghdr.msg_accrightslen = sizeof(client->pass_fd);
#else
Alexandre Julliard's avatar
Alexandre Julliard committed
104 105
        msghdr.msg_control = &cmsg;
        msghdr.msg_controllen = sizeof(cmsg);
Alexandre Julliard's avatar
Alexandre Julliard committed
106
#endif
Alexandre Julliard's avatar
Alexandre Julliard committed
107 108 109 110 111
    }
    ret = sendmsg( client_fd, &msghdr, 0 );
    if (ret == -1)
    {
        if (errno != EPIPE) perror("sendmsg");
Alexandre Julliard's avatar
Alexandre Julliard committed
112 113
        remove_client( client_fd, BROKEN_PIPE );
        return;
Alexandre Julliard's avatar
Alexandre Julliard committed
114 115 116 117 118 119
    }
    if (client->pass_fd != -1)  /* We sent the fd, now we can close it */
    {
        close( client->pass_fd );
        client->pass_fd = -1;
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
120
    if ((client->count += ret) < client->head.len) return;
Alexandre Julliard's avatar
Alexandre Julliard committed
121 122 123 124 125 126 127

    /* we have finished with this message */
    if (client->data) free( client->data );
    client->data  = NULL;
    client->count = 0;
    client->state = RUNNING;
    client->seq++;
128
    set_select_events( client_fd, READ_EVENT );
Alexandre Julliard's avatar
Alexandre Julliard committed
129 130 131 132
}


/* read a message from a client that has something to say */
133
static void do_read( struct client *client, int client_fd )
Alexandre Julliard's avatar
Alexandre Julliard committed
134 135
{
    struct iovec vec;
Alexandre Julliard's avatar
Alexandre Julliard committed
136 137 138 139 140
    int pass_fd = -1;
#ifdef HAVE_MSGHDR_ACCRIGHTS
    struct msghdr msghdr = { NULL, 0, &vec, 1, (void*)&pass_fd, sizeof(int) };
#else
    struct cmsg_fd cmsg  = { sizeof(cmsg), SOL_SOCKET, SCM_RIGHTS, -1 };
Alexandre Julliard's avatar
Alexandre Julliard committed
141
    struct msghdr msghdr = { NULL, 0, &vec, 1, &cmsg, sizeof(cmsg), 0 };
Alexandre Julliard's avatar
Alexandre Julliard committed
142
#endif
Alexandre Julliard's avatar
Alexandre Julliard committed
143 144 145 146 147 148 149 150 151 152 153
    int ret;

    if (client->count < sizeof(client->head))
    {
        vec.iov_base = (char *)&client->head + client->count;
        vec.iov_len  = sizeof(client->head) - client->count;
    }
    else
    {
        if (!client->data &&
            !(client->data = malloc(client->head.len-sizeof(client->head))))
Alexandre Julliard's avatar
Alexandre Julliard committed
154 155 156 157
        {
            remove_client( client_fd, OUT_OF_MEMORY );
            return;
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
158 159 160 161 162 163 164 165
        vec.iov_base = client->data + client->count - sizeof(client->head);
        vec.iov_len  = client->head.len - client->count;
    }

    ret = recvmsg( client_fd, &msghdr, 0 );
    if (ret == -1)
    {
        perror("recvmsg");
Alexandre Julliard's avatar
Alexandre Julliard committed
166 167
        remove_client( client_fd, BROKEN_PIPE );
        return;
Alexandre Julliard's avatar
Alexandre Julliard committed
168
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
169 170 171 172
#ifndef HAVE_MSGHDR_ACCRIGHTS
    pass_fd = cmsg.fd;
#endif
    if (pass_fd != -1)
Alexandre Julliard's avatar
Alexandre Julliard committed
173 174 175
    {
        /* can only receive one fd per message */
        if (client->pass_fd != -1) close( client->pass_fd );
Alexandre Julliard's avatar
Alexandre Julliard committed
176
        client->pass_fd = pass_fd;
Alexandre Julliard's avatar
Alexandre Julliard committed
177
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
178 179 180 181 182
    else if (!ret)  /* closed pipe */
    {
        remove_client( client_fd, BROKEN_PIPE );
        return;
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
183

Alexandre Julliard's avatar
Alexandre Julliard committed
184 185 186
    if (client->state == RUNNING) client->state = SENDING;
    assert( client->state == SENDING );

Alexandre Julliard's avatar
Alexandre Julliard committed
187 188 189
    client->count += ret;

    /* received the complete header yet? */
Alexandre Julliard's avatar
Alexandre Julliard committed
190
    if (client->count < sizeof(client->head)) return;
Alexandre Julliard's avatar
Alexandre Julliard committed
191 192 193 194 195 196

    /* sanity checks */
    if (client->head.seq != client->seq)
    {
        protocol_error( client_fd, "bad sequence %08x instead of %08x\n",
                        client->head.seq, client->seq );
Alexandre Julliard's avatar
Alexandre Julliard committed
197 198
        remove_client( client_fd, PROTOCOL_ERROR );
        return;
Alexandre Julliard's avatar
Alexandre Julliard committed
199 200 201 202 203 204
    }
    if ((client->head.len < sizeof(client->head)) ||
        (client->head.len > MAX_MSG_LENGTH + sizeof(client->head)))
    {
        protocol_error( client_fd, "bad header length %08x\n",
                        client->head.len );
Alexandre Julliard's avatar
Alexandre Julliard committed
205 206
        remove_client( client_fd, PROTOCOL_ERROR );
        return;
Alexandre Julliard's avatar
Alexandre Julliard committed
207 208 209 210 211 212 213
    }

    /* received the whole message? */
    if (client->count == client->head.len)
    {
        /* done reading the data, call the callback function */

Alexandre Julliard's avatar
Alexandre Julliard committed
214
        int len = client->head.len - sizeof(client->head);
Alexandre Julliard's avatar
Alexandre Julliard committed
215 216
        char *data = client->data;
        int passed_fd = client->pass_fd;
Alexandre Julliard's avatar
Alexandre Julliard committed
217
        enum request type = client->head.type;
Alexandre Julliard's avatar
Alexandre Julliard committed
218 219 220 221 222 223 224 225 226 227

        /* clear the info now, as the client may be deleted by the callback */
        client->head.len  = 0;
        client->head.type = 0;
        client->count     = 0;
        client->data      = NULL;
        client->pass_fd   = -1;
        client->state     = WAITING;
        client->seq++;

Alexandre Julliard's avatar
Alexandre Julliard committed
228
        call_req_handler( client->self, type, data, len, passed_fd );
Alexandre Julliard's avatar
Alexandre Julliard committed
229 230 231 232 233 234
        if (passed_fd != -1) close( passed_fd );
        if (data) free( data );
    }
}

/* handle a client timeout */
235
static void client_timeout( int client_fd, void *private )
Alexandre Julliard's avatar
Alexandre Julliard committed
236
{
237 238
    struct client *client = (struct client *)private;
    set_select_timeout( client_fd, 0 );  /* Remove the timeout */
Alexandre Julliard's avatar
Alexandre Julliard committed
239
    call_timeout_handler( client->self );
Alexandre Julliard's avatar
Alexandre Julliard committed
240 241
}

242 243 244 245 246 247 248 249 250
/* handle a client event */
static void client_event( int client_fd, int event, void *private )
{
    struct client *client = (struct client *)private;
    if (event & WRITE_EVENT)
        do_write( client, client_fd );
    if (event & READ_EVENT)
        do_read( client, client_fd );
}
Alexandre Julliard's avatar
Alexandre Julliard committed
251

252
static const struct select_ops client_ops =
Alexandre Julliard's avatar
Alexandre Julliard committed
253
{
254 255 256
    client_event,
    client_timeout
};
Alexandre Julliard's avatar
Alexandre Julliard committed
257

258 259
/*******************************************************************/
/* server-side exported functions                                  */
Alexandre Julliard's avatar
Alexandre Julliard committed
260

261 262 263
/* server initialization */
void server_init( int fd )
{
Alexandre Julliard's avatar
Alexandre Julliard committed
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
    /* special magic to create the initial thread */
    initial_client_fd = fd;
    add_client( initial_client_fd, NULL );
}


/* add a client */
int add_client( int client_fd, struct thread *self )
{
    struct client *client = malloc( sizeof(*client) );
    if (!client) return -1;

    client->state                = RUNNING;
    client->seq                  = 0;
    client->head.len             = 0;
    client->head.type            = 0;
    client->count                = 0;
    client->data                 = NULL;
    client->self                 = self;
    client->pass_fd              = -1;

285 286 287 288 289
    if (add_select_user( client_fd, READ_EVENT, &client_ops, client ) == -1)
    {
        free( client );
        return -1;
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
290 291 292 293
    return client_fd;
}

/* remove a client */
Alexandre Julliard's avatar
Alexandre Julliard committed
294
void remove_client( int client_fd, int exit_code )
Alexandre Julliard's avatar
Alexandre Julliard committed
295
{
296
    struct client *client = (struct client *)get_select_private_data( &client_ops, client_fd );
Alexandre Julliard's avatar
Alexandre Julliard committed
297 298
    assert( client );

Alexandre Julliard's avatar
Alexandre Julliard committed
299
    call_kill_handler( client->self, exit_code );
Alexandre Julliard's avatar
Alexandre Julliard committed
300

301
    remove_select_user( client_fd );
Alexandre Julliard's avatar
Alexandre Julliard committed
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
    if (initial_client_fd == client_fd) initial_client_fd = -1;
    close( client_fd );

    /* Purge messages */
    if (client->data) free( client->data );
    if (client->pass_fd != -1) close( client->pass_fd );
    free( client );
}

/* return the fd of the initial client */
int get_initial_client_fd(void)
{
    assert( initial_client_fd != -1 );
    return initial_client_fd;
}

/* send a reply to a client */
Alexandre Julliard's avatar
Alexandre Julliard committed
319 320
int send_reply_v( int client_fd, int type, int pass_fd,
                  struct iovec *vec, int veclen )
Alexandre Julliard's avatar
Alexandre Julliard committed
321 322
{
    int i;
Alexandre Julliard's avatar
Alexandre Julliard committed
323 324
    unsigned int len;
    char *p;
325
    struct client *client = (struct client *)get_select_private_data( &client_ops, client_fd );
Alexandre Julliard's avatar
Alexandre Julliard committed
326

Alexandre Julliard's avatar
Alexandre Julliard committed
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
    assert( client );
    assert( client->state == WAITING );
    assert( !client->data );

    if (debug_level) trace_reply( client->self, type, pass_fd, vec, veclen );

    for (i = len = 0; i < veclen; i++) len += vec[i].iov_len;
    assert( len < MAX_MSG_LENGTH );

    if (len && !(client->data = malloc( len ))) return -1;
    client->count     = 0;
    client->head.len  = len + sizeof(client->head);
    client->head.type = type;
    client->head.seq  = client->seq;
    client->pass_fd   = pass_fd;

    for (i = 0, p = client->data; i < veclen; i++)
Alexandre Julliard's avatar
Alexandre Julliard committed
344
    {
Alexandre Julliard's avatar
Alexandre Julliard committed
345 346
        memcpy( p, vec[i].iov_base, vec[i].iov_len );
        p += vec[i].iov_len;
Alexandre Julliard's avatar
Alexandre Julliard committed
347
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
348 349

    client->state = READING;
350
    set_select_events( client_fd, WRITE_EVENT );
Alexandre Julliard's avatar
Alexandre Julliard committed
351
    return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
352
}