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 #include <sys/param.h>
29 #include <sys/stat.h>
30 #include <sys/wait.h>
31
32 #include <err.h>
33 #include <fcntl.h>
34 #include <signal.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39
40 #include "stress.h"
41
42 static char path[MAXPATHLEN+1];
43 static int bufsize, freespace;
44
45 static void
reader(void)46 reader(void) {
47 fd_set set;
48 struct timeval tv;
49 int *buf, fd, n;
50
51 setproctitle("reader");
52 if ((fd = open(path, O_RDONLY | O_NONBLOCK)) < 0)
53 err(1, "open(%s)", path);
54 if ((buf = malloc(bufsize)) == NULL)
55 err(1, "malloc(%d), %s:%d", bufsize, __FILE__, __LINE__);
56 n = 0;
57 FD_ZERO(&set);
58 FD_SET(fd, &set);
59 tv.tv_sec = 10;
60 tv.tv_usec = 0;
61 if (select(fd + 1, &set, NULL, NULL, &tv) == 1) {
62 if ((n = read(fd, buf, bufsize)) < 0)
63 err(1, "read(), %s:%d", __FILE__, __LINE__);
64 }
65 close(fd);
66 free(buf);
67 }
68
69 static void
writer(void)70 writer(void) {
71 int *buf, fd;
72
73 setproctitle("writer");
74 if ((fd = open(path, O_WRONLY)) < 0) {
75 unlink(path);
76 err(1, "open(%s)", path);
77 }
78 if ((buf = malloc(bufsize)) == NULL)
79 err(1, "malloc(%d), %s:%d", bufsize, __FILE__, __LINE__);
80 memset(buf, 0, bufsize);
81
82 if (write(fd, buf, bufsize) < 0)
83 err(1, "write(%d), %s:%d", fd, __FILE__, __LINE__);
84 close(fd);
85 free(buf);
86 }
87
88 int
setup(int nb)89 setup(int nb)
90 {
91 int64_t bl;
92 int64_t in;
93 int64_t reserve_bl;
94 int64_t reserve_in;
95
96 if (nb == 0) {
97 getdf(&bl, &in);
98
99 /* Resource requirements: */
100 reserve_in = 200 * op->incarnations;
101 reserve_bl = 2048 * op->incarnations;
102 freespace = (reserve_bl <= bl && reserve_in <= in);
103 if (!freespace)
104 reserve_bl = reserve_in = 0;
105
106 if (op->verbose > 1)
107 printf("mkfifo(incarnations=%d). Free(%jdk, %jd), reserve(%jdk, %jd)\n",
108 op->incarnations, bl/1024, in, reserve_bl/1024, reserve_in);
109 reservedf(reserve_bl, reserve_in);
110 putval(freespace);
111 fflush(stdout);
112 } else {
113 freespace = getval();
114 }
115 if (!freespace)
116 _exit(0);
117 bufsize = 2 << random_int(2, 12);
118
119 return (0);
120 }
121
122 void
cleanup(void)123 cleanup(void)
124 {
125 }
126
127 int
test(void)128 test(void)
129 {
130 pid_t pid;
131 int i, status;
132
133 for (i = 0; i < 100; i++) {
134 if (sprintf(path, "fifo.%d.%d", getpid(), i) < 0)
135 err(1, "sprintf()");
136 if (mkfifo(path, 0600) < 0)
137 err(1, "mkfifo(%s)", path);
138 }
139 for (i = 0; i < 100; i++) {
140 if (sprintf(path, "fifo.%d.%d", getpid(), i) < 0)
141 err(1, "sprintf()");
142 if (unlink(path) < 0)
143 err(1, "unlink(%s)", path);
144 }
145
146 if (sprintf(path, "fifo.%d", getpid()) < 0)
147 err(1, "sprintf()");
148 if (mkfifo(path, 0600) < 0)
149 err(1, "mkfifo(%s)", path);
150
151 if ((pid = fork()) == 0) {
152 writer();
153 _exit(EXIT_SUCCESS);
154
155 } else if (pid > 0) {
156 reader();
157 kill(pid, SIGINT);
158 if (waitpid(pid, &status, 0) == -1)
159 warn("waitpid(%d)", pid);
160 } else
161 err(1, "fork(), %s:%d", __FILE__, __LINE__);
162
163 unlink(path);
164
165 return (0);
166 }
167