gtpc2m6m | C/C++ Language Support User's Guide |
This function allows the calling process to examine or change the action
associated with a signal.
- Note:
- The sigaction function is similar to the signal
function.
Format
#include <signal.h>
int sigaction( int sig,
const struct sigaction *act,
struct sigaction *oact);
- sig
- One of the signals defined in the signal.h header file
that corresponds to a signal to be examined or changed. See Table 13 for a list of supported signals.
- act
- One of the following:
- A pointer to a sigaction data structure that is initialized by
the caller. The signal action is changed according to the
sigaction data structure referenced by the act
parameter.
- A value of NULL. The signal action is not changed.
- oact
- One of the following:
- A pointer to a sigaction data structure where the
sigaction function stores information related to the signal
identified by the sig parameter before changing the signal
action.
- A value of NULL. This causes the sigaction function to
not store information related to the signal identified by the sig
parameter.
Normal Return
If successful, the sigaction function returns a value of
0.
Error Return
If unsuccessful, the sigaction function returns a value of
-1 and sets errno to following:
- EINVAL
- One of the following is true:
- The sig parameter is not a valid signal number.
- An attempt was made to set the action to SIG_DFL for a signal (specified
by the sig parameter) that cannot be caught or ignored.
Programming Considerations
- When a signal action is changed by either the sigaction or the
signal function, the new signal action replaces the previous signal
action regardless of whether the previous signal action was installed by a
sigaction or signal function.
- When a signal handler that is installed by the sigaction
function is given control, a new signal mask is created for the
process. The new signal mask includes all of the signals in the current
signal mask and all of the signals in the sa_mask field in the
sigaction data structure specified by the act
parameter. In addition, if SA_NODEFER and SA_RESETHAND are not set in
the sa_flags field in the sigaction data structure
specified by the act parameter, the new signal mask also includes
the signal being handled. When the signal handler returns control, the
signal mask is restored to the mask that was in use before the signal handler
received control.
If SA_NODEFER or SA_RESETHAND is set on in the sa_flags
field in the sigaction data structure specified by the
act parameter, the signal mask is not changed.
- When the oact parameter is a pointer, the action for the
specified signal is stored in the sigaction data structure
referenced by the oact parameter. Moreover, if the action
for the specified signal was installed by a signal function instead
of by a sigaction function, the information stored in the
sigaction data structure referenced by the oact
parameter by a subsequent sigaction function call cannot be
predicted.
- Table 12 summarizes sigaction function processing based on
the specifications for the act and oact
parameters:
Table 12. sigaction Function Specification Summary
act
| oact
| Results
|
Pointer
| NULL
| The signal action is changed as specified by the sigaction
data structure specified by the act parameter.
|
Pointer
| Pointer
| The following actions occur:
- The signal action is changed as specified by the sigaction data
structure specified by the act parameter.
- The signal action before sigaction function processing occurs
is stored in the sigaction data structure specified by the
oact parameter.
|
NULL
| NULL
| No action.
|
NULL
| Pointer
| The signal action before sigaction function processing occurs
is stored in the sigaction data structure specified by the
oact parameter.
|
- The following fields are defined in the sigaction data
structure:
- sa_handler
- The action to be associated with the signal specified by the
sig parameter. This parameter is specified as one of the
following:
- SIG_DFL
- Indicates that the default action defined for the signal specified by the
sig parameter is to be taken if this signal is raised.
- SIG_IGN
- Indicates that the signal specified by the sig parameter is to
be ignored.
- A pointer
- To a function that is given control if the signal specified by the
sig parameter is raised.
- sa_mask
- A signal mask of type sigset_t that specifies an additional set
of signals to be blocked during processing by the signal handling function
specified by the sa_handler field.
- sa_flags
- Flags that affect the behavior of the signal. Any combination of
the following flags defined in the signal.h header file can
be set on in the sa_flags field.
- SA_RESETHAND
- If set on, the disposition of the signal handler is automatically reset to
SIG_DFL, and the SA_SIGINFO flag is set off at entry to the signal
handler. Additionally, if the SA_RESETHAND flag is set on, the
sigaction function behaves as if the SA_NODEFER flag is set
on.
- SA_SIGINFO
- This flag controls how the signal handler specified by the
sa_handler field is entered.
If set off, the signal handler prototype is:
void func(int signo);
If set on, the signal handler prototype is:
void func(int signo, siginfo_t *info, void *context);
The info parameter in the prototype is a pointer to a
siginfo_t data structure, which is defined in the
signal.h header file and which describes why the signal was
generated. The context parameter in the prototype is always
NULL.
- SA_NOCLDWAIT
- When the signal specified by the sig parameter is SIGCHLD and
if the SA_NOCLDWAIT flag is set on, exit status is not saved for the child
processes of the calling process. However, a SIGCHLD signal is sent to
the calling process when a child process exits.
Otherwise this flag is ignored.
- SA_NODEFER
- If set on when the signal specified by the sig parameter is
raised, this signal is not added to the signal mask of the calling process on
entry to the signal handler unless it is included in the sa_mask
field.
If set off when the signal specified by the sig parameter is
raised, this signal is added to the signal mask of the calling process on
entry to the signal handler.
Examples
The following example establishes a signal handler by using the
sigaction function.
#include <sysapi.h>
#include <signal.h>
·
·
·
{
void ChldHndlr(int, siginfo_t *, void *);
struct tpf_fork_input create_parameters;
struct sigaction act, oact;
pid_t child_pid;
/* initialize the new signal action */
act.sa_handler = ChldHndlr;
sigempty
set(&act.sa_mask);
act.sa_flags = 0;
/* install SIG_CHLD signal handler */
sigaction(SIG_CHLD, &act, &oact);
/* processing loop */
·
·
·
/* set up create_parameters */
create_parameters.program = "/bin/usr/usr1/app1.exe"
·
·
·
child_pid = tpf_fork(&create_parameters);
·
·
·
/* end of processing loop */
return;
}
void ChldHndlr(int signo, siginfo_t *info, void *context) {
int chld_state;
{
/* If someone used kill() to send SIGCHLD, ignore it */
if (info.si_si_code != SI_USER)
{
exited_pid = wait( &chld_state );
/* query return status */
·
·
·
}
return;
}
Related Information