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