From nobody Wed Apr 24 09:40:28 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 207.211.31.81 as permitted sender) client-ip=207.211.31.81; 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 207.211.31.81 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=1592778361; cv=none; d=zohomail.com; s=zohoarc; b=TDbsao7yQgZsD1v0yex9/LS4pq5tSgrsaEXZvZzVi1AiX77eGftQdQjxsNpUyXeKhKEVDALSsdzYYnQJvbzbl+A60/eouXICN0pmJ+uv13cqQfgM+Z9oVpdf0lqWIpFRJ1ITpvzBFfpWc+CfgCtyEyKG8iuQ3CuHkpuT0mpyvs0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1592778361; 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=P3Unw1EXcbsPX3rQNzDhlRvuyZNKGoxGjjtQy1qvkI0=; b=DP7CNAMbHzQIlUFpBhTxy4LL6Qz6W5YC871tOG57pvpJF5pq1ZuO34SQHXQvgOCEtucPVy39Dzl5I+YQo7hwb3CX6SAyEIaCbv035hhrAm/hUgdXzEvPKJJ4u63bms4xXKAf7Ynoou9TJzkFP1c4EQ+8BrHgcGdD4WwU6sIvdoQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.81 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-2.mimecast.com [207.211.31.81]) by mx.zohomail.com with SMTPS id 1592778361386894.520707981267; Sun, 21 Jun 2020 15:26:01 -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-301-mBg3Yt3-O3SrFmYKU2so-g-1; Sun, 21 Jun 2020 18:25:58 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 5A7CA8018AB; Sun, 21 Jun 2020 22:25:53 +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 39DDD7C1F5; Sun, 21 Jun 2020 22:25:53 +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 0542F1809547; Sun, 21 Jun 2020 22:25:53 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 05LMPRWN001559 for ; Sun, 21 Jun 2020 18:25:27 -0400 Received: by smtp.corp.redhat.com (Postfix) id 279D72157F25; Sun, 21 Jun 2020 22:25:27 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast06.extmail.prod.ext.rdu2.redhat.com [10.11.55.22]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 22D2F2157F23 for ; Sun, 21 Jun 2020 22:25:24 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-1.mimecast.com [205.139.110.61]) (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 100B3185A78B for ; Sun, 21 Jun 2020 22:25:24 +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-144-cUlY40qxOjurzu7FyOA_3w-1; Sun, 21 Jun 2020 18:25:21 -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 1jn7Ka-00045s-C6 for libvir-list@redhat.com; Mon, 22 Jun 2020 00:16:48 +0300 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1592778360; 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=P3Unw1EXcbsPX3rQNzDhlRvuyZNKGoxGjjtQy1qvkI0=; b=U47j4s6bPGpVeDDUQ+puPf50B0IcKZaOXgsjmOPUqF79grhnUfv/L/WrMf70lKFGq0IoFz bzmfTbx4k9YIIQUqiCG3U/Qu8wyGfyvDzElcCMXmBT7BuWGYFit1nXqF25qSRELHM2RdOe hm6Ck60rxfcCQrJ2+habuCcj7ToVA+A= X-MC-Unique: mBg3Yt3-O3SrFmYKU2so-g-1 X-MC-Unique: cUlY40qxOjurzu7FyOA_3w-1 From: Dmitry Nesterenko To: libvir-list@redhat.com Subject: [PATCH 1/2] virthook: refactoring for support hooks placed in several files Date: Mon, 22 Jun 2020 00:16:38 +0300 Message-Id: <20200621211639.385175-2-dmitry.nesterenko@virtuozzo.com> In-Reply-To: <20200621211639.385175-1-dmitry.nesterenko@virtuozzo.com> References: <20200621211639.385175-1-dmitry.nesterenko@virtuozzo.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.6 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.13 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 Reviewed-by: Michal Privoznik --- src/util/virhook.c | 74 +++++++++++++++++++++++++++++++++------------- 1 file changed, 53 insertions(+), 21 deletions(-) diff --git a/src/util/virhook.c b/src/util/virhook.c index e499841f66..64ee9a2307 100644 --- a/src/util/virhook.c +++ b/src/util/virhook.c @@ -221,6 +221,57 @@ virHookPresent(int driver) return 1; } =20 +/** + * virRunScript: + * @path: the script path + * @id: an id for the object '-' if non available for example on daemon ho= oks + * @op: the operation on the id + * @subop: a sub_operation, currently unused + * @extra: optional string information + * @input: extra input given to the script on stdin + * @output: optional address of variable to store malloced result buffer + * + * Implement a execution of script. This is a synchronous call, we wait for + * execution completion. If @output is non-NULL, *output is guaranteed to = be + * allocated after successful virRunScript, and is best-effort allocated a= fter + * failed virRunScript; the caller is responsible for freeing *output. + * + * Returns: 0 if the execution succeeded, -1 if script returned an error + */ +static int +virRunScript(const char *path, + const char *id, + const char *op, + const char *subop, + const char *extra, + const char *input, + char **output) +{ + int ret; + g_autoptr(virCommand) cmd =3D NULL; + + VIR_DEBUG("Calling hook %s id=3D%s op=3D%s subop=3D%s extra=3D%s", + path, id, op, subop, extra); + + cmd =3D virCommandNewArgList(path, id, op, subop, extra, NULL); + + virCommandAddEnvPassCommon(cmd); + + if (input) + virCommandSetInputBuffer(cmd, input); + if (output) + virCommandSetOutputBuffer(cmd, output); + + ret =3D virCommandRun(cmd, NULL); + if (ret < 0) { + /* Convert INTERNAL_ERROR into known error. */ + virReportError(VIR_ERR_HOOK_SCRIPT_FAILED, "%s", + virGetLastErrorMessage()); + } + + return ret; +} + /** * virHookCall: * @driver: the driver number (from virHookDriver enum) @@ -249,7 +300,6 @@ virHookCall(int driver, const char *input, char **output) { - int ret; g_autofree char *path =3D NULL; g_autoptr(virCommand) cmd =3D NULL; const char *drvstr; @@ -318,24 +368,6 @@ virHookCall(int driver, return -1; } =20 - VIR_DEBUG("Calling hook opstr=3D%s subopstr=3D%s extra=3D%s", - opstr, subopstr, extra); - - cmd =3D virCommandNewArgList(path, id, opstr, subopstr, extra, NULL); - - virCommandAddEnvPassCommon(cmd); - - if (input) - virCommandSetInputBuffer(cmd, input); - if (output) - virCommandSetOutputBuffer(cmd, output); - - ret =3D virCommandRun(cmd, NULL); - if (ret < 0) { - /* Convert INTERNAL_ERROR into known error. */ - virReportError(VIR_ERR_HOOK_SCRIPT_FAILED, "%s", - virGetLastErrorMessage()); - } - - return ret; + return virRunScript(path, id, opstr, subopstr, extra, + input, output); } --=20 2.18.4 From nobody Wed Apr 24 09:40:28 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 207.211.31.120 as permitted sender) client-ip=207.211.31.120; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.120 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=1592778341; cv=none; d=zohomail.com; s=zohoarc; b=cZ3TOupknRC91xlnR3pD2ql3ozbY3a/XoY8eXMiiUB5G9tljF0jiOGybhwa4o0KZ7ZWCGAaLvMD69Urmi/Zs+5YNm85AOGp2Rdu/qdho15cUpYOxXBFiImgUNpHYpGp2E9/eEaRydHW9PvDqx2qdYW2AqWtwOmg39IA7xFrm+yA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1592778341; 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=HT4u5l/UMkKpq7vLMmfVWM1E5lTbDpNiVbgTiSdnE5c=; b=OrBVZwjzibz5blO/rGkU6QUKENU9N50yI9YSvyeQH682gWRYye/Y9KElI6SIducZJ6JQ0BqEbvc8XvfGkUTKbWox8LfnakY7EtdGmsNiBFuChr2NqKJHtgL0yoe5FIIaR610orxsJsfRn5g+dHXCIGQZpj/+XokZOdjUZL/WNO8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.120 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-1.mimecast.com (us-smtp-delivery-1.mimecast.com [207.211.31.120]) by mx.zohomail.com with SMTPS id 1592778341472689.1559905939005; Sun, 21 Jun 2020 15:25:41 -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-259-i2VOUWHfN-KbqkL-Rn8Ryw-1; Sun, 21 Jun 2020 18:25:36 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 8E1248018AC; Sun, 21 Jun 2020 22:25:30 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 43E001001E91; Sun, 21 Jun 2020 22:25:29 +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 8E589833CC; Sun, 21 Jun 2020 22:25:26 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 05LMPO6V001536 for ; Sun, 21 Jun 2020 18:25:24 -0400 Received: by smtp.corp.redhat.com (Postfix) id 525AE33245; Sun, 21 Jun 2020 22:25:24 +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 4DC4D33244 for ; Sun, 21 Jun 2020 22:25:21 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-1.mimecast.com [205.139.110.61]) (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 216E0811768 for ; Sun, 21 Jun 2020 22:25:21 +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-386-xCsXcywhMDGRuqKZVXteyA-1; Sun, 21 Jun 2020 18:25:18 -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 1jn7Ka-00045s-FD for libvir-list@redhat.com; Mon, 22 Jun 2020 00:16:48 +0300 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1592778340; 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=HT4u5l/UMkKpq7vLMmfVWM1E5lTbDpNiVbgTiSdnE5c=; b=Jv9efK+Pjqx1eqbQHQPkrisq36hy5kUxQVIGpWboL+TkBBeQaFcc+Sp/0jL8dek53BGu9g 2/0JLxa/4dryB/eUicSQIPCFsNIrPfHm976KMuLlE+noKyqo1uZTLLXdk5fnmTogdbE0Ef EL3IHHkxM0orX6ixF9DKJf00jcXp3X4= X-MC-Unique: i2VOUWHfN-KbqkL-Rn8Ryw-1 X-MC-Unique: xCsXcywhMDGRuqKZVXteyA-1 From: Dmitry Nesterenko To: libvir-list@redhat.com Subject: [PATCH 2/2] virhook: support hooks placed in several files Date: Mon, 22 Jun 2020 00:16:39 +0300 Message-Id: <20200621211639.385175-3-dmitry.nesterenko@virtuozzo.com> In-Reply-To: <20200621211639.385175-1-dmitry.nesterenko@virtuozzo.com> References: <20200621211639.385175-1-dmitry.nesterenko@virtuozzo.com> X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 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.84 on 10.5.11.22 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 | 110 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 101 insertions(+), 9 deletions(-) diff --git a/src/util/virhook.c b/src/util/virhook.c index 64ee9a2307..763381d962 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,65 @@ virHookCall(int driver, return -1; } =20 - return virRunScript(path, id, opstr, subopstr, extra, - input, output); + script_ret =3D 1; + + if (virFileExists(path) && virFileIsExecutable(path)) { + script_ret =3D virRunScript(path, id, opstr, subopstr, extra, + input, output); + if (script_ret < 0 && output) + return -1; + } + + 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 < 0 && output) + return -1; + if (entry_ret < script_ret) + script_ret =3D entry_ret; + + /* Replace output to new output from item */ + if (output && !virStringIsEmpty(entry_output)) { + g_free(*output); + *output =3D g_steal_pointer(&entry_output); + } + } + + return script_ret; } --=20 2.18.4