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