1 /* $NetBSD: t_lockf.c,v 1.9 2013/10/19 17:45:00 christos Exp $ */
4 * Copyright (c) 2000 The NetBSD Foundation, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
39 #include <sys/types.h>
41 #include <sys/ptrace.h>
44 * lockf1 regression test:
47 * Fork N child processes, each of which gets M random byte range locks
48 * on a common file. We ignore all lock errors (practically speaking,
49 * this means EDEADLK or ENOLOCK), but we make numerous passes over all
50 * the children to make sure that they are still awake. (We do this by
51 * verifying that we can ptrace(ATTACH/DETACH) to the children and get
52 * their status via waitpid().)
53 * When finished, reap all the children.
56 #define nlocks 500 /* number of locks per thread */
57 #define nprocs 10 /* number of processes to spawn */
58 #define npasses 50 /* number of passes to make over the children */
59 #define sleeptime 150000 /* sleep time between locks, usec */
60 #define filesize 8192 /* size of file to lock */
62 const char *lockfile = "lockf_test";
77 fd = open (lockfile, O_RDWR, 0);
80 err(1, "%s", lockfile);
82 printf("%d: start\n", id);
84 for (i = 0; i < nlocks; i++) {
87 fl.l_start = random_uint32() % filesize;
88 fl.l_len = random_uint32() % filesize;
89 switch (random_uint32() % 3) {
100 fl.l_whence = SEEK_SET;
102 (void)fcntl(fd, F_SETLKW, &fl);
104 if (usleep(sleeptime) < 0)
105 #if defined(__FreeBSD__)
110 printf("%d: done\n", id);
115 ATF_TC_HEAD(randlock, tc)
118 atf_tc_set_md_var(tc, "timeout", "300");
119 atf_tc_set_md_var(tc, "descr", "Checks fcntl(2) locking");
122 ATF_TC_BODY(randlock, tc)
128 char pipe_in, pipe_out;
129 const char pipe_errmsg[] = "child: pipe write failed\n";
131 (void)unlink(lockfile);
133 fd = open (lockfile, O_RDWR|O_CREAT|O_EXCL|O_TRUNC, 0666);
134 ATF_REQUIRE_MSG(fd >= 0, "open(%s): %s", lockfile, strerror(errno));
136 ATF_REQUIRE_MSG(ftruncate(fd, filesize) >= 0,
137 "ftruncate(%s): %s", lockfile, strerror(errno));
139 ATF_REQUIRE_MSG(pipe(pipe_fd) == 0, "pipe: %s", strerror(errno));
144 pid = malloc(nprocs * sizeof(pid_t));
146 for (i = 0; i < nprocs; i++) {
147 pipe_out = (char)('A' + i);
151 if (write(pipe_fd[1], &pipe_out, 1) != 1)
152 write(STDERR_FILENO, pipe_errmsg,
153 __arraycount(pipe_errmsg) - 1);
159 atf_tc_fail("fork %d failed", i);
162 ATF_REQUIRE_MSG(read(pipe_fd[0], &pipe_in, 1) == 1,
163 "parent: read_pipe(%i): %s", i, strerror(errno));
164 ATF_REQUIRE_MSG(pipe_in == pipe_out,
165 "parent: pipe does not match");
169 for (j = 0; j < npasses; j++) {
170 printf("parent: run %i\n", j+1);
171 for (i = 0; i < nprocs; i++) {
172 ATF_REQUIRE_MSG(ptrace(PT_ATTACH, pid[i], 0, 0) >= 0,
173 "ptrace attach %d", pid[i]);
174 ATF_REQUIRE_MSG(waitpid(pid[i], &status, WUNTRACED) >= 0,
176 usleep(sleeptime / 3);
177 ATF_REQUIRE_MSG(ptrace(PT_DETACH, pid[i], (caddr_t)1,
179 "ptrace detach %d", pid[i]);
180 usleep(sleeptime / 3);
183 for (i = 0; i < nprocs; i++) {
184 printf("reap %d: ", i);
186 kill(pid[i], SIGINT);
187 waitpid(pid[i], &status, 0);
188 printf(" status %d\n", status);
194 dolock(int fd, int op, off_t lk_off, off_t lk_size)
199 result = lseek(fd, lk_off, SEEK_SET);
203 ATF_REQUIRE_MSG(result == lk_off, "lseek to wrong offset");
204 ret = lockf(fd, op, lk_size);
212 ATF_TC_HEAD(deadlock, tc)
215 atf_tc_set_md_var(tc, "timeout", "30");
216 atf_tc_set_md_var(tc, "descr", "Checks fcntl(2) deadlock detection");
219 ATF_TC_BODY(deadlock, tc)
226 (void)unlink(lockfile);
228 fd = open (lockfile, O_RDWR|O_CREAT|O_EXCL|O_TRUNC, 0666);
229 ATF_REQUIRE_MSG(fd >= 0, "open(%s): %s", lockfile, strerror(errno));
231 ATF_REQUIRE_MSG(ftruncate(fd, filesize) >= 0,
232 "ftruncate(%s): %s", lockfile, strerror(errno));
236 error = dolock(fd, F_LOCK, 0, 1);
237 ATF_REQUIRE_MSG(error == 0, "initial dolock: %s", strerror(errno));
240 ATF_REQUIRE_MSG(pid != -1, "fork failed: %s", strerror(errno));
242 error = dolock(fd, F_LOCK, 1, 1);
243 ATF_REQUIRE_MSG(error == 0, "child dolock: %s",
245 dolock(fd, F_LOCK, 0, 1); /* will block */
246 atf_tc_fail("child did not block");
248 sleep(1); /* give child time to grab its lock then block */
250 error = dolock(fd, F_LOCK, 1, 1);
251 ATF_REQUIRE_MSG(error == EDEADLK, "parent did not detect deadlock: %s",
253 ret = kill(pid, SIGKILL);
254 ATF_REQUIRE_MSG(ret != -1, "failed to kill child: %s", strerror(errno));
261 ATF_TP_ADD_TC(tp, randlock);
262 ATF_TP_ADD_TC(tp, deadlock);
264 return atf_no_error();