DIR *grpdir;
struct dirent *ent;
int rc = 0;
+ int direrr;
grpdir = opendir(grppath);
if (grpdir == NULL) {
return rc;
}
- for (;;) {
+ /* This is best-effort cleanup: we want to log failures with just
+ * VIR_ERROR instead of normal virReportError */
+ while ((direrr = virDirRead(grpdir, &ent, NULL)) > 0) {
char *path;
- errno = 0;
- ent = readdir(grpdir);
- if (ent == NULL) {
- if ((rc = -errno))
- VIR_ERROR(_("Failed to readdir for %s (%d)"), grppath, errno);
- break;
- }
-
if (ent->d_name[0] == '.') continue;
if (ent->d_type != DT_DIR) continue;
if (rc != 0)
break;
}
+ if (direrr < 0) {
+ rc = -errno;
+ VIR_ERROR(_("Failed to readdir for %s (%d)"), grppath, errno);
+ }
+
closedir(grpdir);
VIR_DEBUG("Removing cgroup %s", grppath);
DIR *dp;
virCgroupPtr subgroup = NULL;
struct dirent *ent;
+ int direrr;
VIR_DEBUG("group=%p path=%s signum=%d pids=%p",
group, group->path, signum, pids);
return -1;
}
- while ((ent = readdir(dp))) {
+ while ((direrr = virDirRead(dp, &ent, keypath)) > 0) {
if (STREQ(ent->d_name, "."))
continue;
if (STREQ(ent->d_name, ".."))
virCgroupFree(&subgroup);
}
+ if (direrr < 0)
+ goto cleanup;
done:
ret = killedAny ? 1 : 0;
size_t i;
char *base = NULL, *entry = NULL;
DIR *dh = NULL;
+ int direrr;
for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) {
struct dirent *de;
goto cleanup;
}
- while ((de = readdir(dh)) != NULL) {
+ while ((direrr = virDirRead(dh, &de, base)) > 0) {
if (STREQ(de->d_name, ".") ||
STREQ(de->d_name, ".."))
continue;
VIR_FREE(entry);
}
+ if (direrr < 0)
+ goto cleanup;
if (chown(base, uid, gid) < 0) {
virReportSystemError(errno,
struct dirent *de;
char *looppath = NULL;
struct loop_info64 lo;
+ int direrr;
VIR_DEBUG("Looking for loop devices in /dev");
goto cleanup;
}
- errno = 0;
- while ((de = readdir(dh)) != NULL) {
+ while ((direrr = virDirRead(dh, &de, "/dev")) > 0) {
/* Checking 'loop' prefix is insufficient, since
* new kernels have a dev named 'loop-control'
*/
/* Oh well, try the next device */
VIR_FORCE_CLOSE(fd);
VIR_FREE(looppath);
- errno = 0;
}
-
- if (errno != 0)
- virReportSystemError(errno, "%s",
- _("Unable to iterate over loop devices"));
- else
- virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
- _("Unable to find a free loop device in /dev"));
+ if (direrr < 0)
+ goto cleanup;
+ virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+ _("Unable to find a free loop device in /dev"));
cleanup:
if (fd != -1) {
DIR *dh;
char *ret = NULL;
struct dirent *de;
+ int direrr;
if (!(dh = opendir(SYSFS_BLOCK_DIR))) {
virReportSystemError(errno,
return NULL;
}
- errno = 0;
- while ((de = readdir(dh)) != NULL) {
+ while ((direrr = virDirRead(dh, &de, SYSFS_BLOCK_DIR)) > 0) {
if (STRPREFIX(de->d_name, "nbd")) {
int rv = virFileNBDDeviceIsBusy(de->d_name);
if (rv < 0)
goto cleanup;
}
}
- errno = 0;
}
-
- if (errno != 0)
- virReportSystemError(errno, "%s",
- _("Unable to iterate over NBD devices"));
- else
- virReportSystemError(EBUSY, "%s",
- _("No free NBD devices"));
+ if (direrr < 0)
+ goto cleanup;
+ virReportSystemError(EBUSY, "%s",
+ _("No free NBD devices"));
cleanup:
closedir(dh);
struct dirent *de;
char *filepath = NULL;
int ret = -1;
+ int direrr;
if (!dh) {
virReportSystemError(errno, _("Cannot open dir '%s'"),
return -1;
}
- errno = 0;
- while ((de = readdir(dh)) != NULL) {
+ while ((direrr = virDirRead(dh, &de, dir)) > 0) {
struct stat sb;
if (STREQ(de->d_name, ".") ||
}
VIR_FREE(filepath);
- errno = 0;
}
-
- if (errno) {
- virReportSystemError(errno, _("Cannot read dir '%s'"),
- dir);
+ if (direrr < 0)
goto cleanup;
- }
if (rmdir(dir) < 0 && errno != ENOENT) {
virReportSystemError(errno,
/*
- * Copyright (C) 2007-2013 Red Hat, Inc.
+ * Copyright (C) 2007-2014 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
return NULL;
}
- while ((dp = readdir(dirp)) != NULL) {
+ while (virDirRead(dirp, &dp, "/dev") > 0) {
if (STRPREFIX(dp->d_name, "tap")) {
struct ifreq ifr;
if (virAsprintf(&devpath, "/dev/%s", dp->d_name) < 0) {
VIR_FREE(devpath);
VIR_FORCE_CLOSE(fd);
}
-
- errno = 0;
}
- if (errno != 0)
- virReportSystemError(errno, "%s",
- _("Unable to iterate over TAP devices"));
-
cleanup:
VIR_FREE(devpath);
VIR_FORCE_CLOSE(fd);
/*
* virpci.c: helper APIs for managing host PCI devices
*
- * Copyright (C) 2009-2013 Red Hat, Inc.
+ * Copyright (C) 2009-2014 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
return -1;
}
- while ((entry = readdir(dir))) {
+ while ((ret = virDirRead(dir, &entry, PCI_SYSFS "devices")) > 0) {
unsigned int domain, bus, slot, function;
virPCIDevicePtr check;
char *tmp;
DIR *dir = NULL;
int ret = -1;
struct dirent *ent;
+ int direrr;
if (virAsprintf(&pcidir, "/sys/bus/pci/devices/%04x:%02x:%02x.%x",
dev->domain, dev->bus, dev->slot, dev->function) < 0)
goto cleanup;
}
- while ((ent = readdir(dir)) != NULL) {
+ while ((direrr = virDirRead(dir, &ent, pcidir)) > 0) {
/* Device assignment requires:
* $PCIDIR/config, $PCIDIR/resource, $PCIDIR/resourceNNN,
* $PCIDIR/rom, $PCIDIR/reset
VIR_FREE(file);
}
}
+ if (direrr < 0)
+ goto cleanup;
ret = 0;
DIR *groupDir = NULL;
int ret = -1;
struct dirent *ent;
+ int direrr;
if (virAsprintf(&groupPath,
PCI_SYSFS "devices/%04x:%02x:%02x.%x/iommu_group/devices",
goto cleanup;
}
- while ((errno = 0, ent = readdir(groupDir)) != NULL) {
+ while ((direrr = virDirRead(groupDir, &ent, groupPath)) > 0) {
virPCIDeviceAddress newDev;
if (ent->d_name[0] == '.')
if ((actor)(&newDev, opaque) < 0)
goto cleanup;
}
- if (errno != 0) {
- virReportSystemError(errno,
- _("Failed to read directory entry for %s"),
- groupPath);
+ if (direrr < 0)
goto cleanup;
- }
ret = 0;
if (dir == NULL)
goto out;
- while ((entry = readdir(dir))) {
+ while (virDirRead(dir, &entry, pcidev_sysfs_net_path) > 0) {
if (STREQ(entry->d_name, ".") ||
STREQ(entry->d_name, ".."))
continue;
goto cleanup;
}
- while ((entry = readdir(dir))) {
+ while (virDirRead(dir, &entry, path) > 0) {
if (entry->d_name[0] == '.')
continue;
- if (VIR_STRDUP(sg, entry->d_name) < 0)
- goto cleanup;
+ /* Assume a single directory entry */
+ ignore_value(VIR_STRDUP(sg, entry->d_name));
+ break;
}
cleanup:
goto cleanup;
}
- while ((entry = readdir(dir))) {
+ while (virDirRead(dir, &entry, path) > 0) {
if (entry->d_name[0] == '.')
continue;
/*
* virusb.c: helper APIs for managing host USB devices
*
- * Copyright (C) 2009-2013 Red Hat, Inc.
+ * Copyright (C) 2009-2014 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
struct dirent *de;
virUSBDeviceListPtr list = NULL, ret = NULL;
virUSBDevicePtr usb;
+ int direrr;
if (!(list = virUSBDeviceListNew()))
goto cleanup;
goto cleanup;
}
- while ((de = readdir(dir))) {
+ while ((direrr = virDirRead(dir, &de, USB_SYSFS "/devices")) > 0) {
unsigned int found_prod, found_vend, found_bus, found_devno;
char *tmpstr = de->d_name;
if (found)
break;
}
+ if (direrr < 0)
+ goto cleanup;
ret = list;
cleanup:
p = buf; \
} while (0)
- while ((entry = readdir(dir))) {
+ while (virDirRead(dir, &entry, prefix) > 0) {
if (entry->d_name[0] == '.')
continue;
return NULL;
}
- while ((entry = readdir(dir))) {
+ while (virDirRead(dir, &entry, prefix) > 0) {
unsigned int host;
char *p = NULL;