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