xref: /dragonfly/lib/libc/sys/sigaction.2 (revision ef3ac1d1)
1.\" Copyright (c) 1980, 1990, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\" 3. Neither the name of the University nor the names of its contributors
13.\"    may be used to endorse or promote products derived from this software
14.\"    without specific prior written permission.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE.
27.\"
28.\"	From: @(#)sigaction.2	8.2 (Berkeley) 4/3/94
29.\" $FreeBSD: src/lib/libc/sys/sigaction.2,v 1.22.2.10 2002/12/29 16:35:34 schweikh Exp $
30.\"
31.Dd April 3, 1994
32.Dt SIGACTION 2
33.Os
34.Sh NAME
35.Nm sigaction
36.Nd software signal facilities
37.Sh LIBRARY
38.Lb libc
39.Sh SYNOPSIS
40.In signal.h
41.Bd -literal
42struct sigaction {
43	/*
44	 * Signal handler function if flag SA_SIGINFO is not used and for
45	 * SIG_DFL and SIG_IGN.
46	 */
47	void     (*sa_handler)(int);
48
49	/* Signal handler function if flag SA_SIGINFO is used */
50	void     (*sa_sigaction)(int, siginfo_t *, void *);
51
52	sigset_t sa_mask;            /* signal mask to apply */
53	int	 sa_flags;           /* see signal options below */
54};
55.Ed
56.Ft int
57.Fn sigaction "int sig" "const struct sigaction *act" "struct sigaction *oact"
58.Sh DESCRIPTION
59The system defines a set of signals that may be delivered to a process.
60Signal delivery resembles the occurrence of a hardware interrupt:
61the signal is normally blocked from further occurrence, the current process
62context is saved, and a new one is built.
63A process may specify a
64.Em handler
65to which a signal is delivered, or specify that a signal is to be
66.Em ignored .
67A process may also specify that a default action is to be taken
68by the system when a signal occurs.
69A signal may also be
70.Em blocked ,
71in which case its delivery is postponed until it is
72.Em unblocked .
73The action to be taken on delivery is determined at the time
74of delivery.
75Normally, signal handlers execute on the current stack
76of the process.
77This may be changed, on a per-handler basis,
78so that signals are taken on a special
79.Em "signal stack" .
80.Pp
81Signal routines normally execute with the signal that caused their
82invocation
83.Em blocked ,
84but other signals may yet occur.
85A global
86.Em "signal mask"
87defines the set of signals currently blocked from delivery
88to a process.
89The signal mask for a process is initialized
90from that of its parent (normally empty).
91It may be changed with a
92.Xr sigprocmask 2
93call, or when a signal is delivered to the process.
94.Pp
95When a signal
96condition arises for a process, the signal is added to a set of
97signals pending for the process.
98If the signal is not currently
99.Em blocked
100by the process then it is delivered to the process.
101Signals may be delivered any time a process enters the operating system
102(e.g., during a system call, page fault or trap, or clock interrupt).
103If multiple signals are ready to be delivered at the same time,
104any signals that could be caused by traps are delivered first.
105Additional signals may be processed at the same time, with each
106appearing to interrupt the handlers for the previous signals
107before their first instructions.
108The set of pending signals is returned by the
109.Xr sigpending 2
110function.
111When a caught signal
112is delivered, the current state of the process is saved,
113a new signal mask is calculated (as described below),
114and the signal handler is invoked.
115The call to the handler
116is arranged so that if the signal handling routine returns
117normally the process will resume execution in the context
118from before the signal's delivery.
119If the process wishes to resume in a different context, then it
120must arrange to restore the previous context itself.
121.Pp
122When a signal is delivered to a process a new signal mask is
123installed for the duration of the process' signal handler
124(or until a
125.Xr sigprocmask 2
126call is made).
127This mask is formed by taking the union of the current signal mask set,
128the signal to be delivered, and
129the signal mask associated with the handler to be invoked.
130.Pp
131.Fn Sigaction
132assigns an action for a signal specified by
133.Fa sig .
134If
135.Fa act
136is non-zero, it
137specifies an action
138.Pf ( Dv SIG_DFL ,
139.Dv SIG_IGN ,
140or a handler routine) and mask
141to be used when delivering the specified signal.
142If
143.Fa oact
144is non-zero, the previous handling information for the signal
145is returned to the user.
146.Pp
147Once a signal handler is installed, it normally remains installed
148until another
149.Fn sigaction
150call is made, or an
151.Xr execve 2
152is performed.
153A signal-specific default action may be reset by
154setting
155.Fa sa_handler
156to
157.Dv SIG_DFL .
158The defaults are process termination, possibly with core dump;
159no action; stopping the process; or continuing the process.
160See the signal list below for each signal's default action.
161If
162.Fa sa_handler
163is
164.Dv SIG_DFL ,
165the default action for the signal is to discard the signal,
166and if a signal is pending,
167the pending signal is discarded even if the signal is masked.
168If
169.Fa sa_handler
170is set to
171.Dv SIG_IGN
172current and pending instances
173of the signal are ignored and discarded.
174.Pp
175Options may be specified by setting
176.Em sa_flags .
177The meaning of the various bits is as follows:
178.Bl -tag -offset indent -width SA_RESETHANDXX
179.It Dv SA_NOCLDSTOP
180If this bit is set when installing a catching function
181for the
182.Dv SIGCHLD
183signal,
184the
185.Dv SIGCHLD
186signal will be generated only when a child process exits,
187not when a child process stops.
188.It Dv SA_NOCLDWAIT
189If this bit is set when calling
190.Fn sigaction
191for the
192.Dv SIGCHLD
193signal, the system will not create zombie processes when children of
194the calling process exit.
195If the calling process subsequently issues a
196.Xr wait 2
197(or equivalent), it blocks until all of the calling process's child
198processes terminate, and then returns a value of -1 with
199.Va errno
200set to
201.Er ECHILD .
202.It Dv SA_ONSTACK
203If this bit is set, the system will deliver the signal to the process
204on a
205.Em "signal stack" ,
206specified with
207.Xr sigaltstack 2 .
208.It Dv SA_NODEFER
209If this bit is set, further occurrences of the delivered signal are
210not masked during the execution of the handler.
211.It Dv SA_RESETHAND
212If this bit is set, the handler is reset back to
213.Dv SIG_DFL
214at the moment the signal is delivered.
215.It Dv SA_SIGINFO
216If this bit is set, the handler function is assumed to be pointed to by the
217.Dv sa_sigaction
218member of struct sigaction and should match the prototype shown above or as
219below in
220.Sx EXAMPLES .
221This bit should not be set when assigning
222.Dv SIG_DFL
223or
224.Dv SIG_IGN .
225.El
226.Pp
227If a signal is caught during the system calls listed below,
228the call may be forced to terminate
229with the error
230.Er EINTR ,
231the call may return with a data transfer shorter than requested,
232or the call may be restarted.
233Restart of pending calls is requested
234by setting the
235.Dv SA_RESTART
236bit in
237.Ar sa_flags .
238The affected system calls include
239.Xr open 2 ,
240.Xr read 2 ,
241.Xr write 2 ,
242.Xr sendto 2 ,
243.Xr recvfrom 2 ,
244.Xr sendmsg 2
245and
246.Xr recvmsg 2
247on a communications channel or a slow device (such as a terminal,
248but not a regular file)
249and during a
250.Xr wait 2
251or
252.Xr ioctl 2 .
253However, calls that have already committed are not restarted,
254but instead return a partial success (for example, a short read count).
255.Pp
256After a
257.Xr fork 2
258or
259.Xr vfork 2
260all signals, the signal mask, the signal stack,
261and the restart/interrupt flags are inherited by the child.
262.Pp
263.Xr Execve 2
264reinstates the default
265action for all signals which were caught and
266resets all signals to be caught on the user stack.
267Ignored signals remain ignored;
268the signal mask remains the same;
269signals that restart pending system calls continue to do so.
270.Pp
271The following is a list of all signals
272with names as in the include file
273.In signal.h :
274.Bl -column SIGVTALARMXX "create core imagexxx"
275.It Sy "NAME	Default Action	Description"
276.It Dv SIGHUP No "	terminate process" "	terminal line hangup"
277.It Dv SIGINT No "	terminate process" "	interrupt program"
278.It Dv SIGQUIT No "	create core image" "	quit program"
279.It Dv SIGILL No "	create core image" "	illegal instruction"
280.It Dv SIGTRAP No "	create core image" "	trace trap"
281.It Dv SIGABRT No "	create core image" Ta Xr abort 3
282call (formerly
283.Dv SIGIOT )
284.It Dv SIGEMT No "	create core image" "	emulate instruction executed"
285.It Dv SIGFPE No "	create core image" "	floating-point exception"
286.It Dv SIGKILL No "	terminate process" "	kill program"
287.It Dv SIGBUS No "	create core image" "	bus error"
288.It Dv SIGSEGV No "	create core image" "	segmentation violation"
289.It Dv SIGSYS No "	create core image" "	non-existent system call invoked"
290.It Dv SIGPIPE No "	terminate process" "	write on a pipe with no reader"
291.It Dv SIGALRM No "	terminate process" "	real-time timer expired"
292.It Dv SIGTERM No "	terminate process" "	software termination signal"
293.It Dv SIGURG No "	discard signal" "	urgent condition present on socket"
294.It Dv SIGSTOP No "	stop process" "	stop (cannot be caught or ignored)"
295.It Dv SIGTSTP No "	stop process" "	stop signal generated from keyboard"
296.It Dv SIGCONT No "	discard signal" "	continue after stop"
297.It Dv SIGCHLD No "	discard signal" "	child status has changed"
298.It Dv SIGTTIN No "	stop process" "	background read attempted from control terminal"
299.It Dv SIGTTOU No "	stop process" "	background write attempted to control terminal"
300.It Dv SIGIO No "	discard signal" Tn "	I/O"
301is possible on a descriptor (see
302.Xr fcntl 2 )
303.It Dv SIGXCPU No "	terminate process" "	cpu time limit exceeded (see"
304.Xr setrlimit 2 )
305.It Dv SIGXFSZ No "	terminate process" "	file size limit exceeded (see"
306.Xr setrlimit 2 )
307.It Dv SIGVTALRM No "	terminate process" "	virtual time alarm (see"
308.Xr setitimer 2 )
309.It Dv SIGPROF No "	terminate process" "	profiling timer alarm (see"
310.Xr setitimer 2 )
311.It Dv SIGWINCH No "	discard signal" "	Window size change"
312.It Dv SIGINFO No "	discard signal" "	status request from keyboard"
313.It Dv SIGUSR1 No "	terminate process" "	User defined signal 1"
314.It Dv SIGUSR2 No "	terminate process" "	User defined signal 2"
315.El
316.Sh NOTE
317The
318.Fa sa_mask
319field specified in
320.Fa act
321is not allowed to block
322.Dv SIGKILL
323or
324.Dv SIGSTOP .
325Any attempt to do so will be silently ignored.
326.Pp
327The following functions are either reentrant or not interruptible
328by signals and are async-signal safe.
329Therefore applications may
330invoke them, without restriction, from signal-catching functions:
331.Pp
332Base Interfaces:
333.Pp
334.Fn _exit ,
335.Fn access ,
336.Fn alarm ,
337.Fn cfgetispeed ,
338.Fn cfgetospeed ,
339.Fn cfsetispeed ,
340.Fn cfsetospeed ,
341.Fn chdir ,
342.Fn chmod ,
343.Fn chown ,
344.Fn close ,
345.Fn creat ,
346.Fn dup ,
347.Fn dup2 ,
348.Fn execle ,
349.Fn execve ,
350.Fn fcntl ,
351.Fn fork ,
352.Fn fpathconf ,
353.Fn fstat ,
354.Fn fsync ,
355.Fn getegid ,
356.Fn geteuid ,
357.Fn getgid ,
358.Fn getgroups ,
359.Fn getpgrp ,
360.Fn getpid ,
361.Fn getppid ,
362.Fn getuid ,
363.Fn kill ,
364.Fn link ,
365.Fn lseek ,
366.Fn mkdir ,
367.Fn mkfifo ,
368.Fn open ,
369.Fn pathconf ,
370.Fn pause ,
371.Fn pipe ,
372.Fn raise ,
373.Fn read ,
374.Fn rename ,
375.Fn rmdir ,
376.Fn setgid ,
377.Fn setpgid ,
378.Fn setsid ,
379.Fn setuid ,
380.Fn sigaction ,
381.Fn sigaddset ,
382.Fn sigdelset ,
383.Fn sigemptyset ,
384.Fn sigfillset  ,
385.Fn sigismember ,
386.Fn signal ,
387.Fn sigpending ,
388.Fn sigprocmask ,
389.Fn sigsuspend ,
390.Fn sleep ,
391.Fn stat ,
392.Fn sysconf ,
393.Fn tcdrain ,
394.Fn tcflow ,
395.Fn tcflush ,
396.Fn tcgetattr ,
397.Fn tcgetpgrp ,
398.Fn tcsendbreak ,
399.Fn tcsetattr ,
400.Fn tcsetpgrp ,
401.Fn time ,
402.Fn times ,
403.Fn umask ,
404.Fn uname ,
405.Fn unlink ,
406.Fn utime ,
407.Fn wait ,
408.Fn waitpid ,
409.Fn write .
410.Pp
411Realtime Interfaces:
412.Pp
413.Fn aio_error ,
414.Fn clock_gettime ,
415.Fn sigpause ,
416.\".Fn timer_getoverrun ,
417.Fn aio_return ,
418.\".Fn fdatasync ,
419.Fn sigqueue ,
420.\".Fn timer_gettime ,
421.Fn aio_suspend ,
422.Fn sem_post ,
423.Fn sigset .
424.\".Fn timer_settime .
425.Pp
426All functions not in the above lists are considered to be unsafe
427with respect to signals.
428That is to say, the behaviour of such
429functions when called from a signal handler is undefined.
430.Sh RETURN VALUES
431.Rv -std sigaction
432.Sh EXAMPLES
433There are three possible prototypes the handler may match:
434.Bl -tag -offset indent -width short
435.It ANSI C:
436.Ft void
437.Fn handler int ;
438.It Traditional BSD style:
439.Ft void
440.Fn handler int "int code" "struct sigcontext *scp" ;
441.It POSIX SA_SIGINFO:
442.Ft void
443.Fn handler int "siginfo_t *info" "ucontext_t *uap" ;
444.El
445.Pp
446The handler function should match the
447.Dv SA_SIGINFO
448prototype if the
449.Dv SA_SIGINFO
450bit is set in flags.
451It then should be pointed to by the
452.Dv sa_sigaction
453member of
454.Dv struct sigaction .
455Note that you should not assign
456.Dv SIG_DFL
457or
458.Dv SIG_IGN
459this way.
460.Pp
461If the
462.Dv SA_SIGINFO
463flag is not set, the handler function should match
464either the ANSI C or traditional
465.Bx
466prototype and be pointed to by
467the
468.Dv sa_handler
469member of
470.Dv struct sigaction .
471In practice,
472.Dx
473always sends the three arguments of the latter and since the ANSI C
474prototype is a subset, both will work.
475The
476.Dv sa_handler
477member declaration in
478.Dx
479include files is that of ANSI C (as required by POSIX),
480so a function pointer of a
481.Bx Ns -style
482function needs to be casted to
483compile without warning.
484The traditional
485.Bx
486style is not portable and since its capabilities are a full subset of a
487.Dv SA_SIGINFO
488handler, its use is deprecated.
489.Pp
490The
491.Fa sig
492argument is the signal number, one of the
493.Dv SIG...
494.In signal.h .
495.Pp
496The
497.Fa code
498argument of the
499.Bx Ns -style
500handler and the
501.Dv si_code
502member of the
503.Dv info
504argument to a
505.Dv SA_SIGINFO
506handler contain a numeric code explaining the
507cause of the signal, usually one of the
508.Dv SI_...
509values from
510.In sys/signal.h
511or codes specific to a signal, i.e.\& one of the
512.Dv FPE_...
513values for
514.Dv SIGFPE .
515.Pp
516The
517.Fa scp
518argument to a
519.Bx Ns -style
520handler points to an instance of struct
521sigcontext.
522.Pp
523The
524.Fa uap
525argument to a POSIX
526.Dv SA_SIGINFO
527handler points to an instance of ucontext_t.
528.Sh ERRORS
529.Fn Sigaction
530will fail and no new signal handler will be installed if one
531of the following occurs:
532.Bl -tag -width Er
533.It Bq Er EFAULT
534Either
535.Fa act
536or
537.Fa oact
538points to memory that is not a valid part of the process
539address space.
540.It Bq Er EINVAL
541.Fa Sig
542is not a valid signal number.
543.It Bq Er EINVAL
544An attempt is made to ignore or supply a handler for
545.Dv SIGKILL
546or
547.Dv SIGSTOP .
548.El
549.Sh SEE ALSO
550.Xr kill 1 ,
551.Xr kill 2 ,
552.Xr ptrace 2 ,
553.Xr sigaltstack 2 ,
554.Xr sigpending 2 ,
555.Xr sigprocmask 2 ,
556.Xr sigsuspend 2 ,
557.Xr wait 2 ,
558.Xr fpsetmask 3 ,
559.Xr setjmp 3 ,
560.Xr siginterrupt 3 ,
561.Xr sigsetops 3 ,
562.Xr ucontext 3 ,
563.Xr tty 4
564.Sh STANDARDS
565The
566.Fn sigaction
567function call is expected to conform to
568.St -p1003.1-90 .
569The
570.Dv SA_ONSTACK
571and
572.Dv SA_RESTART
573flags are Berkeley extensions,
574as are the signals,
575.Dv SIGTRAP ,
576.Dv SIGEMT ,
577.Dv SIGBUS ,
578.Dv SIGSYS ,
579.Dv SIGURG ,
580.Dv SIGIO ,
581.Dv SIGXCPU ,
582.Dv SIGXFSZ ,
583.Dv SIGVTALRM ,
584.Dv SIGPROF ,
585.Dv SIGWINCH ,
586and
587.Dv SIGINFO .
588Those signals are available on most
589.Bx Ns \-derived
590systems.
591The
592.Dv SA_NODEFER
593and
594.Dv SA_RESETHAND
595flags are intended for backwards compatibility with other operating
596systems.
597The
598.Dv SA_NOCLDSTOP ,
599and
600.Dv SA_NOCLDWAIT
601.\" and
602.\" SA_SIGINFO
603flags are featuring options commonly found in other operating systems.
604