blob: bcb50a22d81c642ef3bc90db8e84132e2c0e4175 [file] [log] [blame]
Harald Welteb7e40232016-07-29 14:26:50 +02001#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 Welte2359de62019-11-06 06:55:47 +01007#include <sys/uio.h>
Harald Welteb7e40232016-07-29 14:26:50 +02008
Harald Welte39cfbf42016-07-28 09:04:11 +02009#include <osmocom/core/msgb.h>
Harald Weltedbb0f5a2016-10-18 23:51:54 +020010#include <osmocom/core/talloc.h>
Harald Welte39cfbf42016-07-28 09:04:11 +020011#include <osmocom/abis/e1_input.h>
12
13#include "storage.h"
Harald Welteb7e40232016-07-29 14:26:50 +020014#include "recorder.h"
15
16static int g_out_fd = -1;;
17static uint64_t g_written_bytes;
18
19static 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 Weltedf7a3062016-10-19 10:47:17 +020031void 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 Welteb7e40232016-07-29 14:26:50 +020038static int storage_reopen_if_needed(void)
39{
40 if (g_written_bytes / (1024*1024) >= g_recorder.max_file_size_mb) {
Harald Weltedf7a3062016-10-19 10:47:17 +020041 storage_close();
42 /* we re-open below */
Harald Welteb7e40232016-07-29 14:26:50 +020043 }
44
45 if (g_out_fd < 0) {
46 int rc;
47 const char *fname = storage_gen_filename();
Harald Weltedf7a3062016-10-19 10:47:17 +020048 LOGP(DMAIN, LOGL_INFO, "Opening Log file %s\n", fname);
Harald Welteb7e40232016-07-29 14:26:50 +020049 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 Welte39cfbf42016-07-28 09:04:11 +020065
66int e1frame_store(struct e1inp_ts *ts, struct msgb *msg, enum osmo_e1cap_capture_mode mode)
67{
Harald Welteb7e40232016-07-29 14:26:50 +020068 struct osmo_e1cap_pkthdr _h, *h = &_h;
69 int rc;
70 struct iovec iov[2] = {
71 {
72 .iov_base = h,
73 .iov_len = sizeof(*h),
74 }, {
75 .iov_base = msg->data,
76 .iov_len = msg->len,
77 }
78 };
Harald Welte39cfbf42016-07-28 09:04:11 +020079
Harald Welteb7e40232016-07-29 14:26:50 +020080 h->len = htonl(msg->len);
81 gettimeofday(&h->ts, NULL);
Harald Welte39cfbf42016-07-28 09:04:11 +020082 h->line_nr = ts->line->num;
83 h->ts_nr = ts->num;
84 h->capture_mode = mode;
85 h->flags = 0;
86
Harald Welte2ac78492016-10-19 10:47:41 +020087 rc = storage_reopen_if_needed();
88 if (rc < 0)
89 return rc;
Harald Welteb7e40232016-07-29 14:26:50 +020090
91 rc = writev(g_out_fd, iov, ARRAY_SIZE(iov));
92 if (rc < 0)
93 return rc;
94
95 g_written_bytes += rc;
96
Harald Welte39cfbf42016-07-28 09:04:11 +020097 return 0;
98}
Harald Weltedbb0f5a2016-10-18 23:51:54 +020099
100/* reading */
101
102
103struct osmo_e1cap_file {
104 int fd;
105};
106
107#define OSMO_E1REC_ALLOC_SIZE 1024
108
109struct osmo_e1cap_file *osmo_e1cap_open(void *ctx, const char *path)
110{
111 struct osmo_e1cap_file *f = talloc_zero(ctx, struct osmo_e1cap_file);
112 if (!f)
113 return NULL;
114
115 f->fd = open(path, O_RDONLY);
116 if (f->fd < 0) {
117 talloc_free(f);
118 return NULL;
119 }
120 return f;
121}
122
123struct osmo_e1cap_pkthdr *osmo_e1cap_read_next(struct osmo_e1cap_file *f)
124{
125 struct osmo_e1cap_pkthdr *pkt = talloc_zero_size(f, OSMO_E1REC_ALLOC_SIZE);
126 int rc;
127
128 if (!pkt)
129 return NULL;
130
131 /* read header */
132 rc = read(f->fd, (uint8_t *)pkt, sizeof(*pkt));
133 if (rc < sizeof(*pkt)) {
134 talloc_free(pkt);
135 return NULL;
136 }
137 pkt->len = ntohl(pkt->len);
Harald Weltedbb0f5a2016-10-18 23:51:54 +0200138 /* read data */
139 if (pkt->len > OSMO_E1REC_ALLOC_SIZE - sizeof(*pkt)) {
140 pkt = talloc_realloc_size(f, pkt, sizeof(*pkt) + pkt->len);
141 if (!pkt)
142 return NULL;
143 }
144 rc = read(f->fd, (uint8_t*)(pkt+1), pkt->len);
145 if (rc < pkt->len) {
146 talloc_free(pkt);
147 return NULL;
148 }
149 return pkt;
150}