Skip to content

Commit ba07574

Browse files
fxysunshinepussuw
authored andcommitted
ostest: Add initial support for CONFIG_BUILD_KERNEL
task_* APIs are unavailable in kernel build mode, replacing with posix_spawn or pthread_* API to pass the case. But posix_spawn requires some dependency, for example CONFIG_BUILTIN and CONFIG_LIBC_EXECFUNCS, so pthread_* APIs are used in most scenarios. Some tests should be re-visited because the intent is to user another task (in this case another process) to e.g. receive signals. That will require quite a bit of extra work. Tests that had to be disabled: - restart: task_restart() does not work at all with kernel mode so it is disabled entirely - fpu: make sure the FPU test is not even attempted, because it will cause ASSERT() and stop the test - vfork: vfork() does not work for some reason in CONFIG_BUILD_KERNEL, there is something missing on the kernel side, so just disable the test for now Tests that should be re-visited: - The signal tests, now they signal the process itself while before the signal was sent to another task. This will require building the part that receives the signal as a separate process - waitpid: Like stated above, waitpid does not work for pthreads - suspend: kill to send signal does not work for pthreads Signed-off-by: fangxinyong <[email protected]> Co-authored-by: Ville Juven <[email protected]>
1 parent f12586c commit ba07574

File tree

8 files changed

+116
-38
lines changed

8 files changed

+116
-38
lines changed

testing/ostest/ostest.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@ void aio_test(void);
115115

116116
/* restart.c ****************************************************************/
117117

118+
#ifndef CONFIG_BUILD_KERNEL
118119
void restart_test(void);
120+
#endif
119121

120122
/* waitpid.c ****************************************************************/
121123

testing/ostest/ostest_main.c

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -339,21 +339,24 @@ static int user_main(int argc, char *argv[])
339339
check_test_memory_usage();
340340
#endif
341341

342-
#if defined(CONFIG_ARCH_FPU) && !defined(CONFIG_TESTING_OSTEST_FPUTESTDISABLE)
342+
#if defined(CONFIG_ARCH_FPU) && !defined(CONFIG_TESTING_OSTEST_FPUTESTDISABLE) && \
343+
!defined(CONFIG_BUILD_KERNEL)
343344
/* Check that the FPU is properly supported during context switching */
344345

345346
printf("\nuser_main: FPU test\n");
346347
fpu_test();
347348
check_test_memory_usage();
348349
#endif
349350

351+
#ifndef CONFIG_BUILD_KERNEL
350352
/* Checkout task_restart() */
351353

352354
printf("\nuser_main: task_restart test\n");
353355
restart_test();
354356
check_test_memory_usage();
357+
#endif
355358

356-
#ifdef CONFIG_SCHED_WAITPID
359+
#if defined(CONFIG_SCHED_WAITPID) && !defined(CONFIG_BUILD_KERNEL)
357360
/* Check waitpid() and friends */
358361

359362
printf("\nuser_main: waitpid test\n");
@@ -508,7 +511,8 @@ static int user_main(int argc, char *argv[])
508511
signest_test();
509512
check_test_memory_usage();
510513

511-
#if defined(CONFIG_SIG_SIGSTOP_ACTION) && defined(CONFIG_SIG_SIGKILL_ACTION)
514+
#if defined(CONFIG_SIG_SIGSTOP_ACTION) && defined(CONFIG_SIG_SIGKILL_ACTION) && \
515+
!defined(CONFIG_BUILD_KERNEL)
512516
printf("\nuser_main: signal action test\n");
513517
suspend_test();
514518
check_test_memory_usage();
@@ -581,8 +585,16 @@ static int user_main(int argc, char *argv[])
581585
#endif
582586

583587
#if defined(CONFIG_ARCH_HAVE_FORK) && defined(CONFIG_SCHED_WAITPID)
588+
#ifndef CONFIG_BUILD_KERNEL
584589
printf("\nuser_main: vfork() test\n");
585590
vfork_test();
591+
#else
592+
/* REVISIT: The issue with vfork() is on the kernel side, fix the issue
593+
* and re-enable this test with CONFIG_BUILD_KERNEL
594+
*/
595+
596+
printf("\nuser_main: vfork() test DISABLED (CONFIG_BUILD_KERNEL)\n");
597+
#endif
586598
#endif
587599

588600
#ifdef CONFIG_SMP_CALL
@@ -635,13 +647,36 @@ static void stdio_test(void)
635647

636648
int main(int argc, FAR char **argv)
637649
{
638-
int result;
639650
#ifdef CONFIG_TESTING_OSTEST_WAITRESULT
640651
int ostest_result = ERROR;
641652
#else
642653
int ostest_result = OK;
643654
#endif
644655

656+
#ifndef CONFIG_BUILD_KERNEL
657+
int result;
658+
#else
659+
struct sched_param param;
660+
posix_spawnattr_t attr;
661+
FAR const char * const arg[7] =
662+
{
663+
argv[0], "user_main", arg1, arg2, arg3, arg4, NULL
664+
};
665+
666+
pid_t result;
667+
int status;
668+
669+
/* Use posix_spawn API to spawn the new task for tests, should re-enter
670+
* the main entry in new task, the second arg as to identify the real
671+
* entry point of the test tasks.
672+
*/
673+
674+
if (argc >= 2 && strcmp(argv[1], "user_main") == 0)
675+
{
676+
return user_main(argc - 1 , &argv[1]);
677+
}
678+
#endif
679+
645680
/* Verify that stdio works first */
646681

647682
stdio_test();
@@ -680,9 +715,19 @@ int main(int argc, FAR char **argv)
680715

681716
/* Verify that we can spawn a new task */
682717

718+
#ifdef CONFIG_BUILD_KERNEL
719+
posix_spawnattr_init(&attr);
720+
param.sched_priority = PRIORITY;
721+
posix_spawnattr_setschedparam(&attr, &param);
722+
posix_spawnattr_setstacksize(&attr, STACKSIZE);
723+
status = posix_spawn(&result, arg[0], NULL, &attr,
724+
(char * const *)arg, NULL);
725+
if (status != 0)
726+
#else
683727
result = task_create("ostest", PRIORITY, STACKSIZE, user_main,
684728
(FAR char * const *)g_argv);
685729
if (result == ERROR)
730+
#endif
686731
{
687732
printf("ostest_main: ERROR Failed to start user_main\n");
688733
ASSERT(false);

testing/ostest/pthread_exit.c

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
* Private Functions
4545
****************************************************************************/
4646

47-
static void *pthread_exit_thread(FAR void *parameter)
47+
static FAR void *pthread_exit_thread(FAR void *parameter)
4848
{
4949
unsigned me = (unsigned)pthread_self();
5050

@@ -56,7 +56,7 @@ static void *pthread_exit_thread(FAR void *parameter)
5656
return NULL;
5757
}
5858

59-
static int pthread_exit_main(int argc, char **argv)
59+
static FAR void *pthread_exit_main(FAR void *arg)
6060
{
6161
pthread_t child;
6262
#ifdef SDCC
@@ -87,7 +87,7 @@ static int pthread_exit_main(int argc, char **argv)
8787
printf("pthread_exit_main %u: ERROR: Still running\n", me);
8888
exit(0);
8989

90-
return 0;
90+
return NULL;
9191
}
9292

9393
/****************************************************************************
@@ -96,20 +96,29 @@ static int pthread_exit_main(int argc, char **argv)
9696

9797
void pthread_exit_test(void)
9898
{
99-
int statloc;
99+
struct sched_param param;
100+
pthread_attr_t attr;
101+
pid_t pid;
100102
int ret;
101103

102-
ret = task_create("pthread_exit", PRIORITY, STACKSIZE, pthread_exit_main,
103-
NULL);
104+
pthread_attr_init(&attr);
105+
param.sched_priority = PRIORITY;
106+
pthread_attr_setschedparam(&attr, &param);
107+
pthread_attr_setstacksize(&attr, STACKSIZE);
108+
ret = pthread_create(&pid, &attr, pthread_exit_main, NULL);
104109
if (ret < 0)
105110
{
106-
printf("pthread_exit_test: ERROR task_create Failed\n");
111+
printf("pthread_exit_test: ERROR pthread_create Failed\n");
107112
}
108113
else
109114
{
110115
printf("pthread_exit_test: Started pthread_exit_main at PID=%d\n",
111-
ret);
112-
waitpid((pid_t)ret, &statloc, 0);
116+
pid);
117+
if (pthread_join(pid, NULL) != 0)
118+
{
119+
printf("pthread_exit_test: ERROR Failed to join to terminate\n");
120+
ASSERT(false);
121+
};
113122
}
114123
}
115124

testing/ostest/restart.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636

3737
#include "ostest.h"
3838

39+
#ifndef CONFIG_BUILD_KERNEL
40+
3941
/****************************************************************************
4042
* Pre-processor Definitions
4143
****************************************************************************/
@@ -225,3 +227,5 @@ void restart_test(void)
225227

226228
printf("restart_main: Exiting\n");
227229
}
230+
231+
#endif /* !CONFIG_BUILD_KERNEL */

testing/ostest/sighand.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ static void wakeup_action(int signo, siginfo_t *info, void *ucontext)
128128
}
129129
}
130130

131-
static int waiter_main(int argc, char *argv[])
131+
static FAR void *waiter_main(FAR void *arg)
132132
{
133133
sigset_t set;
134134
struct sigaction act;
@@ -218,6 +218,7 @@ void sighand_test(void)
218218
sigset_t set;
219219
#endif
220220
struct sched_param param;
221+
pthread_attr_t attr;
221222
union sigval sigvalue;
222223
pid_t waiterpid;
223224
int status;
@@ -265,9 +266,11 @@ void sighand_test(void)
265266
param.sched_priority = PTHREAD_DEFAULT_PRIORITY;
266267
}
267268

268-
waiterpid = task_create("waiter", param.sched_priority,
269-
STACKSIZE, waiter_main, NULL);
270-
if (waiterpid == ERROR)
269+
pthread_attr_init(&attr);
270+
pthread_attr_setschedparam(&attr, &param);
271+
pthread_attr_setstacksize(&attr, STACKSIZE);
272+
status = pthread_create(&waiterpid, &attr, waiter_main, NULL);
273+
if (status != 0)
271274
{
272275
printf("sighand_test: ERROR failed to start waiter_main\n");
273276
ASSERT(false);
@@ -293,7 +296,7 @@ void sighand_test(void)
293296
{
294297
printf("sighand_test: ERROR sigqueue failed\n");
295298
ASSERT(false);
296-
task_delete(waiterpid);
299+
pthread_cancel(waiterpid);
297300
}
298301

299302
/* Wait a bit */

testing/ostest/signest.c

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ static void waiter_action(int signo)
9494

9595
sched_lock();
9696
nest_level = g_nest_level++;
97-
sched_unlock();
9897

9998
if ((signo & 1) != 0)
10099
{
@@ -114,11 +113,12 @@ static void waiter_action(int signo)
114113
}
115114

116115
g_nest_level = nest_level;
116+
sched_unlock();
117117

118118
sem_post(&g_sem_signal_finish);
119119
}
120120

121-
static int waiter_main(int argc, char *argv[])
121+
static FAR void *waiter_main(FAR void *arg)
122122
{
123123
sigset_t set;
124124
struct sigaction act;
@@ -134,7 +134,7 @@ static int waiter_main(int argc, char *argv[])
134134
{
135135
printf("waiter_main: ERROR sigprocmask failed: %d\n", errno);
136136
ASSERT(false);
137-
return EXIT_FAILURE;
137+
return NULL;
138138
}
139139

140140
printf("waiter_main: Registering signal handler\n");
@@ -160,7 +160,7 @@ static int waiter_main(int argc, char *argv[])
160160
{
161161
printf("waiter_main: WARNING sigaction failed with %d\n",
162162
errno);
163-
return EXIT_FAILURE;
163+
return NULL;
164164
}
165165
}
166166
}
@@ -180,10 +180,10 @@ static int waiter_main(int argc, char *argv[])
180180
/* Just exit, the system should clean up the signal handlers */
181181

182182
g_waiter_running = false;
183-
return EXIT_SUCCESS;
183+
return NULL;
184184
}
185185

186-
static int interfere_main(int argc, char *argv[])
186+
static FAR void *interfere_main(FAR void *arg)
187187
{
188188
/* Now just loop staying in the way as much as possible */
189189

@@ -202,7 +202,7 @@ static int interfere_main(int argc, char *argv[])
202202
return EXIT_SUCCESS;
203203
}
204204

205-
void wait_finish(int pid, int sig)
205+
static void wait_finish(int pid, int sig)
206206
{
207207
struct timespec ts;
208208
int wait_times;
@@ -239,14 +239,14 @@ void wait_finish(int pid, int sig)
239239
void signest_test(void)
240240
{
241241
struct sched_param param;
242+
pthread_attr_t attr;
242243
pid_t waiterpid;
243244
pid_t interferepid;
244245
int total_signals;
245246
int total_handled;
246247
int total_nested;
247248
int even_signals;
248249
int odd_signals;
249-
int prio;
250250
int ret;
251251
int i;
252252
int j;
@@ -280,11 +280,14 @@ void signest_test(void)
280280

281281
/* Start waiter thread */
282282

283-
prio = param.sched_priority + 1;
284-
printf("signest_test: Starting signal waiter task at priority %d\n", prio);
285-
waiterpid = task_create("waiter", prio, STACKSIZE,
286-
waiter_main, NULL);
287-
if (waiterpid == ERROR)
283+
param.sched_priority++;
284+
printf("signest_test: Starting signal waiter task at priority %d\n",
285+
param.sched_priority);
286+
pthread_attr_init(&attr);
287+
pthread_attr_setschedparam(&attr, &param);
288+
pthread_attr_setstacksize(&attr, STACKSIZE);
289+
ret = pthread_create(&waiterpid, &attr, waiter_main, NULL);
290+
if (ret != 0)
288291
{
289292
printf("signest_test: ERROR failed to start waiter_main\n");
290293
ASSERT(false);
@@ -295,11 +298,12 @@ void signest_test(void)
295298

296299
/* Start interfering thread */
297300

298-
prio++;
299-
printf("signest_test: Starting interfering task at priority %d\n", prio);
300-
interferepid = task_create("interfere", prio, STACKSIZE,
301-
interfere_main, NULL);
302-
if (interferepid == ERROR)
301+
param.sched_priority++;
302+
printf("signest_test: Starting interfering task at priority %d\n",
303+
param.sched_priority);
304+
pthread_attr_setschedparam(&attr, &param);
305+
ret = pthread_create(&interferepid, &attr, interfere_main, NULL);
306+
if (ret != 0)
303307
{
304308
printf("signest_test: ERROR failed to start interfere_main\n");
305309
ASSERT(false);

testing/ostest/suspend.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@
3737

3838
#include "ostest.h"
3939

40+
/* REVISIT: This could be implemented for CONFIG_BUILD_KERNEL as well, by
41+
* starting a new process instead of using task_create()
42+
*/
43+
44+
#ifndef CONFIG_BUILD_KERNEL
45+
4046
/****************************************************************************
4147
* Public Functions
4248
****************************************************************************/
@@ -136,3 +142,4 @@ void suspend_test(void)
136142
printf("suspend_test: done\n");
137143
FFLUSH();
138144
}
145+
#endif /* !CONFIG_BUILD_KERNEL */

testing/ostest/waitpid.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@
3535

3636
#include "ostest.h"
3737

38-
#ifdef CONFIG_SCHED_WAITPID
38+
/* REVISIT: This could be implemented for CONFIG_BUILD_KERNEL as well, by
39+
* starting a new process instead of using task_create()
40+
*/
41+
42+
#if defined(CONFIG_SCHED_WAITPID) && !defined(CONFIG_BUILD_KERNEL)
3943

4044
/****************************************************************************
4145
* Pre-processor Definitions
@@ -392,4 +396,4 @@ int waitpid_test(void)
392396
return 0;
393397
}
394398

395-
#endif /* CONFIG_SCHED_WAITPID */
399+
#endif /* defined(CONFIG_SCHED_WAITPID) && !defined(CONFIG_BUILD_KERNEL) */

0 commit comments

Comments
 (0)