Fix for lingerd and bad free.
[awesomized/libmemcached] / libmemcached / memcached_connect.c
1 #include "common.h"
2 #include <poll.h>
3 #include <sys/time.h>
4
5 static memcached_return set_hostinfo(memcached_server_st *server)
6 {
7 struct addrinfo *ai;
8 struct addrinfo hints;
9 int e;
10 char str_port[NI_MAXSERV];
11
12 sprintf(str_port, "%u", server->port);
13
14 memset(&hints, 0, sizeof(hints));
15
16 hints.ai_family= AF_INET;
17 if (server->type == MEMCACHED_CONNECTION_UDP)
18 {
19 hints.ai_protocol= IPPROTO_UDP;
20 hints.ai_socktype= SOCK_DGRAM;
21 }
22 else
23 {
24 hints.ai_socktype= SOCK_STREAM;
25 hints.ai_protocol= IPPROTO_TCP;
26 }
27
28 e= getaddrinfo(server->hostname, str_port, &hints, &ai);
29 if (e != 0)
30 {
31 WATCHPOINT_STRING(server->hostname);
32 WATCHPOINT_STRING(gai_strerror(e));
33 return MEMCACHED_HOST_LOOKUP_FAILURE;
34 }
35
36 if (server->address_info)
37 freeaddrinfo(server->address_info);
38 server->address_info= ai;
39
40 return MEMCACHED_SUCCESS;
41 }
42
43 static memcached_return set_socket_options(memcached_server_st *ptr)
44 {
45 if (ptr->type == MEMCACHED_CONNECTION_UDP)
46 return MEMCACHED_SUCCESS;
47
48 if (ptr->root->flags & MEM_NO_BLOCK)
49 {
50 int error;
51 struct timeval waittime;
52
53 waittime.tv_sec= 10;
54 waittime.tv_usec= 0;
55
56 error= setsockopt(ptr->fd, SOL_SOCKET, SO_SNDTIMEO,
57 &waittime, (socklen_t)sizeof(struct timeval));
58 WATCHPOINT_ASSERT(error == 0);
59
60 error= setsockopt(ptr->fd, SOL_SOCKET, SO_RCVTIMEO,
61 &waittime, (socklen_t)sizeof(struct timeval));
62 WATCHPOINT_ASSERT(error == 0);
63 }
64
65 {
66 int error;
67 struct linger linger;
68
69 linger.l_onoff= 1;
70 linger.l_linger= MEMCACHED_DEFAULT_TIMEOUT;
71 error= setsockopt(ptr->fd, SOL_SOCKET, SO_LINGER,
72 &linger, (socklen_t)sizeof(struct linger));
73 WATCHPOINT_ASSERT(error == 0);
74 }
75
76 if (ptr->root->flags & MEM_TCP_NODELAY)
77 {
78 int flag= 1;
79 int error;
80
81 error= setsockopt(ptr->fd, IPPROTO_TCP, TCP_NODELAY,
82 &flag, (socklen_t)sizeof(int));
83 WATCHPOINT_ASSERT(error == 0);
84 }
85
86 if (ptr->root->send_size)
87 {
88 int error;
89
90 error= setsockopt(ptr->fd, SOL_SOCKET, SO_SNDBUF,
91 &ptr->root->send_size, (socklen_t)sizeof(int));
92 WATCHPOINT_ASSERT(error == 0);
93 }
94
95 if (ptr->root->recv_size)
96 {
97 int error;
98
99 error= setsockopt(ptr->fd, SOL_SOCKET, SO_SNDBUF,
100 &ptr->root->recv_size, (socklen_t)sizeof(int));
101 WATCHPOINT_ASSERT(error == 0);
102 }
103
104 /* For the moment, not getting a nonblocking mode will not be fatal */
105 if (ptr->root->flags & MEM_NO_BLOCK)
106 {
107 int flags;
108
109 flags= fcntl(ptr->fd, F_GETFL, 0);
110 unlikely (flags != -1)
111 {
112 (void)fcntl(ptr->fd, F_SETFL, flags | O_NONBLOCK);
113 }
114 }
115
116 return MEMCACHED_SUCCESS;
117 }
118
119 static memcached_return unix_socket_connect(memcached_server_st *ptr)
120 {
121 struct sockaddr_un servAddr;
122 socklen_t addrlen;
123
124 if (ptr->fd == -1)
125 {
126 if ((ptr->fd= socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
127 {
128 ptr->cached_errno= errno;
129 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE;
130 }
131
132 memset(&servAddr, 0, sizeof (struct sockaddr_un));
133 servAddr.sun_family= AF_UNIX;
134 strcpy(servAddr.sun_path, ptr->hostname); /* Copy filename */
135
136 addrlen= strlen(servAddr.sun_path) + sizeof(servAddr.sun_family);
137
138 test_connect:
139 if (connect(ptr->fd,
140 (struct sockaddr *)&servAddr,
141 sizeof(servAddr)) < 0)
142 {
143 switch (errno) {
144 case EINPROGRESS:
145 case EALREADY:
146 case EINTR:
147 goto test_connect;
148 case EISCONN: /* We were spinning waiting on connect */
149 break;
150 default:
151 WATCHPOINT_ERRNO(errno);
152 ptr->cached_errno= errno;
153 return MEMCACHED_ERRNO;
154 }
155 }
156 }
157 return MEMCACHED_SUCCESS;
158 }
159
160 static memcached_return network_connect(memcached_server_st *ptr)
161 {
162 if (ptr->fd == -1)
163 {
164 struct addrinfo *use;
165
166 /* Old connection junk still is in the structure */
167 WATCHPOINT_ASSERT(ptr->cursor_active == 0);
168
169 if (ptr->sockaddr_inited == MEMCACHED_NOT_ALLOCATED ||
170 (!(ptr->root->flags & MEM_USE_CACHE_LOOKUPS)))
171 {
172 memcached_return rc;
173
174 rc= set_hostinfo(ptr);
175 if (rc != MEMCACHED_SUCCESS)
176 return rc;
177 ptr->sockaddr_inited= MEMCACHED_ALLOCATED;
178 }
179
180 use= ptr->address_info;
181 /* Create the socket */
182 while (use != NULL)
183 {
184 if ((ptr->fd= socket(use->ai_family,
185 use->ai_socktype,
186 use->ai_protocol)) < 0)
187 {
188 ptr->cached_errno= errno;
189 WATCHPOINT_ERRNO(errno);
190 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE;
191 }
192
193 (void)set_socket_options(ptr);
194
195 /* connect to server */
196 test_connect:
197 if (connect(ptr->fd,
198 use->ai_addr,
199 use->ai_addrlen) < 0)
200 {
201 switch (errno) {
202 /* We are spinning waiting on connect */
203 case EALREADY:
204 case EINPROGRESS:
205 {
206 struct pollfd fds[1];
207 int error;
208
209 memset(&fds, 0, sizeof(struct pollfd));
210 fds[0].fd= ptr->fd;
211 fds[0].events= POLLOUT | POLLERR;
212 error= poll(fds, 1, ptr->root->connect_timeout);
213
214 if (error == 0)
215 {
216 goto handle_retry;
217 }
218 else if (error != 1)
219 {
220 ptr->cached_errno= errno;
221 WATCHPOINT_ERRNO(ptr->cached_errno);
222 WATCHPOINT_NUMBER(ptr->root->connect_timeout);
223 close(ptr->fd);
224 ptr->fd= -1;
225 if (ptr->address_info)
226 {
227 freeaddrinfo(ptr->address_info);
228 ptr->address_info= NULL;
229 }
230
231 return MEMCACHED_ERRNO;
232 }
233
234 break;
235 }
236 /* We are spinning waiting on connect */
237 case EINTR:
238 goto test_connect;
239 case EISCONN: /* We were spinning waiting on connect */
240 break;
241 default:
242 handle_retry:
243 ptr->cached_errno= errno;
244 close(ptr->fd);
245 ptr->fd= -1;
246 if (ptr->root->retry_timeout)
247 {
248 struct timeval next_time;
249
250 gettimeofday(&next_time, NULL);
251 ptr->next_retry= next_time.tv_sec + ptr->root->retry_timeout;
252 }
253 }
254 }
255 else
256 {
257 WATCHPOINT_ASSERT(ptr->cursor_active == 0);
258 return MEMCACHED_SUCCESS;
259 }
260 use = use->ai_next;
261 }
262 }
263
264 if (ptr->fd == -1)
265 return MEMCACHED_ERRNO; /* The last error should be from connect() */
266
267 return MEMCACHED_SUCCESS; /* The last error should be from connect() */
268 }
269
270
271 memcached_return memcached_connect(memcached_server_st *ptr)
272 {
273 memcached_return rc= MEMCACHED_NO_SERVERS;
274 LIBMEMCACHED_MEMCACHED_CONNECT_START();
275
276 if (ptr->root->retry_timeout)
277 {
278 struct timeval next_time;
279
280 gettimeofday(&next_time, NULL);
281 if (next_time.tv_sec < ptr->next_retry)
282 return MEMCACHED_TIMEOUT;
283 }
284 /* We need to clean up the multi startup piece */
285 switch (ptr->type)
286 {
287 case MEMCACHED_CONNECTION_UNKNOWN:
288 WATCHPOINT_ASSERT(0);
289 rc= MEMCACHED_NOT_SUPPORTED;
290 break;
291 case MEMCACHED_CONNECTION_UDP:
292 case MEMCACHED_CONNECTION_TCP:
293 rc= network_connect(ptr);
294 break;
295 case MEMCACHED_CONNECTION_UNIX_SOCKET:
296 rc= unix_socket_connect(ptr);
297 break;
298 default:
299 WATCHPOINT_ASSERT(0);
300 }
301
302 LIBMEMCACHED_MEMCACHED_CONNECT_END();
303
304 return rc;
305 }