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