diff options
author | Stefan Metzmacher <metze@samba.org> | 2009-01-03 11:27:00 +0100 |
---|---|---|
committer | Stefan Metzmacher <metze@samba.org> | 2009-01-03 19:58:51 +0100 |
commit | e45ed828d042acca09cb1ed5b6dd7a24eb5e7ffd (patch) | |
tree | 87cc211ffd1b8354299ab720446dec89e8b1c18a | |
parent | e928d863e2bb654d16ac9094e3406ef6a207c023 (diff) | |
download | samba-e45ed828d042acca09cb1ed5b6dd7a24eb5e7ffd.tar.gz samba-e45ed828d042acca09cb1ed5b6dd7a24eb5e7ffd.tar.bz2 samba-e45ed828d042acca09cb1ed5b6dd7a24eb5e7ffd.zip |
tevent: add tevent_fd_set_close_fn()
Let callers specify a close function
as an alternative to TEVENT_FD_AUTOCLOSE.
metze
-rw-r--r-- | lib/tevent/tevent.c | 10 | ||||
-rw-r--r-- | lib/tevent/tevent.h | 6 | ||||
-rw-r--r-- | lib/tevent/tevent_aio.c | 6 | ||||
-rw-r--r-- | lib/tevent/tevent_epoll.c | 6 | ||||
-rw-r--r-- | lib/tevent/tevent_fd.c | 6 | ||||
-rw-r--r-- | lib/tevent/tevent_internal.h | 7 | ||||
-rw-r--r-- | lib/tevent/tevent_select.c | 6 | ||||
-rw-r--r-- | lib/tevent/tevent_standard.c | 6 |
8 files changed, 48 insertions, 5 deletions
diff --git a/lib/tevent/tevent.c b/lib/tevent/tevent.c index 9319177646..93918cc14d 100644 --- a/lib/tevent/tevent.c +++ b/lib/tevent/tevent.c @@ -241,6 +241,16 @@ struct tevent_aio *_tevent_add_aio(struct tevent_context *ev, } /* + set a close function on the fd event +*/ +void tevent_fd_set_close_fn(struct tevent_fd *fde, + tevent_fd_close_fn_t close_fn) +{ + if (!fde) return; + fde->event_ctx->ops->set_fd_close_fn(fde, close_fn); +} + +/* return the fd event flags */ uint16_t tevent_fd_get_flags(struct tevent_fd *fde) diff --git a/lib/tevent/tevent.h b/lib/tevent/tevent.h index 3a1b6057fb..9d3b8aa39e 100644 --- a/lib/tevent/tevent.h +++ b/lib/tevent/tevent.h @@ -37,6 +37,10 @@ typedef void (*tevent_fd_handler_t)(struct tevent_context *ev, struct tevent_fd *fde, uint16_t flags, void *private_data); +typedef void (*tevent_fd_close_fn_t)(struct tevent_context *ev, + struct tevent_fd *fde, + int fd, + void *private_data); typedef void (*tevent_timer_handler_t)(struct tevent_context *ev, struct tevent_timer *te, struct timeval current_time, @@ -107,6 +111,8 @@ struct tevent_aio *_tevent_add_aio(struct tevent_context *ev, int tevent_loop_once(struct tevent_context *ev); int tevent_loop_wait(struct tevent_context *ev); +void tevent_fd_set_close_fn(struct tevent_fd *fde, + tevent_fd_close_fn_t close_fn); uint16_t tevent_fd_get_flags(struct tevent_fd *fde); void tevent_fd_set_flags(struct tevent_fd *fde, uint16_t flags); diff --git a/lib/tevent/tevent_aio.c b/lib/tevent/tevent_aio.c index bfe3f070db..b768b7906d 100644 --- a/lib/tevent/tevent_aio.c +++ b/lib/tevent/tevent_aio.c @@ -400,7 +400,10 @@ static int aio_event_fd_destructor(struct tevent_fd *fde) epoll_del_event(aio_ev, fde); - if (fde->flags & TEVENT_FD_AUTOCLOSE) { + if (fde->close_fn) { + fde->close_fn(ev, fde, fde->fd, fde->private_data); + fde->fd = -1; + } else if (fde->flags & TEVENT_FD_AUTOCLOSE) { close(fde->fd); fde->fd = -1; } @@ -551,6 +554,7 @@ static const struct tevent_ops aio_event_ops = { .context_init = aio_event_context_init, .add_fd = aio_event_add_fd, .add_aio = aio_event_add_aio, + .set_fd_close_fn= tevent_common_fd_set_close_fn, .get_fd_flags = tevent_common_fd_get_flags, .set_fd_flags = aio_event_set_fd_flags, .add_timer = tevent_common_add_timer, diff --git a/lib/tevent/tevent_epoll.c b/lib/tevent/tevent_epoll.c index 8fb662e168..de2d6847a4 100644 --- a/lib/tevent/tevent_epoll.c +++ b/lib/tevent/tevent_epoll.c @@ -358,7 +358,10 @@ static int epoll_event_fd_destructor(struct tevent_fd *fde) epoll_del_event(epoll_ev, fde); - if (fde->flags & TEVENT_FD_AUTOCLOSE) { + if (fde->close_fn) { + fde->close_fn(ev, fde, fde->fd, fde->private_data); + fde->fd = -1; + } else if (fde->flags & TEVENT_FD_AUTOCLOSE) { close(fde->fd); fde->fd = -1; } @@ -463,6 +466,7 @@ static int epoll_event_loop_wait(struct tevent_context *ev) static const struct tevent_ops epoll_event_ops = { .context_init = epoll_event_context_init, .add_fd = epoll_event_add_fd, + .set_fd_close_fn= tevent_common_fd_set_close_fn, .get_fd_flags = tevent_common_fd_get_flags, .set_fd_flags = epoll_event_set_fd_flags, .add_timer = tevent_common_add_timer, diff --git a/lib/tevent/tevent_fd.c b/lib/tevent/tevent_fd.c index eb9cdab531..d450e2168d 100644 --- a/lib/tevent/tevent_fd.c +++ b/lib/tevent/tevent_fd.c @@ -34,3 +34,9 @@ void tevent_common_fd_set_flags(struct tevent_fd *fde, uint16_t flags) if (fde->flags == flags) return; fde->flags = flags; } + +void tevent_common_fd_set_close_fn(struct tevent_fd *fde, + tevent_fd_close_fn_t close_fn) +{ + fde->close_fn = close_fn; +} diff --git a/lib/tevent/tevent_internal.h b/lib/tevent/tevent_internal.h index 6099387362..f29e9c62f5 100644 --- a/lib/tevent/tevent_internal.h +++ b/lib/tevent/tevent_internal.h @@ -33,6 +33,8 @@ struct tevent_ops { void *private_data, const char *handler_name, const char *location); + void (*set_fd_close_fn)(struct tevent_fd *fde, + tevent_fd_close_fn_t close_fn); uint16_t (*get_fd_flags)(struct tevent_fd *fde); void (*set_fd_flags)(struct tevent_fd *fde, uint16_t flags); @@ -70,8 +72,9 @@ struct tevent_fd { struct tevent_fd *prev, *next; struct tevent_context *event_ctx; int fd; - uint16_t flags; /* see EVENT_FD_* flags */ + uint16_t flags; /* see TEVENT_FD_* flags */ tevent_fd_handler_t handler; + tevent_fd_close_fn_t close_fn; /* this is private for the specific handler */ void *private_data; /* this is for debugging only! */ @@ -146,6 +149,8 @@ struct tevent_context { bool tevent_register_backend(const char *name, const struct tevent_ops *ops); +void tevent_common_fd_set_close_fn(struct tevent_fd *fde, + tevent_fd_close_fn_t close_fn); uint16_t tevent_common_fd_get_flags(struct tevent_fd *fde); void tevent_common_fd_set_flags(struct tevent_fd *fde, uint16_t flags); diff --git a/lib/tevent/tevent_select.c b/lib/tevent/tevent_select.c index f6790ab0f4..cca771ce85 100644 --- a/lib/tevent/tevent_select.c +++ b/lib/tevent/tevent_select.c @@ -103,7 +103,10 @@ static int select_event_fd_destructor(struct tevent_fd *fde) DLIST_REMOVE(select_ev->fd_events, fde); select_ev->destruction_count++; - if (fde->flags & TEVENT_FD_AUTOCLOSE) { + if (fde->close_fn) { + fde->close_fn(ev, fde, fde->fd, fde->private_data); + fde->fd = -1; + } else if (fde->flags & TEVENT_FD_AUTOCLOSE) { close(fde->fd); fde->fd = -1; } @@ -266,6 +269,7 @@ static int select_event_loop_wait(struct tevent_context *ev) static const struct tevent_ops select_event_ops = { .context_init = select_event_context_init, .add_fd = select_event_add_fd, + .set_fd_close_fn= tevent_common_fd_set_close_fn, .get_fd_flags = tevent_common_fd_get_flags, .set_fd_flags = tevent_common_fd_set_flags, .add_timer = tevent_common_add_timer, diff --git a/lib/tevent/tevent_standard.c b/lib/tevent/tevent_standard.c index 3a674152b1..5d73db55b0 100644 --- a/lib/tevent/tevent_standard.c +++ b/lib/tevent/tevent_standard.c @@ -392,7 +392,10 @@ static int std_event_fd_destructor(struct tevent_fd *fde) epoll_del_event(std_ev, fde); - if (fde->flags & TEVENT_FD_AUTOCLOSE) { + if (fde->close_fn) { + fde->close_fn(ev, fde, fde->fd, fde->private_data); + fde->fd = -1; + } else if (fde->flags & TEVENT_FD_AUTOCLOSE) { close(fde->fd); fde->fd = -1; } @@ -584,6 +587,7 @@ static int std_event_loop_wait(struct tevent_context *ev) static const struct tevent_ops std_event_ops = { .context_init = std_event_context_init, .add_fd = std_event_add_fd, + .set_fd_close_fn= tevent_common_fd_set_close_fn, .get_fd_flags = tevent_common_fd_get_flags, .set_fd_flags = std_event_set_fd_flags, .add_timer = tevent_common_add_timer, |