From: "Jan Beulich" Subject: [PATCH] libxl: use libxl_fd_set_{cloexec, nonblock} helpers Date: Fri, 25 Jul 2014 16:02:23 +0100 Message-ID: <53D28D9F020000780002615D@mail.emea.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__PartF7C57B6F.1__=" Return-path: Received: from mail6.bemta14.messagelabs.com ([193.109.254.103]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1XAh0t-0002b4-GO for xen-devel@lists.xenproject.org; Fri, 25 Jul 2014 15:02:27 +0000 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: Ian Campbell , Ian Jackson , Stefano Stabellini List-Id: xen-devel@lists.xenproject.org This is a MIME message. If you are reading this text, you may want to consider changing to a mail reader or gateway that understands how to properly handle MIME multipart messages. --=__PartF7C57B6F.1__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline ... 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. Signed-off-by: Jan Beulich --- a/tools/libxl/libxl_create.c +++ b/tools/libxl/libxl_create.c @@ -977,7 +977,7 @@ void libxl__xc_domain_restore_done(libxl libxl_ctx *ctx =3D libxl__gc_owner(gc); char **vments =3D NULL, **localents =3D NULL; struct timeval start_time; - int i, esave, flags; + int i, esave; =20 /* convenience aliases */ const uint32_t domid =3D dcs->guest_domid; @@ -1046,15 +1046,10 @@ out: =20 esave =3D errno; =20 - flags =3D fcntl(fd, F_GETFL); - if (flags =3D=3D -1) { - LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "unable to get flags on = restore fd"); - } else { - flags &=3D ~O_NONBLOCK; - if (fcntl(fd, F_SETFL, flags) =3D=3D -1) - LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "unable to put = restore fd" + ret =3D 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"); - } =20 errno =3D esave; domcreate_rebuild_done(egc, dcs, ret); --- a/tools/libxl/libxl_qmp.c +++ b/tools/libxl/libxl_qmp.c @@ -358,19 +358,14 @@ static int qmp_open(libxl__qmp_handler * int timeout) { int ret; - int flags =3D 0; int i =3D 0; =20 qmp->qmp_fd =3D socket(AF_UNIX, SOCK_STREAM, 0); if (qmp->qmp_fd < 0) { return -1; } - if ((flags =3D fcntl(qmp->qmp_fd, F_GETFL)) =3D=3D -1) { - flags =3D 0; - } - if (fcntl(qmp->qmp_fd, F_SETFL, flags | O_NONBLOCK) =3D=3D -1) { - return -1; - } + ret =3D libxl_fd_set_nonblock(qmp->ctx, qmp->qmp_fd, 1); + if (ret) return -1; ret =3D libxl_fd_set_cloexec(qmp->ctx, qmp->qmp_fd, 1); if (ret) return -1; =20 --- a/tools/libxl/libxl_utils.c +++ b/tools/libxl/libxl_utils.c @@ -1047,11 +1047,17 @@ int libxl__random_bytes(libxl__gc *gc, u int fd; int ret; =20 - fd =3D open(dev, O_RDONLY | O_CLOEXEC); + fd =3D open(dev, O_RDONLY); if (fd < 0) { LOGE(ERROR, "failed to open \"%s\"", dev); return ERROR_FAIL; } + ret =3D 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; + } =20 ret =3D libxl_read_exactly(CTX, fd, buf, len, dev, NULL); =20 --=__PartF7C57B6F.1__= Content-Type: text/plain; name="libxl-use-cloexec-nonblock-helpers.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="libxl-use-cloexec-nonblock-helpers.patch" libxl: use libxl_fd_set_{cloexec,nonblock} helpers=0A=0A... instead of = open-coding them or not using them at all. This in=0Aparticular fixes a = build (and presumably also runtime) problem on old=0Aenough libc due to = the recent introduction of a use of O_CLOEXEC. The=0Aother two changes are = only of cleanup kind.=0A=0ASigned-off-by: Jan Beulich = =0A=0A--- a/tools/libxl/libxl_create.c=0A+++ b/tools/libxl/libxl_create.c= =0A@@ -977,7 +977,7 @@ void libxl__xc_domain_restore_done(libxl=0A = libxl_ctx *ctx =3D libxl__gc_owner(gc);=0A char **vments =3D NULL, = **localents =3D NULL;=0A struct timeval start_time;=0A- int i, = esave, flags;=0A+ int i, esave;=0A =0A /* convenience aliases */=0A = const uint32_t domid =3D dcs->guest_domid;=0A@@ -1046,15 +1046,10 @@ = out:=0A =0A esave =3D errno;=0A =0A- flags =3D fcntl(fd, = F_GETFL);=0A- if (flags =3D=3D -1) {=0A- LIBXL__LOG_ERRNO(ctx, = LIBXL__LOG_ERROR, "unable to get flags on restore fd");=0A- } else = {=0A- flags &=3D ~O_NONBLOCK;=0A- if (fcntl(fd, F_SETFL, = flags) =3D=3D -1)=0A- LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, = "unable to put restore fd"=0A+ ret =3D libxl_fd_set_nonblock(ctx, fd, = 0);=0A+ if (ret)=0A+ LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, = "unable to put restore fd"=0A " back to blocking = mode");=0A- }=0A =0A errno =3D esave;=0A domcreate_rebuild_done(= egc, dcs, ret);=0A--- a/tools/libxl/libxl_qmp.c=0A+++ b/tools/libxl/libxl_q= mp.c=0A@@ -358,19 +358,14 @@ static int qmp_open(libxl__qmp_handler *=0A = int timeout)=0A {=0A int ret;=0A- int flags =3D = 0;=0A int i =3D 0;=0A =0A qmp->qmp_fd =3D socket(AF_UNIX, = SOCK_STREAM, 0);=0A if (qmp->qmp_fd < 0) {=0A return -1;=0A = }=0A- if ((flags =3D fcntl(qmp->qmp_fd, F_GETFL)) =3D=3D -1) {=0A- = flags =3D 0;=0A- }=0A- if (fcntl(qmp->qmp_fd, F_SETFL, flags | = O_NONBLOCK) =3D=3D -1) {=0A- return -1;=0A- }=0A+ ret =3D = libxl_fd_set_nonblock(qmp->ctx, qmp->qmp_fd, 1);=0A+ if (ret) return = -1;=0A ret =3D libxl_fd_set_cloexec(qmp->ctx, qmp->qmp_fd, 1);=0A = if (ret) return -1;=0A =0A--- a/tools/libxl/libxl_utils.c=0A+++ b/tools/lib= xl/libxl_utils.c=0A@@ -1047,11 +1047,17 @@ int libxl__random_bytes(libxl__g= c *gc, u=0A int fd;=0A int ret;=0A =0A- fd =3D open(dev, = O_RDONLY | O_CLOEXEC);=0A+ fd =3D open(dev, O_RDONLY);=0A if (fd < = 0) {=0A LOGE(ERROR, "failed to open \"%s\"", dev);=0A = return ERROR_FAIL;=0A }=0A+ ret =3D libxl_fd_set_cloexec(CTX, fd, = 1);=0A+ if (ret) {=0A+ close(fd);=0A+ LOGE(ERROR, "failed = to set close-on-exec on handle to \"%s\"", dev);=0A+ return = ERROR_FAIL;=0A+ }=0A =0A ret =3D libxl_read_exactly(CTX, fd, buf, = len, dev, NULL);=0A =0A --=__PartF7C57B6F.1__= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xen.org http://lists.xen.org/xen-devel --=__PartF7C57B6F.1__=--