]> mj.ucw.cz Git - pciutils.git/commitdiff
Sysfs: Read failures of optional attributes are not fatal
authorMartin Mares <mj@ucw.cz>
Mon, 14 Sep 2015 15:43:04 +0000 (17:43 +0200)
committerMartin Mares <mj@ucw.cz>
Mon, 14 Sep 2015 15:43:04 +0000 (17:43 +0200)
Ameya Palande reported that with some kernels, reads of such
attributes fail on some hardware. He suggested to ignore read
failures completely, but I decided to turn the errors into
warnings in such cases. At least, the user will know that something
fishy is going on.

lib/sysfs.c

index a16c92a2b8980b2a9b79bdadbd92407d07fd8f95..986ecc93185aec0429961e4c8fedc815855c19f5 100644 (file)
@@ -93,21 +93,28 @@ sysfs_get_string(struct pci_dev *d, char *object, char *buf, int mandatory)
   struct pci_access *a = d->access;
   int fd, n;
   char namebuf[OBJNAMELEN];
+  void (*warn)(char *msg, ...) = (mandatory ? a->error : a->warning);
 
   sysfs_obj_name(d, object, namebuf);
   fd = open(namebuf, O_RDONLY);
   if (fd < 0)
     {
-      if (mandatory)
-       a->error("Cannot open %s: %s", namebuf, strerror(errno));
+      if (mandatory || errno != ENOENT)
+       warn("Cannot open %s: %s", namebuf, strerror(errno));
       return 0;
     }
   n = read(fd, buf, OBJBUFSIZE);
   close(fd);
   if (n < 0)
-    a->error("Error reading %s: %s", namebuf, strerror(errno));
+    {
+      warn("Error reading %s: %s", namebuf, strerror(errno));
+      return 0;
+     }
   if (n >= OBJBUFSIZE)
-    a->error("Value in %s too long", namebuf);
+    {
+      warn("Value in %s too long", namebuf);
+      return 0;
+    }
   buf[n] = 0;
   return 1;
 }