xref: /f-stack/app/redis-5.0.5/src/config.c (revision 572c4311)
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 "server.h"
32 #include "cluster.h"
33 
34 #include <fcntl.h>
35 #include <sys/stat.h>
36 
37 /*-----------------------------------------------------------------------------
38  * Config file name-value maps.
39  *----------------------------------------------------------------------------*/
40 
41 typedef struct configEnum {
42     const char *name;
43     const int val;
44 } configEnum;
45 
46 configEnum maxmemory_policy_enum[] = {
47     {"volatile-lru", MAXMEMORY_VOLATILE_LRU},
48     {"volatile-lfu", MAXMEMORY_VOLATILE_LFU},
49     {"volatile-random",MAXMEMORY_VOLATILE_RANDOM},
50     {"volatile-ttl",MAXMEMORY_VOLATILE_TTL},
51     {"allkeys-lru",MAXMEMORY_ALLKEYS_LRU},
52     {"allkeys-lfu",MAXMEMORY_ALLKEYS_LFU},
53     {"allkeys-random",MAXMEMORY_ALLKEYS_RANDOM},
54     {"noeviction",MAXMEMORY_NO_EVICTION},
55     {NULL, 0}
56 };
57 
58 configEnum syslog_facility_enum[] = {
59     {"user",    LOG_USER},
60     {"local0",  LOG_LOCAL0},
61     {"local1",  LOG_LOCAL1},
62     {"local2",  LOG_LOCAL2},
63     {"local3",  LOG_LOCAL3},
64     {"local4",  LOG_LOCAL4},
65     {"local5",  LOG_LOCAL5},
66     {"local6",  LOG_LOCAL6},
67     {"local7",  LOG_LOCAL7},
68     {NULL, 0}
69 };
70 
71 configEnum loglevel_enum[] = {
72     {"debug", LL_DEBUG},
73     {"verbose", LL_VERBOSE},
74     {"notice", LL_NOTICE},
75     {"warning", LL_WARNING},
76     {NULL,0}
77 };
78 
79 configEnum supervised_mode_enum[] = {
80     {"upstart", SUPERVISED_UPSTART},
81     {"systemd", SUPERVISED_SYSTEMD},
82     {"auto", SUPERVISED_AUTODETECT},
83     {"no", SUPERVISED_NONE},
84     {NULL, 0}
85 };
86 
87 configEnum aof_fsync_enum[] = {
88     {"everysec", AOF_FSYNC_EVERYSEC},
89     {"always", AOF_FSYNC_ALWAYS},
90     {"no", AOF_FSYNC_NO},
91     {NULL, 0}
92 };
93 
94 /* Output buffer limits presets. */
95 clientBufferLimitsConfig clientBufferLimitsDefaults[CLIENT_TYPE_OBUF_COUNT] = {
96     {0, 0, 0}, /* normal */
97     {1024*1024*256, 1024*1024*64, 60}, /* slave */
98     {1024*1024*32, 1024*1024*8, 60}  /* pubsub */
99 };
100 
101 /*-----------------------------------------------------------------------------
102  * Enum access functions
103  *----------------------------------------------------------------------------*/
104 
105 /* Get enum value from name. If there is no match INT_MIN is returned. */
configEnumGetValue(configEnum * ce,char * name)106 int configEnumGetValue(configEnum *ce, char *name) {
107     while(ce->name != NULL) {
108         if (!strcasecmp(ce->name,name)) return ce->val;
109         ce++;
110     }
111     return INT_MIN;
112 }
113 
114 /* Get enum name from value. If no match is found NULL is returned. */
configEnumGetName(configEnum * ce,int val)115 const char *configEnumGetName(configEnum *ce, int val) {
116     while(ce->name != NULL) {
117         if (ce->val == val) return ce->name;
118         ce++;
119     }
120     return NULL;
121 }
122 
123 /* Wrapper for configEnumGetName() returning "unknown" instead of NULL if
124  * there is no match. */
configEnumGetNameOrUnknown(configEnum * ce,int val)125 const char *configEnumGetNameOrUnknown(configEnum *ce, int val) {
126     const char *name = configEnumGetName(ce,val);
127     return name ? name : "unknown";
128 }
129 
130 /* Used for INFO generation. */
evictPolicyToString(void)131 const char *evictPolicyToString(void) {
132     return configEnumGetNameOrUnknown(maxmemory_policy_enum,server.maxmemory_policy);
133 }
134 
135 /*-----------------------------------------------------------------------------
136  * Config file parsing
137  *----------------------------------------------------------------------------*/
138 
yesnotoi(char * s)139 int yesnotoi(char *s) {
140     if (!strcasecmp(s,"yes")) return 1;
141     else if (!strcasecmp(s,"no")) return 0;
142     else return -1;
143 }
144 
appendServerSaveParams(time_t seconds,int changes)145 void appendServerSaveParams(time_t seconds, int changes) {
146     server.saveparams = zrealloc(server.saveparams,sizeof(struct saveparam)*(server.saveparamslen+1));
147     server.saveparams[server.saveparamslen].seconds = seconds;
148     server.saveparams[server.saveparamslen].changes = changes;
149     server.saveparamslen++;
150 }
151 
resetServerSaveParams(void)152 void resetServerSaveParams(void) {
153     zfree(server.saveparams);
154     server.saveparams = NULL;
155     server.saveparamslen = 0;
156 }
157 
queueLoadModule(sds path,sds * argv,int argc)158 void queueLoadModule(sds path, sds *argv, int argc) {
159     int i;
160     struct moduleLoadQueueEntry *loadmod;
161 
162     loadmod = zmalloc(sizeof(struct moduleLoadQueueEntry));
163     loadmod->argv = zmalloc(sizeof(robj*)*argc);
164     loadmod->path = sdsnew(path);
165     loadmod->argc = argc;
166     for (i = 0; i < argc; i++) {
167         loadmod->argv[i] = createRawStringObject(argv[i],sdslen(argv[i]));
168     }
169     listAddNodeTail(server.loadmodule_queue,loadmod);
170 }
171 
loadServerConfigFromString(char * config)172 void loadServerConfigFromString(char *config) {
173     char *err = NULL;
174     int linenum = 0, totlines, i;
175     int slaveof_linenum = 0;
176     sds *lines;
177 
178     lines = sdssplitlen(config,strlen(config),"\n",1,&totlines);
179 
180     for (i = 0; i < totlines; i++) {
181         sds *argv;
182         int argc;
183 
184         linenum = i+1;
185         lines[i] = sdstrim(lines[i]," \t\r\n");
186 
187         /* Skip comments and blank lines */
188         if (lines[i][0] == '#' || lines[i][0] == '\0') continue;
189 
190         /* Split into arguments */
191         argv = sdssplitargs(lines[i],&argc);
192         if (argv == NULL) {
193             err = "Unbalanced quotes in configuration line";
194             goto loaderr;
195         }
196 
197         /* Skip this line if the resulting command vector is empty. */
198         if (argc == 0) {
199             sdsfreesplitres(argv,argc);
200             continue;
201         }
202         sdstolower(argv[0]);
203 
204         /* Execute config directives */
205         if (!strcasecmp(argv[0],"timeout") && argc == 2) {
206             server.maxidletime = atoi(argv[1]);
207             if (server.maxidletime < 0) {
208                 err = "Invalid timeout value"; goto loaderr;
209             }
210         } else if (!strcasecmp(argv[0],"tcp-keepalive") && argc == 2) {
211             server.tcpkeepalive = atoi(argv[1]);
212             if (server.tcpkeepalive < 0) {
213                 err = "Invalid tcp-keepalive value"; goto loaderr;
214             }
215         } else if (!strcasecmp(argv[0],"protected-mode") && argc == 2) {
216             if ((server.protected_mode = yesnotoi(argv[1])) == -1) {
217                 err = "argument must be 'yes' or 'no'"; goto loaderr;
218             }
219         } else if (!strcasecmp(argv[0],"port") && argc == 2) {
220             server.port = atoi(argv[1]);
221             if (server.port < 0 || server.port > 65535) {
222                 err = "Invalid port"; goto loaderr;
223             }
224         } else if (!strcasecmp(argv[0],"tcp-backlog") && argc == 2) {
225             server.tcp_backlog = atoi(argv[1]);
226             if (server.tcp_backlog < 0) {
227                 err = "Invalid backlog value"; goto loaderr;
228             }
229         } else if (!strcasecmp(argv[0],"bind") && argc >= 2) {
230             int j, addresses = argc-1;
231 
232             if (addresses > CONFIG_BINDADDR_MAX) {
233                 err = "Too many bind addresses specified"; goto loaderr;
234             }
235             for (j = 0; j < addresses; j++)
236                 server.bindaddr[j] = zstrdup(argv[j+1]);
237             server.bindaddr_count = addresses;
238         } else if (!strcasecmp(argv[0],"unixsocket") && argc == 2) {
239             server.unixsocket = zstrdup(argv[1]);
240         } else if (!strcasecmp(argv[0],"unixsocketperm") && argc == 2) {
241             errno = 0;
242             server.unixsocketperm = (mode_t)strtol(argv[1], NULL, 8);
243             if (errno || server.unixsocketperm > 0777) {
244                 err = "Invalid socket file permissions"; goto loaderr;
245             }
246         } else if (!strcasecmp(argv[0],"save")) {
247             if (argc == 3) {
248                 int seconds = atoi(argv[1]);
249                 int changes = atoi(argv[2]);
250                 if (seconds < 1 || changes < 0) {
251                     err = "Invalid save parameters"; goto loaderr;
252                 }
253                 appendServerSaveParams(seconds,changes);
254             } else if (argc == 2 && !strcasecmp(argv[1],"")) {
255                 resetServerSaveParams();
256             }
257         } else if (!strcasecmp(argv[0],"dir") && argc == 2) {
258             if (chdir(argv[1]) == -1) {
259                 serverLog(LL_WARNING,"Can't chdir to '%s': %s",
260                     argv[1], strerror(errno));
261                 exit(1);
262             }
263         } else if (!strcasecmp(argv[0],"loglevel") && argc == 2) {
264             server.verbosity = configEnumGetValue(loglevel_enum,argv[1]);
265             if (server.verbosity == INT_MIN) {
266                 err = "Invalid log level. "
267                       "Must be one of debug, verbose, notice, warning";
268                 goto loaderr;
269             }
270         } else if (!strcasecmp(argv[0],"logfile") && argc == 2) {
271             FILE *logfp;
272 
273             zfree(server.logfile);
274             server.logfile = zstrdup(argv[1]);
275             if (server.logfile[0] != '\0') {
276                 /* Test if we are able to open the file. The server will not
277                  * be able to abort just for this problem later... */
278                 logfp = fopen(server.logfile,"a");
279                 if (logfp == NULL) {
280                     err = sdscatprintf(sdsempty(),
281                         "Can't open the log file: %s", strerror(errno));
282                     goto loaderr;
283                 }
284                 fclose(logfp);
285             }
286         } else if (!strcasecmp(argv[0],"always-show-logo") && argc == 2) {
287             if ((server.always_show_logo = yesnotoi(argv[1])) == -1) {
288                 err = "argument must be 'yes' or 'no'"; goto loaderr;
289             }
290         } else if (!strcasecmp(argv[0],"syslog-enabled") && argc == 2) {
291             if ((server.syslog_enabled = yesnotoi(argv[1])) == -1) {
292                 err = "argument must be 'yes' or 'no'"; goto loaderr;
293             }
294         } else if (!strcasecmp(argv[0],"syslog-ident") && argc == 2) {
295             if (server.syslog_ident) zfree(server.syslog_ident);
296             server.syslog_ident = zstrdup(argv[1]);
297         } else if (!strcasecmp(argv[0],"syslog-facility") && argc == 2) {
298             server.syslog_facility =
299                 configEnumGetValue(syslog_facility_enum,argv[1]);
300             if (server.syslog_facility == INT_MIN) {
301                 err = "Invalid log facility. Must be one of USER or between LOCAL0-LOCAL7";
302                 goto loaderr;
303             }
304         } else if (!strcasecmp(argv[0],"databases") && argc == 2) {
305             server.dbnum = atoi(argv[1]);
306             if (server.dbnum < 1) {
307                 err = "Invalid number of databases"; goto loaderr;
308             }
309         } else if (!strcasecmp(argv[0],"include") && argc == 2) {
310             loadServerConfig(argv[1],NULL);
311         } else if (!strcasecmp(argv[0],"maxclients") && argc == 2) {
312             server.maxclients = atoi(argv[1]);
313             if (server.maxclients < 1) {
314                 err = "Invalid max clients limit"; goto loaderr;
315             }
316         } else if (!strcasecmp(argv[0],"maxmemory") && argc == 2) {
317             server.maxmemory = memtoll(argv[1],NULL);
318         } else if (!strcasecmp(argv[0],"maxmemory-policy") && argc == 2) {
319             server.maxmemory_policy =
320                 configEnumGetValue(maxmemory_policy_enum,argv[1]);
321             if (server.maxmemory_policy == INT_MIN) {
322                 err = "Invalid maxmemory policy";
323                 goto loaderr;
324             }
325         } else if (!strcasecmp(argv[0],"maxmemory-samples") && argc == 2) {
326             server.maxmemory_samples = atoi(argv[1]);
327             if (server.maxmemory_samples <= 0) {
328                 err = "maxmemory-samples must be 1 or greater";
329                 goto loaderr;
330             }
331         } else if ((!strcasecmp(argv[0],"proto-max-bulk-len")) && argc == 2) {
332             server.proto_max_bulk_len = memtoll(argv[1],NULL);
333         } else if ((!strcasecmp(argv[0],"client-query-buffer-limit")) && argc == 2) {
334             server.client_max_querybuf_len = memtoll(argv[1],NULL);
335         } else if (!strcasecmp(argv[0],"lfu-log-factor") && argc == 2) {
336             server.lfu_log_factor = atoi(argv[1]);
337             if (server.lfu_log_factor < 0) {
338                 err = "lfu-log-factor must be 0 or greater";
339                 goto loaderr;
340             }
341         } else if (!strcasecmp(argv[0],"lfu-decay-time") && argc == 2) {
342             server.lfu_decay_time = atoi(argv[1]);
343             if (server.lfu_decay_time < 0) {
344                 err = "lfu-decay-time must be 0 or greater";
345                 goto loaderr;
346             }
347         } else if ((!strcasecmp(argv[0],"slaveof") ||
348                     !strcasecmp(argv[0],"replicaof")) && argc == 3) {
349             slaveof_linenum = linenum;
350             server.masterhost = sdsnew(argv[1]);
351             server.masterport = atoi(argv[2]);
352             server.repl_state = REPL_STATE_CONNECT;
353         } else if ((!strcasecmp(argv[0],"repl-ping-slave-period") ||
354                     !strcasecmp(argv[0],"repl-ping-replica-period")) &&
355                     argc == 2)
356         {
357             server.repl_ping_slave_period = atoi(argv[1]);
358             if (server.repl_ping_slave_period <= 0) {
359                 err = "repl-ping-replica-period must be 1 or greater";
360                 goto loaderr;
361             }
362         } else if (!strcasecmp(argv[0],"repl-timeout") && argc == 2) {
363             server.repl_timeout = atoi(argv[1]);
364             if (server.repl_timeout <= 0) {
365                 err = "repl-timeout must be 1 or greater";
366                 goto loaderr;
367             }
368         } else if (!strcasecmp(argv[0],"repl-disable-tcp-nodelay") && argc==2) {
369             if ((server.repl_disable_tcp_nodelay = yesnotoi(argv[1])) == -1) {
370                 err = "argument must be 'yes' or 'no'"; goto loaderr;
371             }
372         } else if (!strcasecmp(argv[0],"repl-diskless-sync") && argc==2) {
373             if ((server.repl_diskless_sync = yesnotoi(argv[1])) == -1) {
374                 err = "argument must be 'yes' or 'no'"; goto loaderr;
375             }
376         } else if (!strcasecmp(argv[0],"repl-diskless-sync-delay") && argc==2) {
377             server.repl_diskless_sync_delay = atoi(argv[1]);
378             if (server.repl_diskless_sync_delay < 0) {
379                 err = "repl-diskless-sync-delay can't be negative";
380                 goto loaderr;
381             }
382         } else if (!strcasecmp(argv[0],"repl-backlog-size") && argc == 2) {
383             long long size = memtoll(argv[1],NULL);
384             if (size <= 0) {
385                 err = "repl-backlog-size must be 1 or greater.";
386                 goto loaderr;
387             }
388             resizeReplicationBacklog(size);
389         } else if (!strcasecmp(argv[0],"repl-backlog-ttl") && argc == 2) {
390             server.repl_backlog_time_limit = atoi(argv[1]);
391             if (server.repl_backlog_time_limit < 0) {
392                 err = "repl-backlog-ttl can't be negative ";
393                 goto loaderr;
394             }
395         } else if (!strcasecmp(argv[0],"masterauth") && argc == 2) {
396             zfree(server.masterauth);
397             server.masterauth = argv[1][0] ? zstrdup(argv[1]) : NULL;
398         } else if ((!strcasecmp(argv[0],"slave-serve-stale-data") ||
399                     !strcasecmp(argv[0],"replica-serve-stale-data"))
400                     && argc == 2)
401         {
402             if ((server.repl_serve_stale_data = yesnotoi(argv[1])) == -1) {
403                 err = "argument must be 'yes' or 'no'"; goto loaderr;
404             }
405         } else if ((!strcasecmp(argv[0],"slave-read-only") ||
406                     !strcasecmp(argv[0],"replica-read-only"))
407                     && argc == 2)
408         {
409             if ((server.repl_slave_ro = yesnotoi(argv[1])) == -1) {
410                 err = "argument must be 'yes' or 'no'"; goto loaderr;
411             }
412         } else if ((!strcasecmp(argv[0],"slave-ignore-maxmemory") ||
413                     !strcasecmp(argv[0],"replica-ignore-maxmemory"))
414                     && argc == 2)
415         {
416             if ((server.repl_slave_ignore_maxmemory = yesnotoi(argv[1])) == -1) {
417                 err = "argument must be 'yes' or 'no'"; goto loaderr;
418             }
419         } else if (!strcasecmp(argv[0],"rdbcompression") && argc == 2) {
420             if ((server.rdb_compression = yesnotoi(argv[1])) == -1) {
421                 err = "argument must be 'yes' or 'no'"; goto loaderr;
422             }
423         } else if (!strcasecmp(argv[0],"rdbchecksum") && argc == 2) {
424             if ((server.rdb_checksum = yesnotoi(argv[1])) == -1) {
425                 err = "argument must be 'yes' or 'no'"; goto loaderr;
426             }
427         } else if (!strcasecmp(argv[0],"activerehashing") && argc == 2) {
428             if ((server.activerehashing = yesnotoi(argv[1])) == -1) {
429                 err = "argument must be 'yes' or 'no'"; goto loaderr;
430             }
431         } else if (!strcasecmp(argv[0],"lazyfree-lazy-eviction") && argc == 2) {
432             if ((server.lazyfree_lazy_eviction = yesnotoi(argv[1])) == -1) {
433                 err = "argument must be 'yes' or 'no'"; goto loaderr;
434             }
435         } else if (!strcasecmp(argv[0],"lazyfree-lazy-expire") && argc == 2) {
436             if ((server.lazyfree_lazy_expire = yesnotoi(argv[1])) == -1) {
437                 err = "argument must be 'yes' or 'no'"; goto loaderr;
438             }
439         } else if (!strcasecmp(argv[0],"lazyfree-lazy-server-del") && argc == 2){
440             if ((server.lazyfree_lazy_server_del = yesnotoi(argv[1])) == -1) {
441                 err = "argument must be 'yes' or 'no'"; goto loaderr;
442             }
443         } else if ((!strcasecmp(argv[0],"slave-lazy-flush") ||
444                     !strcasecmp(argv[0],"replica-lazy-flush")) && argc == 2)
445         {
446             if ((server.repl_slave_lazy_flush = yesnotoi(argv[1])) == -1) {
447                 err = "argument must be 'yes' or 'no'"; goto loaderr;
448             }
449         } else if (!strcasecmp(argv[0],"activedefrag") && argc == 2) {
450             if ((server.active_defrag_enabled = yesnotoi(argv[1])) == -1) {
451                 err = "argument must be 'yes' or 'no'"; goto loaderr;
452             }
453             if (server.active_defrag_enabled) {
454 #ifndef HAVE_DEFRAG
455                 err = "active defrag can't be enabled without proper jemalloc support"; goto loaderr;
456 #endif
457             }
458         } else if (!strcasecmp(argv[0],"daemonize") && argc == 2) {
459             if ((server.daemonize = yesnotoi(argv[1])) == -1) {
460                 err = "argument must be 'yes' or 'no'"; goto loaderr;
461             }
462         } else if (!strcasecmp(argv[0],"dynamic-hz") && argc == 2) {
463             if ((server.dynamic_hz = yesnotoi(argv[1])) == -1) {
464                 err = "argument must be 'yes' or 'no'"; goto loaderr;
465             }
466         } else if (!strcasecmp(argv[0],"hz") && argc == 2) {
467             server.config_hz = atoi(argv[1]);
468             if (server.config_hz < CONFIG_MIN_HZ) server.config_hz = CONFIG_MIN_HZ;
469             if (server.config_hz > CONFIG_MAX_HZ) server.config_hz = CONFIG_MAX_HZ;
470         } else if (!strcasecmp(argv[0],"appendonly") && argc == 2) {
471             int yes;
472 
473             if ((yes = yesnotoi(argv[1])) == -1) {
474                 err = "argument must be 'yes' or 'no'"; goto loaderr;
475             }
476             server.aof_state = yes ? AOF_ON : AOF_OFF;
477         } else if (!strcasecmp(argv[0],"appendfilename") && argc == 2) {
478             if (!pathIsBaseName(argv[1])) {
479                 err = "appendfilename can't be a path, just a filename";
480                 goto loaderr;
481             }
482             zfree(server.aof_filename);
483             server.aof_filename = zstrdup(argv[1]);
484         } else if (!strcasecmp(argv[0],"no-appendfsync-on-rewrite")
485                    && argc == 2) {
486             if ((server.aof_no_fsync_on_rewrite= yesnotoi(argv[1])) == -1) {
487                 err = "argument must be 'yes' or 'no'"; goto loaderr;
488             }
489         } else if (!strcasecmp(argv[0],"appendfsync") && argc == 2) {
490             server.aof_fsync = configEnumGetValue(aof_fsync_enum,argv[1]);
491             if (server.aof_fsync == INT_MIN) {
492                 err = "argument must be 'no', 'always' or 'everysec'";
493                 goto loaderr;
494             }
495         } else if (!strcasecmp(argv[0],"auto-aof-rewrite-percentage") &&
496                    argc == 2)
497         {
498             server.aof_rewrite_perc = atoi(argv[1]);
499             if (server.aof_rewrite_perc < 0) {
500                 err = "Invalid negative percentage for AOF auto rewrite";
501                 goto loaderr;
502             }
503         } else if (!strcasecmp(argv[0],"auto-aof-rewrite-min-size") &&
504                    argc == 2)
505         {
506             server.aof_rewrite_min_size = memtoll(argv[1],NULL);
507         } else if (!strcasecmp(argv[0],"aof-rewrite-incremental-fsync") &&
508                    argc == 2)
509         {
510             if ((server.aof_rewrite_incremental_fsync =
511                  yesnotoi(argv[1])) == -1) {
512                 err = "argument must be 'yes' or 'no'"; goto loaderr;
513             }
514         } else if (!strcasecmp(argv[0],"rdb-save-incremental-fsync") &&
515                    argc == 2)
516         {
517             if ((server.rdb_save_incremental_fsync =
518                  yesnotoi(argv[1])) == -1) {
519                 err = "argument must be 'yes' or 'no'"; goto loaderr;
520             }
521         } else if (!strcasecmp(argv[0],"aof-load-truncated") && argc == 2) {
522             if ((server.aof_load_truncated = yesnotoi(argv[1])) == -1) {
523                 err = "argument must be 'yes' or 'no'"; goto loaderr;
524             }
525         } else if (!strcasecmp(argv[0],"aof-use-rdb-preamble") && argc == 2) {
526             if ((server.aof_use_rdb_preamble = yesnotoi(argv[1])) == -1) {
527                 err = "argument must be 'yes' or 'no'"; goto loaderr;
528             }
529         } else if (!strcasecmp(argv[0],"requirepass") && argc == 2) {
530             if (strlen(argv[1]) > CONFIG_AUTHPASS_MAX_LEN) {
531                 err = "Password is longer than CONFIG_AUTHPASS_MAX_LEN";
532                 goto loaderr;
533             }
534             server.requirepass = argv[1][0] ? zstrdup(argv[1]) : NULL;
535         } else if (!strcasecmp(argv[0],"pidfile") && argc == 2) {
536             zfree(server.pidfile);
537             server.pidfile = zstrdup(argv[1]);
538         } else if (!strcasecmp(argv[0],"dbfilename") && argc == 2) {
539             if (!pathIsBaseName(argv[1])) {
540                 err = "dbfilename can't be a path, just a filename";
541                 goto loaderr;
542             }
543             zfree(server.rdb_filename);
544             server.rdb_filename = zstrdup(argv[1]);
545         } else if (!strcasecmp(argv[0],"active-defrag-threshold-lower") && argc == 2) {
546             server.active_defrag_threshold_lower = atoi(argv[1]);
547             if (server.active_defrag_threshold_lower < 0 ||
548                 server.active_defrag_threshold_lower > 1000) {
549                 err = "active-defrag-threshold-lower must be between 0 and 1000";
550                 goto loaderr;
551             }
552         } else if (!strcasecmp(argv[0],"active-defrag-threshold-upper") && argc == 2) {
553             server.active_defrag_threshold_upper = atoi(argv[1]);
554             if (server.active_defrag_threshold_upper < 0 ||
555                 server.active_defrag_threshold_upper > 1000) {
556                 err = "active-defrag-threshold-upper must be between 0 and 1000";
557                 goto loaderr;
558             }
559         } else if (!strcasecmp(argv[0],"active-defrag-ignore-bytes") && argc == 2) {
560             server.active_defrag_ignore_bytes = memtoll(argv[1], NULL);
561             if (server.active_defrag_ignore_bytes <= 0) {
562                 err = "active-defrag-ignore-bytes must above 0";
563                 goto loaderr;
564             }
565         } else if (!strcasecmp(argv[0],"active-defrag-cycle-min") && argc == 2) {
566             server.active_defrag_cycle_min = atoi(argv[1]);
567             if (server.active_defrag_cycle_min < 1 || server.active_defrag_cycle_min > 99) {
568                 err = "active-defrag-cycle-min must be between 1 and 99";
569                 goto loaderr;
570             }
571         } else if (!strcasecmp(argv[0],"active-defrag-cycle-max") && argc == 2) {
572             server.active_defrag_cycle_max = atoi(argv[1]);
573             if (server.active_defrag_cycle_max < 1 || server.active_defrag_cycle_max > 99) {
574                 err = "active-defrag-cycle-max must be between 1 and 99";
575                 goto loaderr;
576             }
577         } else if (!strcasecmp(argv[0],"active-defrag-max-scan-fields") && argc == 2) {
578             server.active_defrag_max_scan_fields = strtoll(argv[1],NULL,10);
579             if (server.active_defrag_max_scan_fields < 1) {
580                 err = "active-defrag-max-scan-fields must be positive";
581                 goto loaderr;
582             }
583         } else if (!strcasecmp(argv[0],"hash-max-ziplist-entries") && argc == 2) {
584             server.hash_max_ziplist_entries = memtoll(argv[1], NULL);
585         } else if (!strcasecmp(argv[0],"hash-max-ziplist-value") && argc == 2) {
586             server.hash_max_ziplist_value = memtoll(argv[1], NULL);
587         } else if (!strcasecmp(argv[0],"stream-node-max-bytes") && argc == 2) {
588             server.stream_node_max_bytes = memtoll(argv[1], NULL);
589         } else if (!strcasecmp(argv[0],"stream-node-max-entries") && argc == 2) {
590             server.stream_node_max_entries = atoi(argv[1]);
591         } else if (!strcasecmp(argv[0],"list-max-ziplist-entries") && argc == 2){
592             /* DEAD OPTION */
593         } else if (!strcasecmp(argv[0],"list-max-ziplist-value") && argc == 2) {
594             /* DEAD OPTION */
595         } else if (!strcasecmp(argv[0],"list-max-ziplist-size") && argc == 2) {
596             server.list_max_ziplist_size = atoi(argv[1]);
597         } else if (!strcasecmp(argv[0],"list-compress-depth") && argc == 2) {
598             server.list_compress_depth = atoi(argv[1]);
599         } else if (!strcasecmp(argv[0],"set-max-intset-entries") && argc == 2) {
600             server.set_max_intset_entries = memtoll(argv[1], NULL);
601         } else if (!strcasecmp(argv[0],"zset-max-ziplist-entries") && argc == 2) {
602             server.zset_max_ziplist_entries = memtoll(argv[1], NULL);
603         } else if (!strcasecmp(argv[0],"zset-max-ziplist-value") && argc == 2) {
604             server.zset_max_ziplist_value = memtoll(argv[1], NULL);
605         } else if (!strcasecmp(argv[0],"hll-sparse-max-bytes") && argc == 2) {
606             server.hll_sparse_max_bytes = memtoll(argv[1], NULL);
607         } else if (!strcasecmp(argv[0],"rename-command") && argc == 3) {
608             struct redisCommand *cmd = lookupCommand(argv[1]);
609             int retval;
610 
611             if (!cmd) {
612                 err = "No such command in rename-command";
613                 goto loaderr;
614             }
615 
616             /* If the target command name is the empty string we just
617              * remove it from the command table. */
618             retval = dictDelete(server.commands, argv[1]);
619             serverAssert(retval == DICT_OK);
620 
621             /* Otherwise we re-add the command under a different name. */
622             if (sdslen(argv[2]) != 0) {
623                 sds copy = sdsdup(argv[2]);
624 
625                 retval = dictAdd(server.commands, copy, cmd);
626                 if (retval != DICT_OK) {
627                     sdsfree(copy);
628                     err = "Target command name already exists"; goto loaderr;
629                 }
630             }
631         } else if (!strcasecmp(argv[0],"cluster-enabled") && argc == 2) {
632             if ((server.cluster_enabled = yesnotoi(argv[1])) == -1) {
633                 err = "argument must be 'yes' or 'no'"; goto loaderr;
634             }
635         } else if (!strcasecmp(argv[0],"cluster-config-file") && argc == 2) {
636             zfree(server.cluster_configfile);
637             server.cluster_configfile = zstrdup(argv[1]);
638         } else if (!strcasecmp(argv[0],"cluster-announce-ip") && argc == 2) {
639             zfree(server.cluster_announce_ip);
640             server.cluster_announce_ip = zstrdup(argv[1]);
641         } else if (!strcasecmp(argv[0],"cluster-announce-port") && argc == 2) {
642             server.cluster_announce_port = atoi(argv[1]);
643             if (server.cluster_announce_port < 0 ||
644                 server.cluster_announce_port > 65535)
645             {
646                 err = "Invalid port"; goto loaderr;
647             }
648         } else if (!strcasecmp(argv[0],"cluster-announce-bus-port") &&
649                    argc == 2)
650         {
651             server.cluster_announce_bus_port = atoi(argv[1]);
652             if (server.cluster_announce_bus_port < 0 ||
653                 server.cluster_announce_bus_port > 65535)
654             {
655                 err = "Invalid port"; goto loaderr;
656             }
657         } else if (!strcasecmp(argv[0],"cluster-require-full-coverage") &&
658                     argc == 2)
659         {
660             if ((server.cluster_require_full_coverage = yesnotoi(argv[1])) == -1)
661             {
662                 err = "argument must be 'yes' or 'no'"; goto loaderr;
663             }
664         } else if (!strcasecmp(argv[0],"cluster-node-timeout") && argc == 2) {
665             server.cluster_node_timeout = strtoll(argv[1],NULL,10);
666             if (server.cluster_node_timeout <= 0) {
667                 err = "cluster node timeout must be 1 or greater"; goto loaderr;
668             }
669         } else if (!strcasecmp(argv[0],"cluster-migration-barrier")
670                    && argc == 2)
671         {
672             server.cluster_migration_barrier = atoi(argv[1]);
673             if (server.cluster_migration_barrier < 0) {
674                 err = "cluster migration barrier must zero or positive";
675                 goto loaderr;
676             }
677         } else if ((!strcasecmp(argv[0],"cluster-slave-validity-factor") ||
678                     !strcasecmp(argv[0],"cluster-replica-validity-factor"))
679                    && argc == 2)
680         {
681             server.cluster_slave_validity_factor = atoi(argv[1]);
682             if (server.cluster_slave_validity_factor < 0) {
683                 err = "cluster replica validity factor must be zero or positive";
684                 goto loaderr;
685             }
686         } else if ((!strcasecmp(argv[0],"cluster-slave-no-failover") ||
687                     !strcasecmp(argv[0],"cluster-replica-no-failover")) &&
688                    argc == 2)
689         {
690             server.cluster_slave_no_failover = yesnotoi(argv[1]);
691             if (server.cluster_slave_no_failover == -1) {
692                 err = "argument must be 'yes' or 'no'";
693                 goto loaderr;
694             }
695         } else if (!strcasecmp(argv[0],"lua-time-limit") && argc == 2) {
696             server.lua_time_limit = strtoll(argv[1],NULL,10);
697         } else if (!strcasecmp(argv[0],"lua-replicate-commands") && argc == 2) {
698             server.lua_always_replicate_commands = yesnotoi(argv[1]);
699         } else if (!strcasecmp(argv[0],"slowlog-log-slower-than") &&
700                    argc == 2)
701         {
702             server.slowlog_log_slower_than = strtoll(argv[1],NULL,10);
703         } else if (!strcasecmp(argv[0],"latency-monitor-threshold") &&
704                    argc == 2)
705         {
706             server.latency_monitor_threshold = strtoll(argv[1],NULL,10);
707             if (server.latency_monitor_threshold < 0) {
708                 err = "The latency threshold can't be negative";
709                 goto loaderr;
710             }
711         } else if (!strcasecmp(argv[0],"slowlog-max-len") && argc == 2) {
712             server.slowlog_max_len = strtoll(argv[1],NULL,10);
713         } else if (!strcasecmp(argv[0],"client-output-buffer-limit") &&
714                    argc == 5)
715         {
716             int class = getClientTypeByName(argv[1]);
717             unsigned long long hard, soft;
718             int soft_seconds;
719 
720             if (class == -1 || class == CLIENT_TYPE_MASTER) {
721                 err = "Unrecognized client limit class: the user specified "
722                 "an invalid one, or 'master' which has no buffer limits.";
723                 goto loaderr;
724             }
725             hard = memtoll(argv[2],NULL);
726             soft = memtoll(argv[3],NULL);
727             soft_seconds = atoi(argv[4]);
728             if (soft_seconds < 0) {
729                 err = "Negative number of seconds in soft limit is invalid";
730                 goto loaderr;
731             }
732             server.client_obuf_limits[class].hard_limit_bytes = hard;
733             server.client_obuf_limits[class].soft_limit_bytes = soft;
734             server.client_obuf_limits[class].soft_limit_seconds = soft_seconds;
735         } else if (!strcasecmp(argv[0],"stop-writes-on-bgsave-error") &&
736                    argc == 2) {
737             if ((server.stop_writes_on_bgsave_err = yesnotoi(argv[1])) == -1) {
738                 err = "argument must be 'yes' or 'no'"; goto loaderr;
739             }
740         } else if ((!strcasecmp(argv[0],"slave-priority") ||
741                     !strcasecmp(argv[0],"replica-priority")) && argc == 2)
742         {
743             server.slave_priority = atoi(argv[1]);
744         } else if ((!strcasecmp(argv[0],"slave-announce-ip") ||
745                     !strcasecmp(argv[0],"replica-announce-ip")) && argc == 2)
746         {
747             zfree(server.slave_announce_ip);
748             server.slave_announce_ip = zstrdup(argv[1]);
749         } else if ((!strcasecmp(argv[0],"slave-announce-port") ||
750                     !strcasecmp(argv[0],"replica-announce-port")) && argc == 2)
751         {
752             server.slave_announce_port = atoi(argv[1]);
753             if (server.slave_announce_port < 0 ||
754                 server.slave_announce_port > 65535)
755             {
756                 err = "Invalid port"; goto loaderr;
757             }
758         } else if ((!strcasecmp(argv[0],"min-slaves-to-write") ||
759                     !strcasecmp(argv[0],"min-replicas-to-write")) && argc == 2)
760         {
761             server.repl_min_slaves_to_write = atoi(argv[1]);
762             if (server.repl_min_slaves_to_write < 0) {
763                 err = "Invalid value for min-replicas-to-write."; goto loaderr;
764             }
765         } else if ((!strcasecmp(argv[0],"min-slaves-max-lag") ||
766                     !strcasecmp(argv[0],"min-replicas-max-lag")) && argc == 2)
767         {
768             server.repl_min_slaves_max_lag = atoi(argv[1]);
769             if (server.repl_min_slaves_max_lag < 0) {
770                 err = "Invalid value for min-replicas-max-lag."; goto loaderr;
771             }
772         } else if (!strcasecmp(argv[0],"notify-keyspace-events") && argc == 2) {
773             int flags = keyspaceEventsStringToFlags(argv[1]);
774 
775             if (flags == -1) {
776                 err = "Invalid event class character. Use 'g$lshzxeA'.";
777                 goto loaderr;
778             }
779             server.notify_keyspace_events = flags;
780         } else if (!strcasecmp(argv[0],"supervised") && argc == 2) {
781             server.supervised_mode =
782                 configEnumGetValue(supervised_mode_enum,argv[1]);
783 
784             if (server.supervised_mode == INT_MIN) {
785                 err = "Invalid option for 'supervised'. "
786                     "Allowed values: 'upstart', 'systemd', 'auto', or 'no'";
787                 goto loaderr;
788             }
789         } else if (!strcasecmp(argv[0],"loadmodule") && argc >= 2) {
790             queueLoadModule(argv[1],&argv[2],argc-2);
791         } else if (!strcasecmp(argv[0],"sentinel")) {
792             /* argc == 1 is handled by main() as we need to enter the sentinel
793              * mode ASAP. */
794             if (argc != 1) {
795                 if (!server.sentinel_mode) {
796                     err = "sentinel directive while not in sentinel mode";
797                     goto loaderr;
798                 }
799                 err = sentinelHandleConfiguration(argv+1,argc-1);
800                 if (err) goto loaderr;
801             }
802         } else {
803             err = "Bad directive or wrong number of arguments"; goto loaderr;
804         }
805         sdsfreesplitres(argv,argc);
806     }
807 
808     /* Sanity checks. */
809     if (server.cluster_enabled && server.masterhost) {
810         linenum = slaveof_linenum;
811         i = linenum-1;
812         err = "replicaof directive not allowed in cluster mode";
813         goto loaderr;
814     }
815 
816     sdsfreesplitres(lines,totlines);
817     return;
818 
819 loaderr:
820     fprintf(stderr, "\n*** FATAL CONFIG FILE ERROR ***\n");
821     fprintf(stderr, "Reading the configuration file, at line %d\n", linenum);
822     fprintf(stderr, ">>> '%s'\n", lines[i]);
823     fprintf(stderr, "%s\n", err);
824     exit(1);
825 }
826 
827 /* Load the server configuration from the specified filename.
828  * The function appends the additional configuration directives stored
829  * in the 'options' string to the config file before loading.
830  *
831  * Both filename and options can be NULL, in such a case are considered
832  * empty. This way loadServerConfig can be used to just load a file or
833  * just load a string. */
loadServerConfig(char * filename,char * options)834 void loadServerConfig(char *filename, char *options) {
835     sds config = sdsempty();
836     char buf[CONFIG_MAX_LINE+1];
837 
838     /* Load the file content */
839     if (filename) {
840         FILE *fp;
841 
842         if (filename[0] == '-' && filename[1] == '\0') {
843             fp = stdin;
844         } else {
845             if ((fp = fopen(filename,"r")) == NULL) {
846                 serverLog(LL_WARNING,
847                     "Fatal error, can't open config file '%s'", filename);
848                 exit(1);
849             }
850         }
851         while(fgets(buf,CONFIG_MAX_LINE+1,fp) != NULL)
852             config = sdscat(config,buf);
853         if (fp != stdin) fclose(fp);
854     }
855     /* Append the additional options */
856     if (options) {
857         config = sdscat(config,"\n");
858         config = sdscat(config,options);
859     }
860     loadServerConfigFromString(config);
861     sdsfree(config);
862 }
863 
864 /*-----------------------------------------------------------------------------
865  * CONFIG SET implementation
866  *----------------------------------------------------------------------------*/
867 
868 #define config_set_bool_field(_name,_var) \
869     } else if (!strcasecmp(c->argv[2]->ptr,_name)) { \
870         int yn = yesnotoi(o->ptr); \
871         if (yn == -1) goto badfmt; \
872         _var = yn;
873 
874 #define config_set_numerical_field(_name,_var,min,max) \
875     } else if (!strcasecmp(c->argv[2]->ptr,_name)) { \
876         if (getLongLongFromObject(o,&ll) == C_ERR) goto badfmt; \
877         if (min != LLONG_MIN && ll < min) goto badfmt; \
878         if (max != LLONG_MAX && ll > max) goto badfmt; \
879         _var = ll;
880 
881 #define config_set_memory_field(_name,_var) \
882     } else if (!strcasecmp(c->argv[2]->ptr,_name)) { \
883         ll = memtoll(o->ptr,&err); \
884         if (err || ll < 0) goto badfmt; \
885         _var = ll;
886 
887 #define config_set_enum_field(_name,_var,_enumvar) \
888     } else if (!strcasecmp(c->argv[2]->ptr,_name)) { \
889         int enumval = configEnumGetValue(_enumvar,o->ptr); \
890         if (enumval == INT_MIN) goto badfmt; \
891         _var = enumval;
892 
893 #define config_set_special_field(_name) \
894     } else if (!strcasecmp(c->argv[2]->ptr,_name)) {
895 
896 #define config_set_special_field_with_alias(_name1,_name2) \
897     } else if (!strcasecmp(c->argv[2]->ptr,_name1) || \
898                !strcasecmp(c->argv[2]->ptr,_name2)) {
899 
900 #define config_set_else } else
901 
configSetCommand(client * c)902 void configSetCommand(client *c) {
903     robj *o;
904     long long ll;
905     int err;
906     serverAssertWithInfo(c,c->argv[2],sdsEncodedObject(c->argv[2]));
907     serverAssertWithInfo(c,c->argv[3],sdsEncodedObject(c->argv[3]));
908     o = c->argv[3];
909 
910     if (0) { /* this starts the config_set macros else-if chain. */
911 
912     /* Special fields that can't be handled with general macros. */
913     config_set_special_field("dbfilename") {
914         if (!pathIsBaseName(o->ptr)) {
915             addReplyError(c, "dbfilename can't be a path, just a filename");
916             return;
917         }
918         zfree(server.rdb_filename);
919         server.rdb_filename = zstrdup(o->ptr);
920     } config_set_special_field("requirepass") {
921         if (sdslen(o->ptr) > CONFIG_AUTHPASS_MAX_LEN) goto badfmt;
922         zfree(server.requirepass);
923         server.requirepass = ((char*)o->ptr)[0] ? zstrdup(o->ptr) : NULL;
924     } config_set_special_field("masterauth") {
925         zfree(server.masterauth);
926         server.masterauth = ((char*)o->ptr)[0] ? zstrdup(o->ptr) : NULL;
927     } config_set_special_field("cluster-announce-ip") {
928         zfree(server.cluster_announce_ip);
929         server.cluster_announce_ip = ((char*)o->ptr)[0] ? zstrdup(o->ptr) : NULL;
930     } config_set_special_field("maxclients") {
931         int orig_value = server.maxclients;
932 
933         if (getLongLongFromObject(o,&ll) == C_ERR || ll < 1) goto badfmt;
934 
935         /* Try to check if the OS is capable of supporting so many FDs. */
936         server.maxclients = ll;
937         if (ll > orig_value) {
938             adjustOpenFilesLimit();
939             if (server.maxclients != ll) {
940                 addReplyErrorFormat(c,"The operating system is not able to handle the specified number of clients, try with %d", server.maxclients);
941                 server.maxclients = orig_value;
942                 return;
943             }
944             if ((unsigned int) aeGetSetSize(server.el) <
945                 server.maxclients + CONFIG_FDSET_INCR)
946             {
947                 if (aeResizeSetSize(server.el,
948                     server.maxclients + CONFIG_FDSET_INCR) == AE_ERR)
949                 {
950                     addReplyError(c,"The event loop API used by Redis is not able to handle the specified number of clients");
951                     server.maxclients = orig_value;
952                     return;
953                 }
954             }
955         }
956     } config_set_special_field("appendonly") {
957         int enable = yesnotoi(o->ptr);
958 
959         if (enable == -1) goto badfmt;
960         if (enable == 0 && server.aof_state != AOF_OFF) {
961             stopAppendOnly();
962         } else if (enable && server.aof_state == AOF_OFF) {
963             if (startAppendOnly() == C_ERR) {
964                 addReplyError(c,
965                     "Unable to turn on AOF. Check server logs.");
966                 return;
967             }
968         }
969     } config_set_special_field("save") {
970         int vlen, j;
971         sds *v = sdssplitlen(o->ptr,sdslen(o->ptr)," ",1,&vlen);
972 
973         /* Perform sanity check before setting the new config:
974          * - Even number of args
975          * - Seconds >= 1, changes >= 0 */
976         if (vlen & 1) {
977             sdsfreesplitres(v,vlen);
978             goto badfmt;
979         }
980         for (j = 0; j < vlen; j++) {
981             char *eptr;
982             long val;
983 
984             val = strtoll(v[j], &eptr, 10);
985             if (eptr[0] != '\0' ||
986                 ((j & 1) == 0 && val < 1) ||
987                 ((j & 1) == 1 && val < 0)) {
988                 sdsfreesplitres(v,vlen);
989                 goto badfmt;
990             }
991         }
992         /* Finally set the new config */
993         resetServerSaveParams();
994         for (j = 0; j < vlen; j += 2) {
995             time_t seconds;
996             int changes;
997 
998             seconds = strtoll(v[j],NULL,10);
999             changes = strtoll(v[j+1],NULL,10);
1000             appendServerSaveParams(seconds, changes);
1001         }
1002         sdsfreesplitres(v,vlen);
1003     } config_set_special_field("dir") {
1004         if (chdir((char*)o->ptr) == -1) {
1005             addReplyErrorFormat(c,"Changing directory: %s", strerror(errno));
1006             return;
1007         }
1008     } config_set_special_field("client-output-buffer-limit") {
1009         int vlen, j;
1010         sds *v = sdssplitlen(o->ptr,sdslen(o->ptr)," ",1,&vlen);
1011 
1012         /* We need a multiple of 4: <class> <hard> <soft> <soft_seconds> */
1013         if (vlen % 4) {
1014             sdsfreesplitres(v,vlen);
1015             goto badfmt;
1016         }
1017 
1018         /* Sanity check of single arguments, so that we either refuse the
1019          * whole configuration string or accept it all, even if a single
1020          * error in a single client class is present. */
1021         for (j = 0; j < vlen; j++) {
1022             long val;
1023 
1024             if ((j % 4) == 0) {
1025                 int class = getClientTypeByName(v[j]);
1026                 if (class == -1 || class == CLIENT_TYPE_MASTER) {
1027                     sdsfreesplitres(v,vlen);
1028                     goto badfmt;
1029                 }
1030             } else {
1031                 val = memtoll(v[j], &err);
1032                 if (err || val < 0) {
1033                     sdsfreesplitres(v,vlen);
1034                     goto badfmt;
1035                 }
1036             }
1037         }
1038         /* Finally set the new config */
1039         for (j = 0; j < vlen; j += 4) {
1040             int class;
1041             unsigned long long hard, soft;
1042             int soft_seconds;
1043 
1044             class = getClientTypeByName(v[j]);
1045             hard = memtoll(v[j+1],NULL);
1046             soft = memtoll(v[j+2],NULL);
1047             soft_seconds = strtoll(v[j+3],NULL,10);
1048 
1049             server.client_obuf_limits[class].hard_limit_bytes = hard;
1050             server.client_obuf_limits[class].soft_limit_bytes = soft;
1051             server.client_obuf_limits[class].soft_limit_seconds = soft_seconds;
1052         }
1053         sdsfreesplitres(v,vlen);
1054     } config_set_special_field("notify-keyspace-events") {
1055         int flags = keyspaceEventsStringToFlags(o->ptr);
1056 
1057         if (flags == -1) goto badfmt;
1058         server.notify_keyspace_events = flags;
1059     } config_set_special_field_with_alias("slave-announce-ip",
1060                                           "replica-announce-ip")
1061     {
1062         zfree(server.slave_announce_ip);
1063         server.slave_announce_ip = ((char*)o->ptr)[0] ? zstrdup(o->ptr) : NULL;
1064 
1065     /* Boolean fields.
1066      * config_set_bool_field(name,var). */
1067     } config_set_bool_field(
1068       "rdbcompression", server.rdb_compression) {
1069     } config_set_bool_field(
1070       "repl-disable-tcp-nodelay",server.repl_disable_tcp_nodelay) {
1071     } config_set_bool_field(
1072       "repl-diskless-sync",server.repl_diskless_sync) {
1073     } config_set_bool_field(
1074       "cluster-require-full-coverage",server.cluster_require_full_coverage) {
1075     } config_set_bool_field(
1076       "cluster-slave-no-failover",server.cluster_slave_no_failover) {
1077     } config_set_bool_field(
1078       "cluster-replica-no-failover",server.cluster_slave_no_failover) {
1079     } config_set_bool_field(
1080       "aof-rewrite-incremental-fsync",server.aof_rewrite_incremental_fsync) {
1081     } config_set_bool_field(
1082       "rdb-save-incremental-fsync",server.rdb_save_incremental_fsync) {
1083     } config_set_bool_field(
1084       "aof-load-truncated",server.aof_load_truncated) {
1085     } config_set_bool_field(
1086       "aof-use-rdb-preamble",server.aof_use_rdb_preamble) {
1087     } config_set_bool_field(
1088       "slave-serve-stale-data",server.repl_serve_stale_data) {
1089     } config_set_bool_field(
1090       "replica-serve-stale-data",server.repl_serve_stale_data) {
1091     } config_set_bool_field(
1092       "slave-read-only",server.repl_slave_ro) {
1093     } config_set_bool_field(
1094       "replica-read-only",server.repl_slave_ro) {
1095     } config_set_bool_field(
1096       "slave-ignore-maxmemory",server.repl_slave_ignore_maxmemory) {
1097     } config_set_bool_field(
1098       "replica-ignore-maxmemory",server.repl_slave_ignore_maxmemory) {
1099     } config_set_bool_field(
1100       "activerehashing",server.activerehashing) {
1101     } config_set_bool_field(
1102       "activedefrag",server.active_defrag_enabled) {
1103 #ifndef HAVE_DEFRAG
1104         if (server.active_defrag_enabled) {
1105             server.active_defrag_enabled = 0;
1106             addReplyError(c,
1107                 "-DISABLED Active defragmentation cannot be enabled: it "
1108                 "requires a Redis server compiled with a modified Jemalloc "
1109                 "like the one shipped by default with the Redis source "
1110                 "distribution");
1111             return;
1112         }
1113 #endif
1114     } config_set_bool_field(
1115       "protected-mode",server.protected_mode) {
1116     } config_set_bool_field(
1117       "stop-writes-on-bgsave-error",server.stop_writes_on_bgsave_err) {
1118     } config_set_bool_field(
1119       "lazyfree-lazy-eviction",server.lazyfree_lazy_eviction) {
1120     } config_set_bool_field(
1121       "lazyfree-lazy-expire",server.lazyfree_lazy_expire) {
1122     } config_set_bool_field(
1123       "lazyfree-lazy-server-del",server.lazyfree_lazy_server_del) {
1124     } config_set_bool_field(
1125       "slave-lazy-flush",server.repl_slave_lazy_flush) {
1126     } config_set_bool_field(
1127       "replica-lazy-flush",server.repl_slave_lazy_flush) {
1128     } config_set_bool_field(
1129       "no-appendfsync-on-rewrite",server.aof_no_fsync_on_rewrite) {
1130     } config_set_bool_field(
1131       "dynamic-hz",server.dynamic_hz) {
1132 
1133     /* Numerical fields.
1134      * config_set_numerical_field(name,var,min,max) */
1135     } config_set_numerical_field(
1136       "tcp-keepalive",server.tcpkeepalive,0,INT_MAX) {
1137     } config_set_numerical_field(
1138       "maxmemory-samples",server.maxmemory_samples,1,INT_MAX) {
1139     } config_set_numerical_field(
1140       "lfu-log-factor",server.lfu_log_factor,0,INT_MAX) {
1141     } config_set_numerical_field(
1142       "lfu-decay-time",server.lfu_decay_time,0,INT_MAX) {
1143     } config_set_numerical_field(
1144       "timeout",server.maxidletime,0,INT_MAX) {
1145     } config_set_numerical_field(
1146       "active-defrag-threshold-lower",server.active_defrag_threshold_lower,0,1000) {
1147     } config_set_numerical_field(
1148       "active-defrag-threshold-upper",server.active_defrag_threshold_upper,0,1000) {
1149     } config_set_memory_field(
1150       "active-defrag-ignore-bytes",server.active_defrag_ignore_bytes) {
1151     } config_set_numerical_field(
1152       "active-defrag-cycle-min",server.active_defrag_cycle_min,1,99) {
1153     } config_set_numerical_field(
1154       "active-defrag-cycle-max",server.active_defrag_cycle_max,1,99) {
1155     } config_set_numerical_field(
1156       "active-defrag-max-scan-fields",server.active_defrag_max_scan_fields,1,LONG_MAX) {
1157     } config_set_numerical_field(
1158       "auto-aof-rewrite-percentage",server.aof_rewrite_perc,0,INT_MAX){
1159     } config_set_numerical_field(
1160       "hash-max-ziplist-entries",server.hash_max_ziplist_entries,0,LONG_MAX) {
1161     } config_set_numerical_field(
1162       "hash-max-ziplist-value",server.hash_max_ziplist_value,0,LONG_MAX) {
1163     } config_set_numerical_field(
1164       "stream-node-max-bytes",server.stream_node_max_bytes,0,LONG_MAX) {
1165     } config_set_numerical_field(
1166       "stream-node-max-entries",server.stream_node_max_entries,0,LLONG_MAX) {
1167     } config_set_numerical_field(
1168       "list-max-ziplist-size",server.list_max_ziplist_size,INT_MIN,INT_MAX) {
1169     } config_set_numerical_field(
1170       "list-compress-depth",server.list_compress_depth,0,INT_MAX) {
1171     } config_set_numerical_field(
1172       "set-max-intset-entries",server.set_max_intset_entries,0,LONG_MAX) {
1173     } config_set_numerical_field(
1174       "zset-max-ziplist-entries",server.zset_max_ziplist_entries,0,LONG_MAX) {
1175     } config_set_numerical_field(
1176       "zset-max-ziplist-value",server.zset_max_ziplist_value,0,LONG_MAX) {
1177     } config_set_numerical_field(
1178       "hll-sparse-max-bytes",server.hll_sparse_max_bytes,0,LONG_MAX) {
1179     } config_set_numerical_field(
1180       "lua-time-limit",server.lua_time_limit,0,LONG_MAX) {
1181     } config_set_numerical_field(
1182       "slowlog-log-slower-than",server.slowlog_log_slower_than,-1,LLONG_MAX) {
1183     } config_set_numerical_field(
1184       "slowlog-max-len",ll,0,LONG_MAX) {
1185       /* Cast to unsigned. */
1186         server.slowlog_max_len = (unsigned long)ll;
1187     } config_set_numerical_field(
1188       "latency-monitor-threshold",server.latency_monitor_threshold,0,LLONG_MAX){
1189     } config_set_numerical_field(
1190       "repl-ping-slave-period",server.repl_ping_slave_period,1,INT_MAX) {
1191     } config_set_numerical_field(
1192       "repl-ping-replica-period",server.repl_ping_slave_period,1,INT_MAX) {
1193     } config_set_numerical_field(
1194       "repl-timeout",server.repl_timeout,1,INT_MAX) {
1195     } config_set_numerical_field(
1196       "repl-backlog-ttl",server.repl_backlog_time_limit,0,LONG_MAX) {
1197     } config_set_numerical_field(
1198       "repl-diskless-sync-delay",server.repl_diskless_sync_delay,0,INT_MAX) {
1199     } config_set_numerical_field(
1200       "slave-priority",server.slave_priority,0,INT_MAX) {
1201     } config_set_numerical_field(
1202       "replica-priority",server.slave_priority,0,INT_MAX) {
1203     } config_set_numerical_field(
1204       "slave-announce-port",server.slave_announce_port,0,65535) {
1205     } config_set_numerical_field(
1206       "replica-announce-port",server.slave_announce_port,0,65535) {
1207     } config_set_numerical_field(
1208       "min-slaves-to-write",server.repl_min_slaves_to_write,0,INT_MAX) {
1209         refreshGoodSlavesCount();
1210     } config_set_numerical_field(
1211       "min-replicas-to-write",server.repl_min_slaves_to_write,0,INT_MAX) {
1212         refreshGoodSlavesCount();
1213     } config_set_numerical_field(
1214       "min-slaves-max-lag",server.repl_min_slaves_max_lag,0,INT_MAX) {
1215         refreshGoodSlavesCount();
1216     } config_set_numerical_field(
1217       "min-replicas-max-lag",server.repl_min_slaves_max_lag,0,INT_MAX) {
1218         refreshGoodSlavesCount();
1219     } config_set_numerical_field(
1220       "cluster-node-timeout",server.cluster_node_timeout,0,LLONG_MAX) {
1221     } config_set_numerical_field(
1222       "cluster-announce-port",server.cluster_announce_port,0,65535) {
1223     } config_set_numerical_field(
1224       "cluster-announce-bus-port",server.cluster_announce_bus_port,0,65535) {
1225     } config_set_numerical_field(
1226       "cluster-migration-barrier",server.cluster_migration_barrier,0,INT_MAX){
1227     } config_set_numerical_field(
1228       "cluster-slave-validity-factor",server.cluster_slave_validity_factor,0,INT_MAX) {
1229     } config_set_numerical_field(
1230       "cluster-replica-validity-factor",server.cluster_slave_validity_factor,0,INT_MAX) {
1231     } config_set_numerical_field(
1232       "hz",server.config_hz,0,INT_MAX) {
1233         /* Hz is more an hint from the user, so we accept values out of range
1234          * but cap them to reasonable values. */
1235         if (server.config_hz < CONFIG_MIN_HZ) server.config_hz = CONFIG_MIN_HZ;
1236         if (server.config_hz > CONFIG_MAX_HZ) server.config_hz = CONFIG_MAX_HZ;
1237     } config_set_numerical_field(
1238       "watchdog-period",ll,0,INT_MAX) {
1239         if (ll)
1240             enableWatchdog(ll);
1241         else
1242             disableWatchdog();
1243 
1244     /* Memory fields.
1245      * config_set_memory_field(name,var) */
1246     } config_set_memory_field("maxmemory",server.maxmemory) {
1247         if (server.maxmemory) {
1248             if (server.maxmemory < zmalloc_used_memory()) {
1249                 serverLog(LL_WARNING,"WARNING: the new maxmemory value set via CONFIG SET is smaller than the current memory usage. This will result in key eviction and/or the inability to accept new write commands depending on the maxmemory-policy.");
1250             }
1251             freeMemoryIfNeededAndSafe();
1252         }
1253     } config_set_memory_field(
1254       "proto-max-bulk-len",server.proto_max_bulk_len) {
1255     } config_set_memory_field(
1256       "client-query-buffer-limit",server.client_max_querybuf_len) {
1257     } config_set_memory_field("repl-backlog-size",ll) {
1258         resizeReplicationBacklog(ll);
1259     } config_set_memory_field("auto-aof-rewrite-min-size",ll) {
1260         server.aof_rewrite_min_size = ll;
1261 
1262     /* Enumeration fields.
1263      * config_set_enum_field(name,var,enum_var) */
1264     } config_set_enum_field(
1265       "loglevel",server.verbosity,loglevel_enum) {
1266     } config_set_enum_field(
1267       "maxmemory-policy",server.maxmemory_policy,maxmemory_policy_enum) {
1268     } config_set_enum_field(
1269       "appendfsync",server.aof_fsync,aof_fsync_enum) {
1270 
1271     /* Everyhing else is an error... */
1272     } config_set_else {
1273         addReplyErrorFormat(c,"Unsupported CONFIG parameter: %s",
1274             (char*)c->argv[2]->ptr);
1275         return;
1276     }
1277 
1278     /* On success we just return a generic OK for all the options. */
1279     addReply(c,shared.ok);
1280     return;
1281 
1282 badfmt: /* Bad format errors */
1283     addReplyErrorFormat(c,"Invalid argument '%s' for CONFIG SET '%s'",
1284             (char*)o->ptr,
1285             (char*)c->argv[2]->ptr);
1286 }
1287 
1288 /*-----------------------------------------------------------------------------
1289  * CONFIG GET implementation
1290  *----------------------------------------------------------------------------*/
1291 
1292 #define config_get_string_field(_name,_var) do { \
1293     if (stringmatch(pattern,_name,1)) { \
1294         addReplyBulkCString(c,_name); \
1295         addReplyBulkCString(c,_var ? _var : ""); \
1296         matches++; \
1297     } \
1298 } while(0);
1299 
1300 #define config_get_bool_field(_name,_var) do { \
1301     if (stringmatch(pattern,_name,1)) { \
1302         addReplyBulkCString(c,_name); \
1303         addReplyBulkCString(c,_var ? "yes" : "no"); \
1304         matches++; \
1305     } \
1306 } while(0);
1307 
1308 #define config_get_numerical_field(_name,_var) do { \
1309     if (stringmatch(pattern,_name,1)) { \
1310         ll2string(buf,sizeof(buf),_var); \
1311         addReplyBulkCString(c,_name); \
1312         addReplyBulkCString(c,buf); \
1313         matches++; \
1314     } \
1315 } while(0);
1316 
1317 #define config_get_enum_field(_name,_var,_enumvar) do { \
1318     if (stringmatch(pattern,_name,1)) { \
1319         addReplyBulkCString(c,_name); \
1320         addReplyBulkCString(c,configEnumGetNameOrUnknown(_enumvar,_var)); \
1321         matches++; \
1322     } \
1323 } while(0);
1324 
1325 void configGetCommand(client *c) {
1326     robj *o = c->argv[2];
1327     void *replylen = addDeferredMultiBulkLength(c);
1328     char *pattern = o->ptr;
1329     char buf[128];
1330     int matches = 0;
1331     serverAssertWithInfo(c,o,sdsEncodedObject(o));
1332 
1333     /* String values */
1334     config_get_string_field("dbfilename",server.rdb_filename);
1335     config_get_string_field("requirepass",server.requirepass);
1336     config_get_string_field("masterauth",server.masterauth);
1337     config_get_string_field("cluster-announce-ip",server.cluster_announce_ip);
1338     config_get_string_field("unixsocket",server.unixsocket);
1339     config_get_string_field("logfile",server.logfile);
1340     config_get_string_field("pidfile",server.pidfile);
1341     config_get_string_field("slave-announce-ip",server.slave_announce_ip);
1342     config_get_string_field("replica-announce-ip",server.slave_announce_ip);
1343 
1344     /* Numerical values */
1345     config_get_numerical_field("maxmemory",server.maxmemory);
1346     config_get_numerical_field("proto-max-bulk-len",server.proto_max_bulk_len);
1347     config_get_numerical_field("client-query-buffer-limit",server.client_max_querybuf_len);
1348     config_get_numerical_field("maxmemory-samples",server.maxmemory_samples);
1349     config_get_numerical_field("lfu-log-factor",server.lfu_log_factor);
1350     config_get_numerical_field("lfu-decay-time",server.lfu_decay_time);
1351     config_get_numerical_field("timeout",server.maxidletime);
1352     config_get_numerical_field("active-defrag-threshold-lower",server.active_defrag_threshold_lower);
1353     config_get_numerical_field("active-defrag-threshold-upper",server.active_defrag_threshold_upper);
1354     config_get_numerical_field("active-defrag-ignore-bytes",server.active_defrag_ignore_bytes);
1355     config_get_numerical_field("active-defrag-cycle-min",server.active_defrag_cycle_min);
1356     config_get_numerical_field("active-defrag-cycle-max",server.active_defrag_cycle_max);
1357     config_get_numerical_field("active-defrag-max-scan-fields",server.active_defrag_max_scan_fields);
1358     config_get_numerical_field("auto-aof-rewrite-percentage",
1359             server.aof_rewrite_perc);
1360     config_get_numerical_field("auto-aof-rewrite-min-size",
1361             server.aof_rewrite_min_size);
1362     config_get_numerical_field("hash-max-ziplist-entries",
1363             server.hash_max_ziplist_entries);
1364     config_get_numerical_field("hash-max-ziplist-value",
1365             server.hash_max_ziplist_value);
1366     config_get_numerical_field("stream-node-max-bytes",
1367             server.stream_node_max_bytes);
1368     config_get_numerical_field("stream-node-max-entries",
1369             server.stream_node_max_entries);
1370     config_get_numerical_field("list-max-ziplist-size",
1371             server.list_max_ziplist_size);
1372     config_get_numerical_field("list-compress-depth",
1373             server.list_compress_depth);
1374     config_get_numerical_field("set-max-intset-entries",
1375             server.set_max_intset_entries);
1376     config_get_numerical_field("zset-max-ziplist-entries",
1377             server.zset_max_ziplist_entries);
1378     config_get_numerical_field("zset-max-ziplist-value",
1379             server.zset_max_ziplist_value);
1380     config_get_numerical_field("hll-sparse-max-bytes",
1381             server.hll_sparse_max_bytes);
1382     config_get_numerical_field("lua-time-limit",server.lua_time_limit);
1383     config_get_numerical_field("slowlog-log-slower-than",
1384             server.slowlog_log_slower_than);
1385     config_get_numerical_field("latency-monitor-threshold",
1386             server.latency_monitor_threshold);
1387     config_get_numerical_field("slowlog-max-len",
1388             server.slowlog_max_len);
1389     config_get_numerical_field("port",server.port);
1390     config_get_numerical_field("cluster-announce-port",server.cluster_announce_port);
1391     config_get_numerical_field("cluster-announce-bus-port",server.cluster_announce_bus_port);
1392     config_get_numerical_field("tcp-backlog",server.tcp_backlog);
1393     config_get_numerical_field("databases",server.dbnum);
1394     config_get_numerical_field("repl-ping-slave-period",server.repl_ping_slave_period);
1395     config_get_numerical_field("repl-ping-replica-period",server.repl_ping_slave_period);
1396     config_get_numerical_field("repl-timeout",server.repl_timeout);
1397     config_get_numerical_field("repl-backlog-size",server.repl_backlog_size);
1398     config_get_numerical_field("repl-backlog-ttl",server.repl_backlog_time_limit);
1399     config_get_numerical_field("maxclients",server.maxclients);
1400     config_get_numerical_field("watchdog-period",server.watchdog_period);
1401     config_get_numerical_field("slave-priority",server.slave_priority);
1402     config_get_numerical_field("replica-priority",server.slave_priority);
1403     config_get_numerical_field("slave-announce-port",server.slave_announce_port);
1404     config_get_numerical_field("replica-announce-port",server.slave_announce_port);
1405     config_get_numerical_field("min-slaves-to-write",server.repl_min_slaves_to_write);
1406     config_get_numerical_field("min-replicas-to-write",server.repl_min_slaves_to_write);
1407     config_get_numerical_field("min-slaves-max-lag",server.repl_min_slaves_max_lag);
1408     config_get_numerical_field("min-replicas-max-lag",server.repl_min_slaves_max_lag);
1409     config_get_numerical_field("hz",server.config_hz);
1410     config_get_numerical_field("cluster-node-timeout",server.cluster_node_timeout);
1411     config_get_numerical_field("cluster-migration-barrier",server.cluster_migration_barrier);
1412     config_get_numerical_field("cluster-slave-validity-factor",server.cluster_slave_validity_factor);
1413     config_get_numerical_field("cluster-replica-validity-factor",server.cluster_slave_validity_factor);
1414     config_get_numerical_field("repl-diskless-sync-delay",server.repl_diskless_sync_delay);
1415     config_get_numerical_field("tcp-keepalive",server.tcpkeepalive);
1416 
1417     /* Bool (yes/no) values */
1418     config_get_bool_field("cluster-require-full-coverage",
1419             server.cluster_require_full_coverage);
1420     config_get_bool_field("cluster-slave-no-failover",
1421             server.cluster_slave_no_failover);
1422     config_get_bool_field("cluster-replica-no-failover",
1423             server.cluster_slave_no_failover);
1424     config_get_bool_field("no-appendfsync-on-rewrite",
1425             server.aof_no_fsync_on_rewrite);
1426     config_get_bool_field("slave-serve-stale-data",
1427             server.repl_serve_stale_data);
1428     config_get_bool_field("replica-serve-stale-data",
1429             server.repl_serve_stale_data);
1430     config_get_bool_field("slave-read-only",
1431             server.repl_slave_ro);
1432     config_get_bool_field("replica-read-only",
1433             server.repl_slave_ro);
1434     config_get_bool_field("slave-ignore-maxmemory",
1435             server.repl_slave_ignore_maxmemory);
1436     config_get_bool_field("replica-ignore-maxmemory",
1437             server.repl_slave_ignore_maxmemory);
1438     config_get_bool_field("stop-writes-on-bgsave-error",
1439             server.stop_writes_on_bgsave_err);
1440     config_get_bool_field("daemonize", server.daemonize);
1441     config_get_bool_field("rdbcompression", server.rdb_compression);
1442     config_get_bool_field("rdbchecksum", server.rdb_checksum);
1443     config_get_bool_field("activerehashing", server.activerehashing);
1444     config_get_bool_field("activedefrag", server.active_defrag_enabled);
1445     config_get_bool_field("protected-mode", server.protected_mode);
1446     config_get_bool_field("repl-disable-tcp-nodelay",
1447             server.repl_disable_tcp_nodelay);
1448     config_get_bool_field("repl-diskless-sync",
1449             server.repl_diskless_sync);
1450     config_get_bool_field("aof-rewrite-incremental-fsync",
1451             server.aof_rewrite_incremental_fsync);
1452     config_get_bool_field("rdb-save-incremental-fsync",
1453             server.rdb_save_incremental_fsync);
1454     config_get_bool_field("aof-load-truncated",
1455             server.aof_load_truncated);
1456     config_get_bool_field("aof-use-rdb-preamble",
1457             server.aof_use_rdb_preamble);
1458     config_get_bool_field("lazyfree-lazy-eviction",
1459             server.lazyfree_lazy_eviction);
1460     config_get_bool_field("lazyfree-lazy-expire",
1461             server.lazyfree_lazy_expire);
1462     config_get_bool_field("lazyfree-lazy-server-del",
1463             server.lazyfree_lazy_server_del);
1464     config_get_bool_field("slave-lazy-flush",
1465             server.repl_slave_lazy_flush);
1466     config_get_bool_field("replica-lazy-flush",
1467             server.repl_slave_lazy_flush);
1468     config_get_bool_field("dynamic-hz",
1469             server.dynamic_hz);
1470 
1471     /* Enum values */
1472     config_get_enum_field("maxmemory-policy",
1473             server.maxmemory_policy,maxmemory_policy_enum);
1474     config_get_enum_field("loglevel",
1475             server.verbosity,loglevel_enum);
1476     config_get_enum_field("supervised",
1477             server.supervised_mode,supervised_mode_enum);
1478     config_get_enum_field("appendfsync",
1479             server.aof_fsync,aof_fsync_enum);
1480     config_get_enum_field("syslog-facility",
1481             server.syslog_facility,syslog_facility_enum);
1482 
1483     /* Everything we can't handle with macros follows. */
1484 
1485     if (stringmatch(pattern,"appendonly",1)) {
1486         addReplyBulkCString(c,"appendonly");
1487         addReplyBulkCString(c,server.aof_state == AOF_OFF ? "no" : "yes");
1488         matches++;
1489     }
1490     if (stringmatch(pattern,"dir",1)) {
1491         char buf[1024];
1492 
1493         if (getcwd(buf,sizeof(buf)) == NULL)
1494             buf[0] = '\0';
1495 
1496         addReplyBulkCString(c,"dir");
1497         addReplyBulkCString(c,buf);
1498         matches++;
1499     }
1500     if (stringmatch(pattern,"save",1)) {
1501         sds buf = sdsempty();
1502         int j;
1503 
1504         for (j = 0; j < server.saveparamslen; j++) {
1505             buf = sdscatprintf(buf,"%jd %d",
1506                     (intmax_t)server.saveparams[j].seconds,
1507                     server.saveparams[j].changes);
1508             if (j != server.saveparamslen-1)
1509                 buf = sdscatlen(buf," ",1);
1510         }
1511         addReplyBulkCString(c,"save");
1512         addReplyBulkCString(c,buf);
1513         sdsfree(buf);
1514         matches++;
1515     }
1516     if (stringmatch(pattern,"client-output-buffer-limit",1)) {
1517         sds buf = sdsempty();
1518         int j;
1519 
1520         for (j = 0; j < CLIENT_TYPE_OBUF_COUNT; j++) {
1521             buf = sdscatprintf(buf,"%s %llu %llu %ld",
1522                     getClientTypeName(j),
1523                     server.client_obuf_limits[j].hard_limit_bytes,
1524                     server.client_obuf_limits[j].soft_limit_bytes,
1525                     (long) server.client_obuf_limits[j].soft_limit_seconds);
1526             if (j != CLIENT_TYPE_OBUF_COUNT-1)
1527                 buf = sdscatlen(buf," ",1);
1528         }
1529         addReplyBulkCString(c,"client-output-buffer-limit");
1530         addReplyBulkCString(c,buf);
1531         sdsfree(buf);
1532         matches++;
1533     }
1534     if (stringmatch(pattern,"unixsocketperm",1)) {
1535         char buf[32];
1536         snprintf(buf,sizeof(buf),"%o",server.unixsocketperm);
1537         addReplyBulkCString(c,"unixsocketperm");
1538         addReplyBulkCString(c,buf);
1539         matches++;
1540     }
1541     if (stringmatch(pattern,"slaveof",1) ||
1542         stringmatch(pattern,"replicaof",1))
1543     {
1544         char *optname = stringmatch(pattern,"slaveof",1) ?
1545                         "slaveof" : "replicaof";
1546         char buf[256];
1547 
1548         addReplyBulkCString(c,optname);
1549         if (server.masterhost)
1550             snprintf(buf,sizeof(buf),"%s %d",
1551                 server.masterhost, server.masterport);
1552         else
1553             buf[0] = '\0';
1554         addReplyBulkCString(c,buf);
1555         matches++;
1556     }
1557     if (stringmatch(pattern,"notify-keyspace-events",1)) {
1558         robj *flagsobj = createObject(OBJ_STRING,
1559             keyspaceEventsFlagsToString(server.notify_keyspace_events));
1560 
1561         addReplyBulkCString(c,"notify-keyspace-events");
1562         addReplyBulk(c,flagsobj);
1563         decrRefCount(flagsobj);
1564         matches++;
1565     }
1566     if (stringmatch(pattern,"bind",1)) {
1567         sds aux = sdsjoin(server.bindaddr,server.bindaddr_count," ");
1568 
1569         addReplyBulkCString(c,"bind");
1570         addReplyBulkCString(c,aux);
1571         sdsfree(aux);
1572         matches++;
1573     }
1574     setDeferredMultiBulkLength(c,replylen,matches*2);
1575 }
1576 
1577 /*-----------------------------------------------------------------------------
1578  * CONFIG REWRITE implementation
1579  *----------------------------------------------------------------------------*/
1580 
1581 #define REDIS_CONFIG_REWRITE_SIGNATURE "# Generated by CONFIG REWRITE"
1582 
1583 /* We use the following dictionary type to store where a configuration
1584  * option is mentioned in the old configuration file, so it's
1585  * like "maxmemory" -> list of line numbers (first line is zero). */
1586 uint64_t dictSdsCaseHash(const void *key);
1587 int dictSdsKeyCaseCompare(void *privdata, const void *key1, const void *key2);
1588 void dictSdsDestructor(void *privdata, void *val);
1589 void dictListDestructor(void *privdata, void *val);
1590 
1591 /* Sentinel config rewriting is implemented inside sentinel.c by
1592  * rewriteConfigSentinelOption(). */
1593 void rewriteConfigSentinelOption(struct rewriteConfigState *state);
1594 
1595 dictType optionToLineDictType = {
1596     dictSdsCaseHash,            /* hash function */
1597     NULL,                       /* key dup */
1598     NULL,                       /* val dup */
1599     dictSdsKeyCaseCompare,      /* key compare */
1600     dictSdsDestructor,          /* key destructor */
1601     dictListDestructor          /* val destructor */
1602 };
1603 
1604 dictType optionSetDictType = {
1605     dictSdsCaseHash,            /* hash function */
1606     NULL,                       /* key dup */
1607     NULL,                       /* val dup */
1608     dictSdsKeyCaseCompare,      /* key compare */
1609     dictSdsDestructor,          /* key destructor */
1610     NULL                        /* val destructor */
1611 };
1612 
1613 /* The config rewrite state. */
1614 struct rewriteConfigState {
1615     dict *option_to_line; /* Option -> list of config file lines map */
1616     dict *rewritten;      /* Dictionary of already processed options */
1617     int numlines;         /* Number of lines in current config */
1618     sds *lines;           /* Current lines as an array of sds strings */
1619     int has_tail;         /* True if we already added directives that were
1620                              not present in the original config file. */
1621 };
1622 
1623 /* Append the new line to the current configuration state. */
1624 void rewriteConfigAppendLine(struct rewriteConfigState *state, sds line) {
1625     state->lines = zrealloc(state->lines, sizeof(char*) * (state->numlines+1));
1626     state->lines[state->numlines++] = line;
1627 }
1628 
1629 /* Populate the option -> list of line numbers map. */
1630 void rewriteConfigAddLineNumberToOption(struct rewriteConfigState *state, sds option, int linenum) {
1631     list *l = dictFetchValue(state->option_to_line,option);
1632 
1633     if (l == NULL) {
1634         l = listCreate();
1635         dictAdd(state->option_to_line,sdsdup(option),l);
1636     }
1637     listAddNodeTail(l,(void*)(long)linenum);
1638 }
1639 
1640 /* Add the specified option to the set of processed options.
1641  * This is useful as only unused lines of processed options will be blanked
1642  * in the config file, while options the rewrite process does not understand
1643  * remain untouched. */
1644 void rewriteConfigMarkAsProcessed(struct rewriteConfigState *state, const char *option) {
1645     sds opt = sdsnew(option);
1646 
1647     if (dictAdd(state->rewritten,opt,NULL) != DICT_OK) sdsfree(opt);
1648 }
1649 
1650 /* Read the old file, split it into lines to populate a newly created
1651  * config rewrite state, and return it to the caller.
1652  *
1653  * If it is impossible to read the old file, NULL is returned.
1654  * If the old file does not exist at all, an empty state is returned. */
1655 struct rewriteConfigState *rewriteConfigReadOldFile(char *path) {
1656     FILE *fp = fopen(path,"r");
1657     if (fp == NULL && errno != ENOENT) return NULL;
1658 
1659     char buf[CONFIG_MAX_LINE+1];
1660     int linenum = -1;
1661     struct rewriteConfigState *state = zmalloc(sizeof(*state));
1662     state->option_to_line = dictCreate(&optionToLineDictType,NULL);
1663     state->rewritten = dictCreate(&optionSetDictType,NULL);
1664     state->numlines = 0;
1665     state->lines = NULL;
1666     state->has_tail = 0;
1667     if (fp == NULL) return state;
1668 
1669     /* Read the old file line by line, populate the state. */
1670     while(fgets(buf,CONFIG_MAX_LINE+1,fp) != NULL) {
1671         int argc;
1672         sds *argv;
1673         sds line = sdstrim(sdsnew(buf),"\r\n\t ");
1674 
1675         linenum++; /* Zero based, so we init at -1 */
1676 
1677         /* Handle comments and empty lines. */
1678         if (line[0] == '#' || line[0] == '\0') {
1679             if (!state->has_tail && !strcmp(line,REDIS_CONFIG_REWRITE_SIGNATURE))
1680                 state->has_tail = 1;
1681             rewriteConfigAppendLine(state,line);
1682             continue;
1683         }
1684 
1685         /* Not a comment, split into arguments. */
1686         argv = sdssplitargs(line,&argc);
1687         if (argv == NULL) {
1688             /* Apparently the line is unparsable for some reason, for
1689              * instance it may have unbalanced quotes. Load it as a
1690              * comment. */
1691             sds aux = sdsnew("# ??? ");
1692             aux = sdscatsds(aux,line);
1693             sdsfree(line);
1694             rewriteConfigAppendLine(state,aux);
1695             continue;
1696         }
1697 
1698         sdstolower(argv[0]); /* We only want lowercase config directives. */
1699 
1700         /* Now we populate the state according to the content of this line.
1701          * Append the line and populate the option -> line numbers map. */
1702         rewriteConfigAppendLine(state,line);
1703 
1704         /* Translate options using the word "slave" to the corresponding name
1705          * "replica", before adding such option to the config name -> lines
1706          * mapping. */
1707         char *p = strstr(argv[0],"slave");
1708         if (p) {
1709             sds alt = sdsempty();
1710             alt = sdscatlen(alt,argv[0],p-argv[0]);;
1711             alt = sdscatlen(alt,"replica",7);
1712             alt = sdscatlen(alt,p+5,strlen(p+5));
1713             sdsfree(argv[0]);
1714             argv[0] = alt;
1715         }
1716         rewriteConfigAddLineNumberToOption(state,argv[0],linenum);
1717         sdsfreesplitres(argv,argc);
1718     }
1719     fclose(fp);
1720     return state;
1721 }
1722 
1723 /* Rewrite the specified configuration option with the new "line".
1724  * It progressively uses lines of the file that were already used for the same
1725  * configuration option in the old version of the file, removing that line from
1726  * the map of options -> line numbers.
1727  *
1728  * If there are lines associated with a given configuration option and
1729  * "force" is non-zero, the line is appended to the configuration file.
1730  * Usually "force" is true when an option has not its default value, so it
1731  * must be rewritten even if not present previously.
1732  *
1733  * The first time a line is appended into a configuration file, a comment
1734  * is added to show that starting from that point the config file was generated
1735  * by CONFIG REWRITE.
1736  *
1737  * "line" is either used, or freed, so the caller does not need to free it
1738  * in any way. */
1739 void rewriteConfigRewriteLine(struct rewriteConfigState *state, const char *option, sds line, int force) {
1740     sds o = sdsnew(option);
1741     list *l = dictFetchValue(state->option_to_line,o);
1742 
1743     rewriteConfigMarkAsProcessed(state,option);
1744 
1745     if (!l && !force) {
1746         /* Option not used previously, and we are not forced to use it. */
1747         sdsfree(line);
1748         sdsfree(o);
1749         return;
1750     }
1751 
1752     if (l) {
1753         listNode *ln = listFirst(l);
1754         int linenum = (long) ln->value;
1755 
1756         /* There are still lines in the old configuration file we can reuse
1757          * for this option. Replace the line with the new one. */
1758         listDelNode(l,ln);
1759         if (listLength(l) == 0) dictDelete(state->option_to_line,o);
1760         sdsfree(state->lines[linenum]);
1761         state->lines[linenum] = line;
1762     } else {
1763         /* Append a new line. */
1764         if (!state->has_tail) {
1765             rewriteConfigAppendLine(state,
1766                 sdsnew(REDIS_CONFIG_REWRITE_SIGNATURE));
1767             state->has_tail = 1;
1768         }
1769         rewriteConfigAppendLine(state,line);
1770     }
1771     sdsfree(o);
1772 }
1773 
1774 /* Write the long long 'bytes' value as a string in a way that is parsable
1775  * inside redis.conf. If possible uses the GB, MB, KB notation. */
1776 int rewriteConfigFormatMemory(char *buf, size_t len, long long bytes) {
1777     int gb = 1024*1024*1024;
1778     int mb = 1024*1024;
1779     int kb = 1024;
1780 
1781     if (bytes && (bytes % gb) == 0) {
1782         return snprintf(buf,len,"%lldgb",bytes/gb);
1783     } else if (bytes && (bytes % mb) == 0) {
1784         return snprintf(buf,len,"%lldmb",bytes/mb);
1785     } else if (bytes && (bytes % kb) == 0) {
1786         return snprintf(buf,len,"%lldkb",bytes/kb);
1787     } else {
1788         return snprintf(buf,len,"%lld",bytes);
1789     }
1790 }
1791 
1792 /* Rewrite a simple "option-name <bytes>" configuration option. */
1793 void rewriteConfigBytesOption(struct rewriteConfigState *state, char *option, long long value, long long defvalue) {
1794     char buf[64];
1795     int force = value != defvalue;
1796     sds line;
1797 
1798     rewriteConfigFormatMemory(buf,sizeof(buf),value);
1799     line = sdscatprintf(sdsempty(),"%s %s",option,buf);
1800     rewriteConfigRewriteLine(state,option,line,force);
1801 }
1802 
1803 /* Rewrite a yes/no option. */
1804 void rewriteConfigYesNoOption(struct rewriteConfigState *state, char *option, int value, int defvalue) {
1805     int force = value != defvalue;
1806     sds line = sdscatprintf(sdsempty(),"%s %s",option,
1807         value ? "yes" : "no");
1808 
1809     rewriteConfigRewriteLine(state,option,line,force);
1810 }
1811 
1812 /* Rewrite a string option. */
1813 void rewriteConfigStringOption(struct rewriteConfigState *state, char *option, char *value, char *defvalue) {
1814     int force = 1;
1815     sds line;
1816 
1817     /* String options set to NULL need to be not present at all in the
1818      * configuration file to be set to NULL again at the next reboot. */
1819     if (value == NULL) {
1820         rewriteConfigMarkAsProcessed(state,option);
1821         return;
1822     }
1823 
1824     /* Set force to zero if the value is set to its default. */
1825     if (defvalue && strcmp(value,defvalue) == 0) force = 0;
1826 
1827     line = sdsnew(option);
1828     line = sdscatlen(line, " ", 1);
1829     line = sdscatrepr(line, value, strlen(value));
1830 
1831     rewriteConfigRewriteLine(state,option,line,force);
1832 }
1833 
1834 /* Rewrite a numerical (long long range) option. */
1835 void rewriteConfigNumericalOption(struct rewriteConfigState *state, char *option, long long value, long long defvalue) {
1836     int force = value != defvalue;
1837     sds line = sdscatprintf(sdsempty(),"%s %lld",option,value);
1838 
1839     rewriteConfigRewriteLine(state,option,line,force);
1840 }
1841 
1842 /* Rewrite a octal option. */
1843 void rewriteConfigOctalOption(struct rewriteConfigState *state, char *option, int value, int defvalue) {
1844     int force = value != defvalue;
1845     sds line = sdscatprintf(sdsempty(),"%s %o",option,value);
1846 
1847     rewriteConfigRewriteLine(state,option,line,force);
1848 }
1849 
1850 /* Rewrite an enumeration option. It takes as usually state and option name,
1851  * and in addition the enumeration array and the default value for the
1852  * option. */
1853 void rewriteConfigEnumOption(struct rewriteConfigState *state, char *option, int value, configEnum *ce, int defval) {
1854     sds line;
1855     const char *name = configEnumGetNameOrUnknown(ce,value);
1856     int force = value != defval;
1857 
1858     line = sdscatprintf(sdsempty(),"%s %s",option,name);
1859     rewriteConfigRewriteLine(state,option,line,force);
1860 }
1861 
1862 /* Rewrite the syslog-facility option. */
1863 void rewriteConfigSyslogfacilityOption(struct rewriteConfigState *state) {
1864     int value = server.syslog_facility;
1865     int force = value != LOG_LOCAL0;
1866     const char *name = NULL, *option = "syslog-facility";
1867     sds line;
1868 
1869     name = configEnumGetNameOrUnknown(syslog_facility_enum,value);
1870     line = sdscatprintf(sdsempty(),"%s %s",option,name);
1871     rewriteConfigRewriteLine(state,option,line,force);
1872 }
1873 
1874 /* Rewrite the save option. */
1875 void rewriteConfigSaveOption(struct rewriteConfigState *state) {
1876     int j;
1877     sds line;
1878 
1879     /* Note that if there are no save parameters at all, all the current
1880      * config line with "save" will be detected as orphaned and deleted,
1881      * resulting into no RDB persistence as expected. */
1882     for (j = 0; j < server.saveparamslen; j++) {
1883         line = sdscatprintf(sdsempty(),"save %ld %d",
1884             (long) server.saveparams[j].seconds, server.saveparams[j].changes);
1885         rewriteConfigRewriteLine(state,"save",line,1);
1886     }
1887     /* Mark "save" as processed in case server.saveparamslen is zero. */
1888     rewriteConfigMarkAsProcessed(state,"save");
1889 }
1890 
1891 /* Rewrite the dir option, always using absolute paths.*/
1892 void rewriteConfigDirOption(struct rewriteConfigState *state) {
1893     char cwd[1024];
1894 
1895     if (getcwd(cwd,sizeof(cwd)) == NULL) {
1896         rewriteConfigMarkAsProcessed(state,"dir");
1897         return; /* no rewrite on error. */
1898     }
1899     rewriteConfigStringOption(state,"dir",cwd,NULL);
1900 }
1901 
1902 /* Rewrite the slaveof option. */
1903 void rewriteConfigSlaveofOption(struct rewriteConfigState *state, char *option) {
1904     sds line;
1905 
1906     /* If this is a master, we want all the slaveof config options
1907      * in the file to be removed. Note that if this is a cluster instance
1908      * we don't want a slaveof directive inside redis.conf. */
1909     if (server.cluster_enabled || server.masterhost == NULL) {
1910         rewriteConfigMarkAsProcessed(state,option);
1911         return;
1912     }
1913     line = sdscatprintf(sdsempty(),"%s %s %d", option,
1914         server.masterhost, server.masterport);
1915     rewriteConfigRewriteLine(state,option,line,1);
1916 }
1917 
1918 /* Rewrite the notify-keyspace-events option. */
1919 void rewriteConfigNotifykeyspaceeventsOption(struct rewriteConfigState *state) {
1920     int force = server.notify_keyspace_events != 0;
1921     char *option = "notify-keyspace-events";
1922     sds line, flags;
1923 
1924     flags = keyspaceEventsFlagsToString(server.notify_keyspace_events);
1925     line = sdsnew(option);
1926     line = sdscatlen(line, " ", 1);
1927     line = sdscatrepr(line, flags, sdslen(flags));
1928     sdsfree(flags);
1929     rewriteConfigRewriteLine(state,option,line,force);
1930 }
1931 
1932 /* Rewrite the client-output-buffer-limit option. */
1933 void rewriteConfigClientoutputbufferlimitOption(struct rewriteConfigState *state) {
1934     int j;
1935     char *option = "client-output-buffer-limit";
1936 
1937     for (j = 0; j < CLIENT_TYPE_OBUF_COUNT; j++) {
1938         int force = (server.client_obuf_limits[j].hard_limit_bytes !=
1939                     clientBufferLimitsDefaults[j].hard_limit_bytes) ||
1940                     (server.client_obuf_limits[j].soft_limit_bytes !=
1941                     clientBufferLimitsDefaults[j].soft_limit_bytes) ||
1942                     (server.client_obuf_limits[j].soft_limit_seconds !=
1943                     clientBufferLimitsDefaults[j].soft_limit_seconds);
1944         sds line;
1945         char hard[64], soft[64];
1946 
1947         rewriteConfigFormatMemory(hard,sizeof(hard),
1948                 server.client_obuf_limits[j].hard_limit_bytes);
1949         rewriteConfigFormatMemory(soft,sizeof(soft),
1950                 server.client_obuf_limits[j].soft_limit_bytes);
1951 
1952         char *typename = getClientTypeName(j);
1953         if (!strcmp(typename,"slave")) typename = "replica";
1954         line = sdscatprintf(sdsempty(),"%s %s %s %s %ld",
1955                 option, typename, hard, soft,
1956                 (long) server.client_obuf_limits[j].soft_limit_seconds);
1957         rewriteConfigRewriteLine(state,option,line,force);
1958     }
1959 }
1960 
1961 /* Rewrite the bind option. */
1962 void rewriteConfigBindOption(struct rewriteConfigState *state) {
1963     int force = 1;
1964     sds line, addresses;
1965     char *option = "bind";
1966 
1967     /* Nothing to rewrite if we don't have bind addresses. */
1968     if (server.bindaddr_count == 0) {
1969         rewriteConfigMarkAsProcessed(state,option);
1970         return;
1971     }
1972 
1973     /* Rewrite as bind <addr1> <addr2> ... <addrN> */
1974     addresses = sdsjoin(server.bindaddr,server.bindaddr_count," ");
1975     line = sdsnew(option);
1976     line = sdscatlen(line, " ", 1);
1977     line = sdscatsds(line, addresses);
1978     sdsfree(addresses);
1979 
1980     rewriteConfigRewriteLine(state,option,line,force);
1981 }
1982 
1983 /* Glue together the configuration lines in the current configuration
1984  * rewrite state into a single string, stripping multiple empty lines. */
1985 sds rewriteConfigGetContentFromState(struct rewriteConfigState *state) {
1986     sds content = sdsempty();
1987     int j, was_empty = 0;
1988 
1989     for (j = 0; j < state->numlines; j++) {
1990         /* Every cluster of empty lines is turned into a single empty line. */
1991         if (sdslen(state->lines[j]) == 0) {
1992             if (was_empty) continue;
1993             was_empty = 1;
1994         } else {
1995             was_empty = 0;
1996         }
1997         content = sdscatsds(content,state->lines[j]);
1998         content = sdscatlen(content,"\n",1);
1999     }
2000     return content;
2001 }
2002 
2003 /* Free the configuration rewrite state. */
2004 void rewriteConfigReleaseState(struct rewriteConfigState *state) {
2005     sdsfreesplitres(state->lines,state->numlines);
2006     dictRelease(state->option_to_line);
2007     dictRelease(state->rewritten);
2008     zfree(state);
2009 }
2010 
2011 /* At the end of the rewrite process the state contains the remaining
2012  * map between "option name" => "lines in the original config file".
2013  * Lines used by the rewrite process were removed by the function
2014  * rewriteConfigRewriteLine(), all the other lines are "orphaned" and
2015  * should be replaced by empty lines.
2016  *
2017  * This function does just this, iterating all the option names and
2018  * blanking all the lines still associated. */
2019 void rewriteConfigRemoveOrphaned(struct rewriteConfigState *state) {
2020     dictIterator *di = dictGetIterator(state->option_to_line);
2021     dictEntry *de;
2022 
2023     while((de = dictNext(di)) != NULL) {
2024         list *l = dictGetVal(de);
2025         sds option = dictGetKey(de);
2026 
2027         /* Don't blank lines about options the rewrite process
2028          * don't understand. */
2029         if (dictFind(state->rewritten,option) == NULL) {
2030             serverLog(LL_DEBUG,"Not rewritten option: %s", option);
2031             continue;
2032         }
2033 
2034         while(listLength(l)) {
2035             listNode *ln = listFirst(l);
2036             int linenum = (long) ln->value;
2037 
2038             sdsfree(state->lines[linenum]);
2039             state->lines[linenum] = sdsempty();
2040             listDelNode(l,ln);
2041         }
2042     }
2043     dictReleaseIterator(di);
2044 }
2045 
2046 /* This function overwrites the old configuration file with the new content.
2047  *
2048  * 1) The old file length is obtained.
2049  * 2) If the new content is smaller, padding is added.
2050  * 3) A single write(2) call is used to replace the content of the file.
2051  * 4) Later the file is truncated to the length of the new content.
2052  *
2053  * This way we are sure the file is left in a consistent state even if the
2054  * process is stopped between any of the four operations.
2055  *
2056  * The function returns 0 on success, otherwise -1 is returned and errno
2057  * set accordingly. */
2058 int rewriteConfigOverwriteFile(char *configfile, sds content) {
2059     int retval = 0;
2060     int fd = open(configfile,O_RDWR|O_CREAT,0644);
2061     int content_size = sdslen(content), padding = 0;
2062     struct stat sb;
2063     sds content_padded;
2064 
2065     /* 1) Open the old file (or create a new one if it does not
2066      *    exist), get the size. */
2067     if (fd == -1) return -1; /* errno set by open(). */
2068     if (fstat(fd,&sb) == -1) {
2069         close(fd);
2070         return -1; /* errno set by fstat(). */
2071     }
2072 
2073     /* 2) Pad the content at least match the old file size. */
2074     content_padded = sdsdup(content);
2075     if (content_size < sb.st_size) {
2076         /* If the old file was bigger, pad the content with
2077          * a newline plus as many "#" chars as required. */
2078         padding = sb.st_size - content_size;
2079         content_padded = sdsgrowzero(content_padded,sb.st_size);
2080         content_padded[content_size] = '\n';
2081         memset(content_padded+content_size+1,'#',padding-1);
2082     }
2083 
2084     /* 3) Write the new content using a single write(2). */
2085     if (write(fd,content_padded,strlen(content_padded)) == -1) {
2086         retval = -1;
2087         goto cleanup;
2088     }
2089 
2090     /* 4) Truncate the file to the right length if we used padding. */
2091     if (padding) {
2092         if (ftruncate(fd,content_size) == -1) {
2093             /* Non critical error... */
2094         }
2095     }
2096 
2097 cleanup:
2098     sdsfree(content_padded);
2099     close(fd);
2100     return retval;
2101 }
2102 
2103 /* Rewrite the configuration file at "path".
2104  * If the configuration file already exists, we try at best to retain comments
2105  * and overall structure.
2106  *
2107  * Configuration parameters that are at their default value, unless already
2108  * explicitly included in the old configuration file, are not rewritten.
2109  *
2110  * On error -1 is returned and errno is set accordingly, otherwise 0. */
2111 int rewriteConfig(char *path) {
2112     struct rewriteConfigState *state;
2113     sds newcontent;
2114     int retval;
2115 
2116     /* Step 1: read the old config into our rewrite state. */
2117     if ((state = rewriteConfigReadOldFile(path)) == NULL) return -1;
2118 
2119     /* Step 2: rewrite every single option, replacing or appending it inside
2120      * the rewrite state. */
2121 
2122     rewriteConfigYesNoOption(state,"daemonize",server.daemonize,0);
2123     rewriteConfigStringOption(state,"pidfile",server.pidfile,CONFIG_DEFAULT_PID_FILE);
2124     rewriteConfigNumericalOption(state,"port",server.port,CONFIG_DEFAULT_SERVER_PORT);
2125     rewriteConfigNumericalOption(state,"cluster-announce-port",server.cluster_announce_port,CONFIG_DEFAULT_CLUSTER_ANNOUNCE_PORT);
2126     rewriteConfigNumericalOption(state,"cluster-announce-bus-port",server.cluster_announce_bus_port,CONFIG_DEFAULT_CLUSTER_ANNOUNCE_BUS_PORT);
2127     rewriteConfigNumericalOption(state,"tcp-backlog",server.tcp_backlog,CONFIG_DEFAULT_TCP_BACKLOG);
2128     rewriteConfigBindOption(state);
2129     rewriteConfigStringOption(state,"unixsocket",server.unixsocket,NULL);
2130     rewriteConfigOctalOption(state,"unixsocketperm",server.unixsocketperm,CONFIG_DEFAULT_UNIX_SOCKET_PERM);
2131     rewriteConfigNumericalOption(state,"timeout",server.maxidletime,CONFIG_DEFAULT_CLIENT_TIMEOUT);
2132     rewriteConfigNumericalOption(state,"tcp-keepalive",server.tcpkeepalive,CONFIG_DEFAULT_TCP_KEEPALIVE);
2133     rewriteConfigNumericalOption(state,"replica-announce-port",server.slave_announce_port,CONFIG_DEFAULT_SLAVE_ANNOUNCE_PORT);
2134     rewriteConfigEnumOption(state,"loglevel",server.verbosity,loglevel_enum,CONFIG_DEFAULT_VERBOSITY);
2135     rewriteConfigStringOption(state,"logfile",server.logfile,CONFIG_DEFAULT_LOGFILE);
2136     rewriteConfigYesNoOption(state,"syslog-enabled",server.syslog_enabled,CONFIG_DEFAULT_SYSLOG_ENABLED);
2137     rewriteConfigStringOption(state,"syslog-ident",server.syslog_ident,CONFIG_DEFAULT_SYSLOG_IDENT);
2138     rewriteConfigSyslogfacilityOption(state);
2139     rewriteConfigSaveOption(state);
2140     rewriteConfigNumericalOption(state,"databases",server.dbnum,CONFIG_DEFAULT_DBNUM);
2141     rewriteConfigYesNoOption(state,"stop-writes-on-bgsave-error",server.stop_writes_on_bgsave_err,CONFIG_DEFAULT_STOP_WRITES_ON_BGSAVE_ERROR);
2142     rewriteConfigYesNoOption(state,"rdbcompression",server.rdb_compression,CONFIG_DEFAULT_RDB_COMPRESSION);
2143     rewriteConfigYesNoOption(state,"rdbchecksum",server.rdb_checksum,CONFIG_DEFAULT_RDB_CHECKSUM);
2144     rewriteConfigStringOption(state,"dbfilename",server.rdb_filename,CONFIG_DEFAULT_RDB_FILENAME);
2145     rewriteConfigDirOption(state);
2146     rewriteConfigSlaveofOption(state,"replicaof");
2147     rewriteConfigStringOption(state,"replica-announce-ip",server.slave_announce_ip,CONFIG_DEFAULT_SLAVE_ANNOUNCE_IP);
2148     rewriteConfigStringOption(state,"masterauth",server.masterauth,NULL);
2149     rewriteConfigStringOption(state,"cluster-announce-ip",server.cluster_announce_ip,NULL);
2150     rewriteConfigYesNoOption(state,"replica-serve-stale-data",server.repl_serve_stale_data,CONFIG_DEFAULT_SLAVE_SERVE_STALE_DATA);
2151     rewriteConfigYesNoOption(state,"replica-read-only",server.repl_slave_ro,CONFIG_DEFAULT_SLAVE_READ_ONLY);
2152     rewriteConfigYesNoOption(state,"replica-ignore-maxmemory",server.repl_slave_ignore_maxmemory,CONFIG_DEFAULT_SLAVE_IGNORE_MAXMEMORY);
2153     rewriteConfigNumericalOption(state,"repl-ping-replica-period",server.repl_ping_slave_period,CONFIG_DEFAULT_REPL_PING_SLAVE_PERIOD);
2154     rewriteConfigNumericalOption(state,"repl-timeout",server.repl_timeout,CONFIG_DEFAULT_REPL_TIMEOUT);
2155     rewriteConfigBytesOption(state,"repl-backlog-size",server.repl_backlog_size,CONFIG_DEFAULT_REPL_BACKLOG_SIZE);
2156     rewriteConfigBytesOption(state,"repl-backlog-ttl",server.repl_backlog_time_limit,CONFIG_DEFAULT_REPL_BACKLOG_TIME_LIMIT);
2157     rewriteConfigYesNoOption(state,"repl-disable-tcp-nodelay",server.repl_disable_tcp_nodelay,CONFIG_DEFAULT_REPL_DISABLE_TCP_NODELAY);
2158     rewriteConfigYesNoOption(state,"repl-diskless-sync",server.repl_diskless_sync,CONFIG_DEFAULT_REPL_DISKLESS_SYNC);
2159     rewriteConfigNumericalOption(state,"repl-diskless-sync-delay",server.repl_diskless_sync_delay,CONFIG_DEFAULT_REPL_DISKLESS_SYNC_DELAY);
2160     rewriteConfigNumericalOption(state,"replica-priority",server.slave_priority,CONFIG_DEFAULT_SLAVE_PRIORITY);
2161     rewriteConfigNumericalOption(state,"min-replicas-to-write",server.repl_min_slaves_to_write,CONFIG_DEFAULT_MIN_SLAVES_TO_WRITE);
2162     rewriteConfigNumericalOption(state,"min-replicas-max-lag",server.repl_min_slaves_max_lag,CONFIG_DEFAULT_MIN_SLAVES_MAX_LAG);
2163     rewriteConfigStringOption(state,"requirepass",server.requirepass,NULL);
2164     rewriteConfigNumericalOption(state,"maxclients",server.maxclients,CONFIG_DEFAULT_MAX_CLIENTS);
2165     rewriteConfigBytesOption(state,"maxmemory",server.maxmemory,CONFIG_DEFAULT_MAXMEMORY);
2166     rewriteConfigBytesOption(state,"proto-max-bulk-len",server.proto_max_bulk_len,CONFIG_DEFAULT_PROTO_MAX_BULK_LEN);
2167     rewriteConfigBytesOption(state,"client-query-buffer-limit",server.client_max_querybuf_len,PROTO_MAX_QUERYBUF_LEN);
2168     rewriteConfigEnumOption(state,"maxmemory-policy",server.maxmemory_policy,maxmemory_policy_enum,CONFIG_DEFAULT_MAXMEMORY_POLICY);
2169     rewriteConfigNumericalOption(state,"maxmemory-samples",server.maxmemory_samples,CONFIG_DEFAULT_MAXMEMORY_SAMPLES);
2170     rewriteConfigNumericalOption(state,"lfu-log-factor",server.lfu_log_factor,CONFIG_DEFAULT_LFU_LOG_FACTOR);
2171     rewriteConfigNumericalOption(state,"lfu-decay-time",server.lfu_decay_time,CONFIG_DEFAULT_LFU_DECAY_TIME);
2172     rewriteConfigNumericalOption(state,"active-defrag-threshold-lower",server.active_defrag_threshold_lower,CONFIG_DEFAULT_DEFRAG_THRESHOLD_LOWER);
2173     rewriteConfigNumericalOption(state,"active-defrag-threshold-upper",server.active_defrag_threshold_upper,CONFIG_DEFAULT_DEFRAG_THRESHOLD_UPPER);
2174     rewriteConfigBytesOption(state,"active-defrag-ignore-bytes",server.active_defrag_ignore_bytes,CONFIG_DEFAULT_DEFRAG_IGNORE_BYTES);
2175     rewriteConfigNumericalOption(state,"active-defrag-cycle-min",server.active_defrag_cycle_min,CONFIG_DEFAULT_DEFRAG_CYCLE_MIN);
2176     rewriteConfigNumericalOption(state,"active-defrag-cycle-max",server.active_defrag_cycle_max,CONFIG_DEFAULT_DEFRAG_CYCLE_MAX);
2177     rewriteConfigNumericalOption(state,"active-defrag-max-scan-fields",server.active_defrag_max_scan_fields,CONFIG_DEFAULT_DEFRAG_MAX_SCAN_FIELDS);
2178     rewriteConfigYesNoOption(state,"appendonly",server.aof_state != AOF_OFF,0);
2179     rewriteConfigStringOption(state,"appendfilename",server.aof_filename,CONFIG_DEFAULT_AOF_FILENAME);
2180     rewriteConfigEnumOption(state,"appendfsync",server.aof_fsync,aof_fsync_enum,CONFIG_DEFAULT_AOF_FSYNC);
2181     rewriteConfigYesNoOption(state,"no-appendfsync-on-rewrite",server.aof_no_fsync_on_rewrite,CONFIG_DEFAULT_AOF_NO_FSYNC_ON_REWRITE);
2182     rewriteConfigNumericalOption(state,"auto-aof-rewrite-percentage",server.aof_rewrite_perc,AOF_REWRITE_PERC);
2183     rewriteConfigBytesOption(state,"auto-aof-rewrite-min-size",server.aof_rewrite_min_size,AOF_REWRITE_MIN_SIZE);
2184     rewriteConfigNumericalOption(state,"lua-time-limit",server.lua_time_limit,LUA_SCRIPT_TIME_LIMIT);
2185     rewriteConfigYesNoOption(state,"cluster-enabled",server.cluster_enabled,0);
2186     rewriteConfigStringOption(state,"cluster-config-file",server.cluster_configfile,CONFIG_DEFAULT_CLUSTER_CONFIG_FILE);
2187     rewriteConfigYesNoOption(state,"cluster-require-full-coverage",server.cluster_require_full_coverage,CLUSTER_DEFAULT_REQUIRE_FULL_COVERAGE);
2188     rewriteConfigYesNoOption(state,"cluster-replica-no-failover",server.cluster_slave_no_failover,CLUSTER_DEFAULT_SLAVE_NO_FAILOVER);
2189     rewriteConfigNumericalOption(state,"cluster-node-timeout",server.cluster_node_timeout,CLUSTER_DEFAULT_NODE_TIMEOUT);
2190     rewriteConfigNumericalOption(state,"cluster-migration-barrier",server.cluster_migration_barrier,CLUSTER_DEFAULT_MIGRATION_BARRIER);
2191     rewriteConfigNumericalOption(state,"cluster-replica-validity-factor",server.cluster_slave_validity_factor,CLUSTER_DEFAULT_SLAVE_VALIDITY);
2192     rewriteConfigNumericalOption(state,"slowlog-log-slower-than",server.slowlog_log_slower_than,CONFIG_DEFAULT_SLOWLOG_LOG_SLOWER_THAN);
2193     rewriteConfigNumericalOption(state,"latency-monitor-threshold",server.latency_monitor_threshold,CONFIG_DEFAULT_LATENCY_MONITOR_THRESHOLD);
2194     rewriteConfigNumericalOption(state,"slowlog-max-len",server.slowlog_max_len,CONFIG_DEFAULT_SLOWLOG_MAX_LEN);
2195     rewriteConfigNotifykeyspaceeventsOption(state);
2196     rewriteConfigNumericalOption(state,"hash-max-ziplist-entries",server.hash_max_ziplist_entries,OBJ_HASH_MAX_ZIPLIST_ENTRIES);
2197     rewriteConfigNumericalOption(state,"hash-max-ziplist-value",server.hash_max_ziplist_value,OBJ_HASH_MAX_ZIPLIST_VALUE);
2198     rewriteConfigNumericalOption(state,"stream-node-max-bytes",server.stream_node_max_bytes,OBJ_STREAM_NODE_MAX_BYTES);
2199     rewriteConfigNumericalOption(state,"stream-node-max-entries",server.stream_node_max_entries,OBJ_STREAM_NODE_MAX_ENTRIES);
2200     rewriteConfigNumericalOption(state,"list-max-ziplist-size",server.list_max_ziplist_size,OBJ_LIST_MAX_ZIPLIST_SIZE);
2201     rewriteConfigNumericalOption(state,"list-compress-depth",server.list_compress_depth,OBJ_LIST_COMPRESS_DEPTH);
2202     rewriteConfigNumericalOption(state,"set-max-intset-entries",server.set_max_intset_entries,OBJ_SET_MAX_INTSET_ENTRIES);
2203     rewriteConfigNumericalOption(state,"zset-max-ziplist-entries",server.zset_max_ziplist_entries,OBJ_ZSET_MAX_ZIPLIST_ENTRIES);
2204     rewriteConfigNumericalOption(state,"zset-max-ziplist-value",server.zset_max_ziplist_value,OBJ_ZSET_MAX_ZIPLIST_VALUE);
2205     rewriteConfigNumericalOption(state,"hll-sparse-max-bytes",server.hll_sparse_max_bytes,CONFIG_DEFAULT_HLL_SPARSE_MAX_BYTES);
2206     rewriteConfigYesNoOption(state,"activerehashing",server.activerehashing,CONFIG_DEFAULT_ACTIVE_REHASHING);
2207     rewriteConfigYesNoOption(state,"activedefrag",server.active_defrag_enabled,CONFIG_DEFAULT_ACTIVE_DEFRAG);
2208     rewriteConfigYesNoOption(state,"protected-mode",server.protected_mode,CONFIG_DEFAULT_PROTECTED_MODE);
2209     rewriteConfigClientoutputbufferlimitOption(state);
2210     rewriteConfigNumericalOption(state,"hz",server.config_hz,CONFIG_DEFAULT_HZ);
2211     rewriteConfigYesNoOption(state,"aof-rewrite-incremental-fsync",server.aof_rewrite_incremental_fsync,CONFIG_DEFAULT_AOF_REWRITE_INCREMENTAL_FSYNC);
2212     rewriteConfigYesNoOption(state,"rdb-save-incremental-fsync",server.rdb_save_incremental_fsync,CONFIG_DEFAULT_RDB_SAVE_INCREMENTAL_FSYNC);
2213     rewriteConfigYesNoOption(state,"aof-load-truncated",server.aof_load_truncated,CONFIG_DEFAULT_AOF_LOAD_TRUNCATED);
2214     rewriteConfigYesNoOption(state,"aof-use-rdb-preamble",server.aof_use_rdb_preamble,CONFIG_DEFAULT_AOF_USE_RDB_PREAMBLE);
2215     rewriteConfigEnumOption(state,"supervised",server.supervised_mode,supervised_mode_enum,SUPERVISED_NONE);
2216     rewriteConfigYesNoOption(state,"lazyfree-lazy-eviction",server.lazyfree_lazy_eviction,CONFIG_DEFAULT_LAZYFREE_LAZY_EVICTION);
2217     rewriteConfigYesNoOption(state,"lazyfree-lazy-expire",server.lazyfree_lazy_expire,CONFIG_DEFAULT_LAZYFREE_LAZY_EXPIRE);
2218     rewriteConfigYesNoOption(state,"lazyfree-lazy-server-del",server.lazyfree_lazy_server_del,CONFIG_DEFAULT_LAZYFREE_LAZY_SERVER_DEL);
2219     rewriteConfigYesNoOption(state,"replica-lazy-flush",server.repl_slave_lazy_flush,CONFIG_DEFAULT_SLAVE_LAZY_FLUSH);
2220     rewriteConfigYesNoOption(state,"dynamic-hz",server.dynamic_hz,CONFIG_DEFAULT_DYNAMIC_HZ);
2221 
2222     /* Rewrite Sentinel config if in Sentinel mode. */
2223     if (server.sentinel_mode) rewriteConfigSentinelOption(state);
2224 
2225     /* Step 3: remove all the orphaned lines in the old file, that is, lines
2226      * that were used by a config option and are no longer used, like in case
2227      * of multiple "save" options or duplicated options. */
2228     rewriteConfigRemoveOrphaned(state);
2229 
2230     /* Step 4: generate a new configuration file from the modified state
2231      * and write it into the original file. */
2232     newcontent = rewriteConfigGetContentFromState(state);
2233     retval = rewriteConfigOverwriteFile(server.configfile,newcontent);
2234 
2235     sdsfree(newcontent);
2236     rewriteConfigReleaseState(state);
2237     return retval;
2238 }
2239 
2240 /*-----------------------------------------------------------------------------
2241  * CONFIG command entry point
2242  *----------------------------------------------------------------------------*/
2243 
2244 void configCommand(client *c) {
2245     /* Only allow CONFIG GET while loading. */
2246     if (server.loading && strcasecmp(c->argv[1]->ptr,"get")) {
2247         addReplyError(c,"Only CONFIG GET is allowed during loading");
2248         return;
2249     }
2250 
2251     if (c->argc == 2 && !strcasecmp(c->argv[1]->ptr,"help")) {
2252         const char *help[] = {
2253 "GET <pattern> -- Return parameters matching the glob-like <pattern> and their values.",
2254 "SET <parameter> <value> -- Set parameter to value.",
2255 "RESETSTAT -- Reset statistics reported by INFO.",
2256 "REWRITE -- Rewrite the configuration file.",
2257 NULL
2258         };
2259         addReplyHelp(c, help);
2260     } else if (!strcasecmp(c->argv[1]->ptr,"set") && c->argc == 4) {
2261         configSetCommand(c);
2262     } else if (!strcasecmp(c->argv[1]->ptr,"get") && c->argc == 3) {
2263         configGetCommand(c);
2264     } else if (!strcasecmp(c->argv[1]->ptr,"resetstat") && c->argc == 2) {
2265         resetServerStats();
2266         resetCommandTableStats();
2267         addReply(c,shared.ok);
2268     } else if (!strcasecmp(c->argv[1]->ptr,"rewrite") && c->argc == 2) {
2269         if (server.configfile == NULL) {
2270             addReplyError(c,"The server is running without a config file");
2271             return;
2272         }
2273         if (rewriteConfig(server.configfile) == -1) {
2274             serverLog(LL_WARNING,"CONFIG REWRITE failed: %s", strerror(errno));
2275             addReplyErrorFormat(c,"Rewriting config file: %s", strerror(errno));
2276         } else {
2277             serverLog(LL_WARNING,"CONFIG REWRITE executed with success.");
2278             addReply(c,shared.ok);
2279         }
2280     } else {
2281         addReplySubcommandSyntaxError(c);
2282         return;
2283     }
2284 }
2285