summaryrefslogtreecommitdiff
path: root/ell/io.c
blob: 878060cd7c2f373e3fbf7404986868046bec8b60 (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
/*
 *
 *  Embedded Linux library
 *
 *  Copyright (C) 2011-2014  Intel Corporation. All rights reserved.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <errno.h>
#include <unistd.h>
#include <sys/epoll.h>

#include "util.h"
#include "io.h"
#include "private.h"

/**
 * SECTION:io
 * @short_description: IO support
 *
 * IO support
 */

/**
 * l_io:
 *
 * Opague object representing the IO.
 */
struct l_io {
	int fd;
	uint32_t events;
	bool close_on_destroy;
	l_io_read_cb_t read_handler;
	l_io_destroy_cb_t read_destroy;
	void *read_data;
	l_io_write_cb_t write_handler;
	l_io_destroy_cb_t write_destroy;
	void *write_data;
	l_io_disconnect_cb_t disconnect_handler;
	l_io_destroy_cb_t disconnect_destroy;
	void *disconnect_data;
	l_io_debug_cb_t debug_handler;
	l_io_destroy_cb_t debug_destroy;
	void *debug_data;
};

static void io_cleanup(void *user_data)
{
	struct l_io *io = user_data;

	l_util_debug(io->debug_handler, io->debug_data, "cleanup <%p>", io);

	if (io->write_destroy)
		io->write_destroy(io->write_data);

	io->write_handler = NULL;
	io->write_data = NULL;

	if (io->read_destroy)
		io->read_destroy(io->read_data);

	io->read_handler = NULL;
	io->read_data = NULL;

	if (io->close_on_destroy)
		close(io->fd);

	io->fd = -1;
}

static void io_closed(struct l_io *io)
{
	/*
	 * Save off copies of disconnect_handler, disconnect_destroy
	 * and disconnect_data in case the handler calls io_destroy
	 */
	l_io_disconnect_cb_t handler = io->disconnect_handler;
	l_io_destroy_cb_t destroy = io->disconnect_destroy;
	void *disconnect_data = io->disconnect_data;

	io->disconnect_handler = NULL;
	io->disconnect_destroy = NULL;
	io->disconnect_data = NULL;

	if (handler)
		handler(io, disconnect_data);

	if (destroy)
		destroy(disconnect_data);
}

static void io_callback(int fd, uint32_t events, void *user_data)
{
	struct l_io *io = user_data;

	if (unlikely(events & (EPOLLERR | EPOLLHUP))) {
		l_util_debug(io->debug_handler, io->debug_data,
						"disconnect event <%p>", io);
		watch_remove(io->fd);
		io_closed(io);
		return;
	}

	if ((events & EPOLLIN) && io->read_handler) {
		l_util_debug(io->debug_handler, io->debug_data,
						"read event <%p>", io);

		if (!io->read_handler(io, io->read_data)) {
			if (io->read_destroy)
				io->read_destroy(io->read_data);

			io->read_handler = NULL;
			io->read_destroy = NULL;
			io->read_data = NULL;

			io->events &= ~EPOLLIN;

			if (watch_modify(io->fd, io->events, false) == -EBADF) {
				io->close_on_destroy = false;
				watch_clear(io->fd);
				io_closed(io);
				return;
			}
		}
	}

	if ((events & EPOLLOUT) && io->write_handler) {
		l_util_debug(io->debug_handler, io->debug_data,
						"write event <%p>", io);

		if (!io->write_handler(io, io->write_data)) {
			if (io->write_destroy)
				io->write_destroy(io->write_data);

			io->write_handler = NULL;
			io->write_destroy = NULL;
			io->write_data = NULL;

			io->events &= ~EPOLLOUT;

			if (watch_modify(io->fd, io->events, false) == -EBADF) {
				io->close_on_destroy = false;
				watch_clear(io->fd);
				io_closed(io);
				return;
			}
		}
	}
}

/**
 * l_io_new:
 * @fd: file descriptor
 *
 * Create new IO handling for a given file descriptor.
 *
 * Returns: a newly allocated #l_io object
 **/
LIB_EXPORT struct l_io *l_io_new(int fd)
{
	struct l_io *io;
	int err;

	if (unlikely(fd < 0))
		return NULL;

	io = l_new(struct l_io, 1);

	io->fd = fd;
	io->events = EPOLLHUP | EPOLLERR;
	io->close_on_destroy = false;

	err = watch_add(io->fd, io->events, io_callback, io, io_cleanup);
	if (err) {
		l_free(io);
		return NULL;
	}

	return io;
}

/**
 * l_io_destroy:
 * @io: IO object
 *
 * Free IO object and close file descriptor (if enabled).
 **/
LIB_EXPORT void l_io_destroy(struct l_io *io)
{
	if (unlikely(!io))
		return;

	if (io->fd != -1)
		watch_remove(io->fd);

	io_closed(io);

	if (io->debug_destroy)
		io->debug_destroy(io->debug_data);

	l_free(io);
}

/**
 * l_io_get_fd:
 * @io: IO object
 *
 * Returns: file descriptor associated with @io
 **/
LIB_EXPORT int l_io_get_fd(struct l_io *io)
{
	if (unlikely(!io))
		return -1;

	return io->fd;
}

/**
 * l_io_set_close_on_destroy:
 * @io: IO object
 * @do_close: setting for destroy handling
 *
 * Set the automatic closing of the file descriptor when destroying @io.
 *
 * Returns: #true on success and #false on failure
 **/
LIB_EXPORT bool l_io_set_close_on_destroy(struct l_io *io, bool do_close)
{
	if (unlikely(!io))
		return false;

	io->close_on_destroy = do_close;

	return true;
}

/**
 * l_io_set_read_handler:
 * @io: IO object
 * @callback: read handler callback function
 * @user_data: user data provided to read handler callback function
 * @destroy: destroy function for user data
 *
 * Set read function.
 *
 * Returns: #true on success and #false on failure
 **/
LIB_EXPORT bool l_io_set_read_handler(struct l_io *io, l_io_read_cb_t callback,
				void *user_data, l_io_destroy_cb_t destroy)
{
	uint32_t events;
	int err;

	if (unlikely(!io || io->fd < 0))
		return false;

	l_util_debug(io->debug_handler, io->debug_data,
					"set read handler <%p>", io);

	if (io->read_destroy)
		io->read_destroy(io->read_data);

	if (callback)
		events = io->events | EPOLLIN;
	else
		events = io->events & ~EPOLLIN;

	io->read_handler = callback;
	io->read_destroy = destroy;
	io->read_data = user_data;

	if (events == io->events)
		return true;

	err = watch_modify(io->fd, events, false);
	if (err)
		return false;

	io->events = events;

	return true;
}

/**
 * l_io_set_write_handler:
 * @io: IO object
 * @callback: write handler callback function
 * @user_data: user data provided to write handler callback function
 * @destroy: destroy function for user data
 *
 * Set write function.
 *
 * Returns: #true on success and #false on failure
 **/
LIB_EXPORT bool l_io_set_write_handler(struct l_io *io, l_io_write_cb_t callback,
				void *user_data, l_io_destroy_cb_t destroy)
{
	uint32_t events;
	int err;

	if (unlikely(!io || io->fd < 0))
		return false;

	l_util_debug(io->debug_handler, io->debug_data,
					"set write handler <%p>", io);

	if (io->write_handler == callback && io->write_destroy == destroy &&
						io->write_data == user_data)
		return true;

	if (io->write_destroy)
		io->write_destroy(io->write_data);

	if (callback)
		events = io->events | EPOLLOUT;
	else
		events = io->events & ~EPOLLOUT;

	io->write_handler = callback;
	io->write_destroy = destroy;
	io->write_data = user_data;

	if (events == io->events)
		return true;

	err = watch_modify(io->fd, events, false);
	if (err)
		return false;

	io->events = events;

	return true;
}

/**
 * l_io_set_disconnect_handler:
 * @io: IO object
 * @callback: disconnect handler callback function
 * @user_data: user data provided to disconnect handler callback function
 * @destroy: destroy function for user data
 *
 * Set disconnect function.
 *
 * Returns: #true on success and #false on failure
 **/
LIB_EXPORT bool l_io_set_disconnect_handler(struct l_io *io,
				l_io_disconnect_cb_t callback,
				void *user_data, l_io_destroy_cb_t destroy)
{
	if (unlikely(!io || io->fd < 0))
		return false;

	l_util_debug(io->debug_handler, io->debug_data,
					"set disconnect handler <%p>", io);

	if (io->disconnect_destroy)
		io->disconnect_destroy(io->disconnect_data);

	io->disconnect_handler = callback;
	io->disconnect_destroy = destroy;
	io->disconnect_data = user_data;

	return true;
}

/**
 * l_io_set_debug:
 * @io: IO object
 * @callback: debug callback function
 * @user_data: user data provided to debug callback function
 * @destroy: destroy function for user data
 *
 * Set debug function.
 *
 * Returns: #true on success and #false on failure
 **/
LIB_EXPORT bool l_io_set_debug(struct l_io *io, l_io_debug_cb_t callback,
				void *user_data, l_io_destroy_cb_t destroy)
{
	if (unlikely(!io))
		return false;

	if (io->debug_destroy)
		io->debug_destroy(io->debug_data);

	io->debug_handler = callback;
	io->debug_destroy = destroy;
	io->debug_data = user_data;

	return true;
}