1 /* saxdb.c - srvx database manager
2 * Copyright 2002-2004 srvx Development Team
4 * This file is part of srvx.
6 * srvx is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with srvx; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
27 DEFINE_LIST(int_list, int)
33 saxdb_reader_func_t *reader;
34 saxdb_writer_func_t *writer;
35 unsigned int write_interval;
36 unsigned long last_write;
37 unsigned int last_write_duration;
41 #define COMPLEX(CTX) ((CTX)->complex.used ? ((CTX)->complex.list[(CTX)->complex.used-1]) : 1)
43 static struct saxdb *last_db;
44 static struct dict *saxdbs; /* -> struct saxdb */
45 static struct dict *mondo_db;
46 static struct module *saxdb_module;
48 static SAXDB_WRITER(saxdb_mondo_writer);
49 static void saxdb_timed_write(void *data);
52 saxdb_read_db(struct saxdb *db) {
57 data = parse_database(db->filename);
60 if (db->writer == saxdb_mondo_writer) {
61 free_database(mondo_db);
70 saxdb_register(const char *name, saxdb_reader_func_t *reader, saxdb_writer_func_t *writer) {
74 const char *filename = NULL, *str;
75 char conf_path[MAXLEN];
77 db = calloc(1, sizeof(*db));
78 db->name = strdup(name);
81 /* Look up configuration */
82 sprintf(conf_path, "dbs/%s", name);
83 if ((conf = conf_get_data(conf_path, RECDB_OBJECT))) {
84 if ((str = database_get_data(conf, "mondo_section", RECDB_QSTRING))) {
85 db->mondo_section = strdup(str);
87 str = database_get_data(conf, "frequency", RECDB_QSTRING);
88 db->write_interval = str ? ParseInterval(str) : 1800;
89 filename = database_get_data(conf, "filename", RECDB_QSTRING);
91 db->write_interval = 1800;
93 /* Schedule database writes */
94 if (db->write_interval && !db->mondo_section) {
95 timeq_add(now + db->write_interval, saxdb_timed_write, db);
99 db->filename = strdup(filename);
101 db->filename = malloc(strlen(db->name)+4);
102 for (ii=0; db->name[ii]; ++ii) db->filename[ii] = tolower(db->name[ii]);
103 strcpy(db->filename+ii, ".db");
105 /* Read from disk (or mondo DB) */
106 if (db->mondo_section) {
107 if (mondo_db && (conf = database_get_data(mondo_db, db->mondo_section, RECDB_OBJECT))) {
113 /* Remember the database */
114 dict_insert(saxdbs, db->name, db);
121 saxdb_write_db(struct saxdb *db) {
122 struct saxdb_context ctx;
123 char tmp_fname[MAXLEN];
125 unsigned long start, finish;
127 assert(db->filename);
128 sprintf(tmp_fname, "%s.new", db->filename);
129 memset(&ctx, 0, sizeof(ctx));
130 ctx.output = fopen(tmp_fname, "w+");
131 int_list_init(&ctx.complex);
133 log_module(MAIN_LOG, LOG_ERROR, "Unable to write to %s: %s", tmp_fname, strerror(errno));
134 int_list_clean(&ctx.complex);
138 if ((res = setjmp(ctx.jbuf)) || (res2 = db->writer(&ctx))) {
140 log_module(MAIN_LOG, LOG_ERROR, "Error writing to %s: %s", tmp_fname, strerror(res));
142 log_module(MAIN_LOG, LOG_ERROR, "Internal error %d while writing to %s", res2, tmp_fname);
144 int_list_clean(&ctx.complex);
150 assert(ctx.complex.used == 0);
151 int_list_clean(&ctx.complex);
153 if (rename(tmp_fname, db->filename) < 0) {
154 log_module(MAIN_LOG, LOG_ERROR, "Unable to rename %s to %s: %s", tmp_fname, db->filename, strerror(errno));
156 db->last_write = now;
157 db->last_write_duration = finish - start;
158 log_module(MAIN_LOG, LOG_INFO, "Wrote %s database to disk.", db->name);
163 saxdb_timed_write(void *data) {
164 struct saxdb *db = data;
166 timeq_add(now + db->write_interval, saxdb_timed_write, db);
170 saxdb_write(const char *db_name) {
172 db = dict_find(saxdbs, db_name, NULL);
173 if (db) saxdb_write_db(db);
177 saxdb_write_all(void) {
181 for (it = dict_first(saxdbs); it; it = iter_next(it)) {
183 if (!db->mondo_section)
188 #define saxdb_put_char(DEST, CH) do { \
189 if (fputc(CH, (DEST)->output) == EOF) \
190 longjmp((DEST)->jbuf, errno); \
192 #define saxdb_put_string(DEST, CH) do { \
193 if (fputs(CH, (DEST)->output) == EOF) \
194 longjmp((DEST)->jbuf, errno); \
198 saxdb_put_nchars(struct saxdb_context *dest, const char *name, int len) {
200 if (fputc(*name++, dest->output) == EOF)
201 longjmp(dest->jbuf, errno);
205 saxdb_put_qstring(struct saxdb_context *dest, const char *str) {
209 saxdb_put_char(dest, '"');
210 while ((esc = strpbrk(str, "\\\a\b\t\n\v\f\r\""))) {
212 saxdb_put_nchars(dest, str, esc-str);
213 saxdb_put_char(dest, '\\');
215 case '\a': saxdb_put_char(dest, 'a'); break;
216 case '\b': saxdb_put_char(dest, 'b'); break;
217 case '\t': saxdb_put_char(dest, 't'); break;
218 case '\n': saxdb_put_char(dest, 'n'); break;
219 case '\v': saxdb_put_char(dest, 'v'); break;
220 case '\f': saxdb_put_char(dest, 'f'); break;
221 case '\r': saxdb_put_char(dest, 'r'); break;
222 case '\\': saxdb_put_char(dest, '\\'); break;
223 case '"': saxdb_put_char(dest, '"'); break;
227 saxdb_put_string(dest, str);
228 saxdb_put_char(dest, '"');
233 saxdb_pre_object(struct saxdb_context *dest) {
236 for (ii=0; ii<dest->indent; ++ii) saxdb_put_char(dest, '\t');
240 #define saxdb_pre_object(DEST)
244 saxdb_post_object(struct saxdb_context *dest) {
245 saxdb_put_char(dest, ';');
246 saxdb_put_char(dest, COMPLEX(dest) ? '\n' : ' ');
250 saxdb_start_record(struct saxdb_context *dest, const char *name, int complex) {
251 saxdb_pre_object(dest);
252 saxdb_put_qstring(dest, name);
253 saxdb_put_string(dest, " { ");
254 int_list_append(&dest->complex, complex);
257 saxdb_put_char(dest, '\n');
262 saxdb_end_record(struct saxdb_context *dest) {
263 assert(dest->complex.used > 0);
264 if (COMPLEX(dest)) dest->indent--;
265 saxdb_pre_object(dest);
266 dest->complex.used--;
267 saxdb_put_char(dest, '}');
268 saxdb_post_object(dest);
272 saxdb_write_string_list(struct saxdb_context *dest, const char *name, struct string_list *list) {
275 saxdb_pre_object(dest);
276 saxdb_put_qstring(dest, name);
277 saxdb_put_string(dest, " (");
279 for (ii=0; ii<list->used-1; ++ii) {
280 saxdb_put_qstring(dest, list->list[ii]);
281 saxdb_put_string(dest, ", ");
283 saxdb_put_qstring(dest, list->list[list->used-1]);
285 saxdb_put_string(dest, ")");
286 saxdb_post_object(dest);
290 saxdb_write_string(struct saxdb_context *dest, const char *name, const char *value) {
291 saxdb_pre_object(dest);
292 saxdb_put_qstring(dest, name);
293 saxdb_put_char(dest, ' ');
294 saxdb_put_qstring(dest, value);
295 saxdb_post_object(dest);
299 saxdb_write_int(struct saxdb_context *dest, const char *name, unsigned long value) {
301 /* we could optimize this to take advantage of the fact that buf will never need escapes */
302 snprintf(buf, sizeof(buf), "%lu", value);
303 saxdb_write_string(dest, name, buf);
307 saxdb_write_sint(struct saxdb_context *dest, const char *name, long value) {
309 /* we could optimize this to take advantage of the fact that buf will never need escapes */
310 snprintf(buf, sizeof(buf), "%ld", value);
311 saxdb_write_string(dest, name, buf);
315 saxdb_free(void *data) {
316 struct saxdb *db = data;
319 free(db->mondo_section);
324 saxdb_mondo_read(struct dict *db, struct saxdb *saxdb) {
328 if (saxdb->prev && (res = saxdb_mondo_read(db, saxdb->prev))) return res;
329 if (saxdb->mondo_section
330 && (subdb = database_get_data(db, saxdb->mondo_section, RECDB_OBJECT))
331 && (res = saxdb->reader(subdb))) {
332 log_module(MAIN_LOG, LOG_INFO, " mondo section read for %s failed: %d", saxdb->mondo_section, res);
338 static SAXDB_READER(saxdb_mondo_reader) {
339 return saxdb_mondo_read(db, last_db);
343 saxdb_mondo_write(struct saxdb_context *ctx, struct saxdb *saxdb) {
345 if (saxdb->prev && (res = saxdb_mondo_write(ctx, saxdb->prev))) return res;
346 if (saxdb->mondo_section) {
347 saxdb_start_record(ctx, saxdb->mondo_section, 1);
348 if ((res = saxdb->writer(ctx))) {
349 log_module(MAIN_LOG, LOG_INFO, " mondo section write for %s failed: %d", saxdb->mondo_section, res);
352 saxdb_end_record(ctx);
353 /* cheat a little here to put a newline between mondo sections */
354 saxdb_put_char(ctx, '\n');
359 static SAXDB_WRITER(saxdb_mondo_writer) {
360 return saxdb_mondo_write(ctx, last_db);
363 static MODCMD_FUNC(cmd_write) {
364 struct timeval start, stop;
365 unsigned int ii, written;
370 for (ii=1; ii<argc; ++ii) {
371 if (!(db = dict_find(saxdbs, argv[ii], NULL))) {
372 reply("MSG_DB_UNKNOWN", argv[ii]);
375 if (db->mondo_section) {
376 reply("MSG_DB_IS_MONDO", db->name);
379 gettimeofday(&start, NULL);
380 if (saxdb_write_db(db)) {
381 reply("MSG_DB_WRITE_ERROR", db->name);
383 gettimeofday(&stop, NULL);
384 stop.tv_sec -= start.tv_sec;
385 stop.tv_usec -= start.tv_usec;
386 if (stop.tv_usec < 0) {
388 stop.tv_usec += 1000000;
390 reply("MSG_DB_WROTE_DB", db->name, (unsigned long)stop.tv_sec, (unsigned long)stop.tv_usec);
397 static MODCMD_FUNC(cmd_writeall) {
398 struct timeval start, stop;
400 gettimeofday(&start, NULL);
402 gettimeofday(&stop, NULL);
403 stop.tv_sec -= start.tv_sec;
404 stop.tv_usec -= start.tv_usec;
405 if (stop.tv_usec < 0) {
407 stop.tv_usec += 1000000;
409 reply("MSG_DB_WROTE_ALL", (unsigned long)stop.tv_sec, (unsigned long)stop.tv_usec);
413 static MODCMD_FUNC(cmd_stats_databases) {
414 struct helpfile_table tbl;
418 tbl.length = dict_size(saxdbs) + 1;
420 tbl.flags = TABLE_NO_FREE;
421 tbl.contents = calloc(tbl.length, sizeof(tbl.contents[0]));
422 tbl.contents[0] = calloc(tbl.width, sizeof(tbl.contents[0][0]));
423 tbl.contents[0][0] = "Database";
424 tbl.contents[0][1] = "Filename/Section";
425 tbl.contents[0][2] = "Interval";
426 tbl.contents[0][3] = "Last Written";
427 tbl.contents[0][4] = "Last Duration";
428 for (ii=1, it=dict_first(saxdbs); it; it=iter_next(it), ++ii) {
429 struct saxdb *db = iter_data(it);
430 if (db->mondo_section) {
434 char *buf = malloc(INTERVALLEN*3);
435 tbl.contents[ii] = calloc(tbl.width, sizeof(tbl.contents[ii][0]));
436 tbl.contents[ii][0] = db->name;
437 tbl.contents[ii][1] = db->mondo_section ? db->mondo_section : db->filename;
438 if (db->write_interval) {
439 intervalString(buf, db->write_interval, user->handle_info);
441 strcpy(buf, "Never");
443 tbl.contents[ii][2] = buf;
444 if (db->last_write) {
445 intervalString(buf+INTERVALLEN, now - db->last_write, user->handle_info);
446 intervalString(buf+INTERVALLEN*2, db->last_write_duration, user->handle_info);
448 strcpy(buf+INTERVALLEN, "Never");
449 strcpy(buf+INTERVALLEN*2, "Never");
451 tbl.contents[ii][3] = buf+INTERVALLEN;
452 tbl.contents[ii][4] = buf+INTERVALLEN*2;
455 table_send(cmd->parent->bot, user->nick, 0, 0, tbl);
456 free(tbl.contents[0]);
457 for (ii=1; ii<tbl.length; ++ii) {
458 free((char*)tbl.contents[ii][2]);
459 free(tbl.contents[ii]);
466 saxdb_cleanup(void) {
470 static struct helpfile_expansion
471 saxdb_expand_help(const char *variable) {
472 struct helpfile_expansion exp;
473 if (!strcasecmp(variable, "dblist")) {
475 struct string_buffer sbuf;
478 exp.type = HF_STRING;
479 string_buffer_init(&sbuf);
480 for (it = dict_first(saxdbs); it; it = iter_next(it)) {
482 if (db->mondo_section) continue;
483 if (sbuf.used) string_buffer_append_string(&sbuf, ", ");
484 string_buffer_append_string(&sbuf, iter_key(it));
486 exp.value.str = sbuf.list;
488 exp.type = HF_STRING;
489 exp.value.str = NULL;
496 reg_exit_func(saxdb_cleanup);
498 dict_set_free_data(saxdbs, saxdb_free);
499 saxdb_register("mondo", saxdb_mondo_reader, saxdb_mondo_writer);
500 saxdb_module = module_register("saxdb", MAIN_LOG, "saxdb.help", saxdb_expand_help);
501 modcmd_register(saxdb_module, "write", cmd_write, 2, MODCMD_REQUIRE_AUTHED, "level", "800", NULL);
502 modcmd_register(saxdb_module, "writeall", cmd_writeall, 0, MODCMD_REQUIRE_AUTHED, "level", "800", NULL);
503 modcmd_register(saxdb_module, "stats databases", cmd_stats_databases, 0, 0, NULL);
507 saxdb_finalize(void) {
508 free_database(mondo_db);
512 write_database_helper(struct saxdb_context *ctx, struct dict *db) {
514 struct record_data *rd;
516 for (it = dict_first(db); it; it = iter_next(it)) {
519 case RECDB_INVALID: break;
520 case RECDB_QSTRING: saxdb_write_string(ctx, iter_key(it), rd->d.qstring); break;
521 case RECDB_STRING_LIST: saxdb_write_string_list(ctx, iter_key(it), rd->d.slist); break;
523 saxdb_start_record(ctx, iter_key(it), 1);
524 write_database_helper(ctx, rd->d.object);
525 saxdb_end_record(ctx);
532 write_database(FILE *out, struct dict *db) {
533 struct saxdb_context ctx;
538 int_list_init(&ctx.complex);
539 if (!(res = setjmp(ctx.jbuf))) {
540 write_database_helper(&ctx, db);
542 log_module(MAIN_LOG, LOG_ERROR, "Exception %d caught while writing to stream", res);
543 int_list_clean(&ctx.complex);
546 assert(ctx.complex.used == 0);
547 int_list_clean(&ctx.complex);
551 struct saxdb_context *
552 saxdb_open_context(FILE *file) {
553 struct saxdb_context *ctx;
556 ctx = calloc(1, sizeof(*ctx));
558 int_list_init(&ctx->complex);
564 saxdb_close_context(struct saxdb_context *ctx) {
565 assert(ctx->complex.used == 0);
566 int_list_clean(&ctx->complex);