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