]> git.saurik.com Git - redis.git/blob - src/networking.c
df059f6ba8dae47ab5e79d0221c776eaaac24191
[redis.git] / src / networking.c
1 #include "redis.h"
2 #include <sys/uio.h>
3
4 void *dupClientReplyValue(void *o) {
5 incrRefCount((robj*)o);
6 return o;
7 }
8
9 int listMatchObjects(void *a, void *b) {
10 return equalStringObjects(a,b);
11 }
12
13 redisClient *createClient(int fd) {
14 redisClient *c = zmalloc(sizeof(redisClient));
15 c->bufpos = 0;
16
17 /* passing -1 as fd it is possible to create a non connected client.
18 * This is useful since all the Redis commands needs to be executed
19 * in the context of a client. When commands are executed in other
20 * contexts (for instance a Lua script) we need a non connected client. */
21 if (fd != -1) {
22 anetNonBlock(NULL,fd);
23 anetTcpNoDelay(NULL,fd);
24 if (aeCreateFileEvent(server.el,fd,AE_READABLE,
25 readQueryFromClient, c) == AE_ERR)
26 {
27 close(fd);
28 zfree(c);
29 return NULL;
30 }
31 }
32
33 selectDb(c,0);
34 c->fd = fd;
35 c->querybuf = sdsempty();
36 c->reqtype = 0;
37 c->argc = 0;
38 c->argv = NULL;
39 c->cmd = c->lastcmd = NULL;
40 c->multibulklen = 0;
41 c->bulklen = -1;
42 c->sentlen = 0;
43 c->flags = 0;
44 c->lastinteraction = time(NULL);
45 c->authenticated = 0;
46 c->replstate = REDIS_REPL_NONE;
47 c->reply = listCreate();
48 listSetFreeMethod(c->reply,decrRefCount);
49 listSetDupMethod(c->reply,dupClientReplyValue);
50 c->bpop.keys = NULL;
51 c->bpop.count = 0;
52 c->bpop.timeout = 0;
53 c->bpop.target = NULL;
54 c->io_keys = listCreate();
55 c->watched_keys = listCreate();
56 listSetFreeMethod(c->io_keys,decrRefCount);
57 c->pubsub_channels = dictCreate(&setDictType,NULL);
58 c->pubsub_patterns = listCreate();
59 listSetFreeMethod(c->pubsub_patterns,decrRefCount);
60 listSetMatchMethod(c->pubsub_patterns,listMatchObjects);
61 if (fd != -1) listAddNodeTail(server.clients,c);
62 initClientMultiState(c);
63 return c;
64 }
65
66 /* Set the event loop to listen for write events on the client's socket.
67 * Typically gets called every time a reply is built. */
68 int _installWriteEvent(redisClient *c) {
69 if (c->flags & REDIS_LUA_CLIENT) return REDIS_OK;
70 if (c->fd <= 0) return REDIS_ERR;
71 if (c->bufpos == 0 && listLength(c->reply) == 0 &&
72 (c->replstate == REDIS_REPL_NONE ||
73 c->replstate == REDIS_REPL_ONLINE) &&
74 aeCreateFileEvent(server.el, c->fd, AE_WRITABLE,
75 sendReplyToClient, c) == AE_ERR) return REDIS_ERR;
76 return REDIS_OK;
77 }
78
79 /* Create a duplicate of the last object in the reply list when
80 * it is not exclusively owned by the reply list. */
81 robj *dupLastObjectIfNeeded(list *reply) {
82 robj *new, *cur;
83 listNode *ln;
84 redisAssert(listLength(reply) > 0);
85 ln = listLast(reply);
86 cur = listNodeValue(ln);
87 if (cur->refcount > 1) {
88 new = dupStringObject(cur);
89 decrRefCount(cur);
90 listNodeValue(ln) = new;
91 }
92 return listNodeValue(ln);
93 }
94
95 /* -----------------------------------------------------------------------------
96 * Low level functions to add more data to output buffers.
97 * -------------------------------------------------------------------------- */
98
99 int _addReplyToBuffer(redisClient *c, char *s, size_t len) {
100 size_t available = sizeof(c->buf)-c->bufpos;
101
102 if (c->flags & REDIS_CLOSE_AFTER_REPLY) return REDIS_OK;
103
104 /* If there already are entries in the reply list, we cannot
105 * add anything more to the static buffer. */
106 if (listLength(c->reply) > 0) return REDIS_ERR;
107
108 /* Check that the buffer has enough space available for this string. */
109 if (len > available) return REDIS_ERR;
110
111 memcpy(c->buf+c->bufpos,s,len);
112 c->bufpos+=len;
113 return REDIS_OK;
114 }
115
116 void _addReplyObjectToList(redisClient *c, robj *o) {
117 robj *tail;
118
119 if (c->flags & REDIS_CLOSE_AFTER_REPLY) return;
120
121 if (listLength(c->reply) == 0) {
122 incrRefCount(o);
123 listAddNodeTail(c->reply,o);
124 } else {
125 tail = listNodeValue(listLast(c->reply));
126
127 /* Append to this object when possible. */
128 if (tail->ptr != NULL &&
129 sdslen(tail->ptr)+sdslen(o->ptr) <= REDIS_REPLY_CHUNK_BYTES)
130 {
131 tail = dupLastObjectIfNeeded(c->reply);
132 tail->ptr = sdscatlen(tail->ptr,o->ptr,sdslen(o->ptr));
133 } else {
134 incrRefCount(o);
135 listAddNodeTail(c->reply,o);
136 }
137 }
138 }
139
140 /* This method takes responsibility over the sds. When it is no longer
141 * needed it will be free'd, otherwise it ends up in a robj. */
142 void _addReplySdsToList(redisClient *c, sds s) {
143 robj *tail;
144
145 if (c->flags & REDIS_CLOSE_AFTER_REPLY) {
146 sdsfree(s);
147 return;
148 }
149
150 if (listLength(c->reply) == 0) {
151 listAddNodeTail(c->reply,createObject(REDIS_STRING,s));
152 } else {
153 tail = listNodeValue(listLast(c->reply));
154
155 /* Append to this object when possible. */
156 if (tail->ptr != NULL &&
157 sdslen(tail->ptr)+sdslen(s) <= REDIS_REPLY_CHUNK_BYTES)
158 {
159 tail = dupLastObjectIfNeeded(c->reply);
160 tail->ptr = sdscatlen(tail->ptr,s,sdslen(s));
161 sdsfree(s);
162 } else {
163 listAddNodeTail(c->reply,createObject(REDIS_STRING,s));
164 }
165 }
166 }
167
168 void _addReplyStringToList(redisClient *c, char *s, size_t len) {
169 robj *tail;
170
171 if (c->flags & REDIS_CLOSE_AFTER_REPLY) return;
172
173 if (listLength(c->reply) == 0) {
174 listAddNodeTail(c->reply,createStringObject(s,len));
175 } else {
176 tail = listNodeValue(listLast(c->reply));
177
178 /* Append to this object when possible. */
179 if (tail->ptr != NULL &&
180 sdslen(tail->ptr)+len <= REDIS_REPLY_CHUNK_BYTES)
181 {
182 tail = dupLastObjectIfNeeded(c->reply);
183 tail->ptr = sdscatlen(tail->ptr,s,len);
184 } else {
185 listAddNodeTail(c->reply,createStringObject(s,len));
186 }
187 }
188 }
189
190 /* -----------------------------------------------------------------------------
191 * Higher level functions to queue data on the client output buffer.
192 * The following functions are the ones that commands implementations will call.
193 * -------------------------------------------------------------------------- */
194
195 void addReply(redisClient *c, robj *obj) {
196 if (_installWriteEvent(c) != REDIS_OK) return;
197
198 /* This is an important place where we can avoid copy-on-write
199 * when there is a saving child running, avoiding touching the
200 * refcount field of the object if it's not needed.
201 *
202 * If the encoding is RAW and there is room in the static buffer
203 * we'll be able to send the object to the client without
204 * messing with its page. */
205 if (obj->encoding == REDIS_ENCODING_RAW) {
206 if (_addReplyToBuffer(c,obj->ptr,sdslen(obj->ptr)) != REDIS_OK)
207 _addReplyObjectToList(c,obj);
208 } else {
209 /* FIXME: convert the long into string and use _addReplyToBuffer()
210 * instead of calling getDecodedObject. As this place in the
211 * code is too performance critical. */
212 obj = getDecodedObject(obj);
213 if (_addReplyToBuffer(c,obj->ptr,sdslen(obj->ptr)) != REDIS_OK)
214 _addReplyObjectToList(c,obj);
215 decrRefCount(obj);
216 }
217 }
218
219 void addReplySds(redisClient *c, sds s) {
220 if (_installWriteEvent(c) != REDIS_OK) {
221 /* The caller expects the sds to be free'd. */
222 sdsfree(s);
223 return;
224 }
225 if (_addReplyToBuffer(c,s,sdslen(s)) == REDIS_OK) {
226 sdsfree(s);
227 } else {
228 /* This method free's the sds when it is no longer needed. */
229 _addReplySdsToList(c,s);
230 }
231 }
232
233 void addReplyString(redisClient *c, char *s, size_t len) {
234 if (_installWriteEvent(c) != REDIS_OK) return;
235 if (_addReplyToBuffer(c,s,len) != REDIS_OK)
236 _addReplyStringToList(c,s,len);
237 }
238
239 void _addReplyError(redisClient *c, char *s, size_t len) {
240 addReplyString(c,"-ERR ",5);
241 addReplyString(c,s,len);
242 addReplyString(c,"\r\n",2);
243 }
244
245 void addReplyError(redisClient *c, char *err) {
246 _addReplyError(c,err,strlen(err));
247 }
248
249 void addReplyErrorFormat(redisClient *c, const char *fmt, ...) {
250 size_t l, j;
251 va_list ap;
252 va_start(ap,fmt);
253 sds s = sdscatvprintf(sdsempty(),fmt,ap);
254 va_end(ap);
255 /* Make sure there are no newlines in the string, otherwise invalid protocol
256 * is emitted. */
257 l = sdslen(s);
258 for (j = 0; j < l; j++) {
259 if (s[j] == '\r' || s[j] == '\n') s[j] = ' ';
260 }
261 _addReplyError(c,s,sdslen(s));
262 sdsfree(s);
263 }
264
265 void _addReplyStatus(redisClient *c, char *s, size_t len) {
266 addReplyString(c,"+",1);
267 addReplyString(c,s,len);
268 addReplyString(c,"\r\n",2);
269 }
270
271 void addReplyStatus(redisClient *c, char *status) {
272 _addReplyStatus(c,status,strlen(status));
273 }
274
275 void addReplyStatusFormat(redisClient *c, const char *fmt, ...) {
276 va_list ap;
277 va_start(ap,fmt);
278 sds s = sdscatvprintf(sdsempty(),fmt,ap);
279 va_end(ap);
280 _addReplyStatus(c,s,sdslen(s));
281 sdsfree(s);
282 }
283
284 /* Adds an empty object to the reply list that will contain the multi bulk
285 * length, which is not known when this function is called. */
286 void *addDeferredMultiBulkLength(redisClient *c) {
287 /* Note that we install the write event here even if the object is not
288 * ready to be sent, since we are sure that before returning to the
289 * event loop setDeferredMultiBulkLength() will be called. */
290 if (_installWriteEvent(c) != REDIS_OK) return NULL;
291 listAddNodeTail(c->reply,createObject(REDIS_STRING,NULL));
292 return listLast(c->reply);
293 }
294
295 /* Populate the length object and try glueing it to the next chunk. */
296 void setDeferredMultiBulkLength(redisClient *c, void *node, long length) {
297 listNode *ln = (listNode*)node;
298 robj *len, *next;
299
300 /* Abort when *node is NULL (see addDeferredMultiBulkLength). */
301 if (node == NULL) return;
302
303 len = listNodeValue(ln);
304 len->ptr = sdscatprintf(sdsempty(),"*%ld\r\n",length);
305 if (ln->next != NULL) {
306 next = listNodeValue(ln->next);
307
308 /* Only glue when the next node is non-NULL (an sds in this case) */
309 if (next->ptr != NULL) {
310 len->ptr = sdscatlen(len->ptr,next->ptr,sdslen(next->ptr));
311 listDelNode(c->reply,ln->next);
312 }
313 }
314 }
315
316 /* Add a duble as a bulk reply */
317 void addReplyDouble(redisClient *c, double d) {
318 char dbuf[128], sbuf[128];
319 int dlen, slen;
320 dlen = snprintf(dbuf,sizeof(dbuf),"%.17g",d);
321 slen = snprintf(sbuf,sizeof(sbuf),"$%d\r\n%s\r\n",dlen,dbuf);
322 addReplyString(c,sbuf,slen);
323 }
324
325 /* Add a long long as integer reply or bulk len / multi bulk count.
326 * Basically this is used to output <prefix><long long><crlf>. */
327 void _addReplyLongLong(redisClient *c, long long ll, char prefix) {
328 char buf[128];
329 int len;
330 buf[0] = prefix;
331 len = ll2string(buf+1,sizeof(buf)-1,ll);
332 buf[len+1] = '\r';
333 buf[len+2] = '\n';
334 addReplyString(c,buf,len+3);
335 }
336
337 void addReplyLongLong(redisClient *c, long long ll) {
338 if (ll == 0)
339 addReply(c,shared.czero);
340 else if (ll == 1)
341 addReply(c,shared.cone);
342 else
343 _addReplyLongLong(c,ll,':');
344 }
345
346 void addReplyMultiBulkLen(redisClient *c, long length) {
347 _addReplyLongLong(c,length,'*');
348 }
349
350 /* Create the length prefix of a bulk reply, example: $2234 */
351 void addReplyBulkLen(redisClient *c, robj *obj) {
352 size_t len;
353
354 if (obj->encoding == REDIS_ENCODING_RAW) {
355 len = sdslen(obj->ptr);
356 } else {
357 long n = (long)obj->ptr;
358
359 /* Compute how many bytes will take this integer as a radix 10 string */
360 len = 1;
361 if (n < 0) {
362 len++;
363 n = -n;
364 }
365 while((n = n/10) != 0) {
366 len++;
367 }
368 }
369 _addReplyLongLong(c,len,'$');
370 }
371
372 /* Add a Redis Object as a bulk reply */
373 void addReplyBulk(redisClient *c, robj *obj) {
374 addReplyBulkLen(c,obj);
375 addReply(c,obj);
376 addReply(c,shared.crlf);
377 }
378
379 /* Add a C buffer as bulk reply */
380 void addReplyBulkCBuffer(redisClient *c, void *p, size_t len) {
381 _addReplyLongLong(c,len,'$');
382 addReplyString(c,p,len);
383 addReply(c,shared.crlf);
384 }
385
386 /* Add a C nul term string as bulk reply */
387 void addReplyBulkCString(redisClient *c, char *s) {
388 if (s == NULL) {
389 addReply(c,shared.nullbulk);
390 } else {
391 addReplyBulkCBuffer(c,s,strlen(s));
392 }
393 }
394
395 /* Add a long long as a bulk reply */
396 void addReplyBulkLongLong(redisClient *c, long long ll) {
397 char buf[64];
398 int len;
399
400 len = ll2string(buf,64,ll);
401 addReplyBulkCBuffer(c,buf,len);
402 }
403
404 /* Copy 'src' client output buffers into 'dst' client output buffers.
405 * The function takes care of freeing the old output buffers of the
406 * destination client. */
407 void copyClientOutputBuffer(redisClient *dst, redisClient *src) {
408 listRelease(dst->reply);
409 dst->reply = listDup(src->reply);
410 memcpy(dst->buf,src->buf,src->bufpos);
411 dst->bufpos = src->bufpos;
412 }
413
414 static void acceptCommonHandler(int fd) {
415 redisClient *c;
416 if ((c = createClient(fd)) == NULL) {
417 redisLog(REDIS_WARNING,"Error allocating resoures for the client");
418 close(fd); /* May be already closed, just ingore errors */
419 return;
420 }
421 /* If maxclient directive is set and this is one client more... close the
422 * connection. Note that we create the client instead to check before
423 * for this condition, since now the socket is already set in nonblocking
424 * mode and we can send an error for free using the Kernel I/O */
425 if (listLength(server.clients) > server.maxclients) {
426 char *err = "-ERR max number of clients reached\r\n";
427
428 /* That's a best effort error message, don't check write errors */
429 if (write(c->fd,err,strlen(err)) == -1) {
430 /* Nothing to do, Just to avoid the warning... */
431 }
432 server.stat_rejected_conn++;
433 freeClient(c);
434 return;
435 }
436 server.stat_numconnections++;
437 }
438
439 void acceptTcpHandler(aeEventLoop *el, int fd, void *privdata, int mask) {
440 int cport, cfd;
441 char cip[128];
442 REDIS_NOTUSED(el);
443 REDIS_NOTUSED(mask);
444 REDIS_NOTUSED(privdata);
445
446 cfd = anetTcpAccept(server.neterr, fd, cip, &cport);
447 if (cfd == AE_ERR) {
448 redisLog(REDIS_WARNING,"Accepting client connection: %s", server.neterr);
449 return;
450 }
451 redisLog(REDIS_VERBOSE,"Accepted %s:%d", cip, cport);
452 acceptCommonHandler(cfd);
453 }
454
455 void acceptUnixHandler(aeEventLoop *el, int fd, void *privdata, int mask) {
456 int cfd;
457 REDIS_NOTUSED(el);
458 REDIS_NOTUSED(mask);
459 REDIS_NOTUSED(privdata);
460
461 cfd = anetUnixAccept(server.neterr, fd);
462 if (cfd == AE_ERR) {
463 redisLog(REDIS_WARNING,"Accepting client connection: %s", server.neterr);
464 return;
465 }
466 redisLog(REDIS_VERBOSE,"Accepted connection to %s", server.unixsocket);
467 acceptCommonHandler(cfd);
468 }
469
470
471 static void freeClientArgv(redisClient *c) {
472 int j;
473 for (j = 0; j < c->argc; j++)
474 decrRefCount(c->argv[j]);
475 c->argc = 0;
476 c->cmd = NULL;
477 }
478
479 void freeClient(redisClient *c) {
480 listNode *ln;
481
482 /* Note that if the client we are freeing is blocked into a blocking
483 * call, we have to set querybuf to NULL *before* to call
484 * unblockClientWaitingData() to avoid processInputBuffer() will get
485 * called. Also it is important to remove the file events after
486 * this, because this call adds the READABLE event. */
487 sdsfree(c->querybuf);
488 c->querybuf = NULL;
489 if (c->flags & REDIS_BLOCKED)
490 unblockClientWaitingData(c);
491
492 /* UNWATCH all the keys */
493 unwatchAllKeys(c);
494 listRelease(c->watched_keys);
495 /* Unsubscribe from all the pubsub channels */
496 pubsubUnsubscribeAllChannels(c,0);
497 pubsubUnsubscribeAllPatterns(c,0);
498 dictRelease(c->pubsub_channels);
499 listRelease(c->pubsub_patterns);
500 /* Obvious cleanup */
501 aeDeleteFileEvent(server.el,c->fd,AE_READABLE);
502 aeDeleteFileEvent(server.el,c->fd,AE_WRITABLE);
503 listRelease(c->reply);
504 freeClientArgv(c);
505 close(c->fd);
506 /* Remove from the list of clients */
507 ln = listSearchKey(server.clients,c);
508 redisAssert(ln != NULL);
509 listDelNode(server.clients,ln);
510 /* When client was just unblocked because of a blocking operation,
511 * remove it from the list with unblocked clients. */
512 if (c->flags & REDIS_UNBLOCKED) {
513 ln = listSearchKey(server.unblocked_clients,c);
514 redisAssert(ln != NULL);
515 listDelNode(server.unblocked_clients,ln);
516 }
517 listRelease(c->io_keys);
518 /* Master/slave cleanup.
519 * Case 1: we lost the connection with a slave. */
520 if (c->flags & REDIS_SLAVE) {
521 if (c->replstate == REDIS_REPL_SEND_BULK && c->repldbfd != -1)
522 close(c->repldbfd);
523 list *l = (c->flags & REDIS_MONITOR) ? server.monitors : server.slaves;
524 ln = listSearchKey(l,c);
525 redisAssert(ln != NULL);
526 listDelNode(l,ln);
527 }
528
529 /* Case 2: we lost the connection with the master. */
530 if (c->flags & REDIS_MASTER) {
531 server.master = NULL;
532 server.repl_state = REDIS_REPL_CONNECT;
533 server.repl_down_since = time(NULL);
534 /* Since we lost the connection with the master, we should also
535 * close the connection with all our slaves if we have any, so
536 * when we'll resync with the master the other slaves will sync again
537 * with us as well. Note that also when the slave is not connected
538 * to the master it will keep refusing connections by other slaves.
539 *
540 * We do this only if server.masterhost != NULL. If it is NULL this
541 * means the user called SLAVEOF NO ONE and we are freeing our
542 * link with the master, so no need to close link with slaves. */
543 if (server.masterhost != NULL) {
544 while (listLength(server.slaves)) {
545 ln = listFirst(server.slaves);
546 freeClient((redisClient*)ln->value);
547 }
548 }
549 }
550 /* Release memory */
551 zfree(c->argv);
552 freeClientMultiState(c);
553 zfree(c);
554 }
555
556 void sendReplyToClient(aeEventLoop *el, int fd, void *privdata, int mask) {
557 redisClient *c = privdata;
558 int nwritten = 0, totwritten = 0, objlen;
559 robj *o;
560 REDIS_NOTUSED(el);
561 REDIS_NOTUSED(mask);
562
563 while(c->bufpos > 0 || listLength(c->reply)) {
564 if (c->bufpos > 0) {
565 if (c->flags & REDIS_MASTER) {
566 /* Don't reply to a master */
567 nwritten = c->bufpos - c->sentlen;
568 } else {
569 nwritten = write(fd,c->buf+c->sentlen,c->bufpos-c->sentlen);
570 if (nwritten <= 0) break;
571 }
572 c->sentlen += nwritten;
573 totwritten += nwritten;
574
575 /* If the buffer was sent, set bufpos to zero to continue with
576 * the remainder of the reply. */
577 if (c->sentlen == c->bufpos) {
578 c->bufpos = 0;
579 c->sentlen = 0;
580 }
581 } else {
582 o = listNodeValue(listFirst(c->reply));
583 objlen = sdslen(o->ptr);
584
585 if (objlen == 0) {
586 listDelNode(c->reply,listFirst(c->reply));
587 continue;
588 }
589
590 if (c->flags & REDIS_MASTER) {
591 /* Don't reply to a master */
592 nwritten = objlen - c->sentlen;
593 } else {
594 nwritten = write(fd, ((char*)o->ptr)+c->sentlen,objlen-c->sentlen);
595 if (nwritten <= 0) break;
596 }
597 c->sentlen += nwritten;
598 totwritten += nwritten;
599
600 /* If we fully sent the object on head go to the next one */
601 if (c->sentlen == objlen) {
602 listDelNode(c->reply,listFirst(c->reply));
603 c->sentlen = 0;
604 }
605 }
606 /* Note that we avoid to send more thank REDIS_MAX_WRITE_PER_EVENT
607 * bytes, in a single threaded server it's a good idea to serve
608 * other clients as well, even if a very large request comes from
609 * super fast link that is always able to accept data (in real world
610 * scenario think about 'KEYS *' against the loopback interfae) */
611 if (totwritten > REDIS_MAX_WRITE_PER_EVENT) break;
612 }
613 if (nwritten == -1) {
614 if (errno == EAGAIN) {
615 nwritten = 0;
616 } else {
617 redisLog(REDIS_VERBOSE,
618 "Error writing to client: %s", strerror(errno));
619 freeClient(c);
620 return;
621 }
622 }
623 if (totwritten > 0) c->lastinteraction = time(NULL);
624 if (c->bufpos == 0 && listLength(c->reply) == 0) {
625 c->sentlen = 0;
626 aeDeleteFileEvent(server.el,c->fd,AE_WRITABLE);
627
628 /* Close connection after entire reply has been sent. */
629 if (c->flags & REDIS_CLOSE_AFTER_REPLY) freeClient(c);
630 }
631 }
632
633 /* resetClient prepare the client to process the next command */
634 void resetClient(redisClient *c) {
635 freeClientArgv(c);
636 c->reqtype = 0;
637 c->multibulklen = 0;
638 c->bulklen = -1;
639 /* We clear the ASKING flag as well if we are not inside a MULTI. */
640 if (!(c->flags & REDIS_MULTI)) c->flags &= (~REDIS_ASKING);
641 }
642
643 void closeTimedoutClients(void) {
644 redisClient *c;
645 listNode *ln;
646 time_t now = time(NULL);
647 listIter li;
648
649 listRewind(server.clients,&li);
650 while ((ln = listNext(&li)) != NULL) {
651 c = listNodeValue(ln);
652 if (server.maxidletime &&
653 !(c->flags & REDIS_SLAVE) && /* no timeout for slaves */
654 !(c->flags & REDIS_MASTER) && /* no timeout for masters */
655 !(c->flags & REDIS_BLOCKED) && /* no timeout for BLPOP */
656 dictSize(c->pubsub_channels) == 0 && /* no timeout for pubsub */
657 listLength(c->pubsub_patterns) == 0 &&
658 (now - c->lastinteraction > server.maxidletime))
659 {
660 redisLog(REDIS_VERBOSE,"Closing idle client");
661 freeClient(c);
662 } else if (c->flags & REDIS_BLOCKED) {
663 if (c->bpop.timeout != 0 && c->bpop.timeout < now) {
664 addReply(c,shared.nullmultibulk);
665 unblockClientWaitingData(c);
666 }
667 }
668 }
669 }
670
671 int processInlineBuffer(redisClient *c) {
672 char *newline = strstr(c->querybuf,"\r\n");
673 int argc, j;
674 sds *argv;
675 size_t querylen;
676
677 /* Nothing to do without a \r\n */
678 if (newline == NULL)
679 return REDIS_ERR;
680
681 /* Split the input buffer up to the \r\n */
682 querylen = newline-(c->querybuf);
683 argv = sdssplitlen(c->querybuf,querylen," ",1,&argc);
684
685 /* Leave data after the first line of the query in the buffer */
686 c->querybuf = sdsrange(c->querybuf,querylen+2,-1);
687
688 /* Setup argv array on client structure */
689 if (c->argv) zfree(c->argv);
690 c->argv = zmalloc(sizeof(robj*)*argc);
691
692 /* Create redis objects for all arguments. */
693 for (c->argc = 0, j = 0; j < argc; j++) {
694 if (sdslen(argv[j])) {
695 c->argv[c->argc] = createObject(REDIS_STRING,argv[j]);
696 c->argc++;
697 } else {
698 sdsfree(argv[j]);
699 }
700 }
701 zfree(argv);
702 return REDIS_OK;
703 }
704
705 /* Helper function. Trims query buffer to make the function that processes
706 * multi bulk requests idempotent. */
707 static void setProtocolError(redisClient *c, int pos) {
708 if (server.verbosity >= REDIS_VERBOSE) {
709 sds client = getClientInfoString(c);
710 redisLog(REDIS_VERBOSE,
711 "Protocol error from client: %s", client);
712 sdsfree(client);
713 }
714 c->flags |= REDIS_CLOSE_AFTER_REPLY;
715 c->querybuf = sdsrange(c->querybuf,pos,-1);
716 }
717
718 int processMultibulkBuffer(redisClient *c) {
719 char *newline = NULL;
720 int pos = 0, ok;
721 long long ll;
722
723 if (c->multibulklen == 0) {
724 /* The client should have been reset */
725 redisAssertWithInfo(c,NULL,c->argc == 0);
726
727 /* Multi bulk length cannot be read without a \r\n */
728 newline = strchr(c->querybuf,'\r');
729 if (newline == NULL)
730 return REDIS_ERR;
731
732 /* Buffer should also contain \n */
733 if (newline-(c->querybuf) > ((signed)sdslen(c->querybuf)-2))
734 return REDIS_ERR;
735
736 /* We know for sure there is a whole line since newline != NULL,
737 * so go ahead and find out the multi bulk length. */
738 redisAssertWithInfo(c,NULL,c->querybuf[0] == '*');
739 ok = string2ll(c->querybuf+1,newline-(c->querybuf+1),&ll);
740 if (!ok || ll > 1024*1024) {
741 addReplyError(c,"Protocol error: invalid multibulk length");
742 setProtocolError(c,pos);
743 return REDIS_ERR;
744 }
745
746 pos = (newline-c->querybuf)+2;
747 if (ll <= 0) {
748 c->querybuf = sdsrange(c->querybuf,pos,-1);
749 return REDIS_OK;
750 }
751
752 c->multibulklen = ll;
753
754 /* Setup argv array on client structure */
755 if (c->argv) zfree(c->argv);
756 c->argv = zmalloc(sizeof(robj*)*c->multibulklen);
757 }
758
759 redisAssertWithInfo(c,NULL,c->multibulklen > 0);
760 while(c->multibulklen) {
761 /* Read bulk length if unknown */
762 if (c->bulklen == -1) {
763 newline = strchr(c->querybuf+pos,'\r');
764 if (newline == NULL)
765 break;
766
767 /* Buffer should also contain \n */
768 if (newline-(c->querybuf) > ((signed)sdslen(c->querybuf)-2))
769 break;
770
771 if (c->querybuf[pos] != '$') {
772 addReplyErrorFormat(c,
773 "Protocol error: expected '$', got '%c'",
774 c->querybuf[pos]);
775 setProtocolError(c,pos);
776 return REDIS_ERR;
777 }
778
779 ok = string2ll(c->querybuf+pos+1,newline-(c->querybuf+pos+1),&ll);
780 if (!ok || ll < 0 || ll > 512*1024*1024) {
781 addReplyError(c,"Protocol error: invalid bulk length");
782 setProtocolError(c,pos);
783 return REDIS_ERR;
784 }
785
786 pos += newline-(c->querybuf+pos)+2;
787 if (ll >= REDIS_MBULK_BIG_ARG) {
788 /* If we are going to read a large object from network
789 * try to make it likely that it will start at c->querybuf
790 * boundary so that we can optimized object creation
791 * avoiding a large copy of data. */
792 c->querybuf = sdsrange(c->querybuf,pos,-1);
793 pos = 0;
794 /* Hint the sds library about the amount of bytes this string is
795 * going to contain. */
796 c->querybuf = sdsMakeRoomFor(c->querybuf,ll+2);
797 }
798 c->bulklen = ll;
799 }
800
801 /* Read bulk argument */
802 if (sdslen(c->querybuf)-pos < (unsigned)(c->bulklen+2)) {
803 /* Not enough data (+2 == trailing \r\n) */
804 break;
805 } else {
806 /* Optimization: if the buffer contanins JUST our bulk element
807 * instead of creating a new object by *copying* the sds we
808 * just use the current sds string. */
809 if (pos == 0 &&
810 c->bulklen >= REDIS_MBULK_BIG_ARG &&
811 (signed) sdslen(c->querybuf) == c->bulklen+2)
812 {
813 c->argv[c->argc++] = createObject(REDIS_STRING,c->querybuf);
814 sdsIncrLen(c->querybuf,-2); /* remove CRLF */
815 c->querybuf = sdsempty();
816 /* Assume that if we saw a fat argument we'll see another one
817 * likely... */
818 c->querybuf = sdsMakeRoomFor(c->querybuf,c->bulklen+2);
819 pos = 0;
820 } else {
821 c->argv[c->argc++] =
822 createStringObject(c->querybuf+pos,c->bulklen);
823 pos += c->bulklen+2;
824 }
825 c->bulklen = -1;
826 c->multibulklen--;
827 }
828 }
829
830 /* Trim to pos */
831 if (pos) c->querybuf = sdsrange(c->querybuf,pos,-1);
832
833 /* We're done when c->multibulk == 0 */
834 if (c->multibulklen == 0) {
835 return REDIS_OK;
836 }
837 return REDIS_ERR;
838 }
839
840 void processInputBuffer(redisClient *c) {
841 /* Keep processing while there is something in the input buffer */
842 while(sdslen(c->querybuf)) {
843 /* Immediately abort if the client is in the middle of something. */
844 if (c->flags & REDIS_BLOCKED) return;
845
846 /* REDIS_CLOSE_AFTER_REPLY closes the connection once the reply is
847 * written to the client. Make sure to not let the reply grow after
848 * this flag has been set (i.e. don't process more commands). */
849 if (c->flags & REDIS_CLOSE_AFTER_REPLY) return;
850
851 /* Determine request type when unknown. */
852 if (!c->reqtype) {
853 if (c->querybuf[0] == '*') {
854 c->reqtype = REDIS_REQ_MULTIBULK;
855 } else {
856 c->reqtype = REDIS_REQ_INLINE;
857 }
858 }
859
860 if (c->reqtype == REDIS_REQ_INLINE) {
861 if (processInlineBuffer(c) != REDIS_OK) break;
862 } else if (c->reqtype == REDIS_REQ_MULTIBULK) {
863 if (processMultibulkBuffer(c) != REDIS_OK) break;
864 } else {
865 redisPanic("Unknown request type");
866 }
867
868 /* Multibulk processing could see a <= 0 length. */
869 if (c->argc == 0) {
870 resetClient(c);
871 } else {
872 /* Only reset the client when the command was executed. */
873 if (processCommand(c) == REDIS_OK)
874 resetClient(c);
875 }
876 }
877 }
878
879 void readQueryFromClient(aeEventLoop *el, int fd, void *privdata, int mask) {
880 redisClient *c = (redisClient*) privdata;
881 int nread, readlen;
882 size_t qblen;
883 REDIS_NOTUSED(el);
884 REDIS_NOTUSED(mask);
885
886 readlen = REDIS_IOBUF_LEN;
887 /* If this is a multi bulk request, and we are processing a bulk reply
888 * that is large enough, try to maximize the probabilty that the query
889 * buffer contains excatly the SDS string representing the object, even
890 * at the risk of requring more read(2) calls. This way the function
891 * processMultiBulkBuffer() can avoid copying buffers to create the
892 * Redis Object representing the argument. */
893 if (c->reqtype == REDIS_REQ_MULTIBULK && c->multibulklen && c->bulklen != -1
894 && c->bulklen >= REDIS_MBULK_BIG_ARG)
895 {
896 int remaining = (unsigned)(c->bulklen+2)-sdslen(c->querybuf);
897
898 if (remaining < readlen) readlen = remaining;
899 }
900
901 qblen = sdslen(c->querybuf);
902 c->querybuf = sdsMakeRoomFor(c->querybuf, readlen);
903 nread = read(fd, c->querybuf+qblen, readlen);
904 if (nread == -1) {
905 if (errno == EAGAIN) {
906 nread = 0;
907 } else {
908 redisLog(REDIS_VERBOSE, "Reading from client: %s",strerror(errno));
909 freeClient(c);
910 return;
911 }
912 } else if (nread == 0) {
913 redisLog(REDIS_VERBOSE, "Client closed connection");
914 freeClient(c);
915 return;
916 }
917 if (nread) {
918 sdsIncrLen(c->querybuf,nread);
919 c->lastinteraction = time(NULL);
920 } else {
921 return;
922 }
923 if (sdslen(c->querybuf) > server.client_max_querybuf_len) {
924 sds ci = getClientInfoString(c), bytes = sdsempty();
925
926 bytes = sdscatrepr(bytes,c->querybuf,64);
927 redisLog(REDIS_WARNING,"Closing client that reached max query buffer length: %s (qbuf initial bytes: %s)", ci, bytes);
928 sdsfree(ci);
929 sdsfree(bytes);
930 freeClient(c);
931 return;
932 }
933 processInputBuffer(c);
934 }
935
936 void getClientsMaxBuffers(unsigned long *longest_output_list,
937 unsigned long *biggest_input_buffer) {
938 redisClient *c;
939 listNode *ln;
940 listIter li;
941 unsigned long lol = 0, bib = 0;
942
943 listRewind(server.clients,&li);
944 while ((ln = listNext(&li)) != NULL) {
945 c = listNodeValue(ln);
946
947 if (listLength(c->reply) > lol) lol = listLength(c->reply);
948 if (sdslen(c->querybuf) > bib) bib = sdslen(c->querybuf);
949 }
950 *longest_output_list = lol;
951 *biggest_input_buffer = bib;
952 }
953
954 /* Turn a Redis client into an sds string representing its state. */
955 sds getClientInfoString(redisClient *client) {
956 char ip[32], flags[16], events[3], *p;
957 int port;
958 time_t now = time(NULL);
959 int emask;
960
961 if (anetPeerToString(client->fd,ip,&port) == -1) {
962 ip[0] = '?';
963 ip[1] = '\0';
964 port = 0;
965 }
966 p = flags;
967 if (client->flags & REDIS_SLAVE) {
968 if (client->flags & REDIS_MONITOR)
969 *p++ = 'O';
970 else
971 *p++ = 'S';
972 }
973 if (client->flags & REDIS_MASTER) *p++ = 'M';
974 if (client->flags & REDIS_MULTI) *p++ = 'x';
975 if (client->flags & REDIS_BLOCKED) *p++ = 'b';
976 if (client->flags & REDIS_DIRTY_CAS) *p++ = 'd';
977 if (client->flags & REDIS_CLOSE_AFTER_REPLY) *p++ = 'c';
978 if (client->flags & REDIS_UNBLOCKED) *p++ = 'u';
979 if (p == flags) *p++ = 'N';
980 *p++ = '\0';
981
982 emask = client->fd == -1 ? 0 : aeGetFileEvents(server.el,client->fd);
983 p = events;
984 if (emask & AE_READABLE) *p++ = 'r';
985 if (emask & AE_WRITABLE) *p++ = 'w';
986 *p = '\0';
987 return sdscatprintf(sdsempty(),
988 "addr=%s:%d fd=%d idle=%ld flags=%s db=%d sub=%d psub=%d qbuf=%lu obl=%lu oll=%lu events=%s cmd=%s",
989 ip,port,client->fd,
990 (long)(now - client->lastinteraction),
991 flags,
992 client->db->id,
993 (int) dictSize(client->pubsub_channels),
994 (int) listLength(client->pubsub_patterns),
995 (unsigned long) sdslen(client->querybuf),
996 (unsigned long) client->bufpos,
997 (unsigned long) listLength(client->reply),
998 events,
999 client->lastcmd ? client->lastcmd->name : "NULL");
1000 }
1001
1002 sds getAllClientsInfoString(void) {
1003 listNode *ln;
1004 listIter li;
1005 redisClient *client;
1006 sds o = sdsempty();
1007
1008 listRewind(server.clients,&li);
1009 while ((ln = listNext(&li)) != NULL) {
1010 sds cs;
1011
1012 client = listNodeValue(ln);
1013 cs = getClientInfoString(client);
1014 o = sdscatsds(o,cs);
1015 sdsfree(cs);
1016 o = sdscatlen(o,"\n",1);
1017 }
1018 return o;
1019 }
1020
1021 void clientCommand(redisClient *c) {
1022 listNode *ln;
1023 listIter li;
1024 redisClient *client;
1025
1026 if (!strcasecmp(c->argv[1]->ptr,"list") && c->argc == 2) {
1027 sds o = getAllClientsInfoString();
1028 addReplyBulkCBuffer(c,o,sdslen(o));
1029 sdsfree(o);
1030 } else if (!strcasecmp(c->argv[1]->ptr,"kill") && c->argc == 3) {
1031 listRewind(server.clients,&li);
1032 while ((ln = listNext(&li)) != NULL) {
1033 char ip[32], addr[64];
1034 int port;
1035
1036 client = listNodeValue(ln);
1037 if (anetPeerToString(client->fd,ip,&port) == -1) continue;
1038 snprintf(addr,sizeof(addr),"%s:%d",ip,port);
1039 if (strcmp(addr,c->argv[2]->ptr) == 0) {
1040 addReply(c,shared.ok);
1041 if (c == client) {
1042 client->flags |= REDIS_CLOSE_AFTER_REPLY;
1043 } else {
1044 freeClient(client);
1045 }
1046 return;
1047 }
1048 }
1049 addReplyError(c,"No such client");
1050 } else {
1051 addReplyError(c, "Syntax error, try CLIENT (LIST | KILL ip:port)");
1052 }
1053 }
1054
1055 /* Rewrite the command vector of the client. All the new objects ref count
1056 * is incremented. The old command vector is freed, and the old objects
1057 * ref count is decremented. */
1058 void rewriteClientCommandVector(redisClient *c, int argc, ...) {
1059 va_list ap;
1060 int j;
1061 robj **argv; /* The new argument vector */
1062
1063 argv = zmalloc(sizeof(robj*)*argc);
1064 va_start(ap,argc);
1065 for (j = 0; j < argc; j++) {
1066 robj *a;
1067
1068 a = va_arg(ap, robj*);
1069 argv[j] = a;
1070 incrRefCount(a);
1071 }
1072 /* We free the objects in the original vector at the end, so we are
1073 * sure that if the same objects are reused in the new vector the
1074 * refcount gets incremented before it gets decremented. */
1075 for (j = 0; j < c->argc; j++) decrRefCount(c->argv[j]);
1076 zfree(c->argv);
1077 /* Replace argv and argc with our new versions. */
1078 c->argv = argv;
1079 c->argc = argc;
1080 c->cmd = lookupCommand(c->argv[0]->ptr);
1081 redisAssertWithInfo(c,NULL,c->cmd != NULL);
1082 va_end(ap);
1083 }
1084
1085 /* Rewrite a single item in the command vector.
1086 * The new val ref count is incremented, and the old decremented. */
1087 void rewriteClientCommandArgument(redisClient *c, int i, robj *newval) {
1088 robj *oldval;
1089
1090 redisAssertWithInfo(c,NULL,i < c->argc);
1091 oldval = c->argv[i];
1092 c->argv[i] = newval;
1093 incrRefCount(newval);
1094 decrRefCount(oldval);
1095
1096 /* If this is the command name make sure to fix c->cmd. */
1097 if (i == 0) {
1098 c->cmd = lookupCommand(c->argv[0]->ptr);
1099 redisAssertWithInfo(c,NULL,c->cmd != NULL);
1100 }
1101 }