2015-04-09 18:16:35 +02:00
|
|
|
/****************************************************************************
|
2018-09-17 21:55:33 +02:00
|
|
|
* libs/libc/signal/sig_signal.c
|
2015-04-09 18:16:35 +02:00
|
|
|
*
|
2021-03-02 15:54:21 +01: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
|
2015-04-09 18:16:35 +02:00
|
|
|
*
|
2021-03-02 15:54:21 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-04-09 18:16:35 +02:00
|
|
|
*
|
2021-03-02 15:54:21 +01: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.
|
2015-04-09 18:16:35 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-04-11 17:04:54 +02:00
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2015-04-09 18:16:35 +02:00
|
|
|
#include <signal.h>
|
2016-04-11 17:04:54 +02:00
|
|
|
#include <assert.h>
|
2022-11-25 15:59:46 +01:00
|
|
|
#include <errno.h>
|
2015-04-09 18:16:35 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2016-04-10 22:35:45 +02:00
|
|
|
* Name: signal
|
2015-04-09 18:16:35 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2016-04-10 22:35:45 +02:00
|
|
|
* The signal() function will modify signal dispositions. The 'signo'
|
|
|
|
* argument specifies the signal. The 'func' argument specifies the
|
2015-04-09 18:16:35 +02:00
|
|
|
* signal's disposition, which may be SIG_DFL, SIG_IGN, or the address
|
2016-04-11 17:04:54 +02:00
|
|
|
* of a signal handler. If 'func' is the address of a signal handler, the
|
2015-04-09 18:16:35 +02:00
|
|
|
* system will add 'signo' to the calling process' signal mask before
|
|
|
|
* executing the signal handler; when the signal handler returns, the
|
|
|
|
* system will restore the calling process' signal mask to its state prior
|
2016-04-11 17:04:54 +02:00
|
|
|
* to the delivery of the signal.
|
2015-04-09 18:16:35 +02:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* signo - Identifies the signal to operate on
|
2016-04-10 22:35:45 +02:00
|
|
|
* func - The new disposition of the signal
|
2015-04-09 18:16:35 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
2016-04-11 17:04:54 +02:00
|
|
|
* Upon successful completion, signal() will return the previous
|
|
|
|
* disposition of the signal handling. Otherwise, SIG_ERR will be returned
|
|
|
|
* and errno set to indicate the nature of the error.
|
2015-04-09 18:16:35 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-10-08 17:22:00 +02:00
|
|
|
_sa_handler_t signal(int signo, _sa_handler_t func)
|
2015-04-09 18:16:35 +02:00
|
|
|
{
|
|
|
|
struct sigaction act;
|
|
|
|
struct sigaction oact;
|
2022-11-25 15:59:46 +01:00
|
|
|
int ret = -EINVAL;
|
2015-04-09 18:16:35 +02:00
|
|
|
|
2023-06-24 12:19:25 +02:00
|
|
|
if (!GOOD_SIGNO(signo) || UNCAUGHT_SIGNO(signo))
|
2022-11-25 15:59:46 +01:00
|
|
|
{
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUGASSERT(func != SIG_ERR && func != SIG_HOLD);
|
2016-04-11 17:04:54 +02:00
|
|
|
|
2015-04-09 18:16:35 +02:00
|
|
|
/* Initialize the sigaction structure */
|
|
|
|
|
2016-04-10 22:35:45 +02:00
|
|
|
act.sa_handler = func;
|
2015-04-09 18:16:35 +02:00
|
|
|
act.sa_flags = 0;
|
2020-01-02 17:49:34 +01:00
|
|
|
sigemptyset(&act.sa_mask);
|
2015-04-09 18:16:35 +02:00
|
|
|
|
|
|
|
/* Check for SIG_IGN and SIG_DFL (and someday SIG_HOLD)
|
|
|
|
*
|
|
|
|
* REVISIT: Currently SIG_IGN, SIG_DFL, and SIG_HOLD have the same value
|
|
|
|
* and cannot be distinguished.
|
|
|
|
*/
|
|
|
|
|
2016-04-10 22:35:45 +02:00
|
|
|
if (func != SIG_DFL /* && func != SIG_IGN */)
|
2015-04-09 18:16:35 +02:00
|
|
|
{
|
2015-04-10 00:13:03 +02:00
|
|
|
/* Add the signal to the set of signals to be ignored when the signal
|
2015-04-09 18:16:35 +02:00
|
|
|
* handler executes.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = sigaddset(&act.sa_mask, signo);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* Would happen if signo were invalid */
|
|
|
|
|
2022-11-25 15:59:46 +01:00
|
|
|
goto err;
|
2015-04-09 18:16:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the signal disposition */
|
|
|
|
|
|
|
|
ret = sigaction(signo, &act, &oact);
|
|
|
|
|
2016-04-10 22:35:45 +02:00
|
|
|
/* Upon successful completion, signal() will the signal's previous
|
2015-04-09 18:16:35 +02:00
|
|
|
* disposition. Otherwise, SIG_ERR will be returned and errno set to
|
|
|
|
* indicate the error.
|
|
|
|
*/
|
|
|
|
|
2015-10-04 23:04:00 +02:00
|
|
|
if (ret == OK)
|
2021-03-02 14:33:27 +01:00
|
|
|
{
|
|
|
|
return oact.sa_handler;
|
|
|
|
}
|
2015-04-09 18:16:35 +02:00
|
|
|
|
2022-11-25 15:59:46 +01:00
|
|
|
err:
|
|
|
|
set_errno(-ret);
|
2016-04-03 21:58:49 +02:00
|
|
|
return (_sa_handler_t)SIG_ERR;
|
2015-04-09 18:16:35 +02:00
|
|
|
}
|