libvirt-sandbox-context.c 14 KB
Newer Older
1 2 3
/*
 * libvirt-sandbox-context.c: libvirt sandbox context
 *
4
 * Copyright (C) 2011 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * 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, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19 20 21 22 23 24
 *
 * Author: Daniel P. Berrange <berrange@redhat.com>
 */

#include <config.h>

25
#include <string.h>
26
#include <errno.h>
27

28 29
#include <glib/gi18n.h>

30 31
#include "libvirt-sandbox/libvirt-sandbox.h"

32 33 34 35 36 37 38 39 40 41 42 43 44
/**
 * SECTION: libvirt-sandbox-context
 * @short_description: Application sandbox context
 * @include: libvirt-sandbox/libvirt-sandbox.h
 *
 * Provides a base class for implementing console based application sandboxes
 *
 * The GVirSandboxContext object takes a #GVirSandboxConfig instance, passing it
 * to #GVirSandboxBuilder instance to create a virtual machine, and then provides
 * access to a #GVirSandboxConsole instance for interacting with the sandboxed
 * application's stdio.
 */

45 46
#define GVIR_SANDBOX_CONTEXT_GET_PRIVATE(obj)                           \
    (G_TYPE_INSTANCE_GET_PRIVATE((obj), GVIR_SANDBOX_TYPE_CONTEXT, GVirSandboxContextPrivate))
47 48 49 50 51 52 53 54

struct _GVirSandboxContextPrivate
{
    GVirConnection *connection;
    GVirDomain *domain;
    GVirSandboxConfig *config;
};

55
G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE(GVirSandboxContext, gvir_sandbox_context, G_TYPE_OBJECT);
56 57 58 59 60 61 62 63 64 65 66 67 68 69


enum {
    PROP_0,

    PROP_CONFIG,
    PROP_DOMAIN,
    PROP_CONNECTION,
};

enum {
    LAST_SIGNAL
};

70 71 72 73 74 75
static gboolean gvir_sandbox_context_start_default(GVirSandboxContext *ctxt, GError **error);
static gboolean gvir_sandbox_context_stop_default(GVirSandboxContext *ctxt, GError **error);
static gboolean gvir_sandbox_context_attach_default(GVirSandboxContext *ctxt, GError **error);
static gboolean gvir_sandbox_context_detach_default(GVirSandboxContext *ctxt, GError **error);


76 77 78 79 80 81 82 83 84 85 86
//static gint signals[LAST_SIGNAL];

#define GVIR_SANDBOX_CONTEXT_ERROR gvir_sandbox_context_error_quark()

static GQuark
gvir_sandbox_context_error_quark(void)
{
    return g_quark_from_static_string("gvir-sandbox-context");
}

static void gvir_sandbox_context_get_property(GObject *object,
87 88 89
                                              guint prop_id,
                                              GValue *value,
                                              GParamSpec *pspec)
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
{
    GVirSandboxContext *ctxt = GVIR_SANDBOX_CONTEXT(object);
    GVirSandboxContextPrivate *priv = ctxt->priv;

    switch (prop_id) {
    case PROP_CONFIG:
        g_value_set_object(value, priv->config);
        break;

    case PROP_DOMAIN:
        g_value_set_object(value, priv->domain);
        break;

    case PROP_CONNECTION:
        g_value_set_object(value, priv->connection);
        break;

    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
    }
}


static void gvir_sandbox_context_set_property(GObject *object,
114 115 116
                                              guint prop_id,
                                              const GValue *value,
                                              GParamSpec *pspec)
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
{
    GVirSandboxContext *ctxt = GVIR_SANDBOX_CONTEXT(object);
    GVirSandboxContextPrivate *priv = ctxt->priv;

    switch (prop_id) {
    case PROP_CONFIG:
        if (priv->config)
            g_object_unref(priv->config);
        priv->config = g_value_dup_object(value);
        break;

    case PROP_DOMAIN:
        if (priv->domain)
            g_object_unref(priv->domain);
        priv->domain = g_value_dup_object(value);
        break;

    case PROP_CONNECTION:
        if (priv->connection)
            g_object_unref(priv->connection);
        priv->connection = g_value_dup_object(value);
        break;

    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
    }
}


static void gvir_sandbox_context_finalize(GObject *object)
{
    GVirSandboxContext *ctxt = GVIR_SANDBOX_CONTEXT(object);
    GVirSandboxContextPrivate *priv = ctxt->priv;

151
    if (priv->domain)
152 153
        gvir_sandbox_context_stop(ctxt, NULL);

154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
    if (priv->connection)
        g_object_unref(priv->connection);
    if (priv->config)
        g_object_unref(priv->config);

    G_OBJECT_CLASS(gvir_sandbox_context_parent_class)->finalize(object);
}


static void gvir_sandbox_context_class_init(GVirSandboxContextClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS(klass);

    object_class->finalize = gvir_sandbox_context_finalize;
    object_class->get_property = gvir_sandbox_context_get_property;
    object_class->set_property = gvir_sandbox_context_set_property;

171 172 173 174 175
    klass->start = gvir_sandbox_context_start_default;
    klass->stop = gvir_sandbox_context_stop_default;
    klass->attach = gvir_sandbox_context_attach_default;
    klass->detach = gvir_sandbox_context_detach_default;

176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
    g_object_class_install_property(object_class,
                                    PROP_CONFIG,
                                    g_param_spec_object("config",
                                                        "Config",
                                                        "The sandbox configuration",
                                                        GVIR_SANDBOX_TYPE_CONFIG,
                                                        G_PARAM_READABLE |
                                                        G_PARAM_WRITABLE |
                                                        G_PARAM_CONSTRUCT_ONLY |
                                                        G_PARAM_STATIC_NAME |
                                                        G_PARAM_STATIC_NICK |
                                                        G_PARAM_STATIC_BLURB));
    g_object_class_install_property(object_class,
                                    PROP_DOMAIN,
                                    g_param_spec_object("domain",
                                                        "Domain",
                                                        "The sandbox domain",
                                                        GVIR_TYPE_DOMAIN,
                                                        G_PARAM_READABLE |
195
                                                        G_PARAM_WRITABLE |
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
                                                        G_PARAM_STATIC_NAME |
                                                        G_PARAM_STATIC_NICK |
                                                        G_PARAM_STATIC_BLURB));
    g_object_class_install_property(object_class,
                                    PROP_CONNECTION,
                                    g_param_spec_object("connection",
                                                        "Connection",
                                                        "The sandbox connection",
                                                        GVIR_TYPE_CONNECTION,
                                                        G_PARAM_READABLE |
                                                        G_PARAM_WRITABLE |
                                                        G_PARAM_CONSTRUCT_ONLY |
                                                        G_PARAM_STATIC_NAME |
                                                        G_PARAM_STATIC_NICK |
                                                        G_PARAM_STATIC_BLURB));
}


static void gvir_sandbox_context_init(GVirSandboxContext *ctxt)
{
216
    ctxt->priv = GVIR_SANDBOX_CONTEXT_GET_PRIVATE(ctxt);
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
}


/**
 * gvir_sandbox_context_get_config:
 * @ctxt: (transfer none): the sandbox context
 *
 * Retrieves the sandbox configuration
 *
 * Returns: (transfer full): the current configuration
 */
GVirSandboxConfig *gvir_sandbox_context_get_config(GVirSandboxContext *ctxt)
{
    GVirSandboxContextPrivate *priv = ctxt->priv;
    g_object_ref(priv->config);
    return priv->config;
}


/**
 * gvir_sandbox_context_get_domain:
 * @ctxt: (transfer none): the sandbox context
 *
 * Retrieves the sandbox domain (if running)
 *
 * Returns: (transfer full): the current domain or NULL
 */
244 245
GVirDomain *gvir_sandbox_context_get_domain(GVirSandboxContext *ctxt,
                                            GError **error)
246 247
{
    GVirSandboxContextPrivate *priv = ctxt->priv;
248 249 250

    if (!priv->domain) {
        g_set_error(error, GVIR_SANDBOX_CONTEXT_ERROR, 0,
251
                    _("Domain is not currently running"));
252 253 254 255
        return NULL;
    }

    return g_object_ref(priv->domain);
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
}

/**
 * gvir_sandbox_context_get_connection:
 * @ctxt: (transfer none): the sandbox context
 *
 * Retrieves the sandbox connection
 *
 * Returns: (transfer full): the current connection or NULL
 */
GVirConnection *gvir_sandbox_context_get_connection(GVirSandboxContext *ctxt)
{
    GVirSandboxContextPrivate *priv = ctxt->priv;
    if (priv->connection)
        g_object_ref(priv->connection);
    return priv->connection;
}


275
static gboolean gvir_sandbox_context_start_default(GVirSandboxContext *ctxt, GError **error)
276 277 278 279 280 281 282 283 284
{
    GVirSandboxContextPrivate *priv = ctxt->priv;

    if (priv->domain) {
        *error = g_error_new(GVIR_SANDBOX_CONTEXT_ERROR, 0,
                             "%s", "A previously built sandbox still exists");
        return FALSE;
    }

285
    return TRUE;
286 287 288
}


289
static gboolean gvir_sandbox_context_attach_default(GVirSandboxContext *ctxt, GError **error)
290 291 292
{
    GVirSandboxContextPrivate *priv = ctxt->priv;

293
    if (priv->domain) {
294 295 296 297 298 299 300 301 302
        *error = g_error_new(GVIR_SANDBOX_CONTEXT_ERROR, 0,
                             "%s", "A previously built sandbox still exists");
        return FALSE;
    }

    if (!gvir_connection_fetch_domains(priv->connection, NULL, error))
        return FALSE;

    if (!(priv->domain = gvir_connection_find_domain_by_name(
303 304
                                                             priv->connection,
                                                             gvir_sandbox_config_get_name(priv->config)))) {
305 306 307 308 309 310
        *error = g_error_new(GVIR_SANDBOX_CONTEXT_ERROR, 0,
                             "Sandbox %s does not exist",
                             gvir_sandbox_config_get_name(priv->config));
        return FALSE;
    }

311 312 313 314
    return TRUE;
}


315
static gboolean gvir_sandbox_context_detach_default(GVirSandboxContext *ctxt, GError **error)
316 317 318
{
    GVirSandboxContextPrivate *priv = ctxt->priv;

319 320 321 322 323
    if (!priv->domain) {
        g_set_error(error, GVIR_SANDBOX_CONTEXT_ERROR, 0,
                    _("Sandbox is not currently running"));
        return FALSE;
    }
324

325 326
    g_object_unref(priv->domain);
    priv->domain = NULL;
327

328 329 330 331
    return TRUE;
}


332
static gboolean gvir_sandbox_context_stop_default(GVirSandboxContext *ctxt, GError **error)
333 334 335
{
    GVirSandboxContextPrivate *priv = ctxt->priv;

336 337 338 339 340
    if (!priv->domain) {
        g_set_error(error, GVIR_SANDBOX_CONTEXT_ERROR, 0,
                    _("Sandbox is not currently running"));
        return FALSE;
    }
341

342
    return TRUE;
343
}
344 345 346


/**
347
 * gvir_sandbox_context_get_log_console:
348 349 350 351
 * @ctxt: (transfer none): the sandbox context
 *
 * Returns: (transfer full)(allow-none): the sandbox console (or NULL)
 */
352 353
GVirSandboxConsole *gvir_sandbox_context_get_log_console(GVirSandboxContext *ctxt,
                                                         GError **error)
354 355
{
    GVirSandboxContextPrivate *priv = ctxt->priv;
356 357
    GVirSandboxConsole *console;
    const char *devname = NULL;
358

359
    if (!priv->domain) {
360
        g_set_error(error, GVIR_SANDBOX_CONTEXT_ERROR, 0,
361
                    _("Sandbox is not currently running"));
362
        return NULL;
363
    }
364

365 366 367 368 369 370 371 372 373 374 375
    /* XXX get from config */
    if (strstr(gvir_connection_get_uri(priv->connection), "lxc"))
        devname = "console0";
    else
        devname = "serial0";

    console = GVIR_SANDBOX_CONSOLE(gvir_sandbox_console_raw_new(priv->connection,
                                                                priv->domain,
                                                                devname));

    return console;
376
}
377 378 379 380 381 382 383 384


/**
 * gvir_sandbox_context_get_shell_console:
 * @ctxt: (transfer none): the sandbox context
 *
 * Returns: (transfer full)(allow-none): the sandbox console (or NULL)
 */
385 386
GVirSandboxConsole *gvir_sandbox_context_get_shell_console(GVirSandboxContext *ctxt,
                                                           GError **error)
387 388
{
    GVirSandboxContextPrivate *priv = ctxt->priv;
389 390
    GVirSandboxConsole *console;
    const char *devname = NULL;
391

392 393
    if (!priv->domain) {
        g_set_error(error, GVIR_SANDBOX_CONTEXT_ERROR, 0,
394
                    _("Domain is not currently running"));
395
        return NULL;
396 397 398 399
    }

    /* XXX get from config */
    if (strstr(gvir_connection_get_uri(priv->connection), "lxc"))
400
        devname = "console1";
401
    else
402
        devname = "serial1";
403

404 405 406
    console = GVIR_SANDBOX_CONSOLE(gvir_sandbox_console_raw_new(priv->connection, priv->domain,
                                                                devname));
    return console;
407
}
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430

gboolean gvir_sandbox_context_start(GVirSandboxContext *ctxt, GError **error)
{
    return GVIR_SANDBOX_CONTEXT_GET_CLASS(ctxt)->start(ctxt, error);
}


gboolean gvir_sandbox_context_stop(GVirSandboxContext *ctxt, GError **error)
{
    return GVIR_SANDBOX_CONTEXT_GET_CLASS(ctxt)->stop(ctxt, error);
}


gboolean gvir_sandbox_context_attach(GVirSandboxContext *ctxt, GError **error)
{
    return GVIR_SANDBOX_CONTEXT_GET_CLASS(ctxt)->attach(ctxt, error);
}


gboolean gvir_sandbox_context_detach(GVirSandboxContext *ctxt, GError **error)
{
    return GVIR_SANDBOX_CONTEXT_GET_CLASS(ctxt)->detach(ctxt, error);
}
431 432 433 434 435 436

gboolean gvir_sandbox_context_is_attached(GVirSandboxContext *ctxt)
{
    GVirSandboxContextPrivate *priv = ctxt->priv;
    return priv->domain != NULL;
}
437 438 439 440 441 442 443 444 445

/*
 * Local variables:
 *  c-indent-level: 4
 *  c-basic-offset: 4
 *  indent-tabs-mode: nil
 *  tab-width: 8
 * End:
 */