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