pipe_fstat_bug_test.c (228274) | pipe_fstat_bug_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> 32#include <inttypes.h> |
|
26#include <stdio.h> | 33#include <stdio.h> |
34#include <stdlib.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-fstatbug.c 228274 2011-12-05 04:20:13Z eadler $ | 38 * $FreeBSD: head/tests/sys/kern/pipe/pipe_fstat_bug_test.c 290914 2015-11-16 05:38:40Z ngie $ |
32 * The goal of this program is to see if fstat reports the correct 33 * data count for a pipe. Prior to revision 1.172 of sys_pipe.c, 34 * 0 would be returned once the pipe entered direct write mode. 35 * 36 * Linux (2.6) always returns zero, so it's not a valuable platform 37 * for comparison. 38 */ 39 | 39 * The goal of this program is to see if fstat reports the correct 40 * data count for a pipe. Prior to revision 1.172 of sys_pipe.c, 41 * 0 would be returned once the pipe entered direct write mode. 42 * 43 * Linux (2.6) always returns zero, so it's not a valuable platform 44 * for comparison. 45 */ 46 |
40int main (void) | 47int 48main(void) |
41{ | 49{ |
42char buffer[32768], buffer2[32768]; 43int desc[2]; 44int error, successes = 0; 45struct stat status; 46pid_t new_pid; | 50 char buffer[32768], buffer2[32768], go[] = "go", go2[] = "go2"; 51 int desc[2], ipc_coord[2]; 52 ssize_t error; 53 int successes = 0; 54 struct stat status; 55 pid_t new_pid; |
47 | 56 |
48error = pipe(desc); | 57 error = pipe(desc); 58 if (error == -1) 59 err(1, "Couldn't allocate data pipe"); |
49 | 60 |
50if (error) 51 err(0, "Couldn't allocate fds\n"); | 61 error = pipe(ipc_coord); 62 if (error == -1) 63 err(1, "Couldn't allocate IPC coordination pipe"); |
52 | 64 |
53new_pid = fork(); | 65 new_pid = fork(); 66 assert(new_pid != -1); |
54 | 67 |
55if (new_pid == 0) { 56 write(desc[1], &buffer, 145); 57 usleep(1000000); 58 write(desc[1], &buffer, 2048); 59 usleep(1000000); 60 write(desc[1], &buffer, 4096); 61 usleep(1000000); 62 write(desc[1], &buffer, 8191); 63 usleep(1000000); 64 write(desc[1], &buffer, 8192); 65 usleep(1000000); 66} else { | 68 close(new_pid == 0 ? desc[0] : desc[1]); 69 70#define SYNC_R(i, _buf) do { \ 71 int _error = errno; \ 72 warnx("%d: waiting for synchronization", __LINE__); \ 73 if (read(ipc_coord[i], &_buf, sizeof(_buf)) != sizeof(_buf)) \ 74 err(1, "failed to synchronize (%s)", (i == 0 ? "parent" : "child")); \ 75 errno = _error; \ 76 } while(0) 77 78#define SYNC_W(i, _buf) do { \ 79 int _error = errno; \ 80 warnx("%d: sending synchronization", __LINE__); \ 81 if (write(ipc_coord[i], &_buf, sizeof(_buf)) != sizeof(_buf)) \ 82 err(1, "failed to synchronize (%s)", (i == 0 ? "child" : "parent")); \ 83 errno = _error; \ 84 } while(0) 85 86#define WRITE(s) do { \ 87 ssize_t _size; \ 88 if ((_size = write(desc[1], &buffer, s)) != s) \ 89 warn("short write; wrote %zd, expected %d", _size, s); \ 90 } while(0) 91 92 if (new_pid == 0) { 93 94 SYNC_R(0, go); 95 WRITE(145); 96 SYNC_W(0, go2); 97 98 SYNC_R(0, go); 99 WRITE(2048); 100 SYNC_W(0, go2); 101 102 SYNC_R(0, go); 103 WRITE(4096); 104 SYNC_W(0, go2); 105 106 SYNC_R(0, go); 107 WRITE(8191); 108 SYNC_W(0, go2); 109 110 SYNC_R(0, go); 111 SYNC_W(0, go2); /* XXX: why is this required? */ 112 WRITE(8192); 113 SYNC_W(0, go2); 114 115 close(ipc_coord[0]); 116 close(ipc_coord[1]); 117 118 _exit(0); 119 } 120 |
67 while (successes < 5) { | 121 while (successes < 5) { |
68 usleep(3000); | 122 SYNC_W(1, go); 123 SYNC_R(1, go2); |
69 fstat(desc[0], &status); | 124 fstat(desc[0], &status); |
70 error = read(desc[0], &buffer2, 32768); | 125 error = read(desc[0], &buffer2, sizeof(buffer2)); 126 |
71 if (status.st_size != error) | 127 if (status.st_size != error) |
72 err(0, "FAILURE: stat size %d read size %d\n", (int)status.st_size, error); | 128 err(1, "FAILURE: stat size %jd read size %zd", 129 (intmax_t)status.st_size, error); |
73 if (error > 0) { | 130 if (error > 0) { |
74 printf("SUCCESS at stat size %d read size %d\n", (int)status.st_size, error); | 131 printf("SUCCESS at stat size %jd read size %zd\n", 132 (intmax_t)status.st_size, error); |
75 successes++; | 133 successes++; |
76 /* Sleep to avoid the natural race in reading st_size. */ 77 usleep(1000000); | |
78 } 79 } | 134 } 135 } |
80} | |
81 | 136 |
137 exit(0); |
|
82} | 138} |