1 /*
2  * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  *   * Redistributions of source code must retain the above copyright notice,
9  *     this list of conditions and the following disclaimer.
10  *   * Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  *   * Neither the name of Redis nor the names of its contributors may be used
14  *     to endorse or promote products derived from this software without
15  *     specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #ifndef __REDIS_H
31 #define __REDIS_H
32 
33 #include "fmacros.h"
34 #include "config.h"
35 #include "solarisfixes.h"
36 #include "rio.h"
37 #include "atomicvar.h"
38 
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <time.h>
43 #include <limits.h>
44 #include <unistd.h>
45 #include <errno.h>
46 #include <inttypes.h>
47 #include <pthread.h>
48 #include <syslog.h>
49 #include <netinet/in.h>
50 #include <sys/socket.h>
51 #include <lua.h>
52 #include <signal.h>
53 
54 #ifdef HAVE_LIBSYSTEMD
55 #include <systemd/sd-daemon.h>
56 #endif
57 
58 typedef long long mstime_t; /* millisecond time type. */
59 typedef long long ustime_t; /* microsecond time type. */
60 
61 #include "ae.h"      /* Event driven programming library */
62 #include "sds.h"     /* Dynamic safe strings */
63 #include "dict.h"    /* Hash tables */
64 #include "adlist.h"  /* Linked lists */
65 #include "zmalloc.h" /* total memory usage aware version of malloc/free */
66 #include "anet.h"    /* Networking the easy way */
67 #include "ziplist.h" /* Compact list data structure */
68 #include "intset.h"  /* Compact integer set structure */
69 #include "version.h" /* Version macro */
70 #include "util.h"    /* Misc functions useful in many places */
71 #include "latency.h" /* Latency monitor API */
72 #include "sparkline.h" /* ASCII graphs API */
73 #include "quicklist.h"  /* Lists are encoded as linked lists of
74                            N-elements flat arrays */
75 #include "rax.h"     /* Radix tree */
76 #include "connection.h" /* Connection abstraction */
77 
78 #define REDISMODULE_CORE 1
79 #include "redismodule.h"    /* Redis modules API defines. */
80 
81 /* Following includes allow test functions to be called from Redis main() */
82 #include "zipmap.h"
83 #include "sha1.h"
84 #include "endianconv.h"
85 #include "crc64.h"
86 
87 /* min/max */
88 #define min(a, b) ((a) < (b) ? (a) : (b))
89 #define max(a, b) ((a) > (b) ? (a) : (b))
90 
91 /* Error codes */
92 #define C_OK                    0
93 #define C_ERR                   -1
94 
95 /* Static server configuration */
96 #define CONFIG_DEFAULT_HZ        10             /* Time interrupt calls/sec. */
97 #define CONFIG_MIN_HZ            1
98 #define CONFIG_MAX_HZ            500
99 #define MAX_CLIENTS_PER_CLOCK_TICK 200          /* HZ is adapted based on that. */
100 #define CONFIG_MAX_LINE    1024
101 #define CRON_DBS_PER_CALL 16
102 #define NET_MAX_WRITES_PER_EVENT (1024*64)
103 #define PROTO_SHARED_SELECT_CMDS 10
104 #define OBJ_SHARED_INTEGERS 10000
105 #define OBJ_SHARED_BULKHDR_LEN 32
106 #define LOG_MAX_LEN    1024 /* Default maximum length of syslog messages.*/
107 #define AOF_REWRITE_ITEMS_PER_CMD 64
108 #define AOF_READ_DIFF_INTERVAL_BYTES (1024*10)
109 #define AOF_ANNOTATION_LINE_MAX_LEN 1024
110 #define CONFIG_AUTHPASS_MAX_LEN 512
111 #define CONFIG_RUN_ID_SIZE 40
112 #define RDB_EOF_MARK_SIZE 40
113 #define CONFIG_REPL_BACKLOG_MIN_SIZE (1024*16)          /* 16k */
114 #define CONFIG_BGSAVE_RETRY_DELAY 5 /* Wait a few secs before trying again. */
115 #define CONFIG_DEFAULT_PID_FILE "/var/run/redis.pid"
116 #define CONFIG_DEFAULT_BINDADDR_COUNT 2
117 #define CONFIG_DEFAULT_BINDADDR { "*", "-::*" }
118 #define NET_HOST_STR_LEN 256 /* Longest valid hostname */
119 #define NET_IP_STR_LEN 46 /* INET6_ADDRSTRLEN is 46, but we need to be sure */
120 #define NET_ADDR_STR_LEN (NET_IP_STR_LEN+32) /* Must be enough for ip:port */
121 #define NET_HOST_PORT_STR_LEN (NET_HOST_STR_LEN+32) /* Must be enough for hostname:port */
122 #define CONFIG_BINDADDR_MAX 16
123 #define CONFIG_MIN_RESERVED_FDS 32
124 #define CONFIG_DEFAULT_PROC_TITLE_TEMPLATE "{title} {listen-addr} {server-mode}"
125 
126 /* Bucket sizes for client eviction pools. Each bucket stores clients with
127  * memory usage of up to twice the size of the bucket below it. */
128 #define CLIENT_MEM_USAGE_BUCKET_MIN_LOG 15 /* Bucket sizes start at up to 32KB (2^15) */
129 #define CLIENT_MEM_USAGE_BUCKET_MAX_LOG 33 /* Bucket for largest clients: sizes above 4GB (2^32) */
130 #define CLIENT_MEM_USAGE_BUCKETS (1+CLIENT_MEM_USAGE_BUCKET_MAX_LOG-CLIENT_MEM_USAGE_BUCKET_MIN_LOG)
131 
132 #define ACTIVE_EXPIRE_CYCLE_SLOW 0
133 #define ACTIVE_EXPIRE_CYCLE_FAST 1
134 
135 /* Children process will exit with this status code to signal that the
136  * process terminated without an error: this is useful in order to kill
137  * a saving child (RDB or AOF one), without triggering in the parent the
138  * write protection that is normally turned on on write errors.
139  * Usually children that are terminated with SIGUSR1 will exit with this
140  * special code. */
141 #define SERVER_CHILD_NOERROR_RETVAL    255
142 
143 /* Reading copy-on-write info is sometimes expensive and may slow down child
144  * processes that report it continuously. We measure the cost of obtaining it
145  * and hold back additional reading based on this factor. */
146 #define CHILD_COW_DUTY_CYCLE           100
147 
148 /* Instantaneous metrics tracking. */
149 #define STATS_METRIC_SAMPLES 16     /* Number of samples per metric. */
150 #define STATS_METRIC_COMMAND 0      /* Number of commands executed. */
151 #define STATS_METRIC_NET_INPUT 1    /* Bytes read to network .*/
152 #define STATS_METRIC_NET_OUTPUT 2   /* Bytes written to network. */
153 #define STATS_METRIC_COUNT 3
154 
155 /* Protocol and I/O related defines */
156 #define PROTO_IOBUF_LEN         (1024*16)  /* Generic I/O buffer size */
157 #define PROTO_REPLY_CHUNK_BYTES (16*1024) /* 16k output buffer */
158 #define PROTO_INLINE_MAX_SIZE   (1024*64) /* Max size of inline reads */
159 #define PROTO_MBULK_BIG_ARG     (1024*32)
160 #define PROTO_RESIZE_THRESHOLD  (1024*32) /* Threshold for determining whether to resize query buffer */
161 #define LONG_STR_SIZE      21          /* Bytes needed for long -> str + '\0' */
162 #define REDIS_AUTOSYNC_BYTES (1024*1024*4) /* Sync file every 4MB. */
163 
164 #define LIMIT_PENDING_QUERYBUF (4*1024*1024) /* 4mb */
165 
166 /* When configuring the server eventloop, we setup it so that the total number
167  * of file descriptors we can handle are server.maxclients + RESERVED_FDS +
168  * a few more to stay safe. Since RESERVED_FDS defaults to 32, we add 96
169  * in order to make sure of not over provisioning more than 128 fds. */
170 #define CONFIG_FDSET_INCR (CONFIG_MIN_RESERVED_FDS+96)
171 
172 /* OOM Score Adjustment classes. */
173 #define CONFIG_OOM_MASTER 0
174 #define CONFIG_OOM_REPLICA 1
175 #define CONFIG_OOM_BGCHILD 2
176 #define CONFIG_OOM_COUNT 3
177 
178 extern int configOOMScoreAdjValuesDefaults[CONFIG_OOM_COUNT];
179 
180 /* Hash table parameters */
181 #define HASHTABLE_MIN_FILL        10      /* Minimal hash table fill 10% */
182 #define HASHTABLE_MAX_LOAD_FACTOR 1.618   /* Maximum hash table load factor. */
183 
184 /* Command flags. Please check the command table defined in the server.c file
185  * for more information about the meaning of every flag. */
186 #define CMD_WRITE (1ULL<<0)            /* "write" flag */
187 #define CMD_READONLY (1ULL<<1)         /* "read-only" flag */
188 #define CMD_DENYOOM (1ULL<<2)          /* "use-memory" flag */
189 #define CMD_MODULE (1ULL<<3)           /* Command exported by module. */
190 #define CMD_ADMIN (1ULL<<4)            /* "admin" flag */
191 #define CMD_PUBSUB (1ULL<<5)           /* "pub-sub" flag */
192 #define CMD_NOSCRIPT (1ULL<<6)         /* "no-script" flag */
193 #define CMD_RANDOM (1ULL<<7)           /* "random" flag */
194 #define CMD_SORT_FOR_SCRIPT (1ULL<<8)  /* "to-sort" flag */
195 #define CMD_LOADING (1ULL<<9)          /* "ok-loading" flag */
196 #define CMD_STALE (1ULL<<10)           /* "ok-stale" flag */
197 #define CMD_SKIP_MONITOR (1ULL<<11)    /* "no-monitor" flag */
198 #define CMD_SKIP_SLOWLOG (1ULL<<12)    /* "no-slowlog" flag */
199 #define CMD_ASKING (1ULL<<13)          /* "cluster-asking" flag */
200 #define CMD_FAST (1ULL<<14)            /* "fast" flag */
201 #define CMD_NO_AUTH (1ULL<<15)         /* "no-auth" flag */
202 #define CMD_MAY_REPLICATE (1ULL<<16)   /* "may-replicate" flag */
203 
204 /* Key argument flags. Please check the command table defined in the server.c file
205  * for more information about the meaning of every flag. */
206 #define CMD_KEY_WRITE (1ULL<<0)        /* "write" flag */
207 #define CMD_KEY_READ (1ULL<<1)         /* "read" flag */
208 #define CMD_KEY_INCOMPLETE (1ULL<<2)   /* "incomplete" flag (meaning that the keyspec might not point out to all keys it should cover) */
209 
210 /* Command flags used by the module system. */
211 #define CMD_MODULE_GETKEYS (1ULL<<17)  /* Use the modules getkeys interface. */
212 #define CMD_MODULE_NO_CLUSTER (1ULL<<18) /* Deny on Redis Cluster. */
213 
214 /* Command flags that describe ACLs categories. */
215 #define CMD_CATEGORY_KEYSPACE (1ULL<<19)
216 #define CMD_CATEGORY_READ (1ULL<<20)
217 #define CMD_CATEGORY_WRITE (1ULL<<21)
218 #define CMD_CATEGORY_SET (1ULL<<22)
219 #define CMD_CATEGORY_SORTEDSET (1ULL<<23)
220 #define CMD_CATEGORY_LIST (1ULL<<24)
221 #define CMD_CATEGORY_HASH (1ULL<<25)
222 #define CMD_CATEGORY_STRING (1ULL<<26)
223 #define CMD_CATEGORY_BITMAP (1ULL<<27)
224 #define CMD_CATEGORY_HYPERLOGLOG (1ULL<<28)
225 #define CMD_CATEGORY_GEO (1ULL<<29)
226 #define CMD_CATEGORY_STREAM (1ULL<<30)
227 #define CMD_CATEGORY_PUBSUB (1ULL<<31)
228 #define CMD_CATEGORY_ADMIN (1ULL<<32)
229 #define CMD_CATEGORY_FAST (1ULL<<33)
230 #define CMD_CATEGORY_SLOW (1ULL<<34)
231 #define CMD_CATEGORY_BLOCKING (1ULL<<35)
232 #define CMD_CATEGORY_DANGEROUS (1ULL<<36)
233 #define CMD_CATEGORY_CONNECTION (1ULL<<37)
234 #define CMD_CATEGORY_TRANSACTION (1ULL<<38)
235 #define CMD_CATEGORY_SCRIPTING (1ULL<<39)
236 
237 #define CMD_SENTINEL (1ULL<<40)        /* "sentinel" flag */
238 #define CMD_ONLY_SENTINEL (1ULL<<41)   /* "only-sentinel" flag */
239 #define CMD_NO_MANDATORY_KEYS (1ULL<<42)   /* "no-mandatory-keys" flag */
240 
241 /* AOF states */
242 #define AOF_OFF 0             /* AOF is off */
243 #define AOF_ON 1              /* AOF is on */
244 #define AOF_WAIT_REWRITE 2    /* AOF waits rewrite to start appending */
245 
246 /* AOF return values for loadAppendOnlyFile() */
247 #define AOF_OK 0
248 #define AOF_NOT_EXIST 1
249 #define AOF_EMPTY 2
250 #define AOF_OPEN_ERR 3
251 #define AOF_FAILED 4
252 
253 /* Client flags */
254 #define CLIENT_SLAVE (1<<0)   /* This client is a replica */
255 #define CLIENT_MASTER (1<<1)  /* This client is a master */
256 #define CLIENT_MONITOR (1<<2) /* This client is a slave monitor, see MONITOR */
257 #define CLIENT_MULTI (1<<3)   /* This client is in a MULTI context */
258 #define CLIENT_BLOCKED (1<<4) /* The client is waiting in a blocking operation */
259 #define CLIENT_DIRTY_CAS (1<<5) /* Watched keys modified. EXEC will fail. */
260 #define CLIENT_CLOSE_AFTER_REPLY (1<<6) /* Close after writing entire reply. */
261 #define CLIENT_UNBLOCKED (1<<7) /* This client was unblocked and is stored in
262                                   server.unblocked_clients */
263 #define CLIENT_LUA (1<<8) /* This is a non connected client used by Lua */
264 #define CLIENT_ASKING (1<<9)     /* Client issued the ASKING command */
265 #define CLIENT_CLOSE_ASAP (1<<10)/* Close this client ASAP */
266 #define CLIENT_UNIX_SOCKET (1<<11) /* Client connected via Unix domain socket */
267 #define CLIENT_DIRTY_EXEC (1<<12)  /* EXEC will fail for errors while queueing */
268 #define CLIENT_MASTER_FORCE_REPLY (1<<13)  /* Queue replies even if is master */
269 #define CLIENT_FORCE_AOF (1<<14)   /* Force AOF propagation of current cmd. */
270 #define CLIENT_FORCE_REPL (1<<15)  /* Force replication of current cmd. */
271 #define CLIENT_PRE_PSYNC (1<<16)   /* Instance don't understand PSYNC. */
272 #define CLIENT_READONLY (1<<17)    /* Cluster client is in read-only state. */
273 #define CLIENT_PUBSUB (1<<18)      /* Client is in Pub/Sub mode. */
274 #define CLIENT_PREVENT_AOF_PROP (1<<19)  /* Don't propagate to AOF. */
275 #define CLIENT_PREVENT_REPL_PROP (1<<20)  /* Don't propagate to slaves. */
276 #define CLIENT_PREVENT_PROP (CLIENT_PREVENT_AOF_PROP|CLIENT_PREVENT_REPL_PROP)
277 #define CLIENT_PENDING_WRITE (1<<21) /* Client has output to send but a write
278                                         handler is yet not installed. */
279 #define CLIENT_REPLY_OFF (1<<22)   /* Don't send replies to client. */
280 #define CLIENT_REPLY_SKIP_NEXT (1<<23)  /* Set CLIENT_REPLY_SKIP for next cmd */
281 #define CLIENT_REPLY_SKIP (1<<24)  /* Don't send just this reply. */
282 #define CLIENT_LUA_DEBUG (1<<25)  /* Run EVAL in debug mode. */
283 #define CLIENT_LUA_DEBUG_SYNC (1<<26)  /* EVAL debugging without fork() */
284 #define CLIENT_MODULE (1<<27) /* Non connected client used by some module. */
285 #define CLIENT_PROTECTED (1<<28) /* Client should not be freed for now. */
286 /* #define CLIENT_... (1<<29) currently unused, feel free to use in the future */
287 #define CLIENT_PENDING_COMMAND (1<<30) /* Indicates the client has a fully
288                                         * parsed command ready for execution. */
289 #define CLIENT_TRACKING (1ULL<<31) /* Client enabled keys tracking in order to
290                                    perform client side caching. */
291 #define CLIENT_TRACKING_BROKEN_REDIR (1ULL<<32) /* Target client is invalid. */
292 #define CLIENT_TRACKING_BCAST (1ULL<<33) /* Tracking in BCAST mode. */
293 #define CLIENT_TRACKING_OPTIN (1ULL<<34)  /* Tracking in opt-in mode. */
294 #define CLIENT_TRACKING_OPTOUT (1ULL<<35) /* Tracking in opt-out mode. */
295 #define CLIENT_TRACKING_CACHING (1ULL<<36) /* CACHING yes/no was given,
296                                               depending on optin/optout mode. */
297 #define CLIENT_TRACKING_NOLOOP (1ULL<<37) /* Don't send invalidation messages
298                                              about writes performed by myself.*/
299 #define CLIENT_IN_TO_TABLE (1ULL<<38) /* This client is in the timeout table. */
300 #define CLIENT_PROTOCOL_ERROR (1ULL<<39) /* Protocol error chatting with it. */
301 #define CLIENT_CLOSE_AFTER_COMMAND (1ULL<<40) /* Close after executing commands
302                                                * and writing entire reply. */
303 #define CLIENT_DENY_BLOCKING (1ULL<<41) /* Indicate that the client should not be blocked.
304                                            currently, turned on inside MULTI, Lua, RM_Call,
305                                            and AOF client */
306 #define CLIENT_REPL_RDBONLY (1ULL<<42) /* This client is a replica that only wants
307                                           RDB without replication buffer. */
308 #define CLIENT_NO_EVICT (1ULL<<43) /* This client is protected against client
309                                       memory eviction. */
310 
311 /* Client block type (btype field in client structure)
312  * if CLIENT_BLOCKED flag is set. */
313 #define BLOCKED_NONE 0    /* Not blocked, no CLIENT_BLOCKED flag set. */
314 #define BLOCKED_LIST 1    /* BLPOP & co. */
315 #define BLOCKED_WAIT 2    /* WAIT for synchronous replication. */
316 #define BLOCKED_MODULE 3  /* Blocked by a loadable module. */
317 #define BLOCKED_STREAM 4  /* XREAD. */
318 #define BLOCKED_ZSET 5    /* BZPOP et al. */
319 #define BLOCKED_PAUSE 6   /* Blocked by CLIENT PAUSE */
320 #define BLOCKED_NUM 7     /* Number of blocked states. */
321 
322 /* Client request types */
323 #define PROTO_REQ_INLINE 1
324 #define PROTO_REQ_MULTIBULK 2
325 
326 /* Client classes for client limits, currently used only for
327  * the max-client-output-buffer limit implementation. */
328 #define CLIENT_TYPE_NORMAL 0 /* Normal req-reply clients + MONITORs */
329 #define CLIENT_TYPE_SLAVE 1  /* Slaves. */
330 #define CLIENT_TYPE_PUBSUB 2 /* Clients subscribed to PubSub channels. */
331 #define CLIENT_TYPE_MASTER 3 /* Master. */
332 #define CLIENT_TYPE_COUNT 4  /* Total number of client types. */
333 #define CLIENT_TYPE_OBUF_COUNT 3 /* Number of clients to expose to output
334                                     buffer configuration. Just the first
335                                     three: normal, slave, pubsub. */
336 
337 /* Slave replication state. Used in server.repl_state for slaves to remember
338  * what to do next. */
339 typedef enum {
340     REPL_STATE_NONE = 0,            /* No active replication */
341     REPL_STATE_CONNECT,             /* Must connect to master */
342     REPL_STATE_CONNECTING,          /* Connecting to master */
343     /* --- Handshake states, must be ordered --- */
344     REPL_STATE_RECEIVE_PING_REPLY,  /* Wait for PING reply */
345     REPL_STATE_SEND_HANDSHAKE,      /* Send handshake sequence to master */
346     REPL_STATE_RECEIVE_AUTH_REPLY,  /* Wait for AUTH reply */
347     REPL_STATE_RECEIVE_PORT_REPLY,  /* Wait for REPLCONF reply */
348     REPL_STATE_RECEIVE_IP_REPLY,    /* Wait for REPLCONF reply */
349     REPL_STATE_RECEIVE_CAPA_REPLY,  /* Wait for REPLCONF reply */
350     REPL_STATE_SEND_PSYNC,          /* Send PSYNC */
351     REPL_STATE_RECEIVE_PSYNC_REPLY, /* Wait for PSYNC reply */
352     /* --- End of handshake states --- */
353     REPL_STATE_TRANSFER,        /* Receiving .rdb from master */
354     REPL_STATE_CONNECTED,       /* Connected to master */
355 } repl_state;
356 
357 /* The state of an in progress coordinated failover */
358 typedef enum {
359     NO_FAILOVER = 0,        /* No failover in progress */
360     FAILOVER_WAIT_FOR_SYNC, /* Waiting for target replica to catch up */
361     FAILOVER_IN_PROGRESS    /* Waiting for target replica to accept
362                              * PSYNC FAILOVER request. */
363 } failover_state;
364 
365 /* State of slaves from the POV of the master. Used in client->replstate.
366  * In SEND_BULK and ONLINE state the slave receives new updates
367  * in its output queue. In the WAIT_BGSAVE states instead the server is waiting
368  * to start the next background saving in order to send updates to it. */
369 #define SLAVE_STATE_WAIT_BGSAVE_START 6 /* We need to produce a new RDB file. */
370 #define SLAVE_STATE_WAIT_BGSAVE_END 7 /* Waiting RDB file creation to finish. */
371 #define SLAVE_STATE_SEND_BULK 8 /* Sending RDB file to slave. */
372 #define SLAVE_STATE_ONLINE 9 /* RDB file transmitted, sending just updates. */
373 
374 /* Slave capabilities. */
375 #define SLAVE_CAPA_NONE 0
376 #define SLAVE_CAPA_EOF (1<<0)    /* Can parse the RDB EOF streaming format. */
377 #define SLAVE_CAPA_PSYNC2 (1<<1) /* Supports PSYNC2 protocol. */
378 
379 /* Synchronous read timeout - slave side */
380 #define CONFIG_REPL_SYNCIO_TIMEOUT 5
381 
382 /* The default number of replication backlog blocks to trim per call. */
383 #define REPL_BACKLOG_TRIM_BLOCKS_PER_CALL 64
384 
385 /* In order to quickly find the requested offset for PSYNC requests,
386  * we index some nodes in the replication buffer linked list into a rax. */
387 #define REPL_BACKLOG_INDEX_PER_BLOCKS 64
388 
389 /* List related stuff */
390 #define LIST_HEAD 0
391 #define LIST_TAIL 1
392 #define ZSET_MIN 0
393 #define ZSET_MAX 1
394 
395 /* Sort operations */
396 #define SORT_OP_GET 0
397 
398 /* Log levels */
399 #define LL_DEBUG 0
400 #define LL_VERBOSE 1
401 #define LL_NOTICE 2
402 #define LL_WARNING 3
403 #define LL_RAW (1<<10) /* Modifier to log without timestamp */
404 
405 /* Supervision options */
406 #define SUPERVISED_NONE 0
407 #define SUPERVISED_AUTODETECT 1
408 #define SUPERVISED_SYSTEMD 2
409 #define SUPERVISED_UPSTART 3
410 
411 /* Anti-warning macro... */
412 #define UNUSED(V) ((void) V)
413 
414 #define ZSKIPLIST_MAXLEVEL 32 /* Should be enough for 2^64 elements */
415 #define ZSKIPLIST_P 0.25      /* Skiplist P = 1/4 */
416 
417 /* Append only defines */
418 #define AOF_FSYNC_NO 0
419 #define AOF_FSYNC_ALWAYS 1
420 #define AOF_FSYNC_EVERYSEC 2
421 
422 /* Replication diskless load defines */
423 #define REPL_DISKLESS_LOAD_DISABLED 0
424 #define REPL_DISKLESS_LOAD_WHEN_DB_EMPTY 1
425 #define REPL_DISKLESS_LOAD_SWAPDB 2
426 
427 /* TLS Client Authentication */
428 #define TLS_CLIENT_AUTH_NO 0
429 #define TLS_CLIENT_AUTH_YES 1
430 #define TLS_CLIENT_AUTH_OPTIONAL 2
431 
432 /* Sanitize dump payload */
433 #define SANITIZE_DUMP_NO 0
434 #define SANITIZE_DUMP_YES 1
435 #define SANITIZE_DUMP_CLIENTS 2
436 
437 /* Sets operations codes */
438 #define SET_OP_UNION 0
439 #define SET_OP_DIFF 1
440 #define SET_OP_INTER 2
441 
442 /* oom-score-adj defines */
443 #define OOM_SCORE_ADJ_NO 0
444 #define OOM_SCORE_RELATIVE 1
445 #define OOM_SCORE_ADJ_ABSOLUTE 2
446 
447 /* Redis maxmemory strategies. Instead of using just incremental number
448  * for this defines, we use a set of flags so that testing for certain
449  * properties common to multiple policies is faster. */
450 #define MAXMEMORY_FLAG_LRU (1<<0)
451 #define MAXMEMORY_FLAG_LFU (1<<1)
452 #define MAXMEMORY_FLAG_ALLKEYS (1<<2)
453 #define MAXMEMORY_FLAG_NO_SHARED_INTEGERS \
454     (MAXMEMORY_FLAG_LRU|MAXMEMORY_FLAG_LFU)
455 
456 #define MAXMEMORY_VOLATILE_LRU ((0<<8)|MAXMEMORY_FLAG_LRU)
457 #define MAXMEMORY_VOLATILE_LFU ((1<<8)|MAXMEMORY_FLAG_LFU)
458 #define MAXMEMORY_VOLATILE_TTL (2<<8)
459 #define MAXMEMORY_VOLATILE_RANDOM (3<<8)
460 #define MAXMEMORY_ALLKEYS_LRU ((4<<8)|MAXMEMORY_FLAG_LRU|MAXMEMORY_FLAG_ALLKEYS)
461 #define MAXMEMORY_ALLKEYS_LFU ((5<<8)|MAXMEMORY_FLAG_LFU|MAXMEMORY_FLAG_ALLKEYS)
462 #define MAXMEMORY_ALLKEYS_RANDOM ((6<<8)|MAXMEMORY_FLAG_ALLKEYS)
463 #define MAXMEMORY_NO_EVICTION (7<<8)
464 
465 /* Units */
466 #define UNIT_SECONDS 0
467 #define UNIT_MILLISECONDS 1
468 
469 /* SHUTDOWN flags */
470 #define SHUTDOWN_NOFLAGS 0      /* No flags. */
471 #define SHUTDOWN_SAVE 1         /* Force SAVE on SHUTDOWN even if no save
472                                    points are configured. */
473 #define SHUTDOWN_NOSAVE 2       /* Don't SAVE on SHUTDOWN. */
474 
475 /* Command call flags, see call() function */
476 #define CMD_CALL_NONE 0
477 #define CMD_CALL_SLOWLOG (1<<0)
478 #define CMD_CALL_STATS (1<<1)
479 #define CMD_CALL_PROPAGATE_AOF (1<<2)
480 #define CMD_CALL_PROPAGATE_REPL (1<<3)
481 #define CMD_CALL_PROPAGATE (CMD_CALL_PROPAGATE_AOF|CMD_CALL_PROPAGATE_REPL)
482 #define CMD_CALL_FULL (CMD_CALL_SLOWLOG | CMD_CALL_STATS | CMD_CALL_PROPAGATE)
483 #define CMD_CALL_NOWRAP (1<<4)  /* Don't wrap also propagate array into
484                                    MULTI/EXEC: the caller will handle it.  */
485 
486 /* Command propagation flags, see propagate() function */
487 #define PROPAGATE_NONE 0
488 #define PROPAGATE_AOF 1
489 #define PROPAGATE_REPL 2
490 
491 /* Client pause types, larger types are more restrictive
492  * pause types than smaller pause types. */
493 typedef enum {
494     CLIENT_PAUSE_OFF = 0, /* Pause no commands */
495     CLIENT_PAUSE_WRITE,   /* Pause write commands */
496     CLIENT_PAUSE_ALL      /* Pause all commands */
497 } pause_type;
498 
499 /* RDB active child save type. */
500 #define RDB_CHILD_TYPE_NONE 0
501 #define RDB_CHILD_TYPE_DISK 1     /* RDB is written to disk. */
502 #define RDB_CHILD_TYPE_SOCKET 2   /* RDB is written to slave socket. */
503 
504 /* Keyspace changes notification classes. Every class is associated with a
505  * character for configuration purposes. */
506 #define NOTIFY_KEYSPACE (1<<0)    /* K */
507 #define NOTIFY_KEYEVENT (1<<1)    /* E */
508 #define NOTIFY_GENERIC (1<<2)     /* g */
509 #define NOTIFY_STRING (1<<3)      /* $ */
510 #define NOTIFY_LIST (1<<4)        /* l */
511 #define NOTIFY_SET (1<<5)         /* s */
512 #define NOTIFY_HASH (1<<6)        /* h */
513 #define NOTIFY_ZSET (1<<7)        /* z */
514 #define NOTIFY_EXPIRED (1<<8)     /* x */
515 #define NOTIFY_EVICTED (1<<9)     /* e */
516 #define NOTIFY_STREAM (1<<10)     /* t */
517 #define NOTIFY_KEY_MISS (1<<11)   /* m (Note: This one is excluded from NOTIFY_ALL on purpose) */
518 #define NOTIFY_LOADED (1<<12)     /* module only key space notification, indicate a key loaded from rdb */
519 #define NOTIFY_MODULE (1<<13)     /* d, module key space notification */
520 #define NOTIFY_ALL (NOTIFY_GENERIC | NOTIFY_STRING | NOTIFY_LIST | NOTIFY_SET | NOTIFY_HASH | NOTIFY_ZSET | NOTIFY_EXPIRED | NOTIFY_EVICTED | NOTIFY_STREAM | NOTIFY_MODULE) /* A flag */
521 
522 /* Using the following macro you can run code inside serverCron() with the
523  * specified period, specified in milliseconds.
524  * The actual resolution depends on server.hz. */
525 #define run_with_period(_ms_) if ((_ms_ <= 1000/server.hz) || !(server.cronloops%((_ms_)/(1000/server.hz))))
526 
527 /* We can print the stacktrace, so our assert is defined this way: */
528 #define serverAssertWithInfo(_c,_o,_e) ((_e)?(void)0 : (_serverAssertWithInfo(_c,_o,#_e,__FILE__,__LINE__),redis_unreachable()))
529 #define serverAssert(_e) ((_e)?(void)0 : (_serverAssert(#_e,__FILE__,__LINE__),redis_unreachable()))
530 #define serverPanic(...) _serverPanic(__FILE__,__LINE__,__VA_ARGS__),redis_unreachable()
531 
532 /*-----------------------------------------------------------------------------
533  * Data types
534  *----------------------------------------------------------------------------*/
535 
536 /* A redis object, that is a type able to hold a string / list / set */
537 
538 /* The actual Redis Object */
539 #define OBJ_STRING 0    /* String object. */
540 #define OBJ_LIST 1      /* List object. */
541 #define OBJ_SET 2       /* Set object. */
542 #define OBJ_ZSET 3      /* Sorted set object. */
543 #define OBJ_HASH 4      /* Hash object. */
544 
545 /* The "module" object type is a special one that signals that the object
546  * is one directly managed by a Redis module. In this case the value points
547  * to a moduleValue struct, which contains the object value (which is only
548  * handled by the module itself) and the RedisModuleType struct which lists
549  * function pointers in order to serialize, deserialize, AOF-rewrite and
550  * free the object.
551  *
552  * Inside the RDB file, module types are encoded as OBJ_MODULE followed
553  * by a 64 bit module type ID, which has a 54 bits module-specific signature
554  * in order to dispatch the loading to the right module, plus a 10 bits
555  * encoding version. */
556 #define OBJ_MODULE 5    /* Module object. */
557 #define OBJ_STREAM 6    /* Stream object. */
558 
559 /* Extract encver / signature from a module type ID. */
560 #define REDISMODULE_TYPE_ENCVER_BITS 10
561 #define REDISMODULE_TYPE_ENCVER_MASK ((1<<REDISMODULE_TYPE_ENCVER_BITS)-1)
562 #define REDISMODULE_TYPE_ENCVER(id) (id & REDISMODULE_TYPE_ENCVER_MASK)
563 #define REDISMODULE_TYPE_SIGN(id) ((id & ~((uint64_t)REDISMODULE_TYPE_ENCVER_MASK)) >>REDISMODULE_TYPE_ENCVER_BITS)
564 
565 /* Bit flags for moduleTypeAuxSaveFunc */
566 #define REDISMODULE_AUX_BEFORE_RDB (1<<0)
567 #define REDISMODULE_AUX_AFTER_RDB (1<<1)
568 
569 struct RedisModule;
570 struct RedisModuleIO;
571 struct RedisModuleDigest;
572 struct RedisModuleCtx;
573 struct moduleLoadQueueEntry;
574 struct redisObject;
575 struct RedisModuleDefragCtx;
576 struct RedisModuleInfoCtx;
577 struct RedisModuleKeyOptCtx;
578 
579 /* Each module type implementation should export a set of methods in order
580  * to serialize and deserialize the value in the RDB file, rewrite the AOF
581  * log, create the digest for "DEBUG DIGEST", and free the value when a key
582  * is deleted. */
583 typedef void *(*moduleTypeLoadFunc)(struct RedisModuleIO *io, int encver);
584 typedef void (*moduleTypeSaveFunc)(struct RedisModuleIO *io, void *value);
585 typedef int (*moduleTypeAuxLoadFunc)(struct RedisModuleIO *rdb, int encver, int when);
586 typedef void (*moduleTypeAuxSaveFunc)(struct RedisModuleIO *rdb, int when);
587 typedef void (*moduleTypeRewriteFunc)(struct RedisModuleIO *io, struct redisObject *key, void *value);
588 typedef void (*moduleTypeDigestFunc)(struct RedisModuleDigest *digest, void *value);
589 typedef size_t (*moduleTypeMemUsageFunc)(const void *value);
590 typedef void (*moduleTypeFreeFunc)(void *value);
591 typedef size_t (*moduleTypeFreeEffortFunc)(struct redisObject *key, const void *value);
592 typedef void (*moduleTypeUnlinkFunc)(struct redisObject *key, void *value);
593 typedef void *(*moduleTypeCopyFunc)(struct redisObject *fromkey, struct redisObject *tokey, const void *value);
594 typedef int (*moduleTypeDefragFunc)(struct RedisModuleDefragCtx *ctx, struct redisObject *key, void **value);
595 typedef void (*RedisModuleInfoFunc)(struct RedisModuleInfoCtx *ctx, int for_crash_report);
596 typedef void (*RedisModuleDefragFunc)(struct RedisModuleDefragCtx *ctx);
597 typedef size_t (*moduleTypeMemUsageFunc2)(struct RedisModuleKeyOptCtx *ctx, const void *value, size_t sample_size);
598 typedef void (*moduleTypeFreeFunc2)(struct RedisModuleKeyOptCtx *ctx, void *value);
599 typedef size_t (*moduleTypeFreeEffortFunc2)(struct RedisModuleKeyOptCtx *ctx, const void *value);
600 typedef void (*moduleTypeUnlinkFunc2)(struct RedisModuleKeyOptCtx *ctx, void *value);
601 typedef void *(*moduleTypeCopyFunc2)(struct RedisModuleKeyOptCtx *ctx, const void *value);
602 
603 /* This callback type is called by moduleNotifyUserChanged() every time
604  * a user authenticated via the module API is associated with a different
605  * user or gets disconnected. This needs to be exposed since you can't cast
606  * a function pointer to (void *). */
607 typedef void (*RedisModuleUserChangedFunc) (uint64_t client_id, void *privdata);
608 
609 
610 /* The module type, which is referenced in each value of a given type, defines
611  * the methods and links to the module exporting the type. */
612 typedef struct RedisModuleType {
613     uint64_t id; /* Higher 54 bits of type ID + 10 lower bits of encoding ver. */
614     struct RedisModule *module;
615     moduleTypeLoadFunc rdb_load;
616     moduleTypeSaveFunc rdb_save;
617     moduleTypeRewriteFunc aof_rewrite;
618     moduleTypeMemUsageFunc mem_usage;
619     moduleTypeDigestFunc digest;
620     moduleTypeFreeFunc free;
621     moduleTypeFreeEffortFunc free_effort;
622     moduleTypeUnlinkFunc unlink;
623     moduleTypeCopyFunc copy;
624     moduleTypeDefragFunc defrag;
625     moduleTypeAuxLoadFunc aux_load;
626     moduleTypeAuxSaveFunc aux_save;
627     moduleTypeMemUsageFunc2 mem_usage2;
628     moduleTypeFreeEffortFunc2 free_effort2;
629     moduleTypeUnlinkFunc2 unlink2;
630     moduleTypeCopyFunc2 copy2;
631     int aux_save_triggers;
632     char name[10]; /* 9 bytes name + null term. Charset: A-Z a-z 0-9 _- */
633 } moduleType;
634 
635 /* In Redis objects 'robj' structures of type OBJ_MODULE, the value pointer
636  * is set to the following structure, referencing the moduleType structure
637  * in order to work with the value, and at the same time providing a raw
638  * pointer to the value, as created by the module commands operating with
639  * the module type.
640  *
641  * So for example in order to free such a value, it is possible to use
642  * the following code:
643  *
644  *  if (robj->type == OBJ_MODULE) {
645  *      moduleValue *mt = robj->ptr;
646  *      mt->type->free(mt->value);
647  *      zfree(mt); // We need to release this in-the-middle struct as well.
648  *  }
649  */
650 typedef struct moduleValue {
651     moduleType *type;
652     void *value;
653 } moduleValue;
654 
655 /* This structure represents a module inside the system. */
656 struct RedisModule {
657     void *handle;   /* Module dlopen() handle. */
658     char *name;     /* Module name. */
659     int ver;        /* Module version. We use just progressive integers. */
660     int apiver;     /* Module API version as requested during initialization.*/
661     list *types;    /* Module data types. */
662     list *usedby;   /* List of modules using APIs from this one. */
663     list *using;    /* List of modules we use some APIs of. */
664     list *filters;  /* List of filters the module has registered. */
665     int in_call;    /* RM_Call() nesting level */
666     int in_hook;    /* Hooks callback nesting level for this module (0 or 1). */
667     int options;    /* Module options and capabilities. */
668     int blocked_clients;         /* Count of RedisModuleBlockedClient in this module. */
669     RedisModuleInfoFunc info_cb; /* Callback for module to add INFO fields. */
670     RedisModuleDefragFunc defrag_cb;    /* Callback for global data defrag. */
671     struct moduleLoadQueueEntry *loadmod; /* Module load arguments for config rewrite. */
672 };
673 typedef struct RedisModule RedisModule;
674 
675 /* This is a wrapper for the 'rio' streams used inside rdb.c in Redis, so that
676  * the user does not have to take the total count of the written bytes nor
677  * to care about error conditions. */
678 typedef struct RedisModuleIO {
679     size_t bytes;       /* Bytes read / written so far. */
680     rio *rio;           /* Rio stream. */
681     moduleType *type;   /* Module type doing the operation. */
682     int error;          /* True if error condition happened. */
683     int ver;            /* Module serialization version: 1 (old),
684                          * 2 (current version with opcodes annotation). */
685     struct RedisModuleCtx *ctx; /* Optional context, see RM_GetContextFromIO()*/
686     struct redisObject *key;    /* Optional name of key processed */
687     int dbid;            /* The dbid of the key being processed, -1 when unknown. */
688 } RedisModuleIO;
689 
690 /* Macro to initialize an IO context. Note that the 'ver' field is populated
691  * inside rdb.c according to the version of the value to load. */
692 #define moduleInitIOContext(iovar,mtype,rioptr,keyptr,db) do { \
693     iovar.rio = rioptr; \
694     iovar.type = mtype; \
695     iovar.bytes = 0; \
696     iovar.error = 0; \
697     iovar.ver = 0; \
698     iovar.key = keyptr; \
699     iovar.dbid = db; \
700     iovar.ctx = NULL; \
701 } while(0)
702 
703 /* This is a structure used to export DEBUG DIGEST capabilities to Redis
704  * modules. We want to capture both the ordered and unordered elements of
705  * a data structure, so that a digest can be created in a way that correctly
706  * reflects the values. See the DEBUG DIGEST command implementation for more
707  * background. */
708 typedef struct RedisModuleDigest {
709     unsigned char o[20];    /* Ordered elements. */
710     unsigned char x[20];    /* Xored elements. */
711     struct redisObject *key; /* Optional name of key processed */
712     int dbid;                /* The dbid of the key being processed */
713 } RedisModuleDigest;
714 
715 /* Just start with a digest composed of all zero bytes. */
716 #define moduleInitDigestContext(mdvar) do { \
717     memset(mdvar.o,0,sizeof(mdvar.o)); \
718     memset(mdvar.x,0,sizeof(mdvar.x)); \
719 } while(0)
720 
721 /* Objects encoding. Some kind of objects like Strings and Hashes can be
722  * internally represented in multiple ways. The 'encoding' field of the object
723  * is set to one of this fields for this object. */
724 #define OBJ_ENCODING_RAW 0     /* Raw representation */
725 #define OBJ_ENCODING_INT 1     /* Encoded as integer */
726 #define OBJ_ENCODING_HT 2      /* Encoded as hash table */
727 #define OBJ_ENCODING_ZIPMAP 3  /* Encoded as zipmap */
728 #define OBJ_ENCODING_LINKEDLIST 4 /* No longer used: old list encoding. */
729 #define OBJ_ENCODING_ZIPLIST 5 /* Encoded as ziplist */
730 #define OBJ_ENCODING_INTSET 6  /* Encoded as intset */
731 #define OBJ_ENCODING_SKIPLIST 7  /* Encoded as skiplist */
732 #define OBJ_ENCODING_EMBSTR 8  /* Embedded sds string encoding */
733 #define OBJ_ENCODING_QUICKLIST 9 /* Encoded as linked list of listpacks */
734 #define OBJ_ENCODING_STREAM 10 /* Encoded as a radix tree of listpacks */
735 #define OBJ_ENCODING_LISTPACK 11 /* Encoded as a listpack */
736 
737 #define LRU_BITS 24
738 #define LRU_CLOCK_MAX ((1<<LRU_BITS)-1) /* Max value of obj->lru */
739 #define LRU_CLOCK_RESOLUTION 1000 /* LRU clock resolution in ms */
740 
741 #define OBJ_SHARED_REFCOUNT INT_MAX     /* Global object never destroyed. */
742 #define OBJ_STATIC_REFCOUNT (INT_MAX-1) /* Object allocated in the stack. */
743 #define OBJ_FIRST_SPECIAL_REFCOUNT OBJ_STATIC_REFCOUNT
744 typedef struct redisObject {
745     unsigned type:4;
746     unsigned encoding:4;
747     unsigned lru:LRU_BITS; /* LRU time (relative to global lru_clock) or
748                             * LFU data (least significant 8 bits frequency
749                             * and most significant 16 bits access time). */
750     int refcount;
751     void *ptr;
752 } robj;
753 
754 /* The a string name for an object's type as listed above
755  * Native types are checked against the OBJ_STRING, OBJ_LIST, OBJ_* defines,
756  * and Module types have their registered name returned. */
757 char *getObjectTypeName(robj*);
758 
759 /* Macro used to initialize a Redis object allocated on the stack.
760  * Note that this macro is taken near the structure definition to make sure
761  * we'll update it when the structure is changed, to avoid bugs like
762  * bug #85 introduced exactly in this way. */
763 #define initStaticStringObject(_var,_ptr) do { \
764     _var.refcount = OBJ_STATIC_REFCOUNT; \
765     _var.type = OBJ_STRING; \
766     _var.encoding = OBJ_ENCODING_RAW; \
767     _var.ptr = _ptr; \
768 } while(0)
769 
770 struct evictionPoolEntry; /* Defined in evict.c */
771 
772 /* This structure is used in order to represent the output buffer of a client,
773  * which is actually a linked list of blocks like that, that is: client->reply. */
774 typedef struct clientReplyBlock {
775     size_t size, used;
776     char buf[];
777 } clientReplyBlock;
778 
779 /* Replication buffer blocks is the list of replBufBlock.
780  *
781  * +--------------+       +--------------+       +--------------+
782  * | refcount = 1 |  ...  | refcount = 0 |  ...  | refcount = 2 |
783  * +--------------+       +--------------+       +--------------+
784  *      |                                            /       \
785  *      |                                           /         \
786  *      |                                          /           \
787  *  Repl Backlog                               Replia_A      Replia_B
788  *
789  * Each replica or replication backlog increments only the refcount of the
790  * 'ref_repl_buf_node' which it points to. So when replica walks to the next
791  * node, it should first increase the next node's refcount, and when we trim
792  * the replication buffer nodes, we remove node always from the head node which
793  * refcount is 0. If the refcount of the head node is not 0, we must stop
794  * trimming and never iterate the next node. */
795 
796 /* Similar with 'clientReplyBlock', it is used for shared buffers between
797  * all replica clients and replication backlog. */
798 typedef struct replBufBlock {
799     int refcount;           /* Number of replicas or repl backlog using. */
800     long long id;           /* The unique incremental number. */
801     long long repl_offset;  /* Start replication offset of the block. */
802     size_t size, used;
803     char buf[];
804 } replBufBlock;
805 
806 /* Opaque type for the Slot to Key API. */
807 typedef struct clusterSlotToKeyMapping clusterSlotToKeyMapping;
808 
809 /* Redis database representation. There are multiple databases identified
810  * by integers from 0 (the default database) up to the max configured
811  * database. The database number is the 'id' field in the structure. */
812 typedef struct redisDb {
813     dict *dict;                 /* The keyspace for this DB */
814     dict *expires;              /* Timeout of keys with a timeout set */
815     dict *blocking_keys;        /* Keys with clients waiting for data (BLPOP)*/
816     dict *ready_keys;           /* Blocked keys that received a PUSH */
817     dict *watched_keys;         /* WATCHED keys for MULTI/EXEC CAS */
818     int id;                     /* Database ID */
819     long long avg_ttl;          /* Average TTL, just for stats */
820     unsigned long expires_cursor; /* Cursor of the active expire cycle. */
821     list *defrag_later;         /* List of key names to attempt to defrag one by one, gradually. */
822     clusterSlotToKeyMapping *slots_to_keys; /* Array of slots to keys. Only used in cluster mode (db 0). */
823 } redisDb;
824 
825 /* Client MULTI/EXEC state */
826 typedef struct multiCmd {
827     robj **argv;
828     int argv_len;
829     int argc;
830     struct redisCommand *cmd;
831 } multiCmd;
832 
833 typedef struct multiState {
834     multiCmd *commands;     /* Array of MULTI commands */
835     int count;              /* Total number of MULTI commands */
836     int cmd_flags;          /* The accumulated command flags OR-ed together.
837                                So if at least a command has a given flag, it
838                                will be set in this field. */
839     int cmd_inv_flags;      /* Same as cmd_flags, OR-ing the ~flags. so that it
840                                is possible to know if all the commands have a
841                                certain flag. */
842     size_t argv_len_sums;    /* mem used by all commands arguments */
843 } multiState;
844 
845 /* This structure holds the blocking operation state for a client.
846  * The fields used depend on client->btype. */
847 typedef struct blockingState {
848     /* Generic fields. */
849     long count;             /* Elements to pop if count was specified (BLMPOP/BZMPOP), -1 otherwise. */
850     mstime_t timeout;       /* Blocking operation timeout. If UNIX current time
851                              * is > timeout then the operation timed out. */
852 
853     /* BLOCKED_LIST, BLOCKED_ZSET and BLOCKED_STREAM */
854     dict *keys;             /* The keys we are waiting to terminate a blocking
855                              * operation such as BLPOP or XREAD. Or NULL. */
856     robj *target;           /* The key that should receive the element,
857                              * for BLMOVE. */
858     struct blockPos {
859         int wherefrom;      /* Where to pop from */
860         int whereto;        /* Where to push to */
861     } blockpos;              /* The positions in the src/dst lists/zsets
862                              * where we want to pop/push an element
863                              * for BLPOP, BRPOP, BLMOVE and BZMPOP. */
864 
865     /* BLOCK_STREAM */
866     size_t xread_count;     /* XREAD COUNT option. */
867     robj *xread_group;      /* XREADGROUP group name. */
868     robj *xread_consumer;   /* XREADGROUP consumer name. */
869     int xread_group_noack;
870 
871     /* BLOCKED_WAIT */
872     int numreplicas;        /* Number of replicas we are waiting for ACK. */
873     long long reploffset;   /* Replication offset to reach. */
874 
875     /* BLOCKED_MODULE */
876     void *module_blocked_handle; /* RedisModuleBlockedClient structure.
877                                     which is opaque for the Redis core, only
878                                     handled in module.c. */
879 } blockingState;
880 
881 /* The following structure represents a node in the server.ready_keys list,
882  * where we accumulate all the keys that had clients blocked with a blocking
883  * operation such as B[LR]POP, but received new data in the context of the
884  * last executed command.
885  *
886  * After the execution of every command or script, we run this list to check
887  * if as a result we should serve data to clients blocked, unblocking them.
888  * Note that server.ready_keys will not have duplicates as there dictionary
889  * also called ready_keys in every structure representing a Redis database,
890  * where we make sure to remember if a given key was already added in the
891  * server.ready_keys list. */
892 typedef struct readyList {
893     redisDb *db;
894     robj *key;
895 } readyList;
896 
897 /* This structure represents a Redis user. This is useful for ACLs, the
898  * user is associated to the connection after the connection is authenticated.
899  * If there is no associated user, the connection uses the default user. */
900 #define USER_COMMAND_BITS_COUNT 1024    /* The total number of command bits
901                                            in the user structure. The last valid
902                                            command ID we can set in the user
903                                            is USER_COMMAND_BITS_COUNT-1. */
904 #define USER_FLAG_ENABLED (1<<0)        /* The user is active. */
905 #define USER_FLAG_DISABLED (1<<1)       /* The user is disabled. */
906 #define USER_FLAG_ALLKEYS (1<<2)        /* The user can mention any key. */
907 #define USER_FLAG_ALLCOMMANDS (1<<3)    /* The user can run all commands. */
908 #define USER_FLAG_NOPASS      (1<<4)    /* The user requires no password, any
909                                            provided password will work. For the
910                                            default user, this also means that
911                                            no AUTH is needed, and every
912                                            connection is immediately
913                                            authenticated. */
914 #define USER_FLAG_ALLCHANNELS (1<<5)    /* The user can mention any Pub/Sub
915                                            channel. */
916 #define USER_FLAG_SANITIZE_PAYLOAD (1<<6)       /* The user require a deep RESTORE
917                                                  * payload sanitization. */
918 #define USER_FLAG_SANITIZE_PAYLOAD_SKIP (1<<7)  /* The user should skip the
919                                                  * deep sanitization of RESTORE
920                                                  * payload. */
921 
922 typedef struct {
923     sds name;       /* The username as an SDS string. */
924     uint64_t flags; /* See USER_FLAG_* */
925 
926     /* The bit in allowed_commands is set if this user has the right to
927      * execute this command.
928      *
929      * If the bit for a given command is NOT set and the command has
930      * allowed first-args, Redis will also check allowed_firstargs in order to
931      * understand if the command can be executed. */
932     uint64_t allowed_commands[USER_COMMAND_BITS_COUNT/64];
933 
934     /* allowed_firstargs is used by ACL rules to block access to a command unless a
935      * specific argv[1] is given (or argv[2] in case it is applied on a sub-command).
936      * For example, a user can use the rule "-select +select|0" to block all
937      * SELECT commands, except "SELECT 0".
938      * And for a sub-command: "+config -config|set +config|set|loglevel"
939      *
940      * For each command ID (corresponding to the command bit set in allowed_commands),
941      * This array points to an array of SDS strings, terminated by a NULL pointer,
942      * with all the first-args that are allowed for this command. When no first-arg
943      * matching is used, the field is just set to NULL to avoid allocating
944      * USER_COMMAND_BITS_COUNT pointers. */
945     sds **allowed_firstargs;
946     list *passwords; /* A list of SDS valid passwords for this user. */
947     list *patterns;  /* A list of allowed key patterns. If this field is NULL
948                         the user cannot mention any key in a command, unless
949                         the flag ALLKEYS is set in the user. */
950     list *channels;  /* A list of allowed Pub/Sub channel patterns. If this
951                         field is NULL the user cannot mention any channel in a
952                         `PUBLISH` or [P][UNSUBSCRIBE] command, unless the flag
953                         ALLCHANNELS is set in the user. */
954 } user;
955 
956 /* With multiplexing we need to take per-client state.
957  * Clients are taken in a linked list. */
958 
959 #define CLIENT_ID_AOF (UINT64_MAX) /* Reserved ID for the AOF client. If you
960                                       need more reserved IDs use UINT64_MAX-1,
961                                       -2, ... and so forth. */
962 
963 /* Replication backlog is not separate memory, it just is one consumer of
964  * the global replication buffer. This structure records the reference of
965  * replication buffers. Since the replication buffer block list may be very long,
966  * it would cost much time to search replication offset on partial resync, so
967  * we use one rax tree to index some blocks every REPL_BACKLOG_INDEX_PER_BLOCKS
968  * to make searching offset from replication buffer blocks list faster. */
969 typedef struct replBacklog {
970     listNode *ref_repl_buf_node; /* Referenced node of replication buffer blocks,
971                                   * see the definition of replBufBlock. */
972     size_t unindexed_count;      /* The count from last creating index block. */
973     rax *blocks_index;           /* The index of reocrded blocks of replication
974                                   * buffer for quickly searching replication
975                                   * offset on partial resynchronization. */
976     long long histlen;           /* Backlog actual data length */
977     long long offset;            /* Replication "master offset" of first
978                                   * byte in the replication backlog buffer.*/
979 } replBacklog;
980 
981 typedef struct {
982     list *clients;
983     size_t mem_usage_sum;
984 } clientMemUsageBucket;
985 
986 typedef struct client {
987     uint64_t id;            /* Client incremental unique ID. */
988     connection *conn;
989     int resp;               /* RESP protocol version. Can be 2 or 3. */
990     redisDb *db;            /* Pointer to currently SELECTed DB. */
991     robj *name;             /* As set by CLIENT SETNAME. */
992     sds querybuf;           /* Buffer we use to accumulate client queries. */
993     size_t qb_pos;          /* The position we have read in querybuf. */
994     sds pending_querybuf;   /* If this client is flagged as master, this buffer
995                                represents the yet not applied portion of the
996                                replication stream that we are receiving from
997                                the master. */
998     size_t querybuf_peak;   /* Recent (100ms or more) peak of querybuf size. */
999     int argc;               /* Num of arguments of current command. */
1000     robj **argv;            /* Arguments of current command. */
1001     int argv_len;           /* Size of argv array (may be more than argc) */
1002     int original_argc;      /* Num of arguments of original command if arguments were rewritten. */
1003     robj **original_argv;   /* Arguments of original command if arguments were rewritten. */
1004     size_t argv_len_sum;    /* Sum of lengths of objects in argv list. */
1005     struct redisCommand *cmd, *lastcmd;  /* Last command executed. */
1006     user *user;             /* User associated with this connection. If the
1007                                user is set to NULL the connection can do
1008                                anything (admin). */
1009     int reqtype;            /* Request protocol type: PROTO_REQ_* */
1010     int multibulklen;       /* Number of multi bulk arguments left to read. */
1011     long bulklen;           /* Length of bulk argument in multi bulk request. */
1012     list *reply;            /* List of reply objects to send to the client. */
1013     unsigned long long reply_bytes; /* Tot bytes of objects in reply list. */
1014     size_t sentlen;         /* Amount of bytes already sent in the current
1015                                buffer or object being sent. */
1016     time_t ctime;           /* Client creation time. */
1017     long duration;          /* Current command duration. Used for measuring latency of blocking/non-blocking cmds */
1018     time_t lastinteraction; /* Time of the last interaction, used for timeout */
1019     time_t obuf_soft_limit_reached_time;
1020     uint64_t flags;         /* Client flags: CLIENT_* macros. */
1021     int authenticated;      /* Needed when the default user requires auth. */
1022     int replstate;          /* Replication state if this is a slave. */
1023     int repl_put_online_on_ack; /* Install slave write handler on first ACK. */
1024     int repldbfd;           /* Replication DB file descriptor. */
1025     off_t repldboff;        /* Replication DB file offset. */
1026     off_t repldbsize;       /* Replication DB file size. */
1027     sds replpreamble;       /* Replication DB preamble. */
1028     long long read_reploff; /* Read replication offset if this is a master. */
1029     long long reploff;      /* Applied replication offset if this is a master. */
1030     long long repl_ack_off; /* Replication ack offset, if this is a slave. */
1031     long long repl_ack_time;/* Replication ack time, if this is a slave. */
1032     long long repl_last_partial_write; /* The last time the server did a partial write from the RDB child pipe to this replica  */
1033     long long psync_initial_offset; /* FULLRESYNC reply offset other slaves
1034                                        copying this slave output buffer
1035                                        should use. */
1036     char replid[CONFIG_RUN_ID_SIZE+1]; /* Master replication ID (if master). */
1037     int slave_listening_port; /* As configured with: REPLCONF listening-port */
1038     char *slave_addr;       /* Optionally given by REPLCONF ip-address */
1039     int slave_capa;         /* Slave capabilities: SLAVE_CAPA_* bitwise OR. */
1040     multiState mstate;      /* MULTI/EXEC state */
1041     int btype;              /* Type of blocking op if CLIENT_BLOCKED. */
1042     blockingState bpop;     /* blocking state */
1043     long long woff;         /* Last write global replication offset. */
1044     list *watched_keys;     /* Keys WATCHED for MULTI/EXEC CAS */
1045     dict *pubsub_channels;  /* channels a client is interested in (SUBSCRIBE) */
1046     list *pubsub_patterns;  /* patterns a client is interested in (SUBSCRIBE) */
1047     sds peerid;             /* Cached peer ID. */
1048     sds sockname;           /* Cached connection target address. */
1049     listNode *client_list_node; /* list node in client list */
1050     listNode *paused_list_node; /* list node within the pause list */
1051     listNode *pending_read_list_node; /* list node in clients pending read list */
1052     RedisModuleUserChangedFunc auth_callback; /* Module callback to execute
1053                                                * when the authenticated user
1054                                                * changes. */
1055     void *auth_callback_privdata; /* Private data that is passed when the auth
1056                                    * changed callback is executed. Opaque for
1057                                    * Redis Core. */
1058     void *auth_module;      /* The module that owns the callback, which is used
1059                              * to disconnect the client if the module is
1060                              * unloaded for cleanup. Opaque for Redis Core.*/
1061 
1062     /* If this client is in tracking mode and this field is non zero,
1063      * invalidation messages for keys fetched by this client will be send to
1064      * the specified client ID. */
1065     uint64_t client_tracking_redirection;
1066     rax *client_tracking_prefixes; /* A dictionary of prefixes we are already
1067                                       subscribed to in BCAST mode, in the
1068                                       context of client side caching. */
1069     /* In updateClientMemUsage() we track the memory usage of
1070      * each client and add it to the sum of all the clients of a given type,
1071      * however we need to remember what was the old contribution of each
1072      * client, and in which category the client was, in order to remove it
1073      * before adding it the new value. */
1074     size_t last_memory_usage;
1075     int last_memory_type;
1076 
1077     size_t last_memory_usage_on_bucket_update;
1078     listNode *mem_usage_bucket_node;
1079     clientMemUsageBucket *mem_usage_bucket;
1080 
1081     listNode *ref_repl_buf_node; /* Referenced node of replication buffer blocks,
1082                                   * see the definition of replBufBlock. */
1083     size_t ref_block_pos;        /* Access position of referenced buffer block,
1084                                   * i.e. the next offset to send. */
1085 
1086     /* Response buffer */
1087     int bufpos;
1088     size_t buf_usable_size; /* Usable size of buffer. */
1089     /* Note that 'buf' must be the last field of client struct, because memory
1090      * allocator may give us more memory than our apply for reducing fragments,
1091      * but we want to make full use of given memory, i.e. we may access the
1092      * memory after 'buf'. To avoid make others fields corrupt, 'buf' must be
1093      * the last one. */
1094     char buf[PROTO_REPLY_CHUNK_BYTES];
1095 } client;
1096 
1097 struct saveparam {
1098     time_t seconds;
1099     int changes;
1100 };
1101 
1102 struct moduleLoadQueueEntry {
1103     sds path;
1104     int argc;
1105     robj **argv;
1106 };
1107 
1108 struct sentinelLoadQueueEntry {
1109     int argc;
1110     sds *argv;
1111     int linenum;
1112     sds line;
1113 };
1114 
1115 struct sentinelConfig {
1116     list *pre_monitor_cfg;
1117     list *monitor_cfg;
1118     list *post_monitor_cfg;
1119 };
1120 
1121 struct sharedObjectsStruct {
1122     robj *crlf, *ok, *err, *emptybulk, *czero, *cone, *pong, *space,
1123     *queued, *null[4], *nullarray[4], *emptymap[4], *emptyset[4],
1124     *emptyarray, *wrongtypeerr, *nokeyerr, *syntaxerr, *sameobjecterr,
1125     *outofrangeerr, *noscripterr, *loadingerr, *slowscripterr, *bgsaveerr,
1126     *masterdownerr, *roslaveerr, *execaborterr, *noautherr, *noreplicaserr,
1127     *busykeyerr, *oomerr, *plus, *messagebulk, *pmessagebulk, *subscribebulk,
1128     *unsubscribebulk, *psubscribebulk, *punsubscribebulk, *del, *unlink,
1129     *rpop, *lpop, *lpush, *rpoplpush, *lmove, *blmove, *zpopmin, *zpopmax,
1130     *emptyscan, *multi, *exec, *left, *right, *hset, *srem, *xgroup, *xclaim,
1131     *script, *replconf, *eval, *persist, *set, *pexpireat, *pexpire,
1132     *time, *pxat, *absttl, *retrycount, *force, *justid,
1133     *lastid, *ping, *setid, *keepttl, *load, *createconsumer,
1134     *getack, *special_asterick, *special_equals, *default_username, *redacted,
1135     *select[PROTO_SHARED_SELECT_CMDS],
1136     *integers[OBJ_SHARED_INTEGERS],
1137     *mbulkhdr[OBJ_SHARED_BULKHDR_LEN], /* "*<value>\r\n" */
1138     *bulkhdr[OBJ_SHARED_BULKHDR_LEN];  /* "$<value>\r\n" */
1139     sds minstring, maxstring;
1140 };
1141 
1142 /* ZSETs use a specialized version of Skiplists */
1143 typedef struct zskiplistNode {
1144     sds ele;
1145     double score;
1146     struct zskiplistNode *backward;
1147     struct zskiplistLevel {
1148         struct zskiplistNode *forward;
1149         unsigned long span;
1150     } level[];
1151 } zskiplistNode;
1152 
1153 typedef struct zskiplist {
1154     struct zskiplistNode *header, *tail;
1155     unsigned long length;
1156     int level;
1157 } zskiplist;
1158 
1159 typedef struct zset {
1160     dict *dict;
1161     zskiplist *zsl;
1162 } zset;
1163 
1164 typedef struct clientBufferLimitsConfig {
1165     unsigned long long hard_limit_bytes;
1166     unsigned long long soft_limit_bytes;
1167     time_t soft_limit_seconds;
1168 } clientBufferLimitsConfig;
1169 
1170 extern clientBufferLimitsConfig clientBufferLimitsDefaults[CLIENT_TYPE_OBUF_COUNT];
1171 
1172 /* The redisOp structure defines a Redis Operation, that is an instance of
1173  * a command with an argument vector, database ID, propagation target
1174  * (PROPAGATE_*), and command pointer.
1175  *
1176  * Currently only used to additionally propagate more commands to AOF/Replication
1177  * after the propagation of the executed command. */
1178 typedef struct redisOp {
1179     robj **argv;
1180     int argc, dbid, target;
1181 } redisOp;
1182 
1183 /* Defines an array of Redis operations. There is an API to add to this
1184  * structure in an easy way.
1185  *
1186  * redisOpArrayInit();
1187  * redisOpArrayAppend();
1188  * redisOpArrayFree();
1189  */
1190 typedef struct redisOpArray {
1191     redisOp *ops;
1192     int numops;
1193 } redisOpArray;
1194 
1195 /* This structure is returned by the getMemoryOverheadData() function in
1196  * order to return memory overhead information. */
1197 struct redisMemOverhead {
1198     size_t peak_allocated;
1199     size_t total_allocated;
1200     size_t startup_allocated;
1201     size_t repl_backlog;
1202     size_t clients_slaves;
1203     size_t clients_normal;
1204     size_t aof_buffer;
1205     size_t lua_caches;
1206     size_t overhead_total;
1207     size_t dataset;
1208     size_t total_keys;
1209     size_t bytes_per_key;
1210     float dataset_perc;
1211     float peak_perc;
1212     float total_frag;
1213     ssize_t total_frag_bytes;
1214     float allocator_frag;
1215     ssize_t allocator_frag_bytes;
1216     float allocator_rss;
1217     ssize_t allocator_rss_bytes;
1218     float rss_extra;
1219     size_t rss_extra_bytes;
1220     size_t num_dbs;
1221     struct {
1222         size_t dbid;
1223         size_t overhead_ht_main;
1224         size_t overhead_ht_expires;
1225     } *db;
1226 };
1227 
1228 /* This structure can be optionally passed to RDB save/load functions in
1229  * order to implement additional functionalities, by storing and loading
1230  * metadata to the RDB file.
1231  *
1232  * For example, to use select a DB at load time, useful in
1233  * replication in order to make sure that chained slaves (slaves of slaves)
1234  * select the correct DB and are able to accept the stream coming from the
1235  * top-level master. */
1236 typedef struct rdbSaveInfo {
1237     /* Used saving and loading. */
1238     int repl_stream_db;  /* DB to select in server.master client. */
1239 
1240     /* Used only loading. */
1241     int repl_id_is_set;  /* True if repl_id field is set. */
1242     char repl_id[CONFIG_RUN_ID_SIZE+1];     /* Replication ID. */
1243     long long repl_offset;                  /* Replication offset. */
1244 } rdbSaveInfo;
1245 
1246 #define RDB_SAVE_INFO_INIT {-1,0,"0000000000000000000000000000000000000000",-1}
1247 
1248 struct malloc_stats {
1249     size_t zmalloc_used;
1250     size_t process_rss;
1251     size_t allocator_allocated;
1252     size_t allocator_active;
1253     size_t allocator_resident;
1254 };
1255 
1256 typedef struct socketFds {
1257     int fd[CONFIG_BINDADDR_MAX];
1258     int count;
1259 } socketFds;
1260 
1261 /*-----------------------------------------------------------------------------
1262  * TLS Context Configuration
1263  *----------------------------------------------------------------------------*/
1264 
1265 typedef struct redisTLSContextConfig {
1266     char *cert_file;                /* Server side and optionally client side cert file name */
1267     char *key_file;                 /* Private key filename for cert_file */
1268     char *key_file_pass;            /* Optional password for key_file */
1269     char *client_cert_file;         /* Certificate to use as a client; if none, use cert_file */
1270     char *client_key_file;          /* Private key filename for client_cert_file */
1271     char *client_key_file_pass;     /* Optional password for client_key_file */
1272     char *dh_params_file;
1273     char *ca_cert_file;
1274     char *ca_cert_dir;
1275     char *protocols;
1276     char *ciphers;
1277     char *ciphersuites;
1278     int prefer_server_ciphers;
1279     int session_caching;
1280     int session_cache_size;
1281     int session_cache_timeout;
1282 } redisTLSContextConfig;
1283 
1284 /*-----------------------------------------------------------------------------
1285  * Global server state
1286  *----------------------------------------------------------------------------*/
1287 
1288 /* AIX defines hz to __hz, we don't use this define and in order to allow
1289  * Redis build on AIX we need to undef it. */
1290 #ifdef _AIX
1291 #undef hz
1292 #endif
1293 
1294 #define CHILD_TYPE_NONE 0
1295 #define CHILD_TYPE_RDB 1
1296 #define CHILD_TYPE_AOF 2
1297 #define CHILD_TYPE_LDB 3
1298 #define CHILD_TYPE_MODULE 4
1299 
1300 typedef enum childInfoType {
1301     CHILD_INFO_TYPE_CURRENT_INFO,
1302     CHILD_INFO_TYPE_AOF_COW_SIZE,
1303     CHILD_INFO_TYPE_RDB_COW_SIZE,
1304     CHILD_INFO_TYPE_MODULE_COW_SIZE
1305 } childInfoType;
1306 
1307 struct redisServer {
1308     /* General */
1309     pid_t pid;                  /* Main process pid. */
1310     pthread_t main_thread_id;         /* Main thread id */
1311     char *configfile;           /* Absolute config file path, or NULL */
1312     char *executable;           /* Absolute executable file path. */
1313     char **exec_argv;           /* Executable argv vector (copy). */
1314     int dynamic_hz;             /* Change hz value depending on # of clients. */
1315     int config_hz;              /* Configured HZ value. May be different than
1316                                    the actual 'hz' field value if dynamic-hz
1317                                    is enabled. */
1318     mode_t umask;               /* The umask value of the process on startup */
1319     int hz;                     /* serverCron() calls frequency in hertz */
1320     int in_fork_child;          /* indication that this is a fork child */
1321     redisDb *db;
1322     dict *commands;             /* Command table */
1323     dict *orig_commands;        /* Command table before command renaming. */
1324     aeEventLoop *el;
1325     rax *errors;                /* Errors table */
1326     redisAtomic unsigned int lruclock; /* Clock for LRU eviction */
1327     volatile sig_atomic_t shutdown_asap; /* SHUTDOWN needed ASAP */
1328     int activerehashing;        /* Incremental rehash in serverCron() */
1329     int active_defrag_running;  /* Active defragmentation running (holds current scan aggressiveness) */
1330     char *pidfile;              /* PID file path */
1331     int arch_bits;              /* 32 or 64 depending on sizeof(long) */
1332     int cronloops;              /* Number of times the cron function run */
1333     char runid[CONFIG_RUN_ID_SIZE+1];  /* ID always different at every exec. */
1334     int sentinel_mode;          /* True if this instance is a Sentinel. */
1335     size_t initial_memory_usage; /* Bytes used after initialization. */
1336     int always_show_logo;       /* Show logo even for non-stdout logging. */
1337     int in_eval;                /* Are we inside EVAL? */
1338     int in_exec;                /* Are we inside EXEC? */
1339     int propagate_in_transaction;  /* Make sure we don't propagate nested MULTI/EXEC */
1340     char *ignore_warnings;      /* Config: warnings that should be ignored. */
1341     int client_pause_in_transaction; /* Was a client pause executed during this Exec? */
1342     int thp_enabled;                 /* If true, THP is enabled. */
1343     size_t page_size;                /* The page size of OS. */
1344     /* Modules */
1345     dict *moduleapi;            /* Exported core APIs dictionary for modules. */
1346     dict *sharedapi;            /* Like moduleapi but containing the APIs that
1347                                    modules share with each other. */
1348     list *loadmodule_queue;     /* List of modules to load at startup. */
1349     int module_blocked_pipe[2]; /* Pipe used to awake the event loop if a
1350                                    client blocked on a module command needs
1351                                    to be processed. */
1352     pid_t child_pid;            /* PID of current child */
1353     int child_type;             /* Type of current child */
1354     client *module_client;      /* "Fake" client to call Redis from modules */
1355     /* Networking */
1356     int port;                   /* TCP listening port */
1357     int tls_port;               /* TLS listening port */
1358     int tcp_backlog;            /* TCP listen() backlog */
1359     char *bindaddr[CONFIG_BINDADDR_MAX]; /* Addresses we should bind to */
1360     int bindaddr_count;         /* Number of addresses in server.bindaddr[] */
1361     char *bind_source_addr;     /* Source address to bind on for outgoing connections */
1362     char *unixsocket;           /* UNIX socket path */
1363     unsigned int unixsocketperm; /* UNIX socket permission (see mode_t) */
1364     socketFds ipfd;             /* TCP socket file descriptors */
1365     socketFds tlsfd;            /* TLS socket file descriptors */
1366     int sofd;                   /* Unix socket file descriptor */
1367     socketFds cfd;              /* Cluster bus listening socket */
1368     list *clients;              /* List of active clients */
1369     list *clients_to_close;     /* Clients to close asynchronously */
1370     list *clients_pending_write; /* There is to write or install handler. */
1371     list *clients_pending_read;  /* Client has pending read socket buffers. */
1372     list *slaves, *monitors;    /* List of slaves and MONITORs */
1373     client *current_client;     /* Current client executing the command. */
1374 
1375     /* Stuff for client mem eviction */
1376     clientMemUsageBucket client_mem_usage_buckets[CLIENT_MEM_USAGE_BUCKETS];
1377 
1378     rax *clients_timeout_table; /* Radix tree for blocked clients timeouts. */
1379     long fixed_time_expire;     /* If > 0, expire keys against server.mstime. */
1380     int in_nested_call;         /* If > 0, in a nested call of a call */
1381     rax *clients_index;         /* Active clients dictionary by client ID. */
1382     pause_type client_pause_type;      /* True if clients are currently paused */
1383     list *paused_clients;       /* List of pause clients */
1384     mstime_t client_pause_end_time;    /* Time when we undo clients_paused */
1385     char neterr[ANET_ERR_LEN];   /* Error buffer for anet.c */
1386     dict *migrate_cached_sockets;/* MIGRATE cached sockets */
1387     redisAtomic uint64_t next_client_id; /* Next client unique ID. Incremental. */
1388     int protected_mode;         /* Don't accept external connections. */
1389     int io_threads_num;         /* Number of IO threads to use. */
1390     int io_threads_do_reads;    /* Read and parse from IO threads? */
1391     int io_threads_active;      /* Is IO threads currently active? */
1392     long long events_processed_while_blocked; /* processEventsWhileBlocked() */
1393 
1394     /* RDB / AOF loading information */
1395     volatile sig_atomic_t loading; /* We are loading data from disk if true */
1396     volatile sig_atomic_t async_loading; /* We are loading data without blocking the db being served */
1397     off_t loading_total_bytes;
1398     off_t loading_rdb_used_mem;
1399     off_t loading_loaded_bytes;
1400     time_t loading_start_time;
1401     off_t loading_process_events_interval_bytes;
1402     /* Fields used only for stats */
1403     time_t stat_starttime;          /* Server start time */
1404     long long stat_numcommands;     /* Number of processed commands */
1405     long long stat_numconnections;  /* Number of connections received */
1406     long long stat_expiredkeys;     /* Number of expired keys */
1407     double stat_expired_stale_perc; /* Percentage of keys probably expired */
1408     long long stat_expired_time_cap_reached_count; /* Early expire cycle stops.*/
1409     long long stat_expire_cycle_time_used; /* Cumulative microseconds used. */
1410     long long stat_evictedkeys;     /* Number of evicted keys (maxmemory) */
1411     long long stat_evictedclients;  /* Number of evicted clients */
1412     long long stat_total_eviction_exceeded_time;  /* Total time over the memory limit, unit us */
1413     monotime stat_last_eviction_exceeded_time;  /* Timestamp of current eviction start, unit us */
1414     long long stat_keyspace_hits;   /* Number of successful lookups of keys */
1415     long long stat_keyspace_misses; /* Number of failed lookups of keys */
1416     long long stat_active_defrag_hits;      /* number of allocations moved */
1417     long long stat_active_defrag_misses;    /* number of allocations scanned but not moved */
1418     long long stat_active_defrag_key_hits;  /* number of keys with moved allocations */
1419     long long stat_active_defrag_key_misses;/* number of keys scanned and not moved */
1420     long long stat_active_defrag_scanned;   /* number of dictEntries scanned */
1421     long long stat_total_active_defrag_time; /* Total time memory fragmentation over the limit, unit us */
1422     monotime stat_last_active_defrag_time; /* Timestamp of current active defrag start */
1423     size_t stat_peak_memory;        /* Max used memory record */
1424     long long stat_fork_time;       /* Time needed to perform latest fork() */
1425     double stat_fork_rate;          /* Fork rate in GB/sec. */
1426     long long stat_total_forks;     /* Total count of fork. */
1427     long long stat_rejected_conn;   /* Clients rejected because of maxclients */
1428     long long stat_sync_full;       /* Number of full resyncs with slaves. */
1429     long long stat_sync_partial_ok; /* Number of accepted PSYNC requests. */
1430     long long stat_sync_partial_err;/* Number of unaccepted PSYNC requests. */
1431     list *slowlog;                  /* SLOWLOG list of commands */
1432     long long slowlog_entry_id;     /* SLOWLOG current entry ID */
1433     long long slowlog_log_slower_than; /* SLOWLOG time limit (to get logged) */
1434     unsigned long slowlog_max_len;     /* SLOWLOG max number of items logged */
1435     struct malloc_stats cron_malloc_stats; /* sampled in serverCron(). */
1436     redisAtomic long long stat_net_input_bytes; /* Bytes read from network. */
1437     redisAtomic long long stat_net_output_bytes; /* Bytes written to network. */
1438     size_t stat_current_cow_peak;   /* Peak size of copy on write bytes. */
1439     size_t stat_current_cow_bytes;  /* Copy on write bytes while child is active. */
1440     monotime stat_current_cow_updated;  /* Last update time of stat_current_cow_bytes */
1441     size_t stat_current_save_keys_processed;  /* Processed keys while child is active. */
1442     size_t stat_current_save_keys_total;  /* Number of keys when child started. */
1443     size_t stat_rdb_cow_bytes;      /* Copy on write bytes during RDB saving. */
1444     size_t stat_aof_cow_bytes;      /* Copy on write bytes during AOF rewrite. */
1445     size_t stat_module_cow_bytes;   /* Copy on write bytes during module fork. */
1446     double stat_module_progress;   /* Module save progress. */
1447     redisAtomic size_t stat_clients_type_memory[CLIENT_TYPE_COUNT];/* Mem usage by type */
1448     long long stat_unexpected_error_replies; /* Number of unexpected (aof-loading, replica to master, etc.) error replies */
1449     long long stat_total_error_replies; /* Total number of issued error replies ( command + rejected errors ) */
1450     long long stat_dump_payload_sanitizations; /* Number deep dump payloads integrity validations. */
1451     long long stat_io_reads_processed; /* Number of read events processed by IO / Main threads */
1452     long long stat_io_writes_processed; /* Number of write events processed by IO / Main threads */
1453     redisAtomic long long stat_total_reads_processed; /* Total number of read events processed */
1454     redisAtomic long long stat_total_writes_processed; /* Total number of write events processed */
1455     /* The following two are used to track instantaneous metrics, like
1456      * number of operations per second, network traffic. */
1457     struct {
1458         long long last_sample_time; /* Timestamp of last sample in ms */
1459         long long last_sample_count;/* Count in last sample */
1460         long long samples[STATS_METRIC_SAMPLES];
1461         int idx;
1462     } inst_metric[STATS_METRIC_COUNT];
1463     /* Configuration */
1464     int verbosity;                  /* Loglevel in redis.conf */
1465     int maxidletime;                /* Client timeout in seconds */
1466     int tcpkeepalive;               /* Set SO_KEEPALIVE if non-zero. */
1467     int active_expire_enabled;      /* Can be disabled for testing purposes. */
1468     int active_expire_effort;       /* From 1 (default) to 10, active effort. */
1469     int active_defrag_enabled;
1470     int sanitize_dump_payload;      /* Enables deep sanitization for ziplist and listpack in RDB and RESTORE. */
1471     int skip_checksum_validation;   /* Disable checksum validation for RDB and RESTORE payload. */
1472     int jemalloc_bg_thread;         /* Enable jemalloc background thread */
1473     size_t active_defrag_ignore_bytes; /* minimum amount of fragmentation waste to start active defrag */
1474     int active_defrag_threshold_lower; /* minimum percentage of fragmentation to start active defrag */
1475     int active_defrag_threshold_upper; /* maximum percentage of fragmentation at which we use maximum effort */
1476     int active_defrag_cycle_min;       /* minimal effort for defrag in CPU percentage */
1477     int active_defrag_cycle_max;       /* maximal effort for defrag in CPU percentage */
1478     unsigned long active_defrag_max_scan_fields; /* maximum number of fields of set/hash/zset/list to process from within the main dict scan */
1479     size_t client_max_querybuf_len; /* Limit for client query buffer length */
1480     int dbnum;                      /* Total number of configured DBs */
1481     int supervised;                 /* 1 if supervised, 0 otherwise. */
1482     int supervised_mode;            /* See SUPERVISED_* */
1483     int daemonize;                  /* True if running as a daemon */
1484     int set_proc_title;             /* True if change proc title */
1485     char *proc_title_template;      /* Process title template format */
1486     clientBufferLimitsConfig client_obuf_limits[CLIENT_TYPE_OBUF_COUNT];
1487     int pause_cron;                 /* Don't run cron tasks (debug) */
1488     /* AOF persistence */
1489     int aof_enabled;                /* AOF configuration */
1490     int aof_state;                  /* AOF_(ON|OFF|WAIT_REWRITE) */
1491     int aof_fsync;                  /* Kind of fsync() policy */
1492     char *aof_filename;             /* Name of the AOF file */
1493     int aof_no_fsync_on_rewrite;    /* Don't fsync if a rewrite is in prog. */
1494     int aof_rewrite_perc;           /* Rewrite AOF if % growth is > M and... */
1495     off_t aof_rewrite_min_size;     /* the AOF file is at least N bytes. */
1496     off_t aof_rewrite_base_size;    /* AOF size on latest startup or rewrite. */
1497     off_t aof_current_size;         /* AOF current size. */
1498     off_t aof_fsync_offset;         /* AOF offset which is already synced to disk. */
1499     int aof_flush_sleep;            /* Micros to sleep before flush. (used by tests) */
1500     int aof_rewrite_scheduled;      /* Rewrite once BGSAVE terminates. */
1501     list *aof_rewrite_buf_blocks;   /* Hold changes during an AOF rewrite. */
1502     sds aof_buf;      /* AOF buffer, written before entering the event loop */
1503     int aof_fd;       /* File descriptor of currently selected AOF file */
1504     int aof_selected_db; /* Currently selected DB in AOF */
1505     time_t aof_flush_postponed_start; /* UNIX time of postponed AOF flush */
1506     time_t aof_last_fsync;            /* UNIX time of last fsync() */
1507     time_t aof_rewrite_time_last;   /* Time used by last AOF rewrite run. */
1508     time_t aof_rewrite_time_start;  /* Current AOF rewrite start time. */
1509     time_t aof_cur_timestamp;       /* Current record timestamp in AOF */
1510     int aof_timestamp_enabled;      /* Enable record timestamp in AOF */
1511     int aof_lastbgrewrite_status;   /* C_OK or C_ERR */
1512     unsigned long aof_delayed_fsync;  /* delayed AOF fsync() counter */
1513     int aof_rewrite_incremental_fsync;/* fsync incrementally while aof rewriting? */
1514     int rdb_save_incremental_fsync;   /* fsync incrementally while rdb saving? */
1515     int aof_last_write_status;      /* C_OK or C_ERR */
1516     int aof_last_write_errno;       /* Valid if aof write/fsync status is ERR */
1517     int aof_load_truncated;         /* Don't stop on unexpected AOF EOF. */
1518     int aof_use_rdb_preamble;       /* Use RDB preamble on AOF rewrites. */
1519     redisAtomic int aof_bio_fsync_status; /* Status of AOF fsync in bio job. */
1520     redisAtomic int aof_bio_fsync_errno;  /* Errno of AOF fsync in bio job. */
1521     /* AOF pipes used to communicate between parent and child during rewrite. */
1522     int aof_pipe_write_data_to_child;
1523     int aof_pipe_read_data_from_parent;
1524     int aof_pipe_write_ack_to_parent;
1525     int aof_pipe_read_ack_from_child;
1526     int aof_pipe_write_ack_to_child;
1527     int aof_pipe_read_ack_from_parent;
1528     int aof_stop_sending_diff;     /* If true stop sending accumulated diffs
1529                                       to child process. */
1530     sds aof_child_diff;             /* AOF diff accumulator child side. */
1531     /* RDB persistence */
1532     long long dirty;                /* Changes to DB from the last save */
1533     long long dirty_before_bgsave;  /* Used to restore dirty on failed BGSAVE */
1534     long long rdb_last_load_keys_expired;  /* number of expired keys when loading RDB */
1535     long long rdb_last_load_keys_loaded;   /* number of loaded keys when loading RDB */
1536     struct saveparam *saveparams;   /* Save points array for RDB */
1537     int saveparamslen;              /* Number of saving points */
1538     char *rdb_filename;             /* Name of RDB file */
1539     int rdb_compression;            /* Use compression in RDB? */
1540     int rdb_checksum;               /* Use RDB checksum? */
1541     int rdb_del_sync_files;         /* Remove RDB files used only for SYNC if
1542                                        the instance does not use persistence. */
1543     time_t lastsave;                /* Unix time of last successful save */
1544     time_t lastbgsave_try;          /* Unix time of last attempted bgsave */
1545     time_t rdb_save_time_last;      /* Time used by last RDB save run. */
1546     time_t rdb_save_time_start;     /* Current RDB save start time. */
1547     int rdb_bgsave_scheduled;       /* BGSAVE when possible if true. */
1548     int rdb_child_type;             /* Type of save by active child. */
1549     int lastbgsave_status;          /* C_OK or C_ERR */
1550     int stop_writes_on_bgsave_err;  /* Don't allow writes if can't BGSAVE */
1551     int rdb_pipe_read;              /* RDB pipe used to transfer the rdb data */
1552                                     /* to the parent process in diskless repl. */
1553     int rdb_child_exit_pipe;        /* Used by the diskless parent allow child exit. */
1554     connection **rdb_pipe_conns;    /* Connections which are currently the */
1555     int rdb_pipe_numconns;          /* target of diskless rdb fork child. */
1556     int rdb_pipe_numconns_writing;  /* Number of rdb conns with pending writes. */
1557     char *rdb_pipe_buff;            /* In diskless replication, this buffer holds data */
1558     int rdb_pipe_bufflen;           /* that was read from the the rdb pipe. */
1559     int rdb_key_save_delay;         /* Delay in microseconds between keys while
1560                                      * writing the RDB. (for testings). negative
1561                                      * value means fractions of microseconds (on average). */
1562     int key_load_delay;             /* Delay in microseconds between keys while
1563                                      * loading aof or rdb. (for testings). negative
1564                                      * value means fractions of microseconds (on average). */
1565     /* Pipe and data structures for child -> parent info sharing. */
1566     int child_info_pipe[2];         /* Pipe used to write the child_info_data. */
1567     int child_info_nread;           /* Num of bytes of the last read from pipe */
1568     /* Propagation of commands in AOF / replication */
1569     redisOpArray also_propagate;    /* Additional command to propagate. */
1570     int replication_allowed;        /* Are we allowed to replicate? */
1571     /* Logging */
1572     char *logfile;                  /* Path of log file */
1573     int syslog_enabled;             /* Is syslog enabled? */
1574     char *syslog_ident;             /* Syslog ident */
1575     int syslog_facility;            /* Syslog facility */
1576     int crashlog_enabled;           /* Enable signal handler for crashlog.
1577                                      * disable for clean core dumps. */
1578     int memcheck_enabled;           /* Enable memory check on crash. */
1579     int use_exit_on_panic;          /* Use exit() on panic and assert rather than
1580                                      * abort(). useful for Valgrind. */
1581     /* Replication (master) */
1582     char replid[CONFIG_RUN_ID_SIZE+1];  /* My current replication ID. */
1583     char replid2[CONFIG_RUN_ID_SIZE+1]; /* replid inherited from master*/
1584     long long master_repl_offset;   /* My current replication offset */
1585     long long second_replid_offset; /* Accept offsets up to this for replid2. */
1586     int slaveseldb;                 /* Last SELECTed DB in replication output */
1587     int repl_ping_slave_period;     /* Master pings the slave every N seconds */
1588     replBacklog *repl_backlog;      /* Replication backlog for partial syncs */
1589     long long repl_backlog_size;    /* Backlog circular buffer size */
1590     time_t repl_backlog_time_limit; /* Time without slaves after the backlog
1591                                        gets released. */
1592     time_t repl_no_slaves_since;    /* We have no slaves since that time.
1593                                        Only valid if server.slaves len is 0. */
1594     int repl_min_slaves_to_write;   /* Min number of slaves to write. */
1595     int repl_min_slaves_max_lag;    /* Max lag of <count> slaves to write. */
1596     int repl_good_slaves_count;     /* Number of slaves with lag <= max_lag. */
1597     int repl_diskless_sync;         /* Master send RDB to slaves sockets directly. */
1598     int repl_diskless_load;         /* Slave parse RDB directly from the socket.
1599                                      * see REPL_DISKLESS_LOAD_* enum */
1600     int repl_diskless_sync_delay;   /* Delay to start a diskless repl BGSAVE. */
1601     size_t repl_buffer_mem;         /* The memory of replication buffer. */
1602     list *repl_buffer_blocks;       /* Replication buffers blocks list
1603                                      * (serving replica clients and repl backlog) */
1604     /* Replication (slave) */
1605     char *masteruser;               /* AUTH with this user and masterauth with master */
1606     sds masterauth;                 /* AUTH with this password with master */
1607     char *masterhost;               /* Hostname of master */
1608     int masterport;                 /* Port of master */
1609     int repl_timeout;               /* Timeout after N seconds of master idle */
1610     client *master;     /* Client that is master for this slave */
1611     client *cached_master; /* Cached master to be reused for PSYNC. */
1612     int repl_syncio_timeout; /* Timeout for synchronous I/O calls */
1613     int repl_state;          /* Replication status if the instance is a slave */
1614     off_t repl_transfer_size; /* Size of RDB to read from master during sync. */
1615     off_t repl_transfer_read; /* Amount of RDB read from master during sync. */
1616     off_t repl_transfer_last_fsync_off; /* Offset when we fsync-ed last time. */
1617     connection *repl_transfer_s;     /* Slave -> Master SYNC connection */
1618     int repl_transfer_fd;    /* Slave -> Master SYNC temp file descriptor */
1619     char *repl_transfer_tmpfile; /* Slave-> master SYNC temp file name */
1620     time_t repl_transfer_lastio; /* Unix time of the latest read, for timeout */
1621     int repl_serve_stale_data; /* Serve stale data when link is down? */
1622     int repl_slave_ro;          /* Slave is read only? */
1623     int repl_slave_ignore_maxmemory;    /* If true slaves do not evict. */
1624     time_t repl_down_since; /* Unix time at which link with master went down */
1625     int repl_disable_tcp_nodelay;   /* Disable TCP_NODELAY after SYNC? */
1626     int slave_priority;             /* Reported in INFO and used by Sentinel. */
1627     int replica_announced;          /* If true, replica is announced by Sentinel */
1628     int slave_announce_port;        /* Give the master this listening port. */
1629     char *slave_announce_ip;        /* Give the master this ip address. */
1630     /* The following two fields is where we store master PSYNC replid/offset
1631      * while the PSYNC is in progress. At the end we'll copy the fields into
1632      * the server->master client structure. */
1633     char master_replid[CONFIG_RUN_ID_SIZE+1];  /* Master PSYNC runid. */
1634     long long master_initial_offset;           /* Master PSYNC offset. */
1635     int repl_slave_lazy_flush;          /* Lazy FLUSHALL before loading DB? */
1636     /* Replication script cache. */
1637     dict *repl_scriptcache_dict;        /* SHA1 all slaves are aware of. */
1638     list *repl_scriptcache_fifo;        /* First in, first out LRU eviction. */
1639     unsigned int repl_scriptcache_size; /* Max number of elements. */
1640     /* Synchronous replication. */
1641     list *clients_waiting_acks;         /* Clients waiting in WAIT command. */
1642     int get_ack_from_slaves;            /* If true we send REPLCONF GETACK. */
1643     /* Limits */
1644     unsigned int maxclients;            /* Max number of simultaneous clients */
1645     unsigned long long maxmemory;   /* Max number of memory bytes to use */
1646     ssize_t maxmemory_clients;       /* Memory limit for total client buffers */
1647     int maxmemory_policy;           /* Policy for key eviction */
1648     int maxmemory_samples;          /* Precision of random sampling */
1649     int maxmemory_eviction_tenacity;/* Aggressiveness of eviction processing */
1650     int lfu_log_factor;             /* LFU logarithmic counter factor. */
1651     int lfu_decay_time;             /* LFU counter decay factor. */
1652     long long proto_max_bulk_len;   /* Protocol bulk length maximum size. */
1653     int oom_score_adj_base;         /* Base oom_score_adj value, as observed on startup */
1654     int oom_score_adj_values[CONFIG_OOM_COUNT];   /* Linux oom_score_adj configuration */
1655     int oom_score_adj;                            /* If true, oom_score_adj is managed */
1656     int disable_thp;                              /* If true, disable THP by syscall */
1657     /* Blocked clients */
1658     unsigned int blocked_clients;   /* # of clients executing a blocking cmd.*/
1659     unsigned int blocked_clients_by_type[BLOCKED_NUM];
1660     list *unblocked_clients; /* list of clients to unblock before next loop */
1661     list *ready_keys;        /* List of readyList structures for BLPOP & co */
1662     /* Client side caching. */
1663     unsigned int tracking_clients;  /* # of clients with tracking enabled.*/
1664     size_t tracking_table_max_keys; /* Max number of keys in tracking table. */
1665     list *tracking_pending_keys; /* tracking invalidation keys pending to flush */
1666     /* Sort parameters - qsort_r() is only available under BSD so we
1667      * have to take this state global, in order to pass it to sortCompare() */
1668     int sort_desc;
1669     int sort_alpha;
1670     int sort_bypattern;
1671     int sort_store;
1672     /* Zip structure config, see redis.conf for more information  */
1673     size_t hash_max_listpack_entries;
1674     size_t hash_max_listpack_value;
1675     size_t set_max_intset_entries;
1676     size_t zset_max_listpack_entries;
1677     size_t zset_max_listpack_value;
1678     size_t hll_sparse_max_bytes;
1679     size_t stream_node_max_bytes;
1680     long long stream_node_max_entries;
1681     /* List parameters */
1682     int list_max_listpack_size;
1683     int list_compress_depth;
1684     /* time cache */
1685     redisAtomic time_t unixtime; /* Unix time sampled every cron cycle. */
1686     time_t timezone;            /* Cached timezone. As set by tzset(). */
1687     int daylight_active;        /* Currently in daylight saving time. */
1688     mstime_t mstime;            /* 'unixtime' in milliseconds. */
1689     ustime_t ustime;            /* 'unixtime' in microseconds. */
1690     size_t blocking_op_nesting; /* Nesting level of blocking operation, used to reset blocked_last_cron. */
1691     long long blocked_last_cron; /* Indicate the mstime of the last time we did cron jobs from a blocking operation */
1692     /* Pubsub */
1693     dict *pubsub_channels;  /* Map channels to list of subscribed clients */
1694     dict *pubsub_patterns;  /* A dict of pubsub_patterns */
1695     int notify_keyspace_events; /* Events to propagate via Pub/Sub. This is an
1696                                    xor of NOTIFY_... flags. */
1697     /* Cluster */
1698     int cluster_enabled;      /* Is cluster enabled? */
1699     int cluster_port;         /* Set the cluster port for a node. */
1700     mstime_t cluster_node_timeout; /* Cluster node timeout. */
1701     char *cluster_configfile; /* Cluster auto-generated config file name. */
1702     struct clusterState *cluster;  /* State of the cluster */
1703     int cluster_migration_barrier; /* Cluster replicas migration barrier. */
1704     int cluster_allow_replica_migration; /* Automatic replica migrations to orphaned masters and from empty masters */
1705     int cluster_slave_validity_factor; /* Slave max data age for failover. */
1706     int cluster_require_full_coverage; /* If true, put the cluster down if
1707                                           there is at least an uncovered slot.*/
1708     int cluster_slave_no_failover;  /* Prevent slave from starting a failover
1709                                        if the master is in failure state. */
1710     char *cluster_announce_ip;  /* IP address to announce on cluster bus. */
1711     int cluster_announce_port;     /* base port to announce on cluster bus. */
1712     int cluster_announce_tls_port; /* TLS port to announce on cluster bus. */
1713     int cluster_announce_bus_port; /* bus port to announce on cluster bus. */
1714     int cluster_module_flags;      /* Set of flags that Redis modules are able
1715                                       to set in order to suppress certain
1716                                       native Redis Cluster features. Check the
1717                                       REDISMODULE_CLUSTER_FLAG_*. */
1718     int cluster_allow_reads_when_down; /* Are reads allowed when the cluster
1719                                         is down? */
1720     int cluster_config_file_lock_fd;   /* cluster config fd, will be flock */
1721     /* Scripting */
1722     lua_State *lua; /* The Lua interpreter. We use just one for all clients */
1723     client *lua_client;   /* The "fake client" to query Redis from Lua */
1724     client *lua_caller;   /* The client running EVAL right now, or NULL */
1725     char* lua_cur_script; /* SHA1 of the script currently running, or NULL */
1726     dict *lua_scripts;         /* A dictionary of SHA1 -> Lua scripts */
1727     unsigned long long lua_scripts_mem;  /* Cached scripts' memory + oh */
1728     mstime_t lua_time_limit;  /* Script timeout in milliseconds */
1729     monotime lua_time_start;  /* monotonic timer to detect timed-out script */
1730     mstime_t lua_time_snapshot; /* Snapshot of mstime when script is started */
1731     int lua_write_dirty;  /* True if a write command was called during the
1732                              execution of the current script. */
1733     int lua_random_dirty; /* True if a random command was called during the
1734                              execution of the current script. */
1735     int lua_replicate_commands; /* True if we are doing single commands repl. */
1736     int lua_multi_emitted;/* True if we already propagated MULTI. */
1737     int lua_repl;         /* Script replication flags for redis.set_repl(). */
1738     int lua_timedout;     /* True if we reached the time limit for script
1739                              execution. */
1740     int lua_kill;         /* Kill the script if true. */
1741     int lua_always_replicate_commands; /* Default replication type. */
1742     int lua_oom;          /* OOM detected when script start? */
1743     int lua_disable_deny_script; /* Allow running commands marked "no-script" inside a script. */
1744     /* Lazy free */
1745     int lazyfree_lazy_eviction;
1746     int lazyfree_lazy_expire;
1747     int lazyfree_lazy_server_del;
1748     int lazyfree_lazy_user_del;
1749     int lazyfree_lazy_user_flush;
1750     /* Latency monitor */
1751     long long latency_monitor_threshold;
1752     dict *latency_events;
1753     /* ACLs */
1754     char *acl_filename;           /* ACL Users file. NULL if not configured. */
1755     unsigned long acllog_max_len; /* Maximum length of the ACL LOG list. */
1756     sds requirepass;              /* Remember the cleartext password set with
1757                                      the old "requirepass" directive for
1758                                      backward compatibility with Redis <= 5. */
1759     int acl_pubsub_default;      /* Default ACL pub/sub channels flag */
1760     /* Assert & bug reporting */
1761     int watchdog_period;  /* Software watchdog period in ms. 0 = off */
1762     /* System hardware info */
1763     size_t system_memory_size;  /* Total memory in system as reported by OS */
1764     /* TLS Configuration */
1765     int tls_cluster;
1766     int tls_replication;
1767     int tls_auth_clients;
1768     redisTLSContextConfig tls_ctx_config;
1769     /* cpu affinity */
1770     char *server_cpulist; /* cpu affinity list of redis server main/io thread. */
1771     char *bio_cpulist; /* cpu affinity list of bio thread. */
1772     char *aof_rewrite_cpulist; /* cpu affinity list of aof rewrite process. */
1773     char *bgsave_cpulist; /* cpu affinity list of bgsave process. */
1774     /* Sentinel config */
1775     struct sentinelConfig *sentinel_config; /* sentinel config to load at startup time. */
1776     /* Coordinate failover info */
1777     mstime_t failover_end_time; /* Deadline for failover command. */
1778     int force_failover; /* If true then failover will be forced at the
1779                          * deadline, otherwise failover is aborted. */
1780     char *target_replica_host; /* Failover target host. If null during a
1781                                 * failover then any replica can be used. */
1782     int target_replica_port; /* Failover target port */
1783     int failover_state; /* Failover state */
1784 };
1785 
1786 #define MAX_KEYS_BUFFER 256
1787 
1788 /* A result structure for the various getkeys function calls. It lists the
1789  * keys as indices to the provided argv.
1790  */
1791 typedef struct {
1792     int keysbuf[MAX_KEYS_BUFFER];       /* Pre-allocated buffer, to save heap allocations */
1793     int *keys;                          /* Key indices array, points to keysbuf or heap */
1794     int numkeys;                        /* Number of key indices return */
1795     int size;                           /* Available array size */
1796 } getKeysResult;
1797 #define GETKEYS_RESULT_INIT { {0}, NULL, 0, MAX_KEYS_BUFFER }
1798 
1799 /* Key specs definitions.
1800  *
1801  * Brief: This is a scheme that tries to describe the location
1802  * of key arguments better than the old [first,last,step] scheme
1803  * which is limited and doesn't fit many commands.
1804  *
1805  * There are two steps:
1806  * 1. begin_search (BS): in which index should we start seacrhing for keys?
1807  * 2. find_keys (FK): relative to the output of BS, how can we will which args are keys?
1808  *
1809  * There are two types of BS:
1810  * 1. index: key args start at a constant index
1811  * 2. keyword: key args start just after a specific keyword
1812  *
1813  * There are two kinds of FK:
1814  * 1. range: keys end at a specific index (or relative to the last argument)
1815  * 2. keynum: there's an arg that contains the number of key args somewhere before the keys themselves
1816  */
1817 
1818 typedef enum {
1819     KSPEC_BS_INVALID = 0, /* Must be 0 */
1820     KSPEC_BS_UNKNOWN,
1821     KSPEC_BS_INDEX,
1822     KSPEC_BS_KEYWORD
1823 } kspec_bs_type;
1824 
1825 typedef enum {
1826     KSPEC_FK_INVALID = 0, /* Must be 0 */
1827     KSPEC_FK_UNKNOWN,
1828     KSPEC_FK_RANGE,
1829     KSPEC_FK_KEYNUM
1830 } kspec_fk_type;
1831 
1832 typedef struct {
1833     /* Declarative data */
1834     const char *sflags;
1835     kspec_bs_type begin_search_type;
1836     union {
1837         struct {
1838             /* The index from which we start the search for keys */
1839             int pos;
1840         } index;
1841         struct {
1842             /* The keyword that indicates the beginning of key args */
1843             const char *keyword;
1844             /* An index in argv from which to start searching.
1845              * Can be negative, which means start search from the end, in reverse
1846              * (Example: -2 means to start in reverse from the panultimate arg) */
1847             int startfrom;
1848         } keyword;
1849     } bs;
1850     kspec_fk_type find_keys_type;
1851     union {
1852         /* NOTE: Indices in this struct are relative to the result of the begin_search step!
1853          * These are: range.lastkey, keynum.keynumidx, keynum.firstkey */
1854         struct {
1855             /* Index of the last key.
1856              * Can be negative, in which case it's not relative. -1 indicating till the last argument,
1857              * -2 one before the last and so on. */
1858             int lastkey;
1859             /* How many args should we skip after finding a key, in order to find the next one. */
1860             int keystep;
1861             /* If lastkey is -1, we use limit to stop the search by a factor. 0 and 1 mean no limit.
1862              * 2 means 1/2 of the remaining args, 3 means 1/3, and so on. */
1863             int limit;
1864         } range;
1865         struct {
1866             /* Index of the argument containing the number of keys to come */
1867             int keynumidx;
1868             /* Index of the fist key (Usually it's just after keynumidx, in
1869              * which case it should be set to keynumidx+1). */
1870             int firstkey;
1871             /* How many args should we skip after finding a key, in order to find the next one. */
1872             int keystep;
1873         } keynum;
1874     } fk;
1875 
1876     /* Runtime data */
1877     uint64_t flags;
1878 } keySpec;
1879 
1880 /* Number of static key specs */
1881 #define STATIC_KEY_SPECS_NUM 4
1882 
1883 typedef void redisCommandProc(client *c);
1884 typedef int redisGetKeysProc(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
1885 struct redisCommand {
1886     /* Declarative data */
1887     char *name;
1888     redisCommandProc *proc;
1889     int arity;
1890     char *sflags;   /* Flags as string representation, one char per flag. */
1891     keySpec key_specs_static[STATIC_KEY_SPECS_NUM];
1892     /* Use a function to determine keys arguments in a command line.
1893      * Used for Redis Cluster redirect (may be NULL) */
1894     redisGetKeysProc *getkeys_proc;
1895     /* Array of subcommands (may be NULL) */
1896     struct redisCommand *subcommands;
1897 
1898     /* Runtime data */
1899     uint64_t flags; /* The actual flags, obtained from the 'sflags' field. */
1900     /* What keys should be loaded in background when calling this command? */
1901     long long microseconds, calls, rejected_calls, failed_calls;
1902     int id;     /* Command ID. This is a progressive ID starting from 0 that
1903                    is assigned at runtime, and is used in order to check
1904                    ACLs. A connection is able to execute a given command if
1905                    the user associated to the connection has this command
1906                    bit set in the bitmap of allowed commands. */
1907     keySpec *key_specs;
1908     keySpec legacy_range_key_spec; /* The legacy (first,last,step) key spec is
1909                                      * still maintained (if applicable) so that
1910                                      * we can still support the reply format of
1911                                      * COMMAND INFO and COMMAND GETKEYS */
1912     int key_specs_num;
1913     int key_specs_max;
1914     int movablekeys; /* See populateCommandMovableKeys */
1915     dict *subcommands_dict;
1916     struct redisCommand *parent;
1917 };
1918 
1919 struct redisError {
1920     long long count;
1921 };
1922 
1923 struct redisFunctionSym {
1924     char *name;
1925     unsigned long pointer;
1926 };
1927 
1928 typedef struct _redisSortObject {
1929     robj *obj;
1930     union {
1931         double score;
1932         robj *cmpobj;
1933     } u;
1934 } redisSortObject;
1935 
1936 typedef struct _redisSortOperation {
1937     int type;
1938     robj *pattern;
1939 } redisSortOperation;
1940 
1941 /* Structure to hold list iteration abstraction. */
1942 typedef struct {
1943     robj *subject;
1944     unsigned char encoding;
1945     unsigned char direction; /* Iteration direction */
1946     quicklistIter *iter;
1947 } listTypeIterator;
1948 
1949 /* Structure for an entry while iterating over a list. */
1950 typedef struct {
1951     listTypeIterator *li;
1952     quicklistEntry entry; /* Entry in quicklist */
1953 } listTypeEntry;
1954 
1955 /* Structure to hold set iteration abstraction. */
1956 typedef struct {
1957     robj *subject;
1958     int encoding;
1959     int ii; /* intset iterator */
1960     dictIterator *di;
1961 } setTypeIterator;
1962 
1963 /* Structure to hold hash iteration abstraction. Note that iteration over
1964  * hashes involves both fields and values. Because it is possible that
1965  * not both are required, store pointers in the iterator to avoid
1966  * unnecessary memory allocation for fields/values. */
1967 typedef struct {
1968     robj *subject;
1969     int encoding;
1970 
1971     unsigned char *fptr, *vptr;
1972 
1973     dictIterator *di;
1974     dictEntry *de;
1975 } hashTypeIterator;
1976 
1977 #include "stream.h"  /* Stream data type header file. */
1978 
1979 #define OBJ_HASH_KEY 1
1980 #define OBJ_HASH_VALUE 2
1981 
1982 #define IO_THREADS_OP_IDLE 0
1983 #define IO_THREADS_OP_READ 1
1984 #define IO_THREADS_OP_WRITE 2
1985 extern int io_threads_op;
1986 
1987 /*-----------------------------------------------------------------------------
1988  * Extern declarations
1989  *----------------------------------------------------------------------------*/
1990 
1991 extern struct redisServer server;
1992 extern struct sharedObjectsStruct shared;
1993 extern dictType objectKeyPointerValueDictType;
1994 extern dictType objectKeyHeapPointerValueDictType;
1995 extern dictType setDictType;
1996 extern dictType zsetDictType;
1997 extern dictType dbDictType;
1998 extern dictType shaScriptObjectDictType;
1999 extern double R_Zero, R_PosInf, R_NegInf, R_Nan;
2000 extern dictType hashDictType;
2001 extern dictType replScriptCacheDictType;
2002 extern dictType dbExpiresDictType;
2003 extern dictType modulesDictType;
2004 extern dictType sdsReplyDictType;
2005 extern dict *modules;
2006 
2007 /*-----------------------------------------------------------------------------
2008  * Functions prototypes
2009  *----------------------------------------------------------------------------*/
2010 
2011 /* Key arguments specs */
2012 void populateCommandLegacyRangeSpec(struct redisCommand *c);
2013 
2014 /* Modules */
2015 void moduleInitModulesSystem(void);
2016 void moduleInitModulesSystemLast(void);
2017 int moduleLoad(const char *path, void **argv, int argc);
2018 void moduleLoadFromQueue(void);
2019 int moduleGetCommandKeysViaAPI(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
2020 moduleType *moduleTypeLookupModuleByID(uint64_t id);
2021 void moduleTypeNameByID(char *name, uint64_t moduleid);
2022 const char *moduleTypeModuleName(moduleType *mt);
2023 void moduleFreeContext(struct RedisModuleCtx *ctx);
2024 void unblockClientFromModule(client *c);
2025 void moduleHandleBlockedClients(void);
2026 void moduleBlockedClientTimedOut(client *c);
2027 void moduleBlockedClientPipeReadable(aeEventLoop *el, int fd, void *privdata, int mask);
2028 size_t moduleCount(void);
2029 void moduleAcquireGIL(void);
2030 int moduleTryAcquireGIL(void);
2031 void moduleReleaseGIL(void);
2032 void moduleNotifyKeyspaceEvent(int type, const char *event, robj *key, int dbid);
2033 void moduleCallCommandFilters(client *c);
2034 void ModuleForkDoneHandler(int exitcode, int bysignal);
2035 int TerminateModuleForkChild(int child_pid, int wait);
2036 ssize_t rdbSaveModulesAux(rio *rdb, int when);
2037 int moduleAllDatatypesHandleErrors();
2038 int moduleAllModulesHandleReplAsyncLoad();
2039 sds modulesCollectInfo(sds info, const char *section, int for_crash_report, int sections);
2040 void moduleFireServerEvent(uint64_t eid, int subid, void *data);
2041 void processModuleLoadingProgressEvent(int is_aof);
2042 int moduleTryServeClientBlockedOnKey(client *c, robj *key);
2043 void moduleUnblockClient(client *c);
2044 int moduleBlockedClientMayTimeout(client *c);
2045 int moduleClientIsBlockedOnKeys(client *c);
2046 void moduleNotifyUserChanged(client *c);
2047 void moduleNotifyKeyUnlink(robj *key, robj *val, int dbid);
2048 size_t moduleGetFreeEffort(robj *key, robj *val, int dbid);
2049 size_t moduleGetMemUsage(robj *key, robj *val, size_t sample_size, int dbid);
2050 robj *moduleTypeDupOrReply(client *c, robj *fromkey, robj *tokey, int todb, robj *value);
2051 int moduleDefragValue(robj *key, robj *obj, long *defragged, int dbid);
2052 int moduleLateDefrag(robj *key, robj *value, unsigned long *cursor, long long endtime, long long *defragged, int dbid);
2053 long moduleDefragGlobals(void);
2054 void *moduleGetHandleByName(char *modulename);
2055 int moduleIsModuleCommand(void *module_handle, struct redisCommand *cmd);
2056 
2057 /* Utils */
2058 long long ustime(void);
2059 long long mstime(void);
2060 void getRandomHexChars(char *p, size_t len);
2061 void getRandomBytes(unsigned char *p, size_t len);
2062 uint64_t crc64(uint64_t crc, const unsigned char *s, uint64_t l);
2063 void exitFromChild(int retcode);
2064 long long redisPopcount(void *s, long count);
2065 int redisSetProcTitle(char *title);
2066 int validateProcTitleTemplate(const char *template);
2067 int redisCommunicateSystemd(const char *sd_notify_msg);
2068 void redisSetCpuAffinity(const char *cpulist);
2069 
2070 /* networking.c -- Networking and Client related operations */
2071 client *createClient(connection *conn);
2072 void freeClient(client *c);
2073 void freeClientAsync(client *c);
2074 int beforeNextClient(client *c);
2075 void resetClient(client *c);
2076 void freeClientOriginalArgv(client *c);
2077 void sendReplyToClient(connection *conn);
2078 void *addReplyDeferredLen(client *c);
2079 void setDeferredArrayLen(client *c, void *node, long length);
2080 void setDeferredMapLen(client *c, void *node, long length);
2081 void setDeferredSetLen(client *c, void *node, long length);
2082 void setDeferredAttributeLen(client *c, void *node, long length);
2083 void setDeferredPushLen(client *c, void *node, long length);
2084 int processInputBuffer(client *c);
2085 void acceptTcpHandler(aeEventLoop *el, int fd, void *privdata, int mask);
2086 void acceptTLSHandler(aeEventLoop *el, int fd, void *privdata, int mask);
2087 void acceptUnixHandler(aeEventLoop *el, int fd, void *privdata, int mask);
2088 void readQueryFromClient(connection *conn);
2089 int prepareClientToWrite(client *c);
2090 void addReplyNull(client *c);
2091 void addReplyNullArray(client *c);
2092 void addReplyBool(client *c, int b);
2093 void addReplyVerbatim(client *c, const char *s, size_t len, const char *ext);
2094 void addReplyProto(client *c, const char *s, size_t len);
2095 void AddReplyFromClient(client *c, client *src);
2096 void addReplyBulk(client *c, robj *obj);
2097 void addReplyBulkCString(client *c, const char *s);
2098 void addReplyBulkCBuffer(client *c, const void *p, size_t len);
2099 void addReplyBulkLongLong(client *c, long long ll);
2100 void addReply(client *c, robj *obj);
2101 void addReplySds(client *c, sds s);
2102 void addReplyBulkSds(client *c, sds s);
2103 void setDeferredReplyBulkSds(client *c, void *node, sds s);
2104 void addReplyErrorObject(client *c, robj *err);
2105 void addReplyOrErrorObject(client *c, robj *reply);
2106 void addReplyErrorSds(client *c, sds err);
2107 void addReplyError(client *c, const char *err);
2108 void addReplyStatus(client *c, const char *status);
2109 void addReplyDouble(client *c, double d);
2110 void addReplyLongLongWithPrefix(client *c, long long ll, char prefix);
2111 void addReplyBigNum(client *c, const char* num, size_t len);
2112 void addReplyHumanLongDouble(client *c, long double d);
2113 void addReplyLongLong(client *c, long long ll);
2114 void addReplyArrayLen(client *c, long length);
2115 void addReplyMapLen(client *c, long length);
2116 void addReplySetLen(client *c, long length);
2117 void addReplyAttributeLen(client *c, long length);
2118 void addReplyPushLen(client *c, long length);
2119 void addReplyHelp(client *c, const char **help);
2120 void addReplySubcommandSyntaxError(client *c);
2121 void addReplyLoadedModules(client *c);
2122 void copyReplicaOutputBuffer(client *dst, client *src);
2123 void addListRangeReply(client *c, robj *o, long start, long end, int reverse);
2124 size_t sdsZmallocSize(sds s);
2125 size_t getStringObjectSdsUsedMemory(robj *o);
2126 void freeClientReplyValue(void *o);
2127 void *dupClientReplyValue(void *o);
2128 char *getClientPeerId(client *client);
2129 char *getClientSockName(client *client);
2130 sds catClientInfoString(sds s, client *client);
2131 sds getAllClientsInfoString(int type);
2132 void rewriteClientCommandVector(client *c, int argc, ...);
2133 void rewriteClientCommandArgument(client *c, int i, robj *newval);
2134 void replaceClientCommandVector(client *c, int argc, robj **argv);
2135 void redactClientCommandArgument(client *c, int argc);
2136 size_t getClientOutputBufferMemoryUsage(client *c);
2137 size_t getClientMemoryUsage(client *c, size_t *output_buffer_mem_usage);
2138 int freeClientsInAsyncFreeQueue(void);
2139 int closeClientOnOutputBufferLimitReached(client *c, int async);
2140 int getClientType(client *c);
2141 int getClientTypeByName(char *name);
2142 char *getClientTypeName(int class);
2143 void flushSlavesOutputBuffers(void);
2144 void disconnectSlaves(void);
2145 void evictClients(void);
2146 int listenToPort(int port, socketFds *fds);
2147 void pauseClients(mstime_t duration, pause_type type);
2148 void unpauseClients(void);
2149 int areClientsPaused(void);
2150 int checkClientPauseTimeoutAndReturnIfPaused(void);
2151 void processEventsWhileBlocked(void);
2152 void whileBlockedCron();
2153 void blockingOperationStarts();
2154 void blockingOperationEnds();
2155 int handleClientsWithPendingWrites(void);
2156 int handleClientsWithPendingWritesUsingThreads(void);
2157 int handleClientsWithPendingReadsUsingThreads(void);
2158 int stopThreadedIOIfNeeded(void);
2159 int clientHasPendingReplies(client *c);
2160 int updateClientMemUsage(client *c);
2161 void updateClientMemUsageBucket(client *c);
2162 void unlinkClient(client *c);
2163 int writeToClient(client *c, int handler_installed);
2164 void linkClient(client *c);
2165 void protectClient(client *c);
2166 void unprotectClient(client *c);
2167 void initThreadedIO(void);
2168 client *lookupClientByID(uint64_t id);
2169 int authRequired(client *c);
2170 
2171 #ifdef __GNUC__
2172 void addReplyErrorFormat(client *c, const char *fmt, ...)
2173     __attribute__((format(printf, 2, 3)));
2174 void addReplyStatusFormat(client *c, const char *fmt, ...)
2175     __attribute__((format(printf, 2, 3)));
2176 #else
2177 void addReplyErrorFormat(client *c, const char *fmt, ...);
2178 void addReplyStatusFormat(client *c, const char *fmt, ...);
2179 #endif
2180 
2181 /* Client side caching (tracking mode) */
2182 void enableTracking(client *c, uint64_t redirect_to, uint64_t options, robj **prefix, size_t numprefix);
2183 void disableTracking(client *c);
2184 void trackingRememberKeys(client *c);
2185 void trackingInvalidateKey(client *c, robj *keyobj, int bcast);
2186 void trackingScheduleKeyInvalidation(uint64_t client_id, robj *keyobj);
2187 void trackingHandlePendingKeyInvalidations(void);
2188 void trackingInvalidateKeysOnFlush(int async);
2189 void freeTrackingRadixTree(rax *rt);
2190 void freeTrackingRadixTreeAsync(rax *rt);
2191 void trackingLimitUsedSlots(void);
2192 uint64_t trackingGetTotalItems(void);
2193 uint64_t trackingGetTotalKeys(void);
2194 uint64_t trackingGetTotalPrefixes(void);
2195 void trackingBroadcastInvalidationMessages(void);
2196 int checkPrefixCollisionsOrReply(client *c, robj **prefix, size_t numprefix);
2197 
2198 /* List data type */
2199 void listTypePush(robj *subject, robj *value, int where);
2200 robj *listTypePop(robj *subject, int where);
2201 unsigned long listTypeLength(const robj *subject);
2202 listTypeIterator *listTypeInitIterator(robj *subject, long index, unsigned char direction);
2203 void listTypeReleaseIterator(listTypeIterator *li);
2204 void listTypeSetIteratorDirection(listTypeIterator *li, unsigned char direction);
2205 int listTypeNext(listTypeIterator *li, listTypeEntry *entry);
2206 robj *listTypeGet(listTypeEntry *entry);
2207 void listTypeInsert(listTypeEntry *entry, robj *value, int where);
2208 void listTypeReplace(listTypeEntry *entry, robj *value);
2209 int listTypeEqual(listTypeEntry *entry, robj *o);
2210 void listTypeDelete(listTypeIterator *iter, listTypeEntry *entry);
2211 robj *listTypeDup(robj *o);
2212 int listTypeDelRange(robj *o, long start, long stop);
2213 void unblockClientWaitingData(client *c);
2214 void popGenericCommand(client *c, int where);
2215 void listElementsRemoved(client *c, robj *key, int where, robj *o, long count, int *deleted);
2216 
2217 /* MULTI/EXEC/WATCH... */
2218 void unwatchAllKeys(client *c);
2219 void initClientMultiState(client *c);
2220 void freeClientMultiState(client *c);
2221 void queueMultiCommand(client *c);
2222 size_t multiStateMemOverhead(client *c);
2223 void touchWatchedKey(redisDb *db, robj *key);
2224 int isWatchedKeyExpired(client *c);
2225 void touchAllWatchedKeysInDb(redisDb *emptied, redisDb *replaced_with);
2226 void discardTransaction(client *c);
2227 void flagTransaction(client *c);
2228 void execCommandAbort(client *c, sds error);
2229 void execCommandPropagateMulti(int dbid);
2230 void execCommandPropagateExec(int dbid);
2231 void beforePropagateMulti();
2232 void afterPropagateExec();
2233 
2234 /* Redis object implementation */
2235 void decrRefCount(robj *o);
2236 void decrRefCountVoid(void *o);
2237 void incrRefCount(robj *o);
2238 robj *makeObjectShared(robj *o);
2239 robj *resetRefCount(robj *obj);
2240 void freeStringObject(robj *o);
2241 void freeListObject(robj *o);
2242 void freeSetObject(robj *o);
2243 void freeZsetObject(robj *o);
2244 void freeHashObject(robj *o);
2245 void dismissObject(robj *o, size_t dump_size);
2246 robj *createObject(int type, void *ptr);
2247 robj *createStringObject(const char *ptr, size_t len);
2248 robj *createRawStringObject(const char *ptr, size_t len);
2249 robj *createEmbeddedStringObject(const char *ptr, size_t len);
2250 robj *tryCreateRawStringObject(const char *ptr, size_t len);
2251 robj *tryCreateStringObject(const char *ptr, size_t len);
2252 robj *dupStringObject(const robj *o);
2253 int isSdsRepresentableAsLongLong(sds s, long long *llval);
2254 int isObjectRepresentableAsLongLong(robj *o, long long *llongval);
2255 robj *tryObjectEncoding(robj *o);
2256 robj *getDecodedObject(robj *o);
2257 size_t stringObjectLen(robj *o);
2258 robj *createStringObjectFromLongLong(long long value);
2259 robj *createStringObjectFromLongLongForValue(long long value);
2260 robj *createStringObjectFromLongDouble(long double value, int humanfriendly);
2261 robj *createQuicklistObject(void);
2262 robj *createSetObject(void);
2263 robj *createIntsetObject(void);
2264 robj *createHashObject(void);
2265 robj *createZsetObject(void);
2266 robj *createZsetListpackObject(void);
2267 robj *createStreamObject(void);
2268 robj *createModuleObject(moduleType *mt, void *value);
2269 int getLongFromObjectOrReply(client *c, robj *o, long *target, const char *msg);
2270 int getPositiveLongFromObjectOrReply(client *c, robj *o, long *target, const char *msg);
2271 int getRangeLongFromObjectOrReply(client *c, robj *o, long min, long max, long *target, const char *msg);
2272 int checkType(client *c, robj *o, int type);
2273 int getLongLongFromObjectOrReply(client *c, robj *o, long long *target, const char *msg);
2274 int getDoubleFromObjectOrReply(client *c, robj *o, double *target, const char *msg);
2275 int getDoubleFromObject(const robj *o, double *target);
2276 int getLongLongFromObject(robj *o, long long *target);
2277 int getLongDoubleFromObject(robj *o, long double *target);
2278 int getLongDoubleFromObjectOrReply(client *c, robj *o, long double *target, const char *msg);
2279 int getIntFromObjectOrReply(client *c, robj *o, int *target, const char *msg);
2280 char *strEncoding(int encoding);
2281 int compareStringObjects(robj *a, robj *b);
2282 int collateStringObjects(robj *a, robj *b);
2283 int equalStringObjects(robj *a, robj *b);
2284 unsigned long long estimateObjectIdleTime(robj *o);
2285 void trimStringObjectIfNeeded(robj *o);
2286 #define sdsEncodedObject(objptr) (objptr->encoding == OBJ_ENCODING_RAW || objptr->encoding == OBJ_ENCODING_EMBSTR)
2287 
2288 /* Synchronous I/O with timeout */
2289 ssize_t syncWrite(int fd, char *ptr, ssize_t size, long long timeout);
2290 ssize_t syncRead(int fd, char *ptr, ssize_t size, long long timeout);
2291 ssize_t syncReadLine(int fd, char *ptr, ssize_t size, long long timeout);
2292 
2293 /* Replication */
2294 void replicationFeedSlaves(list *slaves, int dictid, robj **argv, int argc);
2295 void replicationFeedStreamFromMasterStream(char *buf, size_t buflen);
2296 void resetReplicationBuffer(void);
2297 void feedReplicationBuffer(char *buf, size_t len);
2298 void freeReplicaReferencedReplBuffer(client *replica);
2299 void replicationFeedMonitors(client *c, list *monitors, int dictid, robj **argv, int argc);
2300 void updateSlavesWaitingBgsave(int bgsaveerr, int type);
2301 void replicationCron(void);
2302 void replicationStartPendingFork(void);
2303 void replicationHandleMasterDisconnection(void);
2304 void replicationCacheMaster(client *c);
2305 void resizeReplicationBacklog();
2306 void replicationSetMaster(char *ip, int port);
2307 void replicationUnsetMaster(void);
2308 void refreshGoodSlavesCount(void);
2309 void replicationScriptCacheInit(void);
2310 void replicationScriptCacheFlush(void);
2311 void replicationScriptCacheAdd(sds sha1);
2312 int replicationScriptCacheExists(sds sha1);
2313 void processClientsWaitingReplicas(void);
2314 void unblockClientWaitingReplicas(client *c);
2315 int replicationCountAcksByOffset(long long offset);
2316 void replicationSendNewlineToMaster(void);
2317 long long replicationGetSlaveOffset(void);
2318 char *replicationGetSlaveName(client *c);
2319 long long getPsyncInitialOffset(void);
2320 int replicationSetupSlaveForFullResync(client *slave, long long offset);
2321 void changeReplicationId(void);
2322 void clearReplicationId2(void);
2323 void createReplicationBacklog(void);
2324 void freeReplicationBacklog(void);
2325 void replicationCacheMasterUsingMyself(void);
2326 void feedReplicationBacklog(void *ptr, size_t len);
2327 void incrementalTrimReplicationBacklog(size_t blocks);
2328 int canFeedReplicaReplBuffer(client *replica);
2329 void rebaseReplicationBuffer(long long base_repl_offset);
2330 void showLatestBacklog(void);
2331 void rdbPipeReadHandler(struct aeEventLoop *eventLoop, int fd, void *clientData, int mask);
2332 void rdbPipeWriteHandlerConnRemoved(struct connection *conn);
2333 void clearFailoverState(void);
2334 void updateFailoverStatus(void);
2335 void abortFailover(const char *err);
2336 const char *getFailoverStateString();
2337 
2338 /* Generic persistence functions */
2339 void startLoadingFile(FILE* fp, char* filename, int rdbflags);
2340 void startLoading(size_t size, int rdbflags, int async);
2341 void loadingProgress(off_t pos);
2342 void stopLoading(int success);
2343 void startSaving(int rdbflags);
2344 void stopSaving(int success);
2345 int allPersistenceDisabled(void);
2346 
2347 #define DISK_ERROR_TYPE_AOF 1       /* Don't accept writes: AOF errors. */
2348 #define DISK_ERROR_TYPE_RDB 2       /* Don't accept writes: RDB errors. */
2349 #define DISK_ERROR_TYPE_NONE 0      /* No problems, we can accept writes. */
2350 int writeCommandsDeniedByDiskError(void);
2351 
2352 /* RDB persistence */
2353 #include "rdb.h"
2354 void killRDBChild(void);
2355 int bg_unlink(const char *filename);
2356 
2357 /* AOF persistence */
2358 void flushAppendOnlyFile(int force);
2359 void feedAppendOnlyFile(int dictid, robj **argv, int argc);
2360 void aofRemoveTempFile(pid_t childpid);
2361 int rewriteAppendOnlyFileBackground(void);
2362 int loadAppendOnlyFile(char *filename);
2363 void stopAppendOnly(void);
2364 int startAppendOnly(void);
2365 void backgroundRewriteDoneHandler(int exitcode, int bysignal);
2366 void aofRewriteBufferReset(void);
2367 unsigned long aofRewriteBufferSize(void);
2368 unsigned long aofRewriteBufferMemoryUsage(void);
2369 ssize_t aofReadDiffFromParent(void);
2370 void killAppendOnlyChild(void);
2371 void restartAOFAfterSYNC();
2372 
2373 /* Child info */
2374 void openChildInfoPipe(void);
2375 void closeChildInfoPipe(void);
2376 void sendChildInfoGeneric(childInfoType info_type, size_t keys, double progress, char *pname);
2377 void sendChildCowInfo(childInfoType info_type, char *pname);
2378 void sendChildInfo(childInfoType info_type, size_t keys, char *pname);
2379 void receiveChildInfo(void);
2380 
2381 /* Fork helpers */
2382 int redisFork(int type);
2383 int hasActiveChildProcess();
2384 void resetChildState();
2385 int isMutuallyExclusiveChildType(int type);
2386 
2387 /* acl.c -- Authentication related prototypes. */
2388 extern rax *Users;
2389 extern user *DefaultUser;
2390 void ACLInit(void);
2391 /* Return values for ACLCheckAllPerm(). */
2392 #define ACL_OK 0
2393 #define ACL_DENIED_CMD 1
2394 #define ACL_DENIED_KEY 2
2395 #define ACL_DENIED_AUTH 3 /* Only used for ACL LOG entries. */
2396 #define ACL_DENIED_CHANNEL 4 /* Only used for pub/sub commands */
2397 
2398 /* Context values for addACLLogEntry(). */
2399 #define ACL_LOG_CTX_TOPLEVEL 0
2400 #define ACL_LOG_CTX_LUA 1
2401 #define ACL_LOG_CTX_MULTI 2
2402 #define ACL_LOG_CTX_MODULE 3
2403 
2404 int ACLCheckUserCredentials(robj *username, robj *password);
2405 int ACLAuthenticateUser(client *c, robj *username, robj *password);
2406 unsigned long ACLGetCommandID(const char *cmdname);
2407 void ACLClearCommandID(void);
2408 user *ACLGetUserByName(const char *name, size_t namelen);
2409 int ACLCheckKey(const user *u, const char *key, int keylen);
2410 int ACLCheckPubsubChannelPerm(sds channel, list *allowed, int literal);
2411 int ACLCheckAllUserCommandPerm(const user *u, struct redisCommand *cmd, robj **argv, int argc, int *idxptr);
2412 int ACLCheckAllPerm(client *c, int *idxptr);
2413 int ACLSetUser(user *u, const char *op, ssize_t oplen);
2414 uint64_t ACLGetCommandCategoryFlagByName(const char *name);
2415 int ACLAppendUserForLoading(sds *argv, int argc, int *argc_err);
2416 const char *ACLSetUserStringError(void);
2417 int ACLLoadConfiguredUsers(void);
2418 sds ACLDescribeUser(user *u);
2419 void ACLLoadUsersAtStartup(void);
2420 void addReplyCommandCategories(client *c, struct redisCommand *cmd);
2421 user *ACLCreateUnlinkedUser();
2422 void ACLFreeUserAndKillClients(user *u);
2423 void addACLLogEntry(client *c, int reason, int context, int argpos, sds username, sds object);
2424 void ACLUpdateDefaultUserPassword(sds password);
2425 
2426 /* Sorted sets data type */
2427 
2428 /* Input flags. */
2429 #define ZADD_IN_NONE 0
2430 #define ZADD_IN_INCR (1<<0)    /* Increment the score instead of setting it. */
2431 #define ZADD_IN_NX (1<<1)      /* Don't touch elements not already existing. */
2432 #define ZADD_IN_XX (1<<2)      /* Only touch elements already existing. */
2433 #define ZADD_IN_GT (1<<3)      /* Only update existing when new scores are higher. */
2434 #define ZADD_IN_LT (1<<4)      /* Only update existing when new scores are lower. */
2435 
2436 /* Output flags. */
2437 #define ZADD_OUT_NOP (1<<0)     /* Operation not performed because of conditionals.*/
2438 #define ZADD_OUT_NAN (1<<1)     /* Only touch elements already existing. */
2439 #define ZADD_OUT_ADDED (1<<2)   /* The element was new and was added. */
2440 #define ZADD_OUT_UPDATED (1<<3) /* The element already existed, score updated. */
2441 
2442 /* Struct to hold an inclusive/exclusive range spec by score comparison. */
2443 typedef struct {
2444     double min, max;
2445     int minex, maxex; /* are min or max exclusive? */
2446 } zrangespec;
2447 
2448 /* Struct to hold an inclusive/exclusive range spec by lexicographic comparison. */
2449 typedef struct {
2450     sds min, max;     /* May be set to shared.(minstring|maxstring) */
2451     int minex, maxex; /* are min or max exclusive? */
2452 } zlexrangespec;
2453 
2454 zskiplist *zslCreate(void);
2455 void zslFree(zskiplist *zsl);
2456 zskiplistNode *zslInsert(zskiplist *zsl, double score, sds ele);
2457 unsigned char *zzlInsert(unsigned char *zl, sds ele, double score);
2458 int zslDelete(zskiplist *zsl, double score, sds ele, zskiplistNode **node);
2459 zskiplistNode *zslFirstInRange(zskiplist *zsl, zrangespec *range);
2460 zskiplistNode *zslLastInRange(zskiplist *zsl, zrangespec *range);
2461 double zzlGetScore(unsigned char *sptr);
2462 void zzlNext(unsigned char *zl, unsigned char **eptr, unsigned char **sptr);
2463 void zzlPrev(unsigned char *zl, unsigned char **eptr, unsigned char **sptr);
2464 unsigned char *zzlFirstInRange(unsigned char *zl, zrangespec *range);
2465 unsigned char *zzlLastInRange(unsigned char *zl, zrangespec *range);
2466 unsigned long zsetLength(const robj *zobj);
2467 void zsetConvert(robj *zobj, int encoding);
2468 void zsetConvertToListpackIfNeeded(robj *zobj, size_t maxelelen, size_t totelelen);
2469 int zsetScore(robj *zobj, sds member, double *score);
2470 unsigned long zslGetRank(zskiplist *zsl, double score, sds o);
2471 int zsetAdd(robj *zobj, double score, sds ele, int in_flags, int *out_flags, double *newscore);
2472 long zsetRank(robj *zobj, sds ele, int reverse);
2473 int zsetDel(robj *zobj, sds ele);
2474 robj *zsetDup(robj *o);
2475 void genericZpopCommand(client *c, robj **keyv, int keyc, int where, int emitkey, long count, int use_nested_array, int reply_nil_when_empty, int *deleted);
2476 sds lpGetObject(unsigned char *sptr);
2477 int zslValueGteMin(double value, zrangespec *spec);
2478 int zslValueLteMax(double value, zrangespec *spec);
2479 void zslFreeLexRange(zlexrangespec *spec);
2480 int zslParseLexRange(robj *min, robj *max, zlexrangespec *spec);
2481 unsigned char *zzlFirstInLexRange(unsigned char *zl, zlexrangespec *range);
2482 unsigned char *zzlLastInLexRange(unsigned char *zl, zlexrangespec *range);
2483 zskiplistNode *zslFirstInLexRange(zskiplist *zsl, zlexrangespec *range);
2484 zskiplistNode *zslLastInLexRange(zskiplist *zsl, zlexrangespec *range);
2485 int zzlLexValueGteMin(unsigned char *p, zlexrangespec *spec);
2486 int zzlLexValueLteMax(unsigned char *p, zlexrangespec *spec);
2487 int zslLexValueGteMin(sds value, zlexrangespec *spec);
2488 int zslLexValueLteMax(sds value, zlexrangespec *spec);
2489 
2490 /* Core functions */
2491 int getMaxmemoryState(size_t *total, size_t *logical, size_t *tofree, float *level);
2492 size_t freeMemoryGetNotCountedMemory();
2493 int overMaxmemoryAfterAlloc(size_t moremem);
2494 int processCommand(client *c);
2495 int processPendingCommandsAndResetClient(client *c);
2496 void setupSignalHandlers(void);
2497 void removeSignalHandlers(void);
2498 int createSocketAcceptHandler(socketFds *sfd, aeFileProc *accept_handler);
2499 int changeListenPort(int port, socketFds *sfd, aeFileProc *accept_handler);
2500 int changeBindAddr(void);
2501 struct redisCommand *lookupCommand(robj **argv ,int argc);
2502 struct redisCommand *lookupCommandBySdsLogic(dict *commands, sds s);
2503 struct redisCommand *lookupCommandBySds(sds s);
2504 struct redisCommand *lookupCommandByCStringLogic(dict *commands, const char *s);
2505 struct redisCommand *lookupCommandByCString(const char *s);
2506 struct redisCommand *lookupCommandOrOriginal(robj **argv ,int argc);
2507 void call(client *c, int flags);
2508 void propagate(int dbid, robj **argv, int argc, int flags);
2509 void alsoPropagate(int dbid, robj **argv, int argc, int target);
2510 void redisOpArrayInit(redisOpArray *oa);
2511 void redisOpArrayFree(redisOpArray *oa);
2512 void forceCommandPropagation(client *c, int flags);
2513 void preventCommandPropagation(client *c);
2514 void preventCommandAOF(client *c);
2515 void preventCommandReplication(client *c);
2516 void slowlogPushCurrentCommand(client *c, struct redisCommand *cmd, ustime_t duration);
2517 int prepareForShutdown(int flags);
2518 void afterCommand(client *c);
2519 int inNestedCall(void);
2520 #ifdef __GNUC__
2521 void _serverLog(int level, const char *fmt, ...)
2522     __attribute__((format(printf, 2, 3)));
2523 #else
2524 void _serverLog(int level, const char *fmt, ...);
2525 #endif
2526 void serverLogRaw(int level, const char *msg);
2527 void serverLogFromHandler(int level, const char *msg);
2528 void usage(void);
2529 void updateDictResizePolicy(void);
2530 int htNeedsResize(dict *dict);
2531 void populateCommandTable(void);
2532 void resetCommandTableStats(dict* commands);
2533 void resetErrorTableStats(void);
2534 void adjustOpenFilesLimit(void);
2535 void incrementErrorCount(const char *fullerr, size_t namelen);
2536 void closeListeningSockets(int unlink_unix_socket);
2537 void updateCachedTime(int update_daylight_info);
2538 void resetServerStats(void);
2539 void activeDefragCycle(void);
2540 unsigned int getLRUClock(void);
2541 unsigned int LRU_CLOCK(void);
2542 const char *evictPolicyToString(void);
2543 struct redisMemOverhead *getMemoryOverheadData(void);
2544 void freeMemoryOverheadData(struct redisMemOverhead *mh);
2545 void checkChildrenDone(void);
2546 int setOOMScoreAdj(int process_class);
2547 void rejectCommandFormat(client *c, const char *fmt, ...);
2548 void *activeDefragAlloc(void *ptr);
2549 robj *activeDefragStringOb(robj* ob, long *defragged);
2550 void dismissSds(sds s);
2551 void dismissMemory(void* ptr, size_t size_hint);
2552 void dismissMemoryInChild(void);
2553 
2554 #define RESTART_SERVER_NONE 0
2555 #define RESTART_SERVER_GRACEFULLY (1<<0)     /* Do proper shutdown. */
2556 #define RESTART_SERVER_CONFIG_REWRITE (1<<1) /* CONFIG REWRITE before restart.*/
2557 int restartServer(int flags, mstime_t delay);
2558 
2559 /* Set data type */
2560 robj *setTypeCreate(sds value);
2561 int setTypeAdd(robj *subject, sds value);
2562 int setTypeRemove(robj *subject, sds value);
2563 int setTypeIsMember(robj *subject, sds value);
2564 setTypeIterator *setTypeInitIterator(robj *subject);
2565 void setTypeReleaseIterator(setTypeIterator *si);
2566 int setTypeNext(setTypeIterator *si, sds *sdsele, int64_t *llele);
2567 sds setTypeNextObject(setTypeIterator *si);
2568 int setTypeRandomElement(robj *setobj, sds *sdsele, int64_t *llele);
2569 unsigned long setTypeRandomElements(robj *set, unsigned long count, robj *aux_set);
2570 unsigned long setTypeSize(const robj *subject);
2571 void setTypeConvert(robj *subject, int enc);
2572 robj *setTypeDup(robj *o);
2573 
2574 /* Hash data type */
2575 #define HASH_SET_TAKE_FIELD (1<<0)
2576 #define HASH_SET_TAKE_VALUE (1<<1)
2577 #define HASH_SET_COPY 0
2578 
2579 void hashTypeConvert(robj *o, int enc);
2580 void hashTypeTryConversion(robj *subject, robj **argv, int start, int end);
2581 int hashTypeExists(robj *o, sds key);
2582 int hashTypeDelete(robj *o, sds key);
2583 unsigned long hashTypeLength(const robj *o);
2584 hashTypeIterator *hashTypeInitIterator(robj *subject);
2585 void hashTypeReleaseIterator(hashTypeIterator *hi);
2586 int hashTypeNext(hashTypeIterator *hi);
2587 void hashTypeCurrentFromListpack(hashTypeIterator *hi, int what,
2588                                  unsigned char **vstr,
2589                                  unsigned int *vlen,
2590                                  long long *vll);
2591 sds hashTypeCurrentFromHashTable(hashTypeIterator *hi, int what);
2592 void hashTypeCurrentObject(hashTypeIterator *hi, int what, unsigned char **vstr, unsigned int *vlen, long long *vll);
2593 sds hashTypeCurrentObjectNewSds(hashTypeIterator *hi, int what);
2594 robj *hashTypeLookupWriteOrCreate(client *c, robj *key);
2595 robj *hashTypeGetValueObject(robj *o, sds field);
2596 int hashTypeSet(robj *o, sds field, sds value, int flags);
2597 robj *hashTypeDup(robj *o);
2598 
2599 /* Pub / Sub */
2600 int pubsubUnsubscribeAllChannels(client *c, int notify);
2601 int pubsubUnsubscribeAllPatterns(client *c, int notify);
2602 int pubsubPublishMessage(robj *channel, robj *message);
2603 void addReplyPubsubMessage(client *c, robj *channel, robj *msg);
2604 
2605 /* Keyspace events notification */
2606 void notifyKeyspaceEvent(int type, char *event, robj *key, int dbid);
2607 int keyspaceEventsStringToFlags(char *classes);
2608 sds keyspaceEventsFlagsToString(int flags);
2609 
2610 /* Configuration */
2611 void loadServerConfig(char *filename, char config_from_stdin, char *options);
2612 void appendServerSaveParams(time_t seconds, int changes);
2613 void resetServerSaveParams(void);
2614 struct rewriteConfigState; /* Forward declaration to export API. */
2615 void rewriteConfigRewriteLine(struct rewriteConfigState *state, const char *option, sds line, int force);
2616 void rewriteConfigMarkAsProcessed(struct rewriteConfigState *state, const char *option);
2617 int rewriteConfig(char *path, int force_all);
2618 void initConfigValues();
2619 sds getConfigDebugInfo();
2620 
2621 /* db.c -- Keyspace access API */
2622 int removeExpire(redisDb *db, robj *key);
2623 void deleteExpiredKeyAndPropagate(redisDb *db, robj *keyobj);
2624 void propagateExpire(redisDb *db, robj *key, int lazy);
2625 int keyIsExpired(redisDb *db, robj *key);
2626 long long getExpire(redisDb *db, robj *key);
2627 void setExpire(client *c, redisDb *db, robj *key, long long when);
2628 int checkAlreadyExpired(long long when);
2629 robj *lookupKeyRead(redisDb *db, robj *key);
2630 robj *lookupKeyWrite(redisDb *db, robj *key);
2631 robj *lookupKeyReadOrReply(client *c, robj *key, robj *reply);
2632 robj *lookupKeyWriteOrReply(client *c, robj *key, robj *reply);
2633 robj *lookupKeyReadWithFlags(redisDb *db, robj *key, int flags);
2634 robj *lookupKeyWriteWithFlags(redisDb *db, robj *key, int flags);
2635 robj *objectCommandLookup(client *c, robj *key);
2636 robj *objectCommandLookupOrReply(client *c, robj *key, robj *reply);
2637 int objectSetLRUOrLFU(robj *val, long long lfu_freq, long long lru_idle,
2638                        long long lru_clock, int lru_multiplier);
2639 #define LOOKUP_NONE 0
2640 #define LOOKUP_NOTOUCH (1<<0)  /* Don't update LRU. */
2641 #define LOOKUP_NONOTIFY (1<<1) /* Don't trigger keyspace event on key misses. */
2642 #define LOOKUP_NOSTATS (1<<2)  /* Don't update keyspace hits/misses couters. */
2643 #define LOOKUP_WRITE (1<<3)    /* Delete expired keys even in replicas. */
2644 
2645 void dbAdd(redisDb *db, robj *key, robj *val);
2646 int dbAddRDBLoad(redisDb *db, sds key, robj *val);
2647 void dbOverwrite(redisDb *db, robj *key, robj *val);
2648 
2649 #define SETKEY_KEEPTTL 1
2650 #define SETKEY_NO_SIGNAL 2
2651 #define SETKEY_ALREADY_EXIST 4
2652 #define SETKEY_DOESNT_EXIST 8
2653 void setKey(client *c, redisDb *db, robj *key, robj *val, int flags);
2654 robj *dbRandomKey(redisDb *db);
2655 int dbSyncDelete(redisDb *db, robj *key);
2656 int dbDelete(redisDb *db, robj *key);
2657 robj *dbUnshareStringValue(redisDb *db, robj *key, robj *o);
2658 
2659 #define EMPTYDB_NO_FLAGS 0      /* No flags. */
2660 #define EMPTYDB_ASYNC (1<<0)    /* Reclaim memory in another thread. */
2661 long long emptyDb(int dbnum, int flags, void(callback)(dict*));
2662 long long emptyDbStructure(redisDb *dbarray, int dbnum, int async, void(callback)(dict*));
2663 void flushAllDataAndResetRDB(int flags);
2664 long long dbTotalServerKeyCount();
2665 redisDb *initTempDb(void);
2666 void discardTempDb(redisDb *tempDb, void(callback)(dict*));
2667 
2668 
2669 int selectDb(client *c, int id);
2670 void signalModifiedKey(client *c, redisDb *db, robj *key);
2671 void signalFlushedDb(int dbid, int async);
2672 void scanGenericCommand(client *c, robj *o, unsigned long cursor);
2673 int parseScanCursorOrReply(client *c, robj *o, unsigned long *cursor);
2674 int dbAsyncDelete(redisDb *db, robj *key);
2675 void emptyDbAsync(redisDb *db);
2676 size_t lazyfreeGetPendingObjectsCount(void);
2677 size_t lazyfreeGetFreedObjectsCount(void);
2678 void lazyfreeResetStats(void);
2679 void freeObjAsync(robj *key, robj *obj, int dbid);
2680 void freeReplicationBacklogRefMemAsync(list *blocks, rax *index);
2681 
2682 /* API to get key arguments from commands */
2683 int *getKeysPrepareResult(getKeysResult *result, int numkeys);
2684 int getKeysFromCommand(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
2685 void getKeysFreeResult(getKeysResult *result);
2686 int sintercardGetKeys(struct redisCommand *cmd,robj **argv, int argc, getKeysResult *result);
2687 int zunionInterDiffGetKeys(struct redisCommand *cmd,robj **argv, int argc, getKeysResult *result);
2688 int zunionInterDiffStoreGetKeys(struct redisCommand *cmd,robj **argv, int argc, getKeysResult *result);
2689 int evalGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
2690 int sortGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
2691 int migrateGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
2692 int georadiusGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
2693 int xreadGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
2694 int lmpopGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
2695 int blmpopGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
2696 int zmpopGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
2697 int bzmpopGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
2698 
2699 unsigned short crc16(const char *buf, int len);
2700 
2701 /* Sentinel */
2702 void initSentinelConfig(void);
2703 void initSentinel(void);
2704 void sentinelTimer(void);
2705 const char *sentinelHandleConfiguration(char **argv, int argc);
2706 void queueSentinelConfig(sds *argv, int argc, int linenum, sds line);
2707 void loadSentinelConfigFromQueue(void);
2708 void sentinelIsRunning(void);
2709 void sentinelCheckConfigFile(void);
2710 void sentinelCommand(client *c);
2711 void sentinelInfoCommand(client *c);
2712 void sentinelPublishCommand(client *c);
2713 void sentinelRoleCommand(client *c);
2714 
2715 /* redis-check-rdb & aof */
2716 int redis_check_rdb(char *rdbfilename, FILE *fp);
2717 int redis_check_rdb_main(int argc, char **argv, FILE *fp);
2718 int redis_check_aof_main(int argc, char **argv);
2719 
2720 /* Scripting */
2721 void scriptingInit(int setup);
2722 int ldbRemoveChild(pid_t pid);
2723 void ldbKillForkedSessions(void);
2724 int ldbPendingChildren(void);
2725 sds luaCreateFunction(client *c, lua_State *lua, robj *body);
2726 void freeLuaScriptsAsync(dict *lua_scripts);
2727 
2728 /* Blocked clients */
2729 void processUnblockedClients(void);
2730 void blockClient(client *c, int btype);
2731 void unblockClient(client *c);
2732 void queueClientForReprocessing(client *c);
2733 void replyToBlockedClientTimedOut(client *c);
2734 int getTimeoutFromObjectOrReply(client *c, robj *object, mstime_t *timeout, int unit);
2735 void disconnectAllBlockedClients(void);
2736 void handleClientsBlockedOnKeys(void);
2737 void signalKeyAsReady(redisDb *db, robj *key, int type);
2738 void blockForKeys(client *c, int btype, robj **keys, int numkeys, long count, mstime_t timeout, robj *target, struct blockPos *blockpos, streamID *ids);
2739 void updateStatsOnUnblock(client *c, long blocked_us, long reply_us);
2740 
2741 /* timeout.c -- Blocked clients timeout and connections timeout. */
2742 void addClientToTimeoutTable(client *c);
2743 void removeClientFromTimeoutTable(client *c);
2744 void handleBlockedClientsTimeout(void);
2745 int clientsCronHandleTimeout(client *c, mstime_t now_ms);
2746 
2747 /* expire.c -- Handling of expired keys */
2748 void activeExpireCycle(int type);
2749 void expireSlaveKeys(void);
2750 void rememberSlaveKeyWithExpire(redisDb *db, robj *key);
2751 void flushSlaveKeysWithExpireList(void);
2752 size_t getSlaveKeyWithExpireCount(void);
2753 
2754 /* evict.c -- maxmemory handling and LRU eviction. */
2755 void evictionPoolAlloc(void);
2756 #define LFU_INIT_VAL 5
2757 unsigned long LFUGetTimeInMinutes(void);
2758 uint8_t LFULogIncr(uint8_t value);
2759 unsigned long LFUDecrAndReturn(robj *o);
2760 #define EVICT_OK 0
2761 #define EVICT_RUNNING 1
2762 #define EVICT_FAIL 2
2763 int performEvictions(void);
2764 
2765 
2766 /* Keys hashing / comparison functions for dict.c hash tables. */
2767 uint64_t dictSdsHash(const void *key);
2768 uint64_t dictSdsCaseHash(const void *key);
2769 int dictSdsKeyCompare(dict *d, const void *key1, const void *key2);
2770 int dictSdsKeyCaseCompare(dict *d, const void *key1, const void *key2);
2771 void dictSdsDestructor(dict *d, void *val);
2772 
2773 /* Git SHA1 */
2774 char *redisGitSHA1(void);
2775 char *redisGitDirty(void);
2776 uint64_t redisBuildId(void);
2777 char *redisBuildIdString(void);
2778 
2779 /* Commands prototypes */
2780 void authCommand(client *c);
2781 void pingCommand(client *c);
2782 void echoCommand(client *c);
2783 void commandCommand(client *c);
2784 void commandCountCommand(client *c);
2785 void commandListCommand(client *c);
2786 void commandInfoCommand(client *c);
2787 void commandGetKeysCommand(client *c);
2788 void commandHelpCommand(client *c);
2789 void setCommand(client *c);
2790 void setnxCommand(client *c);
2791 void setexCommand(client *c);
2792 void psetexCommand(client *c);
2793 void getCommand(client *c);
2794 void getexCommand(client *c);
2795 void getdelCommand(client *c);
2796 void delCommand(client *c);
2797 void unlinkCommand(client *c);
2798 void existsCommand(client *c);
2799 void setbitCommand(client *c);
2800 void getbitCommand(client *c);
2801 void bitfieldCommand(client *c);
2802 void bitfieldroCommand(client *c);
2803 void setrangeCommand(client *c);
2804 void getrangeCommand(client *c);
2805 void incrCommand(client *c);
2806 void decrCommand(client *c);
2807 void incrbyCommand(client *c);
2808 void decrbyCommand(client *c);
2809 void incrbyfloatCommand(client *c);
2810 void selectCommand(client *c);
2811 void swapdbCommand(client *c);
2812 void randomkeyCommand(client *c);
2813 void keysCommand(client *c);
2814 void scanCommand(client *c);
2815 void dbsizeCommand(client *c);
2816 void lastsaveCommand(client *c);
2817 void saveCommand(client *c);
2818 void bgsaveCommand(client *c);
2819 void bgrewriteaofCommand(client *c);
2820 void shutdownCommand(client *c);
2821 void moveCommand(client *c);
2822 void copyCommand(client *c);
2823 void renameCommand(client *c);
2824 void renamenxCommand(client *c);
2825 void lpushCommand(client *c);
2826 void rpushCommand(client *c);
2827 void lpushxCommand(client *c);
2828 void rpushxCommand(client *c);
2829 void linsertCommand(client *c);
2830 void lpopCommand(client *c);
2831 void rpopCommand(client *c);
2832 void lmpopCommand(client *c);
2833 void llenCommand(client *c);
2834 void lindexCommand(client *c);
2835 void lrangeCommand(client *c);
2836 void ltrimCommand(client *c);
2837 void typeCommand(client *c);
2838 void lsetCommand(client *c);
2839 void saddCommand(client *c);
2840 void sremCommand(client *c);
2841 void smoveCommand(client *c);
2842 void sismemberCommand(client *c);
2843 void smismemberCommand(client *c);
2844 void scardCommand(client *c);
2845 void spopCommand(client *c);
2846 void srandmemberCommand(client *c);
2847 void sinterCommand(client *c);
2848 void sinterCardCommand(client *c);
2849 void sinterstoreCommand(client *c);
2850 void sunionCommand(client *c);
2851 void sunionstoreCommand(client *c);
2852 void sdiffCommand(client *c);
2853 void sdiffstoreCommand(client *c);
2854 void sscanCommand(client *c);
2855 void syncCommand(client *c);
2856 void flushdbCommand(client *c);
2857 void flushallCommand(client *c);
2858 void sortCommand(client *c);
2859 void sortroCommand(client *c);
2860 void lremCommand(client *c);
2861 void lposCommand(client *c);
2862 void rpoplpushCommand(client *c);
2863 void lmoveCommand(client *c);
2864 void infoCommand(client *c);
2865 void mgetCommand(client *c);
2866 void monitorCommand(client *c);
2867 void expireCommand(client *c);
2868 void expireatCommand(client *c);
2869 void pexpireCommand(client *c);
2870 void pexpireatCommand(client *c);
2871 void getsetCommand(client *c);
2872 void ttlCommand(client *c);
2873 void touchCommand(client *c);
2874 void pttlCommand(client *c);
2875 void expiretimeCommand(client *c);
2876 void pexpiretimeCommand(client *c);
2877 void persistCommand(client *c);
2878 void replicaofCommand(client *c);
2879 void roleCommand(client *c);
2880 void debugCommand(client *c);
2881 void msetCommand(client *c);
2882 void msetnxCommand(client *c);
2883 void zaddCommand(client *c);
2884 void zincrbyCommand(client *c);
2885 void zrangeCommand(client *c);
2886 void zrangebyscoreCommand(client *c);
2887 void zrevrangebyscoreCommand(client *c);
2888 void zrangebylexCommand(client *c);
2889 void zrevrangebylexCommand(client *c);
2890 void zcountCommand(client *c);
2891 void zlexcountCommand(client *c);
2892 void zrevrangeCommand(client *c);
2893 void zcardCommand(client *c);
2894 void zremCommand(client *c);
2895 void zscoreCommand(client *c);
2896 void zmscoreCommand(client *c);
2897 void zremrangebyscoreCommand(client *c);
2898 void zremrangebylexCommand(client *c);
2899 void zpopminCommand(client *c);
2900 void zpopmaxCommand(client *c);
2901 void zmpopCommand(client *c);
2902 void bzpopminCommand(client *c);
2903 void bzpopmaxCommand(client *c);
2904 void bzmpopCommand(client *c);
2905 void zrandmemberCommand(client *c);
2906 void multiCommand(client *c);
2907 void execCommand(client *c);
2908 void discardCommand(client *c);
2909 void blpopCommand(client *c);
2910 void brpopCommand(client *c);
2911 void blmpopCommand(client *c);
2912 void brpoplpushCommand(client *c);
2913 void blmoveCommand(client *c);
2914 void appendCommand(client *c);
2915 void strlenCommand(client *c);
2916 void zrankCommand(client *c);
2917 void zrevrankCommand(client *c);
2918 void hsetCommand(client *c);
2919 void hsetnxCommand(client *c);
2920 void hgetCommand(client *c);
2921 void hmgetCommand(client *c);
2922 void hdelCommand(client *c);
2923 void hlenCommand(client *c);
2924 void hstrlenCommand(client *c);
2925 void zremrangebyrankCommand(client *c);
2926 void zunionstoreCommand(client *c);
2927 void zinterstoreCommand(client *c);
2928 void zdiffstoreCommand(client *c);
2929 void zunionCommand(client *c);
2930 void zinterCommand(client *c);
2931 void zinterCardCommand(client *c);
2932 void zrangestoreCommand(client *c);
2933 void zdiffCommand(client *c);
2934 void zscanCommand(client *c);
2935 void hkeysCommand(client *c);
2936 void hvalsCommand(client *c);
2937 void hgetallCommand(client *c);
2938 void hexistsCommand(client *c);
2939 void hscanCommand(client *c);
2940 void hrandfieldCommand(client *c);
2941 void configSetCommand(client *c);
2942 void configGetCommand(client *c);
2943 void configResetStatCommand(client *c);
2944 void configRewriteCommand(client *c);
2945 void configHelpCommand(client *c);
2946 void hincrbyCommand(client *c);
2947 void hincrbyfloatCommand(client *c);
2948 void subscribeCommand(client *c);
2949 void unsubscribeCommand(client *c);
2950 void psubscribeCommand(client *c);
2951 void punsubscribeCommand(client *c);
2952 void publishCommand(client *c);
2953 void pubsubCommand(client *c);
2954 void watchCommand(client *c);
2955 void unwatchCommand(client *c);
2956 void clusterCommand(client *c);
2957 void restoreCommand(client *c);
2958 void migrateCommand(client *c);
2959 void askingCommand(client *c);
2960 void readonlyCommand(client *c);
2961 void readwriteCommand(client *c);
2962 void dumpCommand(client *c);
2963 void objectCommand(client *c);
2964 void memoryCommand(client *c);
2965 void clientCommand(client *c);
2966 void helloCommand(client *c);
2967 void evalCommand(client *c);
2968 void evalRoCommand(client *c);
2969 void evalShaCommand(client *c);
2970 void evalShaRoCommand(client *c);
2971 void scriptCommand(client *c);
2972 void timeCommand(client *c);
2973 void bitopCommand(client *c);
2974 void bitcountCommand(client *c);
2975 void bitposCommand(client *c);
2976 void replconfCommand(client *c);
2977 void waitCommand(client *c);
2978 void geoencodeCommand(client *c);
2979 void geodecodeCommand(client *c);
2980 void georadiusbymemberCommand(client *c);
2981 void georadiusbymemberroCommand(client *c);
2982 void georadiusCommand(client *c);
2983 void georadiusroCommand(client *c);
2984 void geoaddCommand(client *c);
2985 void geohashCommand(client *c);
2986 void geoposCommand(client *c);
2987 void geodistCommand(client *c);
2988 void geosearchCommand(client *c);
2989 void geosearchstoreCommand(client *c);
2990 void pfselftestCommand(client *c);
2991 void pfaddCommand(client *c);
2992 void pfcountCommand(client *c);
2993 void pfmergeCommand(client *c);
2994 void pfdebugCommand(client *c);
2995 void latencyCommand(client *c);
2996 void moduleCommand(client *c);
2997 void securityWarningCommand(client *c);
2998 void xaddCommand(client *c);
2999 void xrangeCommand(client *c);
3000 void xrevrangeCommand(client *c);
3001 void xlenCommand(client *c);
3002 void xreadCommand(client *c);
3003 void xgroupCommand(client *c);
3004 void xsetidCommand(client *c);
3005 void xackCommand(client *c);
3006 void xpendingCommand(client *c);
3007 void xclaimCommand(client *c);
3008 void xautoclaimCommand(client *c);
3009 void xinfoCommand(client *c);
3010 void xdelCommand(client *c);
3011 void xtrimCommand(client *c);
3012 void lolwutCommand(client *c);
3013 void aclCommand(client *c);
3014 void lcsCommand(client *c);
3015 void quitCommand(client *c);
3016 void resetCommand(client *c);
3017 void failoverCommand(client *c);
3018 
3019 #if defined(__GNUC__)
3020 void *calloc(size_t count, size_t size) __attribute__ ((deprecated));
3021 void free(void *ptr) __attribute__ ((deprecated));
3022 void *malloc(size_t size) __attribute__ ((deprecated));
3023 void *realloc(void *ptr, size_t size) __attribute__ ((deprecated));
3024 #endif
3025 
3026 /* Debugging stuff */
3027 void _serverAssertWithInfo(const client *c, const robj *o, const char *estr, const char *file, int line);
3028 void _serverAssert(const char *estr, const char *file, int line);
3029 #ifdef __GNUC__
3030 void _serverPanic(const char *file, int line, const char *msg, ...)
3031     __attribute__ ((format (printf, 3, 4)));
3032 #else
3033 void _serverPanic(const char *file, int line, const char *msg, ...);
3034 #endif
3035 void serverLogObjectDebugInfo(const robj *o);
3036 void sigsegvHandler(int sig, siginfo_t *info, void *secret);
3037 sds getFullCommandName(struct redisCommand *cmd);
3038 const char *getSafeInfoString(const char *s, size_t len, char **tmp);
3039 sds genRedisInfoString(const char *section);
3040 sds genModulesInfoString(sds info);
3041 void applyWatchdogPeriod();
3042 void watchdogScheduleSignal(int period);
3043 void serverLogHexDump(int level, char *descr, void *value, size_t len);
3044 int memtest_preserving_test(unsigned long *m, size_t bytes, int passes);
3045 void mixDigest(unsigned char *digest, void *ptr, size_t len);
3046 void xorDigest(unsigned char *digest, void *ptr, size_t len);
3047 int populateSingleCommand(struct redisCommand *c, char *strflags);
3048 void commandAddSubcommand(struct redisCommand *parent, struct redisCommand *subcommand);
3049 void populateCommandMovableKeys(struct redisCommand *cmd);
3050 void debugDelay(int usec);
3051 void killIOThreads(void);
3052 void killThreads(void);
3053 void makeThreadKillable(void);
3054 void swapMainDbWithTempDb(redisDb *tempDb);
3055 
3056 /* Use macro for checking log level to avoid evaluating arguments in cases log
3057  * should be ignored due to low level. */
3058 #define serverLog(level, ...) do {\
3059         if (((level)&0xff) < server.verbosity) break;\
3060         _serverLog(level, __VA_ARGS__);\
3061     } while(0)
3062 
3063 /* TLS stuff */
3064 void tlsInit(void);
3065 void tlsCleanup(void);
3066 int tlsConfigure(redisTLSContextConfig *ctx_config);
3067 int isTlsConfigured(void);
3068 
3069 #define redisDebug(fmt, ...) \
3070     printf("DEBUG %s:%d > " fmt "\n", __FILE__, __LINE__, __VA_ARGS__)
3071 #define redisDebugMark() \
3072     printf("-- MARK %s:%d --\n", __FILE__, __LINE__)
3073 
3074 int iAmMaster(void);
3075 
3076 #endif
3077