Implement timeout for connect ipv4/ipv6
[lttng-tools.git] / src / common / sessiond-comm / inet.c
CommitLineData
6364a07a
DG
1/*
2 * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18#define _GNU_SOURCE
19#include <assert.h>
20#include <limits.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26#include <unistd.h>
27#include <errno.h>
a655f4cf
MD
28#include <fcntl.h>
29#include <time.h>
30#include <poll.h>
6364a07a 31
90e535ef 32#include <common/common.h>
6364a07a
DG
33
34#include "inet.h"
35
a655f4cf
MD
36#define MSEC_PER_SEC 1000
37#define NSEC_PER_MSEC 1000000
38#define RECONNECT_DELAY 200 /* ms */
39
6364a07a
DG
40/*
41 * INET protocol operations.
42 */
43static const struct lttcomm_proto_ops inet_ops = {
44 .bind = lttcomm_bind_inet_sock,
45 .close = lttcomm_close_inet_sock,
46 .connect = lttcomm_connect_inet_sock,
47 .accept = lttcomm_accept_inet_sock,
48 .listen = lttcomm_listen_inet_sock,
49 .recvmsg = lttcomm_recvmsg_inet_sock,
50 .sendmsg = lttcomm_sendmsg_inet_sock,
51};
52
53/*
54 * Creates an PF_INET socket.
55 */
90e535ef 56LTTNG_HIDDEN
6364a07a
DG
57int lttcomm_create_inet_sock(struct lttcomm_sock *sock, int type, int proto)
58{
de5e9086 59 int val = 1, ret;
6364a07a
DG
60
61 /* Create server socket */
62 if ((sock->fd = socket(PF_INET, type, proto)) < 0) {
63 PERROR("socket inet");
64 goto error;
65 }
66
67 sock->ops = &inet_ops;
68
69 /*
70 * Set socket option to reuse the address.
71 */
72 ret = setsockopt(sock->fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(int));
73 if (ret < 0) {
74 PERROR("setsockopt inet");
75 goto error;
76 }
77
78 return 0;
79
80error:
81 return -1;
82}
83
84/*
85 * Bind socket and return.
86 */
90e535ef 87LTTNG_HIDDEN
6364a07a
DG
88int lttcomm_bind_inet_sock(struct lttcomm_sock *sock)
89{
90 int ret;
91
92 ret = bind(sock->fd, &sock->sockaddr.addr.sin,
93 sizeof(sock->sockaddr.addr.sin));
94 if (ret < 0) {
95 PERROR("bind inet");
96 }
97
98 return ret;
99}
100
a655f4cf
MD
101static
102int connect_no_timeout(struct lttcomm_sock *sock)
103{
104 return connect(sock->fd, (struct sockaddr *) &sock->sockaddr.addr.sin,
105 sizeof(sock->sockaddr.addr.sin));
106}
107
108/*
109 * Return time_a - time_b in milliseconds.
110 */
111static
112unsigned long time_diff_ms(struct timespec *time_a,
113 struct timespec *time_b)
114{
115 time_t sec_diff;
116 long nsec_diff;
117 unsigned long result_ms;
118
119 sec_diff = time_a->tv_sec - time_b->tv_sec;
120 nsec_diff = time_a->tv_nsec - time_b->tv_nsec;
121
122 result_ms = sec_diff * MSEC_PER_SEC;
123 result_ms += nsec_diff / NSEC_PER_MSEC;
124 return result_ms;
125}
126
127static
128int connect_with_timeout(struct lttcomm_sock *sock)
129{
130 unsigned long timeout = lttcomm_get_network_timeout();
131 int ret, flags, connect_ret;
132 struct timespec orig_time, cur_time;
133
134 ret = fcntl(sock->fd, F_GETFL, 0);
135 if (ret == -1) {
136 PERROR("fcntl");
137 return -1;
138 }
139 flags = ret;
140
141 /* Set socket to nonblock */
142 ret = fcntl(sock->fd, F_SETFL, flags | O_NONBLOCK);
143 if (ret == -1) {
144 PERROR("fcntl");
145 return -1;
146 }
147
148 ret = clock_gettime(CLOCK_MONOTONIC, &orig_time);
149 if (ret == -1) {
150 PERROR("clock_gettime");
151 return -1;
152 }
153
154 connect_ret = connect(sock->fd,
155 (struct sockaddr *) &sock->sockaddr.addr.sin,
156 sizeof(sock->sockaddr.addr.sin));
157 if (connect_ret == -1 && errno != EAGAIN
158 && errno != EWOULDBLOCK
159 && errno != EINPROGRESS) {
160 goto error;
161 } else if (!connect_ret) {
162 /* Connect succeeded */
163 goto success;
164 }
165
166 /*
167 * Perform poll loop following EINPROGRESS recommendation from
168 * connect(2) man page.
169 */
170 do {
171 struct pollfd fds;
172
173 fds.fd = sock->fd;
174 fds.events = POLLOUT;
175 fds.revents = 0;
176 ret = poll(&fds, 1, RECONNECT_DELAY);
177 if (ret < 0) {
178 goto error;
179 } else if (ret > 0) {
180 int optval;
181 socklen_t optval_len = sizeof(optval);
182
183 if (!(fds.revents & POLLOUT)) {
184 /* Either hup or error */
185 errno = EPIPE;
186 goto error;
187 }
188 /* got something */
189 ret = getsockopt(sock->fd, SOL_SOCKET,
190 SO_ERROR, &optval, &optval_len);
191 if (ret) {
192 goto error;
193 }
194 if (!optval) {
195 connect_ret = 0;
196 goto success;
197 } else {
198 goto error;
199 }
200 }
201 /* ret == 0: timeout */
202 ret = clock_gettime(CLOCK_MONOTONIC, &cur_time);
203 if (ret == -1) {
204 PERROR("clock_gettime");
205 connect_ret = ret;
206 goto error;
207 }
208 } while (time_diff_ms(&cur_time, &orig_time) < timeout);
209
210 /* Timeout */
211 errno = ETIMEDOUT;
212 connect_ret = -1;
213
214success:
215 /* Restore initial flags */
216 ret = fcntl(sock->fd, F_SETFL, flags);
217 if (ret == -1) {
218 PERROR("fcntl");
219 /* Continue anyway */
220 }
221error:
222 return connect_ret;
223}
224
6364a07a
DG
225/*
226 * Connect PF_INET socket.
227 */
90e535ef 228LTTNG_HIDDEN
6364a07a
DG
229int lttcomm_connect_inet_sock(struct lttcomm_sock *sock)
230{
231 int ret, closeret;
232
a655f4cf
MD
233 if (lttcomm_get_network_timeout()) {
234 ret = connect_with_timeout(sock);
235 } else {
236 ret = connect_no_timeout(sock);
237 }
6364a07a 238 if (ret < 0) {
82c05d47 239 PERROR("connect");
6364a07a
DG
240 goto error_connect;
241 }
242
243 return ret;
244
245error_connect:
246 closeret = close(sock->fd);
247 if (closeret) {
248 PERROR("close inet");
249 }
250
251 return ret;
252}
253
254/*
255 * Do an accept(2) on the sock and return the new lttcomm socket. The socket
256 * MUST be bind(2) before.
257 */
90e535ef 258LTTNG_HIDDEN
6364a07a
DG
259struct lttcomm_sock *lttcomm_accept_inet_sock(struct lttcomm_sock *sock)
260{
261 int new_fd;
88a5db70 262 socklen_t len;
6364a07a
DG
263 struct lttcomm_sock *new_sock;
264
265 if (sock->proto == LTTCOMM_SOCK_UDP) {
266 /*
267 * accept(2) does not exist for UDP so simply return the passed socket.
268 */
269 new_sock = sock;
270 goto end;
271 }
272
de5e9086 273 new_sock = lttcomm_alloc_sock(sock->proto);
6364a07a
DG
274 if (new_sock == NULL) {
275 goto error;
276 }
277
88a5db70
DG
278 len = sizeof(new_sock->sockaddr.addr.sin);
279
6364a07a
DG
280 /* Blocking call */
281 new_fd = accept(sock->fd, (struct sockaddr *) &new_sock->sockaddr.addr.sin,
282 &len);
283 if (new_fd < 0) {
284 PERROR("accept inet");
285 goto error;
286 }
287
288 new_sock->fd = new_fd;
de5e9086 289 new_sock->ops = &inet_ops;
6364a07a
DG
290
291end:
292 return new_sock;
293
294error:
295 free(new_sock);
296 return NULL;
297}
298
299/*
300 * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN.
301 */
90e535ef 302LTTNG_HIDDEN
6364a07a
DG
303int lttcomm_listen_inet_sock(struct lttcomm_sock *sock, int backlog)
304{
305 int ret;
306
307 if (sock->proto == LTTCOMM_SOCK_UDP) {
308 /* listen(2) does not exist for UDP so simply return success. */
309 ret = 0;
310 goto end;
311 }
312
313 /* Default listen backlog */
314 if (backlog <= 0) {
315 backlog = LTTNG_SESSIOND_COMM_MAX_LISTEN;
316 }
317
318 ret = listen(sock->fd, backlog);
319 if (ret < 0) {
320 PERROR("listen inet");
321 }
322
323end:
324 return ret;
325}
326
327/*
328 * Receive data of size len in put that data into the buf param. Using recvmsg
329 * API.
330 *
331 * Return the size of received data.
332 */
90e535ef 333LTTNG_HIDDEN
6364a07a
DG
334ssize_t lttcomm_recvmsg_inet_sock(struct lttcomm_sock *sock, void *buf,
335 size_t len, int flags)
336{
337 struct msghdr msg;
338 struct iovec iov[1];
339 ssize_t ret = -1;
7c5aef62 340 size_t len_last;
6364a07a
DG
341
342 memset(&msg, 0, sizeof(msg));
343
344 iov[0].iov_base = buf;
345 iov[0].iov_len = len;
346 msg.msg_iov = iov;
347 msg.msg_iovlen = 1;
348
349 msg.msg_name = (struct sockaddr *) &sock->sockaddr.addr.sin;
350 msg.msg_namelen = sizeof(sock->sockaddr.addr.sin);
351
6364a07a 352 do {
7c5aef62 353 len_last = iov[0].iov_len;
6364a07a 354 ret = recvmsg(sock->fd, &msg, flags);
7c5aef62
DG
355 if (ret > 0) {
356 iov[0].iov_base += ret;
357 iov[0].iov_len -= ret;
358 assert(ret <= len_last);
359 }
360 } while ((ret > 0 && ret < len_last) || (ret < 0 && errno == EINTR));
6364a07a
DG
361 if (ret < 0) {
362 PERROR("recvmsg inet");
7c5aef62
DG
363 } else if (ret > 0) {
364 ret = len;
6364a07a 365 }
7c5aef62 366 /* Else ret = 0 meaning an orderly shutdown. */
6364a07a
DG
367
368 return ret;
369}
370
371/*
372 * Send buf data of size len. Using sendmsg API.
373 *
374 * Return the size of sent data.
375 */
90e535ef 376LTTNG_HIDDEN
6364a07a
DG
377ssize_t lttcomm_sendmsg_inet_sock(struct lttcomm_sock *sock, void *buf,
378 size_t len, int flags)
379{
380 struct msghdr msg;
381 struct iovec iov[1];
382 ssize_t ret = -1;
383
384 memset(&msg, 0, sizeof(msg));
385
386 iov[0].iov_base = buf;
387 iov[0].iov_len = len;
388 msg.msg_iov = iov;
389 msg.msg_iovlen = 1;
390
391 switch (sock->proto) {
392 case LTTCOMM_SOCK_UDP:
393 msg.msg_name = (struct sockaddr *) &sock->sockaddr.addr.sin;
394 msg.msg_namelen = sizeof(sock->sockaddr.addr.sin);
395 break;
396 default:
397 break;
398 }
399
400 do {
401 ret = sendmsg(sock->fd, &msg, flags);
402 } while (ret < 0 && errno == EINTR);
403 if (ret < 0) {
404 /*
405 * Only warn about EPIPE when quiet mode is deactivated.
406 * We consider EPIPE as expected.
407 */
408 if (errno != EPIPE || !lttng_opt_quiet) {
409 PERROR("sendmsg inet");
410 }
411 }
412
413 return ret;
414}
415
416/*
417 * Shutdown cleanly and close.
418 */
90e535ef 419LTTNG_HIDDEN
6364a07a
DG
420int lttcomm_close_inet_sock(struct lttcomm_sock *sock)
421{
6e742359 422 int ret;
6364a07a 423
de5e9086 424 /* Don't try to close an invalid marked socket */
6364a07a
DG
425 if (sock->fd == -1) {
426 return 0;
427 }
428
6e742359
DG
429 ret = close(sock->fd);
430 if (ret) {
6364a07a
DG
431 PERROR("close inet");
432 }
433
434 /* Mark socket */
435 sock->fd = -1;
436
437 return ret;
438}
This page took 0.067298 seconds and 4 git commands to generate.