From mboxrd@z Thu Jan 1 00:00:00 1970 From: Andrew Cooper Subject: [PATCH v4 09/29] tools/libxl: Fix libxl__carefd_opened() to be more useful with an invalid fd Date: Tue, 14 Jul 2015 11:59:24 +0100 Message-ID: <1436871584-6522-10-git-send-email-andrew.cooper3@citrix.com> References: <1436871584-6522-1-git-send-email-andrew.cooper3@citrix.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: In-Reply-To: <1436871584-6522-1-git-send-email-andrew.cooper3@citrix.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: Xen-devel Cc: Andrew Cooper , Ian Jackson , Ian Campbell , Wei Liu List-Id: xen-devel@lists.xenproject.org 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 CC: Ian Campbell CC: Ian Jackson CC: Wei Liu --- Fixing the fnctl() error issue involves more TUITs than I currently have. New in v4 --- tools/libxl/libxl_fork.c | 5 ++++- tools/libxl/libxl_internal.h | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/tools/libxl/libxl_fork.c b/tools/libxl/libxl_fork.c index 4486687..024c1e2 100644 --- a/tools/libxl/libxl_fork.c +++ b/tools/libxl/libxl_fork.c @@ -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; } diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h index 3f1fed8..5d3499e 100644 --- a/tools/libxl/libxl_internal.h +++ b/tools/libxl/libxl_internal.h @@ -2189,7 +2189,7 @@ _hidden void libxl__ao_progress_report(libxl__egc *egc, libxl__ao *ao, _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 -- 1.7.10.4