]>
git.saurik.com Git - apple/ipsec.git/blob - ipsec-tools/racoon/schedule.c
1 /* $NetBSD: schedule.c,v 1.4 2006/09/09 16:22:10 manu Exp $ */
3 /* $KAME: schedule.c,v 1.19 2001/11/05 10:53:19 sakane Exp $ */
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/types.h>
37 #include <sys/param.h>
39 #include <sys/queue.h>
40 #include <sys/socket.h>
54 #define FIXY2038PROBLEM
57 #define TAILQ_FOREACH(elm, head, field) \
58 for (elm = TAILQ_FIRST(head); elm; elm = TAILQ_NEXT(elm, field))
61 extern int terminated
;
62 static struct timeval timeout
;
64 #ifdef FIXY2038PROBLEM
65 #define Y2038TIME_T 0x7fffffff
66 static time_t launched
; /* time when the program launched. */
67 static time_t deltaY2038
;
70 static TAILQ_HEAD(_schedtree
, sched
) sctree
;
72 static void sched_add
__P((struct sched
*));
73 static time_t current_time
__P((void));
78 * time to block until next event.
79 * if no entry, NULL returned.
85 struct sched
*p
, *next
= NULL
;
89 for (p
= TAILQ_FIRST(&sctree
); p
; p
= next
) {
90 /* if the entry has been dead, remove it */
94 /* if the time hasn't come, proceed to the next entry */
96 next
= TAILQ_NEXT(p
, chain
);
100 /* mark it with dead. and call the function. */
102 if (p
->func
!= NULL
&& !terminated
)
106 next
= TAILQ_NEXT(p
, chain
);
107 TAILQ_REMOVE(&sctree
, p
, chain
);
111 p
= TAILQ_FIRST(&sctree
);
115 now
= current_time();
117 delta
= p
->xtime
- now
;
118 timeout
.tv_sec
= delta
< 0 ? 0 : delta
;
125 * add new schedule to schedule table.
128 sched_new(tick
, func
, param
)
130 void (*func
) __P((void *));
136 new = (struct sched
*)racoon_malloc(sizeof(*new));
140 memset(new, 0, sizeof(*new));
148 new->xtime
= current_time() + tick
;
151 /* add to schedule table */
157 /* add new schedule to schedule table */
164 TAILQ_FOREACH(p
, &sctree
, chain
) {
165 if (sc
->xtime
< p
->xtime
) {
166 TAILQ_INSERT_BEFORE(p
, sc
, chain
);
171 TAILQ_INSERT_TAIL(&sctree
, sc
, chain
);
177 * if defined FIXY2038PROBLEM, base time is the time when called sched_init().
178 * Otherwise, conform to time(3).
184 #ifdef FIXY2038PROBLEM
207 /* XXX this function is probably unnecessary. */
209 sched_scrub_param(param
)
214 TAILQ_FOREACH(sc
, &sctree
, chain
) {
215 if (sc
->param
== param
) {
217 plog(LLV_DEBUG
, LOCATION
, NULL
,
218 "an undead schedule has been deleted.\n");
235 struct scheddump
*dst
;
242 TAILQ_FOREACH(p
, &sctree
, chain
)
249 *len
= cnt
* sizeof(*dst
);
251 new = racoon_malloc(*len
);
254 dst
= (struct scheddump
*)new;
256 p
= TAILQ_FIRST(&sctree
);
258 dst
->xtime
= p
->xtime
;
260 dst
->created
= p
->created
;
263 p
= TAILQ_NEXT(p
, chain
);
274 /* initialize schedule table */
278 #ifdef FIXY2038PROBLEM
281 deltaY2038
= Y2038TIME_T
- launched
;
290 #include <sys/types.h>
291 #include <sys/time.h>
299 printf("execute %d\n", *tick
);
309 read(0, buf
, sizeof(buf
));
311 struct scheddump
*scbuf
, *p
;
313 sched_dump((caddr_t
*)&scbuf
, &len
);
316 for (p
= scbuf
; len
; p
++) {
317 printf("xtime=%ld\n", p
->xtime
);
324 tick
= (int *)racoon_malloc(sizeof(*tick
));
326 printf("new queue tick = %d\n", *tick
);
327 sched_new(*tick
, test
, tick
);
336 struct timeval
*timeout
;
349 timeout
= schedular();
351 error
= select(nfds
, &rfds
, (fd_set
*)0, (fd_set
*)0, timeout
);
354 case EINTR
: continue;
361 if (FD_ISSET(0, &rfds
))