]> git.saurik.com Git - redis.git/blob - src/config.c
fix typo of redis.conf
[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],"rdbcompression") && argc == 2) {
206 if ((server.rdb_compression = yesnotoi(argv[1])) == -1) {
207 err = "argument must be 'yes' or 'no'"; goto loaderr;
208 }
209 } else if (!strcasecmp(argv[0],"activerehashing") && argc == 2) {
210 if ((server.activerehashing = yesnotoi(argv[1])) == -1) {
211 err = "argument must be 'yes' or 'no'"; goto loaderr;
212 }
213 } else if (!strcasecmp(argv[0],"daemonize") && argc == 2) {
214 if ((server.daemonize = yesnotoi(argv[1])) == -1) {
215 err = "argument must be 'yes' or 'no'"; goto loaderr;
216 }
217 } else if (!strcasecmp(argv[0],"appendonly") && argc == 2) {
218 int yes;
219
220 if ((yes = yesnotoi(argv[1])) == -1) {
221 err = "argument must be 'yes' or 'no'"; goto loaderr;
222 }
223 server.aof_state = yes ? REDIS_AOF_ON : REDIS_AOF_OFF;
224 } else if (!strcasecmp(argv[0],"appendfilename") && argc == 2) {
225 zfree(server.aof_filename);
226 server.aof_filename = zstrdup(argv[1]);
227 } else if (!strcasecmp(argv[0],"no-appendfsync-on-rewrite")
228 && argc == 2) {
229 if ((server.aof_no_fsync_on_rewrite= yesnotoi(argv[1])) == -1) {
230 err = "argument must be 'yes' or 'no'"; goto loaderr;
231 }
232 } else if (!strcasecmp(argv[0],"appendfsync") && argc == 2) {
233 if (!strcasecmp(argv[1],"no")) {
234 server.aof_fsync = AOF_FSYNC_NO;
235 } else if (!strcasecmp(argv[1],"always")) {
236 server.aof_fsync = AOF_FSYNC_ALWAYS;
237 } else if (!strcasecmp(argv[1],"everysec")) {
238 server.aof_fsync = AOF_FSYNC_EVERYSEC;
239 } else {
240 err = "argument must be 'no', 'always' or 'everysec'";
241 goto loaderr;
242 }
243 } else if (!strcasecmp(argv[0],"auto-aof-rewrite-percentage") &&
244 argc == 2)
245 {
246 server.aof_rewrite_perc = atoi(argv[1]);
247 if (server.aof_rewrite_perc < 0) {
248 err = "Invalid negative percentage for AOF auto rewrite";
249 goto loaderr;
250 }
251 } else if (!strcasecmp(argv[0],"auto-aof-rewrite-min-size") &&
252 argc == 2)
253 {
254 server.aof_rewrite_min_size = memtoll(argv[1],NULL);
255 } else if (!strcasecmp(argv[0],"requirepass") && argc == 2) {
256 server.requirepass = zstrdup(argv[1]);
257 } else if (!strcasecmp(argv[0],"pidfile") && argc == 2) {
258 zfree(server.pidfile);
259 server.pidfile = zstrdup(argv[1]);
260 } else if (!strcasecmp(argv[0],"dbfilename") && argc == 2) {
261 zfree(server.rdb_filename);
262 server.rdb_filename = zstrdup(argv[1]);
263 } else if (!strcasecmp(argv[0],"hash-max-ziplist-entries") && argc == 2) {
264 server.hash_max_ziplist_entries = memtoll(argv[1], NULL);
265 } else if (!strcasecmp(argv[0],"hash-max-ziplist-value") && argc == 2) {
266 server.hash_max_ziplist_value = memtoll(argv[1], NULL);
267 } else if (!strcasecmp(argv[0],"list-max-ziplist-entries") && argc == 2){
268 server.list_max_ziplist_entries = memtoll(argv[1], NULL);
269 } else if (!strcasecmp(argv[0],"list-max-ziplist-value") && argc == 2) {
270 server.list_max_ziplist_value = memtoll(argv[1], NULL);
271 } else if (!strcasecmp(argv[0],"set-max-intset-entries") && argc == 2) {
272 server.set_max_intset_entries = memtoll(argv[1], NULL);
273 } else if (!strcasecmp(argv[0],"zset-max-ziplist-entries") && argc == 2) {
274 server.zset_max_ziplist_entries = memtoll(argv[1], NULL);
275 } else if (!strcasecmp(argv[0],"zset-max-ziplist-value") && argc == 2) {
276 server.zset_max_ziplist_value = memtoll(argv[1], NULL);
277 } else if (!strcasecmp(argv[0],"rename-command") && argc == 3) {
278 struct redisCommand *cmd = lookupCommand(argv[1]);
279 int retval;
280
281 if (!cmd) {
282 err = "No such command in rename-command";
283 goto loaderr;
284 }
285
286 /* If the target command name is the emtpy string we just
287 * remove it from the command table. */
288 retval = dictDelete(server.commands, argv[1]);
289 redisAssert(retval == DICT_OK);
290
291 /* Otherwise we re-add the command under a different name. */
292 if (sdslen(argv[2]) != 0) {
293 sds copy = sdsdup(argv[2]);
294
295 retval = dictAdd(server.commands, copy, cmd);
296 if (retval != DICT_OK) {
297 sdsfree(copy);
298 err = "Target command name already exists"; goto loaderr;
299 }
300 }
301 } else if (!strcasecmp(argv[0],"lua-time-limit") && argc == 2) {
302 server.lua_time_limit = strtoll(argv[1],NULL,10);
303 } else if (!strcasecmp(argv[0],"slowlog-log-slower-than") &&
304 argc == 2)
305 {
306 server.slowlog_log_slower_than = strtoll(argv[1],NULL,10);
307 } else if (!strcasecmp(argv[0],"slowlog-max-len") && argc == 2) {
308 server.slowlog_max_len = strtoll(argv[1],NULL,10);
309 } else if (!strcasecmp(argv[0],"client-output-buffer-limit") &&
310 argc == 5)
311 {
312 int class = getClientLimitClassByName(argv[1]);
313 unsigned long long hard, soft;
314 int soft_seconds;
315
316 if (class == -1) {
317 err = "Unrecognized client limit class";
318 goto loaderr;
319 }
320 hard = memtoll(argv[2],NULL);
321 soft = memtoll(argv[3],NULL);
322 soft_seconds = atoi(argv[4]);
323 if (soft_seconds < 0) {
324 err = "Negative number of seconds in soft limt is invalid";
325 goto loaderr;
326 }
327 server.client_obuf_limits[class].hard_limit_bytes = hard;
328 server.client_obuf_limits[class].soft_limit_bytes = soft;
329 server.client_obuf_limits[class].soft_limit_seconds = soft_seconds;
330 } else if (!strcasecmp(argv[0],"stop-writes-on-bgsave-error") &&
331 argc == 2) {
332 if ((server.stop_writes_on_bgsave_err = yesnotoi(argv[1])) == -1) {
333 err = "argument must be 'yes' or 'no'"; goto loaderr;
334 }
335 } else {
336 err = "Bad directive or wrong number of arguments"; goto loaderr;
337 }
338 sdsfreesplitres(argv,argc);
339 }
340 sdsfreesplitres(lines,totlines);
341 return;
342
343 loaderr:
344 fprintf(stderr, "\n*** FATAL CONFIG FILE ERROR ***\n");
345 fprintf(stderr, "Reading the configuration file, at line %d\n", linenum);
346 fprintf(stderr, ">>> '%s'\n", lines[i]);
347 fprintf(stderr, "%s\n", err);
348 exit(1);
349 }
350
351 /* Load the server configuration from the specified filename.
352 * The function appends the additional configuration directives stored
353 * in the 'options' string to the config file before loading.
354 *
355 * Both filename and options can be NULL, in such a case are considered
356 * emtpy. This way loadServerConfig can be used to just load a file or
357 * just load a string. */
358 void loadServerConfig(char *filename, char *options) {
359 sds config = sdsempty();
360 char buf[REDIS_CONFIGLINE_MAX+1];
361
362 /* Load the file content */
363 if (filename) {
364 FILE *fp;
365
366 if (filename[0] == '-' && filename[1] == '\0') {
367 fp = stdin;
368 } else {
369 if ((fp = fopen(filename,"r")) == NULL) {
370 redisLog(REDIS_WARNING,
371 "Fatal error, can't open config file '%s'", filename);
372 exit(1);
373 }
374 }
375 while(fgets(buf,REDIS_CONFIGLINE_MAX+1,fp) != NULL)
376 config = sdscat(config,buf);
377 if (fp != stdin) fclose(fp);
378 }
379 /* Append the additional options */
380 if (options) {
381 config = sdscat(config,"\n");
382 config = sdscat(config,options);
383 }
384 loadServerConfigFromString(config);
385 sdsfree(config);
386 }
387
388 /*-----------------------------------------------------------------------------
389 * CONFIG command for remote configuration
390 *----------------------------------------------------------------------------*/
391
392 void configSetCommand(redisClient *c) {
393 robj *o;
394 long long ll;
395 redisAssertWithInfo(c,c->argv[2],c->argv[2]->encoding == REDIS_ENCODING_RAW);
396 redisAssertWithInfo(c,c->argv[2],c->argv[3]->encoding == REDIS_ENCODING_RAW);
397 o = c->argv[3];
398
399 if (!strcasecmp(c->argv[2]->ptr,"dbfilename")) {
400 zfree(server.rdb_filename);
401 server.rdb_filename = zstrdup(o->ptr);
402 } else if (!strcasecmp(c->argv[2]->ptr,"requirepass")) {
403 zfree(server.requirepass);
404 server.requirepass = ((char*)o->ptr)[0] ? zstrdup(o->ptr) : NULL;
405 } else if (!strcasecmp(c->argv[2]->ptr,"masterauth")) {
406 zfree(server.masterauth);
407 server.masterauth = zstrdup(o->ptr);
408 } else if (!strcasecmp(c->argv[2]->ptr,"maxmemory")) {
409 if (getLongLongFromObject(o,&ll) == REDIS_ERR ||
410 ll < 0) goto badfmt;
411 server.maxmemory = ll;
412 if (server.maxmemory) freeMemoryIfNeeded();
413 } else if (!strcasecmp(c->argv[2]->ptr,"maxmemory-policy")) {
414 if (!strcasecmp(o->ptr,"volatile-lru")) {
415 server.maxmemory_policy = REDIS_MAXMEMORY_VOLATILE_LRU;
416 } else if (!strcasecmp(o->ptr,"volatile-random")) {
417 server.maxmemory_policy = REDIS_MAXMEMORY_VOLATILE_RANDOM;
418 } else if (!strcasecmp(o->ptr,"volatile-ttl")) {
419 server.maxmemory_policy = REDIS_MAXMEMORY_VOLATILE_TTL;
420 } else if (!strcasecmp(o->ptr,"allkeys-lru")) {
421 server.maxmemory_policy = REDIS_MAXMEMORY_ALLKEYS_LRU;
422 } else if (!strcasecmp(o->ptr,"allkeys-random")) {
423 server.maxmemory_policy = REDIS_MAXMEMORY_ALLKEYS_RANDOM;
424 } else if (!strcasecmp(o->ptr,"noeviction")) {
425 server.maxmemory_policy = REDIS_MAXMEMORY_NO_EVICTION;
426 } else {
427 goto badfmt;
428 }
429 } else if (!strcasecmp(c->argv[2]->ptr,"maxmemory-samples")) {
430 if (getLongLongFromObject(o,&ll) == REDIS_ERR ||
431 ll <= 0) goto badfmt;
432 server.maxmemory_samples = ll;
433 } else if (!strcasecmp(c->argv[2]->ptr,"timeout")) {
434 if (getLongLongFromObject(o,&ll) == REDIS_ERR ||
435 ll < 0 || ll > LONG_MAX) goto badfmt;
436 server.maxidletime = ll;
437 } else if (!strcasecmp(c->argv[2]->ptr,"appendfsync")) {
438 if (!strcasecmp(o->ptr,"no")) {
439 server.aof_fsync = AOF_FSYNC_NO;
440 } else if (!strcasecmp(o->ptr,"everysec")) {
441 server.aof_fsync = AOF_FSYNC_EVERYSEC;
442 } else if (!strcasecmp(o->ptr,"always")) {
443 server.aof_fsync = AOF_FSYNC_ALWAYS;
444 } else {
445 goto badfmt;
446 }
447 } else if (!strcasecmp(c->argv[2]->ptr,"no-appendfsync-on-rewrite")) {
448 int yn = yesnotoi(o->ptr);
449
450 if (yn == -1) goto badfmt;
451 server.aof_no_fsync_on_rewrite = yn;
452 } else if (!strcasecmp(c->argv[2]->ptr,"appendonly")) {
453 int enable = yesnotoi(o->ptr);
454
455 if (enable == -1) goto badfmt;
456 if (enable == 0 && server.aof_state != REDIS_AOF_OFF) {
457 stopAppendOnly();
458 } else if (enable && server.aof_state == REDIS_AOF_OFF) {
459 if (startAppendOnly() == REDIS_ERR) {
460 addReplyError(c,
461 "Unable to turn on AOF. Check server logs.");
462 return;
463 }
464 }
465 } else if (!strcasecmp(c->argv[2]->ptr,"auto-aof-rewrite-percentage")) {
466 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
467 server.aof_rewrite_perc = ll;
468 } else if (!strcasecmp(c->argv[2]->ptr,"auto-aof-rewrite-min-size")) {
469 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
470 server.aof_rewrite_min_size = ll;
471 } else if (!strcasecmp(c->argv[2]->ptr,"save")) {
472 int vlen, j;
473 sds *v = sdssplitlen(o->ptr,sdslen(o->ptr)," ",1,&vlen);
474
475 /* Perform sanity check before setting the new config:
476 * - Even number of args
477 * - Seconds >= 1, changes >= 0 */
478 if (vlen & 1) {
479 sdsfreesplitres(v,vlen);
480 goto badfmt;
481 }
482 for (j = 0; j < vlen; j++) {
483 char *eptr;
484 long val;
485
486 val = strtoll(v[j], &eptr, 10);
487 if (eptr[0] != '\0' ||
488 ((j & 1) == 0 && val < 1) ||
489 ((j & 1) == 1 && val < 0)) {
490 sdsfreesplitres(v,vlen);
491 goto badfmt;
492 }
493 }
494 /* Finally set the new config */
495 resetServerSaveParams();
496 for (j = 0; j < vlen; j += 2) {
497 time_t seconds;
498 int changes;
499
500 seconds = strtoll(v[j],NULL,10);
501 changes = strtoll(v[j+1],NULL,10);
502 appendServerSaveParams(seconds, changes);
503 }
504 sdsfreesplitres(v,vlen);
505 } else if (!strcasecmp(c->argv[2]->ptr,"slave-serve-stale-data")) {
506 int yn = yesnotoi(o->ptr);
507
508 if (yn == -1) goto badfmt;
509 server.repl_serve_stale_data = yn;
510 } else if (!strcasecmp(c->argv[2]->ptr,"dir")) {
511 if (chdir((char*)o->ptr) == -1) {
512 addReplyErrorFormat(c,"Changing directory: %s", strerror(errno));
513 return;
514 }
515 } else if (!strcasecmp(c->argv[2]->ptr,"hash-max-ziplist-entries")) {
516 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
517 server.hash_max_ziplist_entries = ll;
518 } else if (!strcasecmp(c->argv[2]->ptr,"hash-max-ziplist-value")) {
519 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
520 server.hash_max_ziplist_value = ll;
521 } else if (!strcasecmp(c->argv[2]->ptr,"list-max-ziplist-entries")) {
522 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
523 server.list_max_ziplist_entries = ll;
524 } else if (!strcasecmp(c->argv[2]->ptr,"list-max-ziplist-value")) {
525 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
526 server.list_max_ziplist_value = ll;
527 } else if (!strcasecmp(c->argv[2]->ptr,"set-max-intset-entries")) {
528 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
529 server.set_max_intset_entries = ll;
530 } else if (!strcasecmp(c->argv[2]->ptr,"zset-max-ziplist-entries")) {
531 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
532 server.zset_max_ziplist_entries = ll;
533 } else if (!strcasecmp(c->argv[2]->ptr,"zset-max-ziplist-value")) {
534 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
535 server.zset_max_ziplist_value = ll;
536 } else if (!strcasecmp(c->argv[2]->ptr,"lua-time-limit")) {
537 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
538 server.lua_time_limit = ll;
539 } else if (!strcasecmp(c->argv[2]->ptr,"slowlog-log-slower-than")) {
540 if (getLongLongFromObject(o,&ll) == REDIS_ERR) goto badfmt;
541 server.slowlog_log_slower_than = ll;
542 } else if (!strcasecmp(c->argv[2]->ptr,"slowlog-max-len")) {
543 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
544 server.slowlog_max_len = (unsigned)ll;
545 } else if (!strcasecmp(c->argv[2]->ptr,"loglevel")) {
546 if (!strcasecmp(o->ptr,"warning")) {
547 server.verbosity = REDIS_WARNING;
548 } else if (!strcasecmp(o->ptr,"notice")) {
549 server.verbosity = REDIS_NOTICE;
550 } else if (!strcasecmp(o->ptr,"verbose")) {
551 server.verbosity = REDIS_VERBOSE;
552 } else if (!strcasecmp(o->ptr,"debug")) {
553 server.verbosity = REDIS_DEBUG;
554 } else {
555 goto badfmt;
556 }
557 } else if (!strcasecmp(c->argv[2]->ptr,"client-output-buffer-limit")) {
558 int vlen, j;
559 sds *v = sdssplitlen(o->ptr,sdslen(o->ptr)," ",1,&vlen);
560
561 /* We need a multiple of 4: <class> <hard> <soft> <soft_seconds> */
562 if (vlen % 4) {
563 sdsfreesplitres(v,vlen);
564 goto badfmt;
565 }
566
567 /* Sanity check of single arguments, so that we either refuse the
568 * whole configuration string or accept it all, even if a single
569 * error in a single client class is present. */
570 for (j = 0; j < vlen; j++) {
571 char *eptr;
572 long val;
573
574 if ((j % 4) == 0) {
575 if (getClientLimitClassByName(v[j]) == -1) {
576 sdsfreesplitres(v,vlen);
577 goto badfmt;
578 }
579 } else {
580 val = strtoll(v[j], &eptr, 10);
581 if (eptr[0] != '\0' || val < 0) {
582 sdsfreesplitres(v,vlen);
583 goto badfmt;
584 }
585 }
586 }
587 /* Finally set the new config */
588 for (j = 0; j < vlen; j += 4) {
589 int class;
590 unsigned long long hard, soft;
591 int soft_seconds;
592
593 class = getClientLimitClassByName(v[j]);
594 hard = strtoll(v[j+1],NULL,10);
595 soft = strtoll(v[j+2],NULL,10);
596 soft_seconds = strtoll(v[j+3],NULL,10);
597
598 server.client_obuf_limits[class].hard_limit_bytes = hard;
599 server.client_obuf_limits[class].soft_limit_bytes = soft;
600 server.client_obuf_limits[class].soft_limit_seconds = soft_seconds;
601 }
602 sdsfreesplitres(v,vlen);
603 } else if (!strcasecmp(c->argv[2]->ptr,"stop-writes-on-bgsave-error")) {
604 int yn = yesnotoi(o->ptr);
605
606 if (yn == -1) goto badfmt;
607 server.stop_writes_on_bgsave_err = yn;
608 } else if (!strcasecmp(c->argv[2]->ptr,"repl-ping-slave-period")) {
609 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll <= 0) goto badfmt;
610 server.repl_ping_slave_period = ll;
611 } else if (!strcasecmp(c->argv[2]->ptr,"repl-timeout")) {
612 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll <= 0) goto badfmt;
613 server.repl_timeout = ll;
614 } else {
615 addReplyErrorFormat(c,"Unsupported CONFIG parameter: %s",
616 (char*)c->argv[2]->ptr);
617 return;
618 }
619 addReply(c,shared.ok);
620 return;
621
622 badfmt: /* Bad format errors */
623 addReplyErrorFormat(c,"Invalid argument '%s' for CONFIG SET '%s'",
624 (char*)o->ptr,
625 (char*)c->argv[2]->ptr);
626 }
627
628 #define config_get_string_field(_name,_var) do { \
629 if (stringmatch(pattern,_name,0)) { \
630 addReplyBulkCString(c,_name); \
631 addReplyBulkCString(c,_var ? _var : ""); \
632 matches++; \
633 } \
634 } while(0);
635
636 #define config_get_bool_field(_name,_var) do { \
637 if (stringmatch(pattern,_name,0)) { \
638 addReplyBulkCString(c,_name); \
639 addReplyBulkCString(c,_var ? "yes" : "no"); \
640 matches++; \
641 } \
642 } while(0);
643
644 #define config_get_numerical_field(_name,_var) do { \
645 if (stringmatch(pattern,_name,0)) { \
646 ll2string(buf,sizeof(buf),_var); \
647 addReplyBulkCString(c,_name); \
648 addReplyBulkCString(c,buf); \
649 matches++; \
650 } \
651 } while(0);
652
653 void configGetCommand(redisClient *c) {
654 robj *o = c->argv[2];
655 void *replylen = addDeferredMultiBulkLength(c);
656 char *pattern = o->ptr;
657 char buf[128];
658 int matches = 0;
659 redisAssertWithInfo(c,o,o->encoding == REDIS_ENCODING_RAW);
660
661 /* String values */
662 config_get_string_field("dbfilename",server.rdb_filename);
663 config_get_string_field("requirepass",server.requirepass);
664 config_get_string_field("masterauth",server.requirepass);
665 config_get_string_field("bind",server.bindaddr);
666 config_get_string_field("unixsocket",server.unixsocket);
667 config_get_string_field("logfile",server.logfile);
668 config_get_string_field("pidfile",server.pidfile);
669
670 /* Numerical values */
671 config_get_numerical_field("maxmemory",server.maxmemory);
672 config_get_numerical_field("maxmemory-samples",server.maxmemory_samples);
673 config_get_numerical_field("timeout",server.maxidletime);
674 config_get_numerical_field("auto-aof-rewrite-percentage",
675 server.aof_rewrite_perc);
676 config_get_numerical_field("auto-aof-rewrite-min-size",
677 server.aof_rewrite_min_size);
678 config_get_numerical_field("hash-max-ziplist-entries",
679 server.hash_max_ziplist_entries);
680 config_get_numerical_field("hash-max-ziplist-value",
681 server.hash_max_ziplist_value);
682 config_get_numerical_field("list-max-ziplist-entries",
683 server.list_max_ziplist_entries);
684 config_get_numerical_field("list-max-ziplist-value",
685 server.list_max_ziplist_value);
686 config_get_numerical_field("set-max-intset-entries",
687 server.set_max_intset_entries);
688 config_get_numerical_field("zset-max-ziplist-entries",
689 server.zset_max_ziplist_entries);
690 config_get_numerical_field("zset-max-ziplist-value",
691 server.zset_max_ziplist_value);
692 config_get_numerical_field("lua-time-limit",server.lua_time_limit);
693 config_get_numerical_field("slowlog-log-slower-than",
694 server.slowlog_log_slower_than);
695 config_get_numerical_field("slowlog-max-len",
696 server.slowlog_max_len);
697 config_get_numerical_field("port",server.port);
698 config_get_numerical_field("databases",server.dbnum);
699 config_get_numerical_field("repl-ping-slave-period",server.repl_ping_slave_period);
700 config_get_numerical_field("repl-timeout",server.repl_timeout);
701 config_get_numerical_field("maxclients",server.maxclients);
702
703 /* Bool (yes/no) values */
704 config_get_bool_field("no-appendfsync-on-rewrite",
705 server.aof_no_fsync_on_rewrite);
706 config_get_bool_field("slave-serve-stale-data",
707 server.repl_serve_stale_data);
708 config_get_bool_field("stop-writes-on-bgsave-error",
709 server.stop_writes_on_bgsave_err);
710 config_get_bool_field("daemonize", server.daemonize);
711 config_get_bool_field("rdbcompression", server.rdb_compression);
712 config_get_bool_field("activerehashing", server.activerehashing);
713
714 /* Everything we can't handle with macros follows. */
715
716 if (stringmatch(pattern,"appendonly",0)) {
717 addReplyBulkCString(c,"appendonly");
718 addReplyBulkCString(c,server.aof_state == REDIS_AOF_OFF ? "no" : "yes");
719 matches++;
720 }
721 if (stringmatch(pattern,"dir",0)) {
722 char buf[1024];
723
724 if (getcwd(buf,sizeof(buf)) == NULL)
725 buf[0] = '\0';
726
727 addReplyBulkCString(c,"dir");
728 addReplyBulkCString(c,buf);
729 matches++;
730 }
731 if (stringmatch(pattern,"maxmemory-policy",0)) {
732 char *s;
733
734 switch(server.maxmemory_policy) {
735 case REDIS_MAXMEMORY_VOLATILE_LRU: s = "volatile-lru"; break;
736 case REDIS_MAXMEMORY_VOLATILE_TTL: s = "volatile-ttl"; break;
737 case REDIS_MAXMEMORY_VOLATILE_RANDOM: s = "volatile-random"; break;
738 case REDIS_MAXMEMORY_ALLKEYS_LRU: s = "allkeys-lru"; break;
739 case REDIS_MAXMEMORY_ALLKEYS_RANDOM: s = "allkeys-random"; break;
740 case REDIS_MAXMEMORY_NO_EVICTION: s = "noeviction"; break;
741 default: s = "unknown"; break; /* too harmless to panic */
742 }
743 addReplyBulkCString(c,"maxmemory-policy");
744 addReplyBulkCString(c,s);
745 matches++;
746 }
747 if (stringmatch(pattern,"appendfsync",0)) {
748 char *policy;
749
750 switch(server.aof_fsync) {
751 case AOF_FSYNC_NO: policy = "no"; break;
752 case AOF_FSYNC_EVERYSEC: policy = "everysec"; break;
753 case AOF_FSYNC_ALWAYS: policy = "always"; break;
754 default: policy = "unknown"; break; /* too harmless to panic */
755 }
756 addReplyBulkCString(c,"appendfsync");
757 addReplyBulkCString(c,policy);
758 matches++;
759 }
760 if (stringmatch(pattern,"save",0)) {
761 sds buf = sdsempty();
762 int j;
763
764 for (j = 0; j < server.saveparamslen; j++) {
765 buf = sdscatprintf(buf,"%ld %d",
766 server.saveparams[j].seconds,
767 server.saveparams[j].changes);
768 if (j != server.saveparamslen-1)
769 buf = sdscatlen(buf," ",1);
770 }
771 addReplyBulkCString(c,"save");
772 addReplyBulkCString(c,buf);
773 sdsfree(buf);
774 matches++;
775 }
776 if (stringmatch(pattern,"loglevel",0)) {
777 char *s;
778
779 switch(server.verbosity) {
780 case REDIS_WARNING: s = "warning"; break;
781 case REDIS_VERBOSE: s = "verbose"; break;
782 case REDIS_NOTICE: s = "notice"; break;
783 case REDIS_DEBUG: s = "debug"; break;
784 default: s = "unknown"; break; /* too harmless to panic */
785 }
786 addReplyBulkCString(c,"loglevel");
787 addReplyBulkCString(c,s);
788 matches++;
789 }
790 if (stringmatch(pattern,"client-output-buffer-limit",0)) {
791 sds buf = sdsempty();
792 int j;
793
794 for (j = 0; j < REDIS_CLIENT_LIMIT_NUM_CLASSES; j++) {
795 buf = sdscatprintf(buf,"%s %llu %llu %ld",
796 getClientLimitClassName(j),
797 server.client_obuf_limits[j].hard_limit_bytes,
798 server.client_obuf_limits[j].soft_limit_bytes,
799 (long) server.client_obuf_limits[j].soft_limit_seconds);
800 if (j != REDIS_CLIENT_LIMIT_NUM_CLASSES-1)
801 buf = sdscatlen(buf," ",1);
802 }
803 addReplyBulkCString(c,"client-output-buffer-limit");
804 addReplyBulkCString(c,buf);
805 sdsfree(buf);
806 matches++;
807 }
808 if (stringmatch(pattern,"unixsocketperm",0)) {
809 char buf[32];
810 snprintf(buf,sizeof(buf),"%o",server.unixsocketperm);
811 addReplyBulkCString(c,"unixsocketperm");
812 addReplyBulkCString(c,buf);
813 matches++;
814 }
815 if (stringmatch(pattern,"slaveof",0)) {
816 char buf[256];
817
818 addReplyBulkCString(c,"slaveof");
819 if (server.masterhost)
820 snprintf(buf,sizeof(buf),"%s %d",
821 server.masterhost, server.masterport);
822 else
823 buf[0] = '\0';
824 addReplyBulkCString(c,buf);
825 matches++;
826 }
827 setDeferredMultiBulkLength(c,replylen,matches*2);
828 }
829
830 void configCommand(redisClient *c) {
831 if (!strcasecmp(c->argv[1]->ptr,"set")) {
832 if (c->argc != 4) goto badarity;
833 configSetCommand(c);
834 } else if (!strcasecmp(c->argv[1]->ptr,"get")) {
835 if (c->argc != 3) goto badarity;
836 configGetCommand(c);
837 } else if (!strcasecmp(c->argv[1]->ptr,"resetstat")) {
838 if (c->argc != 2) goto badarity;
839 server.stat_keyspace_hits = 0;
840 server.stat_keyspace_misses = 0;
841 server.stat_numcommands = 0;
842 server.stat_numconnections = 0;
843 server.stat_expiredkeys = 0;
844 resetCommandTableStats();
845 addReply(c,shared.ok);
846 } else {
847 addReplyError(c,
848 "CONFIG subcommand must be one of GET, SET, RESETSTAT");
849 }
850 return;
851
852 badarity:
853 addReplyErrorFormat(c,"Wrong number of arguments for CONFIG %s",
854 (char*) c->argv[1]->ptr);
855 }