Patchwork [i-g-t] lib/igt_sysfs: Let igt_sysfs_read|write return -errno

login
register
mail settings
Submitter Michal Wajdeczko
Date Dec. 6, 2017, 7 p.m.
Message ID <20171206190022.48132-1-michal.wajdeczko@intel.com>
Download mbox | patch
Permalink /patch/10096771/
State New
Headers show

Comments

Michal Wajdeczko - Dec. 6, 2017, 7 p.m.
In some cases debugfs or sysfs may return errors that we
want to check. Return -errno from helper functions to make
asserts easier.

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 | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
Chris Wilson - Dec. 6, 2017, 7:07 p.m.
Quoting Michal Wajdeczko (2017-12-06 19:00:22)
> In some cases debugfs or sysfs may return errors that we
> want to check. Return -errno from helper functions to make
> asserts easier.
> 
> 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 | 18 +++++++++---------
>  1 file changed, 9 insertions(+), 9 deletions(-)
> 
> diff --git a/lib/igt_sysfs.c b/lib/igt_sysfs.c
> index e7c67da..9e9fdde 100644
> --- a/lib/igt_sysfs.c
> +++ b/lib/igt_sysfs.c
> @@ -60,8 +60,8 @@ static int readN(int fd, char *buf, int len)
>                 if (ret < 0 && (errno == EINTR || errno == EAGAIN))
>                         continue;
>  
> -               if (ret <= 0)
> -                       return total ?: ret;
> +               if (ret < 0)
> +                       return total ?: -errno;

We do need to keep the break on ret == 0; some fs will just keep on
reporting 0 bytes read for EOF.

How about

if (ret < 0) {
	ret = -errno;
	if (ret == -EINTR || ret == -EAGAIN)
		continue;
}

if (ret <= 0)
	return total ?: ret;

>  
>                 total += ret;
>                 if (total == len)
> @@ -77,8 +77,8 @@ static int writeN(int fd, const char *buf, int len)
>                 if (ret < 0 && (errno == EINTR || errno == EAGAIN))
>                         continue;
>  
> -               if (ret <= 0)
> -                       return total ?: ret;
> +               if (ret < 0)
> +                       return total ?: -errno;

Ditto.

Reviewed-by: Chris Wilson <chris@chris-wilson.co.uk>
-Chris

Patch

diff --git a/lib/igt_sysfs.c b/lib/igt_sysfs.c
index e7c67da..9e9fdde 100644
--- a/lib/igt_sysfs.c
+++ b/lib/igt_sysfs.c
@@ -60,8 +60,8 @@  static int readN(int fd, char *buf, int len)
 		if (ret < 0 && (errno == EINTR || errno == EAGAIN))
 			continue;
 
-		if (ret <= 0)
-			return total ?: ret;
+		if (ret < 0)
+			return total ?: -errno;
 
 		total += ret;
 		if (total == len)
@@ -77,8 +77,8 @@  static int writeN(int fd, const char *buf, int len)
 		if (ret < 0 && (errno == EINTR || errno == EAGAIN))
 			continue;
 
-		if (ret <= 0)
-			return total ?: ret;
+		if (ret < 0)
+			return total ?: -errno;
 
 		total += ret;
 		if (total == len)
@@ -238,7 +238,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 +246,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 +264,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 +272,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 +338,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')