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