Coverage Report

Created: 2025-10-10 07:09

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
#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
    struct snmp_gen_callback *newscp = NULL, *scp = NULL;
264
265
0
    if (major >= MAX_CALLBACK_IDS || minor >= MAX_CALLBACK_SUBIDS) {
266
0
        return SNMPERR_GENERR;
267
0
    }
268
269
0
    if (_callback_need_init)
270
0
        init_callbacks();
271
272
0
    _callback_lock(major,minor, "netsnmp_register_callback", 1);
273
    
274
0
    if ((newscp = SNMP_MALLOC_STRUCT(snmp_gen_callback)) == NULL) {
275
0
        _callback_unlock(major,minor);
276
0
        return SNMPERR_GENERR;
277
0
    } else {
278
0
        struct snmp_gen_callback **prevNext = &(thecallbacks[major][minor]);
279
280
0
        newscp->priority = priority;
281
0
        newscp->sc_client_arg = arg;
282
0
        newscp->sc_callback = new_callback;
283
0
        newscp->next = NULL;
284
285
0
        for (scp = thecallbacks[major][minor]; scp != NULL;
286
0
             scp = scp->next) {
287
0
            if (newscp->priority < scp->priority) {
288
0
                newscp->next = scp;
289
0
                break;
290
0
            }
291
0
            prevNext = &(scp->next);
292
0
        }
293
294
0
        *prevNext = newscp;
295
296
0
        DEBUGMSGTL(("callback", "registered (%d,%d) at %p with priority %d\n",
297
0
                    major, minor, newscp, priority));
298
0
        _callback_unlock(major,minor);
299
0
        return SNMPERR_SUCCESS;
300
0
    }
301
0
}
302
303
/**
304
 * This function calls the callback function for each registered callback of
305
 * type major and minor.
306
 *
307
 * @param major is the SNMP callback major type used
308
 *
309
 * @param minor is the SNMP callback minor type used
310
 *
311
 * @param caller_arg is a void pointer which is sent in as the callback's 
312
 *  serverarg parameter, if needed.
313
 *
314
 * @return Returns SNMPERR_GENERR if major is >= MAX_CALLBACK_IDS or
315
 * minor is >= MAX_CALLBACK_SUBIDS, otherwise SNMPERR_SUCCESS is returned.
316
 *
317
 * @see snmp_register_callback
318
 * @see snmp_unregister_callback
319
 */
320
int
321
snmp_call_callbacks(int major, int minor, void *caller_arg)
322
0
{
323
0
    struct snmp_gen_callback *scp;
324
0
    unsigned int    count = 0;
325
    
326
0
    if (major >= MAX_CALLBACK_IDS || minor >= MAX_CALLBACK_SUBIDS) {
327
0
        return SNMPERR_GENERR;
328
0
    }
329
    
330
0
    if (_callback_need_init)
331
0
        init_callbacks();
332
333
0
#ifdef LOCK_PER_CALLBACK_SUBID
334
0
    _callback_lock(major,minor,"snmp_call_callbacks", 1);
335
#else
336
    /*
337
     * Notes:
338
     * - this gets hit the first time a trap is sent after a new trap
339
     *   destination has been added (session init cb during send trap cb)
340
     */
341
    _callback_lock(major,minor, NULL, 0);
342
#endif
343
344
0
    DEBUGMSGTL(("callback", "START calling callbacks for maj=%d min=%d\n",
345
0
                major, minor));
346
347
    /*
348
     * for each registered callback of type major and minor 
349
     */
350
0
    for (scp = thecallbacks[major][minor]; scp != NULL; scp = scp->next) {
351
352
        /*
353
         * skip unregistered callbacks
354
         */
355
0
        if(NULL == scp->sc_callback)
356
0
            continue;
357
358
0
        DEBUGMSGTL(("callback", "calling a callback for maj=%d min=%d\n",
359
0
                    major, minor));
360
361
        /*
362
         * call them 
363
         */
364
0
        (*(scp->sc_callback)) (major, minor, caller_arg,
365
0
                               scp->sc_client_arg);
366
0
        count++;
367
0
    }
368
369
0
    DEBUGMSGTL(("callback",
370
0
                "END calling callbacks for maj=%d min=%d (%d called)\n",
371
0
                major, minor, count));
372
373
0
    _callback_unlock(major,minor);
374
0
    return SNMPERR_SUCCESS;
375
0
}
376
377
#ifndef NETSNMP_FEATURE_REMOVE_CALLBACK_COUNT
378
int
379
snmp_count_callbacks(int major, int minor)
380
0
{
381
0
    int             count = 0;
382
0
    struct snmp_gen_callback *scp;
383
384
0
    if (major >= MAX_CALLBACK_IDS || minor >= MAX_CALLBACK_SUBIDS) {
385
0
        return SNMPERR_GENERR;
386
0
    }
387
    
388
0
    if (_callback_need_init)
389
0
        init_callbacks();
390
391
0
    for (scp = thecallbacks[major][minor]; scp != NULL; scp = scp->next) {
392
0
        count++;
393
0
    }
394
395
0
    return count;
396
0
}
397
#endif /* NETSNMP_FEATURE_REMOVE_CALLBACK_COUNT */
398
399
int
400
snmp_callback_available(int major, int minor)
401
0
{
402
0
    if (major >= MAX_CALLBACK_IDS || minor >= MAX_CALLBACK_SUBIDS) {
403
0
        return SNMPERR_GENERR;
404
0
    }
405
    
406
0
    if (_callback_need_init)
407
0
        init_callbacks();
408
409
0
    if (thecallbacks[major][minor] != NULL) {
410
0
        return SNMPERR_SUCCESS;
411
0
    }
412
413
0
    return SNMPERR_GENERR;
414
0
}
415
416
/**
417
 * This function unregisters a specified callback function given a major
418
 * and minor type.
419
 *
420
 * Note: no bound checking on major and minor.
421
 *
422
 * @param major is the SNMP callback major type used
423
 *
424
 * @param minor is the SNMP callback minor type used
425
 *
426
 * @param target is the callback function that will be unregistered.
427
 *
428
 * @param arg is a void pointer used for comparison against the registered 
429
 *  callback's sc_client_arg variable.
430
 *
431
 * @param matchargs is an integer used to bypass the comparison of arg and the
432
 *  callback's sc_client_arg variable only when matchargs is set to 0.
433
 *
434
 *
435
 * @return
436
 *        Returns the number of callbacks that were unregistered.
437
 *
438
 * @see snmp_register_callback
439
 * @see snmp_call_callbacks
440
 */
441
442
int
443
snmp_unregister_callback(int major, int minor, SNMPCallback * target,
444
                         void *arg, int matchargs)
445
0
{
446
0
    struct snmp_gen_callback *scp;
447
0
    struct snmp_gen_callback **prevNext;
448
0
    int             count = 0;
449
450
0
    if (major >= MAX_CALLBACK_IDS || minor >= MAX_CALLBACK_SUBIDS)
451
0
        return SNMPERR_GENERR;
452
453
0
    scp = thecallbacks[major][minor];
454
0
    prevNext = &(thecallbacks[major][minor]);
455
456
0
    if (_callback_need_init)
457
0
        init_callbacks();
458
459
0
#ifdef LOCK_PER_CALLBACK_SUBID
460
0
    _callback_lock(major,minor,"snmp_unregister_callback", 1);
461
#else
462
    /*
463
     * Notes;
464
     * - this gets hit at shutdown, during cleanup. No easy fix.
465
     */
466
    _callback_lock(major,minor,"snmp_unregister_callback", 0);
467
#endif
468
469
0
    while (scp != NULL) {
470
0
        if ((scp->sc_callback == target) &&
471
0
            (!matchargs || (scp->sc_client_arg == arg))) {
472
0
            DEBUGMSGTL(("callback", "unregistering (%d,%d) at %p\n", major,
473
0
                        minor, scp));
474
0
            if(1 == CALLBACK_LOCK_COUNT(major,minor)) {
475
0
                *prevNext = scp->next;
476
0
                SNMP_FREE(scp);
477
0
                scp = *prevNext;
478
0
            }
479
0
            else {
480
0
                scp->sc_callback = NULL;
481
                /** set cleanup flag? */
482
0
            }
483
0
            count++;
484
0
        } else {
485
0
            prevNext = &(scp->next);
486
0
            scp = scp->next;
487
0
        }
488
0
    }
489
490
0
    _callback_unlock(major,minor);
491
0
    return count;
492
0
}
493
494
/**
495
 * find and clear client args that match ptr
496
 *
497
 * @param ptr  pointer to search for
498
 * @param i    callback id to start at
499
 * @param j    callback subid to start at
500
 */
501
int
502
netsnmp_callback_clear_client_arg(void *ptr, int i, int j)
503
0
{
504
0
    struct snmp_gen_callback *scp = NULL;
505
0
    int rc = 0;
506
507
    /*
508
     * don't init i and j before loop, since the caller specified
509
     * the starting point explicitly. But *after* the i loop has
510
     * finished executing once, init j to 0 for the next pass
511
     * through the subids.
512
     */
513
0
    for (; i < MAX_CALLBACK_IDS; i++,j=0) {
514
0
        for (; j < MAX_CALLBACK_SUBIDS; j++) {
515
0
            scp = thecallbacks[i][j]; 
516
0
            while (scp != NULL) {
517
0
                if ((NULL != scp->sc_callback) &&
518
0
                    (scp->sc_client_arg != NULL) &&
519
0
                    (scp->sc_client_arg == ptr)) {
520
0
                    DEBUGMSGTL(("9:callback", "  clearing %p at [%d,%d]\n", ptr, i, j));
521
0
                    scp->sc_client_arg = NULL;
522
0
                    ++rc;
523
0
                }
524
0
                scp = scp->next;
525
0
            }
526
0
        }
527
0
    }
528
529
0
    if (0 != rc) {
530
0
        DEBUGMSGTL(("callback", "removed %d client args\n", rc));
531
0
    }
532
533
0
    return rc;
534
0
}
535
536
void
537
clear_callback(void)
538
0
{
539
0
    unsigned int i = 0, j = 0;
540
0
    struct snmp_gen_callback *scp = NULL;
541
542
0
    if (_callback_need_init)
543
0
        init_callbacks();
544
545
0
    DEBUGMSGTL(("callback", "clear callback\n"));
546
0
    for (i = 0; i < MAX_CALLBACK_IDS; i++) {
547
0
        for (j = 0; j < MAX_CALLBACK_SUBIDS; j++) {
548
0
            _callback_lock(i,j, "clear_callback", 1);
549
0
            scp = thecallbacks[i][j];
550
0
            while (scp != NULL) {
551
0
                thecallbacks[i][j] = scp->next;
552
                /*
553
                 * if there is a client arg, check for duplicates
554
                 * and then free it.
555
                 */
556
0
                if ((NULL != scp->sc_callback) &&
557
0
                    (scp->sc_client_arg != NULL)) {
558
0
                    void *tmp_arg;
559
                    /*
560
                     * save the client arg, then set it to null so that it
561
                     * won't look like a duplicate, then check for duplicates
562
                     * starting at the current i,j (earlier dups should have
563
                     * already been found) and free the pointer.
564
                     */
565
0
                    tmp_arg = scp->sc_client_arg;
566
0
                    scp->sc_client_arg = NULL;
567
0
                    DEBUGMSGTL(("9:callback", "  freeing %p at [%d,%d]\n", tmp_arg, i, j));
568
0
                    (void)netsnmp_callback_clear_client_arg(tmp_arg, i, j);
569
0
                    free(tmp_arg);
570
0
                }
571
0
                SNMP_FREE(scp);
572
0
                scp = thecallbacks[i][j];
573
0
            }
574
0
            _callback_unlock(i,j);
575
0
        }
576
0
    }
577
0
}
578
579
#ifndef NETSNMP_FEATURE_REMOVE_CALLBACK_LIST
580
struct snmp_gen_callback *
581
snmp_callback_list(int major, int minor)
582
0
{
583
0
    if (_callback_need_init)
584
0
        init_callbacks();
585
586
0
    return (thecallbacks[major][minor]);
587
0
}
588
#endif /* NETSNMP_FEATURE_REMOVE_CALLBACK_LIST */
589
/**  @} */