]>
git.saurik.com Git - apple/network_cmds.git/blob - racoon.tproj/schedule.c
1 /* $KAME: schedule.c,v 1.18 2001/10/08 23:58:22 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
32 #include <sys/types.h>
33 #include <sys/param.h>
35 #include <sys/queue.h>
48 #define FIXY2038PROBLEM
51 #define TAILQ_FOREACH(elm, head, field) \
52 for (elm = TAILQ_FIRST(head); elm; elm = TAILQ_NEXT(elm, field))
55 static struct timeval timeout
;
57 #ifdef FIXY2038PROBLEM
58 #define Y2038TIME_T 0x7fffffff
59 static time_t launched
; /* time when the program launched. */
60 static time_t deltaY2038
;
63 static TAILQ_HEAD(_schedtree
, sched
) sctree
;
65 static void sched_add
__P((struct sched
*));
66 static time_t current_time
__P((void));
71 * time to block until next event.
72 * if no entry, NULL returned.
78 struct sched
*p
, *next
= NULL
;
82 for (p
= TAILQ_FIRST(&sctree
); p
; p
= next
) {
83 /* if the entry has been daed, remove it */
87 /* if the time hasn't come, proceed to the next entry */
89 next
= TAILQ_NEXT(p
, chain
);
93 /* mark it with dead. and call the function. */
99 next
= TAILQ_NEXT(p
, chain
);
100 TAILQ_REMOVE(&sctree
, p
, chain
);
104 p
= TAILQ_FIRST(&sctree
);
108 now
= current_time();
110 delta
= p
->xtime
- now
;
111 timeout
.tv_sec
= delta
< 0 ? 0 : delta
;
118 * add new schedule to schedule table.
121 sched_new(tick
, func
, param
)
123 void (*func
) __P((void *));
129 new = (struct sched
*)racoon_malloc(sizeof(*new));
133 memset(new, 0, sizeof(*new));
141 new->xtime
= current_time() + tick
;
144 /* add to schedule table */
150 /* add new schedule to schedule table */
157 TAILQ_FOREACH(p
, &sctree
, chain
) {
158 if (sc
->xtime
< p
->xtime
) {
159 TAILQ_INSERT_BEFORE(p
, sc
, chain
);
164 TAILQ_INSERT_TAIL(&sctree
, sc
, chain
);
170 * if defined FIXY2038PROBLEM, base time is the time when called sched_init().
171 * Otherwise, conform to time(3).
177 #ifdef FIXY2038PROBLEM
200 /* XXX this function is probably unnecessary. */
202 sched_scrub_param(param
)
207 TAILQ_FOREACH(sc
, &sctree
, chain
) {
208 if (sc
->param
== param
) {
210 plog(LLV_DEBUG
, LOCATION
, NULL
,
211 "an undead schedule has been deleted.\n");
228 struct scheddump
*dst
;
235 TAILQ_FOREACH(p
, &sctree
, chain
)
242 *len
= cnt
* sizeof(*dst
);
244 new = racoon_malloc(*len
);
247 dst
= (struct scheddump
*)new;
249 p
= TAILQ_FIRST(&sctree
);
251 dst
->xtime
= p
->xtime
;
253 dst
->created
= p
->created
;
256 p
= TAILQ_NEXT(p
, chain
);
267 /* initialize schedule table */
271 #ifdef FIXY2038PROBLEM
274 deltaY2038
= Y2038TIME_T
- launched
;
283 #include <sys/types.h>
284 #include <sys/time.h>
292 printf("execute %d\n", *tick
);
302 read(0, buf
, sizeof(buf
));
304 struct scheddump
*scbuf
, *p
;
306 sched_dump((caddr_t
*)&scbuf
, &len
);
309 for (p
= scbuf
; len
; p
++) {
310 printf("xtime=%ld\n", p
->xtime
);
317 tick
= (int *)racoon_malloc(sizeof(*tick
));
319 printf("new queue tick = %d\n", *tick
);
320 sched_new(*tick
, test
, tick
);
329 struct timeval
*timeout
;
342 timeout
= schedular();
344 error
= select(nfds
, &rfds
, (fd_set
*)0, (fd_set
*)0, timeout
);
347 case EINTR
: continue;
354 if (FD_ISSET(0, &rfds
))