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