async_task.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * Copyright (C) 2014 Daniel-Constantin Mierla (asipto.com)
  3. *
  4. * This file is part of Kamailio, a free SIP server.
  5. *
  6. * Permission to use, copy, modify, and distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. /*!
  19. * \file
  20. * \brief Kamailio core :: Asynchronus tasks
  21. * \ingroup core
  22. * Module: \ref core
  23. */
  24. #include <stdio.h>
  25. #include <unistd.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <sys/socket.h>
  29. #include <sys/types.h>
  30. #include <sys/un.h>
  31. #include <netinet/in.h>
  32. #include <arpa/inet.h>
  33. #include <fcntl.h>
  34. #include <errno.h>
  35. #include "dprint.h"
  36. #include "sr_module.h"
  37. #include "ut.h"
  38. #include "pt.h"
  39. #include "cfg/cfg_struct.h"
  40. #include "async_task.h"
  41. static int _async_task_workers = 0;
  42. static int _async_task_sockets[2];
  43. int async_task_run(int idx);
  44. /**
  45. *
  46. */
  47. int async_task_init_sockets(void)
  48. {
  49. if (socketpair(PF_UNIX, SOCK_DGRAM, 0, _async_task_sockets) < 0) {
  50. LM_ERR("opening tasks dgram socket pair\n");
  51. return -1;
  52. }
  53. LM_DBG("inter-process event notification sockets initialized\n");
  54. return 0;
  55. }
  56. /**
  57. *
  58. */
  59. void async_task_close_sockets_child(void)
  60. {
  61. LM_DBG("closing the notification socket used by children\n");
  62. close(_async_task_sockets[1]);
  63. }
  64. /**
  65. *
  66. */
  67. void async_task_close_sockets_parent(void)
  68. {
  69. LM_DBG("closing the notification socket used by parent\n");
  70. close(_async_task_sockets[0]);
  71. }
  72. /**
  73. *
  74. */
  75. int async_task_init(void)
  76. {
  77. LM_DBG("start initializing asynk task framework\n");
  78. if(_async_task_workers<=0)
  79. return 0;
  80. /* advertise new processes to core */
  81. register_procs(_async_task_workers);
  82. /* advertise new processes to cfg framework */
  83. cfg_register_child(_async_task_workers);
  84. return 0;
  85. }
  86. /**
  87. *
  88. */
  89. int async_task_initialized(void)
  90. {
  91. if(_async_task_workers<=0)
  92. return 0;
  93. return 1;
  94. }
  95. /**
  96. *
  97. */
  98. int async_task_child_init(int rank)
  99. {
  100. int pid;
  101. int i;
  102. if(_async_task_workers<=0)
  103. return 0;
  104. LM_DBG("child initializing asynk task framework\n");
  105. if (rank==PROC_INIT) {
  106. if(async_task_init_sockets()<0) {
  107. LM_ERR("failed to initialize tasks sockets\n");
  108. return -1;
  109. }
  110. return 0;
  111. }
  112. if(rank>0) {
  113. async_task_close_sockets_parent();
  114. return 0;
  115. }
  116. if (rank!=PROC_MAIN)
  117. return 0;
  118. for(i=0; i<_async_task_workers; i++) {
  119. pid=fork_process(PROC_RPC, "Async Task Worker", 1);
  120. if (pid<0)
  121. return -1; /* error */
  122. if(pid==0) {
  123. /* child */
  124. /* initialize the config framework */
  125. if (cfg_child_init())
  126. return -1;
  127. /* main function for workers */
  128. if(async_task_run(i+1)<0) {
  129. LM_ERR("failed to initialize task worker process: %d\n", i);
  130. return -1;
  131. }
  132. }
  133. }
  134. return 0;
  135. }
  136. /**
  137. *
  138. */
  139. int async_task_set_workers(int n)
  140. {
  141. if(_async_task_workers>0) {
  142. LM_WARN("task workers already set\n");
  143. return 0;
  144. }
  145. if(n<=0)
  146. return 0;
  147. _async_task_workers = n;
  148. return 0;
  149. }
  150. /**
  151. *
  152. */
  153. int async_task_push(async_task_t *task)
  154. {
  155. int len;
  156. if(_async_task_workers<=0)
  157. return 0;
  158. len = write(_async_task_sockets[1], &task, sizeof(async_task_t*));
  159. if(len<=0) {
  160. LM_ERR("failed to pass the task to asynk workers\n");
  161. return -1;
  162. }
  163. LM_DBG("task sent [%p]\n", task);
  164. return 0;
  165. }
  166. /**
  167. *
  168. */
  169. int async_task_run(int idx)
  170. {
  171. async_task_t *ptask;
  172. int received;
  173. LM_DBG("async task worker %d ready\n", idx);
  174. for( ; ; ) {
  175. if ((received = recvfrom(_async_task_sockets[0],
  176. &ptask, sizeof(async_task_t*),
  177. 0, NULL, 0)) < 0) {
  178. LM_ERR("failed to received task (%d: %s)\n", errno, strerror(errno));
  179. continue;
  180. }
  181. if(received != sizeof(async_task_t*)) {
  182. LM_ERR("invalid task size %d\n", received);
  183. continue;
  184. }
  185. if(ptask->exec!=NULL) {
  186. LM_DBG("task executed [%p] (%p/%p)\n", ptask,
  187. ptask->exec, ptask->param);
  188. ptask->exec(ptask->param);
  189. }
  190. shm_free(ptask);
  191. }
  192. return 0;
  193. }