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