1 //==========================================================================
3 // ./lib/current/src/snmp_alarm.c
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.
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.
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
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.
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.
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.
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####
41 // -------------------------------------------
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.
49 // The release used was version 4.1.2 of May 2000. "ucd-snmp-4.1.2"
50 // -------------------------------------------
52 //####UCDSNMPCOPYRIGHTEND####
53 //==========================================================================
54 //#####DESCRIPTIONBEGIN####
59 // Purpose: Port of UCD-SNMP distribution to eCos.
63 //####DESCRIPTIONEND####
65 //==========================================================================
66 /********************************************************************
67 Copyright 1989, 1991, 1992 by Carnegie Mellon University
70 Copyright 1996, 1998, 1999, 2000 The Regents of the University of California
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
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
103 #if HAVE_NETINET_IN_H
104 #include <netinet/in.h>
107 #if TIME_WITH_SYS_TIME
109 # include <sys/timeb.h>
111 # include <sys/time.h>
116 # include <sys/time.h>
130 #include "snmp_api.h"
131 #include "snmp_debug.h"
133 #include "default_store.h"
134 #include "callback.h"
135 #include "snmp_alarm.h"
137 static struct snmp_alarm *thealarms;
138 static int start_alarms = 0;
139 static unsigned int regnum = 1;
142 init_alarm_post_config(int majorid, int minorid, void *serverarg,
146 return SNMPERR_SUCCESS;
150 init_snmp_alarm(void) {
152 snmp_register_callback(SNMP_CALLBACK_LIBRARY, SNMP_CALLBACK_POST_READ_CONFIG,
153 init_alarm_post_config, NULL);
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 */
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;
171 /* single time call, remove it */
172 snmp_alarm_unregister(alrm->clientreg);
178 snmp_alarm_unregister(unsigned int clientreg) {
179 struct snmp_alarm *sa_ptr, *alrm=NULL;
181 if (thealarms == NULL)
184 if (clientreg == thealarms->clientreg) {
186 thealarms = alrm->next;
189 for(sa_ptr = thealarms;
190 sa_ptr != NULL && sa_ptr->next->clientreg != clientreg;
191 sa_ptr = sa_ptr->next);
195 sa_ptr->next = sa_ptr->next->next;
200 /* Note: do not free the clientarg, its the clients responsibility */
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)
219 struct snmp_alarm *sa_ptr;
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"));
225 sa_ptr = sa_find_next();
228 if (sa_ptr->nextcall <= time(NULL)) {
229 DEBUGMSGTL(("snmp_alarm_run_alarms"," running alarm %d\n",
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);
240 DEBUGMSGTL(("snmp_alarm_run_alarms","Done.\n"));
245 alarm_handler(int a) {
251 get_next_alarm_delay_time(void) {
252 struct snmp_alarm *sa_ptr;
255 sa_ptr = sa_find_next();
257 nexttime = sa_ptr->nextcall - time(NULL);
259 nexttime = 1; /* occurred already, return 1 second */
267 int nexttime = get_next_alarm_delay_time();
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) {
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);
282 DEBUGMSGTL(("snmp_alarm_set_an_alarm","no alarms found to handle\n"));
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));
294 sa_pptr = &thealarms;
297 *sa_pptr = SNMP_MALLOC_STRUCT(snmp_alarm);
298 if (*sa_pptr == NULL)
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);
308 DEBUGMSGTL(("snmp_alarm_register","registered alarm %d, secends=%d, flags=%d\n",
309 (*sa_pptr)->clientreg, (*sa_pptr)->seconds, (*sa_pptr)->flags));
313 return (*sa_pptr)->clientreg;