[stgt] [PATCH 1/3] add tgt_event_* helper functions

FUJITA Tomonori fujita.tomonori at lab.ntt.co.jp
Thu Jun 10 03:19:27 CEST 2010


This enables us to handle events for non main process.

Signed-off-by: FUJITA Tomonori <fujita.tomonori at lab.ntt.co.jp>
---
 usr/tgtd.c |   39 ++++++++++++++++++++++++++++-----------
 usr/tgtd.h |    5 +++++
 2 files changed, 33 insertions(+), 11 deletions(-)

diff --git a/usr/tgtd.c b/usr/tgtd.c
index f0a158f..18ad7be 100644
--- a/usr/tgtd.c
+++ b/usr/tgtd.c
@@ -135,7 +135,8 @@ set_rlimit:
 	return 0;
 }
 
-int tgt_event_add(int fd, int events, event_handler_t handler, void *data)
+int do_tgt_event_add(int efd, struct list_head *list, int fd, int events,
+		     event_handler_t handler, void *data)
 {
 	struct epoll_event ev;
 	struct event_data *tev;
@@ -152,39 +153,45 @@ int tgt_event_add(int fd, int events, event_handler_t handler, void *data)
 	memset(&ev, 0, sizeof(ev));
 	ev.events = events;
 	ev.data.ptr = tev;
-	err = epoll_ctl(ep_fd, EPOLL_CTL_ADD, fd, &ev);
+	err = epoll_ctl(efd, EPOLL_CTL_ADD, fd, &ev);
 	if (err) {
 		eprintf("Cannot add fd, %m\n");
 		free(tev);
 	} else
-		list_add(&tev->e_list, &tgt_events_list);
+		list_add(&tev->e_list, list);
 
 	return err;
 }
 
-static struct event_data *tgt_event_lookup(int fd)
+int tgt_event_add(int fd, int events, event_handler_t handler, void *data)
+{
+	return do_tgt_event_add(ep_fd, &tgt_events_list, fd, events, handler,
+				data);
+}
+
+static struct event_data *tgt_event_lookup(struct list_head *list, int fd)
 {
 	struct event_data *tev;
 
-	list_for_each_entry(tev, &tgt_events_list, e_list) {
+	list_for_each_entry(tev, list, e_list) {
 		if (tev->fd == fd)
 			return tev;
 	}
 	return NULL;
 }
 
-void tgt_event_del(int fd)
+void do_tgt_event_del(int efd, struct list_head *list, int fd)
 {
 	struct event_data *tev;
 	int ret;
 
-	tev = tgt_event_lookup(fd);
+	tev = tgt_event_lookup(list, fd);
 	if (!tev) {
 		eprintf("Cannot find event %d\n", fd);
 		return;
 	}
 
-	ret = epoll_ctl(ep_fd, EPOLL_CTL_DEL, fd, NULL);
+	ret = epoll_ctl(efd, EPOLL_CTL_DEL, fd, NULL);
 	if (ret < 0)
 		eprintf("fail to remove epoll event, %s\n", strerror(errno));
 
@@ -192,12 +199,17 @@ void tgt_event_del(int fd)
 	free(tev);
 }
 
-int tgt_event_modify(int fd, int events)
+void tgt_event_del(int fd)
+{
+	do_tgt_event_del(ep_fd, &tgt_events_list, fd);
+}
+
+int do_tgt_event_modify(int efd, struct list_head *list, int fd, int events)
 {
 	struct epoll_event ev;
 	struct event_data *tev;
 
-	tev = tgt_event_lookup(fd);
+	tev = tgt_event_lookup(list, fd);
 	if (!tev) {
 		eprintf("Cannot find event %d\n", fd);
 		return -EINVAL;
@@ -207,7 +219,12 @@ int tgt_event_modify(int fd, int events)
 	ev.events = events;
 	ev.data.ptr = tev;
 
-	return epoll_ctl(ep_fd, EPOLL_CTL_MOD, fd, &ev);
+	return epoll_ctl(efd, EPOLL_CTL_MOD, fd, &ev);
+}
+
+int tgt_event_modify(int fd, int events)
+{
+	return do_tgt_event_modify(ep_fd, &tgt_events_list, fd, events);
 }
 
 void tgt_init_sched_event(struct event_data *evt,
diff --git a/usr/tgtd.h b/usr/tgtd.h
index 158731b..8295bb3 100644
--- a/usr/tgtd.h
+++ b/usr/tgtd.h
@@ -332,4 +332,9 @@ struct event_data {
 	struct list_head e_list;
 };
 
+int do_tgt_event_add(int efd, struct list_head *list, int fd, int events,
+		     event_handler_t handler, void *data);
+void do_tgt_event_del(int efd, struct list_head *list, int fd);
+int do_tgt_event_modify(int efd, struct list_head *list, int fd, int events);
+
 #endif
-- 
1.6.5

--
To unsubscribe from this list: send the line "unsubscribe stgt" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html



More information about the stgt mailing list