Skip to content

Commit 4138849

Browse files
committed
Check that flopen() can lock against self and that children inherit the lock.
1 parent 5649afd commit 4138849

File tree

1 file changed

+41
-6
lines changed

1 file changed

+41
-6
lines changed

tools/regression/lib/libutil/test-flopen.c

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <sys/cdefs.h>
3131
__FBSDID("$FreeBSD$");
3232

33+
#include <sys/signal.h>
3334
#include <sys/types.h>
3435
#include <sys/fcntl.h>
3536

@@ -89,14 +90,13 @@ test_flopen_open(void)
8990
return (result);
9091
}
9192

92-
#if FLOPEN_CAN_LOCK_AGAINST_SELF
9393
/*
9494
* Test that flopen() can lock against itself
9595
*/
9696
const char *
9797
test_flopen_lock_self(void)
9898
{
99-
const char *fn = "test_flopen_lock";
99+
const char *fn = "test_flopen_lock_self";
100100
const char *result = NULL;
101101
int fd1, fd2;
102102

@@ -115,15 +115,14 @@ test_flopen_lock_self(void)
115115
unlink(fn);
116116
return (result);
117117
}
118-
#endif
119118

120119
/*
121120
* Test that flopen() can lock against other processes
122121
*/
123122
const char *
124123
test_flopen_lock_other(void)
125124
{
126-
const char *fn = "test_flopen_lock";
125+
const char *fn = "test_flopen_lock_other";
127126
const char *result = NULL;
128127
volatile int fd1, fd2;
129128

@@ -148,16 +147,52 @@ test_flopen_lock_other(void)
148147
return (result);
149148
}
150149

150+
/*
151+
* Test that child processes inherit the lock
152+
*/
153+
const char *
154+
test_flopen_lock_child(void)
155+
{
156+
const char *fn = "test_flopen_lock_child";
157+
const char *result = NULL;
158+
pid_t pid;
159+
volatile int fd1, fd2;
160+
161+
unlink(fn);
162+
fd1 = flopen(fn, O_RDWR|O_CREAT, 0640);
163+
if (fd1 < 0) {
164+
result = strerror(errno);
165+
} else {
166+
if ((pid = fork()) == 0) {
167+
select(0, 0, 0, 0, 0);
168+
_exit(0);
169+
}
170+
close(fd1);
171+
fd2 = -42;
172+
if (vfork() == 0) {
173+
fd2 = flopen(fn, O_RDWR|O_NONBLOCK);
174+
close(fd2);
175+
_exit(0);
176+
}
177+
if (fd2 == -42)
178+
result = "vfork() doesn't work as expected";
179+
if (fd2 >= 0)
180+
result = "second open succeeded";
181+
kill(pid, SIGINT);
182+
}
183+
unlink(fn);
184+
return (result);
185+
}
186+
151187
static struct test {
152188
const char *name;
153189
const char *(*func)(void);
154190
} t[] = {
155191
{ "flopen_create", test_flopen_create },
156192
{ "flopen_open", test_flopen_open },
157-
#if FLOPEN_CAN_LOCK_AGAINST_SELF
158193
{ "flopen_lock_self", test_flopen_lock_self },
159-
#endif
160194
{ "flopen_lock_other", test_flopen_lock_other },
195+
{ "flopen_lock_child", test_flopen_lock_child },
161196
};
162197

163198
int

0 commit comments

Comments
 (0)