2015-10-03 01:33:30 +02:00
|
|
|
/****************************************************************************
|
2018-12-12 23:37:38 +01:00
|
|
|
* apps/testing/ostest/sighand.c
|
2011-03-20 19:18:19 +01:00
|
|
|
*
|
2020-04-11 20:13:56 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2011-03-20 19:18:19 +01:00
|
|
|
*
|
2020-04-11 20:13:56 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-03-20 19:18:19 +01:00
|
|
|
*
|
2020-04-11 20:13:56 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2011-03-20 19:18:19 +01:00
|
|
|
*
|
2015-10-03 01:33:30 +02:00
|
|
|
****************************************************************************/
|
2011-03-20 19:18:19 +01:00
|
|
|
|
2020-04-11 20:56:24 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-03-06 22:17:52 +01:00
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2023-03-16 03:35:15 +01:00
|
|
|
#include <assert.h>
|
2023-03-06 22:17:52 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <sched.h>
|
|
|
|
#include <semaphore.h>
|
|
|
|
#include <signal.h>
|
2011-03-20 19:18:19 +01:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
2023-03-06 22:17:52 +01:00
|
|
|
#include <sys/types.h>
|
2011-03-20 19:18:19 +01:00
|
|
|
#include <unistd.h>
|
2023-03-06 22:17:52 +01:00
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
#include "ostest.h"
|
|
|
|
|
2015-10-03 01:33:30 +02:00
|
|
|
/****************************************************************************
|
2015-04-09 23:53:59 +02:00
|
|
|
* Pre-processor Definitions
|
2015-10-03 01:33:30 +02:00
|
|
|
****************************************************************************/
|
2015-04-09 23:53:59 +02:00
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
#define WAKEUP_SIGNAL 17
|
|
|
|
#define SIGVALUE_INT 42
|
|
|
|
|
2015-10-03 01:33:30 +02:00
|
|
|
/****************************************************************************
|
2015-04-09 23:53:59 +02:00
|
|
|
* Private Data
|
2015-10-03 01:33:30 +02:00
|
|
|
****************************************************************************/
|
2015-04-09 23:53:59 +02:00
|
|
|
|
2022-01-04 08:53:16 +01:00
|
|
|
static sem_t sem1;
|
|
|
|
static sem_t sem2;
|
2011-03-20 19:18:19 +01:00
|
|
|
static bool sigreceived = false;
|
2022-01-04 08:53:16 +01:00
|
|
|
static bool thread1exited = false;
|
2011-03-20 19:18:19 +01:00
|
|
|
|
2015-10-03 01:33:30 +02:00
|
|
|
/****************************************************************************
|
2015-04-09 23:53:59 +02:00
|
|
|
* Private Functions
|
2015-10-03 01:33:30 +02:00
|
|
|
****************************************************************************/
|
2015-04-09 23:53:59 +02:00
|
|
|
|
2013-01-12 20:58:45 +01:00
|
|
|
#ifdef CONFIG_SCHED_HAVE_PARENT
|
|
|
|
static void death_of_child(int signo, siginfo_t *info, void *ucontext)
|
|
|
|
{
|
2013-01-13 01:35:47 +01:00
|
|
|
/* Use of printf in a signal handler is NOT safe! It can cause deadlocks!
|
|
|
|
* Also, signals are not queued by NuttX. As a consequence, some
|
2014-01-01 19:40:18 +01:00
|
|
|
* notifications will get lost (or the info data can be overwrittedn)!
|
2013-01-13 01:35:47 +01:00
|
|
|
* Because POSIX does not require signals to be queued, I do not think
|
|
|
|
* that this is a bug (the overwriting is a bug, however).
|
|
|
|
*/
|
2013-01-12 20:58:45 +01:00
|
|
|
|
|
|
|
if (info)
|
|
|
|
{
|
2015-08-14 18:12:35 +02:00
|
|
|
printf("death_of_child: PID %d received signal=%d code=%d "
|
|
|
|
"errno=%d pid=%d status=%d\n",
|
|
|
|
getpid(), signo, info->si_code, info->si_errno,
|
|
|
|
info->si_pid, info->si_status);
|
2013-01-12 20:58:45 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("death_of_child: PID %d received signal=%d (no info?)\n",
|
|
|
|
getpid(), signo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
static void wakeup_action(int signo, siginfo_t *info, void *ucontext)
|
|
|
|
{
|
|
|
|
sigset_t oldset;
|
|
|
|
sigset_t allsigs;
|
|
|
|
int status;
|
|
|
|
|
2013-01-12 20:58:45 +01:00
|
|
|
/* Use of printf in a signal handler is NOT safe! It can cause deadlocks! */
|
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
sigreceived = true;
|
|
|
|
|
|
|
|
/* Check signo */
|
|
|
|
|
|
|
|
if (signo != WAKEUP_SIGNAL)
|
|
|
|
{
|
2022-10-15 16:36:29 +02:00
|
|
|
ASSERT(false);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check siginfo */
|
|
|
|
|
|
|
|
if (info->si_value.sival_int != SIGVALUE_INT)
|
|
|
|
{
|
2022-10-15 16:36:29 +02:00
|
|
|
ASSERT(false);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (info->si_signo != WAKEUP_SIGNAL)
|
|
|
|
{
|
2022-10-15 16:36:29 +02:00
|
|
|
ASSERT(false);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check sigprocmask */
|
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
sigfillset(&allsigs);
|
2011-03-20 19:18:19 +01:00
|
|
|
status = sigprocmask(SIG_SETMASK, NULL, &oldset);
|
|
|
|
if (status != OK)
|
|
|
|
{
|
2022-10-15 16:36:29 +02:00
|
|
|
ASSERT(false);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (oldset != allsigs)
|
|
|
|
{
|
2022-10-15 16:36:29 +02:00
|
|
|
ASSERT(false);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int waiter_main(int argc, char *argv[])
|
|
|
|
{
|
2015-07-31 21:31:44 +02:00
|
|
|
sigset_t set;
|
2011-03-20 19:18:19 +01:00
|
|
|
struct sigaction act;
|
|
|
|
struct sigaction oact;
|
|
|
|
int status;
|
|
|
|
|
2020-04-11 20:56:24 +02:00
|
|
|
printf("waiter_main: Waiter started\n");
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
printf("waiter_main: Unmasking signal %d\n" , WAKEUP_SIGNAL);
|
2020-01-02 13:09:50 +01:00
|
|
|
sigemptyset(&set);
|
|
|
|
sigaddset(&set, WAKEUP_SIGNAL);
|
2015-07-31 21:31:44 +02:00
|
|
|
status = sigprocmask(SIG_UNBLOCK, &set, NULL);
|
2011-03-20 19:18:19 +01:00
|
|
|
if (status != OK)
|
|
|
|
{
|
|
|
|
printf("waiter_main: ERROR sigprocmask failed, status=%d\n",
|
|
|
|
status);
|
2022-10-15 16:36:29 +02:00
|
|
|
ASSERT(false);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
2020-04-11 20:56:24 +02:00
|
|
|
printf("waiter_main: Registering signal handler\n");
|
2011-03-20 19:18:19 +01:00
|
|
|
act.sa_sigaction = wakeup_action;
|
|
|
|
act.sa_flags = SA_SIGINFO;
|
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
sigfillset(&act.sa_mask);
|
|
|
|
sigdelset(&act.sa_mask, WAKEUP_SIGNAL);
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
status = sigaction(WAKEUP_SIGNAL, &act, &oact);
|
|
|
|
if (status != OK)
|
|
|
|
{
|
|
|
|
printf("waiter_main: ERROR sigaction failed, status=%d\n" , status);
|
2022-10-15 16:36:29 +02:00
|
|
|
ASSERT(false);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef SDCC
|
2020-11-13 04:30:30 +01:00
|
|
|
printf("waiter_main: oact.sigaction=%p oact.sa_flags=%x "
|
|
|
|
"oact.sa_mask=%jx\n",
|
|
|
|
oact.sa_sigaction, oact.sa_flags, (uintmax_t)oact.sa_mask);
|
2011-03-20 19:18:19 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Take the semaphore */
|
|
|
|
|
2020-04-11 20:56:24 +02:00
|
|
|
printf("waiter_main: Waiting on semaphore\n");
|
2011-05-28 23:42:18 +02:00
|
|
|
FFLUSH();
|
2011-03-20 19:18:19 +01:00
|
|
|
|
2022-01-04 08:53:16 +01:00
|
|
|
status = sem_wait(&sem1);
|
2011-03-20 19:18:19 +01:00
|
|
|
if (status != 0)
|
|
|
|
{
|
2011-04-03 16:26:05 +02:00
|
|
|
int error = errno;
|
2011-03-20 19:18:19 +01:00
|
|
|
if (error == EINTR)
|
|
|
|
{
|
2020-04-11 20:56:24 +02:00
|
|
|
printf("waiter_main: sem_wait() successfully interrupted by "
|
|
|
|
"signal\n");
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("waiter_main: ERROR sem_wait failed, errno=%d\n" , error);
|
2022-10-15 16:36:29 +02:00
|
|
|
ASSERT(false);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-04-11 20:56:24 +02:00
|
|
|
printf("waiter_main: ERROR awakened with no error!\n");
|
2022-10-15 16:36:29 +02:00
|
|
|
ASSERT(false);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Detach the signal handler */
|
|
|
|
|
2016-06-09 02:02:37 +02:00
|
|
|
act.sa_handler = SIG_DFL;
|
2020-01-02 13:09:50 +01:00
|
|
|
sigaction(WAKEUP_SIGNAL, &act, &oact);
|
2011-03-20 19:18:19 +01:00
|
|
|
|
2020-04-11 20:56:24 +02:00
|
|
|
printf("waiter_main: done\n");
|
2011-05-28 23:42:18 +02:00
|
|
|
FFLUSH();
|
2011-03-20 19:18:19 +01:00
|
|
|
|
2022-01-04 08:53:16 +01:00
|
|
|
thread1exited = true;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-03 01:33:30 +02:00
|
|
|
/****************************************************************************
|
2015-04-09 23:53:59 +02:00
|
|
|
* Public Functions
|
2015-10-03 01:33:30 +02:00
|
|
|
****************************************************************************/
|
2015-04-09 23:53:59 +02:00
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
void sighand_test(void)
|
|
|
|
{
|
2013-01-12 20:58:45 +01:00
|
|
|
#ifdef CONFIG_SCHED_HAVE_PARENT
|
|
|
|
struct sigaction act;
|
|
|
|
struct sigaction oact;
|
2015-07-31 21:31:44 +02:00
|
|
|
sigset_t set;
|
2013-01-12 20:58:45 +01:00
|
|
|
#endif
|
2011-03-20 19:18:19 +01:00
|
|
|
struct sched_param param;
|
|
|
|
union sigval sigvalue;
|
2022-12-20 08:38:20 +01:00
|
|
|
pid_t waiterpid;
|
2011-03-20 19:18:19 +01:00
|
|
|
int status;
|
|
|
|
|
2020-04-11 20:56:24 +02:00
|
|
|
printf("sighand_test: Initializing semaphore to 0\n");
|
2022-01-04 08:53:16 +01:00
|
|
|
sem_init(&sem1, 0, 0);
|
|
|
|
sem_init(&sem2, 0, 0);
|
2011-03-20 19:18:19 +01:00
|
|
|
|
2013-01-12 20:58:45 +01:00
|
|
|
#ifdef CONFIG_SCHED_HAVE_PARENT
|
|
|
|
printf("sighand_test: Unmasking SIGCHLD\n");
|
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
sigemptyset(&set);
|
|
|
|
sigaddset(&set, SIGCHLD);
|
2015-07-31 21:31:44 +02:00
|
|
|
status = sigprocmask(SIG_UNBLOCK, &set, NULL);
|
2013-01-12 20:58:45 +01:00
|
|
|
if (status != OK)
|
|
|
|
{
|
|
|
|
printf("sighand_test: ERROR sigprocmask failed, status=%d\n",
|
|
|
|
status);
|
2022-10-15 16:36:29 +02:00
|
|
|
ASSERT(false);
|
2013-01-12 20:58:45 +01:00
|
|
|
}
|
|
|
|
|
2020-04-11 20:56:24 +02:00
|
|
|
printf("sighand_test: Registering SIGCHLD handler\n");
|
2013-01-12 20:58:45 +01:00
|
|
|
act.sa_sigaction = death_of_child;
|
|
|
|
act.sa_flags = SA_SIGINFO;
|
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
sigfillset(&act.sa_mask);
|
|
|
|
sigdelset(&act.sa_mask, SIGCHLD);
|
2013-01-12 20:58:45 +01:00
|
|
|
|
|
|
|
status = sigaction(SIGCHLD, &act, &oact);
|
|
|
|
if (status != OK)
|
|
|
|
{
|
|
|
|
printf("waiter_main: ERROR sigaction failed, status=%d\n" , status);
|
2022-10-15 16:36:29 +02:00
|
|
|
ASSERT(false);
|
2013-01-12 20:58:45 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
/* Start waiter thread */
|
|
|
|
|
2020-04-11 20:56:24 +02:00
|
|
|
printf("sighand_test: Starting waiter task\n");
|
2011-03-20 19:18:19 +01:00
|
|
|
status = sched_getparam (0, ¶m);
|
|
|
|
if (status != OK)
|
|
|
|
{
|
2020-04-11 20:56:24 +02:00
|
|
|
printf("sighand_test: ERROR sched_getparam() failed\n");
|
2022-10-15 16:36:29 +02:00
|
|
|
ASSERT(false);
|
2011-03-20 19:18:19 +01:00
|
|
|
param.sched_priority = PTHREAD_DEFAULT_PRIORITY;
|
|
|
|
}
|
|
|
|
|
|
|
|
waiterpid = task_create("waiter", param.sched_priority,
|
2018-06-07 17:52:02 +02:00
|
|
|
STACKSIZE, waiter_main, NULL);
|
2011-03-20 19:18:19 +01:00
|
|
|
if (waiterpid == ERROR)
|
|
|
|
{
|
2020-04-11 20:56:24 +02:00
|
|
|
printf("sighand_test: ERROR failed to start waiter_main\n");
|
2022-10-15 16:36:29 +02:00
|
|
|
ASSERT(false);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("sighand_test: Started waiter_main pid=%d\n", waiterpid);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait a bit */
|
|
|
|
|
2011-05-28 23:42:18 +02:00
|
|
|
FFLUSH();
|
2011-03-20 19:18:19 +01:00
|
|
|
sleep(2);
|
|
|
|
|
|
|
|
/* Then signal the waiter thread. */
|
|
|
|
|
|
|
|
printf("sighand_test: Signaling pid=%d with signo=%d sigvalue=%d\n",
|
|
|
|
waiterpid, WAKEUP_SIGNAL, SIGVALUE_INT);
|
|
|
|
|
|
|
|
sigvalue.sival_int = SIGVALUE_INT;
|
|
|
|
status = sigqueue(waiterpid, WAKEUP_SIGNAL, sigvalue);
|
|
|
|
if (status != OK)
|
|
|
|
{
|
2020-04-11 20:56:24 +02:00
|
|
|
printf("sighand_test: ERROR sigqueue failed\n");
|
2022-10-15 16:36:29 +02:00
|
|
|
ASSERT(false);
|
2011-03-20 19:18:19 +01:00
|
|
|
task_delete(waiterpid);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait a bit */
|
|
|
|
|
2011-05-28 23:42:18 +02:00
|
|
|
FFLUSH();
|
2022-09-06 15:00:08 +02:00
|
|
|
status = sleep(2);
|
|
|
|
while (status)
|
|
|
|
{
|
|
|
|
status = sleep(status);
|
|
|
|
}
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
/* Then check the result */
|
|
|
|
|
2022-01-04 08:53:16 +01:00
|
|
|
if (!thread1exited)
|
2011-03-20 19:18:19 +01:00
|
|
|
{
|
2020-04-11 20:56:24 +02:00
|
|
|
printf("sighand_test: ERROR waiter task did not exit\n");
|
2022-10-15 16:36:29 +02:00
|
|
|
ASSERT(false);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!sigreceived)
|
|
|
|
{
|
2020-04-11 20:56:24 +02:00
|
|
|
printf("sighand_test: ERROR signal handler did not run\n");
|
2022-10-15 16:36:29 +02:00
|
|
|
ASSERT(false);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
2013-01-12 20:58:45 +01:00
|
|
|
/* Detach the signal handler */
|
|
|
|
|
|
|
|
#ifdef CONFIG_SCHED_HAVE_PARENT
|
2016-06-09 02:02:37 +02:00
|
|
|
act.sa_handler = SIG_DFL;
|
2020-01-02 13:09:50 +01:00
|
|
|
sigaction(SIGCHLD, &act, &oact);
|
2013-01-12 20:58:45 +01:00
|
|
|
#endif
|
|
|
|
|
2020-04-11 20:56:24 +02:00
|
|
|
printf("sighand_test: done\n");
|
2011-05-28 23:42:18 +02:00
|
|
|
FFLUSH();
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|