Skip to contentSkip to conversation

Allow -readconfig to read from a pre-existing filedescriptor

PATCH New Ronnie Sahlberg 5 participants 6 messages Jan 25, 2012 – Mar 8, 2012

lore
Share & tools
lore
#0 lore raw

cover Allow -readconfig to read from a pre-existing filedescriptor

List,

Please find attached a trivial patch to allow -readconfig to read from a pre-existing filedescriptor instead of a file off disk.

Syntax is '-readconfig fd:<n>' to read from filedescriptor <n>.

This is useful for example for libvirt which allow it to pass configuration data, including possibly sensitive such, into qemu without this data hitting the filesystem or a disk.

regards
ronnie sahlberg

#1 lore raw

PATCH 1/1 READCONFIG: Allow reading the configuration from a pre-existing filedescriptor

Update the readconfig filename parsing to allow specifying an existing, inherited, filedescriptor as 'fd:<n>'
This is useful when you want to pass potentially sensitive onfiguration data to qemu without having it hit the filesystem/stable-storage

---
 qemu-config.c   |   15 +++++++++++++--
 qemu-options.hx |    3 ++-
 2 files changed, 15 insertions(+), 3 deletions(-)
qemu-config.c +13 -2
index b030205..c12c5eb 100644
@@ -770,8 +770,19 @@ out:
 
 int qemu_read_config_file(const char *filename)
 {
-    FILE *f = fopen(filename, "r");
-    int ret;
+    FILE *f;
+    int ret, fd;
+
+    if (strncmp(filename, "fd:", 3)) {
+        f = fopen(filename, "r");
+    } else {
+        errno = 0;
+        fd = strtol(filename + 3, NULL, 10);
+        if (errno != 0) {
+            return -errno;
+        }
+        f = fdopen(fd, "r");
+    }
 
     if (f == NULL) {
         return -errno;
qemu-options.hx +2 -1
index 3a07ae8..653ff2d 100644
@@ -2577,7 +2577,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 '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
Reviewed-by on v1 1/1
#2 lore raw

Re: [Qemu-devel] [PATCH] READCONFIG: Allow reading the configuration from a pre-existing filedescriptor

Quoted 8 lines · Ronnie Sahlberg

Update the readconfig filename parsing to allow specifying an existing, inherited, filedescriptor as 'fd:<n>'
This is useful when you want to pass potentially sensitive onfiguration data to qemu without having it hit the filesystem/stable-storage

---
 qemu-config.c   |   15 +++++++++++++--
 qemu-options.hx |    3 ++-
 2 files changed, 15 insertions(+), 3 deletions(-)
Quoted 9 lines
qemu-config.c +0 -0
index b030205..c12c5eb 100644
@@ -770,8 +770,19 @@ out:
 
 int qemu_read_config_file(const char *filename)
 {

qemu-config.c out: · line 780

Quoted 10 lines
qemu-config.c +8 -2
-    FILE *f = fopen(filename, "r");
-    int ret;
+    FILE *f;
+    int ret, fd;
+
+    if (strncmp(filename, "fd:", 3)) {
+        f = fopen(filename, "r");
+    } else {
+        errno = 0;
+        fd = strtol(filename + 3, NULL, 10);

This means

-readconfig fd:4junk

will read fd 4. I don't know if there is a policy on ignoring vs. rejecting ill-formed command line, which is why I'm okay with the patch as-is if it meets project policy; but you might want to consider passing a non-NULL pointer for the second argument and rejecting an empty string or trailing junk.

qemu-config.c out: · line 783

Quoted 3 lines
qemu-config.c +3 -0
+        if (errno != 0) {
+            return -errno;
+        }

POSIX says that strtol("", NULL, 10) may return 0 without setting errno (that is, you can't rely on EINVAL in that case). That's another argument for _always_ passing a non-NULL pointer and to see if you accidentally parsed an empty string, since you don't want to have another FILE* competing with stdin. [Libvirt forbids the direct use of strtol and friends, and instead provides wrapper functions that take care of the sanity checking that is not mandated by POSIX; it may be worth introducing a qemu_strtol that does likewise, but that's a different cleanup project with wider scope.]

Signature · 2 lines
Eric Blake   [email protected]    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#3 lore raw

Re: [Qemu-devel] [PATCH] READCONFIG: Allow reading the configuration from a pre-existing filedescriptor

Quoted 19 lines · Eric Blake
Quoted 8 lines · Ronnie Sahlberg

Update the readconfig filename parsing to allow specifying an existing, inherited, filedescriptor as 'fd:<n>'
This is useful when you want to pass potentially sensitive onfiguration data to qemu without having it hit the filesystem/stable-storage

---
 qemu-config.c   |   15 +++++++++++++--
 qemu-options.hx |    3 ++-
 2 files changed, 15 insertions(+), 3 deletions(-)
Quoted 7 lines
qemu-config.c +0 -0
index b030205..c12c5eb 100644
@@ -770,8 +770,19 @@ out:
 
Quoted 12 lines

qemu-config.c out: · line 780

Quoted 12 lines
qemu-config.c +8 -2
 int qemu_read_config_file(const char *filename)
 {
-    FILE *f = fopen(filename, "r");
-    int ret;
+    FILE *f;
+    int ret, fd;
+
+    if (strncmp(filename, "fd:", 3)) {
+        f = fopen(filename, "r");
+    } else {
+        errno = 0;
+        fd = strtol(filename + 3, NULL, 10);

The user needs to escape filenames starting with fd by prefixing "./".

Makes -readconfig yet another special case. Well, one more won't kill us.

Still, it would be nice to factor out the common code, so that other instances can share it (and thus use the same syntax).

For full generality, make it a function taking (const char *filename_or_fd, int flags) and returning the open file descriptor. If filename_or_fd refers to a file descriptor, it must be open and compatible with flags.

Cleaner than this ad hoc overloading of the filename argument would be:

* Separate option for fd: -readconfig-fd FD

* Use the general syntax for NAME=VALUE arguments: -readconfig path=FNAME -readonfig fd=FD

With QemuOpts, can make "path" the implied_opt_name so that -readconfig FNAME still works. Need to escape ',' and '=' in filenames, but that's common in qemu command syntax.

Quoted 14 lines

This means

-readconfig fd:4junk

will read fd 4. I don't know if there is a policy on ignoring vs. rejecting ill-formed command line, which is why I'm okay with the patch as-is if it meets project policy; but you might want to consider passing a non-NULL pointer for the second argument and rejecting an empty string or trailing junk.

qemu-config.c out: · line 783

Quoted 3 lines
qemu-config.c +3 -0
+        if (errno != 0) {
+            return -errno;
+        }
Quoted 9 lines

POSIX says that strtol("", NULL, 10) may return 0 without setting errno (that is, you can't rely on EINVAL in that case). That's another argument for _always_ passing a non-NULL pointer and to see if you accidentally parsed an empty string, since you don't want to have another FILE* competing with stdin. [Libvirt forbids the direct use of strtol and friends, and instead provides wrapper functions that take care of the sanity checking that is not mandated by POSIX; it may be worth introducing a qemu_strtol that does likewise, but that's a different cleanup project with wider scope.]

strtol() should be used like this:

    errno = 0;
    val = strtol(str, &end, base);
    if (end == str || || *end != 0 || errno != 0) {
	// conversion failed
    }

Use "*end in follow set" instead of "*end != 0" when the number needn't be null terminated.

Plenty of bad examples to be found in qemu, I'm afraid.

#4 lore raw

Re: [Qemu-devel] [PATCH] READCONFIG: Allow reading the configuration from a pre-existing filedescriptor

Quoted 44 lines · Markus Armbruster
Quoted 25 lines · Eric Blake
Quoted 13 lines · Ronnie Sahlberg
qemu-config.c +3 -2
index b030205..c12c5eb 100644
@@ -770,8 +770,19 @@ out:
 
 int qemu_read_config_file(const char *filename)
 {
-    FILE *f = fopen(filename, "r");
-    int ret;
+    FILE *f;
+    int ret, fd;
+

qemu-config.c out: · line 785

Quoted 10 lines
qemu-config.c +8 -0
+    if (strncmp(filename, "fd:", 3)) {
+        f = fopen(filename, "r");
+    } else {
+        errno = 0;
+        fd = strtol(filename + 3, NULL, 10);
 
 
+        if (errno != 0) {
+            return -errno;
+        }
Quoted 9 lines

POSIX says that strtol("", NULL, 10) may return 0 without setting errno (that is, you can't rely on EINVAL in that case). That's another argument for _always_ passing a non-NULL pointer and to see if you accidentally parsed an empty string, since you don't want to have another FILE* competing with stdin. [Libvirt forbids the direct use of strtol and friends, and instead provides wrapper functions that take care of the sanity checking that is not mandated by POSIX; it may be worth introducing a qemu_strtol that does likewise, but that's a different cleanup project with wider scope.]

strtol() should be used like this:

    errno = 0;
    val = strtol(str, &end, base);
    if (end == str || || *end != 0 || errno != 0) {
	// conversion failed
    }
Quoted 4 lines

Use "*end in follow set" instead of "*end != 0" when the number needn't be null terminated.

Plenty of bad examples to be found in qemu, I'm afraid.

The rules for correct strtol() usage are so bizarre that, IMHO, you are doomed to always have plenty of bad usage in any non-trivial code. As Eric mentions, after fighting bad usage for a while in libvirt, we eventually banned all direct usage of strtol() in favour of a saner helper API, which has really helped our code quality in this area. The most important aspect of the saner API is the avoidance of overloading the return value to contain both the error status & and parsed value. We ended up using this (and some other variants for different sized integers):

/* Like strtol, but produce an "int" result, and check more carefully.
Return 0 upon success; return -1 to indicate failure.
When END_PTR is NULL, the byte after the final valid digit must be NUL. Otherwise, it's like strtol and lets the caller check any suffix for validity. This function is careful to return -1 when the string S represents a number that is not representable as an "int". */
int
virStrToLong_i(char const *s, char **end_ptr, int base, int *result)

{
    long int val;
    char *p;
    int err;

    errno = 0;
    val = strtol(s, &p, base);
    err = (errno || (!end_ptr && *p) || p == s || (int) val != val);
    if (end_ptr)
        *end_ptr = p;
    if (err)
        return -1;
    *result = val;
    return 0;
}

For extra paranoia you could also annotate the header file declaration with __attribute__((return_check)) to force callers to always check the error return status. I'd heartily encourage QEMU folks to similarly ban any use of strtol() (or the similarly badly designed glib equivalent) in favour of saner helper APIs.

Regards,
Daniel

Signature · 4 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|
#5 lore raw

Re: [Qemu-devel] [PATCH] READCONFIG: Allow reading the configuration from a pre-existing filedescriptor

In reply to Markus Armbruster
Quoted 10 lines · Markus Armbruster

Cleaner than this ad hoc overloading of the filename argument would be:

* Separate option for fd: -readconfig-fd FD

* Use the general syntax for NAME=VALUE arguments: -readconfig path=FNAME -readonfig fd=FD

With QemuOpts, can make "path" the implied_opt_name so that -readconfig FNAME still works. Need to escape ',' and '=' in filenames, but that's common in qemu command syntax.

I really think this option is better and cleaner. There's no reason to invent a new mini-language for argument syntax if we already have one.

Signature · 1 line
Eduardo