Michael Roth | 13a286d | 2011-07-19 15:41:53 -0500 | [diff] [blame] | 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 | */ |
Peter Maydell | 4459bf3 | 2016-01-29 17:49:58 +0000 | [diff] [blame] | 12 | #include "qemu/osdep.h" |
Michael S. Tsirkin | dc03272 | 2018-05-03 22:50:57 +0300 | [diff] [blame] | 13 | #include "guest-agent-core.h" |
Michael Roth | 13a286d | 2011-07-19 15:41:53 -0500 | [diff] [blame] | 14 | |
| 15 | struct GACommandState { |
| 16 | GSList *groups; |
| 17 | }; |
| 18 | |
| 19 | typedef struct GACommandGroup { |
| 20 | void (*init)(void); |
| 21 | void (*cleanup)(void); |
| 22 | } GACommandGroup; |
| 23 | |
| 24 | /* handle init/cleanup for stateful guest commands */ |
| 25 | |
| 26 | void ga_command_state_add(GACommandState *cs, |
| 27 | void (*init)(void), |
| 28 | void (*cleanup)(void)) |
| 29 | { |
Markus Armbruster | f3a0640 | 2015-09-14 13:50:44 +0200 | [diff] [blame] | 30 | GACommandGroup *cg = g_new0(GACommandGroup, 1); |
Michael Roth | 13a286d | 2011-07-19 15:41:53 -0500 | [diff] [blame] | 31 | cg->init = init; |
| 32 | cg->cleanup = cleanup; |
| 33 | cs->groups = g_slist_append(cs->groups, cg); |
| 34 | } |
| 35 | |
| 36 | static 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 | |
| 46 | void 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 | |
| 52 | static 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 | |
| 62 | void 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 | |
| 68 | GACommandState *ga_command_state_new(void) |
| 69 | { |
Markus Armbruster | f3a0640 | 2015-09-14 13:50:44 +0200 | [diff] [blame] | 70 | GACommandState *cs = g_new0(GACommandState, 1); |
Michael Roth | 13a286d | 2011-07-19 15:41:53 -0500 | [diff] [blame] | 71 | cs->groups = NULL; |
| 72 | return cs; |
| 73 | } |
Marc-André Lureau | 3e3e302 | 2016-07-15 17:52:52 +0200 | [diff] [blame] | 74 | |
| 75 | void ga_command_state_free(GACommandState *cs) |
| 76 | { |
| 77 | g_slist_free_full(cs->groups, g_free); |
| 78 | g_free(cs); |
| 79 | } |