xref: /redis-3.2.3/src/networking.c (revision 897adc1c)
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,
1004                         "Protocol error: too big bulk count string");
1005                     setProtocolError(c,0);
1006                     return REDIS_ERR;
1007                 }
1008                 break;
1009             }
1010 
1011             /* Buffer should also contain \n */
1012             if (newline-(c->querybuf) > ((signed)sdslen(c->querybuf)-2))
1013                 break;
1014 
1015             if (c->querybuf[pos] != '$') {
1016                 addReplyErrorFormat(c,
1017                     "Protocol error: expected '$', got '%c'",
1018                     c->querybuf[pos]);
1019                 setProtocolError(c,pos);
1020                 return REDIS_ERR;
1021             }
1022 
1023             ok = string2ll(c->querybuf+pos+1,newline-(c->querybuf+pos+1),&ll);
1024             if (!ok || ll < 0 || ll > 512*1024*1024) {
1025                 addReplyError(c,"Protocol error: invalid bulk length");
1026                 setProtocolError(c,pos);
1027                 return REDIS_ERR;
1028             }
1029 
1030             pos += newline-(c->querybuf+pos)+2;
1031             if (ll >= REDIS_MBULK_BIG_ARG) {
1032                 size_t qblen;
1033 
1034                 /* If we are going to read a large object from network
1035                  * try to make it likely that it will start at c->querybuf
1036                  * boundary so that we can optimize object creation
1037                  * avoiding a large copy of data. */
1038                 sdsrange(c->querybuf,pos,-1);
1039                 pos = 0;
1040                 qblen = sdslen(c->querybuf);
1041                 /* Hint the sds library about the amount of bytes this string is
1042                  * going to contain. */
1043                 if (qblen < ll+2)
1044                     c->querybuf = sdsMakeRoomFor(c->querybuf,ll+2-qblen);
1045             }
1046             c->bulklen = ll;
1047         }
1048 
1049         /* Read bulk argument */
1050         if (sdslen(c->querybuf)-pos < (unsigned)(c->bulklen+2)) {
1051             /* Not enough data (+2 == trailing \r\n) */
1052             break;
1053         } else {
1054             /* Optimization: if the buffer contains JUST our bulk element
1055              * instead of creating a new object by *copying* the sds we
1056              * just use the current sds string. */
1057             if (pos == 0 &&
1058                 c->bulklen >= REDIS_MBULK_BIG_ARG &&
1059                 (signed) sdslen(c->querybuf) == c->bulklen+2)
1060             {
1061                 c->argv[c->argc++] = createObject(REDIS_STRING,c->querybuf);
1062                 sdsIncrLen(c->querybuf,-2); /* remove CRLF */
1063                 c->querybuf = sdsempty();
1064                 /* Assume that if we saw a fat argument we'll see another one
1065                  * likely... */
1066                 c->querybuf = sdsMakeRoomFor(c->querybuf,c->bulklen+2);
1067                 pos = 0;
1068             } else {
1069                 c->argv[c->argc++] =
1070                     createStringObject(c->querybuf+pos,c->bulklen);
1071                 pos += c->bulklen+2;
1072             }
1073             c->bulklen = -1;
1074             c->multibulklen--;
1075         }
1076     }
1077 
1078     /* Trim to pos */
1079     if (pos) sdsrange(c->querybuf,pos,-1);
1080 
1081     /* We're done when c->multibulk == 0 */
1082     if (c->multibulklen == 0) return REDIS_OK;
1083 
1084     /* Still not read to process the command */
1085     return REDIS_ERR;
1086 }
1087 
1088 void processInputBuffer(redisClient *c) {
1089     /* Keep processing while there is something in the input buffer */
1090     while(sdslen(c->querybuf)) {
1091         /* Return if clients are paused. */
1092         if (!(c->flags & REDIS_SLAVE) && clientsArePaused()) return;
1093 
1094         /* Immediately abort if the client is in the middle of something. */
1095         if (c->flags & REDIS_BLOCKED) return;
1096 
1097         /* REDIS_CLOSE_AFTER_REPLY closes the connection once the reply is
1098          * written to the client. Make sure to not let the reply grow after
1099          * this flag has been set (i.e. don't process more commands). */
1100         if (c->flags & REDIS_CLOSE_AFTER_REPLY) return;
1101 
1102         /* Determine request type when unknown. */
1103         if (!c->reqtype) {
1104             if (c->querybuf[0] == '*') {
1105                 c->reqtype = REDIS_REQ_MULTIBULK;
1106             } else {
1107                 c->reqtype = REDIS_REQ_INLINE;
1108             }
1109         }
1110 
1111         if (c->reqtype == REDIS_REQ_INLINE) {
1112             if (processInlineBuffer(c) != REDIS_OK) break;
1113         } else if (c->reqtype == REDIS_REQ_MULTIBULK) {
1114             if (processMultibulkBuffer(c) != REDIS_OK) break;
1115         } else {
1116             redisPanic("Unknown request type");
1117         }
1118 
1119         /* Multibulk processing could see a <= 0 length. */
1120         if (c->argc == 0) {
1121             resetClient(c);
1122         } else {
1123             /* Only reset the client when the command was executed. */
1124             if (processCommand(c) == REDIS_OK)
1125                 resetClient(c);
1126         }
1127     }
1128 }
1129 
1130 void readQueryFromClient(aeEventLoop *el, int fd, void *privdata, int mask) {
1131     redisClient *c = (redisClient*) privdata;
1132     int nread, readlen;
1133     size_t qblen;
1134     REDIS_NOTUSED(el);
1135     REDIS_NOTUSED(mask);
1136 
1137     server.current_client = c;
1138     readlen = REDIS_IOBUF_LEN;
1139     /* If this is a multi bulk request, and we are processing a bulk reply
1140      * that is large enough, try to maximize the probability that the query
1141      * buffer contains exactly the SDS string representing the object, even
1142      * at the risk of requiring more read(2) calls. This way the function
1143      * processMultiBulkBuffer() can avoid copying buffers to create the
1144      * Redis Object representing the argument. */
1145     if (c->reqtype == REDIS_REQ_MULTIBULK && c->multibulklen && c->bulklen != -1
1146         && c->bulklen >= REDIS_MBULK_BIG_ARG)
1147     {
1148         int remaining = (unsigned)(c->bulklen+2)-sdslen(c->querybuf);
1149 
1150         if (remaining < readlen) readlen = remaining;
1151     }
1152 
1153     qblen = sdslen(c->querybuf);
1154     if (c->querybuf_peak < qblen) c->querybuf_peak = qblen;
1155     c->querybuf = sdsMakeRoomFor(c->querybuf, readlen);
1156     nread = read(fd, c->querybuf+qblen, readlen);
1157     if (nread == -1) {
1158         if (errno == EAGAIN) {
1159             nread = 0;
1160         } else {
1161             redisLog(REDIS_VERBOSE, "Reading from client: %s",strerror(errno));
1162             freeClient(c);
1163             return;
1164         }
1165     } else if (nread == 0) {
1166         redisLog(REDIS_VERBOSE, "Client closed connection");
1167         freeClient(c);
1168         return;
1169     }
1170     if (nread) {
1171         sdsIncrLen(c->querybuf,nread);
1172         c->lastinteraction = server.unixtime;
1173         if (c->flags & REDIS_MASTER) c->reploff += nread;
1174     } else {
1175         server.current_client = NULL;
1176         return;
1177     }
1178     if (sdslen(c->querybuf) > server.client_max_querybuf_len) {
1179         sds ci = getClientInfoString(c), bytes = sdsempty();
1180 
1181         bytes = sdscatrepr(bytes,c->querybuf,64);
1182         redisLog(REDIS_WARNING,"Closing client that reached max query buffer length: %s (qbuf initial bytes: %s)", ci, bytes);
1183         sdsfree(ci);
1184         sdsfree(bytes);
1185         freeClient(c);
1186         return;
1187     }
1188     processInputBuffer(c);
1189     server.current_client = NULL;
1190 }
1191 
1192 void getClientsMaxBuffers(unsigned long *longest_output_list,
1193                           unsigned long *biggest_input_buffer) {
1194     redisClient *c;
1195     listNode *ln;
1196     listIter li;
1197     unsigned long lol = 0, bib = 0;
1198 
1199     listRewind(server.clients,&li);
1200     while ((ln = listNext(&li)) != NULL) {
1201         c = listNodeValue(ln);
1202 
1203         if (listLength(c->reply) > lol) lol = listLength(c->reply);
1204         if (sdslen(c->querybuf) > bib) bib = sdslen(c->querybuf);
1205     }
1206     *longest_output_list = lol;
1207     *biggest_input_buffer = bib;
1208 }
1209 
1210 /* This is a helper function for getClientPeerId().
1211  * It writes the specified ip/port to "peerid" as a null termiated string
1212  * in the form ip:port if ip does not contain ":" itself, otherwise
1213  * [ip]:port format is used (for IPv6 addresses basically). */
1214 void formatPeerId(char *peerid, size_t peerid_len, char *ip, int port) {
1215     if (strchr(ip,':'))
1216         snprintf(peerid,peerid_len,"[%s]:%d",ip,port);
1217     else
1218         snprintf(peerid,peerid_len,"%s:%d",ip,port);
1219 }
1220 
1221 /* A Redis "Peer ID" is a colon separated ip:port pair.
1222  * For IPv4 it's in the form x.y.z.k:pork, example: "127.0.0.1:1234".
1223  * For IPv6 addresses we use [] around the IP part, like in "[::1]:1234".
1224  * For Unix socekts we use path:0, like in "/tmp/redis:0".
1225  *
1226  * A Peer ID always fits inside a buffer of REDIS_PEER_ID_LEN bytes, including
1227  * the null term.
1228  *
1229  * The function returns REDIS_OK on succcess, and REDIS_ERR on failure.
1230  *
1231  * On failure the function still populates 'peerid' with the "?:0" string
1232  * in case you want to relax error checking or need to display something
1233  * anyway (see anetPeerToString implementation for more info). */
1234 int getClientPeerId(redisClient *client, char *peerid, size_t peerid_len) {
1235     char ip[REDIS_IP_STR_LEN];
1236     int port;
1237 
1238     if (client->flags & REDIS_UNIX_SOCKET) {
1239         /* Unix socket client. */
1240         snprintf(peerid,peerid_len,"%s:0",server.unixsocket);
1241         return REDIS_OK;
1242     } else {
1243         /* TCP client. */
1244         int retval = anetPeerToString(client->fd,ip,sizeof(ip),&port);
1245         formatPeerId(peerid,peerid_len,ip,port);
1246         return (retval == -1) ? REDIS_ERR : REDIS_OK;
1247     }
1248 }
1249 
1250 /* Turn a Redis client into an sds string representing its state. */
1251 sds getClientInfoString(redisClient *client) {
1252     char peerid[REDIS_PEER_ID_LEN], flags[16], events[3], *p;
1253     int emask;
1254 
1255     getClientPeerId(client,peerid,sizeof(peerid));
1256     p = flags;
1257     if (client->flags & REDIS_SLAVE) {
1258         if (client->flags & REDIS_MONITOR)
1259             *p++ = 'O';
1260         else
1261             *p++ = 'S';
1262     }
1263     if (client->flags & REDIS_MASTER) *p++ = 'M';
1264     if (client->flags & REDIS_MULTI) *p++ = 'x';
1265     if (client->flags & REDIS_BLOCKED) *p++ = 'b';
1266     if (client->flags & REDIS_DIRTY_CAS) *p++ = 'd';
1267     if (client->flags & REDIS_CLOSE_AFTER_REPLY) *p++ = 'c';
1268     if (client->flags & REDIS_UNBLOCKED) *p++ = 'u';
1269     if (client->flags & REDIS_CLOSE_ASAP) *p++ = 'A';
1270     if (client->flags & REDIS_UNIX_SOCKET) *p++ = 'U';
1271     if (client->flags & REDIS_READONLY) *p++ = 'r';
1272     if (p == flags) *p++ = 'N';
1273     *p++ = '\0';
1274 
1275     emask = client->fd == -1 ? 0 : aeGetFileEvents(server.el,client->fd);
1276     p = events;
1277     if (emask & AE_READABLE) *p++ = 'r';
1278     if (emask & AE_WRITABLE) *p++ = 'w';
1279     *p = '\0';
1280     return sdscatprintf(sdsempty(),
1281         "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",
1282         peerid,
1283         client->fd,
1284         client->name ? (char*)client->name->ptr : "",
1285         (long)(server.unixtime - client->ctime),
1286         (long)(server.unixtime - client->lastinteraction),
1287         flags,
1288         client->db->id,
1289         (int) dictSize(client->pubsub_channels),
1290         (int) listLength(client->pubsub_patterns),
1291         (client->flags & REDIS_MULTI) ? client->mstate.count : -1,
1292         (unsigned long) sdslen(client->querybuf),
1293         (unsigned long) sdsavail(client->querybuf),
1294         (unsigned long) client->bufpos,
1295         (unsigned long) listLength(client->reply),
1296         getClientOutputBufferMemoryUsage(client),
1297         events,
1298         client->lastcmd ? client->lastcmd->name : "NULL");
1299 }
1300 
1301 sds getAllClientsInfoString(void) {
1302     listNode *ln;
1303     listIter li;
1304     redisClient *client;
1305     sds o = sdsempty();
1306 
1307     listRewind(server.clients,&li);
1308     while ((ln = listNext(&li)) != NULL) {
1309         sds cs;
1310 
1311         client = listNodeValue(ln);
1312         cs = getClientInfoString(client);
1313         o = sdscatsds(o,cs);
1314         sdsfree(cs);
1315         o = sdscatlen(o,"\n",1);
1316     }
1317     return o;
1318 }
1319 
1320 void clientCommand(redisClient *c) {
1321     listNode *ln;
1322     listIter li;
1323     redisClient *client;
1324 
1325     if (!strcasecmp(c->argv[1]->ptr,"list") && c->argc == 2) {
1326         sds o = getAllClientsInfoString();
1327         addReplyBulkCBuffer(c,o,sdslen(o));
1328         sdsfree(o);
1329     } else if (!strcasecmp(c->argv[1]->ptr,"kill") && c->argc == 3) {
1330         listRewind(server.clients,&li);
1331         while ((ln = listNext(&li)) != NULL) {
1332             char peerid[REDIS_PEER_ID_LEN];
1333 
1334             client = listNodeValue(ln);
1335             if (getClientPeerId(client,peerid,sizeof(peerid)) == REDIS_ERR)
1336                 continue;
1337             if (strcmp(peerid,c->argv[2]->ptr) == 0) {
1338                 addReply(c,shared.ok);
1339                 if (c == client) {
1340                     client->flags |= REDIS_CLOSE_AFTER_REPLY;
1341                 } else {
1342                     freeClient(client);
1343                 }
1344                 return;
1345             }
1346         }
1347         addReplyError(c,"No such client");
1348     } else if (!strcasecmp(c->argv[1]->ptr,"setname") && c->argc == 3) {
1349         int j, len = sdslen(c->argv[2]->ptr);
1350         char *p = c->argv[2]->ptr;
1351 
1352         /* Setting the client name to an empty string actually removes
1353          * the current name. */
1354         if (len == 0) {
1355             if (c->name) decrRefCount(c->name);
1356             c->name = NULL;
1357             addReply(c,shared.ok);
1358             return;
1359         }
1360 
1361         /* Otherwise check if the charset is ok. We need to do this otherwise
1362          * CLIENT LIST format will break. You should always be able to
1363          * split by space to get the different fields. */
1364         for (j = 0; j < len; j++) {
1365             if (p[j] < '!' || p[j] > '~') { /* ASCII is assumed. */
1366                 addReplyError(c,
1367                     "Client names cannot contain spaces, "
1368                     "newlines or special characters.");
1369                 return;
1370             }
1371         }
1372         if (c->name) decrRefCount(c->name);
1373         c->name = c->argv[2];
1374         incrRefCount(c->name);
1375         addReply(c,shared.ok);
1376     } else if (!strcasecmp(c->argv[1]->ptr,"getname") && c->argc == 2) {
1377         if (c->name)
1378             addReplyBulk(c,c->name);
1379         else
1380             addReply(c,shared.nullbulk);
1381     } else if (!strcasecmp(c->argv[1]->ptr,"pause") && c->argc == 3) {
1382         long long duration;
1383 
1384         if (getTimeoutFromObjectOrReply(c,c->argv[2],&duration,UNIT_MILLISECONDS)
1385                                         != REDIS_OK) return;
1386         pauseClients(duration);
1387         addReply(c,shared.ok);
1388     } else {
1389         addReplyError(c, "Syntax error, try CLIENT (LIST | KILL ip:port | GETNAME | SETNAME connection-name)");
1390     }
1391 }
1392 
1393 /* Rewrite the command vector of the client. All the new objects ref count
1394  * is incremented. The old command vector is freed, and the old objects
1395  * ref count is decremented. */
1396 void rewriteClientCommandVector(redisClient *c, int argc, ...) {
1397     va_list ap;
1398     int j;
1399     robj **argv; /* The new argument vector */
1400 
1401     argv = zmalloc(sizeof(robj*)*argc);
1402     va_start(ap,argc);
1403     for (j = 0; j < argc; j++) {
1404         robj *a;
1405 
1406         a = va_arg(ap, robj*);
1407         argv[j] = a;
1408         incrRefCount(a);
1409     }
1410     /* We free the objects in the original vector at the end, so we are
1411      * sure that if the same objects are reused in the new vector the
1412      * refcount gets incremented before it gets decremented. */
1413     for (j = 0; j < c->argc; j++) decrRefCount(c->argv[j]);
1414     zfree(c->argv);
1415     /* Replace argv and argc with our new versions. */
1416     c->argv = argv;
1417     c->argc = argc;
1418     c->cmd = lookupCommandOrOriginal(c->argv[0]->ptr);
1419     redisAssertWithInfo(c,NULL,c->cmd != NULL);
1420     va_end(ap);
1421 }
1422 
1423 /* Rewrite a single item in the command vector.
1424  * The new val ref count is incremented, and the old decremented. */
1425 void rewriteClientCommandArgument(redisClient *c, int i, robj *newval) {
1426     robj *oldval;
1427 
1428     redisAssertWithInfo(c,NULL,i < c->argc);
1429     oldval = c->argv[i];
1430     c->argv[i] = newval;
1431     incrRefCount(newval);
1432     decrRefCount(oldval);
1433 
1434     /* If this is the command name make sure to fix c->cmd. */
1435     if (i == 0) {
1436         c->cmd = lookupCommandOrOriginal(c->argv[0]->ptr);
1437         redisAssertWithInfo(c,NULL,c->cmd != NULL);
1438     }
1439 }
1440 
1441 /* This function returns the number of bytes that Redis is virtually
1442  * using to store the reply still not read by the client.
1443  * It is "virtual" since the reply output list may contain objects that
1444  * are shared and are not really using additional memory.
1445  *
1446  * The function returns the total sum of the length of all the objects
1447  * stored in the output list, plus the memory used to allocate every
1448  * list node. The static reply buffer is not taken into account since it
1449  * is allocated anyway.
1450  *
1451  * Note: this function is very fast so can be called as many time as
1452  * the caller wishes. The main usage of this function currently is
1453  * enforcing the client output length limits. */
1454 unsigned long getClientOutputBufferMemoryUsage(redisClient *c) {
1455     unsigned long list_item_size = sizeof(listNode)+sizeof(robj);
1456 
1457     return c->reply_bytes + (list_item_size*listLength(c->reply));
1458 }
1459 
1460 /* Get the class of a client, used in order to enforce limits to different
1461  * classes of clients.
1462  *
1463  * The function will return one of the following:
1464  * REDIS_CLIENT_LIMIT_CLASS_NORMAL -> Normal client
1465  * REDIS_CLIENT_LIMIT_CLASS_SLAVE  -> Slave or client executing MONITOR command
1466  * REDIS_CLIENT_LIMIT_CLASS_PUBSUB -> Client subscribed to Pub/Sub channels
1467  */
1468 int getClientLimitClass(redisClient *c) {
1469     if (c->flags & REDIS_SLAVE) return REDIS_CLIENT_LIMIT_CLASS_SLAVE;
1470     if (dictSize(c->pubsub_channels) || listLength(c->pubsub_patterns))
1471         return REDIS_CLIENT_LIMIT_CLASS_PUBSUB;
1472     return REDIS_CLIENT_LIMIT_CLASS_NORMAL;
1473 }
1474 
1475 int getClientLimitClassByName(char *name) {
1476     if (!strcasecmp(name,"normal")) return REDIS_CLIENT_LIMIT_CLASS_NORMAL;
1477     else if (!strcasecmp(name,"slave")) return REDIS_CLIENT_LIMIT_CLASS_SLAVE;
1478     else if (!strcasecmp(name,"pubsub")) return REDIS_CLIENT_LIMIT_CLASS_PUBSUB;
1479     else return -1;
1480 }
1481 
1482 char *getClientLimitClassName(int class) {
1483     switch(class) {
1484     case REDIS_CLIENT_LIMIT_CLASS_NORMAL:   return "normal";
1485     case REDIS_CLIENT_LIMIT_CLASS_SLAVE:    return "slave";
1486     case REDIS_CLIENT_LIMIT_CLASS_PUBSUB:   return "pubsub";
1487     default:                                return NULL;
1488     }
1489 }
1490 
1491 /* The function checks if the client reached output buffer soft or hard
1492  * limit, and also update the state needed to check the soft limit as
1493  * a side effect.
1494  *
1495  * Return value: non-zero if the client reached the soft or the hard limit.
1496  *               Otherwise zero is returned. */
1497 int checkClientOutputBufferLimits(redisClient *c) {
1498     int soft = 0, hard = 0, class;
1499     unsigned long used_mem = getClientOutputBufferMemoryUsage(c);
1500 
1501     class = getClientLimitClass(c);
1502     if (server.client_obuf_limits[class].hard_limit_bytes &&
1503         used_mem >= server.client_obuf_limits[class].hard_limit_bytes)
1504         hard = 1;
1505     if (server.client_obuf_limits[class].soft_limit_bytes &&
1506         used_mem >= server.client_obuf_limits[class].soft_limit_bytes)
1507         soft = 1;
1508 
1509     /* We need to check if the soft limit is reached continuously for the
1510      * specified amount of seconds. */
1511     if (soft) {
1512         if (c->obuf_soft_limit_reached_time == 0) {
1513             c->obuf_soft_limit_reached_time = server.unixtime;
1514             soft = 0; /* First time we see the soft limit reached */
1515         } else {
1516             time_t elapsed = server.unixtime - c->obuf_soft_limit_reached_time;
1517 
1518             if (elapsed <=
1519                 server.client_obuf_limits[class].soft_limit_seconds) {
1520                 soft = 0; /* The client still did not reached the max number of
1521                              seconds for the soft limit to be considered
1522                              reached. */
1523             }
1524         }
1525     } else {
1526         c->obuf_soft_limit_reached_time = 0;
1527     }
1528     return soft || hard;
1529 }
1530 
1531 /* Asynchronously close a client if soft or hard limit is reached on the
1532  * output buffer size. The caller can check if the client will be closed
1533  * checking if the client REDIS_CLOSE_ASAP flag is set.
1534  *
1535  * Note: we need to close the client asynchronously because this function is
1536  * called from contexts where the client can't be freed safely, i.e. from the
1537  * lower level functions pushing data inside the client output buffers. */
1538 void asyncCloseClientOnOutputBufferLimitReached(redisClient *c) {
1539     redisAssert(c->reply_bytes < ULONG_MAX-(1024*64));
1540     if (c->reply_bytes == 0 || c->flags & REDIS_CLOSE_ASAP) return;
1541     if (checkClientOutputBufferLimits(c)) {
1542         sds client = getClientInfoString(c);
1543 
1544         freeClientAsync(c);
1545         redisLog(REDIS_WARNING,"Client %s scheduled to be closed ASAP for overcoming of output buffer limits.", client);
1546         sdsfree(client);
1547     }
1548 }
1549 
1550 /* Helper function used by freeMemoryIfNeeded() in order to flush slaves
1551  * output buffers without returning control to the event loop. */
1552 void flushSlavesOutputBuffers(void) {
1553     listIter li;
1554     listNode *ln;
1555 
1556     listRewind(server.slaves,&li);
1557     while((ln = listNext(&li))) {
1558         redisClient *slave = listNodeValue(ln);
1559         int events;
1560 
1561         events = aeGetFileEvents(server.el,slave->fd);
1562         if (events & AE_WRITABLE &&
1563             slave->replstate == REDIS_REPL_ONLINE &&
1564             listLength(slave->reply))
1565         {
1566             sendReplyToClient(server.el,slave->fd,slave,0);
1567         }
1568     }
1569 }
1570 
1571 /* Pause clients up to the specified unixtime (in ms). While clients
1572  * are paused no command is processed from clients, so the data set can't
1573  * change during that time.
1574  *
1575  * However while this function pauses normal and Pub/Sub clients, slaves are
1576  * still served, so this function can be used on server upgrades where it is
1577  * required that slaves process the latest bytes from the replication stream
1578  * before being turned to masters.
1579  *
1580  * This function is also internally used by Redis Cluster for the manual
1581  * failover procedure implemented by CLUSTER FAILOVER.
1582  *
1583  * The function always succeed, even if there is already a pause in progress.
1584  * In such a case, the pause is extended if the duration is more than the
1585  * time left for the previous duration. However if the duration is smaller
1586  * than the time left for the previous pause, no change is made to the
1587  * left duration. */
1588 void pauseClients(mstime_t end) {
1589     if (!server.clients_paused || end > server.clients_pause_end_time)
1590         server.clients_pause_end_time = end;
1591     server.clients_paused = 1;
1592 }
1593 
1594 /* Return non-zero if clients are currently paused. As a side effect the
1595  * function checks if the pause time was reached and clear it. */
1596 int clientsArePaused(void) {
1597     if (server.clients_paused && server.clients_pause_end_time < server.mstime) {
1598         listNode *ln;
1599         listIter li;
1600         redisClient *c;
1601 
1602         server.clients_paused = 0;
1603 
1604         /* Put all the clients in the unblocked clients queue in order to
1605          * force the re-processing of the input buffer if any. */
1606         listRewind(server.clients,&li);
1607         while ((ln = listNext(&li)) != NULL) {
1608             c = listNodeValue(ln);
1609 
1610             if (c->flags & REDIS_SLAVE) continue;
1611             listAddNodeTail(server.unblocked_clients,c);
1612         }
1613     }
1614     return server.clients_paused;
1615 }
1616