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