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