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 "qemu/osdep.h"
13 #include <glib.h>
14 #include "qga/guest-agent-core.h"
15 
16 struct GACommandState {
17     GSList *groups;
18 };
19 
20 typedef struct GACommandGroup {
21     void (*init)(void);
22     void (*cleanup)(void);
23 } GACommandGroup;
24 
25 /* handle init/cleanup for stateful guest commands */
26 
27 void ga_command_state_add(GACommandState *cs,
28                           void (*init)(void),
29                           void (*cleanup)(void))
30 {
31     GACommandGroup *cg = g_new0(GACommandGroup, 1);
32     cg->init = init;
33     cg->cleanup = cleanup;
34     cs->groups = g_slist_append(cs->groups, cg);
35 }
36 
37 static void ga_command_group_init(gpointer opaque, gpointer unused)
38 {
39     GACommandGroup *cg = opaque;
40 
41     g_assert(cg);
42     if (cg->init) {
43         cg->init();
44     }
45 }
46 
47 void ga_command_state_init_all(GACommandState *cs)
48 {
49     g_assert(cs);
50     g_slist_foreach(cs->groups, ga_command_group_init, NULL);
51 }
52 
53 static void ga_command_group_cleanup(gpointer opaque, gpointer unused)
54 {
55     GACommandGroup *cg = opaque;
56 
57     g_assert(cg);
58     if (cg->cleanup) {
59         cg->cleanup();
60     }
61 }
62 
63 void ga_command_state_cleanup_all(GACommandState *cs)
64 {
65     g_assert(cs);
66     g_slist_foreach(cs->groups, ga_command_group_cleanup, NULL);
67 }
68 
69 GACommandState *ga_command_state_new(void)
70 {
71     GACommandState *cs = g_new0(GACommandState, 1);
72     cs->groups = NULL;
73     return cs;
74 }
75