4 * Copyright (C) 2005 iptelorg GmbH
6 * This file is part of ser, a free SIP server.
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
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:
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include <sys/types.h>
32 #include "mem/shm_mem.h"
33 #include "sr_module.h"
43 void dns_cache_debug(rpc_t* rpc, void* ctx);
44 void dns_cache_debug_all(rpc_t* rpc, void* ctx);
45 void dns_cache_mem_info(rpc_t* rpc, void* ctx);
47 static const char* dns_cache_mem_info_doc[] = {
48 "dns cache memory info.", /* Documentation string */
49 0 /* Method signature(s) */
51 static const char* dns_cache_debug_doc[] = {
52 "dns debug info.", /* Documentation string */
53 0 /* Method signature(s) */
56 static const char* dns_cache_debug_all_doc[] = {
57 "complete dns debug dump", /* Documentation string */
58 0 /* Method signature(s) */
61 #ifdef USE_DST_BLACKLIST
62 void dst_blst_debug(rpc_t* rpc, void* ctx);
63 void dst_blst_mem_info(rpc_t* rpc, void* ctx);
65 static const char* dst_blst_mem_info_doc[] = {
66 "dst blacklist memory usage info.", /* Documentation string */
67 0 /* Method signature(s) */
69 static const char* dst_blst_debug_doc[] = {
70 "dst blacklist debug info.", /* Documentation string */
71 0 /* Method signature(s) */
77 #define MAX_CTIME_LEN 128
80 static time_t up_since;
81 static char up_since_ctime[MAX_CTIME_LEN];
84 static const char* system_listMethods_doc[] = {
85 "Lists all RPC methods supported by the server.", /* Documentation string */
86 0 /* Method signature(s) */
89 static void system_listMethods(rpc_t* rpc, void* c)
94 for(ptr = core_rpc_methods; ptr && ptr->name; ptr++) {
95 if (rpc->add(c, "s", ptr->name) < 0) return;
98 for(t = modules; t; t = t->next) {
99 for(ptr = t->exports->rpc_methods; ptr && ptr->name; ptr++) {
100 if (rpc->add(c, "s", ptr->name) < 0) return;
105 static const char* system_methodSignature_doc[] = {
106 "Returns signature of given method.", /* Documentation string */
107 0 /* Method signature(s) */
110 static void system_methodSignature(rpc_t* rpc, void* c)
112 rpc->fault(c, 500, "Not Implemented Yet");
116 static const char* system_methodHelp_doc[] = {
117 "Print the help string for given method.", /* Documentation string */
118 0 /* Method signature(s) */
121 static void system_methodHelp(rpc_t* rpc, void* c)
127 if (rpc->scan(c, "s", &name) < 1) {
128 rpc->fault(c, 400, "Method Name Expected");
132 for(t = modules; t; t = t->next) {
133 for(ptr = t->exports->rpc_methods; ptr && ptr->name; ptr++) {
134 if (strcmp(name, ptr->name) == 0) {
135 if (ptr->doc_str && ptr->doc_str[0]) {
136 rpc->add(c, "s", ptr->doc_str[0]);
138 rpc->add(c, "s", "undocumented");
144 /* try the core methods too */
145 for (ptr=core_rpc_methods;ptr && ptr->name; ptr++){
146 if (strcmp(name, ptr->name) == 0) {
147 if (ptr->doc_str && ptr->doc_str[0]) {
148 rpc->add(c, "s", ptr->doc_str[0]);
150 rpc->add(c, "s", "undocumented");
155 rpc->fault(c, 400, "command not found");
159 static const char* core_prints_doc[] = {
160 "Returns the string given as parameter.", /* Documentation string */
161 0 /* Method signature(s) */
165 static void core_prints(rpc_t* rpc, void* c)
168 if (rpc->scan(c, "s", &string)>0)
169 rpc->add(c, "s", string);
173 static const char* core_version_doc[] = {
174 "Returns the version string of the server.", /* Documentation string */
175 0 /* Method signature(s) */
178 static void core_version(rpc_t* rpc, void* c)
180 rpc->add(c, "s", SERVER_HDR);
185 static const char* core_uptime_doc[] = {
186 "Returns uptime of SER server.", /* Documentation string */
187 0 /* Method signature(s) */
191 static void core_uptime(rpc_t* rpc, void* c)
198 if (rpc->add(c, "{", &s) < 0) return;
199 rpc->struct_add(s, "s", "now", ctime(&now));
200 rpc->struct_add(s, "s", "up_since", up_since_ctime);
201 /* no need for a float here (unless you're concerned that your uptime)
202 rpc->struct_add(s, "f", "uptime", difftime(now, up_since));
204 /* on posix system we can substract time_t directly */
205 rpc->struct_add(s, "d", "uptime", (int)(now-up_since));
209 static const char* core_ps_doc[] = {
210 "Returns the description of running SER processes.", /* Documentation string */
211 0 /* Method signature(s) */
215 static void core_ps(rpc_t* rpc, void* c)
219 for (p=0; p<process_count;p++) {
220 rpc->add(c, "d", pt[p].pid);
221 rpc->add(c, "s", pt[p].desc);
226 static const char* core_pwd_doc[] = {
227 "Returns the working directory of SER server.", /* Documentation string */
228 0 /* Method signature(s) */
232 static void core_pwd(rpc_t* rpc, void* c)
238 cwd_buf = pkg_malloc(max_len);
240 ERR("core_pwd: No memory left\n");
241 rpc->fault(c, 500, "Server Ran Out of Memory");
245 if (getcwd(cwd_buf, max_len)) {
246 rpc->add(c, "s", cwd_buf);
248 rpc->fault(c, 500, "getcwd Failed");
254 static const char* core_arg_doc[] = {
255 "Returns the list of command line arguments used on SER startup.", /* Documentation string */
256 0 /* Method signature(s) */
260 static void core_arg(rpc_t* rpc, void* c)
264 for (p = 0; p < my_argc; p++) {
265 if (rpc->add(c, "s", my_argv[p]) < 0) return;
270 static const char* core_kill_doc[] = {
271 "Sends the given signal to SER.", /* Documentation string */
272 0 /* Method signature(s) */
276 static void core_kill(rpc_t* rpc, void* c)
279 rpc->scan(c, "d", &sig_no);
284 static void core_shmmem(rpc_t* rpc, void* c)
290 rpc->add(c, "{", &handle);
291 rpc->struct_add(handle, "ddddd",
292 "total", mi.total_size,
294 "used", mi.real_used,
295 "max_used", mi.max_used,
296 "fragments", mi.total_frags
300 static const char* core_shmmem_doc[] = {
301 "Returns shared memory info.", /* Documentation string */
302 0 /* Method signature(s) */
306 static const char* core_tcpinfo_doc[] = {
307 "Returns tcp related info.", /* Documentation string */
308 0 /* Method signature(s) */
311 static void core_tcpinfo(rpc_t* rpc, void* c)
315 struct tcp_gen_info ti;
319 rpc->add(c, "{", &handle);
320 rpc->struct_add(handle, "ddd",
321 "readers", ti.tcp_readers,
322 "max_connections", ti.tcp_max_connections,
323 "opened_connections", ti.tcp_connections_no
326 rpc->fault(c, 500, "tcp support disabled");
329 rpc->fault(c, 500, "tcp support not compiled");
334 * RPC Methods exported by this module
336 rpc_export_t core_rpc_methods[] = {
337 {"system.listMethods", system_listMethods, system_listMethods_doc, RET_ARRAY},
338 {"system.methodSignature", system_methodSignature, system_methodSignature_doc, 0 },
339 {"system.methodHelp", system_methodHelp, system_methodHelp_doc, 0 },
340 {"core.prints", core_prints, core_prints_doc, 0 },
341 {"core.version", core_version, core_version_doc, 0 },
342 {"core.uptime", core_uptime, core_uptime_doc, 0 },
343 {"core.ps", core_ps, core_ps_doc, RET_ARRAY},
344 {"core.pwd", core_pwd, core_pwd_doc, RET_ARRAY},
345 {"core.arg", core_arg, core_arg_doc, RET_ARRAY},
346 {"core.kill", core_kill, core_kill_doc, 0 },
347 {"core.shmmem", core_shmmem, core_shmmem_doc, 0 },
348 {"core.tcp_info", core_tcpinfo, core_tcpinfo_doc, 0 },
350 {"dns.mem_info", dns_cache_mem_info, dns_cache_mem_info_doc, 0 },
351 {"dns.debug", dns_cache_debug, dns_cache_debug_doc, 0 },
352 {"dns.debug_all", dns_cache_debug_all, dns_cache_debug_all_doc, 0 },
354 #ifdef USE_DST_BLACKLIST
355 {"dst_blacklist.mem_info", dst_blst_mem_info, dst_blst_mem_info_doc, 0 },
356 {"dst_blacklist.debug", dst_blst_debug, dst_blst_debug_doc, 0 },
361 int rpc_init_time(void)
366 if (strlen(t)+1>=MAX_CTIME_LEN) {
367 ERR("Too long data %d\n", (int)strlen(t));
370 memcpy(up_since_ctime,t,strlen(t)+1);