public inbox for fstests@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] generic/484: Need another process to check record locks
@ 2018-05-21  5:42 Xiao Yang
  2018-05-22  2:36 ` Xiong Murphy Zhou
  0 siblings, 1 reply; 4+ messages in thread
From: Xiao Yang @ 2018-05-21  5:42 UTC (permalink / raw)
  To: fstests; +Cc: guaneryu, xzhou, jlayton, Xiao Yang

1) According to fcntl(2) manpage, A single process always gets F_UNLCK in
   the l_type field when using fcntl(F_GETLK) to acquire the existing lock
   set by itself because it could convert the existing lock to a new lock
   unconditionally.  So we need another process to check if the lock exists.

2) Remove redundant exit(0).

Signed-off-by: Xiao Yang <yangx.jy@cn.fujitsu.com>
---
 src/t_locks_execve.c | 24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)

diff --git a/src/t_locks_execve.c b/src/t_locks_execve.c
index 9ad2dc3..d99d7de 100644
--- a/src/t_locks_execve.c
+++ b/src/t_locks_execve.c
@@ -8,6 +8,7 @@
 #include <errno.h>
 #include <pthread.h>
 #include <unistd.h>
+#include <sys/types.h>
 #include <sys/wait.h>
 
 static void err_exit(char *op, int errn)
@@ -32,12 +33,24 @@ struct flock fl = {
 
 static void checklock(int fd)
 {
-	if (fcntl(fd, F_GETLK, &fl) < 0)
-		err_exit("getlk", errno);
-	if (fl.l_type == F_UNLCK) {
-		printf("record lock is not preserved across execve(2)\n");
-		exit(1);
+	pid_t pid;
+
+	pid = fork();
+	if (pid < 0)
+		err_exit("fork", errno);
+
+	if (!pid) {
+		if (fcntl(fd, F_GETLK, &fl) < 0)
+			err_exit("getlk", errno);
+		if (fl.l_type == F_UNLCK) {
+			printf("record lock is not preserved across execve(2)\n");
+			exit(1);
+		}
+		exit(0);
 	}
+
+	waitpid(pid, NULL, 0);
+
 	exit(0);
 }
 
@@ -52,7 +65,6 @@ int main(int argc, char **argv)
 	if (argc == 3) {
 		fd = atoi(argv[2]);
 		checklock(fd);
-		exit(0);
 	}
 
 	fd = open(argv[1], O_WRONLY|O_CREAT, 0755);
-- 
1.8.3.1




^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2018-05-22 13:57 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-05-21  5:42 [PATCH] generic/484: Need another process to check record locks Xiao Yang
2018-05-22  2:36 ` Xiong Murphy Zhou
2018-05-22  2:48   ` Xiao Yang
2018-05-22 13:57     ` Xiong Murphy Zhou

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox