* POSSIBILITY OF SUCH DAMAGE.
*/
-#define REDIS_VERSION "1.07"
+#define REDIS_VERSION "1.1.91"
#include "fmacros.h"
#include "config.h"
#define APPENDFSYNC_ALWAYS 1
#define APPENDFSYNC_EVERYSEC 2
+/* We can print the stacktrace, so our assert is defined this way: */
+#define redisAssert(_e) ((_e)?(void)0 : (_redisAssert(#_e),exit(1)))
+static void _redisAssert(char *estr);
+
/*================================= Data types ============================== */
/* A redis object, that is a type able to hold a string / list / set */
int refcount;
} robj;
+/* Macro used to initalize a Redis object allocated on the stack.
+ * Note that this macro is taken near the structure definition to make sure
+ * we'll update it when the structure is changed, to avoid bugs like
+ * bug #85 introduced exactly in this way. */
+#define initStaticStringObject(_var,_ptr) do { \
+ _var.refcount = 1; \
+ _var.type = REDIS_STRING; \
+ _var.encoding = REDIS_ENCODING_RAW; \
+ _var.ptr = _ptr; \
+} while(0);
+
typedef struct redisDb {
dict *dict;
dict *expires;
{"lrange",lrangeCommand,4,REDIS_CMD_INLINE},
{"ltrim",ltrimCommand,4,REDIS_CMD_INLINE},
{"lrem",lremCommand,4,REDIS_CMD_BULK},
- {"rpoplpush",rpoplpushcommand,3,REDIS_CMD_BULK},
+ {"rpoplpush",rpoplpushcommand,3,REDIS_CMD_BULK|REDIS_CMD_DENYOOM},
{"sadd",saddCommand,3,REDIS_CMD_BULK|REDIS_CMD_DENYOOM},
{"srem",sremCommand,3,REDIS_CMD_BULK},
{"smove",smoveCommand,4,REDIS_CMD_BULK},
NULL, /* val dup */
dictEncObjKeyCompare, /* key compare */
dictRedisObjectDestructor, /* key destructor */
- dictVanillaFree /* val destructor */
+ dictVanillaFree /* val destructor of malloc(sizeof(double)) */
};
static dictType hashDictType = {
* is based on heap allocation for send buffers, so we simply abort.
* At least the code will be simpler to read... */
static void oom(const char *msg) {
- fprintf(stderr, "%s: Out of memory\n",msg);
- fflush(stderr);
+ redisLog(REDIS_WARNING, "%s: Out of memory\n",msg);
sleep(1);
abort();
}
close(fd);
goto cleanup;
}
- redisLog(REDIS_WARNING,"Parent diff flushed into the new append log file with success");
+ redisLog(REDIS_NOTICE,"Parent diff flushed into the new append log file with success (%lu bytes)",sdslen(server.bgrewritebuf));
/* Now our work is to rename the temp file into the stable file. And
* switch the file descriptor used by the server for append only. */
if (rename(tmpfile,server.appendfilename) == -1) {
freeClientArgv(c);
close(c->fd);
ln = listSearchKey(server.clients,c);
- assert(ln != NULL);
+ redisAssert(ln != NULL);
listDelNode(server.clients,ln);
if (c->flags & REDIS_SLAVE) {
if (c->replstate == REDIS_REPL_SEND_BULK && c->repldbfd != -1)
close(c->repldbfd);
list *l = (c->flags & REDIS_MONITOR) ? server.monitors : server.slaves;
ln = listSearchKey(l,c);
- assert(ln != NULL);
+ redisAssert(ln != NULL);
listDelNode(l,ln);
}
if (c->flags & REDIS_MASTER) {
return 1;
} else if ((cmd->arity > 0 && cmd->arity != c->argc) ||
(c->argc < -cmd->arity)) {
- addReplySds(c,sdsnew("-ERR wrong number of arguments\r\n"));
+ addReplySds(c,
+ sdscatprintf(sdsempty(),
+ "-ERR wrong number of arguments for '%s' command\r\n",
+ cmd->name));
resetClient(c);
return 1;
} else if (server.maxmemory && cmd->flags & REDIS_CMD_DENYOOM && zmalloc_used_memory() > server.maxmemory) {
robj *lenobj;
lenobj = createObject(REDIS_STRING,
- sdscatprintf(sdsempty(),"%d\r\n",
- stringObjectLen(argv[j])));
+ sdscatprintf(sdsempty(),"%lu\r\n",
+ (unsigned long) stringObjectLen(argv[j])));
lenobj->refcount = 0;
outv[outc++] = lenobj;
}
if (sdslen(query) == 0) {
/* Ignore empty query */
sdsfree(query);
+ if (sdslen(c->querybuf)) goto again;
return;
}
argv = sdssplitlen(query,sdslen(query)," ",1,&argc);
}
}
zfree(argv);
- /* Execute the command. If the client is still valid
- * after processCommand() return and there is something
- * on the query buffer try to process the next command. */
- if (c->argc && processCommand(c) && sdslen(c->querybuf)) goto again;
+ if (c->argc) {
+ /* Execute the command. If the client is still valid
+ * after processCommand() return and there is something
+ * on the query buffer try to process the next command. */
+ if (processCommand(c) && sdslen(c->querybuf)) goto again;
+ } else {
+ /* Nothing to process, argc == 0. Just process the query
+ * buffer if it's not empty or return to the caller */
+ if (sdslen(c->querybuf)) goto again;
+ }
return;
} else if (sdslen(c->querybuf) >= REDIS_REQUEST_MAX_SIZE) {
redisLog(REDIS_DEBUG, "Client protocol error");
char buf[128];
snprintf(buf,sizeof(buf),"%.17g",d);
- addReplySds(c,sdscatprintf(sdsempty(),"$%d\r\n%s\r\n",
- strlen(buf),buf));
+ addReplySds(c,sdscatprintf(sdsempty(),"$%lu\r\n%s\r\n",
+ (unsigned long) strlen(buf),buf));
}
static void addReplyBulkLen(redisClient *c, robj *obj) {
len++;
}
}
- addReplySds(c,sdscatprintf(sdsempty(),"$%d\r\n",len));
+ addReplySds(c,sdscatprintf(sdsempty(),"$%lu\r\n",(unsigned long)len));
}
static void acceptHandler(aeEventLoop *el, int fd, void *privdata, int mask) {
case REDIS_SET: freeSetObject(o); break;
case REDIS_ZSET: freeZsetObject(o); break;
case REDIS_HASH: freeHashObject(o); break;
- default: assert(0 != 0); break;
+ default: redisAssert(0 != 0); break;
}
if (listLength(server.objfreelist) > REDIS_OBJFREELIST_MAX ||
!listAddNodeHead(server.objfreelist,o))
if (o == NULL || server.shareobjects == 0) return o;
- assert(o->type == REDIS_STRING);
+ redisAssert(o->type == REDIS_STRING);
de = dictFind(server.sharingpool,o);
if (de) {
robj *shared = dictGetEntryKey(de);
if (dictSize(server.sharingpool) >=
server.sharingpoolsize) {
de = dictGetRandomKey(server.sharingpool);
- assert(de != NULL);
+ redisAssert(de != NULL);
c = ((unsigned long) dictGetEntryVal(de))-1;
dictGetEntryVal(de) = (void*) c;
if (c == 0) {
int retval;
retval = dictAdd(server.sharingpool,o,(void*)1);
- assert(retval == DICT_OK);
+ redisAssert(retval == DICT_OK);
incrRefCount(o);
}
return o;
if (o->refcount > 1) return REDIS_ERR;
/* Currently we try to encode only strings */
- assert(o->type == REDIS_STRING);
+ redisAssert(o->type == REDIS_STRING);
/* Check if we can represent this string as a long integer */
if (isStringRepresentableAsLong(s,&value) == REDIS_ERR) return REDIS_ERR;
dec = createStringObject(buf,strlen(buf));
return dec;
} else {
- assert(1 != 1);
+ redisAssert(1 != 1);
}
}
* sdscmp() from sds.c will apply memcmp() so this function ca be considered
* binary safe. */
static int compareStringObjects(robj *a, robj *b) {
- assert(a->type == REDIS_STRING && b->type == REDIS_STRING);
+ redisAssert(a->type == REDIS_STRING && b->type == REDIS_STRING);
char bufa[128], bufb[128], *astr, *bstr;
int bothsds = 1;
}
static size_t stringObjectLen(robj *o) {
- assert(o->type == REDIS_STRING);
+ redisAssert(o->type == REDIS_STRING);
if (o->encoding == REDIS_ENCODING_RAW) {
return sdslen(o->ptr);
} else {
}
dictReleaseIterator(di);
} else {
- assert(0 != 0);
+ redisAssert(0 != 0);
}
}
dictReleaseIterator(di);
val = (int32_t)v;
} else {
val = 0; /* anti-warning */
- assert(0!=0);
+ redisAssert(0!=0);
}
return createObject(REDIS_STRING,sdscatprintf(sdsempty(),"%lld",val));
}
case REDIS_RDB_ENC_LZF:
return tryObjectSharing(rdbLoadLzfStringObject(fp,rdbver));
default:
- assert(0!=0);
+ redisAssert(0!=0);
}
}
case 253: *val = R_Nan; return 0;
default:
if (fread(buf,len,1,fp) == 0) return -1;
+ buf[len] = '\0';
sscanf(buf, "%lg", val);
return 0;
}
incrRefCount(ele); /* added to skiplist */
}
} else {
- assert(0 != 0);
+ redisAssert(0 != 0);
}
/* Add the new object in the hash table */
retval = dictAdd(d,keyobj,o);
static void setGenericCommand(redisClient *c, int nx) {
int retval;
+ if (nx) deleteIfVolatile(c->db,c->argv[1]);
retval = dictAdd(c->db->dict,c->argv[1],c->argv[2]);
if (retval == DICT_ERR) {
if (!nx) {
}
static void msetGenericCommand(redisClient *c, int nx) {
- int j;
+ int j, busykeys = 0;
if ((c->argc % 2) == 0) {
- addReplySds(c,sdsnew("-ERR wrong number of arguments\r\n"));
+ addReplySds(c,sdsnew("-ERR wrong number of arguments for MSET\r\n"));
return;
}
/* Handle the NX flag. The MSETNX semantic is to return zero and don't
* set nothing at all if at least one already key exists. */
if (nx) {
for (j = 1; j < c->argc; j += 2) {
- if (dictFind(c->db->dict,c->argv[j]) != NULL) {
- addReply(c, shared.czero);
- return;
+ if (lookupKeyWrite(c->db,c->argv[j]) != NULL) {
+ busykeys++;
}
}
}
+ if (busykeys) {
+ addReply(c, shared.czero);
+ return;
+ }
for (j = 1; j < c->argc; j += 2) {
int retval;
else if (o->encoding == REDIS_ENCODING_INT)
value = (long)o->ptr;
else
- assert(1 != 1);
+ redisAssert(1 != 1);
}
}
dictEntry *de;
sds pattern = c->argv[1]->ptr;
int plen = sdslen(pattern);
- int numkeys = 0, keyslen = 0;
+ unsigned long numkeys = 0, keyslen = 0;
robj *lenobj = createObject(REDIS_STRING,NULL);
di = dictGetIterator(c->db->dict);
return;
}
if (rdbSaveBackground(server.dbfilename) == REDIS_OK) {
- addReply(c,shared.ok);
+ char *status = "+Background saving started\r\n";
+ addReplySds(c,sdsnew(status));
} else {
addReply(c,shared.err);
}
addReply(c,shared.wrongtypeerr);
} else {
s = o->ptr;
- addReplySds(c,sdscatprintf(sdsempty(),":%d\r\n",
+ addReplySds(c,sdscatprintf(sdsempty(),":%lu\r\n",
dictSize(s)));
}
}
return dictSize(*d1)-dictSize(*d2);
}
-static void sinterGenericCommand(redisClient *c, robj **setskeys, int setsnum, robj *dstkey) {
+static void sinterGenericCommand(redisClient *c, robj **setskeys, unsigned long setsnum, robj *dstkey) {
dict **dv = zmalloc(sizeof(dict*)*setsnum);
dictIterator *di;
dictEntry *de;
robj *lenobj = NULL, *dstset = NULL;
- int j, cardinality = 0;
+ unsigned long j, cardinality = 0;
for (j = 0; j < setsnum; j++) {
robj *setobj;
zfree(dv);
if (dstkey) {
deleteKey(c->db,dstkey);
- addReply(c,shared.ok);
+ addReply(c,shared.czero);
} else {
addReply(c,shared.nullmultibulk);
}
}
if (!dstkey) {
- lenobj->ptr = sdscatprintf(sdsempty(),"*%d\r\n",cardinality);
+ lenobj->ptr = sdscatprintf(sdsempty(),"*%lu\r\n",cardinality);
} else {
- addReplySds(c,sdscatprintf(sdsempty(),":%d\r\n",
+ addReplySds(c,sdscatprintf(sdsempty(),":%lu\r\n",
dictSize((dict*)dstset->ptr)));
server.dirty++;
}
if (!dstkey) {
decrRefCount(dstset);
} else {
- addReplySds(c,sdscatprintf(sdsempty(),":%d\r\n",
+ addReplySds(c,sdscatprintf(sdsempty(),":%lu\r\n",
dictSize((dict*)dstset->ptr)));
server.dirty++;
}
/* case 2: Score update operation */
de = dictFind(zs->dict,ele);
- assert(de != NULL);
+ redisAssert(de != NULL);
oldscore = dictGetEntryVal(de);
if (*score != *oldscore) {
int deleted;
/* Remove and insert the element in the skip list with new score */
deleted = zslDelete(zs->zsl,*oldscore,ele);
- assert(deleted != 0);
+ redisAssert(deleted != 0);
zslInsert(zs->zsl,*score,ele);
incrRefCount(ele);
/* Update the score in the hash table */
/* Delete from the skiplist */
oldscore = dictGetEntryVal(de);
deleted = zslDelete(zs->zsl,*oldscore,c->argv[2]);
- assert(deleted != 0);
+ redisAssert(deleted != 0);
/* Delete from the hash table */
dictDelete(zs->dict,c->argv[2]);
int offset = 0, limit = -1;
if (c->argc != 4 && c->argc != 7) {
- addReplySds(c,sdsnew("-ERR wrong number of arguments\r\n"));
+ addReplySds(c,
+ sdsnew("-ERR wrong number of arguments for ZRANGEBYSCORE\r\n"));
return;
} else if (c->argc == 7 && strcasecmp(c->argv[4]->ptr,"limit")) {
addReply(c,shared.syntaxerr);
} else if (c->argc == 7) {
offset = atoi(c->argv[5]->ptr);
limit = atoi(c->argv[6]->ptr);
+ if (offset < 0) offset = 0;
}
o = lookupKeyRead(c->db,c->argv[1]);
addReply(c,shared.wrongtypeerr);
} else {
zs = o->ptr;
- addReplySds(c,sdscatprintf(sdsempty(),":%d\r\n",zs->zsl->length));
+ addReplySds(c,sdscatprintf(sdsempty(),":%lu\r\n",zs->zsl->length));
}
}
}
keyname.buf[prefixlen+sublen+postfixlen] = '\0';
keyname.len = prefixlen+sublen+postfixlen;
- keyobj.refcount = 1;
- keyobj.type = REDIS_STRING;
- keyobj.ptr = ((char*)&keyname)+(sizeof(long)*2);
-
+ initStaticStringObject(keyobj,((char*)&keyname)+(sizeof(long)*2))
decrRefCount(subst);
/* printf("lookup '%s' => %p\n", keyname.buf,de); */
case REDIS_LIST: vectorlen = listLength((list*)sortval->ptr); break;
case REDIS_SET: vectorlen = dictSize((dict*)sortval->ptr); break;
case REDIS_ZSET: vectorlen = dictSize(((zset*)sortval->ptr)->dict); break;
- default: vectorlen = 0; assert(0); /* Avoid GCC warning */
+ default: vectorlen = 0; redisAssert(0); /* Avoid GCC warning */
}
vector = zmalloc(sizeof(redisSortObject)*vectorlen);
j = 0;
}
dictReleaseIterator(di);
}
- assert(j == vectorlen);
+ redisAssert(j == vectorlen);
/* Now it's time to load the right scores in the sorting vector */
if (dontsort == 0) {
if (byval->encoding == REDIS_ENCODING_INT) {
vector[j].u.score = (long)byval->ptr;
} else
- assert(1 != 1);
+ redisAssert(1 != 1);
}
}
} else {
if (vector[j].obj->encoding == REDIS_ENCODING_INT)
vector[j].u.score = (long) vector[j].obj->ptr;
else
- assert(1 != 1);
+ redisAssert(1 != 1);
}
}
}
addReply(c,shared.crlf);
}
} else {
- assert(sop->type == REDIS_SORT_GET); /* always fails */
+ redisAssert(sop->type == REDIS_SORT_GET); /* always fails */
}
}
}
incrRefCount(val);
}
} else {
- assert(sop->type == REDIS_SORT_GET); /* always fails */
+ redisAssert(sop->type == REDIS_SORT_GET); /* always fails */
}
}
}
"redis_version:%s\r\n"
"arch_bits:%s\r\n"
"multiplexing_api:%s\r\n"
- "uptime_in_seconds:%d\r\n"
- "uptime_in_days:%d\r\n"
+ "uptime_in_seconds:%ld\r\n"
+ "uptime_in_days:%ld\r\n"
"connected_clients:%d\r\n"
"connected_slaves:%d\r\n"
"used_memory:%zu\r\n"
"changes_since_last_save:%lld\r\n"
"bgsave_in_progress:%d\r\n"
- "last_save_time:%d\r\n"
+ "last_save_time:%ld\r\n"
+ "bgrewriteaof_in_progress:%d\r\n"
"total_connections_received:%lld\r\n"
"total_commands_processed:%lld\r\n"
"role:%s\r\n"
server.dirty,
server.bgsavechildpid != -1,
server.lastsave,
+ server.bgrewritechildpid != -1,
server.stat_numconnections,
server.stat_numcommands,
server.masterhost == NULL ? "master" : "slave"
static void infoCommand(redisClient *c) {
sds info = genRedisInfoString();
- addReplySds(c,sdscatprintf(sdsempty(),"$%d\r\n",sdslen(info)));
+ addReplySds(c,sdscatprintf(sdsempty(),"$%lu\r\n",
+ (unsigned long)sdslen(info)));
addReplySds(c,info);
addReply(c,shared.crlf);
}
}
server.master = createClient(fd);
server.master->flags |= REDIS_MASTER;
+ server.master->authenticated = 1;
server.replstate = REDIS_REPL_CONNECTED;
return REDIS_OK;
}
char seldb[64];
snprintf(seldb,sizeof(seldb),"%d",dictid);
- buf = sdscatprintf(buf,"*2\r\n$6\r\nSELECT\r\n$%d\r\n%s\r\n",
- strlen(seldb),seldb);
+ buf = sdscatprintf(buf,"*2\r\n$6\r\nSELECT\r\n$%lu\r\n%s\r\n",
+ (unsigned long)strlen(seldb),seldb);
server.appendseldb = dictid;
}
robj *o = argv[j];
o = getDecodedObject(o);
- buf = sdscatprintf(buf,"$%d\r\n",sdslen(o->ptr));
+ buf = sdscatprintf(buf,"$%lu\r\n",(unsigned long)sdslen(o->ptr));
buf = sdscatlen(buf,o->ptr,sdslen(o->ptr));
buf = sdscatlen(buf,"\r\n",2);
decrRefCount(o);
}
dictReleaseIterator(di);
} else {
- assert(0 != 0);
+ redisAssert(0 != 0);
}
/* Save the expire time */
if (expiretime != -1) {
return;
}
if (rewriteAppendOnlyFileBackground() == REDIS_OK) {
- addReply(c,shared.ok);
+ char *status = "+Background append only file rewriting started\r\n";
+ addReplySds(c,sdsnew(status));
} else {
addReply(c,shared.err);
}
val = dictGetEntryVal(de);
addReplySds(c,sdscatprintf(sdsempty(),
"+Key at:%p refcount:%d, value at:%p refcount:%d encoding:%d\r\n",
- key, key->refcount, val, val->refcount, val->encoding));
+ (void*)key, key->refcount, (void*)val, val->refcount,
+ val->encoding));
} else {
addReplySds(c,sdsnew(
"-ERR Syntax error, try DEBUG [SEGFAULT|OBJECT <key>|RELOAD]\r\n"));
}
}
+static void _redisAssert(char *estr) {
+ redisLog(REDIS_WARNING,"=== ASSERTION FAILED ===");
+ redisLog(REDIS_WARNING,"==> %s\n",estr);
+#ifdef HAVE_BACKTRACE
+ redisLog(REDIS_WARNING,"(forcing SIGSEGV in order to print the stack trace)");
+ *((char*)-1) = 'x';
+#endif
+}
+
/* =================================== Main! ================================ */
#ifdef __linux__
FILE *fp;
if (fork() != 0) exit(0); /* parent exits */
+ printf("New pid: %d\n", getpid());
setsid(); /* create a new session */
/* Every output goes to /dev/null. If Redis is daemonized but
} else {
redisLog(REDIS_WARNING,"Warning: no config file specified, using the default config. In order to specify a config file use 'redis-server /path/to/redis.conf'");
}
- initServer();
if (server.daemonize) daemonize();
+ initServer();
redisLog(REDIS_NOTICE,"Server started, Redis version " REDIS_VERSION);
#ifdef __linux__
linuxOvercommitMemoryWarning();
#elif defined(__dietlibc__)
return (void*) uc->uc_mcontext.eip;
#elif defined(__APPLE__) && !defined(MAC_OS_X_VERSION_10_6)
+ #if __x86_64__
+ return (void*) uc->uc_mcontext->__ss.__rip;
+ #else
return (void*) uc->uc_mcontext->__ss.__eip;
+ #endif
#elif defined(__APPLE__) && defined(MAC_OS_X_VERSION_10_6)
#if defined(_STRUCT_X86_THREAD_STATE64) && !defined(__i386__)
return (void*) uc->uc_mcontext->__ss.__rip;