From 400cdcd624230f082be3fc322fb5abf8cd553936 Mon Sep 17 00:00:00 2001 From: Yong Cong Sin Date: Thu, 6 Jul 2023 17:05:39 +0800 Subject: [PATCH] posix: signal: implement sigdelset Implementation and ztest for sigdelset. Signed-off-by: Yong Cong Sin --- include/zephyr/posix/signal.h | 1 + lib/posix/signal.c | 12 +++++ tests/posix/common/src/signal.c | 86 +++++++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+) diff --git a/include/zephyr/posix/signal.h b/include/zephyr/posix/signal.h index 6615fbdae7f40a5..0da478b02e4c685 100644 --- a/include/zephyr/posix/signal.h +++ b/include/zephyr/posix/signal.h @@ -62,6 +62,7 @@ typedef struct { int sigemptyset(sigset_t *set); int sigfillset(sigset_t *set); int sigaddset(sigset_t *set, int signo); +int sigdelset(sigset_t *set, int signo); #endif /* CONFIG_POSIX_SIGNAL */ #ifndef SIGEV_NONE diff --git a/lib/posix/signal.c b/lib/posix/signal.c index 3aaad5cf046ae59..3507cdce6748de3 100644 --- a/lib/posix/signal.c +++ b/lib/posix/signal.c @@ -49,3 +49,15 @@ int sigaddset(sigset_t *set, int signo) return 0; } + +int sigdelset(sigset_t *set, int signo) +{ + if (signo_invalid(signo)) { + errno = EINVAL; + return -1; + } + + WRITE_BIT(set->sig[signo_word_idx(signo)], signo_word_bit(signo), 0); + + return 0; +} diff --git a/tests/posix/common/src/signal.c b/tests/posix/common/src/signal.c index 043237a105057d1..ade91d2e03f371e 100644 --- a/tests/posix/common/src/signal.c +++ b/tests/posix/common/src/signal.c @@ -114,3 +114,89 @@ ZTEST(posix_apis, test_signal_addset) set.sig[i], sig_mask[i]); } } + +ZTEST(posix_apis, test_signal_delset_oor) +{ + int rc; + sigset_t set; + + rc = sigdelset(&set, -1); + zassert_equal(rc, -1, "rc should be -1"); + zassert_equal(errno, EINVAL, "errno should be %s", "EINVAL"); + + rc = sigdelset(&set, 0); + zassert_equal(rc, -1, "rc should be -1"); + zassert_equal(errno, EINVAL, "errno should be %s", "EINVAL"); + + rc = sigdelset(&set, _NSIG + 1); + zassert_equal(rc, -1, "rc should be -1"); + zassert_equal(errno, EINVAL, "errno should be %s", "EINVAL"); +} + +#ifdef CONFIG_64BIT +ZTEST(posix_apis, test_signal_delset_64) +{ + sigset_t set; + unsigned long sig_mask = -1; + + memset(&set, -1, sizeof(set)); + + WRITE_BIT(sig_mask, SIGHUP - 1, 0); + zassert_ok(sigdelset(&set, SIGHUP)); + zassert_equal(set.sig[0], sig_mask, "set.sig[%d of %d] has content: %lx, expected %lx", 0, + ARRAY_SIZE(set.sig), set.sig[0], sig_mask); + + WRITE_BIT(sig_mask, SIGSYS - 1, 0); + zassert_ok(sigdelset(&set, SIGSYS)); + zassert_equal(set.sig[0], sig_mask, "set.sig[%d of %d] has content: %lx, expected %lx", 0, + ARRAY_SIZE(set.sig), set.sig[0], sig_mask); + + WRITE_BIT(sig_mask, SIGRTMIN - 1, 0); + zassert_ok(sigdelset(&set, SIGRTMIN)); + zassert_equal(set.sig[0], sig_mask, "set.sig[%d of %d] has content: %lx, expected %lx", 0, + ARRAY_SIZE(set.sig), set.sig[0], sig_mask); + + WRITE_BIT(sig_mask, SIGRTMIN, 0); + zassert_ok(sigdelset(&set, SIGRTMIN + 1)); + zassert_equal(set.sig[0], sig_mask, "set.sig[%d of %d] has content: %lx, expected %lx", 0, + ARRAY_SIZE(set.sig), set.sig[0], sig_mask); + + WRITE_BIT(sig_mask, SIGRTMAX - 1, 0); + zassert_ok(sigdelset(&set, SIGRTMAX)); + zassert_equal(set.sig[0], sig_mask, "set.sig[%d of %d] has content: %lx, expected %lx", 0, + ARRAY_SIZE(set.sig), set.sig[0], sig_mask); +} +#else /* 32BIT */ +ZTEST(posix_apis, test_signal_delset_32) +{ + sigset_t set; + unsigned long sig_mask[2] = {-1, -1}; + + memset(&set, -1, sizeof(set)); + + WRITE_BIT(sig_mask[0], SIGHUP - 1, 0); + zassert_ok(sigdelset(&set, SIGHUP)); + zassert_equal(set.sig[0], sig_mask[0], "set.sig[%d of %d] has content: %lx, expected %lx", + 0, ARRAY_SIZE(set.sig), set.sig[0], sig_mask[0]); + + WRITE_BIT(sig_mask[0], SIGSYS - 1, 0); + zassert_ok(sigdelset(&set, SIGSYS)); + zassert_equal(set.sig[0], sig_mask[0], "set.sig[%d of %d] has content: %lx, expected %lx", + 0, ARRAY_SIZE(set.sig), set.sig[0], sig_mask[0]); + + WRITE_BIT(sig_mask[0], SIGRTMIN - 1, 0); + zassert_ok(sigdelset(&set, SIGRTMIN)); + zassert_equal(set.sig[0], sig_mask[0], "set.sig[%d of %d] has content: %lx, expected %lx", + 0, ARRAY_SIZE(set.sig), set.sig[0], sig_mask[0]); + + WRITE_BIT(sig_mask[1], SIGRTMIN % BITS_PER_LONG, 0); + zassert_ok(sigdelset(&set, SIGRTMIN + 1)); + zassert_equal(set.sig[1], sig_mask[1], "set.sig[%d of %d] has content: %lx, expected %lx", + 1, ARRAY_SIZE(set.sig), set.sig[1], sig_mask[1]); + + WRITE_BIT(sig_mask[1], (SIGRTMAX - 1) % BITS_PER_LONG, 0); + zassert_ok(sigdelset(&set, SIGRTMAX)); + zassert_equal(set.sig[1], sig_mask[1], "set.sig[%d of %d] has content: %lx, expected %lx", + 1, ARRAY_SIZE(set.sig), set.sig[1], sig_mask[1]); +} +#endif /* CONFIG_64BIT */