]> git.saurik.com Git - redis.git/blob - src/config.c
Added a config directive for a Unix socket mask
[redis.git] / src / config.c
1 #include "redis.h"
2
3 /*-----------------------------------------------------------------------------
4 * Config file parsing
5 *----------------------------------------------------------------------------*/
6
7 int yesnotoi(char *s) {
8 if (!strcasecmp(s,"yes")) return 1;
9 else if (!strcasecmp(s,"no")) return 0;
10 else return -1;
11 }
12
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++;
18 }
19
20 void 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 */
28 void 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 = sdssplitargs(line,&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 < 0 || 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]);
74 } else if (!strcasecmp(argv[0],"unixsocket") && argc == 2) {
75 server.unixsocket = zstrdup(argv[1]);
76 } else if (!strcasecmp(argv[0],"unixsocketperm") && argc == 2) {
77 server.unixsocketperm = (mode_t)strtol(argv[1], NULL, 8);
78 if (errno || server.unixsocketperm > 0777) {
79 err = "Invalid socket file permissions"; goto loaderr;
80 }
81 } else if (!strcasecmp(argv[0],"save") && argc == 3) {
82 int seconds = atoi(argv[1]);
83 int changes = atoi(argv[2]);
84 if (seconds < 1 || changes < 0) {
85 err = "Invalid save parameters"; goto loaderr;
86 }
87 appendServerSaveParams(seconds,changes);
88 } else if (!strcasecmp(argv[0],"dir") && argc == 2) {
89 if (chdir(argv[1]) == -1) {
90 redisLog(REDIS_WARNING,"Can't chdir to '%s': %s",
91 argv[1], strerror(errno));
92 exit(1);
93 }
94 } else if (!strcasecmp(argv[0],"loglevel") && argc == 2) {
95 if (!strcasecmp(argv[1],"debug")) server.verbosity = REDIS_DEBUG;
96 else if (!strcasecmp(argv[1],"verbose")) server.verbosity = REDIS_VERBOSE;
97 else if (!strcasecmp(argv[1],"notice")) server.verbosity = REDIS_NOTICE;
98 else if (!strcasecmp(argv[1],"warning")) server.verbosity = REDIS_WARNING;
99 else {
100 err = "Invalid log level. Must be one of debug, notice, warning";
101 goto loaderr;
102 }
103 } else if (!strcasecmp(argv[0],"logfile") && argc == 2) {
104 FILE *logfp;
105
106 server.logfile = zstrdup(argv[1]);
107 if (!strcasecmp(server.logfile,"stdout")) {
108 zfree(server.logfile);
109 server.logfile = NULL;
110 }
111 if (server.logfile) {
112 /* Test if we are able to open the file. The server will not
113 * be able to abort just for this problem later... */
114 logfp = fopen(server.logfile,"a");
115 if (logfp == NULL) {
116 err = sdscatprintf(sdsempty(),
117 "Can't open the log file: %s", strerror(errno));
118 goto loaderr;
119 }
120 fclose(logfp);
121 }
122 } else if (!strcasecmp(argv[0],"syslog-enabled") && argc == 2) {
123 if ((server.syslog_enabled = yesnotoi(argv[1])) == -1) {
124 err = "argument must be 'yes' or 'no'"; goto loaderr;
125 }
126 } else if (!strcasecmp(argv[0],"syslog-ident") && argc == 2) {
127 if (server.syslog_ident) zfree(server.syslog_ident);
128 server.syslog_ident = zstrdup(argv[1]);
129 } else if (!strcasecmp(argv[0],"syslog-facility") && argc == 2) {
130 struct {
131 const char *name;
132 const int value;
133 } validSyslogFacilities[] = {
134 {"user", LOG_USER},
135 {"local0", LOG_LOCAL0},
136 {"local1", LOG_LOCAL1},
137 {"local2", LOG_LOCAL2},
138 {"local3", LOG_LOCAL3},
139 {"local4", LOG_LOCAL4},
140 {"local5", LOG_LOCAL5},
141 {"local6", LOG_LOCAL6},
142 {"local7", LOG_LOCAL7},
143 {NULL, 0}
144 };
145 int i;
146
147 for (i = 0; validSyslogFacilities[i].name; i++) {
148 if (!strcasecmp(validSyslogFacilities[i].name, argv[1])) {
149 server.syslog_facility = validSyslogFacilities[i].value;
150 break;
151 }
152 }
153
154 if (!validSyslogFacilities[i].name) {
155 err = "Invalid log facility. Must be one of USER or between LOCAL0-LOCAL7";
156 goto loaderr;
157 }
158 } else if (!strcasecmp(argv[0],"databases") && argc == 2) {
159 server.dbnum = atoi(argv[1]);
160 if (server.dbnum < 1) {
161 err = "Invalid number of databases"; goto loaderr;
162 }
163 } else if (!strcasecmp(argv[0],"include") && argc == 2) {
164 loadServerConfig(argv[1]);
165 } else if (!strcasecmp(argv[0],"maxclients") && argc == 2) {
166 server.maxclients = atoi(argv[1]);
167 } else if (!strcasecmp(argv[0],"maxmemory") && argc == 2) {
168 server.maxmemory = memtoll(argv[1],NULL);
169 } else if (!strcasecmp(argv[0],"maxmemory-policy") && argc == 2) {
170 if (!strcasecmp(argv[1],"volatile-lru")) {
171 server.maxmemory_policy = REDIS_MAXMEMORY_VOLATILE_LRU;
172 } else if (!strcasecmp(argv[1],"volatile-random")) {
173 server.maxmemory_policy = REDIS_MAXMEMORY_VOLATILE_RANDOM;
174 } else if (!strcasecmp(argv[1],"volatile-ttl")) {
175 server.maxmemory_policy = REDIS_MAXMEMORY_VOLATILE_TTL;
176 } else if (!strcasecmp(argv[1],"allkeys-lru")) {
177 server.maxmemory_policy = REDIS_MAXMEMORY_ALLKEYS_LRU;
178 } else if (!strcasecmp(argv[1],"allkeys-random")) {
179 server.maxmemory_policy = REDIS_MAXMEMORY_ALLKEYS_RANDOM;
180 } else if (!strcasecmp(argv[1],"noeviction")) {
181 server.maxmemory_policy = REDIS_MAXMEMORY_NO_EVICTION;
182 } else {
183 err = "Invalid maxmemory policy";
184 goto loaderr;
185 }
186 } else if (!strcasecmp(argv[0],"maxmemory-samples") && argc == 2) {
187 server.maxmemory_samples = atoi(argv[1]);
188 if (server.maxmemory_samples <= 0) {
189 err = "maxmemory-samples must be 1 or greater";
190 goto loaderr;
191 }
192 } else if (!strcasecmp(argv[0],"slaveof") && argc == 3) {
193 server.masterhost = sdsnew(argv[1]);
194 server.masterport = atoi(argv[2]);
195 server.replstate = REDIS_REPL_CONNECT;
196 } else if (!strcasecmp(argv[0],"masterauth") && argc == 2) {
197 server.masterauth = zstrdup(argv[1]);
198 } else if (!strcasecmp(argv[0],"slave-serve-stale-data") && argc == 2) {
199 if ((server.repl_serve_stale_data = yesnotoi(argv[1])) == -1) {
200 err = "argument must be 'yes' or 'no'"; goto loaderr;
201 }
202 } else if (!strcasecmp(argv[0],"glueoutputbuf")) {
203 redisLog(REDIS_WARNING, "Deprecated configuration directive: \"%s\"", argv[0]);
204 } else if (!strcasecmp(argv[0],"rdbcompression") && argc == 2) {
205 if ((server.rdbcompression = yesnotoi(argv[1])) == -1) {
206 err = "argument must be 'yes' or 'no'"; goto loaderr;
207 }
208 } else if (!strcasecmp(argv[0],"activerehashing") && argc == 2) {
209 if ((server.activerehashing = yesnotoi(argv[1])) == -1) {
210 err = "argument must be 'yes' or 'no'"; goto loaderr;
211 }
212 } else if (!strcasecmp(argv[0],"daemonize") && argc == 2) {
213 if ((server.daemonize = yesnotoi(argv[1])) == -1) {
214 err = "argument must be 'yes' or 'no'"; goto loaderr;
215 }
216 } else if (!strcasecmp(argv[0],"appendonly") && argc == 2) {
217 if ((server.appendonly = yesnotoi(argv[1])) == -1) {
218 err = "argument must be 'yes' or 'no'"; goto loaderr;
219 }
220 } else if (!strcasecmp(argv[0],"appendfilename") && argc == 2) {
221 zfree(server.appendfilename);
222 server.appendfilename = zstrdup(argv[1]);
223 } else if (!strcasecmp(argv[0],"no-appendfsync-on-rewrite")
224 && argc == 2) {
225 if ((server.no_appendfsync_on_rewrite= yesnotoi(argv[1])) == -1) {
226 err = "argument must be 'yes' or 'no'"; goto loaderr;
227 }
228 } else if (!strcasecmp(argv[0],"appendfsync") && argc == 2) {
229 if (!strcasecmp(argv[1],"no")) {
230 server.appendfsync = APPENDFSYNC_NO;
231 } else if (!strcasecmp(argv[1],"always")) {
232 server.appendfsync = APPENDFSYNC_ALWAYS;
233 } else if (!strcasecmp(argv[1],"everysec")) {
234 server.appendfsync = APPENDFSYNC_EVERYSEC;
235 } else {
236 err = "argument must be 'no', 'always' or 'everysec'";
237 goto loaderr;
238 }
239 } else if (!strcasecmp(argv[0],"auto-aof-rewrite-percentage") &&
240 argc == 2)
241 {
242 server.auto_aofrewrite_perc = atoi(argv[1]);
243 if (server.auto_aofrewrite_perc < 0) {
244 err = "Invalid negative percentage for AOF auto rewrite";
245 goto loaderr;
246 }
247 } else if (!strcasecmp(argv[0],"auto-aof-rewrite-min-size") &&
248 argc == 2)
249 {
250 server.auto_aofrewrite_min_size = memtoll(argv[1],NULL);
251 } else if (!strcasecmp(argv[0],"requirepass") && argc == 2) {
252 server.requirepass = zstrdup(argv[1]);
253 } else if (!strcasecmp(argv[0],"pidfile") && argc == 2) {
254 zfree(server.pidfile);
255 server.pidfile = zstrdup(argv[1]);
256 } else if (!strcasecmp(argv[0],"dbfilename") && argc == 2) {
257 zfree(server.dbfilename);
258 server.dbfilename = zstrdup(argv[1]);
259 } else if (!strcasecmp(argv[0],"hash-max-zipmap-entries") && argc == 2) {
260 server.hash_max_zipmap_entries = memtoll(argv[1], NULL);
261 } else if (!strcasecmp(argv[0],"hash-max-zipmap-value") && argc == 2) {
262 server.hash_max_zipmap_value = memtoll(argv[1], NULL);
263 } else if (!strcasecmp(argv[0],"list-max-ziplist-entries") && argc == 2){
264 server.list_max_ziplist_entries = memtoll(argv[1], NULL);
265 } else if (!strcasecmp(argv[0],"list-max-ziplist-value") && argc == 2) {
266 server.list_max_ziplist_value = memtoll(argv[1], NULL);
267 } else if (!strcasecmp(argv[0],"set-max-intset-entries") && argc == 2) {
268 server.set_max_intset_entries = memtoll(argv[1], NULL);
269 } else if (!strcasecmp(argv[0],"zset-max-ziplist-entries") && argc == 2) {
270 server.zset_max_ziplist_entries = memtoll(argv[1], NULL);
271 } else if (!strcasecmp(argv[0],"zset-max-ziplist-value") && argc == 2) {
272 server.zset_max_ziplist_value = memtoll(argv[1], NULL);
273 } else if (!strcasecmp(argv[0],"rename-command") && argc == 3) {
274 struct redisCommand *cmd = lookupCommand(argv[1]);
275 int retval;
276
277 if (!cmd) {
278 err = "No such command in rename-command";
279 goto loaderr;
280 }
281
282 /* If the target command name is the emtpy string we just
283 * remove it from the command table. */
284 retval = dictDelete(server.commands, argv[1]);
285 redisAssert(retval == DICT_OK);
286
287 /* Otherwise we re-add the command under a different name. */
288 if (sdslen(argv[2]) != 0) {
289 sds copy = sdsdup(argv[2]);
290
291 retval = dictAdd(server.commands, copy, cmd);
292 if (retval != DICT_OK) {
293 sdsfree(copy);
294 err = "Target command name already exists"; goto loaderr;
295 }
296 }
297 } else if (!strcasecmp(argv[0],"cluster-enabled") && argc == 2) {
298 if ((server.cluster_enabled = yesnotoi(argv[1])) == -1) {
299 err = "argument must be 'yes' or 'no'"; goto loaderr;
300 }
301 } else if (!strcasecmp(argv[0],"cluster-config-file") && argc == 2) {
302 zfree(server.cluster.configfile);
303 server.cluster.configfile = zstrdup(argv[1]);
304 } else if (!strcasecmp(argv[0],"lua-time-limit") && argc == 2) {
305 server.lua_time_limit = strtoll(argv[1],NULL,10);
306 } else if (!strcasecmp(argv[0],"slowlog-log-slower-than") &&
307 argc == 2)
308 {
309 server.slowlog_log_slower_than = strtoll(argv[1],NULL,10);
310 } else if (!strcasecmp(argv[0],"slowlog-max-len") && argc == 2) {
311 server.slowlog_max_len = strtoll(argv[1],NULL,10);
312 } else {
313 err = "Bad directive or wrong number of arguments"; goto loaderr;
314 }
315 for (j = 0; j < argc; j++)
316 sdsfree(argv[j]);
317 zfree(argv);
318 sdsfree(line);
319 }
320 if (fp != stdin) fclose(fp);
321 return;
322
323 loaderr:
324 fprintf(stderr, "\n*** FATAL CONFIG FILE ERROR ***\n");
325 fprintf(stderr, "Reading the configuration file, at line %d\n", linenum);
326 fprintf(stderr, ">>> '%s'\n", line);
327 fprintf(stderr, "%s\n", err);
328 exit(1);
329 }
330
331 /*-----------------------------------------------------------------------------
332 * CONFIG command for remote configuration
333 *----------------------------------------------------------------------------*/
334
335 void configSetCommand(redisClient *c) {
336 robj *o;
337 long long ll;
338 redisAssertWithInfo(c,c->argv[2],c->argv[2]->encoding == REDIS_ENCODING_RAW);
339 redisAssertWithInfo(c,c->argv[2],c->argv[3]->encoding == REDIS_ENCODING_RAW);
340 o = c->argv[3];
341
342 if (!strcasecmp(c->argv[2]->ptr,"dbfilename")) {
343 zfree(server.dbfilename);
344 server.dbfilename = zstrdup(o->ptr);
345 } else if (!strcasecmp(c->argv[2]->ptr,"requirepass")) {
346 zfree(server.requirepass);
347 server.requirepass = zstrdup(o->ptr);
348 } else if (!strcasecmp(c->argv[2]->ptr,"masterauth")) {
349 zfree(server.masterauth);
350 server.masterauth = zstrdup(o->ptr);
351 } else if (!strcasecmp(c->argv[2]->ptr,"maxmemory")) {
352 if (getLongLongFromObject(o,&ll) == REDIS_ERR ||
353 ll < 0) goto badfmt;
354 server.maxmemory = ll;
355 if (server.maxmemory) freeMemoryIfNeeded();
356 } else if (!strcasecmp(c->argv[2]->ptr,"maxmemory-policy")) {
357 if (!strcasecmp(o->ptr,"volatile-lru")) {
358 server.maxmemory_policy = REDIS_MAXMEMORY_VOLATILE_LRU;
359 } else if (!strcasecmp(o->ptr,"volatile-random")) {
360 server.maxmemory_policy = REDIS_MAXMEMORY_VOLATILE_RANDOM;
361 } else if (!strcasecmp(o->ptr,"volatile-ttl")) {
362 server.maxmemory_policy = REDIS_MAXMEMORY_VOLATILE_TTL;
363 } else if (!strcasecmp(o->ptr,"allkeys-lru")) {
364 server.maxmemory_policy = REDIS_MAXMEMORY_ALLKEYS_LRU;
365 } else if (!strcasecmp(o->ptr,"allkeys-random")) {
366 server.maxmemory_policy = REDIS_MAXMEMORY_ALLKEYS_RANDOM;
367 } else if (!strcasecmp(o->ptr,"noeviction")) {
368 server.maxmemory_policy = REDIS_MAXMEMORY_NO_EVICTION;
369 } else {
370 goto badfmt;
371 }
372 } else if (!strcasecmp(c->argv[2]->ptr,"maxmemory-samples")) {
373 if (getLongLongFromObject(o,&ll) == REDIS_ERR ||
374 ll <= 0) goto badfmt;
375 server.maxmemory_samples = ll;
376 } else if (!strcasecmp(c->argv[2]->ptr,"timeout")) {
377 if (getLongLongFromObject(o,&ll) == REDIS_ERR ||
378 ll < 0 || ll > LONG_MAX) goto badfmt;
379 server.maxidletime = ll;
380 } else if (!strcasecmp(c->argv[2]->ptr,"appendfsync")) {
381 if (!strcasecmp(o->ptr,"no")) {
382 server.appendfsync = APPENDFSYNC_NO;
383 } else if (!strcasecmp(o->ptr,"everysec")) {
384 server.appendfsync = APPENDFSYNC_EVERYSEC;
385 } else if (!strcasecmp(o->ptr,"always")) {
386 server.appendfsync = APPENDFSYNC_ALWAYS;
387 } else {
388 goto badfmt;
389 }
390 } else if (!strcasecmp(c->argv[2]->ptr,"no-appendfsync-on-rewrite")) {
391 int yn = yesnotoi(o->ptr);
392
393 if (yn == -1) goto badfmt;
394 server.no_appendfsync_on_rewrite = yn;
395 } else if (!strcasecmp(c->argv[2]->ptr,"appendonly")) {
396 int old = server.appendonly;
397 int new = yesnotoi(o->ptr);
398
399 if (new == -1) goto badfmt;
400 if (old != new) {
401 if (new == 0) {
402 stopAppendOnly();
403 } else {
404 if (startAppendOnly() == REDIS_ERR) {
405 addReplyError(c,
406 "Unable to turn on AOF. Check server logs.");
407 return;
408 }
409 }
410 }
411 } else if (!strcasecmp(c->argv[2]->ptr,"auto-aof-rewrite-percentage")) {
412 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
413 server.auto_aofrewrite_perc = ll;
414 } else if (!strcasecmp(c->argv[2]->ptr,"auto-aof-rewrite-min-size")) {
415 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
416 server.auto_aofrewrite_min_size = ll;
417 } else if (!strcasecmp(c->argv[2]->ptr,"save")) {
418 int vlen, j;
419 sds *v = sdssplitlen(o->ptr,sdslen(o->ptr)," ",1,&vlen);
420
421 /* Perform sanity check before setting the new config:
422 * - Even number of args
423 * - Seconds >= 1, changes >= 0 */
424 if (vlen & 1) {
425 sdsfreesplitres(v,vlen);
426 goto badfmt;
427 }
428 for (j = 0; j < vlen; j++) {
429 char *eptr;
430 long val;
431
432 val = strtoll(v[j], &eptr, 10);
433 if (eptr[0] != '\0' ||
434 ((j & 1) == 0 && val < 1) ||
435 ((j & 1) == 1 && val < 0)) {
436 sdsfreesplitres(v,vlen);
437 goto badfmt;
438 }
439 }
440 /* Finally set the new config */
441 resetServerSaveParams();
442 for (j = 0; j < vlen; j += 2) {
443 time_t seconds;
444 int changes;
445
446 seconds = strtoll(v[j],NULL,10);
447 changes = strtoll(v[j+1],NULL,10);
448 appendServerSaveParams(seconds, changes);
449 }
450 sdsfreesplitres(v,vlen);
451 } else if (!strcasecmp(c->argv[2]->ptr,"slave-serve-stale-data")) {
452 int yn = yesnotoi(o->ptr);
453
454 if (yn == -1) goto badfmt;
455 server.repl_serve_stale_data = yn;
456 } else if (!strcasecmp(c->argv[2]->ptr,"dir")) {
457 if (chdir((char*)o->ptr) == -1) {
458 addReplyErrorFormat(c,"Changing directory: %s", strerror(errno));
459 return;
460 }
461 } else if (!strcasecmp(c->argv[2]->ptr,"hash-max-zipmap-entries")) {
462 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
463 server.hash_max_zipmap_entries = ll;
464 } else if (!strcasecmp(c->argv[2]->ptr,"hash-max-zipmap-value")) {
465 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
466 server.hash_max_zipmap_value = ll;
467 } else if (!strcasecmp(c->argv[2]->ptr,"list-max-ziplist-entries")) {
468 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
469 server.list_max_ziplist_entries = ll;
470 } else if (!strcasecmp(c->argv[2]->ptr,"list-max-ziplist-value")) {
471 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
472 server.list_max_ziplist_value = ll;
473 } else if (!strcasecmp(c->argv[2]->ptr,"set-max-intset-entries")) {
474 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
475 server.set_max_intset_entries = ll;
476 } else if (!strcasecmp(c->argv[2]->ptr,"zset-max-ziplist-entries")) {
477 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
478 server.zset_max_ziplist_entries = ll;
479 } else if (!strcasecmp(c->argv[2]->ptr,"zset-max-ziplist-value")) {
480 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
481 server.zset_max_ziplist_value = ll;
482 } else if (!strcasecmp(c->argv[2]->ptr,"lua-time-limit")) {
483 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
484 server.lua_time_limit = ll;
485 } else if (!strcasecmp(c->argv[2]->ptr,"slowlog-log-slower-than")) {
486 if (getLongLongFromObject(o,&ll) == REDIS_ERR) goto badfmt;
487 server.slowlog_log_slower_than = ll;
488 } else if (!strcasecmp(c->argv[2]->ptr,"slowlog-max-len")) {
489 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
490 server.slowlog_max_len = (unsigned)ll;
491 } else if (!strcasecmp(c->argv[2]->ptr,"loglevel")) {
492 if (!strcasecmp(o->ptr,"warning")) {
493 server.verbosity = REDIS_WARNING;
494 } else if (!strcasecmp(o->ptr,"notice")) {
495 server.verbosity = REDIS_NOTICE;
496 } else if (!strcasecmp(o->ptr,"verbose")) {
497 server.verbosity = REDIS_VERBOSE;
498 } else if (!strcasecmp(o->ptr,"debug")) {
499 server.verbosity = REDIS_DEBUG;
500 } else {
501 goto badfmt;
502 }
503 } else {
504 addReplyErrorFormat(c,"Unsupported CONFIG parameter: %s",
505 (char*)c->argv[2]->ptr);
506 return;
507 }
508 addReply(c,shared.ok);
509 return;
510
511 badfmt: /* Bad format errors */
512 addReplyErrorFormat(c,"Invalid argument '%s' for CONFIG SET '%s'",
513 (char*)o->ptr,
514 (char*)c->argv[2]->ptr);
515 }
516
517 void configGetCommand(redisClient *c) {
518 robj *o = c->argv[2];
519 void *replylen = addDeferredMultiBulkLength(c);
520 char *pattern = o->ptr;
521 char buf[128];
522 int matches = 0;
523 redisAssertWithInfo(c,o,o->encoding == REDIS_ENCODING_RAW);
524
525 if (stringmatch(pattern,"dir",0)) {
526 char buf[1024];
527
528 if (getcwd(buf,sizeof(buf)) == NULL)
529 buf[0] = '\0';
530
531 addReplyBulkCString(c,"dir");
532 addReplyBulkCString(c,buf);
533 matches++;
534 }
535 if (stringmatch(pattern,"dbfilename",0)) {
536 addReplyBulkCString(c,"dbfilename");
537 addReplyBulkCString(c,server.dbfilename);
538 matches++;
539 }
540 if (stringmatch(pattern,"requirepass",0)) {
541 addReplyBulkCString(c,"requirepass");
542 addReplyBulkCString(c,server.requirepass);
543 matches++;
544 }
545 if (stringmatch(pattern,"masterauth",0)) {
546 addReplyBulkCString(c,"masterauth");
547 addReplyBulkCString(c,server.masterauth);
548 matches++;
549 }
550 if (stringmatch(pattern,"maxmemory",0)) {
551 ll2string(buf,sizeof(buf),server.maxmemory);
552 addReplyBulkCString(c,"maxmemory");
553 addReplyBulkCString(c,buf);
554 matches++;
555 }
556 if (stringmatch(pattern,"maxmemory-policy",0)) {
557 char *s;
558
559 switch(server.maxmemory_policy) {
560 case REDIS_MAXMEMORY_VOLATILE_LRU: s = "volatile-lru"; break;
561 case REDIS_MAXMEMORY_VOLATILE_TTL: s = "volatile-ttl"; break;
562 case REDIS_MAXMEMORY_VOLATILE_RANDOM: s = "volatile-random"; break;
563 case REDIS_MAXMEMORY_ALLKEYS_LRU: s = "allkeys-lru"; break;
564 case REDIS_MAXMEMORY_ALLKEYS_RANDOM: s = "allkeys-random"; break;
565 case REDIS_MAXMEMORY_NO_EVICTION: s = "noeviction"; break;
566 default: s = "unknown"; break; /* too harmless to panic */
567 }
568 addReplyBulkCString(c,"maxmemory-policy");
569 addReplyBulkCString(c,s);
570 matches++;
571 }
572 if (stringmatch(pattern,"maxmemory-samples",0)) {
573 ll2string(buf,sizeof(buf),server.maxmemory_samples);
574 addReplyBulkCString(c,"maxmemory-samples");
575 addReplyBulkCString(c,buf);
576 matches++;
577 }
578 if (stringmatch(pattern,"timeout",0)) {
579 ll2string(buf,sizeof(buf),server.maxidletime);
580 addReplyBulkCString(c,"timeout");
581 addReplyBulkCString(c,buf);
582 matches++;
583 }
584 if (stringmatch(pattern,"appendonly",0)) {
585 addReplyBulkCString(c,"appendonly");
586 addReplyBulkCString(c,server.appendonly ? "yes" : "no");
587 matches++;
588 }
589 if (stringmatch(pattern,"no-appendfsync-on-rewrite",0)) {
590 addReplyBulkCString(c,"no-appendfsync-on-rewrite");
591 addReplyBulkCString(c,server.no_appendfsync_on_rewrite ? "yes" : "no");
592 matches++;
593 }
594 if (stringmatch(pattern,"appendfsync",0)) {
595 char *policy;
596
597 switch(server.appendfsync) {
598 case APPENDFSYNC_NO: policy = "no"; break;
599 case APPENDFSYNC_EVERYSEC: policy = "everysec"; break;
600 case APPENDFSYNC_ALWAYS: policy = "always"; break;
601 default: policy = "unknown"; break; /* too harmless to panic */
602 }
603 addReplyBulkCString(c,"appendfsync");
604 addReplyBulkCString(c,policy);
605 matches++;
606 }
607 if (stringmatch(pattern,"save",0)) {
608 sds buf = sdsempty();
609 int j;
610
611 for (j = 0; j < server.saveparamslen; j++) {
612 buf = sdscatprintf(buf,"%ld %d",
613 server.saveparams[j].seconds,
614 server.saveparams[j].changes);
615 if (j != server.saveparamslen-1)
616 buf = sdscatlen(buf," ",1);
617 }
618 addReplyBulkCString(c,"save");
619 addReplyBulkCString(c,buf);
620 sdsfree(buf);
621 matches++;
622 }
623 if (stringmatch(pattern,"auto-aof-rewrite-percentage",0)) {
624 addReplyBulkCString(c,"auto-aof-rewrite-percentage");
625 addReplyBulkLongLong(c,server.auto_aofrewrite_perc);
626 matches++;
627 }
628 if (stringmatch(pattern,"auto-aof-rewrite-min-size",0)) {
629 addReplyBulkCString(c,"auto-aof-rewrite-min-size");
630 addReplyBulkLongLong(c,server.auto_aofrewrite_min_size);
631 matches++;
632 }
633 if (stringmatch(pattern,"slave-serve-stale-data",0)) {
634 addReplyBulkCString(c,"slave-serve-stale-data");
635 addReplyBulkCString(c,server.repl_serve_stale_data ? "yes" : "no");
636 matches++;
637 }
638 if (stringmatch(pattern,"hash-max-zipmap-entries",0)) {
639 addReplyBulkCString(c,"hash-max-zipmap-entries");
640 addReplyBulkLongLong(c,server.hash_max_zipmap_entries);
641 matches++;
642 }
643 if (stringmatch(pattern,"hash-max-zipmap-value",0)) {
644 addReplyBulkCString(c,"hash-max-zipmap-value");
645 addReplyBulkLongLong(c,server.hash_max_zipmap_value);
646 matches++;
647 }
648 if (stringmatch(pattern,"list-max-ziplist-entries",0)) {
649 addReplyBulkCString(c,"list-max-ziplist-entries");
650 addReplyBulkLongLong(c,server.list_max_ziplist_entries);
651 matches++;
652 }
653 if (stringmatch(pattern,"list-max-ziplist-value",0)) {
654 addReplyBulkCString(c,"list-max-ziplist-value");
655 addReplyBulkLongLong(c,server.list_max_ziplist_value);
656 matches++;
657 }
658 if (stringmatch(pattern,"set-max-intset-entries",0)) {
659 addReplyBulkCString(c,"set-max-intset-entries");
660 addReplyBulkLongLong(c,server.set_max_intset_entries);
661 matches++;
662 }
663 if (stringmatch(pattern,"zset-max-ziplist-entries",0)) {
664 addReplyBulkCString(c,"zset-max-ziplist-entries");
665 addReplyBulkLongLong(c,server.zset_max_ziplist_entries);
666 matches++;
667 }
668 if (stringmatch(pattern,"zset-max-ziplist-value",0)) {
669 addReplyBulkCString(c,"zset-max-ziplist-value");
670 addReplyBulkLongLong(c,server.zset_max_ziplist_value);
671 matches++;
672 }
673 if (stringmatch(pattern,"lua-time-limit",0)) {
674 addReplyBulkCString(c,"lua-time-limit");
675 addReplyBulkLongLong(c,server.lua_time_limit);
676 matches++;
677 }
678 if (stringmatch(pattern,"slowlog-log-slower-than",0)) {
679 addReplyBulkCString(c,"slowlog-log-slower-than");
680 addReplyBulkLongLong(c,server.slowlog_log_slower_than);
681 matches++;
682 }
683 if (stringmatch(pattern,"slowlog-max-len",0)) {
684 addReplyBulkCString(c,"slowlog-max-len");
685 addReplyBulkLongLong(c,server.slowlog_max_len);
686 matches++;
687 }
688 if (stringmatch(pattern,"loglevel",0)) {
689 char *s;
690
691 switch(server.verbosity) {
692 case REDIS_WARNING: s = "warning"; break;
693 case REDIS_VERBOSE: s = "verbose"; break;
694 case REDIS_NOTICE: s = "notice"; break;
695 case REDIS_DEBUG: s = "debug"; break;
696 default: s = "unknown"; break; /* too harmless to panic */
697 }
698 addReplyBulkCString(c,"loglevel");
699 addReplyBulkCString(c,s);
700 matches++;
701 }
702 setDeferredMultiBulkLength(c,replylen,matches*2);
703 }
704
705 void configCommand(redisClient *c) {
706 if (!strcasecmp(c->argv[1]->ptr,"set")) {
707 if (c->argc != 4) goto badarity;
708 configSetCommand(c);
709 } else if (!strcasecmp(c->argv[1]->ptr,"get")) {
710 if (c->argc != 3) goto badarity;
711 configGetCommand(c);
712 } else if (!strcasecmp(c->argv[1]->ptr,"resetstat")) {
713 if (c->argc != 2) goto badarity;
714 server.stat_keyspace_hits = 0;
715 server.stat_keyspace_misses = 0;
716 server.stat_numcommands = 0;
717 server.stat_numconnections = 0;
718 server.stat_expiredkeys = 0;
719 resetCommandTableStats();
720 addReply(c,shared.ok);
721 } else {
722 addReplyError(c,
723 "CONFIG subcommand must be one of GET, SET, RESETSTAT");
724 }
725 return;
726
727 badarity:
728 addReplyErrorFormat(c,"Wrong number of arguments for CONFIG %s",
729 (char*) c->argv[1]->ptr);
730 }