Remove dead variable and on close check value of file descriptor.
[awesomized/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 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 set_socket_options(memcached_server_st *ptr)
48 {
49 if (ptr->type == MEMCACHED_CONNECTION_UDP)
50 return MEMCACHED_SUCCESS;
51
52 if (ptr->root->snd_timeout)
53 {
54 int error;
55 struct timeval waittime;
56
57 waittime.tv_sec= 0;
58 waittime.tv_usec= ptr->root->snd_timeout;
59
60 error= setsockopt(ptr->fd, SOL_SOCKET, SO_SNDTIMEO,
61 &waittime, (socklen_t)sizeof(struct timeval));
62 WATCHPOINT_ASSERT(error == 0);
63 }
64
65 if (ptr->root->rcv_timeout)
66 {
67 int error;
68 struct timeval waittime;
69
70 waittime.tv_sec= 0;
71 waittime.tv_usec= ptr->root->rcv_timeout;
72
73 error= setsockopt(ptr->fd, SOL_SOCKET, SO_RCVTIMEO,
74 &waittime, (socklen_t)sizeof(struct timeval));
75 WATCHPOINT_ASSERT(error == 0);
76 }
77
78 {
79 int error;
80 struct linger linger;
81
82 linger.l_onoff= 1;
83 linger.l_linger= MEMCACHED_DEFAULT_TIMEOUT;
84 error= setsockopt(ptr->fd, SOL_SOCKET, SO_LINGER,
85 &linger, (socklen_t)sizeof(struct linger));
86 WATCHPOINT_ASSERT(error == 0);
87 }
88
89 if (ptr->root->flags & MEM_TCP_NODELAY)
90 {
91 int flag= 1;
92 int error;
93
94 error= setsockopt(ptr->fd, IPPROTO_TCP, TCP_NODELAY,
95 &flag, (socklen_t)sizeof(int));
96 WATCHPOINT_ASSERT(error == 0);
97 }
98
99 if (ptr->root->send_size)
100 {
101 int error;
102
103 error= setsockopt(ptr->fd, SOL_SOCKET, SO_SNDBUF,
104 &ptr->root->send_size, (socklen_t)sizeof(int));
105 WATCHPOINT_ASSERT(error == 0);
106 }
107
108 if (ptr->root->recv_size)
109 {
110 int error;
111
112 error= setsockopt(ptr->fd, SOL_SOCKET, SO_SNDBUF,
113 &ptr->root->recv_size, (socklen_t)sizeof(int));
114 WATCHPOINT_ASSERT(error == 0);
115 }
116
117 /* For the moment, not getting a nonblocking mode will not be fatal */
118 if (ptr->root->flags & MEM_NO_BLOCK)
119 {
120 int flags;
121
122 flags= fcntl(ptr->fd, F_GETFL, 0);
123 unlikely (flags != -1)
124 {
125 (void)fcntl(ptr->fd, F_SETFL, flags | O_NONBLOCK);
126 }
127 }
128
129 return MEMCACHED_SUCCESS;
130 }
131
132 static memcached_return unix_socket_connect(memcached_server_st *ptr)
133 {
134 struct sockaddr_un servAddr;
135 socklen_t addrlen;
136
137 if (ptr->fd == -1)
138 {
139 if ((ptr->fd= socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
140 {
141 ptr->cached_errno= errno;
142 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE;
143 }
144
145 memset(&servAddr, 0, sizeof (struct sockaddr_un));
146 servAddr.sun_family= AF_UNIX;
147 strcpy(servAddr.sun_path, ptr->hostname); /* Copy filename */
148
149 addrlen= strlen(servAddr.sun_path) + sizeof(servAddr.sun_family);
150
151 test_connect:
152 if (connect(ptr->fd,
153 (struct sockaddr *)&servAddr,
154 sizeof(servAddr)) < 0)
155 {
156 switch (errno) {
157 case EINPROGRESS:
158 case EALREADY:
159 case EINTR:
160 goto test_connect;
161 case EISCONN: /* We were spinning waiting on connect */
162 break;
163 default:
164 WATCHPOINT_ERRNO(errno);
165 ptr->cached_errno= errno;
166 return MEMCACHED_ERRNO;
167 }
168 }
169 }
170 return MEMCACHED_SUCCESS;
171 }
172
173 static memcached_return network_connect(memcached_server_st *ptr)
174 {
175 if (ptr->fd == -1)
176 {
177 struct addrinfo *use;
178
179 if (ptr->root->server_failure_limit != 0)
180 {
181 if (ptr->server_failure_counter >= ptr->root->server_failure_limit)
182 {
183 memcached_server_remove(ptr);
184 return MEMCACHED_FAILURE;
185 }
186 }
187
188 if (ptr->sockaddr_inited == MEMCACHED_NOT_ALLOCATED ||
189 (!(ptr->root->flags & MEM_USE_CACHE_LOOKUPS)))
190 {
191 memcached_return rc;
192
193 rc= set_hostinfo(ptr);
194 if (rc != MEMCACHED_SUCCESS)
195 return rc;
196 ptr->sockaddr_inited= MEMCACHED_ALLOCATED;
197 }
198
199 use= ptr->address_info;
200 /* Create the socket */
201 while (use != NULL)
202 {
203 if ((ptr->fd= socket(use->ai_family,
204 use->ai_socktype,
205 use->ai_protocol)) < 0)
206 {
207 ptr->cached_errno= errno;
208 WATCHPOINT_ERRNO(errno);
209 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE;
210 }
211
212 (void)set_socket_options(ptr);
213
214 /* connect to server */
215 test_connect:
216 if (connect(ptr->fd,
217 use->ai_addr,
218 use->ai_addrlen) < 0)
219 {
220 switch (errno) {
221 /* We are spinning waiting on connect */
222 case EALREADY:
223 case EINPROGRESS:
224 {
225 struct pollfd fds[1];
226 int error;
227
228 memset(&fds, 0, sizeof(struct pollfd));
229 fds[0].fd= ptr->fd;
230 fds[0].events= POLLOUT | POLLERR;
231 error= poll(fds, 1, ptr->root->connect_timeout);
232
233 if (error == 0)
234 {
235 goto handle_retry;
236 }
237 else if (error != 1 || fds[0].revents & POLLERR)
238 {
239 ptr->cached_errno= errno;
240 WATCHPOINT_ERRNO(ptr->cached_errno);
241 WATCHPOINT_NUMBER(ptr->root->connect_timeout);
242 close(ptr->fd);
243 ptr->fd= -1;
244 if (ptr->address_info)
245 {
246 freeaddrinfo(ptr->address_info);
247 ptr->address_info= NULL;
248 }
249
250 if (ptr->root->retry_timeout)
251 {
252 struct timeval next_time;
253
254 gettimeofday(&next_time, NULL);
255 ptr->next_retry= next_time.tv_sec + ptr->root->retry_timeout;
256 }
257 ptr->server_failure_counter+= 1;
258 return MEMCACHED_ERRNO;
259 }
260
261 break;
262 }
263 /* We are spinning waiting on connect */
264 case EINTR:
265 goto test_connect;
266 case EISCONN: /* We were spinning waiting on connect */
267 break;
268 default:
269 handle_retry:
270 ptr->cached_errno= errno;
271 close(ptr->fd);
272 ptr->fd= -1;
273 if (ptr->root->retry_timeout)
274 {
275 struct timeval next_time;
276
277 gettimeofday(&next_time, NULL);
278 ptr->next_retry= next_time.tv_sec + ptr->root->retry_timeout;
279 }
280 }
281 }
282 else
283 {
284 WATCHPOINT_ASSERT(ptr->cursor_active == 0);
285 ptr->server_failure_counter= 0;
286 return MEMCACHED_SUCCESS;
287 }
288 use = use->ai_next;
289 }
290 }
291
292 if (ptr->fd == -1) {
293 ptr->server_failure_counter+= 1;
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 memcached_connect(memcached_server_st *ptr)
303 {
304 memcached_return rc= MEMCACHED_NO_SERVERS;
305 LIBMEMCACHED_MEMCACHED_CONNECT_START();
306
307 if (ptr->root->retry_timeout)
308 {
309 struct timeval next_time;
310
311 gettimeofday(&next_time, NULL);
312 if (next_time.tv_sec < ptr->next_retry)
313 return MEMCACHED_TIMEOUT;
314 }
315 /* We need to clean up the multi startup piece */
316 switch (ptr->type)
317 {
318 case MEMCACHED_CONNECTION_UNKNOWN:
319 WATCHPOINT_ASSERT(0);
320 rc= MEMCACHED_NOT_SUPPORTED;
321 break;
322 case MEMCACHED_CONNECTION_UDP:
323 case MEMCACHED_CONNECTION_TCP:
324 rc= network_connect(ptr);
325 break;
326 case MEMCACHED_CONNECTION_UNIX_SOCKET:
327 rc= unix_socket_connect(ptr);
328 break;
329 default:
330 WATCHPOINT_ASSERT(0);
331 }
332
333 LIBMEMCACHED_MEMCACHED_CONNECT_END();
334
335 return rc;
336 }