| 1 | /*
|
|---|
| 2 | Unix SMB/CIFS implementation.
|
|---|
| 3 | Timed event library.
|
|---|
| 4 | Copyright (C) Andrew Tridgell 1992-1998
|
|---|
| 5 | Copyright (C) Volker Lendecke 2005
|
|---|
| 6 |
|
|---|
| 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.
|
|---|
| 11 |
|
|---|
| 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.
|
|---|
| 16 |
|
|---|
| 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/>.
|
|---|
| 19 | */
|
|---|
| 20 |
|
|---|
| 21 | #include "includes.h"
|
|---|
| 22 |
|
|---|
| 23 | struct timed_event {
|
|---|
| 24 | struct timed_event *next, *prev;
|
|---|
| 25 | struct event_context *event_ctx;
|
|---|
| 26 | struct timeval when;
|
|---|
| 27 | const char *event_name;
|
|---|
| 28 | void (*handler)(struct event_context *event_ctx,
|
|---|
| 29 | struct timed_event *te,
|
|---|
| 30 | struct timeval now,
|
|---|
| 31 | void *private_data);
|
|---|
| 32 | void *private_data;
|
|---|
| 33 | };
|
|---|
| 34 |
|
|---|
| 35 | struct fd_event {
|
|---|
| 36 | struct fd_event *prev, *next;
|
|---|
| 37 | struct event_context *event_ctx;
|
|---|
| 38 | int fd;
|
|---|
| 39 | uint16_t flags; /* see EVENT_FD_* flags */
|
|---|
| 40 | void (*handler)(struct event_context *event_ctx,
|
|---|
| 41 | struct fd_event *event,
|
|---|
| 42 | uint16 flags,
|
|---|
| 43 | void *private_data);
|
|---|
| 44 | void *private_data;
|
|---|
| 45 | };
|
|---|
| 46 |
|
|---|
| 47 | struct event_context {
|
|---|
| 48 | struct timed_event *timed_events;
|
|---|
| 49 | struct fd_event *fd_events;
|
|---|
| 50 | };
|
|---|
| 51 |
|
|---|
| 52 | static int timed_event_destructor(struct timed_event *te)
|
|---|
| 53 | {
|
|---|
| 54 | DEBUG(10, ("Destroying timed event %lx \"%s\"\n", (unsigned long)te,
|
|---|
| 55 | te->event_name));
|
|---|
| 56 | if (te->event_ctx != NULL) {
|
|---|
| 57 | DLIST_REMOVE(te->event_ctx->timed_events, te);
|
|---|
| 58 | }
|
|---|
| 59 | return 0;
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | /****************************************************************************
|
|---|
| 63 | Add te by time.
|
|---|
| 64 | ****************************************************************************/
|
|---|
| 65 |
|
|---|
| 66 | static void add_event_by_time(struct timed_event *te)
|
|---|
| 67 | {
|
|---|
| 68 | struct event_context *ctx = te->event_ctx;
|
|---|
| 69 | struct timed_event *last_te, *cur_te;
|
|---|
| 70 |
|
|---|
| 71 | /* Keep the list ordered by time. We must preserve this. */
|
|---|
| 72 | last_te = NULL;
|
|---|
| 73 | for (cur_te = ctx->timed_events; cur_te; cur_te = cur_te->next) {
|
|---|
| 74 | /* if the new event comes before the current one break */
|
|---|
| 75 | if (!timeval_is_zero(&cur_te->when) &&
|
|---|
| 76 | timeval_compare(&te->when, &cur_te->when) < 0) {
|
|---|
| 77 | break;
|
|---|
| 78 | }
|
|---|
| 79 | last_te = cur_te;
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | DLIST_ADD_AFTER(ctx->timed_events, te, last_te);
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | /****************************************************************************
|
|---|
| 86 | Schedule a function for future calling, cancel with TALLOC_FREE().
|
|---|
| 87 | It's the responsibility of the handler to call TALLOC_FREE() on the event
|
|---|
| 88 | handed to it.
|
|---|
| 89 | ****************************************************************************/
|
|---|
| 90 |
|
|---|
| 91 | struct timed_event *_event_add_timed(struct event_context *event_ctx,
|
|---|
| 92 | TALLOC_CTX *mem_ctx,
|
|---|
| 93 | struct timeval when,
|
|---|
| 94 | const char *event_name,
|
|---|
| 95 | void (*handler)(struct event_context *event_ctx,
|
|---|
| 96 | struct timed_event *te,
|
|---|
| 97 | struct timeval now,
|
|---|
| 98 | void *private_data),
|
|---|
| 99 | void *private_data)
|
|---|
| 100 | {
|
|---|
| 101 | struct timed_event *te;
|
|---|
| 102 |
|
|---|
| 103 | te = TALLOC_P(mem_ctx, struct timed_event);
|
|---|
| 104 | if (te == NULL) {
|
|---|
| 105 | DEBUG(0, ("talloc failed\n"));
|
|---|
| 106 | return NULL;
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | te->event_ctx = event_ctx;
|
|---|
| 110 | te->when = when;
|
|---|
| 111 | te->event_name = event_name;
|
|---|
| 112 | te->handler = handler;
|
|---|
| 113 | te->private_data = private_data;
|
|---|
| 114 |
|
|---|
| 115 | add_event_by_time(te);
|
|---|
| 116 |
|
|---|
| 117 | talloc_set_destructor(te, timed_event_destructor);
|
|---|
| 118 |
|
|---|
| 119 | DEBUG(10, ("Added timed event \"%s\": %lx\n", event_name,
|
|---|
| 120 | (unsigned long)te));
|
|---|
| 121 | return te;
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | static int fd_event_destructor(struct fd_event *fde)
|
|---|
| 125 | {
|
|---|
| 126 | if (fde->event_ctx != NULL) {
|
|---|
| 127 | DLIST_REMOVE(fde->event_ctx->fd_events, fde);
|
|---|
| 128 | }
|
|---|
| 129 | return 0;
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | struct fd_event *event_add_fd(struct event_context *event_ctx,
|
|---|
| 133 | TALLOC_CTX *mem_ctx,
|
|---|
| 134 | int fd, uint16_t flags,
|
|---|
| 135 | void (*handler)(struct event_context *event_ctx,
|
|---|
| 136 | struct fd_event *event,
|
|---|
| 137 | uint16 flags,
|
|---|
| 138 | void *private_data),
|
|---|
| 139 | void *private_data)
|
|---|
| 140 | {
|
|---|
| 141 | struct fd_event *fde;
|
|---|
| 142 |
|
|---|
| 143 | if (fd < 0 || fd >= FD_SETSIZE) {
|
|---|
| 144 | errno = EBADF;
|
|---|
| 145 | return NULL;
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | if (!(fde = TALLOC_P(mem_ctx, struct fd_event))) {
|
|---|
| 149 | return NULL;
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | fde->event_ctx = event_ctx;
|
|---|
| 153 | fde->fd = fd;
|
|---|
| 154 | fde->flags = flags;
|
|---|
| 155 | fde->handler = handler;
|
|---|
| 156 | fde->private_data = private_data;
|
|---|
| 157 |
|
|---|
| 158 | DLIST_ADD(event_ctx->fd_events, fde);
|
|---|
| 159 |
|
|---|
| 160 | talloc_set_destructor(fde, fd_event_destructor);
|
|---|
| 161 | return fde;
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | void event_fd_set_writeable(struct fd_event *fde)
|
|---|
| 165 | {
|
|---|
| 166 | fde->flags |= EVENT_FD_WRITE;
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | void event_fd_set_not_writeable(struct fd_event *fde)
|
|---|
| 170 | {
|
|---|
| 171 | fde->flags &= ~EVENT_FD_WRITE;
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | void event_fd_set_readable(struct fd_event *fde)
|
|---|
| 175 | {
|
|---|
| 176 | fde->flags |= EVENT_FD_READ;
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | void event_fd_set_not_readable(struct fd_event *fde)
|
|---|
| 180 | {
|
|---|
| 181 | fde->flags &= ~EVENT_FD_READ;
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | /*
|
|---|
| 185 | * Return if there's something in the queue
|
|---|
| 186 | */
|
|---|
| 187 |
|
|---|
| 188 | bool event_add_to_select_args(struct event_context *event_ctx,
|
|---|
| 189 | const struct timeval *now,
|
|---|
| 190 | fd_set *read_fds, fd_set *write_fds,
|
|---|
| 191 | struct timeval *timeout, int *maxfd)
|
|---|
| 192 | {
|
|---|
| 193 | struct fd_event *fde;
|
|---|
| 194 | struct timeval diff;
|
|---|
| 195 | bool ret = False;
|
|---|
| 196 |
|
|---|
| 197 | for (fde = event_ctx->fd_events; fde; fde = fde->next) {
|
|---|
| 198 | if (fde->fd < 0 || fde->fd >= FD_SETSIZE) {
|
|---|
| 199 | /* We ignore here, as it shouldn't be
|
|---|
| 200 | possible to add an invalid fde->fd
|
|---|
| 201 | but we don't want FD_SET to see an
|
|---|
| 202 | invalid fd. */
|
|---|
| 203 | continue;
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | if (fde->flags & EVENT_FD_READ) {
|
|---|
| 207 | FD_SET(fde->fd, read_fds);
|
|---|
| 208 | ret = True;
|
|---|
| 209 | }
|
|---|
| 210 | if (fde->flags & EVENT_FD_WRITE) {
|
|---|
| 211 | FD_SET(fde->fd, write_fds);
|
|---|
| 212 | ret = True;
|
|---|
| 213 | }
|
|---|
| 214 |
|
|---|
| 215 | if ((fde->flags & (EVENT_FD_READ|EVENT_FD_WRITE))
|
|---|
| 216 | && (fde->fd > *maxfd)) {
|
|---|
| 217 | *maxfd = fde->fd;
|
|---|
| 218 | }
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | if (event_ctx->timed_events == NULL) {
|
|---|
| 222 | return ret;
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| 225 | diff = timeval_until(now, &event_ctx->timed_events->when);
|
|---|
| 226 | *timeout = timeval_min(timeout, &diff);
|
|---|
| 227 |
|
|---|
| 228 | return True;
|
|---|
| 229 | }
|
|---|
| 230 |
|
|---|
| 231 | bool run_events(struct event_context *event_ctx,
|
|---|
| 232 | int selrtn, fd_set *read_fds, fd_set *write_fds)
|
|---|
| 233 | {
|
|---|
| 234 | struct fd_event *fde;
|
|---|
| 235 | struct timeval now;
|
|---|
| 236 |
|
|---|
| 237 | GetTimeOfDay(&now);
|
|---|
| 238 |
|
|---|
| 239 | if ((event_ctx->timed_events != NULL)
|
|---|
| 240 | && (timeval_compare(&now, &event_ctx->timed_events->when) >= 0)) {
|
|---|
| 241 |
|
|---|
| 242 | DEBUG(10, ("Running event \"%s\" %lx\n",
|
|---|
| 243 | event_ctx->timed_events->event_name,
|
|---|
| 244 | (unsigned long)event_ctx->timed_events));
|
|---|
| 245 |
|
|---|
| 246 | event_ctx->timed_events->handler(
|
|---|
| 247 | event_ctx,
|
|---|
| 248 | event_ctx->timed_events, now,
|
|---|
| 249 | event_ctx->timed_events->private_data);
|
|---|
| 250 |
|
|---|
| 251 | return true;
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 | if (selrtn == 0) {
|
|---|
| 255 | /*
|
|---|
| 256 | * No fd ready
|
|---|
| 257 | */
|
|---|
| 258 | return false;
|
|---|
| 259 | }
|
|---|
| 260 |
|
|---|
| 261 | for (fde = event_ctx->fd_events; fde; fde = fde->next) {
|
|---|
| 262 | uint16 flags = 0;
|
|---|
| 263 |
|
|---|
| 264 | if (FD_ISSET(fde->fd, read_fds)) flags |= EVENT_FD_READ;
|
|---|
| 265 | if (FD_ISSET(fde->fd, write_fds)) flags |= EVENT_FD_WRITE;
|
|---|
| 266 |
|
|---|
| 267 | if (flags & fde->flags) {
|
|---|
| 268 | fde->handler(event_ctx, fde, flags, fde->private_data);
|
|---|
| 269 | return true;
|
|---|
| 270 | }
|
|---|
| 271 | }
|
|---|
| 272 |
|
|---|
| 273 | return false;
|
|---|
| 274 | }
|
|---|
| 275 |
|
|---|
| 276 |
|
|---|
| 277 | struct timeval *get_timed_events_timeout(struct event_context *event_ctx,
|
|---|
| 278 | struct timeval *to_ret)
|
|---|
| 279 | {
|
|---|
| 280 | struct timeval now;
|
|---|
| 281 |
|
|---|
| 282 | if (event_ctx->timed_events == NULL) {
|
|---|
| 283 | return NULL;
|
|---|
| 284 | }
|
|---|
| 285 |
|
|---|
| 286 | now = timeval_current();
|
|---|
| 287 | *to_ret = timeval_until(&now, &event_ctx->timed_events->when);
|
|---|
| 288 |
|
|---|
| 289 | DEBUG(10, ("timed_events_timeout: %d/%d\n", (int)to_ret->tv_sec,
|
|---|
| 290 | (int)to_ret->tv_usec));
|
|---|
| 291 |
|
|---|
| 292 | return to_ret;
|
|---|
| 293 | }
|
|---|
| 294 |
|
|---|
| 295 | int event_loop_once(struct event_context *ev)
|
|---|
| 296 | {
|
|---|
| 297 | struct timeval now, to;
|
|---|
| 298 | fd_set r_fds, w_fds;
|
|---|
| 299 | int maxfd = 0;
|
|---|
| 300 | int ret;
|
|---|
| 301 |
|
|---|
| 302 | FD_ZERO(&r_fds);
|
|---|
| 303 | FD_ZERO(&w_fds);
|
|---|
| 304 |
|
|---|
| 305 | to.tv_sec = 9999; /* Max timeout */
|
|---|
| 306 | to.tv_usec = 0;
|
|---|
| 307 |
|
|---|
| 308 | GetTimeOfDay(&now);
|
|---|
| 309 |
|
|---|
| 310 | if (!event_add_to_select_args(ev, &now, &r_fds, &w_fds, &to, &maxfd)) {
|
|---|
| 311 | return -1;
|
|---|
| 312 | }
|
|---|
| 313 |
|
|---|
| 314 | if (timeval_is_zero(&to)) {
|
|---|
| 315 | run_events(ev, 0, NULL, NULL);
|
|---|
| 316 | return 0;
|
|---|
| 317 | }
|
|---|
| 318 |
|
|---|
| 319 | ret = sys_select(maxfd+1, &r_fds, &w_fds, NULL, &to);
|
|---|
| 320 |
|
|---|
| 321 | if (ret == -1 && errno != EINTR) {
|
|---|
| 322 | return -1;
|
|---|
| 323 | }
|
|---|
| 324 |
|
|---|
| 325 | run_events(ev, ret, &r_fds, &w_fds);
|
|---|
| 326 | return 0;
|
|---|
| 327 | }
|
|---|
| 328 |
|
|---|
| 329 | static int event_context_destructor(struct event_context *ev)
|
|---|
| 330 | {
|
|---|
| 331 | while (ev->fd_events != NULL) {
|
|---|
| 332 | ev->fd_events->event_ctx = NULL;
|
|---|
| 333 | DLIST_REMOVE(ev->fd_events, ev->fd_events);
|
|---|
| 334 | }
|
|---|
| 335 | while (ev->timed_events != NULL) {
|
|---|
| 336 | ev->timed_events->event_ctx = NULL;
|
|---|
| 337 | DLIST_REMOVE(ev->timed_events, ev->timed_events);
|
|---|
| 338 | }
|
|---|
| 339 | return 0;
|
|---|
| 340 | }
|
|---|
| 341 |
|
|---|
| 342 | void event_context_reinit(struct event_context *ev)
|
|---|
| 343 | {
|
|---|
| 344 | event_context_destructor(ev);
|
|---|
| 345 | return;
|
|---|
| 346 | }
|
|---|
| 347 |
|
|---|
| 348 | struct event_context *event_context_init(TALLOC_CTX *mem_ctx)
|
|---|
| 349 | {
|
|---|
| 350 | struct event_context *result;
|
|---|
| 351 |
|
|---|
| 352 | result = TALLOC_ZERO_P(mem_ctx, struct event_context);
|
|---|
| 353 | if (result == NULL) {
|
|---|
| 354 | return NULL;
|
|---|
| 355 | }
|
|---|
| 356 |
|
|---|
| 357 | talloc_set_destructor(result, event_context_destructor);
|
|---|
| 358 | return result;
|
|---|
| 359 | }
|
|---|
| 360 |
|
|---|
| 361 | void dump_event_list(struct event_context *event_ctx)
|
|---|
| 362 | {
|
|---|
| 363 | struct timed_event *te;
|
|---|
| 364 | struct fd_event *fe;
|
|---|
| 365 | struct timeval evt, now;
|
|---|
| 366 |
|
|---|
| 367 | if (!event_ctx) {
|
|---|
| 368 | return;
|
|---|
| 369 | }
|
|---|
| 370 |
|
|---|
| 371 | now = timeval_current();
|
|---|
| 372 |
|
|---|
| 373 | DEBUG(10,("dump_event_list:\n"));
|
|---|
| 374 |
|
|---|
| 375 | for (te = event_ctx->timed_events; te; te = te->next) {
|
|---|
| 376 |
|
|---|
| 377 | evt = timeval_until(&now, &te->when);
|
|---|
| 378 |
|
|---|
| 379 | DEBUGADD(10,("Timed Event \"%s\" %lx handled in %d seconds (at %s)\n",
|
|---|
| 380 | te->event_name,
|
|---|
| 381 | (unsigned long)te,
|
|---|
| 382 | (int)evt.tv_sec,
|
|---|
| 383 | http_timestring(te->when.tv_sec)));
|
|---|
| 384 | }
|
|---|
| 385 |
|
|---|
| 386 | for (fe = event_ctx->fd_events; fe; fe = fe->next) {
|
|---|
| 387 |
|
|---|
| 388 | DEBUGADD(10,("FD Event %d %lx, flags: 0x%04x\n",
|
|---|
| 389 | fe->fd,
|
|---|
| 390 | (unsigned long)fe,
|
|---|
| 391 | fe->flags));
|
|---|
| 392 | }
|
|---|
| 393 | }
|
|---|