diff --git a/src/module.c b/src/module.c index 9f426ffca3..42c9ab5973 100644 --- a/src/module.c +++ b/src/module.c @@ -2254,6 +2254,33 @@ int moduleIsModuleCommand(void *module_handle, struct serverCommand *cmd) { return (cp->module == module_handle); } +/* ValkeyModule_UpdateRuntimeArgs can be used to update the values of module->loadmod + * so that the updated values can be saved into conf file once 'config rewrite' command + * is called + * One example can be found in file modules/moduleparameter.c + * + * Returns: + * - VALKEYMODULE_OK on successfully updating. + * - VALKEYMODULE_ERR on failure. + */ +int VM_UpdateRuntimeArgs(ValkeyModuleCtx *ctx, int argc, ValkeyModuleString **argv) { + if (!ctx->module->onload) { + return VALKEYMODULE_ERR; + } + struct moduleLoadQueueEntry *loadmod = ctx->module->loadmod; + for (int i = 0; i < loadmod->argc; i++) { + decrRefCount(loadmod->argv[i]); + } + zfree(loadmod->argv); + loadmod->argv = argc ? zmalloc(sizeof(robj *) * argc) : NULL; + loadmod->argc = argc; + for (int i = 0; i < argc; i++) { + loadmod->argv[i] = argv[i]; + incrRefCount(loadmod->argv[i]); + } + return VALKEYMODULE_OK; +} + /* -------------------------------------------------------------------------- * ## Module information and time measurement * -------------------------------------------------------------------------- */ @@ -3042,23 +3069,6 @@ client *moduleGetReplyClient(ValkeyModuleCtx *ctx) { } } -int VM_UpdateRuntimeArgs(ValkeyModuleCtx *ctx, int argc, ValkeyModuleString **argv) { - client *c = moduleGetReplyClient(ctx); - if (c == NULL) return VALKEYMODULE_OK; - struct moduleLoadQueueEntry *loadmod = ctx->module->loadmod; - for (int i = 0; i < loadmod->argc; i++) { - decrRefCount(loadmod->argv[i]); - } - zfree(loadmod->argv); - loadmod->argv = argc ? zmalloc(sizeof(robj *) * argc) : NULL; - loadmod->argc = argc; - for (int i = 0; i < argc; i++) { - loadmod->argv[i] = argv[i]; - incrRefCount(loadmod->argv[i]); - } - return VALKEYMODULE_OK; -} - /* Send an integer reply to the client, with the specified `long long` value. * The function always returns VALKEYMODULE_OK. */ int VM_ReplyWithLongLong(ValkeyModuleCtx *ctx, long long ll) { diff --git a/tests/unit/moduleapi/moduleconfigs.tcl b/tests/unit/moduleapi/moduleconfigs.tcl index b29d808b80..2c03e6e8dd 100644 --- a/tests/unit/moduleapi/moduleconfigs.tcl +++ b/tests/unit/moduleapi/moduleconfigs.tcl @@ -256,6 +256,3 @@ start_server {tags {"modules"}} { string match "40 50 60 70" [lmap x [r module list] {dict get $x args}] } } - - -