rtimer_mod.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /**
  2. * $Id$
  3. *
  4. * Copyright (C) 2009 Daniel-Constantin Mierla (asipto.com)
  5. *
  6. * This file is part of Kamailio, a free SIP server.
  7. *
  8. * Kamailio 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. * Kamailio is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <stdlib.h>
  25. #include <sys/types.h>
  26. #include <sys/ipc.h>
  27. #include <unistd.h>
  28. #include <fcntl.h>
  29. #include "../../sr_module.h"
  30. #include "../../timer.h"
  31. #include "../../dprint.h"
  32. #include "../../route.h"
  33. #include "../../receive.h"
  34. #include "../../action.h"
  35. #include "../../socket_info.h"
  36. #include "../../dset.h"
  37. #include "../../pt.h"
  38. #include "../../usr_avp.h"
  39. #include "../../xavp.h"
  40. #include "../../timer_proc.h"
  41. #include "../../script_cb.h"
  42. #include "../../parser/parse_param.h"
  43. #include "../../lib/kcore/faked_msg.h"
  44. MODULE_VERSION
  45. typedef struct _stm_route {
  46. str timer;
  47. unsigned int route;
  48. struct _stm_route *next;
  49. } stm_route_t;
  50. typedef struct _stm_timer {
  51. str name;
  52. unsigned int mode;
  53. unsigned int flags;
  54. unsigned int interval;
  55. stm_route_t *rt;
  56. struct _stm_timer *next;
  57. } stm_timer_t;
  58. #define RTIMER_INTERVAL_USEC (1<<0)
  59. stm_timer_t *_stm_list = NULL;
  60. /** module functions */
  61. static int mod_init(void);
  62. static int child_init(int);
  63. int stm_t_param(modparam_t type, void* val);
  64. int stm_e_param(modparam_t type, void* val);
  65. void stm_timer_exec(unsigned int ticks, void *param);
  66. static param_export_t params[]={
  67. {"timer", PARAM_STRING|USE_FUNC_PARAM, (void*)stm_t_param},
  68. {"exec", PARAM_STRING|USE_FUNC_PARAM, (void*)stm_e_param},
  69. {0,0,0}
  70. };
  71. /** module exports */
  72. struct module_exports exports= {
  73. "rtimer",
  74. DEFAULT_DLFLAGS, /* dlopen flags */
  75. 0,
  76. params,
  77. 0, /* exported statistics */
  78. 0, /* exported MI functions */
  79. 0, /* exported pseudo-variables */
  80. 0, /* extra processes */
  81. mod_init, /* module initialization function */
  82. 0,
  83. 0,
  84. child_init /* per-child init function */
  85. };
  86. /**
  87. * init module function
  88. */
  89. static int mod_init(void)
  90. {
  91. stm_timer_t *it;
  92. if(_stm_list==NULL)
  93. return 0;
  94. /* init faked sip msg */
  95. if(faked_msg_init()<0)
  96. {
  97. LM_ERR("failed to init timer local sip msg\n");
  98. return -1;
  99. }
  100. /* register timers */
  101. it = _stm_list;
  102. while(it)
  103. {
  104. if(it->mode==0)
  105. {
  106. if(register_timer(stm_timer_exec, (void*)it, it->interval)<0)
  107. {
  108. LM_ERR("failed to register timer function\n");
  109. return -1;
  110. }
  111. } else {
  112. register_basic_timers(1);
  113. }
  114. it = it->next;
  115. }
  116. return 0;
  117. }
  118. static int child_init(int rank)
  119. {
  120. stm_timer_t *it;
  121. if(_stm_list==NULL)
  122. return 0;
  123. if (rank!=PROC_MAIN)
  124. return 0;
  125. it = _stm_list;
  126. while(it)
  127. {
  128. if(it->mode!=0)
  129. {
  130. if(it->flags & RTIMER_INTERVAL_USEC)
  131. {
  132. if(fork_basic_utimer(PROC_TIMER, "RTIMER USEC EXEC", 1 /*socks flag*/,
  133. stm_timer_exec, (void*)it, it->interval
  134. /*usec*/)<0) {
  135. LM_ERR("failed to start utimer routine as process\n");
  136. return -1; /* error */
  137. }
  138. } else {
  139. if(fork_basic_timer(PROC_TIMER, "RTIMER SEC EXEC", 1 /*socks flag*/,
  140. stm_timer_exec, (void*)it, it->interval
  141. /*sec*/)<0) {
  142. LM_ERR("failed to start timer routine as process\n");
  143. return -1; /* error */
  144. }
  145. }
  146. }
  147. it = it->next;
  148. }
  149. return 0;
  150. }
  151. void stm_timer_exec(unsigned int ticks, void *param)
  152. {
  153. stm_timer_t *it;
  154. stm_route_t *rt;
  155. sip_msg_t *fmsg;
  156. if(param==NULL)
  157. return;
  158. it = (stm_timer_t*)param;
  159. if(it->rt==NULL)
  160. return;
  161. for(rt=it->rt; rt; rt=rt->next)
  162. {
  163. fmsg = faked_msg_next();
  164. if (exec_pre_script_cb(fmsg, REQUEST_CB_TYPE)==0 )
  165. continue; /* drop the request */
  166. set_route_type(REQUEST_ROUTE);
  167. run_top_route(main_rt.rlist[rt->route], fmsg, 0);
  168. exec_post_script_cb(fmsg, REQUEST_CB_TYPE);
  169. reset_avps();
  170. #ifdef WITH_XAVP
  171. xavp_reset_list();
  172. #endif
  173. }
  174. }
  175. int stm_t_param(modparam_t type, void *val)
  176. {
  177. param_t* params_list = NULL;
  178. param_hooks_t phooks;
  179. param_t *pit=NULL;
  180. stm_timer_t tmp;
  181. stm_timer_t *nt;
  182. str s;
  183. if(val==NULL)
  184. return -1;
  185. s.s = (char*)val;
  186. s.len = strlen(s.s);
  187. if(s.s[s.len-1]==';')
  188. s.len--;
  189. if (parse_params(&s, CLASS_ANY, &phooks, &params_list)<0)
  190. return -1;
  191. memset(&tmp, 0, sizeof(stm_timer_t));
  192. for (pit = params_list; pit; pit=pit->next)
  193. {
  194. if (pit->name.len==4
  195. && strncasecmp(pit->name.s, "name", 4)==0) {
  196. tmp.name = pit->body;
  197. } else if(pit->name.len==4
  198. && strncasecmp(pit->name.s, "mode", 4)==0) {
  199. if(tmp.mode==0)
  200. str2int(&pit->body, &tmp.mode);
  201. } else if(pit->name.len==8
  202. && strncasecmp(pit->name.s, "interval", 8)==0) {
  203. if(pit->body.s[pit->body.len-1]=='u'
  204. || pit->body.s[pit->body.len-1]=='U') {
  205. pit->body.len--;
  206. tmp.flags |= RTIMER_INTERVAL_USEC;
  207. tmp.mode = 1;
  208. }
  209. str2int(&pit->body, &tmp.interval);
  210. }
  211. }
  212. if(tmp.name.s==NULL)
  213. {
  214. LM_ERR("invalid timer name\n");
  215. free_params(params_list);
  216. return -1;
  217. }
  218. /* check for same timer */
  219. nt = _stm_list;
  220. while(nt) {
  221. if(nt->name.len==tmp.name.len
  222. && strncasecmp(nt->name.s, tmp.name.s, tmp.name.len)==0)
  223. break;
  224. nt = nt->next;
  225. }
  226. if(nt!=NULL)
  227. {
  228. LM_ERR("duplicate timer with same name: %.*s\n",
  229. tmp.name.len, tmp.name.s);
  230. free_params(params_list);
  231. return -1;
  232. }
  233. if(tmp.interval==0)
  234. tmp.interval = 120;
  235. nt = (stm_timer_t*)pkg_malloc(sizeof(stm_timer_t));
  236. if(nt==0)
  237. {
  238. LM_ERR("no more pkg memory\n");
  239. free_params(params_list);
  240. return -1;
  241. }
  242. memcpy(nt, &tmp, sizeof(stm_timer_t));
  243. nt->next = _stm_list;
  244. _stm_list = nt;
  245. free_params(params_list);
  246. return 0;
  247. }
  248. int stm_e_param(modparam_t type, void *val)
  249. {
  250. param_t* params_list = NULL;
  251. param_hooks_t phooks;
  252. param_t *pit=NULL;
  253. stm_route_t tmp;
  254. stm_route_t *rt;
  255. stm_timer_t *nt;
  256. str s;
  257. char c;
  258. if(val==NULL)
  259. return -1;
  260. s.s = (char*)val;
  261. s.len = strlen(s.s);
  262. if(s.s[s.len-1]==';')
  263. s.len--;
  264. if (parse_params(&s, CLASS_ANY, &phooks, &params_list)<0)
  265. return -1;
  266. memset(&tmp, 0, sizeof(stm_route_t));
  267. for (pit = params_list; pit; pit=pit->next)
  268. {
  269. if (pit->name.len==5
  270. && strncasecmp(pit->name.s, "timer", 5)==0) {
  271. tmp.timer = pit->body;
  272. } else if(pit->name.len==5
  273. && strncasecmp(pit->name.s, "route", 5)==0) {
  274. s = pit->body;
  275. }
  276. }
  277. if(tmp.timer.s==NULL)
  278. {
  279. LM_ERR("invalid timer name\n");
  280. free_params(params_list);
  281. return -1;
  282. }
  283. /* get the timer */
  284. nt = _stm_list;
  285. while(nt) {
  286. if(nt->name.len==tmp.timer.len
  287. && strncasecmp(nt->name.s, tmp.timer.s, tmp.timer.len)==0)
  288. break;
  289. nt = nt->next;
  290. }
  291. if(nt==NULL)
  292. {
  293. LM_ERR("timer not found - name: %.*s\n",
  294. tmp.timer.len, tmp.timer.s);
  295. free_params(params_list);
  296. return -1;
  297. }
  298. c = s.s[s.len];
  299. s.s[s.len] = '\0';
  300. tmp.route = route_get(&main_rt, s.s);
  301. s.s[s.len] = c;
  302. if(tmp.route == -1)
  303. {
  304. LM_ERR("invalid route: %.*s\n",
  305. s.len, s.s);
  306. free_params(params_list);
  307. return -1;
  308. }
  309. rt = (stm_route_t*)pkg_malloc(sizeof(stm_route_t));
  310. if(rt==0)
  311. {
  312. LM_ERR("no more pkg memory\n");
  313. free_params(params_list);
  314. return -1;
  315. }
  316. memcpy(rt, &tmp, sizeof(stm_route_t));
  317. rt->next = nt->rt;
  318. nt->rt = rt;
  319. free_params(params_list);
  320. return 0;
  321. }