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