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