Coverage Report

Created: 2024-07-27 06:05

/src/net-snmp/snmplib/callback.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * callback.c: A generic callback mechanism 
3
 */
4
/* Portions of this file are subject to the following copyright(s).  See
5
 * the Net-SNMP's COPYING file for more details and other copyrights
6
 * that may apply:
7
 */
8
/*
9
 * Portions of this file are copyrighted by:
10
 * Copyright © 2003 Sun Microsystems, Inc. All rights reserved.
11
 * Use is subject to license terms specified in the COPYING file
12
 * distributed with the Net-SNMP package.
13
 */
14
/** @defgroup callback A generic callback mechanism 
15
 *  @ingroup library
16
 * 
17
 *  @{
18
 */
19
#include <net-snmp/net-snmp-config.h>
20
#include <net-snmp/net-snmp-features.h>
21
#include <sys/types.h>
22
#include <stdio.h>
23
#ifdef HAVE_STDLIB_H
24
#include <stdlib.h>
25
#endif
26
#ifdef HAVE_NETINET_IN_H
27
#include <netinet/in.h>
28
#endif
29
#ifdef HAVE_STRING_H
30
#include <string.h>
31
#else
32
#include <strings.h>
33
#endif
34
35
#ifdef HAVE_UNISTD_H
36
#include <unistd.h>
37
#endif
38
39
#ifdef HAVE_SYS_SOCKET_H
40
#include <sys/socket.h>
41
#endif
42
#if !defined(mingw32) && defined(HAVE_SYS_TIME_H)
43
#include <sys/time.h>
44
#endif
45
46
#include <net-snmp/types.h>
47
#include <net-snmp/output_api.h>
48
#include <net-snmp/utilities.h>
49
50
#include <net-snmp/library/callback.h>
51
#include <net-snmp/library/snmp_api.h>
52
53
netsnmp_feature_child_of(callbacks_all, libnetsnmp);
54
55
netsnmp_feature_child_of(callback_count, callbacks_all);
56
netsnmp_feature_child_of(callback_list, callbacks_all);
57
58
/*
59
 * the inline callback methods use major/minor to index into arrays.
60
 * all users in this function do range checking before calling these
61
 * functions, so it is redundant for them to check again. But if you
62
 * want to be paranoid, define this var, and additional range checks
63
 * will be performed.
64
 * #define NETSNMP_PARANOID_LEVEL_HIGH 1 
65
 */
66
67
static int _callback_need_init = 1;
68
static struct snmp_gen_callback
69
               *thecallbacks[MAX_CALLBACK_IDS][MAX_CALLBACK_SUBIDS];
70
71
#define CALLBACK_NAME_LOGGING 1
72
#ifdef CALLBACK_NAME_LOGGING
73
static const char *types[MAX_CALLBACK_IDS] = { "LIB", "APP" };
74
static const char *lib[MAX_CALLBACK_SUBIDS] = {
75
    "POST_READ_CONFIG", /* 0 */
76
    "STORE_DATA", /* 1 */
77
    "SHUTDOWN", /* 2 */
78
    "POST_PREMIB_READ_CONFIG", /* 3 */
79
    "LOGGING", /* 4 */
80
    "SESSION_INIT", /* 5 */
81
    NULL, /* 6 */
82
    NULL, /* 7 */
83
    NULL, /* 8 */
84
    NULL, /* 9 */
85
    NULL, /* 10 */
86
    NULL, /* 11 */
87
    NULL, /* 12 */
88
    NULL, /* 13 */
89
    NULL, /* 14 */
90
    NULL /* 15 */
91
};
92
#endif
93
94
/*
95
 * extremely simplistic locking, just to find problems were the
96
 * callback list is modified while being traversed. Not intended
97
 * to do any real protection, or in any way imply that this code
98
 * has been evaluated for use in a multi-threaded environment.
99
 * In 5.2, it was a single lock. For 5.3, it has been updated to
100
 * a lock per callback, since a particular callback may trigger
101
 * registration/unregistration of other callbacks (eg AgentX
102
 * subagents do this).
103
 */
104
#define LOCK_PER_CALLBACK_SUBID 1
105
#ifdef LOCK_PER_CALLBACK_SUBID
106
static int _locks[MAX_CALLBACK_IDS][MAX_CALLBACK_SUBIDS];
107
0
#define CALLBACK_LOCK(maj,min) ++_locks[maj][min]
108
0
#define CALLBACK_UNLOCK(maj,min) --_locks[maj][min]
109
0
#define CALLBACK_LOCK_COUNT(maj,min) _locks[maj][min]
110
#else
111
static int _lock;
112
#define CALLBACK_LOCK(maj,min) ++_lock
113
#define CALLBACK_UNLOCK(maj,min) --_lock
114
#define CALLBACK_LOCK_COUNT(maj,min) _lock
115
#endif
116
117
NETSNMP_STATIC_INLINE int
118
_callback_lock(int major, int minor, const char* warn, int do_assert)
119
0
{
120
0
    int lock_holded=0;
121
0
    NETSNMP_SELECT_TIMEVAL lock_time;
122
123
#ifdef NETSNMP_PARANOID_LEVEL_HIGH
124
    if (major >= MAX_CALLBACK_IDS || minor >= MAX_CALLBACK_SUBIDS) {
125
        netsnmp_assert("bad callback id");
126
        return 1;
127
    }
128
#endif
129
    
130
0
#ifdef CALLBACK_NAME_LOGGING
131
0
    DEBUGMSGTL(("9:callback:lock", "locked (%s,%s)\n",
132
0
                types[major], (SNMP_CALLBACK_LIBRARY == major) ?
133
0
                SNMP_STRORNULL(lib[minor]) : "null"));
134
0
#endif
135
0
    while (CALLBACK_LOCK_COUNT(major,minor) >= 1 && ++lock_holded < 100) {
136
0
  lock_time.tv_sec = 0;
137
0
  lock_time.tv_usec = 1000;
138
0
  select(0, NULL, NULL, NULL, &lock_time);
139
0
    }
140
141
0
    if(lock_holded >= 100) {
142
0
        if (NULL != warn)
143
0
            snmp_log(LOG_WARNING,
144
0
                     "lock in _callback_lock sleeps more than 100 milliseconds in %s\n", warn);
145
0
        if (do_assert)
146
0
            netsnmp_assert(lock_holded < 100);
147
        
148
0
        return 1;
149
0
    }
150
151
0
    CALLBACK_LOCK(major,minor);
152
0
    return 0;
153
0
}
154
155
NETSNMP_STATIC_INLINE void
156
_callback_unlock(int major, int minor)
157
0
{
158
#ifdef NETSNMP_PARANOID_LEVEL_HIGH
159
    if (major >= MAX_CALLBACK_IDS || minor >= MAX_CALLBACK_SUBIDS) {
160
        netsnmp_assert("bad callback id");
161
        return;
162
    }
163
#endif
164
    
165
0
    CALLBACK_UNLOCK(major,minor);
166
167
0
#ifdef CALLBACK_NAME_LOGGING
168
0
    DEBUGMSGTL(("9:callback:lock", "unlocked (%s,%s)\n",
169
0
                types[major], (SNMP_CALLBACK_LIBRARY == major) ?
170
0
                SNMP_STRORNULL(lib[minor]) : "null"));
171
0
#endif
172
0
}
173
174
175
/*
176
 * the chicken. or the egg.  You pick. 
177
 */
178
void
179
init_callbacks(void)
180
0
{
181
    /*
182
     * (poses a problem if you put init_callbacks() inside of
183
     * init_snmp() and then want the app to register a callback before
184
     * init_snmp() is called in the first place.  -- Wes 
185
     */
186
0
    if (0 == _callback_need_init)
187
0
        return;
188
    
189
0
    _callback_need_init = 0;
190
    
191
0
    memset(thecallbacks, 0, sizeof(thecallbacks)); 
192
0
#ifdef LOCK_PER_CALLBACK_SUBID
193
0
    memset(_locks, 0, sizeof(_locks));
194
#else
195
    _lock = 0;
196
#endif
197
    
198
0
    DEBUGMSGTL(("callback", "initialized\n"));
199
0
}
200
201
/**
202
 * This function registers a generic callback function.  The major and
203
 * minor values are used to set the new_callback function into a global 
204
 * static multi-dimensional array of type struct snmp_gen_callback.  
205
 * The function makes sure to append this callback function at the end
206
 * of the link list, snmp_gen_callback->next.
207
 *
208
 * @param major is the SNMP callback major type used
209
 *    - SNMP_CALLBACK_LIBRARY
210
 *              - SNMP_CALLBACK_APPLICATION
211
 *
212
 * @param minor is the SNMP callback minor type used
213
 *    - SNMP_CALLBACK_POST_READ_CONFIG
214
 *    - SNMP_CALLBACK_STORE_DATA          
215
 *    - SNMP_CALLBACK_SHUTDOWN            
216
 *    - SNMP_CALLBACK_POST_PREMIB_READ_CONFIG 
217
 *    - SNMP_CALLBACK_LOGGING     
218
 *    - SNMP_CALLBACK_SESSION_INIT         
219
 *
220
 * @param new_callback is the callback function that is registered.
221
 *
222
 * @param arg when not NULL is a void pointer used whenever new_callback 
223
 *  function is exercised. Ownership is transferred to the twodimensional
224
 *      thecallbacks[][] array. The function clear_callback() will deallocate
225
 *      the memory pointed at by calling free().
226
 *
227
 * @return 
228
 *  Returns SNMPERR_GENERR if major is >= MAX_CALLBACK_IDS or minor is >=
229
 *  MAX_CALLBACK_SUBIDS or a snmp_gen_callback pointer could not be 
230
 *  allocated, otherwise SNMPERR_SUCCESS is returned.
231
 *  - \#define MAX_CALLBACK_IDS    2
232
 *  - \#define MAX_CALLBACK_SUBIDS 16
233
 *
234
 * @see snmp_call_callbacks
235
 * @see snmp_unregister_callback
236
 */
237
int
238
snmp_register_callback(int major, int minor, SNMPCallback * new_callback,
239
                       void *arg)
240
0
{
241
0
    return netsnmp_register_callback( major, minor, new_callback, arg,
242
0
                                      NETSNMP_CALLBACK_DEFAULT_PRIORITY);
243
0
}
244
245
/**
246
 * Register a callback function.
247
 *
248
 * @param major        Major callback event type.
249
 * @param minor        Minor callback event type.
250
 * @param new_callback Callback function being registered.
251
 * @param arg          Argument that will be passed to the callback function.
252
 * @param priority     Handler invocation priority. When multiple handlers have
253
 *   been registered for the same (major, minor) callback event type, handlers
254
 *   with the numerically lowest priority will be invoked first. Handlers with
255
 *   identical priority are invoked in the order they have been registered.
256
 *
257
 * @see snmp_register_callback
258
 */
259
int
260
netsnmp_register_callback(int major, int minor, SNMPCallback * new_callback,
261
                          void *arg, int priority)
262
0
{
263
0
    if (major >= MAX_CALLBACK_IDS || minor >= MAX_CALLBACK_SUBIDS) {
264
0
        return SNMPERR_GENERR;
265
0
    }
266
267
0
    struct snmp_gen_callback *newscp = NULL, *scp = NULL;
268
0
    struct snmp_gen_callback **prevNext = &(thecallbacks[major][minor]);
269
270
0
    if (_callback_need_init)
271
0
        init_callbacks();
272
273
0
    _callback_lock(major,minor, "netsnmp_register_callback", 1);
274
    
275
0
    if ((newscp = SNMP_MALLOC_STRUCT(snmp_gen_callback)) == NULL) {
276
0
        _callback_unlock(major,minor);
277
0
        return SNMPERR_GENERR;
278
0
    } else {
279
0
        newscp->priority = priority;
280
0
        newscp->sc_client_arg = arg;
281
0
        newscp->sc_callback = new_callback;
282
0
        newscp->next = NULL;
283
284
0
        for (scp = thecallbacks[major][minor]; scp != NULL;
285
0
             scp = scp->next) {
286
0
            if (newscp->priority < scp->priority) {
287
0
                newscp->next = scp;
288
0
                break;
289
0
            }
290
0
            prevNext = &(scp->next);
291
0
        }
292
293
0
        *prevNext = newscp;
294
295
0
        DEBUGMSGTL(("callback", "registered (%d,%d) at %p with priority %d\n",
296
0
                    major, minor, newscp, priority));
297
0
        _callback_unlock(major,minor);
298
0
        return SNMPERR_SUCCESS;
299
0
    }
300
0
}
301
302
/**
303
 * This function calls the callback function for each registered callback of
304
 * type major and minor.
305
 *
306
 * @param major is the SNMP callback major type used
307
 *
308
 * @param minor is the SNMP callback minor type used
309
 *
310
 * @param caller_arg is a void pointer which is sent in as the callback's 
311
 *  serverarg parameter, if needed.
312
 *
313
 * @return Returns SNMPERR_GENERR if major is >= MAX_CALLBACK_IDS or
314
 * minor is >= MAX_CALLBACK_SUBIDS, otherwise SNMPERR_SUCCESS is returned.
315
 *
316
 * @see snmp_register_callback
317
 * @see snmp_unregister_callback
318
 */
319
int
320
snmp_call_callbacks(int major, int minor, void *caller_arg)
321
0
{
322
0
    struct snmp_gen_callback *scp;
323
0
    unsigned int    count = 0;
324
    
325
0
    if (major >= MAX_CALLBACK_IDS || minor >= MAX_CALLBACK_SUBIDS) {
326
0
        return SNMPERR_GENERR;
327
0
    }
328
    
329
0
    if (_callback_need_init)
330
0
        init_callbacks();
331
332
0
#ifdef LOCK_PER_CALLBACK_SUBID
333
0
    _callback_lock(major,minor,"snmp_call_callbacks", 1);
334
#else
335
    /*
336
     * Notes:
337
     * - this gets hit the first time a trap is sent after a new trap
338
     *   destination has been added (session init cb during send trap cb)
339
     */
340
    _callback_lock(major,minor, NULL, 0);
341
#endif
342
343
0
    DEBUGMSGTL(("callback", "START calling callbacks for maj=%d min=%d\n",
344
0
                major, minor));
345
346
    /*
347
     * for each registered callback of type major and minor 
348
     */
349
0
    for (scp = thecallbacks[major][minor]; scp != NULL; scp = scp->next) {
350
351
        /*
352
         * skip unregistered callbacks
353
         */
354
0
        if(NULL == scp->sc_callback)
355
0
            continue;
356
357
0
        DEBUGMSGTL(("callback", "calling a callback for maj=%d min=%d\n",
358
0
                    major, minor));
359
360
        /*
361
         * call them 
362
         */
363
0
        (*(scp->sc_callback)) (major, minor, caller_arg,
364
0
                               scp->sc_client_arg);
365
0
        count++;
366
0
    }
367
368
0
    DEBUGMSGTL(("callback",
369
0
                "END calling callbacks for maj=%d min=%d (%d called)\n",
370
0
                major, minor, count));
371
372
0
    _callback_unlock(major,minor);
373
0
    return SNMPERR_SUCCESS;
374
0
}
375
376
#ifndef NETSNMP_FEATURE_REMOVE_CALLBACK_COUNT
377
int
378
snmp_count_callbacks(int major, int minor)
379
0
{
380
0
    int             count = 0;
381
0
    struct snmp_gen_callback *scp;
382
383
0
    if (major >= MAX_CALLBACK_IDS || minor >= MAX_CALLBACK_SUBIDS) {
384
0
        return SNMPERR_GENERR;
385
0
    }
386
    
387
0
    if (_callback_need_init)
388
0
        init_callbacks();
389
390
0
    for (scp = thecallbacks[major][minor]; scp != NULL; scp = scp->next) {
391
0
        count++;
392
0
    }
393
394
0
    return count;
395
0
}
396
#endif /* NETSNMP_FEATURE_REMOVE_CALLBACK_COUNT */
397
398
int
399
snmp_callback_available(int major, int minor)
400
0
{
401
0
    if (major >= MAX_CALLBACK_IDS || minor >= MAX_CALLBACK_SUBIDS) {
402
0
        return SNMPERR_GENERR;
403
0
    }
404
    
405
0
    if (_callback_need_init)
406
0
        init_callbacks();
407
408
0
    if (thecallbacks[major][minor] != NULL) {
409
0
        return SNMPERR_SUCCESS;
410
0
    }
411
412
0
    return SNMPERR_GENERR;
413
0
}
414
415
/**
416
 * This function unregisters a specified callback function given a major
417
 * and minor type.
418
 *
419
 * Note: no bound checking on major and minor.
420
 *
421
 * @param major is the SNMP callback major type used
422
 *
423
 * @param minor is the SNMP callback minor type used
424
 *
425
 * @param target is the callback function that will be unregistered.
426
 *
427
 * @param arg is a void pointer used for comparison against the registered 
428
 *  callback's sc_client_arg variable.
429
 *
430
 * @param matchargs is an integer used to bypass the comparison of arg and the
431
 *  callback's sc_client_arg variable only when matchargs is set to 0.
432
 *
433
 *
434
 * @return
435
 *        Returns the number of callbacks that were unregistered.
436
 *
437
 * @see snmp_register_callback
438
 * @see snmp_call_callbacks
439
 */
440
441
int
442
snmp_unregister_callback(int major, int minor, SNMPCallback * target,
443
                         void *arg, int matchargs)
444
0
{
445
0
    struct snmp_gen_callback *scp;
446
0
    struct snmp_gen_callback **prevNext;
447
0
    int             count = 0;
448
449
0
    if (major >= MAX_CALLBACK_IDS || minor >= MAX_CALLBACK_SUBIDS)
450
0
        return SNMPERR_GENERR;
451
452
0
    scp = thecallbacks[major][minor];
453
0
    prevNext = &(thecallbacks[major][minor]);
454
455
0
    if (_callback_need_init)
456
0
        init_callbacks();
457
458
0
#ifdef LOCK_PER_CALLBACK_SUBID
459
0
    _callback_lock(major,minor,"snmp_unregister_callback", 1);
460
#else
461
    /*
462
     * Notes;
463
     * - this gets hit at shutdown, during cleanup. No easy fix.
464
     */
465
    _callback_lock(major,minor,"snmp_unregister_callback", 0);
466
#endif
467
468
0
    while (scp != NULL) {
469
0
        if ((scp->sc_callback == target) &&
470
0
            (!matchargs || (scp->sc_client_arg == arg))) {
471
0
            DEBUGMSGTL(("callback", "unregistering (%d,%d) at %p\n", major,
472
0
                        minor, scp));
473
0
            if(1 == CALLBACK_LOCK_COUNT(major,minor)) {
474
0
                *prevNext = scp->next;
475
0
                SNMP_FREE(scp);
476
0
                scp = *prevNext;
477
0
            }
478
0
            else {
479
0
                scp->sc_callback = NULL;
480
                /** set cleanup flag? */
481
0
            }
482
0
            count++;
483
0
        } else {
484
0
            prevNext = &(scp->next);
485
0
            scp = scp->next;
486
0
        }
487
0
    }
488
489
0
    _callback_unlock(major,minor);
490
0
    return count;
491
0
}
492
493
/**
494
 * find and clear client args that match ptr
495
 *
496
 * @param ptr  pointer to search for
497
 * @param i    callback id to start at
498
 * @param j    callback subid to start at
499
 */
500
int
501
netsnmp_callback_clear_client_arg(void *ptr, int i, int j)
502
0
{
503
0
    struct snmp_gen_callback *scp = NULL;
504
0
    int rc = 0;
505
506
    /*
507
     * don't init i and j before loop, since the caller specified
508
     * the starting point explicitly. But *after* the i loop has
509
     * finished executing once, init j to 0 for the next pass
510
     * through the subids.
511
     */
512
0
    for (; i < MAX_CALLBACK_IDS; i++,j=0) {
513
0
        for (; j < MAX_CALLBACK_SUBIDS; j++) {
514
0
            scp = thecallbacks[i][j]; 
515
0
            while (scp != NULL) {
516
0
                if ((NULL != scp->sc_callback) &&
517
0
                    (scp->sc_client_arg != NULL) &&
518
0
                    (scp->sc_client_arg == ptr)) {
519
0
                    DEBUGMSGTL(("9:callback", "  clearing %p at [%d,%d]\n", ptr, i, j));
520
0
                    scp->sc_client_arg = NULL;
521
0
                    ++rc;
522
0
                }
523
0
                scp = scp->next;
524
0
            }
525
0
        }
526
0
    }
527
528
0
    if (0 != rc) {
529
0
        DEBUGMSGTL(("callback", "removed %d client args\n", rc));
530
0
    }
531
532
0
    return rc;
533
0
}
534
535
void
536
clear_callback(void)
537
0
{
538
0
    unsigned int i = 0, j = 0;
539
0
    struct snmp_gen_callback *scp = NULL;
540
541
0
    if (_callback_need_init)
542
0
        init_callbacks();
543
544
0
    DEBUGMSGTL(("callback", "clear callback\n"));
545
0
    for (i = 0; i < MAX_CALLBACK_IDS; i++) {
546
0
        for (j = 0; j < MAX_CALLBACK_SUBIDS; j++) {
547
0
            _callback_lock(i,j, "clear_callback", 1);
548
0
            scp = thecallbacks[i][j];
549
0
            while (scp != NULL) {
550
0
                thecallbacks[i][j] = scp->next;
551
                /*
552
                 * if there is a client arg, check for duplicates
553
                 * and then free it.
554
                 */
555
0
                if ((NULL != scp->sc_callback) &&
556
0
                    (scp->sc_client_arg != NULL)) {
557
0
                    void *tmp_arg;
558
                    /*
559
                     * save the client arg, then set it to null so that it
560
                     * won't look like a duplicate, then check for duplicates
561
                     * starting at the current i,j (earlier dups should have
562
                     * already been found) and free the pointer.
563
                     */
564
0
                    tmp_arg = scp->sc_client_arg;
565
0
                    scp->sc_client_arg = NULL;
566
0
                    DEBUGMSGTL(("9:callback", "  freeing %p at [%d,%d]\n", tmp_arg, i, j));
567
0
                    (void)netsnmp_callback_clear_client_arg(tmp_arg, i, j);
568
0
                    free(tmp_arg);
569
0
                }
570
0
                SNMP_FREE(scp);
571
0
                scp = thecallbacks[i][j];
572
0
            }
573
0
            _callback_unlock(i,j);
574
0
        }
575
0
    }
576
0
}
577
578
#ifndef NETSNMP_FEATURE_REMOVE_CALLBACK_LIST
579
struct snmp_gen_callback *
580
snmp_callback_list(int major, int minor)
581
0
{
582
0
    if (_callback_need_init)
583
0
        init_callbacks();
584
585
0
    return (thecallbacks[major][minor]);
586
0
}
587
#endif /* NETSNMP_FEATURE_REMOVE_CALLBACK_LIST */
588
/**  @} */