]>
git.saurik.com Git - apple/ipsec.git/blob - ipsec-tools/racoon/schedule.c
1 /* $KAME: schedule.c,v 1.19 2001/11/05 10:53:19 sakane Exp $ */
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
34 #include <sys/types.h>
35 #include <sys/param.h>
37 #include <sys/queue.h>
38 #include <sys/socket.h>
52 #define FIXY2038PROBLEM
55 #define TAILQ_FOREACH(elm, head, field) \
56 for (elm = TAILQ_FIRST(head); elm; elm = TAILQ_NEXT(elm, field))
59 static struct timeval timeout
;
61 #ifdef FIXY2038PROBLEM
62 #define Y2038TIME_T 0x7fffffff
63 static time_t launched
; /* time when the program launched. */
64 static time_t deltaY2038
;
67 static TAILQ_HEAD(_schedtree
, sched
) sctree
;
69 static void sched_add
__P((struct sched
*));
70 static time_t current_time
__P((void));
75 * time to block until next event.
76 * if no entry, NULL returned.
82 struct sched
*p
, *next
= NULL
;
86 for (p
= TAILQ_FIRST(&sctree
); p
; p
= next
) {
87 /* if the entry has been dead, remove it */
91 /* if the time hasn't come, proceed to the next entry */
93 next
= TAILQ_NEXT(p
, chain
);
97 /* mark it with dead. and call the function. */
103 next
= TAILQ_NEXT(p
, chain
);
104 TAILQ_REMOVE(&sctree
, p
, chain
);
108 p
= TAILQ_FIRST(&sctree
);
112 now
= current_time();
114 delta
= p
->xtime
- now
;
115 timeout
.tv_sec
= delta
< 0 ? 0 : delta
;
122 * add new schedule to schedule table.
125 sched_new(tick
, func
, param
)
127 void (*func
) __P((void *));
133 new = (struct sched
*)racoon_malloc(sizeof(*new));
137 memset(new, 0, sizeof(*new));
145 new->xtime
= current_time() + tick
;
148 /* add to schedule table */
154 /* add new schedule to schedule table */
161 TAILQ_FOREACH(p
, &sctree
, chain
) {
162 if (sc
->xtime
< p
->xtime
) {
163 TAILQ_INSERT_BEFORE(p
, sc
, chain
);
168 TAILQ_INSERT_TAIL(&sctree
, sc
, chain
);
174 * if defined FIXY2038PROBLEM, base time is the time when called sched_init().
175 * Otherwise, conform to time(3).
181 #ifdef FIXY2038PROBLEM
204 /* XXX this function is probably unnecessary. */
206 sched_scrub_param(param
)
211 TAILQ_FOREACH(sc
, &sctree
, chain
) {
212 if (sc
->param
== param
) {
214 plog(LLV_DEBUG
, LOCATION
, NULL
,
215 "an undead schedule has been deleted.\n");
232 struct scheddump
*dst
;
239 TAILQ_FOREACH(p
, &sctree
, chain
)
246 *len
= cnt
* sizeof(*dst
);
248 new = racoon_malloc(*len
);
251 dst
= (struct scheddump
*)new;
253 p
= TAILQ_FIRST(&sctree
);
255 dst
->xtime
= p
->xtime
;
257 dst
->created
= p
->created
;
260 p
= TAILQ_NEXT(p
, chain
);
271 /* initialize schedule table */
275 #ifdef FIXY2038PROBLEM
278 deltaY2038
= Y2038TIME_T
- launched
;
287 #include <sys/types.h>
288 #include <sys/time.h>
296 printf("execute %d\n", *tick
);
306 read(0, buf
, sizeof(buf
));
308 struct scheddump
*scbuf
, *p
;
310 sched_dump((caddr_t
*)&scbuf
, &len
);
313 for (p
= scbuf
; len
; p
++) {
314 printf("xtime=%ld\n", p
->xtime
);
321 tick
= (int *)racoon_malloc(sizeof(*tick
));
323 printf("new queue tick = %d\n", *tick
);
324 sched_new(*tick
, test
, tick
);
333 struct timeval
*timeout
;
346 timeout
= schedular();
348 error
= select(nfds
, &rfds
, (fd_set
*)0, (fd_set
*)0, timeout
);
351 case EINTR
: continue;
358 if (FD_ISSET(0, &rfds
))