]> git.saurik.com Git - redis.git/blob - src/config.c
Safer handling of MULTI/EXEC on errors.
[redis.git] / src / config.c
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
32 #include "redis.h"
33
34 /*-----------------------------------------------------------------------------
35 * Config file parsing
36 *----------------------------------------------------------------------------*/
37
38 int yesnotoi(char *s) {
39 if (!strcasecmp(s,"yes")) return 1;
40 else if (!strcasecmp(s,"no")) return 0;
41 else return -1;
42 }
43
44 void 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
51 void resetServerSaveParams() {
52 zfree(server.saveparams);
53 server.saveparams = NULL;
54 server.saveparamslen = 0;
55 }
56
57 void loadServerConfigFromString(char *config) {
58 char *err = NULL;
59 int linenum = 0, totlines, i;
60 sds *lines;
61
62 lines = sdssplitlen(config,strlen(config),"\n",1,&totlines);
63
64 for (i = 0; i < totlines; i++) {
65 sds *argv;
66 int argc;
67
68 linenum = i+1;
69 lines[i] = sdstrim(lines[i]," \t\r\n");
70
71 /* Skip comments and blank lines*/
72 if (lines[i][0] == '#' || lines[i][0] == '\0') continue;
73
74 /* Split into arguments */
75 argv = sdssplitargs(lines[i],&argc);
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]);
86 if (server.port < 0 || server.port > 65535) {
87 err = "Invalid port"; goto loaderr;
88 }
89 } else if (!strcasecmp(argv[0],"bind") && argc == 2) {
90 server.bindaddr = zstrdup(argv[1]);
91 } else if (!strcasecmp(argv[0],"unixsocket") && argc == 2) {
92 server.unixsocket = zstrdup(argv[1]);
93 } else if (!strcasecmp(argv[0],"unixsocketperm") && argc == 2) {
94 errno = 0;
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 }
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();
109 }
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 }
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 }
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) {
186 loadServerConfig(argv[1],NULL);
187 } else if (!strcasecmp(argv[0],"maxclients") && argc == 2) {
188 server.maxclients = atoi(argv[1]);
189 if (server.maxclients < 1) {
190 err = "Invalid max clients limit"; goto loaderr;
191 }
192 } else if (!strcasecmp(argv[0],"maxmemory") && argc == 2) {
193 server.maxmemory = memtoll(argv[1],NULL);
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;
205 } else if (!strcasecmp(argv[1],"noeviction")) {
206 server.maxmemory_policy = REDIS_MAXMEMORY_NO_EVICTION;
207 } else {
208 err = "Invalid maxmemory policy";
209 goto loaderr;
210 }
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 }
217 } else if (!strcasecmp(argv[0],"slaveof") && argc == 3) {
218 server.masterhost = sdsnew(argv[1]);
219 server.masterport = atoi(argv[2]);
220 server.repl_state = REDIS_REPL_CONNECT;
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 }
233 } else if (!strcasecmp(argv[0],"masterauth") && argc == 2) {
234 server.masterauth = zstrdup(argv[1]);
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 }
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 }
243 } else if (!strcasecmp(argv[0],"rdbcompression") && argc == 2) {
244 if ((server.rdb_compression = yesnotoi(argv[1])) == -1) {
245 err = "argument must be 'yes' or 'no'"; goto loaderr;
246 }
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 }
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) {
260 int yes;
261
262 if ((yes = yesnotoi(argv[1])) == -1) {
263 err = "argument must be 'yes' or 'no'"; goto loaderr;
264 }
265 server.aof_state = yes ? REDIS_AOF_ON : REDIS_AOF_OFF;
266 } else if (!strcasecmp(argv[0],"appendfilename") && argc == 2) {
267 zfree(server.aof_filename);
268 server.aof_filename = zstrdup(argv[1]);
269 } else if (!strcasecmp(argv[0],"no-appendfsync-on-rewrite")
270 && argc == 2) {
271 if ((server.aof_no_fsync_on_rewrite= yesnotoi(argv[1])) == -1) {
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")) {
276 server.aof_fsync = AOF_FSYNC_NO;
277 } else if (!strcasecmp(argv[1],"always")) {
278 server.aof_fsync = AOF_FSYNC_ALWAYS;
279 } else if (!strcasecmp(argv[1],"everysec")) {
280 server.aof_fsync = AOF_FSYNC_EVERYSEC;
281 } else {
282 err = "argument must be 'no', 'always' or 'everysec'";
283 goto loaderr;
284 }
285 } else if (!strcasecmp(argv[0],"auto-aof-rewrite-percentage") &&
286 argc == 2)
287 {
288 server.aof_rewrite_perc = atoi(argv[1]);
289 if (server.aof_rewrite_perc < 0) {
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 {
296 server.aof_rewrite_min_size = memtoll(argv[1],NULL);
297 } else if (!strcasecmp(argv[0],"requirepass") && argc == 2) {
298 if (strlen(argv[1]) > REDIS_AUTHPASS_MAX_LEN) {
299 err = "Password is longer than REDIS_AUTHPASS_MAX_LEN";
300 goto loaderr;
301 }
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) {
307 zfree(server.rdb_filename);
308 server.rdb_filename = zstrdup(argv[1]);
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);
313 } else if (!strcasecmp(argv[0],"list-max-ziplist-entries") && argc == 2){
314 server.list_max_ziplist_entries = memtoll(argv[1], NULL);
315 } else if (!strcasecmp(argv[0],"list-max-ziplist-value") && argc == 2) {
316 server.list_max_ziplist_value = memtoll(argv[1], NULL);
317 } else if (!strcasecmp(argv[0],"set-max-intset-entries") && argc == 2) {
318 server.set_max_intset_entries = memtoll(argv[1], NULL);
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);
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 }
347 } else if (!strcasecmp(argv[0],"cluster-enabled") && argc == 2) {
348 if ((server.cluster_enabled = yesnotoi(argv[1])) == -1) {
349 err = "argument must be 'yes' or 'no'"; goto loaderr;
350 }
351 } else if (!strcasecmp(argv[0],"cluster-config-file") && argc == 2) {
352 zfree(server.cluster.configfile);
353 server.cluster.configfile = zstrdup(argv[1]);
354 } else if (!strcasecmp(argv[0],"lua-time-limit") && argc == 2) {
355 server.lua_time_limit = strtoll(argv[1],NULL,10);
356 } else if (!strcasecmp(argv[0],"slowlog-log-slower-than") &&
357 argc == 2)
358 {
359 server.slowlog_log_slower_than = strtoll(argv[1],NULL,10);
360 } else if (!strcasecmp(argv[0],"slowlog-max-len") && argc == 2) {
361 server.slowlog_max_len = strtoll(argv[1],NULL,10);
362 } else if (!strcasecmp(argv[0],"client-output-buffer-limit") &&
363 argc == 5)
364 {
365 int class = getClientLimitClassByName(argv[1]);
366 unsigned long long hard, soft;
367 int soft_seconds;
368
369 if (class == -1) {
370 err = "Unrecognized client limit class";
371 goto loaderr;
372 }
373 hard = memtoll(argv[2],NULL);
374 soft = memtoll(argv[3],NULL);
375 soft_seconds = atoi(argv[4]);
376 if (soft_seconds < 0) {
377 err = "Negative number of seconds in soft limt is invalid";
378 goto loaderr;
379 }
380 server.client_obuf_limits[class].hard_limit_bytes = hard;
381 server.client_obuf_limits[class].soft_limit_bytes = soft;
382 server.client_obuf_limits[class].soft_limit_seconds = soft_seconds;
383 } else if (!strcasecmp(argv[0],"stop-writes-on-bgsave-error") &&
384 argc == 2) {
385 if ((server.stop_writes_on_bgsave_err = yesnotoi(argv[1])) == -1) {
386 err = "argument must be 'yes' or 'no'"; goto loaderr;
387 }
388 } else if (!strcasecmp(argv[0],"slave-priority") && argc == 2) {
389 server.slave_priority = atoi(argv[1]);
390 } else if (!strcasecmp(argv[0],"sentinel")) {
391 /* argc == 1 is handled by main() as we need to enter the sentinel
392 * mode ASAP. */
393 if (argc != 1) {
394 if (!server.sentinel_mode) {
395 err = "sentinel directive while not in sentinel mode";
396 goto loaderr;
397 }
398 err = sentinelHandleConfiguration(argv+1,argc-1);
399 if (err) goto loaderr;
400 }
401 } else {
402 err = "Bad directive or wrong number of arguments"; goto loaderr;
403 }
404 sdsfreesplitres(argv,argc);
405 }
406 sdsfreesplitres(lines,totlines);
407 return;
408
409 loaderr:
410 fprintf(stderr, "\n*** FATAL CONFIG FILE ERROR ***\n");
411 fprintf(stderr, "Reading the configuration file, at line %d\n", linenum);
412 fprintf(stderr, ">>> '%s'\n", lines[i]);
413 fprintf(stderr, "%s\n", err);
414 exit(1);
415 }
416
417 /* Load the server configuration from the specified filename.
418 * The function appends the additional configuration directives stored
419 * in the 'options' string to the config file before loading.
420 *
421 * Both filename and options can be NULL, in such a case are considered
422 * emtpy. This way loadServerConfig can be used to just load a file or
423 * just load a string. */
424 void loadServerConfig(char *filename, char *options) {
425 sds config = sdsempty();
426 char buf[REDIS_CONFIGLINE_MAX+1];
427
428 /* Load the file content */
429 if (filename) {
430 FILE *fp;
431
432 if (filename[0] == '-' && filename[1] == '\0') {
433 fp = stdin;
434 } else {
435 if ((fp = fopen(filename,"r")) == NULL) {
436 redisLog(REDIS_WARNING,
437 "Fatal error, can't open config file '%s'", filename);
438 exit(1);
439 }
440 }
441 while(fgets(buf,REDIS_CONFIGLINE_MAX+1,fp) != NULL)
442 config = sdscat(config,buf);
443 if (fp != stdin) fclose(fp);
444 }
445 /* Append the additional options */
446 if (options) {
447 config = sdscat(config,"\n");
448 config = sdscat(config,options);
449 }
450 loadServerConfigFromString(config);
451 sdsfree(config);
452 }
453
454 /*-----------------------------------------------------------------------------
455 * CONFIG command for remote configuration
456 *----------------------------------------------------------------------------*/
457
458 void configSetCommand(redisClient *c) {
459 robj *o;
460 long long ll;
461 redisAssertWithInfo(c,c->argv[2],c->argv[2]->encoding == REDIS_ENCODING_RAW);
462 redisAssertWithInfo(c,c->argv[2],c->argv[3]->encoding == REDIS_ENCODING_RAW);
463 o = c->argv[3];
464
465 if (!strcasecmp(c->argv[2]->ptr,"dbfilename")) {
466 zfree(server.rdb_filename);
467 server.rdb_filename = zstrdup(o->ptr);
468 } else if (!strcasecmp(c->argv[2]->ptr,"requirepass")) {
469 if (sdslen(o->ptr) > REDIS_AUTHPASS_MAX_LEN) goto badfmt;
470 zfree(server.requirepass);
471 server.requirepass = ((char*)o->ptr)[0] ? zstrdup(o->ptr) : NULL;
472 } else if (!strcasecmp(c->argv[2]->ptr,"masterauth")) {
473 zfree(server.masterauth);
474 server.masterauth = zstrdup(o->ptr);
475 } else if (!strcasecmp(c->argv[2]->ptr,"maxmemory")) {
476 if (getLongLongFromObject(o,&ll) == REDIS_ERR ||
477 ll < 0) goto badfmt;
478 server.maxmemory = ll;
479 if (server.maxmemory) {
480 if (server.maxmemory < zmalloc_used_memory()) {
481 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.");
482 }
483 freeMemoryIfNeeded();
484 }
485 } else if (!strcasecmp(c->argv[2]->ptr,"maxmemory-policy")) {
486 if (!strcasecmp(o->ptr,"volatile-lru")) {
487 server.maxmemory_policy = REDIS_MAXMEMORY_VOLATILE_LRU;
488 } else if (!strcasecmp(o->ptr,"volatile-random")) {
489 server.maxmemory_policy = REDIS_MAXMEMORY_VOLATILE_RANDOM;
490 } else if (!strcasecmp(o->ptr,"volatile-ttl")) {
491 server.maxmemory_policy = REDIS_MAXMEMORY_VOLATILE_TTL;
492 } else if (!strcasecmp(o->ptr,"allkeys-lru")) {
493 server.maxmemory_policy = REDIS_MAXMEMORY_ALLKEYS_LRU;
494 } else if (!strcasecmp(o->ptr,"allkeys-random")) {
495 server.maxmemory_policy = REDIS_MAXMEMORY_ALLKEYS_RANDOM;
496 } else if (!strcasecmp(o->ptr,"noeviction")) {
497 server.maxmemory_policy = REDIS_MAXMEMORY_NO_EVICTION;
498 } else {
499 goto badfmt;
500 }
501 } else if (!strcasecmp(c->argv[2]->ptr,"maxmemory-samples")) {
502 if (getLongLongFromObject(o,&ll) == REDIS_ERR ||
503 ll <= 0) goto badfmt;
504 server.maxmemory_samples = ll;
505 } else if (!strcasecmp(c->argv[2]->ptr,"timeout")) {
506 if (getLongLongFromObject(o,&ll) == REDIS_ERR ||
507 ll < 0 || ll > LONG_MAX) goto badfmt;
508 server.maxidletime = ll;
509 } else if (!strcasecmp(c->argv[2]->ptr,"appendfsync")) {
510 if (!strcasecmp(o->ptr,"no")) {
511 server.aof_fsync = AOF_FSYNC_NO;
512 } else if (!strcasecmp(o->ptr,"everysec")) {
513 server.aof_fsync = AOF_FSYNC_EVERYSEC;
514 } else if (!strcasecmp(o->ptr,"always")) {
515 server.aof_fsync = AOF_FSYNC_ALWAYS;
516 } else {
517 goto badfmt;
518 }
519 } else if (!strcasecmp(c->argv[2]->ptr,"no-appendfsync-on-rewrite")) {
520 int yn = yesnotoi(o->ptr);
521
522 if (yn == -1) goto badfmt;
523 server.aof_no_fsync_on_rewrite = yn;
524 } else if (!strcasecmp(c->argv[2]->ptr,"appendonly")) {
525 int enable = yesnotoi(o->ptr);
526
527 if (enable == -1) goto badfmt;
528 if (enable == 0 && server.aof_state != REDIS_AOF_OFF) {
529 stopAppendOnly();
530 } else if (enable && server.aof_state == REDIS_AOF_OFF) {
531 if (startAppendOnly() == REDIS_ERR) {
532 addReplyError(c,
533 "Unable to turn on AOF. Check server logs.");
534 return;
535 }
536 }
537 } else if (!strcasecmp(c->argv[2]->ptr,"auto-aof-rewrite-percentage")) {
538 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
539 server.aof_rewrite_perc = ll;
540 } else if (!strcasecmp(c->argv[2]->ptr,"auto-aof-rewrite-min-size")) {
541 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
542 server.aof_rewrite_min_size = ll;
543 } else if (!strcasecmp(c->argv[2]->ptr,"save")) {
544 int vlen, j;
545 sds *v = sdssplitlen(o->ptr,sdslen(o->ptr)," ",1,&vlen);
546
547 /* Perform sanity check before setting the new config:
548 * - Even number of args
549 * - Seconds >= 1, changes >= 0 */
550 if (vlen & 1) {
551 sdsfreesplitres(v,vlen);
552 goto badfmt;
553 }
554 for (j = 0; j < vlen; j++) {
555 char *eptr;
556 long val;
557
558 val = strtoll(v[j], &eptr, 10);
559 if (eptr[0] != '\0' ||
560 ((j & 1) == 0 && val < 1) ||
561 ((j & 1) == 1 && val < 0)) {
562 sdsfreesplitres(v,vlen);
563 goto badfmt;
564 }
565 }
566 /* Finally set the new config */
567 resetServerSaveParams();
568 for (j = 0; j < vlen; j += 2) {
569 time_t seconds;
570 int changes;
571
572 seconds = strtoll(v[j],NULL,10);
573 changes = strtoll(v[j+1],NULL,10);
574 appendServerSaveParams(seconds, changes);
575 }
576 sdsfreesplitres(v,vlen);
577 } else if (!strcasecmp(c->argv[2]->ptr,"slave-serve-stale-data")) {
578 int yn = yesnotoi(o->ptr);
579
580 if (yn == -1) goto badfmt;
581 server.repl_serve_stale_data = yn;
582 } else if (!strcasecmp(c->argv[2]->ptr,"slave-read-only")) {
583 int yn = yesnotoi(o->ptr);
584
585 if (yn == -1) goto badfmt;
586 server.repl_slave_ro = yn;
587 } else if (!strcasecmp(c->argv[2]->ptr,"dir")) {
588 if (chdir((char*)o->ptr) == -1) {
589 addReplyErrorFormat(c,"Changing directory: %s", strerror(errno));
590 return;
591 }
592 } else if (!strcasecmp(c->argv[2]->ptr,"hash-max-ziplist-entries")) {
593 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
594 server.hash_max_ziplist_entries = ll;
595 } else if (!strcasecmp(c->argv[2]->ptr,"hash-max-ziplist-value")) {
596 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
597 server.hash_max_ziplist_value = ll;
598 } else if (!strcasecmp(c->argv[2]->ptr,"list-max-ziplist-entries")) {
599 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
600 server.list_max_ziplist_entries = ll;
601 } else if (!strcasecmp(c->argv[2]->ptr,"list-max-ziplist-value")) {
602 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
603 server.list_max_ziplist_value = ll;
604 } else if (!strcasecmp(c->argv[2]->ptr,"set-max-intset-entries")) {
605 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
606 server.set_max_intset_entries = ll;
607 } else if (!strcasecmp(c->argv[2]->ptr,"zset-max-ziplist-entries")) {
608 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
609 server.zset_max_ziplist_entries = ll;
610 } else if (!strcasecmp(c->argv[2]->ptr,"zset-max-ziplist-value")) {
611 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
612 server.zset_max_ziplist_value = ll;
613 } else if (!strcasecmp(c->argv[2]->ptr,"lua-time-limit")) {
614 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
615 server.lua_time_limit = ll;
616 } else if (!strcasecmp(c->argv[2]->ptr,"slowlog-log-slower-than")) {
617 if (getLongLongFromObject(o,&ll) == REDIS_ERR) goto badfmt;
618 server.slowlog_log_slower_than = ll;
619 } else if (!strcasecmp(c->argv[2]->ptr,"slowlog-max-len")) {
620 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
621 server.slowlog_max_len = (unsigned)ll;
622 } else if (!strcasecmp(c->argv[2]->ptr,"loglevel")) {
623 if (!strcasecmp(o->ptr,"warning")) {
624 server.verbosity = REDIS_WARNING;
625 } else if (!strcasecmp(o->ptr,"notice")) {
626 server.verbosity = REDIS_NOTICE;
627 } else if (!strcasecmp(o->ptr,"verbose")) {
628 server.verbosity = REDIS_VERBOSE;
629 } else if (!strcasecmp(o->ptr,"debug")) {
630 server.verbosity = REDIS_DEBUG;
631 } else {
632 goto badfmt;
633 }
634 } else if (!strcasecmp(c->argv[2]->ptr,"client-output-buffer-limit")) {
635 int vlen, j;
636 sds *v = sdssplitlen(o->ptr,sdslen(o->ptr)," ",1,&vlen);
637
638 /* We need a multiple of 4: <class> <hard> <soft> <soft_seconds> */
639 if (vlen % 4) {
640 sdsfreesplitres(v,vlen);
641 goto badfmt;
642 }
643
644 /* Sanity check of single arguments, so that we either refuse the
645 * whole configuration string or accept it all, even if a single
646 * error in a single client class is present. */
647 for (j = 0; j < vlen; j++) {
648 char *eptr;
649 long val;
650
651 if ((j % 4) == 0) {
652 if (getClientLimitClassByName(v[j]) == -1) {
653 sdsfreesplitres(v,vlen);
654 goto badfmt;
655 }
656 } else {
657 val = strtoll(v[j], &eptr, 10);
658 if (eptr[0] != '\0' || val < 0) {
659 sdsfreesplitres(v,vlen);
660 goto badfmt;
661 }
662 }
663 }
664 /* Finally set the new config */
665 for (j = 0; j < vlen; j += 4) {
666 int class;
667 unsigned long long hard, soft;
668 int soft_seconds;
669
670 class = getClientLimitClassByName(v[j]);
671 hard = strtoll(v[j+1],NULL,10);
672 soft = strtoll(v[j+2],NULL,10);
673 soft_seconds = strtoll(v[j+3],NULL,10);
674
675 server.client_obuf_limits[class].hard_limit_bytes = hard;
676 server.client_obuf_limits[class].soft_limit_bytes = soft;
677 server.client_obuf_limits[class].soft_limit_seconds = soft_seconds;
678 }
679 sdsfreesplitres(v,vlen);
680 } else if (!strcasecmp(c->argv[2]->ptr,"stop-writes-on-bgsave-error")) {
681 int yn = yesnotoi(o->ptr);
682
683 if (yn == -1) goto badfmt;
684 server.stop_writes_on_bgsave_err = yn;
685 } else if (!strcasecmp(c->argv[2]->ptr,"repl-ping-slave-period")) {
686 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll <= 0) goto badfmt;
687 server.repl_ping_slave_period = ll;
688 } else if (!strcasecmp(c->argv[2]->ptr,"repl-timeout")) {
689 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll <= 0) goto badfmt;
690 server.repl_timeout = ll;
691 } else if (!strcasecmp(c->argv[2]->ptr,"watchdog-period")) {
692 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
693 if (ll)
694 enableWatchdog(ll);
695 else
696 disableWatchdog();
697 } else if (!strcasecmp(c->argv[2]->ptr,"rdbcompression")) {
698 int yn = yesnotoi(o->ptr);
699
700 if (yn == -1) goto badfmt;
701 server.rdb_compression = yn;
702 } else if (!strcasecmp(c->argv[2]->ptr,"rdbchecksum")) {
703 int yn = yesnotoi(o->ptr);
704
705 if (yn == -1) goto badfmt;
706 server.rdb_checksum = yn;
707 } else if (!strcasecmp(c->argv[2]->ptr,"slave-priority")) {
708 if (getLongLongFromObject(o,&ll) == REDIS_ERR ||
709 ll <= 0) goto badfmt;
710 server.slave_priority = ll;
711 } else {
712 addReplyErrorFormat(c,"Unsupported CONFIG parameter: %s",
713 (char*)c->argv[2]->ptr);
714 return;
715 }
716 addReply(c,shared.ok);
717 return;
718
719 badfmt: /* Bad format errors */
720 addReplyErrorFormat(c,"Invalid argument '%s' for CONFIG SET '%s'",
721 (char*)o->ptr,
722 (char*)c->argv[2]->ptr);
723 }
724
725 #define config_get_string_field(_name,_var) do { \
726 if (stringmatch(pattern,_name,0)) { \
727 addReplyBulkCString(c,_name); \
728 addReplyBulkCString(c,_var ? _var : ""); \
729 matches++; \
730 } \
731 } while(0);
732
733 #define config_get_bool_field(_name,_var) do { \
734 if (stringmatch(pattern,_name,0)) { \
735 addReplyBulkCString(c,_name); \
736 addReplyBulkCString(c,_var ? "yes" : "no"); \
737 matches++; \
738 } \
739 } while(0);
740
741 #define config_get_numerical_field(_name,_var) do { \
742 if (stringmatch(pattern,_name,0)) { \
743 ll2string(buf,sizeof(buf),_var); \
744 addReplyBulkCString(c,_name); \
745 addReplyBulkCString(c,buf); \
746 matches++; \
747 } \
748 } while(0);
749
750 void configGetCommand(redisClient *c) {
751 robj *o = c->argv[2];
752 void *replylen = addDeferredMultiBulkLength(c);
753 char *pattern = o->ptr;
754 char buf[128];
755 int matches = 0;
756 redisAssertWithInfo(c,o,o->encoding == REDIS_ENCODING_RAW);
757
758 /* String values */
759 config_get_string_field("dbfilename",server.rdb_filename);
760 config_get_string_field("requirepass",server.requirepass);
761 config_get_string_field("masterauth",server.requirepass);
762 config_get_string_field("bind",server.bindaddr);
763 config_get_string_field("unixsocket",server.unixsocket);
764 config_get_string_field("logfile",server.logfile);
765 config_get_string_field("pidfile",server.pidfile);
766
767 /* Numerical values */
768 config_get_numerical_field("maxmemory",server.maxmemory);
769 config_get_numerical_field("maxmemory-samples",server.maxmemory_samples);
770 config_get_numerical_field("timeout",server.maxidletime);
771 config_get_numerical_field("auto-aof-rewrite-percentage",
772 server.aof_rewrite_perc);
773 config_get_numerical_field("auto-aof-rewrite-min-size",
774 server.aof_rewrite_min_size);
775 config_get_numerical_field("hash-max-ziplist-entries",
776 server.hash_max_ziplist_entries);
777 config_get_numerical_field("hash-max-ziplist-value",
778 server.hash_max_ziplist_value);
779 config_get_numerical_field("list-max-ziplist-entries",
780 server.list_max_ziplist_entries);
781 config_get_numerical_field("list-max-ziplist-value",
782 server.list_max_ziplist_value);
783 config_get_numerical_field("set-max-intset-entries",
784 server.set_max_intset_entries);
785 config_get_numerical_field("zset-max-ziplist-entries",
786 server.zset_max_ziplist_entries);
787 config_get_numerical_field("zset-max-ziplist-value",
788 server.zset_max_ziplist_value);
789 config_get_numerical_field("lua-time-limit",server.lua_time_limit);
790 config_get_numerical_field("slowlog-log-slower-than",
791 server.slowlog_log_slower_than);
792 config_get_numerical_field("slowlog-max-len",
793 server.slowlog_max_len);
794 config_get_numerical_field("port",server.port);
795 config_get_numerical_field("databases",server.dbnum);
796 config_get_numerical_field("repl-ping-slave-period",server.repl_ping_slave_period);
797 config_get_numerical_field("repl-timeout",server.repl_timeout);
798 config_get_numerical_field("maxclients",server.maxclients);
799 config_get_numerical_field("watchdog-period",server.watchdog_period);
800 config_get_numerical_field("slave-priority",server.slave_priority);
801
802 /* Bool (yes/no) values */
803 config_get_bool_field("no-appendfsync-on-rewrite",
804 server.aof_no_fsync_on_rewrite);
805 config_get_bool_field("slave-serve-stale-data",
806 server.repl_serve_stale_data);
807 config_get_bool_field("slave-read-only",
808 server.repl_slave_ro);
809 config_get_bool_field("stop-writes-on-bgsave-error",
810 server.stop_writes_on_bgsave_err);
811 config_get_bool_field("daemonize", server.daemonize);
812 config_get_bool_field("rdbcompression", server.rdb_compression);
813 config_get_bool_field("rdbchecksum", server.rdb_checksum);
814 config_get_bool_field("activerehashing", server.activerehashing);
815
816 /* Everything we can't handle with macros follows. */
817
818 if (stringmatch(pattern,"appendonly",0)) {
819 addReplyBulkCString(c,"appendonly");
820 addReplyBulkCString(c,server.aof_state == REDIS_AOF_OFF ? "no" : "yes");
821 matches++;
822 }
823 if (stringmatch(pattern,"dir",0)) {
824 char buf[1024];
825
826 if (getcwd(buf,sizeof(buf)) == NULL)
827 buf[0] = '\0';
828
829 addReplyBulkCString(c,"dir");
830 addReplyBulkCString(c,buf);
831 matches++;
832 }
833 if (stringmatch(pattern,"maxmemory-policy",0)) {
834 char *s;
835
836 switch(server.maxmemory_policy) {
837 case REDIS_MAXMEMORY_VOLATILE_LRU: s = "volatile-lru"; break;
838 case REDIS_MAXMEMORY_VOLATILE_TTL: s = "volatile-ttl"; break;
839 case REDIS_MAXMEMORY_VOLATILE_RANDOM: s = "volatile-random"; break;
840 case REDIS_MAXMEMORY_ALLKEYS_LRU: s = "allkeys-lru"; break;
841 case REDIS_MAXMEMORY_ALLKEYS_RANDOM: s = "allkeys-random"; break;
842 case REDIS_MAXMEMORY_NO_EVICTION: s = "noeviction"; break;
843 default: s = "unknown"; break; /* too harmless to panic */
844 }
845 addReplyBulkCString(c,"maxmemory-policy");
846 addReplyBulkCString(c,s);
847 matches++;
848 }
849 if (stringmatch(pattern,"appendfsync",0)) {
850 char *policy;
851
852 switch(server.aof_fsync) {
853 case AOF_FSYNC_NO: policy = "no"; break;
854 case AOF_FSYNC_EVERYSEC: policy = "everysec"; break;
855 case AOF_FSYNC_ALWAYS: policy = "always"; break;
856 default: policy = "unknown"; break; /* too harmless to panic */
857 }
858 addReplyBulkCString(c,"appendfsync");
859 addReplyBulkCString(c,policy);
860 matches++;
861 }
862 if (stringmatch(pattern,"save",0)) {
863 sds buf = sdsempty();
864 int j;
865
866 for (j = 0; j < server.saveparamslen; j++) {
867 buf = sdscatprintf(buf,"%ld %d",
868 server.saveparams[j].seconds,
869 server.saveparams[j].changes);
870 if (j != server.saveparamslen-1)
871 buf = sdscatlen(buf," ",1);
872 }
873 addReplyBulkCString(c,"save");
874 addReplyBulkCString(c,buf);
875 sdsfree(buf);
876 matches++;
877 }
878 if (stringmatch(pattern,"loglevel",0)) {
879 char *s;
880
881 switch(server.verbosity) {
882 case REDIS_WARNING: s = "warning"; break;
883 case REDIS_VERBOSE: s = "verbose"; break;
884 case REDIS_NOTICE: s = "notice"; break;
885 case REDIS_DEBUG: s = "debug"; break;
886 default: s = "unknown"; break; /* too harmless to panic */
887 }
888 addReplyBulkCString(c,"loglevel");
889 addReplyBulkCString(c,s);
890 matches++;
891 }
892 if (stringmatch(pattern,"client-output-buffer-limit",0)) {
893 sds buf = sdsempty();
894 int j;
895
896 for (j = 0; j < REDIS_CLIENT_LIMIT_NUM_CLASSES; j++) {
897 buf = sdscatprintf(buf,"%s %llu %llu %ld",
898 getClientLimitClassName(j),
899 server.client_obuf_limits[j].hard_limit_bytes,
900 server.client_obuf_limits[j].soft_limit_bytes,
901 (long) server.client_obuf_limits[j].soft_limit_seconds);
902 if (j != REDIS_CLIENT_LIMIT_NUM_CLASSES-1)
903 buf = sdscatlen(buf," ",1);
904 }
905 addReplyBulkCString(c,"client-output-buffer-limit");
906 addReplyBulkCString(c,buf);
907 sdsfree(buf);
908 matches++;
909 }
910 if (stringmatch(pattern,"unixsocketperm",0)) {
911 char buf[32];
912 snprintf(buf,sizeof(buf),"%o",server.unixsocketperm);
913 addReplyBulkCString(c,"unixsocketperm");
914 addReplyBulkCString(c,buf);
915 matches++;
916 }
917 if (stringmatch(pattern,"slaveof",0)) {
918 char buf[256];
919
920 addReplyBulkCString(c,"slaveof");
921 if (server.masterhost)
922 snprintf(buf,sizeof(buf),"%s %d",
923 server.masterhost, server.masterport);
924 else
925 buf[0] = '\0';
926 addReplyBulkCString(c,buf);
927 matches++;
928 }
929 setDeferredMultiBulkLength(c,replylen,matches*2);
930 }
931
932 void configCommand(redisClient *c) {
933 if (!strcasecmp(c->argv[1]->ptr,"set")) {
934 if (c->argc != 4) goto badarity;
935 configSetCommand(c);
936 } else if (!strcasecmp(c->argv[1]->ptr,"get")) {
937 if (c->argc != 3) goto badarity;
938 configGetCommand(c);
939 } else if (!strcasecmp(c->argv[1]->ptr,"resetstat")) {
940 if (c->argc != 2) goto badarity;
941 server.stat_keyspace_hits = 0;
942 server.stat_keyspace_misses = 0;
943 server.stat_numcommands = 0;
944 server.stat_numconnections = 0;
945 server.stat_expiredkeys = 0;
946 server.stat_rejected_conn = 0;
947 server.stat_fork_time = 0;
948 server.aof_delayed_fsync = 0;
949 resetCommandTableStats();
950 addReply(c,shared.ok);
951 } else {
952 addReplyError(c,
953 "CONFIG subcommand must be one of GET, SET, RESETSTAT");
954 }
955 return;
956
957 badarity:
958 addReplyErrorFormat(c,"Wrong number of arguments for CONFIG %s",
959 (char*) c->argv[1]->ptr);
960 }