mi_xmlrpc.c

Go to the documentation of this file.
00001 /*
00002  * $Id: mi_xmlrpc.c 5074 2008-10-15 11:34:29Z henningw $
00003  *
00004  * Copyright (C) 2006 Voice Sistem SRL
00005  *
00006  * This file is part of Kamailio.
00007  *
00008  * Kamailio is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU General Public License
00010  * as published by the Free Software Foundation; either version 2
00011  * of the License, or (at your option) any later version.
00012  *
00013  * Kamailio is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00021  *
00022  * History:
00023  * ---------
00024  *  2006-11-30  first version (lavinia)
00025  *  2007-10-05  support for libxmlrpc-c3 version 1.x.x added (dragos)
00026  */
00027 
00028 
00029 
00030 
00031 #include <sys/stat.h>
00032 #include <sys/types.h>
00033 #include <unistd.h>
00034 #include <errno.h>
00035 #include <signal.h>
00036 #include <sys/signal.h>
00037 #include <sys/wait.h>
00038 #include <grp.h>
00039 #include <stdlib.h>
00040 #include "mi_xmlrpc.h"
00041 #include "xr_writer.h"
00042 #include "xr_parser.h"
00043 #include "xr_server.h"
00044 
00045 #define XMLRPC_SERVER_WANT_ABYSS_HANDLERS
00046 
00047 #ifdef XMLRPC_OLD_VERSION
00048 
00049 #include "abyss.h"
00050 #include <xmlrpc_abyss.h>
00051 #include <xmlrpc.h>
00052 
00053 #else
00054 
00055 #include <xmlrpc-c/base.h>
00056 #include <xmlrpc-c/abyss.h>
00057 #include <xmlrpc-c/server.h>
00058 #include <xmlrpc-c/server_abyss.h>
00059 
00060 #endif
00061 
00062 
00063 #include "../../sr_module.h"
00064 #include "../../str.h"
00065 #include "../../mem/mem.h"
00066 #include "../../mem/shm_mem.h"
00067 
00068 xmlrpc_env env;
00069 xmlrpc_value * xr_response;
00070 xmlrpc_registry * registryP;
00071 
00072 int rpl_opt = 0;
00073 
00074 /* module functions */
00075 static int mod_init();
00076 static void destroy(void);
00077 static void xmlrpc_process(int rank);
00078 
00079 static int port = 8080;
00080 static char *log_file = NULL; 
00081 static int read_buf_size = MAX_READ;
00082 static TServer srv;
00083 MODULE_VERSION
00084 
00085 
00086 static proc_export_t mi_procs[] = {
00087    {"MI XMLRPC",  0,  0, xmlrpc_process, 1 },
00088    {0,0,0,0,0}
00089 };
00090 
00091 
00092 /* module parameters */
00093 static param_export_t mi_params[] = {
00094    {"port",             INT_PARAM, &port},
00095    {"log_file",            STR_PARAM, &log_file},
00096    {"reply_option",        INT_PARAM, &rpl_opt},
00097    {"buffer_size",            INT_PARAM, &read_buf_size},
00098    {0,0,0}
00099 };
00100 
00101 /* module exports */
00102 struct module_exports exports = {
00103    "mi_xmlrpc",                        /* module name */
00104    DEFAULT_DLFLAGS,                    /* dlopen flags */
00105    0,                                  /* exported functions */
00106    mi_params,                          /* exported parameters */
00107    0,                                  /* exported statistics */
00108    0,                                  /* exported MI functions */
00109    0,                                  /* exported PV */
00110    mi_procs,                           /* extra processes */
00111    mod_init,                           /* module initialization function */
00112    0,                                  /* response handling function */
00113    destroy,                            /* destroy function */
00114    0                                   /* per-child init function */
00115 };
00116 
00117 
00118 static int mod_init(void)
00119 {
00120    LM_DBG("testing port number...\n");
00121 
00122    if ( port <= 1024 ) {
00123       LM_WARN("port<1024, using 8080...\n");
00124       port = 8080;
00125    }
00126 
00127    if (init_async_lock()!=0) {
00128       LM_ERR("failed to init async lock\n");
00129       return -1;
00130    }
00131 
00132    return 0;
00133 }
00134 
00135 
00136 static void xmlrpc_sigchld( int sig )
00137 {
00138    pid_t pid;
00139    int status;
00140 
00141    while(1) {
00142       pid = waitpid( (pid_t) -1, &status, WNOHANG );
00143 
00144       /* none left */
00145       if ( pid == 0 )
00146          break;
00147 
00148       if (pid<0) {
00149          /* because of ptrace */
00150          if ( errno == EINTR )
00151             continue;
00152 
00153          break;
00154       }
00155       #ifndef XMLRPC_OLD_VERSION
00156       else 
00157          ServerHandleSigchld(pid);
00158       #endif
00159    }
00160 }
00161 
00162 
00163 static void xmlrpc_process(int rank)
00164 {
00165    /* install handler to catch termination of child processes */
00166    if (signal(SIGCHLD, xmlrpc_sigchld)==SIG_ERR) {
00167       LM_ERR("failed to install signal handler for SIGCHLD\n");
00168       goto error;
00169    }
00170 
00171    /* Server Abyss init */
00172 
00173    xmlrpc_env_init(&env);
00174 
00175    #ifdef XMLRPC_OLD_VERSION
00176    xmlrpc_server_abyss_init_registry();
00177    registryP= xmlrpc_server_abyss_registry();
00178    #else
00179    registryP = xmlrpc_registry_new(&env);    
00180    #endif
00181 
00182    DateInit();
00183    MIMETypeInit();
00184 
00185    if (!ServerCreate(&srv, "XmlRpcServer", port, "", log_file)) {
00186       LM_ERR("failed to create XMLRPC server\n");
00187       goto error;
00188    }
00189 
00190    #ifdef XMLRPC_OLD_VERSION
00191    if (!ServerAddHandler(&srv, xmlrpc_server_abyss_rpc2_handler)) {
00192       LM_ERR("failed to add handler to server\n");
00193       goto error;
00194    }
00195 
00196    ServerDefaultHandler(&srv, xmlrpc_server_abyss_default_handler);
00197 
00198    #else
00199 
00200    xmlrpc_server_abyss_set_handlers2(&srv, "/RPC2", registryP);
00201 
00202    #endif
00203 
00204    ServerInit(&srv);
00205 
00206    if( init_mi_child() != 0 ) {
00207       LM_CRIT("failed to init the mi process\n");
00208       goto error;
00209    }
00210 
00211    if ( xr_writer_init(read_buf_size) != 0 ) {
00212       LM_ERR("failed to init the reply writer\n");
00213       goto error;
00214    }
00215    #ifdef XMLRPC_OLD_VERSION
00216    xmlrpc_env_init(&env);
00217    #endif
00218 
00219    if ( rpl_opt == 1 ) {
00220       xr_response = xmlrpc_build_value(&env, "()");
00221       if ( env.fault_occurred ){
00222          LM_ERR("failed to create an empty array: %s\n", env.fault_string);
00223          goto cleanup;
00224       }
00225    }
00226 
00227    if ( set_default_method(&env,registryP) != 0 ) {
00228       LM_ERR("failed to set up the default method!\n");
00229       goto cleanup;
00230    }
00231 
00232    /* Run server abyss */
00233    LM_INFO("starting xmlrpc server\n");
00234 
00235    ServerRun(&srv);
00236 
00237    LM_CRIT("Server terminated!!!\n");
00238 
00239 cleanup:
00240    xmlrpc_env_clean(&env);
00241    if ( xr_response ) xmlrpc_DECREF(xr_response);
00242 error:
00243    exit(-1);
00244 }
00245 
00246 
00247 void destroy(void)
00248 {
00249    destroy_async_lock();
00250 }

Generated on Wed May 23 20:00:27 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6