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