km_flat_con.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * $Id$
  3. *
  4. * Flastore module connection structure
  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. #include <string.h>
  25. #include <errno.h>
  26. #include "../../mem/mem.h"
  27. #include "../../dprint.h"
  28. #include "../../ut.h"
  29. #include "km_flatstore_mod.h"
  30. #include "km_flat_con.h"
  31. #define FILE_SUFFIX ".log"
  32. #define FILE_SUFFIX_LEN (sizeof(FILE_SUFFIX) - 1)
  33. /* returns a pkg_malloc'ed file name */
  34. static char* get_name(struct flat_id* id)
  35. {
  36. char* buf;
  37. int buf_len;
  38. char* num, *ptr;
  39. int num_len;
  40. int total_len;
  41. buf_len=pathmax();
  42. if (!id) {
  43. LM_ERR("invalid parameter value\n");
  44. return 0;
  45. }
  46. total_len=id->dir.len+1 /* / */+id->table.len+1 /* _ */+
  47. FILE_SUFFIX_LEN+1 /* \0 */; /* without pid*/
  48. if (buf_len<total_len){
  49. LM_ERR("the path is too long (%d and PATHMAX is %d)\n",
  50. total_len, buf_len);
  51. return 0;
  52. }
  53. buf=pkg_malloc(buf_len);
  54. if (buf==0){
  55. LM_ERR("pkg memory allocation failure\n");
  56. return 0;
  57. }
  58. ptr = buf;
  59. memcpy(ptr, id->dir.s, id->dir.len);
  60. ptr += id->dir.len;
  61. *ptr++ = '/';
  62. memcpy(ptr, id->table.s, id->table.len);
  63. ptr += id->table.len;
  64. *ptr++ = '_';
  65. num = int2str(km_flat_pid, &num_len);
  66. if (buf_len<(total_len+num_len)){
  67. LM_ERR("the path is too long (%d and PATHMAX is"
  68. " %d)\n", total_len+num_len, buf_len);
  69. pkg_free(buf);
  70. return 0;
  71. }
  72. memcpy(ptr, num, num_len);
  73. ptr += num_len;
  74. memcpy(ptr, FILE_SUFFIX, FILE_SUFFIX_LEN);
  75. ptr += FILE_SUFFIX_LEN;
  76. *ptr = '\0';
  77. return buf;
  78. }
  79. struct flat_con* flat_new_connection(struct flat_id* id)
  80. {
  81. char* fn;
  82. struct flat_con* res;
  83. if (!id) {
  84. LM_ERR("invalid parameter value\n");
  85. return 0;
  86. }
  87. res = (struct flat_con*)pkg_malloc(sizeof(struct flat_con));
  88. if (!res) {
  89. LM_ERR("no pkg memory left\n");
  90. return 0;
  91. }
  92. memset(res, 0, sizeof(struct flat_con));
  93. res->ref = 1;
  94. res->id = id;
  95. fn = get_name(id);
  96. if (fn==0){
  97. LM_ERR("get_name() failed\n");
  98. pkg_free(res);
  99. return 0;
  100. }
  101. res->file = fopen(fn, "a");
  102. pkg_free(fn); /* we don't need fn anymore */
  103. if (!res->file) {
  104. LM_ERR(" %s\n", strerror(errno));
  105. pkg_free(res);
  106. return 0;
  107. }
  108. return res;
  109. }
  110. /*
  111. * Close the connection and release memory
  112. */
  113. void flat_free_connection(struct flat_con* con)
  114. {
  115. if (!con) return;
  116. if (con->id) free_flat_id(con->id);
  117. if (con->file) {
  118. fclose(con->file);
  119. }
  120. pkg_free(con);
  121. }
  122. /*
  123. * Reopen a connection
  124. */
  125. int flat_reopen_connection(struct flat_con* con)
  126. {
  127. char* fn;
  128. if (!con) {
  129. LM_ERR("invalid parameter value\n");
  130. return -1;
  131. }
  132. if (con->file) {
  133. fclose(con->file);
  134. con->file = 0;
  135. fn = get_name(con->id);
  136. if (fn == 0) {
  137. LM_ERR("failed to get_name\n");
  138. return -1;
  139. }
  140. con->file = fopen(fn, "a");
  141. pkg_free(fn);
  142. if (!con->file) {
  143. LM_ERR("invalid parameter value\n");
  144. return -1;
  145. }
  146. }
  147. return 0;
  148. }