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