]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib: [sysfs.c] make sysfs_read_* function more robust
authorKarel Zak <kzak@redhat.com>
Wed, 18 May 2011 09:57:17 +0000 (11:57 +0200)
committerKarel Zak <kzak@redhat.com>
Wed, 18 May 2011 09:57:17 +0000 (11:57 +0200)
The functions does not modify result if the requested sysfs attribute
does not exist.

Signed-off-by: Karel Zak <kzak@redhat.com>
include/sysfs.h
lib/sysfs.c
misc-utils/lsblk.c
shlibs/blkid/src/devname.c
shlibs/blkid/src/partitions/partitions.c
shlibs/blkid/src/topology/sysfs.c

index ed11a5e19602b1e101e8caf87d254325487ca821..5190ac4829146adfdef62a1168a82e5fea38ca1a 100644 (file)
@@ -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);
index bc380c030ed162f78bd206db7f2815e3cbbee669..3b805239001302e45b7227028ab4894a36d7afcf 100644 (file)
@@ -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;
 }
index 02061629e72ebc6e9b7fab5a6701116cf5691412..49be9fd11a0168c11cc472e2e22fb6eaa0bdabc0 100644 (file)
@@ -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)
index 81eac9aa34ab41f5587e661bf370c2d992a93e62..2ed85cdbded595b76ed560554db7c70df273c701 100644 (file)
@@ -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)
index 2ac297a1ca041d7a518b6b7cdbaddd5ab8ec3f90..060461d2714a2bb7d3cb1b4571f0408856ec669a 100644 (file)
@@ -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];
 
index d5169d02a9c4bf7d5645129e8a54142087504640..fccd58f4d584600ca3fab35a4f123c4b294adeb0 100644 (file)
@@ -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);
                }