t_lockf.c revision 1.7 1 1.7 pgoyette /* $NetBSD: t_lockf.c,v 1.7 2013/02/19 22:44:27 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.5 pgoyette #define nlocks 500 /* number of locks per thread */
57 1.5 pgoyette #define nprocs 10 /* number of processes to spawn */
58 1.5 pgoyette #define npasses 50 /* number of passes to make over the children */
59 1.5 pgoyette #define sleeptime 150000 /* sleep time between locks, usec */
60 1.5 pgoyette #define filesize 8192 /* size of file to lock */
61 1.1 pgoyette
62 1.1 pgoyette const char *lockfile = "lockf_test";
63 1.1 pgoyette
64 1.1 pgoyette static u_int32_t
65 1.1 pgoyette random_uint32(void)
66 1.1 pgoyette {
67 1.1 pgoyette return lrand48();
68 1.1 pgoyette }
69 1.1 pgoyette
70 1.1 pgoyette static void
71 1.1 pgoyette trylocks(int id)
72 1.1 pgoyette {
73 1.1 pgoyette int i, ret, fd;
74 1.1 pgoyette
75 1.1 pgoyette srand48(getpid());
76 1.1 pgoyette
77 1.1 pgoyette fd = open (lockfile, O_RDWR, 0);
78 1.1 pgoyette
79 1.1 pgoyette if (fd < 0)
80 1.1 pgoyette err(1, "%s", lockfile);
81 1.1 pgoyette
82 1.1 pgoyette printf("%d: start\n", id);
83 1.1 pgoyette
84 1.5 pgoyette for (i = 0; i < nlocks; i++) {
85 1.1 pgoyette struct flock fl;
86 1.1 pgoyette
87 1.5 pgoyette fl.l_start = random_uint32() % filesize;
88 1.5 pgoyette fl.l_len = random_uint32() % filesize;
89 1.1 pgoyette switch (random_uint32() % 3) {
90 1.1 pgoyette case 0:
91 1.1 pgoyette fl.l_type = F_RDLCK;
92 1.1 pgoyette break;
93 1.1 pgoyette case 1:
94 1.1 pgoyette fl.l_type = F_WRLCK;
95 1.1 pgoyette break;
96 1.1 pgoyette case 2:
97 1.1 pgoyette fl.l_type = F_UNLCK;
98 1.1 pgoyette break;
99 1.1 pgoyette }
100 1.1 pgoyette fl.l_whence = SEEK_SET;
101 1.1 pgoyette
102 1.1 pgoyette ret = fcntl(fd, F_SETLKW, &fl);
103 1.1 pgoyette
104 1.1 pgoyette if (usleep(sleeptime) < 0)
105 1.1 pgoyette err(1, "usleep");
106 1.1 pgoyette }
107 1.1 pgoyette printf("%d: done\n", id);
108 1.1 pgoyette close (fd);
109 1.1 pgoyette }
110 1.1 pgoyette
111 1.1 pgoyette ATF_TC(randlock);
112 1.1 pgoyette ATF_TC_HEAD(randlock, tc)
113 1.1 pgoyette {
114 1.1 pgoyette
115 1.1 pgoyette atf_tc_set_md_var(tc, "timeout", "300");
116 1.1 pgoyette atf_tc_set_md_var(tc, "descr", "Checks fcntl(2) locking");
117 1.1 pgoyette }
118 1.1 pgoyette
119 1.1 pgoyette ATF_TC_BODY(randlock, tc)
120 1.1 pgoyette {
121 1.1 pgoyette int i, j, fd;
122 1.6 pgoyette int pipe_fd[2];
123 1.1 pgoyette pid_t *pid;
124 1.1 pgoyette int status;
125 1.6 pgoyette char pipe_in, pipe_out;
126 1.7 pgoyette const char *pipe_errmsg = "child: pipe write failed\n";
127 1.1 pgoyette
128 1.1 pgoyette (void)unlink(lockfile);
129 1.1 pgoyette
130 1.1 pgoyette fd = open (lockfile, O_RDWR|O_CREAT|O_EXCL|O_TRUNC, 0666);
131 1.1 pgoyette ATF_REQUIRE_MSG(fd >= 0, "open(%s): %s", lockfile, strerror(errno));
132 1.1 pgoyette
133 1.5 pgoyette ATF_REQUIRE_MSG(ftruncate(fd, filesize) >= 0,
134 1.1 pgoyette "ftruncate(%s): %s", lockfile, strerror(errno));
135 1.1 pgoyette
136 1.6 pgoyette ATF_REQUIRE_MSG(pipe(pipe_fd) == 0, "pipe: %s", strerror(errno));
137 1.6 pgoyette
138 1.1 pgoyette fsync(fd);
139 1.1 pgoyette close(fd);
140 1.1 pgoyette
141 1.1 pgoyette pid = malloc(nprocs * sizeof(pid_t));
142 1.1 pgoyette
143 1.5 pgoyette for (i = 0; i < nprocs; i++) {
144 1.6 pgoyette pipe_out = (char)('A' + i);
145 1.1 pgoyette pid[i] = fork();
146 1.1 pgoyette switch (pid[i]) {
147 1.1 pgoyette case 0:
148 1.6 pgoyette if (write(pipe_fd[1], &pipe_out, 1) != 1)
149 1.7 pgoyette write(STDERR_FILENO, pipe_errmsg,
150 1.7 pgoyette __arraycount(pipe_errmsg));
151 1.6 pgoyette else
152 1.6 pgoyette trylocks(i);
153 1.1 pgoyette _exit(0);
154 1.1 pgoyette break;
155 1.1 pgoyette case -1:
156 1.1 pgoyette atf_tc_fail("fork %d failed", i);
157 1.1 pgoyette break;
158 1.1 pgoyette default:
159 1.6 pgoyette ATF_REQUIRE_MSG(read(pipe_fd[0], &pipe_in, 1) == 1,
160 1.6 pgoyette "parent: read_pipe(%i): %s", i, strerror(errno));
161 1.6 pgoyette ATF_REQUIRE_MSG(pipe_in == pipe_out,
162 1.6 pgoyette "parent: pipe does not match");
163 1.1 pgoyette break;
164 1.1 pgoyette }
165 1.1 pgoyette }
166 1.4 pgoyette for (j = 0; j < npasses; j++) {
167 1.1 pgoyette printf("parent: run %i\n", j+1);
168 1.5 pgoyette for (i = 0; i < nprocs; i++) {
169 1.1 pgoyette ATF_REQUIRE_MSG(ptrace(PT_ATTACH, pid[i], 0, 0) >= 0,
170 1.1 pgoyette "ptrace attach %d", pid[i]);
171 1.1 pgoyette ATF_REQUIRE_MSG(waitpid(pid[i], &status, WUNTRACED) >= 0,
172 1.1 pgoyette "waitpid(ptrace)");
173 1.5 pgoyette usleep(sleeptime / 3);
174 1.1 pgoyette ATF_REQUIRE_MSG(ptrace(PT_DETACH, pid[i], (caddr_t)1,
175 1.1 pgoyette 0) >= 0,
176 1.1 pgoyette "ptrace detach %d", pid[i]);
177 1.5 pgoyette usleep(sleeptime / 3);
178 1.1 pgoyette }
179 1.1 pgoyette }
180 1.5 pgoyette for (i = 0; i < nprocs; i++) {
181 1.1 pgoyette printf("reap %d: ", i);
182 1.1 pgoyette fflush(stdout);
183 1.1 pgoyette kill(pid[i], SIGINT);
184 1.1 pgoyette waitpid(pid[i], &status, 0);
185 1.1 pgoyette printf(" status %d\n", status);
186 1.1 pgoyette }
187 1.1 pgoyette atf_tc_pass();
188 1.1 pgoyette }
189 1.1 pgoyette
190 1.1 pgoyette static int
191 1.1 pgoyette dolock(int fd, int op, off_t lk_off, off_t lk_size)
192 1.1 pgoyette {
193 1.1 pgoyette off_t result;
194 1.1 pgoyette int ret;
195 1.1 pgoyette
196 1.1 pgoyette result = lseek(fd, lk_off, SEEK_SET);
197 1.1 pgoyette if (result == -1) {
198 1.1 pgoyette return errno;
199 1.1 pgoyette }
200 1.1 pgoyette ATF_REQUIRE_MSG(result == lk_off, "lseek to wrong offset");
201 1.1 pgoyette ret = lockf(fd, op, lk_size);
202 1.1 pgoyette if (ret == -1) {
203 1.1 pgoyette return errno;
204 1.1 pgoyette }
205 1.1 pgoyette return 0;
206 1.1 pgoyette }
207 1.1 pgoyette
208 1.1 pgoyette ATF_TC(deadlock);
209 1.1 pgoyette ATF_TC_HEAD(deadlock, tc)
210 1.1 pgoyette {
211 1.1 pgoyette
212 1.1 pgoyette atf_tc_set_md_var(tc, "timeout", "30");
213 1.1 pgoyette atf_tc_set_md_var(tc, "descr", "Checks fcntl(2) deadlock detection");
214 1.1 pgoyette }
215 1.1 pgoyette
216 1.1 pgoyette ATF_TC_BODY(deadlock, tc)
217 1.1 pgoyette {
218 1.1 pgoyette int fd;
219 1.1 pgoyette int error;
220 1.1 pgoyette int ret;
221 1.1 pgoyette pid_t pid;
222 1.1 pgoyette
223 1.1 pgoyette (void)unlink(lockfile);
224 1.1 pgoyette
225 1.1 pgoyette fd = open (lockfile, O_RDWR|O_CREAT|O_EXCL|O_TRUNC, 0666);
226 1.1 pgoyette ATF_REQUIRE_MSG(fd >= 0, "open(%s): %s", lockfile, strerror(errno));
227 1.1 pgoyette
228 1.5 pgoyette ATF_REQUIRE_MSG(ftruncate(fd, filesize) >= 0,
229 1.1 pgoyette "ftruncate(%s): %s", lockfile, strerror(errno));
230 1.1 pgoyette
231 1.1 pgoyette fsync(fd);
232 1.1 pgoyette
233 1.1 pgoyette error = dolock(fd, F_LOCK, 0, 1);
234 1.1 pgoyette ATF_REQUIRE_MSG(error == 0, "initial dolock: %s", strerror(errno));
235 1.1 pgoyette
236 1.1 pgoyette pid = fork();
237 1.1 pgoyette ATF_REQUIRE_MSG(pid != -1, "fork failed: %s", strerror(errno));
238 1.1 pgoyette if (pid == 0) {
239 1.1 pgoyette error = dolock(fd, F_LOCK, 1, 1);
240 1.1 pgoyette ATF_REQUIRE_MSG(error == 0, "child dolock: %s",
241 1.1 pgoyette strerror(errno));
242 1.1 pgoyette dolock(fd, F_LOCK, 0, 1); /* will block */
243 1.1 pgoyette atf_tc_fail("child did not block");
244 1.1 pgoyette }
245 1.1 pgoyette sleep(1); /* give child time to grab its lock then block */
246 1.1 pgoyette
247 1.1 pgoyette error = dolock(fd, F_LOCK, 1, 1);
248 1.2 pgoyette ATF_REQUIRE_MSG(error == EDEADLK, "parent did not detect deadlock: %s",
249 1.1 pgoyette strerror(errno));
250 1.1 pgoyette ret = kill(pid, SIGKILL);
251 1.1 pgoyette ATF_REQUIRE_MSG(ret != -1, "failed to kill child: %s", strerror(errno));
252 1.1 pgoyette
253 1.1 pgoyette atf_tc_pass();
254 1.1 pgoyette }
255 1.1 pgoyette
256 1.1 pgoyette ATF_TP_ADD_TCS(tp)
257 1.1 pgoyette {
258 1.1 pgoyette ATF_TP_ADD_TC(tp, randlock);
259 1.1 pgoyette ATF_TP_ADD_TC(tp, deadlock);
260 1.1 pgoyette
261 1.1 pgoyette return atf_no_error();
262 1.1 pgoyette }
263