~ubuntu-branches/ubuntu/trusty/conntrack/trusty-proposed

« back to all changes in this revision

Viewing changes to src/alarm.c

  • Committer: Bazaar Package Importer
  • Author(s): Alexander Wirt, Max Kellermann
  • Date: 2008-04-14 23:09:22 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080414230922-9xoi1gl38tc8lyng
Tags: 1:0.9.6-4
[ Max Kellermann ]
fix compilation on SPARC (printf argument mismatch)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * (C) 2006-2008 by Pablo Neira Ayuso <pablo@netfilter.org>
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License as published by
 
6
 * the Free Software Foundation; either version 2 of the License, or
 
7
 * (at your option) any later version.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 * GNU General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with this program; if not, write to the Free Software
 
16
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
17
 */
 
18
 
 
19
#include "alarm.h"
 
20
#include <stdlib.h>
 
21
#include <limits.h>
 
22
 
 
23
static struct rb_root alarm_root = RB_ROOT;
 
24
 
 
25
void init_alarm(struct alarm_block *t,
 
26
                void *data,
 
27
                void (*fcn)(struct alarm_block *a, void *data))
 
28
{
 
29
        /* initialize the head to check whether a node is inserted */
 
30
        RB_CLEAR_NODE(&t->node);
 
31
        timerclear(&t->tv);
 
32
        t->data = data;
 
33
        t->function = fcn;
 
34
}
 
35
 
 
36
static void __add_alarm(struct alarm_block *alarm)
 
37
{
 
38
        struct rb_node **new = &(alarm_root.rb_node);
 
39
        struct rb_node *parent = NULL;
 
40
 
 
41
        while (*new) {
 
42
                struct alarm_block *this;
 
43
 
 
44
                this = container_of(*new, struct alarm_block, node);
 
45
 
 
46
                parent = *new;
 
47
                if (timercmp(&alarm->tv, &this->tv, <))
 
48
                        new = &((*new)->rb_left);
 
49
                else
 
50
                        new = &((*new)->rb_right);
 
51
        }
 
52
 
 
53
        rb_link_node(&alarm->node, parent, new);
 
54
        rb_insert_color(&alarm->node, &alarm_root);
 
55
}
 
56
 
 
57
void add_alarm(struct alarm_block *alarm, unsigned long sc, unsigned long usc)
 
58
{
 
59
        struct timeval tv;
 
60
 
 
61
        del_alarm(alarm);
 
62
        alarm->tv.tv_sec = sc;
 
63
        alarm->tv.tv_usec = usc;
 
64
        gettimeofday(&tv, NULL);
 
65
        timeradd(&alarm->tv, &tv, &alarm->tv);
 
66
        __add_alarm(alarm);
 
67
}
 
68
 
 
69
void del_alarm(struct alarm_block *alarm)
 
70
{
 
71
        /* don't remove a non-inserted node */
 
72
        if (!RB_EMPTY_NODE(&alarm->node)) {
 
73
                rb_erase(&alarm->node, &alarm_root);
 
74
                RB_CLEAR_NODE(&alarm->node);
 
75
        }
 
76
}
 
77
 
 
78
int alarm_pending(struct alarm_block *alarm)
 
79
{
 
80
        if (RB_EMPTY_NODE(&alarm->node))
 
81
                return 0;
 
82
 
 
83
        return 1;
 
84
}
 
85
 
 
86
static struct timeval *
 
87
calculate_next_run(struct timeval *cand,
 
88
                   struct timeval *tv,
 
89
                   struct timeval *next_run)
 
90
{
 
91
        if (cand->tv_sec != LONG_MAX) {
 
92
                if (timercmp(cand, tv, >))
 
93
                        timersub(cand, tv, next_run);
 
94
                else {
 
95
                        /* loop again inmediately */
 
96
                        next_run->tv_sec = 0;
 
97
                        next_run->tv_usec = 0;
 
98
                }
 
99
                return next_run;
 
100
        }
 
101
        return NULL;
 
102
}
 
103
 
 
104
struct timeval *
 
105
get_next_alarm_run(struct timeval *next_run)
 
106
{
 
107
        struct rb_node *node;
 
108
        struct timeval tv;
 
109
 
 
110
        gettimeofday(&tv, NULL);
 
111
 
 
112
        node = rb_first(&alarm_root);
 
113
        if (node) {
 
114
                struct alarm_block *this;
 
115
                this = container_of(node, struct alarm_block, node);
 
116
                return calculate_next_run(&this->tv, &tv, next_run);
 
117
        }
 
118
        return NULL;
 
119
}
 
120
 
 
121
struct timeval *
 
122
do_alarm_run(struct timeval *next_run)
 
123
{
 
124
        struct list_head alarm_run_queue;
 
125
        struct rb_node *node;
 
126
        struct alarm_block *this;
 
127
        struct timeval tv;
 
128
 
 
129
        gettimeofday(&tv, NULL);
 
130
 
 
131
        INIT_LIST_HEAD(&alarm_run_queue);
 
132
        for (node = rb_first(&alarm_root); node; node = rb_next(node)) {
 
133
                this = container_of(node, struct alarm_block, node);
 
134
 
 
135
                if (timercmp(&this->tv, &tv, >))
 
136
                        break;
 
137
 
 
138
                list_add(&this->list, &alarm_run_queue);
 
139
        }
 
140
 
 
141
        list_for_each_entry(this, &alarm_run_queue, list) {
 
142
                rb_erase(&this->node, &alarm_root);
 
143
                RB_CLEAR_NODE(&this->node);
 
144
                this->function(this, this->data);
 
145
        }
 
146
 
 
147
        return get_next_alarm_run(next_run);
 
148
}