1 |
27 |
unneback |
//==========================================================================
|
2 |
|
|
//
|
3 |
|
|
// ./lib/current/src/snmp_alarm.c
|
4 |
|
|
//
|
5 |
|
|
//
|
6 |
|
|
//==========================================================================
|
7 |
|
|
//####ECOSGPLCOPYRIGHTBEGIN####
|
8 |
|
|
// -------------------------------------------
|
9 |
|
|
// This file is part of eCos, the Embedded Configurable Operating System.
|
10 |
|
|
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
|
11 |
|
|
//
|
12 |
|
|
// eCos is free software; you can redistribute it and/or modify it under
|
13 |
|
|
// the terms of the GNU General Public License as published by the Free
|
14 |
|
|
// Software Foundation; either version 2 or (at your option) any later version.
|
15 |
|
|
//
|
16 |
|
|
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
|
17 |
|
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
18 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
19 |
|
|
// for more details.
|
20 |
|
|
//
|
21 |
|
|
// You should have received a copy of the GNU General Public License along
|
22 |
|
|
// with eCos; if not, write to the Free Software Foundation, Inc.,
|
23 |
|
|
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
|
24 |
|
|
//
|
25 |
|
|
// As a special exception, if other files instantiate templates or use macros
|
26 |
|
|
// or inline functions from this file, or you compile this file and link it
|
27 |
|
|
// with other works to produce a work based on this file, this file does not
|
28 |
|
|
// by itself cause the resulting work to be covered by the GNU General Public
|
29 |
|
|
// License. However the source code for this file must still be made available
|
30 |
|
|
// in accordance with section (3) of the GNU General Public License.
|
31 |
|
|
//
|
32 |
|
|
// This exception does not invalidate any other reasons why a work based on
|
33 |
|
|
// this file might be covered by the GNU General Public License.
|
34 |
|
|
//
|
35 |
|
|
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
|
36 |
|
|
// at http://sources.redhat.com/ecos/ecos-license/
|
37 |
|
|
// -------------------------------------------
|
38 |
|
|
//####ECOSGPLCOPYRIGHTEND####
|
39 |
|
|
//####UCDSNMPCOPYRIGHTBEGIN####
|
40 |
|
|
//
|
41 |
|
|
// -------------------------------------------
|
42 |
|
|
//
|
43 |
|
|
// Portions of this software may have been derived from the UCD-SNMP
|
44 |
|
|
// project, <http://ucd-snmp.ucdavis.edu/> from the University of
|
45 |
|
|
// California at Davis, which was originally based on the Carnegie Mellon
|
46 |
|
|
// University SNMP implementation. Portions of this software are therefore
|
47 |
|
|
// covered by the appropriate copyright disclaimers included herein.
|
48 |
|
|
//
|
49 |
|
|
// The release used was version 4.1.2 of May 2000. "ucd-snmp-4.1.2"
|
50 |
|
|
// -------------------------------------------
|
51 |
|
|
//
|
52 |
|
|
//####UCDSNMPCOPYRIGHTEND####
|
53 |
|
|
//==========================================================================
|
54 |
|
|
//#####DESCRIPTIONBEGIN####
|
55 |
|
|
//
|
56 |
|
|
// Author(s): hmt
|
57 |
|
|
// Contributors: hmt
|
58 |
|
|
// Date: 2000-05-30
|
59 |
|
|
// Purpose: Port of UCD-SNMP distribution to eCos.
|
60 |
|
|
// Description:
|
61 |
|
|
//
|
62 |
|
|
//
|
63 |
|
|
//####DESCRIPTIONEND####
|
64 |
|
|
//
|
65 |
|
|
//==========================================================================
|
66 |
|
|
/********************************************************************
|
67 |
|
|
Copyright 1989, 1991, 1992 by Carnegie Mellon University
|
68 |
|
|
|
69 |
|
|
Derivative Work -
|
70 |
|
|
Copyright 1996, 1998, 1999, 2000 The Regents of the University of California
|
71 |
|
|
|
72 |
|
|
All Rights Reserved
|
73 |
|
|
|
74 |
|
|
Permission to use, copy, modify and distribute this software and its
|
75 |
|
|
documentation for any purpose and without fee is hereby granted,
|
76 |
|
|
provided that the above copyright notice appears in all copies and
|
77 |
|
|
that both that copyright notice and this permission notice appear in
|
78 |
|
|
supporting documentation, and that the name of CMU and The Regents of
|
79 |
|
|
the University of California not be used in advertising or publicity
|
80 |
|
|
pertaining to distribution of the software without specific written
|
81 |
|
|
permission.
|
82 |
|
|
|
83 |
|
|
CMU AND THE REGENTS OF THE UNIVERSITY OF CALIFORNIA DISCLAIM ALL
|
84 |
|
|
WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED
|
85 |
|
|
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL CMU OR
|
86 |
|
|
THE REGENTS OF THE UNIVERSITY OF CALIFORNIA BE LIABLE FOR ANY SPECIAL,
|
87 |
|
|
INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
|
88 |
|
|
FROM THE LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
|
89 |
|
|
CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
90 |
|
|
CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
91 |
|
|
*********************************************************************/
|
92 |
|
|
/* snmp_alarm.c: generic library based alarm timers for various parts
|
93 |
|
|
of an application */
|
94 |
|
|
|
95 |
|
|
#include <config.h>
|
96 |
|
|
#if HAVE_UNISTD_H
|
97 |
|
|
#include <unistd.h>
|
98 |
|
|
#endif
|
99 |
|
|
#include <signal.h>
|
100 |
|
|
#if HAVE_STDLIB_H
|
101 |
|
|
#include <stdlib.h>
|
102 |
|
|
#endif
|
103 |
|
|
#if HAVE_NETINET_IN_H
|
104 |
|
|
#include <netinet/in.h>
|
105 |
|
|
#endif
|
106 |
|
|
|
107 |
|
|
#if TIME_WITH_SYS_TIME
|
108 |
|
|
# ifdef WIN32
|
109 |
|
|
# include <sys/timeb.h>
|
110 |
|
|
# else
|
111 |
|
|
# include <sys/time.h>
|
112 |
|
|
# endif
|
113 |
|
|
# include <time.h>
|
114 |
|
|
#else
|
115 |
|
|
# if HAVE_SYS_TIME_H
|
116 |
|
|
# include <sys/time.h>
|
117 |
|
|
# else
|
118 |
|
|
# include <time.h>
|
119 |
|
|
# endif
|
120 |
|
|
#endif
|
121 |
|
|
#if HAVE_WINSOCK_H
|
122 |
|
|
#include <winsock.h>
|
123 |
|
|
#endif
|
124 |
|
|
|
125 |
|
|
#if HAVE_DMALLOC_H
|
126 |
|
|
#include <dmalloc.h>
|
127 |
|
|
#endif
|
128 |
|
|
|
129 |
|
|
#include "asn1.h"
|
130 |
|
|
#include "snmp_api.h"
|
131 |
|
|
#include "snmp_debug.h"
|
132 |
|
|
#include "tools.h"
|
133 |
|
|
#include "default_store.h"
|
134 |
|
|
#include "callback.h"
|
135 |
|
|
#include "snmp_alarm.h"
|
136 |
|
|
|
137 |
|
|
static struct snmp_alarm *thealarms;
|
138 |
|
|
static int start_alarms = 0;
|
139 |
|
|
static unsigned int regnum = 1;
|
140 |
|
|
|
141 |
|
|
int
|
142 |
|
|
init_alarm_post_config(int majorid, int minorid, void *serverarg,
|
143 |
|
|
void *clientarg) {
|
144 |
|
|
start_alarms = 1;
|
145 |
|
|
set_an_alarm();
|
146 |
|
|
return SNMPERR_SUCCESS;
|
147 |
|
|
}
|
148 |
|
|
|
149 |
|
|
void
|
150 |
|
|
init_snmp_alarm(void) {
|
151 |
|
|
start_alarms = 0;
|
152 |
|
|
snmp_register_callback(SNMP_CALLBACK_LIBRARY, SNMP_CALLBACK_POST_READ_CONFIG,
|
153 |
|
|
init_alarm_post_config, NULL);
|
154 |
|
|
}
|
155 |
|
|
|
156 |
|
|
void
|
157 |
|
|
sa_update_entry(struct snmp_alarm *alrm) {
|
158 |
|
|
if (alrm->seconds == 0) {
|
159 |
|
|
DEBUGMSGTL(("snmp_alarm_update_entry","illegal 0 length alarm timer specified\n"));
|
160 |
|
|
return; /* illegal */
|
161 |
|
|
}
|
162 |
|
|
if (alrm->lastcall == 0) {
|
163 |
|
|
/* never been called yet, call seconds from now. */
|
164 |
|
|
alrm->lastcall = time(NULL);
|
165 |
|
|
alrm->nextcall = alrm->lastcall + alrm->seconds;
|
166 |
|
|
} else if (alrm->nextcall == 0) {
|
167 |
|
|
/* We've been called but not reset for the next? call */
|
168 |
|
|
if ((alrm->flags & SA_REPEAT) == SA_REPEAT) {
|
169 |
|
|
alrm->nextcall = alrm->lastcall + alrm->seconds;
|
170 |
|
|
} else {
|
171 |
|
|
/* single time call, remove it */
|
172 |
|
|
snmp_alarm_unregister(alrm->clientreg);
|
173 |
|
|
}
|
174 |
|
|
}
|
175 |
|
|
}
|
176 |
|
|
|
177 |
|
|
void
|
178 |
|
|
snmp_alarm_unregister(unsigned int clientreg) {
|
179 |
|
|
struct snmp_alarm *sa_ptr, *alrm=NULL;
|
180 |
|
|
|
181 |
|
|
if (thealarms == NULL)
|
182 |
|
|
return;
|
183 |
|
|
|
184 |
|
|
if (clientreg == thealarms->clientreg) {
|
185 |
|
|
alrm = thealarms;
|
186 |
|
|
thealarms = alrm->next;
|
187 |
|
|
}
|
188 |
|
|
else {
|
189 |
|
|
for(sa_ptr = thealarms;
|
190 |
|
|
sa_ptr != NULL && sa_ptr->next->clientreg != clientreg;
|
191 |
|
|
sa_ptr = sa_ptr->next);
|
192 |
|
|
if (sa_ptr) {
|
193 |
|
|
if (sa_ptr->next) {
|
194 |
|
|
alrm = sa_ptr->next;
|
195 |
|
|
sa_ptr->next = sa_ptr->next->next;
|
196 |
|
|
}
|
197 |
|
|
}
|
198 |
|
|
}
|
199 |
|
|
|
200 |
|
|
/* Note: do not free the clientarg, its the clients responsibility */
|
201 |
|
|
if (alrm)
|
202 |
|
|
free(alrm);
|
203 |
|
|
}
|
204 |
|
|
|
205 |
|
|
|
206 |
|
|
struct snmp_alarm *
|
207 |
|
|
sa_find_next(void) {
|
208 |
|
|
struct snmp_alarm *sa_ptr, *sa_ret = NULL;
|
209 |
|
|
for(sa_ptr = thealarms; sa_ptr != NULL; sa_ptr = sa_ptr->next) {
|
210 |
|
|
if (sa_ret == NULL || sa_ptr->nextcall < sa_ret->nextcall)
|
211 |
|
|
sa_ret = sa_ptr;
|
212 |
|
|
}
|
213 |
|
|
return sa_ret;
|
214 |
|
|
}
|
215 |
|
|
|
216 |
|
|
void
|
217 |
|
|
run_alarms(void) {
|
218 |
|
|
int done=0;
|
219 |
|
|
struct snmp_alarm *sa_ptr;
|
220 |
|
|
|
221 |
|
|
/* loop through everything we have repeatedly looking for the next
|
222 |
|
|
thing to call until all events are finally in the future again */
|
223 |
|
|
DEBUGMSGTL(("snmp_alarm_run_alarms","looking for alarms to run...\n"));
|
224 |
|
|
while(done == 0) {
|
225 |
|
|
sa_ptr = sa_find_next();
|
226 |
|
|
if (sa_ptr == NULL)
|
227 |
|
|
return;
|
228 |
|
|
if (sa_ptr->nextcall <= time(NULL)) {
|
229 |
|
|
DEBUGMSGTL(("snmp_alarm_run_alarms"," running alarm %d\n",
|
230 |
|
|
sa_ptr->clientreg));
|
231 |
|
|
(*(sa_ptr->thecallback))(sa_ptr->clientreg, sa_ptr->clientarg);
|
232 |
|
|
DEBUGMSGTL(("snmp_alarm_run_alarms"," ... done\n"));
|
233 |
|
|
sa_ptr->lastcall = time(NULL);
|
234 |
|
|
sa_ptr->nextcall = 0;
|
235 |
|
|
sa_update_entry(sa_ptr);
|
236 |
|
|
} else {
|
237 |
|
|
done = 1;
|
238 |
|
|
}
|
239 |
|
|
}
|
240 |
|
|
DEBUGMSGTL(("snmp_alarm_run_alarms","Done.\n"));
|
241 |
|
|
}
|
242 |
|
|
|
243 |
|
|
|
244 |
|
|
RETSIGTYPE
|
245 |
|
|
alarm_handler(int a) {
|
246 |
|
|
run_alarms();
|
247 |
|
|
set_an_alarm();
|
248 |
|
|
}
|
249 |
|
|
|
250 |
|
|
int
|
251 |
|
|
get_next_alarm_delay_time(void) {
|
252 |
|
|
struct snmp_alarm *sa_ptr;
|
253 |
|
|
int nexttime = 0;
|
254 |
|
|
|
255 |
|
|
sa_ptr = sa_find_next();
|
256 |
|
|
if (sa_ptr) {
|
257 |
|
|
nexttime = sa_ptr->nextcall - time(NULL);
|
258 |
|
|
if (nexttime <= 0)
|
259 |
|
|
nexttime = 1; /* occurred already, return 1 second */
|
260 |
|
|
}
|
261 |
|
|
return nexttime;
|
262 |
|
|
}
|
263 |
|
|
|
264 |
|
|
|
265 |
|
|
void
|
266 |
|
|
set_an_alarm(void) {
|
267 |
|
|
int nexttime = get_next_alarm_delay_time();
|
268 |
|
|
|
269 |
|
|
/* we don't use signals if they asked us nicely not to. It's
|
270 |
|
|
expected they'll check the next alarm time and do their own
|
271 |
|
|
calling of run_alarms(). */
|
272 |
|
|
if (!ds_get_boolean(DS_LIBRARY_ID, DS_LIB_ALARM_DONT_USE_SIG) && nexttime) {
|
273 |
|
|
#ifndef WIN32
|
274 |
|
|
#ifdef SIGALRM
|
275 |
|
|
//FIXMEHMTHMT alarm(nexttime);
|
276 |
|
|
DEBUGMSGTL(("snmp_alarm_set_an_alarm","setting an alarm for %d seconds from now\n",nexttime));
|
277 |
|
|
signal(SIGALRM, alarm_handler);
|
278 |
|
|
#endif /* SIGALRM */
|
279 |
|
|
#endif
|
280 |
|
|
|
281 |
|
|
} else {
|
282 |
|
|
DEBUGMSGTL(("snmp_alarm_set_an_alarm","no alarms found to handle\n"));
|
283 |
|
|
}
|
284 |
|
|
}
|
285 |
|
|
|
286 |
|
|
unsigned int
|
287 |
|
|
snmp_alarm_register(unsigned int when, unsigned int flags,
|
288 |
|
|
SNMPAlarmCallback *thecallback, void *clientarg) {
|
289 |
|
|
struct snmp_alarm **sa_pptr;
|
290 |
|
|
if (thealarms != NULL) {
|
291 |
|
|
for(sa_pptr = &thealarms; (*sa_pptr) != NULL;
|
292 |
|
|
sa_pptr = &((*sa_pptr)->next));
|
293 |
|
|
} else {
|
294 |
|
|
sa_pptr = &thealarms;
|
295 |
|
|
}
|
296 |
|
|
|
297 |
|
|
*sa_pptr = SNMP_MALLOC_STRUCT(snmp_alarm);
|
298 |
|
|
if (*sa_pptr == NULL)
|
299 |
|
|
return 0;
|
300 |
|
|
|
301 |
|
|
(*sa_pptr)->seconds = when;
|
302 |
|
|
(*sa_pptr)->flags = flags;
|
303 |
|
|
(*sa_pptr)->clientarg = clientarg;
|
304 |
|
|
(*sa_pptr)->thecallback = thecallback;
|
305 |
|
|
(*sa_pptr)->clientreg = regnum++;
|
306 |
|
|
sa_update_entry(*sa_pptr);
|
307 |
|
|
|
308 |
|
|
DEBUGMSGTL(("snmp_alarm_register","registered alarm %d, secends=%d, flags=%d\n",
|
309 |
|
|
(*sa_pptr)->clientreg, (*sa_pptr)->seconds, (*sa_pptr)->flags));
|
310 |
|
|
|
311 |
|
|
if (start_alarms)
|
312 |
|
|
set_an_alarm();
|
313 |
|
|
return (*sa_pptr)->clientreg;
|
314 |
|
|
}
|