xref: /freebsd/lib/libsys/eventfd.2 (revision 8269e7673cf033aba67dab8264fe719920c70f87)
1*8269e767SBrooks Davis.\" SPDX-License-Identifier: BSD-2-Clause
2*8269e767SBrooks Davis.\"
3*8269e767SBrooks Davis.\" Copyright (c) 2020 Val Packett <val@packett.cool>
4*8269e767SBrooks Davis.\"
5*8269e767SBrooks Davis.\" Redistribution and use in source and binary forms, with or without
6*8269e767SBrooks Davis.\" modification, are permitted provided that the following conditions
7*8269e767SBrooks Davis.\" are met:
8*8269e767SBrooks Davis.\" 1. Redistributions of source code must retain the above copyright
9*8269e767SBrooks Davis.\"    notice, this list of conditions and the following disclaimer.
10*8269e767SBrooks Davis.\" 2. Redistributions in binary form must reproduce the above copyright
11*8269e767SBrooks Davis.\"    notice, this list of conditions and the following disclaimer in the
12*8269e767SBrooks Davis.\"    documentation and/or other materials provided with the distribution.
13*8269e767SBrooks Davis.\"
14*8269e767SBrooks Davis.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*8269e767SBrooks Davis.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*8269e767SBrooks Davis.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*8269e767SBrooks Davis.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*8269e767SBrooks Davis.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*8269e767SBrooks Davis.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*8269e767SBrooks Davis.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*8269e767SBrooks Davis.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*8269e767SBrooks Davis.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*8269e767SBrooks Davis.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*8269e767SBrooks Davis.\" SUCH DAMAGE.
25*8269e767SBrooks Davis.\"
26*8269e767SBrooks Davis.Dd October 8, 2020
27*8269e767SBrooks Davis.Dt EVENTFD 2
28*8269e767SBrooks Davis.Os
29*8269e767SBrooks Davis.Sh NAME
30*8269e767SBrooks Davis.Nm eventfd
31*8269e767SBrooks Davis.Nd create a file descriptor for event notification
32*8269e767SBrooks Davis.Sh LIBRARY
33*8269e767SBrooks Davis.Lb libc
34*8269e767SBrooks Davis.Sh SYNOPSIS
35*8269e767SBrooks Davis.In sys/eventfd.h
36*8269e767SBrooks Davis.Ft int
37*8269e767SBrooks Davis.Fn eventfd "unsigned int initval" "int flags"
38*8269e767SBrooks Davis.Ft int
39*8269e767SBrooks Davis.Fn eventfd_read "int fd" "eventfd_t *value"
40*8269e767SBrooks Davis.Ft int
41*8269e767SBrooks Davis.Fn eventfd_write "int fd" "eventfd_t value"
42*8269e767SBrooks Davis.Sh DESCRIPTION
43*8269e767SBrooks Davis.Fn eventfd
44*8269e767SBrooks Daviscreates a special file descriptor with event counter or semaphore semantics,
45*8269e767SBrooks Davisdesigned for interprocess communication.
46*8269e767SBrooks DavisThe returned file descriptor refers to a kernel object containing an
47*8269e767SBrooks Davisunsigned 64-bit integer counter, which is initialized with the value of the
48*8269e767SBrooks Davis.Fa initval
49*8269e767SBrooks Davisargument.
50*8269e767SBrooks Davis.Pp
51*8269e767SBrooks DavisThe
52*8269e767SBrooks Davis.Fa flags
53*8269e767SBrooks Davisargument may contain the result of
54*8269e767SBrooks Davis.Em or Ns 'ing
55*8269e767SBrooks Davisthe following values:
56*8269e767SBrooks Davis.Pp
57*8269e767SBrooks Davis.Bl -tag -width "EFD_SEMAPHORE" -compact
58*8269e767SBrooks Davis.It Dv EFD_CLOEXEC
59*8269e767SBrooks Davisset FD_CLOEXEC on the file descriptor
60*8269e767SBrooks Davis.It Dv EFD_NONBLOCK
61*8269e767SBrooks Davisdo not block on read/write operations
62*8269e767SBrooks Davis.It Dv EFD_SEMAPHORE
63*8269e767SBrooks Davisuse semaphore semantics
64*8269e767SBrooks Davis.El
65*8269e767SBrooks Davis.Pp
66*8269e767SBrooks DavisFile operations have the following semantics:
67*8269e767SBrooks Davis.Bl -tag -width EFD_SEMAPHORE
68*8269e767SBrooks Davis.It Xr read 2
69*8269e767SBrooks DavisIf the counter is zero, the call blocks until the counter becomes non-zero, unless
70*8269e767SBrooks Davis.Dv EFD_NONBLOCK
71*8269e767SBrooks Daviswas set, in which case it would fail with
72*8269e767SBrooks Davis.Dv EAGAIN
73*8269e767SBrooks Davisinstead.
74*8269e767SBrooks Davis.Pp
75*8269e767SBrooks DavisIf the counter is non-zero:
76*8269e767SBrooks Davis.Bl -bullet
77*8269e767SBrooks Davis.It
78*8269e767SBrooks DavisIf
79*8269e767SBrooks Davis.Dv EFD_SEMAPHORE
80*8269e767SBrooks Davisis not set, the current value of the counter is returned,
81*8269e767SBrooks Davisand the value is reset to zero.
82*8269e767SBrooks Davis.It
83*8269e767SBrooks DavisIf
84*8269e767SBrooks Davis.Dv EFD_SEMAPHORE
85*8269e767SBrooks Davisis set, the constant 1 is returned, and the value is decremented by 1.
86*8269e767SBrooks Davis.El
87*8269e767SBrooks Davis.Pp
88*8269e767SBrooks DavisThe numeric value is encoded as 64-bit (8 bytes) in host byte order.
89*8269e767SBrooks DavisThe
90*8269e767SBrooks Davis.Xr read 2
91*8269e767SBrooks Daviscall fails with
92*8269e767SBrooks Davis.Dv EINVAL
93*8269e767SBrooks Davisif there is less than 8 bytes available in the supplied buffer.
94*8269e767SBrooks Davis.It Xr write 2
95*8269e767SBrooks DavisAdds the given value to the counter.
96*8269e767SBrooks DavisThe maximum value that can be stored in the counter is the
97*8269e767SBrooks Davismaximum unsigned 64-bit integer value minus one (0xfffffffffffffffe).
98*8269e767SBrooks Davis.Pp
99*8269e767SBrooks DavisIf the resulting value exceeds the maximum, the call would block
100*8269e767SBrooks Davisuntil the value is reduced by
101*8269e767SBrooks Davis.Xr read 2 ,
102*8269e767SBrooks Davisunless
103*8269e767SBrooks Davis.Dv EFD_NONBLOCK
104*8269e767SBrooks Daviswas set, in which case it would fail with
105*8269e767SBrooks Davis.Dv EAGAIN
106*8269e767SBrooks Davisinstead.
107*8269e767SBrooks Davis.Pp
108*8269e767SBrooks DavisThe numeric value is encoded as 64-bit (8 bytes) in host byte order.
109*8269e767SBrooks DavisThe
110*8269e767SBrooks Davis.Xr write 2
111*8269e767SBrooks Daviscall fails with
112*8269e767SBrooks Davis.Dv EINVAL
113*8269e767SBrooks Davisif there is less than 8 bytes available in the supplied buffer,
114*8269e767SBrooks Davisor if the value 0xffffffffffffffff is given.
115*8269e767SBrooks Davis.It Xr poll 2
116*8269e767SBrooks DavisWhen receiving notifications via
117*8269e767SBrooks Davis.Xr poll 2 /
118*8269e767SBrooks Davis.Xr ppoll 2 /
119*8269e767SBrooks Davis.Xr select 2 /
120*8269e767SBrooks Davis.Xr pselect 2 /
121*8269e767SBrooks Davis.Xr kqueue 2 ,
122*8269e767SBrooks Davisthe following semantics apply:
123*8269e767SBrooks Davis.Bl -bullet
124*8269e767SBrooks Davis.It
125*8269e767SBrooks DavisThe file descriptor is readable when the counter is greater than zero.
126*8269e767SBrooks Davis.It
127*8269e767SBrooks DavisThe file descriptor is writable when the counter is less than the maximum value.
128*8269e767SBrooks Davis.El
129*8269e767SBrooks Davis.El
130*8269e767SBrooks Davis.Pp
131*8269e767SBrooks DavisFile descriptors created by
132*8269e767SBrooks Davis.Fn eventfd
133*8269e767SBrooks Davisare passable to other processes via
134*8269e767SBrooks Davis.Xr sendmsg 2
135*8269e767SBrooks Davisand are preserved across
136*8269e767SBrooks Davis.Xr fork 2 ;
137*8269e767SBrooks Davisin both cases the descriptors refer to the same counter from both processes.
138*8269e767SBrooks DavisUnless
139*8269e767SBrooks Davis.Dv O_CLOEXEC
140*8269e767SBrooks Davisflag was specified,
141*8269e767SBrooks Davisthe created file descriptor will remain open across
142*8269e767SBrooks Davis.Xr execve 2
143*8269e767SBrooks Davissystem calls; see
144*8269e767SBrooks Davis.Xr close 2 ,
145*8269e767SBrooks Davis.Xr fcntl 2
146*8269e767SBrooks Davisand
147*8269e767SBrooks Davis.Dv O_CLOEXEC
148*8269e767SBrooks Davisdescription.
149*8269e767SBrooks Davis.Pp
150*8269e767SBrooks Davis.Fn eventfd_read
151*8269e767SBrooks Davisand
152*8269e767SBrooks Davis.Fn eventfd_write
153*8269e767SBrooks Davisare thin wrappers around
154*8269e767SBrooks Davis.Xr read 2
155*8269e767SBrooks Davisand
156*8269e767SBrooks Davis.Xr write 2
157*8269e767SBrooks Davissystem calls,
158*8269e767SBrooks Davisprovided for compatibility with glibc.
159*8269e767SBrooks Davis.Sh RETURN VALUES
160*8269e767SBrooks DavisIf successful,
161*8269e767SBrooks Davis.Fn eventfd
162*8269e767SBrooks Davisreturns a non-negative integer, termed a file descriptor.
163*8269e767SBrooks DavisIt returns \-1 on failure, and sets
164*8269e767SBrooks Davis.Va errno
165*8269e767SBrooks Davisto indicate the error.
166*8269e767SBrooks Davis.Pp
167*8269e767SBrooks DavisThe
168*8269e767SBrooks Davis.Fn eventfd_read
169*8269e767SBrooks Davisand
170*8269e767SBrooks Davis.Fn eventfd_write
171*8269e767SBrooks Davisfunctions return 0 if the operation succeeded, -1 otherwise.
172*8269e767SBrooks Davis.Sh ERRORS
173*8269e767SBrooks Davis.Fn eventfd
174*8269e767SBrooks Davismay fail with:
175*8269e767SBrooks Davis.Bl -tag -width Er
176*8269e767SBrooks Davis.It Bq Er EINVAL
177*8269e767SBrooks DavisThe
178*8269e767SBrooks Davis.Fa flags
179*8269e767SBrooks Davisargument given to
180*8269e767SBrooks Davis.Fn eventfd
181*8269e767SBrooks Davishas unknown bits set.
182*8269e767SBrooks Davis.It Bq Er EMFILE
183*8269e767SBrooks DavisThe process has already reached its limit for open
184*8269e767SBrooks Davisfile descriptors.
185*8269e767SBrooks Davis.It Bq Er ENFILE
186*8269e767SBrooks DavisThe system file table is full.
187*8269e767SBrooks Davis.It Bq Er ENOMEM
188*8269e767SBrooks DavisNo memory was available to create the kernel object.
189*8269e767SBrooks Davis.El
190*8269e767SBrooks Davis.Sh SEE ALSO
191*8269e767SBrooks Davis.Xr close 2 ,
192*8269e767SBrooks Davis.Xr kqueue 2 ,
193*8269e767SBrooks Davis.Xr poll 2 ,
194*8269e767SBrooks Davis.Xr read 2 ,
195*8269e767SBrooks Davis.Xr select 2 ,
196*8269e767SBrooks Davis.Xr write 2
197*8269e767SBrooks Davis.Sh STANDARDS
198*8269e767SBrooks DavisThe
199*8269e767SBrooks Davis.Fn eventfd
200*8269e767SBrooks Davissystem call is non-standard.
201*8269e767SBrooks DavisIt is present in Linux.
202*8269e767SBrooks Davis.Sh HISTORY
203*8269e767SBrooks DavisThe
204*8269e767SBrooks Davis.Fn eventfd
205*8269e767SBrooks Davissystem call first appeared in
206*8269e767SBrooks Davis.Fx 13.0 .
207