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],"lua-time-limit") && argc
== 2) {
317 server
.lua_time_limit
= strtoll(argv
[1],NULL
,10);
318 } else if (!strcasecmp(argv
[0],"slowlog-log-slower-than") &&
321 server
.slowlog_log_slower_than
= strtoll(argv
[1],NULL
,10);
322 } else if (!strcasecmp(argv
[0],"slowlog-max-len") && argc
== 2) {
323 server
.slowlog_max_len
= strtoll(argv
[1],NULL
,10);
324 } else if (!strcasecmp(argv
[0],"client-output-buffer-limit") &&
327 int class = getClientLimitClassByName(argv
[1]);
328 unsigned long long hard
, soft
;
332 err
= "Unrecognized client limit class";
335 hard
= memtoll(argv
[2],NULL
);
336 soft
= memtoll(argv
[3],NULL
);
337 soft_seconds
= atoi(argv
[4]);
338 if (soft_seconds
< 0) {
339 err
= "Negative number of seconds in soft limt is invalid";
342 server
.client_obuf_limits
[class].hard_limit_bytes
= hard
;
343 server
.client_obuf_limits
[class].soft_limit_bytes
= soft
;
344 server
.client_obuf_limits
[class].soft_limit_seconds
= soft_seconds
;
345 } else if (!strcasecmp(argv
[0],"stop-writes-on-bgsave-error") &&
347 if ((server
.stop_writes_on_bgsave_err
= yesnotoi(argv
[1])) == -1) {
348 err
= "argument must be 'yes' or 'no'"; goto loaderr
;
351 err
= "Bad directive or wrong number of arguments"; goto loaderr
;
353 sdsfreesplitres(argv
,argc
);
355 sdsfreesplitres(lines
,totlines
);
359 fprintf(stderr
, "\n*** FATAL CONFIG FILE ERROR ***\n");
360 fprintf(stderr
, "Reading the configuration file, at line %d\n", linenum
);
361 fprintf(stderr
, ">>> '%s'\n", lines
[i
]);
362 fprintf(stderr
, "%s\n", err
);
366 /* Load the server configuration from the specified filename.
367 * The function appends the additional configuration directives stored
368 * in the 'options' string to the config file before loading.
370 * Both filename and options can be NULL, in such a case are considered
371 * emtpy. This way loadServerConfig can be used to just load a file or
372 * just load a string. */
373 void loadServerConfig(char *filename
, char *options
) {
374 sds config
= sdsempty();
375 char buf
[REDIS_CONFIGLINE_MAX
+1];
377 /* Load the file content */
381 if (filename
[0] == '-' && filename
[1] == '\0') {
384 if ((fp
= fopen(filename
,"r")) == NULL
) {
385 redisLog(REDIS_WARNING
,
386 "Fatal error, can't open config file '%s'", filename
);
390 while(fgets(buf
,REDIS_CONFIGLINE_MAX
+1,fp
) != NULL
)
391 config
= sdscat(config
,buf
);
392 if (fp
!= stdin
) fclose(fp
);
394 /* Append the additional options */
396 config
= sdscat(config
,"\n");
397 config
= sdscat(config
,options
);
399 loadServerConfigFromString(config
);
403 /*-----------------------------------------------------------------------------
404 * CONFIG command for remote configuration
405 *----------------------------------------------------------------------------*/
407 void configSetCommand(redisClient
*c
) {
410 redisAssertWithInfo(c
,c
->argv
[2],c
->argv
[2]->encoding
== REDIS_ENCODING_RAW
);
411 redisAssertWithInfo(c
,c
->argv
[2],c
->argv
[3]->encoding
== REDIS_ENCODING_RAW
);
414 if (!strcasecmp(c
->argv
[2]->ptr
,"dbfilename")) {
415 zfree(server
.rdb_filename
);
416 server
.rdb_filename
= zstrdup(o
->ptr
);
417 } else if (!strcasecmp(c
->argv
[2]->ptr
,"requirepass")) {
418 if (sdslen(o
->ptr
) > REDIS_AUTHPASS_MAX_LEN
) goto badfmt
;
419 zfree(server
.requirepass
);
420 server
.requirepass
= ((char*)o
->ptr
)[0] ? zstrdup(o
->ptr
) : NULL
;
421 } else if (!strcasecmp(c
->argv
[2]->ptr
,"masterauth")) {
422 zfree(server
.masterauth
);
423 server
.masterauth
= zstrdup(o
->ptr
);
424 } else if (!strcasecmp(c
->argv
[2]->ptr
,"maxmemory")) {
425 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
||
427 server
.maxmemory
= ll
;
428 if (server
.maxmemory
) freeMemoryIfNeeded();
429 } else if (!strcasecmp(c
->argv
[2]->ptr
,"maxmemory-policy")) {
430 if (!strcasecmp(o
->ptr
,"volatile-lru")) {
431 server
.maxmemory_policy
= REDIS_MAXMEMORY_VOLATILE_LRU
;
432 } else if (!strcasecmp(o
->ptr
,"volatile-random")) {
433 server
.maxmemory_policy
= REDIS_MAXMEMORY_VOLATILE_RANDOM
;
434 } else if (!strcasecmp(o
->ptr
,"volatile-ttl")) {
435 server
.maxmemory_policy
= REDIS_MAXMEMORY_VOLATILE_TTL
;
436 } else if (!strcasecmp(o
->ptr
,"allkeys-lru")) {
437 server
.maxmemory_policy
= REDIS_MAXMEMORY_ALLKEYS_LRU
;
438 } else if (!strcasecmp(o
->ptr
,"allkeys-random")) {
439 server
.maxmemory_policy
= REDIS_MAXMEMORY_ALLKEYS_RANDOM
;
440 } else if (!strcasecmp(o
->ptr
,"noeviction")) {
441 server
.maxmemory_policy
= REDIS_MAXMEMORY_NO_EVICTION
;
445 } else if (!strcasecmp(c
->argv
[2]->ptr
,"maxmemory-samples")) {
446 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
||
447 ll
<= 0) goto badfmt
;
448 server
.maxmemory_samples
= ll
;
449 } else if (!strcasecmp(c
->argv
[2]->ptr
,"timeout")) {
450 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
||
451 ll
< 0 || ll
> LONG_MAX
) goto badfmt
;
452 server
.maxidletime
= ll
;
453 } else if (!strcasecmp(c
->argv
[2]->ptr
,"appendfsync")) {
454 if (!strcasecmp(o
->ptr
,"no")) {
455 server
.aof_fsync
= AOF_FSYNC_NO
;
456 } else if (!strcasecmp(o
->ptr
,"everysec")) {
457 server
.aof_fsync
= AOF_FSYNC_EVERYSEC
;
458 } else if (!strcasecmp(o
->ptr
,"always")) {
459 server
.aof_fsync
= AOF_FSYNC_ALWAYS
;
463 } else if (!strcasecmp(c
->argv
[2]->ptr
,"no-appendfsync-on-rewrite")) {
464 int yn
= yesnotoi(o
->ptr
);
466 if (yn
== -1) goto badfmt
;
467 server
.aof_no_fsync_on_rewrite
= yn
;
468 } else if (!strcasecmp(c
->argv
[2]->ptr
,"appendonly")) {
469 int enable
= yesnotoi(o
->ptr
);
471 if (enable
== -1) goto badfmt
;
472 if (enable
== 0 && server
.aof_state
!= REDIS_AOF_OFF
) {
474 } else if (enable
&& server
.aof_state
== REDIS_AOF_OFF
) {
475 if (startAppendOnly() == REDIS_ERR
) {
477 "Unable to turn on AOF. Check server logs.");
481 } else if (!strcasecmp(c
->argv
[2]->ptr
,"auto-aof-rewrite-percentage")) {
482 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
|| ll
< 0) goto badfmt
;
483 server
.aof_rewrite_perc
= ll
;
484 } else if (!strcasecmp(c
->argv
[2]->ptr
,"auto-aof-rewrite-min-size")) {
485 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
|| ll
< 0) goto badfmt
;
486 server
.aof_rewrite_min_size
= ll
;
487 } else if (!strcasecmp(c
->argv
[2]->ptr
,"save")) {
489 sds
*v
= sdssplitlen(o
->ptr
,sdslen(o
->ptr
)," ",1,&vlen
);
491 /* Perform sanity check before setting the new config:
492 * - Even number of args
493 * - Seconds >= 1, changes >= 0 */
495 sdsfreesplitres(v
,vlen
);
498 for (j
= 0; j
< vlen
; j
++) {
502 val
= strtoll(v
[j
], &eptr
, 10);
503 if (eptr
[0] != '\0' ||
504 ((j
& 1) == 0 && val
< 1) ||
505 ((j
& 1) == 1 && val
< 0)) {
506 sdsfreesplitres(v
,vlen
);
510 /* Finally set the new config */
511 resetServerSaveParams();
512 for (j
= 0; j
< vlen
; j
+= 2) {
516 seconds
= strtoll(v
[j
],NULL
,10);
517 changes
= strtoll(v
[j
+1],NULL
,10);
518 appendServerSaveParams(seconds
, changes
);
520 sdsfreesplitres(v
,vlen
);
521 } else if (!strcasecmp(c
->argv
[2]->ptr
,"slave-serve-stale-data")) {
522 int yn
= yesnotoi(o
->ptr
);
524 if (yn
== -1) goto badfmt
;
525 server
.repl_serve_stale_data
= yn
;
526 } else if (!strcasecmp(c
->argv
[2]->ptr
,"slave-read-only")) {
527 int yn
= yesnotoi(o
->ptr
);
529 if (yn
== -1) goto badfmt
;
530 server
.repl_slave_ro
= yn
;
531 } else if (!strcasecmp(c
->argv
[2]->ptr
,"dir")) {
532 if (chdir((char*)o
->ptr
) == -1) {
533 addReplyErrorFormat(c
,"Changing directory: %s", strerror(errno
));
536 } else if (!strcasecmp(c
->argv
[2]->ptr
,"hash-max-ziplist-entries")) {
537 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
|| ll
< 0) goto badfmt
;
538 server
.hash_max_ziplist_entries
= ll
;
539 } else if (!strcasecmp(c
->argv
[2]->ptr
,"hash-max-ziplist-value")) {
540 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
|| ll
< 0) goto badfmt
;
541 server
.hash_max_ziplist_value
= ll
;
542 } else if (!strcasecmp(c
->argv
[2]->ptr
,"list-max-ziplist-entries")) {
543 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
|| ll
< 0) goto badfmt
;
544 server
.list_max_ziplist_entries
= ll
;
545 } else if (!strcasecmp(c
->argv
[2]->ptr
,"list-max-ziplist-value")) {
546 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
|| ll
< 0) goto badfmt
;
547 server
.list_max_ziplist_value
= ll
;
548 } else if (!strcasecmp(c
->argv
[2]->ptr
,"set-max-intset-entries")) {
549 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
|| ll
< 0) goto badfmt
;
550 server
.set_max_intset_entries
= ll
;
551 } else if (!strcasecmp(c
->argv
[2]->ptr
,"zset-max-ziplist-entries")) {
552 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
|| ll
< 0) goto badfmt
;
553 server
.zset_max_ziplist_entries
= ll
;
554 } else if (!strcasecmp(c
->argv
[2]->ptr
,"zset-max-ziplist-value")) {
555 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
|| ll
< 0) goto badfmt
;
556 server
.zset_max_ziplist_value
= ll
;
557 } else if (!strcasecmp(c
->argv
[2]->ptr
,"lua-time-limit")) {
558 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
|| ll
< 0) goto badfmt
;
559 server
.lua_time_limit
= ll
;
560 } else if (!strcasecmp(c
->argv
[2]->ptr
,"slowlog-log-slower-than")) {
561 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
) goto badfmt
;
562 server
.slowlog_log_slower_than
= ll
;
563 } else if (!strcasecmp(c
->argv
[2]->ptr
,"slowlog-max-len")) {
564 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
|| ll
< 0) goto badfmt
;
565 server
.slowlog_max_len
= (unsigned)ll
;
566 } else if (!strcasecmp(c
->argv
[2]->ptr
,"loglevel")) {
567 if (!strcasecmp(o
->ptr
,"warning")) {
568 server
.verbosity
= REDIS_WARNING
;
569 } else if (!strcasecmp(o
->ptr
,"notice")) {
570 server
.verbosity
= REDIS_NOTICE
;
571 } else if (!strcasecmp(o
->ptr
,"verbose")) {
572 server
.verbosity
= REDIS_VERBOSE
;
573 } else if (!strcasecmp(o
->ptr
,"debug")) {
574 server
.verbosity
= REDIS_DEBUG
;
578 } else if (!strcasecmp(c
->argv
[2]->ptr
,"client-output-buffer-limit")) {
580 sds
*v
= sdssplitlen(o
->ptr
,sdslen(o
->ptr
)," ",1,&vlen
);
582 /* We need a multiple of 4: <class> <hard> <soft> <soft_seconds> */
584 sdsfreesplitres(v
,vlen
);
588 /* Sanity check of single arguments, so that we either refuse the
589 * whole configuration string or accept it all, even if a single
590 * error in a single client class is present. */
591 for (j
= 0; j
< vlen
; j
++) {
596 if (getClientLimitClassByName(v
[j
]) == -1) {
597 sdsfreesplitres(v
,vlen
);
601 val
= strtoll(v
[j
], &eptr
, 10);
602 if (eptr
[0] != '\0' || val
< 0) {
603 sdsfreesplitres(v
,vlen
);
608 /* Finally set the new config */
609 for (j
= 0; j
< vlen
; j
+= 4) {
611 unsigned long long hard
, soft
;
614 class = getClientLimitClassByName(v
[j
]);
615 hard
= strtoll(v
[j
+1],NULL
,10);
616 soft
= strtoll(v
[j
+2],NULL
,10);
617 soft_seconds
= strtoll(v
[j
+3],NULL
,10);
619 server
.client_obuf_limits
[class].hard_limit_bytes
= hard
;
620 server
.client_obuf_limits
[class].soft_limit_bytes
= soft
;
621 server
.client_obuf_limits
[class].soft_limit_seconds
= soft_seconds
;
623 sdsfreesplitres(v
,vlen
);
624 } else if (!strcasecmp(c
->argv
[2]->ptr
,"stop-writes-on-bgsave-error")) {
625 int yn
= yesnotoi(o
->ptr
);
627 if (yn
== -1) goto badfmt
;
628 server
.stop_writes_on_bgsave_err
= yn
;
629 } else if (!strcasecmp(c
->argv
[2]->ptr
,"repl-ping-slave-period")) {
630 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
|| ll
<= 0) goto badfmt
;
631 server
.repl_ping_slave_period
= ll
;
632 } else if (!strcasecmp(c
->argv
[2]->ptr
,"repl-timeout")) {
633 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
|| ll
<= 0) goto badfmt
;
634 server
.repl_timeout
= ll
;
635 } else if (!strcasecmp(c
->argv
[2]->ptr
,"watchdog-period")) {
636 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
|| ll
< 0) goto badfmt
;
641 } else if (!strcasecmp(c
->argv
[2]->ptr
,"rdbcompression")) {
642 int yn
= yesnotoi(o
->ptr
);
644 if (yn
== -1) goto badfmt
;
645 server
.rdb_compression
= yn
;
646 } else if (!strcasecmp(c
->argv
[2]->ptr
,"rdbchecksum")) {
647 int yn
= yesnotoi(o
->ptr
);
649 if (yn
== -1) goto badfmt
;
650 server
.rdb_checksum
= yn
;
652 addReplyErrorFormat(c
,"Unsupported CONFIG parameter: %s",
653 (char*)c
->argv
[2]->ptr
);
656 addReply(c
,shared
.ok
);
659 badfmt
: /* Bad format errors */
660 addReplyErrorFormat(c
,"Invalid argument '%s' for CONFIG SET '%s'",
662 (char*)c
->argv
[2]->ptr
);
665 #define config_get_string_field(_name,_var) do { \
666 if (stringmatch(pattern,_name,0)) { \
667 addReplyBulkCString(c,_name); \
668 addReplyBulkCString(c,_var ? _var : ""); \
673 #define config_get_bool_field(_name,_var) do { \
674 if (stringmatch(pattern,_name,0)) { \
675 addReplyBulkCString(c,_name); \
676 addReplyBulkCString(c,_var ? "yes" : "no"); \
681 #define config_get_numerical_field(_name,_var) do { \
682 if (stringmatch(pattern,_name,0)) { \
683 ll2string(buf,sizeof(buf),_var); \
684 addReplyBulkCString(c,_name); \
685 addReplyBulkCString(c,buf); \
690 void configGetCommand(redisClient
*c
) {
691 robj
*o
= c
->argv
[2];
692 void *replylen
= addDeferredMultiBulkLength(c
);
693 char *pattern
= o
->ptr
;
696 redisAssertWithInfo(c
,o
,o
->encoding
== REDIS_ENCODING_RAW
);
699 config_get_string_field("dbfilename",server
.rdb_filename
);
700 config_get_string_field("requirepass",server
.requirepass
);
701 config_get_string_field("masterauth",server
.requirepass
);
702 config_get_string_field("bind",server
.bindaddr
);
703 config_get_string_field("unixsocket",server
.unixsocket
);
704 config_get_string_field("logfile",server
.logfile
);
705 config_get_string_field("pidfile",server
.pidfile
);
707 /* Numerical values */
708 config_get_numerical_field("maxmemory",server
.maxmemory
);
709 config_get_numerical_field("maxmemory-samples",server
.maxmemory_samples
);
710 config_get_numerical_field("timeout",server
.maxidletime
);
711 config_get_numerical_field("auto-aof-rewrite-percentage",
712 server
.aof_rewrite_perc
);
713 config_get_numerical_field("auto-aof-rewrite-min-size",
714 server
.aof_rewrite_min_size
);
715 config_get_numerical_field("hash-max-ziplist-entries",
716 server
.hash_max_ziplist_entries
);
717 config_get_numerical_field("hash-max-ziplist-value",
718 server
.hash_max_ziplist_value
);
719 config_get_numerical_field("list-max-ziplist-entries",
720 server
.list_max_ziplist_entries
);
721 config_get_numerical_field("list-max-ziplist-value",
722 server
.list_max_ziplist_value
);
723 config_get_numerical_field("set-max-intset-entries",
724 server
.set_max_intset_entries
);
725 config_get_numerical_field("zset-max-ziplist-entries",
726 server
.zset_max_ziplist_entries
);
727 config_get_numerical_field("zset-max-ziplist-value",
728 server
.zset_max_ziplist_value
);
729 config_get_numerical_field("lua-time-limit",server
.lua_time_limit
);
730 config_get_numerical_field("slowlog-log-slower-than",
731 server
.slowlog_log_slower_than
);
732 config_get_numerical_field("slowlog-max-len",
733 server
.slowlog_max_len
);
734 config_get_numerical_field("port",server
.port
);
735 config_get_numerical_field("databases",server
.dbnum
);
736 config_get_numerical_field("repl-ping-slave-period",server
.repl_ping_slave_period
);
737 config_get_numerical_field("repl-timeout",server
.repl_timeout
);
738 config_get_numerical_field("maxclients",server
.maxclients
);
739 config_get_numerical_field("watchdog-period",server
.watchdog_period
);
741 /* Bool (yes/no) values */
742 config_get_bool_field("no-appendfsync-on-rewrite",
743 server
.aof_no_fsync_on_rewrite
);
744 config_get_bool_field("slave-serve-stale-data",
745 server
.repl_serve_stale_data
);
746 config_get_bool_field("slave-read-only",
747 server
.repl_slave_ro
);
748 config_get_bool_field("stop-writes-on-bgsave-error",
749 server
.stop_writes_on_bgsave_err
);
750 config_get_bool_field("daemonize", server
.daemonize
);
751 config_get_bool_field("rdbcompression", server
.rdb_compression
);
752 config_get_bool_field("rdbchecksum", server
.rdb_checksum
);
753 config_get_bool_field("activerehashing", server
.activerehashing
);
755 /* Everything we can't handle with macros follows. */
757 if (stringmatch(pattern
,"appendonly",0)) {
758 addReplyBulkCString(c
,"appendonly");
759 addReplyBulkCString(c
,server
.aof_state
== REDIS_AOF_OFF
? "no" : "yes");
762 if (stringmatch(pattern
,"dir",0)) {
765 if (getcwd(buf
,sizeof(buf
)) == NULL
)
768 addReplyBulkCString(c
,"dir");
769 addReplyBulkCString(c
,buf
);
772 if (stringmatch(pattern
,"maxmemory-policy",0)) {
775 switch(server
.maxmemory_policy
) {
776 case REDIS_MAXMEMORY_VOLATILE_LRU
: s
= "volatile-lru"; break;
777 case REDIS_MAXMEMORY_VOLATILE_TTL
: s
= "volatile-ttl"; break;
778 case REDIS_MAXMEMORY_VOLATILE_RANDOM
: s
= "volatile-random"; break;
779 case REDIS_MAXMEMORY_ALLKEYS_LRU
: s
= "allkeys-lru"; break;
780 case REDIS_MAXMEMORY_ALLKEYS_RANDOM
: s
= "allkeys-random"; break;
781 case REDIS_MAXMEMORY_NO_EVICTION
: s
= "noeviction"; break;
782 default: s
= "unknown"; break; /* too harmless to panic */
784 addReplyBulkCString(c
,"maxmemory-policy");
785 addReplyBulkCString(c
,s
);
788 if (stringmatch(pattern
,"appendfsync",0)) {
791 switch(server
.aof_fsync
) {
792 case AOF_FSYNC_NO
: policy
= "no"; break;
793 case AOF_FSYNC_EVERYSEC
: policy
= "everysec"; break;
794 case AOF_FSYNC_ALWAYS
: policy
= "always"; break;
795 default: policy
= "unknown"; break; /* too harmless to panic */
797 addReplyBulkCString(c
,"appendfsync");
798 addReplyBulkCString(c
,policy
);
801 if (stringmatch(pattern
,"save",0)) {
802 sds buf
= sdsempty();
805 for (j
= 0; j
< server
.saveparamslen
; j
++) {
806 buf
= sdscatprintf(buf
,"%ld %d",
807 server
.saveparams
[j
].seconds
,
808 server
.saveparams
[j
].changes
);
809 if (j
!= server
.saveparamslen
-1)
810 buf
= sdscatlen(buf
," ",1);
812 addReplyBulkCString(c
,"save");
813 addReplyBulkCString(c
,buf
);
817 if (stringmatch(pattern
,"loglevel",0)) {
820 switch(server
.verbosity
) {
821 case REDIS_WARNING
: s
= "warning"; break;
822 case REDIS_VERBOSE
: s
= "verbose"; break;
823 case REDIS_NOTICE
: s
= "notice"; break;
824 case REDIS_DEBUG
: s
= "debug"; break;
825 default: s
= "unknown"; break; /* too harmless to panic */
827 addReplyBulkCString(c
,"loglevel");
828 addReplyBulkCString(c
,s
);
831 if (stringmatch(pattern
,"client-output-buffer-limit",0)) {
832 sds buf
= sdsempty();
835 for (j
= 0; j
< REDIS_CLIENT_LIMIT_NUM_CLASSES
; j
++) {
836 buf
= sdscatprintf(buf
,"%s %llu %llu %ld",
837 getClientLimitClassName(j
),
838 server
.client_obuf_limits
[j
].hard_limit_bytes
,
839 server
.client_obuf_limits
[j
].soft_limit_bytes
,
840 (long) server
.client_obuf_limits
[j
].soft_limit_seconds
);
841 if (j
!= REDIS_CLIENT_LIMIT_NUM_CLASSES
-1)
842 buf
= sdscatlen(buf
," ",1);
844 addReplyBulkCString(c
,"client-output-buffer-limit");
845 addReplyBulkCString(c
,buf
);
849 if (stringmatch(pattern
,"unixsocketperm",0)) {
851 snprintf(buf
,sizeof(buf
),"%o",server
.unixsocketperm
);
852 addReplyBulkCString(c
,"unixsocketperm");
853 addReplyBulkCString(c
,buf
);
856 if (stringmatch(pattern
,"slaveof",0)) {
859 addReplyBulkCString(c
,"slaveof");
860 if (server
.masterhost
)
861 snprintf(buf
,sizeof(buf
),"%s %d",
862 server
.masterhost
, server
.masterport
);
865 addReplyBulkCString(c
,buf
);
868 setDeferredMultiBulkLength(c
,replylen
,matches
*2);
871 void configCommand(redisClient
*c
) {
872 if (!strcasecmp(c
->argv
[1]->ptr
,"set")) {
873 if (c
->argc
!= 4) goto badarity
;
875 } else if (!strcasecmp(c
->argv
[1]->ptr
,"get")) {
876 if (c
->argc
!= 3) goto badarity
;
878 } else if (!strcasecmp(c
->argv
[1]->ptr
,"resetstat")) {
879 if (c
->argc
!= 2) goto badarity
;
880 server
.stat_keyspace_hits
= 0;
881 server
.stat_keyspace_misses
= 0;
882 server
.stat_numcommands
= 0;
883 server
.stat_numconnections
= 0;
884 server
.stat_expiredkeys
= 0;
885 server
.stat_rejected_conn
= 0;
886 server
.stat_fork_time
= 0;
887 server
.aof_delayed_fsync
= 0;
888 resetCommandTableStats();
889 addReply(c
,shared
.ok
);
892 "CONFIG subcommand must be one of GET, SET, RESETSTAT");
897 addReplyErrorFormat(c
,"Wrong number of arguments for CONFIG %s",
898 (char*) c
->argv
[1]->ptr
);