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