mysql_mod.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * $Id$
  3. *
  4. * MySQL module interface
  5. *
  6. * Copyright (C) 2001-2003 FhG Fokus
  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
  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. * For a license to use the ser software under conditions
  16. * other than those described here, or to purchase support for this
  17. * software, please contact iptel.org by e-mail at the following addresses:
  18. * [email protected]
  19. *
  20. * ser is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  28. */
  29. /*
  30. * History:
  31. * --------
  32. * 2003-03-11 updated to the new module exports interface (andrei)
  33. * 2003-03-16 flags export parameter added (janakj)
  34. */
  35. /** @addtogroup mysql
  36. * @{
  37. */
  38. #include "mysql_mod.h"
  39. #include "km_db_mysql.h"
  40. #include "my_uri.h"
  41. #include "my_con.h"
  42. #include "my_cmd.h"
  43. #include "my_fld.h"
  44. #include "my_res.h"
  45. #include "../../sr_module.h"
  46. #include "../../lib/srdb2/db.h"
  47. #include "../../dprint.h"
  48. int my_ping_interval = 5 * 60; /* Default is 5 minutes */
  49. unsigned int my_connect_to = 2; /* 2 s by default */
  50. unsigned int my_send_to = 0; /* enabled only for mysql >= 5.25 */
  51. unsigned int my_recv_to = 0; /* enabled only for mysql >= 5.25 */
  52. unsigned int my_retries = 1; /* Number of retries when command fails */
  53. unsigned long my_client_ver = 0;
  54. struct mysql_counters_h mysql_cnts_h;
  55. counter_def_t mysql_cnt_defs[] = {
  56. {&mysql_cnts_h.driver_err, "driver_errors", 0, 0, 0,
  57. "incremented each time a Mysql error happened because the server/connection has failed."},
  58. {0, 0, 0, 0, 0, 0 }
  59. };
  60. #define DEFAULT_MY_SEND_TO 2 /* in seconds */
  61. #define DEFAULT_MY_RECV_TO 4 /* in seconds */
  62. static int mysql_mod_init(void);
  63. MODULE_VERSION
  64. /*
  65. * MySQL database module interface
  66. */
  67. static cmd_export_t cmds[] = {
  68. {"db_ctx", (cmd_function)NULL, 0, 0, 0},
  69. {"db_con", (cmd_function)my_con, 0, 0, 0},
  70. {"db_uri", (cmd_function)my_uri, 0, 0, 0},
  71. {"db_cmd", (cmd_function)my_cmd, 0, 0, 0},
  72. {"db_put", (cmd_function)my_cmd_exec, 0, 0, 0},
  73. {"db_del", (cmd_function)my_cmd_exec, 0, 0, 0},
  74. {"db_get", (cmd_function)my_cmd_exec, 0, 0, 0},
  75. {"db_upd", (cmd_function)my_cmd_exec, 0, 0, 0},
  76. {"db_sql", (cmd_function)my_cmd_exec, 0, 0, 0},
  77. {"db_res", (cmd_function)my_res, 0, 0, 0},
  78. {"db_fld", (cmd_function)my_fld, 0, 0, 0},
  79. {"db_first", (cmd_function)my_cmd_first, 0, 0, 0},
  80. {"db_next", (cmd_function)my_cmd_next, 0, 0, 0},
  81. {"db_setopt", (cmd_function)my_setopt, 0, 0, 0},
  82. {"db_getopt", (cmd_function)my_getopt, 0, 0, 0},
  83. {"db_bind_api", (cmd_function)db_mysql_bind_api, 0, 0, 0},
  84. {0, 0, 0, 0, 0}
  85. };
  86. /*
  87. * Exported parameters
  88. */
  89. static param_export_t params[] = {
  90. {"ping_interval", PARAM_INT, &my_ping_interval},
  91. {"connect_timeout", PARAM_INT, &my_connect_to},
  92. {"send_timeout", PARAM_INT, &my_send_to},
  93. {"receive_timeout", PARAM_INT, &my_recv_to},
  94. {"retries", PARAM_INT, &my_retries},
  95. {"timeout_interval", INT_PARAM, &db_mysql_timeout_interval},
  96. {"auto_reconnect", INT_PARAM, &db_mysql_auto_reconnect},
  97. {0, 0, 0}
  98. };
  99. struct module_exports exports = {
  100. "db_mysql",
  101. cmds,
  102. 0, /* RPC method */
  103. params, /* module parameters */
  104. mysql_mod_init, /* module initialization function */
  105. 0, /* response function*/
  106. 0, /* destroy function */
  107. 0, /* oncancel function */
  108. 0 /* per-child init function */
  109. };
  110. int mod_register(char *path, int *dlflags, void *p1, void *p2)
  111. {
  112. if(db_mysql_alloc_buffer()<0)
  113. return -1;
  114. return 0;
  115. }
  116. static int mysql_mod_init(void)
  117. {
  118. #if MYSQL_VERSION_ID >= 40101
  119. my_client_ver = mysql_get_client_version();
  120. if ((my_client_ver >= 50025) ||
  121. ((my_client_ver >= 40122) &&
  122. (my_client_ver < 50000))) {
  123. if (my_send_to == 0) {
  124. my_send_to= DEFAULT_MY_SEND_TO;
  125. }
  126. if (my_recv_to == 0) {
  127. my_recv_to= DEFAULT_MY_RECV_TO;
  128. }
  129. } else if (my_recv_to || my_send_to) {
  130. LOG(L_WARN, "WARNING: mysql send or received timeout set, but "
  131. " not supported by the installed mysql client library"
  132. " (needed at least 4.1.22 or 5.0.25, but installed %ld)\n",
  133. my_client_ver);
  134. }
  135. #else
  136. if (my_recv_to || my_send_to) {
  137. LOG(L_WARN, "WARNING: mysql send or received timeout set, but "
  138. " not supported by the mysql client library used to compile"
  139. " the mysql module (needed at least 4.1.1 but "
  140. " compiled against %ld)\n", MYSQL_VERSION_ID);
  141. }
  142. #endif
  143. if (counter_register_array("mysql", mysql_cnt_defs) < 0)
  144. goto error;
  145. return kam_mysql_mod_init();
  146. error:
  147. return -1;
  148. }
  149. /** @} */