Debian Codegraph @

agent/mibgroup/disman/schedule/schedCore.c

raw
1 /*
2 * DisMan Schedule MIB:
3 * Core implementation of the schedule handling behaviour
4 */
5
6 #include <net-snmp/net-snmp-config.h>
7 #include <net-snmp/net-snmp-features.h>
8 #include <net-snmp/net-snmp-includes.h>
9 #include <net-snmp/agent/net-snmp-agent-includes.h>
10 #include "disman/schedule/schedCore.h"
11 #include "utilities/iquery.h"
12
13 netsnmp_feature_require(iquery);
14
15 netsnmp_feature_child_of(sched_nextrowtime, netsnmp_unused);
16
17 netsnmp_tdata *schedule_table;
18
19
20 #if !defined(HAVE_LOCALTIME_R) && !defined(localtime_r)
21 /*
22 * localtime_r() replacement for older MinGW versions.
23 * Note: this implementation is not thread-safe, while it should.
24 */
25 struct tm *
26 localtime_r(const time_t * timer, struct tm *result)
27 {
28 struct tm *result_p;
29
30 result_p = localtime(timer);
31 if (result && result_p)
32 *result = *result_p;
33 return result_p;
34 }
35 #endif
36
37
38 /*
39 * Initialize the container for the schedule table,
40 * regardless of which initialisation routine is called first.
41 */
42 void
43 init_schedule_container(void)
44 {
45 DEBUGMSGTL(("disman:schedule:init", "init schedule container\n"));
46 if (!schedule_table) {
47 schedule_table = netsnmp_tdata_create_table("schedTable", 0);
48 DEBUGMSGTL(("disman:schedule:init",
49 "create schedule container(%p)\n", schedule_table));
50 }
51 }
52
53 /** Initializes the schedCore module */
54 void
55 init_schedCore(void)
56 {
57 /*
58 * Create a table structure for the schedule table
59 * This will typically be registered by the schedTable module
60 */
61 DEBUGMSGTL(("disman:schedule:init", "Initializing core module\n"));
62 init_schedule_container();
63 }
64
65
66 /*
67 * Callback to invoke a scheduled action
68 */
69 static void
70 _sched_callback( unsigned int reg, void *magic )
71 {
72 struct schedTable_entry *entry = (struct schedTable_entry *)magic;
73 int ret;
74 netsnmp_variable_list assign;
75
76 if ( !entry ) {
77 DEBUGMSGTL(("disman:schedule:callback", "missing entry\n"));
78 return;
79 }
80 entry->schedLastRun = time(NULL);
81 entry->schedTriggers++;
82
83 DEBUGMSGTL(( "disman:schedule:callback", "assignment "));
84 DEBUGMSGOID(("disman:schedule:callback", entry->schedVariable,
85 entry->schedVariable_len));
86 DEBUGMSG(( "disman:schedule:callback", " = %ld\n", entry->schedValue));
87
88 memset(&assign, 0, sizeof(netsnmp_variable_list));
89 snmp_set_var_objid(&assign, entry->schedVariable, entry->schedVariable_len);
90 snmp_set_var_typed_value(&assign, ASN_INTEGER,
91 (u_char *)&entry->schedValue,
92 sizeof(entry->schedValue));
93
94 ret = netsnmp_query_set( &assign, entry->session );
95 if ( ret != SNMP_ERR_NOERROR ) {
96 DEBUGMSGTL(( "disman:schedule:callback",
97 "assignment failed (%d)\n", ret));
98 entry->schedFailures++;
99 entry->schedLastFailure = ret;
100 time ( &entry->schedLastFailed );
101 }
102
103 sched_nextTime( entry );
104 }
105
106
107 /*
108 * Internal utility routines to help interpret
109 * calendar-based schedule bit strings
110 */
111 static u_char _masks[] = { /* 0xff, */ 0x7f, 0x3f, 0x1f,
112 0x0f, 0x07, 0x03, 0x01, 0x00 };
113 static u_char _bits[] = { 0x80, 0x40, 0x20, 0x10,
114 0x08, 0x04, 0x02, 0x01 };
115
116 /*
117 * Are any of the bits set?
118 */
119 static int
120 _bit_allClear(const u_char *pattern, int len) {
121 int i;
122
123 for (i=0; i<len; i++) {
124 if ( pattern[i] != 0 )
125 return 0; /* At least one bit set */
126 }
127 return 1; /* All bits clear */
128 }
129
130 /*
131 * Is a particular bit set?
132 */
133 static int
134 _bit_set(const u_char *pattern, int bit) {
135 int major, minor;
136
137 major = bit/8;
138 minor = bit%8;
139 if ( pattern[major] & _bits[minor] ) {
140 return 1; /* Specified bit is set */
141 }
142 return 0; /* Bit not set */
143 }
144
145 /*
146 * What is the next bit set?
147 * (after a specified point)
148 */
149 static int
150 _bit_next(const u_char *pattern, int current, size_t len) {
151 char buf[ 8 ];
152 int major, minor, i, j;
153
154 /* Make a working copy of the bit pattern */
155 memset( buf, 0, 8 );
156 memcpy( buf, pattern, len );
157
158 /*
159 * If we're looking for the first bit after some point,
160 * then clear all earlier bits from the working copy.
161 */
162 if ( current > -1 ) {
163 major = current/8;
164 minor = current%8;
165 for ( i=0; i<major; i++ )
166 buf[i]=0;
167 buf[major] &= _masks[minor];
168 }
169
170 /*
171 * Look for the first bit that's set
172 */
173 for ( i=0; i<(int)len; i++ ) {
174 if ( buf[i] != 0 ) {
175 major = i*8;
176 for ( j=0; j<8; j++ ) {
177 if ( buf[i] & _bits[j] ) {
178 return major+j;
179 }
180 }
181 }
182 }
183 return -1; /* No next bit */
184 }
185
186
187 static int _daysPerMonth[] = { 31, 28, 31, 30,
188 31, 30, 31, 31,
189 30, 31, 30, 31, 29 };
190
191 static u_char _truncate[] = { 0xfe, 0xf0, 0xfe, 0xfc,
192 0xfe, 0xfc, 0xfe, 0xfe,
193 0xfc, 0xfe, 0xfc, 0xfe, 0xf8 };
194
195 /*
196 * What is the next day with a relevant bit set?
197 *
198 * Merge the forward and reverse day bits into a single
199 * pattern relevant for this particular month,
200 * and apply the standard _bit_next() call.
201 * Then check this result against the day of the week bits.
202 */
203 static int
204 _bit_next_day(const u_char *day_pattern, const u_char *weekday_pattern,
205 int day, int month, int year ) {
206 u_char buf[4];
207 union {
208 char buf2[4];
209 int int_val;
210 } rev;
211 int next_day, i;
212 struct tm tm_val;
213
214 /* Make a working copy of the forward day bits ... */
215 memset( buf, 0, 4 );
216 memcpy( buf, day_pattern, 4 );
217
218 /* ... and another (right-aligned) of the reverse day bits */
219 memset( rev.buf2, 0, 4 );
220 memcpy( rev.buf2, day_pattern+4, 4 );
221 rev.int_val >>= 2;
222 if ( buf[3] & 0x01 )
223 rev.buf2[0] |= 0x40;
224 if ( month == 3 || month == 5 ||
225 month == 8 || month == 10 )
226 rev.int_val >>= 1; /* April, June, September, November */
227 if ( month == 1 )
228 rev.int_val >>= 3; /* February */
229 if ( month == 12 )
230 rev.int_val >>= 2; /* February (leap year) */
231
232 /* Combine the two bit patterns, and truncate to the month end */
233 for ( i=0; i<4; i++ ) {
234 if ( rev.buf2[i] & 0x80 ) buf[3-i] |= 0x01;
235 if ( rev.buf2[i] & 0x40 ) buf[3-i] |= 0x02;
236 if ( rev.buf2[i] & 0x20 ) buf[3-i] |= 0x04;
237 if ( rev.buf2[i] & 0x10 ) buf[3-i] |= 0x08;
238 if ( rev.buf2[i] & 0x08 ) buf[3-i] |= 0x10;
239 if ( rev.buf2[i] & 0x04 ) buf[3-i] |= 0x20;
240 if ( rev.buf2[i] & 0x02 ) buf[3-i] |= 0x40;
241 if ( rev.buf2[i] & 0x01 ) buf[3-i] |= 0x80;
242 }
243
244 buf[3] &= _truncate[ month ];
245
246 next_day = day-1; /* tm_day is 1-based, not 0-based */
247 do {
248 next_day = _bit_next( buf, next_day, 4 );
249 if ( next_day < 0 )
250 return -1;
251
252 /*
253 * Calculate the day of the week, and
254 * check this against the weekday pattern
255 */
256 memset( &tm_val, 0, sizeof(struct tm));
257 tm_val.tm_mday = next_day+1;
258 tm_val.tm_mon = month;
259 tm_val.tm_year = year;
260 if (mktime(&tm_val) == (time_t)-1)
261 return -1;
262 } while ( !_bit_set( weekday_pattern, tm_val.tm_wday ));
263 return next_day+1; /* Convert back to 1-based list */
264 }
265
266
267 /*
268 * determine the time for the next scheduled action of a given entry
269 */
270 void
271 sched_nextTime( struct schedTable_entry *entry )
272 {
273 time_t now;
274 struct tm now_tm, next_tm;
275 int rev_day, mon;
276
277 time( &now );
278
279 if ( !entry ) {
280 DEBUGMSGTL(("disman:schedule:time", "missing entry\n"));
281 return;
282 }
283
284 if ( entry->schedCallbackID )
285 snmp_alarm_unregister( entry->schedCallbackID );
286
287 if (!(entry->flags & SCHEDULE_FLAG_ENABLED) ||
288 !(entry->flags & SCHEDULE_FLAG_ACTIVE)) {
289 DEBUGMSGTL(("disman:schedule:time", "inactive entry\n"));
290 return;
291 }
292
293 switch ( entry->schedType ) {
294 case SCHED_TYPE_PERIODIC:
295 if ( !entry->schedInterval ) {
296 DEBUGMSGTL(("disman:schedule:time", "periodic: no interval\n"));
297 return;
298 }
299 if ( entry->schedLastRun ) {
300 entry->schedNextRun = entry->schedLastRun +
301 entry->schedInterval;
302 } else {
303 entry->schedNextRun = now + entry->schedInterval;
304 }
305 DEBUGMSGTL(("disman:schedule:time", "periodic: (%ld) %s",
306 (long) entry->schedNextRun, ctime(&entry->schedNextRun)));
307 break;
308
309 case SCHED_TYPE_ONESHOT:
310 if ( entry->schedLastRun ) {
311 DEBUGMSGTL(("disman:schedule:time", "one-shot: expired (%ld) %s",
312 (long) entry->schedNextRun,
313 ctime(&entry->schedNextRun)));
314 return;
315 }
316 DEBUGMSGTL(("disman:schedule:time", "one-shot: fallthrough\n"));
317 /* FALL THROUGH */
318 case SCHED_TYPE_CALENDAR:
319 /*
320 * Check for complete time specification
321 * If any of the five fields have no bits set,
322 * the entry can't possibly match any time.
323 */
324 if ( _bit_allClear( entry->schedMinute, 8 ) ||
325 _bit_allClear( entry->schedHour, 3 ) ||
326 _bit_allClear( entry->schedDay, 4+4 ) ||
327 _bit_allClear( entry->schedMonth, 2 ) ||
328 _bit_allClear( entry->schedWeekDay, 1 )) {
329 DEBUGMSGTL(("disman:schedule:time", "calendar: incomplete spec\n"));
330 return;
331 }
332
333 /*
334 * Calculate the next run time:
335 *
336 * If the current Month, Day & Hour bits are set
337 * calculate the next specified minute
338 * If this fails (or the current Hour bit is not set)
339 * use the first specified minute,
340 * and calculate the next specified hour
341 * If this fails (or the current Day bit is not set)
342 * use the first specified minute and hour
343 * and calculate the next specified day (in this month)
344 * If this fails (or the current Month bit is not set)
345 * use the first specified minute and hour
346 * calculate the next specified month, and
347 * the first specified day (in that month)
348 */
349
350 (void) localtime_r( &now, &now_tm );
351 (void) localtime_r( &now, &next_tm );
352
353 next_tm.tm_mon=-1;
354 next_tm.tm_mday=-1;
355 next_tm.tm_hour=-1;
356 next_tm.tm_min=-1;
357 next_tm.tm_sec=0;
358 if ( _bit_set( entry->schedMonth, now_tm.tm_mon )) {
359 next_tm.tm_mon = now_tm.tm_mon;
360 rev_day = _daysPerMonth[ now_tm.tm_mon ] - now_tm.tm_mday;
361 if ( _bit_set( entry->schedWeekDay, now_tm.tm_wday ) &&
362 (_bit_set( entry->schedDay, now_tm.tm_mday-1 ) ||
363 _bit_set( entry->schedDay, 31+rev_day ))) {
364 next_tm.tm_mday = now_tm.tm_mday;
365
366 if ( _bit_set( entry->schedHour, now_tm.tm_hour )) {
367 next_tm.tm_hour = now_tm.tm_hour;
368 /* XXX - Check Fall timechange */
369 next_tm.tm_min = _bit_next( entry->schedMinute,
370 now_tm.tm_min, 8 );
371 } else {
372 next_tm.tm_min = -1;
373 }
374
375 if ( next_tm.tm_min == -1 ) {
376 next_tm.tm_min = _bit_next( entry->schedMinute, -1, 8 );
377 next_tm.tm_hour = _bit_next( entry->schedHour,
378 now_tm.tm_hour, 3 );
379 }
380 } else {
381 next_tm.tm_hour = -1;
382 }
383
384 if ( next_tm.tm_hour == -1 ) {
385 next_tm.tm_min = _bit_next( entry->schedMinute, -1, 8 );
386 next_tm.tm_hour = _bit_next( entry->schedHour, -1, 3 );
387 /* Handle leap years */
388 mon = now_tm.tm_mon;
389 if ( mon == 1 && (now_tm.tm_year%4 == 0) )
390 mon = 12;
391 next_tm.tm_mday = _bit_next_day( entry->schedDay,
392 entry->schedWeekDay,
393 now_tm.tm_mday,
394 mon, now_tm.tm_year );
395 }
396 } else {
397 next_tm.tm_min = _bit_next( entry->schedMinute, -1, 2 );
398 next_tm.tm_hour = _bit_next( entry->schedHour, -1, 3 );
399 next_tm.tm_mday = -1;
400 next_tm.tm_mon = now_tm.tm_mon;
401 }
402
403 while ( next_tm.tm_mday == -1 ) {
404 next_tm.tm_mon = _bit_next( entry->schedMonth,
405 next_tm.tm_mon, 2 );
406 if ( next_tm.tm_mon == -1 ) {
407 next_tm.tm_year++;
408 next_tm.tm_mon = _bit_next( entry->schedMonth,
409 -1, 2 );
410 }
411 /* Handle leap years */
412 mon = next_tm.tm_mon;
413 if ( mon == 1 && (next_tm.tm_year%4 == 0) )
414 mon = 12;
415 next_tm.tm_mday = _bit_next_day( entry->schedDay,
416 entry->schedWeekDay,
417 -1, mon, next_tm.tm_year );
418 /* XXX - catch infinite loop */
419 }
420
421 /* XXX - Check for Spring timechange */
422
423 /*
424 * 'next_tm' now contains the time for the next scheduled run
425 */
426 entry->schedNextRun = mktime( &next_tm );
427 DEBUGMSGTL(("disman:schedule:time", "calendar: (%ld) %s",
428 (long) entry->schedNextRun, ctime(&entry->schedNextRun)));
429 return;
430
431 default:
432 DEBUGMSGTL(("disman:schedule:time", "unknown type (%ld)\n",
433 entry->schedType));
434 return;
435 }
436 entry->schedCallbackID = snmp_alarm_register(
437 entry->schedNextRun - now,
438 0, _sched_callback, entry );
439 return;
440 }
441
442 #ifndef NETSNMP_FEATURE_REMOVE_SCHED_NEXTROWTIME
443 void
444 sched_nextRowTime( netsnmp_tdata_row *row )
445 {
446 sched_nextTime((struct schedTable_entry *) row->data );
447 }
448 #endif /* NETSNMP_FEATURE_REMOVE_SCHED_NEXTROWTIME */
449
450 /*
451 * create a new row in the table
452 */
453 netsnmp_tdata_row *
454 schedTable_createEntry(const char *schedOwner, const char *schedName)
455 {
456 struct schedTable_entry *entry;
457 netsnmp_tdata_row *row;
458 int len;
459
460 DEBUGMSGTL(("disman:schedule:entry", "creating entry (%s, %s)\n",
461 schedOwner, schedName));
462 entry = SNMP_MALLOC_TYPEDEF(struct schedTable_entry);
463 if (!entry)
464 return NULL;
465
466 row = netsnmp_tdata_create_row();
467 if (!row) {
468 SNMP_FREE(entry);
469 return NULL;
470 }
471 row->data = entry;
472 /*
473 * Set the indexing for this entry, both in the row
474 * data structure, and in the table_data helper.
475 */
476 if (schedOwner) {
477 len = strlen(schedOwner);
478 if (len > sizeof(entry->schedOwner))
479 len = sizeof(entry->schedOwner);
480 memcpy(entry->schedOwner, schedOwner, len);
481 netsnmp_tdata_row_add_index(row, ASN_OCTET_STR, entry->schedOwner, len);
482 }
483 else
484 netsnmp_tdata_row_add_index(row, ASN_OCTET_STR, "", 0 );
485
486 len = strlen(schedName);
487 if (len > sizeof(entry->schedName))
488 len = sizeof(entry->schedName);
489 memcpy(entry->schedName, schedName, len);
490 netsnmp_tdata_row_add_index(row, ASN_OCTET_STR, entry->schedName, len);
491 /*
492 * Set the (non-zero) default values in the row data structure.
493 */
494 entry->schedType = SCHED_TYPE_PERIODIC;
495 entry->schedVariable_len = 2; /* .0.0 */
496
497 netsnmp_tdata_add_row(schedule_table, row);
498 return row;
499 }
500
501
502 /*
503 * remove a row from the table
504 */
505 void
506 schedTable_removeEntry(netsnmp_tdata_row *row)
507 {
508 struct schedTable_entry *entry;
509
510 if (!row || !row->data) {
511 DEBUGMSGTL(("disman:schedule:entry", "remove: missing entry\n"));
512 return; /* Nothing to remove */
513 }
514 entry = (struct schedTable_entry *)
515 netsnmp_tdata_remove_and_delete_row(schedule_table, row);
516 if (entry) {
517 DEBUGMSGTL(("disman:schedule:entry", "remove entry (%s, %s)\n",
518 entry->schedOwner, entry->schedName));
519 SNMP_FREE(entry);
520 }
521 }