Skip to contentSkip to conversation

readconfig: add emulation of /dev/fd/ to platforms that that lacks this API

PATCH In review Ronnie Sahlberg 2 participants 4 messages Mar 3, 2012 – Mar 20, 2012

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 readconfig: add emulation of /dev/fd/ to platforms that that lacks this API

Please find a patch to -readconfig.

On many platforms -readconfig /dev/fd/<n> can be used to read from an already opened and inherited filedescriptor <n>.

On platforms that do not natively provide /dev/fd/<n>
add emulation of this by checking if the ofiginal fopen(path) failed, then IF the path starts with /dev/fd/ then try to read the descriptorvalue and fdopen() it.

It means that we can use -readconfig /dev/fd/<n> on all platforms. On those that provide a /dev/fd we just open that file. On those that do not we emulate it.

regards
ronnie sahlberg

#1 lore raw

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

On many platforms /dev/fd/<n> ise 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

---
 qemu-config.c   |   16 +++++++++++++++-
 qemu-options.hx |    3 ++-
 2 files changed, 17 insertions(+), 2 deletions(-)
qemu-config.c +15 -1
index 7d9da78..7abc08d 100644
@@ -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;
     }
qemu-options.hx +2 -1
index b129996..c70c6e7 100644
@@ -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"
Signature · 1 line
1.7.3.1
#2 lore raw
In reply to Ronnie Sahlberg
Quoted 5 lines · Ronnie Sahlberg

Please find a patch to -readconfig.

On many platforms -readconfig /dev/fd/<n> can be used to read from an already opened and inherited filedescriptor<n>.

On platforms that do not natively provide /dev/fd/<n>

What platforms don't provide /dev/fd/<n> where fd passing makes sense?

Regards,

Anthony Liguori

Quoted 11 lines

add emulation of this by checking if the ofiginal fopen(path) failed, then IF the path starts with /dev/fd/ then try to read the descriptorvalue and fdopen() it.

It means that we can use -readconfig /dev/fd/<n> on all platforms. On those that provide a /dev/fd we just open that file. On those that do not we emulate it.

regards
ronnie sahlberg

#3 lore raw

None.
No platforms that I care about.

Quoted 33 lines · Anthony Liguori
Quoted 7 lines · Ronnie Sahlberg

Please find a patch to -readconfig.

On many platforms -readconfig /dev/fd/<n>  can be used to read from an already opened and inherited filedescriptor<n>.

On platforms that do not natively provide /dev/fd/<n>

What platforms don't provide /dev/fd/<n> where fd passing makes sense?

Regards,

Anthony Liguori

Quoted 15 lines

add emulation of this by checking if the ofiginal fopen(path) failed, then IF
the path starts with /dev/fd/ then try to read the descriptorvalue and fdopen() it.

It means that we can use -readconfig /dev/fd/<n>  on all platforms. On those that provide a /dev/fd we just open that file. On those that do not we emulate it.

regards
ronnie sahlberg