Skip to content

Commit

Permalink
test/zdtm: use flock64 instead of flock
Browse files Browse the repository at this point in the history
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 it.

Signed-off-by: Dmitry Safonov <dsafonov@virtuozzo.com>
  • Loading branch information
0x7f454c46 committed Jan 19, 2017
1 parent d6bcd38 commit 9bc80a7
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 22 deletions.
34 changes: 24 additions & 10 deletions test/zdtm/static/file_locks00.c
Expand Up @@ -24,24 +24,25 @@ char file1[PATH_MAX];
static int lock_reg(int fd, int cmd, int type, int whence,
off_t offset, off_t len)
{
struct flock lock;
struct flock64 lock;

lock.l_type = type; /* F_RDLCK, F_WRLCK, F_UNLCK */
lock.l_whence = whence; /* SEEK_SET, SEEK_CUR, SEEK_END */
lock.l_start = offset; /* byte offset, relative to l_whence */
lock.l_len = len; /* #bytes (0 means to EOF) */

errno = 0;
return fcntl(fd, cmd, &lock);
}

#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) \
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)
{
struct flock lock;
struct flock64 lock;
int ret;

lock.l_type = F_RDLCK; /* F_RDLCK, F_WRLCK, F_UNLCK */
Expand All @@ -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_pid = -1;

ret = fcntl(fd, F_GETLK, &lock);
errno = 0;
ret = fcntl(fd, F_GETLK64, &lock);
if (ret == -1) {
pr_perror("F_GETLK failed.");
return -1;
Expand All @@ -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)
{
struct flock lock;
struct flock64 lock;

int ret;
pid_t ppid = getppid();
Expand All @@ -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_pid = -1;

ret = fcntl(fd, F_GETLK, &lock);
errno = 0;
ret = fcntl(fd, F_GETLK64, &lock);
if (ret == -1) {
pr_perror("F_GETLK failed.");
return -1;
Expand Down Expand Up @@ -129,7 +132,7 @@ static int check_file_locks()

int main(int argc, char **argv)
{
int fd_0, fd_1;
int fd_0, fd_1, ret;
pid_t pid;

test_init(argc, argv);
Expand Down Expand Up @@ -168,8 +171,19 @@ int main(int argc, char **argv)
exit(0);
}

set_read_lock(fd_0, SEEK_SET, 0, 0);
set_write_lock(fd_1, SEEK_SET, 0, 0);
ret = set_read_lock(fd_0, 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_waitsig();
Expand Down
4 changes: 2 additions & 2 deletions test/zdtm/static/file_locks06.c
Expand Up @@ -14,7 +14,7 @@ char *filename;
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);
if (*fd < 0) {
Expand Down Expand Up @@ -47,7 +47,7 @@ void cleanup(int *fd)
int main(int argc, char **argv)
{
int fd;
struct flock lck;
struct flock64 lck;

test_init(argc, argv);
if (init_lock(&fd, &lck))
Expand Down
4 changes: 2 additions & 2 deletions test/zdtm/static/file_locks07.c
Expand Up @@ -16,12 +16,12 @@ TEST_OPTION(filename, string, "file name", 1);

#define FILE_NUM 4
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 off_t starts[] = {0, 10, 0, 70};
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_len = len;
Expand Down
4 changes: 2 additions & 2 deletions test/zdtm/static/file_locks08.c
Expand Up @@ -16,7 +16,7 @@ char *filename;
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);
if (*fd < 0) {
Expand Down Expand Up @@ -53,7 +53,7 @@ int main(int argc, char **argv)
int status;
int ret = 0;
task_waiter_t tw;
struct flock lck;
struct flock64 lck;

test_init(argc, argv);
if (init_file_lock(&fd, &lck))
Expand Down
12 changes: 6 additions & 6 deletions test/zdtm/static/ofd_file_locks.h
Expand Up @@ -20,7 +20,7 @@
* 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];
long long start;
Expand Down Expand Up @@ -61,7 +61,7 @@ static int parse_ofd_lock(char *buf, struct flock *lck)
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 buf[100];
Expand Down Expand Up @@ -90,7 +90,7 @@ static int read_fd_ofd_lock(int pid, int fd, struct flock *lck)
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 fd;
Expand Down Expand Up @@ -129,16 +129,16 @@ static int check_lock_exists(const char *filename, struct flock *lck)
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 &&
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 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))
return -1;
Expand Down

0 comments on commit 9bc80a7

Please sign in to comment.