Ben Cheng | 2b16b5f | 2008-10-23 16:07:43 -0700 | [diff] [blame^] | 1 | /** |
| 2 | * @file daemon/opd_pipe.c |
| 3 | * Functions handling the $SESSIONDIR/opd_pipe FIFO special file. |
| 4 | * NOTE: This code is dealing with potentially insecure input. |
| 5 | * |
| 6 | * @remark Copyright 2008 OProfile authors |
| 7 | * @remark Read the file COPYING |
| 8 | * |
| 9 | * @author Daniel Hansel |
| 10 | */ |
| 11 | |
| 12 | #include "opd_pipe.h" |
| 13 | #include "opd_printf.h" |
| 14 | #include "op_config.h" |
| 15 | |
| 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
| 19 | #include <fcntl.h> |
| 20 | #include <unistd.h> |
| 21 | #include <errno.h> |
| 22 | #include <sys/stat.h> |
| 23 | |
| 24 | static int fifo; |
| 25 | |
| 26 | void opd_create_pipe(void) |
| 27 | { |
| 28 | mode_t orig_umask = umask(0111); |
| 29 | if (mkfifo(op_pipe_file, 0666) == -1) { |
| 30 | if (errno != EEXIST) { |
| 31 | perror("oprofiled: couldn't create pipe: "); |
| 32 | exit(EXIT_FAILURE); |
| 33 | } |
| 34 | } |
| 35 | umask(orig_umask); |
| 36 | } |
| 37 | |
| 38 | |
| 39 | void opd_open_pipe(void) |
| 40 | { |
| 41 | fifo = open(op_pipe_file, O_RDONLY | O_NONBLOCK); |
| 42 | if (fifo == -1) { |
| 43 | perror("oprofiled: couldn't open pipe: "); |
| 44 | exit(EXIT_FAILURE); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | |
| 49 | void opd_close_pipe(void) |
| 50 | { |
| 51 | close(fifo); |
| 52 | } |
| 53 | |
| 54 | |
| 55 | int is_jitconv_requested(void) |
| 56 | { |
| 57 | /* number of dropped (unknown) requests */ |
| 58 | static long nr_drops = 0; |
| 59 | /* modulus to output only a few warnings to avoid flooding oprofiled.log */ |
| 60 | static int mod_cnt_drops = 1; |
| 61 | FILE * fd; |
| 62 | char line[256]; |
| 63 | int i, ret = 0; |
| 64 | |
| 65 | /* get a file descriptor to the pipe */ |
| 66 | fd = fdopen(fifo, "r"); |
| 67 | |
| 68 | if (fd == NULL) { |
| 69 | perror("oprofiled: couldn't create file descriptor: "); |
| 70 | exit(EXIT_FAILURE); |
| 71 | } |
| 72 | |
| 73 | /* read up to 99 lines to check for 'do_jitconv' */ |
| 74 | for (i = 0; i < 99; i++) { |
| 75 | /* just break if no new line is found */ |
| 76 | if (fgets(line, 256, fd) == NULL) |
| 77 | break; |
| 78 | line[strlen(line) - 1] = '\0'; |
| 79 | |
| 80 | if (strstr(line, "do_jitconv") != NULL) { |
| 81 | ret = 1; |
| 82 | } else { |
| 83 | nr_drops++; |
| 84 | |
| 85 | if (nr_drops % mod_cnt_drops == 0) { |
| 86 | printf( |
| 87 | "Warning: invalid pipe request received (dropped request(s): %ld)\n", |
| 88 | nr_drops); |
| 89 | /* increase modulus to avoid flooding log file */ |
| 90 | mod_cnt_drops *= 5; |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | return ret; |
| 96 | } |