]> git.saurik.com Git - redis.git/blob - src/networking.c
Rename bstate to bpop.
[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 anetNonBlock(NULL,fd);
18 anetTcpNoDelay(NULL,fd);
19 if (!c) return NULL;
20 if (aeCreateFileEvent(server.el,fd,AE_READABLE,
21 readQueryFromClient, c) == AE_ERR)
22 {
23 close(fd);
24 zfree(c);
25 return NULL;
26 }
27
28 selectDb(c,0);
29 c->fd = fd;
30 c->querybuf = sdsempty();
31 c->reqtype = 0;
32 c->argc = 0;
33 c->argv = NULL;
34 c->multibulklen = 0;
35 c->bulklen = -1;
36 c->sentlen = 0;
37 c->flags = 0;
38 c->lastinteraction = time(NULL);
39 c->authenticated = 0;
40 c->replstate = REDIS_REPL_NONE;
41 c->reply = listCreate();
42 listSetFreeMethod(c->reply,decrRefCount);
43 listSetDupMethod(c->reply,dupClientReplyValue);
44 c->bpop.keys = NULL;
45 c->bpop.count = 0;
46 c->bpop.timeout = 0;
47 c->bpop.target = NULL;
48 c->io_keys = listCreate();
49 c->watched_keys = listCreate();
50 listSetFreeMethod(c->io_keys,decrRefCount);
51 c->pubsub_channels = dictCreate(&setDictType,NULL);
52 c->pubsub_patterns = listCreate();
53 listSetFreeMethod(c->pubsub_patterns,decrRefCount);
54 listSetMatchMethod(c->pubsub_patterns,listMatchObjects);
55 listAddNodeTail(server.clients,c);
56 initClientMultiState(c);
57 return c;
58 }
59
60 /* Set the event loop to listen for write events on the client's socket.
61 * Typically gets called every time a reply is built. */
62 int _installWriteEvent(redisClient *c) {
63 /* When CLOSE_AFTER_REPLY is set, no more replies may be added! */
64 redisAssert(!(c->flags & REDIS_CLOSE_AFTER_REPLY));
65
66 if (c->fd <= 0) return REDIS_ERR;
67 if (c->bufpos == 0 && listLength(c->reply) == 0 &&
68 (c->replstate == REDIS_REPL_NONE ||
69 c->replstate == REDIS_REPL_ONLINE) &&
70 aeCreateFileEvent(server.el, c->fd, AE_WRITABLE,
71 sendReplyToClient, c) == AE_ERR) return REDIS_ERR;
72 return REDIS_OK;
73 }
74
75 /* Create a duplicate of the last object in the reply list when
76 * it is not exclusively owned by the reply list. */
77 robj *dupLastObjectIfNeeded(list *reply) {
78 robj *new, *cur;
79 listNode *ln;
80 redisAssert(listLength(reply) > 0);
81 ln = listLast(reply);
82 cur = listNodeValue(ln);
83 if (cur->refcount > 1) {
84 new = dupStringObject(cur);
85 decrRefCount(cur);
86 listNodeValue(ln) = new;
87 }
88 return listNodeValue(ln);
89 }
90
91 int _addReplyToBuffer(redisClient *c, char *s, size_t len) {
92 size_t available = sizeof(c->buf)-c->bufpos;
93
94 /* If there already are entries in the reply list, we cannot
95 * add anything more to the static buffer. */
96 if (listLength(c->reply) > 0) return REDIS_ERR;
97
98 /* Check that the buffer has enough space available for this string. */
99 if (len > available) return REDIS_ERR;
100
101 memcpy(c->buf+c->bufpos,s,len);
102 c->bufpos+=len;
103 return REDIS_OK;
104 }
105
106 void _addReplyObjectToList(redisClient *c, robj *o) {
107 robj *tail;
108 if (listLength(c->reply) == 0) {
109 incrRefCount(o);
110 listAddNodeTail(c->reply,o);
111 } else {
112 tail = listNodeValue(listLast(c->reply));
113
114 /* Append to this object when possible. */
115 if (tail->ptr != NULL &&
116 sdslen(tail->ptr)+sdslen(o->ptr) <= REDIS_REPLY_CHUNK_BYTES)
117 {
118 tail = dupLastObjectIfNeeded(c->reply);
119 tail->ptr = sdscatlen(tail->ptr,o->ptr,sdslen(o->ptr));
120 } else {
121 incrRefCount(o);
122 listAddNodeTail(c->reply,o);
123 }
124 }
125 }
126
127 /* This method takes responsibility over the sds. When it is no longer
128 * needed it will be free'd, otherwise it ends up in a robj. */
129 void _addReplySdsToList(redisClient *c, sds s) {
130 robj *tail;
131 if (listLength(c->reply) == 0) {
132 listAddNodeTail(c->reply,createObject(REDIS_STRING,s));
133 } else {
134 tail = listNodeValue(listLast(c->reply));
135
136 /* Append to this object when possible. */
137 if (tail->ptr != NULL &&
138 sdslen(tail->ptr)+sdslen(s) <= REDIS_REPLY_CHUNK_BYTES)
139 {
140 tail = dupLastObjectIfNeeded(c->reply);
141 tail->ptr = sdscatlen(tail->ptr,s,sdslen(s));
142 sdsfree(s);
143 } else {
144 listAddNodeTail(c->reply,createObject(REDIS_STRING,s));
145 }
146 }
147 }
148
149 void _addReplyStringToList(redisClient *c, char *s, size_t len) {
150 robj *tail;
151 if (listLength(c->reply) == 0) {
152 listAddNodeTail(c->reply,createStringObject(s,len));
153 } else {
154 tail = listNodeValue(listLast(c->reply));
155
156 /* Append to this object when possible. */
157 if (tail->ptr != NULL &&
158 sdslen(tail->ptr)+len <= REDIS_REPLY_CHUNK_BYTES)
159 {
160 tail = dupLastObjectIfNeeded(c->reply);
161 tail->ptr = sdscatlen(tail->ptr,s,len);
162 } else {
163 listAddNodeTail(c->reply,createStringObject(s,len));
164 }
165 }
166 }
167
168 void addReply(redisClient *c, robj *obj) {
169 if (_installWriteEvent(c) != REDIS_OK) return;
170 redisAssert(!server.vm_enabled || obj->storage == REDIS_VM_MEMORY);
171
172 /* This is an important place where we can avoid copy-on-write
173 * when there is a saving child running, avoiding touching the
174 * refcount field of the object if it's not needed.
175 *
176 * If the encoding is RAW and there is room in the static buffer
177 * we'll be able to send the object to the client without
178 * messing with its page. */
179 if (obj->encoding == REDIS_ENCODING_RAW) {
180 if (_addReplyToBuffer(c,obj->ptr,sdslen(obj->ptr)) != REDIS_OK)
181 _addReplyObjectToList(c,obj);
182 } else {
183 obj = getDecodedObject(obj);
184 if (_addReplyToBuffer(c,obj->ptr,sdslen(obj->ptr)) != REDIS_OK)
185 _addReplyObjectToList(c,obj);
186 decrRefCount(obj);
187 }
188 }
189
190 void addReplySds(redisClient *c, sds s) {
191 if (_installWriteEvent(c) != REDIS_OK) {
192 /* The caller expects the sds to be free'd. */
193 sdsfree(s);
194 return;
195 }
196 if (_addReplyToBuffer(c,s,sdslen(s)) == REDIS_OK) {
197 sdsfree(s);
198 } else {
199 /* This method free's the sds when it is no longer needed. */
200 _addReplySdsToList(c,s);
201 }
202 }
203
204 void addReplyString(redisClient *c, char *s, size_t len) {
205 if (_installWriteEvent(c) != REDIS_OK) return;
206 if (_addReplyToBuffer(c,s,len) != REDIS_OK)
207 _addReplyStringToList(c,s,len);
208 }
209
210 void _addReplyError(redisClient *c, char *s, size_t len) {
211 addReplyString(c,"-ERR ",5);
212 addReplyString(c,s,len);
213 addReplyString(c,"\r\n",2);
214 }
215
216 void addReplyError(redisClient *c, char *err) {
217 _addReplyError(c,err,strlen(err));
218 }
219
220 void addReplyErrorFormat(redisClient *c, const char *fmt, ...) {
221 va_list ap;
222 va_start(ap,fmt);
223 sds s = sdscatvprintf(sdsempty(),fmt,ap);
224 va_end(ap);
225 _addReplyError(c,s,sdslen(s));
226 sdsfree(s);
227 }
228
229 void _addReplyStatus(redisClient *c, char *s, size_t len) {
230 addReplyString(c,"+",1);
231 addReplyString(c,s,len);
232 addReplyString(c,"\r\n",2);
233 }
234
235 void addReplyStatus(redisClient *c, char *status) {
236 _addReplyStatus(c,status,strlen(status));
237 }
238
239 void addReplyStatusFormat(redisClient *c, const char *fmt, ...) {
240 va_list ap;
241 va_start(ap,fmt);
242 sds s = sdscatvprintf(sdsempty(),fmt,ap);
243 va_end(ap);
244 _addReplyStatus(c,s,sdslen(s));
245 sdsfree(s);
246 }
247
248 /* Adds an empty object to the reply list that will contain the multi bulk
249 * length, which is not known when this function is called. */
250 void *addDeferredMultiBulkLength(redisClient *c) {
251 /* Note that we install the write event here even if the object is not
252 * ready to be sent, since we are sure that before returning to the
253 * event loop setDeferredMultiBulkLength() will be called. */
254 if (_installWriteEvent(c) != REDIS_OK) return NULL;
255 listAddNodeTail(c->reply,createObject(REDIS_STRING,NULL));
256 return listLast(c->reply);
257 }
258
259 /* Populate the length object and try glueing it to the next chunk. */
260 void setDeferredMultiBulkLength(redisClient *c, void *node, long length) {
261 listNode *ln = (listNode*)node;
262 robj *len, *next;
263
264 /* Abort when *node is NULL (see addDeferredMultiBulkLength). */
265 if (node == NULL) return;
266
267 len = listNodeValue(ln);
268 len->ptr = sdscatprintf(sdsempty(),"*%ld\r\n",length);
269 if (ln->next != NULL) {
270 next = listNodeValue(ln->next);
271
272 /* Only glue when the next node is non-NULL (an sds in this case) */
273 if (next->ptr != NULL) {
274 len->ptr = sdscatlen(len->ptr,next->ptr,sdslen(next->ptr));
275 listDelNode(c->reply,ln->next);
276 }
277 }
278 }
279
280 void addReplyDouble(redisClient *c, double d) {
281 char dbuf[128], sbuf[128];
282 int dlen, slen;
283 dlen = snprintf(dbuf,sizeof(dbuf),"%.17g",d);
284 slen = snprintf(sbuf,sizeof(sbuf),"$%d\r\n%s\r\n",dlen,dbuf);
285 addReplyString(c,sbuf,slen);
286 }
287
288 void _addReplyLongLong(redisClient *c, long long ll, char prefix) {
289 char buf[128];
290 int len;
291 buf[0] = prefix;
292 len = ll2string(buf+1,sizeof(buf)-1,ll);
293 buf[len+1] = '\r';
294 buf[len+2] = '\n';
295 addReplyString(c,buf,len+3);
296 }
297
298 void addReplyLongLong(redisClient *c, long long ll) {
299 _addReplyLongLong(c,ll,':');
300 }
301
302 void addReplyMultiBulkLen(redisClient *c, long length) {
303 _addReplyLongLong(c,length,'*');
304 }
305
306 void addReplyBulkLen(redisClient *c, robj *obj) {
307 size_t len;
308
309 if (obj->encoding == REDIS_ENCODING_RAW) {
310 len = sdslen(obj->ptr);
311 } else {
312 long n = (long)obj->ptr;
313
314 /* Compute how many bytes will take this integer as a radix 10 string */
315 len = 1;
316 if (n < 0) {
317 len++;
318 n = -n;
319 }
320 while((n = n/10) != 0) {
321 len++;
322 }
323 }
324 _addReplyLongLong(c,len,'$');
325 }
326
327 void addReplyBulk(redisClient *c, robj *obj) {
328 addReplyBulkLen(c,obj);
329 addReply(c,obj);
330 addReply(c,shared.crlf);
331 }
332
333 /* In the CONFIG command we need to add vanilla C string as bulk replies */
334 void addReplyBulkCString(redisClient *c, char *s) {
335 if (s == NULL) {
336 addReply(c,shared.nullbulk);
337 } else {
338 robj *o = createStringObject(s,strlen(s));
339 addReplyBulk(c,o);
340 decrRefCount(o);
341 }
342 }
343
344 static void acceptCommonHandler(int fd) {
345 redisClient *c;
346 if ((c = createClient(fd)) == NULL) {
347 redisLog(REDIS_WARNING,"Error allocating resoures for the client");
348 close(fd); /* May be already closed, just ingore errors */
349 return;
350 }
351 /* If maxclient directive is set and this is one client more... close the
352 * connection. Note that we create the client instead to check before
353 * for this condition, since now the socket is already set in nonblocking
354 * mode and we can send an error for free using the Kernel I/O */
355 if (server.maxclients && listLength(server.clients) > server.maxclients) {
356 char *err = "-ERR max number of clients reached\r\n";
357
358 /* That's a best effort error message, don't check write errors */
359 if (write(c->fd,err,strlen(err)) == -1) {
360 /* Nothing to do, Just to avoid the warning... */
361 }
362 freeClient(c);
363 return;
364 }
365 server.stat_numconnections++;
366 }
367
368 void acceptTcpHandler(aeEventLoop *el, int fd, void *privdata, int mask) {
369 int cport, cfd;
370 char cip[128];
371 REDIS_NOTUSED(el);
372 REDIS_NOTUSED(mask);
373 REDIS_NOTUSED(privdata);
374
375 cfd = anetTcpAccept(server.neterr, fd, cip, &cport);
376 if (cfd == AE_ERR) {
377 redisLog(REDIS_VERBOSE,"Accepting client connection: %s", server.neterr);
378 return;
379 }
380 redisLog(REDIS_VERBOSE,"Accepted %s:%d", cip, cport);
381 acceptCommonHandler(cfd);
382 }
383
384 void acceptUnixHandler(aeEventLoop *el, int fd, void *privdata, int mask) {
385 int cfd;
386 REDIS_NOTUSED(el);
387 REDIS_NOTUSED(mask);
388 REDIS_NOTUSED(privdata);
389
390 cfd = anetUnixAccept(server.neterr, fd);
391 if (cfd == AE_ERR) {
392 redisLog(REDIS_VERBOSE,"Accepting client connection: %s", server.neterr);
393 return;
394 }
395 redisLog(REDIS_VERBOSE,"Accepted connection to %s", server.unixsocket);
396 acceptCommonHandler(cfd);
397 }
398
399
400 static void freeClientArgv(redisClient *c) {
401 int j;
402 for (j = 0; j < c->argc; j++)
403 decrRefCount(c->argv[j]);
404 c->argc = 0;
405 }
406
407 void freeClient(redisClient *c) {
408 listNode *ln;
409
410 /* Note that if the client we are freeing is blocked into a blocking
411 * call, we have to set querybuf to NULL *before* to call
412 * unblockClientWaitingData() to avoid processInputBuffer() will get
413 * called. Also it is important to remove the file events after
414 * this, because this call adds the READABLE event. */
415 sdsfree(c->querybuf);
416 c->querybuf = NULL;
417 if (c->flags & REDIS_BLOCKED)
418 unblockClientWaitingData(c);
419
420 /* UNWATCH all the keys */
421 unwatchAllKeys(c);
422 listRelease(c->watched_keys);
423 /* Unsubscribe from all the pubsub channels */
424 pubsubUnsubscribeAllChannels(c,0);
425 pubsubUnsubscribeAllPatterns(c,0);
426 dictRelease(c->pubsub_channels);
427 listRelease(c->pubsub_patterns);
428 /* Obvious cleanup */
429 aeDeleteFileEvent(server.el,c->fd,AE_READABLE);
430 aeDeleteFileEvent(server.el,c->fd,AE_WRITABLE);
431 listRelease(c->reply);
432 freeClientArgv(c);
433 close(c->fd);
434 /* Remove from the list of clients */
435 ln = listSearchKey(server.clients,c);
436 redisAssert(ln != NULL);
437 listDelNode(server.clients,ln);
438 /* Remove from the list of clients waiting for swapped keys, or ready
439 * to be restarted, but not yet woken up again. */
440 if (c->flags & REDIS_IO_WAIT) {
441 redisAssert(server.vm_enabled);
442 if (listLength(c->io_keys) == 0) {
443 ln = listSearchKey(server.io_ready_clients,c);
444
445 /* When this client is waiting to be woken up (REDIS_IO_WAIT),
446 * it should be present in the list io_ready_clients */
447 redisAssert(ln != NULL);
448 listDelNode(server.io_ready_clients,ln);
449 } else {
450 while (listLength(c->io_keys)) {
451 ln = listFirst(c->io_keys);
452 dontWaitForSwappedKey(c,ln->value);
453 }
454 }
455 server.vm_blocked_clients--;
456 }
457 listRelease(c->io_keys);
458 /* Master/slave cleanup.
459 * Case 1: we lost the connection with a slave. */
460 if (c->flags & REDIS_SLAVE) {
461 if (c->replstate == REDIS_REPL_SEND_BULK && c->repldbfd != -1)
462 close(c->repldbfd);
463 list *l = (c->flags & REDIS_MONITOR) ? server.monitors : server.slaves;
464 ln = listSearchKey(l,c);
465 redisAssert(ln != NULL);
466 listDelNode(l,ln);
467 }
468
469 /* Case 2: we lost the connection with the master. */
470 if (c->flags & REDIS_MASTER) {
471 server.master = NULL;
472 /* FIXME */
473 server.replstate = REDIS_REPL_CONNECT;
474 /* Since we lost the connection with the master, we should also
475 * close the connection with all our slaves if we have any, so
476 * when we'll resync with the master the other slaves will sync again
477 * with us as well. Note that also when the slave is not connected
478 * to the master it will keep refusing connections by other slaves. */
479 while (listLength(server.slaves)) {
480 ln = listFirst(server.slaves);
481 freeClient((redisClient*)ln->value);
482 }
483 }
484 /* Release memory */
485 zfree(c->argv);
486 freeClientMultiState(c);
487 zfree(c);
488 }
489
490 void sendReplyToClient(aeEventLoop *el, int fd, void *privdata, int mask) {
491 redisClient *c = privdata;
492 int nwritten = 0, totwritten = 0, objlen;
493 robj *o;
494 REDIS_NOTUSED(el);
495 REDIS_NOTUSED(mask);
496
497 /* Use writev() if we have enough buffers to send */
498 if (!server.glueoutputbuf &&
499 listLength(c->reply) > REDIS_WRITEV_THRESHOLD &&
500 !(c->flags & REDIS_MASTER))
501 {
502 sendReplyToClientWritev(el, fd, privdata, mask);
503 return;
504 }
505
506 while(c->bufpos > 0 || listLength(c->reply)) {
507 if (c->bufpos > 0) {
508 if (c->flags & REDIS_MASTER) {
509 /* Don't reply to a master */
510 nwritten = c->bufpos - c->sentlen;
511 } else {
512 nwritten = write(fd,c->buf+c->sentlen,c->bufpos-c->sentlen);
513 if (nwritten <= 0) break;
514 }
515 c->sentlen += nwritten;
516 totwritten += nwritten;
517
518 /* If the buffer was sent, set bufpos to zero to continue with
519 * the remainder of the reply. */
520 if (c->sentlen == c->bufpos) {
521 c->bufpos = 0;
522 c->sentlen = 0;
523 }
524 } else {
525 o = listNodeValue(listFirst(c->reply));
526 objlen = sdslen(o->ptr);
527
528 if (objlen == 0) {
529 listDelNode(c->reply,listFirst(c->reply));
530 continue;
531 }
532
533 if (c->flags & REDIS_MASTER) {
534 /* Don't reply to a master */
535 nwritten = objlen - c->sentlen;
536 } else {
537 nwritten = write(fd, ((char*)o->ptr)+c->sentlen,objlen-c->sentlen);
538 if (nwritten <= 0) break;
539 }
540 c->sentlen += nwritten;
541 totwritten += nwritten;
542
543 /* If we fully sent the object on head go to the next one */
544 if (c->sentlen == objlen) {
545 listDelNode(c->reply,listFirst(c->reply));
546 c->sentlen = 0;
547 }
548 }
549 /* Note that we avoid to send more thank REDIS_MAX_WRITE_PER_EVENT
550 * bytes, in a single threaded server it's a good idea to serve
551 * other clients as well, even if a very large request comes from
552 * super fast link that is always able to accept data (in real world
553 * scenario think about 'KEYS *' against the loopback interfae) */
554 if (totwritten > REDIS_MAX_WRITE_PER_EVENT) break;
555 }
556 if (nwritten == -1) {
557 if (errno == EAGAIN) {
558 nwritten = 0;
559 } else {
560 redisLog(REDIS_VERBOSE,
561 "Error writing to client: %s", strerror(errno));
562 freeClient(c);
563 return;
564 }
565 }
566 if (totwritten > 0) c->lastinteraction = time(NULL);
567 if (listLength(c->reply) == 0) {
568 c->sentlen = 0;
569 aeDeleteFileEvent(server.el,c->fd,AE_WRITABLE);
570
571 /* Close connection after entire reply has been sent. */
572 if (c->flags & REDIS_CLOSE_AFTER_REPLY) freeClient(c);
573 }
574 }
575
576 void sendReplyToClientWritev(aeEventLoop *el, int fd, void *privdata, int mask)
577 {
578 redisClient *c = privdata;
579 int nwritten = 0, totwritten = 0, objlen, willwrite;
580 robj *o;
581 struct iovec iov[REDIS_WRITEV_IOVEC_COUNT];
582 int offset, ion = 0;
583 REDIS_NOTUSED(el);
584 REDIS_NOTUSED(mask);
585
586 listNode *node;
587 while (listLength(c->reply)) {
588 offset = c->sentlen;
589 ion = 0;
590 willwrite = 0;
591
592 /* fill-in the iov[] array */
593 for(node = listFirst(c->reply); node; node = listNextNode(node)) {
594 o = listNodeValue(node);
595 objlen = sdslen(o->ptr);
596
597 if (totwritten + objlen - offset > REDIS_MAX_WRITE_PER_EVENT)
598 break;
599
600 if(ion == REDIS_WRITEV_IOVEC_COUNT)
601 break; /* no more iovecs */
602
603 iov[ion].iov_base = ((char*)o->ptr) + offset;
604 iov[ion].iov_len = objlen - offset;
605 willwrite += objlen - offset;
606 offset = 0; /* just for the first item */
607 ion++;
608 }
609
610 if(willwrite == 0)
611 break;
612
613 /* write all collected blocks at once */
614 if((nwritten = writev(fd, iov, ion)) < 0) {
615 if (errno != EAGAIN) {
616 redisLog(REDIS_VERBOSE,
617 "Error writing to client: %s", strerror(errno));
618 freeClient(c);
619 return;
620 }
621 break;
622 }
623
624 totwritten += nwritten;
625 offset = c->sentlen;
626
627 /* remove written robjs from c->reply */
628 while (nwritten && listLength(c->reply)) {
629 o = listNodeValue(listFirst(c->reply));
630 objlen = sdslen(o->ptr);
631
632 if(nwritten >= objlen - offset) {
633 listDelNode(c->reply, listFirst(c->reply));
634 nwritten -= objlen - offset;
635 c->sentlen = 0;
636 } else {
637 /* partial write */
638 c->sentlen += nwritten;
639 break;
640 }
641 offset = 0;
642 }
643 }
644
645 if (totwritten > 0)
646 c->lastinteraction = time(NULL);
647
648 if (listLength(c->reply) == 0) {
649 c->sentlen = 0;
650 aeDeleteFileEvent(server.el,c->fd,AE_WRITABLE);
651 }
652 }
653
654 /* resetClient prepare the client to process the next command */
655 void resetClient(redisClient *c) {
656 freeClientArgv(c);
657 c->reqtype = 0;
658 c->multibulklen = 0;
659 c->bulklen = -1;
660 }
661
662 void closeTimedoutClients(void) {
663 redisClient *c;
664 listNode *ln;
665 time_t now = time(NULL);
666 listIter li;
667
668 listRewind(server.clients,&li);
669 while ((ln = listNext(&li)) != NULL) {
670 c = listNodeValue(ln);
671 if (server.maxidletime &&
672 !(c->flags & REDIS_SLAVE) && /* no timeout for slaves */
673 !(c->flags & REDIS_MASTER) && /* no timeout for masters */
674 !(c->flags & REDIS_BLOCKED) && /* no timeout for BLPOP */
675 dictSize(c->pubsub_channels) == 0 && /* no timeout for pubsub */
676 listLength(c->pubsub_patterns) == 0 &&
677 (now - c->lastinteraction > server.maxidletime))
678 {
679 redisLog(REDIS_VERBOSE,"Closing idle client");
680 freeClient(c);
681 } else if (c->flags & REDIS_BLOCKED) {
682 if (c->bpop.timeout != 0 && c->bpop.timeout < now) {
683 addReply(c,shared.nullmultibulk);
684 unblockClientWaitingData(c);
685 }
686 }
687 }
688 }
689
690 int processInlineBuffer(redisClient *c) {
691 char *newline = strstr(c->querybuf,"\r\n");
692 int argc, j;
693 sds *argv;
694 size_t querylen;
695
696 /* Nothing to do without a \r\n */
697 if (newline == NULL)
698 return REDIS_ERR;
699
700 /* Split the input buffer up to the \r\n */
701 querylen = newline-(c->querybuf);
702 argv = sdssplitlen(c->querybuf,querylen," ",1,&argc);
703
704 /* Leave data after the first line of the query in the buffer */
705 c->querybuf = sdsrange(c->querybuf,querylen+2,-1);
706
707 /* Setup argv array on client structure */
708 if (c->argv) zfree(c->argv);
709 c->argv = zmalloc(sizeof(robj*)*argc);
710
711 /* Create redis objects for all arguments. */
712 for (c->argc = 0, j = 0; j < argc; j++) {
713 if (sdslen(argv[j])) {
714 c->argv[c->argc] = createObject(REDIS_STRING,argv[j]);
715 c->argc++;
716 } else {
717 sdsfree(argv[j]);
718 }
719 }
720 zfree(argv);
721 return REDIS_OK;
722 }
723
724 /* Helper function. Trims query buffer to make the function that processes
725 * multi bulk requests idempotent. */
726 static void setProtocolError(redisClient *c, int pos) {
727 c->flags |= REDIS_CLOSE_AFTER_REPLY;
728 c->querybuf = sdsrange(c->querybuf,pos,-1);
729 }
730
731 int processMultibulkBuffer(redisClient *c) {
732 char *newline = NULL;
733 char *eptr;
734 int pos = 0, tolerr;
735 long bulklen;
736
737 if (c->multibulklen == 0) {
738 /* The client should have been reset */
739 redisAssert(c->argc == 0);
740
741 /* Multi bulk length cannot be read without a \r\n */
742 newline = strstr(c->querybuf,"\r\n");
743 if (newline == NULL)
744 return REDIS_ERR;
745
746 /* We know for sure there is a whole line since newline != NULL,
747 * so go ahead and find out the multi bulk length. */
748 redisAssert(c->querybuf[0] == '*');
749 c->multibulklen = strtol(c->querybuf+1,&eptr,10);
750 pos = (newline-c->querybuf)+2;
751 if (c->multibulklen <= 0) {
752 c->querybuf = sdsrange(c->querybuf,pos,-1);
753 return REDIS_OK;
754 } else if (c->multibulklen > 1024*1024) {
755 addReplyError(c,"Protocol error: invalid multibulk length");
756 setProtocolError(c,pos);
757 return REDIS_ERR;
758 }
759
760 /* Setup argv array on client structure */
761 if (c->argv) zfree(c->argv);
762 c->argv = zmalloc(sizeof(robj*)*c->multibulklen);
763
764 /* Search new newline */
765 newline = strstr(c->querybuf+pos,"\r\n");
766 }
767
768 redisAssert(c->multibulklen > 0);
769 while(c->multibulklen) {
770 /* Read bulk length if unknown */
771 if (c->bulklen == -1) {
772 newline = strstr(c->querybuf+pos,"\r\n");
773 if (newline != NULL) {
774 if (c->querybuf[pos] != '$') {
775 addReplyErrorFormat(c,
776 "Protocol error: expected '$', got '%c'",
777 c->querybuf[pos]);
778 setProtocolError(c,pos);
779 return REDIS_ERR;
780 }
781
782 bulklen = strtol(c->querybuf+pos+1,&eptr,10);
783 tolerr = (eptr[0] != '\r');
784 if (tolerr || bulklen == LONG_MIN || bulklen == LONG_MAX ||
785 bulklen < 0 || bulklen > 1024*1024*1024)
786 {
787 addReplyError(c,"Protocol error: invalid bulk length");
788 setProtocolError(c,pos);
789 return REDIS_ERR;
790 }
791 pos += eptr-(c->querybuf+pos)+2;
792 c->bulklen = bulklen;
793 } else {
794 /* No newline in current buffer, so wait for more data */
795 break;
796 }
797 }
798
799 /* Read bulk argument */
800 if (sdslen(c->querybuf)-pos < (unsigned)(c->bulklen+2)) {
801 /* Not enough data (+2 == trailing \r\n) */
802 break;
803 } else {
804 c->argv[c->argc++] = createStringObject(c->querybuf+pos,c->bulklen);
805 pos += c->bulklen+2;
806 c->bulklen = -1;
807 c->multibulklen--;
808 }
809 }
810
811 /* Trim to pos */
812 c->querybuf = sdsrange(c->querybuf,pos,-1);
813
814 /* We're done when c->multibulk == 0 */
815 if (c->multibulklen == 0) {
816 return REDIS_OK;
817 }
818 return REDIS_ERR;
819 }
820
821 void processInputBuffer(redisClient *c) {
822 /* Keep processing while there is something in the input buffer */
823 while(sdslen(c->querybuf)) {
824 /* Immediately abort if the client is in the middle of something. */
825 if (c->flags & REDIS_BLOCKED || c->flags & REDIS_IO_WAIT) return;
826
827 /* REDIS_CLOSE_AFTER_REPLY closes the connection once the reply is
828 * written to the client. Make sure to not let the reply grow after
829 * this flag has been set (i.e. don't process more commands). */
830 if (c->flags & REDIS_CLOSE_AFTER_REPLY) return;
831
832 /* Determine request type when unknown. */
833 if (!c->reqtype) {
834 if (c->querybuf[0] == '*') {
835 c->reqtype = REDIS_REQ_MULTIBULK;
836 } else {
837 c->reqtype = REDIS_REQ_INLINE;
838 }
839 }
840
841 if (c->reqtype == REDIS_REQ_INLINE) {
842 if (processInlineBuffer(c) != REDIS_OK) break;
843 } else if (c->reqtype == REDIS_REQ_MULTIBULK) {
844 if (processMultibulkBuffer(c) != REDIS_OK) break;
845 } else {
846 redisPanic("Unknown request type");
847 }
848
849 /* Multibulk processing could see a <= 0 length. */
850 if (c->argc == 0) {
851 resetClient(c);
852 } else {
853 /* Only reset the client when the command was executed. */
854 if (processCommand(c) == REDIS_OK)
855 resetClient(c);
856 }
857 }
858 }
859
860 void readQueryFromClient(aeEventLoop *el, int fd, void *privdata, int mask) {
861 redisClient *c = (redisClient*) privdata;
862 char buf[REDIS_IOBUF_LEN];
863 int nread;
864 REDIS_NOTUSED(el);
865 REDIS_NOTUSED(mask);
866
867 nread = read(fd, buf, REDIS_IOBUF_LEN);
868 if (nread == -1) {
869 if (errno == EAGAIN) {
870 nread = 0;
871 } else {
872 redisLog(REDIS_VERBOSE, "Reading from client: %s",strerror(errno));
873 freeClient(c);
874 return;
875 }
876 } else if (nread == 0) {
877 redisLog(REDIS_VERBOSE, "Client closed connection");
878 freeClient(c);
879 return;
880 }
881 if (nread) {
882 c->querybuf = sdscatlen(c->querybuf,buf,nread);
883 c->lastinteraction = time(NULL);
884 } else {
885 return;
886 }
887 processInputBuffer(c);
888 }