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