[PHP-CVS] [php-src] master: Add snmp_init_mib function to allow MIB tree to be reset using environment variables (#21452)

[email protected] (eskyuu via GitHub)
Newsgroups php.cvs
Message-ID <[email protected]>
Author: eskyuu (eskyuu)
Committer: GitHub (web-flow)
Pusher: Girgias
Date: 2026-08-21T12:59:59+01:00

Commit: https://github.com/php/php-src/commit/a3b61b4dee7abd2ad8107cc4296cf969cf4fbb52
Raw diff: https://github.com/php/php-src/commit/a3b61b4dee7abd2ad8107cc4296cf969cf4fbb52.diff

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

Changed paths:
  M  ext/snmp/snmp.c
  M  ext/snmp/snmp.stub.php
  M  ext/snmp/snmp_arginfo.h


Diff:

diff --git a/ext/snmp/snmp.c b/ext/snmp/snmp.c
index 0c3e53614a6a..e921adb98352 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 0283f7ff7620..0f939b5b23fd 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 07caa70fa9eb..85b9a1131f1c 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: 20039fa88cb9f8a861bf3bf3e2e5e291e50c6a12 */
+ * Stub hash: 18e6ffa1acd3c1b6d1c962be97f36e9a46a317a2 */
 
 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
 };
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.