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