1SIGACTION(2)               Linux Programmer's Manual              SIGACTION(2)
2
3
4

NAME

6       sigaction - examine and change a signal action
7

SYNOPSIS

9       #include <signal.h>
10
11       int sigaction(int signum, const struct sigaction *act,
12                     struct sigaction *oldact);
13
14   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
15
16       sigaction(): _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _POSIX_SOURCE
17
18       siginfo_t: _POSIX_C_SOURCE >= 199309L
19

DESCRIPTION

21       The  sigaction()  system  call  is used to change the action taken by a
22       process on receipt of a specific signal.  (See signal(7) for  an  over‐
23       view of signals.)
24
25       signum  specifies the signal and can be any valid signal except SIGKILL
26       and SIGSTOP.
27
28       If act is non-NULL, the new action for signal signum is installed  from
29       act.  If oldact is non-NULL, the previous action is saved in oldact.
30
31       The sigaction structure is defined as something like:
32
33           struct sigaction {
34               void     (*sa_handler)(int);
35               void     (*sa_sigaction)(int, siginfo_t *, void *);
36               sigset_t   sa_mask;
37               int        sa_flags;
38               void     (*sa_restorer)(void);
39           };
40
41       On  some  architectures  a  union  is  involved:  do not assign to both
42       sa_handler and sa_sigaction.
43
44       The sa_restorer element is obsolete and should not be used.  POSIX does
45       not specify a sa_restorer element.
46
47       sa_handler specifies the action to be associated with signum and may be
48       SIG_DFL for the default action, SIG_IGN to ignore  this  signal,  or  a
49       pointer to a signal handling function.  This function receives the sig‐
50       nal number as its only argument.
51
52       If SA_SIGINFO is specified in sa_flags, then sa_sigaction  (instead  of
53       sa_handler)  specifies  the  signal-handling function for signum.  This
54       function receives the signal number as its first argument, a pointer to
55       a  siginfo_t as its second argument and a pointer to a ucontext_t (cast
56       to void *) as its third  argument.   (Commonly,  the  handler  function
57       doesn't make any use of the third argument.  See getcontext(3) for fur‐
58       ther information about ucontext_t.)
59
60       sa_mask specifies a mask of signals  which  should  be  blocked  (i.e.,
61       added  to  the signal mask of the thread in which the signal handler is
62       invoked) during execution of the signal handler.  In addition, the sig‐
63       nal  which triggered the handler will be blocked, unless the SA_NODEFER
64       flag is used.
65
66       sa_flags specifies a set of flags which modify the behavior of the sig‐
67       nal.  It is formed by the bitwise OR of zero or more of the following:
68
69           SA_NOCLDSTOP
70                  If signum is SIGCHLD, do not receive notification when child
71                  processes stop (i.e., when  they  receive  one  of  SIGSTOP,
72                  SIGTSTP,  SIGTTIN  or SIGTTOU) or resume (i.e., they receive
73                  SIGCONT) (see wait(2)).  This flag is meaningful  only  when
74                  establishing a handler for SIGCHLD.
75
76           SA_NOCLDWAIT (since Linux 2.6)
77                  If signum is SIGCHLD, do not transform children into zombies
78                  when they terminate.  See also  waitpid(2).   This  flag  is
79                  meaningful  only when establishing a handler for SIGCHLD, or
80                  when setting that signal's disposition to SIG_DFL.
81
82                  If the SA_NOCLDWAIT flag is set when establishing a  handler
83                  for SIGCHLD, POSIX.1 leaves it unspecified whether a SIGCHLD
84                  signal is generated when a  child  process  terminates.   On
85                  Linux,  a  SIGCHLD signal is generated in this case; on some
86                  other implementations, it is not.
87
88           SA_NODEFER
89                  Do not prevent the signal from being  received  from  within
90                  its  own  signal handler.  This flag is meaningful only when
91                  establishing a signal handler.  SA_NOMASK  is  an  obsolete,
92                  nonstandard synonym for this flag.
93
94           SA_ONSTACK
95                  Call  the  signal  handler on an alternate signal stack pro‐
96                  vided by sigaltstack(2).   If  an  alternate  stack  is  not
97                  available,  the  default  stack  will be used.  This flag is
98                  meaningful only when establishing a signal handler.
99
100           SA_RESETHAND
101                  Restore the signal action to the default upon entry  to  the
102                  signal  handler.   This  flag is meaningful only when estab‐
103                  lishing a signal handler.  SA_ONESHOT is an  obsolete,  non‐
104                  standard synonym for this flag.
105
106           SA_RESTART
107                  Provide  behavior  compatible  with  BSD signal semantics by
108                  making certain  system  calls  restartable  across  signals.
109                  This flag is meaningful only when establishing a signal han‐
110                  dler.   See  signal(7)  for  a  discussion  of  system  call
111                  restarting.
112
113           SA_SIGINFO (since Linux 2.2)
114                  The  signal handler takes three arguments, not one.  In this
115                  case, sa_sigaction should  be  set  instead  of  sa_handler.
116                  This flag is meaningful only when establishing a signal han‐
117                  dler.
118
119       The siginfo_t argument to sa_sigaction is a struct with  the  following
120       elements:
121
122           siginfo_t {
123               int      si_signo;    /* Signal number */
124               int      si_errno;    /* An errno value */
125               int      si_code;     /* Signal code */
126               int      si_trapno;   /* Trap number that caused
127                                        hardware-generated signal
128                                        (unused on most architectures) */
129               pid_t    si_pid;      /* Sending process ID */
130               uid_t    si_uid;      /* Real user ID of sending process */
131               int      si_status;   /* Exit value or signal */
132               clock_t  si_utime;    /* User time consumed */
133               clock_t  si_stime;    /* System time consumed */
134               sigval_t si_value;    /* Signal value */
135               int      si_int;      /* POSIX.1b signal */
136               void    *si_ptr;      /* POSIX.1b signal */
137               int      si_overrun;  /* Timer overrun count; POSIX.1b timers */
138               int      si_timerid;  /* Timer ID; POSIX.1b timers */
139               void    *si_addr;     /* Memory location which caused fault */
140               long     si_band;     /* Band event (was int in
141                                        glibc 2.3.2 and earlier) */
142               int      si_fd;       /* File descriptor */
143               short    si_addr_lsb; /* Least significant bit of address
144                                        (since Linux 2.6.32) */
145           }
146
147       si_signo,  si_errno and si_code are defined for all signals.  (si_errno
148       is generally unused on Linux.)  The rest of the struct may be a  union,
149       so  that  one  should  read only the fields that are meaningful for the
150       given signal:
151
152       * Signals sent with kill(2) and sigqueue(3) fill in si_pid and  si_uid.
153         In  addition, signals sent with sigqueue(3) fill in si_int and si_ptr
154         with  the  values  specified  by  the  sender  of  the  signal;   see
155         sigqueue(3) for more details.
156
157       * Signals  sent by POSIX.1b timers (since Linux 2.6) fill in si_overrun
158         and si_timerid.  The si_timerid field is an internal ID used  by  the
159         kernel  to  identify  the  timer;  it is not the same as the timer ID
160         returned by timer_create(2).  The si_overrun field is the timer over‐
161         run  count;  this is the same information as is obtained by a call to
162         timer_getoverrun(2).  These fields are nonstandard Linux extensions.
163
164       * Signals sent for message queue notification (see the  description  of
165         SIGEV_SIGNAL   in  mq_notify(3))  fill  in  si_int/si_ptr,  with  the
166         sigev_value supplied to mq_notify(3); si_pid, with the process ID  of
167         the  message sender; and si_uid, with the real user ID of the message
168         sender.
169
170       * SIGCHLD fills in si_pid, si_uid, si_status,  si_utime  and  si_stime,
171         providing  information  about  the  child.   The  si_pid field is the
172         process ID of the child; si_uid is the child's  real  user  ID.   The
173         si_status  field contains the exit status of the child (if si_code is
174         CLD_EXITED), or the signal number that caused the process  to  change
175         state.   The  si_utime  and  si_stime contain the user and system CPU
176         time used by the child process; these fields do not include the times
177         used  by  waited-for  children (unlike getrusage(2) and time(2)).  In
178         kernels up to 2.6, and since 2.6.27, these fields report CPU time  in
179         units  of  sysconf(_SC_CLK_TCK).  In 2.6 kernels before 2.6.27, a bug
180         meant that these fields reported time in units of the  (configurable)
181         system jiffy (see time(7)).
182
183       * SIGILL, SIGFPE, SIGSEGV, SIGBUS, and SIGTRAP fill in si_addr with the
184         address of the fault.  On some architectures, these signals also fill
185         in  the  si_trapno  filed.   Some  suberrors of SIGBUS, in particular
186         BUS_MCEERR_AO and BUS_MCEERR_AR,  also  fill  in  si_addr_lsb.   This
187         field indicates the least significant bit of the reported address and
188         therefore the extent of the corruption.  For example, if a full  page
189         was   corrupted,  si_addr_lsb  contains  log2(sysconf(_SC_PAGESIZE)).
190         BUS_MCERR_* and si_addr_lsb are Linux-specific extensions.
191
192       * SIGIO/SIGPOLL (the two names are synonyms on Linux) fills in  si_band
193         and  si_fd.  The si_band event is a bit mask containing the same val‐
194         ues as are filled in the revents field by poll(2).  The  si_fd  field
195         indicates the file descriptor for which the I/O event occurred.
196
197       si_code  is  a  value  (not  a bit mask) indicating why this signal was
198       sent.  The following list shows the  values  which  can  be  placed  in
199       si_code  for  any  signal, along with reason that the signal was gener‐
200       ated.
201
202           SI_USER        kill(2)
203
204           SI_KERNEL      Sent by the kernel.
205
206           SI_QUEUE       sigqueue(3)
207
208           SI_TIMER       POSIX timer expired
209
210           SI_MESGQ       POSIX  message  queue  state  changed  (since  Linux
211                          2.6.6); see mq_notify(3)
212
213           SI_ASYNCIO     AIO completed
214
215           SI_SIGIO       Queued  SIGIO (only in kernels up to Linux 2.2; from
216                          Linux 2.4 onward SIGIO/SIGPOLL fills in  si_code  as
217                          described below).
218
219           SI_TKILL       tkill(2) or tgkill(2) (since Linux 2.4.19)
220
221       The following values can be placed in si_code for a SIGILL signal:
222
223           ILL_ILLOPC     illegal opcode
224
225           ILL_ILLOPN     illegal operand
226
227           ILL_ILLADR     illegal addressing mode
228
229           ILL_ILLTRP     illegal trap
230
231           ILL_PRVOPC     privileged opcode
232
233           ILL_PRVREG     privileged register
234
235           ILL_COPROC     coprocessor error
236
237           ILL_BADSTK     internal stack error
238
239       The following values can be placed in si_code for a SIGFPE signal:
240
241           FPE_INTDIV     integer divide by zero
242
243           FPE_INTOVF     integer overflow
244
245           FPE_FLTDIV     floating-point divide by zero
246
247           FPE_FLTOVF     floating-point overflow
248
249           FPE_FLTUND     floating-point underflow
250
251           FPE_FLTRES     floating-point inexact result
252
253           FPE_FLTINV     floating-point invalid operation
254
255           FPE_FLTSUB     subscript out of range
256
257       The following values can be placed in si_code for a SIGSEGV signal:
258
259           SEGV_MAPERR    address not mapped to object
260
261           SEGV_ACCERR    invalid permissions for mapped object
262
263       The following values can be placed in si_code for a SIGBUS signal:
264
265           BUS_ADRALN     invalid address alignment
266
267           BUS_ADRERR     nonexistent physical address
268
269           BUS_OBJERR     object-specific hardware error
270
271           BUS_MCEERR_AR (since Linux 2.6.32)
272                          Hardware  memory  error consumed on a machine check;
273                          action required.
274
275           BUS_MCEERR_AO (since Linux 2.6.32)
276                          Hardware memory error detected in  process  but  not
277                          consumed; action optional.
278
279       The following values can be placed in si_code for a SIGTRAP signal:
280
281           TRAP_BRKPT     process breakpoint
282
283           TRAP_TRACE     process trace trap
284
285           TRAP_BRANCH (since Linux 2.4)
286                          process taken branch trap
287
288           TRAP_HWBKPT (since Linux 2.4)
289                          hardware breakpoint/watchpoint
290
291       The following values can be placed in si_code for a SIGCHLD signal:
292
293           CLD_EXITED     child has exited
294
295           CLD_KILLED     child was killed
296
297           CLD_DUMPED     child terminated abnormally
298
299           CLD_TRAPPED    traced child has trapped
300
301           CLD_STOPPED    child has stopped
302
303           CLD_CONTINUED  stopped child has continued (since Linux 2.6.9)
304
305       The  following values can be placed in si_code for a SIGIO/SIGPOLL sig‐
306       nal:
307
308           POLL_IN        data input available
309
310           POLL_OUT       output buffers available
311
312           POLL_MSG       input message available
313
314           POLL_ERR       I/O error
315
316           POLL_PRI       high priority input available
317
318           POLL_HUP       device disconnected
319

RETURN VALUE

321       sigaction() returns 0 on success; on error, -1 is returned,  and  errno
322       is set to indicate the error.
323

ERRORS

325       EFAULT act  or oldact points to memory which is not a valid part of the
326              process address space.
327
328       EINVAL An invalid signal was specified.  This will also be generated if
329              an  attempt is made to change the action for SIGKILL or SIGSTOP,
330              which cannot be caught or ignored.
331

CONFORMING TO

333       POSIX.1-2001, SVr4.
334

NOTES

336       A child created via fork(2) inherits a copy of its parent's signal dis‐
337       positions.   During  an  execve(2), the dispositions of handled signals
338       are reset to the default; the dispositions of ignored signals are  left
339       unchanged.
340
341       According  to  POSIX,  the  behavior of a process is undefined after it
342       ignores a SIGFPE, SIGILL, or SIGSEGV signal that was not  generated  by
343       kill(2)  or  raise(3).   Integer division by zero has undefined result.
344       On some architectures it will generate a SIGFPE signal.  (Also dividing
345       the  most  negative  integer by -1 may generate SIGFPE.)  Ignoring this
346       signal might lead to an endless loop.
347
348       POSIX.1-1990 disallowed setting the  action  for  SIGCHLD  to  SIG_IGN.
349       POSIX.1-2001  allows  this possibility, so that ignoring SIGCHLD can be
350       used to prevent the creation of zombies (see  wait(2)).   Nevertheless,
351       the  historical BSD and System V behaviors for ignoring SIGCHLD differ,
352       so that the only completely portable method of ensuring that terminated
353       children  do not become zombies is to catch the SIGCHLD signal and per‐
354       form a wait(2) or similar.
355
356       POSIX.1-1990 specified only SA_NOCLDSTOP.  POSIX.1-2001 added SA_NOCLD‐
357       WAIT,  SA_RESETHAND,  SA_NODEFER,  and SA_SIGINFO.  Use of these latter
358       values in sa_flags may be less portable in  applications  intended  for
359       older UNIX implementations.
360
361       The  SA_RESETHAND  flag  is  compatible  with the SVr4 flag of the same
362       name.
363
364       The SA_NODEFER flag is compatible with the SVr4 flag of the  same  name
365       under  kernels 1.3.9 and newer.  On older kernels the Linux implementa‐
366       tion allowed the receipt of  any  signal,  not  just  the  one  we  are
367       installing (effectively overriding any sa_mask settings).
368
369       sigaction() can be called with a NULL second argument to query the cur‐
370       rent signal handler.  It can also be used to check whether a given sig‐
371       nal is valid for the current machine by calling it with NULL second and
372       third arguments.
373
374       It is not possible to block SIGKILL or SIGSTOP (by specifying  them  in
375       sa_mask).  Attempts to do so are silently ignored.
376
377       See sigsetops(3) for details on manipulating signal sets.
378
379       See signal(7) for a list of the async-signal-safe functions that can be
380       safely called inside from inside a signal handler.
381
382   Undocumented
383       Before the introduction of SA_SIGINFO it was also possible to get  some
384       additional  information, namely by using a sa_handler with second argu‐
385       ment of type struct sigcontext.  See the relevant Linux kernel  sources
386       for details.  This use is obsolete now.
387

BUGS

389       In  kernels  up  to  and  including  2.6.13,  specifying  SA_NODEFER in
390       sa_flags prevents not only the delivered signal from being masked  dur‐
391       ing  execution  of  the  handler,  but  also  the  signals specified in
392       sa_mask.  This bug was fixed in kernel 2.6.14.
393

EXAMPLE

395       See mprotect(2).
396

SEE ALSO

398       kill(1),  kill(2),  killpg(2),  pause(2),  restart_syscall(2),  sigalt‐
399       stack(2),  signal(2),  signalfd(2), sigpending(2), sigprocmask(2), sig‐
400       suspend(2), wait(2),  raise(3),  siginterrupt(3),  sigqueue(3),  sigse‐
401       tops(3), sigvec(3), core(5), signal(7)
402

COLOPHON

404       This  page  is  part of release 3.53 of the Linux man-pages project.  A
405       description of the project, and information about reporting  bugs,  can
406       be found at http://www.kernel.org/doc/man-pages/.
407
408
409
410Linux                             2013-07-30                      SIGACTION(2)
Impressum