]>
Commit | Line | Data |
---|---|---|
52b7d2ce A |
1 | /* $KAME: schedule.c,v 1.19 2001/11/05 10:53:19 sakane Exp $ */ |
2 | ||
3 | /* | |
4 | * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. | |
5 | * All rights reserved. | |
6 | * | |
7 | * Redistribution and use in source and binary forms, with or without | |
8 | * modification, are permitted provided that the following conditions | |
9 | * are met: | |
10 | * 1. Redistributions of source code must retain the above copyright | |
11 | * notice, this list of conditions and the following disclaimer. | |
12 | * 2. Redistributions in binary form must reproduce the above copyright | |
13 | * notice, this list of conditions and the following disclaimer in the | |
14 | * documentation and/or other materials provided with the distribution. | |
15 | * 3. Neither the name of the project nor the names of its contributors | |
16 | * may be used to endorse or promote products derived from this software | |
17 | * without specific prior written permission. | |
18 | * | |
19 | * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND | |
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE | |
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
29 | * SUCH DAMAGE. | |
30 | */ | |
31 | ||
32 | #include "config.h" | |
33 | ||
34 | #include <sys/types.h> | |
35 | #include <sys/param.h> | |
36 | #include <sys/time.h> | |
37 | #include <sys/queue.h> | |
38 | #include <sys/socket.h> | |
39 | ||
40 | #include <stdlib.h> | |
41 | #include <stdio.h> | |
42 | #include <string.h> | |
43 | #include <errno.h> | |
44 | #include <time.h> | |
45 | ||
46 | #include "misc.h" | |
47 | #include "plog.h" | |
48 | #include "schedule.h" | |
49 | #include "var.h" | |
50 | #include "gcmalloc.h" | |
51 | ||
52 | #define FIXY2038PROBLEM | |
53 | ||
54 | #ifndef TAILQ_FOREACH | |
55 | #define TAILQ_FOREACH(elm, head, field) \ | |
56 | for (elm = TAILQ_FIRST(head); elm; elm = TAILQ_NEXT(elm, field)) | |
57 | #endif | |
58 | ||
59 | static struct timeval timeout; | |
60 | ||
61 | #ifdef FIXY2038PROBLEM | |
62 | #define Y2038TIME_T 0x7fffffff | |
63 | static time_t launched; /* time when the program launched. */ | |
64 | static time_t deltaY2038; | |
65 | #endif | |
66 | ||
67 | static TAILQ_HEAD(_schedtree, sched) sctree; | |
68 | ||
69 | static void sched_add __P((struct sched *)); | |
70 | static time_t current_time __P((void)); | |
71 | ||
72 | /* | |
73 | * schedule handler | |
74 | * OUT: | |
75 | * time to block until next event. | |
76 | * if no entry, NULL returned. | |
77 | */ | |
78 | struct timeval * | |
79 | schedular() | |
80 | { | |
81 | time_t now, delta; | |
82 | struct sched *p, *next = NULL; | |
83 | ||
84 | now = current_time(); | |
85 | ||
86 | for (p = TAILQ_FIRST(&sctree); p; p = next) { | |
87 | /* if the entry has been dead, remove it */ | |
88 | if (p->dead) | |
89 | goto next_schedule; | |
90 | ||
91 | /* if the time hasn't come, proceed to the next entry */ | |
92 | if (now < p->xtime) { | |
93 | next = TAILQ_NEXT(p, chain); | |
94 | continue; | |
95 | } | |
96 | ||
97 | /* mark it with dead. and call the function. */ | |
98 | p->dead = 1; | |
99 | if (p->func != NULL) | |
100 | (p->func)(p->param); | |
101 | ||
102 | next_schedule: | |
103 | next = TAILQ_NEXT(p, chain); | |
104 | TAILQ_REMOVE(&sctree, p, chain); | |
105 | racoon_free(p); | |
106 | } | |
107 | ||
108 | p = TAILQ_FIRST(&sctree); | |
109 | if (p == NULL) | |
110 | return NULL; | |
111 | ||
112 | now = current_time(); | |
113 | ||
114 | delta = p->xtime - now; | |
115 | timeout.tv_sec = delta < 0 ? 0 : delta; | |
116 | timeout.tv_usec = 0; | |
117 | ||
118 | return &timeout; | |
119 | } | |
120 | ||
121 | /* | |
122 | * add new schedule to schedule table. | |
123 | */ | |
124 | struct sched * | |
125 | sched_new(tick, func, param) | |
126 | time_t tick; | |
127 | void (*func) __P((void *)); | |
128 | void *param; | |
129 | { | |
130 | static long id = 1; | |
131 | struct sched *new; | |
132 | ||
133 | new = (struct sched *)racoon_malloc(sizeof(*new)); | |
134 | if (new == NULL) | |
135 | return NULL; | |
136 | ||
137 | memset(new, 0, sizeof(*new)); | |
138 | new->func = func; | |
139 | new->param = param; | |
140 | ||
141 | new->id = id++; | |
142 | time(&new->created); | |
143 | new->tick = tick; | |
144 | ||
145 | new->xtime = current_time() + tick; | |
146 | new->dead = 0; | |
147 | ||
148 | /* add to schedule table */ | |
149 | sched_add(new); | |
150 | ||
151 | return(new); | |
152 | } | |
153 | ||
154 | /* add new schedule to schedule table */ | |
155 | static void | |
156 | sched_add(sc) | |
157 | struct sched *sc; | |
158 | { | |
159 | struct sched *p; | |
160 | ||
161 | TAILQ_FOREACH(p, &sctree, chain) { | |
162 | if (sc->xtime < p->xtime) { | |
163 | TAILQ_INSERT_BEFORE(p, sc, chain); | |
164 | return; | |
165 | } | |
166 | } | |
167 | if (p == NULL) | |
168 | TAILQ_INSERT_TAIL(&sctree, sc, chain); | |
169 | ||
170 | return; | |
171 | } | |
172 | ||
173 | /* get current time. | |
174 | * if defined FIXY2038PROBLEM, base time is the time when called sched_init(). | |
175 | * Otherwise, conform to time(3). | |
176 | */ | |
177 | static time_t | |
178 | current_time() | |
179 | { | |
180 | time_t n; | |
181 | #ifdef FIXY2038PROBLEM | |
182 | time_t t; | |
183 | ||
184 | time(&n); | |
185 | t = n - launched; | |
186 | if (t < 0) | |
187 | t += deltaY2038; | |
188 | ||
189 | return t; | |
190 | #else | |
191 | return time(&n); | |
192 | #endif | |
193 | } | |
194 | ||
195 | void | |
196 | sched_kill(sc) | |
197 | struct sched *sc; | |
198 | { | |
199 | sc->dead = 1; | |
200 | ||
201 | return; | |
202 | } | |
203 | ||
204 | /* XXX this function is probably unnecessary. */ | |
205 | void | |
206 | sched_scrub_param(param) | |
207 | void *param; | |
208 | { | |
209 | struct sched *sc; | |
210 | ||
211 | TAILQ_FOREACH(sc, &sctree, chain) { | |
212 | if (sc->param == param) { | |
213 | if (!sc->dead) { | |
214 | plog(LLV_DEBUG, LOCATION, NULL, | |
215 | "an undead schedule has been deleted.\n"); | |
216 | } | |
217 | sched_kill(sc); | |
218 | } | |
219 | } | |
220 | } | |
221 | ||
222 | /* | |
223 | * for debug | |
224 | */ | |
225 | int | |
226 | sched_dump(buf, len) | |
227 | caddr_t *buf; | |
228 | int *len; | |
229 | { | |
230 | caddr_t new; | |
231 | struct sched *p; | |
232 | struct scheddump *dst; | |
233 | int cnt = 0; | |
234 | ||
235 | /* initialize */ | |
236 | *len = 0; | |
237 | *buf = NULL; | |
238 | ||
239 | TAILQ_FOREACH(p, &sctree, chain) | |
240 | cnt++; | |
241 | ||
242 | /* no entry */ | |
243 | if (cnt == 0) | |
244 | return -1; | |
245 | ||
246 | *len = cnt * sizeof(*dst); | |
247 | ||
248 | new = racoon_malloc(*len); | |
249 | if (new == NULL) | |
250 | return -1; | |
251 | dst = (struct scheddump *)new; | |
252 | ||
253 | p = TAILQ_FIRST(&sctree); | |
254 | while (p) { | |
255 | dst->xtime = p->xtime; | |
256 | dst->id = p->id; | |
257 | dst->created = p->created; | |
258 | dst->tick = p->tick; | |
259 | ||
260 | p = TAILQ_NEXT(p, chain); | |
261 | if (p == NULL) | |
262 | break; | |
263 | dst++; | |
264 | } | |
265 | ||
266 | *buf = new; | |
267 | ||
268 | return 0; | |
269 | } | |
270 | ||
271 | /* initialize schedule table */ | |
272 | void | |
273 | sched_init() | |
274 | { | |
275 | #ifdef FIXY2038PROBLEM | |
276 | time(&launched); | |
277 | ||
278 | deltaY2038 = Y2038TIME_T - launched; | |
279 | #endif | |
280 | ||
281 | TAILQ_INIT(&sctree); | |
282 | ||
283 | return; | |
284 | } | |
285 | ||
286 | #ifdef STEST | |
287 | #include <sys/types.h> | |
288 | #include <sys/time.h> | |
289 | #include <unistd.h> | |
290 | #include <err.h> | |
291 | ||
292 | void | |
293 | test(tick) | |
294 | int *tick; | |
295 | { | |
296 | printf("execute %d\n", *tick); | |
297 | racoon_free(tick); | |
298 | } | |
299 | ||
300 | void | |
301 | getstdin() | |
302 | { | |
303 | int *tick; | |
304 | char buf[16]; | |
305 | ||
306 | read(0, buf, sizeof(buf)); | |
307 | if (buf[0] == 'd') { | |
308 | struct scheddump *scbuf, *p; | |
309 | int len; | |
310 | sched_dump((caddr_t *)&scbuf, &len); | |
311 | if (buf == NULL) | |
312 | return; | |
313 | for (p = scbuf; len; p++) { | |
314 | printf("xtime=%ld\n", p->xtime); | |
315 | len -= sizeof(*p); | |
316 | } | |
317 | racoon_free(scbuf); | |
318 | return; | |
319 | } | |
320 | ||
321 | tick = (int *)racoon_malloc(sizeof(*tick)); | |
322 | *tick = atoi(buf); | |
323 | printf("new queue tick = %d\n", *tick); | |
324 | sched_new(*tick, test, tick); | |
325 | } | |
326 | ||
327 | int | |
328 | main() | |
329 | { | |
330 | static fd_set mask0; | |
331 | int nfds = 0; | |
332 | fd_set rfds; | |
333 | struct timeval *timeout; | |
334 | int error; | |
335 | ||
336 | FD_ZERO(&mask0); | |
337 | FD_SET(0, &mask0); | |
338 | nfds = 1; | |
339 | ||
340 | /* initialize */ | |
341 | sched_init(); | |
342 | ||
343 | while (1) { | |
344 | rfds = mask0; | |
345 | ||
346 | timeout = schedular(); | |
347 | ||
348 | error = select(nfds, &rfds, (fd_set *)0, (fd_set *)0, timeout); | |
349 | if (error < 0) { | |
350 | switch (errno) { | |
351 | case EINTR: continue; | |
352 | default: | |
353 | err(1, "select"); | |
354 | } | |
355 | /*NOTREACHED*/ | |
356 | } | |
357 | ||
358 | if (FD_ISSET(0, &rfds)) | |
359 | getstdin(); | |
360 | } | |
361 | } | |
362 | #endif |