| 1 | /* |
| 2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> |
| 3 | * Copyright (C) 2011-2013 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Lesser General Public |
| 7 | * License as published by the Free Software Foundation; only |
| 8 | * version 2.1 of the License. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Lesser General Public |
| 16 | * License along with this library; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #define _GNU_SOURCE |
| 21 | #include <limits.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <sys/socket.h> |
| 26 | #include <sys/stat.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <sys/un.h> |
| 29 | #include <unistd.h> |
| 30 | #include <assert.h> |
| 31 | #include <errno.h> |
| 32 | #include <fcntl.h> |
| 33 | |
| 34 | #include <lttng/ust-ctl.h> |
| 35 | #include <ust-comm.h> |
| 36 | #include <helper.h> |
| 37 | #include <lttng/ust-error.h> |
| 38 | #include <lttng/ust-events.h> |
| 39 | #include <usterr-signal-safe.h> |
| 40 | |
| 41 | #include "../liblttng-ust/compat.h" |
| 42 | |
| 43 | #define USTCOMM_CODE_OFFSET(code) \ |
| 44 | (code == LTTNG_UST_OK ? 0 : (code - LTTNG_UST_ERR + 1)) |
| 45 | |
| 46 | #define USTCOMM_MAX_SEND_FDS 4 |
| 47 | |
| 48 | /* |
| 49 | * Human readable error message. |
| 50 | */ |
| 51 | static const char *ustcomm_readable_code[] = { |
| 52 | [ USTCOMM_CODE_OFFSET(LTTNG_UST_OK) ] = "Success", |
| 53 | [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR) ] = "Unknown error", |
| 54 | [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_NOENT) ] = "No entry", |
| 55 | [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_EXIST) ] = "Object already exists", |
| 56 | [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_INVAL) ] = "Invalid argument", |
| 57 | [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_PERM) ] = "Permission denied", |
| 58 | [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_NOSYS) ] = "Not implemented", |
| 59 | [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_EXITING) ] = "Process is exiting", |
| 60 | |
| 61 | [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_INVAL_MAGIC) ] = "Invalid magic number", |
| 62 | [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_INVAL_SOCKET_TYPE) ] = "Invalid socket type", |
| 63 | [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_UNSUP_MAJOR) ] = "Unsupported major version", |
| 64 | }; |
| 65 | |
| 66 | /* |
| 67 | * lttng_ust_strerror |
| 68 | * |
| 69 | * Receives positive error value. |
| 70 | * Return ptr to string representing a human readable |
| 71 | * error code from the ustcomm_return_code enum. |
| 72 | */ |
| 73 | const char *lttng_ust_strerror(int code) |
| 74 | { |
| 75 | if (code == LTTNG_UST_OK) |
| 76 | return ustcomm_readable_code[USTCOMM_CODE_OFFSET(code)]; |
| 77 | if (code < LTTNG_UST_ERR) |
| 78 | return strerror(code); |
| 79 | if (code >= LTTNG_UST_ERR_NR) |
| 80 | code = LTTNG_UST_ERR; |
| 81 | return ustcomm_readable_code[USTCOMM_CODE_OFFSET(code)]; |
| 82 | } |
| 83 | |
| 84 | /* |
| 85 | * ustcomm_connect_unix_sock |
| 86 | * |
| 87 | * Connect to unix socket using the path name. |
| 88 | */ |
| 89 | int ustcomm_connect_unix_sock(const char *pathname) |
| 90 | { |
| 91 | struct sockaddr_un sun; |
| 92 | int fd, ret; |
| 93 | |
| 94 | /* |
| 95 | * libust threads require the close-on-exec flag for all |
| 96 | * resources so it does not leak file descriptors upon exec. |
| 97 | */ |
| 98 | fd = socket(PF_UNIX, SOCK_STREAM, 0); |
| 99 | if (fd < 0) { |
| 100 | PERROR("socket"); |
| 101 | ret = -errno; |
| 102 | goto error; |
| 103 | } |
| 104 | ret = fcntl(fd, F_SETFD, FD_CLOEXEC); |
| 105 | if (ret < 0) { |
| 106 | PERROR("fcntl"); |
| 107 | ret = -errno; |
| 108 | goto error_fcntl; |
| 109 | } |
| 110 | |
| 111 | memset(&sun, 0, sizeof(sun)); |
| 112 | sun.sun_family = AF_UNIX; |
| 113 | strncpy(sun.sun_path, pathname, sizeof(sun.sun_path)); |
| 114 | sun.sun_path[sizeof(sun.sun_path) - 1] = '\0'; |
| 115 | |
| 116 | ret = connect(fd, (struct sockaddr *) &sun, sizeof(sun)); |
| 117 | if (ret < 0) { |
| 118 | /* |
| 119 | * Don't print message on connect ENOENT error, because |
| 120 | * connect is used in normal execution to detect if |
| 121 | * sessiond is alive. ENOENT is when the unix socket |
| 122 | * file does not exist, and ECONNREFUSED is when the |
| 123 | * file exists but no sessiond is listening. |
| 124 | */ |
| 125 | if (errno != ECONNREFUSED && errno != ECONNRESET |
| 126 | && errno != ENOENT && errno != EACCES) |
| 127 | PERROR("connect"); |
| 128 | ret = -errno; |
| 129 | if (ret == -ECONNREFUSED || ret == -ECONNRESET) |
| 130 | ret = -EPIPE; |
| 131 | goto error_connect; |
| 132 | } |
| 133 | |
| 134 | return fd; |
| 135 | |
| 136 | error_connect: |
| 137 | error_fcntl: |
| 138 | { |
| 139 | int closeret; |
| 140 | |
| 141 | closeret = close(fd); |
| 142 | if (closeret) |
| 143 | PERROR("close"); |
| 144 | } |
| 145 | error: |
| 146 | return ret; |
| 147 | } |
| 148 | |
| 149 | /* |
| 150 | * ustcomm_accept_unix_sock |
| 151 | * |
| 152 | * Do an accept(2) on the sock and return the |
| 153 | * new file descriptor. The socket MUST be bind(2) before. |
| 154 | */ |
| 155 | int ustcomm_accept_unix_sock(int sock) |
| 156 | { |
| 157 | int new_fd; |
| 158 | struct sockaddr_un sun; |
| 159 | socklen_t len = 0; |
| 160 | |
| 161 | /* Blocking call */ |
| 162 | new_fd = accept(sock, (struct sockaddr *) &sun, &len); |
| 163 | if (new_fd < 0) { |
| 164 | if (errno != ECONNABORTED) |
| 165 | PERROR("accept"); |
| 166 | new_fd = -errno; |
| 167 | if (new_fd == -ECONNABORTED) |
| 168 | new_fd = -EPIPE; |
| 169 | } |
| 170 | return new_fd; |
| 171 | } |
| 172 | |
| 173 | /* |
| 174 | * ustcomm_create_unix_sock |
| 175 | * |
| 176 | * Creates a AF_UNIX local socket using pathname |
| 177 | * bind the socket upon creation and return the fd. |
| 178 | */ |
| 179 | int ustcomm_create_unix_sock(const char *pathname) |
| 180 | { |
| 181 | struct sockaddr_un sun; |
| 182 | int fd, ret; |
| 183 | |
| 184 | /* Create server socket */ |
| 185 | if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) { |
| 186 | PERROR("socket"); |
| 187 | ret = -errno; |
| 188 | goto error; |
| 189 | } |
| 190 | |
| 191 | memset(&sun, 0, sizeof(sun)); |
| 192 | sun.sun_family = AF_UNIX; |
| 193 | strncpy(sun.sun_path, pathname, sizeof(sun.sun_path)); |
| 194 | sun.sun_path[sizeof(sun.sun_path) - 1] = '\0'; |
| 195 | |
| 196 | /* Unlink the old file if present */ |
| 197 | (void) unlink(pathname); |
| 198 | ret = bind(fd, (struct sockaddr *) &sun, sizeof(sun)); |
| 199 | if (ret < 0) { |
| 200 | PERROR("bind"); |
| 201 | ret = -errno; |
| 202 | goto error_close; |
| 203 | } |
| 204 | |
| 205 | return fd; |
| 206 | |
| 207 | error_close: |
| 208 | { |
| 209 | int closeret; |
| 210 | |
| 211 | closeret = close(fd); |
| 212 | if (closeret) { |
| 213 | PERROR("close"); |
| 214 | } |
| 215 | } |
| 216 | error: |
| 217 | return ret; |
| 218 | } |
| 219 | |
| 220 | /* |
| 221 | * ustcomm_listen_unix_sock |
| 222 | * |
| 223 | * Make the socket listen using LTTNG_UST_COMM_MAX_LISTEN. |
| 224 | */ |
| 225 | int ustcomm_listen_unix_sock(int sock) |
| 226 | { |
| 227 | int ret; |
| 228 | |
| 229 | ret = listen(sock, LTTNG_UST_COMM_MAX_LISTEN); |
| 230 | if (ret < 0) { |
| 231 | ret = -errno; |
| 232 | PERROR("listen"); |
| 233 | } |
| 234 | |
| 235 | return ret; |
| 236 | } |
| 237 | |
| 238 | /* |
| 239 | * ustcomm_close_unix_sock |
| 240 | * |
| 241 | * Shutdown cleanly a unix socket. |
| 242 | */ |
| 243 | int ustcomm_close_unix_sock(int sock) |
| 244 | { |
| 245 | int ret; |
| 246 | |
| 247 | ret = close(sock); |
| 248 | if (ret < 0) { |
| 249 | PERROR("close"); |
| 250 | ret = -errno; |
| 251 | } |
| 252 | |
| 253 | return ret; |
| 254 | } |
| 255 | |
| 256 | /* |
| 257 | * ustcomm_recv_unix_sock |
| 258 | * |
| 259 | * Receive data of size len in put that data into |
| 260 | * the buf param. Using recvmsg API. |
| 261 | * Return the size of received data. |
| 262 | * Return 0 on orderly shutdown. |
| 263 | */ |
| 264 | ssize_t ustcomm_recv_unix_sock(int sock, void *buf, size_t len) |
| 265 | { |
| 266 | struct msghdr msg; |
| 267 | struct iovec iov[1]; |
| 268 | ssize_t ret = -1; |
| 269 | size_t len_last; |
| 270 | |
| 271 | memset(&msg, 0, sizeof(msg)); |
| 272 | |
| 273 | iov[0].iov_base = buf; |
| 274 | iov[0].iov_len = len; |
| 275 | msg.msg_iov = iov; |
| 276 | msg.msg_iovlen = 1; |
| 277 | |
| 278 | do { |
| 279 | len_last = iov[0].iov_len; |
| 280 | ret = recvmsg(sock, &msg, 0); |
| 281 | if (ret > 0) { |
| 282 | iov[0].iov_base += ret; |
| 283 | iov[0].iov_len -= ret; |
| 284 | assert(ret <= len_last); |
| 285 | } |
| 286 | } while ((ret > 0 && ret < len_last) || (ret < 0 && errno == EINTR)); |
| 287 | |
| 288 | if (ret < 0) { |
| 289 | int shutret; |
| 290 | |
| 291 | if (errno != EPIPE && errno != ECONNRESET && errno != ECONNREFUSED) |
| 292 | PERROR("recvmsg"); |
| 293 | ret = -errno; |
| 294 | if (ret == -ECONNRESET || ret == -ECONNREFUSED) |
| 295 | ret = -EPIPE; |
| 296 | |
| 297 | shutret = shutdown(sock, SHUT_RDWR); |
| 298 | if (shutret) |
| 299 | ERR("Socket shutdown error"); |
| 300 | } else if (ret > 0) { |
| 301 | ret = len; |
| 302 | } |
| 303 | /* ret = 0 means an orderly shutdown. */ |
| 304 | |
| 305 | return ret; |
| 306 | } |
| 307 | |
| 308 | /* |
| 309 | * ustcomm_send_unix_sock |
| 310 | * |
| 311 | * Send buf data of size len. Using sendmsg API. |
| 312 | * Return the size of sent data. |
| 313 | */ |
| 314 | ssize_t ustcomm_send_unix_sock(int sock, const void *buf, size_t len) |
| 315 | { |
| 316 | struct msghdr msg; |
| 317 | struct iovec iov[1]; |
| 318 | ssize_t ret; |
| 319 | |
| 320 | memset(&msg, 0, sizeof(msg)); |
| 321 | |
| 322 | iov[0].iov_base = (void *) buf; |
| 323 | iov[0].iov_len = len; |
| 324 | msg.msg_iov = iov; |
| 325 | msg.msg_iovlen = 1; |
| 326 | |
| 327 | /* |
| 328 | * Using the MSG_NOSIGNAL when sending data from sessiond to |
| 329 | * libust, so libust does not receive an unhandled SIGPIPE or |
| 330 | * SIGURG. The sessiond receiver side can be made more resilient |
| 331 | * by ignoring SIGPIPE, but we don't have this luxury on the |
| 332 | * libust side. |
| 333 | */ |
| 334 | do { |
| 335 | ret = sendmsg(sock, &msg, MSG_NOSIGNAL); |
| 336 | } while (ret < 0 && errno == EINTR); |
| 337 | |
| 338 | if (ret < 0) { |
| 339 | int shutret; |
| 340 | |
| 341 | if (errno != EPIPE && errno != ECONNRESET) |
| 342 | PERROR("sendmsg"); |
| 343 | ret = -errno; |
| 344 | if (ret == -ECONNRESET) |
| 345 | ret = -EPIPE; |
| 346 | |
| 347 | shutret = shutdown(sock, SHUT_RDWR); |
| 348 | if (shutret) |
| 349 | ERR("Socket shutdown error"); |
| 350 | } |
| 351 | |
| 352 | return ret; |
| 353 | } |
| 354 | |
| 355 | /* |
| 356 | * Send a message accompanied by fd(s) over a unix socket. |
| 357 | * |
| 358 | * Returns the size of data sent, or negative error value. |
| 359 | */ |
| 360 | ssize_t ustcomm_send_fds_unix_sock(int sock, int *fds, size_t nb_fd) |
| 361 | { |
| 362 | struct msghdr msg; |
| 363 | struct cmsghdr *cmptr; |
| 364 | struct iovec iov[1]; |
| 365 | ssize_t ret = -1; |
| 366 | unsigned int sizeof_fds = nb_fd * sizeof(int); |
| 367 | char tmp[CMSG_SPACE(sizeof_fds)]; |
| 368 | char dummy = 0; |
| 369 | |
| 370 | memset(&msg, 0, sizeof(msg)); |
| 371 | memset(tmp, 0, CMSG_SPACE(sizeof_fds) * sizeof(char)); |
| 372 | |
| 373 | if (nb_fd > USTCOMM_MAX_SEND_FDS) |
| 374 | return -EINVAL; |
| 375 | |
| 376 | msg.msg_control = (caddr_t)tmp; |
| 377 | msg.msg_controllen = CMSG_LEN(sizeof_fds); |
| 378 | |
| 379 | cmptr = CMSG_FIRSTHDR(&msg); |
| 380 | cmptr->cmsg_level = SOL_SOCKET; |
| 381 | cmptr->cmsg_type = SCM_RIGHTS; |
| 382 | cmptr->cmsg_len = CMSG_LEN(sizeof_fds); |
| 383 | memcpy(CMSG_DATA(cmptr), fds, sizeof_fds); |
| 384 | /* Sum of the length of all control messages in the buffer: */ |
| 385 | msg.msg_controllen = cmptr->cmsg_len; |
| 386 | |
| 387 | iov[0].iov_base = &dummy; |
| 388 | iov[0].iov_len = 1; |
| 389 | msg.msg_iov = iov; |
| 390 | msg.msg_iovlen = 1; |
| 391 | |
| 392 | do { |
| 393 | ret = sendmsg(sock, &msg, 0); |
| 394 | } while (ret < 0 && errno == EINTR); |
| 395 | if (ret < 0) { |
| 396 | /* |
| 397 | * We consider EPIPE and ECONNRESET as expected. |
| 398 | */ |
| 399 | if (errno != EPIPE && errno != ECONNRESET) { |
| 400 | PERROR("sendmsg"); |
| 401 | } |
| 402 | ret = -errno; |
| 403 | if (ret == -ECONNRESET) |
| 404 | ret = -EPIPE; |
| 405 | } |
| 406 | return ret; |
| 407 | } |
| 408 | |
| 409 | /* |
| 410 | * Recv a message accompanied by fd(s) from a unix socket. |
| 411 | * |
| 412 | * Returns the size of received data, or negative error value. |
| 413 | * |
| 414 | * Expect at most "nb_fd" file descriptors. Returns the number of fd |
| 415 | * actually received in nb_fd. |
| 416 | * Returns -EPIPE on orderly shutdown. |
| 417 | */ |
| 418 | ssize_t ustcomm_recv_fds_unix_sock(int sock, int *fds, size_t nb_fd) |
| 419 | { |
| 420 | struct iovec iov[1]; |
| 421 | ssize_t ret = 0; |
| 422 | struct cmsghdr *cmsg; |
| 423 | size_t sizeof_fds = nb_fd * sizeof(int); |
| 424 | char recv_fd[CMSG_SPACE(sizeof_fds)]; |
| 425 | struct msghdr msg; |
| 426 | char dummy; |
| 427 | |
| 428 | memset(&msg, 0, sizeof(msg)); |
| 429 | |
| 430 | /* Prepare to receive the structures */ |
| 431 | iov[0].iov_base = &dummy; |
| 432 | iov[0].iov_len = 1; |
| 433 | msg.msg_iov = iov; |
| 434 | msg.msg_iovlen = 1; |
| 435 | msg.msg_control = recv_fd; |
| 436 | msg.msg_controllen = sizeof(recv_fd); |
| 437 | |
| 438 | do { |
| 439 | ret = recvmsg(sock, &msg, 0); |
| 440 | } while (ret < 0 && errno == EINTR); |
| 441 | if (ret < 0) { |
| 442 | if (errno != EPIPE && errno != ECONNRESET) { |
| 443 | PERROR("recvmsg fds"); |
| 444 | } |
| 445 | ret = -errno; |
| 446 | if (ret == -ECONNRESET) |
| 447 | ret = -EPIPE; |
| 448 | goto end; |
| 449 | } |
| 450 | if (ret == 0) { |
| 451 | /* orderly shutdown */ |
| 452 | ret = -EPIPE; |
| 453 | goto end; |
| 454 | } |
| 455 | if (ret != 1) { |
| 456 | ERR("Error: Received %zd bytes, expected %d\n", |
| 457 | ret, 1); |
| 458 | goto end; |
| 459 | } |
| 460 | if (msg.msg_flags & MSG_CTRUNC) { |
| 461 | ERR("Error: Control message truncated.\n"); |
| 462 | ret = -1; |
| 463 | goto end; |
| 464 | } |
| 465 | cmsg = CMSG_FIRSTHDR(&msg); |
| 466 | if (!cmsg) { |
| 467 | ERR("Error: Invalid control message header\n"); |
| 468 | ret = -1; |
| 469 | goto end; |
| 470 | } |
| 471 | if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) { |
| 472 | ERR("Didn't received any fd\n"); |
| 473 | ret = -1; |
| 474 | goto end; |
| 475 | } |
| 476 | if (cmsg->cmsg_len != CMSG_LEN(sizeof_fds)) { |
| 477 | ERR("Error: Received %zu bytes of ancillary data, expected %zu\n", |
| 478 | (size_t) cmsg->cmsg_len, (size_t) CMSG_LEN(sizeof_fds)); |
| 479 | ret = -1; |
| 480 | goto end; |
| 481 | } |
| 482 | memcpy(fds, CMSG_DATA(cmsg), sizeof_fds); |
| 483 | ret = sizeof_fds; |
| 484 | end: |
| 485 | return ret; |
| 486 | } |
| 487 | |
| 488 | int ustcomm_send_app_msg(int sock, struct ustcomm_ust_msg *lum) |
| 489 | { |
| 490 | ssize_t len; |
| 491 | |
| 492 | len = ustcomm_send_unix_sock(sock, lum, sizeof(*lum)); |
| 493 | switch (len) { |
| 494 | case sizeof(*lum): |
| 495 | break; |
| 496 | default: |
| 497 | if (len < 0) { |
| 498 | return len; |
| 499 | } else { |
| 500 | ERR("incorrect message size: %zd\n", len); |
| 501 | return -EINVAL; |
| 502 | } |
| 503 | } |
| 504 | return 0; |
| 505 | } |
| 506 | |
| 507 | int ustcomm_recv_app_reply(int sock, struct ustcomm_ust_reply *lur, |
| 508 | uint32_t expected_handle, uint32_t expected_cmd) |
| 509 | { |
| 510 | ssize_t len; |
| 511 | |
| 512 | memset(lur, 0, sizeof(*lur)); |
| 513 | len = ustcomm_recv_unix_sock(sock, lur, sizeof(*lur)); |
| 514 | switch (len) { |
| 515 | case 0: /* orderly shutdown */ |
| 516 | return -EPIPE; |
| 517 | case sizeof(*lur): |
| 518 | if (lur->handle != expected_handle) { |
| 519 | ERR("Unexpected result message handle: " |
| 520 | "expected: %u vs received: %u\n", |
| 521 | expected_handle, lur->handle); |
| 522 | return -EINVAL; |
| 523 | } |
| 524 | if (lur->cmd != expected_cmd) { |
| 525 | ERR("Unexpected result message command " |
| 526 | "expected: %u vs received: %u\n", |
| 527 | expected_cmd, lur->cmd); |
| 528 | return -EINVAL; |
| 529 | } |
| 530 | return lur->ret_code; |
| 531 | default: |
| 532 | if (len >= 0) { |
| 533 | ERR("incorrect message size: %zd\n", len); |
| 534 | } |
| 535 | return len; |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | int ustcomm_send_app_cmd(int sock, |
| 540 | struct ustcomm_ust_msg *lum, |
| 541 | struct ustcomm_ust_reply *lur) |
| 542 | { |
| 543 | int ret; |
| 544 | |
| 545 | ret = ustcomm_send_app_msg(sock, lum); |
| 546 | if (ret) |
| 547 | return ret; |
| 548 | ret = ustcomm_recv_app_reply(sock, lur, lum->handle, lum->cmd); |
| 549 | if (ret > 0) |
| 550 | return -EIO; |
| 551 | return ret; |
| 552 | } |
| 553 | |
| 554 | /* |
| 555 | * chan_data is allocated internally if this function returns the |
| 556 | * expected var_len. |
| 557 | */ |
| 558 | ssize_t ustcomm_recv_channel_from_sessiond(int sock, |
| 559 | void **_chan_data, uint64_t var_len, |
| 560 | int *_wakeup_fd) |
| 561 | { |
| 562 | void *chan_data; |
| 563 | ssize_t len, nr_fd; |
| 564 | int wakeup_fd; |
| 565 | |
| 566 | if (var_len > LTTNG_UST_CHANNEL_DATA_MAX_LEN) { |
| 567 | len = -EINVAL; |
| 568 | goto error_check; |
| 569 | } |
| 570 | /* Receive variable length data */ |
| 571 | chan_data = zmalloc(var_len); |
| 572 | if (!chan_data) { |
| 573 | len = -ENOMEM; |
| 574 | goto error_alloc; |
| 575 | } |
| 576 | len = ustcomm_recv_unix_sock(sock, chan_data, var_len); |
| 577 | if (len != var_len) { |
| 578 | goto error_recv; |
| 579 | } |
| 580 | /* recv wakeup fd */ |
| 581 | nr_fd = ustcomm_recv_fds_unix_sock(sock, &wakeup_fd, 1); |
| 582 | if (nr_fd <= 0) { |
| 583 | if (nr_fd < 0) { |
| 584 | len = nr_fd; |
| 585 | goto error_recv; |
| 586 | } else { |
| 587 | len = -EIO; |
| 588 | goto error_recv; |
| 589 | } |
| 590 | } |
| 591 | *_wakeup_fd = wakeup_fd; |
| 592 | *_chan_data = chan_data; |
| 593 | return len; |
| 594 | |
| 595 | error_recv: |
| 596 | free(chan_data); |
| 597 | error_alloc: |
| 598 | error_check: |
| 599 | return len; |
| 600 | } |
| 601 | |
| 602 | int ustcomm_recv_stream_from_sessiond(int sock, |
| 603 | uint64_t *memory_map_size, |
| 604 | int *shm_fd, int *wakeup_fd) |
| 605 | { |
| 606 | ssize_t len; |
| 607 | int ret; |
| 608 | int fds[2]; |
| 609 | |
| 610 | /* recv shm fd and wakeup fd */ |
| 611 | len = ustcomm_recv_fds_unix_sock(sock, fds, 2); |
| 612 | if (len <= 0) { |
| 613 | if (len < 0) { |
| 614 | ret = len; |
| 615 | goto error; |
| 616 | } else { |
| 617 | ret = -EIO; |
| 618 | goto error; |
| 619 | } |
| 620 | } |
| 621 | *shm_fd = fds[0]; |
| 622 | *wakeup_fd = fds[1]; |
| 623 | return 0; |
| 624 | |
| 625 | error: |
| 626 | return ret; |
| 627 | } |
| 628 | |
| 629 | /* |
| 630 | * Returns 0 on success, negative error value on error. |
| 631 | */ |
| 632 | int ustcomm_send_reg_msg(int sock, |
| 633 | enum ustctl_socket_type type, |
| 634 | uint32_t bits_per_long, |
| 635 | uint32_t uint8_t_alignment, |
| 636 | uint32_t uint16_t_alignment, |
| 637 | uint32_t uint32_t_alignment, |
| 638 | uint32_t uint64_t_alignment, |
| 639 | uint32_t long_alignment) |
| 640 | { |
| 641 | ssize_t len; |
| 642 | struct ustctl_reg_msg reg_msg; |
| 643 | |
| 644 | reg_msg.magic = LTTNG_UST_COMM_MAGIC; |
| 645 | reg_msg.major = LTTNG_UST_ABI_MAJOR_VERSION; |
| 646 | reg_msg.minor = LTTNG_UST_ABI_MINOR_VERSION; |
| 647 | reg_msg.pid = getpid(); |
| 648 | reg_msg.ppid = getppid(); |
| 649 | reg_msg.uid = getuid(); |
| 650 | reg_msg.gid = getgid(); |
| 651 | reg_msg.bits_per_long = bits_per_long; |
| 652 | reg_msg.uint8_t_alignment = uint8_t_alignment; |
| 653 | reg_msg.uint16_t_alignment = uint16_t_alignment; |
| 654 | reg_msg.uint32_t_alignment = uint32_t_alignment; |
| 655 | reg_msg.uint64_t_alignment = uint64_t_alignment; |
| 656 | reg_msg.long_alignment = long_alignment; |
| 657 | reg_msg.socket_type = type; |
| 658 | lttng_ust_getprocname(reg_msg.name); |
| 659 | memset(reg_msg.padding, 0, sizeof(reg_msg.padding)); |
| 660 | |
| 661 | len = ustcomm_send_unix_sock(sock, ®_msg, sizeof(reg_msg)); |
| 662 | if (len > 0 && len != sizeof(reg_msg)) |
| 663 | return -EIO; |
| 664 | if (len < 0) |
| 665 | return len; |
| 666 | return 0; |
| 667 | } |
| 668 | |
| 669 | static |
| 670 | int serialize_string_encoding(enum ustctl_string_encodings *ue, |
| 671 | enum lttng_string_encodings le) |
| 672 | { |
| 673 | switch (le) { |
| 674 | case lttng_encode_none: |
| 675 | *ue = ustctl_encode_none; |
| 676 | break; |
| 677 | case lttng_encode_UTF8: |
| 678 | *ue = ustctl_encode_UTF8; |
| 679 | break; |
| 680 | case lttng_encode_ASCII: |
| 681 | *ue = ustctl_encode_ASCII; |
| 682 | break; |
| 683 | default: |
| 684 | return -EINVAL; |
| 685 | } |
| 686 | return 0; |
| 687 | } |
| 688 | |
| 689 | static |
| 690 | int serialize_basic_type(enum ustctl_abstract_types *uatype, |
| 691 | enum lttng_abstract_types atype, |
| 692 | union _ustctl_basic_type *ubt, |
| 693 | const union _lttng_basic_type *lbt) |
| 694 | { |
| 695 | switch (atype) { |
| 696 | case atype_integer: |
| 697 | { |
| 698 | struct ustctl_integer_type *uit; |
| 699 | const struct lttng_integer_type *lit; |
| 700 | |
| 701 | uit = &ubt->integer; |
| 702 | lit = &lbt->integer; |
| 703 | uit->size = lit->size; |
| 704 | uit->signedness = lit->signedness; |
| 705 | uit->reverse_byte_order = lit->reverse_byte_order; |
| 706 | uit->base = lit->base; |
| 707 | if (serialize_string_encoding(&uit->encoding, lit->encoding)) |
| 708 | return -EINVAL; |
| 709 | uit->alignment = lit->alignment; |
| 710 | *uatype = ustctl_atype_integer; |
| 711 | break; |
| 712 | } |
| 713 | case atype_string: |
| 714 | { |
| 715 | if (serialize_string_encoding(&ubt->string.encoding, |
| 716 | lbt->string.encoding)) |
| 717 | return -EINVAL; |
| 718 | *uatype = ustctl_atype_string; |
| 719 | break; |
| 720 | } |
| 721 | case atype_float: |
| 722 | { |
| 723 | struct ustctl_float_type *uft; |
| 724 | const struct lttng_float_type *lft; |
| 725 | |
| 726 | uft = &ubt->_float; |
| 727 | lft = &lbt->_float; |
| 728 | uft->exp_dig = lft->exp_dig; |
| 729 | uft->mant_dig = lft->mant_dig; |
| 730 | uft->alignment = lft->alignment; |
| 731 | uft->reverse_byte_order = lft->reverse_byte_order; |
| 732 | *uatype = ustctl_atype_float; |
| 733 | break; |
| 734 | } |
| 735 | case atype_enum: |
| 736 | case atype_array: |
| 737 | case atype_sequence: |
| 738 | default: |
| 739 | return -EINVAL; |
| 740 | } |
| 741 | return 0; |
| 742 | } |
| 743 | |
| 744 | static |
| 745 | int serialize_one_type(struct ustctl_type *ut, const struct lttng_type *lt) |
| 746 | { |
| 747 | int ret; |
| 748 | |
| 749 | switch (lt->atype) { |
| 750 | case atype_integer: |
| 751 | case atype_float: |
| 752 | case atype_string: |
| 753 | ret = serialize_basic_type(&ut->atype, lt->atype, |
| 754 | &ut->u.basic, <->u.basic); |
| 755 | if (ret) |
| 756 | return ret; |
| 757 | break; |
| 758 | case atype_array: |
| 759 | { |
| 760 | struct ustctl_basic_type *ubt; |
| 761 | const struct lttng_basic_type *lbt; |
| 762 | int ret; |
| 763 | |
| 764 | ubt = &ut->u.array.elem_type; |
| 765 | lbt = <->u.array.elem_type; |
| 766 | ut->u.array.length = lt->u.array.length; |
| 767 | ret = serialize_basic_type(&ubt->atype, lbt->atype, |
| 768 | &ubt->u.basic, &lbt->u.basic); |
| 769 | if (ret) |
| 770 | return -EINVAL; |
| 771 | ut->atype = ustctl_atype_array; |
| 772 | break; |
| 773 | } |
| 774 | case atype_sequence: |
| 775 | { |
| 776 | struct ustctl_basic_type *ubt; |
| 777 | const struct lttng_basic_type *lbt; |
| 778 | int ret; |
| 779 | |
| 780 | ubt = &ut->u.sequence.length_type; |
| 781 | lbt = <->u.sequence.length_type; |
| 782 | ret = serialize_basic_type(&ubt->atype, lbt->atype, |
| 783 | &ubt->u.basic, &lbt->u.basic); |
| 784 | if (ret) |
| 785 | return -EINVAL; |
| 786 | ubt = &ut->u.sequence.elem_type; |
| 787 | lbt = <->u.sequence.elem_type; |
| 788 | ret = serialize_basic_type(&ubt->atype, lbt->atype, |
| 789 | &ubt->u.basic, &lbt->u.basic); |
| 790 | if (ret) |
| 791 | return -EINVAL; |
| 792 | ut->atype = ustctl_atype_sequence; |
| 793 | break; |
| 794 | } |
| 795 | case atype_enum: |
| 796 | default: |
| 797 | return -EINVAL; |
| 798 | } |
| 799 | return 0; |
| 800 | } |
| 801 | |
| 802 | static |
| 803 | int serialize_fields(size_t *_nr_write_fields, |
| 804 | struct ustctl_field **ustctl_fields, |
| 805 | size_t nr_fields, |
| 806 | const struct lttng_event_field *lttng_fields) |
| 807 | { |
| 808 | struct ustctl_field *fields; |
| 809 | int i, ret; |
| 810 | size_t nr_write_fields = 0; |
| 811 | |
| 812 | fields = zmalloc(nr_fields * sizeof(*fields)); |
| 813 | if (!fields) |
| 814 | return -ENOMEM; |
| 815 | |
| 816 | for (i = 0; i < nr_fields; i++) { |
| 817 | struct ustctl_field *f; |
| 818 | const struct lttng_event_field *lf; |
| 819 | |
| 820 | f = &fields[nr_write_fields]; |
| 821 | lf = <tng_fields[i]; |
| 822 | |
| 823 | /* skip 'nowrite' fields */ |
| 824 | if (lf->nowrite) |
| 825 | continue; |
| 826 | strncpy(f->name, lf->name, LTTNG_UST_SYM_NAME_LEN); |
| 827 | f->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0'; |
| 828 | ret = serialize_one_type(&f->type, &lf->type); |
| 829 | if (ret) |
| 830 | goto error_type; |
| 831 | nr_write_fields++; |
| 832 | } |
| 833 | |
| 834 | *_nr_write_fields = nr_write_fields; |
| 835 | *ustctl_fields = fields; |
| 836 | return 0; |
| 837 | |
| 838 | error_type: |
| 839 | free(fields); |
| 840 | return ret; |
| 841 | } |
| 842 | |
| 843 | static |
| 844 | int serialize_ctx_fields(size_t *_nr_write_fields, |
| 845 | struct ustctl_field **ustctl_fields, |
| 846 | size_t nr_fields, |
| 847 | const struct lttng_ctx_field *lttng_fields) |
| 848 | { |
| 849 | struct ustctl_field *fields; |
| 850 | int i, ret; |
| 851 | size_t nr_write_fields = 0; |
| 852 | |
| 853 | fields = zmalloc(nr_fields * sizeof(*fields)); |
| 854 | if (!fields) |
| 855 | return -ENOMEM; |
| 856 | |
| 857 | for (i = 0; i < nr_fields; i++) { |
| 858 | struct ustctl_field *f; |
| 859 | const struct lttng_event_field *lf; |
| 860 | |
| 861 | f = &fields[nr_write_fields]; |
| 862 | lf = <tng_fields[i].event_field; |
| 863 | |
| 864 | /* skip 'nowrite' fields */ |
| 865 | if (lf->nowrite) |
| 866 | continue; |
| 867 | strncpy(f->name, lf->name, LTTNG_UST_SYM_NAME_LEN); |
| 868 | f->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0'; |
| 869 | ret = serialize_one_type(&f->type, &lf->type); |
| 870 | if (ret) |
| 871 | goto error_type; |
| 872 | nr_write_fields++; |
| 873 | } |
| 874 | |
| 875 | *_nr_write_fields = nr_write_fields; |
| 876 | *ustctl_fields = fields; |
| 877 | return 0; |
| 878 | |
| 879 | error_type: |
| 880 | free(fields); |
| 881 | return ret; |
| 882 | } |
| 883 | |
| 884 | /* |
| 885 | * Returns 0 on success, negative error value on error. |
| 886 | */ |
| 887 | int ustcomm_register_event(int sock, |
| 888 | int session_objd, /* session descriptor */ |
| 889 | int channel_objd, /* channel descriptor */ |
| 890 | const char *event_name, /* event name (input) */ |
| 891 | int loglevel, |
| 892 | const char *signature, /* event signature (input) */ |
| 893 | size_t nr_fields, /* fields */ |
| 894 | const struct lttng_event_field *lttng_fields, |
| 895 | const char *model_emf_uri, |
| 896 | uint32_t *id) /* event id (output) */ |
| 897 | { |
| 898 | ssize_t len; |
| 899 | struct { |
| 900 | struct ustcomm_notify_hdr header; |
| 901 | struct ustcomm_notify_event_msg m; |
| 902 | } msg; |
| 903 | struct { |
| 904 | struct ustcomm_notify_hdr header; |
| 905 | struct ustcomm_notify_event_reply r; |
| 906 | } reply; |
| 907 | size_t signature_len, fields_len, model_emf_uri_len; |
| 908 | struct ustctl_field *fields = NULL; |
| 909 | size_t nr_write_fields = 0; |
| 910 | int ret; |
| 911 | |
| 912 | memset(&msg, 0, sizeof(msg)); |
| 913 | msg.header.notify_cmd = USTCTL_NOTIFY_CMD_EVENT; |
| 914 | msg.m.session_objd = session_objd; |
| 915 | msg.m.channel_objd = channel_objd; |
| 916 | strncpy(msg.m.event_name, event_name, LTTNG_UST_SYM_NAME_LEN); |
| 917 | msg.m.event_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0'; |
| 918 | msg.m.loglevel = loglevel; |
| 919 | signature_len = strlen(signature) + 1; |
| 920 | msg.m.signature_len = signature_len; |
| 921 | |
| 922 | /* Calculate fields len, serialize fields. */ |
| 923 | if (nr_fields > 0) { |
| 924 | ret = serialize_fields(&nr_write_fields, &fields, |
| 925 | nr_fields, lttng_fields); |
| 926 | if (ret) |
| 927 | return ret; |
| 928 | } |
| 929 | |
| 930 | fields_len = sizeof(*fields) * nr_write_fields; |
| 931 | msg.m.fields_len = fields_len; |
| 932 | if (model_emf_uri) { |
| 933 | model_emf_uri_len = strlen(model_emf_uri) + 1; |
| 934 | } else { |
| 935 | model_emf_uri_len = 0; |
| 936 | } |
| 937 | msg.m.model_emf_uri_len = model_emf_uri_len; |
| 938 | len = ustcomm_send_unix_sock(sock, &msg, sizeof(msg)); |
| 939 | if (len > 0 && len != sizeof(msg)) { |
| 940 | free(fields); |
| 941 | return -EIO; |
| 942 | } |
| 943 | if (len < 0) { |
| 944 | free(fields); |
| 945 | return len; |
| 946 | } |
| 947 | |
| 948 | /* send signature */ |
| 949 | len = ustcomm_send_unix_sock(sock, signature, signature_len); |
| 950 | if (len > 0 && len != signature_len) { |
| 951 | free(fields); |
| 952 | return -EIO; |
| 953 | } |
| 954 | if (len < 0) { |
| 955 | free(fields); |
| 956 | return len; |
| 957 | } |
| 958 | |
| 959 | /* send fields */ |
| 960 | if (fields_len > 0) { |
| 961 | len = ustcomm_send_unix_sock(sock, fields, fields_len); |
| 962 | free(fields); |
| 963 | if (len > 0 && len != fields_len) { |
| 964 | return -EIO; |
| 965 | } |
| 966 | if (len < 0) { |
| 967 | return len; |
| 968 | } |
| 969 | } else { |
| 970 | free(fields); |
| 971 | } |
| 972 | |
| 973 | if (model_emf_uri_len) { |
| 974 | /* send model_emf_uri */ |
| 975 | len = ustcomm_send_unix_sock(sock, model_emf_uri, |
| 976 | model_emf_uri_len); |
| 977 | if (len > 0 && len != model_emf_uri_len) |
| 978 | return -EIO; |
| 979 | if (len < 0) |
| 980 | return len; |
| 981 | } |
| 982 | |
| 983 | /* receive reply */ |
| 984 | len = ustcomm_recv_unix_sock(sock, &reply, sizeof(reply)); |
| 985 | switch (len) { |
| 986 | case 0: /* orderly shutdown */ |
| 987 | return -EPIPE; |
| 988 | case sizeof(reply): |
| 989 | if (reply.header.notify_cmd != msg.header.notify_cmd) { |
| 990 | ERR("Unexpected result message command " |
| 991 | "expected: %u vs received: %u\n", |
| 992 | msg.header.notify_cmd, reply.header.notify_cmd); |
| 993 | return -EINVAL; |
| 994 | } |
| 995 | if (reply.r.ret_code > 0) |
| 996 | return -EINVAL; |
| 997 | if (reply.r.ret_code < 0) |
| 998 | return reply.r.ret_code; |
| 999 | *id = reply.r.event_id; |
| 1000 | DBG("Sent register event notification for name \"%s\": ret_code %d, event_id %u\n", |
| 1001 | event_name, reply.r.ret_code, reply.r.event_id); |
| 1002 | return 0; |
| 1003 | default: |
| 1004 | if (len < 0) { |
| 1005 | /* Transport level error */ |
| 1006 | if (errno == EPIPE || errno == ECONNRESET) |
| 1007 | len = -errno; |
| 1008 | return len; |
| 1009 | } else { |
| 1010 | ERR("incorrect message size: %zd\n", len); |
| 1011 | return len; |
| 1012 | } |
| 1013 | } |
| 1014 | } |
| 1015 | |
| 1016 | /* |
| 1017 | * Returns 0 on success, negative error value on error. |
| 1018 | * Returns -EPIPE or -ECONNRESET if other end has hung up. |
| 1019 | */ |
| 1020 | int ustcomm_register_channel(int sock, |
| 1021 | int session_objd, /* session descriptor */ |
| 1022 | int channel_objd, /* channel descriptor */ |
| 1023 | size_t nr_ctx_fields, |
| 1024 | const struct lttng_ctx_field *ctx_fields, |
| 1025 | uint32_t *chan_id, /* channel id (output) */ |
| 1026 | int *header_type) /* header type (output) */ |
| 1027 | { |
| 1028 | ssize_t len; |
| 1029 | struct { |
| 1030 | struct ustcomm_notify_hdr header; |
| 1031 | struct ustcomm_notify_channel_msg m; |
| 1032 | } msg; |
| 1033 | struct { |
| 1034 | struct ustcomm_notify_hdr header; |
| 1035 | struct ustcomm_notify_channel_reply r; |
| 1036 | } reply; |
| 1037 | size_t fields_len; |
| 1038 | struct ustctl_field *fields = NULL; |
| 1039 | int ret; |
| 1040 | size_t nr_write_fields = 0; |
| 1041 | |
| 1042 | memset(&msg, 0, sizeof(msg)); |
| 1043 | msg.header.notify_cmd = USTCTL_NOTIFY_CMD_CHANNEL; |
| 1044 | msg.m.session_objd = session_objd; |
| 1045 | msg.m.channel_objd = channel_objd; |
| 1046 | |
| 1047 | /* Calculate fields len, serialize fields. */ |
| 1048 | if (nr_ctx_fields > 0) { |
| 1049 | ret = serialize_ctx_fields(&nr_write_fields, &fields, |
| 1050 | nr_ctx_fields, ctx_fields); |
| 1051 | if (ret) |
| 1052 | return ret; |
| 1053 | } |
| 1054 | |
| 1055 | fields_len = sizeof(*fields) * nr_write_fields; |
| 1056 | msg.m.ctx_fields_len = fields_len; |
| 1057 | len = ustcomm_send_unix_sock(sock, &msg, sizeof(msg)); |
| 1058 | if (len > 0 && len != sizeof(msg)) { |
| 1059 | free(fields); |
| 1060 | return -EIO; |
| 1061 | } |
| 1062 | if (len < 0) { |
| 1063 | free(fields); |
| 1064 | return len; |
| 1065 | } |
| 1066 | |
| 1067 | /* send fields */ |
| 1068 | if (fields_len > 0) { |
| 1069 | len = ustcomm_send_unix_sock(sock, fields, fields_len); |
| 1070 | free(fields); |
| 1071 | if (len > 0 && len != fields_len) { |
| 1072 | return -EIO; |
| 1073 | } |
| 1074 | if (len < 0) { |
| 1075 | return len; |
| 1076 | } |
| 1077 | } else { |
| 1078 | free(fields); |
| 1079 | } |
| 1080 | |
| 1081 | len = ustcomm_recv_unix_sock(sock, &reply, sizeof(reply)); |
| 1082 | switch (len) { |
| 1083 | case 0: /* orderly shutdown */ |
| 1084 | return -EPIPE; |
| 1085 | case sizeof(reply): |
| 1086 | if (reply.header.notify_cmd != msg.header.notify_cmd) { |
| 1087 | ERR("Unexpected result message command " |
| 1088 | "expected: %u vs received: %u\n", |
| 1089 | msg.header.notify_cmd, reply.header.notify_cmd); |
| 1090 | return -EINVAL; |
| 1091 | } |
| 1092 | if (reply.r.ret_code > 0) |
| 1093 | return -EINVAL; |
| 1094 | if (reply.r.ret_code < 0) |
| 1095 | return reply.r.ret_code; |
| 1096 | *chan_id = reply.r.chan_id; |
| 1097 | switch (reply.r.header_type) { |
| 1098 | case 1: |
| 1099 | case 2: |
| 1100 | *header_type = reply.r.header_type; |
| 1101 | break; |
| 1102 | default: |
| 1103 | ERR("Unexpected channel header type %u\n", |
| 1104 | reply.r.header_type); |
| 1105 | return -EINVAL; |
| 1106 | } |
| 1107 | DBG("Sent register channel notification: chan_id %d, header_type %d\n", |
| 1108 | reply.r.chan_id, reply.r.header_type); |
| 1109 | return 0; |
| 1110 | default: |
| 1111 | if (len < 0) { |
| 1112 | /* Transport level error */ |
| 1113 | if (errno == EPIPE || errno == ECONNRESET) |
| 1114 | len = -errno; |
| 1115 | return len; |
| 1116 | } else { |
| 1117 | ERR("incorrect message size: %zd\n", len); |
| 1118 | return len; |
| 1119 | } |
| 1120 | } |
| 1121 | } |
| 1122 | |
| 1123 | /* |
| 1124 | * Set socket reciving timeout. |
| 1125 | */ |
| 1126 | int ustcomm_setsockopt_rcv_timeout(int sock, unsigned int msec) |
| 1127 | { |
| 1128 | int ret; |
| 1129 | struct timeval tv; |
| 1130 | |
| 1131 | tv.tv_sec = msec / 1000; |
| 1132 | tv.tv_usec = (msec * 1000 % 1000000); |
| 1133 | |
| 1134 | ret = setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); |
| 1135 | if (ret < 0) { |
| 1136 | PERROR("setsockopt SO_RCVTIMEO"); |
| 1137 | ret = -errno; |
| 1138 | } |
| 1139 | |
| 1140 | return ret; |
| 1141 | } |
| 1142 | |
| 1143 | /* |
| 1144 | * Set socket sending timeout. |
| 1145 | */ |
| 1146 | int ustcomm_setsockopt_snd_timeout(int sock, unsigned int msec) |
| 1147 | { |
| 1148 | int ret; |
| 1149 | struct timeval tv; |
| 1150 | |
| 1151 | tv.tv_sec = msec / 1000; |
| 1152 | tv.tv_usec = (msec * 1000) % 1000000; |
| 1153 | |
| 1154 | ret = setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)); |
| 1155 | if (ret < 0) { |
| 1156 | PERROR("setsockopt SO_SNDTIMEO"); |
| 1157 | ret = -errno; |
| 1158 | } |
| 1159 | |
| 1160 | return ret; |
| 1161 | } |