-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_base_new.3
307 lines (307 loc) · 7.8 KB
/
event_base_new.3
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
.\" $OpenBSD$
.\" Copyright (c) 2023 Ted Bullock <[email protected]>
.\"
.\" Permission to use, copy, modify, and distribute this software for any
.\" purpose with or without fee is hereby granted, provided that the above
.\" copyright notice and this permission notice appear in all copies.
.\"
.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
.\"
.Dd $Mdocdate$
.Dt EVENT_BASE_NEW 3
.Os
.Sh NAME
.Nm event_base_new ,
.Nm event_init ,
.Nm event_reinit ,
.Nm event_base_free
.Nd event_base structure initialization
.Sh SYNOPSIS
.In event.h
.Ft "struct event_base *"
.Fn event_base_new void
.Ft "struct event_base *"
.Fn event_init void
.Ft int
.Fn event_reinit "struct event_base *base"
.Ft void
.Fn event_base_free "struct event_base *base"
.Sh DESCRIPTION
The functions
.Fn event_base_new
and
.Fn event_init
allocate memory and initialize an opaque
.Vt event_base
structure.
This structure is used to schedule and monitor events using the operating
system's most efficient or stable kernel notification method.
.Pp
Kernel notification methods are ways for a program to be notified of
events that occur in the operating system's kernel.
Examples of such events include changes to file descriptors, file I/O
operations or network activity.
The library chooses from several methods to notify programs about events.
Each method is implemented using a system call, including
.Xr kqueue 2 ,
.Xr poll 2 ,
or
.Xr select 2 .
By default,
.Ox
uses the
.Xr kqueue 2
method.
.Pp
Environment variables can modify the behavior of
.Fn event_base_new
to disable library support for certain kernel notification methods and to
enable additional diagnostic reporting.
For a complete list of environment variables refer to
.Sx ENVIRONMENT .
.Pp
The function
.Fn event_init
is a version of
.Fn event_base_new
that sets internal global variables and can support one event loop.
Invoking most functions documented in the event library to require
.Fn event_init
without first calling
.Fn event_init
generates a segmentation fault.
.Pp
After calling
.Xr fork 2 ,
the function
.Fn event_reinit
must be invoked to reset the event queues and any events registered with
the kernel notification method in the
.Vt event_base
structure of the child process.
The function takes a single argument, a pointer to an
.Vt event_base
structure returned by
.Fn event_init
or
.Fn event_base_new .
The behavior is undefined if
.Fa base
is
.Dv NULL .
.Pp
The
.Fn event_base_free
function releases all resources associated with an
.Vt event_base
structure and is intended to be called after the event loop has been stopped.
If
.Fa base
is not
.Dv NULL
it is a pointer returned by an earlier call to
.Fn event_base_new
or
.Fn event_init .
If
.Fa base
is
.Dv NULL
and the library was initialized with
.Fn event_init ,
then
.Fn event_base_free
deallocates internal global library variables.
Otherwise, it triggers an
.Xr assert 3
call.
.Pp
Functions documented to require
.Fn event_init
generate a segmentation fault if invoked after
.Fn event_base_free
destroys the internal global variables initialized by
.Fn event_init .
There is no API to restore these internal global variables to their default
state once they are configured with
.Fn event_init .
.Pp
Errors and diagnostics from many event library functions are reported using
the log callback system configured with
.Xr event_set_log_callback 3 .
In case of an error, the functions report the problem and then call
.Xr exit 3
with a status of 1.
.Sh RETURN VALUES
.Fn event_base_new
and
.Fn event_init
return the newly allocated
.Vt event_base
structure.
.Pp
On success,
.Fn event_reinit
returns 0.
If one or more events fail to reinitialize, the function returns -1.
.Pp
Fatal error conditions do
.Em not
return.
.Sh ENVIRONMENT
Environment variables that modify the behavior of
.Fn event_base_new
and
.Fn event_init
are:
.Bl -tag -width Ds
.It Ev EVENT_SHOW_METHOD
If the log callback is configured, report which kernel notification method the
library is using.
.It Ev EVENT_NOKQUEUE
Disable library support for
.Xr kqueue 2 .
.It Ev EVENT_NOPOLL
Disable library support for
.Xr poll 2 .
.It Ev EVENT_NOSELECT
Disable library support for
.Xr select 2 .
.El
.Pp
These environment variables work unless the library detects the program
was executed as either setuid or setgid using
.Xr issetugid 2 .
The value of the environment variables is ignored, even if it is
empty or zero.
.Sh DIAGNOSTICS
The event library relies on its own system to issue messages via callbacks via
.Xr event_set_log_callback 3 .
.Pp
.Sy Error
messages that
.Fn event_base_new
and
.Fn event_init
can report and what they mean:
.Bl -tag -width Ds
.It Dq event_base_new: calloc: Cannot allocate memory
.Fn event_base_new
failed to allocate memory for the
.Vt event_base
structure.
.It Dq event_base_priority_init: calloc: Cannot allocate memory
.Fn event_base_new
failed to allocate memory for event queue.
.It Dq event_base_priority_init: malloc: Cannot allocate memory
.Fn event_base_new
failed to allocate memory for event queue.
.It Dq event_base_new: no event mechanism available
Event library support for all kernel notification
methods is disabled; see
.Sx ENVIRONMENT .
.El
.Pp
.Sy Diagnostic
messages that
.Fn event_base_new
and
.Fn event_init
can report and what they mean:
.Bl -tag -width Ds
.It Dq libevent using: kqueue
The environment variable
.Ev EVENT_SHOW_METHOD
is defined and the event library is using
.Xr kqueue 2 .
.It Dq libevent using: poll
The environment variable
.Ev EVENT_SHOW_METHOD
is defined and the event library is using
.Xr poll 2 .
.It Dq libevent using: select
The environment variable
.Ev EVENT_SHOW_METHOD
is defined and the event library is using
.Xr select 2 .
.El
.Pp
.Sy Error
messages that
.Fn event_reinit
can report and what they mean:
.Bl -tag -width Ds
.It Dq event_base_reinit: could not reinitialize event mechanism
Failed to reinitialize the kernel notification method.
.It Dq event_queue_remove: Em @p Po fd Em @d Pc not on queue Em @x
Failed to clear an event queue.
The message format is populated as follows:
.Bl -hyphen -compact -width 1n
.It
.Em @p :
Event structure memory address in hexadecimal format.
.It
.Em @d :
File descriptor in decimal format.
.It
.Em @x :
Event queue number in hexadecimal format.
.El
.It Dq event_queue_remove: unknown queue Em @q
Encountered an unknown queue number, indicated by
.Em @q
in hexadecimal format.
.El
.Sh ERRORS
The event library, rather than setting
.Xr errno 2 ,
often employs an alternative system to issue messages through callbacks.
This system is configured using
.Xr event_set_log_callback 3 .
.Sh SEE ALSO
.Xr fork 2 ,
.Xr kqueue 2 ,
.Xr poll 2 ,
.Xr select 2 ,
.Xr event_base_loop 3 ,
.Xr event_set_log_callback 3
.Sh HISTORY
The
.Ox
event library is a modified version of libevent-1.4.
.Pp
The function
.Fn event_init
first appeared in libevent-0.1 and has been available since
.Ox 3.2 .
.Pp
.Fn event_base_new
and
.Fn event_reinit
first appeared in libevent-1.4.1 and have been available since
.Ox 4.8 .
.Pp
Support for environment variables first appeared in libevent-0.7a and
.Ox 3.6 .
.Sh AUTHORS
The event library and these functions were written by
.An -nosplit
.An Niels Provos .
.Pp
This manual page was written by
.An Ted Bullock Aq Mt [email protected] .
.Sh CAVEATS
The event API is not thread safe unless only one
.Vt "event_base"
structure is accessible per thread or care is taken to lock access.
The simplified API that is initialized by using
.Fn event_init
instead of
.Fn event_base_new
is not thread safe.