Commit 86f811eb authored by Dmitry Safonov's avatar Dmitry Safonov Committed by Andrei Vagin

test/zdtm: use flock64 instead of flock

For 32-bit fcntl() Glibc function calls sys_fcntl64(), which
needs struct flock64, otherwise the kernel gets a wrong struct.
For 64-bit, it's all the same.

Also unset errno before fcntl() and check return value of the call.

Cc: Qiang Huang <h.huangqiang@huawei.com>
Cc: Begunkov Pavel <asml.silence@gmail.com>
Cc: Pavel Emelyanov <xemul@virtuozzo.com>
travis-ci: success for test/zdtm: use flock64 instead of flock
Signed-off-by: 's avatarDmitry Safonov <dsafonov@virtuozzo.com>
Signed-off-by: 's avatarPavel Emelyanov <xemul@virtuozzo.com>
Signed-off-by: 's avatarAndrei Vagin <avagin@virtuozzo.com>
parent 6cacaa69
...@@ -24,24 +24,25 @@ char file1[PATH_MAX]; ...@@ -24,24 +24,25 @@ char file1[PATH_MAX];
static int lock_reg(int fd, int cmd, int type, int whence, static int lock_reg(int fd, int cmd, int type, int whence,
off_t offset, off_t len) off_t offset, off_t len)
{ {
struct flock lock; struct flock64 lock;
lock.l_type = type; /* F_RDLCK, F_WRLCK, F_UNLCK */ lock.l_type = type; /* F_RDLCK, F_WRLCK, F_UNLCK */
lock.l_whence = whence; /* SEEK_SET, SEEK_CUR, SEEK_END */ lock.l_whence = whence; /* SEEK_SET, SEEK_CUR, SEEK_END */
lock.l_start = offset; /* byte offset, relative to l_whence */ lock.l_start = offset; /* byte offset, relative to l_whence */
lock.l_len = len; /* #bytes (0 means to EOF) */ lock.l_len = len; /* #bytes (0 means to EOF) */
errno = 0;
return fcntl(fd, cmd, &lock); return fcntl(fd, cmd, &lock);
} }
#define set_read_lock(fd, whence, offset, len) \ #define set_read_lock(fd, whence, offset, len) \
lock_reg(fd, F_SETLK, F_RDLCK, whence, offset, len) lock_reg(fd, F_SETLK64, F_RDLCK, whence, offset, len)
#define set_write_lock(fd, whence, offset, len) \ #define set_write_lock(fd, whence, offset, len) \
lock_reg(fd, F_SETLK, F_WRLCK, whence, offset, len) lock_reg(fd, F_SETLK64, F_WRLCK, whence, offset, len)
static int check_read_lock(int fd, int whence, off_t offset, off_t len) static int check_read_lock(int fd, int whence, off_t offset, off_t len)
{ {
struct flock lock; struct flock64 lock;
int ret; int ret;
lock.l_type = F_RDLCK; /* F_RDLCK, F_WRLCK, F_UNLCK */ lock.l_type = F_RDLCK; /* F_RDLCK, F_WRLCK, F_UNLCK */
...@@ -50,7 +51,8 @@ static int check_read_lock(int fd, int whence, off_t offset, off_t len) ...@@ -50,7 +51,8 @@ static int check_read_lock(int fd, int whence, off_t offset, off_t len)
lock.l_len = len; /* #bytes (0 means to EOF) */ lock.l_len = len; /* #bytes (0 means to EOF) */
lock.l_pid = -1; lock.l_pid = -1;
ret = fcntl(fd, F_GETLK, &lock); errno = 0;
ret = fcntl(fd, F_GETLK64, &lock);
if (ret == -1) { if (ret == -1) {
pr_perror("F_GETLK failed."); pr_perror("F_GETLK failed.");
return -1; return -1;
...@@ -67,7 +69,7 @@ static int check_read_lock(int fd, int whence, off_t offset, off_t len) ...@@ -67,7 +69,7 @@ static int check_read_lock(int fd, int whence, off_t offset, off_t len)
static int check_write_lock(int fd, int whence, off_t offset, off_t len) static int check_write_lock(int fd, int whence, off_t offset, off_t len)
{ {
struct flock lock; struct flock64 lock;
int ret; int ret;
pid_t ppid = getppid(); pid_t ppid = getppid();
...@@ -78,7 +80,8 @@ static int check_write_lock(int fd, int whence, off_t offset, off_t len) ...@@ -78,7 +80,8 @@ static int check_write_lock(int fd, int whence, off_t offset, off_t len)
lock.l_len = len; /* #bytes (0 means to EOF) */ lock.l_len = len; /* #bytes (0 means to EOF) */
lock.l_pid = -1; lock.l_pid = -1;
ret = fcntl(fd, F_GETLK, &lock); errno = 0;
ret = fcntl(fd, F_GETLK64, &lock);
if (ret == -1) { if (ret == -1) {
pr_perror("F_GETLK failed."); pr_perror("F_GETLK failed.");
return -1; return -1;
...@@ -129,7 +132,7 @@ static int check_file_locks() ...@@ -129,7 +132,7 @@ static int check_file_locks()
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int fd_0, fd_1; int fd_0, fd_1, ret;
pid_t pid; pid_t pid;
test_init(argc, argv); test_init(argc, argv);
...@@ -168,8 +171,19 @@ int main(int argc, char **argv) ...@@ -168,8 +171,19 @@ int main(int argc, char **argv)
exit(0); exit(0);
} }
set_read_lock(fd_0, SEEK_SET, 0, 0); ret = set_read_lock(fd_0, SEEK_SET, 0, 0);
set_write_lock(fd_1, SEEK_SET, 0, 0); if (ret == -1) {
pr_perror("Failed to set read lock");
kill(pid, SIGTERM);
return -1;
}
ret = set_write_lock(fd_1, SEEK_SET, 0, 0);
if (ret == -1) {
pr_perror("Failed to set write lock");
kill(pid, SIGTERM);
return -1;
}
test_daemon(); test_daemon();
test_waitsig(); test_waitsig();
......
...@@ -14,7 +14,7 @@ char *filename; ...@@ -14,7 +14,7 @@ char *filename;
TEST_OPTION(filename, string, "file name", 1); TEST_OPTION(filename, string, "file name", 1);
int init_lock(int *fd, struct flock *lck) int init_lock(int *fd, struct flock64 *lck)
{ {
*fd = open(filename, O_RDWR | O_CREAT, 0666); *fd = open(filename, O_RDWR | O_CREAT, 0666);
if (*fd < 0) { if (*fd < 0) {
...@@ -47,7 +47,7 @@ void cleanup(int *fd) ...@@ -47,7 +47,7 @@ void cleanup(int *fd)
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int fd; int fd;
struct flock lck; struct flock64 lck;
test_init(argc, argv); test_init(argc, argv);
if (init_lock(&fd, &lck)) if (init_lock(&fd, &lck))
......
...@@ -16,12 +16,12 @@ TEST_OPTION(filename, string, "file name", 1); ...@@ -16,12 +16,12 @@ TEST_OPTION(filename, string, "file name", 1);
#define FILE_NUM 4 #define FILE_NUM 4
static int fds[FILE_NUM]; static int fds[FILE_NUM];
static struct flock lcks[FILE_NUM]; static struct flock64 lcks[FILE_NUM];
static short types[] = {F_RDLCK, F_RDLCK, F_RDLCK, F_RDLCK}; static short types[] = {F_RDLCK, F_RDLCK, F_RDLCK, F_RDLCK};
static off_t starts[] = {0, 10, 0, 70}; static off_t starts[] = {0, 10, 0, 70};
static off_t lens[] = {20, 30, 100, 200}; static off_t lens[] = {20, 30, 100, 200};
void fill_lock(struct flock *lock, off_t start, off_t len, short int type) void fill_lock(struct flock64 *lock, off_t start, off_t len, short int type)
{ {
lock->l_start = start; lock->l_start = start;
lock->l_len = len; lock->l_len = len;
......
...@@ -16,7 +16,7 @@ char *filename; ...@@ -16,7 +16,7 @@ char *filename;
TEST_OPTION(filename, string, "file name", 1); TEST_OPTION(filename, string, "file name", 1);
int init_file_lock(int *fd, struct flock *lck) int init_file_lock(int *fd, struct flock64 *lck)
{ {
*fd = open(filename, O_RDWR | O_CREAT, 0666); *fd = open(filename, O_RDWR | O_CREAT, 0666);
if (*fd < 0) { if (*fd < 0) {
...@@ -53,7 +53,7 @@ int main(int argc, char **argv) ...@@ -53,7 +53,7 @@ int main(int argc, char **argv)
int status; int status;
int ret = 0; int ret = 0;
task_waiter_t tw; task_waiter_t tw;
struct flock lck; struct flock64 lck;
test_init(argc, argv); test_init(argc, argv);
if (init_file_lock(&fd, &lck)) if (init_file_lock(&fd, &lck))
......
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
* from procfs and checking them after restoring. * from procfs and checking them after restoring.
*/ */
static int parse_ofd_lock(char *buf, struct flock *lck) static int parse_ofd_lock(char *buf, struct flock64 *lck)
{ {
char fl_flag[10], fl_type[15], fl_option[10], fl_end[32]; char fl_flag[10], fl_type[15], fl_option[10], fl_end[32];
long long start; long long start;
...@@ -61,7 +61,7 @@ static int parse_ofd_lock(char *buf, struct flock *lck) ...@@ -61,7 +61,7 @@ static int parse_ofd_lock(char *buf, struct flock *lck)
return 0; return 0;
} }
static int read_fd_ofd_lock(int pid, int fd, struct flock *lck) static int read_fd_ofd_lock(int pid, int fd, struct flock64 *lck)
{ {
char path[PATH_MAX]; char path[PATH_MAX];
char buf[100]; char buf[100];
...@@ -90,7 +90,7 @@ static int read_fd_ofd_lock(int pid, int fd, struct flock *lck) ...@@ -90,7 +90,7 @@ static int read_fd_ofd_lock(int pid, int fd, struct flock *lck)
return num; return num;
} }
static int check_lock_exists(const char *filename, struct flock *lck) static int check_lock_exists(const char *filename, struct flock64 *lck)
{ {
int ret = -1; int ret = -1;
int fd; int fd;
...@@ -129,16 +129,16 @@ out: ...@@ -129,16 +129,16 @@ out:
return ret; return ret;
} }
static int check_file_locks_match(struct flock *orig_lck, struct flock *lck) static int check_file_locks_match(struct flock64 *orig_lck, struct flock64 *lck)
{ {
return orig_lck->l_start == lck->l_start && return orig_lck->l_start == lck->l_start &&
orig_lck->l_len == lck->l_len && orig_lck->l_len == lck->l_len &&
orig_lck->l_type == lck->l_type; orig_lck->l_type == lck->l_type;
} }
static int check_file_lock_restored(int pid, int fd, struct flock *lck) static int check_file_lock_restored(int pid, int fd, struct flock64 *lck)
{ {
struct flock lck_restored; struct flock64 lck_restored;
if (read_fd_ofd_lock(pid, fd, &lck_restored)) if (read_fd_ofd_lock(pid, fd, &lck_restored))
return -1; return -1;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment