summaryrefslogtreecommitdiff
path: root/src/config.c
diff options
context:
space:
mode:
authoryourtree <56780191+yourtree@users.noreply.github.com>2022-08-21 22:55:45 +0800
committerGitHub <noreply@github.com>2022-08-21 17:55:45 +0300
commitca6aeadfbef8a0da8817f9f74a7bde1f5df004f7 (patch)
treecfef91d7fff74cad6898cd2418e55b9f7cbba2ba /src/config.c
parent31ef410e88dabb3f0d475dd3cae9edc00ba55aa7 (diff)
downloadredis-ca6aeadfbef8a0da8817f9f74a7bde1f5df004f7.tar.gz
Support setlocale via CONFIG operation. (#11059)
Till now Redis officially supported tuning it via environment variable see #1074. But we had other requests to allow changing it at runtime, see #799, and #11041. Note that `strcoll()` is used as Lua comparison function and also for comparison of certain string objects in Redis, which leads to a problem that, in different regions, for some characters, the result may be different. Below is an example. ``` 127.0.0.1:6333> SORT test alpha 1) "<" 2) ">" 3) "," 4) "*" 127.0.0.1:6333> CONFIG GET locale-collate 1) "locale-collate" 2) "" 127.0.0.1:6333> CONFIG SET locale-collate 1 (error) ERR CONFIG SET failed (possibly related to argument 'locale') 127.0.0.1:6333> CONFIG SET locale-collate C OK 127.0.0.1:6333> SORT test alpha 1) "*" 2) "," 3) "<" 4) ">" ``` That will cause accidental code compatibility issues for Lua scripts and some Redis commands. This commit creates a new config parameter to control the local environment which only affects `Collate` category. Above shows how it affects `SORT` command, and below shows the influence on Lua scripts. ``` 127.0.0.1:6333> CONFIG GET locale-collate 1) " locale-collate" 2) "C" 127.0.0.1:6333> EVAL "return ',' < '*'" 0 (nil) 127.0.0.1:6333> CONFIG SET locale-collate "" OK 127.0.0.1:6333> EVAL "return ',' < '*'" 0 (integer) 1 ``` Co-authored-by: calvincjli <calvincjli@tencent.com> Co-authored-by: Oran Agra <oran@redislabs.com>
Diffstat (limited to 'src/config.c')
-rw-r--r--src/config.c11
1 files changed, 11 insertions, 0 deletions
diff --git a/src/config.c b/src/config.c
index 0ffe65a61..f98225cbe 100644
--- a/src/config.c
+++ b/src/config.c
@@ -35,6 +35,7 @@
#include <sys/stat.h>
#include <glob.h>
#include <string.h>
+#include <locale.h>
/*-----------------------------------------------------------------------------
* Config file name-value maps.
@@ -2400,6 +2401,15 @@ static int isValidProcTitleTemplate(char *val, const char **err) {
return 1;
}
+static int updateLocaleCollate(const char **err) {
+ const char *s = setlocale(LC_COLLATE, server.locale_collate);
+ if (s == NULL) {
+ *err = "Invalid locale name";
+ return 0;
+ }
+ return 1;
+}
+
static int updateProcTitleTemplate(const char **err) {
if (redisSetProcTitle(NULL) == C_ERR) {
*err = "failed to set process title";
@@ -2995,6 +3005,7 @@ standardConfig static_configs[] = {
createStringConfig("proc-title-template", NULL, MODIFIABLE_CONFIG, ALLOW_EMPTY_STRING, server.proc_title_template, CONFIG_DEFAULT_PROC_TITLE_TEMPLATE, isValidProcTitleTemplate, updateProcTitleTemplate),
createStringConfig("bind-source-addr", NULL, MODIFIABLE_CONFIG, EMPTY_STRING_IS_NULL, server.bind_source_addr, NULL, NULL, NULL),
createStringConfig("logfile", NULL, IMMUTABLE_CONFIG, ALLOW_EMPTY_STRING, server.logfile, "", NULL, NULL),
+ createStringConfig("locale-collate", NULL, MODIFIABLE_CONFIG, ALLOW_EMPTY_STRING, server.locale_collate, "", NULL, updateLocaleCollate),
/* SDS Configs */
createSDSConfig("masterauth", NULL, MODIFIABLE_CONFIG | SENSITIVE_CONFIG, EMPTY_STRING_IS_NULL, server.masterauth, NULL, NULL, NULL),