| 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 | #define _GNU_SOURCE |
| 19 | #include <sys/types.h> |
| 20 | #include <signal.h> |
| 21 | #include <errno.h> |
| 22 | #include <sys/socket.h> |
| 23 | #include <sys/un.h> |
| 24 | #include <unistd.h> |
| 25 | #include <poll.h> |
| 26 | |
| 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
| 30 | #include <execinfo.h> |
| 31 | |
| 32 | #include "ustcomm.h" |
| 33 | #include "localerr.h" |
| 34 | |
| 35 | #define UNIX_PATH_MAX 108 |
| 36 | #define SOCK_DIR "/tmp/socks" |
| 37 | #define UST_SIGNAL SIGIO |
| 38 | |
| 39 | #define MSG_MAX 1000 |
| 40 | |
| 41 | /* FIXME: ustcomm blocks on message sending, which might be problematic in |
| 42 | * some cases. Fix the poll() usage so sends are buffered until they don't |
| 43 | * block. |
| 44 | */ |
| 45 | |
| 46 | //static void bt(void) |
| 47 | //{ |
| 48 | // void *buffer[100]; |
| 49 | // int result; |
| 50 | // |
| 51 | // result = backtrace(&buffer, 100); |
| 52 | // backtrace_symbols_fd(buffer, result, STDERR_FILENO); |
| 53 | //} |
| 54 | |
| 55 | char *strdup_malloc(const char *s) |
| 56 | { |
| 57 | char *retval; |
| 58 | |
| 59 | if(s == NULL) |
| 60 | return NULL; |
| 61 | |
| 62 | retval = (char *) malloc(strlen(s)+1); |
| 63 | |
| 64 | strcpy(retval, s); |
| 65 | |
| 66 | return retval; |
| 67 | } |
| 68 | |
| 69 | static int signal_process(pid_t pid) |
| 70 | { |
| 71 | int result; |
| 72 | |
| 73 | result = kill(pid, UST_SIGNAL); |
| 74 | if(result == -1) { |
| 75 | PERROR("kill"); |
| 76 | return -1; |
| 77 | } |
| 78 | |
| 79 | /* FIXME: should wait in a better way */ |
| 80 | //sleep(1); |
| 81 | |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | static int send_message_fd(int fd, const char *msg) |
| 86 | { |
| 87 | int result; |
| 88 | |
| 89 | result = send(fd, msg, strlen(msg), 0); |
| 90 | if(result == -1) { |
| 91 | PERROR("send"); |
| 92 | return -1; |
| 93 | } |
| 94 | else if(result == 0) { |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | return 1; |
| 99 | |
| 100 | // *reply = (char *) malloc(MSG_MAX+1); |
| 101 | // result = recv(fd, *reply, MSG_MAX, 0); |
| 102 | // if(result == -1) { |
| 103 | // PERROR("recv"); |
| 104 | // return -1; |
| 105 | // } |
| 106 | // else if(result == 0) { |
| 107 | // return 0; |
| 108 | // } |
| 109 | // |
| 110 | // (*reply)[result] = '\0'; |
| 111 | // |
| 112 | // return 1; |
| 113 | } |
| 114 | |
| 115 | static int send_message_path(const char *path, const char *msg, int signalpid) |
| 116 | { |
| 117 | int fd; |
| 118 | int result; |
| 119 | struct sockaddr_un addr; |
| 120 | |
| 121 | result = fd = socket(PF_UNIX, SOCK_STREAM, 0); |
| 122 | if(result == -1) { |
| 123 | PERROR("socket"); |
| 124 | return -1; |
| 125 | } |
| 126 | |
| 127 | addr.sun_family = AF_UNIX; |
| 128 | |
| 129 | result = snprintf(addr.sun_path, UNIX_PATH_MAX, "%s", path); |
| 130 | if(result >= UNIX_PATH_MAX) { |
| 131 | ERR("string overflow allocating socket name"); |
| 132 | return -1; |
| 133 | } |
| 134 | |
| 135 | if(signalpid >= 0) { |
| 136 | result = signal_process(signalpid); |
| 137 | if(result == -1) { |
| 138 | ERR("could not signal process"); |
| 139 | return -1; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | result = connect(fd, (struct sockaddr *)&addr, sizeof(addr)); |
| 144 | if(result == -1) { |
| 145 | PERROR("connect"); |
| 146 | return -1; |
| 147 | } |
| 148 | |
| 149 | return send_message_fd(fd, msg); |
| 150 | } |
| 151 | |
| 152 | ///* pid: the pid of the trace process that must receive the msg |
| 153 | // msg: pointer to a null-terminated message to send |
| 154 | // reply: location where to put the null-terminated string of the reply; |
| 155 | // it must be free'd after usage |
| 156 | // */ |
| 157 | // |
| 158 | //int send_message_pid(pid_t pid, const char *msg, char **reply) |
| 159 | //{ |
| 160 | // int result; |
| 161 | // char path[UNIX_PATH_MAX]; |
| 162 | // |
| 163 | // result = snprintf(path, UNIX_PATH_MAX, "%s/%d", SOCK_DIR, pid); |
| 164 | // if(result >= UNIX_PATH_MAX) { |
| 165 | // fprintf(stderr, "string overflow allocating socket name"); |
| 166 | // return -1; |
| 167 | // } |
| 168 | // |
| 169 | // send_message_path(path, msg, reply, pid); |
| 170 | // |
| 171 | // return 0; |
| 172 | //} |
| 173 | |
| 174 | /* Called by an app to ask the consumer daemon to connect to it. */ |
| 175 | |
| 176 | int ustcomm_request_consumer(pid_t pid, const char *channel) |
| 177 | { |
| 178 | char path[UNIX_PATH_MAX]; |
| 179 | int result; |
| 180 | char *msg; |
| 181 | |
| 182 | result = snprintf(path, UNIX_PATH_MAX, "%s/ustd", SOCK_DIR); |
| 183 | if(result >= UNIX_PATH_MAX) { |
| 184 | fprintf(stderr, "string overflow allocating socket name"); |
| 185 | return -1; |
| 186 | } |
| 187 | |
| 188 | asprintf(&msg, "collect %d %s", pid, channel); |
| 189 | |
| 190 | send_message_path(path, msg, -1); |
| 191 | free(msg); |
| 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | /* returns 1 to indicate a message was received |
| 197 | * returns 0 to indicate no message was received (cannot happen) |
| 198 | * returns -1 to indicate an error |
| 199 | */ |
| 200 | |
| 201 | static int recv_message_fd(int fd, char **msg, struct ustcomm_source *src) |
| 202 | { |
| 203 | int result; |
| 204 | |
| 205 | *msg = (char *) malloc(MSG_MAX+1); |
| 206 | |
| 207 | result = recv(fd, *msg, MSG_MAX, 0); |
| 208 | if(result == -1) { |
| 209 | PERROR("recv"); |
| 210 | return -1; |
| 211 | } |
| 212 | |
| 213 | (*msg)[result] = '\0'; |
| 214 | |
| 215 | DBG("ustcomm_app_recv_message: result is %d, message is %s", result, (*msg)); |
| 216 | |
| 217 | if(src) |
| 218 | src->fd = fd; |
| 219 | |
| 220 | return 1; |
| 221 | } |
| 222 | |
| 223 | int ustcomm_send_reply(struct ustcomm_server *server, char *msg, struct ustcomm_source *src) |
| 224 | { |
| 225 | int result; |
| 226 | |
| 227 | result = send_message_fd(src->fd, msg); |
| 228 | if(result < 0) { |
| 229 | ERR("error in send_message_fd"); |
| 230 | return -1; |
| 231 | } |
| 232 | |
| 233 | return 0; |
| 234 | } |
| 235 | |
| 236 | /* @timeout: max blocking time in milliseconds, -1 means infinity |
| 237 | * |
| 238 | * returns 1 to indicate a message was received |
| 239 | * returns 0 to indicate no message was received |
| 240 | * returns -1 to indicate an error |
| 241 | */ |
| 242 | |
| 243 | int ustcomm_recv_message(struct ustcomm_server *server, char **msg, struct ustcomm_source *src, int timeout) |
| 244 | { |
| 245 | struct pollfd *fds; |
| 246 | struct ustcomm_connection *conn; |
| 247 | int result; |
| 248 | int retval; |
| 249 | |
| 250 | for(;;) { |
| 251 | int idx = 0; |
| 252 | int n_fds = 1; |
| 253 | |
| 254 | list_for_each_entry(conn, &server->connections, list) { |
| 255 | n_fds++; |
| 256 | } |
| 257 | |
| 258 | fds = (struct pollfd *) malloc(n_fds * sizeof(struct pollfd)); |
| 259 | if(fds == NULL) { |
| 260 | ERR("malloc returned NULL"); |
| 261 | return -1; |
| 262 | } |
| 263 | |
| 264 | /* special idx 0 is for listening socket */ |
| 265 | fds[idx].fd = server->listen_fd; |
| 266 | fds[idx].events = POLLIN; |
| 267 | idx++; |
| 268 | |
| 269 | list_for_each_entry(conn, &server->connections, list) { |
| 270 | fds[idx].fd = conn->fd; |
| 271 | fds[idx].events = POLLIN; |
| 272 | idx++; |
| 273 | } |
| 274 | |
| 275 | while((result = poll(fds, n_fds, timeout)) == -1 && errno == EINTR) |
| 276 | /* nothing */; |
| 277 | if(result == -1) { |
| 278 | PERROR("poll"); |
| 279 | return -1; |
| 280 | } |
| 281 | |
| 282 | if(result == 0) |
| 283 | return 0; |
| 284 | |
| 285 | if(fds[0].revents) { |
| 286 | struct ustcomm_connection *newconn; |
| 287 | int newfd; |
| 288 | |
| 289 | result = newfd = accept(server->listen_fd, NULL, NULL); |
| 290 | if(result == -1) { |
| 291 | PERROR("accept"); |
| 292 | return -1; |
| 293 | } |
| 294 | |
| 295 | newconn = (struct ustcomm_connection *) malloc(sizeof(struct ustcomm_connection)); |
| 296 | if(newconn == NULL) { |
| 297 | ERR("malloc returned NULL"); |
| 298 | return -1; |
| 299 | } |
| 300 | |
| 301 | newconn->fd = newfd; |
| 302 | |
| 303 | list_add(&newconn->list, &server->connections); |
| 304 | } |
| 305 | |
| 306 | for(idx=1; idx<n_fds; idx++) { |
| 307 | if(fds[idx].revents) { |
| 308 | retval = recv_message_fd(fds[idx].fd, msg, src); |
| 309 | if(**msg == 0) { |
| 310 | /* connection finished */ |
| 311 | close(fds[idx].fd); |
| 312 | |
| 313 | list_for_each_entry(conn, &server->connections, list) { |
| 314 | if(conn->fd == fds[idx].fd) { |
| 315 | list_del(&conn->list); |
| 316 | break; |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | else { |
| 321 | goto free_fds_return; |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | free(fds); |
| 327 | } |
| 328 | |
| 329 | free_fds_return: |
| 330 | free(fds); |
| 331 | return retval; |
| 332 | } |
| 333 | |
| 334 | int ustcomm_ustd_recv_message(struct ustcomm_ustd *ustd, char **msg, struct ustcomm_source *src, int timeout) |
| 335 | { |
| 336 | return ustcomm_recv_message(&ustd->server, msg, src, timeout); |
| 337 | } |
| 338 | |
| 339 | int ustcomm_app_recv_message(struct ustcomm_app *app, char **msg, struct ustcomm_source *src, int timeout) |
| 340 | { |
| 341 | return ustcomm_recv_message(&app->server, msg, src, timeout); |
| 342 | } |
| 343 | |
| 344 | /* This removes src from the list of active connections of app. |
| 345 | */ |
| 346 | |
| 347 | int ustcomm_app_detach_client(struct ustcomm_app *app, struct ustcomm_source *src) |
| 348 | { |
| 349 | struct ustcomm_server *server = (struct ustcomm_server *)app; |
| 350 | struct ustcomm_connection *conn; |
| 351 | |
| 352 | list_for_each_entry(conn, &server->connections, list) { |
| 353 | if(conn->fd == src->fd) { |
| 354 | list_del(&conn->list); |
| 355 | goto found; |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | return -1; |
| 360 | found: |
| 361 | return src->fd; |
| 362 | } |
| 363 | |
| 364 | static int init_named_socket(char *name, char **path_out) |
| 365 | { |
| 366 | int result; |
| 367 | int fd; |
| 368 | |
| 369 | struct sockaddr_un addr; |
| 370 | |
| 371 | result = fd = socket(PF_UNIX, SOCK_STREAM, 0); |
| 372 | if(result == -1) { |
| 373 | PERROR("socket"); |
| 374 | return -1; |
| 375 | } |
| 376 | |
| 377 | addr.sun_family = AF_UNIX; |
| 378 | |
| 379 | strncpy(addr.sun_path, name, UNIX_PATH_MAX); |
| 380 | addr.sun_path[UNIX_PATH_MAX-1] = '\0'; |
| 381 | |
| 382 | result = access(name, F_OK); |
| 383 | if(result == 0) { |
| 384 | /* file exists */ |
| 385 | result = unlink(name); |
| 386 | if(result == -1) { |
| 387 | PERROR("unlink of socket file"); |
| 388 | goto close_sock; |
| 389 | } |
| 390 | WARN("socket already exists; overwriting"); |
| 391 | } |
| 392 | |
| 393 | result = bind(fd, (struct sockaddr *)&addr, sizeof(addr)); |
| 394 | if(result == -1) { |
| 395 | PERROR("bind"); |
| 396 | goto close_sock; |
| 397 | } |
| 398 | |
| 399 | result = listen(fd, 1); |
| 400 | if(result == -1) { |
| 401 | PERROR("listen"); |
| 402 | goto close_sock; |
| 403 | } |
| 404 | |
| 405 | if(path_out) { |
| 406 | *path_out = ""; |
| 407 | *path_out = strdupa(addr.sun_path); |
| 408 | } |
| 409 | |
| 410 | return fd; |
| 411 | |
| 412 | close_sock: |
| 413 | close(fd); |
| 414 | |
| 415 | return -1; |
| 416 | } |
| 417 | |
| 418 | int ustcomm_send_request(struct ustcomm_connection *conn, char *req, char **reply) |
| 419 | { |
| 420 | int result; |
| 421 | |
| 422 | result = send(conn->fd, req, strlen(req), 0); |
| 423 | if(result == -1) { |
| 424 | PERROR("send"); |
| 425 | return -1; |
| 426 | } |
| 427 | |
| 428 | if(!reply) |
| 429 | return 1; |
| 430 | |
| 431 | *reply = (char *) malloc(MSG_MAX+1); |
| 432 | result = recv(conn->fd, *reply, MSG_MAX, 0); |
| 433 | if(result == -1) { |
| 434 | PERROR("recv"); |
| 435 | return -1; |
| 436 | } |
| 437 | else if(result == 0) { |
| 438 | return 0; |
| 439 | } |
| 440 | |
| 441 | (*reply)[result] = '\0'; |
| 442 | |
| 443 | return 1; |
| 444 | } |
| 445 | |
| 446 | int ustcomm_connect_path(char *path, struct ustcomm_connection *conn, pid_t signalpid) |
| 447 | { |
| 448 | int fd; |
| 449 | int result; |
| 450 | struct sockaddr_un addr; |
| 451 | |
| 452 | result = fd = socket(PF_UNIX, SOCK_STREAM, 0); |
| 453 | if(result == -1) { |
| 454 | PERROR("socket"); |
| 455 | return -1; |
| 456 | } |
| 457 | |
| 458 | addr.sun_family = AF_UNIX; |
| 459 | |
| 460 | result = snprintf(addr.sun_path, UNIX_PATH_MAX, "%s", path); |
| 461 | if(result >= UNIX_PATH_MAX) { |
| 462 | ERR("string overflow allocating socket name"); |
| 463 | return -1; |
| 464 | } |
| 465 | |
| 466 | if(signalpid >= 0) { |
| 467 | result = signal_process(signalpid); |
| 468 | if(result == -1) { |
| 469 | ERR("could not signal process"); |
| 470 | return -1; |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | result = connect(fd, (struct sockaddr *)&addr, sizeof(addr)); |
| 475 | if(result == -1) { |
| 476 | PERROR("connect"); |
| 477 | return -1; |
| 478 | } |
| 479 | |
| 480 | conn->fd = fd; |
| 481 | |
| 482 | return 0; |
| 483 | } |
| 484 | |
| 485 | int ustcomm_disconnect(struct ustcomm_connection *conn) |
| 486 | { |
| 487 | return close(conn->fd); |
| 488 | } |
| 489 | |
| 490 | int ustcomm_connect_app(pid_t pid, struct ustcomm_connection *conn) |
| 491 | { |
| 492 | int result; |
| 493 | char path[UNIX_PATH_MAX]; |
| 494 | |
| 495 | |
| 496 | result = snprintf(path, UNIX_PATH_MAX, "%s/%d", SOCK_DIR, pid); |
| 497 | if(result >= UNIX_PATH_MAX) { |
| 498 | fprintf(stderr, "string overflow allocating socket name"); |
| 499 | return -1; |
| 500 | } |
| 501 | |
| 502 | return ustcomm_connect_path(path, conn, pid); |
| 503 | } |
| 504 | |
| 505 | int ustcomm_disconnect_app(struct ustcomm_connection *conn) |
| 506 | { |
| 507 | close(conn->fd); |
| 508 | return 0; |
| 509 | } |
| 510 | |
| 511 | int ustcomm_init_app(pid_t pid, struct ustcomm_app *handle) |
| 512 | { |
| 513 | int result; |
| 514 | char *name; |
| 515 | |
| 516 | result = asprintf(&name, "%s/%d", SOCK_DIR, (int)pid); |
| 517 | if(result >= UNIX_PATH_MAX) { |
| 518 | ERR("string overflow allocating socket name"); |
| 519 | return -1; |
| 520 | } |
| 521 | |
| 522 | handle->server.listen_fd = init_named_socket(name, &(handle->server.socketpath)); |
| 523 | if(handle->server.listen_fd < 0) { |
| 524 | ERR("error initializing named socket"); |
| 525 | goto free_name; |
| 526 | } |
| 527 | free(name); |
| 528 | |
| 529 | INIT_LIST_HEAD(&handle->server.connections); |
| 530 | |
| 531 | return 0; |
| 532 | |
| 533 | free_name: |
| 534 | free(name); |
| 535 | return -1; |
| 536 | } |
| 537 | |
| 538 | int ustcomm_init_ustd(struct ustcomm_ustd *handle) |
| 539 | { |
| 540 | int result; |
| 541 | char *name; |
| 542 | |
| 543 | result = asprintf(&name, "%s/%s", SOCK_DIR, "ustd"); |
| 544 | if(result >= UNIX_PATH_MAX) { |
| 545 | ERR("string overflow allocating socket name"); |
| 546 | return -1; |
| 547 | } |
| 548 | |
| 549 | handle->server.listen_fd = init_named_socket(name, &handle->server.socketpath); |
| 550 | if(handle->server.listen_fd < 0) { |
| 551 | ERR("error initializing named socket at %s", name); |
| 552 | goto free_name; |
| 553 | } |
| 554 | free(name); |
| 555 | |
| 556 | INIT_LIST_HEAD(&handle->server.connections); |
| 557 | |
| 558 | return 0; |
| 559 | |
| 560 | free_name: |
| 561 | free(name); |
| 562 | return -1; |
| 563 | } |
| 564 | |
| 565 | static char *find_tok(char *str) |
| 566 | { |
| 567 | while(*str == ' ') { |
| 568 | str++; |
| 569 | |
| 570 | if(*str == 0) |
| 571 | return NULL; |
| 572 | } |
| 573 | |
| 574 | return str; |
| 575 | } |
| 576 | |
| 577 | static char *find_sep(char *str) |
| 578 | { |
| 579 | while(*str != ' ') { |
| 580 | str++; |
| 581 | |
| 582 | if(*str == 0) |
| 583 | break; |
| 584 | } |
| 585 | |
| 586 | return str; |
| 587 | } |
| 588 | |
| 589 | int nth_token_is(char *str, char *token, int tok_no) |
| 590 | { |
| 591 | int i; |
| 592 | char *start; |
| 593 | char *end; |
| 594 | |
| 595 | for(i=0; i<=tok_no; i++) { |
| 596 | str = find_tok(str); |
| 597 | if(str == NULL) |
| 598 | return -1; |
| 599 | |
| 600 | start = str; |
| 601 | |
| 602 | str = find_sep(str); |
| 603 | if(str == NULL) |
| 604 | return -1; |
| 605 | |
| 606 | end = str; |
| 607 | } |
| 608 | |
| 609 | if(end-start != strlen(token)) |
| 610 | return 0; |
| 611 | |
| 612 | if(strncmp(start, token, end-start)) |
| 613 | return 0; |
| 614 | |
| 615 | return 1; |
| 616 | } |
| 617 | |
| 618 | char *nth_token(char *str, int tok_no) |
| 619 | { |
| 620 | static char *retval = NULL; |
| 621 | int i; |
| 622 | char *start; |
| 623 | char *end; |
| 624 | |
| 625 | for(i=0; i<=tok_no; i++) { |
| 626 | str = find_tok(str); |
| 627 | if(str == NULL) |
| 628 | return NULL; |
| 629 | |
| 630 | start = str; |
| 631 | |
| 632 | str = find_sep(str); |
| 633 | if(str == NULL) |
| 634 | return NULL; |
| 635 | |
| 636 | end = str; |
| 637 | } |
| 638 | |
| 639 | if(retval) { |
| 640 | free(retval); |
| 641 | retval = NULL; |
| 642 | } |
| 643 | |
| 644 | asprintf(&retval, "%.*s", (int)(end-start), start); |
| 645 | |
| 646 | return retval; |
| 647 | } |
| 648 | |