| 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 | |
| 20 | #include <sys/types.h> |
| 21 | #include <sys/stat.h> |
| 22 | #include <sys/shm.h> |
| 23 | #include <fcntl.h> |
| 24 | #include <unistd.h> |
| 25 | #include <pthread.h> |
| 26 | #include <signal.h> |
| 27 | |
| 28 | #include <stdlib.h> |
| 29 | #include <stdio.h> |
| 30 | #include <string.h> |
| 31 | #include <errno.h> |
| 32 | #include <assert.h> |
| 33 | #include <getopt.h> |
| 34 | |
| 35 | #include "ustd.h" |
| 36 | #include "localerr.h" |
| 37 | #include "ustcomm.h" |
| 38 | #include "share.h" |
| 39 | |
| 40 | /* return value: 0 = subbuffer is finished, it won't produce data anymore |
| 41 | * 1 = got subbuffer successfully |
| 42 | * <0 = error |
| 43 | */ |
| 44 | |
| 45 | #define GET_SUBBUF_OK 1 |
| 46 | #define GET_SUBBUF_DONE 0 |
| 47 | #define GET_SUBBUF_DIED 2 |
| 48 | |
| 49 | #define PUT_SUBBUF_OK 1 |
| 50 | #define PUT_SUBBUF_DIED 0 |
| 51 | #define PUT_SUBBUF_PUSHED 2 |
| 52 | |
| 53 | char *sock_path=NULL; |
| 54 | char *trace_path=NULL; |
| 55 | |
| 56 | /* Number of active buffers and the mutex to protect it. */ |
| 57 | int active_buffers = 0; |
| 58 | pthread_mutex_t active_buffers_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 59 | /* Whether a request to end the program was received. */ |
| 60 | sig_atomic_t terminate_req = 0; |
| 61 | |
| 62 | int test_sigpipe(void) |
| 63 | { |
| 64 | sigset_t sigset; |
| 65 | int result; |
| 66 | |
| 67 | result = sigemptyset(&sigset); |
| 68 | if(result == -1) { |
| 69 | PERROR("sigemptyset"); |
| 70 | return -1; |
| 71 | } |
| 72 | result = sigaddset(&sigset, SIGPIPE); |
| 73 | if(result == -1) { |
| 74 | PERROR("sigaddset"); |
| 75 | return -1; |
| 76 | } |
| 77 | |
| 78 | result = sigtimedwait(&sigset, NULL, &(struct timespec){0,0}); |
| 79 | if(result == -1 && errno == EAGAIN) { |
| 80 | /* no signal received */ |
| 81 | return 0; |
| 82 | } |
| 83 | else if(result == -1) { |
| 84 | PERROR("sigtimedwait"); |
| 85 | return -1; |
| 86 | } |
| 87 | else if(result == SIGPIPE) { |
| 88 | /* received sigpipe */ |
| 89 | return 1; |
| 90 | } |
| 91 | else { |
| 92 | assert(0); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | int get_subbuffer(struct buffer_info *buf) |
| 97 | { |
| 98 | char *send_msg=NULL; |
| 99 | char *received_msg=NULL; |
| 100 | char *rep_code=NULL; |
| 101 | int retval; |
| 102 | int result; |
| 103 | |
| 104 | asprintf(&send_msg, "get_subbuffer %s", buf->name); |
| 105 | result = ustcomm_send_request(&buf->conn, send_msg, &received_msg); |
| 106 | if(test_sigpipe()) { |
| 107 | WARN("process %d destroyed before we could connect to it", buf->pid); |
| 108 | retval = GET_SUBBUF_DONE; |
| 109 | goto end; |
| 110 | } |
| 111 | else if(result < 0) { |
| 112 | ERR("get_subbuffer: ustcomm_send_request failed"); |
| 113 | retval = -1; |
| 114 | goto end; |
| 115 | } |
| 116 | |
| 117 | result = sscanf(received_msg, "%as %ld", &rep_code, &buf->consumed_old); |
| 118 | if(result != 2 && result != 1) { |
| 119 | ERR("unable to parse response to get_subbuffer"); |
| 120 | retval = -1; |
| 121 | goto end_rep; |
| 122 | } |
| 123 | |
| 124 | DBG("received msg is %s", received_msg); |
| 125 | |
| 126 | if(!strcmp(rep_code, "OK")) { |
| 127 | DBG("got subbuffer %s", buf->name); |
| 128 | retval = GET_SUBBUF_OK; |
| 129 | } |
| 130 | else if(nth_token_is(received_msg, "END", 0) == 1) { |
| 131 | retval = GET_SUBBUF_DONE; |
| 132 | goto end_rep; |
| 133 | } |
| 134 | else { |
| 135 | DBG("error getting subbuffer %s", buf->name); |
| 136 | retval = -1; |
| 137 | } |
| 138 | |
| 139 | /* FIMXE: free correctly the stuff */ |
| 140 | end_rep: |
| 141 | if(rep_code) |
| 142 | free(rep_code); |
| 143 | end: |
| 144 | if(send_msg) |
| 145 | free(send_msg); |
| 146 | if(received_msg) |
| 147 | free(received_msg); |
| 148 | |
| 149 | return retval; |
| 150 | } |
| 151 | |
| 152 | int put_subbuffer(struct buffer_info *buf) |
| 153 | { |
| 154 | char *send_msg=NULL; |
| 155 | char *received_msg=NULL; |
| 156 | char *rep_code=NULL; |
| 157 | int retval; |
| 158 | int result; |
| 159 | |
| 160 | asprintf(&send_msg, "put_subbuffer %s %ld", buf->name, buf->consumed_old); |
| 161 | result = ustcomm_send_request(&buf->conn, send_msg, &received_msg); |
| 162 | if(result < 0 && errno == ECONNRESET) { |
| 163 | retval = PUT_SUBBUF_DIED; |
| 164 | goto end; |
| 165 | } |
| 166 | if(result < 0) { |
| 167 | ERR("put_subbuffer: send_message failed"); |
| 168 | retval = -1; |
| 169 | goto end; |
| 170 | } |
| 171 | |
| 172 | result = sscanf(received_msg, "%as", &rep_code); |
| 173 | if(result != 1) { |
| 174 | ERR("unable to parse response to put_subbuffer"); |
| 175 | retval = -1; |
| 176 | goto end_rep; |
| 177 | } |
| 178 | |
| 179 | if(!strcmp(rep_code, "OK")) { |
| 180 | DBG("subbuffer put %s", buf->name); |
| 181 | retval = PUT_SUBBUF_OK; |
| 182 | } |
| 183 | else { |
| 184 | DBG("put_subbuffer: received error, we were pushed"); |
| 185 | retval = PUT_SUBBUF_PUSHED; |
| 186 | goto end_rep; |
| 187 | } |
| 188 | |
| 189 | end_rep: |
| 190 | if(rep_code) |
| 191 | free(rep_code); |
| 192 | |
| 193 | end: |
| 194 | if(send_msg) |
| 195 | free(send_msg); |
| 196 | if(received_msg) |
| 197 | free(received_msg); |
| 198 | |
| 199 | return retval; |
| 200 | } |
| 201 | |
| 202 | void decrement_active_buffers(void *arg) |
| 203 | { |
| 204 | pthread_mutex_lock(&active_buffers_mutex); |
| 205 | active_buffers--; |
| 206 | pthread_mutex_unlock(&active_buffers_mutex); |
| 207 | } |
| 208 | |
| 209 | void *consumer_thread(void *arg) |
| 210 | { |
| 211 | struct buffer_info *buf = (struct buffer_info *) arg; |
| 212 | int result; |
| 213 | |
| 214 | pthread_cleanup_push(decrement_active_buffers, NULL); |
| 215 | |
| 216 | for(;;) { |
| 217 | /* get the subbuffer */ |
| 218 | result = get_subbuffer(buf); |
| 219 | if(result == -1) { |
| 220 | ERR("error getting subbuffer"); |
| 221 | continue; |
| 222 | } |
| 223 | else if(result == GET_SUBBUF_DONE) { |
| 224 | /* this is done */ |
| 225 | break; |
| 226 | } |
| 227 | else if(result == GET_SUBBUF_DIED) { |
| 228 | finish_consuming_dead_subbuffer(buf); |
| 229 | break; |
| 230 | } |
| 231 | |
| 232 | /* write data to file */ |
| 233 | result = patient_write(buf->file_fd, buf->mem + (buf->consumed_old & (buf->n_subbufs * buf->subbuf_size-1)), buf->subbuf_size); |
| 234 | if(result == -1) { |
| 235 | PERROR("write"); |
| 236 | /* FIXME: maybe drop this trace */ |
| 237 | } |
| 238 | |
| 239 | /* put the subbuffer */ |
| 240 | result = put_subbuffer(buf); |
| 241 | if(result == -1) { |
| 242 | ERR("unknown error putting subbuffer (channel=%s)", buf->name); |
| 243 | break; |
| 244 | } |
| 245 | else if(result == PUT_SUBBUF_PUSHED) { |
| 246 | ERR("Buffer overflow (channel=%s), reader pushed. This channel will not be usable passed this point.", buf->name); |
| 247 | break; |
| 248 | } |
| 249 | else if(result == PUT_SUBBUF_DIED) { |
| 250 | WARN("application died while putting subbuffer"); |
| 251 | /* FIXME: probably need to skip the first subbuffer in finish_consuming_dead_subbuffer */ |
| 252 | finish_consuming_dead_subbuffer(buf); |
| 253 | break; |
| 254 | } |
| 255 | else if(result == PUT_SUBBUF_OK) { |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | DBG("thread for buffer %s is stopping", buf->name); |
| 260 | |
| 261 | /* FIXME: destroy, unalloc... */ |
| 262 | |
| 263 | pthread_cleanup_pop(1); |
| 264 | |
| 265 | return NULL; |
| 266 | } |
| 267 | |
| 268 | int create_dir_if_needed(char *dir) |
| 269 | { |
| 270 | int result; |
| 271 | result = mkdir(dir, 0777); |
| 272 | if(result == -1) { |
| 273 | if(errno != EEXIST) { |
| 274 | PERROR("mkdir"); |
| 275 | return -1; |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | int is_directory(const char *dir) |
| 283 | { |
| 284 | int result; |
| 285 | struct stat st; |
| 286 | |
| 287 | result = stat(dir, &st); |
| 288 | if(result == -1) { |
| 289 | PERROR("stat"); |
| 290 | return 0; |
| 291 | } |
| 292 | |
| 293 | if(!S_ISDIR(st.st_mode)) { |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | return 1; |
| 298 | } |
| 299 | |
| 300 | int add_buffer(pid_t pid, char *bufname) |
| 301 | { |
| 302 | struct buffer_info *buf; |
| 303 | char *send_msg; |
| 304 | char *received_msg; |
| 305 | int result; |
| 306 | char *tmp; |
| 307 | int fd; |
| 308 | pthread_t thr; |
| 309 | struct shmid_ds shmds; |
| 310 | |
| 311 | buf = (struct buffer_info *) malloc(sizeof(struct buffer_info)); |
| 312 | if(buf == NULL) { |
| 313 | ERR("add_buffer: insufficient memory"); |
| 314 | return -1; |
| 315 | } |
| 316 | |
| 317 | buf->name = bufname; |
| 318 | buf->pid = pid; |
| 319 | |
| 320 | /* connect to app */ |
| 321 | result = ustcomm_connect_app(buf->pid, &buf->conn); |
| 322 | if(result) { |
| 323 | WARN("unable to connect to process, it probably died before we were able to connect"); |
| 324 | return -1; |
| 325 | } |
| 326 | |
| 327 | /* get pidunique */ |
| 328 | asprintf(&send_msg, "get_pidunique"); |
| 329 | result = ustcomm_send_request(&buf->conn, send_msg, &received_msg); |
| 330 | free(send_msg); |
| 331 | if(result == -1) { |
| 332 | ERR("problem in ustcomm_send_request(get_pidunique)"); |
| 333 | return -1; |
| 334 | } |
| 335 | |
| 336 | result = sscanf(received_msg, "%lld", &buf->pidunique); |
| 337 | if(result != 1) { |
| 338 | ERR("unable to parse response to get_pidunique"); |
| 339 | return -1; |
| 340 | } |
| 341 | free(received_msg); |
| 342 | DBG("got pidunique %lld", buf->pidunique); |
| 343 | |
| 344 | /* get shmid */ |
| 345 | asprintf(&send_msg, "get_shmid %s", buf->name); |
| 346 | result = ustcomm_send_request(&buf->conn, send_msg, &received_msg); |
| 347 | free(send_msg); |
| 348 | if(result == -1) { |
| 349 | ERR("problem in ustcomm_send_request(get_shmid)"); |
| 350 | return -1; |
| 351 | } |
| 352 | |
| 353 | result = sscanf(received_msg, "%d %d", &buf->shmid, &buf->bufstruct_shmid); |
| 354 | if(result != 2) { |
| 355 | ERR("unable to parse response to get_shmid"); |
| 356 | return -1; |
| 357 | } |
| 358 | free(received_msg); |
| 359 | DBG("got shmids %d %d", buf->shmid, buf->bufstruct_shmid); |
| 360 | |
| 361 | /* get n_subbufs */ |
| 362 | asprintf(&send_msg, "get_n_subbufs %s", buf->name); |
| 363 | result = ustcomm_send_request(&buf->conn, send_msg, &received_msg); |
| 364 | free(send_msg); |
| 365 | if(result == -1) { |
| 366 | ERR("problem in ustcomm_send_request(g_n_subbufs)"); |
| 367 | return -1; |
| 368 | } |
| 369 | |
| 370 | result = sscanf(received_msg, "%d", &buf->n_subbufs); |
| 371 | if(result != 1) { |
| 372 | ERR("unable to parse response to get_n_subbufs"); |
| 373 | return -1; |
| 374 | } |
| 375 | free(received_msg); |
| 376 | DBG("got n_subbufs %d", buf->n_subbufs); |
| 377 | |
| 378 | /* get subbuf size */ |
| 379 | asprintf(&send_msg, "get_subbuf_size %s", buf->name); |
| 380 | ustcomm_send_request(&buf->conn, send_msg, &received_msg); |
| 381 | free(send_msg); |
| 382 | |
| 383 | result = sscanf(received_msg, "%d", &buf->subbuf_size); |
| 384 | if(result != 1) { |
| 385 | ERR("unable to parse response to get_subbuf_size"); |
| 386 | return -1; |
| 387 | } |
| 388 | free(received_msg); |
| 389 | DBG("got subbuf_size %d", buf->subbuf_size); |
| 390 | |
| 391 | /* attach memory */ |
| 392 | buf->mem = shmat(buf->shmid, NULL, 0); |
| 393 | if(buf->mem == (void *) 0) { |
| 394 | PERROR("shmat"); |
| 395 | return -1; |
| 396 | } |
| 397 | DBG("successfully attached buffer memory"); |
| 398 | |
| 399 | buf->bufstruct_mem = shmat(buf->bufstruct_shmid, NULL, 0); |
| 400 | if(buf->bufstruct_mem == (void *) 0) { |
| 401 | PERROR("shmat"); |
| 402 | return -1; |
| 403 | } |
| 404 | DBG("successfully attached buffer bufstruct memory"); |
| 405 | |
| 406 | /* obtain info on the memory segment */ |
| 407 | result = shmctl(buf->shmid, IPC_STAT, &shmds); |
| 408 | if(result == -1) { |
| 409 | PERROR("shmctl"); |
| 410 | return -1; |
| 411 | } |
| 412 | buf->memlen = shmds.shm_segsz; |
| 413 | |
| 414 | /* open file for output */ |
| 415 | if(!trace_path) { |
| 416 | /* Only create the directory if using the default path, because |
| 417 | * of the risk of typo when using trace path override. We don't |
| 418 | * want to risk creating plenty of useless directories in that case. |
| 419 | */ |
| 420 | result = create_dir_if_needed(USTD_DEFAULT_TRACE_PATH); |
| 421 | if(result == -1) { |
| 422 | ERR("could not create directory %s", USTD_DEFAULT_TRACE_PATH); |
| 423 | return -1; |
| 424 | } |
| 425 | |
| 426 | trace_path = USTD_DEFAULT_TRACE_PATH; |
| 427 | } |
| 428 | |
| 429 | asprintf(&tmp, "%s/%u_%lld", trace_path, buf->pid, buf->pidunique); |
| 430 | result = create_dir_if_needed(tmp); |
| 431 | if(result == -1) { |
| 432 | ERR("could not create directory %s", tmp); |
| 433 | free(tmp); |
| 434 | return -1; |
| 435 | } |
| 436 | free(tmp); |
| 437 | |
| 438 | asprintf(&tmp, "%s/%u_%lld/%s_0", trace_path, buf->pid, buf->pidunique, buf->name); |
| 439 | result = fd = open(tmp, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 00600); |
| 440 | if(result == -1) { |
| 441 | PERROR("open"); |
| 442 | ERR("failed opening trace file %s", tmp); |
| 443 | return -1; |
| 444 | } |
| 445 | buf->file_fd = fd; |
| 446 | free(tmp); |
| 447 | |
| 448 | pthread_mutex_lock(&active_buffers_mutex); |
| 449 | active_buffers++; |
| 450 | pthread_mutex_unlock(&active_buffers_mutex); |
| 451 | |
| 452 | pthread_create(&thr, NULL, consumer_thread, buf); |
| 453 | |
| 454 | return 0; |
| 455 | } |
| 456 | |
| 457 | void usage(void) |
| 458 | { |
| 459 | fprintf(stderr, "Usage:\nustd OPTIONS\n\nOptions:\n" |
| 460 | "\t-h\t\tDisplay this usage.\n" |
| 461 | "\t-o DIR\t\tSpecify the directory where to output the traces.\n" |
| 462 | "\t-s PATH\t\tSpecify the path to use for the daemon socket.\n"); |
| 463 | } |
| 464 | |
| 465 | int parse_args(int argc, char **argv) |
| 466 | { |
| 467 | int c; |
| 468 | |
| 469 | while (1) { |
| 470 | int option_index = 0; |
| 471 | static struct option long_options[] = { |
| 472 | {"help", 0, 0, 'h'}, |
| 473 | {"version", 0, 0, 'V'}, |
| 474 | {0, 0, 0, 0} |
| 475 | }; |
| 476 | |
| 477 | c = getopt_long(argc, argv, "hs:o:", long_options, &option_index); |
| 478 | if (c == -1) |
| 479 | break; |
| 480 | |
| 481 | switch (c) { |
| 482 | case 0: |
| 483 | printf("option %s", long_options[option_index].name); |
| 484 | if (optarg) |
| 485 | printf(" with arg %s", optarg); |
| 486 | printf("\n"); |
| 487 | break; |
| 488 | case 's': |
| 489 | sock_path = optarg; |
| 490 | break; |
| 491 | case 'o': |
| 492 | trace_path = optarg; |
| 493 | if(!is_directory(trace_path)) { |
| 494 | ERR("Not a valid directory. (%s)", trace_path); |
| 495 | return -1; |
| 496 | } |
| 497 | break; |
| 498 | case 'h': |
| 499 | usage(); |
| 500 | exit(0); |
| 501 | case 'V': |
| 502 | printf("Version 0.0\n"); |
| 503 | break; |
| 504 | |
| 505 | default: |
| 506 | /* unknown option or other error; error is |
| 507 | printed by getopt, just return */ |
| 508 | return -1; |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | return 0; |
| 513 | } |
| 514 | |
| 515 | void sigterm_handler(int sig) |
| 516 | { |
| 517 | terminate_req = 1; |
| 518 | } |
| 519 | |
| 520 | int main(int argc, char **argv) |
| 521 | { |
| 522 | struct ustcomm_ustd ustd; |
| 523 | int result; |
| 524 | sigset_t sigset; |
| 525 | struct sigaction sa; |
| 526 | |
| 527 | result = sigemptyset(&sigset); |
| 528 | if(result == -1) { |
| 529 | PERROR("sigemptyset"); |
| 530 | return 1; |
| 531 | } |
| 532 | sa.sa_handler = sigterm_handler; |
| 533 | sa.sa_mask = sigset; |
| 534 | sa.sa_flags = SA_RESTART; |
| 535 | result = sigaction(SIGTERM, &sa, NULL); |
| 536 | if(result == -1) { |
| 537 | PERROR("sigaction"); |
| 538 | return 1; |
| 539 | } |
| 540 | |
| 541 | result = parse_args(argc, argv); |
| 542 | if(result == -1) { |
| 543 | exit(1); |
| 544 | } |
| 545 | |
| 546 | result = ustcomm_init_ustd(&ustd, sock_path); |
| 547 | if(result == -1) { |
| 548 | ERR("failed to initialize socket"); |
| 549 | return 1; |
| 550 | } |
| 551 | |
| 552 | /* setup handler for SIGPIPE */ |
| 553 | result = sigemptyset(&sigset); |
| 554 | if(result == -1) { |
| 555 | PERROR("sigemptyset"); |
| 556 | return 1; |
| 557 | } |
| 558 | result = sigaddset(&sigset, SIGPIPE); |
| 559 | if(result == -1) { |
| 560 | PERROR("sigaddset"); |
| 561 | return 1; |
| 562 | } |
| 563 | result = sigprocmask(SIG_BLOCK, &sigset, NULL); |
| 564 | if(result == -1) { |
| 565 | PERROR("sigprocmask"); |
| 566 | return 1; |
| 567 | } |
| 568 | |
| 569 | /* app loop */ |
| 570 | for(;;) { |
| 571 | char *recvbuf; |
| 572 | |
| 573 | /* check for requests on our public socket */ |
| 574 | result = ustcomm_ustd_recv_message(&ustd, &recvbuf, NULL, 100); |
| 575 | if(result == -1) { |
| 576 | ERR("error in ustcomm_ustd_recv_message"); |
| 577 | continue; |
| 578 | } |
| 579 | if(result > 0) { |
| 580 | if(!strncmp(recvbuf, "collect", 7)) { |
| 581 | pid_t pid; |
| 582 | char *bufname; |
| 583 | int result; |
| 584 | |
| 585 | result = sscanf(recvbuf, "%*s %d %50as", &pid, &bufname); |
| 586 | if(result != 2) { |
| 587 | fprintf(stderr, "parsing error: %s\n", recvbuf); |
| 588 | } |
| 589 | |
| 590 | result = add_buffer(pid, bufname); |
| 591 | if(result < 0) { |
| 592 | ERR("error in add_buffer"); |
| 593 | continue; |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | free(recvbuf); |
| 598 | } |
| 599 | |
| 600 | if(terminate_req) { |
| 601 | pthread_mutex_lock(&active_buffers_mutex); |
| 602 | if(active_buffers == 0) { |
| 603 | pthread_mutex_unlock(&active_buffers_mutex); |
| 604 | break; |
| 605 | } |
| 606 | pthread_mutex_unlock(&active_buffers_mutex); |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | return 0; |
| 611 | } |