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