From 7dc859a5f3cf2ccee2b5faf855648c3e1da4e215 Mon Sep 17 00:00:00 2001 From: Kyle Evans Date: Thu, 12 Dec 2019 01:41:55 +0000 Subject: [PATCH] Add sigsetop extensions commonly found in musl libc and glibc These functions (sigandset, sigisemptyset, sigorset) are commonly available in at least musl libc and glibc; sigorset, at least, has proven quite useful in qemu-bsd-user work for tracking the current process signal mask in a more self-documenting/aesthetically pleasing manner. Reviewed by: bapt, jilles, pfg MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D22187 --- include/signal.h | 3 + lib/libc/gen/Makefile.inc | 5 +- lib/libc/gen/Symbol.map | 6 + lib/libc/gen/sigsetops.3 | 55 +++++++- lib/libc/gen/sigsetops.c | 31 +++++ lib/libc/tests/gen/Makefile | 1 + lib/libc/tests/gen/sigsetops_test.c | 191 ++++++++++++++++++++++++++++ 7 files changed, 288 insertions(+), 4 deletions(-) create mode 100644 lib/libc/tests/gen/sigsetops_test.c diff --git a/include/signal.h b/include/signal.h index 56d1b82e87a9..3b20875290d6 100644 --- a/include/signal.h +++ b/include/signal.h @@ -122,7 +122,10 @@ void psignal(int, const char *); #endif #if __BSD_VISIBLE +int sigandset(sigset_t *dest, const sigset_t *left, const sigset_t *right); int sigblock(int); +int sigisemptyset(const sigset_t *set); +int sigorset(sigset_t *dest, const sigset_t *left, const sigset_t *right); int sigreturn(const struct __ucontext *); int sigsetmask(int); int sigstack(const struct sigstack *, struct sigstack *); diff --git a/lib/libc/gen/Makefile.inc b/lib/libc/gen/Makefile.inc index 3a543ddf1cc4..9028043b532b 100644 --- a/lib/libc/gen/Makefile.inc +++ b/lib/libc/gen/Makefile.inc @@ -497,10 +497,13 @@ MLINKS+=setjmp.3 _longjmp.3 \ MLINKS+=setmode.3 getmode.3 MLINKS+=setproctitle.3 setproctitle_fast.3 MLINKS+=sigsetops.3 sigaddset.3 \ + sigsetops.3 sigandset.3 \ sigsetops.3 sigdelset.3 \ sigsetops.3 sigemptyset.3 \ sigsetops.3 sigfillset.3 \ - sigsetops.3 sigismember.3 + sigsetops.3 sigisemptyset.3 \ + sigsetops.3 sigismember.3 \ + sigsetops.3 sigorset.3 MLINKS+=statvfs.3 fstatvfs.3 MLINKS+=stringlist.3 sl_add.3 \ stringlist.3 sl_find.3 \ diff --git a/lib/libc/gen/Symbol.map b/lib/libc/gen/Symbol.map index 703cfe796a38..c6ecbc65f849 100644 --- a/lib/libc/gen/Symbol.map +++ b/lib/libc/gen/Symbol.map @@ -422,6 +422,12 @@ FBSD_1.5 { timespec_get; }; +FBSD_1.6 { + sigandset; + sigisemptyset; + sigorset; +}; + FBSDprivate_1.0 { /* needed by thread libraries */ __thr_jtable; diff --git a/lib/libc/gen/sigsetops.3 b/lib/libc/gen/sigsetops.3 index 48c14106a4cc..a184289aeb8a 100644 --- a/lib/libc/gen/sigsetops.3 +++ b/lib/libc/gen/sigsetops.3 @@ -28,15 +28,18 @@ .\" @(#)sigsetops.3 8.1 (Berkeley) 6/4/93 .\" $FreeBSD$ .\" -.Dd December 16, 2004 +.Dd October 29, 2019 .Dt SIGSETOPS 3 .Os .Sh NAME .Nm sigemptyset , .Nm sigfillset , .Nm sigaddset , +.Nm sigandset , .Nm sigdelset , -.Nm sigismember +.Nm sigisemptyset , +.Nm sigismember , +.Nm sigorset .Nd manipulate signal sets .Sh LIBRARY .Lb libc @@ -49,9 +52,15 @@ .Ft int .Fn sigaddset "sigset_t *set" "int signo" .Ft int +.Fn sigandset "sigset_t *set" "const sigset_t *left" "const sigset_t *right" +.Ft int .Fn sigdelset "sigset_t *set" "int signo" .Ft int +.Fn sigisemptyset "const sigset_t *set" +.Ft int .Fn sigismember "const sigset_t *set" "int signo" +.Ft int +.Fn sigorset "sigset_t *set" "const sigset_t *left" "const sigset_t *right" .Sh DESCRIPTION These functions manipulate signal sets stored in a .Fa sigset_t . @@ -78,22 +87,54 @@ function adds the specified signal to the signal set. .Pp The +.Fn sigandset +function sets the specified +.Fa set +to the logical AND of all signals from the +.Fa left +and +.Fa right +signal sets. +.Pp +The .Fn sigdelset function deletes the specified signal .Fa signo from the signal set. .Pp The +.Fn sigisemptyset +function returns whether the specified +.Fa set +is empty or not. +.Pp +The .Fn sigismember function returns whether a specified signal .Fa signo is contained in the signal set. +.Pp +The +.Fn sigorset +function sets the specified +.Fa set +to the logical OR of all signals from the +.Fa left +and +.Fa right +signal sets. .Sh RETURN VALUES The +.Fn sigisemptyset +function returns 1 +if the set is empty, 0 otherwise. +.Pp +The .Fn sigismember function returns 1 if the signal is a member of the set, 0 otherwise. +.Pp The other functions return 0 upon success. A \-1 return value indicates an error occurred and the global variable @@ -113,5 +154,13 @@ has an invalid value. .Xr sigprocmask 2 , .Xr sigsuspend 2 .Sh STANDARDS -These functions are defined by +The +.Fn sigandset , +.Fn sigisemptyset , +and +.Fn sigorset +functions are FreeBSD extensions, compatible with functions of the same name +provided by both musl libc and GNU libc. +.Pp +The rest of these functions are defined by .St -p1003.1-88 . diff --git a/lib/libc/gen/sigsetops.c b/lib/libc/gen/sigsetops.c index 933bfa13ab01..53a450c0e5f6 100644 --- a/lib/libc/gen/sigsetops.c +++ b/lib/libc/gen/sigsetops.c @@ -80,6 +80,37 @@ sigfillset(sigset_t *set) return (0); } +int +sigorset(sigset_t *dest, const sigset_t *left, const sigset_t *right) +{ + int i; + + for (i = 0; i < _SIG_WORDS; i++) + dest->__bits[i] = left->__bits[i] | right->__bits[i]; + return (0); +} + +int +sigandset(sigset_t *dest, const sigset_t *left, const sigset_t *right) +{ + int i; + + for (i = 0; i < _SIG_WORDS; i++) + dest->__bits[i] = left->__bits[i] & right->__bits[i]; + return (0); +} + +int +sigisemptyset(const sigset_t *set) +{ + int i; + + for (i = 0; i < _SIG_WORDS; i++) + if (set->__bits[i] != 0) + return (0); + return (1); +} + int sigismember(const sigset_t *set, int signo) { diff --git a/lib/libc/tests/gen/Makefile b/lib/libc/tests/gen/Makefile index 80bec69bbcaa..182966aadb26 100644 --- a/lib/libc/tests/gen/Makefile +++ b/lib/libc/tests/gen/Makefile @@ -17,6 +17,7 @@ ATF_TESTS_C+= makecontext_test ATF_TESTS_C+= popen_test ATF_TESTS_C+= posix_spawn_test ATF_TESTS_C+= realpath2_test +ATF_TESTS_C+= sigsetops_test ATF_TESTS_C+= wordexp_test # TODO: t_closefrom, t_cpuset, t_fmtcheck, t_randomid, diff --git a/lib/libc/tests/gen/sigsetops_test.c b/lib/libc/tests/gen/sigsetops_test.c new file mode 100644 index 000000000000..ba9abed403b0 --- /dev/null +++ b/lib/libc/tests/gen/sigsetops_test.c @@ -0,0 +1,191 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * + * Copyright (c) 2019 Kyle Evans + * 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. + */ + +#include +__FBSDID("$FreeBSD$"); + +#include +#include +#include + +#include + +/* Return the status of the specified sig's bit. */ +static bool +sigbitstatus(const sigset_t *set, int sig) +{ + + return (set->__bits[_SIG_WORD(sig)] & _SIG_BIT(sig)) != 0; +} + +/* Verify that sig is the lone bit set in the sigset. */ +static void +siglonebit(const sigset_t *set, int sig) +{ + int i; + + for (i = 0; i < _SIG_WORDS; ++i) { + if (i != _SIG_WORD(sig)) + ATF_REQUIRE_MSG(set->__bits[i] == 0, + "word %d altered to %x", i, set->__bits[i]); + else + ATF_REQUIRE_MSG((set->__bits[i] & ~_SIG_BIT(sig)) == 0, + "word %d has other bits set: %x", i, + set->__bits[i] & ~_SIG_BIT(sig)); + } +} + +static void +sigcompare(const sigset_t *left, const sigset_t *right) +{ + int i; + + for (i = 0; i < _SIG_WORDS; ++i) { + ATF_REQUIRE_MSG(left->__bits[i] == right->__bits[i], + "sig comparison failed at %d; left=%x, right=%x", + i, left->__bits[i], right->__bits[i]); + } +} + +/* + * Test implementation details of our sigsetops... make sure the correct bits + * are getting set, for the most part, and that duplicate operations don't + * error out. + */ +ATF_TC_WITHOUT_HEAD(posix_sigsetop_test); +ATF_TC_BODY(posix_sigsetop_test, tc) +{ + sigset_t set; + int i; + + ATF_REQUIRE(sigfillset(&set) == 0); + for (i = 0; i < _SIG_WORDS; ++i) { + ATF_REQUIRE_MSG(set.__bits[i] == ~0U, "sigfillset failed @ %d", + i); + } + ATF_REQUIRE(sigemptyset(&set) == 0); + for (i = 0; i < _SIG_WORDS; ++i) { + ATF_REQUIRE_MSG(set.__bits[i] == 0, "sigemptyset failed @ %d", + i); + } + /* Ensure that sigismember reflects the empty set status. */ + for (i = 1; i < NSIG; ++i) { + ATF_REQUIRE(sigismember(&set, i) == 0); + } + + ATF_REQUIRE(sigaddset(&set, -1) == -1 && errno == EINVAL); + ATF_REQUIRE(sigaddset(&set, _SIG_MAXSIG + 1) == -1 && errno == EINVAL); + ATF_REQUIRE(sigdelset(&set, -1) == -1 && errno == EINVAL); + ATF_REQUIRE(sigdelset(&set, _SIG_MAXSIG + 1) == -1 && errno == EINVAL); + + ATF_REQUIRE(sigaddset(&set, SIGSEGV) == 0); + ATF_REQUIRE(sigismember(&set, SIGSEGV) != 0); + ATF_REQUIRE_MSG(sigbitstatus(&set, SIGSEGV), "sigaddset failure"); + siglonebit(&set, SIGSEGV); + + /* + * A second addition should succeed without altering the state. This + * should be trivially true. + */ + ATF_REQUIRE(sigaddset(&set, SIGSEGV) == 0); + ATF_REQUIRE_MSG(sigbitstatus(&set, SIGSEGV), + "sigaddset twice changed bit"); + + ATF_REQUIRE(sigdelset(&set, SIGSEGV) == 0); + ATF_REQUIRE_MSG(!sigbitstatus(&set, SIGSEGV), "sigdelset failure"); + ATF_REQUIRE(sigismember(&set, SIGSEGV) == 0); + ATF_REQUIRE(sigdelset(&set, SIGSEGV) == 0); + ATF_REQUIRE_MSG(!sigbitstatus(&set, SIGSEGV), + "sigdelset twice changed bit"); + for (i = 0; i < _SIG_WORDS; ++i) { + ATF_REQUIRE_MSG(set.__bits[i] == 0, "set not empty @ %d", + i); + } + for (i = 1; i < NSIG; ++i) { + ATF_REQUIRE(sigismember(&set, i) == 0); + } +} + +/* + * Test extended sigset ops for union/intersection and testing of empty set. + */ +ATF_TC_WITHOUT_HEAD(extended_sigsetop_test); +ATF_TC_BODY(extended_sigsetop_test, tc) +{ + sigset_t chkset, set1, set2, set3; + + sigemptyset(&chkset); + sigemptyset(&set1); + sigemptyset(&set2); + ATF_REQUIRE(sigisemptyset(&chkset) != 0); + sigaddset(&set1, SIGSEGV); + sigaddset(&set2, SIGKILL); + sigaddset(&chkset, SIGSEGV); + ATF_REQUIRE(sigisemptyset(&chkset) == 0); + sigaddset(&chkset, SIGKILL); + ATF_REQUIRE(sigorset(&set3, &set1, &set2) == 0); + ATF_REQUIRE(sigbitstatus(&set3, SIGSEGV)); + ATF_REQUIRE(sigbitstatus(&set3, SIGKILL)); + + /* + * chkset was built with our POSIX-specified set operations that we've + * already tested, so it's a good comparison. + */ + sigcompare(&chkset, &set3); + /* + * Clear chkset; make sure sigisemptyset() still looks ok. sigaddset + * and sigdelset have already been tested to make sure that they're not + * touching other bits. + */ + sigdelset(&chkset, SIGSEGV); + sigdelset(&chkset, SIGKILL); + ATF_REQUIRE(sigisemptyset(&chkset) != 0); + ATF_REQUIRE(sigandset(&set3, &set1, &set2) == 0); + /* Make sure we clobbered these. */ + ATF_REQUIRE(!sigbitstatus(&set3, SIGSEGV)); + ATF_REQUIRE(!sigbitstatus(&set3, SIGKILL)); + ATF_REQUIRE(sigisemptyset(&set3) != 0); + /* Rebuild for sigandset test */ + sigemptyset(&set1); + sigemptyset(&set2); + sigaddset(&set1, SIGSEGV); + sigaddset(&set1, SIGKILL); + sigaddset(&set2, SIGSEGV); + ATF_REQUIRE(sigandset(&set3, &set1, &set2) == 0); + ATF_REQUIRE(sigbitstatus(&set3, SIGSEGV)); + ATF_REQUIRE(!sigbitstatus(&set3, SIGKILL)); +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, posix_sigsetop_test); + ATF_TP_ADD_TC(tp, extended_sigsetop_test); + + return (atf_no_error()); +}