virnetserverclient.c 41.9 KB
Newer Older
1 2 3
/*
 * virnetserverclient.c: generic network RPC server client
 *
4
 * Copyright (C) 2006-2013 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17
 * Copyright (C) 2006 Daniel P. Berrange
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with this library.  If not, see
O
Osier Yang 已提交
19
 * <http://www.gnu.org/licenses/>.
20 21 22 23 24 25
 *
 * Author: Daniel P. Berrange <berrange@redhat.com>
 */

#include <config.h>

26
#include "internal.h"
27
#if WITH_SASL
28 29 30 31 32
# include <sasl/sasl.h>
#endif

#include "virnetserverclient.h"

33
#include "virlog.h"
34
#include "virerror.h"
35
#include "viralloc.h"
36
#include "virthread.h"
37
#include "virkeepalive.h"
38 39
#include "virstring.h"
#include "virutil.h"
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

#define VIR_FROM_THIS VIR_FROM_RPC

/* Allow for filtering of incoming messages to a custom
 * dispatch processing queue, instead of the workers.
 * This allows for certain types of messages to be handled
 * strictly "in order"
 */

typedef struct _virNetServerClientFilter virNetServerClientFilter;
typedef virNetServerClientFilter *virNetServerClientFilterPtr;

struct _virNetServerClientFilter {
    int id;
    virNetServerClientFilterFunc func;
    void *opaque;

    virNetServerClientFilterPtr next;
};


struct _virNetServerClient
{
63
    virObjectLockable parent;
64

65
    bool wantClose;
66
    bool delayedClose;
67 68 69
    virNetSocketPtr sock;
    int auth;
    bool readonly;
70
#if WITH_GNUTLS
71 72
    virNetTLSContextPtr tlsCtxt;
    virNetTLSSessionPtr tls;
73
#endif
74
#if WITH_SASL
75 76
    virNetSASLSessionPtr sasl;
#endif
M
Michal Privoznik 已提交
77 78
    int sockTimer; /* Timer to be fired upon cached data,
                    * so we jump out from poll() immediately */
79

80 81 82

    virIdentityPtr identity;

83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
    /* Count of messages in the 'tx' queue,
     * and the server worker pool queue
     * ie RPC calls in progress. Does not count
     * async events which are not used for
     * throttling calculations */
    size_t nrequests;
    size_t nrequests_max;
    /* Zero or one messages being received. Zero if
     * nrequests >= max_clients and throttling */
    virNetMessagePtr rx;
    /* Zero or many messages waiting for transmit
     * back to client, including async events */
    virNetMessagePtr tx;

    /* Filters to capture messages that would otherwise
     * end up on the 'dx' queue */
    virNetServerClientFilterPtr filters;
    int nextFilterID;

    virNetServerClientDispatchFunc dispatchFunc;
    void *dispatchOpaque;

    void *privateData;
106
    virFreeCallback privateDataFreeFunc;
107
    virNetServerClientPrivPreExecRestart privateDataPreExecRestart;
108
    virNetServerClientCloseFunc privateDataCloseFunc;
109 110

    virKeepAlivePtr keepalive;
111 112 113
};


114 115 116 117 118
static virClassPtr virNetServerClientClass;
static void virNetServerClientDispose(void *obj);

static int virNetServerClientOnceInit(void)
{
119
    if (!(virNetServerClientClass = virClassNew(virClassForObjectLockable(),
120
                                                "virNetServerClient",
121 122 123 124 125 126 127 128 129 130
                                                sizeof(virNetServerClient),
                                                virNetServerClientDispose)))
        return -1;

    return 0;
}

VIR_ONCE_GLOBAL_INIT(virNetServerClient)


131 132
static void virNetServerClientDispatchEvent(virNetSocketPtr sock, int events, void *opaque);
static void virNetServerClientUpdateEvent(virNetServerClientPtr client);
M
Michal Privoznik 已提交
133
static void virNetServerClientDispatchRead(virNetServerClientPtr client);
134 135
static int virNetServerClientSendMessageLocked(virNetServerClientPtr client,
                                               virNetMessagePtr msg);
136 137 138 139 140 141 142 143 144 145

/*
 * @client: a locked client object
 */
static int
virNetServerClientCalculateHandleMode(virNetServerClientPtr client) {
    int mode = 0;


    VIR_DEBUG("tls=%p hs=%d, rx=%p tx=%p",
146
#ifdef WITH_GNUTLS
147 148
              client->tls,
              client->tls ? virNetTLSSessionGetHandshakeStatus(client->tls) : -1,
149 150 151
#else
              NULL, -1,
#endif
152 153 154 155 156
              client->rx,
              client->tx);
    if (!client->sock || client->wantClose)
        return 0;

157
#if WITH_GNUTLS
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
    if (client->tls) {
        switch (virNetTLSSessionGetHandshakeStatus(client->tls)) {
        case VIR_NET_TLS_HANDSHAKE_RECVING:
            mode |= VIR_EVENT_HANDLE_READABLE;
            break;
        case VIR_NET_TLS_HANDSHAKE_SENDING:
            mode |= VIR_EVENT_HANDLE_WRITABLE;
            break;
        default:
        case VIR_NET_TLS_HANDSHAKE_COMPLETE:
            if (client->rx)
                mode |= VIR_EVENT_HANDLE_READABLE;
            if (client->tx)
                mode |= VIR_EVENT_HANDLE_WRITABLE;
        }
    } else {
174
#endif
175 176
        /* If there is a message on the rx queue, and
         * we're not in middle of a delayedClose, then
177
         * we're wanting more input */
178
        if (client->rx && !client->delayedClose)
179 180 181 182 183 184
            mode |= VIR_EVENT_HANDLE_READABLE;

        /* If there are one or more messages to send back to client,
           then monitor for writability on socket */
        if (client->tx)
            mode |= VIR_EVENT_HANDLE_WRITABLE;
185
#if WITH_GNUTLS
186
    }
187
#endif
188
    VIR_DEBUG("mode=%o", mode);
189 190 191 192 193 194 195 196 197 198 199
    return mode;
}

/*
 * @server: a locked or unlocked server object
 * @client: a locked client object
 */
static int virNetServerClientRegisterEvent(virNetServerClientPtr client)
{
    int mode = virNetServerClientCalculateHandleMode(client);

200 201 202 203
    if (!client->sock)
        return -1;

    virObjectRef(client);
204
    VIR_DEBUG("Registering client event callback %d", mode);
205
    if (virNetSocketAddIOCallback(client->sock,
206 207
                                  mode,
                                  virNetServerClientDispatchEvent,
208
                                  client,
209 210
                                  virObjectFreeCallback) < 0) {
        virObjectUnref(client);
211
        return -1;
212
    }
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229

    return 0;
}

/*
 * @client: a locked client object
 */
static void virNetServerClientUpdateEvent(virNetServerClientPtr client)
{
    int mode;

    if (!client->sock)
        return;

    mode = virNetServerClientCalculateHandleMode(client);

    virNetSocketUpdateIOCallback(client->sock, mode);
M
Michal Privoznik 已提交
230 231 232

    if (client->rx && virNetSocketHasCachedData(client->sock))
        virEventUpdateTimeout(client->sockTimer, 0);
233 234 235
}


236 237 238
int virNetServerClientAddFilter(virNetServerClientPtr client,
                                virNetServerClientFilterFunc func,
                                void *opaque)
239 240
{
    virNetServerClientFilterPtr filter;
241
    virNetServerClientFilterPtr *place;
242
    int ret;
243

244
    if (VIR_ALLOC(filter) < 0)
245
        return -1;
246

247
    virObjectLock(client);
248

249 250 251 252
    filter->id = client->nextFilterID++;
    filter->func = func;
    filter->opaque = opaque;

253 254 255 256
    place = &client->filters;
    while (*place)
        place = &(*place)->next;
    *place = filter;
257 258 259

    ret = filter->id;

260
    virObjectUnlock(client);
261

262 263 264
    return ret;
}

265 266
void virNetServerClientRemoveFilter(virNetServerClientPtr client,
                                    int filterID)
267 268 269
{
    virNetServerClientFilterPtr tmp, prev;

270
    virObjectLock(client);
271

272 273 274 275 276 277 278 279 280 281 282 283
    prev = NULL;
    tmp = client->filters;
    while (tmp) {
        if (tmp->id == filterID) {
            if (prev)
                prev->next = tmp->next;
            else
                client->filters = tmp->next;

            VIR_FREE(tmp);
            break;
        }
E
Eric Blake 已提交
284
        prev = tmp;
285 286 287
        tmp = tmp->next;
    }

288
    virObjectUnlock(client);
289 290 291
}


292
#ifdef WITH_GNUTLS
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
/* Check the client's access. */
static int
virNetServerClientCheckAccess(virNetServerClientPtr client)
{
    virNetMessagePtr confirm;

    /* Verify client certificate. */
    if (virNetTLSContextCheckCertificate(client->tlsCtxt, client->tls) < 0)
        return -1;

    if (client->tx) {
        VIR_DEBUG("client had unexpected data pending tx after access check");
        return -1;
    }

308
    if (!(confirm = virNetMessageNew(false)))
309 310 311 312 313 314 315
        return -1;

    /* Checks have succeeded.  Write a '\1' byte back to the client to
     * indicate this (otherwise the socket is abruptly closed).
     * (NB. The '\1' byte is sent in an encrypted record).
     */
    confirm->bufferLength = 1;
316 317 318 319
    if (VIR_ALLOC_N(confirm->buffer, confirm->bufferLength) < 0) {
        virNetMessageFree(confirm);
        return -1;
    }
320 321 322 323 324 325 326
    confirm->bufferOffset = 0;
    confirm->buffer[0] = '\1';

    client->tx = confirm;

    return 0;
}
327 328
#endif

329

M
Michal Privoznik 已提交
330 331 332 333
static void virNetServerClientSockTimerFunc(int timer,
                                            void *opaque)
{
    virNetServerClientPtr client = opaque;
334
    virObjectLock(client);
M
Michal Privoznik 已提交
335 336 337 338 339
    virEventUpdateTimeout(timer, -1);
    /* Although client->rx != NULL when this timer is enabled, it might have
     * changed since the client was unlocked in the meantime. */
    if (client->rx)
        virNetServerClientDispatchRead(client);
340
    virObjectUnlock(client);
M
Michal Privoznik 已提交
341 342
}

343

344 345 346
static virNetServerClientPtr
virNetServerClientNewInternal(virNetSocketPtr sock,
                              int auth,
347
#ifdef WITH_GNUTLS
348 349
                              virNetTLSContextPtr tls,
#endif
350
                              bool readonly,
351
                              size_t nrequests_max)
352 353 354
{
    virNetServerClientPtr client;

355
    if (virNetServerClientInitialize() < 0)
356 357
        return NULL;

358
    if (!(client = virObjectLockableNew(virNetServerClientClass)))
359 360
        return NULL;

361
    client->sock = virObjectRef(sock);
362 363
    client->auth = auth;
    client->readonly = readonly;
364
#ifdef WITH_GNUTLS
365
    client->tlsCtxt = virObjectRef(tls);
366
#endif
367
    client->nrequests_max = nrequests_max;
368

M
Michal Privoznik 已提交
369 370 371 372 373
    client->sockTimer = virEventAddTimeout(-1, virNetServerClientSockTimerFunc,
                                           client, NULL);
    if (client->sockTimer < 0)
        goto error;

374
    /* Prepare one for packet receive */
375
    if (!(client->rx = virNetMessageNew(true)))
376 377
        goto error;
    client->rx->bufferLength = VIR_NET_MESSAGE_LEN_MAX;
378
    if (VIR_ALLOC_N(client->rx->buffer, client->rx->bufferLength) < 0)
379
        goto error;
380 381
    client->nrequests = 1;

382
    PROBE(RPC_SERVER_CLIENT_NEW,
383 384
          "client=%p sock=%p",
          client, client->sock);
385 386 387 388

    return client;

error:
389
    virObjectUnref(client);
390 391 392 393
    return NULL;
}


394 395 396 397
virNetServerClientPtr virNetServerClientNew(virNetSocketPtr sock,
                                            int auth,
                                            bool readonly,
                                            size_t nrequests_max,
398
#ifdef WITH_GNUTLS
399
                                            virNetTLSContextPtr tls,
400
#endif
401
                                            virNetServerClientPrivNew privNew,
402
                                            virNetServerClientPrivPreExecRestart privPreExecRestart,
403 404 405 406 407
                                            virFreeCallback privFree,
                                            void *privOpaque)
{
    virNetServerClientPtr client;

408
    VIR_DEBUG("sock=%p auth=%d tls=%p", sock, auth,
409
#ifdef WITH_GNUTLS
410 411 412 413 414
              tls
#else
              NULL
#endif
        );
415

416
    if (!(client = virNetServerClientNewInternal(sock, auth,
417
#ifdef WITH_GNUTLS
418 419 420
                                                 tls,
#endif
                                                 readonly, nrequests_max)))
421 422 423 424 425 426 427 428
        return NULL;

    if (privNew) {
        if (!(client->privateData = privNew(client, privOpaque))) {
            virObjectUnref(client);
            return NULL;
        }
        client->privateDataFreeFunc = privFree;
429
        client->privateDataPreExecRestart = privPreExecRestart;
430 431 432 433 434 435
    }

    return client;
}


436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
virNetServerClientPtr virNetServerClientNewPostExecRestart(virJSONValuePtr object,
                                                           virNetServerClientPrivNewPostExecRestart privNew,
                                                           virNetServerClientPrivPreExecRestart privPreExecRestart,
                                                           virFreeCallback privFree,
                                                           void *privOpaque)
{
    virJSONValuePtr child;
    virNetServerClientPtr client = NULL;
    virNetSocketPtr sock;
    int auth;
    bool readonly;
    unsigned int nrequests_max;

    if (virJSONValueObjectGetNumberInt(object, "auth", &auth) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Missing auth field in JSON state document"));
        return NULL;
    }
    if (virJSONValueObjectGetBoolean(object, "readonly", &readonly) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Missing readonly field in JSON state document"));
        return NULL;
    }
    if (virJSONValueObjectGetNumberUint(object, "nrequests_max",
                                        (unsigned int *)&nrequests_max) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Missing nrequests_client_max field in JSON state document"));
        return NULL;
    }

    if (!(child = virJSONValueObjectGet(object, "sock"))) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Missing sock field in JSON state document"));
        return NULL;
    }

    if (!(sock = virNetSocketNewPostExecRestart(child))) {
        virObjectUnref(sock);
        return NULL;
    }

    if (!(client = virNetServerClientNewInternal(sock,
                                                 auth,
479
#ifdef WITH_GNUTLS
480 481
                                                 NULL,
#endif
482
                                                 readonly,
483
                                                 nrequests_max))) {
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
        virObjectUnref(sock);
        return NULL;
    }
    virObjectUnref(sock);

    if (privNew) {
        if (!(child = virJSONValueObjectGet(object, "privateData"))) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("Missing privateData field in JSON state document"));
            goto error;
        }
        if (!(client->privateData = privNew(client, child, privOpaque))) {
            goto error;
        }
        client->privateDataFreeFunc = privFree;
        client->privateDataPreExecRestart = privPreExecRestart;
    }


    return client;

error:
    virObjectUnref(client);
    return NULL;
}


virJSONValuePtr virNetServerClientPreExecRestart(virNetServerClientPtr client)
{
    virJSONValuePtr object = virJSONValueNewObject();
    virJSONValuePtr child;

    if (!object)
        return NULL;

519
    virObjectLock(client);
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544

    if (virJSONValueObjectAppendNumberInt(object, "auth", client->auth) < 0)
        goto error;
    if (virJSONValueObjectAppendBoolean(object, "readonly", client->readonly) < 0)
        goto error;
    if (virJSONValueObjectAppendNumberUint(object, "nrequests_max", client->nrequests_max) < 0)
        goto error;

    if (!(child = virNetSocketPreExecRestart(client->sock)))
        goto error;

    if (virJSONValueObjectAppend(object, "sock", child) < 0) {
        virJSONValueFree(child);
        goto error;
    }

    if (client->privateData && client->privateDataPreExecRestart &&
        !(child = client->privateDataPreExecRestart(client, client->privateData)))
        goto error;

    if (virJSONValueObjectAppend(object, "privateData", child) < 0) {
        virJSONValueFree(child);
        goto error;
    }

545
    virObjectUnlock(client);
546 547 548
    return object;

error:
549
    virObjectUnlock(client);
550 551 552 553 554
    virJSONValueFree(object);
    return NULL;
}


555 556 557
int virNetServerClientGetAuth(virNetServerClientPtr client)
{
    int auth;
558
    virObjectLock(client);
559
    auth = client->auth;
560
    virObjectUnlock(client);
561 562 563
    return auth;
}

564 565 566 567 568 569 570
void virNetServerClientSetAuth(virNetServerClientPtr client, int auth)
{
    virObjectLock(client);
    client->auth = auth;
    virObjectUnlock(client);
}

571 572 573
bool virNetServerClientGetReadonly(virNetServerClientPtr client)
{
    bool readonly;
574
    virObjectLock(client);
575
    readonly = client->readonly;
576
    virObjectUnlock(client);
577 578 579 580
    return readonly;
}


581
#ifdef WITH_GNUTLS
582 583 584
bool virNetServerClientHasTLSSession(virNetServerClientPtr client)
{
    bool has;
585
    virObjectLock(client);
586
    has = client->tls ? true : false;
587
    virObjectUnlock(client);
588 589 590
    return has;
}

591 592 593 594 595 596 597 598 599 600

virNetTLSSessionPtr virNetServerClientGetTLSSession(virNetServerClientPtr client)
{
    virNetTLSSessionPtr tls;
    virObjectLock(client);
    tls = client->tls;
    virObjectUnlock(client);
    return tls;
}

601 602 603
int virNetServerClientGetTLSKeySize(virNetServerClientPtr client)
{
    int size = 0;
604
    virObjectLock(client);
605 606
    if (client->tls)
        size = virNetTLSSessionGetKeySize(client->tls);
607
    virObjectUnlock(client);
608 609
    return size;
}
610
#endif
611 612 613

int virNetServerClientGetFD(virNetServerClientPtr client)
{
614
    int fd = -1;
615
    virObjectLock(client);
616 617
    if (client->sock)
        fd = virNetSocketGetFD(client->sock);
618
    virObjectUnlock(client);
619 620 621
    return fd;
}

622 623 624 625 626 627 628 629 630 631 632 633

bool virNetServerClientIsLocal(virNetServerClientPtr client)
{
    bool local = false;
    virObjectLock(client);
    if (client->sock)
        local = virNetSocketIsLocal(client->sock);
    virObjectUnlock(client);
    return local;
}


634
int virNetServerClientGetUNIXIdentity(virNetServerClientPtr client,
635 636
                                      uid_t *uid, gid_t *gid, pid_t *pid,
                                      unsigned long long *timestamp)
637
{
638
    int ret = -1;
639
    virObjectLock(client);
640
    if (client->sock)
641 642 643
        ret = virNetSocketGetUNIXIdentity(client->sock,
                                          uid, gid, pid,
                                          timestamp);
644
    virObjectUnlock(client);
645 646 647
    return ret;
}

648

649 650 651 652
static virIdentityPtr
virNetServerClientCreateIdentity(virNetServerClientPtr client)
{
    char *processid = NULL;
653
    char *processtime = NULL;
654
    char *username = NULL;
655
    char *userid = NULL;
656
    char *groupname = NULL;
657
    char *groupid = NULL;
658 659 660
#if WITH_SASL
    char *saslname = NULL;
#endif
661
#if WITH_GNUTLS
662
    char *x509dname = NULL;
663
#endif
664 665 666 667 668 669 670
    char *seccontext = NULL;
    virIdentityPtr ret = NULL;

    if (client->sock && virNetSocketIsLocal(client->sock)) {
        gid_t gid;
        uid_t uid;
        pid_t pid;
671 672 673 674
        unsigned long long timestamp;
        if (virNetSocketGetUNIXIdentity(client->sock,
                                        &uid, &gid, &pid,
                                        &timestamp) < 0)
675 676 677 678
            goto cleanup;

        if (!(username = virGetUserName(uid)))
            goto cleanup;
679 680
        if (virAsprintf(&userid, "%d", (int)uid) < 0)
            goto cleanup;
681 682
        if (!(groupname = virGetGroupName(gid)))
            goto cleanup;
683
        if (virAsprintf(&groupid, "%d", (int)gid) < 0)
684
            goto cleanup;
685
        if (virAsprintf(&processid, "%llu",
686
                        (unsigned long long)pid) < 0)
687 688
            goto cleanup;
        if (virAsprintf(&processtime, "%llu",
689
                        timestamp) < 0)
690 691 692 693 694 695
            goto cleanup;
    }

#if WITH_SASL
    if (client->sasl) {
        const char *identity = virNetSASLSessionGetIdentity(client->sasl);
696
        if (VIR_STRDUP(saslname, identity) < 0)
697 698 699 700
            goto cleanup;
    }
#endif

701
#if WITH_GNUTLS
702 703
    if (client->tls) {
        const char *identity = virNetTLSSessionGetX509DName(client->tls);
704
        if (VIR_STRDUP(x509dname, identity) < 0)
705 706
            goto cleanup;
    }
707
#endif
708 709

    if (client->sock &&
710
        virNetSocketGetSELinuxContext(client->sock, &seccontext) < 0)
711 712 713 714 715 716 717 718 719 720
        goto cleanup;

    if (!(ret = virIdentityNew()))
        goto cleanup;

    if (username &&
        virIdentitySetAttr(ret,
                           VIR_IDENTITY_ATTR_UNIX_USER_NAME,
                           username) < 0)
        goto error;
721 722 723 724 725
    if (userid &&
        virIdentitySetAttr(ret,
                           VIR_IDENTITY_ATTR_UNIX_USER_ID,
                           userid) < 0)
        goto error;
726 727 728 729 730
    if (groupname &&
        virIdentitySetAttr(ret,
                           VIR_IDENTITY_ATTR_UNIX_GROUP_NAME,
                           groupname) < 0)
        goto error;
731 732 733 734 735
    if (groupid &&
        virIdentitySetAttr(ret,
                           VIR_IDENTITY_ATTR_UNIX_GROUP_ID,
                           groupid) < 0)
        goto error;
736 737 738 739 740
    if (processid &&
        virIdentitySetAttr(ret,
                           VIR_IDENTITY_ATTR_UNIX_PROCESS_ID,
                           processid) < 0)
        goto error;
741 742 743 744 745
    if (processtime &&
        virIdentitySetAttr(ret,
                           VIR_IDENTITY_ATTR_UNIX_PROCESS_TIME,
                           processtime) < 0)
        goto error;
746
#if WITH_SASL
747 748 749 750 751 752
    if (saslname &&
        virIdentitySetAttr(ret,
                           VIR_IDENTITY_ATTR_SASL_USER_NAME,
                           saslname) < 0)
        goto error;
#endif
753
#if WITH_GNUTLS
754 755 756 757 758
    if (x509dname &&
        virIdentitySetAttr(ret,
                           VIR_IDENTITY_ATTR_X509_DISTINGUISHED_NAME,
                           x509dname) < 0)
        goto error;
759
#endif
760 761
    if (seccontext &&
        virIdentitySetAttr(ret,
762
                           VIR_IDENTITY_ATTR_SELINUX_CONTEXT,
763 764 765 766 767
                           seccontext) < 0)
        goto error;

cleanup:
    VIR_FREE(username);
768
    VIR_FREE(userid);
769
    VIR_FREE(groupname);
770
    VIR_FREE(groupid);
771
    VIR_FREE(processid);
772
    VIR_FREE(processtime);
773
    VIR_FREE(seccontext);
774
#if WITH_SASL
775 776
    VIR_FREE(saslname);
#endif
777
#if WITH_GNUTLS
778
    VIR_FREE(x509dname);
779
#endif
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
    return ret;

error:
    virObjectUnref(ret);
    ret = NULL;
    goto cleanup;
}


virIdentityPtr virNetServerClientGetIdentity(virNetServerClientPtr client)
{
    virIdentityPtr ret = NULL;
    virObjectLock(client);
    if (!client->identity)
        client->identity = virNetServerClientCreateIdentity(client);
    if (client->identity)
        ret = virObjectRef(client->identity);
    virObjectUnlock(client);
    return ret;
}


802 803
int virNetServerClientGetSELinuxContext(virNetServerClientPtr client,
                                        char **context)
804 805 806 807 808
{
    int ret = 0;
    *context = NULL;
    virObjectLock(client);
    if (client->sock)
809
        ret = virNetSocketGetSELinuxContext(client->sock, context);
810 811 812 813 814
    virObjectUnlock(client);
    return ret;
}


815 816 817
bool virNetServerClientIsSecure(virNetServerClientPtr client)
{
    bool secure = false;
818
    virObjectLock(client);
819
#if WITH_GNUTLS
820 821
    if (client->tls)
        secure = true;
822
#endif
823
#if WITH_SASL
824 825 826
    if (client->sasl)
        secure = true;
#endif
827
    if (client->sock && virNetSocketIsLocal(client->sock))
828
        secure = true;
829
    virObjectUnlock(client);
830 831 832 833
    return secure;
}


834
#if WITH_SASL
835 836 837 838 839 840 841 842
void virNetServerClientSetSASLSession(virNetServerClientPtr client,
                                      virNetSASLSessionPtr sasl)
{
    /* We don't set the sasl session on the socket here
     * because we need to send out the auth confirmation
     * in the clear. Only once we complete the next 'tx'
     * operation do we switch to SASL mode
     */
843
    virObjectLock(client);
844
    client->sasl = virObjectRef(sasl);
845
    virObjectUnlock(client);
846
}
847 848 849 850 851 852 853 854 855 856


virNetSASLSessionPtr virNetServerClientGetSASLSession(virNetServerClientPtr client)
{
    virNetSASLSessionPtr sasl;
    virObjectLock(client);
    sasl = client->sasl;
    virObjectUnlock(client);
    return sasl;
}
857 858 859 860 861 862
#endif


void *virNetServerClientGetPrivateData(virNetServerClientPtr client)
{
    void *data;
863
    virObjectLock(client);
864
    data = client->privateData;
865
    virObjectUnlock(client);
866 867 868 869
    return data;
}


870 871 872
void virNetServerClientSetCloseHook(virNetServerClientPtr client,
                                    virNetServerClientCloseFunc cf)
{
873
    virObjectLock(client);
874
    client->privateDataCloseFunc = cf;
875
    virObjectUnlock(client);
876 877 878
}


879 880 881 882
void virNetServerClientSetDispatcher(virNetServerClientPtr client,
                                     virNetServerClientDispatchFunc func,
                                     void *opaque)
{
883
    virObjectLock(client);
884 885
    client->dispatchFunc = func;
    client->dispatchOpaque = opaque;
886
    virObjectUnlock(client);
887 888 889 890 891
}


const char *virNetServerClientLocalAddrString(virNetServerClientPtr client)
{
892 893
    if (!client->sock)
        return NULL;
894 895 896 897 898 899
    return virNetSocketLocalAddrString(client->sock);
}


const char *virNetServerClientRemoteAddrString(virNetServerClientPtr client)
{
900 901
    if (!client->sock)
        return NULL;
902 903 904 905
    return virNetSocketRemoteAddrString(client->sock);
}


906
void virNetServerClientDispose(void *obj)
907
{
908
    virNetServerClientPtr client = obj;
909

910 911 912
    PROBE(RPC_SERVER_CLIENT_DISPOSE,
          "client=%p", client);

913 914
    virObjectUnref(client->identity);

915 916 917 918
    if (client->privateData &&
        client->privateDataFreeFunc)
        client->privateDataFreeFunc(client->privateData);

919
#if WITH_SASL
920
    virObjectUnref(client->sasl);
921
#endif
M
Michal Privoznik 已提交
922 923
    if (client->sockTimer > 0)
        virEventRemoveTimeout(client->sockTimer);
924
#if WITH_GNUTLS
925 926
    virObjectUnref(client->tls);
    virObjectUnref(client->tlsCtxt);
927
#endif
928
    virObjectUnref(client->sock);
929
    virObjectUnlock(client);
930 931 932 933 934 935 936 937 938 939 940 941 942
}


/*
 *
 * We don't free stuff here, merely disconnect the client's
 * network socket & resources.
 *
 * Full free of the client is done later in a safe point
 * where it can be guaranteed it is no longer in use
 */
void virNetServerClientClose(virNetServerClientPtr client)
{
943
    virNetServerClientCloseFunc cf;
944
    virKeepAlivePtr ka;
945

946
    virObjectLock(client);
947
    VIR_DEBUG("client=%p", client);
948
    if (!client->sock) {
949
        virObjectUnlock(client);
950 951 952
        return;
    }

953 954 955 956
    if (client->keepalive) {
        virKeepAliveStop(client->keepalive);
        ka = client->keepalive;
        client->keepalive = NULL;
957
        virObjectRef(client);
958
        virObjectUnlock(client);
959
        virObjectUnref(ka);
960
        virObjectLock(client);
961
        virObjectUnref(client);
962 963
    }

964 965
    if (client->privateDataCloseFunc) {
        cf = client->privateDataCloseFunc;
966
        virObjectRef(client);
967
        virObjectUnlock(client);
968
        (cf)(client);
969
        virObjectLock(client);
970
        virObjectUnref(client);
971 972
    }

973 974 975 976 977 978
    /* Do now, even though we don't close the socket
     * until end, to ensure we don't get invoked
     * again due to tls shutdown */
    if (client->sock)
        virNetSocketRemoveIOCallback(client->sock);

979
#if WITH_GNUTLS
980
    if (client->tls) {
981
        virObjectUnref(client->tls);
982 983
        client->tls = NULL;
    }
984
#endif
985
    client->wantClose = true;
986 987 988 989 990 991 992 993 994 995 996 997

    while (client->rx) {
        virNetMessagePtr msg
            = virNetMessageQueueServe(&client->rx);
        virNetMessageFree(msg);
    }
    while (client->tx) {
        virNetMessagePtr msg
            = virNetMessageQueueServe(&client->tx);
        virNetMessageFree(msg);
    }

998
    if (client->sock) {
999
        virObjectUnref(client->sock);
1000 1001 1002
        client->sock = NULL;
    }

1003
    virObjectUnlock(client);
1004 1005 1006 1007 1008 1009
}


bool virNetServerClientIsClosed(virNetServerClientPtr client)
{
    bool closed;
1010
    virObjectLock(client);
1011
    closed = client->sock == NULL ? true : false;
1012
    virObjectUnlock(client);
1013 1014 1015
    return closed;
}

1016 1017
void virNetServerClientDelayedClose(virNetServerClientPtr client)
{
1018
    virObjectLock(client);
1019
    client->delayedClose = true;
1020
    virObjectUnlock(client);
1021 1022 1023
}

void virNetServerClientImmediateClose(virNetServerClientPtr client)
1024
{
1025
    virObjectLock(client);
1026
    client->wantClose = true;
1027
    virObjectUnlock(client);
1028 1029 1030 1031 1032
}

bool virNetServerClientWantClose(virNetServerClientPtr client)
{
    bool wantClose;
1033
    virObjectLock(client);
1034
    wantClose = client->wantClose;
1035
    virObjectUnlock(client);
1036 1037 1038 1039 1040 1041
    return wantClose;
}


int virNetServerClientInit(virNetServerClientPtr client)
{
1042
    virObjectLock(client);
1043

1044
#if WITH_GNUTLS
1045
    if (!client->tlsCtxt) {
1046
#endif
1047 1048 1049
        /* Plain socket, so prepare to read first message */
        if (virNetServerClientRegisterEvent(client) < 0)
            goto error;
1050
#if WITH_GNUTLS
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
    } else {
        int ret;

        if (!(client->tls = virNetTLSSessionNew(client->tlsCtxt,
                                                NULL)))
            goto error;

        virNetSocketSetTLSSession(client->sock,
                                  client->tls);

        /* Begin the TLS handshake. */
        ret = virNetTLSSessionHandshake(client->tls);
        if (ret == 0) {
            /* Unlikely, but ...  Next step is to check the certificate. */
            if (virNetServerClientCheckAccess(client) < 0)
                goto error;

            /* Handshake & cert check OK,  so prepare to read first message */
            if (virNetServerClientRegisterEvent(client) < 0)
                goto error;
        } else if (ret > 0) {
            /* Most likely, need to do more handshake data */
            if (virNetServerClientRegisterEvent(client) < 0)
                goto error;
        } else {
            goto error;
        }
    }
1079
#endif
1080

1081
    virObjectUnlock(client);
1082 1083 1084 1085
    return 0;

error:
    client->wantClose = true;
1086
    virObjectUnlock(client);
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
    return -1;
}



/*
 * Read data into buffer using wire decoding (plain or TLS)
 *
 * Returns:
 *   -1 on error or EOF
 *    0 on EAGAIN
 *    n number of bytes
 */
static ssize_t virNetServerClientRead(virNetServerClientPtr client)
{
    ssize_t ret;

    if (client->rx->bufferLength <= client->rx->bufferOffset) {
1105 1106 1107
        virReportError(VIR_ERR_RPC,
                       _("unexpected zero/negative length request %lld"),
                       (long long int)(client->rx->bufferLength - client->rx->bufferOffset));
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
        client->wantClose = true;
        return -1;
    }

    ret = virNetSocketRead(client->sock,
                           client->rx->buffer + client->rx->bufferOffset,
                           client->rx->bufferLength - client->rx->bufferOffset);

    if (ret <= 0)
        return ret;

    client->rx->bufferOffset += ret;
    return ret;
}


/*
 * Read data until we get a complete message to process
 */
static void virNetServerClientDispatchRead(virNetServerClientPtr client)
{
readmore:
1130 1131 1132 1133 1134
    if (client->rx->nfds == 0) {
        if (virNetServerClientRead(client) < 0) {
            client->wantClose = true;
            return; /* Error */
        }
1135 1136 1137 1138 1139 1140 1141
    }

    if (client->rx->bufferOffset < client->rx->bufferLength)
        return; /* Still not read enough */

    /* Either done with length word header */
    if (client->rx->bufferLength == VIR_NET_MESSAGE_LEN_MAX) {
1142 1143
        if (virNetMessageDecodeLength(client->rx) < 0) {
            client->wantClose = true;
1144
            return;
1145
        }
1146 1147 1148 1149 1150 1151 1152 1153 1154

        virNetServerClientUpdateEvent(client);

        /* Try and read payload immediately instead of going back
           into poll() because chances are the data is already
           waiting for us */
        goto readmore;
    } else {
        /* Grab the completed message */
1155
        virNetMessagePtr msg = client->rx;
1156
        virNetMessagePtr response = NULL;
1157
        virNetServerClientFilterPtr filter;
1158
        size_t i;
1159 1160 1161

        /* Decode the header so we can use it for routing decisions */
        if (virNetMessageDecodeHeader(msg) < 0) {
1162
            virNetMessageQueueServe(&client->rx);
1163 1164 1165 1166 1167
            virNetMessageFree(msg);
            client->wantClose = true;
            return;
        }

1168 1169
        /* Now figure out if we need to read more data to get some
         * file descriptors */
1170 1171
        if (msg->header.type == VIR_NET_CALL_WITH_FDS &&
            virNetMessageDecodeNumFDs(msg) < 0) {
1172
            virNetMessageQueueServe(&client->rx);
1173 1174
            virNetMessageFree(msg);
            client->wantClose = true;
1175
            return; /* Error */
1176
        }
1177 1178

        /* Try getting the file descriptors (may fail if blocking) */
1179
        for (i = msg->donefds; i < msg->nfds; i++) {
1180 1181
            int rv;
            if ((rv = virNetSocketRecvFD(client->sock, &(msg->fds[i]))) < 0) {
1182
                virNetMessageQueueServe(&client->rx);
1183 1184 1185 1186
                virNetMessageFree(msg);
                client->wantClose = true;
                return;
            }
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
            if (rv == 0) /* Blocking */
                break;
            msg->donefds++;
        }

        /* Need to poll() until FDs arrive */
        if (msg->donefds < msg->nfds) {
            /* Because DecodeHeader/NumFDs reset bufferOffset, we
             * put it back to what it was, so everything works
             * again next time we run this method
             */
            client->rx->bufferOffset = client->rx->bufferLength;
            return;
1200 1201
        }

1202 1203
        /* Definitely finished reading, so remove from queue */
        virNetMessageQueueServe(&client->rx);
1204 1205 1206 1207 1208 1209
        PROBE(RPC_SERVER_CLIENT_MSG_RX,
              "client=%p len=%zu prog=%u vers=%u proc=%u type=%u status=%u serial=%u",
              client, msg->bufferLength,
              msg->header.prog, msg->header.vers, msg->header.proc,
              msg->header.type, msg->header.status, msg->header.serial);

1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
        if (virKeepAliveCheckMessage(client->keepalive, msg, &response)) {
            virNetMessageFree(msg);
            client->nrequests--;
            msg = NULL;

            if (response &&
                virNetServerClientSendMessageLocked(client, response) < 0)
                virNetMessageFree(response);
        }

1220
        /* Maybe send off for queue against a filter */
1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235
        if (msg) {
            filter = client->filters;
            while (filter) {
                int ret = filter->func(client, msg, filter->opaque);
                if (ret < 0) {
                    virNetMessageFree(msg);
                    msg = NULL;
                    if (ret < 0)
                        client->wantClose = true;
                    break;
                }
                if (ret > 0) {
                    msg = NULL;
                    break;
                }
1236

1237 1238
                filter = filter->next;
            }
1239 1240 1241 1242
        }

        /* Send off to for normal dispatch to workers */
        if (msg) {
1243
            virObjectRef(client);
1244 1245 1246 1247
            if (!client->dispatchFunc ||
                client->dispatchFunc(client, msg, client->dispatchOpaque) < 0) {
                virNetMessageFree(msg);
                client->wantClose = true;
1248
                virObjectUnref(client);
1249 1250 1251 1252 1253 1254
                return;
            }
        }

        /* Possibly need to create another receive buffer */
        if (client->nrequests < client->nrequests_max) {
1255
            if (!(client->rx = virNetMessageNew(true))) {
1256
                client->wantClose = true;
E
Eric Blake 已提交
1257 1258
            } else {
                client->rx->bufferLength = VIR_NET_MESSAGE_LEN_MAX;
1259 1260 1261 1262 1263 1264
                if (VIR_ALLOC_N(client->rx->buffer,
                                client->rx->bufferLength) < 0) {
                    client->wantClose = true;
                } else {
                    client->nrequests++;
                }
1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284
            }
        }
        virNetServerClientUpdateEvent(client);
    }
}


/*
 * Send client->tx using no encoding
 *
 * Returns:
 *   -1 on error or EOF
 *    0 on EAGAIN
 *    n number of bytes
 */
static ssize_t virNetServerClientWrite(virNetServerClientPtr client)
{
    ssize_t ret;

    if (client->tx->bufferLength < client->tx->bufferOffset) {
1285 1286 1287
        virReportError(VIR_ERR_RPC,
                       _("unexpected zero/negative length request %lld"),
                       (long long int)(client->tx->bufferLength - client->tx->bufferOffset));
1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313
        client->wantClose = true;
        return -1;
    }

    if (client->tx->bufferLength == client->tx->bufferOffset)
        return 1;

    ret = virNetSocketWrite(client->sock,
                            client->tx->buffer + client->tx->bufferOffset,
                            client->tx->bufferLength - client->tx->bufferOffset);
    if (ret <= 0)
        return ret; /* -1 error, 0 = egain */

    client->tx->bufferOffset += ret;
    return ret;
}


/*
 * Process all queued client->tx messages until
 * we would block on I/O
 */
static void
virNetServerClientDispatchWrite(virNetServerClientPtr client)
{
    while (client->tx) {
1314 1315 1316 1317 1318 1319 1320 1321 1322
        if (client->tx->bufferOffset < client->tx->bufferLength) {
            ssize_t ret;
            ret = virNetServerClientWrite(client);
            if (ret < 0) {
                client->wantClose = true;
                return;
            }
            if (ret == 0)
                return; /* Would block on write EAGAIN */
1323 1324 1325 1326
        }

        if (client->tx->bufferOffset == client->tx->bufferLength) {
            virNetMessagePtr msg;
1327 1328
            size_t i;

1329
            for (i = client->tx->donefds; i < client->tx->nfds; i++) {
1330 1331
                int rv;
                if ((rv = virNetSocketSendFD(client->sock, client->tx->fds[i])) < 0) {
1332 1333 1334
                    client->wantClose = true;
                    return;
                }
1335 1336 1337
                if (rv == 0) /* Blocking */
                    return;
                client->tx->donefds++;
1338 1339
            }

1340
#if WITH_SASL
1341 1342 1343 1344 1345
            /* Completed this 'tx' operation, so now read for all
             * future rx/tx to be under a SASL SSF layer
             */
            if (client->sasl) {
                virNetSocketSetSASLSession(client->sock, client->sasl);
1346
                virObjectUnref(client->sasl);
1347 1348 1349 1350 1351 1352 1353
                client->sasl = NULL;
            }
#endif

            /* Get finished msg from head of tx queue */
            msg = virNetMessageQueueServe(&client->tx);

1354
            if (msg->tracked) {
1355 1356 1357 1358 1359
                client->nrequests--;
                /* See if the recv queue is currently throttled */
                if (!client->rx &&
                    client->nrequests < client->nrequests_max) {
                    /* Ready to recv more messages */
1360
                    virNetMessageClear(msg);
1361 1362 1363 1364 1365
                    msg->bufferLength = VIR_NET_MESSAGE_LEN_MAX;
                    if (VIR_ALLOC_N(msg->buffer, msg->bufferLength) < 0) {
                        virNetMessageFree(msg);
                        return;
                    }
1366 1367 1368 1369 1370 1371 1372 1373 1374
                    client->rx = msg;
                    msg = NULL;
                    client->nrequests++;
                }
            }

            virNetMessageFree(msg);

            virNetServerClientUpdateEvent(client);
1375 1376 1377

            if (client->delayedClose)
                client->wantClose = true;
1378 1379 1380 1381
         }
    }
}

1382

1383
#if WITH_GNUTLS
1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399
static void
virNetServerClientDispatchHandshake(virNetServerClientPtr client)
{
    int ret;
    /* Continue the handshake. */
    ret = virNetTLSSessionHandshake(client->tls);
    if (ret == 0) {
        /* Finished.  Next step is to check the certificate. */
        if (virNetServerClientCheckAccess(client) < 0)
            client->wantClose = true;
        else
            virNetServerClientUpdateEvent(client);
    } else if (ret > 0) {
        /* Carry on waiting for more handshake. Update
           the events just in case handshake data flow
           direction has changed */
1400
        virNetServerClientUpdateEvent(client);
1401 1402 1403 1404 1405
    } else {
        /* Fatal error in handshake */
        client->wantClose = true;
    }
}
1406
#endif
1407 1408 1409 1410 1411 1412

static void
virNetServerClientDispatchEvent(virNetSocketPtr sock, int events, void *opaque)
{
    virNetServerClientPtr client = opaque;

1413
    virObjectLock(client);
1414 1415 1416

    if (client->sock != sock) {
        virNetSocketRemoveIOCallback(sock);
1417
        virObjectUnlock(client);
1418 1419 1420 1421 1422
        return;
    }

    if (events & (VIR_EVENT_HANDLE_WRITABLE |
                  VIR_EVENT_HANDLE_READABLE)) {
1423
#if WITH_GNUTLS
1424 1425 1426 1427 1428
        if (client->tls &&
            virNetTLSSessionGetHandshakeStatus(client->tls) !=
            VIR_NET_TLS_HANDSHAKE_COMPLETE) {
            virNetServerClientDispatchHandshake(client);
        } else {
1429
#endif
1430 1431
            if (events & VIR_EVENT_HANDLE_WRITABLE)
                virNetServerClientDispatchWrite(client);
M
Michal Privoznik 已提交
1432 1433
            if (events & VIR_EVENT_HANDLE_READABLE &&
                client->rx)
1434
                virNetServerClientDispatchRead(client);
1435
#if WITH_GNUTLS
1436
        }
1437
#endif
1438 1439 1440 1441 1442 1443 1444 1445
    }

    /* NB, will get HANGUP + READABLE at same time upon
     * disconnect */
    if (events & (VIR_EVENT_HANDLE_ERROR |
                  VIR_EVENT_HANDLE_HANGUP))
        client->wantClose = true;

1446
    virObjectUnlock(client);
1447 1448 1449
}


1450 1451 1452
static int
virNetServerClientSendMessageLocked(virNetServerClientPtr client,
                                    virNetMessagePtr msg)
1453 1454 1455 1456 1457
{
    int ret = -1;
    VIR_DEBUG("msg=%p proc=%d len=%zu offset=%zu",
              msg, msg->header.proc,
              msg->bufferLength, msg->bufferOffset);
1458

1459
    msg->donefds = 0;
1460
    if (client->sock && !client->wantClose) {
1461 1462 1463 1464 1465
        PROBE(RPC_SERVER_CLIENT_MSG_TX_QUEUE,
              "client=%p len=%zu prog=%u vers=%u proc=%u type=%u status=%u serial=%u",
              client, msg->bufferLength,
              msg->header.prog, msg->header.vers, msg->header.proc,
              msg->header.type, msg->header.status, msg->header.serial);
1466 1467 1468 1469 1470 1471
        virNetMessageQueuePush(&client->tx, msg);

        virNetServerClientUpdateEvent(client);
        ret = 0;
    }

1472 1473 1474 1475 1476 1477 1478 1479
    return ret;
}

int virNetServerClientSendMessage(virNetServerClientPtr client,
                                  virNetMessagePtr msg)
{
    int ret;

1480
    virObjectLock(client);
1481
    ret = virNetServerClientSendMessageLocked(client, msg);
1482
    virObjectUnlock(client);
1483

1484 1485 1486 1487 1488 1489 1490
    return ret;
}


bool virNetServerClientNeedAuth(virNetServerClientPtr client)
{
    bool need = false;
1491
    virObjectLock(client);
1492
    if (client->auth)
1493
        need = true;
1494
    virObjectUnlock(client);
1495 1496
    return need;
}
1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520


static void
virNetServerClientKeepAliveDeadCB(void *opaque)
{
    virNetServerClientImmediateClose(opaque);
}

static int
virNetServerClientKeepAliveSendCB(void *opaque,
                                  virNetMessagePtr msg)
{
    return virNetServerClientSendMessage(opaque, msg);
}


int
virNetServerClientInitKeepAlive(virNetServerClientPtr client,
                                int interval,
                                unsigned int count)
{
    virKeepAlivePtr ka;
    int ret = -1;

1521
    virObjectLock(client);
1522 1523 1524 1525

    if (!(ka = virKeepAliveNew(interval, count, client,
                               virNetServerClientKeepAliveSendCB,
                               virNetServerClientKeepAliveDeadCB,
1526
                               virObjectFreeCallback)))
1527 1528
        goto cleanup;
    /* keepalive object has a reference to client */
1529
    virObjectRef(client);
1530 1531 1532 1533

    client->keepalive = ka;

cleanup:
1534
    virObjectUnlock(client);
1535 1536 1537 1538 1539 1540 1541 1542

    return ret;
}

int
virNetServerClientStartKeepAlive(virNetServerClientPtr client)
{
    int ret;
1543
    virObjectLock(client);
1544
    ret = virKeepAliveStart(client->keepalive, 0, 0);
1545
    virObjectUnlock(client);
1546 1547
    return ret;
}