diff --git a/ext/snmp/snmp.c b/ext/snmp/snmp.c index d116339dd1f40..c52e7edec07c6 100644 --- a/ext/snmp/snmp.c +++ b/ext/snmp/snmp.c @@ -91,6 +91,8 @@ typedef struct snmp_session php_snmp_session; } \ } +static int mib_needs_reset; + ZEND_DECLARE_MODULE_GLOBALS(snmp) static PHP_GINIT_FUNCTION(snmp); @@ -1628,6 +1630,7 @@ PHP_FUNCTION(snmp_read_mib) RETURN_THROWS(); } + mib_needs_reset = 1; if (!read_mib(filename)) { char *error = strerror(errno); php_error_docref(NULL, E_WARNING, "Error while reading MIB file '%s': %s", filename, error); @@ -1637,6 +1640,27 @@ PHP_FUNCTION(snmp_read_mib) } /* }}} */ +/* {{{ Resets the MIB tree and set the mib directories to the provided mibdirs. */ +PHP_FUNCTION(snmp_init_mib) +{ + zend_string *mibdirs = NULL; + + ZEND_PARSE_PARAMETERS_START(0, 1) + Z_PARAM_OPTIONAL + Z_PARAM_STR_OR_NULL(mibdirs) + ZEND_PARSE_PARAMETERS_END(); + + // If the mibdirs has been changed, we need to reset the MIB tree at the end of the request + if (mibdirs != NULL) { + mib_needs_reset = 1; + } + + shutdown_mib(); + netsnmp_ds_set_string(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_MIBDIRS, ZSTR_VAL(mibdirs)); + init_mib(); +} +/* }}} */ + /* {{{ Creates a new SNMP session to specified host. */ PHP_METHOD(SNMP, __construct) { @@ -2173,6 +2197,19 @@ PHP_MSHUTDOWN_FUNCTION(snmp) } /* }}} */ +/* {{{ PHP_RSHUTDOWN_FUNCTION */ +static PHP_RSHUTDOWN_FUNCTION(snmp) +{ + if (mib_needs_reset) { + shutdown_mib(); + netsnmp_ds_set_string(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_MIBDIRS, NULL); + init_mib(); + } + + return SUCCESS; +} +/* }}} */ + /* {{{ PHP_MINFO_FUNCTION */ PHP_MINFO_FUNCTION(snmp) { @@ -2200,7 +2237,7 @@ zend_module_entry snmp_module_entry = { PHP_MINIT(snmp), PHP_MSHUTDOWN(snmp), NULL, - NULL, + PHP_RSHUTDOWN(snmp), PHP_MINFO(snmp), PHP_SNMP_VERSION, PHP_MODULE_GLOBALS(snmp), diff --git a/ext/snmp/snmp.stub.php b/ext/snmp/snmp.stub.php index 0a303aea77ff0..9bfb47e640dd3 100644 --- a/ext/snmp/snmp.stub.php +++ b/ext/snmp/snmp.stub.php @@ -181,6 +181,8 @@ function snmp_get_valueretrieval(): int {} function snmp_read_mib(string $filename): bool {} +function snmp_init_mib(?string $mibdirs): void {} + class SNMP { /** @cvalue SNMP_VERSION_1 */ diff --git a/ext/snmp/snmp_arginfo.h b/ext/snmp/snmp_arginfo.h index 1ee821f0538da..7e3d647ed7e21 100644 --- a/ext/snmp/snmp_arginfo.h +++ b/ext/snmp/snmp_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit snmp.stub.php instead. - * Stub hash: e2451ac3ea0fa5eb1158e8b7252e61c6794d514f */ + * Stub hash: 68dd2791d2ed4c931d99e012f6dc2d5d6f5e23b4 */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_snmpget, 0, 3, IS_MIXED, 0) ZEND_ARG_TYPE_INFO(0, hostname, IS_STRING, 0) @@ -114,6 +114,10 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_snmp_read_mib, 0, 1, _IS_BOOL, 0 ZEND_ARG_TYPE_INFO(0, filename, IS_STRING, 0) ZEND_END_ARG_INFO() +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_snmp_init_mib, 0, 1, IS_VOID, 0) + ZEND_ARG_TYPE_INFO(0, mibdirs, IS_STRING, 1) +ZEND_END_ARG_INFO() + ZEND_BEGIN_ARG_INFO_EX(arginfo_class_SNMP___construct, 0, 0, 3) ZEND_ARG_TYPE_INFO(0, version, IS_LONG, 0) ZEND_ARG_TYPE_INFO(0, hostname, IS_STRING, 0) @@ -185,6 +189,7 @@ ZEND_FUNCTION(snmp3_set); ZEND_FUNCTION(snmp_set_valueretrieval); ZEND_FUNCTION(snmp_get_valueretrieval); ZEND_FUNCTION(snmp_read_mib); +ZEND_FUNCTION(snmp_init_mib); ZEND_METHOD(SNMP, __construct); ZEND_METHOD(SNMP, close); ZEND_METHOD(SNMP, setSecurity); @@ -220,6 +225,7 @@ static const zend_function_entry ext_functions[] = { ZEND_FE(snmp_set_valueretrieval, arginfo_snmp_set_valueretrieval) ZEND_FE(snmp_get_valueretrieval, arginfo_snmp_get_valueretrieval) ZEND_FE(snmp_read_mib, arginfo_snmp_read_mib) + ZEND_FE(snmp_init_mib, arginfo_snmp_init_mib) ZEND_FE_END };