Sync usage with man page.
[netbsd-mini2440.git] / regress / sys / kern / lockf / lockf.c
blob34c467defd5495cbd38ed8e45be3772921d55a64
1 /* $NetBSD: lockf.c,v 1.6 2005/06/02 11:18:37 lukem Exp $ */
3 /*-
4 * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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:
32 * Tests:
33 * 1) fork N child processes, do a bunch of random byte range lock/unlock.
36 #include <sys/types.h>
37 #include <sys/wait.h>
38 #include <sys/ptrace.h>
40 #include <unistd.h>
41 #include <fcntl.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <err.h>
45 #include <signal.h>
46 #include <errno.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";
54 static u_int32_t
55 random_uint32(void)
57 return lrand48();
61 static void
62 trylocks(int id)
64 int i, ret, fd;
65 const char *which = NULL;
67 srand48(getpid());
69 fd = open (lockfile, O_RDWR, 0);
71 if (fd < 0)
72 err(1, lockfile);
74 printf("%d: start\n", id);
76 for (i=0; i<nlocks; i++) {
77 struct flock fl;
79 fl.l_start = random_uint32() % size;
80 fl.l_len = random_uint32() % size;
81 switch (random_uint32() % 3) {
82 case 0:
83 which = "read";
84 fl.l_type = F_RDLCK;
85 break;
86 case 1:
87 which = "write";
88 fl.l_type = F_WRLCK;
89 break;
90 case 2:
91 which = "un";
92 fl.l_type = F_UNLCK;
93 break;
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);
102 if (ret < 0)
103 perror("fcntl");
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)
108 err(1, "usleep");
110 printf("%d: done\n", id);
111 close (fd);
114 /* ARGSUSED */
116 main(int argc, char **argv)
118 int i, j;
119 pid_t *pid;
120 int status;
121 int fd;
123 (void)unlink(lockfile);
125 fd = open (lockfile, O_RDWR|O_CREAT|O_EXCL|O_TRUNC, 0666);
126 if (fd < 0)
127 err(1, "%s", lockfile);
129 if (ftruncate(fd, size) < 0)
130 err(1, "ftruncate of %s failed", lockfile);
132 fsync(fd);
133 close(fd);
135 pid = malloc(nprocs * sizeof(pid_t));
137 for (i=0; i<nprocs; i++) {
138 pid[i] = fork();
139 switch (pid[i]) {
140 case 0:
141 trylocks(i);
142 _exit(0);
143 break;
144 case -1:
145 err(1, "fork failed");
146 break;
147 default:
148 break;
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);
161 usleep(sleeptime/3);
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);
165 usleep(sleeptime/3);
168 for (i=0; i<nprocs; i++) {
169 printf("reap %d: ", i);
170 fflush(stdout);
171 kill(pid[i], SIGINT);
172 waitpid(pid[i], &status, 0);
173 printf(" status %d\n", status);
175 exit(0);
176 /* NOTREACHED */