Commit a3b61b4dee7 for php.net
commit a3b61b4dee7abd2ad8107cc4296cf969cf4fbb52
Author: eskyuu <swilton@fluentit.au>
Date: Fri Aug 21 19:59:59 2026 +0800
Add snmp_init_mib function to allow MIB tree to be reset using environment variables (#21452)
RFC: https://wiki.php.net/rfc/snmp_improvements_2026#allow_the_snmp_mib_to_be_reset
diff --git a/ext/snmp/snmp.c b/ext/snmp/snmp.c
index 0c3e53614a6..e921adb9835 100644
--- a/ext/snmp/snmp.c
+++ b/ext/snmp/snmp.c
@@ -89,6 +89,8 @@ typedef struct snmp_session php_snmp_session;
} \
}
+static bool mib_needs_reset;
+
ZEND_DECLARE_MODULE_GLOBALS(snmp)
static PHP_GINIT_FUNCTION(snmp);
@@ -1658,6 +1660,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);
@@ -1667,6 +1670,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_PATH_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)
{
@@ -2201,6 +2225,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)
{
@@ -2228,7 +2265,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 0283f7ff762..0f939b5b23f 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 {}
+
/** @not-serializable */
class SNMP
{
diff --git a/ext/snmp/snmp_arginfo.h b/ext/snmp/snmp_arginfo.h
index 07caa70fa9e..85b9a1131f1 100644
Binary files a/ext/snmp/snmp_arginfo.h and b/ext/snmp/snmp_arginfo.h differ