Provided by: manpages-ja-dev_0.5.0.0.20221215+dfsg-1_all bug

名前

       poll, ppoll - ファイルディスクリプターにおけるイベントを待つ

書式

       #include <poll.h>

       int poll(struct pollfd *fds, nfds_t nfds, int timeout);

       #define _GNU_SOURCE         /* feature_test_macros(7) 参照 */
       #include <signal.h>
       #include <poll.h>

       int ppoll(struct pollfd *fds, nfds_t nfds,
               const struct timespec *tmo_p, const sigset_t *sigmask);

説明

       poll()   performs  a  similar  task to select(2): it waits for one of a set of file descriptors to become
       ready to perform I/O.  The Linux-specific epoll(7)  API performs a  similar  task,  but  offers  features
       beyond those found in poll().

       監視するファイルディスクリプター集合は、 fds 引数で指定する。 fds は、以下の型の構造体の配列である。

           struct pollfd {
               int   fd;         /* file descriptor */
               short events;     /* requested events */
               short revents;    /* returned events */
           };

       nfds には、 fds 配列の要素数を指定する。

       fd  フィールドには、オープンされたファイルのファイルディスクリプターが入る。 このフィールドが負の場合、対
       応する events フィールドは無視され、 revents には 0 が返される。(この機能により、一つの poll() の呼び出し
       で簡単にあるファイルディスクリプターを無視することができる。  単に  fd フィールドの符号を反転するだけでよ
       い。 ただし、この方法はファイルディスクリプター 0 を無視するのには使用できない点に注意すること。)

       構造体の events 要素は入力パラメーターで、 ファイルディスクリプター fd に関して、  アプリケーションが興味
       を持っているイベントのビットマスクを指定する。   このフィールドには   0  を指定することもでき、  その場合
       revents で返されるイベントは POLLHUP, POLLERR, POLLNVAL だけである (下記参照)。

       revents 要素は出力パラメーターで、実際に起こったイベントがカーネルにより設定される。  revents  で返される
       ビット列には、  events で指定したもののどれか、もしくは POLLERR, POLLHUP, POLLNVAL のうちの一つが含まれる
       (POLLERR, POLLHUP,  POLLNVAL  の  3つのビットは  events  に指定しても意味がなく、対応した状態が真の場合に
       revents に設定される)。

       どのファイルディスクリプターにも要求したイベントが発生しておらず、 エラーも起こらない場合、 poll() はイベ
       ントのうちいずれか一つが発生するまで停止 (block) する。

       timeout 引数は、 ファイルディスクリプターが利用可能になるまで  poll()  が停止する時間をミリ秒で指定する。
       poll() の呼び出しは以下のいずれかになるまで停止する。

       • ファイルディスクリプターが利用可能になる

       • 呼び出しがシグナルハンドラーにより割り込まれた

       • タイムアウトが満了する

       timeout 時間はシステムクロックの粒度に切り上げられ、 カーネルのスケジューリング遅延により少しだけ長くなる
       可能性がある点に注意すること。    timeout    に負の値を指定した場合、タイムアウト時間が無限大を意味する。
       timeout  を  0 に指定した場合、I/O 可能なファイルディスクリプターがない場合であっても、 poll() はすぐに返
       る。

       events に指定したり、 revents で返されるビットは <poll.h> で定義されている:

       POLLIN 読み出し可能なデータがある。

       POLLPRI
              There is some exceptional condition on the file descriptor.  Possibilities include:

              • There is out-of-band data on a TCP socket (see tcp(7)).

              • A pseudoterminal master in packet mode has seen a state change on the slave (see ioctl_tty(2)).

              • A cgroup.events file has been modified (see cgroups(7)).

       POLLOUT
              書き込みが可能になった。ただし、ソケットやパイプで利用可能な空間よりも大きなデータを書き込んだ場合
              には (O_NONBLOCK がセットされている場合以外は) やはり停止することになる。

       POLLRDHUP (Linux 2.6.17 以降)
              ストリームソケットの他端が、コネクションを  close  したか、 コネクションの書き込み側を shutdown し
              た。       この定義を有効にするには、       (「どの」ヘッダーファイルをインクルードするよりも前に)
              _GNU_SOURCE 機能検査マクロを定義しなければならない。

       POLLERR
              Error  condition  (only  returned in revents; ignored in events).  This bit is also set for a file
              descriptor referring to the write end of a pipe when the read end has been closed.

       POLLHUP
              Hang up (only returned in revents; ignored in events).  Note that when reading from a channel such
              as  a  pipe  or  a  stream socket, this event merely indicates that the peer closed its end of the
              channel.  Subsequent reads from  the  channel  will  return  0  (end  of  file)   only  after  all
              outstanding data in the channel has been consumed.

       POLLNVAL
              Invalid request: fd not open (only returned in revents; ignored in events).

       _XOPEN_SOURCE を定義してコンパイルした場合には、以下の定義も行われる。 ただし、上記のリストにあるビット以
       上の情報が得られる訳ではない。

       POLLRDNORM
              POLLIN と同じ。

       POLLRDBAND
              優先帯域データ (priority band data) が読み出し可能である (普通は Linux では使用されない)。

       POLLWRNORM
              POLLOUT と同じ。

       POLLWRBAND
              優先帯域データ (priority data) が書き込み可能である。

       Linux では POLLMSG も定義されているが、使用されていない。

   ppoll()
       poll()  と ppoll()  の関係は select(2)  と pselect(2)  の関係と同じようなものである:  pselect(2)   と同様
       に、 ppoll()  を使うと、アプリケーションはファイルディスクリプターの状態変化 もしくはシグナルの捕捉を安全
       に待つことができる。

       timeout 引数の精度の違いを除くと、以下の ppoll()  の呼び出しは、

           ready = ppoll(&fds, nfds, tmo_p, &sigmask);

       次の呼び出しを atomic に実行するのとほぼ等価である。

           sigset_t origmask;
           int timeout;

           timeout = (tmo_p == NULL) ? -1 :
                     (tmo_p->tv_sec * 1000 + tmo_p->tv_nsec / 1000000);
           pthread_sigmask(SIG_SETMASK, &sigmask, &origmask);
           ready = poll(&fds, nfds, timeout);
           pthread_sigmask(SIG_SETMASK, &origmask, NULL);

       The above code segment is described as nearly equivalent because whereas a  negative  timeout  value  for
       poll()   is  interpreted as an infinite timeout, a negative value expressed in *tmo_p results in an error
       from ppoll().

       なぜ ppoll()  が必要なのかについての説明は pselect(2)  の説明を参照のこと。

       sigmask 引数に NULL が指定された場合、シグナルマスクの操作は行われない (したがって、 ppoll()   の  poll()
       との違いは timeout 引数の精度だけとなる)。

       tmo_p  引数は  ppoll()  が停止する時間の上限を指定するものである。 この引数には以下の型の構造体へのポイン
       ターを指定する。

           struct timespec {
               long    tv_sec;         /* seconds */
               long    tv_nsec;        /* nanoseconds */
           };

       tmo_p に NULL が指定された場合、 ppoll は無限に停止することがあり得る。

返り値

       On success, poll()  returns a nonnegative value which is the number of  elements  in  the  pollfds  whose
       revents  fields  have  been  set to a nonzero value (indicating an event or an error).  A return value of
       zero indicates that the system call timed out before any file descriptors became read.

       On error, -1 is returned, and errno is set to indicate the cause of the error.

エラー

       EFAULT fds points outside the process's accessible address space.  The array given as  argument  was  not
              contained in the calling program's address space.

       EINTR  要求されたイベントのどれかが起こる前にシグナルが発生した。 signal(7)  参照。

       EINVAL nfds の値が RLIMIT_NOFILE を超えた。

       EINVAL (ppoll())  The timeout value expressed in *ip is invalid (negative).

       ENOMEM Unable to allocate memory for kernel data structures.

バージョン

       poll() システムコールは Linux 2.1.23 で導入された。このシステムコールが存在しない古いカーネルでは、 glibc
       は select(2) を使用して poll() ラッパー関数のエミュレーションを行う。

       ppoll()  システムコールは カーネル 2.6.16 で Linux に追加された。 ppoll()  ライブラリコールは  glibc  2.4
       に追加された。

準拠

       poll() は POSIX.1-2001 と POSIX.1-2008 に準拠している。 ppoll()  は Linux 固有である。

注意

       The operation of poll()  and ppoll()  is not affected by the O_NONBLOCK flag.

       On  some  other  UNIX  systems,  poll()   can  fail with the error EAGAIN if the system fails to allocate
       kernel-internal resources, rather than ENOMEM as Linux does.   POSIX  permits  this  behavior.   Portable
       programs may wish to check for EAGAIN and loop, just as with EINTR.

       いくつかの実装では、値  -1 を持った非標準の定数 INFTIM が定義されており、 poll()  の timeout の指定に使用
       できる。 この定数は glibc では定義されていない。

       poll() で監視中のファイルディスクリプターが別のスレッドによってクローズされた場合に何が起こるかの議論につ
       いては、 select(2) を参照してほしい。

   C ライブラリとカーネルの違い
       Linux  の ppoll()  システムコールは tmo_p 引数を変更する。 しかし、glibc のラッパー関数は、システムコール
       に渡す timeout 引数 としてローカル変数を使うことでこの動作を隠蔽している。 このため、glibc の ppoll()  関
       数では tmo_p 引数は変更されない。

       The  raw  ppoll()  system call has a fifth argument, size_t sigsetsize, which specifies the size in bytes
       of the sigmask argument.  The glibc ppoll()  wrapper function specifies this argument as  a  fixed  value
       (equal  to sizeof(kernel_sigset_t)).  See sigprocmask(2)  for a discussion on the differences between the
       kernel and the libc notion of the sigset.

バグ

       select(2)  の「バグ」の節に書かれている、誤った準備完了通知 (spurious readiness notifications) についての
       議論を参照のこと。

       The  program below opens each of the files named in its command-line arguments and monitors the resulting
       file descriptors for readiness to read (POLLIN).  The program loops, repeatedly using poll()  to  monitor
       the  file  descriptors,  printing  the  number  of ready file descriptors on return.  For each ready file
       descriptor, the program:

       • displays the returned revents field in a human-readable form;

       • if the file descriptor is readable, reads some data from it, and displays that data on standard output;
         and

       • if  the  file  descriptors was not readable, but some other event occurred (presumably POLLHUP), closes
         the file descriptor.

       Suppose we run the program in one terminal, asking it to open a FIFO:

           $ mkfifo myfifo
           $ ./poll_input myfifo

       In a second terminal window, we then open the FIFO for writing, write some data  to  it,  and  close  the
       FIFO:

           $ echo aaaaabbbbbccccc > myfifo

       In the terminal where we are running the program, we would then see:

           Opened "myfifo" on fd 3
           About to poll()
           Ready: 1
             fd=3; events: POLLIN POLLHUP
               read 10 bytes: aaaaabbbbb
           About to poll()
           Ready: 1
             fd=3; events: POLLIN POLLHUP
               read 6 bytes: ccccc

           About to poll()
           Ready: 1
             fd=3; events: POLLHUP
               closing fd 3
           All file descriptors closed; bye

       In the above output, we see that poll()  returned three times:

       • On  the  first  return,  the  bits  returned in the revents field were POLLIN, indicating that the file
         descriptor is readable, and POLLHUP, indicating that the other end of the FIFO has  been  closed.   The
         program then consumed some of the available input.

       • The second return from poll()  also indicated POLLIN and POLLHUP; the program then consumed the last of
         the available input.

       • On the final return, poll()  indicated only POLLHUP on the FIFO, at which point the file descriptor was
         closed and the program terminated.

   プログラムのソース

       /* poll_input.c

          Licensed under GNU General Public License v2 or later.
       */
       #include <poll.h>
       #include <fcntl.h>
       #include <sys/types.h>
       #include <stdio.h>
       #include <stdlib.h>
       #include <unistd.h>

       #define errExit(msg)    do { perror(msg); exit(EXIT_FAILURE); \
                               } while (0)

       int
       main(int argc, char *argv[])
       {
           int nfds, num_open_fds;
           struct pollfd *pfds;

           if (argc < 2) {
              fprintf(stderr, "Usage: %s file...\n", argv[0]);
              exit(EXIT_FAILURE);
           }

           num_open_fds = nfds = argc - 1;
           pfds = calloc(nfds, sizeof(struct pollfd));
           if (pfds == NULL)
               errExit("malloc");

           /* Open each file on command line, and add it 'pfds' array */

           for (int j = 0; j < nfds; j++) {
               pfds[j].fd = open(argv[j + 1], O_RDONLY);
               if (pfds[j].fd == -1)
                   errExit("open");

               printf("Opened \"%s\" on fd %d\n", argv[j + 1], pfds[j].fd);

               pfds[j].events = POLLIN;
           }

           /* Keep calling poll() as long as at least one file descriptor is
              open */

           while (num_open_fds > 0) {
               int ready;

               printf("About to poll()\n");
               ready = poll(pfds, nfds, -1);
               if (ready == -1)
                   errExit("poll");

               printf("Ready: %d\n", ready);

               /* Deal with array returned by poll() */

               for (int j = 0; j < nfds; j++) {
                   char buf[10];

                   if (pfds[j].revents != 0) {
                       printf("  fd=%d; events: %s%s%s\n", pfds[j].fd,
                               (pfds[j].revents & POLLIN)  ? "POLLIN "  : "",
                               (pfds[j].revents & POLLHUP) ? "POLLHUP " : "",
                               (pfds[j].revents & POLLERR) ? "POLLERR " : "");

                       if (pfds[j].revents & POLLIN) {
                           ssize_t s = read(pfds[j].fd, buf, sizeof(buf));
                           if (s == -1)
                               errExit("read");
                           printf("    read %zd bytes: %.*s\n",
                                   s, (int) s, buf);
                       } else {                /* POLLERR | POLLHUP */
                           printf("    closing fd %d\n", pfds[j].fd);
                           if (close(pfds[j].fd) == -1)
                               errExit("close");
                           num_open_fds--;
                       }
                   }
               }
           }

           printf("All file descriptors closed; bye\n");
           exit(EXIT_SUCCESS);
       }

関連項目

       restart_syscall(2), select(2), select_tut(2), epoll(7), time(7)

この文書について

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