<div dir="ltr">Thank you, Pavel.</div><div class="gmail_extra"><br><div class="gmail_quote">2016-12-30 13:47 GMT+03:00 Pavel Emelyanov <span dir="ltr"><<a href="mailto:xemul@virtuozzo.com" target="_blank">xemul@virtuozzo.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On 12/30/2016 12:17 PM, Pavel Emelyanov wrote:<br>
> On 12/20/2016 01:24 PM, Pavel Begunkov wrote:<br>
>> From: Begunkov Pavel <<a href="mailto:asml.silence@gmail.com">asml.silence@gmail.com</a>><br>
>><br>
>> There are 3 tests that cover the following cases:<br>
>><br>
>> 1. single OFD lock<br>
>> 2. overlapped OFD locks<br>
>> 3. inhertited OFD locks<br>
>><br>
>> Tests logic is similar to logic of tests for other lock types.<br>
>><br>
>> OFD lock specific header-only library was added to avoid code<br>
>> duplication in the tests.<br>
>><br>
>> Signed-off-by: Begunkov Pavel <<a href="mailto:asml.silence@gmail.com">asml.silence@gmail.com</a>><br>
>> Signed-off-by: Eugene Batalov <<a href="mailto:eabatalov89@gmail.com">eabatalov89@gmail.com</a>><br>
>> ---<br>
>> test/zdtm/static/Makefile | 3 +<br>
>> test/zdtm/static/file_locks06.<wbr>c | 67 ++++++++++++++++<br>
>> test/zdtm/static/file_locks06.<wbr>desc | 1 +<br>
>> test/zdtm/static/file_locks07.<wbr>c | 101 ++++++++++++++++++++++++<br>
>> test/zdtm/static/file_locks07.<wbr>desc | 1 +<br>
>> test/zdtm/static/file_locks08.<wbr>c | 86 +++++++++++++++++++++<br>
><br>
> Guys, this test fails:<br>
<br>
</span>I've fixed it with 7d38db56a67d5f0d9f6e3fb499197b<wbr>4d081108b2<br>
<div class="HOEnZb"><div class="h5"><br>
> # ./zdtm.py run -t zdtm/static/file_locks08<br>
> cc -D_GNU_SOURCE umount2.c -o umount2<br>
> cc -D_GNU_SOURCE zdtm_ct.c -o zdtm_ct<br>
> === Run 1/1 ================ zdtm/static/file_locks08<br>
><br>
> ====================== Run zdtm/static/file_locks08 in h =======================<br>
> cc -g -O2 -Wall -Werror -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=0 -iquote ../lib/arch/x86/include -I../lib file_locks08.c ../lib/libzdtmtst.a ../lib/libzdtmtst.a -o file_locks08<br>
> Start test<br>
> ./file_locks08 --pidfile=file_locks08.pid --outfile=file_locks08.out --filename=file_locks08.test<br>
> Run criu dump<br>
> Run criu restore<br>
> Send the 15 signal to 39<br>
> Wait for zdtm/static/file_locks08(39) to die for 0.100000<br>
> Removing dump/zdtm/static/file_locks08/<wbr>39<br>
> ====================== Test zdtm/static/file_locks08 PASS ======================<br>
><br>
> ====================== Run zdtm/static/file_locks08 in ns ======================<br>
> Construct root for zdtm/static/file_locks08<br>
> Start test<br>
> ./file_locks08 --pidfile=file_locks08.pid --outfile=file_locks08.out --filename=file_locks08.test<br>
> Run criu dump<br>
> Run criu restore<br>
> 5: Old files lost: set(['1', '0', '3', '2', '4'])<br>
> 5: New files appeared: set([])<br>
> ############## Test zdtm/static/file_locks08 FAIL at fds compare ###############<br>
> Send the 9 signal to 100<br>
> ##############################<wbr>####### FAIL ##############################<wbr>#######<br>
><br>
> -- Pavel<br>
><br>
>> test/zdtm/static/file_locks08.<wbr>desc | 1 +<br>
>> test/zdtm/static/ofd_file_<wbr>locks.h | 153 ++++++++++++++++++++++++++++++<wbr>+++++++<br>
>> 8 files changed, 413 insertions(+)<br>
>> create mode 100644 test/zdtm/static/file_locks06.<wbr>c<br>
>> create mode 100644 test/zdtm/static/file_locks06.<wbr>desc<br>
>> create mode 100644 test/zdtm/static/file_locks07.<wbr>c<br>
>> create mode 100644 test/zdtm/static/file_locks07.<wbr>desc<br>
>> create mode 100644 test/zdtm/static/file_locks08.<wbr>c<br>
>> create mode 100644 test/zdtm/static/file_locks08.<wbr>desc<br>
>> create mode 100644 test/zdtm/static/ofd_file_<wbr>locks.h<br>
>><br>
>> diff --git a/test/zdtm/static/Makefile b/test/zdtm/static/Makefile<br>
>> index c932093..f270f9f 100644<br>
>> --- a/test/zdtm/static/Makefile<br>
>> +++ b/test/zdtm/static/Makefile<br>
>> @@ -221,6 +221,9 @@ TST_FILE = \<br>
>> file_locks03 \<br>
>> file_locks04 \<br>
>> file_locks05 \<br>
>> + file_locks06 \<br>
>> + file_locks07 \<br>
>> + file_locks08 \<br>
>> netns-nf \<br>
>> maps_file_prot \<br>
>> socket_close_data01 \<br>
>> diff --git a/test/zdtm/static/file_<wbr>locks06.c b/test/zdtm/static/file_<wbr>locks06.c<br>
>> new file mode 100644<br>
>> index 0000000..2e1ba43<br>
>> --- /dev/null<br>
>> +++ b/test/zdtm/static/file_<wbr>locks06.c<br>
>> @@ -0,0 +1,67 @@<br>
>> +#define _GNU_SOURCE<br>
>> +<br>
>> +#include <sys/file.h><br>
>> +#include <unistd.h><br>
>> +#include <fcntl.h><br>
>> +<br>
>> +#include "ofd_file_locks.h"<br>
>> +#include "zdtmtst.h"<br>
>> +<br>
>> +const char *test_doc = "Check that OFD lock for the whole file is restored";<br>
>> +const char *test_author = "Begunkov Pavel <<a href="mailto:asml.silence@gmail.com">asml.silence@gmail.com</a>>";<br>
>> +<br>
>> +char *filename;<br>
>> +TEST_OPTION(filename, string, "file name", 1);<br>
>> +<br>
>> +<br>
>> +int init_lock(int *fd, struct flock *lck)<br>
>> +{<br>
>> + *fd = open(filename, O_RDWR | O_CREAT, 0666);<br>
>> + if (*fd < 0) {<br>
>> + pr_perror("Can't open file");<br>
>> + return -1;<br>
>> + }<br>
>> +<br>
>> + lck->l_type = F_WRLCK;<br>
>> + lck->l_whence = SEEK_SET;<br>
>> + lck->l_start = 0;<br>
>> + lck->l_len = 0;<br>
>> + lck->l_pid = 0;<br>
>> +<br>
>> + if (fcntl(*fd, F_OFD_SETLK, lck) < 0) {<br>
>> + pr_perror("Can't set ofd lock");<br>
>> + return -1;<br>
>> + }<br>
>> + return 0;<br>
>> +}<br>
>> +<br>
>> +void cleanup(int *fd)<br>
>> +{<br>
>> + if (close(*fd))<br>
>> + pr_perror("Can't close fd\n");<br>
>> +<br>
>> + if (unlink(filename))<br>
>> + pr_perror("Can't unlink file\n");<br>
>> +}<br>
>> +<br>
>> +int main(int argc, char **argv)<br>
>> +{<br>
>> + int fd;<br>
>> + struct flock lck;<br>
>> +<br>
>> + test_init(argc, argv);<br>
>> + if (init_lock(&fd, &lck))<br>
>> + return 1;<br>
>> +<br>
>> + test_daemon();<br>
>> + test_waitsig();<br>
>> +<br>
>> + if (check_file_lock_restored(<wbr>getpid(), fd, &lck) ||<br>
>> + check_lock_exists(filename, &lck) < 0)<br>
>> + fail("OFD file locks check failed\n");<br>
>> + else<br>
>> + pass();<br>
>> +<br>
>> + cleanup(&fd);<br>
>> + return 0;<br>
>> +}<br>
>> diff --git a/test/zdtm/static/file_<wbr>locks06.desc b/test/zdtm/static/file_<wbr>locks06.desc<br>
>> new file mode 100644<br>
>> index 0000000..80cd04e<br>
>> --- /dev/null<br>
>> +++ b/test/zdtm/static/file_<wbr>locks06.desc<br>
>> @@ -0,0 +1 @@<br>
>> +{'flags': 'excl', 'opts': '--file-locks'}<br>
>> diff --git a/test/zdtm/static/file_<wbr>locks07.c b/test/zdtm/static/file_<wbr>locks07.c<br>
>> new file mode 100644<br>
>> index 0000000..1f94625<br>
>> --- /dev/null<br>
>> +++ b/test/zdtm/static/file_<wbr>locks07.c<br>
>> @@ -0,0 +1,101 @@<br>
>> +#define _GNU_SOURCE<br>
>> +<br>
>> +#include <sys/file.h><br>
>> +#include <unistd.h><br>
>> +#include <fcntl.h><br>
>> +<br>
>> +#include "ofd_file_locks.h"<br>
>> +#include "zdtmtst.h"<br>
>> +<br>
>> +const char *test_doc = "Check that 'overlapping' OFD read locks work";<br>
>> +const char *test_author = "Begunkov Pavel <<a href="mailto:asml.silence@gmail.com">asml.silence@gmail.com</a>>";<br>
>> +<br>
>> +char *filename;<br>
>> +TEST_OPTION(filename, string, "file name", 1);<br>
>> +<br>
>> +<br>
>> +#define FILE_NUM 4<br>
>> +static int fds[FILE_NUM];<br>
>> +static struct flock lcks[FILE_NUM];<br>
>> +static short types[] = {F_RDLCK, F_RDLCK, F_RDLCK, F_RDLCK};<br>
>> +static off_t starts[] = {0, 10, 0, 70};<br>
>> +static off_t lens[] = {20, 30, 100, 200};<br>
>> +<br>
>> +void fill_lock(struct flock *lock, off_t start, off_t len, short int type)<br>
>> +{<br>
>> + lock->l_start = start;<br>
>> + lock->l_len = len;<br>
>> + lock->l_type = type;<br>
>> + lock->l_whence = SEEK_SET;<br>
>> + lock->l_pid = 0;<br>
>> +}<br>
>> +<br>
>> +int init_file_locks(void)<br>
>> +{<br>
>> + size_t i;<br>
>> +<br>
>> + for (i = 0; i < FILE_NUM; ++i)<br>
>> + fill_lock(&lcks[i], starts[i], lens[i], types[i]);<br>
>> +<br>
>> + for (i = 0; i < FILE_NUM; ++i) {<br>
>> + fds[i] = open(filename, O_RDWR | O_CREAT, 0666);<br>
>> +<br>
>> + if (fds[i] < 0) {<br>
>> + pr_perror("Can't open file");<br>
>> + return -1;<br>
>> + }<br>
>> + }<br>
>> +<br>
>> + for (i = 0; i < FILE_NUM; ++i)<br>
>> + if (fcntl(fds[i], F_OFD_SETLKW, &lcks[i]) < 0) {<br>
>> + pr_perror("Can't set ofd lock");<br>
>> + return -1;<br>
>> + }<br>
>> +<br>
>> + return 0;<br>
>> +}<br>
>> +<br>
>> +void cleanup(void)<br>
>> +{<br>
>> + size_t i;<br>
>> +<br>
>> + for (i = 0; i < FILE_NUM; ++i)<br>
>> + if (close(fds[i]))<br>
>> + pr_perror("Can't close fd\n");<br>
>> +<br>
>> + if (unlink(filename))<br>
>> + pr_perror("Can't unlink file failed\n");<br>
>> +}<br>
>> +<br>
>> +int check_file_locks_restored(<wbr>void)<br>
>> +{<br>
>> + size_t i;<br>
>> + int pid = getpid();<br>
>> +<br>
>> + for (i = 0; i < FILE_NUM; ++i) {<br>
>> + if (check_file_lock_restored(pid, fds[i], &lcks[i]))<br>
>> + return -1;<br>
>> + if (check_lock_exists(filename, &lcks[i]) < 0)<br>
>> + return -1;<br>
>> + }<br>
>> +<br>
>> + return 0;<br>
>> +}<br>
>> +<br>
>> +int main(int argc, char **argv)<br>
>> +{<br>
>> + test_init(argc, argv);<br>
>> + if (init_file_locks())<br>
>> + return -1;<br>
>> +<br>
>> + test_daemon();<br>
>> + test_waitsig();<br>
>> +<br>
>> + if (check_file_locks_restored())<br>
>> + fail("OFD file locks check failed\n");<br>
>> + else<br>
>> + pass();<br>
>> +<br>
>> + cleanup();<br>
>> + return 0;<br>
>> +}<br>
>> diff --git a/test/zdtm/static/file_<wbr>locks07.desc b/test/zdtm/static/file_<wbr>locks07.desc<br>
>> new file mode 100644<br>
>> index 0000000..80cd04e<br>
>> --- /dev/null<br>
>> +++ b/test/zdtm/static/file_<wbr>locks07.desc<br>
>> @@ -0,0 +1 @@<br>
>> +{'flags': 'excl', 'opts': '--file-locks'}<br>
>> diff --git a/test/zdtm/static/file_<wbr>locks08.c b/test/zdtm/static/file_<wbr>locks08.c<br>
>> new file mode 100644<br>
>> index 0000000..a0d93dd<br>
>> --- /dev/null<br>
>> +++ b/test/zdtm/static/file_<wbr>locks08.c<br>
>> @@ -0,0 +1,86 @@<br>
>> +#define _GNU_SOURCE<br>
>> +<br>
>> +#include <sys/wait.h><br>
>> +#include <sys/file.h><br>
>> +#include <unistd.h><br>
>> +#include <fcntl.h><br>
>> +#include <stdlib.h><br>
>> +<br>
>> +#include "ofd_file_locks.h"<br>
>> +#include "zdtmtst.h"<br>
>> +<br>
>> +const char *test_doc = "Check that inherited OFD locks work";<br>
>> +const char *test_author = "Begunkov Pavel <<a href="mailto:asml.silence@gmail.com">asml.silence@gmail.com</a>>";<br>
>> +<br>
>> +char *filename;<br>
>> +TEST_OPTION(filename, string, "file name", 1);<br>
>> +<br>
>> +<br>
>> +int init_file_lock(int *fd, struct flock *lck)<br>
>> +{<br>
>> + *fd = open(filename, O_RDWR | O_CREAT, 0666);<br>
>> + if (*fd < 0) {<br>
>> + pr_perror("Can't open file");<br>
>> + return -1;<br>
>> + }<br>
>> +<br>
>> + lck->l_type = F_WRLCK;<br>
>> + lck->l_whence = SEEK_SET;<br>
>> + lck->l_start = 0;<br>
>> + lck->l_len = 0; /* lock whole file */<br>
>> + lck->l_pid = 0; /* should be 0 for ofd lock */<br>
>> +<br>
>> + if (fcntl(*fd, F_OFD_SETLKW, lck) < 0) {<br>
>> + pr_perror("Can't set ofd lock");<br>
>> + return -1;<br>
>> + }<br>
>> + return 0;<br>
>> +}<br>
>> +<br>
>> +void cleanup(int *fd)<br>
>> +{<br>
>> + if (close(*fd))<br>
>> + pr_perror("Can't close fd\n");<br>
>> +<br>
>> + if (unlink(filename))<br>
>> + pr_perror("Can't unlink file\n");<br>
>> +}<br>
>> +<br>
>> +int main(int argc, char **argv)<br>
>> +{<br>
>> + int fd;<br>
>> + int pid;<br>
>> + int status;<br>
>> + int ret = 0;<br>
>> + struct flock lck;<br>
>> +<br>
>> + test_init(argc, argv);<br>
>> + if (init_file_lock(&fd, &lck))<br>
>> + return -1;<br>
>> +<br>
>> + pid = fork();<br>
>> + if (pid == 0) {<br>
>> + if (check_file_lock_restored(<wbr>getpid(), fd, &lck) ||<br>
>> + check_lock_exists(filename, &lck) < 0)<br>
>> + ret = -1;<br>
>> + exit(ret);<br>
>> + }<br>
>> +<br>
>> + test_daemon();<br>
>> + test_waitsig();<br>
>> +<br>
>> + if (check_file_lock_restored(<wbr>getpid(), fd, &lck) ||<br>
>> + check_lock_exists(filename, &lck) < 0)<br>
>> + fail("OFD file locks check failed\n");<br>
>> +<br>
>> + kill(pid, SIGTERM);<br>
>> + ret = waitpid(pid, &status, 0);<br>
>> +<br>
>> + if (ret < 0 || !WIFEXITED(status) || WEXITSTATUS(status))<br>
>> + fail("OFD file locks check failed\n");<br>
>> + else<br>
>> + pass();<br>
>> +<br>
>> + cleanup(&fd);<br>
>> + return 0;<br>
>> +}<br>
>> diff --git a/test/zdtm/static/file_<wbr>locks08.desc b/test/zdtm/static/file_<wbr>locks08.desc<br>
>> new file mode 100644<br>
>> index 0000000..80cd04e<br>
>> --- /dev/null<br>
>> +++ b/test/zdtm/static/file_<wbr>locks08.desc<br>
>> @@ -0,0 +1 @@<br>
>> +{'flags': 'excl', 'opts': '--file-locks'}<br>
>> diff --git a/test/zdtm/static/ofd_file_<wbr>locks.h b/test/zdtm/static/ofd_file_<wbr>locks.h<br>
>> new file mode 100644<br>
>> index 0000000..049401a<br>
>> --- /dev/null<br>
>> +++ b/test/zdtm/static/ofd_file_<wbr>locks.h<br>
>> @@ -0,0 +1,153 @@<br>
>> +#ifndef ZDTM_OFD_FILE_LOCKS_H_<br>
>> +#define ZDTM_OFD_FILE_LOCKS_H_<br>
>> +<br>
>> +#include <sys/file.h><br>
>> +#include <unistd.h><br>
>> +#include <fcntl.h><br>
>> +#include <string.h><br>
>> +<br>
>> +#include "zdtmtst.h"<br>
>> +#include "fs.h"<br>
>> +<br>
>> +#ifndef F_OFD_GETLK<br>
>> +#define F_OFD_GETLK 36<br>
>> +#define F_OFD_SETLK 37<br>
>> +#define F_OFD_SETLKW 38<br>
>> +#endif<br>
>> +<br>
>> +/*<br>
>> + * Header library for parsing of OFD locks<br>
>> + * from procfs and checking them after restoring.<br>
>> + */<br>
>> +<br>
>> +static int parse_ofd_lock(char *buf, struct flock *lck)<br>
>> +{<br>
>> + char fl_flag[10], fl_type[15], fl_option[10], fl_end[32];<br>
>> + long long start;<br>
>> + int num;<br>
>> +<br>
>> + if (strncmp(buf, "lock:\t", 6) != 0)<br>
>> + return 1; /* isn't lock, skip record */<br>
>> +<br>
>> + num = sscanf(buf,<br>
>> + "%*s %*d: %s %s %s %*d %*x:%*x:%*d %lld %s",<br>
>> + fl_flag, fl_type, fl_option, &start, fl_end);<br>
>> +<br>
>> + if (num < 4) {<br>
>> + pr_err("Invalid lock info %s\n", buf);<br>
>> + return -1;<br>
>> + }<br>
>> + if (strcmp(fl_flag, "OFDLCK"))<br>
>> + return 1;<br>
>> +<br>
>> + lck->l_start = start;<br>
>> +<br>
>> + if (strcmp(fl_end, "EOF")) {<br>
>> + unsigned long end;<br>
>> +<br>
>> + if (sscanf(fl_end, "%lu", &end) <= 0) {<br>
>> + pr_err("Invalid lock entry\n");<br>
>> + return -1;<br>
>> + }<br>
>> + lck->l_len = end - lck->l_start + 1;<br>
>> + } else {<br>
>> + lck->l_len = 0;<br>
>> + }<br>
>> + if (strcmp(fl_option, "WRITE") == 0)<br>
>> + lck->l_type = F_WRLCK;<br>
>> + else<br>
>> + lck->l_type = F_RDLCK;<br>
>> +<br>
>> + return 0;<br>
>> +}<br>
>> +<br>
>> +static int read_fd_ofd_lock(int pid, int fd, struct flock *lck)<br>
>> +{<br>
>> + char path[PATH_MAX];<br>
>> + char buf[100];<br>
>> + int num;<br>
>> + FILE *proc_file = NULL;<br>
>> +<br>
>> + sprintf(path, "/proc/%i/fdinfo/%i", pid, fd);<br>
>> + proc_file = fopen(path, "r");<br>
>> +<br>
>> + if (!proc_file) {<br>
>> + pr_err("Can't open %s\n", path);<br>
>> + return -1;<br>
>> + }<br>
>> +<br>
>> + num = -1;<br>
>> + while (fgets(buf, sizeof(buf), proc_file)) {<br>
>> + num = parse_ofd_lock(buf, lck);<br>
>> + if (num <= 0)<br>
>> + break;<br>
>> + }<br>
>> +<br>
>> + if (fclose(proc_file)) {<br>
>> + pr_err("Can't close %s\n", path);<br>
>> + return -1;<br>
>> + }<br>
>> + return num;<br>
>> +}<br>
>> +<br>
>> +static int check_lock_exists(const char *filename, struct flock *lck)<br>
>> +{<br>
>> + int ret = -1;<br>
>> + int fd;<br>
>> +<br>
>> + fd = open(filename, O_RDWR, 0666);<br>
>> +<br>
>> + if (lck->l_type == F_RDLCK) {<br>
>> + /* check, that there is no write lock */<br>
>> + ret = fcntl(fd, F_OFD_GETLK, lck);<br>
>> + if (ret) {<br>
>> + pr_err("fcntl failed (%i)\n", ret);<br>
>> + goto out;<br>
>> + }<br>
>> + if (lck->l_type != F_UNLCK) {<br>
>> + pr_err("OFD lock type do not match\n");<br>
>> + goto out;<br>
>> + }<br>
>> + }<br>
>> +<br>
>> + /* check, that lock is set */<br>
>> + lck->l_type = F_WRLCK;<br>
>> + ret = fcntl(fd, F_OFD_GETLK, lck);<br>
>> + if (ret) {<br>
>> + pr_err("fcntl failed (%i)\n", ret);<br>
>> + goto out;<br>
>> + }<br>
>> + if (lck->l_type == F_UNLCK) {<br>
>> + pr_err("Lock not found\n");<br>
>> + goto out;<br>
>> + }<br>
>> +<br>
>> + ret = 0;<br>
>> +out:<br>
>> + if (close(fd))<br>
>> + return -1;<br>
>> + return ret;<br>
>> +}<br>
>> +<br>
>> +static int check_file_locks_match(struct flock *orig_lck, struct flock *lck)<br>
>> +{<br>
>> + return orig_lck->l_start == lck->l_start &&<br>
>> + orig_lck->l_len == lck->l_len &&<br>
>> + orig_lck->l_type == lck->l_type;<br>
>> +}<br>
>> +<br>
>> +static int check_file_lock_restored(int pid, int fd, struct flock *lck)<br>
>> +{<br>
>> + struct flock lck_restored;<br>
>> +<br>
>> + if (read_fd_ofd_lock(pid, fd, &lck_restored))<br>
>> + return -1;<br>
>> +<br>
>> + if (!check_file_locks_match(lck, &lck_restored)) {<br>
>> + pr_err("Can't restore file lock (fd: %i)\n", fd);<br>
>> + return -1;<br>
>> + }<br>
>> + return 0;<br>
>> +}<br>
>> +<br>
>> +#endif /* ZDTM_OFD_FILE_LOCKS_H_ */<br>
>><br>
><br>
<br>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature">Best regards,<br>Eugene Batalov.</div>
</div>