bf93dff1bf66d481d6a219b2c0f9b7bccbf6965a
[awesomized/libmemcached] / libmemcached / connect.c
1 /* LibMemcached
2 * Copyright (C) 2006-2010 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 #include "common.h"
13 #include <netdb.h>
14 #include <poll.h>
15 #include <sys/time.h>
16 #include <time.h>
17
18 static memcached_return_t connect_poll(memcached_server_st *ptr)
19 {
20 struct pollfd fds[1];
21 fds[0].fd = ptr->fd;
22 fds[0].events = POLLOUT;
23
24 int timeout= ptr->root->connect_timeout;
25 if (ptr->root->flags.no_block == true)
26 timeout= -1;
27
28 int error;
29 size_t loop_max= 5;
30
31 while (--loop_max) // Should only loop on cases of ERESTART or EINTR
32 {
33 error= poll(fds, 1, timeout);
34
35 switch (error)
36 {
37 case 1:
38 {
39 int err;
40 socklen_t len= sizeof (err);
41 (void)getsockopt(ptr->fd, SOL_SOCKET, SO_ERROR, &err, &len);
42
43 // We check the value to see what happened wth the socket.
44 if (err == 0)
45 {
46 return MEMCACHED_SUCCESS;
47 }
48 else
49 {
50 ptr->cached_errno= errno;
51
52 return MEMCACHED_ERRNO;
53 }
54 }
55 case 0:
56 return MEMCACHED_TIMEOUT;
57 default: // A real error occurred and we need to completely bail
58 WATCHPOINT_ERRNO(errno);
59 switch (errno)
60 {
61 #ifdef TARGET_OS_LINUX
62 case ERESTART:
63 #endif
64 case EINTR:
65 continue;
66 default:
67 if (fds[0].revents & POLLERR)
68 {
69 int err;
70 socklen_t len= sizeof (err);
71 (void)getsockopt(ptr->fd, SOL_SOCKET, SO_ERROR, &err, &len);
72 ptr->cached_errno= (err == 0) ? errno : err;
73 }
74 else
75 {
76 ptr->cached_errno= errno;
77 }
78
79 (void)close(ptr->fd);
80 ptr->fd= -1;
81
82 return MEMCACHED_ERRNO;
83 }
84 }
85 WATCHPOINT_ASSERT(0); // Programming error
86 }
87
88 // This should only be possible from ERESTART or EINTR;
89 ptr->cached_errno= errno;
90
91 return MEMCACHED_ERRNO;
92 }
93
94 static memcached_return_t set_hostinfo(memcached_server_st *server)
95 {
96 struct addrinfo *ai;
97 struct addrinfo hints;
98 char str_port[NI_MAXSERV];
99 uint32_t counter= 5;
100
101 snprintf(str_port, NI_MAXSERV, "%u", (uint32_t)server->port);
102
103 memset(&hints, 0, sizeof(hints));
104
105 // hints.ai_family= AF_INET;
106 if (server->type == MEMCACHED_CONNECTION_UDP)
107 {
108 hints.ai_protocol= IPPROTO_UDP;
109 hints.ai_socktype= SOCK_DGRAM;
110 }
111 else
112 {
113 hints.ai_socktype= SOCK_STREAM;
114 hints.ai_protocol= IPPROTO_TCP;
115 }
116
117 while (--counter)
118 {
119 int e= getaddrinfo(server->hostname, str_port, &hints, &ai);
120
121 if (e == 0)
122 {
123 break;
124 }
125 else if (e == EAI_AGAIN)
126 {
127 struct timespec dream, rem;
128
129 dream.tv_nsec= 1000;
130 dream.tv_sec= 0;
131
132 nanosleep(&dream, &rem);
133
134 continue;
135 }
136 else
137 {
138 WATCHPOINT_STRING(server->hostname);
139 WATCHPOINT_STRING(gai_strerror(e));
140 return MEMCACHED_HOST_LOOKUP_FAILURE;
141 }
142 }
143
144 if (server->address_info)
145 {
146 freeaddrinfo(server->address_info);
147 server->address_info= NULL;
148 }
149 server->address_info= ai;
150
151 return MEMCACHED_SUCCESS;
152 }
153
154 static memcached_return_t set_socket_options(memcached_server_st *ptr)
155 {
156 WATCHPOINT_ASSERT(ptr->fd != -1);
157
158 if (ptr->type == MEMCACHED_CONNECTION_UDP)
159 return MEMCACHED_SUCCESS;
160
161 #ifdef HAVE_SNDTIMEO
162 if (ptr->root->snd_timeout)
163 {
164 int error;
165 struct timeval waittime;
166
167 waittime.tv_sec= 0;
168 waittime.tv_usec= ptr->root->snd_timeout;
169
170 error= setsockopt(ptr->fd, SOL_SOCKET, SO_SNDTIMEO,
171 &waittime, (socklen_t)sizeof(struct timeval));
172 WATCHPOINT_ASSERT(error == 0);
173 if (error)
174 return MEMCACHED_FAILURE;
175 }
176 #endif
177
178 #ifdef HAVE_RCVTIMEO
179 if (ptr->root->rcv_timeout)
180 {
181 int error;
182 struct timeval waittime;
183
184 waittime.tv_sec= 0;
185 waittime.tv_usec= ptr->root->rcv_timeout;
186
187 error= setsockopt(ptr->fd, SOL_SOCKET, SO_RCVTIMEO,
188 &waittime, (socklen_t)sizeof(struct timeval));
189 WATCHPOINT_ASSERT(error == 0);
190 if (error)
191 return MEMCACHED_FAILURE;
192 }
193 #endif
194
195
196 #if defined(__MACH__) && defined(__APPLE__) || defined(__FreeBSD__)
197 {
198 int set = 1;
199 int error= setsockopt(ptr->fd, SOL_SOCKET, SO_NOSIGPIPE, (void *)&set, sizeof(int));
200
201 // This is not considered a fatal error
202 if (error == -1)
203 {
204 WATCHPOINT_ERRNO(errno);
205 perror("setsockopt(SO_NOSIGPIPE)");
206 }
207 }
208 #endif
209
210 if (ptr->root->flags.no_block)
211 {
212 int error;
213 struct linger linger;
214
215 linger.l_onoff= 1;
216 linger.l_linger= 0; /* By default on close() just drop the socket */
217 error= setsockopt(ptr->fd, SOL_SOCKET, SO_LINGER,
218 &linger, (socklen_t)sizeof(struct linger));
219 WATCHPOINT_ASSERT(error == 0);
220 if (error)
221 return MEMCACHED_FAILURE;
222 }
223
224 if (ptr->root->flags.tcp_nodelay)
225 {
226 int flag= 1;
227 int error;
228
229 error= setsockopt(ptr->fd, IPPROTO_TCP, TCP_NODELAY,
230 &flag, (socklen_t)sizeof(int));
231 WATCHPOINT_ASSERT(error == 0);
232 if (error)
233 return MEMCACHED_FAILURE;
234 }
235
236 if (ptr->root->flags.tcp_keepalive)
237 {
238 int flag= 1;
239 int error;
240
241 error= setsockopt(ptr->fd, SOL_SOCKET, SO_KEEPALIVE,
242 &flag, (socklen_t)sizeof(int));
243 WATCHPOINT_ASSERT(error == 0);
244 if (error)
245 return MEMCACHED_FAILURE;
246 }
247
248 #ifdef TCP_KEEPIDLE
249 if (ptr->root->tcp_keepidle > 0)
250 {
251 int error;
252
253 error= setsockopt(ptr->fd, IPPROTO_TCP, TCP_KEEPIDLE,
254 &ptr->root->tcp_keepidle, (socklen_t)sizeof(int));
255 WATCHPOINT_ASSERT(error == 0);
256 if (error)
257 return MEMCACHED_FAILURE;
258 }
259 #endif
260
261 if (ptr->root->send_size > 0)
262 {
263 int error;
264
265 error= setsockopt(ptr->fd, SOL_SOCKET, SO_SNDBUF,
266 &ptr->root->send_size, (socklen_t)sizeof(int));
267 WATCHPOINT_ASSERT(error == 0);
268 if (error)
269 return MEMCACHED_FAILURE;
270 }
271
272 if (ptr->root->recv_size > 0)
273 {
274 int error;
275
276 error= setsockopt(ptr->fd, SOL_SOCKET, SO_RCVBUF,
277 &ptr->root->recv_size, (socklen_t)sizeof(int));
278 WATCHPOINT_ASSERT(error == 0);
279 if (error)
280 return MEMCACHED_FAILURE;
281 }
282
283 /* libmemcached will always use nonblocking IO to avoid write deadlocks */
284 int flags;
285
286 do
287 flags= fcntl(ptr->fd, F_GETFL, 0);
288 while (flags == -1 && (errno == EINTR || errno == EAGAIN));
289
290 unlikely (flags == -1)
291 {
292 return MEMCACHED_CONNECTION_FAILURE;
293 }
294 else if ((flags & O_NONBLOCK) == 0)
295 {
296 int rval;
297
298 do
299 rval= fcntl(ptr->fd, F_SETFL, flags | O_NONBLOCK);
300 while (rval == -1 && (errno == EINTR || errno == EAGAIN));
301
302 unlikely (rval == -1)
303 {
304 return MEMCACHED_CONNECTION_FAILURE;
305 }
306 }
307
308 return MEMCACHED_SUCCESS;
309 }
310
311 static memcached_return_t unix_socket_connect(memcached_server_st *ptr)
312 {
313 struct sockaddr_un servAddr;
314
315 if (ptr->fd == -1)
316 {
317 if ((ptr->fd= socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
318 {
319 ptr->cached_errno= errno;
320 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE;
321 }
322
323 memset(&servAddr, 0, sizeof (struct sockaddr_un));
324 servAddr.sun_family= AF_UNIX;
325 strcpy(servAddr.sun_path, ptr->hostname); /* Copy filename */
326
327 test_connect:
328 if (connect(ptr->fd,
329 (struct sockaddr *)&servAddr,
330 sizeof(servAddr)) < 0)
331 {
332 switch (errno)
333 {
334 case EINPROGRESS:
335 case EALREADY:
336 case EINTR:
337 goto test_connect;
338 case EISCONN: /* We were spinning waiting on connect */
339 break;
340 default:
341 WATCHPOINT_ERRNO(errno);
342 ptr->cached_errno= errno;
343 return MEMCACHED_ERRNO;
344 }
345 }
346 }
347
348 WATCHPOINT_ASSERT(ptr->fd != -1);
349
350 return MEMCACHED_SUCCESS;
351 }
352
353 static memcached_return_t network_connect(memcached_server_st *ptr)
354 {
355 bool timeout_error_occured= false;
356
357 if (ptr->fd == -1)
358 {
359 struct addrinfo *use;
360
361 WATCHPOINT_ASSERT(ptr->cursor_active == 0);
362
363 if (! ptr->options.sockaddr_inited ||
364 (!(ptr->root->flags.use_cache_lookups)))
365 {
366 memcached_return_t rc;
367
368 rc= set_hostinfo(ptr);
369 if (rc != MEMCACHED_SUCCESS)
370 return rc;
371 ptr->options.sockaddr_inited= true;
372 }
373
374 use= ptr->address_info;
375 /* Create the socket */
376 while (use != NULL)
377 {
378 /* Memcache server does not support IPV6 in udp mode, so skip if not ipv4 */
379 if (ptr->type == MEMCACHED_CONNECTION_UDP && use->ai_family != AF_INET)
380 {
381 use= use->ai_next;
382 continue;
383 }
384
385 if ((ptr->fd= socket(use->ai_family,
386 use->ai_socktype,
387 use->ai_protocol)) < 0)
388 {
389 ptr->cached_errno= errno;
390 WATCHPOINT_ERRNO(errno);
391 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE;
392 }
393
394 (void)set_socket_options(ptr);
395
396 /* connect to server */
397 if ((connect(ptr->fd, use->ai_addr, use->ai_addrlen) > -1))
398 {
399 break; // Success
400 }
401
402 /* An error occurred */
403 ptr->cached_errno= errno;
404 if (errno == EINPROGRESS || /* nonblocking mode - first return, */
405 errno == EALREADY) /* nonblocking mode - subsequent returns */
406 {
407 memcached_return_t rc;
408 rc= connect_poll(ptr);
409
410 if (rc == MEMCACHED_TIMEOUT)
411 timeout_error_occured= true;
412
413 if (rc == MEMCACHED_SUCCESS)
414 break;
415 }
416 else if (errno == EISCONN) /* we are connected :-) */
417 {
418 break;
419 }
420 else if (errno == EINTR) // Special case, we retry ai_addr
421 {
422 (void)close(ptr->fd);
423 ptr->fd= -1;
424 continue;
425 }
426
427 (void)close(ptr->fd);
428 ptr->fd= -1;
429 use= use->ai_next;
430 }
431 }
432
433 if (ptr->fd == -1)
434 {
435 WATCHPOINT_STRING("Never got a good file descriptor");
436
437 /* Failed to connect. schedule next retry */
438 if (ptr->root->retry_timeout)
439 {
440 struct timeval next_time;
441
442 if (gettimeofday(&next_time, NULL) == 0)
443 ptr->next_retry= next_time.tv_sec + ptr->root->retry_timeout;
444 }
445
446 if (timeout_error_occured)
447 return MEMCACHED_TIMEOUT;
448
449 return MEMCACHED_ERRNO; /* The last error should be from connect() */
450 }
451
452 return MEMCACHED_SUCCESS; /* The last error should be from connect() */
453 }
454
455 void set_last_disconnected_host(memcached_server_write_instance_st ptr)
456 {
457 // const_cast
458 memcached_st *root= (memcached_st *)ptr->root;
459
460 #if 0
461 WATCHPOINT_STRING(ptr->hostname);
462 WATCHPOINT_NUMBER(ptr->port);
463 WATCHPOINT_ERRNO(ptr->cached_errno);
464 #endif
465 if (root->last_disconnected_server)
466 memcached_server_free(root->last_disconnected_server);
467 root->last_disconnected_server= memcached_server_clone(NULL, ptr);
468 }
469
470 memcached_return_t memcached_connect(memcached_server_write_instance_st ptr)
471 {
472 memcached_return_t rc= MEMCACHED_NO_SERVERS;
473
474 if (ptr->fd > -1)
475 return MEMCACHED_SUCCESS;
476
477 LIBMEMCACHED_MEMCACHED_CONNECT_START();
478
479 /* both retry_timeout and server_failure_limit must be set in order to delay retrying a server on error. */
480 WATCHPOINT_ASSERT(ptr->root);
481 if (ptr->root->retry_timeout && ptr->next_retry)
482 {
483 struct timeval curr_time;
484
485 gettimeofday(&curr_time, NULL);
486
487 // We should optimize this to remove the allocation if the server was
488 // the last server to die
489 if (ptr->next_retry > curr_time.tv_sec)
490 {
491 set_last_disconnected_host(ptr);
492
493 return MEMCACHED_SERVER_MARKED_DEAD;
494 }
495 }
496
497 // If we are over the counter failure, we just fail. Reject host only
498 // works if you have a set number of failures.
499 if (ptr->root->server_failure_limit && ptr->server_failure_counter >= ptr->root->server_failure_limit)
500 {
501 set_last_disconnected_host(ptr);
502
503 // @todo fix this by fixing behavior to no longer make use of
504 // memcached_st
505 if (_is_auto_eject_host(ptr->root))
506 {
507 run_distribution((memcached_st *)ptr->root);
508 }
509
510 return MEMCACHED_SERVER_MARKED_DEAD;
511 }
512
513 /* We need to clean up the multi startup piece */
514 switch (ptr->type)
515 {
516 case MEMCACHED_CONNECTION_UNKNOWN:
517 WATCHPOINT_ASSERT(0);
518 rc= MEMCACHED_NOT_SUPPORTED;
519 break;
520 case MEMCACHED_CONNECTION_UDP:
521 case MEMCACHED_CONNECTION_TCP:
522 rc= network_connect(ptr);
523 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
524 if (ptr->fd != -1 && ptr->root->sasl && ptr->root->sasl->callbacks)
525 {
526 rc= memcached_sasl_authenticate_connection(ptr);
527 if (rc != MEMCACHED_SUCCESS)
528 {
529 (void)close(ptr->fd);
530 ptr->fd= -1;
531 }
532 }
533 #endif
534 break;
535 case MEMCACHED_CONNECTION_UNIX_SOCKET:
536 rc= unix_socket_connect(ptr);
537 break;
538 case MEMCACHED_CONNECTION_MAX:
539 default:
540 WATCHPOINT_ASSERT(0);
541 }
542
543 if (rc == MEMCACHED_SUCCESS)
544 {
545 ptr->server_failure_counter= 0;
546 ptr->next_retry= 0;
547 }
548 else
549 {
550 ptr->server_failure_counter++;
551
552 set_last_disconnected_host(ptr);
553 }
554
555 LIBMEMCACHED_MEMCACHED_CONNECT_END();
556
557 return rc;
558 }