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