From nobody Sun Feb 8 22:34:58 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1608050196; cv=none; d=zohomail.com; s=zohoarc; b=QZWYFAzl8SoLO8tWR0N39DM2A6mNYJw+0jmy4FYsm/B5AfCTEXjc6rRaEYOP29ezIGntNexzY2tHrpRgVkbIitMqvo8vUFsCxr+hJrq1hAmmf39p8VcGrOf0EGd+qXw93wvM2cwRWUOPckN3D3F46CabRMJborzg8HMqTJ3GTJ8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1608050196; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=iNV/KA8nEAAEkG05F5JTnuJ6pmnkrzAhlaFIwhddg8E=; b=cqSYY5caW97ouZZg9rBEaKvP8xgUgwSzNWu1aYGZ1ivzrQfzJu8bwDac1W/UPTdyxJMZex0edLH7bFht8bWFFSqfj2LizU2yd5I80T2GfiIYFWtFvVQBXnoIFU39RKH2K4aq3soWdbnHO39xIgPT8eOcLu6wu9grEJXtaitIHco= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1608050196044905.0170701075587; Tue, 15 Dec 2020 08:36:36 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.54527.94816 (Exim 4.92) (envelope-from ) id 1kpDJ5-00066a-Vi; Tue, 15 Dec 2020 16:36:11 +0000 Received: by outflank-mailman (output) from mailman id 54527.94816; Tue, 15 Dec 2020 16:36:11 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kpDJ5-00066T-RO; Tue, 15 Dec 2020 16:36:11 +0000 Received: by outflank-mailman (input) for mailman id 54527; Tue, 15 Dec 2020 16:36:10 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kpDJ4-000667-HD for xen-devel@lists.xenproject.org; Tue, 15 Dec 2020 16:36:10 +0000 Received: from mx2.suse.de (unknown [195.135.220.15]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 149f1d9d-c46c-4705-a48a-fc83a6645843; Tue, 15 Dec 2020 16:36:09 +0000 (UTC) Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 69CFDAD6A; Tue, 15 Dec 2020 16:36:08 +0000 (UTC) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 149f1d9d-c46c-4705-a48a-fc83a6645843 X-Virus-Scanned: by amavisd-new at test-mx.suse.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1608050168; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=iNV/KA8nEAAEkG05F5JTnuJ6pmnkrzAhlaFIwhddg8E=; b=YCy9BEGPS5F1VKvruRzYcdVXEV0FJVzDpOVUONqjGiynneQTGpLlQNqVGLYAOOr/x+SGNC KwaT/VQS4+5Xo1G88/GKds4KGuRfw+tzH897tVDe8j01cujdCoUfctAdbiMGdHob753AUZ QXUrBGyoLzKc7Wsxa5dcKs7eh7JghgQ= From: Juergen Gross To: xen-devel@lists.xenproject.org Cc: Juergen Gross , Ian Jackson , Wei Liu , Julien Grall Subject: [PATCH v10 04/25] tools/libxenevtchn: add possibility to not close file descriptor on exec Date: Tue, 15 Dec 2020 17:35:42 +0100 Message-Id: <20201215163603.21700-5-jgross@suse.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20201215163603.21700-1-jgross@suse.com> References: <20201215163603.21700-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) Content-Type: text/plain; charset="utf-8" Today the file descriptor for the access of the event channel driver is being closed in case of exec(2). For the support of live update of a daemon using libxenevtchn this can be problematic, so add a way to keep that file descriptor open. Add support of a flag XENEVTCHN_NO_CLOEXEC for xenevtchn_open() which will result in _not_ setting O_CLOEXEC when opening the event channel driver node. The caller can then obtain the file descriptor via xenevtchn_fd(). Add an alternative open function xenevtchn_open_fd() which takes that file descriptor as an additional parameter. This allows to allocate a xenevtchn_handle and to associate it with that file descriptor. Signed-off-by: Juergen Gross Reviewed-by: Wei Liu Reviewed-by: Julien Grall --- V7: - new patch V8: - some minor comments by Julien Grall addressed Signed-off-by: Juergen Gross --- tools/include/xenevtchn.h | 16 ++++++++++- tools/libs/evtchn/Makefile | 2 +- tools/libs/evtchn/core.c | 45 ++++++++++++++++++++++++------ tools/libs/evtchn/freebsd.c | 9 ++++-- tools/libs/evtchn/libxenevtchn.map | 4 +++ tools/libs/evtchn/linux.c | 9 ++++-- tools/libs/evtchn/minios.c | 6 +++- tools/libs/evtchn/netbsd.c | 2 +- tools/libs/evtchn/private.h | 2 +- tools/libs/evtchn/solaris.c | 2 +- 10 files changed, 79 insertions(+), 18 deletions(-) diff --git a/tools/include/xenevtchn.h b/tools/include/xenevtchn.h index 91821ee56d..dadc46ea36 100644 --- a/tools/include/xenevtchn.h +++ b/tools/include/xenevtchn.h @@ -64,11 +64,25 @@ struct xentoollog_logger; * * Calling xenevtchn_close() is the only safe operation on a * xenevtchn_handle which has been inherited. + * + * Setting XENEVTCHN_NO_CLOEXEC allows to keep the file descriptor used + * for the event channel driver open across exec(2). In order to be able + * to use that file descriptor the new binary activated via exec(2) has + * to call xenevtchn_open_fd() with that file descriptor as parameter in + * order to associate it with a new handle. The file descriptor can be + * obtained via xenevtchn_fd() before calling exec(2). */ -/* Currently no flags are defined */ + +/* Don't set O_CLOEXEC when opening event channel driver node. */ +#define XENEVTCHN_NO_CLOEXEC 0x01 + xenevtchn_handle *xenevtchn_open(struct xentoollog_logger *logger, unsigned open_flags); =20 +/* Flag XENEVTCHN_NO_CLOEXEC is ignored by xenevtchn_open_fd(). */ +xenevtchn_handle *xenevtchn_open_fd(struct xentoollog_logger *logger, + int fd, unsigned open_flags); + /* * Close a handle previously allocated with xenevtchn_open(). */ diff --git a/tools/libs/evtchn/Makefile b/tools/libs/evtchn/Makefile index ad01a17b3d..b8c37b5b97 100644 --- a/tools/libs/evtchn/Makefile +++ b/tools/libs/evtchn/Makefile @@ -2,7 +2,7 @@ XEN_ROOT =3D $(CURDIR)/../../.. include $(XEN_ROOT)/tools/Rules.mk =20 MAJOR =3D 1 -MINOR =3D 1 +MINOR =3D 2 =20 SRCS-y +=3D core.c SRCS-$(CONFIG_Linux) +=3D linux.c diff --git a/tools/libs/evtchn/core.c b/tools/libs/evtchn/core.c index aff6ecfaa0..fa1e44b6ea 100644 --- a/tools/libs/evtchn/core.c +++ b/tools/libs/evtchn/core.c @@ -13,6 +13,7 @@ * License along with this library; If not, see . */ =20 +#include #include #include =20 @@ -28,10 +29,9 @@ static int all_restrict_cb(Xentoolcore__Active_Handle *a= h, domid_t domid) { return xenevtchn_restrict(xce, domid); } =20 -xenevtchn_handle *xenevtchn_open(xentoollog_logger *logger, unsigned open_= flags) +static xenevtchn_handle *xenevtchn_alloc_handle(xentoollog_logger *logger) { xenevtchn_handle *xce =3D malloc(sizeof(*xce)); - int rc; =20 if (!xce) return NULL; =20 @@ -49,19 +49,48 @@ xenevtchn_handle *xenevtchn_open(xentoollog_logger *log= ger, unsigned open_flags) if (!xce->logger) goto err; } =20 - rc =3D osdep_evtchn_open(xce); - if ( rc < 0 ) goto err; + return xce; + +err: + xenevtchn_close(xce); + return NULL; +} + +xenevtchn_handle *xenevtchn_open(xentoollog_logger *logger, unsigned open_= flags) +{ + xenevtchn_handle *xce =3D xenevtchn_alloc_handle(logger); + int rc; + + if (!xce) return NULL; + + rc =3D osdep_evtchn_open(xce, open_flags); + if ( rc < 0 ) goto err; =20 return xce; =20 err: - xentoolcore__deregister_active_handle(&xce->tc_ah); - osdep_evtchn_close(xce); - xtl_logger_destroy(xce->logger_tofree); - free(xce); + xenevtchn_close(xce); return NULL; } =20 +xenevtchn_handle *xenevtchn_open_fd(struct xentoollog_logger *logger, + int fd, unsigned open_flags) +{ + xenevtchn_handle *xce; + + if (open_flags & ~XENEVTCHN_NO_CLOEXEC) { + errno =3D EINVAL; + return NULL; + } + + xce =3D xenevtchn_alloc_handle(logger); + if (!xce) return NULL; + + xce->fd =3D fd; + + return xce; +} + int xenevtchn_close(xenevtchn_handle *xce) { int rc; diff --git a/tools/libs/evtchn/freebsd.c b/tools/libs/evtchn/freebsd.c index 6564ed4c44..635c10f09f 100644 --- a/tools/libs/evtchn/freebsd.c +++ b/tools/libs/evtchn/freebsd.c @@ -31,9 +31,14 @@ =20 #define EVTCHN_DEV "/dev/xen/evtchn" =20 -int osdep_evtchn_open(xenevtchn_handle *xce) +int osdep_evtchn_open(xenevtchn_handle *xce, unsigned int open_flags) { - int fd =3D open(EVTCHN_DEV, O_RDWR|O_CLOEXEC); + int flags =3D O_RDWR; + int fd; + + if ( !(open_flags & XENEVTCHN_NO_CLOEXEC) ) + flags |=3D O_CLOEXEC; + fd =3D open(EVTCHN_DEV, flags); if ( fd =3D=3D -1 ) return -1; xce->fd =3D fd; diff --git a/tools/libs/evtchn/libxenevtchn.map b/tools/libs/evtchn/libxene= vtchn.map index 33a38f953a..722fa026f9 100644 --- a/tools/libs/evtchn/libxenevtchn.map +++ b/tools/libs/evtchn/libxenevtchn.map @@ -21,3 +21,7 @@ VERS_1.1 { global: xenevtchn_restrict; } VERS_1.0; +VERS_1.2 { + global: + xenevtchn_open_fd; +} VERS_1.1; diff --git a/tools/libs/evtchn/linux.c b/tools/libs/evtchn/linux.c index 17e64aea32..2297488f88 100644 --- a/tools/libs/evtchn/linux.c +++ b/tools/libs/evtchn/linux.c @@ -34,9 +34,14 @@ #define O_CLOEXEC 0 #endif =20 -int osdep_evtchn_open(xenevtchn_handle *xce) +int osdep_evtchn_open(xenevtchn_handle *xce, unsigned int open_flags) { - int fd =3D open("/dev/xen/evtchn", O_RDWR|O_CLOEXEC); + int flags =3D O_RDWR; + int fd; + + if ( !(open_flags & XENEVTCHN_NO_CLOEXEC) ) + flags |=3D O_CLOEXEC; + fd =3D open("/dev/xen/evtchn", flags); if ( fd =3D=3D -1 ) return -1; xce->fd =3D fd; diff --git a/tools/libs/evtchn/minios.c b/tools/libs/evtchn/minios.c index 9cd7636fc5..f5db021747 100644 --- a/tools/libs/evtchn/minios.c +++ b/tools/libs/evtchn/minios.c @@ -63,7 +63,11 @@ static void port_dealloc(struct evtchn_port_info *port_i= nfo) { free(port_info); } =20 -int osdep_evtchn_open(xenevtchn_handle *xce) +/* + * XENEVTCHN_NO_CLOEXEC is being ignored, as there is no exec() call suppo= rted + * in Mini-OS. + */ +int osdep_evtchn_open(xenevtchn_handle *xce, unsigned int open_flags) { int fd =3D alloc_fd(FTYPE_EVTCHN); if ( fd =3D=3D -1 ) diff --git a/tools/libs/evtchn/netbsd.c b/tools/libs/evtchn/netbsd.c index 8b8545d2f9..7c73d1c599 100644 --- a/tools/libs/evtchn/netbsd.c +++ b/tools/libs/evtchn/netbsd.c @@ -31,7 +31,7 @@ =20 #define EVTCHN_DEV_NAME "/dev/xenevt" =20 -int osdep_evtchn_open(xenevtchn_handle *xce) +int osdep_evtchn_open(xenevtchn_handle *xce, unsigned int open_flags) { int fd =3D open(EVTCHN_DEV_NAME, O_NONBLOCK|O_RDWR); if ( fd =3D=3D -1 ) diff --git a/tools/libs/evtchn/private.h b/tools/libs/evtchn/private.h index 31e595bea2..bcac2a191d 100644 --- a/tools/libs/evtchn/private.h +++ b/tools/libs/evtchn/private.h @@ -14,7 +14,7 @@ struct xenevtchn_handle { Xentoolcore__Active_Handle tc_ah; }; =20 -int osdep_evtchn_open(xenevtchn_handle *xce); +int osdep_evtchn_open(xenevtchn_handle *xce, unsigned int open_flags); int osdep_evtchn_close(xenevtchn_handle *xce); int osdep_evtchn_restrict(xenevtchn_handle *xce, domid_t domid); =20 diff --git a/tools/libs/evtchn/solaris.c b/tools/libs/evtchn/solaris.c index dd41f62a24..7e22f7906a 100644 --- a/tools/libs/evtchn/solaris.c +++ b/tools/libs/evtchn/solaris.c @@ -29,7 +29,7 @@ =20 #include "private.h" =20 -int osdep_evtchn_open(xenevtchn_handle *xce) +int osdep_evtchn_open(xenevtchn_handle *xce, unsigned int open_flags) { int fd; =20 --=20 2.26.2