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