Re: [libvirt] [PATCH v3 04/11] util: netlink: use VIR_AUTOPTR for aggregate types

2018-08-13 Thread Sukrit Bhatnagar
On Mon, 13 Aug 2018 at 18:10, Erik Skultety  wrote:
>
> On Thu, Aug 09, 2018 at 09:42:12AM +0530, Sukrit Bhatnagar wrote:
> > By making use of GNU C's cleanup attribute handled by the
> > VIR_AUTOPTR macro for declaring aggregate pointer variables,
> > majority of the calls to *Free functions can be dropped, which
> > in turn leads to getting rid of most of our cleanup sections.
> >
> > Signed-off-by: Sukrit Bhatnagar 
> > ---
> >  src/util/virnetlink.c | 72 
> > ---
> >  1 file changed, 28 insertions(+), 44 deletions(-)
> >
> > diff --git a/src/util/virnetlink.c b/src/util/virnetlink.c
> > index fcdc09d..66e80e2 100644
> > --- a/src/util/virnetlink.c
> > +++ b/src/util/virnetlink.c
> > @@ -297,15 +297,16 @@ int virNetlinkCommand(struct nl_msg *nl_msg,
> >uint32_t src_pid, uint32_t dst_pid,
> >unsigned int protocol, unsigned int groups)
> >  {
> > -int ret = -1;
> >  struct sockaddr_nl nladdr = {
> >  .nl_family = AF_NETLINK,
> >  .nl_pid= dst_pid,
> >  .nl_groups = 0,
> >  };
> >  struct pollfd fds[1];
> > -VIR_AUTOPTR(virNetlinkHandle) nlhandle = NULL;
> >  int len = 0;
> > +VIR_AUTOPTR(virNetlinkHandle) nlhandle = NULL;
>
> unjustified code movement...
>
> > +
> > +*respbuflen = 0;
>
> unnecessary initialization..

We need *respbuflen to be 0 if -1 is returned. So if this initialization is
removed, we need to add `*respbuflen = 0;` in the cleanup section below.

> >  memset(fds, 0, sizeof(fds));
> >
> > @@ -324,15 +325,12 @@ int virNetlinkCommand(struct nl_msg *nl_msg,
> >  goto cleanup;
> >  }
> >
> > -ret = 0;
> >  *respbuflen = len;
> > - cleanup:
> > -if (ret < 0) {
> > -*resp = NULL;
> > -*respbuflen = 0;
> > -}
> > +return 0;
> >
> > -return ret;
> > + cleanup:
> > +*resp = NULL;
> > +return -1;
>
> I moved ^this hunk into the previous patch as I converted 1 more var into
> VIR_AUTOFREE.
>
> Reviewed-by: Erik Skultety 

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


Re: [libvirt] [PATCH v3 11/11] util: qemu: use VIR_AUTOPTR for aggregate types

2018-08-09 Thread Sukrit Bhatnagar
My bad. You can ignore this patch.
On Thu, 9 Aug 2018 at 18:18, Erik Skultety  wrote:
>
> On Thu, Aug 09, 2018 at 09:42:19AM +0530, Sukrit Bhatnagar wrote:
> > By making use of GNU C's cleanup attribute handled by the
> > VIR_AUTOPTR macro for declaring aggregate pointer variables,
> > majority of the calls to *Free functions can be dropped, which
> > in turn leads to getting rid of most of our cleanup sections.
> >
> > Signed-off-by: Sukrit Bhatnagar 
> > ---
> >  src/util/virqemu.c | 1 +
> >  1 file changed, 1 insertion(+)
> >
> > diff --git a/src/util/virqemu.c b/src/util/virqemu.c
> > index bc78853..1cd2b93 100644
> > --- a/src/util/virqemu.c
> > +++ b/src/util/virqemu.c
> > @@ -281,6 +281,7 @@ virQEMUBuildDriveCommandlineFromJSON(virJSONValuePtr 
> > srcdef)
> >   cleanup:
> >  virBufferFreeAndReset();
> >  return ret;
> > +
>
> I'm sure you wanted to add some more into the commit, right? :)
>
> Erik

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v3 09/11] util: netdevip: use VIR_AUTOPTR for aggregate types

2018-08-09 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOPTR macro for declaring aggregate pointer variables,
majority of the calls to *Free functions can be dropped, which
in turn leads to getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virnetdevip.c | 130 +++--
 1 file changed, 51 insertions(+), 79 deletions(-)

diff --git a/src/util/virnetdevip.c b/src/util/virnetdevip.c
index c6d6175..4a38a54 100644
--- a/src/util/virnetdevip.c
+++ b/src/util/virnetdevip.c
@@ -168,10 +168,9 @@ virNetDevIPAddrAdd(const char *ifname,
virSocketAddr *peer,
unsigned int prefix)
 {
-virSocketAddr *broadcast = NULL;
-int ret = -1;
-struct nl_msg *nlmsg = NULL;
 unsigned int recvbuflen;
+VIR_AUTOPTR(virNlMsg) nlmsg = NULL;
+VIR_AUTOPTR(virSocketAddr) broadcast = NULL;
 VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
 VIR_AUTOFREE(char *) ipStr = NULL;
 VIR_AUTOFREE(char *) peerStr = NULL;
@@ -186,13 +185,13 @@ virNetDevIPAddrAdd(const char *ifname,
 !(peer && VIR_SOCKET_ADDR_VALID(peer))) {
 /* compute a broadcast address if this is IPv4 */
 if (VIR_ALLOC(broadcast) < 0)
-goto cleanup;
+return -1;
 
 if (virSocketAddrBroadcastByPrefix(addr, prefix, broadcast) < 0) {
-virReportError(VIR_ERR_INTERNAL_ERROR,
-   _("Failed to determine broadcast address for '%s/%d'"),
-   ipStr, prefix);
-goto cleanup;
+virReportError(VIR_ERR_INTERNAL_ERROR,
+   _("Failed to determine broadcast address for 
'%s/%d'"),
+   ipStr, prefix);
+return -1;
 }
 bcastStr = virSocketAddrFormat(broadcast);
 }
@@ -206,11 +205,11 @@ virNetDevIPAddrAdd(const char *ifname,
 if (!(nlmsg = virNetDevCreateNetlinkAddressMessage(RTM_NEWADDR, ifname,
addr, prefix,
broadcast, peer)))
-goto cleanup;
+return -1;
 
 if (virNetlinkCommand(nlmsg, , ,
   0, 0, NETLINK_ROUTE, 0) < 0)
-goto cleanup;
+return -1;
 
 
 if (virNetlinkGetErrorCode(resp, recvbuflen) < 0) {
@@ -220,14 +219,10 @@ virNetDevIPAddrAdd(const char *ifname,
peerStr ? " peer " : "", peerStr ? peerStr : "",
bcastStr ? " bcast " : "", bcastStr ? bcastStr : "",
ifname);
-goto cleanup;
+return -1;
 }
 
-ret = 0;
- cleanup:
-nlmsg_free(nlmsg);
-VIR_FREE(broadcast);
-return ret;
+return 0;
 }
 
 
@@ -246,30 +241,26 @@ virNetDevIPAddrDel(const char *ifname,
virSocketAddr *addr,
unsigned int prefix)
 {
-int ret = -1;
-struct nl_msg *nlmsg = NULL;
 unsigned int recvbuflen;
+VIR_AUTOPTR(virNlMsg) nlmsg = NULL;
 VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
 
 if (!(nlmsg = virNetDevCreateNetlinkAddressMessage(RTM_DELADDR, ifname,
addr, prefix,
NULL, NULL)))
-goto cleanup;
+return -1;
 
 if (virNetlinkCommand(nlmsg, , , 0, 0,
   NETLINK_ROUTE, 0) < 0)
-goto cleanup;
+return -1;
 
 if (virNetlinkGetErrorCode(resp, recvbuflen) < 0) {
 virReportError(VIR_ERR_SYSTEM_ERROR,
_("Error removing IP address from %s"), ifname);
-goto cleanup;
+return -1;
 }
 
-ret = 0;
- cleanup:
-nlmsg_free(nlmsg);
-return ret;
+return 0;
 }
 
 
@@ -292,8 +283,6 @@ virNetDevIPRouteAdd(const char *ifname,
 virSocketAddrPtr gateway,
 unsigned int metric)
 {
-int ret = -1;
-struct nl_msg *nlmsg = NULL;
 struct nlmsghdr *resp = NULL;
 unsigned int recvbuflen;
 unsigned int ifindex;
@@ -304,6 +293,7 @@ virNetDevIPRouteAdd(const char *ifname,
 int errCode;
 virSocketAddr defaultAddr;
 virSocketAddrPtr actualAddr;
+VIR_AUTOPTR(virNlMsg) nlmsg = NULL;
 VIR_AUTOFREE(char *) toStr = NULL;
 VIR_AUTOFREE(char *) viaStr = NULL;
 
@@ -315,10 +305,10 @@ virNetDevIPRouteAdd(const char *ifname,
 int family = VIR_SOCKET_ADDR_FAMILY(gateway);
 if (family == AF_INET) {
 if (virSocketAddrParseIPv4(, VIR_SOCKET_ADDR_IPV4_ALL) 
< 0)
-goto cleanup;
+return -1;
 } else {
 if (virSocketAddrParseIPv6(, VIR_SOCKET_ADDR_IPV6_ALL) 
< 0)
-goto cleanup;
+return -1;
 }
 
 actualAddr =

[libvirt] [PATCH v3 06/11] util: netdevbridge: use VIR_AUTOPTR for aggregate types

2018-08-09 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOPTR macro for declaring aggregate pointer variables,
majority of the calls to *Free functions can be dropped, which
in turn leads to getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virnetdevbridge.c | 35 +--
 1 file changed, 13 insertions(+), 22 deletions(-)

diff --git a/src/util/virnetdevbridge.c b/src/util/virnetdevbridge.c
index fe3697b..089da78 100644
--- a/src/util/virnetdevbridge.c
+++ b/src/util/virnetdevbridge.c
@@ -417,12 +417,11 @@ virNetDevBridgeCreate(const char *brname)
 {
 /* use a netlink RTM_NEWLINK message to create the bridge */
 const char *type = "bridge";
-int rc = -1;
 struct nlmsgerr *err;
 struct ifinfomsg ifinfo = { .ifi_family = AF_UNSPEC };
 unsigned int recvbuflen;
-struct nl_msg *nl_msg;
 struct nlattr *linkinfo;
+VIR_AUTOPTR(virNlMsg) nl_msg = NULL;
 VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
 
 nl_msg = nlmsg_alloc_simple(RTM_NEWLINK,
@@ -444,7 +443,7 @@ virNetDevBridgeCreate(const char *brname)
 
 if (virNetlinkCommand(nl_msg, , , 0, 0,
   NETLINK_ROUTE, 0) < 0) {
-goto cleanup;
+return -1;
 }
 
 if (recvbuflen < NLMSG_LENGTH(0) || resp == NULL)
@@ -462,15 +461,14 @@ virNetDevBridgeCreate(const char *brname)
 /* fallback to ioctl if netlink doesn't support creating
  * bridges
  */
-rc = virNetDevBridgeCreateWithIoctl(brname);
-goto cleanup;
+return virNetDevBridgeCreateWithIoctl(brname);
 }
 # endif
 
 virReportSystemError(-err->error,
  _("error creating bridge interface %s"),
  brname);
-goto cleanup;
+return -1;
 }
 break;
 
@@ -480,19 +478,16 @@ virNetDevBridgeCreate(const char *brname)
 goto malformed_resp;
 }
 
-rc = 0;
- cleanup:
-nlmsg_free(nl_msg);
-return rc;
+return 0;
 
  malformed_resp:
 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("malformed netlink response message"));
-goto cleanup;
+return -1;
  buffer_too_small:
 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("allocated netlink buffer is too small"));
-goto cleanup;
+return -1;
 }
 
 
@@ -1055,11 +1050,10 @@ static int
 virNetDevBridgeFDBAddDel(const virMacAddr *mac, const char *ifname,
  unsigned int flags, bool isAdd)
 {
-int ret = -1;
 struct nlmsgerr *err;
 unsigned int recvbuflen;
-struct nl_msg *nl_msg;
 struct ndmsg ndm = { .ndm_family = PF_BRIDGE, .ndm_state = NUD_NOARP };
+VIR_AUTOPTR(virNlMsg) nl_msg = NULL;
 VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
 
 if (virNetDevGetIndex(ifname, _ifindex) < 0)
@@ -1103,7 +1097,7 @@ virNetDevBridgeFDBAddDel(const virMacAddr *mac, const 
char *ifname,
 
 if (virNetlinkCommand(nl_msg, , , 0, 0,
   NETLINK_ROUTE, 0) < 0) {
-goto cleanup;
+return -1;
 }
 if (recvbuflen < NLMSG_LENGTH(0) || resp == NULL)
 goto malformed_resp;
@@ -1116,7 +1110,7 @@ virNetDevBridgeFDBAddDel(const virMacAddr *mac, const 
char *ifname,
 if (err->error) {
 virReportSystemError(-err->error,
  _("error adding fdb entry for %s"), ifname);
-goto cleanup;
+return -1;
 }
 break;
 case NLMSG_DONE:
@@ -1126,20 +1120,17 @@ virNetDevBridgeFDBAddDel(const virMacAddr *mac, const 
char *ifname,
 goto malformed_resp;
 }
 
-ret = 0;
- cleanup:
-nlmsg_free(nl_msg);
-return ret;
+return 0;
 
  malformed_resp:
 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("malformed netlink response message"));
-goto cleanup;
+return -1;
 
  buffer_too_small:
 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("allocated netlink buffer is too small"));
-goto cleanup;
+return -1;
 }
 
 
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v3 10/11] util: netdevopenvswitch: use VIR_AUTOPTR for aggregate types

2018-08-09 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOPTR macro for declaring aggregate pointer variables,
majority of the calls to *Free functions can be dropped, which
in turn leads to getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virnetdevopenvswitch.c | 80 ++---
 1 file changed, 27 insertions(+), 53 deletions(-)

diff --git a/src/util/virnetdevopenvswitch.c b/src/util/virnetdevopenvswitch.c
index 9a9435f..a5de541 100644
--- a/src/util/virnetdevopenvswitch.c
+++ b/src/util/virnetdevopenvswitch.c
@@ -144,11 +144,10 @@ int virNetDevOpenvswitchAddPort(const char *brname, const 
char *ifname,
virNetDevVPortProfilePtr ovsport,
virNetDevVlanPtr virtVlan)
 {
-int ret = -1;
-virCommandPtr cmd = NULL;
 char macaddrstr[VIR_MAC_STRING_BUFLEN];
 char ifuuidstr[VIR_UUID_STRING_BUFLEN];
 char vmuuidstr[VIR_UUID_STRING_BUFLEN];
+VIR_AUTOPTR(virCommand) cmd = NULL;
 VIR_AUTOFREE(char *) attachedmac_ex_id = NULL;
 VIR_AUTOFREE(char *) ifaceid_ex_id = NULL;
 VIR_AUTOFREE(char *) profile_ex_id = NULL;
@@ -160,17 +159,17 @@ int virNetDevOpenvswitchAddPort(const char *brname, const 
char *ifname,
 
 if (virAsprintf(_ex_id, "external-ids:attached-mac=\"%s\"",
 macaddrstr) < 0)
-goto cleanup;
+return -1;
 if (virAsprintf(_ex_id, "external-ids:iface-id=\"%s\"",
 ifuuidstr) < 0)
-goto cleanup;
+return -1;
 if (virAsprintf(_ex_id, "external-ids:vm-id=\"%s\"",
 vmuuidstr) < 0)
-goto cleanup;
+return -1;
 if (ovsport->profileID[0] != '\0') {
 if (virAsprintf(_ex_id, "external-ids:port-profile=\"%s\"",
 ovsport->profileID) < 0)
-goto cleanup;
+return -1;
 }
 
 cmd = virCommandNew(OVSVSCTL);
@@ -179,7 +178,7 @@ int virNetDevOpenvswitchAddPort(const char *brname, const 
char *ifname,
  ifname, "--", "add-port", brname, ifname, NULL);
 
 if (virNetDevOpenvswitchConstructVlans(cmd, virtVlan) < 0)
-goto cleanup;
+return -1;
 
 if (ovsport->profileID[0] == '\0') {
 virCommandAddArgList(cmd,
@@ -204,13 +203,10 @@ int virNetDevOpenvswitchAddPort(const char *brname, const 
char *ifname,
 virReportError(VIR_ERR_INTERNAL_ERROR,
_("Unable to add port %s to OVS bridge %s"),
ifname, brname);
-goto cleanup;
+return -1;
 }
 
-ret = 0;
- cleanup:
-virCommandFree(cmd);
-return ret;
+return 0;
 }
 
 /**
@@ -223,8 +219,7 @@ int virNetDevOpenvswitchAddPort(const char *brname, const 
char *ifname,
  */
 int virNetDevOpenvswitchRemovePort(const char *brname ATTRIBUTE_UNUSED, const 
char *ifname)
 {
-int ret = -1;
-virCommandPtr cmd = NULL;
+VIR_AUTOPTR(virCommand) cmd = NULL;
 
 cmd = virCommandNew(OVSVSCTL);
 virNetDevOpenvswitchAddTimeout(cmd);
@@ -233,13 +228,10 @@ int virNetDevOpenvswitchRemovePort(const char *brname 
ATTRIBUTE_UNUSED, const ch
 if (virCommandRun(cmd, NULL) < 0) {
 virReportError(VIR_ERR_INTERNAL_ERROR,
_("Unable to delete port %s from OVS"), ifname);
-goto cleanup;
+return -1;
 }
 
-ret = 0;
- cleanup:
-virCommandFree(cmd);
-return ret;
+return 0;
 }
 
 /**
@@ -253,9 +245,8 @@ int virNetDevOpenvswitchRemovePort(const char *brname 
ATTRIBUTE_UNUSED, const ch
  */
 int virNetDevOpenvswitchGetMigrateData(char **migrate, const char *ifname)
 {
-virCommandPtr cmd = NULL;
 size_t len;
-int ret = -1;
+VIR_AUTOPTR(virCommand) cmd = NULL;
 
 cmd = virCommandNew(OVSVSCTL);
 virNetDevOpenvswitchAddTimeout(cmd);
@@ -269,7 +260,7 @@ int virNetDevOpenvswitchGetMigrateData(char **migrate, 
const char *ifname)
 virReportError(VIR_ERR_INTERNAL_ERROR,
_("Unable to run command to get OVS port data for "
  "interface %s"), ifname);
-goto cleanup;
+return -1;
 }
 
 /* Wipeout the newline, if it exists */
@@ -277,10 +268,7 @@ int virNetDevOpenvswitchGetMigrateData(char **migrate, 
const char *ifname)
 if (len > 0)
 (*migrate)[len - 1] = '\0';
 
-ret = 0;
- cleanup:
-virCommandFree(cmd);
-return ret;
+return 0;
 }
 
 /**
@@ -294,8 +282,7 @@ int virNetDevOpenvswitchGetMigrateData(char **migrate, 
const char *ifname)
  */
 int virNetDevOpenvswitchSetMigrateData(char *migrate, const char *ifname)
 {
-virCommandPtr cmd = NULL;
-int ret = -1;
+VIR_AUTOPTR(virCommand) cmd = NULL;
 
 if (!migrate) {
 VIR_DE

[libvirt] [PATCH v3 08/11] util: netdev: use VIR_AUTOPTR for aggregate types

2018-08-09 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOPTR macro for declaring aggregate pointer variables,
majority of the calls to *Free functions can be dropped, which
in turn leads to getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virnetdev.c | 249 +--
 1 file changed, 103 insertions(+), 146 deletions(-)

diff --git a/src/util/virnetdev.c b/src/util/virnetdev.c
index edb7393..d5aa94c 100644
--- a/src/util/virnetdev.c
+++ b/src/util/virnetdev.c
@@ -1182,27 +1182,21 @@ virNetDevIsPCIDevice(const char *devpath)
 static virPCIDevicePtr
 virNetDevGetPCIDevice(const char *devName)
 {
-virPCIDeviceAddressPtr vfPCIAddr = NULL;
-virPCIDevicePtr vfPCIDevice = NULL;
+VIR_AUTOPTR(virPCIDeviceAddress) vfPCIAddr = NULL;
 VIR_AUTOFREE(char *) vfSysfsDevicePath = NULL;
 
 if (virNetDevSysfsFile(, devName, "device") < 0)
-goto cleanup;
+return NULL;
 
 if (!virNetDevIsPCIDevice(vfSysfsDevicePath))
-goto cleanup;
+return NULL;
 
 vfPCIAddr = virPCIGetDeviceAddressFromSysfsLink(vfSysfsDevicePath);
 if (!vfPCIAddr)
-goto cleanup;
+return NULL;
 
-vfPCIDevice = virPCIDeviceNew(vfPCIAddr->domain, vfPCIAddr->bus,
-  vfPCIAddr->slot, vfPCIAddr->function);
-
- cleanup:
-VIR_FREE(vfPCIAddr);
-
-return vfPCIDevice;
+return virPCIDeviceNew(vfPCIAddr->domain, vfPCIAddr->bus,
+   vfPCIAddr->slot, vfPCIAddr->function);
 }
 
 
@@ -1601,12 +1595,12 @@ virNetDevSetVfConfig(const char *ifname, int vf,
 char macstr[VIR_MAC_STRING_BUFLEN];
 struct nlmsgerr *err;
 unsigned int recvbuflen = 0;
-struct nl_msg *nl_msg;
 struct nlattr *vfinfolist, *vfinfo;
 struct ifinfomsg ifinfo = {
 .ifi_family = AF_UNSPEC,
 .ifi_index  = -1,
 };
+VIR_AUTOPTR(virNlMsg) nl_msg = NULL;
 VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
 
 if (!macaddr && vlanid < 0)
@@ -1710,7 +1704,6 @@ virNetDevSetVfConfig(const char *ifname, int vf,
   macaddr ? virMacAddrFormat(macaddr, macstr) : "(unchanged)",
   vlanid, rc < 0 ? "Fail" : "Success");
 
-nlmsg_free(nl_msg);
 return rc;
 
  malformed_resp:
@@ -1849,12 +1842,11 @@ virNetDevSaveNetConfig(const char *linkdev, int vf,
const char *stateDir,
bool saveVlan)
 {
-int ret = -1;
 const char *pfDevName = NULL;
 virMacAddr oldMAC;
 char MACStr[VIR_MAC_STRING_BUFLEN];
 int oldVlanTag = -1;
-virJSONValuePtr configJSON = NULL;
+VIR_AUTOPTR(virJSONValue) configJSON = NULL;
 VIR_AUTOFREE(char *) pfDevOrig = NULL;
 VIR_AUTOFREE(char *) vfDevOrig = NULL;
 VIR_AUTOFREE(char *) filePath = NULL;
@@ -1866,7 +1858,7 @@ virNetDevSaveNetConfig(const char *linkdev, int vf,
 
 /* linkdev should get the VF's netdev name (or NULL if none) */
 if (virNetDevPFGetVF(pfDevName, vf, ) < 0)
-goto cleanup;
+return -1;
 
 linkdev = vfDevOrig;
 saveVlan = true;
@@ -1878,12 +1870,12 @@ virNetDevSaveNetConfig(const char *linkdev, int vf,
  */
 
 if (virNetDevGetPhysicalFunction(linkdev, ) < 0)
-goto cleanup;
+return -1;
 
 pfDevName = pfDevOrig;
 
 if (virNetDevGetVirtualFunctionIndex(pfDevName, linkdev, ) < 0)
-goto cleanup;
+return -1;
 }
 
 if (pfDevName) {
@@ -1901,7 +1893,7 @@ virNetDevSaveNetConfig(const char *linkdev, int vf,
  * explicitly enable the PF in the host system network config.
  */
 if (virNetDevGetOnline(pfDevName, ) < 0)
-goto cleanup;
+return -1;
 
 if (!pfIsOnline) {
 virReportError(VIR_ERR_INTERNAL_ERROR,
@@ -1910,12 +1902,12 @@ virNetDevSaveNetConfig(const char *linkdev, int vf,
  "change host network config to put the "
  "PF online."),
vf, pfDevName);
-goto cleanup;
+return -1;
 }
 }
 
 if (!(configJSON = virJSONValueNewObject()))
-goto cleanup;
+return -1;
 
 /* if there is a PF, it's now in pfDevName, and linkdev is either
  * the VF's name, or NULL (if the VF isn't bound to a net driver
@@ -1924,11 +1916,11 @@ virNetDevSaveNetConfig(const char *linkdev, int vf,
 
 if (pfDevName && saveVlan) {
 if (virAsprintf(, "%s/%s_vf%d", stateDir, pfDevName, vf) < 0)
-goto cleanup;
+return -1;
 
 /* get admin MAC and vlan tag */
 if (virNetDevGetVfConfig(pfDevName, vf, , ) < 0)
-goto cleanup;
+return -1;
 
 if (virJSONValueObjectAppendSt

[libvirt] [PATCH v3 07/11] util: netdev: use VIR_AUTOFREE instead of VIR_FREE for scalar types

2018-08-09 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOFREE macro for declaring scalar variables, majority
of the VIR_FREE calls can be dropped, which in turn leads to
getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virnetdev.c | 343 +++
 1 file changed, 125 insertions(+), 218 deletions(-)

diff --git a/src/util/virnetdev.c b/src/util/virnetdev.c
index 8eac419..edb7393 100644
--- a/src/util/virnetdev.c
+++ b/src/util/virnetdev.c
@@ -535,18 +535,17 @@ int virNetDevSetMTUFromDevice(const char *ifname,
  */
 int virNetDevSetNamespace(const char *ifname, pid_t pidInNs)
 {
-int ret = -1;
-char *pid = NULL;
-char *phy = NULL;
-char *phy_path = NULL;
 int len;
+VIR_AUTOFREE(char *) pid = NULL;
+VIR_AUTOFREE(char *) phy = NULL;
+VIR_AUTOFREE(char *) phy_path = NULL;
 
 if (virAsprintf(, "%lld", (long long) pidInNs) == -1)
 return -1;
 
 /* The 802.11 wireless devices only move together with their PHY. */
 if (virNetDevSysfsFile(_path, ifname, "phy80211/name") < 0)
-goto cleanup;
+return -1;
 
 if ((len = virFileReadAllQuiet(phy_path, 1024, )) <= 0) {
 /* Not a wireless device. */
@@ -556,7 +555,7 @@ int virNetDevSetNamespace(const char *ifname, pid_t pidInNs)
 
 argv[5] = pid;
 if (virRun(argv, NULL) < 0)
-goto cleanup;
+return -1;
 
 } else {
 const char *argv[] = {
@@ -569,15 +568,10 @@ int virNetDevSetNamespace(const char *ifname, pid_t 
pidInNs)
 argv[2] = phy;
 argv[5] = pid;
 if (virRun(argv, NULL) < 0)
-goto cleanup;
+return -1;
 }
 
-ret = 0;
- cleanup:
-VIR_FREE(phy_path);
-VIR_FREE(phy);
-VIR_FREE(pid);
-return ret;
+return 0;
 }
 
 #if defined(SIOCSIFNAME) && defined(HAVE_STRUCT_IFREQ)
@@ -969,25 +963,21 @@ int virNetDevGetIndex(const char *ifname ATTRIBUTE_UNUSED,
 int
 virNetDevGetMaster(const char *ifname, char **master)
 {
-int ret = -1;
-void *nlData = NULL;
 struct nlattr *tb[IFLA_MAX + 1] = {NULL, };
+VIR_AUTOFREE(void *) nlData = NULL;
 
 *master = NULL;
 
 if (virNetlinkDumpLink(ifname, -1, , tb, 0, 0) < 0)
-goto cleanup;
+return -1;
 
 if (tb[IFLA_MASTER]) {
 if (!(*master = virNetDevGetName(*(int *)RTA_DATA(tb[IFLA_MASTER]
-goto cleanup;
+return -1;
 }
 
 VIR_DEBUG("IFLA_MASTER for %s is %s", ifname, *master ? *master : 
"(none)");
-ret = 0;
- cleanup:
-VIR_FREE(nlData);
-return ret;
+return 0;
 }
 
 
@@ -1168,39 +1158,33 @@ virNetDevSysfsDeviceFile(char **pf_sysfs_device_link, 
const char *ifname,
 static bool
 virNetDevIsPCIDevice(const char *devpath)
 {
-char *subsys_link = NULL;
-char *abs_path = NULL;
 char *subsys = NULL;
-bool ret = false;
+VIR_AUTOFREE(char *) subsys_link = NULL;
+VIR_AUTOFREE(char *) abs_path = NULL;
 
 if (virAsprintf(_link, "%s/subsystem", devpath) < 0)
 return false;
 
 if (!virFileExists(subsys_link))
-goto cleanup;
+return false;
 
 if (virFileResolveLink(subsys_link, _path) < 0) {
 virReportError(VIR_ERR_INTERNAL_ERROR,
_("Unable to resolve device subsystem symlink %s"),
subsys_link);
-goto cleanup;
+return false;
 }
 
 subsys = last_component(abs_path);
-ret = STRPREFIX(subsys, "pci");
-
- cleanup:
-VIR_FREE(subsys_link);
-VIR_FREE(abs_path);
-return ret;
+return STRPREFIX(subsys, "pci");
 }
 
 static virPCIDevicePtr
 virNetDevGetPCIDevice(const char *devName)
 {
-char *vfSysfsDevicePath = NULL;
 virPCIDeviceAddressPtr vfPCIAddr = NULL;
 virPCIDevicePtr vfPCIDevice = NULL;
+VIR_AUTOFREE(char *) vfSysfsDevicePath = NULL;
 
 if (virNetDevSysfsFile(, devName, "device") < 0)
 goto cleanup;
@@ -1216,7 +1200,6 @@ virNetDevGetPCIDevice(const char *devName)
   vfPCIAddr->slot, vfPCIAddr->function);
 
  cleanup:
-VIR_FREE(vfSysfsDevicePath);
 VIR_FREE(vfPCIAddr);
 
 return vfPCIDevice;
@@ -1241,25 +1224,20 @@ int
 virNetDevGetPhysPortID(const char *ifname,
char **physPortID)
 {
-int ret = -1;
-char *physPortIDFile = NULL;
+VIR_AUTOFREE(char *) physPortIDFile = NULL;
 
 *physPortID = NULL;
 
 if (virNetDevSysfsFile(, ifname, "phys_port_id") < 0)
-goto cleanup;
+return -1;
 
 /* a failure to read just means the driver doesn't support
- * phys_port_id, so set success now and ignore the return from
- * virFileReadAllQuiet().
+ * phys_port_id, so ignore the return from virFileReadAllQuiet()
+ * and return success.

[libvirt] [PATCH v3 11/11] util: qemu: use VIR_AUTOPTR for aggregate types

2018-08-09 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOPTR macro for declaring aggregate pointer variables,
majority of the calls to *Free functions can be dropped, which
in turn leads to getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virqemu.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/src/util/virqemu.c b/src/util/virqemu.c
index bc78853..1cd2b93 100644
--- a/src/util/virqemu.c
+++ b/src/util/virqemu.c
@@ -281,6 +281,7 @@ virQEMUBuildDriveCommandlineFromJSON(virJSONValuePtr srcdef)
  cleanup:
 virBufferFreeAndReset();
 return ret;
+
 }
 
 
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v3 01/11] util: iscsi: use VIR_AUTOFREE instead of VIR_FREE for scalar types

2018-08-09 Thread Sukrit Bhatnagar
Add another usage for VIR_AUTOFREE macro which was left in the
commit ec3e878, thereby dropping a VIR_FREE call and and a cleanup
section.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/viriscsi.c | 22 ++
 1 file changed, 10 insertions(+), 12 deletions(-)

diff --git a/src/util/viriscsi.c b/src/util/viriscsi.c
index c805ffc..cf07968 100644
--- a/src/util/viriscsi.c
+++ b/src/util/viriscsi.c
@@ -208,9 +208,10 @@ static int
 virStorageBackendCreateIfaceIQN(const char *initiatoriqn,
 char **ifacename)
 {
-int ret = -1, exitstatus = -1;
+int exitstatus = -1;
+VIR_AUTOPTR(virCommand) cmd = NULL;
+VIR_AUTOFREE(char *) iface_name = NULL;
 VIR_AUTOFREE(char *) temp_ifacename = NULL;
-VIR_AUTOPTR(virCommand) cmd = NULL;
 
 if (virAsprintf(_ifacename,
 "libvirt-iface-%08llx",
@@ -233,7 +234,7 @@ virStorageBackendCreateIfaceIQN(const char *initiatoriqn,
 virReportError(VIR_ERR_INTERNAL_ERROR,
_("Failed to run command '%s' to create new iscsi 
interface"),
ISCSIADM);
-goto cleanup;
+return -1;
 }
 virCommandFree(cmd);
 
@@ -252,26 +253,23 @@ virStorageBackendCreateIfaceIQN(const char *initiatoriqn,
 virReportError(VIR_ERR_INTERNAL_ERROR,
_("Failed to run command '%s' to update iscsi interface 
with IQN '%s'"),
ISCSIADM, initiatoriqn);
-goto cleanup;
+return -1;
 }
 
 /* Check again to make sure the interface was created. */
-if (virStorageBackendIQNFound(initiatoriqn, ifacename) != IQN_FOUND) {
+if (virStorageBackendIQNFound(initiatoriqn, _name) != IQN_FOUND) {
 VIR_DEBUG("Failed to find interface '%s' with IQN '%s' "
   "after attempting to create it",
   _ifacename[0], initiatoriqn);
-goto cleanup;
+return -1;
 } else {
 VIR_DEBUG("Interface '%s' with IQN '%s' was created successfully",
-  *ifacename, initiatoriqn);
+  iface_name, initiatoriqn);
 }
 
-ret = 0;
+VIR_STEAL_PTR(*ifacename, iface_name);
 
- cleanup:
-if (ret != 0)
-VIR_FREE(*ifacename);
-return ret;
+return 0;
 }
 
 
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v3 03/11] util: netlink: use VIR_AUTOFREE instead of VIR_FREE for scalar types

2018-08-09 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOFREE macro for declaring scalar variables, majority
of the VIR_FREE calls can be dropped, which in turn leads to
getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virnetlink.c | 43 ---
 1 file changed, 20 insertions(+), 23 deletions(-)

diff --git a/src/util/virnetlink.c b/src/util/virnetlink.c
index ecf62c9..fcdc09d 100644
--- a/src/util/virnetlink.c
+++ b/src/util/virnetlink.c
@@ -342,10 +342,8 @@ virNetlinkDumpCommand(struct nl_msg *nl_msg,
   unsigned int protocol, unsigned int groups,
   void *opaque)
 {
-int ret = -1;
 bool end = false;
 int len = 0;
-struct nlmsghdr *resp = NULL;
 struct nlmsghdr *msg = NULL;
 
 struct sockaddr_nl nladdr = {
@@ -357,9 +355,11 @@ virNetlinkDumpCommand(struct nl_msg *nl_msg,
 
 if (!(nlhandle = virNetlinkSendRequest(nl_msg, src_pid, nladdr,
protocol, groups)))
-goto cleanup;
+return -1;
 
 while (!end) {
+VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
+
 len = nl_recv(nlhandle, , (unsigned char **), NULL);
 VIR_WARNINGS_NO_CAST_ALIGN
 for (msg = resp; NLMSG_OK(msg, len); msg = NLMSG_NEXT(msg, len)) {
@@ -368,19 +368,14 @@ virNetlinkDumpCommand(struct nl_msg *nl_msg,
 end = true;
 
 if (virNetlinkGetErrorCode(msg, len) < 0)
-goto cleanup;
+return -1;
 
 if (callback(msg, opaque) < 0)
-goto cleanup;
+return -1;
 }
-VIR_FREE(resp);
 }
 
-ret = 0;
-
- cleanup:
-VIR_FREE(resp);
-return ret;
+return 0;
 }
 
 /**
@@ -408,7 +403,6 @@ virNetlinkDumpLink(const char *ifname, int ifindex,
uint32_t src_pid, uint32_t dst_pid)
 {
 int rc = -1;
-struct nlmsghdr *resp = NULL;
 struct nlmsgerr *err;
 struct ifinfomsg ifinfo = {
 .ifi_family = AF_UNSPEC,
@@ -416,6 +410,9 @@ virNetlinkDumpLink(const char *ifname, int ifindex,
 };
 unsigned int recvbuflen;
 struct nl_msg *nl_msg;
+VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
+
+*nlData = NULL;
 
 if (ifname && ifindex <= 0 && virNetDevGetIndex(ifname, ) < 0)
 return -1;
@@ -483,12 +480,12 @@ virNetlinkDumpLink(const char *ifname, int ifindex,
 default:
 goto malformed_resp;
 }
+
+VIR_STEAL_PTR(*nlData, resp);
 rc = 0;
+
  cleanup:
 nlmsg_free(nl_msg);
-if (rc < 0)
-   VIR_FREE(resp);
-*nlData = resp;
 return rc;
 
  malformed_resp:
@@ -522,11 +519,11 @@ int
 virNetlinkDelLink(const char *ifname, virNetlinkDelLinkFallback fallback)
 {
 int rc = -1;
-struct nlmsghdr *resp = NULL;
 struct nlmsgerr *err;
 struct ifinfomsg ifinfo = { .ifi_family = AF_UNSPEC };
 unsigned int recvbuflen;
 struct nl_msg *nl_msg;
+VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
 
 nl_msg = nlmsg_alloc_simple(RTM_DELLINK,
 NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL);
@@ -577,7 +574,6 @@ virNetlinkDelLink(const char *ifname, 
virNetlinkDelLinkFallback fallback)
 rc = 0;
  cleanup:
 nlmsg_free(nl_msg);
-VIR_FREE(resp);
 return rc;
 
  malformed_resp:
@@ -610,13 +606,15 @@ int
 virNetlinkGetNeighbor(void **nlData, uint32_t src_pid, uint32_t dst_pid)
 {
 int rc = -1;
-struct nlmsghdr *resp = NULL;
 struct nlmsgerr *err;
 struct ndmsg ndinfo = {
 .ndm_family = AF_UNSPEC,
 };
 unsigned int recvbuflen;
 struct nl_msg *nl_msg;
+VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
+
+*nlData = NULL;
 
 nl_msg = nlmsg_alloc_simple(RTM_GETNEIGH, NLM_F_DUMP | NLM_F_REQUEST);
 if (!nl_msg) {
@@ -654,13 +652,12 @@ virNetlinkGetNeighbor(void **nlData, uint32_t src_pid, 
uint32_t dst_pid)
 default:
 goto malformed_resp;
 }
+
+VIR_STEAL_PTR(*nlData, resp);
 rc = recvbuflen;
 
  cleanup:
 nlmsg_free(nl_msg);
-if (rc < 0)
-   VIR_FREE(resp);
-*nlData = resp;
 return rc;
 
  malformed_resp:
@@ -766,12 +763,12 @@ virNetlinkEventCallback(int watch,
 void *opaque)
 {
 virNetlinkEventSrvPrivatePtr srv = opaque;
-struct nlmsghdr *msg;
 struct sockaddr_nl peer;
 struct ucred *creds = NULL;
 size_t i;
 int length;
 bool handled = false;
+VIR_AUTOFREE(struct nlmsghdr *) msg = NULL;
 
 length = nl_recv(srv->netlinknh, ,
  (unsigned char **), );
@@ -801,7 +798,7 @@ virNetlinkEventCallback(int watch,
 
 if (!handled)
 VIR_DEBUG("event not handled.");
-VIR_FREE(msg);
+
 virNetlinkEventServerUnlock(srv);
 }
 
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v3 04/11] util: netlink: use VIR_AUTOPTR for aggregate types

2018-08-09 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOPTR macro for declaring aggregate pointer variables,
majority of the calls to *Free functions can be dropped, which
in turn leads to getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virnetlink.c | 72 ---
 1 file changed, 28 insertions(+), 44 deletions(-)

diff --git a/src/util/virnetlink.c b/src/util/virnetlink.c
index fcdc09d..66e80e2 100644
--- a/src/util/virnetlink.c
+++ b/src/util/virnetlink.c
@@ -297,15 +297,16 @@ int virNetlinkCommand(struct nl_msg *nl_msg,
   uint32_t src_pid, uint32_t dst_pid,
   unsigned int protocol, unsigned int groups)
 {
-int ret = -1;
 struct sockaddr_nl nladdr = {
 .nl_family = AF_NETLINK,
 .nl_pid= dst_pid,
 .nl_groups = 0,
 };
 struct pollfd fds[1];
-VIR_AUTOPTR(virNetlinkHandle) nlhandle = NULL;
 int len = 0;
+VIR_AUTOPTR(virNetlinkHandle) nlhandle = NULL;
+
+*respbuflen = 0;
 
 memset(fds, 0, sizeof(fds));
 
@@ -324,15 +325,12 @@ int virNetlinkCommand(struct nl_msg *nl_msg,
 goto cleanup;
 }
 
-ret = 0;
 *respbuflen = len;
- cleanup:
-if (ret < 0) {
-*resp = NULL;
-*respbuflen = 0;
-}
+return 0;
 
-return ret;
+ cleanup:
+*resp = NULL;
+return -1;
 }
 
 int
@@ -409,7 +407,7 @@ virNetlinkDumpLink(const char *ifname, int ifindex,
 .ifi_index  = ifindex
 };
 unsigned int recvbuflen;
-struct nl_msg *nl_msg;
+VIR_AUTOPTR(virNlMsg) nl_msg = NULL;
 VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
 
 *nlData = NULL;
@@ -450,7 +448,7 @@ virNetlinkDumpLink(const char *ifname, int ifindex,
 
 if (virNetlinkCommand(nl_msg, , ,
   src_pid, dst_pid, NETLINK_ROUTE, 0) < 0)
-goto cleanup;
+return -1;
 
 if (recvbuflen < NLMSG_LENGTH(0) || resp == NULL)
 goto malformed_resp;
@@ -465,7 +463,7 @@ virNetlinkDumpLink(const char *ifname, int ifindex,
 virReportSystemError(-err->error,
  _("error dumping %s (%d) interface"),
  ifname, ifindex);
-goto cleanup;
+return -1;
 }
 break;
 
@@ -482,21 +480,17 @@ virNetlinkDumpLink(const char *ifname, int ifindex,
 }
 
 VIR_STEAL_PTR(*nlData, resp);
-rc = 0;
-
- cleanup:
-nlmsg_free(nl_msg);
-return rc;
+return 0;
 
  malformed_resp:
 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("malformed netlink response message"));
-goto cleanup;
+return rc;
 
  buffer_too_small:
 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("allocated netlink buffer is too small"));
-goto cleanup;
+return rc;
 }
 
 
@@ -518,11 +512,10 @@ virNetlinkDumpLink(const char *ifname, int ifindex,
 int
 virNetlinkDelLink(const char *ifname, virNetlinkDelLinkFallback fallback)
 {
-int rc = -1;
 struct nlmsgerr *err;
 struct ifinfomsg ifinfo = { .ifi_family = AF_UNSPEC };
 unsigned int recvbuflen;
-struct nl_msg *nl_msg;
+VIR_AUTOPTR(virNlMsg) nl_msg = NULL;
 VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
 
 nl_msg = nlmsg_alloc_simple(RTM_DELLINK,
@@ -540,7 +533,7 @@ virNetlinkDelLink(const char *ifname, 
virNetlinkDelLinkFallback fallback)
 
 if (virNetlinkCommand(nl_msg, , , 0, 0,
   NETLINK_ROUTE, 0) < 0) {
-goto cleanup;
+return -1;
 }
 
 if (recvbuflen < NLMSG_LENGTH(0) || resp == NULL)
@@ -552,15 +545,14 @@ virNetlinkDelLink(const char *ifname, 
virNetlinkDelLinkFallback fallback)
 if (resp->nlmsg_len < NLMSG_LENGTH(sizeof(*err)))
 goto malformed_resp;
 
-if (-err->error == EOPNOTSUPP && fallback) {
-rc = fallback(ifname);
-goto cleanup;
-}
+if (-err->error == EOPNOTSUPP && fallback)
+return fallback(ifname);
+
 if (err->error) {
 virReportSystemError(-err->error,
  _("error destroying network device %s"),
  ifname);
-goto cleanup;
+return -1;
 }
 break;
 
@@ -571,20 +563,17 @@ virNetlinkDelLink(const char *ifname, 
virNetlinkDelLinkFallback fallback)
 goto malformed_resp;
 }
 
-rc = 0;
- cleanup:
-nlmsg_free(nl_msg);
-return rc;
+return 0;
 
  malformed_resp:
 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("malformed netlink response message"));
-goto cleanup;
+return -1;
 
  buffer_too_small:
 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_(&quo

[libvirt] [PATCH v3 05/11] util: netdevbridge: use VIR_AUTOFREE instead of VIR_FREE for scalar types

2018-08-09 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOFREE macro for declaring scalar variables, majority
of the VIR_FREE calls can be dropped, which in turn leads to
getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virnetdevbridge.c | 46 --
 1 file changed, 16 insertions(+), 30 deletions(-)

diff --git a/src/util/virnetdevbridge.c b/src/util/virnetdevbridge.c
index e46ac35..fe3697b 100644
--- a/src/util/virnetdevbridge.c
+++ b/src/util/virnetdevbridge.c
@@ -126,8 +126,7 @@ static int virNetDevBridgeSet(const char *brname,
   int fd, /* control socket */
   struct ifreq *ifr)  /* pre-filled bridge 
name */
 {
-char *path = NULL;
-int ret = -1;
+VIR_AUTOFREE(char *) path = NULL;
 
 if (virAsprintf(, SYSFS_NET_DIR "%s/bridge/%s", brname, paramname) < 
0)
 return -1;
@@ -138,7 +137,7 @@ static int virNetDevBridgeSet(const char *brname,
 if (virFileWriteStr(path, valuestr, 0) < 0) {
 virReportSystemError(errno,
  _("Unable to set bridge %s %s"), brname, 
paramname);
-goto cleanup;
+return -1;
 }
 } else {
 unsigned long paramid;
@@ -149,21 +148,18 @@ static int virNetDevBridgeSet(const char *brname,
 } else {
 virReportSystemError(EINVAL,
  _("Unable to set bridge %s %s"), brname, 
paramname);
-goto cleanup;
+return -1;
 }
 unsigned long args[] = { paramid, value, 0, 0 };
 ifr->ifr_data = (char*)
 if (ioctl(fd, SIOCDEVPRIVATE, ifr) < 0) {
 virReportSystemError(errno,
  _("Unable to set bridge %s %s"), brname, 
paramname);
-goto cleanup;
+return -1;
 }
 }
 
-ret = 0;
- cleanup:
-VIR_FREE(path);
-return ret;
+return 0;
 }
 
 
@@ -171,16 +167,17 @@ static int virNetDevBridgeGet(const char *brname,
   const char *paramname,  /* sysfs param name */
   unsigned long *value)   /* current value */
 {
-char *path = NULL;
 int ret = -1;
 int fd = -1;
 struct ifreq ifr;
+VIR_AUTOFREE(char *) path = NULL;
 
 if (virAsprintf(, SYSFS_NET_DIR "%s/bridge/%s", brname, paramname) < 
0)
 return -1;
 
 if (virFileExists(path)) {
-char *valuestr;
+VIR_AUTOFREE(char *) valuestr = NULL;
+
 if (virFileReadAll(path, INT_BUFSIZE_BOUND(unsigned long),
) < 0)
 goto cleanup;
@@ -189,10 +186,8 @@ static int virNetDevBridgeGet(const char *brname,
 virReportSystemError(EINVAL,
  _("Unable to get bridge %s %s"),
  brname, paramname);
-VIR_FREE(valuestr);
 goto cleanup;
 }
-VIR_FREE(valuestr);
 } else {
 struct __bridge_info info;
 unsigned long args[] = { BRCTL_GET_BRIDGE_INFO, (unsigned long), 
0, 0 };
@@ -221,7 +216,6 @@ static int virNetDevBridgeGet(const char *brname,
 ret = 0;
  cleanup:
 VIR_FORCE_CLOSE(fd);
-VIR_FREE(path);
 return ret;
 }
 #endif /* __linux__ */
@@ -233,9 +227,9 @@ virNetDevBridgePortSet(const char *brname,
const char *paramname,
unsigned long value)
 {
-char *path = NULL;
 char valuestr[INT_BUFSIZE_BOUND(value)];
 int ret = -1;
+VIR_AUTOFREE(char *) path = NULL;
 
 snprintf(valuestr, sizeof(valuestr), "%lu", value);
 
@@ -254,7 +248,6 @@ virNetDevBridgePortSet(const char *brname,
  brname, ifname, paramname, valuestr);
 }
 
-VIR_FREE(path);
 return ret;
 }
 
@@ -265,29 +258,24 @@ virNetDevBridgePortGet(const char *brname,
const char *paramname,
unsigned long *value)
 {
-char *path = NULL;
-char *valuestr = NULL;
-int ret = -1;
+VIR_AUTOFREE(char *) path = NULL;
+VIR_AUTOFREE(char *) valuestr = NULL;
 
 if (virAsprintf(, SYSFS_NET_DIR "%s/brif/%s/%s",
 brname, ifname, paramname) < 0)
 return -1;
 
 if (virFileReadAll(path, INT_BUFSIZE_BOUND(unsigned long), ) < 0)
-goto cleanup;
+return -1;
 
 if (virStrToLong_ul(valuestr, NULL, 10, value) < 0) {
 virReportSystemError(EINVAL,
  _("Unable to get bridge %s port %s %s"),
  brname, ifname, paramname);
-goto cleanup;
+return -1;
 }
 
-ret = 0;
- cleanup:
-VIR_FREE(path);
-VIR_FREE(valuestr);
-return ret;
+return 0;
 }
 
 
@@ -430,1

[libvirt] [PATCH v3 00/11] use GNU C's cleanup attribute in src/util (batch III)

2018-08-09 Thread Sukrit Bhatnagar
This third series of patches also modifies a few files in src/util
to use VIR_AUTOFREE and VIR_AUTOPTR for automatic freeing of memory
and get rid of some VIR_FREE macro invocations and *Free function
calls.

This is meant as a follow-up of the v1 series [1] of the same batch,
and contains those patches which were not (completely) pushed upstream.


[1] https://www.redhat.com/archives/libvir-list/2018-July/msg01947.html

Sukrit Bhatnagar (11):
  util: iscsi: use VIR_AUTOFREE instead of VIR_FREE for scalar types
  util: netlink: define cleanup function using VIR_DEFINE_AUTOPTR_FUNC
  util: netlink: use VIR_AUTOFREE instead of VIR_FREE for scalar types
  util: netlink: use VIR_AUTOPTR for aggregate types
  util: netdevbridge: use VIR_AUTOFREE instead of VIR_FREE for scalar
types
  util: netdevbridge: use VIR_AUTOPTR for aggregate types
  util: netdev: use VIR_AUTOFREE instead of VIR_FREE for scalar types
  util: netdev: use VIR_AUTOPTR for aggregate types
  util: netdevip: use VIR_AUTOPTR for aggregate types
  util: netdevopenvswitch: use VIR_AUTOPTR for aggregate types
  util: qemu: use VIR_AUTOPTR for aggregate types

 src/util/viriscsi.c |  22 +-
 src/util/virnetdev.c| 592 
 src/util/virnetdevbridge.c  |  81 ++
 src/util/virnetdevip.c  | 130 -
 src/util/virnetdevopenvswitch.c |  80 ++
 src/util/virnetlink.c   | 112 
 src/util/virnetlink.h   |   5 +
 src/util/virqemu.c  |   1 +
 8 files changed, 397 insertions(+), 626 deletions(-)

-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v3 02/11] util: netlink: define cleanup function using VIR_DEFINE_AUTOPTR_FUNC

2018-08-09 Thread Sukrit Bhatnagar
Using the new VIR_DEFINE_AUTOPTR_FUNC macro defined in
src/util/viralloc.h, define a new wrapper around an existing
cleanup function which will be called when a variable declared
with VIR_AUTOPTR macro goes out of scope. Also, drop the redundant
viralloc.h include, since that has moved from the source module into
the header.

This commit also typedefs virNlMsg to struct nl_msg type for use
with the cleanup macros.

When a variable of type virNlMsg * is declared using VIR_AUTOPTR,
the function nlmsg_free will be run automatically on it when it
goes out of scope.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virnetlink.c | 1 -
 src/util/virnetlink.h | 5 +
 2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/src/util/virnetlink.c b/src/util/virnetlink.c
index 162efe6..ecf62c9 100644
--- a/src/util/virnetlink.c
+++ b/src/util/virnetlink.c
@@ -38,7 +38,6 @@
 #include "virnetlink.h"
 #include "virnetdev.h"
 #include "virlog.h"
-#include "viralloc.h"
 #include "virthread.h"
 #include "virmacaddr.h"
 #include "virerror.h"
diff --git a/src/util/virnetlink.h b/src/util/virnetlink.h
index 2a9de0a..8ebeab8 100644
--- a/src/util/virnetlink.h
+++ b/src/util/virnetlink.h
@@ -22,6 +22,7 @@
 
 # include "internal.h"
 # include "virmacaddr.h"
+# include "viralloc.h"
 
 # if defined(__linux__) && defined(HAVE_LIBNL)
 
@@ -44,6 +45,8 @@ struct nlmsghdr;
 
 # endif /* __linux__ */
 
+typedef struct nl_msg virNlMsg;
+
 int virNetlinkStartup(void);
 void virNetlinkShutdown(void);
 
@@ -123,4 +126,6 @@ int virNetlinkEventAddClient(virNetlinkEventHandleCallback 
handleCB,
 int virNetlinkEventRemoveClient(int watch, const virMacAddr *macaddr,
 unsigned int protocol);
 
+VIR_DEFINE_AUTOPTR_FUNC(virNlMsg, nlmsg_free)
+
 #endif /* __VIR_NETLINK_H__ */
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v2 28/35] util: numa: use VIR_AUTOPTR for aggregate types

2018-08-08 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOPTR macro for declaring aggregate pointer variables,
majority of the calls to *Free functions can be dropped, which
in turn leads to getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virnuma.c | 28 +---
 1 file changed, 9 insertions(+), 19 deletions(-)

diff --git a/src/util/virnuma.c b/src/util/virnuma.c
index 6c6be1c..c1457be 100644
--- a/src/util/virnuma.c
+++ b/src/util/virnuma.c
@@ -57,8 +57,8 @@ char *
 virNumaGetAutoPlacementAdvice(unsigned short vcpus,
   unsigned long long balloon)
 {
-virCommandPtr cmd = NULL;
 char *output = NULL;
+VIR_AUTOPTR(virCommand) cmd = NULL;
 
 cmd = virCommandNewArgList(NUMAD, "-w", NULL);
 virCommandAddArgFormat(cmd, "%d:%llu", vcpus,
@@ -71,7 +71,6 @@ virNumaGetAutoPlacementAdvice(unsigned short vcpus,
_("Failed to query numad for the "
  "advisory nodeset"));
 
-virCommandFree(cmd);
 return output;
 }
 #else /* !HAVE_NUMAD */
@@ -252,41 +251,38 @@ int
 virNumaGetNodeCPUs(int node,
virBitmapPtr *cpus)
 {
+VIR_AUTOPTR(virBitmap) cpumap = NULL;
 VIR_AUTOFREE(unsigned long *) mask = NULL;
 VIR_AUTOFREE(unsigned long *) allonesmask = NULL;
-virBitmapPtr cpumap = NULL;
 int ncpus = 0;
 int max_n_cpus = virNumaGetMaxCPUs();
 int mask_n_bytes = max_n_cpus / 8;
 size_t i;
-int ret = -1;
 
 *cpus = NULL;
 
 if (VIR_ALLOC_N(mask, mask_n_bytes / sizeof(*mask)) < 0)
-goto cleanup;
+return -1;
 
 if (VIR_ALLOC_N(allonesmask, mask_n_bytes / sizeof(*mask)) < 0)
-goto cleanup;
+return -1;
 
 memset(allonesmask, 0xff, mask_n_bytes);
 
 /* The first time this returns -1, ENOENT if node doesn't exist... */
 if (numa_node_to_cpus(node, mask, mask_n_bytes) < 0) {
 VIR_WARN("NUMA topology for cell %d is not available, ignoring", node);
-ret = -2;
-goto cleanup;
+return -2;
 }
 
 /* second, third... times it returns an all-1's mask */
 if (memcmp(mask, allonesmask, mask_n_bytes) == 0) {
 VIR_DEBUG("NUMA topology for cell %d is invalid, ignoring", node);
-ret = -2;
-goto cleanup;
+return -2;
 }
 
 if (!(cpumap = virBitmapNew(max_n_cpus)))
-goto cleanup;
+return -1;
 
 for (i = 0; i < max_n_cpus; i++) {
 if (MASK_CPU_ISSET(mask, i)) {
@@ -295,14 +291,8 @@ virNumaGetNodeCPUs(int node,
 }
 }
 
-*cpus = cpumap;
-cpumap = NULL;
-ret = ncpus;
-
- cleanup:
-virBitmapFree(cpumap);
-
-return ret;
+VIR_STEAL_PTR(*cpus, cpumap);
+return ncpus;
 }
 # undef MASK_CPU_ISSET
 # undef n_bits
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v2 21/35] util: netdevmacvlan: use VIR_AUTOPTR for aggregate types

2018-08-08 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOPTR macro for declaring aggregate pointer variables,
majority of the calls to *Free functions can be dropped, which
in turn leads to getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
Reviewed-by: Erik Skultety 
---
 src/util/virnetdevmacvlan.c | 9 +++--
 1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/src/util/virnetdevmacvlan.c b/src/util/virnetdevmacvlan.c
index a2ed65c..100507f 100644
--- a/src/util/virnetdevmacvlan.c
+++ b/src/util/virnetdevmacvlan.c
@@ -1184,9 +1184,9 @@ int virNetDevMacVLanDeleteWithVPortProfile(const char 
*ifname,
 }
 
 if (mode == VIR_NETDEV_MACVLAN_MODE_PASSTHRU) {
-virMacAddrPtr MAC = NULL;
-virMacAddrPtr adminMAC = NULL;
-virNetDevVlanPtr vlan = NULL;
+VIR_AUTOPTR(virMacAddr) MAC = NULL;
+VIR_AUTOPTR(virMacAddr) adminMAC = NULL;
+VIR_AUTOPTR(virNetDevVlan) vlan = NULL;
 
 if ((virNetDevReadNetConfig(linkdev, -1, stateDir,
 , , ) == 0) &&
@@ -1194,9 +1194,6 @@ int virNetDevMacVLanDeleteWithVPortProfile(const char 
*ifname,
 
 ignore_value(virNetDevSetNetConfig(linkdev, -1,
adminMAC, vlan, MAC, !!vlan));
-VIR_FREE(MAC);
-VIR_FREE(adminMAC);
-virNetDevVlanFree(vlan);
 }
 }
 
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v2 18/35] util: netdevip: use VIR_AUTOPTR for aggregate types

2018-08-08 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOPTR macro for declaring aggregate pointer variables,
majority of the calls to *Free functions can be dropped, which
in turn leads to getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virnetdevip.c | 125 +++--
 1 file changed, 48 insertions(+), 77 deletions(-)

diff --git a/src/util/virnetdevip.c b/src/util/virnetdevip.c
index 78c6dcf..df19d16 100644
--- a/src/util/virnetdevip.c
+++ b/src/util/virnetdevip.c
@@ -168,10 +168,9 @@ virNetDevIPAddrAdd(const char *ifname,
virSocketAddr *peer,
unsigned int prefix)
 {
-virSocketAddr *broadcast = NULL;
-int ret = -1;
-struct nl_msg *nlmsg = NULL;
 unsigned int recvbuflen;
+VIR_AUTOPTR(virNlMsg) nlmsg = NULL;
+VIR_AUTOPTR(virSocketAddr) broadcast = NULL;
 VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
 VIR_AUTOFREE(char *) ipStr = NULL;
 VIR_AUTOFREE(char *) peerStr = NULL;
@@ -186,13 +185,13 @@ virNetDevIPAddrAdd(const char *ifname,
 !(peer && VIR_SOCKET_ADDR_VALID(peer))) {
 /* compute a broadcast address if this is IPv4 */
 if (VIR_ALLOC(broadcast) < 0)
-goto cleanup;
+return -1;
 
 if (virSocketAddrBroadcastByPrefix(addr, prefix, broadcast) < 0) {
-virReportError(VIR_ERR_INTERNAL_ERROR,
-   _("Failed to determine broadcast address for '%s/%d'"),
-   ipStr, prefix);
-goto cleanup;
+virReportError(VIR_ERR_INTERNAL_ERROR,
+   _("Failed to determine broadcast address for 
'%s/%d'"),
+   ipStr, prefix);
+return -1;
 }
 bcastStr = virSocketAddrFormat(broadcast);
 }
@@ -206,11 +205,11 @@ virNetDevIPAddrAdd(const char *ifname,
 if (!(nlmsg = virNetDevCreateNetlinkAddressMessage(RTM_NEWADDR, ifname,
addr, prefix,
broadcast, peer)))
-goto cleanup;
+return -1;
 
 if (virNetlinkCommand(nlmsg, , ,
   0, 0, NETLINK_ROUTE, 0) < 0)
-goto cleanup;
+return -1;
 
 
 if (virNetlinkGetErrorCode(resp, recvbuflen) < 0) {
@@ -220,14 +219,10 @@ virNetDevIPAddrAdd(const char *ifname,
peerStr ? " peer " : "", peerStr ? peerStr : "",
bcastStr ? " bcast " : "", bcastStr ? bcastStr : "",
ifname);
-goto cleanup;
+return -1;
 }
 
-ret = 0;
- cleanup:
-nlmsg_free(nlmsg);
-VIR_FREE(broadcast);
-return ret;
+return 0;
 }
 
 
@@ -246,30 +241,26 @@ virNetDevIPAddrDel(const char *ifname,
virSocketAddr *addr,
unsigned int prefix)
 {
-int ret = -1;
-struct nl_msg *nlmsg = NULL;
 unsigned int recvbuflen;
+VIR_AUTOPTR(virNlMsg) nlmsg = NULL;
 VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
 
 if (!(nlmsg = virNetDevCreateNetlinkAddressMessage(RTM_DELADDR, ifname,
addr, prefix,
NULL, NULL)))
-goto cleanup;
+return -1;
 
 if (virNetlinkCommand(nlmsg, , , 0, 0,
   NETLINK_ROUTE, 0) < 0)
-goto cleanup;
+return -1;
 
 if (virNetlinkGetErrorCode(resp, recvbuflen) < 0) {
 virReportError(VIR_ERR_SYSTEM_ERROR,
_("Error removing IP address from %s"), ifname);
-goto cleanup;
+return -1;
 }
 
-ret = 0;
- cleanup:
-nlmsg_free(nlmsg);
-return ret;
+return 0;
 }
 
 
@@ -292,8 +283,6 @@ virNetDevIPRouteAdd(const char *ifname,
 virSocketAddrPtr gateway,
 unsigned int metric)
 {
-int ret = -1;
-struct nl_msg *nlmsg = NULL;
 struct nlmsghdr *resp = NULL;
 unsigned int recvbuflen;
 unsigned int ifindex;
@@ -304,6 +293,7 @@ virNetDevIPRouteAdd(const char *ifname,
 int errCode;
 virSocketAddr defaultAddr;
 virSocketAddrPtr actualAddr;
+VIR_AUTOPTR(virNlMsg) nlmsg = NULL;
 VIR_AUTOFREE(char *) toStr = NULL;
 VIR_AUTOFREE(char *) viaStr = NULL;
 
@@ -315,10 +305,10 @@ virNetDevIPRouteAdd(const char *ifname,
 int family = VIR_SOCKET_ADDR_FAMILY(gateway);
 if (family == AF_INET) {
 if (virSocketAddrParseIPv4(, VIR_SOCKET_ADDR_IPV4_ALL) 
< 0)
-goto cleanup;
+return -1;
 } else {
 if (virSocketAddrParseIPv6(, VIR_SOCKET_ADDR_IPV6_ALL) 
< 0)
-goto cleanup;
+return -1;
 }
 
 actualAddr =

[libvirt] [PATCH v2 35/35] util: qemu: use VIR_AUTOPTR for aggregate types

2018-08-08 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOPTR macro for declaring aggregate pointer variables,
majority of the calls to *Free functions can be dropped, which
in turn leads to getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virqemu.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/src/util/virqemu.c b/src/util/virqemu.c
index 4089b8e..bb88f49 100644
--- a/src/util/virqemu.c
+++ b/src/util/virqemu.c
@@ -56,7 +56,7 @@ virQEMUBuildCommandLineJSONArrayBitmap(const char *key,
 {
 ssize_t pos = -1;
 ssize_t end;
-virBitmapPtr bitmap = NULL;
+VIR_AUTOPTR(virBitmap) bitmap = NULL;
 
 if (virJSONValueGetArrayAsBitmap(array, ) < 0)
 return -1;
@@ -73,8 +73,6 @@ virQEMUBuildCommandLineJSONArrayBitmap(const char *key,
 }
 }
 
-virBitmapFree(bitmap);
-
 return 0;
 }
 
@@ -282,6 +280,7 @@ virQEMUBuildDriveCommandlineFromJSON(virJSONValuePtr srcdef)
  cleanup:
 virBufferFreeAndReset();
 return ret;
+
 }
 
 
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v2 27/35] util: numa: use VIR_AUTOFREE instead of VIR_FREE for scalar types

2018-08-08 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOFREE macro for declaring scalar variables, majority
of the VIR_FREE calls can be dropped, which in turn leads to
getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virnuma.c | 75 +-
 1 file changed, 29 insertions(+), 46 deletions(-)

diff --git a/src/util/virnuma.c b/src/util/virnuma.c
index 784db0a..6c6be1c 100644
--- a/src/util/virnuma.c
+++ b/src/util/virnuma.c
@@ -252,8 +252,8 @@ int
 virNumaGetNodeCPUs(int node,
virBitmapPtr *cpus)
 {
-unsigned long *mask = NULL;
-unsigned long *allonesmask = NULL;
+VIR_AUTOFREE(unsigned long *) mask = NULL;
+VIR_AUTOFREE(unsigned long *) allonesmask = NULL;
 virBitmapPtr cpumap = NULL;
 int ncpus = 0;
 int max_n_cpus = virNumaGetMaxCPUs();
@@ -300,8 +300,6 @@ virNumaGetNodeCPUs(int node,
 ret = ncpus;
 
  cleanup:
-VIR_FREE(mask);
-VIR_FREE(allonesmask);
 virBitmapFree(cpumap);
 
 return ret;
@@ -566,25 +564,24 @@ virNumaGetHugePageInfo(int node,
unsigned long long *page_avail,
unsigned long long *page_free)
 {
-int ret = -1;
-char *path = NULL;
-char *buf = NULL;
 char *end;
+VIR_AUTOFREE(char *) buf = NULL;
+VIR_AUTOFREE(char *) path = NULL;
 
 if (page_avail) {
 if (virNumaGetHugePageInfoPath(, node,
page_size, "nr_hugepages") < 0)
-goto cleanup;
+return -1;
 
 if (virFileReadAll(path, 1024, ) < 0)
-goto cleanup;
+return -1;
 
 if (virStrToLong_ull(buf, , 10, page_avail) < 0 ||
 *end != '\n') {
 virReportError(VIR_ERR_INTERNAL_ERROR,
_("unable to parse: %s"),
buf);
-goto cleanup;
+return -1;
 }
 VIR_FREE(buf);
 VIR_FREE(path);
@@ -593,25 +590,21 @@ virNumaGetHugePageInfo(int node,
 if (page_free) {
 if (virNumaGetHugePageInfoPath(, node,
page_size, "free_hugepages") < 0)
-goto cleanup;
+return -1;
 
 if (virFileReadAll(path, 1024, ) < 0)
-goto cleanup;
+return -1;
 
 if (virStrToLong_ull(buf, , 10, page_free) < 0 ||
 *end != '\n') {
 virReportError(VIR_ERR_INTERNAL_ERROR,
_("unable to parse: %s"),
buf);
-goto cleanup;
+return -1;
 }
 }
 
-ret = 0;
- cleanup:
-VIR_FREE(buf);
-VIR_FREE(path);
-return ret;
+return 0;
 }
 
 /**
@@ -714,13 +707,13 @@ virNumaGetPages(int node,
 size_t *npages)
 {
 int ret = -1;
-char *path = NULL;
+VIR_AUTOFREE(char *) path = NULL;
+VIR_AUTOFREE(unsigned int *) tmp_size = NULL;
+VIR_AUTOFREE(unsigned long long *) tmp_avail = NULL;
+VIR_AUTOFREE(unsigned long long *) tmp_free = NULL;
 DIR *dir = NULL;
 int direrr = 0;
 struct dirent *entry;
-unsigned int *tmp_size = NULL;
-unsigned long long *tmp_avail = NULL;
-unsigned long long *tmp_free = NULL;
 unsigned int ntmp = 0;
 size_t i;
 bool exchange;
@@ -828,11 +821,7 @@ virNumaGetPages(int node,
 *npages = ntmp;
 ret = 0;
  cleanup:
-VIR_FREE(tmp_free);
-VIR_FREE(tmp_avail);
-VIR_FREE(tmp_size);
 VIR_DIR_CLOSE(dir);
-VIR_FREE(path);
 return ret;
 }
 
@@ -843,8 +832,8 @@ virNumaSetPagePoolSize(int node,
unsigned long long page_count,
bool add)
 {
-int ret = -1;
-char *nr_path = NULL, *nr_buf =  NULL;
+VIR_AUTOFREE(char *) nr_path = NULL;
+VIR_AUTOFREE(char *) nr_buf =  NULL;
 char *end;
 unsigned long long nr_count;
 
@@ -853,37 +842,35 @@ virNumaSetPagePoolSize(int node,
  * differently to huge pages. */
 virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
_("system pages pool can't be modified"));
-goto cleanup;
+return -1;
 }
 
 if (virNumaGetHugePageInfoPath(_path, node, page_size, "nr_hugepages") 
< 0)
-goto cleanup;
+return -1;
 
 /* Firstly check, if there's anything for us to do */
 if (virFileReadAll(nr_path, 1024, _buf) < 0)
-goto cleanup;
+return -1;
 
 if (virStrToLong_ull(nr_buf, , 10, _count) < 0 ||
 *end != '\n') {
 virReportError(VIR_ERR_OPERATION_FAILED,
_("invalid number '%s' in '%s'"),
nr_buf, nr_path);
-goto cleanup;
+return -1;
 }
 
 if (add) {
 if (!page_count) {
 VIR_DEBUG("Nothing

[libvirt] [PATCH v2 22/35] util: netdevopenvswitch: use VIR_AUTOFREE instead of VIR_FREE for scalar types

2018-08-08 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOFREE macro for declaring scalar variables, majority
of the VIR_FREE calls can be dropped, which in turn leads to
getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virnetdevopenvswitch.c | 18 ++
 1 file changed, 6 insertions(+), 12 deletions(-)

diff --git a/src/util/virnetdevopenvswitch.c b/src/util/virnetdevopenvswitch.c
index d1c5cf4..eff6b0f 100644
--- a/src/util/virnetdevopenvswitch.c
+++ b/src/util/virnetdevopenvswitch.c
@@ -149,10 +149,10 @@ int virNetDevOpenvswitchAddPort(const char *brname, const 
char *ifname,
 char macaddrstr[VIR_MAC_STRING_BUFLEN];
 char ifuuidstr[VIR_UUID_STRING_BUFLEN];
 char vmuuidstr[VIR_UUID_STRING_BUFLEN];
-char *attachedmac_ex_id = NULL;
-char *ifaceid_ex_id = NULL;
-char *profile_ex_id = NULL;
-char *vmid_ex_id = NULL;
+VIR_AUTOFREE(char *) attachedmac_ex_id = NULL;
+VIR_AUTOFREE(char *) ifaceid_ex_id = NULL;
+VIR_AUTOFREE(char *) profile_ex_id = NULL;
+VIR_AUTOFREE(char *) vmid_ex_id = NULL;
 
 virMacAddrFormat(macaddr, macaddrstr);
 virUUIDFormat(ovsport->interfaceID, ifuuidstr);
@@ -209,10 +209,6 @@ int virNetDevOpenvswitchAddPort(const char *brname, const 
char *ifname,
 
 ret = 0;
  cleanup:
-VIR_FREE(attachedmac_ex_id);
-VIR_FREE(ifaceid_ex_id);
-VIR_FREE(vmid_ex_id);
-VIR_FREE(profile_ex_id);
 virCommandFree(cmd);
 return ret;
 }
@@ -339,10 +335,10 @@ virNetDevOpenvswitchInterfaceStats(const char *ifname,
virDomainInterfaceStatsPtr stats)
 {
 virCommandPtr cmd = NULL;
-char *output;
 char *tmp;
 bool gotStats = false;
 int ret = -1;
+VIR_AUTOFREE(char *) output = NULL;
 
 /* Just ensure the interface exists in ovs */
 cmd = virCommandNew(OVSVSCTL);
@@ -399,7 +395,6 @@ virNetDevOpenvswitchInterfaceStats(const char *ifname,
 ret = 0;
 
  cleanup:
-VIR_FREE(output);
 virCommandFree(cmd);
 return ret;
 }
@@ -481,7 +476,7 @@ virNetDevOpenvswitchGetVhostuserIfname(const char *path,
 size_t ntokens = 0;
 int status;
 int ret = -1;
-char *ovs_timeout = NULL;
+VIR_AUTOFREE(char *) ovs_timeout = NULL;
 
 /* Openvswitch vhostuser path are hardcoded to
  * //openvswitch/
@@ -513,7 +508,6 @@ virNetDevOpenvswitchGetVhostuserIfname(const char *path,
  cleanup:
 virStringListFreeCount(tokens, ntokens);
 virCommandFree(cmd);
-VIR_FREE(ovs_timeout);
 return ret;
 }
 
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v2 33/35] util: process: use VIR_AUTOPTR for aggregate types

2018-08-08 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOPTR macro for declaring aggregate pointer variables,
majority of the calls to *Free functions can be dropped, which
in turn leads to getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
Reviewed-by: Erik Skultety 
---
 src/util/virprocess.c | 19 +++
 1 file changed, 7 insertions(+), 12 deletions(-)

diff --git a/src/util/virprocess.c b/src/util/virprocess.c
index 215e05d..1fbbe0c 100644
--- a/src/util/virprocess.c
+++ b/src/util/virprocess.c
@@ -971,9 +971,8 @@ int virProcessGetStartTime(pid_t pid,
unsigned long long *timestamp)
 {
 char *tmp;
-int ret = -1;
 int len;
-char **tokens = NULL;
+VIR_AUTOPTR(virString) tokens = NULL;
 VIR_AUTOFREE(char *) filename = NULL;
 VIR_AUTOFREE(char *) buf = NULL;
 
@@ -981,7 +980,7 @@ int virProcessGetStartTime(pid_t pid,
 return -1;
 
 if ((len = virFileReadAll(filename, 1024, )) < 0)
-goto cleanup;
+return -1;
 
 /* start time is the token at index 19 after the '(process name)' entry - 
since only this
  * field can contain the ')' character, search backwards for this to avoid 
malicious
@@ -992,14 +991,14 @@ int virProcessGetStartTime(pid_t pid,
 virReportError(VIR_ERR_INTERNAL_ERROR,
_("Cannot find start time in %s"),
filename);
-goto cleanup;
+return -1;
 }
 tmp += 2; /* skip ') ' */
 if ((tmp - buf) >= len) {
 virReportError(VIR_ERR_INTERNAL_ERROR,
_("Cannot find start time in %s"),
filename);
-goto cleanup;
+return -1;
 }
 
 tokens = virStringSplit(tmp, " ", 0);
@@ -1008,7 +1007,7 @@ int virProcessGetStartTime(pid_t pid,
 virReportError(VIR_ERR_INTERNAL_ERROR,
_("Cannot find start time in %s"),
filename);
-goto cleanup;
+return -1;
 }
 
 if (virStrToLong_ull(tokens[19],
@@ -1018,14 +1017,10 @@ int virProcessGetStartTime(pid_t pid,
 virReportError(VIR_ERR_INTERNAL_ERROR,
_("Cannot parse start time %s in %s"),
tokens[19], filename);
-goto cleanup;
+return -1;
 }
 
-ret = 0;
-
- cleanup:
-virStringListFree(tokens);
-return ret;
+return 0;
 }
 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
 int virProcessGetStartTime(pid_t pid,
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v2 30/35] util: perf: use VIR_AUTOFREE instead of VIR_FREE for scalar types

2018-08-08 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOFREE macro for declaring scalar variables, majority
of the VIR_FREE calls can be dropped, which in turn leads to
getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
Reviewed-by: Erik Skultety 
---
 src/util/virperf.c | 17 +
 1 file changed, 5 insertions(+), 12 deletions(-)

diff --git a/src/util/virperf.c b/src/util/virperf.c
index 4537cd0..23f7703 100644
--- a/src/util/virperf.c
+++ b/src/util/virperf.c
@@ -175,12 +175,12 @@ typedef struct virPerfEventAttr *virPerfEventAttrPtr;
 static int
 virPerfRdtAttrInit(void)
 {
-char *buf = NULL;
 char *tmp = NULL;
 unsigned int attr_type = 0;
+VIR_AUTOFREE(char *) buf = NULL;
 
 if (virFileReadAllQuiet("/sys/devices/intel_cqm/type", 10, ) < 0)
-goto error;
+return -1;
 
 if ((tmp = strchr(buf, '\n')))
 *tmp = '\0';
@@ -188,19 +188,14 @@ virPerfRdtAttrInit(void)
 if (virStrToLong_ui(buf, NULL, 10, _type) < 0) {
 virReportSystemError(errno, "%s",
  _("failed to get rdt event type"));
-goto error;
+return -1;
 }
-VIR_FREE(buf);
 
 attrs[VIR_PERF_EVENT_CMT].attrType = attr_type;
 attrs[VIR_PERF_EVENT_MBMT].attrType = attr_type;
 attrs[VIR_PERF_EVENT_MBML].attrType = attr_type;
 
 return 0;
-
- error:
-VIR_FREE(buf);
-return -1;
 }
 
 
@@ -209,7 +204,6 @@ virPerfEventEnable(virPerfPtr perf,
virPerfEventType type,
pid_t pid)
 {
-char *buf = NULL;
 struct perf_event_attr attr;
 virPerfEventPtr event = &(perf->events[type]);
 virPerfEventAttrPtr event_attr = [type];
@@ -227,6 +221,8 @@ virPerfEventEnable(virPerfPtr perf,
 }
 
 if (type == VIR_PERF_EVENT_CMT) {
+VIR_AUTOFREE(char *) buf = NULL;
+
 if (virFileReadAll("/sys/devices/intel_cqm/events/llc_occupancy.scale",
10, ) < 0)
 goto error;
@@ -236,8 +232,6 @@ virPerfEventEnable(virPerfPtr perf,
  _("failed to get cmt scaling factor"));
 goto error;
 }
-
-VIR_FREE(buf);
 }
 
 memset(, 0, sizeof(attr));
@@ -268,7 +262,6 @@ virPerfEventEnable(virPerfPtr perf,
 
  error:
 VIR_FORCE_CLOSE(event->fd);
-VIR_FREE(buf);
 return -1;
 }
 
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v2 23/35] util: netdevopenvswitch: use VIR_AUTOPTR for aggregate types

2018-08-08 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOPTR macro for declaring aggregate pointer variables,
majority of the calls to *Free functions can be dropped, which
in turn leads to getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virnetdevopenvswitch.c | 80 ++---
 1 file changed, 27 insertions(+), 53 deletions(-)

diff --git a/src/util/virnetdevopenvswitch.c b/src/util/virnetdevopenvswitch.c
index eff6b0f..d34fb6f 100644
--- a/src/util/virnetdevopenvswitch.c
+++ b/src/util/virnetdevopenvswitch.c
@@ -144,11 +144,10 @@ int virNetDevOpenvswitchAddPort(const char *brname, const 
char *ifname,
virNetDevVPortProfilePtr ovsport,
virNetDevVlanPtr virtVlan)
 {
-int ret = -1;
-virCommandPtr cmd = NULL;
 char macaddrstr[VIR_MAC_STRING_BUFLEN];
 char ifuuidstr[VIR_UUID_STRING_BUFLEN];
 char vmuuidstr[VIR_UUID_STRING_BUFLEN];
+VIR_AUTOPTR(virCommand) cmd = NULL;
 VIR_AUTOFREE(char *) attachedmac_ex_id = NULL;
 VIR_AUTOFREE(char *) ifaceid_ex_id = NULL;
 VIR_AUTOFREE(char *) profile_ex_id = NULL;
@@ -160,17 +159,17 @@ int virNetDevOpenvswitchAddPort(const char *brname, const 
char *ifname,
 
 if (virAsprintf(_ex_id, "external-ids:attached-mac=\"%s\"",
 macaddrstr) < 0)
-goto cleanup;
+return -1;
 if (virAsprintf(_ex_id, "external-ids:iface-id=\"%s\"",
 ifuuidstr) < 0)
-goto cleanup;
+return -1;
 if (virAsprintf(_ex_id, "external-ids:vm-id=\"%s\"",
 vmuuidstr) < 0)
-goto cleanup;
+return -1;
 if (ovsport->profileID[0] != '\0') {
 if (virAsprintf(_ex_id, "external-ids:port-profile=\"%s\"",
 ovsport->profileID) < 0)
-goto cleanup;
+return -1;
 }
 
 cmd = virCommandNew(OVSVSCTL);
@@ -179,7 +178,7 @@ int virNetDevOpenvswitchAddPort(const char *brname, const 
char *ifname,
  ifname, "--", "add-port", brname, ifname, NULL);
 
 if (virNetDevOpenvswitchConstructVlans(cmd, virtVlan) < 0)
-goto cleanup;
+return -1;
 
 if (ovsport->profileID[0] == '\0') {
 virCommandAddArgList(cmd,
@@ -204,13 +203,10 @@ int virNetDevOpenvswitchAddPort(const char *brname, const 
char *ifname,
 virReportError(VIR_ERR_INTERNAL_ERROR,
_("Unable to add port %s to OVS bridge %s"),
ifname, brname);
-goto cleanup;
+return -1;
 }
 
-ret = 0;
- cleanup:
-virCommandFree(cmd);
-return ret;
+return 0;
 }
 
 /**
@@ -223,8 +219,7 @@ int virNetDevOpenvswitchAddPort(const char *brname, const 
char *ifname,
  */
 int virNetDevOpenvswitchRemovePort(const char *brname ATTRIBUTE_UNUSED, const 
char *ifname)
 {
-int ret = -1;
-virCommandPtr cmd = NULL;
+VIR_AUTOPTR(virCommand) cmd = NULL;
 
 cmd = virCommandNew(OVSVSCTL);
 virNetDevOpenvswitchAddTimeout(cmd);
@@ -233,13 +228,10 @@ int virNetDevOpenvswitchRemovePort(const char *brname 
ATTRIBUTE_UNUSED, const ch
 if (virCommandRun(cmd, NULL) < 0) {
 virReportError(VIR_ERR_INTERNAL_ERROR,
_("Unable to delete port %s from OVS"), ifname);
-goto cleanup;
+return -1;
 }
 
-ret = 0;
- cleanup:
-virCommandFree(cmd);
-return ret;
+return 0;
 }
 
 /**
@@ -253,9 +245,8 @@ int virNetDevOpenvswitchRemovePort(const char *brname 
ATTRIBUTE_UNUSED, const ch
  */
 int virNetDevOpenvswitchGetMigrateData(char **migrate, const char *ifname)
 {
-virCommandPtr cmd = NULL;
 size_t len;
-int ret = -1;
+VIR_AUTOPTR(virCommand) cmd = NULL;
 
 cmd = virCommandNew(OVSVSCTL);
 virNetDevOpenvswitchAddTimeout(cmd);
@@ -269,7 +260,7 @@ int virNetDevOpenvswitchGetMigrateData(char **migrate, 
const char *ifname)
 virReportError(VIR_ERR_INTERNAL_ERROR,
_("Unable to run command to get OVS port data for "
  "interface %s"), ifname);
-goto cleanup;
+return -1;
 }
 
 /* Wipeout the newline, if it exists */
@@ -277,10 +268,7 @@ int virNetDevOpenvswitchGetMigrateData(char **migrate, 
const char *ifname)
 if (len > 0)
 (*migrate)[len - 1] = '\0';
 
-ret = 0;
- cleanup:
-virCommandFree(cmd);
-return ret;
+return 0;
 }
 
 /**
@@ -294,8 +282,7 @@ int virNetDevOpenvswitchGetMigrateData(char **migrate, 
const char *ifname)
  */
 int virNetDevOpenvswitchSetMigrateData(char *migrate, const char *ifname)
 {
-virCommandPtr cmd = NULL;
-int ret = -1;
+VIR_AUTOPTR(virCommand) cmd = NULL;
 
 if (!migrate) {
 VIR_DE

[libvirt] [PATCH v2 25/35] util: netdevveth: use VIR_AUTOFREE instead of VIR_FREE for scalar types

2018-08-08 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOFREE macro for declaring scalar variables, majority
of the VIR_FREE calls can be dropped, which in turn leads to
getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
Reviewed-by: Erik Skultety 
---
 src/util/virnetdevveth.c | 12 
 1 file changed, 4 insertions(+), 8 deletions(-)

diff --git a/src/util/virnetdevveth.c b/src/util/virnetdevveth.c
index 6905168..8c1a7f3 100644
--- a/src/util/virnetdevveth.c
+++ b/src/util/virnetdevveth.c
@@ -46,12 +46,11 @@ virMutex virNetDevVethCreateMutex = VIR_MUTEX_INITIALIZER;
 static int virNetDevVethExists(int devNum)
 {
 int ret;
-char *path = NULL;
+VIR_AUTOFREE(char *) path = NULL;
 if (virAsprintf(, SYSFS_NET_DIR "vnet%d/", devNum) < 0)
 return -1;
 ret = virFileExists(path) ? 1 : 0;
 VIR_DEBUG("Checked dev vnet%d usage: %d", devNum, ret);
-VIR_FREE(path);
 return ret;
 }
 
@@ -111,8 +110,6 @@ static int virNetDevVethGetFreeNum(int startDev)
 int virNetDevVethCreate(char** veth1, char** veth2)
 {
 int ret = -1;
-char *veth1auto = NULL;
-char *veth2auto = NULL;
 int vethNum = 0;
 virCommandPtr cmd = NULL;
 size_t i;
@@ -125,6 +122,9 @@ int virNetDevVethCreate(char** veth1, char** veth2)
 #define MAX_VETH_RETRIES 10
 
 for (i = 0; i < MAX_VETH_RETRIES; i++) {
+VIR_AUTOFREE(char *) veth1auto = NULL;
+VIR_AUTOFREE(char *) veth2auto = NULL;
+
 int status;
 if (!*veth1) {
 int veth1num;
@@ -173,8 +173,6 @@ int virNetDevVethCreate(char** veth1, char** veth2)
   *veth1 ? *veth1 : veth1auto,
   *veth2 ? *veth2 : veth2auto,
   status);
-VIR_FREE(veth1auto);
-VIR_FREE(veth2auto);
 virCommandFree(cmd);
 cmd = NULL;
 }
@@ -186,8 +184,6 @@ int virNetDevVethCreate(char** veth1, char** veth2)
  cleanup:
 virMutexUnlock();
 virCommandFree(cmd);
-VIR_FREE(veth1auto);
-VIR_FREE(veth2auto);
 return ret;
 }
 
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v2 26/35] util: netdevveth: use VIR_AUTOPTR for aggregate types

2018-08-08 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOPTR macro for declaring aggregate pointer variables,
majority of the calls to *Free functions can be dropped, which
in turn leads to getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
Reviewed-by: Erik Skultety 
---
 src/util/virnetdevveth.c | 20 ++--
 1 file changed, 6 insertions(+), 14 deletions(-)

diff --git a/src/util/virnetdevveth.c b/src/util/virnetdevveth.c
index 8c1a7f3..d2232ac 100644
--- a/src/util/virnetdevveth.c
+++ b/src/util/virnetdevveth.c
@@ -111,7 +111,6 @@ int virNetDevVethCreate(char** veth1, char** veth2)
 {
 int ret = -1;
 int vethNum = 0;
-virCommandPtr cmd = NULL;
 size_t i;
 
 /*
@@ -124,6 +123,7 @@ int virNetDevVethCreate(char** veth1, char** veth2)
 for (i = 0; i < MAX_VETH_RETRIES; i++) {
 VIR_AUTOFREE(char *) veth1auto = NULL;
 VIR_AUTOFREE(char *) veth2auto = NULL;
+VIR_AUTOPTR(virCommand) cmd = NULL;
 
 int status;
 if (!*veth1) {
@@ -173,8 +173,6 @@ int virNetDevVethCreate(char** veth1, char** veth2)
   *veth1 ? *veth1 : veth1auto,
   *veth2 ? *veth2 : veth2auto,
   status);
-virCommandFree(cmd);
-cmd = NULL;
 }
 
 virReportError(VIR_ERR_INTERNAL_ERROR,
@@ -183,7 +181,6 @@ int virNetDevVethCreate(char** veth1, char** veth2)
 
  cleanup:
 virMutexUnlock();
-virCommandFree(cmd);
 return ret;
 }
 
@@ -200,26 +197,21 @@ int virNetDevVethCreate(char** veth1, char** veth2)
  */
 int virNetDevVethDelete(const char *veth)
 {
-virCommandPtr cmd = virCommandNewArgList("ip", "link", "del", veth, NULL);
 int status;
-int ret = -1;
+VIR_AUTOPTR(virCommand) cmd = virCommandNewArgList("ip", "link", "del", 
veth, NULL);
 
 if (virCommandRun(cmd, ) < 0)
-goto cleanup;
+return -1;
 
 if (status != 0) {
 if (!virNetDevExists(veth)) {
 VIR_DEBUG("Device %s already deleted (by kernel namespace 
cleanup)", veth);
-ret = 0;
-goto cleanup;
+return 0;
 }
 virReportError(VIR_ERR_INTERNAL_ERROR,
_("Failed to delete veth device %s"), veth);
-goto cleanup;
+return -1;
 }
 
-ret = 0;
- cleanup:
-virCommandFree(cmd);
-return ret;
+return 0;
 }
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v2 31/35] util: pidfile: use VIR_AUTOFREE instead of VIR_FREE for scalar types

2018-08-08 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOFREE macro for declaring scalar variables, majority
of the VIR_FREE calls can be dropped, which in turn leads to
getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
Reviewed-by: Erik Skultety 
---
 src/util/virpidfile.c | 186 --
 1 file changed, 60 insertions(+), 126 deletions(-)

diff --git a/src/util/virpidfile.c b/src/util/virpidfile.c
index 1a85d43..999bccb 100644
--- a/src/util/virpidfile.c
+++ b/src/util/virpidfile.c
@@ -97,29 +97,18 @@ int virPidFileWrite(const char *dir,
 const char *name,
 pid_t pid)
 {
-int rc;
-char *pidfile = NULL;
+VIR_AUTOFREE(char *) pidfile = NULL;
 
-if (name == NULL || dir == NULL) {
-rc = -EINVAL;
-goto cleanup;
-}
+if (name == NULL || dir == NULL)
+return -EINVAL;
 
-if (virFileMakePath(dir) < 0) {
-rc = -errno;
-goto cleanup;
-}
+if (virFileMakePath(dir) < 0)
+return -errno;
 
-if (!(pidfile = virPidFileBuildPath(dir, name))) {
-rc = -ENOMEM;
-goto cleanup;
-}
+if (!(pidfile = virPidFileBuildPath(dir, name)))
+return -ENOMEM;
 
-rc = virPidFileWritePath(pidfile, pid);
-
- cleanup:
-VIR_FREE(pidfile);
-return rc;
+return virPidFileWritePath(pidfile, pid);
 }
 
 
@@ -170,25 +159,17 @@ int virPidFileRead(const char *dir,
const char *name,
pid_t *pid)
 {
-int rc;
-char *pidfile = NULL;
+VIR_AUTOFREE(char *) pidfile = NULL;
+
 *pid = 0;
 
-if (name == NULL || dir == NULL) {
-rc = -EINVAL;
-goto cleanup;
-}
+if (name == NULL || dir == NULL)
+return -EINVAL;
 
-if (!(pidfile = virPidFileBuildPath(dir, name))) {
-rc = -ENOMEM;
-goto cleanup;
-}
+if (!(pidfile = virPidFileBuildPath(dir, name)))
+return -ENOMEM;
 
-rc = virPidFileReadPath(pidfile, pid);
-
- cleanup:
-VIR_FREE(pidfile);
-return rc;
+return virPidFileReadPath(pidfile, pid);
 }
 
 
@@ -219,20 +200,20 @@ int virPidFileReadPathIfAlive(const char *path,
 {
 int ret;
 bool isLink;
-char *procPath = NULL;
-char *procLink = NULL;
 size_t procLinkLen;
-char *resolvedBinPath = NULL;
-char *resolvedProcLink = NULL;
 const char deletedText[] = " (deleted)";
 size_t deletedTextLen = strlen(deletedText);
 pid_t retPid;
+VIR_AUTOFREE(char *) procPath = NULL;
+VIR_AUTOFREE(char *) procLink = NULL;
+VIR_AUTOFREE(char *) resolvedBinPath = NULL;
+VIR_AUTOFREE(char *) resolvedProcLink = NULL;
 
 /* only set this at the very end on success */
 *pid = -1;
 
 if ((ret = virPidFileReadPath(path, )) < 0)
-goto cleanup;
+return ret;
 
 #ifndef WIN32
 /* Check that it's still alive.  Safe to skip this sanity check on
@@ -252,13 +233,12 @@ int virPidFileReadPathIfAlive(const char *path,
 goto cleanup;
 }
 
-if (virAsprintf(, "/proc/%lld/exe", (long long)retPid) < 0) {
-ret = -ENOMEM;
-goto cleanup;
-}
+if (virAsprintf(, "/proc/%lld/exe", (long long)retPid) < 0)
+return -ENOMEM;
 
 if ((ret = virFileIsLink(procPath)) < 0)
-goto cleanup;
+return ret;
+
 isLink = ret;
 
 if (isLink && virFileLinkPointsTo(procPath, binPath)) {
@@ -275,27 +255,21 @@ int virPidFileReadPathIfAlive(const char *path,
  * "$procpath (deleted)".  Read that link, remove the " (deleted)"
  * part, and see if it has the same canonicalized name as binpath.
  */
-if (!(procLink = areadlink(procPath))) {
-ret = -errno;
-goto cleanup;
-}
+if (!(procLink = areadlink(procPath)))
+return -errno;
+
 procLinkLen = strlen(procLink);
 if (procLinkLen > deletedTextLen)
 procLink[procLinkLen - deletedTextLen] = 0;
 
 if ((ret = virFileResolveAllLinks(binPath, )) < 0)
-goto cleanup;
+return ret;
 if ((ret = virFileResolveAllLinks(procLink, )) < 0)
-goto cleanup;
+return ret;
 
 ret = STREQ(resolvedBinPath, resolvedProcLink) ? 0 : -1;
 
  cleanup:
-VIR_FREE(procPath);
-VIR_FREE(procLink);
-VIR_FREE(resolvedProcLink);
-VIR_FREE(resolvedBinPath);
-
 /* return the originally set pid of -1 unless we proclaim success */
 if (ret == 0)
 *pid = retPid;
@@ -326,24 +300,15 @@ int virPidFileReadIfAlive(const char *dir,
   pid_t *pid,
   const char *binpath)
 {
-int rc = 0;
-char *pidfile = NULL;
+VIR_AUTOFREE(char *) pidfile = NULL;
 
-if (name == NULL || dir == NULL) {
-rc = -EINVAL;
-goto cleanup;
-}
+if (name == NULL || dir == NULL)
+return -EINVAL;
 
-if

[libvirt] [PATCH v2 24/35] util: netdevtap: use VIR_AUTOFREE instead of VIR_FREE for scalar types

2018-08-08 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOFREE macro for declaring scalar variables, majority
of the VIR_FREE calls can be dropped, which in turn leads to
getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
Reviewed-by: Erik Skultety 
---
 src/util/virnetdevtap.c | 11 +++
 1 file changed, 3 insertions(+), 8 deletions(-)

diff --git a/src/util/virnetdevtap.c b/src/util/virnetdevtap.c
index d432577..c2c2e73 100644
--- a/src/util/virnetdevtap.c
+++ b/src/util/virnetdevtap.c
@@ -400,16 +400,14 @@ int virNetDevTapCreate(char **ifname,
 if (strstr(*ifname, "%d") != NULL) {
 size_t i;
 for (i = 0; i <= IF_MAXUNIT; i++) {
-char *newname;
+VIR_AUTOFREE(char *) newname = NULL;
 if (virAsprintf(, *ifname, i) < 0)
 goto cleanup;
 
 if (virNetDevExists(newname) == 0) {
-newifname = newname;
+VIR_STEAL_PTR(newifname, newname);
 break;
 }
-
-VIR_FREE(newname);
 }
 if (newifname) {
 VIR_FREE(*ifname);
@@ -423,7 +421,7 @@ int virNetDevTapCreate(char **ifname,
 }
 
 if (tapfd) {
-char *dev_path = NULL;
+VIR_AUTOFREE(char *) dev_path = NULL;
 if (virAsprintf(_path, "/dev/%s", ifr.ifr_name) < 0)
 goto cleanup;
 
@@ -431,11 +429,8 @@ int virNetDevTapCreate(char **ifname,
 virReportSystemError(errno,
  _("Unable to open %s"),
  dev_path);
-VIR_FREE(dev_path);
 goto cleanup;
 }
-
-VIR_FREE(dev_path);
 }
 
 if (virNetDevSetName(ifr.ifr_name, *ifname) == -1)
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v2 32/35] util: process: use VIR_AUTOFREE instead of VIR_FREE for scalar types

2018-08-08 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOFREE macro for declaring scalar variables, majority
of the VIR_FREE calls can be dropped, which in turn leads to
getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virprocess.c | 39 ++-
 1 file changed, 14 insertions(+), 25 deletions(-)

diff --git a/src/util/virprocess.c b/src/util/virprocess.c
index f92b0dc..215e05d 100644
--- a/src/util/virprocess.c
+++ b/src/util/virprocess.c
@@ -158,7 +158,7 @@ virProcessAbort(pid_t pid)
 int saved_errno;
 int ret;
 int status;
-char *tmp = NULL;
+VIR_AUTOFREE(char *) tmp = NULL;
 
 if (pid <= 0)
 return;
@@ -199,7 +199,6 @@ virProcessAbort(pid_t pid)
 VIR_DEBUG("failed to reap child %lld, abandoning it", (long long) pid);
 
  cleanup:
-VIR_FREE(tmp);
 errno = saved_errno;
 }
 #else
@@ -238,6 +237,7 @@ virProcessWait(pid_t pid, int *exitstatus, bool raw)
 {
 int ret;
 int status;
+VIR_AUTOFREE(char *) st = NULL;
 
 if (pid <= 0) {
 if (pid != -1)
@@ -270,13 +270,10 @@ virProcessWait(pid_t pid, int *exitstatus, bool raw)
 return 0;
 
  error:
-{
-char *st = virProcessTranslateStatus(status);
-virReportError(VIR_ERR_INTERNAL_ERROR,
-   _("Child process (%lld) unexpected %s"),
-   (long long) pid, NULLSTR(st));
-VIR_FREE(st);
-}
+st = virProcessTranslateStatus(status);
+virReportError(VIR_ERR_INTERNAL_ERROR,
+   _("Child process (%lld) unexpected %s"),
+   (long long) pid, NULLSTR(st));
 return -1;
 }
 
@@ -603,7 +600,7 @@ virProcessGetAffinity(pid_t pid ATTRIBUTE_UNUSED)
 int virProcessGetPids(pid_t pid, size_t *npids, pid_t **pids)
 {
 int ret = -1;
-char *taskPath = NULL;
+VIR_AUTOFREE(char *) taskPath = NULL;
 DIR *dir = NULL;
 int value;
 struct dirent *ent;
@@ -636,7 +633,6 @@ int virProcessGetPids(pid_t pid, size_t *npids, pid_t 
**pids)
 
  cleanup:
 VIR_DIR_CLOSE(dir);
-VIR_FREE(taskPath);
 if (ret < 0)
 VIR_FREE(*pids);
 return ret;
@@ -648,7 +644,6 @@ int virProcessGetNamespaces(pid_t pid,
 int **fdlist)
 {
 int ret = -1;
-char *nsfile = NULL;
 size_t i = 0;
 const char *ns[] = { "user", "ipc", "uts", "net", "pid", "mnt" };
 
@@ -656,6 +651,7 @@ int virProcessGetNamespaces(pid_t pid,
 *fdlist = NULL;
 
 for (i = 0; i < ARRAY_CARDINALITY(ns); i++) {
+VIR_AUTOFREE(char *) nsfile = NULL;
 int fd;
 
 if (virAsprintf(, "/proc/%llu/ns/%s",
@@ -671,14 +667,11 @@ int virProcessGetNamespaces(pid_t pid,
 
 (*fdlist)[(*nfdlist)-1] = fd;
 }
-
-VIR_FREE(nsfile);
 }
 
 ret = 0;
 
  cleanup:
-VIR_FREE(nsfile);
 if (ret < 0) {
 for (i = 0; i < *nfdlist; i++)
 VIR_FORCE_CLOSE((*fdlist)[i]);
@@ -977,12 +970,12 @@ virProcessSetMaxCoreSize(pid_t pid ATTRIBUTE_UNUSED,
 int virProcessGetStartTime(pid_t pid,
unsigned long long *timestamp)
 {
-char *filename = NULL;
-char *buf = NULL;
 char *tmp;
 int ret = -1;
 int len;
 char **tokens = NULL;
+VIR_AUTOFREE(char *) filename = NULL;
+VIR_AUTOFREE(char *) buf = NULL;
 
 if (virAsprintf(, "/proc/%llu/stat", (long long) pid) < 0)
 return -1;
@@ -1032,8 +1025,6 @@ int virProcessGetStartTime(pid_t pid,
 
  cleanup:
 virStringListFree(tokens);
-VIR_FREE(filename);
-VIR_FREE(buf);
 return ret;
 }
 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
@@ -1080,7 +1071,7 @@ static int virProcessNamespaceHelper(int errfd,
  virProcessNamespaceCallback cb,
  void *opaque)
 {
-char *path;
+VIR_AUTOFREE(char *) path = NULL;
 int fd = -1;
 int ret = -1;
 
@@ -1109,7 +1100,6 @@ static int virProcessNamespaceHelper(int errfd,
 ignore_value(safewrite(errfd, err->message, len));
 }
 }
-VIR_FREE(path);
 VIR_FORCE_CLOSE(fd);
 return ret;
 }
@@ -1145,7 +1135,7 @@ virProcessRunInMountNamespace(pid_t pid,
 VIR_FORCE_CLOSE(errfd[1]);
 _exit(ret < 0 ? EXIT_CANCELED : ret);
 } else {
-char *buf = NULL;
+VIR_AUTOFREE(char *) buf = NULL;
 int status;
 
 VIR_FORCE_CLOSE(errfd[1]);
@@ -1159,7 +1149,6 @@ virProcessRunInMountNamespace(pid_t pid,
NULLSTR(buf));
 }
 }
-VIR_FREE(buf);
 }
 
  cleanup:
@@ -1226,7 +1215,7 @@ virProcessNamespaceAvailable(unsigned int ns)
 int flags = 0;
 int cpid;
 char *childStack;
-char *stack;
+VIR_AUTOFREE(char *)stack = NULL;
 int sta

[libvirt] [PATCH v2 34/35] util: qemu: use VIR_AUTOFREE instead of VIR_FREE for scalar types

2018-08-08 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOFREE macro for declaring scalar variables, majority
of the VIR_FREE calls can be dropped, which in turn leads to
getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
Reviewed-by: Erik Skultety 
---
 src/util/virqemu.c | 26 +++---
 1 file changed, 7 insertions(+), 19 deletions(-)

diff --git a/src/util/virqemu.c b/src/util/virqemu.c
index 30b8dc1..4089b8e 100644
--- a/src/util/virqemu.c
+++ b/src/util/virqemu.c
@@ -85,29 +85,22 @@ virQEMUBuildCommandLineJSONArrayNumbered(const char *key,
  virBufferPtr buf)
 {
 virJSONValuePtr member;
-char *prefix = NULL;
 size_t i;
-int ret = 0;
 
 for (i = 0; i < virJSONValueArraySize(array); i++) {
+VIR_AUTOFREE(char *) prefix = NULL;
 member = virJSONValueArrayGet((virJSONValuePtr) array, i);
 
 if (virAsprintf(, "%s.%zu", key, i) < 0)
-goto cleanup;
+return 0;
 
 if (virQEMUBuildCommandLineJSONRecurse(prefix, member, buf,

virQEMUBuildCommandLineJSONArrayNumbered,
true) < 0)
-goto cleanup;
-
-VIR_FREE(prefix);
+return 0;
 }
 
-ret = 0;
-
- cleanup:
-VIR_FREE(prefix);
-return ret;
+return 0;
 }
 
 
@@ -118,23 +111,18 @@ virQEMUBuildCommandLineJSONIterate(const char *key,
void *opaque)
 {
 struct virQEMUCommandLineJSONIteratorData *data = opaque;
-char *tmpkey = NULL;
-int ret = -1;
 
 if (data->prefix) {
+VIR_AUTOFREE(char *) tmpkey = NULL;
 if (virAsprintf(, "%s.%s", data->prefix, key) < 0)
 return -1;
 
-ret = virQEMUBuildCommandLineJSONRecurse(tmpkey, value, data->buf,
+return virQEMUBuildCommandLineJSONRecurse(tmpkey, value, data->buf,
  data->arrayFunc, false);
-
-VIR_FREE(tmpkey);
 } else {
-ret = virQEMUBuildCommandLineJSONRecurse(key, value, data->buf,
+return virQEMUBuildCommandLineJSONRecurse(key, value, data->buf,
  data->arrayFunc, false);
 }
-
-return ret;
 }
 
 
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v2 20/35] util: netdevmacvlan: use VIR_AUTOFREE instead of VIR_FREE for scalar types

2018-08-08 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOFREE macro for declaring scalar variables, majority
of the VIR_FREE calls can be dropped, which in turn leads to
getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
Reviewed-by: Erik Skultety 
---
 src/util/virnetdevmacvlan.c | 6 ++
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/src/util/virnetdevmacvlan.c b/src/util/virnetdevmacvlan.c
index 91c6244..a2ed65c 100644
--- a/src/util/virnetdevmacvlan.c
+++ b/src/util/virnetdevmacvlan.c
@@ -308,7 +308,7 @@ virNetDevMacVLanCreate(const char *ifname,
int *retry)
 {
 int rc = -1;
-struct nlmsghdr *resp = NULL;
+VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
 struct nlmsgerr *err;
 struct ifinfomsg ifinfo = { .ifi_family = AF_UNSPEC };
 int ifindex;
@@ -403,7 +403,6 @@ virNetDevMacVLanCreate(const char *ifname,
 rc = 0;
  cleanup:
 nlmsg_free(nl_msg);
-VIR_FREE(resp);
 return rc;
 
  malformed_resp:
@@ -452,7 +451,7 @@ virNetDevMacVLanTapOpen(const char *ifname,
 {
 int ret = -1;
 int ifindex;
-char *tapname = NULL;
+VIR_AUTOFREE(char *) tapname = NULL;
 size_t i = 0;
 
 if (virNetDevGetIndex(ifname, ) < 0)
@@ -487,7 +486,6 @@ virNetDevMacVLanTapOpen(const char *ifname,
 while (i--)
 VIR_FORCE_CLOSE(tapfd[i]);
 }
-VIR_FREE(tapname);
 return ret;
 }
 
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v2 29/35] util: perf: define cleanup function using VIR_DEFINE_AUTOPTR_FUNC

2018-08-08 Thread Sukrit Bhatnagar
Using the new VIR_DEFINE_AUTOPTR_FUNC macro defined in
src/util/viralloc.h, define a new wrapper around an existing
cleanup function which will be called when a variable declared
with VIR_AUTOPTR macro goes out of scope. Also, drop the redundant
viralloc.h include, since that has moved from the source module into
the header.

When a variable of type virPerfPtr is declared using VIR_AUTOPTR,
the function virPerfFree will be run automatically on it when it
goes out of scope.

This commit also adds an intermediate typedef for virPerf
type for use with the cleanup macros.

Signed-off-by: Sukrit Bhatnagar 
Reviewed-by: Erik Skultety 
---
 src/util/virperf.c | 3 +--
 src/util/virperf.h | 8 ++--
 2 files changed, 7 insertions(+), 4 deletions(-)

diff --git a/src/util/virperf.c b/src/util/virperf.c
index 2c832b3..4537cd0 100644
--- a/src/util/virperf.c
+++ b/src/util/virperf.c
@@ -26,7 +26,6 @@
 #endif
 
 #include "virperf.h"
-#include "viralloc.h"
 #include "virerror.h"
 #include "virlog.h"
 #include "virfile.h"
@@ -61,7 +60,7 @@ struct virPerfEvent {
 };
 typedef struct virPerfEvent *virPerfEventPtr;
 
-struct virPerf {
+struct _virPerf {
 struct virPerfEvent events[VIR_PERF_EVENT_LAST];
 };
 
diff --git a/src/util/virperf.h b/src/util/virperf.h
index eee7a03..9d0d5ac 100644
--- a/src/util/virperf.h
+++ b/src/util/virperf.h
@@ -23,6 +23,7 @@
 # define __VIR_PERF_H__
 
 # include "virutil.h"
+# include "viralloc.h"
 
 /* Some Intel processor families introduced some RDT (Resource Director
  * Technology) features to monitor or control shared resource based on
@@ -62,8 +63,9 @@ typedef enum {
 
 VIR_ENUM_DECL(virPerfEvent);
 
-struct virPerf;
-typedef struct virPerf *virPerfPtr;
+struct _virPerf;
+typedef struct _virPerf virPerf;
+typedef virPerf *virPerfPtr;
 
 virPerfPtr virPerfNew(void);
 
@@ -83,4 +85,6 @@ int virPerfReadEvent(virPerfPtr perf,
  virPerfEventType type,
  uint64_t *value);
 
+VIR_DEFINE_AUTOPTR_FUNC(virPerf, virPerfFree)
+
 #endif /* __VIR_PERF_H__ */
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v2 17/35] util: netdevip: use VIR_AUTOFREE instead of VIR_FREE for scalar types

2018-08-08 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOFREE macro for declaring scalar variables, majority
of the VIR_FREE calls can be dropped, which in turn leads to
getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virnetdevip.c | 97 ++
 1 file changed, 34 insertions(+), 63 deletions(-)

diff --git a/src/util/virnetdevip.c b/src/util/virnetdevip.c
index fdb0b74..78c6dcf 100644
--- a/src/util/virnetdevip.c
+++ b/src/util/virnetdevip.c
@@ -171,11 +171,11 @@ virNetDevIPAddrAdd(const char *ifname,
 virSocketAddr *broadcast = NULL;
 int ret = -1;
 struct nl_msg *nlmsg = NULL;
-struct nlmsghdr *resp = NULL;
 unsigned int recvbuflen;
-char *ipStr = NULL;
-char *peerStr = NULL;
-char *bcastStr = NULL;
+VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
+VIR_AUTOFREE(char *) ipStr = NULL;
+VIR_AUTOFREE(char *) peerStr = NULL;
+VIR_AUTOFREE(char *) bcastStr = NULL;
 
 ipStr = virSocketAddrFormat(addr);
 if (peer && VIR_SOCKET_ADDR_VALID(peer))
@@ -225,11 +225,7 @@ virNetDevIPAddrAdd(const char *ifname,
 
 ret = 0;
  cleanup:
-VIR_FREE(ipStr);
-VIR_FREE(peerStr);
-VIR_FREE(bcastStr);
 nlmsg_free(nlmsg);
-VIR_FREE(resp);
 VIR_FREE(broadcast);
 return ret;
 }
@@ -252,8 +248,8 @@ virNetDevIPAddrDel(const char *ifname,
 {
 int ret = -1;
 struct nl_msg *nlmsg = NULL;
-struct nlmsghdr *resp = NULL;
 unsigned int recvbuflen;
+VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
 
 if (!(nlmsg = virNetDevCreateNetlinkAddressMessage(RTM_DELADDR, ifname,
addr, prefix,
@@ -273,7 +269,6 @@ virNetDevIPAddrDel(const char *ifname,
 ret = 0;
  cleanup:
 nlmsg_free(nlmsg);
-VIR_FREE(resp);
 return ret;
 }
 
@@ -309,8 +304,8 @@ virNetDevIPRouteAdd(const char *ifname,
 int errCode;
 virSocketAddr defaultAddr;
 virSocketAddrPtr actualAddr;
-char *toStr = NULL;
-char *viaStr = NULL;
+VIR_AUTOFREE(char *) toStr = NULL;
+VIR_AUTOFREE(char *) viaStr = NULL;
 
 actualAddr = addr;
 
@@ -383,8 +378,6 @@ virNetDevIPRouteAdd(const char *ifname,
 
 ret = 0;
  cleanup:
-VIR_FREE(toStr);
-VIR_FREE(viaStr);
 nlmsg_free(nlmsg);
 return ret;
 
@@ -452,7 +445,6 @@ virNetDevIPWaitDadFinish(virSocketAddrPtr *addrs, size_t 
count)
 {
 struct nl_msg *nlmsg = NULL;
 struct ifaddrmsg ifa;
-struct nlmsghdr *resp = NULL;
 unsigned int recvbuflen;
 int ret = -1;
 bool dad = true;
@@ -475,6 +467,8 @@ virNetDevIPWaitDadFinish(virSocketAddrPtr *addrs, size_t 
count)
 
 /* Periodically query netlink until DAD finishes on all known addresses. */
 while (dad && time(NULL) < max_time) {
+VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
+
 if (virNetlinkCommand(nlmsg, , , 0, 0,
   NETLINK_ROUTE, 0) < 0)
 goto cleanup;
@@ -489,8 +483,6 @@ virNetDevIPWaitDadFinish(virSocketAddrPtr *addrs, size_t 
count)
 dad = virNetDevIPParseDadStatus(resp, recvbuflen, addrs, count);
 if (dad)
 usleep(1000 * 10);
-
-VIR_FREE(resp);
 }
 /* Check timeout. */
 if (dad) {
@@ -502,7 +494,6 @@ virNetDevIPWaitDadFinish(virSocketAddrPtr *addrs, size_t 
count)
 }
 
  cleanup:
-VIR_FREE(resp);
 nlmsg_free(nlmsg);
 return ret;
 }
@@ -510,22 +501,18 @@ virNetDevIPWaitDadFinish(virSocketAddrPtr *addrs, size_t 
count)
 static int
 virNetDevIPGetAcceptRA(const char *ifname)
 {
-char *path = NULL;
-char *buf = NULL;
 char *suffix;
 int accept_ra = -1;
+VIR_AUTOFREE(char *) path = NULL;
+VIR_AUTOFREE(char *) buf = NULL;
 
 if (virAsprintf(, "/proc/sys/net/ipv6/conf/%s/accept_ra",
 ifname ? ifname : "all") < 0)
-goto cleanup;
+return -1;
 
 if ((virFileReadAll(path, 512, ) < 0) ||
 (virStrToLong_i(buf, , 10, _ra) < 0))
-goto cleanup;
-
- cleanup:
-VIR_FREE(path);
-VIR_FREE(buf);
+return accept_ra;
 
 return accept_ra;
 }
@@ -545,17 +532,16 @@ virNetDevIPCheckIPv6ForwardingCallback(const struct 
nlmsghdr *resp,
 struct rtmsg *rtmsg = NLMSG_DATA(resp);
 int accept_ra = -1;
 struct rtattr *rta;
-char *ifname = NULL;
 struct virNetDevIPCheckIPv6ForwardingData *data = opaque;
-int ret = 0;
 int len = RTM_PAYLOAD(resp);
 int oif = -1;
 size_t i;
 bool hasDevice;
+VIR_AUTOFREE(char *) ifname = NULL;
 
 /* Ignore messages other than route ones */
 if (resp->nlmsg_type != RTM_NEWROUTE)
-return ret;
+return 0;
 
 /* Extract a device ID attribute */
 VIR_WARNINGS_NO_CAST_ALIGN
@@ -566,21 +552,20 @@ virNetDevIPCheckIPv6ForwardingCallback(const struct 
nlmsghdr *resp,
 
 /* Should never happen: netl

[libvirt] [PATCH v2 14/35] util: socketaddr: use VIR_AUTOPTR for aggregate types

2018-08-08 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOPTR macro for declaring aggregate pointer variables,
majority of the calls to *Free functions can be dropped, which
in turn leads to getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
Reviewed-by: Erik Skultety 
---
 src/util/virsocketaddr.c | 38 --
 1 file changed, 16 insertions(+), 22 deletions(-)

diff --git a/src/util/virsocketaddr.c b/src/util/virsocketaddr.c
index eee725d..3e5ea64 100644
--- a/src/util/virsocketaddr.c
+++ b/src/util/virsocketaddr.c
@@ -1193,52 +1193,46 @@ virSocketAddrPTRDomain(const virSocketAddr *addr,
unsigned int prefix,
char **ptr)
 {
-virBuffer buf = VIR_BUFFER_INITIALIZER;
 size_t i;
-int ret = -1;
+VIR_AUTOPTR(virBuffer) buf = NULL;
+
+if (VIR_ALLOC(buf) < 0)
+return -1;
 
 if (VIR_SOCKET_ADDR_IS_FAMILY(addr, AF_INET)) {
 virSocketAddrIPv4 ip;
 
 if (prefix == 0 || prefix >= 32 || prefix % 8 != 0)
-goto unsupported;
+return -2;
 
 if (virSocketAddrGetIPv4Addr(addr, ) < 0)
-goto cleanup;
+return -1;
 
 for (i = prefix / 8; i > 0; i--)
-virBufferAsprintf(, "%u.", ip[i - 1]);
+virBufferAsprintf(buf, "%u.", ip[i - 1]);
 
-virBufferAddLit(, VIR_SOCKET_ADDR_IPV4_ARPA);
+virBufferAddLit(buf, VIR_SOCKET_ADDR_IPV4_ARPA);
 } else if (VIR_SOCKET_ADDR_IS_FAMILY(addr, AF_INET6)) {
 virSocketAddrIPv6Nibbles ip;
 
 if (prefix == 0 || prefix >= 128 || prefix % 4 != 0)
-goto unsupported;
+return -2;
 
 if (virSocketAddrGetIPv6Nibbles(addr, ) < 0)
-goto cleanup;
+return -1;
 
 for (i = prefix / 4; i > 0; i--)
-virBufferAsprintf(, "%x.", ip[i - 1]);
+virBufferAsprintf(buf, "%x.", ip[i - 1]);
 
-virBufferAddLit(, VIR_SOCKET_ADDR_IPV6_ARPA);
+virBufferAddLit(buf, VIR_SOCKET_ADDR_IPV6_ARPA);
 } else {
-goto unsupported;
+return -2;
 }
 
-if (!(*ptr = virBufferContentAndReset()))
-goto cleanup;
+if (!(*ptr = virBufferContentAndReset(buf)))
+return -1;
 
-ret = 0;
-
- cleanup:
-virBufferFreeAndReset();
-return ret;
-
- unsupported:
-ret = -2;
-goto cleanup;
+return 0;
 }
 
 void
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v2 19/35] util: netdevmacvlan: define cleanup function using VIR_DEFINE_AUTOPTR_FUNC

2018-08-08 Thread Sukrit Bhatnagar
Using the new VIR_DEFINE_AUTOPTR_FUNC macro defined in
src/util/viralloc.h, define a new wrapper around an existing
cleanup function which will be called when a variable declared
with VIR_AUTOPTR macro goes out of scope. Also, drop the redundant
viralloc.h include, since that has moved from the source module into
the header.

When a variable of type virNetlinkCallbackDataPtr is declared using
VIR_AUTOPTR, the function virNetlinkCallbackDataFree will be run
automatically on it when it goes out of scope.

This commit also adds an intermediate typedef for virNetlinkCallbackData
type for use with the cleanup macros.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virnetdevmacvlan.c | 7 +--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/src/util/virnetdevmacvlan.c b/src/util/virnetdevmacvlan.c
index fb41bf9..91c6244 100644
--- a/src/util/virnetdevmacvlan.c
+++ b/src/util/virnetdevmacvlan.c
@@ -576,7 +576,7 @@ static const uint32_t modeMap[VIR_NETDEV_MACVLAN_MODE_LAST] 
= {
 };
 
 /* Struct to hold the state and configuration of a 802.1qbg port */
-struct virNetlinkCallbackData {
+struct _virNetlinkCallbackData {
 char *cr_ifname;
 virNetDevVPortProfilePtr virtPortProfile;
 virMacAddr macaddress;
@@ -587,7 +587,8 @@ struct virNetlinkCallbackData {
 unsigned int linkState;
 };
 
-typedef struct virNetlinkCallbackData *virNetlinkCallbackDataPtr;
+typedef struct _virNetlinkCallbackData virNetlinkCallbackData;
+typedef virNetlinkCallbackData *virNetlinkCallbackDataPtr;
 
 # define INSTANCE_STRLEN 36
 
@@ -870,6 +871,8 @@ virNetlinkCallbackDataFree(virNetlinkCallbackDataPtr calld)
 VIR_FREE(calld);
 }
 
+VIR_DEFINE_AUTOPTR_FUNC(virNetlinkCallbackData, virNetlinkCallbackDataFree)
+
 /**
  * virNetDevMacVLanVPortProfileDestroyCallback:
  *
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v2 15/35] util: netdevip: define virNetDevIPAddrFree for use with cleanup macros

2018-08-08 Thread Sukrit Bhatnagar
Define a free function for virNetDevIPAddrPtr type for consistency
and extensibility.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virnetdevip.c | 6 ++
 src/util/virnetdevip.h | 1 +
 2 files changed, 7 insertions(+)

diff --git a/src/util/virnetdevip.c b/src/util/virnetdevip.c
index bf98ed8..7197d07 100644
--- a/src/util/virnetdevip.c
+++ b/src/util/virnetdevip.c
@@ -1129,3 +1129,9 @@ virNetDevIPInfoAddToDev(const char *ifname,
  cleanup:
 return ret;
 }
+
+void
+virNetDevIPAddrFree(virNetDevIPAddrPtr ip)
+{
+VIR_FREE(ip);
+}
diff --git a/src/util/virnetdevip.h b/src/util/virnetdevip.h
index 6b509ea..dfc978d 100644
--- a/src/util/virnetdevip.h
+++ b/src/util/virnetdevip.h
@@ -84,6 +84,7 @@ int virNetDevIPAddrGet(const char *ifname, virSocketAddrPtr 
addr)
 int virNetDevIPWaitDadFinish(virSocketAddrPtr *addrs, size_t count)
 ATTRIBUTE_NONNULL(1);
 bool virNetDevIPCheckIPv6Forwarding(void);
+void virNetDevIPAddrFree(virNetDevIPAddrPtr ip);
 
 /* virNetDevIPRoute object */
 void virNetDevIPRouteFree(virNetDevIPRoutePtr def);
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v2 13/35] util: socketaddr: use VIR_AUTOFREE instead of VIR_FREE for scalar types

2018-08-08 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOFREE macro for declaring scalar variables, majority
of the VIR_FREE calls can be dropped, which in turn leads to
getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
Reviewed-by: Erik Skultety 
---
 src/util/virsocketaddr.c | 70 
 1 file changed, 29 insertions(+), 41 deletions(-)

diff --git a/src/util/virsocketaddr.c b/src/util/virsocketaddr.c
index 6b52a3d..eee725d 100644
--- a/src/util/virsocketaddr.c
+++ b/src/util/virsocketaddr.c
@@ -432,10 +432,10 @@ virSocketAddrFormatFull(const virSocketAddr *addr,
 if (withService) {
 if (virAsprintf(, VIR_LOOPBACK_IPV4_ADDR"%s0",
 separator ? separator : ":") < 0)
-goto error;
+return NULL;
 } else {
 if (VIR_STRDUP(addrstr, VIR_LOOPBACK_IPV4_ADDR) < 0)
-goto error;
+return NULL;
 }
 return addrstr;
 }
@@ -452,33 +452,27 @@ virSocketAddrFormatFull(const virSocketAddr *addr,
 }
 
 if (withService) {
-char *ipv6_host = NULL;
+VIR_AUTOFREE(char *) ipv6_host = NULL;
 /* sasl_new_client demands the socket address to be in an odd format:
  * a.b.c.d;port or e:f:g:h:i:j:k:l;port, so use square brackets for
  * IPv6 only if no separator is passed to the function
  */
 if (!separator && VIR_SOCKET_ADDR_FAMILY(addr) == AF_INET6) {
 if (virAsprintf(_host, "[%s]", host) < 0)
-goto error;
+return NULL;
 }
 
 if (virAsprintf(, "%s%s%s",
 ipv6_host ? ipv6_host : host,
 separator ? separator : ":", port) == -1) {
-VIR_FREE(ipv6_host);
-goto error;
+return NULL;
 }
-
-VIR_FREE(ipv6_host);
 } else {
 if (VIR_STRDUP(addrstr, host) < 0)
-goto error;
+return NULL;
 }
 
 return addrstr;
-
- error:
-return NULL;
 }
 
 
@@ -759,24 +753,26 @@ virSocketAddrGetRange(virSocketAddrPtr start, 
virSocketAddrPtr end,
 int ret = 0;
 size_t i;
 virSocketAddr netmask;
-char *startStr = NULL, *endStr = NULL, *netStr = NULL;
+VIR_AUTOFREE(char *) startStr = NULL;
+VIR_AUTOFREE(char *) endStr = NULL;
+VIR_AUTOFREE(char *) netStr = NULL;
 
 if (start == NULL || end == NULL) {
 virReportError(VIR_ERR_INTERNAL_ERROR,
_("NULL argument - %p %p"), start, end);
-goto error;
+return -1;
 }
 
 startStr = virSocketAddrFormat(start);
 endStr = virSocketAddrFormat(end);
 if (!startStr || !endStr)
-goto error; /*error already reported */
+return -1; /*error already reported */
 
 if (VIR_SOCKET_ADDR_FAMILY(start) != VIR_SOCKET_ADDR_FAMILY(end)) {
 virReportError(VIR_ERR_INTERNAL_ERROR,
_("mismatch of address family in range %s - %s"),
startStr, endStr);
-goto error;
+return -1;
 }
 
 if (network) {
@@ -784,14 +780,14 @@ virSocketAddrGetRange(virSocketAddrPtr start, 
virSocketAddrPtr end,
  * network the range should be within
  */
 if (!(netStr = virSocketAddrFormat(network)))
-goto error;
+return -1;
 
 if (VIR_SOCKET_ADDR_FAMILY(start) != VIR_SOCKET_ADDR_FAMILY(network)) {
 virReportError(VIR_ERR_INTERNAL_ERROR,
_("mismatch of address family in "
  "range %s - %s for network %s"),
startStr, endStr, netStr);
-goto error;
+return -1;
 }
 
 if (prefix < 0 ||
@@ -801,7 +797,7 @@ virSocketAddrGetRange(virSocketAddrPtr start, 
virSocketAddrPtr end,
_("bad prefix %d for network %s when "
  " checking range %s - %s"),
prefix, netStr, startStr, endStr);
-goto error;
+return -1;
 }
 
 /* both start and end of range need to be within network */
@@ -811,7 +807,7 @@ virSocketAddrGetRange(virSocketAddrPtr start, 
virSocketAddrPtr end,
_("range %s - %s is not entirely within "
  "network %s/%d"),
startStr, endStr, netStr, prefix);
-goto error;
+return -1;
 }
 
 if (VIR_SOCKET_ADDR_IS_FAMILY(start, AF_INET)) {
@@ -823,7 +819,7 @@ virSocketAddrGetRange(virSocketAddrPtr start, 
virSocketAddrPtr end,
_("failed to construct broadcast or network "
 

[libvirt] [PATCH v2 11/35] util: netdev: use VIR_AUTOPTR for aggregate types

2018-08-08 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOPTR macro for declaring aggregate pointer variables,
majority of the calls to *Free functions can be dropped, which
in turn leads to getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virnetdev.c | 267 +--
 1 file changed, 110 insertions(+), 157 deletions(-)

diff --git a/src/util/virnetdev.c b/src/util/virnetdev.c
index 5651f6f..d5aa94c 100644
--- a/src/util/virnetdev.c
+++ b/src/util/virnetdev.c
@@ -1182,27 +1182,21 @@ virNetDevIsPCIDevice(const char *devpath)
 static virPCIDevicePtr
 virNetDevGetPCIDevice(const char *devName)
 {
-virPCIDeviceAddressPtr vfPCIAddr = NULL;
-virPCIDevicePtr vfPCIDevice = NULL;
+VIR_AUTOPTR(virPCIDeviceAddress) vfPCIAddr = NULL;
 VIR_AUTOFREE(char *) vfSysfsDevicePath = NULL;
 
 if (virNetDevSysfsFile(, devName, "device") < 0)
-goto cleanup;
+return NULL;
 
 if (!virNetDevIsPCIDevice(vfSysfsDevicePath))
-goto cleanup;
+return NULL;
 
 vfPCIAddr = virPCIGetDeviceAddressFromSysfsLink(vfSysfsDevicePath);
 if (!vfPCIAddr)
-goto cleanup;
+return NULL;
 
-vfPCIDevice = virPCIDeviceNew(vfPCIAddr->domain, vfPCIAddr->bus,
-  vfPCIAddr->slot, vfPCIAddr->function);
-
- cleanup:
-VIR_FREE(vfPCIAddr);
-
-return vfPCIDevice;
+return virPCIDeviceNew(vfPCIAddr->domain, vfPCIAddr->bus,
+   vfPCIAddr->slot, vfPCIAddr->function);
 }
 
 
@@ -1601,12 +1595,12 @@ virNetDevSetVfConfig(const char *ifname, int vf,
 char macstr[VIR_MAC_STRING_BUFLEN];
 struct nlmsgerr *err;
 unsigned int recvbuflen = 0;
-struct nl_msg *nl_msg;
 struct nlattr *vfinfolist, *vfinfo;
 struct ifinfomsg ifinfo = {
 .ifi_family = AF_UNSPEC,
 .ifi_index  = -1,
 };
+VIR_AUTOPTR(virNlMsg) nl_msg = NULL;
 VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
 
 if (!macaddr && vlanid < 0)
@@ -1710,7 +1704,6 @@ virNetDevSetVfConfig(const char *ifname, int vf,
   macaddr ? virMacAddrFormat(macaddr, macstr) : "(unchanged)",
   vlanid, rc < 0 ? "Fail" : "Success");
 
-nlmsg_free(nl_msg);
 return rc;
 
  malformed_resp:
@@ -1849,12 +1842,11 @@ virNetDevSaveNetConfig(const char *linkdev, int vf,
const char *stateDir,
bool saveVlan)
 {
-int ret = -1;
 const char *pfDevName = NULL;
 virMacAddr oldMAC;
 char MACStr[VIR_MAC_STRING_BUFLEN];
 int oldVlanTag = -1;
-virJSONValuePtr configJSON = NULL;
+VIR_AUTOPTR(virJSONValue) configJSON = NULL;
 VIR_AUTOFREE(char *) pfDevOrig = NULL;
 VIR_AUTOFREE(char *) vfDevOrig = NULL;
 VIR_AUTOFREE(char *) filePath = NULL;
@@ -1866,7 +1858,7 @@ virNetDevSaveNetConfig(const char *linkdev, int vf,
 
 /* linkdev should get the VF's netdev name (or NULL if none) */
 if (virNetDevPFGetVF(pfDevName, vf, ) < 0)
-goto cleanup;
+return -1;
 
 linkdev = vfDevOrig;
 saveVlan = true;
@@ -1878,12 +1870,12 @@ virNetDevSaveNetConfig(const char *linkdev, int vf,
  */
 
 if (virNetDevGetPhysicalFunction(linkdev, ) < 0)
-goto cleanup;
+return -1;
 
 pfDevName = pfDevOrig;
 
 if (virNetDevGetVirtualFunctionIndex(pfDevName, linkdev, ) < 0)
-goto cleanup;
+return -1;
 }
 
 if (pfDevName) {
@@ -1901,7 +1893,7 @@ virNetDevSaveNetConfig(const char *linkdev, int vf,
  * explicitly enable the PF in the host system network config.
  */
 if (virNetDevGetOnline(pfDevName, ) < 0)
-goto cleanup;
+return -1;
 
 if (!pfIsOnline) {
 virReportError(VIR_ERR_INTERNAL_ERROR,
@@ -1910,12 +1902,12 @@ virNetDevSaveNetConfig(const char *linkdev, int vf,
  "change host network config to put the "
  "PF online."),
vf, pfDevName);
-goto cleanup;
+return -1;
 }
 }
 
 if (!(configJSON = virJSONValueNewObject()))
-goto cleanup;
+return -1;
 
 /* if there is a PF, it's now in pfDevName, and linkdev is either
  * the VF's name, or NULL (if the VF isn't bound to a net driver
@@ -1924,11 +1916,11 @@ virNetDevSaveNetConfig(const char *linkdev, int vf,
 
 if (pfDevName && saveVlan) {
 if (virAsprintf(, "%s/%s_vf%d", stateDir, pfDevName, vf) < 0)
-goto cleanup;
+return -1;
 
 /* get admin MAC and vlan tag */
 if (virNetDevGetVfConfig(pfDevName, vf, , ) < 0)
-goto cleanup;
+return -1;
 
 if (virJSONValueObjectAppendSt

[libvirt] [PATCH v2 03/35] util: netlink: define cleanup function using VIR_DEFINE_AUTOPTR_FUNC

2018-08-08 Thread Sukrit Bhatnagar
Using the new VIR_DEFINE_AUTOPTR_FUNC macro defined in
src/util/viralloc.h, define a new wrapper around an existing
cleanup function which will be called when a variable declared
with VIR_AUTOPTR macro goes out of scope. Also, drop the redundant
viralloc.h include, since that has moved from the source module into
the header.

This commit also typedefs virNlMsg to struct nl_msg type for use
with the cleanup macros.

When pointers to virNlMsg and virNetlinkHandle types are declared
using VIR_AUTOPTR, the functions nlmsg_free and virNetlinkFree,
respectively, will be run automatically on them when they go out
of scope.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virnetlink.c | 3 ++-
 src/util/virnetlink.h | 5 +
 2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/src/util/virnetlink.c b/src/util/virnetlink.c
index fa1ba3e..1c1eac7 100644
--- a/src/util/virnetlink.c
+++ b/src/util/virnetlink.c
@@ -38,7 +38,6 @@
 #include "virnetlink.h"
 #include "virnetdev.h"
 #include "virlog.h"
-#include "viralloc.h"
 #include "virthread.h"
 #include "virmacaddr.h"
 #include "virerror.h"
@@ -72,6 +71,8 @@ typedef struct nl_handle virNetlinkHandle;
 typedef struct nl_sock virNetlinkHandle;
 # endif
 
+VIR_DEFINE_AUTOPTR_FUNC(virNetlinkHandle, virNetlinkFree)
+
 typedef struct _virNetlinkEventSrvPrivate virNetlinkEventSrvPrivate;
 typedef virNetlinkEventSrvPrivate *virNetlinkEventSrvPrivatePtr;
 struct _virNetlinkEventSrvPrivate {
diff --git a/src/util/virnetlink.h b/src/util/virnetlink.h
index 2a9de0a..647f589 100644
--- a/src/util/virnetlink.h
+++ b/src/util/virnetlink.h
@@ -44,6 +44,9 @@ struct nlmsghdr;
 
 # endif /* __linux__ */
 
+typedef struct nl_msg virNlMsg;
+typedef virNlMsg *virNlMsgPtr;
+
 int virNetlinkStartup(void);
 void virNetlinkShutdown(void);
 
@@ -123,4 +126,6 @@ int virNetlinkEventAddClient(virNetlinkEventHandleCallback 
handleCB,
 int virNetlinkEventRemoveClient(int watch, const virMacAddr *macaddr,
 unsigned int protocol);
 
+VIR_DEFINE_AUTOPTR_FUNC(virNlMsg, nlmsg_free)
+
 #endif /* __VIR_NETLINK_H__ */
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v2 09/35] util: netdev: define cleanup function using VIR_DEFINE_AUTOPTR_FUNC

2018-08-08 Thread Sukrit Bhatnagar
Using the new VIR_DEFINE_AUTOPTR_FUNC macro defined in
src/util/viralloc.h, define a new wrapper around an existing
cleanup function which will be called when a variable declared
with VIR_AUTOPTR macro goes out of scope. Also, drop the redundant
viralloc.h include, since that has moved from the source module into
the header.

When variables of type virNetDevRxFilterPtr and virNetDevMcastEntryPtr
are declared using VIR_AUTOPTR, the functions virNetDevRxFilterFree
and virNetDevMcastEntryFree, respectively, will be run
automatically on them when they go out of scope.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virnetdev.c | 9 -
 src/util/virnetdev.h | 4 
 2 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/src/util/virnetdev.c b/src/util/virnetdev.c
index 0777eca..9eca786 100644
--- a/src/util/virnetdev.c
+++ b/src/util/virnetdev.c
@@ -29,7 +29,6 @@
 #include "virfile.h"
 #include "virerror.h"
 #include "vircommand.h"
-#include "viralloc.h"
 #include "virpci.h"
 #include "virlog.h"
 #include "virstring.h"
@@ -120,6 +119,14 @@ struct _virNetDevMcastEntry  {
 virMacAddr macaddr;
 };
 
+static void
+virNetDevMcastEntryFree(virNetDevMcastEntryPtr entry)
+{
+VIR_FREE(entry);
+}
+
+VIR_DEFINE_AUTOPTR_FUNC(virNetDevMcastEntry, virNetDevMcastEntryFree)
+
 typedef struct _virNetDevMcastList virNetDevMcastList;
 typedef virNetDevMcastList *virNetDevMcastListPtr;
 struct _virNetDevMcastList {
diff --git a/src/util/virnetdev.h b/src/util/virnetdev.h
index 71eaf45..8860ea1 100644
--- a/src/util/virnetdev.h
+++ b/src/util/virnetdev.h
@@ -30,6 +30,7 @@
 # include "virmacaddr.h"
 # include "virpci.h"
 # include "virnetdevvlan.h"
+# include "viralloc.h"
 
 # ifdef HAVE_STRUCT_IFREQ
 typedef struct ifreq virIfreq;
@@ -313,4 +314,7 @@ int virNetDevSysfsFile(char **pf_sysfs_device_link,
 
 int virNetDevRunEthernetScript(const char *ifname, const char *script)
 ATTRIBUTE_NOINLINE;
+
+VIR_DEFINE_AUTOPTR_FUNC(virNetDevRxFilter, virNetDevRxFilterFree)
+
 #endif /* __VIR_NETDEV_H__ */
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v2 16/35] util: netdevip: define cleanup function using VIR_DEFINE_AUTOPTR_FUNC

2018-08-08 Thread Sukrit Bhatnagar
Using the new VIR_DEFINE_AUTOPTR_FUNC macro defined in
src/util/viralloc.h, define a new wrapper around an existing
cleanup function which will be called when a variable declared
with VIR_AUTOPTR macro goes out of scope. Also, drop the redundant
viralloc.h include, since that has moved from the source module into
the header.

When variables of type virNetDevIPAddrPtr and virNetDevIPRoutePtr
are declared using VIR_AUTOPTR, the functions virNetDevIPAddrFree
and virNetDevIPRouteFree, respectively, will be run
automatically on them when they go out of scope.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virnetdevip.c | 1 -
 src/util/virnetdevip.h | 4 
 2 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/src/util/virnetdevip.c b/src/util/virnetdevip.c
index 7197d07..fdb0b74 100644
--- a/src/util/virnetdevip.c
+++ b/src/util/virnetdevip.c
@@ -27,7 +27,6 @@
 #include "virnetlink.h"
 #include "virfile.h"
 #include "virerror.h"
-#include "viralloc.h"
 #include "virlog.h"
 #include "virstring.h"
 #include "virutil.h"
diff --git a/src/util/virnetdevip.h b/src/util/virnetdevip.h
index dfc978d..9cfd27c 100644
--- a/src/util/virnetdevip.h
+++ b/src/util/virnetdevip.h
@@ -24,6 +24,7 @@
 # define __VIR_NETDEVIP_H__
 
 # include "virsocketaddr.h"
+# include "viralloc.h"
 
 typedef struct _virNetDevIPAddr virNetDevIPAddr;
 typedef virNetDevIPAddr *virNetDevIPAddrPtr;
@@ -98,4 +99,7 @@ void virNetDevIPInfoClear(virNetDevIPInfoPtr ip);
 int virNetDevIPInfoAddToDev(const char *ifname,
 virNetDevIPInfo const *ipInfo);
 
+VIR_DEFINE_AUTOPTR_FUNC(virNetDevIPAddr, virNetDevIPAddrFree)
+VIR_DEFINE_AUTOPTR_FUNC(virNetDevIPRoute, virNetDevIPRouteFree)
+
 #endif /* __VIR_NETDEVIP_H__ */
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v2 12/35] util: socketaddr: define cleanup function using VIR_DEFINE_AUTOPTR_FUNC

2018-08-08 Thread Sukrit Bhatnagar
Using the new VIR_DEFINE_AUTOPTR_FUNC macro defined in
src/util/viralloc.h, define a new wrapper around an existing
cleanup function which will be called when a variable declared
with VIR_AUTOPTR macro goes out of scope. Also, drop the redundant
viralloc.h include, since that has moved from the source module into
the header.

When a variable of type virSocketAddrPtr is declared using
VIR_AUTOPTR, the function virSocketAddrFree will be run
automatically on it when it goes out of scope.

Signed-off-by: Sukrit Bhatnagar 
Reviewed-by: Erik Skultety 
---
 src/util/virsocketaddr.c | 7 ++-
 src/util/virsocketaddr.h | 9 +++--
 2 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/src/util/virsocketaddr.c b/src/util/virsocketaddr.c
index 5c3bfad..6b52a3d 100644
--- a/src/util/virsocketaddr.c
+++ b/src/util/virsocketaddr.c
@@ -26,7 +26,6 @@
 #include "virsocketaddr.h"
 #include "virerror.h"
 #include "virstring.h"
-#include "viralloc.h"
 #include "virbuffer.h"
 
 #include 
@@ -1253,3 +1252,9 @@ virSocketAddrPTRDomain(const virSocketAddr *addr,
 ret = -2;
 goto cleanup;
 }
+
+void
+virSocketAddrFree(virSocketAddrPtr addr)
+{
+VIR_FREE(addr);
+}
diff --git a/src/util/virsocketaddr.h b/src/util/virsocketaddr.h
index 3029338..66f5998 100644
--- a/src/util/virsocketaddr.h
+++ b/src/util/virsocketaddr.h
@@ -24,14 +24,15 @@
 #ifndef __VIR_SOCKETADDR_H__
 # define __VIR_SOCKETADDR_H__
 
-# include "internal.h"
-
 # include 
 # include 
 # ifdef HAVE_SYS_UN_H
 #  include 
 # endif
 
+# include "internal.h"
+# include "viralloc.h"
+
 /* On architectures which lack these limits, define them (ie. Cygwin).
  * Note that the libvirt code should be robust enough to handle the
  * case where actual value is longer than these limits (eg. by setting
@@ -162,4 +163,8 @@ int virSocketAddrPTRDomain(const virSocketAddr *addr,
char **ptr)
 ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3);
 
+void virSocketAddrFree(virSocketAddrPtr addr);
+
+VIR_DEFINE_AUTOPTR_FUNC(virSocketAddr, virSocketAddrFree)
+
 #endif /* __VIR_SOCKETADDR_H__ */
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v2 08/35] util: macaddr: define cleanup function using VIR_DEFINE_AUTOPTR_FUNC

2018-08-08 Thread Sukrit Bhatnagar
Using the new VIR_DEFINE_AUTOPTR_FUNC macro defined in
src/util/viralloc.h, define a new wrapper around an existing
cleanup function which will be called when a variable declared
with VIR_AUTOPTR macro goes out of scope. Also, drop the redundant
viralloc.h include, since that has moved from the source module into
the header.

When a variable of type virMacAddrPtr is declared using VIR_AUTOPTR,
the function virMacAddrFree will be run automatically on it when it
goes out of scope.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virmacaddr.c | 6 ++
 src/util/virmacaddr.h | 4 
 2 files changed, 10 insertions(+)

diff --git a/src/util/virmacaddr.c b/src/util/virmacaddr.c
index 7afe032..e739775 100644
--- a/src/util/virmacaddr.c
+++ b/src/util/virmacaddr.c
@@ -252,3 +252,9 @@ virMacAddrIsBroadcastRaw(const unsigned char 
s[VIR_MAC_BUFLEN])
 {
 return memcmp(virMacAddrBroadcastAddrRaw, s, sizeof(*s)) == 0;
 }
+
+void
+virMacAddrFree(virMacAddrPtr addr)
+{
+VIR_FREE(addr);
+}
diff --git a/src/util/virmacaddr.h b/src/util/virmacaddr.h
index d0dd4a4..39dd51b 100644
--- a/src/util/virmacaddr.h
+++ b/src/util/virmacaddr.h
@@ -25,6 +25,7 @@
 # define __VIR_MACADDR_H__
 
 # include "internal.h"
+# include "viralloc.h"
 
 # define VIR_MAC_BUFLEN 6
 # define VIR_MAC_HEXLEN (VIR_MAC_BUFLEN * 2)
@@ -64,5 +65,8 @@ int virMacAddrParseHex(const char* str,
 bool virMacAddrIsUnicast(const virMacAddr *addr);
 bool virMacAddrIsMulticast(const virMacAddr *addr);
 bool virMacAddrIsBroadcastRaw(const unsigned char s[VIR_MAC_BUFLEN]);
+void virMacAddrFree(virMacAddrPtr addr);
+
+VIR_DEFINE_AUTOPTR_FUNC(virMacAddr, virMacAddrFree)
 
 #endif /* __VIR_MACADDR_H__ */
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v2 01/35] util: iscsi: use VIR_AUTOFREE instead of VIR_FREE for scalar types

2018-08-08 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOFREE macro for declaring scalar variables, majority
of the VIR_FREE calls can be dropped, which in turn leads to
getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
Reviewed-by: Erik Skultety 
---
 src/util/viriscsi.c | 44 +---
 1 file changed, 17 insertions(+), 27 deletions(-)

diff --git a/src/util/viriscsi.c b/src/util/viriscsi.c
index f00aeb5..0c2d8bb 100644
--- a/src/util/viriscsi.c
+++ b/src/util/viriscsi.c
@@ -88,8 +88,8 @@ virISCSIGetSession(const char *devpath,
 .session = NULL,
 .devpath = devpath,
 };
-char *error = NULL;
 int exitstatus = 0;
+VIR_AUTOFREE(char *) error = NULL;
 
 virCommandPtr cmd = virCommandNewArgList(ISCSIADM, "--mode",
  "session", NULL);
@@ -109,7 +109,6 @@ virISCSIGetSession(const char *devpath,
NULLSTR(error));
 
  cleanup:
-VIR_FREE(error);
 virCommandFree(cmd);
 return cbdata.session;
 }
@@ -125,12 +124,11 @@ virStorageBackendIQNFound(const char *initiatoriqn,
   char **ifacename)
 {
 int ret = IQN_ERROR;
-char *outbuf = NULL;
 char *line = NULL;
-char *iface = NULL;
-char *iqn = NULL;
 virCommandPtr cmd = virCommandNewArgList(ISCSIADM,
  "--mode", "iface", NULL);
+VIR_AUTOFREE(char *) outbuf = NULL;
+VIR_AUTOFREE(char *) iqn = NULL;
 
 *ifacename = NULL;
 
@@ -150,13 +148,13 @@ virStorageBackendIQNFound(const char *initiatoriqn,
 char *newline;
 char *next;
 size_t i;
+VIR_AUTOFREE(char *) iface = NULL;
 
 if (!(newline = strchr(line, '\n')))
 break;
 
 *newline = '\0';
 
-VIR_FREE(iface);
 VIR_FREE(iqn);
 
 /* Find the first space, copy everything up to that point into
@@ -197,9 +195,6 @@ virStorageBackendIQNFound(const char *initiatoriqn,
 if (ret == IQN_MISSING)
 VIR_DEBUG("Could not find interface with IQN '%s'", iqn);
 
-VIR_FREE(iqn);
-VIR_FREE(iface);
-VIR_FREE(outbuf);
 virCommandFree(cmd);
 return ret;
 
@@ -216,8 +211,9 @@ virStorageBackendCreateIfaceIQN(const char *initiatoriqn,
 char **ifacename)
 {
 int ret = -1, exitstatus = -1;
-char *temp_ifacename;
 virCommandPtr cmd = NULL;
+VIR_AUTOFREE(char *) iface_name = NULL;
+VIR_AUTOFREE(char *) temp_ifacename = NULL;
 
 if (virAsprintf(_ifacename,
 "libvirt-iface-%08llx",
@@ -263,23 +259,23 @@ virStorageBackendCreateIfaceIQN(const char *initiatoriqn,
 }
 
 /* Check again to make sure the interface was created. */
-if (virStorageBackendIQNFound(initiatoriqn, ifacename) != IQN_FOUND) {
+if (virStorageBackendIQNFound(initiatoriqn, _name) != IQN_FOUND) {
 VIR_DEBUG("Failed to find interface '%s' with IQN '%s' "
   "after attempting to create it",
   _ifacename[0], initiatoriqn);
 goto cleanup;
 } else {
 VIR_DEBUG("Interface '%s' with IQN '%s' was created successfully",
-  *ifacename, initiatoriqn);
+  iface_name, initiatoriqn);
 }
 
-ret = 0;
+VIR_STEAL_PTR(*ifacename, iface_name);
+
+virCommandFree(cmd);
+return 0;
 
  cleanup:
 virCommandFree(cmd);
-VIR_FREE(temp_ifacename);
-if (ret != 0)
-VIR_FREE(*ifacename);
 return ret;
 }
 
@@ -299,7 +295,7 @@ virISCSIConnection(const char *portal,
 NULL
 };
 virCommandPtr cmd;
-char *ifacename = NULL;
+VIR_AUTOFREE(char *) ifacename = NULL;
 
 cmd = virCommandNewArgs(baseargv);
 virCommandAddArgSet(cmd, extraargv);
@@ -339,7 +335,6 @@ virISCSIConnection(const char *portal,
 
  cleanup:
 virCommandFree(cmd);
-VIR_FREE(ifacename);
 
 return ret;
 }
@@ -390,15 +385,13 @@ virISCSIGetTargets(char **const groups,
void *data)
 {
 struct virISCSITargetList *list = data;
-char *target;
+VIR_AUTOFREE(char *) target = NULL;
 
 if (VIR_STRDUP(target, groups[1]) < 0)
 return -1;
 
-if (VIR_APPEND_ELEMENT(list->targets, list->ntargets, target) < 0) {
-VIR_FREE(target);
+if (VIR_APPEND_ELEMENT(list->targets, list->ntargets, target) < 0)
 return -1;
-}
 
 return 0;
 }
@@ -498,8 +491,7 @@ virISCSIScanTargets(const char *portal,
 size_t *ntargets,
 char ***targets)
 {
-char *ifacename = NULL;
-int ret = -1;
+VIR_AUTOFREE(char *) ifacename = NULL;
 
 if (ntargets)
 *ntargets = 0;
@@ -522,10 +514,8 @@ virISCSIScanTargets(const char *portal,
 }
 }
 
-ret = virISCSIScanTargetsInternal(portal, iface

[libvirt] [PATCH v2 10/35] util: netdev: use VIR_AUTOFREE instead of VIR_FREE for scalar types

2018-08-08 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOFREE macro for declaring scalar variables, majority
of the VIR_FREE calls can be dropped, which in turn leads to
getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virnetdev.c | 328 +++
 1 file changed, 119 insertions(+), 209 deletions(-)

diff --git a/src/util/virnetdev.c b/src/util/virnetdev.c
index 9eca786..5651f6f 100644
--- a/src/util/virnetdev.c
+++ b/src/util/virnetdev.c
@@ -535,18 +535,17 @@ int virNetDevSetMTUFromDevice(const char *ifname,
  */
 int virNetDevSetNamespace(const char *ifname, pid_t pidInNs)
 {
-int ret = -1;
-char *pid = NULL;
-char *phy = NULL;
-char *phy_path = NULL;
 int len;
+VIR_AUTOFREE(char *) pid = NULL;
+VIR_AUTOFREE(char *) phy = NULL;
+VIR_AUTOFREE(char *) phy_path = NULL;
 
 if (virAsprintf(, "%lld", (long long) pidInNs) == -1)
 return -1;
 
 /* The 802.11 wireless devices only move together with their PHY. */
 if (virNetDevSysfsFile(_path, ifname, "phy80211/name") < 0)
-goto cleanup;
+return -1;
 
 if ((len = virFileReadAllQuiet(phy_path, 1024, )) <= 0) {
 /* Not a wireless device. */
@@ -556,7 +555,7 @@ int virNetDevSetNamespace(const char *ifname, pid_t pidInNs)
 
 argv[5] = pid;
 if (virRun(argv, NULL) < 0)
-goto cleanup;
+return -1;
 
 } else {
 const char *argv[] = {
@@ -569,15 +568,10 @@ int virNetDevSetNamespace(const char *ifname, pid_t 
pidInNs)
 argv[2] = phy;
 argv[5] = pid;
 if (virRun(argv, NULL) < 0)
-goto cleanup;
+return -1;
 }
 
-ret = 0;
- cleanup:
-VIR_FREE(phy_path);
-VIR_FREE(phy);
-VIR_FREE(pid);
-return ret;
+return 0;
 }
 
 #if defined(SIOCSIFNAME) && defined(HAVE_STRUCT_IFREQ)
@@ -969,25 +963,21 @@ int virNetDevGetIndex(const char *ifname ATTRIBUTE_UNUSED,
 int
 virNetDevGetMaster(const char *ifname, char **master)
 {
-int ret = -1;
-void *nlData = NULL;
 struct nlattr *tb[IFLA_MAX + 1] = {NULL, };
+VIR_AUTOFREE(void *) nlData = NULL;
 
 *master = NULL;
 
 if (virNetlinkDumpLink(ifname, -1, , tb, 0, 0) < 0)
-goto cleanup;
+return -1;
 
 if (tb[IFLA_MASTER]) {
 if (!(*master = virNetDevGetName(*(int *)RTA_DATA(tb[IFLA_MASTER]
-goto cleanup;
+return -1;
 }
 
 VIR_DEBUG("IFLA_MASTER for %s is %s", ifname, *master ? *master : 
"(none)");
-ret = 0;
- cleanup:
-VIR_FREE(nlData);
-return ret;
+return 0;
 }
 
 
@@ -1168,39 +1158,33 @@ virNetDevSysfsDeviceFile(char **pf_sysfs_device_link, 
const char *ifname,
 static bool
 virNetDevIsPCIDevice(const char *devpath)
 {
-char *subsys_link = NULL;
-char *abs_path = NULL;
 char *subsys = NULL;
-bool ret = false;
+VIR_AUTOFREE(char *) subsys_link = NULL;
+VIR_AUTOFREE(char *) abs_path = NULL;
 
 if (virAsprintf(_link, "%s/subsystem", devpath) < 0)
 return false;
 
 if (!virFileExists(subsys_link))
-goto cleanup;
+return false;
 
 if (virFileResolveLink(subsys_link, _path) < 0) {
 virReportError(VIR_ERR_INTERNAL_ERROR,
_("Unable to resolve device subsystem symlink %s"),
subsys_link);
-goto cleanup;
+return false;
 }
 
 subsys = last_component(abs_path);
-ret = STRPREFIX(subsys, "pci");
-
- cleanup:
-VIR_FREE(subsys_link);
-VIR_FREE(abs_path);
-return ret;
+return STRPREFIX(subsys, "pci");
 }
 
 static virPCIDevicePtr
 virNetDevGetPCIDevice(const char *devName)
 {
-char *vfSysfsDevicePath = NULL;
 virPCIDeviceAddressPtr vfPCIAddr = NULL;
 virPCIDevicePtr vfPCIDevice = NULL;
+VIR_AUTOFREE(char *) vfSysfsDevicePath = NULL;
 
 if (virNetDevSysfsFile(, devName, "device") < 0)
 goto cleanup;
@@ -1216,7 +1200,6 @@ virNetDevGetPCIDevice(const char *devName)
   vfPCIAddr->slot, vfPCIAddr->function);
 
  cleanup:
-VIR_FREE(vfSysfsDevicePath);
 VIR_FREE(vfPCIAddr);
 
 return vfPCIDevice;
@@ -1241,25 +1224,20 @@ int
 virNetDevGetPhysPortID(const char *ifname,
char **physPortID)
 {
-int ret = -1;
-char *physPortIDFile = NULL;
+VIR_AUTOFREE(char *) physPortIDFile = NULL;
 
 *physPortID = NULL;
 
 if (virNetDevSysfsFile(, ifname, "phys_port_id") < 0)
-goto cleanup;
+return -1;
 
 /* a failure to read just means the driver doesn't support
- * phys_port_id, so set success now and ignore the return from
- * virFileReadAllQuiet().
+ * phys_port_id, so ignore the return from virFileReadAllQuiet()
+ * and return success.

[libvirt] [PATCH v2 00/35] use GNU C's cleanup attribute in src/util (batch III)

2018-08-08 Thread Sukrit Bhatnagar
This third series of patches also modifies a few files in src/util
to use VIR_AUTOFREE and VIR_AUTOPTR for automatic freeing of memory
and get rid of some VIR_FREE macro invocations and *Free function
calls.

Sukrit Bhatnagar (35):
  util: iscsi: use VIR_AUTOFREE instead of VIR_FREE for scalar types
  util: iscsi: use VIR_AUTOPTR for aggregate types
  util: netlink: define cleanup function using VIR_DEFINE_AUTOPTR_FUNC
  util: netlink: use VIR_AUTOFREE instead of VIR_FREE for scalar types
  util: netlink: use VIR_AUTOPTR for aggregate types
  util: netdevbridge: use VIR_AUTOFREE instead of VIR_FREE for scalar
types
  util: netdevbridge: use VIR_AUTOPTR for aggregate types
  util: macaddr: define cleanup function using VIR_DEFINE_AUTOPTR_FUNC
  util: netdev: define cleanup function using VIR_DEFINE_AUTOPTR_FUNC
  util: netdev: use VIR_AUTOFREE instead of VIR_FREE for scalar types
  util: netdev: use VIR_AUTOPTR for aggregate types
  util: socketaddr: define cleanup function using
VIR_DEFINE_AUTOPTR_FUNC
  util: socketaddr: use VIR_AUTOFREE instead of VIR_FREE for scalar
types
  util: socketaddr: use VIR_AUTOPTR for aggregate types
  util: netdevip: define virNetDevIPAddrFree for use with cleanup macros
  util: netdevip: define cleanup function using VIR_DEFINE_AUTOPTR_FUNC
  util: netdevip: use VIR_AUTOFREE instead of VIR_FREE for scalar types
  util: netdevip: use VIR_AUTOPTR for aggregate types
  util: netdevmacvlan: define cleanup function using
VIR_DEFINE_AUTOPTR_FUNC
  util: netdevmacvlan: use VIR_AUTOFREE instead of VIR_FREE for scalar
types
  util: netdevmacvlan: use VIR_AUTOPTR for aggregate types
  util: netdevopenvswitch: use VIR_AUTOFREE instead of VIR_FREE for
scalar types
  util: netdevopenvswitch: use VIR_AUTOPTR for aggregate types
  util: netdevtap: use VIR_AUTOFREE instead of VIR_FREE for scalar types
  util: netdevveth: use VIR_AUTOFREE instead of VIR_FREE for scalar
types
  util: netdevveth: use VIR_AUTOPTR for aggregate types
  util: numa: use VIR_AUTOFREE instead of VIR_FREE for scalar types
  util: numa: use VIR_AUTOPTR for aggregate types
  util: perf: define cleanup function using VIR_DEFINE_AUTOPTR_FUNC
  util: perf: use VIR_AUTOFREE instead of VIR_FREE for scalar types
  util: pidfile: use VIR_AUTOFREE instead of VIR_FREE for scalar types
  util: process: use VIR_AUTOFREE instead of VIR_FREE for scalar types
  util: process: use VIR_AUTOPTR for aggregate types
  util: qemu: use VIR_AUTOFREE instead of VIR_FREE for scalar types
  util: qemu: use VIR_AUTOPTR for aggregate types

 src/util/viriscsi.c | 140 --
 src/util/virmacaddr.c   |   6 +
 src/util/virmacaddr.h   |   4 +
 src/util/virnetdev.c| 604 
 src/util/virnetdev.h|   4 +
 src/util/virnetdevbridge.c  |  81 ++
 src/util/virnetdevip.c  | 227 ++-
 src/util/virnetdevip.h  |   5 +
 src/util/virnetdevmacvlan.c |  22 +-
 src/util/virnetdevopenvswitch.c |  98 +++
 src/util/virnetdevtap.c |  11 +-
 src/util/virnetdevveth.c|  32 +--
 src/util/virnetlink.c   | 118 
 src/util/virnetlink.h   |   5 +
 src/util/virnuma.c  | 103 +++
 src/util/virperf.c  |  20 +-
 src/util/virperf.h  |   8 +-
 src/util/virpidfile.c   | 186 -
 src/util/virprocess.c   |  58 ++--
 src/util/virqemu.c  |  31 +--
 src/util/virsocketaddr.c| 113 
 src/util/virsocketaddr.h|   9 +-
 22 files changed, 730 insertions(+), 1155 deletions(-)

-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v2 06/35] util: netdevbridge: use VIR_AUTOFREE instead of VIR_FREE for scalar types

2018-08-08 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOFREE macro for declaring scalar variables, majority
of the VIR_FREE calls can be dropped, which in turn leads to
getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virnetdevbridge.c | 46 --
 1 file changed, 16 insertions(+), 30 deletions(-)

diff --git a/src/util/virnetdevbridge.c b/src/util/virnetdevbridge.c
index e46ac35..fe3697b 100644
--- a/src/util/virnetdevbridge.c
+++ b/src/util/virnetdevbridge.c
@@ -126,8 +126,7 @@ static int virNetDevBridgeSet(const char *brname,
   int fd, /* control socket */
   struct ifreq *ifr)  /* pre-filled bridge 
name */
 {
-char *path = NULL;
-int ret = -1;
+VIR_AUTOFREE(char *) path = NULL;
 
 if (virAsprintf(, SYSFS_NET_DIR "%s/bridge/%s", brname, paramname) < 
0)
 return -1;
@@ -138,7 +137,7 @@ static int virNetDevBridgeSet(const char *brname,
 if (virFileWriteStr(path, valuestr, 0) < 0) {
 virReportSystemError(errno,
  _("Unable to set bridge %s %s"), brname, 
paramname);
-goto cleanup;
+return -1;
 }
 } else {
 unsigned long paramid;
@@ -149,21 +148,18 @@ static int virNetDevBridgeSet(const char *brname,
 } else {
 virReportSystemError(EINVAL,
  _("Unable to set bridge %s %s"), brname, 
paramname);
-goto cleanup;
+return -1;
 }
 unsigned long args[] = { paramid, value, 0, 0 };
 ifr->ifr_data = (char*)
 if (ioctl(fd, SIOCDEVPRIVATE, ifr) < 0) {
 virReportSystemError(errno,
  _("Unable to set bridge %s %s"), brname, 
paramname);
-goto cleanup;
+return -1;
 }
 }
 
-ret = 0;
- cleanup:
-VIR_FREE(path);
-return ret;
+return 0;
 }
 
 
@@ -171,16 +167,17 @@ static int virNetDevBridgeGet(const char *brname,
   const char *paramname,  /* sysfs param name */
   unsigned long *value)   /* current value */
 {
-char *path = NULL;
 int ret = -1;
 int fd = -1;
 struct ifreq ifr;
+VIR_AUTOFREE(char *) path = NULL;
 
 if (virAsprintf(, SYSFS_NET_DIR "%s/bridge/%s", brname, paramname) < 
0)
 return -1;
 
 if (virFileExists(path)) {
-char *valuestr;
+VIR_AUTOFREE(char *) valuestr = NULL;
+
 if (virFileReadAll(path, INT_BUFSIZE_BOUND(unsigned long),
) < 0)
 goto cleanup;
@@ -189,10 +186,8 @@ static int virNetDevBridgeGet(const char *brname,
 virReportSystemError(EINVAL,
  _("Unable to get bridge %s %s"),
  brname, paramname);
-VIR_FREE(valuestr);
 goto cleanup;
 }
-VIR_FREE(valuestr);
 } else {
 struct __bridge_info info;
 unsigned long args[] = { BRCTL_GET_BRIDGE_INFO, (unsigned long), 
0, 0 };
@@ -221,7 +216,6 @@ static int virNetDevBridgeGet(const char *brname,
 ret = 0;
  cleanup:
 VIR_FORCE_CLOSE(fd);
-VIR_FREE(path);
 return ret;
 }
 #endif /* __linux__ */
@@ -233,9 +227,9 @@ virNetDevBridgePortSet(const char *brname,
const char *paramname,
unsigned long value)
 {
-char *path = NULL;
 char valuestr[INT_BUFSIZE_BOUND(value)];
 int ret = -1;
+VIR_AUTOFREE(char *) path = NULL;
 
 snprintf(valuestr, sizeof(valuestr), "%lu", value);
 
@@ -254,7 +248,6 @@ virNetDevBridgePortSet(const char *brname,
  brname, ifname, paramname, valuestr);
 }
 
-VIR_FREE(path);
 return ret;
 }
 
@@ -265,29 +258,24 @@ virNetDevBridgePortGet(const char *brname,
const char *paramname,
unsigned long *value)
 {
-char *path = NULL;
-char *valuestr = NULL;
-int ret = -1;
+VIR_AUTOFREE(char *) path = NULL;
+VIR_AUTOFREE(char *) valuestr = NULL;
 
 if (virAsprintf(, SYSFS_NET_DIR "%s/brif/%s/%s",
 brname, ifname, paramname) < 0)
 return -1;
 
 if (virFileReadAll(path, INT_BUFSIZE_BOUND(unsigned long), ) < 0)
-goto cleanup;
+return -1;
 
 if (virStrToLong_ul(valuestr, NULL, 10, value) < 0) {
 virReportSystemError(EINVAL,
  _("Unable to get bridge %s port %s %s"),
  brname, ifname, paramname);
-goto cleanup;
+return -1;
 }
 
-ret = 0;
- cleanup:
-VIR_FREE(path);
-VIR_FREE(valuestr);
-return ret;
+return 0;
 }
 
 
@@ -430,1

[libvirt] [PATCH v2 07/35] util: netdevbridge: use VIR_AUTOPTR for aggregate types

2018-08-08 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOPTR macro for declaring aggregate pointer variables,
majority of the calls to *Free functions can be dropped, which
in turn leads to getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virnetdevbridge.c | 35 +--
 1 file changed, 13 insertions(+), 22 deletions(-)

diff --git a/src/util/virnetdevbridge.c b/src/util/virnetdevbridge.c
index fe3697b..089da78 100644
--- a/src/util/virnetdevbridge.c
+++ b/src/util/virnetdevbridge.c
@@ -417,12 +417,11 @@ virNetDevBridgeCreate(const char *brname)
 {
 /* use a netlink RTM_NEWLINK message to create the bridge */
 const char *type = "bridge";
-int rc = -1;
 struct nlmsgerr *err;
 struct ifinfomsg ifinfo = { .ifi_family = AF_UNSPEC };
 unsigned int recvbuflen;
-struct nl_msg *nl_msg;
 struct nlattr *linkinfo;
+VIR_AUTOPTR(virNlMsg) nl_msg = NULL;
 VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
 
 nl_msg = nlmsg_alloc_simple(RTM_NEWLINK,
@@ -444,7 +443,7 @@ virNetDevBridgeCreate(const char *brname)
 
 if (virNetlinkCommand(nl_msg, , , 0, 0,
   NETLINK_ROUTE, 0) < 0) {
-goto cleanup;
+return -1;
 }
 
 if (recvbuflen < NLMSG_LENGTH(0) || resp == NULL)
@@ -462,15 +461,14 @@ virNetDevBridgeCreate(const char *brname)
 /* fallback to ioctl if netlink doesn't support creating
  * bridges
  */
-rc = virNetDevBridgeCreateWithIoctl(brname);
-goto cleanup;
+return virNetDevBridgeCreateWithIoctl(brname);
 }
 # endif
 
 virReportSystemError(-err->error,
  _("error creating bridge interface %s"),
  brname);
-goto cleanup;
+return -1;
 }
 break;
 
@@ -480,19 +478,16 @@ virNetDevBridgeCreate(const char *brname)
 goto malformed_resp;
 }
 
-rc = 0;
- cleanup:
-nlmsg_free(nl_msg);
-return rc;
+return 0;
 
  malformed_resp:
 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("malformed netlink response message"));
-goto cleanup;
+return -1;
  buffer_too_small:
 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("allocated netlink buffer is too small"));
-goto cleanup;
+return -1;
 }
 
 
@@ -1055,11 +1050,10 @@ static int
 virNetDevBridgeFDBAddDel(const virMacAddr *mac, const char *ifname,
  unsigned int flags, bool isAdd)
 {
-int ret = -1;
 struct nlmsgerr *err;
 unsigned int recvbuflen;
-struct nl_msg *nl_msg;
 struct ndmsg ndm = { .ndm_family = PF_BRIDGE, .ndm_state = NUD_NOARP };
+VIR_AUTOPTR(virNlMsg) nl_msg = NULL;
 VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
 
 if (virNetDevGetIndex(ifname, _ifindex) < 0)
@@ -1103,7 +1097,7 @@ virNetDevBridgeFDBAddDel(const virMacAddr *mac, const 
char *ifname,
 
 if (virNetlinkCommand(nl_msg, , , 0, 0,
   NETLINK_ROUTE, 0) < 0) {
-goto cleanup;
+return -1;
 }
 if (recvbuflen < NLMSG_LENGTH(0) || resp == NULL)
 goto malformed_resp;
@@ -1116,7 +1110,7 @@ virNetDevBridgeFDBAddDel(const virMacAddr *mac, const 
char *ifname,
 if (err->error) {
 virReportSystemError(-err->error,
  _("error adding fdb entry for %s"), ifname);
-goto cleanup;
+return -1;
 }
 break;
 case NLMSG_DONE:
@@ -1126,20 +1120,17 @@ virNetDevBridgeFDBAddDel(const virMacAddr *mac, const 
char *ifname,
 goto malformed_resp;
 }
 
-ret = 0;
- cleanup:
-nlmsg_free(nl_msg);
-return ret;
+return 0;
 
  malformed_resp:
 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("malformed netlink response message"));
-goto cleanup;
+return -1;
 
  buffer_too_small:
 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("allocated netlink buffer is too small"));
-goto cleanup;
+return -1;
 }
 
 
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v2 05/35] util: netlink: use VIR_AUTOPTR for aggregate types

2018-08-08 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOPTR macro for declaring aggregate pointer variables,
majority of the calls to *Free functions can be dropped, which
in turn leads to getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virnetlink.c | 88 +++
 1 file changed, 33 insertions(+), 55 deletions(-)

diff --git a/src/util/virnetlink.c b/src/util/virnetlink.c
index aa3a86d..66e80e2 100644
--- a/src/util/virnetlink.c
+++ b/src/util/virnetlink.c
@@ -297,15 +297,16 @@ int virNetlinkCommand(struct nl_msg *nl_msg,
   uint32_t src_pid, uint32_t dst_pid,
   unsigned int protocol, unsigned int groups)
 {
-int ret = -1;
 struct sockaddr_nl nladdr = {
 .nl_family = AF_NETLINK,
 .nl_pid= dst_pid,
 .nl_groups = 0,
 };
 struct pollfd fds[1];
-virNetlinkHandle *nlhandle = NULL;
 int len = 0;
+VIR_AUTOPTR(virNetlinkHandle) nlhandle = NULL;
+
+*respbuflen = 0;
 
 memset(fds, 0, sizeof(fds));
 
@@ -324,16 +325,12 @@ int virNetlinkCommand(struct nl_msg *nl_msg,
 goto cleanup;
 }
 
-ret = 0;
 *respbuflen = len;
- cleanup:
-if (ret < 0) {
-*resp = NULL;
-*respbuflen = 0;
-}
+return 0;
 
-virNetlinkFree(nlhandle);
-return ret;
+ cleanup:
+*resp = NULL;
+return -1;
 }
 
 int
@@ -343,7 +340,6 @@ virNetlinkDumpCommand(struct nl_msg *nl_msg,
   unsigned int protocol, unsigned int groups,
   void *opaque)
 {
-int ret = -1;
 bool end = false;
 int len = 0;
 struct nlmsghdr *msg = NULL;
@@ -353,11 +349,11 @@ virNetlinkDumpCommand(struct nl_msg *nl_msg,
 .nl_pid= dst_pid,
 .nl_groups = 0,
 };
-virNetlinkHandle *nlhandle = NULL;
+VIR_AUTOPTR(virNetlinkHandle) nlhandle = NULL;
 
 if (!(nlhandle = virNetlinkSendRequest(nl_msg, src_pid, nladdr,
protocol, groups)))
-goto cleanup;
+return -1;
 
 while (!end) {
 VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
@@ -370,18 +366,14 @@ virNetlinkDumpCommand(struct nl_msg *nl_msg,
 end = true;
 
 if (virNetlinkGetErrorCode(msg, len) < 0)
-goto cleanup;
+return -1;
 
 if (callback(msg, opaque) < 0)
-goto cleanup;
+return -1;
 }
 }
 
-ret = 0;
-
- cleanup:
-virNetlinkFree(nlhandle);
-return ret;
+return 0;
 }
 
 /**
@@ -415,7 +407,7 @@ virNetlinkDumpLink(const char *ifname, int ifindex,
 .ifi_index  = ifindex
 };
 unsigned int recvbuflen;
-struct nl_msg *nl_msg;
+VIR_AUTOPTR(virNlMsg) nl_msg = NULL;
 VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
 
 *nlData = NULL;
@@ -456,7 +448,7 @@ virNetlinkDumpLink(const char *ifname, int ifindex,
 
 if (virNetlinkCommand(nl_msg, , ,
   src_pid, dst_pid, NETLINK_ROUTE, 0) < 0)
-goto cleanup;
+return -1;
 
 if (recvbuflen < NLMSG_LENGTH(0) || resp == NULL)
 goto malformed_resp;
@@ -471,7 +463,7 @@ virNetlinkDumpLink(const char *ifname, int ifindex,
 virReportSystemError(-err->error,
  _("error dumping %s (%d) interface"),
  ifname, ifindex);
-goto cleanup;
+return -1;
 }
 break;
 
@@ -488,21 +480,17 @@ virNetlinkDumpLink(const char *ifname, int ifindex,
 }
 
 VIR_STEAL_PTR(*nlData, resp);
-rc = 0;
-
- cleanup:
-nlmsg_free(nl_msg);
-return rc;
+return 0;
 
  malformed_resp:
 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("malformed netlink response message"));
-goto cleanup;
+return rc;
 
  buffer_too_small:
 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("allocated netlink buffer is too small"));
-goto cleanup;
+return rc;
 }
 
 
@@ -524,11 +512,10 @@ virNetlinkDumpLink(const char *ifname, int ifindex,
 int
 virNetlinkDelLink(const char *ifname, virNetlinkDelLinkFallback fallback)
 {
-int rc = -1;
 struct nlmsgerr *err;
 struct ifinfomsg ifinfo = { .ifi_family = AF_UNSPEC };
 unsigned int recvbuflen;
-struct nl_msg *nl_msg;
+VIR_AUTOPTR(virNlMsg) nl_msg = NULL;
 VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
 
 nl_msg = nlmsg_alloc_simple(RTM_DELLINK,
@@ -546,7 +533,7 @@ virNetlinkDelLink(const char *ifname, 
virNetlinkDelLinkFallback fallback)
 
 if (virNetlinkCommand(nl_msg, , , 0, 0,
   NETLINK_ROUTE, 0) < 0) {
-goto cleanup;
+return -1;
 }
 
 if (recvbuflen < NLMSG_LENGTH(0) || resp == NULL)
@@ -558,15 +545,14 @@ virNetlinkDelLink(

[libvirt] [PATCH v2 02/35] util: iscsi: use VIR_AUTOPTR for aggregate types

2018-08-08 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOPTR macro for declaring aggregate pointer variables,
majority of the calls to *Free functions can be dropped, which
in turn leads to getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/viriscsi.c | 100 +++-
 1 file changed, 36 insertions(+), 64 deletions(-)

diff --git a/src/util/viriscsi.c b/src/util/viriscsi.c
index 0c2d8bb..8c272b5 100644
--- a/src/util/viriscsi.c
+++ b/src/util/viriscsi.c
@@ -89,10 +89,10 @@ virISCSIGetSession(const char *devpath,
 .devpath = devpath,
 };
 int exitstatus = 0;
+VIR_AUTOPTR(virCommand) cmd = virCommandNewArgList(ISCSIADM, "--mode",
+   "session", NULL);
 VIR_AUTOFREE(char *) error = NULL;
 
-virCommandPtr cmd = virCommandNewArgList(ISCSIADM, "--mode",
- "session", NULL);
 virCommandSetErrorBuffer(cmd, );
 
 if (virCommandRunRegex(cmd,
@@ -101,15 +101,13 @@ virISCSIGetSession(const char *devpath,
vars,
virISCSIExtractSession,
, NULL, ) < 0)
-goto cleanup;
+return cbdata.session;
 
 if (cbdata.session == NULL && !probe)
 virReportError(VIR_ERR_INTERNAL_ERROR,
_("cannot find iscsiadm session: %s"),
NULLSTR(error));
 
- cleanup:
-virCommandFree(cmd);
 return cbdata.session;
 }
 
@@ -125,8 +123,8 @@ virStorageBackendIQNFound(const char *initiatoriqn,
 {
 int ret = IQN_ERROR;
 char *line = NULL;
-virCommandPtr cmd = virCommandNewArgList(ISCSIADM,
- "--mode", "iface", NULL);
+VIR_AUTOPTR(virCommand) cmd = virCommandNewArgList(ISCSIADM,
+   "--mode", "iface", 
NULL);
 VIR_AUTOFREE(char *) outbuf = NULL;
 VIR_AUTOFREE(char *) iqn = NULL;
 
@@ -195,7 +193,6 @@ virStorageBackendIQNFound(const char *initiatoriqn,
 if (ret == IQN_MISSING)
 VIR_DEBUG("Could not find interface with IQN '%s'", iqn);
 
-virCommandFree(cmd);
 return ret;
 
  error:
@@ -210,8 +207,8 @@ static int
 virStorageBackendCreateIfaceIQN(const char *initiatoriqn,
 char **ifacename)
 {
-int ret = -1, exitstatus = -1;
-virCommandPtr cmd = NULL;
+int exitstatus = -1;
+VIR_AUTOPTR(virCommand) cmd = NULL;
 VIR_AUTOFREE(char *) iface_name = NULL;
 VIR_AUTOFREE(char *) temp_ifacename = NULL;
 
@@ -236,7 +233,7 @@ virStorageBackendCreateIfaceIQN(const char *initiatoriqn,
 virReportError(VIR_ERR_INTERNAL_ERROR,
_("Failed to run command '%s' to create new iscsi 
interface"),
ISCSIADM);
-goto cleanup;
+return -1;
 }
 virCommandFree(cmd);
 
@@ -255,7 +252,7 @@ virStorageBackendCreateIfaceIQN(const char *initiatoriqn,
 virReportError(VIR_ERR_INTERNAL_ERROR,
_("Failed to run command '%s' to update iscsi interface 
with IQN '%s'"),
ISCSIADM, initiatoriqn);
-goto cleanup;
+return -1;
 }
 
 /* Check again to make sure the interface was created. */
@@ -263,7 +260,7 @@ virStorageBackendCreateIfaceIQN(const char *initiatoriqn,
 VIR_DEBUG("Failed to find interface '%s' with IQN '%s' "
   "after attempting to create it",
   _ifacename[0], initiatoriqn);
-goto cleanup;
+return -1;
 } else {
 VIR_DEBUG("Interface '%s' with IQN '%s' was created successfully",
   iface_name, initiatoriqn);
@@ -271,12 +268,7 @@ virStorageBackendCreateIfaceIQN(const char *initiatoriqn,
 
 VIR_STEAL_PTR(*ifacename, iface_name);
 
-virCommandFree(cmd);
 return 0;
-
- cleanup:
-virCommandFree(cmd);
-return ret;
 }
 
 
@@ -286,7 +278,6 @@ virISCSIConnection(const char *portal,
const char *target,
const char **extraargv)
 {
-int ret = -1;
 const char *const baseargv[] = {
 ISCSIADM,
 "--mode", "node",
@@ -294,7 +285,7 @@ virISCSIConnection(const char *portal,
 "--targetname", target,
 NULL
 };
-virCommandPtr cmd;
+VIR_AUTOPTR(virCommand) cmd = NULL;
 VIR_AUTOFREE(char *) ifacename = NULL;
 
 cmd = virCommandNewArgs(baseargv);
@@ -307,7 +298,7 @@ virISCSIConnection(const char *portal,
 break;
 case IQN_MISSING:
 if (virStorageBackendCreateIfaceIQN(initiatoriqn, ) != 0)
-goto cleanup;
+return -1;
 /*
  

[libvirt] [PATCH v2 04/35] util: netlink: use VIR_AUTOFREE instead of VIR_FREE for scalar types

2018-08-08 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOFREE macro for declaring scalar variables, majority
of the VIR_FREE calls can be dropped, which in turn leads to
getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virnetlink.c | 31 ---
 1 file changed, 16 insertions(+), 15 deletions(-)

diff --git a/src/util/virnetlink.c b/src/util/virnetlink.c
index 1c1eac7..aa3a86d 100644
--- a/src/util/virnetlink.c
+++ b/src/util/virnetlink.c
@@ -346,7 +346,6 @@ virNetlinkDumpCommand(struct nl_msg *nl_msg,
 int ret = -1;
 bool end = false;
 int len = 0;
-struct nlmsghdr *resp = NULL;
 struct nlmsghdr *msg = NULL;
 
 struct sockaddr_nl nladdr = {
@@ -361,6 +360,8 @@ virNetlinkDumpCommand(struct nl_msg *nl_msg,
 goto cleanup;
 
 while (!end) {
+VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
+
 len = nl_recv(nlhandle, , (unsigned char **), NULL);
 VIR_WARNINGS_NO_CAST_ALIGN
 for (msg = resp; NLMSG_OK(msg, len); msg = NLMSG_NEXT(msg, len)) {
@@ -374,13 +375,11 @@ virNetlinkDumpCommand(struct nl_msg *nl_msg,
 if (callback(msg, opaque) < 0)
 goto cleanup;
 }
-VIR_FREE(resp);
 }
 
 ret = 0;
 
  cleanup:
-VIR_FREE(resp);
 virNetlinkFree(nlhandle);
 return ret;
 }
@@ -410,7 +409,6 @@ virNetlinkDumpLink(const char *ifname, int ifindex,
uint32_t src_pid, uint32_t dst_pid)
 {
 int rc = -1;
-struct nlmsghdr *resp = NULL;
 struct nlmsgerr *err;
 struct ifinfomsg ifinfo = {
 .ifi_family = AF_UNSPEC,
@@ -418,6 +416,9 @@ virNetlinkDumpLink(const char *ifname, int ifindex,
 };
 unsigned int recvbuflen;
 struct nl_msg *nl_msg;
+VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
+
+*nlData = NULL;
 
 if (ifname && ifindex <= 0 && virNetDevGetIndex(ifname, ) < 0)
 return -1;
@@ -485,12 +486,12 @@ virNetlinkDumpLink(const char *ifname, int ifindex,
 default:
 goto malformed_resp;
 }
+
+VIR_STEAL_PTR(*nlData, resp);
 rc = 0;
+
  cleanup:
 nlmsg_free(nl_msg);
-if (rc < 0)
-   VIR_FREE(resp);
-*nlData = resp;
 return rc;
 
  malformed_resp:
@@ -524,11 +525,11 @@ int
 virNetlinkDelLink(const char *ifname, virNetlinkDelLinkFallback fallback)
 {
 int rc = -1;
-struct nlmsghdr *resp = NULL;
 struct nlmsgerr *err;
 struct ifinfomsg ifinfo = { .ifi_family = AF_UNSPEC };
 unsigned int recvbuflen;
 struct nl_msg *nl_msg;
+VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
 
 nl_msg = nlmsg_alloc_simple(RTM_DELLINK,
 NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL);
@@ -579,7 +580,6 @@ virNetlinkDelLink(const char *ifname, 
virNetlinkDelLinkFallback fallback)
 rc = 0;
  cleanup:
 nlmsg_free(nl_msg);
-VIR_FREE(resp);
 return rc;
 
  malformed_resp:
@@ -612,13 +612,15 @@ int
 virNetlinkGetNeighbor(void **nlData, uint32_t src_pid, uint32_t dst_pid)
 {
 int rc = -1;
-struct nlmsghdr *resp = NULL;
 struct nlmsgerr *err;
 struct ndmsg ndinfo = {
 .ndm_family = AF_UNSPEC,
 };
 unsigned int recvbuflen;
 struct nl_msg *nl_msg;
+VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
+
+*nlData = NULL;
 
 nl_msg = nlmsg_alloc_simple(RTM_GETNEIGH, NLM_F_DUMP | NLM_F_REQUEST);
 if (!nl_msg) {
@@ -656,13 +658,12 @@ virNetlinkGetNeighbor(void **nlData, uint32_t src_pid, 
uint32_t dst_pid)
 default:
 goto malformed_resp;
 }
+
+VIR_STEAL_PTR(*nlData, resp);
 rc = recvbuflen;
 
  cleanup:
 nlmsg_free(nl_msg);
-if (rc < 0)
-   VIR_FREE(resp);
-*nlData = resp;
 return rc;
 
  malformed_resp:
@@ -768,12 +769,12 @@ virNetlinkEventCallback(int watch,
 void *opaque)
 {
 virNetlinkEventSrvPrivatePtr srv = opaque;
-struct nlmsghdr *msg;
 struct sockaddr_nl peer;
 struct ucred *creds = NULL;
 size_t i;
 int length;
 bool handled = false;
+VIR_AUTOFREE(struct nlmsghdr *) msg = NULL;
 
 length = nl_recv(srv->netlinknh, ,
  (unsigned char **), );
@@ -803,7 +804,7 @@ virNetlinkEventCallback(int watch,
 
 if (!handled)
 VIR_DEBUG("event not handled.");
-VIR_FREE(msg);
+
 virNetlinkEventServerUnlock(srv);
 }
 
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


Re: [libvirt] [PATCH v1 17/32] util: netdevopenvswitch: use VIR_AUTOFREE instead of VIR_FREE for scalar types

2018-08-05 Thread Sukrit Bhatnagar
On Fri, 3 Aug 2018 at 19:02, Erik Skultety  wrote:
>
> On Sat, Jul 28, 2018 at 11:31:32PM +0530, Sukrit Bhatnagar wrote:
> > By making use of GNU C's cleanup attribute handled by the
> > VIR_AUTOFREE macro for declaring scalar variables, majority
> > of the VIR_FREE calls can be dropped, which in turn leads to
> > getting rid of most of our cleanup sections.
> >
> > Signed-off-by: Sukrit Bhatnagar 
> > ---
> ...
>
> > @@ -424,7 +419,6 @@ int
> >  virNetDevOpenvswitchInterfaceGetMaster(const char *ifname, char **master)
> >  {
> >  virCommandPtr cmd = NULL;
> > -int ret = -1;
> >  int exitstatus;
> >
> >  *master = NULL;
> > @@ -438,7 +432,7 @@ virNetDevOpenvswitchInterfaceGetMaster(const char 
> > *ifname, char **master)
> >  virReportError(VIR_ERR_INTERNAL_ERROR,
> > _("Unable to run command to get OVS master for "
> >   "interface %s"), ifname);
> > -goto cleanup;
> > +return -1;
> >  }
> >
> >  /* non-0 exit code just means that the interface has no master in OVS 
> > */
> > @@ -454,9 +448,7 @@ virNetDevOpenvswitchInterfaceGetMaster(const char 
> > *ifname, char **master)
> >
> >  VIR_DEBUG("OVS master for %s is %s", ifname, *master ? *master : 
> > "(none)");
> >
> > -ret = 0;
> > - cleanup:
> > -return ret;
> > +return 0;
>
> Probably should be a separate patch. The rest is fine.

Another patch just for this function?

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


Re: [libvirt] [PATCH v1 13/32] util: netdevip: use VIR_AUTOPTR for aggregate types

2018-08-05 Thread Sukrit Bhatnagar
On Fri, 3 Aug 2018 at 18:58, Erik Skultety  wrote:
>
> On Fri, Aug 03, 2018 at 06:46:28PM +0530, Sukrit Bhatnagar wrote:
> > On Fri, 3 Aug 2018 at 18:41, Erik Skultety  wrote:
> > >
> > > On Fri, Aug 03, 2018 at 06:38:30PM +0530, Sukrit Bhatnagar wrote:
> > > > On Fri, 3 Aug 2018 at 18:32, Erik Skultety  wrote:
> > > > >
> > > > > On Sat, Jul 28, 2018 at 11:31:28PM +0530, Sukrit Bhatnagar wrote:
> > > > > > By making use of GNU C's cleanup attribute handled by the
> > > > > > VIR_AUTOPTR macro for declaring aggregate pointer variables,
> > > > > > majority of the calls to *Free functions can be dropped, which
> > > > > > in turn leads to getting rid of most of our cleanup sections.
> > > > > >
> > > > > > Signed-off-by: Sukrit Bhatnagar 
> > > > > > ---
> > > > > >  src/util/virnetdevip.c | 55 
> > > > > > +-
> > > > > >  1 file changed, 23 insertions(+), 32 deletions(-)
> > > > > >
> > > > > > diff --git a/src/util/virnetdevip.c b/src/util/virnetdevip.c
> > > > > > index 8f1081b..ca206e2 100644
> > > > > > --- a/src/util/virnetdevip.c
> > > > > > +++ b/src/util/virnetdevip.c
> > > > > > @@ -634,19 +634,22 @@ virNetDevIPCheckIPv6Forwarding(void)
> > > > > >  }
> > > > > >
> > > > > >  if (!valid) {
> > > > > > -virBuffer buf = VIR_BUFFER_INITIALIZER;
> > > > > > +VIR_AUTOPTR(virBuffer) buf = NULL;
> > > > > > +
> > > > > > +if (VIR_ALLOC(buf) < 0)
> > > > > > +goto cleanup;
> > > > >
> > > > > Hmm, this will actually leak memory because @buf is never going to be 
> > > > > freed,
> > > > > worse, we'll assign NULL to it.
> > > >
> > > > But since @buf is declared as AUTOPTR, virBufferFreeAndReset will be 
> > > > called
> > > > when it exits the scope, right?
> > > > If I were using virBufferContentAndReset, then it might be the case.
> > >
> > > How does virBufferFreeAndReset free @buf? It frees buf->content, but keeps
> > > @buf.
> >
> > Got it. I actually made a lot of such changes, have to revert them all.
>
> Luckily I don't see any of that merged yet, so at least we don't have to hunt
> it down in the release.

Yes. Most of the changes are in the patches I haven't posted here yet.

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


Re: [libvirt] [PATCH v1 13/32] util: netdevip: use VIR_AUTOPTR for aggregate types

2018-08-05 Thread Sukrit Bhatnagar
On Fri, 3 Aug 2018 at 18:32, Erik Skultety  wrote:
>
> On Sat, Jul 28, 2018 at 11:31:28PM +0530, Sukrit Bhatnagar wrote:
> > By making use of GNU C's cleanup attribute handled by the
> > VIR_AUTOPTR macro for declaring aggregate pointer variables,
> > majority of the calls to *Free functions can be dropped, which
> > in turn leads to getting rid of most of our cleanup sections.
> >
> > Signed-off-by: Sukrit Bhatnagar 
> > ---
> >  src/util/virnetdevip.c | 55 
> > +-
> >  1 file changed, 23 insertions(+), 32 deletions(-)
> >
> > diff --git a/src/util/virnetdevip.c b/src/util/virnetdevip.c
> > index 8f1081b..ca206e2 100644
> > --- a/src/util/virnetdevip.c
> > +++ b/src/util/virnetdevip.c
> > @@ -634,19 +634,22 @@ virNetDevIPCheckIPv6Forwarding(void)
> >  }
> >
> >  if (!valid) {
> > -virBuffer buf = VIR_BUFFER_INITIALIZER;
> > +VIR_AUTOPTR(virBuffer) buf = NULL;
> > +
> > +if (VIR_ALLOC(buf) < 0)
> > +goto cleanup;
>
> Hmm, this will actually leak memory because @buf is never going to be freed,
> worse, we'll assign NULL to it.

But since @buf is declared as AUTOPTR, virBufferFreeAndReset will be called
when it exits the scope, right?
If I were using virBufferContentAndReset, then it might be the case.

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


Re: [libvirt] [PATCH v1 13/32] util: netdevip: use VIR_AUTOPTR for aggregate types

2018-08-05 Thread Sukrit Bhatnagar
On Fri, 3 Aug 2018 at 18:41, Erik Skultety  wrote:
>
> On Fri, Aug 03, 2018 at 06:38:30PM +0530, Sukrit Bhatnagar wrote:
> > On Fri, 3 Aug 2018 at 18:32, Erik Skultety  wrote:
> > >
> > > On Sat, Jul 28, 2018 at 11:31:28PM +0530, Sukrit Bhatnagar wrote:
> > > > By making use of GNU C's cleanup attribute handled by the
> > > > VIR_AUTOPTR macro for declaring aggregate pointer variables,
> > > > majority of the calls to *Free functions can be dropped, which
> > > > in turn leads to getting rid of most of our cleanup sections.
> > > >
> > > > Signed-off-by: Sukrit Bhatnagar 
> > > > ---
> > > >  src/util/virnetdevip.c | 55 
> > > > +-
> > > >  1 file changed, 23 insertions(+), 32 deletions(-)
> > > >
> > > > diff --git a/src/util/virnetdevip.c b/src/util/virnetdevip.c
> > > > index 8f1081b..ca206e2 100644
> > > > --- a/src/util/virnetdevip.c
> > > > +++ b/src/util/virnetdevip.c
> > > > @@ -634,19 +634,22 @@ virNetDevIPCheckIPv6Forwarding(void)
> > > >  }
> > > >
> > > >  if (!valid) {
> > > > -virBuffer buf = VIR_BUFFER_INITIALIZER;
> > > > +VIR_AUTOPTR(virBuffer) buf = NULL;
> > > > +
> > > > +if (VIR_ALLOC(buf) < 0)
> > > > +goto cleanup;
> > >
> > > Hmm, this will actually leak memory because @buf is never going to be 
> > > freed,
> > > worse, we'll assign NULL to it.
> >
> > But since @buf is declared as AUTOPTR, virBufferFreeAndReset will be called
> > when it exits the scope, right?
> > If I were using virBufferContentAndReset, then it might be the case.
>
> How does virBufferFreeAndReset free @buf? It frees buf->content, but keeps
> @buf.

Got it. I actually made a lot of such changes, have to revert them all.

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


Re: [libvirt] [PATCH v1 03/32] util: netdevbridge: use VIR_AUTOFREE instead of VIR_FREE for scalar types

2018-08-05 Thread Sukrit Bhatnagar
On Thu, 2 Aug 2018 at 19:33, Erik Skultety  wrote:
>
> On Sat, Jul 28, 2018 at 11:31:18PM +0530, Sukrit Bhatnagar wrote:
> > By making use of GNU C's cleanup attribute handled by the
> > VIR_AUTOFREE macro for declaring scalar variables, majority
> > of the VIR_FREE calls can be dropped, which in turn leads to
> > getting rid of most of our cleanup sections.
> >
> > Signed-off-by: Sukrit Bhatnagar 
> > ---
> >  src/util/virnetdevbridge.c | 45 
> > +++--
> >  1 file changed, 15 insertions(+), 30 deletions(-)
> >
> > diff --git a/src/util/virnetdevbridge.c b/src/util/virnetdevbridge.c
> > index e46ac35..bf30d7c 100644
> > --- a/src/util/virnetdevbridge.c
> > +++ b/src/util/virnetdevbridge.c
> > @@ -126,8 +126,7 @@ static int virNetDevBridgeSet(const char *brname,
> >int fd, /* control socket */
> >struct ifreq *ifr)  /* pre-filled bridge 
> > name */
> >  {
> > -char *path = NULL;
> > -int ret = -1;
> > +VIR_AUTOFREE(char *) path = NULL;
> >
> >  if (virAsprintf(, SYSFS_NET_DIR "%s/bridge/%s", brname, 
> > paramname) < 0)
> >  return -1;
> > @@ -138,7 +137,7 @@ static int virNetDevBridgeSet(const char *brname,
> >  if (virFileWriteStr(path, valuestr, 0) < 0) {
> >  virReportSystemError(errno,
> >   _("Unable to set bridge %s %s"), brname, 
> > paramname);
> > -goto cleanup;
> > +return -1;
> >  }
> >  } else {
> >  unsigned long paramid;
> > @@ -149,21 +148,18 @@ static int virNetDevBridgeSet(const char *brname,
> >  } else {
> >  virReportSystemError(EINVAL,
> >   _("Unable to set bridge %s %s"), brname, 
> > paramname);
> > -goto cleanup;
> > +return -1;
> >  }
> >  unsigned long args[] = { paramid, value, 0, 0 };
> >  ifr->ifr_data = (char*)
> >  if (ioctl(fd, SIOCDEVPRIVATE, ifr) < 0) {
> >  virReportSystemError(errno,
> >   _("Unable to set bridge %s %s"), brname, 
> > paramname);
> > -goto cleanup;
> > +return -1;
> >  }
> >  }
> >
> > -ret = 0;
> > - cleanup:
> > -VIR_FREE(path);
> > -return ret;
> > +return 0;
> >  }
> >
> >
> > @@ -171,7 +167,7 @@ static int virNetDevBridgeGet(const char *brname,
> >const char *paramname,  /* sysfs param name 
> > */
> >unsigned long *value)   /* current value */
> >  {
> > -char *path = NULL;
> > +VIR_AUTOFREE(char *) path = NULL;
>
> Referring to my response to previous patch, I'll move ^this at the end of the
> "declare" block (there are a few identical spots across the patch).
>
> ...
>
> >   malformed_resp:
> > @@ -1069,7 +1055,7 @@ virNetDevBridgeFDBAddDel(const virMacAddr *mac, const 
> > char *ifname,
> >   unsigned int flags, bool isAdd)
> >  {
> >  int ret = -1;
> > -struct nlmsghdr *resp = NULL;
> > +VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
> >  struct nlmsgerr *err;
> >  unsigned int recvbuflen;
> >  struct nl_msg *nl_msg;
>
> So, I believe ^this external type can easily be turned into an autoclean
> variant.
>
> > @@ -1142,7 +1128,6 @@ virNetDevBridgeFDBAddDel(const virMacAddr *mac, const 
> > char *ifname,
> >  ret = 0;
> >   cleanup:
> >  nlmsg_free(nl_msg);
>
> So that ^this would be done automatically.

We would need to pass the nlmsg_free function in the cleanup attribute somehow.
So, shall we not use the VIR_AUTO macros and declare it with cleanup attribute
explicitly, or create a new type and a new Free wrapper for it to use
with out macros?

> Otherwise it the patch looks fine.
>
> Erik

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


Re: [libvirt] [PATCH v1 16/32] util: netdevmacvlan: use VIR_AUTOPTR for aggregate types

2018-08-03 Thread Sukrit Bhatnagar
On Fri, 3 Aug 2018 at 18:49, Erik Skultety  wrote:
>
> On Sat, Jul 28, 2018 at 11:31:31PM +0530, Sukrit Bhatnagar wrote:
> > By making use of GNU C's cleanup attribute handled by the
> > VIR_AUTOPTR macro for declaring aggregate pointer variables,
> > majority of the calls to *Free functions can be dropped, which
> > in turn leads to getting rid of most of our cleanup sections.
> >
> > Signed-off-by: Sukrit Bhatnagar 
> > ---
> >  src/util/virnetdevmacvlan.c | 28 
> >  1 file changed, 12 insertions(+), 16 deletions(-)
> >
> > diff --git a/src/util/virnetdevmacvlan.c b/src/util/virnetdevmacvlan.c
> > index a2ed65c..d01e5ef 100644
> > --- a/src/util/virnetdevmacvlan.c
> > +++ b/src/util/virnetdevmacvlan.c
> > @@ -898,19 +898,20 @@ virNetDevMacVLanVPortProfileRegisterCallback(const 
> > char *ifname,
> >   virNetDevVPortProfilePtr 
> > virtPortProfile,
> >   virNetDevVPortProfileOp vmOp)
> >  {
> > -virNetlinkCallbackDataPtr calld = NULL;
> > +VIR_AUTOPTR(virNetlinkCallbackData) calld = NULL;
> > +virNetlinkCallbackDataPtr temp ATTRIBUTE_UNUSED = NULL;
> >
> >  if (virtPortProfile && virNetlinkEventServiceIsRunning(NETLINK_ROUTE)) 
> > {
> >  if (VIR_ALLOC(calld) < 0)
> > -goto error;
> > +return -1;
> >  if (VIR_STRDUP(calld->cr_ifname, ifname) < 0)
> > -goto error;
> > +return -1;
> >  if (VIR_ALLOC(calld->virtPortProfile) < 0)
> > -goto error;
> > +return -1;
> >  memcpy(calld->virtPortProfile, virtPortProfile, 
> > sizeof(*virtPortProfile));
> >  virMacAddrSet(>macaddress, macaddress);
> >  if (VIR_STRDUP(calld->linkdev, linkdev) < 0)
> > -goto error;
> > +return -1;
> >  memcpy(calld->vmuuid, vmuuid, sizeof(calld->vmuuid));
> >
> >  calld->vmOp = vmOp;
> > @@ -918,14 +919,12 @@ virNetDevMacVLanVPortProfileRegisterCallback(const 
> > char *ifname,
> >  if (virNetlinkEventAddClient(virNetDevMacVLanVPortProfileCallback,
> >   
> > virNetDevMacVLanVPortProfileDestroyCallback,
> >   calld, macaddress, NETLINK_ROUTE) < 0)
> > -goto error;
> > +return -1;
> >  }
> >
> > +VIR_STEAL_PTR(temp, calld);
> > +
> >  return 0;
> > -
> > - error:
> > -virNetlinkCallbackDataFree(calld);
> > -return -1;
> >  }
>
> ^This is stretching the VIR_AUTO* concept too much, there's no apparent gain
> here and should be left as is.

But by doing this, are getting rid of goto jumps and error section.
That was one of the goals, right?

>
> >
> >
> > @@ -1184,9 +1183,9 @@ int virNetDevMacVLanDeleteWithVPortProfile(const char 
> > *ifname,
> >  }
> >
> >  if (mode == VIR_NETDEV_MACVLAN_MODE_PASSTHRU) {
> > -virMacAddrPtr MAC = NULL;
> > -virMacAddrPtr adminMAC = NULL;
> > -virNetDevVlanPtr vlan = NULL;
> > +VIR_AUTOPTR(virMacAddr) MAC = NULL;
> > +VIR_AUTOPTR(virMacAddr) adminMAC = NULL;
> > +VIR_AUTOPTR(virNetDevVlan) vlan = NULL;
> >
> >  if ((virNetDevReadNetConfig(linkdev, -1, stateDir,
> >  , , ) == 0) &&
> > @@ -1194,9 +1193,6 @@ int virNetDevMacVLanDeleteWithVPortProfile(const char 
> > *ifname,
> >
> >  ignore_value(virNetDevSetNetConfig(linkdev, -1,
> > adminMAC, vlan, MAC, 
> > !!vlan));
> > -VIR_FREE(MAC);
> > -VIR_FREE(adminMAC);
> > -virNetDevVlanFree(vlan);
> >  }
>
> To ^this hunk:
> Reviewed-by: Erik Skultety 
>
>
> >  }
> >
> > --
> > 1.8.3.1
> >
> > --
> > libvir-list mailing list
> > libvir-list@redhat.com
> > https://www.redhat.com/mailman/listinfo/libvir-list

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v1 32/32] util: qemu: use VIR_AUTOPTR for aggregate types

2018-07-28 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOPTR macro for declaring aggregate pointer variables,
majority of the calls to *Free functions can be dropped, which
in turn leads to getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virqemu.c | 24 ++--
 1 file changed, 10 insertions(+), 14 deletions(-)

diff --git a/src/util/virqemu.c b/src/util/virqemu.c
index 4089b8e..cb42d38 100644
--- a/src/util/virqemu.c
+++ b/src/util/virqemu.c
@@ -56,7 +56,7 @@ virQEMUBuildCommandLineJSONArrayBitmap(const char *key,
 {
 ssize_t pos = -1;
 ssize_t end;
-virBitmapPtr bitmap = NULL;
+VIR_AUTOPTR(virBitmap) bitmap = NULL;
 
 if (virJSONValueGetArrayAsBitmap(array, ) < 0)
 return -1;
@@ -73,8 +73,6 @@ virQEMUBuildCommandLineJSONArrayBitmap(const char *key,
 }
 }
 
-virBitmapFree(bitmap);
-
 return 0;
 }
 
@@ -267,21 +265,19 @@ virQEMUBuildObjectCommandlineFromJSON(virBufferPtr buf,
 char *
 virQEMUBuildDriveCommandlineFromJSON(virJSONValuePtr srcdef)
 {
-virBuffer buf = VIR_BUFFER_INITIALIZER;
-char *ret = NULL;
+VIR_AUTOPTR(virBuffer) buf = NULL;
 
-if (virQEMUBuildCommandLineJSON(srcdef, ,
+if (VIR_ALLOC(buf) < 0)
+return NULL;
+
+if (virQEMUBuildCommandLineJSON(srcdef, buf,
 virQEMUBuildCommandLineJSONArrayNumbered) 
< 0)
-goto cleanup;
+return NULL;
 
-if (virBufferCheckError() < 0)
-goto cleanup;
+if (virBufferCheckError(buf) < 0)
+return NULL;
 
-ret = virBufferContentAndReset();
-
- cleanup:
-virBufferFreeAndReset();
-return ret;
+return virBufferContentAndReset(buf);
 }
 
 
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v1 27/32] util: perf: use VIR_AUTOFREE instead of VIR_FREE for scalar types

2018-07-28 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOFREE macro for declaring scalar variables, majority
of the VIR_FREE calls can be dropped, which in turn leads to
getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virperf.c | 17 +
 1 file changed, 5 insertions(+), 12 deletions(-)

diff --git a/src/util/virperf.c b/src/util/virperf.c
index 4537cd0..1203a6e 100644
--- a/src/util/virperf.c
+++ b/src/util/virperf.c
@@ -175,12 +175,12 @@ typedef struct virPerfEventAttr *virPerfEventAttrPtr;
 static int
 virPerfRdtAttrInit(void)
 {
-char *buf = NULL;
+VIR_AUTOFREE(char *) buf = NULL;
 char *tmp = NULL;
 unsigned int attr_type = 0;
 
 if (virFileReadAllQuiet("/sys/devices/intel_cqm/type", 10, ) < 0)
-goto error;
+return -1;
 
 if ((tmp = strchr(buf, '\n')))
 *tmp = '\0';
@@ -188,19 +188,14 @@ virPerfRdtAttrInit(void)
 if (virStrToLong_ui(buf, NULL, 10, _type) < 0) {
 virReportSystemError(errno, "%s",
  _("failed to get rdt event type"));
-goto error;
+return -1;
 }
-VIR_FREE(buf);
 
 attrs[VIR_PERF_EVENT_CMT].attrType = attr_type;
 attrs[VIR_PERF_EVENT_MBMT].attrType = attr_type;
 attrs[VIR_PERF_EVENT_MBML].attrType = attr_type;
 
 return 0;
-
- error:
-VIR_FREE(buf);
-return -1;
 }
 
 
@@ -209,7 +204,6 @@ virPerfEventEnable(virPerfPtr perf,
virPerfEventType type,
pid_t pid)
 {
-char *buf = NULL;
 struct perf_event_attr attr;
 virPerfEventPtr event = &(perf->events[type]);
 virPerfEventAttrPtr event_attr = [type];
@@ -227,6 +221,8 @@ virPerfEventEnable(virPerfPtr perf,
 }
 
 if (type == VIR_PERF_EVENT_CMT) {
+VIR_AUTOFREE(char *) buf = NULL;
+
 if (virFileReadAll("/sys/devices/intel_cqm/events/llc_occupancy.scale",
10, ) < 0)
 goto error;
@@ -236,8 +232,6 @@ virPerfEventEnable(virPerfPtr perf,
  _("failed to get cmt scaling factor"));
 goto error;
 }
-
-VIR_FREE(buf);
 }
 
 memset(, 0, sizeof(attr));
@@ -268,7 +262,6 @@ virPerfEventEnable(virPerfPtr perf,
 
  error:
 VIR_FORCE_CLOSE(event->fd);
-VIR_FREE(buf);
 return -1;
 }
 
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v1 12/32] util: netdevip: use VIR_AUTOFREE instead of VIR_FREE for scalar types

2018-07-28 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOFREE macro for declaring scalar variables, majority
of the VIR_FREE calls can be dropped, which in turn leads to
getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virnetdevip.c | 95 ++
 1 file changed, 33 insertions(+), 62 deletions(-)

diff --git a/src/util/virnetdevip.c b/src/util/virnetdevip.c
index fdb0b74..8f1081b 100644
--- a/src/util/virnetdevip.c
+++ b/src/util/virnetdevip.c
@@ -171,11 +171,11 @@ virNetDevIPAddrAdd(const char *ifname,
 virSocketAddr *broadcast = NULL;
 int ret = -1;
 struct nl_msg *nlmsg = NULL;
-struct nlmsghdr *resp = NULL;
 unsigned int recvbuflen;
-char *ipStr = NULL;
-char *peerStr = NULL;
-char *bcastStr = NULL;
+VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
+VIR_AUTOFREE(char *) ipStr = NULL;
+VIR_AUTOFREE(char *) peerStr = NULL;
+VIR_AUTOFREE(char *) bcastStr = NULL;
 
 ipStr = virSocketAddrFormat(addr);
 if (peer && VIR_SOCKET_ADDR_VALID(peer))
@@ -225,11 +225,7 @@ virNetDevIPAddrAdd(const char *ifname,
 
 ret = 0;
  cleanup:
-VIR_FREE(ipStr);
-VIR_FREE(peerStr);
-VIR_FREE(bcastStr);
 nlmsg_free(nlmsg);
-VIR_FREE(resp);
 VIR_FREE(broadcast);
 return ret;
 }
@@ -252,7 +248,7 @@ virNetDevIPAddrDel(const char *ifname,
 {
 int ret = -1;
 struct nl_msg *nlmsg = NULL;
-struct nlmsghdr *resp = NULL;
+VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
 unsigned int recvbuflen;
 
 if (!(nlmsg = virNetDevCreateNetlinkAddressMessage(RTM_DELADDR, ifname,
@@ -273,7 +269,6 @@ virNetDevIPAddrDel(const char *ifname,
 ret = 0;
  cleanup:
 nlmsg_free(nlmsg);
-VIR_FREE(resp);
 return ret;
 }
 
@@ -309,8 +304,8 @@ virNetDevIPRouteAdd(const char *ifname,
 int errCode;
 virSocketAddr defaultAddr;
 virSocketAddrPtr actualAddr;
-char *toStr = NULL;
-char *viaStr = NULL;
+VIR_AUTOFREE(char *) toStr = NULL;
+VIR_AUTOFREE(char *) viaStr = NULL;
 
 actualAddr = addr;
 
@@ -383,8 +378,6 @@ virNetDevIPRouteAdd(const char *ifname,
 
 ret = 0;
  cleanup:
-VIR_FREE(toStr);
-VIR_FREE(viaStr);
 nlmsg_free(nlmsg);
 return ret;
 
@@ -452,7 +445,6 @@ virNetDevIPWaitDadFinish(virSocketAddrPtr *addrs, size_t 
count)
 {
 struct nl_msg *nlmsg = NULL;
 struct ifaddrmsg ifa;
-struct nlmsghdr *resp = NULL;
 unsigned int recvbuflen;
 int ret = -1;
 bool dad = true;
@@ -475,6 +467,8 @@ virNetDevIPWaitDadFinish(virSocketAddrPtr *addrs, size_t 
count)
 
 /* Periodically query netlink until DAD finishes on all known addresses. */
 while (dad && time(NULL) < max_time) {
+VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
+
 if (virNetlinkCommand(nlmsg, , , 0, 0,
   NETLINK_ROUTE, 0) < 0)
 goto cleanup;
@@ -489,8 +483,6 @@ virNetDevIPWaitDadFinish(virSocketAddrPtr *addrs, size_t 
count)
 dad = virNetDevIPParseDadStatus(resp, recvbuflen, addrs, count);
 if (dad)
 usleep(1000 * 10);
-
-VIR_FREE(resp);
 }
 /* Check timeout. */
 if (dad) {
@@ -502,7 +494,6 @@ virNetDevIPWaitDadFinish(virSocketAddrPtr *addrs, size_t 
count)
 }
 
  cleanup:
-VIR_FREE(resp);
 nlmsg_free(nlmsg);
 return ret;
 }
@@ -510,22 +501,18 @@ virNetDevIPWaitDadFinish(virSocketAddrPtr *addrs, size_t 
count)
 static int
 virNetDevIPGetAcceptRA(const char *ifname)
 {
-char *path = NULL;
-char *buf = NULL;
+VIR_AUTOFREE(char *) path = NULL;
+VIR_AUTOFREE(char *) buf = NULL;
 char *suffix;
 int accept_ra = -1;
 
 if (virAsprintf(, "/proc/sys/net/ipv6/conf/%s/accept_ra",
 ifname ? ifname : "all") < 0)
-goto cleanup;
+return -1;
 
 if ((virFileReadAll(path, 512, ) < 0) ||
 (virStrToLong_i(buf, , 10, _ra) < 0))
-goto cleanup;
-
- cleanup:
-VIR_FREE(path);
-VIR_FREE(buf);
+return accept_ra;
 
 return accept_ra;
 }
@@ -545,9 +532,8 @@ virNetDevIPCheckIPv6ForwardingCallback(const struct 
nlmsghdr *resp,
 struct rtmsg *rtmsg = NLMSG_DATA(resp);
 int accept_ra = -1;
 struct rtattr *rta;
-char *ifname = NULL;
+VIR_AUTOFREE(char *) ifname = NULL;
 struct virNetDevIPCheckIPv6ForwardingData *data = opaque;
-int ret = 0;
 int len = RTM_PAYLOAD(resp);
 int oif = -1;
 size_t i;
@@ -555,7 +541,7 @@ virNetDevIPCheckIPv6ForwardingCallback(const struct 
nlmsghdr *resp,
 
 /* Ignore messages other than route ones */
 if (resp->nlmsg_type != RTM_NEWROUTE)
-return ret;
+return 0;
 
 /* Extract a device ID attribute */
 VIR_WARNINGS_NO_CAST_ALIGN
@@ -566,21 +552,20 @@ virNetDevIPCheckIPv6ForwardingCallback(const struct 
nlmsghdr *resp,
 
 /* Should never happen: netl

[libvirt] [PATCH v1 20/32] util: netdevveth: use VIR_AUTOFREE instead of VIR_FREE for scalar types

2018-07-28 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOFREE macro for declaring scalar variables, majority
of the VIR_FREE calls can be dropped, which in turn leads to
getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virnetdevveth.c | 12 
 1 file changed, 4 insertions(+), 8 deletions(-)

diff --git a/src/util/virnetdevveth.c b/src/util/virnetdevveth.c
index 6905168..8c1a7f3 100644
--- a/src/util/virnetdevveth.c
+++ b/src/util/virnetdevveth.c
@@ -46,12 +46,11 @@ virMutex virNetDevVethCreateMutex = VIR_MUTEX_INITIALIZER;
 static int virNetDevVethExists(int devNum)
 {
 int ret;
-char *path = NULL;
+VIR_AUTOFREE(char *) path = NULL;
 if (virAsprintf(, SYSFS_NET_DIR "vnet%d/", devNum) < 0)
 return -1;
 ret = virFileExists(path) ? 1 : 0;
 VIR_DEBUG("Checked dev vnet%d usage: %d", devNum, ret);
-VIR_FREE(path);
 return ret;
 }
 
@@ -111,8 +110,6 @@ static int virNetDevVethGetFreeNum(int startDev)
 int virNetDevVethCreate(char** veth1, char** veth2)
 {
 int ret = -1;
-char *veth1auto = NULL;
-char *veth2auto = NULL;
 int vethNum = 0;
 virCommandPtr cmd = NULL;
 size_t i;
@@ -125,6 +122,9 @@ int virNetDevVethCreate(char** veth1, char** veth2)
 #define MAX_VETH_RETRIES 10
 
 for (i = 0; i < MAX_VETH_RETRIES; i++) {
+VIR_AUTOFREE(char *) veth1auto = NULL;
+VIR_AUTOFREE(char *) veth2auto = NULL;
+
 int status;
 if (!*veth1) {
 int veth1num;
@@ -173,8 +173,6 @@ int virNetDevVethCreate(char** veth1, char** veth2)
   *veth1 ? *veth1 : veth1auto,
   *veth2 ? *veth2 : veth2auto,
   status);
-VIR_FREE(veth1auto);
-VIR_FREE(veth2auto);
 virCommandFree(cmd);
 cmd = NULL;
 }
@@ -186,8 +184,6 @@ int virNetDevVethCreate(char** veth1, char** veth2)
  cleanup:
 virMutexUnlock();
 virCommandFree(cmd);
-VIR_FREE(veth1auto);
-VIR_FREE(veth2auto);
 return ret;
 }
 
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v1 24/32] util: numa: use VIR_AUTOFREE instead of VIR_FREE for scalar types

2018-07-28 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOFREE macro for declaring scalar variables, majority
of the VIR_FREE calls can be dropped, which in turn leads to
getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virnuma.c | 79 +-
 1 file changed, 31 insertions(+), 48 deletions(-)

diff --git a/src/util/virnuma.c b/src/util/virnuma.c
index 784db0a..841c7cb 100644
--- a/src/util/virnuma.c
+++ b/src/util/virnuma.c
@@ -252,8 +252,8 @@ int
 virNumaGetNodeCPUs(int node,
virBitmapPtr *cpus)
 {
-unsigned long *mask = NULL;
-unsigned long *allonesmask = NULL;
+VIR_AUTOFREE(unsigned long *) mask = NULL;
+VIR_AUTOFREE(unsigned long *) allonesmask = NULL;
 virBitmapPtr cpumap = NULL;
 int ncpus = 0;
 int max_n_cpus = virNumaGetMaxCPUs();
@@ -300,8 +300,6 @@ virNumaGetNodeCPUs(int node,
 ret = ncpus;
 
  cleanup:
-VIR_FREE(mask);
-VIR_FREE(allonesmask);
 virBitmapFree(cpumap);
 
 return ret;
@@ -566,52 +564,47 @@ virNumaGetHugePageInfo(int node,
unsigned long long *page_avail,
unsigned long long *page_free)
 {
-int ret = -1;
-char *path = NULL;
-char *buf = NULL;
 char *end;
 
 if (page_avail) {
+VIR_AUTOFREE(char *) path = NULL;
+VIR_AUTOFREE(char *) buf = NULL;
 if (virNumaGetHugePageInfoPath(, node,
page_size, "nr_hugepages") < 0)
-goto cleanup;
+return -1;
 
 if (virFileReadAll(path, 1024, ) < 0)
-goto cleanup;
+return -1;
 
 if (virStrToLong_ull(buf, , 10, page_avail) < 0 ||
 *end != '\n') {
 virReportError(VIR_ERR_INTERNAL_ERROR,
_("unable to parse: %s"),
buf);
-goto cleanup;
+return -1;
 }
-VIR_FREE(buf);
-VIR_FREE(path);
 }
 
 if (page_free) {
+VIR_AUTOFREE(char *) path = NULL;
+VIR_AUTOFREE(char *) buf = NULL;
 if (virNumaGetHugePageInfoPath(, node,
page_size, "free_hugepages") < 0)
-goto cleanup;
+return -1;
 
 if (virFileReadAll(path, 1024, ) < 0)
-goto cleanup;
+return -1;
 
 if (virStrToLong_ull(buf, , 10, page_free) < 0 ||
 *end != '\n') {
 virReportError(VIR_ERR_INTERNAL_ERROR,
_("unable to parse: %s"),
buf);
-goto cleanup;
+return -1;
 }
 }
 
-ret = 0;
- cleanup:
-VIR_FREE(buf);
-VIR_FREE(path);
-return ret;
+return 0;
 }
 
 /**
@@ -714,13 +707,13 @@ virNumaGetPages(int node,
 size_t *npages)
 {
 int ret = -1;
-char *path = NULL;
+VIR_AUTOFREE(char *) path = NULL;
+VIR_AUTOFREE(unsigned int *) tmp_size = NULL;
+VIR_AUTOFREE(unsigned long long *) tmp_avail = NULL;
+VIR_AUTOFREE(unsigned long long *) tmp_free = NULL;
 DIR *dir = NULL;
 int direrr = 0;
 struct dirent *entry;
-unsigned int *tmp_size = NULL;
-unsigned long long *tmp_avail = NULL;
-unsigned long long *tmp_free = NULL;
 unsigned int ntmp = 0;
 size_t i;
 bool exchange;
@@ -828,11 +821,7 @@ virNumaGetPages(int node,
 *npages = ntmp;
 ret = 0;
  cleanup:
-VIR_FREE(tmp_free);
-VIR_FREE(tmp_avail);
-VIR_FREE(tmp_size);
 VIR_DIR_CLOSE(dir);
-VIR_FREE(path);
 return ret;
 }
 
@@ -843,8 +832,8 @@ virNumaSetPagePoolSize(int node,
unsigned long long page_count,
bool add)
 {
-int ret = -1;
-char *nr_path = NULL, *nr_buf =  NULL;
+VIR_AUTOFREE(char *) nr_path = NULL;
+VIR_AUTOFREE(char *) nr_buf =  NULL;
 char *end;
 unsigned long long nr_count;
 
@@ -853,37 +842,35 @@ virNumaSetPagePoolSize(int node,
  * differently to huge pages. */
 virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
_("system pages pool can't be modified"));
-goto cleanup;
+return -1;
 }
 
 if (virNumaGetHugePageInfoPath(_path, node, page_size, "nr_hugepages") 
< 0)
-goto cleanup;
+return -1;
 
 /* Firstly check, if there's anything for us to do */
 if (virFileReadAll(nr_path, 1024, _buf) < 0)
-goto cleanup;
+return -1;
 
 if (virStrToLong_ull(nr_buf, , 10, _count) < 0 ||
 *end != '\n') {
 virReportError(VIR_ERR_OPERATION_FAILED,
_("invalid number '%s' in '%s'"),
nr_buf, nr_path);
-goto cleanup;
+return -1;
 }
 
 if (add) {
 if (!

[libvirt] [PATCH v1 28/32] util: pidfile: use VIR_AUTOFREE instead of VIR_FREE for scalar types

2018-07-28 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOFREE macro for declaring scalar variables, majority
of the VIR_FREE calls can be dropped, which in turn leads to
getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virpidfile.c | 185 --
 1 file changed, 59 insertions(+), 126 deletions(-)

diff --git a/src/util/virpidfile.c b/src/util/virpidfile.c
index 1a85d43..d82bf92 100644
--- a/src/util/virpidfile.c
+++ b/src/util/virpidfile.c
@@ -97,29 +97,18 @@ int virPidFileWrite(const char *dir,
 const char *name,
 pid_t pid)
 {
-int rc;
-char *pidfile = NULL;
+VIR_AUTOFREE(char *) pidfile = NULL;
 
-if (name == NULL || dir == NULL) {
-rc = -EINVAL;
-goto cleanup;
-}
+if (name == NULL || dir == NULL)
+return -EINVAL;
 
-if (virFileMakePath(dir) < 0) {
-rc = -errno;
-goto cleanup;
-}
+if (virFileMakePath(dir) < 0)
+return -errno;
 
-if (!(pidfile = virPidFileBuildPath(dir, name))) {
-rc = -ENOMEM;
-goto cleanup;
-}
+if (!(pidfile = virPidFileBuildPath(dir, name)))
+return -ENOMEM;
 
-rc = virPidFileWritePath(pidfile, pid);
-
- cleanup:
-VIR_FREE(pidfile);
-return rc;
+return virPidFileWritePath(pidfile, pid);
 }
 
 
@@ -170,25 +159,16 @@ int virPidFileRead(const char *dir,
const char *name,
pid_t *pid)
 {
-int rc;
-char *pidfile = NULL;
+VIR_AUTOFREE(char *) pidfile = NULL;
 *pid = 0;
 
-if (name == NULL || dir == NULL) {
-rc = -EINVAL;
-goto cleanup;
-}
+if (name == NULL || dir == NULL)
+return -EINVAL;
 
-if (!(pidfile = virPidFileBuildPath(dir, name))) {
-rc = -ENOMEM;
-goto cleanup;
-}
+if (!(pidfile = virPidFileBuildPath(dir, name)))
+return -ENOMEM;
 
-rc = virPidFileReadPath(pidfile, pid);
-
- cleanup:
-VIR_FREE(pidfile);
-return rc;
+return virPidFileReadPath(pidfile, pid);
 }
 
 
@@ -219,11 +199,11 @@ int virPidFileReadPathIfAlive(const char *path,
 {
 int ret;
 bool isLink;
-char *procPath = NULL;
-char *procLink = NULL;
+VIR_AUTOFREE(char *) procPath = NULL;
+VIR_AUTOFREE(char *) procLink = NULL;
+VIR_AUTOFREE(char *) resolvedBinPath = NULL;
+VIR_AUTOFREE(char *) resolvedProcLink = NULL;
 size_t procLinkLen;
-char *resolvedBinPath = NULL;
-char *resolvedProcLink = NULL;
 const char deletedText[] = " (deleted)";
 size_t deletedTextLen = strlen(deletedText);
 pid_t retPid;
@@ -232,7 +212,7 @@ int virPidFileReadPathIfAlive(const char *path,
 *pid = -1;
 
 if ((ret = virPidFileReadPath(path, )) < 0)
-goto cleanup;
+return ret;
 
 #ifndef WIN32
 /* Check that it's still alive.  Safe to skip this sanity check on
@@ -252,13 +232,12 @@ int virPidFileReadPathIfAlive(const char *path,
 goto cleanup;
 }
 
-if (virAsprintf(, "/proc/%lld/exe", (long long)retPid) < 0) {
-ret = -ENOMEM;
-goto cleanup;
-}
+if (virAsprintf(, "/proc/%lld/exe", (long long)retPid) < 0)
+return -ENOMEM;
 
 if ((ret = virFileIsLink(procPath)) < 0)
-goto cleanup;
+return ret;
+
 isLink = ret;
 
 if (isLink && virFileLinkPointsTo(procPath, binPath)) {
@@ -275,27 +254,21 @@ int virPidFileReadPathIfAlive(const char *path,
  * "$procpath (deleted)".  Read that link, remove the " (deleted)"
  * part, and see if it has the same canonicalized name as binpath.
  */
-if (!(procLink = areadlink(procPath))) {
-ret = -errno;
-goto cleanup;
-}
+if (!(procLink = areadlink(procPath)))
+return -errno;
+
 procLinkLen = strlen(procLink);
 if (procLinkLen > deletedTextLen)
 procLink[procLinkLen - deletedTextLen] = 0;
 
 if ((ret = virFileResolveAllLinks(binPath, )) < 0)
-goto cleanup;
+return ret;
 if ((ret = virFileResolveAllLinks(procLink, )) < 0)
-goto cleanup;
+return ret;
 
 ret = STREQ(resolvedBinPath, resolvedProcLink) ? 0 : -1;
 
  cleanup:
-VIR_FREE(procPath);
-VIR_FREE(procLink);
-VIR_FREE(resolvedProcLink);
-VIR_FREE(resolvedBinPath);
-
 /* return the originally set pid of -1 unless we proclaim success */
 if (ret == 0)
 *pid = retPid;
@@ -326,24 +299,15 @@ int virPidFileReadIfAlive(const char *dir,
   pid_t *pid,
   const char *binpath)
 {
-int rc = 0;
-char *pidfile = NULL;
+VIR_AUTOFREE(char *) pidfile = NULL;
 
-if (name == NULL || dir == NULL) {
-rc = -EINVAL;
-goto cleanup;
-}
+if (name == NULL || dir == NULL)
+return -EINVAL;
 
-if (!(pidf

[libvirt] [PATCH v1 23/32] util: netlink: use VIR_AUTOPTR for aggregate types

2018-07-28 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOPTR macro for declaring aggregate pointer variables,
majority of the calls to *Free functions can be dropped, which
in turn leads to getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virnetlink.c | 48 +++-
 1 file changed, 19 insertions(+), 29 deletions(-)

diff --git a/src/util/virnetlink.c b/src/util/virnetlink.c
index 8d28387..6b00559 100644
--- a/src/util/virnetlink.c
+++ b/src/util/virnetlink.c
@@ -221,30 +221,31 @@ virNetlinkSendRequest(struct nl_msg *nl_msg, uint32_t 
src_pid,
 ssize_t nbytes;
 int fd;
 int n;
-virNetlinkHandlePtr nlhandle = NULL;
+VIR_AUTOPTR(virNetlinkHandle) nlhandle = NULL;
+virNetlinkHandlePtr temp = NULL;
 struct pollfd fds[1];
 struct nlmsghdr *nlmsg = nlmsg_hdr(nl_msg);
 
 if (protocol >= MAX_LINKS) {
 virReportSystemError(EINVAL,
  _("invalid protocol argument: %d"), protocol);
-goto error;
+return NULL;
 }
 
 if (!(nlhandle = virNetlinkCreateSocket(protocol)))
-goto error;
+return NULL;
 
 fd = nl_socket_get_fd(nlhandle);
 if (fd < 0) {
 virReportSystemError(errno,
  "%s", _("cannot get netlink socket fd"));
-goto error;
+return NULL;
 }
 
 if (groups && nl_socket_add_membership(nlhandle, groups) < 0) {
 virReportSystemError(errno,
  "%s", _("cannot add netlink membership"));
-goto error;
+return NULL;
 }
 
 nlmsg_set_dst(nl_msg, );
@@ -255,7 +256,7 @@ virNetlinkSendRequest(struct nl_msg *nl_msg, uint32_t 
src_pid,
 if (nbytes < 0) {
 virReportSystemError(errno,
  "%s", _("cannot send to netlink socket"));
-goto error;
+return NULL;
 }
 
 memset(fds, 0, sizeof(fds));
@@ -273,11 +274,9 @@ virNetlinkSendRequest(struct nl_msg *nl_msg, uint32_t 
src_pid,
  _("no valid netlink response was received"));
 }
 
-return nlhandle;
+VIR_STEAL_PTR(temp, nlhandle);
 
- error:
-virNetlinkFree(nlhandle);
-return NULL;
+return temp;
 }
 
 /**
@@ -307,7 +306,7 @@ int virNetlinkCommand(struct nl_msg *nl_msg,
 .nl_groups = 0,
 };
 struct pollfd fds[1];
-virNetlinkHandlePtr nlhandle = NULL;
+VIR_AUTOPTR(virNetlinkHandle) nlhandle = NULL;
 int len = 0;
 
 memset(fds, 0, sizeof(fds));
@@ -335,7 +334,6 @@ int virNetlinkCommand(struct nl_msg *nl_msg,
 *respbuflen = 0;
 }
 
-virNetlinkFree(nlhandle);
 return ret;
 }
 
@@ -346,10 +344,8 @@ virNetlinkDumpCommand(struct nl_msg *nl_msg,
   unsigned int protocol, unsigned int groups,
   void *opaque)
 {
-int ret = -1;
 bool end = false;
 int len = 0;
-struct nlmsghdr *resp = NULL;
 struct nlmsghdr *msg = NULL;
 
 struct sockaddr_nl nladdr = {
@@ -357,13 +353,14 @@ virNetlinkDumpCommand(struct nl_msg *nl_msg,
 .nl_pid= dst_pid,
 .nl_groups = 0,
 };
-virNetlinkHandlePtr nlhandle = NULL;
+VIR_AUTOPTR(virNetlinkHandle) nlhandle = NULL;
 
 if (!(nlhandle = virNetlinkSendRequest(nl_msg, src_pid, nladdr,
protocol, groups)))
-goto cleanup;
+return -1;
 
 while (!end) {
+VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
 len = nl_recv(nlhandle, , (unsigned char **), NULL);
 VIR_WARNINGS_NO_CAST_ALIGN
 for (msg = resp; NLMSG_OK(msg, len); msg = NLMSG_NEXT(msg, len)) {
@@ -372,20 +369,14 @@ virNetlinkDumpCommand(struct nl_msg *nl_msg,
 end = true;
 
 if (virNetlinkGetErrorCode(msg, len) < 0)
-goto cleanup;
+return -1;
 
 if (callback(msg, opaque) < 0)
-goto cleanup;
+return -1;
 }
-VIR_FREE(resp);
 }
 
-ret = 0;
-
- cleanup:
-VIR_FREE(resp);
-virNetlinkFree(nlhandle);
-return ret;
+return 0;
 }
 
 /**
@@ -527,7 +518,7 @@ int
 virNetlinkDelLink(const char *ifname, virNetlinkDelLinkFallback fallback)
 {
 int rc = -1;
-struct nlmsghdr *resp = NULL;
+VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
 struct nlmsgerr *err;
 struct ifinfomsg ifinfo = { .ifi_family = AF_UNSPEC };
 unsigned int recvbuflen;
@@ -582,7 +573,6 @@ virNetlinkDelLink(const char *ifname, 
virNetlinkDelLinkFallback fallback)
 rc = 0;
  cleanup:
 nlmsg_free(nl_msg);
-VIR_FREE(resp);
 return rc;
 
  malformed_resp:
@@ -771,7 +761,7 @@ virNetlinkEventCallback(int watch,
 void *opaque)
 {
 virNetlinkEventSrvPrivatePtr srv = opaque

[libvirt] [PATCH v1 21/32] util: netdevveth: use VIR_AUTOPTR for aggregate types

2018-07-28 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOPTR macro for declaring aggregate pointer variables,
majority of the calls to *Free functions can be dropped, which
in turn leads to getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virnetdevveth.c | 21 +++--
 1 file changed, 7 insertions(+), 14 deletions(-)

diff --git a/src/util/virnetdevveth.c b/src/util/virnetdevveth.c
index 8c1a7f3..0b94f73 100644
--- a/src/util/virnetdevveth.c
+++ b/src/util/virnetdevveth.c
@@ -111,7 +111,6 @@ int virNetDevVethCreate(char** veth1, char** veth2)
 {
 int ret = -1;
 int vethNum = 0;
-virCommandPtr cmd = NULL;
 size_t i;
 
 /*
@@ -124,6 +123,7 @@ int virNetDevVethCreate(char** veth1, char** veth2)
 for (i = 0; i < MAX_VETH_RETRIES; i++) {
 VIR_AUTOFREE(char *) veth1auto = NULL;
 VIR_AUTOFREE(char *) veth2auto = NULL;
+VIR_AUTOPTR(virCommand) cmd = NULL;
 
 int status;
 if (!*veth1) {
@@ -173,8 +173,6 @@ int virNetDevVethCreate(char** veth1, char** veth2)
   *veth1 ? *veth1 : veth1auto,
   *veth2 ? *veth2 : veth2auto,
   status);
-virCommandFree(cmd);
-cmd = NULL;
 }
 
 virReportError(VIR_ERR_INTERNAL_ERROR,
@@ -183,7 +181,6 @@ int virNetDevVethCreate(char** veth1, char** veth2)
 
  cleanup:
 virMutexUnlock();
-virCommandFree(cmd);
 return ret;
 }
 
@@ -200,26 +197,22 @@ int virNetDevVethCreate(char** veth1, char** veth2)
  */
 int virNetDevVethDelete(const char *veth)
 {
-virCommandPtr cmd = virCommandNewArgList("ip", "link", "del", veth, NULL);
+VIR_AUTOPTR(virCommand) cmd = NULL;
+cmd = virCommandNewArgList("ip", "link", "del", veth, NULL);
 int status;
-int ret = -1;
 
 if (virCommandRun(cmd, ) < 0)
-goto cleanup;
+return -1;
 
 if (status != 0) {
 if (!virNetDevExists(veth)) {
 VIR_DEBUG("Device %s already deleted (by kernel namespace 
cleanup)", veth);
-ret = 0;
-goto cleanup;
+return 0;
 }
 virReportError(VIR_ERR_INTERNAL_ERROR,
_("Failed to delete veth device %s"), veth);
-goto cleanup;
+return -1;
 }
 
-ret = 0;
- cleanup:
-virCommandFree(cmd);
-return ret;
+return 0;
 }
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v1 26/32] util: perf: define cleanup function using VIR_DEFINE_AUTOPTR_FUNC

2018-07-28 Thread Sukrit Bhatnagar
Using the new VIR_DEFINE_AUTOPTR_FUNC macro defined in
src/util/viralloc.h, define a new wrapper around an existing
cleanup function which will be called when a variable declared
with VIR_AUTOPTR macro goes out of scope. Also, drop the redundant
viralloc.h include, since that has moved from the source module into
the header.

When a variable of type virPerfPtr is declared using VIR_AUTOPTR,
the function virPerfFree will be run automatically on it when it
goes out of scope.

This commit also adds an intermediate typedef for virPerf
type for use with the cleanup macros.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virperf.c | 3 +--
 src/util/virperf.h | 8 ++--
 2 files changed, 7 insertions(+), 4 deletions(-)

diff --git a/src/util/virperf.c b/src/util/virperf.c
index 2c832b3..4537cd0 100644
--- a/src/util/virperf.c
+++ b/src/util/virperf.c
@@ -26,7 +26,6 @@
 #endif
 
 #include "virperf.h"
-#include "viralloc.h"
 #include "virerror.h"
 #include "virlog.h"
 #include "virfile.h"
@@ -61,7 +60,7 @@ struct virPerfEvent {
 };
 typedef struct virPerfEvent *virPerfEventPtr;
 
-struct virPerf {
+struct _virPerf {
 struct virPerfEvent events[VIR_PERF_EVENT_LAST];
 };
 
diff --git a/src/util/virperf.h b/src/util/virperf.h
index eee7a03..9d0d5ac 100644
--- a/src/util/virperf.h
+++ b/src/util/virperf.h
@@ -23,6 +23,7 @@
 # define __VIR_PERF_H__
 
 # include "virutil.h"
+# include "viralloc.h"
 
 /* Some Intel processor families introduced some RDT (Resource Director
  * Technology) features to monitor or control shared resource based on
@@ -62,8 +63,9 @@ typedef enum {
 
 VIR_ENUM_DECL(virPerfEvent);
 
-struct virPerf;
-typedef struct virPerf *virPerfPtr;
+struct _virPerf;
+typedef struct _virPerf virPerf;
+typedef virPerf *virPerfPtr;
 
 virPerfPtr virPerfNew(void);
 
@@ -83,4 +85,6 @@ int virPerfReadEvent(virPerfPtr perf,
  virPerfEventType type,
  uint64_t *value);
 
+VIR_DEFINE_AUTOPTR_FUNC(virPerf, virPerfFree)
+
 #endif /* __VIR_PERF_H__ */
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v1 22/32] util: netlink: define cleanup function using VIR_DEFINE_AUTOPTR_FUNC

2018-07-28 Thread Sukrit Bhatnagar
Using the new VIR_DEFINE_AUTOPTR_FUNC macro defined in
src/util/viralloc.h, define a new wrapper around an existing
cleanup function which will be called when a variable declared
with VIR_AUTOPTR macro goes out of scope. Also, drop the redundant
viralloc.h include, since that has moved from the source module into
the header.

This commit also adds a typedef for virNetlinkHandlePtr for use with
the cleanup macros.

When a variable of type virNetlinkHandlePtr is declared using
VIR_AUTOPTR, the function virNetlinkFree will be run automatically
on it when it goes out of scope.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virnetlink.c | 20 
 1 file changed, 12 insertions(+), 8 deletions(-)

diff --git a/src/util/virnetlink.c b/src/util/virnetlink.c
index fa1ba3e..8d28387 100644
--- a/src/util/virnetlink.c
+++ b/src/util/virnetlink.c
@@ -72,6 +72,10 @@ typedef struct nl_handle virNetlinkHandle;
 typedef struct nl_sock virNetlinkHandle;
 # endif
 
+typedef virNetlinkHandle *virNetlinkHandlePtr;
+
+VIR_DEFINE_AUTOPTR_FUNC(virNetlinkHandle, virNetlinkFree)
+
 typedef struct _virNetlinkEventSrvPrivate virNetlinkEventSrvPrivate;
 typedef virNetlinkEventSrvPrivate *virNetlinkEventSrvPrivatePtr;
 struct _virNetlinkEventSrvPrivate {
@@ -79,7 +83,7 @@ struct _virNetlinkEventSrvPrivate {
 virMutex lock;
 int eventwatch;
 int netlinkfd;
-virNetlinkHandle *netlinknh;
+virNetlinkHandlePtr netlinknh;
 /*Events*/
 int handled;
 size_t handlesCount;
@@ -102,7 +106,7 @@ static int nextWatch = 1;
 /* Linux kernel supports up to MAX_LINKS (32 at the time) individual
  * netlink protocols. */
 static virNetlinkEventSrvPrivatePtr server[MAX_LINKS] = {NULL};
-static virNetlinkHandle *placeholder_nlhandle;
+static virNetlinkHandlePtr placeholder_nlhandle;
 
 /* Function definitions */
 
@@ -172,10 +176,10 @@ virNetlinkShutdown(void)
  * Returns a handle to the new netlink socket, or 0 if there was a failure.
  *
  */
-static virNetlinkHandle *
+static virNetlinkHandlePtr
 virNetlinkCreateSocket(int protocol)
 {
-virNetlinkHandle *nlhandle = NULL;
+virNetlinkHandlePtr nlhandle = NULL;
 
 if (!(nlhandle = virNetlinkAlloc())) {
 virReportSystemError(errno, "%s",
@@ -209,7 +213,7 @@ virNetlinkCreateSocket(int protocol)
 goto cleanup;
 }
 
-static virNetlinkHandle *
+static virNetlinkHandlePtr
 virNetlinkSendRequest(struct nl_msg *nl_msg, uint32_t src_pid,
   struct sockaddr_nl nladdr,
   unsigned int protocol, unsigned int groups)
@@ -217,7 +221,7 @@ virNetlinkSendRequest(struct nl_msg *nl_msg, uint32_t 
src_pid,
 ssize_t nbytes;
 int fd;
 int n;
-virNetlinkHandle *nlhandle = NULL;
+virNetlinkHandlePtr nlhandle = NULL;
 struct pollfd fds[1];
 struct nlmsghdr *nlmsg = nlmsg_hdr(nl_msg);
 
@@ -303,7 +307,7 @@ int virNetlinkCommand(struct nl_msg *nl_msg,
 .nl_groups = 0,
 };
 struct pollfd fds[1];
-virNetlinkHandle *nlhandle = NULL;
+virNetlinkHandlePtr nlhandle = NULL;
 int len = 0;
 
 memset(fds, 0, sizeof(fds));
@@ -353,7 +357,7 @@ virNetlinkDumpCommand(struct nl_msg *nl_msg,
 .nl_pid= dst_pid,
 .nl_groups = 0,
 };
-virNetlinkHandle *nlhandle = NULL;
+virNetlinkHandlePtr nlhandle = NULL;
 
 if (!(nlhandle = virNetlinkSendRequest(nl_msg, src_pid, nladdr,
protocol, groups)))
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v1 11/32] util: netdevip: define cleanup function using VIR_DEFINE_AUTOPTR_FUNC

2018-07-28 Thread Sukrit Bhatnagar
Using the new VIR_DEFINE_AUTOPTR_FUNC macro defined in
src/util/viralloc.h, define a new wrapper around an existing
cleanup function which will be called when a variable declared
with VIR_AUTOPTR macro goes out of scope. Also, drop the redundant
viralloc.h include, since that has moved from the source module into
the header.

When variables of type virNetDevIPAddrPtr and virNetDevIPRoutePtr
are declared using VIR_AUTOPTR, the functions virNetDevIPAddrFree
and virNetDevIPRouteFree, respectively, will be run
automatically on them when they go out of scope.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virnetdevip.c | 7 ++-
 src/util/virnetdevip.h | 4 
 2 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/src/util/virnetdevip.c b/src/util/virnetdevip.c
index bf98ed8..fdb0b74 100644
--- a/src/util/virnetdevip.c
+++ b/src/util/virnetdevip.c
@@ -27,7 +27,6 @@
 #include "virnetlink.h"
 #include "virfile.h"
 #include "virerror.h"
-#include "viralloc.h"
 #include "virlog.h"
 #include "virstring.h"
 #include "virutil.h"
@@ -1129,3 +1128,9 @@ virNetDevIPInfoAddToDev(const char *ifname,
  cleanup:
 return ret;
 }
+
+void
+virNetDevIPAddrFree(virNetDevIPAddrPtr ip)
+{
+VIR_FREE(ip);
+}
diff --git a/src/util/virnetdevip.h b/src/util/virnetdevip.h
index 6b509ea..5608c37 100644
--- a/src/util/virnetdevip.h
+++ b/src/util/virnetdevip.h
@@ -84,6 +84,7 @@ int virNetDevIPAddrGet(const char *ifname, virSocketAddrPtr 
addr)
 int virNetDevIPWaitDadFinish(virSocketAddrPtr *addrs, size_t count)
 ATTRIBUTE_NONNULL(1);
 bool virNetDevIPCheckIPv6Forwarding(void);
+void virNetDevIPAddrFree(virNetDevIPAddrPtr ip);
 
 /* virNetDevIPRoute object */
 void virNetDevIPRouteFree(virNetDevIPRoutePtr def);
@@ -97,4 +98,7 @@ void virNetDevIPInfoClear(virNetDevIPInfoPtr ip);
 int virNetDevIPInfoAddToDev(const char *ifname,
 virNetDevIPInfo const *ipInfo);
 
+VIR_DEFINE_AUTOPTR_FUNC(virNetDevIPAddr, virNetDevIPAddrFree)
+VIR_DEFINE_AUTOPTR_FUNC(virNetDevIPRoute, virNetDevIPRouteFree)
+
 #endif /* __VIR_NETDEVIP_H__ */
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v1 30/32] util: process: use VIR_AUTOPTR for aggregate types

2018-07-28 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOPTR macro for declaring aggregate pointer variables,
majority of the calls to *Free functions can be dropped, which
in turn leads to getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virprocess.c | 19 +++
 1 file changed, 7 insertions(+), 12 deletions(-)

diff --git a/src/util/virprocess.c b/src/util/virprocess.c
index 1dd14de..b51d899 100644
--- a/src/util/virprocess.c
+++ b/src/util/virprocess.c
@@ -982,16 +982,15 @@ int virProcessGetStartTime(pid_t pid,
 {
 VIR_AUTOFREE(char *) filename = NULL;
 VIR_AUTOFREE(char *) buf = NULL;
+VIR_AUTOPTR(virString) tokens = NULL;
 char *tmp;
-int ret = -1;
 int len;
-char **tokens = NULL;
 
 if (virAsprintf(, "/proc/%llu/stat", (long long) pid) < 0)
 return -1;
 
 if ((len = virFileReadAll(filename, 1024, )) < 0)
-goto cleanup;
+return -1;
 
 /* start time is the token at index 19 after the '(process name)' entry - 
since only this
  * field can contain the ')' character, search backwards for this to avoid 
malicious
@@ -1002,14 +1001,14 @@ int virProcessGetStartTime(pid_t pid,
 virReportError(VIR_ERR_INTERNAL_ERROR,
_("Cannot find start time in %s"),
filename);
-goto cleanup;
+return -1;
 }
 tmp += 2; /* skip ') ' */
 if ((tmp - buf) >= len) {
 virReportError(VIR_ERR_INTERNAL_ERROR,
_("Cannot find start time in %s"),
filename);
-goto cleanup;
+return -1;
 }
 
 tokens = virStringSplit(tmp, " ", 0);
@@ -1018,7 +1017,7 @@ int virProcessGetStartTime(pid_t pid,
 virReportError(VIR_ERR_INTERNAL_ERROR,
_("Cannot find start time in %s"),
filename);
-goto cleanup;
+return -1;
 }
 
 if (virStrToLong_ull(tokens[19],
@@ -1028,14 +1027,10 @@ int virProcessGetStartTime(pid_t pid,
 virReportError(VIR_ERR_INTERNAL_ERROR,
_("Cannot parse start time %s in %s"),
tokens[19], filename);
-goto cleanup;
+return -1;
 }
 
-ret = 0;
-
- cleanup:
-virStringListFree(tokens);
-return ret;
+return 0;
 }
 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
 int virProcessGetStartTime(pid_t pid,
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v1 29/32] util: process: use VIR_AUTOFREE instead of VIR_FREE for scalar types

2018-07-28 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOFREE macro for declaring scalar variables, majority
of the VIR_FREE calls can be dropped, which in turn leads to
getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virprocess.c | 49 -
 1 file changed, 24 insertions(+), 25 deletions(-)

diff --git a/src/util/virprocess.c b/src/util/virprocess.c
index f92b0dc..1dd14de 100644
--- a/src/util/virprocess.c
+++ b/src/util/virprocess.c
@@ -158,7 +158,7 @@ virProcessAbort(pid_t pid)
 int saved_errno;
 int ret;
 int status;
-char *tmp = NULL;
+VIR_AUTOFREE(char *) tmp = NULL;
 
 if (pid <= 0)
 return;
@@ -199,7 +199,6 @@ virProcessAbort(pid_t pid)
 VIR_DEBUG("failed to reap child %lld, abandoning it", (long long) pid);
 
  cleanup:
-VIR_FREE(tmp);
 errno = saved_errno;
 }
 #else
@@ -271,11 +270,10 @@ virProcessWait(pid_t pid, int *exitstatus, bool raw)
 
  error:
 {
-char *st = virProcessTranslateStatus(status);
+VIR_AUTOFREE(char *) st = virProcessTranslateStatus(status);
 virReportError(VIR_ERR_INTERNAL_ERROR,
_("Child process (%lld) unexpected %s"),
(long long) pid, NULLSTR(st));
-VIR_FREE(st);
 }
 return -1;
 }
@@ -475,7 +473,11 @@ virBitmapPtr
 virProcessGetAffinity(pid_t pid)
 {
 size_t i;
+# ifdef CPU_ALLOC
 cpu_set_t *mask;
+# else
+VIR_AUTOFREE(cpu_set_t *) mask = NULL;
+# endif
 size_t masklen;
 size_t ncpus;
 virBitmapPtr ret = NULL;
@@ -504,11 +506,20 @@ virProcessGetAffinity(pid_t pid)
 if (sched_getaffinity(pid, masklen, mask) < 0) {
 virReportSystemError(errno,
  _("cannot get CPU affinity of process %d"), pid);
+# ifdef CPU_ALLOC
 goto cleanup;
+# else
+return ret;
+# endif
 }
 
 if (!(ret = virBitmapNew(ncpus)))
-  goto cleanup;
+# ifdef CPU_ALLOC
+
+goto cleanup;
+# else
+return ret;
+# endif
 
 for (i = 0; i < ncpus; i++) {
 # ifdef CPU_ALLOC
@@ -522,11 +533,7 @@ virProcessGetAffinity(pid_t pid)
 }
 
  cleanup:
-# ifdef CPU_ALLOC
 CPU_FREE(mask);
-# else
-VIR_FREE(mask);
-# endif
 
 return ret;
 }
@@ -603,7 +610,7 @@ virProcessGetAffinity(pid_t pid ATTRIBUTE_UNUSED)
 int virProcessGetPids(pid_t pid, size_t *npids, pid_t **pids)
 {
 int ret = -1;
-char *taskPath = NULL;
+VIR_AUTOFREE(char *) taskPath = NULL;
 DIR *dir = NULL;
 int value;
 struct dirent *ent;
@@ -636,7 +643,6 @@ int virProcessGetPids(pid_t pid, size_t *npids, pid_t 
**pids)
 
  cleanup:
 VIR_DIR_CLOSE(dir);
-VIR_FREE(taskPath);
 if (ret < 0)
 VIR_FREE(*pids);
 return ret;
@@ -648,7 +654,6 @@ int virProcessGetNamespaces(pid_t pid,
 int **fdlist)
 {
 int ret = -1;
-char *nsfile = NULL;
 size_t i = 0;
 const char *ns[] = { "user", "ipc", "uts", "net", "pid", "mnt" };
 
@@ -656,6 +661,7 @@ int virProcessGetNamespaces(pid_t pid,
 *fdlist = NULL;
 
 for (i = 0; i < ARRAY_CARDINALITY(ns); i++) {
+VIR_AUTOFREE(char *) nsfile = NULL;
 int fd;
 
 if (virAsprintf(, "/proc/%llu/ns/%s",
@@ -671,14 +677,11 @@ int virProcessGetNamespaces(pid_t pid,
 
 (*fdlist)[(*nfdlist)-1] = fd;
 }
-
-VIR_FREE(nsfile);
 }
 
 ret = 0;
 
  cleanup:
-VIR_FREE(nsfile);
 if (ret < 0) {
 for (i = 0; i < *nfdlist; i++)
 VIR_FORCE_CLOSE((*fdlist)[i]);
@@ -977,8 +980,8 @@ virProcessSetMaxCoreSize(pid_t pid ATTRIBUTE_UNUSED,
 int virProcessGetStartTime(pid_t pid,
unsigned long long *timestamp)
 {
-char *filename = NULL;
-char *buf = NULL;
+VIR_AUTOFREE(char *) filename = NULL;
+VIR_AUTOFREE(char *) buf = NULL;
 char *tmp;
 int ret = -1;
 int len;
@@ -1032,8 +1035,6 @@ int virProcessGetStartTime(pid_t pid,
 
  cleanup:
 virStringListFree(tokens);
-VIR_FREE(filename);
-VIR_FREE(buf);
 return ret;
 }
 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
@@ -1080,7 +1081,7 @@ static int virProcessNamespaceHelper(int errfd,
  virProcessNamespaceCallback cb,
  void *opaque)
 {
-char *path;
+VIR_AUTOFREE(char *) path = NULL;
 int fd = -1;
 int ret = -1;
 
@@ -1109,7 +1110,6 @@ static int virProcessNamespaceHelper(int errfd,
 ignore_value(safewrite(errfd, err->message, len));
 }
 }
-VIR_FREE(path);
 VIR_FORCE_CLOSE(fd);
 return ret;
 }
@@ -1145,7 +1145,7 @@ virProcessRunInMountNamespace(pid_t pid,
 VIR_FORCE_CLOSE(errfd[1]);
 _exit(ret < 0 ? EXIT_CANCELED : ret);
 } else {
-ch

[libvirt] [PATCH v1 17/32] util: netdevopenvswitch: use VIR_AUTOFREE instead of VIR_FREE for scalar types

2018-07-28 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOFREE macro for declaring scalar variables, majority
of the VIR_FREE calls can be dropped, which in turn leads to
getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virnetdevopenvswitch.c | 25 -
 1 file changed, 8 insertions(+), 17 deletions(-)

diff --git a/src/util/virnetdevopenvswitch.c b/src/util/virnetdevopenvswitch.c
index d1c5cf4..a9c5e2a 100644
--- a/src/util/virnetdevopenvswitch.c
+++ b/src/util/virnetdevopenvswitch.c
@@ -149,10 +149,10 @@ int virNetDevOpenvswitchAddPort(const char *brname, const 
char *ifname,
 char macaddrstr[VIR_MAC_STRING_BUFLEN];
 char ifuuidstr[VIR_UUID_STRING_BUFLEN];
 char vmuuidstr[VIR_UUID_STRING_BUFLEN];
-char *attachedmac_ex_id = NULL;
-char *ifaceid_ex_id = NULL;
-char *profile_ex_id = NULL;
-char *vmid_ex_id = NULL;
+VIR_AUTOFREE(char *) attachedmac_ex_id = NULL;
+VIR_AUTOFREE(char *) ifaceid_ex_id = NULL;
+VIR_AUTOFREE(char *) profile_ex_id = NULL;
+VIR_AUTOFREE(char *) vmid_ex_id = NULL;
 
 virMacAddrFormat(macaddr, macaddrstr);
 virUUIDFormat(ovsport->interfaceID, ifuuidstr);
@@ -209,10 +209,6 @@ int virNetDevOpenvswitchAddPort(const char *brname, const 
char *ifname,
 
 ret = 0;
  cleanup:
-VIR_FREE(attachedmac_ex_id);
-VIR_FREE(ifaceid_ex_id);
-VIR_FREE(vmid_ex_id);
-VIR_FREE(profile_ex_id);
 virCommandFree(cmd);
 return ret;
 }
@@ -339,7 +335,7 @@ virNetDevOpenvswitchInterfaceStats(const char *ifname,
virDomainInterfaceStatsPtr stats)
 {
 virCommandPtr cmd = NULL;
-char *output;
+VIR_AUTOFREE(char *) output = NULL;
 char *tmp;
 bool gotStats = false;
 int ret = -1;
@@ -399,7 +395,6 @@ virNetDevOpenvswitchInterfaceStats(const char *ifname,
 ret = 0;
 
  cleanup:
-VIR_FREE(output);
 virCommandFree(cmd);
 return ret;
 }
@@ -424,7 +419,6 @@ int
 virNetDevOpenvswitchInterfaceGetMaster(const char *ifname, char **master)
 {
 virCommandPtr cmd = NULL;
-int ret = -1;
 int exitstatus;
 
 *master = NULL;
@@ -438,7 +432,7 @@ virNetDevOpenvswitchInterfaceGetMaster(const char *ifname, 
char **master)
 virReportError(VIR_ERR_INTERNAL_ERROR,
_("Unable to run command to get OVS master for "
  "interface %s"), ifname);
-goto cleanup;
+return -1;
 }
 
 /* non-0 exit code just means that the interface has no master in OVS */
@@ -454,9 +448,7 @@ virNetDevOpenvswitchInterfaceGetMaster(const char *ifname, 
char **master)
 
 VIR_DEBUG("OVS master for %s is %s", ifname, *master ? *master : "(none)");
 
-ret = 0;
- cleanup:
-return ret;
+return 0;
 }
 
 
@@ -476,12 +468,12 @@ virNetDevOpenvswitchGetVhostuserIfname(const char *path,
char **ifname)
 {
 virCommandPtr cmd = NULL;
+VIR_AUTOFREE(char *) ovs_timeout = NULL;
 char *tmpIfname = NULL;
 char **tokens = NULL;
 size_t ntokens = 0;
 int status;
 int ret = -1;
-char *ovs_timeout = NULL;
 
 /* Openvswitch vhostuser path are hardcoded to
  * //openvswitch/
@@ -513,7 +505,6 @@ virNetDevOpenvswitchGetVhostuserIfname(const char *path,
  cleanup:
 virStringListFreeCount(tokens, ntokens);
 virCommandFree(cmd);
-VIR_FREE(ovs_timeout);
 return ret;
 }
 
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v1 19/32] util: netdevtap: use VIR_AUTOFREE instead of VIR_FREE for scalar types

2018-07-28 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOFREE macro for declaring scalar variables, majority
of the VIR_FREE calls can be dropped, which in turn leads to
getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virnetdevtap.c | 11 +++
 1 file changed, 3 insertions(+), 8 deletions(-)

diff --git a/src/util/virnetdevtap.c b/src/util/virnetdevtap.c
index d432577..c2c2e73 100644
--- a/src/util/virnetdevtap.c
+++ b/src/util/virnetdevtap.c
@@ -400,16 +400,14 @@ int virNetDevTapCreate(char **ifname,
 if (strstr(*ifname, "%d") != NULL) {
 size_t i;
 for (i = 0; i <= IF_MAXUNIT; i++) {
-char *newname;
+VIR_AUTOFREE(char *) newname = NULL;
 if (virAsprintf(, *ifname, i) < 0)
 goto cleanup;
 
 if (virNetDevExists(newname) == 0) {
-newifname = newname;
+VIR_STEAL_PTR(newifname, newname);
 break;
 }
-
-VIR_FREE(newname);
 }
 if (newifname) {
 VIR_FREE(*ifname);
@@ -423,7 +421,7 @@ int virNetDevTapCreate(char **ifname,
 }
 
 if (tapfd) {
-char *dev_path = NULL;
+VIR_AUTOFREE(char *) dev_path = NULL;
 if (virAsprintf(_path, "/dev/%s", ifr.ifr_name) < 0)
 goto cleanup;
 
@@ -431,11 +429,8 @@ int virNetDevTapCreate(char **ifname,
 virReportSystemError(errno,
  _("Unable to open %s"),
  dev_path);
-VIR_FREE(dev_path);
 goto cleanup;
 }
-
-VIR_FREE(dev_path);
 }
 
 if (virNetDevSetName(ifr.ifr_name, *ifname) == -1)
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v1 31/32] util: qemu: use VIR_AUTOFREE instead of VIR_FREE for scalar types

2018-07-28 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOFREE macro for declaring scalar variables, majority
of the VIR_FREE calls can be dropped, which in turn leads to
getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virqemu.c | 26 +++---
 1 file changed, 7 insertions(+), 19 deletions(-)

diff --git a/src/util/virqemu.c b/src/util/virqemu.c
index 30b8dc1..4089b8e 100644
--- a/src/util/virqemu.c
+++ b/src/util/virqemu.c
@@ -85,29 +85,22 @@ virQEMUBuildCommandLineJSONArrayNumbered(const char *key,
  virBufferPtr buf)
 {
 virJSONValuePtr member;
-char *prefix = NULL;
 size_t i;
-int ret = 0;
 
 for (i = 0; i < virJSONValueArraySize(array); i++) {
+VIR_AUTOFREE(char *) prefix = NULL;
 member = virJSONValueArrayGet((virJSONValuePtr) array, i);
 
 if (virAsprintf(, "%s.%zu", key, i) < 0)
-goto cleanup;
+return 0;
 
 if (virQEMUBuildCommandLineJSONRecurse(prefix, member, buf,

virQEMUBuildCommandLineJSONArrayNumbered,
true) < 0)
-goto cleanup;
-
-VIR_FREE(prefix);
+return 0;
 }
 
-ret = 0;
-
- cleanup:
-VIR_FREE(prefix);
-return ret;
+return 0;
 }
 
 
@@ -118,23 +111,18 @@ virQEMUBuildCommandLineJSONIterate(const char *key,
void *opaque)
 {
 struct virQEMUCommandLineJSONIteratorData *data = opaque;
-char *tmpkey = NULL;
-int ret = -1;
 
 if (data->prefix) {
+VIR_AUTOFREE(char *) tmpkey = NULL;
 if (virAsprintf(, "%s.%s", data->prefix, key) < 0)
 return -1;
 
-ret = virQEMUBuildCommandLineJSONRecurse(tmpkey, value, data->buf,
+return virQEMUBuildCommandLineJSONRecurse(tmpkey, value, data->buf,
  data->arrayFunc, false);
-
-VIR_FREE(tmpkey);
 } else {
-ret = virQEMUBuildCommandLineJSONRecurse(key, value, data->buf,
+return virQEMUBuildCommandLineJSONRecurse(key, value, data->buf,
  data->arrayFunc, false);
 }
-
-return ret;
 }
 
 
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v1 25/32] util: numa: use VIR_AUTOPTR for aggregate types

2018-07-28 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOPTR macro for declaring aggregate pointer variables,
majority of the calls to *Free functions can be dropped, which
in turn leads to getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virnuma.c | 28 +---
 1 file changed, 9 insertions(+), 19 deletions(-)

diff --git a/src/util/virnuma.c b/src/util/virnuma.c
index 841c7cb..fde46ec 100644
--- a/src/util/virnuma.c
+++ b/src/util/virnuma.c
@@ -57,7 +57,7 @@ char *
 virNumaGetAutoPlacementAdvice(unsigned short vcpus,
   unsigned long long balloon)
 {
-virCommandPtr cmd = NULL;
+VIR_AUTOPTR(virCommand) cmd = NULL;
 char *output = NULL;
 
 cmd = virCommandNewArgList(NUMAD, "-w", NULL);
@@ -71,7 +71,6 @@ virNumaGetAutoPlacementAdvice(unsigned short vcpus,
_("Failed to query numad for the "
  "advisory nodeset"));
 
-virCommandFree(cmd);
 return output;
 }
 #else /* !HAVE_NUMAD */
@@ -252,41 +251,38 @@ int
 virNumaGetNodeCPUs(int node,
virBitmapPtr *cpus)
 {
+VIR_AUTOPTR(virBitmap) cpumap = NULL;
 VIR_AUTOFREE(unsigned long *) mask = NULL;
 VIR_AUTOFREE(unsigned long *) allonesmask = NULL;
-virBitmapPtr cpumap = NULL;
 int ncpus = 0;
 int max_n_cpus = virNumaGetMaxCPUs();
 int mask_n_bytes = max_n_cpus / 8;
 size_t i;
-int ret = -1;
 
 *cpus = NULL;
 
 if (VIR_ALLOC_N(mask, mask_n_bytes / sizeof(*mask)) < 0)
-goto cleanup;
+return -1;
 
 if (VIR_ALLOC_N(allonesmask, mask_n_bytes / sizeof(*mask)) < 0)
-goto cleanup;
+return -1;
 
 memset(allonesmask, 0xff, mask_n_bytes);
 
 /* The first time this returns -1, ENOENT if node doesn't exist... */
 if (numa_node_to_cpus(node, mask, mask_n_bytes) < 0) {
 VIR_WARN("NUMA topology for cell %d is not available, ignoring", node);
-ret = -2;
-goto cleanup;
+return -2;
 }
 
 /* second, third... times it returns an all-1's mask */
 if (memcmp(mask, allonesmask, mask_n_bytes) == 0) {
 VIR_DEBUG("NUMA topology for cell %d is invalid, ignoring", node);
-ret = -2;
-goto cleanup;
+return -2;
 }
 
 if (!(cpumap = virBitmapNew(max_n_cpus)))
-goto cleanup;
+return -1;
 
 for (i = 0; i < max_n_cpus; i++) {
 if (MASK_CPU_ISSET(mask, i)) {
@@ -295,14 +291,8 @@ virNumaGetNodeCPUs(int node,
 }
 }
 
-*cpus = cpumap;
-cpumap = NULL;
-ret = ncpus;
-
- cleanup:
-virBitmapFree(cpumap);
-
-return ret;
+VIR_STEAL_PTR(*cpus, cpumap);
+return ncpus;
 }
 # undef MASK_CPU_ISSET
 # undef n_bits
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v1 06/32] util: netdev: use VIR_AUTOFREE instead of VIR_FREE for scalar types

2018-07-28 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOFREE macro for declaring scalar variables, majority
of the VIR_FREE calls can be dropped, which in turn leads to
getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virnetdev.c | 276 ++-
 1 file changed, 96 insertions(+), 180 deletions(-)

diff --git a/src/util/virnetdev.c b/src/util/virnetdev.c
index 9eca786..7653f8b 100644
--- a/src/util/virnetdev.c
+++ b/src/util/virnetdev.c
@@ -535,10 +535,9 @@ int virNetDevSetMTUFromDevice(const char *ifname,
  */
 int virNetDevSetNamespace(const char *ifname, pid_t pidInNs)
 {
-int ret = -1;
-char *pid = NULL;
-char *phy = NULL;
-char *phy_path = NULL;
+VIR_AUTOFREE(char *) pid = NULL;
+VIR_AUTOFREE(char *) phy = NULL;
+VIR_AUTOFREE(char *) phy_path = NULL;
 int len;
 
 if (virAsprintf(, "%lld", (long long) pidInNs) == -1)
@@ -546,7 +545,7 @@ int virNetDevSetNamespace(const char *ifname, pid_t pidInNs)
 
 /* The 802.11 wireless devices only move together with their PHY. */
 if (virNetDevSysfsFile(_path, ifname, "phy80211/name") < 0)
-goto cleanup;
+return -1;
 
 if ((len = virFileReadAllQuiet(phy_path, 1024, )) <= 0) {
 /* Not a wireless device. */
@@ -556,7 +555,7 @@ int virNetDevSetNamespace(const char *ifname, pid_t pidInNs)
 
 argv[5] = pid;
 if (virRun(argv, NULL) < 0)
-goto cleanup;
+return -1;
 
 } else {
 const char *argv[] = {
@@ -569,15 +568,10 @@ int virNetDevSetNamespace(const char *ifname, pid_t 
pidInNs)
 argv[2] = phy;
 argv[5] = pid;
 if (virRun(argv, NULL) < 0)
-goto cleanup;
+return -1;
 }
 
-ret = 0;
- cleanup:
-VIR_FREE(phy_path);
-VIR_FREE(phy);
-VIR_FREE(pid);
-return ret;
+return 0;
 }
 
 #if defined(SIOCSIFNAME) && defined(HAVE_STRUCT_IFREQ)
@@ -969,25 +963,21 @@ int virNetDevGetIndex(const char *ifname ATTRIBUTE_UNUSED,
 int
 virNetDevGetMaster(const char *ifname, char **master)
 {
-int ret = -1;
-void *nlData = NULL;
+VIR_AUTOFREE(void *) nlData = NULL;
 struct nlattr *tb[IFLA_MAX + 1] = {NULL, };
 
 *master = NULL;
 
 if (virNetlinkDumpLink(ifname, -1, , tb, 0, 0) < 0)
-goto cleanup;
+return -1;
 
 if (tb[IFLA_MASTER]) {
 if (!(*master = virNetDevGetName(*(int *)RTA_DATA(tb[IFLA_MASTER]
-goto cleanup;
+return -1;
 }
 
 VIR_DEBUG("IFLA_MASTER for %s is %s", ifname, *master ? *master : 
"(none)");
-ret = 0;
- cleanup:
-VIR_FREE(nlData);
-return ret;
+return 0;
 }
 
 
@@ -1168,37 +1158,31 @@ virNetDevSysfsDeviceFile(char **pf_sysfs_device_link, 
const char *ifname,
 static bool
 virNetDevIsPCIDevice(const char *devpath)
 {
-char *subsys_link = NULL;
-char *abs_path = NULL;
+VIR_AUTOFREE(char *) subsys_link = NULL;
+VIR_AUTOFREE(char *) abs_path = NULL;
 char *subsys = NULL;
-bool ret = false;
 
 if (virAsprintf(_link, "%s/subsystem", devpath) < 0)
 return false;
 
 if (!virFileExists(subsys_link))
-goto cleanup;
+return false;
 
 if (virFileResolveLink(subsys_link, _path) < 0) {
 virReportError(VIR_ERR_INTERNAL_ERROR,
_("Unable to resolve device subsystem symlink %s"),
subsys_link);
-goto cleanup;
+return false;
 }
 
 subsys = last_component(abs_path);
-ret = STRPREFIX(subsys, "pci");
-
- cleanup:
-VIR_FREE(subsys_link);
-VIR_FREE(abs_path);
-return ret;
+return STRPREFIX(subsys, "pci");
 }
 
 static virPCIDevicePtr
 virNetDevGetPCIDevice(const char *devName)
 {
-char *vfSysfsDevicePath = NULL;
+VIR_AUTOFREE(char *) vfSysfsDevicePath = NULL;
 virPCIDeviceAddressPtr vfPCIAddr = NULL;
 virPCIDevicePtr vfPCIDevice = NULL;
 
@@ -1216,7 +1200,6 @@ virNetDevGetPCIDevice(const char *devName)
   vfPCIAddr->slot, vfPCIAddr->function);
 
  cleanup:
-VIR_FREE(vfSysfsDevicePath);
 VIR_FREE(vfPCIAddr);
 
 return vfPCIDevice;
@@ -1241,25 +1224,20 @@ int
 virNetDevGetPhysPortID(const char *ifname,
char **physPortID)
 {
-int ret = -1;
-char *physPortIDFile = NULL;
+VIR_AUTOFREE(char *) physPortIDFile = NULL;
 
 *physPortID = NULL;
 
 if (virNetDevSysfsFile(, ifname, "phys_port_id") < 0)
-goto cleanup;
+return -1;
 
 /* a failure to read just means the driver doesn't support
- * phys_port_id, so set success now and ignore the return from
- * virFileReadAllQuiet().
+ * phys_port_id, so ignore the return from virFileReadAllQuiet()
+ * and return success.
  */
-ret = 0;
-
 

[libvirt] [PATCH v1 07/32] util: netdev: use VIR_AUTOPTR for aggregate types

2018-07-28 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOPTR macro for declaring aggregate pointer variables,
majority of the calls to *Free functions can be dropped, which
in turn leads to getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virnetdev.c | 129 ++-
 1 file changed, 55 insertions(+), 74 deletions(-)

diff --git a/src/util/virnetdev.c b/src/util/virnetdev.c
index 7653f8b..c5871b4 100644
--- a/src/util/virnetdev.c
+++ b/src/util/virnetdev.c
@@ -1855,16 +1855,15 @@ virNetDevSaveNetConfig(const char *linkdev, int vf,
const char *stateDir,
bool saveVlan)
 {
-int ret = -1;
 const char *pfDevName = NULL;
 VIR_AUTOFREE(char *) pfDevOrig = NULL;
 VIR_AUTOFREE(char *) vfDevOrig = NULL;
 VIR_AUTOFREE(char *) filePath = NULL;
 VIR_AUTOFREE(char *) fileStr = NULL;
+VIR_AUTOPTR(virJSONValue) configJSON = NULL;
 virMacAddr oldMAC;
 char MACStr[VIR_MAC_STRING_BUFLEN];
 int oldVlanTag = -1;
-virJSONValuePtr configJSON = NULL;
 
 if (vf >= 0) {
 /* linkdev is the PF */
@@ -1872,7 +1871,7 @@ virNetDevSaveNetConfig(const char *linkdev, int vf,
 
 /* linkdev should get the VF's netdev name (or NULL if none) */
 if (virNetDevPFGetVF(pfDevName, vf, ) < 0)
-goto cleanup;
+return -1;
 
 linkdev = vfDevOrig;
 saveVlan = true;
@@ -1884,12 +1883,12 @@ virNetDevSaveNetConfig(const char *linkdev, int vf,
  */
 
 if (virNetDevGetPhysicalFunction(linkdev, ) < 0)
-goto cleanup;
+return -1;
 
 pfDevName = pfDevOrig;
 
 if (virNetDevGetVirtualFunctionIndex(pfDevName, linkdev, ) < 0)
-goto cleanup;
+return -1;
 }
 
 if (pfDevName) {
@@ -1907,7 +1906,7 @@ virNetDevSaveNetConfig(const char *linkdev, int vf,
  * explicitly enable the PF in the host system network config.
  */
 if (virNetDevGetOnline(pfDevName, ) < 0)
-goto cleanup;
+return -1;
 
 if (!pfIsOnline) {
 virReportError(VIR_ERR_INTERNAL_ERROR,
@@ -1916,12 +1915,12 @@ virNetDevSaveNetConfig(const char *linkdev, int vf,
  "change host network config to put the "
  "PF online."),
vf, pfDevName);
-goto cleanup;
+return -1;
 }
 }
 
 if (!(configJSON = virJSONValueNewObject()))
-goto cleanup;
+return -1;
 
 /* if there is a PF, it's now in pfDevName, and linkdev is either
  * the VF's name, or NULL (if the VF isn't bound to a net driver
@@ -1930,11 +1929,11 @@ virNetDevSaveNetConfig(const char *linkdev, int vf,
 
 if (pfDevName && saveVlan) {
 if (virAsprintf(, "%s/%s_vf%d", stateDir, pfDevName, vf) < 0)
-goto cleanup;
+return -1;
 
 /* get admin MAC and vlan tag */
 if (virNetDevGetVfConfig(pfDevName, vf, , ) < 0)
-goto cleanup;
+return -1;
 
 if (virJSONValueObjectAppendString(configJSON,
VIR_NETDEV_KEYNAME_ADMIN_MAC,
@@ -1942,39 +1941,36 @@ virNetDevSaveNetConfig(const char *linkdev, int vf,
 virJSONValueObjectAppendNumberInt(configJSON,
   VIR_NETDEV_KEYNAME_VLAN_TAG,
   oldVlanTag) < 0) {
-goto cleanup;
+return -1;
 }
 
 } else {
 if (virAsprintf(, "%s/%s", stateDir, linkdev) < 0)
-goto cleanup;
+return -1;
 }
 
 if (linkdev) {
 if (virNetDevGetMAC(linkdev, ) < 0)
-goto cleanup;
+return -1;
 
 /* for interfaces with no pfDevName (i.e. not a VF, this will
  * be the only value in the file.
  */
 if (virJSONValueObjectAppendString(configJSON, VIR_NETDEV_KEYNAME_MAC,
virMacAddrFormat(, MACStr)) 
< 0)
-   goto cleanup;
+return -1;
 }
 
 if (!(fileStr = virJSONValueToString(configJSON, true)))
-goto cleanup;
+return -1;
 
 if (virFileWriteStr(filePath, fileStr, O_CREAT|O_TRUNC|O_WRONLY) < 0) {
 virReportSystemError(errno, _("Unable to preserve mac/vlan tag "
   "for device = %s, vf = %d"), linkdev, 
vf);
-goto cleanup;
+return -1;
 }
 
-ret = 0;
- cleanup:
-virJSONValueFree(configJSON);
-return ret;
+return 0;
 }
 
 
@@ -2012,7 +2008,10 @@ virNetDevReadNetConfig(const char *linkdev, int vf,
 VIR_AUTOFREE(char *) vfDevOrig = NULL;
 VIR_AUTOFREE(char *) filePath = NULL;
 VI

[libvirt] [PATCH v1 14/32] util: netdevmacvlan: define cleanup function using VIR_DEFINE_AUTOPTR_FUNC

2018-07-28 Thread Sukrit Bhatnagar
Using the new VIR_DEFINE_AUTOPTR_FUNC macro defined in
src/util/viralloc.h, define a new wrapper around an existing
cleanup function which will be called when a variable declared
with VIR_AUTOPTR macro goes out of scope. Also, drop the redundant
viralloc.h include, since that has moved from the source module into
the header.

When a variable of type virNetlinkCallbackDataPtr is declared using
VIR_AUTOPTR, the function virNetlinkCallbackDataFree will be run
automatically on it when it goes out of scope.

This commit also adds an intermediate typedef for virNetlinkCallbackData
type for use with the cleanup macros.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virnetdevmacvlan.c | 7 +--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/src/util/virnetdevmacvlan.c b/src/util/virnetdevmacvlan.c
index fb41bf9..91c6244 100644
--- a/src/util/virnetdevmacvlan.c
+++ b/src/util/virnetdevmacvlan.c
@@ -576,7 +576,7 @@ static const uint32_t modeMap[VIR_NETDEV_MACVLAN_MODE_LAST] 
= {
 };
 
 /* Struct to hold the state and configuration of a 802.1qbg port */
-struct virNetlinkCallbackData {
+struct _virNetlinkCallbackData {
 char *cr_ifname;
 virNetDevVPortProfilePtr virtPortProfile;
 virMacAddr macaddress;
@@ -587,7 +587,8 @@ struct virNetlinkCallbackData {
 unsigned int linkState;
 };
 
-typedef struct virNetlinkCallbackData *virNetlinkCallbackDataPtr;
+typedef struct _virNetlinkCallbackData virNetlinkCallbackData;
+typedef virNetlinkCallbackData *virNetlinkCallbackDataPtr;
 
 # define INSTANCE_STRLEN 36
 
@@ -870,6 +871,8 @@ virNetlinkCallbackDataFree(virNetlinkCallbackDataPtr calld)
 VIR_FREE(calld);
 }
 
+VIR_DEFINE_AUTOPTR_FUNC(virNetlinkCallbackData, virNetlinkCallbackDataFree)
+
 /**
  * virNetDevMacVLanVPortProfileDestroyCallback:
  *
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v1 03/32] util: netdevbridge: use VIR_AUTOFREE instead of VIR_FREE for scalar types

2018-07-28 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOFREE macro for declaring scalar variables, majority
of the VIR_FREE calls can be dropped, which in turn leads to
getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virnetdevbridge.c | 45 +++--
 1 file changed, 15 insertions(+), 30 deletions(-)

diff --git a/src/util/virnetdevbridge.c b/src/util/virnetdevbridge.c
index e46ac35..bf30d7c 100644
--- a/src/util/virnetdevbridge.c
+++ b/src/util/virnetdevbridge.c
@@ -126,8 +126,7 @@ static int virNetDevBridgeSet(const char *brname,
   int fd, /* control socket */
   struct ifreq *ifr)  /* pre-filled bridge 
name */
 {
-char *path = NULL;
-int ret = -1;
+VIR_AUTOFREE(char *) path = NULL;
 
 if (virAsprintf(, SYSFS_NET_DIR "%s/bridge/%s", brname, paramname) < 
0)
 return -1;
@@ -138,7 +137,7 @@ static int virNetDevBridgeSet(const char *brname,
 if (virFileWriteStr(path, valuestr, 0) < 0) {
 virReportSystemError(errno,
  _("Unable to set bridge %s %s"), brname, 
paramname);
-goto cleanup;
+return -1;
 }
 } else {
 unsigned long paramid;
@@ -149,21 +148,18 @@ static int virNetDevBridgeSet(const char *brname,
 } else {
 virReportSystemError(EINVAL,
  _("Unable to set bridge %s %s"), brname, 
paramname);
-goto cleanup;
+return -1;
 }
 unsigned long args[] = { paramid, value, 0, 0 };
 ifr->ifr_data = (char*)
 if (ioctl(fd, SIOCDEVPRIVATE, ifr) < 0) {
 virReportSystemError(errno,
  _("Unable to set bridge %s %s"), brname, 
paramname);
-goto cleanup;
+return -1;
 }
 }
 
-ret = 0;
- cleanup:
-VIR_FREE(path);
-return ret;
+return 0;
 }
 
 
@@ -171,7 +167,7 @@ static int virNetDevBridgeGet(const char *brname,
   const char *paramname,  /* sysfs param name */
   unsigned long *value)   /* current value */
 {
-char *path = NULL;
+VIR_AUTOFREE(char *) path = NULL;
 int ret = -1;
 int fd = -1;
 struct ifreq ifr;
@@ -180,7 +176,7 @@ static int virNetDevBridgeGet(const char *brname,
 return -1;
 
 if (virFileExists(path)) {
-char *valuestr;
+VIR_AUTOFREE(char *) valuestr = NULL;
 if (virFileReadAll(path, INT_BUFSIZE_BOUND(unsigned long),
) < 0)
 goto cleanup;
@@ -189,10 +185,8 @@ static int virNetDevBridgeGet(const char *brname,
 virReportSystemError(EINVAL,
  _("Unable to get bridge %s %s"),
  brname, paramname);
-VIR_FREE(valuestr);
 goto cleanup;
 }
-VIR_FREE(valuestr);
 } else {
 struct __bridge_info info;
 unsigned long args[] = { BRCTL_GET_BRIDGE_INFO, (unsigned long), 
0, 0 };
@@ -221,7 +215,6 @@ static int virNetDevBridgeGet(const char *brname,
 ret = 0;
  cleanup:
 VIR_FORCE_CLOSE(fd);
-VIR_FREE(path);
 return ret;
 }
 #endif /* __linux__ */
@@ -233,7 +226,7 @@ virNetDevBridgePortSet(const char *brname,
const char *paramname,
unsigned long value)
 {
-char *path = NULL;
+VIR_AUTOFREE(char *) path = NULL;
 char valuestr[INT_BUFSIZE_BOUND(value)];
 int ret = -1;
 
@@ -254,7 +247,6 @@ virNetDevBridgePortSet(const char *brname,
  brname, ifname, paramname, valuestr);
 }
 
-VIR_FREE(path);
 return ret;
 }
 
@@ -265,29 +257,24 @@ virNetDevBridgePortGet(const char *brname,
const char *paramname,
unsigned long *value)
 {
-char *path = NULL;
-char *valuestr = NULL;
-int ret = -1;
+VIR_AUTOFREE(char *) path = NULL;
+VIR_AUTOFREE(char *) valuestr = NULL;
 
 if (virAsprintf(, SYSFS_NET_DIR "%s/brif/%s/%s",
 brname, ifname, paramname) < 0)
 return -1;
 
 if (virFileReadAll(path, INT_BUFSIZE_BOUND(unsigned long), ) < 0)
-goto cleanup;
+return -1;
 
 if (virStrToLong_ul(valuestr, NULL, 10, value) < 0) {
 virReportSystemError(EINVAL,
  _("Unable to get bridge %s port %s %s"),
  brname, ifname, paramname);
-goto cleanup;
+return -1;
 }
 
-ret = 0;
- cleanup:
-VIR_FREE(path);
-VIR_FREE(valuestr);
-return ret;
+return 0;
 }
 
 
@@ -430,7 +417,7 @@ virNetDevBridgeCreate(const char *brname)
 /* use a netlink RTM_NEWLINK message to

[libvirt] [PATCH v1 18/32] util: netdevopenvswitch: use VIR_AUTOPTR for aggregate types

2018-07-28 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOPTR macro for declaring aggregate pointer variables,
majority of the calls to *Free functions can be dropped, which
in turn leads to getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virnetdevopenvswitch.c | 106 +++-
 1 file changed, 40 insertions(+), 66 deletions(-)

diff --git a/src/util/virnetdevopenvswitch.c b/src/util/virnetdevopenvswitch.c
index a9c5e2a..eae5861 100644
--- a/src/util/virnetdevopenvswitch.c
+++ b/src/util/virnetdevopenvswitch.c
@@ -76,9 +76,7 @@ virNetDevOpenvswitchAddTimeout(virCommandPtr cmd)
 static int
 virNetDevOpenvswitchConstructVlans(virCommandPtr cmd, virNetDevVlanPtr 
virtVlan)
 {
-int ret = -1;
 size_t i = 0;
-virBuffer buf = VIR_BUFFER_INITIALIZER;
 
 if (!virtVlan || !virtVlan->nTags)
 return 0;
@@ -98,7 +96,12 @@ virNetDevOpenvswitchConstructVlans(virCommandPtr cmd, 
virNetDevVlanPtr virtVlan)
 }
 
 if (virtVlan->trunk) {
-virBufferAddLit(, "trunk=");
+VIR_AUTOPTR(virBuffer) buf = NULL;
+
+if (VIR_ALLOC(buf) < 0)
+return -1;
+
+virBufferAddLit(buf, "trunk=");
 
 /*
  * Trunk ports have at least one VLAN. Do the first one
@@ -106,24 +109,21 @@ virNetDevOpenvswitchConstructVlans(virCommandPtr cmd, 
virNetDevVlanPtr virtVlan)
  * start of the for loop if there are more than one VLANs
  * on this trunk port.
  */
-virBufferAsprintf(, "%d", virtVlan->tag[i]);
+virBufferAsprintf(buf, "%d", virtVlan->tag[i]);
 
 for (i = 1; i < virtVlan->nTags; i++) {
-virBufferAddLit(, ",");
-virBufferAsprintf(, "%d", virtVlan->tag[i]);
+virBufferAddLit(buf, ",");
+virBufferAsprintf(buf, "%d", virtVlan->tag[i]);
 }
 
-if (virBufferCheckError() < 0)
-goto cleanup;
-virCommandAddArg(cmd, virBufferCurrentContent());
+if (virBufferCheckError(buf) < 0)
+return -1;
+virCommandAddArg(cmd, virBufferCurrentContent(buf));
 } else if (virtVlan->nTags) {
 virCommandAddArgFormat(cmd, "tag=%d", virtVlan->tag[0]);
 }
 
-ret = 0;
- cleanup:
-virBufferFreeAndReset();
-return ret;
+return 0;
 }
 
 /**
@@ -144,11 +144,10 @@ int virNetDevOpenvswitchAddPort(const char *brname, const 
char *ifname,
virNetDevVPortProfilePtr ovsport,
virNetDevVlanPtr virtVlan)
 {
-int ret = -1;
-virCommandPtr cmd = NULL;
 char macaddrstr[VIR_MAC_STRING_BUFLEN];
 char ifuuidstr[VIR_UUID_STRING_BUFLEN];
 char vmuuidstr[VIR_UUID_STRING_BUFLEN];
+VIR_AUTOPTR(virCommand) cmd = NULL;
 VIR_AUTOFREE(char *) attachedmac_ex_id = NULL;
 VIR_AUTOFREE(char *) ifaceid_ex_id = NULL;
 VIR_AUTOFREE(char *) profile_ex_id = NULL;
@@ -160,17 +159,17 @@ int virNetDevOpenvswitchAddPort(const char *brname, const 
char *ifname,
 
 if (virAsprintf(_ex_id, "external-ids:attached-mac=\"%s\"",
 macaddrstr) < 0)
-goto cleanup;
+return -1;
 if (virAsprintf(_ex_id, "external-ids:iface-id=\"%s\"",
 ifuuidstr) < 0)
-goto cleanup;
+return -1;
 if (virAsprintf(_ex_id, "external-ids:vm-id=\"%s\"",
 vmuuidstr) < 0)
-goto cleanup;
+return -1;
 if (ovsport->profileID[0] != '\0') {
 if (virAsprintf(_ex_id, "external-ids:port-profile=\"%s\"",
 ovsport->profileID) < 0)
-goto cleanup;
+return -1;
 }
 
 cmd = virCommandNew(OVSVSCTL);
@@ -179,7 +178,7 @@ int virNetDevOpenvswitchAddPort(const char *brname, const 
char *ifname,
  ifname, "--", "add-port", brname, ifname, NULL);
 
 if (virNetDevOpenvswitchConstructVlans(cmd, virtVlan) < 0)
-goto cleanup;
+return -1;
 
 if (ovsport->profileID[0] == '\0') {
 virCommandAddArgList(cmd,
@@ -204,13 +203,10 @@ int virNetDevOpenvswitchAddPort(const char *brname, const 
char *ifname,
 virReportError(VIR_ERR_INTERNAL_ERROR,
_("Unable to add port %s to OVS bridge %s"),
ifname, brname);
-goto cleanup;
+return -1;
 }
 
-ret = 0;
- cleanup:
-virCommandFree(cmd);
-return ret;
+return 0;
 }
 
 /**
@@ -223,8 +219,7 @@ int virNetDevOpenvswitchAddPort(const char *brname, const 
char *ifname,
  */
 int virNetDevOpenvswitchRemovePort(const char *brname ATTRIBUTE_UNUSED, const 
char *ifname)
 {
-int ret = -1;
-virCommandPtr 

[libvirt] [PATCH v1 13/32] util: netdevip: use VIR_AUTOPTR for aggregate types

2018-07-28 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOPTR macro for declaring aggregate pointer variables,
majority of the calls to *Free functions can be dropped, which
in turn leads to getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virnetdevip.c | 55 +-
 1 file changed, 23 insertions(+), 32 deletions(-)

diff --git a/src/util/virnetdevip.c b/src/util/virnetdevip.c
index 8f1081b..ca206e2 100644
--- a/src/util/virnetdevip.c
+++ b/src/util/virnetdevip.c
@@ -634,19 +634,22 @@ virNetDevIPCheckIPv6Forwarding(void)
 }
 
 if (!valid) {
-virBuffer buf = VIR_BUFFER_INITIALIZER;
+VIR_AUTOPTR(virBuffer) buf = NULL;
+
+if (VIR_ALLOC(buf) < 0)
+goto cleanup;
+
 for (i = 0; i < data.ndevices; i++) {
-virBufferAdd(, data.devices[i], -1);
+virBufferAdd(buf, data.devices[i], -1);
 if (i < data.ndevices - 1)
-virBufferAddLit(, ", ");
+virBufferAddLit(buf, ", ");
 }
 
 virReportError(VIR_ERR_INTERNAL_ERROR,
_("Check the host setup: enabling IPv6 forwarding with "
  "RA routes without accept_ra set to 2 is likely to 
cause "
  "routes loss. Interfaces to look at: %s"),
-   virBufferCurrentContent());
-virBufferFreeAndReset();
+   virBufferCurrentContent(buf));
 }
 
  cleanup:
@@ -665,24 +668,23 @@ virNetDevIPAddrAdd(const char *ifname,
virSocketAddr *peer,
unsigned int prefix)
 {
-virCommandPtr cmd = NULL;
+VIR_AUTOPTR(virCommand) cmd = NULL;
 VIR_AUTOFREE(char *) addrstr = NULL;
 VIR_AUTOFREE(char *) bcaststr = NULL;
 VIR_AUTOFREE(char *) peerstr = NULL;
 virSocketAddr broadcast;
-int ret = -1;
 
 if (!(addrstr = virSocketAddrFormat(addr)))
-goto cleanup;
+return -1;
 
 if (peer && VIR_SOCKET_ADDR_VALID(peer) && !(peerstr = 
virSocketAddrFormat(peer)))
-goto cleanup;
+return -1;
 
 /* format up a broadcast address if this is IPv4 */
 if (!peerstr && ((VIR_SOCKET_ADDR_IS_FAMILY(addr, AF_INET)) &&
 ((virSocketAddrBroadcastByPrefix(addr, prefix, ) < 0) ||
  !(bcaststr = virSocketAddrFormat() {
-goto cleanup;
+return -1;
 }
 
 # ifdef IFCONFIG_PATH
@@ -710,12 +712,9 @@ virNetDevIPAddrAdd(const char *ifname,
 # endif
 
 if (virCommandRun(cmd, NULL) < 0)
-goto cleanup;
+return -1;
 
-ret = 0;
- cleanup:
-virCommandFree(cmd);
-return ret;
+return 0;
 }
 
 
@@ -724,12 +723,11 @@ virNetDevIPAddrDel(const char *ifname,
virSocketAddr *addr,
unsigned int prefix)
 {
-virCommandPtr cmd = NULL;
+VIR_AUTOPTR(virCommand) cmd = NULL;
 VIR_AUTOFREE(char *) addrstr = NULL;
-int ret = -1;
 
 if (!(addrstr = virSocketAddrFormat(addr)))
-goto cleanup;
+return -1;
 # ifdef IFCONFIG_PATH
 cmd = virCommandNew(IFCONFIG_PATH);
 virCommandAddArg(cmd, ifname);
@@ -747,12 +745,9 @@ virNetDevIPAddrDel(const char *ifname,
 # endif
 
 if (virCommandRun(cmd, NULL) < 0)
-goto cleanup;
+return -1;
 
-ret = 0;
- cleanup:
-virCommandFree(cmd);
-return ret;
+return 0;
 }
 
 
@@ -763,15 +758,14 @@ virNetDevIPRouteAdd(const char *ifname,
 virSocketAddrPtr gateway,
 unsigned int metric)
 {
-virCommandPtr cmd = NULL;
+VIR_AUTOPTR(virCommand) cmd = NULL;
 VIR_AUTOFREE(char *) addrstr = NULL;
 VIR_AUTOFREE(char *) gatewaystr = NULL;
-int ret = -1;
 
 if (!(addrstr = virSocketAddrFormat(addr)))
-goto cleanup;
+return -1;
 if (!(gatewaystr = virSocketAddrFormat(gateway)))
-goto cleanup;
+return -1;
 cmd = virCommandNew(IP_PATH);
 virCommandAddArgList(cmd, "route", "add", NULL);
 virCommandAddArgFormat(cmd, "%s/%u", addrstr, prefix);
@@ -780,12 +774,9 @@ virNetDevIPRouteAdd(const char *ifname,
 virCommandAddArgFormat(cmd, "%u", metric);
 
 if (virCommandRun(cmd, NULL) < 0)
-goto cleanup;
+return -1;
 
-ret = 0;
- cleanup:
-virCommandFree(cmd);
-return ret;
+return 0;
 }
 
 
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v1 08/32] util: socketaddr: define cleanup function using VIR_DEFINE_AUTOPTR_FUNC

2018-07-28 Thread Sukrit Bhatnagar
Using the new VIR_DEFINE_AUTOPTR_FUNC macro defined in
src/util/viralloc.h, define a new wrapper around an existing
cleanup function which will be called when a variable declared
with VIR_AUTOPTR macro goes out of scope. Also, drop the redundant
viralloc.h include, since that has moved from the source module into
the header.

When a variable of type virSocketAddrPtr is declared using
VIR_AUTOPTR, the function virSocketAddrFree will be run
automatically on it when it goes out of scope.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virsocketaddr.c | 7 ++-
 src/util/virsocketaddr.h | 9 +++--
 2 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/src/util/virsocketaddr.c b/src/util/virsocketaddr.c
index 5c3bfad..6b52a3d 100644
--- a/src/util/virsocketaddr.c
+++ b/src/util/virsocketaddr.c
@@ -26,7 +26,6 @@
 #include "virsocketaddr.h"
 #include "virerror.h"
 #include "virstring.h"
-#include "viralloc.h"
 #include "virbuffer.h"
 
 #include 
@@ -1253,3 +1252,9 @@ virSocketAddrPTRDomain(const virSocketAddr *addr,
 ret = -2;
 goto cleanup;
 }
+
+void
+virSocketAddrFree(virSocketAddrPtr addr)
+{
+VIR_FREE(addr);
+}
diff --git a/src/util/virsocketaddr.h b/src/util/virsocketaddr.h
index 3029338..66f5998 100644
--- a/src/util/virsocketaddr.h
+++ b/src/util/virsocketaddr.h
@@ -24,14 +24,15 @@
 #ifndef __VIR_SOCKETADDR_H__
 # define __VIR_SOCKETADDR_H__
 
-# include "internal.h"
-
 # include 
 # include 
 # ifdef HAVE_SYS_UN_H
 #  include 
 # endif
 
+# include "internal.h"
+# include "viralloc.h"
+
 /* On architectures which lack these limits, define them (ie. Cygwin).
  * Note that the libvirt code should be robust enough to handle the
  * case where actual value is longer than these limits (eg. by setting
@@ -162,4 +163,8 @@ int virSocketAddrPTRDomain(const virSocketAddr *addr,
char **ptr)
 ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3);
 
+void virSocketAddrFree(virSocketAddrPtr addr);
+
+VIR_DEFINE_AUTOPTR_FUNC(virSocketAddr, virSocketAddrFree)
+
 #endif /* __VIR_SOCKETADDR_H__ */
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v1 16/32] util: netdevmacvlan: use VIR_AUTOPTR for aggregate types

2018-07-28 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOPTR macro for declaring aggregate pointer variables,
majority of the calls to *Free functions can be dropped, which
in turn leads to getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virnetdevmacvlan.c | 28 
 1 file changed, 12 insertions(+), 16 deletions(-)

diff --git a/src/util/virnetdevmacvlan.c b/src/util/virnetdevmacvlan.c
index a2ed65c..d01e5ef 100644
--- a/src/util/virnetdevmacvlan.c
+++ b/src/util/virnetdevmacvlan.c
@@ -898,19 +898,20 @@ virNetDevMacVLanVPortProfileRegisterCallback(const char 
*ifname,
  virNetDevVPortProfilePtr 
virtPortProfile,
  virNetDevVPortProfileOp vmOp)
 {
-virNetlinkCallbackDataPtr calld = NULL;
+VIR_AUTOPTR(virNetlinkCallbackData) calld = NULL;
+virNetlinkCallbackDataPtr temp ATTRIBUTE_UNUSED = NULL;
 
 if (virtPortProfile && virNetlinkEventServiceIsRunning(NETLINK_ROUTE)) {
 if (VIR_ALLOC(calld) < 0)
-goto error;
+return -1;
 if (VIR_STRDUP(calld->cr_ifname, ifname) < 0)
-goto error;
+return -1;
 if (VIR_ALLOC(calld->virtPortProfile) < 0)
-goto error;
+return -1;
 memcpy(calld->virtPortProfile, virtPortProfile, 
sizeof(*virtPortProfile));
 virMacAddrSet(>macaddress, macaddress);
 if (VIR_STRDUP(calld->linkdev, linkdev) < 0)
-goto error;
+return -1;
 memcpy(calld->vmuuid, vmuuid, sizeof(calld->vmuuid));
 
 calld->vmOp = vmOp;
@@ -918,14 +919,12 @@ virNetDevMacVLanVPortProfileRegisterCallback(const char 
*ifname,
 if (virNetlinkEventAddClient(virNetDevMacVLanVPortProfileCallback,
  
virNetDevMacVLanVPortProfileDestroyCallback,
  calld, macaddress, NETLINK_ROUTE) < 0)
-goto error;
+return -1;
 }
 
+VIR_STEAL_PTR(temp, calld);
+
 return 0;
-
- error:
-virNetlinkCallbackDataFree(calld);
-return -1;
 }
 
 
@@ -1184,9 +1183,9 @@ int virNetDevMacVLanDeleteWithVPortProfile(const char 
*ifname,
 }
 
 if (mode == VIR_NETDEV_MACVLAN_MODE_PASSTHRU) {
-virMacAddrPtr MAC = NULL;
-virMacAddrPtr adminMAC = NULL;
-virNetDevVlanPtr vlan = NULL;
+VIR_AUTOPTR(virMacAddr) MAC = NULL;
+VIR_AUTOPTR(virMacAddr) adminMAC = NULL;
+VIR_AUTOPTR(virNetDevVlan) vlan = NULL;
 
 if ((virNetDevReadNetConfig(linkdev, -1, stateDir,
 , , ) == 0) &&
@@ -1194,9 +1193,6 @@ int virNetDevMacVLanDeleteWithVPortProfile(const char 
*ifname,
 
 ignore_value(virNetDevSetNetConfig(linkdev, -1,
adminMAC, vlan, MAC, !!vlan));
-VIR_FREE(MAC);
-VIR_FREE(adminMAC);
-virNetDevVlanFree(vlan);
 }
 }
 
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v1 10/32] util: socketaddr: use VIR_AUTOPTR for aggregate types

2018-07-28 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOPTR macro for declaring aggregate pointer variables,
majority of the calls to *Free functions can be dropped, which
in turn leads to getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virsocketaddr.c | 38 --
 1 file changed, 16 insertions(+), 22 deletions(-)

diff --git a/src/util/virsocketaddr.c b/src/util/virsocketaddr.c
index eee725d..1b195cd 100644
--- a/src/util/virsocketaddr.c
+++ b/src/util/virsocketaddr.c
@@ -1193,52 +1193,46 @@ virSocketAddrPTRDomain(const virSocketAddr *addr,
unsigned int prefix,
char **ptr)
 {
-virBuffer buf = VIR_BUFFER_INITIALIZER;
+VIR_AUTOPTR(virBuffer) buf = NULL;
 size_t i;
-int ret = -1;
+
+if (VIR_ALLOC(buf) < 0)
+return -1;
 
 if (VIR_SOCKET_ADDR_IS_FAMILY(addr, AF_INET)) {
 virSocketAddrIPv4 ip;
 
 if (prefix == 0 || prefix >= 32 || prefix % 8 != 0)
-goto unsupported;
+return -2;
 
 if (virSocketAddrGetIPv4Addr(addr, ) < 0)
-goto cleanup;
+return -1;
 
 for (i = prefix / 8; i > 0; i--)
-virBufferAsprintf(, "%u.", ip[i - 1]);
+virBufferAsprintf(buf, "%u.", ip[i - 1]);
 
-virBufferAddLit(, VIR_SOCKET_ADDR_IPV4_ARPA);
+virBufferAddLit(buf, VIR_SOCKET_ADDR_IPV4_ARPA);
 } else if (VIR_SOCKET_ADDR_IS_FAMILY(addr, AF_INET6)) {
 virSocketAddrIPv6Nibbles ip;
 
 if (prefix == 0 || prefix >= 128 || prefix % 4 != 0)
-goto unsupported;
+return -2;
 
 if (virSocketAddrGetIPv6Nibbles(addr, ) < 0)
-goto cleanup;
+return -1;
 
 for (i = prefix / 4; i > 0; i--)
-virBufferAsprintf(, "%x.", ip[i - 1]);
+virBufferAsprintf(buf, "%x.", ip[i - 1]);
 
-virBufferAddLit(, VIR_SOCKET_ADDR_IPV6_ARPA);
+virBufferAddLit(buf, VIR_SOCKET_ADDR_IPV6_ARPA);
 } else {
-goto unsupported;
+return -2;
 }
 
-if (!(*ptr = virBufferContentAndReset()))
-goto cleanup;
+if (!(*ptr = virBufferContentAndReset(buf)))
+return -1;
 
-ret = 0;
-
- cleanup:
-virBufferFreeAndReset();
-return ret;
-
- unsupported:
-ret = -2;
-goto cleanup;
+return 0;
 }
 
 void
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v1 01/32] util: iscsi: use VIR_AUTOFREE instead of VIR_FREE for scalar types

2018-07-28 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOFREE macro for declaring scalar variables, majority
of the VIR_FREE calls can be dropped, which in turn leads to
getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/viriscsi.c | 31 ++-
 1 file changed, 10 insertions(+), 21 deletions(-)

diff --git a/src/util/viriscsi.c b/src/util/viriscsi.c
index 653b4fd..48f4106 100644
--- a/src/util/viriscsi.c
+++ b/src/util/viriscsi.c
@@ -88,7 +88,7 @@ virISCSIGetSession(const char *devpath,
 .session = NULL,
 .devpath = devpath,
 };
-char *error = NULL;
+VIR_AUTOFREE(char *) error = NULL;
 int exitstatus = 0;
 
 virCommandPtr cmd = virCommandNewArgList(ISCSIADM, "--mode",
@@ -109,7 +109,6 @@ virISCSIGetSession(const char *devpath,
NULLSTR(error));
 
  cleanup:
-VIR_FREE(error);
 virCommandFree(cmd);
 return cbdata.session;
 }
@@ -125,10 +124,10 @@ virStorageBackendIQNFound(const char *initiatoriqn,
   char **ifacename)
 {
 int ret = IQN_ERROR;
-char *outbuf = NULL;
 char *line = NULL;
-char *iface = NULL;
-char *iqn = NULL;
+VIR_AUTOFREE(char *) outbuf = NULL;
+VIR_AUTOFREE(char *) iface = NULL;
+VIR_AUTOFREE(char *) iqn = NULL;
 virCommandPtr cmd = virCommandNewArgList(ISCSIADM,
  "--mode", "iface", NULL);
 
@@ -197,9 +196,6 @@ virStorageBackendIQNFound(const char *initiatoriqn,
 if (ret == IQN_MISSING)
 VIR_DEBUG("Could not find interface with IQN '%s'", iqn);
 
-VIR_FREE(iqn);
-VIR_FREE(iface);
-VIR_FREE(outbuf);
 virCommandFree(cmd);
 return ret;
 
@@ -216,7 +212,7 @@ virStorageBackendCreateIfaceIQN(const char *initiatoriqn,
 char **ifacename)
 {
 int ret = -1, exitstatus = -1;
-char *temp_ifacename;
+VIR_AUTOFREE(char *) temp_ifacename = NULL;
 virCommandPtr cmd = NULL;
 
 if (virAsprintf(_ifacename,
@@ -277,7 +273,6 @@ virStorageBackendCreateIfaceIQN(const char *initiatoriqn,
 
  cleanup:
 virCommandFree(cmd);
-VIR_FREE(temp_ifacename);
 if (ret != 0)
 VIR_FREE(*ifacename);
 return ret;
@@ -299,7 +294,7 @@ virISCSIConnection(const char *portal,
 NULL
 };
 virCommandPtr cmd;
-char *ifacename = NULL;
+VIR_AUTOFREE(char *) ifacename = NULL;
 
 cmd = virCommandNewArgs(baseargv);
 virCommandAddArgSet(cmd, extraargv);
@@ -339,7 +334,6 @@ virISCSIConnection(const char *portal,
 
  cleanup:
 virCommandFree(cmd);
-VIR_FREE(ifacename);
 
 return ret;
 }
@@ -390,15 +384,13 @@ virISCSIGetTargets(char **const groups,
void *data)
 {
 struct virISCSITargetList *list = data;
-char *target;
+VIR_AUTOFREE(char *) target = NULL;
 
 if (VIR_STRDUP(target, groups[1]) < 0)
 return -1;
 
-if (VIR_APPEND_ELEMENT(list->targets, list->ntargets, target) < 0) {
-VIR_FREE(target);
+if (VIR_APPEND_ELEMENT(list->targets, list->ntargets, target) < 0)
 return -1;
-}
 
 return 0;
 }
@@ -498,8 +490,7 @@ virISCSIScanTargets(const char *portal,
 size_t *ntargets,
 char ***targets)
 {
-char *ifacename = NULL;
-int ret = -1;
+VIR_AUTOFREE(char *) ifacename = NULL;
 
 if (ntargets)
 *ntargets = 0;
@@ -522,10 +513,8 @@ virISCSIScanTargets(const char *portal,
 }
 }
 
-ret = virISCSIScanTargetsInternal(portal, ifacename,
+return virISCSIScanTargetsInternal(portal, ifacename,
   persist, ntargets, targets);
-VIR_FREE(ifacename);
-return ret;
 }
 
 
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v1 02/32] util: iscsi: use VIR_AUTOPTR for aggregate types

2018-07-28 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOPTR macro for declaring aggregate pointer variables,
majority of the calls to *Free functions can be dropped, which
in turn leads to getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/viriscsi.c | 68 +
 1 file changed, 22 insertions(+), 46 deletions(-)

diff --git a/src/util/viriscsi.c b/src/util/viriscsi.c
index 48f4106..81404f8 100644
--- a/src/util/viriscsi.c
+++ b/src/util/viriscsi.c
@@ -91,7 +91,7 @@ virISCSIGetSession(const char *devpath,
 VIR_AUTOFREE(char *) error = NULL;
 int exitstatus = 0;
 
-virCommandPtr cmd = virCommandNewArgList(ISCSIADM, "--mode",
+VIR_AUTOPTR(virCommand) cmd = virCommandNewArgList(ISCSIADM, "--mode",
  "session", NULL);
 virCommandSetErrorBuffer(cmd, );
 
@@ -101,15 +101,13 @@ virISCSIGetSession(const char *devpath,
vars,
virISCSIExtractSession,
, NULL, ) < 0)
-goto cleanup;
+return NULL;
 
 if (cbdata.session == NULL && !probe)
 virReportError(VIR_ERR_INTERNAL_ERROR,
_("cannot find iscsiadm session: %s"),
NULLSTR(error));
 
- cleanup:
-virCommandFree(cmd);
 return cbdata.session;
 }
 
@@ -128,7 +126,7 @@ virStorageBackendIQNFound(const char *initiatoriqn,
 VIR_AUTOFREE(char *) outbuf = NULL;
 VIR_AUTOFREE(char *) iface = NULL;
 VIR_AUTOFREE(char *) iqn = NULL;
-virCommandPtr cmd = virCommandNewArgList(ISCSIADM,
+VIR_AUTOPTR(virCommand) cmd = virCommandNewArgList(ISCSIADM,
  "--mode", "iface", NULL);
 
 *ifacename = NULL;
@@ -196,7 +194,6 @@ virStorageBackendIQNFound(const char *initiatoriqn,
 if (ret == IQN_MISSING)
 VIR_DEBUG("Could not find interface with IQN '%s'", iqn);
 
-virCommandFree(cmd);
 return ret;
 
  error:
@@ -213,7 +210,7 @@ virStorageBackendCreateIfaceIQN(const char *initiatoriqn,
 {
 int ret = -1, exitstatus = -1;
 VIR_AUTOFREE(char *) temp_ifacename = NULL;
-virCommandPtr cmd = NULL;
+VIR_AUTOPTR(virCommand) cmd = NULL;
 
 if (virAsprintf(_ifacename,
 "libvirt-iface-%08llx",
@@ -272,7 +269,6 @@ virStorageBackendCreateIfaceIQN(const char *initiatoriqn,
 ret = 0;
 
  cleanup:
-virCommandFree(cmd);
 if (ret != 0)
 VIR_FREE(*ifacename);
 return ret;
@@ -285,7 +281,6 @@ virISCSIConnection(const char *portal,
const char *target,
const char **extraargv)
 {
-int ret = -1;
 const char *const baseargv[] = {
 ISCSIADM,
 "--mode", "node",
@@ -293,7 +288,7 @@ virISCSIConnection(const char *portal,
 "--targetname", target,
 NULL
 };
-virCommandPtr cmd;
+VIR_AUTOPTR(virCommand) cmd = NULL;
 VIR_AUTOFREE(char *) ifacename = NULL;
 
 cmd = virCommandNewArgs(baseargv);
@@ -306,7 +301,7 @@ virISCSIConnection(const char *portal,
 break;
 case IQN_MISSING:
 if (virStorageBackendCreateIfaceIQN(initiatoriqn, ) != 0)
-goto cleanup;
+return -1;
 /*
  * iscsiadm doesn't let you send commands to the Interface IQN,
  * unless you've first issued a 'sendtargets' command to the
@@ -317,25 +312,20 @@ virISCSIConnection(const char *portal,
  */
 if (virISCSIScanTargetsInternal(portal, ifacename,
 true, NULL, NULL) < 0)
-goto cleanup;
+return -1;
 
 break;
 case IQN_ERROR:
 default:
-goto cleanup;
+return -1;
 }
 virCommandAddArgList(cmd, "--interface", ifacename, NULL);
 }
 
 if (virCommandRun(cmd, NULL) < 0)
-goto cleanup;
+return -1;
 
-ret = 0;
-
- cleanup:
-virCommandFree(cmd);
-
-return ret;
+return 0;
 }
 
 
@@ -362,14 +352,12 @@ virISCSIConnectionLogout(const char *portal,
 int
 virISCSIRescanLUNs(const char *session)
 {
-virCommandPtr cmd = virCommandNewArgList(ISCSIADM,
+VIR_AUTOPTR(virCommand) cmd = virCommandNewArgList(ISCSIADM,
  "--mode", "session",
  "-r", session,
  "-R",
  NULL);
-int ret = virCommandRun(cmd, NULL);
-virCommandFree(cmd);
-return ret;
+return virCommandRun(cmd, NULL);
 }
 
 
@@ -419,8 +407,7 @@ virISCSIScanTargetsInternal(const char *portal,
 in

[libvirt] [PATCH v1 09/32] util: socketaddr: use VIR_AUTOFREE instead of VIR_FREE for scalar types

2018-07-28 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOFREE macro for declaring scalar variables, majority
of the VIR_FREE calls can be dropped, which in turn leads to
getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virsocketaddr.c | 70 
 1 file changed, 29 insertions(+), 41 deletions(-)

diff --git a/src/util/virsocketaddr.c b/src/util/virsocketaddr.c
index 6b52a3d..eee725d 100644
--- a/src/util/virsocketaddr.c
+++ b/src/util/virsocketaddr.c
@@ -432,10 +432,10 @@ virSocketAddrFormatFull(const virSocketAddr *addr,
 if (withService) {
 if (virAsprintf(, VIR_LOOPBACK_IPV4_ADDR"%s0",
 separator ? separator : ":") < 0)
-goto error;
+return NULL;
 } else {
 if (VIR_STRDUP(addrstr, VIR_LOOPBACK_IPV4_ADDR) < 0)
-goto error;
+return NULL;
 }
 return addrstr;
 }
@@ -452,33 +452,27 @@ virSocketAddrFormatFull(const virSocketAddr *addr,
 }
 
 if (withService) {
-char *ipv6_host = NULL;
+VIR_AUTOFREE(char *) ipv6_host = NULL;
 /* sasl_new_client demands the socket address to be in an odd format:
  * a.b.c.d;port or e:f:g:h:i:j:k:l;port, so use square brackets for
  * IPv6 only if no separator is passed to the function
  */
 if (!separator && VIR_SOCKET_ADDR_FAMILY(addr) == AF_INET6) {
 if (virAsprintf(_host, "[%s]", host) < 0)
-goto error;
+return NULL;
 }
 
 if (virAsprintf(, "%s%s%s",
 ipv6_host ? ipv6_host : host,
 separator ? separator : ":", port) == -1) {
-VIR_FREE(ipv6_host);
-goto error;
+return NULL;
 }
-
-VIR_FREE(ipv6_host);
 } else {
 if (VIR_STRDUP(addrstr, host) < 0)
-goto error;
+return NULL;
 }
 
 return addrstr;
-
- error:
-return NULL;
 }
 
 
@@ -759,24 +753,26 @@ virSocketAddrGetRange(virSocketAddrPtr start, 
virSocketAddrPtr end,
 int ret = 0;
 size_t i;
 virSocketAddr netmask;
-char *startStr = NULL, *endStr = NULL, *netStr = NULL;
+VIR_AUTOFREE(char *) startStr = NULL;
+VIR_AUTOFREE(char *) endStr = NULL;
+VIR_AUTOFREE(char *) netStr = NULL;
 
 if (start == NULL || end == NULL) {
 virReportError(VIR_ERR_INTERNAL_ERROR,
_("NULL argument - %p %p"), start, end);
-goto error;
+return -1;
 }
 
 startStr = virSocketAddrFormat(start);
 endStr = virSocketAddrFormat(end);
 if (!startStr || !endStr)
-goto error; /*error already reported */
+return -1; /*error already reported */
 
 if (VIR_SOCKET_ADDR_FAMILY(start) != VIR_SOCKET_ADDR_FAMILY(end)) {
 virReportError(VIR_ERR_INTERNAL_ERROR,
_("mismatch of address family in range %s - %s"),
startStr, endStr);
-goto error;
+return -1;
 }
 
 if (network) {
@@ -784,14 +780,14 @@ virSocketAddrGetRange(virSocketAddrPtr start, 
virSocketAddrPtr end,
  * network the range should be within
  */
 if (!(netStr = virSocketAddrFormat(network)))
-goto error;
+return -1;
 
 if (VIR_SOCKET_ADDR_FAMILY(start) != VIR_SOCKET_ADDR_FAMILY(network)) {
 virReportError(VIR_ERR_INTERNAL_ERROR,
_("mismatch of address family in "
  "range %s - %s for network %s"),
startStr, endStr, netStr);
-goto error;
+return -1;
 }
 
 if (prefix < 0 ||
@@ -801,7 +797,7 @@ virSocketAddrGetRange(virSocketAddrPtr start, 
virSocketAddrPtr end,
_("bad prefix %d for network %s when "
  " checking range %s - %s"),
prefix, netStr, startStr, endStr);
-goto error;
+return -1;
 }
 
 /* both start and end of range need to be within network */
@@ -811,7 +807,7 @@ virSocketAddrGetRange(virSocketAddrPtr start, 
virSocketAddrPtr end,
_("range %s - %s is not entirely within "
  "network %s/%d"),
startStr, endStr, netStr, prefix);
-goto error;
+return -1;
 }
 
 if (VIR_SOCKET_ADDR_IS_FAMILY(start, AF_INET)) {
@@ -823,7 +819,7 @@ virSocketAddrGetRange(virSocketAddrPtr start, 
virSocketAddrPtr end,
_("failed to construct broadcast or network "
 

[libvirt] [PATCH v1 04/32] util: macaddr: define cleanup function using VIR_DEFINE_AUTOPTR_FUNC

2018-07-28 Thread Sukrit Bhatnagar
Using the new VIR_DEFINE_AUTOPTR_FUNC macro defined in
src/util/viralloc.h, define a new wrapper around an existing
cleanup function which will be called when a variable declared
with VIR_AUTOPTR macro goes out of scope. Also, drop the redundant
viralloc.h include, since that has moved from the source module into
the header.

When a variable of type virMacAddrPtr is declared using VIR_AUTOPTR,
the function virMacAddrFree will be run automatically on it when it
goes out of scope.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virmacaddr.c | 6 ++
 src/util/virmacaddr.h | 4 
 2 files changed, 10 insertions(+)

diff --git a/src/util/virmacaddr.c b/src/util/virmacaddr.c
index 7afe032..e739775 100644
--- a/src/util/virmacaddr.c
+++ b/src/util/virmacaddr.c
@@ -252,3 +252,9 @@ virMacAddrIsBroadcastRaw(const unsigned char 
s[VIR_MAC_BUFLEN])
 {
 return memcmp(virMacAddrBroadcastAddrRaw, s, sizeof(*s)) == 0;
 }
+
+void
+virMacAddrFree(virMacAddrPtr addr)
+{
+VIR_FREE(addr);
+}
diff --git a/src/util/virmacaddr.h b/src/util/virmacaddr.h
index d0dd4a4..39dd51b 100644
--- a/src/util/virmacaddr.h
+++ b/src/util/virmacaddr.h
@@ -25,6 +25,7 @@
 # define __VIR_MACADDR_H__
 
 # include "internal.h"
+# include "viralloc.h"
 
 # define VIR_MAC_BUFLEN 6
 # define VIR_MAC_HEXLEN (VIR_MAC_BUFLEN * 2)
@@ -64,5 +65,8 @@ int virMacAddrParseHex(const char* str,
 bool virMacAddrIsUnicast(const virMacAddr *addr);
 bool virMacAddrIsMulticast(const virMacAddr *addr);
 bool virMacAddrIsBroadcastRaw(const unsigned char s[VIR_MAC_BUFLEN]);
+void virMacAddrFree(virMacAddrPtr addr);
+
+VIR_DEFINE_AUTOPTR_FUNC(virMacAddr, virMacAddrFree)
 
 #endif /* __VIR_MACADDR_H__ */
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v1 05/32] util: netdev: define cleanup function using VIR_DEFINE_AUTOPTR_FUNC

2018-07-28 Thread Sukrit Bhatnagar
Using the new VIR_DEFINE_AUTOPTR_FUNC macro defined in
src/util/viralloc.h, define a new wrapper around an existing
cleanup function which will be called when a variable declared
with VIR_AUTOPTR macro goes out of scope. Also, drop the redundant
viralloc.h include, since that has moved from the source module into
the header.

When variables of type virNetDevRxFilterPtr and virNetDevMcastEntryPtr
are declared using VIR_AUTOPTR, the functions virNetDevRxFilterFree
and virNetDevMcastEntryFree, respectively, will be run
automatically on them when they go out of scope.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virnetdev.c | 9 -
 src/util/virnetdev.h | 4 
 2 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/src/util/virnetdev.c b/src/util/virnetdev.c
index 0777eca..9eca786 100644
--- a/src/util/virnetdev.c
+++ b/src/util/virnetdev.c
@@ -29,7 +29,6 @@
 #include "virfile.h"
 #include "virerror.h"
 #include "vircommand.h"
-#include "viralloc.h"
 #include "virpci.h"
 #include "virlog.h"
 #include "virstring.h"
@@ -120,6 +119,14 @@ struct _virNetDevMcastEntry  {
 virMacAddr macaddr;
 };
 
+static void
+virNetDevMcastEntryFree(virNetDevMcastEntryPtr entry)
+{
+VIR_FREE(entry);
+}
+
+VIR_DEFINE_AUTOPTR_FUNC(virNetDevMcastEntry, virNetDevMcastEntryFree)
+
 typedef struct _virNetDevMcastList virNetDevMcastList;
 typedef virNetDevMcastList *virNetDevMcastListPtr;
 struct _virNetDevMcastList {
diff --git a/src/util/virnetdev.h b/src/util/virnetdev.h
index 71eaf45..8860ea1 100644
--- a/src/util/virnetdev.h
+++ b/src/util/virnetdev.h
@@ -30,6 +30,7 @@
 # include "virmacaddr.h"
 # include "virpci.h"
 # include "virnetdevvlan.h"
+# include "viralloc.h"
 
 # ifdef HAVE_STRUCT_IFREQ
 typedef struct ifreq virIfreq;
@@ -313,4 +314,7 @@ int virNetDevSysfsFile(char **pf_sysfs_device_link,
 
 int virNetDevRunEthernetScript(const char *ifname, const char *script)
 ATTRIBUTE_NOINLINE;
+
+VIR_DEFINE_AUTOPTR_FUNC(virNetDevRxFilter, virNetDevRxFilterFree)
+
 #endif /* __VIR_NETDEV_H__ */
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v1 15/32] util: netdevmacvlan: use VIR_AUTOFREE instead of VIR_FREE for scalar types

2018-07-28 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOFREE macro for declaring scalar variables, majority
of the VIR_FREE calls can be dropped, which in turn leads to
getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virnetdevmacvlan.c | 6 ++
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/src/util/virnetdevmacvlan.c b/src/util/virnetdevmacvlan.c
index 91c6244..a2ed65c 100644
--- a/src/util/virnetdevmacvlan.c
+++ b/src/util/virnetdevmacvlan.c
@@ -308,7 +308,7 @@ virNetDevMacVLanCreate(const char *ifname,
int *retry)
 {
 int rc = -1;
-struct nlmsghdr *resp = NULL;
+VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
 struct nlmsgerr *err;
 struct ifinfomsg ifinfo = { .ifi_family = AF_UNSPEC };
 int ifindex;
@@ -403,7 +403,6 @@ virNetDevMacVLanCreate(const char *ifname,
 rc = 0;
  cleanup:
 nlmsg_free(nl_msg);
-VIR_FREE(resp);
 return rc;
 
  malformed_resp:
@@ -452,7 +451,7 @@ virNetDevMacVLanTapOpen(const char *ifname,
 {
 int ret = -1;
 int ifindex;
-char *tapname = NULL;
+VIR_AUTOFREE(char *) tapname = NULL;
 size_t i = 0;
 
 if (virNetDevGetIndex(ifname, ) < 0)
@@ -487,7 +486,6 @@ virNetDevMacVLanTapOpen(const char *ifname,
 while (i--)
 VIR_FORCE_CLOSE(tapfd[i]);
 }
-VIR_FREE(tapname);
 return ret;
 }
 
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v1 00/32] use GNU C's cleanup attribute in src/util (batch III)

2018-07-28 Thread Sukrit Bhatnagar
This third series of patches also modifies a few files in src/util
to use VIR_AUTOFREE and VIR_AUTOPTR for automatic freeing of memory
and get rid of some VIR_FREE macro invocations and *Free function
calls.

Sukrit Bhatnagar (32):
  util: iscsi: use VIR_AUTOFREE instead of VIR_FREE for scalar types
  util: iscsi: use VIR_AUTOPTR for aggregate types
  util: netdevbridge: use VIR_AUTOFREE instead of VIR_FREE for scalar
types
  util: macaddr: define cleanup function using VIR_DEFINE_AUTOPTR_FUNC
  util: netdev: define cleanup function using VIR_DEFINE_AUTOPTR_FUNC
  util: netdev: use VIR_AUTOFREE instead of VIR_FREE for scalar types
  util: netdev: use VIR_AUTOPTR for aggregate types
  util: socketaddr: define cleanup function using
VIR_DEFINE_AUTOPTR_FUNC
  util: socketaddr: use VIR_AUTOFREE instead of VIR_FREE for scalar
types
  util: socketaddr: use VIR_AUTOPTR for aggregate types
  util: netdevip: define cleanup function using VIR_DEFINE_AUTOPTR_FUNC
  util: netdevip: use VIR_AUTOFREE instead of VIR_FREE for scalar types
  util: netdevip: use VIR_AUTOPTR for aggregate types
  util: netdevmacvlan: define cleanup function using
VIR_DEFINE_AUTOPTR_FUNC
  util: netdevmacvlan: use VIR_AUTOFREE instead of VIR_FREE for scalar
types
  util: netdevmacvlan: use VIR_AUTOPTR for aggregate types
  util: netdevopenvswitch: use VIR_AUTOFREE instead of VIR_FREE for
scalar types
  util: netdevopenvswitch: use VIR_AUTOPTR for aggregate types
  util: netdevtap: use VIR_AUTOFREE instead of VIR_FREE for scalar types
  util: netdevveth: use VIR_AUTOFREE instead of VIR_FREE for scalar
types
  util: netdevveth: use VIR_AUTOPTR for aggregate types
  util: netlink: define cleanup function using VIR_DEFINE_AUTOPTR_FUNC
  util: netlink: use VIR_AUTOPTR for aggregate types
  util: numa: use VIR_AUTOFREE instead of VIR_FREE for scalar types
  util: numa: use VIR_AUTOPTR for aggregate types
  util: perf: define cleanup function using VIR_DEFINE_AUTOPTR_FUNC
  util: perf: use VIR_AUTOFREE instead of VIR_FREE for scalar types
  util: pidfile: use VIR_AUTOFREE instead of VIR_FREE for scalar types
  util: process: use VIR_AUTOFREE instead of VIR_FREE for scalar types
  util: process: use VIR_AUTOPTR for aggregate types
  util: qemu: use VIR_AUTOFREE instead of VIR_FREE for scalar types
  util: qemu: use VIR_AUTOPTR for aggregate types

 src/util/viriscsi.c |  99 --
 src/util/virmacaddr.c   |   6 +
 src/util/virmacaddr.h   |   4 +
 src/util/virnetdev.c| 412 +++-
 src/util/virnetdev.h|   4 +
 src/util/virnetdevbridge.c  |  45 ++---
 src/util/virnetdevip.c  | 157 ++-
 src/util/virnetdevip.h  |   4 +
 src/util/virnetdevmacvlan.c |  41 ++--
 src/util/virnetdevopenvswitch.c | 131 +
 src/util/virnetdevtap.c |  11 +-
 src/util/virnetdevveth.c|  33 ++--
 src/util/virnetlink.c   |  62 +++---
 src/util/virnuma.c  | 107 ---
 src/util/virperf.c  |  20 +-
 src/util/virperf.h  |   8 +-
 src/util/virpidfile.c   | 185 ++
 src/util/virprocess.c   |  68 +++
 src/util/virqemu.c  |  50 ++---
 src/util/virsocketaddr.c| 113 +--
 src/util/virsocketaddr.h|   9 +-
 21 files changed, 610 insertions(+), 959 deletions(-)

-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v2 40/41] util: lease: use VIR_AUTOFREE instead of VIR_FREE for scalar types

2018-07-26 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOFREE macro for declaring scalar variables, majority
of the VIR_FREE calls can be dropped, which in turn leads to
getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
Reviewed-by: Erik Skultety 
---
 src/util/virlease.c | 6 ++
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/src/util/virlease.c b/src/util/virlease.c
index b49105d..baaceaf 100644
--- a/src/util/virlease.c
+++ b/src/util/virlease.c
@@ -55,7 +55,7 @@ virLeaseReadCustomLeaseFile(virJSONValuePtr leases_array_new,
 const char *ip_to_delete,
 char **server_duid)
 {
-char *lease_entries = NULL;
+VIR_AUTOFREE(char *) lease_entries = NULL;
 virJSONValuePtr leases_array = NULL;
 long long expirytime;
 int custom_lease_file_len = 0;
@@ -146,7 +146,6 @@ virLeaseReadCustomLeaseFile(virJSONValuePtr 
leases_array_new,
 
  cleanup:
 virJSONValueFree(leases_array);
-VIR_FREE(lease_entries);
 return ret;
 }
 
@@ -235,7 +234,7 @@ virLeaseNew(virJSONValuePtr *lease_ret,
 virJSONValuePtr lease_new = NULL;
 const char *exptime_tmp = virGetEnvAllowSUID("DNSMASQ_LEASE_EXPIRES");
 long long expirytime = 0;
-char *exptime = NULL;
+VIR_AUTOFREE(char *) exptime = NULL;
 int ret = -1;
 
 /* In case hostname is still unknown, use the last known one */
@@ -291,7 +290,6 @@ virLeaseNew(virJSONValuePtr *lease_ret,
 *lease_ret = lease_new;
 lease_new = NULL;
  cleanup:
-VIR_FREE(exptime);
 virJSONValueFree(lease_new);
 return ret;
 }
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v2 32/41] util: hostdev: use VIR_AUTOFREE instead of VIR_FREE for scalar types

2018-07-26 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOFREE macro for declaring scalar variables, majority
of the VIR_FREE calls can be dropped, which in turn leads to
getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
Reviewed-by: Erik Skultety 
---
 src/util/virhostdev.c | 91 +--
 1 file changed, 30 insertions(+), 61 deletions(-)

diff --git a/src/util/virhostdev.c b/src/util/virhostdev.c
index d5075ac..492c42f 100644
--- a/src/util/virhostdev.c
+++ b/src/util/virhostdev.c
@@ -186,17 +186,14 @@ virHostdevManagerNew(void)
 goto error;
 }
 } else {
-char *rundir = NULL;
+VIR_AUTOFREE(char *) rundir = NULL;
 mode_t old_umask;
 
 if (!(rundir = virGetUserRuntimeDirectory()))
 goto error;
 
-if (virAsprintf(>stateDir, "%s/hostdevmgr", rundir) < 0) {
-VIR_FREE(rundir);
+if (virAsprintf(>stateDir, "%s/hostdevmgr", rundir) < 0)
 goto error;
-}
-VIR_FREE(rundir);
 
 old_umask = umask(077);
 
@@ -289,17 +286,12 @@ virHostdevPCISysfsPath(virDomainHostdevDefPtr hostdev,
 static int
 virHostdevIsVirtualFunction(virDomainHostdevDefPtr hostdev)
 {
-char *sysfs_path = NULL;
-int ret = -1;
+VIR_AUTOFREE(char *) sysfs_path = NULL;
 
 if (virHostdevPCISysfsPath(hostdev, _path) < 0)
-return ret;
+return -1;
 
-ret = virPCIIsVirtualFunction(sysfs_path);
-
-VIR_FREE(sysfs_path);
-
-return ret;
+return virPCIIsVirtualFunction(sysfs_path);
 }
 
 
@@ -309,17 +301,15 @@ virHostdevNetDevice(virDomainHostdevDefPtr hostdev,
 char **linkdev,
 int *vf)
 {
-int ret = -1;
-char *sysfs_path = NULL;
+VIR_AUTOFREE(char *) sysfs_path = NULL;
 
 if (virHostdevPCISysfsPath(hostdev, _path) < 0)
-return ret;
+return -1;
 
 if (virPCIIsVirtualFunction(sysfs_path) == 1) {
 if (virPCIGetVirtualFunctionInfo(sysfs_path, pfNetDevIdx,
- linkdev, vf) < 0) {
-goto cleanup;
-}
+ linkdev, vf) < 0)
+return -1;
 } else {
 /* In practice this should never happen, since we currently
  * only support assigning SRIOV VFs via parent.data.net);
 virtPort = virDomainNetGetActualVirtPortProfile(hostdev->parent.data.net);
@@ -507,24 +487,19 @@ virHostdevSetNetConfig(virDomainHostdevDefPtr hostdev,
_("direct setting of the vlan tag is not allowed "
  "for hostdev devices using %s mode"),
virNetDevVPortTypeToString(virtPort->virtPortType));
-goto cleanup;
+return -1;
 }
 if (virHostdevNetConfigVirtPortProfile(linkdev, vf, virtPort,
>parent.data.net->mac,
-   uuid, port_profile_associate) < 
0) {
-goto cleanup;
-}
+   uuid, port_profile_associate) < 
0)
+return -1;
 } else {
 if (virNetDevSetNetConfig(linkdev, vf, >parent.data.net->mac,
-  vlan, NULL, true) < 0) {
-goto cleanup;
-}
+  vlan, NULL, true) < 0)
+return -1;
 }
 
-ret = 0;
- cleanup:
-VIR_FREE(linkdev);
-return ret;
+return 0;
 }
 
 
@@ -540,13 +515,13 @@ virHostdevRestoreNetConfig(virDomainHostdevDefPtr hostdev,
const char *stateDir,
const char *oldStateDir)
 {
-char *linkdev = NULL;
+VIR_AUTOFREE(char *) linkdev = NULL;
+VIR_AUTOFREE(virMacAddrPtr) MAC = NULL;
+VIR_AUTOFREE(virMacAddrPtr) adminMAC = NULL;
 virNetDevVPortProfilePtr virtPort;
 int ret = -1;
 int vf = -1;
 bool port_profile_associate = false;
-virMacAddrPtr MAC = NULL;
-virMacAddrPtr adminMAC = NULL;
 virNetDevVlanPtr vlan = NULL;
 
 
@@ -656,9 +631,6 @@ virHostdevRestoreNetConfig(virDomainHostdevDefPtr hostdev,
 }
 
  cleanup:
-VIR_FREE(linkdev);
-VIR_FREE(MAC);
-VIR_FREE(adminMAC);
 virNetDevVlanFree(vlan);
 
 return ret;
@@ -763,8 +735,8 @@ virHostdevPreparePCIDevices(virHostdevManagerPtr mgr,
mgr->inactivePCIHostdevs) < 0)
 goto reattachdevs;
 } else {
-char *driverPath;
-char *driverName;
+VIR_AUTOFREE(char *) driverPath = NULL;
+VIR_AUTOFREE(char *) driverName = NULL;
 int stub;
 
 /* Unmanaged devices should already have been marked as
@@ -790,9 +762,6 @@ virHostdevPrepare

[libvirt] [PATCH v2 41/41] util: lease: use VIR_AUTOPTR for aggregate types

2018-07-26 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOPTR macro for declaring aggregate pointer variables,
majority of the calls to *Free functions can be dropped, which
in turn leads to getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
Reviewed-by: Erik Skultety 
---
 src/util/virlease.c | 76 -
 1 file changed, 29 insertions(+), 47 deletions(-)

diff --git a/src/util/virlease.c b/src/util/virlease.c
index baaceaf..7c6c37e 100644
--- a/src/util/virlease.c
+++ b/src/util/virlease.c
@@ -56,40 +56,36 @@ virLeaseReadCustomLeaseFile(virJSONValuePtr 
leases_array_new,
 char **server_duid)
 {
 VIR_AUTOFREE(char *) lease_entries = NULL;
-virJSONValuePtr leases_array = NULL;
+VIR_AUTOPTR(virJSONValue) leases_array = NULL;
 long long expirytime;
 int custom_lease_file_len = 0;
 virJSONValuePtr lease_tmp = NULL;
 const char *ip_tmp = NULL;
 const char *server_duid_tmp = NULL;
 size_t i;
-int ret = -1;
 
 /* Read entire contents */
 if ((custom_lease_file_len = virFileReadAll(custom_lease_file,
 
VIR_NETWORK_DHCP_LEASE_FILE_SIZE_MAX,
 _entries)) < 0) {
-goto cleanup;
+return -1;
 }
 
 /* Check for previous leases */
-if (custom_lease_file_len == 0) {
-ret = 0;
-goto cleanup;
-}
+if (custom_lease_file_len == 0)
+return 0;
 
 if (!(leases_array = virJSONValueFromString(lease_entries))) {
 virReportError(VIR_ERR_INTERNAL_ERROR,
_("invalid json in file: %s, rewriting it"),
custom_lease_file);
-ret = 0;
-goto cleanup;
+return 0;
 }
 
 if (!virJSONValueIsArray(leases_array)) {
 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("couldn't fetch array of leases"));
-goto cleanup;
+return -1;
 }
 
 i = 0;
@@ -97,14 +93,14 @@ virLeaseReadCustomLeaseFile(virJSONValuePtr 
leases_array_new,
 if (!(lease_tmp = virJSONValueArrayGet(leases_array, i))) {
 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("failed to parse json"));
-goto cleanup;
+return -1;
 }
 
 if (!(ip_tmp = virJSONValueObjectGetString(lease_tmp, "ip-address")) ||
 (virJSONValueObjectGetNumberLong(lease_tmp, "expiry-time", 
) < 0)) {
 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("failed to parse json"));
-goto cleanup;
+return -1;
 }
 
 /* Check whether lease has to be included or not */
@@ -121,14 +117,14 @@ virLeaseReadCustomLeaseFile(virJSONValuePtr 
leases_array_new,
 /* Control reaches here when the 'action' is not for an
  * ipv6 lease or, for some weird reason the env var
  * DNSMASQ_SERVER_DUID wasn't set*/
-goto cleanup;
+return -1;
 }
 } else {
 /* Inject server-duid into those ipv6 leases which
  * didn't have it previously, for example, those
  * created by leaseshelper from libvirt 1.2.6 */
 if (virJSONValueObjectAppendString(lease_tmp, "server-duid", 
*server_duid) < 0)
-goto cleanup;
+return -1;
 }
 }
 
@@ -136,17 +132,13 @@ virLeaseReadCustomLeaseFile(virJSONValuePtr 
leases_array_new,
 if (virJSONValueArrayAppend(leases_array_new, lease_tmp) < 0) {
 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("failed to create json"));
-goto cleanup;
+return -1;
 }
 
 ignore_value(virJSONValueArraySteal(leases_array, i));
 }
 
-ret = 0;
-
- cleanup:
-virJSONValueFree(leases_array);
-return ret;
+return 0;
 }
 
 
@@ -157,7 +149,6 @@ virLeasePrintLeases(virJSONValuePtr leases_array_new,
 virJSONValuePtr lease_tmp = NULL;
 const char *ip_tmp = NULL;
 long long expirytime = 0;
-int ret = -1;
 size_t i;
 
 /* Man page of dnsmasq says: the script (helper program, in our case)
@@ -174,7 +165,7 @@ virLeasePrintLeases(virJSONValuePtr leases_array_new,
 if (!(ip_tmp = virJSONValueObjectGetString(lease_tmp, "ip-address"))) {
 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("failed to parse json"));
-goto cleanup;
+return -1;
 }
 if (!strchr(ip_tmp, ':')) {
 if (virJSONValueObjectGetNumberLong(lease_tmp

[libvirt] [PATCH v2 22/41] util: usb: modify virUSBDeviceListAdd to take double pointer

2018-07-26 Thread Sukrit Bhatnagar
Modify virUSBDeviceListAdd to take a double pointer to
virUSBDevicePtr as the second argument. This will enable usage
of cleanup macros upon the virUSBDevicePtr item which is to be
added to the list as it will be cleared by virInsertElementsN
upon success.

Signed-off-by: Sukrit Bhatnagar 
---
 src/util/virhostdev.c |  6 +++---
 src/util/virusb.c | 10 +-
 src/util/virusb.h |  2 +-
 tests/virusbtest.c|  4 ++--
 4 files changed, 11 insertions(+), 11 deletions(-)

diff --git a/src/util/virhostdev.c b/src/util/virhostdev.c
index f4bd19d..d5075ac 100644
--- a/src/util/virhostdev.c
+++ b/src/util/virhostdev.c
@@ -1236,7 +1236,7 @@ virHostdevUpdateActiveUSBDevices(virHostdevManagerPtr mgr,
 
 virUSBDeviceSetUsedBy(usb, drv_name, dom_name);
 
-if (virUSBDeviceListAdd(mgr->activeUSBHostdevs, usb) < 0) {
+if (virUSBDeviceListAdd(mgr->activeUSBHostdevs, ) < 0) {
 virUSBDeviceFree(usb);
 goto cleanup;
 }
@@ -1406,7 +1406,7 @@ virHostdevMarkUSBDevices(virHostdevManagerPtr mgr,
  * from the virUSBDeviceList that passed in on success,
  * perform rollback on failure.
  */
-if (virUSBDeviceListAdd(mgr->activeUSBHostdevs, usb) < 0)
+if (virUSBDeviceListAdd(mgr->activeUSBHostdevs, ) < 0)
 goto error;
 }
 
@@ -1555,7 +1555,7 @@ virHostdevPrepareUSBDevices(virHostdevManagerPtr mgr,
 if (virHostdevFindUSBDevice(hostdev, required, ) < 0)
 goto cleanup;
 
-if (usb && virUSBDeviceListAdd(list, usb) < 0) {
+if (usb && virUSBDeviceListAdd(list, ) < 0) {
 virUSBDeviceFree(usb);
 goto cleanup;
 }
diff --git a/src/util/virusb.c b/src/util/virusb.c
index 2fe1bfc..7818232 100644
--- a/src/util/virusb.c
+++ b/src/util/virusb.c
@@ -181,7 +181,7 @@ virUSBDeviceSearch(unsigned int vendor,
 if (!usb)
 goto cleanup;
 
-if (virUSBDeviceListAdd(list, usb) < 0) {
+if (virUSBDeviceListAdd(list, ) < 0) {
 virUSBDeviceFree(usb);
 goto cleanup;
 }
@@ -463,15 +463,15 @@ virUSBDeviceListDispose(void *obj)
 
 int
 virUSBDeviceListAdd(virUSBDeviceListPtr list,
-virUSBDevicePtr dev)
+virUSBDevicePtr *dev)
 {
-if (virUSBDeviceListFind(list, dev)) {
+if (virUSBDeviceListFind(list, *dev)) {
 virReportError(VIR_ERR_INTERNAL_ERROR,
_("Device %s is already in use"),
-   dev->name);
+   (*dev)->name);
 return -1;
 }
-return VIR_APPEND_ELEMENT(list->devs, list->count, dev);
+return VIR_APPEND_ELEMENT(list->devs, list->count, *dev);
 }
 
 virUSBDevicePtr
diff --git a/src/util/virusb.h b/src/util/virusb.h
index 716e8c6..078dee6 100644
--- a/src/util/virusb.h
+++ b/src/util/virusb.h
@@ -88,7 +88,7 @@ int virUSBDeviceFileIterate(virUSBDevicePtr dev,
 
 virUSBDeviceListPtr virUSBDeviceListNew(void);
 int virUSBDeviceListAdd(virUSBDeviceListPtr list,
-virUSBDevicePtr dev);
+virUSBDevicePtr *dev);
 virUSBDevicePtr virUSBDeviceListGet(virUSBDeviceListPtr list,
 int idx);
 size_t virUSBDeviceListCount(virUSBDeviceListPtr list);
diff --git a/tests/virusbtest.c b/tests/virusbtest.c
index 8728fe9..05bba2b 100644
--- a/tests/virusbtest.c
+++ b/tests/virusbtest.c
@@ -173,7 +173,7 @@ testUSBList(const void *opaque ATTRIBUTE_UNUSED)
 dev = virUSBDeviceListGet(devlist, 0);
 dev = virUSBDeviceListSteal(devlist, dev);
 
-if (virUSBDeviceListAdd(list, dev) < 0)
+if (virUSBDeviceListAdd(list, ) < 0)
 goto cleanup;
 dev = NULL;
 }
@@ -196,7 +196,7 @@ testUSBList(const void *opaque ATTRIBUTE_UNUSED)
 dev = virUSBDeviceListGet(devlist, 0);
 dev = virUSBDeviceListSteal(devlist, dev);
 
-if (virUSBDeviceListAdd(list, dev) < 0)
+if (virUSBDeviceListAdd(list, ) < 0)
 goto cleanup;
 dev = NULL;
 }
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v2 36/41] util: iscsi: use VIR_AUTOFREE instead of VIR_FREE for scalar types

2018-07-26 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOFREE macro for declaring scalar variables, majority
of the VIR_FREE calls can be dropped, which in turn leads to
getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
Reviewed-by: Erik Skultety 
---
 src/util/viriscsi.c | 21 +
 1 file changed, 9 insertions(+), 12 deletions(-)

diff --git a/src/util/viriscsi.c b/src/util/viriscsi.c
index d4c745a..13fd02c 100644
--- a/src/util/viriscsi.c
+++ b/src/util/viriscsi.c
@@ -80,7 +80,7 @@ virISCSIGetSession(const char *devpath,
 .session = NULL,
 .devpath = devpath,
 };
-char *error = NULL;
+VIR_AUTOFREE(char *) error = NULL;
 int exitstatus = 0;
 
 virCommandPtr cmd = virCommandNewArgList(ISCSIADM, "--mode",
@@ -101,7 +101,6 @@ virISCSIGetSession(const char *devpath,
NULLSTR(error));
 
  cleanup:
-VIR_FREE(error);
 virCommandFree(cmd);
 return cbdata.session;
 }
@@ -120,7 +119,10 @@ virStorageBackendIQNFound(const char *initiatoriqn,
 int ret = IQN_MISSING, fd = -1;
 char ebuf[64];
 FILE *fp = NULL;
-char *line = NULL, *newline = NULL, *iqn = NULL, *token = NULL;
+VIR_AUTOFREE(char *) line = NULL;
+char *newline = NULL;
+char *iqn = NULL;
+char *token = NULL;
 virCommandPtr cmd = virCommandNewArgList(ISCSIADM,
  "--mode", "iface", NULL);
 
@@ -192,7 +194,6 @@ virStorageBackendIQNFound(const char *initiatoriqn,
 if (ret == IQN_MISSING)
 VIR_DEBUG("Could not find interface with IQN '%s'", iqn);
 
-VIR_FREE(line);
 VIR_FORCE_FCLOSE(fp);
 VIR_FORCE_CLOSE(fd);
 virCommandFree(cmd);
@@ -206,7 +207,7 @@ virStorageBackendCreateIfaceIQN(const char *initiatoriqn,
 char **ifacename)
 {
 int ret = -1, exitstatus = -1;
-char *temp_ifacename;
+VIR_AUTOFREE(char *) temp_ifacename = NULL;
 virCommandPtr cmd = NULL;
 
 if (virAsprintf(_ifacename,
@@ -267,7 +268,6 @@ virStorageBackendCreateIfaceIQN(const char *initiatoriqn,
 
  cleanup:
 virCommandFree(cmd);
-VIR_FREE(temp_ifacename);
 if (ret != 0)
 VIR_FREE(*ifacename);
 return ret;
@@ -289,7 +289,7 @@ virISCSIConnection(const char *portal,
 NULL
 };
 virCommandPtr cmd;
-char *ifacename = NULL;
+VIR_AUTOFREE(char *) ifacename = NULL;
 
 cmd = virCommandNewArgs(baseargv);
 virCommandAddArgSet(cmd, extraargv);
@@ -326,7 +326,6 @@ virISCSIConnection(const char *portal,
 
  cleanup:
 virCommandFree(cmd);
-VIR_FREE(ifacename);
 
 return ret;
 }
@@ -377,15 +376,13 @@ virISCSIGetTargets(char **const groups,
void *data)
 {
 struct virISCSITargetList *list = data;
-char *target;
+VIR_AUTOFREE(char *) target = NULL;
 
 if (VIR_STRDUP(target, groups[1]) < 0)
 return -1;
 
-if (VIR_APPEND_ELEMENT(list->targets, list->ntargets, target) < 0) {
-VIR_FREE(target);
+if (VIR_APPEND_ELEMENT(list->targets, list->ntargets, target) < 0)
 return -1;
-}
 
 return 0;
 }
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v2 39/41] util: kmod: use VIR_AUTOPTR for aggregate types

2018-07-26 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOPTR macro for declaring aggregate pointer variables,
majority of the calls to *Free functions can be dropped, which
in turn leads to getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
Reviewed-by: Erik Skultety 
---
 src/util/virkmod.c | 22 ++
 1 file changed, 6 insertions(+), 16 deletions(-)

diff --git a/src/util/virkmod.c b/src/util/virkmod.c
index d981cd4..9d0375b 100644
--- a/src/util/virkmod.c
+++ b/src/util/virkmod.c
@@ -28,8 +28,7 @@
 static int
 doModprobe(const char *opts, const char *module, char **outbuf, char **errbuf)
 {
-int ret = -1;
-virCommandPtr cmd = NULL;
+VIR_AUTOPTR(virCommand) cmd = NULL;
 
 cmd = virCommandNew(MODPROBE);
 if (opts)
@@ -42,32 +41,23 @@ doModprobe(const char *opts, const char *module, char 
**outbuf, char **errbuf)
 virCommandSetErrorBuffer(cmd, errbuf);
 
 if (virCommandRun(cmd, NULL) < 0)
-goto cleanup;
+return -1;
 
-ret = 0;
-
- cleanup:
-virCommandFree(cmd);
-return ret;
+return 0;
 }
 
 static int
 doRmmod(const char *module, char **errbuf)
 {
-int ret = -1;
-virCommandPtr cmd = NULL;
+VIR_AUTOPTR(virCommand) cmd = NULL;
 
 cmd = virCommandNewArgList(RMMOD, module, NULL);
 virCommandSetErrorBuffer(cmd, errbuf);
 
 if (virCommandRun(cmd, NULL) < 0)
-goto cleanup;
+return -1;
 
-ret = 0;
-
- cleanup:
-virCommandFree(cmd);
-return ret;
+return 0;
 }
 
 /**
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v2 38/41] util: kmod: use VIR_AUTOFREE instead of VIR_FREE for scalar types

2018-07-26 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOFREE macro for declaring scalar variables, majority
of the VIR_FREE calls can be dropped, which in turn leads to
getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
Reviewed-by: Erik Skultety 
---
 src/util/virkmod.c | 16 ++--
 1 file changed, 6 insertions(+), 10 deletions(-)

diff --git a/src/util/virkmod.c b/src/util/virkmod.c
index 219fad6..d981cd4 100644
--- a/src/util/virkmod.c
+++ b/src/util/virkmod.c
@@ -155,13 +155,12 @@ virKModUnload(const char *module)
 bool
 virKModIsBlacklisted(const char *module)
 {
-bool retval = false;
 size_t i;
-char *drvblklst = NULL;
-char *outbuf = NULL;
+VIR_AUTOFREE(char *) drvblklst = NULL;
+VIR_AUTOFREE(char *) outbuf = NULL;
 
 if (virAsprintfQuiet(, "blacklist %s\n", module) < 0)
-goto cleanup;
+return false;
 
 /* modprobe will convert all '-' into '_', so we need to as well */
 for (i = 0; i < drvblklst[i]; i++)
@@ -169,13 +168,10 @@ virKModIsBlacklisted(const char *module)
 drvblklst[i] = '_';
 
 if (doModprobe("-c", NULL, , NULL) < 0)
-goto cleanup;
+return false;
 
 if (strstr(outbuf, drvblklst))
-retval = true;
+return true;
 
- cleanup:
-VIR_FREE(drvblklst);
-VIR_FREE(outbuf);
-return retval;
+return false;
 }
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v2 16/41] util: firewall: use VIR_AUTOPTR for aggregate types

2018-07-26 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOPTR macro for declaring aggregate pointer variables,
majority of the calls to *Free functions can be dropped, which
in turn leads to getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
Reviewed-by: Erik Skultety 
---
 src/util/virfirewall.c | 36 +---
 1 file changed, 13 insertions(+), 23 deletions(-)

diff --git a/src/util/virfirewall.c b/src/util/virfirewall.c
index b4a4d06..c786d76 100644
--- a/src/util/virfirewall.c
+++ b/src/util/virfirewall.c
@@ -119,14 +119,13 @@ virFirewallCheckUpdateLock(bool *lockflag,
const char *const*args)
 {
 int status; /* Ignore failed commands without logging them */
-virCommandPtr cmd = virCommandNewArgs(args);
+VIR_AUTOPTR(virCommand) cmd = virCommandNewArgs(args);
 if (virCommandRun(cmd, ) < 0 || status) {
 VIR_INFO("locking not supported by %s", args[0]);
 } else {
 VIR_INFO("using locking for %s", args[0]);
 *lockflag = true;
 }
-virCommandFree(cmd);
 }
 
 static void
@@ -673,16 +672,15 @@ virFirewallApplyRuleDirect(virFirewallRulePtr rule,
 {
 size_t i;
 const char *bin = virFirewallLayerCommandTypeToString(rule->layer);
-virCommandPtr cmd = NULL;
+VIR_AUTOPTR(virCommand) cmd = NULL;
 int status;
-int ret = -1;
 VIR_AUTOFREE(char *) error = NULL;
 
 if (!bin) {
 virReportError(VIR_ERR_INTERNAL_ERROR,
_("Unknown firewall layer %d"),
rule->layer);
-goto cleanup;
+return -1;
 }
 
 cmd = virCommandNewArgList(bin, NULL);
@@ -694,7 +692,7 @@ virFirewallApplyRuleDirect(virFirewallRulePtr rule,
 virCommandSetErrorBuffer(cmd, );
 
 if (virCommandRun(cmd, ) < 0)
-goto cleanup;
+return -1;
 
 if (status != 0) {
 if (ignoreErrors) {
@@ -705,14 +703,11 @@ virFirewallApplyRuleDirect(virFirewallRulePtr rule,
_("Failed to apply firewall rules %s: %s"),
NULLSTR(args), NULLSTR(error));
 VIR_FREE(*output);
-goto cleanup;
+return -1;
 }
 }
 
-ret = 0;
- cleanup:
-virCommandFree(cmd);
-return ret;
+return 0;
 }
 
 
@@ -805,8 +800,7 @@ virFirewallApplyRule(virFirewallPtr firewall,
 {
 VIR_AUTOFREE(char *) output = NULL;
 VIR_AUTOFREE(char *) str = virFirewallRuleToString(rule);
-char **lines = NULL;
-int ret = -1;
+VIR_AUTOPTR(virString) lines = NULL;
 VIR_INFO("Applying rule '%s'", NULLSTR(str));
 
 if (rule->ignoreErrors)
@@ -831,28 +825,25 @@ virFirewallApplyRule(virFirewallPtr firewall,
 
 if (rule->queryCB && output) {
 if (!(lines = virStringSplit(output, "\n", -1)))
-goto cleanup;
+return -1;
 
 VIR_DEBUG("Invoking query %p with '%s'", rule->queryCB, output);
 if (rule->queryCB(firewall, (const char *const *)lines, 
rule->queryOpaque) < 0)
-goto cleanup;
+return -1;
 
 if (firewall->err == ENOMEM) {
 virReportOOMError();
-goto cleanup;
+return -1;
 }
 if (firewall->err) {
 virReportSystemError(firewall->err, "%s",
  _("Unable to create rule"));
-goto cleanup;
+return -1;
 }
 
 }
 
-ret = 0;
- cleanup:
-virStringListFree(lines);
-return ret;
+return 0;
 }
 
 static int
@@ -926,7 +917,7 @@ virFirewallApply(virFirewallPtr firewall)
 if (virFirewallApplyGroup(firewall, i) < 0) {
 VIR_DEBUG("Rolling back groups up to %zu for %p", i, firewall);
 size_t first = i;
-virErrorPtr saved_error = virSaveLastError();
+VIR_AUTOPTR(virError) saved_error = virSaveLastError();
 
 /*
  * Look at any inheritance markers to figure out
@@ -947,7 +938,6 @@ virFirewallApply(virFirewallPtr firewall)
 }
 
 virSetError(saved_error);
-virFreeError(saved_error);
 VIR_DEBUG("Done rolling back groups for %p", firewall);
 goto cleanup;
 }
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v2 09/41] util: cgroup: use VIR_AUTOFREE instead of VIR_FREE for scalar types

2018-07-26 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOFREE macro for declaring scalar variables, majority
of the VIR_FREE calls can be dropped, which in turn leads to
getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
Reviewed-by: Erik Skultety 
---
 src/util/vircgroup.c | 528 ++-
 1 file changed, 181 insertions(+), 347 deletions(-)

diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c
index bc5f774..6f7b5b4 100644
--- a/src/util/vircgroup.c
+++ b/src/util/vircgroup.c
@@ -162,7 +162,7 @@ virCgroupPartitionNeedsEscaping(const char *path)
 {
 FILE *fp = NULL;
 int ret = 0;
-char *line = NULL;
+VIR_AUTOFREE(char *) line = NULL;
 size_t buflen;
 
 /* If it starts with 'cgroup.' or a '_' of any
@@ -222,7 +222,6 @@ virCgroupPartitionNeedsEscaping(const char *path)
 }
 
  cleanup:
-VIR_FREE(line);
 VIR_FORCE_FCLOSE(fp);
 return ret;
 }
@@ -255,41 +254,40 @@ virCgroupValidateMachineGroup(virCgroupPtr group,
   const char *machinename)
 {
 size_t i;
-bool valid = false;
-char *partname = NULL;
-char *scopename_old = NULL;
-char *scopename_new = NULL;
-char *partmachinename = NULL;
+VIR_AUTOFREE(char *) partname = NULL;
+VIR_AUTOFREE(char *) scopename_old = NULL;
+VIR_AUTOFREE(char *) scopename_new = NULL;
+VIR_AUTOFREE(char *) partmachinename = NULL;
 
 if (virAsprintf(, "%s.libvirt-%s",
 name, drivername) < 0)
-goto cleanup;
+return false;
 
 if (virCgroupPartitionEscape() < 0)
-goto cleanup;
+return false;
 
 if (machinename &&
 (virAsprintf(, "%s.libvirt-%s",
  machinename, drivername) < 0 ||
  virCgroupPartitionEscape() < 0))
-goto cleanup;
+return false;
 
 if (!(scopename_old = virSystemdMakeScopeName(name, drivername, true)))
-goto cleanup;
+return false;
 
 /* We should keep trying even if this failed */
 if (!machinename)
 virResetLastError();
 else if (!(scopename_new = virSystemdMakeScopeName(machinename,
drivername, false)))
-goto cleanup;
+return false;
 
 if (virCgroupPartitionEscape(_old) < 0)
-goto cleanup;
+return false;
 
 if (scopename_new &&
 virCgroupPartitionEscape(_new) < 0)
-goto cleanup;
+return false;
 
 for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) {
 char *tmp;
@@ -302,7 +300,7 @@ virCgroupValidateMachineGroup(virCgroupPtr group,
 
 tmp = strrchr(group->controllers[i].placement, '/');
 if (!tmp)
-goto cleanup;
+return false;
 
 if (stripEmulatorSuffix &&
 (i == VIR_CGROUP_CONTROLLER_CPU ||
@@ -312,7 +310,7 @@ virCgroupValidateMachineGroup(virCgroupPtr group,
 *tmp = '\0';
 tmp = strrchr(group->controllers[i].placement, '/');
 if (!tmp)
-goto cleanup;
+return false;
 }
 
 tmp++;
@@ -328,18 +326,11 @@ virCgroupValidateMachineGroup(virCgroupPtr group,
   tmp, virCgroupControllerTypeToString(i),
   name, NULLSTR(machinename), partname,
   scopename_old, NULLSTR(scopename_new));
-goto cleanup;
+return false;
 }
 }
 
-valid = true;
-
- cleanup:
-VIR_FREE(partmachinename);
-VIR_FREE(partname);
-VIR_FREE(scopename_old);
-VIR_FREE(scopename_new);
-return valid;
+return true;
 }
 
 
@@ -377,7 +368,6 @@ virCgroupDetectMountsFromFile(virCgroupPtr group,
 FILE *mounts = NULL;
 struct mntent entry;
 char buf[CGROUP_MAX_VAL];
-char *linksrc = NULL;
 int ret = -1;
 
 mounts = fopen(path, "r");
@@ -432,8 +422,9 @@ virCgroupDetectMountsFromFile(virCgroupPtr group,
 /* If it is a co-mount it has a filename like "cpu,cpuacct"
  * and we must identify the symlink path */
 if (checkLinks && strchr(tmp2 + 1, ',')) {
+VIR_AUTOFREE(char *) linksrc = NULL;
+
 *tmp2 = '\0';
-VIR_FREE(linksrc);
 if (virAsprintf(, "%s/%s",
 entry.mnt_dir, typestr) < 0)
 goto cleanup;
@@ -467,7 +458,6 @@ virCgroupDetectMountsFromFile(virCgroupPtr group,
 
 ret = 0;
  cleanup:
-VIR_FREE(linksrc);
 VIR_FORCE_FCLOSE(mounts);
 return ret;
 }
@@ -546,7 +536,7 @@ virCgroupDetectPlacement(virCgroupPtr group,
 FILE *mapping  = NULL;
 char line[1024];
 int ret = -1;
-char *procfile;
+VIR_AUTOFREE(char

[libvirt] [PATCH v2 19/41] util: pci: define cleanup function using VIR_DEFINE_AUTOPTR_FUNC

2018-07-26 Thread Sukrit Bhatnagar
Using the new VIR_DEFINE_AUTOPTR_FUNC macro defined in
src/util/viralloc.h, define a new wrapper around an existing
cleanup function which will be called when a variable declared
with VIR_AUTOPTR macro goes out of scope. Also, drop the redundant
viralloc.h include, since that has moved from the source module into
the header.

When variables of types virPCIDevicePtr, virPCIDeviceAddressPtr
and virPCIEDeviceInfoPtr are declared using VIR_AUTOPTR, the functions
virPCIDeviceFree, virPCIDeviceAddressFree and virPCIEDeviceInfoFree,
respectively, will be run automatically on them when they go out of scope.

Signed-off-by: Sukrit Bhatnagar 
Reviewed-by: Erik Skultety 
---
 src/util/virpci.c | 7 ++-
 src/util/virpci.h | 7 +++
 2 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/src/util/virpci.c b/src/util/virpci.c
index 8d02366..46f9905 100644
--- a/src/util/virpci.c
+++ b/src/util/virpci.c
@@ -39,7 +39,6 @@
 
 #include "dirname.h"
 #include "virlog.h"
-#include "viralloc.h"
 #include "vircommand.h"
 #include "virerror.h"
 #include "virfile.h"
@@ -3288,3 +3287,9 @@ virPCIEDeviceInfoFree(virPCIEDeviceInfoPtr dev)
 VIR_FREE(dev->link_sta);
 VIR_FREE(dev);
 }
+
+void
+virPCIDeviceAddressFree(virPCIDeviceAddressPtr address)
+{
+VIR_FREE(address);
+}
diff --git a/src/util/virpci.h b/src/util/virpci.h
index 794b7e5..2ac8769 100644
--- a/src/util/virpci.h
+++ b/src/util/virpci.h
@@ -28,6 +28,7 @@
 # include "virmdev.h"
 # include "virobject.h"
 # include "virutil.h"
+# include "viralloc.h"
 
 typedef struct _virPCIDevice virPCIDevice;
 typedef virPCIDevice *virPCIDevicePtr;
@@ -253,4 +254,10 @@ void virPCIEDeviceInfoFree(virPCIEDeviceInfoPtr dev);
 ssize_t virPCIGetMdevTypes(const char *sysfspath,
virMediatedDeviceType ***types);
 
+void virPCIDeviceAddressFree(virPCIDeviceAddressPtr address);
+
+VIR_DEFINE_AUTOPTR_FUNC(virPCIDevice, virPCIDeviceFree)
+VIR_DEFINE_AUTOPTR_FUNC(virPCIDeviceAddress, virPCIDeviceAddressFree)
+VIR_DEFINE_AUTOPTR_FUNC(virPCIEDeviceInfo, virPCIEDeviceInfoFree)
+
 #endif /* __VIR_PCI_H__ */
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v2 17/41] util: hook: use VIR_AUTOFREE instead of VIR_FREE for scalar types

2018-07-26 Thread Sukrit Bhatnagar
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOFREE macro for declaring scalar variables, majority
of the VIR_FREE calls can be dropped, which in turn leads to
getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar 
Reviewed-by: Erik Skultety 
---
 src/util/virhook.c | 22 +-
 1 file changed, 9 insertions(+), 13 deletions(-)

diff --git a/src/util/virhook.c b/src/util/virhook.c
index facd74a..4673655 100644
--- a/src/util/virhook.c
+++ b/src/util/virhook.c
@@ -122,8 +122,7 @@ static int virHooksFound = -1;
 static int
 virHookCheck(int no, const char *driver)
 {
-char *path;
-int ret;
+VIR_AUTOFREE(char *) path = NULL;
 
 if (driver == NULL) {
 virReportError(VIR_ERR_INTERNAL_ERROR,
@@ -139,18 +138,17 @@ virHookCheck(int no, const char *driver)
 }
 
 if (!virFileExists(path)) {
-ret = 0;
 VIR_DEBUG("No hook script %s", path);
-} else if (!virFileIsExecutable(path)) {
-ret = 0;
+return 0;
+}
+
+if (!virFileIsExecutable(path)) {
 VIR_WARN("Non-executable hook script %s", path);
-} else {
-ret = 1;
-VIR_DEBUG("Found hook script %s", path);
+return 0;
 }
 
-VIR_FREE(path);
-return ret;
+VIR_DEBUG("Found hook script %s", path);
+return 1;
 }
 
 /*
@@ -233,7 +231,7 @@ virHookCall(int driver,
 char **output)
 {
 int ret;
-char *path;
+VIR_AUTOFREE(char *) path = NULL;
 virCommandPtr cmd;
 const char *drvstr;
 const char *opstr;
@@ -318,7 +316,5 @@ virHookCall(int driver,
 
 virCommandFree(cmd);
 
-VIR_FREE(path);
-
 return ret;
 }
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


  1   2   3   4   >