OSDN Git Service

297f4c42b07c91fe640f34e861e1849a4b06fb16
[linuxjm/LDP_man-pages.git] / draft / man2 / signal.2
1 .\" Copyright (c) 2000 Andries Brouwer <aeb@cwi.nl>
2 .\" and Copyright (c) 2008, Linux Foundation, written by Michael Kerrisk
3 .\"      <mtk.manpages@gmail.com>
4 .\" and Copyright (c) 2008, Linux Foundation, written by Michael Kerrisk
5 .\"      <mtk.manpages@gmail.com>
6 .\" based on work by Rik Faith <faith@cs.unc.edu>
7 .\" and Mike Battersby <mike@starbug.apana.org.au>.
8 .\"
9 .\" Permission is granted to make and distribute verbatim copies of this
10 .\" manual provided the copyright notice and this permission notice are
11 .\" preserved on all copies.
12 .\"
13 .\" Permission is granted to copy and distribute modified versions of this
14 .\" manual under the conditions for verbatim copying, provided that the
15 .\" entire resulting derived work is distributed under the terms of a
16 .\" permission notice identical to this one.
17 .\"
18 .\" Since the Linux kernel and libraries are constantly changing, this
19 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
20 .\" responsibility for errors or omissions, or for damages resulting from
21 .\" the use of the information contained herein.  The author(s) may not
22 .\" have taken the same level of care in the production of this manual,
23 .\" which is licensed free of charge, as they might when working
24 .\" professionally.
25 .\"
26 .\" Formatted or processed versions of this manual, if unaccompanied by
27 .\" the source, must acknowledge the copyright and authors of this work.
28 .\"
29 .\" Modified 2004-11-19, mtk:
30 .\" added pointer to sigaction.2 for details of ignoring SIGCHLD
31 .\" 2007-06-03, mtk: strengthened portability warning, and rewrote
32 .\"     various sections.
33 .\" 2008-07-11, mtk: rewrote and expanded portability discussion.
34 .\"
35 .\" Japanese Version Copyright (c) 1997 HANATAKA Shinya
36 .\"         all rights reserved.
37 .\" Translated 1997-03-03, HANATAKA Shinya <hanataka@abyss.rim.or.jp>
38 .\" Modified 2000-09-24, HANATAKA Shinya <hanataka@abyss.rim.or.jp>
39 .\" Updated & Modified 2002-01-14, Yuichi SATO <ysato@h4.dion.ne.jp>
40 .\" Updated & Modified 2004-01-17, Yuichi SATO <ysato444@yahoo.co.jp>
41 .\" Updated & Modified 2005-01-07, Yuichi SATO
42 .\" Updated 2007-06-01, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v2.50
43 .\" Updated 2007-06-13, Akihiro MOTOKI, LDP v2.55
44 .\" Updated 2008-08-08, Akihiro MOTOKI, LDP v3.05
45 .\"
46 .\"WORD:        signal                  シグナル
47 .\"WORD:        signal handler          シグナル・ハンドラ
48 .\"WORD:        library                 ライブラリ
49 .\"WORD:        semantics               方式
50 .\"WORD:        prototype               型宣言
51 .\"WORD:        architecture            アーキテクチャー
52 .\"WORD:        endless loop            無限ループ
53 .\"WORD:        feature test macro      機能検査マクロ
54 .\"WORD:        disposition             処理方法
55 .\"
56 .TH SIGNAL 2 2008-07-11 "Linux" "Linux Programmer's Manual"
57 .\"O .SH NAME
58 .SH 名前
59 .\"O signal \- ANSI C signal handling
60 signal \- ANSI C シグナル操作
61 .\"O .SH SYNOPSIS
62 .SH 書式
63 .B #include <signal.h>
64 .sp
65 .B typedef void (*sighandler_t)(int);
66 .sp
67 .\"O .BI "sighandler_t signal(int " signum ", sighandler_t " handler );
68 .BI "sighandler_t signal(int " signum ", sighandler_t " sighandler );
69 .\"O .SH DESCRIPTION
70 .SH 説明
71 .\"O The behavior of
72 .\"O .BR signal ()
73 .\"O varies across UNIX versions,
74 .\"O and has also varied historically across different versions of Linux.
75 .\"O \fBAvoid its use\fP: use
76 .\"O .BR sigaction (2)
77 .\"O instead.
78 .\"O See \fIPortability\fP below.
79 .BR signal ()
80 の動作は UNIX のバージョンにより異なる。
81 また、歴史的に見て Linux のバージョンによっても異なっている。
82 \fBこのシステムコールの使用は避け、\fP
83 代わりに
84 .BR sigaction (2)
85 を使用すること。
86 下記の「移植性」を参照。
87
88 .\"O .BR signal ()
89 .\"O sets the disposition of the signal
90 .\"O .I signum
91 .\"O to
92 .\"O .IR handler ,
93 .\"O which is either
94 .\"O .BR SIG_IGN ,
95 .\"O .BR SIG_DFL ,
96 .\"O or the address of a programmer-defined function (a "signal handler").
97 .BR signal ()
98 はシグナル
99 .I signum
100 の処理方法を
101 .I handler
102 に設定する。
103 .I handler
104 には、
105 .BR SIG_IGN "、"
106 .BR SIG_DFL "、"
107 プログラマが定義した関数 (「シグナル・ハンドラ」) のアドレスの
108 いずれかを指定する。
109
110 .\"O If the signal
111 .\"O .I signum
112 .\"O is delivered to the process, then one of the following happens:
113 シグナル
114 .I signum
115 がプロセスに配送されると、以下のいずれかが発生する。
116 .TP 3
117 *
118 .\"O If the disposition is set to
119 .\"O .BR SIG_IGN ,
120 .\"O then the signal is ignored.
121 処理方法が
122 .B SIG_IGN
123 に設定されている場合、そのシグナルは無視される。
124 .TP
125 *
126 .\"O If the disposition is set to
127 .\"O .BR SIG_DFL ,
128 .\"O then the default action associated with the signal (see
129 .\"O .BR signal (7))
130 .\"O occurs.
131 処理方法が
132 .B SIG_DFL
133 に設定されている場合、シグナルに関連づけられた
134 デフォルトの動作が行われる
135 .RB ( signal (7)
136 参照)。
137 .TP
138 *
139 .\"O If the disposition is set to a function,
140 .\"O then first either the disposition is reset to
141 .\"O .BR SIG_DFL ,
142 .\"O or the signal is blocked (see \fIPortability\fP below), and then
143 .\"O .I handler
144 .\"O is called with argument
145 .\"O .IR signum .
146 処理方法として関数が設定されている場合、
147 まず最初に処理方法が
148 .B SIG_DFL
149 にリセットされるかそのシグナルのブロックが実行された後、
150 .I signum
151 を引き数として
152 .I handler
153 が呼び出される。
154 .\"O If invocation of the handler caused the signal to be blocked,
155 .\"O then the signal is unblocked upon return from the handler.
156 ハンドラが起動される際にシグナルがブロックされた場合、
157 ハンドラが返る際にそのシグナルのブロックが解除される。
158 .PP
159 .\"O The signals
160 .\"O .B SIGKILL
161 .\"O and
162 .\"O .B SIGSTOP
163 .\"O cannot be caught or ignored.
164 シグナル
165 .B SIGKILL
166
167 .B SIGSTOP
168 は捕捉できず、無視することもできない。
169 .\"O .SH "RETURN VALUE"
170 .SH 返り値
171 .\"O .BR signal ()
172 .\"O returns the previous value of the signal handler, or
173 .\"O .B SIG_ERR
174 .\"O on error.
175 .BR signal ()
176 は、今までのシグナル・ハンドラの値を返す。エラーの場合は
177 .B SIG_ERR
178 を返す。
179 .\"O .SH ERRORS
180 .SH エラー
181 .TP
182 .B EINVAL
183 .\"O .I signum
184 .\"O is invalid.
185 .I signum
186 が不正である。
187 .\"O .SH "CONFORMING TO"
188 .SH 準拠
189 C89, C99, POSIX.1-2001.
190 .\"O .SH NOTES
191 .SH 注意
192 .\"O The effects of
193 .\"O .BR signal ()
194 .\"O in a multithreaded process are unspecified.
195 マルチスレッドプロセスにおける
196 .BR signal ()
197 の結果は、指定されていない。
198 .PP
199 .\"O According to POSIX, the behavior of a process is undefined after it
200 .\"O ignores a
201 .\"O .BR SIGFPE ,
202 .\"O .BR SIGILL ,
203 .\"O or
204 .\"O .B SIGSEGV
205 .\"O signal that was not generated by
206 .\"O .BR kill (2)
207 .\"O or
208 .\"O .BR raise (3).
209 POSIX によると、
210 .BR kill (2)
211
212 .BR raise (3)
213 で生成できない
214 .BR SIGFPE ,
215 .BR SIGILL ,
216 .B SIGSEGV
217 シグナルを無視した後の動作は未定義である。
218 .\"O Integer division by zero has undefined result.
219 .\"O On some architectures it will generate a
220 .\"O .B SIGFPE
221 .\"O signal.
222 .\"O (Also dividing the most negative integer by \-1 may generate
223 .\"O .BR SIGFPE .)
224 .\"O Ignoring this signal might lead to an endless loop.
225 0 による整数割り算の結果は未定義となる。
226 ある種のアーキテクチャでは、これは
227 .B SIGFPE
228 シグナルを生成する。
229 (同様に負の最大整数を \-1 で割ると
230 .B SIGFPE
231 が生成されるかもしれない。)
232 このシグナルを無視すると無限ループに陥るかもしれない。
233 .PP
234 .\"O See
235 .\"O .BR sigaction (2)
236 .\"O for details on what happens when
237 .\"O .B SIGCHLD
238 .\"O is set to
239 .\"O .BR SIG_IGN .
240 .B SIGCHLD
241 の動作として
242 .B SIG_IGN
243 を設定した場合の詳細な動作については、
244 .BR sigaction (2)
245 を参照すること。
246 .PP
247 .\"O See
248 .\"O .BR signal (7)
249 .\"O for a list of the async-signal-safe functions that can be
250 .\"O safely called from inside a signal handler.
251 シグナル・ハンドラ内から安全に呼び出すことができる、
252 async-signal-safe functions (非同期シングルで安全な関数) の
253 リストについては
254 .BR signal (7)
255 を参照。
256 .PP
257 .\"O The use of
258 .\"O .I sighandler_t
259 .\"O is a GNU extension.
260 .I sighandler_t
261 の使用は GNU 拡張である。
262 .\"O Various versions of libc predefine this type; libc4 and libc5 define
263 .\"O .IR SignalHandler ;
264 .\"O glibc defines
265 .\"O .I sig_t
266 .\"O and, when
267 .\"O .B _GNU_SOURCE
268 .\"O is defined, also
269 .\"O .IR sighandler_t .
270 各種バージョンの libc でこの型は定義済みである;
271 libc4 と libc5 では
272 .I SignalHandler
273 を定義している。
274 glibc では
275 .I sig_t
276 を定義しており、
277 .B _GNU_SOURCE
278 が定義されている場合には
279 .I sighandler_t
280 も定義されている。
281 .\"O Without use of such a type, the declaration of
282 .\"O .BR signal ()
283 .\"O is the somewhat harder to read:
284 このような型を使用しないと、
285 .BR signal ()
286 の宣言は読みにくいものとなる。
287 .in +4n
288 .nf
289
290 .BI "void ( *" signal "(int " signum ", void (*" handler ")(int)) ) (int);"
291 .fi
292 .in
293 .\"O .SS PORTABILITY
294 .SS 移植性
295 .\"O The only portable use of
296 .\"O .BR signal ()
297 .\"O is to set a signal's disposition to
298 .\"O .BR SIG_DFL
299 .\"O or
300 .\"O .BR SIG_IGN .
301 .\"O The semantics when using
302 .\"O .BR signal ()
303 .\"O to establish a signal handler vary across systems
304 .\"O (and POSIX.1 explicitly permits this variation);
305 .\"O .B do not use it for this purpose.
306 移植性のある
307 .BR signal ()
308 の使い方は、シグナルの処理方法を
309 .BR SIG_DFL
310
311 .BR SIG_IGN
312 に設定する方法だけである。
313 シグナル・ハンドラを設定するのに
314 .BR signal ()
315 を使ったときの動作はシステムにより異なる
316 (POSIX.1 は明示的にこの違いを認めている)。
317 .B "移植性が必要なときはこのシステムコールを使用しないこと。"
318
319 .\"O POSIX.1 solved the portability mess by specifying
320 .\"O .BR sigaction (2),
321 .\"O which provides explicit control of the semantics when a
322 .\"O signal handler is invoked; use that interface instead of
323 .\"O .BR signal ().
324 POSIX.1 は、
325 .BR sigaction (2)
326 を規定することで移植性に関する混乱を解決した。
327 .BR sigaction (2)
328 はシグナル・ハンドラが起動される際の挙動を明示的に制御できる。
329 .BR signal ()
330 の代わりにこのインターフェイスを使うこと。
331
332 .\"O In the original UNIX systems, when a handler that was established using
333 .\"O .BR signal ()
334 .\"O was invoked by the delivery of a signal,
335 .\"O the disposition of the signal would be reset to
336 .\"O .BR SIG_DFL ,
337 .\"O and the system did not block delivery of further instances of the signal.
338 .\"O System V also provides these semantics for
339 .\"O .BR signal ().
340 .\"O This was bad because the signal might be delivered again
341 .\"O before the handler had a chance to reestablish itself.
342 .\"O Furthermore, rapid deliveries of the same signal could
343 .\"O result in recursive invocations of the handler.
344 オリジナルの UNIX システムでは、
345 .BR signal ()
346 を使って設定されたハンドラがシグナルの配送により起動されると、
347 そのシグナルの処理方法は
348 .B SIG_DFL
349 にリセットされ、システムは同じシグナルがさらに生成されても
350 シグナルの配送をブロックしなかった。
351 System V でも、
352 .BR signal ()
353 に対してこれらの挙動を規定している。
354 こうした挙動はまずく、ハンドラがハンドラ自身を再設定する機会が
355 来るより前に、同じシグナルがまた配送される可能性がある。
356 さらに、同じシグナルが立て続けに配送されると、同じシグナルが
357 ハンドラを繰り返し起動されることになる。
358
359 .\"O BSD improved on this situation by changing the semantics of
360 .\"O signal handling
361 .\"O (but, unfortunately, silently changed the semantics
362 .\"O when establishing a handler with
363 .\"O .BR signal ()).
364 .\"O On BSD, when a signal handler is invoked,
365 .\"O the signal disposition is not reset,
366 .\"O and further instances of the signal are blocked from
367 .\"O being delivered while the handler is executing.
368 BSD では、シグナル処理の挙動を変更することで、この状況を改善した
369 (しかし、残念なことに、
370 .BR signal ()
371 を使ってハンドラを設定する際に挙動が黙って変更される)。
372 BSD では、シグナルハンドラが起動された際、
373 シグナルの処理方法はリセットされず、
374 ハンドラの実行中は、同じシグナルのさらなる生成は配送がブロックされる。
375
376 .\"O The situation on Linux is as follows:
377 Linux での状況は以下の通りである。
378 .IP * 2
379 .\"O The kernel's
380 .\"O .BR signal ()
381 .\"O system call provides System V semantics.
382 カーネルの
383 .BR signal ()
384 システムコールは System V 方式を提供している。
385 .IP *
386 .\"O By default, in glibc 2 and later, the
387 .\"O .BR signal ()
388 .\"O wrapper function does not invoke the kernel system call.
389 .\"O Instead, it calls
390 .\"O .BR sigaction (2)
391 .\"O using flags that supply BSD semantics.
392 デフォルトでは、glibc 2 以降では、
393 .BR signal ()
394 ラッパー関数はカーネルのシステムコールを起動しない。
395 代わりに、ラッパー関数は BSD 方式を示すフラグを使って
396 .BR sigaction (2)
397 を呼び出す。
398 .\"O This default behavior is provided as long as the
399 .\"O .B _BSD_SOURCE
400 .\"O feature test macro is defined.
401 .\"O By default,
402 .\"O .B _BSD_SOURCE
403 .\"O is defined;
404 .\"O it is also implicitly defined if one defines
405 .\"O .BR _GNU_SOURCE ,
406 .\"O and can of course be explicitly defined.
407 機能検査マクロ
408 .B _BSD_SOURCE
409 を定義していれば、このデフォルトの動作となる。
410 デフォルトでは、
411 .B _BSD_SOURCE
412 が定義される。
413 .B _BSD_SOURCE
414
415 .B _GNU_SOURCE
416 が定義された場合には暗黙のうちに定義され、
417 もちろん明示的に定義することもできる。
418 .sp
419 .\"O On glibc 2 and later, if the
420 .\"O .B _BSD_SOURCE
421 .\"O feature test macro is not defined, then
422 .\"O .BR signal ()
423 .\"O provides System V semantics.
424 glibc 2 以降では、機能検査マクロ
425 .B _BSD_SOURCE
426 が定義されていなければ、
427 .BR signal ()
428 は System V 方式となる。
429 .\"O (The default implicit definition of
430 .\"O .B _BSD_SOURCE
431 .\"O is not provided if one invokes
432 .\"O .BR gcc (1)
433 .\"O in one of its standard modes
434 .\"O .RI ( -std=xxx " or " -ansi )
435 .\"O or defines various other feature test macros such as
436 .\"O .BR _POSIX_SOURCE ,
437 .\"O .BR _XOPEN_SOURCE ,
438 .\"O or
439 .\"O .BR _SVID_SOURCE ;
440 .\"O see
441 .\"O .BR feature_test_macros (7).)
442 .RB ( gcc (1)
443 が標準指定モード
444 .RI ( -std=xxx " or " -ansi )
445 で起動された場合、もしくは
446 .BR _POSIX_SOURCE ,
447 .BR _XOPEN_SOURCE ,
448 .B _SVID_SOURCE
449 といった他の様々な機能検査マクロが定義された場合、
450 デフォルトの
451 .B _BSD_SOURCE
452 の暗黙の定義は行われない。
453 .BR feature_test_macros (7)
454 を参照のこと。)
455 .\"
456 .\" System V semantics are also provided if one uses the separate
457 .\" .BR sysv_signal (3)
458 .\" function.
459 .IP *
460 .\"O The
461 .\"O .BR signal ()
462 .\"O function in Linux libc4 and libc5 provide System V semantics.
463 .\"O If one on a libc5 system includes
464 .\"O .I <bsd/signal.h>
465 .\"O instead of
466 .\"O .IR <signal.h> ,
467 .\"O then
468 .\"O .BR signal ()
469 .\"O provides BSD semantics.
470 Linux の libc4 と libc5 の
471 .BR signal ()
472 関数は System V 方式である。
473 libc5 システムにおいて
474 .I <signal.h>
475 のかわりに
476 .I <bsd/signal.h>
477 をインクルードすると、
478 .BR signal ()
479
480 .BR __bsd_signal ()
481 に再定義され、
482 .BR signal ()
483 は BSD 方式となる。
484 .\"O .SH "SEE ALSO"
485 .SH 関連項目
486 .BR kill (1),
487 .BR alarm (2),
488 .BR kill (2),
489 .BR killpg (2),
490 .BR pause (2),
491 .BR sigaction (2),
492 .BR signalfd (2),
493 .BR sigpending (2),
494 .BR sigprocmask (2),
495 .BR sigsuspend (2),
496 .BR bsd_signal (3),
497 .BR raise (3),
498 .BR siginterrupt (3),
499 .BR sigqueue (3),
500 .BR sigsetops (3),
501 .BR sigvec (3),
502 .BR sysv_signal (3),
503 .BR signal (7)