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