qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH v2] util: check the return value of fcntl in qemu_set_{block, nonblock}
@ 2018-12-13 11:37 Li Qiang
  2018-12-13 11:45 ` Peter Maydell
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Li Qiang @ 2018-12-13 11:37 UTC (permalink / raw)
  To: peter.maydell, marcandre.lureau, pbonzini, berrange; +Cc: qemu-devel, Li Qiang

Also add diagnostics info in 'qemu_set_cloexec' so that we can know
what happen when error occurs.

Signed-off-by: Li Qiang <liq3ea@gmail.com>
---
Change since v1: add diagnostics info
 
 util/oslib-posix.c | 37 ++++++++++++++++++++++++++++++++-----
 1 file changed, 32 insertions(+), 5 deletions(-)

diff --git a/util/oslib-posix.c b/util/oslib-posix.c
index c1bee2a581..14cbef1e35 100644
--- a/util/oslib-posix.c
+++ b/util/oslib-posix.c
@@ -38,6 +38,7 @@
 #include <libgen.h>
 #include <sys/signal.h>
 #include "qemu/cutils.h"
+#include "qemu/error-report.h"
 
 #ifdef CONFIG_LINUX
 #include <sys/syscall.h>
@@ -233,14 +234,32 @@ void qemu_set_block(int fd)
 {
     int f;
     f = fcntl(fd, F_GETFL);
-    fcntl(fd, F_SETFL, f & ~O_NONBLOCK);
+    if (f < 0) {
+        error_report("Unable to get file status flag on fd %d: %s(errno=%d)",
+                    fd, strerror(errno), errno);
+        abort();
+    }
+    if (fcntl(fd, F_SETFL, f & ~O_NONBLOCK) < 0) {
+        error_report("Unable to set blocking flag on fd %d: %s(errno=%d)",
+                    fd, strerror(errno), errno);
+        abort();
+    }
 }
 
 void qemu_set_nonblock(int fd)
 {
     int f;
     f = fcntl(fd, F_GETFL);
-    fcntl(fd, F_SETFL, f | O_NONBLOCK);
+    if (f < 0) {
+        error_report("Unable to get file status flag on fd %d: %s(errno=%d)",
+                    fd, strerror(errno), errno);
+        abort();
+    }
+    if (fcntl(fd, F_SETFL, f | O_NONBLOCK) < 0) {
+        error_report("Unable to set nonblocking flag on fd %d: %s(errno=%d)",
+                    fd, strerror(errno), errno);
+        abort();
+    }
 }
 
 int socket_set_fast_reuse(int fd)
@@ -259,9 +278,17 @@ void qemu_set_cloexec(int fd)
 {
     int f;
     f = fcntl(fd, F_GETFD);
-    assert(f != -1);
-    f = fcntl(fd, F_SETFD, f | FD_CLOEXEC);
-    assert(f != -1);
+    if (f < 0) {
+        error_report("Unable to get fd flags on fd %d: %s(errno=%d)",
+                    fd, strerror(errno), errno);
+        abort();
+    }
+    if (fcntl(fd, F_SETFD, f | FD_CLOEXEC) < 0) {
+        error_report("Unable to set fd close-on-exec flag on fd %d:"
+                    "%s(errno=%d)",
+                    fd, strerror(errno), errno);
+        abort();
+    }
 }
 
 /*
-- 
2.11.0

^ permalink raw reply related	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2018-12-14  9:49 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-12-13 11:37 [Qemu-devel] [PATCH v2] util: check the return value of fcntl in qemu_set_{block, nonblock} Li Qiang
2018-12-13 11:45 ` Peter Maydell
2018-12-13 11:48   ` Daniel P. Berrangé
2018-12-13 11:49   ` Li Qiang
2018-12-13 12:38 ` Markus Armbruster
2018-12-13 16:01 ` no-reply
2018-12-14  1:46   ` Li Qiang
2018-12-14  9:15     ` Li Qiang
2018-12-14  9:49       ` Peter Maydell

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).