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