km_flatstore_mod.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * $Id$
  3. *
  4. * Flatstore module interface
  5. *
  6. * Copyright (C) 2004 FhG Fokus
  7. *
  8. * This file is part of Kamailio, a free SIP server.
  9. *
  10. * Kamailio is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version
  14. *
  15. * Kamailio is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. *
  24. * History:
  25. * --------
  26. * 2003-03-11 updated to the new module exports interface (andrei)
  27. * 2003-03-16 flags export parameter added (janakj)
  28. */
  29. #include "../../sr_module.h"
  30. #include "../../mem/shm_mem.h"
  31. #include "../../lib/srdb1/db.h"
  32. #include "../../rpc_lookup.h"
  33. #include "km_flatstore.h"
  34. #include "km_flatstore_mod.h"
  35. #include "flatstore_mod.h"
  36. /*MODULE_VERSION*/
  37. int db_flat_bind_api(db_func_t *dbb);
  38. /*
  39. * Process number used in filenames
  40. */
  41. int km_flat_pid;
  42. /*
  43. * Delimiter delimiting columns
  44. */
  45. char* km_flat_delimiter = "|";
  46. /*
  47. * Timestamp of the last log rotation request from
  48. * the FIFO interface
  49. */
  50. time_t* km_flat_rotate;
  51. time_t km_local_timestamp;
  52. /*
  53. * Flatstore database module interface
  54. */
  55. static kam_cmd_export_t cmds[] = {
  56. {"db_bind_api", (cmd_function)db_flat_bind_api, 0, 0, 0, 0},
  57. {0, 0, 0, 0, 0, 0}
  58. };
  59. /*
  60. * Exported parameters
  61. */
  62. static param_export_t params[] = {
  63. {0, 0, 0}
  64. };
  65. static rpc_export_t k_rpc_methods[];
  66. struct kam_module_exports km_exports = {
  67. "db_flatstore",
  68. DEFAULT_DLFLAGS, /* dlopen flags */
  69. cmds,
  70. params, /* module parameters */
  71. 0, /* exported statistics */
  72. NULL, /* exported MI functions */
  73. 0, /* exported pseudo-variables */
  74. 0, /* extra processes */
  75. km_mod_init, /* module initialization function */
  76. 0, /* response function*/
  77. km_mod_destroy, /* destroy function */
  78. km_child_init /* per-child init function */
  79. };
  80. int km_mod_init(void)
  81. {
  82. if (rpc_register_array(k_rpc_methods)!=0) {
  83. LM_ERR("failed to register RPC commands\n");
  84. return -1;
  85. }
  86. if (strlen(km_flat_delimiter) != 1) {
  87. LM_ERR("delimiter has to be exactly one character\n");
  88. return -1;
  89. }
  90. km_flat_rotate = (time_t*)shm_malloc(sizeof(time_t));
  91. if (!km_flat_rotate) {
  92. LM_ERR("no shared memory left\n");
  93. return -1;
  94. }
  95. *km_flat_rotate = time(0);
  96. km_local_timestamp = *km_flat_rotate;
  97. return 0;
  98. }
  99. void km_mod_destroy(void)
  100. {
  101. if (km_flat_rotate) shm_free(km_flat_rotate);
  102. }
  103. int km_child_init(int rank)
  104. {
  105. if (rank <= 0) {
  106. km_flat_pid = - rank;
  107. } else {
  108. km_flat_pid = rank - PROC_MIN;
  109. }
  110. return 0;
  111. }
  112. int db_flat_bind_api(db_func_t *dbb)
  113. {
  114. if(dbb==NULL)
  115. return -1;
  116. memset(dbb, 0, sizeof(db_func_t));
  117. dbb->use_table = flat_use_table;
  118. dbb->init = flat_db_init;
  119. dbb->close = flat_db_close;
  120. dbb->insert = flat_db_insert;
  121. return 0;
  122. }
  123. /* rpc function documentation */
  124. static const char *rpc_k_rotate_doc[2] = {
  125. "Close and reopen flatrotate files during log rotation.", 0
  126. };
  127. /* rpc function implementations */
  128. static void rpc_k_rotate(rpc_t *rpc, void *c)
  129. {
  130. *km_flat_rotate = time(0);
  131. return;
  132. }
  133. static rpc_export_t k_rpc_methods[] = {
  134. {"flatstore.k_rotate", rpc_k_rotate, rpc_k_rotate_doc, 0},
  135. {0, 0, 0, 0},
  136. };