1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
|
#include "conf.h"
#include "config.h"
#include "array.h"
#include "nastdio.h"
#include "cdbpriv.h"
#include "log.h"
#include "memdb.h"
#include "thread.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
RCSID("$Id: cdb.c,v 1.64 2001/10/19 22:28:07 shmit Exp $");
time_t cdb_mtime;
static char *memcdb = NULL;
static rw_mutex_t *cdb_lk = NULL;
static uint32_t cdb_len = 0;
/*
* Exportable symbols - the CDB file is authoritative for them, so they
* come from here, but nastdio.c is their consumer.
*/
char db_key[1024];
char db_dbn[1024];
char db_tbl[1024];
fieldent *db_fields = NULL;
char db_sep;
short db_fieldcount = 0;
static int sec_c;
static float onemin_c, fivemin_c, fifteenmin_c;
static int
cdb_findbykey(char *mcdb, int len, const char *key, int keylen,
char **data, int *dlen)
{
if (cdb_find(mcdb, len, key, keylen, data, dlen) != 1)
return -1;
return 0;
}
int
cdb_new()
{
char *newcdb;
char *p, *s_p, *e_p;
fieldent *newfields;
char buffer[1024];
char cdbfields_s[1024], newkey[1024], newtbl[1024], newdbn[1024];
char newsep;
struct stat sb;
int i, fieldslen, newfieldcount;
int fd;
size_t len;
log_info("Initialising CDB interface.");
sec_c = 0;
onemin_c = fivemin_c = fifteenmin_c = 0;
if (!cdb_lk) {
cdb_lk = malloc(sizeof(rw_mutex_t));
if (!cdb_lk) {
log_err("Couldn't allocate CDB mutex: %s.",
strerror(errno));
return -1;
}
if (rw_mutex_new(cdb_lk)) {
log_err("Couldn't initialse CDB mutex: %s.",
strerror(errno));
return -1;
}
}
snprintf(buffer, sizeof(buffer), "%s/%s",
config.nast_dir, config.nast_cdb_file);
fd = open(buffer, O_RDONLY);
if (fd == -1) {
log_err("Couldn't open %s: %s.", buffer, strerror(errno));
return -1;
}
if (fstat(fd, &sb) == -1) {
log_err("Couldn't stat %s: %s.", buffer, strerror(errno));
return -1;
}
len = sb.st_size;
newcdb = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
if (newcdb == MAP_FAILED) {
log_err("Couldn't mmap %s: %s.", buffer, strerror(errno));
return -1;
}
(void)close(fd);
/* Get the key out of the CDB, so clients know how to search. */
if (cdb_findbykey(newcdb, len, "_KEY_", strlen("_KEY_"),
&p, &i) == -1) {
log_err("Couldn't find `_KEY_' in the CDB.");
munmap(newcdb, len);
return -1;
}
if (i > sizeof(newkey)) {
log_err("Couldn't copy key information from the CDB.");
munmap(newcdb, len);
return -1;
}
memcpy(newkey, p, i);
newkey[i] = '\0';
/* Get the dbname out of the CDB, so mysql knows which db to use. */
if (cdb_findbykey(newcdb, len, "_DB_", strlen("_DB_"), &p, &i) == -1) {
log_err("Warning: Couldn't find `_DB_' in the CDB.");
strncpy(newdbn, DBNAME, sizeof(newdbn));
} else {
if (i > sizeof(newdbn)) {
log_err("Couldn't copy database information "
"from the CDB.");
munmap(newcdb, len);
return -1;
}
memcpy(newdbn, p, i);
newdbn[i] = '\0';
}
/* Get the table out of the CDB, so mysql knows which to use. */
if (cdb_findbykey(newcdb, len, "_TABLE_", strlen("_TABLE_"),
&p, &i) == -1) {
log_err("Warning: Couldn't find `_TABLE_' in the CDB.");
strncpy(newtbl, DBTBL, sizeof(newtbl));
} else {
if (i > sizeof(newtbl)) {
log_err("Couldn't copy table information "
"from the CDB.");
munmap(newcdb, len);
return -1;
}
memcpy(newtbl, p, i);
newtbl[i] = '\0';
}
/* Get the delimiter out of the CDB file. */
if (cdb_findbykey(newcdb, len, "_DELIM_", strlen("_DELIM_"),
&p, &i) == -1) {
log_info("Couldn't find `_DELIM_' in the CDB. Using default.");
newsep = ':';
} else
newsep = *p;
/* Now get the column names. */
if (cdb_findbykey(newcdb, len, "_VALUES_", strlen("_VALUES_"),
&p, &i) == -1) {
log_err("Couldn't find `_VALUES_' in the CDB.");
munmap(newcdb, len);
return -1;
}
if (i >= sizeof(cdbfields_s)) {
log_err("Couldn't copy value information from the CDB.");
munmap(newcdb, len);
return -1;
}
memcpy(cdbfields_s, p, i);
cdbfields_s[i] = '\0';
fieldslen = strlen(cdbfields_s);
/* Fill out cdbfields with an array of field names. */
newfieldcount = 0;
for (i = 0; i <= fieldslen; i++) {
if (cdbfields_s[i] == newsep || i == fieldslen)
newfieldcount++;
}
newfields = malloc(sizeof(fieldent) * newfieldcount);
if (newfields == NULL) {
log_err("Couldn't allocate space for field array: %s.",
strerror(errno));
munmap(newcdb, len);
return -1;
}
s_p = cdbfields_s;
for (i = 0; i < newfieldcount; i++) {
for (e_p = s_p; *e_p != newsep && *e_p != '\0'; e_p++);
newfields[i].index = i;
newfields[i].name = malloc(e_p - s_p + 1*sizeof(char));
if (newfields[i].name == NULL) {
/* XXX: clean up. */
log_err("Couldn't allocate space for field: %s.",
strerror(errno));
munmap(newcdb, len);
return -1;
}
memcpy(newfields[i].name, s_p, e_p - s_p);
newfields[i].name[e_p - s_p] = '\0';
s_p = e_p + 1;
}
rw_mutex_write_lock(cdb_lk);
if (memcdb)
munmap(memcdb, cdb_len);
memcdb = newcdb;
cdb_len = (uint32_t)len;
cdb_mtime = sb.st_mtime;
memcpy(db_dbn, newdbn, sizeof(db_dbn));
memcpy(db_tbl, newtbl, sizeof(db_tbl));
memcpy(db_key, newkey, sizeof(db_key));
if (db_fields)
free(db_fields);
db_fields = newfields;
db_sep = newsep;
db_fieldcount = newfieldcount;
rw_mutex_unlock(cdb_lk);
log_info("CDB interface initialised.");
return 0;
}
int
cdb_get(const char *key, int keylen, array_t *aa)
{
char *s_p, *e_p;
char *data;
int dlen;
rw_mutex_read_lock(cdb_lk);
if (cdb_findbykey(memcdb, cdb_len, key, keylen, &data, &dlen) == -1) {
rw_mutex_unlock(cdb_lk);
return -1;
}
sec_c++;
s_p = data; e_p = data;
while (e_p <= data+dlen) {
if (*e_p == db_sep || e_p == data+dlen) {
if (array_add(aa, e_p-s_p, s_p, ARRTERM) == -1) {
rw_mutex_unlock(cdb_lk);
return -1;
}
s_p = e_p + 1;
}
e_p++;
}
rw_mutex_unlock(cdb_lk);
return 0;
}
void
cdb_periodic()
{
char buffer[1024];
struct stat sb;
snprintf(buffer, sizeof(buffer), "%s/%s",
config.nast_dir, config.nast_cdb_file);
if (stat(buffer, &sb) == -1) {
log_err("PERIODIC: Couldn't stat %s: %s.\n", buffer,
strerror(errno));
return;
}
if (sb.st_size == 0) {
log_err("PERIODIC: WARNING! CDB file is empty!");
return;
}
if (cdb_mtime < sb.st_mtime) {
log_info("PERIODIC: CDB file changed, reloading.\n");
(void)cdb_new();
/*
* Turned off, as now entries are time stamped and
* auto-expire when the cdb file is updated.
*/
/* (void)memdb_new(); */
return;
}
}
int
cdb_stats(array_t *statarr)
{
char buffer[512];
char tbuff[512];
struct tm res;
snprintf(buffer, sizeof(buffer), "CDB: %.2f, %.2f, %.2f",
onemin_c, fivemin_c, fifteenmin_c);
if (array_add(statarr, strlen(buffer), buffer, ARRTERM) == -1)
return -1;
/* Convert CDB mtime into a string. */
(void)localtime_r(&cdb_mtime, &res);
strftime(tbuff, sizeof(tbuff), "%Y%m%d %H:%M:%S", &res);
snprintf(buffer, sizeof(buffer), "CDB: last updated: %s",
tbuff);
return array_add(statarr, strlen(buffer), buffer, ARRTERM);
}
void
cdb_collate()
{
onemin_c = ((onemin_c * 59) + sec_c) / 60;
fivemin_c = ((fivemin_c * 299) + sec_c) / 300;
fifteenmin_c = ((fifteenmin_c * 899) + sec_c) / 900;
sec_c = 0;
}
|