Patchwork READCONFIG: allow /dev/fd/ the file to use for --readconfig

login
register
mail settings
Submitter ronniesahlberg@gmail.com
Date March 3, 2012, 7:57 a.m.
Message ID <1330761421-17379-2-git-send-email-ronniesahlberg@gmail.com>
Download mbox | patch
Permalink /patch/144411/
State New
Headers show

Comments

ronniesahlberg@gmail.com - March 3, 2012, 7:57 a.m.
On many platforms /dev/fd/<n> is used to refer to open filedescriptor <n> of the running process.
If we fail to open the provided filename and if the filename starts with '/dev/fd/' then assume this is a platform which do not support these special files.
In that case read out the descriptor value from the provided string and fdopen it.

This allows to use /dev/fd/<n> syntax on all platforms, those that do provide this indeface in /dev  and an emulation of this for the platforms that do not provide /dev/fd

Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
---
 qemu-config.c   |   16 +++++++++++++++-
 qemu-options.hx |    3 ++-
 2 files changed, 17 insertions(+), 2 deletions(-)

Patch

diff --git a/qemu-config.c b/qemu-config.c
index 7d9da78..96e7497 100644
--- a/qemu-config.c
+++ b/qemu-config.c
@@ -814,9 +814,23 @@  out:
 
 int qemu_read_config_file(const char *filename)
 {
-    FILE *f = fopen(filename, "r");
+    FILE *f;
     int ret;
 
+    f = fopen(filename, "r");
+
+    if (f == NULL && !strncmp(filename, "/dev/fd/", 8)) {
+        int fd;
+        char *ptr = NULL;
+
+        errno = 0;
+        fd = strtol(filename + 8, &ptr, 10);
+        if (errno != 0 || ptr == filename + 8 || *ptr != '\0') {
+            return -EINVAL;
+        }
+        f = fdopen(fd, "r");
+    }
+
     if (f == NULL) {
         return -errno;
     }
diff --git a/qemu-options.hx b/qemu-options.hx
index b129996..c70c6e7 100644
--- a/qemu-options.hx
+++ b/qemu-options.hx
@@ -2650,7 +2650,8 @@  DEF("readconfig", HAS_ARG, QEMU_OPTION_readconfig,
 STEXI
 @item -readconfig @var{file}
 @findex -readconfig
-Read device configuration from @var{file}.
+Read device configuration from @var{file}. Use '/dev/fd/<n>' as filename
+to read from an existing, inherited, filedesriptor.
 ETEXI
 DEF("writeconfig", HAS_ARG, QEMU_OPTION_writeconfig,
     "-writeconfig <file>\n"