[Intel-gfx] [PATCH i-g-t 3/3] lib/igt_sysfs: Simplify igt_sysfs_path

Tvrtko Ursulin tursulin at ursulin.net
Tue Nov 28 18:09:57 UTC 2017


From: Tvrtko Ursulin <tvrtko.ursulin at intel.com>

There is no need to iterate over /sys/class/drm/card* directories looking
for the one which matches our major and minor, when we can directly find
the right one via the /sys/dev/char/<major>:<minor> symlink.

Signed-off-by: Tvrtko Ursulin <tvrtko.ursulin at intel.com>
Suggested-by: Lionel Landwerlin <lionel.g.landwerlin at intel.com>
---
 lib/igt_sysfs.c | 36 ++++++++++++------------------------
 1 file changed, 12 insertions(+), 24 deletions(-)

diff --git a/lib/igt_sysfs.c b/lib/igt_sysfs.c
index f0fef8d434ef..a4a87711ed0c 100644
--- a/lib/igt_sysfs.c
+++ b/lib/igt_sysfs.c
@@ -100,40 +100,28 @@ static int writeN(int fd, const char *buf, int len)
  */
 char *igt_sysfs_path(int device, char *path, int pathlen, int *idx)
 {
+	unsigned int maj, min;
 	struct stat st;
+	int len;
 
 	if (device < 0 || fstat(device, &st) || !S_ISCHR(st.st_mode))
 		return NULL;
 
+	maj = major(st.st_rdev);
+	min = minor(st.st_rdev);
+
 	/* Only support master nodes. */
-	if (minor(st.st_rdev) >= 64)
+	if (min >= 64)
 		return NULL;
 
-	for (int n = 0; n < 16; n++) {
-		int len = snprintf(path, pathlen, "/sys/class/drm/card%d", n);
-		int ret, maj, min;
-		FILE *file;
-
-		sprintf(path + len, "/dev");
-		file = fopen(path, "r");
-		if (!file)
-			continue;
-
-		ret = fscanf(file, "%d:%d", &maj, &min);
-		fclose(file);
-
-		if (ret != 2 ||
-			major(st.st_rdev) != maj ||
-			minor(st.st_rdev) != min)
-			continue;
+	len = snprintf(path, pathlen, "/sys/dev/char/%u:%u", maj, min);
+	if (len == pathlen)
+		return NULL;
 
-		path[len] = '\0';
-		if (idx)
-			*idx = n;
-		return path;
-	}
+	if (idx)
+		*idx = min;
 
-	return NULL;
+	return path;
 }
 
 /**
-- 
2.14.1



More information about the Intel-gfx mailing list