flatstore_mod.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2004 FhG FOKUS
  5. * Copyright (C) 2008 iptelorg GmbH
  6. * Written by Jan Janak <[email protected]>
  7. *
  8. * This file is part of SER, a free SIP server.
  9. *
  10. * SER is free software; you can redistribute it and/or modify it under the
  11. * terms of the GNU General Public License as published by the Free Software
  12. * Foundation; either version 2 of the License, or (at your option) any later
  13. * version.
  14. *
  15. * SER is distributed in the hope that it will be useful, but WITHOUT ANY
  16. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  17. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  18. * details.
  19. *
  20. * You should have received a copy of the GNU General Public License along
  21. * with this program; if not, write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. /** \addtogroup flatstore
  25. * @{
  26. */
  27. /** \file
  28. * Flatstore module interface.
  29. */
  30. #include "flatstore_mod.h"
  31. #include "km_flatstore_mod.h"
  32. #include "flat_con.h"
  33. #include "flat_cmd.h"
  34. #include "flat_rpc.h"
  35. #include "flat_uri.h"
  36. #include "../../sr_module.h"
  37. #include "../../mem/shm_mem.h"
  38. #include "../../ut.h"
  39. #include <stdlib.h>
  40. #include <string.h>
  41. MODULE_VERSION
  42. static int child_init(int rank);
  43. static int mod_init(void);
  44. static void mod_destroy(void);
  45. /** PID to be used in file names.
  46. * The flatstore module generates one file per SER process to ensure that
  47. * every SER process has its own file and no locking/synchronization is
  48. * necessary. This variable contains a unique id of the SER process which
  49. * will be added to the file name.
  50. */
  51. str flat_pid = STR_NULL;
  52. /** Enable/disable flushing after eaach write. */
  53. int flat_flush = 1;
  54. /** Row delimiter.
  55. * The character in this variable will be used to delimit rows.
  56. */
  57. str flat_record_delimiter = STR_STATIC_INIT("\n");
  58. /** Field delimiter.
  59. * The character in this variable will be used to delimit fields.
  60. */
  61. str flat_delimiter = STR_STATIC_INIT("|");
  62. /** Escape character.
  63. * The character in this variable will be used to escape specia characters,
  64. * such as row and field delimiters, if they appear in the data being written
  65. * in the files.
  66. */
  67. str flat_escape = STR_STATIC_INIT("\\");
  68. /** Filename suffix.
  69. * This is the suffix of newly created files.
  70. */
  71. str flat_suffix = STR_STATIC_INIT(".log");
  72. /** Timestamp of last file rotation request.
  73. * This variable holds the timestamp of the last file rotation request
  74. * received through the management interface.
  75. */
  76. time_t* flat_rotate;
  77. /** Timestamp of last file rotation.
  78. * This variable contains the time of the last rotation of files.
  79. */
  80. time_t flat_local_timestamp;
  81. /* Flatstore database module interface */
  82. static cmd_export_t cmds[] = {
  83. {"db_uri", (cmd_function)flat_uri, 0, 0, 0},
  84. {"db_con", (cmd_function)flat_con, 0, 0, 0},
  85. {"db_cmd", (cmd_function)flat_cmd, 0, 0, 0},
  86. {"db_put", (cmd_function)flat_put, 0, 0, 0},
  87. {"db_bind_api", (cmd_function)db_flat_bind_api, 0, 0, 0},
  88. {0, 0, 0, 0, 0}
  89. };
  90. /* Exported parameters */
  91. static param_export_t params[] = {
  92. {"flush", PARAM_INT, &flat_flush},
  93. {"field_delimiter", PARAM_STR, &flat_delimiter},
  94. {"record_delimiter", PARAM_STR, &flat_record_delimiter},
  95. {"escape_char", PARAM_STR, &flat_escape},
  96. {"file_suffix", PARAM_STR, &flat_suffix},
  97. {0, 0, 0}
  98. };
  99. struct module_exports exports = {
  100. "db_flatstore",
  101. cmds,
  102. flat_rpc, /* RPC methods */
  103. params, /* module parameters */
  104. mod_init, /* module initialization function */
  105. 0, /* response function*/
  106. mod_destroy, /* destroy function */
  107. 0, /* oncancel function */
  108. child_init /* per-child init function */
  109. };
  110. int mod_register(char *path, int *dlflags, void *p1, void *p2)
  111. {
  112. if(db_api_init()<0)
  113. return -1;
  114. return 0;
  115. }
  116. static int mod_init(void)
  117. {
  118. if (flat_delimiter.len != 1) {
  119. ERR("flatstore: Parameter 'field_delimiter' "
  120. "must be exactly one character long.\n");
  121. return -1;
  122. }
  123. if (flat_record_delimiter.len != 1) {
  124. ERR("flatstore: Parameter 'record_delimiter' "
  125. "must be exactly one character long.\n");
  126. return -1;
  127. }
  128. if (flat_escape.len != 1) {
  129. ERR("flatstore: Parameter 'escape_char' "
  130. "must be exaactly one character long.\n");
  131. return -1;
  132. }
  133. flat_rotate = (time_t*)shm_malloc(sizeof(time_t));
  134. if (!flat_rotate) {
  135. ERR("flatstore: Not enough shared memory left\n");
  136. return -1;
  137. }
  138. *flat_rotate = time(0);
  139. flat_local_timestamp = *flat_rotate;
  140. return km_mod_init();
  141. }
  142. static void mod_destroy(void)
  143. {
  144. km_mod_destroy();
  145. if (flat_pid.s) free(flat_pid.s);
  146. if (flat_rotate) shm_free(flat_rotate);
  147. }
  148. /*
  149. * FIXME: We should check whether just calling km_child_init would really work
  150. * here. This function comes from kamailio and since the core of sip-router is
  151. * based on SER 2.0, the way how child_init is called and values of the rank
  152. * variable could be incompatible with km_child_init function. A solution here
  153. * would be to rewrite km_child_init with ser 2.0 init stuff in mind.
  154. */
  155. static int child_init(int rank)
  156. {
  157. char* tmp;
  158. unsigned int v;
  159. if(rank==PROC_INIT)
  160. return 0;
  161. km_child_init(rank);
  162. if (rank <= 0) {
  163. v = -rank;
  164. } else {
  165. v = rank - PROC_MIN;
  166. }
  167. if ((tmp = int2str(v, &flat_pid.len)) == NULL) {
  168. BUG("flatstore: Error while converting process id to number\n");
  169. return -1;
  170. }
  171. if ((flat_pid.s = strdup(tmp)) == NULL) {
  172. ERR("flatstore: No memory left\n");
  173. return -1;
  174. }
  175. return 0;
  176. }
  177. /** @} */