From nobody Fri Dec 19 22:01:37 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) client-ip=170.10.133.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1678337361; cv=none; d=zohomail.com; s=zohoarc; b=ishYItlSCNoyLLBcVmSRgRC24tLfS+B2tUfz8xNqeKolVzTtusjs1Kqlrk5mJQJmMOg5W6gPq/yPimSdjMrwcvyx12BAKtUR15omwqVGq3dKGJyrYY7Du44YQg5wDuGypU0m9rrxWdFrKQTfICqz1ear8tFcCewiH+1R83k6H/M= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1678337361; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=Kfl7Si8JVmSTEERpZFldzWyqzfU5s12BUzhhptxHPXg=; b=kSg1myKLohuyNBrmjWRFiWDzutHxbHdlJaolEHa21q5QyKUU8FXB4WmN502M6dmzR/wLvEAQ7PbIgEQZNkjsDJq3iaPBOdrmimvfUDFVjKQtisQ1M2BpM/4iEfpIWxZucQyo6DWIxYP/G08fpKwY10NUsSiWTUH8xqyp2OJRPlM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mx.zohomail.com with SMTPS id 1678337361046274.97781601516317; Wed, 8 Mar 2023 20:49:21 -0800 (PST) Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-456-tEb1szfEP4aNEvw3wK1aWg-1; Wed, 08 Mar 2023 23:49:14 -0500 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 53D1E1C06EE3; Thu, 9 Mar 2023 04:49:12 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (unknown [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id EEB05403570F; Thu, 9 Mar 2023 04:49:10 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (localhost [IPv6:::1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 9DF111946A6C; Thu, 9 Mar 2023 04:49:09 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id E389A19465B5 for ; Thu, 9 Mar 2023 04:49:08 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id C7BB2C15BAD; Thu, 9 Mar 2023 04:49:08 +0000 (UTC) Received: from vhost3.router.laine.org (unknown [10.2.16.63]) by smtp.corp.redhat.com (Postfix) with ESMTP id A9CE4C15BA0; Thu, 9 Mar 2023 04:49:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1678337360; h=from:from:sender:sender: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:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=Kfl7Si8JVmSTEERpZFldzWyqzfU5s12BUzhhptxHPXg=; b=f/bLzikchwKQv1swRH9Tg66psXSPwY0uClCSCQaZMtCL9WA1OtfA+yn9ETPFwGWKMjNoTR adZUcRbp7p7q5tqrBbfap84FfXnOTyP66lVcG5QoMxpjxjWUc/DTnGPVTJj0sJgchNHxnU vP1rmMC2PGNw2qYoBkSVtRFDSqTO42g= X-MC-Unique: tEb1szfEP4aNEvw3wK1aWg-1 X-Original-To: libvir-list@listman.corp.redhat.com From: Laine Stump To: libvir-list@redhat.com Subject: [libvirt PATCH 1/4] util: add an API to retrieve the resolved path to a virCommand's binary Date: Wed, 8 Mar 2023 23:49:05 -0500 Message-Id: <20230309044908.29316-2-laine@redhat.com> In-Reply-To: <20230309044908.29316-1-laine@redhat.com> References: <20230309044908.29316-1-laine@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: passt-dev@passt.top Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1678337362641100001 Content-Type: text/plain; charset="utf-8"; x-default="true" The binary to be exec'ed by virExec() is stored in virCommand::args[0], and is resolved to a full absolute path (stored in a local of virExec() just prior to execve(). Since we will have another use for the full absolute path, lets make an API to resolve/retrieve the absolute path, and cache it in virCommand::binaryPath so we only have to do the resolution once. Signed-off-by: Laine Stump --- src/libvirt_private.syms | 1 + src/util/vircommand.c | 51 +++++++++++++++++++++++++++++++--------- src/util/vircommand.h | 1 + 3 files changed, 42 insertions(+), 11 deletions(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 4959dcec67..6f44788233 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2091,6 +2091,7 @@ virCommandDryRunTokenNew; virCommandExec; virCommandFree; virCommandGetArgList; +virCommandGetBinaryPath; virCommandGetGID; virCommandGetUID; virCommandHandshakeNotify; diff --git a/src/util/vircommand.c b/src/util/vircommand.c index 1144dfb597..757d462ba1 100644 --- a/src/util/vircommand.c +++ b/src/util/vircommand.c @@ -88,6 +88,7 @@ struct _virCommandSendBuffer { struct _virCommand { int has_error; /* 0 on success, -1 on error */ =20 + char *binaryPath; /* only valid if args[0] isn't absolute path */ char **args; size_t nargs; size_t maxargs; @@ -630,6 +631,7 @@ virCommandMassClose(virCommand *cmd, =20 # endif /* ! __FreeBSD__ */ =20 + /* * virExec: * @cmd virCommand * containing all information about the program to @@ -646,22 +648,13 @@ virExec(virCommand *cmd) int childin =3D cmd->infd; int childout =3D -1; int childerr =3D -1; - g_autofree char *binarystr =3D NULL; const char *binary =3D NULL; int ret; g_autofree gid_t *groups =3D NULL; int ngroups; =20 - if (!g_path_is_absolute(cmd->args[0])) { - if (!(binary =3D binarystr =3D virFindFileInPath(cmd->args[0]))) { - virReportSystemError(ENOENT, - _("Cannot find '%s' in path"), - cmd->args[0]); - return -1; - } - } else { - binary =3D cmd->args[0]; - } + if (!(binary =3D virCommandGetBinaryPath(cmd))) + return -1; =20 if (childin < 0) { if (getDevNull(&null) < 0) @@ -2164,6 +2157,40 @@ virCommandGetArgList(virCommand *cmd, } =20 =20 +/* + * virCommandGetBinaryPath: + * @cmd: virCommand* containing all information about the program + * + * If args[0] is an absolute path, return that. If not, then resolve + * args[0] to a full absolute path, cache that in binaryPath, and + * return a pointer to this resolved string. binaryPath is only set by + * calling this function, so even other virCommand functions should + * access binaryPath via this function. + * + * returns const char* with the full path of the binary to be + * executed, or NULL on failure. + */ +const char * +virCommandGetBinaryPath(virCommand *cmd) +{ + + if (cmd->binaryPath) + return cmd->binaryPath; + + if (g_path_is_absolute(cmd->args[0])) + return cmd->args[0]; + + if (!(cmd->binaryPath =3D virFindFileInPath(cmd->args[0]))) { + virReportSystemError(ENOENT, + _("Cannot find '%s' in path"), + cmd->args[0]); + return NULL; + } + + return cmd->binaryPath; +} + + #ifndef WIN32 /* * Manage input and output to the child process. @@ -3015,6 +3042,8 @@ virCommandFree(virCommand *cmd) VIR_FORCE_CLOSE(cmd->outfd); VIR_FORCE_CLOSE(cmd->errfd); =20 + g_free(cmd->binaryPath); + for (i =3D 0; i < cmd->nargs; i++) g_free(cmd->args[i]); g_free(cmd->args); diff --git a/src/util/vircommand.h b/src/util/vircommand.h index e0002103b6..d51449ac90 100644 --- a/src/util/vircommand.h +++ b/src/util/vircommand.h @@ -170,6 +170,7 @@ int virCommandToStringBuf(virCommand *cmd, bool linebreaks, bool stripCommandPath); =20 +const char *virCommandGetBinaryPath(virCommand *cmd); int virCommandGetArgList(virCommand *cmd, char ***args); =20 int virCommandExec(virCommand *cmd, gid_t *groups, int ngroups) G_GNUC_WAR= N_UNUSED_RESULT; --=20 2.39.2