]>
Commit | Line | Data |
---|---|---|
e2641e09 | 1 | #include "redis.h" |
e2641e09 | 2 | #include <sys/uio.h> |
3 | ||
11e0c4c5 | 4 | static void setProtocolError(redisClient *c, int pos); |
5 | ||
e2641e09 | 6 | void *dupClientReplyValue(void *o) { |
7 | incrRefCount((robj*)o); | |
8 | return o; | |
9 | } | |
10 | ||
11 | int listMatchObjects(void *a, void *b) { | |
12 | return equalStringObjects(a,b); | |
13 | } | |
14 | ||
15 | redisClient *createClient(int fd) { | |
f3357792 | 16 | redisClient *c = zmalloc(sizeof(redisClient)); |
834ef78e | 17 | c->bufpos = 0; |
e2641e09 | 18 | |
0f1d64ca | 19 | /* passing -1 as fd it is possible to create a non connected client. |
20 | * This is useful since all the Redis commands needs to be executed | |
21 | * in the context of a client. When commands are executed in other | |
22 | * contexts (for instance a Lua script) we need a non connected client. */ | |
23 | if (fd != -1) { | |
24 | anetNonBlock(NULL,fd); | |
25 | anetTcpNoDelay(NULL,fd); | |
26 | if (aeCreateFileEvent(server.el,fd,AE_READABLE, | |
27 | readQueryFromClient, c) == AE_ERR) | |
28 | { | |
29 | close(fd); | |
30 | zfree(c); | |
31 | return NULL; | |
32 | } | |
106bd87a PN |
33 | } |
34 | ||
e2641e09 | 35 | selectDb(c,0); |
36 | c->fd = fd; | |
37 | c->querybuf = sdsempty(); | |
cd8788f2 | 38 | c->reqtype = 0; |
e2641e09 | 39 | c->argc = 0; |
40 | c->argv = NULL; | |
2c74a9f9 | 41 | c->cmd = c->lastcmd = NULL; |
cd8788f2 | 42 | c->multibulklen = 0; |
e2641e09 | 43 | c->bulklen = -1; |
e2641e09 | 44 | c->sentlen = 0; |
45 | c->flags = 0; | |
46 | c->lastinteraction = time(NULL); | |
47 | c->authenticated = 0; | |
48 | c->replstate = REDIS_REPL_NONE; | |
49 | c->reply = listCreate(); | |
3853c168 | 50 | c->reply_bytes = 0; |
e2641e09 | 51 | listSetFreeMethod(c->reply,decrRefCount); |
52 | listSetDupMethod(c->reply,dupClientReplyValue); | |
e3c51c4b DJMM |
53 | c->bpop.keys = NULL; |
54 | c->bpop.count = 0; | |
55 | c->bpop.timeout = 0; | |
56 | c->bpop.target = NULL; | |
e2641e09 | 57 | c->io_keys = listCreate(); |
58 | c->watched_keys = listCreate(); | |
59 | listSetFreeMethod(c->io_keys,decrRefCount); | |
60 | c->pubsub_channels = dictCreate(&setDictType,NULL); | |
61 | c->pubsub_patterns = listCreate(); | |
62 | listSetFreeMethod(c->pubsub_patterns,decrRefCount); | |
63 | listSetMatchMethod(c->pubsub_patterns,listMatchObjects); | |
7b722727 | 64 | if (fd != -1) listAddNodeTail(server.clients,c); |
e2641e09 | 65 | initClientMultiState(c); |
66 | return c; | |
67 | } | |
68 | ||
a3a323e0 PN |
69 | /* Set the event loop to listen for write events on the client's socket. |
70 | * Typically gets called every time a reply is built. */ | |
4c2e506a | 71 | int _installWriteEvent(redisClient *c) { |
7156f43c | 72 | if (c->flags & REDIS_LUA_CLIENT) return REDIS_OK; |
57b07380 | 73 | if (c->fd <= 0) return REDIS_ERR; |
834ef78e | 74 | if (c->bufpos == 0 && listLength(c->reply) == 0 && |
e2641e09 | 75 | (c->replstate == REDIS_REPL_NONE || |
76 | c->replstate == REDIS_REPL_ONLINE) && | |
77 | aeCreateFileEvent(server.el, c->fd, AE_WRITABLE, | |
834ef78e PN |
78 | sendReplyToClient, c) == AE_ERR) return REDIS_ERR; |
79 | return REDIS_OK; | |
80 | } | |
81 | ||
36c19d03 PN |
82 | /* Create a duplicate of the last object in the reply list when |
83 | * it is not exclusively owned by the reply list. */ | |
84 | robj *dupLastObjectIfNeeded(list *reply) { | |
85 | robj *new, *cur; | |
86 | listNode *ln; | |
87 | redisAssert(listLength(reply) > 0); | |
88 | ln = listLast(reply); | |
89 | cur = listNodeValue(ln); | |
90 | if (cur->refcount > 1) { | |
91 | new = dupStringObject(cur); | |
92 | decrRefCount(cur); | |
93 | listNodeValue(ln) = new; | |
94 | } | |
95 | return listNodeValue(ln); | |
834ef78e PN |
96 | } |
97 | ||
25ef3192 | 98 | /* ----------------------------------------------------------------------------- |
99 | * Low level functions to add more data to output buffers. | |
100 | * -------------------------------------------------------------------------- */ | |
101 | ||
36c19d03 | 102 | int _addReplyToBuffer(redisClient *c, char *s, size_t len) { |
f3357792 | 103 | size_t available = sizeof(c->buf)-c->bufpos; |
36c19d03 | 104 | |
25ef3192 | 105 | if (c->flags & REDIS_CLOSE_AFTER_REPLY) return REDIS_OK; |
106 | ||
36c19d03 PN |
107 | /* If there already are entries in the reply list, we cannot |
108 | * add anything more to the static buffer. */ | |
109 | if (listLength(c->reply) > 0) return REDIS_ERR; | |
110 | ||
111 | /* Check that the buffer has enough space available for this string. */ | |
112 | if (len > available) return REDIS_ERR; | |
e2641e09 | 113 | |
36c19d03 PN |
114 | memcpy(c->buf+c->bufpos,s,len); |
115 | c->bufpos+=len; | |
116 | return REDIS_OK; | |
834ef78e PN |
117 | } |
118 | ||
36c19d03 PN |
119 | void _addReplyObjectToList(redisClient *c, robj *o) { |
120 | robj *tail; | |
25ef3192 | 121 | |
122 | if (c->flags & REDIS_CLOSE_AFTER_REPLY) return; | |
123 | ||
36c19d03 PN |
124 | if (listLength(c->reply) == 0) { |
125 | incrRefCount(o); | |
126 | listAddNodeTail(c->reply,o); | |
127 | } else { | |
128 | tail = listNodeValue(listLast(c->reply)); | |
129 | ||
130 | /* Append to this object when possible. */ | |
131 | if (tail->ptr != NULL && | |
132 | sdslen(tail->ptr)+sdslen(o->ptr) <= REDIS_REPLY_CHUNK_BYTES) | |
133 | { | |
134 | tail = dupLastObjectIfNeeded(c->reply); | |
135 | tail->ptr = sdscatlen(tail->ptr,o->ptr,sdslen(o->ptr)); | |
136 | } else { | |
137 | incrRefCount(o); | |
138 | listAddNodeTail(c->reply,o); | |
139 | } | |
140 | } | |
3853c168 | 141 | c->reply_bytes += sdslen(o->ptr); |
36c19d03 | 142 | } |
834ef78e | 143 | |
36c19d03 PN |
144 | /* This method takes responsibility over the sds. When it is no longer |
145 | * needed it will be free'd, otherwise it ends up in a robj. */ | |
146 | void _addReplySdsToList(redisClient *c, sds s) { | |
147 | robj *tail; | |
25ef3192 | 148 | |
5b94b8ac | 149 | if (c->flags & REDIS_CLOSE_AFTER_REPLY) { |
150 | sdsfree(s); | |
151 | return; | |
152 | } | |
25ef3192 | 153 | |
3853c168 | 154 | c->reply_bytes += sdslen(s); |
36c19d03 PN |
155 | if (listLength(c->reply) == 0) { |
156 | listAddNodeTail(c->reply,createObject(REDIS_STRING,s)); | |
157 | } else { | |
158 | tail = listNodeValue(listLast(c->reply)); | |
159 | ||
160 | /* Append to this object when possible. */ | |
161 | if (tail->ptr != NULL && | |
162 | sdslen(tail->ptr)+sdslen(s) <= REDIS_REPLY_CHUNK_BYTES) | |
163 | { | |
164 | tail = dupLastObjectIfNeeded(c->reply); | |
165 | tail->ptr = sdscatlen(tail->ptr,s,sdslen(s)); | |
166 | sdsfree(s); | |
834ef78e | 167 | } else { |
36c19d03 | 168 | listAddNodeTail(c->reply,createObject(REDIS_STRING,s)); |
834ef78e | 169 | } |
36c19d03 PN |
170 | } |
171 | } | |
172 | ||
173 | void _addReplyStringToList(redisClient *c, char *s, size_t len) { | |
174 | robj *tail; | |
25ef3192 | 175 | |
176 | if (c->flags & REDIS_CLOSE_AFTER_REPLY) return; | |
177 | ||
36c19d03 PN |
178 | if (listLength(c->reply) == 0) { |
179 | listAddNodeTail(c->reply,createStringObject(s,len)); | |
834ef78e | 180 | } else { |
36c19d03 PN |
181 | tail = listNodeValue(listLast(c->reply)); |
182 | ||
183 | /* Append to this object when possible. */ | |
184 | if (tail->ptr != NULL && | |
185 | sdslen(tail->ptr)+len <= REDIS_REPLY_CHUNK_BYTES) | |
186 | { | |
187 | tail = dupLastObjectIfNeeded(c->reply); | |
188 | tail->ptr = sdscatlen(tail->ptr,s,len); | |
834ef78e | 189 | } else { |
36c19d03 | 190 | listAddNodeTail(c->reply,createStringObject(s,len)); |
834ef78e PN |
191 | } |
192 | } | |
3853c168 | 193 | c->reply_bytes += len; |
834ef78e | 194 | } |
e2641e09 | 195 | |
25ef3192 | 196 | /* ----------------------------------------------------------------------------- |
197 | * Higher level functions to queue data on the client output buffer. | |
198 | * The following functions are the ones that commands implementations will call. | |
199 | * -------------------------------------------------------------------------- */ | |
200 | ||
834ef78e | 201 | void addReply(redisClient *c, robj *obj) { |
4c2e506a | 202 | if (_installWriteEvent(c) != REDIS_OK) return; |
4c2e506a | 203 | |
204 | /* This is an important place where we can avoid copy-on-write | |
205 | * when there is a saving child running, avoiding touching the | |
206 | * refcount field of the object if it's not needed. | |
207 | * | |
208 | * If the encoding is RAW and there is room in the static buffer | |
209 | * we'll be able to send the object to the client without | |
210 | * messing with its page. */ | |
211 | if (obj->encoding == REDIS_ENCODING_RAW) { | |
212 | if (_addReplyToBuffer(c,obj->ptr,sdslen(obj->ptr)) != REDIS_OK) | |
213 | _addReplyObjectToList(c,obj); | |
834ef78e | 214 | } else { |
d51ebef5 | 215 | /* FIXME: convert the long into string and use _addReplyToBuffer() |
216 | * instead of calling getDecodedObject. As this place in the | |
217 | * code is too performance critical. */ | |
834ef78e | 218 | obj = getDecodedObject(obj); |
4c2e506a | 219 | if (_addReplyToBuffer(c,obj->ptr,sdslen(obj->ptr)) != REDIS_OK) |
220 | _addReplyObjectToList(c,obj); | |
221 | decrRefCount(obj); | |
e2641e09 | 222 | } |
e2641e09 | 223 | } |
224 | ||
225 | void addReplySds(redisClient *c, sds s) { | |
4c2e506a | 226 | if (_installWriteEvent(c) != REDIS_OK) { |
cd76bb65 PN |
227 | /* The caller expects the sds to be free'd. */ |
228 | sdsfree(s); | |
229 | return; | |
230 | } | |
36c19d03 | 231 | if (_addReplyToBuffer(c,s,sdslen(s)) == REDIS_OK) { |
834ef78e PN |
232 | sdsfree(s); |
233 | } else { | |
36c19d03 PN |
234 | /* This method free's the sds when it is no longer needed. */ |
235 | _addReplySdsToList(c,s); | |
834ef78e | 236 | } |
e2641e09 | 237 | } |
238 | ||
834ef78e | 239 | void addReplyString(redisClient *c, char *s, size_t len) { |
4c2e506a | 240 | if (_installWriteEvent(c) != REDIS_OK) return; |
36c19d03 PN |
241 | if (_addReplyToBuffer(c,s,len) != REDIS_OK) |
242 | _addReplyStringToList(c,s,len); | |
834ef78e | 243 | } |
e2641e09 | 244 | |
3ab20376 PN |
245 | void _addReplyError(redisClient *c, char *s, size_t len) { |
246 | addReplyString(c,"-ERR ",5); | |
247 | addReplyString(c,s,len); | |
248 | addReplyString(c,"\r\n",2); | |
e2641e09 | 249 | } |
250 | ||
3ab20376 PN |
251 | void addReplyError(redisClient *c, char *err) { |
252 | _addReplyError(c,err,strlen(err)); | |
253 | } | |
e2641e09 | 254 | |
3ab20376 | 255 | void addReplyErrorFormat(redisClient *c, const char *fmt, ...) { |
3bb818df | 256 | size_t l, j; |
3ab20376 PN |
257 | va_list ap; |
258 | va_start(ap,fmt); | |
259 | sds s = sdscatvprintf(sdsempty(),fmt,ap); | |
260 | va_end(ap); | |
3bb818df | 261 | /* Make sure there are no newlines in the string, otherwise invalid protocol |
262 | * is emitted. */ | |
263 | l = sdslen(s); | |
264 | for (j = 0; j < l; j++) { | |
265 | if (s[j] == '\r' || s[j] == '\n') s[j] = ' '; | |
266 | } | |
3ab20376 PN |
267 | _addReplyError(c,s,sdslen(s)); |
268 | sdsfree(s); | |
269 | } | |
270 | ||
271 | void _addReplyStatus(redisClient *c, char *s, size_t len) { | |
272 | addReplyString(c,"+",1); | |
273 | addReplyString(c,s,len); | |
274 | addReplyString(c,"\r\n",2); | |
275 | } | |
276 | ||
277 | void addReplyStatus(redisClient *c, char *status) { | |
278 | _addReplyStatus(c,status,strlen(status)); | |
279 | } | |
280 | ||
281 | void addReplyStatusFormat(redisClient *c, const char *fmt, ...) { | |
282 | va_list ap; | |
283 | va_start(ap,fmt); | |
284 | sds s = sdscatvprintf(sdsempty(),fmt,ap); | |
285 | va_end(ap); | |
286 | _addReplyStatus(c,s,sdslen(s)); | |
287 | sdsfree(s); | |
288 | } | |
289 | ||
b301c1fc PN |
290 | /* Adds an empty object to the reply list that will contain the multi bulk |
291 | * length, which is not known when this function is called. */ | |
292 | void *addDeferredMultiBulkLength(redisClient *c) { | |
4c2e506a | 293 | /* Note that we install the write event here even if the object is not |
294 | * ready to be sent, since we are sure that before returning to the | |
295 | * event loop setDeferredMultiBulkLength() will be called. */ | |
296 | if (_installWriteEvent(c) != REDIS_OK) return NULL; | |
36c19d03 | 297 | listAddNodeTail(c->reply,createObject(REDIS_STRING,NULL)); |
b301c1fc PN |
298 | return listLast(c->reply); |
299 | } | |
300 | ||
301 | /* Populate the length object and try glueing it to the next chunk. */ | |
302 | void setDeferredMultiBulkLength(redisClient *c, void *node, long length) { | |
303 | listNode *ln = (listNode*)node; | |
304 | robj *len, *next; | |
305 | ||
306 | /* Abort when *node is NULL (see addDeferredMultiBulkLength). */ | |
307 | if (node == NULL) return; | |
308 | ||
309 | len = listNodeValue(ln); | |
310 | len->ptr = sdscatprintf(sdsempty(),"*%ld\r\n",length); | |
3853c168 | 311 | c->reply_bytes += sdslen(len->ptr); |
b301c1fc PN |
312 | if (ln->next != NULL) { |
313 | next = listNodeValue(ln->next); | |
36c19d03 | 314 | |
49128f0b | 315 | /* Only glue when the next node is non-NULL (an sds in this case) */ |
36c19d03 | 316 | if (next->ptr != NULL) { |
49128f0b | 317 | len->ptr = sdscatlen(len->ptr,next->ptr,sdslen(next->ptr)); |
b301c1fc PN |
318 | listDelNode(c->reply,ln->next); |
319 | } | |
e2641e09 | 320 | } |
b301c1fc PN |
321 | } |
322 | ||
d51ebef5 | 323 | /* Add a duble as a bulk reply */ |
834ef78e PN |
324 | void addReplyDouble(redisClient *c, double d) { |
325 | char dbuf[128], sbuf[128]; | |
326 | int dlen, slen; | |
327 | dlen = snprintf(dbuf,sizeof(dbuf),"%.17g",d); | |
328 | slen = snprintf(sbuf,sizeof(sbuf),"$%d\r\n%s\r\n",dlen,dbuf); | |
329 | addReplyString(c,sbuf,slen); | |
e2641e09 | 330 | } |
331 | ||
d51ebef5 | 332 | /* Add a long long as integer reply or bulk len / multi bulk count. |
333 | * Basically this is used to output <prefix><long long><crlf>. */ | |
834ef78e | 334 | void _addReplyLongLong(redisClient *c, long long ll, char prefix) { |
e2641e09 | 335 | char buf[128]; |
834ef78e PN |
336 | int len; |
337 | buf[0] = prefix; | |
e2641e09 | 338 | len = ll2string(buf+1,sizeof(buf)-1,ll); |
339 | buf[len+1] = '\r'; | |
340 | buf[len+2] = '\n'; | |
834ef78e | 341 | addReplyString(c,buf,len+3); |
e2641e09 | 342 | } |
343 | ||
834ef78e | 344 | void addReplyLongLong(redisClient *c, long long ll) { |
009db676 | 345 | if (ll == 0) |
346 | addReply(c,shared.czero); | |
347 | else if (ll == 1) | |
348 | addReply(c,shared.cone); | |
349 | else | |
350 | _addReplyLongLong(c,ll,':'); | |
834ef78e | 351 | } |
e2641e09 | 352 | |
0537e7bf PN |
353 | void addReplyMultiBulkLen(redisClient *c, long length) { |
354 | _addReplyLongLong(c,length,'*'); | |
e2641e09 | 355 | } |
356 | ||
d51ebef5 | 357 | /* Create the length prefix of a bulk reply, example: $2234 */ |
e2641e09 | 358 | void addReplyBulkLen(redisClient *c, robj *obj) { |
834ef78e | 359 | size_t len; |
e2641e09 | 360 | |
361 | if (obj->encoding == REDIS_ENCODING_RAW) { | |
362 | len = sdslen(obj->ptr); | |
363 | } else { | |
364 | long n = (long)obj->ptr; | |
365 | ||
366 | /* Compute how many bytes will take this integer as a radix 10 string */ | |
367 | len = 1; | |
368 | if (n < 0) { | |
369 | len++; | |
370 | n = -n; | |
371 | } | |
372 | while((n = n/10) != 0) { | |
373 | len++; | |
374 | } | |
375 | } | |
834ef78e | 376 | _addReplyLongLong(c,len,'$'); |
e2641e09 | 377 | } |
378 | ||
d51ebef5 | 379 | /* Add a Redis Object as a bulk reply */ |
e2641e09 | 380 | void addReplyBulk(redisClient *c, robj *obj) { |
381 | addReplyBulkLen(c,obj); | |
382 | addReply(c,obj); | |
383 | addReply(c,shared.crlf); | |
384 | } | |
385 | ||
d51ebef5 | 386 | /* Add a C buffer as bulk reply */ |
387 | void addReplyBulkCBuffer(redisClient *c, void *p, size_t len) { | |
388 | _addReplyLongLong(c,len,'$'); | |
389 | addReplyString(c,p,len); | |
390 | addReply(c,shared.crlf); | |
391 | } | |
392 | ||
393 | /* Add a C nul term string as bulk reply */ | |
e2641e09 | 394 | void addReplyBulkCString(redisClient *c, char *s) { |
395 | if (s == NULL) { | |
396 | addReply(c,shared.nullbulk); | |
397 | } else { | |
d51ebef5 | 398 | addReplyBulkCBuffer(c,s,strlen(s)); |
e2641e09 | 399 | } |
400 | } | |
401 | ||
d51ebef5 | 402 | /* Add a long long as a bulk reply */ |
403 | void addReplyBulkLongLong(redisClient *c, long long ll) { | |
404 | char buf[64]; | |
405 | int len; | |
406 | ||
407 | len = ll2string(buf,64,ll); | |
408 | addReplyBulkCBuffer(c,buf,len); | |
409 | } | |
410 | ||
1824e3a3 | 411 | /* Copy 'src' client output buffers into 'dst' client output buffers. |
412 | * The function takes care of freeing the old output buffers of the | |
413 | * destination client. */ | |
414 | void copyClientOutputBuffer(redisClient *dst, redisClient *src) { | |
415 | listRelease(dst->reply); | |
416 | dst->reply = listDup(src->reply); | |
417 | memcpy(dst->buf,src->buf,src->bufpos); | |
418 | dst->bufpos = src->bufpos; | |
3853c168 | 419 | dst->reply_bytes = src->reply_bytes; |
1824e3a3 | 420 | } |
421 | ||
ab17b909 | 422 | static void acceptCommonHandler(int fd) { |
e2641e09 | 423 | redisClient *c; |
ab17b909 | 424 | if ((c = createClient(fd)) == NULL) { |
e2641e09 | 425 | redisLog(REDIS_WARNING,"Error allocating resoures for the client"); |
ab17b909 | 426 | close(fd); /* May be already closed, just ingore errors */ |
e2641e09 | 427 | return; |
428 | } | |
429 | /* If maxclient directive is set and this is one client more... close the | |
430 | * connection. Note that we create the client instead to check before | |
431 | * for this condition, since now the socket is already set in nonblocking | |
432 | * mode and we can send an error for free using the Kernel I/O */ | |
58732c23 | 433 | if (listLength(server.clients) > server.maxclients) { |
e2641e09 | 434 | char *err = "-ERR max number of clients reached\r\n"; |
435 | ||
436 | /* That's a best effort error message, don't check write errors */ | |
437 | if (write(c->fd,err,strlen(err)) == -1) { | |
438 | /* Nothing to do, Just to avoid the warning... */ | |
439 | } | |
3c95e721 | 440 | server.stat_rejected_conn++; |
e2641e09 | 441 | freeClient(c); |
442 | return; | |
443 | } | |
444 | server.stat_numconnections++; | |
445 | } | |
446 | ||
ab17b909 PN |
447 | void acceptTcpHandler(aeEventLoop *el, int fd, void *privdata, int mask) { |
448 | int cport, cfd; | |
449 | char cip[128]; | |
450 | REDIS_NOTUSED(el); | |
451 | REDIS_NOTUSED(mask); | |
452 | REDIS_NOTUSED(privdata); | |
453 | ||
454 | cfd = anetTcpAccept(server.neterr, fd, cip, &cport); | |
455 | if (cfd == AE_ERR) { | |
df541bea | 456 | redisLog(REDIS_WARNING,"Accepting client connection: %s", server.neterr); |
ab17b909 PN |
457 | return; |
458 | } | |
459 | redisLog(REDIS_VERBOSE,"Accepted %s:%d", cip, cport); | |
460 | acceptCommonHandler(cfd); | |
461 | } | |
462 | ||
463 | void acceptUnixHandler(aeEventLoop *el, int fd, void *privdata, int mask) { | |
464 | int cfd; | |
ab17b909 PN |
465 | REDIS_NOTUSED(el); |
466 | REDIS_NOTUSED(mask); | |
467 | REDIS_NOTUSED(privdata); | |
468 | ||
4fe83b55 | 469 | cfd = anetUnixAccept(server.neterr, fd); |
ab17b909 | 470 | if (cfd == AE_ERR) { |
df541bea | 471 | redisLog(REDIS_WARNING,"Accepting client connection: %s", server.neterr); |
ab17b909 PN |
472 | return; |
473 | } | |
474 | redisLog(REDIS_VERBOSE,"Accepted connection to %s", server.unixsocket); | |
475 | acceptCommonHandler(cfd); | |
476 | } | |
477 | ||
478 | ||
e2641e09 | 479 | static void freeClientArgv(redisClient *c) { |
480 | int j; | |
e2641e09 | 481 | for (j = 0; j < c->argc; j++) |
482 | decrRefCount(c->argv[j]); | |
e2641e09 | 483 | c->argc = 0; |
09e2d9ee | 484 | c->cmd = NULL; |
e2641e09 | 485 | } |
486 | ||
487 | void freeClient(redisClient *c) { | |
488 | listNode *ln; | |
489 | ||
00010fa9 | 490 | /* If this is marked as current client unset it */ |
491 | if (server.current_client == c) server.current_client = NULL; | |
492 | ||
e2641e09 | 493 | /* Note that if the client we are freeing is blocked into a blocking |
494 | * call, we have to set querybuf to NULL *before* to call | |
495 | * unblockClientWaitingData() to avoid processInputBuffer() will get | |
496 | * called. Also it is important to remove the file events after | |
497 | * this, because this call adds the READABLE event. */ | |
498 | sdsfree(c->querybuf); | |
499 | c->querybuf = NULL; | |
500 | if (c->flags & REDIS_BLOCKED) | |
501 | unblockClientWaitingData(c); | |
502 | ||
503 | /* UNWATCH all the keys */ | |
504 | unwatchAllKeys(c); | |
505 | listRelease(c->watched_keys); | |
506 | /* Unsubscribe from all the pubsub channels */ | |
507 | pubsubUnsubscribeAllChannels(c,0); | |
508 | pubsubUnsubscribeAllPatterns(c,0); | |
509 | dictRelease(c->pubsub_channels); | |
510 | listRelease(c->pubsub_patterns); | |
511 | /* Obvious cleanup */ | |
512 | aeDeleteFileEvent(server.el,c->fd,AE_READABLE); | |
513 | aeDeleteFileEvent(server.el,c->fd,AE_WRITABLE); | |
514 | listRelease(c->reply); | |
515 | freeClientArgv(c); | |
516 | close(c->fd); | |
517 | /* Remove from the list of clients */ | |
518 | ln = listSearchKey(server.clients,c); | |
519 | redisAssert(ln != NULL); | |
520 | listDelNode(server.clients,ln); | |
3bcffcbe PN |
521 | /* When client was just unblocked because of a blocking operation, |
522 | * remove it from the list with unblocked clients. */ | |
523 | if (c->flags & REDIS_UNBLOCKED) { | |
524 | ln = listSearchKey(server.unblocked_clients,c); | |
525 | redisAssert(ln != NULL); | |
526 | listDelNode(server.unblocked_clients,ln); | |
527 | } | |
e2641e09 | 528 | listRelease(c->io_keys); |
778b2210 | 529 | /* Master/slave cleanup. |
530 | * Case 1: we lost the connection with a slave. */ | |
e2641e09 | 531 | if (c->flags & REDIS_SLAVE) { |
532 | if (c->replstate == REDIS_REPL_SEND_BULK && c->repldbfd != -1) | |
533 | close(c->repldbfd); | |
534 | list *l = (c->flags & REDIS_MONITOR) ? server.monitors : server.slaves; | |
535 | ln = listSearchKey(l,c); | |
536 | redisAssert(ln != NULL); | |
537 | listDelNode(l,ln); | |
538 | } | |
778b2210 | 539 | |
540 | /* Case 2: we lost the connection with the master. */ | |
e2641e09 | 541 | if (c->flags & REDIS_MASTER) { |
542 | server.master = NULL; | |
1844f990 | 543 | server.repl_state = REDIS_REPL_CONNECT; |
07486df6 | 544 | server.repl_down_since = time(NULL); |
778b2210 | 545 | /* Since we lost the connection with the master, we should also |
546 | * close the connection with all our slaves if we have any, so | |
547 | * when we'll resync with the master the other slaves will sync again | |
548 | * with us as well. Note that also when the slave is not connected | |
d37299e3 | 549 | * to the master it will keep refusing connections by other slaves. |
550 | * | |
551 | * We do this only if server.masterhost != NULL. If it is NULL this | |
552 | * means the user called SLAVEOF NO ONE and we are freeing our | |
553 | * link with the master, so no need to close link with slaves. */ | |
554 | if (server.masterhost != NULL) { | |
555 | while (listLength(server.slaves)) { | |
556 | ln = listFirst(server.slaves); | |
557 | freeClient((redisClient*)ln->value); | |
558 | } | |
778b2210 | 559 | } |
e2641e09 | 560 | } |
561 | /* Release memory */ | |
562 | zfree(c->argv); | |
e2641e09 | 563 | freeClientMultiState(c); |
564 | zfree(c); | |
565 | } | |
566 | ||
e2641e09 | 567 | void sendReplyToClient(aeEventLoop *el, int fd, void *privdata, int mask) { |
568 | redisClient *c = privdata; | |
569 | int nwritten = 0, totwritten = 0, objlen; | |
570 | robj *o; | |
571 | REDIS_NOTUSED(el); | |
572 | REDIS_NOTUSED(mask); | |
573 | ||
834ef78e PN |
574 | while(c->bufpos > 0 || listLength(c->reply)) { |
575 | if (c->bufpos > 0) { | |
576 | if (c->flags & REDIS_MASTER) { | |
577 | /* Don't reply to a master */ | |
578 | nwritten = c->bufpos - c->sentlen; | |
579 | } else { | |
580 | nwritten = write(fd,c->buf+c->sentlen,c->bufpos-c->sentlen); | |
581 | if (nwritten <= 0) break; | |
582 | } | |
583 | c->sentlen += nwritten; | |
584 | totwritten += nwritten; | |
585 | ||
586 | /* If the buffer was sent, set bufpos to zero to continue with | |
587 | * the remainder of the reply. */ | |
588 | if (c->sentlen == c->bufpos) { | |
589 | c->bufpos = 0; | |
590 | c->sentlen = 0; | |
591 | } | |
592 | } else { | |
593 | o = listNodeValue(listFirst(c->reply)); | |
594 | objlen = sdslen(o->ptr); | |
e2641e09 | 595 | |
834ef78e PN |
596 | if (objlen == 0) { |
597 | listDelNode(c->reply,listFirst(c->reply)); | |
598 | continue; | |
599 | } | |
e2641e09 | 600 | |
834ef78e PN |
601 | if (c->flags & REDIS_MASTER) { |
602 | /* Don't reply to a master */ | |
603 | nwritten = objlen - c->sentlen; | |
604 | } else { | |
605 | nwritten = write(fd, ((char*)o->ptr)+c->sentlen,objlen-c->sentlen); | |
606 | if (nwritten <= 0) break; | |
607 | } | |
608 | c->sentlen += nwritten; | |
609 | totwritten += nwritten; | |
e2641e09 | 610 | |
834ef78e PN |
611 | /* If we fully sent the object on head go to the next one */ |
612 | if (c->sentlen == objlen) { | |
613 | listDelNode(c->reply,listFirst(c->reply)); | |
614 | c->sentlen = 0; | |
3853c168 | 615 | c->reply_bytes -= objlen; |
834ef78e | 616 | } |
e2641e09 | 617 | } |
618 | /* Note that we avoid to send more thank REDIS_MAX_WRITE_PER_EVENT | |
619 | * bytes, in a single threaded server it's a good idea to serve | |
620 | * other clients as well, even if a very large request comes from | |
621 | * super fast link that is always able to accept data (in real world | |
622 | * scenario think about 'KEYS *' against the loopback interfae) */ | |
623 | if (totwritten > REDIS_MAX_WRITE_PER_EVENT) break; | |
624 | } | |
625 | if (nwritten == -1) { | |
626 | if (errno == EAGAIN) { | |
627 | nwritten = 0; | |
628 | } else { | |
629 | redisLog(REDIS_VERBOSE, | |
630 | "Error writing to client: %s", strerror(errno)); | |
631 | freeClient(c); | |
632 | return; | |
633 | } | |
634 | } | |
635 | if (totwritten > 0) c->lastinteraction = time(NULL); | |
3bc89500 | 636 | if (c->bufpos == 0 && listLength(c->reply) == 0) { |
e2641e09 | 637 | c->sentlen = 0; |
638 | aeDeleteFileEvent(server.el,c->fd,AE_WRITABLE); | |
941c9fa2 PN |
639 | |
640 | /* Close connection after entire reply has been sent. */ | |
cd8788f2 | 641 | if (c->flags & REDIS_CLOSE_AFTER_REPLY) freeClient(c); |
e2641e09 | 642 | } |
643 | } | |
644 | ||
e2641e09 | 645 | /* resetClient prepare the client to process the next command */ |
646 | void resetClient(redisClient *c) { | |
647 | freeClientArgv(c); | |
cd8788f2 PN |
648 | c->reqtype = 0; |
649 | c->multibulklen = 0; | |
e2641e09 | 650 | c->bulklen = -1; |
6856c7b4 | 651 | /* We clear the ASKING flag as well if we are not inside a MULTI. */ |
652 | if (!(c->flags & REDIS_MULTI)) c->flags &= (~REDIS_ASKING); | |
e2641e09 | 653 | } |
654 | ||
655 | void closeTimedoutClients(void) { | |
656 | redisClient *c; | |
657 | listNode *ln; | |
658 | time_t now = time(NULL); | |
659 | listIter li; | |
660 | ||
661 | listRewind(server.clients,&li); | |
662 | while ((ln = listNext(&li)) != NULL) { | |
663 | c = listNodeValue(ln); | |
664 | if (server.maxidletime && | |
665 | !(c->flags & REDIS_SLAVE) && /* no timeout for slaves */ | |
666 | !(c->flags & REDIS_MASTER) && /* no timeout for masters */ | |
e452436a | 667 | !(c->flags & REDIS_BLOCKED) && /* no timeout for BLPOP */ |
e2641e09 | 668 | dictSize(c->pubsub_channels) == 0 && /* no timeout for pubsub */ |
669 | listLength(c->pubsub_patterns) == 0 && | |
670 | (now - c->lastinteraction > server.maxidletime)) | |
671 | { | |
672 | redisLog(REDIS_VERBOSE,"Closing idle client"); | |
673 | freeClient(c); | |
674 | } else if (c->flags & REDIS_BLOCKED) { | |
e3c51c4b | 675 | if (c->bpop.timeout != 0 && c->bpop.timeout < now) { |
e2641e09 | 676 | addReply(c,shared.nullmultibulk); |
677 | unblockClientWaitingData(c); | |
678 | } | |
679 | } | |
680 | } | |
681 | } | |
682 | ||
cd8788f2 PN |
683 | int processInlineBuffer(redisClient *c) { |
684 | char *newline = strstr(c->querybuf,"\r\n"); | |
685 | int argc, j; | |
686 | sds *argv; | |
687 | size_t querylen; | |
688 | ||
689 | /* Nothing to do without a \r\n */ | |
11e0c4c5 | 690 | if (newline == NULL) { |
691 | if (sdslen(c->querybuf) > REDIS_INLINE_MAX_SIZE) { | |
692 | addReplyError(c,"Protocol error: too big inline request"); | |
693 | setProtocolError(c,0); | |
694 | } | |
cd8788f2 | 695 | return REDIS_ERR; |
11e0c4c5 | 696 | } |
cd8788f2 PN |
697 | |
698 | /* Split the input buffer up to the \r\n */ | |
699 | querylen = newline-(c->querybuf); | |
700 | argv = sdssplitlen(c->querybuf,querylen," ",1,&argc); | |
701 | ||
702 | /* Leave data after the first line of the query in the buffer */ | |
703 | c->querybuf = sdsrange(c->querybuf,querylen+2,-1); | |
704 | ||
705 | /* Setup argv array on client structure */ | |
706 | if (c->argv) zfree(c->argv); | |
707 | c->argv = zmalloc(sizeof(robj*)*argc); | |
708 | ||
709 | /* Create redis objects for all arguments. */ | |
710 | for (c->argc = 0, j = 0; j < argc; j++) { | |
711 | if (sdslen(argv[j])) { | |
712 | c->argv[c->argc] = createObject(REDIS_STRING,argv[j]); | |
713 | c->argc++; | |
714 | } else { | |
715 | sdsfree(argv[j]); | |
716 | } | |
717 | } | |
718 | zfree(argv); | |
719 | return REDIS_OK; | |
720 | } | |
721 | ||
722 | /* Helper function. Trims query buffer to make the function that processes | |
723 | * multi bulk requests idempotent. */ | |
724 | static void setProtocolError(redisClient *c, int pos) { | |
3e0a975e | 725 | if (server.verbosity >= REDIS_VERBOSE) { |
726 | sds client = getClientInfoString(c); | |
727 | redisLog(REDIS_VERBOSE, | |
728 | "Protocol error from client: %s", client); | |
729 | sdsfree(client); | |
730 | } | |
cd8788f2 PN |
731 | c->flags |= REDIS_CLOSE_AFTER_REPLY; |
732 | c->querybuf = sdsrange(c->querybuf,pos,-1); | |
733 | } | |
734 | ||
735 | int processMultibulkBuffer(redisClient *c) { | |
736 | char *newline = NULL; | |
5af30201 PN |
737 | int pos = 0, ok; |
738 | long long ll; | |
cd8788f2 PN |
739 | |
740 | if (c->multibulklen == 0) { | |
741 | /* The client should have been reset */ | |
eab0e26e | 742 | redisAssertWithInfo(c,NULL,c->argc == 0); |
cd8788f2 PN |
743 | |
744 | /* Multi bulk length cannot be read without a \r\n */ | |
5af30201 | 745 | newline = strchr(c->querybuf,'\r'); |
11e0c4c5 | 746 | if (newline == NULL) { |
747 | if (sdslen(c->querybuf) > REDIS_INLINE_MAX_SIZE) { | |
748 | addReplyError(c,"Protocol error: too big mbulk count string"); | |
749 | setProtocolError(c,0); | |
750 | } | |
cd8788f2 | 751 | return REDIS_ERR; |
11e0c4c5 | 752 | } |
cd8788f2 | 753 | |
bf9fd5ff PN |
754 | /* Buffer should also contain \n */ |
755 | if (newline-(c->querybuf) > ((signed)sdslen(c->querybuf)-2)) | |
756 | return REDIS_ERR; | |
757 | ||
cd8788f2 PN |
758 | /* We know for sure there is a whole line since newline != NULL, |
759 | * so go ahead and find out the multi bulk length. */ | |
eab0e26e | 760 | redisAssertWithInfo(c,NULL,c->querybuf[0] == '*'); |
5af30201 PN |
761 | ok = string2ll(c->querybuf+1,newline-(c->querybuf+1),&ll); |
762 | if (!ok || ll > 1024*1024) { | |
b19c33d4 PN |
763 | addReplyError(c,"Protocol error: invalid multibulk length"); |
764 | setProtocolError(c,pos); | |
765 | return REDIS_ERR; | |
cd8788f2 | 766 | } |
af0e51f2 PN |
767 | |
768 | pos = (newline-c->querybuf)+2; | |
769 | if (ll <= 0) { | |
770 | c->querybuf = sdsrange(c->querybuf,pos,-1); | |
771 | return REDIS_OK; | |
772 | } | |
773 | ||
5af30201 | 774 | c->multibulklen = ll; |
cd8788f2 PN |
775 | |
776 | /* Setup argv array on client structure */ | |
777 | if (c->argv) zfree(c->argv); | |
778 | c->argv = zmalloc(sizeof(robj*)*c->multibulklen); | |
cd8788f2 PN |
779 | } |
780 | ||
eab0e26e | 781 | redisAssertWithInfo(c,NULL,c->multibulklen > 0); |
cd8788f2 PN |
782 | while(c->multibulklen) { |
783 | /* Read bulk length if unknown */ | |
784 | if (c->bulklen == -1) { | |
5af30201 | 785 | newline = strchr(c->querybuf+pos,'\r'); |
11e0c4c5 | 786 | if (newline == NULL) { |
787 | if (sdslen(c->querybuf) > REDIS_INLINE_MAX_SIZE) { | |
788 | addReplyError(c,"Protocol error: too big bulk count string"); | |
789 | setProtocolError(c,0); | |
790 | } | |
bf9fd5ff | 791 | break; |
11e0c4c5 | 792 | } |
bf9fd5ff PN |
793 | |
794 | /* Buffer should also contain \n */ | |
795 | if (newline-(c->querybuf) > ((signed)sdslen(c->querybuf)-2)) | |
cd8788f2 | 796 | break; |
bf9fd5ff PN |
797 | |
798 | if (c->querybuf[pos] != '$') { | |
799 | addReplyErrorFormat(c, | |
800 | "Protocol error: expected '$', got '%c'", | |
801 | c->querybuf[pos]); | |
802 | setProtocolError(c,pos); | |
803 | return REDIS_ERR; | |
e2641e09 | 804 | } |
bf9fd5ff PN |
805 | |
806 | ok = string2ll(c->querybuf+pos+1,newline-(c->querybuf+pos+1),&ll); | |
807 | if (!ok || ll < 0 || ll > 512*1024*1024) { | |
808 | addReplyError(c,"Protocol error: invalid bulk length"); | |
809 | setProtocolError(c,pos); | |
810 | return REDIS_ERR; | |
811 | } | |
812 | ||
813 | pos += newline-(c->querybuf+pos)+2; | |
94d490b9 | 814 | if (ll >= REDIS_MBULK_BIG_ARG) { |
826b5beb | 815 | /* If we are going to read a large object from network |
816 | * try to make it likely that it will start at c->querybuf | |
817 | * boundary so that we can optimized object creation | |
818 | * avoiding a large copy of data. */ | |
819 | c->querybuf = sdsrange(c->querybuf,pos,-1); | |
820 | pos = 0; | |
b9031458 | 821 | /* Hint the sds library about the amount of bytes this string is |
822 | * going to contain. */ | |
94d490b9 | 823 | c->querybuf = sdsMakeRoomFor(c->querybuf,ll+2); |
b9031458 | 824 | } |
bf9fd5ff | 825 | c->bulklen = ll; |
cd8788f2 PN |
826 | } |
827 | ||
828 | /* Read bulk argument */ | |
829 | if (sdslen(c->querybuf)-pos < (unsigned)(c->bulklen+2)) { | |
830 | /* Not enough data (+2 == trailing \r\n) */ | |
831 | break; | |
832 | } else { | |
92170955 | 833 | /* Optimization: if the buffer contanins JUST our bulk element |
834 | * instead of creating a new object by *copying* the sds we | |
835 | * just use the current sds string. */ | |
836 | if (pos == 0 && | |
94d490b9 | 837 | c->bulklen >= REDIS_MBULK_BIG_ARG && |
92170955 | 838 | (signed) sdslen(c->querybuf) == c->bulklen+2) |
839 | { | |
840 | c->argv[c->argc++] = createObject(REDIS_STRING,c->querybuf); | |
841 | sdsIncrLen(c->querybuf,-2); /* remove CRLF */ | |
842 | c->querybuf = sdsempty(); | |
843 | /* Assume that if we saw a fat argument we'll see another one | |
844 | * likely... */ | |
845 | c->querybuf = sdsMakeRoomFor(c->querybuf,c->bulklen+2); | |
846 | pos = 0; | |
847 | } else { | |
848 | c->argv[c->argc++] = | |
849 | createStringObject(c->querybuf+pos,c->bulklen); | |
850 | pos += c->bulklen+2; | |
851 | } | |
cd8788f2 PN |
852 | c->bulklen = -1; |
853 | c->multibulklen--; | |
854 | } | |
855 | } | |
856 | ||
857 | /* Trim to pos */ | |
92170955 | 858 | if (pos) c->querybuf = sdsrange(c->querybuf,pos,-1); |
cd8788f2 PN |
859 | |
860 | /* We're done when c->multibulk == 0 */ | |
11e0c4c5 | 861 | if (c->multibulklen == 0) return REDIS_OK; |
862 | ||
863 | /* Still not read to process the command */ | |
cd8788f2 PN |
864 | return REDIS_ERR; |
865 | } | |
866 | ||
867 | void processInputBuffer(redisClient *c) { | |
868 | /* Keep processing while there is something in the input buffer */ | |
869 | while(sdslen(c->querybuf)) { | |
64f201c2 HW |
870 | /* Immediately abort if the client is in the middle of something. */ |
871 | if (c->flags & REDIS_BLOCKED) return; | |
872 | ||
5e78edb3 PN |
873 | /* REDIS_CLOSE_AFTER_REPLY closes the connection once the reply is |
874 | * written to the client. Make sure to not let the reply grow after | |
875 | * this flag has been set (i.e. don't process more commands). */ | |
876 | if (c->flags & REDIS_CLOSE_AFTER_REPLY) return; | |
cd8788f2 PN |
877 | |
878 | /* Determine request type when unknown. */ | |
879 | if (!c->reqtype) { | |
880 | if (c->querybuf[0] == '*') { | |
881 | c->reqtype = REDIS_REQ_MULTIBULK; | |
e2641e09 | 882 | } else { |
cd8788f2 | 883 | c->reqtype = REDIS_REQ_INLINE; |
e2641e09 | 884 | } |
e2641e09 | 885 | } |
cd8788f2 PN |
886 | |
887 | if (c->reqtype == REDIS_REQ_INLINE) { | |
888 | if (processInlineBuffer(c) != REDIS_OK) break; | |
889 | } else if (c->reqtype == REDIS_REQ_MULTIBULK) { | |
890 | if (processMultibulkBuffer(c) != REDIS_OK) break; | |
891 | } else { | |
892 | redisPanic("Unknown request type"); | |
e2641e09 | 893 | } |
cd8788f2 PN |
894 | |
895 | /* Multibulk processing could see a <= 0 length. */ | |
9da6caac PN |
896 | if (c->argc == 0) { |
897 | resetClient(c); | |
898 | } else { | |
899 | /* Only reset the client when the command was executed. */ | |
900 | if (processCommand(c) == REDIS_OK) | |
901 | resetClient(c); | |
902 | } | |
e2641e09 | 903 | } |
904 | } | |
905 | ||
906 | void readQueryFromClient(aeEventLoop *el, int fd, void *privdata, int mask) { | |
907 | redisClient *c = (redisClient*) privdata; | |
826b5beb | 908 | int nread, readlen; |
b8d743e1 | 909 | size_t qblen; |
e2641e09 | 910 | REDIS_NOTUSED(el); |
911 | REDIS_NOTUSED(mask); | |
912 | ||
00010fa9 | 913 | server.current_client = c; |
826b5beb | 914 | readlen = REDIS_IOBUF_LEN; |
915 | /* If this is a multi bulk request, and we are processing a bulk reply | |
916 | * that is large enough, try to maximize the probabilty that the query | |
917 | * buffer contains excatly the SDS string representing the object, even | |
918 | * at the risk of requring more read(2) calls. This way the function | |
919 | * processMultiBulkBuffer() can avoid copying buffers to create the | |
920 | * Redis Object representing the argument. */ | |
826b5beb | 921 | if (c->reqtype == REDIS_REQ_MULTIBULK && c->multibulklen && c->bulklen != -1 |
94d490b9 | 922 | && c->bulklen >= REDIS_MBULK_BIG_ARG) |
826b5beb | 923 | { |
924 | int remaining = (unsigned)(c->bulklen+2)-sdslen(c->querybuf); | |
925 | ||
926 | if (remaining < readlen) readlen = remaining; | |
927 | } | |
826b5beb | 928 | |
b8d743e1 | 929 | qblen = sdslen(c->querybuf); |
826b5beb | 930 | c->querybuf = sdsMakeRoomFor(c->querybuf, readlen); |
931 | nread = read(fd, c->querybuf+qblen, readlen); | |
e2641e09 | 932 | if (nread == -1) { |
933 | if (errno == EAGAIN) { | |
934 | nread = 0; | |
935 | } else { | |
936 | redisLog(REDIS_VERBOSE, "Reading from client: %s",strerror(errno)); | |
937 | freeClient(c); | |
938 | return; | |
939 | } | |
940 | } else if (nread == 0) { | |
941 | redisLog(REDIS_VERBOSE, "Client closed connection"); | |
942 | freeClient(c); | |
943 | return; | |
944 | } | |
945 | if (nread) { | |
b8d743e1 | 946 | sdsIncrLen(c->querybuf,nread); |
e2641e09 | 947 | c->lastinteraction = time(NULL); |
948 | } else { | |
00010fa9 | 949 | server.current_client = NULL; |
e2641e09 | 950 | return; |
951 | } | |
becf5fdb | 952 | if (sdslen(c->querybuf) > server.client_max_querybuf_len) { |
63fd1399 | 953 | sds ci = getClientInfoString(c), bytes = sdsempty(); |
954 | ||
955 | bytes = sdscatrepr(bytes,c->querybuf,64); | |
956 | redisLog(REDIS_WARNING,"Closing client that reached max query buffer length: %s (qbuf initial bytes: %s)", ci, bytes); | |
becf5fdb | 957 | sdsfree(ci); |
63fd1399 | 958 | sdsfree(bytes); |
becf5fdb | 959 | freeClient(c); |
960 | return; | |
961 | } | |
e2641e09 | 962 | processInputBuffer(c); |
00010fa9 | 963 | server.current_client = NULL; |
e2641e09 | 964 | } |
7a1fd61e | 965 | |
966 | void getClientsMaxBuffers(unsigned long *longest_output_list, | |
967 | unsigned long *biggest_input_buffer) { | |
968 | redisClient *c; | |
969 | listNode *ln; | |
970 | listIter li; | |
971 | unsigned long lol = 0, bib = 0; | |
972 | ||
973 | listRewind(server.clients,&li); | |
974 | while ((ln = listNext(&li)) != NULL) { | |
975 | c = listNodeValue(ln); | |
976 | ||
977 | if (listLength(c->reply) > lol) lol = listLength(c->reply); | |
978 | if (sdslen(c->querybuf) > bib) bib = sdslen(c->querybuf); | |
979 | } | |
980 | *longest_output_list = lol; | |
981 | *biggest_input_buffer = bib; | |
982 | } | |
983 | ||
17d25a33 | 984 | /* Turn a Redis client into an sds string representing its state. */ |
985 | sds getClientInfoString(redisClient *client) { | |
6621b8ff | 986 | char ip[32], flags[16], events[3], *p; |
17d25a33 | 987 | int port; |
988 | time_t now = time(NULL); | |
6621b8ff | 989 | int emask; |
17d25a33 | 990 | |
991 | if (anetPeerToString(client->fd,ip,&port) == -1) { | |
992 | ip[0] = '?'; | |
993 | ip[1] = '\0'; | |
994 | port = 0; | |
995 | } | |
996 | p = flags; | |
997 | if (client->flags & REDIS_SLAVE) { | |
998 | if (client->flags & REDIS_MONITOR) | |
999 | *p++ = 'O'; | |
1000 | else | |
1001 | *p++ = 'S'; | |
1002 | } | |
1003 | if (client->flags & REDIS_MASTER) *p++ = 'M'; | |
17d25a33 | 1004 | if (client->flags & REDIS_MULTI) *p++ = 'x'; |
1005 | if (client->flags & REDIS_BLOCKED) *p++ = 'b'; | |
1006 | if (client->flags & REDIS_DIRTY_CAS) *p++ = 'd'; | |
1007 | if (client->flags & REDIS_CLOSE_AFTER_REPLY) *p++ = 'c'; | |
1008 | if (client->flags & REDIS_UNBLOCKED) *p++ = 'u'; | |
afd0f06b | 1009 | if (p == flags) *p++ = 'N'; |
17d25a33 | 1010 | *p++ = '\0'; |
6621b8ff | 1011 | |
1012 | emask = client->fd == -1 ? 0 : aeGetFileEvents(server.el,client->fd); | |
1013 | p = events; | |
1014 | if (emask & AE_READABLE) *p++ = 'r'; | |
1015 | if (emask & AE_WRITABLE) *p++ = 'w'; | |
1016 | *p = '\0'; | |
17d25a33 | 1017 | return sdscatprintf(sdsempty(), |
3853c168 | 1018 | "addr=%s:%d fd=%d idle=%ld flags=%s db=%d sub=%d psub=%d qbuf=%lu obl=%lu oll=%lu omem=%lu events=%s cmd=%s", |
17d25a33 | 1019 | ip,port,client->fd, |
1020 | (long)(now - client->lastinteraction), | |
1021 | flags, | |
1022 | client->db->id, | |
1023 | (int) dictSize(client->pubsub_channels), | |
491c1c4e | 1024 | (int) listLength(client->pubsub_patterns), |
1025 | (unsigned long) sdslen(client->querybuf), | |
1026 | (unsigned long) client->bufpos, | |
6621b8ff | 1027 | (unsigned long) listLength(client->reply), |
3853c168 | 1028 | getClientOutputBufferMemoryUsage(client), |
2c74a9f9 | 1029 | events, |
1030 | client->lastcmd ? client->lastcmd->name : "NULL"); | |
17d25a33 | 1031 | } |
1032 | ||
45e7a1ce | 1033 | sds getAllClientsInfoString(void) { |
1034 | listNode *ln; | |
1035 | listIter li; | |
1036 | redisClient *client; | |
1037 | sds o = sdsempty(); | |
1038 | ||
1039 | listRewind(server.clients,&li); | |
1040 | while ((ln = listNext(&li)) != NULL) { | |
0a466a75 | 1041 | sds cs; |
1042 | ||
45e7a1ce | 1043 | client = listNodeValue(ln); |
0a466a75 | 1044 | cs = getClientInfoString(client); |
1045 | o = sdscatsds(o,cs); | |
1046 | sdsfree(cs); | |
45e7a1ce | 1047 | o = sdscatlen(o,"\n",1); |
1048 | } | |
1049 | return o; | |
1050 | } | |
1051 | ||
3cd12b56 | 1052 | void clientCommand(redisClient *c) { |
b93fdb7b | 1053 | listNode *ln; |
1054 | listIter li; | |
1055 | redisClient *client; | |
1056 | ||
3cd12b56 | 1057 | if (!strcasecmp(c->argv[1]->ptr,"list") && c->argc == 2) { |
45e7a1ce | 1058 | sds o = getAllClientsInfoString(); |
3cd12b56 | 1059 | addReplyBulkCBuffer(c,o,sdslen(o)); |
1060 | sdsfree(o); | |
b93fdb7b | 1061 | } else if (!strcasecmp(c->argv[1]->ptr,"kill") && c->argc == 3) { |
1062 | listRewind(server.clients,&li); | |
1063 | while ((ln = listNext(&li)) != NULL) { | |
1064 | char ip[32], addr[64]; | |
1065 | int port; | |
1066 | ||
1067 | client = listNodeValue(ln); | |
1068 | if (anetPeerToString(client->fd,ip,&port) == -1) continue; | |
1069 | snprintf(addr,sizeof(addr),"%s:%d",ip,port); | |
1070 | if (strcmp(addr,c->argv[2]->ptr) == 0) { | |
1071 | addReply(c,shared.ok); | |
1072 | if (c == client) { | |
1073 | client->flags |= REDIS_CLOSE_AFTER_REPLY; | |
1074 | } else { | |
1075 | freeClient(client); | |
1076 | } | |
1077 | return; | |
1078 | } | |
1079 | } | |
1080 | addReplyError(c,"No such client"); | |
3cd12b56 | 1081 | } else { |
1082 | addReplyError(c, "Syntax error, try CLIENT (LIST | KILL ip:port)"); | |
1083 | } | |
1084 | } | |
c1c9d551 | 1085 | |
4dd444bb | 1086 | /* Rewrite the command vector of the client. All the new objects ref count |
1087 | * is incremented. The old command vector is freed, and the old objects | |
1088 | * ref count is decremented. */ | |
c1c9d551 | 1089 | void rewriteClientCommandVector(redisClient *c, int argc, ...) { |
1090 | va_list ap; | |
1091 | int j; | |
1092 | robj **argv; /* The new argument vector */ | |
1093 | ||
1094 | argv = zmalloc(sizeof(robj*)*argc); | |
1095 | va_start(ap,argc); | |
1096 | for (j = 0; j < argc; j++) { | |
1097 | robj *a; | |
1098 | ||
1099 | a = va_arg(ap, robj*); | |
1100 | argv[j] = a; | |
1101 | incrRefCount(a); | |
1102 | } | |
1103 | /* We free the objects in the original vector at the end, so we are | |
1104 | * sure that if the same objects are reused in the new vector the | |
1105 | * refcount gets incremented before it gets decremented. */ | |
1106 | for (j = 0; j < c->argc; j++) decrRefCount(c->argv[j]); | |
1107 | zfree(c->argv); | |
1108 | /* Replace argv and argc with our new versions. */ | |
1109 | c->argv = argv; | |
1110 | c->argc = argc; | |
09e2d9ee | 1111 | c->cmd = lookupCommand(c->argv[0]->ptr); |
eab0e26e | 1112 | redisAssertWithInfo(c,NULL,c->cmd != NULL); |
c1c9d551 | 1113 | va_end(ap); |
1114 | } | |
4dd444bb | 1115 | |
1116 | /* Rewrite a single item in the command vector. | |
1117 | * The new val ref count is incremented, and the old decremented. */ | |
1118 | void rewriteClientCommandArgument(redisClient *c, int i, robj *newval) { | |
1119 | robj *oldval; | |
1120 | ||
eab0e26e | 1121 | redisAssertWithInfo(c,NULL,i < c->argc); |
4dd444bb | 1122 | oldval = c->argv[i]; |
1123 | c->argv[i] = newval; | |
1124 | incrRefCount(newval); | |
1125 | decrRefCount(oldval); | |
1126 | ||
1127 | /* If this is the command name make sure to fix c->cmd. */ | |
1128 | if (i == 0) { | |
1129 | c->cmd = lookupCommand(c->argv[0]->ptr); | |
eab0e26e | 1130 | redisAssertWithInfo(c,NULL,c->cmd != NULL); |
4dd444bb | 1131 | } |
1132 | } | |
3853c168 | 1133 | |
1134 | /* This function returns the number of bytes that Redis is virtually | |
1135 | * using to store the reply still not read by the client. | |
1136 | * It is "virtual" since the reply output list may contain objects that | |
1137 | * are shared and are not really using additional memory. | |
1138 | * | |
1139 | * The function returns the total sum of the length of all the objects | |
1140 | * stored in the output list, plus the memory used to allocate every | |
1141 | * list node. The static reply buffer is not taken into account since it | |
1142 | * is allocated anyway. | |
1143 | * | |
1144 | * Note: this function is very fast so can be called as many time as | |
1145 | * the caller wishes. The main usage of this function currently is | |
1146 | * enforcing the client output lenght limits. */ | |
1147 | unsigned long getClientOutputBufferMemoryUsage(redisClient *c) { | |
1148 | unsigned long list_item_size = sizeof(listNode); | |
1149 | ||
1150 | return c->reply_bytes + (list_item_size*listLength(c->reply)); | |
1151 | } |