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