Fixed mget_execute possibly failing into some non ending recursive call
[m6w6/libmemcached] / libmemcached / memcached_io.c
1 /*
2 Basic socket buffered IO
3 */
4
5 #include "common.h"
6 #include "memcached_io.h"
7 #include <sys/select.h>
8 #include <poll.h>
9
10 typedef enum {
11 MEM_READ,
12 MEM_WRITE
13 } memc_read_or_write;
14
15 static ssize_t io_flush(memcached_server_st *ptr, memcached_return *error);
16 static void increment_udp_message_id(memcached_server_st *ptr);
17
18 static memcached_return io_wait(memcached_server_st *ptr,
19 memc_read_or_write read_or_write)
20 {
21 struct pollfd fds= {
22 .fd= ptr->fd,
23 .events = POLLIN
24 };
25 int error;
26
27 unlikely (read_or_write == MEM_WRITE) /* write */
28 fds.events= POLLOUT;
29
30 /*
31 ** We are going to block on write, but at least on Solaris we might block
32 ** on write if we haven't read anything from our input buffer..
33 ** Try to purge the input buffer if we don't do any flow control in the
34 ** application layer (just sending a lot of data etc)
35 ** The test is moved down in the purge function to avoid duplication of
36 ** the test.
37 */
38 if (read_or_write == MEM_WRITE)
39 {
40 memcached_return rc= memcached_purge(ptr);
41 if (rc != MEMCACHED_SUCCESS && rc != MEMCACHED_STORED)
42 return MEMCACHED_FAILURE;
43 }
44
45 int timeout= ptr->root->poll_timeout;
46 if ((ptr->root->flags & MEM_NO_BLOCK) == 0)
47 timeout= -1;
48
49 error= poll(&fds, 1, timeout);
50
51 if (error == 1)
52 return MEMCACHED_SUCCESS;
53 else if (error == 0)
54 return MEMCACHED_TIMEOUT;
55
56 /* Imposssible for anything other then -1 */
57 WATCHPOINT_ASSERT(error == -1);
58 memcached_quit_server(ptr, 1);
59
60 return MEMCACHED_FAILURE;
61 }
62
63 /**
64 * Try to fill the input buffer for a server with as much
65 * data as possible.
66 *
67 * @param ptr the server to pack
68 */
69 static bool repack_input_buffer(memcached_server_st *ptr)
70 {
71 if (ptr->read_ptr != ptr->read_buffer)
72 {
73 /* Move all of the data to the beginning of the buffer so
74 ** that we can fit more data into the buffer...
75 */
76 memmove(ptr->read_buffer, ptr->read_ptr, ptr->read_buffer_length);
77 ptr->read_ptr= ptr->read_buffer;
78 ptr->read_data_length= ptr->read_buffer_length;
79 }
80
81 /* There is room in the buffer, try to fill it! */
82 if (ptr->read_buffer_length != MEMCACHED_MAX_BUFFER)
83 {
84 /* Just try a single read to grab what's available */
85 ssize_t nr= read(ptr->fd,
86 ptr->read_ptr + ptr->read_data_length,
87 MEMCACHED_MAX_BUFFER - ptr->read_data_length);
88
89 if (nr > 0)
90 {
91 ptr->read_data_length+= (size_t)nr;
92 ptr->read_buffer_length+= (size_t)nr;
93 return true;
94 }
95 }
96 return false;
97 }
98
99 /**
100 * If the we have callbacks connected to this server structure
101 * we may start process the input queue and fire the callbacks
102 * for the incomming messages. This function is _only_ called
103 * when the input buffer is full, so that we _know_ that we have
104 * at least _one_ message to process.
105 *
106 * @param ptr the server to star processing iput messages for
107 * @return true if we processed anything, false otherwise
108 */
109 static bool process_input_buffer(memcached_server_st *ptr)
110 {
111 /*
112 ** We might be able to process some of the response messages if we
113 ** have a callback set up
114 */
115 if (ptr->root->callbacks != NULL && (ptr->root->flags & MEM_USE_UDP) == 0)
116 {
117 /*
118 * We might have responses... try to read them out and fire
119 * callbacks
120 */
121 memcached_callback_st cb= *ptr->root->callbacks;
122
123 ptr->root->processing_input = 1;
124
125 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
126 memcached_return error;
127 error= memcached_response(ptr, buffer, sizeof(buffer),
128 &ptr->root->result);
129
130 ptr->root->processing_input = 0;
131
132 if (error == MEMCACHED_SUCCESS)
133 {
134 for (unsigned int x= 0; x < cb.number_of_callback; x++)
135 {
136 error= (*cb.callback[x])(ptr->root, &ptr->root->result, cb.context);
137 if (error != MEMCACHED_SUCCESS)
138 break;
139 }
140
141 /* @todo what should I do with the error message??? */
142 }
143 /* @todo what should I do with other error messages?? */
144 return true;
145 }
146
147 return false;
148 }
149
150 #ifdef UNUSED
151 void memcached_io_preread(memcached_st *ptr)
152 {
153 unsigned int x;
154
155 return;
156
157 for (x= 0; x < ptr->number_of_hosts; x++)
158 {
159 if (memcached_server_response_count(ptr, x) &&
160 ptr->hosts[x].read_data_length < MEMCACHED_MAX_BUFFER )
161 {
162 size_t data_read;
163
164 data_read= read(ptr->hosts[x].fd,
165 ptr->hosts[x].read_ptr + ptr->hosts[x].read_data_length,
166 MEMCACHED_MAX_BUFFER - ptr->hosts[x].read_data_length);
167 if (data_read == -1)
168 continue;
169
170 ptr->hosts[x].read_buffer_length+= data_read;
171 ptr->hosts[x].read_data_length+= data_read;
172 }
173 }
174 }
175 #endif
176
177 memcached_return memcached_io_read(memcached_server_st *ptr,
178 void *buffer, size_t length, ssize_t *nread)
179 {
180 char *buffer_ptr;
181
182 buffer_ptr= buffer;
183
184 while (length)
185 {
186 if (!ptr->read_buffer_length)
187 {
188 ssize_t data_read;
189
190 while (1)
191 {
192 data_read= read(ptr->fd, ptr->read_buffer, MEMCACHED_MAX_BUFFER);
193 if (data_read > 0)
194 break;
195 else if (data_read == -1)
196 {
197 ptr->cached_errno= errno;
198 memcached_return rc= MEMCACHED_UNKNOWN_READ_FAILURE;
199 switch (errno)
200 {
201 case EAGAIN:
202 case EINTR:
203 if ((rc= io_wait(ptr, MEM_READ)) == MEMCACHED_SUCCESS)
204 continue;
205 /* fall through */
206
207 default:
208 {
209 memcached_quit_server(ptr, 1);
210 *nread= -1;
211 return rc;
212 }
213 }
214 }
215 else
216 {
217 /*
218 EOF. Any data received so far is incomplete
219 so discard it. This always reads by byte in case of TCP
220 and protocol enforcement happens at memcached_response()
221 looking for '\n'. We do not care for UDB which requests 8 bytes
222 at once. Generally, this means that connection went away. Since
223 for blocking I/O we do not return 0 and for non-blocking case
224 it will return EGAIN if data is not immediatly available.
225 */
226 memcached_quit_server(ptr, 1);
227 *nread= -1;
228 return MEMCACHED_UNKNOWN_READ_FAILURE;
229 }
230 }
231
232 ptr->io_bytes_sent = 0;
233 ptr->read_data_length= (size_t) data_read;
234 ptr->read_buffer_length= (size_t) data_read;
235 ptr->read_ptr= ptr->read_buffer;
236 }
237
238 if (length > 1)
239 {
240 size_t difference;
241
242 difference= (length > ptr->read_buffer_length) ? ptr->read_buffer_length : length;
243
244 memcpy(buffer_ptr, ptr->read_ptr, difference);
245 length -= difference;
246 ptr->read_ptr+= difference;
247 ptr->read_buffer_length-= difference;
248 buffer_ptr+= difference;
249 }
250 else
251 {
252 *buffer_ptr= *ptr->read_ptr;
253 ptr->read_ptr++;
254 ptr->read_buffer_length--;
255 buffer_ptr++;
256 break;
257 }
258 }
259
260 ptr->server_failure_counter= 0;
261 *nread = (ssize_t)(buffer_ptr - (char*)buffer);
262 return MEMCACHED_SUCCESS;
263 }
264
265 ssize_t memcached_io_write(memcached_server_st *ptr,
266 const void *buffer, size_t length, char with_flush)
267 {
268 size_t original_length;
269 const char* buffer_ptr;
270
271 WATCHPOINT_ASSERT(ptr->fd != -1);
272
273 original_length= length;
274 buffer_ptr= buffer;
275
276 while (length)
277 {
278 char *write_ptr;
279 size_t should_write;
280 size_t buffer_end;
281
282 if (ptr->type == MEMCACHED_CONNECTION_UDP)
283 {
284 //UDP does not support partial writes
285 buffer_end= MAX_UDP_DATAGRAM_LENGTH;
286 should_write= length;
287 if (ptr->write_buffer_offset + should_write > buffer_end)
288 return -1;
289 }
290 else
291 {
292 buffer_end= MEMCACHED_MAX_BUFFER;
293 should_write= buffer_end - ptr->write_buffer_offset;
294 should_write= (should_write < length) ? should_write : length;
295 }
296
297 write_ptr= ptr->write_buffer + ptr->write_buffer_offset;
298 memcpy(write_ptr, buffer_ptr, should_write);
299 ptr->write_buffer_offset+= should_write;
300 buffer_ptr+= should_write;
301 length-= should_write;
302
303 if (ptr->write_buffer_offset == buffer_end && ptr->type != MEMCACHED_CONNECTION_UDP)
304 {
305 memcached_return rc;
306 ssize_t sent_length;
307
308 WATCHPOINT_ASSERT(ptr->fd != -1);
309 sent_length= io_flush(ptr, &rc);
310 if (sent_length == -1)
311 return -1;
312
313 /* If io_flush calls memcached_purge, sent_length may be 0 */
314 unlikely (sent_length != 0)
315 {
316 WATCHPOINT_ASSERT(sent_length == (ssize_t)buffer_end);
317 }
318 }
319 }
320
321 if (with_flush)
322 {
323 memcached_return rc;
324 WATCHPOINT_ASSERT(ptr->fd != -1);
325 if (io_flush(ptr, &rc) == -1)
326 return -1;
327 }
328
329 return (ssize_t) original_length;
330 }
331
332 memcached_return memcached_io_close(memcached_server_st *ptr)
333 {
334 int r;
335
336 if (ptr->fd == -1)
337 return MEMCACHED_SUCCESS;
338
339 /* in case of death shutdown to avoid blocking at close() */
340 if (1)
341 {
342 r= shutdown(ptr->fd, SHUT_RDWR);
343
344 #ifdef DEBUG
345 if (r && errno != ENOTCONN)
346 {
347 WATCHPOINT_NUMBER(ptr->fd);
348 WATCHPOINT_ERRNO(errno);
349 WATCHPOINT_ASSERT(errno);
350 }
351 #endif
352 }
353
354 r= close(ptr->fd);
355 #ifdef DEBUG
356 if (r != 0)
357 WATCHPOINT_ERRNO(errno);
358 #endif
359
360 return MEMCACHED_SUCCESS;
361 }
362
363 memcached_server_st *memcached_io_get_readable_server(memcached_st *memc)
364 {
365 #define MAX_SERVERS_TO_POLL 100
366 struct pollfd fds[MAX_SERVERS_TO_POLL];
367 unsigned int host_index= 0;
368
369 for (unsigned int x= 0;
370 x< memc->number_of_hosts && host_index < MAX_SERVERS_TO_POLL;
371 ++x)
372 {
373 if (memc->hosts[x].read_buffer_length > 0) /* I have data in the buffer */
374 return &memc->hosts[x];
375
376 if (memcached_server_response_count(&memc->hosts[x]) > 0)
377 {
378 fds[host_index].events = POLLIN;
379 fds[host_index].revents = 0;
380 fds[host_index].fd = memc->hosts[x].fd;
381 ++host_index;
382 }
383 }
384
385 if (host_index < 2)
386 {
387 /* We have 0 or 1 server with pending events.. */
388 for (unsigned int x= 0; x< memc->number_of_hosts; ++x)
389 if (memcached_server_response_count(&memc->hosts[x]) > 0)
390 return &memc->hosts[x];
391
392 return NULL;
393 }
394
395 int err= poll(fds, host_index, memc->poll_timeout);
396 switch (err) {
397 case -1:
398 memc->cached_errno = errno;
399 /* FALLTHROUGH */
400 case 0:
401 break;
402 default:
403 for (unsigned int x= 0; x < host_index; ++x)
404 if (fds[x].revents & POLLIN)
405 for (unsigned int y= 0; y < memc->number_of_hosts; ++y)
406 if (memc->hosts[y].fd == fds[x].fd)
407 return &memc->hosts[y];
408 }
409
410 return NULL;
411 }
412
413 static ssize_t io_flush(memcached_server_st *ptr,
414 memcached_return *error)
415 {
416 /*
417 ** We might want to purge the input buffer if we haven't consumed
418 ** any output yet... The test for the limits is the purge is inline
419 ** in the purge function to avoid duplicating the logic..
420 */
421 {
422 memcached_return rc;
423 WATCHPOINT_ASSERT(ptr->fd != -1);
424 rc= memcached_purge(ptr);
425
426 if (rc != MEMCACHED_SUCCESS && rc != MEMCACHED_STORED)
427 return -1;
428 }
429 ssize_t sent_length;
430 size_t return_length;
431 char *local_write_ptr= ptr->write_buffer;
432 size_t write_length= ptr->write_buffer_offset;
433
434 *error= MEMCACHED_SUCCESS;
435
436 WATCHPOINT_ASSERT(ptr->fd != -1);
437
438 // UDP Sanity check, make sure that we are not sending somthing too big
439 if (ptr->type == MEMCACHED_CONNECTION_UDP && write_length > MAX_UDP_DATAGRAM_LENGTH)
440 return -1;
441
442 if (ptr->write_buffer_offset == 0 || (ptr->type == MEMCACHED_CONNECTION_UDP
443 && ptr->write_buffer_offset == UDP_DATAGRAM_HEADER_LENGTH))
444 return 0;
445
446 /* Looking for memory overflows */
447 #if defined(DEBUG)
448 if (write_length == MEMCACHED_MAX_BUFFER)
449 WATCHPOINT_ASSERT(ptr->write_buffer == local_write_ptr);
450 WATCHPOINT_ASSERT((ptr->write_buffer + MEMCACHED_MAX_BUFFER) >= (local_write_ptr + write_length));
451 #endif
452
453 return_length= 0;
454 while (write_length)
455 {
456 WATCHPOINT_ASSERT(ptr->fd != -1);
457 WATCHPOINT_ASSERT(write_length > 0);
458 sent_length= 0;
459 if (ptr->type == MEMCACHED_CONNECTION_UDP)
460 increment_udp_message_id(ptr);
461 sent_length= write(ptr->fd, local_write_ptr, write_length);
462
463 if (sent_length == -1)
464 {
465 ptr->cached_errno= errno;
466 switch (errno)
467 {
468 case ENOBUFS:
469 continue;
470 case EAGAIN:
471 {
472 /*
473 * We may be blocked on write because the input buffer
474 * is full. Let's check if we have room in our input
475 * buffer for more data and retry the write before
476 * waiting..
477 */
478 if (repack_input_buffer(ptr) ||
479 process_input_buffer(ptr))
480 continue;
481
482 memcached_return rc;
483 rc= io_wait(ptr, MEM_WRITE);
484
485 if (rc == MEMCACHED_SUCCESS || rc == MEMCACHED_TIMEOUT)
486 continue;
487
488 memcached_quit_server(ptr, 1);
489 return -1;
490 }
491 default:
492 memcached_quit_server(ptr, 1);
493 *error= MEMCACHED_ERRNO;
494 return -1;
495 }
496 }
497
498 if (ptr->type == MEMCACHED_CONNECTION_UDP &&
499 (size_t)sent_length != write_length)
500 {
501 memcached_quit_server(ptr, 1);
502 return -1;
503 }
504
505 ptr->io_bytes_sent += (uint32_t) sent_length;
506
507 local_write_ptr+= sent_length;
508 write_length-= (uint32_t) sent_length;
509 return_length+= (uint32_t) sent_length;
510 }
511
512 WATCHPOINT_ASSERT(write_length == 0);
513 // Need to study this assert() WATCHPOINT_ASSERT(return_length ==
514 // ptr->write_buffer_offset);
515
516 // if we are a udp server, the begining of the buffer is reserverd for
517 // the upd frame header
518 if (ptr->type == MEMCACHED_CONNECTION_UDP)
519 ptr->write_buffer_offset= UDP_DATAGRAM_HEADER_LENGTH;
520 else
521 ptr->write_buffer_offset= 0;
522
523 return (ssize_t) return_length;
524 }
525
526 /*
527 Eventually we will just kill off the server with the problem.
528 */
529 void memcached_io_reset(memcached_server_st *ptr)
530 {
531 memcached_quit_server(ptr, 1);
532 }
533
534 /**
535 * Read a given number of bytes from the server and place it into a specific
536 * buffer. Reset the IO channel on this server if an error occurs.
537 */
538 memcached_return memcached_safe_read(memcached_server_st *ptr,
539 void *dta,
540 size_t size)
541 {
542 size_t offset= 0;
543 char *data= dta;
544
545 while (offset < size)
546 {
547 ssize_t nread;
548 memcached_return rc= memcached_io_read(ptr, data + offset, size - offset,
549 &nread);
550 if (rc != MEMCACHED_SUCCESS)
551 return rc;
552
553 offset+= (size_t) nread;
554 }
555
556 return MEMCACHED_SUCCESS;
557 }
558
559 memcached_return memcached_io_readline(memcached_server_st *ptr,
560 char *buffer_ptr,
561 size_t size)
562 {
563 bool line_complete= false;
564 size_t total_nr= 0;
565
566 while (!line_complete)
567 {
568 if (ptr->read_buffer_length == 0)
569 {
570 /*
571 * We don't have any data in the buffer, so let's fill the read
572 * buffer. Call the standard read function to avoid duplicating
573 * the logic.
574 */
575 ssize_t nread;
576 memcached_return rc= memcached_io_read(ptr, buffer_ptr, 1, &nread);
577 if (rc != MEMCACHED_SUCCESS)
578 return rc;
579
580 if (*buffer_ptr == '\n')
581 line_complete= true;
582
583 ++buffer_ptr;
584 ++total_nr;
585 }
586
587 /* Now let's look in the buffer and copy as we go! */
588 while (ptr->read_buffer_length && total_nr < size && !line_complete)
589 {
590 *buffer_ptr = *ptr->read_ptr;
591 if (*buffer_ptr == '\n')
592 line_complete = true;
593 --ptr->read_buffer_length;
594 ++ptr->read_ptr;
595 ++total_nr;
596 ++buffer_ptr;
597 }
598
599 if (total_nr == size)
600 return MEMCACHED_PROTOCOL_ERROR;
601 }
602
603 return MEMCACHED_SUCCESS;
604 }
605
606 /*
607 * The udp request id consists of two seperate sections
608 * 1) The thread id
609 * 2) The message number
610 * The thread id should only be set when the memcached_st struct is created
611 * and should not be changed.
612 *
613 * The message num is incremented for each new message we send, this function
614 * extracts the message number from message_id, increments it and then
615 * writes the new value back into the header
616 */
617 static void increment_udp_message_id(memcached_server_st *ptr)
618 {
619 struct udp_datagram_header_st *header= (struct udp_datagram_header_st *)ptr->write_buffer;
620 uint16_t cur_req= get_udp_datagram_request_id(header);
621 int msg_num= get_msg_num_from_request_id(cur_req);
622 int thread_id= get_thread_id_from_request_id(cur_req);
623
624 if (((++msg_num) & UDP_REQUEST_ID_THREAD_MASK) != 0)
625 msg_num= 0;
626
627 header->request_id= htons((uint16_t) (thread_id | msg_num));
628 }
629
630 memcached_return memcached_io_init_udp_header(memcached_server_st *ptr, uint16_t thread_id)
631 {
632 if (thread_id > UDP_REQUEST_ID_MAX_THREAD_ID)
633 return MEMCACHED_FAILURE;
634
635 struct udp_datagram_header_st *header= (struct udp_datagram_header_st *)ptr->write_buffer;
636 header->request_id= htons((uint16_t) (generate_udp_request_thread_id(thread_id)));
637 header->num_datagrams= htons(1);
638 header->sequence_number= htons(0);
639
640 return MEMCACHED_SUCCESS;
641 }