Skip to contentSkip to conversation

Add interface to traverse the qmp command list by QmpCommand

PATCH Superseded Mark Wu 3 participants 5 messages Sep 26, 2013 – Sep 27, 2013

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Mark Wu

author
#0 lore raw

PATCH 1/1 Add interface to traverse the qmp command list by QmpCommand

In the original code, qmp_get_command_list is used to construct a list of all commands' name. To get the information of all qga commands, it traverses the name list and search the command info with its name. So it can cause O(n^2) in the number of commands.

This patch adds an interface to traverse the qmp command list by QmpCommand to replace qmp_get_command_list. It can decrease the complexity from O(n) to O(n^2)

---
 include/qapi/qmp/dispatch.h |  3 +-
 qapi/qmp-registry.c         | 28 ++-----------------
 qga/commands.c              | 39 ++++++++++----------------
 qga/main.c                  | 68 +++++++++++++++++----------------------------
 4 files changed, 45 insertions(+), 93 deletions(-)
include/qapi/qmp/dispatch.h +2 -1
index 1ce11f5..fb174f6 100644
@@ -48,8 +48,9 @@ QObject *qmp_dispatch(QObject *request);
 void qmp_disable_command(const char *name);
 void qmp_enable_command(const char *name);
 bool qmp_command_is_enabled(const char *name);
-char **qmp_get_command_list(void);
 QObject *qmp_build_error_object(Error *errp);
+typedef void (*qmp_cmd_callback_fn)(QmpCommand *cmd, void *opaque);
+void qmp_for_each_command(qmp_cmd_callback_fn fn, void *opaque);
 
 #endif
 
qapi/qmp-registry.c +2 -26
index 28bbbe8..844d597 100644
@@ -66,35 +66,11 @@ void qmp_enable_command(const char *name)
     qmp_toggle_command(name, true);
 }
 
-bool qmp_command_is_enabled(const char *name)
+void qmp_for_each_command(qmp_cmd_callback_fn fn, void *opaque)
 {
     QmpCommand *cmd;
 
     QTAILQ_FOREACH(cmd, &qmp_commands, node) {
-        if (strcmp(cmd->name, name) == 0) {
-            return cmd->enabled;
-        }
+        fn(cmd, opaque);
     }
-
-    return false;
-}
-
-char **qmp_get_command_list(void)
-{
-    QmpCommand *cmd;
-    int count = 1;
-    char **list_head, **list;
-
-    QTAILQ_FOREACH(cmd, &qmp_commands, node) {
-        count++;
-    }
-
-    list_head = list = g_malloc0(count * sizeof(char *));
-
-    QTAILQ_FOREACH(cmd, &qmp_commands, node) {
-        *list = g_strdup(cmd->name);
-        list++;
-    }
-
-    return list_head;
 }
qga/commands.c +15 -24
index 528b082..602cd47 100644
@@ -45,35 +45,26 @@ void qmp_guest_ping(Error **err)
     slog("guest-ping called");
 }
 
-struct GuestAgentInfo *qmp_guest_info(Error **err)
+static void qmp_command_info(QmpCommand *cmd, void *opaque)
 {
-    GuestAgentInfo *info = g_malloc0(sizeof(GuestAgentInfo));
+    GuestAgentInfo *info = (GuestAgentInfo *)opaque;
     GuestAgentCommandInfo *cmd_info;
     GuestAgentCommandInfoList *cmd_info_list;
-    char **cmd_list_head, **cmd_list;
-
-    info->version = g_strdup(QEMU_VERSION);
-
-    cmd_list_head = cmd_list = qmp_get_command_list();
-    if (*cmd_list_head == NULL) {
-        goto out;
-    }
-
-    while (*cmd_list) {
-        cmd_info = g_malloc0(sizeof(GuestAgentCommandInfo));
-        cmd_info->name = g_strdup(*cmd_list);
-        cmd_info->enabled = qmp_command_is_enabled(cmd_info->name);
 
-        cmd_info_list = g_malloc0(sizeof(GuestAgentCommandInfoList));
-        cmd_info_list->value = cmd_info;
-        cmd_info_list->next = info->supported_commands;
-        info->supported_commands = cmd_info_list;
+    cmd_info = g_malloc0(sizeof(GuestAgentCommandInfo));
+    cmd_info->name = g_strdup(cmd->name);
+    cmd_info->enabled = cmd->enabled;
+    cmd_info_list = g_malloc0(sizeof(GuestAgentCommandInfoList));
+    cmd_info_list->value = cmd_info;
+    cmd_info_list->next = info->supported_commands;
+    info->supported_commands = cmd_info_list;
+}
 
-        g_free(*cmd_list);
-        cmd_list++;
-    }
+struct GuestAgentInfo *qmp_guest_info(Error **err)
+{
+    GuestAgentInfo *info = g_malloc0(sizeof(GuestAgentInfo));
 
-out:
-    g_free(cmd_list_head);
+    info->version = g_strdup(QEMU_VERSION);
+    qmp_for_each_command(qmp_command_info, info);
     return info;
 }
qga/main.c +26 -42

116 lines not on this page · raw message

Signature · 1 line
1.8.3.1
author
#1 lore raw

It's verified by the following tests:

1. run ./qemu-ga -b help,
it print a list of qga command names as expected
2. run ./guest-sync -b guest-ping, guest-sync .... command 'guest-info' show that guest-ping and guest-sync are disabled

Quoted 255 lines · Mark Wu

In the original code, qmp_get_command_list is used to construct a list of all commands' name. To get the information of all qga commands, it traverses the name list and search the command info with its name. So it can cause O(n^2) in the number of commands.

This patch adds an interface to traverse the qmp command list by QmpCommand to replace qmp_get_command_list. It can decrease the complexity from O(n) to O(n^2)

---
  include/qapi/qmp/dispatch.h |  3 +-
  qapi/qmp-registry.c         | 28 ++-----------------
  qga/commands.c              | 39 ++++++++++----------------
  qga/main.c                  | 68 +++++++++++++++++----------------------------
  4 files changed, 45 insertions(+), 93 deletions(-)
include/qapi/qmp/dispatch.h +2 -1
index 1ce11f5..fb174f6 100644
@@ -48,8 +48,9 @@ QObject *qmp_dispatch(QObject *request);
  void qmp_disable_command(const char *name);
  void qmp_enable_command(const char *name);
  bool qmp_command_is_enabled(const char *name);
-char **qmp_get_command_list(void);
  QObject *qmp_build_error_object(Error *errp);
+typedef void (*qmp_cmd_callback_fn)(QmpCommand *cmd, void *opaque);
+void qmp_for_each_command(qmp_cmd_callback_fn fn, void *opaque);
 
  #endif
 
qapi/qmp-registry.c +2 -26
index 28bbbe8..844d597 100644
@@ -66,35 +66,11 @@ void qmp_enable_command(const char *name)
      qmp_toggle_command(name, true);
  }
 
-bool qmp_command_is_enabled(const char *name)
+void qmp_for_each_command(qmp_cmd_callback_fn fn, void *opaque)
  {
      QmpCommand *cmd;
 
      QTAILQ_FOREACH(cmd, &qmp_commands, node) {
-        if (strcmp(cmd->name, name) == 0) {
-            return cmd->enabled;
-        }
+        fn(cmd, opaque);
      }
-
-    return false;
-}
-
-char **qmp_get_command_list(void)
-{
-    QmpCommand *cmd;
-    int count = 1;
-    char **list_head, **list;
-
-    QTAILQ_FOREACH(cmd, &qmp_commands, node) {
-        count++;
-    }
-
-    list_head = list = g_malloc0(count * sizeof(char *));
-
-    QTAILQ_FOREACH(cmd, &qmp_commands, node) {
-        *list = g_strdup(cmd->name);
-        list++;
-    }
-
-    return list_head;
  }
qga/commands.c +15 -24
index 528b082..602cd47 100644
@@ -45,35 +45,26 @@ void qmp_guest_ping(Error **err)
      slog("guest-ping called");
  }
 
-struct GuestAgentInfo *qmp_guest_info(Error **err)
+static void qmp_command_info(QmpCommand *cmd, void *opaque)
  {
-    GuestAgentInfo *info = g_malloc0(sizeof(GuestAgentInfo));
+    GuestAgentInfo *info = (GuestAgentInfo *)opaque;
      GuestAgentCommandInfo *cmd_info;
      GuestAgentCommandInfoList *cmd_info_list;
-    char **cmd_list_head, **cmd_list;
-
-    info->version = g_strdup(QEMU_VERSION);
-
-    cmd_list_head = cmd_list = qmp_get_command_list();
-    if (*cmd_list_head == NULL) {
-        goto out;
-    }
-
-    while (*cmd_list) {
-        cmd_info = g_malloc0(sizeof(GuestAgentCommandInfo));
-        cmd_info->name = g_strdup(*cmd_list);
-        cmd_info->enabled = qmp_command_is_enabled(cmd_info->name);
 
-        cmd_info_list = g_malloc0(sizeof(GuestAgentCommandInfoList));
-        cmd_info_list->value = cmd_info;
-        cmd_info_list->next = info->supported_commands;
-        info->supported_commands = cmd_info_list;
+    cmd_info = g_malloc0(sizeof(GuestAgentCommandInfo));
+    cmd_info->name = g_strdup(cmd->name);
+    cmd_info->enabled = cmd->enabled;
+    cmd_info_list = g_malloc0(sizeof(GuestAgentCommandInfoList));
+    cmd_info_list->value = cmd_info;
+    cmd_info_list->next = info->supported_commands;
+    info->supported_commands = cmd_info_list;
+}
 
-        g_free(*cmd_list);
-        cmd_list++;
-    }
+struct GuestAgentInfo *qmp_guest_info(Error **err)
+{
+    GuestAgentInfo *info = g_malloc0(sizeof(GuestAgentInfo));
 
-out:
-    g_free(cmd_list_head);
+    info->version = g_strdup(QEMU_VERSION);
+    qmp_for_each_command(qmp_command_info, info);
      return info;
  }
qga/main.c +26 -42
index 6c746c8..1741d3f 100644
@@ -347,48 +347,34 @@ static gint ga_strcmp(gconstpointer str1, gconstpointer str2)
  }
 
  /* disable commands that aren't safe for fsfreeze */
-static void ga_disable_non_whitelisted(void)
+static void ga_disable_non_whitelisted(QmpCommand *cmd, void *opaque)
  {
-    char **list_head, **list;
      bool whitelisted;
      int i;
 
-    list_head = list = qmp_get_command_list();
-    while (*list != NULL) {
-        whitelisted = false;
-        i = 0;
-        while (ga_freeze_whitelist[i] != NULL) {
-            if (strcmp(*list, ga_freeze_whitelist[i]) == 0) {
-                whitelisted = true;
-            }
-            i++;
-        }
-        if (!whitelisted) {
-            g_debug("disabling command: %s", *list);
-            qmp_disable_command(*list);
+    whitelisted = false;
+    i = 0;
+    while (ga_freeze_whitelist[i] != NULL) {
+        if (strcmp(cmd->name, ga_freeze_whitelist[i]) == 0) {
+            whitelisted = true;
          }
-        g_free(*list);
-        list++;
+        i++;
+    }
+    if (!whitelisted) {
+        g_debug("disabling command: %s", cmd->name);
+        qmp_disable_command(cmd->name);
      }
-    g_free(list_head);
  }
 
  /* [re-]enable all commands, except those explicitly blacklisted by user */
-static void ga_enable_non_blacklisted(GList *blacklist)
+static void ga_enable_non_blacklisted(QmpCommand *cmd, void *opaque)
  {
-    char **list_head, **list;
-
-    list_head = list = qmp_get_command_list();
-    while (*list != NULL) {
-        if (g_list_find_custom(blacklist, *list, ga_strcmp) == NULL &&
-            !qmp_command_is_enabled(*list)) {
-            g_debug("enabling command: %s", *list);
-            qmp_enable_command(*list);
-        }
-        g_free(*list);
-        list++;
+    GList *blacklist = (GList *)opaque;
+    if (g_list_find_custom(blacklist, cmd->name, ga_strcmp) == NULL &&
+        !cmd->enabled) {
+        g_debug("enabling command: %s", cmd->name);
+        qmp_enable_command(cmd->name);
      }
-    g_free(list_head);
  }
 
  static bool ga_create_file(const char *path)
@@ -424,7 +410,7 @@ void ga_set_frozen(GAState *s)
          return;
      }
      /* disable all non-whitelisted (for frozen state) commands */
-    ga_disable_non_whitelisted();
+    qmp_for_each_command(ga_disable_non_whitelisted, NULL);
      g_warning("disabling logging due to filesystem freeze");
      ga_disable_logging(s);
      s->frozen = true;
@@ -460,7 +446,7 @@ void ga_unset_frozen(GAState *s)
      }
 
      /* enable all disabled, non-blacklisted commands */
-    ga_enable_non_blacklisted(s->blacklist);
+    qmp_for_each_command(ga_enable_non_blacklisted, s->blacklist);
      s->frozen = false;
      if (!ga_delete_file(s->state_filepath_isfrozen)) {
          g_warning("unable to delete %s, fsfreeze may not function properly",
@@ -920,6 +906,11 @@ int64_t ga_get_fd_handle(GAState *s, Error **errp)
      return handle;
  }
 
+static void ga_print_cmd(QmpCommand *cmd, void *opaque)
+{
+    printf("%s\n", cmd->name);
+}
+
  int main(int argc, char **argv)
  {
      const char *sopt = "hVvdm:p:l:f:F::b:s:t:";
@@ -996,15 +987,8 @@ int main(int argc, char **argv)
              daemonize = 1;
              break;
          case 'b': {
-            char **list_head, **list;
              if (is_help_option(optarg)) {
-                list_head = list = qmp_get_command_list();
-                while (*list != NULL) {
-                    printf("%s\n", *list);
-                    g_free(*list);
-                    list++;
-                }
-                g_free(list_head);
+                qmp_for_each_command(ga_print_cmd, NULL);
                  return 0;
              }
              for (j = 0, i = 0, len = strlen(optarg); i < len; i++) {
@@ -1126,7 +1110,7 @@ int main(int argc, char **argv)
              s->deferred_options.log_filepath = log_filepath;
          }
          ga_disable_logging(s);
-        ga_disable_non_whitelisted();
+        qmp_for_each_command(ga_disable_non_whitelisted, NULL);
      } else {
          if (daemonize) {
              become_daemon(pid_filepath);
Reviewed-by on v1 1/1
#2 lore raw
In reply to Mark Wu
Quoted 8 lines · Mark Wu

In the original code, qmp_get_command_list is used to construct a list of all commands' name. To get the information of all qga commands, it traverses the name list and search the command info with its name. So it can cause O(n^2) in the number of commands.

This patch adds an interface to traverse the qmp command list by QmpCommand to replace qmp_get_command_list. It can decrease the complexity from O(n) to O(n^2)

from O(n^2) to O(n)

Quoted 11 lines
---
 include/qapi/qmp/dispatch.h |  3 +-
 qapi/qmp-registry.c         | 28 ++-----------------
 qga/commands.c              | 39 ++++++++++----------------
 qga/main.c                  | 68 +++++++++++++++++----------------------------
 4 files changed, 45 insertions(+), 93 deletions(-)
Quoted 1 line

-bool qmp_command_is_enabled(const char *name)

I think one of Michael's suggestions was that you may still want qmp_command_is_enabled, but with a new signature:

bool qmp_command_is_enabled(const QmpCommand *cmd)

{
    return cmd->enabled;
}

qga/commands.c void qmp_guest_ping(Error **err) · line 50

Quoted 5 lines
qga/commands.c +2 -2
-struct GuestAgentInfo *qmp_guest_info(Error **err)
+static void qmp_command_info(QmpCommand *cmd, void *opaque)
  {
-    GuestAgentInfo *info = g_malloc0(sizeof(GuestAgentInfo));
+    GuestAgentInfo *info = (GuestAgentInfo *)opaque; 

This is C, not C++. The cast is not necessary.

qga/commands.c void qmp_guest_ping(Error **err) · line 56

Quoted 9 lines
qga/commands.c +3 -5
-        cmd_info->enabled = qmp_command_is_enabled(cmd_info->name);
 
-        cmd_info_list = g_malloc0(sizeof(GuestAgentCommandInfoList));
-        cmd_info_list->value = cmd_info;
-        cmd_info_list->next = info->supported_commands;
-        info->supported_commands = cmd_info_list;
+    cmd_info = g_malloc0(sizeof(GuestAgentCommandInfo));
+    cmd_info->name = g_strdup(cmd->name);
+    cmd_info->enabled = cmd->enabled;

I guess it all depends on whether we want QmpCommand to be an opaque type outside of a single file. But I don't have a strong argument for making it opaque, and your approach works if we don't mind exposing the details of QmpCommand across multiple files. So I can live with your patch as-is.

qga/main.c static gint ga_strcmp(gconstpointer str1, gconstpointer str2)

Quoted 4 lines
qga/main.c +2 -0
+static void ga_enable_non_blacklisted(QmpCommand *cmd, void *opaque)
 {
 
+    GList *blacklist = (GList *)opaque;

Again, the cast is not necessary.

Overall, I like the patch. Just a few tweaks suggested, but I can live with you adding:

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#3 lore raw

Quoting Eric Blake (2013-09-25 21:57:39)

Quoted 25 lines · Mark Wu
Quoted 8 lines

In the original code, qmp_get_command_list is used to construct a list of all commands' name. To get the information of all qga commands, it traverses the name list and search the command info with its name. So it can cause O(n^2) in the number of commands.

This patch adds an interface to traverse the qmp command list by QmpCommand to replace qmp_get_command_list. It can decrease the complexity from O(n) to O(n^2)

from O(n^2) to O(n)

Quoted 11 lines
---
 include/qapi/qmp/dispatch.h |  3 +-
 qapi/qmp-registry.c         | 28 ++-----------------
 qga/commands.c              | 39 ++++++++++----------------
 qga/main.c                  | 68 +++++++++++++++++----------------------------
 4 files changed, 45 insertions(+), 93 deletions(-)
Quoted 1 line

-bool qmp_command_is_enabled(const char *name)

Quoted 7 lines

I think one of Michael's suggestions was that you may still want qmp_command_is_enabled, but with a new signature:

bool qmp_command_is_enabled(const QmpCommand *cmd)

{
    return cmd->enabled;
}

That would still be my preference, as I can see the underlying fields changing somewhat in a future where QMP starts using the same infrastructure, and new users come along like qemu-ga guest->host commands we did as part of GSoC.

Wouldn't hold things up in the meantime, but if we're re-spinning anyway let's maintain the precedence of using getter/setter funcs for QmpCommand.

Quoted 40 lines
Quoted 7 lines
qga/commands.c +2 -2
-struct GuestAgentInfo *qmp_guest_info(Error **err)
+static void qmp_command_info(QmpCommand *cmd, void *opaque)
  {
-    GuestAgentInfo *info = g_malloc0(sizeof(GuestAgentInfo));
+    GuestAgentInfo *info = (GuestAgentInfo *)opaque; 
 

qga/commands.c void qmp_guest_ping(Error **err)

Quoted 10 lines
qga/commands.c +3 -5
 
-        cmd_info->enabled = qmp_command_is_enabled(cmd_info->name);
 
-        cmd_info_list = g_malloc0(sizeof(GuestAgentCommandInfoList));
-        cmd_info_list->value = cmd_info;
-        cmd_info_list->next = info->supported_commands;
-        info->supported_commands = cmd_info_list;
+    cmd_info = g_malloc0(sizeof(GuestAgentCommandInfo));
+    cmd_info->name = g_strdup(cmd->name);
+    cmd_info->enabled = cmd->enabled;

I guess it all depends on whether we want QmpCommand to be an opaque type outside of a single file. But I don't have a strong argument for making it opaque, and your approach works if we don't mind exposing the details of QmpCommand across multiple files. So I can live with your patch as-is.

qga/main.c static gint ga_strcmp(gconstpointer str1, gconstpointer str2)

Quoted 4 lines
qga/main.c +2 -0
+static void ga_enable_non_blacklisted(QmpCommand *cmd, void *opaque)
 {
 
+    GList *blacklist = (GList *)opaque;

Again, the cast is not necessary.

Overall, I like the patch. Just a few tweaks suggested, but I can live with you adding:

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#4 lore raw
In reply to Mark Wu

Quoting Mark Wu (2013-09-25 20:56:39)

Quoted 10 lines

In the original code, qmp_get_command_list is used to construct a list of all commands' name. To get the information of all qga commands, it traverses the name list and search the command info with its name. So it can cause O(n^2) in the number of commands.

This patch adds an interface to traverse the qmp command list by QmpCommand to replace qmp_get_command_list. It can decrease the complexity from O(n) to O(n^2)

Thanks, the qmp_for_each_command stuff is a nice cleanup.

Looks good other than previous comments.

Quoted 247 lines
---
 include/qapi/qmp/dispatch.h |  3 +-
 qapi/qmp-registry.c         | 28 ++-----------------
 qga/commands.c              | 39 ++++++++++----------------
 qga/main.c                  | 68 +++++++++++++++++----------------------------
 4 files changed, 45 insertions(+), 93 deletions(-)
include/qapi/qmp/dispatch.h +2 -1
index 1ce11f5..fb174f6 100644
@@ -48,8 +48,9 @@ QObject *qmp_dispatch(QObject *request);
 void qmp_disable_command(const char *name);
 void qmp_enable_command(const char *name);
 bool qmp_command_is_enabled(const char *name);
-char **qmp_get_command_list(void);
 QObject *qmp_build_error_object(Error *errp);
+typedef void (*qmp_cmd_callback_fn)(QmpCommand *cmd, void *opaque);
+void qmp_for_each_command(qmp_cmd_callback_fn fn, void *opaque);
 
 #endif
 
qapi/qmp-registry.c +2 -26
index 28bbbe8..844d597 100644
@@ -66,35 +66,11 @@ void qmp_enable_command(const char *name)
     qmp_toggle_command(name, true);
 }
 
-bool qmp_command_is_enabled(const char *name)
+void qmp_for_each_command(qmp_cmd_callback_fn fn, void *opaque)
 {
     QmpCommand *cmd;
 
     QTAILQ_FOREACH(cmd, &qmp_commands, node) {
-        if (strcmp(cmd->name, name) == 0) {
-            return cmd->enabled;
-        }
+        fn(cmd, opaque);
     }
-
-    return false;
-}
-
-char **qmp_get_command_list(void)
-{
-    QmpCommand *cmd;
-    int count = 1;
-    char **list_head, **list;
-
-    QTAILQ_FOREACH(cmd, &qmp_commands, node) {
-        count++;
-    }
-
-    list_head = list = g_malloc0(count * sizeof(char *));
-
-    QTAILQ_FOREACH(cmd, &qmp_commands, node) {
-        *list = g_strdup(cmd->name);
-        list++;
-    }
-
-    return list_head;
 }
qga/commands.c +15 -24
index 528b082..602cd47 100644
@@ -45,35 +45,26 @@ void qmp_guest_ping(Error **err)
     slog("guest-ping called");
 }
 
-struct GuestAgentInfo *qmp_guest_info(Error **err)
+static void qmp_command_info(QmpCommand *cmd, void *opaque)
 {
-    GuestAgentInfo *info = g_malloc0(sizeof(GuestAgentInfo));
+    GuestAgentInfo *info = (GuestAgentInfo *)opaque;
     GuestAgentCommandInfo *cmd_info;
     GuestAgentCommandInfoList *cmd_info_list;
-    char **cmd_list_head, **cmd_list;
-
-    info->version = g_strdup(QEMU_VERSION);
-
-    cmd_list_head = cmd_list = qmp_get_command_list();
-    if (*cmd_list_head == NULL) {
-        goto out;
-    }
-
-    while (*cmd_list) {
-        cmd_info = g_malloc0(sizeof(GuestAgentCommandInfo));
-        cmd_info->name = g_strdup(*cmd_list);
-        cmd_info->enabled = qmp_command_is_enabled(cmd_info->name);
 
-        cmd_info_list = g_malloc0(sizeof(GuestAgentCommandInfoList));
-        cmd_info_list->value = cmd_info;
-        cmd_info_list->next = info->supported_commands;
-        info->supported_commands = cmd_info_list;
+    cmd_info = g_malloc0(sizeof(GuestAgentCommandInfo));
+    cmd_info->name = g_strdup(cmd->name);
+    cmd_info->enabled = cmd->enabled;
+    cmd_info_list = g_malloc0(sizeof(GuestAgentCommandInfoList));
+    cmd_info_list->value = cmd_info;
+    cmd_info_list->next = info->supported_commands;
+    info->supported_commands = cmd_info_list;
+}
 
-        g_free(*cmd_list);
-        cmd_list++;
-    }
+struct GuestAgentInfo *qmp_guest_info(Error **err)
+{
+    GuestAgentInfo *info = g_malloc0(sizeof(GuestAgentInfo));
 
-out:
-    g_free(cmd_list_head);
+    info->version = g_strdup(QEMU_VERSION);
+    qmp_for_each_command(qmp_command_info, info);
     return info;
 }
qga/main.c +26 -42
index 6c746c8..1741d3f 100644
@@ -347,48 +347,34 @@ static gint ga_strcmp(gconstpointer str1, gconstpointer str2)
 }
 
 /* disable commands that aren't safe for fsfreeze */
-static void ga_disable_non_whitelisted(void)
+static void ga_disable_non_whitelisted(QmpCommand *cmd, void *opaque)
 {
-    char **list_head, **list;
     bool whitelisted;
     int i;
 
-    list_head = list = qmp_get_command_list();
-    while (*list != NULL) {
-        whitelisted = false;
-        i = 0;
-        while (ga_freeze_whitelist[i] != NULL) {
-            if (strcmp(*list, ga_freeze_whitelist[i]) == 0) {
-                whitelisted = true;
-            }
-            i++;
-        }
-        if (!whitelisted) {
-            g_debug("disabling command: %s", *list);
-            qmp_disable_command(*list);
+    whitelisted = false;
+    i = 0;
+    while (ga_freeze_whitelist[i] != NULL) {
+        if (strcmp(cmd->name, ga_freeze_whitelist[i]) == 0) {
+            whitelisted = true;
         }
-        g_free(*list);
-        list++;
+        i++;
+    }
+    if (!whitelisted) {
+        g_debug("disabling command: %s", cmd->name);
+        qmp_disable_command(cmd->name);
     }
-    g_free(list_head);
 }
 
 /* [re-]enable all commands, except those explicitly blacklisted by user */
-static void ga_enable_non_blacklisted(GList *blacklist)
+static void ga_enable_non_blacklisted(QmpCommand *cmd, void *opaque)
 {
-    char **list_head, **list;
-
-    list_head = list = qmp_get_command_list();
-    while (*list != NULL) {
-        if (g_list_find_custom(blacklist, *list, ga_strcmp) == NULL &&
-            !qmp_command_is_enabled(*list)) {
-            g_debug("enabling command: %s", *list);
-            qmp_enable_command(*list);
-        }
-        g_free(*list);
-        list++;
+    GList *blacklist = (GList *)opaque;
+    if (g_list_find_custom(blacklist, cmd->name, ga_strcmp) == NULL &&
+        !cmd->enabled) {
+        g_debug("enabling command: %s", cmd->name);
+        qmp_enable_command(cmd->name);
     }
-    g_free(list_head);
 }
 
 static bool ga_create_file(const char *path)
@@ -424,7 +410,7 @@ void ga_set_frozen(GAState *s)
         return;
     }
     /* disable all non-whitelisted (for frozen state) commands */
-    ga_disable_non_whitelisted();
+    qmp_for_each_command(ga_disable_non_whitelisted, NULL);
     g_warning("disabling logging due to filesystem freeze");
     ga_disable_logging(s);
     s->frozen = true;
@@ -460,7 +446,7 @@ void ga_unset_frozen(GAState *s)
     }
 
     /* enable all disabled, non-blacklisted commands */
-    ga_enable_non_blacklisted(s->blacklist);
+    qmp_for_each_command(ga_enable_non_blacklisted, s->blacklist);
     s->frozen = false;
     if (!ga_delete_file(s->state_filepath_isfrozen)) {
         g_warning("unable to delete %s, fsfreeze may not function properly",
@@ -920,6 +906,11 @@ int64_t ga_get_fd_handle(GAState *s, Error **errp)
     return handle;
 }
 
+static void ga_print_cmd(QmpCommand *cmd, void *opaque)
+{
+    printf("%s\n", cmd->name);
+}
+
 int main(int argc, char **argv)
 {
     const char *sopt = "hVvdm:p:l:f:F::b:s:t:";
@@ -996,15 +987,8 @@ int main(int argc, char **argv)
             daemonize = 1;
             break;
         case 'b': {
-            char **list_head, **list;
             if (is_help_option(optarg)) {
-                list_head = list = qmp_get_command_list();
-                while (*list != NULL) {
-                    printf("%s\n", *list);
-                    g_free(*list);
-                    list++;
-                }
-                g_free(list_head);
+                qmp_for_each_command(ga_print_cmd, NULL);
                 return 0;
             }
             for (j = 0, i = 0, len = strlen(optarg); i < len; i++) {
@@ -1126,7 +1110,7 @@ int main(int argc, char **argv)
             s->deferred_options.log_filepath = log_filepath;
         }
         ga_disable_logging(s);
-        ga_disable_non_whitelisted();
+        qmp_for_each_command(ga_disable_non_whitelisted, NULL);
     } else {
         if (daemonize) {
             become_daemon(pid_filepath);
Signature · 1 line
1.8.3.1