r20087: Ensure we clean up any random pending events we
[ira/wip.git] / source3 / lib / events.c
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 2 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, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23
24 static struct timed_event *timed_events;
25
26 static int timed_event_destructor(struct timed_event *te)
27 {
28         DEBUG(10, ("Destroying timed event %lx \"%s\"\n", (unsigned long)te,
29                 te->event_name));
30         DLIST_REMOVE(timed_events, te);
31         return 0;
32 }
33
34 /****************************************************************************
35  Schedule a function for future calling, cancel with TALLOC_FREE().
36  It's the responsibility of the handler to call TALLOC_FREE() on the event
37  handed to it.
38 ****************************************************************************/
39
40 struct timed_event *add_timed_event(TALLOC_CTX *mem_ctx,
41                                 struct timeval when,
42                                 const char *event_name,
43                                 void (*handler)(struct timed_event *te,
44                                                 const struct timeval *now,
45                                                 void *private_data),
46                                 void *private_data)
47 {
48         struct timed_event *te, *last_te, *cur_te;
49
50         te = TALLOC_P(mem_ctx, struct timed_event);
51         if (te == NULL) {
52                 DEBUG(0, ("talloc failed\n"));
53                 return NULL;
54         }
55
56         te->when = when;
57         te->event_name = event_name;
58         te->handler = handler;
59         te->private_data = private_data;
60
61         /* keep the list ordered - this is NOT guarenteed as event times
62            may be changed after insertion */
63         last_te = NULL;
64         for (cur_te = timed_events; cur_te; cur_te = cur_te->next) {
65                 /* if the new event comes before the current one break */
66                 if (!timeval_is_zero(&cur_te->when) &&
67                                 timeval_compare(&te->when, &cur_te->when) < 0) {
68                         break;
69                 }
70                 last_te = cur_te;
71         }
72
73         DLIST_ADD_AFTER(timed_events, te, last_te);
74         talloc_set_destructor(te, timed_event_destructor);
75
76         DEBUG(10, ("Added timed event \"%s\": %lx\n", event_name,
77                         (unsigned long)te));
78         return te;
79 }
80
81 void run_events(void)
82 {
83         /* Run all events that are pending, not just one (as we
84            did previously. */
85
86         while (timed_events) {
87                 struct timeval now;
88                 GetTimeOfDay(&now);
89
90                 if (timeval_compare(&now, &timed_events->when) < 0) {
91                         /* Nothing to do yet */
92                         DEBUG(11, ("run_events: Nothing to do\n"));
93                         return;
94                 }
95
96                 DEBUG(10, ("Running event \"%s\" %lx\n", timed_events->event_name,
97                         (unsigned long)timed_events));
98
99                 timed_events->handler(timed_events, &now, timed_events->private_data);
100         }
101 }
102
103 struct timeval *get_timed_events_timeout(struct timeval *to_ret)
104 {
105         struct timeval now;
106
107         if (timed_events == NULL) {
108                 return NULL;
109         }
110
111         now = timeval_current();
112         *to_ret = timeval_until(&now, &timed_events->when);
113
114         DEBUG(10, ("timed_events_timeout: %d/%d\n", (int)to_ret->tv_sec,
115                 (int)to_ret->tv_usec));
116
117         return to_ret;
118 }
119
120 int set_event_dispatch_time(const char *event_name, struct timeval when)
121 {
122         int num_events = 0;
123         struct timed_event *te;
124
125         for (te = timed_events; te; te = te->next) {
126                 if (strcmp(event_name, te->event_name) == 0) {
127                         te->when = when;
128                         num_events++;
129                 }
130         }
131         return num_events;
132 }
133
134 /* Returns 1 if event was found and cancelled, 0 otherwise. */
135
136 int cancel_named_event(const char *event_name)
137 {
138         struct timed_event *te;
139
140         for (te = timed_events; te; te = te->next) {
141                 if (strcmp(event_name, te->event_name) == 0) {
142                         TALLOC_FREE(te);
143                         return 1;
144                 }
145         }
146         return 0;
147 }