t_lockf.c revision 1.3 1 1.3 pgoyette /* $NetBSD: t_lockf.c,v 1.3 2013/02/19 00:54:47 pgoyette Exp $ */
2 1.1 pgoyette
3 1.1 pgoyette /*-
4 1.1 pgoyette * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 1.1 pgoyette * All rights reserved.
6 1.1 pgoyette *
7 1.1 pgoyette * Redistribution and use in source and binary forms, with or without
8 1.1 pgoyette * modification, are permitted provided that the following conditions
9 1.1 pgoyette * are met:
10 1.1 pgoyette * 1. Redistributions of source code must retain the above copyright
11 1.1 pgoyette * notice, this list of conditions and the following disclaimer.
12 1.1 pgoyette * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 pgoyette * notice, this list of conditions and the following disclaimer in the
14 1.1 pgoyette * documentation and/or other materials provided with the distribution.
15 1.1 pgoyette *
16 1.1 pgoyette * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 pgoyette * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 pgoyette * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 pgoyette * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 pgoyette * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 pgoyette * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 pgoyette * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 pgoyette * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 pgoyette * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 pgoyette * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 pgoyette * POSSIBILITY OF SUCH DAMAGE.
27 1.1 pgoyette */
28 1.1 pgoyette
29 1.1 pgoyette #include <atf-c.h>
30 1.1 pgoyette #include <err.h>
31 1.1 pgoyette #include <errno.h>
32 1.1 pgoyette #include <fcntl.h>
33 1.1 pgoyette #include <signal.h>
34 1.1 pgoyette #include <stdio.h>
35 1.1 pgoyette #include <stdlib.h>
36 1.1 pgoyette #include <string.h>
37 1.1 pgoyette #include <unistd.h>
38 1.1 pgoyette
39 1.1 pgoyette #include <sys/types.h>
40 1.1 pgoyette #include <sys/wait.h>
41 1.1 pgoyette #include <sys/ptrace.h>
42 1.1 pgoyette
43 1.1 pgoyette /*
44 1.1 pgoyette * lockf1 regression test:
45 1.1 pgoyette *
46 1.1 pgoyette * Tests:
47 1.1 pgoyette * Fork N child processes, each of which gets M random byte range locks
48 1.1 pgoyette * on a common file. We ignore all lock errors (practically speaking,
49 1.1 pgoyette * this means EDEADLK or ENOLOCK), but we make numerous passes over all
50 1.1 pgoyette * the children to make sure that they are still awake. (We do this by
51 1.1 pgoyette * verifying that we can ptrace(ATTACH/DETACH) to the children and get
52 1.1 pgoyette * their status via waitpid().)
53 1.1 pgoyette * When finished, reap all the children.
54 1.1 pgoyette */
55 1.1 pgoyette
56 1.1 pgoyette int nlocks = 500; /* number of locks per thread */
57 1.1 pgoyette int nprocs = 10; /* number of processes to spawn */
58 1.1 pgoyette int sleeptime = 150000; /* sleep time between locks, usec */
59 1.1 pgoyette off_t size = 8192; /* size of file to lock */
60 1.1 pgoyette
61 1.1 pgoyette const char *lockfile = "lockf_test";
62 1.1 pgoyette
63 1.1 pgoyette static u_int32_t
64 1.1 pgoyette random_uint32(void)
65 1.1 pgoyette {
66 1.1 pgoyette return lrand48();
67 1.1 pgoyette }
68 1.1 pgoyette
69 1.1 pgoyette static void
70 1.1 pgoyette trylocks(int id)
71 1.1 pgoyette {
72 1.1 pgoyette int i, ret, fd;
73 1.1 pgoyette
74 1.1 pgoyette srand48(getpid());
75 1.1 pgoyette
76 1.1 pgoyette fd = open (lockfile, O_RDWR, 0);
77 1.1 pgoyette
78 1.1 pgoyette if (fd < 0)
79 1.1 pgoyette err(1, "%s", lockfile);
80 1.1 pgoyette
81 1.1 pgoyette printf("%d: start\n", id);
82 1.1 pgoyette
83 1.1 pgoyette for (i=0; i<nlocks; i++) {
84 1.1 pgoyette struct flock fl;
85 1.1 pgoyette
86 1.1 pgoyette fl.l_start = random_uint32() % size;
87 1.1 pgoyette fl.l_len = random_uint32() % size;
88 1.1 pgoyette switch (random_uint32() % 3) {
89 1.1 pgoyette case 0:
90 1.1 pgoyette fl.l_type = F_RDLCK;
91 1.1 pgoyette break;
92 1.1 pgoyette case 1:
93 1.1 pgoyette fl.l_type = F_WRLCK;
94 1.1 pgoyette break;
95 1.1 pgoyette case 2:
96 1.1 pgoyette fl.l_type = F_UNLCK;
97 1.1 pgoyette break;
98 1.1 pgoyette }
99 1.1 pgoyette fl.l_whence = SEEK_SET;
100 1.1 pgoyette
101 1.1 pgoyette ret = fcntl(fd, F_SETLKW, &fl);
102 1.1 pgoyette
103 1.1 pgoyette if (usleep(sleeptime) < 0)
104 1.1 pgoyette err(1, "usleep");
105 1.1 pgoyette }
106 1.1 pgoyette printf("%d: done\n", id);
107 1.1 pgoyette close (fd);
108 1.1 pgoyette }
109 1.1 pgoyette
110 1.1 pgoyette ATF_TC(randlock);
111 1.1 pgoyette ATF_TC_HEAD(randlock, tc)
112 1.1 pgoyette {
113 1.1 pgoyette
114 1.1 pgoyette atf_tc_set_md_var(tc, "timeout", "300");
115 1.1 pgoyette atf_tc_set_md_var(tc, "descr", "Checks fcntl(2) locking");
116 1.1 pgoyette }
117 1.1 pgoyette
118 1.1 pgoyette ATF_TC_BODY(randlock, tc)
119 1.1 pgoyette {
120 1.1 pgoyette int i, j, fd;
121 1.1 pgoyette pid_t *pid;
122 1.1 pgoyette int status;
123 1.1 pgoyette
124 1.1 pgoyette (void)unlink(lockfile);
125 1.1 pgoyette
126 1.1 pgoyette fd = open (lockfile, O_RDWR|O_CREAT|O_EXCL|O_TRUNC, 0666);
127 1.1 pgoyette ATF_REQUIRE_MSG(fd >= 0, "open(%s): %s", lockfile, strerror(errno));
128 1.1 pgoyette
129 1.1 pgoyette ATF_REQUIRE_MSG(ftruncate(fd, size) >= 0,
130 1.1 pgoyette "ftruncate(%s): %s", lockfile, strerror(errno));
131 1.1 pgoyette
132 1.1 pgoyette fsync(fd);
133 1.1 pgoyette close(fd);
134 1.1 pgoyette
135 1.1 pgoyette pid = malloc(nprocs * sizeof(pid_t));
136 1.1 pgoyette
137 1.1 pgoyette for (i=0; i<nprocs; i++) {
138 1.1 pgoyette pid[i] = fork();
139 1.1 pgoyette switch (pid[i]) {
140 1.1 pgoyette case 0:
141 1.1 pgoyette trylocks(i);
142 1.1 pgoyette _exit(0);
143 1.1 pgoyette break;
144 1.1 pgoyette case -1:
145 1.1 pgoyette atf_tc_fail("fork %d failed", i);
146 1.1 pgoyette break;
147 1.1 pgoyette default:
148 1.1 pgoyette break;
149 1.1 pgoyette }
150 1.1 pgoyette }
151 1.3 pgoyette usleep(sleeptime/10);
152 1.1 pgoyette for (j=0; j<100; j++) {
153 1.1 pgoyette printf("parent: run %i\n", j+1);
154 1.1 pgoyette for (i=0; i<nprocs; i++) {
155 1.1 pgoyette ATF_REQUIRE_MSG(ptrace(PT_ATTACH, pid[i], 0, 0) >= 0,
156 1.1 pgoyette "ptrace attach %d", pid[i]);
157 1.1 pgoyette ATF_REQUIRE_MSG(waitpid(pid[i], &status, WUNTRACED) >= 0,
158 1.1 pgoyette "waitpid(ptrace)");
159 1.1 pgoyette usleep(sleeptime/3);
160 1.1 pgoyette ATF_REQUIRE_MSG(ptrace(PT_DETACH, pid[i], (caddr_t)1,
161 1.1 pgoyette 0) >= 0,
162 1.1 pgoyette "ptrace detach %d", pid[i]);
163 1.1 pgoyette usleep(sleeptime/3);
164 1.1 pgoyette }
165 1.1 pgoyette }
166 1.1 pgoyette for (i=0; i<nprocs; i++) {
167 1.1 pgoyette printf("reap %d: ", i);
168 1.1 pgoyette fflush(stdout);
169 1.1 pgoyette kill(pid[i], SIGINT);
170 1.1 pgoyette waitpid(pid[i], &status, 0);
171 1.1 pgoyette printf(" status %d\n", status);
172 1.1 pgoyette }
173 1.1 pgoyette atf_tc_pass();
174 1.1 pgoyette }
175 1.1 pgoyette
176 1.1 pgoyette static int
177 1.1 pgoyette dolock(int fd, int op, off_t lk_off, off_t lk_size)
178 1.1 pgoyette {
179 1.1 pgoyette off_t result;
180 1.1 pgoyette int ret;
181 1.1 pgoyette
182 1.1 pgoyette result = lseek(fd, lk_off, SEEK_SET);
183 1.1 pgoyette if (result == -1) {
184 1.1 pgoyette return errno;
185 1.1 pgoyette }
186 1.1 pgoyette ATF_REQUIRE_MSG(result == lk_off, "lseek to wrong offset");
187 1.1 pgoyette ret = lockf(fd, op, lk_size);
188 1.1 pgoyette if (ret == -1) {
189 1.1 pgoyette return errno;
190 1.1 pgoyette }
191 1.1 pgoyette return 0;
192 1.1 pgoyette }
193 1.1 pgoyette
194 1.1 pgoyette ATF_TC(deadlock);
195 1.1 pgoyette ATF_TC_HEAD(deadlock, tc)
196 1.1 pgoyette {
197 1.1 pgoyette
198 1.1 pgoyette atf_tc_set_md_var(tc, "timeout", "30");
199 1.1 pgoyette atf_tc_set_md_var(tc, "descr", "Checks fcntl(2) deadlock detection");
200 1.1 pgoyette }
201 1.1 pgoyette
202 1.1 pgoyette ATF_TC_BODY(deadlock, tc)
203 1.1 pgoyette {
204 1.1 pgoyette int fd;
205 1.1 pgoyette int error;
206 1.1 pgoyette int ret;
207 1.1 pgoyette pid_t pid;
208 1.1 pgoyette
209 1.1 pgoyette (void)unlink(lockfile);
210 1.1 pgoyette
211 1.1 pgoyette fd = open (lockfile, O_RDWR|O_CREAT|O_EXCL|O_TRUNC, 0666);
212 1.1 pgoyette ATF_REQUIRE_MSG(fd >= 0, "open(%s): %s", lockfile, strerror(errno));
213 1.1 pgoyette
214 1.1 pgoyette ATF_REQUIRE_MSG(ftruncate(fd, size) >= 0,
215 1.1 pgoyette "ftruncate(%s): %s", lockfile, strerror(errno));
216 1.1 pgoyette
217 1.1 pgoyette fsync(fd);
218 1.1 pgoyette
219 1.1 pgoyette error = dolock(fd, F_LOCK, 0, 1);
220 1.1 pgoyette ATF_REQUIRE_MSG(error == 0, "initial dolock: %s", strerror(errno));
221 1.1 pgoyette
222 1.1 pgoyette pid = fork();
223 1.1 pgoyette ATF_REQUIRE_MSG(pid != -1, "fork failed: %s", strerror(errno));
224 1.1 pgoyette if (pid == 0) {
225 1.1 pgoyette error = dolock(fd, F_LOCK, 1, 1);
226 1.1 pgoyette ATF_REQUIRE_MSG(error == 0, "child dolock: %s",
227 1.1 pgoyette strerror(errno));
228 1.1 pgoyette dolock(fd, F_LOCK, 0, 1); /* will block */
229 1.1 pgoyette atf_tc_fail("child did not block");
230 1.1 pgoyette }
231 1.1 pgoyette sleep(1); /* give child time to grab its lock then block */
232 1.1 pgoyette
233 1.1 pgoyette error = dolock(fd, F_LOCK, 1, 1);
234 1.2 pgoyette ATF_REQUIRE_MSG(error == EDEADLK, "parent did not detect deadlock: %s",
235 1.1 pgoyette strerror(errno));
236 1.1 pgoyette ret = kill(pid, SIGKILL);
237 1.1 pgoyette ATF_REQUIRE_MSG(ret != -1, "failed to kill child: %s", strerror(errno));
238 1.1 pgoyette
239 1.1 pgoyette atf_tc_pass();
240 1.1 pgoyette }
241 1.1 pgoyette
242 1.1 pgoyette ATF_TP_ADD_TCS(tp)
243 1.1 pgoyette {
244 1.1 pgoyette ATF_TP_ADD_TC(tp, randlock);
245 1.1 pgoyette ATF_TP_ADD_TC(tp, deadlock);
246 1.1 pgoyette
247 1.1 pgoyette return atf_no_error();
248 1.1 pgoyette }
249