From nobody Mon May 6 21:10:23 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1532619426998720.8779416345523; Thu, 26 Jul 2018 08:37:06 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 4A7D337E87; Thu, 26 Jul 2018 15:36:41 +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 05CB368B23; Thu, 26 Jul 2018 15:36:41 +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 4637F18037F7; Thu, 26 Jul 2018 15:36:39 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w6QFaZqi008817 for ; Thu, 26 Jul 2018 11:36:35 -0400 Received: by smtp.corp.redhat.com (Postfix) id 4795E2027EB6; Thu, 26 Jul 2018 15:36:35 +0000 (UTC) Received: from moe.brq.redhat.com (unknown [10.43.2.192]) by smtp.corp.redhat.com (Postfix) with ESMTP id BD4ED2026D68; Thu, 26 Jul 2018 15:36:34 +0000 (UTC) From: Michal Privoznik To: libvir-list@redhat.com Date: Thu, 26 Jul 2018 17:36:27 +0200 Message-Id: <2c51f874a6bfa6290ad10607ae647169d106b993.1532619290.git.mprivozn@redhat.com> In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-loop: libvir-list@redhat.com Cc: eskultet@redhat.com Subject: [libvirt] [PATCH 1/3] util: Rework virStringListAdd 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: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Thu, 26 Jul 2018 15:37:05 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" So every caller does the same: they use virStringListAdd() to add new item into the list and then free the old copy to replace it with new list. It's not very memory effective, nor environmental friendly. Signed-off-by: Michal Privoznik Reviewed-by: Erik Skultety --- src/util/virmacmap.c | 8 ++------ src/util/virstring.c | 34 ++++++++++++++-------------------- src/util/virstring.h | 4 ++-- tests/virstringtest.c | 6 +----- 4 files changed, 19 insertions(+), 33 deletions(-) diff --git a/src/util/virmacmap.c b/src/util/virmacmap.c index 88ca9b3f36..c7b700fa05 100644 --- a/src/util/virmacmap.c +++ b/src/util/virmacmap.c @@ -90,7 +90,6 @@ virMacMapAddLocked(virMacMapPtr mgr, { int ret =3D -1; char **macsList =3D NULL; - char **newMacsList =3D NULL; =20 if ((macsList =3D virHashLookup(mgr->macs, domain)) && virStringListHasString((const char**) macsList, mac)) { @@ -98,15 +97,12 @@ virMacMapAddLocked(virMacMapPtr mgr, goto cleanup; } =20 - if (!(newMacsList =3D virStringListAdd((const char **) macsList, mac))= || - virHashUpdateEntry(mgr->macs, domain, newMacsList) < 0) + if (virStringListAdd(&macsList, mac) < 0 || + virHashUpdateEntry(mgr->macs, domain, macsList) < 0) goto cleanup; - newMacsList =3D NULL; - virStringListFree(macsList); =20 ret =3D 0; cleanup: - virStringListFree(newMacsList); return ret; } =20 diff --git a/src/util/virstring.c b/src/util/virstring.c index 93fda69d7f..59f57a97b8 100644 --- a/src/util/virstring.c +++ b/src/util/virstring.c @@ -175,32 +175,26 @@ char *virStringListJoin(const char **strings, * @strings: a NULL-terminated array of strings * @item: string to add * - * Creates new strings list with all strings duplicated and @item - * at the end of the list. Callers is responsible for freeing - * both @strings and returned list. + * Appends @item into string list @strings. If *@strings is not + * allocated yet new string list is created. + * + * Returns: 0 on success, + * -1 otherwise */ -char ** -virStringListAdd(const char **strings, +int +virStringListAdd(char ***strings, const char *item) { - char **ret =3D NULL; - size_t i =3D virStringListLength(strings); + size_t i =3D virStringListLength((const char **) *strings); =20 - if (VIR_ALLOC_N(ret, i + 2) < 0) - goto error; + if (VIR_REALLOC_N(*strings, i + 2) < 0) + return -1; =20 - for (i =3D 0; strings && strings[i]; i++) { - if (VIR_STRDUP(ret[i], strings[i]) < 0) - goto error; - } + (*strings)[i + 1] =3D NULL; + if (VIR_STRDUP((*strings)[i], item) < 0) + return -1; =20 - if (VIR_STRDUP(ret[i], item) < 0) - goto error; - - return ret; - error: - virStringListFree(ret); - return NULL; + return 0; } =20 =20 diff --git a/src/util/virstring.h b/src/util/virstring.h index 125fd4eede..a2133ab7ce 100644 --- a/src/util/virstring.h +++ b/src/util/virstring.h @@ -44,8 +44,8 @@ char *virStringListJoin(const char **strings, const char *delim) ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2); =20 -char **virStringListAdd(const char **strings, - const char *item); +int virStringListAdd(char ***strings, + const char *item); void virStringListRemove(char ***strings, const char *item); =20 diff --git a/tests/virstringtest.c b/tests/virstringtest.c index 1230aba5b7..1a1e6364d1 100644 --- a/tests/virstringtest.c +++ b/tests/virstringtest.c @@ -179,12 +179,8 @@ static int testAdd(const void *args) size_t i; =20 for (i =3D 0; data->tokens[i]; i++) { - char **tmp =3D virStringListAdd((const char **)list, data->tokens[= i]); - if (!tmp) + if (virStringListAdd(&list, data->tokens[i]) < 0) goto cleanup; - virStringListFree(list); - list =3D tmp; - tmp =3D NULL; } =20 if (!list && --=20 2.16.4 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Mon May 6 21:10:23 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1532619794149902.8956100581588; Thu, 26 Jul 2018 08:43:14 -0700 (PDT) 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 mx1.redhat.com (Postfix) with ESMTPS id 67779935A7 for ; Thu, 26 Jul 2018 15:43:08 +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 EBEE1108427C; Thu, 26 Jul 2018 15:36:47 +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 6B5AD18037FA; Thu, 26 Jul 2018 15:36:47 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w6QFaaC0008825 for ; Thu, 26 Jul 2018 11:36:36 -0400 Received: by smtp.corp.redhat.com (Postfix) id 0DCC62027EA0; Thu, 26 Jul 2018 15:36:36 +0000 (UTC) Received: from moe.brq.redhat.com (unknown [10.43.2.192]) by smtp.corp.redhat.com (Postfix) with ESMTP id 84ED92026D68; Thu, 26 Jul 2018 15:36:35 +0000 (UTC) From: Michal Privoznik To: libvir-list@redhat.com Date: Thu, 26 Jul 2018 17:36:28 +0200 Message-Id: <63c304e1211b7daf6836e0ed732213a3b8206b22.1532619290.git.mprivozn@redhat.com> In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-loop: libvir-list@redhat.com Cc: eskultet@redhat.com Subject: [libvirt] [PATCH 2/3] lxc: Turn @veths into a string list in virLXCProcessStart 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: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Thu, 26 Jul 2018 15:43:13 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" This way it will be easier to use autofree. Signed-off-by: Michal Privoznik Reviewed-by: Erik Skultety --- src/lxc/lxc_process.c | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/src/lxc/lxc_process.c b/src/lxc/lxc_process.c index d021a890f7..3ac39d598c 100644 --- a/src/lxc/lxc_process.c +++ b/src/lxc/lxc_process.c @@ -514,8 +514,7 @@ static int virLXCProcessSetupNamespaces(virConnectPtr c= onn, * virLXCProcessSetupInterfaces: * @conn: pointer to connection * @def: pointer to virtual machine structure - * @nveths: number of interfaces - * @veths: interface names + * @veths: string list of interface names * * Sets up the container interfaces by creating the veth device pairs and * attaching the parent end to the appropriate bridge. The container end @@ -525,7 +524,6 @@ static int virLXCProcessSetupNamespaces(virConnectPtr c= onn, */ static int virLXCProcessSetupInterfaces(virConnectPtr conn, virDomainDefPtr def, - size_t *nveths, char ***veths) { int ret =3D -1; @@ -534,6 +532,8 @@ static int virLXCProcessSetupInterfaces(virConnectPtr c= onn, virDomainNetDefPtr net; virDomainNetType type; =20 + *veths =3D NULL; + for (i =3D 0; i < def->nnets; i++) { char *veth =3D NULL; virNetDevBandwidthPtr actualBandwidth; @@ -549,9 +549,6 @@ static int virLXCProcessSetupInterfaces(virConnectPtr c= onn, if (virDomainNetAllocateActualDevice(def, net) < 0) goto cleanup; =20 - if (VIR_EXPAND_N(*veths, *nveths, 1) < 0) - goto cleanup; - type =3D virDomainNetGetActualType(net); switch (type) { case VIR_DOMAIN_NET_TYPE_NETWORK: @@ -604,7 +601,8 @@ static int virLXCProcessSetupInterfaces(virConnectPtr c= onn, } } =20 - (*veths)[(*nveths)-1] =3D veth; + if (virStringListAdd(veths, veth) < 0) + goto cleanup; =20 if (VIR_STRDUP(def->nets[i]->ifname_guest_actual, veth) < 0) goto cleanup; @@ -902,7 +900,6 @@ int virLXCProcessStop(virLXCDriverPtr driver, static virCommandPtr virLXCProcessBuildControllerCmd(virLXCDriverPtr driver, virDomainObjPtr vm, - int nveths, char **veths, int *ttyFDs, size_t nttyFDs, @@ -987,7 +984,7 @@ virLXCProcessBuildControllerCmd(virLXCDriverPtr driver, virCommandAddArg(cmd, "--handshake"); virCommandAddArgFormat(cmd, "%d", handshakefd); =20 - for (i =3D 0; i < nveths; i++) + for (i =3D 0; veths && veths[i]; i++) virCommandAddArgList(cmd, "--veth", veths[i], NULL); =20 virCommandPassFD(cmd, handshakefd, 0); @@ -1184,7 +1181,6 @@ int virLXCProcessStart(virConnectPtr conn, size_t i; char *logfile =3D NULL; int logfd =3D -1; - size_t nveths =3D 0; char **veths =3D NULL; int handshakefds[2] =3D { -1, -1 }; off_t pos =3D -1; @@ -1355,7 +1351,7 @@ int virLXCProcessStart(virConnectPtr conn, } =20 VIR_DEBUG("Setting up Interfaces"); - if (virLXCProcessSetupInterfaces(conn, vm->def, &nveths, &veths) < 0) + if (virLXCProcessSetupInterfaces(conn, vm->def, &veths) < 0) goto cleanup; =20 VIR_DEBUG("Setting up namespaces if any"); @@ -1379,7 +1375,7 @@ int virLXCProcessStart(virConnectPtr conn, =20 if (!(cmd =3D virLXCProcessBuildControllerCmd(driver, vm, - nveths, veths, + veths, ttyFDs, nttyFDs, nsInheritFDs, files, nfiles, @@ -1559,9 +1555,7 @@ int virLXCProcessStart(virConnectPtr conn, virLXCProcessStop(driver, vm, VIR_DOMAIN_SHUTOFF_FAILED); } virCommandFree(cmd); - for (i =3D 0; i < nveths; i++) - VIR_FREE(veths[i]); - VIR_FREE(veths); + virStringListFree(veths); for (i =3D 0; i < nttyFDs; i++) VIR_FORCE_CLOSE(ttyFDs[i]); VIR_FREE(ttyFDs); --=20 2.16.4 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Mon May 6 21:10:23 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1532619434273473.6590174876027; Thu, 26 Jul 2018 08:37:14 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 5AB50206E47; Thu, 26 Jul 2018 15:36:51 +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 10A72679F0; Thu, 26 Jul 2018 15:36:50 +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 4EBE03FB1D; Thu, 26 Jul 2018 15:36:45 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w6QFaaCe008830 for ; Thu, 26 Jul 2018 11:36:37 -0400 Received: by smtp.corp.redhat.com (Postfix) id C8F0A2027EA0; Thu, 26 Jul 2018 15:36:36 +0000 (UTC) Received: from moe.brq.redhat.com (unknown [10.43.2.192]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4B8E02026D68; Thu, 26 Jul 2018 15:36:36 +0000 (UTC) From: Michal Privoznik To: libvir-list@redhat.com Date: Thu, 26 Jul 2018 17:36:29 +0200 Message-Id: <69016711e14af245a11b8841447c8c36bc4ce089.1532619290.git.mprivozn@redhat.com> In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-loop: libvir-list@redhat.com Cc: eskultet@redhat.com Subject: [libvirt] [PATCH 3/3] lxc: Use VIR_AUTOPTR for @veths in virLXCProcessStart 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: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Thu, 26 Jul 2018 15:37:13 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Now that we have VIR_AUTOPTR and that @veths is a string list we can use VIR_AUTOPTR to free it automagically. Signed-off-by: Michal Privoznik Reviewed-by: Erik Skultety --- src/lxc/lxc_process.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/lxc/lxc_process.c b/src/lxc/lxc_process.c index 3ac39d598c..6eef17d1ce 100644 --- a/src/lxc/lxc_process.c +++ b/src/lxc/lxc_process.c @@ -1181,7 +1181,7 @@ int virLXCProcessStart(virConnectPtr conn, size_t i; char *logfile =3D NULL; int logfd =3D -1; - char **veths =3D NULL; + VIR_AUTOPTR(virString) veths =3D NULL; int handshakefds[2] =3D { -1, -1 }; off_t pos =3D -1; char ebuf[1024]; @@ -1555,7 +1555,6 @@ int virLXCProcessStart(virConnectPtr conn, virLXCProcessStop(driver, vm, VIR_DOMAIN_SHUTOFF_FAILED); } virCommandFree(cmd); - virStringListFree(veths); for (i =3D 0; i < nttyFDs; i++) VIR_FORCE_CLOSE(ttyFDs[i]); VIR_FREE(ttyFDs); --=20 2.16.4 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list