Module: sip-router
Branch: janakj/flatstore
Commit: faac0ca2434db223299ad8e1271cbaa31bd00004
URL:
http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=faac0ca…
Author: Jan Janak <jan(a)iptel.org>
Committer: Jan Janak <jan(a)iptel.org>
Date: Sat Oct 9 14:30:27 2004 +0000
FIFO interface for log file rotation.
---
modules/db_flatstore/flat_con.c | 76 ++++++++++++++++++++++++++-------
modules/db_flatstore/flat_con.h | 6 +++
modules/db_flatstore/flat_pool.c | 20 +++++++++
modules/db_flatstore/flat_pool.h | 12 +++++
modules/db_flatstore/flatstore.c | 5 ++
modules/db_flatstore/flatstore_mod.c | 47 ++++++++++++++++++---
modules/db_flatstore/flatstore_mod.h | 15 +++++++
7 files changed, 159 insertions(+), 22 deletions(-)
diff --git a/modules/db_flatstore/flat_con.c b/modules/db_flatstore/flat_con.c
index f8db134..cd5b5c9 100644
--- a/modules/db_flatstore/flat_con.c
+++ b/modules/db_flatstore/flat_con.c
@@ -39,29 +39,18 @@
#define FILE_SUFFIX ".log"
#define FILE_SUFFIX_LEN (sizeof(FILE_SUFFIX) - 1)
-struct flat_con* flat_new_connection(struct flat_id* id)
+
+static char* get_name(struct flat_id* id)
{
- char buf[PATH_MAX];
+ static char buf[PATH_MAX];
char* num, *ptr;
int num_len;
- struct flat_con* res;
-
if (!id) {
- LOG(L_ERR, "flat_new_connection: Invalid parameter value\n");
- return 0;
- }
-
- res = (struct flat_con*)pkg_malloc(sizeof(struct flat_con));
- if (!res) {
- LOG(L_ERR, "flat_new_connection: No memory left\n");
+ LOG(L_ERR, "get_name: Invalid parameter value\n");
return 0;
}
- memset(res, 0, sizeof(struct flat_con));
- res->ref = 1;
-
- res->id = id;
ptr = buf;
memcpy(ptr, id->dir.s, id->dir.len);
@@ -81,8 +70,35 @@ struct flat_con* flat_new_connection(struct flat_id* id)
ptr += FILE_SUFFIX_LEN;
*ptr = '\0';
+ return buf;
+}
+
+
+struct flat_con* flat_new_connection(struct flat_id* id)
+{
+ char* fn;
+
+ struct flat_con* res;
+
+ if (!id) {
+ LOG(L_ERR, "flat_new_connection: Invalid parameter value\n");
+ return 0;
+ }
- res->file = fopen(buf, "w");
+ res = (struct flat_con*)pkg_malloc(sizeof(struct flat_con));
+ if (!res) {
+ LOG(L_ERR, "flat_new_connection: No memory left\n");
+ return 0;
+ }
+
+ memset(res, 0, sizeof(struct flat_con));
+ res->ref = 1;
+
+ res->id = id;
+
+ fn = get_name(id);
+
+ res->file = fopen(fn, "w");
if (!res->file) {
LOG(L_ERR, "flat_new_connection: %s\n", strerror(errno));
pkg_free(res);
@@ -105,3 +121,31 @@ void flat_free_connection(struct flat_con* con)
}
pkg_free(con);
}
+
+
+/*
+ * Reopen a connection
+ */
+int flat_reopen_connection(struct flat_con* con)
+{
+ char* fn;
+
+ if (!con) {
+ LOG(L_ERR, "flat_reopen_connection: Invalid parameter value\n");
+ return -1;
+ }
+
+ if (con->file) {
+ fclose(con->file);
+
+ fn = get_name(con->id);
+
+ con->file = fopen(fn, "w");
+ if (!con->file) {
+ LOG(L_ERR, "flat_reopen_connection: Invalid parameter value\n");
+ return -1;
+ }
+ }
+
+ return 0;
+}
diff --git a/modules/db_flatstore/flat_con.h b/modules/db_flatstore/flat_con.h
index c075a07..341b2bd 100644
--- a/modules/db_flatstore/flat_con.h
+++ b/modules/db_flatstore/flat_con.h
@@ -61,4 +61,10 @@ struct flat_con* flat_new_connection(struct flat_id* id);
void flat_free_connection(struct flat_con* con);
+/*
+ * Reopen a connection
+ */
+int flat_reopen_connection(struct flat_con* con);
+
+
#endif /* _FLAT_CON_H */
diff --git a/modules/db_flatstore/flat_pool.c b/modules/db_flatstore/flat_pool.c
index da213cf..fadc472 100644
--- a/modules/db_flatstore/flat_pool.c
+++ b/modules/db_flatstore/flat_pool.c
@@ -44,6 +44,7 @@ static struct flat_con* pool = 0;
static int pool_pid;
+
/*
* Get a connection from the pool, reuse existing
* if possible, otherwise create a new one
@@ -134,3 +135,22 @@ void flat_release_connection(struct flat_con* con)
flat_free_connection(con);
}
+
+
+/*
+ * Close and reopen all opened connections
+ */
+int flat_rotate_logs(void)
+{
+ struct flat_con* ptr;
+
+ ptr = pool;
+ while(ptr) {
+ if (flat_reopen_connection(ptr)) {
+ return -1;
+ }
+ ptr = ptr->next;
+ }
+
+ return 0;
+}
diff --git a/modules/db_flatstore/flat_pool.h b/modules/db_flatstore/flat_pool.h
index 955ae8b..3d6fc27 100644
--- a/modules/db_flatstore/flat_pool.h
+++ b/modules/db_flatstore/flat_pool.h
@@ -47,4 +47,16 @@ struct flat_con* flat_get_connection(char* dir, char* table);
void flat_release_connection(struct flat_con* con);
+/*
+ * Close and reopen all opened connections
+ */
+int flat_rotate_logs(void);
+
+
+/*
+ * Reopen a connection
+ */
+int flat_reopen_connection(struct flat_con* con);
+
+
#endif /* _FLAT_POOL_H */
diff --git a/modules/db_flatstore/flatstore.c b/modules/db_flatstore/flatstore.c
index ffe8027..8b7bfc2 100644
--- a/modules/db_flatstore/flatstore.c
+++ b/modules/db_flatstore/flatstore.c
@@ -162,6 +162,11 @@ int flat_db_insert(db_con_t* h, db_key_t* k, db_val_t* v, int n)
return -1;
}
+ if (local_timestamp < *flat_rotate) {
+ flat_rotate_logs();
+ local_timestamp = *flat_rotate;
+ }
+
for(i = 0; i < n; i++) {
switch(VAL_TYPE(v + i)) {
case DB_INT:
diff --git a/modules/db_flatstore/flatstore_mod.c b/modules/db_flatstore/flatstore_mod.c
index a7106a3..d281992 100644
--- a/modules/db_flatstore/flatstore_mod.c
+++ b/modules/db_flatstore/flatstore_mod.c
@@ -34,7 +34,9 @@
*/
#include "../../sr_module.h"
+#include "../../mem/shm_mem.h"
#include "flatstore.h"
+#include "flat_fifo.h"
#include "flatstore_mod.h"
MODULE_VERSION
@@ -43,6 +45,9 @@ static int child_init(int rank);
static int mod_init(void);
+static void mod_destroy(void);
+
+
/*
* Process number used in filenames
*/
@@ -61,6 +66,14 @@ char* flat_delimiter = "|";
/*
+ * Timestamp of the last log rotation request from
+ * the FIFO interface
+ */
+time_t* flat_rotate;
+
+time_t local_timestamp;
+
+/*
* Flatstore database module interface
*/
static cmd_export_t cmds[] = {
@@ -84,12 +97,12 @@ static param_export_t params[] = {
struct module_exports exports = {
"flatstore",
cmds,
- params, /* module parameters */
- mod_init, /* module initialization function */
- 0, /* response function*/
- 0, /* destroy function */
- 0, /* oncancel function */
- child_init /* per-child init function */
+ params, /* module parameters */
+ mod_init, /* module initialization function */
+ 0, /* response function*/
+ mod_destroy, /* destroy function */
+ 0, /* oncancel function */
+ child_init /* per-child init function */
};
@@ -99,10 +112,32 @@ static int mod_init(void)
LOG(L_ERR, "flatstore:mod_init: Delimiter has to be exactly one
character\n");
return -1;
}
+
+ /* Initialize fifo interface */
+ if (init_flat_fifo() < 0) {
+ LOG(L_ERR, "usrloc/fifo initialization failed\n");
+ return -1;
+ }
+
+ flat_rotate = (time_t*)shm_malloc(sizeof(time_t));
+ if (!flat_rotate) {
+ LOG(L_ERR, "flatstore: No shared memory left\n");
+ return -1;
+ }
+
+ *flat_rotate = time(0);
+ local_timestamp = *flat_rotate;
+
return 0;
}
+static void mod_destroy(void)
+{
+ if (flat_rotate) shm_free(flat_rotate);
+}
+
+
static int child_init(int rank)
{
if (rank <= 0) {
diff --git a/modules/db_flatstore/flatstore_mod.h b/modules/db_flatstore/flatstore_mod.h
index 24ea2f5..83bc593 100644
--- a/modules/db_flatstore/flatstore_mod.h
+++ b/modules/db_flatstore/flatstore_mod.h
@@ -36,6 +36,7 @@
#ifndef FLATSTORE_MOD_H
#define FLATSTORE_MOD_H
+#include <time.h>
/*
* Process number used in filenames
@@ -55,4 +56,18 @@ extern int flat_flush;
extern char* flat_delimiter;
+/*
+ * The timestamp of log rotation request from
+ * the FIFO interface
+ */
+extern time_t* flat_rotate;
+
+
+/*
+ * Local timestamp marking the time of the
+ * last log rotation in the process
+ */
+extern time_t local_timestamp;
+
+
#endif /* FLATSTORE_MOD_H */