]> git.saurik.com Git - redis.git/blob - redis.c
358463cdb25f999af5a910803a6f3a4513e2714d
[redis.git] / redis.c
1 /*
2 * Copyright (c) 2006-2009, 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 #define REDIS_VERSION "0.100"
31
32 #include "fmacros.h"
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <time.h>
38 #include <unistd.h>
39 #include <signal.h>
40 #include <sys/wait.h>
41 #include <errno.h>
42 #include <assert.h>
43 #include <ctype.h>
44 #include <stdarg.h>
45 #include <inttypes.h>
46 #include <arpa/inet.h>
47 #include <sys/stat.h>
48 #include <fcntl.h>
49 #include <sys/time.h>
50 #include <sys/resource.h>
51 #include <limits.h>
52
53 #include "ae.h" /* Event driven programming library */
54 #include "sds.h" /* Dynamic safe strings */
55 #include "anet.h" /* Networking the easy way */
56 #include "dict.h" /* Hash tables */
57 #include "adlist.h" /* Linked lists */
58 #include "zmalloc.h" /* total memory usage aware version of malloc/free */
59 #include "lzf.h" /* LZF compression library */
60 #include "pqsort.h" /* Partial qsort for SORT+LIMIT */
61
62 /* Error codes */
63 #define REDIS_OK 0
64 #define REDIS_ERR -1
65
66 /* Static server configuration */
67 #define REDIS_SERVERPORT 6379 /* TCP port */
68 #define REDIS_MAXIDLETIME (60*5) /* default client timeout */
69 #define REDIS_IOBUF_LEN 1024
70 #define REDIS_LOADBUF_LEN 1024
71 #define REDIS_STATIC_ARGS 4
72 #define REDIS_DEFAULT_DBNUM 16
73 #define REDIS_CONFIGLINE_MAX 1024
74 #define REDIS_OBJFREELIST_MAX 1000000 /* Max number of objects to cache */
75 #define REDIS_MAX_SYNC_TIME 60 /* Slave can't take more to sync */
76 #define REDIS_EXPIRELOOKUPS_PER_CRON 100 /* try to expire 100 keys/second */
77
78 /* Hash table parameters */
79 #define REDIS_HT_MINFILL 10 /* Minimal hash table fill 10% */
80 #define REDIS_HT_MINSLOTS 16384 /* Never resize the HT under this */
81
82 /* Command flags */
83 #define REDIS_CMD_BULK 1
84 #define REDIS_CMD_INLINE 2
85
86 /* Object types */
87 #define REDIS_STRING 0
88 #define REDIS_LIST 1
89 #define REDIS_SET 2
90 #define REDIS_HASH 3
91
92 /* Object types only used for dumping to disk */
93 #define REDIS_EXPIRETIME 253
94 #define REDIS_SELECTDB 254
95 #define REDIS_EOF 255
96
97 /* Defines related to the dump file format. To store 32 bits lengths for short
98 * keys requires a lot of space, so we check the most significant 2 bits of
99 * the first byte to interpreter the length:
100 *
101 * 00|000000 => if the two MSB are 00 the len is the 6 bits of this byte
102 * 01|000000 00000000 => 01, the len is 14 byes, 6 bits + 8 bits of next byte
103 * 10|000000 [32 bit integer] => if it's 01, a full 32 bit len will follow
104 * 11|000000 this means: specially encoded object will follow. The six bits
105 * number specify the kind of object that follows.
106 * See the REDIS_RDB_ENC_* defines.
107 *
108 * Lenghts up to 63 are stored using a single byte, most DB keys, and may
109 * values, will fit inside. */
110 #define REDIS_RDB_6BITLEN 0
111 #define REDIS_RDB_14BITLEN 1
112 #define REDIS_RDB_32BITLEN 2
113 #define REDIS_RDB_ENCVAL 3
114 #define REDIS_RDB_LENERR UINT_MAX
115
116 /* When a length of a string object stored on disk has the first two bits
117 * set, the remaining two bits specify a special encoding for the object
118 * accordingly to the following defines: */
119 #define REDIS_RDB_ENC_INT8 0 /* 8 bit signed integer */
120 #define REDIS_RDB_ENC_INT16 1 /* 16 bit signed integer */
121 #define REDIS_RDB_ENC_INT32 2 /* 32 bit signed integer */
122 #define REDIS_RDB_ENC_LZF 3 /* string compressed with FASTLZ */
123
124 /* Client flags */
125 #define REDIS_CLOSE 1 /* This client connection should be closed ASAP */
126 #define REDIS_SLAVE 2 /* This client is a slave server */
127 #define REDIS_MASTER 4 /* This client is a master server */
128 #define REDIS_MONITOR 8 /* This client is a slave monitor, see MONITOR */
129
130 /* Slave replication state - slave side */
131 #define REDIS_REPL_NONE 0 /* No active replication */
132 #define REDIS_REPL_CONNECT 1 /* Must connect to master */
133 #define REDIS_REPL_CONNECTED 2 /* Connected to master */
134
135 /* Slave replication state - from the point of view of master
136 * Note that in SEND_BULK and ONLINE state the slave receives new updates
137 * in its output queue. In the WAIT_BGSAVE state instead the server is waiting
138 * to start the next background saving in order to send updates to it. */
139 #define REDIS_REPL_WAIT_BGSAVE_START 3 /* master waits bgsave to start feeding it */
140 #define REDIS_REPL_WAIT_BGSAVE_END 4 /* master waits bgsave to start bulk DB transmission */
141 #define REDIS_REPL_SEND_BULK 5 /* master is sending the bulk DB */
142 #define REDIS_REPL_ONLINE 6 /* bulk DB already transmitted, receive updates */
143
144 /* List related stuff */
145 #define REDIS_HEAD 0
146 #define REDIS_TAIL 1
147
148 /* Sort operations */
149 #define REDIS_SORT_GET 0
150 #define REDIS_SORT_DEL 1
151 #define REDIS_SORT_INCR 2
152 #define REDIS_SORT_DECR 3
153 #define REDIS_SORT_ASC 4
154 #define REDIS_SORT_DESC 5
155 #define REDIS_SORTKEY_MAX 1024
156
157 /* Log levels */
158 #define REDIS_DEBUG 0
159 #define REDIS_NOTICE 1
160 #define REDIS_WARNING 2
161
162 /* Anti-warning macro... */
163 #define REDIS_NOTUSED(V) ((void) V)
164
165 /*================================= Data types ============================== */
166
167 /* A redis object, that is a type able to hold a string / list / set */
168 typedef struct redisObject {
169 void *ptr;
170 int type;
171 int refcount;
172 } robj;
173
174 typedef struct redisDb {
175 dict *dict;
176 dict *expires;
177 int id;
178 } redisDb;
179
180 /* With multiplexing we need to take per-clinet state.
181 * Clients are taken in a liked list. */
182 typedef struct redisClient {
183 int fd;
184 redisDb *db;
185 int dictid;
186 sds querybuf;
187 robj **argv;
188 int argc;
189 int bulklen; /* bulk read len. -1 if not in bulk read mode */
190 list *reply;
191 int sentlen;
192 time_t lastinteraction; /* time of the last interaction, used for timeout */
193 int flags; /* REDIS_CLOSE | REDIS_SLAVE | REDIS_MONITOR */
194 int slaveseldb; /* slave selected db, if this client is a slave */
195 int authenticated; /* when requirepass is non-NULL */
196 int replstate; /* replication state if this is a slave */
197 int repldbfd; /* replication DB file descriptor */
198 long repldboff; /* replication DB file offset */
199 off_t repldbsize; /* replication DB file size */
200 } redisClient;
201
202 struct saveparam {
203 time_t seconds;
204 int changes;
205 };
206
207 /* Global server state structure */
208 struct redisServer {
209 int port;
210 int fd;
211 redisDb *db;
212 dict *sharingpool;
213 unsigned int sharingpoolsize;
214 long long dirty; /* changes to DB from the last save */
215 list *clients;
216 list *slaves, *monitors;
217 char neterr[ANET_ERR_LEN];
218 aeEventLoop *el;
219 int cronloops; /* number of times the cron function run */
220 list *objfreelist; /* A list of freed objects to avoid malloc() */
221 time_t lastsave; /* Unix time of last save succeeede */
222 size_t usedmemory; /* Used memory in megabytes */
223 /* Fields used only for stats */
224 time_t stat_starttime; /* server start time */
225 long long stat_numcommands; /* number of processed commands */
226 long long stat_numconnections; /* number of connections received */
227 /* Configuration */
228 int verbosity;
229 int glueoutputbuf;
230 int maxidletime;
231 int dbnum;
232 int daemonize;
233 char *pidfile;
234 int bgsaveinprogress;
235 struct saveparam *saveparams;
236 int saveparamslen;
237 char *logfile;
238 char *bindaddr;
239 char *dbfilename;
240 char *requirepass;
241 int shareobjects;
242 /* Replication related */
243 int isslave;
244 char *masterhost;
245 int masterport;
246 redisClient *master; /* client that is master for this slave */
247 int replstate;
248 /* Sort parameters - qsort_r() is only available under BSD so we
249 * have to take this state global, in order to pass it to sortCompare() */
250 int sort_desc;
251 int sort_alpha;
252 int sort_bypattern;
253 };
254
255 typedef void redisCommandProc(redisClient *c);
256 struct redisCommand {
257 char *name;
258 redisCommandProc *proc;
259 int arity;
260 int flags;
261 };
262
263 typedef struct _redisSortObject {
264 robj *obj;
265 union {
266 double score;
267 robj *cmpobj;
268 } u;
269 } redisSortObject;
270
271 typedef struct _redisSortOperation {
272 int type;
273 robj *pattern;
274 } redisSortOperation;
275
276 struct sharedObjectsStruct {
277 robj *crlf, *ok, *err, *emptybulk, *czero, *cone, *pong, *space,
278 *colon, *nullbulk, *nullmultibulk,
279 *emptymultibulk, *wrongtypeerr, *nokeyerr, *syntaxerr, *sameobjecterr,
280 *outofrangeerr, *plus,
281 *select0, *select1, *select2, *select3, *select4,
282 *select5, *select6, *select7, *select8, *select9;
283 } shared;
284
285 /*================================ Prototypes =============================== */
286
287 static void freeStringObject(robj *o);
288 static void freeListObject(robj *o);
289 static void freeSetObject(robj *o);
290 static void decrRefCount(void *o);
291 static robj *createObject(int type, void *ptr);
292 static void freeClient(redisClient *c);
293 static int rdbLoad(char *filename);
294 static void addReply(redisClient *c, robj *obj);
295 static void addReplySds(redisClient *c, sds s);
296 static void incrRefCount(robj *o);
297 static int rdbSaveBackground(char *filename);
298 static robj *createStringObject(char *ptr, size_t len);
299 static void replicationFeedSlaves(list *slaves, struct redisCommand *cmd, int dictid, robj **argv, int argc);
300 static int syncWithMaster(void);
301 static robj *tryObjectSharing(robj *o);
302 static int removeExpire(redisDb *db, robj *key);
303 static int expireIfNeeded(redisDb *db, robj *key);
304 static int deleteIfVolatile(redisDb *db, robj *key);
305 static int deleteKey(redisDb *db, robj *key);
306 static time_t getExpire(redisDb *db, robj *key);
307 static int setExpire(redisDb *db, robj *key, time_t when);
308 static void updateSalvesWaitingBgsave(int bgsaveerr);
309
310 static void authCommand(redisClient *c);
311 static void pingCommand(redisClient *c);
312 static void echoCommand(redisClient *c);
313 static void setCommand(redisClient *c);
314 static void setnxCommand(redisClient *c);
315 static void getCommand(redisClient *c);
316 static void delCommand(redisClient *c);
317 static void existsCommand(redisClient *c);
318 static void incrCommand(redisClient *c);
319 static void decrCommand(redisClient *c);
320 static void incrbyCommand(redisClient *c);
321 static void decrbyCommand(redisClient *c);
322 static void selectCommand(redisClient *c);
323 static void randomkeyCommand(redisClient *c);
324 static void keysCommand(redisClient *c);
325 static void dbsizeCommand(redisClient *c);
326 static void lastsaveCommand(redisClient *c);
327 static void saveCommand(redisClient *c);
328 static void bgsaveCommand(redisClient *c);
329 static void shutdownCommand(redisClient *c);
330 static void moveCommand(redisClient *c);
331 static void renameCommand(redisClient *c);
332 static void renamenxCommand(redisClient *c);
333 static void lpushCommand(redisClient *c);
334 static void rpushCommand(redisClient *c);
335 static void lpopCommand(redisClient *c);
336 static void rpopCommand(redisClient *c);
337 static void llenCommand(redisClient *c);
338 static void lindexCommand(redisClient *c);
339 static void lrangeCommand(redisClient *c);
340 static void ltrimCommand(redisClient *c);
341 static void typeCommand(redisClient *c);
342 static void lsetCommand(redisClient *c);
343 static void saddCommand(redisClient *c);
344 static void sremCommand(redisClient *c);
345 static void smoveCommand(redisClient *c);
346 static void sismemberCommand(redisClient *c);
347 static void scardCommand(redisClient *c);
348 static void sinterCommand(redisClient *c);
349 static void sinterstoreCommand(redisClient *c);
350 static void sunionCommand(redisClient *c);
351 static void sunionstoreCommand(redisClient *c);
352 static void sdiffCommand(redisClient *c);
353 static void sdiffstoreCommand(redisClient *c);
354 static void syncCommand(redisClient *c);
355 static void flushdbCommand(redisClient *c);
356 static void flushallCommand(redisClient *c);
357 static void sortCommand(redisClient *c);
358 static void lremCommand(redisClient *c);
359 static void infoCommand(redisClient *c);
360 static void mgetCommand(redisClient *c);
361 static void monitorCommand(redisClient *c);
362 static void expireCommand(redisClient *c);
363 static void getSetCommand(redisClient *c);
364 static void ttlCommand(redisClient *c);
365
366 /*================================= Globals ================================= */
367
368 /* Global vars */
369 static struct redisServer server; /* server global state */
370 static struct redisCommand cmdTable[] = {
371 {"get",getCommand,2,REDIS_CMD_INLINE},
372 {"set",setCommand,3,REDIS_CMD_BULK},
373 {"setnx",setnxCommand,3,REDIS_CMD_BULK},
374 {"del",delCommand,-2,REDIS_CMD_INLINE},
375 {"exists",existsCommand,2,REDIS_CMD_INLINE},
376 {"incr",incrCommand,2,REDIS_CMD_INLINE},
377 {"decr",decrCommand,2,REDIS_CMD_INLINE},
378 {"mget",mgetCommand,-2,REDIS_CMD_INLINE},
379 {"rpush",rpushCommand,3,REDIS_CMD_BULK},
380 {"lpush",lpushCommand,3,REDIS_CMD_BULK},
381 {"rpop",rpopCommand,2,REDIS_CMD_INLINE},
382 {"lpop",lpopCommand,2,REDIS_CMD_INLINE},
383 {"llen",llenCommand,2,REDIS_CMD_INLINE},
384 {"lindex",lindexCommand,3,REDIS_CMD_INLINE},
385 {"lset",lsetCommand,4,REDIS_CMD_BULK},
386 {"lrange",lrangeCommand,4,REDIS_CMD_INLINE},
387 {"ltrim",ltrimCommand,4,REDIS_CMD_INLINE},
388 {"lrem",lremCommand,4,REDIS_CMD_BULK},
389 {"sadd",saddCommand,3,REDIS_CMD_BULK},
390 {"srem",sremCommand,3,REDIS_CMD_BULK},
391 {"smove",smoveCommand,4,REDIS_CMD_BULK},
392 {"sismember",sismemberCommand,3,REDIS_CMD_BULK},
393 {"scard",scardCommand,2,REDIS_CMD_INLINE},
394 {"sinter",sinterCommand,-2,REDIS_CMD_INLINE},
395 {"sinterstore",sinterstoreCommand,-3,REDIS_CMD_INLINE},
396 {"sunion",sunionCommand,-2,REDIS_CMD_INLINE},
397 {"sunionstore",sunionstoreCommand,-3,REDIS_CMD_INLINE},
398 {"sdiff",sdiffCommand,-2,REDIS_CMD_INLINE},
399 {"sdiffstore",sdiffstoreCommand,-3,REDIS_CMD_INLINE},
400 {"smembers",sinterCommand,2,REDIS_CMD_INLINE},
401 {"incrby",incrbyCommand,3,REDIS_CMD_INLINE},
402 {"decrby",decrbyCommand,3,REDIS_CMD_INLINE},
403 {"getset",getSetCommand,3,REDIS_CMD_BULK},
404 {"randomkey",randomkeyCommand,1,REDIS_CMD_INLINE},
405 {"select",selectCommand,2,REDIS_CMD_INLINE},
406 {"move",moveCommand,3,REDIS_CMD_INLINE},
407 {"rename",renameCommand,3,REDIS_CMD_INLINE},
408 {"renamenx",renamenxCommand,3,REDIS_CMD_INLINE},
409 {"keys",keysCommand,2,REDIS_CMD_INLINE},
410 {"dbsize",dbsizeCommand,1,REDIS_CMD_INLINE},
411 {"auth",authCommand,2,REDIS_CMD_INLINE},
412 {"ping",pingCommand,1,REDIS_CMD_INLINE},
413 {"echo",echoCommand,2,REDIS_CMD_BULK},
414 {"save",saveCommand,1,REDIS_CMD_INLINE},
415 {"bgsave",bgsaveCommand,1,REDIS_CMD_INLINE},
416 {"shutdown",shutdownCommand,1,REDIS_CMD_INLINE},
417 {"lastsave",lastsaveCommand,1,REDIS_CMD_INLINE},
418 {"type",typeCommand,2,REDIS_CMD_INLINE},
419 {"sync",syncCommand,1,REDIS_CMD_INLINE},
420 {"flushdb",flushdbCommand,1,REDIS_CMD_INLINE},
421 {"flushall",flushallCommand,1,REDIS_CMD_INLINE},
422 {"sort",sortCommand,-2,REDIS_CMD_INLINE},
423 {"info",infoCommand,1,REDIS_CMD_INLINE},
424 {"monitor",monitorCommand,1,REDIS_CMD_INLINE},
425 {"expire",expireCommand,3,REDIS_CMD_INLINE},
426 {"ttl",ttlCommand,2,REDIS_CMD_INLINE},
427 {NULL,NULL,0,0}
428 };
429
430 /*============================ Utility functions ============================ */
431
432 /* Glob-style pattern matching. */
433 int stringmatchlen(const char *pattern, int patternLen,
434 const char *string, int stringLen, int nocase)
435 {
436 while(patternLen) {
437 switch(pattern[0]) {
438 case '*':
439 while (pattern[1] == '*') {
440 pattern++;
441 patternLen--;
442 }
443 if (patternLen == 1)
444 return 1; /* match */
445 while(stringLen) {
446 if (stringmatchlen(pattern+1, patternLen-1,
447 string, stringLen, nocase))
448 return 1; /* match */
449 string++;
450 stringLen--;
451 }
452 return 0; /* no match */
453 break;
454 case '?':
455 if (stringLen == 0)
456 return 0; /* no match */
457 string++;
458 stringLen--;
459 break;
460 case '[':
461 {
462 int not, match;
463
464 pattern++;
465 patternLen--;
466 not = pattern[0] == '^';
467 if (not) {
468 pattern++;
469 patternLen--;
470 }
471 match = 0;
472 while(1) {
473 if (pattern[0] == '\\') {
474 pattern++;
475 patternLen--;
476 if (pattern[0] == string[0])
477 match = 1;
478 } else if (pattern[0] == ']') {
479 break;
480 } else if (patternLen == 0) {
481 pattern--;
482 patternLen++;
483 break;
484 } else if (pattern[1] == '-' && patternLen >= 3) {
485 int start = pattern[0];
486 int end = pattern[2];
487 int c = string[0];
488 if (start > end) {
489 int t = start;
490 start = end;
491 end = t;
492 }
493 if (nocase) {
494 start = tolower(start);
495 end = tolower(end);
496 c = tolower(c);
497 }
498 pattern += 2;
499 patternLen -= 2;
500 if (c >= start && c <= end)
501 match = 1;
502 } else {
503 if (!nocase) {
504 if (pattern[0] == string[0])
505 match = 1;
506 } else {
507 if (tolower((int)pattern[0]) == tolower((int)string[0]))
508 match = 1;
509 }
510 }
511 pattern++;
512 patternLen--;
513 }
514 if (not)
515 match = !match;
516 if (!match)
517 return 0; /* no match */
518 string++;
519 stringLen--;
520 break;
521 }
522 case '\\':
523 if (patternLen >= 2) {
524 pattern++;
525 patternLen--;
526 }
527 /* fall through */
528 default:
529 if (!nocase) {
530 if (pattern[0] != string[0])
531 return 0; /* no match */
532 } else {
533 if (tolower((int)pattern[0]) != tolower((int)string[0]))
534 return 0; /* no match */
535 }
536 string++;
537 stringLen--;
538 break;
539 }
540 pattern++;
541 patternLen--;
542 if (stringLen == 0) {
543 while(*pattern == '*') {
544 pattern++;
545 patternLen--;
546 }
547 break;
548 }
549 }
550 if (patternLen == 0 && stringLen == 0)
551 return 1;
552 return 0;
553 }
554
555 void redisLog(int level, const char *fmt, ...)
556 {
557 va_list ap;
558 FILE *fp;
559
560 fp = (server.logfile == NULL) ? stdout : fopen(server.logfile,"a");
561 if (!fp) return;
562
563 va_start(ap, fmt);
564 if (level >= server.verbosity) {
565 char *c = ".-*";
566 char buf[64];
567 time_t now;
568
569 now = time(NULL);
570 strftime(buf,64,"%d %b %H:%M:%S",gmtime(&now));
571 fprintf(fp,"%s %c ",buf,c[level]);
572 vfprintf(fp, fmt, ap);
573 fprintf(fp,"\n");
574 fflush(fp);
575 }
576 va_end(ap);
577
578 if (server.logfile) fclose(fp);
579 }
580
581 /*====================== Hash table type implementation ==================== */
582
583 /* This is an hash table type that uses the SDS dynamic strings libary as
584 * keys and radis objects as values (objects can hold SDS strings,
585 * lists, sets). */
586
587 static int sdsDictKeyCompare(void *privdata, const void *key1,
588 const void *key2)
589 {
590 int l1,l2;
591 DICT_NOTUSED(privdata);
592
593 l1 = sdslen((sds)key1);
594 l2 = sdslen((sds)key2);
595 if (l1 != l2) return 0;
596 return memcmp(key1, key2, l1) == 0;
597 }
598
599 static void dictRedisObjectDestructor(void *privdata, void *val)
600 {
601 DICT_NOTUSED(privdata);
602
603 decrRefCount(val);
604 }
605
606 static int dictSdsKeyCompare(void *privdata, const void *key1,
607 const void *key2)
608 {
609 const robj *o1 = key1, *o2 = key2;
610 return sdsDictKeyCompare(privdata,o1->ptr,o2->ptr);
611 }
612
613 static unsigned int dictSdsHash(const void *key) {
614 const robj *o = key;
615 return dictGenHashFunction(o->ptr, sdslen((sds)o->ptr));
616 }
617
618 static dictType setDictType = {
619 dictSdsHash, /* hash function */
620 NULL, /* key dup */
621 NULL, /* val dup */
622 dictSdsKeyCompare, /* key compare */
623 dictRedisObjectDestructor, /* key destructor */
624 NULL /* val destructor */
625 };
626
627 static dictType hashDictType = {
628 dictSdsHash, /* hash function */
629 NULL, /* key dup */
630 NULL, /* val dup */
631 dictSdsKeyCompare, /* key compare */
632 dictRedisObjectDestructor, /* key destructor */
633 dictRedisObjectDestructor /* val destructor */
634 };
635
636 /* ========================= Random utility functions ======================= */
637
638 /* Redis generally does not try to recover from out of memory conditions
639 * when allocating objects or strings, it is not clear if it will be possible
640 * to report this condition to the client since the networking layer itself
641 * is based on heap allocation for send buffers, so we simply abort.
642 * At least the code will be simpler to read... */
643 static void oom(const char *msg) {
644 fprintf(stderr, "%s: Out of memory\n",msg);
645 fflush(stderr);
646 sleep(1);
647 abort();
648 }
649
650 /* ====================== Redis server networking stuff ===================== */
651 void closeTimedoutClients(void) {
652 redisClient *c;
653 listNode *ln;
654 time_t now = time(NULL);
655
656 listRewind(server.clients);
657 while ((ln = listYield(server.clients)) != NULL) {
658 c = listNodeValue(ln);
659 if (!(c->flags & REDIS_SLAVE) && /* no timeout for slaves */
660 !(c->flags & REDIS_MASTER) && /* no timeout for masters */
661 (now - c->lastinteraction > server.maxidletime)) {
662 redisLog(REDIS_DEBUG,"Closing idle client");
663 freeClient(c);
664 }
665 }
666 }
667
668 /* If the percentage of used slots in the HT reaches REDIS_HT_MINFILL
669 * we resize the hash table to save memory */
670 void tryResizeHashTables(void) {
671 int j;
672
673 for (j = 0; j < server.dbnum; j++) {
674 long long size, used;
675
676 size = dictSlots(server.db[j].dict);
677 used = dictSize(server.db[j].dict);
678 if (size && used && size > REDIS_HT_MINSLOTS &&
679 (used*100/size < REDIS_HT_MINFILL)) {
680 redisLog(REDIS_NOTICE,"The hash table %d is too sparse, resize it...",j);
681 dictResize(server.db[j].dict);
682 redisLog(REDIS_NOTICE,"Hash table %d resized.",j);
683 }
684 }
685 }
686
687 int serverCron(struct aeEventLoop *eventLoop, long long id, void *clientData) {
688 int j, loops = server.cronloops++;
689 REDIS_NOTUSED(eventLoop);
690 REDIS_NOTUSED(id);
691 REDIS_NOTUSED(clientData);
692
693 /* Update the global state with the amount of used memory */
694 server.usedmemory = zmalloc_used_memory();
695
696 /* Show some info about non-empty databases */
697 for (j = 0; j < server.dbnum; j++) {
698 long long size, used, vkeys;
699
700 size = dictSlots(server.db[j].dict);
701 used = dictSize(server.db[j].dict);
702 vkeys = dictSize(server.db[j].expires);
703 if (!(loops % 5) && used > 0) {
704 redisLog(REDIS_DEBUG,"DB %d: %d keys (%d volatile) in %d slots HT.",j,used,vkeys,size);
705 /* dictPrintStats(server.dict); */
706 }
707 }
708
709 /* We don't want to resize the hash tables while a bacground saving
710 * is in progress: the saving child is created using fork() that is
711 * implemented with a copy-on-write semantic in most modern systems, so
712 * if we resize the HT while there is the saving child at work actually
713 * a lot of memory movements in the parent will cause a lot of pages
714 * copied. */
715 if (!server.bgsaveinprogress) tryResizeHashTables();
716
717 /* Show information about connected clients */
718 if (!(loops % 5)) {
719 redisLog(REDIS_DEBUG,"%d clients connected (%d slaves), %zu bytes in use",
720 listLength(server.clients)-listLength(server.slaves),
721 listLength(server.slaves),
722 server.usedmemory,
723 dictSize(server.sharingpool));
724 }
725
726 /* Close connections of timedout clients */
727 if (server.maxidletime && !(loops % 10))
728 closeTimedoutClients();
729
730 /* Check if a background saving in progress terminated */
731 if (server.bgsaveinprogress) {
732 int statloc;
733 /* XXX: TODO handle the case of the saving child killed */
734 if (wait4(-1,&statloc,WNOHANG,NULL)) {
735 int exitcode = WEXITSTATUS(statloc);
736 if (exitcode == 0) {
737 redisLog(REDIS_NOTICE,
738 "Background saving terminated with success");
739 server.dirty = 0;
740 server.lastsave = time(NULL);
741 } else {
742 redisLog(REDIS_WARNING,
743 "Background saving error");
744 }
745 server.bgsaveinprogress = 0;
746 updateSalvesWaitingBgsave(exitcode == 0 ? REDIS_OK : REDIS_ERR);
747 }
748 } else {
749 /* If there is not a background saving in progress check if
750 * we have to save now */
751 time_t now = time(NULL);
752 for (j = 0; j < server.saveparamslen; j++) {
753 struct saveparam *sp = server.saveparams+j;
754
755 if (server.dirty >= sp->changes &&
756 now-server.lastsave > sp->seconds) {
757 redisLog(REDIS_NOTICE,"%d changes in %d seconds. Saving...",
758 sp->changes, sp->seconds);
759 rdbSaveBackground(server.dbfilename);
760 break;
761 }
762 }
763 }
764
765 /* Try to expire a few timed out keys */
766 for (j = 0; j < server.dbnum; j++) {
767 redisDb *db = server.db+j;
768 int num = dictSize(db->expires);
769
770 if (num) {
771 time_t now = time(NULL);
772
773 if (num > REDIS_EXPIRELOOKUPS_PER_CRON)
774 num = REDIS_EXPIRELOOKUPS_PER_CRON;
775 while (num--) {
776 dictEntry *de;
777 time_t t;
778
779 if ((de = dictGetRandomKey(db->expires)) == NULL) break;
780 t = (time_t) dictGetEntryVal(de);
781 if (now > t) {
782 deleteKey(db,dictGetEntryKey(de));
783 }
784 }
785 }
786 }
787
788 /* Check if we should connect to a MASTER */
789 if (server.replstate == REDIS_REPL_CONNECT) {
790 redisLog(REDIS_NOTICE,"Connecting to MASTER...");
791 if (syncWithMaster() == REDIS_OK) {
792 redisLog(REDIS_NOTICE,"MASTER <-> SLAVE sync succeeded");
793 }
794 }
795 return 1000;
796 }
797
798 static void createSharedObjects(void) {
799 shared.crlf = createObject(REDIS_STRING,sdsnew("\r\n"));
800 shared.ok = createObject(REDIS_STRING,sdsnew("+OK\r\n"));
801 shared.err = createObject(REDIS_STRING,sdsnew("-ERR\r\n"));
802 shared.emptybulk = createObject(REDIS_STRING,sdsnew("$0\r\n\r\n"));
803 shared.czero = createObject(REDIS_STRING,sdsnew(":0\r\n"));
804 shared.cone = createObject(REDIS_STRING,sdsnew(":1\r\n"));
805 shared.nullbulk = createObject(REDIS_STRING,sdsnew("$-1\r\n"));
806 shared.nullmultibulk = createObject(REDIS_STRING,sdsnew("*-1\r\n"));
807 shared.emptymultibulk = createObject(REDIS_STRING,sdsnew("*0\r\n"));
808 /* no such key */
809 shared.pong = createObject(REDIS_STRING,sdsnew("+PONG\r\n"));
810 shared.wrongtypeerr = createObject(REDIS_STRING,sdsnew(
811 "-ERR Operation against a key holding the wrong kind of value\r\n"));
812 shared.nokeyerr = createObject(REDIS_STRING,sdsnew(
813 "-ERR no such key\r\n"));
814 shared.syntaxerr = createObject(REDIS_STRING,sdsnew(
815 "-ERR syntax error\r\n"));
816 shared.sameobjecterr = createObject(REDIS_STRING,sdsnew(
817 "-ERR source and destination objects are the same\r\n"));
818 shared.outofrangeerr = createObject(REDIS_STRING,sdsnew(
819 "-ERR index out of range\r\n"));
820 shared.space = createObject(REDIS_STRING,sdsnew(" "));
821 shared.colon = createObject(REDIS_STRING,sdsnew(":"));
822 shared.plus = createObject(REDIS_STRING,sdsnew("+"));
823 shared.select0 = createStringObject("select 0\r\n",10);
824 shared.select1 = createStringObject("select 1\r\n",10);
825 shared.select2 = createStringObject("select 2\r\n",10);
826 shared.select3 = createStringObject("select 3\r\n",10);
827 shared.select4 = createStringObject("select 4\r\n",10);
828 shared.select5 = createStringObject("select 5\r\n",10);
829 shared.select6 = createStringObject("select 6\r\n",10);
830 shared.select7 = createStringObject("select 7\r\n",10);
831 shared.select8 = createStringObject("select 8\r\n",10);
832 shared.select9 = createStringObject("select 9\r\n",10);
833 }
834
835 static void appendServerSaveParams(time_t seconds, int changes) {
836 server.saveparams = zrealloc(server.saveparams,sizeof(struct saveparam)*(server.saveparamslen+1));
837 if (server.saveparams == NULL) oom("appendServerSaveParams");
838 server.saveparams[server.saveparamslen].seconds = seconds;
839 server.saveparams[server.saveparamslen].changes = changes;
840 server.saveparamslen++;
841 }
842
843 static void ResetServerSaveParams() {
844 zfree(server.saveparams);
845 server.saveparams = NULL;
846 server.saveparamslen = 0;
847 }
848
849 static void initServerConfig() {
850 server.dbnum = REDIS_DEFAULT_DBNUM;
851 server.port = REDIS_SERVERPORT;
852 server.verbosity = REDIS_DEBUG;
853 server.maxidletime = REDIS_MAXIDLETIME;
854 server.saveparams = NULL;
855 server.logfile = NULL; /* NULL = log on standard output */
856 server.bindaddr = NULL;
857 server.glueoutputbuf = 1;
858 server.daemonize = 0;
859 server.pidfile = "/var/run/redis.pid";
860 server.dbfilename = "dump.rdb";
861 server.requirepass = NULL;
862 server.shareobjects = 0;
863 ResetServerSaveParams();
864
865 appendServerSaveParams(60*60,1); /* save after 1 hour and 1 change */
866 appendServerSaveParams(300,100); /* save after 5 minutes and 100 changes */
867 appendServerSaveParams(60,10000); /* save after 1 minute and 10000 changes */
868 /* Replication related */
869 server.isslave = 0;
870 server.masterhost = NULL;
871 server.masterport = 6379;
872 server.master = NULL;
873 server.replstate = REDIS_REPL_NONE;
874 }
875
876 static void initServer() {
877 int j;
878
879 signal(SIGHUP, SIG_IGN);
880 signal(SIGPIPE, SIG_IGN);
881
882 server.clients = listCreate();
883 server.slaves = listCreate();
884 server.monitors = listCreate();
885 server.objfreelist = listCreate();
886 createSharedObjects();
887 server.el = aeCreateEventLoop();
888 server.db = zmalloc(sizeof(redisDb)*server.dbnum);
889 server.sharingpool = dictCreate(&setDictType,NULL);
890 server.sharingpoolsize = 1024;
891 if (!server.db || !server.clients || !server.slaves || !server.monitors || !server.el || !server.objfreelist)
892 oom("server initialization"); /* Fatal OOM */
893 server.fd = anetTcpServer(server.neterr, server.port, server.bindaddr);
894 if (server.fd == -1) {
895 redisLog(REDIS_WARNING, "Opening TCP port: %s", server.neterr);
896 exit(1);
897 }
898 for (j = 0; j < server.dbnum; j++) {
899 server.db[j].dict = dictCreate(&hashDictType,NULL);
900 server.db[j].expires = dictCreate(&setDictType,NULL);
901 server.db[j].id = j;
902 }
903 server.cronloops = 0;
904 server.bgsaveinprogress = 0;
905 server.lastsave = time(NULL);
906 server.dirty = 0;
907 server.usedmemory = 0;
908 server.stat_numcommands = 0;
909 server.stat_numconnections = 0;
910 server.stat_starttime = time(NULL);
911 aeCreateTimeEvent(server.el, 1000, serverCron, NULL, NULL);
912 }
913
914 /* Empty the whole database */
915 static long long emptyDb() {
916 int j;
917 long long removed = 0;
918
919 for (j = 0; j < server.dbnum; j++) {
920 removed += dictSize(server.db[j].dict);
921 dictEmpty(server.db[j].dict);
922 dictEmpty(server.db[j].expires);
923 }
924 return removed;
925 }
926
927 static int yesnotoi(char *s) {
928 if (!strcasecmp(s,"yes")) return 1;
929 else if (!strcasecmp(s,"no")) return 0;
930 else return -1;
931 }
932
933 /* I agree, this is a very rudimental way to load a configuration...
934 will improve later if the config gets more complex */
935 static void loadServerConfig(char *filename) {
936 FILE *fp = fopen(filename,"r");
937 char buf[REDIS_CONFIGLINE_MAX+1], *err = NULL;
938 int linenum = 0;
939 sds line = NULL;
940
941 if (!fp) {
942 redisLog(REDIS_WARNING,"Fatal error, can't open config file");
943 exit(1);
944 }
945 while(fgets(buf,REDIS_CONFIGLINE_MAX+1,fp) != NULL) {
946 sds *argv;
947 int argc, j;
948
949 linenum++;
950 line = sdsnew(buf);
951 line = sdstrim(line," \t\r\n");
952
953 /* Skip comments and blank lines*/
954 if (line[0] == '#' || line[0] == '\0') {
955 sdsfree(line);
956 continue;
957 }
958
959 /* Split into arguments */
960 argv = sdssplitlen(line,sdslen(line)," ",1,&argc);
961 sdstolower(argv[0]);
962
963 /* Execute config directives */
964 if (!strcasecmp(argv[0],"timeout") && argc == 2) {
965 server.maxidletime = atoi(argv[1]);
966 if (server.maxidletime < 0) {
967 err = "Invalid timeout value"; goto loaderr;
968 }
969 } else if (!strcasecmp(argv[0],"port") && argc == 2) {
970 server.port = atoi(argv[1]);
971 if (server.port < 1 || server.port > 65535) {
972 err = "Invalid port"; goto loaderr;
973 }
974 } else if (!strcasecmp(argv[0],"bind") && argc == 2) {
975 server.bindaddr = zstrdup(argv[1]);
976 } else if (!strcasecmp(argv[0],"save") && argc == 3) {
977 int seconds = atoi(argv[1]);
978 int changes = atoi(argv[2]);
979 if (seconds < 1 || changes < 0) {
980 err = "Invalid save parameters"; goto loaderr;
981 }
982 appendServerSaveParams(seconds,changes);
983 } else if (!strcasecmp(argv[0],"dir") && argc == 2) {
984 if (chdir(argv[1]) == -1) {
985 redisLog(REDIS_WARNING,"Can't chdir to '%s': %s",
986 argv[1], strerror(errno));
987 exit(1);
988 }
989 } else if (!strcasecmp(argv[0],"loglevel") && argc == 2) {
990 if (!strcasecmp(argv[1],"debug")) server.verbosity = REDIS_DEBUG;
991 else if (!strcasecmp(argv[1],"notice")) server.verbosity = REDIS_NOTICE;
992 else if (!strcasecmp(argv[1],"warning")) server.verbosity = REDIS_WARNING;
993 else {
994 err = "Invalid log level. Must be one of debug, notice, warning";
995 goto loaderr;
996 }
997 } else if (!strcasecmp(argv[0],"logfile") && argc == 2) {
998 FILE *fp;
999
1000 server.logfile = zstrdup(argv[1]);
1001 if (!strcasecmp(server.logfile,"stdout")) {
1002 zfree(server.logfile);
1003 server.logfile = NULL;
1004 }
1005 if (server.logfile) {
1006 /* Test if we are able to open the file. The server will not
1007 * be able to abort just for this problem later... */
1008 fp = fopen(server.logfile,"a");
1009 if (fp == NULL) {
1010 err = sdscatprintf(sdsempty(),
1011 "Can't open the log file: %s", strerror(errno));
1012 goto loaderr;
1013 }
1014 fclose(fp);
1015 }
1016 } else if (!strcasecmp(argv[0],"databases") && argc == 2) {
1017 server.dbnum = atoi(argv[1]);
1018 if (server.dbnum < 1) {
1019 err = "Invalid number of databases"; goto loaderr;
1020 }
1021 } else if (!strcasecmp(argv[0],"slaveof") && argc == 3) {
1022 server.masterhost = sdsnew(argv[1]);
1023 server.masterport = atoi(argv[2]);
1024 server.replstate = REDIS_REPL_CONNECT;
1025 } else if (!strcasecmp(argv[0],"glueoutputbuf") && argc == 2) {
1026 if ((server.glueoutputbuf = yesnotoi(argv[1])) == -1) {
1027 err = "argument must be 'yes' or 'no'"; goto loaderr;
1028 }
1029 } else if (!strcasecmp(argv[0],"shareobjects") && argc == 2) {
1030 if ((server.shareobjects = yesnotoi(argv[1])) == -1) {
1031 err = "argument must be 'yes' or 'no'"; goto loaderr;
1032 }
1033 } else if (!strcasecmp(argv[0],"daemonize") && argc == 2) {
1034 if ((server.daemonize = yesnotoi(argv[1])) == -1) {
1035 err = "argument must be 'yes' or 'no'"; goto loaderr;
1036 }
1037 } else if (!strcasecmp(argv[0],"requirepass") && argc == 2) {
1038 server.requirepass = zstrdup(argv[1]);
1039 } else if (!strcasecmp(argv[0],"pidfile") && argc == 2) {
1040 server.pidfile = zstrdup(argv[1]);
1041 } else if (!strcasecmp(argv[0],"dbfilename") && argc == 2) {
1042 server.dbfilename = zstrdup(argv[1]);
1043 } else {
1044 err = "Bad directive or wrong number of arguments"; goto loaderr;
1045 }
1046 for (j = 0; j < argc; j++)
1047 sdsfree(argv[j]);
1048 zfree(argv);
1049 sdsfree(line);
1050 }
1051 fclose(fp);
1052 return;
1053
1054 loaderr:
1055 fprintf(stderr, "\n*** FATAL CONFIG FILE ERROR ***\n");
1056 fprintf(stderr, "Reading the configuration file, at line %d\n", linenum);
1057 fprintf(stderr, ">>> '%s'\n", line);
1058 fprintf(stderr, "%s\n", err);
1059 exit(1);
1060 }
1061
1062 static void freeClientArgv(redisClient *c) {
1063 int j;
1064
1065 for (j = 0; j < c->argc; j++)
1066 decrRefCount(c->argv[j]);
1067 c->argc = 0;
1068 }
1069
1070 static void freeClient(redisClient *c) {
1071 listNode *ln;
1072
1073 aeDeleteFileEvent(server.el,c->fd,AE_READABLE);
1074 aeDeleteFileEvent(server.el,c->fd,AE_WRITABLE);
1075 sdsfree(c->querybuf);
1076 listRelease(c->reply);
1077 freeClientArgv(c);
1078 close(c->fd);
1079 ln = listSearchKey(server.clients,c);
1080 assert(ln != NULL);
1081 listDelNode(server.clients,ln);
1082 if (c->flags & REDIS_SLAVE) {
1083 if (c->replstate == REDIS_REPL_SEND_BULK && c->repldbfd != -1)
1084 close(c->repldbfd);
1085 list *l = (c->flags & REDIS_MONITOR) ? server.monitors : server.slaves;
1086 ln = listSearchKey(l,c);
1087 assert(ln != NULL);
1088 listDelNode(l,ln);
1089 }
1090 if (c->flags & REDIS_MASTER) {
1091 server.master = NULL;
1092 server.replstate = REDIS_REPL_CONNECT;
1093 }
1094 zfree(c->argv);
1095 zfree(c);
1096 }
1097
1098 static void glueReplyBuffersIfNeeded(redisClient *c) {
1099 int totlen = 0;
1100 listNode *ln;
1101 robj *o;
1102
1103 listRewind(c->reply);
1104 while((ln = listYield(c->reply))) {
1105 o = ln->value;
1106 totlen += sdslen(o->ptr);
1107 /* This optimization makes more sense if we don't have to copy
1108 * too much data */
1109 if (totlen > 1024) return;
1110 }
1111 if (totlen > 0) {
1112 char buf[1024];
1113 int copylen = 0;
1114
1115 listRewind(c->reply);
1116 while((ln = listYield(c->reply))) {
1117 o = ln->value;
1118 memcpy(buf+copylen,o->ptr,sdslen(o->ptr));
1119 copylen += sdslen(o->ptr);
1120 listDelNode(c->reply,ln);
1121 }
1122 /* Now the output buffer is empty, add the new single element */
1123 o = createObject(REDIS_STRING,sdsnewlen(buf,totlen));
1124 if (!listAddNodeTail(c->reply,o)) oom("listAddNodeTail");
1125 }
1126 }
1127
1128 static void sendReplyToClient(aeEventLoop *el, int fd, void *privdata, int mask) {
1129 redisClient *c = privdata;
1130 int nwritten = 0, totwritten = 0, objlen;
1131 robj *o;
1132 REDIS_NOTUSED(el);
1133 REDIS_NOTUSED(mask);
1134
1135 if (server.glueoutputbuf && listLength(c->reply) > 1)
1136 glueReplyBuffersIfNeeded(c);
1137 while(listLength(c->reply)) {
1138 o = listNodeValue(listFirst(c->reply));
1139 objlen = sdslen(o->ptr);
1140
1141 if (objlen == 0) {
1142 listDelNode(c->reply,listFirst(c->reply));
1143 continue;
1144 }
1145
1146 if (c->flags & REDIS_MASTER) {
1147 nwritten = objlen - c->sentlen;
1148 } else {
1149 nwritten = write(fd, ((char*)o->ptr)+c->sentlen, objlen - c->sentlen);
1150 if (nwritten <= 0) break;
1151 }
1152 c->sentlen += nwritten;
1153 totwritten += nwritten;
1154 /* If we fully sent the object on head go to the next one */
1155 if (c->sentlen == objlen) {
1156 listDelNode(c->reply,listFirst(c->reply));
1157 c->sentlen = 0;
1158 }
1159 }
1160 if (nwritten == -1) {
1161 if (errno == EAGAIN) {
1162 nwritten = 0;
1163 } else {
1164 redisLog(REDIS_DEBUG,
1165 "Error writing to client: %s", strerror(errno));
1166 freeClient(c);
1167 return;
1168 }
1169 }
1170 if (totwritten > 0) c->lastinteraction = time(NULL);
1171 if (listLength(c->reply) == 0) {
1172 c->sentlen = 0;
1173 aeDeleteFileEvent(server.el,c->fd,AE_WRITABLE);
1174 }
1175 }
1176
1177 static struct redisCommand *lookupCommand(char *name) {
1178 int j = 0;
1179 while(cmdTable[j].name != NULL) {
1180 if (!strcasecmp(name,cmdTable[j].name)) return &cmdTable[j];
1181 j++;
1182 }
1183 return NULL;
1184 }
1185
1186 /* resetClient prepare the client to process the next command */
1187 static void resetClient(redisClient *c) {
1188 freeClientArgv(c);
1189 c->bulklen = -1;
1190 }
1191
1192 /* If this function gets called we already read a whole
1193 * command, argments are in the client argv/argc fields.
1194 * processCommand() execute the command or prepare the
1195 * server for a bulk read from the client.
1196 *
1197 * If 1 is returned the client is still alive and valid and
1198 * and other operations can be performed by the caller. Otherwise
1199 * if 0 is returned the client was destroied (i.e. after QUIT). */
1200 static int processCommand(redisClient *c) {
1201 struct redisCommand *cmd;
1202 long long dirty;
1203
1204 /* The QUIT command is handled as a special case. Normal command
1205 * procs are unable to close the client connection safely */
1206 if (!strcasecmp(c->argv[0]->ptr,"quit")) {
1207 freeClient(c);
1208 return 0;
1209 }
1210 cmd = lookupCommand(c->argv[0]->ptr);
1211 if (!cmd) {
1212 addReplySds(c,sdsnew("-ERR unknown command\r\n"));
1213 resetClient(c);
1214 return 1;
1215 } else if ((cmd->arity > 0 && cmd->arity != c->argc) ||
1216 (c->argc < -cmd->arity)) {
1217 addReplySds(c,sdsnew("-ERR wrong number of arguments\r\n"));
1218 resetClient(c);
1219 return 1;
1220 } else if (cmd->flags & REDIS_CMD_BULK && c->bulklen == -1) {
1221 int bulklen = atoi(c->argv[c->argc-1]->ptr);
1222
1223 decrRefCount(c->argv[c->argc-1]);
1224 if (bulklen < 0 || bulklen > 1024*1024*1024) {
1225 c->argc--;
1226 addReplySds(c,sdsnew("-ERR invalid bulk write count\r\n"));
1227 resetClient(c);
1228 return 1;
1229 }
1230 c->argc--;
1231 c->bulklen = bulklen+2; /* add two bytes for CR+LF */
1232 /* It is possible that the bulk read is already in the
1233 * buffer. Check this condition and handle it accordingly */
1234 if ((signed)sdslen(c->querybuf) >= c->bulklen) {
1235 c->argv[c->argc] = createStringObject(c->querybuf,c->bulklen-2);
1236 c->argc++;
1237 c->querybuf = sdsrange(c->querybuf,c->bulklen,-1);
1238 } else {
1239 return 1;
1240 }
1241 }
1242 /* Let's try to share objects on the command arguments vector */
1243 if (server.shareobjects) {
1244 int j;
1245 for(j = 1; j < c->argc; j++)
1246 c->argv[j] = tryObjectSharing(c->argv[j]);
1247 }
1248 /* Check if the user is authenticated */
1249 if (server.requirepass && !c->authenticated && cmd->proc != authCommand) {
1250 addReplySds(c,sdsnew("-ERR operation not permitted\r\n"));
1251 resetClient(c);
1252 return 1;
1253 }
1254
1255 /* Exec the command */
1256 dirty = server.dirty;
1257 cmd->proc(c);
1258 if (server.dirty-dirty != 0 && listLength(server.slaves))
1259 replicationFeedSlaves(server.slaves,cmd,c->db->id,c->argv,c->argc);
1260 if (listLength(server.monitors))
1261 replicationFeedSlaves(server.monitors,cmd,c->db->id,c->argv,c->argc);
1262 server.stat_numcommands++;
1263
1264 /* Prepare the client for the next command */
1265 if (c->flags & REDIS_CLOSE) {
1266 freeClient(c);
1267 return 0;
1268 }
1269 resetClient(c);
1270 return 1;
1271 }
1272
1273 static void replicationFeedSlaves(list *slaves, struct redisCommand *cmd, int dictid, robj **argv, int argc) {
1274 listNode *ln;
1275 int outc = 0, j;
1276 robj **outv;
1277 /* (args*2)+1 is enough room for args, spaces, newlines */
1278 robj *static_outv[REDIS_STATIC_ARGS*2+1];
1279
1280 if (argc <= REDIS_STATIC_ARGS) {
1281 outv = static_outv;
1282 } else {
1283 outv = zmalloc(sizeof(robj*)*(argc*2+1));
1284 if (!outv) oom("replicationFeedSlaves");
1285 }
1286
1287 for (j = 0; j < argc; j++) {
1288 if (j != 0) outv[outc++] = shared.space;
1289 if ((cmd->flags & REDIS_CMD_BULK) && j == argc-1) {
1290 robj *lenobj;
1291
1292 lenobj = createObject(REDIS_STRING,
1293 sdscatprintf(sdsempty(),"%d\r\n",sdslen(argv[j]->ptr)));
1294 lenobj->refcount = 0;
1295 outv[outc++] = lenobj;
1296 }
1297 outv[outc++] = argv[j];
1298 }
1299 outv[outc++] = shared.crlf;
1300
1301 /* Increment all the refcounts at start and decrement at end in order to
1302 * be sure to free objects if there is no slave in a replication state
1303 * able to be feed with commands */
1304 for (j = 0; j < outc; j++) incrRefCount(outv[j]);
1305 listRewind(slaves);
1306 while((ln = listYield(slaves))) {
1307 redisClient *slave = ln->value;
1308
1309 /* Don't feed slaves that are still waiting for BGSAVE to start */
1310 if (slave->replstate == REDIS_REPL_WAIT_BGSAVE_START) continue;
1311
1312 /* Feed all the other slaves, MONITORs and so on */
1313 if (slave->slaveseldb != dictid) {
1314 robj *selectcmd;
1315
1316 switch(dictid) {
1317 case 0: selectcmd = shared.select0; break;
1318 case 1: selectcmd = shared.select1; break;
1319 case 2: selectcmd = shared.select2; break;
1320 case 3: selectcmd = shared.select3; break;
1321 case 4: selectcmd = shared.select4; break;
1322 case 5: selectcmd = shared.select5; break;
1323 case 6: selectcmd = shared.select6; break;
1324 case 7: selectcmd = shared.select7; break;
1325 case 8: selectcmd = shared.select8; break;
1326 case 9: selectcmd = shared.select9; break;
1327 default:
1328 selectcmd = createObject(REDIS_STRING,
1329 sdscatprintf(sdsempty(),"select %d\r\n",dictid));
1330 selectcmd->refcount = 0;
1331 break;
1332 }
1333 addReply(slave,selectcmd);
1334 slave->slaveseldb = dictid;
1335 }
1336 for (j = 0; j < outc; j++) addReply(slave,outv[j]);
1337 }
1338 for (j = 0; j < outc; j++) decrRefCount(outv[j]);
1339 if (outv != static_outv) zfree(outv);
1340 }
1341
1342 static void readQueryFromClient(aeEventLoop *el, int fd, void *privdata, int mask) {
1343 redisClient *c = (redisClient*) privdata;
1344 char buf[REDIS_IOBUF_LEN];
1345 int nread;
1346 REDIS_NOTUSED(el);
1347 REDIS_NOTUSED(mask);
1348
1349 nread = read(fd, buf, REDIS_IOBUF_LEN);
1350 if (nread == -1) {
1351 if (errno == EAGAIN) {
1352 nread = 0;
1353 } else {
1354 redisLog(REDIS_DEBUG, "Reading from client: %s",strerror(errno));
1355 freeClient(c);
1356 return;
1357 }
1358 } else if (nread == 0) {
1359 redisLog(REDIS_DEBUG, "Client closed connection");
1360 freeClient(c);
1361 return;
1362 }
1363 if (nread) {
1364 c->querybuf = sdscatlen(c->querybuf, buf, nread);
1365 c->lastinteraction = time(NULL);
1366 } else {
1367 return;
1368 }
1369
1370 again:
1371 if (c->bulklen == -1) {
1372 /* Read the first line of the query */
1373 char *p = strchr(c->querybuf,'\n');
1374 size_t querylen;
1375 if (p) {
1376 sds query, *argv;
1377 int argc, j;
1378
1379 query = c->querybuf;
1380 c->querybuf = sdsempty();
1381 querylen = 1+(p-(query));
1382 if (sdslen(query) > querylen) {
1383 /* leave data after the first line of the query in the buffer */
1384 c->querybuf = sdscatlen(c->querybuf,query+querylen,sdslen(query)-querylen);
1385 }
1386 *p = '\0'; /* remove "\n" */
1387 if (*(p-1) == '\r') *(p-1) = '\0'; /* and "\r" if any */
1388 sdsupdatelen(query);
1389
1390 /* Now we can split the query in arguments */
1391 if (sdslen(query) == 0) {
1392 /* Ignore empty query */
1393 sdsfree(query);
1394 return;
1395 }
1396 argv = sdssplitlen(query,sdslen(query)," ",1,&argc);
1397 if (argv == NULL) oom("sdssplitlen");
1398 sdsfree(query);
1399
1400 if (c->argv) zfree(c->argv);
1401 c->argv = zmalloc(sizeof(robj*)*argc);
1402 if (c->argv == NULL) oom("allocating arguments list for client");
1403
1404 for (j = 0; j < argc; j++) {
1405 if (sdslen(argv[j])) {
1406 c->argv[c->argc] = createObject(REDIS_STRING,argv[j]);
1407 c->argc++;
1408 } else {
1409 sdsfree(argv[j]);
1410 }
1411 }
1412 zfree(argv);
1413 /* Execute the command. If the client is still valid
1414 * after processCommand() return and there is something
1415 * on the query buffer try to process the next command. */
1416 if (processCommand(c) && sdslen(c->querybuf)) goto again;
1417 return;
1418 } else if (sdslen(c->querybuf) >= 1024) {
1419 redisLog(REDIS_DEBUG, "Client protocol error");
1420 freeClient(c);
1421 return;
1422 }
1423 } else {
1424 /* Bulk read handling. Note that if we are at this point
1425 the client already sent a command terminated with a newline,
1426 we are reading the bulk data that is actually the last
1427 argument of the command. */
1428 int qbl = sdslen(c->querybuf);
1429
1430 if (c->bulklen <= qbl) {
1431 /* Copy everything but the final CRLF as final argument */
1432 c->argv[c->argc] = createStringObject(c->querybuf,c->bulklen-2);
1433 c->argc++;
1434 c->querybuf = sdsrange(c->querybuf,c->bulklen,-1);
1435 processCommand(c);
1436 return;
1437 }
1438 }
1439 }
1440
1441 static int selectDb(redisClient *c, int id) {
1442 if (id < 0 || id >= server.dbnum)
1443 return REDIS_ERR;
1444 c->db = &server.db[id];
1445 return REDIS_OK;
1446 }
1447
1448 static void *dupClientReplyValue(void *o) {
1449 incrRefCount((robj*)o);
1450 return 0;
1451 }
1452
1453 static redisClient *createClient(int fd) {
1454 redisClient *c = zmalloc(sizeof(*c));
1455
1456 anetNonBlock(NULL,fd);
1457 anetTcpNoDelay(NULL,fd);
1458 if (!c) return NULL;
1459 selectDb(c,0);
1460 c->fd = fd;
1461 c->querybuf = sdsempty();
1462 c->argc = 0;
1463 c->argv = NULL;
1464 c->bulklen = -1;
1465 c->sentlen = 0;
1466 c->flags = 0;
1467 c->lastinteraction = time(NULL);
1468 c->authenticated = 0;
1469 c->replstate = REDIS_REPL_NONE;
1470 if ((c->reply = listCreate()) == NULL) oom("listCreate");
1471 listSetFreeMethod(c->reply,decrRefCount);
1472 listSetDupMethod(c->reply,dupClientReplyValue);
1473 if (aeCreateFileEvent(server.el, c->fd, AE_READABLE,
1474 readQueryFromClient, c, NULL) == AE_ERR) {
1475 freeClient(c);
1476 return NULL;
1477 }
1478 if (!listAddNodeTail(server.clients,c)) oom("listAddNodeTail");
1479 return c;
1480 }
1481
1482 static void addReply(redisClient *c, robj *obj) {
1483 if (listLength(c->reply) == 0 &&
1484 (c->replstate == REDIS_REPL_NONE ||
1485 c->replstate == REDIS_REPL_ONLINE) &&
1486 aeCreateFileEvent(server.el, c->fd, AE_WRITABLE,
1487 sendReplyToClient, c, NULL) == AE_ERR) return;
1488 if (!listAddNodeTail(c->reply,obj)) oom("listAddNodeTail");
1489 incrRefCount(obj);
1490 }
1491
1492 static void addReplySds(redisClient *c, sds s) {
1493 robj *o = createObject(REDIS_STRING,s);
1494 addReply(c,o);
1495 decrRefCount(o);
1496 }
1497
1498 static void acceptHandler(aeEventLoop *el, int fd, void *privdata, int mask) {
1499 int cport, cfd;
1500 char cip[128];
1501 REDIS_NOTUSED(el);
1502 REDIS_NOTUSED(mask);
1503 REDIS_NOTUSED(privdata);
1504
1505 cfd = anetAccept(server.neterr, fd, cip, &cport);
1506 if (cfd == AE_ERR) {
1507 redisLog(REDIS_DEBUG,"Accepting client connection: %s", server.neterr);
1508 return;
1509 }
1510 redisLog(REDIS_DEBUG,"Accepted %s:%d", cip, cport);
1511 if (createClient(cfd) == NULL) {
1512 redisLog(REDIS_WARNING,"Error allocating resoures for the client");
1513 close(cfd); /* May be already closed, just ingore errors */
1514 return;
1515 }
1516 server.stat_numconnections++;
1517 }
1518
1519 /* ======================= Redis objects implementation ===================== */
1520
1521 static robj *createObject(int type, void *ptr) {
1522 robj *o;
1523
1524 if (listLength(server.objfreelist)) {
1525 listNode *head = listFirst(server.objfreelist);
1526 o = listNodeValue(head);
1527 listDelNode(server.objfreelist,head);
1528 } else {
1529 o = zmalloc(sizeof(*o));
1530 }
1531 if (!o) oom("createObject");
1532 o->type = type;
1533 o->ptr = ptr;
1534 o->refcount = 1;
1535 return o;
1536 }
1537
1538 static robj *createStringObject(char *ptr, size_t len) {
1539 return createObject(REDIS_STRING,sdsnewlen(ptr,len));
1540 }
1541
1542 static robj *createListObject(void) {
1543 list *l = listCreate();
1544
1545 if (!l) oom("listCreate");
1546 listSetFreeMethod(l,decrRefCount);
1547 return createObject(REDIS_LIST,l);
1548 }
1549
1550 static robj *createSetObject(void) {
1551 dict *d = dictCreate(&setDictType,NULL);
1552 if (!d) oom("dictCreate");
1553 return createObject(REDIS_SET,d);
1554 }
1555
1556 static void freeStringObject(robj *o) {
1557 sdsfree(o->ptr);
1558 }
1559
1560 static void freeListObject(robj *o) {
1561 listRelease((list*) o->ptr);
1562 }
1563
1564 static void freeSetObject(robj *o) {
1565 dictRelease((dict*) o->ptr);
1566 }
1567
1568 static void freeHashObject(robj *o) {
1569 dictRelease((dict*) o->ptr);
1570 }
1571
1572 static void incrRefCount(robj *o) {
1573 o->refcount++;
1574 #ifdef DEBUG_REFCOUNT
1575 if (o->type == REDIS_STRING)
1576 printf("Increment '%s'(%p), now is: %d\n",o->ptr,o,o->refcount);
1577 #endif
1578 }
1579
1580 static void decrRefCount(void *obj) {
1581 robj *o = obj;
1582
1583 #ifdef DEBUG_REFCOUNT
1584 if (o->type == REDIS_STRING)
1585 printf("Decrement '%s'(%p), now is: %d\n",o->ptr,o,o->refcount-1);
1586 #endif
1587 if (--(o->refcount) == 0) {
1588 switch(o->type) {
1589 case REDIS_STRING: freeStringObject(o); break;
1590 case REDIS_LIST: freeListObject(o); break;
1591 case REDIS_SET: freeSetObject(o); break;
1592 case REDIS_HASH: freeHashObject(o); break;
1593 default: assert(0 != 0); break;
1594 }
1595 if (listLength(server.objfreelist) > REDIS_OBJFREELIST_MAX ||
1596 !listAddNodeHead(server.objfreelist,o))
1597 zfree(o);
1598 }
1599 }
1600
1601 /* Try to share an object against the shared objects pool */
1602 static robj *tryObjectSharing(robj *o) {
1603 struct dictEntry *de;
1604 unsigned long c;
1605
1606 if (o == NULL || server.shareobjects == 0) return o;
1607
1608 assert(o->type == REDIS_STRING);
1609 de = dictFind(server.sharingpool,o);
1610 if (de) {
1611 robj *shared = dictGetEntryKey(de);
1612
1613 c = ((unsigned long) dictGetEntryVal(de))+1;
1614 dictGetEntryVal(de) = (void*) c;
1615 incrRefCount(shared);
1616 decrRefCount(o);
1617 return shared;
1618 } else {
1619 /* Here we are using a stream algorihtm: Every time an object is
1620 * shared we increment its count, everytime there is a miss we
1621 * recrement the counter of a random object. If this object reaches
1622 * zero we remove the object and put the current object instead. */
1623 if (dictSize(server.sharingpool) >=
1624 server.sharingpoolsize) {
1625 de = dictGetRandomKey(server.sharingpool);
1626 assert(de != NULL);
1627 c = ((unsigned long) dictGetEntryVal(de))-1;
1628 dictGetEntryVal(de) = (void*) c;
1629 if (c == 0) {
1630 dictDelete(server.sharingpool,de->key);
1631 }
1632 } else {
1633 c = 0; /* If the pool is empty we want to add this object */
1634 }
1635 if (c == 0) {
1636 int retval;
1637
1638 retval = dictAdd(server.sharingpool,o,(void*)1);
1639 assert(retval == DICT_OK);
1640 incrRefCount(o);
1641 }
1642 return o;
1643 }
1644 }
1645
1646 static robj *lookupKey(redisDb *db, robj *key) {
1647 dictEntry *de = dictFind(db->dict,key);
1648 return de ? dictGetEntryVal(de) : NULL;
1649 }
1650
1651 static robj *lookupKeyRead(redisDb *db, robj *key) {
1652 expireIfNeeded(db,key);
1653 return lookupKey(db,key);
1654 }
1655
1656 static robj *lookupKeyWrite(redisDb *db, robj *key) {
1657 deleteIfVolatile(db,key);
1658 return lookupKey(db,key);
1659 }
1660
1661 static int deleteKey(redisDb *db, robj *key) {
1662 int retval;
1663
1664 /* We need to protect key from destruction: after the first dictDelete()
1665 * it may happen that 'key' is no longer valid if we don't increment
1666 * it's count. This may happen when we get the object reference directly
1667 * from the hash table with dictRandomKey() or dict iterators */
1668 incrRefCount(key);
1669 if (dictSize(db->expires)) dictDelete(db->expires,key);
1670 retval = dictDelete(db->dict,key);
1671 decrRefCount(key);
1672
1673 return retval == DICT_OK;
1674 }
1675
1676 /*============================ DB saving/loading ============================ */
1677
1678 static int rdbSaveType(FILE *fp, unsigned char type) {
1679 if (fwrite(&type,1,1,fp) == 0) return -1;
1680 return 0;
1681 }
1682
1683 static int rdbSaveTime(FILE *fp, time_t t) {
1684 int32_t t32 = (int32_t) t;
1685 if (fwrite(&t32,4,1,fp) == 0) return -1;
1686 return 0;
1687 }
1688
1689 /* check rdbLoadLen() comments for more info */
1690 static int rdbSaveLen(FILE *fp, uint32_t len) {
1691 unsigned char buf[2];
1692
1693 if (len < (1<<6)) {
1694 /* Save a 6 bit len */
1695 buf[0] = (len&0xFF)|(REDIS_RDB_6BITLEN<<6);
1696 if (fwrite(buf,1,1,fp) == 0) return -1;
1697 } else if (len < (1<<14)) {
1698 /* Save a 14 bit len */
1699 buf[0] = ((len>>8)&0xFF)|(REDIS_RDB_14BITLEN<<6);
1700 buf[1] = len&0xFF;
1701 if (fwrite(buf,2,1,fp) == 0) return -1;
1702 } else {
1703 /* Save a 32 bit len */
1704 buf[0] = (REDIS_RDB_32BITLEN<<6);
1705 if (fwrite(buf,1,1,fp) == 0) return -1;
1706 len = htonl(len);
1707 if (fwrite(&len,4,1,fp) == 0) return -1;
1708 }
1709 return 0;
1710 }
1711
1712 /* String objects in the form "2391" "-100" without any space and with a
1713 * range of values that can fit in an 8, 16 or 32 bit signed value can be
1714 * encoded as integers to save space */
1715 int rdbTryIntegerEncoding(sds s, unsigned char *enc) {
1716 long long value;
1717 char *endptr, buf[32];
1718
1719 /* Check if it's possible to encode this value as a number */
1720 value = strtoll(s, &endptr, 10);
1721 if (endptr[0] != '\0') return 0;
1722 snprintf(buf,32,"%lld",value);
1723
1724 /* If the number converted back into a string is not identical
1725 * then it's not possible to encode the string as integer */
1726 if (strlen(buf) != sdslen(s) || memcmp(buf,s,sdslen(s))) return 0;
1727
1728 /* Finally check if it fits in our ranges */
1729 if (value >= -(1<<7) && value <= (1<<7)-1) {
1730 enc[0] = (REDIS_RDB_ENCVAL<<6)|REDIS_RDB_ENC_INT8;
1731 enc[1] = value&0xFF;
1732 return 2;
1733 } else if (value >= -(1<<15) && value <= (1<<15)-1) {
1734 enc[0] = (REDIS_RDB_ENCVAL<<6)|REDIS_RDB_ENC_INT16;
1735 enc[1] = value&0xFF;
1736 enc[2] = (value>>8)&0xFF;
1737 return 3;
1738 } else if (value >= -((long long)1<<31) && value <= ((long long)1<<31)-1) {
1739 enc[0] = (REDIS_RDB_ENCVAL<<6)|REDIS_RDB_ENC_INT32;
1740 enc[1] = value&0xFF;
1741 enc[2] = (value>>8)&0xFF;
1742 enc[3] = (value>>16)&0xFF;
1743 enc[4] = (value>>24)&0xFF;
1744 return 5;
1745 } else {
1746 return 0;
1747 }
1748 }
1749
1750 static int rdbSaveLzfStringObject(FILE *fp, robj *obj) {
1751 unsigned int comprlen, outlen;
1752 unsigned char byte;
1753 void *out;
1754
1755 /* We require at least four bytes compression for this to be worth it */
1756 outlen = sdslen(obj->ptr)-4;
1757 if (outlen <= 0) return 0;
1758 if ((out = zmalloc(outlen+1)) == NULL) return 0;
1759 comprlen = lzf_compress(obj->ptr, sdslen(obj->ptr), out, outlen);
1760 if (comprlen == 0) {
1761 zfree(out);
1762 return 0;
1763 }
1764 /* Data compressed! Let's save it on disk */
1765 byte = (REDIS_RDB_ENCVAL<<6)|REDIS_RDB_ENC_LZF;
1766 if (fwrite(&byte,1,1,fp) == 0) goto writeerr;
1767 if (rdbSaveLen(fp,comprlen) == -1) goto writeerr;
1768 if (rdbSaveLen(fp,sdslen(obj->ptr)) == -1) goto writeerr;
1769 if (fwrite(out,comprlen,1,fp) == 0) goto writeerr;
1770 zfree(out);
1771 return comprlen;
1772
1773 writeerr:
1774 zfree(out);
1775 return -1;
1776 }
1777
1778 /* Save a string objet as [len][data] on disk. If the object is a string
1779 * representation of an integer value we try to safe it in a special form */
1780 static int rdbSaveStringObject(FILE *fp, robj *obj) {
1781 size_t len = sdslen(obj->ptr);
1782 int enclen;
1783
1784 /* Try integer encoding */
1785 if (len <= 11) {
1786 unsigned char buf[5];
1787 if ((enclen = rdbTryIntegerEncoding(obj->ptr,buf)) > 0) {
1788 if (fwrite(buf,enclen,1,fp) == 0) return -1;
1789 return 0;
1790 }
1791 }
1792
1793 /* Try LZF compression - under 20 bytes it's unable to compress even
1794 * aaaaaaaaaaaaaaaaaa so skip it */
1795 if (1 && len > 20) {
1796 int retval;
1797
1798 retval = rdbSaveLzfStringObject(fp,obj);
1799 if (retval == -1) return -1;
1800 if (retval > 0) return 0;
1801 /* retval == 0 means data can't be compressed, save the old way */
1802 }
1803
1804 /* Store verbatim */
1805 if (rdbSaveLen(fp,len) == -1) return -1;
1806 if (len && fwrite(obj->ptr,len,1,fp) == 0) return -1;
1807 return 0;
1808 }
1809
1810 /* Save the DB on disk. Return REDIS_ERR on error, REDIS_OK on success */
1811 static int rdbSave(char *filename) {
1812 dictIterator *di = NULL;
1813 dictEntry *de;
1814 FILE *fp;
1815 char tmpfile[256];
1816 int j;
1817 time_t now = time(NULL);
1818
1819 snprintf(tmpfile,256,"temp-%d.%ld.rdb",(int)time(NULL),(long int)random());
1820 fp = fopen(tmpfile,"w");
1821 if (!fp) {
1822 redisLog(REDIS_WARNING, "Failed saving the DB: %s", strerror(errno));
1823 return REDIS_ERR;
1824 }
1825 if (fwrite("REDIS0001",9,1,fp) == 0) goto werr;
1826 for (j = 0; j < server.dbnum; j++) {
1827 redisDb *db = server.db+j;
1828 dict *d = db->dict;
1829 if (dictSize(d) == 0) continue;
1830 di = dictGetIterator(d);
1831 if (!di) {
1832 fclose(fp);
1833 return REDIS_ERR;
1834 }
1835
1836 /* Write the SELECT DB opcode */
1837 if (rdbSaveType(fp,REDIS_SELECTDB) == -1) goto werr;
1838 if (rdbSaveLen(fp,j) == -1) goto werr;
1839
1840 /* Iterate this DB writing every entry */
1841 while((de = dictNext(di)) != NULL) {
1842 robj *key = dictGetEntryKey(de);
1843 robj *o = dictGetEntryVal(de);
1844 time_t expiretime = getExpire(db,key);
1845
1846 /* Save the expire time */
1847 if (expiretime != -1) {
1848 /* If this key is already expired skip it */
1849 if (expiretime < now) continue;
1850 if (rdbSaveType(fp,REDIS_EXPIRETIME) == -1) goto werr;
1851 if (rdbSaveTime(fp,expiretime) == -1) goto werr;
1852 }
1853 /* Save the key and associated value */
1854 if (rdbSaveType(fp,o->type) == -1) goto werr;
1855 if (rdbSaveStringObject(fp,key) == -1) goto werr;
1856 if (o->type == REDIS_STRING) {
1857 /* Save a string value */
1858 if (rdbSaveStringObject(fp,o) == -1) goto werr;
1859 } else if (o->type == REDIS_LIST) {
1860 /* Save a list value */
1861 list *list = o->ptr;
1862 listNode *ln;
1863
1864 listRewind(list);
1865 if (rdbSaveLen(fp,listLength(list)) == -1) goto werr;
1866 while((ln = listYield(list))) {
1867 robj *eleobj = listNodeValue(ln);
1868
1869 if (rdbSaveStringObject(fp,eleobj) == -1) goto werr;
1870 }
1871 } else if (o->type == REDIS_SET) {
1872 /* Save a set value */
1873 dict *set = o->ptr;
1874 dictIterator *di = dictGetIterator(set);
1875 dictEntry *de;
1876
1877 if (!set) oom("dictGetIteraotr");
1878 if (rdbSaveLen(fp,dictSize(set)) == -1) goto werr;
1879 while((de = dictNext(di)) != NULL) {
1880 robj *eleobj = dictGetEntryKey(de);
1881
1882 if (rdbSaveStringObject(fp,eleobj) == -1) goto werr;
1883 }
1884 dictReleaseIterator(di);
1885 } else {
1886 assert(0 != 0);
1887 }
1888 }
1889 dictReleaseIterator(di);
1890 }
1891 /* EOF opcode */
1892 if (rdbSaveType(fp,REDIS_EOF) == -1) goto werr;
1893
1894 /* Make sure data will not remain on the OS's output buffers */
1895 fflush(fp);
1896 fsync(fileno(fp));
1897 fclose(fp);
1898
1899 /* Use RENAME to make sure the DB file is changed atomically only
1900 * if the generate DB file is ok. */
1901 if (rename(tmpfile,filename) == -1) {
1902 redisLog(REDIS_WARNING,"Error moving temp DB file on the final destionation: %s", strerror(errno));
1903 unlink(tmpfile);
1904 return REDIS_ERR;
1905 }
1906 redisLog(REDIS_NOTICE,"DB saved on disk");
1907 server.dirty = 0;
1908 server.lastsave = time(NULL);
1909 return REDIS_OK;
1910
1911 werr:
1912 fclose(fp);
1913 unlink(tmpfile);
1914 redisLog(REDIS_WARNING,"Write error saving DB on disk: %s", strerror(errno));
1915 if (di) dictReleaseIterator(di);
1916 return REDIS_ERR;
1917 }
1918
1919 static int rdbSaveBackground(char *filename) {
1920 pid_t childpid;
1921
1922 if (server.bgsaveinprogress) return REDIS_ERR;
1923 if ((childpid = fork()) == 0) {
1924 /* Child */
1925 close(server.fd);
1926 if (rdbSave(filename) == REDIS_OK) {
1927 exit(0);
1928 } else {
1929 exit(1);
1930 }
1931 } else {
1932 /* Parent */
1933 if (childpid == -1) {
1934 redisLog(REDIS_WARNING,"Can't save in background: fork: %s",
1935 strerror(errno));
1936 return REDIS_ERR;
1937 }
1938 redisLog(REDIS_NOTICE,"Background saving started by pid %d",childpid);
1939 server.bgsaveinprogress = 1;
1940 return REDIS_OK;
1941 }
1942 return REDIS_OK; /* unreached */
1943 }
1944
1945 static int rdbLoadType(FILE *fp) {
1946 unsigned char type;
1947 if (fread(&type,1,1,fp) == 0) return -1;
1948 return type;
1949 }
1950
1951 static time_t rdbLoadTime(FILE *fp) {
1952 int32_t t32;
1953 if (fread(&t32,4,1,fp) == 0) return -1;
1954 return (time_t) t32;
1955 }
1956
1957 /* Load an encoded length from the DB, see the REDIS_RDB_* defines on the top
1958 * of this file for a description of how this are stored on disk.
1959 *
1960 * isencoded is set to 1 if the readed length is not actually a length but
1961 * an "encoding type", check the above comments for more info */
1962 static uint32_t rdbLoadLen(FILE *fp, int rdbver, int *isencoded) {
1963 unsigned char buf[2];
1964 uint32_t len;
1965
1966 if (isencoded) *isencoded = 0;
1967 if (rdbver == 0) {
1968 if (fread(&len,4,1,fp) == 0) return REDIS_RDB_LENERR;
1969 return ntohl(len);
1970 } else {
1971 int type;
1972
1973 if (fread(buf,1,1,fp) == 0) return REDIS_RDB_LENERR;
1974 type = (buf[0]&0xC0)>>6;
1975 if (type == REDIS_RDB_6BITLEN) {
1976 /* Read a 6 bit len */
1977 return buf[0]&0x3F;
1978 } else if (type == REDIS_RDB_ENCVAL) {
1979 /* Read a 6 bit len encoding type */
1980 if (isencoded) *isencoded = 1;
1981 return buf[0]&0x3F;
1982 } else if (type == REDIS_RDB_14BITLEN) {
1983 /* Read a 14 bit len */
1984 if (fread(buf+1,1,1,fp) == 0) return REDIS_RDB_LENERR;
1985 return ((buf[0]&0x3F)<<8)|buf[1];
1986 } else {
1987 /* Read a 32 bit len */
1988 if (fread(&len,4,1,fp) == 0) return REDIS_RDB_LENERR;
1989 return ntohl(len);
1990 }
1991 }
1992 }
1993
1994 static robj *rdbLoadIntegerObject(FILE *fp, int enctype) {
1995 unsigned char enc[4];
1996 long long val;
1997
1998 if (enctype == REDIS_RDB_ENC_INT8) {
1999 if (fread(enc,1,1,fp) == 0) return NULL;
2000 val = (signed char)enc[0];
2001 } else if (enctype == REDIS_RDB_ENC_INT16) {
2002 uint16_t v;
2003 if (fread(enc,2,1,fp) == 0) return NULL;
2004 v = enc[0]|(enc[1]<<8);
2005 val = (int16_t)v;
2006 } else if (enctype == REDIS_RDB_ENC_INT32) {
2007 uint32_t v;
2008 if (fread(enc,4,1,fp) == 0) return NULL;
2009 v = enc[0]|(enc[1]<<8)|(enc[2]<<16)|(enc[3]<<24);
2010 val = (int32_t)v;
2011 } else {
2012 val = 0; /* anti-warning */
2013 assert(0!=0);
2014 }
2015 return createObject(REDIS_STRING,sdscatprintf(sdsempty(),"%lld",val));
2016 }
2017
2018 static robj *rdbLoadLzfStringObject(FILE*fp, int rdbver) {
2019 unsigned int len, clen;
2020 unsigned char *c = NULL;
2021 sds val = NULL;
2022
2023 if ((clen = rdbLoadLen(fp,rdbver,NULL)) == REDIS_RDB_LENERR) return NULL;
2024 if ((len = rdbLoadLen(fp,rdbver,NULL)) == REDIS_RDB_LENERR) return NULL;
2025 if ((c = zmalloc(clen)) == NULL) goto err;
2026 if ((val = sdsnewlen(NULL,len)) == NULL) goto err;
2027 if (fread(c,clen,1,fp) == 0) goto err;
2028 if (lzf_decompress(c,clen,val,len) == 0) goto err;
2029 zfree(c);
2030 return createObject(REDIS_STRING,val);
2031 err:
2032 zfree(c);
2033 sdsfree(val);
2034 return NULL;
2035 }
2036
2037 static robj *rdbLoadStringObject(FILE*fp, int rdbver) {
2038 int isencoded;
2039 uint32_t len;
2040 sds val;
2041
2042 len = rdbLoadLen(fp,rdbver,&isencoded);
2043 if (isencoded) {
2044 switch(len) {
2045 case REDIS_RDB_ENC_INT8:
2046 case REDIS_RDB_ENC_INT16:
2047 case REDIS_RDB_ENC_INT32:
2048 return tryObjectSharing(rdbLoadIntegerObject(fp,len));
2049 case REDIS_RDB_ENC_LZF:
2050 return tryObjectSharing(rdbLoadLzfStringObject(fp,rdbver));
2051 default:
2052 assert(0!=0);
2053 }
2054 }
2055
2056 if (len == REDIS_RDB_LENERR) return NULL;
2057 val = sdsnewlen(NULL,len);
2058 if (len && fread(val,len,1,fp) == 0) {
2059 sdsfree(val);
2060 return NULL;
2061 }
2062 return tryObjectSharing(createObject(REDIS_STRING,val));
2063 }
2064
2065 static int rdbLoad(char *filename) {
2066 FILE *fp;
2067 robj *keyobj = NULL;
2068 uint32_t dbid;
2069 int type, retval, rdbver;
2070 dict *d = server.db[0].dict;
2071 redisDb *db = server.db+0;
2072 char buf[1024];
2073 time_t expiretime = -1, now = time(NULL);
2074
2075 fp = fopen(filename,"r");
2076 if (!fp) return REDIS_ERR;
2077 if (fread(buf,9,1,fp) == 0) goto eoferr;
2078 buf[9] = '\0';
2079 if (memcmp(buf,"REDIS",5) != 0) {
2080 fclose(fp);
2081 redisLog(REDIS_WARNING,"Wrong signature trying to load DB from file");
2082 return REDIS_ERR;
2083 }
2084 rdbver = atoi(buf+5);
2085 if (rdbver > 1) {
2086 fclose(fp);
2087 redisLog(REDIS_WARNING,"Can't handle RDB format version %d",rdbver);
2088 return REDIS_ERR;
2089 }
2090 while(1) {
2091 robj *o;
2092
2093 /* Read type. */
2094 if ((type = rdbLoadType(fp)) == -1) goto eoferr;
2095 if (type == REDIS_EXPIRETIME) {
2096 if ((expiretime = rdbLoadTime(fp)) == -1) goto eoferr;
2097 /* We read the time so we need to read the object type again */
2098 if ((type = rdbLoadType(fp)) == -1) goto eoferr;
2099 }
2100 if (type == REDIS_EOF) break;
2101 /* Handle SELECT DB opcode as a special case */
2102 if (type == REDIS_SELECTDB) {
2103 if ((dbid = rdbLoadLen(fp,rdbver,NULL)) == REDIS_RDB_LENERR)
2104 goto eoferr;
2105 if (dbid >= (unsigned)server.dbnum) {
2106 redisLog(REDIS_WARNING,"FATAL: Data file was created with a Redis server configured to handle more than %d databases. Exiting\n", server.dbnum);
2107 exit(1);
2108 }
2109 db = server.db+dbid;
2110 d = db->dict;
2111 continue;
2112 }
2113 /* Read key */
2114 if ((keyobj = rdbLoadStringObject(fp,rdbver)) == NULL) goto eoferr;
2115
2116 if (type == REDIS_STRING) {
2117 /* Read string value */
2118 if ((o = rdbLoadStringObject(fp,rdbver)) == NULL) goto eoferr;
2119 } else if (type == REDIS_LIST || type == REDIS_SET) {
2120 /* Read list/set value */
2121 uint32_t listlen;
2122
2123 if ((listlen = rdbLoadLen(fp,rdbver,NULL)) == REDIS_RDB_LENERR)
2124 goto eoferr;
2125 o = (type == REDIS_LIST) ? createListObject() : createSetObject();
2126 /* Load every single element of the list/set */
2127 while(listlen--) {
2128 robj *ele;
2129
2130 if ((ele = rdbLoadStringObject(fp,rdbver)) == NULL) goto eoferr;
2131 if (type == REDIS_LIST) {
2132 if (!listAddNodeTail((list*)o->ptr,ele))
2133 oom("listAddNodeTail");
2134 } else {
2135 if (dictAdd((dict*)o->ptr,ele,NULL) == DICT_ERR)
2136 oom("dictAdd");
2137 }
2138 }
2139 } else {
2140 assert(0 != 0);
2141 }
2142 /* Add the new object in the hash table */
2143 retval = dictAdd(d,keyobj,o);
2144 if (retval == DICT_ERR) {
2145 redisLog(REDIS_WARNING,"Loading DB, duplicated key (%s) found! Unrecoverable error, exiting now.", keyobj->ptr);
2146 exit(1);
2147 }
2148 /* Set the expire time if needed */
2149 if (expiretime != -1) {
2150 setExpire(db,keyobj,expiretime);
2151 /* Delete this key if already expired */
2152 if (expiretime < now) deleteKey(db,keyobj);
2153 expiretime = -1;
2154 }
2155 keyobj = o = NULL;
2156 }
2157 fclose(fp);
2158 return REDIS_OK;
2159
2160 eoferr: /* unexpected end of file is handled here with a fatal exit */
2161 if (keyobj) decrRefCount(keyobj);
2162 redisLog(REDIS_WARNING,"Short read or OOM loading DB. Unrecoverable error, exiting now.");
2163 exit(1);
2164 return REDIS_ERR; /* Just to avoid warning */
2165 }
2166
2167 /*================================== Commands =============================== */
2168
2169 static void authCommand(redisClient *c) {
2170 if (!server.requirepass || !strcmp(c->argv[1]->ptr, server.requirepass)) {
2171 c->authenticated = 1;
2172 addReply(c,shared.ok);
2173 } else {
2174 c->authenticated = 0;
2175 addReply(c,shared.err);
2176 }
2177 }
2178
2179 static void pingCommand(redisClient *c) {
2180 addReply(c,shared.pong);
2181 }
2182
2183 static void echoCommand(redisClient *c) {
2184 addReplySds(c,sdscatprintf(sdsempty(),"$%d\r\n",
2185 (int)sdslen(c->argv[1]->ptr)));
2186 addReply(c,c->argv[1]);
2187 addReply(c,shared.crlf);
2188 }
2189
2190 /*=================================== Strings =============================== */
2191
2192 static void setGenericCommand(redisClient *c, int nx) {
2193 int retval;
2194
2195 retval = dictAdd(c->db->dict,c->argv[1],c->argv[2]);
2196 if (retval == DICT_ERR) {
2197 if (!nx) {
2198 dictReplace(c->db->dict,c->argv[1],c->argv[2]);
2199 incrRefCount(c->argv[2]);
2200 } else {
2201 addReply(c,shared.czero);
2202 return;
2203 }
2204 } else {
2205 incrRefCount(c->argv[1]);
2206 incrRefCount(c->argv[2]);
2207 }
2208 server.dirty++;
2209 removeExpire(c->db,c->argv[1]);
2210 addReply(c, nx ? shared.cone : shared.ok);
2211 }
2212
2213 static void setCommand(redisClient *c) {
2214 setGenericCommand(c,0);
2215 }
2216
2217 static void setnxCommand(redisClient *c) {
2218 setGenericCommand(c,1);
2219 }
2220
2221 static void getCommand(redisClient *c) {
2222 robj *o = lookupKeyRead(c->db,c->argv[1]);
2223
2224 if (o == NULL) {
2225 addReply(c,shared.nullbulk);
2226 } else {
2227 if (o->type != REDIS_STRING) {
2228 addReply(c,shared.wrongtypeerr);
2229 } else {
2230 addReplySds(c,sdscatprintf(sdsempty(),"$%d\r\n",(int)sdslen(o->ptr)));
2231 addReply(c,o);
2232 addReply(c,shared.crlf);
2233 }
2234 }
2235 }
2236
2237 static void getSetCommand(redisClient *c) {
2238 getCommand(c);
2239 if (dictAdd(c->db->dict,c->argv[1],c->argv[2]) == DICT_ERR) {
2240 dictReplace(c->db->dict,c->argv[1],c->argv[2]);
2241 } else {
2242 incrRefCount(c->argv[1]);
2243 }
2244 incrRefCount(c->argv[2]);
2245 server.dirty++;
2246 removeExpire(c->db,c->argv[1]);
2247 }
2248
2249 static void mgetCommand(redisClient *c) {
2250 int j;
2251
2252 addReplySds(c,sdscatprintf(sdsempty(),"*%d\r\n",c->argc-1));
2253 for (j = 1; j < c->argc; j++) {
2254 robj *o = lookupKeyRead(c->db,c->argv[j]);
2255 if (o == NULL) {
2256 addReply(c,shared.nullbulk);
2257 } else {
2258 if (o->type != REDIS_STRING) {
2259 addReply(c,shared.nullbulk);
2260 } else {
2261 addReplySds(c,sdscatprintf(sdsempty(),"$%d\r\n",(int)sdslen(o->ptr)));
2262 addReply(c,o);
2263 addReply(c,shared.crlf);
2264 }
2265 }
2266 }
2267 }
2268
2269 static void incrDecrCommand(redisClient *c, long long incr) {
2270 long long value;
2271 int retval;
2272 robj *o;
2273
2274 o = lookupKeyWrite(c->db,c->argv[1]);
2275 if (o == NULL) {
2276 value = 0;
2277 } else {
2278 if (o->type != REDIS_STRING) {
2279 value = 0;
2280 } else {
2281 char *eptr;
2282
2283 value = strtoll(o->ptr, &eptr, 10);
2284 }
2285 }
2286
2287 value += incr;
2288 o = createObject(REDIS_STRING,sdscatprintf(sdsempty(),"%lld",value));
2289 retval = dictAdd(c->db->dict,c->argv[1],o);
2290 if (retval == DICT_ERR) {
2291 dictReplace(c->db->dict,c->argv[1],o);
2292 removeExpire(c->db,c->argv[1]);
2293 } else {
2294 incrRefCount(c->argv[1]);
2295 }
2296 server.dirty++;
2297 addReply(c,shared.colon);
2298 addReply(c,o);
2299 addReply(c,shared.crlf);
2300 }
2301
2302 static void incrCommand(redisClient *c) {
2303 incrDecrCommand(c,1);
2304 }
2305
2306 static void decrCommand(redisClient *c) {
2307 incrDecrCommand(c,-1);
2308 }
2309
2310 static void incrbyCommand(redisClient *c) {
2311 long long incr = strtoll(c->argv[2]->ptr, NULL, 10);
2312 incrDecrCommand(c,incr);
2313 }
2314
2315 static void decrbyCommand(redisClient *c) {
2316 long long incr = strtoll(c->argv[2]->ptr, NULL, 10);
2317 incrDecrCommand(c,-incr);
2318 }
2319
2320 /* ========================= Type agnostic commands ========================= */
2321
2322 static void delCommand(redisClient *c) {
2323 int deleted = 0, j;
2324
2325 for (j = 1; j < c->argc; j++) {
2326 if (deleteKey(c->db,c->argv[j])) {
2327 server.dirty++;
2328 deleted++;
2329 }
2330 }
2331 switch(deleted) {
2332 case 0:
2333 addReply(c,shared.czero);
2334 break;
2335 case 1:
2336 addReply(c,shared.cone);
2337 break;
2338 default:
2339 addReplySds(c,sdscatprintf(sdsempty(),":%d\r\n",deleted));
2340 break;
2341 }
2342 }
2343
2344 static void existsCommand(redisClient *c) {
2345 addReply(c,lookupKeyRead(c->db,c->argv[1]) ? shared.cone : shared.czero);
2346 }
2347
2348 static void selectCommand(redisClient *c) {
2349 int id = atoi(c->argv[1]->ptr);
2350
2351 if (selectDb(c,id) == REDIS_ERR) {
2352 addReplySds(c,sdsnew("-ERR invalid DB index\r\n"));
2353 } else {
2354 addReply(c,shared.ok);
2355 }
2356 }
2357
2358 static void randomkeyCommand(redisClient *c) {
2359 dictEntry *de;
2360
2361 while(1) {
2362 de = dictGetRandomKey(c->db->dict);
2363 if (!de || expireIfNeeded(c->db,dictGetEntryKey(de)) == 0) break;
2364 }
2365 if (de == NULL) {
2366 addReply(c,shared.plus);
2367 addReply(c,shared.crlf);
2368 } else {
2369 addReply(c,shared.plus);
2370 addReply(c,dictGetEntryKey(de));
2371 addReply(c,shared.crlf);
2372 }
2373 }
2374
2375 static void keysCommand(redisClient *c) {
2376 dictIterator *di;
2377 dictEntry *de;
2378 sds pattern = c->argv[1]->ptr;
2379 int plen = sdslen(pattern);
2380 int numkeys = 0, keyslen = 0;
2381 robj *lenobj = createObject(REDIS_STRING,NULL);
2382
2383 di = dictGetIterator(c->db->dict);
2384 if (!di) oom("dictGetIterator");
2385 addReply(c,lenobj);
2386 decrRefCount(lenobj);
2387 while((de = dictNext(di)) != NULL) {
2388 robj *keyobj = dictGetEntryKey(de);
2389
2390 sds key = keyobj->ptr;
2391 if ((pattern[0] == '*' && pattern[1] == '\0') ||
2392 stringmatchlen(pattern,plen,key,sdslen(key),0)) {
2393 if (expireIfNeeded(c->db,keyobj) == 0) {
2394 if (numkeys != 0)
2395 addReply(c,shared.space);
2396 addReply(c,keyobj);
2397 numkeys++;
2398 keyslen += sdslen(key);
2399 }
2400 }
2401 }
2402 dictReleaseIterator(di);
2403 lenobj->ptr = sdscatprintf(sdsempty(),"$%lu\r\n",keyslen+(numkeys ? (numkeys-1) : 0));
2404 addReply(c,shared.crlf);
2405 }
2406
2407 static void dbsizeCommand(redisClient *c) {
2408 addReplySds(c,
2409 sdscatprintf(sdsempty(),":%lu\r\n",dictSize(c->db->dict)));
2410 }
2411
2412 static void lastsaveCommand(redisClient *c) {
2413 addReplySds(c,
2414 sdscatprintf(sdsempty(),":%lu\r\n",server.lastsave));
2415 }
2416
2417 static void typeCommand(redisClient *c) {
2418 robj *o;
2419 char *type;
2420
2421 o = lookupKeyRead(c->db,c->argv[1]);
2422 if (o == NULL) {
2423 type = "+none";
2424 } else {
2425 switch(o->type) {
2426 case REDIS_STRING: type = "+string"; break;
2427 case REDIS_LIST: type = "+list"; break;
2428 case REDIS_SET: type = "+set"; break;
2429 default: type = "unknown"; break;
2430 }
2431 }
2432 addReplySds(c,sdsnew(type));
2433 addReply(c,shared.crlf);
2434 }
2435
2436 static void saveCommand(redisClient *c) {
2437 if (server.bgsaveinprogress) {
2438 addReplySds(c,sdsnew("-ERR background save in progress\r\n"));
2439 return;
2440 }
2441 if (rdbSave(server.dbfilename) == REDIS_OK) {
2442 addReply(c,shared.ok);
2443 } else {
2444 addReply(c,shared.err);
2445 }
2446 }
2447
2448 static void bgsaveCommand(redisClient *c) {
2449 if (server.bgsaveinprogress) {
2450 addReplySds(c,sdsnew("-ERR background save already in progress\r\n"));
2451 return;
2452 }
2453 if (rdbSaveBackground(server.dbfilename) == REDIS_OK) {
2454 addReply(c,shared.ok);
2455 } else {
2456 addReply(c,shared.err);
2457 }
2458 }
2459
2460 static void shutdownCommand(redisClient *c) {
2461 redisLog(REDIS_WARNING,"User requested shutdown, saving DB...");
2462 /* XXX: TODO kill the child if there is a bgsave in progress */
2463 if (rdbSave(server.dbfilename) == REDIS_OK) {
2464 if (server.daemonize) {
2465 unlink(server.pidfile);
2466 }
2467 redisLog(REDIS_WARNING,"%zu bytes used at exit",zmalloc_used_memory());
2468 redisLog(REDIS_WARNING,"Server exit now, bye bye...");
2469 exit(1);
2470 } else {
2471 redisLog(REDIS_WARNING,"Error trying to save the DB, can't exit");
2472 addReplySds(c,sdsnew("-ERR can't quit, problems saving the DB\r\n"));
2473 }
2474 }
2475
2476 static void renameGenericCommand(redisClient *c, int nx) {
2477 robj *o;
2478
2479 /* To use the same key as src and dst is probably an error */
2480 if (sdscmp(c->argv[1]->ptr,c->argv[2]->ptr) == 0) {
2481 addReply(c,shared.sameobjecterr);
2482 return;
2483 }
2484
2485 o = lookupKeyWrite(c->db,c->argv[1]);
2486 if (o == NULL) {
2487 addReply(c,shared.nokeyerr);
2488 return;
2489 }
2490 incrRefCount(o);
2491 deleteIfVolatile(c->db,c->argv[2]);
2492 if (dictAdd(c->db->dict,c->argv[2],o) == DICT_ERR) {
2493 if (nx) {
2494 decrRefCount(o);
2495 addReply(c,shared.czero);
2496 return;
2497 }
2498 dictReplace(c->db->dict,c->argv[2],o);
2499 } else {
2500 incrRefCount(c->argv[2]);
2501 }
2502 deleteKey(c->db,c->argv[1]);
2503 server.dirty++;
2504 addReply(c,nx ? shared.cone : shared.ok);
2505 }
2506
2507 static void renameCommand(redisClient *c) {
2508 renameGenericCommand(c,0);
2509 }
2510
2511 static void renamenxCommand(redisClient *c) {
2512 renameGenericCommand(c,1);
2513 }
2514
2515 static void moveCommand(redisClient *c) {
2516 robj *o;
2517 redisDb *src, *dst;
2518 int srcid;
2519
2520 /* Obtain source and target DB pointers */
2521 src = c->db;
2522 srcid = c->db->id;
2523 if (selectDb(c,atoi(c->argv[2]->ptr)) == REDIS_ERR) {
2524 addReply(c,shared.outofrangeerr);
2525 return;
2526 }
2527 dst = c->db;
2528 selectDb(c,srcid); /* Back to the source DB */
2529
2530 /* If the user is moving using as target the same
2531 * DB as the source DB it is probably an error. */
2532 if (src == dst) {
2533 addReply(c,shared.sameobjecterr);
2534 return;
2535 }
2536
2537 /* Check if the element exists and get a reference */
2538 o = lookupKeyWrite(c->db,c->argv[1]);
2539 if (!o) {
2540 addReply(c,shared.czero);
2541 return;
2542 }
2543
2544 /* Try to add the element to the target DB */
2545 deleteIfVolatile(dst,c->argv[1]);
2546 if (dictAdd(dst->dict,c->argv[1],o) == DICT_ERR) {
2547 addReply(c,shared.czero);
2548 return;
2549 }
2550 incrRefCount(c->argv[1]);
2551 incrRefCount(o);
2552
2553 /* OK! key moved, free the entry in the source DB */
2554 deleteKey(src,c->argv[1]);
2555 server.dirty++;
2556 addReply(c,shared.cone);
2557 }
2558
2559 /* =================================== Lists ================================ */
2560 static void pushGenericCommand(redisClient *c, int where) {
2561 robj *lobj;
2562 list *list;
2563
2564 lobj = lookupKeyWrite(c->db,c->argv[1]);
2565 if (lobj == NULL) {
2566 lobj = createListObject();
2567 list = lobj->ptr;
2568 if (where == REDIS_HEAD) {
2569 if (!listAddNodeHead(list,c->argv[2])) oom("listAddNodeHead");
2570 } else {
2571 if (!listAddNodeTail(list,c->argv[2])) oom("listAddNodeTail");
2572 }
2573 dictAdd(c->db->dict,c->argv[1],lobj);
2574 incrRefCount(c->argv[1]);
2575 incrRefCount(c->argv[2]);
2576 } else {
2577 if (lobj->type != REDIS_LIST) {
2578 addReply(c,shared.wrongtypeerr);
2579 return;
2580 }
2581 list = lobj->ptr;
2582 if (where == REDIS_HEAD) {
2583 if (!listAddNodeHead(list,c->argv[2])) oom("listAddNodeHead");
2584 } else {
2585 if (!listAddNodeTail(list,c->argv[2])) oom("listAddNodeTail");
2586 }
2587 incrRefCount(c->argv[2]);
2588 }
2589 server.dirty++;
2590 addReply(c,shared.ok);
2591 }
2592
2593 static void lpushCommand(redisClient *c) {
2594 pushGenericCommand(c,REDIS_HEAD);
2595 }
2596
2597 static void rpushCommand(redisClient *c) {
2598 pushGenericCommand(c,REDIS_TAIL);
2599 }
2600
2601 static void llenCommand(redisClient *c) {
2602 robj *o;
2603 list *l;
2604
2605 o = lookupKeyRead(c->db,c->argv[1]);
2606 if (o == NULL) {
2607 addReply(c,shared.czero);
2608 return;
2609 } else {
2610 if (o->type != REDIS_LIST) {
2611 addReply(c,shared.wrongtypeerr);
2612 } else {
2613 l = o->ptr;
2614 addReplySds(c,sdscatprintf(sdsempty(),":%d\r\n",listLength(l)));
2615 }
2616 }
2617 }
2618
2619 static void lindexCommand(redisClient *c) {
2620 robj *o;
2621 int index = atoi(c->argv[2]->ptr);
2622
2623 o = lookupKeyRead(c->db,c->argv[1]);
2624 if (o == NULL) {
2625 addReply(c,shared.nullbulk);
2626 } else {
2627 if (o->type != REDIS_LIST) {
2628 addReply(c,shared.wrongtypeerr);
2629 } else {
2630 list *list = o->ptr;
2631 listNode *ln;
2632
2633 ln = listIndex(list, index);
2634 if (ln == NULL) {
2635 addReply(c,shared.nullbulk);
2636 } else {
2637 robj *ele = listNodeValue(ln);
2638 addReplySds(c,sdscatprintf(sdsempty(),"$%d\r\n",(int)sdslen(ele->ptr)));
2639 addReply(c,ele);
2640 addReply(c,shared.crlf);
2641 }
2642 }
2643 }
2644 }
2645
2646 static void lsetCommand(redisClient *c) {
2647 robj *o;
2648 int index = atoi(c->argv[2]->ptr);
2649
2650 o = lookupKeyWrite(c->db,c->argv[1]);
2651 if (o == NULL) {
2652 addReply(c,shared.nokeyerr);
2653 } else {
2654 if (o->type != REDIS_LIST) {
2655 addReply(c,shared.wrongtypeerr);
2656 } else {
2657 list *list = o->ptr;
2658 listNode *ln;
2659
2660 ln = listIndex(list, index);
2661 if (ln == NULL) {
2662 addReply(c,shared.outofrangeerr);
2663 } else {
2664 robj *ele = listNodeValue(ln);
2665
2666 decrRefCount(ele);
2667 listNodeValue(ln) = c->argv[3];
2668 incrRefCount(c->argv[3]);
2669 addReply(c,shared.ok);
2670 server.dirty++;
2671 }
2672 }
2673 }
2674 }
2675
2676 static void popGenericCommand(redisClient *c, int where) {
2677 robj *o;
2678
2679 o = lookupKeyWrite(c->db,c->argv[1]);
2680 if (o == NULL) {
2681 addReply(c,shared.nullbulk);
2682 } else {
2683 if (o->type != REDIS_LIST) {
2684 addReply(c,shared.wrongtypeerr);
2685 } else {
2686 list *list = o->ptr;
2687 listNode *ln;
2688
2689 if (where == REDIS_HEAD)
2690 ln = listFirst(list);
2691 else
2692 ln = listLast(list);
2693
2694 if (ln == NULL) {
2695 addReply(c,shared.nullbulk);
2696 } else {
2697 robj *ele = listNodeValue(ln);
2698 addReplySds(c,sdscatprintf(sdsempty(),"$%d\r\n",(int)sdslen(ele->ptr)));
2699 addReply(c,ele);
2700 addReply(c,shared.crlf);
2701 listDelNode(list,ln);
2702 server.dirty++;
2703 }
2704 }
2705 }
2706 }
2707
2708 static void lpopCommand(redisClient *c) {
2709 popGenericCommand(c,REDIS_HEAD);
2710 }
2711
2712 static void rpopCommand(redisClient *c) {
2713 popGenericCommand(c,REDIS_TAIL);
2714 }
2715
2716 static void lrangeCommand(redisClient *c) {
2717 robj *o;
2718 int start = atoi(c->argv[2]->ptr);
2719 int end = atoi(c->argv[3]->ptr);
2720
2721 o = lookupKeyRead(c->db,c->argv[1]);
2722 if (o == NULL) {
2723 addReply(c,shared.nullmultibulk);
2724 } else {
2725 if (o->type != REDIS_LIST) {
2726 addReply(c,shared.wrongtypeerr);
2727 } else {
2728 list *list = o->ptr;
2729 listNode *ln;
2730 int llen = listLength(list);
2731 int rangelen, j;
2732 robj *ele;
2733
2734 /* convert negative indexes */
2735 if (start < 0) start = llen+start;
2736 if (end < 0) end = llen+end;
2737 if (start < 0) start = 0;
2738 if (end < 0) end = 0;
2739
2740 /* indexes sanity checks */
2741 if (start > end || start >= llen) {
2742 /* Out of range start or start > end result in empty list */
2743 addReply(c,shared.emptymultibulk);
2744 return;
2745 }
2746 if (end >= llen) end = llen-1;
2747 rangelen = (end-start)+1;
2748
2749 /* Return the result in form of a multi-bulk reply */
2750 ln = listIndex(list, start);
2751 addReplySds(c,sdscatprintf(sdsempty(),"*%d\r\n",rangelen));
2752 for (j = 0; j < rangelen; j++) {
2753 ele = listNodeValue(ln);
2754 addReplySds(c,sdscatprintf(sdsempty(),"$%d\r\n",(int)sdslen(ele->ptr)));
2755 addReply(c,ele);
2756 addReply(c,shared.crlf);
2757 ln = ln->next;
2758 }
2759 }
2760 }
2761 }
2762
2763 static void ltrimCommand(redisClient *c) {
2764 robj *o;
2765 int start = atoi(c->argv[2]->ptr);
2766 int end = atoi(c->argv[3]->ptr);
2767
2768 o = lookupKeyWrite(c->db,c->argv[1]);
2769 if (o == NULL) {
2770 addReply(c,shared.nokeyerr);
2771 } else {
2772 if (o->type != REDIS_LIST) {
2773 addReply(c,shared.wrongtypeerr);
2774 } else {
2775 list *list = o->ptr;
2776 listNode *ln;
2777 int llen = listLength(list);
2778 int j, ltrim, rtrim;
2779
2780 /* convert negative indexes */
2781 if (start < 0) start = llen+start;
2782 if (end < 0) end = llen+end;
2783 if (start < 0) start = 0;
2784 if (end < 0) end = 0;
2785
2786 /* indexes sanity checks */
2787 if (start > end || start >= llen) {
2788 /* Out of range start or start > end result in empty list */
2789 ltrim = llen;
2790 rtrim = 0;
2791 } else {
2792 if (end >= llen) end = llen-1;
2793 ltrim = start;
2794 rtrim = llen-end-1;
2795 }
2796
2797 /* Remove list elements to perform the trim */
2798 for (j = 0; j < ltrim; j++) {
2799 ln = listFirst(list);
2800 listDelNode(list,ln);
2801 }
2802 for (j = 0; j < rtrim; j++) {
2803 ln = listLast(list);
2804 listDelNode(list,ln);
2805 }
2806 addReply(c,shared.ok);
2807 server.dirty++;
2808 }
2809 }
2810 }
2811
2812 static void lremCommand(redisClient *c) {
2813 robj *o;
2814
2815 o = lookupKeyWrite(c->db,c->argv[1]);
2816 if (o == NULL) {
2817 addReply(c,shared.nokeyerr);
2818 } else {
2819 if (o->type != REDIS_LIST) {
2820 addReply(c,shared.wrongtypeerr);
2821 } else {
2822 list *list = o->ptr;
2823 listNode *ln, *next;
2824 int toremove = atoi(c->argv[2]->ptr);
2825 int removed = 0;
2826 int fromtail = 0;
2827
2828 if (toremove < 0) {
2829 toremove = -toremove;
2830 fromtail = 1;
2831 }
2832 ln = fromtail ? list->tail : list->head;
2833 while (ln) {
2834 robj *ele = listNodeValue(ln);
2835
2836 next = fromtail ? ln->prev : ln->next;
2837 if (sdscmp(ele->ptr,c->argv[3]->ptr) == 0) {
2838 listDelNode(list,ln);
2839 server.dirty++;
2840 removed++;
2841 if (toremove && removed == toremove) break;
2842 }
2843 ln = next;
2844 }
2845 addReplySds(c,sdscatprintf(sdsempty(),":%d\r\n",removed));
2846 }
2847 }
2848 }
2849
2850 /* ==================================== Sets ================================ */
2851
2852 static void saddCommand(redisClient *c) {
2853 robj *set;
2854
2855 set = lookupKeyWrite(c->db,c->argv[1]);
2856 if (set == NULL) {
2857 set = createSetObject();
2858 dictAdd(c->db->dict,c->argv[1],set);
2859 incrRefCount(c->argv[1]);
2860 } else {
2861 if (set->type != REDIS_SET) {
2862 addReply(c,shared.wrongtypeerr);
2863 return;
2864 }
2865 }
2866 if (dictAdd(set->ptr,c->argv[2],NULL) == DICT_OK) {
2867 incrRefCount(c->argv[2]);
2868 server.dirty++;
2869 addReply(c,shared.cone);
2870 } else {
2871 addReply(c,shared.czero);
2872 }
2873 }
2874
2875 static void sremCommand(redisClient *c) {
2876 robj *set;
2877
2878 set = lookupKeyWrite(c->db,c->argv[1]);
2879 if (set == NULL) {
2880 addReply(c,shared.czero);
2881 } else {
2882 if (set->type != REDIS_SET) {
2883 addReply(c,shared.wrongtypeerr);
2884 return;
2885 }
2886 if (dictDelete(set->ptr,c->argv[2]) == DICT_OK) {
2887 server.dirty++;
2888 addReply(c,shared.cone);
2889 } else {
2890 addReply(c,shared.czero);
2891 }
2892 }
2893 }
2894
2895 static void smoveCommand(redisClient *c) {
2896 robj *srcset, *dstset;
2897
2898 srcset = lookupKeyWrite(c->db,c->argv[1]);
2899 dstset = lookupKeyWrite(c->db,c->argv[2]);
2900
2901 /* If the source key does not exist return 0, if it's of the wrong type
2902 * raise an error */
2903 if (srcset == NULL || srcset->type != REDIS_SET) {
2904 addReply(c, srcset ? shared.wrongtypeerr : shared.czero);
2905 return;
2906 }
2907 /* Error if the destination key is not a set as well */
2908 if (dstset && dstset->type != REDIS_SET) {
2909 addReply(c,shared.wrongtypeerr);
2910 return;
2911 }
2912 /* Remove the element from the source set */
2913 if (dictDelete(srcset->ptr,c->argv[3]) == DICT_ERR) {
2914 /* Key not found in the src set! return zero */
2915 addReply(c,shared.czero);
2916 return;
2917 }
2918 server.dirty++;
2919 /* Add the element to the destination set */
2920 if (!dstset) {
2921 dstset = createSetObject();
2922 dictAdd(c->db->dict,c->argv[2],dstset);
2923 incrRefCount(c->argv[2]);
2924 }
2925 if (dictAdd(dstset->ptr,c->argv[3],NULL) == DICT_OK)
2926 incrRefCount(c->argv[3]);
2927 addReply(c,shared.cone);
2928 }
2929
2930 static void sismemberCommand(redisClient *c) {
2931 robj *set;
2932
2933 set = lookupKeyRead(c->db,c->argv[1]);
2934 if (set == NULL) {
2935 addReply(c,shared.czero);
2936 } else {
2937 if (set->type != REDIS_SET) {
2938 addReply(c,shared.wrongtypeerr);
2939 return;
2940 }
2941 if (dictFind(set->ptr,c->argv[2]))
2942 addReply(c,shared.cone);
2943 else
2944 addReply(c,shared.czero);
2945 }
2946 }
2947
2948 static void scardCommand(redisClient *c) {
2949 robj *o;
2950 dict *s;
2951
2952 o = lookupKeyRead(c->db,c->argv[1]);
2953 if (o == NULL) {
2954 addReply(c,shared.czero);
2955 return;
2956 } else {
2957 if (o->type != REDIS_SET) {
2958 addReply(c,shared.wrongtypeerr);
2959 } else {
2960 s = o->ptr;
2961 addReplySds(c,sdscatprintf(sdsempty(),":%d\r\n",
2962 dictSize(s)));
2963 }
2964 }
2965 }
2966
2967 static int qsortCompareSetsByCardinality(const void *s1, const void *s2) {
2968 dict **d1 = (void*) s1, **d2 = (void*) s2;
2969
2970 return dictSize(*d1)-dictSize(*d2);
2971 }
2972
2973 static void sinterGenericCommand(redisClient *c, robj **setskeys, int setsnum, robj *dstkey) {
2974 dict **dv = zmalloc(sizeof(dict*)*setsnum);
2975 dictIterator *di;
2976 dictEntry *de;
2977 robj *lenobj = NULL, *dstset = NULL;
2978 int j, cardinality = 0;
2979
2980 if (!dv) oom("sinterGenericCommand");
2981 for (j = 0; j < setsnum; j++) {
2982 robj *setobj;
2983
2984 setobj = dstkey ?
2985 lookupKeyWrite(c->db,setskeys[j]) :
2986 lookupKeyRead(c->db,setskeys[j]);
2987 if (!setobj) {
2988 zfree(dv);
2989 if (dstkey) {
2990 deleteKey(c->db,dstkey);
2991 addReply(c,shared.ok);
2992 } else {
2993 addReply(c,shared.nullmultibulk);
2994 }
2995 return;
2996 }
2997 if (setobj->type != REDIS_SET) {
2998 zfree(dv);
2999 addReply(c,shared.wrongtypeerr);
3000 return;
3001 }
3002 dv[j] = setobj->ptr;
3003 }
3004 /* Sort sets from the smallest to largest, this will improve our
3005 * algorithm's performace */
3006 qsort(dv,setsnum,sizeof(dict*),qsortCompareSetsByCardinality);
3007
3008 /* The first thing we should output is the total number of elements...
3009 * since this is a multi-bulk write, but at this stage we don't know
3010 * the intersection set size, so we use a trick, append an empty object
3011 * to the output list and save the pointer to later modify it with the
3012 * right length */
3013 if (!dstkey) {
3014 lenobj = createObject(REDIS_STRING,NULL);
3015 addReply(c,lenobj);
3016 decrRefCount(lenobj);
3017 } else {
3018 /* If we have a target key where to store the resulting set
3019 * create this key with an empty set inside */
3020 dstset = createSetObject();
3021 }
3022
3023 /* Iterate all the elements of the first (smallest) set, and test
3024 * the element against all the other sets, if at least one set does
3025 * not include the element it is discarded */
3026 di = dictGetIterator(dv[0]);
3027 if (!di) oom("dictGetIterator");
3028
3029 while((de = dictNext(di)) != NULL) {
3030 robj *ele;
3031
3032 for (j = 1; j < setsnum; j++)
3033 if (dictFind(dv[j],dictGetEntryKey(de)) == NULL) break;
3034 if (j != setsnum)
3035 continue; /* at least one set does not contain the member */
3036 ele = dictGetEntryKey(de);
3037 if (!dstkey) {
3038 addReplySds(c,sdscatprintf(sdsempty(),"$%d\r\n",sdslen(ele->ptr)));
3039 addReply(c,ele);
3040 addReply(c,shared.crlf);
3041 cardinality++;
3042 } else {
3043 dictAdd(dstset->ptr,ele,NULL);
3044 incrRefCount(ele);
3045 }
3046 }
3047 dictReleaseIterator(di);
3048
3049 if (dstkey) {
3050 /* Store the resulting set into the target */
3051 deleteKey(c->db,dstkey);
3052 dictAdd(c->db->dict,dstkey,dstset);
3053 incrRefCount(dstkey);
3054 }
3055
3056 if (!dstkey) {
3057 lenobj->ptr = sdscatprintf(sdsempty(),"*%d\r\n",cardinality);
3058 } else {
3059 addReplySds(c,sdscatprintf(sdsempty(),":%d\r\n",
3060 dictSize((dict*)dstset->ptr)));
3061 server.dirty++;
3062 }
3063 zfree(dv);
3064 }
3065
3066 static void sinterCommand(redisClient *c) {
3067 sinterGenericCommand(c,c->argv+1,c->argc-1,NULL);
3068 }
3069
3070 static void sinterstoreCommand(redisClient *c) {
3071 sinterGenericCommand(c,c->argv+2,c->argc-2,c->argv[1]);
3072 }
3073
3074 #define REDIS_OP_UNION 0
3075 #define REDIS_OP_DIFF 1
3076
3077 static void sunionDiffGenericCommand(redisClient *c, robj **setskeys, int setsnum, robj *dstkey, int op) {
3078 dict **dv = zmalloc(sizeof(dict*)*setsnum);
3079 dictIterator *di;
3080 dictEntry *de;
3081 robj *dstset = NULL;
3082 int j, cardinality = 0;
3083
3084 if (!dv) oom("sunionDiffGenericCommand");
3085 for (j = 0; j < setsnum; j++) {
3086 robj *setobj;
3087
3088 setobj = dstkey ?
3089 lookupKeyWrite(c->db,setskeys[j]) :
3090 lookupKeyRead(c->db,setskeys[j]);
3091 if (!setobj) {
3092 dv[j] = NULL;
3093 continue;
3094 }
3095 if (setobj->type != REDIS_SET) {
3096 zfree(dv);
3097 addReply(c,shared.wrongtypeerr);
3098 return;
3099 }
3100 dv[j] = setobj->ptr;
3101 }
3102
3103 /* We need a temp set object to store our union. If the dstkey
3104 * is not NULL (that is, we are inside an SUNIONSTORE operation) then
3105 * this set object will be the resulting object to set into the target key*/
3106 dstset = createSetObject();
3107
3108 /* Iterate all the elements of all the sets, add every element a single
3109 * time to the result set */
3110 for (j = 0; j < setsnum; j++) {
3111 if (op == REDIS_OP_DIFF && j == 0 && !dv[j]) break; /* result set is empty */
3112 if (!dv[j]) continue; /* non existing keys are like empty sets */
3113
3114 di = dictGetIterator(dv[j]);
3115 if (!di) oom("dictGetIterator");
3116
3117 while((de = dictNext(di)) != NULL) {
3118 robj *ele;
3119
3120 /* dictAdd will not add the same element multiple times */
3121 ele = dictGetEntryKey(de);
3122 if (op == REDIS_OP_UNION || j == 0) {
3123 if (dictAdd(dstset->ptr,ele,NULL) == DICT_OK) {
3124 incrRefCount(ele);
3125 cardinality++;
3126 }
3127 } else if (op == REDIS_OP_DIFF) {
3128 if (dictDelete(dstset->ptr,ele) == DICT_OK) {
3129 cardinality--;
3130 }
3131 }
3132 }
3133 dictReleaseIterator(di);
3134
3135 if (op == REDIS_OP_DIFF && cardinality == 0) break; /* result set is empty */
3136 }
3137
3138 /* Output the content of the resulting set, if not in STORE mode */
3139 if (!dstkey) {
3140 addReplySds(c,sdscatprintf(sdsempty(),"*%d\r\n",cardinality));
3141 di = dictGetIterator(dstset->ptr);
3142 if (!di) oom("dictGetIterator");
3143 while((de = dictNext(di)) != NULL) {
3144 robj *ele;
3145
3146 ele = dictGetEntryKey(de);
3147 addReplySds(c,sdscatprintf(sdsempty(),
3148 "$%d\r\n",sdslen(ele->ptr)));
3149 addReply(c,ele);
3150 addReply(c,shared.crlf);
3151 }
3152 dictReleaseIterator(di);
3153 } else {
3154 /* If we have a target key where to store the resulting set
3155 * create this key with the result set inside */
3156 deleteKey(c->db,dstkey);
3157 dictAdd(c->db->dict,dstkey,dstset);
3158 incrRefCount(dstkey);
3159 }
3160
3161 /* Cleanup */
3162 if (!dstkey) {
3163 decrRefCount(dstset);
3164 } else {
3165 addReplySds(c,sdscatprintf(sdsempty(),":%d\r\n",
3166 dictSize((dict*)dstset->ptr)));
3167 server.dirty++;
3168 }
3169 zfree(dv);
3170 }
3171
3172 static void sunionCommand(redisClient *c) {
3173 sunionDiffGenericCommand(c,c->argv+1,c->argc-1,NULL,REDIS_OP_UNION);
3174 }
3175
3176 static void sunionstoreCommand(redisClient *c) {
3177 sunionDiffGenericCommand(c,c->argv+2,c->argc-2,c->argv[1],REDIS_OP_UNION);
3178 }
3179
3180 static void sdiffCommand(redisClient *c) {
3181 sunionDiffGenericCommand(c,c->argv+1,c->argc-1,NULL,REDIS_OP_DIFF);
3182 }
3183
3184 static void sdiffstoreCommand(redisClient *c) {
3185 sunionDiffGenericCommand(c,c->argv+2,c->argc-2,c->argv[1],REDIS_OP_DIFF);
3186 }
3187
3188 static void flushdbCommand(redisClient *c) {
3189 server.dirty += dictSize(c->db->dict);
3190 dictEmpty(c->db->dict);
3191 dictEmpty(c->db->expires);
3192 addReply(c,shared.ok);
3193 }
3194
3195 static void flushallCommand(redisClient *c) {
3196 server.dirty += emptyDb();
3197 addReply(c,shared.ok);
3198 rdbSave(server.dbfilename);
3199 server.dirty++;
3200 }
3201
3202 redisSortOperation *createSortOperation(int type, robj *pattern) {
3203 redisSortOperation *so = zmalloc(sizeof(*so));
3204 if (!so) oom("createSortOperation");
3205 so->type = type;
3206 so->pattern = pattern;
3207 return so;
3208 }
3209
3210 /* Return the value associated to the key with a name obtained
3211 * substituting the first occurence of '*' in 'pattern' with 'subst' */
3212 robj *lookupKeyByPattern(redisDb *db, robj *pattern, robj *subst) {
3213 char *p;
3214 sds spat, ssub;
3215 robj keyobj;
3216 int prefixlen, sublen, postfixlen;
3217 /* Expoit the internal sds representation to create a sds string allocated on the stack in order to make this function faster */
3218 struct {
3219 long len;
3220 long free;
3221 char buf[REDIS_SORTKEY_MAX+1];
3222 } keyname;
3223
3224 spat = pattern->ptr;
3225 ssub = subst->ptr;
3226 if (sdslen(spat)+sdslen(ssub)-1 > REDIS_SORTKEY_MAX) return NULL;
3227 p = strchr(spat,'*');
3228 if (!p) return NULL;
3229
3230 prefixlen = p-spat;
3231 sublen = sdslen(ssub);
3232 postfixlen = sdslen(spat)-(prefixlen+1);
3233 memcpy(keyname.buf,spat,prefixlen);
3234 memcpy(keyname.buf+prefixlen,ssub,sublen);
3235 memcpy(keyname.buf+prefixlen+sublen,p+1,postfixlen);
3236 keyname.buf[prefixlen+sublen+postfixlen] = '\0';
3237 keyname.len = prefixlen+sublen+postfixlen;
3238
3239 keyobj.refcount = 1;
3240 keyobj.type = REDIS_STRING;
3241 keyobj.ptr = ((char*)&keyname)+(sizeof(long)*2);
3242
3243 /* printf("lookup '%s' => %p\n", keyname.buf,de); */
3244 return lookupKeyRead(db,&keyobj);
3245 }
3246
3247 /* sortCompare() is used by qsort in sortCommand(). Given that qsort_r with
3248 * the additional parameter is not standard but a BSD-specific we have to
3249 * pass sorting parameters via the global 'server' structure */
3250 static int sortCompare(const void *s1, const void *s2) {
3251 const redisSortObject *so1 = s1, *so2 = s2;
3252 int cmp;
3253
3254 if (!server.sort_alpha) {
3255 /* Numeric sorting. Here it's trivial as we precomputed scores */
3256 if (so1->u.score > so2->u.score) {
3257 cmp = 1;
3258 } else if (so1->u.score < so2->u.score) {
3259 cmp = -1;
3260 } else {
3261 cmp = 0;
3262 }
3263 } else {
3264 /* Alphanumeric sorting */
3265 if (server.sort_bypattern) {
3266 if (!so1->u.cmpobj || !so2->u.cmpobj) {
3267 /* At least one compare object is NULL */
3268 if (so1->u.cmpobj == so2->u.cmpobj)
3269 cmp = 0;
3270 else if (so1->u.cmpobj == NULL)
3271 cmp = -1;
3272 else
3273 cmp = 1;
3274 } else {
3275 /* We have both the objects, use strcoll */
3276 cmp = strcoll(so1->u.cmpobj->ptr,so2->u.cmpobj->ptr);
3277 }
3278 } else {
3279 /* Compare elements directly */
3280 cmp = strcoll(so1->obj->ptr,so2->obj->ptr);
3281 }
3282 }
3283 return server.sort_desc ? -cmp : cmp;
3284 }
3285
3286 /* The SORT command is the most complex command in Redis. Warning: this code
3287 * is optimized for speed and a bit less for readability */
3288 static void sortCommand(redisClient *c) {
3289 list *operations;
3290 int outputlen = 0;
3291 int desc = 0, alpha = 0;
3292 int limit_start = 0, limit_count = -1, start, end;
3293 int j, dontsort = 0, vectorlen;
3294 int getop = 0; /* GET operation counter */
3295 robj *sortval, *sortby = NULL;
3296 redisSortObject *vector; /* Resulting vector to sort */
3297
3298 /* Lookup the key to sort. It must be of the right types */
3299 sortval = lookupKeyRead(c->db,c->argv[1]);
3300 if (sortval == NULL) {
3301 addReply(c,shared.nokeyerr);
3302 return;
3303 }
3304 if (sortval->type != REDIS_SET && sortval->type != REDIS_LIST) {
3305 addReply(c,shared.wrongtypeerr);
3306 return;
3307 }
3308
3309 /* Create a list of operations to perform for every sorted element.
3310 * Operations can be GET/DEL/INCR/DECR */
3311 operations = listCreate();
3312 listSetFreeMethod(operations,zfree);
3313 j = 2;
3314
3315 /* Now we need to protect sortval incrementing its count, in the future
3316 * SORT may have options able to overwrite/delete keys during the sorting
3317 * and the sorted key itself may get destroied */
3318 incrRefCount(sortval);
3319
3320 /* The SORT command has an SQL-alike syntax, parse it */
3321 while(j < c->argc) {
3322 int leftargs = c->argc-j-1;
3323 if (!strcasecmp(c->argv[j]->ptr,"asc")) {
3324 desc = 0;
3325 } else if (!strcasecmp(c->argv[j]->ptr,"desc")) {
3326 desc = 1;
3327 } else if (!strcasecmp(c->argv[j]->ptr,"alpha")) {
3328 alpha = 1;
3329 } else if (!strcasecmp(c->argv[j]->ptr,"limit") && leftargs >= 2) {
3330 limit_start = atoi(c->argv[j+1]->ptr);
3331 limit_count = atoi(c->argv[j+2]->ptr);
3332 j+=2;
3333 } else if (!strcasecmp(c->argv[j]->ptr,"by") && leftargs >= 1) {
3334 sortby = c->argv[j+1];
3335 /* If the BY pattern does not contain '*', i.e. it is constant,
3336 * we don't need to sort nor to lookup the weight keys. */
3337 if (strchr(c->argv[j+1]->ptr,'*') == NULL) dontsort = 1;
3338 j++;
3339 } else if (!strcasecmp(c->argv[j]->ptr,"get") && leftargs >= 1) {
3340 listAddNodeTail(operations,createSortOperation(
3341 REDIS_SORT_GET,c->argv[j+1]));
3342 getop++;
3343 j++;
3344 } else if (!strcasecmp(c->argv[j]->ptr,"del") && leftargs >= 1) {
3345 listAddNodeTail(operations,createSortOperation(
3346 REDIS_SORT_DEL,c->argv[j+1]));
3347 j++;
3348 } else if (!strcasecmp(c->argv[j]->ptr,"incr") && leftargs >= 1) {
3349 listAddNodeTail(operations,createSortOperation(
3350 REDIS_SORT_INCR,c->argv[j+1]));
3351 j++;
3352 } else if (!strcasecmp(c->argv[j]->ptr,"get") && leftargs >= 1) {
3353 listAddNodeTail(operations,createSortOperation(
3354 REDIS_SORT_DECR,c->argv[j+1]));
3355 j++;
3356 } else {
3357 decrRefCount(sortval);
3358 listRelease(operations);
3359 addReply(c,shared.syntaxerr);
3360 return;
3361 }
3362 j++;
3363 }
3364
3365 /* Load the sorting vector with all the objects to sort */
3366 vectorlen = (sortval->type == REDIS_LIST) ?
3367 listLength((list*)sortval->ptr) :
3368 dictSize((dict*)sortval->ptr);
3369 vector = zmalloc(sizeof(redisSortObject)*vectorlen);
3370 if (!vector) oom("allocating objects vector for SORT");
3371 j = 0;
3372 if (sortval->type == REDIS_LIST) {
3373 list *list = sortval->ptr;
3374 listNode *ln;
3375
3376 listRewind(list);
3377 while((ln = listYield(list))) {
3378 robj *ele = ln->value;
3379 vector[j].obj = ele;
3380 vector[j].u.score = 0;
3381 vector[j].u.cmpobj = NULL;
3382 j++;
3383 }
3384 } else {
3385 dict *set = sortval->ptr;
3386 dictIterator *di;
3387 dictEntry *setele;
3388
3389 di = dictGetIterator(set);
3390 if (!di) oom("dictGetIterator");
3391 while((setele = dictNext(di)) != NULL) {
3392 vector[j].obj = dictGetEntryKey(setele);
3393 vector[j].u.score = 0;
3394 vector[j].u.cmpobj = NULL;
3395 j++;
3396 }
3397 dictReleaseIterator(di);
3398 }
3399 assert(j == vectorlen);
3400
3401 /* Now it's time to load the right scores in the sorting vector */
3402 if (dontsort == 0) {
3403 for (j = 0; j < vectorlen; j++) {
3404 if (sortby) {
3405 robj *byval;
3406
3407 byval = lookupKeyByPattern(c->db,sortby,vector[j].obj);
3408 if (!byval || byval->type != REDIS_STRING) continue;
3409 if (alpha) {
3410 vector[j].u.cmpobj = byval;
3411 incrRefCount(byval);
3412 } else {
3413 vector[j].u.score = strtod(byval->ptr,NULL);
3414 }
3415 } else {
3416 if (!alpha) vector[j].u.score = strtod(vector[j].obj->ptr,NULL);
3417 }
3418 }
3419 }
3420
3421 /* We are ready to sort the vector... perform a bit of sanity check
3422 * on the LIMIT option too. We'll use a partial version of quicksort. */
3423 start = (limit_start < 0) ? 0 : limit_start;
3424 end = (limit_count < 0) ? vectorlen-1 : start+limit_count-1;
3425 if (start >= vectorlen) {
3426 start = vectorlen-1;
3427 end = vectorlen-2;
3428 }
3429 if (end >= vectorlen) end = vectorlen-1;
3430
3431 if (dontsort == 0) {
3432 server.sort_desc = desc;
3433 server.sort_alpha = alpha;
3434 server.sort_bypattern = sortby ? 1 : 0;
3435 if (sortby && (start != 0 || end != vectorlen-1))
3436 pqsort(vector,vectorlen,sizeof(redisSortObject),sortCompare, start,end);
3437 else
3438 qsort(vector,vectorlen,sizeof(redisSortObject),sortCompare);
3439 }
3440
3441 /* Send command output to the output buffer, performing the specified
3442 * GET/DEL/INCR/DECR operations if any. */
3443 outputlen = getop ? getop*(end-start+1) : end-start+1;
3444 addReplySds(c,sdscatprintf(sdsempty(),"*%d\r\n",outputlen));
3445 for (j = start; j <= end; j++) {
3446 listNode *ln;
3447 if (!getop) {
3448 addReplySds(c,sdscatprintf(sdsempty(),"$%d\r\n",
3449 sdslen(vector[j].obj->ptr)));
3450 addReply(c,vector[j].obj);
3451 addReply(c,shared.crlf);
3452 }
3453 listRewind(operations);
3454 while((ln = listYield(operations))) {
3455 redisSortOperation *sop = ln->value;
3456 robj *val = lookupKeyByPattern(c->db,sop->pattern,
3457 vector[j].obj);
3458
3459 if (sop->type == REDIS_SORT_GET) {
3460 if (!val || val->type != REDIS_STRING) {
3461 addReply(c,shared.nullbulk);
3462 } else {
3463 addReplySds(c,sdscatprintf(sdsempty(),"$%d\r\n",
3464 sdslen(val->ptr)));
3465 addReply(c,val);
3466 addReply(c,shared.crlf);
3467 }
3468 } else if (sop->type == REDIS_SORT_DEL) {
3469 /* TODO */
3470 }
3471 }
3472 }
3473
3474 /* Cleanup */
3475 decrRefCount(sortval);
3476 listRelease(operations);
3477 for (j = 0; j < vectorlen; j++) {
3478 if (sortby && alpha && vector[j].u.cmpobj)
3479 decrRefCount(vector[j].u.cmpobj);
3480 }
3481 zfree(vector);
3482 }
3483
3484 static void infoCommand(redisClient *c) {
3485 sds info;
3486 time_t uptime = time(NULL)-server.stat_starttime;
3487
3488 info = sdscatprintf(sdsempty(),
3489 "redis_version:%s\r\n"
3490 "connected_clients:%d\r\n"
3491 "connected_slaves:%d\r\n"
3492 "used_memory:%zu\r\n"
3493 "changes_since_last_save:%lld\r\n"
3494 "bgsave_in_progress:%d\r\n"
3495 "last_save_time:%d\r\n"
3496 "total_connections_received:%lld\r\n"
3497 "total_commands_processed:%lld\r\n"
3498 "uptime_in_seconds:%d\r\n"
3499 "uptime_in_days:%d\r\n"
3500 ,REDIS_VERSION,
3501 listLength(server.clients)-listLength(server.slaves),
3502 listLength(server.slaves),
3503 server.usedmemory,
3504 server.dirty,
3505 server.bgsaveinprogress,
3506 server.lastsave,
3507 server.stat_numconnections,
3508 server.stat_numcommands,
3509 uptime,
3510 uptime/(3600*24)
3511 );
3512 addReplySds(c,sdscatprintf(sdsempty(),"$%d\r\n",sdslen(info)));
3513 addReplySds(c,info);
3514 addReply(c,shared.crlf);
3515 }
3516
3517 static void monitorCommand(redisClient *c) {
3518 /* ignore MONITOR if aleady slave or in monitor mode */
3519 if (c->flags & REDIS_SLAVE) return;
3520
3521 c->flags |= (REDIS_SLAVE|REDIS_MONITOR);
3522 c->slaveseldb = 0;
3523 if (!listAddNodeTail(server.monitors,c)) oom("listAddNodeTail");
3524 addReply(c,shared.ok);
3525 }
3526
3527 /* ================================= Expire ================================= */
3528 static int removeExpire(redisDb *db, robj *key) {
3529 if (dictDelete(db->expires,key) == DICT_OK) {
3530 return 1;
3531 } else {
3532 return 0;
3533 }
3534 }
3535
3536 static int setExpire(redisDb *db, robj *key, time_t when) {
3537 if (dictAdd(db->expires,key,(void*)when) == DICT_ERR) {
3538 return 0;
3539 } else {
3540 incrRefCount(key);
3541 return 1;
3542 }
3543 }
3544
3545 /* Return the expire time of the specified key, or -1 if no expire
3546 * is associated with this key (i.e. the key is non volatile) */
3547 static time_t getExpire(redisDb *db, robj *key) {
3548 dictEntry *de;
3549
3550 /* No expire? return ASAP */
3551 if (dictSize(db->expires) == 0 ||
3552 (de = dictFind(db->expires,key)) == NULL) return -1;
3553
3554 return (time_t) dictGetEntryVal(de);
3555 }
3556
3557 static int expireIfNeeded(redisDb *db, robj *key) {
3558 time_t when;
3559 dictEntry *de;
3560
3561 /* No expire? return ASAP */
3562 if (dictSize(db->expires) == 0 ||
3563 (de = dictFind(db->expires,key)) == NULL) return 0;
3564
3565 /* Lookup the expire */
3566 when = (time_t) dictGetEntryVal(de);
3567 if (time(NULL) <= when) return 0;
3568
3569 /* Delete the key */
3570 dictDelete(db->expires,key);
3571 return dictDelete(db->dict,key) == DICT_OK;
3572 }
3573
3574 static int deleteIfVolatile(redisDb *db, robj *key) {
3575 dictEntry *de;
3576
3577 /* No expire? return ASAP */
3578 if (dictSize(db->expires) == 0 ||
3579 (de = dictFind(db->expires,key)) == NULL) return 0;
3580
3581 /* Delete the key */
3582 server.dirty++;
3583 dictDelete(db->expires,key);
3584 return dictDelete(db->dict,key) == DICT_OK;
3585 }
3586
3587 static void expireCommand(redisClient *c) {
3588 dictEntry *de;
3589 int seconds = atoi(c->argv[2]->ptr);
3590
3591 de = dictFind(c->db->dict,c->argv[1]);
3592 if (de == NULL) {
3593 addReply(c,shared.czero);
3594 return;
3595 }
3596 if (seconds <= 0) {
3597 addReply(c, shared.czero);
3598 return;
3599 } else {
3600 time_t when = time(NULL)+seconds;
3601 if (setExpire(c->db,c->argv[1],when))
3602 addReply(c,shared.cone);
3603 else
3604 addReply(c,shared.czero);
3605 return;
3606 }
3607 }
3608
3609 static void ttlCommand(redisClient *c) {
3610 time_t expire;
3611 int ttl = -1;
3612
3613 expire = getExpire(c->db,c->argv[1]);
3614 if (expire != -1) {
3615 ttl = (int) (expire-time(NULL));
3616 if (ttl < 0) ttl = -1;
3617 }
3618 addReplySds(c,sdscatprintf(sdsempty(),":%d\r\n",ttl));
3619 }
3620
3621 /* =============================== Replication ============================= */
3622
3623 static int syncWrite(int fd, char *ptr, ssize_t size, int timeout) {
3624 ssize_t nwritten, ret = size;
3625 time_t start = time(NULL);
3626
3627 timeout++;
3628 while(size) {
3629 if (aeWait(fd,AE_WRITABLE,1000) & AE_WRITABLE) {
3630 nwritten = write(fd,ptr,size);
3631 if (nwritten == -1) return -1;
3632 ptr += nwritten;
3633 size -= nwritten;
3634 }
3635 if ((time(NULL)-start) > timeout) {
3636 errno = ETIMEDOUT;
3637 return -1;
3638 }
3639 }
3640 return ret;
3641 }
3642
3643 static int syncRead(int fd, char *ptr, ssize_t size, int timeout) {
3644 ssize_t nread, totread = 0;
3645 time_t start = time(NULL);
3646
3647 timeout++;
3648 while(size) {
3649 if (aeWait(fd,AE_READABLE,1000) & AE_READABLE) {
3650 nread = read(fd,ptr,size);
3651 if (nread == -1) return -1;
3652 ptr += nread;
3653 size -= nread;
3654 totread += nread;
3655 }
3656 if ((time(NULL)-start) > timeout) {
3657 errno = ETIMEDOUT;
3658 return -1;
3659 }
3660 }
3661 return totread;
3662 }
3663
3664 static int syncReadLine(int fd, char *ptr, ssize_t size, int timeout) {
3665 ssize_t nread = 0;
3666
3667 size--;
3668 while(size) {
3669 char c;
3670
3671 if (syncRead(fd,&c,1,timeout) == -1) return -1;
3672 if (c == '\n') {
3673 *ptr = '\0';
3674 if (nread && *(ptr-1) == '\r') *(ptr-1) = '\0';
3675 return nread;
3676 } else {
3677 *ptr++ = c;
3678 *ptr = '\0';
3679 nread++;
3680 }
3681 }
3682 return nread;
3683 }
3684
3685 static void syncCommand(redisClient *c) {
3686 /* ignore SYNC if aleady slave or in monitor mode */
3687 if (c->flags & REDIS_SLAVE) return;
3688
3689 /* SYNC can't be issued when the server has pending data to send to
3690 * the client about already issued commands. We need a fresh reply
3691 * buffer registering the differences between the BGSAVE and the current
3692 * dataset, so that we can copy to other slaves if needed. */
3693 if (listLength(c->reply) != 0) {
3694 addReplySds(c,sdsnew("-ERR SYNC is invalid with pending input\r\n"));
3695 return;
3696 }
3697
3698 redisLog(REDIS_NOTICE,"Slave ask for synchronization");
3699 /* Here we need to check if there is a background saving operation
3700 * in progress, or if it is required to start one */
3701 if (server.bgsaveinprogress) {
3702 /* Ok a background save is in progress. Let's check if it is a good
3703 * one for replication, i.e. if there is another slave that is
3704 * registering differences since the server forked to save */
3705 redisClient *slave;
3706 listNode *ln;
3707
3708 listRewind(server.slaves);
3709 while((ln = listYield(server.slaves))) {
3710 slave = ln->value;
3711 if (slave->replstate == REDIS_REPL_WAIT_BGSAVE_END) break;
3712 }
3713 if (ln) {
3714 /* Perfect, the server is already registering differences for
3715 * another slave. Set the right state, and copy the buffer. */
3716 listRelease(c->reply);
3717 c->reply = listDup(slave->reply);
3718 if (!c->reply) oom("listDup copying slave reply list");
3719 c->replstate = REDIS_REPL_WAIT_BGSAVE_END;
3720 redisLog(REDIS_NOTICE,"Waiting for end of BGSAVE for SYNC");
3721 } else {
3722 /* No way, we need to wait for the next BGSAVE in order to
3723 * register differences */
3724 c->replstate = REDIS_REPL_WAIT_BGSAVE_START;
3725 redisLog(REDIS_NOTICE,"Waiting for next BGSAVE for SYNC");
3726 }
3727 } else {
3728 /* Ok we don't have a BGSAVE in progress, let's start one */
3729 redisLog(REDIS_NOTICE,"Starting BGSAVE for SYNC");
3730 if (rdbSaveBackground(server.dbfilename) != REDIS_OK) {
3731 redisLog(REDIS_NOTICE,"Replication failed, can't BGSAVE");
3732 addReplySds(c,sdsnew("-ERR Unalbe to perform background save\r\n"));
3733 return;
3734 }
3735 c->replstate = REDIS_REPL_WAIT_BGSAVE_END;
3736 }
3737 c->repldbfd = -1;
3738 c->flags |= REDIS_SLAVE;
3739 c->slaveseldb = 0;
3740 if (!listAddNodeTail(server.slaves,c)) oom("listAddNodeTail");
3741 return;
3742 }
3743
3744 static void sendBulkToSlave(aeEventLoop *el, int fd, void *privdata, int mask) {
3745 redisClient *slave = privdata;
3746 REDIS_NOTUSED(el);
3747 REDIS_NOTUSED(mask);
3748 char buf[REDIS_IOBUF_LEN];
3749 ssize_t nwritten, buflen;
3750
3751 if (slave->repldboff == 0) {
3752 /* Write the bulk write count before to transfer the DB. In theory here
3753 * we don't know how much room there is in the output buffer of the
3754 * socket, but in pratice SO_SNDLOWAT (the minimum count for output
3755 * operations) will never be smaller than the few bytes we need. */
3756 sds bulkcount;
3757
3758 bulkcount = sdscatprintf(sdsempty(),"$%lld\r\n",(unsigned long long)
3759 slave->repldbsize);
3760 if (write(fd,bulkcount,sdslen(bulkcount)) != (signed)sdslen(bulkcount))
3761 {
3762 sdsfree(bulkcount);
3763 freeClient(slave);
3764 return;
3765 }
3766 sdsfree(bulkcount);
3767 }
3768 lseek(slave->repldbfd,slave->repldboff,SEEK_SET);
3769 buflen = read(slave->repldbfd,buf,REDIS_IOBUF_LEN);
3770 if (buflen <= 0) {
3771 redisLog(REDIS_WARNING,"Read error sending DB to slave: %s",
3772 (buflen == 0) ? "premature EOF" : strerror(errno));
3773 freeClient(slave);
3774 return;
3775 }
3776 if ((nwritten = write(fd,buf,buflen)) == -1) {
3777 redisLog(REDIS_DEBUG,"Write error sending DB to slave: %s",
3778 strerror(errno));
3779 freeClient(slave);
3780 return;
3781 }
3782 slave->repldboff += nwritten;
3783 if (slave->repldboff == slave->repldbsize) {
3784 close(slave->repldbfd);
3785 slave->repldbfd = -1;
3786 aeDeleteFileEvent(server.el,slave->fd,AE_WRITABLE);
3787 slave->replstate = REDIS_REPL_ONLINE;
3788 if (aeCreateFileEvent(server.el, slave->fd, AE_WRITABLE,
3789 sendReplyToClient, slave, NULL) == AE_ERR) {
3790 freeClient(slave);
3791 return;
3792 }
3793 addReplySds(slave,sdsempty());
3794 redisLog(REDIS_NOTICE,"Synchronization with slave succeeded");
3795 }
3796 }
3797
3798 static void updateSalvesWaitingBgsave(int bgsaveerr) {
3799 listNode *ln;
3800 int startbgsave = 0;
3801
3802 listRewind(server.slaves);
3803 while((ln = listYield(server.slaves))) {
3804 redisClient *slave = ln->value;
3805
3806 if (slave->replstate == REDIS_REPL_WAIT_BGSAVE_START) {
3807 startbgsave = 1;
3808 slave->replstate = REDIS_REPL_WAIT_BGSAVE_END;
3809 } else if (slave->replstate == REDIS_REPL_WAIT_BGSAVE_END) {
3810 struct stat buf;
3811
3812 if (bgsaveerr != REDIS_OK) {
3813 freeClient(slave);
3814 redisLog(REDIS_WARNING,"SYNC failed. BGSAVE child returned an error");
3815 continue;
3816 }
3817 if ((slave->repldbfd = open(server.dbfilename,O_RDONLY)) == -1 ||
3818 fstat(slave->repldbfd,&buf) == -1) {
3819 freeClient(slave);
3820 redisLog(REDIS_WARNING,"SYNC failed. Can't open/stat DB after BGSAVE: %s", strerror(errno));
3821 continue;
3822 }
3823 slave->repldboff = 0;
3824 slave->repldbsize = buf.st_size;
3825 slave->replstate = REDIS_REPL_SEND_BULK;
3826 aeDeleteFileEvent(server.el,slave->fd,AE_WRITABLE);
3827 if (aeCreateFileEvent(server.el, slave->fd, AE_WRITABLE, sendBulkToSlave, slave, NULL) == AE_ERR) {
3828 freeClient(slave);
3829 continue;
3830 }
3831 }
3832 }
3833 if (startbgsave) {
3834 if (rdbSaveBackground(server.dbfilename) != REDIS_OK) {
3835 listRewind(server.slaves);
3836 redisLog(REDIS_WARNING,"SYNC failed. BGSAVE failed");
3837 while((ln = listYield(server.slaves))) {
3838 redisClient *slave = ln->value;
3839
3840 if (slave->replstate == REDIS_REPL_WAIT_BGSAVE_START)
3841 freeClient(slave);
3842 }
3843 }
3844 }
3845 }
3846
3847 static int syncWithMaster(void) {
3848 char buf[1024], tmpfile[256];
3849 int dumpsize;
3850 int fd = anetTcpConnect(NULL,server.masterhost,server.masterport);
3851 int dfd;
3852
3853 if (fd == -1) {
3854 redisLog(REDIS_WARNING,"Unable to connect to MASTER: %s",
3855 strerror(errno));
3856 return REDIS_ERR;
3857 }
3858 /* Issue the SYNC command */
3859 if (syncWrite(fd,"SYNC \r\n",7,5) == -1) {
3860 close(fd);
3861 redisLog(REDIS_WARNING,"I/O error writing to MASTER: %s",
3862 strerror(errno));
3863 return REDIS_ERR;
3864 }
3865 /* Read the bulk write count */
3866 if (syncReadLine(fd,buf,1024,3600) == -1) {
3867 close(fd);
3868 redisLog(REDIS_WARNING,"I/O error reading bulk count from MASTER: %s",
3869 strerror(errno));
3870 return REDIS_ERR;
3871 }
3872 dumpsize = atoi(buf+1);
3873 redisLog(REDIS_NOTICE,"Receiving %d bytes data dump from MASTER",dumpsize);
3874 /* Read the bulk write data on a temp file */
3875 snprintf(tmpfile,256,"temp-%d.%ld.rdb",(int)time(NULL),(long int)random());
3876 dfd = open(tmpfile,O_CREAT|O_WRONLY,0644);
3877 if (dfd == -1) {
3878 close(fd);
3879 redisLog(REDIS_WARNING,"Opening the temp file needed for MASTER <-> SLAVE synchronization: %s",strerror(errno));
3880 return REDIS_ERR;
3881 }
3882 while(dumpsize) {
3883 int nread, nwritten;
3884
3885 nread = read(fd,buf,(dumpsize < 1024)?dumpsize:1024);
3886 if (nread == -1) {
3887 redisLog(REDIS_WARNING,"I/O error trying to sync with MASTER: %s",
3888 strerror(errno));
3889 close(fd);
3890 close(dfd);
3891 return REDIS_ERR;
3892 }
3893 nwritten = write(dfd,buf,nread);
3894 if (nwritten == -1) {
3895 redisLog(REDIS_WARNING,"Write error writing to the DB dump file needed for MASTER <-> SLAVE synchrnonization: %s", strerror(errno));
3896 close(fd);
3897 close(dfd);
3898 return REDIS_ERR;
3899 }
3900 dumpsize -= nread;
3901 }
3902 close(dfd);
3903 if (rename(tmpfile,server.dbfilename) == -1) {
3904 redisLog(REDIS_WARNING,"Failed trying to rename the temp DB into dump.rdb in MASTER <-> SLAVE synchronization: %s", strerror(errno));
3905 unlink(tmpfile);
3906 close(fd);
3907 return REDIS_ERR;
3908 }
3909 emptyDb();
3910 if (rdbLoad(server.dbfilename) != REDIS_OK) {
3911 redisLog(REDIS_WARNING,"Failed trying to load the MASTER synchronization DB from disk");
3912 close(fd);
3913 return REDIS_ERR;
3914 }
3915 server.master = createClient(fd);
3916 server.master->flags |= REDIS_MASTER;
3917 server.replstate = REDIS_REPL_CONNECTED;
3918 return REDIS_OK;
3919 }
3920
3921 /* =================================== Main! ================================ */
3922
3923 #ifdef __linux__
3924 int linuxOvercommitMemoryValue(void) {
3925 FILE *fp = fopen("/proc/sys/vm/overcommit_memory","r");
3926 char buf[64];
3927
3928 if (!fp) return -1;
3929 if (fgets(buf,64,fp) == NULL) {
3930 fclose(fp);
3931 return -1;
3932 }
3933 fclose(fp);
3934
3935 return atoi(buf);
3936 }
3937
3938 void linuxOvercommitMemoryWarning(void) {
3939 if (linuxOvercommitMemoryValue() == 0) {
3940 redisLog(REDIS_WARNING,"WARNING overcommit_memory is set to 0! Background save may fail under low condition memory. To fix this issue add 'echo 1 > /proc/sys/vm/overcommit_memory' in your init scripts.");
3941 }
3942 }
3943 #endif /* __linux__ */
3944
3945 static void daemonize(void) {
3946 int fd;
3947 FILE *fp;
3948
3949 if (fork() != 0) exit(0); /* parent exits */
3950 setsid(); /* create a new session */
3951
3952 /* Every output goes to /dev/null. If Redis is daemonized but
3953 * the 'logfile' is set to 'stdout' in the configuration file
3954 * it will not log at all. */
3955 if ((fd = open("/dev/null", O_RDWR, 0)) != -1) {
3956 dup2(fd, STDIN_FILENO);
3957 dup2(fd, STDOUT_FILENO);
3958 dup2(fd, STDERR_FILENO);
3959 if (fd > STDERR_FILENO) close(fd);
3960 }
3961 /* Try to write the pid file */
3962 fp = fopen(server.pidfile,"w");
3963 if (fp) {
3964 fprintf(fp,"%d\n",getpid());
3965 fclose(fp);
3966 }
3967 }
3968
3969 int main(int argc, char **argv) {
3970 #ifdef __linux__
3971 linuxOvercommitMemoryWarning();
3972 #endif
3973
3974 initServerConfig();
3975 if (argc == 2) {
3976 ResetServerSaveParams();
3977 loadServerConfig(argv[1]);
3978 } else if (argc > 2) {
3979 fprintf(stderr,"Usage: ./redis-server [/path/to/redis.conf]\n");
3980 exit(1);
3981 }
3982 initServer();
3983 if (server.daemonize) daemonize();
3984 redisLog(REDIS_NOTICE,"Server started, Redis version " REDIS_VERSION);
3985 if (rdbLoad(server.dbfilename) == REDIS_OK)
3986 redisLog(REDIS_NOTICE,"DB loaded from disk");
3987 if (aeCreateFileEvent(server.el, server.fd, AE_READABLE,
3988 acceptHandler, NULL, NULL) == AE_ERR) oom("creating file event");
3989 redisLog(REDIS_NOTICE,"The server is now ready to accept connections on port %d", server.port);
3990 aeMain(server.el);
3991 aeDeleteEventLoop(server.el);
3992 return 0;
3993 }