Fix: Connect timeout arithmetic in inet/inet6 (v4)
[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
6c1c0768 18#define _LGPL_SOURCE
6364a07a
DG
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 28#include <fcntl.h>
389fbf04 29#include <common/compat/time.h>
a655f4cf 30#include <poll.h>
6364a07a 31
90e535ef 32#include <common/common.h>
395d6b02 33#include <common/time.h>
6364a07a
DG
34
35#include "inet.h"
36
a655f4cf
MD
37#define RECONNECT_DELAY 200 /* ms */
38
6364a07a
DG
39/*
40 * INET protocol operations.
41 */
42static const struct lttcomm_proto_ops inet_ops = {
43 .bind = lttcomm_bind_inet_sock,
44 .close = lttcomm_close_inet_sock,
45 .connect = lttcomm_connect_inet_sock,
46 .accept = lttcomm_accept_inet_sock,
47 .listen = lttcomm_listen_inet_sock,
48 .recvmsg = lttcomm_recvmsg_inet_sock,
49 .sendmsg = lttcomm_sendmsg_inet_sock,
50};
51
d831c249
DG
52unsigned long lttcomm_inet_tcp_timeout;
53
6364a07a
DG
54/*
55 * Creates an PF_INET socket.
56 */
90e535ef 57LTTNG_HIDDEN
6364a07a
DG
58int lttcomm_create_inet_sock(struct lttcomm_sock *sock, int type, int proto)
59{
de5e9086 60 int val = 1, ret;
783a3b9a 61 unsigned long timeout;
6364a07a
DG
62
63 /* Create server socket */
64 if ((sock->fd = socket(PF_INET, type, proto)) < 0) {
65 PERROR("socket inet");
66 goto error;
67 }
68
69 sock->ops = &inet_ops;
70
71 /*
72 * Set socket option to reuse the address.
73 */
74 ret = setsockopt(sock->fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(int));
75 if (ret < 0) {
76 PERROR("setsockopt inet");
77 goto error;
78 }
783a3b9a
MD
79 timeout = lttcomm_get_network_timeout();
80 if (timeout) {
81 ret = lttcomm_setsockopt_rcv_timeout(sock->fd, timeout);
82 if (ret) {
83 goto error;
84 }
85 ret = lttcomm_setsockopt_snd_timeout(sock->fd, timeout);
86 if (ret) {
87 goto error;
88 }
89 }
6364a07a
DG
90
91 return 0;
92
93error:
94 return -1;
95}
96
97/*
98 * Bind socket and return.
99 */
90e535ef 100LTTNG_HIDDEN
6364a07a
DG
101int lttcomm_bind_inet_sock(struct lttcomm_sock *sock)
102{
103 int ret;
104
14b88ccf 105 ret = bind(sock->fd, (const struct sockaddr *) &sock->sockaddr.addr.sin,
6364a07a
DG
106 sizeof(sock->sockaddr.addr.sin));
107 if (ret < 0) {
108 PERROR("bind inet");
109 }
110
111 return ret;
112}
113
a655f4cf
MD
114static
115int connect_no_timeout(struct lttcomm_sock *sock)
116{
117 return connect(sock->fd, (struct sockaddr *) &sock->sockaddr.addr.sin,
118 sizeof(sock->sockaddr.addr.sin));
119}
120
a655f4cf
MD
121static
122int connect_with_timeout(struct lttcomm_sock *sock)
123{
124 unsigned long timeout = lttcomm_get_network_timeout();
125 int ret, flags, connect_ret;
126 struct timespec orig_time, cur_time;
f6739b01 127 unsigned long diff_ms;
a655f4cf
MD
128
129 ret = fcntl(sock->fd, F_GETFL, 0);
130 if (ret == -1) {
131 PERROR("fcntl");
132 return -1;
133 }
134 flags = ret;
135
136 /* Set socket to nonblock */
137 ret = fcntl(sock->fd, F_SETFL, flags | O_NONBLOCK);
138 if (ret == -1) {
139 PERROR("fcntl");
140 return -1;
141 }
142
389fbf04 143 ret = lttng_clock_gettime(CLOCK_MONOTONIC, &orig_time);
a655f4cf
MD
144 if (ret == -1) {
145 PERROR("clock_gettime");
146 return -1;
147 }
148
149 connect_ret = connect(sock->fd,
150 (struct sockaddr *) &sock->sockaddr.addr.sin,
151 sizeof(sock->sockaddr.addr.sin));
152 if (connect_ret == -1 && errno != EAGAIN
153 && errno != EWOULDBLOCK
154 && errno != EINPROGRESS) {
155 goto error;
156 } else if (!connect_ret) {
157 /* Connect succeeded */
158 goto success;
159 }
160
40bb1b2a
JR
161 DBG("Asynchronous connect for sock %d, performing polling with"
162 " timeout: %lums", sock->fd, timeout);
a655f4cf
MD
163 /*
164 * Perform poll loop following EINPROGRESS recommendation from
165 * connect(2) man page.
166 */
167 do {
168 struct pollfd fds;
169
170 fds.fd = sock->fd;
171 fds.events = POLLOUT;
172 fds.revents = 0;
173 ret = poll(&fds, 1, RECONNECT_DELAY);
174 if (ret < 0) {
175 goto error;
176 } else if (ret > 0) {
177 int optval;
178 socklen_t optval_len = sizeof(optval);
179
180 if (!(fds.revents & POLLOUT)) {
181 /* Either hup or error */
182 errno = EPIPE;
183 goto error;
184 }
185 /* got something */
186 ret = getsockopt(sock->fd, SOL_SOCKET,
187 SO_ERROR, &optval, &optval_len);
188 if (ret) {
40bb1b2a 189 PERROR("getsockopt");
a655f4cf
MD
190 goto error;
191 }
192 if (!optval) {
193 connect_ret = 0;
194 goto success;
195 } else {
40bb1b2a
JR
196 /* Get actual connect() errno from opt_val */
197 errno = optval;
a655f4cf
MD
198 goto error;
199 }
200 }
201 /* ret == 0: timeout */
389fbf04 202 ret = lttng_clock_gettime(CLOCK_MONOTONIC, &cur_time);
a655f4cf
MD
203 if (ret == -1) {
204 PERROR("clock_gettime");
205 connect_ret = ret;
206 goto error;
207 }
f6739b01
MD
208 if (timespec_to_ms(timespec_abs_diff(cur_time, orig_time), &diff_ms) < 0) {
209 ERR("timespec_to_ms input overflows milliseconds output");
210 connect_ret = -1;
211 goto error;
212 }
213 } while (diff_ms < timeout);
a655f4cf
MD
214
215 /* Timeout */
216 errno = ETIMEDOUT;
217 connect_ret = -1;
218
219success:
220 /* Restore initial flags */
221 ret = fcntl(sock->fd, F_SETFL, flags);
222 if (ret == -1) {
223 PERROR("fcntl");
224 /* Continue anyway */
225 }
226error:
227 return connect_ret;
228}
229
6364a07a
DG
230/*
231 * Connect PF_INET socket.
232 */
90e535ef 233LTTNG_HIDDEN
6364a07a
DG
234int lttcomm_connect_inet_sock(struct lttcomm_sock *sock)
235{
236 int ret, closeret;
237
a655f4cf
MD
238 if (lttcomm_get_network_timeout()) {
239 ret = connect_with_timeout(sock);
240 } else {
241 ret = connect_no_timeout(sock);
242 }
6364a07a 243 if (ret < 0) {
82c05d47 244 PERROR("connect");
6364a07a
DG
245 goto error_connect;
246 }
247
248 return ret;
249
250error_connect:
251 closeret = close(sock->fd);
252 if (closeret) {
253 PERROR("close inet");
254 }
255
256 return ret;
257}
258
259/*
260 * Do an accept(2) on the sock and return the new lttcomm socket. The socket
261 * MUST be bind(2) before.
262 */
90e535ef 263LTTNG_HIDDEN
6364a07a
DG
264struct lttcomm_sock *lttcomm_accept_inet_sock(struct lttcomm_sock *sock)
265{
266 int new_fd;
88a5db70 267 socklen_t len;
6364a07a 268 struct lttcomm_sock *new_sock;
71c648d8 269 unsigned long timeout;
6364a07a
DG
270
271 if (sock->proto == LTTCOMM_SOCK_UDP) {
272 /*
273 * accept(2) does not exist for UDP so simply return the passed socket.
274 */
275 new_sock = sock;
276 goto end;
277 }
278
de5e9086 279 new_sock = lttcomm_alloc_sock(sock->proto);
6364a07a
DG
280 if (new_sock == NULL) {
281 goto error;
282 }
283
88a5db70
DG
284 len = sizeof(new_sock->sockaddr.addr.sin);
285
6364a07a
DG
286 /* Blocking call */
287 new_fd = accept(sock->fd, (struct sockaddr *) &new_sock->sockaddr.addr.sin,
288 &len);
289 if (new_fd < 0) {
290 PERROR("accept inet");
291 goto error;
292 }
71c648d8
JG
293 timeout = lttcomm_get_network_timeout();
294 if (timeout) {
295 int ret;
296
297 ret = lttcomm_setsockopt_rcv_timeout(new_fd, timeout);
298 if (ret) {
475cd9fa 299 goto error_close;
71c648d8
JG
300 }
301 ret = lttcomm_setsockopt_snd_timeout(new_fd, timeout);
302 if (ret) {
475cd9fa 303 goto error_close;
71c648d8
JG
304 }
305 }
6364a07a
DG
306
307 new_sock->fd = new_fd;
de5e9086 308 new_sock->ops = &inet_ops;
6364a07a
DG
309
310end:
311 return new_sock;
312
475cd9fa
DG
313error_close:
314 if (close(new_fd) < 0) {
315 PERROR("accept inet close fd");
316 }
317
6364a07a
DG
318error:
319 free(new_sock);
320 return NULL;
321}
322
323/*
324 * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN.
325 */
90e535ef 326LTTNG_HIDDEN
6364a07a
DG
327int lttcomm_listen_inet_sock(struct lttcomm_sock *sock, int backlog)
328{
329 int ret;
330
331 if (sock->proto == LTTCOMM_SOCK_UDP) {
332 /* listen(2) does not exist for UDP so simply return success. */
333 ret = 0;
334 goto end;
335 }
336
337 /* Default listen backlog */
338 if (backlog <= 0) {
339 backlog = LTTNG_SESSIOND_COMM_MAX_LISTEN;
340 }
341
342 ret = listen(sock->fd, backlog);
343 if (ret < 0) {
344 PERROR("listen inet");
345 }
346
347end:
348 return ret;
349}
350
351/*
352 * Receive data of size len in put that data into the buf param. Using recvmsg
353 * API.
354 *
355 * Return the size of received data.
356 */
90e535ef 357LTTNG_HIDDEN
6364a07a
DG
358ssize_t lttcomm_recvmsg_inet_sock(struct lttcomm_sock *sock, void *buf,
359 size_t len, int flags)
360{
361 struct msghdr msg;
362 struct iovec iov[1];
363 ssize_t ret = -1;
7c5aef62 364 size_t len_last;
6364a07a
DG
365
366 memset(&msg, 0, sizeof(msg));
367
368 iov[0].iov_base = buf;
369 iov[0].iov_len = len;
370 msg.msg_iov = iov;
371 msg.msg_iovlen = 1;
372
373 msg.msg_name = (struct sockaddr *) &sock->sockaddr.addr.sin;
374 msg.msg_namelen = sizeof(sock->sockaddr.addr.sin);
375
6364a07a 376 do {
7c5aef62 377 len_last = iov[0].iov_len;
6364a07a 378 ret = recvmsg(sock->fd, &msg, flags);
7c5aef62
DG
379 if (ret > 0) {
380 iov[0].iov_base += ret;
381 iov[0].iov_len -= ret;
382 assert(ret <= len_last);
383 }
384 } while ((ret > 0 && ret < len_last) || (ret < 0 && errno == EINTR));
6364a07a
DG
385 if (ret < 0) {
386 PERROR("recvmsg inet");
7c5aef62
DG
387 } else if (ret > 0) {
388 ret = len;
6364a07a 389 }
7c5aef62 390 /* Else ret = 0 meaning an orderly shutdown. */
6364a07a
DG
391
392 return ret;
393}
394
395/*
396 * Send buf data of size len. Using sendmsg API.
397 *
398 * Return the size of sent data.
399 */
90e535ef 400LTTNG_HIDDEN
c2d69327 401ssize_t lttcomm_sendmsg_inet_sock(struct lttcomm_sock *sock, const void *buf,
6364a07a
DG
402 size_t len, int flags)
403{
404 struct msghdr msg;
405 struct iovec iov[1];
406 ssize_t ret = -1;
407
408 memset(&msg, 0, sizeof(msg));
409
c2d69327 410 iov[0].iov_base = (void *) buf;
6364a07a
DG
411 iov[0].iov_len = len;
412 msg.msg_iov = iov;
413 msg.msg_iovlen = 1;
414
415 switch (sock->proto) {
416 case LTTCOMM_SOCK_UDP:
417 msg.msg_name = (struct sockaddr *) &sock->sockaddr.addr.sin;
418 msg.msg_namelen = sizeof(sock->sockaddr.addr.sin);
419 break;
420 default:
421 break;
422 }
423
424 do {
425 ret = sendmsg(sock->fd, &msg, flags);
426 } while (ret < 0 && errno == EINTR);
427 if (ret < 0) {
428 /*
429 * Only warn about EPIPE when quiet mode is deactivated.
430 * We consider EPIPE as expected.
431 */
432 if (errno != EPIPE || !lttng_opt_quiet) {
433 PERROR("sendmsg inet");
434 }
435 }
436
437 return ret;
438}
439
440/*
441 * Shutdown cleanly and close.
442 */
90e535ef 443LTTNG_HIDDEN
6364a07a
DG
444int lttcomm_close_inet_sock(struct lttcomm_sock *sock)
445{
6e742359 446 int ret;
6364a07a 447
de5e9086 448 /* Don't try to close an invalid marked socket */
6364a07a
DG
449 if (sock->fd == -1) {
450 return 0;
451 }
452
6e742359
DG
453 ret = close(sock->fd);
454 if (ret) {
6364a07a
DG
455 PERROR("close inet");
456 }
457
458 /* Mark socket */
459 sock->fd = -1;
460
461 return ret;
462}
d831c249
DG
463
464/*
465 * Return value read from /proc or else 0 if value is not found.
466 */
467static unsigned long read_proc_value(const char *path)
468{
469 int ret, fd;
6cd525e8 470 ssize_t size_ret;
d831c249
DG
471 long r_val;
472 unsigned long val = 0;
473 char buf[64];
474
475 fd = open(path, O_RDONLY);
476 if (fd < 0) {
477 goto error;
478 }
479
6cd525e8
MD
480 size_ret = lttng_read(fd, buf, sizeof(buf));
481 /*
482 * Allow reading a file smaller than buf, but keep space for
483 * final \0.
484 */
485 if (size_ret < 0 || size_ret >= sizeof(buf)) {
d831c249
DG
486 PERROR("read proc failed");
487 goto error_close;
488 }
6cd525e8 489 buf[size_ret] = '\0';
d831c249
DG
490
491 errno = 0;
492 r_val = strtol(buf, NULL, 10);
493 if (errno != 0 || r_val < -1L) {
494 val = 0;
495 goto error_close;
496 } else {
497 if (r_val > 0) {
498 val = r_val;
499 }
500 }
501
502error_close:
503 ret = close(fd);
504 if (ret) {
505 PERROR("close /proc value");
506 }
507error:
508 return val;
509}
510
511LTTNG_HIDDEN
512void lttcomm_inet_init(void)
513{
18eed43f
DG
514 unsigned long syn_retries, fin_timeout, syn_timeout, env;
515
516 env = lttcomm_get_network_timeout();
517 if (env) {
518 lttcomm_inet_tcp_timeout = env;
519 goto end;
520 }
d831c249
DG
521
522 /* Assign default value and see if we can change it. */
523 lttcomm_inet_tcp_timeout = DEFAULT_INET_TCP_TIMEOUT;
524
525 syn_retries = read_proc_value(LTTCOMM_INET_PROC_SYN_RETRIES_PATH);
526 fin_timeout = read_proc_value(LTTCOMM_INET_PROC_FIN_TIMEOUT_PATH);
527
528 syn_timeout = syn_retries * LTTCOMM_INET_SYN_TIMEOUT_FACTOR;
529
530 /*
531 * Get the maximum between the two possible timeout value and use that to
532 * get the maximum with the default timeout.
533 */
534 lttcomm_inet_tcp_timeout = max_t(unsigned long,
535 max_t(unsigned long, syn_timeout, fin_timeout),
536 lttcomm_inet_tcp_timeout);
537
18eed43f 538end:
d831c249
DG
539 DBG("TCP inet operation timeout set to %lu sec", lttcomm_inet_tcp_timeout);
540}
This page took 0.060699 seconds and 4 git commands to generate.