1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
/* $OpenBSD: test-run-down.c,v 1.2 2019/11/14 21:17:00 anton Exp $ */
/*
* Copyright (c) 2019 Anton Lindqvist <anton@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <err.h>
#include <errno.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include "pipe.h"
struct context {
int c_pipe[2];
char *c_buf;
size_t c_bufsiz;
unsigned int c_nsyscalls;
pthread_cond_t c_cv;
pthread_mutex_t c_mtx;
};
static void ctx_setup(struct context *, size_t);
static void ctx_teardown(struct context *);
static void ctx_lock(struct context *);
static void ctx_unlock(struct context *);
static int test_run_down(size_t);
static void *write_thread(void *);
/*
* Verify delivery of SIGPIPE while trying to write on a pipe where the read end
* is gone.
*
* The writer thread first of writes BIG_PIPE_SIZE number of bytes and then
* tries to perform the same operation again. The ambition is to cause the
* writer thread to go to sleep since the pipe capacity is exhausted. The main
* thread closes the read end at this point causing the writer thread to wake up
* and punt.
*/
int
test_run_down_write_big()
{
return test_run_down(BIG_PIPE_SIZE);
}
/*
* Verify delivery of SIGPIPE while trying to write on a pipe where the read end
* is gone.
*
* The writer thread writes a single byte continously while the main thread ends
* up closing the read end after at least a single byte has been written. The
* ambition is not to make the writer thread go to sleep but rather punt early
* while writing since the read end is gone.
*/
int
test_run_down_write_small()
{
return test_run_down(1);
}
static void
ctx_setup(struct context *ctx, size_t bufsiz)
{
int error;
if (pipe(ctx->c_pipe) == -1)
err(1, "pipe");
ctx->c_buf = malloc(bufsiz);
if (ctx->c_buf == NULL)
err(1, NULL);
ctx->c_bufsiz = bufsiz;
ctx->c_nsyscalls = 0;
error = pthread_cond_init(&ctx->c_cv, NULL);
if (error)
errc(1, error, "pthread_cond_init");
error = pthread_mutex_init(&ctx->c_mtx, NULL);
if (error)
errc(1, error, "pthread_mutex_init");
}
static void
ctx_teardown(struct context *ctx)
{
int error;
if (ctx->c_pipe[0] != -1)
close(ctx->c_pipe[0]);
ctx->c_pipe[0] = -1;
if (ctx->c_pipe[1] != -1)
close(ctx->c_pipe[1]);
ctx->c_pipe[1] = -1;
free(ctx->c_buf);
error = pthread_cond_destroy(&ctx->c_cv);
if (error)
errc(1, error, "pthread_cond_destroy");
error = pthread_mutex_destroy(&ctx->c_mtx);
if (error)
errc(1, error, "pthread_mutex_destroy");
}
static void
ctx_lock(struct context *ctx)
{
int error;
error = pthread_mutex_lock(&ctx->c_mtx);
if (error)
errc(1, error, "pthread_mutex_lock");
}
static void ctx_unlock(struct context *ctx)
{
int error;
error = pthread_mutex_unlock(&ctx->c_mtx);
if (error)
errc(1, error, "pthread_mutex_unlock");
}
static int
test_run_down(size_t bufsiz)
{
struct context ctx;
pthread_t th;
int error;
ctx_setup(&ctx, bufsiz);
error = pthread_create(&th, NULL, write_thread, &ctx);
if (error)
errc(1, error, "pthread_create");
ctx_lock(&ctx);
for (;;) {
if (ctx.c_nsyscalls > 0)
break;
error = pthread_cond_wait(&ctx.c_cv, &ctx.c_mtx);
if (error)
errc(1, error, "pthread_cond_wait");
}
ctx_unlock(&ctx);
/* Signal shutdown to the write thread. */
close(ctx.c_pipe[0]);
ctx.c_pipe[0] = -1;
error = pthread_join(th, NULL);
if (error)
errc(1, error, "pthread_join");
if (!gotsigpipe)
errx(1, "no SIGPIPE");
ctx_teardown(&ctx);
return 0;
}
static void *
write_thread(void *arg)
{
struct context *ctx = arg;
int error;
for (;;) {
ssize_t n;
n = write(ctx->c_pipe[1], ctx->c_buf, ctx->c_bufsiz);
if (n == -1) {
if (errno == EPIPE)
break;
err(1, "write");
}
ctx_lock(ctx);
ctx->c_nsyscalls++;
ctx_unlock(ctx);
error = pthread_cond_signal(&ctx->c_cv);
if (error)
errc(1, error, "pthread_cond_signal");
}
return NULL;
}
|