Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/tests/sys/kern/pipe/pipe_wraparound_test.c
39488 views
1
/*
2
Copyright (C) 2004 Michael J. Silbersack. All rights reserved.
3
4
Redistribution and use in source and binary forms, with or without
5
modification, are permitted provided that the following conditions
6
are met:
7
1. Redistributions of source code must retain the above copyright
8
notice, this list of conditions and the following disclaimer.
9
2. Redistributions in binary form must reproduce the above copyright
10
notice, this list of conditions and the following disclaimer in the
11
documentation and/or other materials provided with the distribution.
12
13
THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16
ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
17
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23
SUCH 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 <stdio.h>
33
#include <stdlib.h>
34
#include <string.h>
35
#include <unistd.h>
36
37
/*
38
* This program tests to make sure that wraparound writes and reads
39
* are working, assuming that 16K socket buffers are used. In order
40
* to really stress the pipe code with this test, kernel modifications
41
* nay be necessary.
42
*/
43
44
int main (void)
45
{
46
char buffer[32768], buffer2[32768], go[] = "go", go2[] = "go2";
47
int desc[2], ipc_coord[2];
48
ssize_t error, total;
49
int buggy, i;
50
pid_t new_pid;
51
52
buggy = 0;
53
total = 0;
54
55
error = pipe(desc);
56
if (error == -1)
57
err(1, "Couldn't allocate data pipe");
58
59
error = pipe(ipc_coord);
60
if (error == -1)
61
err(1, "Couldn't allocate IPC coordination pipe");
62
63
buffer[0] = 'A';
64
65
for (i = 1; i < (int)sizeof(buffer); i++) {
66
buffer[i] = buffer[i - 1] + 1;
67
if (buffer[i] > 'Z')
68
buffer[i] = 'A';
69
}
70
71
new_pid = fork();
72
assert(new_pid != -1);
73
74
#define SYNC_R(i, _buf) do { \
75
int _error = errno; \
76
warnx("%d: waiting for synchronization", __LINE__); \
77
if (read(ipc_coord[i], &_buf, sizeof(_buf)) != sizeof(_buf)) \
78
err(1, "failed to synchronize (%s)", (i == 0 ? "parent" : "child")); \
79
errno = _error; \
80
} while(0)
81
82
#define SYNC_W(i, _buf) do { \
83
int _error = errno; \
84
warnx("%d: sending synchronization", __LINE__); \
85
if (write(ipc_coord[i], &_buf, sizeof(_buf)) != sizeof(_buf)) \
86
err(1, "failed to synchronize (%s)", (i == 0 ? "child" : "parent")); \
87
errno = _error; \
88
} while(0)
89
90
#define WRITE(s) do { \
91
ssize_t _size; \
92
if ((_size = write(desc[1], &buffer[total], s)) != s) \
93
warn("short write; wrote %zd, expected %d", _size, s); \
94
total += _size; \
95
} while(0)
96
97
if (new_pid == 0) {
98
WRITE(4096);
99
WRITE(4096);
100
WRITE(4000);
101
SYNC_W(0, go2);
102
103
SYNC_R(0, go);
104
WRITE(3000);
105
WRITE(3000);
106
SYNC_W(0, go2);
107
108
_exit(0);
109
}
110
111
SYNC_R(1, go2);
112
error = read(desc[0], &buffer2, 8192);
113
total += error;
114
printf("Read %zd bytes\n", error);
115
SYNC_W(1, go);
116
SYNC_R(1, go2);
117
error = read(desc[0], &buffer2[total], 16384);
118
total += error;
119
printf("Read %zd bytes, done\n", error);
120
121
if (memcmp(buffer, buffer2, total) != 0) {
122
for (i = 0; i < total; i++) {
123
if (buffer[i] != buffer2[i]) {
124
buggy = 1;
125
printf("Location %d input: %hhx output: %hhx\n",
126
i, buffer[i], buffer2[i]);
127
}
128
}
129
}
130
131
waitpid(new_pid, NULL, 0);
132
133
if (buggy)
134
errx(1, "FAILURE");
135
136
printf("SUCCESS\n");
137
138
exit(0);
139
}
140
141