pipe_wraparound_test.c (228274) | pipe_wraparound_test.c (290914) |
---|---|
1/* 2Copyright (C) 2004 Michael J. Silbersack. All rights reserved. 3 4Redistribution and use in source and binary forms, with or without 5modification, are permitted provided that the following conditions 6are met: 71. Redistributions of source code must retain the above copyright 8 notice, this list of conditions and the following disclaimer. --- 9 unchanged lines hidden (view full) --- 18DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23SUCH DAMAGE. 24*/ 25 | 1/* 2Copyright (C) 2004 Michael J. Silbersack. All rights reserved. 3 4Redistribution and use in source and binary forms, with or without 5modification, are permitted provided that the following conditions 6are met: 71. Redistributions of source code must retain the above copyright 8 notice, this list of conditions and the following disclaimer. --- 9 unchanged lines hidden (view full) --- 18DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23SUCH DAMAGE. 24*/ 25 |
26#include <sys/types.h> 27#include <sys/stat.h> 28#include <sys/wait.h> 29#include <assert.h> 30#include <err.h> 31#include <errno.h> |
|
26#include <stdio.h> | 32#include <stdio.h> |
33#include <stdlib.h> 34#include <string.h> |
|
27#include <unistd.h> | 35#include <unistd.h> |
28#include <sys/stat.h> | |
29 30/* | 36 37/* |
31 * $FreeBSD: head/tools/regression/pipe/pipe-wraparound.c 228274 2011-12-05 04:20:13Z eadler $ | 38 * $FreeBSD: head/tests/sys/kern/pipe/pipe_wraparound_test.c 290914 2015-11-16 05:38:40Z ngie $ |
32 * This program tests to make sure that wraparound writes and reads 33 * are working, assuming that 16K socket buffers are used. In order 34 * to really stress the pipe code with this test, kernel modifications 35 * nay be necessary. 36 */ 37 38int main (void) 39{ | 39 * This program tests to make sure that wraparound writes and reads 40 * are working, assuming that 16K socket buffers are used. In order 41 * to really stress the pipe code with this test, kernel modifications 42 * nay be necessary. 43 */ 44 45int main (void) 46{ |
40char buffer[32768], buffer2[32768]; 41int desc[2]; 42int buggy, error, i, successes, total; 43struct stat status; 44pid_t new_pid; | 47 char buffer[32768], buffer2[32768], go[] = "go", go2[] = "go2"; 48 int desc[2], ipc_coord[2]; 49 ssize_t error, total; 50 int buggy, i; 51 pid_t new_pid; |
45 | 52 |
46buggy = 0; 47total = 0; | 53 buggy = 0; 54 total = 0; |
48 | 55 |
49error = pipe(desc); | 56 error = pipe(desc); 57 if (error == -1) 58 err(1, "Couldn't allocate data pipe"); |
50 | 59 |
51if (error) 52 err(0, "Couldn't allocate fds\n"); | 60 error = pipe(ipc_coord); 61 if (error == -1) 62 err(1, "Couldn't allocate IPC coordination pipe"); |
53 | 63 |
54buffer[0] = 'A'; | 64 buffer[0] = 'A'; |
55 | 65 |
56for (i = 1; i < 32768; i++) { 57 buffer[i] = buffer[i - 1] + 1; 58 if (buffer[i] > 'Z') 59 buffer[i] = 'A'; | 66 for (i = 1; i < (int)sizeof(buffer); i++) { 67 buffer[i] = buffer[i - 1] + 1; 68 if (buffer[i] > 'Z') 69 buffer[i] = 'A'; |
60 } 61 | 70 } 71 |
62new_pid = fork(); | 72 new_pid = fork(); 73 assert(new_pid != -1); |
63 | 74 |
64if (new_pid == 0) { 65 error = write(desc[1], &buffer, 4096); 66 total += error; 67 error = write(desc[1], &buffer[total], 4096); 68 total += error; 69 error = write(desc[1], &buffer[total], 4000); 70 total += error; 71 printf("Wrote %d bytes, sleeping\n", total); 72 usleep(1000000); 73 error = write(desc[1], &buffer[total], 3000); 74 total += error; 75 error = write(desc[1], &buffer[total], 3000); 76 total += error; 77 printf("Wrote another 6000 bytes, %d total, done\n", total); 78} else { 79 usleep(500000); | 75#define SYNC_R(i, _buf) do { \ 76 int _error = errno; \ 77 warnx("%d: waiting for synchronization", __LINE__); \ 78 if (read(ipc_coord[i], &_buf, sizeof(_buf)) != sizeof(_buf)) \ 79 err(1, "failed to synchronize (%s)", (i == 0 ? "parent" : "child")); \ 80 errno = _error; \ 81 } while(0) 82 83#define SYNC_W(i, _buf) do { \ 84 int _error = errno; \ 85 warnx("%d: sending synchronization", __LINE__); \ 86 if (write(ipc_coord[i], &_buf, sizeof(_buf)) != sizeof(_buf)) \ 87 err(1, "failed to synchronize (%s)", (i == 0 ? "child" : "parent")); \ 88 errno = _error; \ 89 } while(0) 90 91#define WRITE(s) do { \ 92 ssize_t _size; \ 93 if ((_size = write(desc[1], &buffer[total], s)) != s) \ 94 warn("short write; wrote %zd, expected %d", _size, s); \ 95 total += _size; \ 96 } while(0) 97 98 if (new_pid == 0) { 99 WRITE(4096); 100 WRITE(4096); 101 WRITE(4000); 102 SYNC_W(0, go2); 103 104 SYNC_R(0, go); 105 WRITE(3000); 106 WRITE(3000); 107 SYNC_W(0, go2); 108 109 _exit(0); 110 } 111 112 SYNC_R(1, go2); |
80 error = read(desc[0], &buffer2, 8192); 81 total += error; | 113 error = read(desc[0], &buffer2, 8192); 114 total += error; |
82 printf("Read %d bytes, going back to sleep\n", error); 83 usleep(1000000); | 115 printf("Read %zd bytes\n", error); 116 SYNC_W(1, go); 117 SYNC_R(1, go2); |
84 error = read(desc[0], &buffer2[total], 16384); 85 total += error; | 118 error = read(desc[0], &buffer2[total], 16384); 119 total += error; |
86 printf("Read %d bytes, done\n", error); | 120 printf("Read %zd bytes, done\n", error); |
87 | 121 |
88 for (i = 0; i < total; i++) { 89 if (buffer[i] != buffer2[i]) { 90 buggy = 1; 91 printf("Location %d input: %hhx output: %hhx\n", 92 i, buffer[i], buffer2[i]); | 122 if (memcmp(buffer, buffer2, total) != 0) { 123 for (i = 0; i < total; i++) { 124 if (buffer[i] != buffer2[i]) { 125 buggy = 1; 126 printf("Location %d input: %hhx output: %hhx\n", 127 i, buffer[i], buffer2[i]); 128 } |
93 } 94 } 95 | 129 } 130 } 131 |
96if (buggy) 97 printf("FAILURE\n"); 98else 99 printf("SUCCESS\n"); 100} | 132 waitpid(new_pid, NULL, 0); |
101 | 133 |
134 if (buggy) 135 errx(1, "FAILURE"); |
|
102 | 136 |
137 printf("SUCCESS\n"); 138 139 exit(0); |
|
103} | 140} |