diff --git a/include/zephyr/posix/signal.h b/include/zephyr/posix/signal.h index 0da478b02e4c685..6a526a51cd95295 100644 --- a/include/zephyr/posix/signal.h +++ b/include/zephyr/posix/signal.h @@ -63,6 +63,7 @@ int sigemptyset(sigset_t *set); int sigfillset(sigset_t *set); int sigaddset(sigset_t *set, int signo); int sigdelset(sigset_t *set, int signo); +int sigismember(const 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 9fe81b8b4604c19..935a826fcfdcb19 100644 --- a/lib/posix/signal.c +++ b/lib/posix/signal.c @@ -80,3 +80,20 @@ int sigdelset(sigset_t *set, int signo) return 0; } + +int sigismember(const sigset_t *set, int signo) +{ + unsigned long sig; + + if ((signo <= 0) || (signo > _NSIG)) { + errno = EINVAL; + return -1; + } + + sig = signo - 1; + if (_NSIG_WORDS == 1) { + return 1 & (set->sig[0] >> sig); + } else { + return 1 & (set->sig[sig / _NSIG_BPW] >> (sig % _NSIG_BPW)); + } +} diff --git a/tests/posix/common/src/signal.c b/tests/posix/common/src/signal.c index 759e3062758b618..bcac72a31603a68 100644 --- a/tests/posix/common/src/signal.c +++ b/tests/posix/common/src/signal.c @@ -200,3 +200,42 @@ ZTEST(posix_apis, test_posix_signal_delset_32) 1, ARRAY_SIZE(set.sig), set.sig[1], sig_mask[1]); } #endif /* CONFIG_64BIT */ + +ZTEST(posix_apis, test_posix_signal_ismember_oor) +{ + int rc; + sigset_t set = {0}; + + rc = sigismember(&set, -1); + zassert_equal(rc, -1, "rc should be -1"); + zassert_equal(errno, EINVAL, "errno should be %s", "EINVAL"); + + rc = sigismember(&set, 0); + zassert_equal(rc, -1, "rc should be -1"); + zassert_equal(errno, EINVAL, "errno should be %s", "EINVAL"); + + rc = sigismember(&set, _NSIG + 1); + zassert_equal(rc, -1, "rc should be -1"); + zassert_equal(errno, EINVAL, "errno should be %s", "EINVAL"); +} + +ZTEST(posix_apis, test_posix_signal_ismember) +{ + sigset_t set; + +#ifdef CONFIG_64BIT + set.sig[0] = BIT(SIGHUP - 1) | BIT(SIGRTMIN - 1) | BIT(SIGRTMIN) | BIT(SIGRTMAX - 1); +#else /* 32BIT */ + set.sig[0] = BIT(SIGHUP - 1) | BIT(SIGRTMIN - 1); + set.sig[1] = BIT(SIGRTMIN % BITS_PER_LONG) | BIT((SIGRTMAX - 1) % BITS_PER_LONG); +#endif + + zassert_equal(sigismember(&set, SIGHUP), 1, "%s expected to be member", "SIGHUP"); + zassert_equal(sigismember(&set, SIGRTMIN), 1, "%s expected to be member", "SIGRTMIN"); + zassert_equal(sigismember(&set, SIGRTMIN + 1), 1, "%s expected to be member", + "SIGRTMIN + 1"); + zassert_equal(sigismember(&set, SIGRTMAX), 1, "%s expected to be member", "SIGRTMAX"); + + zassert_equal(sigismember(&set, SIGKILL), 0, "%s not expected to be member", "SIGKILL"); + zassert_equal(sigismember(&set, SIGTERM), 0, "%s not expected to be member", "SIGTERM"); +}