tools/libxl: Fix libxl__carefd_opened() to be more useful with an invalid fd
authorAndrew Cooper <andrew.cooper3@citrix.com>
Tue, 14 Jul 2015 09:38:46 +0000 (10:38 +0100)
committerIan Jackson <Ian.Jackson@eu.citrix.com>
Wed, 15 Jul 2015 10:22:53 +0000 (11:22 +0100)
In the case that fd is -1, preserve errno and don't attempt to set
CLOEXEC.

Note that the implementation can still fail, as it ignores fcntl()
errors and may not set CLOEXEC properly.  Update the documentation
accordingly until it is fixed.

Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
CC: Ian Campbell <Ian.Campbell@citrix.com>
CC: Ian Jackson <Ian.Jackson@eu.citrix.com>
CC: Wei Liu <wei.liu2@citrix.com>
Acked-by: Ian Jackson <ian.jackson@eu.citrix.com>
---
Fixing the fnctl() error issue involves more TUITs than I currently
have.

New in v4

tools/libxl/libxl_fork.c
tools/libxl/libxl_internal.h

index 44866872b91b86b755b86ad2f0e990390ec737d1..024c1e2fb993588e02bcb9aab603853fb15c0e2d 100644 (file)
@@ -112,9 +112,12 @@ libxl__carefd *libxl__carefd_record(libxl_ctx *ctx, int fd)
 libxl__carefd *libxl__carefd_opened(libxl_ctx *ctx, int fd)
 {
     libxl__carefd *cf = 0;
+    int saved_errno = errno;
 
-    cf = libxl__carefd_record(ctx, fd);
+    if (fd >= 0)
+        cf = libxl__carefd_record(ctx, fd);
     libxl__carefd_unlock();
+    errno = saved_errno;
     return cf;
 }
 
index 3f1fed847e714d158333fb6b4cfe4a2e5581c8cf..5d3499eaef22a2a15fd7ddb89f9b196732902b25 100644 (file)
@@ -2189,7 +2189,7 @@ _hidden void libxl__carefd_unlock(void);
 _hidden libxl__carefd *libxl__carefd_record(libxl_ctx *ctx, int fd);
 
 /* Combines _record and _unlock in a single call.  If fd==-1,
- * still does the unlock, but returns 0.  Cannot fail. */
+ * still does the unlock, but returns 0. */
 _hidden libxl__carefd *libxl__carefd_opened(libxl_ctx *ctx, int fd);
 
 /* Works just like close(2).  You may pass NULL, in which case it's