Enable ctl on partial mib and partial name

This commit is contained in:
Yinan Zhang 2020-08-13 15:26:46 -07:00
parent 006dd0414e
commit 4557c0a67d
3 changed files with 98 additions and 0 deletions

View File

@ -102,6 +102,8 @@ int ctl_bymib(tsd_t *tsd, const size_t *mib, size_t miblen, void *oldp,
size_t *oldlenp, void *newp, size_t newlen);
int ctl_mibnametomib(tsd_t *tsd, size_t *mib, size_t miblen, const char *name,
size_t *miblenp);
int ctl_bymibname(tsd_t *tsd, size_t *mib, size_t miblen, const char *name,
size_t *miblenp, void *oldp, size_t *oldlenp, void *newp, size_t newlen);
bool ctl_boot(void);
void ctl_prefork(tsdn_t *tsdn);
void ctl_postfork_parent(tsdn_t *tsdn);

View File

@ -1558,6 +1558,51 @@ label_return:
return(ret);
}
int
ctl_bymibname(tsd_t *tsd, size_t *mib, size_t miblen, const char *name,
size_t *miblenp, void *oldp, size_t *oldlenp, void *newp, size_t newlen) {
int ret;
const ctl_named_node_t *node;
if (!ctl_initialized && ctl_init(tsd)) {
ret = EAGAIN;
goto label_return;
}
ret = ctl_lookupbymib(tsd_tsdn(tsd), &node, mib, miblen);
if (ret != 0) {
goto label_return;
}
if (node == NULL || node->ctl != NULL) {
ret = ENOENT;
goto label_return;
}
assert(miblenp != NULL);
assert(*miblenp >= miblen);
*miblenp -= miblen;
/*
* The same node supplies the starting node and stores the ending node.
*/
ret = ctl_lookup(tsd_tsdn(tsd), node, name, &node, mib + miblen,
miblenp);
*miblenp += miblen;
if (ret != 0) {
goto label_return;
}
if (node != NULL && node->ctl) {
ret = node->ctl(tsd, mib, *miblenp, oldp, oldlenp, newp,
newlen);
} else {
/* The name refers to a partial path through the ctl tree. */
ret = ENOENT;
}
label_return:
return(ret);
}
bool
ctl_boot(void) {
if (malloc_mutex_init(&ctl_mtx, "ctl", WITNESS_RANK_CTL,

View File

@ -187,6 +187,56 @@ TEST_BEGIN(test_mallctlmibnametomib) {
}
TEST_END
TEST_BEGIN(test_mallctlbymibname) {
size_t mib[4];
size_t miblen = 4;
uint32_t result, result_ref;
size_t len_result = sizeof(uint32_t);
tsd_t *tsd = tsd_fetch();
/* Error cases. */
assert_d_eq(mallctlnametomib("arenas", mib, &miblen), 0,
"Unexpected mallctlnametomib() failure");
assert_zu_eq(miblen, 1, "");
miblen = 4;
assert_d_eq(ctl_bymibname(tsd, mib, 1, "bin.0", &miblen,
&result, &len_result, NULL, 0), ENOENT, "");
miblen = 4;
assert_d_eq(ctl_bymibname(tsd, mib, 1, "bin.0.bob", &miblen,
&result, &len_result, NULL, 0), ENOENT, "");
assert_zu_eq(miblen, 4, "");
/* Valid cases. */
assert_d_eq(mallctl("arenas.bin.0.nregs", &result_ref, &len_result,
NULL, 0), 0, "Unexpected mallctl() failure");
miblen = 4;
assert_d_eq(ctl_bymibname(tsd, mib, 0, "arenas.bin.0.nregs", &miblen,
&result, &len_result, NULL, 0), 0, "");
assert_zu_eq(miblen, 4, "");
expect_zu_eq(result, result_ref, "Unexpected result");
assert_d_eq(ctl_bymibname(tsd, mib, 1, "bin.0.nregs", &miblen, &result,
&len_result, NULL, 0), 0, "");
assert_zu_eq(miblen, 4, "");
expect_zu_eq(result, result_ref, "Unexpected result");
assert_d_eq(ctl_bymibname(tsd, mib, 2, "0.nregs", &miblen, &result,
&len_result, NULL, 0), 0, "");
assert_zu_eq(miblen, 4, "");
expect_zu_eq(result, result_ref, "Unexpected result");
assert_d_eq(ctl_bymibname(tsd, mib, 3, "nregs", &miblen, &result,
&len_result, NULL, 0), 0, "");
assert_zu_eq(miblen, 4, "");
expect_zu_eq(result, result_ref, "Unexpected result");
}
TEST_END
TEST_BEGIN(test_mallctl_config) {
#define TEST_MALLCTL_CONFIG(config, t) do { \
t oldval; \
@ -1178,6 +1228,7 @@ main(void) {
test_mallctlnametomib_short_mib,
test_mallctlnametomib_short_name,
test_mallctlmibnametomib,
test_mallctlbymibname,
test_mallctl_config,
test_mallctl_opt,
test_manpage_example,