first
This commit is contained in:
13
rt-thread/components/libc/posix/signal/SConscript
Normal file
13
rt-thread/components/libc/posix/signal/SConscript
Normal file
@@ -0,0 +1,13 @@
|
||||
# RT-Thread building script for component
|
||||
|
||||
from building import *
|
||||
|
||||
cwd = GetCurrentDir()
|
||||
src = Glob('*.c') + Glob('*.cpp')
|
||||
CPPPATH = [cwd]
|
||||
|
||||
group = DefineGroup('POSIX', src,
|
||||
depend = ['RT_USING_SIGNALS', 'RT_USING_PTHREADS'],
|
||||
CPPPATH = CPPPATH)
|
||||
|
||||
Return('group')
|
248
rt-thread/components/libc/posix/signal/posix_signal.c
Normal file
248
rt-thread/components/libc/posix/signal/posix_signal.c
Normal file
@@ -0,0 +1,248 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2023, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2017/10/1 Bernard The first version
|
||||
*/
|
||||
|
||||
#include <rthw.h>
|
||||
#include <rtthread.h>
|
||||
|
||||
#include <sys/time.h>
|
||||
#include <sys/errno.h>
|
||||
|
||||
#include "posix_signal.h"
|
||||
|
||||
#define sig_valid(sig_no) (sig_no >= 0 && sig_no < RT_SIG_MAX)
|
||||
|
||||
void (*signal(int sig, void (*func)(int))) (int)
|
||||
{
|
||||
return rt_signal_install(sig, func);
|
||||
}
|
||||
|
||||
/**
|
||||
* @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.
|
||||
*/
|
||||
int sigprocmask (int how, const sigset_t *set, sigset_t *oset)
|
||||
{
|
||||
rt_base_t level;
|
||||
rt_thread_t tid;
|
||||
|
||||
tid = rt_thread_self();
|
||||
|
||||
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);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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.
|
||||
*/
|
||||
int sigpending (sigset_t *set)
|
||||
{
|
||||
sigprocmask(SIG_SETMASK, RT_NULL, set);
|
||||
return 0;
|
||||
}
|
||||
/**
|
||||
* @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.
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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.
|
||||
*/
|
||||
int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact)
|
||||
{
|
||||
rt_sighandler_t old = RT_NULL;
|
||||
|
||||
if (!sig_valid(signum)) return -RT_ERROR;
|
||||
|
||||
if (act)
|
||||
old = rt_signal_install(signum, act->sa_handler);
|
||||
else
|
||||
{
|
||||
old = rt_signal_install(signum, RT_NULL);
|
||||
rt_signal_install(signum, old);
|
||||
}
|
||||
|
||||
if (oldact)
|
||||
oldact->sa_handler = old;
|
||||
|
||||
return 0;
|
||||
}
|
||||
/**
|
||||
* @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.
|
||||
*/
|
||||
int sigtimedwait(const sigset_t *set, siginfo_t *info, const struct timespec *timeout)
|
||||
{
|
||||
int ret = 0;
|
||||
int tick = RT_WAITING_FOREVER;
|
||||
|
||||
if (timeout)
|
||||
{
|
||||
tick = timeout->tv_sec * RT_TICK_PER_SECOND + timeout->tv_nsec * RT_TICK_PER_SECOND / NANOSECOND_PER_SECOND;
|
||||
}
|
||||
|
||||
ret = rt_signal_wait(set, info, tick);
|
||||
if (ret == 0) return 0;
|
||||
|
||||
errno = ret;
|
||||
return -1;
|
||||
}
|
||||
/**
|
||||
* @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.
|
||||
*/
|
||||
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;
|
||||
}
|
||||
/**
|
||||
* @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.
|
||||
*/
|
||||
int sigwaitinfo(const sigset_t *set, siginfo_t *info)
|
||||
{
|
||||
return sigtimedwait(set, info, NULL);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function willsend a signal to the caller
|
||||
*
|
||||
* @param sig is the signal that is to be sent.
|
||||
*
|
||||
* @return Returns 0 on success.
|
||||
*/
|
||||
int raise(int sig)
|
||||
{
|
||||
rt_thread_kill(rt_thread_self(), sig);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#include <sys/types.h>
|
||||
/**
|
||||
* @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.
|
||||
*/
|
||||
int sigqueue (pid_t pid, int signo, const union sigval value)
|
||||
{
|
||||
/* no support, signal queue */
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
61
rt-thread/components/libc/posix/signal/posix_signal.h
Normal file
61
rt-thread/components/libc/posix/signal/posix_signal.h
Normal file
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2021, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2017/10/1 Bernard The first version
|
||||
*/
|
||||
|
||||
#ifndef POSIX_SIGNAL_H__
|
||||
#define POSIX_SIGNAL_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <sys/signal.h>
|
||||
|
||||
enum rt_signal_value{
|
||||
SIG1 = SIGHUP, // Hangup detected on controlling terminal or death of controlling process
|
||||
SIG2 = SIGINT, // Interrupt from keyboard
|
||||
SIG3 = SIGQUIT, // Quit from keyboard
|
||||
SIG4 = SIGILL, // Illegal instruction
|
||||
SIG5 = SIGTRAP, // Trace trap
|
||||
SIG6 = SIGABRT, // Abort signal from abort(3)
|
||||
SIG7 = SIGEMT, // Emulator trap
|
||||
SIG8 = SIGFPE, // Floating-point exception
|
||||
SIG9 = SIGKILL, // Kill signal
|
||||
SIG10 = SIGBUS, // Bus error
|
||||
SIG11 = SIGSEGV, // Segmentation fault
|
||||
SIG12 = SIGSYS, // Bad system call
|
||||
SIG13 = SIGPIPE, // Broken pipe
|
||||
SIG14 = SIGALRM, // Timer signal from alarm(2)
|
||||
SIG15 = SIGTERM, // Termination signal
|
||||
SIG16 = SIGURG, // Urgent condition on socket
|
||||
SIG17 = SIGSTOP, // Stop executing (cannot be caught or ignored)
|
||||
SIG18 = SIGTSTP, // Stop signal from keyboard to suspend execution
|
||||
SIG19 = SIGCONT, // Continue if stopped
|
||||
SIG20 = SIGCHLD, // Child status has changed
|
||||
SIG21 = SIGTTIN, // Background read from control terminal attempted by background process
|
||||
SIG22 = SIGTTOU, // Background write to control terminal attempted by background process
|
||||
SIG23 = SIGPOLL, // Pollable event
|
||||
SIG24 = 24, // SIGXCPU: CPU time limit exceeded
|
||||
SIG25 = 25, // SIGXFSZ: File size limit exceeded
|
||||
SIG26 = 26, // SIGVTALRM: Virtual timer expired
|
||||
SIG27 = 27, // SIGPROF: Profiling timer expired
|
||||
SIG28 = SIGWINCH,// Window size changed
|
||||
SIG29 = 29, // SIGLOST
|
||||
SIG30 = SIGUSR1, // User-defined signal 1
|
||||
SIG31 = SIGUSR2, // User-defined signal 2
|
||||
SIGRT_MIN = 27, // SIGRTMIN: Minimum real-time signal number
|
||||
SIGRT_MAX = 31, // SIGRTMAX: Maximum real-time signal number
|
||||
SIGMAX = NSIG // Number of signals (total)
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user