3 /*-----------------------------------------------------------------------------
5 *----------------------------------------------------------------------------*/
7 int yesnotoi(char *s
) {
8 if (!strcasecmp(s
,"yes")) return 1;
9 else if (!strcasecmp(s
,"no")) return 0;
13 void appendServerSaveParams(time_t seconds
, int changes
) {
14 server
.saveparams
= zrealloc(server
.saveparams
,sizeof(struct saveparam
)*(server
.saveparamslen
+1));
15 server
.saveparams
[server
.saveparamslen
].seconds
= seconds
;
16 server
.saveparams
[server
.saveparamslen
].changes
= changes
;
17 server
.saveparamslen
++;
20 void resetServerSaveParams() {
21 zfree(server
.saveparams
);
22 server
.saveparams
= NULL
;
23 server
.saveparamslen
= 0;
26 void loadServerConfigFromString(char *config
) {
28 int linenum
= 0, totlines
, i
;
31 lines
= sdssplitlen(config
,strlen(config
),"\n",1,&totlines
);
33 for (i
= 0; i
< totlines
; i
++) {
38 lines
[i
] = sdstrim(lines
[i
]," \t\r\n");
40 /* Skip comments and blank lines*/
41 if (lines
[i
][0] == '#' || lines
[i
][0] == '\0') continue;
43 /* Split into arguments */
44 argv
= sdssplitargs(lines
[i
],&argc
);
47 /* Execute config directives */
48 if (!strcasecmp(argv
[0],"timeout") && argc
== 2) {
49 server
.maxidletime
= atoi(argv
[1]);
50 if (server
.maxidletime
< 0) {
51 err
= "Invalid timeout value"; goto loaderr
;
53 } else if (!strcasecmp(argv
[0],"port") && argc
== 2) {
54 server
.port
= atoi(argv
[1]);
55 if (server
.port
< 0 || server
.port
> 65535) {
56 err
= "Invalid port"; goto loaderr
;
58 } else if (!strcasecmp(argv
[0],"bind") && argc
== 2) {
59 server
.bindaddr
= zstrdup(argv
[1]);
60 } else if (!strcasecmp(argv
[0],"unixsocket") && argc
== 2) {
61 server
.unixsocket
= zstrdup(argv
[1]);
62 } else if (!strcasecmp(argv
[0],"unixsocketperm") && argc
== 2) {
64 server
.unixsocketperm
= (mode_t
)strtol(argv
[1], NULL
, 8);
65 if (errno
|| server
.unixsocketperm
> 0777) {
66 err
= "Invalid socket file permissions"; goto loaderr
;
68 } else if (!strcasecmp(argv
[0],"save")) {
70 int seconds
= atoi(argv
[1]);
71 int changes
= atoi(argv
[2]);
72 if (seconds
< 1 || changes
< 0) {
73 err
= "Invalid save parameters"; goto loaderr
;
75 appendServerSaveParams(seconds
,changes
);
76 } else if (argc
== 2 && !strcasecmp(argv
[1],"")) {
77 resetServerSaveParams();
79 } else if (!strcasecmp(argv
[0],"dir") && argc
== 2) {
80 if (chdir(argv
[1]) == -1) {
81 redisLog(REDIS_WARNING
,"Can't chdir to '%s': %s",
82 argv
[1], strerror(errno
));
85 } else if (!strcasecmp(argv
[0],"loglevel") && argc
== 2) {
86 if (!strcasecmp(argv
[1],"debug")) server
.verbosity
= REDIS_DEBUG
;
87 else if (!strcasecmp(argv
[1],"verbose")) server
.verbosity
= REDIS_VERBOSE
;
88 else if (!strcasecmp(argv
[1],"notice")) server
.verbosity
= REDIS_NOTICE
;
89 else if (!strcasecmp(argv
[1],"warning")) server
.verbosity
= REDIS_WARNING
;
91 err
= "Invalid log level. Must be one of debug, notice, warning";
94 } else if (!strcasecmp(argv
[0],"logfile") && argc
== 2) {
97 server
.logfile
= zstrdup(argv
[1]);
98 if (!strcasecmp(server
.logfile
,"stdout")) {
99 zfree(server
.logfile
);
100 server
.logfile
= NULL
;
102 if (server
.logfile
) {
103 /* Test if we are able to open the file. The server will not
104 * be able to abort just for this problem later... */
105 logfp
= fopen(server
.logfile
,"a");
107 err
= sdscatprintf(sdsempty(),
108 "Can't open the log file: %s", strerror(errno
));
113 } else if (!strcasecmp(argv
[0],"syslog-enabled") && argc
== 2) {
114 if ((server
.syslog_enabled
= yesnotoi(argv
[1])) == -1) {
115 err
= "argument must be 'yes' or 'no'"; goto loaderr
;
117 } else if (!strcasecmp(argv
[0],"syslog-ident") && argc
== 2) {
118 if (server
.syslog_ident
) zfree(server
.syslog_ident
);
119 server
.syslog_ident
= zstrdup(argv
[1]);
120 } else if (!strcasecmp(argv
[0],"syslog-facility") && argc
== 2) {
124 } validSyslogFacilities
[] = {
126 {"local0", LOG_LOCAL0
},
127 {"local1", LOG_LOCAL1
},
128 {"local2", LOG_LOCAL2
},
129 {"local3", LOG_LOCAL3
},
130 {"local4", LOG_LOCAL4
},
131 {"local5", LOG_LOCAL5
},
132 {"local6", LOG_LOCAL6
},
133 {"local7", LOG_LOCAL7
},
138 for (i
= 0; validSyslogFacilities
[i
].name
; i
++) {
139 if (!strcasecmp(validSyslogFacilities
[i
].name
, argv
[1])) {
140 server
.syslog_facility
= validSyslogFacilities
[i
].value
;
145 if (!validSyslogFacilities
[i
].name
) {
146 err
= "Invalid log facility. Must be one of USER or between LOCAL0-LOCAL7";
149 } else if (!strcasecmp(argv
[0],"databases") && argc
== 2) {
150 server
.dbnum
= atoi(argv
[1]);
151 if (server
.dbnum
< 1) {
152 err
= "Invalid number of databases"; goto loaderr
;
154 } else if (!strcasecmp(argv
[0],"include") && argc
== 2) {
155 loadServerConfig(argv
[1],NULL
);
156 } else if (!strcasecmp(argv
[0],"maxclients") && argc
== 2) {
157 server
.maxclients
= atoi(argv
[1]);
158 if (server
.maxclients
< 1) {
159 err
= "Invalid max clients limit"; goto loaderr
;
161 } else if (!strcasecmp(argv
[0],"maxmemory") && argc
== 2) {
162 server
.maxmemory
= memtoll(argv
[1],NULL
);
163 } else if (!strcasecmp(argv
[0],"maxmemory-policy") && argc
== 2) {
164 if (!strcasecmp(argv
[1],"volatile-lru")) {
165 server
.maxmemory_policy
= REDIS_MAXMEMORY_VOLATILE_LRU
;
166 } else if (!strcasecmp(argv
[1],"volatile-random")) {
167 server
.maxmemory_policy
= REDIS_MAXMEMORY_VOLATILE_RANDOM
;
168 } else if (!strcasecmp(argv
[1],"volatile-ttl")) {
169 server
.maxmemory_policy
= REDIS_MAXMEMORY_VOLATILE_TTL
;
170 } else if (!strcasecmp(argv
[1],"allkeys-lru")) {
171 server
.maxmemory_policy
= REDIS_MAXMEMORY_ALLKEYS_LRU
;
172 } else if (!strcasecmp(argv
[1],"allkeys-random")) {
173 server
.maxmemory_policy
= REDIS_MAXMEMORY_ALLKEYS_RANDOM
;
174 } else if (!strcasecmp(argv
[1],"noeviction")) {
175 server
.maxmemory_policy
= REDIS_MAXMEMORY_NO_EVICTION
;
177 err
= "Invalid maxmemory policy";
180 } else if (!strcasecmp(argv
[0],"maxmemory-samples") && argc
== 2) {
181 server
.maxmemory_samples
= atoi(argv
[1]);
182 if (server
.maxmemory_samples
<= 0) {
183 err
= "maxmemory-samples must be 1 or greater";
186 } else if (!strcasecmp(argv
[0],"slaveof") && argc
== 3) {
187 server
.masterhost
= sdsnew(argv
[1]);
188 server
.masterport
= atoi(argv
[2]);
189 server
.repl_state
= REDIS_REPL_CONNECT
;
190 } else if (!strcasecmp(argv
[0],"repl-ping-slave-period") && argc
== 2) {
191 server
.repl_ping_slave_period
= atoi(argv
[1]);
192 if (server
.repl_ping_slave_period
<= 0) {
193 err
= "repl-ping-slave-period must be 1 or greater";
196 } else if (!strcasecmp(argv
[0],"repl-timeout") && argc
== 2) {
197 server
.repl_timeout
= atoi(argv
[1]);
198 if (server
.repl_timeout
<= 0) {
199 err
= "repl-timeout must be 1 or greater";
202 } else if (!strcasecmp(argv
[0],"masterauth") && argc
== 2) {
203 server
.masterauth
= zstrdup(argv
[1]);
204 } else if (!strcasecmp(argv
[0],"slave-serve-stale-data") && argc
== 2) {
205 if ((server
.repl_serve_stale_data
= yesnotoi(argv
[1])) == -1) {
206 err
= "argument must be 'yes' or 'no'"; goto loaderr
;
208 } else if (!strcasecmp(argv
[0],"slave-read-only") && argc
== 2) {
209 if ((server
.repl_slave_ro
= yesnotoi(argv
[1])) == -1) {
210 err
= "argument must be 'yes' or 'no'"; goto loaderr
;
212 } else if (!strcasecmp(argv
[0],"rdbcompression") && argc
== 2) {
213 if ((server
.rdb_compression
= yesnotoi(argv
[1])) == -1) {
214 err
= "argument must be 'yes' or 'no'"; goto loaderr
;
216 } else if (!strcasecmp(argv
[0],"rdbchecksum") && argc
== 2) {
217 if ((server
.rdb_checksum
= yesnotoi(argv
[1])) == -1) {
218 err
= "argument must be 'yes' or 'no'"; goto loaderr
;
220 } else if (!strcasecmp(argv
[0],"activerehashing") && argc
== 2) {
221 if ((server
.activerehashing
= yesnotoi(argv
[1])) == -1) {
222 err
= "argument must be 'yes' or 'no'"; goto loaderr
;
224 } else if (!strcasecmp(argv
[0],"daemonize") && argc
== 2) {
225 if ((server
.daemonize
= yesnotoi(argv
[1])) == -1) {
226 err
= "argument must be 'yes' or 'no'"; goto loaderr
;
228 } else if (!strcasecmp(argv
[0],"appendonly") && argc
== 2) {
231 if ((yes
= yesnotoi(argv
[1])) == -1) {
232 err
= "argument must be 'yes' or 'no'"; goto loaderr
;
234 server
.aof_state
= yes
? REDIS_AOF_ON
: REDIS_AOF_OFF
;
235 } else if (!strcasecmp(argv
[0],"appendfilename") && argc
== 2) {
236 zfree(server
.aof_filename
);
237 server
.aof_filename
= zstrdup(argv
[1]);
238 } else if (!strcasecmp(argv
[0],"no-appendfsync-on-rewrite")
240 if ((server
.aof_no_fsync_on_rewrite
= yesnotoi(argv
[1])) == -1) {
241 err
= "argument must be 'yes' or 'no'"; goto loaderr
;
243 } else if (!strcasecmp(argv
[0],"appendfsync") && argc
== 2) {
244 if (!strcasecmp(argv
[1],"no")) {
245 server
.aof_fsync
= AOF_FSYNC_NO
;
246 } else if (!strcasecmp(argv
[1],"always")) {
247 server
.aof_fsync
= AOF_FSYNC_ALWAYS
;
248 } else if (!strcasecmp(argv
[1],"everysec")) {
249 server
.aof_fsync
= AOF_FSYNC_EVERYSEC
;
251 err
= "argument must be 'no', 'always' or 'everysec'";
254 } else if (!strcasecmp(argv
[0],"auto-aof-rewrite-percentage") &&
257 server
.aof_rewrite_perc
= atoi(argv
[1]);
258 if (server
.aof_rewrite_perc
< 0) {
259 err
= "Invalid negative percentage for AOF auto rewrite";
262 } else if (!strcasecmp(argv
[0],"auto-aof-rewrite-min-size") &&
265 server
.aof_rewrite_min_size
= memtoll(argv
[1],NULL
);
266 } else if (!strcasecmp(argv
[0],"requirepass") && argc
== 2) {
267 if (strlen(argv
[1]) > REDIS_AUTHPASS_MAX_LEN
) {
268 err
= "Password is longer than REDIS_AUTHPASS_MAX_LEN";
271 server
.requirepass
= zstrdup(argv
[1]);
272 } else if (!strcasecmp(argv
[0],"pidfile") && argc
== 2) {
273 zfree(server
.pidfile
);
274 server
.pidfile
= zstrdup(argv
[1]);
275 } else if (!strcasecmp(argv
[0],"dbfilename") && argc
== 2) {
276 zfree(server
.rdb_filename
);
277 server
.rdb_filename
= zstrdup(argv
[1]);
278 } else if (!strcasecmp(argv
[0],"hash-max-ziplist-entries") && argc
== 2) {
279 server
.hash_max_ziplist_entries
= memtoll(argv
[1], NULL
);
280 } else if (!strcasecmp(argv
[0],"hash-max-ziplist-value") && argc
== 2) {
281 server
.hash_max_ziplist_value
= memtoll(argv
[1], NULL
);
282 } else if (!strcasecmp(argv
[0],"list-max-ziplist-entries") && argc
== 2){
283 server
.list_max_ziplist_entries
= memtoll(argv
[1], NULL
);
284 } else if (!strcasecmp(argv
[0],"list-max-ziplist-value") && argc
== 2) {
285 server
.list_max_ziplist_value
= memtoll(argv
[1], NULL
);
286 } else if (!strcasecmp(argv
[0],"set-max-intset-entries") && argc
== 2) {
287 server
.set_max_intset_entries
= memtoll(argv
[1], NULL
);
288 } else if (!strcasecmp(argv
[0],"zset-max-ziplist-entries") && argc
== 2) {
289 server
.zset_max_ziplist_entries
= memtoll(argv
[1], NULL
);
290 } else if (!strcasecmp(argv
[0],"zset-max-ziplist-value") && argc
== 2) {
291 server
.zset_max_ziplist_value
= memtoll(argv
[1], NULL
);
292 } else if (!strcasecmp(argv
[0],"rename-command") && argc
== 3) {
293 struct redisCommand
*cmd
= lookupCommand(argv
[1]);
297 err
= "No such command in rename-command";
301 /* If the target command name is the emtpy string we just
302 * remove it from the command table. */
303 retval
= dictDelete(server
.commands
, argv
[1]);
304 redisAssert(retval
== DICT_OK
);
306 /* Otherwise we re-add the command under a different name. */
307 if (sdslen(argv
[2]) != 0) {
308 sds copy
= sdsdup(argv
[2]);
310 retval
= dictAdd(server
.commands
, copy
, cmd
);
311 if (retval
!= DICT_OK
) {
313 err
= "Target command name already exists"; goto loaderr
;
316 } else if (!strcasecmp(argv
[0],"cluster-enabled") && argc
== 2) {
317 if ((server
.cluster_enabled
= yesnotoi(argv
[1])) == -1) {
318 err
= "argument must be 'yes' or 'no'"; goto loaderr
;
320 } else if (!strcasecmp(argv
[0],"cluster-config-file") && argc
== 2) {
321 zfree(server
.cluster
.configfile
);
322 server
.cluster
.configfile
= zstrdup(argv
[1]);
323 } else if (!strcasecmp(argv
[0],"lua-time-limit") && argc
== 2) {
324 server
.lua_time_limit
= strtoll(argv
[1],NULL
,10);
325 } else if (!strcasecmp(argv
[0],"slowlog-log-slower-than") &&
328 server
.slowlog_log_slower_than
= strtoll(argv
[1],NULL
,10);
329 } else if (!strcasecmp(argv
[0],"slowlog-max-len") && argc
== 2) {
330 server
.slowlog_max_len
= strtoll(argv
[1],NULL
,10);
331 } else if (!strcasecmp(argv
[0],"client-output-buffer-limit") &&
334 int class = getClientLimitClassByName(argv
[1]);
335 unsigned long long hard
, soft
;
339 err
= "Unrecognized client limit class";
342 hard
= memtoll(argv
[2],NULL
);
343 soft
= memtoll(argv
[3],NULL
);
344 soft_seconds
= atoi(argv
[4]);
345 if (soft_seconds
< 0) {
346 err
= "Negative number of seconds in soft limt is invalid";
349 server
.client_obuf_limits
[class].hard_limit_bytes
= hard
;
350 server
.client_obuf_limits
[class].soft_limit_bytes
= soft
;
351 server
.client_obuf_limits
[class].soft_limit_seconds
= soft_seconds
;
352 } else if (!strcasecmp(argv
[0],"stop-writes-on-bgsave-error") &&
354 if ((server
.stop_writes_on_bgsave_err
= yesnotoi(argv
[1])) == -1) {
355 err
= "argument must be 'yes' or 'no'"; goto loaderr
;
357 } else if (!strcasecmp(argv
[0],"slave-priority") && argc
== 2) {
358 server
.slave_priority
= atoi(argv
[1]);
359 } else if (!strcasecmp(argv
[0],"sentinel")) {
360 /* argc == 1 is handled by main() as we need to enter the sentinel
363 if (!server
.sentinel_mode
) {
364 err
= "sentinel directive while not in sentinel mode";
367 err
= sentinelHandleConfiguration(argv
+1,argc
-1);
368 if (err
) goto loaderr
;
371 err
= "Bad directive or wrong number of arguments"; goto loaderr
;
373 sdsfreesplitres(argv
,argc
);
375 sdsfreesplitres(lines
,totlines
);
379 fprintf(stderr
, "\n*** FATAL CONFIG FILE ERROR ***\n");
380 fprintf(stderr
, "Reading the configuration file, at line %d\n", linenum
);
381 fprintf(stderr
, ">>> '%s'\n", lines
[i
]);
382 fprintf(stderr
, "%s\n", err
);
386 /* Load the server configuration from the specified filename.
387 * The function appends the additional configuration directives stored
388 * in the 'options' string to the config file before loading.
390 * Both filename and options can be NULL, in such a case are considered
391 * emtpy. This way loadServerConfig can be used to just load a file or
392 * just load a string. */
393 void loadServerConfig(char *filename
, char *options
) {
394 sds config
= sdsempty();
395 char buf
[REDIS_CONFIGLINE_MAX
+1];
397 /* Load the file content */
401 if (filename
[0] == '-' && filename
[1] == '\0') {
404 if ((fp
= fopen(filename
,"r")) == NULL
) {
405 redisLog(REDIS_WARNING
,
406 "Fatal error, can't open config file '%s'", filename
);
410 while(fgets(buf
,REDIS_CONFIGLINE_MAX
+1,fp
) != NULL
)
411 config
= sdscat(config
,buf
);
412 if (fp
!= stdin
) fclose(fp
);
414 /* Append the additional options */
416 config
= sdscat(config
,"\n");
417 config
= sdscat(config
,options
);
419 loadServerConfigFromString(config
);
423 /*-----------------------------------------------------------------------------
424 * CONFIG command for remote configuration
425 *----------------------------------------------------------------------------*/
427 void configSetCommand(redisClient
*c
) {
430 redisAssertWithInfo(c
,c
->argv
[2],c
->argv
[2]->encoding
== REDIS_ENCODING_RAW
);
431 redisAssertWithInfo(c
,c
->argv
[2],c
->argv
[3]->encoding
== REDIS_ENCODING_RAW
);
434 if (!strcasecmp(c
->argv
[2]->ptr
,"dbfilename")) {
435 zfree(server
.rdb_filename
);
436 server
.rdb_filename
= zstrdup(o
->ptr
);
437 } else if (!strcasecmp(c
->argv
[2]->ptr
,"requirepass")) {
438 if (sdslen(o
->ptr
) > REDIS_AUTHPASS_MAX_LEN
) goto badfmt
;
439 zfree(server
.requirepass
);
440 server
.requirepass
= ((char*)o
->ptr
)[0] ? zstrdup(o
->ptr
) : NULL
;
441 } else if (!strcasecmp(c
->argv
[2]->ptr
,"masterauth")) {
442 zfree(server
.masterauth
);
443 server
.masterauth
= zstrdup(o
->ptr
);
444 } else if (!strcasecmp(c
->argv
[2]->ptr
,"maxmemory")) {
445 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
||
447 server
.maxmemory
= ll
;
448 if (server
.maxmemory
) {
449 if (server
.maxmemory
< zmalloc_used_memory()) {
450 redisLog(REDIS_WARNING
,"WARNING: the new maxmemory value set via CONFIG SET is smaller than the current memory usage. This will result in keys eviction and/or inability to accept new write commands depending on the maxmemory-policy.");
452 freeMemoryIfNeeded();
454 } else if (!strcasecmp(c
->argv
[2]->ptr
,"maxmemory-policy")) {
455 if (!strcasecmp(o
->ptr
,"volatile-lru")) {
456 server
.maxmemory_policy
= REDIS_MAXMEMORY_VOLATILE_LRU
;
457 } else if (!strcasecmp(o
->ptr
,"volatile-random")) {
458 server
.maxmemory_policy
= REDIS_MAXMEMORY_VOLATILE_RANDOM
;
459 } else if (!strcasecmp(o
->ptr
,"volatile-ttl")) {
460 server
.maxmemory_policy
= REDIS_MAXMEMORY_VOLATILE_TTL
;
461 } else if (!strcasecmp(o
->ptr
,"allkeys-lru")) {
462 server
.maxmemory_policy
= REDIS_MAXMEMORY_ALLKEYS_LRU
;
463 } else if (!strcasecmp(o
->ptr
,"allkeys-random")) {
464 server
.maxmemory_policy
= REDIS_MAXMEMORY_ALLKEYS_RANDOM
;
465 } else if (!strcasecmp(o
->ptr
,"noeviction")) {
466 server
.maxmemory_policy
= REDIS_MAXMEMORY_NO_EVICTION
;
470 } else if (!strcasecmp(c
->argv
[2]->ptr
,"maxmemory-samples")) {
471 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
||
472 ll
<= 0) goto badfmt
;
473 server
.maxmemory_samples
= ll
;
474 } else if (!strcasecmp(c
->argv
[2]->ptr
,"timeout")) {
475 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
||
476 ll
< 0 || ll
> LONG_MAX
) goto badfmt
;
477 server
.maxidletime
= ll
;
478 } else if (!strcasecmp(c
->argv
[2]->ptr
,"appendfsync")) {
479 if (!strcasecmp(o
->ptr
,"no")) {
480 server
.aof_fsync
= AOF_FSYNC_NO
;
481 } else if (!strcasecmp(o
->ptr
,"everysec")) {
482 server
.aof_fsync
= AOF_FSYNC_EVERYSEC
;
483 } else if (!strcasecmp(o
->ptr
,"always")) {
484 server
.aof_fsync
= AOF_FSYNC_ALWAYS
;
488 } else if (!strcasecmp(c
->argv
[2]->ptr
,"no-appendfsync-on-rewrite")) {
489 int yn
= yesnotoi(o
->ptr
);
491 if (yn
== -1) goto badfmt
;
492 server
.aof_no_fsync_on_rewrite
= yn
;
493 } else if (!strcasecmp(c
->argv
[2]->ptr
,"appendonly")) {
494 int enable
= yesnotoi(o
->ptr
);
496 if (enable
== -1) goto badfmt
;
497 if (enable
== 0 && server
.aof_state
!= REDIS_AOF_OFF
) {
499 } else if (enable
&& server
.aof_state
== REDIS_AOF_OFF
) {
500 if (startAppendOnly() == REDIS_ERR
) {
502 "Unable to turn on AOF. Check server logs.");
506 } else if (!strcasecmp(c
->argv
[2]->ptr
,"auto-aof-rewrite-percentage")) {
507 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
|| ll
< 0) goto badfmt
;
508 server
.aof_rewrite_perc
= ll
;
509 } else if (!strcasecmp(c
->argv
[2]->ptr
,"auto-aof-rewrite-min-size")) {
510 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
|| ll
< 0) goto badfmt
;
511 server
.aof_rewrite_min_size
= ll
;
512 } else if (!strcasecmp(c
->argv
[2]->ptr
,"save")) {
514 sds
*v
= sdssplitlen(o
->ptr
,sdslen(o
->ptr
)," ",1,&vlen
);
516 /* Perform sanity check before setting the new config:
517 * - Even number of args
518 * - Seconds >= 1, changes >= 0 */
520 sdsfreesplitres(v
,vlen
);
523 for (j
= 0; j
< vlen
; j
++) {
527 val
= strtoll(v
[j
], &eptr
, 10);
528 if (eptr
[0] != '\0' ||
529 ((j
& 1) == 0 && val
< 1) ||
530 ((j
& 1) == 1 && val
< 0)) {
531 sdsfreesplitres(v
,vlen
);
535 /* Finally set the new config */
536 resetServerSaveParams();
537 for (j
= 0; j
< vlen
; j
+= 2) {
541 seconds
= strtoll(v
[j
],NULL
,10);
542 changes
= strtoll(v
[j
+1],NULL
,10);
543 appendServerSaveParams(seconds
, changes
);
545 sdsfreesplitres(v
,vlen
);
546 } else if (!strcasecmp(c
->argv
[2]->ptr
,"slave-serve-stale-data")) {
547 int yn
= yesnotoi(o
->ptr
);
549 if (yn
== -1) goto badfmt
;
550 server
.repl_serve_stale_data
= yn
;
551 } else if (!strcasecmp(c
->argv
[2]->ptr
,"slave-read-only")) {
552 int yn
= yesnotoi(o
->ptr
);
554 if (yn
== -1) goto badfmt
;
555 server
.repl_slave_ro
= yn
;
556 } else if (!strcasecmp(c
->argv
[2]->ptr
,"dir")) {
557 if (chdir((char*)o
->ptr
) == -1) {
558 addReplyErrorFormat(c
,"Changing directory: %s", strerror(errno
));
561 } else if (!strcasecmp(c
->argv
[2]->ptr
,"hash-max-ziplist-entries")) {
562 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
|| ll
< 0) goto badfmt
;
563 server
.hash_max_ziplist_entries
= ll
;
564 } else if (!strcasecmp(c
->argv
[2]->ptr
,"hash-max-ziplist-value")) {
565 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
|| ll
< 0) goto badfmt
;
566 server
.hash_max_ziplist_value
= ll
;
567 } else if (!strcasecmp(c
->argv
[2]->ptr
,"list-max-ziplist-entries")) {
568 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
|| ll
< 0) goto badfmt
;
569 server
.list_max_ziplist_entries
= ll
;
570 } else if (!strcasecmp(c
->argv
[2]->ptr
,"list-max-ziplist-value")) {
571 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
|| ll
< 0) goto badfmt
;
572 server
.list_max_ziplist_value
= ll
;
573 } else if (!strcasecmp(c
->argv
[2]->ptr
,"set-max-intset-entries")) {
574 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
|| ll
< 0) goto badfmt
;
575 server
.set_max_intset_entries
= ll
;
576 } else if (!strcasecmp(c
->argv
[2]->ptr
,"zset-max-ziplist-entries")) {
577 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
|| ll
< 0) goto badfmt
;
578 server
.zset_max_ziplist_entries
= ll
;
579 } else if (!strcasecmp(c
->argv
[2]->ptr
,"zset-max-ziplist-value")) {
580 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
|| ll
< 0) goto badfmt
;
581 server
.zset_max_ziplist_value
= ll
;
582 } else if (!strcasecmp(c
->argv
[2]->ptr
,"lua-time-limit")) {
583 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
|| ll
< 0) goto badfmt
;
584 server
.lua_time_limit
= ll
;
585 } else if (!strcasecmp(c
->argv
[2]->ptr
,"slowlog-log-slower-than")) {
586 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
) goto badfmt
;
587 server
.slowlog_log_slower_than
= ll
;
588 } else if (!strcasecmp(c
->argv
[2]->ptr
,"slowlog-max-len")) {
589 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
|| ll
< 0) goto badfmt
;
590 server
.slowlog_max_len
= (unsigned)ll
;
591 } else if (!strcasecmp(c
->argv
[2]->ptr
,"loglevel")) {
592 if (!strcasecmp(o
->ptr
,"warning")) {
593 server
.verbosity
= REDIS_WARNING
;
594 } else if (!strcasecmp(o
->ptr
,"notice")) {
595 server
.verbosity
= REDIS_NOTICE
;
596 } else if (!strcasecmp(o
->ptr
,"verbose")) {
597 server
.verbosity
= REDIS_VERBOSE
;
598 } else if (!strcasecmp(o
->ptr
,"debug")) {
599 server
.verbosity
= REDIS_DEBUG
;
603 } else if (!strcasecmp(c
->argv
[2]->ptr
,"client-output-buffer-limit")) {
605 sds
*v
= sdssplitlen(o
->ptr
,sdslen(o
->ptr
)," ",1,&vlen
);
607 /* We need a multiple of 4: <class> <hard> <soft> <soft_seconds> */
609 sdsfreesplitres(v
,vlen
);
613 /* Sanity check of single arguments, so that we either refuse the
614 * whole configuration string or accept it all, even if a single
615 * error in a single client class is present. */
616 for (j
= 0; j
< vlen
; j
++) {
621 if (getClientLimitClassByName(v
[j
]) == -1) {
622 sdsfreesplitres(v
,vlen
);
626 val
= strtoll(v
[j
], &eptr
, 10);
627 if (eptr
[0] != '\0' || val
< 0) {
628 sdsfreesplitres(v
,vlen
);
633 /* Finally set the new config */
634 for (j
= 0; j
< vlen
; j
+= 4) {
636 unsigned long long hard
, soft
;
639 class = getClientLimitClassByName(v
[j
]);
640 hard
= strtoll(v
[j
+1],NULL
,10);
641 soft
= strtoll(v
[j
+2],NULL
,10);
642 soft_seconds
= strtoll(v
[j
+3],NULL
,10);
644 server
.client_obuf_limits
[class].hard_limit_bytes
= hard
;
645 server
.client_obuf_limits
[class].soft_limit_bytes
= soft
;
646 server
.client_obuf_limits
[class].soft_limit_seconds
= soft_seconds
;
648 sdsfreesplitres(v
,vlen
);
649 } else if (!strcasecmp(c
->argv
[2]->ptr
,"stop-writes-on-bgsave-error")) {
650 int yn
= yesnotoi(o
->ptr
);
652 if (yn
== -1) goto badfmt
;
653 server
.stop_writes_on_bgsave_err
= yn
;
654 } else if (!strcasecmp(c
->argv
[2]->ptr
,"repl-ping-slave-period")) {
655 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
|| ll
<= 0) goto badfmt
;
656 server
.repl_ping_slave_period
= ll
;
657 } else if (!strcasecmp(c
->argv
[2]->ptr
,"repl-timeout")) {
658 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
|| ll
<= 0) goto badfmt
;
659 server
.repl_timeout
= ll
;
660 } else if (!strcasecmp(c
->argv
[2]->ptr
,"watchdog-period")) {
661 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
|| ll
< 0) goto badfmt
;
666 } else if (!strcasecmp(c
->argv
[2]->ptr
,"rdbcompression")) {
667 int yn
= yesnotoi(o
->ptr
);
669 if (yn
== -1) goto badfmt
;
670 server
.rdb_compression
= yn
;
671 } else if (!strcasecmp(c
->argv
[2]->ptr
,"rdbchecksum")) {
672 int yn
= yesnotoi(o
->ptr
);
674 if (yn
== -1) goto badfmt
;
675 server
.rdb_checksum
= yn
;
676 } else if (!strcasecmp(c
->argv
[2]->ptr
,"slave-priority")) {
677 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
||
678 ll
<= 0) goto badfmt
;
679 server
.slave_priority
= ll
;
681 addReplyErrorFormat(c
,"Unsupported CONFIG parameter: %s",
682 (char*)c
->argv
[2]->ptr
);
685 addReply(c
,shared
.ok
);
688 badfmt
: /* Bad format errors */
689 addReplyErrorFormat(c
,"Invalid argument '%s' for CONFIG SET '%s'",
691 (char*)c
->argv
[2]->ptr
);
694 #define config_get_string_field(_name,_var) do { \
695 if (stringmatch(pattern,_name,0)) { \
696 addReplyBulkCString(c,_name); \
697 addReplyBulkCString(c,_var ? _var : ""); \
702 #define config_get_bool_field(_name,_var) do { \
703 if (stringmatch(pattern,_name,0)) { \
704 addReplyBulkCString(c,_name); \
705 addReplyBulkCString(c,_var ? "yes" : "no"); \
710 #define config_get_numerical_field(_name,_var) do { \
711 if (stringmatch(pattern,_name,0)) { \
712 ll2string(buf,sizeof(buf),_var); \
713 addReplyBulkCString(c,_name); \
714 addReplyBulkCString(c,buf); \
719 void configGetCommand(redisClient
*c
) {
720 robj
*o
= c
->argv
[2];
721 void *replylen
= addDeferredMultiBulkLength(c
);
722 char *pattern
= o
->ptr
;
725 redisAssertWithInfo(c
,o
,o
->encoding
== REDIS_ENCODING_RAW
);
728 config_get_string_field("dbfilename",server
.rdb_filename
);
729 config_get_string_field("requirepass",server
.requirepass
);
730 config_get_string_field("masterauth",server
.requirepass
);
731 config_get_string_field("bind",server
.bindaddr
);
732 config_get_string_field("unixsocket",server
.unixsocket
);
733 config_get_string_field("logfile",server
.logfile
);
734 config_get_string_field("pidfile",server
.pidfile
);
736 /* Numerical values */
737 config_get_numerical_field("maxmemory",server
.maxmemory
);
738 config_get_numerical_field("maxmemory-samples",server
.maxmemory_samples
);
739 config_get_numerical_field("timeout",server
.maxidletime
);
740 config_get_numerical_field("auto-aof-rewrite-percentage",
741 server
.aof_rewrite_perc
);
742 config_get_numerical_field("auto-aof-rewrite-min-size",
743 server
.aof_rewrite_min_size
);
744 config_get_numerical_field("hash-max-ziplist-entries",
745 server
.hash_max_ziplist_entries
);
746 config_get_numerical_field("hash-max-ziplist-value",
747 server
.hash_max_ziplist_value
);
748 config_get_numerical_field("list-max-ziplist-entries",
749 server
.list_max_ziplist_entries
);
750 config_get_numerical_field("list-max-ziplist-value",
751 server
.list_max_ziplist_value
);
752 config_get_numerical_field("set-max-intset-entries",
753 server
.set_max_intset_entries
);
754 config_get_numerical_field("zset-max-ziplist-entries",
755 server
.zset_max_ziplist_entries
);
756 config_get_numerical_field("zset-max-ziplist-value",
757 server
.zset_max_ziplist_value
);
758 config_get_numerical_field("lua-time-limit",server
.lua_time_limit
);
759 config_get_numerical_field("slowlog-log-slower-than",
760 server
.slowlog_log_slower_than
);
761 config_get_numerical_field("slowlog-max-len",
762 server
.slowlog_max_len
);
763 config_get_numerical_field("port",server
.port
);
764 config_get_numerical_field("databases",server
.dbnum
);
765 config_get_numerical_field("repl-ping-slave-period",server
.repl_ping_slave_period
);
766 config_get_numerical_field("repl-timeout",server
.repl_timeout
);
767 config_get_numerical_field("maxclients",server
.maxclients
);
768 config_get_numerical_field("watchdog-period",server
.watchdog_period
);
769 config_get_numerical_field("slave-priority",server
.slave_priority
);
771 /* Bool (yes/no) values */
772 config_get_bool_field("no-appendfsync-on-rewrite",
773 server
.aof_no_fsync_on_rewrite
);
774 config_get_bool_field("slave-serve-stale-data",
775 server
.repl_serve_stale_data
);
776 config_get_bool_field("slave-read-only",
777 server
.repl_slave_ro
);
778 config_get_bool_field("stop-writes-on-bgsave-error",
779 server
.stop_writes_on_bgsave_err
);
780 config_get_bool_field("daemonize", server
.daemonize
);
781 config_get_bool_field("rdbcompression", server
.rdb_compression
);
782 config_get_bool_field("rdbchecksum", server
.rdb_checksum
);
783 config_get_bool_field("activerehashing", server
.activerehashing
);
785 /* Everything we can't handle with macros follows. */
787 if (stringmatch(pattern
,"appendonly",0)) {
788 addReplyBulkCString(c
,"appendonly");
789 addReplyBulkCString(c
,server
.aof_state
== REDIS_AOF_OFF
? "no" : "yes");
792 if (stringmatch(pattern
,"dir",0)) {
795 if (getcwd(buf
,sizeof(buf
)) == NULL
)
798 addReplyBulkCString(c
,"dir");
799 addReplyBulkCString(c
,buf
);
802 if (stringmatch(pattern
,"maxmemory-policy",0)) {
805 switch(server
.maxmemory_policy
) {
806 case REDIS_MAXMEMORY_VOLATILE_LRU
: s
= "volatile-lru"; break;
807 case REDIS_MAXMEMORY_VOLATILE_TTL
: s
= "volatile-ttl"; break;
808 case REDIS_MAXMEMORY_VOLATILE_RANDOM
: s
= "volatile-random"; break;
809 case REDIS_MAXMEMORY_ALLKEYS_LRU
: s
= "allkeys-lru"; break;
810 case REDIS_MAXMEMORY_ALLKEYS_RANDOM
: s
= "allkeys-random"; break;
811 case REDIS_MAXMEMORY_NO_EVICTION
: s
= "noeviction"; break;
812 default: s
= "unknown"; break; /* too harmless to panic */
814 addReplyBulkCString(c
,"maxmemory-policy");
815 addReplyBulkCString(c
,s
);
818 if (stringmatch(pattern
,"appendfsync",0)) {
821 switch(server
.aof_fsync
) {
822 case AOF_FSYNC_NO
: policy
= "no"; break;
823 case AOF_FSYNC_EVERYSEC
: policy
= "everysec"; break;
824 case AOF_FSYNC_ALWAYS
: policy
= "always"; break;
825 default: policy
= "unknown"; break; /* too harmless to panic */
827 addReplyBulkCString(c
,"appendfsync");
828 addReplyBulkCString(c
,policy
);
831 if (stringmatch(pattern
,"save",0)) {
832 sds buf
= sdsempty();
835 for (j
= 0; j
< server
.saveparamslen
; j
++) {
836 buf
= sdscatprintf(buf
,"%ld %d",
837 server
.saveparams
[j
].seconds
,
838 server
.saveparams
[j
].changes
);
839 if (j
!= server
.saveparamslen
-1)
840 buf
= sdscatlen(buf
," ",1);
842 addReplyBulkCString(c
,"save");
843 addReplyBulkCString(c
,buf
);
847 if (stringmatch(pattern
,"loglevel",0)) {
850 switch(server
.verbosity
) {
851 case REDIS_WARNING
: s
= "warning"; break;
852 case REDIS_VERBOSE
: s
= "verbose"; break;
853 case REDIS_NOTICE
: s
= "notice"; break;
854 case REDIS_DEBUG
: s
= "debug"; break;
855 default: s
= "unknown"; break; /* too harmless to panic */
857 addReplyBulkCString(c
,"loglevel");
858 addReplyBulkCString(c
,s
);
861 if (stringmatch(pattern
,"client-output-buffer-limit",0)) {
862 sds buf
= sdsempty();
865 for (j
= 0; j
< REDIS_CLIENT_LIMIT_NUM_CLASSES
; j
++) {
866 buf
= sdscatprintf(buf
,"%s %llu %llu %ld",
867 getClientLimitClassName(j
),
868 server
.client_obuf_limits
[j
].hard_limit_bytes
,
869 server
.client_obuf_limits
[j
].soft_limit_bytes
,
870 (long) server
.client_obuf_limits
[j
].soft_limit_seconds
);
871 if (j
!= REDIS_CLIENT_LIMIT_NUM_CLASSES
-1)
872 buf
= sdscatlen(buf
," ",1);
874 addReplyBulkCString(c
,"client-output-buffer-limit");
875 addReplyBulkCString(c
,buf
);
879 if (stringmatch(pattern
,"unixsocketperm",0)) {
881 snprintf(buf
,sizeof(buf
),"%o",server
.unixsocketperm
);
882 addReplyBulkCString(c
,"unixsocketperm");
883 addReplyBulkCString(c
,buf
);
886 if (stringmatch(pattern
,"slaveof",0)) {
889 addReplyBulkCString(c
,"slaveof");
890 if (server
.masterhost
)
891 snprintf(buf
,sizeof(buf
),"%s %d",
892 server
.masterhost
, server
.masterport
);
895 addReplyBulkCString(c
,buf
);
898 setDeferredMultiBulkLength(c
,replylen
,matches
*2);
901 void configCommand(redisClient
*c
) {
902 if (!strcasecmp(c
->argv
[1]->ptr
,"set")) {
903 if (c
->argc
!= 4) goto badarity
;
905 } else if (!strcasecmp(c
->argv
[1]->ptr
,"get")) {
906 if (c
->argc
!= 3) goto badarity
;
908 } else if (!strcasecmp(c
->argv
[1]->ptr
,"resetstat")) {
909 if (c
->argc
!= 2) goto badarity
;
910 server
.stat_keyspace_hits
= 0;
911 server
.stat_keyspace_misses
= 0;
912 server
.stat_numcommands
= 0;
913 server
.stat_numconnections
= 0;
914 server
.stat_expiredkeys
= 0;
915 server
.stat_rejected_conn
= 0;
916 server
.stat_fork_time
= 0;
917 server
.aof_delayed_fsync
= 0;
918 resetCommandTableStats();
919 addReply(c
,shared
.ok
);
922 "CONFIG subcommand must be one of GET, SET, RESETSTAT");
927 addReplyErrorFormat(c
,"Wrong number of arguments for CONFIG %s",
928 (char*) c
->argv
[1]->ptr
);