]> git.proxmox.com Git - qemu.git/blame - qga/guest-agent-command-state.c
Open 2.0 development tree
[qemu.git] / qga / guest-agent-command-state.c
CommitLineData
13a286d5
MR
1/*
2 * QEMU Guest Agent command state interfaces
3 *
4 * Copyright IBM Corp. 2011
5 *
6 * Authors:
7 * Michael Roth <mdroth@linux.vnet.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
11 */
12#include <glib.h>
13#include "qga/guest-agent-core.h"
14
15struct GACommandState {
16 GSList *groups;
17};
18
19typedef struct GACommandGroup {
20 void (*init)(void);
21 void (*cleanup)(void);
22} GACommandGroup;
23
24/* handle init/cleanup for stateful guest commands */
25
26void ga_command_state_add(GACommandState *cs,
27 void (*init)(void),
28 void (*cleanup)(void))
29{
7267c094 30 GACommandGroup *cg = g_malloc0(sizeof(GACommandGroup));
13a286d5
MR
31 cg->init = init;
32 cg->cleanup = cleanup;
33 cs->groups = g_slist_append(cs->groups, cg);
34}
35
36static void ga_command_group_init(gpointer opaque, gpointer unused)
37{
38 GACommandGroup *cg = opaque;
39
40 g_assert(cg);
41 if (cg->init) {
42 cg->init();
43 }
44}
45
46void ga_command_state_init_all(GACommandState *cs)
47{
48 g_assert(cs);
49 g_slist_foreach(cs->groups, ga_command_group_init, NULL);
50}
51
52static void ga_command_group_cleanup(gpointer opaque, gpointer unused)
53{
54 GACommandGroup *cg = opaque;
55
56 g_assert(cg);
57 if (cg->cleanup) {
58 cg->cleanup();
59 }
60}
61
62void ga_command_state_cleanup_all(GACommandState *cs)
63{
64 g_assert(cs);
65 g_slist_foreach(cs->groups, ga_command_group_cleanup, NULL);
66}
67
68GACommandState *ga_command_state_new(void)
69{
7267c094 70 GACommandState *cs = g_malloc0(sizeof(GACommandState));
13a286d5
MR
71 cs->groups = NULL;
72 return cs;
73}