2017-10-15 22:41:59 +08:00
|
|
|
/*
|
2021-03-08 18:19:04 +08:00
|
|
|
* Copyright (c) 2006-2021, 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
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sigtimedwait(const sigset_t *set, siginfo_t *info,
|
|
|
|
const struct timespec *timeout)
|
|
|
|
{
|
2018-03-01 13:36:22 +08:00
|
|
|
int ret = 0;
|
|
|
|
int tick = RT_WAITING_FOREVER;
|
|
|
|
|
|
|
|
if (timeout)
|
|
|
|
{
|
2021-07-22 00:35:08 +08:00
|
|
|
tick = rt_timespec_to_tick(timeout);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|