diff options
author | Karel Zak | 2011-05-18 11:57:17 +0200 |
---|---|---|
committer | Karel Zak | 2011-05-18 11:57:17 +0200 |
commit | 90e9fcda3bb3a215f027fc66c1182a18e0746972 (patch) | |
tree | cd10e1fd1fe586cf3d08287c82f30cd7e4edce72 | |
parent | build-sys: disable lib/ at.c tests building (diff) | |
download | kernel-qcow2-util-linux-90e9fcda3bb3a215f027fc66c1182a18e0746972.tar.gz kernel-qcow2-util-linux-90e9fcda3bb3a215f027fc66c1182a18e0746972.tar.xz kernel-qcow2-util-linux-90e9fcda3bb3a215f027fc66c1182a18e0746972.zip |
lib: [sysfs.c] make sysfs_read_* function more robust
The functions does not modify result if the requested sysfs attribute
does not exist.
Signed-off-by: Karel Zak <kzak@redhat.com>
-rw-r--r-- | include/sysfs.h | 8 | ||||
-rw-r--r-- | lib/sysfs.c | 50 | ||||
-rw-r--r-- | misc-utils/lsblk.c | 13 | ||||
-rw-r--r-- | shlibs/blkid/src/devname.c | 4 | ||||
-rw-r--r-- | shlibs/blkid/src/partitions/partitions.c | 10 | ||||
-rw-r--r-- | shlibs/blkid/src/topology/sysfs.c | 10 |
6 files changed, 70 insertions, 25 deletions
diff --git a/include/sysfs.h b/include/sysfs.h index ed11a5e19..5190ac482 100644 --- a/include/sysfs.h +++ b/include/sysfs.h @@ -44,9 +44,11 @@ extern int sysfs_has_attribute(struct sysfs_cxt *cxt, const char *attr); extern int sysfs_scanf(struct sysfs_cxt *cxt, const char *attr, const char *fmt, ...) __attribute__ ((format (scanf, 3, 4))); -extern int64_t sysfs_read_s64(struct sysfs_cxt *cxt, const char *attr); -extern uint64_t sysfs_read_u64(struct sysfs_cxt *cxt, const char *attr); -extern int sysfs_read_int(struct sysfs_cxt *cxt, const char *attr); + +extern int sysfs_read_s64(struct sysfs_cxt *cxt, const char *attr, int64_t *res); +extern int sysfs_read_u64(struct sysfs_cxt *cxt, const char *attr, uint64_t *res); +extern int sysfs_read_int(struct sysfs_cxt *cxt, const char *attr, int *res); + extern char *sysfs_strdup(struct sysfs_cxt *cxt, const char *attr); extern int sysfs_count_dirents(struct sysfs_cxt *cxt, const char *attr); diff --git a/lib/sysfs.c b/lib/sysfs.c index bc380c030..3b8052390 100644 --- a/lib/sysfs.c +++ b/lib/sysfs.c @@ -259,22 +259,40 @@ int sysfs_scanf(struct sysfs_cxt *cxt, const char *attr, const char *fmt, ...) return rc; } -int64_t sysfs_read_s64(struct sysfs_cxt *cxt, const char *attr) +int sysfs_read_s64(struct sysfs_cxt *cxt, const char *attr, int64_t *res) { - uint64_t x; - return sysfs_scanf(cxt, attr, "%"SCNd64, &x) == 1 ? x : 0; + int64_t x = 0; + + if (sysfs_scanf(cxt, attr, "%"SCNd64, &x) == 1) { + if (res) + *res = x; + return 0; + } + return -1; } -uint64_t sysfs_read_u64(struct sysfs_cxt *cxt, const char *attr) +int sysfs_read_u64(struct sysfs_cxt *cxt, const char *attr, uint64_t *res) { - uint64_t x; - return sysfs_scanf(cxt, attr, "%"SCNu64, &x) == 1 ? x : 0; + uint64_t x = 0; + + if (sysfs_scanf(cxt, attr, "%"SCNu64, &x) == 1) { + if (res) + *res = x; + return 0; + } + return -1; } -int sysfs_read_int(struct sysfs_cxt *cxt, const char *attr) +int sysfs_read_int(struct sysfs_cxt *cxt, const char *attr, int *res) { - int x; - return sysfs_scanf(cxt, attr, "%d", &x) == 1 ? x : 0; + int x = 0; + + if (sysfs_scanf(cxt, attr, "%d", &x) == 1) { + if (res) + *res = x; + return 0; + } + return -1; } char *sysfs_strdup(struct sysfs_cxt *cxt, const char *attr) @@ -328,6 +346,8 @@ int main(int argc, char *argv[]) char *devname; dev_t devno; char path[PATH_MAX]; + int i; + uint64_t u64; if (argc != 2) errx(EXIT_FAILURE, "usage: %s <devname>", argv[0]); @@ -347,8 +367,16 @@ int main(int argc, char *argv[]) sysfs_init(&cxt, devno, NULL); printf("SLAVES: %d\n", sysfs_count_dirents(&cxt, "slaves")); - printf("SIZE: %jd\n", sysfs_read_u64(&cxt, "size")); - printf("SECTOR: %d\n", sysfs_read_int(&cxt, "queue/hw_sector_size")); + + if (sysfs_read_u64(&cxt, "size", &u64)) + printf("read SIZE failed"); + else + printf("SIZE: %jd\n", u64); + + if (sysfs_read_int(&cxt, "queue/hw_sector_size", &i)) + printf("read SECTOR failed"); + else + printf("SECTOR: %d\n", i); return EXIT_SUCCESS; } diff --git a/misc-utils/lsblk.c b/misc-utils/lsblk.c index 02061629e..49be9fd11 100644 --- a/misc-utils/lsblk.c +++ b/misc-utils/lsblk.c @@ -391,8 +391,11 @@ static char *get_type(struct blkdev_cxt *cxt) } else { const char *type = cxt->partition ? "part" : "disk"; + int x = 0; - switch (sysfs_read_int(&cxt->sysfs, "device/type")) { + sysfs_read_int(&cxt->sysfs, "device/type", &x); + + switch (x) { case 0x0c: /* TYPE_RAID */ type = "raid"; break; case 0x01: /* TYPE_TAPE */ @@ -406,7 +409,7 @@ static char *get_type(struct blkdev_cxt *cxt) type = "rbc"; break; } - res = xstrdup(type); + res = xstrdup(type); } for (p = res; p && *p; p++) @@ -619,8 +622,10 @@ static int set_cxt(struct blkdev_cxt *cxt, cxt->maj = major(devno); cxt->min = minor(devno); - cxt->size = sysfs_read_u64(&cxt->sysfs, "size") << 9; - cxt->discard = sysfs_read_int(&cxt->sysfs, "queue/discard_granularity"); + sysfs_read_u64(&cxt->sysfs, "size", &cxt->size); /* in sectors */ + cxt->size <<= 9; /* in bytes */ + + sysfs_read_int(&cxt->sysfs, "queue/discard_granularity", &cxt->discard); /* Ignore devices of zero size */ if (!lsblk->all_devices && cxt->size == 0) diff --git a/shlibs/blkid/src/devname.c b/shlibs/blkid/src/devname.c index 81eac9aa3..2ed85cdbd 100644 --- a/shlibs/blkid/src/devname.c +++ b/shlibs/blkid/src/devname.c @@ -555,7 +555,7 @@ static int probe_all_removable(blkid_cache cache) while((d = readdir(dir))) { struct sysfs_cxt sysfs; - int removable; + int removable = 0; dev_t devno; #ifdef _DIRENT_HAVE_D_TYPE @@ -572,7 +572,7 @@ static int probe_all_removable(blkid_cache cache) continue; sysfs_init(&sysfs, devno, NULL); - removable = sysfs_read_int(&sysfs, "removable"); + sysfs_read_int(&sysfs, "removable", &removable); sysfs_deinit(&sysfs); if (removable) diff --git a/shlibs/blkid/src/partitions/partitions.c b/shlibs/blkid/src/partitions/partitions.c index 2ac297a1c..060461d27 100644 --- a/shlibs/blkid/src/partitions/partitions.c +++ b/shlibs/blkid/src/partitions/partitions.c @@ -892,16 +892,20 @@ blkid_partition blkid_partlist_devno_to_partition(blkid_partlist ls, dev_t devno { struct sysfs_cxt sysfs; uint64_t start, size; - int i; + int i, rc; if (sysfs_init(&sysfs, devno, NULL)) return NULL; - start = sysfs_read_u64(&sysfs, "start"); - size = sysfs_read_u64(&sysfs, "size"); + rc = sysfs_read_u64(&sysfs, "start", &start); + if (!rc) + rc = sysfs_read_u64(&sysfs, "size", &size); sysfs_deinit(&sysfs); + if (rc) + return NULL; + for (i = 0; i < ls->nparts; i++) { blkid_partition par = &ls->parts[i]; diff --git a/shlibs/blkid/src/topology/sysfs.c b/shlibs/blkid/src/topology/sysfs.c index d5169d02a..fccd58f4d 100644 --- a/shlibs/blkid/src/topology/sysfs.c +++ b/shlibs/blkid/src/topology/sysfs.c @@ -78,11 +78,17 @@ static int probe_sysfs_tp(blkid_probe pr, const struct blkid_idmag *mag) } if (val->set_ulong) { - uint64_t data = sysfs_read_u64(&sysfs, val->attr); + uint64_t data; + + if (sysfs_read_u64(&sysfs, val->attr, &data) != 0) + continue; rc = val->set_ulong(pr, (unsigned long) data); } else if (val->set_int) { - int64_t data = sysfs_read_s64(&sysfs, val->attr); + int64_t data; + + if (sysfs_read_s64(&sysfs, val->attr, &data) != 0) + continue; rc = val->set_int(pr, (int) data); } |