[2/3] -readconfig: use QemuOpts option format (v2)
diff mbox

Message ID 1332168883-9961-3-git-send-email-ehabkost@redhat.com
State New
Headers show

Commit Message

Eduardo Habkost March 19, 2012, 2:54 p.m. UTC
This changes -readconfig to use the name=value[,name=value] option
format.

It keeps "-readconfig filename" working by using the implied "path"
option name, but it changes the existing syntax a little, meaning that
files with "=" and "," in the filename have to be escaped.

I chose to break compatibility in those cases, instead of adding a new
option and keeping -readconfig as a legacy option. If you think adding a
new option is better, please let me know.

Most of the code is being added to qemu-config-arch.c because the
command-line handling of -readconfig will eventually depend on
arch-specific stuff, such as the list of default config files, and
qemu-config.o is not arch-specific.

Changes v1 -> v2:
 - Create qemu-config-arch.c for the readconfig-specific code
   that will depend on arch-specific stuff

Cc: Ronnie Sahlberg <ronniesahlberg@gmail.com>
Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
 Makefile.target    |    2 +-
 qemu-config-arch.c |   69 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 qemu-config-arch.h |   15 +++++++++++
 qemu-config.c      |   34 +++++++++++++++++--------
 qemu-config.h      |    4 ++-
 qemu-options.hx    |    4 +-
 vl.c               |    7 +++--
 7 files changed, 117 insertions(+), 18 deletions(-)
 create mode 100644 qemu-config-arch.c
 create mode 100644 qemu-config-arch.h

Patch
diff mbox

diff --git a/Makefile.target b/Makefile.target
index 37fb7ed..e48044b 100644
--- a/Makefile.target
+++ b/Makefile.target
@@ -204,7 +204,7 @@  endif #CONFIG_BSD_USER
 # System emulator target
 ifdef CONFIG_SOFTMMU
 
-obj-y = arch_init.o cpus.o monitor.o machine.o gdbstub.o balloon.o ioport.o
+obj-y = arch_init.o cpus.o monitor.o machine.o gdbstub.o balloon.o ioport.o qemu-config-arch.o
 # virtio has to be here due to weird dependency between PCI and virtio-net.
 # need to fix this properly
 obj-$(CONFIG_NO_PCI) += pci-stub.o
diff --git a/qemu-config-arch.c b/qemu-config-arch.c
new file mode 100644
index 0000000..9180b73
--- /dev/null
+++ b/qemu-config-arch.c
@@ -0,0 +1,69 @@ 
+/*
+ * Arch-specific Qemu config handling
+ *
+ * Copyright (c) 2012 Red Hat, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+#include "qemu-config-arch.h"
+#include "qemu-option.h"
+
+/* Not on vm_config_groups because this is not a global option setable by -set
+ * (QEMU_OPTION_set), just settings used to parse -readconfig argument.
+ */
+static QemuOptsList qemu_readconfig_opts = {
+    .name = "readconfig-opts",
+    .implied_opt_name = "path",
+    .head = QTAILQ_HEAD_INITIALIZER(qemu_readconfig_opts.head),
+    .desc = {
+        {
+            .name = "path",
+            .type = QEMU_OPT_STRING,
+        },
+        { /*End of list */ }
+    },
+};
+
+/* Read Qemu config file based on parsed QemuOpts object
+ *
+ * Returns 0 on success, -errno on failure.
+ */
+static int qemu_read_config_opts(QemuOpts *opts)
+{
+    const char *path = qemu_opt_get(opts, "path");
+    if (!path) {
+        return -EINVAL;
+    }
+    return qemu_read_config_filename(path);
+}
+
+/* Read config file based on option arguments on 'arg'
+ *
+ * Returns 0 on success, -errno on failure.
+ */
+int qemu_read_config_arg(const char *arg)
+{
+    QemuOpts *opts = qemu_opts_parse(&qemu_readconfig_opts, arg, 1);
+    if (!opts) {
+        return -EINVAL;
+    }
+    int r = qemu_read_config_opts(opts);
+    qemu_opts_del(opts);
+    return r;
+}
diff --git a/qemu-config-arch.h b/qemu-config-arch.h
new file mode 100644
index 0000000..3a9943d
--- /dev/null
+++ b/qemu-config-arch.h
@@ -0,0 +1,15 @@ 
+#ifndef QEMU_CONFIG_ARCH_H
+#define QEMU_CONFIG_ARCH_H
+/* Arch-specific qemu config code
+ *
+ * Contains functions that depend on arch-specific variables and settings,
+ * such as the handling of -readconfig help options.
+ */
+
+#include "qemu-config.h"
+
+/* Read config based on key=value options using QemuOpts syntax
+ */
+int qemu_read_config_arg(const char *arg);
+
+#endif /* QEMU_CONFIG_ARCH_H */
diff --git a/qemu-config.c b/qemu-config.c
index be84a03..a2c8453 100644
--- a/qemu-config.c
+++ b/qemu-config.c
@@ -816,21 +816,33 @@  out:
     return res;
 }
 
-int qemu_read_config_file(const char *filename)
+/* Read Qemu config file from open file
+ *
+ * The file will be closed before returning.
+ *
+ * Returns 0 on success, -errno on failure.
+ */
+static int qemu_read_config_file(FILE *f, const char *filename)
 {
-    FILE *f = fopen(filename, "r");
     int ret;
-
-    if (f == NULL) {
-        return -errno;
+    if (qemu_config_parse(f, vm_config_groups, filename) == 0) {
+        ret = 0;
+    } else {
+        ret = -EINVAL;
     }
-
-    ret = qemu_config_parse(f, vm_config_groups, filename);
     fclose(f);
+    return ret;
+}
 
-    if (ret == 0) {
-        return 0;
-    } else {
-        return -EINVAL;
+/* Read Qemu config file from 'filename'
+ *
+ * Returns 0 on success, -errno on failure.
+ */
+int qemu_read_config_filename(const char *filename)
+{
+    FILE *f = fopen(filename, "r");
+    if (f == NULL) {
+        return -errno;
     }
+    return qemu_read_config_file(f, filename);
 }
diff --git a/qemu-config.h b/qemu-config.h
index ba7796c..c2ab4fc 100644
--- a/qemu-config.h
+++ b/qemu-config.h
@@ -16,6 +16,8 @@  void qemu_add_globals(void);
 void qemu_config_write(FILE *fp);
 int qemu_config_parse(FILE *fp, QemuOptsList **lists, const char *fname);
 
-int qemu_read_config_file(const char *filename);
+/* Read config from filename
+ */
+int qemu_read_config_filename(const char *filename);
 
 #endif /* QEMU_CONFIG_H */
diff --git a/qemu-options.hx b/qemu-options.hx
index 39578f1..5d14f92 100644
--- a/qemu-options.hx
+++ b/qemu-options.hx
@@ -2655,9 +2655,9 @@  Old param mode (ARM only).
 ETEXI
 
 DEF("readconfig", HAS_ARG, QEMU_OPTION_readconfig,
-    "-readconfig <file>\n", QEMU_ARCH_ALL)
+    "-readconfig [path=]<file>\n", QEMU_ARCH_ALL)
 STEXI
-@item -readconfig @var{file}
+@item -readconfig [type=]@var{file}
 @findex -readconfig
 Read device configuration from @var{file}.
 ETEXI
diff --git a/vl.c b/vl.c
index bd95539..26e6738 100644
--- a/vl.c
+++ b/vl.c
@@ -146,6 +146,7 @@  int main(int argc, char **argv)
 #include "qjson.h"
 #include "qemu-option.h"
 #include "qemu-config.h"
+#include "qemu-config-arch.h"
 #include "qemu-options.h"
 #include "qmp-commands.h"
 #include "main-loop.h"
@@ -2351,12 +2352,12 @@  int main(int argc, char **argv, char **envp)
     if (defconfig) {
         int ret;
 
-        ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
+        ret = qemu_read_config_filename(CONFIG_QEMU_CONFDIR "/qemu.conf");
         if (ret < 0 && ret != -ENOENT) {
             exit(1);
         }
 
-        ret = qemu_read_config_file(arch_config_name);
+        ret = qemu_read_config_filename(arch_config_name);
         if (ret < 0 && ret != -ENOENT) {
             exit(1);
         }
@@ -3144,7 +3145,7 @@  int main(int argc, char **argv, char **envp)
             }
             case QEMU_OPTION_readconfig:
                 {
-                    int ret = qemu_read_config_file(optarg);
+                    int ret = qemu_read_config_arg(optarg);
                     if (ret < 0) {
                         fprintf(stderr, "read config %s: %s\n", optarg,
                             strerror(-ret));