Skip to contentSkip to conversation

flock02: Add test for EWOULDBLOCK errno

PATCH Superseded Avinesh Kumar 3 participants 4 messages Jan 14, 2025 – Jan 31, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 flock02: Add test for EWOULDBLOCK errno

Also fix a make check warning.

---
Following up on https://lore.kernel.org/ltp/1934768.7Z3S40VBb9@localhost/

 testcases/kernel/syscalls/flock/flock02.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
testcases/kernel/syscalls/flock/flock02.c +9 -1
index b8c7eee68..e2fe73a64 100644
@@ -13,6 +13,7 @@
  * - EBADF if the file descriptor is invalid
  * - EINVAL if the argument operation does not include LOCK_SH,LOCK_EX,LOCK_UN
  * - EINVAL if an invalid combination of locking modes is used i.e LOCK_SH with LOCK_EX
+ * - EWOULDBLOCK if the file is locked and the LOCK_NB flag was selected
  */
 
 #include <errno.h>
@@ -31,13 +32,19 @@ static struct tcase {
 	{&badfd, LOCK_SH, EBADF},
 	{&fd, LOCK_NB, EINVAL},
 	{&fd, LOCK_SH | LOCK_EX, EINVAL},
+	{&fd, LOCK_NB | LOCK_EX, EWOULDBLOCK}
 };
 
-static void verify_flock(unsigned n)
+static void verify_flock(unsigned int n)
 {
 	struct tcase *tc = &tcases[n];
 
 	fd = SAFE_OPEN("testfile", O_RDWR);
+	int fd2 = SAFE_OPEN("testfile", O_RDWR);
+
+	if (tc->exp_err == EWOULDBLOCK)
+		flock(fd2, LOCK_EX);
+
 	TEST(flock(*tc->fd, tc->operation));
 	if (TST_RET == 0) {
 		tst_res(TFAIL | TTERRNO, "flock() succeeded unexpectedly");
@@ -53,6 +60,7 @@ static void verify_flock(unsigned n)
 	}
 
 	SAFE_CLOSE(fd);
+	SAFE_CLOSE(fd2);
 }
 
 static void setup(void)
Signature · 5 lines
2.43.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by Andrea Cervesato on v1 1/1
#1 lore raw

Hi Avinesh,

Quoted 26 lines · Avinesh Kumar

Also fix a make check warning.

---
Following up on https://lore.kernel.org/ltp/1934768.7Z3S40VBb9@localhost/

  testcases/kernel/syscalls/flock/flock02.c | 10 +++++++++-
  1 file changed, 9 insertions(+), 1 deletion(-)
testcases/kernel/syscalls/flock/flock02.c +1 -0
index b8c7eee68..e2fe73a64 100644
@@ -13,6 +13,7 @@
   * - EBADF if the file descriptor is invalid
   * - EINVAL if the argument operation does not include LOCK_SH,LOCK_EX,LOCK_UN
   * - EINVAL if an invalid combination of locking modes is used i.e LOCK_SH with LOCK_EX
+ * - EWOULDBLOCK if the file is locked and the LOCK_NB flag was selected
   */
 
  #include <errno.h>
@@ -31,13 +32,19 @@ static struct tcase {
  	{&badfd, LOCK_SH, EBADF},
  	{&fd, LOCK_NB, EINVAL},
  	{&fd, LOCK_SH | LOCK_EX, EINVAL},

testcases/kernel/syscalls/flock/flock02.c static struct tcase { · line 43

Quoted 10 lines
testcases/kernel/syscalls/flock/flock02.c +3 -1
+	{&fd, LOCK_NB | LOCK_EX, EWOULDBLOCK}
  };
 
-static void verify_flock(unsigned n)
+static void verify_flock(unsigned int n)
  {
  	struct tcase *tc = &tcases[n];
 
  	fd = SAFE_OPEN("testfile", O_RDWR);
+	int fd2 = SAFE_OPEN("testfile", O_RDWR);

We can initialize fd2 at the beginning of verify_flock() with -1, open it only when tc->exp_err is EWOULDBLOCK and close it only when it's not -1. So we avoid to open one more testfile also for the other testcases.

Quoted 8 lines
testcases/kernel/syscalls/flock/flock02.c +4 -0
+
+	if (tc->exp_err == EWOULDBLOCK)
+		flock(fd2, LOCK_EX);
+
  	TEST(flock(*tc->fd, tc->operation));
  	if (TST_RET == 0) {
  		tst_res(TFAIL | TTERRNO, "flock() succeeded unexpectedly");

testcases/kernel/syscalls/flock/flock02.c static void verify_flock(unsigned n) · line 66

Quoted 7 lines
testcases/kernel/syscalls/flock/flock02.c +1 -0
@@ -53,6 +60,7 @@ static void verify_flock(unsigned n)
  	}
 
  	SAFE_CLOSE(fd);
+	SAFE_CLOSE(fd2);
  }
 
  static void setup(void)

With that small thing:

Kind regards,
Andrea

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

PATCH v2 1/1 flock02: Add test for EWOULDBLOCK errno

Also fix a make check warning.

---
 testcases/kernel/syscalls/flock/flock02.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
testcases/kernel/syscalls/flock/flock02.c +12 -1
index b8c7eee68..5c27c81a2 100644
@@ -13,6 +13,7 @@
  * - EBADF if the file descriptor is invalid
  * - EINVAL if the argument operation does not include LOCK_SH,LOCK_EX,LOCK_UN
  * - EINVAL if an invalid combination of locking modes is used i.e LOCK_SH with LOCK_EX
+ * - EWOULDBLOCK if the file is locked and the LOCK_NB flag was selected
  */
 
 #include <errno.h>
@@ -31,13 +32,21 @@ static struct tcase {
 	{&badfd, LOCK_SH, EBADF},
 	{&fd, LOCK_NB, EINVAL},
 	{&fd, LOCK_SH | LOCK_EX, EINVAL},
+	{&fd, LOCK_NB | LOCK_EX, EWOULDBLOCK}
 };
 
-static void verify_flock(unsigned n)
+static void verify_flock(unsigned int n)
 {
+	int fd2 = -1;
 	struct tcase *tc = &tcases[n];
 
 	fd = SAFE_OPEN("testfile", O_RDWR);
+
+	if (tc->exp_err == EWOULDBLOCK) {
+		fd2 = SAFE_OPEN("testfile", O_RDWR);
+		flock(fd2, LOCK_EX);
+	}
+
 	TEST(flock(*tc->fd, tc->operation));
 	if (TST_RET == 0) {
 		tst_res(TFAIL | TTERRNO, "flock() succeeded unexpectedly");
@@ -53,6 +62,8 @@ static void verify_flock(unsigned n)
 	}
 
 	SAFE_CLOSE(fd);
+	if (fd2 != -1)
+		SAFE_CLOSE(fd2);
 }
 
 static void setup(void)
Signature · 5 lines
2.43.0


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

Hi all,

v2 merged also with Andrea's RBT.
Thanks!

Kind regards,
Petr

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