timer.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /*
  2. * Copyright (C) 2006 iptelorg GmbH
  3. *
  4. * This file is part of Kamailio, a free SIP server.
  5. *
  6. * Kamailio is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version
  10. *
  11. * Kamailio is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <string.h>
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <ctype.h>
  24. #include "../../timer.h"
  25. #include "../../timer_ticks.h"
  26. #include "../../route.h"
  27. #include "../../sr_module.h"
  28. #include "../../mem/mem.h"
  29. #include "../../mem/shm_mem.h"
  30. #include "../../str.h"
  31. #include "../../error.h"
  32. #include "../../config.h"
  33. #include "../../trim.h"
  34. #include "../../select.h"
  35. #include "../../ut.h"
  36. #include "../../select_buf.h"
  37. #include "../../receive.h"
  38. #include "../../ip_addr.h"
  39. #include "../../receive.h"
  40. #include "../../globals.h"
  41. #include "../../route.h"
  42. #include "../../parser/msg_parser.h"
  43. #include "../../action.h"
  44. #include "../../script_cb.h"
  45. #include "../../dset.h"
  46. #include "../../usr_avp.h"
  47. MODULE_VERSION
  48. #define MODULE_NAME "timer"
  49. struct timer_action {
  50. char *timer_name;
  51. int route_no;
  52. int interval;
  53. int enable_on_start;
  54. int disable_itself;
  55. unsigned short flags; /* slow / fast */
  56. struct timer_ln *link;
  57. struct timer_action* next;
  58. };
  59. /* list of all operations */
  60. static struct timer_action* timer_actions = 0;
  61. static struct timer_action* pkg_timer_actions = 0;
  62. static struct receive_info rcv_info;
  63. static struct timer_action* timer_executed = 0;
  64. #define eat_spaces(_p) \
  65. while( *(_p)==' ' || *(_p)=='\t' ){\
  66. (_p)++;}
  67. #define eat_alphanum(_p) \
  68. while ( (*(_p) >= 'a' && *(_p) <= 'z') || (*(_p) >= 'A' && *(_p) <= 'Z') || (*(_p) >= '0' && *(_p) <= '9') || (*(_p) == '_') ) {\
  69. (_p)++;\
  70. }
  71. static struct timer_action* find_action_by_name(struct timer_action* timer_actions, char *name, int len) {
  72. struct timer_action *a;
  73. if (len == -1) len = strlen(name);
  74. for (a=timer_actions; a; a = a->next) {
  75. if (a->timer_name && strlen(a->timer_name)==len && strncmp(name, a->timer_name, len) == 0)
  76. return a;
  77. }
  78. return NULL;
  79. }
  80. static int sel_root(str* res, select_t* s, struct sip_msg* msg) { /* dummy */
  81. return 0;
  82. }
  83. static int sel_timer(str* res, select_t* s, struct sip_msg* msg) {
  84. struct timer_action* a;
  85. if (!msg) { /* select fixup */
  86. a = find_action_by_name(timer_actions /* called after mod_init */, s->params[2].v.s.s, s->params[2].v.s.len);
  87. if (!a) {
  88. ERR(MODULE_NAME": timer_enable_fixup: timer '%.*s' not declared\n", s->params[2].v.s.len, s->params[2].v.s.s);
  89. return E_CFG;
  90. }
  91. s->params[2].v.p = a;
  92. }
  93. return 0;
  94. }
  95. static int sel_enabled(str* res, select_t* s, struct sip_msg* msg) {
  96. static char buf[2] = "01";
  97. if (!msg) return sel_timer(res, s, msg);
  98. res->len = 1;
  99. res->s = &buf[(((struct timer_action*) s->params[2].v.p)->link->flags & F_TIMER_ACTIVE) != 0];
  100. return 0;
  101. }
  102. static int sel_executed(str* res, select_t* s, struct sip_msg* msg) {
  103. if (!timer_executed) return 1;
  104. res->s = timer_executed->timer_name;
  105. res->len = strlen(res->s);
  106. return 0;
  107. }
  108. select_row_t sel_declaration[] = {
  109. { NULL, SEL_PARAM_STR, STR_STATIC_INIT(MODULE_NAME), sel_root, SEL_PARAM_EXPECTED},
  110. { sel_root, SEL_PARAM_STR, STR_STATIC_INIT("timer"), sel_timer, SEL_PARAM_EXPECTED|CONSUME_NEXT_STR|FIXUP_CALL},
  111. { sel_timer, SEL_PARAM_STR, STR_STATIC_INIT("enabled"), sel_enabled, 0},
  112. { sel_root, SEL_PARAM_STR, STR_STATIC_INIT("executed"), sel_executed, 0},
  113. { NULL, SEL_PARAM_STR, STR_NULL, NULL, 0}
  114. };
  115. static unsigned int timer_msg_no = 0;
  116. static ticks_t timer_handler(ticks_t ticks, struct timer_ln* tl, void* data) {
  117. /*?min length of first line of message is 16 char!?*/
  118. #define MSG "GET /timer HTTP/0.9\n\n"
  119. struct sip_msg* msg;
  120. struct timer_action *a;
  121. struct run_act_ctx ra_ctx;
  122. a = data;
  123. if (!a->disable_itself) {
  124. DEBUG(MODULE_NAME": handler: called at %d ticks, timer: '%s', pid:%d\n", ticks, a->timer_name, getpid());
  125. if (a->route_no >= main_rt.idx) {
  126. BUG(MODULE_NAME": invalid routing table number #%d of %d\n", a->route_no, main_rt.idx);
  127. goto err2;
  128. }
  129. if (!main_rt.rlist[a->route_no]) {
  130. WARN(MODULE_NAME": route not declared (hash:%d)\n", a->route_no);
  131. goto err2;
  132. }
  133. msg=pkg_malloc(sizeof(struct sip_msg));
  134. if (msg==0) {
  135. ERR(MODULE_NAME": handler: no mem for sip_msg\n");
  136. goto err2;
  137. }
  138. timer_msg_no++;
  139. memset(msg, 0, sizeof(struct sip_msg)); /* init everything to 0 */
  140. msg->buf=MSG;
  141. msg->len=sizeof(MSG)-1;
  142. msg->rcv= rcv_info;
  143. msg->id=timer_msg_no;
  144. msg->set_global_address=default_global_address;
  145. msg->set_global_port=default_global_port;
  146. if (parse_msg(msg->buf, msg->len, msg)!=0){
  147. ERR(MODULE_NAME": handler: parse_msg failed\n");
  148. goto err;
  149. }
  150. /* ... clear branches from previous message */
  151. clear_branches();
  152. reset_static_buffer();
  153. if (exec_pre_script_cb(msg, REQUEST_CB_TYPE)==0 )
  154. goto end; /* drop the request */
  155. /* exec the routing script */
  156. timer_executed = a;
  157. init_run_actions_ctx(&ra_ctx);
  158. run_actions(&ra_ctx, main_rt.rlist[a->route_no], msg);
  159. timer_executed = 0;
  160. /* execute post request-script callbacks */
  161. exec_post_script_cb(msg, REQUEST_CB_TYPE);
  162. end:
  163. reset_avps();
  164. DEBUG(MODULE_NAME": handler: cleaning up\n");
  165. err:
  166. free_sip_msg(msg);
  167. pkg_free(msg);
  168. err2: ;
  169. }
  170. /* begin critical section */
  171. if (a->disable_itself) {
  172. timer_allow_del();
  173. timer_del(a->link);
  174. timer_reinit(a->link);
  175. a->disable_itself = 0;
  176. /* end critical section */
  177. return 0; /* do no call more */
  178. }
  179. else
  180. return (ticks_t)(-1); /* periodical */
  181. }
  182. static int timer_enable_fixup(void** param, int param_no) {
  183. struct timer_action* a;
  184. int /*res, */n;
  185. switch (param_no) {
  186. case 1:
  187. a = find_action_by_name(timer_actions /* called after mod_init*/, (char*) *param, -1);
  188. if (!a) {
  189. ERR(MODULE_NAME": timer_enable_fixup: timer '%s' not declared\n", (char*) *param);
  190. return E_CFG;
  191. }
  192. *param = a;
  193. break;
  194. case 2:
  195. /* res = fixup_int_12(param, param_no);
  196. if (res < 0) return res; */
  197. n=atoi((char *)*param);
  198. *param = (void*)(long)(/*(int) *param*/n != 0);
  199. break;
  200. default: ;
  201. }
  202. return 0;
  203. }
  204. static int timer_enable_func(struct sip_msg* m, char* timer_act, char* enable) {
  205. struct timer_action* a;
  206. int en;
  207. a = (void*) timer_act;
  208. en = (int)(long) enable;
  209. /* timer is not deleted immediately but is removed from handler by itself because timer_del may be slow blocking procedure
  210. * Disable and enable in sequence may be tricky
  211. */
  212. /* begin critical section */
  213. if ((a->link->flags & F_TIMER_ACTIVE) == 0) {
  214. if (en) {
  215. timer_reinit(a->link);
  216. timer_add(a->link, MS_TO_TICKS(a->interval));
  217. a->disable_itself = 0;
  218. }
  219. }
  220. else {
  221. if (en && a->disable_itself) {
  222. a->disable_itself = 0; /* it's not 100% reliable! */
  223. }
  224. else if (!en) {
  225. a->disable_itself++;
  226. }
  227. }
  228. /* end critical section */
  229. return 1;
  230. }
  231. static int get_next_part(char** s, str* part, char delim) {
  232. char *c, *c2;
  233. c = c2 = *s;
  234. eat_spaces(c);
  235. while (*c2!=delim && *c2!=0) c2++;
  236. if (*c2) {
  237. *s = c2+1;
  238. }
  239. else {
  240. *s = c2;
  241. }
  242. eat_spaces(*s);
  243. c2--;
  244. /* rtrim */
  245. while ( c2 >= c && ((*c2 == ' ')||(*c2 == '\t')) ) c2--;
  246. part->s = c;
  247. part->len = c2-c+1;
  248. return part->len;
  249. }
  250. /* timer_id=route_no,interval_ms[,"slow"|"fast"[,"enable"]] */
  251. static int declare_timer(modparam_t type, char* param) {
  252. int n;
  253. unsigned int route_no, interval, enabled, flags;
  254. struct timer_action *pa;
  255. char *p, *save_p, c, *timer_name;
  256. str s;
  257. timer_name = 0;
  258. save_p = p = param;
  259. eat_alphanum(p);
  260. if (*p != '=' || p == save_p) goto err;
  261. *p = '\0';
  262. timer_name = save_p;
  263. p++;
  264. if (find_action_by_name(pkg_timer_actions, timer_name, -1) != NULL) {
  265. ERR(MODULE_NAME": declare_timer: timer '%s' already exists\n", timer_name);
  266. return E_CFG;
  267. }
  268. save_p = p;
  269. if (!get_next_part(&p, &s, ',')) goto err;
  270. c = s.s[s.len];
  271. s.s[s.len] = '\0';
  272. n = route_get(&main_rt, s.s);
  273. s.s[s.len] = c;
  274. if (n == -1) goto err;
  275. route_no = n;
  276. save_p = p;
  277. if (!get_next_part(&p, &s, ',')) goto err;
  278. if (str2int(&s, &interval) < 0) goto err;
  279. save_p = p;
  280. flags = 0;
  281. if (get_next_part(&p, &s, ',')) {
  282. if (s.len == 4 && strncasecmp(s.s, "FAST", 4)==0)
  283. flags = F_TIMER_FAST;
  284. else if (s.len == 4 && strncasecmp(s.s, "SLOW", 4)==0)
  285. ;
  286. else goto err;
  287. }
  288. save_p = p;
  289. enabled = 0;
  290. if (get_next_part(&p, &s, ',')) {
  291. if (s.len == 6 && strncasecmp(s.s, "ENABLE", 6)==0)
  292. enabled = 1;
  293. else goto err;
  294. }
  295. pa = pkg_malloc(sizeof(*pa)); /* cannot use shmmem here! */
  296. if (!pa) {
  297. ERR(MODULE_NAME": cannot allocate timer data\n");
  298. return E_OUT_OF_MEM;
  299. }
  300. memset(pa, 0, sizeof(*pa));
  301. pa->timer_name = timer_name;
  302. pa->route_no = route_no;
  303. pa->interval = interval;
  304. pa->enable_on_start = enabled;
  305. pa->flags = flags;
  306. pa->next = pkg_timer_actions;
  307. pkg_timer_actions = pa;
  308. return 0;
  309. err:
  310. ERR(MODULE_NAME": declare_timer: timer_name: '%s', error near '%s'\n", timer_name, save_p);
  311. return E_CFG;
  312. }
  313. static int mod_init() {
  314. struct timer_action *a, **pa;
  315. DEBUG(MODULE_NAME": init: initializing, pid=%d\n", getpid());
  316. /* copy from pkg to shm memory */
  317. for (pa=&timer_actions; pkg_timer_actions; pa=&(*pa)->next) {
  318. a = pkg_timer_actions;
  319. *pa = shm_malloc(sizeof(**pa));
  320. if (!*pa) {
  321. ERR(MODULE_NAME": cannot allocate timer data\n");
  322. return E_OUT_OF_MEM;
  323. }
  324. memcpy(*pa, a, sizeof(**pa));
  325. (*pa)->next = 0;
  326. pkg_timer_actions = a->next;
  327. pkg_free(a);
  328. }
  329. for (a=timer_actions; a; a=a->next) {
  330. a->link = timer_alloc();
  331. if (!a->link) {
  332. ERR(MODULE_NAME": init: cannot allocate timer\n");
  333. return E_OUT_OF_MEM;
  334. }
  335. timer_init(a->link, timer_handler, a, a->flags);
  336. if (!a->link) {
  337. ERR(MODULE_NAME": init: cannot initialize timer\n");
  338. return E_CFG;
  339. }
  340. }
  341. memset(&rcv_info, 0, sizeof(rcv_info));
  342. register_select_table(sel_declaration);
  343. return 0;
  344. }
  345. static int child_init(int rank) {
  346. struct timer_action* a;
  347. /* may I start timer in mod_init ?? */
  348. if (rank!=PROC_TIMER) return 0;
  349. for (a=timer_actions; a; a=a->next) {
  350. if (a->enable_on_start) {
  351. timer_add(a->link, MS_TO_TICKS(a->interval));
  352. }
  353. }
  354. return 0;
  355. }
  356. static void destroy_mod(void) {
  357. struct timer_action* a;
  358. DEBUG(MODULE_NAME": destroy: destroying, pid=%d\n", getpid());
  359. while (timer_actions) {
  360. a = timer_actions;
  361. if (a->link) {
  362. timer_del(a->link);
  363. timer_free(a->link);
  364. }
  365. timer_actions = a->next;
  366. shm_free(a);
  367. }
  368. }
  369. /*
  370. * Exported functions
  371. */
  372. static cmd_export_t cmds[] = {
  373. {MODULE_NAME"_enable", timer_enable_func, 2, timer_enable_fixup, REQUEST_ROUTE | ONREPLY_ROUTE | FAILURE_ROUTE | BRANCH_ROUTE | ONSEND_ROUTE},
  374. {0, 0, 0, 0, 0}
  375. };
  376. /*
  377. * Exported parameters
  378. */
  379. static param_export_t params[] = {
  380. {"declare_timer", PARAM_STRING|PARAM_USE_FUNC, (void*) declare_timer},
  381. {0, 0, 0}
  382. };
  383. struct module_exports exports = {
  384. MODULE_NAME,
  385. cmds, /* Exported commands */
  386. 0, /* RPC */
  387. params, /* Exported parameters */
  388. mod_init, /* module initialization function */
  389. 0, /* response function*/
  390. destroy_mod, /* destroy function */
  391. 0, /* oncancel function */
  392. child_init /* per-child init function */
  393. };