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