log_daemon_dispatch.c 5.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/*
 * log_daemon_dispatch.c: log management daemon dispatch
 *
 * Copyright (C) 2006-2015 Red Hat, Inc.
 *
 * 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
 * License along with this library;  If not, see
 * <http://www.gnu.org/licenses/>.
 *
 * Author: Daniel P. Berrange <berrange@redhat.com>
 */

#include <config.h>

#include "rpc/virnetserver.h"
#include "rpc/virnetserverclient.h"
#include "virlog.h"
#include "virstring.h"
#include "log_daemon.h"
#include "log_protocol.h"
#include "virerror.h"
32 33
#include "virthreadjob.h"
#include "virfile.h"
34 35 36 37 38 39

#define VIR_FROM_THIS VIR_FROM_RPC

VIR_LOG_INIT("logging.log_daemon_dispatch");

#include "log_daemon_dispatch_stubs.h"
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120

static int
virLogManagerProtocolDispatchDomainOpenLogFile(virNetServerPtr server ATTRIBUTE_UNUSED,
                                               virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                               virNetMessagePtr msg,
                                               virNetMessageErrorPtr rerr,
                                               virLogManagerProtocolDomainOpenLogFileArgs *args,
                                               virLogManagerProtocolDomainOpenLogFileRet *ret)
{
    int fd = -1;
    int rv = -1;
    off_t offset;
    ino_t inode;

    if ((fd = virLogHandlerDomainOpenLogFile(virLogDaemonGetHandler(logDaemon),
                                             args->driver,
                                             (unsigned char *)args->dom.uuid,
                                             args->dom.name,
                                             &inode, &offset)) < 0)
        goto cleanup;

    ret->pos.inode = inode;
    ret->pos.offset = offset;

    if (virNetMessageAddFD(msg, fd) < 0)
        goto cleanup;

    rv = 1; /* '1' tells caller we added some FDs */

 cleanup:
    VIR_FORCE_CLOSE(fd);
    if (rv < 0)
        virNetMessageSaveError(rerr);
    return rv;
}


static int
virLogManagerProtocolDispatchDomainGetLogFilePosition(virNetServerPtr server ATTRIBUTE_UNUSED,
                                                      virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                                      virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                                      virNetMessageErrorPtr rerr,
                                                      virLogManagerProtocolDomainGetLogFilePositionArgs *args,
                                                      virLogManagerProtocolDomainGetLogFilePositionRet *ret)
{
    int rv = -1;
    off_t offset;
    ino_t inode;

    if (virLogHandlerDomainGetLogFilePosition(virLogDaemonGetHandler(logDaemon),
                                              args->driver,
                                              (unsigned char *)args->dom.uuid,
                                              args->dom.name,
                                              &inode, &offset) < 0)
        goto cleanup;

    ret->pos.inode = inode;
    ret->pos.offset = offset;

    rv = 0;
 cleanup:

    if (rv < 0)
        virNetMessageSaveError(rerr);
    return rv;
}


static int
virLogManagerProtocolDispatchDomainReadLogFile(virNetServerPtr server ATTRIBUTE_UNUSED,
                                               virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                               virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                               virNetMessageErrorPtr rerr,
                                               virLogManagerProtocolDomainReadLogFileArgs *args,
                                               virLogManagerProtocolDomainReadLogFileRet *ret)
{
    int rv = -1;
    char *data;

    if (args->maxlen > VIR_LOG_MANAGER_PROTOCOL_STRING_MAX) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
121 122 123
                       _("Requested data len %llu is larger than maximum %d"),
                       (unsigned long long)args->maxlen,
                       VIR_LOG_MANAGER_PROTOCOL_STRING_MAX);
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
        goto cleanup;
    }

    if ((data = virLogHandlerDomainReadLogFile(virLogDaemonGetHandler(logDaemon),
                                               args->driver,
                                               (unsigned char *)args->dom.uuid,
                                               args->dom.name,
                                               args->pos.inode,
                                               args->pos.offset,
                                               args->maxlen)) == NULL)
        goto cleanup;

    ret->data = data;

    rv = 0;

 cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
    return rv;
}