Skip to content

Commit ecb6ca2

Browse files
Avineshpevik
authored andcommitted
flock: Add test for verifying EINTR errno
Link: https://lore.kernel.org/ltp/[email protected]/ Reviewed-by: Petr Vorel <[email protected]> Signed-off-by: Yang Xu <[email protected]> Signed-off-by: Avinesh Kumar <[email protected]>
1 parent 0f38cb8 commit ecb6ca2

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

runtest/syscalls

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ flock02 flock02
385385
flock03 flock03
386386
flock04 flock04
387387
flock06 flock06
388+
flock07 flock07
388389

389390
fmtmsg01 fmtmsg01
390391

testcases/kernel/syscalls/flock/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
/flock03
44
/flock04
55
/flock06
6+
/flock07
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
/*
3+
* Copyright (c) 2024 FUJITSU LIMITED. All Rights Reserved.
4+
* Author: Yang Xu <[email protected]>
5+
* Copyright (c) 2024 Linux Test Project
6+
*/
7+
8+
/*\
9+
* [Description]
10+
*
11+
* Verify that flock(2) fails with errno EINTR when waiting to acquire a lock,
12+
* and the call is interrupted by a signal.
13+
*/
14+
15+
#include <sys/file.h>
16+
#include "tst_test.h"
17+
18+
#define TEMPFILE "test_eintr"
19+
20+
static int fd1 = -1, fd2 = -1;
21+
22+
static void handler(int sig)
23+
{
24+
tst_res(TINFO, "Received signal: %s (%d)", tst_strsig(sig), sig);
25+
}
26+
27+
static void setup(void)
28+
{
29+
SAFE_TOUCH(TEMPFILE, 0777, NULL);
30+
fd1 = SAFE_OPEN(TEMPFILE, O_RDWR);
31+
fd2 = SAFE_OPEN(TEMPFILE, O_RDWR);
32+
}
33+
34+
static void cleanup(void)
35+
{
36+
if (fd1 >= 0)
37+
SAFE_CLOSE(fd1);
38+
39+
if (fd2 >= 0)
40+
SAFE_CLOSE(fd2);
41+
}
42+
43+
static void child_do(int fd)
44+
{
45+
struct sigaction sa;
46+
47+
sa.sa_handler = handler;
48+
SAFE_SIGEMPTYSET(&sa.sa_mask);
49+
SAFE_SIGACTION(SIGUSR1, &sa, NULL);
50+
51+
tst_res(TINFO, "Trying to acquire exclusive lock from child");
52+
TST_EXP_FAIL(flock(fd, LOCK_EX), EINTR);
53+
}
54+
55+
static void verify_flock(void)
56+
{
57+
pid_t pid;
58+
59+
TST_EXP_PASS(flock(fd1, LOCK_EX));
60+
61+
pid = SAFE_FORK();
62+
if (!pid) {
63+
child_do(fd2);
64+
exit(0);
65+
} else {
66+
sleep(1);
67+
SAFE_KILL(pid, SIGUSR1);
68+
SAFE_WAITPID(pid, NULL, 0);
69+
}
70+
}
71+
72+
static struct tst_test test = {
73+
.setup = setup,
74+
.cleanup = cleanup,
75+
.test_all = verify_flock,
76+
.needs_tmpdir = 1,
77+
.needs_root = 1,
78+
.forks_child = 1,
79+
};

0 commit comments

Comments
 (0)