2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Volker Lendecke 2005
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include <tevent_internal.h>
24 void event_fd_set_writeable(struct tevent_fd
*fde
)
26 TEVENT_FD_WRITEABLE(fde
);
29 void event_fd_set_not_writeable(struct tevent_fd
*fde
)
31 TEVENT_FD_NOT_WRITEABLE(fde
);
34 void event_fd_set_readable(struct tevent_fd
*fde
)
36 TEVENT_FD_READABLE(fde
);
39 void event_fd_set_not_readable(struct tevent_fd
*fde
)
41 TEVENT_FD_NOT_READABLE(fde
);
45 * Return if there's something in the queue
48 bool event_add_to_select_args(struct tevent_context
*ev
,
49 const struct timeval
*now
,
50 fd_set
*read_fds
, fd_set
*write_fds
,
51 struct timeval
*timeout
, int *maxfd
)
53 struct tevent_fd
*fde
;
57 for (fde
= ev
->fd_events
; fde
; fde
= fde
->next
) {
58 if (fde
->flags
& EVENT_FD_READ
) {
59 FD_SET(fde
->fd
, read_fds
);
62 if (fde
->flags
& EVENT_FD_WRITE
) {
63 FD_SET(fde
->fd
, write_fds
);
67 if ((fde
->flags
& (EVENT_FD_READ
|EVENT_FD_WRITE
))
68 && (fde
->fd
> *maxfd
)) {
73 if (ev
->immediate_events
!= NULL
) {
74 *timeout
= timeval_zero();
78 if (ev
->timer_events
== NULL
) {
82 diff
= timeval_until(now
, &ev
->timer_events
->next_event
);
83 *timeout
= timeval_min(timeout
, &diff
);
88 bool run_events(struct tevent_context
*ev
,
89 int selrtn
, fd_set
*read_fds
, fd_set
*write_fds
)
91 struct tevent_fd
*fde
;
94 if (ev
->signal_events
&&
95 tevent_common_check_signal(ev
)) {
99 if (ev
->immediate_events
&&
100 tevent_common_loop_immediate(ev
)) {
106 if ((ev
->timer_events
!= NULL
)
107 && (timeval_compare(&now
, &ev
->timer_events
->next_event
) >= 0)) {
109 DEBUG(10, ("Running timed event \"%s\" %p\n",
110 ev
->timer_events
->handler_name
, ev
->timer_events
));
112 ev
->timer_events
->handler(ev
, ev
->timer_events
, now
,
113 ev
->timer_events
->private_data
);
124 for (fde
= ev
->fd_events
; fde
; fde
= fde
->next
) {
127 if (FD_ISSET(fde
->fd
, read_fds
)) flags
|= EVENT_FD_READ
;
128 if (FD_ISSET(fde
->fd
, write_fds
)) flags
|= EVENT_FD_WRITE
;
130 if (flags
& fde
->flags
) {
131 fde
->handler(ev
, fde
, flags
, fde
->private_data
);
140 struct timeval
*get_timed_events_timeout(struct tevent_context
*ev
,
141 struct timeval
*to_ret
)
145 if ((ev
->timer_events
== NULL
) && (ev
->immediate_events
== NULL
)) {
148 if (ev
->immediate_events
!= NULL
) {
149 *to_ret
= timeval_zero();
153 now
= timeval_current();
154 *to_ret
= timeval_until(&now
, &ev
->timer_events
->next_event
);
156 DEBUG(10, ("timed_events_timeout: %d/%d\n", (int)to_ret
->tv_sec
,
157 (int)to_ret
->tv_usec
));
162 static int s3_event_loop_once(struct tevent_context
*ev
, const char *location
)
164 struct timeval now
, to
;
172 to
.tv_sec
= 9999; /* Max timeout */
175 if (run_events(ev
, 0, NULL
, NULL
)) {
181 if (!event_add_to_select_args(ev
, &now
, &r_fds
, &w_fds
, &to
, &maxfd
)) {
185 ret
= sys_select(maxfd
+1, &r_fds
, &w_fds
, NULL
, &to
);
187 if (ret
== -1 && errno
!= EINTR
) {
188 tevent_debug(ev
, TEVENT_DEBUG_FATAL
,
189 "sys_select() failed: %d:%s\n",
190 errno
, strerror(errno
));
194 run_events(ev
, ret
, &r_fds
, &w_fds
);
198 void event_context_reinit(struct tevent_context
*ev
)
200 tevent_common_context_destructor(ev
);
204 static int s3_event_context_init(struct tevent_context
*ev
)
209 void dump_event_list(struct tevent_context
*ev
)
211 struct tevent_timer
*te
;
212 struct tevent_fd
*fe
;
213 struct timeval evt
, now
;
219 now
= timeval_current();
221 DEBUG(10,("dump_event_list:\n"));
223 for (te
= ev
->timer_events
; te
; te
= te
->next
) {
225 evt
= timeval_until(&now
, &te
->next_event
);
227 DEBUGADD(10,("Timed Event \"%s\" %p handled in %d seconds (at %s)\n",
231 http_timestring(talloc_tos(), te
->next_event
.tv_sec
)));
234 for (fe
= ev
->fd_events
; fe
; fe
= fe
->next
) {
236 DEBUGADD(10,("FD Event %d %p, flags: 0x%04x\n",
243 static const struct tevent_ops s3_event_ops
= {
244 .context_init
= s3_event_context_init
,
245 .add_fd
= tevent_common_add_fd
,
246 .set_fd_close_fn
= tevent_common_fd_set_close_fn
,
247 .get_fd_flags
= tevent_common_fd_get_flags
,
248 .set_fd_flags
= tevent_common_fd_set_flags
,
249 .add_timer
= tevent_common_add_timer
,
250 .schedule_immediate
= tevent_common_schedule_immediate
,
251 .add_signal
= tevent_common_add_signal
,
252 .loop_once
= s3_event_loop_once
,
253 .loop_wait
= tevent_common_loop_wait
,
256 static bool s3_tevent_init(void)
258 static bool initialized
;
262 initialized
= tevent_register_backend("s3", &s3_event_ops
);
263 tevent_set_default_backend("s3");
268 this is used to catch debug messages from events
270 static void s3_event_debug(void *context
, enum tevent_debug_level level
,
271 const char *fmt
, va_list ap
) PRINTF_ATTRIBUTE(3,0);
273 static void s3_event_debug(void *context
, enum tevent_debug_level level
,
274 const char *fmt
, va_list ap
)
276 int samba_level
= -1;
279 case TEVENT_DEBUG_FATAL
:
282 case TEVENT_DEBUG_ERROR
:
285 case TEVENT_DEBUG_WARNING
:
288 case TEVENT_DEBUG_TRACE
:
293 if (vasprintf(&s
, fmt
, ap
) == -1) {
296 DEBUG(samba_level
, ("s3_event: %s", s
));
300 struct tevent_context
*s3_tevent_context_init(TALLOC_CTX
*mem_ctx
)
302 struct tevent_context
*ev
;
306 ev
= tevent_context_init_byname(mem_ctx
, "s3");
308 tevent_set_debug(ev
, s3_event_debug
, NULL
);