All of lore.kernel.org
 help / color / mirror / Atom feed
* [LTP] [PATCH 1/5] syscalls/flock01: Rewrite to new library
@ 2018-08-02  3:53 Jinhui huang
  2018-08-02  3:53 ` [LTP] [PATCH 2/5] syscalls/flock02: " Jinhui huang
                   ` (4 more replies)
  0 siblings, 5 replies; 13+ messages in thread
From: Jinhui huang @ 2018-08-02  3:53 UTC (permalink / raw)
  To: ltp

Signed-off-by: Jinhui huang <huangjh.jy@cn.fujitsu.com>
---
 testcases/kernel/syscalls/flock/flock01.c | 186 ++++++------------------------
 1 file changed, 35 insertions(+), 151 deletions(-)

diff --git a/testcases/kernel/syscalls/flock/flock01.c b/testcases/kernel/syscalls/flock/flock01.c
index a4133f1..95c3040 100644
--- a/testcases/kernel/syscalls/flock/flock01.c
+++ b/testcases/kernel/syscalls/flock/flock01.c
@@ -1,172 +1,56 @@
-/*
- * Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of version 2 of the GNU General Public License as
- * published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it would be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+// SPDX-License-Identifier: GPL-2.0-or-later
+/* Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
+ * Author: Vatsal Avasthi
  *
+ * Test Description:
+ *  This test verifies that flock() succeeds with all kind of locks.
  */
-/**********************************************************
- *
- *    TEST IDENTIFIER   : flock01
- *
- *    EXECUTED BY       : anyone
- *
- *    TEST TITLE        : Basic test for flock(2)
- *
- *    TEST CASE TOTAL   : 3
- *
- *    AUTHOR            : Vatsal Avasthi <vatsal.avasthi@wipro.com>
- *
- *    SIGNALS
- *      Uses SIGUSR1 to pause before test if option set.
- *      (See the parse_opts(3) man page).
- *
- *    DESCRIPTION
- * 	Test to verify flock(2) succeeds with all kind of locks.
- *	Intends to provide a limited exposure of system call.
- *    $
- *	Setup:
- *        Setup signal handling.
- *        Pause for SIGUSR1 if option specified.
- *        Create a temporary directory and chdir to it.
- * 	  Create a temporary file
- *
- *	Test:
- *	Loop if proper options are given.
- *		Execute system call
- *		Check return code, if system call failed (return == -1)
- *				Log the error number and issue a FAIL message
- *		otherwise issue a PASS message
- *
- *      Cleanup:
- *        Print errno log and/or timing stats if options given
- *	  Deletes temporary directory.
- *
- * USAGE:  <for command-line>
- *      flock01 [-c n] [-e] [-i n] [-I x] [-P x] [-t] [-h] [-f] [-p]
- *                      where,  -c n : Run n copies concurrently.
- *                              -f   : Turn off functional testing
- *    				-e   : Turn on errno logging.
- *                              -h   : Show help screen                        $
- *				-i n : Execute test n times.
- *                              -I x : Execute test for x seconds.
- *                              -p   : Pause for SIGUSR1 before starting
- *                              -P x : Pause for x seconds between iterations.
- *                              -t   : Turn on syscall timing.
- *
- ****************************************************************/
 
 #include <errno.h>
-#include <stdio.h>
-#include <sys/wait.h>
 #include <sys/file.h>
-#include <fcntl.h>
-#include "test.h"
-#include "safe_macros.h"
 
-void setup(void);
-void cleanup(void);
+#include "tst_test.h"
 
-char *TCID = "flock01";
-int TST_TOTAL = 3;
-char filename[100];
-int fd;
+static int fd;
 
-struct test_case_t {
+static struct tcase {
 	int operation;
 	char *opt;
-} test_cases[] = {
-	{ LOCK_SH, "Shared Lock" },
-	{ LOCK_UN, "Unlock"},
-	{ LOCK_EX, "Exclusive Lock"}
+} tcases[] = {
+	{LOCK_SH, "Shared Lock" },
+	{LOCK_UN, "Unlock"},
+	{LOCK_EX, "Exclusive Lock"},
 };
 
-int main(int argc, char **argv)
+static void verify_flock(unsigned n)
 {
-	int lc, i;
-
-	tst_parse_opts(argc, argv, NULL, NULL);
-
-	/* global setup */
-	setup();
-
-	/* The following loop checks looping state if -i option given */
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-
-		/* reset tst_count in case we are looping */
-		tst_count = 0;
-
-		for (i = 0; i < TST_TOTAL; ++i) {
-
-			/* Testing system call */
-			TEST(flock(fd, test_cases[i].operation));
-			if (TEST_RETURN == -1) {
-				tst_resm(TFAIL | TTERRNO,
-					 "flock() failed to get %s",
-					 test_cases[i].opt);
-				continue;	/*next loop for MTKERNEL  */
-			} else {
-				tst_resm(TPASS,
-					 "flock() succeeded with %s",
-					 test_cases[i].opt);
-			}
-
-		}
-
+	struct tcase *tc = &tcases[n];
+
+	TEST(flock(fd, tc->operation));
+	if (TST_RET == -1) {
+		tst_res(TFAIL | TTERRNO,
+			"flock() failed to get %s", tc->opt);
+	} else {
+		tst_res(TPASS,
+			"flock() succeeded with %s", tc->opt);
 	}
-
-	close(fd);
-
-	cleanup();
-
-	tst_exit();
-
 }
 
-/*
- * setup()
- *	performs all ONE TIME setup for this test
- */
-void setup(void)
+static void setup(void)
 {
-
-	tst_sig(FORK, DEF_HANDLER, cleanup);
-
-	/* Pause if that option was specified
-	 * TEST_PAUSE contains the code to fork the test with the -i option.
-	 * You want to make sure you do this before you create your temporary
-	 * directory.
-	 */
-	TEST_PAUSE;
-
-	/* Create a unique temporary directory and chdir() to it. */
-	tst_tmpdir();
-
-	sprintf(filename, "flock01.%d", getpid());
-
-	/* creating temporary file */
-	fd = SAFE_OPEN(tst_rmdir, filename, O_CREAT | O_TRUNC | O_RDWR, 0644);
+	fd = SAFE_OPEN("testfile", O_CREAT | O_TRUNC | O_RDWR, 0644);
 }
 
-/*
- * cleanup()
- *	performs all ONE TIME cleanup for this test at
- * 	completion or premature exit
- */
-void cleanup(void)
+static void cleanup(void)
 {
-
-	unlink(filename);
-	tst_rmdir();
-
+	if (fd > 0)
+		SAFE_CLOSE(fd);
 }
+
+static struct tst_test test = {
+	.tcnt = ARRAY_SIZE(tcases),
+	.test = verify_flock,
+	.needs_tmpdir = 1,
+	.setup = setup,
+	.cleanup = cleanup,
+};
-- 
1.8.3.1




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

* [LTP] [PATCH 2/5] syscalls/flock02: Rewrite to new library
  2018-08-02  3:53 [LTP] [PATCH 1/5] syscalls/flock01: Rewrite to new library Jinhui huang
@ 2018-08-02  3:53 ` Jinhui huang
  2018-08-02 11:12   ` Jan Stancek
  2018-08-02  3:53 ` [LTP] [PATCH 3/5] syscalls/flock03: " Jinhui huang
                   ` (3 subsequent siblings)
  4 siblings, 1 reply; 13+ messages in thread
From: Jinhui huang @ 2018-08-02  3:53 UTC (permalink / raw)
  To: ltp

Signed-off-by: Jinhui huang <huangjh.jy@cn.fujitsu.com>
---
 testcases/kernel/syscalls/flock/flock02.c | 193 ++++++++----------------------
 1 file changed, 48 insertions(+), 145 deletions(-)

diff --git a/testcases/kernel/syscalls/flock/flock02.c b/testcases/kernel/syscalls/flock/flock02.c
index 9ddf729..6f4086d 100644
--- a/testcases/kernel/syscalls/flock/flock02.c
+++ b/testcases/kernel/syscalls/flock/flock02.c
@@ -1,164 +1,67 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
 /*
  * Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of version 2 of the GNU General Public License as
- * published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it would be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- *
+ * Author: Vatsal Avasthi
+ *
+ * Test Description:
+ *  1) flock() returns -1 and sets error number to EBADF if the file descriptor
+ *     is invalid.
+ *  2) flock() returns -1 and sets error number to EINVAL if the argument
+ *     operation does not include LOCK_SH,LOCK_EX,LOCK_UN.
+ *  3) flock() returns -1 and sets error number to EINVAL if an invalid
+ *     combination of locking modes is used i.e LOCK_SH with LOCK_EX
  */
-/**********************************************************
- *
- *    TEST IDENTIFIER   : flock02
- *
- *    EXECUTED BY       : anyone
- *
- *    TEST TITLE        : Error condition test for flock(2)
- *
- *    TEST CASE TOTAL   : 3
- *
- *    AUTHOR            : Vatsal Avasthi <vatsal.avasthi@wipro.com>
- *
- *    SIGNALS
- *      Uses SIGUSR1 to pause before test if option set.
- *      (See the parse_opts(3) man page).
- *
- *    DESCRIPTION
- * 	This test verifies that
- *	1)	flock(2) returns -1 and sets error number to EBADF
- *		if the file descriptor is invalid.
- *      2)	flock(2) returns -1 and sets error number to EINVAL
- *		if the argument operation does not include LOCK_SH,LOCK_EX,LOCK_UN.$
- *	3)	flock(2) returns -1 and sets error number to EINVAL
- *		if an invalid combination of locking modes is used i.e LOCK_SH with LOCK_EX
- *
- *	Setup:
- *        Setup signal handling.
- *        Pause for SIGUSR1 if option specified.
- *        Create a temporary directory and chdir to it.
- * 	  Create a temporary file
- *
- *	Test:
- *	Loop if proper options are given.
- *		Execute system call
- *		Check return code,
- *			if system call failed (return == -1) and errno == expected_errno
- *				Issue system call fails with expected return value and error number
- *			else
- *				Issue system call failed to produce expected error.
- *
- *      Cleanup:
- *        Print errno log and/or timing stats if options given
- *	  Deletes temporary directory.
- *
- * USAGE:  <for command-line>
- *      flock02 [-c n] [-e] [-i n] [-I x] [-P x] [-t] [-h] [-f] [-p]
- *                      where,  -c n : Run n copies concurrently.
- *                              -f   : Turn off functional testing
- *    				-e   : Turn on errno logging.
- *                              -h   : Show help screen                        $
- *				-i n : Execute test n times.
- *                              -I x : Execute test for x seconds.
- *                              -p   : Pause for SIGUSR1 before starting
- *                              -P x : Pause for x seconds between iterations.
- *                              -t   : Turn on syscall timing.
- *
- ****************************************************************/
 
-#include <sys/types.h>
-#include <sys/file.h>
-#include <sys/wait.h>
-#include <fcntl.h>
 #include <errno.h>
-#include <stdio.h>
-#include "test.h"
-
-void setup(void);
-void cleanup(void);
-
-char *TCID = "flock02";
-int TST_TOTAL = 3;
-char filename[100];
-int fd;
-
-int main(int argc, char **argv)
-{
-	int lc;
-
-	tst_parse_opts(argc, argv, NULL, NULL);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-
-		tst_count = 0;
-
-		TEST(flock(-1, LOCK_SH));
-
-		if (TEST_RETURN == -1 && TEST_ERRNO == EBADF)
-			tst_resm(TPASS, "flock failed as expected with EBADF");
-		else if (TEST_RETURN == 0)
-			tst_resm(TFAIL, "flock succeeded unexpectedly");
-		else
-			tst_resm(TFAIL | TTERRNO, "flock failed unexpectedly");
+#include <sys/file.h>
 
-		/* Test system call with invalid argument */
-		TEST(flock(fd, LOCK_NB));
+#include "tst_test.h"
 
-		if (TEST_RETURN == -1 && TEST_ERRNO == EINVAL)
-			tst_resm(TPASS, "flock failed as expected with EINVAL");
-		else if (TEST_RETURN == 0)
-			tst_resm(TFAIL, "flock succeeded unexpectedly");
-		else
-			tst_resm(TFAIL | TTERRNO, "flock failed unexpectedly");
+static int badfd = -1;
+static int fd;
 
-		/* Test system call with invalid combination of arguments */
-		TEST(flock(fd, LOCK_SH | LOCK_EX));
+static struct tcase {
+	int *fd;
+	int operation;
+	int exp_err;
+} tcases[] = {
+	{&badfd, LOCK_SH, EBADF},
+	{&fd, LOCK_NB, EINVAL},
+	{&fd, LOCK_SH | LOCK_EX, EINVAL},
+};
 
-		if (TEST_RETURN == -1 && TEST_ERRNO == EINVAL) {
-			tst_resm(TPASS, "flock failed as expected with EINVAL");
-			continue;	/*next loop for MTKERNEL  */
-		} else if (TEST_RETURN == 0)
-			tst_resm(TFAIL, "flock succeeded unexpectedly");
-		else
-			tst_resm(TFAIL | TTERRNO, "flock failed unexpectedly");
+static void verify_flock(unsigned n)
+{
+	struct tcase *tc = &tcases[n];
 
+	fd = SAFE_OPEN("testfile", O_RDWR);
+	TEST(flock(*tc->fd, tc->operation));
+	if (TST_RET == 0) {
+		tst_res(TFAIL | TTERRNO, "flock() succeeded unexpectedly");
+		return;
 	}
 
-	close(fd);
-
-	cleanup();
-
-	tst_exit();
+	if (tc->exp_err == TST_ERR) {
+		tst_res(TPASS | TTERRNO, "flock() failed expectedly");
+	} else {
+		tst_res(TFAIL | TTERRNO, "flock() failed unexpectedly, "
+			"expected %s", tst_strerrno(tc->exp_err));
+	}
 
+	SAFE_CLOSE(fd);
 }
 
-void setup(void)
+static void setup(void)
 {
+	int fd1;
 
-	tst_sig(FORK, DEF_HANDLER, cleanup);
-
-	TEST_PAUSE;
-
-	tst_tmpdir();
-
-	sprintf(filename, "flock02.%d", getpid());
-
-	fd = open(filename, O_CREAT | O_TRUNC | O_RDWR, 0666);
-	if (fd < 0)
-		tst_brkm(TFAIL | TERRNO, cleanup, "creat failed");
+	fd1 = SAFE_OPEN("testfile", O_CREAT | O_TRUNC | O_RDWR, 0666);
+	SAFE_CLOSE(fd1);
 }
 
-void cleanup(void)
-{
-	unlink(filename);
-	tst_rmdir();
-
-}
+static struct tst_test test = {
+	.tcnt = ARRAY_SIZE(tcases),
+	.test = verify_flock,
+	.needs_tmpdir = 1,
+	.setup = setup,
+};
-- 
1.8.3.1




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

* [LTP] [PATCH 3/5] syscalls/flock03: Rewrite to new library
  2018-08-02  3:53 [LTP] [PATCH 1/5] syscalls/flock01: Rewrite to new library Jinhui huang
  2018-08-02  3:53 ` [LTP] [PATCH 2/5] syscalls/flock02: " Jinhui huang
@ 2018-08-02  3:53 ` Jinhui huang
  2018-08-02 11:20   ` Jan Stancek
  2018-08-02  3:53 ` [LTP] [PATCH 4/5] syscalls/flock04: " Jinhui huang
                   ` (2 subsequent siblings)
  4 siblings, 1 reply; 13+ messages in thread
From: Jinhui huang @ 2018-08-02  3:53 UTC (permalink / raw)
  To: ltp

Signed-off-by: Jinhui huang <huangjh.jy@cn.fujitsu.com>
---
 testcases/kernel/syscalls/flock/flock03.c | 203 ++++++++----------------------
 1 file changed, 53 insertions(+), 150 deletions(-)

diff --git a/testcases/kernel/syscalls/flock/flock03.c b/testcases/kernel/syscalls/flock/flock03.c
index 420f509..57dcc5f 100644
--- a/testcases/kernel/syscalls/flock/flock03.c
+++ b/testcases/kernel/syscalls/flock/flock03.c
@@ -1,28 +1,13 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
 /*
  * Copyright (c) International Business Machines  Corp., 2002
- * Copyright (c) 2012 Cyril Hrubis <chrubis@suse.cz>
+ * Author: Cyril Hrubis
  *
- * This program is free software;  you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY;  without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
- * the GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program;  if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-/*
- * This test verifies that flock cannot unlock a file locked
- * by another task
+ * Test Description:
+ *  This test verifies that flock() cannot unlock a file locked by another
+ *  task.
  *
  * Test Steps:
- *
  *  Fork a child processes The parent flocks a file with LOCK_EX Child waits
  *  for that to happen, then checks to make sure it is locked.  Child then
  *  tries to unlock the file. If the unlock succeeds, the child attempts to
@@ -30,166 +15,84 @@
  *  the file.
  */
 
-#include <stdio.h>
 #include <errno.h>
-#include <fcntl.h>
-#include <sys/types.h>
-#include <sys/stat.h>
+#include <stdlib.h>
 #include <sys/file.h>
-#include <sys/wait.h>
-#include "test.h"
-
-#define FILE_NAME "flock03"
-
-static void setup(void);
-static void cleanup(void);
-static void childfunc(int);
-
-#ifdef UCLINUX
-static int fd_uc;
-static void childfunc_uc(void)
-{
-	childfunc(fd_uc);
-}
-#endif
-
-char *TCID = "flock03";
-int TST_TOTAL = 3;
-
-int main(int argc, char **argv)
-{
-	int lc;
-	pid_t pid;
-	int status;
-	int fd;
-
-	tst_parse_opts(argc, argv, NULL, NULL);
-
-#ifdef UCLINUX
-	maybe_run_child(&childfunc_uc, "ds", &fd_uc, FILE_NAME);
-#endif
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-
-		fd = open(FILE_NAME, O_RDWR);
-
-		if (fd == -1)
-			tst_brkm(TFAIL | TERRNO, cleanup,
-				 "parent failed to open the file");
-
-		pid = FORK_OR_VFORK();
-
-		if (pid == -1)
-			tst_brkm(TFAIL | TERRNO, cleanup, "fork() failed");
-		if (pid == 0) {
-#ifdef UCLINUX
-			if (self_exec(argv[0], "ds", fd, FILE_NAME) < 0)
-				tst_brkm(TFAIL | TERRNO, cleanup,
-					 "self_exec failed");
-#else
-			childfunc(fd);
-#endif
-		}
-
-		TEST(flock(fd, LOCK_EX | LOCK_NB));
-
-		if (TEST_RETURN != 0)
-			tst_resm(TFAIL | TTERRNO,
-				 "Parent: Initial attempt to flock() failed");
-		else
-			tst_resm(TPASS,
-				 "Parent: Initial attempt to flock() passed");
 
-		TST_SAFE_CHECKPOINT_WAKE(cleanup, 0);
-
-		if ((waitpid(pid, &status, 0)) < 0) {
-			tst_resm(TFAIL, "wait() failed");
-			continue;
-		}
-		if ((WIFEXITED(status)) && (WEXITSTATUS(status) == 0))
-			tst_resm(TPASS, "flock03 Passed");
-		else
-			tst_resm(TFAIL, "flock03 Failed");
-
-		close(fd);
-
-	}
-
-	cleanup();
-	tst_exit();
-}
+#include "tst_test.h"
 
 static void childfunc(int fd)
 {
 	int fd2;
+	TST_CHECKPOINT_WAIT(0);
 
-	TST_SAFE_CHECKPOINT_WAIT(NULL, 0);
-
-	fd2 = open(FILE_NAME, O_RDWR);
-
-	if (fd2 == -1) {
-		fprintf(stderr, "CHILD: failed to open the file: %s\n",
-		        strerror(errno));
-		exit(1);
-	}
-
-	if (flock(fd2, LOCK_EX | LOCK_NB) != -1) {
-		fprintf(stderr, "CHILD: The file was not already locked\n");
-		exit(1);
-	}
+	fd2 = SAFE_OPEN("testfile", O_RDWR);
+	if (flock(fd2, LOCK_EX | LOCK_NB) != -1)
+		tst_brk(TBROK, "CHILD: The file was not already locked");
 
 	TEST(flock(fd, LOCK_UN));
-	/* XXX: LOCK_UN does not return an error if there was nothing to
-	 * unlock.
-	 */
-	if (TEST_RETURN == -1) {
-		fprintf(stderr, "CHILD: Unable to unlock file locked by "
-		        "parent: %s\n", strerror(TEST_ERRNO));
+	if (TST_RET == -1) {
+		tst_res(TFAIL, "CHILD: Unable to unlock file locked by "
+			"parent: %s", tst_strerrno(TST_ERR));
 		exit(1);
 	} else {
-		fprintf(stderr, "CHILD: File locked by parent unlocked\n");
+		tst_res(TPASS, "CHILD: File locked by parent unlocked");
 	}
 
 	TEST(flock(fd2, LOCK_EX | LOCK_NB));
-
-	if (TEST_RETURN == -1) {
-		fprintf(stderr, "CHILD: Unable to lock file after "
-		        "unlocking: %s\n", strerror(TEST_ERRNO));
+	if (TST_RET == -1) {
+		tst_res(TFAIL, "CHILD: Unable to unlock file after "
+			"unlocking: %s", tst_strerrno(TST_ERR));
 		exit(1);
 	} else {
-		fprintf(stderr, "CHILD: Locking after unlock passed\n");
+		tst_res(TPASS, "Locking after unlock passed");
 	}
 
-	close(fd);
-	close(fd2);
+	SAFE_CLOSE(fd);
+	SAFE_CLOSE(fd2);
 
 	exit(0);
 }
 
-static void setup(void)
+static void verify_flock(void)
 {
-	int fd;
+	int fd1;
+	pid_t pid;
+
+	fd1 = SAFE_OPEN("testfile", O_RDWR);
 
-	tst_sig(FORK, DEF_HANDLER, cleanup);
+	pid = SAFE_FORK();
+	if (pid == 0)
+		childfunc(fd1);
 
-	TEST_PAUSE;
+	TEST(flock(fd1, LOCK_EX | LOCK_NB));
+	if (TST_RET != 0) {
+		tst_res(TFAIL | TTERRNO,
+			"Parent: Initial attempt to flock() failed");
+	} else {
+		tst_res(TPASS,
+			"Parent: Initial attempt to flock() passed");
+	}
 
-	tst_tmpdir();
+	TST_CHECKPOINT_WAKE(0);
 
-	TST_CHECKPOINT_INIT(tst_rmdir);
+	tst_reap_children();
 
-	fd = open(FILE_NAME, O_CREAT | O_TRUNC | O_RDWR, 0666);
-	if (fd < 0) {
-		tst_resm(TBROK, "creating a new file failed");
-		cleanup();
-	}
-	close(fd);
+	SAFE_CLOSE(fd1);
 }
 
-static void cleanup(void)
+static void setup(void)
 {
-	tst_rmdir();
+	int fd;
+
+	fd = SAFE_OPEN("testfile", O_CREAT | O_TRUNC | O_RDWR, 0666);
+	SAFE_CLOSE(fd);
 }
+
+static struct tst_test test = {
+	.test_all = verify_flock,
+	.needs_checkpoints = 1,
+	.needs_tmpdir = 1,
+	.forks_child = 1,
+	.setup = setup,
+};
-- 
1.8.3.1




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

* [LTP] [PATCH 4/5] syscalls/flock04: Rewrite to new library
  2018-08-02  3:53 [LTP] [PATCH 1/5] syscalls/flock01: Rewrite to new library Jinhui huang
  2018-08-02  3:53 ` [LTP] [PATCH 2/5] syscalls/flock02: " Jinhui huang
  2018-08-02  3:53 ` [LTP] [PATCH 3/5] syscalls/flock03: " Jinhui huang
@ 2018-08-02  3:53 ` Jinhui huang
  2018-08-02 12:17   ` Jan Stancek
  2018-08-02  3:53 ` [LTP] [PATCH 5/5] syscalls/flock06: " Jinhui huang
  2018-08-02 11:03 ` [LTP] [PATCH 1/5] syscalls/flock01: " Jan Stancek
  4 siblings, 1 reply; 13+ messages in thread
From: Jinhui huang @ 2018-08-02  3:53 UTC (permalink / raw)
  To: ltp

1) Avoid locking closed fd when running flock04 in loops
2) Merge flock05 into flock04

Signed-off-by: Jinhui huang <huangjh.jy@cn.fujitsu.com>
---
 runtest/ltplite                            |   1 -
 runtest/stress.part3                       |   1 -
 runtest/syscalls                           |   1 -
 testcases/kernel/syscalls/flock/.gitignore |   1 -
 testcases/kernel/syscalls/flock/flock04.c  | 237 ++++++++++-------------------
 testcases/kernel/syscalls/flock/flock05.c  | 207 -------------------------
 6 files changed, 82 insertions(+), 366 deletions(-)
 delete mode 100644 testcases/kernel/syscalls/flock/flock05.c

diff --git a/runtest/ltplite b/runtest/ltplite
index 5a7819c..b07d884 100644
--- a/runtest/ltplite
+++ b/runtest/ltplite
@@ -239,7 +239,6 @@ flock01 flock01
 flock02 flock02
 flock03 flock03
 flock04 flock04
-flock05 flock05
 flock06 flock06
 
 fmtmsg01 fmtmsg01
diff --git a/runtest/stress.part3 b/runtest/stress.part3
index 3121cd9..0c320df 100644
--- a/runtest/stress.part3
+++ b/runtest/stress.part3
@@ -178,7 +178,6 @@ flock01 flock01
 flock02 flock02
 flock03 flock03
 flock04 flock04
-flock05 flock05
 flock06 flock06
 
 fmtmsg01 fmtmsg01
diff --git a/runtest/syscalls b/runtest/syscalls
index dc72484..5b38132 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -286,7 +286,6 @@ flock01 flock01
 flock02 flock02
 flock03 flock03
 flock04 flock04
-flock05 flock05
 flock06 flock06
 
 fmtmsg01 fmtmsg01
diff --git a/testcases/kernel/syscalls/flock/.gitignore b/testcases/kernel/syscalls/flock/.gitignore
index 93cce0a..c8cb0fc 100644
--- a/testcases/kernel/syscalls/flock/.gitignore
+++ b/testcases/kernel/syscalls/flock/.gitignore
@@ -2,5 +2,4 @@
 /flock02
 /flock03
 /flock04
-/flock05
 /flock06
diff --git a/testcases/kernel/syscalls/flock/flock04.c b/testcases/kernel/syscalls/flock/flock04.c
index 8a282b4..78fc0f1 100644
--- a/testcases/kernel/syscalls/flock/flock04.c
+++ b/testcases/kernel/syscalls/flock/flock04.c
@@ -1,178 +1,105 @@
-/*
- * Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of version 2 of the GNU General Public License as
- * published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it would be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+// SPDX-License-Identifier: GPL-2.0-or-later
+/* Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
+ * Author: Vatsal Avasthi
  *
+ * Test Description:
+ *  This test verifies that flock() behavior with different locking
+ *  combinations along with LOCK_SH and LOCK_EX:
+ *	1) flock() succeeds in acquiring shared lock on the file which has
+ *	   been locked with shared lock.
+ *	2) flock() fails to acquire exclusive lock on the file which has been
+ *	   locked with shared lock.
+ *	3) flock() fails to acquire shared lock on the file which has been
+ *	   locked with exclusive lock.
+ *	4) flock() fails to acquire exclusive lock on the file which has been
+ *	   locked with exclusive lock.
  */
-/**********************************************************
- *
- *    TEST IDENTIFIER   : flock04
- *
- *    EXECUTED BY       : anyone
- *
- *    TEST TITLE        : Testing different locks on flock(2)
- *
- *    TEST CASE TOTAL   : 2
- *
- *    AUTHOR            : Vatsal Avasthi <vatsal.avasthi@wipro.com>
- *
- *    SIGNALS
- *      Uses SIGUSR1 to pause before test if option set.
- *      (See the parse_opts(3) man page).
- *
- *    DESCRIPTION
- * 	Tests to verify flock(2) behavior with different locking combinations along
- *	with LOCK_SH.
- *    $
- *	Setup:
- *        Setup signal handling.
- *        Pause for SIGUSR1 if option specified.
- *        Create a temporary directory and chdir to it.
- * 	  Create a temporary file
- *
- *	Test:
- *	Loop if proper options are given.
- *		Parent flocks(2) a file
- *		fork() a child process
- * 		Child tries to flock() the already flocked file with different types of locks
- *		Check return code, if system call failed (return == -1)
- *				Log the error number and issue a FAIL message
- *		otherwise issue a PASS message
- *
- *      Cleanup:
- *        Print errno log and/or timing stats if options given
- *	  Deletes temporary directory.
- *
- * USAGE:  <for command-line>
- *      flock04 [-c n] [-e] [-i n] [-I x] [-P x] [-t] [-h] [-f] [-p]
- *                      where,  -c n : Run n copies concurrently.
- *                              -f   : Turn off functional testing
- *    				-e   : Turn on errno logging.
- *                              -h   : Show help screen                        $
- *				-i n : Execute test n times.
- *                              -I x : Execute test for x seconds.
- *                              -p   : Pause for SIGUSR1 before starting
- *                              -P x : Pause for x seconds between iterations.
- *                              -t   : Turn on syscall timing.
- *
- ****************************************************************/
 
 #include <errno.h>
-#include <stdio.h>
-#include <unistd.h>
-#include <sys/types.h>
-#include <sys/wait.h>
 #include <sys/file.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include "test.h"
-#include "safe_macros.h"
+#include <stdlib.h>
 
-void setup(void);
-void cleanup(void);
+#include "tst_test.h"
 
-char *TCID = "flock04";
-int TST_TOTAL = 2;
-char filename[100];
-int fd, fd1, status;
+static struct tcase {
+	int operation;
+	char *filelock;
+} tcases[] = {
+	{LOCK_SH, "shared lock"},
+	{LOCK_EX, "exclusive lock"},
+};
 
-int main(int argc, char **argv)
+static void child(int opt1, int opt2, char *lock)
 {
-	int lc, retval;
-	pid_t pid;
-
-	tst_parse_opts(argc, argv, NULL, NULL);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-
-		tst_count = 0;
-
-		TEST(flock(fd, LOCK_SH));
-		if (TEST_RETURN == 0) {
-
-			pid = FORK_OR_VFORK();
-			if (pid == -1)
-				tst_brkm(TBROK | TERRNO, cleanup,
-					 "fork failed");
-			if (pid == 0) {
-				fd1 = open(filename, O_RDONLY);
-				retval = flock(fd1, LOCK_SH | LOCK_NB);
-				if (retval == -1)
-					tst_resm(TFAIL,
-						 "flock() FAILED to acquire shared lock on existing "
-						 "Share Locked file");
-				else
-					tst_resm(TPASS,
-						 "flock() PASSED in acquiring shared lock on "
-						 "Share Locked file");
-				exit(0);
-			} else {
-				SAFE_WAIT(cleanup, &status);
-			}
-
-			pid = FORK_OR_VFORK();
-			if (pid == -1)
-				tst_brkm(TBROK | TERRNO, cleanup,
-					 "fork failed");
-
-			if (pid == 0) {
-				fd1 = open(filename, O_RDWR);
-				retval = flock(fd1, LOCK_EX | LOCK_NB);
-				if (retval == -1) {
-					tst_resm(TPASS,
-						 "flock() failed to acquire exclusive lock on existing "
-						 "share locked file as expected");
-				} else {
-					tst_resm(TFAIL,
-						 "flock() unexpectedly passed in acquiring exclusive lock on "
-						 "Share Locked file");
-				}
-				exit(0);
-			} else if (wait(&status) == -1)
-				tst_resm(TBROK | TERRNO, "wait failed");
-			TEST(flock(fd, LOCK_UN));
-		} else
-			tst_resm(TFAIL | TERRNO, "flock failed");
-
-		close(fd);
-		close(fd1);
+	int retval, fd1;
+
+	fd1 = SAFE_OPEN("testfile", O_RDWR);
+	retval = flock(fd1, opt1);
+	if (LOCK_SH & opt1 && opt2 == LOCK_SH) {
+		if (retval == -1) {
+			tst_res(TFAIL, "flock() failed to acquire %s",
+				lock);
+			exit(1);
+		} else {
+			tst_res(TPASS, "flock() succeeded in acquiring %s"
+				"as expecetd", lock);
+			exit(0);
+		}
+	} else {
+		if (retval == 0) {
+			tst_res(TFAIL, "flock() succeeded in acquiring %s",
+				lock);
+			exit(1);
+		} else {
+			tst_res(TPASS, "flock() failed to acquire %s "
+				"as expecetd", lock);
+			exit(0);
+		}
 	}
 
-	cleanup();
-	tst_exit();
+	SAFE_CLOSE(fd1);
 }
 
-void setup(void)
+static void verify_flock(unsigned n)
 {
+	int fd2;
+	pid_t pid;
+	struct tcase *tc = &tcases[n];
 
-	tst_sig(FORK, DEF_HANDLER, cleanup);
-
-	TEST_PAUSE;
+	fd2 = SAFE_OPEN("testfile", O_RDWR);
+	TEST(flock(fd2, tc->operation));
+	if (TST_RET != 0) {
+		tst_res(TFAIL, "flock() failed to acquire %s", tc->filelock);
+		return;
+	}
 
-	tst_tmpdir();
+	pid = SAFE_FORK();
+	if (pid == 0)
+		child(LOCK_SH | LOCK_NB, tc->operation, tc->filelock);
+	else
+		tst_reap_children();
 
-	sprintf(filename, "flock04.%d", getpid());
+	pid = SAFE_FORK();
+	if (pid == 0)
+		child(LOCK_EX | LOCK_NB, tc->operation, tc->filelock);
+	else
+		tst_reap_children();
 
-	fd = open(filename, O_CREAT | O_TRUNC | O_RDWR, 0666);
-	if (fd == -1)
-		tst_brkm(TFAIL, cleanup, "creating a new file failed");
+	SAFE_CLOSE(fd2);
 }
 
-void cleanup(void)
+static void setup(void)
 {
-	unlink(filename);
+	int fd;
 
-	tst_rmdir();
+	fd = SAFE_OPEN("testfile", O_CREAT | O_TRUNC | O_RDWR, 0644);
+	SAFE_CLOSE(fd);
 }
+
+static struct tst_test test = {
+	.tcnt = ARRAY_SIZE(tcases),
+	.test = verify_flock,
+	.needs_tmpdir = 1,
+	.setup = setup,
+	.forks_child = 1,
+};
diff --git a/testcases/kernel/syscalls/flock/flock05.c b/testcases/kernel/syscalls/flock/flock05.c
deleted file mode 100644
index 13ae2f6..0000000
--- a/testcases/kernel/syscalls/flock/flock05.c
+++ /dev/null
@@ -1,207 +0,0 @@
-/*
- * Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of version 2 of the GNU General Public License as
- * published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it would be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- */
-/**********************************************************
- *
- *    TEST IDENTIFIER   : flock05
- *
- *    EXECUTED BY       : anyone
- *
- *    TEST TITLE        : Testing different locks on flock(2)
- *
- *    TEST CASE TOTAL   : 2
- *
- *    AUTHOR            : Vatsal Avasthi <vatsal.avasthi@wipro.com>
- *
- *    SIGNALS
- *      Uses SIGUSR1 to pause before test if option set.
- *      (See the parse_opts(3) man page).
- *
- *    DESCRIPTION
- * 	Tests to verify flock(2) behavior with different locking combinations along
- *	with LOCK_EX.
- *    $
- *	Setup:
- *        Setup signal handling.
- *        Pause for SIGUSR1 if option specified.
- *        Create a temporary directory and chdir to it.
- * 	  Create a temporary file
- *
- *	Test:
- *	Loop if proper options are given.
- *		Parent flocks(2) a file
- *		fork() a child process
- * 		Child tries to flock() the already flocked file with different types of locks
- *		Check return code, if system call failed (return == -1)
- *				Log the error number and issue a FAIL message
- *		otherwise issue a PASS message
- *
- *      Cleanup:
- *        Print errno log and/or timing stats if options given
- *	  Deletes temporary directory.
- *
- * USAGE:  <for command-line>
- *      flock05 [-c n] [-e] [-i n] [-I x] [-P x] [-t] [-h] [-f] [-p]
- *                      where,  -c n : Run n copies concurrently.
- *                              -f   : Turn off functional testing
- *    				-e   : Turn on errno logging.
- *                              -h   : Show help screen                        $
- *				-i n : Execute test n times.
- *                              -I x : Execute test for x seconds.
- *                              -p   : Pause for SIGUSR1 before starting
- *                              -P x : Pause for x seconds between iterations.
- *                              -t   : Turn on syscall timing.
- *
- ****************************************************************/
-
-#include <errno.h>
-#include <stdio.h>
-#include <unistd.h>
-#include <sys/types.h>
-#include <sys/wait.h>
-#include <sys/file.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include "test.h"
-
-void setup(void);
-void cleanup(void);
-
-char *TCID = "flock05";
-int TST_TOTAL = 2;
-char filename[100];
-int fd, fd1, status;
-
-int main(int argc, char **argv)
-{
-	int lc, retval;
-	pid_t pid;
-
-	tst_parse_opts(argc, argv, NULL, NULL);
-
-	/* global setup */
-	setup();
-
-	/* The following loop checks looping state if -i option given */
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-
-		/* reset tst_count in case we are looping */
-		tst_count = 0;
-
-		/* Testing Shared lock on Exclusive Locked file */
-		TEST(flock(fd, LOCK_EX));
-		if (TEST_RETURN == 0) {
-
-			pid = FORK_OR_VFORK();
-			if (pid == 0) {
-				fd1 = open(filename, O_RDWR);
-				retval = flock(fd1, LOCK_SH | LOCK_NB);
-				if (retval == -1) {
-					tst_resm(TPASS,
-						 "flock() failed to acquire shared lock on an already"
-						 "exclusive locked file as expected");
-				} else {
-					tst_resm(TFAIL,
-						 "flock() unexpectedly PASSED in acquiring shared lock on "
-						 "an already exclusive locked file");
-				}
-				exit(0);
-			} else {
-				/* parent waiting */
-				wait(&status);
-			}
-
-			/* Testing Exclusive lock on a Exclusive Locked file */
-			pid = FORK_OR_VFORK();
-
-			if (pid == 0) {
-				fd1 = open(filename, O_RDWR);
-				retval = flock(fd1, LOCK_EX | LOCK_NB);
-				if (retval == -1) {
-					tst_resm(TPASS,
-						 "flock() failed to acquire exclusive lock on existing "
-						 " exclusive locked file as expected");
-				} else {
-					tst_resm(TFAIL,
-						 "flock() unexpectedly passed in acquiring exclusive lock on "
-						 "an exclusive locked file");
-				}
-				exit(0);
-			} else {
-				/* parent waiting */
-				wait(&status);
-			}
-			TEST(flock(fd, LOCK_UN));
-		} else {
-			tst_resm(TFAIL,
-				 "flock() failed to acquire exclusive lock");
-		}
-
-	}
-
-	close(fd);
-	close(fd1);
-	cleanup();
-	tst_exit();
-
-}
-
-/*
- * setup()
- *	performs all ONE TIME setup for this test
- */
-void setup(void)
-{
-
-	tst_sig(FORK, DEF_HANDLER, cleanup);
-
-	/* Pause if that option was specified
-	 * TEST_PAUSE contains the code to fork the test with the -i option.
-	 * You want to make sure you do this before you create your temporary
-	 * directory.
-	 */
-	TEST_PAUSE;
-
-	/* Create a unique temporary directory and chdir() to it. */
-	tst_tmpdir();
-
-	sprintf(filename, "flock05.%d", getpid());
-
-	/* creating temporary file */
-	fd = open(filename, O_CREAT | O_TRUNC | O_RDWR, 0666);
-	if (fd == -1) {
-		tst_resm(TFAIL, "creating a new file failed");
-
-		/* Removing temp dir */
-		tst_rmdir();
-
-	}
-}
-
-/*
- * cleanup()
- *	performs all ONE TIME cleanup for this test at
- * 	completion or premature exit
- */
-void cleanup(void)
-{
-
-	unlink(filename);
-
-	tst_rmdir();
-
-}
-- 
1.8.3.1




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

* [LTP] [PATCH 5/5] syscalls/flock06: Rewrite to new library
  2018-08-02  3:53 [LTP] [PATCH 1/5] syscalls/flock01: Rewrite to new library Jinhui huang
                   ` (2 preceding siblings ...)
  2018-08-02  3:53 ` [LTP] [PATCH 4/5] syscalls/flock04: " Jinhui huang
@ 2018-08-02  3:53 ` Jinhui huang
  2018-08-02 12:29   ` Jan Stancek
  2018-08-02 11:03 ` [LTP] [PATCH 1/5] syscalls/flock01: " Jan Stancek
  4 siblings, 1 reply; 13+ messages in thread
From: Jinhui huang @ 2018-08-02  3:53 UTC (permalink / raw)
  To: ltp

Signed-off-by: Jinhui huang <huangjh.jy@cn.fujitsu.com>
---
 testcases/kernel/syscalls/flock/flock06.c | 213 ++++++++----------------------
 1 file changed, 52 insertions(+), 161 deletions(-)

diff --git a/testcases/kernel/syscalls/flock/flock06.c b/testcases/kernel/syscalls/flock/flock06.c
index 617eddf..61e9c4e 100644
--- a/testcases/kernel/syscalls/flock/flock06.c
+++ b/testcases/kernel/syscalls/flock/flock06.c
@@ -1,177 +1,68 @@
-/*
+// SPDX-License-Identifier: GPL-2.0-or-later
+/* Copyright (c) Matthew Wilcox for Hewlett Packard 2003
+ * Author: Matthew Wilcox
  *
- *   Copyright (c) Matthew Wilcox for Hewlett Packard 2003
+ * Test Description:
+ *  This test verifies that flock locks held on one fd conflict with flock
+ *  locks held on a different fd.
  *
- *   This program is free software;  you can redistribute it and/or modify
- *   it under the terms of the GNU General Public License as published by
- *   the Free Software Foundation; either version 2 of the License, or
- *   (at your option) any later version.
- *
- *   This program is distributed in the hope that it will be useful,
- *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
- *   the GNU General Public License for more details.
- *
- *   You should have received a copy of the GNU General Public License
- *   along with this program;  if not, write to the Free Software
- *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ * Test Steps:
+ *  The process opens two file descriptors on the same file.  It acquires
+ *  an exclusive flock on the first descriptor, checks that attempting to
+ *  acquire an flock on the second descriptor fails.  Then it removes the
+ *  first descriptor's lock and attempts to acquire an exclusive lock on
+ *  the second descriptor.
  */
 
-/**********************************************************
- *
- *    TEST IDENTIFIER   : flock06
- *
- *    EXECUTED BY       : anyone
- *
- *    TEST TITLE        : Error condition test for flock(2)
- *
- *    TEST CASE TOTAL   : 1
- *
- *    AUTHOR            : Matthew Wilcox <willy@debian.org>
- *
- *    SIGNALS
- *      Uses SIGUSR1 to pause before test if option set.
- *      (See the parse_opts(3) man page).
- *
- *    DESCRIPTION
- * 		 This test verifies that flock locks held on one fd conflict with
- * 		 flock locks held on a different fd.
- *
- *		 Test:
- * 		 		 The process opens two file descriptors on the same file.
- * 		 		 It acquires an exclusive flock on the first descriptor,
- * 		 		 checks that attempting to acquire an flock on the second
- * 		 		 descriptor fails.  Then it removes the first descriptor's
- * 		 		 lock and attempts to acquire an exclusive lock on the
- * 		 		 second descriptor.
- *
- * USAGE:  <for command-line>
- *      flock06 [-c n] [-e] [-i n] [-I x] [-P x] [-t] [-h] [-f] [-p]
- *                      where,  -c n : Run n copies concurrently
- *                              -f   : Turn off functional testing
- *    		 		 		 		 -e   : Turn on errno logging
- *                              -h   : Show help screen
- *		 		 		 		 -i n : Execute test n times
- *                              -I x : Execute test for x seconds
- *                              -p   : Pause for SIGUSR1 before starting
- *                              -P x : Pause for x seconds between iterations
- *                              -t   : Turn on syscall timing
- *
- ****************************************************************/
-
-#include <stdio.h>
 #include <errno.h>
-#include <fcntl.h>
-#include <sys/types.h>
-#include <sys/stat.h>
 #include <sys/file.h>
-#include <sys/wait.h>
-#include "test.h"
-#include "safe_macros.h"
 
-void setup(void);
-void cleanup(void);
+#include "tst_test.h"
 
-char *TCID = "flock06";
-int TST_TOTAL = 3;
-char filename[100];
-
-int main(int argc, char **argv)
+static void verify_flock(void)
 {
-	int lc;
-
-	tst_parse_opts(argc, argv, NULL, NULL);
-
-	setup();
-
-	/* The following loop checks looping state if -i option given */
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		int fd1, fd2;
-
-		/* reset tst_count in case we are looping */
-		tst_count = 0;
-
-		fd1 = open(filename, O_RDWR);
-		if (fd1 == -1)
-			tst_brkm(TFAIL | TERRNO, cleanup,
-				 "failed to open the file");
-
-		TEST(flock(fd1, LOCK_EX | LOCK_NB));
-		if (TEST_RETURN != 0)
-			tst_resm(TFAIL | TTERRNO,
-				 "First attempt to flock() failed");
-		else
-			tst_resm(TPASS, "First attempt to flock() passed");
-
-		fd2 = open(filename, O_RDWR);
-		if (fd2 == -1)
-			tst_brkm(TFAIL | TERRNO, cleanup,
-				 "failed to open the file");
-
-		TEST(flock(fd2, LOCK_EX | LOCK_NB));
-		if (TEST_RETURN == -1)
-			tst_resm(TPASS, "Second attempt to flock() denied");
-		else
-			tst_resm(TFAIL, "Second attempt to flock() succeeded!");
-
-		TEST(flock(fd1, LOCK_UN));
-		if (TEST_RETURN == -1)
-			tst_resm(TFAIL | TTERRNO, "Failed to unlock fd1");
-		else
-			tst_resm(TPASS, "Unlocked fd1");
-
-		TEST(flock(fd2, LOCK_EX | LOCK_NB));
-		if (TEST_RETURN == -1)
-			tst_resm(TFAIL, "Third attempt to flock() denied!");
-		else
-			tst_resm(TPASS, "Third attempt to flock() succeeded");
-		close(fd1);
-		close(fd2);
-
-	}
-
-	cleanup();
-	tst_exit();
-
+	int fd1, fd2;
+
+	fd1 = SAFE_OPEN("testfile", O_RDWR);
+	TEST(flock(fd1, LOCK_EX | LOCK_NB));
+	if (TST_RET != 0)
+		tst_res(TFAIL | TTERRNO, "First attempt to flock() failed");
+	else
+		tst_res(TPASS, "First attempt to flock() passed");
+
+	fd2 = SAFE_OPEN("testfile", O_RDWR);
+	TEST(flock(fd2, LOCK_EX | LOCK_NB));
+	if (TST_RET != 1)
+		tst_res(TPASS | TTERRNO, "Second attempt to flock() denied");
+	else
+		tst_res(TFAIL, "Second attempt to flock() succeeded!");
+
+	TEST(flock(fd1, LOCK_UN));
+	if (TST_RET == -1)
+		tst_res(TFAIL | TTERRNO, "Failed to unlock fd1");
+	else
+		tst_res(TPASS, "Unlocked fd1");
+
+	TEST(flock(fd2, LOCK_EX | LOCK_NB));
+	if (TST_RET == -1)
+		tst_res(TFAIL | TTERRNO, "Third attempt to flock() denied!");
+	else
+		tst_res(TPASS, "Third attempt to flock() succeeded");
+
+	SAFE_CLOSE(fd1);
+	SAFE_CLOSE(fd2);
 }
 
-/*
- * setup()
- *		 performs all ONE TIME setup for this test
- */
-void setup(void)
+static void setup(void)
 {
 	int fd;
 
-	tst_sig(FORK, DEF_HANDLER, cleanup);
-
-	/* Pause if that option was specified
-	 * TEST_PAUSE contains the code to fork the test with the -i option.
-	 * You want to make sure you do this before you create your temporary
-	 * directory.
-	 */
-	TEST_PAUSE;
-
-	/* Create a unique temporary directory and chdir() to it. */
-	tst_tmpdir();
-
-	sprintf(filename, "flock06.%d", getpid());
-
-	/* creating temporary file */
-	fd = SAFE_OPEN(tst_rmdir, filename, O_CREAT | O_TRUNC | O_RDWR, 0666);
-	close(fd);
+	fd = SAFE_OPEN("testfile", O_CREAT | O_TRUNC | O_RDWR, 0666);
+	SAFE_CLOSE(fd);
 }
 
-/*
- * cleanup()
- *		 performs all ONE TIME cleanup for this test at
- * 		 completion or premature exit
- */
-void cleanup(void)
-{
-
-	unlink(filename);
-	tst_rmdir();
-
-}
+static struct tst_test test = {
+	.test_all = verify_flock,
+	.needs_tmpdir = 1,
+	.setup = setup,
+};
-- 
1.8.3.1




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

* [LTP] [PATCH 1/5] syscalls/flock01: Rewrite to new library
  2018-08-02  3:53 [LTP] [PATCH 1/5] syscalls/flock01: Rewrite to new library Jinhui huang
                   ` (3 preceding siblings ...)
  2018-08-02  3:53 ` [LTP] [PATCH 5/5] syscalls/flock06: " Jinhui huang
@ 2018-08-02 11:03 ` Jan Stancek
  4 siblings, 0 replies; 13+ messages in thread
From: Jan Stancek @ 2018-08-02 11:03 UTC (permalink / raw)
  To: ltp


----- Original Message -----
> Signed-off-by: Jinhui huang <huangjh.jy@cn.fujitsu.com>
> ---
>  testcases/kernel/syscalls/flock/flock01.c | 186
>  ++++++------------------------
>  1 file changed, 35 insertions(+), 151 deletions(-)

ACK

Just a nit:
        if (fd > 0)
                SAFE_CLOSE(fd);

fd == 0 can also be valid fd, but skipping this close would
be fine anyway. (no need to repost)

Regards,
Jan

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

* [LTP] [PATCH 2/5] syscalls/flock02: Rewrite to new library
  2018-08-02  3:53 ` [LTP] [PATCH 2/5] syscalls/flock02: " Jinhui huang
@ 2018-08-02 11:12   ` Jan Stancek
  0 siblings, 0 replies; 13+ messages in thread
From: Jan Stancek @ 2018-08-02 11:12 UTC (permalink / raw)
  To: ltp


----- Original Message -----
> Signed-off-by: Jinhui huang <huangjh.jy@cn.fujitsu.com>

ACK, one note below.

> +static void verify_flock(unsigned n)
> +{
> +	struct tcase *tc = &tcases[n];
>  
> +	fd = SAFE_OPEN("testfile", O_RDWR);
> +	TEST(flock(*tc->fd, tc->operation));
> +	if (TST_RET == 0) {
> +		tst_res(TFAIL | TTERRNO, "flock() succeeded unexpectedly");
> +		return;

You are not closing fd here, but we'll get many TFAILs before
we run out of open file descriptors. (no need to repost)

Regards,
Jan

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

* [LTP] [PATCH 3/5] syscalls/flock03: Rewrite to new library
  2018-08-02  3:53 ` [LTP] [PATCH 3/5] syscalls/flock03: " Jinhui huang
@ 2018-08-02 11:20   ` Jan Stancek
  0 siblings, 0 replies; 13+ messages in thread
From: Jan Stancek @ 2018-08-02 11:20 UTC (permalink / raw)
  To: ltp


----- Original Message -----
> Signed-off-by: Jinhui huang <huangjh.jy@cn.fujitsu.com>
> ---
>  testcases/kernel/syscalls/flock/flock03.c | 203
>  ++++++++----------------------
>  1 file changed, 53 insertions(+), 150 deletions(-)

ACK

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

* [LTP] [PATCH 4/5] syscalls/flock04: Rewrite to new library
  2018-08-02  3:53 ` [LTP] [PATCH 4/5] syscalls/flock04: " Jinhui huang
@ 2018-08-02 12:17   ` Jan Stancek
  2018-08-06  5:07     ` [LTP] [PATCH v2 " Jinhui huang
  0 siblings, 1 reply; 13+ messages in thread
From: Jan Stancek @ 2018-08-02 12:17 UTC (permalink / raw)
  To: ltp


----- Original Message -----
> 1) Avoid locking closed fd when running flock04 in loops
> 2) Merge flock05 into flock04
> 
> Signed-off-by: Jinhui huang <huangjh.jy@cn.fujitsu.com>

+1 for merge, these are very similar

flock04.c:45: PASS: flock() succeeded in acquiring shared lockas expecetd
flock04.c:55: PASS: flock() failed to acquire shared lock as expecetd
flock04.c:55: PASS: flock() failed to acquire exclusive lock as expecetd
flock04.c:55: PASS: flock() failed to acquire exclusive lock as expecetd

This output is bit confusing. 2nd line is test where child is trying to
get exclusive lock". It would help to see what kind of lock parent holds
and what lock is child trying to acquire.

"lockas" is missing space
"expecetd" is typo

> +static void child(int opt1, int opt2, char *lock)
> +	int retval, fd1;
> +
> +	fd1 = SAFE_OPEN("testfile", O_RDWR);
> +	retval = flock(fd1, opt1);
> +	if (LOCK_SH & opt1 && opt2 == LOCK_SH) {
> +		if (retval == -1) {
> +			tst_res(TFAIL, "flock() failed to acquire %s",
> +				lock);

TERRNO would be nice to know here

> +			exit(1);

tst_res will propagate TFAIL to parent via shared memory, so
these exit codes don't matter much, if you use only one exit(0),
after SAFE_CLOSE below, it would work the same.

> +		} else {
> +			tst_res(TPASS, "flock() succeeded in acquiring %s"
> +				"as expecetd", lock);
> +			exit(0);
> +		}
> +	} else {
> +		if (retval == 0) {
> +			tst_res(TFAIL, "flock() succeeded in acquiring %s",
> +				lock);
> +			exit(1);
> +		} else {
> +			tst_res(TPASS, "flock() failed to acquire %s "
> +				"as expecetd", lock);
> +			exit(0);
> +		}
>  	}

If this is unreachable a comment would be nice.

> +	SAFE_CLOSE(fd1);
>  }

For example, I'd suggest following:

static void child(int opt1, int should_pass, char *lock)
{
        int retval, fd1;

        fd1 = SAFE_OPEN("testfile", O_RDWR);
        retval = flock(fd1, opt1);
        if (should_pass)
                tst_res(retval == -1 ? TFAIL : TPASS,
                        " child acquiring %s got %d", lock, retval);
        else
                tst_res(retval == -1 ? TPASS : TFAIL,
                        " child acquiring %s got %d", lock, retval);

        SAFE_CLOSE(fd1);
        exit(0);
}

static void verify_flock(unsigned n)
{
        int fd2;
        pid_t pid;
        struct tcase *tc = &tcases[n];

        fd2 = SAFE_OPEN("testfile", O_RDWR);
        TEST(flock(fd2, tc->operation));
        if (TST_RET != 0) {
                tst_res(TFAIL, "flock() failed to acquire %s", tc->filelock);
                return;
        }
        tst_res(TPASS, "Parent has %s", tc->filelock);

        pid = SAFE_FORK();
        if (pid == 0)
                child(LOCK_SH | LOCK_NB, tc->operation & LOCK_SH, "shared lock");
        else
                tst_reap_children();

        pid = SAFE_FORK();
        if (pid == 0)
                child(LOCK_EX | LOCK_NB, 0, "exclusive lock");
        else
                tst_reap_children();

        SAFE_CLOSE(fd2);
}

which produces:

tst_test.c:1018: INFO: Timeout per run is 0h 05m 00s
flock04.c:61: PASS: Parent has shared lock
flock04.c:40: PASS:  child acquiring shared lock got 0
flock04.c:43: PASS:  child acquiring exclusive lock got -1
flock04.c:61: PASS: Parent has exclusive lock
flock04.c:43: PASS:  child acquiring shared lock got -1
flock04.c:43: PASS:  child acquiring exclusive lock got -1

What do you think?

Regards,
Jan

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

* [LTP] [PATCH 5/5] syscalls/flock06: Rewrite to new library
  2018-08-02  3:53 ` [LTP] [PATCH 5/5] syscalls/flock06: " Jinhui huang
@ 2018-08-02 12:29   ` Jan Stancek
  2018-08-06  5:08     ` [LTP] [PATCH v2 " Jinhui huang
  0 siblings, 1 reply; 13+ messages in thread
From: Jan Stancek @ 2018-08-02 12:29 UTC (permalink / raw)
  To: ltp


----- Original Message -----
> Signed-off-by: Jinhui huang <huangjh.jy@cn.fujitsu.com>
> ---
>  testcases/kernel/syscalls/flock/flock06.c | 213
>  ++++++++----------------------
>  1 file changed, 52 insertions(+), 161 deletions(-)

Hi,

> +	int fd1, fd2;
> +
> +	fd1 = SAFE_OPEN("testfile", O_RDWR);
> +	TEST(flock(fd1, LOCK_EX | LOCK_NB));
> +	if (TST_RET != 0)
> +		tst_res(TFAIL | TTERRNO, "First attempt to flock() failed");
> +	else
> +		tst_res(TPASS, "First attempt to flock() passed");
> +
> +	fd2 = SAFE_OPEN("testfile", O_RDWR);
> +	TEST(flock(fd2, LOCK_EX | LOCK_NB));
> +	if (TST_RET != 1)

This should be == -1
The rest looks good to me.

Regards,
Jan

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

* [LTP] [PATCH v2 4/5] syscalls/flock04: Rewrite to new library
  2018-08-02 12:17   ` Jan Stancek
@ 2018-08-06  5:07     ` Jinhui huang
  0 siblings, 0 replies; 13+ messages in thread
From: Jinhui huang @ 2018-08-06  5:07 UTC (permalink / raw)
  To: ltp

1) Avoid locking closed fd when running flock04 in loops
2) Merge flock05 into flock04

Signed-off-by: Jinhui huang <huangjh.jy@cn.fujitsu.com>
---
 runtest/ltplite                            |   1 -
 runtest/stress.part3                       |   1 -
 runtest/syscalls                           |   1 -
 testcases/kernel/syscalls/flock/.gitignore |   1 -
 testcases/kernel/syscalls/flock/flock04.c  | 224 +++++++++--------------------
 testcases/kernel/syscalls/flock/flock05.c  | 207 --------------------------
 6 files changed, 69 insertions(+), 366 deletions(-)
 delete mode 100644 testcases/kernel/syscalls/flock/flock05.c

diff --git a/runtest/ltplite b/runtest/ltplite
index 0840564..9ca6c42 100644
--- a/runtest/ltplite
+++ b/runtest/ltplite
@@ -239,7 +239,6 @@ flock01 flock01
 flock02 flock02
 flock03 flock03
 flock04 flock04
-flock05 flock05
 flock06 flock06
 
 fmtmsg01 fmtmsg01
diff --git a/runtest/stress.part3 b/runtest/stress.part3
index fea4ccb..ec18dcf 100644
--- a/runtest/stress.part3
+++ b/runtest/stress.part3
@@ -178,7 +178,6 @@ flock01 flock01
 flock02 flock02
 flock03 flock03
 flock04 flock04
-flock05 flock05
 flock06 flock06
 
 fmtmsg01 fmtmsg01
diff --git a/runtest/syscalls b/runtest/syscalls
index 420ff45..6f16317 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -286,7 +286,6 @@ flock01 flock01
 flock02 flock02
 flock03 flock03
 flock04 flock04
-flock05 flock05
 flock06 flock06
 
 fmtmsg01 fmtmsg01
diff --git a/testcases/kernel/syscalls/flock/.gitignore b/testcases/kernel/syscalls/flock/.gitignore
index 93cce0a..c8cb0fc 100644
--- a/testcases/kernel/syscalls/flock/.gitignore
+++ b/testcases/kernel/syscalls/flock/.gitignore
@@ -2,5 +2,4 @@
 /flock02
 /flock03
 /flock04
-/flock05
 /flock06
diff --git a/testcases/kernel/syscalls/flock/flock04.c b/testcases/kernel/syscalls/flock/flock04.c
index 8a282b4..93fa43a 100644
--- a/testcases/kernel/syscalls/flock/flock04.c
+++ b/testcases/kernel/syscalls/flock/flock04.c
@@ -1,178 +1,92 @@
-/*
- * Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of version 2 of the GNU General Public License as
- * published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it would be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+// SPDX-License-Identifier: GPL-2.0-or-later
+/* Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
+ * Author: Vatsal Avasthi
  *
+ * Test Description:
+ *  This test verifies that flock() behavior with different locking
+ *  combinations along with LOCK_SH and LOCK_EX:
+ *	1) flock() succeeded in acquiring shared lock on shared lock file.
+ *	2) flock() failed to acquire exclusive lock on shared lock file.
+ *	3) flock() failed to acquire shared lock on exclusive lock file.
+ *	4) flock() failed to acquire exclusive lock on exclusive lock file.
  */
-/**********************************************************
- *
- *    TEST IDENTIFIER   : flock04
- *
- *    EXECUTED BY       : anyone
- *
- *    TEST TITLE        : Testing different locks on flock(2)
- *
- *    TEST CASE TOTAL   : 2
- *
- *    AUTHOR            : Vatsal Avasthi <vatsal.avasthi@wipro.com>
- *
- *    SIGNALS
- *      Uses SIGUSR1 to pause before test if option set.
- *      (See the parse_opts(3) man page).
- *
- *    DESCRIPTION
- * 	Tests to verify flock(2) behavior with different locking combinations along
- *	with LOCK_SH.
- *    $
- *	Setup:
- *        Setup signal handling.
- *        Pause for SIGUSR1 if option specified.
- *        Create a temporary directory and chdir to it.
- * 	  Create a temporary file
- *
- *	Test:
- *	Loop if proper options are given.
- *		Parent flocks(2) a file
- *		fork() a child process
- * 		Child tries to flock() the already flocked file with different types of locks
- *		Check return code, if system call failed (return == -1)
- *				Log the error number and issue a FAIL message
- *		otherwise issue a PASS message
- *
- *      Cleanup:
- *        Print errno log and/or timing stats if options given
- *	  Deletes temporary directory.
- *
- * USAGE:  <for command-line>
- *      flock04 [-c n] [-e] [-i n] [-I x] [-P x] [-t] [-h] [-f] [-p]
- *                      where,  -c n : Run n copies concurrently.
- *                              -f   : Turn off functional testing
- *    				-e   : Turn on errno logging.
- *                              -h   : Show help screen                        $
- *				-i n : Execute test n times.
- *                              -I x : Execute test for x seconds.
- *                              -p   : Pause for SIGUSR1 before starting
- *                              -P x : Pause for x seconds between iterations.
- *                              -t   : Turn on syscall timing.
- *
- ****************************************************************/
 
 #include <errno.h>
-#include <stdio.h>
-#include <unistd.h>
-#include <sys/types.h>
-#include <sys/wait.h>
 #include <sys/file.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include "test.h"
-#include "safe_macros.h"
+#include <stdlib.h>
 
-void setup(void);
-void cleanup(void);
+#include "tst_test.h"
 
-char *TCID = "flock04";
-int TST_TOTAL = 2;
-char filename[100];
-int fd, fd1, status;
+static struct tcase {
+	int operation;
+	char *f_lock;
+} tcases[] = {
+	{LOCK_SH, "shared lock"},
+	{LOCK_EX, "exclusive lock"},
+};
 
-int main(int argc, char **argv)
+static void child(int opt, int should_pass, char *lock)
 {
-	int lc, retval;
-	pid_t pid;
-
-	tst_parse_opts(argc, argv, NULL, NULL);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-
-		tst_count = 0;
-
-		TEST(flock(fd, LOCK_SH));
-		if (TEST_RETURN == 0) {
-
-			pid = FORK_OR_VFORK();
-			if (pid == -1)
-				tst_brkm(TBROK | TERRNO, cleanup,
-					 "fork failed");
-			if (pid == 0) {
-				fd1 = open(filename, O_RDONLY);
-				retval = flock(fd1, LOCK_SH | LOCK_NB);
-				if (retval == -1)
-					tst_resm(TFAIL,
-						 "flock() FAILED to acquire shared lock on existing "
-						 "Share Locked file");
-				else
-					tst_resm(TPASS,
-						 "flock() PASSED in acquiring shared lock on "
-						 "Share Locked file");
-				exit(0);
-			} else {
-				SAFE_WAIT(cleanup, &status);
-			}
-
-			pid = FORK_OR_VFORK();
-			if (pid == -1)
-				tst_brkm(TBROK | TERRNO, cleanup,
-					 "fork failed");
-
-			if (pid == 0) {
-				fd1 = open(filename, O_RDWR);
-				retval = flock(fd1, LOCK_EX | LOCK_NB);
-				if (retval == -1) {
-					tst_resm(TPASS,
-						 "flock() failed to acquire exclusive lock on existing "
-						 "share locked file as expected");
-				} else {
-					tst_resm(TFAIL,
-						 "flock() unexpectedly passed in acquiring exclusive lock on "
-						 "Share Locked file");
-				}
-				exit(0);
-			} else if (wait(&status) == -1)
-				tst_resm(TBROK | TERRNO, "wait failed");
-			TEST(flock(fd, LOCK_UN));
-		} else
-			tst_resm(TFAIL | TERRNO, "flock failed");
-
-		close(fd);
-		close(fd1);
+	int retval, fd1;
+
+	fd1 = SAFE_OPEN("testfile", O_RDWR);
+	retval = flock(fd1, opt);
+	if (should_pass) {
+		tst_res(retval == -1 ? TFAIL : TPASS,
+			" Child acquiring %s got %d", lock, retval);
+	} else {
+		tst_res(retval == -1 ? TPASS : TFAIL,
+			" Child acquiring %s got %d", lock, retval);
 	}
 
-	cleanup();
-	tst_exit();
+	SAFE_CLOSE(fd1);
+	exit(0);
 }
 
-void setup(void)
+static void verify_flock(unsigned n)
 {
+	int fd2;
+	pid_t pid;
+	struct tcase *tc = &tcases[n];
+
+	fd2 = SAFE_OPEN("testfile", O_RDWR);
+	TEST(flock(fd2, tc->operation));
+	if (TST_RET != 0) {
+		tst_res(TFAIL | TERRNO, "flock() failed to acquire %s",
+			tc->f_lock);
+		SAFE_CLOSE(fd2);
+		return;
+	}
 
-	tst_sig(FORK, DEF_HANDLER, cleanup);
-
-	TEST_PAUSE;
+	tst_res(TPASS, "Parent had %s", tc->f_lock);
 
-	tst_tmpdir();
+	pid = SAFE_FORK();
+	if (pid == 0)
+		child(LOCK_SH | LOCK_NB, tc->operation & LOCK_SH, tc->f_lock);
+	else
+		tst_reap_children();
 
-	sprintf(filename, "flock04.%d", getpid());
+	pid = SAFE_FORK();
+	if (pid == 0)
+		child(LOCK_EX | LOCK_NB, 0, tc->f_lock);
+	else
+		tst_reap_children();
 
-	fd = open(filename, O_CREAT | O_TRUNC | O_RDWR, 0666);
-	if (fd == -1)
-		tst_brkm(TFAIL, cleanup, "creating a new file failed");
+	SAFE_CLOSE(fd2);
 }
 
-void cleanup(void)
+static void setup(void)
 {
-	unlink(filename);
+	int fd;
 
-	tst_rmdir();
+	fd = SAFE_OPEN("testfile", O_CREAT | O_TRUNC | O_RDWR, 0644);
+	SAFE_CLOSE(fd);
 }
+
+static struct tst_test test = {
+	.tcnt = ARRAY_SIZE(tcases),
+	.test = verify_flock,
+	.needs_tmpdir = 1,
+	.setup = setup,
+	.forks_child = 1,
+};
diff --git a/testcases/kernel/syscalls/flock/flock05.c b/testcases/kernel/syscalls/flock/flock05.c
deleted file mode 100644
index 13ae2f6..0000000
--- a/testcases/kernel/syscalls/flock/flock05.c
+++ /dev/null
@@ -1,207 +0,0 @@
-/*
- * Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of version 2 of the GNU General Public License as
- * published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it would be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- */
-/**********************************************************
- *
- *    TEST IDENTIFIER   : flock05
- *
- *    EXECUTED BY       : anyone
- *
- *    TEST TITLE        : Testing different locks on flock(2)
- *
- *    TEST CASE TOTAL   : 2
- *
- *    AUTHOR            : Vatsal Avasthi <vatsal.avasthi@wipro.com>
- *
- *    SIGNALS
- *      Uses SIGUSR1 to pause before test if option set.
- *      (See the parse_opts(3) man page).
- *
- *    DESCRIPTION
- * 	Tests to verify flock(2) behavior with different locking combinations along
- *	with LOCK_EX.
- *    $
- *	Setup:
- *        Setup signal handling.
- *        Pause for SIGUSR1 if option specified.
- *        Create a temporary directory and chdir to it.
- * 	  Create a temporary file
- *
- *	Test:
- *	Loop if proper options are given.
- *		Parent flocks(2) a file
- *		fork() a child process
- * 		Child tries to flock() the already flocked file with different types of locks
- *		Check return code, if system call failed (return == -1)
- *				Log the error number and issue a FAIL message
- *		otherwise issue a PASS message
- *
- *      Cleanup:
- *        Print errno log and/or timing stats if options given
- *	  Deletes temporary directory.
- *
- * USAGE:  <for command-line>
- *      flock05 [-c n] [-e] [-i n] [-I x] [-P x] [-t] [-h] [-f] [-p]
- *                      where,  -c n : Run n copies concurrently.
- *                              -f   : Turn off functional testing
- *    				-e   : Turn on errno logging.
- *                              -h   : Show help screen                        $
- *				-i n : Execute test n times.
- *                              -I x : Execute test for x seconds.
- *                              -p   : Pause for SIGUSR1 before starting
- *                              -P x : Pause for x seconds between iterations.
- *                              -t   : Turn on syscall timing.
- *
- ****************************************************************/
-
-#include <errno.h>
-#include <stdio.h>
-#include <unistd.h>
-#include <sys/types.h>
-#include <sys/wait.h>
-#include <sys/file.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include "test.h"
-
-void setup(void);
-void cleanup(void);
-
-char *TCID = "flock05";
-int TST_TOTAL = 2;
-char filename[100];
-int fd, fd1, status;
-
-int main(int argc, char **argv)
-{
-	int lc, retval;
-	pid_t pid;
-
-	tst_parse_opts(argc, argv, NULL, NULL);
-
-	/* global setup */
-	setup();
-
-	/* The following loop checks looping state if -i option given */
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-
-		/* reset tst_count in case we are looping */
-		tst_count = 0;
-
-		/* Testing Shared lock on Exclusive Locked file */
-		TEST(flock(fd, LOCK_EX));
-		if (TEST_RETURN == 0) {
-
-			pid = FORK_OR_VFORK();
-			if (pid == 0) {
-				fd1 = open(filename, O_RDWR);
-				retval = flock(fd1, LOCK_SH | LOCK_NB);
-				if (retval == -1) {
-					tst_resm(TPASS,
-						 "flock() failed to acquire shared lock on an already"
-						 "exclusive locked file as expected");
-				} else {
-					tst_resm(TFAIL,
-						 "flock() unexpectedly PASSED in acquiring shared lock on "
-						 "an already exclusive locked file");
-				}
-				exit(0);
-			} else {
-				/* parent waiting */
-				wait(&status);
-			}
-
-			/* Testing Exclusive lock on a Exclusive Locked file */
-			pid = FORK_OR_VFORK();
-
-			if (pid == 0) {
-				fd1 = open(filename, O_RDWR);
-				retval = flock(fd1, LOCK_EX | LOCK_NB);
-				if (retval == -1) {
-					tst_resm(TPASS,
-						 "flock() failed to acquire exclusive lock on existing "
-						 " exclusive locked file as expected");
-				} else {
-					tst_resm(TFAIL,
-						 "flock() unexpectedly passed in acquiring exclusive lock on "
-						 "an exclusive locked file");
-				}
-				exit(0);
-			} else {
-				/* parent waiting */
-				wait(&status);
-			}
-			TEST(flock(fd, LOCK_UN));
-		} else {
-			tst_resm(TFAIL,
-				 "flock() failed to acquire exclusive lock");
-		}
-
-	}
-
-	close(fd);
-	close(fd1);
-	cleanup();
-	tst_exit();
-
-}
-
-/*
- * setup()
- *	performs all ONE TIME setup for this test
- */
-void setup(void)
-{
-
-	tst_sig(FORK, DEF_HANDLER, cleanup);
-
-	/* Pause if that option was specified
-	 * TEST_PAUSE contains the code to fork the test with the -i option.
-	 * You want to make sure you do this before you create your temporary
-	 * directory.
-	 */
-	TEST_PAUSE;
-
-	/* Create a unique temporary directory and chdir() to it. */
-	tst_tmpdir();
-
-	sprintf(filename, "flock05.%d", getpid());
-
-	/* creating temporary file */
-	fd = open(filename, O_CREAT | O_TRUNC | O_RDWR, 0666);
-	if (fd == -1) {
-		tst_resm(TFAIL, "creating a new file failed");
-
-		/* Removing temp dir */
-		tst_rmdir();
-
-	}
-}
-
-/*
- * cleanup()
- *	performs all ONE TIME cleanup for this test at
- * 	completion or premature exit
- */
-void cleanup(void)
-{
-
-	unlink(filename);
-
-	tst_rmdir();
-
-}
-- 
1.8.3.1




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

* [LTP] [PATCH v2 5/5] syscalls/flock06: Rewrite to new library
  2018-08-02 12:29   ` Jan Stancek
@ 2018-08-06  5:08     ` Jinhui huang
  2018-08-06  7:07       ` Jan Stancek
  0 siblings, 1 reply; 13+ messages in thread
From: Jinhui huang @ 2018-08-06  5:08 UTC (permalink / raw)
  To: ltp

Signed-off-by: Jinhui huang <huangjh.jy@cn.fujitsu.com>
---
 testcases/kernel/syscalls/flock/flock06.c | 213 ++++++++----------------------
 1 file changed, 52 insertions(+), 161 deletions(-)

diff --git a/testcases/kernel/syscalls/flock/flock06.c b/testcases/kernel/syscalls/flock/flock06.c
index 617eddf..9ef1280 100644
--- a/testcases/kernel/syscalls/flock/flock06.c
+++ b/testcases/kernel/syscalls/flock/flock06.c
@@ -1,177 +1,68 @@
-/*
+// SPDX-License-Identifier: GPL-2.0-or-later
+/* Copyright (c) Matthew Wilcox for Hewlett Packard 2003
+ * Author: Matthew Wilcox
  *
- *   Copyright (c) Matthew Wilcox for Hewlett Packard 2003
+ * Test Description:
+ *  This test verifies that flock locks held on one fd conflict with flock
+ *  locks held on a different fd.
  *
- *   This program is free software;  you can redistribute it and/or modify
- *   it under the terms of the GNU General Public License as published by
- *   the Free Software Foundation; either version 2 of the License, or
- *   (at your option) any later version.
- *
- *   This program is distributed in the hope that it will be useful,
- *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
- *   the GNU General Public License for more details.
- *
- *   You should have received a copy of the GNU General Public License
- *   along with this program;  if not, write to the Free Software
- *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ * Test Steps:
+ *  The process opens two file descriptors on the same file.  It acquires
+ *  an exclusive flock on the first descriptor, checks that attempting to
+ *  acquire an flock on the second descriptor fails.  Then it removes the
+ *  first descriptor's lock and attempts to acquire an exclusive lock on
+ *  the second descriptor.
  */
 
-/**********************************************************
- *
- *    TEST IDENTIFIER   : flock06
- *
- *    EXECUTED BY       : anyone
- *
- *    TEST TITLE        : Error condition test for flock(2)
- *
- *    TEST CASE TOTAL   : 1
- *
- *    AUTHOR            : Matthew Wilcox <willy@debian.org>
- *
- *    SIGNALS
- *      Uses SIGUSR1 to pause before test if option set.
- *      (See the parse_opts(3) man page).
- *
- *    DESCRIPTION
- * 		 This test verifies that flock locks held on one fd conflict with
- * 		 flock locks held on a different fd.
- *
- *		 Test:
- * 		 		 The process opens two file descriptors on the same file.
- * 		 		 It acquires an exclusive flock on the first descriptor,
- * 		 		 checks that attempting to acquire an flock on the second
- * 		 		 descriptor fails.  Then it removes the first descriptor's
- * 		 		 lock and attempts to acquire an exclusive lock on the
- * 		 		 second descriptor.
- *
- * USAGE:  <for command-line>
- *      flock06 [-c n] [-e] [-i n] [-I x] [-P x] [-t] [-h] [-f] [-p]
- *                      where,  -c n : Run n copies concurrently
- *                              -f   : Turn off functional testing
- *    		 		 		 		 -e   : Turn on errno logging
- *                              -h   : Show help screen
- *		 		 		 		 -i n : Execute test n times
- *                              -I x : Execute test for x seconds
- *                              -p   : Pause for SIGUSR1 before starting
- *                              -P x : Pause for x seconds between iterations
- *                              -t   : Turn on syscall timing
- *
- ****************************************************************/
-
-#include <stdio.h>
 #include <errno.h>
-#include <fcntl.h>
-#include <sys/types.h>
-#include <sys/stat.h>
 #include <sys/file.h>
-#include <sys/wait.h>
-#include "test.h"
-#include "safe_macros.h"
 
-void setup(void);
-void cleanup(void);
+#include "tst_test.h"
 
-char *TCID = "flock06";
-int TST_TOTAL = 3;
-char filename[100];
-
-int main(int argc, char **argv)
+static void verify_flock(void)
 {
-	int lc;
-
-	tst_parse_opts(argc, argv, NULL, NULL);
-
-	setup();
-
-	/* The following loop checks looping state if -i option given */
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		int fd1, fd2;
-
-		/* reset tst_count in case we are looping */
-		tst_count = 0;
-
-		fd1 = open(filename, O_RDWR);
-		if (fd1 == -1)
-			tst_brkm(TFAIL | TERRNO, cleanup,
-				 "failed to open the file");
-
-		TEST(flock(fd1, LOCK_EX | LOCK_NB));
-		if (TEST_RETURN != 0)
-			tst_resm(TFAIL | TTERRNO,
-				 "First attempt to flock() failed");
-		else
-			tst_resm(TPASS, "First attempt to flock() passed");
-
-		fd2 = open(filename, O_RDWR);
-		if (fd2 == -1)
-			tst_brkm(TFAIL | TERRNO, cleanup,
-				 "failed to open the file");
-
-		TEST(flock(fd2, LOCK_EX | LOCK_NB));
-		if (TEST_RETURN == -1)
-			tst_resm(TPASS, "Second attempt to flock() denied");
-		else
-			tst_resm(TFAIL, "Second attempt to flock() succeeded!");
-
-		TEST(flock(fd1, LOCK_UN));
-		if (TEST_RETURN == -1)
-			tst_resm(TFAIL | TTERRNO, "Failed to unlock fd1");
-		else
-			tst_resm(TPASS, "Unlocked fd1");
-
-		TEST(flock(fd2, LOCK_EX | LOCK_NB));
-		if (TEST_RETURN == -1)
-			tst_resm(TFAIL, "Third attempt to flock() denied!");
-		else
-			tst_resm(TPASS, "Third attempt to flock() succeeded");
-		close(fd1);
-		close(fd2);
-
-	}
-
-	cleanup();
-	tst_exit();
-
+	int fd1, fd2;
+
+	fd1 = SAFE_OPEN("testfile", O_RDWR);
+	TEST(flock(fd1, LOCK_EX | LOCK_NB));
+	if (TST_RET != 0)
+		tst_res(TFAIL | TTERRNO, "First attempt to flock() failed");
+	else
+		tst_res(TPASS, "First attempt to flock() passed");
+
+	fd2 = SAFE_OPEN("testfile", O_RDWR);
+	TEST(flock(fd2, LOCK_EX | LOCK_NB));
+	if (TST_RET == -1)
+		tst_res(TPASS | TTERRNO, "Second attempt to flock() denied");
+	else
+		tst_res(TFAIL, "Second attempt to flock() succeeded!");
+
+	TEST(flock(fd1, LOCK_UN));
+	if (TST_RET == -1)
+		tst_res(TFAIL | TTERRNO, "Failed to unlock fd1");
+	else
+		tst_res(TPASS, "Unlocked fd1");
+
+	TEST(flock(fd2, LOCK_EX | LOCK_NB));
+	if (TST_RET == -1)
+		tst_res(TFAIL | TTERRNO, "Third attempt to flock() denied!");
+	else
+		tst_res(TPASS, "Third attempt to flock() succeeded");
+
+	SAFE_CLOSE(fd1);
+	SAFE_CLOSE(fd2);
 }
 
-/*
- * setup()
- *		 performs all ONE TIME setup for this test
- */
-void setup(void)
+static void setup(void)
 {
 	int fd;
 
-	tst_sig(FORK, DEF_HANDLER, cleanup);
-
-	/* Pause if that option was specified
-	 * TEST_PAUSE contains the code to fork the test with the -i option.
-	 * You want to make sure you do this before you create your temporary
-	 * directory.
-	 */
-	TEST_PAUSE;
-
-	/* Create a unique temporary directory and chdir() to it. */
-	tst_tmpdir();
-
-	sprintf(filename, "flock06.%d", getpid());
-
-	/* creating temporary file */
-	fd = SAFE_OPEN(tst_rmdir, filename, O_CREAT | O_TRUNC | O_RDWR, 0666);
-	close(fd);
+	fd = SAFE_OPEN("testfile", O_CREAT | O_TRUNC | O_RDWR, 0666);
+	SAFE_CLOSE(fd);
 }
 
-/*
- * cleanup()
- *		 performs all ONE TIME cleanup for this test at
- * 		 completion or premature exit
- */
-void cleanup(void)
-{
-
-	unlink(filename);
-	tst_rmdir();
-
-}
+static struct tst_test test = {
+	.test_all = verify_flock,
+	.needs_tmpdir = 1,
+	.setup = setup,
+};
-- 
1.8.3.1




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

* [LTP] [PATCH v2 5/5] syscalls/flock06: Rewrite to new library
  2018-08-06  5:08     ` [LTP] [PATCH v2 " Jinhui huang
@ 2018-08-06  7:07       ` Jan Stancek
  0 siblings, 0 replies; 13+ messages in thread
From: Jan Stancek @ 2018-08-06  7:07 UTC (permalink / raw)
  To: ltp



----- Original Message -----
> Signed-off-by: Jinhui huang <huangjh.jy@cn.fujitsu.com>
> ---
>  testcases/kernel/syscalls/flock/flock06.c | 213
>  ++++++++----------------------
>  1 file changed, 52 insertions(+), 161 deletions(-)

Hi,

Series pushed with few minor tweaks:

flock01: fd initialized to -1, checked for >= 0 in cleanup()
flock02: added close() to a branch calling return in verify_flock()
flock04: string passed to child() function changed to reflect child parameters
flock06: two conditions made more strict by changing "== -1" to "!= 0"

Thanks,
Jan

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

end of thread, other threads:[~2018-08-06  7:07 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-08-02  3:53 [LTP] [PATCH 1/5] syscalls/flock01: Rewrite to new library Jinhui huang
2018-08-02  3:53 ` [LTP] [PATCH 2/5] syscalls/flock02: " Jinhui huang
2018-08-02 11:12   ` Jan Stancek
2018-08-02  3:53 ` [LTP] [PATCH 3/5] syscalls/flock03: " Jinhui huang
2018-08-02 11:20   ` Jan Stancek
2018-08-02  3:53 ` [LTP] [PATCH 4/5] syscalls/flock04: " Jinhui huang
2018-08-02 12:17   ` Jan Stancek
2018-08-06  5:07     ` [LTP] [PATCH v2 " Jinhui huang
2018-08-02  3:53 ` [LTP] [PATCH 5/5] syscalls/flock06: " Jinhui huang
2018-08-02 12:29   ` Jan Stancek
2018-08-06  5:08     ` [LTP] [PATCH v2 " Jinhui huang
2018-08-06  7:07       ` Jan Stancek
2018-08-02 11:03 ` [LTP] [PATCH 1/5] syscalls/flock01: " Jan Stancek

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.