diff --git a/src/nodeinfo.c b/src/nodeinfo.c index 53ba716..f239d4a 100644 --- a/src/nodeinfo.c +++ b/src/nodeinfo.c @@ -440,6 +440,7 @@ virNodeParseNode(const char *node, int siblings; unsigned int cpu; int online; + int direrr; *threads = 0; *cores = 0; @@ -452,8 +453,7 @@ virNodeParseNode(const char *node, /* enumerate sockets in the node */ CPU_ZERO(&sock_map); - errno = 0; - while ((cpudirent = readdir(cpudir))) { + while ((direrr = virDirRead(cpudir, &cpudirent, node)) == 0) { if (sscanf(cpudirent->d_name, "cpu%u", &cpu) != 1) continue; @@ -470,14 +470,10 @@ virNodeParseNode(const char *node, if (sock > sock_max) sock_max = sock; - - errno = 0; } - if (errno) { - virReportSystemError(errno, _("problem reading %s"), node); + if (direrr < 0) goto cleanup; - } sock_max++; @@ -490,8 +486,7 @@ virNodeParseNode(const char *node, /* iterate over all CPU's in the node */ rewinddir(cpudir); - errno = 0; - while ((cpudirent = readdir(cpudir))) { + while ((direrr = virDirRead(cpudir, &cpudirent, node)) == 0) { if (sscanf(cpudirent->d_name, "cpu%u", &cpu) != 1) continue; @@ -530,14 +525,10 @@ virNodeParseNode(const char *node, if (siblings > *threads) *threads = siblings; - - errno = 0; } - if (errno) { - virReportSystemError(errno, _("problem reading %s"), node); + if (direrr < 0) goto cleanup; - } /* finalize the returned data */ *sockets = CPU_COUNT(&sock_map); @@ -576,6 +567,7 @@ int linuxNodeInfoCPUPopulate(FILE *cpuinfo, int ret = -1; char *sysfs_nodedir = NULL; char *sysfs_cpudir = NULL; + int direrr; /* Start with parsing CPU clock speed from /proc/cpuinfo */ while (fgets(line, sizeof(line), cpuinfo) != NULL) { @@ -672,8 +664,7 @@ int linuxNodeInfoCPUPopulate(FILE *cpuinfo, goto fallback; } - errno = 0; - while ((nodedirent = readdir(nodedir))) { + while ((direrr = virDirRead(nodedir, &nodedirent, sysfs_nodedir)) == 0) { if (sscanf(nodedirent->d_name, "node%u", &node) != 1) continue; @@ -699,14 +690,10 @@ int linuxNodeInfoCPUPopulate(FILE *cpuinfo, if (threads > nodeinfo->threads) nodeinfo->threads = threads; - - errno = 0; } - if (errno) { - virReportSystemError(errno, _("problem reading %s"), sysfs_nodedir); + if (direrr < 0) goto cleanup; - } if (nodeinfo->cpus && nodeinfo->nodes) goto done; diff --git a/src/util/virfile.c b/src/util/virfile.c index 3eb2703..b54b9fd 100644 --- a/src/util/virfile.c +++ b/src/util/virfile.c @@ -2295,6 +2295,20 @@ virDirCreate(const char *path ATTRIBUTE_UNUSED, } #endif /* WIN32 */ +/* return 0 = success, 1 = end-of-dir and -1 = error */ +int virDirRead(DIR *dirp, struct dirent **ent, const char *dirname) +{ + errno = 0; + *ent = readdir(dirp); + if (!*ent && errno) { + if (dirname) + virReportSystemError(errno, _("Unable to read directory '%s'"), + dirname); + return -1; + } + return !*ent; +} + static int virFileMakePathHelper(char *path, mode_t mode) { diff --git a/src/util/virfile.h b/src/util/virfile.h index 46ef781..622a81b 100644 --- a/src/util/virfile.h +++ b/src/util/virfile.h @@ -27,6 +27,7 @@ # define __VIR_FILE_H_ # include +# include # include "internal.h" # include "virstoragefile.h" @@ -211,6 +212,8 @@ enum { }; int virDirCreate(const char *path, mode_t mode, uid_t uid, gid_t gid, unsigned int flags) ATTRIBUTE_RETURN_CHECK; +int virDirRead(DIR *dirp, struct dirent **ent, const char *dirname); + int virFileMakePath(const char *path) ATTRIBUTE_RETURN_CHECK; int virFileMakePathWithMode(const char *path, mode_t mode) ATTRIBUTE_RETURN_CHECK;