You need to sign in or sign up before continuing.
lxc_monitor.c 5.4 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
/*
 * Copyright (C) 2010-2012 Red Hat, Inc.
 *
 * lxc_monitor.c: client for LXC controller monitor
 *
 * 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/>.
 */

#include <config.h>

#include "lxc_monitor.h"
#include "lxc_conf.h"
25 26
#include "lxc_protocol.h"
#include "lxc_monitor_dispatch.h"
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

#include "memory.h"

#include "virterror_internal.h"
#include "logging.h"
#include "threads.h"
#include "rpc/virnetclient.h"

#define VIR_FROM_THIS VIR_FROM_LXC

struct _virLXCMonitor {
    int refs;

    virMutex lock; /* also used to protect refs */

    virDomainObjPtr vm;
    virLXCMonitorCallbacksPtr cb;

    virNetClientPtr client;
46
    virNetClientProgramPtr program;
47 48 49
};

static void virLXCMonitorFree(virLXCMonitorPtr mon);
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
static void
virLXCMonitorHandleEventExit(virNetClientProgramPtr prog,
                             virNetClientPtr client,
                             void *evdata, void *opaque);

static virNetClientProgramEvent virLXCProtocolEvents[] = {
    { VIR_LXC_PROTOCOL_PROC_EXIT_EVENT,
      virLXCMonitorHandleEventExit,
      sizeof(virLXCProtocolExitEventMsg),
      (xdrproc_t)xdr_virLXCProtocolExitEventMsg },
};


static void
virLXCMonitorHandleEventExit(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                             virNetClientPtr client ATTRIBUTE_UNUSED,
                             void *evdata, void *opaque)
{
    virLXCMonitorPtr mon = opaque;
    virLXCProtocolExitEventMsg *msg = evdata;

    VIR_DEBUG("Event exit %d", msg->status);
    if (mon->cb->exitNotify)
        mon->cb->exitNotify(mon, msg->status, mon->vm);
}
75 76 77 78 79 80 81 82 83 84


static void virLXCMonitorEOFNotify(virNetClientPtr client ATTRIBUTE_UNUSED,
                                   int reason ATTRIBUTE_UNUSED,
                                   void *opaque)
{
    virLXCMonitorPtr mon = opaque;
    virLXCMonitorCallbackEOFNotify eofNotify;
    virDomainObjPtr vm;

85
    VIR_DEBUG("EOF notify");
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 121 122 123 124 125 126 127 128 129 130 131
    virLXCMonitorLock(mon);
    eofNotify = mon->cb->eofNotify;
    vm = mon->vm;
    virLXCMonitorUnlock(mon);

    eofNotify(mon, vm);
}


static void virLXCMonitorCloseFreeCallback(void *opaque)
{
    virLXCMonitorPtr mon = opaque;
    virLXCMonitorLock(mon);
    if (virLXCMonitorUnref(mon) > 0)
        virLXCMonitorUnlock(mon);
}


virLXCMonitorPtr virLXCMonitorNew(virDomainObjPtr vm,
                                  const char *socketdir,
                                  virLXCMonitorCallbacksPtr cb)
{
    virLXCMonitorPtr mon;
    char *sockpath = NULL;

    if (VIR_ALLOC(mon) < 0) {
        virReportOOMError();
        return NULL;
    }

    mon->refs = 1;

    if (virMutexInit(&mon->lock) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("cannot initialize monitor mutex"));
        VIR_FREE(mon);
        return NULL;
    }

    if (virAsprintf(&sockpath, "%s/%s.sock",
                    socketdir, vm->def->name) < 0)
        goto no_memory;

    if (!(mon->client = virNetClientNewUNIX(sockpath, false, NULL)))
        goto error;

132 133
    if (virNetClientRegisterAsyncIO(mon->client) < 0)
        goto error;
134

135 136 137 138 139 140 141 142 143 144 145
    if (!(mon->program = virNetClientProgramNew(VIR_LXC_PROTOCOL_PROGRAM,
                                                VIR_LXC_PROTOCOL_PROGRAM_VERSION,
                                                virLXCProtocolEvents,
                                                ARRAY_CARDINALITY(virLXCProtocolEvents),
                                                mon)))
        goto error;

    if (virNetClientAddProgram(mon->client,
                               mon->program) < 0)
        goto error;

146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
    mon->vm = vm;
    mon->cb = cb;

    virLXCMonitorRef(mon);
    virNetClientSetCloseCallback(mon->client, virLXCMonitorEOFNotify, mon,
                                 virLXCMonitorCloseFreeCallback);

cleanup:
    VIR_FREE(sockpath);
    return mon;

no_memory:
    virReportOOMError();
error:
    virLXCMonitorFree(mon);
    mon = NULL;
    goto cleanup;
}


static void virLXCMonitorFree(virLXCMonitorPtr mon)
{
    VIR_DEBUG("mon=%p", mon);
    if (mon->client)
        virLXCMonitorClose(mon);

    if (mon->cb && mon->cb->destroy)
        (mon->cb->destroy)(mon, mon->vm);
    virMutexDestroy(&mon->lock);
175
    virObjectUnref(mon->program);
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
    VIR_FREE(mon);
}


int virLXCMonitorRef(virLXCMonitorPtr mon)
{
    mon->refs++;
    return mon->refs;
}

int virLXCMonitorUnref(virLXCMonitorPtr mon)
{
    mon->refs--;

    if (mon->refs == 0) {
        virLXCMonitorUnlock(mon);
        virLXCMonitorFree(mon);
        return 0;
    }

    return mon->refs;
}


void virLXCMonitorClose(virLXCMonitorPtr mon)
{
    if (mon->client) {
        virNetClientClose(mon->client);
204
        virObjectUnref(mon->client);
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
        mon->client = NULL;
    }
}


void virLXCMonitorLock(virLXCMonitorPtr mon)
{
    virMutexLock(&mon->lock);
}


void virLXCMonitorUnlock(virLXCMonitorPtr mon)
{
    virMutexUnlock(&mon->lock);
}