Skip to content

Commit

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

Signed-off-by: Yong Cong Sin <[email protected]>
ycsin committed Jul 6, 2023
1 parent 68d8995 commit 0b7f186
Showing 3 changed files with 112 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
@@ -61,6 +61,7 @@ typedef struct {

int sigemptyset(sigset_t *set);
int sigfillset(sigset_t *set);
int sigaddset(sigset_t *set, int signo);
#endif /* CONFIG_POSIX_SIGNAL */

#ifndef SIGEV_NONE
28 changes: 28 additions & 0 deletions lib/posix/signal.c
Original file line number Diff line number Diff line change
@@ -4,10 +4,26 @@
* SPDX-License-Identifier: Apache-2.0
*/

#include <errno.h>
#include <string.h>

#include <zephyr/posix/signal.h>

static inline int signo_word_idx(int signo)
{
return (signo - 1) / _NSIG_BPW;
}

static inline int signo_word_bit(int signo)
{
return (signo - 1) % _NSIG_BPW;
}

static inline bool signo_invalid(int signo)
{
return ((signo <= 0) || (signo > _NSIG)) ? true : false;
}

int sigemptyset(sigset_t *set)
{
memset(set, 0, sizeof(*set));
@@ -21,3 +37,15 @@ int sigfillset(sigset_t *set)

return 0;
}

int sigaddset(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), 1);

return 0;
}
83 changes: 83 additions & 0 deletions tests/posix/common/src/signal.c
Original file line number Diff line number Diff line change
@@ -5,7 +5,9 @@
*/

#include <zephyr/ztest.h>
#include <zephyr/sys/util.h>

#include <errno.h>
#include <signal.h>

ZTEST(posix_apis, test_signal_emptyset)
@@ -31,3 +33,84 @@ ZTEST(posix_apis, test_signal_fillset)
zassert_equal(set.sig[i], -1, "set.sig[%d] is not filled: 0x%lx", i, set.sig[i]);
}
}

ZTEST(posix_apis, test_signal_addset_oor)
{
int rc;
sigset_t set = {0};

rc = sigaddset(&set, -1);
zassert_equal(rc, -1, "rc should be -1");
zassert_equal(errno, EINVAL, "errno should be %s", "EINVAL");

rc = sigaddset(&set, 0);
zassert_equal(rc, -1, "rc should be -1");
zassert_equal(errno, EINVAL, "errno should be %s", "EINVAL");

rc = sigaddset(&set, _NSIG + 1);
zassert_equal(rc, -1, "rc should be -1");
zassert_equal(errno, EINVAL, "errno should be %s", "EINVAL");
}

#define SIG_WD_IDX(_signo) ((int)((_signo - 1) / BITS_PER_LONG))
#define SIG_MSK_SET(_signo) (WRITE_BIT(sig_mask[SIG_WD_IDX(_signo)], (_signo - 1) % _NSIG_BPW, 1))
ZTEST(posix_apis, test_signal_addset)
{
int signo;
sigset_t set = {0};
unsigned long sig_mask[_NSIG_WORDS] = {0};

signo = SIGHUP;
SIG_MSK_SET(signo);
zassert_ok(sigaddset(&set, signo));
for (int i = 0; i < _NSIG_WORDS; i++) {
zassert_equal(set.sig[i], sig_mask[i],
"set.sig[%d of %d] has content: %lx, expected %lx", i, _NSIG_WORDS,
set.sig[i], sig_mask[i]);
}

signo = SIGKILL;
SIG_MSK_SET(signo);
zassert_ok(sigaddset(&set, signo));
for (int i = 0; i < _NSIG_WORDS; i++) {
zassert_equal(set.sig[i], sig_mask[i],
"set.sig[%d of %d] has content: %lx, expected %lx", i, _NSIG_WORDS,
set.sig[i], sig_mask[i]);
}

signo = SIGSYS;
SIG_MSK_SET(signo);
zassert_ok(sigaddset(&set, signo));
for (int i = 0; i < _NSIG_WORDS; i++) {
zassert_equal(set.sig[i], sig_mask[i],
"set.sig[%d of %d] has content: %lx, expected %lx", i, _NSIG_WORDS,
set.sig[i], sig_mask[i]);
}

signo = SIGRTMIN;
SIG_MSK_SET(signo);
zassert_ok(sigaddset(&set, signo));
for (int i = 0; i < _NSIG_WORDS; i++) {
zassert_equal(set.sig[i], sig_mask[i],
"set.sig[%d of %d] has content: %lx, expected %lx", i, _NSIG_WORDS,
set.sig[i], sig_mask[i]);
}

signo = SIGRTMIN + 1;
SIG_MSK_SET(signo);
zassert_ok(sigaddset(&set, signo));
for (int i = 0; i < _NSIG_WORDS; i++) {
zassert_equal(set.sig[i], sig_mask[i],
"set.sig[%d of %d] has content: %lx, expected %lx", i, _NSIG_WORDS,
set.sig[i], sig_mask[i]);
}

signo = SIGRTMAX;
SIG_MSK_SET(signo);
zassert_ok(sigaddset(&set, signo));
for (int i = 0; i < _NSIG_WORDS; i++) {
zassert_equal(set.sig[i], sig_mask[i],
"set.sig[%d of %d] has content: %lx, expected %lx", i, _NSIG_WORDS,
set.sig[i], sig_mask[i]);
}
}

0 comments on commit 0b7f186

Please sign in to comment.