Skip to content

Commit c8e22ec

Browse files
committed
tests: posix: signals: adjust expectations for real-time signals
1. Fix an off-by-one error in `signo_fits()` (a utility function inside signal.c 2. Adjust expectations for C libraries that do not provide a sigset_t large enough to support `SIGRTMIN` or `SIGRAMAX`. This fixes an issue in CI that appeared when run under ARC. ```shell west build -p auto -b qemu_arc/qemu_arc_hs -t run tests/posix/signals ``` Signed-off-by: Chris Friedt <[email protected]>
1 parent 6862856 commit c8e22ec

File tree

2 files changed

+67
-63
lines changed

2 files changed

+67
-63
lines changed

lib/posix/options/signal.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ static inline bool signo_is_rt(int signo)
2929

3030
static inline bool signo_fits(int signo)
3131
{
32-
return ((signo > 0) && (signo <= SIGSET_NLONGS * BITS_PER_LONG));
32+
/* technically, 0 is not a valid signal number, but it still fits */
33+
return ((signo >= 0) && (signo < SIGSET_NLONGS * BITS_PER_LONG));
3334
}
3435

3536
#undef sigemptyset

tests/posix/signals/src/main.c

Lines changed: 65 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#define SIGNO_WORD_BIT(_signo) (_signo & BIT_MASK(LOG2(BITS_PER_LONG)))
1717

1818
#define SIGSET_NLONGS (sizeof(sigset_t) / sizeof(unsigned long))
19+
BUILD_ASSERT(SIGSET_NLONGS > 0, "sigset_t has no storage");
1920

2021
ZTEST(posix_signals, test_sigemptyset)
2122
{
@@ -85,31 +86,30 @@ ZTEST(posix_signals, test_sigaddset)
8586
SIGSET_NLONGS - 1, _set[i], _target[i]);
8687
}
8788

88-
signo = SIGRTMIN; /* >=32, will be in the second sig set for 32bit */
89-
zassert_ok(sigaddset(&set, signo));
90-
#ifdef CONFIG_64BIT
91-
WRITE_BIT(_target[0], signo, 1);
92-
#else /* 32BIT */
93-
WRITE_BIT(_target[1], (signo)-BITS_PER_LONG, 1);
94-
#endif
95-
for (int i = 0; i < SIGSET_NLONGS; i++) {
96-
zassert_equal(_set[i], _target[i],
97-
"set.sig[%d of %d] has content: %lx, expected %lx", i,
98-
SIGSET_NLONGS - 1, _set[i], _target[i]);
99-
}
89+
/* TODO: move rt signal tests to realtime_signals testsuite */
90+
static const int rtsigs[] = {SIGRTMIN, SIGRTMAX};
10091

101-
if (SIGRTMAX >= SIGSET_NLONGS * BITS_PER_LONG) {
102-
/* Some libc's use a sigset_t that is too small for real-time signals */
103-
return;
104-
}
92+
ARRAY_FOR_EACH(rtsigs, i) {
93+
int expected_ret = 0;
94+
int expected_errno = 0;
10595

106-
signo = SIGRTMAX;
107-
zassert_ok(sigaddset(&set, signo));
108-
WRITE_BIT(_target[signo / BITS_PER_LONG], signo % BITS_PER_LONG, 1);
109-
for (int i = 0; i < SIGSET_NLONGS; i++) {
110-
zassert_equal(_set[i], _target[i],
111-
"set.sig[%d of %d] has content: %lx, expected %lx", i,
112-
SIGSET_NLONGS - 1, _set[i], _target[i]);
96+
signo = rtsigs[i];
97+
if (signo >= SIGSET_NLONGS * BITS_PER_LONG) {
98+
/* Some libc's provide a sigset_t that is too small for real-time signals */
99+
expected_ret = -1;
100+
expected_errno = EINVAL;
101+
} else {
102+
WRITE_BIT(_target[signo / BITS_PER_LONG], signo % BITS_PER_LONG, 1);
103+
}
104+
105+
errno = 0;
106+
zassert_equal(sigaddset(&set, signo), expected_ret);
107+
zassert_equal(errno, expected_errno);
108+
for (int i = 0; i < SIGSET_NLONGS; i++) {
109+
zassert_equal(_set[i], _target[i],
110+
"set.sig[%d of %d] has content: %lx, expected %lx", i,
111+
SIGSET_NLONGS - 1, _set[i], _target[i]);
112+
}
113113
}
114114
}
115115

@@ -135,6 +135,9 @@ ZTEST(posix_signals, test_sigdelset)
135135
sigset_t target = (sigset_t){0};
136136
unsigned long *const _target = (unsigned long *)&target;
137137

138+
zassert_ok(sigfillset(&set));
139+
zassert_ok(sigfillset(&target));
140+
138141
signo = SIGHUP;
139142
zassert_ok(sigdelset(&set, signo));
140143
WRITE_BIT(_target[0], signo, 0);
@@ -153,36 +156,30 @@ ZTEST(posix_signals, test_sigdelset)
153156
SIGSET_NLONGS - 1, _set[i], _target[i]);
154157
}
155158

156-
signo = SIGRTMIN; /* >=32, will be in the second sig set for 32bit */
157-
zassert_ok(sigdelset(&set, signo));
158-
#ifdef CONFIG_64BIT
159-
WRITE_BIT(_target[0], signo, 0);
160-
#else /* 32BIT */
161-
WRITE_BIT(_target[1], (signo)-BITS_PER_LONG, 0);
162-
#endif
163-
for (int i = 0; i < SIGSET_NLONGS; i++) {
164-
zassert_equal(_set[i], _target[i],
165-
"set.sig[%d of %d] has content: %lx, expected %lx", i,
166-
SIGSET_NLONGS - 1, _set[i], _target[i]);
167-
}
159+
/* TODO: move rt signal tests to realtime_signals testsuite */
160+
static const int rtsigs[] = {SIGRTMIN, SIGRTMAX};
168161

169-
if (SIGRTMAX >= SIGSET_NLONGS * BITS_PER_LONG) {
170-
/* For libc's that use a sigset_t that is too small */
171-
return;
172-
}
162+
ARRAY_FOR_EACH(rtsigs, i) {
163+
int expected_ret = 0;
164+
int expected_errno = 0;
173165

174-
if (SIGRTMAX >= SIGSET_NLONGS * BITS_PER_LONG) {
175-
/* Some libc's use a sigset_t that is too small for real-time signals */
176-
return;
177-
}
166+
signo = rtsigs[i];
167+
if (signo >= SIGSET_NLONGS * BITS_PER_LONG) {
168+
/* Some libc's provide a sigset_t that is too small for real-time signals */
169+
expected_ret = -1;
170+
expected_errno = EINVAL;
171+
} else {
172+
WRITE_BIT(_target[signo / BITS_PER_LONG], signo % BITS_PER_LONG, 0);
173+
}
178174

179-
signo = SIGRTMAX;
180-
zassert_ok(sigdelset(&set, signo));
181-
WRITE_BIT(_target[signo / BITS_PER_LONG], signo % BITS_PER_LONG, 0);
182-
for (int i = 0; i < SIGSET_NLONGS; i++) {
183-
zassert_equal(_set[i], _target[i],
184-
"set.sig[%d of %d] has content: %lx, expected %lx", i,
185-
SIGSET_NLONGS - 1, _set[i], _target[i]);
175+
errno = 0;
176+
zassert_equal(sigdelset(&set, signo), expected_ret);
177+
zassert_equal(errno, expected_errno);
178+
for (int i = 0; i < SIGSET_NLONGS; i++) {
179+
zassert_equal(_set[i], _target[i],
180+
"set.sig[%d of %d] has content: %lx, expected %lx", i,
181+
SIGSET_NLONGS - 1, _set[i], _target[i]);
182+
}
186183
}
187184
}
188185

@@ -217,20 +214,26 @@ ZTEST(posix_signals, test_sigismember)
217214
zassert_equal(sigismember(&set, SIGKILL), 0, "%s not expected to be member", "SIGKILL");
218215
zassert_equal(sigismember(&set, SIGTERM), 0, "%s not expected to be member", "SIGTERM");
219216

220-
if (SIGRTMAX >= SIGSET_NLONGS * BITS_PER_LONG) {
221-
/* Some libc's use a sigset_t that is too small for real-time signals */
222-
return;
223-
}
217+
/* TODO: move rt signal tests to realtime_signals testsuite */
218+
static const int rtsigs[] = {SIGRTMIN, SIGRTMAX};
224219

225-
#ifdef CONFIG_64BIT
226-
_set[0] |= BIT(SIGRTMIN);
227-
#else /* 32BIT */
228-
_set[1] = BIT((SIGRTMIN)-BITS_PER_LONG);
229-
#endif
220+
ARRAY_FOR_EACH(rtsigs, i) {
221+
int expected_ret = 1;
222+
int expected_errno = 0;
223+
int signo = rtsigs[i];
230224

231-
WRITE_BIT(_set[SIGRTMAX / BITS_PER_LONG], SIGRTMAX % BITS_PER_LONG, 1);
232-
zassert_equal(sigismember(&set, SIGRTMIN), 1, "%s expected to be member", "SIGRTMIN");
233-
zassert_equal(sigismember(&set, SIGRTMAX), 1, "%s expected to be member", "SIGRTMAX");
225+
if (signo >= SIGSET_NLONGS * BITS_PER_LONG) {
226+
/* Some libc's provide a sigset_t that is too small for real-time signals */
227+
expected_ret = -1;
228+
expected_errno = EINVAL;
229+
} else {
230+
WRITE_BIT(_set[signo / BITS_PER_LONG], signo % BITS_PER_LONG, 1);
231+
}
232+
233+
errno = 0;
234+
zassert_equal(sigismember(&set, signo), expected_ret);
235+
zassert_equal(errno, expected_errno);
236+
}
234237
}
235238

236239
ZTEST(posix_signals, test_signal_strsignal)

0 commit comments

Comments
 (0)