.\" Copyright (c) 2000 Andries Brouwer .\" and Copyright (c) 2007 Michael Kerrisk .\" and Copyright (c) 2008, Linux Foundation, written by Michael Kerrisk .\" .\" based on work by Rik Faith .\" and Mike Battersby . .\" .\" %%%LICENSE_START(VERBATIM) .\" Permission is granted to make and distribute verbatim copies of this .\" manual provided the copyright notice and this permission notice are .\" preserved on all copies. .\" .\" Permission is granted to copy and distribute modified versions of this .\" manual under the conditions for verbatim copying, provided that the .\" entire resulting derived work is distributed under the terms of a .\" permission notice identical to this one. .\" .\" Since the Linux kernel and libraries are constantly changing, this .\" manual page may be incorrect or out-of-date. The author(s) assume no .\" responsibility for errors or omissions, or for damages resulting from .\" the use of the information contained herein. The author(s) may not .\" have taken the same level of care in the production of this manual, .\" which is licensed free of charge, as they might when working .\" professionally. .\" .\" Formatted or processed versions of this manual, if unaccompanied by .\" the source, must acknowledge the copyright and authors of this work. .\" %%%LICENSE_END .\" .\" Modified 2004-11-19, mtk: .\" added pointer to sigaction.2 for details of ignoring SIGCHLD .\" 2007-06-03, mtk: strengthened portability warning, and rewrote .\" various sections. .\" 2008-07-11, mtk: rewrote and expanded portability discussion. .\" .\"******************************************************************* .\" .\" This file was generated with po4a. Translate the source file. .\" .\"******************************************************************* .\" .\" Japanese Version Copyright (c) 1997 HANATAKA Shinya .\" all rights reserved. .\" Translated 1997-03-03, HANATAKA Shinya .\" Modified 2000-09-24, HANATAKA Shinya .\" Updated & Modified 2002-01-14, Yuichi SATO .\" Updated & Modified 2004-01-17, Yuichi SATO .\" Updated & Modified 2005-01-07, Yuichi SATO .\" Updated 2007-06-01, Akihiro MOTOKI , LDP v2.50 .\" Updated 2007-06-13, Akihiro MOTOKI, LDP v2.55 .\" Updated 2008-08-08, Akihiro MOTOKI, LDP v3.05 .\" Updated 2012-05-29, Akihiro MOTOKI .\" Updated 2013-05-06, Akihiro MOTOKI .\" Updated 2013-07-22, Akihiro MOTOKI .\" .TH SIGNAL 2 2017\-09\-15 Linux "Linux Programmer's Manual" .SH 名前 signal \- ANSI C シグナル操作 .SH 書式 \fB#include \fP .PP \fBtypedef void (*sighandler_t)(int);\fP .PP \fBsighandler_t signal(int \fP\fIsignum\fP\fB, sighandler_t \fP\fIsighandler\fP\fB);\fP .SH 説明 \fB警告\fP: \fBsignal\fP() の動作は UNIX のバージョンにより異なる。 また、歴史的に見て Linux のバージョンによっても異なっている。 \fBこのシステムコールの使用は避け、\fP 代わりに \fBsigaction\fP(2) を使用すること。 下記の「移植性」を参照。 .PP \fBsignal\fP() はシグナル \fIsignum\fP の処理方法を \fIhandler\fP に設定する。 \fIhandler\fP には、 \fBSIG_IGN\fP、 \fBSIG_DFL\fP、 プログラマが定義した関数 (「シグナルハンドラー」) のアドレスの いずれかを指定する。 .PP シグナル \fIsignum\fP がプロセスに配送されると、以下のいずれかが発生する。 .TP 3 * 処理方法が \fBSIG_IGN\fP に設定されている場合、そのシグナルは無視される。 .TP * 処理方法が \fBSIG_DFL\fP に設定されている場合、シグナルに関連づけられた デフォルトの動作が行われる (\fBsignal\fP(7) 参照)。 .TP * 処理方法として関数が設定されている場合、 まず最初に処理方法が \fBSIG_DFL\fP にリセットされるかそのシグナルのブロックが実行された後、 \fIsignum\fP を引数として \fIhandler\fP が呼び出される。 ハンドラーが起動される際にシグナルがブロックされた場合、 ハンドラーが返る際にそのシグナルのブロックが解除される。 .PP シグナル \fBSIGKILL\fP と \fBSIGSTOP\fP は捕捉できず、無視することもできない。 .SH 返り値 \fBsignal\fP() は、今までのシグナルハンドラーの値を返す。 エラーの場合は \fBSIG_ERR\fP を返し、 \fIerrno\fP にエラーの原因を示す値を設定する。 .SH エラー .TP \fBEINVAL\fP \fIsignum\fP が不正である。 .SH 準拠 POSIX.1\-2001, POSIX.1\-2008, C89, C99. .SH 注意 マルチスレッドプロセスにおける \fBsignal\fP() の結果は、指定されていない。 .PP POSIX では、 \fBkill\fP(2) や \fBraise\fP(3) で生成できないシグナル \fBSIGFPE\fP, \fBSIGILL\fP, \fBSIGSEGV\fP を無視 (ignore) した場合、その後の動作は未定義である。 ゼロによる整数割り算の結果は未定義となる。 アーキテクチャーによっては、このとき \fBSIGFPE\fP シグナルが生成される。 (同様に負の最大整数を \-1 で割ると \fBSIGFPE\fP が生成されるかもしれない) このシグナルを無視すると無限ループに陥るかもしれない。 .PP See \fBsigaction\fP(2) for details on what happens when the disposition \fBSIGCHLD\fP is set to \fBSIG_IGN\fP. .PP シグナルハンドラー内から安全に呼び出すことができる、 async\-signal\-safe functions (非同期シグナルで安全な関数) のリストについては \fBsignal\-safety\fP(7) を参照。 .PP .\" libc4 and libc5 define .\" .IR SignalHandler ; The use of \fIsighandler_t\fP is a GNU extension, exposed if \fB_GNU_SOURCE\fP is defined; glibc also defines (the BSD\-derived) \fIsig_t\fP if \fB_BSD_SOURCE\fP (glibc 2.19 and earlier) or \fB_DEFAULT_SOURCE\fP (glibc 2.19 and later) is defined. Without use of such a type, the declaration of \fBsignal\fP() is the somewhat harder to read: .PP .in +4n .EX \fBvoid ( *\fP\fIsignal\fP\fB(int \fP\fIsignum\fP\fB, void (*\fP\fIhandler\fP\fB)(int)) ) (int);\fP .EE .in .SS 移植性 移植性のある \fBsignal\fP() の使い方は、シグナルの処理方法を \fBSIG_DFL\fP か \fBSIG_IGN\fP に設定する方法だけである。 シグナルハンドラーを設定するのに \fBsignal\fP() を使ったときの動作はシステムにより異なる (POSIX.1 は明示的にこの違いを認めている)。 \fB移植性が必要なときはこのシステムコールを使用しないこと。\fP .PP POSIX.1 は、 \fBsigaction\fP(2) を規定することで移植性に関する混乱を解決した。 \fBsigaction\fP(2) はシグナルハンドラーが起動される際の挙動を明示的に制御できる。 \fBsignal\fP() の代わりにこのインターフェイスを使うこと。 .PP オリジナルの UNIX システムでは、 \fBsignal\fP() を使って設定されたハンドラーがシグナルの配送により起動されると、 そのシグナルの処理方法は \fBSIG_DFL\fP にリセットされ、システムは同じシグナルがさらに生成されても シグナルの配送をブロックしなかった。これは、以下のフラグで \fBsigaction\fP(2) を呼び出すのと等価である。 .PP .in +4n .EX sa.sa_flags = SA_RESETHAND | SA_NODEFER; .EE .in .PP System\ V でも、 \fBsignal\fP() に対してこれらの挙動を規定している。 こうした挙動はまずく、ハンドラーがハンドラー自身を再設定する機会が 来るより前に、同じシグナルがまた配送される可能性がある。 さらに、同じシグナルが立て続けに配送されると、同じシグナルが ハンドラーを繰り返し起動されることになる。 .PP BSD はこの状況が改善したが、残念なことに、その過程で既存の \fBsignal\fP() の挙動も変更された。 BSD では、シグナルハンドラーが起動された際、 シグナルの処理方法はリセットされず、 ハンドラーの実行中は、同じシグナルのさらなる生成は配送がブロックされる。 また、 シグナルハンドラーが中断された場合、 停止中のシステムコールのいくつかは自動的に再スタートされる。 BSD の挙動は、 以下のフラグを指定した \fBsigaction\fP(2) の呼び出しと等価である。 .PP .in +4n .EX sa.sa_flags = SA_RESTART; .EE .in .PP Linux での状況は以下の通りである。 .IP * 2 カーネルの \fBsignal\fP() システムコールは System\ V 方式を提供している。 .IP * .\" .\" System V semantics are also provided if one uses the separate .\" .BR sysv_signal (3) .\" function. .\" .IP * .\" The .\" .BR signal () .\" function in Linux libc4 and libc5 provide System\ V semantics. .\" If one on a libc5 system includes .\" .I .\" instead of .\" .IR , .\" then .\" .BR signal () .\" provides BSD semantics. By default, in glibc 2 and later, the \fBsignal\fP() wrapper function does not invoke the kernel system call. Instead, it calls \fBsigaction\fP(2) using flags that supply BSD semantics. This default behavior is provided as long as a suitable feature test macro is defined: \fB_BSD_SOURCE\fP on glibc 2.19 and earlier or \fB_DEFAULT_SOURCE\fP in glibc 2.19 and later. (By default, these macros are defined; see \fBfeature_test_macros\fP(7) for details.) If such a feature test macro is not defined, then \fBsignal\fP() provides System\ V semantics. .SH 関連項目 \fBkill\fP(1), \fBalarm\fP(2), \fBkill\fP(2), \fBpause\fP(2), \fBsigaction\fP(2), \fBsignalfd\fP(2), \fBsigpending\fP(2), \fBsigprocmask\fP(2), \fBsigsuspend\fP(2), \fBbsd_signal\fP(3), \fBkillpg\fP(3), \fBraise\fP(3), \fBsiginterrupt\fP(3), \fBsigqueue\fP(3), \fBsigsetops\fP(3), \fBsigvec\fP(3), \fBsysv_signal\fP(3), \fBsignal\fP(7) .SH この文書について この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 5.10 の一部である。プロジェクトの説明とバグ報告に関する情報は \%https://www.kernel.org/doc/man\-pages/ に書かれている。