Intel-GFX Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH i-g-t v2] lib/igt_sysfs: Let igt_sysfs_read|write return -errno
@ 2017-12-07 16:52 Michal Wajdeczko
  2017-12-07 16:59 ` Chris Wilson
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Michal Wajdeczko @ 2017-12-07 16:52 UTC (permalink / raw)
  To: intel-gfx

In some cases debugfs or sysfs may return errors that we
want to check. Return -errno from helper functions to make
asserts easier.

v2: don't forget about EOF ret=0 (Chris)
    small re-write (Michal)

Signed-off-by: Michal Wajdeczko <michal.wajdeczko@intel.com>
Cc: Chris Wilson <chris@chris-wilson.co.uk>
Cc: Joonas Lahtinen <joonas.lahtinen@linux.intel.com>
---
 lib/igt_sysfs.c | 44 +++++++++++++++++++++-----------------------
 1 file changed, 21 insertions(+), 23 deletions(-)

diff --git a/lib/igt_sysfs.c b/lib/igt_sysfs.c
index e7c67da..842a136 100644
--- a/lib/igt_sysfs.c
+++ b/lib/igt_sysfs.c
@@ -54,36 +54,34 @@
 
 static int readN(int fd, char *buf, int len)
 {
-	int total = 0;
+	int ret, total = 0;
 	do {
-		int ret = read(fd, buf + total, len - total);
-		if (ret < 0 && (errno == EINTR || errno == EAGAIN))
+		ret = read(fd, buf + total, len - total);
+		if (ret < 0)
+			ret = -errno;
+		if (ret == -EINTR || ret == -EAGAIN)
 			continue;
-
 		if (ret <= 0)
-			return total ?: ret;
-
+			break;
 		total += ret;
-		if (total == len)
-			return total;
-	} while (1);
+	} while (total != len);
+	return total ?: ret;
 }
 
 static int writeN(int fd, const char *buf, int len)
 {
-	int total = 0;
+	int ret, total = 0;
 	do {
-		int ret = write(fd, buf + total, len - total);
-		if (ret < 0 && (errno == EINTR || errno == EAGAIN))
+		ret = write(fd, buf + total, len - total);
+		if (ret < 0)
+			ret = -errno;
+		if (ret == -EINTR || ret == -EAGAIN)
 			continue;
-
 		if (ret <= 0)
-			return total ?: ret;
-
+			break;
 		total += ret;
-		if (total == len)
-			return total;
-	} while (1);
+	} while (total != len);
+	return total ?: ret;
 }
 
 /**
@@ -238,7 +236,7 @@ int igt_sysfs_open_parameters(int device)
  * This writes @len bytes from @data to the sysfs file.
  *
  * Returns:
- * The number of bytes written, or -1 on error.
+ * The number of bytes written, or -errno on error.
  */
 int igt_sysfs_write(int dir, const char *attr, const void *data, int len)
 {
@@ -246,7 +244,7 @@ int igt_sysfs_write(int dir, const char *attr, const void *data, int len)
 
 	fd = openat(dir, attr, O_WRONLY);
 	if (fd < 0)
-		return false;
+		return -errno;
 
 	len = writeN(fd, data, len);
 	close(fd);
@@ -264,7 +262,7 @@ int igt_sysfs_write(int dir, const char *attr, const void *data, int len)
  * This reads @len bytes from the sysfs file to @data
  *
  * Returns:
- * The length read, -1 on failure.
+ * The length read, -errno on failure.
  */
 int igt_sysfs_read(int dir, const char *attr, void *data, int len)
 {
@@ -272,7 +270,7 @@ int igt_sysfs_read(int dir, const char *attr, void *data, int len)
 
 	fd = openat(dir, attr, O_RDONLY);
 	if (fd < 0)
-		return false;
+		return -errno;
 
 	len = readN(fd, data, len);
 	close(fd);
@@ -338,7 +336,7 @@ char *igt_sysfs_get(int dir, const char *attr)
 		rem = len - offset - 1;
 	}
 
-	if (ret != -1)
+	if (ret > 0)
 		offset += ret;
 	buf[offset] = '\0';
 	while (offset > 0 && buf[offset-1] == '\n')
-- 
2.7.4

_______________________________________________
Intel-gfx mailing list
Intel-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

^ permalink raw reply related	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2017-12-15 11:28 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-12-07 16:52 [PATCH i-g-t v2] lib/igt_sysfs: Let igt_sysfs_read|write return -errno Michal Wajdeczko
2017-12-07 16:59 ` Chris Wilson
2017-12-07 21:00   ` Daniel Vetter
2017-12-13 14:16     ` Michal Wajdeczko
2017-12-15 11:28   ` Chris Wilson
2017-12-07 19:26 ` ✓ Fi.CI.BAT: success for lib/igt_sysfs: Let igt_sysfs_read|write return -errno (rev2) Patchwork
2017-12-07 23:00 ` ✗ Fi.CI.IGT: failure " Patchwork

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox