jsonrpc_mod.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /**
  2. * $Id$
  3. *
  4. * Copyright (C) 2011 Flowroute LLC (flowroute.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 <arpa/inet.h>
  25. #include <sys/types.h>
  26. #include <errno.h>
  27. #include "../../mod_fix.h"
  28. #include "../../trim.h"
  29. #include "../../sr_module.h"
  30. #include "../tm/tm_load.h"
  31. #include "jsonrpc_request.h"
  32. #include "jsonrpc_io.h"
  33. #include "jsonrpc.h"
  34. MODULE_VERSION
  35. static int mod_init(void);
  36. static int child_init(int);
  37. static int fixup_request(void** param, int param_no);
  38. static int fixup_notification(void** param, int param_no);
  39. static int fixup_request_free(void** param, int param_no);
  40. int fixup_pvar_shm(void** param, int param_no);
  41. char *servers_param;
  42. int pipe_fds[2] = {-1,-1};
  43. struct tm_binds tmb;
  44. /*
  45. * Exported Functions
  46. */
  47. static cmd_export_t cmds[]={
  48. {"jsonrpc_request", (cmd_function)jsonrpc_request, 5, fixup_request, fixup_request_free, ANY_ROUTE},
  49. {"jsonrpc_notification", (cmd_function)jsonrpc_notification, 2, fixup_notification, 0, ANY_ROUTE},
  50. {0, 0, 0, 0, 0, 0}
  51. };
  52. /*
  53. * Script Parameters
  54. */
  55. static param_export_t mod_params[]={
  56. {"servers", PARAM_STRING, &servers_param},
  57. { 0,0,0 }
  58. };
  59. /*
  60. * Exports
  61. */
  62. struct module_exports exports = {
  63. "jsonrpc", /* module name */
  64. DEFAULT_DLFLAGS, /* dlopen flags */
  65. cmds, /* Exported functions */
  66. mod_params, /* Exported parameters */
  67. 0, /* exported statistics */
  68. 0, /* exported MI functions */
  69. 0, /* exported pseudo-variables */
  70. 0, /* extra processes */
  71. mod_init, /* module initialization function */
  72. 0, /* response function*/
  73. 0, /* destroy function */
  74. child_init /* per-child init function */
  75. };
  76. static int mod_init(void) {
  77. load_tm_f load_tm;
  78. /* load the tm functions */
  79. if ( !(load_tm=(load_tm_f)find_export("load_tm", NO_SCRIPT, 0)))
  80. {
  81. LOG(L_ERR, "ERROR:jsonrpc:mod_init: cannot import load_tm\n");
  82. return -1;
  83. }
  84. /* let the auto-loading function load all TM stuff */
  85. if (load_tm( &tmb )==-1)
  86. return -1;
  87. if (servers_param == NULL) {
  88. LM_ERR("servers parameter missing.\n");
  89. return -1;
  90. }
  91. register_procs(1);
  92. if (pipe(pipe_fds) < 0) {
  93. LM_ERR("pipe() failed\n");
  94. return -1;
  95. }
  96. return(0);
  97. }
  98. static int child_init(int rank)
  99. {
  100. int pid;
  101. if (rank>PROC_MAIN)
  102. cmd_pipe = pipe_fds[1];
  103. if (rank!=PROC_MAIN)
  104. return 0;
  105. pid=fork_process(PROC_NOCHLDINIT, "jsonrpc io handler", 1);
  106. if (pid<0)
  107. return -1; /* error */
  108. if(pid==0){
  109. /* child */
  110. close(pipe_fds[1]);
  111. return jsonrpc_io_child_process(pipe_fds[0], servers_param);
  112. }
  113. return 0;
  114. }
  115. /* Fixup Functions */
  116. static int fixup_request(void** param, int param_no)
  117. {
  118. if (param_no <= 4) {
  119. return fixup_spve_null(param, 1);
  120. } else if (param_no == 5) {
  121. return fixup_pvar_null(param, 1);
  122. }
  123. LM_ERR("jsonrpc_request takes exactly 5 parameters.\n");
  124. return -1;
  125. }
  126. static int fixup_notification(void** param, int param_no)
  127. {
  128. if (param_no <= 2) {
  129. return fixup_spve_null(param, 1);
  130. }
  131. LM_ERR("jsonrpc_notification takes exactly 2 parameters.\n");
  132. return -1;
  133. }
  134. static int fixup_request_free(void** param, int param_no)
  135. {
  136. if (param_no <= 4) {
  137. return 0;
  138. } else if (param_no == 5) {
  139. return fixup_free_pvar_null(param, 1);
  140. }
  141. LM_ERR("jsonrpc_request takes exactly 5 parameters.\n");
  142. return -1;
  143. }