summaryrefslogtreecommitdiffstats
path: root/tools/regression/sockets/sblock/sblock.c
blob: a9f9518214b51aed373913c869cf0b966d84d9b5 (plain)
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
/*-
 * Copyright (c) 2007 Robert N. M. Watson
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD$
 */

/*
 * Sockets serialize I/O in each direction in order to avoid interlacing of
 * I/O by multiple processes or threcvs recving or sending the socket.  This
 * is done using some form of kernel lock (varies by kernel version), called
 * "sblock" in FreeBSD.  However, to avoid unkillable processes waiting on
 * I/O that may be entirely controlled by a remote network endpoint, that
 * lock acquisition must be interruptible.
 *
 * To test this, set up a local domain stream socket pair and a set of three
 * processes.  Two processes block in recv(), the first on sbwait (wait for
 * I/O), and the second on the sblock waiting for the first to finish.  A
 * third process is responsible for signalling the second process, then
 * writing to the socket.  Depending on the error returned in the second
 * process, we can tell whether the sblock wait was interrupted, or if
 * instead the process only woke up when the write was performed.
 */

#include <sys/socket.h>

#include <err.h>
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

static int interrupted;
static void
signal_handler(int signum)
{

	interrupted++;
}

/*
 * Process that will perform a blocking recv on a UNIX domain socket.  This
 * should return one byte of data.
 */
static void
blocking_recver(int fd)
{
	ssize_t len;
	char ch;

	len = recv(fd, &ch, sizeof(ch), 0);
	if (len < 0)
		err(-1, "FAIL: blocking_recver: recv");
	if (len == 0)
		errx(-1, "FAIL: blocking_recver: recv: eof");
	if (len != 1)
		errx(-1, "FAIL: blocking_recver: recv: %zd bytes", len);
	if (interrupted)
		errx(-1, "FAIL: blocking_recver: interrupted wrong pid");
}

/*
 * Process that will perform a locking recv on a UNIX domain socket.
 *
 * This is where we figure out if the test worked or not.  If it has failed,
 * then recv() will return EOF, as the close() arrives before the signal,
 * meaning that the wait for the sblock was not interrupted; if it has
 * succeeded, we get EINTR as the signal interrupts the lock request.
 */
static void
locking_recver(int fd)
{
	ssize_t len;
	char ch;

	if (sleep(1) < 0)
		err(-1, "FAIL: locking_recver: sleep");
	len = recv(fd, &ch, sizeof(ch), 0);
	if (len < 0 && errno != EINTR)
		err(-1, "FAIL: locking_recver: recv");
	if (len < 0 && errno == EINTR) {
		fprintf(stderr, "PASS\n");
		exit(0);
	}
	if (len == 0)
		errx(-1, "FAIL: locking_recver: recv: eof");
	if (!interrupted)
		errx(-1, "FAIL: locking_recver: not interrupted");
}

static void
signaller(pid_t locking_recver_pid, int fd)
{
	ssize_t len;
	char ch;

	if (sleep(2) < 0) {
		warn("signaller sleep(2)");
		return;
	}
	if (kill(locking_recver_pid, SIGHUP) < 0) {
		warn("signaller kill(%d)", locking_recver_pid);
		return;
	}
	if (sleep(1) < 0) {
		warn("signaller sleep(1)");
		return;
	}
	len = send(fd, &ch, sizeof(ch), 0);
	if (len < 0) {
		warn("signaller send");
		return;
	}
	if (len != sizeof(ch)) {
		warnx("signaller send ret %zd", len);
		return;
	}
	if (close(fd) < 0) {
		warn("signaller close");
		return;
	}
	if (sleep(1) < 0) {
		warn("signaller sleep(1)");
		return;
	}
}

int
main(int argc, char *argv[])
{
	int error, fds[2], recver_fd, sender_fd;
	pid_t blocking_recver_pid;
	pid_t locking_recver_pid;
	struct sigaction sa;

	if (sigaction(SIGHUP, NULL, &sa) < 0)
		err(-1, "FAIL: sigaction(SIGHUP, NULL, &sa)");

	sa.sa_handler = signal_handler;
	if (sa.sa_flags & SA_RESTART)
		printf("SIGHUP restartable by default (cleared)\n");
	sa.sa_flags &= ~SA_RESTART;

	if (sigaction(SIGHUP, &sa, NULL) < 0)
		err(-1, "FAIL: sigaction(SIGHUP, &sa, NULL)");

#if 0
	if (signal(SIGHUP, signal_handler) == SIG_ERR)
		err(-1, "FAIL: signal(SIGHUP)");
#endif

	if (socketpair(PF_LOCAL, SOCK_STREAM, 0, fds) < 0)
		err(-1, "FAIL: socketpair(PF_LOCAL, SOGK_STREAM, 0)");

	sender_fd = fds[0];
	recver_fd = fds[1];

	blocking_recver_pid = fork();
	if (blocking_recver_pid < 0)
		err(-1, "FAIL: fork");
	if (blocking_recver_pid == 0) {
		close(sender_fd);
		blocking_recver(recver_fd);
		exit(0);
	}

	locking_recver_pid = fork();
	if (locking_recver_pid < 0) {
		error = errno;
		kill(blocking_recver_pid, SIGKILL);
		errno = error;
		err(-1, "FAIL: fork");
	}
	if (locking_recver_pid == 0) {
		close(sender_fd);
		locking_recver(recver_fd);
		exit(0);
	}

	signaller(locking_recver_pid, sender_fd);

	kill(blocking_recver_pid, SIGKILL);
	kill(locking_recver_pid, SIGKILL);
	exit(0);
}
OpenPOWER on IntegriCloud