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 3507cdce6748de3..35310c747754d91 100644 --- a/lib/posix/signal.c +++ b/lib/posix/signal.c @@ -61,3 +61,13 @@ int sigdelset(sigset_t *set, int signo) return 0; } + +int sigismember(const sigset_t *set, int signo) +{ + if (signo_invalid(signo)) { + errno = EINVAL; + return -1; + } + + return 1 & (set->sig[signo_word_idx(signo)] >> signo_word_bit(signo)); +} diff --git a/tests/posix/common/src/signal.c b/tests/posix/common/src/signal.c index ade91d2e03f371e..876bae50f5d64c7 100644 --- a/tests/posix/common/src/signal.c +++ b/tests/posix/common/src/signal.c @@ -200,3 +200,42 @@ ZTEST(posix_apis, test_signal_delset_32) 1, ARRAY_SIZE(set.sig), set.sig[1], sig_mask[1]); } #endif /* CONFIG_64BIT */ + +ZTEST(posix_apis, test_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_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"); +}