sourCEntral - mobile manpages

pdf

SIGNAL

名前

signal - シグナルの概要

説明

Linux は POSIX 信頼シグナル (reliable signal; 以後 "標準シグナル"と表記) と POSIX リアルタイムシグナルの両方に対応している。

シグナル処理方法
シグナルはそれぞれ現在の「処理方法 (disposition)」を保持しており、 この処理方法によりシグナルが配送された際にプロセスが どのような振舞いをするかが決まる。

後述の表の "動作" の欄のエントリーは各シグナルのデフォルトの 処理方法を示しており、以下のような意味を持つ。

Term

デフォルトの動作はプロセス終了。

Ign

デフォルトの動作はこのシグナルの無視。

Core

デフォルトの動作はプロセス終了とコアダンプ出力 (core(5) 参照)。

Stop

デフォルトの動作はプロセスの一時停止。

Cont

デフォルトの動作は、プロセスが停止中の場合にその実行の再開。

プロセスは、 sigaction(2)signal(2) を使って、シグナルの処理方法を変更することができる (signal(2) の方がシグナルハンドラーを設定する際の移植性が低い; 詳細は signal(2) を参照)。 シグナルの配送時に起こる動作として プロセスが選択できるのは、次のいずれか一つである。 デフォルトの動作を実行する、シグナルを無視する、 シグナルハンドラー (signal handler) でシグナルを捕捉する。シグナルハンドラーとは、シグナル配送時に 自動的に起動されるプログラマ定義の関数である。

デフォルトでは、シグナルハンドラーは通常のプロセスのスタック上で起動される。 シグナルハンドラーが代替スタック (alternate stack) を使用するように設定する こともできる。代替スタックを使用するように設定する方法と、どのような際に 代替スタックが役に立つかについての議論については sigaltstack(2) を参照のこと。

シグナルの処理方法はプロセス単位の属性である。 マルチスレッドのアプリケーションでは、あるシグナルの処理方法は 全てのスレッドで同じである。

fork(2) 経由で作成された子プロセスは、親プロセスのシグナルの処理方法の コピーを継承する。 execve(2) の前後で、ハンドラーが設定されているシグナルの処理方法はデフォルトにリセットされ、 無視が設定されているシグナルの処理方法は変更されずそのままとなる。

シグナルの送信
以下のシステムコールとライブラリ関数を使って、 呼び出し者はシグナルを送信することができる。
raise
(3)

呼び出したスレッドにシグナルを送る。

kill(2)

指定されたプロセスや、指定されたプロセスグループの全メンバー、 システムの全プロセスにシグナルを送る。

pidfd_send_signal(2)

Sends a signal to a process identified by a PID file descriptor.

killpg(3)

指定されたプロセスグループの全メンバーにシグナルを送る。

pthread_kill(3)

呼び出し者と同じプロセス内の指定された POSIX スレッドにシグナルを送る。

tgkill(2)

指定されたプロセス内の指定されたスレッドにシグナルを送る (このシステムコールを使って pthread_kill(3) は実装されている)。

sigqueue(3)

指定されたプロセスに付属データとともにリアルタイムシグナルを送る。

シグナルが捕捉されるのを待つ
以下のシステムコールを使って、シグナルが捕捉されるまで 呼び出したスレッドの実行を中断 (suspend) することができる (ハンドラーが設定されていないシグナルによりそのプロセスが終了した 場合にも実行の停止は終了する)。
pause
(2)

何かシグナルが捕捉されるまで実行を停止する。

sigsuspend(2)

一時的にシグナルマスク (下記参照) を変更し、 マスクされていないシグナルのいずれかが捕捉されるまで 実行を中断する。

シグナルの同期受信
シグナルハンドラー経由でシグナルを非同期 (asynchronously) で捕捉する以外にも、 シグナルを同期 (synchronously) して受け付けることもできる。 同期して受け付けるとは、シグナルが配送されるまで実行を停止 (block) するということである。シグナルを受け付けた際に、カーネルは そのシグナルに関する情報を呼び出し者に返す。 これを行う一般的な方法が二つある。

*

sigwaitinfo(2), sigtimedwait(2), sigwait(3) は、指定されたシグナル集合のシグナルの一つが配送されるまで実行を中断する。 どのシステムコールや関数でも、配送されたシグナルに関する情報が返される。

*

signalfd(2) が返すファイルディスクリプターを使うと、呼び出し元に配送された シグナルに関する情報を読み出すことができる。 このファイルディスクリプターからの read(2) は、 signalfd(2) の呼び出し時に指定されたシグナル集合のシグナルの一つが呼び出し元に 配送されるまで停止 (block) する。 read(2) が返すバッファーにはシグナルに関する情報を格納した構造体が入っている。

シグナルマスクと処理待ちシグナル
シグナルは ブロック (block) されることがある。ブロックされると、そのシグナルは その後ブロックを解除されるまで配送されなくなる。 シグナルが生成されてから配送されるまでの間、そのシグナルは 処理待ち (pending) であると呼ばれる。

プロセス内の各スレッドは、それぞれ独立な シグナルマスク (signal mask) を持つ。シグナルマスクはそのスレッドが現在ブロックしている シグナル集合を示すものである。 スレッドは、 pthread_sigmask(3) を使って自分のシグナルマスクを操作できる。 伝統的なシングルスレッドのアプリケーションでは、 sigprocmask(2) を使って、シグナルマスクを操作できる。

fork(2) 経由で作成された子プロセスは親プロセスのシグナルマスクのコピーを継承する。 execve(2) の前後でシグナルマスクは保持される。

A signal may be process-directed or thread-directed. A process-directed signal is one that is targeted at (and thus pending for) the process as a whole. A signal may be process-directed because it was generated by the kernel for reasons other than a hardware exception, or because it was sent using kill(2) or sigqueue(3). A thread-directed signal is one that is targeted at a specific thread. A signal may be thread-directed because it was generated as a consequence of executing a specific machine-language instruction that triggered a hardware exception (e.g., SIGSEGV for an invalid memory access, or SIGFPE for a math error), or because it was targeted at a specific thread using interfaces such as tgkill(2) or pthread_kill(3).

A process-directed signal may be delivered to any one of the threads that does not currently have the signal blocked. If more than one of the threads has the signal unblocked, then the kernel chooses an arbitrary thread to which to deliver the signal.

スレッドは、 sigpending(2) を使って、現在処理待ちのシグナル集合を取得することができる。 この集合は、プロセス宛ての処理待ちシグナルと 呼び出したスレッド宛てのシグナルの両方から構成される。

fork(2) 経由で作成された子プロセスでは、処理待ちのシグナル集合は空の集合で初期化される。 execve(2) の前後で、処理待ちのシグナル集合は保持される。

Execution of signal handlers
Whenever there is a transition from kernel-mode to user-mode execution (e.g., on return from a system call or scheduling of a thread onto the CPU), the kernel checks whether there is a pending unblocked signal for which the process has established a signal handler. If there is such a pending signal, the following steps occur:

1.

The kernel performs the necessary preparatory steps for execution of the signal handler:

a)

The signal is removed from the set of pending signals.

b)

If the signal handler was installed by a call to sigaction(2) that specified the SA_ONSTACK flag and the thread has defined an alternate signal stack (using sigaltstack(2)), then that stack is installed.

c)

Various pieces of signal-related context are saved into a special frame that is created on the stack. The saved information includes:

+

the program counter register (i.e., the address of the next instruction in the main program that should be executed when the signal handler returns);

+

architecture-specific register state required for resuming the interrupted program;

+

the thread’s current signal mask;

+

the thread’s alternate signal stack settings.

(If the signal handler was installed using the sigaction(2) SA_SIGINFO flag, then the above information is accessible via the ucontext_t object that is pointed to by the third argument of the signal handler.)

d)

Any signals specified in act->sa_mask when registering the handler with sigprocmask(2) are added to the thread’s signal mask. The signal being delivered is also added to the signal mask, unless SA_NODEFER was specified when registering the handler. These signals are thus blocked while the handler executes.

2.

The kernel constructs a frame for the signal handler on the stack. The kernel sets the program counter for the thread to point to the first instruction of the signal handler function, and configures the return address for that function to point to a piece of user-space code known as the signal trampoline (described in sigreturn(2)).

3.

The kernel passes control back to user-space, where execution commences at the start of the signal handler function.

4.

When the signal handler returns, control passes to the signal trampoline code.

5.

The signal trampoline calls sigreturn(2), a system call that uses the information in the stack frame created in step 1 to restore the thread to its state before the signal handler was called. The thread’s signal mask and alternate signal stack settings are restored as part of this procedure. Upon completion of the call to sigreturn(2), the kernel transfers control back to user space, and the thread recommences execution at the point where it was interrupted by the signal handler.

Note that if the signal handler does not return (e.g., control is transferred out of the handler using siglongjmp(3), or the handler executes a new program with execve(2)), then the final step is not performed. In particular, in such scenarios it is the programmer’s responsibility to restore the state of the signal mask (using sigprocmask(2)), if it is desired to unblock the signals that were blocked on entry to the signal handler. (Note that siglongjmp(3) may or may not restore the signal mask, depending on the savesigs value that was specified in the corresponding call to sigsetjmp(3).)

From the kernel’s point of view, execution of the signal handler code is exactly the same as the execution of any other user-space code. That is to say, the kernel does not record any special state information indicating that the thread is currently excuting inside a signal handler. All necessary state information is maintained in user-space registers and the user-space stack. The depth to which nested signal handlers may be invoked is thus limited only by the user-space stack (and sensible software design!).

標準シグナル
Linux supports the standard signals listed below. The second column of the table indicates which standard (if any) specified the signal: "P1990" indicates that the signal is described in the original POSIX.1-1990 standard; "P2001" indicates that the signal was added in SUSv2 and POSIX.1-2001.

img

シグナル SIGKILLSIGSTOP はキャッチ、ブロック、無視できない。

Linux 2.2 以前では、 SIGSYS, SIGXCPU, SIGXFSZ および SPARC と MIPS 以外のアーキテクチャーでの SIGBUS のデフォルトの振る舞いは (コアダンプ出力なしの) プロセス終了であった。 (他の UNIX システムにも SIGXCPUSIGXFSZ のデフォルトの動作がコアダンプなしのプロセス終了のものがある。) Linux 2.4 では、POSIX.1-2001 での要求仕様に準拠して、 これらのシグナルで、プロセスを終了させ、コアダンプを出力する ようになっている。

SIGEMT は POSIX.1-2001 に規定されていないが、 その他の多くの UNIX システムに存在する。 デフォルトの動作は多くの場合、コアダンプ出力を伴うプロセスの終了である。

SIGPWR は (POSIX.1-2001 に規定されていないが) このシグナルが存在する 他の UNIX システムでは多くの場合、デフォルト動作は無視である。

SIGIO は (POSIX.1-2001 に規定されていないが) いくつかの他の UNIX システムでは デフォルト動作は無視である。

Queueing and delivery semantics for standard signals
If multiple standard signals are pending for a process, the order in which the signals are delivered is unspecified.

Standard signals do not queue. If multiple instances of a standard signal are generated while that signal is blocked, then only one instance of the signal is marked as pending (and the signal will be delivered just once when it is unblocked). In the case where a standard signal is already pending, the siginfo_t structure (see sigaction(2)) associated with that signal is not overwritten on arrival of subsequent instances of the same signal. Thus, the process will receive the information associated with the first instance of the signal.

Signal numbering for standard signals
The numeric value for each signal is given in the table below. As shown in the table, many signals have different numeric values on different architectures. The first numeric value in each table row shows the signal number on x86, ARM, and most other architectures; the second value is for Alpha and SPARC; the third is for MIPS; and the last is for PARISC. A dash (-) denotes that a signal is absent on the corresponding architecture.

img

Note the following:

*

Where defined, SIGUNUSED is synonymous with SIGSYS. Since glibc 2.26, SIGUNUSED is no longer defined on any architecture.

*

Signal 29 is SIGINFO/SIGPWR (synonyms for the same value) on Alpha but SIGLOST on SPARC.

リアルタイムシグナル
Starting with version 2.2, Linux supports real-time signals as originally defined in the POSIX.1b real-time extensions (and now included in POSIX.1-2001). The range of supported real-time signals is defined by the macros SIGRTMIN and SIGRTMAX. POSIX.1-2001 requires that an implementation support at least _POSIX_RTSIG_MAX (8) real-time signals.

The Linux kernel supports a range of 33 different real-time signals, numbered 32 to 64. However, the glibc POSIX threads implementation internally uses two (for NPTL) or three (for LinuxThreads) real-time signals (see pthreads(7)), and adjusts the value of SIGRTMIN suitably (to 34 or 35). Because the range of available real-time signals varies according to the glibc threading implementation (and this variation can occur at run time according to the available kernel and glibc), and indeed the range of real-time signals varies across UNIX systems, programs should never refer to real-time signals using hard-coded numbers, but instead should always refer to real-time signals using the notation SIGRTMIN+n, and include suitable (run-time) checks that SIGRTMIN+n does not exceed SIGRTMAX.

標準シグナルと異なり、リアルタイムシグナルには 事前に定義された意味はない。 リアルタイムシグナルの全部をアプリケーションで定義した用途に使える。

ハンドリングしないリアルタイムシグナルのデフォルトの動作は 受信したプロセスの終了である。

リアルタイムシグナルは以下の特徴がある:

1.

リアルタイムシグナルは複数の実体をキューに入れることができる。 一方、標準シグナルの場合、そのシグナルがブロックされている間に 同じシグナルの複数のインスタンスが配送されても、 1 つだけがキューに入れられる。

2.

シグナルが sigqueue(3) を用いて送信された場合、 付属データ (整数かポインター) をシグナルと共に送信できる。 受信側プロセスが sigaction(2)SA_SIGINFO フラグを指定してシグナルハンドラーを設定した場合、 このデータは siginfo_t 構造体の si_value フィールド経由でハンドラーの第 2 引数として渡され、 利用することができる。 さらに、この構造体の si_pidsi_uid フィールドでシグナルを送信したプロセスの PID と実ユーザー ID を 得ることができる。

3.

リアルタイムシグナルでは配送される順序が保証される。 同じタイプのリアルタイムシグナルは送信された順番に到着する。 異なるリアルタイムシグナルが一つのプロセスに送信された場合、 番号の小さいシグナルから先に到着する。 (つまり小さい番号のシグナルが高い優先順位を持つ。) 対照的に、一つのプロセスに対して複数の標準シグナルが処理待ちとなった場合、 これらのシグナルが配送される順序は不定である。

一つのプロセスに対して標準シグナルとリアルタイムシグナルの両方が 処理待ちの場合、POSIX はどちらが先に配送されるかを規定していない。 Linux では、他の多くの実装と同様、このような場合には 標準シグナルが優先される。

POSIX によれば、1 プロセス毎に最低 _POSIX_SIGQUEUE_MAX (32) 個のリアルタイムシグナルをキューに入れられるべきとしている。 しかし、 Linux では違った実装になっている。カーネル 2.6.7 までは (2.6.7 を含む)、全プロセスでキューに入っているリアルタイムシグナル の数の合計についてシステム全体での制限がある。 この制限は /proc/sys/kernel/rtsig-max ファイルで見ることができ、 (権限があれば) 変更もできる。 関係するファイルとして、 /proc/sys/kernel/rtsig-nr を見ることで、いくつのリアルタイムシグナルが現在キューに入っているかを 知ることができる。 Linux 2.6.8 で、これらの /proc 経由のインターフェースは、 RLIMIT_SIGPENDING リソース制限に置き換えられた。 これは、キューに入るシグナル数に関してユーザー単位に 上限を指定するものである。 詳しくは setrlimit(2) を参照。

The addition of real-time signals required the widening of the signal set structure (sigset_t) from 32 to 64 bits. Consequently, various system calls were superseded by new system calls that supported the larger signal sets. The old and new system calls are as follows:

img

シグナルハンドラーによるシステムコールやライブラリ関数への割り込み
システムコールやライブラリが停止 (block) している間にシグナルハンドラーが 起動されると、以下のどちらかとなる。

*

シグナルが返った後、呼び出しは自動的に再スタートされる。

*

呼び出しはエラー EINTR で失敗する。

これらの二つの挙動のうちどちらが起こるかは、インターフェイスにより依存し、 シグナルハンドラーが SA_RESTART フラグ (sigaction(2) 参照) を使って設定されていたかにも依存する。 詳細は UNIX システムによって異なる。 Linux における詳細を以下で説明する。

If a blocked call to one of the following interfaces is interrupted by a signal handler, then the call is automatically restarted after the signal handler returns if the SA_RESTART flag was used; otherwise the call fails with the error EINTR:

*

read(2), readv(2), write(2), writev(2), and ioctl(2) calls on "slow" devices. A "slow" device is one where the I/O call may block for an indefinite time, for example, a terminal, pipe, or socket. If an I/O call on a slow device has already transferred some data by the time it is interrupted by a signal handler, then the call will return a success status (normally, the number of bytes transferred). Note that a (local) disk is not a slow device according to this definition; I/O operations on disk devices are not interrupted by signals.

*

停止 (block) する可能性のある open(2) (例えば、FIFO のオープン時; fifo(7) 参照)。

*

wait(2), wait3(2), wait4(2), waitid(2), waitpid(2).

*

ソケットインターフェイス: accept(2), connect(2), recv(2), recvfrom(2), recvmmsg(2), recvmsg(2), send(2), sendto(2), sendmsg(2). 但し、ソケットにタイムアウトが設定されていない場合 (下記参照)。

*

ファイルロック用インターフェイス: flock(2), fcntl(2)F_SETLKWF_OFD_SETLKW 操作。

*

POSIX メッセージキューインターフェイス: mq_receive(3), mq_timedreceive(3), mq_send(3), mq_timedsend(3).

*

futex(2) FUTEX_WAIT (Linux 2.6.22 以降; それ以前は常に EINTR で失敗していた)。

*

getrandom(2).

*

pthread_mutex_lock(3), pthread_cond_wait(3) と関連 API。

*

futex(2) FUTEX_WAIT_BITSET.

*

POSIX セマフォインターフェイス: sem_wait(3), sem_timedwait(3) (Linux 2.6.22 以降; それ以前は常に EINTR で失敗していた)。

*

read(2) from an inotify(7) file descriptor (since Linux 3.8; beforehand, always failed with EINTR).

以下のインターフェイスは、 SA_RESTART を使っているどうかに関わらず、シグナルハンドラーにより割り込まれた後、 再スタートすることは決してない。 これらは、シグナルハンドラーにより割り込まれると、常にエラー EINTR で失敗する。

*

setsockopt(2) を使ってタイムアウト (SO_RCVTIMEO) が設定されている「入力」ソケットインターフェース: accept(2), recv(2), recvfrom(2), recvmmsg(2) (NULL 以外の timeout 引数も指定されている場合), recvmsg(2)

*

"Output" socket interfaces, when a timeout (SO_RCVTIMEO) has been set on the socket using setsockopt(2): connect(2), send(2), sendto(2), and sendmsg(2).

*

シグナル待ちに使われるインターフェイス: pause(2), sigsuspend(2), sigtimedwait(2), sigwaitinfo(2).

*

ファイルディスクリプター多重インターフェイス: epoll_wait(2), epoll_pwait(2), poll(2), ppoll(2), select(2), pselect(2).

*

System V IPC インターフェイス: msgrcv(2), msgsnd(2), semop(2), semtimedop(2).

*

スリープ用のインターフェイス: clock_nanosleep(2), nanosleep(2), usleep(3).

*

io_getevents(2).

sleep(3) 関数も、ハンドラーにより割り込まれた場合、決して再スタートされることはない。 しかし、成功となり、残っている停止時間を返す。

一時停止シグナルによるシステムコールやライブラリ関数への割り込み
Linux では、シグナルハンドラーが設定されていない場合でも、 いくつかのブロッキング型のインターフェイスは、 プロセスが一時停止 (stop) シグナルの一つにより停止され、 SIGCONT により再開された後に、エラー EINTR で失敗する可能性がある。 この挙動は POSIX.1 で認められておらず、他のシステムでは起こらない。

この挙動を示す Linux のインターフェイスは以下の通りである。

*

setsockopt(2) を使ってタイムアウト (SO_RCVTIMEO) が設定されている「入力」ソケットインターフェース: accept(2), recv(2), recvfrom(2), recvmmsg(2) (NULL 以外の timeout 引数も指定されている場合), recvmsg(2)

*

"Output" socket interfaces, when a timeout (SO_RCVTIMEO) has been set on the socket using setsockopt(2): connect(2), send(2), sendto(2), and sendmsg(2), if a send timeout (SO_SNDTIMEO) has been set.

*

epoll_wait(2), epoll_pwait(2).

*

semop(2), semtimedop(2).

*

sigtimedwait(2), sigwaitinfo(2).

*

Linux 3.7 and earlier: read(2) from an inotify(7) file descriptor

*

Linux 2.6.21 以前: futex(2) FUTEX_WAIT, sem_timedwait(3), sem_wait(3).

*

Linux 2.6.8 以前: msgrcv(2), msgsnd(2).

*

Linux 2.4 以前: nanosleep(2).

準拠

POSIX.1 (注記した内容以外)。

注意

For a discussion of async-signal-safe functions, see signal-safety(7).

The /proc/[pid]/task/[tid]/status file contains various fields that show the signals that a thread is blocking (SigBlk), catching (SigCgt), or ignoring (SigIgn). (The set of signals that are caught or ignored will be the same across all threads in a process.) Other fields show the set of pending signals that are directed to the thread (SigPnd) as well as the set of pending signals that are directed to the process as a whole (ShdPnd). The corresponding fields in /proc/[pid]/status show the information for the main thread. See proc(5) for further details.

バグ

There are six signals that can be delivered as a consequence of a hardware exception: SIGBUS, SIGEMT, SIGFPE, SIGILL, SIGSEGV, and SIGTRAP. Which of these signals is delivered, for any given hardware exception, is not documented and does not always make sense.

For example, an invalid memory access that causes delivery of SIGSEGV on one CPU architecture may cause delivery of SIGBUS on another architecture, or vice versa.

For another example, using the x86 int instruction with a forbidden argument (any number other than 3 or 128) causes delivery of SIGSEGV, even though SIGILL would make more sense, because of how the CPU reports the forbidden operation to the kernel.

関連項目

kill(1), clone(2), getrlimit(2), kill(2), pidfd_send_signal(2), restart_syscall(2), rt_sigqueueinfo(2), setitimer(2), setrlimit(2), sgetmask(2), sigaction(2), sigaltstack(2), signal(2), signalfd(2), sigpending(2), sigprocmask(2), sigreturn(2), sigsuspend(2), sigwaitinfo(2), abort(3), bsd_signal(3), killpg(3), longjmp(3), pthread_sigqueue(3), raise(3), sigqueue(3), sigset(3), sigsetops(3), sigvec(3), sigwait(3), strsignal(3), swapcontext(3), sysv_signal(3), core(5), proc(5), nptl(7), pthreads(7), sigevent(7)

この文書について

この man ページは Linux man-pages プロジェクトのリリース 5.10 の一部である。プロジェクトの説明とバグ報告に関する情報は https://www.kernel.org/doc/man-pages/ に書かれている。

pdf