WHATSNEW: Add release notes for Samba 4.5.4.
[Samba.git] / source3 / lib / util_event.c
blob4ca2840b661b04299b1f1dc0eb9db778cc220c9f
1 /*
2 Unix SMB/CIFS implementation.
3 Timed event library.
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Volker Lendecke 2005-2007
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/>.
21 #include "includes.h"
23 struct idle_event {
24 struct tevent_timer *te;
25 struct timeval interval;
26 char *name;
27 bool (*handler)(const struct timeval *now, void *private_data);
28 void *private_data;
31 static void smbd_idle_event_handler(struct tevent_context *ctx,
32 struct tevent_timer *te,
33 struct timeval now,
34 void *private_data)
36 struct idle_event *event =
37 talloc_get_type_abort(private_data, struct idle_event);
39 TALLOC_FREE(event->te);
41 DEBUG(10,("smbd_idle_event_handler: %s %p called\n",
42 event->name, event->te));
44 if (!event->handler(&now, event->private_data)) {
45 DEBUG(10,("smbd_idle_event_handler: %s %p stopped\n",
46 event->name, event->te));
47 /* Don't repeat, delete ourselves */
48 TALLOC_FREE(event);
49 return;
52 DEBUG(10,("smbd_idle_event_handler: %s %p rescheduled\n",
53 event->name, event->te));
55 event->te = tevent_add_timer(ctx, event,
56 timeval_sum(&now, &event->interval),
57 smbd_idle_event_handler, event);
59 /* We can't do much but fail here. */
60 SMB_ASSERT(event->te != NULL);
63 struct idle_event *event_add_idle(struct tevent_context *event_ctx,
64 TALLOC_CTX *mem_ctx,
65 struct timeval interval,
66 const char *name,
67 bool (*handler)(const struct timeval *now,
68 void *private_data),
69 void *private_data)
71 struct idle_event *result;
72 struct timeval now = timeval_current();
74 result = talloc(mem_ctx, struct idle_event);
75 if (result == NULL) {
76 DEBUG(0, ("talloc failed\n"));
77 return NULL;
80 result->interval = interval;
81 result->handler = handler;
82 result->private_data = private_data;
84 if (!(result->name = talloc_asprintf(result, "idle_evt(%s)", name))) {
85 DEBUG(0, ("talloc failed\n"));
86 TALLOC_FREE(result);
87 return NULL;
90 result->te = tevent_add_timer(event_ctx, result,
91 timeval_sum(&now, &interval),
92 smbd_idle_event_handler, result);
93 if (result->te == NULL) {
94 DEBUG(0, ("event_add_timed failed\n"));
95 TALLOC_FREE(result);
96 return NULL;
99 DEBUG(10,("event_add_idle: %s %p\n", result->name, result->te));
100 return result;