diff options
author | Bui Quang Minh <minhquangbui99@gmail.com> | 2022-09-13 17:42:48 +0300 |
---|---|---|
committer | Andrei Vagin <avagin@gmail.com> | 2022-11-02 07:35:04 +0300 |
commit | 1304415e5f159d9c059c48a17f3b7f0468fcb14e (patch) | |
tree | a3288d1e59561be61eed50cd6bc80b44b988e90a | |
parent | f5ad26cf7da6e3302af46ecde93d17b4239c31f5 (diff) |
zdtm: Add write_value/read_value helpers into zdtm library
Add write_value/read_value helpers to write/read buffer to/from files into zdmt
library.
Signed-off-by: Bui Quang Minh <minhquangbui99@gmail.com>
-rw-r--r-- | test/zdtm/lib/Makefile | 2 | ||||
-rw-r--r-- | test/zdtm/lib/file.c | 46 | ||||
-rw-r--r-- | test/zdtm/lib/zdtmtst.h | 3 | ||||
-rw-r--r-- | test/zdtm/static/cgroup04.c | 20 |
4 files changed, 50 insertions, 21 deletions
diff --git a/test/zdtm/lib/Makefile b/test/zdtm/lib/Makefile index 3ec58dfaf..949dc123a 100644 --- a/test/zdtm/lib/Makefile +++ b/test/zdtm/lib/Makefile @@ -4,7 +4,7 @@ CFLAGS += $(USERCFLAGS) LIB := libzdtmtst.a -LIBSRC := datagen.c msg.c parseargs.c test.c streamutil.c lock.c ns.c tcp.c unix.c fs.c sysctl.c mem.c +LIBSRC := datagen.c msg.c parseargs.c test.c streamutil.c lock.c ns.c tcp.c unix.c fs.c sysctl.c mem.c file.c PKG_CONFIG ?= pkg-config pkg-config-check = $(shell sh -c '$(PKG_CONFIG) $(1) && echo y') diff --git a/test/zdtm/lib/file.c b/test/zdtm/lib/file.c new file mode 100644 index 000000000..57d85421d --- /dev/null +++ b/test/zdtm/lib/file.c @@ -0,0 +1,46 @@ +#include <fcntl.h> +#include <unistd.h> +#include "zdtmtst.h" + +int write_value(const char *path, const char *value) +{ + int fd, l; + + fd = open(path, O_WRONLY); + if (fd < 0) { + pr_perror("open %s", path); + return -1; + } + + l = write(fd, value, strlen(value)); + if (l < 0) { + pr_perror("failed to write %s to %s", value, path); + close(fd); + return -1; + } + + close(fd); + return 0; +} + +int read_value(const char *path, char *value, int size) +{ + int fd, ret; + + fd = open(path, O_RDONLY); + if (fd < 0) { + pr_perror("open %s", path); + return -1; + } + + ret = read(fd, (void *)value, size); + if (ret < 0) { + pr_perror("read %s", path); + close(fd); + return -1; + } + + value[ret] = '\0'; + close(fd); + return 0; +} diff --git a/test/zdtm/lib/zdtmtst.h b/test/zdtm/lib/zdtmtst.h index d91886d25..105f3c11a 100644 --- a/test/zdtm/lib/zdtmtst.h +++ b/test/zdtm/lib/zdtmtst.h @@ -216,4 +216,7 @@ static inline void cleanup_closep(void *p) TEMP_FAILURE_RETRY(close(*pp)); } +extern int write_value(const char *path, const char *value); +extern int read_value(const char *path, char *value, int size); + #endif /* _VIMITESU_H_ */ diff --git a/test/zdtm/static/cgroup04.c b/test/zdtm/static/cgroup04.c index 5a424be12..8c40ffd6b 100644 --- a/test/zdtm/static/cgroup04.c +++ b/test/zdtm/static/cgroup04.c @@ -19,26 +19,6 @@ char *dirname; TEST_OPTION(dirname, string, "cgroup directory name", 1); static const char *cgname = "zdtmtst"; -int write_value(const char *path, const char *value) -{ - int fd, l; - - fd = open(path, O_WRONLY); - if (fd < 0) { - pr_perror("open %s", path); - return -1; - } - - l = write(fd, value, strlen(value)); - close(fd); - if (l < 0) { - pr_perror("failed to write %s to %s", value, path); - return -1; - } - - return 0; -} - int mount_and_add(const char *controller, const char *path, const char *prop, const char *value) { char aux[1024], paux[1024], subdir[1024]; |