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 
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <time.h>
42 #include <limits.h>
43 #include <unistd.h>
44 #include <errno.h>
45 #include <inttypes.h>
46 #include <pthread.h>
47 #include <syslog.h>
48 #include <netinet/in.h>
49 #include <sys/socket.h>
50 #include <lua.h>
51 #include <signal.h>
52 
53 #ifdef HAVE_LIBSYSTEMD
54 #include <systemd/sd-daemon.h>
55 #endif
56 
57 typedef long long mstime_t; /* millisecond time type. */
58 typedef long long ustime_t; /* microsecond time type. */
59 
60 #include "ae.h"      /* Event driven programming library */
61 #include "sds.h"     /* Dynamic safe strings */
62 #include "dict.h"    /* Hash tables */
63 #include "adlist.h"  /* Linked lists */
64 #include "zmalloc.h" /* total memory usage aware version of malloc/free */
65 #include "anet.h"    /* Networking the easy way */
66 #include "ziplist.h" /* Compact list data structure */
67 #include "intset.h"  /* Compact integer set structure */
68 #include "version.h" /* Version macro */
69 #include "util.h"    /* Misc functions useful in many places */
70 #include "latency.h" /* Latency monitor API */
71 #include "sparkline.h" /* ASCII graphs API */
72 #include "quicklist.h"  /* Lists are encoded as linked lists of
73                            N-elements flat arrays */
74 #include "rax.h"     /* Radix tree */
75 #include "connection.h" /* Connection abstraction */
76 
77 #define REDISMODULE_CORE 1
78 #include "redismodule.h"    /* Redis modules API defines. */
79 
80 /* Following includes allow test functions to be called from Redis main() */
81 #include "zipmap.h"
82 #include "sha1.h"
83 #include "endianconv.h"
84 #include "crc64.h"
85 
86 /* Error codes */
87 #define C_OK                    0
88 #define C_ERR                   -1
89 
90 /* Static server configuration */
91 #define CONFIG_DEFAULT_HZ        10             /* Time interrupt calls/sec. */
92 #define CONFIG_MIN_HZ            1
93 #define CONFIG_MAX_HZ            500
94 #define MAX_CLIENTS_PER_CLOCK_TICK 200          /* HZ is adapted based on that. */
95 #define CONFIG_MAX_LINE    1024
96 #define CRON_DBS_PER_CALL 16
97 #define NET_MAX_WRITES_PER_EVENT (1024*64)
98 #define PROTO_SHARED_SELECT_CMDS 10
99 #define OBJ_SHARED_INTEGERS 10000
100 #define OBJ_SHARED_BULKHDR_LEN 32
101 #define LOG_MAX_LEN    1024 /* Default maximum length of syslog messages.*/
102 #define AOF_REWRITE_ITEMS_PER_CMD 64
103 #define AOF_READ_DIFF_INTERVAL_BYTES (1024*10)
104 #define CONFIG_AUTHPASS_MAX_LEN 512
105 #define CONFIG_RUN_ID_SIZE 40
106 #define RDB_EOF_MARK_SIZE 40
107 #define CONFIG_REPL_BACKLOG_MIN_SIZE (1024*16)          /* 16k */
108 #define CONFIG_BGSAVE_RETRY_DELAY 5 /* Wait a few secs before trying again. */
109 #define CONFIG_DEFAULT_PID_FILE "/var/run/redis.pid"
110 #define CONFIG_DEFAULT_CLUSTER_CONFIG_FILE "nodes.conf"
111 #define CONFIG_DEFAULT_UNIX_SOCKET_PERM 0
112 #define CONFIG_DEFAULT_LOGFILE ""
113 #define NET_IP_STR_LEN 46 /* INET6_ADDRSTRLEN is 46, but we need to be sure */
114 #define NET_PEER_ID_LEN (NET_IP_STR_LEN+32) /* Must be enough for ip:port */
115 #define CONFIG_BINDADDR_MAX 16
116 #define CONFIG_MIN_RESERVED_FDS 32
117 
118 #define ACTIVE_EXPIRE_CYCLE_SLOW 0
119 #define ACTIVE_EXPIRE_CYCLE_FAST 1
120 
121 /* Children process will exit with this status code to signal that the
122  * process terminated without an error: this is useful in order to kill
123  * a saving child (RDB or AOF one), without triggering in the parent the
124  * write protection that is normally turned on on write errors.
125  * Usually children that are terminated with SIGUSR1 will exit with this
126  * special code. */
127 #define SERVER_CHILD_NOERROR_RETVAL    255
128 
129 /* Instantaneous metrics tracking. */
130 #define STATS_METRIC_SAMPLES 16     /* Number of samples per metric. */
131 #define STATS_METRIC_COMMAND 0      /* Number of commands executed. */
132 #define STATS_METRIC_NET_INPUT 1    /* Bytes read to network .*/
133 #define STATS_METRIC_NET_OUTPUT 2   /* Bytes written to network. */
134 #define STATS_METRIC_COUNT 3
135 
136 /* Protocol and I/O related defines */
137 #define PROTO_MAX_QUERYBUF_LEN  (1024*1024*1024) /* 1GB max query buffer. */
138 #define PROTO_IOBUF_LEN         (1024*16)  /* Generic I/O buffer size */
139 #define PROTO_REPLY_CHUNK_BYTES (16*1024) /* 16k output buffer */
140 #define PROTO_INLINE_MAX_SIZE   (1024*64) /* Max size of inline reads */
141 #define PROTO_MBULK_BIG_ARG     (1024*32)
142 #define LONG_STR_SIZE      21          /* Bytes needed for long -> str + '\0' */
143 #define REDIS_AUTOSYNC_BYTES (1024*1024*32) /* fdatasync every 32MB */
144 
145 #define LIMIT_PENDING_QUERYBUF (4*1024*1024) /* 4mb */
146 
147 /* When configuring the server eventloop, we setup it so that the total number
148  * of file descriptors we can handle are server.maxclients + RESERVED_FDS +
149  * a few more to stay safe. Since RESERVED_FDS defaults to 32, we add 96
150  * in order to make sure of not over provisioning more than 128 fds. */
151 #define CONFIG_FDSET_INCR (CONFIG_MIN_RESERVED_FDS+96)
152 
153 /* OOM Score Adjustment classes. */
154 #define CONFIG_OOM_MASTER 0
155 #define CONFIG_OOM_REPLICA 1
156 #define CONFIG_OOM_BGCHILD 2
157 #define CONFIG_OOM_COUNT 3
158 
159 extern int configOOMScoreAdjValuesDefaults[CONFIG_OOM_COUNT];
160 
161 /* Hash table parameters */
162 #define HASHTABLE_MIN_FILL        10      /* Minimal hash table fill 10% */
163 
164 /* Command flags. Please check the command table defined in the server.c file
165  * for more information about the meaning of every flag. */
166 #define CMD_WRITE (1ULL<<0)            /* "write" flag */
167 #define CMD_READONLY (1ULL<<1)         /* "read-only" flag */
168 #define CMD_DENYOOM (1ULL<<2)          /* "use-memory" flag */
169 #define CMD_MODULE (1ULL<<3)           /* Command exported by module. */
170 #define CMD_ADMIN (1ULL<<4)            /* "admin" flag */
171 #define CMD_PUBSUB (1ULL<<5)           /* "pub-sub" flag */
172 #define CMD_NOSCRIPT (1ULL<<6)         /* "no-script" flag */
173 #define CMD_RANDOM (1ULL<<7)           /* "random" flag */
174 #define CMD_SORT_FOR_SCRIPT (1ULL<<8)  /* "to-sort" flag */
175 #define CMD_LOADING (1ULL<<9)          /* "ok-loading" flag */
176 #define CMD_STALE (1ULL<<10)           /* "ok-stale" flag */
177 #define CMD_SKIP_MONITOR (1ULL<<11)    /* "no-monitor" flag */
178 #define CMD_SKIP_SLOWLOG (1ULL<<12)    /* "no-slowlog" flag */
179 #define CMD_ASKING (1ULL<<13)          /* "cluster-asking" flag */
180 #define CMD_FAST (1ULL<<14)            /* "fast" flag */
181 #define CMD_NO_AUTH (1ULL<<15)         /* "no-auth" flag */
182 
183 /* Command flags used by the module system. */
184 #define CMD_MODULE_GETKEYS (1ULL<<16)  /* Use the modules getkeys interface. */
185 #define CMD_MODULE_NO_CLUSTER (1ULL<<17) /* Deny on Redis Cluster. */
186 
187 /* Command flags that describe ACLs categories. */
188 #define CMD_CATEGORY_KEYSPACE (1ULL<<18)
189 #define CMD_CATEGORY_READ (1ULL<<19)
190 #define CMD_CATEGORY_WRITE (1ULL<<20)
191 #define CMD_CATEGORY_SET (1ULL<<21)
192 #define CMD_CATEGORY_SORTEDSET (1ULL<<22)
193 #define CMD_CATEGORY_LIST (1ULL<<23)
194 #define CMD_CATEGORY_HASH (1ULL<<24)
195 #define CMD_CATEGORY_STRING (1ULL<<25)
196 #define CMD_CATEGORY_BITMAP (1ULL<<26)
197 #define CMD_CATEGORY_HYPERLOGLOG (1ULL<<27)
198 #define CMD_CATEGORY_GEO (1ULL<<28)
199 #define CMD_CATEGORY_STREAM (1ULL<<29)
200 #define CMD_CATEGORY_PUBSUB (1ULL<<30)
201 #define CMD_CATEGORY_ADMIN (1ULL<<31)
202 #define CMD_CATEGORY_FAST (1ULL<<32)
203 #define CMD_CATEGORY_SLOW (1ULL<<33)
204 #define CMD_CATEGORY_BLOCKING (1ULL<<34)
205 #define CMD_CATEGORY_DANGEROUS (1ULL<<35)
206 #define CMD_CATEGORY_CONNECTION (1ULL<<36)
207 #define CMD_CATEGORY_TRANSACTION (1ULL<<37)
208 #define CMD_CATEGORY_SCRIPTING (1ULL<<38)
209 
210 /* AOF states */
211 #define AOF_OFF 0             /* AOF is off */
212 #define AOF_ON 1              /* AOF is on */
213 #define AOF_WAIT_REWRITE 2    /* AOF waits rewrite to start appending */
214 
215 /* Client flags */
216 #define CLIENT_SLAVE (1<<0)   /* This client is a repliaca */
217 #define CLIENT_MASTER (1<<1)  /* This client is a master */
218 #define CLIENT_MONITOR (1<<2) /* This client is a slave monitor, see MONITOR */
219 #define CLIENT_MULTI (1<<3)   /* This client is in a MULTI context */
220 #define CLIENT_BLOCKED (1<<4) /* The client is waiting in a blocking operation */
221 #define CLIENT_DIRTY_CAS (1<<5) /* Watched keys modified. EXEC will fail. */
222 #define CLIENT_CLOSE_AFTER_REPLY (1<<6) /* Close after writing entire reply. */
223 #define CLIENT_UNBLOCKED (1<<7) /* This client was unblocked and is stored in
224                                   server.unblocked_clients */
225 #define CLIENT_LUA (1<<8) /* This is a non connected client used by Lua */
226 #define CLIENT_ASKING (1<<9)     /* Client issued the ASKING command */
227 #define CLIENT_CLOSE_ASAP (1<<10)/* Close this client ASAP */
228 #define CLIENT_UNIX_SOCKET (1<<11) /* Client connected via Unix domain socket */
229 #define CLIENT_DIRTY_EXEC (1<<12)  /* EXEC will fail for errors while queueing */
230 #define CLIENT_MASTER_FORCE_REPLY (1<<13)  /* Queue replies even if is master */
231 #define CLIENT_FORCE_AOF (1<<14)   /* Force AOF propagation of current cmd. */
232 #define CLIENT_FORCE_REPL (1<<15)  /* Force replication of current cmd. */
233 #define CLIENT_PRE_PSYNC (1<<16)   /* Instance don't understand PSYNC. */
234 #define CLIENT_READONLY (1<<17)    /* Cluster client is in read-only state. */
235 #define CLIENT_PUBSUB (1<<18)      /* Client is in Pub/Sub mode. */
236 #define CLIENT_PREVENT_AOF_PROP (1<<19)  /* Don't propagate to AOF. */
237 #define CLIENT_PREVENT_REPL_PROP (1<<20)  /* Don't propagate to slaves. */
238 #define CLIENT_PREVENT_PROP (CLIENT_PREVENT_AOF_PROP|CLIENT_PREVENT_REPL_PROP)
239 #define CLIENT_PENDING_WRITE (1<<21) /* Client has output to send but a write
240                                         handler is yet not installed. */
241 #define CLIENT_REPLY_OFF (1<<22)   /* Don't send replies to client. */
242 #define CLIENT_REPLY_SKIP_NEXT (1<<23)  /* Set CLIENT_REPLY_SKIP for next cmd */
243 #define CLIENT_REPLY_SKIP (1<<24)  /* Don't send just this reply. */
244 #define CLIENT_LUA_DEBUG (1<<25)  /* Run EVAL in debug mode. */
245 #define CLIENT_LUA_DEBUG_SYNC (1<<26)  /* EVAL debugging without fork() */
246 #define CLIENT_MODULE (1<<27) /* Non connected client used by some module. */
247 #define CLIENT_PROTECTED (1<<28) /* Client should not be freed for now. */
248 #define CLIENT_PENDING_READ (1<<29) /* The client has pending reads and was put
249                                        in the list of clients we can read
250                                        from. */
251 #define CLIENT_PENDING_COMMAND (1<<30) /* Used in threaded I/O to signal after
252                                           we return single threaded that the
253                                           client has already pending commands
254                                           to be executed. */
255 #define CLIENT_TRACKING (1ULL<<31) /* Client enabled keys tracking in order to
256                                    perform client side caching. */
257 #define CLIENT_TRACKING_BROKEN_REDIR (1ULL<<32) /* Target client is invalid. */
258 #define CLIENT_TRACKING_BCAST (1ULL<<33) /* Tracking in BCAST mode. */
259 #define CLIENT_TRACKING_OPTIN (1ULL<<34)  /* Tracking in opt-in mode. */
260 #define CLIENT_TRACKING_OPTOUT (1ULL<<35) /* Tracking in opt-out mode. */
261 #define CLIENT_TRACKING_CACHING (1ULL<<36) /* CACHING yes/no was given,
262                                               depending on optin/optout mode. */
263 #define CLIENT_TRACKING_NOLOOP (1ULL<<37) /* Don't send invalidation messages
264                                              about writes performed by myself.*/
265 #define CLIENT_IN_TO_TABLE (1ULL<<38) /* This client is in the timeout table. */
266 #define CLIENT_PROTOCOL_ERROR (1ULL<<39) /* Protocol error chatting with it. */
267 #define CLIENT_CLOSE_AFTER_COMMAND (1ULL<<40) /* Close after executing commands
268                                                * and writing entire reply. */
269 
270 /* Client block type (btype field in client structure)
271  * if CLIENT_BLOCKED flag is set. */
272 #define BLOCKED_NONE 0    /* Not blocked, no CLIENT_BLOCKED flag set. */
273 #define BLOCKED_LIST 1    /* BLPOP & co. */
274 #define BLOCKED_WAIT 2    /* WAIT for synchronous replication. */
275 #define BLOCKED_MODULE 3  /* Blocked by a loadable module. */
276 #define BLOCKED_STREAM 4  /* XREAD. */
277 #define BLOCKED_ZSET 5    /* BZPOP et al. */
278 #define BLOCKED_NUM 6     /* Number of blocked states. */
279 
280 /* Client request types */
281 #define PROTO_REQ_INLINE 1
282 #define PROTO_REQ_MULTIBULK 2
283 
284 /* Client classes for client limits, currently used only for
285  * the max-client-output-buffer limit implementation. */
286 #define CLIENT_TYPE_NORMAL 0 /* Normal req-reply clients + MONITORs */
287 #define CLIENT_TYPE_SLAVE 1  /* Slaves. */
288 #define CLIENT_TYPE_PUBSUB 2 /* Clients subscribed to PubSub channels. */
289 #define CLIENT_TYPE_MASTER 3 /* Master. */
290 #define CLIENT_TYPE_COUNT 4  /* Total number of client types. */
291 #define CLIENT_TYPE_OBUF_COUNT 3 /* Number of clients to expose to output
292                                     buffer configuration. Just the first
293                                     three: normal, slave, pubsub. */
294 
295 /* Slave replication state. Used in server.repl_state for slaves to remember
296  * what to do next. */
297 #define REPL_STATE_NONE 0 /* No active replication */
298 #define REPL_STATE_CONNECT 1 /* Must connect to master */
299 #define REPL_STATE_CONNECTING 2 /* Connecting to master */
300 /* --- Handshake states, must be ordered --- */
301 #define REPL_STATE_RECEIVE_PONG 3 /* Wait for PING reply */
302 #define REPL_STATE_SEND_AUTH 4 /* Send AUTH to master */
303 #define REPL_STATE_RECEIVE_AUTH 5 /* Wait for AUTH reply */
304 #define REPL_STATE_SEND_PORT 6 /* Send REPLCONF listening-port */
305 #define REPL_STATE_RECEIVE_PORT 7 /* Wait for REPLCONF reply */
306 #define REPL_STATE_SEND_IP 8 /* Send REPLCONF ip-address */
307 #define REPL_STATE_RECEIVE_IP 9 /* Wait for REPLCONF reply */
308 #define REPL_STATE_SEND_CAPA 10 /* Send REPLCONF capa */
309 #define REPL_STATE_RECEIVE_CAPA 11 /* Wait for REPLCONF reply */
310 #define REPL_STATE_SEND_PSYNC 12 /* Send PSYNC */
311 #define REPL_STATE_RECEIVE_PSYNC 13 /* Wait for PSYNC reply */
312 /* --- End of handshake states --- */
313 #define REPL_STATE_TRANSFER 14 /* Receiving .rdb from master */
314 #define REPL_STATE_CONNECTED 15 /* Connected to master */
315 
316 /* State of slaves from the POV of the master. Used in client->replstate.
317  * In SEND_BULK and ONLINE state the slave receives new updates
318  * in its output queue. In the WAIT_BGSAVE states instead the server is waiting
319  * to start the next background saving in order to send updates to it. */
320 #define SLAVE_STATE_WAIT_BGSAVE_START 6 /* We need to produce a new RDB file. */
321 #define SLAVE_STATE_WAIT_BGSAVE_END 7 /* Waiting RDB file creation to finish. */
322 #define SLAVE_STATE_SEND_BULK 8 /* Sending RDB file to slave. */
323 #define SLAVE_STATE_ONLINE 9 /* RDB file transmitted, sending just updates. */
324 
325 /* Slave capabilities. */
326 #define SLAVE_CAPA_NONE 0
327 #define SLAVE_CAPA_EOF (1<<0)    /* Can parse the RDB EOF streaming format. */
328 #define SLAVE_CAPA_PSYNC2 (1<<1) /* Supports PSYNC2 protocol. */
329 
330 /* Synchronous read timeout - slave side */
331 #define CONFIG_REPL_SYNCIO_TIMEOUT 5
332 
333 /* List related stuff */
334 #define LIST_HEAD 0
335 #define LIST_TAIL 1
336 #define ZSET_MIN 0
337 #define ZSET_MAX 1
338 
339 /* Sort operations */
340 #define SORT_OP_GET 0
341 
342 /* Log levels */
343 #define LL_DEBUG 0
344 #define LL_VERBOSE 1
345 #define LL_NOTICE 2
346 #define LL_WARNING 3
347 #define LL_RAW (1<<10) /* Modifier to log without timestamp */
348 
349 /* Supervision options */
350 #define SUPERVISED_NONE 0
351 #define SUPERVISED_AUTODETECT 1
352 #define SUPERVISED_SYSTEMD 2
353 #define SUPERVISED_UPSTART 3
354 
355 /* Anti-warning macro... */
356 #define UNUSED(V) ((void) V)
357 
358 #define ZSKIPLIST_MAXLEVEL 32 /* Should be enough for 2^64 elements */
359 #define ZSKIPLIST_P 0.25      /* Skiplist P = 1/4 */
360 
361 /* Append only defines */
362 #define AOF_FSYNC_NO 0
363 #define AOF_FSYNC_ALWAYS 1
364 #define AOF_FSYNC_EVERYSEC 2
365 
366 /* Replication diskless load defines */
367 #define REPL_DISKLESS_LOAD_DISABLED 0
368 #define REPL_DISKLESS_LOAD_WHEN_DB_EMPTY 1
369 #define REPL_DISKLESS_LOAD_SWAPDB 2
370 
371 /* TLS Client Authentication */
372 #define TLS_CLIENT_AUTH_NO 0
373 #define TLS_CLIENT_AUTH_YES 1
374 #define TLS_CLIENT_AUTH_OPTIONAL 2
375 
376 /* Sets operations codes */
377 #define SET_OP_UNION 0
378 #define SET_OP_DIFF 1
379 #define SET_OP_INTER 2
380 
381 /* oom-score-adj defines */
382 #define OOM_SCORE_ADJ_NO 0
383 #define OOM_SCORE_RELATIVE 1
384 #define OOM_SCORE_ADJ_ABSOLUTE 2
385 
386 /* Redis maxmemory strategies. Instead of using just incremental number
387  * for this defines, we use a set of flags so that testing for certain
388  * properties common to multiple policies is faster. */
389 #define MAXMEMORY_FLAG_LRU (1<<0)
390 #define MAXMEMORY_FLAG_LFU (1<<1)
391 #define MAXMEMORY_FLAG_ALLKEYS (1<<2)
392 #define MAXMEMORY_FLAG_NO_SHARED_INTEGERS \
393     (MAXMEMORY_FLAG_LRU|MAXMEMORY_FLAG_LFU)
394 
395 #define MAXMEMORY_VOLATILE_LRU ((0<<8)|MAXMEMORY_FLAG_LRU)
396 #define MAXMEMORY_VOLATILE_LFU ((1<<8)|MAXMEMORY_FLAG_LFU)
397 #define MAXMEMORY_VOLATILE_TTL (2<<8)
398 #define MAXMEMORY_VOLATILE_RANDOM (3<<8)
399 #define MAXMEMORY_ALLKEYS_LRU ((4<<8)|MAXMEMORY_FLAG_LRU|MAXMEMORY_FLAG_ALLKEYS)
400 #define MAXMEMORY_ALLKEYS_LFU ((5<<8)|MAXMEMORY_FLAG_LFU|MAXMEMORY_FLAG_ALLKEYS)
401 #define MAXMEMORY_ALLKEYS_RANDOM ((6<<8)|MAXMEMORY_FLAG_ALLKEYS)
402 #define MAXMEMORY_NO_EVICTION (7<<8)
403 
404 /* Units */
405 #define UNIT_SECONDS 0
406 #define UNIT_MILLISECONDS 1
407 
408 /* SHUTDOWN flags */
409 #define SHUTDOWN_NOFLAGS 0      /* No flags. */
410 #define SHUTDOWN_SAVE 1         /* Force SAVE on SHUTDOWN even if no save
411                                    points are configured. */
412 #define SHUTDOWN_NOSAVE 2       /* Don't SAVE on SHUTDOWN. */
413 
414 /* Command call flags, see call() function */
415 #define CMD_CALL_NONE 0
416 #define CMD_CALL_SLOWLOG (1<<0)
417 #define CMD_CALL_STATS (1<<1)
418 #define CMD_CALL_PROPAGATE_AOF (1<<2)
419 #define CMD_CALL_PROPAGATE_REPL (1<<3)
420 #define CMD_CALL_PROPAGATE (CMD_CALL_PROPAGATE_AOF|CMD_CALL_PROPAGATE_REPL)
421 #define CMD_CALL_FULL (CMD_CALL_SLOWLOG | CMD_CALL_STATS | CMD_CALL_PROPAGATE)
422 #define CMD_CALL_NOWRAP (1<<4)  /* Don't wrap also propagate array into
423                                    MULTI/EXEC: the caller will handle it.  */
424 
425 /* Command propagation flags, see propagate() function */
426 #define PROPAGATE_NONE 0
427 #define PROPAGATE_AOF 1
428 #define PROPAGATE_REPL 2
429 
430 /* RDB active child save type. */
431 #define RDB_CHILD_TYPE_NONE 0
432 #define RDB_CHILD_TYPE_DISK 1     /* RDB is written to disk. */
433 #define RDB_CHILD_TYPE_SOCKET 2   /* RDB is written to slave socket. */
434 
435 /* Keyspace changes notification classes. Every class is associated with a
436  * character for configuration purposes. */
437 #define NOTIFY_KEYSPACE (1<<0)    /* K */
438 #define NOTIFY_KEYEVENT (1<<1)    /* E */
439 #define NOTIFY_GENERIC (1<<2)     /* g */
440 #define NOTIFY_STRING (1<<3)      /* $ */
441 #define NOTIFY_LIST (1<<4)        /* l */
442 #define NOTIFY_SET (1<<5)         /* s */
443 #define NOTIFY_HASH (1<<6)        /* h */
444 #define NOTIFY_ZSET (1<<7)        /* z */
445 #define NOTIFY_EXPIRED (1<<8)     /* x */
446 #define NOTIFY_EVICTED (1<<9)     /* e */
447 #define NOTIFY_STREAM (1<<10)     /* t */
448 #define NOTIFY_KEY_MISS (1<<11)   /* m (Note: This one is excluded from NOTIFY_ALL on purpose) */
449 #define NOTIFY_LOADED (1<<12)     /* module only key space notification, indicate a key loaded from rdb */
450 #define NOTIFY_ALL (NOTIFY_GENERIC | NOTIFY_STRING | NOTIFY_LIST | NOTIFY_SET | NOTIFY_HASH | NOTIFY_ZSET | NOTIFY_EXPIRED | NOTIFY_EVICTED | NOTIFY_STREAM) /* A flag */
451 
452 /* Get the first bind addr or NULL */
453 #define NET_FIRST_BIND_ADDR (server.bindaddr_count ? server.bindaddr[0] : NULL)
454 
455 /* Using the following macro you can run code inside serverCron() with the
456  * specified period, specified in milliseconds.
457  * The actual resolution depends on server.hz. */
458 #define run_with_period(_ms_) if ((_ms_ <= 1000/server.hz) || !(server.cronloops%((_ms_)/(1000/server.hz))))
459 
460 /* We can print the stacktrace, so our assert is defined this way: */
461 #define serverAssertWithInfo(_c,_o,_e) ((_e)?(void)0 : (_serverAssertWithInfo(_c,_o,#_e,__FILE__,__LINE__),_exit(1)))
462 #define serverAssert(_e) ((_e)?(void)0 : (_serverAssert(#_e,__FILE__,__LINE__),_exit(1)))
463 #define serverPanic(...) _serverPanic(__FILE__,__LINE__,__VA_ARGS__),_exit(1)
464 
465 /*-----------------------------------------------------------------------------
466  * Data types
467  *----------------------------------------------------------------------------*/
468 
469 /* A redis object, that is a type able to hold a string / list / set */
470 
471 /* The actual Redis Object */
472 #define OBJ_STRING 0    /* String object. */
473 #define OBJ_LIST 1      /* List object. */
474 #define OBJ_SET 2       /* Set object. */
475 #define OBJ_ZSET 3      /* Sorted set object. */
476 #define OBJ_HASH 4      /* Hash object. */
477 
478 /* The "module" object type is a special one that signals that the object
479  * is one directly managed by a Redis module. In this case the value points
480  * to a moduleValue struct, which contains the object value (which is only
481  * handled by the module itself) and the RedisModuleType struct which lists
482  * function pointers in order to serialize, deserialize, AOF-rewrite and
483  * free the object.
484  *
485  * Inside the RDB file, module types are encoded as OBJ_MODULE followed
486  * by a 64 bit module type ID, which has a 54 bits module-specific signature
487  * in order to dispatch the loading to the right module, plus a 10 bits
488  * encoding version. */
489 #define OBJ_MODULE 5    /* Module object. */
490 #define OBJ_STREAM 6    /* Stream object. */
491 
492 /* Extract encver / signature from a module type ID. */
493 #define REDISMODULE_TYPE_ENCVER_BITS 10
494 #define REDISMODULE_TYPE_ENCVER_MASK ((1<<REDISMODULE_TYPE_ENCVER_BITS)-1)
495 #define REDISMODULE_TYPE_ENCVER(id) (id & REDISMODULE_TYPE_ENCVER_MASK)
496 #define REDISMODULE_TYPE_SIGN(id) ((id & ~((uint64_t)REDISMODULE_TYPE_ENCVER_MASK)) >>REDISMODULE_TYPE_ENCVER_BITS)
497 
498 /* Bit flags for moduleTypeAuxSaveFunc */
499 #define REDISMODULE_AUX_BEFORE_RDB (1<<0)
500 #define REDISMODULE_AUX_AFTER_RDB (1<<1)
501 
502 struct RedisModule;
503 struct RedisModuleIO;
504 struct RedisModuleDigest;
505 struct RedisModuleCtx;
506 struct redisObject;
507 
508 /* Each module type implementation should export a set of methods in order
509  * to serialize and deserialize the value in the RDB file, rewrite the AOF
510  * log, create the digest for "DEBUG DIGEST", and free the value when a key
511  * is deleted. */
512 typedef void *(*moduleTypeLoadFunc)(struct RedisModuleIO *io, int encver);
513 typedef void (*moduleTypeSaveFunc)(struct RedisModuleIO *io, void *value);
514 typedef int (*moduleTypeAuxLoadFunc)(struct RedisModuleIO *rdb, int encver, int when);
515 typedef void (*moduleTypeAuxSaveFunc)(struct RedisModuleIO *rdb, int when);
516 typedef void (*moduleTypeRewriteFunc)(struct RedisModuleIO *io, struct redisObject *key, void *value);
517 typedef void (*moduleTypeDigestFunc)(struct RedisModuleDigest *digest, void *value);
518 typedef size_t (*moduleTypeMemUsageFunc)(const void *value);
519 typedef void (*moduleTypeFreeFunc)(void *value);
520 
521 /* A callback that is called when the client authentication changes. This
522  * needs to be exposed since you can't cast a function pointer to (void *) */
523 typedef void (*RedisModuleUserChangedFunc) (uint64_t client_id, void *privdata);
524 
525 
526 /* The module type, which is referenced in each value of a given type, defines
527  * the methods and links to the module exporting the type. */
528 typedef struct RedisModuleType {
529     uint64_t id; /* Higher 54 bits of type ID + 10 lower bits of encoding ver. */
530     struct RedisModule *module;
531     moduleTypeLoadFunc rdb_load;
532     moduleTypeSaveFunc rdb_save;
533     moduleTypeRewriteFunc aof_rewrite;
534     moduleTypeMemUsageFunc mem_usage;
535     moduleTypeDigestFunc digest;
536     moduleTypeFreeFunc free;
537     moduleTypeAuxLoadFunc aux_load;
538     moduleTypeAuxSaveFunc aux_save;
539     int aux_save_triggers;
540     char name[10]; /* 9 bytes name + null term. Charset: A-Z a-z 0-9 _- */
541 } moduleType;
542 
543 /* In Redis objects 'robj' structures of type OBJ_MODULE, the value pointer
544  * is set to the following structure, referencing the moduleType structure
545  * in order to work with the value, and at the same time providing a raw
546  * pointer to the value, as created by the module commands operating with
547  * the module type.
548  *
549  * So for example in order to free such a value, it is possible to use
550  * the following code:
551  *
552  *  if (robj->type == OBJ_MODULE) {
553  *      moduleValue *mt = robj->ptr;
554  *      mt->type->free(mt->value);
555  *      zfree(mt); // We need to release this in-the-middle struct as well.
556  *  }
557  */
558 typedef struct moduleValue {
559     moduleType *type;
560     void *value;
561 } moduleValue;
562 
563 /* This is a wrapper for the 'rio' streams used inside rdb.c in Redis, so that
564  * the user does not have to take the total count of the written bytes nor
565  * to care about error conditions. */
566 typedef struct RedisModuleIO {
567     size_t bytes;       /* Bytes read / written so far. */
568     rio *rio;           /* Rio stream. */
569     moduleType *type;   /* Module type doing the operation. */
570     int error;          /* True if error condition happened. */
571     int ver;            /* Module serialization version: 1 (old),
572                          * 2 (current version with opcodes annotation). */
573     struct RedisModuleCtx *ctx; /* Optional context, see RM_GetContextFromIO()*/
574     struct redisObject *key;    /* Optional name of key processed */
575 } RedisModuleIO;
576 
577 /* Macro to initialize an IO context. Note that the 'ver' field is populated
578  * inside rdb.c according to the version of the value to load. */
579 #define moduleInitIOContext(iovar,mtype,rioptr,keyptr) do { \
580     iovar.rio = rioptr; \
581     iovar.type = mtype; \
582     iovar.bytes = 0; \
583     iovar.error = 0; \
584     iovar.ver = 0; \
585     iovar.key = keyptr; \
586     iovar.ctx = NULL; \
587 } while(0);
588 
589 /* This is a structure used to export DEBUG DIGEST capabilities to Redis
590  * modules. We want to capture both the ordered and unordered elements of
591  * a data structure, so that a digest can be created in a way that correctly
592  * reflects the values. See the DEBUG DIGEST command implementation for more
593  * background. */
594 typedef struct RedisModuleDigest {
595     unsigned char o[20];    /* Ordered elements. */
596     unsigned char x[20];    /* Xored elements. */
597 } RedisModuleDigest;
598 
599 /* Just start with a digest composed of all zero bytes. */
600 #define moduleInitDigestContext(mdvar) do { \
601     memset(mdvar.o,0,sizeof(mdvar.o)); \
602     memset(mdvar.x,0,sizeof(mdvar.x)); \
603 } while(0);
604 
605 /* Objects encoding. Some kind of objects like Strings and Hashes can be
606  * internally represented in multiple ways. The 'encoding' field of the object
607  * is set to one of this fields for this object. */
608 #define OBJ_ENCODING_RAW 0     /* Raw representation */
609 #define OBJ_ENCODING_INT 1     /* Encoded as integer */
610 #define OBJ_ENCODING_HT 2      /* Encoded as hash table */
611 #define OBJ_ENCODING_ZIPMAP 3  /* Encoded as zipmap */
612 #define OBJ_ENCODING_LINKEDLIST 4 /* No longer used: old list encoding. */
613 #define OBJ_ENCODING_ZIPLIST 5 /* Encoded as ziplist */
614 #define OBJ_ENCODING_INTSET 6  /* Encoded as intset */
615 #define OBJ_ENCODING_SKIPLIST 7  /* Encoded as skiplist */
616 #define OBJ_ENCODING_EMBSTR 8  /* Embedded sds string encoding */
617 #define OBJ_ENCODING_QUICKLIST 9 /* Encoded as linked list of ziplists */
618 #define OBJ_ENCODING_STREAM 10 /* Encoded as a radix tree of listpacks */
619 
620 #define LRU_BITS 24
621 #define LRU_CLOCK_MAX ((1<<LRU_BITS)-1) /* Max value of obj->lru */
622 #define LRU_CLOCK_RESOLUTION 1000 /* LRU clock resolution in ms */
623 
624 #define OBJ_SHARED_REFCOUNT INT_MAX     /* Global object never destroyed. */
625 #define OBJ_STATIC_REFCOUNT (INT_MAX-1) /* Object allocated in the stack. */
626 #define OBJ_FIRST_SPECIAL_REFCOUNT OBJ_STATIC_REFCOUNT
627 typedef struct redisObject {
628     unsigned type:4;
629     unsigned encoding:4;
630     unsigned lru:LRU_BITS; /* LRU time (relative to global lru_clock) or
631                             * LFU data (least significant 8 bits frequency
632                             * and most significant 16 bits access time). */
633     int refcount;
634     void *ptr;
635 } robj;
636 
637 /* The a string name for an object's type as listed above
638  * Native types are checked against the OBJ_STRING, OBJ_LIST, OBJ_* defines,
639  * and Module types have their registered name returned. */
640 char *getObjectTypeName(robj*);
641 
642 /* Macro used to initialize a Redis object allocated on the stack.
643  * Note that this macro is taken near the structure definition to make sure
644  * we'll update it when the structure is changed, to avoid bugs like
645  * bug #85 introduced exactly in this way. */
646 #define initStaticStringObject(_var,_ptr) do { \
647     _var.refcount = OBJ_STATIC_REFCOUNT; \
648     _var.type = OBJ_STRING; \
649     _var.encoding = OBJ_ENCODING_RAW; \
650     _var.ptr = _ptr; \
651 } while(0)
652 
653 struct evictionPoolEntry; /* Defined in evict.c */
654 
655 /* This structure is used in order to represent the output buffer of a client,
656  * which is actually a linked list of blocks like that, that is: client->reply. */
657 typedef struct clientReplyBlock {
658     size_t size, used;
659     char buf[];
660 } clientReplyBlock;
661 
662 /* Redis database representation. There are multiple databases identified
663  * by integers from 0 (the default database) up to the max configured
664  * database. The database number is the 'id' field in the structure. */
665 typedef struct redisDb {
666     dict *dict;                 /* The keyspace for this DB */
667     dict *expires;              /* Timeout of keys with a timeout set */
668     dict *blocking_keys;        /* Keys with clients waiting for data (BLPOP)*/
669     dict *ready_keys;           /* Blocked keys that received a PUSH */
670     dict *watched_keys;         /* WATCHED keys for MULTI/EXEC CAS */
671     int id;                     /* Database ID */
672     long long avg_ttl;          /* Average TTL, just for stats */
673     unsigned long expires_cursor; /* Cursor of the active expire cycle. */
674     list *defrag_later;         /* List of key names to attempt to defrag one by one, gradually. */
675 } redisDb;
676 
677 /* Declare database backup that include redis main DBs and slots to keys map.
678  * Definition is in db.c. We can't define it here since we define CLUSTER_SLOTS
679  * in cluster.h. */
680 typedef struct dbBackup dbBackup;
681 
682 /* Client MULTI/EXEC state */
683 typedef struct multiCmd {
684     robj **argv;
685     int argc;
686     struct redisCommand *cmd;
687 } multiCmd;
688 
689 typedef struct multiState {
690     multiCmd *commands;     /* Array of MULTI commands */
691     int count;              /* Total number of MULTI commands */
692     int cmd_flags;          /* The accumulated command flags OR-ed together.
693                                So if at least a command has a given flag, it
694                                will be set in this field. */
695     int cmd_inv_flags;      /* Same as cmd_flags, OR-ing the ~flags. so that it
696                                is possible to know if all the commands have a
697                                certain flag. */
698     int minreplicas;        /* MINREPLICAS for synchronous replication */
699     time_t minreplicas_timeout; /* MINREPLICAS timeout as unixtime. */
700 } multiState;
701 
702 /* This structure holds the blocking operation state for a client.
703  * The fields used depend on client->btype. */
704 typedef struct blockingState {
705     /* Generic fields. */
706     mstime_t timeout;       /* Blocking operation timeout. If UNIX current time
707                              * is > timeout then the operation timed out. */
708 
709     /* BLOCKED_LIST, BLOCKED_ZSET and BLOCKED_STREAM */
710     dict *keys;             /* The keys we are waiting to terminate a blocking
711                              * operation such as BLPOP or XREAD. Or NULL. */
712     robj *target;           /* The key that should receive the element,
713                              * for BRPOPLPUSH. */
714 
715     /* BLOCK_STREAM */
716     size_t xread_count;     /* XREAD COUNT option. */
717     robj *xread_group;      /* XREADGROUP group name. */
718     robj *xread_consumer;   /* XREADGROUP consumer name. */
719     mstime_t xread_retry_time, xread_retry_ttl;
720     int xread_group_noack;
721 
722     /* BLOCKED_WAIT */
723     int numreplicas;        /* Number of replicas we are waiting for ACK. */
724     long long reploffset;   /* Replication offset to reach. */
725 
726     /* BLOCKED_MODULE */
727     void *module_blocked_handle; /* RedisModuleBlockedClient structure.
728                                     which is opaque for the Redis core, only
729                                     handled in module.c. */
730 } blockingState;
731 
732 /* The following structure represents a node in the server.ready_keys list,
733  * where we accumulate all the keys that had clients blocked with a blocking
734  * operation such as B[LR]POP, but received new data in the context of the
735  * last executed command.
736  *
737  * After the execution of every command or script, we run this list to check
738  * if as a result we should serve data to clients blocked, unblocking them.
739  * Note that server.ready_keys will not have duplicates as there dictionary
740  * also called ready_keys in every structure representing a Redis database,
741  * where we make sure to remember if a given key was already added in the
742  * server.ready_keys list. */
743 typedef struct readyList {
744     redisDb *db;
745     robj *key;
746 } readyList;
747 
748 /* This structure represents a Redis user. This is useful for ACLs, the
749  * user is associated to the connection after the connection is authenticated.
750  * If there is no associated user, the connection uses the default user. */
751 #define USER_COMMAND_BITS_COUNT 1024    /* The total number of command bits
752                                            in the user structure. The last valid
753                                            command ID we can set in the user
754                                            is USER_COMMAND_BITS_COUNT-1. */
755 #define USER_FLAG_ENABLED (1<<0)        /* The user is active. */
756 #define USER_FLAG_DISABLED (1<<1)       /* The user is disabled. */
757 #define USER_FLAG_ALLKEYS (1<<2)        /* The user can mention any key. */
758 #define USER_FLAG_ALLCOMMANDS (1<<3)    /* The user can run all commands. */
759 #define USER_FLAG_NOPASS      (1<<4)    /* The user requires no password, any
760                                            provided password will work. For the
761                                            default user, this also means that
762                                            no AUTH is needed, and every
763                                            connection is immediately
764                                            authenticated. */
765 typedef struct {
766     sds name;       /* The username as an SDS string. */
767     uint64_t flags; /* See USER_FLAG_* */
768 
769     /* The bit in allowed_commands is set if this user has the right to
770      * execute this command. In commands having subcommands, if this bit is
771      * set, then all the subcommands are also available.
772      *
773      * If the bit for a given command is NOT set and the command has
774      * subcommands, Redis will also check allowed_subcommands in order to
775      * understand if the command can be executed. */
776     uint64_t allowed_commands[USER_COMMAND_BITS_COUNT/64];
777 
778     /* This array points, for each command ID (corresponding to the command
779      * bit set in allowed_commands), to an array of SDS strings, terminated by
780      * a NULL pointer, with all the sub commands that can be executed for
781      * this command. When no subcommands matching is used, the field is just
782      * set to NULL to avoid allocating USER_COMMAND_BITS_COUNT pointers. */
783     sds **allowed_subcommands;
784     list *passwords; /* A list of SDS valid passwords for this user. */
785     list *patterns;  /* A list of allowed key patterns. If this field is NULL
786                         the user cannot mention any key in a command, unless
787                         the flag ALLKEYS is set in the user. */
788 } user;
789 
790 /* With multiplexing we need to take per-client state.
791  * Clients are taken in a linked list. */
792 
793 #define CLIENT_ID_AOF (UINT64_MAX) /* Reserved ID for the AOF client. If you
794                                       need more reserved IDs use UINT64_MAX-1,
795                                       -2, ... and so forth. */
796 
797 typedef struct client {
798     uint64_t id;            /* Client incremental unique ID. */
799     connection *conn;
800     int resp;               /* RESP protocol version. Can be 2 or 3. */
801     redisDb *db;            /* Pointer to currently SELECTed DB. */
802     robj *name;             /* As set by CLIENT SETNAME. */
803     sds querybuf;           /* Buffer we use to accumulate client queries. */
804     size_t qb_pos;          /* The position we have read in querybuf. */
805     sds pending_querybuf;   /* If this client is flagged as master, this buffer
806                                represents the yet not applied portion of the
807                                replication stream that we are receiving from
808                                the master. */
809     size_t querybuf_peak;   /* Recent (100ms or more) peak of querybuf size. */
810     int argc;               /* Num of arguments of current command. */
811     robj **argv;            /* Arguments of current command. */
812     size_t argv_len_sum;    /* Sum of lengths of objects in argv list. */
813     struct redisCommand *cmd, *lastcmd;  /* Last command executed. */
814     user *user;             /* User associated with this connection. If the
815                                user is set to NULL the connection can do
816                                anything (admin). */
817     int reqtype;            /* Request protocol type: PROTO_REQ_* */
818     int multibulklen;       /* Number of multi bulk arguments left to read. */
819     long bulklen;           /* Length of bulk argument in multi bulk request. */
820     list *reply;            /* List of reply objects to send to the client. */
821     unsigned long long reply_bytes; /* Tot bytes of objects in reply list. */
822     size_t sentlen;         /* Amount of bytes already sent in the current
823                                buffer or object being sent. */
824     time_t ctime;           /* Client creation time. */
825     time_t lastinteraction; /* Time of the last interaction, used for timeout */
826     time_t obuf_soft_limit_reached_time;
827     uint64_t flags;         /* Client flags: CLIENT_* macros. */
828     int authenticated;      /* Needed when the default user requires auth. */
829     int replstate;          /* Replication state if this is a slave. */
830     int repl_put_online_on_ack; /* Install slave write handler on first ACK. */
831     int repldbfd;           /* Replication DB file descriptor. */
832     off_t repldboff;        /* Replication DB file offset. */
833     off_t repldbsize;       /* Replication DB file size. */
834     sds replpreamble;       /* Replication DB preamble. */
835     long long read_reploff; /* Read replication offset if this is a master. */
836     long long reploff;      /* Applied replication offset if this is a master. */
837     long long repl_ack_off; /* Replication ack offset, if this is a slave. */
838     long long repl_ack_time;/* Replication ack time, if this is a slave. */
839     long long repl_last_partial_write; /* The last time the server did a partial write from the RDB child pipe to this replica  */
840     long long psync_initial_offset; /* FULLRESYNC reply offset other slaves
841                                        copying this slave output buffer
842                                        should use. */
843     char replid[CONFIG_RUN_ID_SIZE+1]; /* Master replication ID (if master). */
844     int slave_listening_port; /* As configured with: REPLCONF listening-port */
845     char slave_ip[NET_IP_STR_LEN]; /* Optionally given by REPLCONF ip-address */
846     int slave_capa;         /* Slave capabilities: SLAVE_CAPA_* bitwise OR. */
847     multiState mstate;      /* MULTI/EXEC state */
848     int btype;              /* Type of blocking op if CLIENT_BLOCKED. */
849     blockingState bpop;     /* blocking state */
850     long long woff;         /* Last write global replication offset. */
851     list *watched_keys;     /* Keys WATCHED for MULTI/EXEC CAS */
852     dict *pubsub_channels;  /* channels a client is interested in (SUBSCRIBE) */
853     list *pubsub_patterns;  /* patterns a client is interested in (SUBSCRIBE) */
854     sds peerid;             /* Cached peer ID. */
855     listNode *client_list_node; /* list node in client list */
856     RedisModuleUserChangedFunc auth_callback; /* Module callback to execute
857                                                * when the authenticated user
858                                                * changes. */
859     void *auth_callback_privdata; /* Private data that is passed when the auth
860                                    * changed callback is executed. Opaque for
861                                    * Redis Core. */
862     void *auth_module;      /* The module that owns the callback, which is used
863                              * to disconnect the client if the module is
864                              * unloaded for cleanup. Opaque for Redis Core.*/
865 
866     /* If this client is in tracking mode and this field is non zero,
867      * invalidation messages for keys fetched by this client will be send to
868      * the specified client ID. */
869     uint64_t client_tracking_redirection;
870     rax *client_tracking_prefixes; /* A dictionary of prefixes we are already
871                                       subscribed to in BCAST mode, in the
872                                       context of client side caching. */
873     /* In clientsCronTrackClientsMemUsage() we track the memory usage of
874      * each client and add it to the sum of all the clients of a given type,
875      * however we need to remember what was the old contribution of each
876      * client, and in which categoty the client was, in order to remove it
877      * before adding it the new value. */
878     uint64_t client_cron_last_memory_usage;
879     int      client_cron_last_memory_type;
880     /* Response buffer */
881     int bufpos;
882     char buf[PROTO_REPLY_CHUNK_BYTES];
883 } client;
884 
885 struct saveparam {
886     time_t seconds;
887     int changes;
888 };
889 
890 struct moduleLoadQueueEntry {
891     sds path;
892     int argc;
893     robj **argv;
894 };
895 
896 struct sharedObjectsStruct {
897     robj *crlf, *ok, *err, *emptybulk, *czero, *cone, *pong, *space,
898     *colon, *queued, *null[4], *nullarray[4], *emptymap[4], *emptyset[4],
899     *emptyarray, *wrongtypeerr, *nokeyerr, *syntaxerr, *sameobjecterr,
900     *outofrangeerr, *noscripterr, *loadingerr, *slowscripterr, *bgsaveerr,
901     *masterdownerr, *roslaveerr, *execaborterr, *noautherr, *noreplicaserr,
902     *busykeyerr, *oomerr, *plus, *messagebulk, *pmessagebulk, *subscribebulk,
903     *unsubscribebulk, *psubscribebulk, *punsubscribebulk, *del, *unlink,
904     *rpop, *lpop, *lpush, *rpoplpush, *zpopmin, *zpopmax, *emptyscan,
905     *multi, *exec,
906     *select[PROTO_SHARED_SELECT_CMDS],
907     *integers[OBJ_SHARED_INTEGERS],
908     *mbulkhdr[OBJ_SHARED_BULKHDR_LEN], /* "*<value>\r\n" */
909     *bulkhdr[OBJ_SHARED_BULKHDR_LEN];  /* "$<value>\r\n" */
910     sds minstring, maxstring;
911 };
912 
913 /* ZSETs use a specialized version of Skiplists */
914 typedef struct zskiplistNode {
915     sds ele;
916     double score;
917     struct zskiplistNode *backward;
918     struct zskiplistLevel {
919         struct zskiplistNode *forward;
920         unsigned long span;
921     } level[];
922 } zskiplistNode;
923 
924 typedef struct zskiplist {
925     struct zskiplistNode *header, *tail;
926     unsigned long length;
927     int level;
928 } zskiplist;
929 
930 typedef struct zset {
931     dict *dict;
932     zskiplist *zsl;
933 } zset;
934 
935 typedef struct clientBufferLimitsConfig {
936     unsigned long long hard_limit_bytes;
937     unsigned long long soft_limit_bytes;
938     time_t soft_limit_seconds;
939 } clientBufferLimitsConfig;
940 
941 extern clientBufferLimitsConfig clientBufferLimitsDefaults[CLIENT_TYPE_OBUF_COUNT];
942 
943 /* The redisOp structure defines a Redis Operation, that is an instance of
944  * a command with an argument vector, database ID, propagation target
945  * (PROPAGATE_*), and command pointer.
946  *
947  * Currently only used to additionally propagate more commands to AOF/Replication
948  * after the propagation of the executed command. */
949 typedef struct redisOp {
950     robj **argv;
951     int argc, dbid, target;
952     struct redisCommand *cmd;
953 } redisOp;
954 
955 /* Defines an array of Redis operations. There is an API to add to this
956  * structure in an easy way.
957  *
958  * redisOpArrayInit();
959  * redisOpArrayAppend();
960  * redisOpArrayFree();
961  */
962 typedef struct redisOpArray {
963     redisOp *ops;
964     int numops;
965 } redisOpArray;
966 
967 /* This structure is returned by the getMemoryOverheadData() function in
968  * order to return memory overhead information. */
969 struct redisMemOverhead {
970     size_t peak_allocated;
971     size_t total_allocated;
972     size_t startup_allocated;
973     size_t repl_backlog;
974     size_t clients_slaves;
975     size_t clients_normal;
976     size_t aof_buffer;
977     size_t lua_caches;
978     size_t overhead_total;
979     size_t dataset;
980     size_t total_keys;
981     size_t bytes_per_key;
982     float dataset_perc;
983     float peak_perc;
984     float total_frag;
985     ssize_t total_frag_bytes;
986     float allocator_frag;
987     ssize_t allocator_frag_bytes;
988     float allocator_rss;
989     ssize_t allocator_rss_bytes;
990     float rss_extra;
991     size_t rss_extra_bytes;
992     size_t num_dbs;
993     struct {
994         size_t dbid;
995         size_t overhead_ht_main;
996         size_t overhead_ht_expires;
997     } *db;
998 };
999 
1000 /* This structure can be optionally passed to RDB save/load functions in
1001  * order to implement additional functionalities, by storing and loading
1002  * metadata to the RDB file.
1003  *
1004  * Currently the only use is to select a DB at load time, useful in
1005  * replication in order to make sure that chained slaves (slaves of slaves)
1006  * select the correct DB and are able to accept the stream coming from the
1007  * top-level master. */
1008 typedef struct rdbSaveInfo {
1009     /* Used saving and loading. */
1010     int repl_stream_db;  /* DB to select in server.master client. */
1011 
1012     /* Used only loading. */
1013     int repl_id_is_set;  /* True if repl_id field is set. */
1014     char repl_id[CONFIG_RUN_ID_SIZE+1];     /* Replication ID. */
1015     long long repl_offset;                  /* Replication offset. */
1016 } rdbSaveInfo;
1017 
1018 #define RDB_SAVE_INFO_INIT {-1,0,"000000000000000000000000000000",-1}
1019 
1020 struct malloc_stats {
1021     size_t zmalloc_used;
1022     size_t process_rss;
1023     size_t allocator_allocated;
1024     size_t allocator_active;
1025     size_t allocator_resident;
1026 };
1027 
1028 /*-----------------------------------------------------------------------------
1029  * TLS Context Configuration
1030  *----------------------------------------------------------------------------*/
1031 
1032 typedef struct redisTLSContextConfig {
1033     char *cert_file;
1034     char *key_file;
1035     char *dh_params_file;
1036     char *ca_cert_file;
1037     char *ca_cert_dir;
1038     char *protocols;
1039     char *ciphers;
1040     char *ciphersuites;
1041     int prefer_server_ciphers;
1042     int session_caching;
1043     int session_cache_size;
1044     int session_cache_timeout;
1045 } redisTLSContextConfig;
1046 
1047 /*-----------------------------------------------------------------------------
1048  * Global server state
1049  *----------------------------------------------------------------------------*/
1050 
1051 struct clusterState;
1052 
1053 /* AIX defines hz to __hz, we don't use this define and in order to allow
1054  * Redis build on AIX we need to undef it. */
1055 #ifdef _AIX
1056 #undef hz
1057 #endif
1058 
1059 #define CHILD_INFO_MAGIC 0xC17DDA7A12345678LL
1060 #define CHILD_TYPE_NONE 0
1061 #define CHILD_TYPE_RDB 1
1062 #define CHILD_TYPE_AOF 2
1063 #define CHILD_TYPE_LDB 3
1064 #define CHILD_TYPE_MODULE 4
1065 
1066 struct redisServer {
1067     /* General */
1068     pid_t pid;                  /* Main process pid. */
1069     pthread_t main_thread_id;         /* Main thread id */
1070     char *configfile;           /* Absolute config file path, or NULL */
1071     char *executable;           /* Absolute executable file path. */
1072     char **exec_argv;           /* Executable argv vector (copy). */
1073     int dynamic_hz;             /* Change hz value depending on # of clients. */
1074     int config_hz;              /* Configured HZ value. May be different than
1075                                    the actual 'hz' field value if dynamic-hz
1076                                    is enabled. */
1077     mode_t umask;               /* The umask value of the process on startup */
1078     int hz;                     /* serverCron() calls frequency in hertz */
1079     int in_fork_child;          /* indication that this is a fork child */
1080     redisDb *db;
1081     dict *commands;             /* Command table */
1082     dict *orig_commands;        /* Command table before command renaming. */
1083     aeEventLoop *el;
1084     _Atomic unsigned int lruclock; /* Clock for LRU eviction */
1085     volatile sig_atomic_t shutdown_asap; /* SHUTDOWN needed ASAP */
1086     int activerehashing;        /* Incremental rehash in serverCron() */
1087     int active_defrag_running;  /* Active defragmentation running (holds current scan aggressiveness) */
1088     char *pidfile;              /* PID file path */
1089     int arch_bits;              /* 32 or 64 depending on sizeof(long) */
1090     int cronloops;              /* Number of times the cron function run */
1091     char runid[CONFIG_RUN_ID_SIZE+1];  /* ID always different at every exec. */
1092     int sentinel_mode;          /* True if this instance is a Sentinel. */
1093     size_t initial_memory_usage; /* Bytes used after initialization. */
1094     int always_show_logo;       /* Show logo even for non-stdout logging. */
1095     char *ignore_warnings;      /* Config: warnings that should be ignored. */
1096     /* Modules */
1097     dict *moduleapi;            /* Exported core APIs dictionary for modules. */
1098     dict *sharedapi;            /* Like moduleapi but containing the APIs that
1099                                    modules share with each other. */
1100     list *loadmodule_queue;     /* List of modules to load at startup. */
1101     int module_blocked_pipe[2]; /* Pipe used to awake the event loop if a
1102                                    client blocked on a module command needs
1103                                    to be processed. */
1104     pid_t module_child_pid;     /* PID of module child */
1105     /* Networking */
1106     int port;                   /* TCP listening port */
1107     int tls_port;               /* TLS listening port */
1108     int tcp_backlog;            /* TCP listen() backlog */
1109     char *bindaddr[CONFIG_BINDADDR_MAX]; /* Addresses we should bind to */
1110     int bindaddr_count;         /* Number of addresses in server.bindaddr[] */
1111     char *unixsocket;           /* UNIX socket path */
1112     mode_t unixsocketperm;      /* UNIX socket permission */
1113     int ipfd[CONFIG_BINDADDR_MAX]; /* TCP socket file descriptors */
1114     int ipfd_count;             /* Used slots in ipfd[] */
1115     int tlsfd[CONFIG_BINDADDR_MAX]; /* TLS socket file descriptors */
1116     int tlsfd_count;            /* Used slots in tlsfd[] */
1117     int sofd;                   /* Unix socket file descriptor */
1118     int cfd[CONFIG_BINDADDR_MAX];/* Cluster bus listening socket */
1119     int cfd_count;              /* Used slots in cfd[] */
1120     list *clients;              /* List of active clients */
1121     list *clients_to_close;     /* Clients to close asynchronously */
1122     list *clients_pending_write; /* There is to write or install handler. */
1123     list *clients_pending_read;  /* Client has pending read socket buffers. */
1124     list *slaves, *monitors;    /* List of slaves and MONITORs */
1125     client *current_client;     /* Current client executing the command. */
1126     rax *clients_timeout_table; /* Radix tree for blocked clients timeouts. */
1127     long fixed_time_expire;     /* If > 0, expire keys against server.mstime. */
1128     rax *clients_index;         /* Active clients dictionary by client ID. */
1129     int clients_paused;         /* True if clients are currently paused */
1130     mstime_t clients_pause_end_time; /* Time when we undo clients_paused */
1131     char neterr[ANET_ERR_LEN];   /* Error buffer for anet.c */
1132     dict *migrate_cached_sockets;/* MIGRATE cached sockets */
1133     _Atomic uint64_t next_client_id; /* Next client unique ID. Incremental. */
1134     int protected_mode;         /* Don't accept external connections. */
1135     int gopher_enabled;         /* If true the server will reply to gopher
1136                                    queries. Will still serve RESP2 queries. */
1137     int io_threads_num;         /* Number of IO threads to use. */
1138     int io_threads_do_reads;    /* Read and parse from IO threads? */
1139     int io_threads_active;      /* Is IO threads currently active? */
1140     long long events_processed_while_blocked; /* processEventsWhileBlocked() */
1141 
1142     /* RDB / AOF loading information */
1143     volatile sig_atomic_t loading; /* We are loading data from disk if true */
1144     off_t loading_total_bytes;
1145     off_t loading_loaded_bytes;
1146     time_t loading_start_time;
1147     off_t loading_process_events_interval_bytes;
1148     /* Fast pointers to often looked up command */
1149     struct redisCommand *delCommand, *multiCommand, *lpushCommand,
1150                         *lpopCommand, *rpopCommand, *zpopminCommand,
1151                         *zpopmaxCommand, *sremCommand, *execCommand,
1152                         *expireCommand, *pexpireCommand, *xclaimCommand,
1153                         *xgroupCommand, *rpoplpushCommand;
1154     /* Fields used only for stats */
1155     time_t stat_starttime;          /* Server start time */
1156     long long stat_numcommands;     /* Number of processed commands */
1157     long long stat_numconnections;  /* Number of connections received */
1158     long long stat_expiredkeys;     /* Number of expired keys */
1159     double stat_expired_stale_perc; /* Percentage of keys probably expired */
1160     long long stat_expired_time_cap_reached_count; /* Early expire cylce stops.*/
1161     long long stat_expire_cycle_time_used; /* Cumulative microseconds used. */
1162     long long stat_evictedkeys;     /* Number of evicted keys (maxmemory) */
1163     long long stat_keyspace_hits;   /* Number of successful lookups of keys */
1164     long long stat_keyspace_misses; /* Number of failed lookups of keys */
1165     long long stat_active_defrag_hits;      /* number of allocations moved */
1166     long long stat_active_defrag_misses;    /* number of allocations scanned but not moved */
1167     long long stat_active_defrag_key_hits;  /* number of keys with moved allocations */
1168     long long stat_active_defrag_key_misses;/* number of keys scanned and not moved */
1169     long long stat_active_defrag_scanned;   /* number of dictEntries scanned */
1170     size_t stat_peak_memory;        /* Max used memory record */
1171     long long stat_fork_time;       /* Time needed to perform latest fork() */
1172     double stat_fork_rate;          /* Fork rate in GB/sec. */
1173     long long stat_rejected_conn;   /* Clients rejected because of maxclients */
1174     long long stat_sync_full;       /* Number of full resyncs with slaves. */
1175     long long stat_sync_partial_ok; /* Number of accepted PSYNC requests. */
1176     long long stat_sync_partial_err;/* Number of unaccepted PSYNC requests. */
1177     list *slowlog;                  /* SLOWLOG list of commands */
1178     long long slowlog_entry_id;     /* SLOWLOG current entry ID */
1179     long long slowlog_log_slower_than; /* SLOWLOG time limit (to get logged) */
1180     unsigned long slowlog_max_len;     /* SLOWLOG max number of items logged */
1181     struct malloc_stats cron_malloc_stats; /* sampled in serverCron(). */
1182     _Atomic long long stat_net_input_bytes; /* Bytes read from network. */
1183     _Atomic long long stat_net_output_bytes; /* Bytes written to network. */
1184     size_t stat_rdb_cow_bytes;      /* Copy on write bytes during RDB saving. */
1185     size_t stat_aof_cow_bytes;      /* Copy on write bytes during AOF rewrite. */
1186     size_t stat_module_cow_bytes;   /* Copy on write bytes during module fork. */
1187     uint64_t stat_clients_type_memory[CLIENT_TYPE_COUNT];/* Mem usage by type */
1188     long long stat_unexpected_error_replies; /* Number of unexpected (aof-loading, replica to master, etc.) error replies */
1189     long long stat_io_reads_processed; /* Number of read events processed by IO / Main threads */
1190     long long stat_io_writes_processed; /* Number of write events processed by IO / Main threads */
1191     _Atomic long long stat_total_reads_processed; /* Total number of read events processed */
1192     _Atomic long long stat_total_writes_processed; /* Total number of write events processed */
1193     /* The following two are used to track instantaneous metrics, like
1194      * number of operations per second, network traffic. */
1195     struct {
1196         long long last_sample_time; /* Timestamp of last sample in ms */
1197         long long last_sample_count;/* Count in last sample */
1198         long long samples[STATS_METRIC_SAMPLES];
1199         int idx;
1200     } inst_metric[STATS_METRIC_COUNT];
1201     /* Configuration */
1202     int verbosity;                  /* Loglevel in redis.conf */
1203     int maxidletime;                /* Client timeout in seconds */
1204     int tcpkeepalive;               /* Set SO_KEEPALIVE if non-zero. */
1205     int active_expire_enabled;      /* Can be disabled for testing purposes. */
1206     int active_expire_effort;       /* From 1 (default) to 10, active effort. */
1207     int active_defrag_enabled;
1208     int jemalloc_bg_thread;         /* Enable jemalloc background thread */
1209     size_t active_defrag_ignore_bytes; /* minimum amount of fragmentation waste to start active defrag */
1210     int active_defrag_threshold_lower; /* minimum percentage of fragmentation to start active defrag */
1211     int active_defrag_threshold_upper; /* maximum percentage of fragmentation at which we use maximum effort */
1212     int active_defrag_cycle_min;       /* minimal effort for defrag in CPU percentage */
1213     int active_defrag_cycle_max;       /* maximal effort for defrag in CPU percentage */
1214     unsigned long active_defrag_max_scan_fields; /* maximum number of fields of set/hash/zset/list to process from within the main dict scan */
1215     _Atomic size_t client_max_querybuf_len; /* Limit for client query buffer length */
1216     int dbnum;                      /* Total number of configured DBs */
1217     int supervised;                 /* 1 if supervised, 0 otherwise. */
1218     int supervised_mode;            /* See SUPERVISED_* */
1219     int daemonize;                  /* True if running as a daemon */
1220     clientBufferLimitsConfig client_obuf_limits[CLIENT_TYPE_OBUF_COUNT];
1221     /* AOF persistence */
1222     int aof_enabled;                /* AOF configuration */
1223     int aof_state;                  /* AOF_(ON|OFF|WAIT_REWRITE) */
1224     int aof_fsync;                  /* Kind of fsync() policy */
1225     char *aof_filename;             /* Name of the AOF file */
1226     int aof_no_fsync_on_rewrite;    /* Don't fsync if a rewrite is in prog. */
1227     int aof_rewrite_perc;           /* Rewrite AOF if % growth is > M and... */
1228     off_t aof_rewrite_min_size;     /* the AOF file is at least N bytes. */
1229     off_t aof_rewrite_base_size;    /* AOF size on latest startup or rewrite. */
1230     off_t aof_current_size;         /* AOF current size. */
1231     off_t aof_fsync_offset;         /* AOF offset which is already synced to disk. */
1232     int aof_flush_sleep;            /* Micros to sleep before flush. (used by tests) */
1233     int aof_rewrite_scheduled;      /* Rewrite once BGSAVE terminates. */
1234     pid_t aof_child_pid;            /* PID if rewriting process */
1235     list *aof_rewrite_buf_blocks;   /* Hold changes during an AOF rewrite. */
1236     sds aof_buf;      /* AOF buffer, written before entering the event loop */
1237     int aof_fd;       /* File descriptor of currently selected AOF file */
1238     int aof_selected_db; /* Currently selected DB in AOF */
1239     time_t aof_flush_postponed_start; /* UNIX time of postponed AOF flush */
1240     time_t aof_last_fsync;            /* UNIX time of last fsync() */
1241     time_t aof_rewrite_time_last;   /* Time used by last AOF rewrite run. */
1242     time_t aof_rewrite_time_start;  /* Current AOF rewrite start time. */
1243     int aof_lastbgrewrite_status;   /* C_OK or C_ERR */
1244     unsigned long aof_delayed_fsync;  /* delayed AOF fsync() counter */
1245     int aof_rewrite_incremental_fsync;/* fsync incrementally while aof rewriting? */
1246     int rdb_save_incremental_fsync;   /* fsync incrementally while rdb saving? */
1247     int aof_last_write_status;      /* C_OK or C_ERR */
1248     int aof_last_write_errno;       /* Valid if aof_last_write_status is ERR */
1249     int aof_load_truncated;         /* Don't stop on unexpected AOF EOF. */
1250     int aof_use_rdb_preamble;       /* Use RDB preamble on AOF rewrites. */
1251     /* AOF pipes used to communicate between parent and child during rewrite. */
1252     int aof_pipe_write_data_to_child;
1253     int aof_pipe_read_data_from_parent;
1254     int aof_pipe_write_ack_to_parent;
1255     int aof_pipe_read_ack_from_child;
1256     int aof_pipe_write_ack_to_child;
1257     int aof_pipe_read_ack_from_parent;
1258     int aof_stop_sending_diff;     /* If true stop sending accumulated diffs
1259                                       to child process. */
1260     sds aof_child_diff;             /* AOF diff accumulator child side. */
1261     /* RDB persistence */
1262     long long dirty;                /* Changes to DB from the last save */
1263     long long dirty_before_bgsave;  /* Used to restore dirty on failed BGSAVE */
1264     pid_t rdb_child_pid;            /* PID of RDB saving child */
1265     struct saveparam *saveparams;   /* Save points array for RDB */
1266     int saveparamslen;              /* Number of saving points */
1267     char *rdb_filename;             /* Name of RDB file */
1268     int rdb_compression;            /* Use compression in RDB? */
1269     int rdb_checksum;               /* Use RDB checksum? */
1270     int rdb_del_sync_files;         /* Remove RDB files used only for SYNC if
1271                                        the instance does not use persistence. */
1272     time_t lastsave;                /* Unix time of last successful save */
1273     time_t lastbgsave_try;          /* Unix time of last attempted bgsave */
1274     time_t rdb_save_time_last;      /* Time used by last RDB save run. */
1275     time_t rdb_save_time_start;     /* Current RDB save start time. */
1276     int rdb_bgsave_scheduled;       /* BGSAVE when possible if true. */
1277     int rdb_child_type;             /* Type of save by active child. */
1278     int lastbgsave_status;          /* C_OK or C_ERR */
1279     int stop_writes_on_bgsave_err;  /* Don't allow writes if can't BGSAVE */
1280     int rdb_pipe_read;              /* RDB pipe used to transfer the rdb data */
1281                                     /* to the parent process in diskless repl. */
1282     int rdb_child_exit_pipe;        /* Used by the diskless parent allow child exit. */
1283     connection **rdb_pipe_conns;    /* Connections which are currently the */
1284     int rdb_pipe_numconns;          /* target of diskless rdb fork child. */
1285     int rdb_pipe_numconns_writing;  /* Number of rdb conns with pending writes. */
1286     char *rdb_pipe_buff;            /* In diskless replication, this buffer holds data */
1287     int rdb_pipe_bufflen;           /* that was read from the the rdb pipe. */
1288     int rdb_key_save_delay;         /* Delay in microseconds between keys while
1289                                      * writing the RDB. (for testings) */
1290     int key_load_delay;             /* Delay in microseconds between keys while
1291                                      * loading aof or rdb. (for testings) */
1292     /* Pipe and data structures for child -> parent info sharing. */
1293     int child_info_pipe[2];         /* Pipe used to write the child_info_data. */
1294     struct {
1295         int process_type;           /* AOF or RDB child? */
1296         size_t cow_size;            /* Copy on write size. */
1297         unsigned long long magic;   /* Magic value to make sure data is valid. */
1298     } child_info_data;
1299     /* Propagation of commands in AOF / replication */
1300     redisOpArray also_propagate;    /* Additional command to propagate. */
1301     /* Logging */
1302     char *logfile;                  /* Path of log file */
1303     int syslog_enabled;             /* Is syslog enabled? */
1304     char *syslog_ident;             /* Syslog ident */
1305     int syslog_facility;            /* Syslog facility */
1306     /* Replication (master) */
1307     char replid[CONFIG_RUN_ID_SIZE+1];  /* My current replication ID. */
1308     char replid2[CONFIG_RUN_ID_SIZE+1]; /* replid inherited from master*/
1309     long long master_repl_offset;   /* My current replication offset */
1310     long long second_replid_offset; /* Accept offsets up to this for replid2. */
1311     int slaveseldb;                 /* Last SELECTed DB in replication output */
1312     int repl_ping_slave_period;     /* Master pings the slave every N seconds */
1313     char *repl_backlog;             /* Replication backlog for partial syncs */
1314     long long repl_backlog_size;    /* Backlog circular buffer size */
1315     long long repl_backlog_histlen; /* Backlog actual data length */
1316     long long repl_backlog_idx;     /* Backlog circular buffer current offset,
1317                                        that is the next byte will'll write to.*/
1318     long long repl_backlog_off;     /* Replication "master offset" of first
1319                                        byte in the replication backlog buffer.*/
1320     time_t repl_backlog_time_limit; /* Time without slaves after the backlog
1321                                        gets released. */
1322     time_t repl_no_slaves_since;    /* We have no slaves since that time.
1323                                        Only valid if server.slaves len is 0. */
1324     int repl_min_slaves_to_write;   /* Min number of slaves to write. */
1325     int repl_min_slaves_max_lag;    /* Max lag of <count> slaves to write. */
1326     int repl_good_slaves_count;     /* Number of slaves with lag <= max_lag. */
1327     int repl_diskless_sync;         /* Master send RDB to slaves sockets directly. */
1328     int repl_diskless_load;         /* Slave parse RDB directly from the socket.
1329                                      * see REPL_DISKLESS_LOAD_* enum */
1330     int repl_diskless_sync_delay;   /* Delay to start a diskless repl BGSAVE. */
1331     /* Replication (slave) */
1332     char *masteruser;               /* AUTH with this user and masterauth with master */
1333     char *masterauth;               /* AUTH with this password with master */
1334     char *masterhost;               /* Hostname of master */
1335     int masterport;                 /* Port of master */
1336     int repl_timeout;               /* Timeout after N seconds of master idle */
1337     client *master;     /* Client that is master for this slave */
1338     client *cached_master; /* Cached master to be reused for PSYNC. */
1339     int repl_syncio_timeout; /* Timeout for synchronous I/O calls */
1340     int repl_state;          /* Replication status if the instance is a slave */
1341     off_t repl_transfer_size; /* Size of RDB to read from master during sync. */
1342     off_t repl_transfer_read; /* Amount of RDB read from master during sync. */
1343     off_t repl_transfer_last_fsync_off; /* Offset when we fsync-ed last time. */
1344     connection *repl_transfer_s;     /* Slave -> Master SYNC connection */
1345     int repl_transfer_fd;    /* Slave -> Master SYNC temp file descriptor */
1346     char *repl_transfer_tmpfile; /* Slave-> master SYNC temp file name */
1347     time_t repl_transfer_lastio; /* Unix time of the latest read, for timeout */
1348     int repl_serve_stale_data; /* Serve stale data when link is down? */
1349     int repl_slave_ro;          /* Slave is read only? */
1350     int repl_slave_ignore_maxmemory;    /* If true slaves do not evict. */
1351     time_t repl_down_since; /* Unix time at which link with master went down */
1352     int repl_disable_tcp_nodelay;   /* Disable TCP_NODELAY after SYNC? */
1353     int slave_priority;             /* Reported in INFO and used by Sentinel. */
1354     int slave_announce_port;        /* Give the master this listening port. */
1355     char *slave_announce_ip;        /* Give the master this ip address. */
1356     /* The following two fields is where we store master PSYNC replid/offset
1357      * while the PSYNC is in progress. At the end we'll copy the fields into
1358      * the server->master client structure. */
1359     char master_replid[CONFIG_RUN_ID_SIZE+1];  /* Master PSYNC runid. */
1360     long long master_initial_offset;           /* Master PSYNC offset. */
1361     int repl_slave_lazy_flush;          /* Lazy FLUSHALL before loading DB? */
1362     /* Replication script cache. */
1363     dict *repl_scriptcache_dict;        /* SHA1 all slaves are aware of. */
1364     list *repl_scriptcache_fifo;        /* First in, first out LRU eviction. */
1365     unsigned int repl_scriptcache_size; /* Max number of elements. */
1366     /* Synchronous replication. */
1367     list *clients_waiting_acks;         /* Clients waiting in WAIT command. */
1368     int get_ack_from_slaves;            /* If true we send REPLCONF GETACK. */
1369     /* Limits */
1370     unsigned int maxclients;            /* Max number of simultaneous clients */
1371     unsigned long long maxmemory;   /* Max number of memory bytes to use */
1372     int maxmemory_policy;           /* Policy for key eviction */
1373     int maxmemory_samples;          /* Precision of random sampling */
1374     int lfu_log_factor;             /* LFU logarithmic counter factor. */
1375     int lfu_decay_time;             /* LFU counter decay factor. */
1376     long long proto_max_bulk_len;   /* Protocol bulk length maximum size. */
1377     int oom_score_adj_base;         /* Base oom_score_adj value, as observed on startup */
1378     int oom_score_adj_values[CONFIG_OOM_COUNT];   /* Linux oom_score_adj configuration */
1379     int oom_score_adj;                            /* If true, oom_score_adj is managed */
1380     /* Blocked clients */
1381     unsigned int blocked_clients;   /* # of clients executing a blocking cmd.*/
1382     unsigned int blocked_clients_by_type[BLOCKED_NUM];
1383     list *unblocked_clients; /* list of clients to unblock before next loop */
1384     list *ready_keys;        /* List of readyList structures for BLPOP & co */
1385     /* Client side caching. */
1386     unsigned int tracking_clients;  /* # of clients with tracking enabled.*/
1387     size_t tracking_table_max_keys; /* Max number of keys in tracking table. */
1388     /* Sort parameters - qsort_r() is only available under BSD so we
1389      * have to take this state global, in order to pass it to sortCompare() */
1390     int sort_desc;
1391     int sort_alpha;
1392     int sort_bypattern;
1393     int sort_store;
1394     /* Zip structure config, see redis.conf for more information  */
1395     size_t hash_max_ziplist_entries;
1396     size_t hash_max_ziplist_value;
1397     size_t set_max_intset_entries;
1398     size_t zset_max_ziplist_entries;
1399     size_t zset_max_ziplist_value;
1400     size_t hll_sparse_max_bytes;
1401     size_t stream_node_max_bytes;
1402     long long stream_node_max_entries;
1403     /* List parameters */
1404     int list_max_ziplist_size;
1405     int list_compress_depth;
1406     /* time cache */
1407     _Atomic time_t unixtime;    /* Unix time sampled every cron cycle. */
1408     time_t timezone;            /* Cached timezone. As set by tzset(). */
1409     int daylight_active;        /* Currently in daylight saving time. */
1410     mstime_t mstime;            /* 'unixtime' in milliseconds. */
1411     ustime_t ustime;            /* 'unixtime' in microseconds. */
1412     /* Pubsub */
1413     dict *pubsub_channels;  /* Map channels to list of subscribed clients */
1414     list *pubsub_patterns;  /* A list of pubsub_patterns */
1415     dict *pubsub_patterns_dict;  /* A dict of pubsub_patterns */
1416     int notify_keyspace_events; /* Events to propagate via Pub/Sub. This is an
1417                                    xor of NOTIFY_... flags. */
1418     /* Cluster */
1419     int cluster_enabled;      /* Is cluster enabled? */
1420     mstime_t cluster_node_timeout; /* Cluster node timeout. */
1421     char *cluster_configfile; /* Cluster auto-generated config file name. */
1422     struct clusterState *cluster;  /* State of the cluster */
1423     int cluster_migration_barrier; /* Cluster replicas migration barrier. */
1424     int cluster_slave_validity_factor; /* Slave max data age for failover. */
1425     int cluster_require_full_coverage; /* If true, put the cluster down if
1426                                           there is at least an uncovered slot.*/
1427     int cluster_slave_no_failover;  /* Prevent slave from starting a failover
1428                                        if the master is in failure state. */
1429     char *cluster_announce_ip;  /* IP address to announce on cluster bus. */
1430     int cluster_announce_port;     /* base port to announce on cluster bus. */
1431     int cluster_announce_bus_port; /* bus port to announce on cluster bus. */
1432     int cluster_module_flags;      /* Set of flags that Redis modules are able
1433                                       to set in order to suppress certain
1434                                       native Redis Cluster features. Check the
1435                                       REDISMODULE_CLUSTER_FLAG_*. */
1436     int cluster_allow_reads_when_down; /* Are reads allowed when the cluster
1437                                         is down? */
1438     int cluster_config_file_lock_fd;   /* cluster config fd, will be flock */
1439     /* Scripting */
1440     lua_State *lua; /* The Lua interpreter. We use just one for all clients */
1441     client *lua_client;   /* The "fake client" to query Redis from Lua */
1442     client *lua_caller;   /* The client running EVAL right now, or NULL */
1443     char* lua_cur_script; /* SHA1 of the script currently running, or NULL */
1444     dict *lua_scripts;         /* A dictionary of SHA1 -> Lua scripts */
1445     unsigned long long lua_scripts_mem;  /* Cached scripts' memory + oh */
1446     mstime_t lua_time_limit;  /* Script timeout in milliseconds */
1447     mstime_t lua_time_start;  /* Start time of script, milliseconds time */
1448     int lua_write_dirty;  /* True if a write command was called during the
1449                              execution of the current script. */
1450     int lua_random_dirty; /* True if a random command was called during the
1451                              execution of the current script. */
1452     int lua_replicate_commands; /* True if we are doing single commands repl. */
1453     int lua_multi_emitted;/* True if we already propagated MULTI. */
1454     int lua_repl;         /* Script replication flags for redis.set_repl(). */
1455     int lua_timedout;     /* True if we reached the time limit for script
1456                              execution. */
1457     int lua_kill;         /* Kill the script if true. */
1458     int lua_always_replicate_commands; /* Default replication type. */
1459     int lua_oom;          /* OOM detected when script start? */
1460     /* Lazy free */
1461     int lazyfree_lazy_eviction;
1462     int lazyfree_lazy_expire;
1463     int lazyfree_lazy_server_del;
1464     int lazyfree_lazy_user_del;
1465     /* Latency monitor */
1466     long long latency_monitor_threshold;
1467     dict *latency_events;
1468     /* ACLs */
1469     char *acl_filename;     /* ACL Users file. NULL if not configured. */
1470     unsigned long acllog_max_len; /* Maximum length of the ACL LOG list. */
1471     sds requirepass;        /* Remember the cleartext password set with the
1472                                old "requirepass" directive for backward
1473                                compatibility with Redis <= 5. */
1474     /* Assert & bug reporting */
1475     const char *assert_failed;
1476     const char *assert_file;
1477     int assert_line;
1478     int bug_report_start; /* True if bug report header was already logged. */
1479     int watchdog_period;  /* Software watchdog period in ms. 0 = off */
1480     /* System hardware info */
1481     size_t system_memory_size;  /* Total memory in system as reported by OS */
1482     /* TLS Configuration */
1483     int tls_cluster;
1484     int tls_replication;
1485     int tls_auth_clients;
1486     redisTLSContextConfig tls_ctx_config;
1487     /* cpu affinity */
1488     char *server_cpulist; /* cpu affinity list of redis server main/io thread. */
1489     char *bio_cpulist; /* cpu affinity list of bio thread. */
1490     char *aof_rewrite_cpulist; /* cpu affinity list of aof rewrite process. */
1491     char *bgsave_cpulist; /* cpu affinity list of bgsave process. */
1492 };
1493 
1494 typedef struct pubsubPattern {
1495     client *client;
1496     robj *pattern;
1497 } pubsubPattern;
1498 
1499 #define MAX_KEYS_BUFFER 256
1500 
1501 /* A result structure for the various getkeys function calls. It lists the
1502  * keys as indices to the provided argv.
1503  */
1504 typedef struct {
1505     int keysbuf[MAX_KEYS_BUFFER];       /* Pre-allocated buffer, to save heap allocations */
1506     int *keys;                          /* Key indices array, points to keysbuf or heap */
1507     int numkeys;                        /* Number of key indices return */
1508     int size;                           /* Available array size */
1509 } getKeysResult;
1510 #define GETKEYS_RESULT_INIT { {0}, NULL, 0, MAX_KEYS_BUFFER }
1511 
1512 typedef void redisCommandProc(client *c);
1513 typedef int redisGetKeysProc(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
1514 struct redisCommand {
1515     char *name;
1516     redisCommandProc *proc;
1517     int arity;
1518     char *sflags;   /* Flags as string representation, one char per flag. */
1519     uint64_t flags; /* The actual flags, obtained from the 'sflags' field. */
1520     /* Use a function to determine keys arguments in a command line.
1521      * Used for Redis Cluster redirect. */
1522     redisGetKeysProc *getkeys_proc;
1523     /* What keys should be loaded in background when calling this command? */
1524     int firstkey; /* The first argument that's a key (0 = no keys) */
1525     int lastkey;  /* The last argument that's a key */
1526     int keystep;  /* The step between first and last key */
1527     long long microseconds, calls;
1528     int id;     /* Command ID. This is a progressive ID starting from 0 that
1529                    is assigned at runtime, and is used in order to check
1530                    ACLs. A connection is able to execute a given command if
1531                    the user associated to the connection has this command
1532                    bit set in the bitmap of allowed commands. */
1533 };
1534 
1535 struct redisFunctionSym {
1536     char *name;
1537     unsigned long pointer;
1538 };
1539 
1540 typedef struct _redisSortObject {
1541     robj *obj;
1542     union {
1543         double score;
1544         robj *cmpobj;
1545     } u;
1546 } redisSortObject;
1547 
1548 typedef struct _redisSortOperation {
1549     int type;
1550     robj *pattern;
1551 } redisSortOperation;
1552 
1553 /* Structure to hold list iteration abstraction. */
1554 typedef struct {
1555     robj *subject;
1556     unsigned char encoding;
1557     unsigned char direction; /* Iteration direction */
1558     quicklistIter *iter;
1559 } listTypeIterator;
1560 
1561 /* Structure for an entry while iterating over a list. */
1562 typedef struct {
1563     listTypeIterator *li;
1564     quicklistEntry entry; /* Entry in quicklist */
1565 } listTypeEntry;
1566 
1567 /* Structure to hold set iteration abstraction. */
1568 typedef struct {
1569     robj *subject;
1570     int encoding;
1571     int ii; /* intset iterator */
1572     dictIterator *di;
1573 } setTypeIterator;
1574 
1575 /* Structure to hold hash iteration abstraction. Note that iteration over
1576  * hashes involves both fields and values. Because it is possible that
1577  * not both are required, store pointers in the iterator to avoid
1578  * unnecessary memory allocation for fields/values. */
1579 typedef struct {
1580     robj *subject;
1581     int encoding;
1582 
1583     unsigned char *fptr, *vptr;
1584 
1585     dictIterator *di;
1586     dictEntry *de;
1587 } hashTypeIterator;
1588 
1589 #include "stream.h"  /* Stream data type header file. */
1590 
1591 #define OBJ_HASH_KEY 1
1592 #define OBJ_HASH_VALUE 2
1593 
1594 /*-----------------------------------------------------------------------------
1595  * Extern declarations
1596  *----------------------------------------------------------------------------*/
1597 
1598 extern struct redisServer server;
1599 extern struct sharedObjectsStruct shared;
1600 extern dictType objectKeyPointerValueDictType;
1601 extern dictType objectKeyHeapPointerValueDictType;
1602 extern dictType setDictType;
1603 extern dictType zsetDictType;
1604 extern dictType clusterNodesDictType;
1605 extern dictType clusterNodesBlackListDictType;
1606 extern dictType dbDictType;
1607 extern dictType shaScriptObjectDictType;
1608 extern double R_Zero, R_PosInf, R_NegInf, R_Nan;
1609 extern dictType hashDictType;
1610 extern dictType replScriptCacheDictType;
1611 extern dictType keyptrDictType;
1612 extern dictType modulesDictType;
1613 
1614 /*-----------------------------------------------------------------------------
1615  * Functions prototypes
1616  *----------------------------------------------------------------------------*/
1617 
1618 /* Modules */
1619 void moduleInitModulesSystem(void);
1620 int moduleLoad(const char *path, void **argv, int argc);
1621 void moduleLoadFromQueue(void);
1622 int moduleGetCommandKeysViaAPI(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
1623 moduleType *moduleTypeLookupModuleByID(uint64_t id);
1624 void moduleTypeNameByID(char *name, uint64_t moduleid);
1625 void moduleFreeContext(struct RedisModuleCtx *ctx);
1626 void unblockClientFromModule(client *c);
1627 void moduleHandleBlockedClients(void);
1628 void moduleBlockedClientTimedOut(client *c);
1629 void moduleBlockedClientPipeReadable(aeEventLoop *el, int fd, void *privdata, int mask);
1630 size_t moduleCount(void);
1631 void moduleAcquireGIL(void);
1632 int moduleTryAcquireGIL(void);
1633 void moduleReleaseGIL(void);
1634 void moduleNotifyKeyspaceEvent(int type, const char *event, robj *key, int dbid);
1635 void moduleCallCommandFilters(client *c);
1636 void ModuleForkDoneHandler(int exitcode, int bysignal);
1637 int TerminateModuleForkChild(int child_pid, int wait);
1638 ssize_t rdbSaveModulesAux(rio *rdb, int when);
1639 int moduleAllDatatypesHandleErrors();
1640 sds modulesCollectInfo(sds info, const char *section, int for_crash_report, int sections);
1641 void moduleFireServerEvent(uint64_t eid, int subid, void *data);
1642 void processModuleLoadingProgressEvent(int is_aof);
1643 int moduleTryServeClientBlockedOnKey(client *c, robj *key);
1644 void moduleUnblockClient(client *c);
1645 int moduleClientIsBlockedOnKeys(client *c);
1646 void moduleNotifyUserChanged(client *c);
1647 
1648 /* Utils */
1649 long long ustime(void);
1650 long long mstime(void);
1651 void getRandomHexChars(char *p, size_t len);
1652 void getRandomBytes(unsigned char *p, size_t len);
1653 uint64_t crc64(uint64_t crc, const unsigned char *s, uint64_t l);
1654 void exitFromChild(int retcode);
1655 long long redisPopcount(void *s, long count);
1656 void redisSetProcTitle(char *title);
1657 int redisCommunicateSystemd(const char *sd_notify_msg);
1658 void redisSetCpuAffinity(const char *cpulist);
1659 
1660 /* networking.c -- Networking and Client related operations */
1661 client *createClient(connection *conn);
1662 void closeTimedoutClients(void);
1663 void freeClient(client *c);
1664 void freeClientAsync(client *c);
1665 void resetClient(client *c);
1666 void sendReplyToClient(connection *conn);
1667 void *addReplyDeferredLen(client *c);
1668 void setDeferredArrayLen(client *c, void *node, long length);
1669 void setDeferredMapLen(client *c, void *node, long length);
1670 void setDeferredSetLen(client *c, void *node, long length);
1671 void setDeferredAttributeLen(client *c, void *node, long length);
1672 void setDeferredPushLen(client *c, void *node, long length);
1673 void processInputBuffer(client *c);
1674 void processGopherRequest(client *c);
1675 void acceptHandler(aeEventLoop *el, int fd, void *privdata, int mask);
1676 void acceptTcpHandler(aeEventLoop *el, int fd, void *privdata, int mask);
1677 void acceptTLSHandler(aeEventLoop *el, int fd, void *privdata, int mask);
1678 void acceptUnixHandler(aeEventLoop *el, int fd, void *privdata, int mask);
1679 void readQueryFromClient(connection *conn);
1680 void addReplyNull(client *c);
1681 void addReplyNullArray(client *c);
1682 void addReplyBool(client *c, int b);
1683 void addReplyVerbatim(client *c, const char *s, size_t len, const char *ext);
1684 void addReplyProto(client *c, const char *s, size_t len);
1685 void AddReplyFromClient(client *c, client *src);
1686 void addReplyBulk(client *c, robj *obj);
1687 void addReplyBulkCString(client *c, const char *s);
1688 void addReplyBulkCBuffer(client *c, const void *p, size_t len);
1689 void addReplyBulkLongLong(client *c, long long ll);
1690 void addReply(client *c, robj *obj);
1691 void addReplySds(client *c, sds s);
1692 void addReplyBulkSds(client *c, sds s);
1693 void addReplyErrorObject(client *c, robj *err);
1694 void addReplyErrorSds(client *c, sds err);
1695 void addReplyError(client *c, const char *err);
1696 void addReplyStatus(client *c, const char *status);
1697 void addReplyDouble(client *c, double d);
1698 void addReplyBigNum(client *c, const char* num, size_t len);
1699 void addReplyHumanLongDouble(client *c, long double d);
1700 void addReplyLongLong(client *c, long long ll);
1701 void addReplyArrayLen(client *c, long length);
1702 void addReplyMapLen(client *c, long length);
1703 void addReplySetLen(client *c, long length);
1704 void addReplyAttributeLen(client *c, long length);
1705 void addReplyPushLen(client *c, long length);
1706 void addReplyHelp(client *c, const char **help);
1707 void addReplySubcommandSyntaxError(client *c);
1708 void addReplyLoadedModules(client *c);
1709 void copyClientOutputBuffer(client *dst, client *src);
1710 size_t sdsZmallocSize(sds s);
1711 size_t getStringObjectSdsUsedMemory(robj *o);
1712 void freeClientReplyValue(void *o);
1713 void *dupClientReplyValue(void *o);
1714 void getClientsMaxBuffers(unsigned long *longest_output_list,
1715                           unsigned long *biggest_input_buffer);
1716 char *getClientPeerId(client *client);
1717 sds catClientInfoString(sds s, client *client);
1718 sds getAllClientsInfoString(int type);
1719 void rewriteClientCommandVector(client *c, int argc, ...);
1720 void rewriteClientCommandArgument(client *c, int i, robj *newval);
1721 void replaceClientCommandVector(client *c, int argc, robj **argv);
1722 unsigned long getClientOutputBufferMemoryUsage(client *c);
1723 int freeClientsInAsyncFreeQueue(void);
1724 void asyncCloseClientOnOutputBufferLimitReached(client *c);
1725 int getClientType(client *c);
1726 int getClientTypeByName(char *name);
1727 char *getClientTypeName(int class);
1728 void flushSlavesOutputBuffers(void);
1729 void disconnectSlaves(void);
1730 int listenToPort(int port, int *fds, int *count);
1731 void pauseClients(mstime_t duration);
1732 int clientsArePaused(void);
1733 void processEventsWhileBlocked(void);
1734 int handleClientsWithPendingWrites(void);
1735 int handleClientsWithPendingWritesUsingThreads(void);
1736 int handleClientsWithPendingReadsUsingThreads(void);
1737 int stopThreadedIOIfNeeded(void);
1738 int clientHasPendingReplies(client *c);
1739 void unlinkClient(client *c);
1740 int writeToClient(client *c, int handler_installed);
1741 void linkClient(client *c);
1742 void protectClient(client *c);
1743 void unprotectClient(client *c);
1744 void initThreadedIO(void);
1745 client *lookupClientByID(uint64_t id);
1746 int authRequired(client *c);
1747 
1748 #ifdef __GNUC__
1749 void addReplyErrorFormat(client *c, const char *fmt, ...)
1750     __attribute__((format(printf, 2, 3)));
1751 void addReplyStatusFormat(client *c, const char *fmt, ...)
1752     __attribute__((format(printf, 2, 3)));
1753 #else
1754 void addReplyErrorFormat(client *c, const char *fmt, ...);
1755 void addReplyStatusFormat(client *c, const char *fmt, ...);
1756 #endif
1757 
1758 /* Client side caching (tracking mode) */
1759 void enableTracking(client *c, uint64_t redirect_to, uint64_t options, robj **prefix, size_t numprefix);
1760 void disableTracking(client *c);
1761 void trackingRememberKeys(client *c);
1762 void trackingInvalidateKey(client *c, robj *keyobj);
1763 void trackingInvalidateKeysOnFlush(int dbid);
1764 void trackingLimitUsedSlots(void);
1765 uint64_t trackingGetTotalItems(void);
1766 uint64_t trackingGetTotalKeys(void);
1767 uint64_t trackingGetTotalPrefixes(void);
1768 void trackingBroadcastInvalidationMessages(void);
1769 
1770 /* List data type */
1771 void listTypeTryConversion(robj *subject, robj *value);
1772 void listTypePush(robj *subject, robj *value, int where);
1773 robj *listTypePop(robj *subject, int where);
1774 unsigned long listTypeLength(const robj *subject);
1775 listTypeIterator *listTypeInitIterator(robj *subject, long index, unsigned char direction);
1776 void listTypeReleaseIterator(listTypeIterator *li);
1777 int listTypeNext(listTypeIterator *li, listTypeEntry *entry);
1778 robj *listTypeGet(listTypeEntry *entry);
1779 void listTypeInsert(listTypeEntry *entry, robj *value, int where);
1780 int listTypeEqual(listTypeEntry *entry, robj *o);
1781 void listTypeDelete(listTypeIterator *iter, listTypeEntry *entry);
1782 void listTypeConvert(robj *subject, int enc);
1783 void unblockClientWaitingData(client *c);
1784 void popGenericCommand(client *c, int where);
1785 
1786 /* MULTI/EXEC/WATCH... */
1787 void unwatchAllKeys(client *c);
1788 void initClientMultiState(client *c);
1789 void freeClientMultiState(client *c);
1790 void queueMultiCommand(client *c);
1791 void touchWatchedKey(redisDb *db, robj *key);
1792 int isWatchedKeyExpired(client *c);
1793 void touchAllWatchedKeysInDb(redisDb *emptied, redisDb *replaced_with);
1794 void discardTransaction(client *c);
1795 void flagTransaction(client *c);
1796 void execCommandAbort(client *c, sds error);
1797 void execCommandPropagateMulti(client *c);
1798 void execCommandPropagateExec(client *c);
1799 
1800 /* Redis object implementation */
1801 void decrRefCount(robj *o);
1802 void decrRefCountVoid(void *o);
1803 void incrRefCount(robj *o);
1804 robj *makeObjectShared(robj *o);
1805 robj *resetRefCount(robj *obj);
1806 void freeStringObject(robj *o);
1807 void freeListObject(robj *o);
1808 void freeSetObject(robj *o);
1809 void freeZsetObject(robj *o);
1810 void freeHashObject(robj *o);
1811 robj *createObject(int type, void *ptr);
1812 robj *createStringObject(const char *ptr, size_t len);
1813 robj *createRawStringObject(const char *ptr, size_t len);
1814 robj *createEmbeddedStringObject(const char *ptr, size_t len);
1815 robj *dupStringObject(const robj *o);
1816 int isSdsRepresentableAsLongLong(sds s, long long *llval);
1817 int isObjectRepresentableAsLongLong(robj *o, long long *llongval);
1818 robj *tryObjectEncoding(robj *o);
1819 robj *getDecodedObject(robj *o);
1820 size_t stringObjectLen(robj *o);
1821 robj *createStringObjectFromLongLong(long long value);
1822 robj *createStringObjectFromLongLongForValue(long long value);
1823 robj *createStringObjectFromLongDouble(long double value, int humanfriendly);
1824 robj *createQuicklistObject(void);
1825 robj *createZiplistObject(void);
1826 robj *createSetObject(void);
1827 robj *createIntsetObject(void);
1828 robj *createHashObject(void);
1829 robj *createZsetObject(void);
1830 robj *createZsetZiplistObject(void);
1831 robj *createStreamObject(void);
1832 robj *createModuleObject(moduleType *mt, void *value);
1833 int getLongFromObjectOrReply(client *c, robj *o, long *target, const char *msg);
1834 int checkType(client *c, robj *o, int type);
1835 int getLongLongFromObjectOrReply(client *c, robj *o, long long *target, const char *msg);
1836 int getDoubleFromObjectOrReply(client *c, robj *o, double *target, const char *msg);
1837 int getDoubleFromObject(const robj *o, double *target);
1838 int getLongLongFromObject(robj *o, long long *target);
1839 int getLongDoubleFromObject(robj *o, long double *target);
1840 int getLongDoubleFromObjectOrReply(client *c, robj *o, long double *target, const char *msg);
1841 char *strEncoding(int encoding);
1842 int compareStringObjects(robj *a, robj *b);
1843 int collateStringObjects(robj *a, robj *b);
1844 int equalStringObjects(robj *a, robj *b);
1845 unsigned long long estimateObjectIdleTime(robj *o);
1846 void trimStringObjectIfNeeded(robj *o);
1847 #define sdsEncodedObject(objptr) (objptr->encoding == OBJ_ENCODING_RAW || objptr->encoding == OBJ_ENCODING_EMBSTR)
1848 
1849 /* Synchronous I/O with timeout */
1850 ssize_t syncWrite(int fd, char *ptr, ssize_t size, long long timeout);
1851 ssize_t syncRead(int fd, char *ptr, ssize_t size, long long timeout);
1852 ssize_t syncReadLine(int fd, char *ptr, ssize_t size, long long timeout);
1853 
1854 /* Replication */
1855 void replicationFeedSlaves(list *slaves, int dictid, robj **argv, int argc);
1856 void replicationFeedSlavesFromMasterStream(list *slaves, char *buf, size_t buflen);
1857 void replicationFeedMonitors(client *c, list *monitors, int dictid, robj **argv, int argc);
1858 void updateSlavesWaitingBgsave(int bgsaveerr, int type);
1859 void replicationCron(void);
1860 void replicationHandleMasterDisconnection(void);
1861 void replicationCacheMaster(client *c);
1862 void resizeReplicationBacklog(long long newsize);
1863 void replicationSetMaster(char *ip, int port);
1864 void replicationUnsetMaster(void);
1865 void refreshGoodSlavesCount(void);
1866 void replicationScriptCacheInit(void);
1867 void replicationScriptCacheFlush(void);
1868 void replicationScriptCacheAdd(sds sha1);
1869 int replicationScriptCacheExists(sds sha1);
1870 void processClientsWaitingReplicas(void);
1871 void unblockClientWaitingReplicas(client *c);
1872 int replicationCountAcksByOffset(long long offset);
1873 void replicationSendNewlineToMaster(void);
1874 long long replicationGetSlaveOffset(void);
1875 char *replicationGetSlaveName(client *c);
1876 long long getPsyncInitialOffset(void);
1877 int replicationSetupSlaveForFullResync(client *slave, long long offset);
1878 void changeReplicationId(void);
1879 void clearReplicationId2(void);
1880 void chopReplicationBacklog(void);
1881 void replicationCacheMasterUsingMyself(void);
1882 void feedReplicationBacklog(void *ptr, size_t len);
1883 void showLatestBacklog(void);
1884 void rdbPipeReadHandler(struct aeEventLoop *eventLoop, int fd, void *clientData, int mask);
1885 void rdbPipeWriteHandlerConnRemoved(struct connection *conn);
1886 
1887 /* Generic persistence functions */
1888 void startLoadingFile(FILE* fp, char* filename, int rdbflags);
1889 void startLoading(size_t size, int rdbflags);
1890 void loadingProgress(off_t pos);
1891 void stopLoading(int success);
1892 void startSaving(int rdbflags);
1893 void stopSaving(int success);
1894 int allPersistenceDisabled(void);
1895 
1896 #define DISK_ERROR_TYPE_AOF 1       /* Don't accept writes: AOF errors. */
1897 #define DISK_ERROR_TYPE_RDB 2       /* Don't accept writes: RDB errors. */
1898 #define DISK_ERROR_TYPE_NONE 0      /* No problems, we can accept writes. */
1899 int writeCommandsDeniedByDiskError(void);
1900 
1901 /* RDB persistence */
1902 #include "rdb.h"
1903 void killRDBChild(void);
1904 int bg_unlink(const char *filename);
1905 
1906 /* AOF persistence */
1907 void flushAppendOnlyFile(int force);
1908 void feedAppendOnlyFile(struct redisCommand *cmd, int dictid, robj **argv, int argc);
1909 void aofRemoveTempFile(pid_t childpid);
1910 int rewriteAppendOnlyFileBackground(void);
1911 int loadAppendOnlyFile(char *filename);
1912 void stopAppendOnly(void);
1913 int startAppendOnly(void);
1914 void backgroundRewriteDoneHandler(int exitcode, int bysignal);
1915 void aofRewriteBufferReset(void);
1916 unsigned long aofRewriteBufferSize(void);
1917 ssize_t aofReadDiffFromParent(void);
1918 void killAppendOnlyChild(void);
1919 void restartAOFAfterSYNC();
1920 
1921 /* Child info */
1922 void openChildInfoPipe(void);
1923 void closeChildInfoPipe(void);
1924 void sendChildInfo(int process_type);
1925 void receiveChildInfo(void);
1926 
1927 /* Fork helpers */
1928 int redisFork(int type);
1929 int hasActiveChildProcess();
1930 void sendChildCOWInfo(int ptype, char *pname);
1931 
1932 /* acl.c -- Authentication related prototypes. */
1933 extern rax *Users;
1934 extern user *DefaultUser;
1935 void ACLInit(void);
1936 /* Return values for ACLCheckUserCredentials(). */
1937 #define ACL_OK 0
1938 #define ACL_DENIED_CMD 1
1939 #define ACL_DENIED_KEY 2
1940 #define ACL_DENIED_AUTH 3 /* Only used for ACL LOG entries. */
1941 int ACLCheckUserCredentials(robj *username, robj *password);
1942 int ACLAuthenticateUser(client *c, robj *username, robj *password);
1943 unsigned long ACLGetCommandID(const char *cmdname);
1944 user *ACLGetUserByName(const char *name, size_t namelen);
1945 int ACLCheckCommandPerm(client *c, int *keyidxptr);
1946 int ACLSetUser(user *u, const char *op, ssize_t oplen);
1947 sds ACLDefaultUserFirstPassword(void);
1948 uint64_t ACLGetCommandCategoryFlagByName(const char *name);
1949 int ACLAppendUserForLoading(sds *argv, int argc, int *argc_err);
1950 char *ACLSetUserStringError(void);
1951 int ACLLoadConfiguredUsers(void);
1952 sds ACLDescribeUser(user *u);
1953 void ACLLoadUsersAtStartup(void);
1954 void addReplyCommandCategories(client *c, struct redisCommand *cmd);
1955 user *ACLCreateUnlinkedUser();
1956 void ACLFreeUserAndKillClients(user *u);
1957 void addACLLogEntry(client *c, int reason, int keypos, sds username);
1958 
1959 /* Sorted sets data type */
1960 
1961 /* Input flags. */
1962 #define ZADD_NONE 0
1963 #define ZADD_INCR (1<<0)    /* Increment the score instead of setting it. */
1964 #define ZADD_NX (1<<1)      /* Don't touch elements not already existing. */
1965 #define ZADD_XX (1<<2)      /* Only touch elements already existing. */
1966 
1967 /* Output flags. */
1968 #define ZADD_NOP (1<<3)     /* Operation not performed because of conditionals.*/
1969 #define ZADD_NAN (1<<4)     /* Only touch elements already existing. */
1970 #define ZADD_ADDED (1<<5)   /* The element was new and was added. */
1971 #define ZADD_UPDATED (1<<6) /* The element already existed, score updated. */
1972 
1973 /* Flags only used by the ZADD command but not by zsetAdd() API: */
1974 #define ZADD_CH (1<<16)      /* Return num of elements added or updated. */
1975 
1976 /* Struct to hold an inclusive/exclusive range spec by score comparison. */
1977 typedef struct {
1978     double min, max;
1979     int minex, maxex; /* are min or max exclusive? */
1980 } zrangespec;
1981 
1982 /* Struct to hold an inclusive/exclusive range spec by lexicographic comparison. */
1983 typedef struct {
1984     sds min, max;     /* May be set to shared.(minstring|maxstring) */
1985     int minex, maxex; /* are min or max exclusive? */
1986 } zlexrangespec;
1987 
1988 zskiplist *zslCreate(void);
1989 void zslFree(zskiplist *zsl);
1990 zskiplistNode *zslInsert(zskiplist *zsl, double score, sds ele);
1991 unsigned char *zzlInsert(unsigned char *zl, sds ele, double score);
1992 int zslDelete(zskiplist *zsl, double score, sds ele, zskiplistNode **node);
1993 zskiplistNode *zslFirstInRange(zskiplist *zsl, zrangespec *range);
1994 zskiplistNode *zslLastInRange(zskiplist *zsl, zrangespec *range);
1995 double zzlGetScore(unsigned char *sptr);
1996 void zzlNext(unsigned char *zl, unsigned char **eptr, unsigned char **sptr);
1997 void zzlPrev(unsigned char *zl, unsigned char **eptr, unsigned char **sptr);
1998 unsigned char *zzlFirstInRange(unsigned char *zl, zrangespec *range);
1999 unsigned char *zzlLastInRange(unsigned char *zl, zrangespec *range);
2000 unsigned long zsetLength(const robj *zobj);
2001 void zsetConvert(robj *zobj, int encoding);
2002 void zsetConvertToZiplistIfNeeded(robj *zobj, size_t maxelelen, size_t totelelen);
2003 int zsetScore(robj *zobj, sds member, double *score);
2004 unsigned long zslGetRank(zskiplist *zsl, double score, sds o);
2005 int zsetAdd(robj *zobj, double score, sds ele, int *flags, double *newscore);
2006 long zsetRank(robj *zobj, sds ele, int reverse);
2007 int zsetDel(robj *zobj, sds ele);
2008 void genericZpopCommand(client *c, robj **keyv, int keyc, int where, int emitkey, robj *countarg);
2009 sds ziplistGetObject(unsigned char *sptr);
2010 int zslValueGteMin(double value, zrangespec *spec);
2011 int zslValueLteMax(double value, zrangespec *spec);
2012 void zslFreeLexRange(zlexrangespec *spec);
2013 int zslParseLexRange(robj *min, robj *max, zlexrangespec *spec);
2014 unsigned char *zzlFirstInLexRange(unsigned char *zl, zlexrangespec *range);
2015 unsigned char *zzlLastInLexRange(unsigned char *zl, zlexrangespec *range);
2016 zskiplistNode *zslFirstInLexRange(zskiplist *zsl, zlexrangespec *range);
2017 zskiplistNode *zslLastInLexRange(zskiplist *zsl, zlexrangespec *range);
2018 int zzlLexValueGteMin(unsigned char *p, zlexrangespec *spec);
2019 int zzlLexValueLteMax(unsigned char *p, zlexrangespec *spec);
2020 int zslLexValueGteMin(sds value, zlexrangespec *spec);
2021 int zslLexValueLteMax(sds value, zlexrangespec *spec);
2022 
2023 /* Core functions */
2024 int getMaxmemoryState(size_t *total, size_t *logical, size_t *tofree, float *level);
2025 size_t freeMemoryGetNotCountedMemory();
2026 int freeMemoryIfNeeded(void);
2027 int freeMemoryIfNeededAndSafe(void);
2028 int processCommand(client *c);
2029 int processPendingCommandsAndResetClient(client *c);
2030 void setupSignalHandlers(void);
2031 struct redisCommand *lookupCommand(sds name);
2032 struct redisCommand *lookupCommandByCString(const char *s);
2033 struct redisCommand *lookupCommandOrOriginal(sds name);
2034 void call(client *c, int flags);
2035 void propagate(struct redisCommand *cmd, int dbid, robj **argv, int argc, int flags);
2036 void alsoPropagate(struct redisCommand *cmd, int dbid, robj **argv, int argc, int target);
2037 void redisOpArrayInit(redisOpArray *oa);
2038 void redisOpArrayFree(redisOpArray *oa);
2039 void forceCommandPropagation(client *c, int flags);
2040 void preventCommandPropagation(client *c);
2041 void preventCommandAOF(client *c);
2042 void preventCommandReplication(client *c);
2043 int prepareForShutdown(int flags);
2044 #ifdef __GNUC__
2045 void serverLog(int level, const char *fmt, ...)
2046     __attribute__((format(printf, 2, 3)));
2047 #else
2048 void serverLog(int level, const char *fmt, ...);
2049 #endif
2050 void serverLogRaw(int level, const char *msg);
2051 void serverLogFromHandler(int level, const char *msg);
2052 void usage(void);
2053 void updateDictResizePolicy(void);
2054 int htNeedsResize(dict *dict);
2055 void populateCommandTable(void);
2056 void resetCommandTableStats(void);
2057 void adjustOpenFilesLimit(void);
2058 void closeListeningSockets(int unlink_unix_socket);
2059 void updateCachedTime(int update_daylight_info);
2060 void resetServerStats(void);
2061 void activeDefragCycle(void);
2062 unsigned int getLRUClock(void);
2063 unsigned int LRU_CLOCK(void);
2064 const char *evictPolicyToString(void);
2065 struct redisMemOverhead *getMemoryOverheadData(void);
2066 void freeMemoryOverheadData(struct redisMemOverhead *mh);
2067 void checkChildrenDone(void);
2068 int setOOMScoreAdj(int process_class);
2069 
2070 #define RESTART_SERVER_NONE 0
2071 #define RESTART_SERVER_GRACEFULLY (1<<0)     /* Do proper shutdown. */
2072 #define RESTART_SERVER_CONFIG_REWRITE (1<<1) /* CONFIG REWRITE before restart.*/
2073 int restartServer(int flags, mstime_t delay);
2074 
2075 /* Set data type */
2076 robj *setTypeCreate(sds value);
2077 int setTypeAdd(robj *subject, sds value);
2078 int setTypeRemove(robj *subject, sds value);
2079 int setTypeIsMember(robj *subject, sds value);
2080 setTypeIterator *setTypeInitIterator(robj *subject);
2081 void setTypeReleaseIterator(setTypeIterator *si);
2082 int setTypeNext(setTypeIterator *si, sds *sdsele, int64_t *llele);
2083 sds setTypeNextObject(setTypeIterator *si);
2084 int setTypeRandomElement(robj *setobj, sds *sdsele, int64_t *llele);
2085 unsigned long setTypeRandomElements(robj *set, unsigned long count, robj *aux_set);
2086 unsigned long setTypeSize(const robj *subject);
2087 void setTypeConvert(robj *subject, int enc);
2088 
2089 /* Hash data type */
2090 #define HASH_SET_TAKE_FIELD (1<<0)
2091 #define HASH_SET_TAKE_VALUE (1<<1)
2092 #define HASH_SET_COPY 0
2093 
2094 void hashTypeConvert(robj *o, int enc);
2095 void hashTypeTryConversion(robj *subject, robj **argv, int start, int end);
2096 int hashTypeExists(robj *o, sds key);
2097 int hashTypeDelete(robj *o, sds key);
2098 unsigned long hashTypeLength(const robj *o);
2099 hashTypeIterator *hashTypeInitIterator(robj *subject);
2100 void hashTypeReleaseIterator(hashTypeIterator *hi);
2101 int hashTypeNext(hashTypeIterator *hi);
2102 void hashTypeCurrentFromZiplist(hashTypeIterator *hi, int what,
2103                                 unsigned char **vstr,
2104                                 unsigned int *vlen,
2105                                 long long *vll);
2106 sds hashTypeCurrentFromHashTable(hashTypeIterator *hi, int what);
2107 void hashTypeCurrentObject(hashTypeIterator *hi, int what, unsigned char **vstr, unsigned int *vlen, long long *vll);
2108 sds hashTypeCurrentObjectNewSds(hashTypeIterator *hi, int what);
2109 robj *hashTypeLookupWriteOrCreate(client *c, robj *key);
2110 robj *hashTypeGetValueObject(robj *o, sds field);
2111 int hashTypeSet(robj *o, sds field, sds value, int flags);
2112 
2113 /* Pub / Sub */
2114 int pubsubUnsubscribeAllChannels(client *c, int notify);
2115 int pubsubUnsubscribeAllPatterns(client *c, int notify);
2116 void freePubsubPattern(void *p);
2117 int listMatchPubsubPattern(void *a, void *b);
2118 int pubsubPublishMessage(robj *channel, robj *message);
2119 void addReplyPubsubMessage(client *c, robj *channel, robj *msg);
2120 
2121 /* Keyspace events notification */
2122 void notifyKeyspaceEvent(int type, char *event, robj *key, int dbid);
2123 int keyspaceEventsStringToFlags(char *classes);
2124 sds keyspaceEventsFlagsToString(int flags);
2125 
2126 /* Configuration */
2127 void loadServerConfig(char *filename, char *options);
2128 void appendServerSaveParams(time_t seconds, int changes);
2129 void resetServerSaveParams(void);
2130 struct rewriteConfigState; /* Forward declaration to export API. */
2131 void rewriteConfigRewriteLine(struct rewriteConfigState *state, const char *option, sds line, int force);
2132 int rewriteConfig(char *path, int force_all);
2133 void initConfigValues();
2134 
2135 /* db.c -- Keyspace access API */
2136 int removeExpire(redisDb *db, robj *key);
2137 void propagateExpire(redisDb *db, robj *key, int lazy);
2138 int keyIsExpired(redisDb *db, robj *key);
2139 int expireIfNeeded(redisDb *db, robj *key);
2140 long long getExpire(redisDb *db, robj *key);
2141 void setExpire(client *c, redisDb *db, robj *key, long long when);
2142 int checkAlreadyExpired(long long when);
2143 robj *lookupKey(redisDb *db, robj *key, int flags);
2144 robj *lookupKeyRead(redisDb *db, robj *key);
2145 robj *lookupKeyWrite(redisDb *db, robj *key);
2146 robj *lookupKeyReadOrReply(client *c, robj *key, robj *reply);
2147 robj *lookupKeyWriteOrReply(client *c, robj *key, robj *reply);
2148 robj *lookupKeyReadWithFlags(redisDb *db, robj *key, int flags);
2149 robj *lookupKeyWriteWithFlags(redisDb *db, robj *key, int flags);
2150 robj *objectCommandLookup(client *c, robj *key);
2151 robj *objectCommandLookupOrReply(client *c, robj *key, robj *reply);
2152 int objectSetLRUOrLFU(robj *val, long long lfu_freq, long long lru_idle,
2153                        long long lru_clock, int lru_multiplier);
2154 #define LOOKUP_NONE 0
2155 #define LOOKUP_NOTOUCH (1<<0)
2156 #define LOOKUP_NONOTIFY (1<<1)
2157 void dbAdd(redisDb *db, robj *key, robj *val);
2158 int dbAddRDBLoad(redisDb *db, sds key, robj *val);
2159 void dbOverwrite(redisDb *db, robj *key, robj *val);
2160 void genericSetKey(client *c, redisDb *db, robj *key, robj *val, int keepttl, int signal);
2161 void setKey(client *c, redisDb *db, robj *key, robj *val);
2162 int dbExists(redisDb *db, robj *key);
2163 robj *dbRandomKey(redisDb *db);
2164 int dbSyncDelete(redisDb *db, robj *key);
2165 int dbDelete(redisDb *db, robj *key);
2166 robj *dbUnshareStringValue(redisDb *db, robj *key, robj *o);
2167 
2168 #define EMPTYDB_NO_FLAGS 0      /* No flags. */
2169 #define EMPTYDB_ASYNC (1<<0)    /* Reclaim memory in another thread. */
2170 long long emptyDb(int dbnum, int flags, void(callback)(void*));
2171 long long emptyDbStructure(redisDb *dbarray, int dbnum, int async, void(callback)(void*));
2172 void flushAllDataAndResetRDB(int flags);
2173 long long dbTotalServerKeyCount();
2174 dbBackup *backupDb(void);
2175 void restoreDbBackup(dbBackup *buckup);
2176 void discardDbBackup(dbBackup *buckup, int flags, void(callback)(void*));
2177 
2178 
2179 int selectDb(client *c, int id);
2180 void signalModifiedKey(client *c, redisDb *db, robj *key);
2181 void signalFlushedDb(int dbid);
2182 unsigned int getKeysInSlot(unsigned int hashslot, robj **keys, unsigned int count);
2183 unsigned int countKeysInSlot(unsigned int hashslot);
2184 unsigned int delKeysInSlot(unsigned int hashslot);
2185 int verifyClusterConfigWithData(void);
2186 void scanGenericCommand(client *c, robj *o, unsigned long cursor);
2187 int parseScanCursorOrReply(client *c, robj *o, unsigned long *cursor);
2188 void slotToKeyAdd(sds key);
2189 void slotToKeyDel(sds key);
2190 int dbAsyncDelete(redisDb *db, robj *key);
2191 void emptyDbAsync(redisDb *db);
2192 void slotToKeyFlush(int async);
2193 size_t lazyfreeGetPendingObjectsCount(void);
2194 void freeObjAsync(robj *obj);
2195 void freeSlotsToKeysMapAsync(rax *rt);
2196 void freeSlotsToKeysMap(rax *rt, int async);
2197 
2198 
2199 /* API to get key arguments from commands */
2200 int *getKeysPrepareResult(getKeysResult *result, int numkeys);
2201 int getKeysFromCommand(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
2202 void getKeysFreeResult(getKeysResult *result);
2203 int zunionInterGetKeys(struct redisCommand *cmd,robj **argv, int argc, getKeysResult *result);
2204 int evalGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
2205 int sortGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
2206 int migrateGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
2207 int georadiusGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
2208 int xreadGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
2209 int memoryGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
2210 int lcsGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
2211 
2212 /* Cluster */
2213 void clusterInit(void);
2214 unsigned short crc16(const char *buf, int len);
2215 unsigned int keyHashSlot(char *key, int keylen);
2216 void clusterCron(void);
2217 void clusterPropagatePublish(robj *channel, robj *message);
2218 void migrateCloseTimedoutSockets(void);
2219 void clusterBeforeSleep(void);
2220 int clusterSendModuleMessageToTarget(const char *target, uint64_t module_id, uint8_t type, unsigned char *payload, uint32_t len);
2221 
2222 /* Sentinel */
2223 void initSentinelConfig(void);
2224 void initSentinel(void);
2225 void sentinelTimer(void);
2226 char *sentinelHandleConfiguration(char **argv, int argc);
2227 void sentinelIsRunning(void);
2228 
2229 /* redis-check-rdb & aof */
2230 int redis_check_rdb(char *rdbfilename, FILE *fp);
2231 int redis_check_rdb_main(int argc, char **argv, FILE *fp);
2232 int redis_check_aof_main(int argc, char **argv);
2233 
2234 /* Scripting */
2235 void scriptingInit(int setup);
2236 int ldbRemoveChild(pid_t pid);
2237 void ldbKillForkedSessions(void);
2238 int ldbPendingChildren(void);
2239 sds luaCreateFunction(client *c, lua_State *lua, robj *body);
2240 
2241 /* Blocked clients */
2242 void processUnblockedClients(void);
2243 void blockClient(client *c, int btype);
2244 void unblockClient(client *c);
2245 void queueClientForReprocessing(client *c);
2246 void replyToBlockedClientTimedOut(client *c);
2247 int getTimeoutFromObjectOrReply(client *c, robj *object, mstime_t *timeout, int unit);
2248 void disconnectAllBlockedClients(void);
2249 void handleClientsBlockedOnKeys(void);
2250 void signalKeyAsReady(redisDb *db, robj *key);
2251 void blockForKeys(client *c, int btype, robj **keys, int numkeys, mstime_t timeout, robj *target, streamID *ids);
2252 
2253 /* timeout.c -- Blocked clients timeout and connections timeout. */
2254 void addClientToTimeoutTable(client *c);
2255 void removeClientFromTimeoutTable(client *c);
2256 void handleBlockedClientsTimeout(void);
2257 int clientsCronHandleTimeout(client *c, mstime_t now_ms);
2258 
2259 /* expire.c -- Handling of expired keys */
2260 void activeExpireCycle(int type);
2261 void expireSlaveKeys(void);
2262 void rememberSlaveKeyWithExpire(redisDb *db, robj *key);
2263 void flushSlaveKeysWithExpireList(void);
2264 size_t getSlaveKeyWithExpireCount(void);
2265 
2266 /* evict.c -- maxmemory handling and LRU eviction. */
2267 void evictionPoolAlloc(void);
2268 #define LFU_INIT_VAL 5
2269 unsigned long LFUGetTimeInMinutes(void);
2270 uint8_t LFULogIncr(uint8_t value);
2271 unsigned long LFUDecrAndReturn(robj *o);
2272 
2273 /* Keys hashing / comparison functions for dict.c hash tables. */
2274 uint64_t dictSdsHash(const void *key);
2275 int dictSdsKeyCompare(void *privdata, const void *key1, const void *key2);
2276 void dictSdsDestructor(void *privdata, void *val);
2277 
2278 /* Git SHA1 */
2279 char *redisGitSHA1(void);
2280 char *redisGitDirty(void);
2281 uint64_t redisBuildId(void);
2282 char *redisBuildIdString(void);
2283 
2284 /* Commands prototypes */
2285 void authCommand(client *c);
2286 void pingCommand(client *c);
2287 void echoCommand(client *c);
2288 void commandCommand(client *c);
2289 void setCommand(client *c);
2290 void setnxCommand(client *c);
2291 void setexCommand(client *c);
2292 void psetexCommand(client *c);
2293 void getCommand(client *c);
2294 void delCommand(client *c);
2295 void unlinkCommand(client *c);
2296 void existsCommand(client *c);
2297 void setbitCommand(client *c);
2298 void getbitCommand(client *c);
2299 void bitfieldCommand(client *c);
2300 void bitfieldroCommand(client *c);
2301 void setrangeCommand(client *c);
2302 void getrangeCommand(client *c);
2303 void incrCommand(client *c);
2304 void decrCommand(client *c);
2305 void incrbyCommand(client *c);
2306 void decrbyCommand(client *c);
2307 void incrbyfloatCommand(client *c);
2308 void selectCommand(client *c);
2309 void swapdbCommand(client *c);
2310 void randomkeyCommand(client *c);
2311 void keysCommand(client *c);
2312 void scanCommand(client *c);
2313 void dbsizeCommand(client *c);
2314 void lastsaveCommand(client *c);
2315 void saveCommand(client *c);
2316 void bgsaveCommand(client *c);
2317 void bgrewriteaofCommand(client *c);
2318 void shutdownCommand(client *c);
2319 void moveCommand(client *c);
2320 void renameCommand(client *c);
2321 void renamenxCommand(client *c);
2322 void lpushCommand(client *c);
2323 void rpushCommand(client *c);
2324 void lpushxCommand(client *c);
2325 void rpushxCommand(client *c);
2326 void linsertCommand(client *c);
2327 void lpopCommand(client *c);
2328 void rpopCommand(client *c);
2329 void llenCommand(client *c);
2330 void lindexCommand(client *c);
2331 void lrangeCommand(client *c);
2332 void ltrimCommand(client *c);
2333 void typeCommand(client *c);
2334 void lsetCommand(client *c);
2335 void saddCommand(client *c);
2336 void sremCommand(client *c);
2337 void smoveCommand(client *c);
2338 void sismemberCommand(client *c);
2339 void scardCommand(client *c);
2340 void spopCommand(client *c);
2341 void srandmemberCommand(client *c);
2342 void sinterCommand(client *c);
2343 void sinterstoreCommand(client *c);
2344 void sunionCommand(client *c);
2345 void sunionstoreCommand(client *c);
2346 void sdiffCommand(client *c);
2347 void sdiffstoreCommand(client *c);
2348 void sscanCommand(client *c);
2349 void syncCommand(client *c);
2350 void flushdbCommand(client *c);
2351 void flushallCommand(client *c);
2352 void sortCommand(client *c);
2353 void lremCommand(client *c);
2354 void lposCommand(client *c);
2355 void rpoplpushCommand(client *c);
2356 void infoCommand(client *c);
2357 void mgetCommand(client *c);
2358 void monitorCommand(client *c);
2359 void expireCommand(client *c);
2360 void expireatCommand(client *c);
2361 void pexpireCommand(client *c);
2362 void pexpireatCommand(client *c);
2363 void getsetCommand(client *c);
2364 void ttlCommand(client *c);
2365 void touchCommand(client *c);
2366 void pttlCommand(client *c);
2367 void persistCommand(client *c);
2368 void replicaofCommand(client *c);
2369 void roleCommand(client *c);
2370 void debugCommand(client *c);
2371 void msetCommand(client *c);
2372 void msetnxCommand(client *c);
2373 void zaddCommand(client *c);
2374 void zincrbyCommand(client *c);
2375 void zrangeCommand(client *c);
2376 void zrangebyscoreCommand(client *c);
2377 void zrevrangebyscoreCommand(client *c);
2378 void zrangebylexCommand(client *c);
2379 void zrevrangebylexCommand(client *c);
2380 void zcountCommand(client *c);
2381 void zlexcountCommand(client *c);
2382 void zrevrangeCommand(client *c);
2383 void zcardCommand(client *c);
2384 void zremCommand(client *c);
2385 void zscoreCommand(client *c);
2386 void zremrangebyscoreCommand(client *c);
2387 void zremrangebylexCommand(client *c);
2388 void zpopminCommand(client *c);
2389 void zpopmaxCommand(client *c);
2390 void bzpopminCommand(client *c);
2391 void bzpopmaxCommand(client *c);
2392 void multiCommand(client *c);
2393 void execCommand(client *c);
2394 void discardCommand(client *c);
2395 void blpopCommand(client *c);
2396 void brpopCommand(client *c);
2397 void brpoplpushCommand(client *c);
2398 void appendCommand(client *c);
2399 void strlenCommand(client *c);
2400 void zrankCommand(client *c);
2401 void zrevrankCommand(client *c);
2402 void hsetCommand(client *c);
2403 void hsetnxCommand(client *c);
2404 void hgetCommand(client *c);
2405 void hmsetCommand(client *c);
2406 void hmgetCommand(client *c);
2407 void hdelCommand(client *c);
2408 void hlenCommand(client *c);
2409 void hstrlenCommand(client *c);
2410 void zremrangebyrankCommand(client *c);
2411 void zunionstoreCommand(client *c);
2412 void zinterstoreCommand(client *c);
2413 void zscanCommand(client *c);
2414 void hkeysCommand(client *c);
2415 void hvalsCommand(client *c);
2416 void hgetallCommand(client *c);
2417 void hexistsCommand(client *c);
2418 void hscanCommand(client *c);
2419 void configCommand(client *c);
2420 void hincrbyCommand(client *c);
2421 void hincrbyfloatCommand(client *c);
2422 void subscribeCommand(client *c);
2423 void unsubscribeCommand(client *c);
2424 void psubscribeCommand(client *c);
2425 void punsubscribeCommand(client *c);
2426 void publishCommand(client *c);
2427 void pubsubCommand(client *c);
2428 void watchCommand(client *c);
2429 void unwatchCommand(client *c);
2430 void clusterCommand(client *c);
2431 void restoreCommand(client *c);
2432 void migrateCommand(client *c);
2433 void askingCommand(client *c);
2434 void readonlyCommand(client *c);
2435 void readwriteCommand(client *c);
2436 void dumpCommand(client *c);
2437 void objectCommand(client *c);
2438 void memoryCommand(client *c);
2439 void clientCommand(client *c);
2440 void helloCommand(client *c);
2441 void evalCommand(client *c);
2442 void evalShaCommand(client *c);
2443 void scriptCommand(client *c);
2444 void timeCommand(client *c);
2445 void bitopCommand(client *c);
2446 void bitcountCommand(client *c);
2447 void bitposCommand(client *c);
2448 void replconfCommand(client *c);
2449 void waitCommand(client *c);
2450 void geoencodeCommand(client *c);
2451 void geodecodeCommand(client *c);
2452 void georadiusbymemberCommand(client *c);
2453 void georadiusbymemberroCommand(client *c);
2454 void georadiusCommand(client *c);
2455 void georadiusroCommand(client *c);
2456 void geoaddCommand(client *c);
2457 void geohashCommand(client *c);
2458 void geoposCommand(client *c);
2459 void geodistCommand(client *c);
2460 void pfselftestCommand(client *c);
2461 void pfaddCommand(client *c);
2462 void pfcountCommand(client *c);
2463 void pfmergeCommand(client *c);
2464 void pfdebugCommand(client *c);
2465 void latencyCommand(client *c);
2466 void moduleCommand(client *c);
2467 void securityWarningCommand(client *c);
2468 void xaddCommand(client *c);
2469 void xrangeCommand(client *c);
2470 void xrevrangeCommand(client *c);
2471 void xlenCommand(client *c);
2472 void xreadCommand(client *c);
2473 void xgroupCommand(client *c);
2474 void xsetidCommand(client *c);
2475 void xackCommand(client *c);
2476 void xpendingCommand(client *c);
2477 void xclaimCommand(client *c);
2478 void xinfoCommand(client *c);
2479 void xdelCommand(client *c);
2480 void xtrimCommand(client *c);
2481 void lolwutCommand(client *c);
2482 void aclCommand(client *c);
2483 void stralgoCommand(client *c);
2484 
2485 #if defined(__GNUC__)
2486 void *calloc(size_t count, size_t size) __attribute__ ((deprecated));
2487 void free(void *ptr) __attribute__ ((deprecated));
2488 void *malloc(size_t size) __attribute__ ((deprecated));
2489 void *realloc(void *ptr, size_t size) __attribute__ ((deprecated));
2490 #endif
2491 
2492 /* Debugging stuff */
2493 void _serverAssertWithInfo(const client *c, const robj *o, const char *estr, const char *file, int line);
2494 void _serverAssert(const char *estr, const char *file, int line);
2495 #ifdef __GNUC__
2496 void _serverPanic(const char *file, int line, const char *msg, ...)
2497     __attribute__ ((format (printf, 3, 4)));
2498 #else
2499 void _serverPanic(const char *file, int line, const char *msg, ...);
2500 #endif
2501 void bugReportStart(void);
2502 void serverLogObjectDebugInfo(const robj *o);
2503 void sigsegvHandler(int sig, siginfo_t *info, void *secret);
2504 sds genRedisInfoString(const char *section);
2505 sds genModulesInfoString(sds info);
2506 void enableWatchdog(int period);
2507 void disableWatchdog(void);
2508 void watchdogScheduleSignal(int period);
2509 void serverLogHexDump(int level, char *descr, void *value, size_t len);
2510 int memtest_preserving_test(unsigned long *m, size_t bytes, int passes);
2511 void mixDigest(unsigned char *digest, void *ptr, size_t len);
2512 void xorDigest(unsigned char *digest, void *ptr, size_t len);
2513 int populateCommandTableParseFlags(struct redisCommand *c, char *strflags);
2514 void killIOThreads(void);
2515 void killThreads(void);
2516 void makeThreadKillable(void);
2517 
2518 /* TLS stuff */
2519 void tlsInit(void);
2520 int tlsConfigure(redisTLSContextConfig *ctx_config);
2521 
2522 #define redisDebug(fmt, ...) \
2523     printf("DEBUG %s:%d > " fmt "\n", __FILE__, __LINE__, __VA_ARGS__)
2524 #define redisDebugMark() \
2525     printf("-- MARK %s:%d --\n", __FILE__, __LINE__)
2526 
2527 int iAmMaster(void);
2528 
2529 #endif
2530