/src/samba/source3/lib/util_event.c
Line | Count | Source |
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 | | #include "util_event.h" |
23 | | |
24 | | struct idle_event { |
25 | | struct tevent_timer *te; |
26 | | struct timeval interval; |
27 | | char *name; |
28 | | bool (*handler)(const struct timeval *now, void *private_data); |
29 | | void *private_data; |
30 | | }; |
31 | | |
32 | | static void smbd_idle_event_handler(struct tevent_context *ctx, |
33 | | struct tevent_timer *te, |
34 | | struct timeval now, |
35 | | void *private_data) |
36 | 0 | { |
37 | 0 | struct idle_event *event = |
38 | 0 | talloc_get_type_abort(private_data, struct idle_event); |
39 | |
|
40 | 0 | TALLOC_FREE(event->te); |
41 | |
|
42 | 0 | DEBUG(10,("smbd_idle_event_handler: %s %p called\n", |
43 | 0 | event->name, event->te)); |
44 | |
|
45 | 0 | if (!event->handler(&now, event->private_data)) { |
46 | 0 | DEBUG(10,("smbd_idle_event_handler: %s %p stopped\n", |
47 | 0 | event->name, event->te)); |
48 | | /* Don't repeat, delete ourselves */ |
49 | 0 | TALLOC_FREE(event); |
50 | 0 | return; |
51 | 0 | } |
52 | | |
53 | 0 | DEBUG(10,("smbd_idle_event_handler: %s %p rescheduled\n", |
54 | 0 | event->name, event->te)); |
55 | |
|
56 | 0 | event->te = tevent_add_timer(ctx, event, |
57 | 0 | timeval_sum(&now, &event->interval), |
58 | 0 | smbd_idle_event_handler, event); |
59 | | |
60 | | /* We can't do much but fail here. */ |
61 | 0 | SMB_ASSERT(event->te != NULL); |
62 | 0 | } |
63 | | |
64 | | struct idle_event *event_add_idle(struct tevent_context *event_ctx, |
65 | | TALLOC_CTX *mem_ctx, |
66 | | struct timeval interval, |
67 | | const char *name, |
68 | | bool (*handler)(const struct timeval *now, |
69 | | void *private_data), |
70 | | void *private_data) |
71 | 0 | { |
72 | 0 | struct idle_event *result; |
73 | 0 | struct timeval now = timeval_current(); |
74 | |
|
75 | 0 | result = talloc(mem_ctx, struct idle_event); |
76 | 0 | if (result == NULL) { |
77 | 0 | DEBUG(0, ("talloc failed\n")); |
78 | 0 | return NULL; |
79 | 0 | } |
80 | | |
81 | 0 | result->interval = interval; |
82 | 0 | result->handler = handler; |
83 | 0 | result->private_data = private_data; |
84 | |
|
85 | 0 | if (!(result->name = talloc_asprintf(result, "idle_evt(%s)", name))) { |
86 | 0 | DEBUG(0, ("talloc failed\n")); |
87 | 0 | TALLOC_FREE(result); |
88 | 0 | return NULL; |
89 | 0 | } |
90 | | |
91 | 0 | result->te = tevent_add_timer(event_ctx, result, |
92 | 0 | timeval_sum(&now, &interval), |
93 | 0 | smbd_idle_event_handler, result); |
94 | 0 | if (result->te == NULL) { |
95 | 0 | DEBUG(0, ("event_add_timed failed\n")); |
96 | 0 | TALLOC_FREE(result); |
97 | 0 | return NULL; |
98 | 0 | } |
99 | | |
100 | 0 | DEBUG(10,("event_add_idle: %s %p\n", result->name, result->te)); |
101 | 0 | return result; |
102 | 0 | } |