xref: /redis-3.2.3/src/networking.c (revision f3fd58eb)
1 /*
2  * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  *   * Redistributions of source code must retain the above copyright notice,
9  *     this list of conditions and the following disclaimer.
10  *   * Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  *   * Neither the name of Redis nor the names of its contributors may be used
14  *     to endorse or promote products derived from this software without
15  *     specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include "redis.h"
31 #include <sys/uio.h>
32 #include <math.h>
33 
34 static void setProtocolError(redisClient *c, int pos);
35 
36 /* To evaluate the output buffer size of a client we need to get size of
37  * allocated objects, however we can't used zmalloc_size() directly on sds
38  * strings because of the trick they use to work (the header is before the
39  * returned pointer), so we use this helper function. */
40 size_t zmalloc_size_sds(sds s) {
41     return zmalloc_size(s-sizeof(struct sdshdr));
42 }
43 
44 /* Return the amount of memory used by the sds string at object->ptr
45  * for a string object. */
46 size_t getStringObjectSdsUsedMemory(robj *o) {
47     redisAssertWithInfo(NULL,o,o->type == REDIS_STRING);
48     switch(o->encoding) {
49     case REDIS_ENCODING_RAW: return zmalloc_size_sds(o->ptr);
50     case REDIS_ENCODING_EMBSTR: return sdslen(o->ptr);
51     default: return 0; /* Just integer encoding for now. */
52     }
53 }
54 
55 void *dupClientReplyValue(void *o) {
56     incrRefCount((robj*)o);
57     return o;
58 }
59 
60 int listMatchObjects(void *a, void *b) {
61     return equalStringObjects(a,b);
62 }
63 
64 redisClient *createClient(int fd) {
65     redisClient *c = zmalloc(sizeof(redisClient));
66 
67     /* passing -1 as fd it is possible to create a non connected client.
68      * This is useful since all the Redis commands needs to be executed
69      * in the context of a client. When commands are executed in other
70      * contexts (for instance a Lua script) we need a non connected client. */
71     if (fd != -1) {
72         anetNonBlock(NULL,fd);
73         anetEnableTcpNoDelay(NULL,fd);
74         if (server.tcpkeepalive)
75             anetKeepAlive(NULL,fd,server.tcpkeepalive);
76         if (aeCreateFileEvent(server.el,fd,AE_READABLE,
77             readQueryFromClient, c) == AE_ERR)
78         {
79             close(fd);
80             zfree(c);
81             return NULL;
82         }
83     }
84 
85     selectDb(c,0);
86     c->id = server.next_client_id++;
87     c->fd = fd;
88     c->name = NULL;
89     c->bufpos = 0;
90     c->querybuf = sdsempty();
91     c->querybuf_peak = 0;
92     c->reqtype = 0;
93     c->argc = 0;
94     c->argv = NULL;
95     c->cmd = c->lastcmd = NULL;
96     c->multibulklen = 0;
97     c->bulklen = -1;
98     c->sentlen = 0;
99     c->flags = 0;
100     c->ctime = c->lastinteraction = server.unixtime;
101     c->authenticated = 0;
102     c->replstate = REDIS_REPL_NONE;
103     c->repl_put_online_on_ack = 0;
104     c->reploff = 0;
105     c->repl_ack_off = 0;
106     c->repl_ack_time = 0;
107     c->slave_listening_port = 0;
108     c->reply = listCreate();
109     c->reply_bytes = 0;
110     c->obuf_soft_limit_reached_time = 0;
111     listSetFreeMethod(c->reply,decrRefCountVoid);
112     listSetDupMethod(c->reply,dupClientReplyValue);
113     c->btype = REDIS_BLOCKED_NONE;
114     c->bpop.timeout = 0;
115     c->bpop.keys = dictCreate(&setDictType,NULL);
116     c->bpop.target = NULL;
117     c->bpop.numreplicas = 0;
118     c->bpop.reploffset = 0;
119     c->woff = 0;
120     c->watched_keys = listCreate();
121     c->pubsub_channels = dictCreate(&setDictType,NULL);
122     c->pubsub_patterns = listCreate();
123     c->peerid = NULL;
124     listSetFreeMethod(c->pubsub_patterns,decrRefCountVoid);
125     listSetMatchMethod(c->pubsub_patterns,listMatchObjects);
126     if (fd != -1) listAddNodeTail(server.clients,c);
127     initClientMultiState(c);
128     return c;
129 }
130 
131 /* This function is called every time we are going to transmit new data
132  * to the client. The behavior is the following:
133  *
134  * If the client should receive new data (normal clients will) the function
135  * returns REDIS_OK, and make sure to install the write handler in our event
136  * loop so that when the socket is writable new data gets written.
137  *
138  * If the client should not receive new data, because it is a fake client,
139  * a master, a slave not yet online, or because the setup of the write handler
140  * failed, the function returns REDIS_ERR.
141  *
142  * Typically gets called every time a reply is built, before adding more
143  * data to the clients output buffers. If the function returns REDIS_ERR no
144  * data should be appended to the output buffers. */
145 int prepareClientToWrite(redisClient *c) {
146     if (c->flags & REDIS_LUA_CLIENT) return REDIS_OK;
147     if ((c->flags & REDIS_MASTER) &&
148         !(c->flags & REDIS_MASTER_FORCE_REPLY)) return REDIS_ERR;
149     if (c->fd <= 0) return REDIS_ERR; /* Fake client */
150     if (c->bufpos == 0 && listLength(c->reply) == 0 &&
151         (c->replstate == REDIS_REPL_NONE ||
152          c->replstate == REDIS_REPL_ONLINE) &&
153         aeCreateFileEvent(server.el, c->fd, AE_WRITABLE,
154         sendReplyToClient, c) == AE_ERR) return REDIS_ERR;
155     return REDIS_OK;
156 }
157 
158 /* Create a duplicate of the last object in the reply list when
159  * it is not exclusively owned by the reply list. */
160 robj *dupLastObjectIfNeeded(list *reply) {
161     robj *new, *cur;
162     listNode *ln;
163     redisAssert(listLength(reply) > 0);
164     ln = listLast(reply);
165     cur = listNodeValue(ln);
166     if (cur->refcount > 1) {
167         new = dupStringObject(cur);
168         decrRefCount(cur);
169         listNodeValue(ln) = new;
170     }
171     return listNodeValue(ln);
172 }
173 
174 /* -----------------------------------------------------------------------------
175  * Low level functions to add more data to output buffers.
176  * -------------------------------------------------------------------------- */
177 
178 int _addReplyToBuffer(redisClient *c, char *s, size_t len) {
179     size_t available = sizeof(c->buf)-c->bufpos;
180 
181     if (c->flags & REDIS_CLOSE_AFTER_REPLY) return REDIS_OK;
182 
183     /* If there already are entries in the reply list, we cannot
184      * add anything more to the static buffer. */
185     if (listLength(c->reply) > 0) return REDIS_ERR;
186 
187     /* Check that the buffer has enough space available for this string. */
188     if (len > available) return REDIS_ERR;
189 
190     memcpy(c->buf+c->bufpos,s,len);
191     c->bufpos+=len;
192     return REDIS_OK;
193 }
194 
195 void _addReplyObjectToList(redisClient *c, robj *o) {
196     robj *tail;
197 
198     if (c->flags & REDIS_CLOSE_AFTER_REPLY) return;
199 
200     if (listLength(c->reply) == 0) {
201         incrRefCount(o);
202         listAddNodeTail(c->reply,o);
203         c->reply_bytes += getStringObjectSdsUsedMemory(o);
204     } else {
205         tail = listNodeValue(listLast(c->reply));
206 
207         /* Append to this object when possible. */
208         if (tail->ptr != NULL &&
209             tail->encoding == REDIS_ENCODING_RAW &&
210             sdslen(tail->ptr)+sdslen(o->ptr) <= REDIS_REPLY_CHUNK_BYTES)
211         {
212             c->reply_bytes -= zmalloc_size_sds(tail->ptr);
213             tail = dupLastObjectIfNeeded(c->reply);
214             tail->ptr = sdscatlen(tail->ptr,o->ptr,sdslen(o->ptr));
215             c->reply_bytes += zmalloc_size_sds(tail->ptr);
216         } else {
217             incrRefCount(o);
218             listAddNodeTail(c->reply,o);
219             c->reply_bytes += getStringObjectSdsUsedMemory(o);
220         }
221     }
222     asyncCloseClientOnOutputBufferLimitReached(c);
223 }
224 
225 /* This method takes responsibility over the sds. When it is no longer
226  * needed it will be free'd, otherwise it ends up in a robj. */
227 void _addReplySdsToList(redisClient *c, sds s) {
228     robj *tail;
229 
230     if (c->flags & REDIS_CLOSE_AFTER_REPLY) {
231         sdsfree(s);
232         return;
233     }
234 
235     if (listLength(c->reply) == 0) {
236         listAddNodeTail(c->reply,createObject(REDIS_STRING,s));
237         c->reply_bytes += zmalloc_size_sds(s);
238     } else {
239         tail = listNodeValue(listLast(c->reply));
240 
241         /* Append to this object when possible. */
242         if (tail->ptr != NULL && tail->encoding == REDIS_ENCODING_RAW &&
243             sdslen(tail->ptr)+sdslen(s) <= REDIS_REPLY_CHUNK_BYTES)
244         {
245             c->reply_bytes -= zmalloc_size_sds(tail->ptr);
246             tail = dupLastObjectIfNeeded(c->reply);
247             tail->ptr = sdscatlen(tail->ptr,s,sdslen(s));
248             c->reply_bytes += zmalloc_size_sds(tail->ptr);
249             sdsfree(s);
250         } else {
251             listAddNodeTail(c->reply,createObject(REDIS_STRING,s));
252             c->reply_bytes += zmalloc_size_sds(s);
253         }
254     }
255     asyncCloseClientOnOutputBufferLimitReached(c);
256 }
257 
258 void _addReplyStringToList(redisClient *c, char *s, size_t len) {
259     robj *tail;
260 
261     if (c->flags & REDIS_CLOSE_AFTER_REPLY) return;
262 
263     if (listLength(c->reply) == 0) {
264         robj *o = createStringObject(s,len);
265 
266         listAddNodeTail(c->reply,o);
267         c->reply_bytes += getStringObjectSdsUsedMemory(o);
268     } else {
269         tail = listNodeValue(listLast(c->reply));
270 
271         /* Append to this object when possible. */
272         if (tail->ptr != NULL && tail->encoding == REDIS_ENCODING_RAW &&
273             sdslen(tail->ptr)+len <= REDIS_REPLY_CHUNK_BYTES)
274         {
275             c->reply_bytes -= zmalloc_size_sds(tail->ptr);
276             tail = dupLastObjectIfNeeded(c->reply);
277             tail->ptr = sdscatlen(tail->ptr,s,len);
278             c->reply_bytes += zmalloc_size_sds(tail->ptr);
279         } else {
280             robj *o = createStringObject(s,len);
281 
282             listAddNodeTail(c->reply,o);
283             c->reply_bytes += getStringObjectSdsUsedMemory(o);
284         }
285     }
286     asyncCloseClientOnOutputBufferLimitReached(c);
287 }
288 
289 /* -----------------------------------------------------------------------------
290  * Higher level functions to queue data on the client output buffer.
291  * The following functions are the ones that commands implementations will call.
292  * -------------------------------------------------------------------------- */
293 
294 void addReply(redisClient *c, robj *obj) {
295     if (prepareClientToWrite(c) != REDIS_OK) return;
296 
297     /* This is an important place where we can avoid copy-on-write
298      * when there is a saving child running, avoiding touching the
299      * refcount field of the object if it's not needed.
300      *
301      * If the encoding is RAW and there is room in the static buffer
302      * we'll be able to send the object to the client without
303      * messing with its page. */
304     if (sdsEncodedObject(obj)) {
305         if (_addReplyToBuffer(c,obj->ptr,sdslen(obj->ptr)) != REDIS_OK)
306             _addReplyObjectToList(c,obj);
307     } else if (obj->encoding == REDIS_ENCODING_INT) {
308         /* Optimization: if there is room in the static buffer for 32 bytes
309          * (more than the max chars a 64 bit integer can take as string) we
310          * avoid decoding the object and go for the lower level approach. */
311         if (listLength(c->reply) == 0 && (sizeof(c->buf) - c->bufpos) >= 32) {
312             char buf[32];
313             int len;
314 
315             len = ll2string(buf,sizeof(buf),(long)obj->ptr);
316             if (_addReplyToBuffer(c,buf,len) == REDIS_OK)
317                 return;
318             /* else... continue with the normal code path, but should never
319              * happen actually since we verified there is room. */
320         }
321         obj = getDecodedObject(obj);
322         if (_addReplyToBuffer(c,obj->ptr,sdslen(obj->ptr)) != REDIS_OK)
323             _addReplyObjectToList(c,obj);
324         decrRefCount(obj);
325     } else {
326         redisPanic("Wrong obj->encoding in addReply()");
327     }
328 }
329 
330 void addReplySds(redisClient *c, sds s) {
331     if (prepareClientToWrite(c) != REDIS_OK) {
332         /* The caller expects the sds to be free'd. */
333         sdsfree(s);
334         return;
335     }
336     if (_addReplyToBuffer(c,s,sdslen(s)) == REDIS_OK) {
337         sdsfree(s);
338     } else {
339         /* This method free's the sds when it is no longer needed. */
340         _addReplySdsToList(c,s);
341     }
342 }
343 
344 void addReplyString(redisClient *c, char *s, size_t len) {
345     if (prepareClientToWrite(c) != REDIS_OK) return;
346     if (_addReplyToBuffer(c,s,len) != REDIS_OK)
347         _addReplyStringToList(c,s,len);
348 }
349 
350 void addReplyErrorLength(redisClient *c, char *s, size_t len) {
351     addReplyString(c,"-ERR ",5);
352     addReplyString(c,s,len);
353     addReplyString(c,"\r\n",2);
354 }
355 
356 void addReplyError(redisClient *c, char *err) {
357     addReplyErrorLength(c,err,strlen(err));
358 }
359 
360 void addReplyErrorFormat(redisClient *c, const char *fmt, ...) {
361     size_t l, j;
362     va_list ap;
363     va_start(ap,fmt);
364     sds s = sdscatvprintf(sdsempty(),fmt,ap);
365     va_end(ap);
366     /* Make sure there are no newlines in the string, otherwise invalid protocol
367      * is emitted. */
368     l = sdslen(s);
369     for (j = 0; j < l; j++) {
370         if (s[j] == '\r' || s[j] == '\n') s[j] = ' ';
371     }
372     addReplyErrorLength(c,s,sdslen(s));
373     sdsfree(s);
374 }
375 
376 void addReplyStatusLength(redisClient *c, char *s, size_t len) {
377     addReplyString(c,"+",1);
378     addReplyString(c,s,len);
379     addReplyString(c,"\r\n",2);
380 }
381 
382 void addReplyStatus(redisClient *c, char *status) {
383     addReplyStatusLength(c,status,strlen(status));
384 }
385 
386 void addReplyStatusFormat(redisClient *c, const char *fmt, ...) {
387     va_list ap;
388     va_start(ap,fmt);
389     sds s = sdscatvprintf(sdsempty(),fmt,ap);
390     va_end(ap);
391     addReplyStatusLength(c,s,sdslen(s));
392     sdsfree(s);
393 }
394 
395 /* Adds an empty object to the reply list that will contain the multi bulk
396  * length, which is not known when this function is called. */
397 void *addDeferredMultiBulkLength(redisClient *c) {
398     /* Note that we install the write event here even if the object is not
399      * ready to be sent, since we are sure that before returning to the
400      * event loop setDeferredMultiBulkLength() will be called. */
401     if (prepareClientToWrite(c) != REDIS_OK) return NULL;
402     listAddNodeTail(c->reply,createObject(REDIS_STRING,NULL));
403     return listLast(c->reply);
404 }
405 
406 /* Populate the length object and try gluing it to the next chunk. */
407 void setDeferredMultiBulkLength(redisClient *c, void *node, long length) {
408     listNode *ln = (listNode*)node;
409     robj *len, *next;
410 
411     /* Abort when *node is NULL (see addDeferredMultiBulkLength). */
412     if (node == NULL) return;
413 
414     len = listNodeValue(ln);
415     len->ptr = sdscatprintf(sdsempty(),"*%ld\r\n",length);
416     len->encoding = REDIS_ENCODING_RAW; /* in case it was an EMBSTR. */
417     c->reply_bytes += zmalloc_size_sds(len->ptr);
418     if (ln->next != NULL) {
419         next = listNodeValue(ln->next);
420 
421         /* Only glue when the next node is non-NULL (an sds in this case) */
422         if (next->ptr != NULL) {
423             c->reply_bytes -= zmalloc_size_sds(len->ptr);
424             c->reply_bytes -= getStringObjectSdsUsedMemory(next);
425             len->ptr = sdscatlen(len->ptr,next->ptr,sdslen(next->ptr));
426             c->reply_bytes += zmalloc_size_sds(len->ptr);
427             listDelNode(c->reply,ln->next);
428         }
429     }
430     asyncCloseClientOnOutputBufferLimitReached(c);
431 }
432 
433 /* Add a double as a bulk reply */
434 void addReplyDouble(redisClient *c, double d) {
435     char dbuf[128], sbuf[128];
436     int dlen, slen;
437     if (isinf(d)) {
438         /* Libc in odd systems (Hi Solaris!) will format infinite in a
439          * different way, so better to handle it in an explicit way. */
440         addReplyBulkCString(c, d > 0 ? "inf" : "-inf");
441     } else {
442         dlen = snprintf(dbuf,sizeof(dbuf),"%.17g",d);
443         slen = snprintf(sbuf,sizeof(sbuf),"$%d\r\n%s\r\n",dlen,dbuf);
444         addReplyString(c,sbuf,slen);
445     }
446 }
447 
448 /* Add a long long as integer reply or bulk len / multi bulk count.
449  * Basically this is used to output <prefix><long long><crlf>. */
450 void addReplyLongLongWithPrefix(redisClient *c, long long ll, char prefix) {
451     char buf[128];
452     int len;
453 
454     /* Things like $3\r\n or *2\r\n are emitted very often by the protocol
455      * so we have a few shared objects to use if the integer is small
456      * like it is most of the times. */
457     if (prefix == '*' && ll < REDIS_SHARED_BULKHDR_LEN) {
458         addReply(c,shared.mbulkhdr[ll]);
459         return;
460     } else if (prefix == '$' && ll < REDIS_SHARED_BULKHDR_LEN) {
461         addReply(c,shared.bulkhdr[ll]);
462         return;
463     }
464 
465     buf[0] = prefix;
466     len = ll2string(buf+1,sizeof(buf)-1,ll);
467     buf[len+1] = '\r';
468     buf[len+2] = '\n';
469     addReplyString(c,buf,len+3);
470 }
471 
472 void addReplyLongLong(redisClient *c, long long ll) {
473     if (ll == 0)
474         addReply(c,shared.czero);
475     else if (ll == 1)
476         addReply(c,shared.cone);
477     else
478         addReplyLongLongWithPrefix(c,ll,':');
479 }
480 
481 void addReplyMultiBulkLen(redisClient *c, long length) {
482     if (length < REDIS_SHARED_BULKHDR_LEN)
483         addReply(c,shared.mbulkhdr[length]);
484     else
485         addReplyLongLongWithPrefix(c,length,'*');
486 }
487 
488 /* Create the length prefix of a bulk reply, example: $2234 */
489 void addReplyBulkLen(redisClient *c, robj *obj) {
490     size_t len;
491 
492     if (sdsEncodedObject(obj)) {
493         len = sdslen(obj->ptr);
494     } else {
495         long n = (long)obj->ptr;
496 
497         /* Compute how many bytes will take this integer as a radix 10 string */
498         len = 1;
499         if (n < 0) {
500             len++;
501             n = -n;
502         }
503         while((n = n/10) != 0) {
504             len++;
505         }
506     }
507 
508     if (len < REDIS_SHARED_BULKHDR_LEN)
509         addReply(c,shared.bulkhdr[len]);
510     else
511         addReplyLongLongWithPrefix(c,len,'$');
512 }
513 
514 /* Add a Redis Object as a bulk reply */
515 void addReplyBulk(redisClient *c, robj *obj) {
516     addReplyBulkLen(c,obj);
517     addReply(c,obj);
518     addReply(c,shared.crlf);
519 }
520 
521 /* Add a C buffer as bulk reply */
522 void addReplyBulkCBuffer(redisClient *c, void *p, size_t len) {
523     addReplyLongLongWithPrefix(c,len,'$');
524     addReplyString(c,p,len);
525     addReply(c,shared.crlf);
526 }
527 
528 /* Add sds to reply (takes ownership of sds and frees it) */
529 void addReplyBulkSds(redisClient *c, sds s)  {
530     addReplySds(c,sdscatfmt(sdsempty(),"$%u\r\n",
531         (unsigned long)sdslen(s)));
532     addReplySds(c,s);
533     addReply(c,shared.crlf);
534 }
535 
536 /* Add a C nul term string as bulk reply */
537 void addReplyBulkCString(redisClient *c, char *s) {
538     if (s == NULL) {
539         addReply(c,shared.nullbulk);
540     } else {
541         addReplyBulkCBuffer(c,s,strlen(s));
542     }
543 }
544 
545 /* Add a long long as a bulk reply */
546 void addReplyBulkLongLong(redisClient *c, long long ll) {
547     char buf[64];
548     int len;
549 
550     len = ll2string(buf,64,ll);
551     addReplyBulkCBuffer(c,buf,len);
552 }
553 
554 /* Copy 'src' client output buffers into 'dst' client output buffers.
555  * The function takes care of freeing the old output buffers of the
556  * destination client. */
557 void copyClientOutputBuffer(redisClient *dst, redisClient *src) {
558     listRelease(dst->reply);
559     dst->reply = listDup(src->reply);
560     memcpy(dst->buf,src->buf,src->bufpos);
561     dst->bufpos = src->bufpos;
562     dst->reply_bytes = src->reply_bytes;
563 }
564 
565 #define MAX_ACCEPTS_PER_CALL 1000
566 static void acceptCommonHandler(int fd, int flags) {
567     redisClient *c;
568     if ((c = createClient(fd)) == NULL) {
569         redisLog(REDIS_WARNING,
570             "Error registering fd event for the new client: %s (fd=%d)",
571             strerror(errno),fd);
572         close(fd); /* May be already closed, just ignore errors */
573         return;
574     }
575     /* If maxclient directive is set and this is one client more... close the
576      * connection. Note that we create the client instead to check before
577      * for this condition, since now the socket is already set in non-blocking
578      * mode and we can send an error for free using the Kernel I/O */
579     if (listLength(server.clients) > server.maxclients) {
580         char *err = "-ERR max number of clients reached\r\n";
581 
582         /* That's a best effort error message, don't check write errors */
583         if (write(c->fd,err,strlen(err)) == -1) {
584             /* Nothing to do, Just to avoid the warning... */
585         }
586         server.stat_rejected_conn++;
587         freeClient(c);
588         return;
589     }
590     server.stat_numconnections++;
591     c->flags |= flags;
592 }
593 
594 void acceptTcpHandler(aeEventLoop *el, int fd, void *privdata, int mask) {
595     int cport, cfd, max = MAX_ACCEPTS_PER_CALL;
596     char cip[REDIS_IP_STR_LEN];
597     REDIS_NOTUSED(el);
598     REDIS_NOTUSED(mask);
599     REDIS_NOTUSED(privdata);
600 
601     while(max--) {
602         cfd = anetTcpAccept(server.neterr, fd, cip, sizeof(cip), &cport);
603         if (cfd == ANET_ERR) {
604             if (errno != EWOULDBLOCK)
605                 redisLog(REDIS_WARNING,
606                     "Accepting client connection: %s", server.neterr);
607             return;
608         }
609         redisLog(REDIS_VERBOSE,"Accepted %s:%d", cip, cport);
610         acceptCommonHandler(cfd,0);
611     }
612 }
613 
614 void acceptUnixHandler(aeEventLoop *el, int fd, void *privdata, int mask) {
615     int cfd, max = MAX_ACCEPTS_PER_CALL;
616     REDIS_NOTUSED(el);
617     REDIS_NOTUSED(mask);
618     REDIS_NOTUSED(privdata);
619 
620     while(max--) {
621         cfd = anetUnixAccept(server.neterr, fd);
622         if (cfd == ANET_ERR) {
623             if (errno != EWOULDBLOCK)
624                 redisLog(REDIS_WARNING,
625                     "Accepting client connection: %s", server.neterr);
626             return;
627         }
628         redisLog(REDIS_VERBOSE,"Accepted connection to %s", server.unixsocket);
629         acceptCommonHandler(cfd,REDIS_UNIX_SOCKET);
630     }
631 }
632 
633 static void freeClientArgv(redisClient *c) {
634     int j;
635     for (j = 0; j < c->argc; j++)
636         decrRefCount(c->argv[j]);
637     c->argc = 0;
638     c->cmd = NULL;
639 }
640 
641 /* Close all the slaves connections. This is useful in chained replication
642  * when we resync with our own master and want to force all our slaves to
643  * resync with us as well. */
644 void disconnectSlaves(void) {
645     while (listLength(server.slaves)) {
646         listNode *ln = listFirst(server.slaves);
647         freeClient((redisClient*)ln->value);
648     }
649 }
650 
651 /* This function is called when the slave lose the connection with the
652  * master into an unexpected way. */
653 void replicationHandleMasterDisconnection(void) {
654     server.master = NULL;
655     server.repl_state = REDIS_REPL_CONNECT;
656     server.repl_down_since = server.unixtime;
657     /* We lost connection with our master, force our slaves to resync
658      * with us as well to load the new data set.
659      *
660      * If server.masterhost is NULL the user called SLAVEOF NO ONE so
661      * slave resync is not needed. */
662     if (server.masterhost != NULL) disconnectSlaves();
663 }
664 
665 void freeClient(redisClient *c) {
666     listNode *ln;
667 
668     /* If this is marked as current client unset it */
669     if (server.current_client == c) server.current_client = NULL;
670 
671     /* If it is our master that's beging disconnected we should make sure
672      * to cache the state to try a partial resynchronization later.
673      *
674      * Note that before doing this we make sure that the client is not in
675      * some unexpected state, by checking its flags. */
676     if (server.master && c->flags & REDIS_MASTER) {
677         redisLog(REDIS_WARNING,"Connection with master lost.");
678         if (!(c->flags & (REDIS_CLOSE_AFTER_REPLY|
679                           REDIS_CLOSE_ASAP|
680                           REDIS_BLOCKED|
681                           REDIS_UNBLOCKED)))
682         {
683             replicationCacheMaster(c);
684             return;
685         }
686     }
687 
688     /* Log link disconnection with slave */
689     if ((c->flags & REDIS_SLAVE) && !(c->flags & REDIS_MONITOR)) {
690         redisLog(REDIS_WARNING,"Connection with slave %s lost.",
691             replicationGetSlaveName(c));
692     }
693 
694     /* Free the query buffer */
695     sdsfree(c->querybuf);
696     c->querybuf = NULL;
697 
698     /* Deallocate structures used to block on blocking ops. */
699     if (c->flags & REDIS_BLOCKED) unblockClient(c);
700     dictRelease(c->bpop.keys);
701 
702     /* UNWATCH all the keys */
703     unwatchAllKeys(c);
704     listRelease(c->watched_keys);
705 
706     /* Unsubscribe from all the pubsub channels */
707     pubsubUnsubscribeAllChannels(c,0);
708     pubsubUnsubscribeAllPatterns(c,0);
709     dictRelease(c->pubsub_channels);
710     listRelease(c->pubsub_patterns);
711 
712     /* Close socket, unregister events, and remove list of replies and
713      * accumulated arguments. */
714     if (c->fd != -1) {
715         aeDeleteFileEvent(server.el,c->fd,AE_READABLE);
716         aeDeleteFileEvent(server.el,c->fd,AE_WRITABLE);
717         close(c->fd);
718     }
719     listRelease(c->reply);
720     freeClientArgv(c);
721 
722     /* Remove from the list of clients */
723     if (c->fd != -1) {
724         ln = listSearchKey(server.clients,c);
725         redisAssert(ln != NULL);
726         listDelNode(server.clients,ln);
727     }
728 
729     /* When client was just unblocked because of a blocking operation,
730      * remove it from the list of unblocked clients. */
731     if (c->flags & REDIS_UNBLOCKED) {
732         ln = listSearchKey(server.unblocked_clients,c);
733         redisAssert(ln != NULL);
734         listDelNode(server.unblocked_clients,ln);
735     }
736 
737     /* Master/slave cleanup Case 1:
738      * we lost the connection with a slave. */
739     if (c->flags & REDIS_SLAVE) {
740         if (c->replstate == REDIS_REPL_SEND_BULK) {
741             if (c->repldbfd != -1) close(c->repldbfd);
742             if (c->replpreamble) sdsfree(c->replpreamble);
743         }
744         list *l = (c->flags & REDIS_MONITOR) ? server.monitors : server.slaves;
745         ln = listSearchKey(l,c);
746         redisAssert(ln != NULL);
747         listDelNode(l,ln);
748         /* We need to remember the time when we started to have zero
749          * attached slaves, as after some time we'll free the replication
750          * backlog. */
751         if (c->flags & REDIS_SLAVE && listLength(server.slaves) == 0)
752             server.repl_no_slaves_since = server.unixtime;
753         refreshGoodSlavesCount();
754     }
755 
756     /* Master/slave cleanup Case 2:
757      * we lost the connection with the master. */
758     if (c->flags & REDIS_MASTER) replicationHandleMasterDisconnection();
759 
760     /* If this client was scheduled for async freeing we need to remove it
761      * from the queue. */
762     if (c->flags & REDIS_CLOSE_ASAP) {
763         ln = listSearchKey(server.clients_to_close,c);
764         redisAssert(ln != NULL);
765         listDelNode(server.clients_to_close,ln);
766     }
767 
768     /* Release other dynamically allocated client structure fields,
769      * and finally release the client structure itself. */
770     if (c->name) decrRefCount(c->name);
771     zfree(c->argv);
772     freeClientMultiState(c);
773     sdsfree(c->peerid);
774     zfree(c);
775 }
776 
777 /* Schedule a client to free it at a safe time in the serverCron() function.
778  * This function is useful when we need to terminate a client but we are in
779  * a context where calling freeClient() is not possible, because the client
780  * should be valid for the continuation of the flow of the program. */
781 void freeClientAsync(redisClient *c) {
782     if (c->flags & REDIS_CLOSE_ASAP) return;
783     c->flags |= REDIS_CLOSE_ASAP;
784     listAddNodeTail(server.clients_to_close,c);
785 }
786 
787 void freeClientsInAsyncFreeQueue(void) {
788     while (listLength(server.clients_to_close)) {
789         listNode *ln = listFirst(server.clients_to_close);
790         redisClient *c = listNodeValue(ln);
791 
792         c->flags &= ~REDIS_CLOSE_ASAP;
793         freeClient(c);
794         listDelNode(server.clients_to_close,ln);
795     }
796 }
797 
798 void sendReplyToClient(aeEventLoop *el, int fd, void *privdata, int mask) {
799     redisClient *c = privdata;
800     int nwritten = 0, totwritten = 0, objlen;
801     size_t objmem;
802     robj *o;
803     REDIS_NOTUSED(el);
804     REDIS_NOTUSED(mask);
805 
806     while(c->bufpos > 0 || listLength(c->reply)) {
807         if (c->bufpos > 0) {
808             nwritten = write(fd,c->buf+c->sentlen,c->bufpos-c->sentlen);
809             if (nwritten <= 0) break;
810             c->sentlen += nwritten;
811             totwritten += nwritten;
812 
813             /* If the buffer was sent, set bufpos to zero to continue with
814              * the remainder of the reply. */
815             if (c->sentlen == c->bufpos) {
816                 c->bufpos = 0;
817                 c->sentlen = 0;
818             }
819         } else {
820             o = listNodeValue(listFirst(c->reply));
821             objlen = sdslen(o->ptr);
822             objmem = getStringObjectSdsUsedMemory(o);
823 
824             if (objlen == 0) {
825                 listDelNode(c->reply,listFirst(c->reply));
826                 c->reply_bytes -= objmem;
827                 continue;
828             }
829 
830             nwritten = write(fd, ((char*)o->ptr)+c->sentlen,objlen-c->sentlen);
831             if (nwritten <= 0) break;
832             c->sentlen += nwritten;
833             totwritten += nwritten;
834 
835             /* If we fully sent the object on head go to the next one */
836             if (c->sentlen == objlen) {
837                 listDelNode(c->reply,listFirst(c->reply));
838                 c->sentlen = 0;
839                 c->reply_bytes -= objmem;
840             }
841         }
842         /* Note that we avoid to send more than REDIS_MAX_WRITE_PER_EVENT
843          * bytes, in a single threaded server it's a good idea to serve
844          * other clients as well, even if a very large request comes from
845          * super fast link that is always able to accept data (in real world
846          * scenario think about 'KEYS *' against the loopback interface).
847          *
848          * However if we are over the maxmemory limit we ignore that and
849          * just deliver as much data as it is possible to deliver. */
850         server.stat_net_output_bytes += totwritten;
851         if (totwritten > REDIS_MAX_WRITE_PER_EVENT &&
852             (server.maxmemory == 0 ||
853              zmalloc_used_memory() < server.maxmemory)) break;
854     }
855     if (nwritten == -1) {
856         if (errno == EAGAIN) {
857             nwritten = 0;
858         } else {
859             redisLog(REDIS_VERBOSE,
860                 "Error writing to client: %s", strerror(errno));
861             freeClient(c);
862             return;
863         }
864     }
865     if (totwritten > 0) {
866         /* For clients representing masters we don't count sending data
867          * as an interaction, since we always send REPLCONF ACK commands
868          * that take some time to just fill the socket output buffer.
869          * We just rely on data / pings received for timeout detection. */
870         if (!(c->flags & REDIS_MASTER)) c->lastinteraction = server.unixtime;
871     }
872     if (c->bufpos == 0 && listLength(c->reply) == 0) {
873         c->sentlen = 0;
874         aeDeleteFileEvent(server.el,c->fd,AE_WRITABLE);
875 
876         /* Close connection after entire reply has been sent. */
877         if (c->flags & REDIS_CLOSE_AFTER_REPLY) freeClient(c);
878     }
879 }
880 
881 /* resetClient prepare the client to process the next command */
882 void resetClient(redisClient *c) {
883     redisCommandProc *prevcmd = c->cmd ? c->cmd->proc : NULL;
884 
885     freeClientArgv(c);
886     c->reqtype = 0;
887     c->multibulklen = 0;
888     c->bulklen = -1;
889     /* We clear the ASKING flag as well if we are not inside a MULTI, and
890      * if what we just executed is not the ASKING command itself. */
891     if (!(c->flags & REDIS_MULTI) && prevcmd != askingCommand)
892         c->flags &= (~REDIS_ASKING);
893 }
894 
895 int processInlineBuffer(redisClient *c) {
896     char *newline;
897     int argc, j;
898     sds *argv, aux;
899     size_t querylen;
900 
901     /* Search for end of line */
902     newline = strchr(c->querybuf,'\n');
903 
904     /* Nothing to do without a \r\n */
905     if (newline == NULL) {
906         if (sdslen(c->querybuf) > REDIS_INLINE_MAX_SIZE) {
907             addReplyError(c,"Protocol error: too big inline request");
908             setProtocolError(c,0);
909         }
910         return REDIS_ERR;
911     }
912 
913     /* Handle the \r\n case. */
914     if (newline && newline != c->querybuf && *(newline-1) == '\r')
915         newline--;
916 
917     /* Split the input buffer up to the \r\n */
918     querylen = newline-(c->querybuf);
919     aux = sdsnewlen(c->querybuf,querylen);
920     argv = sdssplitargs(aux,&argc);
921     sdsfree(aux);
922     if (argv == NULL) {
923         addReplyError(c,"Protocol error: unbalanced quotes in request");
924         setProtocolError(c,0);
925         return REDIS_ERR;
926     }
927 
928     /* Newline from slaves can be used to refresh the last ACK time.
929      * This is useful for a slave to ping back while loading a big
930      * RDB file. */
931     if (querylen == 0 && c->flags & REDIS_SLAVE)
932         c->repl_ack_time = server.unixtime;
933 
934     /* Leave data after the first line of the query in the buffer */
935     sdsrange(c->querybuf,querylen+2,-1);
936 
937     /* Setup argv array on client structure */
938     if (argc) {
939         if (c->argv) zfree(c->argv);
940         c->argv = zmalloc(sizeof(robj*)*argc);
941     }
942 
943     /* Create redis objects for all arguments. */
944     for (c->argc = 0, j = 0; j < argc; j++) {
945         if (sdslen(argv[j])) {
946             c->argv[c->argc] = createObject(REDIS_STRING,argv[j]);
947             c->argc++;
948         } else {
949             sdsfree(argv[j]);
950         }
951     }
952     zfree(argv);
953     return REDIS_OK;
954 }
955 
956 /* Helper function. Trims query buffer to make the function that processes
957  * multi bulk requests idempotent. */
958 static void setProtocolError(redisClient *c, int pos) {
959     if (server.verbosity >= REDIS_VERBOSE) {
960         sds client = catClientInfoString(sdsempty(),c);
961         redisLog(REDIS_VERBOSE,
962             "Protocol error from client: %s", client);
963         sdsfree(client);
964     }
965     c->flags |= REDIS_CLOSE_AFTER_REPLY;
966     sdsrange(c->querybuf,pos,-1);
967 }
968 
969 int processMultibulkBuffer(redisClient *c) {
970     char *newline = NULL;
971     int pos = 0, ok;
972     long long ll;
973 
974     if (c->multibulklen == 0) {
975         /* The client should have been reset */
976         redisAssertWithInfo(c,NULL,c->argc == 0);
977 
978         /* Multi bulk length cannot be read without a \r\n */
979         newline = strchr(c->querybuf,'\r');
980         if (newline == NULL) {
981             if (sdslen(c->querybuf) > REDIS_INLINE_MAX_SIZE) {
982                 addReplyError(c,"Protocol error: too big mbulk count string");
983                 setProtocolError(c,0);
984             }
985             return REDIS_ERR;
986         }
987 
988         /* Buffer should also contain \n */
989         if (newline-(c->querybuf) > ((signed)sdslen(c->querybuf)-2))
990             return REDIS_ERR;
991 
992         /* We know for sure there is a whole line since newline != NULL,
993          * so go ahead and find out the multi bulk length. */
994         redisAssertWithInfo(c,NULL,c->querybuf[0] == '*');
995         ok = string2ll(c->querybuf+1,newline-(c->querybuf+1),&ll);
996         if (!ok || ll > 1024*1024) {
997             addReplyError(c,"Protocol error: invalid multibulk length");
998             setProtocolError(c,pos);
999             return REDIS_ERR;
1000         }
1001 
1002         pos = (newline-c->querybuf)+2;
1003         if (ll <= 0) {
1004             sdsrange(c->querybuf,pos,-1);
1005             return REDIS_OK;
1006         }
1007 
1008         c->multibulklen = ll;
1009 
1010         /* Setup argv array on client structure */
1011         if (c->argv) zfree(c->argv);
1012         c->argv = zmalloc(sizeof(robj*)*c->multibulklen);
1013     }
1014 
1015     redisAssertWithInfo(c,NULL,c->multibulklen > 0);
1016     while(c->multibulklen) {
1017         /* Read bulk length if unknown */
1018         if (c->bulklen == -1) {
1019             newline = strchr(c->querybuf+pos,'\r');
1020             if (newline == NULL) {
1021                 if (sdslen(c->querybuf) > REDIS_INLINE_MAX_SIZE) {
1022                     addReplyError(c,
1023                         "Protocol error: too big bulk count string");
1024                     setProtocolError(c,0);
1025                     return REDIS_ERR;
1026                 }
1027                 break;
1028             }
1029 
1030             /* Buffer should also contain \n */
1031             if (newline-(c->querybuf) > ((signed)sdslen(c->querybuf)-2))
1032                 break;
1033 
1034             if (c->querybuf[pos] != '$') {
1035                 addReplyErrorFormat(c,
1036                     "Protocol error: expected '$', got '%c'",
1037                     c->querybuf[pos]);
1038                 setProtocolError(c,pos);
1039                 return REDIS_ERR;
1040             }
1041 
1042             ok = string2ll(c->querybuf+pos+1,newline-(c->querybuf+pos+1),&ll);
1043             if (!ok || ll < 0 || ll > 512*1024*1024) {
1044                 addReplyError(c,"Protocol error: invalid bulk length");
1045                 setProtocolError(c,pos);
1046                 return REDIS_ERR;
1047             }
1048 
1049             pos += newline-(c->querybuf+pos)+2;
1050             if (ll >= REDIS_MBULK_BIG_ARG) {
1051                 size_t qblen;
1052 
1053                 /* If we are going to read a large object from network
1054                  * try to make it likely that it will start at c->querybuf
1055                  * boundary so that we can optimize object creation
1056                  * avoiding a large copy of data. */
1057                 sdsrange(c->querybuf,pos,-1);
1058                 pos = 0;
1059                 qblen = sdslen(c->querybuf);
1060                 /* Hint the sds library about the amount of bytes this string is
1061                  * going to contain. */
1062                 if (qblen < (size_t)ll+2)
1063                     c->querybuf = sdsMakeRoomFor(c->querybuf,ll+2-qblen);
1064             }
1065             c->bulklen = ll;
1066         }
1067 
1068         /* Read bulk argument */
1069         if (sdslen(c->querybuf)-pos < (unsigned)(c->bulklen+2)) {
1070             /* Not enough data (+2 == trailing \r\n) */
1071             break;
1072         } else {
1073             /* Optimization: if the buffer contains JUST our bulk element
1074              * instead of creating a new object by *copying* the sds we
1075              * just use the current sds string. */
1076             if (pos == 0 &&
1077                 c->bulklen >= REDIS_MBULK_BIG_ARG &&
1078                 (signed) sdslen(c->querybuf) == c->bulklen+2)
1079             {
1080                 c->argv[c->argc++] = createObject(REDIS_STRING,c->querybuf);
1081                 sdsIncrLen(c->querybuf,-2); /* remove CRLF */
1082                 c->querybuf = sdsempty();
1083                 /* Assume that if we saw a fat argument we'll see another one
1084                  * likely... */
1085                 c->querybuf = sdsMakeRoomFor(c->querybuf,c->bulklen+2);
1086                 pos = 0;
1087             } else {
1088                 c->argv[c->argc++] =
1089                     createStringObject(c->querybuf+pos,c->bulklen);
1090                 pos += c->bulklen+2;
1091             }
1092             c->bulklen = -1;
1093             c->multibulklen--;
1094         }
1095     }
1096 
1097     /* Trim to pos */
1098     if (pos) sdsrange(c->querybuf,pos,-1);
1099 
1100     /* We're done when c->multibulk == 0 */
1101     if (c->multibulklen == 0) return REDIS_OK;
1102 
1103     /* Still not read to process the command */
1104     return REDIS_ERR;
1105 }
1106 
1107 void processInputBuffer(redisClient *c) {
1108     /* Keep processing while there is something in the input buffer */
1109     while(sdslen(c->querybuf)) {
1110         /* Return if clients are paused. */
1111         if (!(c->flags & REDIS_SLAVE) && clientsArePaused()) return;
1112 
1113         /* Immediately abort if the client is in the middle of something. */
1114         if (c->flags & REDIS_BLOCKED) return;
1115 
1116         /* REDIS_CLOSE_AFTER_REPLY closes the connection once the reply is
1117          * written to the client. Make sure to not let the reply grow after
1118          * this flag has been set (i.e. don't process more commands). */
1119         if (c->flags & REDIS_CLOSE_AFTER_REPLY) return;
1120 
1121         /* Determine request type when unknown. */
1122         if (!c->reqtype) {
1123             if (c->querybuf[0] == '*') {
1124                 c->reqtype = REDIS_REQ_MULTIBULK;
1125             } else {
1126                 c->reqtype = REDIS_REQ_INLINE;
1127             }
1128         }
1129 
1130         if (c->reqtype == REDIS_REQ_INLINE) {
1131             if (processInlineBuffer(c) != REDIS_OK) break;
1132         } else if (c->reqtype == REDIS_REQ_MULTIBULK) {
1133             if (processMultibulkBuffer(c) != REDIS_OK) break;
1134         } else {
1135             redisPanic("Unknown request type");
1136         }
1137 
1138         /* Multibulk processing could see a <= 0 length. */
1139         if (c->argc == 0) {
1140             resetClient(c);
1141         } else {
1142             /* Only reset the client when the command was executed. */
1143             if (processCommand(c) == REDIS_OK)
1144                 resetClient(c);
1145         }
1146     }
1147 }
1148 
1149 void readQueryFromClient(aeEventLoop *el, int fd, void *privdata, int mask) {
1150     redisClient *c = (redisClient*) privdata;
1151     int nread, readlen;
1152     size_t qblen;
1153     REDIS_NOTUSED(el);
1154     REDIS_NOTUSED(mask);
1155 
1156     server.current_client = c;
1157     readlen = REDIS_IOBUF_LEN;
1158     /* If this is a multi bulk request, and we are processing a bulk reply
1159      * that is large enough, try to maximize the probability that the query
1160      * buffer contains exactly the SDS string representing the object, even
1161      * at the risk of requiring more read(2) calls. This way the function
1162      * processMultiBulkBuffer() can avoid copying buffers to create the
1163      * Redis Object representing the argument. */
1164     if (c->reqtype == REDIS_REQ_MULTIBULK && c->multibulklen && c->bulklen != -1
1165         && c->bulklen >= REDIS_MBULK_BIG_ARG)
1166     {
1167         int remaining = (unsigned)(c->bulklen+2)-sdslen(c->querybuf);
1168 
1169         if (remaining < readlen) readlen = remaining;
1170     }
1171 
1172     qblen = sdslen(c->querybuf);
1173     if (c->querybuf_peak < qblen) c->querybuf_peak = qblen;
1174     c->querybuf = sdsMakeRoomFor(c->querybuf, readlen);
1175     nread = read(fd, c->querybuf+qblen, readlen);
1176     if (nread == -1) {
1177         if (errno == EAGAIN) {
1178             nread = 0;
1179         } else {
1180             redisLog(REDIS_VERBOSE, "Reading from client: %s",strerror(errno));
1181             freeClient(c);
1182             return;
1183         }
1184     } else if (nread == 0) {
1185         redisLog(REDIS_VERBOSE, "Client closed connection");
1186         freeClient(c);
1187         return;
1188     }
1189     if (nread) {
1190         sdsIncrLen(c->querybuf,nread);
1191         c->lastinteraction = server.unixtime;
1192         if (c->flags & REDIS_MASTER) c->reploff += nread;
1193         server.stat_net_input_bytes += nread;
1194     } else {
1195         server.current_client = NULL;
1196         return;
1197     }
1198     if (sdslen(c->querybuf) > server.client_max_querybuf_len) {
1199         sds ci = catClientInfoString(sdsempty(),c), bytes = sdsempty();
1200 
1201         bytes = sdscatrepr(bytes,c->querybuf,64);
1202         redisLog(REDIS_WARNING,"Closing client that reached max query buffer length: %s (qbuf initial bytes: %s)", ci, bytes);
1203         sdsfree(ci);
1204         sdsfree(bytes);
1205         freeClient(c);
1206         return;
1207     }
1208     processInputBuffer(c);
1209     server.current_client = NULL;
1210 }
1211 
1212 void getClientsMaxBuffers(unsigned long *longest_output_list,
1213                           unsigned long *biggest_input_buffer) {
1214     redisClient *c;
1215     listNode *ln;
1216     listIter li;
1217     unsigned long lol = 0, bib = 0;
1218 
1219     listRewind(server.clients,&li);
1220     while ((ln = listNext(&li)) != NULL) {
1221         c = listNodeValue(ln);
1222 
1223         if (listLength(c->reply) > lol) lol = listLength(c->reply);
1224         if (sdslen(c->querybuf) > bib) bib = sdslen(c->querybuf);
1225     }
1226     *longest_output_list = lol;
1227     *biggest_input_buffer = bib;
1228 }
1229 
1230 /* A Redis "Peer ID" is a colon separated ip:port pair.
1231  * For IPv4 it's in the form x.y.z.k:port, example: "127.0.0.1:1234".
1232  * For IPv6 addresses we use [] around the IP part, like in "[::1]:1234".
1233  * For Unix sockets we use path:0, like in "/tmp/redis:0".
1234  *
1235  * A Peer ID always fits inside a buffer of REDIS_PEER_ID_LEN bytes, including
1236  * the null term.
1237  *
1238  * On failure the function still populates 'peerid' with the "?:0" string
1239  * in case you want to relax error checking or need to display something
1240  * anyway (see anetPeerToString implementation for more info). */
1241 void genClientPeerId(redisClient *client, char *peerid,
1242                             size_t peerid_len) {
1243     if (client->flags & REDIS_UNIX_SOCKET) {
1244         /* Unix socket client. */
1245         snprintf(peerid,peerid_len,"%s:0",server.unixsocket);
1246     } else {
1247         /* TCP client. */
1248         anetFormatPeer(client->fd,peerid,peerid_len);
1249     }
1250 }
1251 
1252 /* This function returns the client peer id, by creating and caching it
1253  * if client->peerid is NULL, otherwise returning the cached value.
1254  * The Peer ID never changes during the life of the client, however it
1255  * is expensive to compute. */
1256 char *getClientPeerId(redisClient *c) {
1257     char peerid[REDIS_PEER_ID_LEN];
1258 
1259     if (c->peerid == NULL) {
1260         genClientPeerId(c,peerid,sizeof(peerid));
1261         c->peerid = sdsnew(peerid);
1262     }
1263     return c->peerid;
1264 }
1265 
1266 /* Concatenate a string representing the state of a client in an human
1267  * readable format, into the sds string 's'. */
1268 sds catClientInfoString(sds s, redisClient *client) {
1269     char flags[16], events[3], *p;
1270     int emask;
1271 
1272     p = flags;
1273     if (client->flags & REDIS_SLAVE) {
1274         if (client->flags & REDIS_MONITOR)
1275             *p++ = 'O';
1276         else
1277             *p++ = 'S';
1278     }
1279     if (client->flags & REDIS_MASTER) *p++ = 'M';
1280     if (client->flags & REDIS_MULTI) *p++ = 'x';
1281     if (client->flags & REDIS_BLOCKED) *p++ = 'b';
1282     if (client->flags & REDIS_DIRTY_CAS) *p++ = 'd';
1283     if (client->flags & REDIS_CLOSE_AFTER_REPLY) *p++ = 'c';
1284     if (client->flags & REDIS_UNBLOCKED) *p++ = 'u';
1285     if (client->flags & REDIS_CLOSE_ASAP) *p++ = 'A';
1286     if (client->flags & REDIS_UNIX_SOCKET) *p++ = 'U';
1287     if (client->flags & REDIS_READONLY) *p++ = 'r';
1288     if (p == flags) *p++ = 'N';
1289     *p++ = '\0';
1290 
1291     emask = client->fd == -1 ? 0 : aeGetFileEvents(server.el,client->fd);
1292     p = events;
1293     if (emask & AE_READABLE) *p++ = 'r';
1294     if (emask & AE_WRITABLE) *p++ = 'w';
1295     *p = '\0';
1296     return sdscatfmt(s,
1297         "id=%U addr=%s fd=%i name=%s age=%I idle=%I flags=%s db=%i sub=%i psub=%i multi=%i qbuf=%U qbuf-free=%U obl=%U oll=%U omem=%U events=%s cmd=%s",
1298         (unsigned long long) client->id,
1299         getClientPeerId(client),
1300         client->fd,
1301         client->name ? (char*)client->name->ptr : "",
1302         (long long)(server.unixtime - client->ctime),
1303         (long long)(server.unixtime - client->lastinteraction),
1304         flags,
1305         client->db->id,
1306         (int) dictSize(client->pubsub_channels),
1307         (int) listLength(client->pubsub_patterns),
1308         (client->flags & REDIS_MULTI) ? client->mstate.count : -1,
1309         (unsigned long long) sdslen(client->querybuf),
1310         (unsigned long long) sdsavail(client->querybuf),
1311         (unsigned long long) client->bufpos,
1312         (unsigned long long) listLength(client->reply),
1313         (unsigned long long) getClientOutputBufferMemoryUsage(client),
1314         events,
1315         client->lastcmd ? client->lastcmd->name : "NULL");
1316 }
1317 
1318 sds getAllClientsInfoString(void) {
1319     listNode *ln;
1320     listIter li;
1321     redisClient *client;
1322     sds o = sdsempty();
1323 
1324     o = sdsMakeRoomFor(o,200*listLength(server.clients));
1325     listRewind(server.clients,&li);
1326     while ((ln = listNext(&li)) != NULL) {
1327         client = listNodeValue(ln);
1328         o = catClientInfoString(o,client);
1329         o = sdscatlen(o,"\n",1);
1330     }
1331     return o;
1332 }
1333 
1334 void clientCommand(redisClient *c) {
1335     listNode *ln;
1336     listIter li;
1337     redisClient *client;
1338 
1339     if (!strcasecmp(c->argv[1]->ptr,"list") && c->argc == 2) {
1340         /* CLIENT LIST */
1341         sds o = getAllClientsInfoString();
1342         addReplyBulkCBuffer(c,o,sdslen(o));
1343         sdsfree(o);
1344     } else if (!strcasecmp(c->argv[1]->ptr,"kill")) {
1345         /* CLIENT KILL <ip:port>
1346          * CLIENT KILL <option> [value] ... <option> [value] */
1347         char *addr = NULL;
1348         int type = -1;
1349         uint64_t id = 0;
1350         int skipme = 1;
1351         int killed = 0, close_this_client = 0;
1352 
1353         if (c->argc == 3) {
1354             /* Old style syntax: CLIENT KILL <addr> */
1355             addr = c->argv[2]->ptr;
1356             skipme = 0; /* With the old form, you can kill yourself. */
1357         } else if (c->argc > 3) {
1358             int i = 2; /* Next option index. */
1359 
1360             /* New style syntax: parse options. */
1361             while(i < c->argc) {
1362                 int moreargs = c->argc > i+1;
1363 
1364                 if (!strcasecmp(c->argv[i]->ptr,"id") && moreargs) {
1365                     long long tmp;
1366 
1367                     if (getLongLongFromObjectOrReply(c,c->argv[i+1],&tmp,NULL)
1368                         != REDIS_OK) return;
1369                     id = tmp;
1370                 } else if (!strcasecmp(c->argv[i]->ptr,"type") && moreargs) {
1371                     type = getClientTypeByName(c->argv[i+1]->ptr);
1372                     if (type == -1) {
1373                         addReplyErrorFormat(c,"Unknown client type '%s'",
1374                             (char*) c->argv[i+1]->ptr);
1375                         return;
1376                     }
1377                 } else if (!strcasecmp(c->argv[i]->ptr,"addr") && moreargs) {
1378                     addr = c->argv[i+1]->ptr;
1379                 } else if (!strcasecmp(c->argv[i]->ptr,"skipme") && moreargs) {
1380                     if (!strcasecmp(c->argv[i+1]->ptr,"yes")) {
1381                         skipme = 1;
1382                     } else if (!strcasecmp(c->argv[i+1]->ptr,"no")) {
1383                         skipme = 0;
1384                     } else {
1385                         addReply(c,shared.syntaxerr);
1386                         return;
1387                     }
1388                 } else {
1389                     addReply(c,shared.syntaxerr);
1390                     return;
1391                 }
1392                 i += 2;
1393             }
1394         } else {
1395             addReply(c,shared.syntaxerr);
1396             return;
1397         }
1398 
1399         /* Iterate clients killing all the matching clients. */
1400         listRewind(server.clients,&li);
1401         while ((ln = listNext(&li)) != NULL) {
1402             client = listNodeValue(ln);
1403             if (addr && strcmp(getClientPeerId(client),addr) != 0) continue;
1404             if (type != -1 &&
1405                 (client->flags & REDIS_MASTER ||
1406                  getClientType(client) != type)) continue;
1407             if (id != 0 && client->id != id) continue;
1408             if (c == client && skipme) continue;
1409 
1410             /* Kill it. */
1411             if (c == client) {
1412                 close_this_client = 1;
1413             } else {
1414                 freeClient(client);
1415             }
1416             killed++;
1417         }
1418 
1419         /* Reply according to old/new format. */
1420         if (c->argc == 3) {
1421             if (killed == 0)
1422                 addReplyError(c,"No such client");
1423             else
1424                 addReply(c,shared.ok);
1425         } else {
1426             addReplyLongLong(c,killed);
1427         }
1428 
1429         /* If this client has to be closed, flag it as CLOSE_AFTER_REPLY
1430          * only after we queued the reply to its output buffers. */
1431         if (close_this_client) c->flags |= REDIS_CLOSE_AFTER_REPLY;
1432     } else if (!strcasecmp(c->argv[1]->ptr,"setname") && c->argc == 3) {
1433         int j, len = sdslen(c->argv[2]->ptr);
1434         char *p = c->argv[2]->ptr;
1435 
1436         /* Setting the client name to an empty string actually removes
1437          * the current name. */
1438         if (len == 0) {
1439             if (c->name) decrRefCount(c->name);
1440             c->name = NULL;
1441             addReply(c,shared.ok);
1442             return;
1443         }
1444 
1445         /* Otherwise check if the charset is ok. We need to do this otherwise
1446          * CLIENT LIST format will break. You should always be able to
1447          * split by space to get the different fields. */
1448         for (j = 0; j < len; j++) {
1449             if (p[j] < '!' || p[j] > '~') { /* ASCII is assumed. */
1450                 addReplyError(c,
1451                     "Client names cannot contain spaces, "
1452                     "newlines or special characters.");
1453                 return;
1454             }
1455         }
1456         if (c->name) decrRefCount(c->name);
1457         c->name = c->argv[2];
1458         incrRefCount(c->name);
1459         addReply(c,shared.ok);
1460     } else if (!strcasecmp(c->argv[1]->ptr,"getname") && c->argc == 2) {
1461         if (c->name)
1462             addReplyBulk(c,c->name);
1463         else
1464             addReply(c,shared.nullbulk);
1465     } else if (!strcasecmp(c->argv[1]->ptr,"pause") && c->argc == 3) {
1466         long long duration;
1467 
1468         if (getTimeoutFromObjectOrReply(c,c->argv[2],&duration,UNIT_MILLISECONDS)
1469                                         != REDIS_OK) return;
1470         pauseClients(duration);
1471         addReply(c,shared.ok);
1472     } else {
1473         addReplyError(c, "Syntax error, try CLIENT (LIST | KILL ip:port | GETNAME | SETNAME connection-name)");
1474     }
1475 }
1476 
1477 /* Rewrite the command vector of the client. All the new objects ref count
1478  * is incremented. The old command vector is freed, and the old objects
1479  * ref count is decremented. */
1480 void rewriteClientCommandVector(redisClient *c, int argc, ...) {
1481     va_list ap;
1482     int j;
1483     robj **argv; /* The new argument vector */
1484 
1485     argv = zmalloc(sizeof(robj*)*argc);
1486     va_start(ap,argc);
1487     for (j = 0; j < argc; j++) {
1488         robj *a;
1489 
1490         a = va_arg(ap, robj*);
1491         argv[j] = a;
1492         incrRefCount(a);
1493     }
1494     /* We free the objects in the original vector at the end, so we are
1495      * sure that if the same objects are reused in the new vector the
1496      * refcount gets incremented before it gets decremented. */
1497     for (j = 0; j < c->argc; j++) decrRefCount(c->argv[j]);
1498     zfree(c->argv);
1499     /* Replace argv and argc with our new versions. */
1500     c->argv = argv;
1501     c->argc = argc;
1502     c->cmd = lookupCommandOrOriginal(c->argv[0]->ptr);
1503     redisAssertWithInfo(c,NULL,c->cmd != NULL);
1504     va_end(ap);
1505 }
1506 
1507 /* Rewrite a single item in the command vector.
1508  * The new val ref count is incremented, and the old decremented. */
1509 void rewriteClientCommandArgument(redisClient *c, int i, robj *newval) {
1510     robj *oldval;
1511 
1512     redisAssertWithInfo(c,NULL,i < c->argc);
1513     oldval = c->argv[i];
1514     c->argv[i] = newval;
1515     incrRefCount(newval);
1516     decrRefCount(oldval);
1517 
1518     /* If this is the command name make sure to fix c->cmd. */
1519     if (i == 0) {
1520         c->cmd = lookupCommandOrOriginal(c->argv[0]->ptr);
1521         redisAssertWithInfo(c,NULL,c->cmd != NULL);
1522     }
1523 }
1524 
1525 /* This function returns the number of bytes that Redis is virtually
1526  * using to store the reply still not read by the client.
1527  * It is "virtual" since the reply output list may contain objects that
1528  * are shared and are not really using additional memory.
1529  *
1530  * The function returns the total sum of the length of all the objects
1531  * stored in the output list, plus the memory used to allocate every
1532  * list node. The static reply buffer is not taken into account since it
1533  * is allocated anyway.
1534  *
1535  * Note: this function is very fast so can be called as many time as
1536  * the caller wishes. The main usage of this function currently is
1537  * enforcing the client output length limits. */
1538 unsigned long getClientOutputBufferMemoryUsage(redisClient *c) {
1539     unsigned long list_item_size = sizeof(listNode)+sizeof(robj);
1540 
1541     return c->reply_bytes + (list_item_size*listLength(c->reply));
1542 }
1543 
1544 /* Get the class of a client, used in order to enforce limits to different
1545  * classes of clients.
1546  *
1547  * The function will return one of the following:
1548  * REDIS_CLIENT_TYPE_NORMAL -> Normal client
1549  * REDIS_CLIENT_TYPE_SLAVE  -> Slave or client executing MONITOR command
1550  * REDIS_CLIENT_TYPE_PUBSUB -> Client subscribed to Pub/Sub channels
1551  */
1552 int getClientType(redisClient *c) {
1553     if ((c->flags & REDIS_SLAVE) && !(c->flags & REDIS_MONITOR))
1554         return REDIS_CLIENT_TYPE_SLAVE;
1555     if (c->flags & REDIS_PUBSUB)
1556         return REDIS_CLIENT_TYPE_PUBSUB;
1557     return REDIS_CLIENT_TYPE_NORMAL;
1558 }
1559 
1560 int getClientTypeByName(char *name) {
1561     if (!strcasecmp(name,"normal")) return REDIS_CLIENT_TYPE_NORMAL;
1562     else if (!strcasecmp(name,"slave")) return REDIS_CLIENT_TYPE_SLAVE;
1563     else if (!strcasecmp(name,"pubsub")) return REDIS_CLIENT_TYPE_PUBSUB;
1564     else return -1;
1565 }
1566 
1567 char *getClientTypeName(int class) {
1568     switch(class) {
1569     case REDIS_CLIENT_TYPE_NORMAL: return "normal";
1570     case REDIS_CLIENT_TYPE_SLAVE:  return "slave";
1571     case REDIS_CLIENT_TYPE_PUBSUB: return "pubsub";
1572     default:                       return NULL;
1573     }
1574 }
1575 
1576 /* The function checks if the client reached output buffer soft or hard
1577  * limit, and also update the state needed to check the soft limit as
1578  * a side effect.
1579  *
1580  * Return value: non-zero if the client reached the soft or the hard limit.
1581  *               Otherwise zero is returned. */
1582 int checkClientOutputBufferLimits(redisClient *c) {
1583     int soft = 0, hard = 0, class;
1584     unsigned long used_mem = getClientOutputBufferMemoryUsage(c);
1585 
1586     class = getClientType(c);
1587     if (server.client_obuf_limits[class].hard_limit_bytes &&
1588         used_mem >= server.client_obuf_limits[class].hard_limit_bytes)
1589         hard = 1;
1590     if (server.client_obuf_limits[class].soft_limit_bytes &&
1591         used_mem >= server.client_obuf_limits[class].soft_limit_bytes)
1592         soft = 1;
1593 
1594     /* We need to check if the soft limit is reached continuously for the
1595      * specified amount of seconds. */
1596     if (soft) {
1597         if (c->obuf_soft_limit_reached_time == 0) {
1598             c->obuf_soft_limit_reached_time = server.unixtime;
1599             soft = 0; /* First time we see the soft limit reached */
1600         } else {
1601             time_t elapsed = server.unixtime - c->obuf_soft_limit_reached_time;
1602 
1603             if (elapsed <=
1604                 server.client_obuf_limits[class].soft_limit_seconds) {
1605                 soft = 0; /* The client still did not reached the max number of
1606                              seconds for the soft limit to be considered
1607                              reached. */
1608             }
1609         }
1610     } else {
1611         c->obuf_soft_limit_reached_time = 0;
1612     }
1613     return soft || hard;
1614 }
1615 
1616 /* Asynchronously close a client if soft or hard limit is reached on the
1617  * output buffer size. The caller can check if the client will be closed
1618  * checking if the client REDIS_CLOSE_ASAP flag is set.
1619  *
1620  * Note: we need to close the client asynchronously because this function is
1621  * called from contexts where the client can't be freed safely, i.e. from the
1622  * lower level functions pushing data inside the client output buffers. */
1623 void asyncCloseClientOnOutputBufferLimitReached(redisClient *c) {
1624     redisAssert(c->reply_bytes < ULONG_MAX-(1024*64));
1625     if (c->reply_bytes == 0 || c->flags & REDIS_CLOSE_ASAP) return;
1626     if (checkClientOutputBufferLimits(c)) {
1627         sds client = catClientInfoString(sdsempty(),c);
1628 
1629         freeClientAsync(c);
1630         redisLog(REDIS_WARNING,"Client %s scheduled to be closed ASAP for overcoming of output buffer limits.", client);
1631         sdsfree(client);
1632     }
1633 }
1634 
1635 /* Helper function used by freeMemoryIfNeeded() in order to flush slaves
1636  * output buffers without returning control to the event loop. */
1637 void flushSlavesOutputBuffers(void) {
1638     listIter li;
1639     listNode *ln;
1640 
1641     listRewind(server.slaves,&li);
1642     while((ln = listNext(&li))) {
1643         redisClient *slave = listNodeValue(ln);
1644         int events;
1645 
1646         events = aeGetFileEvents(server.el,slave->fd);
1647         if (events & AE_WRITABLE &&
1648             slave->replstate == REDIS_REPL_ONLINE &&
1649             listLength(slave->reply))
1650         {
1651             sendReplyToClient(server.el,slave->fd,slave,0);
1652         }
1653     }
1654 }
1655 
1656 /* Pause clients up to the specified unixtime (in ms). While clients
1657  * are paused no command is processed from clients, so the data set can't
1658  * change during that time.
1659  *
1660  * However while this function pauses normal and Pub/Sub clients, slaves are
1661  * still served, so this function can be used on server upgrades where it is
1662  * required that slaves process the latest bytes from the replication stream
1663  * before being turned to masters.
1664  *
1665  * This function is also internally used by Redis Cluster for the manual
1666  * failover procedure implemented by CLUSTER FAILOVER.
1667  *
1668  * The function always succeed, even if there is already a pause in progress.
1669  * In such a case, the pause is extended if the duration is more than the
1670  * time left for the previous duration. However if the duration is smaller
1671  * than the time left for the previous pause, no change is made to the
1672  * left duration. */
1673 void pauseClients(mstime_t end) {
1674     if (!server.clients_paused || end > server.clients_pause_end_time)
1675         server.clients_pause_end_time = end;
1676     server.clients_paused = 1;
1677 }
1678 
1679 /* Return non-zero if clients are currently paused. As a side effect the
1680  * function checks if the pause time was reached and clear it. */
1681 int clientsArePaused(void) {
1682     if (server.clients_paused && server.clients_pause_end_time < server.mstime) {
1683         listNode *ln;
1684         listIter li;
1685         redisClient *c;
1686 
1687         server.clients_paused = 0;
1688 
1689         /* Put all the clients in the unblocked clients queue in order to
1690          * force the re-processing of the input buffer if any. */
1691         listRewind(server.clients,&li);
1692         while ((ln = listNext(&li)) != NULL) {
1693             c = listNodeValue(ln);
1694 
1695             if (c->flags & REDIS_SLAVE) continue;
1696             listAddNodeTail(server.unblocked_clients,c);
1697         }
1698     }
1699     return server.clients_paused;
1700 }
1701 
1702 /* This function is called by Redis in order to process a few events from
1703  * time to time while blocked into some not interruptible operation.
1704  * This allows to reply to clients with the -LOADING error while loading the
1705  * data set at startup or after a full resynchronization with the master
1706  * and so forth.
1707  *
1708  * It calls the event loop in order to process a few events. Specifically we
1709  * try to call the event loop for times as long as we receive acknowledge that
1710  * some event was processed, in order to go forward with the accept, read,
1711  * write, close sequence needed to serve a client.
1712  *
1713  * The function returns the total number of events processed. */
1714 int processEventsWhileBlocked(void) {
1715     int iterations = 4; /* See the function top-comment. */
1716     int count = 0;
1717     while (iterations--) {
1718         int events = aeProcessEvents(server.el, AE_FILE_EVENTS|AE_DONT_WAIT);
1719         if (!events) break;
1720         count += events;
1721     }
1722     return count;
1723 }
1724