pipe_reverse_test.c (228274) | pipe_reverse_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-reverse.c 228274 2011-12-05 04:20:13Z eadler $ | 38 * $FreeBSD: head/tests/sys/kern/pipe/pipe_reverse_test.c 290914 2015-11-16 05:38:40Z ngie $ |
32 * This program simply tests writing through the reverse direction of 33 * a pipe. Nothing too fancy, it's only needed because most pipe-using 34 * programs never touch the reverse direction (it doesn't exist on 35 * Linux.) 36 */ 37 | 39 * This program simply tests writing through the reverse direction of 40 * a pipe. Nothing too fancy, it's only needed because most pipe-using 41 * programs never touch the reverse direction (it doesn't exist on 42 * Linux.) 43 */ 44 |
38int main (void) | 45int 46main(void) |
39{ | 47{ |
40char buffer[65535], buffer2[65535]; 41int desc[2]; 42int buggy, error, i, successes, total; 43struct stat status; 44pid_t new_pid; | 48 char buffer[65535], buffer2[65535], go[] = "go", go2[] = "go2"; 49 int desc[2], ipc_coord[2]; 50 size_t i; 51 ssize_t total; 52 int buggy, error; 53 pid_t new_pid; |
45 | 54 |
46buggy = 0; 47total = 0; | 55 buggy = 0; 56 total = 0; |
48 | 57 |
49error = pipe(desc); | 58 error = pipe(desc); 59 if (error == -1) 60 err(1, "Couldn't allocate data pipe"); |
50 | 61 |
51if (error) 52 err(0, "Couldn't allocate fds\n"); | 62 error = pipe(ipc_coord); 63 if (error == -1) 64 err(1, "Couldn't allocate IPC coordination pipe"); |
53 | 65 |
54buffer[0] = 'A'; | 66 buffer[0] = 'A'; |
55 | 67 |
56for (i = 1; i < 65535; i++) { 57 buffer[i] = buffer[i - 1] + 1; 58 if (buffer[i] > 'Z') 59 buffer[i] = 'A'; | 68 for (i = 1; i < (int)sizeof(buffer); i++) { 69 buffer[i] = buffer[i - 1] + 1; 70 if (buffer[i] > 'Z') 71 buffer[i] = 'A'; |
60 } 61 | 72 } 73 |
62new_pid = fork(); | 74 new_pid = fork(); 75 assert(new_pid != -1); |
63 | 76 |
64if (new_pid == 0) { 65 error = write(desc[0], &buffer, 4096); | 77#define SYNC_R(i, _buf) do { \ 78 int _error = errno; \ 79 warnx("%d: waiting for synchronization", __LINE__); \ 80 if (read(ipc_coord[i], &_buf, sizeof(_buf)) != sizeof(_buf)) \ 81 err(1, "failed to synchronize (%s)", (i == 0 ? "parent" : "child")); \ 82 errno = _error; \ 83 } while(0) 84 85#define SYNC_W(i, _buf) do { \ 86 int _error = errno; \ 87 warnx("%d: sending synchronization", __LINE__); \ 88 if (write(ipc_coord[i], &_buf, sizeof(_buf)) != sizeof(_buf)) \ 89 err(1, "failed to synchronize (%s)", (i == 0 ? "child" : "parent")); \ 90 errno = _error; \ 91 } while(0) 92 93#define WRITE(s) do { \ 94 ssize_t _size; \ 95 if ((_size = write(desc[1], &buffer[total], s)) != s) \ 96 warn("short write; wrote %zd, expected %d", _size, s); \ 97 total += _size; \ 98 } while(0) 99 100 if (new_pid == 0) { 101 SYNC_R(0, go); 102 for (i = 0; i < 8; i++) 103 WRITE(4096); 104 105 SYNC_W(0, go2); 106 SYNC_R(0, go); 107 108 for (i = 0; i < 2; i++) 109 WRITE(4096); 110 111 SYNC_W(0, go2); 112 113 _exit(0); 114 } 115 116 SYNC_W(1, go); 117 SYNC_R(1, go2); 118 119 error = read(desc[0], &buffer2, 8 * 4096); |
66 total += error; | 120 total += error; |
67 error = write(desc[0], &buffer[total], 4096); | 121 printf("Read %d bytes\n", error); 122 123 SYNC_W(1, go); 124 SYNC_R(1, go2); 125 126 error = read(desc[0], &buffer2[total], 2 * 4096); |
68 total += error; | 127 total += error; |
69 error = write(desc[0], &buffer[total], 4096); 70 total += error; 71 error = write(desc[0], &buffer[total], 4096); 72 total += error; 73 error = write(desc[0], &buffer[total], 4096); 74 total += error; 75 error = write(desc[0], &buffer[total], 4096); 76 total += error; 77 error = write(desc[0], &buffer[total], 4096); 78 total += error; 79 error = write(desc[0], &buffer[total], 4096); 80 total += error; 81 printf("Wrote %d bytes, sleeping\n", total); 82 usleep(1000000); 83 error = write(desc[0], &buffer[total], 4096); 84 total += error; 85 error = write(desc[0], &buffer[total], 4096); 86 total += error; 87 printf("Wrote another 8192 bytes, %d total, done\n", total); 88} else { 89 usleep(500000); 90 error = read(desc[1], &buffer2, 32768); 91 total += error; 92 printf("Read %d bytes, going back to sleep\n", error); 93 usleep(1000000); 94 error = read(desc[1], &buffer2[total], 8192); 95 total += error; | |
96 printf("Read %d bytes, done\n", error); 97 | 128 printf("Read %d bytes, done\n", error); 129 |
98 for (i = 0; i < total; i++) { 99 if (buffer[i] != buffer2[i]) { 100 buggy = 1; 101 printf("Location %d input: %hhx output: %hhx\n", 102 i, buffer[i], buffer2[i]); | 130 if (memcmp(buffer, buffer2, total) != 0) { 131 for (i = 0; i < (size_t)total; i++) { 132 if (buffer[i] != buffer2[i]) { 133 buggy = 1; 134 printf("Location %zu input: %hhx " 135 "output: %hhx\n", 136 i, buffer[i], buffer2[i]); 137 } |
103 } 104 } 105 | 138 } 139 } 140 |
106if ((buggy == 1) || (total != 40960)) 107 printf("FAILURE\n"); 108else 109 printf("SUCCESS\n"); | 141 waitpid(new_pid, NULL, 0); |
110 | 142 |
111} | 143 if ((buggy == 1) || (total != 10 * 4096)) 144 errx(1, "FAILED"); 145 else 146 printf("SUCCESS\n"); |
112 | 147 |
148 exit(0); |
|
113} | 149} |