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 int bufsize; 46 47 static void 48 reader(void) { 49 int fd; 50 int i, n, *buf; 51 52 if ((fd = open(path, O_RDWR, 0600)) < 0) { 53 unlink(path); 54 err(1, "open(%s)", path); 55 } 56 if ((buf = malloc(bufsize)) == NULL) 57 err(1, "malloc(%d), %s:%d", bufsize, __FILE__, __LINE__); 58 for (i = 0; i < NB; i+= bufsize) { 59 if ((n = read(fd, buf, bufsize)) < 0) 60 err(1, "read(), %s:%d", __FILE__, __LINE__); 61 if (n == 0) break; 62 } 63 close(fd); 64 free(buf); 65 return; 66 } 67 68 static void 69 writer(void) { 70 int i, *buf; 71 int fd; 72 73 if ((fd = open(path, O_RDWR, 0600)) < 0) { 74 unlink(path); 75 err(1, "open(%s)", path); 76 } 77 if ((buf = malloc(bufsize)) == NULL) 78 err(1, "malloc(%d), %s:%d", bufsize, __FILE__, __LINE__); 79 for (i = 0; i < bufsize / sizeof(int); i++) 80 buf[i] = i; 81 82 for (i = 0; i < NB; i+= bufsize) { 83 if (write(fd, buf, bufsize) < 0) { 84 err(1, "write(%d), %s:%d", fd, 85 __FILE__, __LINE__); 86 } 87 } 88 close(fd); 89 free(buf); 90 return; 91 } 92 93 int 94 setup(int nb) 95 { 96 bufsize = 2 << random_int(2, 12); 97 return (0); 98 } 99 100 void 101 cleanup(void) 102 { 103 } 104 105 int 106 test(void) 107 { 108 pid_t pid; 109 int i, status; 110 111 for (i = 0; i < 100; i++) { 112 if (sprintf(path, "fifo.%d.%d", getpid(), i) < 0) 113 err(1, "sprintf()"); 114 if (mkfifo(path, 0600) < 0) 115 err(1, "mkfifo(%s)", path); 116 } 117 for (i = 0; i < 100; i++) { 118 if (sprintf(path, "fifo.%d.%d", getpid(), i) < 0) 119 err(1, "sprintf()"); 120 if (unlink(path) < 0) 121 err(1, "unlink(%s)", path); 122 } 123 124 125 if (sprintf(path, "fifo.%d", getpid()) < 0) 126 err(1, "sprintf()"); 127 if (mkfifo(path, 0600) < 0) 128 err(1, "mkfifo(%s)", path); 129 130 if ((pid = fork()) == 0) { 131 writer(); 132 exit(EXIT_SUCCESS); 133 134 } else if (pid > 0) { 135 reader(); 136 kill(pid, SIGINT); 137 if (waitpid(pid, &status, 0) == -1) 138 warn("waitpid(%d)", pid); 139 } else 140 err(1, "fork(), %s:%d", __FILE__, __LINE__); 141 142 unlink(path); 143 return (0); 144 } 145