Coverage Report

Created: 2026-07-16 06:27

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/net-snmp/snmplib/callback.c
Line
Count
Source
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
    if (CALLBACK_LOCK_COUNT(major,minor) == 0) {
168
0
        struct snmp_gen_callback *scp, **prevNext;
169
0
        scp = thecallbacks[major][minor];
170
0
        prevNext = &(thecallbacks[major][minor]);
171
0
        while (scp != NULL) {
172
0
            if (scp->sc_callback == NULL) {
173
0
                *prevNext = scp->next;
174
0
                SNMP_FREE(scp);
175
0
                scp = *prevNext;
176
0
            } else {
177
0
                prevNext = &(scp->next);
178
0
                scp = scp->next;
179
0
            }
180
0
        }
181
0
    }
182
183
0
#ifdef CALLBACK_NAME_LOGGING
184
0
    DEBUGMSGTL(("9:callback:lock", "unlocked (%s,%s)\n",
185
0
                types[major], (SNMP_CALLBACK_LIBRARY == major) ?
186
0
                SNMP_STRORNULL(lib[minor]) : "null"));
187
0
#endif
188
0
}
189
190
191
/*
192
 * the chicken. or the egg.  You pick. 
193
 */
194
void
195
init_callbacks(void)
196
0
{
197
    /*
198
     * (poses a problem if you put init_callbacks() inside of
199
     * init_snmp() and then want the app to register a callback before
200
     * init_snmp() is called in the first place.  -- Wes 
201
     */
202
0
    if (0 == _callback_need_init)
203
0
        return;
204
    
205
0
    _callback_need_init = 0;
206
    
207
0
    memset(thecallbacks, 0, sizeof(thecallbacks)); 
208
0
#ifdef LOCK_PER_CALLBACK_SUBID
209
0
    memset(_locks, 0, sizeof(_locks));
210
#else
211
    _lock = 0;
212
#endif
213
    
214
0
    DEBUGMSGTL(("callback", "initialized\n"));
215
0
}
216
217
/**
218
 * This function registers a generic callback function.  The major and
219
 * minor values are used to set the new_callback function into a global 
220
 * static multi-dimensional array of type struct snmp_gen_callback.  
221
 * The function makes sure to append this callback function at the end
222
 * of the link list, snmp_gen_callback->next.
223
 *
224
 * @param major is the SNMP callback major type used
225
 *    - SNMP_CALLBACK_LIBRARY
226
 *              - SNMP_CALLBACK_APPLICATION
227
 *
228
 * @param minor is the SNMP callback minor type used
229
 *    - SNMP_CALLBACK_POST_READ_CONFIG
230
 *    - SNMP_CALLBACK_STORE_DATA          
231
 *    - SNMP_CALLBACK_SHUTDOWN            
232
 *    - SNMP_CALLBACK_POST_PREMIB_READ_CONFIG 
233
 *    - SNMP_CALLBACK_LOGGING     
234
 *    - SNMP_CALLBACK_SESSION_INIT         
235
 *
236
 * @param new_callback is the callback function that is registered.
237
 *
238
 * @param arg when not NULL is a void pointer used whenever new_callback 
239
 *  function is exercised. Ownership is transferred to the twodimensional
240
 *      thecallbacks[][] array. The function clear_callback() will deallocate
241
 *      the memory pointed at by calling free().
242
 *
243
 * @return 
244
 *  Returns SNMPERR_GENERR if major is >= MAX_CALLBACK_IDS or minor is >=
245
 *  MAX_CALLBACK_SUBIDS or a snmp_gen_callback pointer could not be 
246
 *  allocated, otherwise SNMPERR_SUCCESS is returned.
247
 *  - \#define MAX_CALLBACK_IDS    2
248
 *  - \#define MAX_CALLBACK_SUBIDS 16
249
 *
250
 * @see snmp_call_callbacks
251
 * @see snmp_unregister_callback
252
 */
253
int
254
snmp_register_callback(int major, int minor, SNMPCallback * new_callback,
255
                       void *arg)
256
0
{
257
0
    return netsnmp_register_callback( major, minor, new_callback, arg,
258
0
                                      NETSNMP_CALLBACK_DEFAULT_PRIORITY);
259
0
}
260
261
/**
262
 * Register a callback function.
263
 *
264
 * @param major        Major callback event type.
265
 * @param minor        Minor callback event type.
266
 * @param new_callback Callback function being registered.
267
 * @param arg          Argument that will be passed to the callback function.
268
 * @param priority     Handler invocation priority. When multiple handlers have
269
 *   been registered for the same (major, minor) callback event type, handlers
270
 *   with the numerically lowest priority will be invoked first. Handlers with
271
 *   identical priority are invoked in the order they have been registered.
272
 *
273
 * @see snmp_register_callback
274
 */
275
int
276
netsnmp_register_callback(int major, int minor, SNMPCallback * new_callback,
277
                          void *arg, int priority)
278
0
{
279
0
    struct snmp_gen_callback *newscp = NULL, *scp = NULL;
280
281
0
    if (major >= MAX_CALLBACK_IDS || minor >= MAX_CALLBACK_SUBIDS) {
282
0
        return SNMPERR_GENERR;
283
0
    }
284
285
0
    if (_callback_need_init)
286
0
        init_callbacks();
287
288
0
    _callback_lock(major,minor, "netsnmp_register_callback", 1);
289
    
290
0
    if ((newscp = SNMP_MALLOC_STRUCT(snmp_gen_callback)) == NULL) {
291
0
        _callback_unlock(major,minor);
292
0
        return SNMPERR_GENERR;
293
0
    } else {
294
0
        struct snmp_gen_callback **prevNext = &(thecallbacks[major][minor]);
295
296
0
        newscp->priority = priority;
297
0
        newscp->sc_client_arg = arg;
298
0
        newscp->sc_callback = new_callback;
299
0
        newscp->next = NULL;
300
301
0
        for (scp = thecallbacks[major][minor]; scp != NULL;
302
0
             scp = scp->next) {
303
0
            if (newscp->priority < scp->priority) {
304
0
                newscp->next = scp;
305
0
                break;
306
0
            }
307
0
            prevNext = &(scp->next);
308
0
        }
309
310
0
        *prevNext = newscp;
311
312
0
        DEBUGMSGTL(("callback", "registered (%d,%d) at %p with priority %d\n",
313
0
                    major, minor, newscp, priority));
314
0
        _callback_unlock(major,minor);
315
0
        return SNMPERR_SUCCESS;
316
0
    }
317
0
}
318
319
/**
320
 * This function calls the callback function for each registered callback of
321
 * type major and minor.
322
 *
323
 * @param major is the SNMP callback major type used
324
 *
325
 * @param minor is the SNMP callback minor type used
326
 *
327
 * @param caller_arg is a void pointer which is sent in as the callback's 
328
 *  serverarg parameter, if needed.
329
 *
330
 * @return Returns SNMPERR_GENERR if major is >= MAX_CALLBACK_IDS or
331
 * minor is >= MAX_CALLBACK_SUBIDS, otherwise SNMPERR_SUCCESS is returned.
332
 *
333
 * @see snmp_register_callback
334
 * @see snmp_unregister_callback
335
 */
336
int
337
snmp_call_callbacks(int major, int minor, void *caller_arg)
338
0
{
339
0
    struct snmp_gen_callback *scp;
340
0
    unsigned int    count = 0;
341
    
342
0
    if (major >= MAX_CALLBACK_IDS || minor >= MAX_CALLBACK_SUBIDS) {
343
0
        return SNMPERR_GENERR;
344
0
    }
345
    
346
0
    if (_callback_need_init)
347
0
        init_callbacks();
348
349
0
#ifdef LOCK_PER_CALLBACK_SUBID
350
0
    _callback_lock(major,minor,"snmp_call_callbacks", 1);
351
#else
352
    /*
353
     * Notes:
354
     * - this gets hit the first time a trap is sent after a new trap
355
     *   destination has been added (session init cb during send trap cb)
356
     */
357
    _callback_lock(major,minor, NULL, 0);
358
#endif
359
360
0
    DEBUGMSGTL(("callback", "START calling callbacks for maj=%d min=%d\n",
361
0
                major, minor));
362
363
    /*
364
     * for each registered callback of type major and minor 
365
     */
366
0
    for (scp = thecallbacks[major][minor]; scp != NULL; scp = scp->next) {
367
368
        /*
369
         * skip unregistered callbacks
370
         */
371
0
        if(NULL == scp->sc_callback)
372
0
            continue;
373
374
0
        DEBUGMSGTL(("callback", "calling a callback for maj=%d min=%d\n",
375
0
                    major, minor));
376
377
        /*
378
         * call them 
379
         */
380
0
        (*(scp->sc_callback)) (major, minor, caller_arg,
381
0
                               scp->sc_client_arg);
382
0
        count++;
383
0
    }
384
385
0
    DEBUGMSGTL(("callback",
386
0
                "END calling callbacks for maj=%d min=%d (%d called)\n",
387
0
                major, minor, count));
388
389
0
    _callback_unlock(major,minor);
390
0
    return SNMPERR_SUCCESS;
391
0
}
392
393
#ifndef NETSNMP_FEATURE_REMOVE_CALLBACK_COUNT
394
int
395
snmp_count_callbacks(int major, int minor)
396
0
{
397
0
    int             count = 0;
398
0
    struct snmp_gen_callback *scp;
399
400
0
    if (major >= MAX_CALLBACK_IDS || minor >= MAX_CALLBACK_SUBIDS) {
401
0
        return SNMPERR_GENERR;
402
0
    }
403
    
404
0
    if (_callback_need_init)
405
0
        init_callbacks();
406
407
0
    for (scp = thecallbacks[major][minor]; scp != NULL; scp = scp->next) {
408
0
        count++;
409
0
    }
410
411
0
    return count;
412
0
}
413
#endif /* NETSNMP_FEATURE_REMOVE_CALLBACK_COUNT */
414
415
int
416
snmp_callback_available(int major, int minor)
417
0
{
418
0
    if (major >= MAX_CALLBACK_IDS || minor >= MAX_CALLBACK_SUBIDS) {
419
0
        return SNMPERR_GENERR;
420
0
    }
421
    
422
0
    if (_callback_need_init)
423
0
        init_callbacks();
424
425
0
    if (thecallbacks[major][minor] != NULL) {
426
0
        return SNMPERR_SUCCESS;
427
0
    }
428
429
0
    return SNMPERR_GENERR;
430
0
}
431
432
/**
433
 * This function unregisters a specified callback function given a major
434
 * and minor type.
435
 *
436
 * Note: no bound checking on major and minor.
437
 *
438
 * @param major is the SNMP callback major type used
439
 *
440
 * @param minor is the SNMP callback minor type used
441
 *
442
 * @param target is the callback function that will be unregistered.
443
 *
444
 * @param arg is a void pointer used for comparison against the registered 
445
 *  callback's sc_client_arg variable.
446
 *
447
 * @param matchargs is an integer used to bypass the comparison of arg and the
448
 *  callback's sc_client_arg variable only when matchargs is set to 0.
449
 *
450
 *
451
 * @return
452
 *        Returns the number of callbacks that were unregistered.
453
 *
454
 * @see snmp_register_callback
455
 * @see snmp_call_callbacks
456
 */
457
458
int
459
snmp_unregister_callback(int major, int minor, SNMPCallback * target,
460
                         void *arg, int matchargs)
461
0
{
462
0
    struct snmp_gen_callback *scp;
463
0
    struct snmp_gen_callback **prevNext;
464
0
    int             count = 0;
465
0
    int             lock_failed;
466
467
0
    if (major >= MAX_CALLBACK_IDS || minor >= MAX_CALLBACK_SUBIDS)
468
0
        return SNMPERR_GENERR;
469
470
0
    scp = thecallbacks[major][minor];
471
0
    prevNext = &(thecallbacks[major][minor]);
472
473
0
    if (_callback_need_init)
474
0
        init_callbacks();
475
0
#ifdef LOCK_PER_CALLBACK_SUBID
476
0
    lock_failed = _callback_lock(major,minor,"snmp_unregister_callback", 1);
477
#else
478
    /*
479
     * Notes;
480
     * - this gets hit at shutdown, during cleanup. No easy fix.
481
     */
482
    lock_failed = _callback_lock(major,minor,"snmp_unregister_callback", 0);
483
#endif
484
485
0
    while (scp != NULL) {
486
0
        if ((scp->sc_callback == target) &&
487
0
            (!matchargs || (scp->sc_client_arg == arg))) {
488
0
            DEBUGMSGTL(("callback", "unregistering (%d,%d) at %p\n", major,
489
0
                        minor, scp));
490
0
            if(!lock_failed && 1 == CALLBACK_LOCK_COUNT(major,minor)) {
491
0
                *prevNext = scp->next;
492
0
                SNMP_FREE(scp);
493
0
                scp = *prevNext;
494
0
            }
495
0
            else {
496
0
                scp->sc_callback = NULL;
497
                /** set cleanup flag? */
498
0
            }
499
0
            count++;
500
0
        } else {
501
0
            prevNext = &(scp->next);
502
0
            scp = scp->next;
503
0
        }
504
0
    }
505
506
0
    if (!lock_failed)
507
0
        _callback_unlock(major,minor);
508
0
    return count;
509
0
}
510
511
/**
512
 * find and clear client args that match ptr
513
 *
514
 * @param ptr  pointer to search for
515
 * @param i    callback id to start at
516
 * @param j    callback subid to start at
517
 */
518
int
519
netsnmp_callback_clear_client_arg(void *ptr, int i, int j)
520
0
{
521
0
    struct snmp_gen_callback *scp = NULL;
522
0
    int rc = 0;
523
524
    /*
525
     * don't init i and j before loop, since the caller specified
526
     * the starting point explicitly. But *after* the i loop has
527
     * finished executing once, init j to 0 for the next pass
528
     * through the subids.
529
     */
530
0
    for (; i < MAX_CALLBACK_IDS; i++,j=0) {
531
0
        for (; j < MAX_CALLBACK_SUBIDS; j++) {
532
0
            scp = thecallbacks[i][j]; 
533
0
            while (scp != NULL) {
534
0
                if ((NULL != scp->sc_callback) &&
535
0
                    (scp->sc_client_arg != NULL) &&
536
0
                    (scp->sc_client_arg == ptr)) {
537
0
                    DEBUGMSGTL(("9:callback", "  clearing %p at [%d,%d]\n", ptr, i, j));
538
0
                    scp->sc_client_arg = NULL;
539
0
                    ++rc;
540
0
                }
541
0
                scp = scp->next;
542
0
            }
543
0
        }
544
0
    }
545
546
0
    if (0 != rc) {
547
0
        DEBUGMSGTL(("callback", "removed %d client args\n", rc));
548
0
    }
549
550
0
    return rc;
551
0
}
552
553
void
554
clear_callback(void)
555
0
{
556
0
    unsigned int i = 0, j = 0;
557
0
    struct snmp_gen_callback *scp = NULL;
558
559
0
    if (_callback_need_init)
560
0
        init_callbacks();
561
562
0
    DEBUGMSGTL(("callback", "clear callback\n"));
563
0
    for (i = 0; i < MAX_CALLBACK_IDS; i++) {
564
0
        for (j = 0; j < MAX_CALLBACK_SUBIDS; j++) {
565
0
            _callback_lock(i,j, "clear_callback", 1);
566
0
            scp = thecallbacks[i][j];
567
0
            while (scp != NULL) {
568
0
                thecallbacks[i][j] = scp->next;
569
                /*
570
                 * if there is a client arg, check for duplicates
571
                 * and then free it.
572
                 */
573
0
                if ((NULL != scp->sc_callback) &&
574
0
                    (scp->sc_client_arg != NULL)) {
575
0
                    void *tmp_arg;
576
                    /*
577
                     * save the client arg, then set it to null so that it
578
                     * won't look like a duplicate, then check for duplicates
579
                     * starting at the current i,j (earlier dups should have
580
                     * already been found) and free the pointer.
581
                     */
582
0
                    tmp_arg = scp->sc_client_arg;
583
0
                    scp->sc_client_arg = NULL;
584
0
                    DEBUGMSGTL(("9:callback", "  freeing %p at [%d,%d]\n", tmp_arg, i, j));
585
0
                    (void)netsnmp_callback_clear_client_arg(tmp_arg, i, j);
586
0
                    free(tmp_arg);
587
0
                }
588
0
                SNMP_FREE(scp);
589
0
                scp = thecallbacks[i][j];
590
0
            }
591
0
            _callback_unlock(i,j);
592
0
        }
593
0
    }
594
0
}
595
596
#ifndef NETSNMP_FEATURE_REMOVE_CALLBACK_LIST
597
struct snmp_gen_callback *
598
snmp_callback_list(int major, int minor)
599
0
{
600
0
    if (_callback_need_init)
601
0
        init_callbacks();
602
603
0
    return (thecallbacks[major][minor]);
604
0
}
605
#endif /* NETSNMP_FEATURE_REMOVE_CALLBACK_LIST */
606
/**  @} */