xref: /openbsd/usr.sbin/sasyncd/timer.c (revision 17cf054f)
1 /*	$OpenBSD: timer.c,v 1.7 2018/04/10 15:58:21 cheloha Exp $	*/
2 
3 /*
4  * Copyright (c) 2005 H�kan Olsson.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
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 Multicom Security AB.
30  */
31 
32 
33 #include <sys/types.h>
34 #include <sys/queue.h>
35 #include <sys/time.h>
36 
37 #include <stdlib.h>
38 #include <string.h>
39 #include <time.h>
40 
41 #include "sasyncd.h"
42 
43 /*
44  * All events have a name, an expiration time and a function to be called
45  * at this time. The queue is always sorted so the next event to happen is
46  * first in the queue.
47  */
48 struct event {
49 	TAILQ_ENTRY (event) next;
50 	struct timespec	 expire;
51 	char		*name;
52 	void		(*fun) (void *);
53 	void		*arg;
54 };
55 
56 static TAILQ_HEAD (event_head, event) events;
57 
58 /* Initialize timer event queue. */
59 void
timer_init(void)60 timer_init(void)
61 {
62 	TAILQ_INIT(&events);
63 }
64 
65 /*
66  * Return the number of seconds until the next event happens. Used for
67  * the select() call in the main loop.
68  */
69 void
timer_next_event(struct timespec * ts)70 timer_next_event(struct timespec *ts)
71 {
72 	struct timespec	 now;
73 	struct event	*e = TAILQ_FIRST(&events);
74 
75 	if (e) {
76 		clock_gettime(CLOCK_MONOTONIC, &now);
77 		if (timespeccmp(&now, &e->expire, >=))
78 			timespecclear(ts);
79 		else
80 			timespecsub(&e->expire, &now, ts);
81 	} else {
82 		ts->tv_sec = 60;	/* "Best guess". */
83 		ts->tv_nsec = 0;
84 	}
85 }
86 
87 /*
88  * Whenever select() times out, we have an event that should happen and this
89  * routine gets called. Handle and remove all pending events.
90  */
91 void
timer_run(void)92 timer_run(void)
93 {
94 	struct timespec	 now;
95 	struct event	*e;
96 
97 	clock_gettime(CLOCK_MONOTONIC, &now);
98 	for (e = TAILQ_FIRST(&events); e && timespeccmp(&now, &e->expire, >=);
99 	     e = TAILQ_FIRST(&events)) {
100 		TAILQ_REMOVE(&events, e, next);
101 		log_msg(2, "timer_run: event \"%s\"",
102 		    e->name ? e->name : "<unknown>");
103 		(*e->fun)(e->arg);
104 		if (e->name)
105 			free(e->name);
106 		free(e);
107 	}
108 }
109 
110 /* Add a new event. */
111 int
timer_add(char * name,u_int32_t when,void (* function)(void *),void * arg)112 timer_add(char *name, u_int32_t when, void (*function)(void *), void *arg)
113 {
114 	struct timespec	 now, tmp;
115 	struct event	*e, *new;
116 
117 	new = calloc(1, sizeof *new);
118 	if (!new) {
119 		log_err("timer_add: calloc (1, %zu) failed", sizeof *new);
120 		return -1;
121 	}
122 
123 	new->name = strdup(name); /* We handle failures here. */
124 	new->fun = function;
125 	new->arg = arg;
126 
127 	tmp.tv_sec = when;
128 	tmp.tv_nsec = 0;
129 	clock_gettime(CLOCK_MONOTONIC, &now);
130 	timespecadd(&now, &tmp, &new->expire);
131 
132 	log_msg(2, "timer_add: new event \"%s\" (expiring in %us)",
133 	    name ? name : "<unknown>", when);
134 
135 	/* Insert the new event in the queue so it's always sorted. */
136 	for (e = TAILQ_FIRST(&events); e; e = TAILQ_NEXT(e, next)) {
137 		if (timespeccmp(&new->expire, &e->expire, >=))
138 			continue;
139 		TAILQ_INSERT_BEFORE(e, new, next);
140 		return 0;
141 	}
142 	TAILQ_INSERT_TAIL(&events, new, next);
143 	return 0;
144 }
145