flatstore_mod.c

Go to the documentation of this file.
00001 /*
00002  * $Id$
00003  *
00004  * Copyright (C) 2004 FhG FOKUS
00005  * Copyright (C) 2008 iptelorg GmbH
00006  * Written by Jan Janak <jan@iptel.org>
00007  *
00008  * This file is part of SER, a free SIP server.
00009  *
00010  * SER is free software; you can redistribute it and/or modify it under the
00011  * terms of the GNU General Public License as published by the Free Software
00012  * Foundation; either version 2 of the License, or (at your option) any later
00013  * version.
00014  *
00015  * SER is distributed in the hope that it will be useful, but WITHOUT ANY
00016  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00017  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
00018  * details.
00019  *
00020  * You should have received a copy of the GNU General Public License along
00021  * with this program; if not, write to the Free Software Foundation, Inc., 
00022  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00023  */
00024 
00032 #include "flatstore_mod.h"
00033 #include "km_flatstore_mod.h"
00034 #include "flat_con.h"
00035 #include "flat_cmd.h"
00036 #include "flat_rpc.h"
00037 #include "flat_uri.h"
00038 
00039 #include "../../sr_module.h"
00040 #include "../../mem/shm_mem.h"
00041 #include "../../ut.h"
00042 
00043 #include <stdlib.h>
00044 #include <string.h>
00045 
00046 MODULE_VERSION
00047 
00048 static int child_init(int rank);
00049 
00050 static int mod_init(void);
00051 
00052 static void mod_destroy(void);
00053 
00054 
00061 str flat_pid = STR_NULL;
00062 
00063 
00065 int flat_flush = 1;
00066 
00067 
00071 str flat_record_delimiter = STR_STATIC_INIT("\n");
00072 
00073 
00077 str flat_delimiter = STR_STATIC_INIT("|");
00078 
00079 
00085 str flat_escape = STR_STATIC_INIT("\\");
00086 
00087 
00091 str flat_suffix = STR_STATIC_INIT(".log");
00092 
00093 
00098 time_t* flat_rotate;
00099 
00100 
00104 time_t flat_local_timestamp;
00105 
00106 
00107 /* Flatstore database module interface */
00108 static cmd_export_t cmds[] = {
00109         {"db_uri", (cmd_function)flat_uri, 0, 0, 0},
00110         {"db_con", (cmd_function)flat_con, 0, 0, 0},
00111         {"db_cmd", (cmd_function)flat_cmd, 0, 0, 0},
00112         {"db_put", (cmd_function)flat_put, 0, 0, 0},
00113         {"db_bind_api", (cmd_function)db_flat_bind_api,      0, 0, 0},
00114         {0, 0, 0, 0, 0}
00115 };
00116 
00117 
00118 /* Exported parameters */
00119 static param_export_t params[] = {
00120         {"flush",            PARAM_INT, &flat_flush},
00121         {"field_delimiter",  PARAM_STR, &flat_delimiter},
00122         {"record_delimiter", PARAM_STR, &flat_record_delimiter},
00123         {"escape_char",      PARAM_STR, &flat_escape},
00124         {"file_suffix",      PARAM_STR, &flat_suffix},
00125         {0, 0, 0}
00126 };
00127 
00128 
00129 struct module_exports exports = {
00130         "db_flatstore",
00131         cmds,
00132         flat_rpc,    /* RPC methods */
00133         params,      /*  module parameters */
00134         mod_init,    /* module initialization function */
00135         0,           /* response function*/
00136         mod_destroy, /* destroy function */
00137         0,           /* oncancel function */
00138         child_init   /* per-child init function */
00139 };
00140 
00141 
00142 int mod_register(char *path, int *dlflags, void *p1, void *p2)
00143 {
00144         if(db_api_init()<0)
00145                 return -1;
00146         return 0;
00147 }
00148 
00149 static int mod_init(void)
00150 {
00151         if (flat_delimiter.len != 1) {
00152                 ERR("flatstore: Parameter 'field_delimiter' "
00153                         "must be exactly one character long.\n");
00154                 return -1;
00155         }
00156 
00157         if (flat_record_delimiter.len != 1) {
00158                 ERR("flatstore: Parameter 'record_delimiter' "
00159                         "must be exactly one character long.\n");
00160                 return -1;
00161         }
00162 
00163         if (flat_escape.len != 1) {
00164                 ERR("flatstore: Parameter 'escape_char' "
00165                         "must be exaactly one character long.\n");
00166                 return -1;
00167         }
00168 
00169         flat_rotate = (time_t*)shm_malloc(sizeof(time_t));
00170         if (!flat_rotate) {
00171                 ERR("flatstore: Not enough shared memory left\n");
00172                 return -1;
00173         }
00174 
00175         *flat_rotate = time(0);
00176         flat_local_timestamp = *flat_rotate;
00177 
00178         return km_mod_init();
00179 }
00180 
00181 
00182 static void mod_destroy(void)
00183 {
00184         km_mod_destroy();
00185         if (flat_pid.s) free(flat_pid.s);
00186         if (flat_rotate) shm_free(flat_rotate);
00187 }
00188 
00189 
00190 /*
00191  * FIXME: We should check whether just calling km_child_init would really work
00192  * here. This function comes from kamailio and since the core of sip-router is
00193  * based on SER 2.0, the way how child_init is called and values of the rank
00194  * variable could be incompatible with km_child_init function. A solution here
00195  * would be to rewrite km_child_init with ser 2.0 init stuff in mind.
00196  */
00197 static int child_init(int rank)
00198 {
00199         char* tmp;
00200         unsigned int v;
00201 
00202         if(rank==PROC_INIT)
00203                 return 0;
00204 
00205         km_child_init(rank);
00206 
00207         if (rank <= 0) {
00208                 v = -rank;
00209         } else {
00210                 v = rank - PROC_MIN;
00211         }
00212 
00213     if ((tmp = int2str(v, &flat_pid.len)) == NULL) {
00214                 BUG("flatstore: Error while converting process id to number\n");
00215                 return -1;
00216         }
00217 
00218         if ((flat_pid.s = strdup(tmp)) == NULL) {
00219                 ERR("flatstore: No memory left\n");
00220                 return -1;
00221         }
00222 
00223         return 0;
00224 }
00225