]> git.saurik.com Git - redis.git/blame - src/config.c
Added missing license and copyright in deps/hiredis.
[redis.git] / src / config.c
CommitLineData
d288ee65 1/* Configuration file parsing and CONFIG GET/SET commands implementation.
2 *
3 * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * * Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * * Neither the name of Redis nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without
16 * specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30
31
e2641e09 32#include "redis.h"
33
34/*-----------------------------------------------------------------------------
35 * Config file parsing
36 *----------------------------------------------------------------------------*/
37
38int yesnotoi(char *s) {
39 if (!strcasecmp(s,"yes")) return 1;
40 else if (!strcasecmp(s,"no")) return 0;
41 else return -1;
42}
43
44void appendServerSaveParams(time_t seconds, int changes) {
45 server.saveparams = zrealloc(server.saveparams,sizeof(struct saveparam)*(server.saveparamslen+1));
46 server.saveparams[server.saveparamslen].seconds = seconds;
47 server.saveparams[server.saveparamslen].changes = changes;
48 server.saveparamslen++;
49}
50
51void resetServerSaveParams() {
52 zfree(server.saveparams);
53 server.saveparams = NULL;
54 server.saveparamslen = 0;
55}
56
67c6f0f6 57void loadServerConfigFromString(char *config) {
58 char *err = NULL;
59 int linenum = 0, totlines, i;
60 sds *lines;
e2641e09 61
67c6f0f6 62 lines = sdssplitlen(config,strlen(config),"\n",1,&totlines);
e2641e09 63
67c6f0f6 64 for (i = 0; i < totlines; i++) {
e2641e09 65 sds *argv;
67c6f0f6 66 int argc;
e2641e09 67
67c6f0f6 68 linenum = i+1;
69 lines[i] = sdstrim(lines[i]," \t\r\n");
e2641e09 70
71 /* Skip comments and blank lines*/
67c6f0f6 72 if (lines[i][0] == '#' || lines[i][0] == '\0') continue;
e2641e09 73
74 /* Split into arguments */
67c6f0f6 75 argv = sdssplitargs(lines[i],&argc);
e2641e09 76 sdstolower(argv[0]);
77
78 /* Execute config directives */
79 if (!strcasecmp(argv[0],"timeout") && argc == 2) {
80 server.maxidletime = atoi(argv[1]);
81 if (server.maxidletime < 0) {
82 err = "Invalid timeout value"; goto loaderr;
83 }
84 } else if (!strcasecmp(argv[0],"port") && argc == 2) {
85 server.port = atoi(argv[1]);
df50d707 86 if (server.port < 0 || server.port > 65535) {
e2641e09 87 err = "Invalid port"; goto loaderr;
88 }
89 } else if (!strcasecmp(argv[0],"bind") && argc == 2) {
90 server.bindaddr = zstrdup(argv[1]);
5d10923f
PN
91 } else if (!strcasecmp(argv[0],"unixsocket") && argc == 2) {
92 server.unixsocket = zstrdup(argv[1]);
85238765 93 } else if (!strcasecmp(argv[0],"unixsocketperm") && argc == 2) {
bb48c5fa 94 errno = 0;
85238765
NF
95 server.unixsocketperm = (mode_t)strtol(argv[1], NULL, 8);
96 if (errno || server.unixsocketperm > 0777) {
97 err = "Invalid socket file permissions"; goto loaderr;
98 }
4aac3ff2 99 } else if (!strcasecmp(argv[0],"save")) {
100 if (argc == 3) {
101 int seconds = atoi(argv[1]);
102 int changes = atoi(argv[2]);
103 if (seconds < 1 || changes < 0) {
104 err = "Invalid save parameters"; goto loaderr;
105 }
106 appendServerSaveParams(seconds,changes);
107 } else if (argc == 2 && !strcasecmp(argv[1],"")) {
108 resetServerSaveParams();
e2641e09 109 }
e2641e09 110 } else if (!strcasecmp(argv[0],"dir") && argc == 2) {
111 if (chdir(argv[1]) == -1) {
112 redisLog(REDIS_WARNING,"Can't chdir to '%s': %s",
113 argv[1], strerror(errno));
114 exit(1);
115 }
116 } else if (!strcasecmp(argv[0],"loglevel") && argc == 2) {
117 if (!strcasecmp(argv[1],"debug")) server.verbosity = REDIS_DEBUG;
118 else if (!strcasecmp(argv[1],"verbose")) server.verbosity = REDIS_VERBOSE;
119 else if (!strcasecmp(argv[1],"notice")) server.verbosity = REDIS_NOTICE;
120 else if (!strcasecmp(argv[1],"warning")) server.verbosity = REDIS_WARNING;
121 else {
122 err = "Invalid log level. Must be one of debug, notice, warning";
123 goto loaderr;
124 }
125 } else if (!strcasecmp(argv[0],"logfile") && argc == 2) {
126 FILE *logfp;
127
128 server.logfile = zstrdup(argv[1]);
129 if (!strcasecmp(server.logfile,"stdout")) {
130 zfree(server.logfile);
131 server.logfile = NULL;
132 }
133 if (server.logfile) {
134 /* Test if we are able to open the file. The server will not
135 * be able to abort just for this problem later... */
136 logfp = fopen(server.logfile,"a");
137 if (logfp == NULL) {
138 err = sdscatprintf(sdsempty(),
139 "Can't open the log file: %s", strerror(errno));
140 goto loaderr;
141 }
142 fclose(logfp);
143 }
e1a586ee
JH
144 } else if (!strcasecmp(argv[0],"syslog-enabled") && argc == 2) {
145 if ((server.syslog_enabled = yesnotoi(argv[1])) == -1) {
146 err = "argument must be 'yes' or 'no'"; goto loaderr;
147 }
148 } else if (!strcasecmp(argv[0],"syslog-ident") && argc == 2) {
149 if (server.syslog_ident) zfree(server.syslog_ident);
150 server.syslog_ident = zstrdup(argv[1]);
151 } else if (!strcasecmp(argv[0],"syslog-facility") && argc == 2) {
152 struct {
153 const char *name;
154 const int value;
155 } validSyslogFacilities[] = {
156 {"user", LOG_USER},
157 {"local0", LOG_LOCAL0},
158 {"local1", LOG_LOCAL1},
159 {"local2", LOG_LOCAL2},
160 {"local3", LOG_LOCAL3},
161 {"local4", LOG_LOCAL4},
162 {"local5", LOG_LOCAL5},
163 {"local6", LOG_LOCAL6},
164 {"local7", LOG_LOCAL7},
165 {NULL, 0}
166 };
167 int i;
168
169 for (i = 0; validSyslogFacilities[i].name; i++) {
170 if (!strcasecmp(validSyslogFacilities[i].name, argv[1])) {
171 server.syslog_facility = validSyslogFacilities[i].value;
172 break;
173 }
174 }
175
176 if (!validSyslogFacilities[i].name) {
177 err = "Invalid log facility. Must be one of USER or between LOCAL0-LOCAL7";
178 goto loaderr;
179 }
e2641e09 180 } else if (!strcasecmp(argv[0],"databases") && argc == 2) {
181 server.dbnum = atoi(argv[1]);
182 if (server.dbnum < 1) {
183 err = "Invalid number of databases"; goto loaderr;
184 }
185 } else if (!strcasecmp(argv[0],"include") && argc == 2) {
67c6f0f6 186 loadServerConfig(argv[1],NULL);
e2641e09 187 } else if (!strcasecmp(argv[0],"maxclients") && argc == 2) {
188 server.maxclients = atoi(argv[1]);
727d6dd5 189 if (server.maxclients < 1) {
190 err = "Invalid max clients limit"; goto loaderr;
191 }
e2641e09 192 } else if (!strcasecmp(argv[0],"maxmemory") && argc == 2) {
193 server.maxmemory = memtoll(argv[1],NULL);
165346ca 194 } else if (!strcasecmp(argv[0],"maxmemory-policy") && argc == 2) {
195 if (!strcasecmp(argv[1],"volatile-lru")) {
196 server.maxmemory_policy = REDIS_MAXMEMORY_VOLATILE_LRU;
197 } else if (!strcasecmp(argv[1],"volatile-random")) {
198 server.maxmemory_policy = REDIS_MAXMEMORY_VOLATILE_RANDOM;
199 } else if (!strcasecmp(argv[1],"volatile-ttl")) {
200 server.maxmemory_policy = REDIS_MAXMEMORY_VOLATILE_TTL;
201 } else if (!strcasecmp(argv[1],"allkeys-lru")) {
202 server.maxmemory_policy = REDIS_MAXMEMORY_ALLKEYS_LRU;
203 } else if (!strcasecmp(argv[1],"allkeys-random")) {
204 server.maxmemory_policy = REDIS_MAXMEMORY_ALLKEYS_RANDOM;
5402c426 205 } else if (!strcasecmp(argv[1],"noeviction")) {
206 server.maxmemory_policy = REDIS_MAXMEMORY_NO_EVICTION;
165346ca 207 } else {
208 err = "Invalid maxmemory policy";
209 goto loaderr;
210 }
a3687929 211 } else if (!strcasecmp(argv[0],"maxmemory-samples") && argc == 2) {
212 server.maxmemory_samples = atoi(argv[1]);
213 if (server.maxmemory_samples <= 0) {
214 err = "maxmemory-samples must be 1 or greater";
215 goto loaderr;
216 }
e2641e09 217 } else if (!strcasecmp(argv[0],"slaveof") && argc == 3) {
218 server.masterhost = sdsnew(argv[1]);
219 server.masterport = atoi(argv[2]);
1844f990 220 server.repl_state = REDIS_REPL_CONNECT;
8996bf77 221 } else if (!strcasecmp(argv[0],"repl-ping-slave-period") && argc == 2) {
222 server.repl_ping_slave_period = atoi(argv[1]);
223 if (server.repl_ping_slave_period <= 0) {
224 err = "repl-ping-slave-period must be 1 or greater";
225 goto loaderr;
226 }
227 } else if (!strcasecmp(argv[0],"repl-timeout") && argc == 2) {
228 server.repl_timeout = atoi(argv[1]);
229 if (server.repl_timeout <= 0) {
230 err = "repl-timeout must be 1 or greater";
231 goto loaderr;
232 }
e2641e09 233 } else if (!strcasecmp(argv[0],"masterauth") && argc == 2) {
234 server.masterauth = zstrdup(argv[1]);
4ebfc455 235 } else if (!strcasecmp(argv[0],"slave-serve-stale-data") && argc == 2) {
236 if ((server.repl_serve_stale_data = yesnotoi(argv[1])) == -1) {
237 err = "argument must be 'yes' or 'no'"; goto loaderr;
238 }
05406168 239 } else if (!strcasecmp(argv[0],"slave-read-only") && argc == 2) {
240 if ((server.repl_slave_ro = yesnotoi(argv[1])) == -1) {
241 err = "argument must be 'yes' or 'no'"; goto loaderr;
242 }
e2641e09 243 } else if (!strcasecmp(argv[0],"rdbcompression") && argc == 2) {
f48cd4b9 244 if ((server.rdb_compression = yesnotoi(argv[1])) == -1) {
e2641e09 245 err = "argument must be 'yes' or 'no'"; goto loaderr;
246 }
39d1e350 247 } else if (!strcasecmp(argv[0],"rdbchecksum") && argc == 2) {
248 if ((server.rdb_checksum = yesnotoi(argv[1])) == -1) {
249 err = "argument must be 'yes' or 'no'"; goto loaderr;
250 }
e2641e09 251 } else if (!strcasecmp(argv[0],"activerehashing") && argc == 2) {
252 if ((server.activerehashing = yesnotoi(argv[1])) == -1) {
253 err = "argument must be 'yes' or 'no'"; goto loaderr;
254 }
255 } else if (!strcasecmp(argv[0],"daemonize") && argc == 2) {
256 if ((server.daemonize = yesnotoi(argv[1])) == -1) {
257 err = "argument must be 'yes' or 'no'"; goto loaderr;
258 }
259 } else if (!strcasecmp(argv[0],"appendonly") && argc == 2) {
e394114d 260 int yes;
261
262 if ((yes = yesnotoi(argv[1])) == -1) {
e2641e09 263 err = "argument must be 'yes' or 'no'"; goto loaderr;
264 }
e394114d 265 server.aof_state = yes ? REDIS_AOF_ON : REDIS_AOF_OFF;
e2641e09 266 } else if (!strcasecmp(argv[0],"appendfilename") && argc == 2) {
2c915bcf 267 zfree(server.aof_filename);
268 server.aof_filename = zstrdup(argv[1]);
e2641e09 269 } else if (!strcasecmp(argv[0],"no-appendfsync-on-rewrite")
270 && argc == 2) {
2c915bcf 271 if ((server.aof_no_fsync_on_rewrite= yesnotoi(argv[1])) == -1) {
e2641e09 272 err = "argument must be 'yes' or 'no'"; goto loaderr;
273 }
274 } else if (!strcasecmp(argv[0],"appendfsync") && argc == 2) {
275 if (!strcasecmp(argv[1],"no")) {
2c915bcf 276 server.aof_fsync = AOF_FSYNC_NO;
e2641e09 277 } else if (!strcasecmp(argv[1],"always")) {
2c915bcf 278 server.aof_fsync = AOF_FSYNC_ALWAYS;
e2641e09 279 } else if (!strcasecmp(argv[1],"everysec")) {
2c915bcf 280 server.aof_fsync = AOF_FSYNC_EVERYSEC;
e2641e09 281 } else {
282 err = "argument must be 'no', 'always' or 'everysec'";
283 goto loaderr;
284 }
b333e239 285 } else if (!strcasecmp(argv[0],"auto-aof-rewrite-percentage") &&
286 argc == 2)
287 {
2c915bcf 288 server.aof_rewrite_perc = atoi(argv[1]);
289 if (server.aof_rewrite_perc < 0) {
b333e239 290 err = "Invalid negative percentage for AOF auto rewrite";
291 goto loaderr;
292 }
293 } else if (!strcasecmp(argv[0],"auto-aof-rewrite-min-size") &&
294 argc == 2)
295 {
2c915bcf 296 server.aof_rewrite_min_size = memtoll(argv[1],NULL);
e2641e09 297 } else if (!strcasecmp(argv[0],"requirepass") && argc == 2) {
4b3865cb 298 if (strlen(argv[1]) > REDIS_AUTHPASS_MAX_LEN) {
299 err = "Password is longer than REDIS_AUTHPASS_MAX_LEN";
300 goto loaderr;
301 }
e2641e09 302 server.requirepass = zstrdup(argv[1]);
303 } else if (!strcasecmp(argv[0],"pidfile") && argc == 2) {
304 zfree(server.pidfile);
305 server.pidfile = zstrdup(argv[1]);
306 } else if (!strcasecmp(argv[0],"dbfilename") && argc == 2) {
f48cd4b9 307 zfree(server.rdb_filename);
308 server.rdb_filename = zstrdup(argv[1]);
ebd85e9a
PN
309 } else if (!strcasecmp(argv[0],"hash-max-ziplist-entries") && argc == 2) {
310 server.hash_max_ziplist_entries = memtoll(argv[1], NULL);
311 } else if (!strcasecmp(argv[0],"hash-max-ziplist-value") && argc == 2) {
312 server.hash_max_ziplist_value = memtoll(argv[1], NULL);
e2641e09 313 } else if (!strcasecmp(argv[0],"list-max-ziplist-entries") && argc == 2){
314 server.list_max_ziplist_entries = memtoll(argv[1], NULL);
8d3e063a 315 } else if (!strcasecmp(argv[0],"list-max-ziplist-value") && argc == 2) {
e2641e09 316 server.list_max_ziplist_value = memtoll(argv[1], NULL);
8d3e063a 317 } else if (!strcasecmp(argv[0],"set-max-intset-entries") && argc == 2) {
96ffb2fe 318 server.set_max_intset_entries = memtoll(argv[1], NULL);
3ea204e1
PN
319 } else if (!strcasecmp(argv[0],"zset-max-ziplist-entries") && argc == 2) {
320 server.zset_max_ziplist_entries = memtoll(argv[1], NULL);
321 } else if (!strcasecmp(argv[0],"zset-max-ziplist-value") && argc == 2) {
322 server.zset_max_ziplist_value = memtoll(argv[1], NULL);
8d3e063a 323 } else if (!strcasecmp(argv[0],"rename-command") && argc == 3) {
324 struct redisCommand *cmd = lookupCommand(argv[1]);
325 int retval;
326
327 if (!cmd) {
328 err = "No such command in rename-command";
329 goto loaderr;
330 }
331
332 /* If the target command name is the emtpy string we just
333 * remove it from the command table. */
334 retval = dictDelete(server.commands, argv[1]);
335 redisAssert(retval == DICT_OK);
336
337 /* Otherwise we re-add the command under a different name. */
338 if (sdslen(argv[2]) != 0) {
339 sds copy = sdsdup(argv[2]);
340
341 retval = dictAdd(server.commands, copy, cmd);
342 if (retval != DICT_OK) {
343 sdsfree(copy);
344 err = "Target command name already exists"; goto loaderr;
345 }
346 }
eeffcf38 347 } else if (!strcasecmp(argv[0],"lua-time-limit") && argc == 2) {
348 server.lua_time_limit = strtoll(argv[1],NULL,10);
35a60441 349 } else if (!strcasecmp(argv[0],"slowlog-log-slower-than") &&
350 argc == 2)
351 {
352 server.slowlog_log_slower_than = strtoll(argv[1],NULL,10);
353 } else if (!strcasecmp(argv[0],"slowlog-max-len") && argc == 2) {
354 server.slowlog_max_len = strtoll(argv[1],NULL,10);
7fe8d49a 355 } else if (!strcasecmp(argv[0],"client-output-buffer-limit") &&
356 argc == 5)
357 {
358 int class = getClientLimitClassByName(argv[1]);
359 unsigned long long hard, soft;
360 int soft_seconds;
361
362 if (class == -1) {
363 err = "Unrecognized client limit class";
364 goto loaderr;
365 }
366 hard = memtoll(argv[2],NULL);
367 soft = memtoll(argv[3],NULL);
368 soft_seconds = atoi(argv[4]);
369 if (soft_seconds < 0) {
370 err = "Negative number of seconds in soft limt is invalid";
371 goto loaderr;
372 }
373 server.client_obuf_limits[class].hard_limit_bytes = hard;
374 server.client_obuf_limits[class].soft_limit_bytes = soft;
375 server.client_obuf_limits[class].soft_limit_seconds = soft_seconds;
4d3bbf35 376 } else if (!strcasecmp(argv[0],"stop-writes-on-bgsave-error") &&
377 argc == 2) {
378 if ((server.stop_writes_on_bgsave_err = yesnotoi(argv[1])) == -1) {
379 err = "argument must be 'yes' or 'no'"; goto loaderr;
380 }
48d26a48 381 } else if (!strcasecmp(argv[0],"slave-priority") && argc == 2) {
382 server.slave_priority = atoi(argv[1]);
120ba392 383 } else if (!strcasecmp(argv[0],"sentinel")) {
384 /* argc == 1 is handled by main() as we need to enter the sentinel
385 * mode ASAP. */
386 if (argc != 1) {
387 if (!server.sentinel_mode) {
388 err = "sentinel directive while not in sentinel mode";
389 goto loaderr;
390 }
391 err = sentinelHandleConfiguration(argv+1,argc-1);
392 if (err) goto loaderr;
393 }
e2641e09 394 } else {
395 err = "Bad directive or wrong number of arguments"; goto loaderr;
396 }
67c6f0f6 397 sdsfreesplitres(argv,argc);
e2641e09 398 }
67c6f0f6 399 sdsfreesplitres(lines,totlines);
e2641e09 400 return;
401
402loaderr:
403 fprintf(stderr, "\n*** FATAL CONFIG FILE ERROR ***\n");
404 fprintf(stderr, "Reading the configuration file, at line %d\n", linenum);
67c6f0f6 405 fprintf(stderr, ">>> '%s'\n", lines[i]);
e2641e09 406 fprintf(stderr, "%s\n", err);
407 exit(1);
408}
409
67c6f0f6 410/* Load the server configuration from the specified filename.
411 * The function appends the additional configuration directives stored
412 * in the 'options' string to the config file before loading.
413 *
414 * Both filename and options can be NULL, in such a case are considered
415 * emtpy. This way loadServerConfig can be used to just load a file or
416 * just load a string. */
417void loadServerConfig(char *filename, char *options) {
418 sds config = sdsempty();
419 char buf[REDIS_CONFIGLINE_MAX+1];
420
421 /* Load the file content */
422 if (filename) {
423 FILE *fp;
424
425 if (filename[0] == '-' && filename[1] == '\0') {
426 fp = stdin;
427 } else {
428 if ((fp = fopen(filename,"r")) == NULL) {
429 redisLog(REDIS_WARNING,
430 "Fatal error, can't open config file '%s'", filename);
431 exit(1);
432 }
433 }
434 while(fgets(buf,REDIS_CONFIGLINE_MAX+1,fp) != NULL)
435 config = sdscat(config,buf);
436 if (fp != stdin) fclose(fp);
437 }
438 /* Append the additional options */
439 if (options) {
440 config = sdscat(config,"\n");
441 config = sdscat(config,options);
442 }
443 loadServerConfigFromString(config);
444 sdsfree(config);
445}
446
e2641e09 447/*-----------------------------------------------------------------------------
448 * CONFIG command for remote configuration
449 *----------------------------------------------------------------------------*/
450
451void configSetCommand(redisClient *c) {
a375b077 452 robj *o;
e2641e09 453 long long ll;
eab0e26e 454 redisAssertWithInfo(c,c->argv[2],c->argv[2]->encoding == REDIS_ENCODING_RAW);
455 redisAssertWithInfo(c,c->argv[2],c->argv[3]->encoding == REDIS_ENCODING_RAW);
a375b077 456 o = c->argv[3];
e2641e09 457
458 if (!strcasecmp(c->argv[2]->ptr,"dbfilename")) {
f48cd4b9 459 zfree(server.rdb_filename);
460 server.rdb_filename = zstrdup(o->ptr);
e2641e09 461 } else if (!strcasecmp(c->argv[2]->ptr,"requirepass")) {
4b3865cb 462 if (sdslen(o->ptr) > REDIS_AUTHPASS_MAX_LEN) goto badfmt;
e2641e09 463 zfree(server.requirepass);
1eceb85e 464 server.requirepass = ((char*)o->ptr)[0] ? zstrdup(o->ptr) : NULL;
e2641e09 465 } else if (!strcasecmp(c->argv[2]->ptr,"masterauth")) {
466 zfree(server.masterauth);
467 server.masterauth = zstrdup(o->ptr);
468 } else if (!strcasecmp(c->argv[2]->ptr,"maxmemory")) {
469 if (getLongLongFromObject(o,&ll) == REDIS_ERR ||
470 ll < 0) goto badfmt;
471 server.maxmemory = ll;
05e06e15 472 if (server.maxmemory) {
473 if (server.maxmemory < zmalloc_used_memory()) {
474 redisLog(REDIS_WARNING,"WARNING: the new maxmemory value set via CONFIG SET is smaller than the current memory usage. This will result in keys eviction and/or inability to accept new write commands depending on the maxmemory-policy.");
475 }
476 freeMemoryIfNeeded();
477 }
165346ca 478 } else if (!strcasecmp(c->argv[2]->ptr,"maxmemory-policy")) {
479 if (!strcasecmp(o->ptr,"volatile-lru")) {
480 server.maxmemory_policy = REDIS_MAXMEMORY_VOLATILE_LRU;
481 } else if (!strcasecmp(o->ptr,"volatile-random")) {
482 server.maxmemory_policy = REDIS_MAXMEMORY_VOLATILE_RANDOM;
483 } else if (!strcasecmp(o->ptr,"volatile-ttl")) {
484 server.maxmemory_policy = REDIS_MAXMEMORY_VOLATILE_TTL;
485 } else if (!strcasecmp(o->ptr,"allkeys-lru")) {
486 server.maxmemory_policy = REDIS_MAXMEMORY_ALLKEYS_LRU;
487 } else if (!strcasecmp(o->ptr,"allkeys-random")) {
488 server.maxmemory_policy = REDIS_MAXMEMORY_ALLKEYS_RANDOM;
5402c426 489 } else if (!strcasecmp(o->ptr,"noeviction")) {
490 server.maxmemory_policy = REDIS_MAXMEMORY_NO_EVICTION;
165346ca 491 } else {
492 goto badfmt;
493 }
a3687929 494 } else if (!strcasecmp(c->argv[2]->ptr,"maxmemory-samples")) {
495 if (getLongLongFromObject(o,&ll) == REDIS_ERR ||
496 ll <= 0) goto badfmt;
497 server.maxmemory_samples = ll;
e2641e09 498 } else if (!strcasecmp(c->argv[2]->ptr,"timeout")) {
499 if (getLongLongFromObject(o,&ll) == REDIS_ERR ||
500 ll < 0 || ll > LONG_MAX) goto badfmt;
501 server.maxidletime = ll;
502 } else if (!strcasecmp(c->argv[2]->ptr,"appendfsync")) {
503 if (!strcasecmp(o->ptr,"no")) {
2c915bcf 504 server.aof_fsync = AOF_FSYNC_NO;
e2641e09 505 } else if (!strcasecmp(o->ptr,"everysec")) {
2c915bcf 506 server.aof_fsync = AOF_FSYNC_EVERYSEC;
e2641e09 507 } else if (!strcasecmp(o->ptr,"always")) {
2c915bcf 508 server.aof_fsync = AOF_FSYNC_ALWAYS;
e2641e09 509 } else {
510 goto badfmt;
511 }
512 } else if (!strcasecmp(c->argv[2]->ptr,"no-appendfsync-on-rewrite")) {
513 int yn = yesnotoi(o->ptr);
514
515 if (yn == -1) goto badfmt;
2c915bcf 516 server.aof_no_fsync_on_rewrite = yn;
e2641e09 517 } else if (!strcasecmp(c->argv[2]->ptr,"appendonly")) {
e394114d 518 int enable = yesnotoi(o->ptr);
519
520 if (enable == -1) goto badfmt;
521 if (enable == 0 && server.aof_state != REDIS_AOF_OFF) {
522 stopAppendOnly();
523 } else if (enable && server.aof_state == REDIS_AOF_OFF) {
524 if (startAppendOnly() == REDIS_ERR) {
525 addReplyError(c,
526 "Unable to turn on AOF. Check server logs.");
527 return;
e2641e09 528 }
529 }
0ec93220 530 } else if (!strcasecmp(c->argv[2]->ptr,"auto-aof-rewrite-percentage")) {
531 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
2c915bcf 532 server.aof_rewrite_perc = ll;
0ec93220 533 } else if (!strcasecmp(c->argv[2]->ptr,"auto-aof-rewrite-min-size")) {
534 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
2c915bcf 535 server.aof_rewrite_min_size = ll;
e2641e09 536 } else if (!strcasecmp(c->argv[2]->ptr,"save")) {
537 int vlen, j;
538 sds *v = sdssplitlen(o->ptr,sdslen(o->ptr)," ",1,&vlen);
539
540 /* Perform sanity check before setting the new config:
541 * - Even number of args
542 * - Seconds >= 1, changes >= 0 */
543 if (vlen & 1) {
544 sdsfreesplitres(v,vlen);
545 goto badfmt;
546 }
547 for (j = 0; j < vlen; j++) {
548 char *eptr;
549 long val;
550
551 val = strtoll(v[j], &eptr, 10);
552 if (eptr[0] != '\0' ||
553 ((j & 1) == 0 && val < 1) ||
554 ((j & 1) == 1 && val < 0)) {
555 sdsfreesplitres(v,vlen);
556 goto badfmt;
557 }
558 }
559 /* Finally set the new config */
560 resetServerSaveParams();
561 for (j = 0; j < vlen; j += 2) {
562 time_t seconds;
563 int changes;
564
565 seconds = strtoll(v[j],NULL,10);
566 changes = strtoll(v[j+1],NULL,10);
567 appendServerSaveParams(seconds, changes);
568 }
569 sdsfreesplitres(v,vlen);
4ebfc455 570 } else if (!strcasecmp(c->argv[2]->ptr,"slave-serve-stale-data")) {
571 int yn = yesnotoi(o->ptr);
572
573 if (yn == -1) goto badfmt;
574 server.repl_serve_stale_data = yn;
05406168 575 } else if (!strcasecmp(c->argv[2]->ptr,"slave-read-only")) {
576 int yn = yesnotoi(o->ptr);
577
578 if (yn == -1) goto badfmt;
579 server.repl_slave_ro = yn;
21cda08b 580 } else if (!strcasecmp(c->argv[2]->ptr,"dir")) {
581 if (chdir((char*)o->ptr) == -1) {
582 addReplyErrorFormat(c,"Changing directory: %s", strerror(errno));
583 return;
584 }
ebd85e9a 585 } else if (!strcasecmp(c->argv[2]->ptr,"hash-max-ziplist-entries")) {
465b4189 586 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
ebd85e9a
PN
587 server.hash_max_ziplist_entries = ll;
588 } else if (!strcasecmp(c->argv[2]->ptr,"hash-max-ziplist-value")) {
465b4189 589 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
ebd85e9a 590 server.hash_max_ziplist_value = ll;
465b4189 591 } else if (!strcasecmp(c->argv[2]->ptr,"list-max-ziplist-entries")) {
592 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
593 server.list_max_ziplist_entries = ll;
594 } else if (!strcasecmp(c->argv[2]->ptr,"list-max-ziplist-value")) {
595 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
596 server.list_max_ziplist_value = ll;
597 } else if (!strcasecmp(c->argv[2]->ptr,"set-max-intset-entries")) {
598 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
599 server.set_max_intset_entries = ll;
3ea204e1
PN
600 } else if (!strcasecmp(c->argv[2]->ptr,"zset-max-ziplist-entries")) {
601 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
602 server.zset_max_ziplist_entries = ll;
603 } else if (!strcasecmp(c->argv[2]->ptr,"zset-max-ziplist-value")) {
604 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
605 server.zset_max_ziplist_value = ll;
eeffcf38 606 } else if (!strcasecmp(c->argv[2]->ptr,"lua-time-limit")) {
607 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
608 server.lua_time_limit = ll;
35a60441 609 } else if (!strcasecmp(c->argv[2]->ptr,"slowlog-log-slower-than")) {
610 if (getLongLongFromObject(o,&ll) == REDIS_ERR) goto badfmt;
611 server.slowlog_log_slower_than = ll;
612 } else if (!strcasecmp(c->argv[2]->ptr,"slowlog-max-len")) {
613 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
614 server.slowlog_max_len = (unsigned)ll;
2aa26d2a 615 } else if (!strcasecmp(c->argv[2]->ptr,"loglevel")) {
616 if (!strcasecmp(o->ptr,"warning")) {
617 server.verbosity = REDIS_WARNING;
618 } else if (!strcasecmp(o->ptr,"notice")) {
619 server.verbosity = REDIS_NOTICE;
620 } else if (!strcasecmp(o->ptr,"verbose")) {
621 server.verbosity = REDIS_VERBOSE;
622 } else if (!strcasecmp(o->ptr,"debug")) {
623 server.verbosity = REDIS_DEBUG;
624 } else {
625 goto badfmt;
626 }
7fe8d49a 627 } else if (!strcasecmp(c->argv[2]->ptr,"client-output-buffer-limit")) {
628 int vlen, j;
629 sds *v = sdssplitlen(o->ptr,sdslen(o->ptr)," ",1,&vlen);
630
631 /* We need a multiple of 4: <class> <hard> <soft> <soft_seconds> */
632 if (vlen % 4) {
633 sdsfreesplitres(v,vlen);
634 goto badfmt;
635 }
636
637 /* Sanity check of single arguments, so that we either refuse the
638 * whole configuration string or accept it all, even if a single
639 * error in a single client class is present. */
640 for (j = 0; j < vlen; j++) {
641 char *eptr;
642 long val;
643
644 if ((j % 4) == 0) {
645 if (getClientLimitClassByName(v[j]) == -1) {
646 sdsfreesplitres(v,vlen);
647 goto badfmt;
648 }
649 } else {
650 val = strtoll(v[j], &eptr, 10);
651 if (eptr[0] != '\0' || val < 0) {
652 sdsfreesplitres(v,vlen);
653 goto badfmt;
654 }
655 }
656 }
657 /* Finally set the new config */
658 for (j = 0; j < vlen; j += 4) {
659 int class;
660 unsigned long long hard, soft;
661 int soft_seconds;
662
663 class = getClientLimitClassByName(v[j]);
664 hard = strtoll(v[j+1],NULL,10);
665 soft = strtoll(v[j+2],NULL,10);
666 soft_seconds = strtoll(v[j+3],NULL,10);
667
668 server.client_obuf_limits[class].hard_limit_bytes = hard;
669 server.client_obuf_limits[class].soft_limit_bytes = soft;
670 server.client_obuf_limits[class].soft_limit_seconds = soft_seconds;
671 }
672 sdsfreesplitres(v,vlen);
4d3bbf35 673 } else if (!strcasecmp(c->argv[2]->ptr,"stop-writes-on-bgsave-error")) {
674 int yn = yesnotoi(o->ptr);
7fe8d49a 675
4d3bbf35 676 if (yn == -1) goto badfmt;
677 server.stop_writes_on_bgsave_err = yn;
0823e48f 678 } else if (!strcasecmp(c->argv[2]->ptr,"repl-ping-slave-period")) {
679 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll <= 0) goto badfmt;
680 server.repl_ping_slave_period = ll;
681 } else if (!strcasecmp(c->argv[2]->ptr,"repl-timeout")) {
682 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll <= 0) goto badfmt;
683 server.repl_timeout = ll;
fc030ac7 684 } else if (!strcasecmp(c->argv[2]->ptr,"watchdog-period")) {
685 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
686 if (ll)
687 enableWatchdog(ll);
688 else
689 disableWatchdog();
39d1e350 690 } else if (!strcasecmp(c->argv[2]->ptr,"rdbcompression")) {
691 int yn = yesnotoi(o->ptr);
692
693 if (yn == -1) goto badfmt;
694 server.rdb_compression = yn;
695 } else if (!strcasecmp(c->argv[2]->ptr,"rdbchecksum")) {
696 int yn = yesnotoi(o->ptr);
697
698 if (yn == -1) goto badfmt;
699 server.rdb_checksum = yn;
48d26a48 700 } else if (!strcasecmp(c->argv[2]->ptr,"slave-priority")) {
701 if (getLongLongFromObject(o,&ll) == REDIS_ERR ||
702 ll <= 0) goto badfmt;
703 server.slave_priority = ll;
e2641e09 704 } else {
3ab20376
PN
705 addReplyErrorFormat(c,"Unsupported CONFIG parameter: %s",
706 (char*)c->argv[2]->ptr);
e2641e09 707 return;
708 }
e2641e09 709 addReply(c,shared.ok);
710 return;
711
712badfmt: /* Bad format errors */
3ab20376 713 addReplyErrorFormat(c,"Invalid argument '%s' for CONFIG SET '%s'",
e2641e09 714 (char*)o->ptr,
3ab20376 715 (char*)c->argv[2]->ptr);
e2641e09 716}
717
0823e48f 718#define config_get_string_field(_name,_var) do { \
719 if (stringmatch(pattern,_name,0)) { \
720 addReplyBulkCString(c,_name); \
721 addReplyBulkCString(c,_var ? _var : ""); \
722 matches++; \
723 } \
724} while(0);
725
726#define config_get_bool_field(_name,_var) do { \
727 if (stringmatch(pattern,_name,0)) { \
728 addReplyBulkCString(c,_name); \
729 addReplyBulkCString(c,_var ? "yes" : "no"); \
730 matches++; \
731 } \
732} while(0);
733
734#define config_get_numerical_field(_name,_var) do { \
735 if (stringmatch(pattern,_name,0)) { \
736 ll2string(buf,sizeof(buf),_var); \
737 addReplyBulkCString(c,_name); \
738 addReplyBulkCString(c,buf); \
739 matches++; \
740 } \
741} while(0);
742
e2641e09 743void configGetCommand(redisClient *c) {
a375b077 744 robj *o = c->argv[2];
b301c1fc 745 void *replylen = addDeferredMultiBulkLength(c);
e2641e09 746 char *pattern = o->ptr;
a3687929 747 char buf[128];
e2641e09 748 int matches = 0;
eab0e26e 749 redisAssertWithInfo(c,o,o->encoding == REDIS_ENCODING_RAW);
e2641e09 750
0823e48f 751 /* String values */
752 config_get_string_field("dbfilename",server.rdb_filename);
753 config_get_string_field("requirepass",server.requirepass);
754 config_get_string_field("masterauth",server.requirepass);
755 config_get_string_field("bind",server.bindaddr);
756 config_get_string_field("unixsocket",server.unixsocket);
757 config_get_string_field("logfile",server.logfile);
758 config_get_string_field("pidfile",server.pidfile);
759
760 /* Numerical values */
761 config_get_numerical_field("maxmemory",server.maxmemory);
762 config_get_numerical_field("maxmemory-samples",server.maxmemory_samples);
763 config_get_numerical_field("timeout",server.maxidletime);
764 config_get_numerical_field("auto-aof-rewrite-percentage",
765 server.aof_rewrite_perc);
766 config_get_numerical_field("auto-aof-rewrite-min-size",
767 server.aof_rewrite_min_size);
85627983 768 config_get_numerical_field("hash-max-ziplist-entries",
769 server.hash_max_ziplist_entries);
770 config_get_numerical_field("hash-max-ziplist-value",
771 server.hash_max_ziplist_value);
0823e48f 772 config_get_numerical_field("list-max-ziplist-entries",
773 server.list_max_ziplist_entries);
774 config_get_numerical_field("list-max-ziplist-value",
775 server.list_max_ziplist_value);
776 config_get_numerical_field("set-max-intset-entries",
777 server.set_max_intset_entries);
778 config_get_numerical_field("zset-max-ziplist-entries",
779 server.zset_max_ziplist_entries);
780 config_get_numerical_field("zset-max-ziplist-value",
781 server.zset_max_ziplist_value);
782 config_get_numerical_field("lua-time-limit",server.lua_time_limit);
783 config_get_numerical_field("slowlog-log-slower-than",
784 server.slowlog_log_slower_than);
785 config_get_numerical_field("slowlog-max-len",
786 server.slowlog_max_len);
787 config_get_numerical_field("port",server.port);
788 config_get_numerical_field("databases",server.dbnum);
789 config_get_numerical_field("repl-ping-slave-period",server.repl_ping_slave_period);
790 config_get_numerical_field("repl-timeout",server.repl_timeout);
791 config_get_numerical_field("maxclients",server.maxclients);
fc030ac7 792 config_get_numerical_field("watchdog-period",server.watchdog_period);
48d26a48 793 config_get_numerical_field("slave-priority",server.slave_priority);
0823e48f 794
795 /* Bool (yes/no) values */
796 config_get_bool_field("no-appendfsync-on-rewrite",
797 server.aof_no_fsync_on_rewrite);
798 config_get_bool_field("slave-serve-stale-data",
799 server.repl_serve_stale_data);
05406168 800 config_get_bool_field("slave-read-only",
801 server.repl_slave_ro);
0823e48f 802 config_get_bool_field("stop-writes-on-bgsave-error",
803 server.stop_writes_on_bgsave_err);
804 config_get_bool_field("daemonize", server.daemonize);
805 config_get_bool_field("rdbcompression", server.rdb_compression);
39d1e350 806 config_get_bool_field("rdbchecksum", server.rdb_checksum);
0823e48f 807 config_get_bool_field("activerehashing", server.activerehashing);
808
809 /* Everything we can't handle with macros follows. */
810
811 if (stringmatch(pattern,"appendonly",0)) {
812 addReplyBulkCString(c,"appendonly");
813 addReplyBulkCString(c,server.aof_state == REDIS_AOF_OFF ? "no" : "yes");
814 matches++;
815 }
21cda08b 816 if (stringmatch(pattern,"dir",0)) {
817 char buf[1024];
818
bb831c31 819 if (getcwd(buf,sizeof(buf)) == NULL)
09d2abdc 820 buf[0] = '\0';
bb831c31
PN
821
822 addReplyBulkCString(c,"dir");
823 addReplyBulkCString(c,buf);
21cda08b 824 matches++;
825 }
165346ca 826 if (stringmatch(pattern,"maxmemory-policy",0)) {
827 char *s;
828
829 switch(server.maxmemory_policy) {
830 case REDIS_MAXMEMORY_VOLATILE_LRU: s = "volatile-lru"; break;
831 case REDIS_MAXMEMORY_VOLATILE_TTL: s = "volatile-ttl"; break;
832 case REDIS_MAXMEMORY_VOLATILE_RANDOM: s = "volatile-random"; break;
833 case REDIS_MAXMEMORY_ALLKEYS_LRU: s = "allkeys-lru"; break;
834 case REDIS_MAXMEMORY_ALLKEYS_RANDOM: s = "allkeys-random"; break;
5402c426 835 case REDIS_MAXMEMORY_NO_EVICTION: s = "noeviction"; break;
165346ca 836 default: s = "unknown"; break; /* too harmless to panic */
837 }
838 addReplyBulkCString(c,"maxmemory-policy");
839 addReplyBulkCString(c,s);
840 matches++;
841 }
e2641e09 842 if (stringmatch(pattern,"appendfsync",0)) {
843 char *policy;
844
2c915bcf 845 switch(server.aof_fsync) {
846 case AOF_FSYNC_NO: policy = "no"; break;
847 case AOF_FSYNC_EVERYSEC: policy = "everysec"; break;
848 case AOF_FSYNC_ALWAYS: policy = "always"; break;
e2641e09 849 default: policy = "unknown"; break; /* too harmless to panic */
850 }
851 addReplyBulkCString(c,"appendfsync");
852 addReplyBulkCString(c,policy);
853 matches++;
854 }
855 if (stringmatch(pattern,"save",0)) {
856 sds buf = sdsempty();
857 int j;
858
859 for (j = 0; j < server.saveparamslen; j++) {
860 buf = sdscatprintf(buf,"%ld %d",
861 server.saveparams[j].seconds,
862 server.saveparams[j].changes);
863 if (j != server.saveparamslen-1)
864 buf = sdscatlen(buf," ",1);
865 }
866 addReplyBulkCString(c,"save");
867 addReplyBulkCString(c,buf);
868 sdsfree(buf);
869 matches++;
870 }
2aa26d2a 871 if (stringmatch(pattern,"loglevel",0)) {
872 char *s;
873
874 switch(server.verbosity) {
875 case REDIS_WARNING: s = "warning"; break;
876 case REDIS_VERBOSE: s = "verbose"; break;
877 case REDIS_NOTICE: s = "notice"; break;
878 case REDIS_DEBUG: s = "debug"; break;
879 default: s = "unknown"; break; /* too harmless to panic */
880 }
881 addReplyBulkCString(c,"loglevel");
882 addReplyBulkCString(c,s);
883 matches++;
884 }
7fe8d49a 885 if (stringmatch(pattern,"client-output-buffer-limit",0)) {
886 sds buf = sdsempty();
887 int j;
888
889 for (j = 0; j < REDIS_CLIENT_LIMIT_NUM_CLASSES; j++) {
890 buf = sdscatprintf(buf,"%s %llu %llu %ld",
891 getClientLimitClassName(j),
892 server.client_obuf_limits[j].hard_limit_bytes,
893 server.client_obuf_limits[j].soft_limit_bytes,
894 (long) server.client_obuf_limits[j].soft_limit_seconds);
895 if (j != REDIS_CLIENT_LIMIT_NUM_CLASSES-1)
896 buf = sdscatlen(buf," ",1);
897 }
898 addReplyBulkCString(c,"client-output-buffer-limit");
899 addReplyBulkCString(c,buf);
900 sdsfree(buf);
901 matches++;
902 }
0823e48f 903 if (stringmatch(pattern,"unixsocketperm",0)) {
904 char buf[32];
905 snprintf(buf,sizeof(buf),"%o",server.unixsocketperm);
906 addReplyBulkCString(c,"unixsocketperm");
907 addReplyBulkCString(c,buf);
908 matches++;
909 }
910 if (stringmatch(pattern,"slaveof",0)) {
911 char buf[256];
912
913 addReplyBulkCString(c,"slaveof");
914 if (server.masterhost)
915 snprintf(buf,sizeof(buf),"%s %d",
916 server.masterhost, server.masterport);
917 else
918 buf[0] = '\0';
919 addReplyBulkCString(c,buf);
4d3bbf35 920 matches++;
921 }
b301c1fc 922 setDeferredMultiBulkLength(c,replylen,matches*2);
e2641e09 923}
924
925void configCommand(redisClient *c) {
926 if (!strcasecmp(c->argv[1]->ptr,"set")) {
927 if (c->argc != 4) goto badarity;
928 configSetCommand(c);
929 } else if (!strcasecmp(c->argv[1]->ptr,"get")) {
930 if (c->argc != 3) goto badarity;
931 configGetCommand(c);
932 } else if (!strcasecmp(c->argv[1]->ptr,"resetstat")) {
933 if (c->argc != 2) goto badarity;
9f8ded8c 934 server.stat_keyspace_hits = 0;
935 server.stat_keyspace_misses = 0;
e2641e09 936 server.stat_numcommands = 0;
937 server.stat_numconnections = 0;
938 server.stat_expiredkeys = 0;
d0407c2d 939 server.stat_rejected_conn = 0;
940 server.stat_fork_time = 0;
81f32c7b 941 server.aof_delayed_fsync = 0;
d7ed7fd2 942 resetCommandTableStats();
e2641e09 943 addReply(c,shared.ok);
944 } else {
3ab20376
PN
945 addReplyError(c,
946 "CONFIG subcommand must be one of GET, SET, RESETSTAT");
e2641e09 947 }
948 return;
949
950badarity:
3ab20376
PN
951 addReplyErrorFormat(c,"Wrong number of arguments for CONFIG %s",
952 (char*) c->argv[1]->ptr);
e2641e09 953}