]> git.saurik.com Git - redis.git/blame - src/config.c
Merge remote branch 'pietern/unixsocket'
[redis.git] / src / config.c
CommitLineData
e2641e09 1#include "redis.h"
2
3/*-----------------------------------------------------------------------------
4 * Config file parsing
5 *----------------------------------------------------------------------------*/
6
7int yesnotoi(char *s) {
8 if (!strcasecmp(s,"yes")) return 1;
9 else if (!strcasecmp(s,"no")) return 0;
10 else return -1;
11}
12
13void 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++;
18}
19
20void resetServerSaveParams() {
21 zfree(server.saveparams);
22 server.saveparams = NULL;
23 server.saveparamslen = 0;
24}
25
26/* I agree, this is a very rudimental way to load a configuration...
27 will improve later if the config gets more complex */
28void loadServerConfig(char *filename) {
29 FILE *fp;
30 char buf[REDIS_CONFIGLINE_MAX+1], *err = NULL;
31 int linenum = 0;
32 sds line = NULL;
33
34 if (filename[0] == '-' && filename[1] == '\0')
35 fp = stdin;
36 else {
37 if ((fp = fopen(filename,"r")) == NULL) {
38 redisLog(REDIS_WARNING, "Fatal error, can't open config file '%s'", filename);
39 exit(1);
40 }
41 }
42
43 while(fgets(buf,REDIS_CONFIGLINE_MAX+1,fp) != NULL) {
44 sds *argv;
45 int argc, j;
46
47 linenum++;
48 line = sdsnew(buf);
49 line = sdstrim(line," \t\r\n");
50
51 /* Skip comments and blank lines*/
52 if (line[0] == '#' || line[0] == '\0') {
53 sdsfree(line);
54 continue;
55 }
56
57 /* Split into arguments */
58 argv = sdssplitlen(line,sdslen(line)," ",1,&argc);
59 sdstolower(argv[0]);
60
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;
66 }
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;
71 }
72 } else if (!strcasecmp(argv[0],"bind") && argc == 2) {
73 server.bindaddr = zstrdup(argv[1]);
5d10923f
PN
74 } else if (!strcasecmp(argv[0],"unixsocket") && argc == 2) {
75 server.unixsocket = zstrdup(argv[1]);
e2641e09 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;
81 }
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));
87 exit(1);
88 }
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;
94 else {
95 err = "Invalid log level. Must be one of debug, notice, warning";
96 goto loaderr;
97 }
98 } else if (!strcasecmp(argv[0],"logfile") && argc == 2) {
99 FILE *logfp;
100
101 server.logfile = zstrdup(argv[1]);
102 if (!strcasecmp(server.logfile,"stdout")) {
103 zfree(server.logfile);
104 server.logfile = NULL;
105 }
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");
110 if (logfp == NULL) {
111 err = sdscatprintf(sdsempty(),
112 "Can't open the log file: %s", strerror(errno));
113 goto loaderr;
114 }
115 fclose(logfp);
116 }
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;
121 }
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);
165346ca 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;
139 } else {
140 err = "Invalid maxmemory policy";
141 goto loaderr;
142 }
a3687929 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";
147 goto loaderr;
148 }
e2641e09 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;
158 }
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;
162 }
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;
166 }
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;
170 }
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;
174 }
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")
179 && argc == 2) {
180 if ((server.no_appendfsync_on_rewrite= yesnotoi(argv[1])) == -1) {
181 err = "argument must be 'yes' or 'no'"; goto loaderr;
182 }
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;
190 } else {
191 err = "argument must be 'no', 'always' or 'everysec'";
192 goto loaderr;
193 }
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;
205 }
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);
96ffb2fe
PN
225 } else if (!strcasecmp(argv[0],"set-max-intset-entries") && argc == 2){
226 server.set_max_intset_entries = memtoll(argv[1], NULL);
e2641e09 227 } else {
228 err = "Bad directive or wrong number of arguments"; goto loaderr;
229 }
230 for (j = 0; j < argc; j++)
231 sdsfree(argv[j]);
232 zfree(argv);
233 sdsfree(line);
234 }
235 if (fp != stdin) fclose(fp);
236 return;
237
238loaderr:
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);
243 exit(1);
244}
245
246/*-----------------------------------------------------------------------------
247 * CONFIG command for remote configuration
248 *----------------------------------------------------------------------------*/
249
250void configSetCommand(redisClient *c) {
a375b077 251 robj *o;
e2641e09 252 long long ll;
a375b077
PN
253 redisAssert(c->argv[2]->encoding == REDIS_ENCODING_RAW);
254 redisAssert(c->argv[3]->encoding == REDIS_ENCODING_RAW);
255 o = c->argv[3];
e2641e09 256
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 ||
268 ll < 0) goto badfmt;
269 server.maxmemory = ll;
b4f2e412 270 if (server.maxmemory) freeMemoryIfNeeded();
165346ca 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;
282 } else {
283 goto badfmt;
284 }
a3687929 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;
e2641e09 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;
300 } else {
301 goto badfmt;
302 }
303 } else if (!strcasecmp(c->argv[2]->ptr,"no-appendfsync-on-rewrite")) {
304 int yn = yesnotoi(o->ptr);
305
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);
311
312 if (new == -1) goto badfmt;
313 if (old != new) {
314 if (new == 0) {
315 stopAppendOnly();
316 } else {
317 if (startAppendOnly() == REDIS_ERR) {
3ab20376
PN
318 addReplyError(c,
319 "Unable to turn on AOF. Check server logs.");
e2641e09 320 return;
321 }
322 }
323 }
324 } else if (!strcasecmp(c->argv[2]->ptr,"save")) {
325 int vlen, j;
326 sds *v = sdssplitlen(o->ptr,sdslen(o->ptr)," ",1,&vlen);
327
328 /* Perform sanity check before setting the new config:
329 * - Even number of args
330 * - Seconds >= 1, changes >= 0 */
331 if (vlen & 1) {
332 sdsfreesplitres(v,vlen);
333 goto badfmt;
334 }
335 for (j = 0; j < vlen; j++) {
336 char *eptr;
337 long val;
338
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);
344 goto badfmt;
345 }
346 }
347 /* Finally set the new config */
348 resetServerSaveParams();
349 for (j = 0; j < vlen; j += 2) {
350 time_t seconds;
351 int changes;
352
353 seconds = strtoll(v[j],NULL,10);
354 changes = strtoll(v[j+1],NULL,10);
355 appendServerSaveParams(seconds, changes);
356 }
357 sdsfreesplitres(v,vlen);
358 } else {
3ab20376
PN
359 addReplyErrorFormat(c,"Unsupported CONFIG parameter: %s",
360 (char*)c->argv[2]->ptr);
e2641e09 361 return;
362 }
e2641e09 363 addReply(c,shared.ok);
364 return;
365
366badfmt: /* Bad format errors */
3ab20376 367 addReplyErrorFormat(c,"Invalid argument '%s' for CONFIG SET '%s'",
e2641e09 368 (char*)o->ptr,
3ab20376 369 (char*)c->argv[2]->ptr);
e2641e09 370}
371
372void configGetCommand(redisClient *c) {
a375b077 373 robj *o = c->argv[2];
b301c1fc 374 void *replylen = addDeferredMultiBulkLength(c);
e2641e09 375 char *pattern = o->ptr;
a3687929 376 char buf[128];
e2641e09 377 int matches = 0;
a375b077 378 redisAssert(o->encoding == REDIS_ENCODING_RAW);
e2641e09 379
e2641e09 380 if (stringmatch(pattern,"dbfilename",0)) {
381 addReplyBulkCString(c,"dbfilename");
382 addReplyBulkCString(c,server.dbfilename);
383 matches++;
384 }
385 if (stringmatch(pattern,"requirepass",0)) {
386 addReplyBulkCString(c,"requirepass");
387 addReplyBulkCString(c,server.requirepass);
388 matches++;
389 }
390 if (stringmatch(pattern,"masterauth",0)) {
391 addReplyBulkCString(c,"masterauth");
392 addReplyBulkCString(c,server.masterauth);
393 matches++;
394 }
395 if (stringmatch(pattern,"maxmemory",0)) {
a3687929 396 ll2string(buf,sizeof(buf),server.maxmemory);
e2641e09 397 addReplyBulkCString(c,"maxmemory");
398 addReplyBulkCString(c,buf);
399 matches++;
400 }
165346ca 401 if (stringmatch(pattern,"maxmemory-policy",0)) {
402 char *s;
403
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 */
411 }
412 addReplyBulkCString(c,"maxmemory-policy");
413 addReplyBulkCString(c,s);
414 matches++;
415 }
a3687929 416 if (stringmatch(pattern,"maxmemory-samples",0)) {
417 ll2string(buf,sizeof(buf),server.maxmemory_samples);
418 addReplyBulkCString(c,"maxmemory-samples");
419 addReplyBulkCString(c,buf);
420 matches++;
421 }
e2641e09 422 if (stringmatch(pattern,"timeout",0)) {
a3687929 423 ll2string(buf,sizeof(buf),server.maxidletime);
e2641e09 424 addReplyBulkCString(c,"timeout");
425 addReplyBulkCString(c,buf);
426 matches++;
427 }
428 if (stringmatch(pattern,"appendonly",0)) {
429 addReplyBulkCString(c,"appendonly");
430 addReplyBulkCString(c,server.appendonly ? "yes" : "no");
431 matches++;
432 }
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");
436 matches++;
437 }
438 if (stringmatch(pattern,"appendfsync",0)) {
439 char *policy;
440
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 */
446 }
447 addReplyBulkCString(c,"appendfsync");
448 addReplyBulkCString(c,policy);
449 matches++;
450 }
451 if (stringmatch(pattern,"save",0)) {
452 sds buf = sdsempty();
453 int j;
454
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);
461 }
462 addReplyBulkCString(c,"save");
463 addReplyBulkCString(c,buf);
464 sdsfree(buf);
465 matches++;
466 }
b301c1fc 467 setDeferredMultiBulkLength(c,replylen,matches*2);
e2641e09 468}
469
470void configCommand(redisClient *c) {
471 if (!strcasecmp(c->argv[1]->ptr,"set")) {
472 if (c->argc != 4) goto badarity;
473 configSetCommand(c);
474 } else if (!strcasecmp(c->argv[1]->ptr,"get")) {
475 if (c->argc != 3) goto badarity;
476 configGetCommand(c);
477 } else if (!strcasecmp(c->argv[1]->ptr,"resetstat")) {
478 if (c->argc != 2) goto badarity;
9f8ded8c 479 server.stat_keyspace_hits = 0;
480 server.stat_keyspace_misses = 0;
e2641e09 481 server.stat_numcommands = 0;
482 server.stat_numconnections = 0;
483 server.stat_expiredkeys = 0;
e2641e09 484 addReply(c,shared.ok);
485 } else {
3ab20376
PN
486 addReplyError(c,
487 "CONFIG subcommand must be one of GET, SET, RESETSTAT");
e2641e09 488 }
489 return;
490
491badarity:
3ab20376
PN
492 addReplyErrorFormat(c,"Wrong number of arguments for CONFIG %s",
493 (char*) c->argv[1]->ptr);
e2641e09 494}