2 * Copyright (C) 2006-2010 Brian Aker
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
8 * Summary: Server IO, Not public!
16 static memcached_return_t
connect_poll(memcached_server_st
*ptr
)
20 fds
[0].events
= POLLOUT
;
25 while (--loop_max
) // Should only loop on cases of ERESTART or EINTR
27 error
= poll(fds
, 1, ptr
->root
->connect_timeout
);
34 socklen_t len
= sizeof (err
);
35 (void)getsockopt(ptr
->fd
, SOL_SOCKET
, SO_ERROR
, &err
, &len
);
37 // We check the value to see what happened wth the socket.
40 return MEMCACHED_SUCCESS
;
44 ptr
->cached_errno
= errno
;
46 return MEMCACHED_ERRNO
;
50 return MEMCACHED_TIMEOUT
;
51 default: // A real error occurred and we need to completely bail
52 WATCHPOINT_ERRNO(get_socket_errno());
53 switch (get_socket_errno())
55 #ifdef TARGET_OS_LINUX
61 if (fds
[0].revents
& POLLERR
)
64 socklen_t len
= sizeof (err
);
65 (void)getsockopt(ptr
->fd
, SOL_SOCKET
, SO_ERROR
, &err
, &len
);
66 ptr
->cached_errno
= (err
== 0) ? get_socket_errno() : err
;
70 ptr
->cached_errno
= get_socket_errno();
73 (void)closesocket(ptr
->fd
);
74 ptr
->fd
= INVALID_SOCKET
;
76 return MEMCACHED_ERRNO
;
81 // This should only be possible from ERESTART or EINTR;
82 ptr
->cached_errno
= get_socket_errno();
84 return MEMCACHED_ERRNO
;
87 static memcached_return_t
set_hostinfo(memcached_server_st
*server
)
89 struct addrinfo hints
;
90 char str_port
[NI_MAXSERV
];
92 if (server
->address_info
)
94 freeaddrinfo(server
->address_info
);
95 server
->address_info
= NULL
;
96 server
->address_info_next
= NULL
;
99 int length
= snprintf(str_port
, NI_MAXSERV
, "%u", (uint32_t)server
->port
);
100 if (length
>= NI_MAXSERV
|| length
< 0)
101 return MEMCACHED_FAILURE
;
103 memset(&hints
, 0, sizeof(hints
));
106 hints
.ai_family
= AF_INET
;
108 if (server
->type
== MEMCACHED_CONNECTION_UDP
)
110 hints
.ai_protocol
= IPPROTO_UDP
;
111 hints
.ai_socktype
= SOCK_DGRAM
;
115 hints
.ai_socktype
= SOCK_STREAM
;
116 hints
.ai_protocol
= IPPROTO_TCP
;
122 int e
= getaddrinfo(server
->hostname
, str_port
, &hints
, &server
->address_info
);
128 else if (e
== EAI_AGAIN
)
131 struct timespec dream
, rem
;
136 nanosleep(&dream
, &rem
);
142 WATCHPOINT_STRING(server
->hostname
);
143 WATCHPOINT_STRING(gai_strerror(e
));
144 return MEMCACHED_HOST_LOOKUP_FAILURE
;
148 server
->address_info_next
= server
->address_info
;
150 return MEMCACHED_SUCCESS
;
153 static inline memcached_return_t
set_socket_nonblocking(memcached_server_st
*ptr
)
157 if (ioctlsocket(ptr
->fd
, FIONBIO
, &arg
) == SOCKET_ERROR
)
159 ptr
->cached_errno
= get_socket_errno();
160 return MEMCACHED_CONNECTION_FAILURE
;
166 flags
= fcntl(ptr
->fd
, F_GETFL
, 0);
167 while (flags
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
169 unlikely (flags
== -1)
171 ptr
->cached_errno
= errno
;
172 return MEMCACHED_CONNECTION_FAILURE
;
174 else if ((flags
& O_NONBLOCK
) == 0)
179 rval
= fcntl(ptr
->fd
, F_SETFL
, flags
| O_NONBLOCK
);
180 while (rval
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
182 unlikely (rval
== -1)
184 ptr
->cached_errno
= errno
;
185 return MEMCACHED_CONNECTION_FAILURE
;
189 return MEMCACHED_SUCCESS
;
192 static memcached_return_t
set_socket_options(memcached_server_st
*ptr
)
194 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
196 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
)
197 return MEMCACHED_SUCCESS
;
200 if (ptr
->root
->snd_timeout
)
203 struct timeval waittime
;
206 waittime
.tv_usec
= ptr
->root
->snd_timeout
;
208 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_SNDTIMEO
,
209 &waittime
, (socklen_t
)sizeof(struct timeval
));
210 WATCHPOINT_ASSERT(error
== 0);
212 return MEMCACHED_FAILURE
;
217 if (ptr
->root
->rcv_timeout
)
220 struct timeval waittime
;
223 waittime
.tv_usec
= ptr
->root
->rcv_timeout
;
225 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_RCVTIMEO
,
226 &waittime
, (socklen_t
)sizeof(struct timeval
));
227 WATCHPOINT_ASSERT(error
== 0);
229 return MEMCACHED_FAILURE
;
234 #if defined(__MACH__) && defined(__APPLE__) || defined(__FreeBSD__)
237 int error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_NOSIGPIPE
, (void *)&set
, sizeof(int));
239 // This is not considered a fatal error
242 WATCHPOINT_ERRNO(get_socket_errno());
243 perror("setsockopt(SO_NOSIGPIPE)");
248 if (ptr
->root
->flags
.no_block
)
251 struct linger linger
;
254 linger
.l_linger
= 0; /* By default on close() just drop the socket */
255 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_LINGER
,
256 &linger
, (socklen_t
)sizeof(struct linger
));
257 WATCHPOINT_ASSERT(error
== 0);
259 return MEMCACHED_FAILURE
;
262 if (ptr
->root
->flags
.tcp_nodelay
)
267 error
= setsockopt(ptr
->fd
, IPPROTO_TCP
, TCP_NODELAY
,
268 &flag
, (socklen_t
)sizeof(int));
269 WATCHPOINT_ASSERT(error
== 0);
271 return MEMCACHED_FAILURE
;
274 if (ptr
->root
->flags
.tcp_keepalive
)
279 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_KEEPALIVE
,
280 &flag
, (socklen_t
)sizeof(int));
281 WATCHPOINT_ASSERT(error
== 0);
283 return MEMCACHED_FAILURE
;
287 if (ptr
->root
->tcp_keepidle
> 0)
291 error
= setsockopt(ptr
->fd
, IPPROTO_TCP
, TCP_KEEPIDLE
,
292 &ptr
->root
->tcp_keepidle
, (socklen_t
)sizeof(int));
293 WATCHPOINT_ASSERT(error
== 0);
295 return MEMCACHED_FAILURE
;
299 if (ptr
->root
->send_size
> 0)
303 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_SNDBUF
,
304 &ptr
->root
->send_size
, (socklen_t
)sizeof(int));
305 WATCHPOINT_ASSERT(error
== 0);
307 return MEMCACHED_FAILURE
;
310 if (ptr
->root
->recv_size
> 0)
314 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_RCVBUF
,
315 &ptr
->root
->recv_size
, (socklen_t
)sizeof(int));
316 WATCHPOINT_ASSERT(error
== 0);
318 return MEMCACHED_FAILURE
;
322 /* libmemcached will always use nonblocking IO to avoid write deadlocks */
323 return set_socket_nonblocking(ptr
);
326 static memcached_return_t
unix_socket_connect(memcached_server_st
*ptr
)
329 struct sockaddr_un servAddr
;
331 WATCHPOINT_ASSERT(ptr
->fd
== -1);
333 if ((ptr
->fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) < 0)
335 ptr
->cached_errno
= errno
;
336 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
339 memset(&servAddr
, 0, sizeof (struct sockaddr_un
));
340 servAddr
.sun_family
= AF_UNIX
;
341 strncpy(servAddr
.sun_path
, ptr
->hostname
, sizeof(servAddr
.sun_path
)); /* Copy filename */
345 (struct sockaddr
*)&servAddr
,
346 sizeof(servAddr
)) < 0)
354 case EISCONN
: /* We were spinning waiting on connect */
357 WATCHPOINT_ERRNO(errno
);
358 ptr
->cached_errno
= errno
;
359 return MEMCACHED_ERRNO
;
363 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
365 return MEMCACHED_SUCCESS
;
368 return MEMCACHED_NOT_SUPPORTED
;
372 static memcached_return_t
network_connect(memcached_server_st
*ptr
)
374 bool timeout_error_occured
= false;
376 WATCHPOINT_ASSERT(ptr
->fd
== INVALID_SOCKET
);
377 WATCHPOINT_ASSERT(ptr
->cursor_active
== 0);
379 if (! ptr
->address_info
)
381 memcached_return_t rc
= set_hostinfo(ptr
);
382 if (rc
!= MEMCACHED_SUCCESS
)
386 /* Create the socket */
387 while (ptr
->address_info_next
&& ptr
->fd
== INVALID_SOCKET
)
389 /* Memcache server does not support IPV6 in udp mode, so skip if not ipv4 */
390 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
&& ptr
->address_info_next
->ai_family
!= AF_INET
)
392 ptr
->address_info_next
= ptr
->address_info_next
->ai_next
;
396 if ((ptr
->fd
= socket(ptr
->address_info_next
->ai_family
,
397 ptr
->address_info_next
->ai_socktype
,
398 ptr
->address_info_next
->ai_protocol
)) < 0)
400 ptr
->cached_errno
= get_socket_errno();
401 WATCHPOINT_ERRNO(get_socket_errno());
402 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
405 (void)set_socket_options(ptr
);
407 /* connect to server */
408 if ((connect(ptr
->fd
, ptr
->address_info_next
->ai_addr
, ptr
->address_info_next
->ai_addrlen
) != SOCKET_ERROR
))
413 /* An error occurred */
414 ptr
->cached_errno
= get_socket_errno();
415 switch (ptr
->cached_errno
)
418 case EINPROGRESS
: // nonblocking mode - first return
419 case EALREADY
: // nonblocking mode - subsequent returns
421 memcached_return_t rc
;
422 rc
= connect_poll(ptr
);
424 if (rc
== MEMCACHED_TIMEOUT
)
425 timeout_error_occured
= true;
427 if (rc
== MEMCACHED_SUCCESS
)
431 case EISCONN
: // we are connected :-)
434 case EINTR
: // Special case, we retry ai_addr
435 (void)closesocket(ptr
->fd
);
436 ptr
->fd
= INVALID_SOCKET
;
440 (void)closesocket(ptr
->fd
);
441 ptr
->fd
= INVALID_SOCKET
;
442 ptr
->address_info_next
= ptr
->address_info_next
->ai_next
;
447 if (ptr
->fd
== INVALID_SOCKET
)
449 WATCHPOINT_STRING("Never got a good file descriptor");
451 /* Failed to connect. schedule next retry */
452 if (ptr
->root
->retry_timeout
)
454 struct timeval next_time
;
456 if (gettimeofday(&next_time
, NULL
) == 0)
457 ptr
->next_retry
= next_time
.tv_sec
+ ptr
->root
->retry_timeout
;
460 if (timeout_error_occured
)
461 return MEMCACHED_TIMEOUT
;
463 return MEMCACHED_ERRNO
; /* The last error should be from connect() */
466 return MEMCACHED_SUCCESS
; /* The last error should be from connect() */
469 void set_last_disconnected_host(memcached_server_write_instance_st ptr
)
472 memcached_st
*root
= (memcached_st
*)ptr
->root
;
475 WATCHPOINT_STRING(ptr
->hostname
);
476 WATCHPOINT_NUMBER(ptr
->port
);
477 WATCHPOINT_ERRNO(ptr
->cached_errno
);
479 if (root
->last_disconnected_server
)
480 memcached_server_free(root
->last_disconnected_server
);
481 root
->last_disconnected_server
= memcached_server_clone(NULL
, ptr
);
484 memcached_return_t
memcached_connect(memcached_server_write_instance_st ptr
)
486 memcached_return_t rc
= MEMCACHED_NO_SERVERS
;
488 if (ptr
->fd
!= INVALID_SOCKET
)
489 return MEMCACHED_SUCCESS
;
491 LIBMEMCACHED_MEMCACHED_CONNECT_START();
493 /* both retry_timeout and server_failure_limit must be set in order to delay retrying a server on error. */
494 WATCHPOINT_ASSERT(ptr
->root
);
495 if (ptr
->root
->retry_timeout
&& ptr
->next_retry
)
497 struct timeval curr_time
;
499 gettimeofday(&curr_time
, NULL
);
501 // We should optimize this to remove the allocation if the server was
502 // the last server to die
503 if (ptr
->next_retry
> curr_time
.tv_sec
)
505 set_last_disconnected_host(ptr
);
507 return MEMCACHED_SERVER_MARKED_DEAD
;
511 // If we are over the counter failure, we just fail. Reject host only
512 // works if you have a set number of failures.
513 if (ptr
->root
->server_failure_limit
&& ptr
->server_failure_counter
>= ptr
->root
->server_failure_limit
)
515 set_last_disconnected_host(ptr
);
517 // @todo fix this by fixing behavior to no longer make use of
519 if (_is_auto_eject_host(ptr
->root
))
521 run_distribution((memcached_st
*)ptr
->root
);
524 return MEMCACHED_SERVER_MARKED_DEAD
;
527 /* We need to clean up the multi startup piece */
530 case MEMCACHED_CONNECTION_UNKNOWN
:
531 WATCHPOINT_ASSERT(0);
532 rc
= MEMCACHED_NOT_SUPPORTED
;
534 case MEMCACHED_CONNECTION_UDP
:
535 case MEMCACHED_CONNECTION_TCP
:
536 rc
= network_connect(ptr
);
537 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
538 if (ptr
->fd
!= INVALID_SOCKET
&& ptr
->root
->sasl
.callbacks
)
540 rc
= memcached_sasl_authenticate_connection(ptr
);
541 if (rc
!= MEMCACHED_SUCCESS
)
543 (void)closesocket(ptr
->fd
);
544 ptr
->fd
= INVALID_SOCKET
;
549 case MEMCACHED_CONNECTION_UNIX_SOCKET
:
550 rc
= unix_socket_connect(ptr
);
552 case MEMCACHED_CONNECTION_MAX
:
554 WATCHPOINT_ASSERT(0);
557 if (rc
== MEMCACHED_SUCCESS
)
559 ptr
->server_failure_counter
= 0;
564 ptr
->server_failure_counter
++;
566 set_last_disconnected_host(ptr
);
569 LIBMEMCACHED_MEMCACHED_CONNECT_END();