Skip to content

Commit

Permalink
posix: signal: implement sigismember
Browse files Browse the repository at this point in the history
Implementation and ztest for sigismember.

Signed-off-by: Yong Cong Sin <[email protected]
  • Loading branch information
ycsin committed Jul 6, 2023
1 parent e93efc6 commit a70b15f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/zephyr/posix/signal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions lib/posix/signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
39 changes: 39 additions & 0 deletions tests/posix/common/src/signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

0 comments on commit a70b15f

Please sign in to comment.