Skip to content

Commit 09a59b9

Browse files
Swatantra-66xiaoxiang781216
authored andcommitted
testing/ostest: Add pthread_sigqueue test
Add automated unit test for pthread_sigqueue() in ostest to verify signal and payload delivery to a target pthread. Tests error handling for invalid signal numbers and non-existent thread IDs, as well as successful signal dispatch and reception with SA_SIGINFO and SI_QUEUE. Now that pthread_sigqueue() is implemented as a system call (apache/nuttx#21070), the test runs across flat, protected, and kernel builds. Configurable via CONFIG_TESTING_OSTEST_PTHREAD_SIGQUEUE, enabled by default for non-small builds (!DEFAULT_SMALL) and gated by ENABLE_ALL_SIGNALS and !DISABLE_PTHREAD. Progress output and error messages are streamlined to minimize the flash footprint. Signed-off-by: Swatantra Yadav <maverickswatantra@gmail.com>
1 parent c2b787b commit 09a59b9

6 files changed

Lines changed: 306 additions & 0 deletions

File tree

‎testing/ostest/CMakeLists.txt‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ if(CONFIG_TESTING_OSTEST)
9393
list(APPEND SRCS timedwait.c pthread_rwlock.c pthread_rwlock_cancel.c
9494
schedlock.c)
9595

96+
if(CONFIG_TESTING_OSTEST_PTHREAD_SIGQUEUE)
97+
list(APPEND SRCS pthread_sigqueue.c)
98+
endif()
99+
96100
if(CONFIG_SCHED_WAITPID)
97101
list(APPEND SRCS pthread_exit.c)
98102
endif()

‎testing/ostest/Kconfig‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,14 @@ config TESTING_OSTEST_MULTIUSER
153153
CONFIG_LIBC_PASSWD_FILEPATH (default /tmp/ostest_passwd when this
154154
option is enabled).
155155

156+
config TESTING_OSTEST_PTHREAD_SIGQUEUE
157+
bool "pthread_sigqueue test"
158+
default !DEFAULT_SMALL
159+
depends on ENABLE_ALL_SIGNALS && !DISABLE_PTHREAD
160+
---help---
161+
Enable automated unit test for pthread_sigqueue() in ostest to
162+
verify signal and payload delivery to a target pthread.
163+
156164
config TESTING_LOOP_SCALE
157165
int "Loop scale of spinlock test (N x 10,000)"
158166
default 5 if MM_KASAN

‎testing/ostest/Makefile‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ endif
9999
ifneq ($(CONFIG_DISABLE_PTHREAD),y)
100100
CSRCS += cancel.c cond.c mutex.c timedmutex.c sem.c semtimed.c barrier.c
101101
CSRCS += timedwait.c pthread_rwlock.c pthread_rwlock_cancel.c schedlock.c
102+
ifeq ($(CONFIG_TESTING_OSTEST_PTHREAD_SIGQUEUE),y)
103+
CSRCS += pthread_sigqueue.c
104+
endif
102105

103106
ifeq ($(CONFIG_SCHED_WAITPID),y)
104107
CSRCS += pthread_exit.c

‎testing/ostest/ostest.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,12 @@ void pthread_exit_test(void);
288288

289289
void pthread_cleanup_test(void);
290290

291+
/* pthread_sigqueue.c *******************************************************/
292+
293+
#ifdef CONFIG_TESTING_OSTEST_PTHREAD_SIGQUEUE
294+
void pthread_sigqueue_test(void);
295+
#endif
296+
291297
/* barrier.c ****************************************************************/
292298

293299
void barrier_test(void);

‎testing/ostest/ostest_main.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,14 @@ static int user_main(int argc, char *argv[])
554554
check_test_memory_usage();
555555
#endif
556556

557+
#ifdef CONFIG_TESTING_OSTEST_PTHREAD_SIGQUEUE
558+
/* Verify pthread_sigqueue interface */
559+
560+
printf("\nuser_main: pthread_sigqueue test\n");
561+
pthread_sigqueue_test();
562+
check_test_memory_usage();
563+
#endif
564+
557565
/* Verify pthreads and condition variable timed waits */
558566

559567
printf("\nuser_main: timed wait test\n");

‎testing/ostest/pthread_sigqueue.c‎

Lines changed: 277 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,277 @@
1+
/****************************************************************************
2+
* apps/testing/ostest/pthread_sigqueue.c
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Licensed to the Apache Software Foundation (ASF) under one or more
7+
* contributor license agreements. See the NOTICE file distributed with
8+
* this work for additional information regarding copyright ownership. The
9+
* ASF licenses this file to you under the Apache License, Version 2.0 (the
10+
* "License"); you may not use this file except in compliance with the
11+
* License. You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18+
* License for the specific language governing permissions and limitations
19+
* under the License.
20+
*
21+
****************************************************************************/
22+
23+
/****************************************************************************
24+
* Included Files
25+
****************************************************************************/
26+
27+
#include <nuttx/config.h>
28+
29+
#include <assert.h>
30+
#include <errno.h>
31+
#include <pthread.h>
32+
#include <semaphore.h>
33+
#include <signal.h>
34+
#include <stdbool.h>
35+
#include <stdio.h>
36+
#include <unistd.h>
37+
38+
#include "ostest.h"
39+
40+
/****************************************************************************
41+
* Pre-processor Definitions
42+
****************************************************************************/
43+
44+
#define TEST_SIGNO SIGUSR1
45+
#define TEST_VAL_INT 42
46+
47+
/****************************************************************************
48+
* Private Data
49+
****************************************************************************/
50+
51+
static sem_t g_ready_sem;
52+
static sem_t g_done_sem;
53+
static struct sigaction g_oact;
54+
static bool g_act_installed;
55+
static volatile bool g_sigreceived;
56+
static volatile int g_received_signo;
57+
static volatile int g_received_code;
58+
static volatile int g_received_val;
59+
60+
/****************************************************************************
61+
* Private Functions
62+
****************************************************************************/
63+
64+
/****************************************************************************
65+
* Name: sigqueue_handler
66+
****************************************************************************/
67+
68+
static void sigqueue_handler(int signo, FAR siginfo_t *info,
69+
FAR void *ucontext)
70+
{
71+
if (info != NULL)
72+
{
73+
g_received_signo = signo;
74+
g_received_code = info->si_code;
75+
g_received_val = info->si_value.sival_int;
76+
g_sigreceived = true;
77+
}
78+
79+
sem_post(&g_done_sem);
80+
}
81+
82+
/****************************************************************************
83+
* Name: sigqueue_thread
84+
****************************************************************************/
85+
86+
static FAR void *sigqueue_thread(FAR void *arg)
87+
{
88+
struct sigaction act;
89+
sigset_t set;
90+
int status;
91+
92+
/* Setup signal action */
93+
94+
act.sa_sigaction = sigqueue_handler;
95+
act.sa_flags = SA_SIGINFO;
96+
sigemptyset(&act.sa_mask);
97+
98+
status = sigaction(TEST_SIGNO, &act, &g_oact);
99+
if (status != OK)
100+
{
101+
printf("sigqueue_thread: sigaction failed, errno=%d\n", errno);
102+
ASSERT(false);
103+
return (FAR void *)ERROR;
104+
}
105+
106+
g_act_installed = true;
107+
108+
/* Unblock test signal */
109+
110+
sigemptyset(&set);
111+
sigaddset(&set, TEST_SIGNO);
112+
status = pthread_sigmask(SIG_UNBLOCK, &set, NULL);
113+
if (status != OK)
114+
{
115+
printf("sigqueue_thread: pthread_sigmask failed: %d\n", status);
116+
ASSERT(false);
117+
return (FAR void *)ERROR;
118+
}
119+
120+
/* Notify sender that we are ready */
121+
122+
sem_post(&g_ready_sem);
123+
124+
/* Wait for signal delivery */
125+
126+
while (sem_wait(&g_done_sem) < 0)
127+
{
128+
if (errno != EINTR)
129+
{
130+
printf("sigqueue_thread: sem_wait failed: %d\n", errno);
131+
ASSERT(false);
132+
return (FAR void *)ERROR;
133+
}
134+
}
135+
136+
return NULL;
137+
}
138+
139+
/****************************************************************************
140+
* Public Functions
141+
****************************************************************************/
142+
143+
/****************************************************************************
144+
* Name: pthread_sigqueue_test
145+
****************************************************************************/
146+
147+
void pthread_sigqueue_test(void)
148+
{
149+
pthread_t thread;
150+
union sigval val;
151+
int status;
152+
153+
sem_init(&g_ready_sem, 0, 0);
154+
sem_init(&g_done_sem, 0, 0);
155+
156+
g_sigreceived = false;
157+
g_received_signo = -1;
158+
g_received_code = -1;
159+
g_received_val = -1;
160+
g_act_installed = false;
161+
162+
/* Test 1: Invalid signal number error handling */
163+
164+
val.sival_int = TEST_VAL_INT;
165+
status = pthread_sigqueue(pthread_self(), -1, val);
166+
if (status != EINVAL)
167+
{
168+
printf("pthread_sigqueue_test: signo err: %d\n", status);
169+
ASSERT(false);
170+
}
171+
172+
/* Test 2: Invalid thread ID error handling */
173+
174+
status = pthread_sigqueue((pthread_t)-1, TEST_SIGNO, val);
175+
if (status != ESRCH)
176+
{
177+
printf("pthread_sigqueue_test: thread err: %d\n", status);
178+
ASSERT(false);
179+
}
180+
181+
/* Test 3: Normal signal queuing to thread */
182+
183+
status = pthread_create(&thread, NULL, sigqueue_thread, NULL);
184+
if (status != OK)
185+
{
186+
printf("pthread_sigqueue_test: pthread_create: %d\n", status);
187+
ASSERT(false);
188+
goto errout;
189+
}
190+
191+
/* Wait until target thread has installed the handler and unblocked */
192+
193+
while (sem_wait(&g_ready_sem) < 0)
194+
{
195+
if (errno != EINTR)
196+
{
197+
printf("pthread_sigqueue_test: sem_wait: %d\n", errno);
198+
ASSERT(false);
199+
goto errout;
200+
}
201+
}
202+
203+
/* Send signal with payload to target thread */
204+
205+
status = pthread_sigqueue(thread, TEST_SIGNO, val);
206+
if (status != OK)
207+
{
208+
printf("pthread_sigqueue_test: pthread_sigqueue: %d\n", status);
209+
ASSERT(false);
210+
goto errout;
211+
}
212+
213+
/* Join target thread */
214+
215+
status = pthread_join(thread, NULL);
216+
if (status != OK)
217+
{
218+
printf("pthread_sigqueue_test: pthread_join: %d\n", status);
219+
ASSERT(false);
220+
goto errout;
221+
}
222+
223+
/* Restore signal action */
224+
225+
if (g_act_installed)
226+
{
227+
status = sigaction(TEST_SIGNO, &g_oact, NULL);
228+
if (status != OK)
229+
{
230+
printf("pthread_sigqueue_test: sigaction restore: %d\n", errno);
231+
ASSERT(false);
232+
}
233+
234+
g_act_installed = false;
235+
}
236+
237+
/* Verify received signal properties */
238+
239+
if (!g_sigreceived)
240+
{
241+
printf("pthread_sigqueue_test: signal not received\n");
242+
ASSERT(false);
243+
}
244+
245+
if (g_received_signo != TEST_SIGNO)
246+
{
247+
printf("pthread_sigqueue_test: signo mismatch: %d\n",
248+
g_received_signo);
249+
ASSERT(false);
250+
}
251+
252+
if (g_received_code != SI_QUEUE)
253+
{
254+
printf("pthread_sigqueue_test: si_code mismatch: %d\n",
255+
g_received_code);
256+
ASSERT(false);
257+
}
258+
259+
if (g_received_val != TEST_VAL_INT)
260+
{
261+
printf("pthread_sigqueue_test: val mismatch: %d\n",
262+
g_received_val);
263+
ASSERT(false);
264+
}
265+
266+
printf("pthread_sigqueue_test: SUCCESS\n");
267+
268+
errout:
269+
if (g_act_installed)
270+
{
271+
sigaction(TEST_SIGNO, &g_oact, NULL);
272+
g_act_installed = false;
273+
}
274+
275+
sem_destroy(&g_ready_sem);
276+
sem_destroy(&g_done_sem);
277+
}

0 commit comments

Comments
 (0)