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 #include "redis.h" 32 #include "cluster.h" 33 34 #include <fcntl.h> 35 #include <sys/stat.h> 36 37 static struct { 38 const char *name; 39 const int value; 40 } validSyslogFacilities[] = { 41 {"user", LOG_USER}, 42 {"local0", LOG_LOCAL0}, 43 {"local1", LOG_LOCAL1}, 44 {"local2", LOG_LOCAL2}, 45 {"local3", LOG_LOCAL3}, 46 {"local4", LOG_LOCAL4}, 47 {"local5", LOG_LOCAL5}, 48 {"local6", LOG_LOCAL6}, 49 {"local7", LOG_LOCAL7}, 50 {NULL, 0} 51 }; 52 53 clientBufferLimitsConfig clientBufferLimitsDefaults[REDIS_CLIENT_TYPE_COUNT] = { 54 {0, 0, 0}, /* normal */ 55 {1024*1024*256, 1024*1024*64, 60}, /* slave */ 56 {1024*1024*32, 1024*1024*8, 60} /* pubsub */ 57 }; 58 59 /*----------------------------------------------------------------------------- 60 * Config file parsing 61 *----------------------------------------------------------------------------*/ 62 63 int yesnotoi(char *s) { 64 if (!strcasecmp(s,"yes")) return 1; 65 else if (!strcasecmp(s,"no")) return 0; 66 else return -1; 67 } 68 69 void appendServerSaveParams(time_t seconds, int changes) { 70 server.saveparams = zrealloc(server.saveparams,sizeof(struct saveparam)*(server.saveparamslen+1)); 71 server.saveparams[server.saveparamslen].seconds = seconds; 72 server.saveparams[server.saveparamslen].changes = changes; 73 server.saveparamslen++; 74 } 75 76 void resetServerSaveParams() { 77 zfree(server.saveparams); 78 server.saveparams = NULL; 79 server.saveparamslen = 0; 80 } 81 82 void loadServerConfigFromString(char *config) { 83 char *err = NULL; 84 int linenum = 0, totlines, i; 85 int slaveof_linenum = 0; 86 sds *lines; 87 88 lines = sdssplitlen(config,strlen(config),"\n",1,&totlines); 89 90 for (i = 0; i < totlines; i++) { 91 sds *argv; 92 int argc; 93 94 linenum = i+1; 95 lines[i] = sdstrim(lines[i]," \t\r\n"); 96 97 /* Skip comments and blank lines */ 98 if (lines[i][0] == '#' || lines[i][0] == '\0') continue; 99 100 /* Split into arguments */ 101 argv = sdssplitargs(lines[i],&argc); 102 if (argv == NULL) { 103 err = "Unbalanced quotes in configuration line"; 104 goto loaderr; 105 } 106 107 /* Skip this line if the resulting command vector is empty. */ 108 if (argc == 0) { 109 sdsfreesplitres(argv,argc); 110 continue; 111 } 112 sdstolower(argv[0]); 113 114 /* Execute config directives */ 115 if (!strcasecmp(argv[0],"timeout") && argc == 2) { 116 server.maxidletime = atoi(argv[1]); 117 if (server.maxidletime < 0) { 118 err = "Invalid timeout value"; goto loaderr; 119 } 120 } else if (!strcasecmp(argv[0],"tcp-keepalive") && argc == 2) { 121 server.tcpkeepalive = atoi(argv[1]); 122 if (server.tcpkeepalive < 0) { 123 err = "Invalid tcp-keepalive value"; goto loaderr; 124 } 125 } else if (!strcasecmp(argv[0],"port") && argc == 2) { 126 server.port = atoi(argv[1]); 127 if (server.port < 0 || server.port > 65535) { 128 err = "Invalid port"; goto loaderr; 129 } 130 } else if (!strcasecmp(argv[0],"tcp-backlog") && argc == 2) { 131 server.tcp_backlog = atoi(argv[1]); 132 if (server.tcp_backlog < 0) { 133 err = "Invalid backlog value"; goto loaderr; 134 } 135 } else if (!strcasecmp(argv[0],"bind") && argc >= 2) { 136 int j, addresses = argc-1; 137 138 if (addresses > REDIS_BINDADDR_MAX) { 139 err = "Too many bind addresses specified"; goto loaderr; 140 } 141 for (j = 0; j < addresses; j++) 142 server.bindaddr[j] = zstrdup(argv[j+1]); 143 server.bindaddr_count = addresses; 144 } else if (!strcasecmp(argv[0],"unixsocket") && argc == 2) { 145 server.unixsocket = zstrdup(argv[1]); 146 } else if (!strcasecmp(argv[0],"unixsocketperm") && argc == 2) { 147 errno = 0; 148 server.unixsocketperm = (mode_t)strtol(argv[1], NULL, 8); 149 if (errno || server.unixsocketperm > 0777) { 150 err = "Invalid socket file permissions"; goto loaderr; 151 } 152 } else if (!strcasecmp(argv[0],"save")) { 153 if (argc == 3) { 154 int seconds = atoi(argv[1]); 155 int changes = atoi(argv[2]); 156 if (seconds < 1 || changes < 0) { 157 err = "Invalid save parameters"; goto loaderr; 158 } 159 appendServerSaveParams(seconds,changes); 160 } else if (argc == 2 && !strcasecmp(argv[1],"")) { 161 resetServerSaveParams(); 162 } 163 } else if (!strcasecmp(argv[0],"dir") && argc == 2) { 164 if (chdir(argv[1]) == -1) { 165 redisLog(REDIS_WARNING,"Can't chdir to '%s': %s", 166 argv[1], strerror(errno)); 167 exit(1); 168 } 169 } else if (!strcasecmp(argv[0],"loglevel") && argc == 2) { 170 if (!strcasecmp(argv[1],"debug")) server.verbosity = REDIS_DEBUG; 171 else if (!strcasecmp(argv[1],"verbose")) server.verbosity = REDIS_VERBOSE; 172 else if (!strcasecmp(argv[1],"notice")) server.verbosity = REDIS_NOTICE; 173 else if (!strcasecmp(argv[1],"warning")) server.verbosity = REDIS_WARNING; 174 else { 175 err = "Invalid log level. Must be one of debug, notice, warning"; 176 goto loaderr; 177 } 178 } else if (!strcasecmp(argv[0],"logfile") && argc == 2) { 179 FILE *logfp; 180 181 zfree(server.logfile); 182 server.logfile = zstrdup(argv[1]); 183 if (server.logfile[0] != '\0') { 184 /* Test if we are able to open the file. The server will not 185 * be able to abort just for this problem later... */ 186 logfp = fopen(server.logfile,"a"); 187 if (logfp == NULL) { 188 err = sdscatprintf(sdsempty(), 189 "Can't open the log file: %s", strerror(errno)); 190 goto loaderr; 191 } 192 fclose(logfp); 193 } 194 } else if (!strcasecmp(argv[0],"syslog-enabled") && argc == 2) { 195 if ((server.syslog_enabled = yesnotoi(argv[1])) == -1) { 196 err = "argument must be 'yes' or 'no'"; goto loaderr; 197 } 198 } else if (!strcasecmp(argv[0],"syslog-ident") && argc == 2) { 199 if (server.syslog_ident) zfree(server.syslog_ident); 200 server.syslog_ident = zstrdup(argv[1]); 201 } else if (!strcasecmp(argv[0],"syslog-facility") && argc == 2) { 202 int i; 203 204 for (i = 0; validSyslogFacilities[i].name; i++) { 205 if (!strcasecmp(validSyslogFacilities[i].name, argv[1])) { 206 server.syslog_facility = validSyslogFacilities[i].value; 207 break; 208 } 209 } 210 211 if (!validSyslogFacilities[i].name) { 212 err = "Invalid log facility. Must be one of USER or between LOCAL0-LOCAL7"; 213 goto loaderr; 214 } 215 } else if (!strcasecmp(argv[0],"databases") && argc == 2) { 216 server.dbnum = atoi(argv[1]); 217 if (server.dbnum < 1) { 218 err = "Invalid number of databases"; goto loaderr; 219 } 220 } else if (!strcasecmp(argv[0],"include") && argc == 2) { 221 loadServerConfig(argv[1],NULL); 222 } else if (!strcasecmp(argv[0],"maxclients") && argc == 2) { 223 server.maxclients = atoi(argv[1]); 224 if (server.maxclients < 1) { 225 err = "Invalid max clients limit"; goto loaderr; 226 } 227 } else if (!strcasecmp(argv[0],"maxmemory") && argc == 2) { 228 server.maxmemory = memtoll(argv[1],NULL); 229 } else if (!strcasecmp(argv[0],"maxmemory-policy") && argc == 2) { 230 if (!strcasecmp(argv[1],"volatile-lru")) { 231 server.maxmemory_policy = REDIS_MAXMEMORY_VOLATILE_LRU; 232 } else if (!strcasecmp(argv[1],"volatile-random")) { 233 server.maxmemory_policy = REDIS_MAXMEMORY_VOLATILE_RANDOM; 234 } else if (!strcasecmp(argv[1],"volatile-ttl")) { 235 server.maxmemory_policy = REDIS_MAXMEMORY_VOLATILE_TTL; 236 } else if (!strcasecmp(argv[1],"allkeys-lru")) { 237 server.maxmemory_policy = REDIS_MAXMEMORY_ALLKEYS_LRU; 238 } else if (!strcasecmp(argv[1],"allkeys-random")) { 239 server.maxmemory_policy = REDIS_MAXMEMORY_ALLKEYS_RANDOM; 240 } else if (!strcasecmp(argv[1],"noeviction")) { 241 server.maxmemory_policy = REDIS_MAXMEMORY_NO_EVICTION; 242 } else { 243 err = "Invalid maxmemory policy"; 244 goto loaderr; 245 } 246 } else if (!strcasecmp(argv[0],"maxmemory-samples") && argc == 2) { 247 server.maxmemory_samples = atoi(argv[1]); 248 if (server.maxmemory_samples <= 0) { 249 err = "maxmemory-samples must be 1 or greater"; 250 goto loaderr; 251 } 252 } else if (!strcasecmp(argv[0],"slaveof") && argc == 3) { 253 slaveof_linenum = linenum; 254 server.masterhost = sdsnew(argv[1]); 255 server.masterport = atoi(argv[2]); 256 server.repl_state = REDIS_REPL_CONNECT; 257 } else if (!strcasecmp(argv[0],"repl-ping-slave-period") && argc == 2) { 258 server.repl_ping_slave_period = atoi(argv[1]); 259 if (server.repl_ping_slave_period <= 0) { 260 err = "repl-ping-slave-period must be 1 or greater"; 261 goto loaderr; 262 } 263 } else if (!strcasecmp(argv[0],"repl-timeout") && argc == 2) { 264 server.repl_timeout = atoi(argv[1]); 265 if (server.repl_timeout <= 0) { 266 err = "repl-timeout must be 1 or greater"; 267 goto loaderr; 268 } 269 } else if (!strcasecmp(argv[0],"repl-disable-tcp-nodelay") && argc==2) { 270 if ((server.repl_disable_tcp_nodelay = yesnotoi(argv[1])) == -1) { 271 err = "argument must be 'yes' or 'no'"; goto loaderr; 272 } 273 } else if (!strcasecmp(argv[0],"repl-backlog-size") && argc == 2) { 274 long long size = memtoll(argv[1],NULL); 275 if (size <= 0) { 276 err = "repl-backlog-size must be 1 or greater."; 277 goto loaderr; 278 } 279 resizeReplicationBacklog(size); 280 } else if (!strcasecmp(argv[0],"repl-backlog-ttl") && argc == 2) { 281 server.repl_backlog_time_limit = atoi(argv[1]); 282 if (server.repl_backlog_time_limit < 0) { 283 err = "repl-backlog-ttl can't be negative "; 284 goto loaderr; 285 } 286 } else if (!strcasecmp(argv[0],"masterauth") && argc == 2) { 287 server.masterauth = zstrdup(argv[1]); 288 } else if (!strcasecmp(argv[0],"slave-serve-stale-data") && argc == 2) { 289 if ((server.repl_serve_stale_data = yesnotoi(argv[1])) == -1) { 290 err = "argument must be 'yes' or 'no'"; goto loaderr; 291 } 292 } else if (!strcasecmp(argv[0],"slave-read-only") && argc == 2) { 293 if ((server.repl_slave_ro = yesnotoi(argv[1])) == -1) { 294 err = "argument must be 'yes' or 'no'"; goto loaderr; 295 } 296 } else if (!strcasecmp(argv[0],"rdbcompression") && argc == 2) { 297 if ((server.rdb_compression = yesnotoi(argv[1])) == -1) { 298 err = "argument must be 'yes' or 'no'"; goto loaderr; 299 } 300 } else if (!strcasecmp(argv[0],"rdbchecksum") && argc == 2) { 301 if ((server.rdb_checksum = yesnotoi(argv[1])) == -1) { 302 err = "argument must be 'yes' or 'no'"; goto loaderr; 303 } 304 } else if (!strcasecmp(argv[0],"activerehashing") && argc == 2) { 305 if ((server.activerehashing = yesnotoi(argv[1])) == -1) { 306 err = "argument must be 'yes' or 'no'"; goto loaderr; 307 } 308 } else if (!strcasecmp(argv[0],"daemonize") && argc == 2) { 309 if ((server.daemonize = yesnotoi(argv[1])) == -1) { 310 err = "argument must be 'yes' or 'no'"; goto loaderr; 311 } 312 } else if (!strcasecmp(argv[0],"hz") && argc == 2) { 313 server.hz = atoi(argv[1]); 314 if (server.hz < REDIS_MIN_HZ) server.hz = REDIS_MIN_HZ; 315 if (server.hz > REDIS_MAX_HZ) server.hz = REDIS_MAX_HZ; 316 } else if (!strcasecmp(argv[0],"appendonly") && argc == 2) { 317 int yes; 318 319 if ((yes = yesnotoi(argv[1])) == -1) { 320 err = "argument must be 'yes' or 'no'"; goto loaderr; 321 } 322 server.aof_state = yes ? REDIS_AOF_ON : REDIS_AOF_OFF; 323 } else if (!strcasecmp(argv[0],"appendfilename") && argc == 2) { 324 if (!pathIsBaseName(argv[1])) { 325 err = "appendfilename can't be a path, just a filename"; 326 goto loaderr; 327 } 328 zfree(server.aof_filename); 329 server.aof_filename = zstrdup(argv[1]); 330 } else if (!strcasecmp(argv[0],"no-appendfsync-on-rewrite") 331 && argc == 2) { 332 if ((server.aof_no_fsync_on_rewrite= yesnotoi(argv[1])) == -1) { 333 err = "argument must be 'yes' or 'no'"; goto loaderr; 334 } 335 } else if (!strcasecmp(argv[0],"appendfsync") && argc == 2) { 336 if (!strcasecmp(argv[1],"no")) { 337 server.aof_fsync = AOF_FSYNC_NO; 338 } else if (!strcasecmp(argv[1],"always")) { 339 server.aof_fsync = AOF_FSYNC_ALWAYS; 340 } else if (!strcasecmp(argv[1],"everysec")) { 341 server.aof_fsync = AOF_FSYNC_EVERYSEC; 342 } else { 343 err = "argument must be 'no', 'always' or 'everysec'"; 344 goto loaderr; 345 } 346 } else if (!strcasecmp(argv[0],"auto-aof-rewrite-percentage") && 347 argc == 2) 348 { 349 server.aof_rewrite_perc = atoi(argv[1]); 350 if (server.aof_rewrite_perc < 0) { 351 err = "Invalid negative percentage for AOF auto rewrite"; 352 goto loaderr; 353 } 354 } else if (!strcasecmp(argv[0],"auto-aof-rewrite-min-size") && 355 argc == 2) 356 { 357 server.aof_rewrite_min_size = memtoll(argv[1],NULL); 358 } else if (!strcasecmp(argv[0],"aof-rewrite-incremental-fsync") && 359 argc == 2) 360 { 361 if ((server.aof_rewrite_incremental_fsync = yesnotoi(argv[1])) == -1) { 362 err = "argument must be 'yes' or 'no'"; goto loaderr; 363 } 364 } else if (!strcasecmp(argv[0],"requirepass") && argc == 2) { 365 if (strlen(argv[1]) > REDIS_AUTHPASS_MAX_LEN) { 366 err = "Password is longer than REDIS_AUTHPASS_MAX_LEN"; 367 goto loaderr; 368 } 369 server.requirepass = zstrdup(argv[1]); 370 } else if (!strcasecmp(argv[0],"pidfile") && argc == 2) { 371 zfree(server.pidfile); 372 server.pidfile = zstrdup(argv[1]); 373 } else if (!strcasecmp(argv[0],"dbfilename") && argc == 2) { 374 if (!pathIsBaseName(argv[1])) { 375 err = "dbfilename can't be a path, just a filename"; 376 goto loaderr; 377 } 378 zfree(server.rdb_filename); 379 server.rdb_filename = zstrdup(argv[1]); 380 } else if (!strcasecmp(argv[0],"hash-max-ziplist-entries") && argc == 2) { 381 server.hash_max_ziplist_entries = memtoll(argv[1], NULL); 382 } else if (!strcasecmp(argv[0],"hash-max-ziplist-value") && argc == 2) { 383 server.hash_max_ziplist_value = memtoll(argv[1], NULL); 384 } else if (!strcasecmp(argv[0],"list-max-ziplist-entries") && argc == 2){ 385 server.list_max_ziplist_entries = memtoll(argv[1], NULL); 386 } else if (!strcasecmp(argv[0],"list-max-ziplist-value") && argc == 2) { 387 server.list_max_ziplist_value = memtoll(argv[1], NULL); 388 } else if (!strcasecmp(argv[0],"set-max-intset-entries") && argc == 2) { 389 server.set_max_intset_entries = memtoll(argv[1], NULL); 390 } else if (!strcasecmp(argv[0],"zset-max-ziplist-entries") && argc == 2) { 391 server.zset_max_ziplist_entries = memtoll(argv[1], NULL); 392 } else if (!strcasecmp(argv[0],"zset-max-ziplist-value") && argc == 2) { 393 server.zset_max_ziplist_value = memtoll(argv[1], NULL); 394 } else if (!strcasecmp(argv[0],"hll-sparse-max-bytes") && argc == 2) { 395 server.hll_sparse_max_bytes = memtoll(argv[1], NULL); 396 } else if (!strcasecmp(argv[0],"rename-command") && argc == 3) { 397 struct redisCommand *cmd = lookupCommand(argv[1]); 398 int retval; 399 400 if (!cmd) { 401 err = "No such command in rename-command"; 402 goto loaderr; 403 } 404 405 /* If the target command name is the empty string we just 406 * remove it from the command table. */ 407 retval = dictDelete(server.commands, argv[1]); 408 redisAssert(retval == DICT_OK); 409 410 /* Otherwise we re-add the command under a different name. */ 411 if (sdslen(argv[2]) != 0) { 412 sds copy = sdsdup(argv[2]); 413 414 retval = dictAdd(server.commands, copy, cmd); 415 if (retval != DICT_OK) { 416 sdsfree(copy); 417 err = "Target command name already exists"; goto loaderr; 418 } 419 } 420 } else if (!strcasecmp(argv[0],"cluster-enabled") && argc == 2) { 421 if ((server.cluster_enabled = yesnotoi(argv[1])) == -1) { 422 err = "argument must be 'yes' or 'no'"; goto loaderr; 423 } 424 } else if (!strcasecmp(argv[0],"cluster-config-file") && argc == 2) { 425 zfree(server.cluster_configfile); 426 server.cluster_configfile = zstrdup(argv[1]); 427 } else if (!strcasecmp(argv[0],"cluster-node-timeout") && argc == 2) { 428 server.cluster_node_timeout = strtoll(argv[1],NULL,10); 429 if (server.cluster_node_timeout <= 0) { 430 err = "cluster node timeout must be 1 or greater"; goto loaderr; 431 } 432 } else if (!strcasecmp(argv[0],"cluster-migration-barrier") 433 && argc == 2) 434 { 435 server.cluster_migration_barrier = atoi(argv[1]); 436 if (server.cluster_migration_barrier < 0) { 437 err = "cluster migration barrier must zero or positive"; 438 goto loaderr; 439 } 440 } else if (!strcasecmp(argv[0],"cluster-slave-validity-factor") 441 && argc == 2) 442 { 443 server.cluster_slave_validity_factor = atoi(argv[1]); 444 if (server.cluster_slave_validity_factor < 0) { 445 err = "cluster slave validity factor must be zero or positive"; 446 goto loaderr; 447 } 448 } else if (!strcasecmp(argv[0],"lua-time-limit") && argc == 2) { 449 server.lua_time_limit = strtoll(argv[1],NULL,10); 450 } else if (!strcasecmp(argv[0],"slowlog-log-slower-than") && 451 argc == 2) 452 { 453 server.slowlog_log_slower_than = strtoll(argv[1],NULL,10); 454 } else if (!strcasecmp(argv[0],"latency-monitor-threshold") && 455 argc == 2) 456 { 457 server.latency_monitor_threshold = strtoll(argv[1],NULL,10); 458 if (server.latency_monitor_threshold < 0) { 459 err = "The latency threshold can't be negative"; 460 goto loaderr; 461 } 462 } else if (!strcasecmp(argv[0],"slowlog-max-len") && argc == 2) { 463 server.slowlog_max_len = strtoll(argv[1],NULL,10); 464 } else if (!strcasecmp(argv[0],"client-output-buffer-limit") && 465 argc == 5) 466 { 467 int class = getClientTypeByName(argv[1]); 468 unsigned long long hard, soft; 469 int soft_seconds; 470 471 if (class == -1) { 472 err = "Unrecognized client limit class"; 473 goto loaderr; 474 } 475 hard = memtoll(argv[2],NULL); 476 soft = memtoll(argv[3],NULL); 477 soft_seconds = atoi(argv[4]); 478 if (soft_seconds < 0) { 479 err = "Negative number of seconds in soft limit is invalid"; 480 goto loaderr; 481 } 482 server.client_obuf_limits[class].hard_limit_bytes = hard; 483 server.client_obuf_limits[class].soft_limit_bytes = soft; 484 server.client_obuf_limits[class].soft_limit_seconds = soft_seconds; 485 } else if (!strcasecmp(argv[0],"stop-writes-on-bgsave-error") && 486 argc == 2) { 487 if ((server.stop_writes_on_bgsave_err = yesnotoi(argv[1])) == -1) { 488 err = "argument must be 'yes' or 'no'"; goto loaderr; 489 } 490 } else if (!strcasecmp(argv[0],"slave-priority") && argc == 2) { 491 server.slave_priority = atoi(argv[1]); 492 } else if (!strcasecmp(argv[0],"min-slaves-to-write") && argc == 2) { 493 server.repl_min_slaves_to_write = atoi(argv[1]); 494 if (server.repl_min_slaves_to_write < 0) { 495 err = "Invalid value for min-slaves-to-write."; goto loaderr; 496 } 497 } else if (!strcasecmp(argv[0],"min-slaves-max-lag") && argc == 2) { 498 server.repl_min_slaves_max_lag = atoi(argv[1]); 499 if (server.repl_min_slaves_max_lag < 0) { 500 err = "Invalid value for min-slaves-max-lag."; goto loaderr; 501 } 502 } else if (!strcasecmp(argv[0],"notify-keyspace-events") && argc == 2) { 503 int flags = keyspaceEventsStringToFlags(argv[1]); 504 505 if (flags == -1) { 506 err = "Invalid event class character. Use 'g$lshzxeA'."; 507 goto loaderr; 508 } 509 server.notify_keyspace_events = flags; 510 } else if (!strcasecmp(argv[0],"sentinel")) { 511 /* argc == 1 is handled by main() as we need to enter the sentinel 512 * mode ASAP. */ 513 if (argc != 1) { 514 if (!server.sentinel_mode) { 515 err = "sentinel directive while not in sentinel mode"; 516 goto loaderr; 517 } 518 err = sentinelHandleConfiguration(argv+1,argc-1); 519 if (err) goto loaderr; 520 } 521 } else { 522 err = "Bad directive or wrong number of arguments"; goto loaderr; 523 } 524 sdsfreesplitres(argv,argc); 525 } 526 527 /* Sanity checks. */ 528 if (server.cluster_enabled && server.masterhost) { 529 linenum = slaveof_linenum; 530 i = linenum-1; 531 err = "slaveof directive not allowed in cluster mode"; 532 goto loaderr; 533 } 534 535 sdsfreesplitres(lines,totlines); 536 return; 537 538 loaderr: 539 fprintf(stderr, "\n*** FATAL CONFIG FILE ERROR ***\n"); 540 fprintf(stderr, "Reading the configuration file, at line %d\n", linenum); 541 fprintf(stderr, ">>> '%s'\n", lines[i]); 542 fprintf(stderr, "%s\n", err); 543 exit(1); 544 } 545 546 /* Load the server configuration from the specified filename. 547 * The function appends the additional configuration directives stored 548 * in the 'options' string to the config file before loading. 549 * 550 * Both filename and options can be NULL, in such a case are considered 551 * empty. This way loadServerConfig can be used to just load a file or 552 * just load a string. */ 553 void loadServerConfig(char *filename, char *options) { 554 sds config = sdsempty(); 555 char buf[REDIS_CONFIGLINE_MAX+1]; 556 557 /* Load the file content */ 558 if (filename) { 559 FILE *fp; 560 561 if (filename[0] == '-' && filename[1] == '\0') { 562 fp = stdin; 563 } else { 564 if ((fp = fopen(filename,"r")) == NULL) { 565 redisLog(REDIS_WARNING, 566 "Fatal error, can't open config file '%s'", filename); 567 exit(1); 568 } 569 } 570 while(fgets(buf,REDIS_CONFIGLINE_MAX+1,fp) != NULL) 571 config = sdscat(config,buf); 572 if (fp != stdin) fclose(fp); 573 } 574 /* Append the additional options */ 575 if (options) { 576 config = sdscat(config,"\n"); 577 config = sdscat(config,options); 578 } 579 loadServerConfigFromString(config); 580 sdsfree(config); 581 } 582 583 /*----------------------------------------------------------------------------- 584 * CONFIG SET implementation 585 *----------------------------------------------------------------------------*/ 586 587 void configSetCommand(redisClient *c) { 588 robj *o; 589 long long ll; 590 redisAssertWithInfo(c,c->argv[2],sdsEncodedObject(c->argv[2])); 591 redisAssertWithInfo(c,c->argv[3],sdsEncodedObject(c->argv[3])); 592 o = c->argv[3]; 593 594 if (!strcasecmp(c->argv[2]->ptr,"dbfilename")) { 595 if (!pathIsBaseName(o->ptr)) { 596 addReplyError(c, "dbfilename can't be a path, just a filename"); 597 return; 598 } 599 zfree(server.rdb_filename); 600 server.rdb_filename = zstrdup(o->ptr); 601 } else if (!strcasecmp(c->argv[2]->ptr,"requirepass")) { 602 if (sdslen(o->ptr) > REDIS_AUTHPASS_MAX_LEN) goto badfmt; 603 zfree(server.requirepass); 604 server.requirepass = ((char*)o->ptr)[0] ? zstrdup(o->ptr) : NULL; 605 } else if (!strcasecmp(c->argv[2]->ptr,"masterauth")) { 606 zfree(server.masterauth); 607 server.masterauth = ((char*)o->ptr)[0] ? zstrdup(o->ptr) : NULL; 608 } else if (!strcasecmp(c->argv[2]->ptr,"maxmemory")) { 609 if (getLongLongFromObject(o,&ll) == REDIS_ERR || 610 ll < 0) goto badfmt; 611 server.maxmemory = ll; 612 if (server.maxmemory) { 613 if (server.maxmemory < zmalloc_used_memory()) { 614 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."); 615 } 616 freeMemoryIfNeeded(); 617 } 618 } else if (!strcasecmp(c->argv[2]->ptr,"maxclients")) { 619 int orig_value = server.maxclients; 620 621 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 1) goto badfmt; 622 623 /* Try to check if the OS is capable of supporting so many FDs. */ 624 server.maxclients = ll; 625 if (ll > orig_value) { 626 adjustOpenFilesLimit(); 627 if (server.maxclients != ll) { 628 addReplyErrorFormat(c,"The operating system is not able to handle the specified number of clients, try with %d", server.maxclients); 629 server.maxclients = orig_value; 630 return; 631 } 632 if (aeGetSetSize(server.el) < 633 server.maxclients + REDIS_EVENTLOOP_FDSET_INCR) 634 { 635 if (aeResizeSetSize(server.el, 636 server.maxclients + REDIS_EVENTLOOP_FDSET_INCR) == AE_ERR) 637 { 638 addReplyError(c,"The event loop API used by Redis is not able to handle the specified number of clients"); 639 server.maxclients = orig_value; 640 return; 641 } 642 } 643 } 644 } else if (!strcasecmp(c->argv[2]->ptr,"hz")) { 645 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt; 646 server.hz = ll; 647 if (server.hz < REDIS_MIN_HZ) server.hz = REDIS_MIN_HZ; 648 if (server.hz > REDIS_MAX_HZ) server.hz = REDIS_MAX_HZ; 649 } else if (!strcasecmp(c->argv[2]->ptr,"maxmemory-policy")) { 650 if (!strcasecmp(o->ptr,"volatile-lru")) { 651 server.maxmemory_policy = REDIS_MAXMEMORY_VOLATILE_LRU; 652 } else if (!strcasecmp(o->ptr,"volatile-random")) { 653 server.maxmemory_policy = REDIS_MAXMEMORY_VOLATILE_RANDOM; 654 } else if (!strcasecmp(o->ptr,"volatile-ttl")) { 655 server.maxmemory_policy = REDIS_MAXMEMORY_VOLATILE_TTL; 656 } else if (!strcasecmp(o->ptr,"allkeys-lru")) { 657 server.maxmemory_policy = REDIS_MAXMEMORY_ALLKEYS_LRU; 658 } else if (!strcasecmp(o->ptr,"allkeys-random")) { 659 server.maxmemory_policy = REDIS_MAXMEMORY_ALLKEYS_RANDOM; 660 } else if (!strcasecmp(o->ptr,"noeviction")) { 661 server.maxmemory_policy = REDIS_MAXMEMORY_NO_EVICTION; 662 } else { 663 goto badfmt; 664 } 665 } else if (!strcasecmp(c->argv[2]->ptr,"maxmemory-samples")) { 666 if (getLongLongFromObject(o,&ll) == REDIS_ERR || 667 ll <= 0) goto badfmt; 668 server.maxmemory_samples = ll; 669 } else if (!strcasecmp(c->argv[2]->ptr,"timeout")) { 670 if (getLongLongFromObject(o,&ll) == REDIS_ERR || 671 ll < 0 || ll > LONG_MAX) goto badfmt; 672 server.maxidletime = ll; 673 } else if (!strcasecmp(c->argv[2]->ptr,"tcp-keepalive")) { 674 if (getLongLongFromObject(o,&ll) == REDIS_ERR || 675 ll < 0 || ll > INT_MAX) goto badfmt; 676 server.tcpkeepalive = ll; 677 } else if (!strcasecmp(c->argv[2]->ptr,"appendfsync")) { 678 if (!strcasecmp(o->ptr,"no")) { 679 server.aof_fsync = AOF_FSYNC_NO; 680 } else if (!strcasecmp(o->ptr,"everysec")) { 681 server.aof_fsync = AOF_FSYNC_EVERYSEC; 682 } else if (!strcasecmp(o->ptr,"always")) { 683 server.aof_fsync = AOF_FSYNC_ALWAYS; 684 } else { 685 goto badfmt; 686 } 687 } else if (!strcasecmp(c->argv[2]->ptr,"no-appendfsync-on-rewrite")) { 688 int yn = yesnotoi(o->ptr); 689 690 if (yn == -1) goto badfmt; 691 server.aof_no_fsync_on_rewrite = yn; 692 } else if (!strcasecmp(c->argv[2]->ptr,"appendonly")) { 693 int enable = yesnotoi(o->ptr); 694 695 if (enable == -1) goto badfmt; 696 if (enable == 0 && server.aof_state != REDIS_AOF_OFF) { 697 stopAppendOnly(); 698 } else if (enable && server.aof_state == REDIS_AOF_OFF) { 699 if (startAppendOnly() == REDIS_ERR) { 700 addReplyError(c, 701 "Unable to turn on AOF. Check server logs."); 702 return; 703 } 704 } 705 } else if (!strcasecmp(c->argv[2]->ptr,"auto-aof-rewrite-percentage")) { 706 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt; 707 server.aof_rewrite_perc = ll; 708 } else if (!strcasecmp(c->argv[2]->ptr,"auto-aof-rewrite-min-size")) { 709 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt; 710 server.aof_rewrite_min_size = ll; 711 } else if (!strcasecmp(c->argv[2]->ptr,"aof-rewrite-incremental-fsync")) { 712 int yn = yesnotoi(o->ptr); 713 714 if (yn == -1) goto badfmt; 715 server.aof_rewrite_incremental_fsync = yn; 716 } else if (!strcasecmp(c->argv[2]->ptr,"save")) { 717 int vlen, j; 718 sds *v = sdssplitlen(o->ptr,sdslen(o->ptr)," ",1,&vlen); 719 720 /* Perform sanity check before setting the new config: 721 * - Even number of args 722 * - Seconds >= 1, changes >= 0 */ 723 if (vlen & 1) { 724 sdsfreesplitres(v,vlen); 725 goto badfmt; 726 } 727 for (j = 0; j < vlen; j++) { 728 char *eptr; 729 long val; 730 731 val = strtoll(v[j], &eptr, 10); 732 if (eptr[0] != '\0' || 733 ((j & 1) == 0 && val < 1) || 734 ((j & 1) == 1 && val < 0)) { 735 sdsfreesplitres(v,vlen); 736 goto badfmt; 737 } 738 } 739 /* Finally set the new config */ 740 resetServerSaveParams(); 741 for (j = 0; j < vlen; j += 2) { 742 time_t seconds; 743 int changes; 744 745 seconds = strtoll(v[j],NULL,10); 746 changes = strtoll(v[j+1],NULL,10); 747 appendServerSaveParams(seconds, changes); 748 } 749 sdsfreesplitres(v,vlen); 750 } else if (!strcasecmp(c->argv[2]->ptr,"slave-serve-stale-data")) { 751 int yn = yesnotoi(o->ptr); 752 753 if (yn == -1) goto badfmt; 754 server.repl_serve_stale_data = yn; 755 } else if (!strcasecmp(c->argv[2]->ptr,"slave-read-only")) { 756 int yn = yesnotoi(o->ptr); 757 758 if (yn == -1) goto badfmt; 759 server.repl_slave_ro = yn; 760 } else if (!strcasecmp(c->argv[2]->ptr,"dir")) { 761 if (chdir((char*)o->ptr) == -1) { 762 addReplyErrorFormat(c,"Changing directory: %s", strerror(errno)); 763 return; 764 } 765 } else if (!strcasecmp(c->argv[2]->ptr,"hash-max-ziplist-entries")) { 766 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt; 767 server.hash_max_ziplist_entries = ll; 768 } else if (!strcasecmp(c->argv[2]->ptr,"hash-max-ziplist-value")) { 769 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt; 770 server.hash_max_ziplist_value = ll; 771 } else if (!strcasecmp(c->argv[2]->ptr,"list-max-ziplist-entries")) { 772 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt; 773 server.list_max_ziplist_entries = ll; 774 } else if (!strcasecmp(c->argv[2]->ptr,"list-max-ziplist-value")) { 775 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt; 776 server.list_max_ziplist_value = ll; 777 } else if (!strcasecmp(c->argv[2]->ptr,"set-max-intset-entries")) { 778 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt; 779 server.set_max_intset_entries = ll; 780 } else if (!strcasecmp(c->argv[2]->ptr,"zset-max-ziplist-entries")) { 781 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt; 782 server.zset_max_ziplist_entries = ll; 783 } else if (!strcasecmp(c->argv[2]->ptr,"zset-max-ziplist-value")) { 784 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt; 785 server.zset_max_ziplist_value = ll; 786 } else if (!strcasecmp(c->argv[2]->ptr,"hll-sparse-max-bytes")) { 787 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt; 788 server.hll_sparse_max_bytes = ll; 789 } else if (!strcasecmp(c->argv[2]->ptr,"lua-time-limit")) { 790 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt; 791 server.lua_time_limit = ll; 792 } else if (!strcasecmp(c->argv[2]->ptr,"slowlog-log-slower-than")) { 793 if (getLongLongFromObject(o,&ll) == REDIS_ERR) goto badfmt; 794 server.slowlog_log_slower_than = ll; 795 } else if (!strcasecmp(c->argv[2]->ptr,"slowlog-max-len")) { 796 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt; 797 server.slowlog_max_len = (unsigned)ll; 798 } else if (!strcasecmp(c->argv[2]->ptr,"latency-monitor-threshold")) { 799 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt; 800 server.latency_monitor_threshold = ll; 801 } else if (!strcasecmp(c->argv[2]->ptr,"loglevel")) { 802 if (!strcasecmp(o->ptr,"warning")) { 803 server.verbosity = REDIS_WARNING; 804 } else if (!strcasecmp(o->ptr,"notice")) { 805 server.verbosity = REDIS_NOTICE; 806 } else if (!strcasecmp(o->ptr,"verbose")) { 807 server.verbosity = REDIS_VERBOSE; 808 } else if (!strcasecmp(o->ptr,"debug")) { 809 server.verbosity = REDIS_DEBUG; 810 } else { 811 goto badfmt; 812 } 813 } else if (!strcasecmp(c->argv[2]->ptr,"client-output-buffer-limit")) { 814 int vlen, j; 815 sds *v = sdssplitlen(o->ptr,sdslen(o->ptr)," ",1,&vlen); 816 817 /* We need a multiple of 4: <class> <hard> <soft> <soft_seconds> */ 818 if (vlen % 4) { 819 sdsfreesplitres(v,vlen); 820 goto badfmt; 821 } 822 823 /* Sanity check of single arguments, so that we either refuse the 824 * whole configuration string or accept it all, even if a single 825 * error in a single client class is present. */ 826 for (j = 0; j < vlen; j++) { 827 char *eptr; 828 long val; 829 830 if ((j % 4) == 0) { 831 if (getClientTypeByName(v[j]) == -1) { 832 sdsfreesplitres(v,vlen); 833 goto badfmt; 834 } 835 } else { 836 val = strtoll(v[j], &eptr, 10); 837 if (eptr[0] != '\0' || val < 0) { 838 sdsfreesplitres(v,vlen); 839 goto badfmt; 840 } 841 } 842 } 843 /* Finally set the new config */ 844 for (j = 0; j < vlen; j += 4) { 845 int class; 846 unsigned long long hard, soft; 847 int soft_seconds; 848 849 class = getClientTypeByName(v[j]); 850 hard = strtoll(v[j+1],NULL,10); 851 soft = strtoll(v[j+2],NULL,10); 852 soft_seconds = strtoll(v[j+3],NULL,10); 853 854 server.client_obuf_limits[class].hard_limit_bytes = hard; 855 server.client_obuf_limits[class].soft_limit_bytes = soft; 856 server.client_obuf_limits[class].soft_limit_seconds = soft_seconds; 857 } 858 sdsfreesplitres(v,vlen); 859 } else if (!strcasecmp(c->argv[2]->ptr,"stop-writes-on-bgsave-error")) { 860 int yn = yesnotoi(o->ptr); 861 862 if (yn == -1) goto badfmt; 863 server.stop_writes_on_bgsave_err = yn; 864 } else if (!strcasecmp(c->argv[2]->ptr,"repl-ping-slave-period")) { 865 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll <= 0) goto badfmt; 866 server.repl_ping_slave_period = ll; 867 } else if (!strcasecmp(c->argv[2]->ptr,"repl-timeout")) { 868 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll <= 0) goto badfmt; 869 server.repl_timeout = ll; 870 } else if (!strcasecmp(c->argv[2]->ptr,"repl-backlog-size")) { 871 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll <= 0) goto badfmt; 872 resizeReplicationBacklog(ll); 873 } else if (!strcasecmp(c->argv[2]->ptr,"repl-backlog-ttl")) { 874 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt; 875 server.repl_backlog_time_limit = ll; 876 } else if (!strcasecmp(c->argv[2]->ptr,"watchdog-period")) { 877 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt; 878 if (ll) 879 enableWatchdog(ll); 880 else 881 disableWatchdog(); 882 } else if (!strcasecmp(c->argv[2]->ptr,"rdbcompression")) { 883 int yn = yesnotoi(o->ptr); 884 885 if (yn == -1) goto badfmt; 886 server.rdb_compression = yn; 887 } else if (!strcasecmp(c->argv[2]->ptr,"notify-keyspace-events")) { 888 int flags = keyspaceEventsStringToFlags(o->ptr); 889 890 if (flags == -1) goto badfmt; 891 server.notify_keyspace_events = flags; 892 } else if (!strcasecmp(c->argv[2]->ptr,"repl-disable-tcp-nodelay")) { 893 int yn = yesnotoi(o->ptr); 894 895 if (yn == -1) goto badfmt; 896 server.repl_disable_tcp_nodelay = yn; 897 } else if (!strcasecmp(c->argv[2]->ptr,"slave-priority")) { 898 if (getLongLongFromObject(o,&ll) == REDIS_ERR || 899 ll < 0) goto badfmt; 900 server.slave_priority = ll; 901 } else if (!strcasecmp(c->argv[2]->ptr,"min-slaves-to-write")) { 902 if (getLongLongFromObject(o,&ll) == REDIS_ERR || 903 ll < 0) goto badfmt; 904 server.repl_min_slaves_to_write = ll; 905 refreshGoodSlavesCount(); 906 } else if (!strcasecmp(c->argv[2]->ptr,"min-slaves-max-lag")) { 907 if (getLongLongFromObject(o,&ll) == REDIS_ERR || 908 ll < 0) goto badfmt; 909 server.repl_min_slaves_max_lag = ll; 910 refreshGoodSlavesCount(); 911 } else if (!strcasecmp(c->argv[2]->ptr,"cluster-node-timeout")) { 912 if (getLongLongFromObject(o,&ll) == REDIS_ERR || 913 ll <= 0) goto badfmt; 914 server.cluster_node_timeout = ll; 915 } else if (!strcasecmp(c->argv[2]->ptr,"cluster-migration-barrier")) { 916 if (getLongLongFromObject(o,&ll) == REDIS_ERR || 917 ll < 0) goto badfmt; 918 server.cluster_migration_barrier = ll; 919 } else if (!strcasecmp(c->argv[2]->ptr,"cluster-slave-validity-factor")) { 920 if (getLongLongFromObject(o,&ll) == REDIS_ERR || 921 ll < 0) goto badfmt; 922 server.cluster_slave_validity_factor = ll; 923 } else { 924 addReplyErrorFormat(c,"Unsupported CONFIG parameter: %s", 925 (char*)c->argv[2]->ptr); 926 return; 927 } 928 addReply(c,shared.ok); 929 return; 930 931 badfmt: /* Bad format errors */ 932 addReplyErrorFormat(c,"Invalid argument '%s' for CONFIG SET '%s'", 933 (char*)o->ptr, 934 (char*)c->argv[2]->ptr); 935 } 936 937 /*----------------------------------------------------------------------------- 938 * CONFIG GET implementation 939 *----------------------------------------------------------------------------*/ 940 941 #define config_get_string_field(_name,_var) do { \ 942 if (stringmatch(pattern,_name,0)) { \ 943 addReplyBulkCString(c,_name); \ 944 addReplyBulkCString(c,_var ? _var : ""); \ 945 matches++; \ 946 } \ 947 } while(0); 948 949 #define config_get_bool_field(_name,_var) do { \ 950 if (stringmatch(pattern,_name,0)) { \ 951 addReplyBulkCString(c,_name); \ 952 addReplyBulkCString(c,_var ? "yes" : "no"); \ 953 matches++; \ 954 } \ 955 } while(0); 956 957 #define config_get_numerical_field(_name,_var) do { \ 958 if (stringmatch(pattern,_name,0)) { \ 959 ll2string(buf,sizeof(buf),_var); \ 960 addReplyBulkCString(c,_name); \ 961 addReplyBulkCString(c,buf); \ 962 matches++; \ 963 } \ 964 } while(0); 965 966 void configGetCommand(redisClient *c) { 967 robj *o = c->argv[2]; 968 void *replylen = addDeferredMultiBulkLength(c); 969 char *pattern = o->ptr; 970 char buf[128]; 971 int matches = 0; 972 redisAssertWithInfo(c,o,sdsEncodedObject(o)); 973 974 /* String values */ 975 config_get_string_field("dbfilename",server.rdb_filename); 976 config_get_string_field("requirepass",server.requirepass); 977 config_get_string_field("masterauth",server.masterauth); 978 config_get_string_field("unixsocket",server.unixsocket); 979 config_get_string_field("logfile",server.logfile); 980 config_get_string_field("pidfile",server.pidfile); 981 982 /* Numerical values */ 983 config_get_numerical_field("maxmemory",server.maxmemory); 984 config_get_numerical_field("maxmemory-samples",server.maxmemory_samples); 985 config_get_numerical_field("timeout",server.maxidletime); 986 config_get_numerical_field("tcp-keepalive",server.tcpkeepalive); 987 config_get_numerical_field("auto-aof-rewrite-percentage", 988 server.aof_rewrite_perc); 989 config_get_numerical_field("auto-aof-rewrite-min-size", 990 server.aof_rewrite_min_size); 991 config_get_numerical_field("hash-max-ziplist-entries", 992 server.hash_max_ziplist_entries); 993 config_get_numerical_field("hash-max-ziplist-value", 994 server.hash_max_ziplist_value); 995 config_get_numerical_field("list-max-ziplist-entries", 996 server.list_max_ziplist_entries); 997 config_get_numerical_field("list-max-ziplist-value", 998 server.list_max_ziplist_value); 999 config_get_numerical_field("set-max-intset-entries", 1000 server.set_max_intset_entries); 1001 config_get_numerical_field("zset-max-ziplist-entries", 1002 server.zset_max_ziplist_entries); 1003 config_get_numerical_field("zset-max-ziplist-value", 1004 server.zset_max_ziplist_value); 1005 config_get_numerical_field("hll-sparse-max-bytes", 1006 server.hll_sparse_max_bytes); 1007 config_get_numerical_field("lua-time-limit",server.lua_time_limit); 1008 config_get_numerical_field("slowlog-log-slower-than", 1009 server.slowlog_log_slower_than); 1010 config_get_numerical_field("latency-monitor-threshold", 1011 server.latency_monitor_threshold); 1012 config_get_numerical_field("slowlog-max-len", 1013 server.slowlog_max_len); 1014 config_get_numerical_field("port",server.port); 1015 config_get_numerical_field("tcp-backlog",server.tcp_backlog); 1016 config_get_numerical_field("databases",server.dbnum); 1017 config_get_numerical_field("repl-ping-slave-period",server.repl_ping_slave_period); 1018 config_get_numerical_field("repl-timeout",server.repl_timeout); 1019 config_get_numerical_field("repl-backlog-size",server.repl_backlog_size); 1020 config_get_numerical_field("repl-backlog-ttl",server.repl_backlog_time_limit); 1021 config_get_numerical_field("maxclients",server.maxclients); 1022 config_get_numerical_field("watchdog-period",server.watchdog_period); 1023 config_get_numerical_field("slave-priority",server.slave_priority); 1024 config_get_numerical_field("min-slaves-to-write",server.repl_min_slaves_to_write); 1025 config_get_numerical_field("min-slaves-max-lag",server.repl_min_slaves_max_lag); 1026 config_get_numerical_field("hz",server.hz); 1027 config_get_numerical_field("cluster-node-timeout",server.cluster_node_timeout); 1028 config_get_numerical_field("cluster-migration-barrier",server.cluster_migration_barrier); 1029 config_get_numerical_field("cluster-slave-validity-factor",server.cluster_slave_validity_factor); 1030 1031 /* Bool (yes/no) values */ 1032 config_get_bool_field("no-appendfsync-on-rewrite", 1033 server.aof_no_fsync_on_rewrite); 1034 config_get_bool_field("slave-serve-stale-data", 1035 server.repl_serve_stale_data); 1036 config_get_bool_field("slave-read-only", 1037 server.repl_slave_ro); 1038 config_get_bool_field("stop-writes-on-bgsave-error", 1039 server.stop_writes_on_bgsave_err); 1040 config_get_bool_field("daemonize", server.daemonize); 1041 config_get_bool_field("rdbcompression", server.rdb_compression); 1042 config_get_bool_field("rdbchecksum", server.rdb_checksum); 1043 config_get_bool_field("activerehashing", server.activerehashing); 1044 config_get_bool_field("repl-disable-tcp-nodelay", 1045 server.repl_disable_tcp_nodelay); 1046 config_get_bool_field("aof-rewrite-incremental-fsync", 1047 server.aof_rewrite_incremental_fsync); 1048 1049 /* Everything we can't handle with macros follows. */ 1050 1051 if (stringmatch(pattern,"appendonly",0)) { 1052 addReplyBulkCString(c,"appendonly"); 1053 addReplyBulkCString(c,server.aof_state == REDIS_AOF_OFF ? "no" : "yes"); 1054 matches++; 1055 } 1056 if (stringmatch(pattern,"dir",0)) { 1057 char buf[1024]; 1058 1059 if (getcwd(buf,sizeof(buf)) == NULL) 1060 buf[0] = '\0'; 1061 1062 addReplyBulkCString(c,"dir"); 1063 addReplyBulkCString(c,buf); 1064 matches++; 1065 } 1066 if (stringmatch(pattern,"maxmemory-policy",0)) { 1067 char *s; 1068 1069 switch(server.maxmemory_policy) { 1070 case REDIS_MAXMEMORY_VOLATILE_LRU: s = "volatile-lru"; break; 1071 case REDIS_MAXMEMORY_VOLATILE_TTL: s = "volatile-ttl"; break; 1072 case REDIS_MAXMEMORY_VOLATILE_RANDOM: s = "volatile-random"; break; 1073 case REDIS_MAXMEMORY_ALLKEYS_LRU: s = "allkeys-lru"; break; 1074 case REDIS_MAXMEMORY_ALLKEYS_RANDOM: s = "allkeys-random"; break; 1075 case REDIS_MAXMEMORY_NO_EVICTION: s = "noeviction"; break; 1076 default: s = "unknown"; break; /* too harmless to panic */ 1077 } 1078 addReplyBulkCString(c,"maxmemory-policy"); 1079 addReplyBulkCString(c,s); 1080 matches++; 1081 } 1082 if (stringmatch(pattern,"appendfsync",0)) { 1083 char *policy; 1084 1085 switch(server.aof_fsync) { 1086 case AOF_FSYNC_NO: policy = "no"; break; 1087 case AOF_FSYNC_EVERYSEC: policy = "everysec"; break; 1088 case AOF_FSYNC_ALWAYS: policy = "always"; break; 1089 default: policy = "unknown"; break; /* too harmless to panic */ 1090 } 1091 addReplyBulkCString(c,"appendfsync"); 1092 addReplyBulkCString(c,policy); 1093 matches++; 1094 } 1095 if (stringmatch(pattern,"save",0)) { 1096 sds buf = sdsempty(); 1097 int j; 1098 1099 for (j = 0; j < server.saveparamslen; j++) { 1100 buf = sdscatprintf(buf,"%jd %d", 1101 (intmax_t)server.saveparams[j].seconds, 1102 server.saveparams[j].changes); 1103 if (j != server.saveparamslen-1) 1104 buf = sdscatlen(buf," ",1); 1105 } 1106 addReplyBulkCString(c,"save"); 1107 addReplyBulkCString(c,buf); 1108 sdsfree(buf); 1109 matches++; 1110 } 1111 if (stringmatch(pattern,"loglevel",0)) { 1112 char *s; 1113 1114 switch(server.verbosity) { 1115 case REDIS_WARNING: s = "warning"; break; 1116 case REDIS_VERBOSE: s = "verbose"; break; 1117 case REDIS_NOTICE: s = "notice"; break; 1118 case REDIS_DEBUG: s = "debug"; break; 1119 default: s = "unknown"; break; /* too harmless to panic */ 1120 } 1121 addReplyBulkCString(c,"loglevel"); 1122 addReplyBulkCString(c,s); 1123 matches++; 1124 } 1125 if (stringmatch(pattern,"client-output-buffer-limit",0)) { 1126 sds buf = sdsempty(); 1127 int j; 1128 1129 for (j = 0; j < REDIS_CLIENT_TYPE_COUNT; j++) { 1130 buf = sdscatprintf(buf,"%s %llu %llu %ld", 1131 getClientTypeName(j), 1132 server.client_obuf_limits[j].hard_limit_bytes, 1133 server.client_obuf_limits[j].soft_limit_bytes, 1134 (long) server.client_obuf_limits[j].soft_limit_seconds); 1135 if (j != REDIS_CLIENT_TYPE_COUNT-1) 1136 buf = sdscatlen(buf," ",1); 1137 } 1138 addReplyBulkCString(c,"client-output-buffer-limit"); 1139 addReplyBulkCString(c,buf); 1140 sdsfree(buf); 1141 matches++; 1142 } 1143 if (stringmatch(pattern,"unixsocketperm",0)) { 1144 char buf[32]; 1145 snprintf(buf,sizeof(buf),"%o",server.unixsocketperm); 1146 addReplyBulkCString(c,"unixsocketperm"); 1147 addReplyBulkCString(c,buf); 1148 matches++; 1149 } 1150 if (stringmatch(pattern,"slaveof",0)) { 1151 char buf[256]; 1152 1153 addReplyBulkCString(c,"slaveof"); 1154 if (server.masterhost) 1155 snprintf(buf,sizeof(buf),"%s %d", 1156 server.masterhost, server.masterport); 1157 else 1158 buf[0] = '\0'; 1159 addReplyBulkCString(c,buf); 1160 matches++; 1161 } 1162 if (stringmatch(pattern,"notify-keyspace-events",0)) { 1163 robj *flagsobj = createObject(REDIS_STRING, 1164 keyspaceEventsFlagsToString(server.notify_keyspace_events)); 1165 1166 addReplyBulkCString(c,"notify-keyspace-events"); 1167 addReplyBulk(c,flagsobj); 1168 decrRefCount(flagsobj); 1169 matches++; 1170 } 1171 if (stringmatch(pattern,"bind",0)) { 1172 sds aux = sdsjoin(server.bindaddr,server.bindaddr_count," "); 1173 1174 addReplyBulkCString(c,"bind"); 1175 addReplyBulkCString(c,aux); 1176 sdsfree(aux); 1177 matches++; 1178 } 1179 setDeferredMultiBulkLength(c,replylen,matches*2); 1180 } 1181 1182 /*----------------------------------------------------------------------------- 1183 * CONFIG REWRITE implementation 1184 *----------------------------------------------------------------------------*/ 1185 1186 #define REDIS_CONFIG_REWRITE_SIGNATURE "# Generated by CONFIG REWRITE" 1187 1188 /* We use the following dictionary type to store where a configuration 1189 * option is mentioned in the old configuration file, so it's 1190 * like "maxmemory" -> list of line numbers (first line is zero). */ 1191 unsigned int dictSdsCaseHash(const void *key); 1192 int dictSdsKeyCaseCompare(void *privdata, const void *key1, const void *key2); 1193 void dictSdsDestructor(void *privdata, void *val); 1194 void dictListDestructor(void *privdata, void *val); 1195 1196 /* Sentinel config rewriting is implemented inside sentinel.c by 1197 * rewriteConfigSentinelOption(). */ 1198 void rewriteConfigSentinelOption(struct rewriteConfigState *state); 1199 1200 dictType optionToLineDictType = { 1201 dictSdsCaseHash, /* hash function */ 1202 NULL, /* key dup */ 1203 NULL, /* val dup */ 1204 dictSdsKeyCaseCompare, /* key compare */ 1205 dictSdsDestructor, /* key destructor */ 1206 dictListDestructor /* val destructor */ 1207 }; 1208 1209 dictType optionSetDictType = { 1210 dictSdsCaseHash, /* hash function */ 1211 NULL, /* key dup */ 1212 NULL, /* val dup */ 1213 dictSdsKeyCaseCompare, /* key compare */ 1214 dictSdsDestructor, /* key destructor */ 1215 NULL /* val destructor */ 1216 }; 1217 1218 /* The config rewrite state. */ 1219 struct rewriteConfigState { 1220 dict *option_to_line; /* Option -> list of config file lines map */ 1221 dict *rewritten; /* Dictionary of already processed options */ 1222 int numlines; /* Number of lines in current config */ 1223 sds *lines; /* Current lines as an array of sds strings */ 1224 int has_tail; /* True if we already added directives that were 1225 not present in the original config file. */ 1226 }; 1227 1228 /* Append the new line to the current configuration state. */ 1229 void rewriteConfigAppendLine(struct rewriteConfigState *state, sds line) { 1230 state->lines = zrealloc(state->lines, sizeof(char*) * (state->numlines+1)); 1231 state->lines[state->numlines++] = line; 1232 } 1233 1234 /* Populate the option -> list of line numbers map. */ 1235 void rewriteConfigAddLineNumberToOption(struct rewriteConfigState *state, sds option, int linenum) { 1236 list *l = dictFetchValue(state->option_to_line,option); 1237 1238 if (l == NULL) { 1239 l = listCreate(); 1240 dictAdd(state->option_to_line,sdsdup(option),l); 1241 } 1242 listAddNodeTail(l,(void*)(long)linenum); 1243 } 1244 1245 /* Add the specified option to the set of processed options. 1246 * This is useful as only unused lines of processed options will be blanked 1247 * in the config file, while options the rewrite process does not understand 1248 * remain untouched. */ 1249 void rewriteConfigMarkAsProcessed(struct rewriteConfigState *state, char *option) { 1250 sds opt = sdsnew(option); 1251 1252 if (dictAdd(state->rewritten,opt,NULL) != DICT_OK) sdsfree(opt); 1253 } 1254 1255 /* Read the old file, split it into lines to populate a newly created 1256 * config rewrite state, and return it to the caller. 1257 * 1258 * If it is impossible to read the old file, NULL is returned. 1259 * If the old file does not exist at all, an empty state is returned. */ 1260 struct rewriteConfigState *rewriteConfigReadOldFile(char *path) { 1261 FILE *fp = fopen(path,"r"); 1262 struct rewriteConfigState *state = zmalloc(sizeof(*state)); 1263 char buf[REDIS_CONFIGLINE_MAX+1]; 1264 int linenum = -1; 1265 1266 if (fp == NULL && errno != ENOENT) return NULL; 1267 1268 state->option_to_line = dictCreate(&optionToLineDictType,NULL); 1269 state->rewritten = dictCreate(&optionSetDictType,NULL); 1270 state->numlines = 0; 1271 state->lines = NULL; 1272 state->has_tail = 0; 1273 if (fp == NULL) return state; 1274 1275 /* Read the old file line by line, populate the state. */ 1276 while(fgets(buf,REDIS_CONFIGLINE_MAX+1,fp) != NULL) { 1277 int argc; 1278 sds *argv; 1279 sds line = sdstrim(sdsnew(buf),"\r\n\t "); 1280 1281 linenum++; /* Zero based, so we init at -1 */ 1282 1283 /* Handle comments and empty lines. */ 1284 if (line[0] == '#' || line[0] == '\0') { 1285 if (!state->has_tail && !strcmp(line,REDIS_CONFIG_REWRITE_SIGNATURE)) 1286 state->has_tail = 1; 1287 rewriteConfigAppendLine(state,line); 1288 continue; 1289 } 1290 1291 /* Not a comment, split into arguments. */ 1292 argv = sdssplitargs(line,&argc); 1293 if (argv == NULL) { 1294 /* Apparently the line is unparsable for some reason, for 1295 * instance it may have unbalanced quotes. Load it as a 1296 * comment. */ 1297 sds aux = sdsnew("# ??? "); 1298 aux = sdscatsds(aux,line); 1299 sdsfree(line); 1300 rewriteConfigAppendLine(state,aux); 1301 continue; 1302 } 1303 1304 sdstolower(argv[0]); /* We only want lowercase config directives. */ 1305 1306 /* Now we populate the state according to the content of this line. 1307 * Append the line and populate the option -> line numbers map. */ 1308 rewriteConfigAppendLine(state,line); 1309 rewriteConfigAddLineNumberToOption(state,argv[0],linenum); 1310 1311 sdsfreesplitres(argv,argc); 1312 } 1313 fclose(fp); 1314 return state; 1315 } 1316 1317 /* Rewrite the specified configuration option with the new "line". 1318 * It progressively uses lines of the file that were already used for the same 1319 * configuration option in the old version of the file, removing that line from 1320 * the map of options -> line numbers. 1321 * 1322 * If there are lines associated with a given configuration option and 1323 * "force" is non-zero, the line is appended to the configuration file. 1324 * Usually "force" is true when an option has not its default value, so it 1325 * must be rewritten even if not present previously. 1326 * 1327 * The first time a line is appended into a configuration file, a comment 1328 * is added to show that starting from that point the config file was generated 1329 * by CONFIG REWRITE. 1330 * 1331 * "line" is either used, or freed, so the caller does not need to free it 1332 * in any way. */ 1333 void rewriteConfigRewriteLine(struct rewriteConfigState *state, char *option, sds line, int force) { 1334 sds o = sdsnew(option); 1335 list *l = dictFetchValue(state->option_to_line,o); 1336 1337 rewriteConfigMarkAsProcessed(state,option); 1338 1339 if (!l && !force) { 1340 /* Option not used previously, and we are not forced to use it. */ 1341 sdsfree(line); 1342 sdsfree(o); 1343 return; 1344 } 1345 1346 if (l) { 1347 listNode *ln = listFirst(l); 1348 int linenum = (long) ln->value; 1349 1350 /* There are still lines in the old configuration file we can reuse 1351 * for this option. Replace the line with the new one. */ 1352 listDelNode(l,ln); 1353 if (listLength(l) == 0) dictDelete(state->option_to_line,o); 1354 sdsfree(state->lines[linenum]); 1355 state->lines[linenum] = line; 1356 } else { 1357 /* Append a new line. */ 1358 if (!state->has_tail) { 1359 rewriteConfigAppendLine(state, 1360 sdsnew(REDIS_CONFIG_REWRITE_SIGNATURE)); 1361 state->has_tail = 1; 1362 } 1363 rewriteConfigAppendLine(state,line); 1364 } 1365 sdsfree(o); 1366 } 1367 1368 /* Write the long long 'bytes' value as a string in a way that is parsable 1369 * inside redis.conf. If possible uses the GB, MB, KB notation. */ 1370 int rewriteConfigFormatMemory(char *buf, size_t len, long long bytes) { 1371 int gb = 1024*1024*1024; 1372 int mb = 1024*1024; 1373 int kb = 1024; 1374 1375 if (bytes && (bytes % gb) == 0) { 1376 return snprintf(buf,len,"%lldgb",bytes/gb); 1377 } else if (bytes && (bytes % mb) == 0) { 1378 return snprintf(buf,len,"%lldmb",bytes/mb); 1379 } else if (bytes && (bytes % kb) == 0) { 1380 return snprintf(buf,len,"%lldkb",bytes/kb); 1381 } else { 1382 return snprintf(buf,len,"%lld",bytes); 1383 } 1384 } 1385 1386 /* Rewrite a simple "option-name <bytes>" configuration option. */ 1387 void rewriteConfigBytesOption(struct rewriteConfigState *state, char *option, long long value, long long defvalue) { 1388 char buf[64]; 1389 int force = value != defvalue; 1390 sds line; 1391 1392 rewriteConfigFormatMemory(buf,sizeof(buf),value); 1393 line = sdscatprintf(sdsempty(),"%s %s",option,buf); 1394 rewriteConfigRewriteLine(state,option,line,force); 1395 } 1396 1397 /* Rewrite a yes/no option. */ 1398 void rewriteConfigYesNoOption(struct rewriteConfigState *state, char *option, int value, int defvalue) { 1399 int force = value != defvalue; 1400 sds line = sdscatprintf(sdsempty(),"%s %s",option, 1401 value ? "yes" : "no"); 1402 1403 rewriteConfigRewriteLine(state,option,line,force); 1404 } 1405 1406 /* Rewrite a string option. */ 1407 void rewriteConfigStringOption(struct rewriteConfigState *state, char *option, char *value, char *defvalue) { 1408 int force = 1; 1409 sds line; 1410 1411 /* String options set to NULL need to be not present at all in the 1412 * configuration file to be set to NULL again at the next reboot. */ 1413 if (value == NULL) { 1414 rewriteConfigMarkAsProcessed(state,option); 1415 return; 1416 } 1417 1418 /* Compare the strings as sds strings to have a binary safe comparison. */ 1419 if (defvalue && strcmp(value,defvalue) == 0) force = 0; 1420 1421 line = sdsnew(option); 1422 line = sdscatlen(line, " ", 1); 1423 line = sdscatrepr(line, value, strlen(value)); 1424 1425 rewriteConfigRewriteLine(state,option,line,force); 1426 } 1427 1428 /* Rewrite a numerical (long long range) option. */ 1429 void rewriteConfigNumericalOption(struct rewriteConfigState *state, char *option, long long value, long long defvalue) { 1430 int force = value != defvalue; 1431 sds line = sdscatprintf(sdsempty(),"%s %lld",option,value); 1432 1433 rewriteConfigRewriteLine(state,option,line,force); 1434 } 1435 1436 /* Rewrite a octal option. */ 1437 void rewriteConfigOctalOption(struct rewriteConfigState *state, char *option, int value, int defvalue) { 1438 int force = value != defvalue; 1439 sds line = sdscatprintf(sdsempty(),"%s %o",option,value); 1440 1441 rewriteConfigRewriteLine(state,option,line,force); 1442 } 1443 1444 /* Rewrite an enumeration option, after the "value" every enum/value pair 1445 * is specified, terminated by NULL. After NULL the default value is 1446 * specified. See how the function is used for more information. */ 1447 void rewriteConfigEnumOption(struct rewriteConfigState *state, char *option, int value, ...) { 1448 va_list ap; 1449 char *enum_name, *matching_name = NULL; 1450 int enum_val, def_val, force; 1451 sds line; 1452 1453 va_start(ap, value); 1454 while(1) { 1455 enum_name = va_arg(ap,char*); 1456 enum_val = va_arg(ap,int); 1457 if (enum_name == NULL) { 1458 def_val = enum_val; 1459 break; 1460 } 1461 if (value == enum_val) matching_name = enum_name; 1462 } 1463 va_end(ap); 1464 1465 force = value != def_val; 1466 line = sdscatprintf(sdsempty(),"%s %s",option,matching_name); 1467 rewriteConfigRewriteLine(state,option,line,force); 1468 } 1469 1470 /* Rewrite the syslog-fability option. */ 1471 void rewriteConfigSyslogfacilityOption(struct rewriteConfigState *state) { 1472 int value = server.syslog_facility, j; 1473 int force = value != LOG_LOCAL0; 1474 char *name = NULL, *option = "syslog-facility"; 1475 sds line; 1476 1477 for (j = 0; validSyslogFacilities[j].name; j++) { 1478 if (validSyslogFacilities[j].value == value) { 1479 name = (char*) validSyslogFacilities[j].name; 1480 break; 1481 } 1482 } 1483 line = sdscatprintf(sdsempty(),"%s %s",option,name); 1484 rewriteConfigRewriteLine(state,option,line,force); 1485 } 1486 1487 /* Rewrite the save option. */ 1488 void rewriteConfigSaveOption(struct rewriteConfigState *state) { 1489 int j; 1490 sds line; 1491 1492 /* Note that if there are no save parameters at all, all the current 1493 * config line with "save" will be detected as orphaned and deleted, 1494 * resulting into no RDB persistence as expected. */ 1495 for (j = 0; j < server.saveparamslen; j++) { 1496 line = sdscatprintf(sdsempty(),"save %ld %d", 1497 (long) server.saveparams[j].seconds, server.saveparams[j].changes); 1498 rewriteConfigRewriteLine(state,"save",line,1); 1499 } 1500 /* Mark "save" as processed in case server.saveparamslen is zero. */ 1501 rewriteConfigMarkAsProcessed(state,"save"); 1502 } 1503 1504 /* Rewrite the dir option, always using absolute paths.*/ 1505 void rewriteConfigDirOption(struct rewriteConfigState *state) { 1506 char cwd[1024]; 1507 1508 if (getcwd(cwd,sizeof(cwd)) == NULL) { 1509 rewriteConfigMarkAsProcessed(state,"dir"); 1510 return; /* no rewrite on error. */ 1511 } 1512 rewriteConfigStringOption(state,"dir",cwd,NULL); 1513 } 1514 1515 /* Rewrite the slaveof option. */ 1516 void rewriteConfigSlaveofOption(struct rewriteConfigState *state) { 1517 char *option = "slaveof"; 1518 sds line; 1519 1520 /* If this is a master, we want all the slaveof config options 1521 * in the file to be removed. Note that if this is a cluster instance 1522 * we don't want a slaveof directive inside redis.conf. */ 1523 if (server.cluster_enabled || server.masterhost == NULL) { 1524 rewriteConfigMarkAsProcessed(state,"slaveof"); 1525 return; 1526 } 1527 line = sdscatprintf(sdsempty(),"%s %s %d", option, 1528 server.masterhost, server.masterport); 1529 rewriteConfigRewriteLine(state,option,line,1); 1530 } 1531 1532 /* Rewrite the notify-keyspace-events option. */ 1533 void rewriteConfigNotifykeyspaceeventsOption(struct rewriteConfigState *state) { 1534 int force = server.notify_keyspace_events != 0; 1535 char *option = "notify-keyspace-events"; 1536 sds line, flags; 1537 1538 flags = keyspaceEventsFlagsToString(server.notify_keyspace_events); 1539 line = sdsnew(option); 1540 line = sdscatlen(line, " ", 1); 1541 line = sdscatrepr(line, flags, sdslen(flags)); 1542 sdsfree(flags); 1543 rewriteConfigRewriteLine(state,option,line,force); 1544 } 1545 1546 /* Rewrite the client-output-buffer-limit option. */ 1547 void rewriteConfigClientoutputbufferlimitOption(struct rewriteConfigState *state) { 1548 int j; 1549 char *option = "client-output-buffer-limit"; 1550 1551 for (j = 0; j < REDIS_CLIENT_TYPE_COUNT; j++) { 1552 int force = (server.client_obuf_limits[j].hard_limit_bytes != 1553 clientBufferLimitsDefaults[j].hard_limit_bytes) || 1554 (server.client_obuf_limits[j].soft_limit_bytes != 1555 clientBufferLimitsDefaults[j].soft_limit_bytes) || 1556 (server.client_obuf_limits[j].soft_limit_seconds != 1557 clientBufferLimitsDefaults[j].soft_limit_seconds); 1558 sds line; 1559 char hard[64], soft[64]; 1560 1561 rewriteConfigFormatMemory(hard,sizeof(hard), 1562 server.client_obuf_limits[j].hard_limit_bytes); 1563 rewriteConfigFormatMemory(soft,sizeof(soft), 1564 server.client_obuf_limits[j].soft_limit_bytes); 1565 1566 line = sdscatprintf(sdsempty(),"%s %s %s %s %ld", 1567 option, getClientTypeName(j), hard, soft, 1568 (long) server.client_obuf_limits[j].soft_limit_seconds); 1569 rewriteConfigRewriteLine(state,option,line,force); 1570 } 1571 } 1572 1573 /* Rewrite the bind option. */ 1574 void rewriteConfigBindOption(struct rewriteConfigState *state) { 1575 int force = 1; 1576 sds line, addresses; 1577 char *option = "bind"; 1578 1579 /* Nothing to rewrite if we don't have bind addresses. */ 1580 if (server.bindaddr_count == 0) { 1581 rewriteConfigMarkAsProcessed(state,option); 1582 return; 1583 } 1584 1585 /* Rewrite as bind <addr1> <addr2> ... <addrN> */ 1586 addresses = sdsjoin(server.bindaddr,server.bindaddr_count," "); 1587 line = sdsnew(option); 1588 line = sdscatlen(line, " ", 1); 1589 line = sdscatsds(line, addresses); 1590 sdsfree(addresses); 1591 1592 rewriteConfigRewriteLine(state,option,line,force); 1593 } 1594 1595 /* Glue together the configuration lines in the current configuration 1596 * rewrite state into a single string, stripping multiple empty lines. */ 1597 sds rewriteConfigGetContentFromState(struct rewriteConfigState *state) { 1598 sds content = sdsempty(); 1599 int j, was_empty = 0; 1600 1601 for (j = 0; j < state->numlines; j++) { 1602 /* Every cluster of empty lines is turned into a single empty line. */ 1603 if (sdslen(state->lines[j]) == 0) { 1604 if (was_empty) continue; 1605 was_empty = 1; 1606 } else { 1607 was_empty = 0; 1608 } 1609 content = sdscatsds(content,state->lines[j]); 1610 content = sdscatlen(content,"\n",1); 1611 } 1612 return content; 1613 } 1614 1615 /* Free the configuration rewrite state. */ 1616 void rewriteConfigReleaseState(struct rewriteConfigState *state) { 1617 sdsfreesplitres(state->lines,state->numlines); 1618 dictRelease(state->option_to_line); 1619 dictRelease(state->rewritten); 1620 zfree(state); 1621 } 1622 1623 /* At the end of the rewrite process the state contains the remaining 1624 * map between "option name" => "lines in the original config file". 1625 * Lines used by the rewrite process were removed by the function 1626 * rewriteConfigRewriteLine(), all the other lines are "orphaned" and 1627 * should be replaced by empty lines. 1628 * 1629 * This function does just this, iterating all the option names and 1630 * blanking all the lines still associated. */ 1631 void rewriteConfigRemoveOrphaned(struct rewriteConfigState *state) { 1632 dictIterator *di = dictGetIterator(state->option_to_line); 1633 dictEntry *de; 1634 1635 while((de = dictNext(di)) != NULL) { 1636 list *l = dictGetVal(de); 1637 sds option = dictGetKey(de); 1638 1639 /* Don't blank lines about options the rewrite process 1640 * don't understand. */ 1641 if (dictFind(state->rewritten,option) == NULL) { 1642 redisLog(REDIS_DEBUG,"Not rewritten option: %s", option); 1643 continue; 1644 } 1645 1646 while(listLength(l)) { 1647 listNode *ln = listFirst(l); 1648 int linenum = (long) ln->value; 1649 1650 sdsfree(state->lines[linenum]); 1651 state->lines[linenum] = sdsempty(); 1652 listDelNode(l,ln); 1653 } 1654 } 1655 dictReleaseIterator(di); 1656 } 1657 1658 /* This function overwrites the old configuration file with the new content. 1659 * 1660 * 1) The old file length is obtained. 1661 * 2) If the new content is smaller, padding is added. 1662 * 3) A single write(2) call is used to replace the content of the file. 1663 * 4) Later the file is truncated to the length of the new content. 1664 * 1665 * This way we are sure the file is left in a consistent state even if the 1666 * process is stopped between any of the four operations. 1667 * 1668 * The function returns 0 on success, otherwise -1 is returned and errno 1669 * set accordingly. */ 1670 int rewriteConfigOverwriteFile(char *configfile, sds content) { 1671 int retval = 0; 1672 int fd = open(configfile,O_RDWR|O_CREAT,0644); 1673 int content_size = sdslen(content), padding = 0; 1674 struct stat sb; 1675 sds content_padded; 1676 1677 /* 1) Open the old file (or create a new one if it does not 1678 * exist), get the size. */ 1679 if (fd == -1) return -1; /* errno set by open(). */ 1680 if (fstat(fd,&sb) == -1) { 1681 close(fd); 1682 return -1; /* errno set by fstat(). */ 1683 } 1684 1685 /* 2) Pad the content at least match the old file size. */ 1686 content_padded = sdsdup(content); 1687 if (content_size < sb.st_size) { 1688 /* If the old file was bigger, pad the content with 1689 * a newline plus as many "#" chars as required. */ 1690 padding = sb.st_size - content_size; 1691 content_padded = sdsgrowzero(content_padded,sb.st_size); 1692 content_padded[content_size] = '\n'; 1693 memset(content_padded+content_size+1,'#',padding-1); 1694 } 1695 1696 /* 3) Write the new content using a single write(2). */ 1697 if (write(fd,content_padded,strlen(content_padded)) == -1) { 1698 retval = -1; 1699 goto cleanup; 1700 } 1701 1702 /* 4) Truncate the file to the right length if we used padding. */ 1703 if (padding) { 1704 if (ftruncate(fd,content_size) == -1) { 1705 /* Non critical error... */ 1706 } 1707 } 1708 1709 cleanup: 1710 sdsfree(content_padded); 1711 close(fd); 1712 return retval; 1713 } 1714 1715 /* Rewrite the configuration file at "path". 1716 * If the configuration file already exists, we try at best to retain comments 1717 * and overall structure. 1718 * 1719 * Configuration parameters that are at their default value, unless already 1720 * explicitly included in the old configuration file, are not rewritten. 1721 * 1722 * On error -1 is returned and errno is set accordingly, otherwise 0. */ 1723 int rewriteConfig(char *path) { 1724 struct rewriteConfigState *state; 1725 sds newcontent; 1726 int retval; 1727 1728 /* Step 1: read the old config into our rewrite state. */ 1729 if ((state = rewriteConfigReadOldFile(path)) == NULL) return -1; 1730 1731 /* Step 2: rewrite every single option, replacing or appending it inside 1732 * the rewrite state. */ 1733 1734 rewriteConfigYesNoOption(state,"daemonize",server.daemonize,0); 1735 rewriteConfigStringOption(state,"pidfile",server.pidfile,REDIS_DEFAULT_PID_FILE); 1736 rewriteConfigNumericalOption(state,"port",server.port,REDIS_SERVERPORT); 1737 rewriteConfigNumericalOption(state,"tcp-backlog",server.tcp_backlog,REDIS_TCP_BACKLOG); 1738 rewriteConfigBindOption(state); 1739 rewriteConfigStringOption(state,"unixsocket",server.unixsocket,NULL); 1740 rewriteConfigOctalOption(state,"unixsocketperm",server.unixsocketperm,REDIS_DEFAULT_UNIX_SOCKET_PERM); 1741 rewriteConfigNumericalOption(state,"timeout",server.maxidletime,REDIS_MAXIDLETIME); 1742 rewriteConfigNumericalOption(state,"tcp-keepalive",server.tcpkeepalive,REDIS_DEFAULT_TCP_KEEPALIVE); 1743 rewriteConfigEnumOption(state,"loglevel",server.verbosity, 1744 "debug", REDIS_DEBUG, 1745 "verbose", REDIS_VERBOSE, 1746 "notice", REDIS_NOTICE, 1747 "warning", REDIS_WARNING, 1748 NULL, REDIS_DEFAULT_VERBOSITY); 1749 rewriteConfigStringOption(state,"logfile",server.logfile,REDIS_DEFAULT_LOGFILE); 1750 rewriteConfigYesNoOption(state,"syslog-enabled",server.syslog_enabled,REDIS_DEFAULT_SYSLOG_ENABLED); 1751 rewriteConfigStringOption(state,"syslog-ident",server.syslog_ident,REDIS_DEFAULT_SYSLOG_IDENT); 1752 rewriteConfigSyslogfacilityOption(state); 1753 rewriteConfigSaveOption(state); 1754 rewriteConfigNumericalOption(state,"databases",server.dbnum,REDIS_DEFAULT_DBNUM); 1755 rewriteConfigYesNoOption(state,"stop-writes-on-bgsave-error",server.stop_writes_on_bgsave_err,REDIS_DEFAULT_STOP_WRITES_ON_BGSAVE_ERROR); 1756 rewriteConfigYesNoOption(state,"rdbcompression",server.rdb_compression,REDIS_DEFAULT_RDB_COMPRESSION); 1757 rewriteConfigYesNoOption(state,"rdbchecksum",server.rdb_checksum,REDIS_DEFAULT_RDB_CHECKSUM); 1758 rewriteConfigStringOption(state,"dbfilename",server.rdb_filename,REDIS_DEFAULT_RDB_FILENAME); 1759 rewriteConfigDirOption(state); 1760 rewriteConfigSlaveofOption(state); 1761 rewriteConfigStringOption(state,"masterauth",server.masterauth,NULL); 1762 rewriteConfigYesNoOption(state,"slave-serve-stale-data",server.repl_serve_stale_data,REDIS_DEFAULT_SLAVE_SERVE_STALE_DATA); 1763 rewriteConfigYesNoOption(state,"slave-read-only",server.repl_slave_ro,REDIS_DEFAULT_SLAVE_READ_ONLY); 1764 rewriteConfigNumericalOption(state,"repl-ping-slave-period",server.repl_ping_slave_period,REDIS_REPL_PING_SLAVE_PERIOD); 1765 rewriteConfigNumericalOption(state,"repl-timeout",server.repl_timeout,REDIS_REPL_TIMEOUT); 1766 rewriteConfigBytesOption(state,"repl-backlog-size",server.repl_backlog_size,REDIS_DEFAULT_REPL_BACKLOG_SIZE); 1767 rewriteConfigBytesOption(state,"repl-backlog-ttl",server.repl_backlog_time_limit,REDIS_DEFAULT_REPL_BACKLOG_TIME_LIMIT); 1768 rewriteConfigYesNoOption(state,"repl-disable-tcp-nodelay",server.repl_disable_tcp_nodelay,REDIS_DEFAULT_REPL_DISABLE_TCP_NODELAY); 1769 rewriteConfigNumericalOption(state,"slave-priority",server.slave_priority,REDIS_DEFAULT_SLAVE_PRIORITY); 1770 rewriteConfigNumericalOption(state,"min-slaves-to-write",server.repl_min_slaves_to_write,REDIS_DEFAULT_MIN_SLAVES_TO_WRITE); 1771 rewriteConfigNumericalOption(state,"min-slaves-max-lag",server.repl_min_slaves_max_lag,REDIS_DEFAULT_MIN_SLAVES_MAX_LAG); 1772 rewriteConfigStringOption(state,"requirepass",server.requirepass,NULL); 1773 rewriteConfigNumericalOption(state,"maxclients",server.maxclients,REDIS_MAX_CLIENTS); 1774 rewriteConfigBytesOption(state,"maxmemory",server.maxmemory,REDIS_DEFAULT_MAXMEMORY); 1775 rewriteConfigEnumOption(state,"maxmemory-policy",server.maxmemory_policy, 1776 "volatile-lru", REDIS_MAXMEMORY_VOLATILE_LRU, 1777 "allkeys-lru", REDIS_MAXMEMORY_ALLKEYS_LRU, 1778 "volatile-random", REDIS_MAXMEMORY_VOLATILE_RANDOM, 1779 "allkeys-random", REDIS_MAXMEMORY_ALLKEYS_RANDOM, 1780 "volatile-ttl", REDIS_MAXMEMORY_VOLATILE_TTL, 1781 "noeviction", REDIS_MAXMEMORY_NO_EVICTION, 1782 NULL, REDIS_DEFAULT_MAXMEMORY_POLICY); 1783 rewriteConfigNumericalOption(state,"maxmemory-samples",server.maxmemory_samples,REDIS_DEFAULT_MAXMEMORY_SAMPLES); 1784 rewriteConfigYesNoOption(state,"appendonly",server.aof_state != REDIS_AOF_OFF,0); 1785 rewriteConfigStringOption(state,"appendfilename",server.aof_filename,REDIS_DEFAULT_AOF_FILENAME); 1786 rewriteConfigEnumOption(state,"appendfsync",server.aof_fsync, 1787 "everysec", AOF_FSYNC_EVERYSEC, 1788 "always", AOF_FSYNC_ALWAYS, 1789 "no", AOF_FSYNC_NO, 1790 NULL, REDIS_DEFAULT_AOF_FSYNC); 1791 rewriteConfigYesNoOption(state,"no-appendfsync-on-rewrite",server.aof_no_fsync_on_rewrite,REDIS_DEFAULT_AOF_NO_FSYNC_ON_REWRITE); 1792 rewriteConfigNumericalOption(state,"auto-aof-rewrite-percentage",server.aof_rewrite_perc,REDIS_AOF_REWRITE_PERC); 1793 rewriteConfigBytesOption(state,"auto-aof-rewrite-min-size",server.aof_rewrite_min_size,REDIS_AOF_REWRITE_MIN_SIZE); 1794 rewriteConfigNumericalOption(state,"lua-time-limit",server.lua_time_limit,REDIS_LUA_TIME_LIMIT); 1795 rewriteConfigYesNoOption(state,"cluster-enabled",server.cluster_enabled,0); 1796 rewriteConfigStringOption(state,"cluster-config-file",server.cluster_configfile,REDIS_DEFAULT_CLUSTER_CONFIG_FILE); 1797 rewriteConfigNumericalOption(state,"cluster-node-timeout",server.cluster_node_timeout,REDIS_CLUSTER_DEFAULT_NODE_TIMEOUT); 1798 rewriteConfigNumericalOption(state,"cluster-migration-barrier",server.cluster_migration_barrier,REDIS_CLUSTER_DEFAULT_MIGRATION_BARRIER); 1799 rewriteConfigNumericalOption(state,"cluster-slave-validity-factor",server.cluster_slave_validity_factor,REDIS_CLUSTER_DEFAULT_SLAVE_VALIDITY); 1800 rewriteConfigNumericalOption(state,"slowlog-log-slower-than",server.slowlog_log_slower_than,REDIS_SLOWLOG_LOG_SLOWER_THAN); 1801 rewriteConfigNumericalOption(state,"latency-monitor-threshold",server.latency_monitor_threshold,REDIS_DEFAULT_LATENCY_MONITOR_THRESHOLD); 1802 rewriteConfigNumericalOption(state,"slowlog-max-len",server.slowlog_max_len,REDIS_SLOWLOG_MAX_LEN); 1803 rewriteConfigNotifykeyspaceeventsOption(state); 1804 rewriteConfigNumericalOption(state,"hash-max-ziplist-entries",server.hash_max_ziplist_entries,REDIS_HASH_MAX_ZIPLIST_ENTRIES); 1805 rewriteConfigNumericalOption(state,"hash-max-ziplist-value",server.hash_max_ziplist_value,REDIS_HASH_MAX_ZIPLIST_VALUE); 1806 rewriteConfigNumericalOption(state,"list-max-ziplist-entries",server.list_max_ziplist_entries,REDIS_LIST_MAX_ZIPLIST_ENTRIES); 1807 rewriteConfigNumericalOption(state,"list-max-ziplist-value",server.list_max_ziplist_value,REDIS_LIST_MAX_ZIPLIST_VALUE); 1808 rewriteConfigNumericalOption(state,"set-max-intset-entries",server.set_max_intset_entries,REDIS_SET_MAX_INTSET_ENTRIES); 1809 rewriteConfigNumericalOption(state,"zset-max-ziplist-entries",server.zset_max_ziplist_entries,REDIS_ZSET_MAX_ZIPLIST_ENTRIES); 1810 rewriteConfigNumericalOption(state,"zset-max-ziplist-value",server.zset_max_ziplist_value,REDIS_ZSET_MAX_ZIPLIST_VALUE); 1811 rewriteConfigNumericalOption(state,"hll-sparse-max-bytes",server.hll_sparse_max_bytes,REDIS_DEFAULT_HLL_SPARSE_MAX_BYTES); 1812 rewriteConfigYesNoOption(state,"activerehashing",server.activerehashing,REDIS_DEFAULT_ACTIVE_REHASHING); 1813 rewriteConfigClientoutputbufferlimitOption(state); 1814 rewriteConfigNumericalOption(state,"hz",server.hz,REDIS_DEFAULT_HZ); 1815 rewriteConfigYesNoOption(state,"aof-rewrite-incremental-fsync",server.aof_rewrite_incremental_fsync,REDIS_DEFAULT_AOF_REWRITE_INCREMENTAL_FSYNC); 1816 if (server.sentinel_mode) rewriteConfigSentinelOption(state); 1817 1818 /* Step 3: remove all the orphaned lines in the old file, that is, lines 1819 * that were used by a config option and are no longer used, like in case 1820 * of multiple "save" options or duplicated options. */ 1821 rewriteConfigRemoveOrphaned(state); 1822 1823 /* Step 4: generate a new configuration file from the modified state 1824 * and write it into the original file. */ 1825 newcontent = rewriteConfigGetContentFromState(state); 1826 retval = rewriteConfigOverwriteFile(server.configfile,newcontent); 1827 1828 sdsfree(newcontent); 1829 rewriteConfigReleaseState(state); 1830 return retval; 1831 } 1832 1833 /*----------------------------------------------------------------------------- 1834 * CONFIG command entry point 1835 *----------------------------------------------------------------------------*/ 1836 1837 void configCommand(redisClient *c) { 1838 if (!strcasecmp(c->argv[1]->ptr,"set")) { 1839 if (c->argc != 4) goto badarity; 1840 configSetCommand(c); 1841 } else if (!strcasecmp(c->argv[1]->ptr,"get")) { 1842 if (c->argc != 3) goto badarity; 1843 configGetCommand(c); 1844 } else if (!strcasecmp(c->argv[1]->ptr,"resetstat")) { 1845 if (c->argc != 2) goto badarity; 1846 resetServerStats(); 1847 resetCommandTableStats(); 1848 addReply(c,shared.ok); 1849 } else if (!strcasecmp(c->argv[1]->ptr,"rewrite")) { 1850 if (c->argc != 2) goto badarity; 1851 if (server.configfile == NULL) { 1852 addReplyError(c,"The server is running without a config file"); 1853 return; 1854 } 1855 if (rewriteConfig(server.configfile) == -1) { 1856 redisLog(REDIS_WARNING,"CONFIG REWRITE failed: %s", strerror(errno)); 1857 addReplyErrorFormat(c,"Rewriting config file: %s", strerror(errno)); 1858 } else { 1859 redisLog(REDIS_WARNING,"CONFIG REWRITE executed with success."); 1860 addReply(c,shared.ok); 1861 } 1862 } else { 1863 addReplyError(c, 1864 "CONFIG subcommand must be one of GET, SET, RESETSTAT, REWRITE"); 1865 } 1866 return; 1867 1868 badarity: 1869 addReplyErrorFormat(c,"Wrong number of arguments for CONFIG %s", 1870 (char*) c->argv[1]->ptr); 1871 } 1872