Commit 2062abf8 authored by Cyrill Gorcunov's avatar Cyrill Gorcunov Committed by Andrei Vagin

test: static,file_locks01 -- Use fdinfo output

Signed-off-by: 's avatarCyrill Gorcunov <gorcunov@openvz.org>
Signed-off-by: 's avatarAndrei Vagin <avagin@virtuozzo.com>
parent be98273c
...@@ -27,7 +27,7 @@ TEST_OPTION(filename, string, "file name", 1); ...@@ -27,7 +27,7 @@ TEST_OPTION(filename, string, "file name", 1);
char file0[PATH_MAX]; char file0[PATH_MAX];
char file1[PATH_MAX]; char file1[PATH_MAX];
char file2[PATH_MAX]; char file2[PATH_MAX];
unsigned int inodes[3]; unsigned long inodes[3];
static mnt_info_t *m; static mnt_info_t *m;
dev_t dev; dev_t dev;
...@@ -72,91 +72,80 @@ static int open_all_files(int *fd_0, int *fd_1, int *fd_2) ...@@ -72,91 +72,80 @@ static int open_all_files(int *fd_0, int *fd_1, int *fd_2)
pr_perror("Unable to open file %s", file1); pr_perror("Unable to open file %s", file1);
return -1; return -1;
} }
fstat(*fd_2, &buf); fstat(*fd_2, &buf);
inodes[2] = buf.st_ino; inodes[2] = buf.st_ino;
return 0; return 0;
} }
static int check_file_locks() static int check_file_lock(int fd, char *expected_type,
char *expected_option,
unsigned int expected_dev,
unsigned long expected_ino)
{ {
FILE *fp_locks = NULL; char buf[100], fl_flag[16], fl_type[16], fl_option[16];
char buf[100]; int found = 0, num, fl_owner;
FILE *fp_locks = NULL;
long long fl_id = 0; char path[PATH_MAX];
char fl_flag[10], fl_type[15], fl_option[10]; unsigned long i_no;
pid_t fl_owner; int maj, min;
int maj, min;
unsigned long i_no; test_msg("check_file_lock: (fsname %s) expecting fd %d type %s option %s dev %u ino %lu\n",
long long start; m->fsname, fd, expected_type, expected_option, expected_dev, expected_ino);
char end[32];
snprintf(path, sizeof(path), "/proc/self/fdinfo/%d", fd);
int num; fp_locks = fopen(path, "r");
int count = 3; if (!fp_locks) {
pr_err("Can't open %s\n", path);
fp_locks = fopen("/proc/locks", "r");
if (!fp_locks)
return -1; return -1;
}
test_msg("C: %d/%d/%d\n", inodes[0], inodes[1], inodes[2]);
while (fgets(buf, sizeof(buf), fp_locks)) { while (fgets(buf, sizeof(buf), fp_locks)) {
test_msg("c: %s", buf); if (strncmp(buf, "lock:\t", 6) != 0)
if (strstr(buf, "->"))
continue; continue;
test_msg("c: %s", buf);
num = sscanf(buf, memset(fl_flag, 0, sizeof(fl_flag));
"%lld:%s %s %s %d %x:%x:%ld %lld %s", memset(fl_type, 0, sizeof(fl_type));
&fl_id, fl_flag, fl_type, fl_option, memset(fl_option, 0, sizeof(fl_option));
&fl_owner, &maj, &min, &i_no, &start, end);
if (num < 10) { num = sscanf(buf, "%*s %*d:%s %s %s %d %02x:%02x:%ld %*d %*s",
fl_flag, fl_type, fl_option, &fl_owner,
&maj, &min, &i_no);
if (num < 7) {
pr_perror("Invalid lock info."); pr_perror("Invalid lock info.");
break; break;
} }
if (i_no != inodes[0] && i_no != inodes[1] && i_no != inodes[2])
continue;
if (!strcmp(m->fsname, "btrfs")) { if (!strcmp(m->fsname, "btrfs")) {
if (MKKDEV(major(maj), minor(min)) != dev) if (MKKDEV(major(maj), minor(min)) != expected_dev)
continue; continue;
} else { } else {
if (makedev(maj, min) != dev) if (makedev(maj, min) != expected_dev)
continue; continue;
} }
if (!strcmp(fl_flag, "FLOCK") && !strcmp(fl_type, "ADVISORY")) { if (fl_owner != getpid())
if (!strcmp(fl_option, "READ")) continue;
count--; if (i_no != expected_ino)
else if (!strcmp(fl_option, "WRITE")) continue;
count--; if (strcmp(fl_flag, "FLOCK"))
} continue;
if (strcmp(fl_type, expected_type))
if (!strcmp(fl_flag, "FLOCK") && continue;
!strcmp(fl_type, "MSNFS") && if (strcmp(fl_option, expected_option))
!strcmp(fl_option, "READ")) continue;
count--; found++;
memset(fl_flag, 0, sizeof(fl_flag));
memset(fl_type, 0, sizeof(fl_type));
memset(fl_option, 0, sizeof(fl_option));
} }
fclose(fp_locks); fclose(fp_locks);
/* return found == 1 ? 0 : -1;
* If we find all three matched file locks, count would be 0,
* return 0 for success.
*/
return count;
} }
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int fd_0, fd_1, fd_2; int fd_0, fd_1, fd_2, ret = 0;
test_init(argc, argv); test_init(argc, argv);
...@@ -178,9 +167,20 @@ int main(int argc, char **argv) ...@@ -178,9 +167,20 @@ int main(int argc, char **argv)
test_daemon(); test_daemon();
test_waitsig(); test_waitsig();
if (check_file_locks()) if (check_file_lock(fd_0, "ADVISORY", "READ", dev, inodes[0])) {
fail("Flock file locks check failed"); fail("Failed on fd %d", fd_0);
else ret |= 1;
}
if (check_file_lock(fd_1, "ADVISORY", "WRITE", dev, inodes[1])) {
fail("Failed on fd %d", fd_1);
ret |= 1;
}
if (check_file_lock(fd_2, "MSNFS", "READ", dev, inodes[2])) {
fail("Failed on fd %d", fd_2);
ret |= 1;
}
if (!ret)
pass(); pass();
close(fd_0); close(fd_0);
...@@ -190,5 +190,5 @@ int main(int argc, char **argv) ...@@ -190,5 +190,5 @@ int main(int argc, char **argv)
unlink(file1); unlink(file1);
unlink(file2); unlink(file2);
return 0; return ret;
} }
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