timer.c 12 KB

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