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 /* I agree, this is a very rudimental way to load a configuration...
27 will improve later if the config gets more complex */
28 void loadServerConfig(char *filename
) {
30 char buf
[REDIS_CONFIGLINE_MAX
+1], *err
= NULL
;
34 if (filename
[0] == '-' && filename
[1] == '\0')
37 if ((fp
= fopen(filename
,"r")) == NULL
) {
38 redisLog(REDIS_WARNING
, "Fatal error, can't open config file '%s'", filename
);
43 while(fgets(buf
,REDIS_CONFIGLINE_MAX
+1,fp
) != NULL
) {
49 line
= sdstrim(line
," \t\r\n");
51 /* Skip comments and blank lines*/
52 if (line
[0] == '#' || line
[0] == '\0') {
57 /* Split into arguments */
58 argv
= sdssplitargs(line
,&argc
);
61 /* Execute config directives */
62 if (!strcasecmp(argv
[0],"timeout") && argc
== 2) {
63 server
.maxidletime
= atoi(argv
[1]);
64 if (server
.maxidletime
< 0) {
65 err
= "Invalid timeout value"; goto loaderr
;
67 } else if (!strcasecmp(argv
[0],"port") && argc
== 2) {
68 server
.port
= atoi(argv
[1]);
69 if (server
.port
< 1 || server
.port
> 65535) {
70 err
= "Invalid port"; goto loaderr
;
72 } else if (!strcasecmp(argv
[0],"bind") && argc
== 2) {
73 server
.bindaddr
= zstrdup(argv
[1]);
74 } else if (!strcasecmp(argv
[0],"unixsocket") && argc
== 2) {
75 server
.unixsocket
= zstrdup(argv
[1]);
76 } else if (!strcasecmp(argv
[0],"save") && argc
== 3) {
77 int seconds
= atoi(argv
[1]);
78 int changes
= atoi(argv
[2]);
79 if (seconds
< 1 || changes
< 0) {
80 err
= "Invalid save parameters"; goto loaderr
;
82 appendServerSaveParams(seconds
,changes
);
83 } else if (!strcasecmp(argv
[0],"dir") && argc
== 2) {
84 if (chdir(argv
[1]) == -1) {
85 redisLog(REDIS_WARNING
,"Can't chdir to '%s': %s",
86 argv
[1], strerror(errno
));
89 } else if (!strcasecmp(argv
[0],"loglevel") && argc
== 2) {
90 if (!strcasecmp(argv
[1],"debug")) server
.verbosity
= REDIS_DEBUG
;
91 else if (!strcasecmp(argv
[1],"verbose")) server
.verbosity
= REDIS_VERBOSE
;
92 else if (!strcasecmp(argv
[1],"notice")) server
.verbosity
= REDIS_NOTICE
;
93 else if (!strcasecmp(argv
[1],"warning")) server
.verbosity
= REDIS_WARNING
;
95 err
= "Invalid log level. Must be one of debug, notice, warning";
98 } else if (!strcasecmp(argv
[0],"logfile") && argc
== 2) {
101 server
.logfile
= zstrdup(argv
[1]);
102 if (!strcasecmp(server
.logfile
,"stdout")) {
103 zfree(server
.logfile
);
104 server
.logfile
= NULL
;
106 if (server
.logfile
) {
107 /* Test if we are able to open the file. The server will not
108 * be able to abort just for this problem later... */
109 logfp
= fopen(server
.logfile
,"a");
111 err
= sdscatprintf(sdsempty(),
112 "Can't open the log file: %s", strerror(errno
));
117 } else if (!strcasecmp(argv
[0],"databases") && argc
== 2) {
118 server
.dbnum
= atoi(argv
[1]);
119 if (server
.dbnum
< 1) {
120 err
= "Invalid number of databases"; goto loaderr
;
122 } else if (!strcasecmp(argv
[0],"include") && argc
== 2) {
123 loadServerConfig(argv
[1]);
124 } else if (!strcasecmp(argv
[0],"maxclients") && argc
== 2) {
125 server
.maxclients
= atoi(argv
[1]);
126 } else if (!strcasecmp(argv
[0],"maxmemory") && argc
== 2) {
127 server
.maxmemory
= memtoll(argv
[1],NULL
);
128 } else if (!strcasecmp(argv
[0],"maxmemory-policy") && argc
== 2) {
129 if (!strcasecmp(argv
[1],"volatile-lru")) {
130 server
.maxmemory_policy
= REDIS_MAXMEMORY_VOLATILE_LRU
;
131 } else if (!strcasecmp(argv
[1],"volatile-random")) {
132 server
.maxmemory_policy
= REDIS_MAXMEMORY_VOLATILE_RANDOM
;
133 } else if (!strcasecmp(argv
[1],"volatile-ttl")) {
134 server
.maxmemory_policy
= REDIS_MAXMEMORY_VOLATILE_TTL
;
135 } else if (!strcasecmp(argv
[1],"allkeys-lru")) {
136 server
.maxmemory_policy
= REDIS_MAXMEMORY_ALLKEYS_LRU
;
137 } else if (!strcasecmp(argv
[1],"allkeys-random")) {
138 server
.maxmemory_policy
= REDIS_MAXMEMORY_ALLKEYS_RANDOM
;
140 err
= "Invalid maxmemory policy";
143 } else if (!strcasecmp(argv
[0],"maxmemory-samples") && argc
== 2) {
144 server
.maxmemory_samples
= atoi(argv
[1]);
145 if (server
.maxmemory_samples
<= 0) {
146 err
= "maxmemory-samples must be 1 or greater";
149 } else if (!strcasecmp(argv
[0],"slaveof") && argc
== 3) {
150 server
.masterhost
= sdsnew(argv
[1]);
151 server
.masterport
= atoi(argv
[2]);
152 server
.replstate
= REDIS_REPL_CONNECT
;
153 } else if (!strcasecmp(argv
[0],"masterauth") && argc
== 2) {
154 server
.masterauth
= zstrdup(argv
[1]);
155 } else if (!strcasecmp(argv
[0],"glueoutputbuf") && argc
== 2) {
156 if ((server
.glueoutputbuf
= yesnotoi(argv
[1])) == -1) {
157 err
= "argument must be 'yes' or 'no'"; goto loaderr
;
159 } else if (!strcasecmp(argv
[0],"rdbcompression") && argc
== 2) {
160 if ((server
.rdbcompression
= yesnotoi(argv
[1])) == -1) {
161 err
= "argument must be 'yes' or 'no'"; goto loaderr
;
163 } else if (!strcasecmp(argv
[0],"activerehashing") && argc
== 2) {
164 if ((server
.activerehashing
= yesnotoi(argv
[1])) == -1) {
165 err
= "argument must be 'yes' or 'no'"; goto loaderr
;
167 } else if (!strcasecmp(argv
[0],"daemonize") && argc
== 2) {
168 if ((server
.daemonize
= yesnotoi(argv
[1])) == -1) {
169 err
= "argument must be 'yes' or 'no'"; goto loaderr
;
171 } else if (!strcasecmp(argv
[0],"appendonly") && argc
== 2) {
172 if ((server
.appendonly
= yesnotoi(argv
[1])) == -1) {
173 err
= "argument must be 'yes' or 'no'"; goto loaderr
;
175 } else if (!strcasecmp(argv
[0],"appendfilename") && argc
== 2) {
176 zfree(server
.appendfilename
);
177 server
.appendfilename
= zstrdup(argv
[1]);
178 } else if (!strcasecmp(argv
[0],"no-appendfsync-on-rewrite")
180 if ((server
.no_appendfsync_on_rewrite
= yesnotoi(argv
[1])) == -1) {
181 err
= "argument must be 'yes' or 'no'"; goto loaderr
;
183 } else if (!strcasecmp(argv
[0],"appendfsync") && argc
== 2) {
184 if (!strcasecmp(argv
[1],"no")) {
185 server
.appendfsync
= APPENDFSYNC_NO
;
186 } else if (!strcasecmp(argv
[1],"always")) {
187 server
.appendfsync
= APPENDFSYNC_ALWAYS
;
188 } else if (!strcasecmp(argv
[1],"everysec")) {
189 server
.appendfsync
= APPENDFSYNC_EVERYSEC
;
191 err
= "argument must be 'no', 'always' or 'everysec'";
194 } else if (!strcasecmp(argv
[0],"requirepass") && argc
== 2) {
195 server
.requirepass
= zstrdup(argv
[1]);
196 } else if (!strcasecmp(argv
[0],"pidfile") && argc
== 2) {
197 zfree(server
.pidfile
);
198 server
.pidfile
= zstrdup(argv
[1]);
199 } else if (!strcasecmp(argv
[0],"dbfilename") && argc
== 2) {
200 zfree(server
.dbfilename
);
201 server
.dbfilename
= zstrdup(argv
[1]);
202 } else if (!strcasecmp(argv
[0],"vm-enabled") && argc
== 2) {
203 if ((server
.vm_enabled
= yesnotoi(argv
[1])) == -1) {
204 err
= "argument must be 'yes' or 'no'"; goto loaderr
;
206 } else if (!strcasecmp(argv
[0],"vm-swap-file") && argc
== 2) {
207 zfree(server
.vm_swap_file
);
208 server
.vm_swap_file
= zstrdup(argv
[1]);
209 } else if (!strcasecmp(argv
[0],"vm-max-memory") && argc
== 2) {
210 server
.vm_max_memory
= memtoll(argv
[1],NULL
);
211 } else if (!strcasecmp(argv
[0],"vm-page-size") && argc
== 2) {
212 server
.vm_page_size
= memtoll(argv
[1], NULL
);
213 } else if (!strcasecmp(argv
[0],"vm-pages") && argc
== 2) {
214 server
.vm_pages
= memtoll(argv
[1], NULL
);
215 } else if (!strcasecmp(argv
[0],"vm-max-threads") && argc
== 2) {
216 server
.vm_max_threads
= strtoll(argv
[1], NULL
, 10);
217 } else if (!strcasecmp(argv
[0],"hash-max-zipmap-entries") && argc
== 2){
218 server
.hash_max_zipmap_entries
= memtoll(argv
[1], NULL
);
219 } else if (!strcasecmp(argv
[0],"hash-max-zipmap-value") && argc
== 2){
220 server
.hash_max_zipmap_value
= memtoll(argv
[1], NULL
);
221 } else if (!strcasecmp(argv
[0],"list-max-ziplist-entries") && argc
== 2){
222 server
.list_max_ziplist_entries
= memtoll(argv
[1], NULL
);
223 } else if (!strcasecmp(argv
[0],"list-max-ziplist-value") && argc
== 2){
224 server
.list_max_ziplist_value
= memtoll(argv
[1], NULL
);
225 } else if (!strcasecmp(argv
[0],"set-max-intset-entries") && argc
== 2){
226 server
.set_max_intset_entries
= memtoll(argv
[1], NULL
);
228 err
= "Bad directive or wrong number of arguments"; goto loaderr
;
230 for (j
= 0; j
< argc
; j
++)
235 if (fp
!= stdin
) fclose(fp
);
239 fprintf(stderr
, "\n*** FATAL CONFIG FILE ERROR ***\n");
240 fprintf(stderr
, "Reading the configuration file, at line %d\n", linenum
);
241 fprintf(stderr
, ">>> '%s'\n", line
);
242 fprintf(stderr
, "%s\n", err
);
246 /*-----------------------------------------------------------------------------
247 * CONFIG command for remote configuration
248 *----------------------------------------------------------------------------*/
250 void configSetCommand(redisClient
*c
) {
253 redisAssert(c
->argv
[2]->encoding
== REDIS_ENCODING_RAW
);
254 redisAssert(c
->argv
[3]->encoding
== REDIS_ENCODING_RAW
);
257 if (!strcasecmp(c
->argv
[2]->ptr
,"dbfilename")) {
258 zfree(server
.dbfilename
);
259 server
.dbfilename
= zstrdup(o
->ptr
);
260 } else if (!strcasecmp(c
->argv
[2]->ptr
,"requirepass")) {
261 zfree(server
.requirepass
);
262 server
.requirepass
= zstrdup(o
->ptr
);
263 } else if (!strcasecmp(c
->argv
[2]->ptr
,"masterauth")) {
264 zfree(server
.masterauth
);
265 server
.masterauth
= zstrdup(o
->ptr
);
266 } else if (!strcasecmp(c
->argv
[2]->ptr
,"maxmemory")) {
267 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
||
269 server
.maxmemory
= ll
;
270 if (server
.maxmemory
) freeMemoryIfNeeded();
271 } else if (!strcasecmp(c
->argv
[2]->ptr
,"maxmemory-policy")) {
272 if (!strcasecmp(o
->ptr
,"volatile-lru")) {
273 server
.maxmemory_policy
= REDIS_MAXMEMORY_VOLATILE_LRU
;
274 } else if (!strcasecmp(o
->ptr
,"volatile-random")) {
275 server
.maxmemory_policy
= REDIS_MAXMEMORY_VOLATILE_RANDOM
;
276 } else if (!strcasecmp(o
->ptr
,"volatile-ttl")) {
277 server
.maxmemory_policy
= REDIS_MAXMEMORY_VOLATILE_TTL
;
278 } else if (!strcasecmp(o
->ptr
,"allkeys-lru")) {
279 server
.maxmemory_policy
= REDIS_MAXMEMORY_ALLKEYS_LRU
;
280 } else if (!strcasecmp(o
->ptr
,"allkeys-random")) {
281 server
.maxmemory_policy
= REDIS_MAXMEMORY_ALLKEYS_RANDOM
;
285 } else if (!strcasecmp(c
->argv
[2]->ptr
,"maxmemory-samples")) {
286 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
||
287 ll
<= 0) goto badfmt
;
288 server
.maxmemory_samples
= ll
;
289 } else if (!strcasecmp(c
->argv
[2]->ptr
,"timeout")) {
290 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
||
291 ll
< 0 || ll
> LONG_MAX
) goto badfmt
;
292 server
.maxidletime
= ll
;
293 } else if (!strcasecmp(c
->argv
[2]->ptr
,"appendfsync")) {
294 if (!strcasecmp(o
->ptr
,"no")) {
295 server
.appendfsync
= APPENDFSYNC_NO
;
296 } else if (!strcasecmp(o
->ptr
,"everysec")) {
297 server
.appendfsync
= APPENDFSYNC_EVERYSEC
;
298 } else if (!strcasecmp(o
->ptr
,"always")) {
299 server
.appendfsync
= APPENDFSYNC_ALWAYS
;
303 } else if (!strcasecmp(c
->argv
[2]->ptr
,"no-appendfsync-on-rewrite")) {
304 int yn
= yesnotoi(o
->ptr
);
306 if (yn
== -1) goto badfmt
;
307 server
.no_appendfsync_on_rewrite
= yn
;
308 } else if (!strcasecmp(c
->argv
[2]->ptr
,"appendonly")) {
309 int old
= server
.appendonly
;
310 int new = yesnotoi(o
->ptr
);
312 if (new == -1) goto badfmt
;
317 if (startAppendOnly() == REDIS_ERR
) {
319 "Unable to turn on AOF. Check server logs.");
324 } else if (!strcasecmp(c
->argv
[2]->ptr
,"save")) {
326 sds
*v
= sdssplitlen(o
->ptr
,sdslen(o
->ptr
)," ",1,&vlen
);
328 /* Perform sanity check before setting the new config:
329 * - Even number of args
330 * - Seconds >= 1, changes >= 0 */
332 sdsfreesplitres(v
,vlen
);
335 for (j
= 0; j
< vlen
; j
++) {
339 val
= strtoll(v
[j
], &eptr
, 10);
340 if (eptr
[0] != '\0' ||
341 ((j
& 1) == 0 && val
< 1) ||
342 ((j
& 1) == 1 && val
< 0)) {
343 sdsfreesplitres(v
,vlen
);
347 /* Finally set the new config */
348 resetServerSaveParams();
349 for (j
= 0; j
< vlen
; j
+= 2) {
353 seconds
= strtoll(v
[j
],NULL
,10);
354 changes
= strtoll(v
[j
+1],NULL
,10);
355 appendServerSaveParams(seconds
, changes
);
357 sdsfreesplitres(v
,vlen
);
359 addReplyErrorFormat(c
,"Unsupported CONFIG parameter: %s",
360 (char*)c
->argv
[2]->ptr
);
363 addReply(c
,shared
.ok
);
366 badfmt
: /* Bad format errors */
367 addReplyErrorFormat(c
,"Invalid argument '%s' for CONFIG SET '%s'",
369 (char*)c
->argv
[2]->ptr
);
372 void configGetCommand(redisClient
*c
) {
373 robj
*o
= c
->argv
[2];
374 void *replylen
= addDeferredMultiBulkLength(c
);
375 char *pattern
= o
->ptr
;
378 redisAssert(o
->encoding
== REDIS_ENCODING_RAW
);
380 if (stringmatch(pattern
,"dbfilename",0)) {
381 addReplyBulkCString(c
,"dbfilename");
382 addReplyBulkCString(c
,server
.dbfilename
);
385 if (stringmatch(pattern
,"requirepass",0)) {
386 addReplyBulkCString(c
,"requirepass");
387 addReplyBulkCString(c
,server
.requirepass
);
390 if (stringmatch(pattern
,"masterauth",0)) {
391 addReplyBulkCString(c
,"masterauth");
392 addReplyBulkCString(c
,server
.masterauth
);
395 if (stringmatch(pattern
,"maxmemory",0)) {
396 ll2string(buf
,sizeof(buf
),server
.maxmemory
);
397 addReplyBulkCString(c
,"maxmemory");
398 addReplyBulkCString(c
,buf
);
401 if (stringmatch(pattern
,"maxmemory-policy",0)) {
404 switch(server
.maxmemory_policy
) {
405 case REDIS_MAXMEMORY_VOLATILE_LRU
: s
= "volatile-lru"; break;
406 case REDIS_MAXMEMORY_VOLATILE_TTL
: s
= "volatile-ttl"; break;
407 case REDIS_MAXMEMORY_VOLATILE_RANDOM
: s
= "volatile-random"; break;
408 case REDIS_MAXMEMORY_ALLKEYS_LRU
: s
= "allkeys-lru"; break;
409 case REDIS_MAXMEMORY_ALLKEYS_RANDOM
: s
= "allkeys-random"; break;
410 default: s
= "unknown"; break; /* too harmless to panic */
412 addReplyBulkCString(c
,"maxmemory-policy");
413 addReplyBulkCString(c
,s
);
416 if (stringmatch(pattern
,"maxmemory-samples",0)) {
417 ll2string(buf
,sizeof(buf
),server
.maxmemory_samples
);
418 addReplyBulkCString(c
,"maxmemory-samples");
419 addReplyBulkCString(c
,buf
);
422 if (stringmatch(pattern
,"timeout",0)) {
423 ll2string(buf
,sizeof(buf
),server
.maxidletime
);
424 addReplyBulkCString(c
,"timeout");
425 addReplyBulkCString(c
,buf
);
428 if (stringmatch(pattern
,"appendonly",0)) {
429 addReplyBulkCString(c
,"appendonly");
430 addReplyBulkCString(c
,server
.appendonly
? "yes" : "no");
433 if (stringmatch(pattern
,"no-appendfsync-on-rewrite",0)) {
434 addReplyBulkCString(c
,"no-appendfsync-on-rewrite");
435 addReplyBulkCString(c
,server
.no_appendfsync_on_rewrite
? "yes" : "no");
438 if (stringmatch(pattern
,"appendfsync",0)) {
441 switch(server
.appendfsync
) {
442 case APPENDFSYNC_NO
: policy
= "no"; break;
443 case APPENDFSYNC_EVERYSEC
: policy
= "everysec"; break;
444 case APPENDFSYNC_ALWAYS
: policy
= "always"; break;
445 default: policy
= "unknown"; break; /* too harmless to panic */
447 addReplyBulkCString(c
,"appendfsync");
448 addReplyBulkCString(c
,policy
);
451 if (stringmatch(pattern
,"save",0)) {
452 sds buf
= sdsempty();
455 for (j
= 0; j
< server
.saveparamslen
; j
++) {
456 buf
= sdscatprintf(buf
,"%ld %d",
457 server
.saveparams
[j
].seconds
,
458 server
.saveparams
[j
].changes
);
459 if (j
!= server
.saveparamslen
-1)
460 buf
= sdscatlen(buf
," ",1);
462 addReplyBulkCString(c
,"save");
463 addReplyBulkCString(c
,buf
);
467 setDeferredMultiBulkLength(c
,replylen
,matches
*2);
470 void configCommand(redisClient
*c
) {
471 if (!strcasecmp(c
->argv
[1]->ptr
,"set")) {
472 if (c
->argc
!= 4) goto badarity
;
474 } else if (!strcasecmp(c
->argv
[1]->ptr
,"get")) {
475 if (c
->argc
!= 3) goto badarity
;
477 } else if (!strcasecmp(c
->argv
[1]->ptr
,"resetstat")) {
478 if (c
->argc
!= 2) goto badarity
;
479 server
.stat_keyspace_hits
= 0;
480 server
.stat_keyspace_misses
= 0;
481 server
.stat_numcommands
= 0;
482 server
.stat_numconnections
= 0;
483 server
.stat_expiredkeys
= 0;
484 addReply(c
,shared
.ok
);
487 "CONFIG subcommand must be one of GET, SET, RESETSTAT");
492 addReplyErrorFormat(c
,"Wrong number of arguments for CONFIG %s",
493 (char*) c
->argv
[1]->ptr
);