Blame src/shared/io-glib.c

Packit 34410b
/*
Packit 34410b
 *
Packit 34410b
 *  BlueZ - Bluetooth protocol stack for Linux
Packit 34410b
 *
Packit 34410b
 *  Copyright (C) 2012-2014  Intel Corporation. All rights reserved.
Packit 34410b
 *
Packit 34410b
 *
Packit 34410b
 *  This library is free software; you can redistribute it and/or
Packit 34410b
 *  modify it under the terms of the GNU Lesser General Public
Packit 34410b
 *  License as published by the Free Software Foundation; either
Packit 34410b
 *  version 2.1 of the License, or (at your option) any later version.
Packit 34410b
 *
Packit 34410b
 *  This library is distributed in the hope that it will be useful,
Packit 34410b
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 34410b
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 34410b
 *  Lesser General Public License for more details.
Packit 34410b
 *
Packit 34410b
 *  You should have received a copy of the GNU Lesser General Public
Packit 34410b
 *  License along with this library; if not, write to the Free Software
Packit 34410b
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
Packit 34410b
 *
Packit 34410b
 */
Packit 34410b
Packit 34410b
#ifdef HAVE_CONFIG_H
Packit 34410b
#include <config.h>
Packit 34410b
#endif
Packit 34410b
Packit 34410b
#include <errno.h>
Packit 34410b
Packit 34410b
#include <glib.h>
Packit 34410b
Packit 34410b
#include "src/shared/io.h"
Packit 34410b
Packit 34410b
struct io_watch {
Packit 34410b
	struct io *io;
Packit 34410b
	guint id;
Packit 34410b
	io_callback_func_t callback;
Packit 34410b
	io_destroy_func_t destroy;
Packit 34410b
	void *user_data;
Packit 34410b
};
Packit 34410b
Packit 34410b
struct io {
Packit 34410b
	int ref_count;
Packit 34410b
	GIOChannel *channel;
Packit 34410b
	struct io_watch *read_watch;
Packit 34410b
	struct io_watch *write_watch;
Packit 34410b
	struct io_watch *disconnect_watch;
Packit 34410b
};
Packit 34410b
Packit 34410b
static struct io *io_ref(struct io *io)
Packit 34410b
{
Packit 34410b
	if (!io)
Packit 34410b
		return NULL;
Packit 34410b
Packit 34410b
	__sync_fetch_and_add(&io->ref_count, 1);
Packit 34410b
Packit 34410b
	return io;
Packit 34410b
}
Packit 34410b
Packit 34410b
static void io_unref(struct io *io)
Packit 34410b
{
Packit 34410b
	if (!io)
Packit 34410b
		return;
Packit 34410b
Packit 34410b
	if (__sync_sub_and_fetch(&io->ref_count, 1))
Packit 34410b
		return;
Packit 34410b
Packit 34410b
	g_free(io);
Packit 34410b
}
Packit 34410b
Packit 34410b
struct io *io_new(int fd)
Packit 34410b
{
Packit 34410b
	struct io *io;
Packit 34410b
Packit 34410b
	if (fd < 0)
Packit 34410b
		return NULL;
Packit 34410b
Packit 34410b
	io = g_try_new0(struct io, 1);
Packit 34410b
	if (!io)
Packit 34410b
		return NULL;
Packit 34410b
Packit 34410b
	io->channel = g_io_channel_unix_new(fd);
Packit 34410b
Packit 34410b
	g_io_channel_set_encoding(io->channel, NULL, NULL);
Packit 34410b
	g_io_channel_set_buffered(io->channel, FALSE);
Packit 34410b
Packit 34410b
	g_io_channel_set_close_on_unref(io->channel, FALSE);
Packit 34410b
Packit 34410b
	return io_ref(io);
Packit 34410b
}
Packit 34410b
Packit 34410b
static void watch_destroy(void *user_data)
Packit 34410b
{
Packit 34410b
	struct io_watch *watch = user_data;
Packit 34410b
	struct io *io = watch->io;
Packit 34410b
Packit 34410b
	if (watch == io->read_watch)
Packit 34410b
		io->read_watch = NULL;
Packit 34410b
	else if (watch == io->write_watch)
Packit 34410b
		io->write_watch = NULL;
Packit 34410b
	else if (watch == io->disconnect_watch)
Packit 34410b
		io->disconnect_watch = NULL;
Packit 34410b
Packit 34410b
	if (watch->destroy)
Packit 34410b
		watch->destroy(watch->user_data);
Packit 34410b
Packit 34410b
	io_unref(watch->io);
Packit 34410b
	g_free(watch);
Packit 34410b
}
Packit 34410b
Packit 34410b
void io_destroy(struct io *io)
Packit 34410b
{
Packit 34410b
	if (!io)
Packit 34410b
		return;
Packit 34410b
Packit 34410b
	if (io->read_watch) {
Packit 34410b
		g_source_remove(io->read_watch->id);
Packit 34410b
		io->read_watch = NULL;
Packit 34410b
	}
Packit 34410b
Packit 34410b
	if (io->write_watch) {
Packit 34410b
		g_source_remove(io->write_watch->id);
Packit 34410b
		io->write_watch = NULL;
Packit 34410b
	}
Packit 34410b
Packit 34410b
	if (io->disconnect_watch) {
Packit 34410b
		g_source_remove(io->disconnect_watch->id);
Packit 34410b
		io->disconnect_watch = NULL;
Packit 34410b
	}
Packit 34410b
Packit 34410b
	g_io_channel_unref(io->channel);
Packit 34410b
	io->channel = NULL;
Packit 34410b
Packit 34410b
	io_unref(io);
Packit 34410b
}
Packit 34410b
Packit 34410b
int io_get_fd(struct io *io)
Packit 34410b
{
Packit 34410b
	if (!io)
Packit 34410b
		return -ENOTCONN;
Packit 34410b
Packit 34410b
	return g_io_channel_unix_get_fd(io->channel);
Packit 34410b
}
Packit 34410b
Packit 34410b
bool io_set_close_on_destroy(struct io *io, bool do_close)
Packit 34410b
{
Packit 34410b
	if (!io)
Packit 34410b
		return false;
Packit 34410b
Packit 34410b
	if (do_close)
Packit 34410b
		g_io_channel_set_close_on_unref(io->channel, TRUE);
Packit 34410b
	else
Packit 34410b
		g_io_channel_set_close_on_unref(io->channel, FALSE);
Packit 34410b
Packit 34410b
	return true;
Packit 34410b
}
Packit 34410b
Packit 34410b
static gboolean watch_callback(GIOChannel *channel, GIOCondition cond,
Packit 34410b
							gpointer user_data)
Packit 34410b
{
Packit 34410b
	struct io_watch *watch = user_data;
Packit 34410b
	bool result, destroy;
Packit 34410b
Packit 34410b
	destroy = watch == watch->io->disconnect_watch;
Packit 34410b
Packit 34410b
	if (!destroy && (cond & (G_IO_ERR | G_IO_NVAL)))
Packit 34410b
		return FALSE;
Packit 34410b
Packit 34410b
	if (watch->callback)
Packit 34410b
		result = watch->callback(watch->io, watch->user_data);
Packit 34410b
	else
Packit 34410b
		result = false;
Packit 34410b
Packit 34410b
	return result ? TRUE : FALSE;
Packit 34410b
}
Packit 34410b
Packit 34410b
static struct io_watch *watch_new(struct io *io, GIOCondition cond,
Packit 34410b
				io_callback_func_t callback, void *user_data,
Packit 34410b
				io_destroy_func_t destroy)
Packit 34410b
{
Packit 34410b
	struct io_watch *watch;
Packit 34410b
	int prio;
Packit 34410b
Packit 34410b
	watch = g_try_new0(struct io_watch, 1);
Packit 34410b
	if (!watch)
Packit 34410b
		return NULL;
Packit 34410b
Packit 34410b
	watch->io = io_ref(io);
Packit 34410b
	watch->callback = callback;
Packit 34410b
	watch->destroy = destroy;
Packit 34410b
	watch->user_data = user_data;
Packit 34410b
Packit 34410b
	prio = cond == G_IO_HUP ? G_PRIORITY_DEFAULT_IDLE : G_PRIORITY_DEFAULT;
Packit 34410b
Packit 34410b
	watch->id = g_io_add_watch_full(io->channel, prio,
Packit 34410b
						cond | G_IO_ERR | G_IO_NVAL,
Packit 34410b
						watch_callback, watch,
Packit 34410b
						watch_destroy);
Packit 34410b
	if (watch->id == 0) {
Packit 34410b
		watch_destroy(watch);
Packit 34410b
		return NULL;
Packit 34410b
	}
Packit 34410b
Packit 34410b
	return watch;
Packit 34410b
}
Packit 34410b
Packit 34410b
static bool io_set_handler(struct io *io, GIOCondition cond,
Packit 34410b
				io_callback_func_t callback, void *user_data,
Packit 34410b
				io_destroy_func_t destroy)
Packit 34410b
{
Packit 34410b
	struct io_watch **watch;
Packit 34410b
Packit 34410b
	if (!io)
Packit 34410b
		return false;
Packit 34410b
Packit 34410b
	switch (cond) {
Packit 34410b
	case G_IO_IN:
Packit 34410b
		watch = &io->read_watch;
Packit 34410b
		break;
Packit 34410b
	case G_IO_OUT:
Packit 34410b
		watch = &io->write_watch;
Packit 34410b
		break;
Packit 34410b
	case G_IO_HUP:
Packit 34410b
		watch = &io->disconnect_watch;
Packit 34410b
		break;
Packit 34410b
	case G_IO_PRI:
Packit 34410b
	case G_IO_ERR:
Packit 34410b
	case G_IO_NVAL:
Packit 34410b
	default:
Packit 34410b
		return false;
Packit 34410b
	}
Packit 34410b
Packit 34410b
	if (*watch) {
Packit 34410b
		g_source_remove((*watch)->id);
Packit 34410b
		*watch = NULL;
Packit 34410b
	}
Packit 34410b
Packit 34410b
	if (!callback)
Packit 34410b
		return true;
Packit 34410b
Packit 34410b
	*watch = watch_new(io, cond, callback, user_data, destroy);
Packit 34410b
	if (!*watch)
Packit 34410b
		return false;
Packit 34410b
Packit 34410b
	return true;
Packit 34410b
}
Packit 34410b
Packit 34410b
bool io_set_read_handler(struct io *io, io_callback_func_t callback,
Packit 34410b
				void *user_data, io_destroy_func_t destroy)
Packit 34410b
{
Packit 34410b
	return io_set_handler(io, G_IO_IN, callback, user_data, destroy);
Packit 34410b
}
Packit 34410b
Packit 34410b
bool io_set_write_handler(struct io *io, io_callback_func_t callback,
Packit 34410b
				void *user_data, io_destroy_func_t destroy)
Packit 34410b
{
Packit 34410b
	return io_set_handler(io, G_IO_OUT, callback, user_data, destroy);
Packit 34410b
}
Packit 34410b
Packit 34410b
bool io_set_disconnect_handler(struct io *io, io_callback_func_t callback,
Packit 34410b
				void *user_data, io_destroy_func_t destroy)
Packit 34410b
{
Packit 34410b
	return io_set_handler(io, G_IO_HUP, callback, user_data, destroy);
Packit 34410b
}
Packit 34410b
Packit 34410b
ssize_t io_send(struct io *io, const struct iovec *iov, int iovcnt)
Packit 34410b
{
Packit 34410b
	int fd;
Packit 34410b
	ssize_t ret;
Packit 34410b
Packit 34410b
	if (!io || !io->channel)
Packit 34410b
		return -ENOTCONN;
Packit 34410b
Packit 34410b
	fd = io_get_fd(io);
Packit 34410b
Packit 34410b
	do {
Packit 34410b
		ret = writev(fd, iov, iovcnt);
Packit 34410b
	} while (ret < 0 && errno == EINTR);
Packit 34410b
Packit 34410b
	if (ret < 0)
Packit 34410b
		return -errno;
Packit 34410b
Packit 34410b
	return ret;
Packit 34410b
}
Packit 34410b
Packit 34410b
bool io_shutdown(struct io *io)
Packit 34410b
{
Packit 34410b
	if (!io || !io->channel)
Packit 34410b
		return false;
Packit 34410b
Packit 34410b
	return g_io_channel_shutdown(io->channel, TRUE, NULL)
Packit 34410b
							== G_IO_STATUS_NORMAL;
Packit 34410b
}