guest-agent-command-state.c 1.7 KB

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