First merge of Trond's patches (cherry picking).
[m6w6/libmemcached] / libmemcached / memcached_connect.c
1 #include "common.h"
2 #include <netdb.h>
3 #include <poll.h>
4 #include <sys/time.h>
5
6 static memcached_return_t set_hostinfo(memcached_server_st *server)
7 {
8 struct addrinfo *ai;
9 struct addrinfo hints;
10 int e;
11 char str_port[NI_MAXSERV];
12
13 sprintf(str_port, "%u", server->port);
14
15 memset(&hints, 0, sizeof(hints));
16
17 // hints.ai_family= AF_INET;
18 if (server->type == MEMCACHED_CONNECTION_UDP)
19 {
20 hints.ai_protocol= IPPROTO_UDP;
21 hints.ai_socktype= SOCK_DGRAM;
22 }
23 else
24 {
25 hints.ai_socktype= SOCK_STREAM;
26 hints.ai_protocol= IPPROTO_TCP;
27 }
28
29 e= getaddrinfo(server->hostname, str_port, &hints, &ai);
30 if (e != 0)
31 {
32 WATCHPOINT_STRING(server->hostname);
33 WATCHPOINT_STRING(gai_strerror(e));
34 return MEMCACHED_HOST_LOOKUP_FAILURE;
35 }
36
37 if (server->address_info)
38 {
39 freeaddrinfo(server->address_info);
40 server->address_info= NULL;
41 }
42 server->address_info= ai;
43
44 return MEMCACHED_SUCCESS;
45 }
46
47 static memcached_return_t set_socket_options(memcached_server_st *ptr)
48 {
49 WATCHPOINT_ASSERT(ptr->fd != -1);
50
51 if (ptr->type == MEMCACHED_CONNECTION_UDP)
52 return MEMCACHED_SUCCESS;
53
54 #ifdef HAVE_SNDTIMEO
55 if (ptr->root->snd_timeout)
56 {
57 int error;
58 struct timeval waittime;
59
60 waittime.tv_sec= 0;
61 waittime.tv_usec= ptr->root->snd_timeout;
62
63 error= setsockopt(ptr->fd, SOL_SOCKET, SO_SNDTIMEO,
64 &waittime, (socklen_t)sizeof(struct timeval));
65 WATCHPOINT_ASSERT(error == 0);
66 }
67 #endif
68
69 #ifdef HAVE_RCVTIMEO
70 if (ptr->root->rcv_timeout)
71 {
72 int error;
73 struct timeval waittime;
74
75 waittime.tv_sec= 0;
76 waittime.tv_usec= ptr->root->rcv_timeout;
77
78 error= setsockopt(ptr->fd, SOL_SOCKET, SO_RCVTIMEO,
79 &waittime, (socklen_t)sizeof(struct timeval));
80 WATCHPOINT_ASSERT(error == 0);
81 }
82 #endif
83
84 if (ptr->root->flags.no_block)
85 {
86 int error;
87 struct linger linger;
88
89 linger.l_onoff= 1;
90 linger.l_linger= 0; /* By default on close() just drop the socket */
91 error= setsockopt(ptr->fd, SOL_SOCKET, SO_LINGER,
92 &linger, (socklen_t)sizeof(struct linger));
93 WATCHPOINT_ASSERT(error == 0);
94 }
95
96 if (ptr->root->flags.tcp_nodelay)
97 {
98 int flag= 1;
99 int error;
100
101 error= setsockopt(ptr->fd, IPPROTO_TCP, TCP_NODELAY,
102 &flag, (socklen_t)sizeof(int));
103 WATCHPOINT_ASSERT(error == 0);
104 }
105
106 if (ptr->root->send_size)
107 {
108 int error;
109
110 error= setsockopt(ptr->fd, SOL_SOCKET, SO_SNDBUF,
111 &ptr->root->send_size, (socklen_t)sizeof(int));
112 WATCHPOINT_ASSERT(error == 0);
113 }
114
115 if (ptr->root->recv_size)
116 {
117 int error;
118
119 error= setsockopt(ptr->fd, SOL_SOCKET, SO_RCVBUF,
120 &ptr->root->recv_size, (socklen_t)sizeof(int));
121 WATCHPOINT_ASSERT(error == 0);
122 }
123
124 /* libmemcached will always use nonblocking IO to avoid write deadlocks */
125 int flags;
126
127 do
128 flags= fcntl(ptr->fd, F_GETFL, 0);
129 while (flags == -1 && (errno == EINTR || errno == EAGAIN));
130
131 unlikely (flags == -1)
132 return MEMCACHED_CONNECTION_FAILURE;
133 else if ((flags & O_NONBLOCK) == 0)
134 {
135 int rval;
136
137 do
138 rval= fcntl(ptr->fd, F_SETFL, flags | O_NONBLOCK);
139 while (rval == -1 && (errno == EINTR || errno == EAGAIN));
140
141 unlikely (rval == -1)
142 return MEMCACHED_CONNECTION_FAILURE;
143 }
144
145 return MEMCACHED_SUCCESS;
146 }
147
148 static memcached_return_t unix_socket_connect(memcached_server_st *ptr)
149 {
150 struct sockaddr_un servAddr;
151 socklen_t addrlen;
152
153 if (ptr->fd == -1)
154 {
155 if ((ptr->fd= socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
156 {
157 ptr->cached_errno= errno;
158 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE;
159 }
160
161 memset(&servAddr, 0, sizeof (struct sockaddr_un));
162 servAddr.sun_family= AF_UNIX;
163 strcpy(servAddr.sun_path, ptr->hostname); /* Copy filename */
164
165 addrlen= (socklen_t) (strlen(servAddr.sun_path) + sizeof(servAddr.sun_family));
166
167 test_connect:
168 if (connect(ptr->fd,
169 (struct sockaddr *)&servAddr,
170 sizeof(servAddr)) < 0)
171 {
172 switch (errno)
173 {
174 case EINPROGRESS:
175 case EALREADY:
176 case EINTR:
177 goto test_connect;
178 case EISCONN: /* We were spinning waiting on connect */
179 break;
180 default:
181 WATCHPOINT_ERRNO(errno);
182 ptr->cached_errno= errno;
183 return MEMCACHED_ERRNO;
184 }
185 }
186 }
187
188 WATCHPOINT_ASSERT(ptr->fd != -1);
189 return MEMCACHED_SUCCESS;
190 }
191
192 static memcached_return_t network_connect(memcached_server_st *ptr)
193 {
194 if (ptr->fd == -1)
195 {
196 struct addrinfo *use;
197
198 if (!ptr->sockaddr_inited ||
199 (!(ptr->root->flags.use_cache_lookups)))
200 {
201 memcached_return_t rc;
202
203 rc= set_hostinfo(ptr);
204 if (rc != MEMCACHED_SUCCESS)
205 return rc;
206 ptr->sockaddr_inited= true;
207 }
208
209 use= ptr->address_info;
210 /* Create the socket */
211 while (use != NULL)
212 {
213 /* Memcache server does not support IPV6 in udp mode, so skip if not ipv4 */
214 if (ptr->type == MEMCACHED_CONNECTION_UDP && use->ai_family != AF_INET)
215 {
216 use= use->ai_next;
217 continue;
218 }
219
220 if ((ptr->fd= socket(use->ai_family,
221 use->ai_socktype,
222 use->ai_protocol)) < 0)
223 {
224 ptr->cached_errno= errno;
225 WATCHPOINT_ERRNO(errno);
226 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE;
227 }
228
229 (void)set_socket_options(ptr);
230
231 /* connect to server */
232 while (ptr->fd != -1 &&
233 connect(ptr->fd, use->ai_addr, use->ai_addrlen) < 0)
234 {
235 ptr->cached_errno= errno;
236 if (errno == EINPROGRESS || /* nonblocking mode - first return, */
237 errno == EALREADY) /* nonblocking mode - subsequent returns */
238 {
239 struct pollfd fds[1];
240 fds[0].fd = ptr->fd;
241 fds[0].events = POLLOUT;
242 int error= poll(fds, 1, ptr->root->connect_timeout);
243
244 if (error != 1 || fds[0].revents & POLLERR)
245 {
246 if (fds[0].revents & POLLERR)
247 {
248 int err;
249 socklen_t len = sizeof (err);
250 (void)getsockopt(ptr->fd, SOL_SOCKET, SO_ERROR, &err, &len);
251 ptr->cached_errno= (err == 0) ? errno : err;
252 }
253
254 (void)close(ptr->fd);
255 ptr->fd= -1;
256 }
257 }
258 else if (errno == EISCONN) /* we are connected :-) */
259 {
260 break;
261 }
262 else if (errno != EINTR)
263 {
264 (void)close(ptr->fd);
265 ptr->fd= -1;
266 break;
267 }
268 }
269
270 if (ptr->fd != -1)
271 {
272 WATCHPOINT_ASSERT(ptr->cursor_active == 0);
273 ptr->server_failure_counter= 0;
274 return MEMCACHED_SUCCESS;
275 }
276 use = use->ai_next;
277 }
278 }
279
280 if (ptr->fd == -1)
281 {
282 /* Failed to connect. schedule next retry */
283 if (ptr->root->retry_timeout)
284 {
285 struct timeval next_time;
286
287 if (gettimeofday(&next_time, NULL) == 0)
288 ptr->next_retry= next_time.tv_sec + ptr->root->retry_timeout;
289 }
290 ptr->server_failure_counter++;
291 if (ptr->cached_errno == 0)
292 return MEMCACHED_TIMEOUT;
293
294 return MEMCACHED_ERRNO; /* The last error should be from connect() */
295 }
296
297 ptr->server_failure_counter= 0;
298 return MEMCACHED_SUCCESS; /* The last error should be from connect() */
299 }
300
301
302 memcached_return_t memcached_connect(memcached_server_st *ptr)
303 {
304 memcached_return_t rc= MEMCACHED_NO_SERVERS;
305 LIBMEMCACHED_MEMCACHED_CONNECT_START();
306
307 /* both retry_timeout and server_failure_limit must be set in order to delay retrying a server on error. */
308 WATCHPOINT_ASSERT(ptr->root);
309 if (ptr->root->retry_timeout && ptr->root->server_failure_limit)
310 {
311 struct timeval curr_time;
312
313 gettimeofday(&curr_time, NULL);
314
315 /* if we've had too many consecutive errors on this server, mark it dead. */
316 if (ptr->server_failure_counter >= ptr->root->server_failure_limit)
317 {
318 ptr->next_retry= curr_time.tv_sec + ptr->root->retry_timeout;
319 ptr->server_failure_counter= 0;
320 }
321
322 if (curr_time.tv_sec < ptr->next_retry)
323 {
324 if (memcached_behavior_get(ptr->root, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS))
325 run_distribution(ptr->root);
326
327 ptr->root->last_disconnected_server = ptr;
328 return MEMCACHED_SERVER_MARKED_DEAD;
329 }
330 }
331
332 /* We need to clean up the multi startup piece */
333 switch (ptr->type)
334 {
335 case MEMCACHED_CONNECTION_UNKNOWN:
336 WATCHPOINT_ASSERT(0);
337 rc= MEMCACHED_NOT_SUPPORTED;
338 break;
339 case MEMCACHED_CONNECTION_UDP:
340 case MEMCACHED_CONNECTION_TCP:
341 rc= network_connect(ptr);
342 break;
343 case MEMCACHED_CONNECTION_UNIX_SOCKET:
344 rc= unix_socket_connect(ptr);
345 break;
346 default:
347 WATCHPOINT_ASSERT(0);
348 }
349
350 unlikely ( rc != MEMCACHED_SUCCESS) ptr->root->last_disconnected_server = ptr;
351
352 LIBMEMCACHED_MEMCACHED_CONNECT_END();
353
354 return rc;
355 }