Path: blob/main/tools/regression/sockets/sblock/sblock.c
48255 views
/*-1* Copyright (c) 2007 Robert N. M. Watson2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*/2526/*27* Sockets serialize I/O in each direction in order to avoid interlacing of28* I/O by multiple processes or threcvs recving or sending the socket. This29* is done using some form of kernel lock (varies by kernel version), called30* "sblock" in FreeBSD. However, to avoid unkillable processes waiting on31* I/O that may be entirely controlled by a remote network endpoint, that32* lock acquisition must be interruptible.33*34* To test this, set up a local domain stream socket pair and a set of three35* processes. Two processes block in recv(), the first on sbwait (wait for36* I/O), and the second on the sblock waiting for the first to finish. A37* third process is responsible for signalling the second process, then38* writing to the socket. Depending on the error returned in the second39* process, we can tell whether the sblock wait was interrupted, or if40* instead the process only woke up when the write was performed.41*/4243#include <sys/socket.h>4445#include <err.h>46#include <errno.h>47#include <signal.h>48#include <stdio.h>49#include <stdlib.h>50#include <unistd.h>5152static int interrupted;53static void54signal_handler(int signum __unused)55{5657interrupted++;58}5960/*61* Process that will perform a blocking recv on a UNIX domain socket. This62* should return one byte of data.63*/64static void65blocking_recver(int fd)66{67ssize_t len;68char ch;6970len = recv(fd, &ch, sizeof(ch), 0);71if (len < 0)72err(-1, "FAIL: blocking_recver: recv");73if (len == 0)74errx(-1, "FAIL: blocking_recver: recv: eof");75if (len != 1)76errx(-1, "FAIL: blocking_recver: recv: %zd bytes", len);77if (interrupted)78errx(-1, "FAIL: blocking_recver: interrupted wrong pid");79}8081/*82* Process that will perform a locking recv on a UNIX domain socket.83*84* This is where we figure out if the test worked or not. If it has failed,85* then recv() will return EOF, as the close() arrives before the signal,86* meaning that the wait for the sblock was not interrupted; if it has87* succeeded, we get EINTR as the signal interrupts the lock request.88*/89static void90locking_recver(int fd)91{92ssize_t len;93char ch;9495if (sleep(1) != 0)96err(-1, "FAIL: locking_recver: sleep");97len = recv(fd, &ch, sizeof(ch), 0);98if (len < 0 && errno != EINTR)99err(-1, "FAIL: locking_recver: recv");100if (len < 0 && errno == EINTR) {101fprintf(stderr, "PASS\n");102exit(0);103}104if (len == 0)105errx(-1, "FAIL: locking_recver: recv: eof");106if (!interrupted)107errx(-1, "FAIL: locking_recver: not interrupted");108}109110static void111signaller(pid_t locking_recver_pid, int fd)112{113ssize_t len;114char ch;115116if (sleep(2) != 0) {117warn("signaller sleep(2)");118return;119}120if (kill(locking_recver_pid, SIGHUP) < 0) {121warn("signaller kill(%d)", locking_recver_pid);122return;123}124if (sleep(1) != 0) {125warn("signaller sleep(1)");126return;127}128len = send(fd, &ch, sizeof(ch), 0);129if (len < 0) {130warn("signaller send");131return;132}133if (len != sizeof(ch)) {134warnx("signaller send ret %zd", len);135return;136}137if (close(fd) < 0) {138warn("signaller close");139return;140}141if (sleep(1) != 0) {142warn("signaller sleep(1)");143return;144}145}146147int148main(void)149{150int error, fds[2], recver_fd, sender_fd;151pid_t blocking_recver_pid;152pid_t locking_recver_pid;153struct sigaction sa;154155if (sigaction(SIGHUP, NULL, &sa) < 0)156err(-1, "FAIL: sigaction(SIGHUP, NULL, &sa)");157158sa.sa_handler = signal_handler;159if (sa.sa_flags & SA_RESTART)160printf("SIGHUP restartable by default (cleared)\n");161sa.sa_flags &= ~SA_RESTART;162163if (sigaction(SIGHUP, &sa, NULL) < 0)164err(-1, "FAIL: sigaction(SIGHUP, &sa, NULL)");165166#if 0167if (signal(SIGHUP, signal_handler) == SIG_ERR)168err(-1, "FAIL: signal(SIGHUP)");169#endif170171if (socketpair(PF_LOCAL, SOCK_STREAM, 0, fds) < 0)172err(-1, "FAIL: socketpair(PF_LOCAL, SOGK_STREAM, 0)");173174sender_fd = fds[0];175recver_fd = fds[1];176177blocking_recver_pid = fork();178if (blocking_recver_pid < 0)179err(-1, "FAIL: fork");180if (blocking_recver_pid == 0) {181close(sender_fd);182blocking_recver(recver_fd);183exit(0);184}185186locking_recver_pid = fork();187if (locking_recver_pid < 0) {188error = errno;189kill(blocking_recver_pid, SIGKILL);190errno = error;191err(-1, "FAIL: fork");192}193if (locking_recver_pid == 0) {194close(sender_fd);195locking_recver(recver_fd);196exit(0);197}198199signaller(locking_recver_pid, sender_fd);200201kill(blocking_recver_pid, SIGKILL);202kill(locking_recver_pid, SIGKILL);203exit(0);204}205206207