| 1 | /* lttd |
| 2 | * |
| 3 | * Linux Trace Toolkit Daemon |
| 4 | * |
| 5 | * This is a simple daemon that reads a few relayfs channels and save them in a |
| 6 | * 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 _GNU_SOURCE |
| 18 | #include <stdio.h> |
| 19 | #include <unistd.h> |
| 20 | #include <errno.h> |
| 21 | #include <sys/types.h> |
| 22 | #include <sys/stat.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <dirent.h> |
| 25 | #include <string.h> |
| 26 | #include <fcntl.h> |
| 27 | #include <sys/poll.h> |
| 28 | #include <sys/mman.h> |
| 29 | #include <signal.h> |
| 30 | |
| 31 | /* Relayfs IOCTL */ |
| 32 | #include <asm/ioctl.h> |
| 33 | #include <asm/types.h> |
| 34 | |
| 35 | /* Get the next sub buffer that can be read. */ |
| 36 | #define RELAYFS_GET_SUBBUF _IOR(0xF4, 0x00,__u32) |
| 37 | /* Release the oldest reserved (by "get") sub buffer. */ |
| 38 | #define RELAYFS_PUT_SUBBUF _IO(0xF4, 0x01) |
| 39 | /* returns the number of sub buffers in the per cpu channel. */ |
| 40 | #define RELAYFS_GET_N_SUBBUFS _IOR(0xF4, 0x02,__u32) |
| 41 | /* returns the size of the sub buffers. */ |
| 42 | #define RELAYFS_GET_SUBBUF_SIZE _IOR(0xF4, 0x03,__u32) |
| 43 | |
| 44 | |
| 45 | |
| 46 | enum { |
| 47 | GET_SUBBUF, |
| 48 | PUT_SUBBUF, |
| 49 | GET_N_BUBBUFS, |
| 50 | GET_SUBBUF_SIZE |
| 51 | }; |
| 52 | |
| 53 | struct fd_pair { |
| 54 | int channel; |
| 55 | int trace; |
| 56 | unsigned int n_subbufs; |
| 57 | unsigned int subbuf_size; |
| 58 | void *mmap; |
| 59 | }; |
| 60 | |
| 61 | struct channel_trace_fd { |
| 62 | struct fd_pair *pair; |
| 63 | int num_pairs; |
| 64 | }; |
| 65 | |
| 66 | static char *trace_name = NULL; |
| 67 | static char *channel_name = NULL; |
| 68 | static int daemon_mode = 0; |
| 69 | static int append_mode = 0; |
| 70 | static int sig_parent = 0; |
| 71 | volatile static int quit_program = 0; /* For signal handler */ |
| 72 | |
| 73 | /* Args : |
| 74 | * |
| 75 | * -t directory Directory name of the trace to write to. Will be created. |
| 76 | * -c directory Root directory of the relayfs trace channels. |
| 77 | * -d Run in background (daemon). |
| 78 | * -a Trace append mode. |
| 79 | * -s Send SIGIO to parent when ready for IO. |
| 80 | */ |
| 81 | void show_arguments(void) |
| 82 | { |
| 83 | printf("Please use the following arguments :\n"); |
| 84 | printf("\n"); |
| 85 | printf("-t directory Directory name of the trace to write to.\n" |
| 86 | " It will be created.\n"); |
| 87 | printf("-c directory Root directory of the relayfs trace channels.\n"); |
| 88 | printf("-d Run in background (daemon).\n"); |
| 89 | printf("-a Append to an possibly existing trace.\n"); |
| 90 | printf("-s Send SIGIO to parent when ready for IO.\n"); |
| 91 | printf("\n"); |
| 92 | } |
| 93 | |
| 94 | |
| 95 | /* parse_arguments |
| 96 | * |
| 97 | * Parses the command line arguments. |
| 98 | * |
| 99 | * Returns 1 if the arguments were correct, but doesn't ask for program |
| 100 | * continuation. Returns -1 if the arguments are incorrect, or 0 if OK. |
| 101 | */ |
| 102 | int parse_arguments(int argc, char **argv) |
| 103 | { |
| 104 | int ret = 0; |
| 105 | int argn = 1; |
| 106 | |
| 107 | if(argc == 2) { |
| 108 | if(strcmp(argv[1], "-h") == 0) { |
| 109 | return 1; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | while(argn < argc) { |
| 114 | |
| 115 | switch(argv[argn][0]) { |
| 116 | case '-': |
| 117 | switch(argv[argn][1]) { |
| 118 | case 't': |
| 119 | if(argn+1 < argc) { |
| 120 | trace_name = argv[argn+1]; |
| 121 | argn++; |
| 122 | } |
| 123 | break; |
| 124 | case 'c': |
| 125 | if(argn+1 < argc) { |
| 126 | channel_name = argv[argn+1]; |
| 127 | argn++; |
| 128 | } |
| 129 | break; |
| 130 | case 'd': |
| 131 | daemon_mode = 1; |
| 132 | break; |
| 133 | case 'a': |
| 134 | append_mode = 1; |
| 135 | break; |
| 136 | case 's': |
| 137 | sig_parent = 1; |
| 138 | break; |
| 139 | default: |
| 140 | printf("Invalid argument '%s'.\n", argv[argn]); |
| 141 | printf("\n"); |
| 142 | ret = -1; |
| 143 | } |
| 144 | break; |
| 145 | default: |
| 146 | printf("Invalid argument '%s'.\n", argv[argn]); |
| 147 | printf("\n"); |
| 148 | ret = -1; |
| 149 | } |
| 150 | argn++; |
| 151 | } |
| 152 | |
| 153 | if(trace_name == NULL) { |
| 154 | printf("Please specify a trace name.\n"); |
| 155 | printf("\n"); |
| 156 | ret = -1; |
| 157 | } |
| 158 | |
| 159 | if(channel_name == NULL) { |
| 160 | printf("Please specify a channel name.\n"); |
| 161 | printf("\n"); |
| 162 | ret = -1; |
| 163 | } |
| 164 | |
| 165 | return ret; |
| 166 | } |
| 167 | |
| 168 | void show_info(void) |
| 169 | { |
| 170 | printf("Linux Trace Toolkit Trace Daemon\n"); |
| 171 | printf("\n"); |
| 172 | printf("Reading from relayfs directory : %s\n", channel_name); |
| 173 | printf("Writing to trace directory : %s\n", trace_name); |
| 174 | printf("\n"); |
| 175 | } |
| 176 | |
| 177 | |
| 178 | /* signal handling */ |
| 179 | |
| 180 | static void handler(int signo) |
| 181 | { |
| 182 | printf("Signal %d received : exiting cleanly\n", signo); |
| 183 | quit_program = 1; |
| 184 | } |
| 185 | |
| 186 | |
| 187 | |
| 188 | int open_channel_trace_pairs(char *subchannel_name, char *subtrace_name, |
| 189 | struct channel_trace_fd *fd_pairs) |
| 190 | { |
| 191 | DIR *channel_dir = opendir(subchannel_name); |
| 192 | struct dirent *entry; |
| 193 | struct stat stat_buf; |
| 194 | int ret; |
| 195 | char path_channel[PATH_MAX]; |
| 196 | int path_channel_len; |
| 197 | char *path_channel_ptr; |
| 198 | char path_trace[PATH_MAX]; |
| 199 | int path_trace_len; |
| 200 | char *path_trace_ptr; |
| 201 | |
| 202 | if(channel_dir == NULL) { |
| 203 | perror(subchannel_name); |
| 204 | return ENOENT; |
| 205 | } |
| 206 | |
| 207 | printf("Creating trace subdirectory %s\n", subtrace_name); |
| 208 | ret = mkdir(subtrace_name, S_IRWXU|S_IRWXG|S_IRWXO); |
| 209 | if(ret == -1) { |
| 210 | if(errno == EEXIST && append_mode) { |
| 211 | printf("Appending to directory %s as resquested\n", subtrace_name); |
| 212 | } else { |
| 213 | perror(subtrace_name); |
| 214 | return -1; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | strncpy(path_channel, subchannel_name, PATH_MAX-1); |
| 219 | path_channel_len = strlen(path_channel); |
| 220 | path_channel[path_channel_len] = '/'; |
| 221 | path_channel_len++; |
| 222 | path_channel_ptr = path_channel + path_channel_len; |
| 223 | |
| 224 | strncpy(path_trace, subtrace_name, PATH_MAX-1); |
| 225 | path_trace_len = strlen(path_trace); |
| 226 | path_trace[path_trace_len] = '/'; |
| 227 | path_trace_len++; |
| 228 | path_trace_ptr = path_trace + path_trace_len; |
| 229 | |
| 230 | while((entry = readdir(channel_dir)) != NULL) { |
| 231 | |
| 232 | if(entry->d_name[0] == '.') continue; |
| 233 | |
| 234 | strncpy(path_channel_ptr, entry->d_name, PATH_MAX - path_channel_len); |
| 235 | strncpy(path_trace_ptr, entry->d_name, PATH_MAX - path_trace_len); |
| 236 | |
| 237 | ret = stat(path_channel, &stat_buf); |
| 238 | if(ret == -1) { |
| 239 | perror(path_channel); |
| 240 | continue; |
| 241 | } |
| 242 | |
| 243 | printf("Channel file : %s\n", path_channel); |
| 244 | |
| 245 | if(S_ISDIR(stat_buf.st_mode)) { |
| 246 | |
| 247 | printf("Entering channel subdirectory...\n"); |
| 248 | ret = open_channel_trace_pairs(path_channel, path_trace, fd_pairs); |
| 249 | if(ret < 0) continue; |
| 250 | } else if(S_ISREG(stat_buf.st_mode)) { |
| 251 | printf("Opening file.\n"); |
| 252 | |
| 253 | fd_pairs->pair = realloc(fd_pairs->pair, |
| 254 | ++fd_pairs->num_pairs * sizeof(struct fd_pair)); |
| 255 | |
| 256 | /* Open the channel in read mode */ |
| 257 | fd_pairs->pair[fd_pairs->num_pairs-1].channel = |
| 258 | open(path_channel, O_RDONLY | O_NONBLOCK); |
| 259 | if(fd_pairs->pair[fd_pairs->num_pairs-1].channel == -1) { |
| 260 | perror(path_channel); |
| 261 | fd_pairs->num_pairs--; |
| 262 | continue; |
| 263 | } |
| 264 | /* Open the trace in write mode, only append if append_mode */ |
| 265 | ret = stat(path_trace, &stat_buf); |
| 266 | if(ret == 0) { |
| 267 | if(append_mode) { |
| 268 | printf("Appending to file %s as requested\n", path_trace); |
| 269 | |
| 270 | fd_pairs->pair[fd_pairs->num_pairs-1].trace = |
| 271 | open(path_trace, O_WRONLY|O_APPEND, |
| 272 | S_IRWXU|S_IRWXG|S_IRWXO); |
| 273 | |
| 274 | if(fd_pairs->pair[fd_pairs->num_pairs-1].trace == -1) { |
| 275 | perror(path_trace); |
| 276 | } |
| 277 | } else { |
| 278 | printf("File %s exists, cannot open. Try append mode.\n", path_trace); |
| 279 | return -1; |
| 280 | } |
| 281 | } else { |
| 282 | if(errno == ENOENT) { |
| 283 | fd_pairs->pair[fd_pairs->num_pairs-1].trace = |
| 284 | open(path_trace, O_WRONLY|O_CREAT|O_EXCL, |
| 285 | S_IRWXU|S_IRWXG|S_IRWXO); |
| 286 | if(fd_pairs->pair[fd_pairs->num_pairs-1].trace == -1) { |
| 287 | perror(path_trace); |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | closedir(channel_dir); |
| 295 | |
| 296 | return 0; |
| 297 | } |
| 298 | |
| 299 | |
| 300 | int read_subbuffer(struct fd_pair *pair) |
| 301 | { |
| 302 | unsigned int subbuf_index; |
| 303 | int err, ret; |
| 304 | |
| 305 | |
| 306 | err = ioctl(pair->channel, RELAYFS_GET_SUBBUF, |
| 307 | &subbuf_index); |
| 308 | printf("index : %u\n", subbuf_index); |
| 309 | if(err != 0) { |
| 310 | perror("Error in reserving sub buffer"); |
| 311 | ret = -EPERM; |
| 312 | goto get_error; |
| 313 | } |
| 314 | |
| 315 | err = TEMP_FAILURE_RETRY(write(pair->trace, |
| 316 | pair->mmap + (subbuf_index * pair->subbuf_size), |
| 317 | pair->subbuf_size)); |
| 318 | |
| 319 | if(err < 0) { |
| 320 | perror("Error in writing to file"); |
| 321 | ret = err; |
| 322 | goto write_error; |
| 323 | } |
| 324 | |
| 325 | |
| 326 | write_error: |
| 327 | err = ioctl(pair->channel, RELAYFS_PUT_SUBBUF); |
| 328 | if(err != 0) { |
| 329 | perror("Error in unreserving sub buffer"); |
| 330 | ret = -EPERM; |
| 331 | goto get_error; |
| 332 | } |
| 333 | |
| 334 | get_error: |
| 335 | return ret; |
| 336 | } |
| 337 | |
| 338 | |
| 339 | /* read_channels |
| 340 | * |
| 341 | * Read the realyfs channels and write them in the paired tracefiles. |
| 342 | * |
| 343 | * @fd_pairs : paired channels and trace files. |
| 344 | * |
| 345 | * returns 0 on success, -1 on error. |
| 346 | * |
| 347 | * Note that the high priority polled channels are consumed first. We then poll |
| 348 | * again to see if these channels are still in priority. Only when no |
| 349 | * high priority channel is left, we start reading low priority channels. |
| 350 | * |
| 351 | * Note that a channel is considered high priority when the buffer is almost |
| 352 | * full. |
| 353 | */ |
| 354 | |
| 355 | int read_channels(struct channel_trace_fd *fd_pairs) |
| 356 | { |
| 357 | struct pollfd *pollfd; |
| 358 | int i,j; |
| 359 | int num_rdy, num_hup; |
| 360 | int high_prio; |
| 361 | int ret; |
| 362 | |
| 363 | if(fd_pairs->num_pairs <= 0) { |
| 364 | printf("No channel to read\n"); |
| 365 | goto end; |
| 366 | } |
| 367 | |
| 368 | /* Get the subbuf sizes and number */ |
| 369 | |
| 370 | for(i=0;i<fd_pairs->num_pairs;i++) { |
| 371 | struct fd_pair *pair = &fd_pairs->pair[i]; |
| 372 | |
| 373 | ret = ioctl(pair->channel, RELAYFS_GET_N_SUBBUFS, |
| 374 | &pair->n_subbufs); |
| 375 | if(ret != 0) { |
| 376 | perror("Error in getting the number of subbuffers"); |
| 377 | goto end; |
| 378 | } |
| 379 | ret = ioctl(pair->channel, RELAYFS_GET_SUBBUF_SIZE, |
| 380 | &pair->subbuf_size); |
| 381 | if(ret != 0) { |
| 382 | perror("Error in getting the size of the subbuffers"); |
| 383 | goto end; |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | /* Mmap each FD */ |
| 388 | for(i=0;i<fd_pairs->num_pairs;i++) { |
| 389 | struct fd_pair *pair = &fd_pairs->pair[i]; |
| 390 | |
| 391 | pair->mmap = mmap(0, pair->subbuf_size * pair->n_subbufs, PROT_READ, |
| 392 | MAP_SHARED, pair->channel, 0); |
| 393 | if(pair->mmap == MAP_FAILED) { |
| 394 | perror("Mmap error"); |
| 395 | goto munmap; |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | |
| 400 | /* Start polling the FD */ |
| 401 | |
| 402 | pollfd = malloc(fd_pairs->num_pairs * sizeof(struct pollfd)); |
| 403 | |
| 404 | /* Note : index in pollfd is the same index as fd_pair->pair */ |
| 405 | for(i=0;i<fd_pairs->num_pairs;i++) { |
| 406 | pollfd[i].fd = fd_pairs->pair[i].channel; |
| 407 | pollfd[i].events = POLLIN|POLLPRI; |
| 408 | } |
| 409 | |
| 410 | /* Signal the parent that ready for IO */ |
| 411 | if(sig_parent) kill(getppid(), SIGIO); |
| 412 | |
| 413 | while(1) { |
| 414 | high_prio = 0; |
| 415 | num_hup = 0; |
| 416 | #ifdef DEBUG |
| 417 | printf("Press a key for next poll...\n"); |
| 418 | char buf[1]; |
| 419 | read(STDIN_FILENO, &buf, 1); |
| 420 | printf("Next poll (polling %d fd) :\n", fd_pairs->num_pairs); |
| 421 | #endif //DEBUG |
| 422 | |
| 423 | /* Have we received a signal ? */ |
| 424 | if(quit_program) break; |
| 425 | |
| 426 | num_rdy = poll(pollfd, fd_pairs->num_pairs, -1); |
| 427 | if(num_rdy == -1) { |
| 428 | perror("Poll error"); |
| 429 | goto free_fd; |
| 430 | } |
| 431 | |
| 432 | printf("Data received\n"); |
| 433 | |
| 434 | for(i=0;i<fd_pairs->num_pairs;i++) { |
| 435 | switch(pollfd[i].revents) { |
| 436 | case POLLERR: |
| 437 | printf("Error returned in polling fd %d.\n", pollfd[i].fd); |
| 438 | num_hup++; |
| 439 | break; |
| 440 | case POLLHUP: |
| 441 | printf("Polling fd %d tells it has hung up.\n", pollfd[i].fd); |
| 442 | num_hup++; |
| 443 | break; |
| 444 | case POLLNVAL: |
| 445 | printf("Polling fd %d tells fd is not open.\n", pollfd[i].fd); |
| 446 | num_hup++; |
| 447 | break; |
| 448 | case POLLPRI: |
| 449 | printf("Urgent read on fd %d\n", pollfd[i].fd); |
| 450 | /* Take care of high priority channels first. */ |
| 451 | high_prio = 1; |
| 452 | ret |= read_subbuffer(&fd_pairs->pair[i]); |
| 453 | break; |
| 454 | } |
| 455 | } |
| 456 | /* If every FD has hung up, we end the read loop here */ |
| 457 | if(num_hup == fd_pairs->num_pairs) break; |
| 458 | |
| 459 | if(!high_prio) { |
| 460 | for(i=0;i<fd_pairs->num_pairs;i++) { |
| 461 | switch(pollfd[i].revents) { |
| 462 | case POLLIN: |
| 463 | /* Take care of low priority channels. */ |
| 464 | printf("Normal read on fd %d\n", pollfd[i].fd); |
| 465 | ret |= read_subbuffer(&fd_pairs->pair[i]); |
| 466 | break; |
| 467 | } |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | } |
| 472 | |
| 473 | free_fd: |
| 474 | free(pollfd); |
| 475 | |
| 476 | /* munmap only the successfully mmapped indexes */ |
| 477 | i = fd_pairs->num_pairs; |
| 478 | munmap: |
| 479 | /* Munmap each FD */ |
| 480 | for(j=0;j<i;j++) { |
| 481 | struct fd_pair *pair = &fd_pairs->pair[j]; |
| 482 | int err_ret; |
| 483 | |
| 484 | err_ret = munmap(pair->mmap, pair->subbuf_size * pair->n_subbufs); |
| 485 | if(err_ret != 0) { |
| 486 | perror("Error in munmap"); |
| 487 | } |
| 488 | ret |= err_ret; |
| 489 | } |
| 490 | |
| 491 | end: |
| 492 | return ret; |
| 493 | } |
| 494 | |
| 495 | |
| 496 | void close_channel_trace_pairs(struct channel_trace_fd *fd_pairs) |
| 497 | { |
| 498 | int i; |
| 499 | int ret; |
| 500 | |
| 501 | for(i=0;i<fd_pairs->num_pairs;i++) { |
| 502 | ret = close(fd_pairs->pair[i].channel); |
| 503 | if(ret == -1) perror("Close error on channel"); |
| 504 | ret = close(fd_pairs->pair[i].trace); |
| 505 | if(ret == -1) perror("Close error on trace"); |
| 506 | } |
| 507 | free(fd_pairs->pair); |
| 508 | } |
| 509 | |
| 510 | int main(int argc, char ** argv) |
| 511 | { |
| 512 | int ret; |
| 513 | pid_t pid; |
| 514 | struct channel_trace_fd fd_pairs = { NULL, 0 }; |
| 515 | struct sigaction act; |
| 516 | |
| 517 | ret = parse_arguments(argc, argv); |
| 518 | |
| 519 | if(ret != 0) show_arguments(); |
| 520 | if(ret < 0) return EINVAL; |
| 521 | if(ret > 0) return 0; |
| 522 | |
| 523 | show_info(); |
| 524 | |
| 525 | if(daemon_mode) { |
| 526 | pid = fork(); |
| 527 | |
| 528 | if(pid > 0) { |
| 529 | /* parent */ |
| 530 | return 0; |
| 531 | } else if(pid < 0) { |
| 532 | /* error */ |
| 533 | printf("An error occured while forking.\n"); |
| 534 | return -1; |
| 535 | } |
| 536 | /* else, we are the child, continue... */ |
| 537 | } |
| 538 | |
| 539 | /* Connect the signal handlers */ |
| 540 | act.sa_handler = handler; |
| 541 | act.sa_flags = 0; |
| 542 | sigemptyset(&(act.sa_mask)); |
| 543 | sigaddset(&(act.sa_mask), SIGTERM); |
| 544 | sigaddset(&(act.sa_mask), SIGQUIT); |
| 545 | sigaddset(&(act.sa_mask), SIGINT); |
| 546 | sigaction(SIGTERM, &act, NULL); |
| 547 | sigaction(SIGQUIT, &act, NULL); |
| 548 | sigaction(SIGINT, &act, NULL); |
| 549 | |
| 550 | //return 0; |
| 551 | if(ret = open_channel_trace_pairs(channel_name, trace_name, &fd_pairs)) |
| 552 | goto close_channel; |
| 553 | |
| 554 | ret = read_channels(&fd_pairs); |
| 555 | |
| 556 | close_channel: |
| 557 | close_channel_trace_pairs(&fd_pairs); |
| 558 | |
| 559 | return ret; |
| 560 | } |