xref: /openbsd/sbin/isakmpd/timer.c (revision 6ee513e5)
1 /* $OpenBSD: timer.c,v 1.18 2017/12/05 20:31:45 jca Exp $	 */
2 /* $EOM: timer.c,v 1.13 2000/02/20 19:58:42 niklas Exp $	 */
3 
4 /*
5  * Copyright (c) 1998, 1999 Niklas Hallqvist.  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  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 /*
29  * This code was written under funding by Ericsson Radio Systems.
30  */
31 
32 #include <sys/queue.h>
33 #include <sys/time.h>
34 
35 #include <stdlib.h>
36 #include <string.h>
37 
38 #include "log.h"
39 #include "timer.h"
40 
41 static	TAILQ_HEAD(event_list, event) events;
42 
43 void
timer_init(void)44 timer_init(void)
45 {
46 	TAILQ_INIT(&events);
47 }
48 
49 void
timer_next_event(struct timespec ** timeout)50 timer_next_event(struct timespec **timeout)
51 {
52 	struct timespec  now;
53 
54 	if (TAILQ_FIRST(&events)) {
55 		clock_gettime(CLOCK_MONOTONIC, &now);
56 		if (timespeccmp(&now, &TAILQ_FIRST(&events)->expiration, >=))
57 			timespecclear(*timeout);
58 		else
59 			timespecsub(&TAILQ_FIRST(&events)->expiration, &now,
60 			    *timeout);
61 	} else
62 		*timeout = 0;
63 }
64 
65 void
timer_handle_expirations(void)66 timer_handle_expirations(void)
67 {
68 	struct timespec now;
69 	struct event   *n;
70 
71 	clock_gettime(CLOCK_MONOTONIC, &now);
72 	for (n = TAILQ_FIRST(&events);
73 	    n && timespeccmp(&now, &n->expiration, >=);
74 	    n = TAILQ_FIRST(&events)) {
75 		LOG_DBG((LOG_TIMER, 10,
76 		    "timer_handle_expirations: event %s(%p)", n->name,
77 		    n->arg));
78 		TAILQ_REMOVE(&events, n, link);
79 		(*n->func)(n->arg);
80 		free(n);
81 	}
82 }
83 
84 struct event *
timer_add_event(char * name,void (* func)(void *),void * arg,struct timespec * expiration)85 timer_add_event(char *name, void (*func)(void *), void *arg,
86     struct timespec *expiration)
87 {
88 	struct event   *ev = malloc(sizeof *ev);
89 	struct event   *n;
90 	struct timespec now;
91 
92 	if (!ev)
93 		return 0;
94 	ev->name = name;
95 	ev->func = func;
96 	ev->arg = arg;
97 	clock_gettime(CLOCK_MONOTONIC, &now);
98 	memcpy(&ev->expiration, expiration, sizeof *expiration);
99 	for (n = TAILQ_FIRST(&events);
100 	    n && timespeccmp(expiration, &n->expiration, >=);
101 	    n = TAILQ_NEXT(n, link))
102 		;
103 	if (n) {
104 		LOG_DBG((LOG_TIMER, 10,
105 		    "timer_add_event: event %s(%p) added before %s(%p), "
106 		    "expiration in %llds", name, arg, n->name, n->arg,
107 		    (long long)(expiration->tv_sec - now.tv_sec)));
108 		TAILQ_INSERT_BEFORE(n, ev, link);
109 	} else {
110 		LOG_DBG((LOG_TIMER, 10, "timer_add_event: event %s(%p) added "
111 		    "last, expiration in %ds", name, arg,
112 		    (int)(expiration->tv_sec - now.tv_sec)));
113 		TAILQ_INSERT_TAIL(&events, ev, link);
114 	}
115 	return ev;
116 }
117 
118 void
timer_remove_event(struct event * ev)119 timer_remove_event(struct event *ev)
120 {
121 	LOG_DBG((LOG_TIMER, 10, "timer_remove_event: removing event %s(%p)",
122 	    ev->name, ev->arg));
123 	TAILQ_REMOVE(&events, ev, link);
124 	free(ev);
125 }
126 
127 void
timer_report(void)128 timer_report(void)
129 {
130 	struct event   *ev;
131 	struct timespec now;
132 
133 	clock_gettime(CLOCK_MONOTONIC, &now);
134 
135 	for (ev = TAILQ_FIRST(&events); ev; ev = TAILQ_NEXT(ev, link))
136 		LOG_DBG((LOG_REPORT, 0,
137 		    "timer_report: event %s(%p) scheduled in %d seconds",
138 		    (ev->name ? ev->name : "<unknown>"), ev,
139 		    (int) (ev->expiration.tv_sec - now.tv_sec)));
140 }
141