From nobody Wed Jan 8 01:53:30 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 1734434235577279.5951670871236; Tue, 17 Dec 2024 03:17:15 -0800 (PST) Received: by lists.libvirt.org (Postfix, from userid 996) id 7235315B5; Tue, 17 Dec 2024 06:17:14 -0500 (EST) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id 5BC6615EB; Tue, 17 Dec 2024 06:15:34 -0500 (EST) Received: by lists.libvirt.org (Postfix, from userid 996) id E63F01559; Tue, 17 Dec 2024 06:15:29 -0500 (EST) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.libvirt.org (Postfix) with ESMTPS id A80DF15D8 for ; Tue, 17 Dec 2024 06:15:18 -0500 (EST) Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-454-kKyqhYCPNEeokzAPY_6uvQ-1; Tue, 17 Dec 2024 06:15:14 -0500 Received: from mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.40]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id DB77A1955F41 for ; Tue, 17 Dec 2024 11:15:13 +0000 (UTC) Received: from toolbox.redhat.com (unknown [10.42.28.136]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id B728019560AD; Tue, 17 Dec 2024 11:15:12 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-1.7 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED,SPF_HELO_NONE autolearn=unavailable autolearn_force=no version=3.4.4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1734434118; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=k4aQ0xan46bIko1X9TNEqsCkXTIrxvvChDBdGFkYgHo=; b=bMsHu97KGAvDGqdHJFFn/0Wwki9YpyYdNXKkoEbs8nvsOLltil8Gp97bAiznFkPxR63nAE c4hmMZtQQTSUIWNn3usDsqIIrWHnRp6ZxOTXiIR+YhXajBCNlQrY/lLGqzUlxfQ+sVR3eq 71reQT+sRNz3GxZGYjAxX9Ysc2S53D4= X-MC-Unique: kKyqhYCPNEeokzAPY_6uvQ-1 X-Mimecast-MFC-AGG-ID: kKyqhYCPNEeokzAPY_6uvQ From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: devel@lists.libvirt.org Cc: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= Subject: [PATCH 1/3] util: introduce object for holding a system inhibitor lock Date: Tue, 17 Dec 2024 11:15:07 +0000 Message-ID: <20241217111509.3779007-2-berrange@redhat.com> In-Reply-To: <20241217111509.3779007-1-berrange@redhat.com> References: <20241217111509.3779007-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.40 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: QsS8oBAfvYWo1br-ZGfmMQyL7Duku6EnYdxssbH07xA_1734434114 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Message-ID-Hash: TKJUBTS2UTB4BLQI53GHSFJYOY7MS3FM X-Message-ID-Hash: TKJUBTS2UTB4BLQI53GHSFJYOY7MS3FM X-MailFrom: berrange@redhat.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-config-1; header-match-config-2; header-match-config-3; header-match-devel.lists.libvirt.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header X-Mailman-Version: 3.2.2 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1734434236800116600 Content-Type: text/plain; charset="utf-8" The system inhibitor locks are currently handled by code in the virNetDaemon class. The driver code invokes a callback provided by the daemon when it wants to start or end inhibition. When the first inhibition is started, the daemon will call out to logind to apply it system wide. This has many flaws * A single message is registered with logind regardless of what driver holds the inhibition * An inhibition of daemon shutdown can't be acquired without also inhibiting system shutdown * Config of the inhibitions cannot be tailored by the driver The new virInhibitor object addresses these: * The object directly manages an inhibition with logind privately to the driver, enabling custom messages to be set. * It is possible to acquire an inhibition locally to the daemon without forwarding it to logind. Signed-off-by: Daniel P. Berrang=C3=A9 Reviewed-by: Michal Privoznik --- po/POTFILES | 1 + src/libvirt_private.syms | 7 ++ src/util/meson.build | 1 + src/util/virinhibitor.c | 214 +++++++++++++++++++++++++++++++++++++++ src/util/virinhibitor.h | 58 +++++++++++ 5 files changed, 281 insertions(+) create mode 100644 src/util/virinhibitor.c create mode 100644 src/util/virinhibitor.h diff --git a/po/POTFILES b/po/POTFILES index 3514aa3dca..c71e439fe3 100644 --- a/po/POTFILES +++ b/po/POTFILES @@ -293,6 +293,7 @@ src/util/virhostcpu.c src/util/virhostmem.c src/util/virhostuptime.c src/util/viridentity.c +src/util/virinhibitor.c src/util/virinitctl.c src/util/viriscsi.c src/util/virjson.c diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index c931003fad..adc3e3064f 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2608,6 +2608,13 @@ virIdentitySetUserName; virIdentitySetX509DName; =20 =20 +# util/virinhibitor.h +virInhibitorFree; +virInhibitorHold; +virInhibitorNew; +virInhibitorRelease; + + # util/virinitctl.h virInitctlFifos; virInitctlSetRunLevel; diff --git a/src/util/meson.build b/src/util/meson.build index 30f71b0227..69ef49139a 100644 --- a/src/util/meson.build +++ b/src/util/meson.build @@ -45,6 +45,7 @@ util_sources =3D [ 'virhostmem.c', 'virhostuptime.c', 'viridentity.c', + 'virinhibitor.c', 'virinitctl.c', 'viriscsi.c', 'virjson.c', diff --git a/src/util/virinhibitor.c b/src/util/virinhibitor.c new file mode 100644 index 0000000000..647bdc9fbb --- /dev/null +++ b/src/util/virinhibitor.c @@ -0,0 +1,214 @@ +/* + * virinhibitor.c: helper APIs for inhibiting host actions + * + * Copyright (C) 2024 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * . + */ + +#include + +#include "virinhibitor.h" +#include "virgdbus.h" +#include "virsystemd.h" +#include "virfile.h" +#include "virlog.h" +#include "virenum.h" + +#define VIR_FROM_THIS VIR_FROM_NONE + +VIR_LOG_INIT("util.inhibitor"); + +struct _virInhibitor { + GMutex lock; + size_t count; + int fd; + + char *what; + char *who; + char *why; + const char *mode; + + virInhibitorAction action; + void *actionData; +}; + +VIR_ENUM_DECL(virInhibitorMode); + +VIR_ENUM_IMPL(virInhibitorMode, + VIR_INHIBITOR_MODE_LAST, + "block", "delay"); + +#ifdef G_OS_UNIX +/* As per: https://www.freedesktop.org/wiki/Software/systemd/inhibit */ +static int +virInhibitorAcquire(const char *what, + const char *who, + const char *why, + const char *mode, + int *inhibitorFD) +{ + g_autoptr(GVariant) reply =3D NULL; + g_autoptr(GUnixFDList) replyFD =3D NULL; + g_autoptr(GVariant) message =3D NULL; + GDBusConnection *systemBus; + int fd; + int rc; + + VIR_DEBUG("what=3D%s who=3D%s why=3D%s mode=3D%s", + NULLSTR(what), NULLSTR(who), NULLSTR(why), NULLSTR(mode)); + + if (!(systemBus =3D virGDBusGetSystemBus())) { + VIR_DEBUG("system dbus not available, skipping system inhibitor"); + return 0; + } + + if (virSystemdHasLogind() < 0) { + VIR_DEBUG("logind not available, skipping system inhibitor"); + return 0; + } + + message =3D g_variant_new("(ssss)", what, who, why, mode); + + rc =3D virGDBusCallMethodWithFD(systemBus, + &reply, + G_VARIANT_TYPE("(h)"), + &replyFD, + NULL, + "org.freedesktop.login1", + "/org/freedesktop/login1", + "org.freedesktop.login1.Manager", + "Inhibit", + message, + NULL); + + if (rc < 0) + return -1; + + if (g_unix_fd_list_get_length(replyFD) <=3D 0) { + VIR_DEBUG("Missing inhibitor FD in logind reply"); + return -1; + } + + fd =3D g_unix_fd_list_get(replyFD, 0, NULL); + if (fd < 0) { + VIR_DEBUG("Unable to get inhibitor FD from logind reply"); + return -1; + } + + *inhibitorFD =3D fd; + VIR_DEBUG("Got inhibitor FD %d", fd); + return 0; +} +#endif + + +static char * +virInhibitorWhatFormat(virInhibitorWhat what) +{ + const char *whatstr[] =3D { + "sleep", + "shutdown", + "idle", + "handle-power-key", + "handle-suspend-key", + "handle-hibernate-key", + "handle-lid-switch", + }; + GString *str =3D g_string_new(""); + size_t i; + + for (i =3D 0; i < G_N_ELEMENTS(whatstr); i++) { + if (what & (1 << i)) { + if (str->len) + g_string_append(str, ":"); + g_string_append(str, whatstr[i]); + } + } + + return g_string_free(str, FALSE); +} + + +virInhibitor *virInhibitorNew(virInhibitorWhat what, + const char *who, + const char *why, + virInhibitorMode mode, + virInhibitorAction action, + void *actionData) +{ + virInhibitor *inhibitor =3D g_new0(virInhibitor, 1); + + inhibitor->fd =3D -1; + inhibitor->what =3D virInhibitorWhatFormat(what); + inhibitor->who =3D g_strdup(who); + inhibitor->why =3D g_strdup(why); + inhibitor->mode =3D virInhibitorModeTypeToString(mode); + inhibitor->action =3D action; + inhibitor->actionData =3D actionData; + + return inhibitor; +} + +void virInhibitorHold(virInhibitor *inhibitor) +{ + g_mutex_lock(&inhibitor->lock); + + if (inhibitor->count =3D=3D 0) { + if (inhibitor->action) { + inhibitor->action(true, inhibitor->actionData); + } +#ifdef G_OS_UNIX + if (virInhibitorAcquire( + inhibitor->what, inhibitor->who, inhibitor->why, + inhibitor->mode, &inhibitor->fd) < 0) { + VIR_ERROR(_("Failed to acquire inhibitor: %1$s"), + virGetLastErrorMessage()); + virResetLastError(); + } +#else + VIR_DEBUG("No inhibitor implementation on non-UNIX platforms"); +#endif + } + inhibitor->count++; + g_mutex_unlock(&inhibitor->lock); +} + + +void virInhibitorRelease(virInhibitor *inhibitor) +{ + g_mutex_lock(&inhibitor->lock); + inhibitor->count--; + if (inhibitor->count =3D=3D 0) { + VIR_FORCE_CLOSE(inhibitor->fd); + if (inhibitor->action) { + inhibitor->action(false, inhibitor->actionData); + } + } + g_mutex_unlock(&inhibitor->lock); +} + + +void virInhibitorFree(virInhibitor *inhibitor) +{ + if (!inhibitor) + return; + + g_free(inhibitor->what); + g_free(inhibitor->who); + g_free(inhibitor->why); + VIR_FORCE_CLOSE(inhibitor->fd); + g_free(inhibitor); +} diff --git a/src/util/virinhibitor.h b/src/util/virinhibitor.h new file mode 100644 index 0000000000..0a1c445d41 --- /dev/null +++ b/src/util/virinhibitor.h @@ -0,0 +1,58 @@ +/* + * virinhibitor.h: helper APIs for inhibiting host actions + * + * Copyright (C) 2024 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * . + */ + +#pragma once + +#include "internal.h" + +typedef struct _virInhibitor virInhibitor; + +typedef enum { + VIR_INHIBITOR_WHAT_NONE =3D 0, + VIR_INHIBITOR_WHAT_SLEEP =3D (1 << 1), + VIR_INHIBITOR_WHAT_SHUTDOWN =3D (1 << 2), + VIR_INHIBITOR_WHAT_IDLE =3D (1 << 3), + VIR_INHIBITOR_WHAT_POWER_KEY =3D (1 << 4), + VIR_INHIBITOR_WHAT_SUSPEND_KEY =3D (1 << 5), + VIR_INHIBITOR_WHAT_HIBERNATE_KEY =3D (1 << 6), + VIR_INHIBITOR_WHAT_LID_SWITCH =3D (1 << 7), +} virInhibitorWhat; + +typedef enum { + VIR_INHIBITOR_MODE_BLOCK, + VIR_INHIBITOR_MODE_DELAY, + + VIR_INHIBITOR_MODE_LAST +} virInhibitorMode; + +typedef void (*virInhibitorAction)(bool inhibited, + void *opaque); + +virInhibitor *virInhibitorNew(virInhibitorWhat what, + const char *who, + const char *why, + virInhibitorMode mode, + virInhibitorAction action, + void *actionData); + +void virInhibitorHold(virInhibitor *inhibitor); +void virInhibitorRelease(virInhibitor *inhibitor); + +void virInhibitorFree(virInhibitor *inhibitor); --=20 2.46.0 From nobody Wed Jan 8 01:53:30 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 1734434186270143.7396410154546; Tue, 17 Dec 2024 03:16:26 -0800 (PST) Received: by lists.libvirt.org (Postfix, from userid 996) id 31A5A1568; Tue, 17 Dec 2024 06:16:25 -0500 (EST) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id 293F015D7; Tue, 17 Dec 2024 06:15:30 -0500 (EST) Received: by lists.libvirt.org (Postfix, from userid 996) id 0262015C4; Tue, 17 Dec 2024 06:15:25 -0500 (EST) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.libvirt.org (Postfix) with ESMTPS id 0AC4D15B5 for ; Tue, 17 Dec 2024 06:15:18 -0500 (EST) Received: from mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-592-zH2omLaKPySqP0VKQmCR3A-1; Tue, 17 Dec 2024 06:15:16 -0500 Received: from mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.40]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id B4DF619560B2 for ; Tue, 17 Dec 2024 11:15:15 +0000 (UTC) Received: from toolbox.redhat.com (unknown [10.42.28.136]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 59EBF19560AD; Tue, 17 Dec 2024 11:15:14 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-1.7 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED,SPF_HELO_NONE autolearn=unavailable autolearn_force=no version=3.4.4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1734434117; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=vs/zr3RteomrGtvQexRaX+wgIeqSfO+HgrP6xvuSnkU=; b=CNFzfj7zD1DFHoNuNyRX4fCD8GZ7HHGQ21IzAqJ4F3I6U3xPNGuTJ84861nFarBo8JHs5N wpKaQc+0+c5oMMzGUtK1JXLn0m9uzJvWdb7LvoZ0yydK+o29M5OGJuugGmkvQeb+0FQTff IHOgbDnihpesqJ6cOe41mxB1NSBk8wM= X-MC-Unique: zH2omLaKPySqP0VKQmCR3A-1 X-Mimecast-MFC-AGG-ID: zH2omLaKPySqP0VKQmCR3A From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: devel@lists.libvirt.org Cc: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= Subject: [PATCH 2/3] src: convert drivers over to new virInhibitor APIs Date: Tue, 17 Dec 2024 11:15:08 +0000 Message-ID: <20241217111509.3779007-3-berrange@redhat.com> In-Reply-To: <20241217111509.3779007-1-berrange@redhat.com> References: <20241217111509.3779007-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.40 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: -2l3e7tamBkTPwDeY47D6oZjE9MlaBR6lQFa8uDRb1c_1734434115 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Message-ID-Hash: NXSWJCPECD7A3LF4SR3DBDX2RB7M7XUJ X-Message-ID-Hash: NXSWJCPECD7A3LF4SR3DBDX2RB7M7XUJ X-MailFrom: berrange@redhat.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-config-1; header-match-config-2; header-match-config-3; header-match-devel.lists.libvirt.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header X-Mailman-Version: 3.2.2 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1734434188731116600 Content-Type: text/plain; charset="utf-8" This initial conversion of the drivers switches them over to use the virInhibitor APIs in local daemon only mode. Communication to logind is still handled by the virNetDaemon class logic. This mostly just replaces upto 3 fields in the driver state with a single new virInhibitor object, but otherwise should not change functionality besides replacing atomics with mutex protected APIs. The exception is the LXC driver which has been trying to inhibit shutdown shutdown but silently failing to, since nothing ever remembered to set the 'inhibitCallback' pointer in the driver state struct. Signed-off-by: Daniel P. Berrang=C3=A9 Reviewed-by: Michal Privoznik --- src/libxl/libxl_conf.h | 9 +++---- src/libxl/libxl_domain.c | 6 ++--- src/libxl/libxl_driver.c | 15 +++++++---- src/lxc/lxc_conf.h | 9 +++---- src/lxc/lxc_driver.c | 13 +++++++-- src/lxc/lxc_process.c | 9 +++---- src/network/bridge_driver.c | 20 +++++++------- src/network/bridge_driver_conf.h | 9 +++---- src/qemu/qemu_conf.h | 9 +++---- src/qemu/qemu_driver.c | 12 ++++++--- src/qemu/qemu_process.c | 9 +++---- src/secret/secret_driver.c | 46 +++++++++++++------------------- 12 files changed, 80 insertions(+), 86 deletions(-) diff --git a/src/libxl/libxl_conf.h b/src/libxl/libxl_conf.h index 7087b41079..0edcde079d 100644 --- a/src/libxl/libxl_conf.h +++ b/src/libxl/libxl_conf.h @@ -35,6 +35,7 @@ #include "virfirmware.h" #include "libxl_capabilities.h" #include "libxl_logger.h" +#include "virinhibitor.h" =20 #define LIBXL_DRIVER_EXTERNAL_NAME "Xen" /* @@ -117,12 +118,8 @@ struct _libxlDriverPrivate { /* pid file FD, ensures two copies of the driver can't use the same ro= ot */ int lockFD; =20 - /* Atomic inc/dec only */ - unsigned int nactive; - - /* Immutable pointers. Caller must provide locking */ - virStateInhibitCallback inhibitCallback; - void *inhibitOpaque; + /* Immutable pointer, self-locking APIs */ + virInhibitor *inhibitor; =20 /* Immutable pointer, self-locking APIs */ virDomainObjList *domains; diff --git a/src/libxl/libxl_domain.c b/src/libxl/libxl_domain.c index cad6c9ce42..a049cdb30f 100644 --- a/src/libxl/libxl_domain.c +++ b/src/libxl/libxl_domain.c @@ -873,8 +873,7 @@ libxlDomainCleanup(libxlDriverPrivate *driver, priv->deathW =3D NULL; } =20 - if (g_atomic_int_dec_and_test(&driver->nactive) && driver->inhibitCall= back) - driver->inhibitCallback(false, driver->inhibitOpaque); + virInhibitorRelease(driver->inhibitor); =20 /* Release auto-allocated graphics ports */ for (i =3D 0; i < vm->def->ngraphics; i++) { @@ -1421,8 +1420,7 @@ libxlDomainStart(libxlDriverPrivate *driver, return -1; } =20 - if (g_atomic_int_add(&driver->nactive, 1) =3D=3D 0 && driver->inhibitC= allback) - driver->inhibitCallback(true, driver->inhibitOpaque); + virInhibitorHold(driver->inhibitor); =20 event =3D virDomainEventLifecycleNewFromObj(vm, VIR_DOMAIN_EVENT_START= ED, restore_fd < 0 ? diff --git a/src/libxl/libxl_driver.c b/src/libxl/libxl_driver.c index e72553603d..eb293172f7 100644 --- a/src/libxl/libxl_driver.c +++ b/src/libxl/libxl_driver.c @@ -437,8 +437,7 @@ libxlReconnectDomain(virDomainObj *vm, virDomainObjSetState(vm, VIR_DOMAIN_RUNNING, VIR_DOMAIN_RUNNING_UNKNOWN); =20 - if (g_atomic_int_add(&driver->nactive, 1) =3D=3D 0 && driver->inhibitC= allback) - driver->inhibitCallback(true, driver->inhibitOpaque); + virInhibitorHold(driver->inhibitor); =20 /* Enable domain death events */ libxl_evenable_domain_death(cfg->ctx, vm->def->id, 0, &priv->deathW); @@ -514,6 +513,7 @@ libxlStateCleanup(void) =20 virObjectUnref(libxl_driver->domainEventState); virSysinfoDefFree(libxl_driver->hostsysinfo); + virInhibitorFree(libxl_driver->inhibitor); =20 if (libxl_driver->lockFD !=3D -1) virPidFileRelease(libxl_driver->config->stateDir, "driver", libxl_= driver->lockFD); @@ -675,9 +675,6 @@ libxlStateInitialize(bool privileged, return VIR_DRV_STATE_INIT_ERROR; } =20 - libxl_driver->inhibitCallback =3D callback; - libxl_driver->inhibitOpaque =3D opaque; - /* Allocate bitmap for vnc port reservation */ if (!(libxl_driver->reservedGraphicsPorts =3D virPortAllocatorRangeNew(_("VNC"), @@ -709,6 +706,14 @@ libxlStateInitialize(bool privileged, if (libxlDriverConfigLoadFile(cfg, driverConf) < 0) goto error; =20 + libxl_driver->inhibitor =3D virInhibitorNew( + VIR_INHIBITOR_WHAT_NONE, + _("Libvirt Xen"), + _("Xen virtual machines are running"), + VIR_INHIBITOR_MODE_DELAY, + callback, + opaque); + /* Register the callbacks providing access to libvirt's event loop */ libxl_osevent_register_hooks(cfg->ctx, &libxl_osevent_callbacks, cfg->= ctx); =20 diff --git a/src/lxc/lxc_conf.h b/src/lxc/lxc_conf.h index a639e3989f..1969ed74cb 100644 --- a/src/lxc/lxc_conf.h +++ b/src/lxc/lxc_conf.h @@ -30,6 +30,7 @@ #include "virsysinfo.h" #include "virclosecallbacks.h" #include "virhostdev.h" +#include "virinhibitor.h" =20 #define LXC_DRIVER_NAME "LXC" =20 @@ -75,12 +76,8 @@ struct _virLXCDriver { /* Immutable pointer, lockless APIs */ virSysinfoDef *hostsysinfo; =20 - /* Atomic inc/dec only */ - unsigned int nactive; - - /* Immutable pointers. Caller must provide locking */ - virStateInhibitCallback inhibitCallback; - void *inhibitOpaque; + /* Immutable pointer, self-locking APIs */ + virInhibitor *inhibitor; =20 /* Immutable pointer, self-locking APIs */ virDomainObjList *domains; diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c index 2488940feb..08516c8297 100644 --- a/src/lxc/lxc_driver.c +++ b/src/lxc/lxc_driver.c @@ -1398,8 +1398,8 @@ static virDrvStateInitResult lxcStateInitialize(bool privileged, const char *root, bool monolithic G_GNUC_UNUSED, - virStateInhibitCallback callback G_GNUC_UNUSED, - void *opaque G_GNUC_UNUSED) + virStateInhibitCallback callback, + void *opaque) { virLXCDriverConfig *cfg =3D NULL; bool autostart =3D true; @@ -1451,6 +1451,14 @@ lxcStateInitialize(bool privileged, if (virLXCLoadDriverConfig(cfg, SYSCONFDIR "/libvirt/lxc.conf") < 0) goto cleanup; =20 + lxc_driver->inhibitor =3D virInhibitorNew( + VIR_INHIBITOR_WHAT_NONE, + _("Libvirt LXC"), + _("LXC containers are running"), + VIR_INHIBITOR_MODE_DELAY, + callback, + opaque); + if (!(lxc_driver->securityManager =3D lxcSecurityInit(cfg))) goto cleanup; =20 @@ -1555,6 +1563,7 @@ static int lxcStateCleanup(void) virObjectUnref(lxc_driver->caps); virObjectUnref(lxc_driver->securityManager); virObjectUnref(lxc_driver->xmlopt); + virInhibitorFree(lxc_driver->inhibitor); =20 if (lxc_driver->lockFD !=3D -1) virPidFileRelease(lxc_driver->config->stateDir, "driver", lxc_driv= er->lockFD); diff --git a/src/lxc/lxc_process.c b/src/lxc/lxc_process.c index cd8bcfc282..c2982244f0 100644 --- a/src/lxc/lxc_process.c +++ b/src/lxc/lxc_process.c @@ -203,8 +203,7 @@ static void virLXCProcessCleanup(virLXCDriver *driver, vm->pid =3D 0; vm->def->id =3D -1; =20 - if (g_atomic_int_dec_and_test(&driver->nactive) && driver->inhibitCall= back) - driver->inhibitCallback(false, driver->inhibitOpaque); + virInhibitorRelease(driver->inhibitor); =20 virLXCDomainReAttachHostDevices(driver, vm->def); =20 @@ -1466,8 +1465,7 @@ int virLXCProcessStart(virLXCDriver * driver, if (virCommandHandshakeNotify(cmd) < 0) goto cleanup; =20 - if (g_atomic_int_add(&driver->nactive, 1) =3D=3D 0 && driver->inhibitC= allback) - driver->inhibitCallback(true, driver->inhibitOpaque); + virInhibitorHold(driver->inhibitor); =20 /* The first synchronization point is when the controller creates CGro= ups. */ if (lxcContainerWaitForContinue(handshakefds[0]) < 0) { @@ -1665,8 +1663,7 @@ virLXCProcessReconnectDomain(virDomainObj *vm, virDomainObjSetState(vm, VIR_DOMAIN_RUNNING, VIR_DOMAIN_RUNNING_UNKNOWN); =20 - if (g_atomic_int_add(&driver->nactive, 1) =3D=3D 0 && driver->inhi= bitCallback) - driver->inhibitCallback(true, driver->inhibitOpaque); + virInhibitorHold(driver->inhibitor); =20 if (!(priv->monitor =3D virLXCProcessConnectMonitor(driver, vm))) goto error; diff --git a/src/network/bridge_driver.c b/src/network/bridge_driver.c index e700a614a9..ce793c12ef 100644 --- a/src/network/bridge_driver.c +++ b/src/network/bridge_driver.c @@ -504,8 +504,7 @@ networkUpdateState(virNetworkObj *obj, if (virNetworkObjIsActive(obj)) { virNetworkObjPortForEach(obj, networkUpdatePort, obj); =20 - if (g_atomic_int_add(&driver->nactive, 1) =3D=3D 0 && driver->inhi= bitCallback) - driver->inhibitCallback(true, driver->inhibitOpaque); + virInhibitorHold(driver->inhibitor); } =20 /* Try and read dnsmasq pids of both active and inactive networks, jus= t in @@ -644,9 +643,6 @@ networkStateInitialize(bool privileged, goto error; } =20 - network_driver->inhibitCallback =3D callback; - network_driver->inhibitOpaque =3D opaque; - network_driver->privileged =3D privileged; =20 if (!(network_driver->xmlopt =3D networkDnsmasqCreateXMLConf())) @@ -655,6 +651,14 @@ networkStateInitialize(bool privileged, if (!(network_driver->config =3D cfg =3D virNetworkDriverConfigNew(pri= vileged))) goto error; =20 + network_driver->inhibitor =3D virInhibitorNew( + VIR_INHIBITOR_WHAT_NONE, + _("Libvirt Network"), + _("Virtual networks are active"), + VIR_INHIBITOR_MODE_DELAY, + callback, + opaque); + if ((network_driver->lockFD =3D virPidFileAcquire(cfg->stateDir, "driver", getpid())) < 0) goto error; @@ -2432,8 +2436,7 @@ networkStartNetwork(virNetworkDriverState *driver, obj, network_driver->xmlopt) < 0) goto cleanup; =20 - if (g_atomic_int_add(&driver->nactive, 1) =3D=3D 0 && driver->inhibitC= allback) - driver->inhibitCallback(true, driver->inhibitOpaque); + virInhibitorHold(driver->inhibitor); =20 virNetworkObjSetActive(obj, true); VIR_INFO("Network '%s' started up", def->name); @@ -2509,8 +2512,7 @@ networkShutdownNetwork(virNetworkDriverState *driver, =20 virNetworkObjSetActive(obj, false); =20 - if (g_atomic_int_dec_and_test(&driver->nactive) && driver->inhibitCall= back) - driver->inhibitCallback(false, driver->inhibitOpaque); + virInhibitorRelease(driver->inhibitor); =20 virNetworkObjUnsetDefTransient(obj); return ret; diff --git a/src/network/bridge_driver_conf.h b/src/network/bridge_driver_c= onf.h index 1beed01efb..2a2e2bc16d 100644 --- a/src/network/bridge_driver_conf.h +++ b/src/network/bridge_driver_conf.h @@ -27,6 +27,7 @@ #include "virnetworkobj.h" #include "object_event.h" #include "virfirewall.h" +#include "virinhibitor.h" =20 typedef struct _virNetworkDriverConfig virNetworkDriverConfig; struct _virNetworkDriverConfig { @@ -49,12 +50,8 @@ typedef struct _virNetworkDriverState virNetworkDriverSt= ate; struct _virNetworkDriverState { virMutex lock; =20 - /* Atomic inc/dec only */ - unsigned int nactive; - - /* Immutable pointers. Caller must provide locking */ - virStateInhibitCallback inhibitCallback; - void *inhibitOpaque; + /* Immutable pointer, self-locking APIs */ + virInhibitor *inhibitor; =20 /* Read-only */ bool privileged; diff --git a/src/qemu/qemu_conf.h b/src/qemu/qemu_conf.h index 23a900193e..42cdb6f883 100644 --- a/src/qemu/qemu_conf.h +++ b/src/qemu/qemu_conf.h @@ -42,6 +42,7 @@ #include "virfile.h" #include "virfilecache.h" #include "virfirmware.h" +#include "virinhibitor.h" =20 #define QEMU_DRIVER_NAME "QEMU" =20 @@ -257,16 +258,12 @@ struct _virQEMUDriver { /* Atomic increment only */ int lastvmid; =20 - /* Atomic inc/dec only */ - unsigned int nactive; - /* Immutable values */ bool privileged; char *embeddedRoot; =20 - /* Immutable pointers. Caller must provide locking */ - virStateInhibitCallback inhibitCallback; - void *inhibitOpaque; + /* Immutable pointer, self-locking APIs */ + virInhibitor *inhibitor; =20 /* Immutable pointer, self-locking APIs */ virDomainObjList *domains; diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index f1a633fdd3..c4cb9b2a9a 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -573,9 +573,6 @@ qemuStateInitialize(bool privileged, return VIR_DRV_STATE_INIT_ERROR; } =20 - qemu_driver->inhibitCallback =3D callback; - qemu_driver->inhibitOpaque =3D opaque; - qemu_driver->privileged =3D privileged; qemu_driver->hostarch =3D virArchFromHost(); if (root !=3D NULL) @@ -675,6 +672,14 @@ qemuStateInitialize(bool privileged, goto error; } =20 + qemu_driver->inhibitor =3D virInhibitorNew( + VIR_INHIBITOR_WHAT_NONE, + _("Libvirt QEMU"), + _("QEMU/KVM virtual machines are running"), + VIR_INHIBITOR_MODE_DELAY, + callback, + opaque); + if ((qemu_driver->lockFD =3D virPidFileAcquire(cfg->stateDir, "driver", getpid())) < 0) goto error; @@ -1065,6 +1070,7 @@ qemuStateCleanup(void) ebtablesContextFree(qemu_driver->ebtables); virObjectUnref(qemu_driver->domains); virObjectUnref(qemu_driver->nbdkitCapsCache); + virInhibitorFree(qemu_driver->inhibitor); =20 if (qemu_driver->lockFD !=3D -1) virPidFileRelease(qemu_driver->config->stateDir, "driver", qemu_dr= iver->lockFD); diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c index bee7a39e4e..c3b6f95493 100644 --- a/src/qemu/qemu_process.c +++ b/src/qemu/qemu_process.c @@ -5809,8 +5809,7 @@ qemuProcessInit(virQEMUDriver *driver, qemuDomainSetFakeReboot(vm, false); virDomainObjSetState(vm, VIR_DOMAIN_PAUSED, VIR_DOMAIN_PAUSED_STAR= TING_UP); =20 - if (g_atomic_int_add(&driver->nactive, 1) =3D=3D 0 && driver->inhi= bitCallback) - driver->inhibitCallback(true, driver->inhibitOpaque); + virInhibitorHold(driver->inhibitor); =20 /* Run an early hook to set-up missing devices */ if (qemuProcessStartHook(driver, vm, @@ -8812,8 +8811,7 @@ void qemuProcessStop(virQEMUDriver *driver, if (priv->eventThread) g_object_unref(g_steal_pointer(&priv->eventThread)); =20 - if (g_atomic_int_dec_and_test(&driver->nactive) && driver->inhibitCall= back) - driver->inhibitCallback(false, driver->inhibitOpaque); + virInhibitorRelease(driver->inhibitor); =20 /* Clear network bandwidth */ virDomainClearNetBandwidth(vm->def); @@ -9571,8 +9569,7 @@ qemuProcessReconnect(void *opaque) goto error; } =20 - if (g_atomic_int_add(&driver->nactive, 1) =3D=3D 0 && driver->inhibitC= allback) - driver->inhibitCallback(true, driver->inhibitOpaque); + virInhibitorHold(driver->inhibitor); =20 cleanup: if (jobStarted) diff --git a/src/secret/secret_driver.c b/src/secret/secret_driver.c index a2d6b879d0..04c3ca49f1 100644 --- a/src/secret/secret_driver.c +++ b/src/secret/secret_driver.c @@ -41,6 +41,7 @@ #include "viraccessapicheck.h" #include "secret_event.h" #include "virutil.h" +#include "virinhibitor.h" =20 #define VIR_FROM_THIS VIR_FROM_SECRET =20 @@ -67,9 +68,8 @@ struct _virSecretDriverState { /* Immutable pointer, self-locking APIs */ virObjectEventState *secretEventState; =20 - /* Immutable pointers. Caller must provide locking */ - virStateInhibitCallback inhibitCallback; - void *inhibitOpaque; + /* Immutable pointer, self-locking APIs */ + virInhibitor *inhibitor; }; =20 static virSecretDriverState *driver; @@ -90,23 +90,6 @@ secretObjFromSecret(virSecretPtr secret) } =20 =20 -static bool -secretNumOfEphemeralSecretsHelper(virConnectPtr conn G_GNUC_UNUSED, - virSecretDef *def) -{ - return def->isephemeral; -} - - -static int -secretNumOfEphemeralSecrets(void) -{ - return virSecretObjListNumOfSecrets(driver->secrets, - secretNumOfEphemeralSecretsHelper, - NULL); -} - - /* Driver functions */ =20 static int @@ -271,6 +254,10 @@ secretDefineXML(virConnectPtr conn, objDef->uuid, objDef->usage_type, objDef->usage_id); + + if (objDef->isephemeral) + virInhibitorHold(driver->inhibitor); + goto cleanup; =20 restore_backup: @@ -288,8 +275,6 @@ secretDefineXML(virConnectPtr conn, virSecretDefFree(def); virSecretObjEndAPI(&obj); =20 - if (secretNumOfEphemeralSecrets() > 0) - driver->inhibitCallback(true, driver->inhibitOpaque); =20 virObjectEventStateQueue(driver->secretEventState, event); =20 @@ -440,6 +425,9 @@ secretUndefine(virSecretPtr secret) VIR_SECRET_EVENT_UNDEFINED, 0); =20 + if (def->isephemeral) + virInhibitorRelease(driver->inhibitor); + virSecretObjDeleteData(obj); =20 virSecretObjListRemove(driver->secrets, obj); @@ -450,9 +438,6 @@ secretUndefine(virSecretPtr secret) cleanup: virSecretObjEndAPI(&obj); =20 - if (secretNumOfEphemeralSecrets() =3D=3D 0) - driver->inhibitCallback(false, driver->inhibitOpaque); - virObjectEventStateQueue(driver->secretEventState, event); =20 return ret; @@ -469,6 +454,7 @@ secretStateCleanupLocked(void) VIR_FREE(driver->configDir); =20 virObjectUnref(driver->secretEventState); + virInhibitorFree(driver->inhibitor); =20 if (driver->lockFD !=3D -1) virPidFileRelease(driver->stateDir, "driver", driver->lockFD); @@ -502,8 +488,6 @@ secretStateInitialize(bool privileged, driver->lockFD =3D -1; driver->secretEventState =3D virObjectEventStateNew(); driver->privileged =3D privileged; - driver->inhibitCallback =3D callback; - driver->inhibitOpaque =3D opaque; =20 if (root) { driver->embeddedRoot =3D g_strdup(root); @@ -535,6 +519,14 @@ secretStateInitialize(bool privileged, goto error; } =20 + driver->inhibitor =3D virInhibitorNew( + VIR_INHIBITOR_WHAT_NONE, + _("Libvirt Secret"), + _("Ephemeral secrets are loaded"), + VIR_INHIBITOR_MODE_DELAY, + callback, + opaque); + if ((driver->lockFD =3D virPidFileAcquire(driver->stateDir, "driver", getpid())) < 0) goto error; --=20 2.46.0 From nobody Wed Jan 8 01:53:30 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 1734434259728686.3901846719136; Tue, 17 Dec 2024 03:17:39 -0800 (PST) Received: by lists.libvirt.org (Postfix, from userid 996) id 908231603; Tue, 17 Dec 2024 06:17:38 -0500 (EST) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id 651C8160B; Tue, 17 Dec 2024 06:15:37 -0500 (EST) Received: by lists.libvirt.org (Postfix, from userid 996) id 9BCF915A0; Tue, 17 Dec 2024 06:15:30 -0500 (EST) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.libvirt.org (Postfix) with ESMTPS id 0573E15EE for ; Tue, 17 Dec 2024 06:15:19 -0500 (EST) Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-294-0g2KDKiHNJuXKOn6A0cqOQ-1; Tue, 17 Dec 2024 06:15:18 -0500 Received: from mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.40]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 49489195608C for ; Tue, 17 Dec 2024 11:15:17 +0000 (UTC) Received: from toolbox.redhat.com (unknown [10.42.28.136]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 431B419560AD; Tue, 17 Dec 2024 11:15:16 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-1.7 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED,SPF_HELO_NONE autolearn=unavailable autolearn_force=no version=3.4.4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1734434119; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=pt5QEv8ippV1Zr1aqkRrLZFHEQ8MpTG9S4nCFcJ2aGM=; b=L72XKLwGVZ5Z6v3f7ttt5wCSdw2gAO6LLHjpSg1XoqJr+WmxIKYkBTYDS6NtypuKsrH2zR Dq2WjP112U2p6bDRv0tYLVxClcMa/fCyXqy9N5ww7F0S4O9NuOg7EQuboeTHeYLCKAtcaE fsmL3q2Gqujzk3FDD2HAJO06j27ZKak= X-MC-Unique: 0g2KDKiHNJuXKOn6A0cqOQ-1 X-Mimecast-MFC-AGG-ID: 0g2KDKiHNJuXKOn6A0cqOQ From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: devel@lists.libvirt.org Cc: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= Subject: [PATCH 3/3] rpc: remove logind support for virNetDaemon Date: Tue, 17 Dec 2024 11:15:09 +0000 Message-ID: <20241217111509.3779007-4-berrange@redhat.com> In-Reply-To: <20241217111509.3779007-1-berrange@redhat.com> References: <20241217111509.3779007-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.40 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: csrl_O96uf3HCVZdsNL2_fsbSrybs-AGv1LLXesG3_U_1734434117 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Message-ID-Hash: KPIQLMRWGCUF7UFQ5F7LGVFXI2KBZDQX X-Message-ID-Hash: KPIQLMRWGCUF7UFQ5F7LGVFXI2KBZDQX X-MailFrom: berrange@redhat.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-config-1; header-match-config-2; header-match-config-3; header-match-devel.lists.libvirt.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header X-Mailman-Version: 3.2.2 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1734434260729116600 Content-Type: text/plain; charset="utf-8" The virNetDaemon code now only concerns itself with preventing auto shutdown of the local daemon. Logind is now handled by the new virInhibitor object, for QEMU, LXC and LibXL. This fixes two notable bugs * Running virtual networks would prevent system shutdown * Loaded ephemeral secrets would prevent system shutdown Fixes 9e3cc0ff5e81ed2056a6a528893fd2cb5609d70b Fixes 37800af9a400385801da6d73654249fdb51a93d8 Signed-off-by: Daniel P. Berrang=C3=A9 Reviewed-by: Michal Privoznik --- src/libxl/libxl_driver.c | 2 +- src/lxc/lxc_driver.c | 2 +- src/qemu/qemu_driver.c | 2 +- src/rpc/virnetdaemon.c | 78 ---------------------------------------- 4 files changed, 3 insertions(+), 81 deletions(-) diff --git a/src/libxl/libxl_driver.c b/src/libxl/libxl_driver.c index eb293172f7..2c0cf75562 100644 --- a/src/libxl/libxl_driver.c +++ b/src/libxl/libxl_driver.c @@ -707,7 +707,7 @@ libxlStateInitialize(bool privileged, goto error; =20 libxl_driver->inhibitor =3D virInhibitorNew( - VIR_INHIBITOR_WHAT_NONE, + VIR_INHIBITOR_WHAT_SHUTDOWN, _("Libvirt Xen"), _("Xen virtual machines are running"), VIR_INHIBITOR_MODE_DELAY, diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c index 08516c8297..cc4b2a05d6 100644 --- a/src/lxc/lxc_driver.c +++ b/src/lxc/lxc_driver.c @@ -1452,7 +1452,7 @@ lxcStateInitialize(bool privileged, goto cleanup; =20 lxc_driver->inhibitor =3D virInhibitorNew( - VIR_INHIBITOR_WHAT_NONE, + VIR_INHIBITOR_WHAT_SHUTDOWN, _("Libvirt LXC"), _("LXC containers are running"), VIR_INHIBITOR_MODE_DELAY, diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index c4cb9b2a9a..95a0e3a170 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -673,7 +673,7 @@ qemuStateInitialize(bool privileged, } =20 qemu_driver->inhibitor =3D virInhibitorNew( - VIR_INHIBITOR_WHAT_NONE, + VIR_INHIBITOR_WHAT_SHUTDOWN, _("Libvirt QEMU"), _("QEMU/KVM virtual machines are running"), VIR_INHIBITOR_MODE_DELAY, diff --git a/src/rpc/virnetdaemon.c b/src/rpc/virnetdaemon.c index 9795418126..e4c6261536 100644 --- a/src/rpc/virnetdaemon.c +++ b/src/rpc/virnetdaemon.c @@ -31,7 +31,6 @@ #include "virutil.h" #include "virfile.h" #include "virnetserver.h" -#include "virgdbus.h" #include "virhash.h" #include "virprocess.h" #include "virsystemd.h" @@ -80,7 +79,6 @@ struct _virNetDaemon { int autoShutdownTimerID; bool autoShutdownTimerActive; size_t autoShutdownInhibitions; - int autoShutdownInhibitFd; }; =20 =20 @@ -109,7 +107,6 @@ virNetDaemonDispose(void *obj) virEventRemoveHandle(dmn->sigwatch); #endif /* !WIN32 */ =20 - VIR_FORCE_CLOSE(dmn->autoShutdownInhibitFd); g_free(dmn->stateStopThread); =20 g_clear_pointer(&dmn->servers, g_hash_table_unref); @@ -150,7 +147,6 @@ virNetDaemonNew(void) #endif /* !WIN32 */ =20 dmn->privileged =3D geteuid() =3D=3D 0; - dmn->autoShutdownInhibitFd =3D -1; =20 virProcessActivateMaxFiles(); =20 @@ -491,66 +487,6 @@ virNetDaemonAutoShutdown(virNetDaemon *dmn, } =20 =20 -#ifdef G_OS_UNIX -/* As per: https://www.freedesktop.org/wiki/Software/systemd/inhibit */ -static void -virNetDaemonCallInhibit(virNetDaemon *dmn, - const char *what, - const char *who, - const char *why, - const char *mode) -{ - g_autoptr(GVariant) reply =3D NULL; - g_autoptr(GUnixFDList) replyFD =3D NULL; - g_autoptr(GVariant) message =3D NULL; - GDBusConnection *systemBus; - int fd; - int rc; - - VIR_DEBUG("dmn=3D%p what=3D%s who=3D%s why=3D%s mode=3D%s", - dmn, NULLSTR(what), NULLSTR(who), NULLSTR(why), NULLSTR(mode= )); - - if (virSystemdHasLogind() < 0) - return; - - if (!(systemBus =3D virGDBusGetSystemBus())) - return; - - message =3D g_variant_new("(ssss)", what, who, why, mode); - - rc =3D virGDBusCallMethodWithFD(systemBus, - &reply, - G_VARIANT_TYPE("(h)"), - &replyFD, - NULL, - "org.freedesktop.login1", - "/org/freedesktop/login1", - "org.freedesktop.login1.Manager", - "Inhibit", - message, - NULL); - - if (rc < 0) - return; - - if (g_unix_fd_list_get_length(replyFD) <=3D 0) - return; - - fd =3D g_unix_fd_list_get(replyFD, 0, NULL); - if (fd < 0) - return; - - if (dmn->autoShutdownInhibitions) { - dmn->autoShutdownInhibitFd =3D fd; - VIR_DEBUG("Got inhibit FD %d", fd); - } else { - /* We stopped the last VM since we made the inhibit call */ - VIR_DEBUG("Closing inhibit FD %d", fd); - VIR_FORCE_CLOSE(fd); - } -} -#endif - void virNetDaemonAddShutdownInhibition(virNetDaemon *dmn) { @@ -559,15 +495,6 @@ virNetDaemonAddShutdownInhibition(virNetDaemon *dmn) dmn->autoShutdownInhibitions++; =20 VIR_DEBUG("dmn=3D%p inhibitions=3D%zu", dmn, dmn->autoShutdownInhibiti= ons); - -#ifdef G_OS_UNIX - if (dmn->autoShutdownInhibitions =3D=3D 1) - virNetDaemonCallInhibit(dmn, - "shutdown", - _("Libvirt"), - _("Virtual machines need to be saved"), - "delay"); -#endif } =20 =20 @@ -579,11 +506,6 @@ virNetDaemonRemoveShutdownInhibition(virNetDaemon *dmn) dmn->autoShutdownInhibitions--; =20 VIR_DEBUG("dmn=3D%p inhibitions=3D%zu", dmn, dmn->autoShutdownInhibiti= ons); - - if (dmn->autoShutdownInhibitions =3D=3D 0) { - VIR_DEBUG("Closing inhibit FD %d", dmn->autoShutdownInhibitFd); - VIR_FORCE_CLOSE(dmn->autoShutdownInhibitFd); - } } =20 =20 --=20 2.46.0