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