Skip to content

Commit

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

Signed-off-by: Yong Cong Sin <[email protected]>
  • Loading branch information
ycsin committed Jul 6, 2023
1 parent 0b7f186 commit 400cdcd
Show file tree
Hide file tree
Showing 3 changed files with 99 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 @@ -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
Expand Down
12 changes: 12 additions & 0 deletions lib/posix/signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
86 changes: 86 additions & 0 deletions tests/posix/common/src/signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

0 comments on commit 400cdcd

Please sign in to comment.