summaryrefslogtreecommitdiffstats
path: root/upstream/debian-unstable/man2/eventfd.2
blob: 23a073089f902b820cb2fc701e257c77a698208e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
'\" t
.\" Copyright (C) 2008 Michael Kerrisk <mtk.manpages@gmail.com>
.\" starting from a version by Davide Libenzi <davidel@xmailserver.org>
.\"
.\" SPDX-License-Identifier: GPL-2.0-or-later
.\"
.\" 2008-10-10, mtk: describe eventfd2(), and EFD_NONBLOCK and EFD_CLOEXEC
.\"
.TH eventfd 2 2024-05-02 "Linux man-pages 6.8"
.SH NAME
eventfd \- create a file descriptor for event notification
.SH LIBRARY
Standard C library
.RI ( libc ", " \-lc )
.SH SYNOPSIS
.nf
.B #include <sys/eventfd.h>
.P
.BI "int eventfd(unsigned int " initval ", int " flags );
.fi
.SH DESCRIPTION
.BR eventfd ()
creates an "eventfd object" that can be used as
an event wait/notify mechanism by user-space applications,
and by the kernel to notify user-space applications of events.
The object contains an unsigned 64-bit integer
.RI ( uint64_t )
counter that is maintained by the kernel.
This counter is initialized with the value specified in the argument
.IR initval .
.P
As its return value,
.BR eventfd ()
returns a new file descriptor that can be used to refer to the
eventfd object.
.P
The following values may be bitwise ORed in
.I flags
to change the behavior of
.BR eventfd ():
.TP
.BR EFD_CLOEXEC " (since Linux 2.6.27)"
Set the close-on-exec
.RB ( FD_CLOEXEC )
flag on the new file descriptor.
See the description of the
.B O_CLOEXEC
flag in
.BR open (2)
for reasons why this may be useful.
.TP
.BR EFD_NONBLOCK " (since Linux 2.6.27)"
Set the
.B O_NONBLOCK
file status flag on the open file description (see
.BR open (2))
referred to by the new file descriptor.
Using this flag saves extra calls to
.BR fcntl (2)
to achieve the same result.
.TP
.BR EFD_SEMAPHORE " (since Linux 2.6.30)"
Provide semaphore-like semantics for reads from the new file descriptor.
See below.
.P
Up to Linux 2.6.26, the
.I flags
argument is unused, and must be specified as zero.
.P
The following operations can be performed on the file descriptor returned by
.BR eventfd ():
.TP
.BR read (2)
Each successful
.BR read (2)
returns an 8-byte integer.
A
.BR read (2)
fails with the error
.B EINVAL
if the size of the supplied buffer is less than 8 bytes.
.IP
The value returned by
.BR read (2)
is in host byte order\[em]that is,
the native byte order for integers on the host machine.
.IP
The semantics of
.BR read (2)
depend on whether the eventfd counter currently has a nonzero value
and whether the
.B EFD_SEMAPHORE
flag was specified when creating the eventfd file descriptor:
.RS
.IP \[bu] 3
If
.B EFD_SEMAPHORE
was not specified and the eventfd counter has a nonzero value, then a
.BR read (2)
returns 8 bytes containing that value,
and the counter's value is reset to zero.
.IP \[bu]
If
.B EFD_SEMAPHORE
was specified and the eventfd counter has a nonzero value, then a
.BR read (2)
returns 8 bytes containing the value 1,
and the counter's value is decremented by 1.
.IP \[bu]
If the eventfd counter is zero at the time of the call to
.BR read (2),
then the call either blocks until the counter becomes nonzero
(at which time, the
.BR read (2)
proceeds as described above)
or fails with the error
.B EAGAIN
if the file descriptor has been made nonblocking.
.RE
.TP
.BR write (2)
A
.BR write (2)
call adds the 8-byte integer value supplied in its
buffer to the counter.
The maximum value that may be stored in the counter is the largest
unsigned 64-bit value minus 1 (i.e., 0xfffffffffffffffe).
If the addition would cause the counter's value to exceed
the maximum, then the
.BR write (2)
either blocks until a
.BR read (2)
is performed on the file descriptor,
or fails with the error
.B EAGAIN
if the file descriptor has been made nonblocking.
.IP
A
.BR write (2)
fails with the error
.B EINVAL
if the size of the supplied buffer is less than 8 bytes,
or if an attempt is made to write the value 0xffffffffffffffff.
.TP
.BR poll (2)
.TQ
.BR select (2)
.TQ
(and similar)
The returned file descriptor supports
.BR poll (2)
(and analogously
.BR epoll (7))
and
.BR select (2),
as follows:
.RS
.IP \[bu] 3
The file descriptor is readable
(the
.BR select (2)
.I readfds
argument; the
.BR poll (2)
.B POLLIN
flag)
if the counter has a value greater than 0.
.IP \[bu]
The file descriptor is writable
(the
.BR select (2)
.I writefds
argument; the
.BR poll (2)
.B POLLOUT
flag)
if it is possible to write a value of at least "1" without blocking.
.IP \[bu]
If an overflow of the counter value was detected,
then
.BR select (2)
indicates the file descriptor as being both readable and writable, and
.BR poll (2)
returns a
.B POLLERR
event.
As noted above,
.BR write (2)
can never overflow the counter.
However an overflow can occur if 2\[ha]64
eventfd "signal posts" were performed by the KAIO
subsystem (theoretically possible, but practically unlikely).
If an overflow has occurred, then
.BR read (2)
will return that maximum
.I uint64_t
value (i.e., 0xffffffffffffffff).
.RE
.IP
The eventfd file descriptor also supports the other file-descriptor
multiplexing APIs:
.BR pselect (2)
and
.BR ppoll (2).
.TP
.BR close (2)
When the file descriptor is no longer required it should be closed.
When all file descriptors associated with the same eventfd object
have been closed, the resources for object are freed by the kernel.
.P
A copy of the file descriptor created by
.BR eventfd ()
is inherited by the child produced by
.BR fork (2).
The duplicate file descriptor is associated with the same
eventfd object.
File descriptors created by
.BR eventfd ()
are preserved across
.BR execve (2),
unless the close-on-exec flag has been set.
.SH RETURN VALUE
On success,
.BR eventfd ()
returns a new eventfd file descriptor.
On error, \-1 is returned and
.I errno
is set to indicate the error.
.SH ERRORS
.TP
.B EINVAL
An unsupported value was specified in
.IR flags .
.TP
.B EMFILE
The per-process limit on the number of open file descriptors has been reached.
.TP
.B ENFILE
The system-wide limit on the total number of open files has been
reached.
.TP
.B ENODEV
.\" Note from Davide:
.\" The ENODEV error is basically never going to happen if
.\" the kernel boots correctly. That error happen only if during
.\" the kernel initialization, some error occur in the anonymous
.\" inode source initialization.
Could not mount (internal) anonymous inode device.
.TP
.B ENOMEM
There was insufficient memory to create a new
eventfd file descriptor.
.SH ATTRIBUTES
For an explanation of the terms used in this section, see
.BR attributes (7).
.TS
allbox;
lbx lb lb
l l l.
Interface	Attribute	Value
T{
.na
.nh
.BR eventfd ()
T}	Thread safety	MT-Safe
.TE
.SH VERSIONS
.SS C library/kernel differences
There are two underlying Linux system calls:
.BR eventfd ()
and the more recent
.BR eventfd2 ().
The former system call does not implement a
.I flags
argument.
The latter system call implements the
.I flags
values described above.
The glibc wrapper function will use
.BR eventfd2 ()
where it is available.
.SS Additional glibc features
The GNU C library defines an additional type,
and two functions that attempt to abstract some of the details of
reading and writing on an eventfd file descriptor:
.P
.in +4n
.EX
typedef uint64_t eventfd_t;
\&
int eventfd_read(int fd, eventfd_t *value);
int eventfd_write(int fd, eventfd_t value);
.EE
.in
.P
The functions perform the read and write operations on an
eventfd file descriptor,
returning 0 if the correct number of bytes was transferred,
or \-1 otherwise.
.SH STANDARDS
Linux, GNU.
.SH HISTORY
.TP
.BR eventfd ()
Linux 2.6.22,
glibc 2.8.
.\" eventfd() is in glibc 2.7, but reportedly does not build
.TP
.BR eventfd2 ()
Linux 2.6.27 (see VERSIONS).
Since glibc 2.9, the
.BR eventfd ()
wrapper will employ the
.BR eventfd2 ()
system call, if it is supported by the kernel.
.SH NOTES
Applications can use an eventfd file descriptor instead of a pipe (see
.BR pipe (2))
in all cases where a pipe is used simply to signal events.
The kernel overhead of an eventfd file descriptor
is much lower than that of a pipe,
and only one file descriptor is
required (versus the two required for a pipe).
.P
When used in the kernel, an eventfd
file descriptor can provide a bridge from kernel to user space, allowing,
for example, functionalities like KAIO (kernel AIO)
.\" or eventually syslets/threadlets
to signal to a file descriptor that some operation is complete.
.P
A key point about an eventfd file descriptor is that it can be
monitored just like any other file descriptor using
.BR select (2),
.BR poll (2),
or
.BR epoll (7).
This means that an application can simultaneously monitor the
readiness of "traditional" files and the readiness of other
kernel mechanisms that support the eventfd interface.
(Without the
.BR eventfd ()
interface, these mechanisms could not be multiplexed via
.BR select (2),
.BR poll (2),
or
.BR epoll (7).)
.P
The current value of an eventfd counter can be viewed
via the entry for the corresponding file descriptor in the process's
.IR /proc/ pid /fdinfo
directory.
See
.BR proc (5)
for further details.
.SH EXAMPLES
The following program creates an eventfd file descriptor
and then forks to create a child process.
While the parent briefly sleeps,
the child writes each of the integers supplied in the program's
command-line arguments to the eventfd file descriptor.
When the parent has finished sleeping,
it reads from the eventfd file descriptor.
.P
The following shell session shows a sample run of the program:
.P
.in +4n
.EX
.RB "$" " ./a.out 1 2 4 7 14"
Child writing 1 to efd
Child writing 2 to efd
Child writing 4 to efd
Child writing 7 to efd
Child writing 14 to efd
Child completed write loop
Parent about to read
Parent read 28 (0x1c) from efd
.EE
.in
.SS Program source
\&
.\" SRC BEGIN (eventfd.c)
.EX
#include <err.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/eventfd.h>
#include <sys/types.h>
#include <unistd.h>
\&
int
main(int argc, char *argv[])
{
    int       efd;
    uint64_t  u;
    ssize_t   s;
\&
    if (argc < 2) {
        fprintf(stderr, "Usage: %s <num>...\en", argv[0]);
        exit(EXIT_FAILURE);
    }
\&
    efd = eventfd(0, 0);
    if (efd == \-1)
        err(EXIT_FAILURE, "eventfd");
\&
    switch (fork()) {
    case 0:
        for (size_t j = 1; j < argc; j++) {
            printf("Child writing %s to efd\en", argv[j]);
            u = strtoull(argv[j], NULL, 0);
                    /* strtoull() allows various bases */
            s = write(efd, &u, sizeof(uint64_t));
            if (s != sizeof(uint64_t))
                err(EXIT_FAILURE, "write");
        }
        printf("Child completed write loop\en");
\&
        exit(EXIT_SUCCESS);
\&
    default:
        sleep(2);
\&
        printf("Parent about to read\en");
        s = read(efd, &u, sizeof(uint64_t));
        if (s != sizeof(uint64_t))
            err(EXIT_FAILURE, "read");
        printf("Parent read %"PRIu64" (%#"PRIx64") from efd\en", u, u);
        exit(EXIT_SUCCESS);
\&
    case \-1:
        err(EXIT_FAILURE, "fork");
    }
}
.EE
.\" SRC END
.SH SEE ALSO
.BR futex (2),
.BR pipe (2),
.BR poll (2),
.BR read (2),
.BR select (2),
.BR signalfd (2),
.BR timerfd_create (2),
.BR write (2),
.BR epoll (7),
.BR sem_overview (7)