You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

564 lines
20 KiB

8 years ago
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <meta name="theme-color" content="#375EAB">
  7. <title>signal - The Go Programming Language</title>
  8. <link type="text/css" rel="stylesheet" href="../../../lib/godoc/style.css">
  9. <link rel="stylesheet" href="../../../lib/godoc/jquery.treeview.css">
  10. <script type="text/javascript">window.initFuncs = [];</script>
  11. </head>
  12. <body>
  13. <div id='lowframe' style="position: fixed; bottom: 0; left: 0; height: 0; width: 100%; border-top: thin solid grey; background-color: white; overflow: auto;">
  14. ...
  15. </div><!-- #lowframe -->
  16. <div id="topbar" class="wide"><div class="container">
  17. <div class="top-heading" id="heading-wide"><a href="http://localhost:6060/">The Go Programming Language</a></div>
  18. <div class="top-heading" id="heading-narrow"><a href="http://localhost:6060/">Go</a></div>
  19. <a href="index.html#" id="menu-button"><span id="menu-button-arrow">&#9661;</span></a>
  20. <form method="GET" action="http://localhost:6060/search">
  21. <div id="menu">
  22. <a href="http://localhost:6060/doc/">Documents</a>
  23. <a href="http://localhost:6060/pkg/">Packages</a>
  24. <a href="http://localhost:6060/project/">The Project</a>
  25. <a href="http://localhost:6060/help/">Help</a>
  26. <a href="http://localhost:6060/blog/">Blog</a>
  27. <input type="text" id="search" name="q" class="inactive" value="Search" placeholder="Search">
  28. </div>
  29. </form>
  30. </div></div>
  31. <div id="page" class="wide">
  32. <div class="container">
  33. <h1>Package signal</h1>
  34. <div id="nav"></div>
  35. <!--
  36. Copyright 2009 The Go Authors. All rights reserved.
  37. Use of this source code is governed by a BSD-style
  38. license that can be found in the LICENSE file.
  39. -->
  40. <!--
  41. Note: Static (i.e., not template-generated) href and id
  42. attributes start with "pkg-" to make it impossible for
  43. them to conflict with generated attributes (some of which
  44. correspond to Go identifiers).
  45. -->
  46. <script type='text/javascript'>
  47. document.ANALYSIS_DATA = null;
  48. document.CALLGRAPH = null;
  49. </script>
  50. <div id="short-nav">
  51. <dl>
  52. <dd><code>import "os/signal"</code></dd>
  53. </dl>
  54. <dl>
  55. <dd><a href="index.html#pkg-overview" class="overviewLink">Overview</a></dd>
  56. <dd><a href="index.html#pkg-index" class="indexLink">Index</a></dd>
  57. <dd><a href="index.html#pkg-examples" class="examplesLink">Examples</a></dd>
  58. </dl>
  59. </div>
  60. <!-- The package's Name is printed as title by the top-level template -->
  61. <div id="pkg-overview" class="toggleVisible">
  62. <div class="collapsed">
  63. <h2 class="toggleButton" title="Click to show Overview section">Overview ▹</h2>
  64. </div>
  65. <div class="expanded">
  66. <h2 class="toggleButton" title="Click to hide Overview section">Overview ▾</h2>
  67. <p>
  68. Package signal implements access to incoming signals.
  69. </p>
  70. <p>
  71. Signals are primarily used on Unix-like systems. For the use of this
  72. package on Windows and Plan 9, see below.
  73. </p>
  74. <h3 id="hdr-Types_of_signals">Types of signals</h3>
  75. <p>
  76. The signals SIGKILL and SIGSTOP may not be caught by a program, and
  77. therefore can not be affected by this package.
  78. </p>
  79. <p>
  80. Synchronous signals are signals triggered by errors in program
  81. execution: SIGBUS, SIGFPE, and SIGSEGV. These are only considered
  82. synchronous when caused by program execution, not when sent using
  83. os.Process.Kill or the kill program or some similar mechanism. In
  84. general, except as discussed below, Go programs will convert a
  85. synchronous signal into a run-time panic.
  86. </p>
  87. <p>
  88. The remaining signals are asynchronous signals. They are not
  89. triggered by program errors, but are instead sent from the kernel or
  90. from some other program.
  91. </p>
  92. <p>
  93. Of the asynchronous signals, the SIGHUP signal is sent when a program
  94. loses its controlling terminal. The SIGINT signal is sent when the
  95. user at the controlling terminal presses the interrupt character,
  96. which by default is ^C (Control-C). The SIGQUIT signal is sent when
  97. the user at the controlling terminal presses the quit character, which
  98. by default is ^\ (Control-Backslash). In general you can cause a
  99. program to simply exit by pressing ^C, and you can cause it to exit
  100. with a stack dump by pressing ^\.
  101. </p>
  102. <h3 id="hdr-Default_behavior_of_signals_in_Go_programs">Default behavior of signals in Go programs</h3>
  103. <p>
  104. By default, a synchronous signal is converted into a run-time panic. A
  105. SIGHUP, SIGINT, or SIGTERM signal causes the program to exit. A
  106. SIGQUIT, SIGILL, SIGTRAP, SIGABRT, SIGSTKFLT, SIGEMT, or SIGSYS signal
  107. causes the program to exit with a stack dump. A SIGTSTP, SIGTTIN, or
  108. SIGTTOU signal gets the system default behavior (these signals are
  109. used by the shell for job control). The SIGPROF signal is handled
  110. directly by the Go runtime to implement runtime.CPUProfile. Other
  111. signals will be caught but no action will be taken.
  112. </p>
  113. <p>
  114. If the Go program is started with either SIGHUP or SIGINT ignored
  115. (signal handler set to SIG_IGN), they will remain ignored.
  116. </p>
  117. <p>
  118. If the Go program is started with a non-empty signal mask, that will
  119. generally be honored. However, some signals are explicitly unblocked:
  120. the synchronous signals, SIGILL, SIGTRAP, SIGSTKFLT, SIGCHLD, SIGPROF,
  121. and, on GNU/Linux, signals 32 (SIGCANCEL) and 33 (SIGSETXID)
  122. (SIGCANCEL and SIGSETXID are used internally by glibc). Subprocesses
  123. started by os.Exec, or by the os/exec package, will inherit the
  124. modified signal mask.
  125. </p>
  126. <h3 id="hdr-Changing_the_behavior_of_signals_in_Go_programs">Changing the behavior of signals in Go programs</h3>
  127. <p>
  128. The functions in this package allow a program to change the way Go
  129. programs handle signals.
  130. </p>
  131. <p>
  132. Notify disables the default behavior for a given set of asynchronous
  133. signals and instead delivers them over one or more registered
  134. channels. Specifically, it applies to the signals SIGHUP, SIGINT,
  135. SIGQUIT, SIGABRT, and SIGTERM. It also applies to the job control
  136. signals SIGTSTP, SIGTTIN, and SIGTTOU, in which case the system
  137. default behavior does not occur. It also applies to some signals that
  138. otherwise cause no action: SIGUSR1, SIGUSR2, SIGPIPE, SIGALRM,
  139. SIGCHLD, SIGCONT, SIGURG, SIGXCPU, SIGXFSZ, SIGVTALRM, SIGWINCH,
  140. SIGIO, SIGPWR, SIGSYS, SIGINFO, SIGTHR, SIGWAITING, SIGLWP, SIGFREEZE,
  141. SIGTHAW, SIGLOST, SIGXRES, SIGJVM1, SIGJVM2, and any real time signals
  142. used on the system. Note that not all of these signals are available
  143. on all systems.
  144. </p>
  145. <p>
  146. If the program was started with SIGHUP or SIGINT ignored, and Notify
  147. is called for either signal, a signal handler will be installed for
  148. that signal and it will no longer be ignored. If, later, Reset or
  149. Ignore is called for that signal, or Stop is called on all channels
  150. passed to Notify for that signal, the signal will once again be
  151. ignored. Reset will restore the system default behavior for the
  152. signal, while Ignore will cause the system to ignore the signal
  153. entirely.
  154. </p>
  155. <p>
  156. If the program is started with a non-empty signal mask, some signals
  157. will be explicitly unblocked as described above. If Notify is called
  158. for a blocked signal, it will be unblocked. If, later, Reset is
  159. called for that signal, or Stop is called on all channels passed to
  160. Notify for that signal, the signal will once again be blocked.
  161. </p>
  162. <h3 id="hdr-SIGPIPE">SIGPIPE</h3>
  163. <p>
  164. When a Go program writes to a broken pipe, the kernel will raise a
  165. SIGPIPE signal.
  166. </p>
  167. <p>
  168. If the program has not called Notify to receive SIGPIPE signals, then
  169. the behavior depends on the file descriptor number. A write to a
  170. broken pipe on file descriptors 1 or 2 (standard output or standard
  171. error) will cause the program to exit with a SIGPIPE signal. A write
  172. to a broken pipe on some other file descriptor will take no action on
  173. the SIGPIPE signal, and the write will fail with an EPIPE error.
  174. </p>
  175. <p>
  176. If the program has called Notify to receive SIGPIPE signals, the file
  177. descriptor number does not matter. The SIGPIPE signal will be
  178. delivered to the Notify channel, and the write will fail with an EPIPE
  179. error.
  180. </p>
  181. <p>
  182. This means that, by default, command line programs will behave like
  183. typical Unix command line programs, while other programs will not
  184. crash with SIGPIPE when writing to a closed network connection.
  185. </p>
  186. <h3 id="hdr-Go_programs_that_use_cgo_or_SWIG">Go programs that use cgo or SWIG</h3>
  187. <p>
  188. In a Go program that includes non-Go code, typically C/C++ code
  189. accessed using cgo or SWIG, Go&#39;s startup code normally runs first. It
  190. configures the signal handlers as expected by the Go runtime, before
  191. the non-Go startup code runs. If the non-Go startup code wishes to
  192. install its own signal handlers, it must take certain steps to keep Go
  193. working well. This section documents those steps and the overall
  194. effect changes to signal handler settings by the non-Go code can have
  195. on Go programs. In rare cases, the non-Go code may run before the Go
  196. code, in which case the next section also applies.
  197. </p>
  198. <p>
  199. If the non-Go code called by the Go program does not change any signal
  200. handlers or masks, then the behavior is the same as for a pure Go
  201. program.
  202. </p>
  203. <p>
  204. If the non-Go code installs any signal handlers, it must use the
  205. SA_ONSTACK flag with sigaction. Failing to do so is likely to cause
  206. the program to crash if the signal is received. Go programs routinely
  207. run with a limited stack, and therefore set up an alternate signal
  208. stack. Also, the Go standard library expects that any signal handlers
  209. will use the SA_RESTART flag. Failing to do so may cause some library
  210. calls to return &#34;interrupted system call&#34; errors.
  211. </p>
  212. <p>
  213. If the non-Go code installs a signal handler for any of the
  214. synchronous signals (SIGBUS, SIGFPE, SIGSEGV), then it should record
  215. the existing Go signal handler. If those signals occur while
  216. executing Go code, it should invoke the Go signal handler (whether the
  217. signal occurs while executing Go code can be determined by looking at
  218. the PC passed to the signal handler). Otherwise some Go run-time
  219. panics will not occur as expected.
  220. </p>
  221. <p>
  222. If the non-Go code installs a signal handler for any of the
  223. asynchronous signals, it may invoke the Go signal handler or not as it
  224. chooses. Naturally, if it does not invoke the Go signal handler, the
  225. Go behavior described above will not occur. This can be an issue with
  226. the SIGPROF signal in particular.
  227. </p>
  228. <p>
  229. The non-Go code should not change the signal mask on any threads
  230. created by the Go runtime. If the non-Go code starts new threads of
  231. its own, it may set the signal mask as it pleases.
  232. </p>
  233. <p>
  234. If the non-Go code starts a new thread, changes the signal mask, and
  235. then invokes a Go function in that thread, the Go runtime will
  236. automatically unblock certain signals: the synchronous signals,
  237. SIGILL, SIGTRAP, SIGSTKFLT, SIGCHLD, SIGPROF, SIGCANCEL, and
  238. SIGSETXID. When the Go function returns, the non-Go signal mask will
  239. be restored.
  240. </p>
  241. <p>
  242. If the Go signal handler is invoked on a non-Go thread not running Go
  243. code, the handler generally forwards the signal to the non-Go code, as
  244. follows. If the signal is SIGPROF, the Go handler does
  245. nothing. Otherwise, the Go handler removes itself, unblocks the
  246. signal, and raises it again, to invoke any non-Go handler or default
  247. system handler. If the program does not exit, the Go handler then
  248. reinstalls itself and continues execution of the program.
  249. </p>
  250. <h3 id="hdr-Non_Go_programs_that_call_Go_code">Non-Go programs that call Go code</h3>
  251. <p>
  252. When Go code is built with options like -buildmode=c-shared, it will
  253. be run as part of an existing non-Go program. The non-Go code may
  254. have already installed signal handlers when the Go code starts (that
  255. may also happen in unusual cases when using cgo or SWIG; in that case,
  256. the discussion here applies). For -buildmode=c-archive the Go runtime
  257. will initialize signals at global constructor time. For
  258. -buildmode=c-shared the Go runtime will initialize signals when the
  259. shared library is loaded.
  260. </p>
  261. <p>
  262. If the Go runtime sees an existing signal handler for the SIGCANCEL or
  263. SIGSETXID signals (which are used only on GNU/Linux), it will turn on
  264. the SA_ONSTACK flag and otherwise keep the signal handler.
  265. </p>
  266. <p>
  267. For the synchronous signals, the Go runtime will install a signal
  268. handler. It will save any existing signal handler. If a synchronous
  269. signal arrives while executing non-Go code, the Go runtime will invoke
  270. the existing signal handler instead of the Go signal handler.
  271. </p>
  272. <p>
  273. Go code built with -buildmode=c-archive or -buildmode=c-shared will
  274. not install any other signal handlers by default. If there is an
  275. existing signal handler, the Go runtime will turn on the SA_ONSTACK
  276. flag and otherwise keep the signal handler. If Notify is called for an
  277. asynchronous signal, a Go signal handler will be installed for that
  278. signal. If, later, Reset is called for that signal, the original
  279. handling for that signal will be reinstalled, restoring the non-Go
  280. signal handler if any.
  281. </p>
  282. <p>
  283. Go code built without -buildmode=c-archive or -buildmode=c-shared will
  284. install a signal handler for the asynchronous signals listed above,
  285. and save any existing signal handler. If a signal is delivered to a
  286. non-Go thread, it will act as described above, except that if there is
  287. an existing non-Go signal handler, that handler will be installed
  288. before raising the signal.
  289. </p>
  290. <h3 id="hdr-Windows">Windows</h3>
  291. <p>
  292. On Windows a ^C (Control-C) or ^BREAK (Control-Break) normally cause
  293. the program to exit. If Notify is called for os.SIGINT, ^C or ^BREAK
  294. will cause os.SIGINT to be sent on the channel, and the program will
  295. not exit. If Reset is called, or Stop is called on all channels passed
  296. to Notify, then the default behavior will be restored.
  297. </p>
  298. <h3 id="hdr-Plan_9">Plan 9</h3>
  299. <p>
  300. On Plan 9, signals have type syscall.Note, which is a string. Calling
  301. Notify with a syscall.Note will cause that value to be sent on the
  302. channel when that string is posted as a note.
  303. </p>
  304. </div>
  305. </div>
  306. <div id="pkg-index" class="toggleVisible">
  307. <div class="collapsed">
  308. <h2 class="toggleButton" title="Click to show Index section">Index ▹</h2>
  309. </div>
  310. <div class="expanded">
  311. <h2 class="toggleButton" title="Click to hide Index section">Index ▾</h2>
  312. <!-- Table of contents for API; must be named manual-nav to turn off auto nav. -->
  313. <div id="manual-nav">
  314. <dl>
  315. <dd><a href="index.html#Ignore">func Ignore(sig ...os.Signal)</a></dd>
  316. <dd><a href="index.html#Notify">func Notify(c chan&lt;- os.Signal, sig ...os.Signal)</a></dd>
  317. <dd><a href="index.html#Reset">func Reset(sig ...os.Signal)</a></dd>
  318. <dd><a href="index.html#Stop">func Stop(c chan&lt;- os.Signal)</a></dd>
  319. </dl>
  320. </div><!-- #manual-nav -->
  321. <div id="pkg-examples">
  322. <h4>Examples</h4>
  323. <dl>
  324. <dd><a class="exampleLink" href="index.html#example_Notify">Notify</a></dd>
  325. </dl>
  326. </div>
  327. <h4>Package files</h4>
  328. <p>
  329. <span style="font-size:90%">
  330. <a href="http://localhost:6060/src/os/signal/doc.go">doc.go</a>
  331. <a href="http://localhost:6060/src/os/signal/signal.go">signal.go</a>
  332. <a href="http://localhost:6060/src/os/signal/signal_unix.go">signal_unix.go</a>
  333. </span>
  334. </p>
  335. </div><!-- .expanded -->
  336. </div><!-- #pkg-index -->
  337. <div id="pkg-callgraph" class="toggle" style="display: none">
  338. <div class="collapsed">
  339. <h2 class="toggleButton" title="Click to show Internal Call Graph section">Internal call graph ▹</h2>
  340. </div> <!-- .expanded -->
  341. <div class="expanded">
  342. <h2 class="toggleButton" title="Click to hide Internal Call Graph section">Internal call graph ▾</h2>
  343. <p>
  344. In the call graph viewer below, each node
  345. is a function belonging to this package
  346. and its children are the functions it
  347. calls&mdash;perhaps dynamically.
  348. </p>
  349. <p>
  350. The root nodes are the entry points of the
  351. package: functions that may be called from
  352. outside the package.
  353. There may be non-exported or anonymous
  354. functions among them if they are called
  355. dynamically from another package.
  356. </p>
  357. <p>
  358. Click a node to visit that function's source code.
  359. From there you can visit its callers by
  360. clicking its declaring <code>func</code>
  361. token.
  362. </p>
  363. <p>
  364. Functions may be omitted if they were
  365. determined to be unreachable in the
  366. particular programs or tests that were
  367. analyzed.
  368. </p>
  369. <!-- Zero means show all package entry points. -->
  370. <ul style="margin-left: 0.5in" id="callgraph-0" class="treeview"></ul>
  371. </div>
  372. </div> <!-- #pkg-callgraph -->
  373. <h2 id="Ignore">func <a href="http://localhost:6060/src/os/signal/signal.go?s=1546:1575#L62">Ignore</a></h2>
  374. <pre>func Ignore(sig ...<a href="../index.html">os</a>.<a href="../index.html#Signal">Signal</a>)</pre>
  375. <p>
  376. Ignore causes the provided signals to be ignored. If they are received by
  377. the program, nothing will happen. Ignore undoes the effect of any prior
  378. calls to Notify for the provided signals.
  379. If no signals are provided, all incoming signals will be ignored.
  380. </p>
  381. <h2 id="Notify">func <a href="http://localhost:6060/src/os/signal/signal.go?s=2413:2462#L82">Notify</a></h2>
  382. <pre>func Notify(c chan&lt;- <a href="../index.html">os</a>.<a href="../index.html#Signal">Signal</a>, sig ...<a href="../index.html">os</a>.<a href="../index.html#Signal">Signal</a>)</pre>
  383. <p>
  384. Notify causes package signal to relay incoming signals to c.
  385. If no signals are provided, all incoming signals will be relayed to c.
  386. Otherwise, just the provided signals will.
  387. </p>
  388. <p>
  389. Package signal will not block sending to c: the caller must ensure
  390. that c has sufficient buffer space to keep up with the expected
  391. signal rate. For a channel used for notification of just one signal value,
  392. a buffer of size 1 is sufficient.
  393. </p>
  394. <p>
  395. It is allowed to call Notify multiple times with the same channel:
  396. each call expands the set of signals sent to that channel.
  397. The only way to remove signals from the set is to call Stop.
  398. </p>
  399. <p>
  400. It is allowed to call Notify multiple times with different channels
  401. and the same signals: each channel receives copies of incoming
  402. signals independently.
  403. </p>
  404. <div id="example_Notify" class="toggle">
  405. <div class="collapsed">
  406. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  407. </div>
  408. <div class="expanded">
  409. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  410. <p>Code:</p>
  411. <pre class="code">
  412. <span class="comment">// Set up channel on which to send signal notifications.</span>
  413. <span class="comment">// We must use a buffered channel or risk missing the signal</span>
  414. <span class="comment">// if we&#39;re not ready to receive when the signal is sent.</span>
  415. c := make(chan os.Signal, 1)
  416. signal.Notify(c, os.Interrupt)
  417. <span class="comment">// Block until a signal is received.</span>
  418. s := &lt;-c
  419. fmt.Println(&#34;Got signal:&#34;, s)
  420. </pre>
  421. </div>
  422. </div>
  423. <h2 id="Reset">func <a href="http://localhost:6060/src/os/signal/signal.go?s=3176:3204#L126">Reset</a></h2>
  424. <pre>func Reset(sig ...<a href="../index.html">os</a>.<a href="../index.html#Signal">Signal</a>)</pre>
  425. <p>
  426. Reset undoes the effect of any prior calls to Notify for the provided
  427. signals.
  428. If no signals are provided, all signal handlers will be reset.
  429. </p>
  430. <h2 id="Stop">func <a href="http://localhost:6060/src/os/signal/signal.go?s=3446:3475#L133">Stop</a></h2>
  431. <pre>func Stop(c chan&lt;- <a href="../index.html">os</a>.<a href="../index.html#Signal">Signal</a>)</pre>
  432. <p>
  433. Stop causes package signal to stop relaying incoming signals to c.
  434. It undoes the effect of all prior calls to Notify using c.
  435. When Stop returns, it is guaranteed that c will receive no more signals.
  436. </p>
  437. <div id="footer">
  438. Build version go1.6.<br>
  439. Except as <a href="https://developers.google.com/site-policies#restrictions">noted</a>,
  440. the content of this page is licensed under the
  441. Creative Commons Attribution 3.0 License,
  442. and code is licensed under a <a href="http://localhost:6060/LICENSE">BSD license</a>.<br>
  443. <a href="http://localhost:6060/doc/tos.html">Terms of Service</a> |
  444. <a href="http://www.google.com/intl/en/policies/privacy/">Privacy Policy</a>
  445. </div>
  446. </div><!-- .container -->
  447. </div><!-- #page -->
  448. <!-- TODO(adonovan): load these from <head> using "defer" attribute? -->
  449. <script type="text/javascript" src="../../../lib/godoc/jquery.js"></script>
  450. <script type="text/javascript" src="../../../lib/godoc/jquery.treeview.js"></script>
  451. <script type="text/javascript" src="../../../lib/godoc/jquery.treeview.edit.js"></script>
  452. <script type="text/javascript" src="../../../lib/godoc/godocs.js"></script>
  453. </body>
  454. </html>