From nobody Tue Feb 10 16:18:46 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 205.139.110.61 as permitted sender) client-ip=205.139.110.61; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.61 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail(p=none dis=none) header.from=virtuozzo.com ARC-Seal: i=1; a=rsa-sha256; t=1592923589; cv=none; d=zohomail.com; s=zohoarc; b=jIpw1A9K5iUCCs/3+PNQAUGq7+xEjzu0dSW6OiVBwW9eqiVgC1vq8+4cPzZdqceQ0sgtoBw/xb+KN7+05XfsEbKgAsytPujU7YGiWY/NmBYLNd+8nyzXqWH93w9301Z/5ozaeRUfhLR/RqCaSZC/TP4b+z+zGwusKHYsuCCRITU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1592923589; h=Content-Type:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=KITFLCyuHI/Jyv2RI/8f5k+7agwEQxGRmHWq7yu46xo=; b=lgNCA6g4Bo6RfxcxOH3/8w8Lg0iMmo0400otdbukAdeplSadfIy2BwzrqRGpjU7c6XJdI8rr92E4IszzKRIz0cu6zsShIzltW9wMCmakesYQjumvZTlkVLGSxfnDHysuBBo8t3uu3aNgUxACVs+2nwuIgpvgNGahNFmY9y2Pd+E= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.61 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-1.mimecast.com (us-smtp-1.mimecast.com [205.139.110.61]) by mx.zohomail.com with SMTPS id 1592923589717443.92824053795107; Tue, 23 Jun 2020 07:46:29 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-454-SB6LWxaVPoqbixC0wneVCg-1; Tue, 23 Jun 2020 10:46:26 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id B3BD5107ACCA; Tue, 23 Jun 2020 14:46:19 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 94D217166A; Tue, 23 Jun 2020 14:46:19 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 6588B1809547; Tue, 23 Jun 2020 14:46:19 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 05NEjorn015937 for ; Tue, 23 Jun 2020 10:45:50 -0400 Received: by smtp.corp.redhat.com (Postfix) id 2380D1007381; Tue, 23 Jun 2020 14:45:50 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast03.extmail.prod.ext.rdu2.redhat.com [10.11.55.19]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 1998A1007389 for ; Tue, 23 Jun 2020 14:45:46 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [205.139.110.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 9DCBE8026A2 for ; Tue, 23 Jun 2020 14:45:46 +0000 (UTC) Received: from relay3.sw.ru (relay.sw.ru [185.231.240.75]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-28-b_Ue4QmbNImVal-Pat0dNw-1; Tue, 23 Jun 2020 10:45:43 -0400 Received: from [172.16.25.65] (helo=dhcp-172-16-25-65.sw.ru) by relay3.sw.ru with esmtp (Exim 4.93) (envelope-from ) id 1jnkB0-000298-LW for libvir-list@redhat.com; Tue, 23 Jun 2020 17:45:30 +0300 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1592923588; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:content-type:content-type: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=KITFLCyuHI/Jyv2RI/8f5k+7agwEQxGRmHWq7yu46xo=; b=JEHUYQv/NeOMO++oUxOJ7yocoEtWho3HOkaswfApsFyt25LHb7W6kpHDhC/SC4/WWQDyhr mk8a1Kpjo6Lcj9G9RLIxizi8b9K31g41avLthCf2tFcfLVtVM8QPey7AQzDXoRnres2yT0 BteUWYj59Iw86VLy49lTynOU2tyJteQ= X-MC-Unique: SB6LWxaVPoqbixC0wneVCg-1 X-MC-Unique: b_Ue4QmbNImVal-Pat0dNw-1 From: Dmitry Nesterenko To: libvir-list@redhat.com Subject: [PATCH v2 2/3] virhook: support hooks placed in several files Date: Tue, 23 Jun 2020 17:45:34 +0300 Message-Id: <20200623144535.149179-3-dmitry.nesterenko@virtuozzo.com> In-Reply-To: <20200623144535.149179-1-dmitry.nesterenko@virtuozzo.com> References: <20200623144535.149179-1-dmitry.nesterenko@virtuozzo.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-loop: libvir-list@redhat.com X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Mimecast-Spam-Score: 2 X-Mimecast-Originator: redhat.com X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Signed-off-by: Dmitry Nesterenko --- src/util/virhook.c | 106 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 97 insertions(+), 9 deletions(-) diff --git a/src/util/virhook.c b/src/util/virhook.c index 64ee9a2307..cd26c8c9bd 100644 --- a/src/util/virhook.c +++ b/src/util/virhook.c @@ -33,6 +33,7 @@ #include "virfile.h" #include "configmake.h" #include "vircommand.h" +#include "virstring.h" =20 #define VIR_FROM_THIS VIR_FROM_HOOK =20 @@ -141,7 +142,11 @@ static int virHooksFound =3D -1; static int virHookCheck(int no, const char *driver) { + int ret; + DIR *dir; + struct dirent *entry; g_autofree char *path =3D NULL; + g_autofree char *dir_path =3D NULL; =20 if (driver =3D=3D NULL) { virReportError(VIR_ERR_INTERNAL_ERROR, @@ -158,16 +163,39 @@ virHookCheck(int no, const char *driver) =20 if (!virFileExists(path)) { VIR_DEBUG("No hook script %s", path); - return 0; + } else if (!virFileIsExecutable(path)) { + VIR_WARN("Non-executable hook script %s", path); + } else { + VIR_DEBUG("Found hook script %s", path); + return 1; } =20 - if (!virFileIsExecutable(path)) { - VIR_WARN("Non-executable hook script %s", path); + dir_path =3D g_strdup_printf("%s.d", path); + if ((ret =3D virDirOpenIfExists(&dir, dir_path)) < 0) + return -1; + + if (!ret) { + VIR_DEBUG("No hook script dir %s", dir_path); return 0; } =20 - VIR_DEBUG("Found hook script %s", path); - return 1; + while ((ret =3D virDirRead(dir, &entry, dir_path)) > 0) { + g_autofree char *entry_path =3D g_build_filename(dir_path, + entry->d_name, + NULL); + if (!virFileIsExecutable(entry_path)) { + VIR_WARN("Non-executable hook script %s", entry_path); + continue; + } + + VIR_DEBUG("Found hook script %s", entry_path); + ret =3D 1; + break; + } + + VIR_DIR_CLOSE(dir); + + return ret; } =20 /* @@ -282,7 +310,7 @@ virRunScript(const char *path, * @input: extra input given to the script on stdin * @output: optional address of variable to store malloced result buffer * - * Implement a hook call, where the external script for the driver is + * Implement a hook call, where the external scripts for the driver is * called with the given information. This is a synchronous call, we wait = for * execution completion. If @output is non-NULL, *output is guaranteed to = be * allocated after successful virHookCall, and is best-effort allocated af= ter @@ -300,11 +328,16 @@ virHookCall(int driver, const char *input, char **output) { + int ret, script_ret; + DIR *dir; + struct dirent *entry; g_autofree char *path =3D NULL; - g_autoptr(virCommand) cmd =3D NULL; + g_autofree char *dir_path =3D NULL; + VIR_AUTOSTRINGLIST entries =3D NULL; const char *drvstr; const char *opstr; const char *subopstr; + size_t i, nentries; =20 if (output) *output =3D NULL; @@ -368,6 +401,61 @@ virHookCall(int driver, return -1; } =20 - return virRunScript(path, id, opstr, subopstr, extra, - input, output); + script_ret =3D 1; + + if (virFileIsExecutable(path)) { + script_ret =3D virRunScript(path, id, opstr, subopstr, extra, + input, output); + } + + dir_path =3D g_strdup_printf("%s.d", path); + if ((ret =3D virDirOpenIfExists(&dir, dir_path)) < 0) + return -1; + + if (!ret) + return script_ret; + + while ((ret =3D virDirRead(dir, &entry, dir_path)) > 0) { + g_autofree char *entry_path =3D g_build_filename(dir_path, + entry->d_name, + NULL); + if (!virFileIsExecutable(entry_path)) + continue; + + virStringListAdd(&entries, entry_path); + } + + VIR_DIR_CLOSE(dir); + + if (ret < 0) + return -1; + + if (!entries) + return script_ret; + + nentries =3D virStringListLength((const char **)entries); + qsort(entries, nentries, sizeof(*entries), virStringSortCompare); + + for (i =3D 0; i < nentries; i++) { + int entry_ret; + char *entry_input; + g_autofree char *entry_output =3D NULL; + + /* Get input from previous output */ + entry_input =3D (!script_ret && output && + !virStringIsEmpty(*output)) ? *output : (char *)inp= ut; + entry_ret =3D virRunScript(entries[i], id, opstr, + subopstr, extra, entry_input, + (output) ? &entry_output : NULL); + if (entry_ret < script_ret) + script_ret =3D entry_ret; + + /* Replace output to new output from item */ + if (!entry_ret && output && !virStringIsEmpty(entry_output)) { + g_free(*output); + *output =3D g_steal_pointer(&entry_output); + } + } + + return script_ret; } --=20 2.18.4