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