s3: events. Move events.c to util_event.c
[samba.git] / source3 / lib / util_event.c
1 /*
2    Unix SMB/CIFS implementation.
3    Timed event library.
4    Copyright (C) Andrew Tridgell 1992-1998
5    Copyright (C) Volker Lendecke 2005-2007
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 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;
29 };
30
31 static void smbd_idle_event_handler(struct tevent_context *ctx,
32                                     struct tevent_timer *te,
33                                     struct timeval now,
34                                     void *private_data)
35 {
36         struct idle_event *event =
37                 talloc_get_type_abort(private_data, struct idle_event);
38
39         TALLOC_FREE(event->te);
40
41         DEBUG(10,("smbd_idle_event_handler: %s %p called\n",
42                   event->name, event->te));
43
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;
50         }
51
52         DEBUG(10,("smbd_idle_event_handler: %s %p rescheduled\n",
53                   event->name, event->te));
54
55         event->te = tevent_add_timer(ctx, event,
56                                      timeval_sum(&now, &event->interval),
57                                      smbd_idle_event_handler, event);
58
59         /* We can't do much but fail here. */
60         SMB_ASSERT(event->te != NULL);
61 }
62
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)
70 {
71         struct idle_event *result;
72         struct timeval now = timeval_current();
73
74         result = talloc(mem_ctx, struct idle_event);
75         if (result == NULL) {
76                 DEBUG(0, ("talloc failed\n"));
77                 return NULL;
78         }
79
80         result->interval = interval;
81         result->handler = handler;
82         result->private_data = private_data;
83
84         if (!(result->name = talloc_asprintf(result, "idle_evt(%s)", name))) {
85                 DEBUG(0, ("talloc failed\n"));
86                 TALLOC_FREE(result);
87                 return NULL;
88         }
89
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;
97         }
98
99         DEBUG(10,("event_add_idle: %s %p\n", result->name, result->te));
100         return result;
101 }