Path: blob/main/tests/sys/kqueue/kqueue_peek_signal.c
39483 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright 2021 Jan Kokemüller4* Copyright 2021 Alex Richardson <[email protected]>5*6* This work was supported by Innovate UK project 105694, "Digital Security by7* Design (DSbD) Technology Platform Prototype".8*9* Redistribution and use in source and binary forms, with or without10* modification, are permitted provided that the following conditions11* are met:12* 1. Redistributions of source code must retain the above copyright13* notice, this list of conditions and the following disclaimer.14* 2. Redistributions in binary form must reproduce the above copyright15* notice, this list of conditions and the following disclaimer in the16* documentation and/or other materials provided with the distribution.17*18* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND19* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE20* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE21* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE22* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL23* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS24* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)25* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT26* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY27* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF28* SUCH DAMAGE.29*/30#include <sys/cdefs.h>31#include <sys/event.h>3233#include <atf-c.h>34#include <errno.h>35#include <signal.h>3637ATF_TC_WITHOUT_HEAD(main);3839ATF_TC_BODY(main, tc)40{41int rv;4243sigset_t set;44rv = sigemptyset(&set);45ATF_REQUIRE_EQ(0, rv);46rv = sigaddset(&set, SIGUSR1);47ATF_REQUIRE_EQ(0, rv);48rv = sigprocmask(SIG_BLOCK, &set, NULL);49ATF_REQUIRE_EQ(0, rv);5051int skq = kqueue();52ATF_REQUIRE(skq >= 0);5354struct kevent kev;55EV_SET(&kev, SIGUSR1, EVFILT_SIGNAL, EV_ADD, 0, 0, 0);56rv = kevent(skq, &kev, 1, NULL, 0, NULL);57ATF_REQUIRE_EQ(0, rv);5859int kq = kqueue();60ATF_REQUIRE(kq >= 0);6162EV_SET(&kev, skq, EVFILT_READ, EV_ADD | EV_CLEAR, 0, 0, 0);63rv = kevent(kq, &kev, 1, NULL, 0, NULL);64ATF_REQUIRE_EQ(0, rv);6566/*67* It was previously not guaranteed that sending a signal to self would68* be immediately visible in the nested kqueue activation with a zero69* timeout. As of https://reviews.freebsd.org/D31858, the kqueue task70* queue will be processed in this case, so we are guaranteed to see the71* SIGUSR1 here even with a zero timeout. We run the code below in a72* loop to make it more likely that older kernels without the fix fail73* this test.74*/75for (int i = 0; i < 100; i++) {76rv = kill(getpid(), SIGUSR1);77ATF_REQUIRE_EQ(0, rv);7879rv = kevent(kq, NULL, 0, &kev, 1, &(struct timespec) { 0, 0 });80ATF_REQUIRE_EQ_MSG(1, rv,81"Unexpected result %d from kevent() after %d iterations",82rv, i);83rv = kevent(kq, NULL, 0, &kev, 1, &(struct timespec) { 0, 0 });84ATF_REQUIRE_EQ(0, rv);8586rv = kevent(skq, NULL, 0, &kev, 1, &(struct timespec) { 0, 0 });87ATF_REQUIRE_EQ(1, rv);88rv = kevent(skq, NULL, 0, &kev, 1, &(struct timespec) { 0, 0 });89ATF_REQUIRE_EQ(0, rv);9091siginfo_t siginfo;92rv = sigtimedwait(&set, &siginfo, &(struct timespec) { 0, 0 });93ATF_REQUIRE_EQ(SIGUSR1, rv);9495rv = sigtimedwait(&set, &siginfo, &(struct timespec) { 0, 0 });96ATF_REQUIRE_ERRNO(EAGAIN, rv < 0);97}98}99100ATF_TP_ADD_TCS(tp)101{102ATF_TP_ADD_TC(tp, main);103104return (atf_no_error());105}106107108