]> git.saurik.com Git - redis.git/blame - src/networking.c
Merge pull request #544 from dvirsky/2.6
[redis.git] / src / networking.c
CommitLineData
e2641e09 1#include "redis.h"
e2641e09 2#include <sys/uio.h>
3
11e0c4c5 4static void setProtocolError(redisClient *c, int pos);
5
609baba8 6/* To evaluate the output buffer size of a client we need to get size of
7 * allocated objects, however we can't used zmalloc_size() directly on sds
8 * strings because of the trick they use to work (the header is before the
9 * returned pointer), so we use this helper function. */
10size_t zmalloc_size_sds(sds s) {
11 return zmalloc_size(s-sizeof(struct sdshdr));
12}
13
e2641e09 14void *dupClientReplyValue(void *o) {
15 incrRefCount((robj*)o);
16 return o;
17}
18
19int listMatchObjects(void *a, void *b) {
20 return equalStringObjects(a,b);
21}
22
23redisClient *createClient(int fd) {
f3357792 24 redisClient *c = zmalloc(sizeof(redisClient));
e2641e09 25
0f1d64ca 26 /* passing -1 as fd it is possible to create a non connected client.
27 * This is useful since all the Redis commands needs to be executed
28 * in the context of a client. When commands are executed in other
29 * contexts (for instance a Lua script) we need a non connected client. */
30 if (fd != -1) {
31 anetNonBlock(NULL,fd);
32 anetTcpNoDelay(NULL,fd);
33 if (aeCreateFileEvent(server.el,fd,AE_READABLE,
34 readQueryFromClient, c) == AE_ERR)
35 {
36 close(fd);
37 zfree(c);
38 return NULL;
39 }
106bd87a
PN
40 }
41
e2641e09 42 selectDb(c,0);
43 c->fd = fd;
56de4964 44 c->bufpos = 0;
e2641e09 45 c->querybuf = sdsempty();
9fa9ccb0 46 c->querybuf_peak = 0;
cd8788f2 47 c->reqtype = 0;
e2641e09 48 c->argc = 0;
49 c->argv = NULL;
2c74a9f9 50 c->cmd = c->lastcmd = NULL;
cd8788f2 51 c->multibulklen = 0;
e2641e09 52 c->bulklen = -1;
e2641e09 53 c->sentlen = 0;
54 c->flags = 0;
56ff70f8 55 c->ctime = c->lastinteraction = server.unixtime;
e2641e09 56 c->authenticated = 0;
57 c->replstate = REDIS_REPL_NONE;
dbd8c753 58 c->slave_listening_port = 0;
e2641e09 59 c->reply = listCreate();
3853c168 60 c->reply_bytes = 0;
7eac2a75 61 c->obuf_soft_limit_reached_time = 0;
e2641e09 62 listSetFreeMethod(c->reply,decrRefCount);
63 listSetDupMethod(c->reply,dupClientReplyValue);
e3c51c4b
DJMM
64 c->bpop.keys = NULL;
65 c->bpop.count = 0;
66 c->bpop.timeout = 0;
67 c->bpop.target = NULL;
e2641e09 68 c->io_keys = listCreate();
69 c->watched_keys = listCreate();
70 listSetFreeMethod(c->io_keys,decrRefCount);
71 c->pubsub_channels = dictCreate(&setDictType,NULL);
72 c->pubsub_patterns = listCreate();
73 listSetFreeMethod(c->pubsub_patterns,decrRefCount);
74 listSetMatchMethod(c->pubsub_patterns,listMatchObjects);
7b722727 75 if (fd != -1) listAddNodeTail(server.clients,c);
e2641e09 76 initClientMultiState(c);
77 return c;
78}
79
51669c5a 80/* This function is called every time we are going to transmit new data
81 * to the client. The behavior is the following:
82 *
83 * If the client should receive new data (normal clients will) the function
84 * returns REDIS_OK, and make sure to install the write handler in our event
85 * loop so that when the socket is writable new data gets written.
86 *
87 * If the client should not receive new data, because it is a fake client
88 * or a slave, or because the setup of the write handler failed, the function
89 * returns REDIS_ERR.
90 *
91 * Typically gets called every time a reply is built, before adding more
92 * data to the clients output buffers. If the function returns REDIS_ERR no
93 * data should be appended to the output buffers. */
94int prepareClientToWrite(redisClient *c) {
7156f43c 95 if (c->flags & REDIS_LUA_CLIENT) return REDIS_OK;
51669c5a 96 if (c->fd <= 0) return REDIS_ERR; /* Fake client */
834ef78e 97 if (c->bufpos == 0 && listLength(c->reply) == 0 &&
e2641e09 98 (c->replstate == REDIS_REPL_NONE ||
99 c->replstate == REDIS_REPL_ONLINE) &&
100 aeCreateFileEvent(server.el, c->fd, AE_WRITABLE,
834ef78e
PN
101 sendReplyToClient, c) == AE_ERR) return REDIS_ERR;
102 return REDIS_OK;
103}
104
36c19d03
PN
105/* Create a duplicate of the last object in the reply list when
106 * it is not exclusively owned by the reply list. */
107robj *dupLastObjectIfNeeded(list *reply) {
108 robj *new, *cur;
109 listNode *ln;
110 redisAssert(listLength(reply) > 0);
111 ln = listLast(reply);
112 cur = listNodeValue(ln);
113 if (cur->refcount > 1) {
114 new = dupStringObject(cur);
115 decrRefCount(cur);
116 listNodeValue(ln) = new;
117 }
118 return listNodeValue(ln);
834ef78e
PN
119}
120
25ef3192 121/* -----------------------------------------------------------------------------
122 * Low level functions to add more data to output buffers.
123 * -------------------------------------------------------------------------- */
124
36c19d03 125int _addReplyToBuffer(redisClient *c, char *s, size_t len) {
f3357792 126 size_t available = sizeof(c->buf)-c->bufpos;
36c19d03 127
25ef3192 128 if (c->flags & REDIS_CLOSE_AFTER_REPLY) return REDIS_OK;
129
36c19d03
PN
130 /* If there already are entries in the reply list, we cannot
131 * add anything more to the static buffer. */
132 if (listLength(c->reply) > 0) return REDIS_ERR;
133
134 /* Check that the buffer has enough space available for this string. */
135 if (len > available) return REDIS_ERR;
e2641e09 136
36c19d03
PN
137 memcpy(c->buf+c->bufpos,s,len);
138 c->bufpos+=len;
139 return REDIS_OK;
834ef78e
PN
140}
141
36c19d03
PN
142void _addReplyObjectToList(redisClient *c, robj *o) {
143 robj *tail;
25ef3192 144
145 if (c->flags & REDIS_CLOSE_AFTER_REPLY) return;
146
36c19d03
PN
147 if (listLength(c->reply) == 0) {
148 incrRefCount(o);
149 listAddNodeTail(c->reply,o);
609baba8 150 c->reply_bytes += zmalloc_size_sds(o->ptr);
36c19d03
PN
151 } else {
152 tail = listNodeValue(listLast(c->reply));
153
154 /* Append to this object when possible. */
155 if (tail->ptr != NULL &&
156 sdslen(tail->ptr)+sdslen(o->ptr) <= REDIS_REPLY_CHUNK_BYTES)
157 {
609baba8 158 c->reply_bytes -= zmalloc_size_sds(tail->ptr);
36c19d03
PN
159 tail = dupLastObjectIfNeeded(c->reply);
160 tail->ptr = sdscatlen(tail->ptr,o->ptr,sdslen(o->ptr));
609baba8 161 c->reply_bytes += zmalloc_size_sds(tail->ptr);
36c19d03
PN
162 } else {
163 incrRefCount(o);
164 listAddNodeTail(c->reply,o);
609baba8 165 c->reply_bytes += zmalloc_size_sds(o->ptr);
36c19d03
PN
166 }
167 }
7eac2a75 168 asyncCloseClientOnOutputBufferLimitReached(c);
36c19d03 169}
834ef78e 170
36c19d03
PN
171/* This method takes responsibility over the sds. When it is no longer
172 * needed it will be free'd, otherwise it ends up in a robj. */
173void _addReplySdsToList(redisClient *c, sds s) {
174 robj *tail;
25ef3192 175
5b94b8ac 176 if (c->flags & REDIS_CLOSE_AFTER_REPLY) {
177 sdsfree(s);
178 return;
179 }
25ef3192 180
36c19d03
PN
181 if (listLength(c->reply) == 0) {
182 listAddNodeTail(c->reply,createObject(REDIS_STRING,s));
609baba8 183 c->reply_bytes += zmalloc_size_sds(s);
36c19d03
PN
184 } else {
185 tail = listNodeValue(listLast(c->reply));
186
187 /* Append to this object when possible. */
188 if (tail->ptr != NULL &&
189 sdslen(tail->ptr)+sdslen(s) <= REDIS_REPLY_CHUNK_BYTES)
190 {
609baba8 191 c->reply_bytes -= zmalloc_size_sds(tail->ptr);
36c19d03
PN
192 tail = dupLastObjectIfNeeded(c->reply);
193 tail->ptr = sdscatlen(tail->ptr,s,sdslen(s));
609baba8 194 c->reply_bytes += zmalloc_size_sds(tail->ptr);
36c19d03 195 sdsfree(s);
834ef78e 196 } else {
36c19d03 197 listAddNodeTail(c->reply,createObject(REDIS_STRING,s));
609baba8 198 c->reply_bytes += zmalloc_size_sds(s);
834ef78e 199 }
36c19d03 200 }
7eac2a75 201 asyncCloseClientOnOutputBufferLimitReached(c);
36c19d03
PN
202}
203
204void _addReplyStringToList(redisClient *c, char *s, size_t len) {
205 robj *tail;
25ef3192 206
207 if (c->flags & REDIS_CLOSE_AFTER_REPLY) return;
208
36c19d03 209 if (listLength(c->reply) == 0) {
442246dd 210 robj *o = createStringObject(s,len);
211
212 listAddNodeTail(c->reply,o);
609baba8 213 c->reply_bytes += zmalloc_size_sds(o->ptr);
834ef78e 214 } else {
36c19d03
PN
215 tail = listNodeValue(listLast(c->reply));
216
217 /* Append to this object when possible. */
218 if (tail->ptr != NULL &&
219 sdslen(tail->ptr)+len <= REDIS_REPLY_CHUNK_BYTES)
220 {
609baba8 221 c->reply_bytes -= zmalloc_size_sds(tail->ptr);
36c19d03
PN
222 tail = dupLastObjectIfNeeded(c->reply);
223 tail->ptr = sdscatlen(tail->ptr,s,len);
609baba8 224 c->reply_bytes += zmalloc_size_sds(tail->ptr);
834ef78e 225 } else {
442246dd 226 robj *o = createStringObject(s,len);
227
228 listAddNodeTail(c->reply,o);
609baba8 229 c->reply_bytes += zmalloc_size_sds(o->ptr);
834ef78e
PN
230 }
231 }
7eac2a75 232 asyncCloseClientOnOutputBufferLimitReached(c);
834ef78e 233}
e2641e09 234
25ef3192 235/* -----------------------------------------------------------------------------
236 * Higher level functions to queue data on the client output buffer.
237 * The following functions are the ones that commands implementations will call.
238 * -------------------------------------------------------------------------- */
239
834ef78e 240void addReply(redisClient *c, robj *obj) {
51669c5a 241 if (prepareClientToWrite(c) != REDIS_OK) return;
4c2e506a 242
243 /* This is an important place where we can avoid copy-on-write
244 * when there is a saving child running, avoiding touching the
245 * refcount field of the object if it's not needed.
246 *
247 * If the encoding is RAW and there is room in the static buffer
248 * we'll be able to send the object to the client without
249 * messing with its page. */
250 if (obj->encoding == REDIS_ENCODING_RAW) {
251 if (_addReplyToBuffer(c,obj->ptr,sdslen(obj->ptr)) != REDIS_OK)
252 _addReplyObjectToList(c,obj);
51669c5a 253 } else if (obj->encoding == REDIS_ENCODING_INT) {
254 /* Optimization: if there is room in the static buffer for 32 bytes
255 * (more than the max chars a 64 bit integer can take as string) we
256 * avoid decoding the object and go for the lower level approach. */
257 if (listLength(c->reply) == 0 && (sizeof(c->buf) - c->bufpos) >= 32) {
258 char buf[32];
259 int len;
260
261 len = ll2string(buf,sizeof(buf),(long)obj->ptr);
262 if (_addReplyToBuffer(c,buf,len) == REDIS_OK)
263 return;
264 /* else... continue with the normal code path, but should never
265 * happen actually since we verified there is room. */
266 }
834ef78e 267 obj = getDecodedObject(obj);
4c2e506a 268 if (_addReplyToBuffer(c,obj->ptr,sdslen(obj->ptr)) != REDIS_OK)
269 _addReplyObjectToList(c,obj);
270 decrRefCount(obj);
51669c5a 271 } else {
272 redisPanic("Wrong obj->encoding in addReply()");
e2641e09 273 }
e2641e09 274}
275
276void addReplySds(redisClient *c, sds s) {
51669c5a 277 if (prepareClientToWrite(c) != REDIS_OK) {
cd76bb65
PN
278 /* The caller expects the sds to be free'd. */
279 sdsfree(s);
280 return;
281 }
36c19d03 282 if (_addReplyToBuffer(c,s,sdslen(s)) == REDIS_OK) {
834ef78e
PN
283 sdsfree(s);
284 } else {
36c19d03
PN
285 /* This method free's the sds when it is no longer needed. */
286 _addReplySdsToList(c,s);
834ef78e 287 }
e2641e09 288}
289
834ef78e 290void addReplyString(redisClient *c, char *s, size_t len) {
51669c5a 291 if (prepareClientToWrite(c) != REDIS_OK) return;
36c19d03
PN
292 if (_addReplyToBuffer(c,s,len) != REDIS_OK)
293 _addReplyStringToList(c,s,len);
834ef78e 294}
e2641e09 295
51669c5a 296void addReplyErrorLength(redisClient *c, char *s, size_t len) {
3ab20376
PN
297 addReplyString(c,"-ERR ",5);
298 addReplyString(c,s,len);
299 addReplyString(c,"\r\n",2);
e2641e09 300}
301
3ab20376 302void addReplyError(redisClient *c, char *err) {
51669c5a 303 addReplyErrorLength(c,err,strlen(err));
3ab20376 304}
e2641e09 305
3ab20376 306void addReplyErrorFormat(redisClient *c, const char *fmt, ...) {
3bb818df 307 size_t l, j;
3ab20376
PN
308 va_list ap;
309 va_start(ap,fmt);
310 sds s = sdscatvprintf(sdsempty(),fmt,ap);
311 va_end(ap);
3bb818df 312 /* Make sure there are no newlines in the string, otherwise invalid protocol
313 * is emitted. */
314 l = sdslen(s);
315 for (j = 0; j < l; j++) {
316 if (s[j] == '\r' || s[j] == '\n') s[j] = ' ';
317 }
51669c5a 318 addReplyErrorLength(c,s,sdslen(s));
3ab20376
PN
319 sdsfree(s);
320}
321
51669c5a 322void addReplyStatusLength(redisClient *c, char *s, size_t len) {
3ab20376
PN
323 addReplyString(c,"+",1);
324 addReplyString(c,s,len);
325 addReplyString(c,"\r\n",2);
326}
327
328void addReplyStatus(redisClient *c, char *status) {
51669c5a 329 addReplyStatusLength(c,status,strlen(status));
3ab20376
PN
330}
331
332void addReplyStatusFormat(redisClient *c, const char *fmt, ...) {
333 va_list ap;
334 va_start(ap,fmt);
335 sds s = sdscatvprintf(sdsempty(),fmt,ap);
336 va_end(ap);
51669c5a 337 addReplyStatusLength(c,s,sdslen(s));
3ab20376
PN
338 sdsfree(s);
339}
340
b301c1fc
PN
341/* Adds an empty object to the reply list that will contain the multi bulk
342 * length, which is not known when this function is called. */
343void *addDeferredMultiBulkLength(redisClient *c) {
4c2e506a 344 /* Note that we install the write event here even if the object is not
345 * ready to be sent, since we are sure that before returning to the
346 * event loop setDeferredMultiBulkLength() will be called. */
51669c5a 347 if (prepareClientToWrite(c) != REDIS_OK) return NULL;
36c19d03 348 listAddNodeTail(c->reply,createObject(REDIS_STRING,NULL));
b301c1fc
PN
349 return listLast(c->reply);
350}
351
352/* Populate the length object and try glueing it to the next chunk. */
353void setDeferredMultiBulkLength(redisClient *c, void *node, long length) {
354 listNode *ln = (listNode*)node;
355 robj *len, *next;
356
357 /* Abort when *node is NULL (see addDeferredMultiBulkLength). */
358 if (node == NULL) return;
359
360 len = listNodeValue(ln);
361 len->ptr = sdscatprintf(sdsempty(),"*%ld\r\n",length);
609baba8 362 c->reply_bytes += zmalloc_size_sds(len->ptr);
b301c1fc
PN
363 if (ln->next != NULL) {
364 next = listNodeValue(ln->next);
36c19d03 365
49128f0b 366 /* Only glue when the next node is non-NULL (an sds in this case) */
36c19d03 367 if (next->ptr != NULL) {
6fe9c402 368 c->reply_bytes -= zmalloc_size_sds(len->ptr);
369 c->reply_bytes -= zmalloc_size_sds(next->ptr);
49128f0b 370 len->ptr = sdscatlen(len->ptr,next->ptr,sdslen(next->ptr));
6fe9c402 371 c->reply_bytes += zmalloc_size_sds(len->ptr);
b301c1fc
PN
372 listDelNode(c->reply,ln->next);
373 }
e2641e09 374 }
7eac2a75 375 asyncCloseClientOnOutputBufferLimitReached(c);
b301c1fc
PN
376}
377
d51ebef5 378/* Add a duble as a bulk reply */
834ef78e
PN
379void addReplyDouble(redisClient *c, double d) {
380 char dbuf[128], sbuf[128];
381 int dlen, slen;
382 dlen = snprintf(dbuf,sizeof(dbuf),"%.17g",d);
383 slen = snprintf(sbuf,sizeof(sbuf),"$%d\r\n%s\r\n",dlen,dbuf);
384 addReplyString(c,sbuf,slen);
e2641e09 385}
386
d51ebef5 387/* Add a long long as integer reply or bulk len / multi bulk count.
388 * Basically this is used to output <prefix><long long><crlf>. */
51669c5a 389void addReplyLongLongWithPrefix(redisClient *c, long long ll, char prefix) {
e2641e09 390 char buf[128];
834ef78e 391 int len;
355f8591 392
393 /* Things like $3\r\n or *2\r\n are emitted very often by the protocol
394 * so we have a few shared objects to use if the integer is small
395 * like it is most of the times. */
396 if (prefix == '*' && ll < REDIS_SHARED_BULKHDR_LEN) {
397 addReply(c,shared.mbulkhdr[ll]);
398 return;
399 } else if (prefix == '$' && ll < REDIS_SHARED_BULKHDR_LEN) {
400 addReply(c,shared.bulkhdr[ll]);
401 return;
402 }
403
834ef78e 404 buf[0] = prefix;
e2641e09 405 len = ll2string(buf+1,sizeof(buf)-1,ll);
406 buf[len+1] = '\r';
407 buf[len+2] = '\n';
834ef78e 408 addReplyString(c,buf,len+3);
e2641e09 409}
410
834ef78e 411void addReplyLongLong(redisClient *c, long long ll) {
009db676 412 if (ll == 0)
413 addReply(c,shared.czero);
414 else if (ll == 1)
415 addReply(c,shared.cone);
416 else
51669c5a 417 addReplyLongLongWithPrefix(c,ll,':');
834ef78e 418}
e2641e09 419
0537e7bf 420void addReplyMultiBulkLen(redisClient *c, long length) {
51669c5a 421 addReplyLongLongWithPrefix(c,length,'*');
e2641e09 422}
423
d51ebef5 424/* Create the length prefix of a bulk reply, example: $2234 */
e2641e09 425void addReplyBulkLen(redisClient *c, robj *obj) {
834ef78e 426 size_t len;
e2641e09 427
428 if (obj->encoding == REDIS_ENCODING_RAW) {
429 len = sdslen(obj->ptr);
430 } else {
431 long n = (long)obj->ptr;
432
433 /* Compute how many bytes will take this integer as a radix 10 string */
434 len = 1;
435 if (n < 0) {
436 len++;
437 n = -n;
438 }
439 while((n = n/10) != 0) {
440 len++;
441 }
442 }
51669c5a 443 addReplyLongLongWithPrefix(c,len,'$');
e2641e09 444}
445
d51ebef5 446/* Add a Redis Object as a bulk reply */
e2641e09 447void addReplyBulk(redisClient *c, robj *obj) {
448 addReplyBulkLen(c,obj);
449 addReply(c,obj);
450 addReply(c,shared.crlf);
451}
452
d51ebef5 453/* Add a C buffer as bulk reply */
454void addReplyBulkCBuffer(redisClient *c, void *p, size_t len) {
51669c5a 455 addReplyLongLongWithPrefix(c,len,'$');
d51ebef5 456 addReplyString(c,p,len);
457 addReply(c,shared.crlf);
458}
459
460/* Add a C nul term string as bulk reply */
e2641e09 461void addReplyBulkCString(redisClient *c, char *s) {
462 if (s == NULL) {
463 addReply(c,shared.nullbulk);
464 } else {
d51ebef5 465 addReplyBulkCBuffer(c,s,strlen(s));
e2641e09 466 }
467}
468
d51ebef5 469/* Add a long long as a bulk reply */
470void addReplyBulkLongLong(redisClient *c, long long ll) {
471 char buf[64];
472 int len;
473
474 len = ll2string(buf,64,ll);
475 addReplyBulkCBuffer(c,buf,len);
476}
477
1824e3a3 478/* Copy 'src' client output buffers into 'dst' client output buffers.
479 * The function takes care of freeing the old output buffers of the
480 * destination client. */
481void copyClientOutputBuffer(redisClient *dst, redisClient *src) {
482 listRelease(dst->reply);
483 dst->reply = listDup(src->reply);
484 memcpy(dst->buf,src->buf,src->bufpos);
485 dst->bufpos = src->bufpos;
3853c168 486 dst->reply_bytes = src->reply_bytes;
1824e3a3 487}
488
ab17b909 489static void acceptCommonHandler(int fd) {
e2641e09 490 redisClient *c;
ab17b909 491 if ((c = createClient(fd)) == NULL) {
e2641e09 492 redisLog(REDIS_WARNING,"Error allocating resoures for the client");
ab17b909 493 close(fd); /* May be already closed, just ingore errors */
e2641e09 494 return;
495 }
496 /* If maxclient directive is set and this is one client more... close the
497 * connection. Note that we create the client instead to check before
498 * for this condition, since now the socket is already set in nonblocking
499 * mode and we can send an error for free using the Kernel I/O */
58732c23 500 if (listLength(server.clients) > server.maxclients) {
e2641e09 501 char *err = "-ERR max number of clients reached\r\n";
502
503 /* That's a best effort error message, don't check write errors */
504 if (write(c->fd,err,strlen(err)) == -1) {
505 /* Nothing to do, Just to avoid the warning... */
506 }
3c95e721 507 server.stat_rejected_conn++;
e2641e09 508 freeClient(c);
509 return;
510 }
511 server.stat_numconnections++;
512}
513
ab17b909
PN
514void acceptTcpHandler(aeEventLoop *el, int fd, void *privdata, int mask) {
515 int cport, cfd;
516 char cip[128];
517 REDIS_NOTUSED(el);
518 REDIS_NOTUSED(mask);
519 REDIS_NOTUSED(privdata);
520
521 cfd = anetTcpAccept(server.neterr, fd, cip, &cport);
522 if (cfd == AE_ERR) {
df541bea 523 redisLog(REDIS_WARNING,"Accepting client connection: %s", server.neterr);
ab17b909
PN
524 return;
525 }
526 redisLog(REDIS_VERBOSE,"Accepted %s:%d", cip, cport);
527 acceptCommonHandler(cfd);
528}
529
530void acceptUnixHandler(aeEventLoop *el, int fd, void *privdata, int mask) {
531 int cfd;
ab17b909
PN
532 REDIS_NOTUSED(el);
533 REDIS_NOTUSED(mask);
534 REDIS_NOTUSED(privdata);
535
4fe83b55 536 cfd = anetUnixAccept(server.neterr, fd);
ab17b909 537 if (cfd == AE_ERR) {
df541bea 538 redisLog(REDIS_WARNING,"Accepting client connection: %s", server.neterr);
ab17b909
PN
539 return;
540 }
541 redisLog(REDIS_VERBOSE,"Accepted connection to %s", server.unixsocket);
542 acceptCommonHandler(cfd);
543}
544
545
e2641e09 546static void freeClientArgv(redisClient *c) {
547 int j;
e2641e09 548 for (j = 0; j < c->argc; j++)
549 decrRefCount(c->argv[j]);
e2641e09 550 c->argc = 0;
09e2d9ee 551 c->cmd = NULL;
e2641e09 552}
553
ed4d4f11 554/* Close all the slaves connections. This is useful in chained replication
555 * when we resync with our own master and want to force all our slaves to
556 * resync with us as well. */
557void disconnectSlaves(void) {
558 while (listLength(server.slaves)) {
559 listNode *ln = listFirst(server.slaves);
560 freeClient((redisClient*)ln->value);
561 }
562}
563
e2641e09 564void freeClient(redisClient *c) {
565 listNode *ln;
566
00010fa9 567 /* If this is marked as current client unset it */
568 if (server.current_client == c) server.current_client = NULL;
569
e2641e09 570 /* Note that if the client we are freeing is blocked into a blocking
571 * call, we have to set querybuf to NULL *before* to call
572 * unblockClientWaitingData() to avoid processInputBuffer() will get
573 * called. Also it is important to remove the file events after
574 * this, because this call adds the READABLE event. */
575 sdsfree(c->querybuf);
576 c->querybuf = NULL;
577 if (c->flags & REDIS_BLOCKED)
578 unblockClientWaitingData(c);
579
580 /* UNWATCH all the keys */
581 unwatchAllKeys(c);
582 listRelease(c->watched_keys);
583 /* Unsubscribe from all the pubsub channels */
584 pubsubUnsubscribeAllChannels(c,0);
585 pubsubUnsubscribeAllPatterns(c,0);
586 dictRelease(c->pubsub_channels);
587 listRelease(c->pubsub_patterns);
588 /* Obvious cleanup */
589 aeDeleteFileEvent(server.el,c->fd,AE_READABLE);
590 aeDeleteFileEvent(server.el,c->fd,AE_WRITABLE);
591 listRelease(c->reply);
592 freeClientArgv(c);
593 close(c->fd);
594 /* Remove from the list of clients */
595 ln = listSearchKey(server.clients,c);
596 redisAssert(ln != NULL);
597 listDelNode(server.clients,ln);
3bcffcbe
PN
598 /* When client was just unblocked because of a blocking operation,
599 * remove it from the list with unblocked clients. */
600 if (c->flags & REDIS_UNBLOCKED) {
601 ln = listSearchKey(server.unblocked_clients,c);
602 redisAssert(ln != NULL);
603 listDelNode(server.unblocked_clients,ln);
604 }
e2641e09 605 listRelease(c->io_keys);
778b2210 606 /* Master/slave cleanup.
607 * Case 1: we lost the connection with a slave. */
e2641e09 608 if (c->flags & REDIS_SLAVE) {
609 if (c->replstate == REDIS_REPL_SEND_BULK && c->repldbfd != -1)
610 close(c->repldbfd);
611 list *l = (c->flags & REDIS_MONITOR) ? server.monitors : server.slaves;
612 ln = listSearchKey(l,c);
613 redisAssert(ln != NULL);
614 listDelNode(l,ln);
615 }
778b2210 616
617 /* Case 2: we lost the connection with the master. */
e2641e09 618 if (c->flags & REDIS_MASTER) {
619 server.master = NULL;
1844f990 620 server.repl_state = REDIS_REPL_CONNECT;
56ff70f8 621 server.repl_down_since = server.unixtime;
ed4d4f11 622 /* We lost connection with our master, force our slaves to resync
623 * with us as well to load the new data set.
d37299e3 624 *
f1e38b35 625 * If server.masterhost is NULL the user called SLAVEOF NO ONE so
ed4d4f11 626 * slave resync is not needed. */
627 if (server.masterhost != NULL) disconnectSlaves();
e2641e09 628 }
7eac2a75 629
630 /* If this client was scheduled for async freeing we need to remove it
631 * from the queue. */
632 if (c->flags & REDIS_CLOSE_ASAP) {
633 ln = listSearchKey(server.clients_to_close,c);
634 redisAssert(ln != NULL);
635 listDelNode(server.clients_to_close,ln);
636 }
637
e2641e09 638 /* Release memory */
639 zfree(c->argv);
e2641e09 640 freeClientMultiState(c);
641 zfree(c);
642}
643
7eac2a75 644/* Schedule a client to free it at a safe time in the serverCron() function.
645 * This function is useful when we need to terminate a client but we are in
646 * a context where calling freeClient() is not possible, because the client
647 * should be valid for the continuation of the flow of the program. */
648void freeClientAsync(redisClient *c) {
649 if (c->flags & REDIS_CLOSE_ASAP) return;
650 c->flags |= REDIS_CLOSE_ASAP;
651 listAddNodeTail(server.clients_to_close,c);
652}
653
654void freeClientsInAsyncFreeQueue(void) {
655 while (listLength(server.clients_to_close)) {
656 listNode *ln = listFirst(server.clients_to_close);
657 redisClient *c = listNodeValue(ln);
658
659 c->flags &= ~REDIS_CLOSE_ASAP;
660 freeClient(c);
661 listDelNode(server.clients_to_close,ln);
662 }
663}
664
e2641e09 665void sendReplyToClient(aeEventLoop *el, int fd, void *privdata, int mask) {
666 redisClient *c = privdata;
667 int nwritten = 0, totwritten = 0, objlen;
442246dd 668 size_t objmem;
e2641e09 669 robj *o;
670 REDIS_NOTUSED(el);
671 REDIS_NOTUSED(mask);
672
834ef78e
PN
673 while(c->bufpos > 0 || listLength(c->reply)) {
674 if (c->bufpos > 0) {
675 if (c->flags & REDIS_MASTER) {
676 /* Don't reply to a master */
677 nwritten = c->bufpos - c->sentlen;
678 } else {
679 nwritten = write(fd,c->buf+c->sentlen,c->bufpos-c->sentlen);
680 if (nwritten <= 0) break;
681 }
682 c->sentlen += nwritten;
683 totwritten += nwritten;
684
685 /* If the buffer was sent, set bufpos to zero to continue with
686 * the remainder of the reply. */
687 if (c->sentlen == c->bufpos) {
688 c->bufpos = 0;
689 c->sentlen = 0;
690 }
691 } else {
692 o = listNodeValue(listFirst(c->reply));
693 objlen = sdslen(o->ptr);
609baba8 694 objmem = zmalloc_size_sds(o->ptr);
e2641e09 695
834ef78e
PN
696 if (objlen == 0) {
697 listDelNode(c->reply,listFirst(c->reply));
698 continue;
699 }
e2641e09 700
834ef78e
PN
701 if (c->flags & REDIS_MASTER) {
702 /* Don't reply to a master */
703 nwritten = objlen - c->sentlen;
704 } else {
705 nwritten = write(fd, ((char*)o->ptr)+c->sentlen,objlen-c->sentlen);
706 if (nwritten <= 0) break;
707 }
708 c->sentlen += nwritten;
709 totwritten += nwritten;
e2641e09 710
834ef78e
PN
711 /* If we fully sent the object on head go to the next one */
712 if (c->sentlen == objlen) {
713 listDelNode(c->reply,listFirst(c->reply));
714 c->sentlen = 0;
442246dd 715 c->reply_bytes -= objmem;
834ef78e 716 }
e2641e09 717 }
f6b32c14 718 /* Note that we avoid to send more than REDIS_MAX_WRITE_PER_EVENT
e2641e09 719 * bytes, in a single threaded server it's a good idea to serve
720 * other clients as well, even if a very large request comes from
721 * super fast link that is always able to accept data (in real world
f6b32c14 722 * scenario think about 'KEYS *' against the loopback interface).
723 *
724 * However if we are over the maxmemory limit we ignore that and
725 * just deliver as much data as it is possible to deliver. */
726 if (totwritten > REDIS_MAX_WRITE_PER_EVENT &&
727 (server.maxmemory == 0 ||
728 zmalloc_used_memory() < server.maxmemory)) break;
e2641e09 729 }
730 if (nwritten == -1) {
731 if (errno == EAGAIN) {
732 nwritten = 0;
733 } else {
734 redisLog(REDIS_VERBOSE,
735 "Error writing to client: %s", strerror(errno));
736 freeClient(c);
737 return;
738 }
739 }
56ff70f8 740 if (totwritten > 0) c->lastinteraction = server.unixtime;
3bc89500 741 if (c->bufpos == 0 && listLength(c->reply) == 0) {
e2641e09 742 c->sentlen = 0;
743 aeDeleteFileEvent(server.el,c->fd,AE_WRITABLE);
941c9fa2
PN
744
745 /* Close connection after entire reply has been sent. */
cd8788f2 746 if (c->flags & REDIS_CLOSE_AFTER_REPLY) freeClient(c);
e2641e09 747 }
748}
749
e2641e09 750/* resetClient prepare the client to process the next command */
751void resetClient(redisClient *c) {
752 freeClientArgv(c);
cd8788f2
PN
753 c->reqtype = 0;
754 c->multibulklen = 0;
e2641e09 755 c->bulklen = -1;
6856c7b4 756 /* We clear the ASKING flag as well if we are not inside a MULTI. */
757 if (!(c->flags & REDIS_MULTI)) c->flags &= (~REDIS_ASKING);
e2641e09 758}
759
cd8788f2
PN
760int processInlineBuffer(redisClient *c) {
761 char *newline = strstr(c->querybuf,"\r\n");
762 int argc, j;
763 sds *argv;
764 size_t querylen;
765
766 /* Nothing to do without a \r\n */
11e0c4c5 767 if (newline == NULL) {
768 if (sdslen(c->querybuf) > REDIS_INLINE_MAX_SIZE) {
769 addReplyError(c,"Protocol error: too big inline request");
770 setProtocolError(c,0);
771 }
cd8788f2 772 return REDIS_ERR;
11e0c4c5 773 }
cd8788f2
PN
774
775 /* Split the input buffer up to the \r\n */
776 querylen = newline-(c->querybuf);
777 argv = sdssplitlen(c->querybuf,querylen," ",1,&argc);
778
779 /* Leave data after the first line of the query in the buffer */
780 c->querybuf = sdsrange(c->querybuf,querylen+2,-1);
781
782 /* Setup argv array on client structure */
783 if (c->argv) zfree(c->argv);
784 c->argv = zmalloc(sizeof(robj*)*argc);
785
786 /* Create redis objects for all arguments. */
787 for (c->argc = 0, j = 0; j < argc; j++) {
788 if (sdslen(argv[j])) {
789 c->argv[c->argc] = createObject(REDIS_STRING,argv[j]);
790 c->argc++;
791 } else {
792 sdsfree(argv[j]);
793 }
794 }
795 zfree(argv);
796 return REDIS_OK;
797}
798
799/* Helper function. Trims query buffer to make the function that processes
800 * multi bulk requests idempotent. */
801static void setProtocolError(redisClient *c, int pos) {
3e0a975e 802 if (server.verbosity >= REDIS_VERBOSE) {
803 sds client = getClientInfoString(c);
804 redisLog(REDIS_VERBOSE,
805 "Protocol error from client: %s", client);
806 sdsfree(client);
807 }
cd8788f2
PN
808 c->flags |= REDIS_CLOSE_AFTER_REPLY;
809 c->querybuf = sdsrange(c->querybuf,pos,-1);
810}
811
812int processMultibulkBuffer(redisClient *c) {
813 char *newline = NULL;
5af30201
PN
814 int pos = 0, ok;
815 long long ll;
cd8788f2
PN
816
817 if (c->multibulklen == 0) {
818 /* The client should have been reset */
eab0e26e 819 redisAssertWithInfo(c,NULL,c->argc == 0);
cd8788f2
PN
820
821 /* Multi bulk length cannot be read without a \r\n */
5af30201 822 newline = strchr(c->querybuf,'\r');
11e0c4c5 823 if (newline == NULL) {
824 if (sdslen(c->querybuf) > REDIS_INLINE_MAX_SIZE) {
825 addReplyError(c,"Protocol error: too big mbulk count string");
826 setProtocolError(c,0);
827 }
cd8788f2 828 return REDIS_ERR;
11e0c4c5 829 }
cd8788f2 830
bf9fd5ff
PN
831 /* Buffer should also contain \n */
832 if (newline-(c->querybuf) > ((signed)sdslen(c->querybuf)-2))
833 return REDIS_ERR;
834
cd8788f2
PN
835 /* We know for sure there is a whole line since newline != NULL,
836 * so go ahead and find out the multi bulk length. */
eab0e26e 837 redisAssertWithInfo(c,NULL,c->querybuf[0] == '*');
5af30201
PN
838 ok = string2ll(c->querybuf+1,newline-(c->querybuf+1),&ll);
839 if (!ok || ll > 1024*1024) {
b19c33d4
PN
840 addReplyError(c,"Protocol error: invalid multibulk length");
841 setProtocolError(c,pos);
842 return REDIS_ERR;
cd8788f2 843 }
af0e51f2
PN
844
845 pos = (newline-c->querybuf)+2;
846 if (ll <= 0) {
847 c->querybuf = sdsrange(c->querybuf,pos,-1);
848 return REDIS_OK;
849 }
850
5af30201 851 c->multibulklen = ll;
cd8788f2
PN
852
853 /* Setup argv array on client structure */
854 if (c->argv) zfree(c->argv);
855 c->argv = zmalloc(sizeof(robj*)*c->multibulklen);
cd8788f2
PN
856 }
857
eab0e26e 858 redisAssertWithInfo(c,NULL,c->multibulklen > 0);
cd8788f2
PN
859 while(c->multibulklen) {
860 /* Read bulk length if unknown */
861 if (c->bulklen == -1) {
5af30201 862 newline = strchr(c->querybuf+pos,'\r');
11e0c4c5 863 if (newline == NULL) {
864 if (sdslen(c->querybuf) > REDIS_INLINE_MAX_SIZE) {
865 addReplyError(c,"Protocol error: too big bulk count string");
866 setProtocolError(c,0);
867 }
bf9fd5ff 868 break;
11e0c4c5 869 }
bf9fd5ff
PN
870
871 /* Buffer should also contain \n */
872 if (newline-(c->querybuf) > ((signed)sdslen(c->querybuf)-2))
cd8788f2 873 break;
bf9fd5ff
PN
874
875 if (c->querybuf[pos] != '$') {
876 addReplyErrorFormat(c,
877 "Protocol error: expected '$', got '%c'",
878 c->querybuf[pos]);
879 setProtocolError(c,pos);
880 return REDIS_ERR;
e2641e09 881 }
bf9fd5ff
PN
882
883 ok = string2ll(c->querybuf+pos+1,newline-(c->querybuf+pos+1),&ll);
884 if (!ok || ll < 0 || ll > 512*1024*1024) {
885 addReplyError(c,"Protocol error: invalid bulk length");
886 setProtocolError(c,pos);
887 return REDIS_ERR;
888 }
889
890 pos += newline-(c->querybuf+pos)+2;
94d490b9 891 if (ll >= REDIS_MBULK_BIG_ARG) {
826b5beb 892 /* If we are going to read a large object from network
893 * try to make it likely that it will start at c->querybuf
894 * boundary so that we can optimized object creation
895 * avoiding a large copy of data. */
896 c->querybuf = sdsrange(c->querybuf,pos,-1);
897 pos = 0;
b9031458 898 /* Hint the sds library about the amount of bytes this string is
899 * going to contain. */
94d490b9 900 c->querybuf = sdsMakeRoomFor(c->querybuf,ll+2);
b9031458 901 }
bf9fd5ff 902 c->bulklen = ll;
cd8788f2
PN
903 }
904
905 /* Read bulk argument */
906 if (sdslen(c->querybuf)-pos < (unsigned)(c->bulklen+2)) {
907 /* Not enough data (+2 == trailing \r\n) */
908 break;
909 } else {
92170955 910 /* Optimization: if the buffer contanins JUST our bulk element
911 * instead of creating a new object by *copying* the sds we
912 * just use the current sds string. */
913 if (pos == 0 &&
94d490b9 914 c->bulklen >= REDIS_MBULK_BIG_ARG &&
92170955 915 (signed) sdslen(c->querybuf) == c->bulklen+2)
916 {
917 c->argv[c->argc++] = createObject(REDIS_STRING,c->querybuf);
918 sdsIncrLen(c->querybuf,-2); /* remove CRLF */
919 c->querybuf = sdsempty();
920 /* Assume that if we saw a fat argument we'll see another one
921 * likely... */
922 c->querybuf = sdsMakeRoomFor(c->querybuf,c->bulklen+2);
923 pos = 0;
924 } else {
925 c->argv[c->argc++] =
926 createStringObject(c->querybuf+pos,c->bulklen);
927 pos += c->bulklen+2;
928 }
cd8788f2
PN
929 c->bulklen = -1;
930 c->multibulklen--;
931 }
932 }
933
934 /* Trim to pos */
92170955 935 if (pos) c->querybuf = sdsrange(c->querybuf,pos,-1);
cd8788f2
PN
936
937 /* We're done when c->multibulk == 0 */
11e0c4c5 938 if (c->multibulklen == 0) return REDIS_OK;
939
940 /* Still not read to process the command */
cd8788f2
PN
941 return REDIS_ERR;
942}
943
944void processInputBuffer(redisClient *c) {
945 /* Keep processing while there is something in the input buffer */
946 while(sdslen(c->querybuf)) {
64f201c2
HW
947 /* Immediately abort if the client is in the middle of something. */
948 if (c->flags & REDIS_BLOCKED) return;
949
5e78edb3
PN
950 /* REDIS_CLOSE_AFTER_REPLY closes the connection once the reply is
951 * written to the client. Make sure to not let the reply grow after
952 * this flag has been set (i.e. don't process more commands). */
953 if (c->flags & REDIS_CLOSE_AFTER_REPLY) return;
cd8788f2
PN
954
955 /* Determine request type when unknown. */
956 if (!c->reqtype) {
957 if (c->querybuf[0] == '*') {
958 c->reqtype = REDIS_REQ_MULTIBULK;
e2641e09 959 } else {
cd8788f2 960 c->reqtype = REDIS_REQ_INLINE;
e2641e09 961 }
e2641e09 962 }
cd8788f2
PN
963
964 if (c->reqtype == REDIS_REQ_INLINE) {
965 if (processInlineBuffer(c) != REDIS_OK) break;
966 } else if (c->reqtype == REDIS_REQ_MULTIBULK) {
967 if (processMultibulkBuffer(c) != REDIS_OK) break;
968 } else {
969 redisPanic("Unknown request type");
e2641e09 970 }
cd8788f2
PN
971
972 /* Multibulk processing could see a <= 0 length. */
9da6caac
PN
973 if (c->argc == 0) {
974 resetClient(c);
975 } else {
976 /* Only reset the client when the command was executed. */
977 if (processCommand(c) == REDIS_OK)
978 resetClient(c);
979 }
e2641e09 980 }
981}
982
983void readQueryFromClient(aeEventLoop *el, int fd, void *privdata, int mask) {
984 redisClient *c = (redisClient*) privdata;
826b5beb 985 int nread, readlen;
b8d743e1 986 size_t qblen;
e2641e09 987 REDIS_NOTUSED(el);
988 REDIS_NOTUSED(mask);
989
00010fa9 990 server.current_client = c;
826b5beb 991 readlen = REDIS_IOBUF_LEN;
992 /* If this is a multi bulk request, and we are processing a bulk reply
993 * that is large enough, try to maximize the probabilty that the query
994 * buffer contains excatly the SDS string representing the object, even
995 * at the risk of requring more read(2) calls. This way the function
996 * processMultiBulkBuffer() can avoid copying buffers to create the
997 * Redis Object representing the argument. */
826b5beb 998 if (c->reqtype == REDIS_REQ_MULTIBULK && c->multibulklen && c->bulklen != -1
94d490b9 999 && c->bulklen >= REDIS_MBULK_BIG_ARG)
826b5beb 1000 {
1001 int remaining = (unsigned)(c->bulklen+2)-sdslen(c->querybuf);
1002
1003 if (remaining < readlen) readlen = remaining;
1004 }
826b5beb 1005
b8d743e1 1006 qblen = sdslen(c->querybuf);
9fa9ccb0 1007 if (c->querybuf_peak < qblen) c->querybuf_peak = qblen;
826b5beb 1008 c->querybuf = sdsMakeRoomFor(c->querybuf, readlen);
1009 nread = read(fd, c->querybuf+qblen, readlen);
e2641e09 1010 if (nread == -1) {
1011 if (errno == EAGAIN) {
1012 nread = 0;
1013 } else {
1014 redisLog(REDIS_VERBOSE, "Reading from client: %s",strerror(errno));
1015 freeClient(c);
1016 return;
1017 }
1018 } else if (nread == 0) {
1019 redisLog(REDIS_VERBOSE, "Client closed connection");
1020 freeClient(c);
1021 return;
1022 }
1023 if (nread) {
b8d743e1 1024 sdsIncrLen(c->querybuf,nread);
56ff70f8 1025 c->lastinteraction = server.unixtime;
e2641e09 1026 } else {
00010fa9 1027 server.current_client = NULL;
e2641e09 1028 return;
1029 }
becf5fdb 1030 if (sdslen(c->querybuf) > server.client_max_querybuf_len) {
63fd1399 1031 sds ci = getClientInfoString(c), bytes = sdsempty();
1032
1033 bytes = sdscatrepr(bytes,c->querybuf,64);
1034 redisLog(REDIS_WARNING,"Closing client that reached max query buffer length: %s (qbuf initial bytes: %s)", ci, bytes);
becf5fdb 1035 sdsfree(ci);
63fd1399 1036 sdsfree(bytes);
becf5fdb 1037 freeClient(c);
1038 return;
1039 }
e2641e09 1040 processInputBuffer(c);
00010fa9 1041 server.current_client = NULL;
e2641e09 1042}
7a1fd61e 1043
1044void getClientsMaxBuffers(unsigned long *longest_output_list,
1045 unsigned long *biggest_input_buffer) {
1046 redisClient *c;
1047 listNode *ln;
1048 listIter li;
1049 unsigned long lol = 0, bib = 0;
1050
1051 listRewind(server.clients,&li);
1052 while ((ln = listNext(&li)) != NULL) {
1053 c = listNodeValue(ln);
1054
1055 if (listLength(c->reply) > lol) lol = listLength(c->reply);
1056 if (sdslen(c->querybuf) > bib) bib = sdslen(c->querybuf);
1057 }
1058 *longest_output_list = lol;
1059 *biggest_input_buffer = bib;
1060}
1061
17d25a33 1062/* Turn a Redis client into an sds string representing its state. */
1063sds getClientInfoString(redisClient *client) {
6621b8ff 1064 char ip[32], flags[16], events[3], *p;
17d25a33 1065 int port;
6621b8ff 1066 int emask;
17d25a33 1067
7b845b62 1068 anetPeerToString(client->fd,ip,&port);
17d25a33 1069 p = flags;
1070 if (client->flags & REDIS_SLAVE) {
1071 if (client->flags & REDIS_MONITOR)
1072 *p++ = 'O';
1073 else
1074 *p++ = 'S';
1075 }
1076 if (client->flags & REDIS_MASTER) *p++ = 'M';
17d25a33 1077 if (client->flags & REDIS_MULTI) *p++ = 'x';
1078 if (client->flags & REDIS_BLOCKED) *p++ = 'b';
1079 if (client->flags & REDIS_DIRTY_CAS) *p++ = 'd';
1080 if (client->flags & REDIS_CLOSE_AFTER_REPLY) *p++ = 'c';
1081 if (client->flags & REDIS_UNBLOCKED) *p++ = 'u';
7eac2a75 1082 if (client->flags & REDIS_CLOSE_ASAP) *p++ = 'A';
afd0f06b 1083 if (p == flags) *p++ = 'N';
17d25a33 1084 *p++ = '\0';
6621b8ff 1085
1086 emask = client->fd == -1 ? 0 : aeGetFileEvents(server.el,client->fd);
1087 p = events;
1088 if (emask & AE_READABLE) *p++ = 'r';
1089 if (emask & AE_WRITABLE) *p++ = 'w';
1090 *p = '\0';
17d25a33 1091 return sdscatprintf(sdsempty(),
b162e6f1 1092 "addr=%s:%d fd=%d age=%ld idle=%ld flags=%s db=%d sub=%d psub=%d multi=%d qbuf=%lu qbuf-free=%lu obl=%lu oll=%lu omem=%lu events=%s cmd=%s",
17d25a33 1093 ip,port,client->fd,
56ff70f8
PH
1094 (long)(server.unixtime - client->ctime),
1095 (long)(server.unixtime - client->lastinteraction),
17d25a33 1096 flags,
1097 client->db->id,
1098 (int) dictSize(client->pubsub_channels),
491c1c4e 1099 (int) listLength(client->pubsub_patterns),
b162e6f1 1100 (client->flags & REDIS_MULTI) ? client->mstate.count : -1,
491c1c4e 1101 (unsigned long) sdslen(client->querybuf),
57a5e54d 1102 (unsigned long) sdsavail(client->querybuf),
491c1c4e 1103 (unsigned long) client->bufpos,
6621b8ff 1104 (unsigned long) listLength(client->reply),
3853c168 1105 getClientOutputBufferMemoryUsage(client),
2c74a9f9 1106 events,
1107 client->lastcmd ? client->lastcmd->name : "NULL");
17d25a33 1108}
1109
45e7a1ce 1110sds getAllClientsInfoString(void) {
1111 listNode *ln;
1112 listIter li;
1113 redisClient *client;
1114 sds o = sdsempty();
1115
1116 listRewind(server.clients,&li);
1117 while ((ln = listNext(&li)) != NULL) {
0a466a75 1118 sds cs;
1119
45e7a1ce 1120 client = listNodeValue(ln);
0a466a75 1121 cs = getClientInfoString(client);
1122 o = sdscatsds(o,cs);
1123 sdsfree(cs);
45e7a1ce 1124 o = sdscatlen(o,"\n",1);
1125 }
1126 return o;
1127}
1128
3cd12b56 1129void clientCommand(redisClient *c) {
b93fdb7b 1130 listNode *ln;
1131 listIter li;
1132 redisClient *client;
1133
3cd12b56 1134 if (!strcasecmp(c->argv[1]->ptr,"list") && c->argc == 2) {
45e7a1ce 1135 sds o = getAllClientsInfoString();
3cd12b56 1136 addReplyBulkCBuffer(c,o,sdslen(o));
1137 sdsfree(o);
b93fdb7b 1138 } else if (!strcasecmp(c->argv[1]->ptr,"kill") && c->argc == 3) {
1139 listRewind(server.clients,&li);
1140 while ((ln = listNext(&li)) != NULL) {
1141 char ip[32], addr[64];
1142 int port;
1143
1144 client = listNodeValue(ln);
1145 if (anetPeerToString(client->fd,ip,&port) == -1) continue;
1146 snprintf(addr,sizeof(addr),"%s:%d",ip,port);
1147 if (strcmp(addr,c->argv[2]->ptr) == 0) {
1148 addReply(c,shared.ok);
1149 if (c == client) {
1150 client->flags |= REDIS_CLOSE_AFTER_REPLY;
1151 } else {
1152 freeClient(client);
1153 }
1154 return;
1155 }
1156 }
1157 addReplyError(c,"No such client");
3cd12b56 1158 } else {
1159 addReplyError(c, "Syntax error, try CLIENT (LIST | KILL ip:port)");
1160 }
1161}
c1c9d551 1162
4dd444bb 1163/* Rewrite the command vector of the client. All the new objects ref count
1164 * is incremented. The old command vector is freed, and the old objects
1165 * ref count is decremented. */
c1c9d551 1166void rewriteClientCommandVector(redisClient *c, int argc, ...) {
1167 va_list ap;
1168 int j;
1169 robj **argv; /* The new argument vector */
1170
1171 argv = zmalloc(sizeof(robj*)*argc);
1172 va_start(ap,argc);
1173 for (j = 0; j < argc; j++) {
1174 robj *a;
1175
1176 a = va_arg(ap, robj*);
1177 argv[j] = a;
1178 incrRefCount(a);
1179 }
1180 /* We free the objects in the original vector at the end, so we are
1181 * sure that if the same objects are reused in the new vector the
1182 * refcount gets incremented before it gets decremented. */
1183 for (j = 0; j < c->argc; j++) decrRefCount(c->argv[j]);
1184 zfree(c->argv);
1185 /* Replace argv and argc with our new versions. */
1186 c->argv = argv;
1187 c->argc = argc;
09e2d9ee 1188 c->cmd = lookupCommand(c->argv[0]->ptr);
eab0e26e 1189 redisAssertWithInfo(c,NULL,c->cmd != NULL);
c1c9d551 1190 va_end(ap);
1191}
4dd444bb 1192
1193/* Rewrite a single item in the command vector.
1194 * The new val ref count is incremented, and the old decremented. */
1195void rewriteClientCommandArgument(redisClient *c, int i, robj *newval) {
1196 robj *oldval;
1197
eab0e26e 1198 redisAssertWithInfo(c,NULL,i < c->argc);
4dd444bb 1199 oldval = c->argv[i];
1200 c->argv[i] = newval;
1201 incrRefCount(newval);
1202 decrRefCount(oldval);
1203
1204 /* If this is the command name make sure to fix c->cmd. */
1205 if (i == 0) {
1206 c->cmd = lookupCommand(c->argv[0]->ptr);
eab0e26e 1207 redisAssertWithInfo(c,NULL,c->cmd != NULL);
4dd444bb 1208 }
1209}
3853c168 1210
1211/* This function returns the number of bytes that Redis is virtually
1212 * using to store the reply still not read by the client.
1213 * It is "virtual" since the reply output list may contain objects that
1214 * are shared and are not really using additional memory.
1215 *
1216 * The function returns the total sum of the length of all the objects
1217 * stored in the output list, plus the memory used to allocate every
1218 * list node. The static reply buffer is not taken into account since it
1219 * is allocated anyway.
1220 *
1221 * Note: this function is very fast so can be called as many time as
1222 * the caller wishes. The main usage of this function currently is
2f0f0d95 1223 * enforcing the client output length limits. */
3853c168 1224unsigned long getClientOutputBufferMemoryUsage(redisClient *c) {
442246dd 1225 unsigned long list_item_size = sizeof(listNode)+sizeof(robj);
3853c168 1226
1227 return c->reply_bytes + (list_item_size*listLength(c->reply));
1228}
498dc555 1229
1230/* Get the class of a client, used in order to envorce limits to different
1231 * classes of clients.
1232 *
1233 * The function will return one of the following:
1234 * REDIS_CLIENT_LIMIT_CLASS_NORMAL -> Normal client
1235 * REDIS_CLIENT_LIMIT_CLASS_SLAVE -> Slave or client executing MONITOR command
1236 * REDIS_CLIENT_LIMIT_CLASS_PUBSUB -> Client subscribed to Pub/Sub channels
1237 */
1238int getClientLimitClass(redisClient *c) {
1239 if (c->flags & REDIS_SLAVE) return REDIS_CLIENT_LIMIT_CLASS_SLAVE;
1240 if (dictSize(c->pubsub_channels) || listLength(c->pubsub_patterns))
1241 return REDIS_CLIENT_LIMIT_CLASS_PUBSUB;
1242 return REDIS_CLIENT_LIMIT_CLASS_NORMAL;
1243}
7eac2a75 1244
7fe8d49a 1245int getClientLimitClassByName(char *name) {
1246 if (!strcasecmp(name,"normal")) return REDIS_CLIENT_LIMIT_CLASS_NORMAL;
1247 else if (!strcasecmp(name,"slave")) return REDIS_CLIENT_LIMIT_CLASS_SLAVE;
c715c9b8 1248 else if (!strcasecmp(name,"pubsub")) return REDIS_CLIENT_LIMIT_CLASS_PUBSUB;
7fe8d49a 1249 else return -1;
1250}
1251
1252char *getClientLimitClassName(int class) {
1253 switch(class) {
1254 case REDIS_CLIENT_LIMIT_CLASS_NORMAL: return "normal";
1255 case REDIS_CLIENT_LIMIT_CLASS_SLAVE: return "slave";
1256 case REDIS_CLIENT_LIMIT_CLASS_PUBSUB: return "pubsub";
1257 default: return NULL;
1258 }
1259}
1260
7eac2a75 1261/* The function checks if the client reached output buffer soft or hard
1262 * limit, and also update the state needed to check the soft limit as
1263 * a side effect.
1264 *
1265 * Return value: non-zero if the client reached the soft or the hard limit.
1266 * Otherwise zero is returned. */
1267int checkClientOutputBufferLimits(redisClient *c) {
1268 int soft = 0, hard = 0, class;
1269 unsigned long used_mem = getClientOutputBufferMemoryUsage(c);
1270
1271 class = getClientLimitClass(c);
1272 if (server.client_obuf_limits[class].hard_limit_bytes &&
1273 used_mem >= server.client_obuf_limits[class].hard_limit_bytes)
1274 hard = 1;
1275 if (server.client_obuf_limits[class].soft_limit_bytes &&
1276 used_mem >= server.client_obuf_limits[class].soft_limit_bytes)
1277 soft = 1;
1278
1279 /* We need to check if the soft limit is reached continuously for the
1280 * specified amount of seconds. */
1281 if (soft) {
1282 if (c->obuf_soft_limit_reached_time == 0) {
1283 c->obuf_soft_limit_reached_time = server.unixtime;
1284 soft = 0; /* First time we see the soft limit reached */
1285 } else {
1286 time_t elapsed = server.unixtime - c->obuf_soft_limit_reached_time;
1287
1288 if (elapsed <=
1289 server.client_obuf_limits[class].soft_limit_seconds) {
1290 soft = 0; /* The client still did not reached the max number of
1291 seconds for the soft limit to be considered
1292 reached. */
1293 }
1294 }
1295 } else {
1296 c->obuf_soft_limit_reached_time = 0;
1297 }
1298 return soft || hard;
1299}
1300
1301/* Asynchronously close a client if soft or hard limit is reached on the
06b3dced 1302 * output buffer size. The caller can check if the client will be closed
1303 * checking if the client REDIS_CLOSE_ASAP flag is set.
7eac2a75 1304 *
1305 * Note: we need to close the client asynchronously because this function is
1306 * called from contexts where the client can't be freed safely, i.e. from the
1307 * lower level functions pushing data inside the client output buffers. */
06b3dced 1308void asyncCloseClientOnOutputBufferLimitReached(redisClient *c) {
6fe9c402 1309 redisAssert(c->reply_bytes < ULONG_MAX-(1024*64));
6e09ad1c 1310 if (c->reply_bytes == 0 || c->flags & REDIS_CLOSE_ASAP) return;
7eac2a75 1311 if (checkClientOutputBufferLimits(c)) {
1312 sds client = getClientInfoString(c);
1313
1314 freeClientAsync(c);
7957c676 1315 redisLog(REDIS_WARNING,"Client %s scheduled to be closed ASAP for overcoming of output buffer limits.", client);
7eac2a75 1316 sdsfree(client);
7eac2a75 1317 }
1318}
8b7c3455 1319
1320/* Helper function used by freeMemoryIfNeeded() in order to flush slaves
1321 * output buffers without returning control to the event loop. */
1322void flushSlavesOutputBuffers(void) {
1323 listIter li;
1324 listNode *ln;
1325
1326 listRewind(server.slaves,&li);
1327 while((ln = listNext(&li))) {
1328 redisClient *slave = listNodeValue(ln);
1329 int events;
1330
1331 events = aeGetFileEvents(server.el,slave->fd);
1332 if (events & AE_WRITABLE &&
1333 slave->replstate == REDIS_REPL_ONLINE &&
1334 listLength(slave->reply))
1335 {
1336 sendReplyToClient(server.el,slave->fd,slave,0);
1337 }
1338 }
1339}