[CRIU] [PATCH 2/3] test: static,file_locks02 -- Use fdinfo output

Cyrill Gorcunov gorcunov at openvz.org
Thu Aug 3 14:23:32 MSK 2017


Signed-off-by: Cyrill Gorcunov <gorcunov at openvz.org>
---
 test/zdtm/static/file_locks02.c | 72 +++++++++++++++++++++++------------------
 1 file changed, 40 insertions(+), 32 deletions(-)

diff --git a/test/zdtm/static/file_locks02.c b/test/zdtm/static/file_locks02.c
index 4e2e31b60ef5..2fbbb72cc400 100644
--- a/test/zdtm/static/file_locks02.c
+++ b/test/zdtm/static/file_locks02.c
@@ -5,6 +5,7 @@
 #include <sys/file.h>
 #include <string.h>
 #include <sys/wait.h>
+#include <linux/limits.h>
 
 #include "zdtmtst.h"
 
@@ -14,55 +15,59 @@ const char *test_author	= "Pavel Emelyanov <xemul at parallels.com>";
 char *filename;
 TEST_OPTION(filename, string, "file name", 1);
 
-static int check_file_locks(pid_t child)
+static int check_file_lock(pid_t pid, int fd, char *expected_type,
+			   char *expected_option)
 {
-	FILE		*fp_locks = NULL;
-	char		buf[100], fl_flag[16], fl_type[16], fl_option[16];
-	pid_t		pid = getpid();
-	int		found = 0, num, fl_owner;
-
-	fp_locks = fopen("/proc/locks", "r");
-	if (!fp_locks)
+	char buf[100], fl_flag[16], fl_type[16], fl_option[16];
+	int found = 0, num, fl_owner;
+	FILE *fp_locks = NULL;
+	char path[PATH_MAX];
+
+	test_msg("check_file_lock: (pid %d) expecting fd %d type %s option %s\n",
+		 pid, fd, expected_type, expected_option);
+
+	snprintf(path, sizeof(path), "/proc/self/fdinfo/%d", fd);
+	fp_locks = fopen(path, "r");
+	if (!fp_locks) {
+		pr_err("Can't open %s\n", path);
 		return -1;
-
-	test_msg("C: %d\n", pid);
+	}
 
 	while (fgets(buf, sizeof(buf), fp_locks)) {
-		test_msg("c: %s", buf);
-
-		if (strstr(buf, "->"))
+		if (strncmp(buf, "lock:\t", 6) != 0)
 			continue;
+		test_msg("c: %s", buf);
 
-		num = sscanf(buf,
-			"%*d:%s %s %s %d %*02x:%*02x:%*d %*d %*s",
-			fl_flag, fl_type, fl_option, &fl_owner);
+		memset(fl_flag, 0, sizeof(fl_flag));
+		memset(fl_type, 0, sizeof(fl_type));
+		memset(fl_option, 0, sizeof(fl_option));
 
+		num = sscanf(buf, "%*s %*d:%s %s %s %d",
+			     fl_flag, fl_type, fl_option, &fl_owner);
 		if (num < 4) {
 			pr_perror("Invalid lock info.");
 			break;
 		}
 
-		if (fl_owner != pid && fl_owner != child)
+		if (fl_owner != pid)
 			continue;
-
-		if (!strcmp(fl_flag, "FLOCK") &&
-				!strcmp(fl_type, "ADVISORY") &&
-				!strcmp(fl_option, "WRITE"))
-			found++;
-
-		memset(fl_flag, 0, sizeof(fl_flag));
-		memset(fl_type, 0, sizeof(fl_type));
-		memset(fl_option, 0, sizeof(fl_option));
+		if (strcmp(fl_flag, "FLOCK"))
+			continue;
+		if (strcmp(fl_type, expected_type))
+			continue;
+		if (strcmp(fl_option, expected_option))
+			continue;
+		found++;
 	}
 
 	fclose(fp_locks);
 
-	return found == 1;
+	return found == 1 ? 0 : -1;
 }
 
 int main(int argc, char **argv)
 {
-	int fd, pid;
+	int fd, pid, ret = 0;
 
 	test_init(argc, argv);
 
@@ -83,15 +88,18 @@ int main(int argc, char **argv)
 	test_daemon();
 	test_waitsig();
 
-	if (check_file_locks(pid))
-		pass();
-	else
+	if (check_file_lock(getpid(), fd, "ADVISORY", "WRITE")) {
 		fail("Flock file locks check failed");
+		ret |= 1;
+	}
+
+	if (!ret)
+		pass();
 
 	kill(pid, SIGTERM);
 	waitpid(pid, NULL, 0);
 	close(fd);
 	unlink(filename);
 
-	return 0;
+	return ret;
 }
-- 
2.7.5



More information about the CRIU mailing list