43fb509e64c18f44f8dd6352d78e21511b8fc2be
[awesomized/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[1];
22 short flags= 0;
23 int error;
24
25 if (read_or_write == MEM_WRITE) /* write */
26 flags= POLLOUT | POLLERR;
27 else
28 flags= POLLIN | POLLERR;
29
30 memset(&fds, 0, sizeof(struct pollfd));
31 fds[0].fd= ptr->fd;
32 fds[0].events= flags;
33
34 /*
35 ** We are going to block on write, but at least on Solaris we might block
36 ** on write if we haven't read anything from our input buffer..
37 ** Try to purge the input buffer if we don't do any flow control in the
38 ** application layer (just sending a lot of data etc)
39 ** The test is moved down in the purge function to avoid duplication of
40 ** the test.
41 */
42 if (read_or_write == MEM_WRITE)
43 {
44 memcached_return rc=memcached_purge(ptr);
45 if (rc != MEMCACHED_SUCCESS && rc != MEMCACHED_STORED)
46 return MEMCACHED_FAILURE;
47 }
48
49 error= poll(fds, 1, ptr->root->poll_timeout);
50
51 if (error == 1)
52 return MEMCACHED_SUCCESS;
53 else if (error == 0)
54 {
55 return MEMCACHED_TIMEOUT;
56 }
57
58 /* Imposssible for anything other then -1 */
59 WATCHPOINT_ASSERT(error == -1);
60 memcached_quit_server(ptr, 1);
61
62 return MEMCACHED_FAILURE;
63
64 }
65
66 #ifdef UNUSED
67 void memcached_io_preread(memcached_st *ptr)
68 {
69 unsigned int x;
70
71 return;
72
73 for (x= 0; x < ptr->number_of_hosts; x++)
74 {
75 if (memcached_server_response_count(ptr, x) &&
76 ptr->hosts[x].read_data_length < MEMCACHED_MAX_BUFFER )
77 {
78 size_t data_read;
79
80 data_read= read(ptr->hosts[x].fd,
81 ptr->hosts[x].read_ptr + ptr->hosts[x].read_data_length,
82 MEMCACHED_MAX_BUFFER - ptr->hosts[x].read_data_length);
83 if (data_read == -1)
84 continue;
85
86 ptr->hosts[x].read_buffer_length+= data_read;
87 ptr->hosts[x].read_data_length+= data_read;
88 }
89 }
90 }
91 #endif
92
93 ssize_t memcached_io_read(memcached_server_st *ptr,
94 void *buffer, size_t length)
95 {
96 char *buffer_ptr;
97
98 buffer_ptr= buffer;
99
100 while (length)
101 {
102 if (!ptr->read_buffer_length)
103 {
104 ssize_t data_read;
105
106 while (1)
107 {
108 data_read= read(ptr->fd,
109 ptr->read_buffer,
110 MEMCACHED_MAX_BUFFER);
111 if (data_read > 0)
112 break;
113 else if (data_read == -1)
114 {
115 ptr->cached_errno= errno;
116 switch (errno)
117 {
118 case EAGAIN:
119 case EINTR:
120 {
121 memcached_return rc;
122
123 rc= io_wait(ptr, MEM_READ);
124
125 if (rc == MEMCACHED_SUCCESS)
126 continue;
127 }
128 /* fall trough */
129 default:
130 {
131 memcached_quit_server(ptr, 1);
132 return -1;
133 }
134 }
135 }
136 else
137 {
138 /*
139 EOF. Any data received so far is incomplete
140 so discard it. This always reads by byte in case of TCP
141 and protocol enforcement happens at memcached_response()
142 looking for '\n'. We do not care for UDB which requests 8 bytes
143 at once. Generally, this means that connection went away. Since
144 for blocking I/O we do not return 0 and for non-blocking case
145 it will return EGAIN if data is not immediatly available.
146 */
147 memcached_quit_server(ptr, 1);
148 return -1;
149 }
150 }
151
152 ptr->io_bytes_sent = 0;
153 ptr->read_data_length= data_read;
154 ptr->read_buffer_length= data_read;
155 ptr->read_ptr= ptr->read_buffer;
156 }
157
158 if (length > 1)
159 {
160 size_t difference;
161
162 difference= (length > ptr->read_buffer_length) ? ptr->read_buffer_length : length;
163
164 memcpy(buffer_ptr, ptr->read_ptr, difference);
165 length -= difference;
166 ptr->read_ptr+= difference;
167 ptr->read_buffer_length-= difference;
168 buffer_ptr+= difference;
169 }
170 else
171 {
172 *buffer_ptr= *ptr->read_ptr;
173 ptr->read_ptr++;
174 ptr->read_buffer_length--;
175 buffer_ptr++;
176 break;
177 }
178 }
179
180 return (size_t)(buffer_ptr - (char*)buffer);
181 }
182
183 ssize_t memcached_io_write(memcached_server_st *ptr,
184 const void *buffer, size_t length, char with_flush)
185 {
186 size_t original_length;
187 const char* buffer_ptr;
188
189 WATCHPOINT_ASSERT(ptr->fd != -1);
190
191 original_length= length;
192 buffer_ptr= buffer;
193
194 while (length)
195 {
196 char *write_ptr;
197 size_t should_write;
198 size_t buffer_end;
199
200 if (ptr->type == MEMCACHED_CONNECTION_UDP)
201 {
202 //UDP does not support partial writes
203 buffer_end= MAX_UDP_DATAGRAM_LENGTH;
204 should_write= length;
205 if (ptr->write_buffer_offset + should_write > buffer_end)
206 return -1;
207 }
208 else
209 {
210 buffer_end= MEMCACHED_MAX_BUFFER;
211 should_write= buffer_end - ptr->write_buffer_offset;
212 should_write= (should_write < length) ? should_write : length;
213 }
214
215 write_ptr= ptr->write_buffer + ptr->write_buffer_offset;
216 memcpy(write_ptr, buffer_ptr, should_write);
217 ptr->write_buffer_offset+= should_write;
218 buffer_ptr+= should_write;
219 length-= should_write;
220
221 if (ptr->write_buffer_offset == buffer_end && ptr->type != MEMCACHED_CONNECTION_UDP)
222 {
223 memcached_return rc;
224 ssize_t sent_length;
225
226 WATCHPOINT_ASSERT(ptr->fd != -1);
227 sent_length= io_flush(ptr, &rc);
228 if (sent_length == -1)
229 return -1;
230
231 /* If io_flush calls memcached_purge, sent_length may be 0 */
232 if (sent_length != 0)
233 WATCHPOINT_ASSERT(sent_length == buffer_end);
234 }
235 }
236
237 if (with_flush)
238 {
239 memcached_return rc;
240 WATCHPOINT_ASSERT(ptr->fd != -1);
241 if (io_flush(ptr, &rc) == -1)
242 return -1;
243 }
244
245 return original_length;
246 }
247
248 memcached_return memcached_io_close(memcached_server_st *ptr)
249 {
250 int r;
251
252 if (ptr->fd == -1)
253 return MEMCACHED_SUCCESS;
254
255 /* in case of death shutdown to avoid blocking at close() */
256 if (1)
257 {
258 r= shutdown(ptr->fd, SHUT_RDWR);
259
260 #ifdef HAVE_DEBUG
261 if (r && errno != ENOTCONN)
262 {
263 WATCHPOINT_NUMBER(ptr->fd);
264 WATCHPOINT_ERRNO(errno);
265 WATCHPOINT_ASSERT(errno);
266 }
267 #endif
268 }
269
270 r= close(ptr->fd);
271 #ifdef HAVE_DEBUG
272 if (r != 0)
273 WATCHPOINT_ERRNO(errno);
274 #endif
275
276 return MEMCACHED_SUCCESS;
277 }
278
279 memcached_server_st *memcached_io_get_readable_server(memcached_st *memc)
280 {
281 #define MAX_SERVERS_TO_POLL 100
282 struct pollfd fds[MAX_SERVERS_TO_POLL];
283 int index= 0;
284
285 for (int x= 0; x< memc->number_of_hosts && index < MAX_SERVERS_TO_POLL; ++x)
286 {
287 if (memc->hosts[x].read_buffer_length > 0) /* I have data in the buffer */
288 return &memc->hosts[x];
289
290 if (memcached_server_response_count(&memc->hosts[x]) > 0)
291 {
292 fds[index].events = POLLIN;
293 fds[index].revents = 0;
294 fds[index].fd = memc->hosts[x].fd;
295 ++index;
296 }
297 }
298
299 if (index < 2)
300 {
301 /* We have 0 or 1 server with pending events.. */
302 for (int x= 0; x< memc->number_of_hosts; ++x)
303 if (memcached_server_response_count(&memc->hosts[x]) > 0)
304 return &memc->hosts[x];
305
306 return NULL;
307 }
308
309 int err= poll(fds, index, memc->poll_timeout);
310 switch (err) {
311 case -1:
312 memc->cached_errno = errno;
313 /* FALLTHROUGH */
314 case 0:
315 break;
316 default:
317 for (int x= 0; x < index; ++x)
318 if (fds[x].revents & POLLIN)
319 for (int y= 0; y < memc->number_of_hosts; ++y)
320 if (memc->hosts[y].fd == fds[x].fd)
321 return &memc->hosts[y];
322 }
323
324 return NULL;
325 }
326
327 static ssize_t io_flush(memcached_server_st *ptr,
328 memcached_return *error)
329 {
330 /*
331 ** We might want to purge the input buffer if we haven't consumed
332 ** any output yet... The test for the limits is the purge is inline
333 ** in the purge function to avoid duplicating the logic..
334 */
335 {
336 memcached_return rc;
337 WATCHPOINT_ASSERT(ptr->fd != -1);
338 rc= memcached_purge(ptr);
339
340 if (rc != MEMCACHED_SUCCESS && rc != MEMCACHED_STORED)
341 return -1;
342 }
343 ssize_t sent_length;
344 size_t return_length;
345 char *local_write_ptr= ptr->write_buffer;
346 size_t write_length= ptr->write_buffer_offset;
347
348 *error= MEMCACHED_SUCCESS;
349
350 WATCHPOINT_ASSERT(ptr->fd != -1);
351
352 // UDP Sanity check, make sure that we are not sending somthing too big
353 if (ptr->type == MEMCACHED_CONNECTION_UDP && write_length > MAX_UDP_DATAGRAM_LENGTH)
354 return -1;
355
356 if (ptr->write_buffer_offset == 0 || (ptr->type == MEMCACHED_CONNECTION_UDP
357 && ptr->write_buffer_offset == UDP_DATAGRAM_HEADER_LENGTH))
358 return 0;
359
360 /* Looking for memory overflows */
361 #if defined(HAVE_DEBUG)
362 if (write_length == MEMCACHED_MAX_BUFFER)
363 WATCHPOINT_ASSERT(ptr->write_buffer == local_write_ptr);
364 WATCHPOINT_ASSERT((ptr->write_buffer + MEMCACHED_MAX_BUFFER) >= (local_write_ptr + write_length));
365 #endif
366
367 return_length= 0;
368 while (write_length)
369 {
370 WATCHPOINT_ASSERT(ptr->fd != -1);
371 WATCHPOINT_ASSERT(write_length > 0);
372 sent_length= 0;
373 if (ptr->type == MEMCACHED_CONNECTION_UDP)
374 increment_udp_message_id(ptr);
375 sent_length= write(ptr->fd, local_write_ptr, write_length);
376
377 if (sent_length == -1)
378 {
379 ptr->cached_errno= errno;
380 switch (errno)
381 {
382 case ENOBUFS:
383 continue;
384 case EAGAIN:
385 {
386 memcached_return rc;
387 rc= io_wait(ptr, MEM_WRITE);
388
389 if (rc == MEMCACHED_SUCCESS || rc == MEMCACHED_TIMEOUT)
390 continue;
391
392 memcached_quit_server(ptr, 1);
393 return -1;
394 }
395 default:
396 memcached_quit_server(ptr, 1);
397 *error= MEMCACHED_ERRNO;
398 return -1;
399 }
400 }
401
402 if (ptr->type == MEMCACHED_CONNECTION_UDP && sent_length != write_length)
403 {
404 memcached_quit_server(ptr, 1);
405 return -1;
406 }
407
408 ptr->io_bytes_sent += sent_length;
409
410 local_write_ptr+= sent_length;
411 write_length-= sent_length;
412 return_length+= sent_length;
413 }
414
415 WATCHPOINT_ASSERT(write_length == 0);
416 // Need to study this assert() WATCHPOINT_ASSERT(return_length ==
417 // ptr->write_buffer_offset);
418
419 // if we are a udp server, the begining of the buffer is reserverd for
420 // the upd frame header
421 if (ptr->type == MEMCACHED_CONNECTION_UDP)
422 ptr->write_buffer_offset= UDP_DATAGRAM_HEADER_LENGTH;
423 else
424 ptr->write_buffer_offset= 0;
425
426 return return_length;
427 }
428
429 /*
430 Eventually we will just kill off the server with the problem.
431 */
432 void memcached_io_reset(memcached_server_st *ptr)
433 {
434 memcached_quit_server(ptr, 1);
435 }
436
437 /**
438 * Read a given number of bytes from the server and place it into a specific
439 * buffer. Reset the IO channel on this server if an error occurs.
440 */
441 memcached_return memcached_safe_read(memcached_server_st *ptr,
442 void *dta,
443 size_t size)
444 {
445 size_t offset= 0;
446 char *data= dta;
447
448 while (offset < size)
449 {
450 ssize_t nread= memcached_io_read(ptr, data + offset, size - offset);
451 if (nread <= 0)
452 {
453 memcached_io_reset(ptr);
454 return MEMCACHED_UNKNOWN_READ_FAILURE;
455 }
456 offset+= nread;
457 }
458
459 return MEMCACHED_SUCCESS;
460 }
461
462 memcached_return memcached_io_readline(memcached_server_st *ptr,
463 char *buffer_ptr,
464 size_t size)
465 {
466 bool line_complete= false;
467 int total_nr= 0;
468
469 while (!line_complete)
470 {
471 if (ptr->read_buffer_length == 0)
472 {
473 /*
474 * We don't have any data in the buffer, so let's fill the read
475 * buffer. Call the standard read function to avoid duplicating
476 * the logic.
477 */
478 if (memcached_io_read(ptr, buffer_ptr, 1) != 1)
479 return MEMCACHED_UNKNOWN_READ_FAILURE;
480
481 if (*buffer_ptr == '\n')
482 line_complete= true;
483
484 ++buffer_ptr;
485 ++total_nr;
486 }
487
488 /* Now let's look in the buffer and copy as we go! */
489 while (ptr->read_buffer_length && total_nr < size && !line_complete)
490 {
491 *buffer_ptr = *ptr->read_ptr;
492 if (*buffer_ptr == '\n')
493 line_complete = true;
494 --ptr->read_buffer_length;
495 ++ptr->read_ptr;
496 ++total_nr;
497 ++buffer_ptr;
498 }
499
500 if (total_nr == size)
501 return MEMCACHED_PROTOCOL_ERROR;
502 }
503
504 return MEMCACHED_SUCCESS;
505 }
506
507 /*
508 * The udp request id consists of two seperate sections
509 * 1) The thread id
510 * 2) The message number
511 * The thread id should only be set when the memcached_st struct is created
512 * and should not be changed.
513 *
514 * The message num is incremented for each new message we send, this function
515 * extracts the message number from message_id, increments it and then
516 * writes the new value back into the header
517 */
518 static void increment_udp_message_id(memcached_server_st *ptr)
519 {
520 struct udp_datagram_header_st *header= (struct udp_datagram_header_st *)ptr->write_buffer;
521 uint16_t cur_req= get_udp_datagram_request_id(header);
522 uint16_t msg_num= get_msg_num_from_request_id(cur_req);
523 uint16_t thread_id= get_thread_id_from_request_id(cur_req);
524
525 if (((++msg_num) & UDP_REQUEST_ID_THREAD_MASK) != 0)
526 msg_num= 0;
527
528 header->request_id= htons(thread_id | msg_num);
529 }
530
531 memcached_return memcached_io_init_udp_header(memcached_server_st *ptr, uint16_t thread_id)
532 {
533 if (thread_id > UDP_REQUEST_ID_MAX_THREAD_ID)
534 return MEMCACHED_FAILURE;
535
536 struct udp_datagram_header_st *header= (struct udp_datagram_header_st *)ptr->write_buffer;
537 header->request_id= htons(generate_udp_request_thread_id(thread_id));
538 header->num_datagrams= htons(1);
539 header->sequence_number= htons(0);
540
541 return MEMCACHED_SUCCESS;
542 }