xref: /freebsd/tools/test/stress2/testcases/lockf/lockf.c (revision f81cdf24ba5436367377f7c8e8f51f6df2a75ca7)
1 /*-
2  * Copyright (c) 2008 Peter Holm <pho@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  */
27 
28 /* Test lockf(3) */
29 
30 #include <sys/param.h>
31 #include <sys/wait.h>
32 
33 #include <err.h>
34 #include <errno.h>
35 #include <fcntl.h>
36 #include <signal.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <unistd.h>
40 
41 #include "stress.h"
42 
43 static pid_t pid;
44 static int fd;
45 static int freespace;
46 static char file[128];
47 
48 static int
49 get(void) {
50 	int r, sem;
51 
52 	do {
53 		r = lockf(fd, F_LOCK, 0);
54 	} while (r == -1 && errno == EINTR && done_testing == 0);
55 	if (r == -1)
56 		err(1, "lockf(%s, F_LOCK)", file);
57 	if (lseek(fd, 0, SEEK_SET) == -1) // XXX
58 		err(1, "lseek"); // XXX
59 	r = read(fd, &sem, sizeof(sem));
60 	if (r == -1)
61 		err(1, "get: read(%d)", fd);
62 	if (r == 0)
63 		errx(1, "get() read 0 bytes");
64 	if (r != sizeof(sem))
65 		errx(1, "get() size error: %d", r);
66 	if (lseek(fd, 0, SEEK_SET) == -1)
67 		err(1, "lseek");
68 	if (lockf(fd, F_ULOCK, 0) == -1)
69 		err(1, "lockf(%s, F_ULOCK)", file);
70 	return (sem);
71 }
72 
73 static void
74 incr(void) {
75 	int r, sem;
76 
77 	do {
78 		r = lockf(fd, F_LOCK, 0);
79 	} while (r == -1 && errno == EINTR && done_testing == 0);
80 	if (r == -1)
81 		err(1, "lockf(%s, F_LOCK)", file);
82 	if (read(fd, &sem, sizeof(sem)) != sizeof(sem))
83 		err(1, "incr: read(%d)", fd);
84 	if (lseek(fd, 0, SEEK_SET) == -1)
85 		err(1, "lseek");
86 	sem++;
87 	if (write(fd, &sem, sizeof(sem)) != sizeof(sem))
88 		err(1, "incr: read");
89 	if (lseek(fd, 0, SEEK_SET) == -1)
90 		err(1, "lseek");
91 	if (lockf(fd, F_ULOCK, 0) == -1)
92 		err(1, "lockf(%s, F_ULOCK)", file);
93 }
94 
95 int
96 setup(int nb)
97 {
98 	int64_t bl;
99 	int64_t in;
100 	int64_t reserve_bl;
101 	int64_t reserve_in;
102 
103 	if (nb == 0) {
104 		getdf(&bl, &in);
105 
106 		/* Resource requirements: */
107 		reserve_in =    1 * op->incarnations;
108 		reserve_bl = 4096 * op->incarnations;
109 		freespace = (reserve_bl <= bl && reserve_in <= in);
110 		if (!freespace)
111 			reserve_bl = reserve_in = 0;
112 
113 		if (op->verbose > 1)
114 			printf("lockf(incarnations=%d). Free(%jdk, %jd), reserve(%jdk, %jd)\n",
115 			    op->incarnations, bl/1024, in, reserve_bl/1024, reserve_in);
116 		reservedf(reserve_bl, reserve_in);
117 		putval(freespace);
118 	} else {
119 		freespace = getval();
120 	}
121 	if (!freespace)
122 		exit(0);
123 
124         return (0);
125 }
126 
127 void
128 cleanup(void)
129 {
130 }
131 
132 int
133 test(void)
134 {
135 	int i;
136 	int sem = 0;
137 
138 	sprintf(file, "lockf.0.%d", getpid());
139 	if ((fd = open(file,O_CREAT | O_TRUNC | O_RDWR, 0600)) == -1) {
140 		if (errno == ENOENT)
141 			return (0);
142 		else
143 			err(1, "creat(%s) %s:%d", file, __FILE__, __LINE__);
144 	}
145 	if (write(fd, &sem, sizeof(sem)) != sizeof(sem))
146 		err(1, "write");
147 	if (lseek(fd, 0, SEEK_SET) == -1)
148 		err(1, "lseek");
149 
150 	pid = fork();
151 	if (pid == -1) {
152 		perror("fork");
153 		exit(2);
154 	}
155 
156 	if (pid == 0) {	/* child */
157 		alarm(60);
158 		for (i = 0; i < 100 && done_testing == 0; i++) {
159 			while ((get() & 1) == 0 && done_testing == 0)
160 				;
161 			if (op->verbose > 3)
162 				printf("Child  %d, sem = %d\n", i, get()),
163 					fflush(stdout);
164 			incr();
165 		}
166 		_exit(0);
167 	} else {	/* parent */
168 		for (i = 0; i < 100 && done_testing == 0; i++) {
169 			while ((get() & 1) == 1 && done_testing == 0)
170 				;
171 			if (op->verbose > 3)
172 				printf("Parent %d, sem = %d\n", i, get()),
173 					fflush(stdout);
174 			incr();
175 		}
176 	}
177 	close(fd);
178 	if (done_testing == 1)
179 		kill(pid, SIGHUP);
180 	waitpid(pid, &i, 0);
181 	unlink(file);
182 
183         return (0);
184 }
185