Browse code

- core commands reimplemented

Jan Janak authored on 12/12/2005 23:41:30
Showing 2 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,256 @@
0
+/*
1
+ * $Id$
2
+ *
3
+ * Copyright (C) 2005 iptelorg GmbH
4
+ *
5
+ * This file is part of ser, a free SIP server.
6
+ *
7
+ * ser is free software; you can redistribute it and/or modify
8
+ * it under the terms of the GNU General Public License as published by
9
+ * the Free Software Foundation; either version 2 of the License, or
10
+ * (at your option) any later version
11
+ *
12
+ * For a license to use the ser software under conditions
13
+ * other than those described here, or to purchase support for this
14
+ * software, please contact iptel.org by e-mail at the following addresses:
15
+ *    info@iptel.org
16
+ *
17
+ * ser is distributed in the hope that it will be useful,
18
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
+ * GNU General Public License for more details.
21
+ *
22
+ * You should have received a copy of the GNU General Public License 
23
+ * along with this program; if not, write to the Free Software 
24
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
+ */
26
+
27
+#include <time.h>
28
+#include <sys/types.h>
29
+#include <signal.h>
30
+#include "mem/mem.h"
31
+#include "sr_module.h"
32
+#include "dprint.h"
33
+#include "core_cmd.h"
34
+#include "globals.h"
35
+#include "pt.h"
36
+#include "ut.h"
37
+#include "core_cmd.h"
38
+
39
+
40
+#define MAX_CTIME_LEN 128
41
+
42
+/* up time */
43
+static time_t up_since;
44
+static char up_since_ctime[MAX_CTIME_LEN];
45
+
46
+
47
+static const char* system_listMethods_doc[] = {
48
+	"Lists all RPC methods supported by the server.",  /* Documentation string */
49
+	0                                                  /* Method signature(s) */
50
+};
51
+
52
+static void system_listMethods(rpc_t* rpc, void* c)
53
+{
54
+	struct sr_module* t;
55
+	rpc_export_t* ptr;
56
+	
57
+	for(ptr = core_rpc_methods; ptr && ptr->name; ptr++) {
58
+		if (rpc->add(c, "s", ptr->name) < 0) return;
59
+	}
60
+
61
+	for(t = modules; t; t = t->next) {
62
+		for(ptr = t->exports->rpc_methods; ptr && ptr->name; ptr++) {
63
+			if (rpc->add(c, "s", ptr->name) < 0) return;
64
+		}
65
+	}
66
+}
67
+
68
+static const char* system_methodSignature_doc[] = {
69
+	"Returns signature of given method.",  /* Documentation string */
70
+	0                                      /* Method signature(s) */
71
+};
72
+
73
+static void system_methodSignature(rpc_t* rpc, void* c)
74
+{
75
+	rpc->fault(c, 500, "Not Implemented Yet");
76
+}
77
+
78
+
79
+static const char* system_methodHelp_doc[] = {
80
+	"Print the help string for given method.",  /* Documentation string */
81
+	0                                           /* Method signature(s) */
82
+};
83
+
84
+static void system_methodHelp(rpc_t* rpc, void* c)
85
+{	
86
+	struct sr_module* t;
87
+	rpc_export_t* ptr;
88
+	char* name;
89
+
90
+	if (rpc->scan(c, "s", &name) < 0) return;
91
+
92
+	for(t = modules; t; t = t->next) {
93
+		for(ptr = t->exports->rpc_methods; ptr && ptr->name; ptr++) {
94
+			if (strcmp(name, ptr->name) == 0) {
95
+				if (ptr->doc_str && ptr->doc_str[0]) {
96
+					rpc->add(c, "s", ptr->doc_str[0]);
97
+				} else {
98
+					rpc->add(c, "s", "");
99
+				}
100
+				return;
101
+			}
102
+		}
103
+	}
104
+	rpc->fault(c, 500, "Method Not Implemented");
105
+}
106
+
107
+
108
+static const char* core_prints_doc[] = {
109
+	"Returns the string given as parameter.",   /* Documentation string */
110
+	0                                           /* Method signature(s) */
111
+};
112
+
113
+static void core_prints(rpc_t* rpc, void* c)
114
+{
115
+	char* string;
116
+
117
+	if (rpc->scan(c, "s", &string) < 0) return;
118
+	rpc->add(c, "s", string);
119
+}
120
+
121
+
122
+static const char* core_version_doc[] = {
123
+	"Returns the version string of the server.", /* Documentation string */
124
+	0                                           /* Method signature(s) */
125
+};
126
+
127
+static void core_version(rpc_t* rpc, void* c)
128
+{
129
+	rpc->add(c, "s", SERVER_HDR);
130
+}
131
+
132
+
133
+
134
+static const char* core_uptime_doc[] = {
135
+	"Returns uptime of SER server.",  /* Documentation string */
136
+	0                                 /* Method signature(s) */
137
+};
138
+
139
+
140
+static void core_uptime(rpc_t* rpc, void* c)
141
+{
142
+	time_t now;
143
+
144
+	time(&now);
145
+	rpc->printf(c, "now: %s", ctime(&now));
146
+	rpc->printf(c, "up_since: %s", up_since_ctime);
147
+	rpc->printf(c, "uptime: %f", difftime(now, up_since));
148
+}
149
+
150
+
151
+static const char* core_ps_doc[] = {
152
+	"Returns the description of running SER processes.",  /* Documentation string */
153
+	0                                                     /* Method signature(s) */
154
+};
155
+
156
+
157
+static void core_ps(rpc_t* rpc, void* c)
158
+{
159
+	int p;
160
+
161
+	for (p=0; p<process_count;p++) {
162
+		rpc->printf(c, "pid: %d", pt[p].pid);
163
+		rpc->printf(c, "desc: %s", pt[p].desc);
164
+	}
165
+}
166
+
167
+
168
+static const char* core_pwd_doc[] = {
169
+	"Returns the working directory of SER server.",    /* Documentation string */
170
+	0                                                  /* Method signature(s) */
171
+};
172
+
173
+
174
+static void core_pwd(rpc_t* rpc, void* c)
175
+{
176
+        char *cwd_buf;
177
+        int max_len;
178
+
179
+        max_len = pathmax();
180
+        cwd_buf = pkg_malloc(max_len);
181
+        if (!cwd_buf) {
182
+                ERR("core_pwd: No memory left\n");
183
+                rpc->fault(c, 500, "Server Ran Out of Memory");
184
+		return;
185
+        }
186
+
187
+        if (getcwd(cwd_buf, max_len)) {
188
+		rpc->add(c, "s", cwd_buf);
189
+        } else {
190
+		rpc->fault(c, 500, "getcwd Failed");
191
+        }
192
+        pkg_free(cwd_buf);
193
+}
194
+
195
+
196
+static const char* core_arg_doc[] = {
197
+	"Returns the list of command line arguments used on SER startup.",  /* Documentation string */
198
+	0                                                                   /* Method signature(s) */
199
+};
200
+
201
+
202
+static void core_arg(rpc_t* rpc, void* c)
203
+{
204
+        int p;
205
+
206
+        for (p = 0; p < my_argc; p++) {
207
+		if (rpc->add(c, "s", my_argv[p]) < 0) return;
208
+        }
209
+}
210
+
211
+
212
+static const char* core_kill_doc[] = {
213
+	"Sends the given signal to SER.",  /* Documentation string */
214
+	0                                  /* Method signature(s) */
215
+};
216
+
217
+
218
+static void core_kill(rpc_t* rpc, void* c)
219
+{
220
+	int sig_no;
221
+	if (rpc->scan(c, "d", &sig_no) < 0) return;
222
+	rpc->send(c);
223
+	kill(0, sig_no);
224
+}
225
+
226
+
227
+/* 
228
+ * RPC Methods exported by this module 
229
+ */
230
+rpc_export_t core_rpc_methods[] = {
231
+	{"system.listMethods",     system_listMethods,     system_listMethods_doc,     RET_ARRAY},
232
+	{"system.methodSignature", system_methodSignature, system_methodSignature_doc, RET_VALUE},
233
+	{"system.methodHelp",      system_methodHelp,      system_methodHelp_doc,      RET_VALUE},
234
+	{"core.prints",            core_prints,            core_prints_doc,            RET_VALUE},
235
+	{"core.version",           core_version,           core_version_doc,           RET_VALUE},
236
+	{"core.uptime",            core_uptime,            core_uptime_doc,            RET_ARRAY},
237
+	{"core.ps",                core_ps,                core_ps_doc,                RET_ARRAY},
238
+	{"core.pwd",               core_pwd,               core_pwd_doc,               RET_VALUE},
239
+	{"core.arg",               core_arg,               core_arg_doc,               RET_ARRAY},
240
+	{"core.kill",              core_kill,              core_kill_doc,              RET_VALUE},
241
+	{0, 0, 0, 0}
242
+};
243
+
244
+int rpc_init_time(void)
245
+{
246
+	char *t;
247
+	time(&up_since);
248
+	t=ctime(&up_since);
249
+	if (strlen(t)+1>=MAX_CTIME_LEN) {
250
+		ERR("Too long data %d\n", (int)strlen(t));
251
+		return -1;
252
+	}
253
+	memcpy(up_since_ctime,t,strlen(t)+1);
254
+	return 0;
255
+}
0 256
new file mode 100644
... ...
@@ -0,0 +1,37 @@
0
+/*
1
+ * $Id$
2
+ *
3
+ * Copyright (C) 2005 iptelorg GmbH
4
+ *
5
+ * This file is part of ser, a free SIP server.
6
+ *
7
+ * ser is free software; you can redistribute it and/or modify
8
+ * it under the terms of the GNU General Public License as published by
9
+ * the Free Software Foundation; either version 2 of the License, or
10
+ * (at your option) any later version
11
+ *
12
+ * For a license to use the ser software under conditions
13
+ * other than those described here, or to purchase support for this
14
+ * software, please contact iptel.org by e-mail at the following addresses:
15
+ *    info@iptel.org
16
+ *
17
+ * ser is distributed in the hope that it will be useful,
18
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
+ * GNU General Public License for more details.
21
+ *
22
+ * You should have received a copy of the GNU General Public License 
23
+ * along with this program; if not, write to the Free Software 
24
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
+ */
26
+
27
+#ifndef _CORE_CMD_H
28
+#define _CORE_CMD_H
29
+
30
+#include "rpc.h"
31
+
32
+extern rpc_export_t core_rpc_methods[];
33
+
34
+int rpc_init_time(void);
35
+
36
+#endif /* _CORE_CMD_H */