From 6329e660a41bb6bc23f85061a1931c97de136664 Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Tue, 2 May 2017 16:51:47 -0700 Subject: [PATCH] test/zdtm/static: split ofd_file_locks.h to .c/.h Having a "header library" is nice if it's small and clean, but - we compile its code a few times; - there is no distinction between internal and external functions. Let's separate functions out of header and into a .c file. Signed-off-by: Kir Kolyshkin Signed-off-by: Andrei Vagin --- test/zdtm/static/Makefile | 3 +- test/zdtm/static/ofd_file_locks.c | 138 +++++++++++++++++++++++++++++ test/zdtm/static/ofd_file_locks.h | 139 +----------------------------- 3 files changed, 143 insertions(+), 137 deletions(-) create mode 100644 test/zdtm/static/ofd_file_locks.c diff --git a/test/zdtm/static/Makefile b/test/zdtm/static/Makefile index 8c8c38362..26cb6eaf2 100644 --- a/test/zdtm/static/Makefile +++ b/test/zdtm/static/Makefile @@ -299,7 +299,7 @@ TST_STATE = \ conntracks \ route_rules \ -AUX_SRC = get_smaps_bits.c +AUX_SRC = get_smaps_bits.c ofd_file_locks.c SRC = $(TST:%=%.c) $(AUX_SRC) OBJ = $(SRC:%.c=%.o) @@ -385,6 +385,7 @@ $(TST): | $(LIB) aio00: LDLIBS += -laio different_creds: LDLIBS += -lcap +file_locks06 file_locks07 file_locks08: ofd_file_locks.o futex: CFLAGS += -pthread futex: LDFLAGS += -pthread futex-rl: CFLAGS += -pthread diff --git a/test/zdtm/static/ofd_file_locks.c b/test/zdtm/static/ofd_file_locks.c new file mode 100644 index 000000000..61495a202 --- /dev/null +++ b/test/zdtm/static/ofd_file_locks.c @@ -0,0 +1,138 @@ +#include +#include +#include +#include + +#include "zdtmtst.h" +#include "fs.h" +#include "ofd_file_locks.h" + +static int parse_ofd_lock(char *buf, struct flock64 *lck) +{ + char fl_flag[10], fl_type[15], fl_option[10], fl_end[32]; + long long start; + int num; + + if (strncmp(buf, "lock:\t", 6) != 0) + return 1; /* isn't lock, skip record */ + + num = sscanf(buf, + "%*s %*d: %s %s %s %*d %*x:%*x:%*d %lld %s", + fl_flag, fl_type, fl_option, &start, fl_end); + + if (num < 4) { + pr_err("Invalid lock info %s\n", buf); + return -1; + } + if (strcmp(fl_flag, "OFDLCK")) + return 1; + + lck->l_start = start; + + if (strcmp(fl_end, "EOF")) { + unsigned long end; + + if (sscanf(fl_end, "%lu", &end) <= 0) { + pr_err("Invalid lock entry\n"); + return -1; + } + lck->l_len = end - lck->l_start + 1; + } else { + lck->l_len = 0; + } + if (strcmp(fl_option, "WRITE") == 0) + lck->l_type = F_WRLCK; + else + lck->l_type = F_RDLCK; + + return 0; +} + +static int read_fd_ofd_lock(int pid, int fd, struct flock64 *lck) +{ + char path[PATH_MAX]; + char buf[100]; + int num; + FILE *proc_file = NULL; + + sprintf(path, "/proc/%i/fdinfo/%i", pid, fd); + proc_file = fopen(path, "r"); + + if (!proc_file) { + pr_err("Can't open %s\n", path); + return -1; + } + + num = -1; + while (fgets(buf, sizeof(buf), proc_file)) { + num = parse_ofd_lock(buf, lck); + if (num <= 0) + break; + } + + if (fclose(proc_file)) { + pr_err("Can't close %s\n", path); + return -1; + } + return num; +} + +int check_lock_exists(const char *filename, struct flock64 *lck) +{ + int ret = -1; + int fd; + + fd = open(filename, O_RDWR, 0666); + + if (lck->l_type == F_RDLCK) { + /* check, that there is no write lock */ + ret = fcntl(fd, F_OFD_GETLK, lck); + if (ret) { + pr_err("fcntl failed (%i)\n", ret); + goto out; + } + if (lck->l_type != F_UNLCK) { + pr_err("OFD lock type do not match\n"); + goto out; + } + } + + /* check, that lock is set */ + lck->l_type = F_WRLCK; + ret = fcntl(fd, F_OFD_GETLK, lck); + if (ret) { + pr_err("fcntl failed (%i)\n", ret); + goto out; + } + if (lck->l_type == F_UNLCK) { + pr_err("Lock not found\n"); + goto out; + } + + ret = 0; +out: + if (close(fd)) + return -1; + return ret; +} + +static int check_file_locks_match(struct flock64 *orig_lck, struct flock64 *lck) +{ + return orig_lck->l_start == lck->l_start && + orig_lck->l_len == lck->l_len && + orig_lck->l_type == lck->l_type; +} + +int check_file_lock_restored(int pid, int fd, struct flock64 *lck) +{ + struct flock64 lck_restored; + + if (read_fd_ofd_lock(pid, fd, &lck_restored)) + return -1; + + if (!check_file_locks_match(lck, &lck_restored)) { + pr_err("Can't restore file lock (fd: %i)\n", fd); + return -1; + } + return 0; +} diff --git a/test/zdtm/static/ofd_file_locks.h b/test/zdtm/static/ofd_file_locks.h index b2c249a9b..0d500e1bf 100644 --- a/test/zdtm/static/ofd_file_locks.h +++ b/test/zdtm/static/ofd_file_locks.h @@ -2,12 +2,6 @@ #define ZDTM_OFD_FILE_LOCKS_H_ #include -#include -#include -#include - -#include "zdtmtst.h" -#include "fs.h" #ifndef F_OFD_GETLK #define F_OFD_GETLK 36 @@ -16,138 +10,11 @@ #endif /* - * Header library for parsing of OFD locks + * Functions for parsing of OFD locks * from procfs and checking them after restoring. */ -static int parse_ofd_lock(char *buf, struct flock64 *lck) -{ - char fl_flag[10], fl_type[15], fl_option[10], fl_end[32]; - long long start; - int num; - - if (strncmp(buf, "lock:\t", 6) != 0) - return 1; /* isn't lock, skip record */ - - num = sscanf(buf, - "%*s %*d: %s %s %s %*d %*x:%*x:%*d %lld %s", - fl_flag, fl_type, fl_option, &start, fl_end); - - if (num < 4) { - pr_err("Invalid lock info %s\n", buf); - return -1; - } - if (strcmp(fl_flag, "OFDLCK")) - return 1; - - lck->l_start = start; - - if (strcmp(fl_end, "EOF")) { - unsigned long end; - - if (sscanf(fl_end, "%lu", &end) <= 0) { - pr_err("Invalid lock entry\n"); - return -1; - } - lck->l_len = end - lck->l_start + 1; - } else { - lck->l_len = 0; - } - if (strcmp(fl_option, "WRITE") == 0) - lck->l_type = F_WRLCK; - else - lck->l_type = F_RDLCK; - - return 0; -} - -static int read_fd_ofd_lock(int pid, int fd, struct flock64 *lck) -{ - char path[PATH_MAX]; - char buf[100]; - int num; - FILE *proc_file = NULL; - - sprintf(path, "/proc/%i/fdinfo/%i", pid, fd); - proc_file = fopen(path, "r"); - - if (!proc_file) { - pr_err("Can't open %s\n", path); - return -1; - } - - num = -1; - while (fgets(buf, sizeof(buf), proc_file)) { - num = parse_ofd_lock(buf, lck); - if (num <= 0) - break; - } - - if (fclose(proc_file)) { - pr_err("Can't close %s\n", path); - return -1; - } - return num; -} - -static int check_lock_exists(const char *filename, struct flock64 *lck) -{ - int ret = -1; - int fd; - - fd = open(filename, O_RDWR, 0666); - - if (lck->l_type == F_RDLCK) { - /* check, that there is no write lock */ - ret = fcntl(fd, F_OFD_GETLK, lck); - if (ret) { - pr_err("fcntl failed (%i)\n", ret); - goto out; - } - if (lck->l_type != F_UNLCK) { - pr_err("OFD lock type do not match\n"); - goto out; - } - } - - /* check, that lock is set */ - lck->l_type = F_WRLCK; - ret = fcntl(fd, F_OFD_GETLK, lck); - if (ret) { - pr_err("fcntl failed (%i)\n", ret); - goto out; - } - if (lck->l_type == F_UNLCK) { - pr_err("Lock not found\n"); - goto out; - } - - ret = 0; -out: - if (close(fd)) - return -1; - return ret; -} - -static int check_file_locks_match(struct flock64 *orig_lck, struct flock64 *lck) -{ - return orig_lck->l_start == lck->l_start && - orig_lck->l_len == lck->l_len && - orig_lck->l_type == lck->l_type; -} - -static int check_file_lock_restored(int pid, int fd, struct flock64 *lck) -{ - struct flock64 lck_restored; - - if (read_fd_ofd_lock(pid, fd, &lck_restored)) - return -1; - - if (!check_file_locks_match(lck, &lck_restored)) { - pr_err("Can't restore file lock (fd: %i)\n", fd); - return -1; - } - return 0; -} +extern int check_lock_exists(const char *filename, struct flock64 *lck); +extern int check_file_lock_restored(int pid, int fd, struct flock64 *lck); #endif /* ZDTM_OFD_FILE_LOCKS_H_ */