1SIGACTION(P) POSIX Programmer's Manual SIGACTION(P)
2
3
4
6 sigaction - examine and change a signal action
7
9 #include <signal.h>
10
11 int sigaction(int sig, const struct sigaction *restrict act,
12 struct sigaction *restrict oact);
13
14
16 The sigaction() function allows the calling process to examine and/or
17 specify the action to be associated with a specific signal. The argu‐
18 ment sig specifies the signal; acceptable values are defined in <sig‐
19 nal.h>.
20
21 The structure sigaction, used to describe an action to be taken, is
22 defined in the <signal.h> header to include at least the following mem‐
23 bers:
24
25 Member Type Member Name Description
26 void(*) (int) sa_handler Pointer to a signal-catching
27 function or one of the macros
28 SIG_IGN or SIG_DFL.
29 sigset_t sa_mask Additional set of signals to
30 be blocked during execution of
31 signal-catching function.
32 int sa_flags Special flags to affect behav‐
33 ior of signal.
34 void(*) (int,
35 siginfo_t *, sa_sigaction Pointer to a signal-catching
36 void *) function.
37
38 The storage occupied by sa_handler and sa_sigaction may overlap, and a
39 conforming application shall not use both simultaneously.
40
41 If the argument act is not a null pointer, it points to a structure
42 specifying the action to be associated with the specified signal. If
43 the argument oact is not a null pointer, the action previously associ‐
44 ated with the signal is stored in the location pointed to by the argu‐
45 ment oact. If the argument act is a null pointer, signal handling is
46 unchanged; thus, the call can be used to enquire about the current han‐
47 dling of a given signal. The SIGKILL and SIGSTOP signals shall not be
48 added to the signal mask using this mechanism; this restriction shall
49 be enforced by the system without causing an error to be indicated.
50
51 If the SA_SIGINFO flag (see below) is cleared in the sa_flags field of
52 the sigaction structure, the sa_handler field identifies the action to
53 be associated with the specified signal. If the SA_SIGINFO flag is
54 set in the sa_flags field, and the implementation supports the Realtime
55 Signals Extension option or the XSI Extension option, the sa_sigaction
56 field specifies a signal-catching function. If the SA_SIGINFO bit is
57 cleared and the sa_handler field specifies a signal-catching function,
58 or if the SA_SIGINFO bit is set, the sa_mask field identifies a set of
59 signals that shall be added to the signal mask of the thread before the
60 signal-catching function is invoked. If the sa_handler field specifies
61 a signal-catching function, the sa_mask field identifies a set of sig‐
62 nals that shall be added to the process' signal mask before the signal-
63 catching function is invoked.
64
65 The sa_flags field can be used to modify the behavior of the specified
66 signal.
67
68 The following flags, defined in the <signal.h> header, can be set in
69 sa_flags:
70
71 SA_NOCLDSTOP
72 Do not generate SIGCHLD when children stop or stopped chil‐
73 dren continue.
74
75 If sig is SIGCHLD and the SA_NOCLDSTOP flag is not set in sa_flags, and
76 the implementation supports the SIGCHLD signal, then a SIGCHLD signal
77 shall be generated for the calling process whenever any of its child
78 processes stop and a SIGCHLD signal may be generated for the calling
79 process whenever any of its stopped child processes are continued. If
80 sig is SIGCHLD and the SA_NOCLDSTOP flag is set in sa_flags, then the
81 implementation shall not generate a SIGCHLD signal in this way.
82
83 SA_ONSTACK
84 If set and an alternate signal stack has been declared with
85 sigaltstack(), the signal shall be delivered to the calling
86 process on that stack. Otherwise, the signal shall be delivered
87 on the current stack.
88
89 SA_RESETHAND
90 If set, the disposition of the signal shall be reset to SIG_DFL
91 and the SA_SIGINFO flag shall be cleared on entry to the signal
92 handler.
93
94 Note:
95 SIGILL and SIGTRAP cannot be automatically reset when delivered;
96 the system silently enforces this restriction.
97
98 Otherwise, the disposition of the signal shall not be modified on entry
99 to the signal handler.
100
101 In addition, if this flag is set, sigaction() behaves as if the
102 SA_NODEFER flag were also set.
103
104 SA_RESTART
105 This flag affects the behavior of interruptible functions; that
106 is, those specified to fail with errno set to [EINTR]. If set,
107 and a function specified as interruptible is interrupted by this
108 signal, the function shall restart and shall not fail with
109 [EINTR] unless otherwise specified. If the flag is not set,
110 interruptible functions interrupted by this signal shall fail
111 with errno set to [EINTR].
112
113 SA_SIGINFO
114 If cleared and the signal is caught, the signal-catching func‐
115 tion shall be entered as:
116
117
118 void func(int signo);
119
120 where signo is the only argument to the signal-catching function. In
121 this case, the application shall use the sa_handler member to describe
122 the signal-catching function and the application shall not modify the
123 sa_sigaction member.
124
125 If SA_SIGINFO is set and the signal is caught, the signal-catching
126 function shall be entered as:
127
128
129 void func(int signo, siginfo_t *info, void *context);
130
131 where two additional arguments are passed to the signal-catching func‐
132 tion. The second argument shall point to an object of type siginfo_t
133 explaining the reason why the signal was generated; the third argument
134 can be cast to a pointer to an object of type ucontext_t to refer to
135 the receiving process' context that was interrupted when the signal was
136 delivered. In this case, the application shall use the sa_sigaction
137 member to describe the signal-catching function and the application
138 shall not modify the sa_handler member.
139
140 The si_signo member contains the system-generated signal number.
141
142 The si_errno member may contain implementation-defined additional error
143 information; if non-zero, it contains an error number identifying the
144 condition that caused the signal to be generated.
145
146 The si_code member contains a code identifying the cause of the signal.
147
148 If the value of si_code is less than or equal to 0, then the signal was
149 generated by a process and si_pid and si_uid, respectively, indicate
150 the process ID and the real user ID of the sender. The <signal.h>
151 header description contains information about the signal-specific con‐
152 tents of the elements of the siginfo_t type.
153
154 SA_NOCLDWAIT
155 If set, and sig equals SIGCHLD, child processes of the calling
156 processes shall not be transformed into zombie processes when
157 they terminate. If the calling process subsequently waits for
158 its children, and the process has no unwaited-for children that
159 were transformed into zombie processes, it shall block until all
160 of its children terminate, and wait(), waitid(), and waitpid()
161 shall fail and set errno to [ECHILD]. Otherwise, terminating
162 child processes shall be transformed into zombie processes,
163 unless SIGCHLD is set to SIG_IGN.
164
165 SA_NODEFER
166 If set and sig is caught, sig shall not be added to the process'
167 signal mask on entry to the signal handler unless it is included
168 in sa_mask. Otherwise, sig shall always be added to the process'
169 signal mask on entry to the signal handler.
170
171
172 When a signal is caught by a signal-catching function installed by
173 sigaction(), a new signal mask is calculated and installed for the
174 duration of the signal-catching function (or until a call to either
175 sigprocmask() or sigsuspend() is made). This mask is formed by taking
176 the union of the current signal mask and the value of the sa_mask for
177 the signal being delivered unless SA_NODEFER or SA_RESETHAND is set,
178 and then including the signal being delivered. If and when the user's
179 signal handler returns normally, the original signal mask is restored.
180
181 Once an action is installed for a specific signal, it shall remain
182 installed until another action is explicitly requested (by another call
183 to sigaction()), until the SA_RESETHAND flag causes resetting of the
184 handler, or until one of the exec functions is called.
185
186 If the previous action for sig had been established by signal(), the
187 values of the fields returned in the structure pointed to by oact are
188 unspecified, and in particular oact-> sa_handler is not necessarily the
189 same value passed to signal(). However, if a pointer to the same
190 structure or a copy thereof is passed to a subsequent call to sigac‐
191 tion() via the act argument, handling of the signal shall be as if the
192 original call to signal() were repeated.
193
194 If sigaction() fails, no new signal handler is installed.
195
196 It is unspecified whether an attempt to set the action for a signal
197 that cannot be caught or ignored to SIG_DFL is ignored or causes an
198 error to be returned with errno set to [EINVAL].
199
200 If SA_SIGINFO is not set in sa_flags, then the disposition of subse‐
201 quent occurrences of sig when it is already pending is implementation-
202 defined; the signal-catching function shall be invoked with a single
203 argument. If the implementation supports the Realtime Signals Exten‐
204 sion option, and if SA_SIGINFO is set in sa_flags, then subsequent
205 occurrences of sig generated by sigqueue() or as a result of any sig‐
206 nal-generating function that supports the specification of an applica‐
207 tion-defined value (when sig is already pending) shall be queued in
208 FIFO order until delivered or accepted; the signal-catching function
209 shall be invoked with three arguments. The application specified value
210 is passed to the signal-catching function as the si_value member of the
211 siginfo_t structure.
212
213 The result of the use of sigaction() and a sigwait() function concur‐
214 rently within a process on the same signal is unspecified.
215
217 Upon successful completion, sigaction() shall return 0; otherwise, -1
218 shall be returned, errno shall be set to indicate the error, and no new
219 signal-catching function shall be installed.
220
222 The sigaction() function shall fail if:
223
224 EINVAL The sig argument is not a valid signal number or an attempt is
225 made to catch a signal that cannot be caught or ignore a signal
226 that cannot be ignored.
227
228 ENOTSUP
229 The SA_SIGINFO bit flag is set in the sa_flags field of the
230 sigaction structure, and the implementation does not support
231 either the Realtime Signals Extension option, or the XSI Exten‐
232 sion option.
233
234
235 The sigaction() function may fail if:
236
237 EINVAL An attempt was made to set the action to SIG_DFL for a signal
238 that cannot be caught or ignored (or both).
239
240
241 The following sections are informative.
242
244 None.
245
247 The sigaction() function supersedes the signal() function, and should
248 be used in preference. In particular, sigaction() and signal() should
249 not be used in the same process to control the same signal. The behav‐
250 ior of reentrant functions, as defined in the DESCRIPTION, is as speci‐
251 fied by this volume of IEEE Std 1003.1-2001, regardless of invocation
252 from a signal-catching function. This is the only intended meaning of
253 the statement that reentrant functions may be used in signal-catching
254 functions without restrictions. Applications must still consider all
255 effects of such functions on such things as data structures, files, and
256 process state. In particular, application writers need to consider the
257 restrictions on interactions when interrupting sleep() and interactions
258 among multiple handles for a file description. The fact that any spe‐
259 cific function is listed as reentrant does not necessarily mean that
260 invocation of that function from a signal-catching function is recom‐
261 mended.
262
263 In order to prevent errors arising from interrupting non-reentrant
264 function calls, applications should protect calls to these functions
265 either by blocking the appropriate signals or through the use of some
266 programmatic semaphore (see semget() , sem_init() , sem_open() , and so
267 on). Note in particular that even the "safe" functions may modify
268 errno; the signal-catching function, if not executing as an independent
269 thread, may want to save and restore its value. Naturally, the same
270 principles apply to the reentrancy of application routines and asyn‐
271 chronous data access. Note that longjmp() and siglongjmp() are not in
272 the list of reentrant functions. This is because the code executing
273 after longjmp() and siglongjmp() can call any unsafe functions with the
274 same danger as calling those unsafe functions directly from the signal
275 handler. Applications that use longjmp() and siglongjmp() from within
276 signal handlers require rigorous protection in order to be portable.
277 Many of the other functions that are excluded from the list are tradi‐
278 tionally implemented using either malloc() or free() functions or the
279 standard I/O library, both of which traditionally use data structures
280 in a non-reentrant manner. Since any combination of different functions
281 using a common data structure can cause reentrancy problems, this vol‐
282 ume of IEEE Std 1003.1-2001 does not define the behavior when any
283 unsafe function is called in a signal handler that interrupts an unsafe
284 function.
285
286 If the signal occurs other than as the result of calling abort(),
287 kill(), or raise(), the behavior is undefined if the signal handler
288 calls any function in the standard library other than one of the func‐
289 tions listed in the table above or refers to any object with static
290 storage duration other than by assigning a value to a static storage
291 duration variable of type volatile sig_atomic_t. Furthermore, if such a
292 call fails, the value of errno is unspecified.
293
294 Usually, the signal is executed on the stack that was in effect before
295 the signal was delivered. An alternate stack may be specified to
296 receive a subset of the signals being caught.
297
298 When the signal handler returns, the receiving process resumes execu‐
299 tion at the point it was interrupted unless the signal handler makes
300 other arrangements. If longjmp() or _longjmp() is used to leave the
301 signal handler, then the signal mask must be explicitly restored by the
302 process.
303
304 This volume of IEEE Std 1003.1-2001 defines the third argument of a
305 signal handling function when SA_SIGINFO is set as a void * instead of
306 a ucontext_t *, but without requiring type checking. New applications
307 should explicitly cast the third argument of the signal handling func‐
308 tion to ucontext_t *.
309
310 The BSD optional four argument signal handling function is not sup‐
311 ported by this volume of IEEE Std 1003.1-2001. The BSD declaration
312 would be:
313
314
315 void handler(int sig, int code, struct sigcontext *scp,
316 char *addr);
317
318 where sig is the signal number, code is additional information on cer‐
319 tain signals, scp is a pointer to the sigcontext structure, and addr is
320 additional address information. Much the same information is available
321 in the objects pointed to by the second argument of the signal handler
322 specified when SA_SIGINFO is set.
323
325 Although this volume of IEEE Std 1003.1-2001 requires that signals that
326 cannot be ignored shall not be added to the signal mask when a signal-
327 catching function is entered, there is no explicit requirement that
328 subsequent calls to sigaction() reflect this in the information
329 returned in the oact argument. In other words, if SIGKILL is included
330 in the sa_mask field of act, it is unspecified whether or not a subse‐
331 quent call to sigaction() returns with SIGKILL included in the sa_mask
332 field of oact.
333
334 The SA_NOCLDSTOP flag, when supplied in the act-> sa_flags parameter,
335 allows overloading SIGCHLD with the System V semantics that each SIGCLD
336 signal indicates a single terminated child. Most conforming applica‐
337 tions that catch SIGCHLD are expected to install signal-catching func‐
338 tions that repeatedly call the waitpid() function with the WNOHANG flag
339 set, acting on each child for which status is returned, until waitpid()
340 returns zero. If stopped children are not of interest, the use of the
341 SA_NOCLDSTOP flag can prevent the overhead from invoking the signal-
342 catching routine when they stop.
343
344 Some historical implementations also define other mechanisms for stop‐
345 ping processes, such as the ptrace() function. These implementations
346 usually do not generate a SIGCHLD signal when processes stop due to
347 this mechanism; however, that is beyond the scope of this volume of
348 IEEE Std 1003.1-2001.
349
350 This volume of IEEE Std 1003.1-2001 requires that calls to sigaction()
351 that supply a NULL act argument succeed, even in the case of signals
352 that cannot be caught or ignored (that is, SIGKILL or SIGSTOP). The
353 System V signal() and BSD sigvec() functions return [EINVAL] in these
354 cases and, in this respect, their behavior varies from sigaction().
355
356 This volume of IEEE Std 1003.1-2001 requires that sigaction() properly
357 save and restore a signal action set up by the ISO C standard signal()
358 function. However, there is no guarantee that the reverse is true, nor
359 could there be given the greater amount of information conveyed by the
360 sigaction structure. Because of this, applications should avoid using
361 both functions for the same signal in the same process. Since this can‐
362 not always be avoided in case of general-purpose library routines, they
363 should always be implemented with sigaction().
364
365 It was intended that the signal() function should be implementable as a
366 library routine using sigaction().
367
368 The POSIX Realtime Extension extends the sigaction() function as speci‐
369 fied by the POSIX.1-1990 standard to allow the application to request
370 on a per-signal basis via an additional signal action flag that the
371 extra parameters, including the application-defined signal value, if
372 any, be passed to the signal-catching function.
373
375 None.
376
378 Signal Concepts , bsd_signal() , kill() , _longjmp() , longjmp() ,
379 raise() , semget() , sem_init() , sem_open() , sigaddset() , sigalt‐
380 stack() , sigdelset() , sigemptyset() , sigfillset() , sigismember() ,
381 signal() , sigprocmask() , sigsuspend() , wait() , waitid() , waitpid()
382 , the Base Definitions volume of IEEE Std 1003.1-2001, <signal.h>,
383 <ucontext.h>
384
386 Portions of this text are reprinted and reproduced in electronic form
387 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
388 -- Portable Operating System Interface (POSIX), The Open Group Base
389 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
390 Electrical and Electronics Engineers, Inc and The Open Group. In the
391 event of any discrepancy between this version and the original IEEE and
392 The Open Group Standard, the original IEEE and The Open Group Standard
393 is the referee document. The original Standard can be obtained online
394 at http://www.opengroup.org/unix/online.html .
395
396
397
398IEEE/The Open Group 2003 SIGACTION(P)