t_lockf.c revision 1.5 1 1.5 pgoyette /* $NetBSD: t_lockf.c,v 1.5 2013/02/19 04:46:46 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.1 pgoyette pid_t *pid;
123 1.1 pgoyette int status;
124 1.1 pgoyette
125 1.1 pgoyette (void)unlink(lockfile);
126 1.1 pgoyette
127 1.1 pgoyette fd = open (lockfile, O_RDWR|O_CREAT|O_EXCL|O_TRUNC, 0666);
128 1.1 pgoyette ATF_REQUIRE_MSG(fd >= 0, "open(%s): %s", lockfile, strerror(errno));
129 1.1 pgoyette
130 1.5 pgoyette ATF_REQUIRE_MSG(ftruncate(fd, filesize) >= 0,
131 1.1 pgoyette "ftruncate(%s): %s", lockfile, strerror(errno));
132 1.1 pgoyette
133 1.1 pgoyette fsync(fd);
134 1.1 pgoyette close(fd);
135 1.1 pgoyette
136 1.1 pgoyette pid = malloc(nprocs * sizeof(pid_t));
137 1.1 pgoyette
138 1.5 pgoyette for (i = 0; i < nprocs; i++) {
139 1.1 pgoyette pid[i] = fork();
140 1.1 pgoyette switch (pid[i]) {
141 1.1 pgoyette case 0:
142 1.1 pgoyette trylocks(i);
143 1.1 pgoyette _exit(0);
144 1.1 pgoyette break;
145 1.1 pgoyette case -1:
146 1.1 pgoyette atf_tc_fail("fork %d failed", i);
147 1.1 pgoyette break;
148 1.1 pgoyette default:
149 1.1 pgoyette break;
150 1.1 pgoyette }
151 1.1 pgoyette }
152 1.3 pgoyette usleep(sleeptime/10);
153 1.4 pgoyette for (j = 0; j < npasses; j++) {
154 1.1 pgoyette printf("parent: run %i\n", j+1);
155 1.5 pgoyette for (i = 0; i < nprocs; i++) {
156 1.1 pgoyette ATF_REQUIRE_MSG(ptrace(PT_ATTACH, pid[i], 0, 0) >= 0,
157 1.1 pgoyette "ptrace attach %d", pid[i]);
158 1.1 pgoyette ATF_REQUIRE_MSG(waitpid(pid[i], &status, WUNTRACED) >= 0,
159 1.1 pgoyette "waitpid(ptrace)");
160 1.5 pgoyette usleep(sleeptime / 3);
161 1.1 pgoyette ATF_REQUIRE_MSG(ptrace(PT_DETACH, pid[i], (caddr_t)1,
162 1.1 pgoyette 0) >= 0,
163 1.1 pgoyette "ptrace detach %d", pid[i]);
164 1.5 pgoyette usleep(sleeptime / 3);
165 1.1 pgoyette }
166 1.1 pgoyette }
167 1.5 pgoyette for (i = 0; i < nprocs; i++) {
168 1.1 pgoyette printf("reap %d: ", i);
169 1.1 pgoyette fflush(stdout);
170 1.1 pgoyette kill(pid[i], SIGINT);
171 1.1 pgoyette waitpid(pid[i], &status, 0);
172 1.1 pgoyette printf(" status %d\n", status);
173 1.1 pgoyette }
174 1.1 pgoyette atf_tc_pass();
175 1.1 pgoyette }
176 1.1 pgoyette
177 1.1 pgoyette static int
178 1.1 pgoyette dolock(int fd, int op, off_t lk_off, off_t lk_size)
179 1.1 pgoyette {
180 1.1 pgoyette off_t result;
181 1.1 pgoyette int ret;
182 1.1 pgoyette
183 1.1 pgoyette result = lseek(fd, lk_off, SEEK_SET);
184 1.1 pgoyette if (result == -1) {
185 1.1 pgoyette return errno;
186 1.1 pgoyette }
187 1.1 pgoyette ATF_REQUIRE_MSG(result == lk_off, "lseek to wrong offset");
188 1.1 pgoyette ret = lockf(fd, op, lk_size);
189 1.1 pgoyette if (ret == -1) {
190 1.1 pgoyette return errno;
191 1.1 pgoyette }
192 1.1 pgoyette return 0;
193 1.1 pgoyette }
194 1.1 pgoyette
195 1.1 pgoyette ATF_TC(deadlock);
196 1.1 pgoyette ATF_TC_HEAD(deadlock, tc)
197 1.1 pgoyette {
198 1.1 pgoyette
199 1.1 pgoyette atf_tc_set_md_var(tc, "timeout", "30");
200 1.1 pgoyette atf_tc_set_md_var(tc, "descr", "Checks fcntl(2) deadlock detection");
201 1.1 pgoyette }
202 1.1 pgoyette
203 1.1 pgoyette ATF_TC_BODY(deadlock, tc)
204 1.1 pgoyette {
205 1.1 pgoyette int fd;
206 1.1 pgoyette int error;
207 1.1 pgoyette int ret;
208 1.1 pgoyette pid_t pid;
209 1.1 pgoyette
210 1.1 pgoyette (void)unlink(lockfile);
211 1.1 pgoyette
212 1.1 pgoyette fd = open (lockfile, O_RDWR|O_CREAT|O_EXCL|O_TRUNC, 0666);
213 1.1 pgoyette ATF_REQUIRE_MSG(fd >= 0, "open(%s): %s", lockfile, strerror(errno));
214 1.1 pgoyette
215 1.5 pgoyette ATF_REQUIRE_MSG(ftruncate(fd, filesize) >= 0,
216 1.1 pgoyette "ftruncate(%s): %s", lockfile, strerror(errno));
217 1.1 pgoyette
218 1.1 pgoyette fsync(fd);
219 1.1 pgoyette
220 1.1 pgoyette error = dolock(fd, F_LOCK, 0, 1);
221 1.1 pgoyette ATF_REQUIRE_MSG(error == 0, "initial dolock: %s", strerror(errno));
222 1.1 pgoyette
223 1.1 pgoyette pid = fork();
224 1.1 pgoyette ATF_REQUIRE_MSG(pid != -1, "fork failed: %s", strerror(errno));
225 1.1 pgoyette if (pid == 0) {
226 1.1 pgoyette error = dolock(fd, F_LOCK, 1, 1);
227 1.1 pgoyette ATF_REQUIRE_MSG(error == 0, "child dolock: %s",
228 1.1 pgoyette strerror(errno));
229 1.1 pgoyette dolock(fd, F_LOCK, 0, 1); /* will block */
230 1.1 pgoyette atf_tc_fail("child did not block");
231 1.1 pgoyette }
232 1.1 pgoyette sleep(1); /* give child time to grab its lock then block */
233 1.1 pgoyette
234 1.1 pgoyette error = dolock(fd, F_LOCK, 1, 1);
235 1.2 pgoyette ATF_REQUIRE_MSG(error == EDEADLK, "parent did not detect deadlock: %s",
236 1.1 pgoyette strerror(errno));
237 1.1 pgoyette ret = kill(pid, SIGKILL);
238 1.1 pgoyette ATF_REQUIRE_MSG(ret != -1, "failed to kill child: %s", strerror(errno));
239 1.1 pgoyette
240 1.1 pgoyette atf_tc_pass();
241 1.1 pgoyette }
242 1.1 pgoyette
243 1.1 pgoyette ATF_TP_ADD_TCS(tp)
244 1.1 pgoyette {
245 1.1 pgoyette ATF_TP_ADD_TC(tp, randlock);
246 1.1 pgoyette ATF_TP_ADD_TC(tp, deadlock);
247 1.1 pgoyette
248 1.1 pgoyette return atf_no_error();
249 1.1 pgoyette }
250