Source File
doc.go
Belonging Package
os/signal
// Copyright 2015 The Go Authors. All rights reserved.// Use of this source code is governed by a BSD-style// license that can be found in the LICENSE file./*Package signal implements access to incoming signals.Signals are primarily used on Unix-like systems. For the use of thispackage on Windows and Plan 9, see below.# Types of signalsThe signals SIGKILL and SIGSTOP may not be caught by a program, andtherefore cannot be affected by this package.Synchronous signals are signals triggered by errors in programexecution: SIGBUS, SIGFPE, and SIGSEGV. These are only consideredsynchronous when caused by program execution, not when sent using[os.Process.Kill] or the kill program or some similar mechanism. Ingeneral, except as discussed below, Go programs will convert asynchronous signal into a run-time panic.The remaining signals are asynchronous signals. They are nottriggered by program errors, but are instead sent from the kernel orfrom some other program.Of the asynchronous signals, the SIGHUP signal is sent when a programloses its controlling terminal. The SIGINT signal is sent when theuser at the controlling terminal presses the interrupt character,which by default is ^C (Control-C). The SIGQUIT signal is sent whenthe user at the controlling terminal presses the quit character, whichby default is ^\ (Control-Backslash). In general you can cause aprogram to simply exit by pressing ^C, and you can cause it to exitwith a stack dump by pressing ^\.# Default behavior of signals in Go programsBy default, a synchronous signal is converted into a run-time panic. ASIGHUP, SIGINT, or SIGTERM signal causes the program to exit. ASIGQUIT, SIGILL, SIGTRAP, SIGABRT, SIGSTKFLT, SIGEMT, or SIGSYS signalcauses the program to exit with a stack dump. A SIGTSTP, SIGTTIN, orSIGTTOU signal gets the system default behavior (these signals areused by the shell for job control). The SIGPROF signal is handleddirectly by the Go runtime to implement runtime.CPUProfile. Othersignals will be caught but no action will be taken.If the Go program is started with either SIGHUP or SIGINT ignored(signal handler set to SIG_IGN), they will remain ignored.If the Go program is started with a non-empty signal mask, that willgenerally be honored. However, some signals are explicitly unblocked:the synchronous signals, SIGILL, SIGTRAP, SIGSTKFLT, SIGCHLD, SIGPROF,and, on Linux, signals 32 (SIGCANCEL) and 33 (SIGSETXID)(SIGCANCEL and SIGSETXID are used internally by glibc). Subprocessesstarted by [os.Exec], or by [os/exec], will inherit themodified signal mask.# Changing the behavior of signals in Go programsThe functions in this package allow a program to change the way Goprograms handle signals.Notify disables the default behavior for a given set of asynchronoussignals and instead delivers them over one or more registeredchannels. Specifically, it applies to the signals SIGHUP, SIGINT,SIGQUIT, SIGABRT, and SIGTERM. It also applies to the job controlsignals SIGTSTP, SIGTTIN, and SIGTTOU, in which case the systemdefault behavior does not occur. It also applies to some signals thatotherwise cause no action: SIGUSR1, SIGUSR2, SIGPIPE, SIGALRM,SIGCHLD, SIGCONT, SIGURG, SIGXCPU, SIGXFSZ, SIGVTALRM, SIGWINCH,SIGIO, SIGPWR, SIGINFO, SIGTHR, SIGWAITING, SIGLWP, SIGFREEZE,SIGTHAW, SIGLOST, SIGXRES, SIGJVM1, SIGJVM2, and any real time signalsused on the system. Note that not all of these signals are availableon all systems.If the program was started with SIGHUP or SIGINT ignored, and [Notify]is called for either signal, a signal handler will be installed forthat signal and it will no longer be ignored. If, later, [Reset] or[Ignore] is called for that signal, or [Stop] is called on all channelspassed to Notify for that signal, the signal will once again beignored. Reset will restore the system default behavior for thesignal, while Ignore will cause the system to ignore the signalentirely.If the program is started with a non-empty signal mask, some signalswill be explicitly unblocked as described above. If Notify is calledfor a blocked signal, it will be unblocked. If, later, Reset iscalled for that signal, or Stop is called on all channels passed toNotify for that signal, the signal will once again be blocked.# SIGPIPEWhen a Go program writes to a broken pipe, the kernel will raise aSIGPIPE signal.If the program has not called Notify to receive SIGPIPE signals, thenthe behavior depends on the file descriptor number. A write to abroken pipe on file descriptors 1 or 2 (standard output or standarderror) will cause the program to exit with a SIGPIPE signal. A writeto a broken pipe on some other file descriptor will take no action onthe SIGPIPE signal, and the write will fail with a [syscall.EPIPE]error.If the program has called Notify to receive SIGPIPE signals, the filedescriptor number does not matter. The SIGPIPE signal will bedelivered to the Notify channel, and the write will fail with a[syscall.EPIPE] error.This means that, by default, command line programs will behave liketypical Unix command line programs, while other programs will notcrash with SIGPIPE when writing to a closed network connection.# Go programs that use cgo or SWIGIn a Go program that includes non-Go code, typically C/C++ codeaccessed using cgo or SWIG, Go's startup code normally runs first. Itconfigures the signal handlers as expected by the Go runtime, beforethe non-Go startup code runs. If the non-Go startup code wishes toinstall its own signal handlers, it must take certain steps to keep Goworking well. This section documents those steps and the overalleffect changes to signal handler settings by the non-Go code can haveon Go programs. In rare cases, the non-Go code may run before the Gocode, in which case the next section also applies.If the non-Go code called by the Go program does not change any signalhandlers or masks, then the behavior is the same as for a pure Goprogram.If the non-Go code installs any signal handlers, it must use theSA_ONSTACK flag with sigaction. Failing to do so is likely to causethe program to crash if the signal is received. Go programs routinelyrun with a limited stack, and therefore set up an alternate signalstack.If the non-Go code installs a signal handler for any of thesynchronous signals (SIGBUS, SIGFPE, SIGSEGV), then it should recordthe existing Go signal handler. If those signals occur whileexecuting Go code, it should invoke the Go signal handler (whether thesignal occurs while executing Go code can be determined by looking atthe PC passed to the signal handler). Otherwise some Go run-timepanics will not occur as expected.If the non-Go code installs a signal handler for any of theasynchronous signals, it may invoke the Go signal handler or not as itchooses. Naturally, if it does not invoke the Go signal handler, theGo behavior described above will not occur. This can be an issue withthe SIGPROF signal in particular.The non-Go code should not change the signal mask on any threadscreated by the Go runtime. If the non-Go code starts new threadsitself, those threads may set the signal mask as they please.If the non-Go code starts a new thread, changes the signal mask, andthen invokes a Go function in that thread, the Go runtime willautomatically unblock certain signals: the synchronous signals,SIGILL, SIGTRAP, SIGSTKFLT, SIGCHLD, SIGPROF, SIGCANCEL, andSIGSETXID. When the Go function returns, the non-Go signal mask willbe restored.If the Go signal handler is invoked on a non-Go thread not running Gocode, the handler generally forwards the signal to the non-Go code, asfollows. If the signal is SIGPROF, the Go handler doesnothing. Otherwise, the Go handler removes itself, unblocks thesignal, and raises it again, to invoke any non-Go handler or defaultsystem handler. If the program does not exit, the Go handler thenreinstalls itself and continues execution of the program.If a SIGPIPE signal is received, the Go program will invoke thespecial handling described above if the SIGPIPE is received on a Gothread. If the SIGPIPE is received on a non-Go thread the signal willbe forwarded to the non-Go handler, if any; if there is none thedefault system handler will cause the program to terminate.# Non-Go programs that call Go codeWhen Go code is built with options like -buildmode=c-shared, it willbe run as part of an existing non-Go program. The non-Go code mayhave already installed signal handlers when the Go code starts (thatmay also happen in unusual cases when using cgo or SWIG; in that case,the discussion here applies). For -buildmode=c-archive the Go runtimewill initialize signals at global constructor time. For-buildmode=c-shared the Go runtime will initialize signals when theshared library is loaded.If the Go runtime sees an existing signal handler for the SIGCANCEL orSIGSETXID signals (which are used only on Linux), it will turn onthe SA_ONSTACK flag and otherwise keep the signal handler.For the synchronous signals and SIGPIPE, the Go runtime will install asignal handler. It will save any existing signal handler. If asynchronous signal arrives while executing non-Go code, the Go runtimewill invoke the existing signal handler instead of the Go signalhandler.Go code built with -buildmode=c-archive or -buildmode=c-shared willnot install any other signal handlers by default. If there is anexisting signal handler, the Go runtime will turn on the SA_ONSTACKflag and otherwise keep the signal handler. If Notify is called for anasynchronous signal, a Go signal handler will be installed for thatsignal. If, later, Reset is called for that signal, the originalhandling for that signal will be reinstalled, restoring the non-Gosignal handler if any.Go code built without -buildmode=c-archive or -buildmode=c-shared willinstall a signal handler for the asynchronous signals listed above,and save any existing signal handler. If a signal is delivered to anon-Go thread, it will act as described above, except that if there isan existing non-Go signal handler, that handler will be installedbefore raising the signal.# WindowsOn Windows a ^C (Control-C) or ^BREAK (Control-Break) normally causethe program to exit. If Notify is called for [os.Interrupt], ^C or ^BREAKwill cause [os.Interrupt] to be sent on the channel, and the program willnot exit. If Reset is called, or Stop is called on all channels passedto Notify, then the default behavior will be restored.Additionally, if Notify is called, and Windows sends CTRL_CLOSE_EVENT,CTRL_LOGOFF_EVENT or CTRL_SHUTDOWN_EVENT to the process, Notify willreturn syscall.SIGTERM. Unlike Control-C and Control-Break, Notify doesnot change process behavior when either CTRL_CLOSE_EVENT,CTRL_LOGOFF_EVENT or CTRL_SHUTDOWN_EVENT is received - the process willstill get terminated unless it exits. But receiving syscall.SIGTERM willgive the process an opportunity to clean up before termination.# Plan 9On Plan 9, signals have type syscall.Note, which is a string. CallingNotify with a syscall.Note will cause that value to be sent on thechannel when that string is posted as a note.*/package signal
![]() |
The pages are generated with Golds v0.7.9-preview. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds. |