1 .\" Copyright (C) 2008 Michael Kerrisk <mtk.manpages@gmail.com>
2 .\" starting from a version by Davide Libenzi <davidel@xmailserver.org>
4 .\" This program is free software; you can redistribute it and/or modify
5 .\" it under the terms of the GNU General Public License as published by
6 .\" the Free Software Foundation; either version 2 of the License, or
7 .\" (at your option) any later version.
9 .\" This program is distributed in the hope that it will be useful,
10 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
11 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 .\" GNU General Public License for more details.
14 .\" You should have received a copy of the GNU General Public License
15 .\" along with this program; if not, write to the Free Software
16 .\" Foundation, Inc., 59 Temple Place, Suite 330, Boston,
19 .\" Japanese Version Copyright (c) 2008 Akihiro MOTOKI
20 .\" all rights reserved.
21 .\" Translated 2008-04-06, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v2.79
22 .\" Updated 2008-11-10, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v3.13
23 .\" Updated 2009-02-23, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v3.18
25 .TH SIGNALFD 2 2009-01-13 Linux "Linux Programmer's Manual"
28 .\"O signalfd \- create a file descriptor for accepting signals
29 signalfd \- シグナル受け付け用のファイルディスクリプタを生成する
32 .B #include <sys/signalfd.h>
34 .BI "int signalfd(int " fd ", const sigset_t *" mask ", int " flags );
38 .\"O creates a file descriptor that can be used to accept signals
39 .\"O targeted at the caller.
40 .\"O This provides an alternative to the use of a signal handler or
41 .\"O .BR sigwaitinfo (2),
42 .\"O and has the advantage that the file descriptor may be monitored by
48 は、呼び出し元宛てのシグナルを受け付けるために使用されるファイル
52 を用いる方法の代わりとなるものであり、このファイルディスクリプタを
60 .\"O argument specifies the set of signals that the caller
61 .\"O wishes to accept via the file descriptor.
62 .\"O This argument is a signal set whose contents can be initialized
63 .\"O using the macros described in
64 .\"O .BR sigsetops (3).
66 引き数には、呼び出し元がこのファイルディスクリプタ経由で受け付けたい
67 シグナル集合を指定する。この引き数で指定するシグナル集合の内容は、
69 で説明されているマクロを使って初期化することができる。
70 .\"O Normally, the set of signals to be received via the
71 .\"O file descriptor should be blocked using
72 .\"O .BR sigprocmask (2),
73 .\"O to prevent the signals being handled according to their default
75 通常、ファイルディスクリプタ経由で受信するシグナル集合は、
76 そのシグナルがデフォルトの配送方法に基いて処理されるのを防ぐために、
79 .\"O It is not possible to receive
83 .\"O signals via a signalfd file descriptor;
84 .\"O these signals are silently ignored if specified in
90 を signalfd ファイルディスクリプタ経由で受信することはできない。
98 .\"O then the call creates a new file descriptor and associates the
99 .\"O signal set specified in
101 .\"O with that descriptor.
105 .\"O then it must specify a valid existing signalfd file descriptor, and
107 .\"O is used to replace the signal set associated with that descriptor.
113 で指定されたシグナル集合をそのファイルディスクリプタに関連付ける。
117 には有効な既存の signalfd ファイルディスクリプタを指定しなければならず、
118 そのディスクリプタに関連付けられているシグナル集合は
122 .\"O Starting with Linux 2.6.27, the following values may be bitwise ORed in
124 .\"O to change the behaviour of
125 .\"O .BR signalfd ():
127 以下の値のいくつかをビット単位の論理和 (OR) で指定することで、
134 .\"O file status flag on the new open file description.
135 .\"O Using this flag saves extra calls to
137 .\"O to achieve the same result.
138 新しく生成されるオープンファイル記述 (open file description) の
148 .\"O Set the close-on-exec
149 .\"O .RB ( FD_CLOEXEC )
150 .\"O flag on the new file descriptor.
151 .\"O See the description of the
155 .\"O for reasons why this may be useful.
166 .\"O In Linux up to version 2.6.26, the
168 .\"O argument is unused, and must be specified as zero.
169 バージョン 2.6.26 以前の Linux では、
171 引き数は未使用であり、0 を指定しなければならない。
174 .\"O returns a file descriptor that supports the following operations:
176 が返すファイルディスクリプタは以下の操作をサポートしている。
179 .\"O If one or more of the signals specified in
181 .\"O is pending for the process, then the buffer supplied to
183 .\"O is used to return one or more
184 .\"O .I signalfd_siginfo
185 .\"O structures (see below) that describe the signals.
188 .\"O returns information for as many signals as are pending and will
189 .\"O fit in the supplied buffer.
190 .\"O The buffer must be at least
191 .\"O .I "sizeof(struct signalfd_siginfo)"
193 .\"O The return value of the
195 .\"O is the total number of bytes read.
197 に指定されているシグナルのうち一つ以上がそのプロセスに対して
198 処理待ち (pending) であれば、それらのシグナルの情報が
204 は、バッファに格納可能な範囲でできるだけ多くの処理待ちのシグナルに
207 .I "sizeof(struct signalfd_siginfo)"
210 の返り値は読み出されたトータルのバイト数である。
212 .\"O As a consequence of the
214 .\"O the signals are consumed,
215 .\"O so that they are no longer pending for the process
216 .\"O (i.e., will not be caught by signal handlers,
217 .\"O and cannot be accepted using
218 .\"O .BR sigwaitinfo (2)).
221 これらのシグナルはそのプロセスに対しては処理待ちではなくなる
222 (つまり、シグナルハンドラで捕捉されることもなく、
226 .\"O If none of the signals in
228 .\"O is pending for the process, then the
230 .\"O either blocks until one of the signals in
232 .\"O is generated for the process,
233 .\"O or fails with the error
235 .\"O if the file descriptor has been made nonblocking.
237 に指定されているシグナルがそのプロセスに対して一つも処理待ちでなければ、
241 で指定されたシグナルのうちいずれか一つがそのプロセスに対して発生するまで
242 停止 (block) する、もしくはファイルディスクリプタが非停止 (nonblocking)
247 .\"O .BR poll "(2), " select "(2) (and similar)"
248 .BR poll "(2), " select "(2) (と同様の操作)"
249 .\"O The file descriptor is readable
257 .\"O if one or more of the signals in
259 .\"O is pending for the process.
261 に指定されたシグナルのうち一つ以上がそのプロセスに対して処理待ちであれば、
262 ファイルディスクリプタは読み出し可能となる
272 .\"O The signalfd file descriptor also supports the other file-descriptor
273 .\"O multiplexing APIs:
274 .\"O .BR pselect (2),
278 signalfd ファイルディスクリプタは、これ以外のファイルディスクリプタ
286 .\"O When the file descriptor is no longer required it should be closed.
287 .\"O When all file descriptors associated with the same signalfd object
288 .\"O have been closed, the resources for object are freed by the kernel.
289 ファイルディスクリプタがそれ以降は必要なくなった際には、クローズすべきである。
290 同じ signalfd オブジェクトに関連付けられたファイルディスクリプタが全て
291 クローズされると、そのオブジェクト用の資源がカーネルにより解放される。
292 .\"O .SS The signalfd_siginfo structure
293 .SS signalfd_siginfo 構造体
294 .\"O The format of the
295 .\"O .I signalfd_siginfo
296 .\"O structure(s) returned by
298 .\"O from a signalfd file descriptor is as follows:
299 signalfd ファイルディスクリプタからの
307 struct signalfd_siginfo {
308 .\"O uint32_t ssi_signo; /* Signal number */
309 .\"O int32_t ssi_errno; /* Error number (unused) */
310 .\"O int32_t ssi_code; /* Signal code */
311 .\"O uint32_t ssi_pid; /* PID of sender */
312 .\"O uint32_t ssi_uid; /* Real UID of sender */
313 .\"O int32_t ssi_fd; /* File descriptor (SIGIO) */
314 .\"O uint32_t ssi_tid; /* Kernel timer ID (POSIX timers)
315 .\"O uint32_t ssi_band; /* Band event (SIGIO) */
316 .\"O uint32_t ssi_overrun; /* POSIX timer overrun count */
317 .\"O uint32_t ssi_trapno; /* Trap number that caused signal */
318 .\"O .\" ssi_trapno is unused on most arches
319 .\"O int32_t ssi_status; /* Exit status or signal (SIGCHLD) */
320 .\"O int32_t ssi_int; /* Integer sent by sigqueue(3) */
321 .\"O uint64_t ssi_ptr; /* Pointer sent by sigqueue(3) */
322 .\"O uint64_t ssi_utime; /* User CPU time consumed (SIGCHLD) */
323 .\"O uint64_t ssi_stime; /* System CPU time consumed (SIGCHLD) */
324 .\"O uint64_t ssi_addr; /* Address that generated signal
325 .\"O (for hardware-generated signals) */
326 .\"O uint8_t pad[\fIX\fP]; /* Pad size to 128 bytes (allow for
327 .\"O additional fields in the future) */
328 struct signalfd_siginfo {
329 uint32_t ssi_signo; /* シグナル番号 */
330 int32_t ssi_errno; /* エラー番号 (未使用) */
331 int32_t ssi_code; /* シグナルコード */
332 uint32_t ssi_pid; /* 送信元の PID */
333 uint32_t ssi_uid; /* 送信元の実 UID */
334 int32_t ssi_fd; /* ファイルディスクリプタ (SIGIO) */
335 uint32_t ssi_tid; /* カーネルタイマ ID (POSIX タイマ)
336 uint32_t ssi_band; /* Band イベント (SIGIO) */
337 uint32_t ssi_overrun; /* POSIX タイマのオーバーラン回数 */
338 uint32_t ssi_trapno; /* シグナルの原因となったトラップ番号 */
339 .\" ほとんどのアーキテクチャで ssi_trapno は未使用
340 int32_t ssi_status; /* 終了ステータスかシグナル (SIGCHLD) */
341 int32_t ssi_int; /* sigqueue(3) から送られた整数 */
342 uint64_t ssi_ptr; /* sigqueue(3) から送られたポインタ */
343 uint64_t ssi_utime; /* 消費したユーザ CPU 時間 (SIGCHLD) */
344 uint64_t ssi_stime; /* 消費したシステム CPU 時間 (SIGCHLD) */
345 uint64_t ssi_addr; /* シグナルを生成したアドレス
346 (ハードウェアが生成したシグナルの場合) */
347 uint8_t pad[\fIX\fP]; /* pad の大きさは 128 バイト
348 (将来のフィールド追加用の場所の確保) */
353 .\"O Each of the fields in this structure
354 .\"O is analogous to the similarly named field in the
359 .\"O structure is described in
360 .\"O .BR sigaction (2).
364 構造体の同じような名前のフィールドと同様である。
369 .\"O Not all fields in the returned
370 .\"O .I signalfd_siginfo
371 .\"O structure will be valid for a specific signal;
372 .\"O the set of valid fields can be determined from the value returned in the
375 .\"O This field is the analog of the
379 .\"O .BR sigaction (2)
383 構造体の全てのフィールドがあるシグナルに対して有効なわけではない。
386 フィールドで返される値から判定することができる。
394 .\"O .SS fork(2) semantics
398 .\"O the child inherits a copy of the signalfd file descriptor.
401 .\"O from the file descriptor in the child will return information
402 .\"O about signals queued to the child.
404 が行われると、子プロセスは signalfd ファイルディスクリプタのコピーを
406 子プロセスでこのファイルディスクリプタから
408 を行うと、子プロセスに対するキューに入っているシグナルに関する
410 .\"O .SS execve(2) semantics
412 .\"O Just like any other file descriptor,
413 .\"O a signalfd file descriptor remains open across an
415 .\"O unless it has been marked for close-on-exec (see
417 .\"O Any signals that were available for reading before the
419 .\"O remain available to the newly loaded program.
420 .\"O (This is analogous to traditional signal semantics,
421 .\"O where a blocked signal that is pending remains pending across an
422 .\"O .BR execve (2).)
424 signalfd ファイルディスクリプタも
426 の前後でオープンされたままとなる。但し、そのファイルディスクリプタに
429 参照) が付いている場合はクローズされる。
431 の前に読み出し可能となっていた全てのシグナルは新しく起動されたプログラム
433 (これは伝統的なシグナルの扱いと同じであり、
437 .\"O .SS Thread semantics
439 .\"O The semantics of signalfd file descriptors in a multithreaded program
440 .\"O mirror the standard semantics for signals.
442 .\"O when a thread reads from a signalfd file descriptor,
443 .\"O it will read the signals that are directed to the thread
444 .\"O itself and the signals that are directed to the process
445 .\"O (i.e., the entire thread group).
446 .\"O (A thread will not be able to read signals that are directed
447 .\"O to other threads in the process.)
448 マルチスレッドプログラムにおける signalfd ファイルディスクリプタの扱いは
450 言い換えると、あるスレッドが signalfd ファイルディスクリプタから
451 読み出しを行うと、そのスレッド自身宛てのシグナルとプロセス (すなわち
452 スレッドグループ全体) 宛てのシグナルが読み出される。
453 (スレッドは同じプロセスの他のスレッド宛てのシグナルを読み出すことはできない。)
454 .\"O .SH "RETURN VALUE"
458 .\"O returns a signalfd file descriptor;
459 .\"O this is either a new file descriptor (if
465 .\"O was a valid signalfd file descriptor.
466 .\"O On error, \-1 is returned and
468 .\"O is set to indicate the error.
471 は signalfd ファイルディスクリプタを返す。
474 が \-1 の場合は新規のファイルディスクリプタであり、
476 が有効な signalfd ファイルディスクリプタだった場合は
488 .\"O file descriptor is not a valid file descriptor.
495 .\"O is not a valid signalfd file descriptor.
497 が有効な signalfd ファイルディスクリプタではない。
500 .\" argument is not equal to
501 .\" .IR sizeof(sigset_t) ;
506 .\"O or, in Linux 2.6.26 or earlier,
516 .\"O The per-process limit of open file descriptors has been reached.
517 オープン済みのファイルディスクリプタの数がプロセスあたりの上限に
521 .\"O The system-wide limit on the total number of open files has been
523 オープン済みのファイル総数がシステム全体の上限に達していた。
526 .\"O Could not mount (internal) anonymous inode device.
527 (カーネル内の) 無名 inode デバイスをマウントできなかった。
530 .\"O There was insufficient memory to create a new signalfd file descriptor.
531 新しい signalfd ファイルディスクリプタを生成するのに十分なメモリがなかった。
535 .\"O is available on Linux since kernel 2.6.22.
536 .\"O Working support is provided in glibc since version 2.8.
538 はカーネル 2.6.22 以降の Linux で利用可能である。
539 正しく動作する glibc 側のサポートはバージョン 2.8 以降で提供されている。
540 .\" signalfd() is in glibc 2.7, but reportedly does not build
542 .\"O .BR signalfd4 ()
543 .\"O system call (see NOTES) is available on Linux since kernel 2.6.27.
546 カーネル 2.6.27 以降の Linux で利用可能である。
547 .\"O .SH CONFORMING TO
551 .\"O .BR signalfd4 ()
552 .\"O are Linux-specific.
559 .\"O The underlying Linux system call requires an additional argument,
560 .\"O .IR "size_t sizemask" ,
561 .\"O which specifies the size of the
566 .\"O wrapper function does not include this argument,
567 .\"O since it provides the required value for the underlying system call.
570 という引き数が追加で必要である。この引き数で
576 ラッパー関数が必要な値を計算して内部で呼び出すシステムコールに提供する。
578 .\"O A process can create multiple signalfd file descriptors.
579 .\"O This makes it possible to accept different signals
580 .\"O on different file descriptors.
581 .\"O (This may be useful if monitoring the file descriptors using
586 .\"O the arrival of different signals will make different descriptors ready.)
587 .\"O If a signal appears in the
589 .\"O of more than one of the file descriptors, then occurrences
590 .\"O of that signal can be read (once) from any one of the descriptors.
591 一つのプロセスは複数の signalfd ファイルディスクリプタを生成することができる。
592 これにより、異なるファイルディスクリプタで異なるシグナルを受け取ることが
597 を使ってファイルディスクリプタを監視する場合に有用かもしれない。
598 異なるシグナルが到着すると、異なるファイルディスクリプタが利用可能に
600 一つのシグナルが二つ以上のファイルディスクリプタの
602 に含まれている場合、そのシグナルの発生はそのシグナルを
604 に含むファイルディスクリプタのうちいずれか一つから読み出すことができる。
605 .\"O .SS Underlying Linux system calls
606 .SH 下層にある Linux のシステムコール
607 .\"O There are two underlying Linux system calls:
609 .\"O and the more recent
610 .\"O .BR signalfd4 ().
611 .\"O The former system call does not implement a
614 .\"O The latter system call implements the
616 .\"O values described above.
617 下層にある Linux システムコールは二種類あり、
630 .\"O Starting with glibc 2.9, the
632 .\"O wrapper function will use
633 .\"O .BR signalfd4 ()
634 .\"O where it is available.
642 .\"O In kernels before 2.6.25, the
646 .\"O fields are not filled in with the data accompanying a signal sent by
647 .\"O .BR sigqueue (3).
650 により送信されたシグナルと一緒に渡されるデータでは、フィールド
655 .\" The fix also was put into 2.6.24.5
658 .\"O The program below accepts the signals
662 .\"O via a signalfd file descriptor.
663 .\"O The program terminates after accepting a
666 .\"O The following shell session demonstrates the use of the program:
671 を signalfd ファイルディスクリプタ経由で受信する。
675 以下に示すシェルセッションにこのプログラムの使い方を示す。
679 .RB "$" " ./signalfd_demo"
680 .BR "^C" " # Control\-C generates SIGINT"
684 \fB^\\\fP # Control\-\\ generates SIGQUIT
689 .\"O .SS Program source
693 #include <sys/signalfd.h>
699 #define handle_error(msg) \\
700 do { perror(msg); exit(EXIT_FAILURE); } while (0)
703 main(int argc, char *argv[])
707 struct signalfd_siginfo fdsi;
711 sigaddset(&mask, SIGINT);
712 sigaddset(&mask, SIGQUIT);
714 /* Block signals so that they aren\(aqt handled
715 according to their default dispositions */
717 if (sigprocmask(SIG_BLOCK, &mask, NULL) == \-1)
718 handle_error("sigprocmask");
720 sfd = signalfd(\-1, &mask, 0);
722 handle_error("signalfd");
725 s = read(sfd, &fdsi, sizeof(struct signalfd_siginfo));
726 if (s != sizeof(struct signalfd_siginfo))
727 handle_error("read");
729 if (fdsi.ssi_signo == SIGINT) {
730 printf("Got SIGINT\\n");
731 } else if (fdsi.ssi_signo == SIGQUIT) {
732 printf("Got SIGQUIT\\n");
735 printf("Read unexpected signal\\n");
749 .BR timerfd_create (2),