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