@@ -172,6 +172,31 @@ int igt_sysfs_open_parameters(int device)
return params;
}
+
+/**
+ * igt_sysfs_write:
+ * @dir: directory for the device from igt_sysfs_open()
+ * @attr: name of the sysfs node to open
+ *
+ * This writes a single byte to the sysfs file.
+ *
+ * Returns:
+ * True on success, false on failure.
+ */
+bool igt_sysfs_write(int dir, const char *attr)
+{
+ int fd, ret;
+
+ fd = openat(dir, attr, O_WRONLY);
+ if (fd < 0)
+ return false;
+
+ ret = write(fd, "\0", 1);
+ close(fd);
+
+ return ret;
+}
+
/**
* igt_sysfs_set:
* @dir: directory for the device from igt_sysfs_open()
@@ -192,6 +217,7 @@ bool igt_sysfs_set(int dir, const char *attr, const char *value)
return false;
len = strlen(value);
+ igt_assert(len > 0);
ret = writeN(fd, value, len);
close(fd);
@@ -29,6 +29,7 @@
int igt_sysfs_open(int device, int *idx);
int igt_sysfs_open_parameters(int device);
+bool igt_sysfs_write(int dir, const char *attr);
bool igt_sysfs_set(int dir, const char *attr, const char *value);
char *igt_sysfs_get(int dir, const char *attr);
@@ -58,7 +58,7 @@ static void assert_entry(const char *s, bool expect)
error = igt_sysfs_get(sysfs, "error");
igt_assert(error);
- igt_assert_f(strcasecmp(error, s) != expect,
+ igt_assert_f(!!strcasecmp(error, s) != expect,
"contents of error: '%s' (expected %s '%s')\n",
error, expect ? "": "not", s);
@@ -77,7 +77,7 @@ static void assert_error_state_collected(void)
static void clear_error_state(void)
{
- igt_sysfs_set(sysfs, "error", "");
+ igt_sysfs_write(sysfs, "error");
}
static void test_error_state_basic(void)
clear_error_state was not doing anything (igt_sysfs_set was not writing to the error file because strlen was 0). Also fix assert_entry to catch this issue; strcasecmp returns 0 when there's a match, or an integer in a mismatch. v2: Use new igt_sysfs_write, add len > 0 assert in igt_sysfs_set to catch future errors, clean-up strcasecmp logic (Chris). Cc: Chris Wilson <chris@chris-wilson.co.uk> Fixes: 79c6a84ca85b ("igt/drv_hangman: Migrate to sysfs") Signed-off-by: Michel Thierry <michel.thierry@intel.com> --- lib/igt_sysfs.c | 26 ++++++++++++++++++++++++++ lib/igt_sysfs.h | 1 + tests/drv_hangman.c | 4 ++-- 3 files changed, 29 insertions(+), 2 deletions(-)