From nobody Sat Feb 7 05:49:28 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=none (zoho.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail(p=none dis=none) header.from=citrix.com ARC-Seal: i=1; a=rsa-sha256; t=1574097277; cv=none; d=zoho.com; s=zohoarc; b=Qi4V1Y45Sk5ytl2T35QXVEzzv/JyoWHu1g2kF3FOwD9GKn0b5KXDFc2uPr6XIfgTVoeBVXEXZttskw4UsepJAZ+7hG6ZhsXxkaaHFDnMBa/i4o5HX5uOVjsWwjZ5QpkP9vtHVt3QOPHNJ1tkZF/dmtQGpt2WWmDYseEldc29h5Y= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1574097277; h=Content-Type: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=qd/FnsivVejZp7KsMK+wJKxMQzVlpYHZhF9KRb60QW0=; b=Zy2cLI2YTOVEcBy49uUDa78a2Y+HFw5GidBOwhua3AEZ0D7PZQlqwoZY0AOS2KiKYoUN4fa8WBSkLDcZdCD7KoEJSkyDcAp1PHospVA4ceHf0sw99xPjhIasfMpNWLC/ZDWMHaTzZ6ck8MbOl7mA6bDG/1QT72LW6sxKurN/LlU= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=none (zoho.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail header.from= (p=none 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 15740972775391015.1335755768482; Mon, 18 Nov 2019 09:14:37 -0800 (PST) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1iWkap-00069G-Nl; Mon, 18 Nov 2019 17:13:39 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1iWkao-00068c-Dt for xen-devel@lists.xenproject.org; Mon, 18 Nov 2019 17:13:38 +0000 Received: from esa6.hc3370-68.iphmx.com (unknown [216.71.155.175]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id b5d7699c-0a26-11ea-a2dc-12813bfff9fa; Mon, 18 Nov 2019 17:13:16 +0000 (UTC) X-Inumbo-ID: b5d7699c-0a26-11ea-a2dc-12813bfff9fa DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=citrix.com; s=securemail; t=1574097196; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=OPSYj/yytfjcIu6uS+v5Pj35ycSoLt2eaB7Pwu1ehiw=; b=Pih5VVFlkTUNCQ7mfBI/UhtN0iJgtyXpw+2XBvVw280tZHbO+wpuXkGi UKolnJOlrdIytkglPwwaT4pvrEG7rMdhRXkSvwRb7+vqpTQVkFiQGf6pj 2wUWkyXKoCol1uIpOtyAg9hw/eeDROPXw1VEDTkV/11XDA3nw6NJrK2VR I=; Authentication-Results: esa6.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none; spf=None smtp.pra=anthony.perard@citrix.com; spf=Pass smtp.mailfrom=anthony.perard@citrix.com; spf=None smtp.helo=postmaster@mail.citrix.com Received-SPF: none (zoho.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Received-SPF: None (esa6.hc3370-68.iphmx.com: no sender authenticity information available from domain of anthony.perard@citrix.com) identity=pra; client-ip=162.221.158.21; receiver=esa6.hc3370-68.iphmx.com; envelope-from="anthony.perard@citrix.com"; x-sender="anthony.perard@citrix.com"; x-conformance=sidf_compatible Received-SPF: Pass (esa6.hc3370-68.iphmx.com: domain of anthony.perard@citrix.com designates 162.221.158.21 as permitted sender) identity=mailfrom; client-ip=162.221.158.21; receiver=esa6.hc3370-68.iphmx.com; envelope-from="anthony.perard@citrix.com"; x-sender="anthony.perard@citrix.com"; x-conformance=sidf_compatible; x-record-type="v=spf1"; x-record-text="v=spf1 ip4:209.167.231.154 ip4:178.63.86.133 ip4:195.66.111.40/30 ip4:85.115.9.32/28 ip4:199.102.83.4 ip4:192.28.146.160 ip4:192.28.146.107 ip4:216.52.6.88 ip4:216.52.6.188 ip4:162.221.158.21 ip4:162.221.156.83 ip4:168.245.78.127 ~all" Received-SPF: None (esa6.hc3370-68.iphmx.com: no sender authenticity information available from domain of postmaster@mail.citrix.com) identity=helo; client-ip=162.221.158.21; receiver=esa6.hc3370-68.iphmx.com; envelope-from="anthony.perard@citrix.com"; x-sender="postmaster@mail.citrix.com"; x-conformance=sidf_compatible IronPort-SDR: QaqZEGu9bwQ7mP35um94W7HpUttL+SP+9SeJmkNjHdP5RKXYT0G+1e3QappyQE06NUsNLvOuc9 Je+Z+1BD6EEtElBrXTSx6BIv70bk2RNgl2pe7QMBLcD5rxcmWTrOUEiTyZf48mZiSY1uz7v48q DDwqkRxYghZvhmYtWQS06gHluRfDuAdpKeEHBM//oId88cJwkR/78bm76OCglZA1QWdkVtf5iV IBQ2SAaHoYgBwnVvpGCRM//Hw/2isWTn6Xd9rlt/Rt4ApADkkuUQePnery1MBUjLcpS8CBaK2A xx4= X-SBRS: 2.7 X-MesageID: 8888856 X-Ironport-Server: esa6.hc3370-68.iphmx.com X-Remote-IP: 162.221.158.21 X-Policy: $RELAYED X-IronPort-AV: E=Sophos;i="5.68,320,1569297600"; d="scan'208";a="8888856" From: Anthony PERARD To: Date: Mon, 18 Nov 2019 17:13:02 +0000 Message-ID: <20191118171309.1459302-2-anthony.perard@citrix.com> X-Mailer: git-send-email 2.24.0 In-Reply-To: <20191118171309.1459302-1-anthony.perard@citrix.com> References: <20191118171309.1459302-1-anthony.perard@citrix.com> MIME-Version: 1.0 Subject: [Xen-devel] [XEN PATCH for-4.13 v3 1/7] libxl: Introduce libxl__ev_child_kill_deregister X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Anthony PERARD , Ian Jackson , Wei Liu Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Allow to deregister the callback associated with a child death event. The death isn't immediate will need to be collected later, so the ev_child machinery register its own callback. libxl__ev_child_kill_deregister() might be called by an AO operation that is finishing/cleaning up without a chance for libxl to be notified of the child death (via SIGCHLD). So it is possible that the application calls libxl_ctx_free() while there are still child around. To avoid the application getting unexpected SIGCHLD, the libxl__ao responsible for killing a child will have to wait until it has been properly reaped. Signed-off-by: Anthony PERARD Acked-by: Ian Jackson --- Notes: v2: - Rename new fn to libxl__ev_child_kill_deregister - Rework documentation of the new API and if ev_child - Add debug log in libxl__ao_complete - Always call libxl_report_child_exitstatus() in child callback. tools/libxl/libxl_event.c | 6 ++++- tools/libxl/libxl_fork.c | 48 ++++++++++++++++++++++++++++++++++++ tools/libxl/libxl_internal.h | 15 ++++++++--- 3 files changed, 65 insertions(+), 4 deletions(-) diff --git a/tools/libxl/libxl_event.c b/tools/libxl/libxl_event.c index 0370b6acdd1c..43155368de76 100644 --- a/tools/libxl/libxl_event.c +++ b/tools/libxl/libxl_event.c @@ -1878,6 +1878,9 @@ void libxl__ao_complete(libxl__egc *egc, libxl__ao *a= o, int rc) ao->complete =3D 1; ao->rc =3D rc; LIBXL_LIST_REMOVE(ao, inprogress_entry); + if (ao->outstanding_killed_child) + LOG(DEBUG, "ao %p: .. but waiting for %d fork to exit", + ao, ao->outstanding_killed_child); libxl__ao_complete_check_progress_reports(egc, ao); } =20 @@ -1891,7 +1894,8 @@ static bool ao_work_outstanding(libxl__ao *ao) * decrement progress_reports_outstanding, and call * libxl__ao_complete_check_progress_reports. */ - return !ao->complete || ao->progress_reports_outstanding; + return !ao->complete || ao->progress_reports_outstanding + || ao->outstanding_killed_child; } =20 void libxl__ao_complete_check_progress_reports(libxl__egc *egc, libxl__ao = *ao) diff --git a/tools/libxl/libxl_fork.c b/tools/libxl/libxl_fork.c index eea3d5d4e68e..0f1b6b518c5c 100644 --- a/tools/libxl/libxl_fork.c +++ b/tools/libxl/libxl_fork.c @@ -678,6 +678,54 @@ int libxl__ev_child_xenstore_reopen(libxl__gc *gc, con= st char *what) { return rc; } =20 +typedef struct ev_child_killed { + libxl__ao *ao; + libxl__ev_child ch; +} ev_child_killed; +static void deregistered_child_callback(libxl__egc *, libxl__ev_child *, + pid_t, int status); + +void libxl__ev_child_kill_deregister(libxl__ao *ao, libxl__ev_child *ch, + int sig) +{ + AO_GC; + + if (!libxl__ev_child_inuse(ch)) + return; + + pid_t pid =3D ch->pid; + + ev_child_killed *new_ch =3D GCNEW(new_ch); + new_ch->ao =3D ao; + new_ch->ch.pid =3D pid; + new_ch->ch.callback =3D deregistered_child_callback; + LIBXL_LIST_INSERT_HEAD(&CTX->children, &new_ch->ch, entry); + ao->outstanding_killed_child++; + + LIBXL_LIST_REMOVE(ch, entry); + ch->pid =3D -1; + int r =3D kill(pid, sig); + if (r) + LOGED(ERROR, ao->domid, + "failed to kill child [%ld] with signal %d", + (unsigned long)pid, sig); +} + +static void deregistered_child_callback(libxl__egc *egc, + libxl__ev_child *ch, + pid_t pid, + int status) +{ + ev_child_killed *ck =3D CONTAINER_OF(ch, *ck, ch); + EGC_GC; + + libxl_report_child_exitstatus(CTX, XTL_ERROR, + "killed fork (dying as expected)", + pid, status); + ck->ao->outstanding_killed_child--; + libxl__ao_complete_check_progress_reports(egc, ck->ao); +} + /* * Local variables: * mode: C diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h index 6a614658c25d..4e433e110664 100644 --- a/tools/libxl/libxl_internal.h +++ b/tools/libxl/libxl_internal.h @@ -730,6 +730,7 @@ struct libxl__ao { libxl__poller *poller; uint32_t domid; LIBXL_TAILQ_ENTRY(libxl__ao) entry_for_callback; + int outstanding_killed_child; }; =20 #define LIBXL_INIT_GC(gc,ctx) do{ \ @@ -1155,9 +1156,14 @@ _hidden int libxl__ctx_evtchn_init(libxl__gc *gc); /= * for libxl_ctx_alloc */ * The parent may signal the child but it must not reap it. That will * be done by the event machinery. * - * It is not possible to "deregister" the child death event source. - * It will generate exactly one event callback; until then the childw - * is Active and may not be reused. + * The child death event will generate exactly one event callback; until + * then the childw is Active and may not be reused. + * + * libxl__ev_child_kill_deregister: Active -> Idle + * This will transfer ownership of the child process death event from + * `ch' to `ao', thus deregister the callback. + * The `ao' completion will wait until the child have been reaped by the + * event machinery. */ _hidden pid_t libxl__ev_child_fork(libxl__gc *gc, libxl__ev_child *childw_= out, libxl__ev_child_callback *death); @@ -1165,6 +1171,9 @@ static inline void libxl__ev_child_init(libxl__ev_chi= ld *childw_out) { childw_out->pid =3D -1; } static inline int libxl__ev_child_inuse(const libxl__ev_child *childw_out) { return childw_out->pid >=3D 0; } +_hidden void libxl__ev_child_kill_deregister(libxl__ao *ao, + libxl__ev_child *ch, + int sig); =20 /* Useable (only) in the child to once more make the ctx useable for * xenstore operations. logs failure in the form "what: