Skip to content

Commit 479c345

Browse files
committed
Add snmp_init_mib function to allow MIB tree to be reset using environment variables (#21452)
1 parent a151551 commit 479c345

3 files changed

Lines changed: 47 additions & 2 deletions

File tree

ext/snmp/snmp.c

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ typedef struct snmp_session php_snmp_session;
8989
} \
9090
}
9191

92+
static int mib_needs_reset;
93+
9294
ZEND_DECLARE_MODULE_GLOBALS(snmp)
9395
static PHP_GINIT_FUNCTION(snmp);
9496

@@ -1629,6 +1631,7 @@ PHP_FUNCTION(snmp_read_mib)
16291631
RETURN_THROWS();
16301632
}
16311633

1634+
mib_needs_reset = 1;
16321635
if (!read_mib(filename)) {
16331636
char *error = strerror(errno);
16341637
php_error_docref(NULL, E_WARNING, "Error while reading MIB file '%s': %s", filename, error);
@@ -1638,6 +1641,27 @@ PHP_FUNCTION(snmp_read_mib)
16381641
}
16391642
/* }}} */
16401643

1644+
/* {{{ Resets the MIB tree and set the mib directories to the provided mibdirs. */
1645+
PHP_FUNCTION(snmp_init_mib)
1646+
{
1647+
zend_string *mibdirs = NULL;
1648+
1649+
ZEND_PARSE_PARAMETERS_START(0, 1)
1650+
Z_PARAM_OPTIONAL
1651+
Z_PARAM_STR_OR_NULL(mibdirs)
1652+
ZEND_PARSE_PARAMETERS_END();
1653+
1654+
// If the mibdirs has been changed, we need to reset the MIB tree at the end of the request
1655+
if (mibdirs != NULL) {
1656+
mib_needs_reset = 1;
1657+
}
1658+
1659+
shutdown_mib();
1660+
netsnmp_ds_set_string(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_MIBDIRS, ZSTR_VAL(mibdirs));
1661+
init_mib();
1662+
}
1663+
/* }}} */
1664+
16411665
/* {{{ Creates a new SNMP session to specified host. */
16421666
PHP_METHOD(SNMP, __construct)
16431667
{
@@ -2172,6 +2196,19 @@ PHP_MSHUTDOWN_FUNCTION(snmp)
21722196
}
21732197
/* }}} */
21742198

2199+
/* {{{ PHP_RSHUTDOWN_FUNCTION */
2200+
static PHP_RSHUTDOWN_FUNCTION(snmp)
2201+
{
2202+
if (mib_needs_reset) {
2203+
shutdown_mib();
2204+
netsnmp_ds_set_string(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_MIBDIRS, NULL);
2205+
init_mib();
2206+
}
2207+
2208+
return SUCCESS;
2209+
}
2210+
/* }}} */
2211+
21752212
/* {{{ PHP_MINFO_FUNCTION */
21762213
PHP_MINFO_FUNCTION(snmp)
21772214
{
@@ -2199,7 +2236,7 @@ zend_module_entry snmp_module_entry = {
21992236
PHP_MINIT(snmp),
22002237
PHP_MSHUTDOWN(snmp),
22012238
NULL,
2202-
NULL,
2239+
PHP_RSHUTDOWN(snmp),
22032240
PHP_MINFO(snmp),
22042241
PHP_SNMP_VERSION,
22052242
PHP_MODULE_GLOBALS(snmp),

ext/snmp/snmp.stub.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ function snmp_get_valueretrieval(): int {}
181181

182182
function snmp_read_mib(string $filename): bool {}
183183

184+
function snmp_init_mib(?string $mibdirs): void {}
185+
184186
class SNMP
185187
{
186188
/** @cvalue SNMP_VERSION_1 */

ext/snmp/snmp_arginfo.h

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)