1SIGACTION(3P)              POSIX Programmer's Manual             SIGACTION(3P)
2
3
4

PROLOG

6       This  manual  page is part of the POSIX Programmer's Manual.  The Linux
7       implementation of this interface may differ (consult the  corresponding
8       Linux  manual page for details of Linux behavior), or the interface may
9       not be implemented on Linux.
10

NAME

12       sigaction — examine and change a signal action
13

SYNOPSIS

15       #include <signal.h>
16
17       int sigaction(int sig, const struct sigaction *restrict act,
18           struct sigaction *restrict oact);
19

DESCRIPTION

21       The sigaction() function allows the calling process to  examine  and/or
22       specify  the  action to be associated with a specific signal. The argu‐
23       ment sig specifies the signal; acceptable values are defined  in  <sig‐
24       nal.h>.
25
26       The  structure  sigaction,  used  to describe an action to be taken, is
27       defined in the <signal.h> header to include at least the following mem‐
28       bers:
29
30      ┌────────────────┬───────────────┬───────────────────────────────────────┐
31Member Type   Member Name   Description               
32      ├────────────────┼───────────────┼───────────────────────────────────────┤
33void(*) (int)   sa_handler    │Pointer to a signal-catching function  │
34      │                │               │or one of the macros SIG_IGN or        │
35      │                │               │SIG_DFL.                               │
36sigset_t        sa_mask       │Additional set of signals to be        │
37      │                │               │blocked during execution of signal-    │
38      │                │               │catching function.                     │
39int             sa_flags      │Special flags to affect behavior of    │
40      │                │               │signal.                                │
41void(*) (int,   sa_sigaction  │Pointer to a signal-catching function. │
42siginfo_t *,  │               │                                       │
43void *)         │               │                                       │
44      └────────────────┴───────────────┴───────────────────────────────────────┘
45       The  storage occupied by sa_handler and sa_sigaction may overlap, and a
46       conforming application shall not use both simultaneously.
47
48       If the argument act is not a null pointer, it  points  to  a  structure
49       specifying  the  action  to be associated with the specified signal. If
50       the argument oact is not a null pointer, the action previously  associ‐
51       ated  with the signal is stored in the location pointed to by the argu‐
52       ment oact.  If the argument act is a null pointer, signal  handling  is
53       unchanged; thus, the call can be used to enquire about the current han‐
54       dling of a given signal. The SIGKILL and SIGSTOP signals shall  not  be
55       added  to  the signal mask using this mechanism; this restriction shall
56       be enforced by the system without causing an error to be indicated.
57
58       If the SA_SIGINFO flag (see below) is cleared in the sa_flags field  of
59       the  sigaction structure, the sa_handler field identifies the action to
60       be associated with the specified signal.  If the SA_SIGINFO flag is set
61       in the sa_flags field, the sa_sigaction field specifies a signal-catch‐
62       ing function.
63
64       The sa_flags field can be used to modify the behavior of the  specified
65       signal.
66
67       The  following  flags,  defined in the <signal.h> header, can be set in
68       sa_flags:
69
70       SA_NOCLDSTOP  Do not generate SIGCHLD when  children  stop  or  stopped
71                     children continue.
72
73                     If sig is SIGCHLD and the SA_NOCLDSTOP flag is not set in
74                     sa_flags, and the  implementation  supports  the  SIGCHLD
75                     signal,  then a SIGCHLD signal shall be generated for the
76                     calling process whenever any of its child processes  stop
77                     and  a  SIGCHLD  signal  may be generated for the calling
78                     process whenever any of its stopped child  processes  are
79                     continued.   If  sig is SIGCHLD and the SA_NOCLDSTOP flag
80                     is set in sa_flags, then  the  implementation  shall  not
81                     generate a SIGCHLD signal in this way.
82
83       SA_ONSTACK    If  set  and  an alternate signal stack has been declared
84                     with sigaltstack(), the signal shall be delivered to  the
85                     calling  process  on  that  stack.  Otherwise, the signal
86                     shall be delivered on the current stack.
87
88       SA_RESETHAND  If set, the disposition of the signal shall be  reset  to
89                     SIG_DFL and the SA_SIGINFO flag shall be cleared on entry
90                     to the signal handler.
91
92                     Note:     SIGILL  and  SIGTRAP  cannot  be  automatically
93                               reset   when  delivered;  the  system  silently
94                               enforces this restriction.
95
96                     Otherwise, the disposition of the  signal  shall  not  be
97                     modified on entry to the signal handler.
98
99                     In  addition, if this flag is set, sigaction() may behave
100                     as if the SA_NODEFER flag were also set.
101
102       SA_RESTART    This flag affects the  behavior  of  interruptible  func‐
103                     tions; that is, those specified to fail with errno set to
104                     [EINTR].  If set, and a function specified as  interrupt‐
105                     ible  is  interrupted  by this signal, the function shall
106                     restart and shall not fail with [EINTR] unless  otherwise
107                     specified.  If  an  interruptible  function  which uses a
108                     timeout is restarted, the duration of the timeout follow‐
109                     ing  the restart is set to an unspecified value that does
110                     not exceed the original timeout value. If the flag is not
111                     set,  interruptible  functions interrupted by this signal
112                     shall fail with errno set to [EINTR].
113
114       SA_SIGINFO    If cleared and the signal is caught, the  signal-catching
115                     function shall be entered as:
116
117
118                         void func(int signo);
119
120                     where  signo  is the only argument to the signal-catching
121                     function. In this case, the  application  shall  use  the
122                     sa_handler  member  to describe the signal-catching func‐
123                     tion and the application shall not modify  the  sa_sigac‐
124                     tion member.
125
126                     If  SA_SIGINFO  is set and the signal is caught, the sig‐
127                     nal-catching function shall be entered as:
128
129
130                         void func(int signo, siginfo_t *info, void *context);
131
132                     where two additional arguments are passed to the  signal-
133                     catching  function. The second argument shall point to an
134                     object of type siginfo_t explaining the  reason  why  the
135                     signal was generated; the third argument can be cast to a
136                     pointer to an object of type ucontext_t to refer  to  the
137                     receiving  thread's context that was interrupted when the
138                     signal was delivered. In this case, the application shall
139                     use the sa_sigaction member to describe the signal-catch‐
140                     ing function and the application  shall  not  modify  the
141                     sa_handler member.
142
143                     The  si_signo member contains the system-generated signal
144                     number.
145
146                     The si_errno member  may  contain  implementation-defined
147                     additional error information; if non-zero, it contains an
148                     error number identifying the condition  that  caused  the
149                     signal to be generated.
150
151                     The  si_code member contains a code identifying the cause
152                     of the signal, as  described  in  Section  2.4.3,  Signal
153                     Actions.
154
155       SA_NOCLDWAIT  If  sig  does not equal SIGCHLD, the behavior is unspeci‐
156                     fied. Otherwise, the behavior of the SA_NOCLDWAIT flag is
157                     as specified in Consequences of Process Termination.
158
159       SA_NODEFER    If  set  and sig is caught, sig shall not be added to the
160                     thread's signal mask  on  entry  to  the  signal  handler
161                     unless  it  is included in sa_mask.  Otherwise, sig shall
162                     always be added to the thread's signal mask on  entry  to
163                     the signal handler.
164
165       When  a  signal  is  caught  by a signal-catching function installed by
166       sigaction(), a new signal mask is  calculated  and  installed  for  the
167       duration  of  the  signal-catching  function (or until a call to either
168       sigprocmask() or sigsuspend() is made). This mask is formed  by  taking
169       the  union  of the current signal mask and the value of the sa_mask for
170       the signal being delivered, and unless SA_NODEFER  or  SA_RESETHAND  is
171       set,  then including the signal being delivered. If and when the user's
172       signal handler returns normally, the original signal mask is restored.
173
174       Once an action is installed for a  specific  signal,  it  shall  remain
175       installed until another action is explicitly requested (by another call
176       to sigaction()), until the SA_RESETHAND flag causes  resetting  of  the
177       handler, or until one of the exec functions is called.
178
179       If  the  previous  action for sig had been established by signal(), the
180       values of the fields returned in the structure pointed to by  oact  are
181       unspecified,  and in particular oact->sa_handler is not necessarily the
182       same value passed to signal().  However,  if  a  pointer  to  the  same
183       structure  or  a  copy thereof is passed to a subsequent call to sigac‐
184       tion() via the act argument, handling of the signal shall be as if  the
185       original call to signal() were repeated.
186
187       If sigaction() fails, no new signal handler is installed.
188
189       It  is  unspecified  whether  an attempt to set the action for a signal
190       that cannot be caught or ignored to SIG_DFL is  ignored  or  causes  an
191       error to be returned with errno set to [EINVAL].
192
193       If  SA_SIGINFO  is  not set in sa_flags, then the disposition of subse‐
194       quent occurrences of sig when it is already pending is  implementation-
195       defined;  the  signal-catching  function shall be invoked with a single
196       argument.  If SA_SIGINFO is set in  sa_flags,  then  subsequent  occur‐
197       rences of sig generated by sigqueue() or as a result of any signal-gen‐
198       erating function that supports the  specification  of  an  application-
199       defined  value  (when  sig  is already pending) shall be queued in FIFO
200       order until delivered or accepted; the signal-catching  function  shall
201       be  invoked  with  three  arguments. The application specified value is
202       passed to the signal-catching function as the si_value  member  of  the
203       siginfo_t structure.
204
205       The  result  of the use of sigaction() and a sigwait() function concur‐
206       rently within a process on the same signal is unspecified.
207

RETURN VALUE

209       Upon successful completion, sigaction() shall return 0;  otherwise,  -1
210       shall be returned, errno shall be set to indicate the error, and no new
211       signal-catching function shall be installed.
212

ERRORS

214       The sigaction() function shall fail if:
215
216       EINVAL The sig argument is not a valid signal number or an  attempt  is
217              made  to catch a signal that cannot be caught or ignore a signal
218              that cannot be ignored.
219
220       The sigaction() function may fail if:
221
222       EINVAL An attempt was made to set the action to SIG_DFL  for  a  signal
223              that cannot be caught or ignored (or both).
224
225       In  addition, on systems that do not support the XSI option, the sigac‐
226       tion() function may fail if the SA_SIGINFO flag is set in the  sa_flags
227       field of the sigaction structure for a signal not in the range SIGRTMIN
228       to SIGRTMAX.
229
230       The following sections are informative.
231

EXAMPLES

233   Establishing a Signal Handler
234       The following example demonstrates the use of sigaction() to  establish
235       a handler for the SIGINT signal.
236
237
238           #include <signal.h>
239
240           static void handler(int signum)
241           {
242               /* Take appropriate actions for signal delivery */
243           }
244
245           int main()
246           {
247               struct sigaction sa;
248
249               sa.sa_handler = handler;
250               sigemptyset(&sa.sa_mask);
251               sa.sa_flags = SA_RESTART; /* Restart functions if
252                                            interrupted by handler */
253               if (sigaction(SIGINT, &sa, NULL) == -1)
254                   /* Handle error */;
255
256               /* Further code */
257           }
258

APPLICATION USAGE

260       The  sigaction()  function supersedes the signal() function, and should
261       be used in preference. In particular, sigaction() and  signal()  should
262       not be used in the same process to control the same signal.  The behav‐
263       ior of async-signal-safe functions,  as  defined  in  their  respective
264       DESCRIPTION  sections,  is as specified by this volume of POSIX.1‐2017,
265       regardless of invocation from a signal-catching function. This  is  the
266       only intended meaning of the statement that async-signal-safe functions
267       may be used in signal-catching functions without restrictions. Applica‐
268       tions  must still consider all effects of such functions on such things
269       as data structures, files, and process state. In  particular,  applica‐
270       tion  developers need to consider the restrictions on interactions when
271       interrupting sleep() and interactions among multiple handles for a file
272       description.  The  fact  that any specific function is listed as async-
273       signal-safe does not necessarily mean that invocation of that  function
274       from a signal-catching function is recommended.
275
276       In  order to prevent errors arising from interrupting non-async-signal-
277       safe function calls, applications should protect calls to  these  func‐
278       tions  either by blocking the appropriate signals or through the use of
279       some programmatic semaphore (see semget(), sem_init(), sem_open(),  and
280       so  on). Note in particular that even the ``safe'' functions may modify
281       errno; the signal-catching function, if not executing as an independent
282       thread,  should save and restore its value in order to avoid the possi‐
283       bility that delivery of a signal in between  an  error  return  from  a
284       function  that sets errno and the subsequent examination of errno could
285       result in the signal-catching function changing  the  value  of  errno.
286       Naturally,  the  same  principles  apply  to the async-signal-safety of
287       application routines and asynchronous data access. Note that  longjmp()
288       and  siglongjmp()  are  not in the list of async-signal-safe functions.
289       This is because the code executing after longjmp() and siglongjmp() can
290       call  any unsafe functions with the same danger as calling those unsafe
291       functions directly from  the  signal  handler.  Applications  that  use
292       longjmp() and siglongjmp() from within signal handlers require rigorous
293       protection in order to be portable. Many of the  other  functions  that
294       are  excluded  from the list are traditionally implemented using either
295       malloc() or free() functions or the standard I/O library, both of which
296       traditionally  use  data  structures in a non-async-signal-safe manner.
297       Since any combination of different functions using a common data struc‐
298       ture   can   cause   async-signal-safety   problems,   this  volume  of
299       POSIX.1‐2017 does not define the behavior when any unsafe  function  is
300       called in a signal handler that interrupts an unsafe function.
301
302       Usually,  the signal is executed on the stack that was in effect before
303       the signal was delivered.  An  alternate  stack  may  be  specified  to
304       receive a subset of the signals being caught.
305
306       When the signal handler returns, the receiving thread resumes execution
307       at the point it was interrupted unless the signal handler  makes  other
308       arrangements.  If  longjmp()  or _longjmp() is used to leave the signal
309       handler, then the signal mask must be explicitly restored.
310
311       This volume of POSIX.1‐2017 defines the third argument of a signal han‐
312       dling  function  when  SA_SIGINFO is set as a void * instead of a ucon‐
313       text_t *, but without requiring type checking. New applications  should
314       explicitly  cast  the third argument of the signal handling function to
315       ucontext_t *.
316
317       The BSD optional four argument signal handling  function  is  not  sup‐
318       ported by this volume of POSIX.1‐2017. The BSD declaration would be:
319
320
321           void handler(int sig, int code, struct sigcontext *scp,
322               char *addr);
323
324       where  sig is the signal number, code is additional information on cer‐
325       tain signals, scp is a pointer to the sigcontext structure, and addr is
326       additional  address information. Much the same information is available
327       in the objects pointed to by the second argument of the signal  handler
328       specified when SA_SIGINFO is set.
329
330       Since  the  sigaction()  function  is  allowed  but not required to set
331       SA_NODEFER when the application sets the  SA_RESETHAND  flag,  applica‐
332       tions  which  depend  on  the  SA_RESETHAND functionality for the newly
333       installed signal handler must always  explicitly  set  SA_NODEFER  when
334       they set SA_RESETHAND in order to be portable.
335
336       See  also  the rationale for Realtime Signal Generation and Delivery in
337       the Rationale (Informative) volume of  POSIX.1‐2017,  Section  B.2.4.2,
338       Signal Generation and Delivery.
339

RATIONALE

341       Although  this volume of POSIX.1‐2017 requires that signals that cannot
342       be ignored shall not be added to the signal mask when a signal-catching
343       function  is  entered, there is no explicit requirement that subsequent
344       calls to sigaction() reflect this in the information  returned  in  the
345       oact  argument.  In  other words, if SIGKILL is included in the sa_mask
346       field of act, it is unspecified whether or not  a  subsequent  call  to
347       sigaction() returns with SIGKILL included in the sa_mask field of oact.
348
349       The  SA_NOCLDSTOP  flag,  when supplied in the act->sa_flags parameter,
350       allows overloading SIGCHLD with the System V semantics that each SIGCLD
351       signal  indicates  a  single terminated child. Most conforming applica‐
352       tions that catch SIGCHLD are expected to install signal-catching  func‐
353       tions that repeatedly call the waitpid() function with the WNOHANG flag
354       set, acting on each child for which status is returned, until waitpid()
355       returns  zero.  If stopped children are not of interest, the use of the
356       SA_NOCLDSTOP flag can prevent the overhead from  invoking  the  signal-
357       catching routine when they stop.
358
359       Some  historical implementations also define other mechanisms for stop‐
360       ping processes, such as the ptrace()  function.  These  implementations
361       usually  do  not  generate  a SIGCHLD signal when processes stop due to
362       this mechanism; however, that is beyond the scope  of  this  volume  of
363       POSIX.1‐2017.
364
365       This  volume  of  POSIX.1‐2017  requires that calls to sigaction() that
366       supply a NULL act argument succeed, even in the case  of  signals  that
367       cannot  be caught or ignored (that is, SIGKILL or SIGSTOP).  The System
368       V signal() and BSD sigvec() functions return [EINVAL]  in  these  cases
369       and, in this respect, their behavior varies from sigaction().
370
371       This volume of POSIX.1‐2017 requires that sigaction() properly save and
372       restore a signal action set up by the ISO C standard signal() function.
373       However,  there  is  no  guarantee  that the reverse is true, nor could
374       there be given the greater amount of information conveyed by the sigac‐
375       tion  structure.  Because of this, applications should avoid using both
376       functions for the same signal in the same process.  Since  this  cannot
377       always  be  avoided  in  case of general-purpose library routines, they
378       should always be implemented with sigaction().
379
380       It was intended that the signal() function should be implementable as a
381       library routine using sigaction().
382
383       The POSIX Realtime Extension extends the sigaction() function as speci‐
384       fied by the POSIX.1‐1990 standard to allow the application  to  request
385       on  a  per-signal  basis  via an additional signal action flag that the
386       extra parameters, including the application-defined  signal  value,  if
387       any, be passed to the signal-catching function.
388

FUTURE DIRECTIONS

390       None.
391

SEE ALSO

393       Section  2.4,  Signal  Concepts,  exec,  _Exit(),  kill(),  _longjmp(),
394       longjmp(),   pthread_sigmask(),    raise(),    semget(),    sem_init(),
395       sem_open(),  sigaddset(),  sigaltstack(),  sigdelset(),  sigemptyset(),
396       sigfillset(), sigismember(), signal(), sigsuspend(), wait(), waitid()
397
398       The Base Definitions volume of POSIX.1‐2017, <signal.h>
399
401       Portions of this text are reprinted and reproduced in  electronic  form
402       from  IEEE Std 1003.1-2017, Standard for Information Technology -- Por‐
403       table Operating System Interface (POSIX), The Open Group Base  Specifi‐
404       cations  Issue  7, 2018 Edition, Copyright (C) 2018 by the Institute of
405       Electrical and Electronics Engineers, Inc and The Open Group.   In  the
406       event of any discrepancy between this version and the original IEEE and
407       The Open Group Standard, the original IEEE and The Open Group  Standard
408       is  the  referee document. The original Standard can be obtained online
409       at http://www.opengroup.org/unix/online.html .
410
411       Any typographical or formatting errors that appear  in  this  page  are
412       most likely to have been introduced during the conversion of the source
413       files to man page format. To report such errors,  see  https://www.ker
414       nel.org/doc/man-pages/reporting_bugs.html .
415
416
417
418IEEE/The Open Group                  2017                        SIGACTION(3P)
Impressum