From nobody Mon Feb 9 17:06:23 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=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1621889010; cv=none; d=zohomail.com; s=zohoarc; b=oG3/PnsJB6Z3K4qKvQvSPnlSMHQ21Ys3ORy32q7kV+0X1+zHp4QIfN8gy9ZH6G+u9i40wgdPBMavYcSoyRiUAV2B6kF/eBWJR1u4Ihqm6hD0DPmN68zjWLN/y2IYiOeBKeBPmI/yeK68V8r3hDrCgAc8pR3JUb8AUcfpEoVGydc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1621889010; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=qZQxVcviBuvDjUeKOepu8YHqa+eGuG2DXDt5FNQL7LQ=; b=NuBXWKtLkU0E8q9WZmKufR9h9RraPRl/XJQpYTX8TljKBnQPGGe+dzX4WO2X2tBHpwSrdm+9/ovlrt/4SAg89UAccsLWlVzzWpy79aV11AAjGx5OF/qjGOw23AlGFFwcXRkqbA/y4hS0LfTV1rhNgJImLi+byMgVKXrC78PBwn8= 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=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 1621889010267238.1746128203058; Mon, 24 May 2021 13:43:30 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.131941.246369 (Exim 4.92) (envelope-from ) id 1llHPt-0000Gx-Rv; Mon, 24 May 2021 20:43:13 +0000 Received: by outflank-mailman (output) from mailman id 131941.246369; Mon, 24 May 2021 20:43:13 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1llHPt-0000Gj-Oc; Mon, 24 May 2021 20:43:13 +0000 Received: by outflank-mailman (input) for mailman id 131941; Mon, 24 May 2021 20:43:13 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1llHLU-0001ey-TW for xen-devel@lists.xenproject.org; Mon, 24 May 2021 20:38:40 +0000 Received: from mail-qk1-x729.google.com (unknown [2607:f8b0:4864:20::729]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 27c6af09-440c-442f-9373-fb53e860d068; Mon, 24 May 2021 20:37:52 +0000 (UTC) Received: by mail-qk1-x729.google.com with SMTP id h20so12792625qko.11 for ; Mon, 24 May 2021 13:37:52 -0700 (PDT) Received: from localhost.localdomain (c-73-89-138-5.hsd1.vt.comcast.net. [73.89.138.5]) by smtp.gmail.com with ESMTPSA id t25sm5142847qkt.62.2021.05.24.13.37.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 May 2021 13:37:51 -0700 (PDT) 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: 27c6af09-440c-442f-9373-fb53e860d068 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=qZQxVcviBuvDjUeKOepu8YHqa+eGuG2DXDt5FNQL7LQ=; b=G+pyi2ApCHcfc8UBweruANC8e0cOXX1CT03CKkAiDQRJHE7Klfn/pOpBYStmWWcyA4 AaYspo8J8GV+nlWKzUOviIvEb+K/L3KaHitNBCX35AfW8kdilbhDTj2Ih+mtyOfGM8Yr F6nBzUQijOg+DmNcRbgoOg96MmWJ0EdBHAWkGpHLkVJTUYcMR6XCap7KSN2lpvXCeSkh I2wZq1GZ/PRiZQNe4/3wLXlfK7NgQOSFqaxwGopBrV+57r0/qvF4ZpsUEW+By46BlXxT dAIWqnDwKUkL/ZBMot4k6a94qk4V1uIM83QIstPfIjkNVGN8MYd8rASPEAQCNcKJUt1r LxWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:in-reply-to:references; bh=qZQxVcviBuvDjUeKOepu8YHqa+eGuG2DXDt5FNQL7LQ=; b=lGYwDTmUVlw67BsPd9DybsYtrfI6U/n3xuFEsVsk5K9vgCoakI2stb8Zm/MFLnumYm qoeC0Y3y2SYn0tOGLNGvH/cPAuDVZF28ahnow891HhbK9nugTbT0CDtqKZhQPnWO4QlV Vk3O4ZKi9cJCqiEyr0nmLbCyj2ZOeInqegJriE+zK+0CYuTVnjFCh4eJXrxK8DiRCari HWyt0NnmXfY/PQYc+OItG3qB+EnULSAwqsZ5w0NdLsF3sC1Zj9AwUg0P9cLACjy+UyY8 onHXzo2N/OFIIVLUbXiiEVzHMvBz5uZ3tmWBw5ehtMtoYhU2vBGDZhmbulH2NrlHvd2U +q7w== X-Gm-Message-State: AOAM533EWehlpskrWWNCsvTzncjw+JjY+PlmMJzT7x04PvccDBnprQjq 9cCJJfTewej3h3xTVVhPtu4= X-Google-Smtp-Source: ABdhPJxK46rUoHOq90Ayw4jb8Va6QGKRHd2hRegjODp3MiUcX7+YcJZ+SyslYYQ7cj5J7aomMpbqPw== X-Received: by 2002:a05:620a:1252:: with SMTP id a18mr30362134qkl.416.1621888672226; Mon, 24 May 2021 13:37:52 -0700 (PDT) From: Nick Rosbrook X-Google-Original-From: Nick Rosbrook To: xen-devel@lists.xenproject.prg, xen-devel@lists.xenproject.org Cc: Nick Rosbrook , George Dunlap , Ian Jackson , Wei Liu Subject: [RESEND PATCH 12/12] golang/xenlight: add NotifyDomainDeath method to Context Date: Mon, 24 May 2021 16:36:53 -0400 Message-Id: X-Mailer: git-send-email 2.17.1 In-Reply-To: References: In-Reply-To: References: X-ZohoMail-DKIM: pass (identity @gmail.com) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Add a helper function to wait for domain death events, and then write the events to a provided channel. This handles the enabling/disabling of the event type, freeing the event, and converting it to a Go type. The caller can then handle the event however they need to. This function will run until a provided context.Context is cancelled. NotifyDomainDeath spawns two goroutines that return when the context.Context is done. The first will make sure that the domain death event is disabled, and that the corresponding event queue is cleared. The second calls libxl_event_wait, and writes the event to the provided channel. With this, callers should be able to manage a full domain life cycle. Add to the comment of DomainCreateNew so that package uses know they should use this method in conjunction with DomainCreateNew. Signed-off-by: Nick Rosbrook --- tools/golang/xenlight/xenlight.go | 83 ++++++++++++++++++++++++++++++- 1 file changed, 82 insertions(+), 1 deletion(-) diff --git a/tools/golang/xenlight/xenlight.go b/tools/golang/xenlight/xenl= ight.go index 6fb22665cc..8406883433 100644 --- a/tools/golang/xenlight/xenlight.go +++ b/tools/golang/xenlight/xenlight.go @@ -53,6 +53,7 @@ import "C" */ =20 import ( + "context" "fmt" "os" "os/signal" @@ -1340,7 +1341,9 @@ func (ctx *Context) DeviceUsbdevRemove(domid Domid, u= sbdev *DeviceUsbdev) error return nil } =20 -// DomainCreateNew creates a new domain. +// DomainCreateNew creates a new domain. Callers of DomainCreateNew are +// responsible for handling the death of the resulting domain. This should= be +// done using NotifyDomainDeath. func (ctx *Context) DomainCreateNew(config *DomainConfig) (Domid, error) { var cdomid C.uint32_t var cconfig C.libxl_domain_config @@ -1358,6 +1361,84 @@ func (ctx *Context) DomainCreateNew(config *DomainCo= nfig) (Domid, error) { return Domid(cdomid), nil } =20 +// NotifyDomainDeath registers an event handler for domain death events fo= r a +// given domnid, and writes events received to ec. NotifyDomainDeath retur= ns an +// error if it cannot register the event handler, but other errors encount= ered +// are just logged. The goroutine spawned by calling NotifyDomainDeath runs +// until the provided context.Context's Done channel is closed. +func (ctx *Context) NotifyDomainDeath(c context.Context, domid Domid, ec c= han<- Event) error { + var deathw *C.libxl_evgen_domain_death + + ret :=3D C.libxl_evenable_domain_death(ctx.ctx, C.uint32_t(domid), 0, &de= athw) + if ret !=3D 0 { + return Error(ret) + } + + // Spawn a goroutine that is responsible for cleaning up when the + // passed context.Context's Done channel is closed. + go func() { + <-c.Done() + + ctx.logd("cleaning up domain death event handler for domain %d", domid) + + // Disable the event generation. + C.libxl_evdisable_domain_death(ctx.ctx, deathw) + + // Make sure any events that were generated get cleaned up so they + // do not linger in the libxl event queue. + var evc *C.libxl_event + for { + ret :=3D C.libxl_event_check(ctx.ctx, &evc, C.LIBXL_EVENTMASK_ALL, nil,= nil) + if ret !=3D 0 { + return + } + C.libxl_event_free(ctx.ctx, evc) + } + }() + + go func() { + var ( + ev Event + evc *C.libxl_event + ) + + for { + select { + case <-c.Done(): + return + default: + // Go on and check for an event... + } + + ret :=3D C.libxl_event_wait(ctx.ctx, &evc, C.LIBXL_EVENTMASK_ALL, nil, = nil) + if ret !=3D 0 { + ctx.logw("unexpected error waiting for event: %s", Error(ret)) + continue + } + + // Try to convert the event to Go, and then free the + // C.libxl_event no matter what. + err :=3D ev.fromC(evc) + C.libxl_event_free(ctx.ctx, evc) + if err !=3D nil { + ctx.logw("error converting event from C: %v", err) + continue + } + + ctx.logd("received domain death event (domid=3D%v, type=3D%v)", ev.Domi= d, ev.Type) + + // Write the event to the channel + select { + case ec <- ev: + case <-c.Done(): + return + } + } + }() + + return nil +} + // DomainDestroy destroys a domain given a domid. func (ctx *Context) DomainDestroy(domid Domid) error { ret :=3D C.libxl_domain_destroy(ctx.ctx, C.uint32_t(domid), nil) --=20 2.17.1