]> git.ozlabs.org Git - ccan-lca-2011.git/blob - ccan/tevent/tevent_fd.c
lca2011: hacky import of tevent.
[ccan-lca-2011.git] / ccan / tevent / tevent_fd.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    common events code for fd events
5
6    Copyright (C) Stefan Metzmacher 2009
7
8      ** NOTE! The following LGPL license applies to the tevent
9      ** library. This does NOT imply that all of Samba is released
10      ** under the LGPL
11
12    This library is free software; you can redistribute it and/or
13    modify it under the terms of the GNU Lesser General Public
14    License as published by the Free Software Foundation; either
15    version 3 of the License, or (at your option) any later version.
16
17    This library is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20    Lesser General Public License for more details.
21
22    You should have received a copy of the GNU Lesser General Public
23    License along with this library; if not, see <http://www.gnu.org/licenses/>.
24 */
25
26 #include <ccan/tevent/tevent.h>
27 #include <ccan/tevent/tevent_internal.h>
28 #include <ccan/tevent/tevent_util.h>
29
30 int tevent_common_fd_destructor(struct tevent_fd *fde)
31 {
32         if (fde->event_ctx) {
33                 DLIST_REMOVE(fde->event_ctx->fd_events, fde);
34         }
35
36         if (fde->close_fn) {
37                 fde->close_fn(fde->event_ctx, fde, fde->fd, fde->private_data);
38                 fde->fd = -1;
39         }
40
41         return 0;
42 }
43
44 struct tevent_fd *tevent_common_add_fd(struct tevent_context *ev, TALLOC_CTX *mem_ctx,
45                                        int fd, uint16_t flags,
46                                        tevent_fd_handler_t handler,
47                                        void *private_data,
48                                        const char *handler_name,
49                                        const char *location)
50 {
51         struct tevent_fd *fde;
52
53         fde = talloc(mem_ctx?mem_ctx:ev, struct tevent_fd);
54         if (!fde) return NULL;
55
56         fde->event_ctx          = ev;
57         fde->fd                 = fd;
58         fde->flags              = flags;
59         fde->handler            = handler;
60         fde->close_fn           = NULL;
61         fde->private_data       = private_data;
62         fde->handler_name       = handler_name;
63         fde->location           = location;
64         fde->additional_flags   = 0;
65         fde->additional_data    = NULL;
66
67         DLIST_ADD(ev->fd_events, fde);
68
69         talloc_set_destructor(fde, tevent_common_fd_destructor);
70
71         return fde;
72 }
73 uint16_t tevent_common_fd_get_flags(struct tevent_fd *fde)
74 {
75         return fde->flags;
76 }
77
78 void tevent_common_fd_set_flags(struct tevent_fd *fde, uint16_t flags)
79 {
80         if (fde->flags == flags) return;
81         fde->flags = flags;
82 }
83
84 void tevent_common_fd_set_close_fn(struct tevent_fd *fde,
85                                    tevent_fd_close_fn_t close_fn)
86 {
87         fde->close_fn = close_fn;
88 }