libxl: use libxl_fd_set_{cloexec, nonblock} helpers
PATCH 1/1 libxl: use libxl_fd_set_{cloexec, nonblock} helpers
... instead of open-coding them or not using them at all. This in particular fixes a build (and presumably also runtime) problem on old enough libc due to the recent introduction of a use of O_CLOEXEC. The other two changes are only of cleanup kind.
tools/libxl/libxl_create.c +4 -9
@@ -977,7 +977,7 @@ void libxl__xc_domain_restore_done(libxllibxl_ctx *ctx =libxl__gc_owner (gc);char **vments =NULL , **localents =NULL ;struct timeval start_time; -int i, esave, flags; +int i, esave;/* convenience aliases */ const uint32_t domid = dcs->guest_domid;
@@ -1046,15 +1046,10 @@ out: esave = errno; - flags =fcntl (fd,F_GETFL ); -if (flags ==-1 ) { -LIBXL__LOG_ERRNO (ctx,LIBXL__LOG_ERROR ,"unable to get flags on restore fd" ); - }else { - flags &= ~O_NONBLOCK ; -if (fcntl (fd,F_SETFL , flags) ==-1 ) -LIBXL__LOG_ERRNO (ctx,LIBXL__LOG_ERROR ,"unable to put restore fd" + ret =libxl_fd_set_nonblock (ctx, fd,0 ); +if (ret) +LIBXL__LOG_ERRNO (ctx,LIBXL__LOG_ERROR ,"unable to put restore fd" " back to blocking mode" ); - } errno = esave;domcreate_rebuild_done (egc, dcs, ret);
tools/libxl/libxl_qmp.c +2 -7
@@ -358,19 +358,14 @@ static int qmp_open(libxl__qmp_handler * int timeout) {int ret; -int flags =0 ;int i =0 ; qmp->qmp_fd =socket (AF_UNIX ,SOCK_STREAM ,0 );if (qmp->qmp_fd <0 ) {return -1 ; } -if ((flags =fcntl (qmp->qmp_fd,F_GETFL )) ==-1 ) { - flags =0 ; - } -if (fcntl (qmp->qmp_fd,F_SETFL , flags |O_NONBLOCK ) ==-1 ) { -return -1 ; - } + ret =libxl_fd_set_nonblock (qmp->ctx, qmp->qmp_fd,1 ); +if (ret)return -1 ; ret =libxl_fd_set_cloexec (qmp->ctx, qmp->qmp_fd,1 );if (ret)return -1 ;
tools/libxl/libxl_utils.c +7 -1
@@ -1047,11 +1047,17 @@ int libxl__random_bytes(libxl__gc *gc, uint fd;int ret; - fd =open (dev,O_RDONLY |O_CLOEXEC ); + fd =open (dev,O_RDONLY );if (fd <0 ) {LOGE (ERROR ,"failed to open \"%s\"" , dev);return ERROR_FAIL ; } + ret =libxl_fd_set_cloexec (CTX , fd,1 ); +if (ret) { +close (fd); +LOGE (ERROR ,"failed to set close-on-exec on handle to \"%s\"" , dev); +return ERROR_FAIL ; + } ret =libxl_read_exactly (CTX , fd, buf, len, dev,NULL );
libxl: use libxl_fd_set_{cloexec,nonblock} helpers
... instead of open-coding them or not using them at all. This in particular fixes a build (and presumably also runtime) problem on old enough libc due to the recent introduction of a use of O_CLOEXEC. The other two changes are only of cleanup kind.
tools/libxl/libxl_create.c +4 -9
@@ -977,7 +977,7 @@ void libxl__xc_domain_restore_done(libxllibxl_ctx *ctx =libxl__gc_owner (gc);char **vments =NULL , **localents =NULL ;struct timeval start_time; -int i, esave, flags; +int i, esave;/* convenience aliases */ const uint32_t domid = dcs->guest_domid;
@@ -1046,15 +1046,10 @@ out: esave = errno; - flags =fcntl (fd,F_GETFL ); -if (flags ==-1 ) { -LIBXL__LOG_ERRNO (ctx,LIBXL__LOG_ERROR ,"unable to get flags on restore fd" ); - }else { - flags &= ~O_NONBLOCK ; -if (fcntl (fd,F_SETFL , flags) ==-1 ) -LIBXL__LOG_ERRNO (ctx,LIBXL__LOG_ERROR ,"unable to put restore fd" + ret =libxl_fd_set_nonblock (ctx, fd,0 ); +if (ret) +LIBXL__LOG_ERRNO (ctx,LIBXL__LOG_ERROR ,"unable to put restore fd" " back to blocking mode" ); - } errno = esave;domcreate_rebuild_done (egc, dcs, ret);
tools/libxl/libxl_qmp.c +2 -7
@@ -358,19 +358,14 @@ static int qmp_open(libxl__qmp_handler * int timeout) {int ret; -int flags =0 ;int i =0 ; qmp->qmp_fd =socket (AF_UNIX ,SOCK_STREAM ,0 );if (qmp->qmp_fd <0 ) {return -1 ; } -if ((flags =fcntl (qmp->qmp_fd,F_GETFL )) ==-1 ) { - flags =0 ; - } -if (fcntl (qmp->qmp_fd,F_SETFL , flags |O_NONBLOCK ) ==-1 ) { -return -1 ; - } + ret =libxl_fd_set_nonblock (qmp->ctx, qmp->qmp_fd,1 ); +if (ret)return -1 ; ret =libxl_fd_set_cloexec (qmp->ctx, qmp->qmp_fd,1 );if (ret)return -1 ;
tools/libxl/libxl_utils.c +7 -1
@@ -1047,11 +1047,17 @@ int libxl__random_bytes(libxl__gc *gc, uint fd;int ret; - fd =open (dev,O_RDONLY |O_CLOEXEC ); + fd =open (dev,O_RDONLY );if (fd <0 ) {LOGE (ERROR ,"failed to open \"%s\"" , dev);return ERROR_FAIL ; } + ret =libxl_fd_set_cloexec (CTX , fd,1 ); +if (ret) { +close (fd); +LOGE (ERROR ,"failed to set close-on-exec on handle to \"%s\"" , dev); +return ERROR_FAIL ; + } ret =libxl_read_exactly (CTX , fd, buf, len, dev,NULL );
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Jan Beulich writes ("[PATCH] libxl: use libxl_fd_set_{cloexec,nonblock} helpers"):
Quoted 4 lines
... instead of open-coding them or not using them at all. This in particular fixes a build (and presumably also runtime) problem on old enough libc due to the recent introduction of a use of
O_CLOEXEC. The other two changes are only of cleanup kind.
I think most of libxl should probably be using libxl__carefd* not setting cloexec. This is necessary whenever we need cloexec for correctness rather than just out of tidiness. (Search for carefd in libxl_internal.h.)
Quoted 6 lines
Signed-off-by Jan Beulich <[email protected]>tools/libxl/libxl_create.c +0
-0@@ -977,7 +977,7 @@ void libxl__xc_domain_restore_done(libxllibxl_ctx *ctx =libxl__gc_owner (gc);
...
tools/libxl/libxl_create.c out: · line 1052
Quoted 4 lines
tools/libxl/libxl_create.c +3
-0+ ret =libxl_fd_set_nonblock (ctx, fd,0 ); +if (ret) +LIBXL__LOG_ERRNO (ctx,LIBXL__LOG_ERROR ,"unable to put restore fd" " back to blocking mode" );
You do not need to log when libxl_fd_set_nonblock fails - it logs already.
Quoted 4 lines
tools/libxl/libxl_qmp.c +0
-0@@ -358,19 +358,14 @@ static int qmp_open(libxl__qmp_handler * int timeout)
...
Quoted 1 line
qmp->qmp_fd = socket(AF_UNIX, SOCK_STREAM, 0);
...
tools/libxl/libxl_qmp.c
Quoted 2 lines
tools/libxl/libxl_qmp.c +2
-0+ ret =libxl_fd_set_nonblock (qmp->ctx, qmp->qmp_fd,1 ); +if (ret)return -1 ;
This change is correct.
Quoted 2 lines
ret = libxl_fd_set_cloexec(qmp->ctx, qmp->qmp_fd, 1);
if (ret) return -1;But this (which you haven't touched) needs to be done with libxl__carefd* instead. (Your patch is not unacceptable due to you not making this change, on the basis that we should accept improvements even if they don't leave the code perfect.)
Quoted 3 lines
tools/libxl/libxl_utils.c +0
-0@@ -1047,11 +1047,17 @@ int libxl__random_bytes(libxl__gc *gc, u
...
tools/libxl/libxl_utils.c
Quoted 2 lines
tools/libxl/libxl_utils.c +1
-1- fd =open (dev,O_RDONLY |O_CLOEXEC ); + fd =open (dev,O_RDONLY );
Firstly, I don't understand why we care about cloexec on this fd, but it's harmless to do so.
tools/libxl/libxl_utils.c int libxl__random_bytes(libxl__gc *gc, u · line 1060
Quoted 6 lines
tools/libxl/libxl_utils.c +6
-0+ ret =libxl_fd_set_cloexec (CTX , fd,1 ); +if (ret) { +close (fd); +LOGE (ERROR ,"failed to set close-on-exec on handle to \"%s\"" , dev); +return ERROR_FAIL ; + }
Again, no need to log.
Thanks,
Ian.
Quoted 14 lines
Quoted 1 line
Quoted 1 line · On 25.07.14 at 19:07, <[email protected]> wrote:
Jan Beulich writes ("[PATCH] libxl: use libxl_fd_set_{cloexec,nonblock}
Quoted 4 lines
tools/libxl/libxl_qmp.c +0
-0@@ -358,19 +358,14 @@ static int qmp_open(libxl__qmp_handler * int timeout)...
Quoted 1 line
qmp->qmp_fd = socket(AF_UNIX, SOCK_STREAM, 0);...
tools/libxl/libxl_qmp.c
Quoted 2 lines
tools/libxl/libxl_qmp.c +2
-0+ ret =libxl_fd_set_nonblock (qmp->ctx, qmp->qmp_fd,1 ); +if (ret)return -1 ;This change is correct.
Quoted 7 lines
Quoted 2 lines
ret = libxl_fd_set_cloexec(qmp->ctx, qmp->qmp_fd, 1); if (ret) return -1;But this (which you haven't touched) needs to be done with libxl__carefd* instead. (Your patch is not unacceptable due to you not making this change, on the basis that we should accept improvements even if they don't leave the code perfect.)
And indeed I'd like to leave that conversion to you or someone else more familiar with the libxl concepts.
Quoted 9 lines
Quoted 3 lines
tools/libxl/libxl_utils.c +0
-0@@ -1047,11 +1047,17 @@ int libxl__random_bytes(libxl__gc *gc, u...
tools/libxl/libxl_utils.c
Quoted 2 lines
tools/libxl/libxl_utils.c +1
-1- fd =open (dev,O_RDONLY |O_CLOEXEC ); + fd =open (dev,O_RDONLY );Firstly, I don't understand why we care about cloexec on this fd, but it's harmless to do so.
And of course I'd be perfectly fine with a one liner just dropping it (all I really care about is that the file builds correctly in all cases). The question of why cloexec is needed/wanted here I'll pass on to author and committer, but I'd guess this is just to avoid proliferation of file handles into clones.
Jan
Quoted 32 lines · Jan Beulich
Quoted 14 lines
Quoted 1 line
Quoted 1 line · On 25.07.14 at 19:07, <[email protected]> wrote:
Jan Beulich writes ("[PATCH] libxl: use libxl_fd_set_{cloexec,nonblock}
Quoted 4 lines
tools/libxl/libxl_qmp.c +0
-0@@ -358,19 +358,14 @@ static int qmp_open(libxl__qmp_handler * int timeout)...
Quoted 1 line
qmp->qmp_fd = socket(AF_UNIX, SOCK_STREAM, 0);...
tools/libxl/libxl_qmp.c
Quoted 2 lines
tools/libxl/libxl_qmp.c +2
-0+ ret =libxl_fd_set_nonblock (qmp->ctx, qmp->qmp_fd,1 ); +if (ret)return -1 ;This change is correct.
Quoted 7 lines
Quoted 2 lines
ret = libxl_fd_set_cloexec(qmp->ctx, qmp->qmp_fd, 1); if (ret) return -1;But this (which you haven't touched) needs to be done with libxl__carefd* instead. (Your patch is not unacceptable due to you not making this change, on the basis that we should accept improvements even if they don't leave the code perfect.)
And indeed I'd like to leave that conversion to you or someone else more familiar with the libxl concepts.
Quoted 7 lines
Quoted 3 lines
tools/libxl/libxl_utils.c +0
-0@@ -1047,11 +1047,17 @@ int libxl__random_bytes(libxl__gc *gc, u...
Quoted 2 lines
tools/libxl/libxl_utils.c +1
-1- fd =open (dev,O_RDONLY |O_CLOEXEC ); + fd =open (dev,O_RDONLY );
Quoted 8 lines
Quoted 2 lines
Firstly, I don't understand why we care about cloexec on this fd, but it's harmless to do so.
And of course I'd be perfectly fine with a one liner just dropping it (all I really care about is that the file builds correctly in all cases). The question of why cloexec is needed/wanted here I'll pass on to author and committer, but I'd guess this is just to avoid proliferation of file handles into clones.
Surely it should be standard practice for a library to always set CLOEXEC on any files it opens, to minimize the side effects the library call has?
And if CLOEXEC is required then setting it at open time is the only safe way.
David
Quoted 7 lines · David Vrabel
Quoted 6 lines
And of course I'd be perfectly fine with a one liner just dropping it (all I really care about is that the file builds correctly in all cases). The question of why cloexec is needed/wanted here I'll pass on to author and committer, but I'd guess this is just to avoid proliferation of file handles into clones.
Quoted 6 lines
Surely it should be standard practice for a library to always set CLOEXEC on any files it opens, to minimize the side effects the library call has?
And if CLOEXEC is required then setting it at open time is the only safe way.
Jan's problem (according to the commit log) is that O_CLOEXEC isn't universally available yet.
Fortunately libxl has the carefd infrastructure which offers an alternative.
Ian.
Quoted 21 lines · David Vrabel
Quoted 13 lines · Jan Beulich
Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 25.07.14 at 19:07, <[email protected]> wrote:
Jan Beulich writes ("[PATCH] libxl: use libxl_fd_set_{cloexec,nonblock}
Quoted 4 lines
tools/libxl/libxl_qmp.c +0
-0@@ -358,19 +358,14 @@ static int qmp_open(libxl__qmp_handler * int timeout)...
Quoted 3 lines
tools/libxl/libxl_utils.c +0
-0@@ -1047,11 +1047,17 @@ int libxl__random_bytes(libxl__gc *gc, u...
Quoted 2 lines
tools/libxl/libxl_utils.c +1
-1- fd =open (dev,O_RDONLY |O_CLOEXEC ); + fd =open (dev,O_RDONLY );Quoted 7 lines
Quoted 2 lines
Firstly, I don't understand why we care about cloexec on this fd, but it's harmless to do so.
And of course I'd be perfectly fine with a one liner just dropping it (all I really care about is that the file builds correctly in all cases). The question of why cloexec is needed/wanted here I'll pass on to author and committer, but I'd guess this is just to avoid proliferation of file handles into clones.
Quoted 8 lines
Surely it should be standard practice for a library to always set CLOEXEC on any files it opens, to minimize the side effects the library call has?
And if CLOEXEC is required then setting it at open time is the only safe way.
David
Since this is an open, read, close; CLOEXEC is not needed at all. And it breaks the build on RHEL5. So I did a quick one liner.
From 46e776a2b770857d869ef54c418924af043274d0 Mon Sep 17 00:00:00 2001
From: Don Slutz <[email protected]>
Date: Thu, 31 Jul 2014 01:26:58 +0000
Subject: [PATCH] Make RHEL5 build again
--- tools/libxl/libxl_utils.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/libxl/libxl_utils.c +1 -1
@@ -1047,7 +1047,7 @@ int libxl__random_bytes(libxl__gc *gc, uint8_t *buf, size_t len)int fd;int ret; - fd =open (dev,O_RDONLY |O_CLOEXEC ); + fd =open (dev,O_RDONLY );if (fd <0 ) {LOGE (ERROR ,"failed to open \"%s\"" , dev);return ERROR_FAIL ;
Signature · 3 lines
1.8.2.1 -Don Slutz
Quoted 6 lines
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-develFrom
46e776a2b770857d869ef54c418924af043274d0Mon Sep 17 00:00:00 2001
From: Don Slutz <[email protected]>
Date: Thu, 31 Jul 2014 01:26:58 +0000
Subject: [PATCH] Make RHEL5 build again
--- tools/libxl/libxl_utils.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/libxl/libxl_utils.c +1 -1
@@ -1047,7 +1047,7 @@ int libxl__random_bytes(libxl__gc *gc, uint8_t *buf, size_t len)int fd;int ret; - fd =open (dev,O_RDONLY |O_CLOEXEC ); + fd =open (dev,O_RDONLY );if (fd <0 ) {LOGE (ERROR ,"failed to open \"%s\"" , dev);return ERROR_FAIL ;
Signature · 1 line
1.8.2.1
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
PATCH v2 1/1 libxl: use libxl_fd_set_{cloexec, nonblock} helpers
In reply to Ian Jackson... instead of open-coding them or not using them at all. This in particular fixes a build (and presumably also runtime) problem on old enough libc due to the recent introduction of a use of O_CLOEXEC. The other two changes are only of cleanup kind.
---
v2: Drop unnecessary logging (and result value corruption in the
libxl__xc_domain_restore_done() case).
tools/libxl/libxl_create.c +2 -12
@@ -977,7 +977,7 @@ void libxl__xc_domain_restore_done(libxllibxl_ctx *ctx =libxl__gc_owner (gc);char **vments =NULL , **localents =NULL ;struct timeval start_time; -int i, esave, flags; +int i, esave;/* convenience aliases */ const uint32_t domid = dcs->guest_domid;
@@ -1045,17 +1045,7 @@ out: } esave = errno; - - flags =fcntl (fd,F_GETFL ); -if (flags ==-1 ) { -LIBXL__LOG_ERRNO (ctx,LIBXL__LOG_ERROR ,"unable to get flags on restore fd" ); - }else { - flags &= ~O_NONBLOCK ; -if (fcntl (fd,F_SETFL , flags) ==-1 ) -LIBXL__LOG_ERRNO (ctx,LIBXL__LOG_ERROR ,"unable to put restore fd" -" back to blocking mode" ); - } - +libxl_fd_set_nonblock (ctx, fd,0 ); errno = esave;domcreate_rebuild_done (egc, dcs, ret); }
tools/libxl/libxl_qmp.c +2 -7
@@ -358,19 +358,14 @@ static int qmp_open(libxl__qmp_handler * int timeout) {int ret; -int flags =0 ;int i =0 ; qmp->qmp_fd =socket (AF_UNIX ,SOCK_STREAM ,0 );if (qmp->qmp_fd <0 ) {return -1 ; } -if ((flags =fcntl (qmp->qmp_fd,F_GETFL )) ==-1 ) { - flags =0 ; - } -if (fcntl (qmp->qmp_fd,F_SETFL , flags |O_NONBLOCK ) ==-1 ) { -return -1 ; - } + ret =libxl_fd_set_nonblock (qmp->ctx, qmp->qmp_fd,1 ); +if (ret)return -1 ; ret =libxl_fd_set_cloexec (qmp->ctx, qmp->qmp_fd,1 );if (ret)return -1 ;
tools/libxl/libxl_utils.c +6 -1
@@ -1047,11 +1047,16 @@ int libxl__random_bytes(libxl__gc *gc, uint fd;int ret; - fd =open (dev,O_RDONLY |O_CLOEXEC ); + fd =open (dev,O_RDONLY );if (fd <0 ) {LOGE (ERROR ,"failed to open \"%s\"" , dev);return ERROR_FAIL ; } + ret =libxl_fd_set_cloexec (CTX , fd,1 ); +if (ret) { +close (fd); +return ERROR_FAIL ; + } ret =libxl_read_exactly (CTX , fd, buf, len, dev,NULL );
libxl: use libxl_fd_set_{cloexec,nonblock} helpers
... instead of open-coding them or not using them at all. This in particular fixes a build (and presumably also runtime) problem on old enough libc due to the recent introduction of a use of O_CLOEXEC. The other two changes are only of cleanup kind.
---
v2: Drop unnecessary logging (and result value corruption in the
libxl__xc_domain_restore_done() case).
tools/libxl/libxl_create.c +2 -12
@@ -977,7 +977,7 @@ void libxl__xc_domain_restore_done(libxllibxl_ctx *ctx =libxl__gc_owner (gc);char **vments =NULL , **localents =NULL ;struct timeval start_time; -int i, esave, flags; +int i, esave;/* convenience aliases */ const uint32_t domid = dcs->guest_domid;
@@ -1045,17 +1045,7 @@ out: } esave = errno; - - flags =fcntl (fd,F_GETFL ); -if (flags ==-1 ) { -LIBXL__LOG_ERRNO (ctx,LIBXL__LOG_ERROR ,"unable to get flags on restore fd" ); - }else { - flags &= ~O_NONBLOCK ; -if (fcntl (fd,F_SETFL , flags) ==-1 ) -LIBXL__LOG_ERRNO (ctx,LIBXL__LOG_ERROR ,"unable to put restore fd" -" back to blocking mode" ); - } - +libxl_fd_set_nonblock (ctx, fd,0 ); errno = esave;domcreate_rebuild_done (egc, dcs, ret); }
tools/libxl/libxl_qmp.c +2 -7
@@ -358,19 +358,14 @@ static int qmp_open(libxl__qmp_handler * int timeout) {int ret; -int flags =0 ;int i =0 ; qmp->qmp_fd =socket (AF_UNIX ,SOCK_STREAM ,0 );if (qmp->qmp_fd <0 ) {return -1 ; } -if ((flags =fcntl (qmp->qmp_fd,F_GETFL )) ==-1 ) { - flags =0 ; - } -if (fcntl (qmp->qmp_fd,F_SETFL , flags |O_NONBLOCK ) ==-1 ) { -return -1 ; - } + ret =libxl_fd_set_nonblock (qmp->ctx, qmp->qmp_fd,1 ); +if (ret)return -1 ; ret =libxl_fd_set_cloexec (qmp->ctx, qmp->qmp_fd,1 );if (ret)return -1 ;
tools/libxl/libxl_utils.c +6 -1
@@ -1047,11 +1047,16 @@ int libxl__random_bytes(libxl__gc *gc, uint fd;int ret; - fd =open (dev,O_RDONLY |O_CLOEXEC ); + fd =open (dev,O_RDONLY );if (fd <0 ) {LOGE (ERROR ,"failed to open \"%s\"" , dev);return ERROR_FAIL ; } + ret =libxl_fd_set_cloexec (CTX , fd,1 ); +if (ret) { +close (fd); +return ERROR_FAIL ; + } ret =libxl_read_exactly (CTX , fd, buf, len, dev,NULL );
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Jan Beulich writes ("[PATCH v2] libxl: use libxl_fd_set_{cloexec,nonblock} helpers"):
Quoted 4 lines
... instead of open-coding them or not using them at all. This in particular fixes a build (and presumably also runtime) problem on old enough libc due to the recent introduction of a use of
O_CLOEXEC. The other two changes are only of cleanup kind.