acba2c7712d730ca23ecee3a18a4fddee6bbd57e
[jra/samba/.git] / lib / 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    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "replace.h"
23 #include "tevent.h"
24 #include "tevent_internal.h"
25 #include "tevent_util.h"
26
27 int tevent_common_fd_destructor(struct tevent_fd *fde)
28 {
29         if (fde->event_ctx) {
30                 DLIST_REMOVE(fde->event_ctx->fd_events, fde);
31         }
32
33         if (fde->close_fn) {
34                 fde->close_fn(fde->event_ctx, fde, fde->fd, fde->private_data);
35                 fde->fd = -1;
36         }
37
38         return 0;
39 }
40
41 struct tevent_fd *tevent_common_add_fd(struct tevent_context *ev, TALLOC_CTX *mem_ctx,
42                                        int fd, uint16_t flags,
43                                        tevent_fd_handler_t handler,
44                                        void *private_data,
45                                        const char *handler_name,
46                                        const char *location)
47 {
48         struct tevent_fd *fde;
49
50         fde = talloc(mem_ctx?mem_ctx:ev, struct tevent_fd);
51         if (!fde) return NULL;
52
53         fde->event_ctx          = ev;
54         fde->fd                 = fd;
55         fde->flags              = flags;
56         fde->handler            = handler;
57         fde->close_fn           = NULL;
58         fde->private_data       = private_data;
59         fde->handler_name       = handler_name;
60         fde->location           = location;
61         fde->additional_flags   = 0;
62         fde->additional_data    = NULL;
63
64         DLIST_ADD(ev->fd_events, fde);
65
66         talloc_set_destructor(fde, tevent_common_fd_destructor);
67
68         return fde;
69 }
70 uint16_t tevent_common_fd_get_flags(struct tevent_fd *fde)
71 {
72         return fde->flags;
73 }
74
75 void tevent_common_fd_set_flags(struct tevent_fd *fde, uint16_t flags)
76 {
77         if (fde->flags == flags) return;
78         fde->flags = flags;
79 }
80
81 void tevent_common_fd_set_close_fn(struct tevent_fd *fde,
82                                    tevent_fd_close_fn_t close_fn)
83 {
84         fde->close_fn = close_fn;
85 }