forked from OpenSIPS/opensips
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmi_core.c
More file actions
348 lines (277 loc) · 6.87 KB
/
Copy pathmi_core.c
File metadata and controls
348 lines (277 loc) · 6.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
* $Id$
*
* Copyright (C) 2006 Voice Sistem SRL
*
* This file is part of opensips, a free SIP server.
*
* opensips is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* opensips is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*
* history:
* ---------
* 2006-09-08 first version (bogdan)
*/
/*!
* \file
* \brief MI :: Core
* \ingroup mi
*/
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <signal.h>
#include "../dprint.h"
#include "../globals.h"
#include "../ut.h"
#include "../pt.h"
#include "../mem/mem.h"
#include "mi.h"
static time_t up_since;
static str up_since_ctime;
static int init_mi_uptime(void)
{
char *p;
time(&up_since);
p = ctime(&up_since);
up_since_ctime.len = strlen(p)-1;
up_since_ctime.s = (char*)pkg_malloc(up_since_ctime.len);
if (up_since_ctime.s==0) {
LM_ERR("no more pkg mem\n");
return -1;
}
memcpy(up_since_ctime.s, p , up_since_ctime.len);
return 0;
}
static struct mi_root *mi_uptime(struct mi_root *cmd, void *param)
{
struct mi_root *rpl_tree;
struct mi_node *rpl;
struct mi_node *node;
time_t now;
char *p;
rpl_tree = init_mi_tree( 200, MI_SSTR(MI_OK));
if (rpl_tree==0)
return 0;
rpl = &rpl_tree->node;
time(&now);
p = ctime(&now);
node = add_mi_node_child( rpl, MI_DUP_VALUE, MI_SSTR("Now"),
p, strlen(p)-1);
if (node==0)
goto error;
node = add_mi_node_child( rpl, 0, MI_SSTR("Up since"),
up_since_ctime.s, up_since_ctime.len);
if (node==0)
goto error;
node = addf_mi_node_child( rpl, 0, MI_SSTR("Up time"),
"%lu [sec]", (unsigned long)difftime(now, up_since) );
if (node==0)
goto error;
return rpl_tree;
error:
LM_ERR("failed to add node\n");
free_mi_tree(rpl_tree);
return 0;
}
static struct mi_root *mi_version(struct mi_root *cmd, void *param)
{
struct mi_root *rpl_tree;
struct mi_node *rpl;
struct mi_node *node;
rpl_tree = init_mi_tree( 200, MI_SSTR(MI_OK));
if (rpl_tree==0)
return 0;
rpl = &rpl_tree->node;
node = add_mi_node_child( rpl, 0, MI_SSTR("Server"), SERVER_HDR+8,
SERVER_HDR_LEN-8);
if (node==0) {
LM_ERR("failed to add node\n");
free_mi_tree(rpl_tree);
return 0;
}
return rpl_tree;
}
static struct mi_root *mi_pwd(struct mi_root *cmd, void *param)
{
static int max_len = 0;
static char *cwd_buf = 0;
struct mi_root *rpl_tree;
struct mi_node *rpl;
struct mi_node *node;
if (cwd_buf==NULL) {
max_len = pathmax();
cwd_buf = pkg_malloc(max_len);
if (cwd_buf==NULL) {
LM_ERR("no more pkg mem\n");
return 0;
}
}
rpl_tree = init_mi_tree( 200, MI_SSTR(MI_OK));
if (rpl_tree==0)
return 0;
rpl = &rpl_tree->node;
if (getcwd(cwd_buf, max_len)==0) {
LM_ERR("getcwd failed = %s\n",strerror(errno));
goto error;
}
node = add_mi_node_child( rpl, 0, MI_SSTR("WD"), cwd_buf,strlen(cwd_buf));
if (node==0) {
LM_ERR("failed to add node\n");
goto error;
}
return rpl_tree;
error:
free_mi_tree(rpl_tree);
return 0;
}
static struct mi_root *mi_arg(struct mi_root *cmd, void *param)
{
struct mi_root *rpl_tree;
struct mi_node *rpl;
struct mi_node *node;
int n;
rpl_tree = init_mi_tree( 200, MI_SSTR(MI_OK));
if (rpl_tree==0)
return 0;
rpl = &rpl_tree->node;
for ( n=0; n<my_argc ; n++ ) {
node = add_mi_node_child(rpl, 0, 0, 0, my_argv[n], strlen(my_argv[n]));
if (node==0) {
LM_ERR("failed to add node\n");
free_mi_tree(rpl_tree);
return 0;
}
}
return rpl_tree;
}
static struct mi_root *mi_which(struct mi_root *cmd, void *param)
{
struct mi_root *rpl_tree;
struct mi_cmd *cmds;
struct mi_node *rpl;
struct mi_node *node;
int size;
int i;
rpl_tree = init_mi_tree( 200, MI_SSTR(MI_OK));
if (rpl_tree==0)
return 0;
rpl = &rpl_tree->node;
get_mi_cmds( &cmds, &size);
for ( i=0 ; i<size ; i++ ) {
node = add_mi_node_child( rpl, 0, 0, 0, cmds[i].name.s,
cmds[i].name.len);
if (node==0) {
LM_ERR("failed to add node\n");
free_mi_tree(rpl_tree);
return 0;
}
}
return rpl_tree;
}
static struct mi_root *mi_ps(struct mi_root *cmd, void *param)
{
struct mi_root *rpl_tree;
struct mi_node *rpl;
struct mi_node *node;
struct mi_attr *attr;
char *p;
int len;
int i;
rpl_tree = init_mi_tree( 200, MI_SSTR(MI_OK));
if (rpl_tree==0)
return 0;
rpl = &rpl_tree->node;
for ( i=0 ; i<counted_processes ; i++ ) {
node = add_mi_node_child(rpl, 0, MI_SSTR("Process"), 0, 0 );
if (node==0)
goto error;
p = int2str((unsigned long)i, &len);
attr = add_mi_attr( node, MI_DUP_VALUE, MI_SSTR("ID"), p, len);
if (attr==0)
goto error;
p = int2str((unsigned long)pt[i].pid, &len);
attr = add_mi_attr( node, MI_DUP_VALUE, MI_SSTR("PID"), p, len);
if (attr==0)
goto error;
attr = add_mi_attr( node, 0, MI_SSTR("Type"),
pt[i].desc, strlen(pt[i].desc));
if (attr==0)
goto error;
}
return rpl_tree;
error:
LM_ERR("failed to add node\n");
free_mi_tree(rpl_tree);
return 0;
}
static struct mi_root *mi_kill(struct mi_root *cmd, void *param)
{
kill(0, SIGTERM);
return 0;
}
static struct mi_root *mi_debug(struct mi_root *cmd, void *param)
{
struct mi_root *rpl_tree;
struct mi_node *node;
char *p;
int len;
int new_debug;
#ifdef CHANGEABLE_DEBUG_LEVEL
node = cmd->node.kids;
if (node!=NULL) {
if (str2sint( &node->value, &new_debug) < 0)
return init_mi_tree( 400, MI_SSTR(MI_BAD_PARM));
} else
new_debug = *debug;
#else
new_debug = debug;
#endif
rpl_tree = init_mi_tree( 200, MI_SSTR(MI_OK));
if (rpl_tree==0)
return 0;
p = sint2str((long)new_debug, &len);
node = add_mi_node_child( &rpl_tree->node, MI_DUP_VALUE,
MI_SSTR("DEBUG"),p, len);
if (node==0) {
free_mi_tree(rpl_tree);
return 0;
}
#ifdef CHANGEABLE_DEBUG_LEVEL
*debug = new_debug;
#endif
return rpl_tree;
}
static mi_export_t mi_core_cmds[] = {
{ "uptime", mi_uptime, MI_NO_INPUT_FLAG, 0, init_mi_uptime },
{ "version", mi_version, MI_NO_INPUT_FLAG, 0, 0 },
{ "pwd", mi_pwd, MI_NO_INPUT_FLAG, 0, 0 },
{ "arg", mi_arg, MI_NO_INPUT_FLAG, 0, 0 },
{ "which", mi_which, MI_NO_INPUT_FLAG, 0, 0 },
{ "ps", mi_ps, MI_NO_INPUT_FLAG, 0, 0 },
{ "kill", mi_kill, MI_NO_INPUT_FLAG, 0, 0 },
{ "debug", mi_debug, 0, 0, 0 },
{ 0, 0, 0, 0, 0}
};
int init_mi_core(void)
{
if (register_mi_mod( "core", mi_core_cmds)<0) {
LM_ERR("unable to register core MI cmds\n");
return -1;
}
return 0;
}