2017-10-15 22:41:59 +08:00
|
|
|
/*
|
2023-05-23 22:01:23 +08:00
|
|
|
* Copyright (c) 2006-2023, RT-Thread Development Team
|
2017-10-15 22:41:59 +08:00
|
|
|
*
|
2018-10-14 19:28:18 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2017-10-15 22:41:59 +08:00
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2017/10/1 Bernard The first version
|
|
|
|
*/
|
2021-07-21 01:43:48 +08:00
|
|
|
|
2017-10-15 22:41:59 +08:00
|
|
|
#include <rthw.h>
|
|
|
|
#include <rtthread.h>
|
|
|
|
|
2021-05-22 02:56:53 +08:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/errno.h>
|
2017-10-15 22:41:59 +08:00
|
|
|
|
|
|
|
#include "posix_signal.h"
|
2021-07-21 01:43:48 +08:00
|
|
|
|
2017-10-15 22:41:59 +08:00
|
|
|
#define sig_valid(sig_no) (sig_no >= 0 && sig_no < RT_SIG_MAX)
|
|
|
|
|
|
|
|
void (*signal(int sig, void (*func)(int))) (int)
|
|
|
|
{
|
2018-03-01 13:36:22 +08:00
|
|
|
return rt_signal_install(sig, func);
|
2017-10-15 22:41:59 +08:00
|
|
|
}
|
|
|
|
|
2023-05-23 22:01:23 +08:00
|
|
|
/**
|
|
|
|
* @brief This function will examines, changes, or examines and changes the signal mask of the calling thread.
|
|
|
|
*
|
|
|
|
* @param how indicates the way in which the existing set of blocked signals should be changed.
|
|
|
|
* The following are the possible values for option:
|
|
|
|
*
|
|
|
|
* SIG_BLOCK The set of blocked signals is the union of the current set and the set argument.
|
|
|
|
*
|
|
|
|
* SIG_UNBLOCK The signals in set are removed from the current set of blocked signals.
|
|
|
|
* It is permissible to attempt to unblock a signal which is not blocked.
|
|
|
|
*
|
|
|
|
* SIG_SETMASK The set of blocked signals is set to the argument set.
|
|
|
|
*
|
|
|
|
* @param set is a pointer to a sigset_t object that specifies the new set of blocked signals.
|
|
|
|
* If set is NULL, then the signal mask is unchanged (i.e., how is ignored)
|
|
|
|
*
|
|
|
|
* @param oset is a pointer to a sigset_t object that is used to return the previous set of blocked signals.
|
|
|
|
* If oset is non-NULL, the previous value of the signal mask is stored in it.
|
|
|
|
*
|
|
|
|
* @return Returns 0 on success.
|
|
|
|
*/
|
2017-10-15 22:41:59 +08:00
|
|
|
int sigprocmask (int how, const sigset_t *set, sigset_t *oset)
|
|
|
|
{
|
2018-03-01 13:36:22 +08:00
|
|
|
rt_base_t level;
|
2017-10-15 22:41:59 +08:00
|
|
|
rt_thread_t tid;
|
|
|
|
|
|
|
|
tid = rt_thread_self();
|
|
|
|
|
2018-03-01 13:36:22 +08:00
|
|
|
level = rt_hw_interrupt_disable();
|
|
|
|
if (oset) *oset = tid->sig_mask;
|
|
|
|
|
|
|
|
if (set)
|
|
|
|
{
|
|
|
|
switch(how)
|
|
|
|
{
|
|
|
|
case SIG_BLOCK:
|
|
|
|
tid->sig_mask |= *set;
|
|
|
|
break;
|
|
|
|
case SIG_UNBLOCK:
|
|
|
|
tid->sig_mask &= ~*set;
|
|
|
|
break;
|
|
|
|
case SIG_SETMASK:
|
|
|
|
tid->sig_mask = *set;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rt_hw_interrupt_enable(level);
|
2017-10-15 22:41:59 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-05-23 22:01:23 +08:00
|
|
|
/**
|
|
|
|
* @brief This function will examines the signal mask of the calling thread.
|
|
|
|
*
|
|
|
|
* @param set is a pointer to a sigset_t object that is used to return the previous set of blocked signals.
|
|
|
|
* If set is non-NULL, the previous value of the signal mask is stored in it.
|
|
|
|
*
|
|
|
|
* @return Returns 0 on success.
|
|
|
|
*/
|
2022-01-20 20:53:47 +08:00
|
|
|
int sigpending (sigset_t *set)
|
|
|
|
{
|
|
|
|
sigprocmask(SIG_SETMASK, RT_NULL, set);
|
|
|
|
return 0;
|
|
|
|
}
|
2023-06-10 12:41:21 +08:00
|
|
|
/**
|
|
|
|
* @brief This function will temporarily replace the signal mask of the calling thread
|
|
|
|
* with the mask given and then suspends the thread until delivery of an expected signal
|
|
|
|
* or a signal whose action is to terminate a process.
|
|
|
|
*
|
|
|
|
* @param set is a pointer of a sigset_t object that is used to replace the original mask of the calling thread.
|
|
|
|
*
|
|
|
|
* @return Returns 0 on success.
|
|
|
|
* If the return value is any other values, it means that the signal wait failed.
|
|
|
|
*/
|
2022-01-20 20:53:47 +08:00
|
|
|
int sigsuspend (const sigset_t *set)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
sigset_t origin_set;
|
|
|
|
sigset_t suspend_set;
|
|
|
|
siginfo_t info; /* unless paremeter */
|
|
|
|
|
|
|
|
/* get the origin signal information */
|
|
|
|
sigpending(&origin_set);
|
|
|
|
|
|
|
|
/* set the new signal information */
|
|
|
|
sigprocmask(SIG_BLOCK, set, RT_NULL);
|
|
|
|
sigpending(&suspend_set);
|
|
|
|
|
|
|
|
ret = rt_signal_wait(&suspend_set, &info, RT_WAITING_FOREVER);
|
|
|
|
|
|
|
|
/* restore the original sigprocmask */
|
|
|
|
sigprocmask(SIG_UNBLOCK, (sigset_t *)0xffffUL, RT_NULL);
|
|
|
|
sigprocmask(SIG_BLOCK, &origin_set, RT_NULL);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-05-23 22:01:23 +08:00
|
|
|
/**
|
|
|
|
* @brief This function will install or confirm action for specified signal.
|
|
|
|
*
|
|
|
|
* @param signum is the signal to be handled.
|
|
|
|
*
|
|
|
|
* @param act is the new signal action, or NULL to restore default action.
|
|
|
|
*
|
|
|
|
* @param oldact returns the previous signal action, or NULL if not required.
|
|
|
|
*
|
|
|
|
* @return Returns 0 on success or -1 on failure.
|
|
|
|
*/
|
2017-10-15 22:41:59 +08:00
|
|
|
int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact)
|
|
|
|
{
|
|
|
|
rt_sighandler_t old = RT_NULL;
|
|
|
|
|
2018-03-01 13:36:22 +08:00
|
|
|
if (!sig_valid(signum)) return -RT_ERROR;
|
2017-10-15 22:41:59 +08:00
|
|
|
|
2018-03-01 13:36:22 +08:00
|
|
|
if (act)
|
|
|
|
old = rt_signal_install(signum, act->sa_handler);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
old = rt_signal_install(signum, RT_NULL);
|
|
|
|
rt_signal_install(signum, old);
|
|
|
|
}
|
2017-10-15 22:41:59 +08:00
|
|
|
|
2018-03-01 13:36:22 +08:00
|
|
|
if (oldact)
|
|
|
|
oldact->sa_handler = old;
|
2017-10-15 22:41:59 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2023-06-10 12:41:21 +08:00
|
|
|
/**
|
|
|
|
* @brief This function will suspends execution of the calling thread until one of
|
|
|
|
* the signals in the given set is pending. If none of the signals specified
|
|
|
|
* are pending, it will wait for the specified time interval.
|
|
|
|
*
|
|
|
|
* @param set is the set of signal values to be waited for.
|
|
|
|
*
|
|
|
|
* @param info is a pointer to the received signal info.
|
|
|
|
*
|
|
|
|
* @param timeout is a pointer to a timespec structure that specifys the waiting time.
|
|
|
|
*
|
|
|
|
* @return Return 0 on success. Otherwise, return -1 and set errno to indicate the error.
|
|
|
|
*/
|
2022-01-20 20:53:47 +08:00
|
|
|
int sigtimedwait(const sigset_t *set, siginfo_t *info, const struct timespec *timeout)
|
2017-10-15 22:41:59 +08:00
|
|
|
{
|
2018-03-01 13:36:22 +08:00
|
|
|
int ret = 0;
|
|
|
|
int tick = RT_WAITING_FOREVER;
|
|
|
|
|
|
|
|
if (timeout)
|
|
|
|
{
|
2023-07-17 20:11:58 +08:00
|
|
|
tick = timeout->tv_sec * RT_TICK_PER_SECOND + timeout->tv_nsec * RT_TICK_PER_SECOND / NANOSECOND_PER_SECOND;
|
2018-03-01 13:36:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = rt_signal_wait(set, info, tick);
|
|
|
|
if (ret == 0) return 0;
|
|
|
|
|
|
|
|
errno = ret;
|
|
|
|
return -1;
|
2017-10-15 22:41:59 +08:00
|
|
|
}
|
2023-06-10 12:41:21 +08:00
|
|
|
/**
|
|
|
|
* @brief This function will suspend execution of the calling thread until one of
|
|
|
|
* the specified signal becomes pending and return the signal number.
|
|
|
|
*
|
|
|
|
* @param set is the set of signal values to be waited for.
|
|
|
|
*
|
|
|
|
* @param sig is a pointer to the received signal number.
|
|
|
|
*
|
|
|
|
* @return Return 0 on success or -1 on failure.
|
|
|
|
*/
|
2017-10-15 22:41:59 +08:00
|
|
|
int sigwait(const sigset_t *set, int *sig)
|
|
|
|
{
|
|
|
|
siginfo_t si;
|
|
|
|
if (sigtimedwait(set, &si, 0) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
*sig = si.si_signo;
|
|
|
|
return 0;
|
|
|
|
}
|
2023-06-10 12:41:21 +08:00
|
|
|
/**
|
|
|
|
* @brief This function will suspend execution of the calling thread until one of
|
|
|
|
* the specified signal is pending.
|
|
|
|
*
|
|
|
|
* @param set is the set of signal values to be waited for.
|
|
|
|
*
|
|
|
|
* @param info is a pointer to the received signal info.
|
|
|
|
*
|
|
|
|
* @return Return 0 on success or -1 on failure.
|
|
|
|
*/
|
2017-10-15 22:41:59 +08:00
|
|
|
int sigwaitinfo(const sigset_t *set, siginfo_t *info)
|
|
|
|
{
|
2018-03-01 13:36:22 +08:00
|
|
|
return sigtimedwait(set, info, NULL);
|
2017-10-15 22:41:59 +08:00
|
|
|
}
|
|
|
|
|
2023-05-23 22:01:23 +08:00
|
|
|
/**
|
|
|
|
* @brief This function willsend a signal to the caller
|
|
|
|
*
|
|
|
|
* @param sig is the signal that is to be sent.
|
|
|
|
*
|
|
|
|
* @return Returns 0 on success.
|
|
|
|
*/
|
2017-10-15 22:41:59 +08:00
|
|
|
int raise(int sig)
|
|
|
|
{
|
2018-03-01 13:36:22 +08:00
|
|
|
rt_thread_kill(rt_thread_self(), sig);
|
2017-10-15 22:41:59 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-01-20 20:53:47 +08:00
|
|
|
#include <sys/types.h>
|
2024-03-25 14:24:26 +08:00
|
|
|
/**
|
|
|
|
* @brief Sends a signal to the caller.
|
|
|
|
*
|
|
|
|
* This function sends the signal specified by @p sig to the caller.
|
|
|
|
*
|
|
|
|
* @param sig The signal to be sent.
|
|
|
|
* This should be one of the standard signal macros such as SIGUSR1, SIGUSR2, etc.
|
|
|
|
*
|
|
|
|
* @return Returns 0 on success. If an error occurs, -1 is returned and errno is set appropriately.
|
|
|
|
*/
|
2022-01-20 20:53:47 +08:00
|
|
|
int sigqueue (pid_t pid, int signo, const union sigval value)
|
|
|
|
{
|
|
|
|
/* no support, signal queue */
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|