Silently ignore the new stats fields
[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 static ssize_t io_flush(memcached_server_st *ptr,
280 memcached_return *error)
281 {
282 /*
283 ** We might want to purge the input buffer if we haven't consumed
284 ** any output yet... The test for the limits is the purge is inline
285 ** in the purge function to avoid duplicating the logic..
286 */
287 {
288 memcached_return rc;
289 WATCHPOINT_ASSERT(ptr->fd != -1);
290 rc= memcached_purge(ptr);
291
292 if (rc != MEMCACHED_SUCCESS && rc != MEMCACHED_STORED)
293 return -1;
294 }
295 ssize_t sent_length;
296 size_t return_length;
297 char *local_write_ptr= ptr->write_buffer;
298 size_t write_length= ptr->write_buffer_offset;
299
300 *error= MEMCACHED_SUCCESS;
301
302 WATCHPOINT_ASSERT(ptr->fd != -1);
303
304 // UDP Sanity check, make sure that we are not sending somthing too big
305 if (ptr->type == MEMCACHED_CONNECTION_UDP && write_length > MAX_UDP_DATAGRAM_LENGTH)
306 return -1;
307
308 if (ptr->write_buffer_offset == 0 || (ptr->type == MEMCACHED_CONNECTION_UDP
309 && ptr->write_buffer_offset == UDP_DATAGRAM_HEADER_LENGTH))
310 return 0;
311
312 /* Looking for memory overflows */
313 #if defined(HAVE_DEBUG)
314 if (write_length == MEMCACHED_MAX_BUFFER)
315 WATCHPOINT_ASSERT(ptr->write_buffer == local_write_ptr);
316 WATCHPOINT_ASSERT((ptr->write_buffer + MEMCACHED_MAX_BUFFER) >= (local_write_ptr + write_length));
317 #endif
318
319 return_length= 0;
320 while (write_length)
321 {
322 WATCHPOINT_ASSERT(ptr->fd != -1);
323 WATCHPOINT_ASSERT(write_length > 0);
324 sent_length= 0;
325 if (ptr->type == MEMCACHED_CONNECTION_UDP)
326 increment_udp_message_id(ptr);
327 sent_length= write(ptr->fd, local_write_ptr, write_length);
328
329 if (sent_length == -1)
330 {
331 ptr->cached_errno= errno;
332 switch (errno)
333 {
334 case ENOBUFS:
335 continue;
336 case EAGAIN:
337 {
338 memcached_return rc;
339 rc= io_wait(ptr, MEM_WRITE);
340
341 if (rc == MEMCACHED_SUCCESS || rc == MEMCACHED_TIMEOUT)
342 continue;
343
344 memcached_quit_server(ptr, 1);
345 return -1;
346 }
347 default:
348 memcached_quit_server(ptr, 1);
349 *error= MEMCACHED_ERRNO;
350 return -1;
351 }
352 }
353
354 if (ptr->type == MEMCACHED_CONNECTION_UDP && sent_length != write_length)
355 {
356 memcached_quit_server(ptr, 1);
357 return -1;
358 }
359
360 ptr->io_bytes_sent += sent_length;
361
362 local_write_ptr+= sent_length;
363 write_length-= sent_length;
364 return_length+= sent_length;
365 }
366
367 WATCHPOINT_ASSERT(write_length == 0);
368 // Need to study this assert() WATCHPOINT_ASSERT(return_length ==
369 // ptr->write_buffer_offset);
370
371 // if we are a udp server, the begining of the buffer is reserverd for
372 // the upd frame header
373 if (ptr->type == MEMCACHED_CONNECTION_UDP)
374 ptr->write_buffer_offset= UDP_DATAGRAM_HEADER_LENGTH;
375 else
376 ptr->write_buffer_offset= 0;
377
378 return return_length;
379 }
380
381 /*
382 Eventually we will just kill off the server with the problem.
383 */
384 void memcached_io_reset(memcached_server_st *ptr)
385 {
386 memcached_quit_server(ptr, 1);
387 }
388
389 /**
390 * Read a given number of bytes from the server and place it into a specific
391 * buffer. Reset the IO channel on this server if an error occurs.
392 */
393 memcached_return memcached_safe_read(memcached_server_st *ptr,
394 void *dta,
395 size_t size)
396 {
397 size_t offset= 0;
398 char *data= dta;
399
400 while (offset < size)
401 {
402 ssize_t nread= memcached_io_read(ptr, data + offset, size - offset);
403 if (nread <= 0)
404 {
405 memcached_io_reset(ptr);
406 return MEMCACHED_UNKNOWN_READ_FAILURE;
407 }
408 offset+= nread;
409 }
410
411 return MEMCACHED_SUCCESS;
412 }
413
414 memcached_return memcached_io_readline(memcached_server_st *ptr,
415 char *buffer_ptr,
416 size_t size)
417 {
418 bool line_complete= false;
419 int total_nr= 0;
420
421 while (!line_complete)
422 {
423 if (ptr->read_buffer_length == 0)
424 {
425 /*
426 * We don't have any data in the buffer, so let's fill the read
427 * buffer. Call the standard read function to avoid duplicating
428 * the logic.
429 */
430 if (memcached_io_read(ptr, buffer_ptr, 1) != 1)
431 return MEMCACHED_UNKNOWN_READ_FAILURE;
432
433 if (*buffer_ptr == '\n')
434 line_complete= true;
435
436 ++buffer_ptr;
437 ++total_nr;
438 }
439
440 /* Now let's look in the buffer and copy as we go! */
441 while (ptr->read_buffer_length && total_nr < size && !line_complete)
442 {
443 *buffer_ptr = *ptr->read_ptr;
444 if (*buffer_ptr == '\n')
445 line_complete = true;
446 --ptr->read_buffer_length;
447 ++ptr->read_ptr;
448 ++total_nr;
449 ++buffer_ptr;
450 }
451
452 if (total_nr == size)
453 return MEMCACHED_PROTOCOL_ERROR;
454 }
455
456 return MEMCACHED_SUCCESS;
457 }
458
459 /*
460 * The udp request id consists of two seperate sections
461 * 1) The thread id
462 * 2) The message number
463 * The thread id should only be set when the memcached_st struct is created
464 * and should not be changed.
465 *
466 * The message num is incremented for each new message we send, this function
467 * extracts the message number from message_id, increments it and then
468 * writes the new value back into the header
469 */
470 static void increment_udp_message_id(memcached_server_st *ptr)
471 {
472 struct udp_datagram_header_st *header= (struct udp_datagram_header_st *)ptr->write_buffer;
473 uint16_t cur_req= get_udp_datagram_request_id(header);
474 uint16_t msg_num= get_msg_num_from_request_id(cur_req);
475 uint16_t thread_id= get_thread_id_from_request_id(cur_req);
476
477 if (((++msg_num) & UDP_REQUEST_ID_THREAD_MASK) != 0)
478 msg_num= 0;
479
480 header->request_id= htons(thread_id | msg_num);
481 }
482
483 memcached_return memcached_io_init_udp_header(memcached_server_st *ptr, uint16_t thread_id)
484 {
485 if (thread_id > UDP_REQUEST_ID_MAX_THREAD_ID)
486 return MEMCACHED_FAILURE;
487
488 struct udp_datagram_header_st *header= (struct udp_datagram_header_st *)ptr->write_buffer;
489 header->request_id= htons(generate_udp_request_thread_id(thread_id));
490 header->num_datagrams= htons(1);
491 header->sequence_number= htons(0);
492
493 return MEMCACHED_SUCCESS;
494 }