1 /* $NetBSD: lockf.c,v 1.6 2005/06/02 11:18:37 lukem 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.
30 * lockf regression test:
33 * 1) fork N child processes, do a bunch of random byte range lock/unlock.
36 #include <sys/types.h>
38 #include <sys/ptrace.h>
48 int nlocks
= 1000; /* number of locks per thread */
49 int nprocs
= 10; /* number of processes to spawn */
50 int sleeptime
= 500000; /* sleep time between locks, usec */
51 off_t size
= 8192; /* size of file to lock */
52 const char *lockfile
= "/tmp/lockf_test";
65 const char *which
= NULL
;
69 fd
= open (lockfile
, O_RDWR
, 0);
74 printf("%d: start\n", id
);
76 for (i
=0; i
<nlocks
; i
++) {
79 fl
.l_start
= random_uint32() % size
;
80 fl
.l_len
= random_uint32() % size
;
81 switch (random_uint32() % 3) {
95 fl
.l_whence
= SEEK_SET
;
97 printf("%d: try %slock %d to %d\n", id
, which
, (int)fl
.l_start
,
98 (int)(fl
.l_start
+ fl
.l_len
));
100 ret
= fcntl(fd
, F_SETLKW
, &fl
);
104 printf("%d: got %slock %d to %d\n", id
, which
, (int)fl
.l_start
,
105 ((int)(fl
.l_start
+ fl
.l_len
)));
107 if (usleep(sleeptime
) < 0)
110 printf("%d: done\n", id
);
116 main(int argc
, char **argv
)
123 (void)unlink(lockfile
);
125 fd
= open (lockfile
, O_RDWR
|O_CREAT
|O_EXCL
|O_TRUNC
, 0666);
127 err(1, "%s", lockfile
);
129 if (ftruncate(fd
, size
) < 0)
130 err(1, "ftruncate of %s failed", lockfile
);
135 pid
= malloc(nprocs
* sizeof(pid_t
));
137 for (i
=0; i
<nprocs
; i
++) {
145 err(1, "fork failed");
151 for (j
=0; j
<100; j
++) {
152 printf("parent: run %i\n", j
+1);
153 for (i
=0; i
<nprocs
; i
++) {
154 printf("stop %d\n", i
);
155 if (ptrace(PT_ATTACH
, pid
[i
], 0, 0) < 0)
156 err(1, "ptrace attach %d", pid
[i
]);
157 printf("wait %d\n", i
);
158 if (waitpid(pid
[i
], &status
, WUNTRACED
) < 0)
159 err(1, "waitpid(ptrace)");
160 printf("awake %d\n", i
);
162 if (ptrace(PT_DETACH
, pid
[i
], (caddr_t
)1, 0) < 0)
163 err(1, "ptrace detach %d", pid
[i
]);
164 printf("done %d\n", i
);
168 for (i
=0; i
<nprocs
; i
++) {
169 printf("reap %d: ", i
);
171 kill(pid
[i
], SIGINT
);
172 waitpid(pid
[i
], &status
, 0);
173 printf(" status %d\n", status
);