Blob: io-glib.c

Blob id: 81cd1122b0ea16a11c41447c509993b4e5d895b7

Size: 9.4 KB

  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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
// SPDX-License-Identifier: LGPL-2.1-or-later
/*
 *
 *  BlueZ - Bluetooth protocol stack for Linux
 *
 *  Copyright (C) 2012-2014  Intel Corporation. All rights reserved.
 *
 *
 */

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

#include <errno.h>
#include <sys/socket.h>

#include <glib.h>

#include "src/shared/io.h"

#define	IO_ERR_WATCH_RATELIMIT		(500 * G_TIME_SPAN_MILLISECOND)

struct io_watch {
	struct io *io;
	guint id;
	io_callback_func_t callback;
	io_destroy_func_t destroy;
	void *user_data;
};

struct io {
	int ref_count;
	GIOChannel *channel;
	bool err_watch;
	struct io_watch *read_watch;
	struct io_watch *write_watch;
	struct io_watch *disconnect_watch;
};

struct io_err_watch {
	GSource			source;
	GIOChannel		*io;
	GIOCondition		events;
	gpointer		tag;
};

static guint io_glib_add_err_watch_full(GIOChannel *io, gint priority,
					GIOCondition events,
					GIOFunc func, gpointer user_data,
					GDestroyNotify notify);

static struct io *io_ref(struct io *io)
{
	if (!io)
		return NULL;

	__sync_fetch_and_add(&io->ref_count, 1);

	return io;
}

static void io_unref(struct io *io)
{
	if (!io)
		return;

	if (__sync_sub_and_fetch(&io->ref_count, 1))
		return;

	g_free(io);
}

struct io *io_new(int fd)
{
	struct io *io;

	if (fd < 0)
		return NULL;

	io = g_try_new0(struct io, 1);
	if (!io)
		return NULL;

	io->channel = g_io_channel_unix_new(fd);

	g_io_channel_set_encoding(io->channel, NULL, NULL);
	g_io_channel_set_buffered(io->channel, FALSE);

	g_io_channel_set_close_on_unref(io->channel, FALSE);

	return io_ref(io);
}

static void watch_destroy(void *user_data)
{
	struct io_watch *watch = user_data;
	struct io *io = watch->io;

	if (watch == io->read_watch)
		io->read_watch = NULL;
	else if (watch == io->write_watch)
		io->write_watch = NULL;
	else if (watch == io->disconnect_watch)
		io->disconnect_watch = NULL;

	if (watch->destroy)
		watch->destroy(watch->user_data);

	io_unref(watch->io);
	g_free(watch);
}

void io_destroy(struct io *io)
{
	if (!io)
		return;

	if (io->read_watch) {
		g_source_remove(io->read_watch->id);
		io->read_watch = NULL;
	}

	if (io->write_watch) {
		g_source_remove(io->write_watch->id);
		io->write_watch = NULL;
	}

	if (io->disconnect_watch) {
		g_source_remove(io->disconnect_watch->id);
		io->disconnect_watch = NULL;
	}

	g_io_channel_unref(io->channel);
	io->channel = NULL;

	io_unref(io);
}

int io_get_fd(struct io *io)
{
	if (!io)
		return -ENOTCONN;

	return g_io_channel_unix_get_fd(io->channel);
}

bool io_set_close_on_destroy(struct io *io, bool do_close)
{
	if (!io)
		return false;

	if (do_close)
		g_io_channel_set_close_on_unref(io->channel, TRUE);
	else
		g_io_channel_set_close_on_unref(io->channel, FALSE);

	return true;
}

static gboolean watch_callback(GIOChannel *channel, GIOCondition cond,
							gpointer user_data)
{
	struct io_watch *watch = user_data;
	bool result, destroy;

	destroy = watch == watch->io->disconnect_watch;

	if (!destroy && (cond & (G_IO_ERR | G_IO_NVAL)))
		return FALSE;

	if (watch->callback)
		result = watch->callback(watch->io, watch->user_data);
	else
		result = false;

	return result ? TRUE : FALSE;
}

static struct io_watch *watch_new(struct io *io, GIOCondition cond,
				io_callback_func_t callback, void *user_data,
				io_destroy_func_t destroy)
{
	struct io_watch *watch;
	int prio;

	watch = g_try_new0(struct io_watch, 1);
	if (!watch)
		return NULL;

	watch->io = io_ref(io);
	watch->callback = callback;
	watch->destroy = destroy;
	watch->user_data = user_data;

	prio = cond == G_IO_HUP ? G_PRIORITY_DEFAULT_IDLE : G_PRIORITY_DEFAULT;

	if (!io->err_watch)
		watch->id = g_io_add_watch_full(io->channel, prio,
						cond | G_IO_ERR | G_IO_NVAL,
						watch_callback, watch,
						watch_destroy);
	else
		watch->id = io_glib_add_err_watch_full(io->channel, prio,
						cond | G_IO_ERR | G_IO_NVAL,
						watch_callback, watch,
						watch_destroy);

	if (watch->id == 0) {
		watch_destroy(watch);
		return NULL;
	}

	return watch;
}

static bool io_set_handler(struct io *io, GIOCondition cond,
				io_callback_func_t callback, void *user_data,
				io_destroy_func_t destroy)
{
	struct io_watch **watch;

	if (!io)
		return false;

	switch (cond) {
	case G_IO_IN:
		watch = &io->read_watch;
		break;
	case G_IO_OUT:
		watch = &io->write_watch;
		break;
	case G_IO_HUP:
		watch = &io->disconnect_watch;
		break;
	case G_IO_PRI:
	case G_IO_ERR:
	case G_IO_NVAL:
	default:
		return false;
	}

	if (*watch) {
		g_source_remove((*watch)->id);
		*watch = NULL;
	}

	if (!callback)
		return true;

	*watch = watch_new(io, cond, callback, user_data, destroy);
	if (!*watch)
		return false;

	return true;
}

bool io_set_read_handler(struct io *io, io_callback_func_t callback,
				void *user_data, io_destroy_func_t destroy)
{
	return io_set_handler(io, G_IO_IN, callback, user_data, destroy);
}

bool io_set_write_handler(struct io *io, io_callback_func_t callback,
				void *user_data, io_destroy_func_t destroy)
{
	return io_set_handler(io, G_IO_OUT, callback, user_data, destroy);
}

bool io_set_disconnect_handler(struct io *io, io_callback_func_t callback,
				void *user_data, io_destroy_func_t destroy)
{
	return io_set_handler(io, G_IO_HUP, callback, user_data, destroy);
}

bool io_set_ignore_errqueue(struct io *io, bool do_ignore)
{
	if (!io)
		return false;

	io->err_watch = do_ignore;
	return true;
}

ssize_t io_send(struct io *io, const struct iovec *iov, int iovcnt)
{
	int fd;
	ssize_t ret;

	if (!io || !io->channel)
		return -ENOTCONN;

	fd = io_get_fd(io);

	do {
		ret = writev(fd, iov, iovcnt);
	} while (ret < 0 && errno == EINTR);

	if (ret < 0)
		return -errno;

	return ret;
}

bool io_shutdown(struct io *io)
{
	if (!io || !io->channel)
		return false;

	return g_io_channel_shutdown(io->channel, TRUE, NULL)
							== G_IO_STATUS_NORMAL;
}

/*
 * GSource implementation that tolerates non-empty MSG_ERRQUEUE, without
 * attempting to flush it. This is intended for use with TX timestamping in
 * cases where someone else is reading the timestamps and we are only interested
 * in POLLHUP or socket errors.
 */

static gint64 io_err_watch_wakeup;

static gboolean io_err_watch_dispatch(GSource *source,
				GSourceFunc callback, gpointer user_data)
{
	struct io_err_watch *watch = (void *)source;
	const GIOFunc func = (void *)callback;
	const gint64 timeout = IO_ERR_WATCH_RATELIMIT;
	GIOCondition cond;
	int fd;

	if (!func)
		return FALSE;

	fd = g_io_channel_unix_get_fd(watch->io);

	/*
	 * If woken up by POLLERR only, and SO_ERROR is not set, ignore this
	 * event. Also disable polling for some time so that we don't consume
	 * too much CPU on events we are not interested in, or busy loop if
	 * nobody is flushing the errqueue.
	 */

	if (watch->tag)
		cond = g_source_query_unix_fd(&watch->source, watch->tag);
	else
		cond = 0;

	if (cond == G_IO_ERR) {
		int err, ret;
		socklen_t len = sizeof(err);

		ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len);
		if (ret == 0 && err == 0) {
			g_source_remove_unix_fd(&watch->source, watch->tag);
			watch->tag = NULL;

			/* io_err watches all wake up at the same time */
			if (!io_err_watch_wakeup)
				io_err_watch_wakeup = g_get_monotonic_time()
								+ timeout;

			g_source_set_ready_time(&watch->source,
							io_err_watch_wakeup);
			return TRUE;
		}
	}

	if (g_source_get_ready_time(&watch->source) != -1) {
		g_assert(!watch->tag);
		io_err_watch_wakeup = 0;
		watch->tag = g_source_add_unix_fd(&watch->source, fd,
							watch->events);
		g_source_set_ready_time(&watch->source, -1);
	}

	cond &= watch->events;

	if (cond)
		return func(watch->io, cond, user_data);
	else
		return TRUE;
}

static void io_err_watch_finalize(GSource *source)
{
	struct io_err_watch *watch = (void *)source;

	if (watch->tag)
		g_source_remove_unix_fd(&watch->source, watch->tag);

	g_io_channel_unref(watch->io);
}

static guint io_glib_add_err_watch_full(GIOChannel *io, gint priority,
					GIOCondition events,
					GIOFunc func, gpointer user_data,
					GDestroyNotify notify)
{
	static GSourceFuncs source_funcs = {
		.dispatch = io_err_watch_dispatch,
		.finalize = io_err_watch_finalize,
	};
	GSourceFunc callback = (void *)func;
	struct io_err_watch *watch;
	gint fd;
	guint id;

	g_return_val_if_fail(!(events & (G_IO_IN | G_IO_OUT)), 0);
	g_return_val_if_fail(events, 0);
	g_return_val_if_fail(func, 0);

	fd = g_io_channel_unix_get_fd(io);

	watch = (void *)g_source_new(&source_funcs,
					sizeof(struct io_err_watch));

	watch->io = g_io_channel_ref(io);
	watch->events = events;
	watch->tag = g_source_add_unix_fd(&watch->source, fd, events);

	g_source_set_name((void *)watch, "io_glib_err_watch");
	g_source_set_callback(&watch->source, callback, user_data, notify);

	if (priority != G_PRIORITY_DEFAULT)
		g_source_set_priority(&watch->source, priority);

	id = g_source_attach(&watch->source, NULL);
	g_source_unref(&watch->source);

	return id;
}

struct err_watch_cb_data {
	io_glib_err_func_t func;
	void *data;
};

static gboolean err_watch_callback(GIOChannel *channel, GIOCondition cond,
							gpointer user_data)
{
	struct err_watch_cb_data *data = user_data;

	data->func(cond, data->data);
	return FALSE;
}

unsigned int io_glib_add_err_watch(void *giochannel,
						io_glib_err_func_t func,
						void *user_data)
{
	struct err_watch_cb_data *data;

	data = g_try_new0(struct err_watch_cb_data, 1);
	if (!data)
		return 0;

	data->func = func;
	data->data = user_data;
	return io_glib_add_err_watch_full(giochannel, G_PRIORITY_DEFAULT,
					G_IO_ERR | G_IO_HUP | G_IO_NVAL,
					err_watch_callback, data, g_free);
}