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