Skip to contentSkip to conversation

Two chardev with fdset fixes

PATCH In review Michal Privoznik 4 participants 9 messages Aug 17, 2021 – Sep 6, 2021

lore
Share & tools
lore
#0 lore raw

cover Two chardev with fdset fixes

I was working on libvirt, trying to make it pass a pre-opened FD for a chardev's logfile. I've spent non-negligible amount of time trying to figure out why I was getting EACCESS error when QEMU definitely was able to access the FD. Well, found the problem and here is my attempt to save future me from figuring it out again.

NB, I'm not fully convinced that EBADFD is the best value, but anything is better than EACCESS.

Michal Privoznik (2):
chardev: Propagate error from logfile opening
monitor: Report EBADFD if fdset contains invalid FD

 chardev/char.c | 7 ++-----
 monitor/misc.c | 2 +-
 2 files changed, 3 insertions(+), 6 deletions(-)
Signature · 1 line
2.31.1
#1 lore raw

PATCH 1/2 chardev: Propagate error from logfile opening

If a chardev has a logfile the file is opened using qemu_open_old() which does the job, but since @errp is not propagated into qemu_open_internal() we lose much more accurate error and just report "Unable to open logfile $errno". When using plain files, it's probably okay as nothing complex is happening behind the curtains. But the problem becomes more prominent when passing an "/dev/fdset/XXX" path since much more needs to be done.

The fix is to use qemu_create() which passes @errp further down.

---
 chardev/char.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
chardev/char.c +2 -5
index 4595a8d430..0169d8dde4 100644
@@ -241,18 +241,15 @@ static void qemu_char_open(Chardev *chr, ChardevBackend *backend,
     ChardevCommon *common = backend ? backend->u.null.data : NULL;
 
     if (common && common->has_logfile) {
-        int flags = O_WRONLY | O_CREAT;
+        int flags = O_WRONLY;
         if (common->has_logappend &&
             common->logappend) {
             flags |= O_APPEND;
         } else {
             flags |= O_TRUNC;
         }
-        chr->logfd = qemu_open_old(common->logfile, flags, 0666);
+        chr->logfd = qemu_create(common->logfile, flags, 0666, errp);
         if (chr->logfd < 0) {
-            error_setg_errno(errp, errno,
-                             "Unable to open logfile %s",
-                             common->logfile);
             return;
         }
     }
Signature · 1 line
2.31.1
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] chardev: Propagate error from logfile opening

Quoted 9 lines · Michal Privoznik

If a chardev has a logfile the file is opened using qemu_open_old() which does the job, but since @errp is not propagated into qemu_open_internal() we lose much more accurate error and just report "Unable to open logfile $errno". When using plain files, it's probably okay as nothing complex is happening behind the curtains. But the problem becomes more prominent when passing an "/dev/fdset/XXX" path since much more needs to be done.

Quoted 6 lines

The fix is to use qemu_create() which passes @errp further down.

---
 chardev/char.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
Reviewed-by on v1 1/2
#3 lore raw

Re: [PATCH 1/2] chardev: Propagate error from logfile opening

In reply to Michal Privoznik
Quoted 9 lines · Michal Privoznik

If a chardev has a logfile the file is opened using qemu_open_old() which does the job, but since @errp is not propagated into qemu_open_internal() we lose much more accurate error and just report "Unable to open logfile $errno". When using plain files, it's probably okay as nothing complex is happening behind the curtains. But the problem becomes more prominent when passing an "/dev/fdset/XXX" path since much more needs to be done.

Quoted 4 lines

The fix is to use qemu_create() which passes @errp further down.

---

Quoted 33 lines
 chardev/char.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
chardev/char.c +2 -5
index 4595a8d430..0169d8dde4 100644
@@ -241,18 +241,15 @@ static void qemu_char_open(Chardev *chr,
 ChardevBackend *backend,
     ChardevCommon *common = backend ? backend->u.null.data : NULL;
 
     if (common && common->has_logfile) {
-        int flags = O_WRONLY | O_CREAT;
+        int flags = O_WRONLY;
         if (common->has_logappend &&
             common->logappend) {
             flags |= O_APPEND;
         } else {
             flags |= O_TRUNC;
         }
-        chr->logfd = qemu_open_old(common->logfile, flags, 0666);
+        chr->logfd = qemu_create(common->logfile, flags, 0666, errp);
         if (chr->logfd < 0) {
-            error_setg_errno(errp, errno,
-                             "Unable to open logfile %s",
-                             common->logfile);
             return;
         }
     }

--
2.31.1

#4 lore raw

Re: [PATCH 1/2] chardev: Propagate error from logfile opening

Quoted 12 lines · Marc-André Lureau
Quoted 9 lines · Michal Privoznik

If a chardev has a logfile the file is opened using qemu_open_old() which does the job, but since @errp is not propagated into qemu_open_internal() we lose much more accurate error and just report "Unable to open logfile $errno". When using plain files, it's probably okay as nothing complex is happening behind the curtains. But the problem becomes more prominent when passing an "/dev/fdset/XXX" path since much more needs to be done.

Quoted 6 lines
Quoted 4 lines

The fix is to use qemu_create() which passes @errp further down.

Thanks, can you add it to your pull queue please?

Michal

#5 lore raw

PATCH 2/2 monitor: Report EBADFD if fdset contains invalid FD

In reply to Michal Privoznik

When opening a path that starts with "/dev/fdset/" the control jumps into qemu_parse_fdset() and then into monitor_fdset_dup_fd_add(). In here, corresponding fdset is found and then all FDs from the set are iterated over trying to find an FD that matches expected access mode. For instance, if caller wants O_WRONLY then the FD set has to contain an O_WRONLY FD.

If no such FD is found then errno is set to EACCES which results in very misleading error messages, for instance:

Could not dup FD for /dev/fdset/3 flags 441: Permission denied

There is no permission issue, the problem is that there was no FD within given fdset that was in expected access mode. Therefore, let's set errno to EBADFD, which gives us somewhat better error messages:

Could not dup FD for /dev/fdset/3 flags 441: File descriptor in bad state

---
 monitor/misc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
monitor/misc.c +1 -1
index ffe7966870..a0eda0d574 100644
@@ -1347,7 +1347,7 @@ int monitor_fdset_dup_fd_add(int64_t fdset_id, int flags)
         }
 
         if (fd == -1) {
-            errno = EACCES;
+            errno = EBADFD;
             return -1;
         }
 
Signature · 1 line
2.31.1
#6 lore raw

Re: [PATCH 2/2] monitor: Report EBADFD if fdset contains invalid FD

Hi

Quoted 12 lines · Michal Privoznik

When opening a path that starts with "/dev/fdset/" the control jumps into qemu_parse_fdset() and then into monitor_fdset_dup_fd_add(). In here, corresponding fdset is found and then all FDs from the set are iterated over trying to find an FD that matches expected access mode. For instance, if caller wants O_WRONLY then the FD set has to contain an O_WRONLY FD.

If no such FD is found then errno is set to EACCES which results in very misleading error messages, for instance:

Could not dup FD for /dev/fdset/3 flags 441: Permission denied

Quoted 9 lines

There is no permission issue, the problem is that there was no FD within given fdset that was in expected access mode. Therefore, let's set errno to EBADFD, which gives us somewhat better error messages:

Could not dup FD for /dev/fdset/3 flags 441: File descriptor in bad state

I am not sure this is any better. If you try to open a read-only file, the system also reports EACCES (Permission denied). This is what the current code models, I believe.

Quoted 22 lines
---
 monitor/misc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
monitor/misc.c +1 -2
index ffe7966870..a0eda0d574 100644
@@ -1347,7 +1347,7 @@ int monitor_fdset_dup_fd_add(int64_t fdset_id, int
 flags)
         }
 
         if (fd == -1) {
-            errno = EACCES;
+            errno = EBADFD;
             return -1;
         }
 
--

2.31.1

#7 lore raw

Re: [PATCH 2/2] monitor: Report EBADFD if fdset contains invalid FD

Quoted 22 lines · Marc-André Lureau

Hi

Quoted 17 lines · Michal Privoznik

When opening a path that starts with "/dev/fdset/" the control jumps into qemu_parse_fdset() and then into monitor_fdset_dup_fd_add(). In here, corresponding fdset is found and then all FDs from the set are iterated over trying to find an FD that matches expected access mode. For instance, if caller wants O_WRONLY then the FD set has to contain an O_WRONLY FD.

If no such FD is found then errno is set to EACCES which results in very misleading error messages, for instance:

Could not dup FD for /dev/fdset/3 flags 441: Permission denied

There is no permission issue, the problem is that there was no FD within given fdset that was in expected access mode. Therefore, let's set errno to EBADFD, which gives us somewhat better error messages:

Quoted 8 lines
Quoted 4 lines

Could not dup FD for /dev/fdset/3 flags 441: File descriptor in bad state

I am not sure this is any better. If you try to open a read-only file, the system also reports EACCES (Permission denied). This is what the current code models, I believe.

Fair enough. Another idea I had was that if an FD that's O_RDWR was passed but only read or only write access was requested then such FD could be accepted because it is capable of reading/writing.

But since patch 1/2 was accepted then I guess 2/2 is not that much needed.

Michal

#8 lore raw

Re: [PATCH 2/2] monitor: Report EBADFD if fdset contains invalid FD

In reply to Marc-André Lureau
Quoted 22 lines · Marc-André Lureau

Hi

Quoted 17 lines · Michal Privoznik

When opening a path that starts with "/dev/fdset/" the control jumps into qemu_parse_fdset() and then into monitor_fdset_dup_fd_add(). In here, corresponding fdset is found and then all FDs from the set are iterated over trying to find an FD that matches expected access mode. For instance, if caller wants O_WRONLY then the FD set has to contain an O_WRONLY FD.

If no such FD is found then errno is set to EACCES which results in very misleading error messages, for instance:

Could not dup FD for /dev/fdset/3 flags 441: Permission denied

There is no permission issue, the problem is that there was no FD within given fdset that was in expected access mode. Therefore, let's set errno to EBADFD, which gives us somewhat better error messages:

Quoted 8 lines
Quoted 4 lines

Could not dup FD for /dev/fdset/3 flags 441: File descriptor in bad state

I am not sure this is any better. If you try to open a read-only file, the system also reports EACCES (Permission denied). This is what the current code models, I believe.

If we want better error reporting quality for this method we ought to just stop using errno and wire up a Error **errp parameter.

Quoted 20 lines
Quoted 20 lines

---
 monitor/misc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
monitor/misc.c +1 -1
index ffe7966870..a0eda0d574 100644
@@ -1347,7 +1347,7 @@ int monitor_fdset_dup_fd_add(int64_t fdset_id, int
 flags)
         }
 
         if (fd == -1) {
-            errno = EACCES;
+            errno = EBADFD;
             return -1;
         }
Quoted 4 lines
Quoted 4 lines

--
2.31.1

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|