diff options
author | Chris Wilson <chris@chris-wilson.co.uk> | 2017-02-23 02:04:48 +0000 |
---|---|---|
committer | Chris Wilson <chris@chris-wilson.co.uk> | 2017-02-23 02:09:10 +0000 |
commit | c5da0662d1c0dd92bbaf307b6c35449c0c598300 (patch) | |
tree | a2a8c26a47f97f1e68bf23a8095eacf28a7704d8 /lib | |
parent | a7c9add6ff823a7c5bd440422fa84a748f08db0f (diff) |
lib/sysfs: Explicit read/write length
Export a couple of routines to read/write an exact length, rather than a
strring.
Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/igt_sysfs.c | 62 | ||||
-rw-r--r-- | lib/igt_sysfs.h | 4 |
2 files changed, 57 insertions, 9 deletions
diff --git a/lib/igt_sysfs.c b/lib/igt_sysfs.c index ded10a67..dc178b3d 100644 --- a/lib/igt_sysfs.c +++ b/lib/igt_sysfs.c @@ -172,30 +172,74 @@ int igt_sysfs_open_parameters(int device) return params; } + /** - * igt_sysfs_set: + * igt_sysfs_write: * @dir: directory for the device from igt_sysfs_open() * @attr: name of the sysfs node to open - * @value: the string to write + * @data: the block to write from + * @len: the length to write * - * This writes the value to the sysfs file. + * This writes @len bytes from @data to the sysfs file. * * Returns: - * True on success, false on failure. + * The number of bytes written, or -1 on error. */ -bool igt_sysfs_set(int dir, const char *attr, const char *value) +int igt_sysfs_write(int dir, const char *attr, const void *data, int len) +{ + int fd; + + fd = openat(dir, attr, O_WRONLY); + if (fd < 0) + return false; + + len = writeN(fd, data, len); + close(fd); + + return len; +} + +/** + * igt_sysfs_read: + * @dir: directory for the device from igt_sysfs_open() + * @attr: name of the sysfs node to open + * @data: the block to read into + * @len: the maximum length to read + * + * This reads @len bytes from the sysfs file to @data + * + * Returns: + * The length read, -1 on failure. + */ +int igt_sysfs_read(int dir, const char *attr, void *data, int len) { - int fd, len, ret; + int fd; fd = openat(dir, attr, O_WRONLY); if (fd < 0) return false; - len = strlen(value); - ret = writeN(fd, value, len); + len = readN(fd, data, len); close(fd); - return len == ret; + return len; +} + +/** + * igt_sysfs_set: + * @dir: directory for the device from igt_sysfs_open() + * @attr: name of the sysfs node to open + * @value: the string to write + * + * This writes the value to the sysfs file. + * + * Returns: + * True on success, false on failure. + */ +bool igt_sysfs_set(int dir, const char *attr, const char *value) +{ + int len = strlen(value); + return igt_sysfs_write(dir, attr, value, len) == len; } /** diff --git a/lib/igt_sysfs.h b/lib/igt_sysfs.h index 69b2e5ec..4089535d 100644 --- a/lib/igt_sysfs.h +++ b/lib/igt_sysfs.h @@ -29,6 +29,10 @@ int igt_sysfs_open(int device, int *idx); int igt_sysfs_open_parameters(int device); + +int igt_sysfs_read(int dir, const char *attr, void *data, int len); +int igt_sysfs_write(int dir, const char *attr, const void *data, int len); + bool igt_sysfs_set(int dir, const char *attr, const char *value); char *igt_sysfs_get(int dir, const char *attr); |