| 1 | /* lttd |
| 2 | * |
| 3 | * Linux Trace Toolkit Daemon |
| 4 | * |
| 5 | * This is a simple daemon that reads a few relay+debugfs channels and save |
| 6 | * them in a trace. |
| 7 | * |
| 8 | * |
| 9 | * Copyright 2005 - |
| 10 | * Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca> |
| 11 | */ |
| 12 | |
| 13 | #ifdef HAVE_CONFIG_H |
| 14 | #include <config.h> |
| 15 | #endif |
| 16 | |
| 17 | #define _REENTRANT |
| 18 | #define _GNU_SOURCE |
| 19 | #include <features.h> |
| 20 | #include <stdio.h> |
| 21 | #include <unistd.h> |
| 22 | #include <errno.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <dirent.h> |
| 27 | #include <string.h> |
| 28 | #include <fcntl.h> |
| 29 | #include <sys/poll.h> |
| 30 | #include <sys/mman.h> |
| 31 | #include <signal.h> |
| 32 | #include <pthread.h> |
| 33 | |
| 34 | /* Relayfs IOCTL */ |
| 35 | #include <asm/ioctl.h> |
| 36 | #include <asm/types.h> |
| 37 | |
| 38 | /* Get the next sub buffer that can be read. */ |
| 39 | #define RELAY_GET_SUBBUF _IOR(0xF4, 0x00,__u32) |
| 40 | /* Release the oldest reserved (by "get") sub buffer. */ |
| 41 | #define RELAY_PUT_SUBBUF _IOW(0xF4, 0x01,__u32) |
| 42 | /* returns the number of sub buffers in the per cpu channel. */ |
| 43 | #define RELAY_GET_N_SUBBUFS _IOR(0xF4, 0x02,__u32) |
| 44 | /* returns the size of the sub buffers. */ |
| 45 | #define RELAY_GET_SUBBUF_SIZE _IOR(0xF4, 0x03,__u32) |
| 46 | |
| 47 | |
| 48 | |
| 49 | enum { |
| 50 | GET_SUBBUF, |
| 51 | PUT_SUBBUF, |
| 52 | GET_N_BUBBUFS, |
| 53 | GET_SUBBUF_SIZE |
| 54 | }; |
| 55 | |
| 56 | struct fd_pair { |
| 57 | int channel; |
| 58 | int trace; |
| 59 | unsigned int n_subbufs; |
| 60 | unsigned int subbuf_size; |
| 61 | void *mmap; |
| 62 | pthread_mutex_t mutex; |
| 63 | }; |
| 64 | |
| 65 | struct channel_trace_fd { |
| 66 | struct fd_pair *pair; |
| 67 | int num_pairs; |
| 68 | }; |
| 69 | |
| 70 | static char *trace_name = NULL; |
| 71 | static char *channel_name = NULL; |
| 72 | static int daemon_mode = 0; |
| 73 | static int append_mode = 0; |
| 74 | static unsigned long num_threads = 1; |
| 75 | volatile static int quit_program = 0; /* For signal handler */ |
| 76 | static int dump_flight_only = 0; |
| 77 | static int dump_normal_only = 0; |
| 78 | |
| 79 | /* Args : |
| 80 | * |
| 81 | * -t directory Directory name of the trace to write to. Will be created. |
| 82 | * -c directory Root directory of the debugfs trace channels. |
| 83 | * -d Run in background (daemon). |
| 84 | * -a Trace append mode. |
| 85 | * -s Send SIGUSR1 to parent when ready for IO. |
| 86 | */ |
| 87 | void show_arguments(void) |
| 88 | { |
| 89 | printf("Please use the following arguments :\n"); |
| 90 | printf("\n"); |
| 91 | printf("-t directory Directory name of the trace to write to.\n" |
| 92 | " It will be created.\n"); |
| 93 | printf("-c directory Root directory of the debugfs trace channels.\n"); |
| 94 | printf("-d Run in background (daemon).\n"); |
| 95 | printf("-a Append to an possibly existing trace.\n"); |
| 96 | printf("-N Number of threads to start.\n"); |
| 97 | printf("-f Dump only flight recorder channels.\n"); |
| 98 | printf("-n Dump only normal channels.\n"); |
| 99 | printf("\n"); |
| 100 | } |
| 101 | |
| 102 | |
| 103 | /* parse_arguments |
| 104 | * |
| 105 | * Parses the command line arguments. |
| 106 | * |
| 107 | * Returns 1 if the arguments were correct, but doesn't ask for program |
| 108 | * continuation. Returns -1 if the arguments are incorrect, or 0 if OK. |
| 109 | */ |
| 110 | int parse_arguments(int argc, char **argv) |
| 111 | { |
| 112 | int ret = 0; |
| 113 | int argn = 1; |
| 114 | |
| 115 | if(argc == 2) { |
| 116 | if(strcmp(argv[1], "-h") == 0) { |
| 117 | return 1; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | while(argn < argc) { |
| 122 | |
| 123 | switch(argv[argn][0]) { |
| 124 | case '-': |
| 125 | switch(argv[argn][1]) { |
| 126 | case 't': |
| 127 | if(argn+1 < argc) { |
| 128 | trace_name = argv[argn+1]; |
| 129 | argn++; |
| 130 | } |
| 131 | break; |
| 132 | case 'c': |
| 133 | if(argn+1 < argc) { |
| 134 | channel_name = argv[argn+1]; |
| 135 | argn++; |
| 136 | } |
| 137 | break; |
| 138 | case 'd': |
| 139 | daemon_mode = 1; |
| 140 | break; |
| 141 | case 'a': |
| 142 | append_mode = 1; |
| 143 | break; |
| 144 | case 'N': |
| 145 | if(argn+1 < argc) { |
| 146 | num_threads = strtoul(argv[argn+1], NULL, 0); |
| 147 | argn++; |
| 148 | } |
| 149 | break; |
| 150 | case 'f': |
| 151 | dump_flight_only = 1; |
| 152 | break; |
| 153 | case 'n': |
| 154 | dump_normal_only = 1; |
| 155 | break; |
| 156 | default: |
| 157 | printf("Invalid argument '%s'.\n", argv[argn]); |
| 158 | printf("\n"); |
| 159 | ret = -1; |
| 160 | } |
| 161 | break; |
| 162 | default: |
| 163 | printf("Invalid argument '%s'.\n", argv[argn]); |
| 164 | printf("\n"); |
| 165 | ret = -1; |
| 166 | } |
| 167 | argn++; |
| 168 | } |
| 169 | |
| 170 | if(trace_name == NULL) { |
| 171 | printf("Please specify a trace name.\n"); |
| 172 | printf("\n"); |
| 173 | ret = -1; |
| 174 | } |
| 175 | |
| 176 | if(channel_name == NULL) { |
| 177 | printf("Please specify a channel name.\n"); |
| 178 | printf("\n"); |
| 179 | ret = -1; |
| 180 | } |
| 181 | |
| 182 | return ret; |
| 183 | } |
| 184 | |
| 185 | void show_info(void) |
| 186 | { |
| 187 | printf("Linux Trace Toolkit Trace Daemon\n"); |
| 188 | printf("\n"); |
| 189 | printf("Reading from debugfs directory : %s\n", channel_name); |
| 190 | printf("Writing to trace directory : %s\n", trace_name); |
| 191 | printf("\n"); |
| 192 | } |
| 193 | |
| 194 | |
| 195 | /* signal handling */ |
| 196 | |
| 197 | static void handler(int signo) |
| 198 | { |
| 199 | printf("Signal %d received : exiting cleanly\n", signo); |
| 200 | quit_program = 1; |
| 201 | } |
| 202 | |
| 203 | |
| 204 | |
| 205 | int open_channel_trace_pairs(char *subchannel_name, char *subtrace_name, |
| 206 | struct channel_trace_fd *fd_pairs) |
| 207 | { |
| 208 | DIR *channel_dir = opendir(subchannel_name); |
| 209 | struct dirent *entry; |
| 210 | struct stat stat_buf; |
| 211 | int ret; |
| 212 | char path_channel[PATH_MAX]; |
| 213 | int path_channel_len; |
| 214 | char *path_channel_ptr; |
| 215 | char path_trace[PATH_MAX]; |
| 216 | int path_trace_len; |
| 217 | char *path_trace_ptr; |
| 218 | int open_ret = 0; |
| 219 | |
| 220 | if(channel_dir == NULL) { |
| 221 | perror(subchannel_name); |
| 222 | open_ret = ENOENT; |
| 223 | goto end; |
| 224 | } |
| 225 | |
| 226 | printf("Creating trace subdirectory %s\n", subtrace_name); |
| 227 | ret = mkdir(subtrace_name, S_IRWXU|S_IRWXG|S_IRWXO); |
| 228 | if(ret == -1) { |
| 229 | if(errno != EEXIST) { |
| 230 | perror(subtrace_name); |
| 231 | open_ret = -1; |
| 232 | goto end; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | strncpy(path_channel, subchannel_name, PATH_MAX-1); |
| 237 | path_channel_len = strlen(path_channel); |
| 238 | path_channel[path_channel_len] = '/'; |
| 239 | path_channel_len++; |
| 240 | path_channel_ptr = path_channel + path_channel_len; |
| 241 | |
| 242 | strncpy(path_trace, subtrace_name, PATH_MAX-1); |
| 243 | path_trace_len = strlen(path_trace); |
| 244 | path_trace[path_trace_len] = '/'; |
| 245 | path_trace_len++; |
| 246 | path_trace_ptr = path_trace + path_trace_len; |
| 247 | |
| 248 | while((entry = readdir(channel_dir)) != NULL) { |
| 249 | |
| 250 | if(entry->d_name[0] == '.') continue; |
| 251 | |
| 252 | strncpy(path_channel_ptr, entry->d_name, PATH_MAX - path_channel_len); |
| 253 | strncpy(path_trace_ptr, entry->d_name, PATH_MAX - path_trace_len); |
| 254 | |
| 255 | ret = stat(path_channel, &stat_buf); |
| 256 | if(ret == -1) { |
| 257 | perror(path_channel); |
| 258 | continue; |
| 259 | } |
| 260 | |
| 261 | printf("Channel file : %s\n", path_channel); |
| 262 | |
| 263 | if(S_ISDIR(stat_buf.st_mode)) { |
| 264 | |
| 265 | printf("Entering channel subdirectory...\n"); |
| 266 | ret = open_channel_trace_pairs(path_channel, path_trace, fd_pairs); |
| 267 | if(ret < 0) continue; |
| 268 | } else if(S_ISREG(stat_buf.st_mode)) { |
| 269 | if(strncmp(entry->d_name, "flight-", sizeof("flight-")-1) != 0) { |
| 270 | if(dump_flight_only) { |
| 271 | printf("Skipping normal channel %s\n", path_channel); |
| 272 | continue; |
| 273 | } |
| 274 | } else { |
| 275 | if(dump_normal_only) { |
| 276 | printf("Skipping flight channel %s\n", path_channel); |
| 277 | continue; |
| 278 | } |
| 279 | } |
| 280 | printf("Opening file.\n"); |
| 281 | |
| 282 | fd_pairs->pair = realloc(fd_pairs->pair, |
| 283 | ++fd_pairs->num_pairs * sizeof(struct fd_pair)); |
| 284 | |
| 285 | /* Open the channel in read mode */ |
| 286 | fd_pairs->pair[fd_pairs->num_pairs-1].channel = |
| 287 | open(path_channel, O_RDONLY | O_NONBLOCK); |
| 288 | if(fd_pairs->pair[fd_pairs->num_pairs-1].channel == -1) { |
| 289 | perror(path_channel); |
| 290 | fd_pairs->num_pairs--; |
| 291 | continue; |
| 292 | } |
| 293 | /* Open the trace in write mode, only append if append_mode */ |
| 294 | ret = stat(path_trace, &stat_buf); |
| 295 | if(ret == 0) { |
| 296 | if(append_mode) { |
| 297 | printf("Appending to file %s as requested\n", path_trace); |
| 298 | |
| 299 | fd_pairs->pair[fd_pairs->num_pairs-1].trace = |
| 300 | open(path_trace, O_WRONLY|O_APPEND, |
| 301 | S_IRWXU|S_IRWXG|S_IRWXO); |
| 302 | |
| 303 | if(fd_pairs->pair[fd_pairs->num_pairs-1].trace == -1) { |
| 304 | perror(path_trace); |
| 305 | } |
| 306 | } else { |
| 307 | printf("File %s exists, cannot open. Try append mode.\n", path_trace); |
| 308 | open_ret = -1; |
| 309 | goto end; |
| 310 | } |
| 311 | } else { |
| 312 | if(errno == ENOENT) { |
| 313 | fd_pairs->pair[fd_pairs->num_pairs-1].trace = |
| 314 | open(path_trace, O_WRONLY|O_CREAT|O_EXCL, |
| 315 | S_IRWXU|S_IRWXG|S_IRWXO); |
| 316 | if(fd_pairs->pair[fd_pairs->num_pairs-1].trace == -1) { |
| 317 | perror(path_trace); |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | end: |
| 325 | closedir(channel_dir); |
| 326 | |
| 327 | return open_ret; |
| 328 | } |
| 329 | |
| 330 | |
| 331 | int read_subbuffer(struct fd_pair *pair) |
| 332 | { |
| 333 | unsigned int consumed_old; |
| 334 | int err, ret=0; |
| 335 | |
| 336 | |
| 337 | err = ioctl(pair->channel, RELAY_GET_SUBBUF, |
| 338 | &consumed_old); |
| 339 | printf("cookie : %u\n", consumed_old); |
| 340 | if(err != 0) { |
| 341 | ret = errno; |
| 342 | perror("Reserving sub buffer failed (everything is normal, it is due to concurrency)"); |
| 343 | goto get_error; |
| 344 | } |
| 345 | |
| 346 | err = TEMP_FAILURE_RETRY(write(pair->trace, |
| 347 | pair->mmap |
| 348 | + (consumed_old & ((pair->n_subbufs * pair->subbuf_size)-1)), |
| 349 | pair->subbuf_size)); |
| 350 | |
| 351 | if(err < 0) { |
| 352 | ret = errno; |
| 353 | perror("Error in writing to file"); |
| 354 | goto write_error; |
| 355 | } |
| 356 | #if 0 |
| 357 | err = fsync(pair->trace); |
| 358 | if(err < 0) { |
| 359 | ret = errno; |
| 360 | perror("Error in writing to file"); |
| 361 | goto write_error; |
| 362 | } |
| 363 | #endif //0 |
| 364 | write_error: |
| 365 | err = ioctl(pair->channel, RELAY_PUT_SUBBUF, &consumed_old); |
| 366 | if(err != 0) { |
| 367 | ret = errno; |
| 368 | if(errno == EFAULT) { |
| 369 | perror("Error in unreserving sub buffer\n"); |
| 370 | } else if(errno == EIO) { |
| 371 | perror("Reader has been pushed by the writer, last subbuffer corrupted."); |
| 372 | /* FIXME : we may delete the last written buffer if we wish. */ |
| 373 | } |
| 374 | goto get_error; |
| 375 | } |
| 376 | |
| 377 | get_error: |
| 378 | return ret; |
| 379 | } |
| 380 | |
| 381 | |
| 382 | |
| 383 | int map_channels(struct channel_trace_fd *fd_pairs) |
| 384 | { |
| 385 | int i,j; |
| 386 | int ret=0; |
| 387 | |
| 388 | if(fd_pairs->num_pairs <= 0) { |
| 389 | printf("No channel to read\n"); |
| 390 | goto end; |
| 391 | } |
| 392 | |
| 393 | /* Get the subbuf sizes and number */ |
| 394 | |
| 395 | for(i=0;i<fd_pairs->num_pairs;i++) { |
| 396 | struct fd_pair *pair = &fd_pairs->pair[i]; |
| 397 | |
| 398 | ret = ioctl(pair->channel, RELAY_GET_N_SUBBUFS, |
| 399 | &pair->n_subbufs); |
| 400 | if(ret != 0) { |
| 401 | perror("Error in getting the number of subbuffers"); |
| 402 | goto end; |
| 403 | } |
| 404 | ret = ioctl(pair->channel, RELAY_GET_SUBBUF_SIZE, |
| 405 | &pair->subbuf_size); |
| 406 | if(ret != 0) { |
| 407 | perror("Error in getting the size of the subbuffers"); |
| 408 | goto end; |
| 409 | } |
| 410 | ret = pthread_mutex_init(&pair->mutex, NULL); /* Fast mutex */ |
| 411 | if(ret != 0) { |
| 412 | perror("Error in mutex init"); |
| 413 | goto end; |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | /* Mmap each FD */ |
| 418 | for(i=0;i<fd_pairs->num_pairs;i++) { |
| 419 | struct fd_pair *pair = &fd_pairs->pair[i]; |
| 420 | |
| 421 | pair->mmap = mmap(0, pair->subbuf_size * pair->n_subbufs, PROT_READ, |
| 422 | MAP_SHARED, pair->channel, 0); |
| 423 | if(pair->mmap == MAP_FAILED) { |
| 424 | perror("Mmap error"); |
| 425 | goto munmap; |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | goto end; /* success */ |
| 430 | |
| 431 | /* Error handling */ |
| 432 | /* munmap only the successfully mmapped indexes */ |
| 433 | munmap: |
| 434 | /* Munmap each FD */ |
| 435 | for(j=0;j<i;j++) { |
| 436 | struct fd_pair *pair = &fd_pairs->pair[j]; |
| 437 | int err_ret; |
| 438 | |
| 439 | err_ret = munmap(pair->mmap, pair->subbuf_size * pair->n_subbufs); |
| 440 | if(err_ret != 0) { |
| 441 | perror("Error in munmap"); |
| 442 | } |
| 443 | ret |= err_ret; |
| 444 | } |
| 445 | |
| 446 | end: |
| 447 | return ret; |
| 448 | |
| 449 | |
| 450 | } |
| 451 | |
| 452 | |
| 453 | int unmap_channels(struct channel_trace_fd *fd_pairs) |
| 454 | { |
| 455 | int j; |
| 456 | int ret=0; |
| 457 | |
| 458 | /* Munmap each FD */ |
| 459 | for(j=0;j<fd_pairs->num_pairs;j++) { |
| 460 | struct fd_pair *pair = &fd_pairs->pair[j]; |
| 461 | int err_ret; |
| 462 | |
| 463 | err_ret = munmap(pair->mmap, pair->subbuf_size * pair->n_subbufs); |
| 464 | if(err_ret != 0) { |
| 465 | perror("Error in munmap"); |
| 466 | } |
| 467 | ret |= err_ret; |
| 468 | err_ret = pthread_mutex_destroy(&pair->mutex); |
| 469 | if(err_ret != 0) { |
| 470 | perror("Error in mutex destroy"); |
| 471 | } |
| 472 | ret |= err_ret; |
| 473 | } |
| 474 | |
| 475 | return ret; |
| 476 | } |
| 477 | |
| 478 | |
| 479 | /* read_channels |
| 480 | * |
| 481 | * Thread worker. |
| 482 | * |
| 483 | * Read the debugfs channels and write them in the paired tracefiles. |
| 484 | * |
| 485 | * @fd_pairs : paired channels and trace files. |
| 486 | * |
| 487 | * returns (void*)0 on success, (void*)-1 on error. |
| 488 | * |
| 489 | * Note that the high priority polled channels are consumed first. We then poll |
| 490 | * again to see if these channels are still in priority. Only when no |
| 491 | * high priority channel is left, we start reading low priority channels. |
| 492 | * |
| 493 | * Note that a channel is considered high priority when the buffer is almost |
| 494 | * full. |
| 495 | */ |
| 496 | |
| 497 | void * read_channels(void *arg) |
| 498 | { |
| 499 | struct pollfd *pollfd; |
| 500 | int i,j; |
| 501 | int num_rdy, num_hup; |
| 502 | int high_prio; |
| 503 | int ret = 0; |
| 504 | struct channel_trace_fd *fd_pairs = (struct channel_trace_fd *)arg; |
| 505 | |
| 506 | /* Start polling the FD */ |
| 507 | |
| 508 | pollfd = malloc(fd_pairs->num_pairs * sizeof(struct pollfd)); |
| 509 | |
| 510 | /* Note : index in pollfd is the same index as fd_pair->pair */ |
| 511 | for(i=0;i<fd_pairs->num_pairs;i++) { |
| 512 | pollfd[i].fd = fd_pairs->pair[i].channel; |
| 513 | pollfd[i].events = POLLIN|POLLPRI; |
| 514 | } |
| 515 | |
| 516 | while(1) { |
| 517 | high_prio = 0; |
| 518 | num_hup = 0; |
| 519 | #ifdef DEBUG |
| 520 | printf("Press a key for next poll...\n"); |
| 521 | char buf[1]; |
| 522 | read(STDIN_FILENO, &buf, 1); |
| 523 | printf("Next poll (polling %d fd) :\n", fd_pairs->num_pairs); |
| 524 | #endif //DEBUG |
| 525 | |
| 526 | /* Have we received a signal ? */ |
| 527 | if(quit_program) break; |
| 528 | |
| 529 | num_rdy = poll(pollfd, fd_pairs->num_pairs, -1); |
| 530 | if(num_rdy == -1) { |
| 531 | perror("Poll error"); |
| 532 | goto free_fd; |
| 533 | } |
| 534 | |
| 535 | printf("Data received\n"); |
| 536 | |
| 537 | for(i=0;i<fd_pairs->num_pairs;i++) { |
| 538 | switch(pollfd[i].revents) { |
| 539 | case POLLERR: |
| 540 | printf("Error returned in polling fd %d.\n", pollfd[i].fd); |
| 541 | num_hup++; |
| 542 | break; |
| 543 | case POLLHUP: |
| 544 | printf("Polling fd %d tells it has hung up.\n", pollfd[i].fd); |
| 545 | num_hup++; |
| 546 | break; |
| 547 | case POLLNVAL: |
| 548 | printf("Polling fd %d tells fd is not open.\n", pollfd[i].fd); |
| 549 | num_hup++; |
| 550 | break; |
| 551 | case POLLPRI: |
| 552 | if(pthread_mutex_trylock(&fd_pairs->pair[i].mutex) == 0) { |
| 553 | printf("Urgent read on fd %d\n", pollfd[i].fd); |
| 554 | /* Take care of high priority channels first. */ |
| 555 | high_prio = 1; |
| 556 | /* it's ok to have an unavailable subbuffer */ |
| 557 | ret = read_subbuffer(&fd_pairs->pair[i]); |
| 558 | if(ret == EAGAIN) ret = 0; |
| 559 | |
| 560 | ret = pthread_mutex_unlock(&fd_pairs->pair[i].mutex); |
| 561 | if(ret) |
| 562 | printf("Error in mutex unlock : %s\n", strerror(ret)); |
| 563 | } |
| 564 | break; |
| 565 | } |
| 566 | } |
| 567 | /* If every FD has hung up, we end the read loop here */ |
| 568 | if(num_hup == fd_pairs->num_pairs) break; |
| 569 | |
| 570 | if(!high_prio) { |
| 571 | for(i=0;i<fd_pairs->num_pairs;i++) { |
| 572 | switch(pollfd[i].revents) { |
| 573 | case POLLIN: |
| 574 | if(pthread_mutex_trylock(&fd_pairs->pair[i].mutex) == 0) { |
| 575 | /* Take care of low priority channels. */ |
| 576 | printf("Normal read on fd %d\n", pollfd[i].fd); |
| 577 | /* it's ok to have an unavailable subbuffer */ |
| 578 | ret = read_subbuffer(&fd_pairs->pair[i]); |
| 579 | if(ret == EAGAIN) ret = 0; |
| 580 | |
| 581 | ret = pthread_mutex_unlock(&fd_pairs->pair[i].mutex); |
| 582 | if(ret) |
| 583 | printf("Error in mutex unlock : %s\n", strerror(ret)); |
| 584 | } |
| 585 | break; |
| 586 | } |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | } |
| 591 | |
| 592 | free_fd: |
| 593 | free(pollfd); |
| 594 | |
| 595 | end: |
| 596 | return (void*)ret; |
| 597 | } |
| 598 | |
| 599 | |
| 600 | void close_channel_trace_pairs(struct channel_trace_fd *fd_pairs) |
| 601 | { |
| 602 | int i; |
| 603 | int ret; |
| 604 | |
| 605 | for(i=0;i<fd_pairs->num_pairs;i++) { |
| 606 | ret = close(fd_pairs->pair[i].channel); |
| 607 | if(ret == -1) perror("Close error on channel"); |
| 608 | ret = close(fd_pairs->pair[i].trace); |
| 609 | if(ret == -1) perror("Close error on trace"); |
| 610 | } |
| 611 | free(fd_pairs->pair); |
| 612 | } |
| 613 | |
| 614 | int main(int argc, char ** argv) |
| 615 | { |
| 616 | int ret = 0; |
| 617 | struct channel_trace_fd fd_pairs = { NULL, 0 }; |
| 618 | struct sigaction act; |
| 619 | pthread_t *tids; |
| 620 | unsigned int i; |
| 621 | void *tret; |
| 622 | |
| 623 | ret = parse_arguments(argc, argv); |
| 624 | |
| 625 | if(ret != 0) show_arguments(); |
| 626 | if(ret < 0) return EINVAL; |
| 627 | if(ret > 0) return 0; |
| 628 | |
| 629 | show_info(); |
| 630 | |
| 631 | if(daemon_mode) { |
| 632 | ret = daemon(0, 0); |
| 633 | |
| 634 | if(ret == -1) { |
| 635 | perror("An error occured while daemonizing."); |
| 636 | exit(-1); |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | /* Connect the signal handlers */ |
| 641 | act.sa_handler = handler; |
| 642 | act.sa_flags = 0; |
| 643 | sigemptyset(&(act.sa_mask)); |
| 644 | sigaddset(&(act.sa_mask), SIGTERM); |
| 645 | sigaddset(&(act.sa_mask), SIGQUIT); |
| 646 | sigaddset(&(act.sa_mask), SIGINT); |
| 647 | sigaction(SIGTERM, &act, NULL); |
| 648 | sigaction(SIGQUIT, &act, NULL); |
| 649 | sigaction(SIGINT, &act, NULL); |
| 650 | |
| 651 | |
| 652 | if(ret = open_channel_trace_pairs(channel_name, trace_name, &fd_pairs)) |
| 653 | goto close_channel; |
| 654 | |
| 655 | if(ret = map_channels(&fd_pairs)) |
| 656 | goto close_channel; |
| 657 | |
| 658 | tids = malloc(sizeof(pthread_t) * num_threads); |
| 659 | for(i=0; i<num_threads; i++) { |
| 660 | ret = pthread_create(&tids[i], NULL, read_channels, &fd_pairs); |
| 661 | if(ret) { |
| 662 | perror("Error creating thread"); |
| 663 | break; |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | for(i=0; i<num_threads; i++) { |
| 668 | ret = pthread_join(tids[i], &tret); |
| 669 | if(ret) { |
| 670 | perror("Error joining thread"); |
| 671 | break; |
| 672 | } |
| 673 | if((int)tret != 0) { |
| 674 | printf("Error %s occured in thread %u\n", strerror((int)tret), i); |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | free(tids); |
| 679 | |
| 680 | ret |= unmap_channels(&fd_pairs); |
| 681 | |
| 682 | close_channel: |
| 683 | close_channel_trace_pairs(&fd_pairs); |
| 684 | |
| 685 | return ret; |
| 686 | } |