2 * Copyright (c) 2006-2009, Salvatore Sanfilippo <antirez at gmail dot com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
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.
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.
30 #define REDIS_VERSION "0.08"
44 #include <arpa/inet.h>
48 #include <sys/resource.h>
51 #include "ae.h" /* Event driven programming library */
52 #include "sds.h" /* Dynamic safe strings */
53 #include "anet.h" /* Networking the easy way */
54 #include "dict.h" /* Hash tables */
55 #include "adlist.h" /* Linked lists */
56 #include "zmalloc.h" /* total memory usage aware version of malloc/free */
62 /* Static server configuration */
63 #define REDIS_SERVERPORT 6379 /* TCP port */
64 #define REDIS_MAXIDLETIME (60*5) /* default client timeout */
65 #define REDIS_QUERYBUF_LEN 1024
66 #define REDIS_LOADBUF_LEN 1024
67 #define REDIS_MAX_ARGS 16
68 #define REDIS_DEFAULT_DBNUM 16
69 #define REDIS_CONFIGLINE_MAX 1024
70 #define REDIS_OBJFREELIST_MAX 1000000 /* Max number of objects to cache */
71 #define REDIS_MAX_SYNC_TIME 60 /* Slave can't take more to sync */
73 /* Hash table parameters */
74 #define REDIS_HT_MINFILL 10 /* Minimal hash table fill 10% */
75 #define REDIS_HT_MINSLOTS 16384 /* Never resize the HT under this */
78 #define REDIS_CMD_BULK 1
79 #define REDIS_CMD_INLINE 2
82 #define REDIS_STRING 0
87 /* Object types only used for dumping to disk */
88 #define REDIS_SELECTDB 254
91 /* Defines related to the dump file format. To store 32 bits lengths for short
92 * keys requires a lot of space, so we check the most significant 2 bits of
93 * the first byte to interpreter the length:
95 * 00|000000 => if the two MSB are 00 the len is the 6 bits of this byte
96 * 01|000000 00000000 => 01, the len is 14 byes, 6 bits + 8 bits of next byte
97 * 10|000000 [32 bit integer] => if it's 01, a full 32 bit len will follow
98 * 11|000000 [64 bit integer] => if it's 11, a full 64 bit len will follow
100 * 64 bit lengths are not used currently. Lenghts up to 63 are stored using
101 * a single byte, most DB keys, and may values, will fit inside. */
102 #define REDIS_RDB_6BITLEN 0
103 #define REDIS_RDB_14BITLEN 1
104 #define REDIS_RDB_32BITLEN 2
105 #define REDIS_RDB_64BITLEN 3
106 #define REDIS_RDB_LENERR UINT_MAX
109 #define REDIS_CLOSE 1 /* This client connection should be closed ASAP */
110 #define REDIS_SLAVE 2 /* This client is a slave server */
111 #define REDIS_MASTER 4 /* This client is a master server */
112 #define REDIS_MONITOR 8 /* This client is a slave monitor, see MONITOR */
114 /* Server replication state */
115 #define REDIS_REPL_NONE 0 /* No active replication */
116 #define REDIS_REPL_CONNECT 1 /* Must connect to master */
117 #define REDIS_REPL_CONNECTED 2 /* Connected to master */
119 /* List related stuff */
123 /* Sort operations */
124 #define REDIS_SORT_GET 0
125 #define REDIS_SORT_DEL 1
126 #define REDIS_SORT_INCR 2
127 #define REDIS_SORT_DECR 3
128 #define REDIS_SORT_ASC 4
129 #define REDIS_SORT_DESC 5
130 #define REDIS_SORTKEY_MAX 1024
133 #define REDIS_DEBUG 0
134 #define REDIS_NOTICE 1
135 #define REDIS_WARNING 2
137 /* Anti-warning macro... */
138 #define REDIS_NOTUSED(V) ((void) V)
140 /*================================= Data types ============================== */
142 /* A redis object, that is a type able to hold a string / list / set */
143 typedef struct redisObject
{
149 /* With multiplexing we need to take per-clinet state.
150 * Clients are taken in a liked list. */
151 typedef struct redisClient
{
156 robj
*argv
[REDIS_MAX_ARGS
];
158 int bulklen
; /* bulk read len. -1 if not in bulk read mode */
161 time_t lastinteraction
; /* time of the last interaction, used for timeout */
162 int flags
; /* REDIS_CLOSE | REDIS_SLAVE | REDIS_MONITOR */
163 int slaveseldb
; /* slave selected db, if this client is a slave */
171 /* Global server state structure */
176 long long dirty
; /* changes to DB from the last save */
178 list
*slaves
, *monitors
;
179 char neterr
[ANET_ERR_LEN
];
181 int cronloops
; /* number of times the cron function run */
182 list
*objfreelist
; /* A list of freed objects to avoid malloc() */
183 time_t lastsave
; /* Unix time of last save succeeede */
184 int usedmemory
; /* Used memory in megabytes */
185 /* Fields used only for stats */
186 time_t stat_starttime
; /* server start time */
187 long long stat_numcommands
; /* number of processed commands */
188 long long stat_numconnections
; /* number of connections received */
196 int bgsaveinprogress
;
197 struct saveparam
*saveparams
;
202 /* Replication related */
208 /* Sort parameters - qsort_r() is only available under BSD so we
209 * have to take this state global, in order to pass it to sortCompare() */
215 typedef void redisCommandProc(redisClient
*c
);
216 struct redisCommand
{
218 redisCommandProc
*proc
;
223 typedef struct _redisSortObject
{
231 typedef struct _redisSortOperation
{
234 } redisSortOperation
;
236 struct sharedObjectsStruct
{
237 robj
*crlf
, *ok
, *err
, *emptybulk
, *czero
, *cone
, *pong
, *space
,
238 *colon
, *nullbulk
, *nullmultibulk
,
239 *emptymultibulk
, *wrongtypeerr
, *nokeyerr
, *syntaxerr
, *sameobjecterr
,
240 *outofrangeerr
, *plus
,
241 *select0
, *select1
, *select2
, *select3
, *select4
,
242 *select5
, *select6
, *select7
, *select8
, *select9
;
245 /*================================ Prototypes =============================== */
247 static void freeStringObject(robj
*o
);
248 static void freeListObject(robj
*o
);
249 static void freeSetObject(robj
*o
);
250 static void decrRefCount(void *o
);
251 static robj
*createObject(int type
, void *ptr
);
252 static void freeClient(redisClient
*c
);
253 static int rdbLoad(char *filename
);
254 static void addReply(redisClient
*c
, robj
*obj
);
255 static void addReplySds(redisClient
*c
, sds s
);
256 static void incrRefCount(robj
*o
);
257 static int rdbSaveBackground(char *filename
);
258 static robj
*createStringObject(char *ptr
, size_t len
);
259 static void replicationFeedSlaves(list
*slaves
, struct redisCommand
*cmd
, int dictid
, robj
**argv
, int argc
);
260 static int syncWithMaster(void);
262 static void pingCommand(redisClient
*c
);
263 static void echoCommand(redisClient
*c
);
264 static void setCommand(redisClient
*c
);
265 static void setnxCommand(redisClient
*c
);
266 static void getCommand(redisClient
*c
);
267 static void delCommand(redisClient
*c
);
268 static void existsCommand(redisClient
*c
);
269 static void incrCommand(redisClient
*c
);
270 static void decrCommand(redisClient
*c
);
271 static void incrbyCommand(redisClient
*c
);
272 static void decrbyCommand(redisClient
*c
);
273 static void selectCommand(redisClient
*c
);
274 static void randomkeyCommand(redisClient
*c
);
275 static void keysCommand(redisClient
*c
);
276 static void dbsizeCommand(redisClient
*c
);
277 static void lastsaveCommand(redisClient
*c
);
278 static void saveCommand(redisClient
*c
);
279 static void bgsaveCommand(redisClient
*c
);
280 static void shutdownCommand(redisClient
*c
);
281 static void moveCommand(redisClient
*c
);
282 static void renameCommand(redisClient
*c
);
283 static void renamenxCommand(redisClient
*c
);
284 static void lpushCommand(redisClient
*c
);
285 static void rpushCommand(redisClient
*c
);
286 static void lpopCommand(redisClient
*c
);
287 static void rpopCommand(redisClient
*c
);
288 static void llenCommand(redisClient
*c
);
289 static void lindexCommand(redisClient
*c
);
290 static void lrangeCommand(redisClient
*c
);
291 static void ltrimCommand(redisClient
*c
);
292 static void typeCommand(redisClient
*c
);
293 static void lsetCommand(redisClient
*c
);
294 static void saddCommand(redisClient
*c
);
295 static void sremCommand(redisClient
*c
);
296 static void sismemberCommand(redisClient
*c
);
297 static void scardCommand(redisClient
*c
);
298 static void sinterCommand(redisClient
*c
);
299 static void sinterstoreCommand(redisClient
*c
);
300 static void syncCommand(redisClient
*c
);
301 static void flushdbCommand(redisClient
*c
);
302 static void flushallCommand(redisClient
*c
);
303 static void sortCommand(redisClient
*c
);
304 static void lremCommand(redisClient
*c
);
305 static void infoCommand(redisClient
*c
);
306 static void mgetCommand(redisClient
*c
);
307 static void monitorCommand(redisClient
*c
);
309 /*================================= Globals ================================= */
312 static struct redisServer server
; /* server global state */
313 static struct redisCommand cmdTable
[] = {
314 {"get",getCommand
,2,REDIS_CMD_INLINE
},
315 {"set",setCommand
,3,REDIS_CMD_BULK
},
316 {"setnx",setnxCommand
,3,REDIS_CMD_BULK
},
317 {"del",delCommand
,2,REDIS_CMD_INLINE
},
318 {"exists",existsCommand
,2,REDIS_CMD_INLINE
},
319 {"incr",incrCommand
,2,REDIS_CMD_INLINE
},
320 {"decr",decrCommand
,2,REDIS_CMD_INLINE
},
321 {"mget",mgetCommand
,-2,REDIS_CMD_INLINE
},
322 {"rpush",rpushCommand
,3,REDIS_CMD_BULK
},
323 {"lpush",lpushCommand
,3,REDIS_CMD_BULK
},
324 {"rpop",rpopCommand
,2,REDIS_CMD_INLINE
},
325 {"lpop",lpopCommand
,2,REDIS_CMD_INLINE
},
326 {"llen",llenCommand
,2,REDIS_CMD_INLINE
},
327 {"lindex",lindexCommand
,3,REDIS_CMD_INLINE
},
328 {"lset",lsetCommand
,4,REDIS_CMD_BULK
},
329 {"lrange",lrangeCommand
,4,REDIS_CMD_INLINE
},
330 {"ltrim",ltrimCommand
,4,REDIS_CMD_INLINE
},
331 {"lrem",lremCommand
,4,REDIS_CMD_BULK
},
332 {"sadd",saddCommand
,3,REDIS_CMD_BULK
},
333 {"srem",sremCommand
,3,REDIS_CMD_BULK
},
334 {"sismember",sismemberCommand
,3,REDIS_CMD_BULK
},
335 {"scard",scardCommand
,2,REDIS_CMD_INLINE
},
336 {"sinter",sinterCommand
,-2,REDIS_CMD_INLINE
},
337 {"sinterstore",sinterstoreCommand
,-3,REDIS_CMD_INLINE
},
338 {"smembers",sinterCommand
,2,REDIS_CMD_INLINE
},
339 {"incrby",incrbyCommand
,3,REDIS_CMD_INLINE
},
340 {"decrby",decrbyCommand
,3,REDIS_CMD_INLINE
},
341 {"randomkey",randomkeyCommand
,1,REDIS_CMD_INLINE
},
342 {"select",selectCommand
,2,REDIS_CMD_INLINE
},
343 {"move",moveCommand
,3,REDIS_CMD_INLINE
},
344 {"rename",renameCommand
,3,REDIS_CMD_INLINE
},
345 {"renamenx",renamenxCommand
,3,REDIS_CMD_INLINE
},
346 {"keys",keysCommand
,2,REDIS_CMD_INLINE
},
347 {"dbsize",dbsizeCommand
,1,REDIS_CMD_INLINE
},
348 {"ping",pingCommand
,1,REDIS_CMD_INLINE
},
349 {"echo",echoCommand
,2,REDIS_CMD_BULK
},
350 {"save",saveCommand
,1,REDIS_CMD_INLINE
},
351 {"bgsave",bgsaveCommand
,1,REDIS_CMD_INLINE
},
352 {"shutdown",shutdownCommand
,1,REDIS_CMD_INLINE
},
353 {"lastsave",lastsaveCommand
,1,REDIS_CMD_INLINE
},
354 {"type",typeCommand
,2,REDIS_CMD_INLINE
},
355 {"sync",syncCommand
,1,REDIS_CMD_INLINE
},
356 {"flushdb",flushdbCommand
,1,REDIS_CMD_INLINE
},
357 {"flushall",flushallCommand
,1,REDIS_CMD_INLINE
},
358 {"sort",sortCommand
,-2,REDIS_CMD_INLINE
},
359 {"info",infoCommand
,1,REDIS_CMD_INLINE
},
360 {"monitor",monitorCommand
,1,REDIS_CMD_INLINE
},
364 /*============================ Utility functions ============================ */
366 /* Glob-style pattern matching. */
367 int stringmatchlen(const char *pattern
, int patternLen
,
368 const char *string
, int stringLen
, int nocase
)
373 while (pattern
[1] == '*') {
378 return 1; /* match */
380 if (stringmatchlen(pattern
+1, patternLen
-1,
381 string
, stringLen
, nocase
))
382 return 1; /* match */
386 return 0; /* no match */
390 return 0; /* no match */
400 not = pattern
[0] == '^';
407 if (pattern
[0] == '\\') {
410 if (pattern
[0] == string
[0])
412 } else if (pattern
[0] == ']') {
414 } else if (patternLen
== 0) {
418 } else if (pattern
[1] == '-' && patternLen
>= 3) {
419 int start
= pattern
[0];
420 int end
= pattern
[2];
428 start
= tolower(start
);
434 if (c
>= start
&& c
<= end
)
438 if (pattern
[0] == string
[0])
441 if (tolower((int)pattern
[0]) == tolower((int)string
[0]))
451 return 0; /* no match */
457 if (patternLen
>= 2) {
464 if (pattern
[0] != string
[0])
465 return 0; /* no match */
467 if (tolower((int)pattern
[0]) != tolower((int)string
[0]))
468 return 0; /* no match */
476 if (stringLen
== 0) {
477 while(*pattern
== '*') {
484 if (patternLen
== 0 && stringLen
== 0)
489 void redisLog(int level
, const char *fmt
, ...)
494 fp
= (server
.logfile
== NULL
) ? stdout
: fopen(server
.logfile
,"a");
498 if (level
>= server
.verbosity
) {
500 fprintf(fp
,"%c ",c
[level
]);
501 vfprintf(fp
, fmt
, ap
);
507 if (server
.logfile
) fclose(fp
);
510 /*====================== Hash table type implementation ==================== */
512 /* This is an hash table type that uses the SDS dynamic strings libary as
513 * keys and radis objects as values (objects can hold SDS strings,
516 static int sdsDictKeyCompare(void *privdata
, const void *key1
,
520 DICT_NOTUSED(privdata
);
522 l1
= sdslen((sds
)key1
);
523 l2
= sdslen((sds
)key2
);
524 if (l1
!= l2
) return 0;
525 return memcmp(key1
, key2
, l1
) == 0;
528 static void dictRedisObjectDestructor(void *privdata
, void *val
)
530 DICT_NOTUSED(privdata
);
535 static int dictSdsKeyCompare(void *privdata
, const void *key1
,
538 const robj
*o1
= key1
, *o2
= key2
;
539 return sdsDictKeyCompare(privdata
,o1
->ptr
,o2
->ptr
);
542 static unsigned int dictSdsHash(const void *key
) {
544 return dictGenHashFunction(o
->ptr
, sdslen((sds
)o
->ptr
));
547 static dictType setDictType
= {
548 dictSdsHash
, /* hash function */
551 dictSdsKeyCompare
, /* key compare */
552 dictRedisObjectDestructor
, /* key destructor */
553 NULL
/* val destructor */
556 static dictType hashDictType
= {
557 dictSdsHash
, /* hash function */
560 dictSdsKeyCompare
, /* key compare */
561 dictRedisObjectDestructor
, /* key destructor */
562 dictRedisObjectDestructor
/* val destructor */
565 /* ========================= Random utility functions ======================= */
567 /* Redis generally does not try to recover from out of memory conditions
568 * when allocating objects or strings, it is not clear if it will be possible
569 * to report this condition to the client since the networking layer itself
570 * is based on heap allocation for send buffers, so we simply abort.
571 * At least the code will be simpler to read... */
572 static void oom(const char *msg
) {
573 fprintf(stderr
, "%s: Out of memory\n",msg
);
579 /* ====================== Redis server networking stuff ===================== */
580 void closeTimedoutClients(void) {
584 time_t now
= time(NULL
);
586 li
= listGetIterator(server
.clients
,AL_START_HEAD
);
588 while ((ln
= listNextElement(li
)) != NULL
) {
589 c
= listNodeValue(ln
);
590 if (!(c
->flags
& REDIS_SLAVE
) && /* no timeout for slaves */
591 (now
- c
->lastinteraction
> server
.maxidletime
)) {
592 redisLog(REDIS_DEBUG
,"Closing idle client");
596 listReleaseIterator(li
);
599 int serverCron(struct aeEventLoop
*eventLoop
, long long id
, void *clientData
) {
600 int j
, size
, used
, loops
= server
.cronloops
++;
601 REDIS_NOTUSED(eventLoop
);
603 REDIS_NOTUSED(clientData
);
605 /* Update the global state with the amount of used memory */
606 server
.usedmemory
= zmalloc_used_memory();
608 /* If the percentage of used slots in the HT reaches REDIS_HT_MINFILL
609 * we resize the hash table to save memory */
610 for (j
= 0; j
< server
.dbnum
; j
++) {
611 size
= dictGetHashTableSize(server
.dict
[j
]);
612 used
= dictGetHashTableUsed(server
.dict
[j
]);
613 if (!(loops
% 5) && used
> 0) {
614 redisLog(REDIS_DEBUG
,"DB %d: %d keys in %d slots HT.",j
,used
,size
);
615 // dictPrintStats(server.dict);
617 if (size
&& used
&& size
> REDIS_HT_MINSLOTS
&&
618 (used
*100/size
< REDIS_HT_MINFILL
)) {
619 redisLog(REDIS_NOTICE
,"The hash table %d is too sparse, resize it...",j
);
620 dictResize(server
.dict
[j
]);
621 redisLog(REDIS_NOTICE
,"Hash table %d resized.",j
);
625 /* Show information about connected clients */
627 redisLog(REDIS_DEBUG
,"%d clients connected (%d slaves), %d bytes in use",
628 listLength(server
.clients
)-listLength(server
.slaves
),
629 listLength(server
.slaves
),
633 /* Close connections of timedout clients */
635 closeTimedoutClients();
637 /* Check if a background saving in progress terminated */
638 if (server
.bgsaveinprogress
) {
640 if (wait4(-1,&statloc
,WNOHANG
,NULL
)) {
641 int exitcode
= WEXITSTATUS(statloc
);
643 redisLog(REDIS_NOTICE
,
644 "Background saving terminated with success");
646 server
.lastsave
= time(NULL
);
648 redisLog(REDIS_WARNING
,
649 "Background saving error");
651 server
.bgsaveinprogress
= 0;
654 /* If there is not a background saving in progress check if
655 * we have to save now */
656 time_t now
= time(NULL
);
657 for (j
= 0; j
< server
.saveparamslen
; j
++) {
658 struct saveparam
*sp
= server
.saveparams
+j
;
660 if (server
.dirty
>= sp
->changes
&&
661 now
-server
.lastsave
> sp
->seconds
) {
662 redisLog(REDIS_NOTICE
,"%d changes in %d seconds. Saving...",
663 sp
->changes
, sp
->seconds
);
664 rdbSaveBackground(server
.dbfilename
);
669 /* Check if we should connect to a MASTER */
670 if (server
.replstate
== REDIS_REPL_CONNECT
) {
671 redisLog(REDIS_NOTICE
,"Connecting to MASTER...");
672 if (syncWithMaster() == REDIS_OK
) {
673 redisLog(REDIS_NOTICE
,"MASTER <-> SLAVE sync succeeded");
679 static void createSharedObjects(void) {
680 shared
.crlf
= createObject(REDIS_STRING
,sdsnew("\r\n"));
681 shared
.ok
= createObject(REDIS_STRING
,sdsnew("+OK\r\n"));
682 shared
.err
= createObject(REDIS_STRING
,sdsnew("-ERR\r\n"));
683 shared
.emptybulk
= createObject(REDIS_STRING
,sdsnew("$0\r\n\r\n"));
684 shared
.czero
= createObject(REDIS_STRING
,sdsnew(":0\r\n"));
685 shared
.cone
= createObject(REDIS_STRING
,sdsnew(":1\r\n"));
686 shared
.nullbulk
= createObject(REDIS_STRING
,sdsnew("$-1\r\n"));
687 shared
.nullmultibulk
= createObject(REDIS_STRING
,sdsnew("*-1\r\n"));
688 shared
.emptymultibulk
= createObject(REDIS_STRING
,sdsnew("*0\r\n"));
690 shared
.pong
= createObject(REDIS_STRING
,sdsnew("+PONG\r\n"));
691 shared
.wrongtypeerr
= createObject(REDIS_STRING
,sdsnew(
692 "-ERR Operation against a key holding the wrong kind of value\r\n"));
693 shared
.nokeyerr
= createObject(REDIS_STRING
,sdsnew(
694 "-ERR no such key\r\n"));
695 shared
.syntaxerr
= createObject(REDIS_STRING
,sdsnew(
696 "-ERR syntax error\r\n"));
697 shared
.sameobjecterr
= createObject(REDIS_STRING
,sdsnew(
698 "-ERR source and destination objects are the same\r\n"));
699 shared
.outofrangeerr
= createObject(REDIS_STRING
,sdsnew(
700 "-ERR index out of range\r\n"));
701 shared
.space
= createObject(REDIS_STRING
,sdsnew(" "));
702 shared
.colon
= createObject(REDIS_STRING
,sdsnew(":"));
703 shared
.plus
= createObject(REDIS_STRING
,sdsnew("+"));
704 shared
.select0
= createStringObject("select 0\r\n",10);
705 shared
.select1
= createStringObject("select 1\r\n",10);
706 shared
.select2
= createStringObject("select 2\r\n",10);
707 shared
.select3
= createStringObject("select 3\r\n",10);
708 shared
.select4
= createStringObject("select 4\r\n",10);
709 shared
.select5
= createStringObject("select 5\r\n",10);
710 shared
.select6
= createStringObject("select 6\r\n",10);
711 shared
.select7
= createStringObject("select 7\r\n",10);
712 shared
.select8
= createStringObject("select 8\r\n",10);
713 shared
.select9
= createStringObject("select 9\r\n",10);
716 static void appendServerSaveParams(time_t seconds
, int changes
) {
717 server
.saveparams
= zrealloc(server
.saveparams
,sizeof(struct saveparam
)*(server
.saveparamslen
+1));
718 if (server
.saveparams
== NULL
) oom("appendServerSaveParams");
719 server
.saveparams
[server
.saveparamslen
].seconds
= seconds
;
720 server
.saveparams
[server
.saveparamslen
].changes
= changes
;
721 server
.saveparamslen
++;
724 static void ResetServerSaveParams() {
725 zfree(server
.saveparams
);
726 server
.saveparams
= NULL
;
727 server
.saveparamslen
= 0;
730 static void initServerConfig() {
731 server
.dbnum
= REDIS_DEFAULT_DBNUM
;
732 server
.port
= REDIS_SERVERPORT
;
733 server
.verbosity
= REDIS_DEBUG
;
734 server
.maxidletime
= REDIS_MAXIDLETIME
;
735 server
.saveparams
= NULL
;
736 server
.logfile
= NULL
; /* NULL = log on standard output */
737 server
.bindaddr
= NULL
;
738 server
.glueoutputbuf
= 1;
739 server
.daemonize
= 0;
740 server
.pidfile
= "/var/run/redis.pid";
741 server
.dbfilename
= "dump.rdb";
742 ResetServerSaveParams();
744 appendServerSaveParams(60*60,1); /* save after 1 hour and 1 change */
745 appendServerSaveParams(300,100); /* save after 5 minutes and 100 changes */
746 appendServerSaveParams(60,10000); /* save after 1 minute and 10000 changes */
747 /* Replication related */
749 server
.masterhost
= NULL
;
750 server
.masterport
= 6379;
751 server
.master
= NULL
;
752 server
.replstate
= REDIS_REPL_NONE
;
755 static void initServer() {
758 signal(SIGHUP
, SIG_IGN
);
759 signal(SIGPIPE
, SIG_IGN
);
761 server
.clients
= listCreate();
762 server
.slaves
= listCreate();
763 server
.monitors
= listCreate();
764 server
.objfreelist
= listCreate();
765 createSharedObjects();
766 server
.el
= aeCreateEventLoop();
767 server
.dict
= zmalloc(sizeof(dict
*)*server
.dbnum
);
768 if (!server
.dict
|| !server
.clients
|| !server
.slaves
|| !server
.monitors
|| !server
.el
|| !server
.objfreelist
)
769 oom("server initialization"); /* Fatal OOM */
770 server
.fd
= anetTcpServer(server
.neterr
, server
.port
, server
.bindaddr
);
771 if (server
.fd
== -1) {
772 redisLog(REDIS_WARNING
, "Opening TCP port: %s", server
.neterr
);
775 for (j
= 0; j
< server
.dbnum
; j
++) {
776 server
.dict
[j
] = dictCreate(&hashDictType
,NULL
);
778 oom("dictCreate"); /* Fatal OOM */
780 server
.cronloops
= 0;
781 server
.bgsaveinprogress
= 0;
782 server
.lastsave
= time(NULL
);
784 server
.usedmemory
= 0;
785 server
.stat_numcommands
= 0;
786 server
.stat_numconnections
= 0;
787 server
.stat_starttime
= time(NULL
);
788 aeCreateTimeEvent(server
.el
, 1000, serverCron
, NULL
, NULL
);
791 /* Empty the whole database */
792 static void emptyDb() {
795 for (j
= 0; j
< server
.dbnum
; j
++)
796 dictEmpty(server
.dict
[j
]);
799 /* I agree, this is a very rudimental way to load a configuration...
800 will improve later if the config gets more complex */
801 static void loadServerConfig(char *filename
) {
802 FILE *fp
= fopen(filename
,"r");
803 char buf
[REDIS_CONFIGLINE_MAX
+1], *err
= NULL
;
808 redisLog(REDIS_WARNING
,"Fatal error, can't open config file");
811 while(fgets(buf
,REDIS_CONFIGLINE_MAX
+1,fp
) != NULL
) {
817 line
= sdstrim(line
," \t\r\n");
819 /* Skip comments and blank lines*/
820 if (line
[0] == '#' || line
[0] == '\0') {
825 /* Split into arguments */
826 argv
= sdssplitlen(line
,sdslen(line
)," ",1,&argc
);
829 /* Execute config directives */
830 if (!strcmp(argv
[0],"timeout") && argc
== 2) {
831 server
.maxidletime
= atoi(argv
[1]);
832 if (server
.maxidletime
< 1) {
833 err
= "Invalid timeout value"; goto loaderr
;
835 } else if (!strcmp(argv
[0],"port") && argc
== 2) {
836 server
.port
= atoi(argv
[1]);
837 if (server
.port
< 1 || server
.port
> 65535) {
838 err
= "Invalid port"; goto loaderr
;
840 } else if (!strcmp(argv
[0],"bind") && argc
== 2) {
841 server
.bindaddr
= zstrdup(argv
[1]);
842 } else if (!strcmp(argv
[0],"save") && argc
== 3) {
843 int seconds
= atoi(argv
[1]);
844 int changes
= atoi(argv
[2]);
845 if (seconds
< 1 || changes
< 0) {
846 err
= "Invalid save parameters"; goto loaderr
;
848 appendServerSaveParams(seconds
,changes
);
849 } else if (!strcmp(argv
[0],"dir") && argc
== 2) {
850 if (chdir(argv
[1]) == -1) {
851 redisLog(REDIS_WARNING
,"Can't chdir to '%s': %s",
852 argv
[1], strerror(errno
));
855 } else if (!strcmp(argv
[0],"loglevel") && argc
== 2) {
856 if (!strcmp(argv
[1],"debug")) server
.verbosity
= REDIS_DEBUG
;
857 else if (!strcmp(argv
[1],"notice")) server
.verbosity
= REDIS_NOTICE
;
858 else if (!strcmp(argv
[1],"warning")) server
.verbosity
= REDIS_WARNING
;
860 err
= "Invalid log level. Must be one of debug, notice, warning";
863 } else if (!strcmp(argv
[0],"logfile") && argc
== 2) {
866 server
.logfile
= zstrdup(argv
[1]);
867 if (!strcmp(server
.logfile
,"stdout")) {
868 zfree(server
.logfile
);
869 server
.logfile
= NULL
;
871 if (server
.logfile
) {
872 /* Test if we are able to open the file. The server will not
873 * be able to abort just for this problem later... */
874 fp
= fopen(server
.logfile
,"a");
876 err
= sdscatprintf(sdsempty(),
877 "Can't open the log file: %s", strerror(errno
));
882 } else if (!strcmp(argv
[0],"databases") && argc
== 2) {
883 server
.dbnum
= atoi(argv
[1]);
884 if (server
.dbnum
< 1) {
885 err
= "Invalid number of databases"; goto loaderr
;
887 } else if (!strcmp(argv
[0],"slaveof") && argc
== 3) {
888 server
.masterhost
= sdsnew(argv
[1]);
889 server
.masterport
= atoi(argv
[2]);
890 server
.replstate
= REDIS_REPL_CONNECT
;
891 } else if (!strcmp(argv
[0],"glueoutputbuf") && argc
== 2) {
893 if (!strcmp(argv
[1],"yes")) server
.glueoutputbuf
= 1;
894 else if (!strcmp(argv
[1],"no")) server
.glueoutputbuf
= 0;
896 err
= "argument must be 'yes' or 'no'"; goto loaderr
;
898 } else if (!strcmp(argv
[0],"daemonize") && argc
== 2) {
900 if (!strcmp(argv
[1],"yes")) server
.daemonize
= 1;
901 else if (!strcmp(argv
[1],"no")) server
.daemonize
= 0;
903 err
= "argument must be 'yes' or 'no'"; goto loaderr
;
905 } else if (!strcmp(argv
[0],"pidfile") && argc
== 2) {
906 server
.pidfile
= zstrdup(argv
[1]);
908 err
= "Bad directive or wrong number of arguments"; goto loaderr
;
910 for (j
= 0; j
< argc
; j
++)
919 fprintf(stderr
, "\n*** FATAL CONFIG FILE ERROR ***\n");
920 fprintf(stderr
, "Reading the configuration file, at line %d\n", linenum
);
921 fprintf(stderr
, ">>> '%s'\n", line
);
922 fprintf(stderr
, "%s\n", err
);
926 static void freeClientArgv(redisClient
*c
) {
929 for (j
= 0; j
< c
->argc
; j
++)
930 decrRefCount(c
->argv
[j
]);
934 static void freeClient(redisClient
*c
) {
937 aeDeleteFileEvent(server
.el
,c
->fd
,AE_READABLE
);
938 aeDeleteFileEvent(server
.el
,c
->fd
,AE_WRITABLE
);
939 sdsfree(c
->querybuf
);
940 listRelease(c
->reply
);
943 ln
= listSearchKey(server
.clients
,c
);
945 listDelNode(server
.clients
,ln
);
946 if (c
->flags
& REDIS_SLAVE
) {
947 list
*l
= (c
->flags
& REDIS_MONITOR
) ? server
.monitors
: server
.slaves
;
948 ln
= listSearchKey(l
,c
);
952 if (c
->flags
& REDIS_MASTER
) {
953 server
.master
= NULL
;
954 server
.replstate
= REDIS_REPL_CONNECT
;
959 static void glueReplyBuffersIfNeeded(redisClient
*c
) {
961 listNode
*ln
= c
->reply
->head
, *next
;
966 totlen
+= sdslen(o
->ptr
);
968 /* This optimization makes more sense if we don't have to copy
970 if (totlen
> 1024) return;
980 memcpy(buf
+copylen
,o
->ptr
,sdslen(o
->ptr
));
981 copylen
+= sdslen(o
->ptr
);
982 listDelNode(c
->reply
,ln
);
985 /* Now the output buffer is empty, add the new single element */
986 addReplySds(c
,sdsnewlen(buf
,totlen
));
990 static void sendReplyToClient(aeEventLoop
*el
, int fd
, void *privdata
, int mask
) {
991 redisClient
*c
= privdata
;
992 int nwritten
= 0, totwritten
= 0, objlen
;
997 if (server
.glueoutputbuf
&& listLength(c
->reply
) > 1)
998 glueReplyBuffersIfNeeded(c
);
999 while(listLength(c
->reply
)) {
1000 o
= listNodeValue(listFirst(c
->reply
));
1001 objlen
= sdslen(o
->ptr
);
1004 listDelNode(c
->reply
,listFirst(c
->reply
));
1008 if (c
->flags
& REDIS_MASTER
) {
1009 nwritten
= objlen
- c
->sentlen
;
1011 nwritten
= write(fd
, o
->ptr
+c
->sentlen
, objlen
- c
->sentlen
);
1012 if (nwritten
<= 0) break;
1014 c
->sentlen
+= nwritten
;
1015 totwritten
+= nwritten
;
1016 /* If we fully sent the object on head go to the next one */
1017 if (c
->sentlen
== objlen
) {
1018 listDelNode(c
->reply
,listFirst(c
->reply
));
1022 if (nwritten
== -1) {
1023 if (errno
== EAGAIN
) {
1026 redisLog(REDIS_DEBUG
,
1027 "Error writing to client: %s", strerror(errno
));
1032 if (totwritten
> 0) c
->lastinteraction
= time(NULL
);
1033 if (listLength(c
->reply
) == 0) {
1035 aeDeleteFileEvent(server
.el
,c
->fd
,AE_WRITABLE
);
1039 static struct redisCommand
*lookupCommand(char *name
) {
1041 while(cmdTable
[j
].name
!= NULL
) {
1042 if (!strcmp(name
,cmdTable
[j
].name
)) return &cmdTable
[j
];
1048 /* resetClient prepare the client to process the next command */
1049 static void resetClient(redisClient
*c
) {
1054 /* If this function gets called we already read a whole
1055 * command, argments are in the client argv/argc fields.
1056 * processCommand() execute the command or prepare the
1057 * server for a bulk read from the client.
1059 * If 1 is returned the client is still alive and valid and
1060 * and other operations can be performed by the caller. Otherwise
1061 * if 0 is returned the client was destroied (i.e. after QUIT). */
1062 static int processCommand(redisClient
*c
) {
1063 struct redisCommand
*cmd
;
1066 sdstolower(c
->argv
[0]->ptr
);
1067 /* The QUIT command is handled as a special case. Normal command
1068 * procs are unable to close the client connection safely */
1069 if (!strcmp(c
->argv
[0]->ptr
,"quit")) {
1073 cmd
= lookupCommand(c
->argv
[0]->ptr
);
1075 addReplySds(c
,sdsnew("-ERR unknown command\r\n"));
1078 } else if ((cmd
->arity
> 0 && cmd
->arity
!= c
->argc
) ||
1079 (c
->argc
< -cmd
->arity
)) {
1080 addReplySds(c
,sdsnew("-ERR wrong number of arguments\r\n"));
1083 } else if (cmd
->flags
& REDIS_CMD_BULK
&& c
->bulklen
== -1) {
1084 int bulklen
= atoi(c
->argv
[c
->argc
-1]->ptr
);
1086 decrRefCount(c
->argv
[c
->argc
-1]);
1087 if (bulklen
< 0 || bulklen
> 1024*1024*1024) {
1089 addReplySds(c
,sdsnew("-ERR invalid bulk write count\r\n"));
1094 c
->bulklen
= bulklen
+2; /* add two bytes for CR+LF */
1095 /* It is possible that the bulk read is already in the
1096 * buffer. Check this condition and handle it accordingly */
1097 if ((signed)sdslen(c
->querybuf
) >= c
->bulklen
) {
1098 c
->argv
[c
->argc
] = createStringObject(c
->querybuf
,c
->bulklen
-2);
1100 c
->querybuf
= sdsrange(c
->querybuf
,c
->bulklen
,-1);
1105 /* Exec the command */
1106 dirty
= server
.dirty
;
1108 if (server
.dirty
-dirty
!= 0 && listLength(server
.slaves
))
1109 replicationFeedSlaves(server
.slaves
,cmd
,c
->dictid
,c
->argv
,c
->argc
);
1110 if (listLength(server
.monitors
))
1111 replicationFeedSlaves(server
.monitors
,cmd
,c
->dictid
,c
->argv
,c
->argc
);
1112 server
.stat_numcommands
++;
1114 /* Prepare the client for the next command */
1115 if (c
->flags
& REDIS_CLOSE
) {
1123 static void replicationFeedSlaves(list
*slaves
, struct redisCommand
*cmd
, int dictid
, robj
**argv
, int argc
) {
1124 listNode
*ln
= slaves
->head
;
1125 robj
*outv
[REDIS_MAX_ARGS
*4]; /* enough room for args, spaces, newlines */
1128 for (j
= 0; j
< argc
; j
++) {
1129 if (j
!= 0) outv
[outc
++] = shared
.space
;
1130 if ((cmd
->flags
& REDIS_CMD_BULK
) && j
== argc
-1) {
1133 lenobj
= createObject(REDIS_STRING
,
1134 sdscatprintf(sdsempty(),"%d\r\n",sdslen(argv
[j
]->ptr
)));
1135 lenobj
->refcount
= 0;
1136 outv
[outc
++] = lenobj
;
1138 outv
[outc
++] = argv
[j
];
1140 outv
[outc
++] = shared
.crlf
;
1143 redisClient
*slave
= ln
->value
;
1144 if (slave
->slaveseldb
!= dictid
) {
1148 case 0: selectcmd
= shared
.select0
; break;
1149 case 1: selectcmd
= shared
.select1
; break;
1150 case 2: selectcmd
= shared
.select2
; break;
1151 case 3: selectcmd
= shared
.select3
; break;
1152 case 4: selectcmd
= shared
.select4
; break;
1153 case 5: selectcmd
= shared
.select5
; break;
1154 case 6: selectcmd
= shared
.select6
; break;
1155 case 7: selectcmd
= shared
.select7
; break;
1156 case 8: selectcmd
= shared
.select8
; break;
1157 case 9: selectcmd
= shared
.select9
; break;
1159 selectcmd
= createObject(REDIS_STRING
,
1160 sdscatprintf(sdsempty(),"select %d\r\n",dictid
));
1161 selectcmd
->refcount
= 0;
1164 addReply(slave
,selectcmd
);
1165 slave
->slaveseldb
= dictid
;
1167 for (j
= 0; j
< outc
; j
++) addReply(slave
,outv
[j
]);
1172 static void readQueryFromClient(aeEventLoop
*el
, int fd
, void *privdata
, int mask
) {
1173 redisClient
*c
= (redisClient
*) privdata
;
1174 char buf
[REDIS_QUERYBUF_LEN
];
1177 REDIS_NOTUSED(mask
);
1179 nread
= read(fd
, buf
, REDIS_QUERYBUF_LEN
);
1181 if (errno
== EAGAIN
) {
1184 redisLog(REDIS_DEBUG
, "Reading from client: %s",strerror(errno
));
1188 } else if (nread
== 0) {
1189 redisLog(REDIS_DEBUG
, "Client closed connection");
1194 c
->querybuf
= sdscatlen(c
->querybuf
, buf
, nread
);
1195 c
->lastinteraction
= time(NULL
);
1201 if (c
->bulklen
== -1) {
1202 /* Read the first line of the query */
1203 char *p
= strchr(c
->querybuf
,'\n');
1209 query
= c
->querybuf
;
1210 c
->querybuf
= sdsempty();
1211 querylen
= 1+(p
-(query
));
1212 if (sdslen(query
) > querylen
) {
1213 /* leave data after the first line of the query in the buffer */
1214 c
->querybuf
= sdscatlen(c
->querybuf
,query
+querylen
,sdslen(query
)-querylen
);
1216 *p
= '\0'; /* remove "\n" */
1217 if (*(p
-1) == '\r') *(p
-1) = '\0'; /* and "\r" if any */
1218 sdsupdatelen(query
);
1220 /* Now we can split the query in arguments */
1221 if (sdslen(query
) == 0) {
1222 /* Ignore empty query */
1226 argv
= sdssplitlen(query
,sdslen(query
)," ",1,&argc
);
1228 if (argv
== NULL
) oom("sdssplitlen");
1229 for (j
= 0; j
< argc
&& j
< REDIS_MAX_ARGS
; j
++) {
1230 if (sdslen(argv
[j
])) {
1231 c
->argv
[c
->argc
] = createObject(REDIS_STRING
,argv
[j
]);
1238 /* Execute the command. If the client is still valid
1239 * after processCommand() return and there is something
1240 * on the query buffer try to process the next command. */
1241 if (processCommand(c
) && sdslen(c
->querybuf
)) goto again
;
1243 } else if (sdslen(c
->querybuf
) >= 1024) {
1244 redisLog(REDIS_DEBUG
, "Client protocol error");
1249 /* Bulk read handling. Note that if we are at this point
1250 the client already sent a command terminated with a newline,
1251 we are reading the bulk data that is actually the last
1252 argument of the command. */
1253 int qbl
= sdslen(c
->querybuf
);
1255 if (c
->bulklen
<= qbl
) {
1256 /* Copy everything but the final CRLF as final argument */
1257 c
->argv
[c
->argc
] = createStringObject(c
->querybuf
,c
->bulklen
-2);
1259 c
->querybuf
= sdsrange(c
->querybuf
,c
->bulklen
,-1);
1266 static int selectDb(redisClient
*c
, int id
) {
1267 if (id
< 0 || id
>= server
.dbnum
)
1269 c
->dict
= server
.dict
[id
];
1274 static redisClient
*createClient(int fd
) {
1275 redisClient
*c
= zmalloc(sizeof(*c
));
1277 anetNonBlock(NULL
,fd
);
1278 anetTcpNoDelay(NULL
,fd
);
1279 if (!c
) return NULL
;
1282 c
->querybuf
= sdsempty();
1287 c
->lastinteraction
= time(NULL
);
1288 if ((c
->reply
= listCreate()) == NULL
) oom("listCreate");
1289 listSetFreeMethod(c
->reply
,decrRefCount
);
1290 if (aeCreateFileEvent(server
.el
, c
->fd
, AE_READABLE
,
1291 readQueryFromClient
, c
, NULL
) == AE_ERR
) {
1295 if (!listAddNodeTail(server
.clients
,c
)) oom("listAddNodeTail");
1299 static void addReply(redisClient
*c
, robj
*obj
) {
1300 if (listLength(c
->reply
) == 0 &&
1301 aeCreateFileEvent(server
.el
, c
->fd
, AE_WRITABLE
,
1302 sendReplyToClient
, c
, NULL
) == AE_ERR
) return;
1303 if (!listAddNodeTail(c
->reply
,obj
)) oom("listAddNodeTail");
1307 static void addReplySds(redisClient
*c
, sds s
) {
1308 robj
*o
= createObject(REDIS_STRING
,s
);
1313 static void acceptHandler(aeEventLoop
*el
, int fd
, void *privdata
, int mask
) {
1317 REDIS_NOTUSED(mask
);
1318 REDIS_NOTUSED(privdata
);
1320 cfd
= anetAccept(server
.neterr
, fd
, cip
, &cport
);
1321 if (cfd
== AE_ERR
) {
1322 redisLog(REDIS_DEBUG
,"Accepting client connection: %s", server
.neterr
);
1325 redisLog(REDIS_DEBUG
,"Accepted %s:%d", cip
, cport
);
1326 if (createClient(cfd
) == NULL
) {
1327 redisLog(REDIS_WARNING
,"Error allocating resoures for the client");
1328 close(cfd
); /* May be already closed, just ingore errors */
1331 server
.stat_numconnections
++;
1334 /* ======================= Redis objects implementation ===================== */
1336 static robj
*createObject(int type
, void *ptr
) {
1339 if (listLength(server
.objfreelist
)) {
1340 listNode
*head
= listFirst(server
.objfreelist
);
1341 o
= listNodeValue(head
);
1342 listDelNode(server
.objfreelist
,head
);
1344 o
= zmalloc(sizeof(*o
));
1346 if (!o
) oom("createObject");
1353 static robj
*createStringObject(char *ptr
, size_t len
) {
1354 return createObject(REDIS_STRING
,sdsnewlen(ptr
,len
));
1357 static robj
*createListObject(void) {
1358 list
*l
= listCreate();
1360 if (!l
) oom("listCreate");
1361 listSetFreeMethod(l
,decrRefCount
);
1362 return createObject(REDIS_LIST
,l
);
1365 static robj
*createSetObject(void) {
1366 dict
*d
= dictCreate(&setDictType
,NULL
);
1367 if (!d
) oom("dictCreate");
1368 return createObject(REDIS_SET
,d
);
1372 static robj
*createHashObject(void) {
1373 dict
*d
= dictCreate(&hashDictType
,NULL
);
1374 if (!d
) oom("dictCreate");
1375 return createObject(REDIS_SET
,d
);
1379 static void freeStringObject(robj
*o
) {
1383 static void freeListObject(robj
*o
) {
1384 listRelease((list
*) o
->ptr
);
1387 static void freeSetObject(robj
*o
) {
1388 dictRelease((dict
*) o
->ptr
);
1391 static void freeHashObject(robj
*o
) {
1392 dictRelease((dict
*) o
->ptr
);
1395 static void incrRefCount(robj
*o
) {
1399 static void decrRefCount(void *obj
) {
1401 if (--(o
->refcount
) == 0) {
1403 case REDIS_STRING
: freeStringObject(o
); break;
1404 case REDIS_LIST
: freeListObject(o
); break;
1405 case REDIS_SET
: freeSetObject(o
); break;
1406 case REDIS_HASH
: freeHashObject(o
); break;
1407 default: assert(0 != 0); break;
1409 if (listLength(server
.objfreelist
) > REDIS_OBJFREELIST_MAX
||
1410 !listAddNodeHead(server
.objfreelist
,o
))
1415 /*============================ DB saving/loading ============================ */
1417 static int rdbSaveType(FILE *fp
, unsigned char type
) {
1418 if (fwrite(&type
,1,1,fp
) == 0) return -1;
1422 static int rdbSaveLen(FILE *fp
, uint32_t len
) {
1423 unsigned char buf
[2];
1426 /* Save a 6 bit len */
1427 buf
[0] = (len
&0xFF)|REDIS_RDB_6BITLEN
;
1428 if (fwrite(buf
,1,1,fp
) == 0) return -1;
1429 } else if (len
< (1<<14)) {
1430 /* Save a 14 bit len */
1431 buf
[0] = ((len
>>8)&0xFF)|REDIS_RDB_14BITLEN
;
1433 if (fwrite(buf
,4,1,fp
) == 0) return -1;
1435 /* Save a 32 bit len */
1436 buf
[0] = REDIS_RDB_32BITLEN
;
1437 if (fwrite(buf
,1,1,fp
) == 0) return -1;
1439 if (fwrite(&len
,4,1,fp
) == 0) return -1;
1444 /* Save the DB on disk. Return REDIS_ERR on error, REDIS_OK on success */
1445 static int rdbSave(char *filename
) {
1446 dictIterator
*di
= NULL
;
1452 snprintf(tmpfile
,256,"temp-%d.%ld.rdb",(int)time(NULL
),(long int)random());
1453 fp
= fopen(tmpfile
,"w");
1455 redisLog(REDIS_WARNING
, "Failed saving the DB: %s", strerror(errno
));
1458 if (fwrite("REDIS0001",9,1,fp
) == 0) goto werr
;
1459 for (j
= 0; j
< server
.dbnum
; j
++) {
1460 dict
*d
= server
.dict
[j
];
1461 if (dictGetHashTableUsed(d
) == 0) continue;
1462 di
= dictGetIterator(d
);
1468 /* Write the SELECT DB opcode */
1469 if (rdbSaveType(fp
,REDIS_SELECTDB
) == -1) goto werr
;
1470 if (rdbSaveLen(fp
,j
) == -1) goto werr
;
1472 /* Iterate this DB writing every entry */
1473 while((de
= dictNext(di
)) != NULL
) {
1474 robj
*key
= dictGetEntryKey(de
);
1475 robj
*o
= dictGetEntryVal(de
);
1477 if (rdbSaveType(fp
,o
->type
) == -1) goto werr
;
1478 if (rdbSaveLen(fp
,sdslen(key
->ptr
)) == -1) goto werr
;
1479 if (fwrite(key
->ptr
,sdslen(key
->ptr
),1,fp
) == 0) goto werr
;
1480 if (o
->type
== REDIS_STRING
) {
1481 /* Save a string value */
1484 if (rdbSaveLen(fp
,sdslen(sval
)) == -1) goto werr
;
1486 fwrite(sval
,sdslen(sval
),1,fp
) == 0) goto werr
;
1487 } else if (o
->type
== REDIS_LIST
) {
1488 /* Save a list value */
1489 list
*list
= o
->ptr
;
1490 listNode
*ln
= list
->head
;
1492 if (rdbSaveLen(fp
,listLength(list
)) == -1) goto werr
;
1494 robj
*eleobj
= listNodeValue(ln
);
1496 if (rdbSaveLen(fp
,sdslen(eleobj
->ptr
)) == -1) goto werr
;
1497 if (sdslen(eleobj
->ptr
) &&
1498 fwrite(eleobj
->ptr
,sdslen(eleobj
->ptr
),1,fp
) == 0)
1502 } else if (o
->type
== REDIS_SET
) {
1503 /* Save a set value */
1505 dictIterator
*di
= dictGetIterator(set
);
1508 if (!set
) oom("dictGetIteraotr");
1509 if (rdbSaveLen(fp
,dictGetHashTableUsed(set
)) == -1) goto werr
;
1510 while((de
= dictNext(di
)) != NULL
) {
1513 eleobj
= dictGetEntryKey(de
);
1514 if (rdbSaveLen(fp
,sdslen(eleobj
->ptr
)) == -1) goto werr
;
1515 if (sdslen(eleobj
->ptr
) &&
1516 fwrite(eleobj
->ptr
,sdslen(eleobj
->ptr
),1,fp
) == 0)
1519 dictReleaseIterator(di
);
1524 dictReleaseIterator(di
);
1527 if (rdbSaveType(fp
,REDIS_EOF
) == -1) goto werr
;
1529 /* Make sure data will not remain on the OS's output buffers */
1534 /* Use RENAME to make sure the DB file is changed atomically only
1535 * if the generate DB file is ok. */
1536 if (rename(tmpfile
,filename
) == -1) {
1537 redisLog(REDIS_WARNING
,"Error moving temp DB file on the final destionation: %s", strerror(errno
));
1541 redisLog(REDIS_NOTICE
,"DB saved on disk");
1543 server
.lastsave
= time(NULL
);
1549 redisLog(REDIS_WARNING
,"Write error saving DB on disk: %s", strerror(errno
));
1550 if (di
) dictReleaseIterator(di
);
1554 static int rdbSaveBackground(char *filename
) {
1557 if (server
.bgsaveinprogress
) return REDIS_ERR
;
1558 if ((childpid
= fork()) == 0) {
1561 if (rdbSave(filename
) == REDIS_OK
) {
1568 redisLog(REDIS_NOTICE
,"Background saving started by pid %d",childpid
);
1569 server
.bgsaveinprogress
= 1;
1572 return REDIS_OK
; /* unreached */
1575 static int rdbLoadType(FILE *fp
) {
1577 if (fread(&type
,1,1,fp
) == 0) return -1;
1581 static uint32_t rdbLoadLen(FILE *fp
, int rdbver
) {
1582 unsigned char buf
[2];
1586 if (fread(&len
,4,1,fp
) == 0) return REDIS_RDB_LENERR
;
1589 if (fread(buf
,1,1,fp
) == 0) return REDIS_RDB_LENERR
;
1590 if ((buf
[0]&0xC0) == REDIS_RDB_6BITLEN
) {
1591 /* Read a 6 bit len */
1593 } else if ((buf
[0]&0xC0) == REDIS_RDB_14BITLEN
) {
1594 /* Read a 14 bit len */
1595 if (fread(buf
+1,1,1,fp
) == 0) return REDIS_RDB_LENERR
;
1596 return ((buf
[0]&0x3F)<<8)|buf
[1];
1598 /* Read a 32 bit len */
1599 if (fread(&len
,4,1,fp
) == 0) return REDIS_RDB_LENERR
;
1606 static robj
*rdbLoadStringObject(FILE*fp
,int rdbver
) {
1607 uint32_t len
= rdbLoadLen(fp
,rdbver
);
1610 if (len
== REDIS_RDB_LENERR
) return NULL
;
1611 val
= sdsnewlen(NULL
,len
);
1612 if (len
&& fread(val
,len
,1,fp
) == 0) {
1616 return createObject(REDIS_STRING
,val
);
1619 static int rdbLoad(char *filename
) {
1621 robj
*keyobj
= NULL
;
1625 dict
*d
= server
.dict
[0];
1629 fp
= fopen(filename
,"r");
1630 if (!fp
) return REDIS_ERR
;
1631 if (fread(buf
,9,1,fp
) == 0) goto eoferr
;
1633 if (memcmp(buf
,"REDIS",5) != 0) {
1635 redisLog(REDIS_WARNING
,"Wrong signature trying to load DB from file");
1638 rdbver
= atoi(buf
+5);
1641 redisLog(REDIS_WARNING
,"Can't handle RDB format version %d",rdbver
);
1648 if ((type
= rdbLoadType(fp
)) == -1) goto eoferr
;
1649 if (type
== REDIS_EOF
) break;
1650 /* Handle SELECT DB opcode as a special case */
1651 if (type
== REDIS_SELECTDB
) {
1652 if ((dbid
= rdbLoadLen(fp
,rdbver
)) == REDIS_RDB_LENERR
) goto eoferr
;
1653 if (dbid
>= (unsigned)server
.dbnum
) {
1654 redisLog(REDIS_WARNING
,"FATAL: Data file was created with a Redis server configured to handle more than %d databases. Exiting\n", server
.dbnum
);
1657 d
= server
.dict
[dbid
];
1661 if ((keyobj
= rdbLoadStringObject(fp
,rdbver
)) == NULL
) goto eoferr
;
1663 if (type
== REDIS_STRING
) {
1664 /* Read string value */
1665 if ((o
= rdbLoadStringObject(fp
,rdbver
)) == NULL
) goto eoferr
;
1666 } else if (type
== REDIS_LIST
|| type
== REDIS_SET
) {
1667 /* Read list/set value */
1670 if ((listlen
= rdbLoadLen(fp
,rdbver
)) == REDIS_RDB_LENERR
)
1672 o
= (type
== REDIS_LIST
) ? createListObject() : createSetObject();
1673 /* Load every single element of the list/set */
1677 if ((ele
= rdbLoadStringObject(fp
,rdbver
)) == NULL
) goto eoferr
;
1678 if (type
== REDIS_LIST
) {
1679 if (!listAddNodeTail((list
*)o
->ptr
,ele
))
1680 oom("listAddNodeTail");
1682 if (dictAdd((dict
*)o
->ptr
,ele
,NULL
) == DICT_ERR
)
1689 /* Add the new object in the hash table */
1690 retval
= dictAdd(d
,keyobj
,o
);
1691 if (retval
== DICT_ERR
) {
1692 redisLog(REDIS_WARNING
,"Loading DB, duplicated key (%s) found! Unrecoverable error, exiting now.", keyobj
->ptr
);
1700 eoferr
: /* unexpected end of file is handled here with a fatal exit */
1701 decrRefCount(keyobj
);
1702 redisLog(REDIS_WARNING
,"Short read loading DB. Unrecoverable error, exiting now.");
1704 return REDIS_ERR
; /* Just to avoid warning */
1707 /*================================== Commands =============================== */
1709 static void pingCommand(redisClient
*c
) {
1710 addReply(c
,shared
.pong
);
1713 static void echoCommand(redisClient
*c
) {
1714 addReplySds(c
,sdscatprintf(sdsempty(),"$%d\r\n",
1715 (int)sdslen(c
->argv
[1]->ptr
)));
1716 addReply(c
,c
->argv
[1]);
1717 addReply(c
,shared
.crlf
);
1720 /*=================================== Strings =============================== */
1722 static void setGenericCommand(redisClient
*c
, int nx
) {
1725 retval
= dictAdd(c
->dict
,c
->argv
[1],c
->argv
[2]);
1726 if (retval
== DICT_ERR
) {
1728 dictReplace(c
->dict
,c
->argv
[1],c
->argv
[2]);
1729 incrRefCount(c
->argv
[2]);
1731 addReply(c
,shared
.czero
);
1735 incrRefCount(c
->argv
[1]);
1736 incrRefCount(c
->argv
[2]);
1739 addReply(c
, nx
? shared
.cone
: shared
.ok
);
1742 static void setCommand(redisClient
*c
) {
1743 return setGenericCommand(c
,0);
1746 static void setnxCommand(redisClient
*c
) {
1747 return setGenericCommand(c
,1);
1750 static void getCommand(redisClient
*c
) {
1753 de
= dictFind(c
->dict
,c
->argv
[1]);
1755 addReply(c
,shared
.nullbulk
);
1757 robj
*o
= dictGetEntryVal(de
);
1759 if (o
->type
!= REDIS_STRING
) {
1760 addReply(c
,shared
.wrongtypeerr
);
1762 addReplySds(c
,sdscatprintf(sdsempty(),"$%d\r\n",(int)sdslen(o
->ptr
)));
1764 addReply(c
,shared
.crlf
);
1769 static void mgetCommand(redisClient
*c
) {
1773 addReplySds(c
,sdscatprintf(sdsempty(),"*%d\r\n",c
->argc
-1));
1774 for (j
= 1; j
< c
->argc
; j
++) {
1775 de
= dictFind(c
->dict
,c
->argv
[j
]);
1777 addReply(c
,shared
.nullbulk
);
1779 robj
*o
= dictGetEntryVal(de
);
1781 if (o
->type
!= REDIS_STRING
) {
1782 addReply(c
,shared
.nullbulk
);
1784 addReplySds(c
,sdscatprintf(sdsempty(),"$%d\r\n",(int)sdslen(o
->ptr
)));
1786 addReply(c
,shared
.crlf
);
1792 static void incrDecrCommand(redisClient
*c
, int incr
) {
1798 de
= dictFind(c
->dict
,c
->argv
[1]);
1802 robj
*o
= dictGetEntryVal(de
);
1804 if (o
->type
!= REDIS_STRING
) {
1809 value
= strtoll(o
->ptr
, &eptr
, 10);
1814 o
= createObject(REDIS_STRING
,sdscatprintf(sdsempty(),"%lld",value
));
1815 retval
= dictAdd(c
->dict
,c
->argv
[1],o
);
1816 if (retval
== DICT_ERR
) {
1817 dictReplace(c
->dict
,c
->argv
[1],o
);
1819 incrRefCount(c
->argv
[1]);
1822 addReply(c
,shared
.colon
);
1824 addReply(c
,shared
.crlf
);
1827 static void incrCommand(redisClient
*c
) {
1828 return incrDecrCommand(c
,1);
1831 static void decrCommand(redisClient
*c
) {
1832 return incrDecrCommand(c
,-1);
1835 static void incrbyCommand(redisClient
*c
) {
1836 int incr
= atoi(c
->argv
[2]->ptr
);
1837 return incrDecrCommand(c
,incr
);
1840 static void decrbyCommand(redisClient
*c
) {
1841 int incr
= atoi(c
->argv
[2]->ptr
);
1842 return incrDecrCommand(c
,-incr
);
1845 /* ========================= Type agnostic commands ========================= */
1847 static void delCommand(redisClient
*c
) {
1848 if (dictDelete(c
->dict
,c
->argv
[1]) == DICT_OK
) {
1850 addReply(c
,shared
.cone
);
1852 addReply(c
,shared
.czero
);
1856 static void existsCommand(redisClient
*c
) {
1859 de
= dictFind(c
->dict
,c
->argv
[1]);
1861 addReply(c
,shared
.czero
);
1863 addReply(c
,shared
.cone
);
1866 static void selectCommand(redisClient
*c
) {
1867 int id
= atoi(c
->argv
[1]->ptr
);
1869 if (selectDb(c
,id
) == REDIS_ERR
) {
1870 addReplySds(c
,"-ERR invalid DB index\r\n");
1872 addReply(c
,shared
.ok
);
1876 static void randomkeyCommand(redisClient
*c
) {
1879 de
= dictGetRandomKey(c
->dict
);
1881 addReply(c
,shared
.crlf
);
1883 addReply(c
,shared
.plus
);
1884 addReply(c
,dictGetEntryKey(de
));
1885 addReply(c
,shared
.crlf
);
1889 static void keysCommand(redisClient
*c
) {
1892 sds pattern
= c
->argv
[1]->ptr
;
1893 int plen
= sdslen(pattern
);
1894 int numkeys
= 0, keyslen
= 0;
1895 robj
*lenobj
= createObject(REDIS_STRING
,NULL
);
1897 di
= dictGetIterator(c
->dict
);
1898 if (!di
) oom("dictGetIterator");
1900 decrRefCount(lenobj
);
1901 while((de
= dictNext(di
)) != NULL
) {
1902 robj
*keyobj
= dictGetEntryKey(de
);
1903 sds key
= keyobj
->ptr
;
1904 if ((pattern
[0] == '*' && pattern
[1] == '\0') ||
1905 stringmatchlen(pattern
,plen
,key
,sdslen(key
),0)) {
1907 addReply(c
,shared
.space
);
1910 keyslen
+= sdslen(key
);
1913 dictReleaseIterator(di
);
1914 lenobj
->ptr
= sdscatprintf(sdsempty(),"$%lu\r\n",keyslen
+(numkeys
? (numkeys
-1) : 0));
1915 addReply(c
,shared
.crlf
);
1918 static void dbsizeCommand(redisClient
*c
) {
1920 sdscatprintf(sdsempty(),":%lu\r\n",dictGetHashTableUsed(c
->dict
)));
1923 static void lastsaveCommand(redisClient
*c
) {
1925 sdscatprintf(sdsempty(),":%lu\r\n",server
.lastsave
));
1928 static void typeCommand(redisClient
*c
) {
1932 de
= dictFind(c
->dict
,c
->argv
[1]);
1936 robj
*o
= dictGetEntryVal(de
);
1939 case REDIS_STRING
: type
= "+string"; break;
1940 case REDIS_LIST
: type
= "+list"; break;
1941 case REDIS_SET
: type
= "+set"; break;
1942 default: type
= "unknown"; break;
1945 addReplySds(c
,sdsnew(type
));
1946 addReply(c
,shared
.crlf
);
1949 static void saveCommand(redisClient
*c
) {
1950 if (rdbSave(server
.dbfilename
) == REDIS_OK
) {
1951 addReply(c
,shared
.ok
);
1953 addReply(c
,shared
.err
);
1957 static void bgsaveCommand(redisClient
*c
) {
1958 if (server
.bgsaveinprogress
) {
1959 addReplySds(c
,sdsnew("-ERR background save already in progress\r\n"));
1962 if (rdbSaveBackground(server
.dbfilename
) == REDIS_OK
) {
1963 addReply(c
,shared
.ok
);
1965 addReply(c
,shared
.err
);
1969 static void shutdownCommand(redisClient
*c
) {
1970 redisLog(REDIS_WARNING
,"User requested shutdown, saving DB...");
1971 if (rdbSave(server
.dbfilename
) == REDIS_OK
) {
1972 if (server
.daemonize
) {
1973 unlink(server
.pidfile
);
1975 redisLog(REDIS_WARNING
,"Server exit now, bye bye...");
1978 redisLog(REDIS_WARNING
,"Error trying to save the DB, can't exit");
1979 addReplySds(c
,sdsnew("-ERR can't quit, problems saving the DB\r\n"));
1983 static void renameGenericCommand(redisClient
*c
, int nx
) {
1987 /* To use the same key as src and dst is probably an error */
1988 if (sdscmp(c
->argv
[1]->ptr
,c
->argv
[2]->ptr
) == 0) {
1989 addReply(c
,shared
.sameobjecterr
);
1993 de
= dictFind(c
->dict
,c
->argv
[1]);
1995 addReply(c
,shared
.nokeyerr
);
1998 o
= dictGetEntryVal(de
);
2000 if (dictAdd(c
->dict
,c
->argv
[2],o
) == DICT_ERR
) {
2003 addReply(c
,shared
.czero
);
2006 dictReplace(c
->dict
,c
->argv
[2],o
);
2008 incrRefCount(c
->argv
[2]);
2010 dictDelete(c
->dict
,c
->argv
[1]);
2012 addReply(c
,nx
? shared
.cone
: shared
.ok
);
2015 static void renameCommand(redisClient
*c
) {
2016 renameGenericCommand(c
,0);
2019 static void renamenxCommand(redisClient
*c
) {
2020 renameGenericCommand(c
,1);
2023 static void moveCommand(redisClient
*c
) {
2029 /* Obtain source and target DB pointers */
2032 if (selectDb(c
,atoi(c
->argv
[2]->ptr
)) == REDIS_ERR
) {
2033 addReply(c
,shared
.outofrangeerr
);
2040 /* If the user is moving using as target the same
2041 * DB as the source DB it is probably an error. */
2043 addReply(c
,shared
.sameobjecterr
);
2047 /* Check if the element exists and get a reference */
2048 de
= dictFind(c
->dict
,c
->argv
[1]);
2050 addReply(c
,shared
.czero
);
2054 /* Try to add the element to the target DB */
2055 key
= dictGetEntryKey(de
);
2056 o
= dictGetEntryVal(de
);
2057 if (dictAdd(dst
,key
,o
) == DICT_ERR
) {
2058 addReply(c
,shared
.czero
);
2064 /* OK! key moved, free the entry in the source DB */
2065 dictDelete(src
,c
->argv
[1]);
2067 addReply(c
,shared
.cone
);
2070 /* =================================== Lists ================================ */
2071 static void pushGenericCommand(redisClient
*c
, int where
) {
2076 de
= dictFind(c
->dict
,c
->argv
[1]);
2078 lobj
= createListObject();
2080 if (where
== REDIS_HEAD
) {
2081 if (!listAddNodeHead(list
,c
->argv
[2])) oom("listAddNodeHead");
2083 if (!listAddNodeTail(list
,c
->argv
[2])) oom("listAddNodeTail");
2085 dictAdd(c
->dict
,c
->argv
[1],lobj
);
2086 incrRefCount(c
->argv
[1]);
2087 incrRefCount(c
->argv
[2]);
2089 lobj
= dictGetEntryVal(de
);
2090 if (lobj
->type
!= REDIS_LIST
) {
2091 addReply(c
,shared
.wrongtypeerr
);
2095 if (where
== REDIS_HEAD
) {
2096 if (!listAddNodeHead(list
,c
->argv
[2])) oom("listAddNodeHead");
2098 if (!listAddNodeTail(list
,c
->argv
[2])) oom("listAddNodeTail");
2100 incrRefCount(c
->argv
[2]);
2103 addReply(c
,shared
.ok
);
2106 static void lpushCommand(redisClient
*c
) {
2107 pushGenericCommand(c
,REDIS_HEAD
);
2110 static void rpushCommand(redisClient
*c
) {
2111 pushGenericCommand(c
,REDIS_TAIL
);
2114 static void llenCommand(redisClient
*c
) {
2118 de
= dictFind(c
->dict
,c
->argv
[1]);
2120 addReply(c
,shared
.czero
);
2123 robj
*o
= dictGetEntryVal(de
);
2124 if (o
->type
!= REDIS_LIST
) {
2125 addReply(c
,shared
.wrongtypeerr
);
2128 addReplySds(c
,sdscatprintf(sdsempty(),":%d\r\n",listLength(l
)));
2133 static void lindexCommand(redisClient
*c
) {
2135 int index
= atoi(c
->argv
[2]->ptr
);
2137 de
= dictFind(c
->dict
,c
->argv
[1]);
2139 addReply(c
,shared
.nullbulk
);
2141 robj
*o
= dictGetEntryVal(de
);
2143 if (o
->type
!= REDIS_LIST
) {
2144 addReply(c
,shared
.wrongtypeerr
);
2146 list
*list
= o
->ptr
;
2149 ln
= listIndex(list
, index
);
2151 addReply(c
,shared
.nullbulk
);
2153 robj
*ele
= listNodeValue(ln
);
2154 addReplySds(c
,sdscatprintf(sdsempty(),"$%d\r\n",(int)sdslen(ele
->ptr
)));
2156 addReply(c
,shared
.crlf
);
2162 static void lsetCommand(redisClient
*c
) {
2164 int index
= atoi(c
->argv
[2]->ptr
);
2166 de
= dictFind(c
->dict
,c
->argv
[1]);
2168 addReply(c
,shared
.nokeyerr
);
2170 robj
*o
= dictGetEntryVal(de
);
2172 if (o
->type
!= REDIS_LIST
) {
2173 addReply(c
,shared
.wrongtypeerr
);
2175 list
*list
= o
->ptr
;
2178 ln
= listIndex(list
, index
);
2180 addReply(c
,shared
.outofrangeerr
);
2182 robj
*ele
= listNodeValue(ln
);
2185 listNodeValue(ln
) = c
->argv
[3];
2186 incrRefCount(c
->argv
[3]);
2187 addReply(c
,shared
.ok
);
2194 static void popGenericCommand(redisClient
*c
, int where
) {
2197 de
= dictFind(c
->dict
,c
->argv
[1]);
2199 addReply(c
,shared
.nullbulk
);
2201 robj
*o
= dictGetEntryVal(de
);
2203 if (o
->type
!= REDIS_LIST
) {
2204 addReply(c
,shared
.wrongtypeerr
);
2206 list
*list
= o
->ptr
;
2209 if (where
== REDIS_HEAD
)
2210 ln
= listFirst(list
);
2212 ln
= listLast(list
);
2215 addReply(c
,shared
.nullbulk
);
2217 robj
*ele
= listNodeValue(ln
);
2218 addReplySds(c
,sdscatprintf(sdsempty(),"$%d\r\n",(int)sdslen(ele
->ptr
)));
2220 addReply(c
,shared
.crlf
);
2221 listDelNode(list
,ln
);
2228 static void lpopCommand(redisClient
*c
) {
2229 popGenericCommand(c
,REDIS_HEAD
);
2232 static void rpopCommand(redisClient
*c
) {
2233 popGenericCommand(c
,REDIS_TAIL
);
2236 static void lrangeCommand(redisClient
*c
) {
2238 int start
= atoi(c
->argv
[2]->ptr
);
2239 int end
= atoi(c
->argv
[3]->ptr
);
2241 de
= dictFind(c
->dict
,c
->argv
[1]);
2243 addReply(c
,shared
.nullmultibulk
);
2245 robj
*o
= dictGetEntryVal(de
);
2247 if (o
->type
!= REDIS_LIST
) {
2248 addReply(c
,shared
.wrongtypeerr
);
2250 list
*list
= o
->ptr
;
2252 int llen
= listLength(list
);
2256 /* convert negative indexes */
2257 if (start
< 0) start
= llen
+start
;
2258 if (end
< 0) end
= llen
+end
;
2259 if (start
< 0) start
= 0;
2260 if (end
< 0) end
= 0;
2262 /* indexes sanity checks */
2263 if (start
> end
|| start
>= llen
) {
2264 /* Out of range start or start > end result in empty list */
2265 addReply(c
,shared
.emptymultibulk
);
2268 if (end
>= llen
) end
= llen
-1;
2269 rangelen
= (end
-start
)+1;
2271 /* Return the result in form of a multi-bulk reply */
2272 ln
= listIndex(list
, start
);
2273 addReplySds(c
,sdscatprintf(sdsempty(),"*%d\r\n",rangelen
));
2274 for (j
= 0; j
< rangelen
; j
++) {
2275 ele
= listNodeValue(ln
);
2276 addReplySds(c
,sdscatprintf(sdsempty(),"$%d\r\n",(int)sdslen(ele
->ptr
)));
2278 addReply(c
,shared
.crlf
);
2285 static void ltrimCommand(redisClient
*c
) {
2287 int start
= atoi(c
->argv
[2]->ptr
);
2288 int end
= atoi(c
->argv
[3]->ptr
);
2290 de
= dictFind(c
->dict
,c
->argv
[1]);
2292 addReply(c
,shared
.nokeyerr
);
2294 robj
*o
= dictGetEntryVal(de
);
2296 if (o
->type
!= REDIS_LIST
) {
2297 addReply(c
,shared
.wrongtypeerr
);
2299 list
*list
= o
->ptr
;
2301 int llen
= listLength(list
);
2302 int j
, ltrim
, rtrim
;
2304 /* convert negative indexes */
2305 if (start
< 0) start
= llen
+start
;
2306 if (end
< 0) end
= llen
+end
;
2307 if (start
< 0) start
= 0;
2308 if (end
< 0) end
= 0;
2310 /* indexes sanity checks */
2311 if (start
> end
|| start
>= llen
) {
2312 /* Out of range start or start > end result in empty list */
2316 if (end
>= llen
) end
= llen
-1;
2321 /* Remove list elements to perform the trim */
2322 for (j
= 0; j
< ltrim
; j
++) {
2323 ln
= listFirst(list
);
2324 listDelNode(list
,ln
);
2326 for (j
= 0; j
< rtrim
; j
++) {
2327 ln
= listLast(list
);
2328 listDelNode(list
,ln
);
2330 addReply(c
,shared
.ok
);
2336 static void lremCommand(redisClient
*c
) {
2339 de
= dictFind(c
->dict
,c
->argv
[1]);
2341 addReply(c
,shared
.nokeyerr
);
2343 robj
*o
= dictGetEntryVal(de
);
2345 if (o
->type
!= REDIS_LIST
) {
2346 addReply(c
,shared
.wrongtypeerr
);
2348 list
*list
= o
->ptr
;
2349 listNode
*ln
, *next
;
2350 int toremove
= atoi(c
->argv
[2]->ptr
);
2355 toremove
= -toremove
;
2358 ln
= fromtail
? list
->tail
: list
->head
;
2360 next
= fromtail
? ln
->prev
: ln
->next
;
2361 robj
*ele
= listNodeValue(ln
);
2362 if (sdscmp(ele
->ptr
,c
->argv
[3]->ptr
) == 0) {
2363 listDelNode(list
,ln
);
2366 if (toremove
&& removed
== toremove
) break;
2370 addReplySds(c
,sdscatprintf(sdsempty(),":%d\r\n",removed
));
2375 /* ==================================== Sets ================================ */
2377 static void saddCommand(redisClient
*c
) {
2381 de
= dictFind(c
->dict
,c
->argv
[1]);
2383 set
= createSetObject();
2384 dictAdd(c
->dict
,c
->argv
[1],set
);
2385 incrRefCount(c
->argv
[1]);
2387 set
= dictGetEntryVal(de
);
2388 if (set
->type
!= REDIS_SET
) {
2389 addReply(c
,shared
.wrongtypeerr
);
2393 if (dictAdd(set
->ptr
,c
->argv
[2],NULL
) == DICT_OK
) {
2394 incrRefCount(c
->argv
[2]);
2396 addReply(c
,shared
.cone
);
2398 addReply(c
,shared
.czero
);
2402 static void sremCommand(redisClient
*c
) {
2405 de
= dictFind(c
->dict
,c
->argv
[1]);
2407 addReply(c
,shared
.czero
);
2411 set
= dictGetEntryVal(de
);
2412 if (set
->type
!= REDIS_SET
) {
2413 addReply(c
,shared
.wrongtypeerr
);
2416 if (dictDelete(set
->ptr
,c
->argv
[2]) == DICT_OK
) {
2418 addReply(c
,shared
.cone
);
2420 addReply(c
,shared
.czero
);
2425 static void sismemberCommand(redisClient
*c
) {
2428 de
= dictFind(c
->dict
,c
->argv
[1]);
2430 addReply(c
,shared
.czero
);
2434 set
= dictGetEntryVal(de
);
2435 if (set
->type
!= REDIS_SET
) {
2436 addReply(c
,shared
.wrongtypeerr
);
2439 if (dictFind(set
->ptr
,c
->argv
[2]))
2440 addReply(c
,shared
.cone
);
2442 addReply(c
,shared
.czero
);
2446 static void scardCommand(redisClient
*c
) {
2450 de
= dictFind(c
->dict
,c
->argv
[1]);
2452 addReply(c
,shared
.czero
);
2455 robj
*o
= dictGetEntryVal(de
);
2456 if (o
->type
!= REDIS_SET
) {
2457 addReply(c
,shared
.wrongtypeerr
);
2460 addReplySds(c
,sdscatprintf(sdsempty(),":%d\r\n",
2461 dictGetHashTableUsed(s
)));
2466 static int qsortCompareSetsByCardinality(const void *s1
, const void *s2
) {
2467 dict
**d1
= (void*) s1
, **d2
= (void*) s2
;
2469 return dictGetHashTableUsed(*d1
)-dictGetHashTableUsed(*d2
);
2472 static void sinterGenericCommand(redisClient
*c
, robj
**setskeys
, int setsnum
, robj
*dstkey
) {
2473 dict
**dv
= zmalloc(sizeof(dict
*)*setsnum
);
2476 robj
*lenobj
= NULL
, *dstset
= NULL
;
2477 int j
, cardinality
= 0;
2479 if (!dv
) oom("sinterCommand");
2480 for (j
= 0; j
< setsnum
; j
++) {
2484 de
= dictFind(c
->dict
,setskeys
[j
]);
2487 addReply(c
,shared
.nokeyerr
);
2490 setobj
= dictGetEntryVal(de
);
2491 if (setobj
->type
!= REDIS_SET
) {
2493 addReply(c
,shared
.wrongtypeerr
);
2496 dv
[j
] = setobj
->ptr
;
2498 /* Sort sets from the smallest to largest, this will improve our
2499 * algorithm's performace */
2500 qsort(dv
,setsnum
,sizeof(dict
*),qsortCompareSetsByCardinality
);
2502 /* The first thing we should output is the total number of elements...
2503 * since this is a multi-bulk write, but at this stage we don't know
2504 * the intersection set size, so we use a trick, append an empty object
2505 * to the output list and save the pointer to later modify it with the
2508 lenobj
= createObject(REDIS_STRING
,NULL
);
2510 decrRefCount(lenobj
);
2512 /* If we have a target key where to store the resulting set
2513 * create this key with an empty set inside */
2514 dstset
= createSetObject();
2515 dictDelete(c
->dict
,dstkey
);
2516 dictAdd(c
->dict
,dstkey
,dstset
);
2517 incrRefCount(dstkey
);
2520 /* Iterate all the elements of the first (smallest) set, and test
2521 * the element against all the other sets, if at least one set does
2522 * not include the element it is discarded */
2523 di
= dictGetIterator(dv
[0]);
2524 if (!di
) oom("dictGetIterator");
2526 while((de
= dictNext(di
)) != NULL
) {
2529 for (j
= 1; j
< setsnum
; j
++)
2530 if (dictFind(dv
[j
],dictGetEntryKey(de
)) == NULL
) break;
2532 continue; /* at least one set does not contain the member */
2533 ele
= dictGetEntryKey(de
);
2535 addReplySds(c
,sdscatprintf(sdsempty(),"$%d\r\n",sdslen(ele
->ptr
)));
2537 addReply(c
,shared
.crlf
);
2540 dictAdd(dstset
->ptr
,ele
,NULL
);
2544 dictReleaseIterator(di
);
2547 lenobj
->ptr
= sdscatprintf(sdsempty(),"*%d\r\n",cardinality
);
2549 addReply(c
,shared
.ok
);
2553 static void sinterCommand(redisClient
*c
) {
2554 sinterGenericCommand(c
,c
->argv
+1,c
->argc
-1,NULL
);
2557 static void sinterstoreCommand(redisClient
*c
) {
2558 sinterGenericCommand(c
,c
->argv
+2,c
->argc
-2,c
->argv
[1]);
2561 static void flushdbCommand(redisClient
*c
) {
2563 addReply(c
,shared
.ok
);
2564 rdbSave(server
.dbfilename
);
2567 static void flushallCommand(redisClient
*c
) {
2569 addReply(c
,shared
.ok
);
2570 rdbSave(server
.dbfilename
);
2573 redisSortOperation
*createSortOperation(int type
, robj
*pattern
) {
2574 redisSortOperation
*so
= zmalloc(sizeof(*so
));
2575 if (!so
) oom("createSortOperation");
2577 so
->pattern
= pattern
;
2581 /* Return the value associated to the key with a name obtained
2582 * substituting the first occurence of '*' in 'pattern' with 'subst' */
2583 robj
*lookupKeyByPattern(dict
*dict
, robj
*pattern
, robj
*subst
) {
2587 int prefixlen
, sublen
, postfixlen
;
2589 /* Expoit the internal sds representation to create a sds string allocated on the stack in order to make this function faster */
2593 char buf
[REDIS_SORTKEY_MAX
+1];
2597 spat
= pattern
->ptr
;
2599 if (sdslen(spat
)+sdslen(ssub
)-1 > REDIS_SORTKEY_MAX
) return NULL
;
2600 p
= strchr(spat
,'*');
2601 if (!p
) return NULL
;
2604 sublen
= sdslen(ssub
);
2605 postfixlen
= sdslen(spat
)-(prefixlen
+1);
2606 memcpy(keyname
.buf
,spat
,prefixlen
);
2607 memcpy(keyname
.buf
+prefixlen
,ssub
,sublen
);
2608 memcpy(keyname
.buf
+prefixlen
+sublen
,p
+1,postfixlen
);
2609 keyname
.buf
[prefixlen
+sublen
+postfixlen
] = '\0';
2610 keyname
.len
= prefixlen
+sublen
+postfixlen
;
2612 keyobj
.refcount
= 1;
2613 keyobj
.type
= REDIS_STRING
;
2614 keyobj
.ptr
= ((char*)&keyname
)+(sizeof(long)*2);
2616 de
= dictFind(dict
,&keyobj
);
2617 // printf("lookup '%s' => %p\n", keyname.buf,de);
2618 if (!de
) return NULL
;
2619 return dictGetEntryVal(de
);
2622 /* sortCompare() is used by qsort in sortCommand(). Given that qsort_r with
2623 * the additional parameter is not standard but a BSD-specific we have to
2624 * pass sorting parameters via the global 'server' structure */
2625 static int sortCompare(const void *s1
, const void *s2
) {
2626 const redisSortObject
*so1
= s1
, *so2
= s2
;
2629 if (!server
.sort_alpha
) {
2630 /* Numeric sorting. Here it's trivial as we precomputed scores */
2631 if (so1
->u
.score
> so2
->u
.score
) {
2633 } else if (so1
->u
.score
< so2
->u
.score
) {
2639 /* Alphanumeric sorting */
2640 if (server
.sort_bypattern
) {
2641 if (!so1
->u
.cmpobj
|| !so2
->u
.cmpobj
) {
2642 /* At least one compare object is NULL */
2643 if (so1
->u
.cmpobj
== so2
->u
.cmpobj
)
2645 else if (so1
->u
.cmpobj
== NULL
)
2650 /* We have both the objects, use strcoll */
2651 cmp
= strcoll(so1
->u
.cmpobj
->ptr
,so2
->u
.cmpobj
->ptr
);
2654 /* Compare elements directly */
2655 cmp
= strcoll(so1
->obj
->ptr
,so2
->obj
->ptr
);
2658 return server
.sort_desc
? -cmp
: cmp
;
2661 /* The SORT command is the most complex command in Redis. Warning: this code
2662 * is optimized for speed and a bit less for readability */
2663 static void sortCommand(redisClient
*c
) {
2667 int desc
= 0, alpha
= 0;
2668 int limit_start
= 0, limit_count
= -1, start
, end
;
2669 int j
, dontsort
= 0, vectorlen
;
2670 int getop
= 0; /* GET operation counter */
2671 robj
*sortval
, *sortby
= NULL
;
2672 redisSortObject
*vector
; /* Resulting vector to sort */
2674 /* Lookup the key to sort. It must be of the right types */
2675 de
= dictFind(c
->dict
,c
->argv
[1]);
2677 addReply(c
,shared
.nokeyerr
);
2680 sortval
= dictGetEntryVal(de
);
2681 if (sortval
->type
!= REDIS_SET
&& sortval
->type
!= REDIS_LIST
) {
2682 addReply(c
,shared
.wrongtypeerr
);
2686 /* Create a list of operations to perform for every sorted element.
2687 * Operations can be GET/DEL/INCR/DECR */
2688 operations
= listCreate();
2689 listSetFreeMethod(operations
,zfree
);
2692 /* Now we need to protect sortval incrementing its count, in the future
2693 * SORT may have options able to overwrite/delete keys during the sorting
2694 * and the sorted key itself may get destroied */
2695 incrRefCount(sortval
);
2697 /* The SORT command has an SQL-alike syntax, parse it */
2698 while(j
< c
->argc
) {
2699 int leftargs
= c
->argc
-j
-1;
2700 if (!strcasecmp(c
->argv
[j
]->ptr
,"asc")) {
2702 } else if (!strcasecmp(c
->argv
[j
]->ptr
,"desc")) {
2704 } else if (!strcasecmp(c
->argv
[j
]->ptr
,"alpha")) {
2706 } else if (!strcasecmp(c
->argv
[j
]->ptr
,"limit") && leftargs
>= 2) {
2707 limit_start
= atoi(c
->argv
[j
+1]->ptr
);
2708 limit_count
= atoi(c
->argv
[j
+2]->ptr
);
2710 } else if (!strcasecmp(c
->argv
[j
]->ptr
,"by") && leftargs
>= 1) {
2711 sortby
= c
->argv
[j
+1];
2712 /* If the BY pattern does not contain '*', i.e. it is constant,
2713 * we don't need to sort nor to lookup the weight keys. */
2714 if (strchr(c
->argv
[j
+1]->ptr
,'*') == NULL
) dontsort
= 1;
2716 } else if (!strcasecmp(c
->argv
[j
]->ptr
,"get") && leftargs
>= 1) {
2717 listAddNodeTail(operations
,createSortOperation(
2718 REDIS_SORT_GET
,c
->argv
[j
+1]));
2721 } else if (!strcasecmp(c
->argv
[j
]->ptr
,"del") && leftargs
>= 1) {
2722 listAddNodeTail(operations
,createSortOperation(
2723 REDIS_SORT_DEL
,c
->argv
[j
+1]));
2725 } else if (!strcasecmp(c
->argv
[j
]->ptr
,"incr") && leftargs
>= 1) {
2726 listAddNodeTail(operations
,createSortOperation(
2727 REDIS_SORT_INCR
,c
->argv
[j
+1]));
2729 } else if (!strcasecmp(c
->argv
[j
]->ptr
,"get") && leftargs
>= 1) {
2730 listAddNodeTail(operations
,createSortOperation(
2731 REDIS_SORT_DECR
,c
->argv
[j
+1]));
2734 decrRefCount(sortval
);
2735 listRelease(operations
);
2736 addReply(c
,shared
.syntaxerr
);
2742 /* Load the sorting vector with all the objects to sort */
2743 vectorlen
= (sortval
->type
== REDIS_LIST
) ?
2744 listLength((list
*)sortval
->ptr
) :
2745 dictGetHashTableUsed((dict
*)sortval
->ptr
);
2746 vector
= zmalloc(sizeof(redisSortObject
)*vectorlen
);
2747 if (!vector
) oom("allocating objects vector for SORT");
2749 if (sortval
->type
== REDIS_LIST
) {
2750 list
*list
= sortval
->ptr
;
2751 listNode
*ln
= list
->head
;
2753 robj
*ele
= ln
->value
;
2754 vector
[j
].obj
= ele
;
2755 vector
[j
].u
.score
= 0;
2756 vector
[j
].u
.cmpobj
= NULL
;
2761 dict
*set
= sortval
->ptr
;
2765 di
= dictGetIterator(set
);
2766 if (!di
) oom("dictGetIterator");
2767 while((setele
= dictNext(di
)) != NULL
) {
2768 vector
[j
].obj
= dictGetEntryKey(setele
);
2769 vector
[j
].u
.score
= 0;
2770 vector
[j
].u
.cmpobj
= NULL
;
2773 dictReleaseIterator(di
);
2775 assert(j
== vectorlen
);
2777 /* Now it's time to load the right scores in the sorting vector */
2778 if (dontsort
== 0) {
2779 for (j
= 0; j
< vectorlen
; j
++) {
2783 byval
= lookupKeyByPattern(c
->dict
,sortby
,vector
[j
].obj
);
2784 if (!byval
|| byval
->type
!= REDIS_STRING
) continue;
2786 vector
[j
].u
.cmpobj
= byval
;
2787 incrRefCount(byval
);
2789 vector
[j
].u
.score
= strtod(byval
->ptr
,NULL
);
2792 if (!alpha
) vector
[j
].u
.score
= strtod(vector
[j
].obj
->ptr
,NULL
);
2797 /* We are ready to sort the vector... perform a bit of sanity check
2798 * on the LIMIT option too. We'll use a partial version of quicksort. */
2799 start
= (limit_start
< 0) ? 0 : limit_start
;
2800 end
= (limit_count
< 0) ? vectorlen
-1 : start
+limit_count
-1;
2801 if (start
>= vectorlen
) {
2802 start
= vectorlen
-1;
2805 if (end
>= vectorlen
) end
= vectorlen
-1;
2807 if (dontsort
== 0) {
2808 server
.sort_desc
= desc
;
2809 server
.sort_alpha
= alpha
;
2810 server
.sort_bypattern
= sortby
? 1 : 0;
2811 qsort(vector
,vectorlen
,sizeof(redisSortObject
),sortCompare
);
2814 /* Send command output to the output buffer, performing the specified
2815 * GET/DEL/INCR/DECR operations if any. */
2816 outputlen
= getop
? getop
*(end
-start
+1) : end
-start
+1;
2817 addReplySds(c
,sdscatprintf(sdsempty(),"*%d\r\n",outputlen
));
2818 for (j
= start
; j
<= end
; j
++) {
2819 listNode
*ln
= operations
->head
;
2821 addReplySds(c
,sdscatprintf(sdsempty(),"$%d\r\n",
2822 sdslen(vector
[j
].obj
->ptr
)));
2823 addReply(c
,vector
[j
].obj
);
2824 addReply(c
,shared
.crlf
);
2827 redisSortOperation
*sop
= ln
->value
;
2828 robj
*val
= lookupKeyByPattern(c
->dict
,sop
->pattern
,
2831 if (sop
->type
== REDIS_SORT_GET
) {
2832 if (!val
|| val
->type
!= REDIS_STRING
) {
2833 addReply(c
,shared
.nullbulk
);
2835 addReplySds(c
,sdscatprintf(sdsempty(),"$%d\r\n",
2838 addReply(c
,shared
.crlf
);
2840 } else if (sop
->type
== REDIS_SORT_DEL
) {
2848 decrRefCount(sortval
);
2849 listRelease(operations
);
2850 for (j
= 0; j
< vectorlen
; j
++) {
2851 if (sortby
&& alpha
&& vector
[j
].u
.cmpobj
)
2852 decrRefCount(vector
[j
].u
.cmpobj
);
2857 static void infoCommand(redisClient
*c
) {
2859 time_t uptime
= time(NULL
)-server
.stat_starttime
;
2861 info
= sdscatprintf(sdsempty(),
2862 "redis_version:%s\r\n"
2863 "connected_clients:%d\r\n"
2864 "connected_slaves:%d\r\n"
2865 "used_memory:%d\r\n"
2866 "changes_since_last_save:%lld\r\n"
2867 "last_save_time:%d\r\n"
2868 "total_connections_received:%lld\r\n"
2869 "total_commands_processed:%lld\r\n"
2870 "uptime_in_seconds:%d\r\n"
2871 "uptime_in_days:%d\r\n"
2873 listLength(server
.clients
)-listLength(server
.slaves
),
2874 listLength(server
.slaves
),
2878 server
.stat_numconnections
,
2879 server
.stat_numcommands
,
2883 addReplySds(c
,sdscatprintf(sdsempty(),"$%d\r\n",sdslen(info
)));
2884 addReplySds(c
,info
);
2885 addReply(c
,shared
.crlf
);
2888 /* =============================== Replication ============================= */
2890 /* Send the whole output buffer syncronously to the slave. This a general operation in theory, but it is actually useful only for replication. */
2891 static int flushClientOutput(redisClient
*c
) {
2893 time_t start
= time(NULL
);
2895 while(listLength(c
->reply
)) {
2896 if (time(NULL
)-start
> 5) return REDIS_ERR
; /* 5 seconds timeout */
2897 retval
= aeWait(c
->fd
,AE_WRITABLE
,1000);
2900 } else if (retval
& AE_WRITABLE
) {
2901 sendReplyToClient(NULL
, c
->fd
, c
, AE_WRITABLE
);
2907 static int syncWrite(int fd
, void *ptr
, ssize_t size
, int timeout
) {
2908 ssize_t nwritten
, ret
= size
;
2909 time_t start
= time(NULL
);
2913 if (aeWait(fd
,AE_WRITABLE
,1000) & AE_WRITABLE
) {
2914 nwritten
= write(fd
,ptr
,size
);
2915 if (nwritten
== -1) return -1;
2919 if ((time(NULL
)-start
) > timeout
) {
2927 static int syncRead(int fd
, void *ptr
, ssize_t size
, int timeout
) {
2928 ssize_t nread
, totread
= 0;
2929 time_t start
= time(NULL
);
2933 if (aeWait(fd
,AE_READABLE
,1000) & AE_READABLE
) {
2934 nread
= read(fd
,ptr
,size
);
2935 if (nread
== -1) return -1;
2940 if ((time(NULL
)-start
) > timeout
) {
2948 static int syncReadLine(int fd
, char *ptr
, ssize_t size
, int timeout
) {
2955 if (syncRead(fd
,&c
,1,timeout
) == -1) return -1;
2958 if (nread
&& *(ptr
-1) == '\r') *(ptr
-1) = '\0';
2969 static void syncCommand(redisClient
*c
) {
2972 time_t start
= time(NULL
);
2975 /* ignore SYNC if aleady slave or in monitor mode */
2976 if (c
->flags
& REDIS_SLAVE
) return;
2978 redisLog(REDIS_NOTICE
,"Slave ask for syncronization");
2979 if (flushClientOutput(c
) == REDIS_ERR
||
2980 rdbSave(server
.dbfilename
) != REDIS_OK
)
2983 fd
= open(server
.dbfilename
, O_RDONLY
);
2984 if (fd
== -1 || fstat(fd
,&sb
) == -1) goto closeconn
;
2987 snprintf(sizebuf
,32,"$%d\r\n",len
);
2988 if (syncWrite(c
->fd
,sizebuf
,strlen(sizebuf
),5) == -1) goto closeconn
;
2993 if (time(NULL
)-start
> REDIS_MAX_SYNC_TIME
) goto closeconn
;
2994 nread
= read(fd
,buf
,1024);
2995 if (nread
== -1) goto closeconn
;
2997 if (syncWrite(c
->fd
,buf
,nread
,5) == -1) goto closeconn
;
2999 if (syncWrite(c
->fd
,"\r\n",2,5) == -1) goto closeconn
;
3001 c
->flags
|= REDIS_SLAVE
;
3003 if (!listAddNodeTail(server
.slaves
,c
)) oom("listAddNodeTail");
3004 redisLog(REDIS_NOTICE
,"Syncronization with slave succeeded");
3008 if (fd
!= -1) close(fd
);
3009 c
->flags
|= REDIS_CLOSE
;
3010 redisLog(REDIS_WARNING
,"Syncronization with slave failed");
3014 static int syncWithMaster(void) {
3015 char buf
[1024], tmpfile
[256];
3017 int fd
= anetTcpConnect(NULL
,server
.masterhost
,server
.masterport
);
3021 redisLog(REDIS_WARNING
,"Unable to connect to MASTER: %s",
3025 /* Issue the SYNC command */
3026 if (syncWrite(fd
,"SYNC \r\n",7,5) == -1) {
3028 redisLog(REDIS_WARNING
,"I/O error writing to MASTER: %s",
3032 /* Read the bulk write count */
3033 if (syncReadLine(fd
,buf
,1024,5) == -1) {
3035 redisLog(REDIS_WARNING
,"I/O error reading bulk count from MASTER: %s",
3039 dumpsize
= atoi(buf
+1);
3040 redisLog(REDIS_NOTICE
,"Receiving %d bytes data dump from MASTER",dumpsize
);
3041 /* Read the bulk write data on a temp file */
3042 snprintf(tmpfile
,256,"temp-%d.%ld.rdb",(int)time(NULL
),(long int)random());
3043 dfd
= open(tmpfile
,O_CREAT
|O_WRONLY
,0644);
3046 redisLog(REDIS_WARNING
,"Opening the temp file needed for MASTER <-> SLAVE synchronization: %s",strerror(errno
));
3050 int nread
, nwritten
;
3052 nread
= read(fd
,buf
,(dumpsize
< 1024)?dumpsize
:1024);
3054 redisLog(REDIS_WARNING
,"I/O error trying to sync with MASTER: %s",
3060 nwritten
= write(dfd
,buf
,nread
);
3061 if (nwritten
== -1) {
3062 redisLog(REDIS_WARNING
,"Write error writing to the DB dump file needed for MASTER <-> SLAVE synchrnonization: %s", strerror(errno
));
3070 if (rename(tmpfile
,server
.dbfilename
) == -1) {
3071 redisLog(REDIS_WARNING
,"Failed trying to rename the temp DB into dump.rdb in MASTER <-> SLAVE synchronization: %s", strerror(errno
));
3077 if (rdbLoad(server
.dbfilename
) != REDIS_OK
) {
3078 redisLog(REDIS_WARNING
,"Failed trying to load the MASTER synchronization DB from disk");
3082 server
.master
= createClient(fd
);
3083 server
.master
->flags
|= REDIS_MASTER
;
3084 server
.replstate
= REDIS_REPL_CONNECTED
;
3088 static void monitorCommand(redisClient
*c
) {
3089 /* ignore MONITOR if aleady slave or in monitor mode */
3090 if (c
->flags
& REDIS_SLAVE
) return;
3092 c
->flags
|= (REDIS_SLAVE
|REDIS_MONITOR
);
3094 if (!listAddNodeTail(server
.monitors
,c
)) oom("listAddNodeTail");
3095 addReply(c
,shared
.ok
);
3098 /* =================================== Main! ================================ */
3100 static void daemonize(void) {
3104 if (fork() != 0) exit(0); /* parent exits */
3105 setsid(); /* create a new session */
3107 /* Every output goes to /dev/null. If Redis is daemonized but
3108 * the 'logfile' is set to 'stdout' in the configuration file
3109 * it will not log at all. */
3110 if ((fd
= open("/dev/null", O_RDWR
, 0)) != -1) {
3111 dup2(fd
, STDIN_FILENO
);
3112 dup2(fd
, STDOUT_FILENO
);
3113 dup2(fd
, STDERR_FILENO
);
3114 if (fd
> STDERR_FILENO
) close(fd
);
3116 /* Try to write the pid file */
3117 fp
= fopen(server
.pidfile
,"w");
3119 fprintf(fp
,"%d\n",getpid());
3124 int main(int argc
, char **argv
) {
3127 ResetServerSaveParams();
3128 loadServerConfig(argv
[1]);
3129 } else if (argc
> 2) {
3130 fprintf(stderr
,"Usage: ./redis-server [/path/to/redis.conf]\n");
3134 if (server
.daemonize
) daemonize();
3135 redisLog(REDIS_NOTICE
,"Server started, Redis version " REDIS_VERSION
);
3136 if (rdbLoad(server
.dbfilename
) == REDIS_OK
)
3137 redisLog(REDIS_NOTICE
,"DB loaded from disk");
3138 if (aeCreateFileEvent(server
.el
, server
.fd
, AE_READABLE
,
3139 acceptHandler
, NULL
, NULL
) == AE_ERR
) oom("creating file event");
3140 redisLog(REDIS_NOTICE
,"The server is now ready to accept connections on port %d", server
.port
);
3142 aeDeleteEventLoop(server
.el
);