From nobody Mon Feb 9 10:32:26 2026 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=1617808367; cv=none; d=zohomail.com; s=zohoarc; b=aqKoPnrpIvV8LiTOu+bTA5houiUQrlrBpCzsNZbjKELQWiCFQq7qfKcZgGOZlBSpmPc2O+IssuiDfeZVG/pcbf1Ul34+saeOc+MvkZfAJ+M/7jD941P8nD+b4cRMc7f98s4Rc9eztA5x6I/Ju0eMEgqI0HxXbRljh8c3vRA7Lp0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1617808367; h=Content-Type:Content-Transfer-Encoding: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=qCrMAXcFUIuvgbyR79NC7f+aMXgFCGsXajwQI6LErZg=; b=fWFvkU7eJqoO5FpX6TmDrQ2k51aT/SXhOw0sJiZW+eP/TXj0OuuhDgyPadRmo4tWsipeeiyoV9HNapX1Qsw8wC8hxhGtf65d5ewZPkAEecRcBijTU4eILOvMULrMke3vlk1xOD3OIN2lTTG2FKqjvQSV1DE5nkxoCQ5mV28MQPo= 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) header.from= 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 1617808367809300.9280337344502; Wed, 7 Apr 2021 08:12:47 -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-336-DfhHwa6IP9K1Y2--Cb0mbA-1; Wed, 07 Apr 2021 11:12:44 -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 D21EF188352A; Wed, 7 Apr 2021 15:12:39 +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 A7D9D1002EF0; Wed, 7 Apr 2021 15:12:39 +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 3D2D455344; Wed, 7 Apr 2021 15:12:39 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 137FA0AG031778 for ; Wed, 7 Apr 2021 11:10:00 -0400 Received: by smtp.corp.redhat.com (Postfix) id 1446119D9D; Wed, 7 Apr 2021 15:10:00 +0000 (UTC) Received: from speedmetal.lan (unknown [10.40.208.33]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4A6CE19C78 for ; Wed, 7 Apr 2021 15:09:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1617808366; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to: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=qCrMAXcFUIuvgbyR79NC7f+aMXgFCGsXajwQI6LErZg=; b=cI5jfvTGm4XhpBnUQB2MlS8S+saMhAFprUQmDxk2TaFukEPvHraZ19XXRjRoFo45R1Z/gT edBK/4XuNG4ZiD4yYabiNl+rXFSl5D9u+XyBbY1RY5M0xGp3dxzh+yowd4HPx+bbYFIJUy 0ksHctvL+jLkRfID5dPnhW+tzIaDze8= X-MC-Unique: DfhHwa6IP9K1Y2--Cb0mbA-1 From: Peter Krempa To: libvir-list@redhat.com Subject: [PATCH 05/24] virCommandToString: Allow stripping command path Date: Wed, 7 Apr 2021 17:09:28 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 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 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" In tests we don't want to use the full path to commands as it's unpleasant to keep that working on all systems. Add an integrated way to strip the prefix which will be used to replace virTestClearCommandPath() as a more systemic solution. Signed-off-by: Peter Krempa --- src/libvirt_private.syms | 1 + src/util/vircommand.c | 35 ++++++++++++++++++++++++++++++++--- src/util/vircommand.h | 4 ++++ 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index cb9fe7c80a..caa0fd18f8 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2025,6 +2025,7 @@ virCommandSetUID; virCommandSetUmask; virCommandSetWorkingDirectory; virCommandToString; +virCommandToStringFull; virCommandWait; virCommandWriteArgLog; virFork; diff --git a/src/util/vircommand.c b/src/util/vircommand.c index eb70f8cf85..8ae5badf0f 100644 --- a/src/util/vircommand.c +++ b/src/util/vircommand.c @@ -2055,9 +2055,10 @@ virCommandWriteArgLog(virCommandPtr cmd, int logfd) /** - * virCommandToString: + * virCommandToStringFull: * @cmd: the command to convert * @linebreaks: true to break line after each env var or option + * @stripCommandPath: strip the path leading to the binary of @cmd * * Call after adding all arguments and environment settings, but * before Run/RunAsync, to return a string representation of the @@ -2067,11 +2068,15 @@ virCommandWriteArgLog(virCommandPtr cmd, int logfd) * Caller is responsible for freeing the resulting string. */ char * -virCommandToString(virCommandPtr cmd, bool linebreaks) +virCommandToStringFull(virCommandPtr cmd, + bool linebreaks, + bool stripCommandPath) { size_t i; g_auto(virBuffer) buf =3D VIR_BUFFER_INITIALIZER; bool prevopt =3D false; + const char *command =3D cmd->args[0]; + g_autofree char *basename =3D NULL; /* Cannot assume virCommandRun will be called; so report the error * now. If virCommandRun is called, it will report the same error. */ @@ -2097,7 +2102,11 @@ virCommandToString(virCommandPtr cmd, bool linebreak= s) if (linebreaks) virBufferAddLit(&buf, "\\\n"); } - virBufferEscapeShell(&buf, cmd->args[0]); + + if (stripCommandPath) + command =3D basename =3D g_path_get_basename(command); + + virBufferEscapeShell(&buf, command); for (i =3D 1; i < cmd->nargs; i++) { virBufferAddChar(&buf, ' '); if (linebreaks) { @@ -2116,6 +2125,26 @@ virCommandToString(virCommandPtr cmd, bool linebreak= s) } +/** + * virCommandToString: + * @cmd: the command to convert + * @linebreaks: true to break line after each env var or option + * + * Call after adding all arguments and environment settings, but + * before Run/RunAsync, to return a string representation of the + * environment and arguments of cmd, suitably quoted for pasting into + * a shell. If virCommandRun cannot succeed (because of an + * out-of-memory condition while building cmd), NULL will be returned. + * Caller is responsible for freeing the resulting string. + */ +char * +virCommandToString(virCommandPtr cmd, + bool linebreaks) +{ + return virCommandToStringFull(cmd, linebreaks, false); +} + + int virCommandGetArgList(virCommandPtr cmd, char ***args, diff --git a/src/util/vircommand.h b/src/util/vircommand.h index a00f30f51f..5a700f8e15 100644 --- a/src/util/vircommand.h +++ b/src/util/vircommand.h @@ -171,6 +171,10 @@ void virCommandWriteArgLog(virCommandPtr cmd, int logfd); char *virCommandToString(virCommandPtr cmd, bool linebreaks) G_GNUC_WARN_U= NUSED_RESULT; +char *virCommandToStringFull(virCommandPtr cmd, + bool linebreaks, + bool stripCommandPath); + int virCommandGetArgList(virCommandPtr cmd, char ***args, size_t *nargs); int virCommandExec(virCommandPtr cmd, gid_t *groups, int ngroups) G_GNUC_W= ARN_UNUSED_RESULT; --=20 2.30.2