async_mod.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /**
  2. * $Id$
  3. *
  4. * Copyright (C) 2011 Daniel-Constantin Mierla (asipto.com)
  5. *
  6. * This file is part of Kamailio, a free SIP server.
  7. *
  8. * This file is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. *
  14. * This file is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. */
  24. #include <stdio.h>
  25. #include <unistd.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include "../../sr_module.h"
  29. #include "../../dprint.h"
  30. #include "../../ut.h"
  31. #include "../../pvar.h"
  32. #include "../../timer_proc.h"
  33. #include "../../route_struct.h"
  34. #include "../../async_task.h"
  35. #include "../../modules/tm/tm_load.h"
  36. #include "async_sleep.h"
  37. MODULE_VERSION
  38. static int async_workers = 1;
  39. static int mod_init(void);
  40. static int child_init(int);
  41. static void mod_destroy(void);
  42. static int w_async_sleep(struct sip_msg* msg, char* sec, char* str2);
  43. static int fixup_async_sleep(void** param, int param_no);
  44. static int w_async_route(struct sip_msg* msg, char* rt, char* sec);
  45. static int fixup_async_route(void** param, int param_no);
  46. static int w_async_task_route(struct sip_msg* msg, char* rt, char* p2);
  47. static int fixup_async_task_route(void** param, int param_no);
  48. /* tm */
  49. struct tm_binds tmb;
  50. static cmd_export_t cmds[]={
  51. {"async_route", (cmd_function)w_async_route, 2, fixup_async_route,
  52. 0, REQUEST_ROUTE|FAILURE_ROUTE},
  53. {"async_sleep", (cmd_function)w_async_sleep, 1, fixup_async_sleep,
  54. 0, REQUEST_ROUTE|FAILURE_ROUTE},
  55. {"async_task_route", (cmd_function)w_async_task_route, 1, fixup_async_task_route,
  56. 0, REQUEST_ROUTE|FAILURE_ROUTE},
  57. {0, 0, 0, 0, 0, 0}
  58. };
  59. static param_export_t params[]={
  60. {"workers", INT_PARAM, &async_workers},
  61. {0, 0, 0}
  62. };
  63. struct module_exports exports = {
  64. "async",
  65. DEFAULT_DLFLAGS, /* dlopen flags */
  66. cmds,
  67. params,
  68. 0,
  69. 0, /* exported MI functions */
  70. 0, /* exported pseudo-variables */
  71. 0, /* extra processes */
  72. mod_init, /* module initialization function */
  73. 0, /* response function */
  74. mod_destroy, /* destroy function */
  75. child_init /* per child init function */
  76. };
  77. /**
  78. * init module function
  79. */
  80. static int mod_init(void)
  81. {
  82. if (load_tm_api( &tmb ) == -1)
  83. {
  84. LM_ERR("cannot load the TM-functions\n");
  85. return -1;
  86. }
  87. if(async_workers<=0)
  88. return 0;
  89. if(async_init_timer_list()<0) {
  90. LM_ERR("cannot initialize internal structure\n");
  91. return -1;
  92. }
  93. register_dummy_timers(async_workers);
  94. return 0;
  95. }
  96. /**
  97. * @brief Initialize async module children
  98. */
  99. static int child_init(int rank)
  100. {
  101. if (rank!=PROC_MAIN)
  102. return 0;
  103. if(async_workers<=0)
  104. return 0;
  105. if(fork_dummy_timer(PROC_TIMER, "ASYNC MOD TIMER", 1 /*socks flag*/,
  106. async_timer_exec, NULL, 1 /*sec*/)<0) {
  107. LM_ERR("failed to register timer routine as process\n");
  108. return -1; /* error */
  109. }
  110. return 0;
  111. }
  112. /**
  113. * destroy module function
  114. */
  115. static void mod_destroy(void)
  116. {
  117. async_destroy_timer_list();
  118. }
  119. /**
  120. *
  121. */
  122. static int w_async_sleep(struct sip_msg* msg, char* sec, char* str2)
  123. {
  124. int s;
  125. async_param_t *ap;
  126. if(msg==NULL)
  127. return -1;
  128. if(async_workers<=0)
  129. {
  130. LM_ERR("no async mod timer wokers\n");
  131. return -1;
  132. }
  133. ap = (async_param_t*)sec;
  134. if(fixup_get_ivalue(msg, ap->pinterval, &s)!=0)
  135. {
  136. LM_ERR("no async sleep time value\n");
  137. return -1;
  138. }
  139. if(ap->type==0)
  140. {
  141. if(ap->u.paction==NULL || ap->u.paction->next==NULL)
  142. {
  143. LM_ERR("cannot be executed as last action in a route block\n");
  144. return -1;
  145. }
  146. if(async_sleep(msg, s, ap->u.paction->next)<0)
  147. return -1;
  148. /* force exit in config */
  149. return 0;
  150. }
  151. return -1;
  152. }
  153. /**
  154. *
  155. */
  156. static int fixup_async_sleep(void** param, int param_no)
  157. {
  158. async_param_t *ap;
  159. if(param_no!=1)
  160. return 0;
  161. ap = (async_param_t*)pkg_malloc(sizeof(async_param_t));
  162. if(ap==NULL)
  163. {
  164. LM_ERR("no more pkg\n");
  165. return -1;
  166. }
  167. memset(ap, 0, sizeof(async_param_t));
  168. ap->u.paction = get_action_from_param(param, param_no);
  169. if(fixup_igp_null(param, param_no)<0)
  170. return -1;
  171. ap->pinterval = (gparam_t*)(*param);
  172. *param = (void*)ap;
  173. return 0;
  174. }
  175. /**
  176. *
  177. */
  178. static int w_async_route(struct sip_msg* msg, char* rt, char* sec)
  179. {
  180. cfg_action_t *act;
  181. int s;
  182. str rn;
  183. int ri;
  184. if(msg==NULL)
  185. return -1;
  186. if(async_workers<=0)
  187. {
  188. LM_ERR("no async mod timer wokers\n");
  189. return -1;
  190. }
  191. if(fixup_get_svalue(msg, (gparam_t*)rt, &rn)!=0)
  192. {
  193. LM_ERR("no async route block name\n");
  194. return -1;
  195. }
  196. if(fixup_get_ivalue(msg, (gparam_t*)sec, &s)!=0)
  197. {
  198. LM_ERR("no async interval value\n");
  199. return -1;
  200. }
  201. ri = route_get(&main_rt, rn.s);
  202. if(ri<0)
  203. {
  204. LM_ERR("unable to find route block [%.*s]\n", rn.len, rn.s);
  205. return -1;
  206. }
  207. act = main_rt.rlist[ri];
  208. if(act==NULL)
  209. {
  210. LM_ERR("empty action lists in route block [%.*s]\n", rn.len, rn.s);
  211. return -1;
  212. }
  213. if(async_sleep(msg, s, act)<0)
  214. return -1;
  215. /* force exit in config */
  216. return 0;
  217. }
  218. /**
  219. *
  220. */
  221. static int fixup_async_route(void** param, int param_no)
  222. {
  223. if(param_no==1)
  224. {
  225. if(fixup_spve_null(param, 1)<0)
  226. return -1;
  227. return 0;
  228. } else if(param_no==2) {
  229. if(fixup_igp_null(param, 1)<0)
  230. return -1;
  231. }
  232. return 0;
  233. }
  234. /**
  235. *
  236. */
  237. static int w_async_task_route(struct sip_msg* msg, char* rt, char* sec)
  238. {
  239. cfg_action_t *act;
  240. str rn;
  241. int ri;
  242. if(msg==NULL)
  243. return -1;
  244. if(fixup_get_svalue(msg, (gparam_t*)rt, &rn)!=0)
  245. {
  246. LM_ERR("no async route block name\n");
  247. return -1;
  248. }
  249. ri = route_get(&main_rt, rn.s);
  250. if(ri<0)
  251. {
  252. LM_ERR("unable to find route block [%.*s]\n", rn.len, rn.s);
  253. return -1;
  254. }
  255. act = main_rt.rlist[ri];
  256. if(act==NULL)
  257. {
  258. LM_ERR("empty action lists in route block [%.*s]\n", rn.len, rn.s);
  259. return -1;
  260. }
  261. if(async_send_task(msg, act)<0)
  262. return -1;
  263. /* force exit in config */
  264. return 0;
  265. }
  266. /**
  267. *
  268. */
  269. static int fixup_async_task_route(void** param, int param_no)
  270. {
  271. if(!async_task_initialized()) {
  272. LM_ERR("async task framework was not initialized"
  273. " - set async_workers parameter in core\n");
  274. return -1;
  275. }
  276. if(param_no==1)
  277. {
  278. if(fixup_spve_null(param, 1)<0)
  279. return -1;
  280. return 0;
  281. }
  282. return 0;
  283. }