]> git.saurik.com Git - redis.git/blob - src/networking.c
Multi bulk optimization for creating big objects without copying data is no longer...
[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 = 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 static void acceptCommonHandler(int fd) {
405 redisClient *c;
406 if ((c = createClient(fd)) == NULL) {
407 redisLog(REDIS_WARNING,"Error allocating resoures for the client");
408 close(fd); /* May be already closed, just ingore errors */
409 return;
410 }
411 /* If maxclient directive is set and this is one client more... close the
412 * connection. Note that we create the client instead to check before
413 * for this condition, since now the socket is already set in nonblocking
414 * mode and we can send an error for free using the Kernel I/O */
415 if (listLength(server.clients) > server.maxclients) {
416 char *err = "-ERR max number of clients reached\r\n";
417
418 /* That's a best effort error message, don't check write errors */
419 if (write(c->fd,err,strlen(err)) == -1) {
420 /* Nothing to do, Just to avoid the warning... */
421 }
422 freeClient(c);
423 return;
424 }
425 server.stat_numconnections++;
426 }
427
428 void acceptTcpHandler(aeEventLoop *el, int fd, void *privdata, int mask) {
429 int cport, cfd;
430 char cip[128];
431 REDIS_NOTUSED(el);
432 REDIS_NOTUSED(mask);
433 REDIS_NOTUSED(privdata);
434
435 cfd = anetTcpAccept(server.neterr, fd, cip, &cport);
436 if (cfd == AE_ERR) {
437 redisLog(REDIS_WARNING,"Accepting client connection: %s", server.neterr);
438 return;
439 }
440 redisLog(REDIS_VERBOSE,"Accepted %s:%d", cip, cport);
441 acceptCommonHandler(cfd);
442 }
443
444 void acceptUnixHandler(aeEventLoop *el, int fd, void *privdata, int mask) {
445 int cfd;
446 REDIS_NOTUSED(el);
447 REDIS_NOTUSED(mask);
448 REDIS_NOTUSED(privdata);
449
450 cfd = anetUnixAccept(server.neterr, fd);
451 if (cfd == AE_ERR) {
452 redisLog(REDIS_WARNING,"Accepting client connection: %s", server.neterr);
453 return;
454 }
455 redisLog(REDIS_VERBOSE,"Accepted connection to %s", server.unixsocket);
456 acceptCommonHandler(cfd);
457 }
458
459
460 static void freeClientArgv(redisClient *c) {
461 int j;
462 for (j = 0; j < c->argc; j++)
463 decrRefCount(c->argv[j]);
464 c->argc = 0;
465 c->cmd = NULL;
466 }
467
468 void freeClient(redisClient *c) {
469 listNode *ln;
470
471 /* Note that if the client we are freeing is blocked into a blocking
472 * call, we have to set querybuf to NULL *before* to call
473 * unblockClientWaitingData() to avoid processInputBuffer() will get
474 * called. Also it is important to remove the file events after
475 * this, because this call adds the READABLE event. */
476 sdsfree(c->querybuf);
477 c->querybuf = NULL;
478 if (c->flags & REDIS_BLOCKED)
479 unblockClientWaitingData(c);
480
481 /* UNWATCH all the keys */
482 unwatchAllKeys(c);
483 listRelease(c->watched_keys);
484 /* Unsubscribe from all the pubsub channels */
485 pubsubUnsubscribeAllChannels(c,0);
486 pubsubUnsubscribeAllPatterns(c,0);
487 dictRelease(c->pubsub_channels);
488 listRelease(c->pubsub_patterns);
489 /* Obvious cleanup */
490 aeDeleteFileEvent(server.el,c->fd,AE_READABLE);
491 aeDeleteFileEvent(server.el,c->fd,AE_WRITABLE);
492 listRelease(c->reply);
493 freeClientArgv(c);
494 close(c->fd);
495 /* Remove from the list of clients */
496 ln = listSearchKey(server.clients,c);
497 redisAssert(ln != NULL);
498 listDelNode(server.clients,ln);
499 /* When client was just unblocked because of a blocking operation,
500 * remove it from the list with unblocked clients. */
501 if (c->flags & REDIS_UNBLOCKED) {
502 ln = listSearchKey(server.unblocked_clients,c);
503 redisAssert(ln != NULL);
504 listDelNode(server.unblocked_clients,ln);
505 }
506 listRelease(c->io_keys);
507 /* Master/slave cleanup.
508 * Case 1: we lost the connection with a slave. */
509 if (c->flags & REDIS_SLAVE) {
510 if (c->replstate == REDIS_REPL_SEND_BULK && c->repldbfd != -1)
511 close(c->repldbfd);
512 list *l = (c->flags & REDIS_MONITOR) ? server.monitors : server.slaves;
513 ln = listSearchKey(l,c);
514 redisAssert(ln != NULL);
515 listDelNode(l,ln);
516 }
517
518 /* Case 2: we lost the connection with the master. */
519 if (c->flags & REDIS_MASTER) {
520 server.master = NULL;
521 server.replstate = REDIS_REPL_CONNECT;
522 server.repl_down_since = time(NULL);
523 /* Since we lost the connection with the master, we should also
524 * close the connection with all our slaves if we have any, so
525 * when we'll resync with the master the other slaves will sync again
526 * with us as well. Note that also when the slave is not connected
527 * to the master it will keep refusing connections by other slaves.
528 *
529 * We do this only if server.masterhost != NULL. If it is NULL this
530 * means the user called SLAVEOF NO ONE and we are freeing our
531 * link with the master, so no need to close link with slaves. */
532 if (server.masterhost != NULL) {
533 while (listLength(server.slaves)) {
534 ln = listFirst(server.slaves);
535 freeClient((redisClient*)ln->value);
536 }
537 }
538 }
539 /* Release memory */
540 zfree(c->argv);
541 freeClientMultiState(c);
542 zfree(c);
543 }
544
545 void sendReplyToClient(aeEventLoop *el, int fd, void *privdata, int mask) {
546 redisClient *c = privdata;
547 int nwritten = 0, totwritten = 0, objlen;
548 robj *o;
549 REDIS_NOTUSED(el);
550 REDIS_NOTUSED(mask);
551
552 while(c->bufpos > 0 || listLength(c->reply)) {
553 if (c->bufpos > 0) {
554 if (c->flags & REDIS_MASTER) {
555 /* Don't reply to a master */
556 nwritten = c->bufpos - c->sentlen;
557 } else {
558 nwritten = write(fd,c->buf+c->sentlen,c->bufpos-c->sentlen);
559 if (nwritten <= 0) break;
560 }
561 c->sentlen += nwritten;
562 totwritten += nwritten;
563
564 /* If the buffer was sent, set bufpos to zero to continue with
565 * the remainder of the reply. */
566 if (c->sentlen == c->bufpos) {
567 c->bufpos = 0;
568 c->sentlen = 0;
569 }
570 } else {
571 o = listNodeValue(listFirst(c->reply));
572 objlen = sdslen(o->ptr);
573
574 if (objlen == 0) {
575 listDelNode(c->reply,listFirst(c->reply));
576 continue;
577 }
578
579 if (c->flags & REDIS_MASTER) {
580 /* Don't reply to a master */
581 nwritten = objlen - c->sentlen;
582 } else {
583 nwritten = write(fd, ((char*)o->ptr)+c->sentlen,objlen-c->sentlen);
584 if (nwritten <= 0) break;
585 }
586 c->sentlen += nwritten;
587 totwritten += nwritten;
588
589 /* If we fully sent the object on head go to the next one */
590 if (c->sentlen == objlen) {
591 listDelNode(c->reply,listFirst(c->reply));
592 c->sentlen = 0;
593 }
594 }
595 /* Note that we avoid to send more thank REDIS_MAX_WRITE_PER_EVENT
596 * bytes, in a single threaded server it's a good idea to serve
597 * other clients as well, even if a very large request comes from
598 * super fast link that is always able to accept data (in real world
599 * scenario think about 'KEYS *' against the loopback interfae) */
600 if (totwritten > REDIS_MAX_WRITE_PER_EVENT) break;
601 }
602 if (nwritten == -1) {
603 if (errno == EAGAIN) {
604 nwritten = 0;
605 } else {
606 redisLog(REDIS_VERBOSE,
607 "Error writing to client: %s", strerror(errno));
608 freeClient(c);
609 return;
610 }
611 }
612 if (totwritten > 0) c->lastinteraction = time(NULL);
613 if (c->bufpos == 0 && listLength(c->reply) == 0) {
614 c->sentlen = 0;
615 aeDeleteFileEvent(server.el,c->fd,AE_WRITABLE);
616
617 /* Close connection after entire reply has been sent. */
618 if (c->flags & REDIS_CLOSE_AFTER_REPLY) freeClient(c);
619 }
620 }
621
622 /* resetClient prepare the client to process the next command */
623 void resetClient(redisClient *c) {
624 freeClientArgv(c);
625 c->reqtype = 0;
626 c->multibulklen = 0;
627 c->bulklen = -1;
628 /* We clear the ASKING flag as well if we are not inside a MULTI. */
629 if (!(c->flags & REDIS_MULTI)) c->flags &= (~REDIS_ASKING);
630 }
631
632 void closeTimedoutClients(void) {
633 redisClient *c;
634 listNode *ln;
635 time_t now = time(NULL);
636 listIter li;
637
638 listRewind(server.clients,&li);
639 while ((ln = listNext(&li)) != NULL) {
640 c = listNodeValue(ln);
641 if (server.maxidletime &&
642 !(c->flags & REDIS_SLAVE) && /* no timeout for slaves */
643 !(c->flags & REDIS_MASTER) && /* no timeout for masters */
644 !(c->flags & REDIS_BLOCKED) && /* no timeout for BLPOP */
645 dictSize(c->pubsub_channels) == 0 && /* no timeout for pubsub */
646 listLength(c->pubsub_patterns) == 0 &&
647 (now - c->lastinteraction > server.maxidletime))
648 {
649 redisLog(REDIS_VERBOSE,"Closing idle client");
650 freeClient(c);
651 } else if (c->flags & REDIS_BLOCKED) {
652 if (c->bpop.timeout != 0 && c->bpop.timeout < now) {
653 addReply(c,shared.nullmultibulk);
654 unblockClientWaitingData(c);
655 }
656 }
657 }
658 }
659
660 int processInlineBuffer(redisClient *c) {
661 char *newline = strstr(c->querybuf,"\r\n");
662 int argc, j;
663 sds *argv;
664 size_t querylen;
665
666 /* Nothing to do without a \r\n */
667 if (newline == NULL)
668 return REDIS_ERR;
669
670 /* Split the input buffer up to the \r\n */
671 querylen = newline-(c->querybuf);
672 argv = sdssplitlen(c->querybuf,querylen," ",1,&argc);
673
674 /* Leave data after the first line of the query in the buffer */
675 c->querybuf = sdsrange(c->querybuf,querylen+2,-1);
676
677 /* Setup argv array on client structure */
678 if (c->argv) zfree(c->argv);
679 c->argv = zmalloc(sizeof(robj*)*argc);
680
681 /* Create redis objects for all arguments. */
682 for (c->argc = 0, j = 0; j < argc; j++) {
683 if (sdslen(argv[j])) {
684 c->argv[c->argc] = createObject(REDIS_STRING,argv[j]);
685 c->argc++;
686 } else {
687 sdsfree(argv[j]);
688 }
689 }
690 zfree(argv);
691 return REDIS_OK;
692 }
693
694 /* Helper function. Trims query buffer to make the function that processes
695 * multi bulk requests idempotent. */
696 static void setProtocolError(redisClient *c, int pos) {
697 c->flags |= REDIS_CLOSE_AFTER_REPLY;
698 c->querybuf = sdsrange(c->querybuf,pos,-1);
699 }
700
701 int processMultibulkBuffer(redisClient *c) {
702 char *newline = NULL;
703 int pos = 0, ok;
704 long long ll;
705
706 if (c->multibulklen == 0) {
707 /* The client should have been reset */
708 redisAssertWithInfo(c,NULL,c->argc == 0);
709
710 /* Multi bulk length cannot be read without a \r\n */
711 newline = strchr(c->querybuf,'\r');
712 if (newline == NULL)
713 return REDIS_ERR;
714
715 /* Buffer should also contain \n */
716 if (newline-(c->querybuf) > ((signed)sdslen(c->querybuf)-2))
717 return REDIS_ERR;
718
719 /* We know for sure there is a whole line since newline != NULL,
720 * so go ahead and find out the multi bulk length. */
721 redisAssertWithInfo(c,NULL,c->querybuf[0] == '*');
722 ok = string2ll(c->querybuf+1,newline-(c->querybuf+1),&ll);
723 if (!ok || ll > 1024*1024) {
724 addReplyError(c,"Protocol error: invalid multibulk length");
725 setProtocolError(c,pos);
726 return REDIS_ERR;
727 }
728
729 pos = (newline-c->querybuf)+2;
730 if (ll <= 0) {
731 c->querybuf = sdsrange(c->querybuf,pos,-1);
732 return REDIS_OK;
733 }
734
735 c->multibulklen = ll;
736
737 /* Setup argv array on client structure */
738 if (c->argv) zfree(c->argv);
739 c->argv = zmalloc(sizeof(robj*)*c->multibulklen);
740 }
741
742 redisAssertWithInfo(c,NULL,c->multibulklen > 0);
743 while(c->multibulklen) {
744 /* Read bulk length if unknown */
745 if (c->bulklen == -1) {
746 newline = strchr(c->querybuf+pos,'\r');
747 if (newline == NULL)
748 break;
749
750 /* Buffer should also contain \n */
751 if (newline-(c->querybuf) > ((signed)sdslen(c->querybuf)-2))
752 break;
753
754 if (c->querybuf[pos] != '$') {
755 addReplyErrorFormat(c,
756 "Protocol error: expected '$', got '%c'",
757 c->querybuf[pos]);
758 setProtocolError(c,pos);
759 return REDIS_ERR;
760 }
761
762 ok = string2ll(c->querybuf+pos+1,newline-(c->querybuf+pos+1),&ll);
763 if (!ok || ll < 0 || ll > 512*1024*1024) {
764 addReplyError(c,"Protocol error: invalid bulk length");
765 setProtocolError(c,pos);
766 return REDIS_ERR;
767 }
768
769 pos += newline-(c->querybuf+pos)+2;
770 if (ll >= REDIS_MBULK_BIG_ARG) {
771 /* If we are going to read a large object from network
772 * try to make it likely that it will start at c->querybuf
773 * boundary so that we can optimized object creation
774 * avoiding a large copy of data. */
775 c->querybuf = sdsrange(c->querybuf,pos,-1);
776 pos = 0;
777 }
778 /* Hint the sds library about the amount of bytes this string is
779 * going to contain. */
780 if (ll >= REDIS_MBULK_BIG_ARG)
781 c->querybuf = sdsMakeRoomFor(c->querybuf,ll+2);
782 c->bulklen = ll;
783 }
784
785 /* Read bulk argument */
786 if (sdslen(c->querybuf)-pos < (unsigned)(c->bulklen+2)) {
787 /* Not enough data (+2 == trailing \r\n) */
788 break;
789 } else {
790 /* Optimization: if the buffer contanins JUST our bulk element
791 * instead of creating a new object by *copying* the sds we
792 * just use the current sds string. */
793 if (pos == 0 &&
794 c->bulklen >= REDIS_MBULK_BIG_ARG &&
795 (signed) sdslen(c->querybuf) == c->bulklen+2)
796 {
797 c->argv[c->argc++] = createObject(REDIS_STRING,c->querybuf);
798 sdsIncrLen(c->querybuf,-2); /* remove CRLF */
799 c->querybuf = sdsempty();
800 /* Assume that if we saw a fat argument we'll see another one
801 * likely... */
802 c->querybuf = sdsMakeRoomFor(c->querybuf,c->bulklen+2);
803 pos = 0;
804 } else {
805 c->argv[c->argc++] =
806 createStringObject(c->querybuf+pos,c->bulklen);
807 pos += c->bulklen+2;
808 }
809 c->bulklen = -1;
810 c->multibulklen--;
811 }
812 }
813
814 /* Trim to pos */
815 if (pos) c->querybuf = sdsrange(c->querybuf,pos,-1);
816
817 /* We're done when c->multibulk == 0 */
818 if (c->multibulklen == 0) {
819 return REDIS_OK;
820 }
821 return REDIS_ERR;
822 }
823
824 void processInputBuffer(redisClient *c) {
825 /* Keep processing while there is something in the input buffer */
826 while(sdslen(c->querybuf)) {
827 /* Immediately abort if the client is in the middle of something. */
828 if (c->flags & REDIS_BLOCKED) return;
829
830 /* REDIS_CLOSE_AFTER_REPLY closes the connection once the reply is
831 * written to the client. Make sure to not let the reply grow after
832 * this flag has been set (i.e. don't process more commands). */
833 if (c->flags & REDIS_CLOSE_AFTER_REPLY) return;
834
835 /* Determine request type when unknown. */
836 if (!c->reqtype) {
837 if (c->querybuf[0] == '*') {
838 c->reqtype = REDIS_REQ_MULTIBULK;
839 } else {
840 c->reqtype = REDIS_REQ_INLINE;
841 }
842 }
843
844 if (c->reqtype == REDIS_REQ_INLINE) {
845 if (processInlineBuffer(c) != REDIS_OK) break;
846 } else if (c->reqtype == REDIS_REQ_MULTIBULK) {
847 if (processMultibulkBuffer(c) != REDIS_OK) break;
848 } else {
849 redisPanic("Unknown request type");
850 }
851
852 /* Multibulk processing could see a <= 0 length. */
853 if (c->argc == 0) {
854 resetClient(c);
855 } else {
856 /* Only reset the client when the command was executed. */
857 if (processCommand(c) == REDIS_OK)
858 resetClient(c);
859 }
860 }
861 }
862
863 void readQueryFromClient(aeEventLoop *el, int fd, void *privdata, int mask) {
864 redisClient *c = (redisClient*) privdata;
865 int nread, readlen;
866 size_t qblen;
867 REDIS_NOTUSED(el);
868 REDIS_NOTUSED(mask);
869
870 readlen = REDIS_IOBUF_LEN;
871 #if REDIS_MBULK_BIG_ARG
872 /* If this is a multi bulk request, and we are processing a bulk reply
873 * that is large enough, try to maximize the probabilty that the query
874 * buffer contains excatly the SDS string representing the object, even
875 * at the risk of requring more read(2) calls. This way the function
876 * processMultiBulkBuffer() can avoid copying buffers to create the
877 * Redis Object representing the argument. */
878 if (c->reqtype == REDIS_REQ_MULTIBULK && c->multibulklen && c->bulklen != -1
879 && c->bulklen >= REDIS_MBULK_BIG_ARG)
880 {
881 int remaining = (unsigned)(c->bulklen+2)-sdslen(c->querybuf);
882
883 if (remaining < readlen) readlen = remaining;
884 }
885 #endif
886
887 qblen = sdslen(c->querybuf);
888 c->querybuf = sdsMakeRoomFor(c->querybuf, readlen);
889 nread = read(fd, c->querybuf+qblen, readlen);
890 if (nread == -1) {
891 if (errno == EAGAIN) {
892 nread = 0;
893 } else {
894 redisLog(REDIS_VERBOSE, "Reading from client: %s",strerror(errno));
895 freeClient(c);
896 return;
897 }
898 } else if (nread == 0) {
899 redisLog(REDIS_VERBOSE, "Client closed connection");
900 freeClient(c);
901 return;
902 }
903 if (nread) {
904 sdsIncrLen(c->querybuf,nread);
905 c->lastinteraction = time(NULL);
906 } else {
907 return;
908 }
909 processInputBuffer(c);
910 }
911
912 void getClientsMaxBuffers(unsigned long *longest_output_list,
913 unsigned long *biggest_input_buffer) {
914 redisClient *c;
915 listNode *ln;
916 listIter li;
917 unsigned long lol = 0, bib = 0;
918
919 listRewind(server.clients,&li);
920 while ((ln = listNext(&li)) != NULL) {
921 c = listNodeValue(ln);
922
923 if (listLength(c->reply) > lol) lol = listLength(c->reply);
924 if (sdslen(c->querybuf) > bib) bib = sdslen(c->querybuf);
925 }
926 *longest_output_list = lol;
927 *biggest_input_buffer = bib;
928 }
929
930 void clientCommand(redisClient *c) {
931 listNode *ln;
932 listIter li;
933 redisClient *client;
934
935 if (!strcasecmp(c->argv[1]->ptr,"list") && c->argc == 2) {
936 sds o = sdsempty();
937 time_t now = time(NULL);
938
939 listRewind(server.clients,&li);
940 while ((ln = listNext(&li)) != NULL) {
941 char ip[32], flags[16], *p;
942 int port;
943
944 client = listNodeValue(ln);
945 if (anetPeerToString(client->fd,ip,&port) == -1) continue;
946 p = flags;
947 if (client->flags & REDIS_SLAVE) {
948 if (client->flags & REDIS_MONITOR)
949 *p++ = 'O';
950 else
951 *p++ = 'S';
952 }
953 if (client->flags & REDIS_MASTER) *p++ = 'M';
954 if (p == flags) *p++ = 'N';
955 if (client->flags & REDIS_MULTI) *p++ = 'x';
956 if (client->flags & REDIS_BLOCKED) *p++ = 'b';
957 if (client->flags & REDIS_DIRTY_CAS) *p++ = 'd';
958 if (client->flags & REDIS_CLOSE_AFTER_REPLY) *p++ = 'c';
959 if (client->flags & REDIS_UNBLOCKED) *p++ = 'u';
960 *p++ = '\0';
961 o = sdscatprintf(o,
962 "addr=%s:%d fd=%d idle=%ld flags=%s db=%d sub=%d psub=%d\n",
963 ip,port,client->fd,
964 (long)(now - client->lastinteraction),
965 flags,
966 client->db->id,
967 (int) dictSize(client->pubsub_channels),
968 (int) listLength(client->pubsub_patterns));
969 }
970 addReplyBulkCBuffer(c,o,sdslen(o));
971 sdsfree(o);
972 } else if (!strcasecmp(c->argv[1]->ptr,"kill") && c->argc == 3) {
973 listRewind(server.clients,&li);
974 while ((ln = listNext(&li)) != NULL) {
975 char ip[32], addr[64];
976 int port;
977
978 client = listNodeValue(ln);
979 if (anetPeerToString(client->fd,ip,&port) == -1) continue;
980 snprintf(addr,sizeof(addr),"%s:%d",ip,port);
981 if (strcmp(addr,c->argv[2]->ptr) == 0) {
982 addReply(c,shared.ok);
983 if (c == client) {
984 client->flags |= REDIS_CLOSE_AFTER_REPLY;
985 } else {
986 freeClient(client);
987 }
988 return;
989 }
990 }
991 addReplyError(c,"No such client");
992 } else {
993 addReplyError(c, "Syntax error, try CLIENT (LIST | KILL ip:port)");
994 }
995 }
996
997 /* Rewrite the command vector of the client. All the new objects ref count
998 * is incremented. The old command vector is freed, and the old objects
999 * ref count is decremented. */
1000 void rewriteClientCommandVector(redisClient *c, int argc, ...) {
1001 va_list ap;
1002 int j;
1003 robj **argv; /* The new argument vector */
1004
1005 argv = zmalloc(sizeof(robj*)*argc);
1006 va_start(ap,argc);
1007 for (j = 0; j < argc; j++) {
1008 robj *a;
1009
1010 a = va_arg(ap, robj*);
1011 argv[j] = a;
1012 incrRefCount(a);
1013 }
1014 /* We free the objects in the original vector at the end, so we are
1015 * sure that if the same objects are reused in the new vector the
1016 * refcount gets incremented before it gets decremented. */
1017 for (j = 0; j < c->argc; j++) decrRefCount(c->argv[j]);
1018 zfree(c->argv);
1019 /* Replace argv and argc with our new versions. */
1020 c->argv = argv;
1021 c->argc = argc;
1022 c->cmd = lookupCommand(c->argv[0]->ptr);
1023 redisAssertWithInfo(c,NULL,c->cmd != NULL);
1024 va_end(ap);
1025 }
1026
1027 /* Rewrite a single item in the command vector.
1028 * The new val ref count is incremented, and the old decremented. */
1029 void rewriteClientCommandArgument(redisClient *c, int i, robj *newval) {
1030 robj *oldval;
1031
1032 redisAssertWithInfo(c,NULL,i < c->argc);
1033 oldval = c->argv[i];
1034 c->argv[i] = newval;
1035 incrRefCount(newval);
1036 decrRefCount(oldval);
1037
1038 /* If this is the command name make sure to fix c->cmd. */
1039 if (i == 0) {
1040 c->cmd = lookupCommand(c->argv[0]->ptr);
1041 redisAssertWithInfo(c,NULL,c->cmd != NULL);
1042 }
1043 }