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