]> git.saurik.com Git - redis.git/blame - src/config.c
By default Redis refuses writes with an error if the latest BGSAVE failed (and at...
[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
67c6f0f6 26void loadServerConfigFromString(char *config) {
27 char *err = NULL;
28 int linenum = 0, totlines, i;
29 sds *lines;
e2641e09 30
67c6f0f6 31 lines = sdssplitlen(config,strlen(config),"\n",1,&totlines);
e2641e09 32
67c6f0f6 33 for (i = 0; i < totlines; i++) {
e2641e09 34 sds *argv;
67c6f0f6 35 int argc;
e2641e09 36
67c6f0f6 37 linenum = i+1;
38 lines[i] = sdstrim(lines[i]," \t\r\n");
e2641e09 39
40 /* Skip comments and blank lines*/
67c6f0f6 41 if (lines[i][0] == '#' || lines[i][0] == '\0') continue;
e2641e09 42
43 /* Split into arguments */
67c6f0f6 44 argv = sdssplitargs(lines[i],&argc);
e2641e09 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]);
df50d707 55 if (server.port < 0 || server.port > 65535) {
e2641e09 56 err = "Invalid port"; goto loaderr;
57 }
58 } else if (!strcasecmp(argv[0],"bind") && argc == 2) {
59 server.bindaddr = zstrdup(argv[1]);
5d10923f
PN
60 } else if (!strcasecmp(argv[0],"unixsocket") && argc == 2) {
61 server.unixsocket = zstrdup(argv[1]);
85238765 62 } else if (!strcasecmp(argv[0],"unixsocketperm") && argc == 2) {
bb48c5fa 63 errno = 0;
85238765
NF
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 }
4aac3ff2 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();
e2641e09 78 }
e2641e09 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 }
e1a586ee
JH
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 }
e2641e09 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) {
67c6f0f6 155 loadServerConfig(argv[1],NULL);
e2641e09 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);
165346ca 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;
5402c426 171 } else if (!strcasecmp(argv[1],"noeviction")) {
172 server.maxmemory_policy = REDIS_MAXMEMORY_NO_EVICTION;
165346ca 173 } else {
174 err = "Invalid maxmemory policy";
175 goto loaderr;
176 }
a3687929 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 }
e2641e09 183 } else if (!strcasecmp(argv[0],"slaveof") && argc == 3) {
184 server.masterhost = sdsnew(argv[1]);
185 server.masterport = atoi(argv[2]);
1844f990 186 server.repl_state = REDIS_REPL_CONNECT;
8996bf77 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 }
e2641e09 199 } else if (!strcasecmp(argv[0],"masterauth") && argc == 2) {
200 server.masterauth = zstrdup(argv[1]);
4ebfc455 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 }
a510cb0c
PN
205 } else if (!strcasecmp(argv[0],"glueoutputbuf")) {
206 redisLog(REDIS_WARNING, "Deprecated configuration directive: \"%s\"", argv[0]);
e2641e09 207 } else if (!strcasecmp(argv[0],"rdbcompression") && argc == 2) {
f48cd4b9 208 if ((server.rdb_compression = yesnotoi(argv[1])) == -1) {
e2641e09 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) {
e394114d 220 int yes;
221
222 if ((yes = yesnotoi(argv[1])) == -1) {
e2641e09 223 err = "argument must be 'yes' or 'no'"; goto loaderr;
224 }
e394114d 225 server.aof_state = yes ? REDIS_AOF_ON : REDIS_AOF_OFF;
e2641e09 226 } else if (!strcasecmp(argv[0],"appendfilename") && argc == 2) {
2c915bcf 227 zfree(server.aof_filename);
228 server.aof_filename = zstrdup(argv[1]);
e2641e09 229 } else if (!strcasecmp(argv[0],"no-appendfsync-on-rewrite")
230 && argc == 2) {
2c915bcf 231 if ((server.aof_no_fsync_on_rewrite= yesnotoi(argv[1])) == -1) {
e2641e09 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")) {
2c915bcf 236 server.aof_fsync = AOF_FSYNC_NO;
e2641e09 237 } else if (!strcasecmp(argv[1],"always")) {
2c915bcf 238 server.aof_fsync = AOF_FSYNC_ALWAYS;
e2641e09 239 } else if (!strcasecmp(argv[1],"everysec")) {
2c915bcf 240 server.aof_fsync = AOF_FSYNC_EVERYSEC;
e2641e09 241 } else {
242 err = "argument must be 'no', 'always' or 'everysec'";
243 goto loaderr;
244 }
b333e239 245 } else if (!strcasecmp(argv[0],"auto-aof-rewrite-percentage") &&
246 argc == 2)
247 {
2c915bcf 248 server.aof_rewrite_perc = atoi(argv[1]);
249 if (server.aof_rewrite_perc < 0) {
b333e239 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 {
2c915bcf 256 server.aof_rewrite_min_size = memtoll(argv[1],NULL);
e2641e09 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) {
f48cd4b9 263 zfree(server.rdb_filename);
264 server.rdb_filename = zstrdup(argv[1]);
8d3e063a 265 } else if (!strcasecmp(argv[0],"hash-max-zipmap-entries") && argc == 2) {
e2641e09 266 server.hash_max_zipmap_entries = memtoll(argv[1], NULL);
8d3e063a 267 } else if (!strcasecmp(argv[0],"hash-max-zipmap-value") && argc == 2) {
e2641e09 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);
8d3e063a 271 } else if (!strcasecmp(argv[0],"list-max-ziplist-value") && argc == 2) {
e2641e09 272 server.list_max_ziplist_value = memtoll(argv[1], NULL);
8d3e063a 273 } else if (!strcasecmp(argv[0],"set-max-intset-entries") && argc == 2) {
96ffb2fe 274 server.set_max_intset_entries = memtoll(argv[1], NULL);
3ea204e1
PN
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);
8d3e063a 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 }
ecc91094 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 }
1793752d 307 } else if (!strcasecmp(argv[0],"cluster-config-file") && argc == 2) {
308 zfree(server.cluster.configfile);
309 server.cluster.configfile = zstrdup(argv[1]);
eeffcf38 310 } else if (!strcasecmp(argv[0],"lua-time-limit") && argc == 2) {
311 server.lua_time_limit = strtoll(argv[1],NULL,10);
35a60441 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);
7fe8d49a 318 } else if (!strcasecmp(argv[0],"client-output-buffer-limit") &&
319 argc == 5)
320 {
321 int class = getClientLimitClassByName(argv[1]);
322 unsigned long long hard, soft;
323 int soft_seconds;
324
325 if (class == -1) {
326 err = "Unrecognized client limit class";
327 goto loaderr;
328 }
329 hard = memtoll(argv[2],NULL);
330 soft = memtoll(argv[3],NULL);
331 soft_seconds = atoi(argv[4]);
332 if (soft_seconds < 0) {
333 err = "Negative number of seconds in soft limt is invalid";
334 goto loaderr;
335 }
336 server.client_obuf_limits[class].hard_limit_bytes = hard;
337 server.client_obuf_limits[class].soft_limit_bytes = soft;
338 server.client_obuf_limits[class].soft_limit_seconds = soft_seconds;
4d3bbf35 339 } else if (!strcasecmp(argv[0],"stop-writes-on-bgsave-error") &&
340 argc == 2) {
341 if ((server.stop_writes_on_bgsave_err = yesnotoi(argv[1])) == -1) {
342 err = "argument must be 'yes' or 'no'"; goto loaderr;
343 }
e2641e09 344 } else {
345 err = "Bad directive or wrong number of arguments"; goto loaderr;
346 }
67c6f0f6 347 sdsfreesplitres(argv,argc);
e2641e09 348 }
67c6f0f6 349 sdsfreesplitres(lines,totlines);
e2641e09 350 return;
351
352loaderr:
353 fprintf(stderr, "\n*** FATAL CONFIG FILE ERROR ***\n");
354 fprintf(stderr, "Reading the configuration file, at line %d\n", linenum);
67c6f0f6 355 fprintf(stderr, ">>> '%s'\n", lines[i]);
e2641e09 356 fprintf(stderr, "%s\n", err);
357 exit(1);
358}
359
67c6f0f6 360/* Load the server configuration from the specified filename.
361 * The function appends the additional configuration directives stored
362 * in the 'options' string to the config file before loading.
363 *
364 * Both filename and options can be NULL, in such a case are considered
365 * emtpy. This way loadServerConfig can be used to just load a file or
366 * just load a string. */
367void loadServerConfig(char *filename, char *options) {
368 sds config = sdsempty();
369 char buf[REDIS_CONFIGLINE_MAX+1];
370
371 /* Load the file content */
372 if (filename) {
373 FILE *fp;
374
375 if (filename[0] == '-' && filename[1] == '\0') {
376 fp = stdin;
377 } else {
378 if ((fp = fopen(filename,"r")) == NULL) {
379 redisLog(REDIS_WARNING,
380 "Fatal error, can't open config file '%s'", filename);
381 exit(1);
382 }
383 }
384 while(fgets(buf,REDIS_CONFIGLINE_MAX+1,fp) != NULL)
385 config = sdscat(config,buf);
386 if (fp != stdin) fclose(fp);
387 }
388 /* Append the additional options */
389 if (options) {
390 config = sdscat(config,"\n");
391 config = sdscat(config,options);
392 }
393 loadServerConfigFromString(config);
394 sdsfree(config);
395}
396
e2641e09 397/*-----------------------------------------------------------------------------
398 * CONFIG command for remote configuration
399 *----------------------------------------------------------------------------*/
400
401void configSetCommand(redisClient *c) {
a375b077 402 robj *o;
e2641e09 403 long long ll;
eab0e26e 404 redisAssertWithInfo(c,c->argv[2],c->argv[2]->encoding == REDIS_ENCODING_RAW);
405 redisAssertWithInfo(c,c->argv[2],c->argv[3]->encoding == REDIS_ENCODING_RAW);
a375b077 406 o = c->argv[3];
e2641e09 407
408 if (!strcasecmp(c->argv[2]->ptr,"dbfilename")) {
f48cd4b9 409 zfree(server.rdb_filename);
410 server.rdb_filename = zstrdup(o->ptr);
e2641e09 411 } else if (!strcasecmp(c->argv[2]->ptr,"requirepass")) {
412 zfree(server.requirepass);
1eceb85e 413 server.requirepass = ((char*)o->ptr)[0] ? zstrdup(o->ptr) : NULL;
e2641e09 414 } else if (!strcasecmp(c->argv[2]->ptr,"masterauth")) {
415 zfree(server.masterauth);
416 server.masterauth = zstrdup(o->ptr);
417 } else if (!strcasecmp(c->argv[2]->ptr,"maxmemory")) {
418 if (getLongLongFromObject(o,&ll) == REDIS_ERR ||
419 ll < 0) goto badfmt;
420 server.maxmemory = ll;
b4f2e412 421 if (server.maxmemory) freeMemoryIfNeeded();
165346ca 422 } else if (!strcasecmp(c->argv[2]->ptr,"maxmemory-policy")) {
423 if (!strcasecmp(o->ptr,"volatile-lru")) {
424 server.maxmemory_policy = REDIS_MAXMEMORY_VOLATILE_LRU;
425 } else if (!strcasecmp(o->ptr,"volatile-random")) {
426 server.maxmemory_policy = REDIS_MAXMEMORY_VOLATILE_RANDOM;
427 } else if (!strcasecmp(o->ptr,"volatile-ttl")) {
428 server.maxmemory_policy = REDIS_MAXMEMORY_VOLATILE_TTL;
429 } else if (!strcasecmp(o->ptr,"allkeys-lru")) {
430 server.maxmemory_policy = REDIS_MAXMEMORY_ALLKEYS_LRU;
431 } else if (!strcasecmp(o->ptr,"allkeys-random")) {
432 server.maxmemory_policy = REDIS_MAXMEMORY_ALLKEYS_RANDOM;
5402c426 433 } else if (!strcasecmp(o->ptr,"noeviction")) {
434 server.maxmemory_policy = REDIS_MAXMEMORY_NO_EVICTION;
165346ca 435 } else {
436 goto badfmt;
437 }
a3687929 438 } else if (!strcasecmp(c->argv[2]->ptr,"maxmemory-samples")) {
439 if (getLongLongFromObject(o,&ll) == REDIS_ERR ||
440 ll <= 0) goto badfmt;
441 server.maxmemory_samples = ll;
e2641e09 442 } else if (!strcasecmp(c->argv[2]->ptr,"timeout")) {
443 if (getLongLongFromObject(o,&ll) == REDIS_ERR ||
444 ll < 0 || ll > LONG_MAX) goto badfmt;
445 server.maxidletime = ll;
446 } else if (!strcasecmp(c->argv[2]->ptr,"appendfsync")) {
447 if (!strcasecmp(o->ptr,"no")) {
2c915bcf 448 server.aof_fsync = AOF_FSYNC_NO;
e2641e09 449 } else if (!strcasecmp(o->ptr,"everysec")) {
2c915bcf 450 server.aof_fsync = AOF_FSYNC_EVERYSEC;
e2641e09 451 } else if (!strcasecmp(o->ptr,"always")) {
2c915bcf 452 server.aof_fsync = AOF_FSYNC_ALWAYS;
e2641e09 453 } else {
454 goto badfmt;
455 }
456 } else if (!strcasecmp(c->argv[2]->ptr,"no-appendfsync-on-rewrite")) {
457 int yn = yesnotoi(o->ptr);
458
459 if (yn == -1) goto badfmt;
2c915bcf 460 server.aof_no_fsync_on_rewrite = yn;
e2641e09 461 } else if (!strcasecmp(c->argv[2]->ptr,"appendonly")) {
e394114d 462 int enable = yesnotoi(o->ptr);
463
464 if (enable == -1) goto badfmt;
465 if (enable == 0 && server.aof_state != REDIS_AOF_OFF) {
466 stopAppendOnly();
467 } else if (enable && server.aof_state == REDIS_AOF_OFF) {
468 if (startAppendOnly() == REDIS_ERR) {
469 addReplyError(c,
470 "Unable to turn on AOF. Check server logs.");
471 return;
e2641e09 472 }
473 }
0ec93220 474 } else if (!strcasecmp(c->argv[2]->ptr,"auto-aof-rewrite-percentage")) {
475 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
2c915bcf 476 server.aof_rewrite_perc = ll;
0ec93220 477 } else if (!strcasecmp(c->argv[2]->ptr,"auto-aof-rewrite-min-size")) {
478 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
2c915bcf 479 server.aof_rewrite_min_size = ll;
e2641e09 480 } else if (!strcasecmp(c->argv[2]->ptr,"save")) {
481 int vlen, j;
482 sds *v = sdssplitlen(o->ptr,sdslen(o->ptr)," ",1,&vlen);
483
484 /* Perform sanity check before setting the new config:
485 * - Even number of args
486 * - Seconds >= 1, changes >= 0 */
487 if (vlen & 1) {
488 sdsfreesplitres(v,vlen);
489 goto badfmt;
490 }
491 for (j = 0; j < vlen; j++) {
492 char *eptr;
493 long val;
494
495 val = strtoll(v[j], &eptr, 10);
496 if (eptr[0] != '\0' ||
497 ((j & 1) == 0 && val < 1) ||
498 ((j & 1) == 1 && val < 0)) {
499 sdsfreesplitres(v,vlen);
500 goto badfmt;
501 }
502 }
503 /* Finally set the new config */
504 resetServerSaveParams();
505 for (j = 0; j < vlen; j += 2) {
506 time_t seconds;
507 int changes;
508
509 seconds = strtoll(v[j],NULL,10);
510 changes = strtoll(v[j+1],NULL,10);
511 appendServerSaveParams(seconds, changes);
512 }
513 sdsfreesplitres(v,vlen);
4ebfc455 514 } else if (!strcasecmp(c->argv[2]->ptr,"slave-serve-stale-data")) {
515 int yn = yesnotoi(o->ptr);
516
517 if (yn == -1) goto badfmt;
518 server.repl_serve_stale_data = yn;
21cda08b 519 } else if (!strcasecmp(c->argv[2]->ptr,"dir")) {
520 if (chdir((char*)o->ptr) == -1) {
521 addReplyErrorFormat(c,"Changing directory: %s", strerror(errno));
522 return;
523 }
465b4189 524 } else if (!strcasecmp(c->argv[2]->ptr,"hash-max-zipmap-entries")) {
525 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
526 server.hash_max_zipmap_entries = ll;
527 } else if (!strcasecmp(c->argv[2]->ptr,"hash-max-zipmap-value")) {
528 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
529 server.hash_max_zipmap_value = ll;
530 } else if (!strcasecmp(c->argv[2]->ptr,"list-max-ziplist-entries")) {
531 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
532 server.list_max_ziplist_entries = ll;
533 } else if (!strcasecmp(c->argv[2]->ptr,"list-max-ziplist-value")) {
534 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
535 server.list_max_ziplist_value = ll;
536 } else if (!strcasecmp(c->argv[2]->ptr,"set-max-intset-entries")) {
537 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
538 server.set_max_intset_entries = ll;
3ea204e1
PN
539 } else if (!strcasecmp(c->argv[2]->ptr,"zset-max-ziplist-entries")) {
540 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
541 server.zset_max_ziplist_entries = ll;
542 } else if (!strcasecmp(c->argv[2]->ptr,"zset-max-ziplist-value")) {
543 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
544 server.zset_max_ziplist_value = ll;
eeffcf38 545 } else if (!strcasecmp(c->argv[2]->ptr,"lua-time-limit")) {
546 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
547 server.lua_time_limit = ll;
35a60441 548 } else if (!strcasecmp(c->argv[2]->ptr,"slowlog-log-slower-than")) {
549 if (getLongLongFromObject(o,&ll) == REDIS_ERR) goto badfmt;
550 server.slowlog_log_slower_than = ll;
551 } else if (!strcasecmp(c->argv[2]->ptr,"slowlog-max-len")) {
552 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
553 server.slowlog_max_len = (unsigned)ll;
2aa26d2a 554 } else if (!strcasecmp(c->argv[2]->ptr,"loglevel")) {
555 if (!strcasecmp(o->ptr,"warning")) {
556 server.verbosity = REDIS_WARNING;
557 } else if (!strcasecmp(o->ptr,"notice")) {
558 server.verbosity = REDIS_NOTICE;
559 } else if (!strcasecmp(o->ptr,"verbose")) {
560 server.verbosity = REDIS_VERBOSE;
561 } else if (!strcasecmp(o->ptr,"debug")) {
562 server.verbosity = REDIS_DEBUG;
563 } else {
564 goto badfmt;
565 }
7fe8d49a 566 } else if (!strcasecmp(c->argv[2]->ptr,"client-output-buffer-limit")) {
567 int vlen, j;
568 sds *v = sdssplitlen(o->ptr,sdslen(o->ptr)," ",1,&vlen);
569
570 /* We need a multiple of 4: <class> <hard> <soft> <soft_seconds> */
571 if (vlen % 4) {
572 sdsfreesplitres(v,vlen);
573 goto badfmt;
574 }
575
576 /* Sanity check of single arguments, so that we either refuse the
577 * whole configuration string or accept it all, even if a single
578 * error in a single client class is present. */
579 for (j = 0; j < vlen; j++) {
580 char *eptr;
581 long val;
582
583 if ((j % 4) == 0) {
584 if (getClientLimitClassByName(v[j]) == -1) {
585 sdsfreesplitres(v,vlen);
586 goto badfmt;
587 }
588 } else {
589 val = strtoll(v[j], &eptr, 10);
590 if (eptr[0] != '\0' || val < 0) {
591 sdsfreesplitres(v,vlen);
592 goto badfmt;
593 }
594 }
595 }
596 /* Finally set the new config */
597 for (j = 0; j < vlen; j += 4) {
598 int class;
599 unsigned long long hard, soft;
600 int soft_seconds;
601
602 class = getClientLimitClassByName(v[j]);
603 hard = strtoll(v[j+1],NULL,10);
604 soft = strtoll(v[j+2],NULL,10);
605 soft_seconds = strtoll(v[j+3],NULL,10);
606
607 server.client_obuf_limits[class].hard_limit_bytes = hard;
608 server.client_obuf_limits[class].soft_limit_bytes = soft;
609 server.client_obuf_limits[class].soft_limit_seconds = soft_seconds;
610 }
611 sdsfreesplitres(v,vlen);
4d3bbf35 612 } else if (!strcasecmp(c->argv[2]->ptr,"stop-writes-on-bgsave-error")) {
613 int yn = yesnotoi(o->ptr);
7fe8d49a 614
4d3bbf35 615 if (yn == -1) goto badfmt;
616 server.stop_writes_on_bgsave_err = yn;
e2641e09 617 } else {
3ab20376
PN
618 addReplyErrorFormat(c,"Unsupported CONFIG parameter: %s",
619 (char*)c->argv[2]->ptr);
e2641e09 620 return;
621 }
e2641e09 622 addReply(c,shared.ok);
623 return;
624
625badfmt: /* Bad format errors */
3ab20376 626 addReplyErrorFormat(c,"Invalid argument '%s' for CONFIG SET '%s'",
e2641e09 627 (char*)o->ptr,
3ab20376 628 (char*)c->argv[2]->ptr);
e2641e09 629}
630
631void configGetCommand(redisClient *c) {
a375b077 632 robj *o = c->argv[2];
b301c1fc 633 void *replylen = addDeferredMultiBulkLength(c);
e2641e09 634 char *pattern = o->ptr;
a3687929 635 char buf[128];
e2641e09 636 int matches = 0;
eab0e26e 637 redisAssertWithInfo(c,o,o->encoding == REDIS_ENCODING_RAW);
e2641e09 638
21cda08b 639 if (stringmatch(pattern,"dir",0)) {
640 char buf[1024];
641
bb831c31 642 if (getcwd(buf,sizeof(buf)) == NULL)
09d2abdc 643 buf[0] = '\0';
bb831c31
PN
644
645 addReplyBulkCString(c,"dir");
646 addReplyBulkCString(c,buf);
21cda08b 647 matches++;
648 }
e2641e09 649 if (stringmatch(pattern,"dbfilename",0)) {
650 addReplyBulkCString(c,"dbfilename");
f48cd4b9 651 addReplyBulkCString(c,server.rdb_filename);
e2641e09 652 matches++;
653 }
654 if (stringmatch(pattern,"requirepass",0)) {
655 addReplyBulkCString(c,"requirepass");
656 addReplyBulkCString(c,server.requirepass);
657 matches++;
658 }
659 if (stringmatch(pattern,"masterauth",0)) {
660 addReplyBulkCString(c,"masterauth");
661 addReplyBulkCString(c,server.masterauth);
662 matches++;
663 }
664 if (stringmatch(pattern,"maxmemory",0)) {
a3687929 665 ll2string(buf,sizeof(buf),server.maxmemory);
e2641e09 666 addReplyBulkCString(c,"maxmemory");
667 addReplyBulkCString(c,buf);
668 matches++;
669 }
165346ca 670 if (stringmatch(pattern,"maxmemory-policy",0)) {
671 char *s;
672
673 switch(server.maxmemory_policy) {
674 case REDIS_MAXMEMORY_VOLATILE_LRU: s = "volatile-lru"; break;
675 case REDIS_MAXMEMORY_VOLATILE_TTL: s = "volatile-ttl"; break;
676 case REDIS_MAXMEMORY_VOLATILE_RANDOM: s = "volatile-random"; break;
677 case REDIS_MAXMEMORY_ALLKEYS_LRU: s = "allkeys-lru"; break;
678 case REDIS_MAXMEMORY_ALLKEYS_RANDOM: s = "allkeys-random"; break;
5402c426 679 case REDIS_MAXMEMORY_NO_EVICTION: s = "noeviction"; break;
165346ca 680 default: s = "unknown"; break; /* too harmless to panic */
681 }
682 addReplyBulkCString(c,"maxmemory-policy");
683 addReplyBulkCString(c,s);
684 matches++;
685 }
a3687929 686 if (stringmatch(pattern,"maxmemory-samples",0)) {
687 ll2string(buf,sizeof(buf),server.maxmemory_samples);
688 addReplyBulkCString(c,"maxmemory-samples");
689 addReplyBulkCString(c,buf);
690 matches++;
691 }
e2641e09 692 if (stringmatch(pattern,"timeout",0)) {
a3687929 693 ll2string(buf,sizeof(buf),server.maxidletime);
e2641e09 694 addReplyBulkCString(c,"timeout");
695 addReplyBulkCString(c,buf);
696 matches++;
697 }
698 if (stringmatch(pattern,"appendonly",0)) {
699 addReplyBulkCString(c,"appendonly");
e394114d 700 addReplyBulkCString(c,server.aof_state == REDIS_AOF_OFF ? "no" : "yes");
e2641e09 701 matches++;
702 }
703 if (stringmatch(pattern,"no-appendfsync-on-rewrite",0)) {
704 addReplyBulkCString(c,"no-appendfsync-on-rewrite");
2c915bcf 705 addReplyBulkCString(c,server.aof_no_fsync_on_rewrite ? "yes" : "no");
e2641e09 706 matches++;
707 }
708 if (stringmatch(pattern,"appendfsync",0)) {
709 char *policy;
710
2c915bcf 711 switch(server.aof_fsync) {
712 case AOF_FSYNC_NO: policy = "no"; break;
713 case AOF_FSYNC_EVERYSEC: policy = "everysec"; break;
714 case AOF_FSYNC_ALWAYS: policy = "always"; break;
e2641e09 715 default: policy = "unknown"; break; /* too harmless to panic */
716 }
717 addReplyBulkCString(c,"appendfsync");
718 addReplyBulkCString(c,policy);
719 matches++;
720 }
721 if (stringmatch(pattern,"save",0)) {
722 sds buf = sdsempty();
723 int j;
724
725 for (j = 0; j < server.saveparamslen; j++) {
726 buf = sdscatprintf(buf,"%ld %d",
727 server.saveparams[j].seconds,
728 server.saveparams[j].changes);
729 if (j != server.saveparamslen-1)
730 buf = sdscatlen(buf," ",1);
731 }
732 addReplyBulkCString(c,"save");
733 addReplyBulkCString(c,buf);
734 sdsfree(buf);
735 matches++;
736 }
0ec93220 737 if (stringmatch(pattern,"auto-aof-rewrite-percentage",0)) {
738 addReplyBulkCString(c,"auto-aof-rewrite-percentage");
2c915bcf 739 addReplyBulkLongLong(c,server.aof_rewrite_perc);
0ec93220 740 matches++;
741 }
742 if (stringmatch(pattern,"auto-aof-rewrite-min-size",0)) {
743 addReplyBulkCString(c,"auto-aof-rewrite-min-size");
2c915bcf 744 addReplyBulkLongLong(c,server.aof_rewrite_min_size);
0ec93220 745 matches++;
746 }
4ebfc455 747 if (stringmatch(pattern,"slave-serve-stale-data",0)) {
748 addReplyBulkCString(c,"slave-serve-stale-data");
749 addReplyBulkCString(c,server.repl_serve_stale_data ? "yes" : "no");
750 matches++;
751 }
465b4189 752 if (stringmatch(pattern,"hash-max-zipmap-entries",0)) {
753 addReplyBulkCString(c,"hash-max-zipmap-entries");
754 addReplyBulkLongLong(c,server.hash_max_zipmap_entries);
755 matches++;
756 }
757 if (stringmatch(pattern,"hash-max-zipmap-value",0)) {
758 addReplyBulkCString(c,"hash-max-zipmap-value");
759 addReplyBulkLongLong(c,server.hash_max_zipmap_value);
760 matches++;
761 }
762 if (stringmatch(pattern,"list-max-ziplist-entries",0)) {
763 addReplyBulkCString(c,"list-max-ziplist-entries");
764 addReplyBulkLongLong(c,server.list_max_ziplist_entries);
765 matches++;
766 }
767 if (stringmatch(pattern,"list-max-ziplist-value",0)) {
768 addReplyBulkCString(c,"list-max-ziplist-value");
769 addReplyBulkLongLong(c,server.list_max_ziplist_value);
770 matches++;
771 }
772 if (stringmatch(pattern,"set-max-intset-entries",0)) {
773 addReplyBulkCString(c,"set-max-intset-entries");
774 addReplyBulkLongLong(c,server.set_max_intset_entries);
775 matches++;
776 }
3ea204e1
PN
777 if (stringmatch(pattern,"zset-max-ziplist-entries",0)) {
778 addReplyBulkCString(c,"zset-max-ziplist-entries");
779 addReplyBulkLongLong(c,server.zset_max_ziplist_entries);
780 matches++;
781 }
782 if (stringmatch(pattern,"zset-max-ziplist-value",0)) {
783 addReplyBulkCString(c,"zset-max-ziplist-value");
784 addReplyBulkLongLong(c,server.zset_max_ziplist_value);
785 matches++;
786 }
eeffcf38 787 if (stringmatch(pattern,"lua-time-limit",0)) {
788 addReplyBulkCString(c,"lua-time-limit");
789 addReplyBulkLongLong(c,server.lua_time_limit);
5f546194 790 matches++;
791 }
35a60441 792 if (stringmatch(pattern,"slowlog-log-slower-than",0)) {
793 addReplyBulkCString(c,"slowlog-log-slower-than");
794 addReplyBulkLongLong(c,server.slowlog_log_slower_than);
795 matches++;
796 }
797 if (stringmatch(pattern,"slowlog-max-len",0)) {
798 addReplyBulkCString(c,"slowlog-max-len");
799 addReplyBulkLongLong(c,server.slowlog_max_len);
eeffcf38 800 matches++;
801 }
2aa26d2a 802 if (stringmatch(pattern,"loglevel",0)) {
803 char *s;
804
805 switch(server.verbosity) {
806 case REDIS_WARNING: s = "warning"; break;
807 case REDIS_VERBOSE: s = "verbose"; break;
808 case REDIS_NOTICE: s = "notice"; break;
809 case REDIS_DEBUG: s = "debug"; break;
810 default: s = "unknown"; break; /* too harmless to panic */
811 }
812 addReplyBulkCString(c,"loglevel");
813 addReplyBulkCString(c,s);
814 matches++;
815 }
7fe8d49a 816 if (stringmatch(pattern,"client-output-buffer-limit",0)) {
817 sds buf = sdsempty();
818 int j;
819
820 for (j = 0; j < REDIS_CLIENT_LIMIT_NUM_CLASSES; j++) {
821 buf = sdscatprintf(buf,"%s %llu %llu %ld",
822 getClientLimitClassName(j),
823 server.client_obuf_limits[j].hard_limit_bytes,
824 server.client_obuf_limits[j].soft_limit_bytes,
825 (long) server.client_obuf_limits[j].soft_limit_seconds);
826 if (j != REDIS_CLIENT_LIMIT_NUM_CLASSES-1)
827 buf = sdscatlen(buf," ",1);
828 }
829 addReplyBulkCString(c,"client-output-buffer-limit");
830 addReplyBulkCString(c,buf);
831 sdsfree(buf);
832 matches++;
833 }
4d3bbf35 834 if (stringmatch(pattern,"stop-writes-on-bgsave-error",0)) {
835 addReplyBulkCString(c,"stop-writes-on-bgsave-error");
836 addReplyBulkCString(c,server.stop_writes_on_bgsave_err ? "yes" : "no");
837 matches++;
838 }
b301c1fc 839 setDeferredMultiBulkLength(c,replylen,matches*2);
e2641e09 840}
841
842void configCommand(redisClient *c) {
843 if (!strcasecmp(c->argv[1]->ptr,"set")) {
844 if (c->argc != 4) goto badarity;
845 configSetCommand(c);
846 } else if (!strcasecmp(c->argv[1]->ptr,"get")) {
847 if (c->argc != 3) goto badarity;
848 configGetCommand(c);
849 } else if (!strcasecmp(c->argv[1]->ptr,"resetstat")) {
850 if (c->argc != 2) goto badarity;
9f8ded8c 851 server.stat_keyspace_hits = 0;
852 server.stat_keyspace_misses = 0;
e2641e09 853 server.stat_numcommands = 0;
854 server.stat_numconnections = 0;
855 server.stat_expiredkeys = 0;
d7ed7fd2 856 resetCommandTableStats();
e2641e09 857 addReply(c,shared.ok);
858 } else {
3ab20376
PN
859 addReplyError(c,
860 "CONFIG subcommand must be one of GET, SET, RESETSTAT");
e2641e09 861 }
862 return;
863
864badarity:
3ab20376
PN
865 addReplyErrorFormat(c,"Wrong number of arguments for CONFIG %s",
866 (char*) c->argv[1]->ptr);
e2641e09 867}