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