libvirt-sandbox-cleaner.c 6.56 KB
Newer Older
1 2 3
/*
 * libvirt-sandbox-cleaner.c: libvirt sandbox cleaner
 *
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 25 26
 *
 * Author: Daniel P. Berrange <berrange@redhat.com>
 */

#include <config.h>

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

27 28 29 30 31 32 33 34 35 36 37 38 39
/**
 * SECTION: libvirt-sandbox-cleaner
 * @short_description: Sandbox context cleanup tasks
 * @include: libvirt-sandbox/libvirt-sandbox.h
 *
 * Provides an object for managing cleanup tasks associated with a sandbox
 *
 * The GVirSandboxCleaner object provides a framework for registering
 * cleanup tasks to be performed at various stages of a sandbox's
 * lifecycle. This is typically used to delete temporary files and
 * other similar state.
 */

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 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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 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
#define GVIR_SANDBOX_CLEANER_GET_PRIVATE(obj)                         \
        (G_TYPE_INSTANCE_GET_PRIVATE((obj), GVIR_SANDBOX_TYPE_CLEANER, GVirSandboxCleanerPrivate))

struct _GVirSandboxCleanerPrivate
{
    GList *postStart;
    GList *postStop;
};

typedef struct _GVirSandboxCleanerEntry GVirSandboxCleanerEntry;
struct _GVirSandboxCleanerEntry
{
    GVirSandboxCleanerFunc func;
    gpointer opaque;
    GDestroyNotify ff;
};


G_DEFINE_TYPE(GVirSandboxCleaner, gvir_sandbox_cleaner, G_TYPE_OBJECT);


enum {
    PROP_0,

};

enum {
    LAST_SIGNAL
};

//static gint signals[LAST_SIGNAL];


static void gvir_sandbox_cleaner_get_property(GObject *object,
                                              guint prop_id,
                                              GValue *value G_GNUC_UNUSED,
                                              GParamSpec *pspec)
{
#if 0
    GVirSandboxCleaner *cleaner = GVIR_SANDBOX_CLEANER(object);
    GVirSandboxCleanerPrivate *priv = cleaner->priv;
#endif

    switch (prop_id) {

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


static void gvir_sandbox_cleaner_set_property(GObject *object,
                                             guint prop_id,
                                             const GValue *value G_GNUC_UNUSED,
                                             GParamSpec *pspec)
{
#if 0
    GVirSandboxCleaner *cleaner = GVIR_SANDBOX_CLEANER(object);
    GVirSandboxCleanerPrivate *priv = cleaner->priv;
#endif

    switch (prop_id) {

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


static void gvir_sandbox_cleaner_finalize(GObject *object)
{
    GVirSandboxCleaner *cleaner = GVIR_SANDBOX_CLEANER(object);
    GVirSandboxCleanerPrivate *priv = cleaner->priv;
    GList *tmp;

    tmp = priv->postStart;
    while (tmp) {
        GVirSandboxCleanerEntry *ent = tmp->data;
        ent->ff(ent->opaque);
        g_free(ent);
        tmp = tmp->next;
    }
    g_list_free(priv->postStart);

    tmp = priv->postStop;
    while (tmp) {
        GVirSandboxCleanerEntry *ent = tmp->data;
        ent->ff(ent->opaque);
        g_free(ent);
        tmp = tmp->next;
    }
    g_list_free(priv->postStop);

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


static void gvir_sandbox_cleaner_class_init(GVirSandboxCleanerClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS(klass);

    object_class->finalize = gvir_sandbox_cleaner_finalize;
    object_class->get_property = gvir_sandbox_cleaner_get_property;
    object_class->set_property = gvir_sandbox_cleaner_set_property;

    g_type_class_add_private(klass, sizeof(GVirSandboxCleanerPrivate));
}


static void gvir_sandbox_cleaner_init(GVirSandboxCleaner *cleaner)
{
    cleaner->priv = GVIR_SANDBOX_CLEANER_GET_PRIVATE(cleaner);
}


/**
 * gvir_sandbox_cleaner_new:
 *
 * Create a new sandbox cleaner
 *
 * Returns: (transfer full): a new sandbox cleaner object
 */
GVirSandboxCleaner *gvir_sandbox_cleaner_new(void)
{
    return GVIR_SANDBOX_CLEANER(g_object_new(GVIR_SANDBOX_TYPE_CLEANER,
                                             NULL));
}


void gvir_sandbox_cleaner_add_action_post_start(GVirSandboxCleaner *cleaner,
                                                GVirSandboxCleanerFunc func,
                                                gpointer opaque,
                                                GDestroyNotify ff)
{
    GVirSandboxCleanerPrivate *priv = cleaner->priv;
    GVirSandboxCleanerEntry *ent = g_new0(GVirSandboxCleanerEntry, 1);

    ent->func = func;
    ent->opaque = opaque;
    ent->ff = ff;

    priv->postStart = g_list_append(priv->postStart, ent);
}


void gvir_sandbox_cleaner_add_action_post_stop(GVirSandboxCleaner *cleaner,
                                               GVirSandboxCleanerFunc func,
                                               gpointer opaque,
                                               GDestroyNotify ff)
{
    GVirSandboxCleanerPrivate *priv = cleaner->priv;
    GVirSandboxCleanerEntry *ent = g_new0(GVirSandboxCleanerEntry, 1);

    ent->func = func;
    ent->opaque = opaque;
    ent->ff = ff;

    priv->postStop = g_list_append(priv->postStop, ent);
}


gboolean gvir_sandbox_cleaner_run_post_start(GVirSandboxCleaner *cleaner, GError **error)
{
    GVirSandboxCleanerPrivate *priv = cleaner->priv;
204 205
    GList *actions = g_list_copy(priv->postStart);
    GList *tmp;
206 207
    gboolean ret = TRUE;

208 209
    actions = g_list_reverse(actions);
    tmp = actions;
210 211 212 213 214 215 216 217
    while (tmp) {
        GVirSandboxCleanerEntry *ent = tmp->data;

        if (!ent->func(cleaner, error, ent->opaque))
            ret = FALSE;

        tmp = tmp->next;
    }
218
    g_list_free(actions);
219 220 221 222 223 224 225 226

    return ret;
}


gboolean gvir_sandbox_cleaner_run_post_stop(GVirSandboxCleaner *cleaner, GError **error)
{
    GVirSandboxCleanerPrivate *priv = cleaner->priv;
227 228
    GList *actions = g_list_copy(priv->postStop);
    GList *tmp;
229 230
    gboolean ret = TRUE;

231 232
    actions = g_list_reverse(actions);
    tmp = actions;
233 234 235 236 237 238 239 240
    while (tmp) {
        GVirSandboxCleanerEntry *ent = tmp->data;

        if (!ent->func(cleaner, error, ent->opaque))
            ret = FALSE;

        tmp = tmp->next;
    }
241
    g_list_free(actions);
242 243 244

    return ret;
}