]> git.saurik.com Git - redis.git/blob - src/config.c
b11bbd0f557494b051c57bc1ad6d0ae9f48c31be
[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 /* I agree, this is a very rudimental way to load a configuration...
27 will improve later if the config gets more complex */
28 void loadServerConfig(char *filename) {
29 FILE *fp;
30 char buf[REDIS_CONFIGLINE_MAX+1], *err = NULL;
31 int linenum = 0;
32 sds line = NULL;
33
34 if (filename[0] == '-' && filename[1] == '\0')
35 fp = stdin;
36 else {
37 if ((fp = fopen(filename,"r")) == NULL) {
38 redisLog(REDIS_WARNING, "Fatal error, can't open config file '%s'", filename);
39 exit(1);
40 }
41 }
42
43 while(fgets(buf,REDIS_CONFIGLINE_MAX+1,fp) != NULL) {
44 sds *argv;
45 int argc, j;
46
47 linenum++;
48 line = sdsnew(buf);
49 line = sdstrim(line," \t\r\n");
50
51 /* Skip comments and blank lines*/
52 if (line[0] == '#' || line[0] == '\0') {
53 sdsfree(line);
54 continue;
55 }
56
57 /* Split into arguments */
58 argv = sdssplitlen(line,sdslen(line)," ",1,&argc);
59 sdstolower(argv[0]);
60
61 /* Execute config directives */
62 if (!strcasecmp(argv[0],"timeout") && argc == 2) {
63 server.maxidletime = atoi(argv[1]);
64 if (server.maxidletime < 0) {
65 err = "Invalid timeout value"; goto loaderr;
66 }
67 } else if (!strcasecmp(argv[0],"port") && argc == 2) {
68 server.port = atoi(argv[1]);
69 if (server.port != -1 &&
70 (server.port < 1 || server.port > 65535))
71 {
72 err = "Invalid port"; goto loaderr;
73 }
74 } else if (!strcasecmp(argv[0],"bind") && argc == 2) {
75 server.bindaddr = zstrdup(argv[1]);
76 } else if (!strcasecmp(argv[0],"socket") && argc == 2) {
77 server.sockpath = zstrdup(argv[1]);
78 } else if (!strcasecmp(argv[0],"save") && argc == 3) {
79 int seconds = atoi(argv[1]);
80 int changes = atoi(argv[2]);
81 if (seconds < 1 || changes < 0) {
82 err = "Invalid save parameters"; goto loaderr;
83 }
84 appendServerSaveParams(seconds,changes);
85 } else if (!strcasecmp(argv[0],"dir") && argc == 2) {
86 if (chdir(argv[1]) == -1) {
87 redisLog(REDIS_WARNING,"Can't chdir to '%s': %s",
88 argv[1], strerror(errno));
89 exit(1);
90 }
91 } else if (!strcasecmp(argv[0],"loglevel") && argc == 2) {
92 if (!strcasecmp(argv[1],"debug")) server.verbosity = REDIS_DEBUG;
93 else if (!strcasecmp(argv[1],"verbose")) server.verbosity = REDIS_VERBOSE;
94 else if (!strcasecmp(argv[1],"notice")) server.verbosity = REDIS_NOTICE;
95 else if (!strcasecmp(argv[1],"warning")) server.verbosity = REDIS_WARNING;
96 else {
97 err = "Invalid log level. Must be one of debug, notice, warning";
98 goto loaderr;
99 }
100 } else if (!strcasecmp(argv[0],"logfile") && argc == 2) {
101 FILE *logfp;
102
103 server.logfile = zstrdup(argv[1]);
104 if (!strcasecmp(server.logfile,"stdout")) {
105 zfree(server.logfile);
106 server.logfile = NULL;
107 }
108 if (server.logfile) {
109 /* Test if we are able to open the file. The server will not
110 * be able to abort just for this problem later... */
111 logfp = fopen(server.logfile,"a");
112 if (logfp == NULL) {
113 err = sdscatprintf(sdsempty(),
114 "Can't open the log file: %s", strerror(errno));
115 goto loaderr;
116 }
117 fclose(logfp);
118 }
119 } else if (!strcasecmp(argv[0],"databases") && argc == 2) {
120 server.dbnum = atoi(argv[1]);
121 if (server.dbnum < 1) {
122 err = "Invalid number of databases"; goto loaderr;
123 }
124 } else if (!strcasecmp(argv[0],"include") && argc == 2) {
125 loadServerConfig(argv[1]);
126 } else if (!strcasecmp(argv[0],"maxclients") && argc == 2) {
127 server.maxclients = atoi(argv[1]);
128 } else if (!strcasecmp(argv[0],"maxmemory") && argc == 2) {
129 server.maxmemory = memtoll(argv[1],NULL);
130 } else if (!strcasecmp(argv[0],"slaveof") && argc == 3) {
131 server.masterhost = sdsnew(argv[1]);
132 server.masterport = atoi(argv[2]);
133 server.replstate = REDIS_REPL_CONNECT;
134 } else if (!strcasecmp(argv[0],"masterauth") && argc == 2) {
135 server.masterauth = zstrdup(argv[1]);
136 } else if (!strcasecmp(argv[0],"glueoutputbuf") && argc == 2) {
137 if ((server.glueoutputbuf = yesnotoi(argv[1])) == -1) {
138 err = "argument must be 'yes' or 'no'"; goto loaderr;
139 }
140 } else if (!strcasecmp(argv[0],"rdbcompression") && argc == 2) {
141 if ((server.rdbcompression = yesnotoi(argv[1])) == -1) {
142 err = "argument must be 'yes' or 'no'"; goto loaderr;
143 }
144 } else if (!strcasecmp(argv[0],"activerehashing") && argc == 2) {
145 if ((server.activerehashing = yesnotoi(argv[1])) == -1) {
146 err = "argument must be 'yes' or 'no'"; goto loaderr;
147 }
148 } else if (!strcasecmp(argv[0],"daemonize") && argc == 2) {
149 if ((server.daemonize = yesnotoi(argv[1])) == -1) {
150 err = "argument must be 'yes' or 'no'"; goto loaderr;
151 }
152 } else if (!strcasecmp(argv[0],"appendonly") && argc == 2) {
153 if ((server.appendonly = yesnotoi(argv[1])) == -1) {
154 err = "argument must be 'yes' or 'no'"; goto loaderr;
155 }
156 } else if (!strcasecmp(argv[0],"appendfilename") && argc == 2) {
157 zfree(server.appendfilename);
158 server.appendfilename = zstrdup(argv[1]);
159 } else if (!strcasecmp(argv[0],"no-appendfsync-on-rewrite")
160 && argc == 2) {
161 if ((server.no_appendfsync_on_rewrite= yesnotoi(argv[1])) == -1) {
162 err = "argument must be 'yes' or 'no'"; goto loaderr;
163 }
164 } else if (!strcasecmp(argv[0],"appendfsync") && argc == 2) {
165 if (!strcasecmp(argv[1],"no")) {
166 server.appendfsync = APPENDFSYNC_NO;
167 } else if (!strcasecmp(argv[1],"always")) {
168 server.appendfsync = APPENDFSYNC_ALWAYS;
169 } else if (!strcasecmp(argv[1],"everysec")) {
170 server.appendfsync = APPENDFSYNC_EVERYSEC;
171 } else {
172 err = "argument must be 'no', 'always' or 'everysec'";
173 goto loaderr;
174 }
175 } else if (!strcasecmp(argv[0],"requirepass") && argc == 2) {
176 server.requirepass = zstrdup(argv[1]);
177 } else if (!strcasecmp(argv[0],"pidfile") && argc == 2) {
178 zfree(server.pidfile);
179 server.pidfile = zstrdup(argv[1]);
180 } else if (!strcasecmp(argv[0],"dbfilename") && argc == 2) {
181 zfree(server.dbfilename);
182 server.dbfilename = zstrdup(argv[1]);
183 } else if (!strcasecmp(argv[0],"vm-enabled") && argc == 2) {
184 if ((server.vm_enabled = yesnotoi(argv[1])) == -1) {
185 err = "argument must be 'yes' or 'no'"; goto loaderr;
186 }
187 } else if (!strcasecmp(argv[0],"vm-swap-file") && argc == 2) {
188 zfree(server.vm_swap_file);
189 server.vm_swap_file = zstrdup(argv[1]);
190 } else if (!strcasecmp(argv[0],"vm-max-memory") && argc == 2) {
191 server.vm_max_memory = memtoll(argv[1],NULL);
192 } else if (!strcasecmp(argv[0],"vm-page-size") && argc == 2) {
193 server.vm_page_size = memtoll(argv[1], NULL);
194 } else if (!strcasecmp(argv[0],"vm-pages") && argc == 2) {
195 server.vm_pages = memtoll(argv[1], NULL);
196 } else if (!strcasecmp(argv[0],"vm-max-threads") && argc == 2) {
197 server.vm_max_threads = strtoll(argv[1], NULL, 10);
198 } else if (!strcasecmp(argv[0],"hash-max-zipmap-entries") && argc == 2){
199 server.hash_max_zipmap_entries = memtoll(argv[1], NULL);
200 } else if (!strcasecmp(argv[0],"hash-max-zipmap-value") && argc == 2){
201 server.hash_max_zipmap_value = memtoll(argv[1], NULL);
202 } else if (!strcasecmp(argv[0],"list-max-ziplist-entries") && argc == 2){
203 server.list_max_ziplist_entries = memtoll(argv[1], NULL);
204 } else if (!strcasecmp(argv[0],"list-max-ziplist-value") && argc == 2){
205 server.list_max_ziplist_value = memtoll(argv[1], NULL);
206 } else {
207 err = "Bad directive or wrong number of arguments"; goto loaderr;
208 }
209 for (j = 0; j < argc; j++)
210 sdsfree(argv[j]);
211 zfree(argv);
212 sdsfree(line);
213 }
214 if (fp != stdin) fclose(fp);
215 return;
216
217 loaderr:
218 fprintf(stderr, "\n*** FATAL CONFIG FILE ERROR ***\n");
219 fprintf(stderr, "Reading the configuration file, at line %d\n", linenum);
220 fprintf(stderr, ">>> '%s'\n", line);
221 fprintf(stderr, "%s\n", err);
222 exit(1);
223 }
224
225 /*-----------------------------------------------------------------------------
226 * CONFIG command for remote configuration
227 *----------------------------------------------------------------------------*/
228
229 void configSetCommand(redisClient *c) {
230 robj *o = getDecodedObject(c->argv[3]);
231 long long ll;
232
233 if (!strcasecmp(c->argv[2]->ptr,"dbfilename")) {
234 zfree(server.dbfilename);
235 server.dbfilename = zstrdup(o->ptr);
236 } else if (!strcasecmp(c->argv[2]->ptr,"requirepass")) {
237 zfree(server.requirepass);
238 server.requirepass = zstrdup(o->ptr);
239 } else if (!strcasecmp(c->argv[2]->ptr,"masterauth")) {
240 zfree(server.masterauth);
241 server.masterauth = zstrdup(o->ptr);
242 } else if (!strcasecmp(c->argv[2]->ptr,"maxmemory")) {
243 if (getLongLongFromObject(o,&ll) == REDIS_ERR ||
244 ll < 0) goto badfmt;
245 server.maxmemory = ll;
246 } else if (!strcasecmp(c->argv[2]->ptr,"timeout")) {
247 if (getLongLongFromObject(o,&ll) == REDIS_ERR ||
248 ll < 0 || ll > LONG_MAX) goto badfmt;
249 server.maxidletime = ll;
250 } else if (!strcasecmp(c->argv[2]->ptr,"appendfsync")) {
251 if (!strcasecmp(o->ptr,"no")) {
252 server.appendfsync = APPENDFSYNC_NO;
253 } else if (!strcasecmp(o->ptr,"everysec")) {
254 server.appendfsync = APPENDFSYNC_EVERYSEC;
255 } else if (!strcasecmp(o->ptr,"always")) {
256 server.appendfsync = APPENDFSYNC_ALWAYS;
257 } else {
258 goto badfmt;
259 }
260 } else if (!strcasecmp(c->argv[2]->ptr,"no-appendfsync-on-rewrite")) {
261 int yn = yesnotoi(o->ptr);
262
263 if (yn == -1) goto badfmt;
264 server.no_appendfsync_on_rewrite = yn;
265 } else if (!strcasecmp(c->argv[2]->ptr,"appendonly")) {
266 int old = server.appendonly;
267 int new = yesnotoi(o->ptr);
268
269 if (new == -1) goto badfmt;
270 if (old != new) {
271 if (new == 0) {
272 stopAppendOnly();
273 } else {
274 if (startAppendOnly() == REDIS_ERR) {
275 addReplySds(c,sdscatprintf(sdsempty(),
276 "-ERR Unable to turn on AOF. Check server logs.\r\n"));
277 decrRefCount(o);
278 return;
279 }
280 }
281 }
282 } else if (!strcasecmp(c->argv[2]->ptr,"save")) {
283 int vlen, j;
284 sds *v = sdssplitlen(o->ptr,sdslen(o->ptr)," ",1,&vlen);
285
286 /* Perform sanity check before setting the new config:
287 * - Even number of args
288 * - Seconds >= 1, changes >= 0 */
289 if (vlen & 1) {
290 sdsfreesplitres(v,vlen);
291 goto badfmt;
292 }
293 for (j = 0; j < vlen; j++) {
294 char *eptr;
295 long val;
296
297 val = strtoll(v[j], &eptr, 10);
298 if (eptr[0] != '\0' ||
299 ((j & 1) == 0 && val < 1) ||
300 ((j & 1) == 1 && val < 0)) {
301 sdsfreesplitres(v,vlen);
302 goto badfmt;
303 }
304 }
305 /* Finally set the new config */
306 resetServerSaveParams();
307 for (j = 0; j < vlen; j += 2) {
308 time_t seconds;
309 int changes;
310
311 seconds = strtoll(v[j],NULL,10);
312 changes = strtoll(v[j+1],NULL,10);
313 appendServerSaveParams(seconds, changes);
314 }
315 sdsfreesplitres(v,vlen);
316 } else {
317 addReplySds(c,sdscatprintf(sdsempty(),
318 "-ERR not supported CONFIG parameter %s\r\n",
319 (char*)c->argv[2]->ptr));
320 decrRefCount(o);
321 return;
322 }
323 decrRefCount(o);
324 addReply(c,shared.ok);
325 return;
326
327 badfmt: /* Bad format errors */
328 addReplySds(c,sdscatprintf(sdsempty(),
329 "-ERR invalid argument '%s' for CONFIG SET '%s'\r\n",
330 (char*)o->ptr,
331 (char*)c->argv[2]->ptr));
332 decrRefCount(o);
333 }
334
335 void configGetCommand(redisClient *c) {
336 robj *o = getDecodedObject(c->argv[2]);
337 robj *lenobj = createObject(REDIS_STRING,NULL);
338 char *pattern = o->ptr;
339 int matches = 0;
340
341 addReply(c,lenobj);
342 decrRefCount(lenobj);
343
344 if (stringmatch(pattern,"dbfilename",0)) {
345 addReplyBulkCString(c,"dbfilename");
346 addReplyBulkCString(c,server.dbfilename);
347 matches++;
348 }
349 if (stringmatch(pattern,"requirepass",0)) {
350 addReplyBulkCString(c,"requirepass");
351 addReplyBulkCString(c,server.requirepass);
352 matches++;
353 }
354 if (stringmatch(pattern,"masterauth",0)) {
355 addReplyBulkCString(c,"masterauth");
356 addReplyBulkCString(c,server.masterauth);
357 matches++;
358 }
359 if (stringmatch(pattern,"maxmemory",0)) {
360 char buf[128];
361
362 ll2string(buf,128,server.maxmemory);
363 addReplyBulkCString(c,"maxmemory");
364 addReplyBulkCString(c,buf);
365 matches++;
366 }
367 if (stringmatch(pattern,"timeout",0)) {
368 char buf[128];
369
370 ll2string(buf,128,server.maxidletime);
371 addReplyBulkCString(c,"timeout");
372 addReplyBulkCString(c,buf);
373 matches++;
374 }
375 if (stringmatch(pattern,"appendonly",0)) {
376 addReplyBulkCString(c,"appendonly");
377 addReplyBulkCString(c,server.appendonly ? "yes" : "no");
378 matches++;
379 }
380 if (stringmatch(pattern,"no-appendfsync-on-rewrite",0)) {
381 addReplyBulkCString(c,"no-appendfsync-on-rewrite");
382 addReplyBulkCString(c,server.no_appendfsync_on_rewrite ? "yes" : "no");
383 matches++;
384 }
385 if (stringmatch(pattern,"appendfsync",0)) {
386 char *policy;
387
388 switch(server.appendfsync) {
389 case APPENDFSYNC_NO: policy = "no"; break;
390 case APPENDFSYNC_EVERYSEC: policy = "everysec"; break;
391 case APPENDFSYNC_ALWAYS: policy = "always"; break;
392 default: policy = "unknown"; break; /* too harmless to panic */
393 }
394 addReplyBulkCString(c,"appendfsync");
395 addReplyBulkCString(c,policy);
396 matches++;
397 }
398 if (stringmatch(pattern,"save",0)) {
399 sds buf = sdsempty();
400 int j;
401
402 for (j = 0; j < server.saveparamslen; j++) {
403 buf = sdscatprintf(buf,"%ld %d",
404 server.saveparams[j].seconds,
405 server.saveparams[j].changes);
406 if (j != server.saveparamslen-1)
407 buf = sdscatlen(buf," ",1);
408 }
409 addReplyBulkCString(c,"save");
410 addReplyBulkCString(c,buf);
411 sdsfree(buf);
412 matches++;
413 }
414 decrRefCount(o);
415 lenobj->ptr = sdscatprintf(sdsempty(),"*%d\r\n",matches*2);
416 }
417
418 void configCommand(redisClient *c) {
419 if (!strcasecmp(c->argv[1]->ptr,"set")) {
420 if (c->argc != 4) goto badarity;
421 configSetCommand(c);
422 } else if (!strcasecmp(c->argv[1]->ptr,"get")) {
423 if (c->argc != 3) goto badarity;
424 configGetCommand(c);
425 } else if (!strcasecmp(c->argv[1]->ptr,"resetstat")) {
426 if (c->argc != 2) goto badarity;
427 server.stat_numcommands = 0;
428 server.stat_numconnections = 0;
429 server.stat_expiredkeys = 0;
430 server.stat_starttime = time(NULL);
431 addReply(c,shared.ok);
432 } else {
433 addReplySds(c,sdscatprintf(sdsempty(),
434 "-ERR CONFIG subcommand must be one of GET, SET, RESETSTAT\r\n"));
435 }
436 return;
437
438 badarity:
439 addReplySds(c,sdscatprintf(sdsempty(),
440 "-ERR Wrong number of arguments for CONFIG %s\r\n",
441 (char*) c->argv[1]->ptr));
442 }