| 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 | LTTNG_HIDDEN |
| 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 | LTTNG_HIDDEN |
| 90 | int lttcomm_bind_inet_sock(struct lttcomm_sock *sock) |
| 91 | { |
| 92 | return bind(sock->fd, |
| 93 | (const struct sockaddr *) ALIGNED_CONST_PTR( |
| 94 | sock->sockaddr.addr.sin), |
| 95 | sizeof(sock->sockaddr.addr.sin)); |
| 96 | } |
| 97 | |
| 98 | static |
| 99 | int connect_no_timeout(struct lttcomm_sock *sock) |
| 100 | { |
| 101 | return connect(sock->fd, |
| 102 | (const struct sockaddr *) ALIGNED_CONST_PTR( |
| 103 | sock->sockaddr.addr.sin), |
| 104 | sizeof(sock->sockaddr.addr.sin)); |
| 105 | } |
| 106 | |
| 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; |
| 113 | unsigned long diff_ms; |
| 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 | |
| 129 | ret = lttng_clock_gettime(CLOCK_MONOTONIC, &orig_time); |
| 130 | if (ret == -1) { |
| 131 | PERROR("clock_gettime"); |
| 132 | return -1; |
| 133 | } |
| 134 | |
| 135 | connect_ret = connect(sock->fd, |
| 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) { |
| 141 | goto error; |
| 142 | } else if (!connect_ret) { |
| 143 | /* Connect succeeded */ |
| 144 | goto success; |
| 145 | } |
| 146 | |
| 147 | DBG("Asynchronous connect for sock %d, performing polling with" |
| 148 | " timeout: %lums", sock->fd, timeout); |
| 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) { |
| 175 | PERROR("getsockopt"); |
| 176 | goto error; |
| 177 | } |
| 178 | if (!optval) { |
| 179 | connect_ret = 0; |
| 180 | goto success; |
| 181 | } else { |
| 182 | /* Get actual connect() errno from opt_val */ |
| 183 | errno = optval; |
| 184 | goto error; |
| 185 | } |
| 186 | } |
| 187 | /* ret == 0: timeout */ |
| 188 | ret = lttng_clock_gettime(CLOCK_MONOTONIC, &cur_time); |
| 189 | if (ret == -1) { |
| 190 | PERROR("clock_gettime"); |
| 191 | connect_ret = ret; |
| 192 | goto error; |
| 193 | } |
| 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); |
| 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 | |
| 216 | /* |
| 217 | * Connect PF_INET socket. |
| 218 | */ |
| 219 | LTTNG_HIDDEN |
| 220 | int lttcomm_connect_inet_sock(struct lttcomm_sock *sock) |
| 221 | { |
| 222 | int ret, closeret; |
| 223 | |
| 224 | if (lttcomm_get_network_timeout()) { |
| 225 | ret = connect_with_timeout(sock); |
| 226 | } else { |
| 227 | ret = connect_no_timeout(sock); |
| 228 | } |
| 229 | if (ret < 0) { |
| 230 | PERROR("connect"); |
| 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 | */ |
| 249 | LTTNG_HIDDEN |
| 250 | struct lttcomm_sock *lttcomm_accept_inet_sock(struct lttcomm_sock *sock) |
| 251 | { |
| 252 | int new_fd; |
| 253 | socklen_t len; |
| 254 | struct lttcomm_sock *new_sock; |
| 255 | unsigned long timeout; |
| 256 | struct sockaddr_in new_addr = {}; |
| 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 | |
| 266 | new_sock = lttcomm_alloc_sock(sock->proto); |
| 267 | if (new_sock == NULL) { |
| 268 | goto error; |
| 269 | } |
| 270 | |
| 271 | len = sizeof(new_addr); |
| 272 | |
| 273 | /* Blocking call */ |
| 274 | new_fd = accept(sock->fd, (struct sockaddr *) &new_addr, &len); |
| 275 | if (new_fd < 0) { |
| 276 | PERROR("accept inet"); |
| 277 | goto error; |
| 278 | } |
| 279 | new_sock->sockaddr.addr.sin = new_addr; |
| 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) { |
| 286 | goto error_close; |
| 287 | } |
| 288 | ret = lttcomm_setsockopt_snd_timeout(new_fd, timeout); |
| 289 | if (ret) { |
| 290 | goto error_close; |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | new_sock->fd = new_fd; |
| 295 | new_sock->ops = &inet_ops; |
| 296 | |
| 297 | end: |
| 298 | return new_sock; |
| 299 | |
| 300 | error_close: |
| 301 | if (close(new_fd) < 0) { |
| 302 | PERROR("accept inet close fd"); |
| 303 | } |
| 304 | |
| 305 | error: |
| 306 | free(new_sock); |
| 307 | return NULL; |
| 308 | } |
| 309 | |
| 310 | /* |
| 311 | * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN. |
| 312 | */ |
| 313 | LTTNG_HIDDEN |
| 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 | */ |
| 344 | LTTNG_HIDDEN |
| 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; |
| 351 | size_t len_last; |
| 352 | struct sockaddr_in addr = sock->sockaddr.addr.sin; |
| 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 | |
| 361 | msg.msg_name = (struct sockaddr *) &addr; |
| 362 | msg.msg_namelen = sizeof(sock->sockaddr.addr.sin); |
| 363 | |
| 364 | do { |
| 365 | len_last = iov[0].iov_len; |
| 366 | ret = recvmsg(sock->fd, &msg, flags); |
| 367 | if (ret > 0) { |
| 368 | if (flags & MSG_DONTWAIT) { |
| 369 | goto end; |
| 370 | } |
| 371 | iov[0].iov_base += ret; |
| 372 | iov[0].iov_len -= ret; |
| 373 | LTTNG_ASSERT(ret <= len_last); |
| 374 | } |
| 375 | } while ((ret > 0 && ret < len_last) || (ret < 0 && errno == EINTR)); |
| 376 | |
| 377 | if (ret < 0) { |
| 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 | } |
| 387 | PERROR("recvmsg inet"); |
| 388 | } else if (ret > 0) { |
| 389 | ret = len; |
| 390 | } |
| 391 | /* Else ret = 0 meaning an orderly shutdown. */ |
| 392 | end: |
| 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 | */ |
| 401 | LTTNG_HIDDEN |
| 402 | ssize_t lttcomm_sendmsg_inet_sock(struct lttcomm_sock *sock, const void *buf, |
| 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 | |
| 411 | iov[0].iov_base = (void *) buf; |
| 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: |
| 418 | { |
| 419 | struct sockaddr_in addr = sock->sockaddr.addr.sin; |
| 420 | |
| 421 | msg.msg_name = (struct sockaddr *) &addr; |
| 422 | msg.msg_namelen = sizeof(sock->sockaddr.addr.sin); |
| 423 | break; |
| 424 | } |
| 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 | */ |
| 448 | LTTNG_HIDDEN |
| 449 | int lttcomm_close_inet_sock(struct lttcomm_sock *sock) |
| 450 | { |
| 451 | int ret; |
| 452 | |
| 453 | /* Don't try to close an invalid marked socket */ |
| 454 | if (sock->fd == -1) { |
| 455 | return 0; |
| 456 | } |
| 457 | |
| 458 | ret = close(sock->fd); |
| 459 | if (ret) { |
| 460 | PERROR("close inet"); |
| 461 | } |
| 462 | |
| 463 | /* Mark socket */ |
| 464 | sock->fd = -1; |
| 465 | |
| 466 | return ret; |
| 467 | } |
| 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; |
| 475 | ssize_t size_ret; |
| 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 | |
| 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)) { |
| 491 | PERROR("read proc failed"); |
| 492 | goto error_close; |
| 493 | } |
| 494 | buf[size_ret] = '\0'; |
| 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 | { |
| 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 | } |
| 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 | |
| 543 | end: |
| 544 | DBG("TCP inet operation timeout set to %lu sec", lttcomm_inet_tcp_timeout); |
| 545 | } |