Update for change in fd.
[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 WATCHPOINT_ASSERT(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 WATCHPOINT_ASSERT(ptr->fd != -1);
348
349 return MEMCACHED_SUCCESS;
350 }
351
352 static memcached_return_t network_connect(memcached_server_st *ptr)
353 {
354 bool timeout_error_occured= false;
355
356
357 WATCHPOINT_ASSERT(ptr->fd == -1);
358 WATCHPOINT_ASSERT(ptr->cursor_active == 0);
359
360 if (! ptr->options.sockaddr_inited || (!(ptr->root->flags.use_cache_lookups)))
361 {
362 memcached_return_t rc;
363
364 rc= set_hostinfo(ptr);
365 if (rc != MEMCACHED_SUCCESS)
366 return rc;
367 ptr->options.sockaddr_inited= true;
368 }
369
370 struct addrinfo *use= ptr->address_info;
371 /* Create the socket */
372 while (use != NULL)
373 {
374 /* Memcache server does not support IPV6 in udp mode, so skip if not ipv4 */
375 if (ptr->type == MEMCACHED_CONNECTION_UDP && use->ai_family != AF_INET)
376 {
377 use= use->ai_next;
378 continue;
379 }
380
381 if ((ptr->fd= socket(use->ai_family,
382 use->ai_socktype,
383 use->ai_protocol)) < 0)
384 {
385 ptr->cached_errno= errno;
386 WATCHPOINT_ERRNO(errno);
387 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE;
388 }
389
390 (void)set_socket_options(ptr);
391
392 /* connect to server */
393 if ((connect(ptr->fd, use->ai_addr, use->ai_addrlen) > -1))
394 {
395 break; // Success
396 }
397
398 /* An error occurred */
399 ptr->cached_errno= errno;
400 if (errno == EINPROGRESS || /* nonblocking mode - first return, */
401 errno == EALREADY) /* nonblocking mode - subsequent returns */
402 {
403 memcached_return_t rc;
404 rc= connect_poll(ptr);
405
406 if (rc == MEMCACHED_TIMEOUT)
407 timeout_error_occured= true;
408
409 if (rc == MEMCACHED_SUCCESS)
410 break;
411 }
412 else if (errno == EISCONN) /* we are connected :-) */
413 {
414 break;
415 }
416 else if (errno == EINTR) // Special case, we retry ai_addr
417 {
418 (void)close(ptr->fd);
419 ptr->fd= -1;
420 continue;
421 }
422
423 (void)close(ptr->fd);
424 ptr->fd= -1;
425 use= use->ai_next;
426 }
427
428 if (ptr->fd == -1)
429 {
430 WATCHPOINT_STRING("Never got a good file descriptor");
431
432 /* Failed to connect. schedule next retry */
433 if (ptr->root->retry_timeout)
434 {
435 struct timeval next_time;
436
437 if (gettimeofday(&next_time, NULL) == 0)
438 ptr->next_retry= next_time.tv_sec + ptr->root->retry_timeout;
439 }
440
441 if (timeout_error_occured)
442 return MEMCACHED_TIMEOUT;
443
444 return MEMCACHED_ERRNO; /* The last error should be from connect() */
445 }
446
447 return MEMCACHED_SUCCESS; /* The last error should be from connect() */
448 }
449
450 void set_last_disconnected_host(memcached_server_write_instance_st ptr)
451 {
452 // const_cast
453 memcached_st *root= (memcached_st *)ptr->root;
454
455 #if 0
456 WATCHPOINT_STRING(ptr->hostname);
457 WATCHPOINT_NUMBER(ptr->port);
458 WATCHPOINT_ERRNO(ptr->cached_errno);
459 #endif
460 if (root->last_disconnected_server)
461 memcached_server_free(root->last_disconnected_server);
462 root->last_disconnected_server= memcached_server_clone(NULL, ptr);
463 }
464
465 memcached_return_t memcached_connect(memcached_server_write_instance_st ptr)
466 {
467 memcached_return_t rc= MEMCACHED_NO_SERVERS;
468
469 if (ptr->fd > -1)
470 return MEMCACHED_SUCCESS;
471
472 LIBMEMCACHED_MEMCACHED_CONNECT_START();
473
474 /* both retry_timeout and server_failure_limit must be set in order to delay retrying a server on error. */
475 WATCHPOINT_ASSERT(ptr->root);
476 if (ptr->root->retry_timeout && ptr->next_retry)
477 {
478 struct timeval curr_time;
479
480 gettimeofday(&curr_time, NULL);
481
482 // We should optimize this to remove the allocation if the server was
483 // the last server to die
484 if (ptr->next_retry > curr_time.tv_sec)
485 {
486 set_last_disconnected_host(ptr);
487
488 return MEMCACHED_SERVER_MARKED_DEAD;
489 }
490 }
491
492 // If we are over the counter failure, we just fail. Reject host only
493 // works if you have a set number of failures.
494 if (ptr->root->server_failure_limit && ptr->server_failure_counter >= ptr->root->server_failure_limit)
495 {
496 set_last_disconnected_host(ptr);
497
498 // @todo fix this by fixing behavior to no longer make use of
499 // memcached_st
500 if (_is_auto_eject_host(ptr->root))
501 {
502 run_distribution((memcached_st *)ptr->root);
503 }
504
505 return MEMCACHED_SERVER_MARKED_DEAD;
506 }
507
508 /* We need to clean up the multi startup piece */
509 switch (ptr->type)
510 {
511 case MEMCACHED_CONNECTION_UNKNOWN:
512 WATCHPOINT_ASSERT(0);
513 rc= MEMCACHED_NOT_SUPPORTED;
514 break;
515 case MEMCACHED_CONNECTION_UDP:
516 case MEMCACHED_CONNECTION_TCP:
517 rc= network_connect(ptr);
518 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
519 if (ptr->fd != -1 && ptr->root->sasl && ptr->root->sasl->callbacks)
520 {
521 rc= memcached_sasl_authenticate_connection(ptr);
522 if (rc != MEMCACHED_SUCCESS)
523 {
524 (void)close(ptr->fd);
525 ptr->fd= -1;
526 }
527 }
528 #endif
529 break;
530 case MEMCACHED_CONNECTION_UNIX_SOCKET:
531 rc= unix_socket_connect(ptr);
532 break;
533 case MEMCACHED_CONNECTION_MAX:
534 default:
535 WATCHPOINT_ASSERT(0);
536 }
537
538 if (rc == MEMCACHED_SUCCESS)
539 {
540 ptr->server_failure_counter= 0;
541 ptr->next_retry= 0;
542 }
543 else
544 {
545 ptr->server_failure_counter++;
546
547 set_last_disconnected_host(ptr);
548 }
549
550 LIBMEMCACHED_MEMCACHED_CONNECT_END();
551
552 return rc;
553 }