2 * Copyright (c) 2007 Robert N. M. Watson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * Sockets serialize I/O in each direction in order to avoid interlacing of
31 * I/O by multiple processes or threcvs recving or sending the socket. This
32 * is done using some form of kernel lock (varies by kernel version), called
33 * "sblock" in FreeBSD. However, to avoid unkillable processes waiting on
34 * I/O that may be entirely controlled by a remote network endpoint, that
35 * lock acquisition must be interruptible.
37 * To test this, set up a local domain stream socket pair and a set of three
38 * processes. Two processes block in recv(), the first on sbwait (wait for
39 * I/O), and the second on the sblock waiting for the first to finish. A
40 * third process is responsible for signalling the second process, then
41 * writing to the socket. Depending on the error returned in the second
42 * process, we can tell whether the sblock wait was interrupted, or if
43 * instead the process only woke up when the write was performed.
46 #include <sys/socket.h>
55 static int interrupted;
57 signal_handler(int signum)
64 * Process that will perform a blocking recv on a UNIX domain socket. This
65 * should return one byte of data.
68 blocking_recver(int fd)
73 len = recv(fd, &ch, sizeof(ch), 0);
75 err(-1, "FAIL: blocking_recver: recv");
77 errx(-1, "FAIL: blocking_recver: recv: eof");
79 errx(-1, "FAIL: blocking_recver: recv: %d bytes", len);
81 errx(-1, "FAIL: blocking_recver: interrupted wrong pid");
85 * Process that will perform a locking recv on a UNIX domain socket.
87 * This is where we figure out if the test worked or not. If it has failed,
88 * then recv() will return EOF, as the close() arrives before the signal,
89 * meaning that the wait for the sblock was not interrupted; if it has
90 * succeeded, we get EINTR as the signal interrupts the lock request.
93 locking_recver(int fd)
99 err(-1, "FAIL: locking_recver: sleep");
100 len = recv(fd, &ch, sizeof(ch), 0);
101 if (len < 0 && errno != EINTR)
102 err(-1, "FAIL: locking_recver: recv");
103 if (len < 0 && errno == EINTR) {
104 fprintf(stderr, "PASS\n");
108 errx(-1, "FAIL: locking_recver: recv: eof");
110 errx(-1, "FAIL: locking_recver: not interrupted");
114 signaller(pid_t locking_recver_pid, int fd)
120 warn("signaller sleep(2)");
123 if (kill(locking_recver_pid, SIGHUP) < 0) {
124 warn("signaller kill(%d)", locking_recver_pid);
128 warn("signaller sleep(1)");
131 len = send(fd, &ch, sizeof(ch), 0);
133 warn("signaller send");
136 if (len != sizeof(ch)) {
137 warnx("signaller send ret %d", len);
141 warn("signaller close");
145 warn("signaller sleep(1)");
151 main(int argc, char *argv[])
153 int error, fds[2], recver_fd, sender_fd;
154 pid_t blocking_recver_pid;
155 pid_t locking_recver_pid;
158 if (sigaction(SIGHUP, NULL, &sa) < 0)
159 err(-1, "FAIL: sigaction(SIGHUP, NULL, &sa)");
161 sa.sa_handler = signal_handler;
162 if (sa.sa_flags & SA_RESTART)
163 printf("SIGHUP restartable by default (cleared)\n");
164 sa.sa_flags &= ~SA_RESTART;
166 if (sigaction(SIGHUP, &sa, NULL) < 0)
167 err(-1, "FAIL: sigaction(SIGHUP, &sa, NULL)");
170 if (signal(SIGHUP, signal_handler) == SIG_ERR)
171 err(-1, "FAIL: signal(SIGHUP)");
174 if (socketpair(PF_LOCAL, SOCK_STREAM, 0, fds) < 0)
175 err(-1, "FAIL: socketpair(PF_LOCAL, SOGK_STREAM, 0)");
180 blocking_recver_pid = fork();
181 if (blocking_recver_pid < 0)
182 err(-1, "FAIL: fork");
183 if (blocking_recver_pid == 0) {
185 blocking_recver(recver_fd);
189 locking_recver_pid = fork();
190 if (locking_recver_pid < 0) {
192 kill(blocking_recver_pid, SIGKILL);
194 err(-1, "FAIL: fork");
196 if (locking_recver_pid == 0) {
198 locking_recver(recver_fd);
202 signaller(locking_recver_pid, sender_fd);
204 kill(blocking_recver_pid, SIGKILL);
205 kill(locking_recver_pid, SIGKILL);