| 1 | /* |
| 2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> |
| 3 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License, version 2 only, |
| 7 | * as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 12 | * more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License along |
| 15 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 17 | */ |
| 18 | |
| 19 | #define _LGPL_SOURCE |
| 20 | #include <assert.h> |
| 21 | #include <limits.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <sys/stat.h> |
| 26 | #include <sys/types.h> |
| 27 | #include <unistd.h> |
| 28 | #include <errno.h> |
| 29 | |
| 30 | #include <common/common.h> |
| 31 | #include <common/sessiond-comm/sessiond-comm.h> |
| 32 | |
| 33 | #include "unix.h" |
| 34 | |
| 35 | /* |
| 36 | * Connect to unix socket using the path name. |
| 37 | */ |
| 38 | LTTNG_HIDDEN |
| 39 | int lttcomm_connect_unix_sock(const char *pathname) |
| 40 | { |
| 41 | struct sockaddr_un s_un; |
| 42 | int fd, ret, closeret; |
| 43 | |
| 44 | if (strlen(pathname) >= sizeof(s_un.sun_path)) { |
| 45 | ERR("unix socket address (\"%s\") is longer than the platform's limit (%zu > %zu).", |
| 46 | pathname, strlen(pathname) + 1, |
| 47 | sizeof(s_un.sun_path)); |
| 48 | ret = -ENAMETOOLONG; |
| 49 | goto error; |
| 50 | } |
| 51 | |
| 52 | fd = socket(PF_UNIX, SOCK_STREAM, 0); |
| 53 | if (fd < 0) { |
| 54 | PERROR("socket"); |
| 55 | ret = fd; |
| 56 | goto error; |
| 57 | } |
| 58 | |
| 59 | memset(&s_un, 0, sizeof(s_un)); |
| 60 | s_un.sun_family = AF_UNIX; |
| 61 | strncpy(s_un.sun_path, pathname, sizeof(s_un.sun_path)); |
| 62 | s_un.sun_path[sizeof(s_un.sun_path) - 1] = '\0'; |
| 63 | |
| 64 | ret = connect(fd, (struct sockaddr *) &s_un, sizeof(s_un)); |
| 65 | if (ret < 0) { |
| 66 | /* |
| 67 | * Don't print message on connect error, because connect is used in |
| 68 | * normal execution to detect if sessiond is alive. |
| 69 | */ |
| 70 | goto error_connect; |
| 71 | } |
| 72 | |
| 73 | return fd; |
| 74 | |
| 75 | error_connect: |
| 76 | closeret = close(fd); |
| 77 | if (closeret) { |
| 78 | PERROR("close"); |
| 79 | } |
| 80 | error: |
| 81 | return ret; |
| 82 | } |
| 83 | |
| 84 | /* |
| 85 | * Do an accept(2) on the sock and return the new file descriptor. The socket |
| 86 | * MUST be bind(2) before. |
| 87 | */ |
| 88 | LTTNG_HIDDEN |
| 89 | int lttcomm_accept_unix_sock(int sock) |
| 90 | { |
| 91 | int new_fd; |
| 92 | struct sockaddr_un s_un; |
| 93 | socklen_t len = sizeof(s_un); |
| 94 | |
| 95 | /* Blocking call */ |
| 96 | new_fd = accept(sock, (struct sockaddr *) &s_un, &len); |
| 97 | if (new_fd < 0) { |
| 98 | PERROR("accept"); |
| 99 | } |
| 100 | |
| 101 | return new_fd; |
| 102 | } |
| 103 | |
| 104 | LTTNG_HIDDEN |
| 105 | int lttcomm_create_anon_unix_socketpair(int *fds) |
| 106 | { |
| 107 | if (socketpair(PF_UNIX, SOCK_STREAM, 0, fds) < 0) { |
| 108 | PERROR("socketpair"); |
| 109 | return -1; |
| 110 | } |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | /* |
| 115 | * Creates a AF_UNIX local socket using pathname bind the socket upon creation |
| 116 | * and return the fd. |
| 117 | */ |
| 118 | LTTNG_HIDDEN |
| 119 | int lttcomm_create_unix_sock(const char *pathname) |
| 120 | { |
| 121 | struct sockaddr_un s_un; |
| 122 | int fd = -1; |
| 123 | int ret = -1; |
| 124 | |
| 125 | if (strlen(pathname) >= sizeof(s_un.sun_path)) { |
| 126 | ERR("unix socket address (\"%s\") is longer than the platform's limit (%zu > %zu).", |
| 127 | pathname, strlen(pathname) + 1, |
| 128 | sizeof(s_un.sun_path)); |
| 129 | ret = -ENAMETOOLONG; |
| 130 | goto error; |
| 131 | } |
| 132 | |
| 133 | /* Create server socket */ |
| 134 | if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) { |
| 135 | PERROR("socket"); |
| 136 | goto error; |
| 137 | } |
| 138 | |
| 139 | memset(&s_un, 0, sizeof(s_un)); |
| 140 | s_un.sun_family = AF_UNIX; |
| 141 | strncpy(s_un.sun_path, pathname, sizeof(s_un.sun_path)); |
| 142 | s_un.sun_path[sizeof(s_un.sun_path) - 1] = '\0'; |
| 143 | |
| 144 | /* Unlink the old file if present */ |
| 145 | (void) unlink(pathname); |
| 146 | ret = bind(fd, (struct sockaddr *) &s_un, sizeof(s_un)); |
| 147 | if (ret < 0) { |
| 148 | PERROR("bind"); |
| 149 | goto error; |
| 150 | } |
| 151 | |
| 152 | return fd; |
| 153 | |
| 154 | error: |
| 155 | if (fd >= 0) { |
| 156 | if (close(fd) < 0) { |
| 157 | PERROR("close create unix sock"); |
| 158 | } |
| 159 | } |
| 160 | return ret; |
| 161 | } |
| 162 | |
| 163 | /* |
| 164 | * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN. |
| 165 | */ |
| 166 | LTTNG_HIDDEN |
| 167 | int lttcomm_listen_unix_sock(int sock) |
| 168 | { |
| 169 | int ret; |
| 170 | |
| 171 | ret = listen(sock, LTTNG_SESSIOND_COMM_MAX_LISTEN); |
| 172 | if (ret < 0) { |
| 173 | PERROR("listen"); |
| 174 | } |
| 175 | |
| 176 | return ret; |
| 177 | } |
| 178 | |
| 179 | /* |
| 180 | * Receive data of size len in put that data into the buf param. Using recvmsg |
| 181 | * API. |
| 182 | * |
| 183 | * Return the size of received data. |
| 184 | */ |
| 185 | LTTNG_HIDDEN |
| 186 | ssize_t lttcomm_recv_unix_sock(int sock, void *buf, size_t len) |
| 187 | { |
| 188 | struct msghdr msg; |
| 189 | struct iovec iov[1]; |
| 190 | ssize_t ret = -1; |
| 191 | size_t len_last; |
| 192 | |
| 193 | memset(&msg, 0, sizeof(msg)); |
| 194 | |
| 195 | iov[0].iov_base = buf; |
| 196 | iov[0].iov_len = len; |
| 197 | msg.msg_iov = iov; |
| 198 | msg.msg_iovlen = 1; |
| 199 | |
| 200 | do { |
| 201 | len_last = iov[0].iov_len; |
| 202 | ret = lttng_recvmsg_nosigpipe(sock, &msg); |
| 203 | if (ret > 0) { |
| 204 | iov[0].iov_base += ret; |
| 205 | iov[0].iov_len -= ret; |
| 206 | assert(ret <= len_last); |
| 207 | } |
| 208 | } while ((ret > 0 && ret < len_last) || (ret < 0 && errno == EINTR)); |
| 209 | if (ret < 0) { |
| 210 | PERROR("recvmsg"); |
| 211 | } else if (ret > 0) { |
| 212 | ret = len; |
| 213 | } |
| 214 | /* Else ret = 0 meaning an orderly shutdown. */ |
| 215 | |
| 216 | return ret; |
| 217 | } |
| 218 | |
| 219 | /* |
| 220 | * Receive data of size len in put that data into the buf param. Using recvmsg |
| 221 | * API. Only use with sockets set in non-blocking mode. |
| 222 | * |
| 223 | * Return the size of received data. |
| 224 | */ |
| 225 | LTTNG_HIDDEN |
| 226 | ssize_t lttcomm_recv_unix_sock_non_block(int sock, void *buf, size_t len) |
| 227 | { |
| 228 | struct msghdr msg; |
| 229 | struct iovec iov[1]; |
| 230 | ssize_t ret; |
| 231 | |
| 232 | memset(&msg, 0, sizeof(msg)); |
| 233 | |
| 234 | iov[0].iov_base = buf; |
| 235 | iov[0].iov_len = len; |
| 236 | msg.msg_iov = iov; |
| 237 | msg.msg_iovlen = 1; |
| 238 | |
| 239 | retry: |
| 240 | ret = lttng_recvmsg_nosigpipe(sock, &msg); |
| 241 | if (ret < 0) { |
| 242 | if (errno == EINTR) { |
| 243 | goto retry; |
| 244 | } else { |
| 245 | /* |
| 246 | * Only warn about EPIPE when quiet mode is |
| 247 | * deactivated. |
| 248 | * We consider EPIPE as expected. |
| 249 | */ |
| 250 | if (errno != EPIPE || !lttng_opt_quiet) { |
| 251 | PERROR("recvmsg"); |
| 252 | } |
| 253 | goto end; |
| 254 | } |
| 255 | } |
| 256 | ret = len; |
| 257 | end: |
| 258 | return ret; |
| 259 | } |
| 260 | |
| 261 | /* |
| 262 | * Send buf data of size len. Using sendmsg API. |
| 263 | * |
| 264 | * Return the size of sent data. |
| 265 | */ |
| 266 | LTTNG_HIDDEN |
| 267 | ssize_t lttcomm_send_unix_sock(int sock, const void *buf, size_t len) |
| 268 | { |
| 269 | struct msghdr msg; |
| 270 | struct iovec iov[1]; |
| 271 | ssize_t ret; |
| 272 | |
| 273 | memset(&msg, 0, sizeof(msg)); |
| 274 | |
| 275 | iov[0].iov_base = (void *) buf; |
| 276 | iov[0].iov_len = len; |
| 277 | msg.msg_iov = iov; |
| 278 | msg.msg_iovlen = 1; |
| 279 | |
| 280 | while (iov[0].iov_len) { |
| 281 | ret = sendmsg(sock, &msg, 0); |
| 282 | if (ret < 0) { |
| 283 | if (errno == EINTR) { |
| 284 | continue; |
| 285 | } else { |
| 286 | /* |
| 287 | * Only warn about EPIPE when quiet mode is |
| 288 | * deactivated. |
| 289 | * We consider EPIPE as expected. |
| 290 | */ |
| 291 | if (errno != EPIPE || !lttng_opt_quiet) { |
| 292 | PERROR("sendmsg"); |
| 293 | } |
| 294 | goto end; |
| 295 | } |
| 296 | } |
| 297 | iov[0].iov_len -= ret; |
| 298 | iov[0].iov_base += ret; |
| 299 | } |
| 300 | ret = len; |
| 301 | end: |
| 302 | return ret; |
| 303 | } |
| 304 | |
| 305 | /* |
| 306 | * Send buf data of size len. Using sendmsg API. |
| 307 | * Only use with non-blocking sockets. The difference with the blocking version |
| 308 | * of the function is that this one does not retry to send on partial sends, |
| 309 | * except if the interruption was caused by a signal (EINTR). |
| 310 | * |
| 311 | * Return the size of sent data. |
| 312 | */ |
| 313 | LTTNG_HIDDEN |
| 314 | ssize_t lttcomm_send_unix_sock_non_block(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 | retry: |
| 328 | ret = sendmsg(sock, &msg, 0); |
| 329 | if (ret < 0) { |
| 330 | if (errno == EINTR) { |
| 331 | goto retry; |
| 332 | } else { |
| 333 | /* |
| 334 | * Only warn about EPIPE when quiet mode is |
| 335 | * deactivated. |
| 336 | * We consider EPIPE as expected. |
| 337 | */ |
| 338 | if (errno != EPIPE || !lttng_opt_quiet) { |
| 339 | PERROR("sendmsg"); |
| 340 | } |
| 341 | goto end; |
| 342 | } |
| 343 | } |
| 344 | ret = len; |
| 345 | end: |
| 346 | return ret; |
| 347 | } |
| 348 | |
| 349 | /* |
| 350 | * Shutdown cleanly a unix socket. |
| 351 | */ |
| 352 | LTTNG_HIDDEN |
| 353 | int lttcomm_close_unix_sock(int sock) |
| 354 | { |
| 355 | int ret, closeret; |
| 356 | |
| 357 | /* Shutdown receptions and transmissions */ |
| 358 | ret = shutdown(sock, SHUT_RDWR); |
| 359 | if (ret < 0) { |
| 360 | PERROR("shutdown"); |
| 361 | } |
| 362 | |
| 363 | closeret = close(sock); |
| 364 | if (closeret) { |
| 365 | PERROR("close"); |
| 366 | } |
| 367 | |
| 368 | return ret; |
| 369 | } |
| 370 | |
| 371 | /* |
| 372 | * Send a message accompanied by fd(s) over a unix socket. |
| 373 | * |
| 374 | * Returns the size of data sent, or negative error value. |
| 375 | */ |
| 376 | LTTNG_HIDDEN |
| 377 | ssize_t lttcomm_send_fds_unix_sock(int sock, int *fds, size_t nb_fd) |
| 378 | { |
| 379 | struct msghdr msg; |
| 380 | struct cmsghdr *cmptr; |
| 381 | struct iovec iov[1]; |
| 382 | ssize_t ret = -1; |
| 383 | unsigned int sizeof_fds = nb_fd * sizeof(int); |
| 384 | char tmp[CMSG_SPACE(sizeof_fds)]; |
| 385 | char dummy = 0; |
| 386 | |
| 387 | memset(&msg, 0, sizeof(msg)); |
| 388 | memset(tmp, 0, CMSG_SPACE(sizeof_fds) * sizeof(char)); |
| 389 | |
| 390 | if (nb_fd > LTTCOMM_MAX_SEND_FDS) |
| 391 | return -EINVAL; |
| 392 | |
| 393 | msg.msg_control = (caddr_t)tmp; |
| 394 | msg.msg_controllen = CMSG_LEN(sizeof_fds); |
| 395 | |
| 396 | cmptr = CMSG_FIRSTHDR(&msg); |
| 397 | if (!cmptr) { |
| 398 | return -1; |
| 399 | } |
| 400 | cmptr->cmsg_level = SOL_SOCKET; |
| 401 | cmptr->cmsg_type = SCM_RIGHTS; |
| 402 | cmptr->cmsg_len = CMSG_LEN(sizeof_fds); |
| 403 | memcpy(CMSG_DATA(cmptr), fds, sizeof_fds); |
| 404 | /* Sum of the length of all control messages in the buffer: */ |
| 405 | msg.msg_controllen = cmptr->cmsg_len; |
| 406 | |
| 407 | iov[0].iov_base = &dummy; |
| 408 | iov[0].iov_len = 1; |
| 409 | msg.msg_iov = iov; |
| 410 | msg.msg_iovlen = 1; |
| 411 | |
| 412 | do { |
| 413 | ret = sendmsg(sock, &msg, 0); |
| 414 | } while (ret < 0 && errno == EINTR); |
| 415 | if (ret < 0) { |
| 416 | /* |
| 417 | * Only warn about EPIPE when quiet mode is deactivated. |
| 418 | * We consider EPIPE as expected. |
| 419 | */ |
| 420 | if (errno != EPIPE || !lttng_opt_quiet) { |
| 421 | PERROR("sendmsg"); |
| 422 | } |
| 423 | } |
| 424 | return ret; |
| 425 | } |
| 426 | |
| 427 | /* |
| 428 | * Recv a message accompanied by fd(s) from a unix socket. |
| 429 | * |
| 430 | * Returns the size of received data, or negative error value. |
| 431 | * |
| 432 | * Expect at most "nb_fd" file descriptors. Returns the number of fd |
| 433 | * actually received in nb_fd. |
| 434 | */ |
| 435 | LTTNG_HIDDEN |
| 436 | ssize_t lttcomm_recv_fds_unix_sock(int sock, int *fds, size_t nb_fd) |
| 437 | { |
| 438 | struct iovec iov[1]; |
| 439 | ssize_t ret = 0; |
| 440 | struct cmsghdr *cmsg; |
| 441 | size_t sizeof_fds = nb_fd * sizeof(int); |
| 442 | char recv_fd[CMSG_SPACE(sizeof_fds)]; |
| 443 | struct msghdr msg; |
| 444 | char dummy; |
| 445 | |
| 446 | memset(&msg, 0, sizeof(msg)); |
| 447 | |
| 448 | /* Prepare to receive the structures */ |
| 449 | iov[0].iov_base = &dummy; |
| 450 | iov[0].iov_len = 1; |
| 451 | msg.msg_iov = iov; |
| 452 | msg.msg_iovlen = 1; |
| 453 | msg.msg_control = recv_fd; |
| 454 | msg.msg_controllen = sizeof(recv_fd); |
| 455 | |
| 456 | do { |
| 457 | ret = recvmsg(sock, &msg, 0); |
| 458 | } while (ret < 0 && errno == EINTR); |
| 459 | if (ret < 0) { |
| 460 | PERROR("recvmsg fds"); |
| 461 | goto end; |
| 462 | } |
| 463 | if (ret != 1) { |
| 464 | fprintf(stderr, "Error: Received %zd bytes, expected %d\n", |
| 465 | ret, 1); |
| 466 | goto end; |
| 467 | } |
| 468 | if (msg.msg_flags & MSG_CTRUNC) { |
| 469 | fprintf(stderr, "Error: Control message truncated.\n"); |
| 470 | ret = -1; |
| 471 | goto end; |
| 472 | } |
| 473 | cmsg = CMSG_FIRSTHDR(&msg); |
| 474 | if (!cmsg) { |
| 475 | fprintf(stderr, "Error: Invalid control message header\n"); |
| 476 | ret = -1; |
| 477 | goto end; |
| 478 | } |
| 479 | if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) { |
| 480 | fprintf(stderr, "Didn't received any fd\n"); |
| 481 | ret = -1; |
| 482 | goto end; |
| 483 | } |
| 484 | if (cmsg->cmsg_len != CMSG_LEN(sizeof_fds)) { |
| 485 | fprintf(stderr, "Error: Received %zu bytes of ancillary data, expected %zu\n", |
| 486 | (size_t) cmsg->cmsg_len, (size_t) CMSG_LEN(sizeof_fds)); |
| 487 | ret = -1; |
| 488 | goto end; |
| 489 | } |
| 490 | memcpy(fds, CMSG_DATA(cmsg), sizeof_fds); |
| 491 | ret = sizeof_fds; |
| 492 | end: |
| 493 | return ret; |
| 494 | } |
| 495 | |
| 496 | /* |
| 497 | * Send a message with credentials over a unix socket. |
| 498 | * |
| 499 | * Returns the size of data sent, or negative error value. |
| 500 | */ |
| 501 | LTTNG_HIDDEN |
| 502 | ssize_t lttcomm_send_creds_unix_sock(int sock, void *buf, size_t len) |
| 503 | { |
| 504 | struct msghdr msg; |
| 505 | struct iovec iov[1]; |
| 506 | ssize_t ret = -1; |
| 507 | #ifdef __linux__ |
| 508 | struct cmsghdr *cmptr; |
| 509 | size_t sizeof_cred = sizeof(lttng_sock_cred); |
| 510 | char anc_buf[CMSG_SPACE(sizeof_cred)]; |
| 511 | lttng_sock_cred *creds; |
| 512 | |
| 513 | memset(anc_buf, 0, CMSG_SPACE(sizeof_cred) * sizeof(char)); |
| 514 | #endif /* __linux__ */ |
| 515 | |
| 516 | memset(&msg, 0, sizeof(msg)); |
| 517 | |
| 518 | iov[0].iov_base = buf; |
| 519 | iov[0].iov_len = len; |
| 520 | msg.msg_iov = iov; |
| 521 | msg.msg_iovlen = 1; |
| 522 | |
| 523 | #ifdef __linux__ |
| 524 | msg.msg_control = (caddr_t) anc_buf; |
| 525 | msg.msg_controllen = CMSG_LEN(sizeof_cred); |
| 526 | |
| 527 | cmptr = CMSG_FIRSTHDR(&msg); |
| 528 | if (!cmptr) { |
| 529 | return -1; |
| 530 | } |
| 531 | cmptr->cmsg_level = SOL_SOCKET; |
| 532 | cmptr->cmsg_type = LTTNG_SOCK_CREDS; |
| 533 | cmptr->cmsg_len = CMSG_LEN(sizeof_cred); |
| 534 | |
| 535 | creds = (lttng_sock_cred*) CMSG_DATA(cmptr); |
| 536 | |
| 537 | LTTNG_SOCK_SET_UID_CRED(creds, geteuid()); |
| 538 | LTTNG_SOCK_SET_GID_CRED(creds, getegid()); |
| 539 | LTTNG_SOCK_SET_PID_CRED(creds, getpid()); |
| 540 | #endif /* __linux__ */ |
| 541 | |
| 542 | do { |
| 543 | ret = sendmsg(sock, &msg, 0); |
| 544 | } while (ret < 0 && errno == EINTR); |
| 545 | if (ret < 0) { |
| 546 | /* |
| 547 | * Only warn about EPIPE when quiet mode is deactivated. |
| 548 | * We consider EPIPE as expected. |
| 549 | */ |
| 550 | if (errno != EPIPE || !lttng_opt_quiet) { |
| 551 | PERROR("sendmsg"); |
| 552 | } |
| 553 | } |
| 554 | return ret; |
| 555 | } |
| 556 | |
| 557 | /* |
| 558 | * Recv a message accompanied with credentials from a unix socket. |
| 559 | * |
| 560 | * Returns the size of received data, or negative error value. |
| 561 | */ |
| 562 | LTTNG_HIDDEN |
| 563 | ssize_t lttcomm_recv_creds_unix_sock(int sock, void *buf, size_t len, |
| 564 | lttng_sock_cred *creds) |
| 565 | { |
| 566 | struct msghdr msg; |
| 567 | struct iovec iov[1]; |
| 568 | ssize_t ret; |
| 569 | size_t len_last; |
| 570 | #ifdef __linux__ |
| 571 | struct cmsghdr *cmptr; |
| 572 | size_t sizeof_cred = sizeof(lttng_sock_cred); |
| 573 | char anc_buf[CMSG_SPACE(sizeof_cred)]; |
| 574 | #endif /* __linux__ */ |
| 575 | |
| 576 | memset(&msg, 0, sizeof(msg)); |
| 577 | |
| 578 | /* Not allowed */ |
| 579 | if (creds == NULL) { |
| 580 | ret = -1; |
| 581 | goto end; |
| 582 | } |
| 583 | |
| 584 | /* Prepare to receive the structures */ |
| 585 | iov[0].iov_base = buf; |
| 586 | iov[0].iov_len = len; |
| 587 | msg.msg_iov = iov; |
| 588 | msg.msg_iovlen = 1; |
| 589 | |
| 590 | #ifdef __linux__ |
| 591 | msg.msg_control = anc_buf; |
| 592 | msg.msg_controllen = sizeof(anc_buf); |
| 593 | #endif /* __linux__ */ |
| 594 | |
| 595 | do { |
| 596 | len_last = iov[0].iov_len; |
| 597 | ret = recvmsg(sock, &msg, 0); |
| 598 | if (ret > 0) { |
| 599 | iov[0].iov_base += ret; |
| 600 | iov[0].iov_len -= ret; |
| 601 | assert(ret <= len_last); |
| 602 | } |
| 603 | } while ((ret > 0 && ret < len_last) || (ret < 0 && errno == EINTR)); |
| 604 | if (ret < 0) { |
| 605 | PERROR("recvmsg fds"); |
| 606 | goto end; |
| 607 | } else if (ret > 0) { |
| 608 | ret = len; |
| 609 | } |
| 610 | /* Else ret = 0 meaning an orderly shutdown. */ |
| 611 | |
| 612 | #ifdef __linux__ |
| 613 | if (msg.msg_flags & MSG_CTRUNC) { |
| 614 | fprintf(stderr, "Error: Control message truncated.\n"); |
| 615 | ret = -1; |
| 616 | goto end; |
| 617 | } |
| 618 | |
| 619 | cmptr = CMSG_FIRSTHDR(&msg); |
| 620 | if (cmptr == NULL) { |
| 621 | fprintf(stderr, "Error: Invalid control message header\n"); |
| 622 | ret = -1; |
| 623 | goto end; |
| 624 | } |
| 625 | |
| 626 | if (cmptr->cmsg_level != SOL_SOCKET || |
| 627 | cmptr->cmsg_type != LTTNG_SOCK_CREDS) { |
| 628 | fprintf(stderr, "Didn't received any credentials\n"); |
| 629 | ret = -1; |
| 630 | goto end; |
| 631 | } |
| 632 | |
| 633 | if (cmptr->cmsg_len != CMSG_LEN(sizeof_cred)) { |
| 634 | fprintf(stderr, "Error: Received %zu bytes of ancillary data, expected %zu\n", |
| 635 | (size_t) cmptr->cmsg_len, (size_t) CMSG_LEN(sizeof_cred)); |
| 636 | ret = -1; |
| 637 | goto end; |
| 638 | } |
| 639 | |
| 640 | memcpy(creds, CMSG_DATA(cmptr), sizeof_cred); |
| 641 | #elif (defined(__FreeBSD__) || defined(__CYGWIN__) || defined(__sun__) || defined(__APPLE__)) |
| 642 | { |
| 643 | int peer_ret; |
| 644 | |
| 645 | peer_ret = getpeereid(sock, &creds->uid, &creds->gid); |
| 646 | if (peer_ret != 0) { |
| 647 | return peer_ret; |
| 648 | } |
| 649 | } |
| 650 | #else |
| 651 | #error "Please implement credential support for your OS." |
| 652 | #endif /* __linux__ */ |
| 653 | |
| 654 | end: |
| 655 | return ret; |
| 656 | } |
| 657 | |
| 658 | /* |
| 659 | * Set socket option to use credentials passing. |
| 660 | */ |
| 661 | #ifdef __linux__ |
| 662 | LTTNG_HIDDEN |
| 663 | int lttcomm_setsockopt_creds_unix_sock(int sock) |
| 664 | { |
| 665 | int ret, on = 1; |
| 666 | |
| 667 | /* Set socket for credentials retrieval */ |
| 668 | ret = setsockopt(sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)); |
| 669 | if (ret < 0) { |
| 670 | PERROR("setsockopt creds unix sock"); |
| 671 | } |
| 672 | return ret; |
| 673 | } |
| 674 | #elif (defined(__FreeBSD__) || defined(__CYGWIN__) || defined(__sun__) || defined(__APPLE__)) |
| 675 | LTTNG_HIDDEN |
| 676 | int lttcomm_setsockopt_creds_unix_sock(int sock) |
| 677 | { |
| 678 | return 0; |
| 679 | } |
| 680 | #else |
| 681 | #error "Please implement credential support for your OS." |
| 682 | #endif /* __linux__ */ |