Skip to contentSkip to conversation

flock: Add negative tests for flock

PATCH In review Yang Xu via ltp 3 participants 13 messages Apr 16, 2024 – Feb 3, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 flock: Add negative tests for flock

Add negative cases for flock(), when errno is EINTR or EWOULDBLOCK

---
 runtest/syscalls                           |  1 +
 testcases/kernel/syscalls/flock/.gitignore |  1 +
 testcases/kernel/syscalls/flock/flock07.c  | 98 ++++++++++++++++++++++
 3 files changed, 100 insertions(+)
 create mode 100644 testcases/kernel/syscalls/flock/flock07.c
runtest/syscalls +1 -0
index 9578e991a..de4f5a633 100644
@@ -375,6 +375,7 @@ flock02 flock02
 flock03 flock03
 flock04 flock04
 flock06 flock06
+flock07 flock07
 
 fmtmsg01 fmtmsg01
 
testcases/kernel/syscalls/flock/.gitignore +1 -0
index c8cb0fc54..9bac582e1 100644
@@ -3,3 +3,4 @@
 /flock03
 /flock04
 /flock06
+/flock07
testcases/kernel/syscalls/flock/flock07.c new file +98 -0

98 lines not on this page · raw message

Signature · 5 lines
2.39.3


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

Hi Yang Xu,

When I am testing this by running multiple times, EINTR test is getting timed out randomly.

tst_test.c:1625: TINFO: Timeout per run is 0h 00m 30s
flock07.c:46: TINFO: Got SIGUSR1
Test timeouted, sending SIGKILL!
tst_test.c:1673: TINFO: Killed the leftover descendant processes tst_test.c:1679: TINFO: If you are running on slow machine, try exporting LTP_TIMEOUT_MUL > 1 tst_test.c:1681: TBROK: Test killed! (timeout?)

In these cases I see that there are two locks (held or waiting) by the process even after SIGUSER1 signal is delivered, so somehow signal is being ignored.

flock07         16520  FLOCK        WRITE* 0          0          0 /tmp...
flock07         16519  FLOCK        WRITE  0          0          0 /tmp...

And I feel we should add EWOULDBLOCK errno test to flock02.c file, and keep only EINTR testcase here.

Quoted 126 lines · On Tuesday, April 16, 2024 10:02:37 AM GMT+2 Yang Xu via ltp wrote:

Add negative cases for flock(), when errno is EINTR or EWOULDBLOCK

---
 runtest/syscalls                           |  1 +
 testcases/kernel/syscalls/flock/.gitignore |  1 +
 testcases/kernel/syscalls/flock/flock07.c  | 98 ++++++++++++++++++++++
 3 files changed, 100 insertions(+)
 create mode 100644 testcases/kernel/syscalls/flock/flock07.c
runtest/syscalls +1 -0
index 9578e991a..de4f5a633 100644
@@ -375,6 +375,7 @@ flock02 flock02
 flock03 flock03
 flock04 flock04
 flock06 flock06
+flock07 flock07
 
 fmtmsg01 fmtmsg01
 
testcases/kernel/syscalls/flock/.gitignore +1 -0
index c8cb0fc54..9bac582e1 100644
@@ -3,3 +3,4 @@
 /flock03
 /flock04
 /flock06
+/flock07
testcases/kernel/syscalls/flock/flock07.c new file +88 -0
new file mode 100644
index 000000000..6fd650186
@@ -0,0 +1,98 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2024 FUJITSU LIMITED. All Rights Reserved.
+ * Author: Yang Xu <[email protected]>
+ */
+
+/*\
+ * [Description]
+ *
+ * Verify that flock(2) fails with
+ *
+ * - EINTR when waiting lock, call is interrupted by signal
+ * - EWOULDBLOCK when file is locked and LOCK_NB flag is selected
+ */
+
+#include <signal.h>
+#include <sys/file.h>
+#include <sys/wait.h>
+#include "tst_test.h"
+
+#define TEST_INTR "test_intr"
+#define TEST_EWOULDBLOCK "test_ewouldblock"
+
+static struct test_case_t {
+	char *filename;
+	int expected_errno;
+	int child;
+	char *desc;
+} tcases[] = {
+	{TEST_INTR, EINTR, 1,
+		"while waiting lock, call is interrupted by signal"},
+	{TEST_EWOULDBLOCK, EWOULDBLOCK, 0,
+		"file is locked and LOCK_NB flag is selected"},
+};
+
+static void setup(void)
+{
+	SAFE_TOUCH(TEST_INTR, 0777, NULL);
+	SAFE_TOUCH(TEST_EWOULDBLOCK, 0777, NULL);
+}
+
+static void handler(int sig)
+{
+	switch (sig) {
+	case SIGUSR1:
+		tst_res(TINFO, "%s", "Got SIGUSR1");
+	break;
+	default:
+		tst_res(TINFO, "%s", "Got other signal");
+	break;
+	}
+}
+
+static void child_do(int fd, struct test_case_t *tc)
+{
+	struct sigaction sa;
+
+	sa.sa_handler = handler;
+	SAFE_SIGEMPTYSET(&sa.sa_mask);
+	SAFE_SIGACTION(SIGUSR1, &sa, NULL);
+
+	TST_EXP_FAIL(flock(fd, LOCK_EX), tc->expected_errno, "%s", tc->desc);
+}
+
+static void verify_flock(unsigned int i)
+{
+	struct test_case_t *tc = &tcases[i];
+	pid_t pid;
+	int fd1 = SAFE_OPEN(tc->filename, O_RDWR);
+	int fd2 = SAFE_OPEN(tc->filename, O_RDWR);
+
+	if (tc->child) {
+		flock(fd1, LOCK_EX);
+		pid = SAFE_FORK();
+		if (!pid) {
+			child_do(fd2, tc);
+			exit(0);
+		}
+		sleep(1);
+		SAFE_KILL(pid, SIGUSR1);
+		SAFE_WAITPID(pid, NULL, 0);
+	} else {
+		flock(fd1, LOCK_EX);
+		TST_EXP_FAIL(flock(fd2, LOCK_EX | LOCK_NB), tc->expected_errno,
+			"%s", tc->desc);
+	}
+	SAFE_CLOSE(fd1);
+	SAFE_CLOSE(fd2);

testcases/kernel/syscalls/flock/flock07.c line 98

Quoted 10 lines
testcases/kernel/syscalls/flock/flock07.c new file +10 -0
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.tcnt = ARRAY_SIZE(tcases),
+	.test = verify_flock,
+	.needs_tmpdir = 1,
+	.needs_root = 1,
+	.forks_child = 1,
+};

Regards,
Avinesh

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw

Hi Yang Xu, Avinesh,

Quoted 16 lines

Hi Yang Xu,

When I am testing this by running multiple times, EINTR test is getting timed out randomly.

tst_test.c:1625: TINFO: Timeout per run is 0h 00m 30s
flock07.c:46: TINFO: Got SIGUSR1
Test timeouted, sending SIGKILL!
tst_test.c:1673: TINFO: Killed the leftover descendant processes tst_test.c:1679: TINFO: If you are running on slow machine, try exporting LTP_TIMEOUT_MUL > 1 tst_test.c:1681: TBROK: Test killed! (timeout?)

In these cases I see that there are two locks (held or waiting) by the process even after SIGUSER1 signal is delivered, so somehow signal is being ignored.

Quoted 6 lines
flock07         16520  FLOCK        WRITE* 0          0          0 /tmp...
flock07         16519  FLOCK        WRITE  0          0          0 /tmp...

And I feel we should add EWOULDBLOCK errno test to flock02.c file, and keep only EINTR testcase here.

There are v2 and v3 sent by Avinesh, therefore I set this in patchwork as "Changes requested". Please correct me if I'm wrong.

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw

PATCH v2 1/1 flock: Add test for verifying EINTR errno

In reply to Yang Xu via ltp

Modified Yang Xu's v1 patch and kept only EINTR test here.

---
 testcases/kernel/syscalls/flock/flock07.c | 70 +++++++++++++++++++++++
 1 file changed, 70 insertions(+)
 create mode 100644 testcases/kernel/syscalls/flock/flock07.c
testcases/kernel/syscalls/flock/flock07.c new file +70 -0
new file mode 100644
index 000000000..b2de84905
@@ -0,0 +1,70 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2024 FUJITSU LIMITED. All Rights Reserved.
+ * Author: Yang Xu <[email protected]>
+ * Copyright (c) 2024 Linux Test Project
+ */
+
+/*\
+ * [Description]
+ *
+ * Verify that flock(2) fails with errno EINTR when waiting to acquire a lock,
+ * and the call is interrupted by a signal.
+ */
+
+#include <sys/file.h>
+#include "tst_test.h"
+
+#define TEMPFILE "test_eintr"
+
+static void handler(int sig)
+{
+	tst_res(TINFO, "Received signal: %d", sig);
+}
+
+static void setup(void)
+{
+	SAFE_TOUCH(TEMPFILE, 0777, NULL);
+}
+
+static void child_do(int fd)
+{
+	struct sigaction sa;
+
+	sa.sa_handler = handler;
+	SAFE_SIGEMPTYSET(&sa.sa_mask);
+	SAFE_SIGACTION(SIGUSR1, &sa, NULL);
+
+	tst_res(TINFO, "Trying to acquire exclusive lock from child");
+	TST_EXP_FAIL(flock(fd, LOCK_EX), EINTR);
+}
+
+static void verify_flock(void)
+{
+	pid_t pid;
+	int fd1 = SAFE_OPEN(TEMPFILE, O_RDWR);
+	int fd2 = SAFE_OPEN(TEMPFILE, O_RDWR);
+
+	TST_EXP_PASS(flock(fd1, LOCK_EX));
+
+	pid = SAFE_FORK();
+	if (!pid) {
+		child_do(fd2);
+		exit(0);
+	} else {
+		sleep(1);
+		SAFE_KILL(pid, SIGUSR1);
+		SAFE_WAITPID(pid, NULL, 0);
+	}
+
+	SAFE_CLOSE(fd1);
+	SAFE_CLOSE(fd2);
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.test_all = verify_flock,
+	.needs_tmpdir = 1,
+	.needs_root = 1,
+	.forks_child = 1,
+};
Signature · 5 lines
2.45.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#4 lore raw

PATCH v3 1/1 flock: Add test for verifying EINTR errno

---
 runtest/syscalls                           |  1 +
 testcases/kernel/syscalls/flock/.gitignore |  1 +
 testcases/kernel/syscalls/flock/flock07.c  | 70 ++++++++++++++++++++++
 3 files changed, 72 insertions(+)
 create mode 100644 testcases/kernel/syscalls/flock/flock07.c
runtest/syscalls +1 -0
index cf06ee563..091453fec 100644
@@ -374,6 +374,7 @@ flock02 flock02
 flock03 flock03
 flock04 flock04
 flock06 flock06
+flock07 flock07
 
 fmtmsg01 fmtmsg01
 
testcases/kernel/syscalls/flock/.gitignore +1 -0
index c8cb0fc54..9bac582e1 100644
@@ -3,3 +3,4 @@
 /flock03
 /flock04
 /flock06
+/flock07
testcases/kernel/syscalls/flock/flock07.c new file +70 -0

70 lines not on this page · raw message

Signature · 5 lines
2.45.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#5 lore raw

Re: [LTP] [PATCH v3] flock: Add test for verifying EINTR errno

Hi Avinesh, Yang Xu,

Quoted 2 lines

...

Quoted 5 lines
testcases/kernel/syscalls/flock/flock07.c new file +2 -0
@@ -0,0 +1,70 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*

testcases/kernel/syscalls/flock/flock07.c line 12

Quoted 10 lines
testcases/kernel/syscalls/flock/flock07.c new file +10 -0
+ * Copyright (c) 2024 FUJITSU LIMITED. All Rights Reserved.
+ * Author: Yang Xu <[email protected]>
+ * Copyright (c) 2024 Linux Test Project
+ */
+
+/*\
+ * [Description]
+ *
+ * Verify that flock(2) fails with errno EINTR when waiting to acquire a lock,
+ * and the call is interrupted by a signal.

Avinesh, you mentioned at Yang Xu's v1 [1] that EINTR test is getting timed out randomly. I also experienced timeouts on aarch64 and ppc64le. v1 had 2 tests (EINTR and EWOULDBLOCK), you here posted only EINTR. I would expect you would put here only the other one - EWOULDBLOCK. Or am I missing something?

Kind regards,
Petr

[1] https://lore.kernel.org/ltp/1934768.7Z3S40VBb9@localhost/

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#6 lore raw

Re: [LTP] [PATCH v3] flock: Add test for verifying EINTR errno

Quoted 17 lines · CET Petr Vorel

Hi Avinesh, Yang Xu,

Quoted 2 lines

...

Quoted 12 lines
testcases/kernel/syscalls/flock/flock07.c new file +9 -0
@@ -0,0 +1,70 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2024 FUJITSU LIMITED. All Rights Reserved.
+ * Author: Yang Xu <[email protected]>
+ * Copyright (c) 2024 Linux Test Project
+ */
+
+/*\
+ * [Description]
Quoted 8 lines

testcases/kernel/syscalls/flock/flock07.c line 12

Quoted 3 lines
testcases/kernel/syscalls/flock/flock07.c new file +3 -0
+ *
+ * Verify that flock(2) fails with errno EINTR when waiting to acquire a lock,
+ * and the call is interrupted by a signal.

Avinesh, you mentioned at Yang Xu's v1 [1] that EINTR test is getting timed out randomly. I also experienced timeouts on aarch64 and ppc64le. v1 had 2 tests (EINTR and EWOULDBLOCK), you here posted only EINTR. I would expect you would put here only the other one - EWOULDBLOCK. Or am I missing something?

Hi Petr,

I am sorry, I completely missed this.
I sent the patch for EWOULDBLOCK case now -
https://lore.kernel.org/ltp/[email protected]/T/#u

Regards,
Avinesh

Quoted 6 lines

Kind regards,
Petr

[1] https://lore.kernel.org/ltp/1934768.7Z3S40VBb9@localhost/

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#7 lore raw

Re: [LTP] [PATCH v3] flock: Add test for verifying EINTR errno

Quoted 26 lines · CET Petr Vorel
Quoted 17 lines

Hi Avinesh, Yang Xu,

Quoted 2 lines

...

Quoted 12 lines
testcases/kernel/syscalls/flock/flock07.c new file +9 -0
@@ -0,0 +1,70 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2024 FUJITSU LIMITED. All Rights Reserved.
+ * Author: Yang Xu <[email protected]>
+ * Copyright (c) 2024 Linux Test Project
+ */
+
+/*\
+ * [Description]
Quoted 8 lines

testcases/kernel/syscalls/flock/flock07.c line 12

Quoted 3 lines
testcases/kernel/syscalls/flock/flock07.c new file +3 -0
+ *
+ * Verify that flock(2) fails with errno EINTR when waiting to acquire a lock,
+ * and the call is interrupted by a signal.

Avinesh, you mentioned at Yang Xu's v1 [1] that EINTR test is getting timed out randomly. I also experienced timeouts on aarch64 and ppc64le. v1 had 2 tests (EINTR and EWOULDBLOCK), you here posted only EINTR. I would expect you would put here only the other one - EWOULDBLOCK. Or am I missing something?

Quoted 5 lines

Hi Petr,

I am sorry, I completely missed this.
I sent the patch for EWOULDBLOCK case now -
https://lore.kernel.org/ltp/[email protected]/T/#u

Thanks for info. I'm closing this as rejected. Please let me know if I did not understand you and this is a valid patch.

Kind regards,
Petr

Quoted 7 lines

Regards,
Avinesh

Quoted 4 lines

Kind regards,
Petr

[1] https://lore.kernel.org/ltp/1934768.7Z3S40VBb9@localhost/

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#8 lore raw

Re: [LTP] [PATCH v3] flock: Add test for verifying EINTR errno

Quoted 26 lines · CET Petr Vorel
Quoted 26 lines · CET Petr Vorel
Quoted 25 lines

Hi Avinesh, Yang Xu,

Quoted 2 lines

...

Quoted 5 lines
testcases/kernel/syscalls/flock/flock07.c new file +2 -0
@@ -0,0 +1,70 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*

testcases/kernel/syscalls/flock/flock07.c line 12

Quoted 10 lines
testcases/kernel/syscalls/flock/flock07.c new file +10 -0
+ * Copyright (c) 2024 FUJITSU LIMITED. All Rights Reserved.
+ * Author: Yang Xu <[email protected]>
+ * Copyright (c) 2024 Linux Test Project
+ */
+
+/*\
+ * [Description]
+ *
+ * Verify that flock(2) fails with errno EINTR when waiting to acquire a lock,
+ * and the call is interrupted by a signal.

Avinesh, you mentioned at Yang Xu's v1 [1] that EINTR test is getting timed out randomly. I also experienced timeouts on aarch64 and ppc64le. v1 had 2 tests (EINTR and EWOULDBLOCK), you here posted only EINTR. I would expect you would put here only the other one - EWOULDBLOCK. Or am I missing something?

Quoted 8 lines
Quoted 5 lines

Hi Petr,

I am sorry, I completely missed this.
I sent the patch for EWOULDBLOCK case now -
https://lore.kernel.org/ltp/[email protected]/T/#u

Thanks for info. I'm closing this as rejected. Please let me know if I did not understand you and this is a valid patch.

Hi Petr, the above v3 patch (for new test of flock/EINTR errno) is still valid. I took out the EWOULDBLOCK test from the original patch (by Yang Xu) and added that to flock02, you already merged that one.
So you can review flock07.c patch just for the EINTR case.

Thanks,
Avinesh

Quoted 17 lines

Kind regards,
Petr

Quoted 7 lines

Regards,
Avinesh

Quoted 4 lines

Kind regards,
Petr

[1] https://lore.kernel.org/ltp/1934768.7Z3S40VBb9@localhost/

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#9 lore raw

Re: [LTP] [PATCH v3] flock: Add test for verifying EINTR errno

Hi Avinesh,

...

Quoted 6 lines
Quoted 5 lines
Quoted 5 lines

Hi Petr,

I am sorry, I completely missed this.
I sent the patch for EWOULDBLOCK case now -
https://lore.kernel.org/ltp/[email protected]/T/#u

Quoted 7 lines
Quoted 2 lines

Thanks for info. I'm closing this as rejected. Please let me know if I did not understand you and this is a valid patch.

Hi Petr, the above v3 patch (for new test of flock/EINTR errno) is still valid. I took out the EWOULDBLOCK test from the original patch (by Yang Xu) and added that to flock02, you already merged that one.
So you can review flock07.c patch just for the EINTR case.

Thanks for info, I reopened it in patchwork and I'll have look later.

I see, the crash is workarounded by forking child and verifying, that's why it's not in flock02.c (which has other errnos, which don't need a workaround with forking).

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#10 lore raw

Re: [LTP] [PATCH v3] flock: Add test for verifying EINTR errno

In reply to Avinesh Kumar

Hi Avinesh, all,

...

Quoted 1 line

+++ b/testcases/kernel/syscalls/flock/flock07.c

...

testcases/kernel/syscalls/flock/flock07.c line 22

Quoted 3 lines
testcases/kernel/syscalls/flock/flock07.c new file +3 -0
+static void handler(int sig)
+{
+	tst_res(TINFO, "Received signal: %d", sig);

How about print a signal constant/name?

	tst_res(TINFO, "Received signal: %s (%d)", tst_strsig(sig), sig);
Quoted 1 line

+}

...

testcases/kernel/syscalls/flock/flock07.c line 46

Quoted 5 lines
testcases/kernel/syscalls/flock/flock07.c new file +5 -0
+static void verify_flock(void)
+{
+	pid_t pid;
+	int fd1 = SAFE_OPEN(TEMPFILE, O_RDWR);
+	int fd2 = SAFE_OPEN(TEMPFILE, O_RDWR);

How about to setup file descriptors in setup() and close them in cleanup()?

I suggest to merge with the change below.

Quoted 6 lines
testcases/kernel/syscalls/flock/flock07.c new file +6 -0
+
+	TST_EXP_PASS(flock(fd1, LOCK_EX));
+
+	pid = SAFE_FORK();
+	if (!pid) {
+		child_do(fd2);

testcases/kernel/syscalls/flock/flock07.c line 62

Quoted 10 lines
testcases/kernel/syscalls/flock/flock07.c new file +10 -0
+		exit(0);
+	} else {
+		sleep(1);
+		SAFE_KILL(pid, SIGUSR1);
+		SAFE_WAITPID(pid, NULL, 0);
+	}
+
+	SAFE_CLOSE(fd1);
+	SAFE_CLOSE(fd2);
+}

Kind regards,
Petr

+++ testcases/kernel/syscalls/flock/flock07.c
@@ -17,14 +17,27 @@

#define TEMPFILE "test_eintr"

+static int fd1 = -1, fd2 = -1;
+
static void handler(int sig)
{
- tst_res(TINFO, "Received signal: %d", sig);
+ tst_res(TINFO, "Received signal: %s (%d)", tst_strsig(sig), sig);
}

static void setup(void)
{
SAFE_TOUCH(TEMPFILE, 0777, NULL);
+ fd1 = SAFE_OPEN(TEMPFILE, O_RDWR);
+ fd2 = SAFE_OPEN(TEMPFILE, O_RDWR);
+}
+
+static void cleanup(void)
+{
+ if (fd1 >= 0)
+ SAFE_CLOSE(fd1);
+
+ if (fd2 >= 0)
+ SAFE_CLOSE(fd2);
}

static void child_do(int fd)
@@ -42,8 +55,6 @@ static void child_do(int fd)
static void verify_flock(void)
{
pid_t pid;
- int fd1 = SAFE_OPEN(TEMPFILE, O_RDWR);
- int fd2 = SAFE_OPEN(TEMPFILE, O_RDWR);

TST_EXP_PASS(flock(fd1, LOCK_EX));

@@ -56,13 +67,11 @@ static void verify_flock(void)
SAFE_KILL(pid, SIGUSR1); SAFE_WAITPID(pid, NULL, 0);
}
-
- SAFE_CLOSE(fd1);
- SAFE_CLOSE(fd2);
}

static struct tst_test test = {
.setup = setup,
+ .cleanup = cleanup,
.test_all = verify_flock,
.needs_tmpdir = 1,
.needs_root = 1,

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#11 lore raw

Re: [LTP] [PATCH v3] flock: Add test for verifying EINTR errno

Quoted 13 lines · CET Petr Vorel

Hi Avinesh, all,

...

Quoted 1 line

+++ b/testcases/kernel/syscalls/flock/flock07.c

...

testcases/kernel/syscalls/flock/flock07.c line 22

Quoted 3 lines
testcases/kernel/syscalls/flock/flock07.c new file +3 -0
+static void handler(int sig)
+{
+	tst_res(TINFO, "Received signal: %d", sig);

How about print a signal constant/name?

	tst_res(TINFO, "Received signal: %s (%d)", tst_strsig(sig), sig);
Quoted 1 line

+}

...

Quoted 8 lines

testcases/kernel/syscalls/flock/flock07.c line 46

Quoted 5 lines
testcases/kernel/syscalls/flock/flock07.c new file +5 -0
+static void verify_flock(void)
+{
+	pid_t pid;
+	int fd1 = SAFE_OPEN(TEMPFILE, O_RDWR);
+	int fd2 = SAFE_OPEN(TEMPFILE, O_RDWR);

How about to setup file descriptors in setup() and close them in cleanup()?

I suggest to merge with the change below.

Hi Petr,

Thank you for the review, and I agree with your suggestions.

Regards,
Avinesh

Quoted 76 lines
Quoted 7 lines
testcases/kernel/syscalls/flock/flock07.c new file +6 -0
+
+	TST_EXP_PASS(flock(fd1, LOCK_EX));
+
+	pid = SAFE_FORK();
+	if (!pid) {
+		child_do(fd2);

testcases/kernel/syscalls/flock/flock07.c line 62

Quoted 10 lines
testcases/kernel/syscalls/flock/flock07.c new file +10 -0
+		exit(0);
+	} else {
+		sleep(1);
+		SAFE_KILL(pid, SIGUSR1);
+		SAFE_WAITPID(pid, NULL, 0);
+	}
+
+	SAFE_CLOSE(fd1);
+	SAFE_CLOSE(fd2);
+}

Kind regards,
Petr

+++ testcases/kernel/syscalls/flock/flock07.c

(quoted diff) +15 -6
@@ -17,14 +17,27 @@
 
 #define TEMPFILE "test_eintr"
 
+static int fd1 = -1, fd2 = -1;
+
 static void handler(int sig)
 {
-	tst_res(TINFO, "Received signal: %d", sig);
+	tst_res(TINFO, "Received signal: %s (%d)", tst_strsig(sig), sig);
 }
 
 static void setup(void)
 {
 	SAFE_TOUCH(TEMPFILE, 0777, NULL);
+	fd1 = SAFE_OPEN(TEMPFILE, O_RDWR);
+	fd2 = SAFE_OPEN(TEMPFILE, O_RDWR);
+}
+
+static void cleanup(void)
+{
+	if (fd1 >= 0)
+		SAFE_CLOSE(fd1);
+
+	if (fd2 >= 0)
+		SAFE_CLOSE(fd2);
 }
 
 static void child_do(int fd)
@@ -42,8 +55,6 @@ static void child_do(int fd)
 static void verify_flock(void)
 {
 	pid_t pid;
-	int fd1 = SAFE_OPEN(TEMPFILE, O_RDWR);
-	int fd2 = SAFE_OPEN(TEMPFILE, O_RDWR);
 
 	TST_EXP_PASS(flock(fd1, LOCK_EX));
 
@@ -56,13 +67,11 @@ static void verify_flock(void)
 		SAFE_KILL(pid, SIGUSR1);
 		SAFE_WAITPID(pid, NULL, 0);
 	}
-
-	SAFE_CLOSE(fd1);
-	SAFE_CLOSE(fd2);
 }
 
 static struct tst_test test = {
 	.setup = setup,
+	.cleanup = cleanup,
 	.test_all = verify_flock,
 	.needs_tmpdir = 1,
 	.needs_root = 1,
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#12 lore raw

Re: [LTP] [PATCH v3] flock: Add test for verifying EINTR errno

Hi Avinesh, all,

...

Quoted 3 lines

Hi Petr,

Thank you for the review, and I agree with your suggestions.

Good, merged. Thank you!

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp