1.\" $NetBSD: signal.9,v 1.10 2004/01/08 09:14:35 wiz Exp $ 2.\" 3.\" Copyright (c) 1996, 2002 The NetBSD Foundation, Inc. 4.\" All rights reserved. 5.\" 6.\" This code is derived from software contributed to The NetBSD Foundation 7.\" by Paul Kranenburg and Jason R. Thorpe. 8.\" 9.\" Redistribution and use in source and binary forms, with or without 10.\" modification, are permitted provided that the following conditions 11.\" are met: 12.\" 1. Redistributions of source code must retain the above copyright 13.\" notice, this list of conditions and the following disclaimer. 14.\" 2. Redistributions in binary form must reproduce the above copyright 15.\" notice, this list of conditions and the following disclaimer in the 16.\" documentation and/or other materials provided with the distribution. 17.\" 3. All advertising materials mentioning features or use of this software 18.\" must display the following acknowledgement: 19.\" This product includes software developed by the NetBSD 20.\" Foundation, Inc. and its contributors. 21.\" 4. Neither the name of The NetBSD Foundation nor the names of its 22.\" contributors may be used to endorse or promote products derived 23.\" from this software without specific prior written permission. 24.\" 25.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 26.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 27.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 28.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 29.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 33.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35.\" POSSIBILITY OF SUCH DAMAGE. 36.\" 37.Dd January 7, 2004 38.Dt SIGNAL 9 39.Os 40.Sh NAME 41.Nm signal , 42.Nm siginit , 43.Nm sigactsinit , 44.Nm sigactsunshare , 45.Nm sigactsfree , 46.Nm execsigs , 47.Nm sigaction1 , 48.Nm sigprocmask1 , 49.Nm sigpending1 , 50.Nm sigsuspend1 , 51.Nm sigaltstack1 , 52.Nm gsignal , 53.Nm pgsignal , 54.Nm psignal , 55.Nm sched_psignal , 56.Nm issignal , 57.Nm postsig , 58.Nm killproc , 59.Nm sigexit , 60.Nm sigmasked , 61.Nm trapsignal , 62.Nm sendsig , 63.Nm sigcode , 64.Nm sigtramp , 65.Nd software signal facilities 66.Sh SYNOPSIS 67.In sys/signal.h 68.In sys/signalvar.h 69.Ft void 70.Fn siginit "struct proc *p" 71.Ft void 72.Fn sigactsinit "struct proc *np" "struct proc *pp" "int share" 73.Ft void 74.Fn sigactsunsuare "struct proc *p" 75.Ft void 76.Fn sigactsfree "struct proc *p" 77.Ft void 78.Fn execsigs "struct proc *p" 79.Ft int 80.Fn sigaction1 "struct proc *p" "int signum" "const struct sigaction *nsa" \ 81 "struct sigaction *osa" "void *tramp" "int vers" 82.Ft int 83.Fn sigprocmask1 "struct proc *p" "int how" "const sigset_t *nss" \ 84 "sigset_t *oss" 85.Ft void 86.Fn sigpending1 "struct proc *p" "sigset_t *ss" 87.Ft int 88.Fn sigsuspend1 "struct proc *p" "const sigset_t *ss" 89.Ft int 90.Fn sigaltstack1 "struct proc *p" "const struct sigaltstack *nss" \ 91 "struct sigaltstack *oss" 92.Ft void 93.Fn gsignal "int pgid" "int signum" 94.Ft void 95.Fn kgsignal "int pgid" "ksiginfo_t *ks" "void *data" 96.Ft void 97.Fn pgsignal "struct pgrp *pgrp" "int signum" "int checkctty" 98.Ft void 99.Fn kpgsignal "struct pgrp *pgrp" "ksiginfo_t *ks" "void *data" "int checkctty" 100.Ft void 101.Fn psignal "struct proc *p" "int signum" 102.Ft void 103.Fn kpsignal "struct proc *p" "ksiginfo_t *ks" "void *data" 104.Ft void 105.Fn sched_psignal "struct proc *p" "int signum" 106.Ft int 107.Fn issignal "struct proc *p" 108.Ft void 109.Fn postsig "int signum" 110.Ft void 111.Fn killproc "struct proc *p" "const char *why" 112.Ft void 113.Fn sigexit "struct proc *p" "int signum" 114.Ft int 115.Fn sigmasked "struct proc *p" "int signum" 116.Ft void 117.Fn trapsignal "struct proc *p" "const ksiginfo_t *ks" 118.Ft void 119.Fn sendsig "const ksiginfo_t *ks" "const sigset_t *mask" 120.Sh DESCRIPTION 121The system defines a set of signals that may be delivered to a process. 122These functions implement the kernel portion of the signal facility. 123.Pp 124Signal numbers used throughout the kernel signal facilities should 125always be within the range of 126.Bq 1- Ns NSIG . 127.Pp 128Most of the kernel's signal infrastructure is implemented in 129machine-independent code. 130Machine-dependent code provides support for invoking a process's 131signal handler, restoring context when the signal handler returns, 132generating signals when hardware traps occur, triggering the delivery 133of signals when a process is about to return from the kernel to userspace. 134.Pp 135The signal state for a process is contained in 136.Fa struct sigctx . 137This includes the list of signals with delivery pending, 138information about the signal handler stack, the signal mask, and the 139address of the signal trampoline. 140.Pp 141The registered signal handlers for a process are recorded in 142.Fa struct sigacts . 143This structure may be shared by multiple processes. 144.Pp 145The kernel's signal facilities are implemented by the following 146functions: 147.Bl -tag -width XXXXX 148.It void Fn siginit "struct proc *p" 149.Pp 150This function initializes the signal state of 151.Va proc0 152to the system default. 153This signal state is then inherited by 154.Xr init 8 155when it is started by the kernel. 156.It void Fn sigactsinit "struct proc *np" "struct proc *pp" "int share" 157.Pp 158This function creates an initial 159.Fa struct sigacts 160for the process 161.Fa np . 162If the 163.Fa share 164argument is non-zero, then 165.Fa np 166shares the 167.Fa struct sigacts 168with the process 169.Fa pp . 170Otherwise, 171.Fa np 172receives a new 173.Fa struct sigacts 174which is copied from 175.Fa pp 176if 177.No non- Ns Dv NULL . 178.It void Fn sigactsunshare "struct proc *p" 179.Pp 180This function causes the process 181.Fa p 182to no longer share its 183.Fa struct sigacts 184The current state of the signal actions is maintained in the new copy. 185.It void Fn sigactsfree "struct proc *p" 186.Pp 187This function decrements the reference count on the 188.Fa struct sigacts 189of process 190.Fa p . 191If the reference count reaches zero, the 192.Fa struct sigacts 193is freed. 194.It void Fn execsigs "struct proc *p" 195.Pp 196This function is used to reset the signal state of the process 197.Fa p 198to the system defaults when the process execs a new program image. 199.It int Fn sigaction1 "struct proc *p" "int signum" \ 200 "const struct sigaction *nsa" "struct sigaction *osa" \ 201 "void *tramp" "int vers" 202.Pp 203This function implements the 204.Xr sigaction 2 205system call. 206The 207.Fa tramp 208and 209.Fa vers 210arguments provide support for userspace signal trampolines. 211Trampoline version 0 is reserved for the legacy kernel-provided 212signal trampoline; 213.Fa tramp 214must be 215.Dv NULL 216in this case. 217Otherwise, 218.Fa vers 219specifies the ABI of the trampoline specified by 220.Fa tramp . 221The signal trampoline ABI is machine-dependent, and must be coordinated 222with the 223.Fn sendsig 224function. 225.It int Fn sigprocmask1 "struct proc *p" "int how" "const sigset_t *nss" \ 226 "sigset_t *oss" 227.Pp 228This function implements the 229.Xr sigprocmask 2 230system call. 231.It void Fn sigpending1 "struct proc *p" "sigset_t *ss" 232.Pp 233This function implements the 234.Xr sigpending 2 235system call. 236.It int Fn sigsuspend1 "struct proc *p" "const sigset_t *ss" 237.Pp 238This function implements the 239.Xr sigsuspend 2 240system call. 241.It int Fn sigaltstack1 "struct proc *p" "const struct sigaltstack *nss" \ 242 "struct sigaltstack *oss" 243.Pp 244This function implements the 245.Xr sigaltstack 2 246system call. 247.It void Fn gsignal "int pgid" "int signum" 248.Pp 249This is a wrapper function for 250.Fn kgsignal 251which is described below. 252.It void Fn kgsignal "int pgid" "ksiginfo_t *ks" "void *data" 253.Pp 254Schedule the signal 255.Fa ks-\*[Gt]ksi_signo 256to be delivered to all members of the process group specified by 257.Fa pgid . 258The 259.Fa data 260argument and the complete signal scheduling semantics are described in the 261.Fn kpsignal 262function below. 263below for a complete description of the signal scheduling semantics. 264.It void Fn pgsignal "struct pgrp *pgrp" "int signum" "int checkctty" 265.Pp 266This is a wrapper function for 267.Fn kpgsignal 268which is described below. 269.It void Fn kpgsignal "struct pgrp *pgrp" "ksiginfo_t *ks" "void *data" "int checkctty" 270.Pp 271Schedule the signal 272.Fa ks-\*[Gt]ksi_signo 273to be delivered to all members of the process group 274.Fa pgrp . 275If 276.Fa checkctty 277is non-zero, the signal is only sent to processes which have a 278controlling terminal. 279The 280.Fa data 281argument and the complete signal scheduling semantics are described in the 282.Fn kpsignal 283function below. 284.It void Fn trapsignal "struct proc *p" "const ksiginfo_t *ks" 285.Pp 286Sends the signal 287.Fa ks-\*[Gt]ksi_signo 288caused by a hardware trap to the process 289.Fa p . 290This function is meant to be called by machine-dependent trap handling 291code, through the 292.Dv p->p_emul->e_trapsignal 293function pointer because some emulations define their own trapsignal 294functions that remap the signal information to what the emulation 295expects. 296.It void Fn psignal "struct proc *p" "int signum" 297.Pp 298This is a wrapper function for 299.Fn kpgsignal 300which is described below. 301.It void Fn kpsignal "struct proc *p" "ksiginfo_t *ks" "void *data" 302.Pp 303Schedule the signal 304.Fa ks-\*[Gt]ksi_signo 305to be delivered to the process 306.Fa p . 307The 308.Fa data 309argument, if not 310.Dv NULL , 311points to the file descriptor data that caused the 312signal to be generated in the 313.Li SIGIO 314case. 315.Pp 316With a few exceptions noted below, the target process signal disposition is 317updated and is marked as runnable, so further handling of the signal is done 318in the context of the target process after a context switch; see 319.Fn issignal 320below. 321Note that 322.Fn kpsignal 323does not by itself cause a context switch to happen. 324.Pp 325The target process is not marked as runnable in the following cases: 326.Bl -bullet -offset indent 327.It 328The target process is sleeping uninterruptibly. 329The signal will be noticed when the process returns from the system 330call or trap. 331.It 332The target process is currently ignoring the signal. 333.It 334If a stop signal is sent to a sleeping process that takes the 335default action 336.Pq see Xr sigaction 2 , 337the process is stopped without awakening it. 338.It 339SIGCONT 340restarts a stopped process 341.Pq or puts them back to sleep 342regardless of the signal action 343.Pq e.g., blocked or ignored . 344.El 345.Pp 346If the target process is being traced, 347.Fn kpsignal 348behaves as if the target process were taking the default action for 349.Fa signum . 350This allows the tracing process to be notified of the signal. 351.It void Fn sched_psignal "struct proc *p" "int signum" 352.Pp 353An alternate version of 354.Fn kpsignal 355which is intended for use by code which holds the scheduler lock. 356.It int Fn issignal "struct proc *p" 357.Pp 358This function determines which signal, if any, is to be posted to 359the process 360.Fa p . 361A signal is to be posted if: 362.Bl -bullet -offset indent 363.It 364The signal has a handler provided by the program image. 365.It 366The signal should cause the process to dump core and/or terminate. 367.It 368The signal should interrupt the current system call. 369.El 370.Pp 371Signals which cause the process to be stopped are handled within 372.Fn issignal 373directly. 374.Pp 375.Fn issignal 376should be called by machine-dependent code when returning to 377userspace from a system call or other trap or interrupt by 378using the following code: 379.Bd -literal -offset indent 380while (signum = CURSIG(curproc)) 381 postsig(signum); 382.Ed 383.Pp 384.It void Fn postsig "int signum" 385.Pp 386The 387.Fn postsig 388function is used to invoke the action for the signal 389.Fa signum 390in the current process. 391If the default action of a signal is to terminate the process, and the 392signal does not have a registered handler, the process exits using 393.Fn sigexit , 394dumping a core image if necessary. 395.It void Fn killproc "struct proc *p" "const char *why" 396.Pp 397This function sends a SIGKILL signal to the specified process. 398The message provided by 399.Fa why 400is sent to the system log and is also displayed on the process's 401controlling terminal. 402.It void Fn sigexit "struct proc *p" "int signum" 403.Pp 404This function forces the process 405.Fa p 406to exit with the signal 407.Fa signum , 408generating a core file if appropriate. 409No checks are made for masked or caught signals; the process always exits. 410.It int Fn sigmasked "struct proc *p" "int signum" 411.Pp 412This function returns non-zero if the signal specified by 413.Fa signum 414is ignored or masked for process 415.Fa p . 416.It void Fn sendsig "const ksiginfo_t *ks" "const sigset_t *mask" 417.Pp 418This function is provided by machine-dependent code, and is used to 419invoke a signal handler for the current process. 420.Fn sendsig 421must prepare the registers and stack of the current process to 422invoke the signal handler stored in the process's 423.Fa struct sigacts . 424This may include switching to an alternate signal 425stack specified by the process. 426The previous register, stack, and signal state are stored in a 427.Fa ucontext_t , 428which is then copied out to the user's stack. 429.Pp 430The registers and stack must be set up to invoke the signal handler as 431follows: 432.Bd -literal -offset indent 433(*handler)(int signum, siginfo_t *info, void *ctx) 434.Ed 435.Pp 436where 437.Fa signum 438is the signal number, 439.Fa info 440contains additional signal specific information when 441.Li SA_SIGINFO 442is specified when setting up the signal handler. 443.Fa ctx 444is the pointer to 445.Fa ucontext_t 446on the user's stack. 447The registers and stack must also arrange for the signal handler to 448return to the signal trampoline. 449The trampoline is then used to return to the code which was executing 450when the signal was delivered using the 451.Xr setcontext 2 452system call. 453.Pp 454For performance reasons, it is recommended that 455.Fn sendsig 456arrange for the signal handler to be invoked directly on architectures 457where it is convenient to do so. 458In this case, the trampoline is used only for the signal return path. 459If it is not feasible to directly invoke the signal handler, the 460trampoline is also used to invoke the handler, performing any final 461set up that was not possible for 462.Fn sendsig 463to perform. 464.Pp 465.Fn sendsig 466must invoke the signal trampoline with the correct ABI. 467The ABI of the signal trampoline is specified on a per-signal basis in the 468.Fn sigacts 469structure for the process. 470Trampoline version 0 is reserved for the legacy kernel-provided, 471on-stack signal trampoline. 472All other trampoline versions indicate a specific trampoline ABI. 473This ABI is coordinated with machine-dependent code in the system 474C library. 475.El 476.Ss SIGNAL TRAMPOLINE 477The signal trampoline is a special piece of code which provides 478support for invoking the signal handlers for a process. 479The trampoline is used to return from the signal handler back to the 480code which was executing when the signal was delivered, and is also used 481to invoke the handler itself on architectures where it is not feasible to 482have the kernel invoke the handler directly. 483.Pp 484In traditional 485.Ux 486systems, the signal trampoline, also referred to as the 487.Dq sigcode , 488is provided by the kernel and copied to the top of the user's 489stack when a new process is created or a new program image is 490exec'd. 491Starting in 492.Nx 2.0 , 493the signal trampoline is provided by the system C library. 494This allows for more flexibility when the signal facility is extended, 495makes dealing with signals easier in debuggers, such as 496.Xr gdb 1 , 497and may also enhance system security by allowing the kernel to 498disallow execution of code on the stack. 499.Pp 500The signal trampoline is specified on a per-signal basis. 501The correct trampoline is selected automatically by the C library 502when a signal handler is registered by a process. 503.Pp 504Signal trampolines have a special naming convention which enables 505debuggers to determine the characteristics of the signal handler 506and its arguments. 507Trampoline functions are named like so: 508.Bd -literal -offset indent 509__sigtramp_\*[Lt]flavor\*[Gt]_\*[Lt]version\*[Gt] 510.Ed 511.Pp 512where: 513.Bl -tag -width versionXX 514.It Aq flavor 515The flavor of the signal handler. 516The following flavors are valid: 517.Bl -tag -width sigcontextXX 518.It sigcontext 519Specifies a traditional BSD-style (deprecated) signal handler with the 520following signature: 521.Bd -literal 522void (*handler)(int signum, 523 int code, 524 struct sigcontext *scp); 525.Ed 526.It siginfo 527Specifies a POSIX-style signal handler with the following signature: 528.Bd -literal 529void (*handler)(int signum, 530 siginfo_t *si, 531 void *uc); 532.Ed 533.Pp 534Note: sigcontext style signal handlers are deprecated, and retained only 535for compatibility with older binaries. 536.El 537.It Aq version 538Specifies the ABI version of the signal trampoline. 539The trampoline ABI is coordinated with the machine-dependent kernel 540.Fn sendsig 541function. 542The trampoline version needs to be unique even across different trampoline 543flavors, in order to simplify trampoline selection in the kernel. 544.El 545.Pp 546The following is an example if a signal trampoline name which indicates 547that the trampoline is used for traditional BSD-style signal handlers 548and implements version 1 of the signal trampoline ABI: 549.Bd -literal -offset indent 550__sigtramp_sigcontext_1 551.Pp 552.Ed 553.Pp 554The current signal trampoline is: 555.Bd -literal -offset indent 556__sigtramp_siginfo_2 557.Sh SEE ALSO 558.Xr sigaction 2 , 559.Xr signal 7 , 560.Xr tsleep 9 561