Browse code

modules/cfgutils: added some RPC commands

Juha Heinanen authored on 04/12/2014 06:12:20
Showing 4 changed files
... ...
@@ -22,14 +22,18 @@ Olle E. Johansson
22 22
 
23 23
    <oej@edvina.net>
24 24
 
25
+Juha Heinanen
26
+
27
+   <jh@tutpro.com>
28
+
25 29
 Edited by
26 30
 
27 31
 Daniel-Constantin Mierla
28 32
 
29 33
    <miconda@gmail.com>
30 34
 
31
-   Copyright � 2013, 2007, 2008, 2004 Edvina AB, 1und1 Internet AG, BASIS
32
-   AudioNet GmbH, Elena-Ramona Modroiu, FhG FOKUS
35
+   Copyright (c) 2014, 2013, 2007, 2008, 2004 Edvina AB, 1und1 Internet
36
+   AG, BASIS AudioNet GmbH, Elena-Ramona Modroiu, Juha Heinanen, FhG FOKUS
33 37
      __________________________________________________________________
34 38
 
35 39
    Table of Contents
... ...
@@ -79,9 +83,15 @@ Daniel-Constantin Mierla
79 83
               5.8. is_gflag
80 84
               5.9. get_gflags
81 85
 
82
-        6. Exported pseudo-variables
86
+        6. RPC Commands
87
+
88
+              6.1. cfgutils.set_gflag
89
+              6.2. cfgutils.reset_gflag
90
+              6.3. cfgutils.is_gflag
83 91
 
84
-              6.1. $RANDOM
92
+        7. Exported pseudo-variables
93
+
94
+              7.1. $RANDOM
85 95
 
86 96
    List of Examples
87 97
 
... ...
@@ -117,7 +127,10 @@ Daniel-Constantin Mierla
117 127
    1.30. reset_gflag usage
118 128
    1.31. is_gflag usage
119 129
    1.32. get_gflags usage
120
-   1.33. RANDOM pseudo-variable usage
130
+   1.33. cfgutils.set_gflag usage
131
+   1.34. cfgutils.reset_gflag usage
132
+   1.35. cfgutils.is_gflag usage
133
+   1.36. RANDOM pseudo-variable usage
121 134
 
122 135
 Chapter 1. Admin Guide
123 136
 
... ...
@@ -166,9 +179,15 @@ Chapter 1. Admin Guide
166 179
         5.8. is_gflag
167 180
         5.9. get_gflags
168 181
 
169
-   6. Exported pseudo-variables
182
+   6. RPC Commands
183
+
184
+        6.1. cfgutils.set_gflag
185
+        6.2. cfgutils.reset_gflag
186
+        6.3. cfgutils.is_gflag
170 187
 
171
-        6.1. $RANDOM
188
+   7. Exported pseudo-variables
189
+
190
+        7.1. $RANDOM
172 191
 
173 192
 1. Overview
174 193
 
... ...
@@ -218,8 +237,8 @@ Chapter 1. Admin Guide
218 237
    The size of bitmap is 32.
219 238
 
220 239
    The module exports external commands that can be used to change the
221
-   global flags via Management Interface. The MI commands are:
222
-   "set_gflag", "reset_gflag" and "is_gflag".
240
+   global flags via Management Interface and RPC. See relevant sections
241
+   below.
223 242
 
224 243
 2. Dependencies
225 244
 
... ...
@@ -338,7 +357,7 @@ rand_reset_prob();
338 357
 ...
339 358
 rand_get_prob();
340 359
 
341
-4.5. sleep(time)
360
+4.5.  sleep(time)
342 361
 
343 362
    Waits "time" seconds.
344 363
 
... ...
@@ -353,7 +372,7 @@ rand_get_prob();
353 372
 sleep("1");
354 373
 ...
355 374
 
356
-4.6. usleep(time)
375
+4.6.  usleep(time)
357 376
 
358 377
    Waits "time" micro-seconds.
359 378
 
... ...
@@ -369,7 +388,7 @@ sleep("1");
369 388
 usleep("5000");
370 389
 ...
371 390
 
372
-4.7. abort()
391
+4.7.  abort()
373 392
 
374 393
    Debugging function that aborts the server. Depending on the
375 394
    configuration of the server a core dump will be created.
... ...
@@ -382,7 +401,7 @@ usleep("5000");
382 401
 abort();
383 402
 ...
384 403
 
385
-4.8. pkg_status()
404
+4.8.  pkg_status()
386 405
 
387 406
    Debugging function that dumps the status for the private (PKG) memory.
388 407
    This information is logged to the default log facility, depending on
... ...
@@ -397,7 +416,7 @@ abort();
397 416
 pkg_status();
398 417
 ...
399 418
 
400
-4.9. pkg_summary()
419
+4.9.  pkg_summary()
401 420
 
402 421
    Debugging function that dumps the summary for the private (PKG) memory
403 422
    usage. This information is logged to the default log facility,
... ...
@@ -412,7 +431,7 @@ pkg_status();
412 431
 pkg_summary();
413 432
 ...
414 433
 
415
-4.10. shm_status()
434
+4.10.  shm_status()
416 435
 
417 436
    Debugging function that dumps the status for the shared (SHM) memory.
418 437
    This information is logged to the default log facility, depending on
... ...
@@ -427,7 +446,7 @@ pkg_summary();
427 446
 shm_status();
428 447
 ...
429 448
 
430
-4.11. shm_summary()
449
+4.11.  shm_summary()
431 450
 
432 451
    Debugging function that dumps the summary for the shared (SHM) memory
433 452
    usage. This information is logged to the default log facility,
... ...
@@ -613,7 +632,7 @@ $ kamctl fifo rand_set_prob 10
613 632
 
614 633
    This command don't need a parameter.
615 634
 
616
-   Example 1.25. rand_reset_prob usage
635
+   Example 1.25.  rand_reset_prob usage
617 636
 ...
618 637
 $ kamctl fifo rand_reset_prob
619 638
 ...
... ...
@@ -677,7 +696,7 @@ $ kamctl fifo set_gflag 0x3
677 696
    The parameter value must be a bitmask in decimal or hexadecimal format.
678 697
    The bitmask has a 32 bit size.
679 698
 
680
-   Example 1.30. reset_gflag usage
699
+   Example 1.30.  reset_gflag usage
681 700
 ...
682 701
 $ kamctl fifo reset_gflag 1
683 702
 $ kamctl fifo reset_gflag 0x3
... ...
@@ -716,22 +735,74 @@ TRUE
716 735
    Return the bitmap with all flags. The function gets no parameters and
717 736
    returns the bitmap in hexadecimal and decimal format.
718 737
 
719
-   Example 1.32. get_gflags usage
738
+   Example 1.32.  get_gflags usage
720 739
 ...
721 740
 $ kamctl fifo get_gflags
722 741
 0x3039
723 742
 12345
724 743
 ...
725 744
 
726
-6. Exported pseudo-variables
745
+6. RPC Commands
746
+
747
+   6.1. cfgutils.set_gflag
748
+   6.2. cfgutils.reset_gflag
749
+   6.3. cfgutils.is_gflag
750
+
751
+6.1. cfgutils.set_gflag
752
+
753
+   Set the value of some flags (specified by bitmask) to 1.
754
+
755
+   The parameter value must be a bitmask in decimal or hexadecimal format.
756
+   The bitmask has a 32 bit size.
757
+
758
+   Example 1.33. cfgutils.set_gflag usage
759
+...
760
+$ kamctl cfgutils.set_gflag s:1
761
+...
762
+
763
+6.2. cfgutils.reset_gflag
764
+
765
+   Reset the value of some flags to 0.
766
+
767
+   Reset the value of some flags (specified by bitmask) to 1.
768
+
769
+   The parameter value must be a bitmask in decimal or hexadecimal format.
770
+   The bitmask has a 32 bit size.
771
+
772
+   Example 1.34.  cfgutils.reset_gflag usage
773
+...
774
+$ kamctl cfgutils.reset_gflag s:0x1
775
+...
776
+
777
+6.3. cfgutils.is_gflag
778
+
779
+   Returns string "TRUE" if all the flags from the given set are set and
780
+   "FALSE" if at least one is not set.
781
+
782
+   The parameter value must be a bitmask in decimal or hexadecimal format.
783
+   The bitmask has a 32 bit size.
784
+
785
+   Example 1.35. cfgutils.is_gflag usage
786
+...
787
+$ kamctl cfgutils.set_gflag s:1024
788
+$ kamctl cfgutils.is_gflag s:1024
789
+TRUE
790
+$ kamctl cfgutils.is_gflag s:1023
791
+FALSE
792
+$ kamctl cfgutils.set_gflag s:0x7FFF
793
+$ kamctl cfgutils.is_gflag s:1023
794
+TRUE
795
+...
796
+
797
+7. Exported pseudo-variables
727 798
 
728
-   6.1. $RANDOM
799
+   7.1. $RANDOM
729 800
 
730
-6.1. $RANDOM
801
+7.1. $RANDOM
731 802
 
732 803
    Returns a random value from the [0 - 2^31) range.
733 804
 
734
-   Example 1.33. RANDOM pseudo-variable usage
805
+   Example 1.36. RANDOM pseudo-variable usage
735 806
 ...
736 807
 if (rand_event()) {
737 808
   $avp(i:10) = ($RANDOM / 16777216); # 2^24
... ...
@@ -72,6 +72,7 @@
72 72
 #include "../../hashes.h"
73 73
 #include "../../locking.h"
74 74
 #include "../../route.h"
75
+#include "../../rpc_lookup.h"
75 76
 #include <stdio.h>
76 77
 #include <stdlib.h>
77 78
 #include <sys/types.h>
... ...
@@ -215,6 +216,106 @@ static mi_export_t mi_cmds[] = {
215 216
 };
216 217
 
217 218
 
219
+static void rpc_is_gflag(rpc_t* rpc, void* c)
220
+{
221
+	str flag_str;
222
+	unsigned int flag;
223
+	
224
+	if (rpc->scan(c, "S", &flag_str) != 1) {
225
+		rpc->fault(c, 400, "flag parameter error");
226
+		return;
227
+	}
228
+
229
+	flag = 0;
230
+	if ((strno2int(&flag_str, &flag) < 0) || !flag) {
231
+		rpc->fault(c, 400, "incorrect flag parameter value");
232
+		return;
233
+	}
234
+
235
+	if (((*gflags) & flag) == flag)
236
+		rpc->add(c, "s", "TRUE");
237
+	else
238
+		rpc->add(c, "s", "FALSE");
239
+
240
+	return;
241
+}
242
+
243
+
244
+static void rpc_set_gflag(rpc_t* rpc, void* c)
245
+{
246
+	str flag_str;
247
+	unsigned int flag;
248
+	
249
+	if (rpc->scan(c, "S", &flag_str) != 1) {
250
+		rpc->fault(c, 400, "flag parameter error");
251
+		return;
252
+	}
253
+
254
+	flag = 0;
255
+	if ((strno2int(&flag_str, &flag) < 0) || !flag) {
256
+		rpc->fault(c, 400, "incorrect flag parameter value '%.*s'",
257
+			   flag_str.len, flag_str.s);
258
+		return;
259
+	}
260
+
261
+	lock_get(gflags_lock);
262
+	(*gflags) |= flag;
263
+	lock_release(gflags_lock);
264
+	
265
+	return;
266
+}
267
+
268
+
269
+static void rpc_reset_gflag(rpc_t* rpc, void* c)
270
+{
271
+	str flag_str;
272
+	unsigned int flag;
273
+	
274
+	if (rpc->scan(c, "S", &flag_str) != 1) {
275
+		rpc->fault(c, 400, "flag parameter error");
276
+		return;
277
+	}
278
+
279
+	flag = 0;
280
+	if ((strno2int(&flag_str, &flag) < 0) || !flag) {
281
+		rpc->fault(c, 400, "incorrect flag parameter value");
282
+		return;
283
+	}
284
+
285
+	lock_get(gflags_lock);
286
+	(*gflags) &= ~ flag;
287
+	lock_release(gflags_lock);
288
+	
289
+	return;
290
+}
291
+
292
+
293
+static const char* is_gflag_doc[2] = {
294
+	"Checks if the bits specified by the argument are all set.",
295
+	0
296
+};
297
+
298
+
299
+static const char* set_gflag_doc[2] = {
300
+	"Sets the bits specified by the argument.",
301
+	0
302
+};
303
+
304
+
305
+static const char* reset_gflag_doc[2] = {
306
+	"Resets the bits specified by the argument.",
307
+	0
308
+};
309
+
310
+
311
+static rpc_export_t rpc_cmds[] = {
312
+	{"cfgutils.is_gflag", rpc_is_gflag, is_gflag_doc, 0},
313
+	{"cfgutils.set_gflag", rpc_set_gflag, set_gflag_doc, 0},
314
+	{"cfgutils.reset_gflag", rpc_reset_gflag, reset_gflag_doc, 0},
315
+	{0, 0, 0, 0}
316
+};
317
+
318
+
218 319
 static pv_export_t mod_items[] = {
219 320
 	{ {"RANDOM", sizeof("RANDOM")-1}, PVT_OTHER, pv_get_random_val, 0,
220 321
 		0, 0, 0, 0 },
... ...
@@ -774,6 +875,12 @@ static int route_exists(struct sip_msg *msg, char *route)
774 875
 
775 876
 static int mod_init(void)
776 877
 {
878
+	/* Register RPC commands */
879
+	if (rpc_register_array(rpc_cmds)!=0) {
880
+		LM_ERR("failed to register RPC commands\n");
881
+		return -1;
882
+	}
883
+
777 884
 	if(register_mi_mod(exports.name, mi_cmds)!=0)
778 885
 	{
779 886
 		LM_ERR("failed to register MI commands\n");
... ...
@@ -42,6 +42,11 @@
42 42
 		<surname>Johansson</surname>
43 43
 		<email>oej@edvina.net</email>
44 44
 		</author>
45
+		<author>
46
+		<firstname>Juha</firstname>
47
+		<surname>Heinanen</surname>
48
+		<email>jh@tutpro.com</email>
49
+		</author>
45 50
 		<editor>
46 51
 		<firstname>Daniel-Constantin</firstname>
47 52
 		<surname>Mierla</surname>
... ...
@@ -49,6 +54,7 @@
49 54
 		</editor>
50 55
 	</authorgroup>
51 56
 	<copyright>
57
+		<year>2014</year>
52 58
 		<year>2013</year>
53 59
 		<year>2007</year>
54 60
 		<year>2008</year>
... ...
@@ -57,6 +63,7 @@
57 63
 		<holder>1und1 Internet AG</holder>
58 64
 		<holder>BASIS AudioNet GmbH</holder>
59 65
 		<holder>Elena-Ramona Modroiu</holder>
66
+		<holder>Juha Heinanen</holder>
60 67
 		<holder>&fhg;</holder>
61 68
 	</copyright>
62 69
 	</bookinfo>
... ...
@@ -73,9 +73,8 @@
73 73
 	</para>
74 74
 	<para>
75 75
 	The module exports external commands that can be used to change
76
-	the global flags via Management Interface. The MI commands are:
77
-	<quote>set_gflag</quote>, <quote>reset_gflag</quote> and
78
-	<quote>is_gflag</quote>.
76
+	the global flags via Management Interface and RPC. See relevant
77
+	sections below.
79 78
 	</para>
80 79
 	</section>
81 80
 	<section>
... ...
@@ -803,6 +802,78 @@ $ &ctltool; fifo get_gflags
803 802
 0x3039
804 803
 12345
805 804
 ...
805
+</programlisting>
806
+			</example>
807
+		</section>
808
+</section>
809
+	<section>
810
+		<title><acronym>RPC</acronym> Commands</title>
811
+		<section  id="cfgutils.rpc.set_gflag">
812
+			<title><function moreinfo="none">cfgutils.set_gflag</function></title>
813
+			<para>
814
+			Set the value of some flags (specified by bitmask) to 1.
815
+			</para>
816
+			<para>
817
+			The parameter value must be a bitmask in decimal or
818
+			hexadecimal format.  The bitmask has a 32 bit size.
819
+			</para>
820
+			<example>
821
+			<title><function moreinfo="none">cfgutils.set_gflag</function> usage</title>
822
+			<programlisting format="linespecific">
823
+...
824
+$ &ctltool; cfgutils.set_gflag s:1
825
+...
826
+</programlisting>
827
+			</example>
828
+
829
+		</section>
830
+		<section  id="cfgutils.rpc.reset_gflag">
831
+			<title><function moreinfo="none">cfgutils.reset_gflag</function></title>
832
+			<para>
833
+			Reset the value of some flags to 0.
834
+			</para>
835
+			<para>
836
+			Reset the value of some flags (specified by bitmask)
837
+			to 1.
838
+			</para>
839
+			<para>
840
+			The parameter value must be a bitmask in decimal or
841
+			hexadecimal format.  The bitmask has a 32 bit size.
842
+			</para>
843
+			<example>
844
+			<title>
845
+				<function moreinfo="none">cfgutils.reset_gflag</function> usage</title>
846
+			<programlisting format="linespecific">
847
+...
848
+$ &ctltool; cfgutils.reset_gflag s:0x1
849
+...
850
+</programlisting>
851
+			</example>
852
+		</section>
853
+		<section  id="cfgutils.rpc.is_gflag">
854
+			<title><function moreinfo="none">cfgutils.is_gflag</function></title>
855
+			<para>
856
+			Returns string "TRUE" if all the
857
+			flags from the given set are set
858
+			and "FALSE" if at least one is not set.
859
+			</para>
860
+			<para>
861
+			The parameter value must be a bitmask in decimal or
862
+			hexadecimal format.  The bitmask has a 32 bit size.
863
+			</para>
864
+			<example>
865
+			<title><function moreinfo="none">cfgutils.is_gflag</function> usage</title>
866
+			<programlisting format="linespecific">
867
+...
868
+$ &ctltool; cfgutils.set_gflag s:1024
869
+$ &ctltool; cfgutils.is_gflag s:1024
870
+TRUE
871
+$ &ctltool; cfgutils.is_gflag s:1023
872
+FALSE
873
+$ &ctltool; cfgutils.set_gflag s:0x7FFF
874
+$ &ctltool; cfgutils.is_gflag s:1023
875
+TRUE
876
+...
806 877
 </programlisting>
807 878
 			</example>
808 879
 		</section>