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;
9cc0ed0d 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 }
9cc0ed0d
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 379 if (ret > 0) {
44a2fbf1
JG
380 if (flags & MSG_DONTWAIT) {
381 goto end;
382 }
7c5aef62
DG
383 iov[0].iov_base += ret;
384 iov[0].iov_len -= ret;
385 assert(ret <= len_last);
386 }
387 } while ((ret > 0 && ret < len_last) || (ret < 0 && errno == EINTR));
44a2fbf1 388
6364a07a
DG
389 if (ret < 0) {
390 PERROR("recvmsg inet");
7c5aef62
DG
391 } else if (ret > 0) {
392 ret = len;
6364a07a 393 }
7c5aef62 394 /* Else ret = 0 meaning an orderly shutdown. */
44a2fbf1 395end:
6364a07a
DG
396 return ret;
397}
398
399/*
400 * Send buf data of size len. Using sendmsg API.
401 *
402 * Return the size of sent data.
403 */
90e535ef 404LTTNG_HIDDEN
c2d69327 405ssize_t lttcomm_sendmsg_inet_sock(struct lttcomm_sock *sock, const void *buf,
6364a07a
DG
406 size_t len, int flags)
407{
408 struct msghdr msg;
409 struct iovec iov[1];
410 ssize_t ret = -1;
411
412 memset(&msg, 0, sizeof(msg));
413
c2d69327 414 iov[0].iov_base = (void *) buf;
6364a07a
DG
415 iov[0].iov_len = len;
416 msg.msg_iov = iov;
417 msg.msg_iovlen = 1;
418
419 switch (sock->proto) {
420 case LTTCOMM_SOCK_UDP:
421 msg.msg_name = (struct sockaddr *) &sock->sockaddr.addr.sin;
422 msg.msg_namelen = sizeof(sock->sockaddr.addr.sin);
423 break;
424 default:
425 break;
426 }
427
428 do {
429 ret = sendmsg(sock->fd, &msg, flags);
430 } while (ret < 0 && errno == EINTR);
431 if (ret < 0) {
432 /*
433 * Only warn about EPIPE when quiet mode is deactivated.
434 * We consider EPIPE as expected.
435 */
436 if (errno != EPIPE || !lttng_opt_quiet) {
437 PERROR("sendmsg inet");
438 }
439 }
440
441 return ret;
442}
443
444/*
445 * Shutdown cleanly and close.
446 */
90e535ef 447LTTNG_HIDDEN
6364a07a
DG
448int lttcomm_close_inet_sock(struct lttcomm_sock *sock)
449{
6e742359 450 int ret;
6364a07a 451
de5e9086 452 /* Don't try to close an invalid marked socket */
6364a07a
DG
453 if (sock->fd == -1) {
454 return 0;
455 }
456
6e742359
DG
457 ret = close(sock->fd);
458 if (ret) {
6364a07a
DG
459 PERROR("close inet");
460 }
461
462 /* Mark socket */
463 sock->fd = -1;
464
465 return ret;
466}
d831c249
DG
467
468/*
469 * Return value read from /proc or else 0 if value is not found.
470 */
471static unsigned long read_proc_value(const char *path)
472{
473 int ret, fd;
6cd525e8 474 ssize_t size_ret;
d831c249
DG
475 long r_val;
476 unsigned long val = 0;
477 char buf[64];
478
479 fd = open(path, O_RDONLY);
480 if (fd < 0) {
481 goto error;
482 }
483
6cd525e8
MD
484 size_ret = lttng_read(fd, buf, sizeof(buf));
485 /*
486 * Allow reading a file smaller than buf, but keep space for
487 * final \0.
488 */
489 if (size_ret < 0 || size_ret >= sizeof(buf)) {
d831c249
DG
490 PERROR("read proc failed");
491 goto error_close;
492 }
6cd525e8 493 buf[size_ret] = '\0';
d831c249
DG
494
495 errno = 0;
496 r_val = strtol(buf, NULL, 10);
497 if (errno != 0 || r_val < -1L) {
498 val = 0;
499 goto error_close;
500 } else {
501 if (r_val > 0) {
502 val = r_val;
503 }
504 }
505
506error_close:
507 ret = close(fd);
508 if (ret) {
509 PERROR("close /proc value");
510 }
511error:
512 return val;
513}
514
515LTTNG_HIDDEN
516void lttcomm_inet_init(void)
517{
18eed43f
DG
518 unsigned long syn_retries, fin_timeout, syn_timeout, env;
519
520 env = lttcomm_get_network_timeout();
521 if (env) {
522 lttcomm_inet_tcp_timeout = env;
523 goto end;
524 }
d831c249
DG
525
526 /* Assign default value and see if we can change it. */
527 lttcomm_inet_tcp_timeout = DEFAULT_INET_TCP_TIMEOUT;
528
529 syn_retries = read_proc_value(LTTCOMM_INET_PROC_SYN_RETRIES_PATH);
530 fin_timeout = read_proc_value(LTTCOMM_INET_PROC_FIN_TIMEOUT_PATH);
531
532 syn_timeout = syn_retries * LTTCOMM_INET_SYN_TIMEOUT_FACTOR;
533
534 /*
535 * Get the maximum between the two possible timeout value and use that to
536 * get the maximum with the default timeout.
537 */
538 lttcomm_inet_tcp_timeout = max_t(unsigned long,
539 max_t(unsigned long, syn_timeout, fin_timeout),
540 lttcomm_inet_tcp_timeout);
541
18eed43f 542end:
d831c249
DG
543 DBG("TCP inet operation timeout set to %lu sec", lttcomm_inet_tcp_timeout);
544}
This page took 0.077609 seconds and 5 git commands to generate.