Blame man2/poll.2

Packit 7cfc04
.\" Copyright (C) 1997 Andries Brouwer (aeb@cwi.nl)
Packit 7cfc04
.\" and Copyright (C) 2006, Michael Kerrisk <mtk.manpages@gmail.com>
Packit 7cfc04
.\"
Packit 7cfc04
.\" %%%LICENSE_START(VERBATIM)
Packit 7cfc04
.\" Permission is granted to make and distribute verbatim copies of this
Packit 7cfc04
.\" manual provided the copyright notice and this permission notice are
Packit 7cfc04
.\" preserved on all copies.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Permission is granted to copy and distribute modified versions of this
Packit 7cfc04
.\" manual under the conditions for verbatim copying, provided that the
Packit 7cfc04
.\" entire resulting derived work is distributed under the terms of a
Packit 7cfc04
.\" permission notice identical to this one.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Since the Linux kernel and libraries are constantly changing, this
Packit 7cfc04
.\" manual page may be incorrect or out-of-date.  The author(s) assume no
Packit 7cfc04
.\" responsibility for errors or omissions, or for damages resulting from
Packit 7cfc04
.\" the use of the information contained herein.  The author(s) may not
Packit 7cfc04
.\" have taken the same level of care in the production of this manual,
Packit 7cfc04
.\" which is licensed free of charge, as they might when working
Packit 7cfc04
.\" professionally.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Formatted or processed versions of this manual, if unaccompanied by
Packit 7cfc04
.\" the source, must acknowledge the copyright and authors of this work.
Packit 7cfc04
.\" %%%LICENSE_END
Packit 7cfc04
.\"
Packit 7cfc04
.\" Additions from Richard Gooch <rgooch@atnf.CSIRO.AU> and aeb, 971207
Packit 7cfc04
.\" 2006-03-13, mtk, Added ppoll() + various other rewordings
Packit 7cfc04
.\" 2006-07-01, mtk, Added POLLRDHUP + various other wording and
Packit 7cfc04
.\"	formatting changes.
Packit 7cfc04
.\"
Packit 7cfc04
.TH POLL 2 2017-09-15 "Linux" "Linux Programmer's Manual"
Packit 7cfc04
.SH NAME
Packit 7cfc04
poll, ppoll \- wait for some event on a file descriptor
Packit 7cfc04
.SH SYNOPSIS
Packit 7cfc04
.nf
Packit 7cfc04
.B #include <poll.h>
Packit 7cfc04
.PP
Packit 7cfc04
.BI "int poll(struct pollfd *" fds ", nfds_t " nfds ", int " timeout );
Packit 7cfc04
Packit 7cfc04
.BR "#define _GNU_SOURCE" "         /* See feature_test_macros(7) */"
Packit 7cfc04
.B #include <signal.h>
Packit 7cfc04
.B #include <poll.h>
Packit 7cfc04
.PP
Packit 7cfc04
.BI "int ppoll(struct pollfd *" fds ", nfds_t " nfds ", "
Packit 7cfc04
.BI "        const struct timespec *" tmo_p ", const sigset_t *" sigmask );
Packit 7cfc04
.fi
Packit 7cfc04
.SH DESCRIPTION
Packit 7cfc04
.BR poll ()
Packit 7cfc04
performs a similar task to
Packit 7cfc04
.BR select (2):
Packit 7cfc04
it waits for one of a set of file descriptors to become ready
Packit 7cfc04
to perform I/O.
Packit 7cfc04
.PP
Packit 7cfc04
The set of file descriptors to be monitored is specified in the
Packit 7cfc04
.I fds
Packit 7cfc04
argument, which is an array of structures of the following form:
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
struct pollfd {
Packit 7cfc04
    int   fd;         /* file descriptor */
Packit 7cfc04
    short events;     /* requested events */
Packit 7cfc04
    short revents;    /* returned events */
Packit 7cfc04
};
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.PP
Packit 7cfc04
The caller should specify the number of items in the
Packit 7cfc04
.I fds
Packit 7cfc04
array in
Packit 7cfc04
.IR nfds .
Packit 7cfc04
.PP
Packit 7cfc04
The field
Packit 7cfc04
.I fd
Packit 7cfc04
contains a file descriptor for an open file.
Packit 7cfc04
If this field is negative, then the corresponding
Packit 7cfc04
.I events
Packit 7cfc04
field is ignored and the
Packit 7cfc04
.I revents
Packit 7cfc04
field returns zero.
Packit 7cfc04
(This provides an easy way of ignoring a
Packit 7cfc04
file descriptor for a single
Packit 7cfc04
.BR poll ()
Packit 7cfc04
call: simply negate the
Packit 7cfc04
.I fd
Packit 7cfc04
field.
Packit 7cfc04
Note, however, that this technique can't be used to ignore file descriptor 0.)
Packit 7cfc04
.PP
Packit 7cfc04
The field
Packit 7cfc04
.I events
Packit 7cfc04
is an input parameter, a bit mask specifying the events the application
Packit 7cfc04
is interested in for the file descriptor
Packit 7cfc04
.IR fd .
Packit 7cfc04
This field may be specified as zero,
Packit 7cfc04
in which case the only events that can be returned in
Packit 7cfc04
.I revents
Packit 7cfc04
are
Packit 7cfc04
.BR POLLHUP ,
Packit 7cfc04
.BR POLLERR ,
Packit 7cfc04
and
Packit 7cfc04
.B POLLNVAL
Packit 7cfc04
(see below).
Packit 7cfc04
.PP
Packit 7cfc04
The field
Packit 7cfc04
.I revents
Packit 7cfc04
is an output parameter, filled by the kernel with the events that
Packit 7cfc04
actually occurred.
Packit 7cfc04
The bits returned in
Packit 7cfc04
.I revents
Packit 7cfc04
can include any of those specified in
Packit 7cfc04
.IR events ,
Packit 7cfc04
or one of the values
Packit 7cfc04
.BR POLLERR ,
Packit 7cfc04
.BR POLLHUP ,
Packit 7cfc04
or
Packit 7cfc04
.BR POLLNVAL .
Packit 7cfc04
(These three bits are meaningless in the
Packit 7cfc04
.I events
Packit 7cfc04
field, and will be set in the
Packit 7cfc04
.I revents
Packit 7cfc04
field whenever the corresponding condition is true.)
Packit 7cfc04
.PP
Packit 7cfc04
If none of the events requested (and no error) has occurred for any
Packit 7cfc04
of the file descriptors, then
Packit 7cfc04
.BR poll ()
Packit 7cfc04
blocks until one of the events occurs.
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.I timeout
Packit 7cfc04
argument specifies the number of milliseconds that
Packit 7cfc04
.BR poll ()
Packit 7cfc04
should block waiting for a file descriptor to become ready.
Packit 7cfc04
The call will block until either:
Packit 7cfc04
.IP * 3
Packit 7cfc04
a file descriptor becomes ready;
Packit 7cfc04
.IP *
Packit 7cfc04
the call is interrupted by a signal handler; or
Packit 7cfc04
.IP *
Packit 7cfc04
the timeout expires.
Packit 7cfc04
.PP
Packit 7cfc04
Note that the
Packit 7cfc04
.I timeout
Packit 7cfc04
interval will be rounded up to the system clock granularity,
Packit 7cfc04
and kernel scheduling delays mean that the blocking interval
Packit 7cfc04
may overrun by a small amount.
Packit 7cfc04
Specifying a negative value in
Packit 7cfc04
.I timeout
Packit 7cfc04
means an infinite timeout.
Packit 7cfc04
Specifying a
Packit 7cfc04
.I timeout
Packit 7cfc04
of zero causes
Packit 7cfc04
.BR poll ()
Packit 7cfc04
to return immediately, even if no file descriptors are ready.
Packit 7cfc04
.PP
Packit 7cfc04
The bits that may be set/returned in
Packit 7cfc04
.I events
Packit 7cfc04
and
Packit 7cfc04
.I revents
Packit 7cfc04
are defined in \fI<poll.h>\fP:
Packit 7cfc04
.TP
Packit 7cfc04
.B POLLIN
Packit 7cfc04
There is data to read.
Packit 7cfc04
.TP
Packit 7cfc04
.B POLLPRI
Packit 7cfc04
There is some exceptional condition on the file descriptor.
Packit 7cfc04
Possibilities include:
Packit 7cfc04
.RS
Packit 7cfc04
.IP * 3
Packit 7cfc04
There is out-of-band data on a TCP socket (see
Packit 7cfc04
.BR tcp (7)).
Packit 7cfc04
.IP *
Packit 7cfc04
A pseudoterminal master in packet mode has seen a state change on the slave
Packit 7cfc04
(see
Packit 7cfc04
.BR ioctl_tty (2)).
Packit 7cfc04
.IP *
Packit 7cfc04
A
Packit 7cfc04
.I cgroup.events
Packit 7cfc04
file has been modified (see
Packit 7cfc04
.BR cgroups (7)).
Packit 7cfc04
.RE
Packit 7cfc04
.TP
Packit 7cfc04
.B POLLOUT
Packit 7cfc04
Writing is now possible, though a write larger that the available space
Packit 7cfc04
in a socket or pipe will still block (unless
Packit 7cfc04
.B O_NONBLOCK
Packit 7cfc04
is set).
Packit 7cfc04
.TP
Packit 7cfc04
.BR POLLRDHUP " (since Linux 2.6.17)"
Packit 7cfc04
Stream socket peer closed connection,
Packit 7cfc04
or shut down writing half of connection.
Packit 7cfc04
The
Packit 7cfc04
.B _GNU_SOURCE
Packit 7cfc04
feature test macro must be defined
Packit 7cfc04
(before including
Packit 7cfc04
.I any
Packit 7cfc04
header files)
Packit 7cfc04
in order to obtain this definition.
Packit 7cfc04
.TP
Packit 7cfc04
.B POLLERR
Packit 7cfc04
Error condition (only returned in
Packit 7cfc04
.IR revents ;
Packit 7cfc04
ignored in
Packit 7cfc04
.IR events ).
Packit 7cfc04
This bit is also set for a file descriptor referring
Packit 7cfc04
to the write end of a pipe when the read end has been closed.
Packit 7cfc04
.TP
Packit 7cfc04
.B POLLHUP
Packit 7cfc04
Hang up (only returned in
Packit 7cfc04
.IR revents ;
Packit 7cfc04
ignored in
Packit 7cfc04
.IR events ).
Packit 7cfc04
Note that when reading from a channel such as a pipe or a stream socket,
Packit 7cfc04
this event merely indicates that the peer closed its end of the channel.
Packit 7cfc04
Subsequent reads from the channel will return 0 (end of file)
Packit 7cfc04
only after all outstanding data in the channel has been consumed.
Packit 7cfc04
.TP
Packit 7cfc04
.B POLLNVAL
Packit 7cfc04
Invalid request:
Packit 7cfc04
.I fd
Packit 7cfc04
not open (only returned in
Packit 7cfc04
.IR revents ;
Packit 7cfc04
ignored in
Packit 7cfc04
.IR events ).
Packit 7cfc04
.PP
Packit 7cfc04
When compiling with
Packit 7cfc04
.B _XOPEN_SOURCE
Packit 7cfc04
defined, one also has the following,
Packit 7cfc04
which convey no further information beyond the bits listed above:
Packit 7cfc04
.TP
Packit 7cfc04
.B POLLRDNORM
Packit 7cfc04
Equivalent to
Packit 7cfc04
.BR POLLIN .
Packit 7cfc04
.TP
Packit 7cfc04
.B POLLRDBAND
Packit 7cfc04
Priority band data can be read (generally unused on Linux).
Packit 7cfc04
.\" POLLRDBAND is used in the DECnet protocol.
Packit 7cfc04
.TP
Packit 7cfc04
.B POLLWRNORM
Packit 7cfc04
Equivalent to
Packit 7cfc04
.BR POLLOUT .
Packit 7cfc04
.TP
Packit 7cfc04
.B POLLWRBAND
Packit 7cfc04
Priority data may be written.
Packit 7cfc04
.PP
Packit 7cfc04
Linux also knows about, but does not use
Packit 7cfc04
.BR POLLMSG .
Packit 7cfc04
.SS ppoll()
Packit 7cfc04
The relationship between
Packit 7cfc04
.BR poll ()
Packit 7cfc04
and
Packit 7cfc04
.BR ppoll ()
Packit 7cfc04
is analogous to the relationship between
Packit 7cfc04
.BR select (2)
Packit 7cfc04
and
Packit 7cfc04
.BR pselect (2):
Packit 7cfc04
like
Packit 7cfc04
.BR pselect (2),
Packit 7cfc04
.BR ppoll ()
Packit 7cfc04
allows an application to safely wait until either a file descriptor
Packit 7cfc04
becomes ready or until a signal is caught.
Packit 7cfc04
.PP
Packit 7cfc04
Other than the difference in the precision of the
Packit 7cfc04
.I timeout
Packit 7cfc04
argument, the following
Packit 7cfc04
.BR ppoll ()
Packit 7cfc04
call:
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
ready = ppoll(&fds, nfds, tmo_p, &sigmask);
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.PP
Packit 7cfc04
is equivalent to
Packit 7cfc04
.I atomically
Packit 7cfc04
executing the following calls:
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
sigset_t origmask;
Packit 7cfc04
int timeout;
Packit 7cfc04
Packit 7cfc04
timeout = (tmo_p == NULL) ? \-1 :
Packit 7cfc04
          (tmo_p\->tv_sec * 1000 + tmo_p\->tv_nsec / 1000000);
Packit 7cfc04
pthread_sigmask(SIG_SETMASK, &sigmask, &origmask);
Packit 7cfc04
ready = poll(&fds, nfds, timeout);
Packit 7cfc04
pthread_sigmask(SIG_SETMASK, &origmask, NULL);
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.PP
Packit 7cfc04
See the description of
Packit 7cfc04
.BR pselect (2)
Packit 7cfc04
for an explanation of why
Packit 7cfc04
.BR ppoll ()
Packit 7cfc04
is necessary.
Packit 7cfc04
.PP
Packit 7cfc04
If the
Packit 7cfc04
.I sigmask
Packit 7cfc04
argument is specified as NULL, then
Packit 7cfc04
no signal mask manipulation is performed
Packit 7cfc04
(and thus
Packit 7cfc04
.BR ppoll ()
Packit 7cfc04
differs from
Packit 7cfc04
.BR poll ()
Packit 7cfc04
only in the precision of the
Packit 7cfc04
.I timeout
Packit 7cfc04
argument).
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.I tmo_p
Packit 7cfc04
argument specifies an upper limit on the amount of time that
Packit 7cfc04
.BR ppoll ()
Packit 7cfc04
will block.
Packit 7cfc04
This argument is a pointer to a structure of the following form:
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
struct timespec {
Packit 7cfc04
    long    tv_sec;         /* seconds */
Packit 7cfc04
    long    tv_nsec;        /* nanoseconds */
Packit 7cfc04
};
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.PP
Packit 7cfc04
If
Packit 7cfc04
.I tmo_p
Packit 7cfc04
is specified as NULL, then
Packit 7cfc04
.BR ppoll ()
Packit 7cfc04
can block indefinitely.
Packit 7cfc04
.SH RETURN VALUE
Packit 7cfc04
On success, a positive number is returned; this is
Packit 7cfc04
the number of structures which have nonzero
Packit 7cfc04
.I revents
Packit 7cfc04
fields (in other words, those descriptors with events or errors reported).
Packit 7cfc04
A value of 0 indicates that the call timed out and no file
Packit 7cfc04
descriptors were ready.
Packit 7cfc04
On error, \-1 is returned, and
Packit 7cfc04
.I errno
Packit 7cfc04
is set appropriately.
Packit 7cfc04
.SH ERRORS
Packit 7cfc04
.TP
Packit 7cfc04
.B EFAULT
Packit 7cfc04
The array given as argument was not contained in the calling program's
Packit 7cfc04
address space.
Packit 7cfc04
.TP
Packit 7cfc04
.B EINTR
Packit 7cfc04
A signal occurred before any requested event; see
Packit 7cfc04
.BR signal (7).
Packit 7cfc04
.TP
Packit 7cfc04
.B EINVAL
Packit 7cfc04
The
Packit 7cfc04
.I nfds
Packit 7cfc04
value exceeds the
Packit 7cfc04
.B RLIMIT_NOFILE
Packit 7cfc04
value.
Packit 7cfc04
.TP
Packit 7cfc04
.B ENOMEM
Packit 7cfc04
There was no space to allocate file descriptor tables.
Packit 7cfc04
.SH VERSIONS
Packit 7cfc04
The
Packit 7cfc04
.BR poll ()
Packit 7cfc04
system call was introduced in Linux 2.1.23.
Packit 7cfc04
On older kernels that lack this system call,
Packit 7cfc04
.\" library call was introduced in libc 5.4.28
Packit 7cfc04
the glibc (and the old Linux libc)
Packit 7cfc04
.BR poll ()
Packit 7cfc04
wrapper function provides emulation using
Packit 7cfc04
.BR select (2).
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.BR ppoll ()
Packit 7cfc04
system call was added to Linux in kernel 2.6.16.
Packit 7cfc04
The
Packit 7cfc04
.BR ppoll ()
Packit 7cfc04
library call was added in glibc 2.4.
Packit 7cfc04
.SH CONFORMING TO
Packit 7cfc04
.BR poll ()
Packit 7cfc04
conforms to POSIX.1-2001 and POSIX.1-2008.
Packit 7cfc04
.BR ppoll ()
Packit 7cfc04
is Linux-specific.
Packit 7cfc04
.\" NetBSD 3.0 has a pollts() which is like Linux ppoll().
Packit 7cfc04
.SH NOTES
Packit 7cfc04
On some other UNIX systems,
Packit 7cfc04
.\" Darwin, according to a report by Jeremy Sequoia, relayed by Josh Triplett
Packit 7cfc04
.BR poll ()
Packit 7cfc04
can fail with the error
Packit 7cfc04
.B EAGAIN
Packit 7cfc04
if the system fails to allocate kernel-internal resources, rather than
Packit 7cfc04
.B ENOMEM
Packit 7cfc04
as Linux does.
Packit 7cfc04
POSIX permits this behavior.
Packit 7cfc04
Portable programs may wish to check for
Packit 7cfc04
.B EAGAIN
Packit 7cfc04
and loop, just as with
Packit 7cfc04
.BR EINTR .
Packit 7cfc04
.PP
Packit 7cfc04
Some implementations define the nonstandard constant
Packit 7cfc04
.B INFTIM
Packit 7cfc04
with the value \-1 for use as a
Packit 7cfc04
.IR timeout
Packit 7cfc04
for
Packit 7cfc04
.BR poll ().
Packit 7cfc04
This constant is not provided in glibc.
Packit 7cfc04
.PP
Packit 7cfc04
For a discussion of what may happen if a file descriptor being monitored by
Packit 7cfc04
.BR poll ()
Packit 7cfc04
is closed in another thread, see
Packit 7cfc04
.BR select (2).
Packit 7cfc04
.SS C library/kernel differences
Packit 7cfc04
The Linux
Packit 7cfc04
.BR ppoll ()
Packit 7cfc04
system call modifies its
Packit 7cfc04
.I tmo_p
Packit 7cfc04
argument.
Packit 7cfc04
However, the glibc wrapper function hides this behavior
Packit 7cfc04
by using a local variable for the timeout argument that
Packit 7cfc04
is passed to the system call.
Packit 7cfc04
Thus, the glibc
Packit 7cfc04
.BR ppoll ()
Packit 7cfc04
function does not modify its
Packit 7cfc04
.I tmo_p
Packit 7cfc04
argument.
Packit 7cfc04
.PP
Packit 7cfc04
The raw
Packit 7cfc04
.BR ppoll ()
Packit 7cfc04
system call has a fifth argument,
Packit 7cfc04
.IR "size_t sigsetsize" ,
Packit 7cfc04
which specifies the size in bytes of the
Packit 7cfc04
.IR sigmask
Packit 7cfc04
argument.
Packit 7cfc04
The glibc
Packit 7cfc04
.BR ppoll ()
Packit 7cfc04
wrapper function specifies this argument as a fixed value
Packit 7cfc04
(equal to
Packit 7cfc04
.IR sizeof(kernel_sigset_t) ).
Packit 7cfc04
See
Packit 7cfc04
.BR sigprocmask (2)
Packit 7cfc04
for a discussion on the differences between the kernel and the libc
Packit 7cfc04
notion of the sigset.
Packit 7cfc04
.SH BUGS
Packit 7cfc04
See the discussion of spurious readiness notifications under the
Packit 7cfc04
BUGS section of
Packit 7cfc04
.BR select (2).
Packit 7cfc04
.SH SEE ALSO
Packit 7cfc04
.BR restart_syscall (2),
Packit 7cfc04
.BR select (2),
Packit 7cfc04
.BR select_tut (2),
Packit 7cfc04
.BR epoll (7),
Packit 7cfc04
.BR time (7)
Packit 7cfc04
.SH COLOPHON
Packit 7cfc04
This page is part of release 4.15 of the Linux
Packit 7cfc04
.I man-pages
Packit 7cfc04
project.
Packit 7cfc04
A description of the project,
Packit 7cfc04
information about reporting bugs,
Packit 7cfc04
and the latest version of this page,
Packit 7cfc04
can be found at
Packit 7cfc04
\%https://www.kernel.org/doc/man\-pages/.