aboutsummaryrefslogtreecommitdiff
path: root/lib/libc/sys/eventfd.2
blob: 597c58122033eea89fe5a6bed2934af7f20380b0 (plain) (blame)
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
.\" SPDX-License-Identifier: BSD-2-Clause
.\"
.\" Copyright (c) 2020 Greg V
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\"    notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\"    notice, this list of conditions and the following disclaimer in the
.\"    documentation and/or other materials provided with the distribution.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" $FreeBSD$
.\"
.Dd October 8, 2020
.Dt EVENTFD 2
.Os
.Sh NAME
.Nm eventfd
.Nd create a file descriptor for event notification
.Sh LIBRARY
.Lb libc
.Sh SYNOPSIS
.In sys/eventfd.h
.Ft int
.Fn eventfd "unsigned int initval" "int flags"
.Ft int
.Fn eventfd_read "int fd" "eventfd_t *value"
.Ft int
.Fn eventfd_write "int fd" "eventfd_t value"
.Sh DESCRIPTION
.Fn eventfd
creates a special file descriptor with event counter or semaphore semantics,
designed for interprocess communication.
The returned file descriptor refers to a kernel object containing an
unsigned 64-bit integer counter, which is initialized with the value of the
.Fa initval
argument.
.Pp
The
.Fa flags
argument may contain the result of
.Em or Ns 'ing
the following values:
.Pp
.Bl -tag -width "EFD_SEMAPHORE" -compact
.It Dv EFD_CLOEXEC
set FD_CLOEXEC on the file descriptor
.It Dv EFD_NONBLOCK
do not block on read/write operations
.It Dv EFD_SEMAPHORE
use semaphore semantics
.El
.Pp
File operations have the following semantics:
.Bl -tag -width EFD_SEMAPHORE
.It Xr read 2
If the counter is zero, the call blocks until the counter becomes non-zero, unless
.Dv EFD_NONBLOCK
was set, in which case it would fail with
.Dv EAGAIN
instead.
.Pp
If the counter is non-zero:
.Bl -bullet
.It
If
.Dv EFD_SEMAPHORE
is not set, the current value of the counter is returned,
and the value is reset to zero.
.It
If
.Dv EFD_SEMAPHORE
is set, the constant 1 is returned, and the value is decremented by 1.
.El
.Pp
The numeric value is encoded as 64-bit (8 bytes) in host byte order.
The
.Xr read 2
call fails with
.Dv EINVAL
if there is less than 8 bytes available in the supplied buffer.
.It Xr write 2
Adds the given value to the counter.
The maximum value that can be stored in the counter is the
maximum unsigned 64-bit integer value minus one (0xfffffffffffffffe).
.Pp
If the resulting value exceeds the maximum, the call would block
until the value is reduced by
.Xr read 2 ,
unless
.Dv EFD_NONBLOCK
was set, in which case it would fail with
.Dv EAGAIN
instead.
.Pp
The numeric value is encoded as 64-bit (8 bytes) in host byte order.
The
.Xr write 2
call fails with
.Dv EINVAL
if there is less than 8 bytes available in the supplied buffer,
or if the value 0xffffffffffffffff is given.
.It Xr poll 2
When receiving notifications via
.Xr poll 2 /
.Xr ppoll 2 /
.Xr select 2 /
.Xr pselect 2 /
.Xr kqueue 2 ,
the following semantics apply:
.Bl -bullet
.It
The file descriptor is readable when the counter is greater than zero.
.It
The file descriptor is writable when the counter is less than the maximum value.
.El
.El
.Pp
File descriptors created by
.Fn eventfd
are passable to other processes via
.Xr sendmsg 2
and are preserved across
.Xr fork 2 ;
in both cases the descriptors refer to the same counter from both processes.
Unless
.Dv O_CLOEXEC
flag was specified,
the created file descriptor will remain open across
.Xr execve 2
system calls; see
.Xr close 2 ,
.Xr fcntl 2
and
.Dv O_CLOEXEC
description.
.Pp
.Fn eventfd_read
and
.Fn eventfd_write
are thin wrappers around
.Xr read 2
and
.Xr write 2
system calls,
provided for compatibility with glibc.
.Sh RETURN VALUES
If successful,
.Fn eventfd
returns a non-negative integer, termed a file descriptor.
It returns \-1 on failure, and sets
.Va errno
to indicate the error.
.Pp
The
.Fn eventfd_read
and
.Fn eventfd_write
functions return 0 if the operation succeeded, -1 otherwise.
.Sh ERRORS
.Fn eventfd
may fail with:
.Bl -tag -width Er
.It Bq Er EINVAL
The
.Fa flags
argument given to
.Fn eventfd
has unknown bits set.
.It Bq Er EMFILE
The process has already reached its limit for open
file descriptors.
.It Bq Er ENFILE
The system file table is full.
.It Bq Er ENOMEM
No memory was available to create the kernel object.
.El
.Sh SEE ALSO
.Xr close 2 ,
.Xr kqueue 2 ,
.Xr poll 2 ,
.Xr read 2 ,
.Xr select 2 ,
.Xr write 2
.Sh STANDARDS
The
.Fn eventfd
system call is non-standard.
It is present in Linux.
.Sh HISTORY
The
.Fn eventfd
system call first appeared in
.Fx 13.0 .