]>
git.saurik.com Git - apple/ipsec.git/blob - ipsec-tools/racoon/schedule.c
fe82c30ddea260b4076125c8a1698e02f7489dd2
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 #if !defined(__LP64__)
55 // year 2038 problem and fix for 32-bit only
56 #define FIXY2038PROBLEM
60 #define TAILQ_FOREACH(elm, head, field) \
61 for (elm = TAILQ_FIRST(head); elm; elm = TAILQ_NEXT(elm, field))
64 extern int terminated
;
65 static struct timeval timeout
;
67 #ifdef FIXY2038PROBLEM
68 #define Y2038TIME_T 0x7fffffff
69 static time_t launched
; /* time when the program launched. */
70 static time_t deltaY2038
;
73 static TAILQ_HEAD(_schedtree
, sched
) sctree
;
75 static void sched_add
__P((struct sched
*));
76 static time_t current_time
__P((void));
81 * time to block until next event.
82 * if no entry, NULL returned.
88 struct sched
*p
, *next
= NULL
;
92 for (p
= TAILQ_FIRST(&sctree
); p
; p
= next
) {
93 /* if the entry has been dead, remove it */
97 /* if the time hasn't come, proceed to the next entry */
99 next
= TAILQ_NEXT(p
, chain
);
103 /* mark it with dead. and call the function. */
105 if (p
->func
!= NULL
&& !terminated
)
109 next
= TAILQ_NEXT(p
, chain
);
110 TAILQ_REMOVE(&sctree
, p
, chain
);
114 p
= TAILQ_FIRST(&sctree
);
118 now
= current_time();
120 delta
= p
->xtime
- now
;
121 timeout
.tv_sec
= delta
< 0 ? 0 : delta
;
128 * add new schedule to schedule table.
131 sched_new(tick
, func
, param
)
133 void (*func
) __P((void *));
139 new = (struct sched
*)racoon_malloc(sizeof(*new));
143 memset(new, 0, sizeof(*new));
151 new->xtime
= current_time() + tick
;
154 /* add to schedule table */
160 /* add new schedule to schedule table */
167 TAILQ_FOREACH(p
, &sctree
, chain
) {
168 if (sc
->xtime
< p
->xtime
) {
169 TAILQ_INSERT_BEFORE(p
, sc
, chain
);
174 TAILQ_INSERT_TAIL(&sctree
, sc
, chain
);
180 * if defined FIXY2038PROBLEM, base time is the time when called sched_init().
181 * Otherwise, conform to time(3).
187 #ifdef FIXY2038PROBLEM
210 /* XXX this function is probably unnecessary. */
212 sched_scrub_param(param
)
217 TAILQ_FOREACH(sc
, &sctree
, chain
) {
218 if (sc
->param
== param
) {
220 plog(LLV_DEBUG
, LOCATION
, NULL
,
221 "an undead schedule has been deleted.\n");
238 struct scheddump
*dst
;
245 TAILQ_FOREACH(p
, &sctree
, chain
)
252 *len
= cnt
* sizeof(*dst
);
254 new = racoon_malloc(*len
);
257 dst
= (struct scheddump
*)new;
259 p
= TAILQ_FIRST(&sctree
);
261 dst
->xtime
= p
->xtime
;
263 dst
->created
= p
->created
;
266 p
= TAILQ_NEXT(p
, chain
);
277 /* initialize schedule table */
281 #ifdef FIXY2038PROBLEM
284 deltaY2038
= Y2038TIME_T
- launched
;
293 #include <sys/types.h>
294 #include <sys/time.h>
302 printf("execute %d\n", *tick
);
312 read(0, buf
, sizeof(buf
));
314 struct scheddump
*scbuf
, *p
;
316 sched_dump((caddr_t
*)&scbuf
, &len
);
319 for (p
= scbuf
; len
; p
++) {
320 printf("xtime=%ld\n", p
->xtime
);
327 tick
= (int *)racoon_malloc(sizeof(*tick
));
329 printf("new queue tick = %d\n", *tick
);
330 sched_new(*tick
, test
, tick
);
339 struct timeval
*timeout
;
352 timeout
= schedular();
354 error
= select(nfds
, &rfds
, (fd_set
*)0, (fd_set
*)0, timeout
);
357 case EINTR
: continue;
364 if (FD_ISSET(0, &rfds
))