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 server
.requirepass
= zstrdup(argv
[1]);
268 } else if (!strcasecmp(argv
[0],"pidfile") && argc
== 2) {
269 zfree(server
.pidfile
);
270 server
.pidfile
= zstrdup(argv
[1]);
271 } else if (!strcasecmp(argv
[0],"dbfilename") && argc
== 2) {
272 zfree(server
.rdb_filename
);
273 server
.rdb_filename
= zstrdup(argv
[1]);
274 } else if (!strcasecmp(argv
[0],"hash-max-ziplist-entries") && argc
== 2) {
275 server
.hash_max_ziplist_entries
= memtoll(argv
[1], NULL
);
276 } else if (!strcasecmp(argv
[0],"hash-max-ziplist-value") && argc
== 2) {
277 server
.hash_max_ziplist_value
= memtoll(argv
[1], NULL
);
278 } else if (!strcasecmp(argv
[0],"list-max-ziplist-entries") && argc
== 2){
279 server
.list_max_ziplist_entries
= memtoll(argv
[1], NULL
);
280 } else if (!strcasecmp(argv
[0],"list-max-ziplist-value") && argc
== 2) {
281 server
.list_max_ziplist_value
= memtoll(argv
[1], NULL
);
282 } else if (!strcasecmp(argv
[0],"set-max-intset-entries") && argc
== 2) {
283 server
.set_max_intset_entries
= memtoll(argv
[1], NULL
);
284 } else if (!strcasecmp(argv
[0],"zset-max-ziplist-entries") && argc
== 2) {
285 server
.zset_max_ziplist_entries
= memtoll(argv
[1], NULL
);
286 } else if (!strcasecmp(argv
[0],"zset-max-ziplist-value") && argc
== 2) {
287 server
.zset_max_ziplist_value
= memtoll(argv
[1], NULL
);
288 } else if (!strcasecmp(argv
[0],"rename-command") && argc
== 3) {
289 struct redisCommand
*cmd
= lookupCommand(argv
[1]);
293 err
= "No such command in rename-command";
297 /* If the target command name is the emtpy string we just
298 * remove it from the command table. */
299 retval
= dictDelete(server
.commands
, argv
[1]);
300 redisAssert(retval
== DICT_OK
);
302 /* Otherwise we re-add the command under a different name. */
303 if (sdslen(argv
[2]) != 0) {
304 sds copy
= sdsdup(argv
[2]);
306 retval
= dictAdd(server
.commands
, copy
, cmd
);
307 if (retval
!= DICT_OK
) {
309 err
= "Target command name already exists"; goto loaderr
;
312 } else if (!strcasecmp(argv
[0],"lua-time-limit") && argc
== 2) {
313 server
.lua_time_limit
= strtoll(argv
[1],NULL
,10);
314 } else if (!strcasecmp(argv
[0],"slowlog-log-slower-than") &&
317 server
.slowlog_log_slower_than
= strtoll(argv
[1],NULL
,10);
318 } else if (!strcasecmp(argv
[0],"slowlog-max-len") && argc
== 2) {
319 server
.slowlog_max_len
= strtoll(argv
[1],NULL
,10);
320 } else if (!strcasecmp(argv
[0],"client-output-buffer-limit") &&
323 int class = getClientLimitClassByName(argv
[1]);
324 unsigned long long hard
, soft
;
328 err
= "Unrecognized client limit class";
331 hard
= memtoll(argv
[2],NULL
);
332 soft
= memtoll(argv
[3],NULL
);
333 soft_seconds
= atoi(argv
[4]);
334 if (soft_seconds
< 0) {
335 err
= "Negative number of seconds in soft limt is invalid";
338 server
.client_obuf_limits
[class].hard_limit_bytes
= hard
;
339 server
.client_obuf_limits
[class].soft_limit_bytes
= soft
;
340 server
.client_obuf_limits
[class].soft_limit_seconds
= soft_seconds
;
341 } else if (!strcasecmp(argv
[0],"stop-writes-on-bgsave-error") &&
343 if ((server
.stop_writes_on_bgsave_err
= yesnotoi(argv
[1])) == -1) {
344 err
= "argument must be 'yes' or 'no'"; goto loaderr
;
347 err
= "Bad directive or wrong number of arguments"; goto loaderr
;
349 sdsfreesplitres(argv
,argc
);
351 sdsfreesplitres(lines
,totlines
);
355 fprintf(stderr
, "\n*** FATAL CONFIG FILE ERROR ***\n");
356 fprintf(stderr
, "Reading the configuration file, at line %d\n", linenum
);
357 fprintf(stderr
, ">>> '%s'\n", lines
[i
]);
358 fprintf(stderr
, "%s\n", err
);
362 /* Load the server configuration from the specified filename.
363 * The function appends the additional configuration directives stored
364 * in the 'options' string to the config file before loading.
366 * Both filename and options can be NULL, in such a case are considered
367 * emtpy. This way loadServerConfig can be used to just load a file or
368 * just load a string. */
369 void loadServerConfig(char *filename
, char *options
) {
370 sds config
= sdsempty();
371 char buf
[REDIS_CONFIGLINE_MAX
+1];
373 /* Load the file content */
377 if (filename
[0] == '-' && filename
[1] == '\0') {
380 if ((fp
= fopen(filename
,"r")) == NULL
) {
381 redisLog(REDIS_WARNING
,
382 "Fatal error, can't open config file '%s'", filename
);
386 while(fgets(buf
,REDIS_CONFIGLINE_MAX
+1,fp
) != NULL
)
387 config
= sdscat(config
,buf
);
388 if (fp
!= stdin
) fclose(fp
);
390 /* Append the additional options */
392 config
= sdscat(config
,"\n");
393 config
= sdscat(config
,options
);
395 loadServerConfigFromString(config
);
399 /*-----------------------------------------------------------------------------
400 * CONFIG command for remote configuration
401 *----------------------------------------------------------------------------*/
403 void configSetCommand(redisClient
*c
) {
406 redisAssertWithInfo(c
,c
->argv
[2],c
->argv
[2]->encoding
== REDIS_ENCODING_RAW
);
407 redisAssertWithInfo(c
,c
->argv
[2],c
->argv
[3]->encoding
== REDIS_ENCODING_RAW
);
410 if (!strcasecmp(c
->argv
[2]->ptr
,"dbfilename")) {
411 zfree(server
.rdb_filename
);
412 server
.rdb_filename
= zstrdup(o
->ptr
);
413 } else if (!strcasecmp(c
->argv
[2]->ptr
,"requirepass")) {
414 zfree(server
.requirepass
);
415 server
.requirepass
= ((char*)o
->ptr
)[0] ? zstrdup(o
->ptr
) : NULL
;
416 } else if (!strcasecmp(c
->argv
[2]->ptr
,"masterauth")) {
417 zfree(server
.masterauth
);
418 server
.masterauth
= zstrdup(o
->ptr
);
419 } else if (!strcasecmp(c
->argv
[2]->ptr
,"maxmemory")) {
420 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
||
422 server
.maxmemory
= ll
;
423 if (server
.maxmemory
) freeMemoryIfNeeded();
424 } else if (!strcasecmp(c
->argv
[2]->ptr
,"maxmemory-policy")) {
425 if (!strcasecmp(o
->ptr
,"volatile-lru")) {
426 server
.maxmemory_policy
= REDIS_MAXMEMORY_VOLATILE_LRU
;
427 } else if (!strcasecmp(o
->ptr
,"volatile-random")) {
428 server
.maxmemory_policy
= REDIS_MAXMEMORY_VOLATILE_RANDOM
;
429 } else if (!strcasecmp(o
->ptr
,"volatile-ttl")) {
430 server
.maxmemory_policy
= REDIS_MAXMEMORY_VOLATILE_TTL
;
431 } else if (!strcasecmp(o
->ptr
,"allkeys-lru")) {
432 server
.maxmemory_policy
= REDIS_MAXMEMORY_ALLKEYS_LRU
;
433 } else if (!strcasecmp(o
->ptr
,"allkeys-random")) {
434 server
.maxmemory_policy
= REDIS_MAXMEMORY_ALLKEYS_RANDOM
;
435 } else if (!strcasecmp(o
->ptr
,"noeviction")) {
436 server
.maxmemory_policy
= REDIS_MAXMEMORY_NO_EVICTION
;
440 } else if (!strcasecmp(c
->argv
[2]->ptr
,"maxmemory-samples")) {
441 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
||
442 ll
<= 0) goto badfmt
;
443 server
.maxmemory_samples
= ll
;
444 } else if (!strcasecmp(c
->argv
[2]->ptr
,"timeout")) {
445 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
||
446 ll
< 0 || ll
> LONG_MAX
) goto badfmt
;
447 server
.maxidletime
= ll
;
448 } else if (!strcasecmp(c
->argv
[2]->ptr
,"appendfsync")) {
449 if (!strcasecmp(o
->ptr
,"no")) {
450 server
.aof_fsync
= AOF_FSYNC_NO
;
451 } else if (!strcasecmp(o
->ptr
,"everysec")) {
452 server
.aof_fsync
= AOF_FSYNC_EVERYSEC
;
453 } else if (!strcasecmp(o
->ptr
,"always")) {
454 server
.aof_fsync
= AOF_FSYNC_ALWAYS
;
458 } else if (!strcasecmp(c
->argv
[2]->ptr
,"no-appendfsync-on-rewrite")) {
459 int yn
= yesnotoi(o
->ptr
);
461 if (yn
== -1) goto badfmt
;
462 server
.aof_no_fsync_on_rewrite
= yn
;
463 } else if (!strcasecmp(c
->argv
[2]->ptr
,"appendonly")) {
464 int enable
= yesnotoi(o
->ptr
);
466 if (enable
== -1) goto badfmt
;
467 if (enable
== 0 && server
.aof_state
!= REDIS_AOF_OFF
) {
469 } else if (enable
&& server
.aof_state
== REDIS_AOF_OFF
) {
470 if (startAppendOnly() == REDIS_ERR
) {
472 "Unable to turn on AOF. Check server logs.");
476 } else if (!strcasecmp(c
->argv
[2]->ptr
,"auto-aof-rewrite-percentage")) {
477 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
|| ll
< 0) goto badfmt
;
478 server
.aof_rewrite_perc
= ll
;
479 } else if (!strcasecmp(c
->argv
[2]->ptr
,"auto-aof-rewrite-min-size")) {
480 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
|| ll
< 0) goto badfmt
;
481 server
.aof_rewrite_min_size
= ll
;
482 } else if (!strcasecmp(c
->argv
[2]->ptr
,"save")) {
484 sds
*v
= sdssplitlen(o
->ptr
,sdslen(o
->ptr
)," ",1,&vlen
);
486 /* Perform sanity check before setting the new config:
487 * - Even number of args
488 * - Seconds >= 1, changes >= 0 */
490 sdsfreesplitres(v
,vlen
);
493 for (j
= 0; j
< vlen
; j
++) {
497 val
= strtoll(v
[j
], &eptr
, 10);
498 if (eptr
[0] != '\0' ||
499 ((j
& 1) == 0 && val
< 1) ||
500 ((j
& 1) == 1 && val
< 0)) {
501 sdsfreesplitres(v
,vlen
);
505 /* Finally set the new config */
506 resetServerSaveParams();
507 for (j
= 0; j
< vlen
; j
+= 2) {
511 seconds
= strtoll(v
[j
],NULL
,10);
512 changes
= strtoll(v
[j
+1],NULL
,10);
513 appendServerSaveParams(seconds
, changes
);
515 sdsfreesplitres(v
,vlen
);
516 } else if (!strcasecmp(c
->argv
[2]->ptr
,"slave-serve-stale-data")) {
517 int yn
= yesnotoi(o
->ptr
);
519 if (yn
== -1) goto badfmt
;
520 server
.repl_serve_stale_data
= yn
;
521 } else if (!strcasecmp(c
->argv
[2]->ptr
,"slave-read-only")) {
522 int yn
= yesnotoi(o
->ptr
);
524 if (yn
== -1) goto badfmt
;
525 server
.repl_slave_ro
= yn
;
526 } else if (!strcasecmp(c
->argv
[2]->ptr
,"dir")) {
527 if (chdir((char*)o
->ptr
) == -1) {
528 addReplyErrorFormat(c
,"Changing directory: %s", strerror(errno
));
531 } else if (!strcasecmp(c
->argv
[2]->ptr
,"hash-max-ziplist-entries")) {
532 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
|| ll
< 0) goto badfmt
;
533 server
.hash_max_ziplist_entries
= ll
;
534 } else if (!strcasecmp(c
->argv
[2]->ptr
,"hash-max-ziplist-value")) {
535 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
|| ll
< 0) goto badfmt
;
536 server
.hash_max_ziplist_value
= ll
;
537 } else if (!strcasecmp(c
->argv
[2]->ptr
,"list-max-ziplist-entries")) {
538 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
|| ll
< 0) goto badfmt
;
539 server
.list_max_ziplist_entries
= ll
;
540 } else if (!strcasecmp(c
->argv
[2]->ptr
,"list-max-ziplist-value")) {
541 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
|| ll
< 0) goto badfmt
;
542 server
.list_max_ziplist_value
= ll
;
543 } else if (!strcasecmp(c
->argv
[2]->ptr
,"set-max-intset-entries")) {
544 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
|| ll
< 0) goto badfmt
;
545 server
.set_max_intset_entries
= ll
;
546 } else if (!strcasecmp(c
->argv
[2]->ptr
,"zset-max-ziplist-entries")) {
547 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
|| ll
< 0) goto badfmt
;
548 server
.zset_max_ziplist_entries
= ll
;
549 } else if (!strcasecmp(c
->argv
[2]->ptr
,"zset-max-ziplist-value")) {
550 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
|| ll
< 0) goto badfmt
;
551 server
.zset_max_ziplist_value
= ll
;
552 } else if (!strcasecmp(c
->argv
[2]->ptr
,"lua-time-limit")) {
553 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
|| ll
< 0) goto badfmt
;
554 server
.lua_time_limit
= ll
;
555 } else if (!strcasecmp(c
->argv
[2]->ptr
,"slowlog-log-slower-than")) {
556 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
) goto badfmt
;
557 server
.slowlog_log_slower_than
= ll
;
558 } else if (!strcasecmp(c
->argv
[2]->ptr
,"slowlog-max-len")) {
559 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
|| ll
< 0) goto badfmt
;
560 server
.slowlog_max_len
= (unsigned)ll
;
561 } else if (!strcasecmp(c
->argv
[2]->ptr
,"loglevel")) {
562 if (!strcasecmp(o
->ptr
,"warning")) {
563 server
.verbosity
= REDIS_WARNING
;
564 } else if (!strcasecmp(o
->ptr
,"notice")) {
565 server
.verbosity
= REDIS_NOTICE
;
566 } else if (!strcasecmp(o
->ptr
,"verbose")) {
567 server
.verbosity
= REDIS_VERBOSE
;
568 } else if (!strcasecmp(o
->ptr
,"debug")) {
569 server
.verbosity
= REDIS_DEBUG
;
573 } else if (!strcasecmp(c
->argv
[2]->ptr
,"client-output-buffer-limit")) {
575 sds
*v
= sdssplitlen(o
->ptr
,sdslen(o
->ptr
)," ",1,&vlen
);
577 /* We need a multiple of 4: <class> <hard> <soft> <soft_seconds> */
579 sdsfreesplitres(v
,vlen
);
583 /* Sanity check of single arguments, so that we either refuse the
584 * whole configuration string or accept it all, even if a single
585 * error in a single client class is present. */
586 for (j
= 0; j
< vlen
; j
++) {
591 if (getClientLimitClassByName(v
[j
]) == -1) {
592 sdsfreesplitres(v
,vlen
);
596 val
= strtoll(v
[j
], &eptr
, 10);
597 if (eptr
[0] != '\0' || val
< 0) {
598 sdsfreesplitres(v
,vlen
);
603 /* Finally set the new config */
604 for (j
= 0; j
< vlen
; j
+= 4) {
606 unsigned long long hard
, soft
;
609 class = getClientLimitClassByName(v
[j
]);
610 hard
= strtoll(v
[j
+1],NULL
,10);
611 soft
= strtoll(v
[j
+2],NULL
,10);
612 soft_seconds
= strtoll(v
[j
+3],NULL
,10);
614 server
.client_obuf_limits
[class].hard_limit_bytes
= hard
;
615 server
.client_obuf_limits
[class].soft_limit_bytes
= soft
;
616 server
.client_obuf_limits
[class].soft_limit_seconds
= soft_seconds
;
618 sdsfreesplitres(v
,vlen
);
619 } else if (!strcasecmp(c
->argv
[2]->ptr
,"stop-writes-on-bgsave-error")) {
620 int yn
= yesnotoi(o
->ptr
);
622 if (yn
== -1) goto badfmt
;
623 server
.stop_writes_on_bgsave_err
= yn
;
624 } else if (!strcasecmp(c
->argv
[2]->ptr
,"repl-ping-slave-period")) {
625 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
|| ll
<= 0) goto badfmt
;
626 server
.repl_ping_slave_period
= ll
;
627 } else if (!strcasecmp(c
->argv
[2]->ptr
,"repl-timeout")) {
628 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
|| ll
<= 0) goto badfmt
;
629 server
.repl_timeout
= ll
;
630 } else if (!strcasecmp(c
->argv
[2]->ptr
,"watchdog-period")) {
631 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
|| ll
< 0) goto badfmt
;
636 } else if (!strcasecmp(c
->argv
[2]->ptr
,"rdbcompression")) {
637 int yn
= yesnotoi(o
->ptr
);
639 if (yn
== -1) goto badfmt
;
640 server
.rdb_compression
= yn
;
641 } else if (!strcasecmp(c
->argv
[2]->ptr
,"rdbchecksum")) {
642 int yn
= yesnotoi(o
->ptr
);
644 if (yn
== -1) goto badfmt
;
645 server
.rdb_checksum
= yn
;
647 addReplyErrorFormat(c
,"Unsupported CONFIG parameter: %s",
648 (char*)c
->argv
[2]->ptr
);
651 addReply(c
,shared
.ok
);
654 badfmt
: /* Bad format errors */
655 addReplyErrorFormat(c
,"Invalid argument '%s' for CONFIG SET '%s'",
657 (char*)c
->argv
[2]->ptr
);
660 #define config_get_string_field(_name,_var) do { \
661 if (stringmatch(pattern,_name,0)) { \
662 addReplyBulkCString(c,_name); \
663 addReplyBulkCString(c,_var ? _var : ""); \
668 #define config_get_bool_field(_name,_var) do { \
669 if (stringmatch(pattern,_name,0)) { \
670 addReplyBulkCString(c,_name); \
671 addReplyBulkCString(c,_var ? "yes" : "no"); \
676 #define config_get_numerical_field(_name,_var) do { \
677 if (stringmatch(pattern,_name,0)) { \
678 ll2string(buf,sizeof(buf),_var); \
679 addReplyBulkCString(c,_name); \
680 addReplyBulkCString(c,buf); \
685 void configGetCommand(redisClient
*c
) {
686 robj
*o
= c
->argv
[2];
687 void *replylen
= addDeferredMultiBulkLength(c
);
688 char *pattern
= o
->ptr
;
691 redisAssertWithInfo(c
,o
,o
->encoding
== REDIS_ENCODING_RAW
);
694 config_get_string_field("dbfilename",server
.rdb_filename
);
695 config_get_string_field("requirepass",server
.requirepass
);
696 config_get_string_field("masterauth",server
.requirepass
);
697 config_get_string_field("bind",server
.bindaddr
);
698 config_get_string_field("unixsocket",server
.unixsocket
);
699 config_get_string_field("logfile",server
.logfile
);
700 config_get_string_field("pidfile",server
.pidfile
);
702 /* Numerical values */
703 config_get_numerical_field("maxmemory",server
.maxmemory
);
704 config_get_numerical_field("maxmemory-samples",server
.maxmemory_samples
);
705 config_get_numerical_field("timeout",server
.maxidletime
);
706 config_get_numerical_field("auto-aof-rewrite-percentage",
707 server
.aof_rewrite_perc
);
708 config_get_numerical_field("auto-aof-rewrite-min-size",
709 server
.aof_rewrite_min_size
);
710 config_get_numerical_field("hash-max-ziplist-entries",
711 server
.hash_max_ziplist_entries
);
712 config_get_numerical_field("hash-max-ziplist-value",
713 server
.hash_max_ziplist_value
);
714 config_get_numerical_field("list-max-ziplist-entries",
715 server
.list_max_ziplist_entries
);
716 config_get_numerical_field("list-max-ziplist-value",
717 server
.list_max_ziplist_value
);
718 config_get_numerical_field("set-max-intset-entries",
719 server
.set_max_intset_entries
);
720 config_get_numerical_field("zset-max-ziplist-entries",
721 server
.zset_max_ziplist_entries
);
722 config_get_numerical_field("zset-max-ziplist-value",
723 server
.zset_max_ziplist_value
);
724 config_get_numerical_field("lua-time-limit",server
.lua_time_limit
);
725 config_get_numerical_field("slowlog-log-slower-than",
726 server
.slowlog_log_slower_than
);
727 config_get_numerical_field("slowlog-max-len",
728 server
.slowlog_max_len
);
729 config_get_numerical_field("port",server
.port
);
730 config_get_numerical_field("databases",server
.dbnum
);
731 config_get_numerical_field("repl-ping-slave-period",server
.repl_ping_slave_period
);
732 config_get_numerical_field("repl-timeout",server
.repl_timeout
);
733 config_get_numerical_field("maxclients",server
.maxclients
);
734 config_get_numerical_field("watchdog-period",server
.watchdog_period
);
736 /* Bool (yes/no) values */
737 config_get_bool_field("no-appendfsync-on-rewrite",
738 server
.aof_no_fsync_on_rewrite
);
739 config_get_bool_field("slave-serve-stale-data",
740 server
.repl_serve_stale_data
);
741 config_get_bool_field("slave-read-only",
742 server
.repl_slave_ro
);
743 config_get_bool_field("stop-writes-on-bgsave-error",
744 server
.stop_writes_on_bgsave_err
);
745 config_get_bool_field("daemonize", server
.daemonize
);
746 config_get_bool_field("rdbcompression", server
.rdb_compression
);
747 config_get_bool_field("rdbchecksum", server
.rdb_checksum
);
748 config_get_bool_field("activerehashing", server
.activerehashing
);
750 /* Everything we can't handle with macros follows. */
752 if (stringmatch(pattern
,"appendonly",0)) {
753 addReplyBulkCString(c
,"appendonly");
754 addReplyBulkCString(c
,server
.aof_state
== REDIS_AOF_OFF
? "no" : "yes");
757 if (stringmatch(pattern
,"dir",0)) {
760 if (getcwd(buf
,sizeof(buf
)) == NULL
)
763 addReplyBulkCString(c
,"dir");
764 addReplyBulkCString(c
,buf
);
767 if (stringmatch(pattern
,"maxmemory-policy",0)) {
770 switch(server
.maxmemory_policy
) {
771 case REDIS_MAXMEMORY_VOLATILE_LRU
: s
= "volatile-lru"; break;
772 case REDIS_MAXMEMORY_VOLATILE_TTL
: s
= "volatile-ttl"; break;
773 case REDIS_MAXMEMORY_VOLATILE_RANDOM
: s
= "volatile-random"; break;
774 case REDIS_MAXMEMORY_ALLKEYS_LRU
: s
= "allkeys-lru"; break;
775 case REDIS_MAXMEMORY_ALLKEYS_RANDOM
: s
= "allkeys-random"; break;
776 case REDIS_MAXMEMORY_NO_EVICTION
: s
= "noeviction"; break;
777 default: s
= "unknown"; break; /* too harmless to panic */
779 addReplyBulkCString(c
,"maxmemory-policy");
780 addReplyBulkCString(c
,s
);
783 if (stringmatch(pattern
,"appendfsync",0)) {
786 switch(server
.aof_fsync
) {
787 case AOF_FSYNC_NO
: policy
= "no"; break;
788 case AOF_FSYNC_EVERYSEC
: policy
= "everysec"; break;
789 case AOF_FSYNC_ALWAYS
: policy
= "always"; break;
790 default: policy
= "unknown"; break; /* too harmless to panic */
792 addReplyBulkCString(c
,"appendfsync");
793 addReplyBulkCString(c
,policy
);
796 if (stringmatch(pattern
,"save",0)) {
797 sds buf
= sdsempty();
800 for (j
= 0; j
< server
.saveparamslen
; j
++) {
801 buf
= sdscatprintf(buf
,"%ld %d",
802 server
.saveparams
[j
].seconds
,
803 server
.saveparams
[j
].changes
);
804 if (j
!= server
.saveparamslen
-1)
805 buf
= sdscatlen(buf
," ",1);
807 addReplyBulkCString(c
,"save");
808 addReplyBulkCString(c
,buf
);
812 if (stringmatch(pattern
,"loglevel",0)) {
815 switch(server
.verbosity
) {
816 case REDIS_WARNING
: s
= "warning"; break;
817 case REDIS_VERBOSE
: s
= "verbose"; break;
818 case REDIS_NOTICE
: s
= "notice"; break;
819 case REDIS_DEBUG
: s
= "debug"; break;
820 default: s
= "unknown"; break; /* too harmless to panic */
822 addReplyBulkCString(c
,"loglevel");
823 addReplyBulkCString(c
,s
);
826 if (stringmatch(pattern
,"client-output-buffer-limit",0)) {
827 sds buf
= sdsempty();
830 for (j
= 0; j
< REDIS_CLIENT_LIMIT_NUM_CLASSES
; j
++) {
831 buf
= sdscatprintf(buf
,"%s %llu %llu %ld",
832 getClientLimitClassName(j
),
833 server
.client_obuf_limits
[j
].hard_limit_bytes
,
834 server
.client_obuf_limits
[j
].soft_limit_bytes
,
835 (long) server
.client_obuf_limits
[j
].soft_limit_seconds
);
836 if (j
!= REDIS_CLIENT_LIMIT_NUM_CLASSES
-1)
837 buf
= sdscatlen(buf
," ",1);
839 addReplyBulkCString(c
,"client-output-buffer-limit");
840 addReplyBulkCString(c
,buf
);
844 if (stringmatch(pattern
,"unixsocketperm",0)) {
846 snprintf(buf
,sizeof(buf
),"%o",server
.unixsocketperm
);
847 addReplyBulkCString(c
,"unixsocketperm");
848 addReplyBulkCString(c
,buf
);
851 if (stringmatch(pattern
,"slaveof",0)) {
854 addReplyBulkCString(c
,"slaveof");
855 if (server
.masterhost
)
856 snprintf(buf
,sizeof(buf
),"%s %d",
857 server
.masterhost
, server
.masterport
);
860 addReplyBulkCString(c
,buf
);
863 setDeferredMultiBulkLength(c
,replylen
,matches
*2);
866 void configCommand(redisClient
*c
) {
867 if (!strcasecmp(c
->argv
[1]->ptr
,"set")) {
868 if (c
->argc
!= 4) goto badarity
;
870 } else if (!strcasecmp(c
->argv
[1]->ptr
,"get")) {
871 if (c
->argc
!= 3) goto badarity
;
873 } else if (!strcasecmp(c
->argv
[1]->ptr
,"resetstat")) {
874 if (c
->argc
!= 2) goto badarity
;
875 server
.stat_keyspace_hits
= 0;
876 server
.stat_keyspace_misses
= 0;
877 server
.stat_numcommands
= 0;
878 server
.stat_numconnections
= 0;
879 server
.stat_expiredkeys
= 0;
880 server
.stat_rejected_conn
= 0;
881 server
.stat_fork_time
= 0;
882 server
.aof_delayed_fsync
= 0;
883 resetCommandTableStats();
884 addReply(c
,shared
.ok
);
887 "CONFIG subcommand must be one of GET, SET, RESETSTAT");
892 addReplyErrorFormat(c
,"Wrong number of arguments for CONFIG %s",
893 (char*) c
->argv
[1]->ptr
);