1 /* Configuration file parsing and CONFIG GET/SET commands implementation.
3 * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * * Neither the name of Redis nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without
16 * specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
34 /*-----------------------------------------------------------------------------
36 *----------------------------------------------------------------------------*/
38 int yesnotoi(char *s
) {
39 if (!strcasecmp(s
,"yes")) return 1;
40 else if (!strcasecmp(s
,"no")) return 0;
44 void appendServerSaveParams(time_t seconds
, int changes
) {
45 server
.saveparams
= zrealloc(server
.saveparams
,sizeof(struct saveparam
)*(server
.saveparamslen
+1));
46 server
.saveparams
[server
.saveparamslen
].seconds
= seconds
;
47 server
.saveparams
[server
.saveparamslen
].changes
= changes
;
48 server
.saveparamslen
++;
51 void resetServerSaveParams() {
52 zfree(server
.saveparams
);
53 server
.saveparams
= NULL
;
54 server
.saveparamslen
= 0;
57 void loadServerConfigFromString(char *config
) {
59 int linenum
= 0, totlines
, i
;
62 lines
= sdssplitlen(config
,strlen(config
),"\n",1,&totlines
);
64 for (i
= 0; i
< totlines
; i
++) {
69 lines
[i
] = sdstrim(lines
[i
]," \t\r\n");
71 /* Skip comments and blank lines*/
72 if (lines
[i
][0] == '#' || lines
[i
][0] == '\0') continue;
74 /* Split into arguments */
75 argv
= sdssplitargs(lines
[i
],&argc
);
78 /* Execute config directives */
79 if (!strcasecmp(argv
[0],"timeout") && argc
== 2) {
80 server
.maxidletime
= atoi(argv
[1]);
81 if (server
.maxidletime
< 0) {
82 err
= "Invalid timeout value"; goto loaderr
;
84 } else if (!strcasecmp(argv
[0],"port") && argc
== 2) {
85 server
.port
= atoi(argv
[1]);
86 if (server
.port
< 0 || server
.port
> 65535) {
87 err
= "Invalid port"; goto loaderr
;
89 } else if (!strcasecmp(argv
[0],"bind") && argc
== 2) {
90 server
.bindaddr
= zstrdup(argv
[1]);
91 } else if (!strcasecmp(argv
[0],"unixsocket") && argc
== 2) {
92 server
.unixsocket
= zstrdup(argv
[1]);
93 } else if (!strcasecmp(argv
[0],"unixsocketperm") && argc
== 2) {
95 server
.unixsocketperm
= (mode_t
)strtol(argv
[1], NULL
, 8);
96 if (errno
|| server
.unixsocketperm
> 0777) {
97 err
= "Invalid socket file permissions"; goto loaderr
;
99 } else if (!strcasecmp(argv
[0],"save")) {
101 int seconds
= atoi(argv
[1]);
102 int changes
= atoi(argv
[2]);
103 if (seconds
< 1 || changes
< 0) {
104 err
= "Invalid save parameters"; goto loaderr
;
106 appendServerSaveParams(seconds
,changes
);
107 } else if (argc
== 2 && !strcasecmp(argv
[1],"")) {
108 resetServerSaveParams();
110 } else if (!strcasecmp(argv
[0],"dir") && argc
== 2) {
111 if (chdir(argv
[1]) == -1) {
112 redisLog(REDIS_WARNING
,"Can't chdir to '%s': %s",
113 argv
[1], strerror(errno
));
116 } else if (!strcasecmp(argv
[0],"loglevel") && argc
== 2) {
117 if (!strcasecmp(argv
[1],"debug")) server
.verbosity
= REDIS_DEBUG
;
118 else if (!strcasecmp(argv
[1],"verbose")) server
.verbosity
= REDIS_VERBOSE
;
119 else if (!strcasecmp(argv
[1],"notice")) server
.verbosity
= REDIS_NOTICE
;
120 else if (!strcasecmp(argv
[1],"warning")) server
.verbosity
= REDIS_WARNING
;
122 err
= "Invalid log level. Must be one of debug, notice, warning";
125 } else if (!strcasecmp(argv
[0],"logfile") && argc
== 2) {
128 server
.logfile
= zstrdup(argv
[1]);
129 if (!strcasecmp(server
.logfile
,"stdout")) {
130 zfree(server
.logfile
);
131 server
.logfile
= NULL
;
133 if (server
.logfile
) {
134 /* Test if we are able to open the file. The server will not
135 * be able to abort just for this problem later... */
136 logfp
= fopen(server
.logfile
,"a");
138 err
= sdscatprintf(sdsempty(),
139 "Can't open the log file: %s", strerror(errno
));
144 } else if (!strcasecmp(argv
[0],"syslog-enabled") && argc
== 2) {
145 if ((server
.syslog_enabled
= yesnotoi(argv
[1])) == -1) {
146 err
= "argument must be 'yes' or 'no'"; goto loaderr
;
148 } else if (!strcasecmp(argv
[0],"syslog-ident") && argc
== 2) {
149 if (server
.syslog_ident
) zfree(server
.syslog_ident
);
150 server
.syslog_ident
= zstrdup(argv
[1]);
151 } else if (!strcasecmp(argv
[0],"syslog-facility") && argc
== 2) {
155 } validSyslogFacilities
[] = {
157 {"local0", LOG_LOCAL0
},
158 {"local1", LOG_LOCAL1
},
159 {"local2", LOG_LOCAL2
},
160 {"local3", LOG_LOCAL3
},
161 {"local4", LOG_LOCAL4
},
162 {"local5", LOG_LOCAL5
},
163 {"local6", LOG_LOCAL6
},
164 {"local7", LOG_LOCAL7
},
169 for (i
= 0; validSyslogFacilities
[i
].name
; i
++) {
170 if (!strcasecmp(validSyslogFacilities
[i
].name
, argv
[1])) {
171 server
.syslog_facility
= validSyslogFacilities
[i
].value
;
176 if (!validSyslogFacilities
[i
].name
) {
177 err
= "Invalid log facility. Must be one of USER or between LOCAL0-LOCAL7";
180 } else if (!strcasecmp(argv
[0],"databases") && argc
== 2) {
181 server
.dbnum
= atoi(argv
[1]);
182 if (server
.dbnum
< 1) {
183 err
= "Invalid number of databases"; goto loaderr
;
185 } else if (!strcasecmp(argv
[0],"include") && argc
== 2) {
186 loadServerConfig(argv
[1],NULL
);
187 } else if (!strcasecmp(argv
[0],"maxclients") && argc
== 2) {
188 server
.maxclients
= atoi(argv
[1]);
189 if (server
.maxclients
< 1) {
190 err
= "Invalid max clients limit"; goto loaderr
;
192 } else if (!strcasecmp(argv
[0],"maxmemory") && argc
== 2) {
193 server
.maxmemory
= memtoll(argv
[1],NULL
);
194 } else if (!strcasecmp(argv
[0],"maxmemory-policy") && argc
== 2) {
195 if (!strcasecmp(argv
[1],"volatile-lru")) {
196 server
.maxmemory_policy
= REDIS_MAXMEMORY_VOLATILE_LRU
;
197 } else if (!strcasecmp(argv
[1],"volatile-random")) {
198 server
.maxmemory_policy
= REDIS_MAXMEMORY_VOLATILE_RANDOM
;
199 } else if (!strcasecmp(argv
[1],"volatile-ttl")) {
200 server
.maxmemory_policy
= REDIS_MAXMEMORY_VOLATILE_TTL
;
201 } else if (!strcasecmp(argv
[1],"allkeys-lru")) {
202 server
.maxmemory_policy
= REDIS_MAXMEMORY_ALLKEYS_LRU
;
203 } else if (!strcasecmp(argv
[1],"allkeys-random")) {
204 server
.maxmemory_policy
= REDIS_MAXMEMORY_ALLKEYS_RANDOM
;
205 } else if (!strcasecmp(argv
[1],"noeviction")) {
206 server
.maxmemory_policy
= REDIS_MAXMEMORY_NO_EVICTION
;
208 err
= "Invalid maxmemory policy";
211 } else if (!strcasecmp(argv
[0],"maxmemory-samples") && argc
== 2) {
212 server
.maxmemory_samples
= atoi(argv
[1]);
213 if (server
.maxmemory_samples
<= 0) {
214 err
= "maxmemory-samples must be 1 or greater";
217 } else if (!strcasecmp(argv
[0],"slaveof") && argc
== 3) {
218 server
.masterhost
= sdsnew(argv
[1]);
219 server
.masterport
= atoi(argv
[2]);
220 server
.repl_state
= REDIS_REPL_CONNECT
;
221 } else if (!strcasecmp(argv
[0],"repl-ping-slave-period") && argc
== 2) {
222 server
.repl_ping_slave_period
= atoi(argv
[1]);
223 if (server
.repl_ping_slave_period
<= 0) {
224 err
= "repl-ping-slave-period must be 1 or greater";
227 } else if (!strcasecmp(argv
[0],"repl-timeout") && argc
== 2) {
228 server
.repl_timeout
= atoi(argv
[1]);
229 if (server
.repl_timeout
<= 0) {
230 err
= "repl-timeout must be 1 or greater";
233 } else if (!strcasecmp(argv
[0],"masterauth") && argc
== 2) {
234 server
.masterauth
= zstrdup(argv
[1]);
235 } else if (!strcasecmp(argv
[0],"slave-serve-stale-data") && argc
== 2) {
236 if ((server
.repl_serve_stale_data
= yesnotoi(argv
[1])) == -1) {
237 err
= "argument must be 'yes' or 'no'"; goto loaderr
;
239 } else if (!strcasecmp(argv
[0],"slave-read-only") && argc
== 2) {
240 if ((server
.repl_slave_ro
= yesnotoi(argv
[1])) == -1) {
241 err
= "argument must be 'yes' or 'no'"; goto loaderr
;
243 } else if (!strcasecmp(argv
[0],"rdbcompression") && argc
== 2) {
244 if ((server
.rdb_compression
= yesnotoi(argv
[1])) == -1) {
245 err
= "argument must be 'yes' or 'no'"; goto loaderr
;
247 } else if (!strcasecmp(argv
[0],"rdbchecksum") && argc
== 2) {
248 if ((server
.rdb_checksum
= yesnotoi(argv
[1])) == -1) {
249 err
= "argument must be 'yes' or 'no'"; goto loaderr
;
251 } else if (!strcasecmp(argv
[0],"activerehashing") && argc
== 2) {
252 if ((server
.activerehashing
= yesnotoi(argv
[1])) == -1) {
253 err
= "argument must be 'yes' or 'no'"; goto loaderr
;
255 } else if (!strcasecmp(argv
[0],"daemonize") && argc
== 2) {
256 if ((server
.daemonize
= yesnotoi(argv
[1])) == -1) {
257 err
= "argument must be 'yes' or 'no'"; goto loaderr
;
259 } else if (!strcasecmp(argv
[0],"appendonly") && argc
== 2) {
262 if ((yes
= yesnotoi(argv
[1])) == -1) {
263 err
= "argument must be 'yes' or 'no'"; goto loaderr
;
265 server
.aof_state
= yes
? REDIS_AOF_ON
: REDIS_AOF_OFF
;
266 } else if (!strcasecmp(argv
[0],"appendfilename") && argc
== 2) {
267 zfree(server
.aof_filename
);
268 server
.aof_filename
= zstrdup(argv
[1]);
269 } else if (!strcasecmp(argv
[0],"no-appendfsync-on-rewrite")
271 if ((server
.aof_no_fsync_on_rewrite
= yesnotoi(argv
[1])) == -1) {
272 err
= "argument must be 'yes' or 'no'"; goto loaderr
;
274 } else if (!strcasecmp(argv
[0],"appendfsync") && argc
== 2) {
275 if (!strcasecmp(argv
[1],"no")) {
276 server
.aof_fsync
= AOF_FSYNC_NO
;
277 } else if (!strcasecmp(argv
[1],"always")) {
278 server
.aof_fsync
= AOF_FSYNC_ALWAYS
;
279 } else if (!strcasecmp(argv
[1],"everysec")) {
280 server
.aof_fsync
= AOF_FSYNC_EVERYSEC
;
282 err
= "argument must be 'no', 'always' or 'everysec'";
285 } else if (!strcasecmp(argv
[0],"auto-aof-rewrite-percentage") &&
288 server
.aof_rewrite_perc
= atoi(argv
[1]);
289 if (server
.aof_rewrite_perc
< 0) {
290 err
= "Invalid negative percentage for AOF auto rewrite";
293 } else if (!strcasecmp(argv
[0],"auto-aof-rewrite-min-size") &&
296 server
.aof_rewrite_min_size
= memtoll(argv
[1],NULL
);
297 } else if (!strcasecmp(argv
[0],"requirepass") && argc
== 2) {
298 if (strlen(argv
[1]) > REDIS_AUTHPASS_MAX_LEN
) {
299 err
= "Password is longer than REDIS_AUTHPASS_MAX_LEN";
302 server
.requirepass
= zstrdup(argv
[1]);
303 } else if (!strcasecmp(argv
[0],"pidfile") && argc
== 2) {
304 zfree(server
.pidfile
);
305 server
.pidfile
= zstrdup(argv
[1]);
306 } else if (!strcasecmp(argv
[0],"dbfilename") && argc
== 2) {
307 zfree(server
.rdb_filename
);
308 server
.rdb_filename
= zstrdup(argv
[1]);
309 } else if (!strcasecmp(argv
[0],"hash-max-ziplist-entries") && argc
== 2) {
310 server
.hash_max_ziplist_entries
= memtoll(argv
[1], NULL
);
311 } else if (!strcasecmp(argv
[0],"hash-max-ziplist-value") && argc
== 2) {
312 server
.hash_max_ziplist_value
= memtoll(argv
[1], NULL
);
313 } else if (!strcasecmp(argv
[0],"list-max-ziplist-entries") && argc
== 2){
314 server
.list_max_ziplist_entries
= memtoll(argv
[1], NULL
);
315 } else if (!strcasecmp(argv
[0],"list-max-ziplist-value") && argc
== 2) {
316 server
.list_max_ziplist_value
= memtoll(argv
[1], NULL
);
317 } else if (!strcasecmp(argv
[0],"set-max-intset-entries") && argc
== 2) {
318 server
.set_max_intset_entries
= memtoll(argv
[1], NULL
);
319 } else if (!strcasecmp(argv
[0],"zset-max-ziplist-entries") && argc
== 2) {
320 server
.zset_max_ziplist_entries
= memtoll(argv
[1], NULL
);
321 } else if (!strcasecmp(argv
[0],"zset-max-ziplist-value") && argc
== 2) {
322 server
.zset_max_ziplist_value
= memtoll(argv
[1], NULL
);
323 } else if (!strcasecmp(argv
[0],"rename-command") && argc
== 3) {
324 struct redisCommand
*cmd
= lookupCommand(argv
[1]);
328 err
= "No such command in rename-command";
332 /* If the target command name is the emtpy string we just
333 * remove it from the command table. */
334 retval
= dictDelete(server
.commands
, argv
[1]);
335 redisAssert(retval
== DICT_OK
);
337 /* Otherwise we re-add the command under a different name. */
338 if (sdslen(argv
[2]) != 0) {
339 sds copy
= sdsdup(argv
[2]);
341 retval
= dictAdd(server
.commands
, copy
, cmd
);
342 if (retval
!= DICT_OK
) {
344 err
= "Target command name already exists"; goto loaderr
;
347 } else if (!strcasecmp(argv
[0],"lua-time-limit") && argc
== 2) {
348 server
.lua_time_limit
= strtoll(argv
[1],NULL
,10);
349 } else if (!strcasecmp(argv
[0],"slowlog-log-slower-than") &&
352 server
.slowlog_log_slower_than
= strtoll(argv
[1],NULL
,10);
353 } else if (!strcasecmp(argv
[0],"slowlog-max-len") && argc
== 2) {
354 server
.slowlog_max_len
= strtoll(argv
[1],NULL
,10);
355 } else if (!strcasecmp(argv
[0],"client-output-buffer-limit") &&
358 int class = getClientLimitClassByName(argv
[1]);
359 unsigned long long hard
, soft
;
363 err
= "Unrecognized client limit class";
366 hard
= memtoll(argv
[2],NULL
);
367 soft
= memtoll(argv
[3],NULL
);
368 soft_seconds
= atoi(argv
[4]);
369 if (soft_seconds
< 0) {
370 err
= "Negative number of seconds in soft limt is invalid";
373 server
.client_obuf_limits
[class].hard_limit_bytes
= hard
;
374 server
.client_obuf_limits
[class].soft_limit_bytes
= soft
;
375 server
.client_obuf_limits
[class].soft_limit_seconds
= soft_seconds
;
376 } else if (!strcasecmp(argv
[0],"stop-writes-on-bgsave-error") &&
378 if ((server
.stop_writes_on_bgsave_err
= yesnotoi(argv
[1])) == -1) {
379 err
= "argument must be 'yes' or 'no'"; goto loaderr
;
381 } else if (!strcasecmp(argv
[0],"slave-priority") && argc
== 2) {
382 server
.slave_priority
= atoi(argv
[1]);
383 } else if (!strcasecmp(argv
[0],"sentinel")) {
384 /* argc == 1 is handled by main() as we need to enter the sentinel
387 if (!server
.sentinel_mode
) {
388 err
= "sentinel directive while not in sentinel mode";
391 err
= sentinelHandleConfiguration(argv
+1,argc
-1);
392 if (err
) goto loaderr
;
395 err
= "Bad directive or wrong number of arguments"; goto loaderr
;
397 sdsfreesplitres(argv
,argc
);
399 sdsfreesplitres(lines
,totlines
);
403 fprintf(stderr
, "\n*** FATAL CONFIG FILE ERROR ***\n");
404 fprintf(stderr
, "Reading the configuration file, at line %d\n", linenum
);
405 fprintf(stderr
, ">>> '%s'\n", lines
[i
]);
406 fprintf(stderr
, "%s\n", err
);
410 /* Load the server configuration from the specified filename.
411 * The function appends the additional configuration directives stored
412 * in the 'options' string to the config file before loading.
414 * Both filename and options can be NULL, in such a case are considered
415 * emtpy. This way loadServerConfig can be used to just load a file or
416 * just load a string. */
417 void loadServerConfig(char *filename
, char *options
) {
418 sds config
= sdsempty();
419 char buf
[REDIS_CONFIGLINE_MAX
+1];
421 /* Load the file content */
425 if (filename
[0] == '-' && filename
[1] == '\0') {
428 if ((fp
= fopen(filename
,"r")) == NULL
) {
429 redisLog(REDIS_WARNING
,
430 "Fatal error, can't open config file '%s'", filename
);
434 while(fgets(buf
,REDIS_CONFIGLINE_MAX
+1,fp
) != NULL
)
435 config
= sdscat(config
,buf
);
436 if (fp
!= stdin
) fclose(fp
);
438 /* Append the additional options */
440 config
= sdscat(config
,"\n");
441 config
= sdscat(config
,options
);
443 loadServerConfigFromString(config
);
447 /*-----------------------------------------------------------------------------
448 * CONFIG command for remote configuration
449 *----------------------------------------------------------------------------*/
451 void configSetCommand(redisClient
*c
) {
454 redisAssertWithInfo(c
,c
->argv
[2],c
->argv
[2]->encoding
== REDIS_ENCODING_RAW
);
455 redisAssertWithInfo(c
,c
->argv
[2],c
->argv
[3]->encoding
== REDIS_ENCODING_RAW
);
458 if (!strcasecmp(c
->argv
[2]->ptr
,"dbfilename")) {
459 zfree(server
.rdb_filename
);
460 server
.rdb_filename
= zstrdup(o
->ptr
);
461 } else if (!strcasecmp(c
->argv
[2]->ptr
,"requirepass")) {
462 if (sdslen(o
->ptr
) > REDIS_AUTHPASS_MAX_LEN
) goto badfmt
;
463 zfree(server
.requirepass
);
464 server
.requirepass
= ((char*)o
->ptr
)[0] ? zstrdup(o
->ptr
) : NULL
;
465 } else if (!strcasecmp(c
->argv
[2]->ptr
,"masterauth")) {
466 zfree(server
.masterauth
);
467 server
.masterauth
= zstrdup(o
->ptr
);
468 } else if (!strcasecmp(c
->argv
[2]->ptr
,"maxmemory")) {
469 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
||
471 server
.maxmemory
= ll
;
472 if (server
.maxmemory
) {
473 if (server
.maxmemory
< zmalloc_used_memory()) {
474 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.");
476 freeMemoryIfNeeded();
478 } else if (!strcasecmp(c
->argv
[2]->ptr
,"maxmemory-policy")) {
479 if (!strcasecmp(o
->ptr
,"volatile-lru")) {
480 server
.maxmemory_policy
= REDIS_MAXMEMORY_VOLATILE_LRU
;
481 } else if (!strcasecmp(o
->ptr
,"volatile-random")) {
482 server
.maxmemory_policy
= REDIS_MAXMEMORY_VOLATILE_RANDOM
;
483 } else if (!strcasecmp(o
->ptr
,"volatile-ttl")) {
484 server
.maxmemory_policy
= REDIS_MAXMEMORY_VOLATILE_TTL
;
485 } else if (!strcasecmp(o
->ptr
,"allkeys-lru")) {
486 server
.maxmemory_policy
= REDIS_MAXMEMORY_ALLKEYS_LRU
;
487 } else if (!strcasecmp(o
->ptr
,"allkeys-random")) {
488 server
.maxmemory_policy
= REDIS_MAXMEMORY_ALLKEYS_RANDOM
;
489 } else if (!strcasecmp(o
->ptr
,"noeviction")) {
490 server
.maxmemory_policy
= REDIS_MAXMEMORY_NO_EVICTION
;
494 } else if (!strcasecmp(c
->argv
[2]->ptr
,"maxmemory-samples")) {
495 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
||
496 ll
<= 0) goto badfmt
;
497 server
.maxmemory_samples
= ll
;
498 } else if (!strcasecmp(c
->argv
[2]->ptr
,"timeout")) {
499 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
||
500 ll
< 0 || ll
> LONG_MAX
) goto badfmt
;
501 server
.maxidletime
= ll
;
502 } else if (!strcasecmp(c
->argv
[2]->ptr
,"appendfsync")) {
503 if (!strcasecmp(o
->ptr
,"no")) {
504 server
.aof_fsync
= AOF_FSYNC_NO
;
505 } else if (!strcasecmp(o
->ptr
,"everysec")) {
506 server
.aof_fsync
= AOF_FSYNC_EVERYSEC
;
507 } else if (!strcasecmp(o
->ptr
,"always")) {
508 server
.aof_fsync
= AOF_FSYNC_ALWAYS
;
512 } else if (!strcasecmp(c
->argv
[2]->ptr
,"no-appendfsync-on-rewrite")) {
513 int yn
= yesnotoi(o
->ptr
);
515 if (yn
== -1) goto badfmt
;
516 server
.aof_no_fsync_on_rewrite
= yn
;
517 } else if (!strcasecmp(c
->argv
[2]->ptr
,"appendonly")) {
518 int enable
= yesnotoi(o
->ptr
);
520 if (enable
== -1) goto badfmt
;
521 if (enable
== 0 && server
.aof_state
!= REDIS_AOF_OFF
) {
523 } else if (enable
&& server
.aof_state
== REDIS_AOF_OFF
) {
524 if (startAppendOnly() == REDIS_ERR
) {
526 "Unable to turn on AOF. Check server logs.");
530 } else if (!strcasecmp(c
->argv
[2]->ptr
,"auto-aof-rewrite-percentage")) {
531 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
|| ll
< 0) goto badfmt
;
532 server
.aof_rewrite_perc
= ll
;
533 } else if (!strcasecmp(c
->argv
[2]->ptr
,"auto-aof-rewrite-min-size")) {
534 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
|| ll
< 0) goto badfmt
;
535 server
.aof_rewrite_min_size
= ll
;
536 } else if (!strcasecmp(c
->argv
[2]->ptr
,"save")) {
538 sds
*v
= sdssplitlen(o
->ptr
,sdslen(o
->ptr
)," ",1,&vlen
);
540 /* Perform sanity check before setting the new config:
541 * - Even number of args
542 * - Seconds >= 1, changes >= 0 */
544 sdsfreesplitres(v
,vlen
);
547 for (j
= 0; j
< vlen
; j
++) {
551 val
= strtoll(v
[j
], &eptr
, 10);
552 if (eptr
[0] != '\0' ||
553 ((j
& 1) == 0 && val
< 1) ||
554 ((j
& 1) == 1 && val
< 0)) {
555 sdsfreesplitres(v
,vlen
);
559 /* Finally set the new config */
560 resetServerSaveParams();
561 for (j
= 0; j
< vlen
; j
+= 2) {
565 seconds
= strtoll(v
[j
],NULL
,10);
566 changes
= strtoll(v
[j
+1],NULL
,10);
567 appendServerSaveParams(seconds
, changes
);
569 sdsfreesplitres(v
,vlen
);
570 } else if (!strcasecmp(c
->argv
[2]->ptr
,"slave-serve-stale-data")) {
571 int yn
= yesnotoi(o
->ptr
);
573 if (yn
== -1) goto badfmt
;
574 server
.repl_serve_stale_data
= yn
;
575 } else if (!strcasecmp(c
->argv
[2]->ptr
,"slave-read-only")) {
576 int yn
= yesnotoi(o
->ptr
);
578 if (yn
== -1) goto badfmt
;
579 server
.repl_slave_ro
= yn
;
580 } else if (!strcasecmp(c
->argv
[2]->ptr
,"dir")) {
581 if (chdir((char*)o
->ptr
) == -1) {
582 addReplyErrorFormat(c
,"Changing directory: %s", strerror(errno
));
585 } else if (!strcasecmp(c
->argv
[2]->ptr
,"hash-max-ziplist-entries")) {
586 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
|| ll
< 0) goto badfmt
;
587 server
.hash_max_ziplist_entries
= ll
;
588 } else if (!strcasecmp(c
->argv
[2]->ptr
,"hash-max-ziplist-value")) {
589 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
|| ll
< 0) goto badfmt
;
590 server
.hash_max_ziplist_value
= ll
;
591 } else if (!strcasecmp(c
->argv
[2]->ptr
,"list-max-ziplist-entries")) {
592 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
|| ll
< 0) goto badfmt
;
593 server
.list_max_ziplist_entries
= ll
;
594 } else if (!strcasecmp(c
->argv
[2]->ptr
,"list-max-ziplist-value")) {
595 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
|| ll
< 0) goto badfmt
;
596 server
.list_max_ziplist_value
= ll
;
597 } else if (!strcasecmp(c
->argv
[2]->ptr
,"set-max-intset-entries")) {
598 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
|| ll
< 0) goto badfmt
;
599 server
.set_max_intset_entries
= ll
;
600 } else if (!strcasecmp(c
->argv
[2]->ptr
,"zset-max-ziplist-entries")) {
601 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
|| ll
< 0) goto badfmt
;
602 server
.zset_max_ziplist_entries
= ll
;
603 } else if (!strcasecmp(c
->argv
[2]->ptr
,"zset-max-ziplist-value")) {
604 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
|| ll
< 0) goto badfmt
;
605 server
.zset_max_ziplist_value
= ll
;
606 } else if (!strcasecmp(c
->argv
[2]->ptr
,"lua-time-limit")) {
607 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
|| ll
< 0) goto badfmt
;
608 server
.lua_time_limit
= ll
;
609 } else if (!strcasecmp(c
->argv
[2]->ptr
,"slowlog-log-slower-than")) {
610 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
) goto badfmt
;
611 server
.slowlog_log_slower_than
= ll
;
612 } else if (!strcasecmp(c
->argv
[2]->ptr
,"slowlog-max-len")) {
613 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
|| ll
< 0) goto badfmt
;
614 server
.slowlog_max_len
= (unsigned)ll
;
615 } else if (!strcasecmp(c
->argv
[2]->ptr
,"loglevel")) {
616 if (!strcasecmp(o
->ptr
,"warning")) {
617 server
.verbosity
= REDIS_WARNING
;
618 } else if (!strcasecmp(o
->ptr
,"notice")) {
619 server
.verbosity
= REDIS_NOTICE
;
620 } else if (!strcasecmp(o
->ptr
,"verbose")) {
621 server
.verbosity
= REDIS_VERBOSE
;
622 } else if (!strcasecmp(o
->ptr
,"debug")) {
623 server
.verbosity
= REDIS_DEBUG
;
627 } else if (!strcasecmp(c
->argv
[2]->ptr
,"client-output-buffer-limit")) {
629 sds
*v
= sdssplitlen(o
->ptr
,sdslen(o
->ptr
)," ",1,&vlen
);
631 /* We need a multiple of 4: <class> <hard> <soft> <soft_seconds> */
633 sdsfreesplitres(v
,vlen
);
637 /* Sanity check of single arguments, so that we either refuse the
638 * whole configuration string or accept it all, even if a single
639 * error in a single client class is present. */
640 for (j
= 0; j
< vlen
; j
++) {
645 if (getClientLimitClassByName(v
[j
]) == -1) {
646 sdsfreesplitres(v
,vlen
);
650 val
= strtoll(v
[j
], &eptr
, 10);
651 if (eptr
[0] != '\0' || val
< 0) {
652 sdsfreesplitres(v
,vlen
);
657 /* Finally set the new config */
658 for (j
= 0; j
< vlen
; j
+= 4) {
660 unsigned long long hard
, soft
;
663 class = getClientLimitClassByName(v
[j
]);
664 hard
= strtoll(v
[j
+1],NULL
,10);
665 soft
= strtoll(v
[j
+2],NULL
,10);
666 soft_seconds
= strtoll(v
[j
+3],NULL
,10);
668 server
.client_obuf_limits
[class].hard_limit_bytes
= hard
;
669 server
.client_obuf_limits
[class].soft_limit_bytes
= soft
;
670 server
.client_obuf_limits
[class].soft_limit_seconds
= soft_seconds
;
672 sdsfreesplitres(v
,vlen
);
673 } else if (!strcasecmp(c
->argv
[2]->ptr
,"stop-writes-on-bgsave-error")) {
674 int yn
= yesnotoi(o
->ptr
);
676 if (yn
== -1) goto badfmt
;
677 server
.stop_writes_on_bgsave_err
= yn
;
678 } else if (!strcasecmp(c
->argv
[2]->ptr
,"repl-ping-slave-period")) {
679 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
|| ll
<= 0) goto badfmt
;
680 server
.repl_ping_slave_period
= ll
;
681 } else if (!strcasecmp(c
->argv
[2]->ptr
,"repl-timeout")) {
682 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
|| ll
<= 0) goto badfmt
;
683 server
.repl_timeout
= ll
;
684 } else if (!strcasecmp(c
->argv
[2]->ptr
,"watchdog-period")) {
685 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
|| ll
< 0) goto badfmt
;
690 } else if (!strcasecmp(c
->argv
[2]->ptr
,"rdbcompression")) {
691 int yn
= yesnotoi(o
->ptr
);
693 if (yn
== -1) goto badfmt
;
694 server
.rdb_compression
= yn
;
695 } else if (!strcasecmp(c
->argv
[2]->ptr
,"rdbchecksum")) {
696 int yn
= yesnotoi(o
->ptr
);
698 if (yn
== -1) goto badfmt
;
699 server
.rdb_checksum
= yn
;
700 } else if (!strcasecmp(c
->argv
[2]->ptr
,"slave-priority")) {
701 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
||
702 ll
<= 0) goto badfmt
;
703 server
.slave_priority
= ll
;
705 addReplyErrorFormat(c
,"Unsupported CONFIG parameter: %s",
706 (char*)c
->argv
[2]->ptr
);
709 addReply(c
,shared
.ok
);
712 badfmt
: /* Bad format errors */
713 addReplyErrorFormat(c
,"Invalid argument '%s' for CONFIG SET '%s'",
715 (char*)c
->argv
[2]->ptr
);
718 #define config_get_string_field(_name,_var) do { \
719 if (stringmatch(pattern,_name,0)) { \
720 addReplyBulkCString(c,_name); \
721 addReplyBulkCString(c,_var ? _var : ""); \
726 #define config_get_bool_field(_name,_var) do { \
727 if (stringmatch(pattern,_name,0)) { \
728 addReplyBulkCString(c,_name); \
729 addReplyBulkCString(c,_var ? "yes" : "no"); \
734 #define config_get_numerical_field(_name,_var) do { \
735 if (stringmatch(pattern,_name,0)) { \
736 ll2string(buf,sizeof(buf),_var); \
737 addReplyBulkCString(c,_name); \
738 addReplyBulkCString(c,buf); \
743 void configGetCommand(redisClient
*c
) {
744 robj
*o
= c
->argv
[2];
745 void *replylen
= addDeferredMultiBulkLength(c
);
746 char *pattern
= o
->ptr
;
749 redisAssertWithInfo(c
,o
,o
->encoding
== REDIS_ENCODING_RAW
);
752 config_get_string_field("dbfilename",server
.rdb_filename
);
753 config_get_string_field("requirepass",server
.requirepass
);
754 config_get_string_field("masterauth",server
.requirepass
);
755 config_get_string_field("bind",server
.bindaddr
);
756 config_get_string_field("unixsocket",server
.unixsocket
);
757 config_get_string_field("logfile",server
.logfile
);
758 config_get_string_field("pidfile",server
.pidfile
);
760 /* Numerical values */
761 config_get_numerical_field("maxmemory",server
.maxmemory
);
762 config_get_numerical_field("maxmemory-samples",server
.maxmemory_samples
);
763 config_get_numerical_field("timeout",server
.maxidletime
);
764 config_get_numerical_field("auto-aof-rewrite-percentage",
765 server
.aof_rewrite_perc
);
766 config_get_numerical_field("auto-aof-rewrite-min-size",
767 server
.aof_rewrite_min_size
);
768 config_get_numerical_field("hash-max-ziplist-entries",
769 server
.hash_max_ziplist_entries
);
770 config_get_numerical_field("hash-max-ziplist-value",
771 server
.hash_max_ziplist_value
);
772 config_get_numerical_field("list-max-ziplist-entries",
773 server
.list_max_ziplist_entries
);
774 config_get_numerical_field("list-max-ziplist-value",
775 server
.list_max_ziplist_value
);
776 config_get_numerical_field("set-max-intset-entries",
777 server
.set_max_intset_entries
);
778 config_get_numerical_field("zset-max-ziplist-entries",
779 server
.zset_max_ziplist_entries
);
780 config_get_numerical_field("zset-max-ziplist-value",
781 server
.zset_max_ziplist_value
);
782 config_get_numerical_field("lua-time-limit",server
.lua_time_limit
);
783 config_get_numerical_field("slowlog-log-slower-than",
784 server
.slowlog_log_slower_than
);
785 config_get_numerical_field("slowlog-max-len",
786 server
.slowlog_max_len
);
787 config_get_numerical_field("port",server
.port
);
788 config_get_numerical_field("databases",server
.dbnum
);
789 config_get_numerical_field("repl-ping-slave-period",server
.repl_ping_slave_period
);
790 config_get_numerical_field("repl-timeout",server
.repl_timeout
);
791 config_get_numerical_field("maxclients",server
.maxclients
);
792 config_get_numerical_field("watchdog-period",server
.watchdog_period
);
793 config_get_numerical_field("slave-priority",server
.slave_priority
);
795 /* Bool (yes/no) values */
796 config_get_bool_field("no-appendfsync-on-rewrite",
797 server
.aof_no_fsync_on_rewrite
);
798 config_get_bool_field("slave-serve-stale-data",
799 server
.repl_serve_stale_data
);
800 config_get_bool_field("slave-read-only",
801 server
.repl_slave_ro
);
802 config_get_bool_field("stop-writes-on-bgsave-error",
803 server
.stop_writes_on_bgsave_err
);
804 config_get_bool_field("daemonize", server
.daemonize
);
805 config_get_bool_field("rdbcompression", server
.rdb_compression
);
806 config_get_bool_field("rdbchecksum", server
.rdb_checksum
);
807 config_get_bool_field("activerehashing", server
.activerehashing
);
809 /* Everything we can't handle with macros follows. */
811 if (stringmatch(pattern
,"appendonly",0)) {
812 addReplyBulkCString(c
,"appendonly");
813 addReplyBulkCString(c
,server
.aof_state
== REDIS_AOF_OFF
? "no" : "yes");
816 if (stringmatch(pattern
,"dir",0)) {
819 if (getcwd(buf
,sizeof(buf
)) == NULL
)
822 addReplyBulkCString(c
,"dir");
823 addReplyBulkCString(c
,buf
);
826 if (stringmatch(pattern
,"maxmemory-policy",0)) {
829 switch(server
.maxmemory_policy
) {
830 case REDIS_MAXMEMORY_VOLATILE_LRU
: s
= "volatile-lru"; break;
831 case REDIS_MAXMEMORY_VOLATILE_TTL
: s
= "volatile-ttl"; break;
832 case REDIS_MAXMEMORY_VOLATILE_RANDOM
: s
= "volatile-random"; break;
833 case REDIS_MAXMEMORY_ALLKEYS_LRU
: s
= "allkeys-lru"; break;
834 case REDIS_MAXMEMORY_ALLKEYS_RANDOM
: s
= "allkeys-random"; break;
835 case REDIS_MAXMEMORY_NO_EVICTION
: s
= "noeviction"; break;
836 default: s
= "unknown"; break; /* too harmless to panic */
838 addReplyBulkCString(c
,"maxmemory-policy");
839 addReplyBulkCString(c
,s
);
842 if (stringmatch(pattern
,"appendfsync",0)) {
845 switch(server
.aof_fsync
) {
846 case AOF_FSYNC_NO
: policy
= "no"; break;
847 case AOF_FSYNC_EVERYSEC
: policy
= "everysec"; break;
848 case AOF_FSYNC_ALWAYS
: policy
= "always"; break;
849 default: policy
= "unknown"; break; /* too harmless to panic */
851 addReplyBulkCString(c
,"appendfsync");
852 addReplyBulkCString(c
,policy
);
855 if (stringmatch(pattern
,"save",0)) {
856 sds buf
= sdsempty();
859 for (j
= 0; j
< server
.saveparamslen
; j
++) {
860 buf
= sdscatprintf(buf
,"%ld %d",
861 server
.saveparams
[j
].seconds
,
862 server
.saveparams
[j
].changes
);
863 if (j
!= server
.saveparamslen
-1)
864 buf
= sdscatlen(buf
," ",1);
866 addReplyBulkCString(c
,"save");
867 addReplyBulkCString(c
,buf
);
871 if (stringmatch(pattern
,"loglevel",0)) {
874 switch(server
.verbosity
) {
875 case REDIS_WARNING
: s
= "warning"; break;
876 case REDIS_VERBOSE
: s
= "verbose"; break;
877 case REDIS_NOTICE
: s
= "notice"; break;
878 case REDIS_DEBUG
: s
= "debug"; break;
879 default: s
= "unknown"; break; /* too harmless to panic */
881 addReplyBulkCString(c
,"loglevel");
882 addReplyBulkCString(c
,s
);
885 if (stringmatch(pattern
,"client-output-buffer-limit",0)) {
886 sds buf
= sdsempty();
889 for (j
= 0; j
< REDIS_CLIENT_LIMIT_NUM_CLASSES
; j
++) {
890 buf
= sdscatprintf(buf
,"%s %llu %llu %ld",
891 getClientLimitClassName(j
),
892 server
.client_obuf_limits
[j
].hard_limit_bytes
,
893 server
.client_obuf_limits
[j
].soft_limit_bytes
,
894 (long) server
.client_obuf_limits
[j
].soft_limit_seconds
);
895 if (j
!= REDIS_CLIENT_LIMIT_NUM_CLASSES
-1)
896 buf
= sdscatlen(buf
," ",1);
898 addReplyBulkCString(c
,"client-output-buffer-limit");
899 addReplyBulkCString(c
,buf
);
903 if (stringmatch(pattern
,"unixsocketperm",0)) {
905 snprintf(buf
,sizeof(buf
),"%o",server
.unixsocketperm
);
906 addReplyBulkCString(c
,"unixsocketperm");
907 addReplyBulkCString(c
,buf
);
910 if (stringmatch(pattern
,"slaveof",0)) {
913 addReplyBulkCString(c
,"slaveof");
914 if (server
.masterhost
)
915 snprintf(buf
,sizeof(buf
),"%s %d",
916 server
.masterhost
, server
.masterport
);
919 addReplyBulkCString(c
,buf
);
922 setDeferredMultiBulkLength(c
,replylen
,matches
*2);
925 void configCommand(redisClient
*c
) {
926 if (!strcasecmp(c
->argv
[1]->ptr
,"set")) {
927 if (c
->argc
!= 4) goto badarity
;
929 } else if (!strcasecmp(c
->argv
[1]->ptr
,"get")) {
930 if (c
->argc
!= 3) goto badarity
;
932 } else if (!strcasecmp(c
->argv
[1]->ptr
,"resetstat")) {
933 if (c
->argc
!= 2) goto badarity
;
934 server
.stat_keyspace_hits
= 0;
935 server
.stat_keyspace_misses
= 0;
936 server
.stat_numcommands
= 0;
937 server
.stat_numconnections
= 0;
938 server
.stat_expiredkeys
= 0;
939 server
.stat_rejected_conn
= 0;
940 server
.stat_fork_time
= 0;
941 server
.aof_delayed_fsync
= 0;
942 resetCommandTableStats();
943 addReply(c
,shared
.ok
);
946 "CONFIG subcommand must be one of GET, SET, RESETSTAT");
951 addReplyErrorFormat(c
,"Wrong number of arguments for CONFIG %s",
952 (char*) c
->argv
[1]->ptr
);