From: Xiao Yang Date: Mon, 21 May 2018 05:42:00 +0000 (+0800) Subject: generic/484: Need another process to check record locks X-Git-Tag: v2022.05.01~1536 X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=69b7f3c4f7203ba8efe08c0408cab157f544f39b;p=users%2Fhch%2Fxfstests-dev.git generic/484: Need another process to check record locks 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. Also remove redundant exit(0). Signed-off-by: Xiao Yang Reviewed-by: Xiong Zhou Signed-off-by: Eryu Guan --- diff --git a/src/t_locks_execve.c b/src/t_locks_execve.c index 9ad2dc370..d99d7de70 100644 --- a/src/t_locks_execve.c +++ b/src/t_locks_execve.c @@ -8,6 +8,7 @@ #include #include #include +#include #include 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);