jsonrpc_mod.c

00001 
00025 #include <arpa/inet.h>
00026 #include <sys/types.h>
00027 #include <errno.h>
00028 
00029 #include "../../mod_fix.h"
00030 #include "../../trim.h"
00031 #include "../../sr_module.h"
00032 #include "../tm/tm_load.h"
00033 
00034 #include "jsonrpc_request.h"
00035 #include "jsonrpc_io.h"
00036 #include "jsonrpc.h"
00037 
00038 
00039 MODULE_VERSION
00040 
00041 
00042 static int mod_init(void);
00043 static int child_init(int);
00044 static int fixup_request(void** param, int param_no);
00045 static int fixup_notification(void** param, int param_no);
00046 static int fixup_request_free(void** param, int param_no);
00047 int        fixup_pvar_shm(void** param, int param_no);
00048 
00049 char *servers_param;
00050 int  pipe_fds[2] = {-1,-1};
00051 
00052 struct tm_binds tmb;
00053 
00054 /*
00055  * Exported Functions
00056  */
00057 static cmd_export_t cmds[]={
00058         {"jsonrpc_request", (cmd_function)jsonrpc_request, 5, fixup_request, fixup_request_free, ANY_ROUTE},
00059         {"jsonrpc_notification", (cmd_function)jsonrpc_notification, 2, fixup_notification, 0, ANY_ROUTE},
00060         {0, 0, 0, 0, 0, 0}
00061 };
00062  
00063 
00064 /*
00065  * Script Parameters
00066  */
00067 static param_export_t mod_params[]={
00068         {"servers", STR_PARAM, &servers_param},
00069         { 0,0,0 }
00070 };
00071 
00072  
00073 /*
00074  * Exports
00075  */
00076 struct module_exports exports = {
00077                 "jsonrpc",           /* module name */
00078                 DEFAULT_DLFLAGS,     /* dlopen flags */
00079                 cmds,                /* Exported functions */
00080                 mod_params,          /* Exported parameters */
00081                 0,                   /* exported statistics */
00082                 0,                   /* exported MI functions */
00083                 0,                   /* exported pseudo-variables */
00084                 0,                   /* extra processes */
00085                 mod_init,            /* module initialization function */
00086                 0,                   /* response function*/
00087                 0,                   /* destroy function */
00088                 child_init           /* per-child init function */
00089 };
00090 
00091 
00092 static int mod_init(void) {
00093         load_tm_f  load_tm;
00094 
00095         /* load the tm functions  */
00096         if ( !(load_tm=(load_tm_f)find_export("load_tm", NO_SCRIPT, 0)))
00097         {
00098                 LOG(L_ERR, "ERROR:jsonrpc:mod_init: cannot import load_tm\n");
00099                 return -1;
00100         }
00101         /* let the auto-loading function load all TM stuff */
00102         if (load_tm( &tmb )==-1)
00103                 return -1;
00104 
00105         if (servers_param == NULL) {
00106                 LM_ERR("servers parameter missing.\n");
00107                 return -1;
00108         }
00109 
00110         register_procs(1);
00111 
00112         if (pipe(pipe_fds) < 0) {
00113                 LM_ERR("pipe() failed\n");
00114                 return -1;
00115         }
00116         
00117         return(0);
00118 }
00119 
00120 static int child_init(int rank) 
00121 {
00122         int pid;
00123         
00124         if (rank>PROC_MAIN)
00125                 cmd_pipe = pipe_fds[1];
00126 
00127         if (rank!=PROC_MAIN)
00128                 return 0;
00129 
00130         pid=fork_process(PROC_NOCHLDINIT, "jsonrpc io handler", 1);
00131         if (pid<0)
00132                 return -1; /* error */
00133         if(pid==0){
00134                 /* child */
00135                 close(pipe_fds[1]);
00136                 return jsonrpc_io_child_process(pipe_fds[0], servers_param);
00137         }
00138         return 0;
00139 }
00140 
00141 /* Fixup Functions */
00142 
00143 static int fixup_request(void** param, int param_no)
00144 {
00145   if (param_no <= 4) {
00146                 return fixup_spve_null(param, 1);
00147         } else if (param_no == 5) {
00148                 return fixup_pvar_null(param, 1);
00149         }
00150         LM_ERR("jsonrpc_request takes exactly 5 parameters.\n");
00151         return -1;
00152 }
00153 
00154 static int fixup_notification(void** param, int param_no)
00155 {
00156   if (param_no <= 2) {
00157                 return fixup_spve_null(param, 1);
00158         }
00159         LM_ERR("jsonrpc_notification takes exactly 2 parameters.\n");
00160         return -1;
00161 }
00162 
00163 static int fixup_request_free(void** param, int param_no)
00164 {
00165   if (param_no <= 4) {
00166                 return 0;
00167         } else if (param_no == 5) {
00168                 return fixup_free_pvar_null(param, 1);
00169         }
00170         LM_ERR("jsonrpc_request takes exactly 5 parameters.\n");
00171         return -1;
00172 }