GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: sbin/isakmpd/timer.c Lines: 0 37 0.0 %
Date: 2016-12-06 Branches: 0 36 0.0 %

Line Branch Exec Source
1
/* $OpenBSD: timer.c,v 1.17 2015/08/20 22:02:21 deraadt 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 <stdlib.h>
34
#include <string.h>
35
36
#include "log.h"
37
#include "timer.h"
38
39
static	TAILQ_HEAD(event_list, event) events;
40
41
void
42
timer_init(void)
43
{
44
	TAILQ_INIT(&events);
45
}
46
47
void
48
timer_next_event(struct timeval **timeout)
49
{
50
	struct timeval  now;
51
52
	if (TAILQ_FIRST(&events)) {
53
		gettimeofday(&now, 0);
54
		if (timercmp(&now, &TAILQ_FIRST(&events)->expiration, >=))
55
			timerclear(*timeout);
56
		else
57
			timersub(&TAILQ_FIRST(&events)->expiration, &now,
58
			    *timeout);
59
	} else
60
		*timeout = 0;
61
}
62
63
void
64
timer_handle_expirations(void)
65
{
66
	struct timeval  now;
67
	struct event   *n;
68
69
	gettimeofday(&now, 0);
70
	for (n = TAILQ_FIRST(&events); n && timercmp(&now, &n->expiration, >=);
71
	    n = TAILQ_FIRST(&events)) {
72
		LOG_DBG((LOG_TIMER, 10,
73
		    "timer_handle_expirations: event %s(%p)", n->name,
74
		    n->arg));
75
		TAILQ_REMOVE(&events, n, link);
76
		(*n->func)(n->arg);
77
		free(n);
78
	}
79
}
80
81
struct event *
82
timer_add_event(char *name, void (*func)(void *), void *arg,
83
    struct timeval *expiration)
84
{
85
	struct event   *ev = malloc(sizeof *ev);
86
	struct event   *n;
87
	struct timeval  now;
88
89
	if (!ev)
90
		return 0;
91
	ev->name = name;
92
	ev->func = func;
93
	ev->arg = arg;
94
	gettimeofday(&now, 0);
95
	memcpy(&ev->expiration, expiration, sizeof *expiration);
96
	for (n = TAILQ_FIRST(&events);
97
	    n && timercmp(expiration, &n->expiration, >=);
98
	    n = TAILQ_NEXT(n, link))
99
		;
100
	if (n) {
101
		LOG_DBG((LOG_TIMER, 10,
102
		    "timer_add_event: event %s(%p) added before %s(%p), "
103
		    "expiration in %llds", name, arg, n->name, n->arg,
104
		    (long long)(expiration->tv_sec - now.tv_sec)));
105
		TAILQ_INSERT_BEFORE(n, ev, link);
106
	} else {
107
		LOG_DBG((LOG_TIMER, 10, "timer_add_event: event %s(%p) added "
108
		    "last, expiration in %ds", name, arg,
109
		    (int)(expiration->tv_sec - now.tv_sec)));
110
		TAILQ_INSERT_TAIL(&events, ev, link);
111
	}
112
	return ev;
113
}
114
115
void
116
timer_remove_event(struct event *ev)
117
{
118
	LOG_DBG((LOG_TIMER, 10, "timer_remove_event: removing event %s(%p)",
119
	    ev->name, ev->arg));
120
	TAILQ_REMOVE(&events, ev, link);
121
	free(ev);
122
}
123
124
void
125
timer_report(void)
126
{
127
	struct event   *ev;
128
	struct timeval  now;
129
130
	gettimeofday(&now, 0);
131
132
	for (ev = TAILQ_FIRST(&events); ev; ev = TAILQ_NEXT(ev, link))
133
		LOG_DBG((LOG_REPORT, 0,
134
		    "timer_report: event %s(%p) scheduled in %d seconds",
135
		    (ev->name ? ev->name : "<unknown>"), ev,
136
		    (int) (ev->expiration.tv_sec - now.tv_sec)));
137
}