Harald Welte | b7e4023 | 2016-07-29 14:26:50 +0200 | [diff] [blame] | 1 | #include <time.h> |
| 2 | #include <fcntl.h> |
| 3 | #include <string.h> |
| 4 | #include <errno.h> |
| 5 | #include <unistd.h> |
| 6 | #include <sys/stat.h> |
Harald Welte | 6f45dcd | 2019-11-06 06:55:47 +0100 | [diff] [blame] | 7 | #include <sys/uio.h> |
Harald Welte | b7e4023 | 2016-07-29 14:26:50 +0200 | [diff] [blame] | 8 | |
Harald Welte | 39cfbf4 | 2016-07-28 09:04:11 +0200 | [diff] [blame] | 9 | #include <osmocom/core/msgb.h> |
Harald Welte | dbb0f5a | 2016-10-18 23:51:54 +0200 | [diff] [blame] | 10 | #include <osmocom/core/talloc.h> |
Harald Welte | 39cfbf4 | 2016-07-28 09:04:11 +0200 | [diff] [blame] | 11 | #include <osmocom/abis/e1_input.h> |
| 12 | |
| 13 | #include "storage.h" |
Harald Welte | b7e4023 | 2016-07-29 14:26:50 +0200 | [diff] [blame] | 14 | #include "recorder.h" |
| 15 | |
| 16 | static int g_out_fd = -1;; |
| 17 | static uint64_t g_written_bytes; |
| 18 | |
| 19 | static const char *storage_gen_filename(void) |
| 20 | { |
| 21 | static char buf[32]; |
| 22 | time_t t; |
| 23 | struct tm *tmp; |
| 24 | |
| 25 | t = time(NULL); |
| 26 | tmp = localtime(&t); |
| 27 | strftime(buf, sizeof(buf), "%Y%m%d%H%M%S.e1cap", tmp); |
| 28 | return buf; |
| 29 | } |
| 30 | |
Harald Welte | df7a306 | 2016-10-19 10:47:17 +0200 | [diff] [blame] | 31 | void storage_close(void) |
| 32 | { |
| 33 | LOGP(DMAIN, LOGL_INFO, "Closing Log file\n"); |
| 34 | close(g_out_fd); |
| 35 | g_out_fd = -1; |
| 36 | } |
| 37 | |
Harald Welte | b7e4023 | 2016-07-29 14:26:50 +0200 | [diff] [blame] | 38 | static int storage_reopen_if_needed(void) |
| 39 | { |
| 40 | if (g_written_bytes / (1024*1024) >= g_recorder.max_file_size_mb) { |
Harald Welte | df7a306 | 2016-10-19 10:47:17 +0200 | [diff] [blame] | 41 | storage_close(); |
| 42 | /* we re-open below */ |
Harald Welte | b7e4023 | 2016-07-29 14:26:50 +0200 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | if (g_out_fd < 0) { |
| 46 | int rc; |
| 47 | const char *fname = storage_gen_filename(); |
Harald Welte | df7a306 | 2016-10-19 10:47:17 +0200 | [diff] [blame] | 48 | LOGP(DMAIN, LOGL_INFO, "Opening Log file %s\n", fname); |
Harald Welte | b7e4023 | 2016-07-29 14:26:50 +0200 | [diff] [blame] | 49 | rc = chdir(g_recorder.storage_path); |
| 50 | if (rc < 0) { |
| 51 | LOGP(DMAIN, LOGL_ERROR, "Unable to chdir(%s): %s\n", |
| 52 | g_recorder.storage_path, strerror(errno)); |
| 53 | return -1; |
| 54 | } |
| 55 | g_out_fd = open(fname, O_WRONLY|O_CREAT, 0664); |
| 56 | if (g_out_fd < 0) { |
| 57 | LOGP(DMAIN, LOGL_ERROR, "Unable to open(%s): %s\n", |
| 58 | fname, strerror(errno)); |
| 59 | } |
| 60 | g_written_bytes = 0; |
| 61 | } |
| 62 | |
| 63 | return g_out_fd; |
| 64 | } |
Harald Welte | 39cfbf4 | 2016-07-28 09:04:11 +0200 | [diff] [blame] | 65 | |
| 66 | int e1frame_store(struct e1inp_ts *ts, struct msgb *msg, enum osmo_e1cap_capture_mode mode) |
| 67 | { |
Harald Welte | b7e4023 | 2016-07-29 14:26:50 +0200 | [diff] [blame] | 68 | struct osmo_e1cap_pkthdr _h, *h = &_h; |
Harald Welte | 6df0729 | 2019-11-06 16:21:55 +0100 | [diff] [blame] | 69 | struct timeval tv; |
Harald Welte | b7e4023 | 2016-07-29 14:26:50 +0200 | [diff] [blame] | 70 | int rc; |
| 71 | struct iovec iov[2] = { |
| 72 | { |
| 73 | .iov_base = h, |
| 74 | .iov_len = sizeof(*h), |
| 75 | }, { |
| 76 | .iov_base = msg->data, |
| 77 | .iov_len = msg->len, |
| 78 | } |
| 79 | }; |
Harald Welte | 39cfbf4 | 2016-07-28 09:04:11 +0200 | [diff] [blame] | 80 | |
Harald Welte | b7e4023 | 2016-07-29 14:26:50 +0200 | [diff] [blame] | 81 | h->len = htonl(msg->len); |
Harald Welte | 6df0729 | 2019-11-06 16:21:55 +0100 | [diff] [blame] | 82 | gettimeofday(&tv, NULL); |
| 83 | h->ts.tv_sec = tv.tv_sec; |
| 84 | h->ts.tv_usec = tv.tv_usec; |
Harald Welte | 39cfbf4 | 2016-07-28 09:04:11 +0200 | [diff] [blame] | 85 | h->line_nr = ts->line->num; |
| 86 | h->ts_nr = ts->num; |
| 87 | h->capture_mode = mode; |
| 88 | h->flags = 0; |
| 89 | |
Harald Welte | 2ac7849 | 2016-10-19 10:47:41 +0200 | [diff] [blame] | 90 | rc = storage_reopen_if_needed(); |
| 91 | if (rc < 0) |
| 92 | return rc; |
Harald Welte | b7e4023 | 2016-07-29 14:26:50 +0200 | [diff] [blame] | 93 | |
| 94 | rc = writev(g_out_fd, iov, ARRAY_SIZE(iov)); |
| 95 | if (rc < 0) |
| 96 | return rc; |
| 97 | |
| 98 | g_written_bytes += rc; |
| 99 | |
Harald Welte | 39cfbf4 | 2016-07-28 09:04:11 +0200 | [diff] [blame] | 100 | return 0; |
| 101 | } |
Harald Welte | dbb0f5a | 2016-10-18 23:51:54 +0200 | [diff] [blame] | 102 | |
| 103 | /* reading */ |
| 104 | |
| 105 | |
| 106 | struct osmo_e1cap_file { |
| 107 | int fd; |
| 108 | }; |
| 109 | |
| 110 | #define OSMO_E1REC_ALLOC_SIZE 1024 |
| 111 | |
| 112 | struct osmo_e1cap_file *osmo_e1cap_open(void *ctx, const char *path) |
| 113 | { |
| 114 | struct osmo_e1cap_file *f = talloc_zero(ctx, struct osmo_e1cap_file); |
| 115 | if (!f) |
| 116 | return NULL; |
| 117 | |
| 118 | f->fd = open(path, O_RDONLY); |
| 119 | if (f->fd < 0) { |
| 120 | talloc_free(f); |
| 121 | return NULL; |
| 122 | } |
| 123 | return f; |
| 124 | } |
| 125 | |
| 126 | struct osmo_e1cap_pkthdr *osmo_e1cap_read_next(struct osmo_e1cap_file *f) |
| 127 | { |
| 128 | struct osmo_e1cap_pkthdr *pkt = talloc_zero_size(f, OSMO_E1REC_ALLOC_SIZE); |
| 129 | int rc; |
| 130 | |
| 131 | if (!pkt) |
| 132 | return NULL; |
| 133 | |
| 134 | /* read header */ |
| 135 | rc = read(f->fd, (uint8_t *)pkt, sizeof(*pkt)); |
| 136 | if (rc < sizeof(*pkt)) { |
| 137 | talloc_free(pkt); |
| 138 | return NULL; |
| 139 | } |
| 140 | pkt->len = ntohl(pkt->len); |
Harald Welte | dbb0f5a | 2016-10-18 23:51:54 +0200 | [diff] [blame] | 141 | /* read data */ |
| 142 | if (pkt->len > OSMO_E1REC_ALLOC_SIZE - sizeof(*pkt)) { |
| 143 | pkt = talloc_realloc_size(f, pkt, sizeof(*pkt) + pkt->len); |
| 144 | if (!pkt) |
| 145 | return NULL; |
| 146 | } |
| 147 | rc = read(f->fd, (uint8_t*)(pkt+1), pkt->len); |
| 148 | if (rc < pkt->len) { |
| 149 | talloc_free(pkt); |
| 150 | return NULL; |
| 151 | } |
| 152 | return pkt; |
| 153 | } |