| 1 | /* Copyright (C) 2009 Pierre-Marc Fournier |
| 2 | * |
| 3 | * This library is free software; you can redistribute it and/or |
| 4 | * modify it under the terms of the GNU Lesser General Public |
| 5 | * License as published by the Free Software Foundation; either |
| 6 | * version 2.1 of the License, or (at your option) any later version. |
| 7 | * |
| 8 | * This library is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * Lesser General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU Lesser General Public |
| 14 | * License along with this library; if not, write to the Free Software |
| 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 16 | */ |
| 17 | |
| 18 | /* API used by UST components to communicate with each other via sockets. */ |
| 19 | |
| 20 | #define _GNU_SOURCE |
| 21 | #include <dirent.h> |
| 22 | #include <sys/types.h> |
| 23 | #include <signal.h> |
| 24 | #include <errno.h> |
| 25 | #include <limits.h> |
| 26 | #include <sys/socket.h> |
| 27 | #include <sys/un.h> |
| 28 | #include <unistd.h> |
| 29 | #include <poll.h> |
| 30 | #include <sys/epoll.h> |
| 31 | #include <sys/stat.h> |
| 32 | |
| 33 | #include <stdio.h> |
| 34 | #include <stdlib.h> |
| 35 | #include <string.h> |
| 36 | #include <execinfo.h> |
| 37 | |
| 38 | #include "ustcomm.h" |
| 39 | #include "usterr.h" |
| 40 | #include "share.h" |
| 41 | |
| 42 | static int mkdir_p(const char *path, mode_t mode) |
| 43 | { |
| 44 | const char *path_p; |
| 45 | char *tmp; |
| 46 | |
| 47 | int retval = 0; |
| 48 | int result; |
| 49 | mode_t old_umask; |
| 50 | |
| 51 | tmp = zmalloc(strlen(path) + 1); |
| 52 | if (tmp == NULL) |
| 53 | return -1; |
| 54 | |
| 55 | /* skip first / */ |
| 56 | path_p = path+1; |
| 57 | |
| 58 | old_umask = umask(0); |
| 59 | for(;;) { |
| 60 | while (*path_p != '/') { |
| 61 | if(*path_p == 0) |
| 62 | break; |
| 63 | ++path_p; |
| 64 | } |
| 65 | if (*path_p == '/') { |
| 66 | strncpy(tmp, path, path_p - path); |
| 67 | tmp[path_p-path] = '\0'; |
| 68 | if (tmp[path_p - path - 1] != '/') { |
| 69 | result = mkdir(tmp, mode); |
| 70 | if(result == -1) { |
| 71 | if (!(errno == EEXIST || errno == EACCES || errno == EROFS)) { |
| 72 | /* Then this is a real error */ |
| 73 | retval = -1; |
| 74 | break; |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | /* pass / */ |
| 79 | path_p++; |
| 80 | } else { |
| 81 | /* last component */ |
| 82 | result = mkdir(path, mode); |
| 83 | if (result == -1) |
| 84 | retval = -1; |
| 85 | break; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | free(tmp); |
| 90 | umask(old_umask); |
| 91 | return retval; |
| 92 | } |
| 93 | |
| 94 | static struct sockaddr_un * create_sock_addr(const char *name, |
| 95 | size_t *sock_addr_size) |
| 96 | { |
| 97 | struct sockaddr_un * addr; |
| 98 | size_t alloc_size; |
| 99 | |
| 100 | alloc_size = (size_t) (((struct sockaddr_un *) 0)->sun_path) + |
| 101 | strlen(name) + 1; |
| 102 | |
| 103 | addr = malloc(alloc_size); |
| 104 | if (addr < 0) { |
| 105 | ERR("allocating addr failed"); |
| 106 | return NULL; |
| 107 | } |
| 108 | |
| 109 | addr->sun_family = AF_UNIX; |
| 110 | strcpy(addr->sun_path, name); |
| 111 | |
| 112 | *sock_addr_size = alloc_size; |
| 113 | |
| 114 | return addr; |
| 115 | } |
| 116 | |
| 117 | struct ustcomm_sock * ustcomm_init_sock(int fd, int epoll_fd, |
| 118 | struct cds_list_head *list) |
| 119 | { |
| 120 | struct epoll_event ev; |
| 121 | struct ustcomm_sock *sock; |
| 122 | |
| 123 | sock = malloc(sizeof(struct ustcomm_sock)); |
| 124 | if (!sock) { |
| 125 | perror("malloc: couldn't allocate ustcomm_sock"); |
| 126 | return NULL; |
| 127 | } |
| 128 | |
| 129 | ev.events = EPOLLIN; |
| 130 | ev.data.ptr = sock; |
| 131 | sock->fd = fd; |
| 132 | |
| 133 | if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, sock->fd, &ev) == -1) { |
| 134 | perror("epoll_ctl: failed to add socket\n"); |
| 135 | free(sock); |
| 136 | return NULL; |
| 137 | } |
| 138 | |
| 139 | sock->epoll_fd = epoll_fd; |
| 140 | if (list) { |
| 141 | cds_list_add(&sock->list, list); |
| 142 | } else { |
| 143 | CDS_INIT_LIST_HEAD(&sock->list); |
| 144 | } |
| 145 | |
| 146 | return sock; |
| 147 | } |
| 148 | |
| 149 | void ustcomm_del_sock(struct ustcomm_sock *sock, int keep_in_epoll) |
| 150 | { |
| 151 | cds_list_del(&sock->list); |
| 152 | if (!keep_in_epoll) { |
| 153 | if (epoll_ctl(sock->epoll_fd, EPOLL_CTL_DEL, sock->fd, NULL) == -1) { |
| 154 | PERROR("epoll_ctl: failed to delete socket"); |
| 155 | } |
| 156 | } |
| 157 | close(sock->fd); |
| 158 | free(sock); |
| 159 | } |
| 160 | |
| 161 | struct ustcomm_sock * ustcomm_init_named_socket(const char *name, |
| 162 | int epoll_fd) |
| 163 | { |
| 164 | int result; |
| 165 | int fd; |
| 166 | size_t sock_addr_size; |
| 167 | struct sockaddr_un * addr; |
| 168 | struct ustcomm_sock *sock; |
| 169 | |
| 170 | fd = socket(PF_UNIX, SOCK_STREAM, 0); |
| 171 | if(fd == -1) { |
| 172 | PERROR("socket"); |
| 173 | return NULL; |
| 174 | } |
| 175 | |
| 176 | addr = create_sock_addr(name, &sock_addr_size); |
| 177 | if (addr == NULL) { |
| 178 | ERR("allocating addr, UST thread bailing"); |
| 179 | goto close_sock; |
| 180 | } |
| 181 | |
| 182 | result = access(name, F_OK); |
| 183 | if(result == 0) { |
| 184 | /* file exists */ |
| 185 | result = unlink(name); |
| 186 | if(result == -1) { |
| 187 | PERROR("unlink of socket file"); |
| 188 | goto free_addr; |
| 189 | } |
| 190 | DBG("socket already exists; overwriting"); |
| 191 | } |
| 192 | |
| 193 | result = bind(fd, (struct sockaddr *)addr, sock_addr_size); |
| 194 | if(result == -1) { |
| 195 | PERROR("bind"); |
| 196 | goto free_addr; |
| 197 | } |
| 198 | |
| 199 | result = listen(fd, 1); |
| 200 | if(result == -1) { |
| 201 | PERROR("listen"); |
| 202 | goto free_addr; |
| 203 | } |
| 204 | |
| 205 | sock = ustcomm_init_sock(fd, epoll_fd, |
| 206 | NULL); |
| 207 | if (!sock) { |
| 208 | ERR("failed to create ustcomm_sock"); |
| 209 | goto free_addr; |
| 210 | } |
| 211 | |
| 212 | free(addr); |
| 213 | |
| 214 | return sock; |
| 215 | |
| 216 | free_addr: |
| 217 | free(addr); |
| 218 | close_sock: |
| 219 | close(fd); |
| 220 | |
| 221 | return NULL; |
| 222 | } |
| 223 | |
| 224 | void ustcomm_del_named_sock(struct ustcomm_sock *sock, |
| 225 | int keep_socket_file) |
| 226 | { |
| 227 | int result, fd; |
| 228 | struct stat st; |
| 229 | struct sockaddr dummy; |
| 230 | struct sockaddr_un *sockaddr = NULL; |
| 231 | int alloc_size; |
| 232 | |
| 233 | fd = sock->fd; |
| 234 | |
| 235 | if(!keep_socket_file) { |
| 236 | |
| 237 | /* Get the socket name */ |
| 238 | alloc_size = sizeof(dummy); |
| 239 | if (getsockname(fd, &dummy, (socklen_t *)&alloc_size) < 0) { |
| 240 | PERROR("getsockname failed"); |
| 241 | goto del_sock; |
| 242 | } |
| 243 | |
| 244 | sockaddr = zmalloc(alloc_size); |
| 245 | if (!sockaddr) { |
| 246 | ERR("failed to allocate sockaddr"); |
| 247 | goto del_sock; |
| 248 | } |
| 249 | |
| 250 | if (getsockname(fd, sockaddr, (socklen_t *)&alloc_size) < 0) { |
| 251 | PERROR("getsockname failed"); |
| 252 | goto free_sockaddr; |
| 253 | } |
| 254 | |
| 255 | /* Destroy socket */ |
| 256 | result = stat(sockaddr->sun_path, &st); |
| 257 | if(result < 0) { |
| 258 | PERROR("stat (%s)", sockaddr->sun_path); |
| 259 | goto free_sockaddr; |
| 260 | } |
| 261 | |
| 262 | /* Paranoid check before deleting. */ |
| 263 | result = S_ISSOCK(st.st_mode); |
| 264 | if(!result) { |
| 265 | ERR("The socket we are about to delete is not a socket."); |
| 266 | goto free_sockaddr; |
| 267 | } |
| 268 | |
| 269 | result = unlink(sockaddr->sun_path); |
| 270 | if(result < 0) { |
| 271 | PERROR("unlink"); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | free_sockaddr: |
| 276 | free(sockaddr); |
| 277 | |
| 278 | del_sock: |
| 279 | ustcomm_del_sock(sock, keep_socket_file); |
| 280 | } |
| 281 | |
| 282 | int ustcomm_recv_alloc(int sock, |
| 283 | struct ustcomm_header *header, |
| 284 | char **data) { |
| 285 | int result; |
| 286 | struct ustcomm_header peek_header; |
| 287 | struct iovec iov[2]; |
| 288 | struct msghdr msg; |
| 289 | |
| 290 | /* Just to make the caller fail hard */ |
| 291 | *data = NULL; |
| 292 | |
| 293 | result = recv(sock, &peek_header, sizeof(peek_header), |
| 294 | MSG_PEEK | MSG_WAITALL); |
| 295 | if (result <= 0) { |
| 296 | if(errno == ECONNRESET) { |
| 297 | return 0; |
| 298 | } else if (errno == EINTR) { |
| 299 | return -1; |
| 300 | } else if (result < 0) { |
| 301 | PERROR("recv"); |
| 302 | return -1; |
| 303 | } |
| 304 | return 0; |
| 305 | } |
| 306 | |
| 307 | memset(&msg, 0, sizeof(msg)); |
| 308 | |
| 309 | iov[0].iov_base = (char *)header; |
| 310 | iov[0].iov_len = sizeof(struct ustcomm_header); |
| 311 | |
| 312 | msg.msg_iov = iov; |
| 313 | msg.msg_iovlen = 1; |
| 314 | |
| 315 | if (peek_header.size) { |
| 316 | *data = zmalloc(peek_header.size); |
| 317 | if (!*data) { |
| 318 | return -ENOMEM; |
| 319 | } |
| 320 | |
| 321 | iov[1].iov_base = *data; |
| 322 | iov[1].iov_len = peek_header.size; |
| 323 | |
| 324 | msg.msg_iovlen++; |
| 325 | } |
| 326 | |
| 327 | result = recvmsg(sock, &msg, MSG_WAITALL); |
| 328 | if (result < 0) { |
| 329 | free(*data); |
| 330 | PERROR("recvmsg failed"); |
| 331 | } |
| 332 | |
| 333 | return result; |
| 334 | } |
| 335 | |
| 336 | /* returns 1 to indicate a message was received |
| 337 | * returns 0 to indicate no message was received (end of stream) |
| 338 | * returns -1 to indicate an error |
| 339 | */ |
| 340 | int ustcomm_recv_fd(int sock, |
| 341 | struct ustcomm_header *header, |
| 342 | char *data, int *fd) |
| 343 | { |
| 344 | int result; |
| 345 | struct ustcomm_header peek_header; |
| 346 | struct iovec iov[2]; |
| 347 | struct msghdr msg; |
| 348 | struct cmsghdr *cmsg; |
| 349 | char buf[CMSG_SPACE(sizeof(int))]; |
| 350 | |
| 351 | result = recv(sock, &peek_header, sizeof(peek_header), |
| 352 | MSG_PEEK | MSG_WAITALL); |
| 353 | if (result <= 0) { |
| 354 | if(errno == ECONNRESET) { |
| 355 | return 0; |
| 356 | } else if (errno == EINTR) { |
| 357 | return -1; |
| 358 | } else if (result < 0) { |
| 359 | PERROR("recv"); |
| 360 | return -1; |
| 361 | } |
| 362 | return 0; |
| 363 | } |
| 364 | |
| 365 | memset(&msg, 0, sizeof(msg)); |
| 366 | |
| 367 | iov[0].iov_base = (char *)header; |
| 368 | iov[0].iov_len = sizeof(struct ustcomm_header); |
| 369 | |
| 370 | msg.msg_iov = iov; |
| 371 | msg.msg_iovlen = 1; |
| 372 | |
| 373 | if (peek_header.size && data) { |
| 374 | if (peek_header.size < 0 || |
| 375 | peek_header.size > USTCOMM_DATA_SIZE) { |
| 376 | ERR("big peek header! %ld", peek_header.size); |
| 377 | return 0; |
| 378 | } |
| 379 | |
| 380 | iov[1].iov_base = data; |
| 381 | iov[1].iov_len = peek_header.size; |
| 382 | |
| 383 | msg.msg_iovlen++; |
| 384 | } |
| 385 | |
| 386 | if (fd && peek_header.fd_included) { |
| 387 | msg.msg_control = buf; |
| 388 | msg.msg_controllen = sizeof(buf); |
| 389 | } |
| 390 | |
| 391 | result = recvmsg(sock, &msg, MSG_WAITALL); |
| 392 | if (result <= 0) { |
| 393 | if (result < 0) { |
| 394 | PERROR("recvmsg failed"); |
| 395 | } |
| 396 | return result; |
| 397 | } |
| 398 | |
| 399 | if (fd && peek_header.fd_included) { |
| 400 | cmsg = CMSG_FIRSTHDR(&msg); |
| 401 | result = 0; |
| 402 | while (cmsg != NULL) { |
| 403 | if (cmsg->cmsg_level == SOL_SOCKET |
| 404 | && cmsg->cmsg_type == SCM_RIGHTS) { |
| 405 | *fd = *(int *) CMSG_DATA(cmsg); |
| 406 | result = 1; |
| 407 | break; |
| 408 | } |
| 409 | cmsg = CMSG_NXTHDR(&msg, cmsg); |
| 410 | } |
| 411 | if (!result) { |
| 412 | ERR("Failed to receive file descriptor\n"); |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | return 1; |
| 417 | } |
| 418 | |
| 419 | int ustcomm_recv(int sock, |
| 420 | struct ustcomm_header *header, |
| 421 | char *data) |
| 422 | { |
| 423 | return ustcomm_recv_fd(sock, header, data, NULL); |
| 424 | } |
| 425 | |
| 426 | |
| 427 | int ustcomm_send_fd(int sock, |
| 428 | const struct ustcomm_header *header, |
| 429 | const char *data, |
| 430 | int *fd) |
| 431 | { |
| 432 | struct iovec iov[2]; |
| 433 | struct msghdr msg; |
| 434 | int result; |
| 435 | struct cmsghdr *cmsg; |
| 436 | char buf[CMSG_SPACE(sizeof(int))]; |
| 437 | |
| 438 | memset(&msg, 0, sizeof(msg)); |
| 439 | |
| 440 | iov[0].iov_base = (char *)header; |
| 441 | iov[0].iov_len = sizeof(struct ustcomm_header); |
| 442 | |
| 443 | msg.msg_iov = iov; |
| 444 | msg.msg_iovlen = 1; |
| 445 | |
| 446 | if (header->size && data) { |
| 447 | iov[1].iov_base = (char *)data; |
| 448 | iov[1].iov_len = header->size; |
| 449 | |
| 450 | msg.msg_iovlen++; |
| 451 | |
| 452 | } |
| 453 | |
| 454 | if (fd && header->fd_included) { |
| 455 | msg.msg_control = buf; |
| 456 | msg.msg_controllen = sizeof(buf); |
| 457 | cmsg = CMSG_FIRSTHDR(&msg); |
| 458 | cmsg->cmsg_level = SOL_SOCKET; |
| 459 | cmsg->cmsg_type = SCM_RIGHTS; |
| 460 | cmsg->cmsg_len = CMSG_LEN(sizeof(int)); |
| 461 | *(int *) CMSG_DATA(cmsg) = *fd; |
| 462 | msg.msg_controllen = cmsg->cmsg_len; |
| 463 | } |
| 464 | |
| 465 | result = sendmsg(sock, &msg, MSG_NOSIGNAL); |
| 466 | if (result < 0 && errno != EPIPE) { |
| 467 | PERROR("sendmsg failed"); |
| 468 | } |
| 469 | return result; |
| 470 | } |
| 471 | |
| 472 | int ustcomm_send(int sock, |
| 473 | const struct ustcomm_header *header, |
| 474 | const char *data) |
| 475 | { |
| 476 | return ustcomm_send_fd(sock, header, data, NULL); |
| 477 | } |
| 478 | |
| 479 | int ustcomm_req(int sock, |
| 480 | const struct ustcomm_header *req_header, |
| 481 | const char *req_data, |
| 482 | struct ustcomm_header *res_header, |
| 483 | char *res_data) |
| 484 | { |
| 485 | int result; |
| 486 | |
| 487 | result = ustcomm_send(sock, req_header, req_data); |
| 488 | if ( result <= 0) { |
| 489 | return result; |
| 490 | } |
| 491 | |
| 492 | return ustcomm_recv(sock, res_header, res_data); |
| 493 | } |
| 494 | |
| 495 | /* Return value: |
| 496 | * 0: success |
| 497 | * -1: error |
| 498 | */ |
| 499 | |
| 500 | int ustcomm_connect_path(const char *name, int *connection_fd) |
| 501 | { |
| 502 | int result, fd; |
| 503 | size_t sock_addr_size; |
| 504 | struct sockaddr_un *addr; |
| 505 | |
| 506 | fd = socket(PF_UNIX, SOCK_STREAM, 0); |
| 507 | if(fd == -1) { |
| 508 | PERROR("socket"); |
| 509 | return -1; |
| 510 | } |
| 511 | |
| 512 | addr = create_sock_addr(name, &sock_addr_size); |
| 513 | if (addr == NULL) { |
| 514 | ERR("allocating addr failed"); |
| 515 | goto close_sock; |
| 516 | } |
| 517 | |
| 518 | result = connect(fd, (struct sockaddr *)addr, sock_addr_size); |
| 519 | if(result == -1) { |
| 520 | PERROR("connect (path=%s)", name); |
| 521 | goto free_sock_addr; |
| 522 | } |
| 523 | |
| 524 | *connection_fd = fd; |
| 525 | |
| 526 | free(addr); |
| 527 | |
| 528 | return 0; |
| 529 | |
| 530 | free_sock_addr: |
| 531 | free(addr); |
| 532 | close_sock: |
| 533 | close(fd); |
| 534 | |
| 535 | return -1; |
| 536 | } |
| 537 | |
| 538 | /* Returns the current users socket directory, must be freed */ |
| 539 | char *ustcomm_user_sock_dir(void) |
| 540 | { |
| 541 | int result; |
| 542 | char *sock_dir = NULL; |
| 543 | |
| 544 | result = asprintf(&sock_dir, "%s%s", USER_SOCK_DIR, |
| 545 | cuserid(NULL)); |
| 546 | if (result < 0) { |
| 547 | ERR("string overflow allocating directory name"); |
| 548 | return NULL; |
| 549 | } |
| 550 | |
| 551 | return sock_dir; |
| 552 | } |
| 553 | |
| 554 | static int time_and_pid_from_socket_name(char *sock_name, unsigned long *time, |
| 555 | pid_t *pid) |
| 556 | { |
| 557 | char *saveptr, *pid_m_time_str; |
| 558 | char *sock_basename = strdup(basename(sock_name)); |
| 559 | |
| 560 | if (!sock_basename) { |
| 561 | return -1; |
| 562 | } |
| 563 | |
| 564 | /* This is the pid */ |
| 565 | pid_m_time_str = strtok_r(sock_basename, ".", &saveptr); |
| 566 | if (!pid_m_time_str) { |
| 567 | goto out_err; |
| 568 | } |
| 569 | |
| 570 | errno = 0; |
| 571 | *pid = (pid_t)strtoul(pid_m_time_str, NULL, 10); |
| 572 | if (errno) { |
| 573 | goto out_err; |
| 574 | } |
| 575 | |
| 576 | /* This should be the time-stamp */ |
| 577 | pid_m_time_str = strtok_r(NULL, ".", &saveptr); |
| 578 | if (!pid_m_time_str) { |
| 579 | goto out_err; |
| 580 | } |
| 581 | |
| 582 | errno = 0; |
| 583 | *time = strtoul(pid_m_time_str, NULL, 10); |
| 584 | if (errno) { |
| 585 | goto out_err; |
| 586 | } |
| 587 | |
| 588 | return 0; |
| 589 | |
| 590 | out_err: |
| 591 | free(sock_basename); |
| 592 | return -1; |
| 593 | } |
| 594 | |
| 595 | time_t ustcomm_pid_st_mtime(pid_t pid) |
| 596 | { |
| 597 | struct stat proc_stat; |
| 598 | char proc_name[PATH_MAX]; |
| 599 | |
| 600 | if (snprintf(proc_name, PATH_MAX - 1, "/proc/%ld", (long) pid) < 0) { |
| 601 | return 0; |
| 602 | } |
| 603 | |
| 604 | if (stat(proc_name, &proc_stat)) { |
| 605 | return 0; |
| 606 | } |
| 607 | |
| 608 | return proc_stat.st_mtime; |
| 609 | } |
| 610 | |
| 611 | int ustcomm_is_socket_live(char *sock_name, pid_t *read_pid) |
| 612 | { |
| 613 | time_t time_from_pid; |
| 614 | unsigned long time_from_sock; |
| 615 | pid_t pid; |
| 616 | |
| 617 | if (time_and_pid_from_socket_name(sock_name, &time_from_sock, &pid)) { |
| 618 | return 0; |
| 619 | } |
| 620 | |
| 621 | if (read_pid) { |
| 622 | *read_pid = pid; |
| 623 | } |
| 624 | |
| 625 | time_from_pid = ustcomm_pid_st_mtime(pid); |
| 626 | if (!time_from_pid) { |
| 627 | return 0; |
| 628 | } |
| 629 | |
| 630 | if ((unsigned long) time_from_pid == time_from_sock) { |
| 631 | return 1; |
| 632 | } |
| 633 | |
| 634 | return 0; |
| 635 | } |
| 636 | |
| 637 | #define MAX_SOCK_PATH_BASE_LEN 100 |
| 638 | |
| 639 | static int ustcomm_get_sock_name(char *dir_name, pid_t pid, char *sock_name) |
| 640 | { |
| 641 | struct dirent *dirent; |
| 642 | char sock_path_base[MAX_SOCK_PATH_BASE_LEN]; |
| 643 | int len; |
| 644 | DIR *dir = opendir(dir_name); |
| 645 | |
| 646 | snprintf(sock_path_base, MAX_SOCK_PATH_BASE_LEN - 1, |
| 647 | "%ld.", (long) pid); |
| 648 | len = strlen(sock_path_base); |
| 649 | |
| 650 | while ((dirent = readdir(dir))) { |
| 651 | if (!strcmp(dirent->d_name, ".") || |
| 652 | !strcmp(dirent->d_name, "..") || |
| 653 | !strcmp(dirent->d_name, "ust-consumer") || |
| 654 | dirent->d_type == DT_DIR || |
| 655 | strncmp(dirent->d_name, sock_path_base, len)) { |
| 656 | continue; |
| 657 | } |
| 658 | |
| 659 | if (ustcomm_is_socket_live(dirent->d_name, NULL)) { |
| 660 | if (snprintf(sock_name, PATH_MAX - 1, "%s/%s", |
| 661 | dir_name, dirent->d_name) < 0) { |
| 662 | PERROR("path longer than PATH_MAX?"); |
| 663 | goto out_err; |
| 664 | } |
| 665 | closedir(dir); |
| 666 | return 0; |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | out_err: |
| 671 | closedir(dir); |
| 672 | return -1; |
| 673 | } |
| 674 | |
| 675 | /* Open a connection to a traceable app. |
| 676 | * |
| 677 | * Return value: |
| 678 | * 0: success |
| 679 | * -1: error |
| 680 | */ |
| 681 | |
| 682 | static int connect_app_non_root(pid_t pid, int *app_fd) |
| 683 | { |
| 684 | int result; |
| 685 | int retval = 0; |
| 686 | char *dir_name; |
| 687 | char sock_name[PATH_MAX]; |
| 688 | |
| 689 | dir_name = ustcomm_user_sock_dir(); |
| 690 | if (!dir_name) |
| 691 | return -ENOMEM; |
| 692 | |
| 693 | if (ustcomm_get_sock_name(dir_name, pid, sock_name)) { |
| 694 | retval = -ENOENT; |
| 695 | goto free_dir_name; |
| 696 | } |
| 697 | |
| 698 | result = ustcomm_connect_path(sock_name, app_fd); |
| 699 | if (result < 0) { |
| 700 | ERR("failed to connect to app"); |
| 701 | retval = -1; |
| 702 | goto free_dir_name; |
| 703 | } |
| 704 | |
| 705 | free_dir_name: |
| 706 | free(dir_name); |
| 707 | |
| 708 | return retval; |
| 709 | } |
| 710 | |
| 711 | |
| 712 | |
| 713 | static int connect_app_root(pid_t pid, int *app_fd) |
| 714 | { |
| 715 | DIR *tmp_dir; |
| 716 | struct dirent *dirent; |
| 717 | char dir_name[PATH_MAX], sock_name[PATH_MAX]; |
| 718 | int result = -1; |
| 719 | |
| 720 | tmp_dir = opendir(USER_TMP_DIR); |
| 721 | if (!tmp_dir) { |
| 722 | return -1; |
| 723 | } |
| 724 | |
| 725 | while ((dirent = readdir(tmp_dir))) { |
| 726 | if (!strncmp(dirent->d_name, USER_SOCK_DIR_BASE, |
| 727 | strlen(USER_SOCK_DIR_BASE))) { |
| 728 | |
| 729 | if (snprintf(dir_name, PATH_MAX - 1, "%s/%s", USER_TMP_DIR, |
| 730 | dirent->d_name) < 0) { |
| 731 | continue; |
| 732 | } |
| 733 | |
| 734 | if (ustcomm_get_sock_name(dir_name, pid, sock_name)) { |
| 735 | continue; |
| 736 | } |
| 737 | |
| 738 | result = ustcomm_connect_path(sock_name, app_fd); |
| 739 | |
| 740 | if (result == 0) { |
| 741 | goto close_tmp_dir; |
| 742 | } |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | close_tmp_dir: |
| 747 | closedir(tmp_dir); |
| 748 | |
| 749 | return result; |
| 750 | } |
| 751 | |
| 752 | int ustcomm_connect_app(pid_t pid, int *app_fd) |
| 753 | { |
| 754 | *app_fd = 0; |
| 755 | |
| 756 | if (geteuid()) { |
| 757 | return connect_app_non_root(pid, app_fd); |
| 758 | } else { |
| 759 | return connect_app_root(pid, app_fd); |
| 760 | } |
| 761 | |
| 762 | } |
| 763 | |
| 764 | int ensure_dir_exists(const char *dir, mode_t mode) |
| 765 | { |
| 766 | struct stat st; |
| 767 | int result; |
| 768 | |
| 769 | if (!strcmp(dir, "")) |
| 770 | return -1; |
| 771 | |
| 772 | result = stat(dir, &st); |
| 773 | if (result < 0 && errno != ENOENT) { |
| 774 | return -1; |
| 775 | } else if (result < 0) { |
| 776 | /* ENOENT */ |
| 777 | int result; |
| 778 | |
| 779 | result = mkdir_p(dir, mode); |
| 780 | if(result != 0) { |
| 781 | ERR("executing in recursive creation of directory %s", dir); |
| 782 | return -1; |
| 783 | } |
| 784 | } else { |
| 785 | if (st.st_mode != mode) { |
| 786 | result = chmod(dir, mode); |
| 787 | if (result < 0) { |
| 788 | ERR("couldn't set directory mode on %s", dir); |
| 789 | return -1; |
| 790 | } |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | return 0; |
| 795 | } |
| 796 | |
| 797 | char * ustcomm_print_data(char *data_field, int field_size, |
| 798 | int *offset, const char *format, ...) |
| 799 | { |
| 800 | va_list args; |
| 801 | int count, limit; |
| 802 | char *ptr = USTCOMM_POISON_PTR; |
| 803 | |
| 804 | limit = field_size - *offset; |
| 805 | va_start(args, format); |
| 806 | count = vsnprintf(&data_field[*offset], limit, format, args); |
| 807 | va_end(args); |
| 808 | |
| 809 | if (count < limit && count > -1) { |
| 810 | ptr = NULL + *offset; |
| 811 | *offset = *offset + count + 1; |
| 812 | } |
| 813 | |
| 814 | return ptr; |
| 815 | } |
| 816 | |
| 817 | char * ustcomm_restore_ptr(char *ptr, char *data_field, int data_field_size) |
| 818 | { |
| 819 | if ((unsigned long)ptr > data_field_size || |
| 820 | ptr == USTCOMM_POISON_PTR) { |
| 821 | return NULL; |
| 822 | } |
| 823 | |
| 824 | return data_field + (long)ptr; |
| 825 | } |
| 826 | |
| 827 | int ustcomm_pack_single_field(struct ustcomm_header *header, |
| 828 | struct ustcomm_single_field *single_field, |
| 829 | const char *string) |
| 830 | { |
| 831 | int offset = 0; |
| 832 | |
| 833 | single_field->field = ustcomm_print_data(single_field->data, |
| 834 | sizeof(single_field->data), |
| 835 | &offset, |
| 836 | string); |
| 837 | |
| 838 | if (single_field->field == USTCOMM_POISON_PTR) { |
| 839 | return -ENOMEM; |
| 840 | } |
| 841 | |
| 842 | header->size = COMPUTE_MSG_SIZE(single_field, offset); |
| 843 | |
| 844 | return 0; |
| 845 | } |
| 846 | |
| 847 | int ustcomm_unpack_single_field(struct ustcomm_single_field *single_field) |
| 848 | { |
| 849 | single_field->field = ustcomm_restore_ptr(single_field->field, |
| 850 | single_field->data, |
| 851 | sizeof(single_field->data)); |
| 852 | if (!single_field->field) { |
| 853 | return -EINVAL; |
| 854 | } |
| 855 | |
| 856 | return 0; |
| 857 | } |
| 858 | |
| 859 | int ustcomm_pack_channel_info(struct ustcomm_header *header, |
| 860 | struct ustcomm_channel_info *ch_inf, |
| 861 | const char *trace, |
| 862 | const char *channel) |
| 863 | { |
| 864 | int offset = 0; |
| 865 | |
| 866 | ch_inf->trace = ustcomm_print_data(ch_inf->data, |
| 867 | sizeof(ch_inf->data), |
| 868 | &offset, |
| 869 | trace); |
| 870 | |
| 871 | if (ch_inf->trace == USTCOMM_POISON_PTR) { |
| 872 | return -ENOMEM; |
| 873 | } |
| 874 | |
| 875 | ch_inf->channel = ustcomm_print_data(ch_inf->data, |
| 876 | sizeof(ch_inf->data), |
| 877 | &offset, |
| 878 | channel); |
| 879 | |
| 880 | if (ch_inf->channel == USTCOMM_POISON_PTR) { |
| 881 | return -ENOMEM; |
| 882 | } |
| 883 | |
| 884 | header->size = COMPUTE_MSG_SIZE(ch_inf, offset); |
| 885 | |
| 886 | return 0; |
| 887 | } |
| 888 | |
| 889 | |
| 890 | int ustcomm_unpack_channel_info(struct ustcomm_channel_info *ch_inf) |
| 891 | { |
| 892 | ch_inf->trace = ustcomm_restore_ptr(ch_inf->trace, |
| 893 | ch_inf->data, |
| 894 | sizeof(ch_inf->data)); |
| 895 | if (!ch_inf->trace) { |
| 896 | return -EINVAL; |
| 897 | } |
| 898 | |
| 899 | ch_inf->channel = ustcomm_restore_ptr(ch_inf->channel, |
| 900 | ch_inf->data, |
| 901 | sizeof(ch_inf->data)); |
| 902 | if (!ch_inf->channel) { |
| 903 | return -EINVAL; |
| 904 | } |
| 905 | |
| 906 | return 0; |
| 907 | } |
| 908 | |
| 909 | int ustcomm_pack_buffer_info(struct ustcomm_header *header, |
| 910 | struct ustcomm_buffer_info *buf_inf, |
| 911 | const char *trace, |
| 912 | const char *channel, |
| 913 | int channel_cpu) |
| 914 | { |
| 915 | int offset = 0; |
| 916 | |
| 917 | buf_inf->trace = ustcomm_print_data(buf_inf->data, |
| 918 | sizeof(buf_inf->data), |
| 919 | &offset, |
| 920 | trace); |
| 921 | |
| 922 | if (buf_inf->trace == USTCOMM_POISON_PTR) { |
| 923 | return -ENOMEM; |
| 924 | } |
| 925 | |
| 926 | buf_inf->channel = ustcomm_print_data(buf_inf->data, |
| 927 | sizeof(buf_inf->data), |
| 928 | &offset, |
| 929 | channel); |
| 930 | |
| 931 | if (buf_inf->channel == USTCOMM_POISON_PTR) { |
| 932 | return -ENOMEM; |
| 933 | } |
| 934 | |
| 935 | buf_inf->ch_cpu = channel_cpu; |
| 936 | |
| 937 | header->size = COMPUTE_MSG_SIZE(buf_inf, offset); |
| 938 | |
| 939 | return 0; |
| 940 | } |
| 941 | |
| 942 | |
| 943 | int ustcomm_unpack_buffer_info(struct ustcomm_buffer_info *buf_inf) |
| 944 | { |
| 945 | buf_inf->trace = ustcomm_restore_ptr(buf_inf->trace, |
| 946 | buf_inf->data, |
| 947 | sizeof(buf_inf->data)); |
| 948 | if (!buf_inf->trace) { |
| 949 | return -EINVAL; |
| 950 | } |
| 951 | |
| 952 | buf_inf->channel = ustcomm_restore_ptr(buf_inf->channel, |
| 953 | buf_inf->data, |
| 954 | sizeof(buf_inf->data)); |
| 955 | if (!buf_inf->channel) { |
| 956 | return -EINVAL; |
| 957 | } |
| 958 | |
| 959 | return 0; |
| 960 | } |
| 961 | |
| 962 | int ustcomm_pack_ust_marker_info(struct ustcomm_header *header, |
| 963 | struct ustcomm_ust_marker_info *ust_marker_inf, |
| 964 | const char *trace, |
| 965 | const char *channel, |
| 966 | const char *ust_marker) |
| 967 | { |
| 968 | int offset = 0; |
| 969 | |
| 970 | ust_marker_inf->trace = ustcomm_print_data(ust_marker_inf->data, |
| 971 | sizeof(ust_marker_inf->data), |
| 972 | &offset, |
| 973 | trace); |
| 974 | |
| 975 | if (ust_marker_inf->trace == USTCOMM_POISON_PTR) { |
| 976 | return -ENOMEM; |
| 977 | } |
| 978 | |
| 979 | |
| 980 | ust_marker_inf->channel = ustcomm_print_data(ust_marker_inf->data, |
| 981 | sizeof(ust_marker_inf->data), |
| 982 | &offset, |
| 983 | channel); |
| 984 | |
| 985 | if (ust_marker_inf->channel == USTCOMM_POISON_PTR) { |
| 986 | return -ENOMEM; |
| 987 | } |
| 988 | |
| 989 | |
| 990 | ust_marker_inf->ust_marker = ustcomm_print_data(ust_marker_inf->data, |
| 991 | sizeof(ust_marker_inf->data), |
| 992 | &offset, |
| 993 | ust_marker); |
| 994 | |
| 995 | if (ust_marker_inf->ust_marker == USTCOMM_POISON_PTR) { |
| 996 | return -ENOMEM; |
| 997 | } |
| 998 | |
| 999 | header->size = COMPUTE_MSG_SIZE(ust_marker_inf, offset); |
| 1000 | |
| 1001 | return 0; |
| 1002 | } |
| 1003 | |
| 1004 | int ustcomm_unpack_ust_marker_info(struct ustcomm_ust_marker_info *ust_marker_inf) |
| 1005 | { |
| 1006 | ust_marker_inf->trace = ustcomm_restore_ptr(ust_marker_inf->trace, |
| 1007 | ust_marker_inf->data, |
| 1008 | sizeof(ust_marker_inf->data)); |
| 1009 | if (!ust_marker_inf->trace) { |
| 1010 | return -EINVAL; |
| 1011 | } |
| 1012 | |
| 1013 | ust_marker_inf->channel = ustcomm_restore_ptr(ust_marker_inf->channel, |
| 1014 | ust_marker_inf->data, |
| 1015 | sizeof(ust_marker_inf->data)); |
| 1016 | if (!ust_marker_inf->channel) { |
| 1017 | return -EINVAL; |
| 1018 | } |
| 1019 | |
| 1020 | ust_marker_inf->ust_marker = ustcomm_restore_ptr(ust_marker_inf->ust_marker, |
| 1021 | ust_marker_inf->data, |
| 1022 | sizeof(ust_marker_inf->data)); |
| 1023 | if (!ust_marker_inf->ust_marker) { |
| 1024 | return -EINVAL; |
| 1025 | } |
| 1026 | |
| 1027 | return 0; |
| 1028 | } |
| 1029 | |