forked from kamailio/kamailio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcfg_struct.c
1314 lines (1134 loc) · 35.9 KB
/
cfg_struct.c
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2007 iptelorg GmbH
*
* This file is part of Kamailio, a free SIP server.
*
* Kamailio 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
*
* Kamailio 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <string.h>
#include "../mem/mem.h"
#include "../mem/shm_mem.h"
#include "../ut.h"
#include "../locking.h"
#include "../bit_scan.h"
#include "cfg_ctx.h"
#include "cfg_script.h"
#include "cfg_select.h"
#include "cfg_struct.h"
cfg_group_t *cfg_group = NULL; /* linked list of registered cfg groups */
cfg_block_t **cfg_global = NULL; /* pointer to the active cfg block */
cfg_block_t *cfg_local = NULL; /* per-process pointer to the active cfg block.
Updated only when the child process
finishes working on the SIP message */
int cfg_block_size = 0; /* size of the cfg block including the meta-data (constant) */
gen_lock_t *cfg_global_lock = 0; /* protects *cfg_global */
gen_lock_t *cfg_writer_lock = 0; /* This lock makes sure that two processes do not
try to clone *cfg_global at the same time.
Never try to get cfg_writer_lock when
cfg_global_lock is held */
int cfg_shmized = 0; /* indicates whether the cfg block has been
already shmized */
cfg_child_cb_t **cfg_child_cb_first = NULL; /* first item of the per-child process
callback list */
cfg_child_cb_t **cfg_child_cb_last = NULL; /* last item of the above list */
cfg_child_cb_t *cfg_child_cb = NULL; /* pointer to the previously executed cb */
int cfg_ginst_count = 0; /* number of group instances set within the child process */
/* forward declarations */
static void del_add_var_list(cfg_group_t *group);
static int apply_add_var_list(cfg_block_t *block, cfg_group_t *group);
/* creates a new cfg group, and adds it to the linked list */
cfg_group_t *cfg_new_group(char *name, int name_len,
int num, cfg_mapping_t *mapping,
char *vars, int size, void **handle)
{
cfg_group_t *group;
if (cfg_shmized) {
LOG(L_ERR, "ERROR: cfg_new_group(): too late config declaration\n");
return NULL;
}
if (num > CFG_MAX_VAR_NUM) {
LOG(L_ERR, "ERROR: cfg_new_group(): too many variables (%d) within a single group,"
" the limit is %d. Increase CFG_MAX_VAR_NUM, or split the group into multiple"
" definitions.\n",
num, CFG_MAX_VAR_NUM);
return NULL;
}
group = (cfg_group_t *)pkg_malloc(sizeof(cfg_group_t)+name_len-1);
if (!group) {
LOG(L_ERR, "ERROR: cfg_new_group(): not enough memory\n");
return NULL;
}
memset(group, 0, sizeof(cfg_group_t)+name_len-1);
group->num = num;
group->mapping = mapping;
group->vars = vars;
group->size = size;
group->handle = handle;
if (handle)
group->orig_handle = *handle;
group->name_len = name_len;
memcpy(&group->name, name, name_len);
/* add the new group to the beginning of the list */
group->next = cfg_group;
cfg_group = group;
return group;
}
/* Set the values of an existing cfg group. */
void cfg_set_group(cfg_group_t *group,
int num, cfg_mapping_t *mapping,
char *vars, int size, void **handle)
{
group->num = num;
group->mapping = mapping;
group->vars = vars;
group->size = size;
group->handle = handle;
if (handle)
group->orig_handle = *handle;
}
/* clones a string to shared memory
* (src and dst can be the same)
*/
int cfg_clone_str(str *src, str *dst)
{
char *c;
if (!src->s) {
dst->s = NULL;
dst->len = 0;
return 0;
}
c = (char *)shm_malloc(sizeof(char)*(src->len+1));
if (!c) {
LOG(L_ERR, "ERROR: cfg_clone_str(): not enough shm memory\n");
return -1;
}
memcpy(c, src->s, src->len);
c[src->len] = '\0';
dst->s = c;
dst->len = src->len;
return 0;
}
/* copies the strings to shared memory */
static int cfg_shmize_strings(cfg_group_t *group)
{
cfg_mapping_t *mapping;
int i;
str s;
/* We do not know in advance whether the variable will be changed or not,
and it can happen that we try to free the shm memory area when the variable
is changed, hence, it must be already in shm mem */
mapping = group->mapping;
for (i=0; i<group->num; i++) {
/* the cfg driver module may have already shmized the variable */
if (mapping[i].flag & cfg_var_shmized) continue;
if (CFG_VAR_TYPE(&mapping[i]) == CFG_VAR_STRING) {
s.s = *(char **)(group->vars + mapping[i].offset);
if (!s.s) continue;
s.len = strlen(s.s);
} else if (CFG_VAR_TYPE(&mapping[i]) == CFG_VAR_STR) {
memcpy(&s, group->vars + mapping[i].offset, sizeof(str));
if (!s.s) continue;
} else {
continue;
}
if (cfg_clone_str(&s, &s)) return -1;
*(char **)(group->vars + mapping[i].offset) = s.s;
mapping[i].flag |= cfg_var_shmized;
}
return 0;
}
/* copy the variables to shm mem */
int cfg_shmize(void)
{
cfg_group_t *group;
cfg_block_t *block = NULL;
int size;
if (!cfg_group) return 0;
/* Let us allocate one memory block that
* will contain all the variables + meta-data
* in the following form:
* |-----------|
* | meta-data | <- group A: meta_offset
* | variables | <- group A: var_offset
* |-----------|
* | meta-data | <- group B: meta_offset
* | variables | <- group B: var_offset
* |-----------|
* | ... |
* |-----------|
*
* The additional array for the multiple values
* of the same variable is linked to the meta-data.
*/
for ( size=0, group = cfg_group;
group;
group=group->next
) {
size = ROUND_POINTER(size);
group->meta_offset = size;
size += sizeof(cfg_group_meta_t);
size = ROUND_POINTER(size);
group->var_offset = size;
size += group->size;
}
block = (cfg_block_t*)shm_malloc(sizeof(cfg_block_t)+size-1);
if (!block) {
LOG(L_ERR, "ERROR: cfg_shmize(): not enough shm memory\n");
goto error;
}
memset(block, 0, sizeof(cfg_block_t)+size-1);
cfg_block_size = size;
/* copy the memory fragments to the single block */
for ( group = cfg_group;
group;
group=group->next
) {
if (group->dynamic == CFG_GROUP_STATIC) {
/* clone the strings to shm mem */
if (cfg_shmize_strings(group)) goto error;
/* copy the values to the new block */
memcpy(CFG_GROUP_DATA(block, group), group->vars, group->size);
} else if (group->dynamic == CFG_GROUP_DYNAMIC) {
/* The group was declared with NULL values,
* we have to fix it up.
* The fixup function takes care about the values,
* it fills up the block */
if (cfg_script_fixup(group, CFG_GROUP_DATA(block, group))) goto error;
/* Notify the drivers about the new config definition.
* Temporary set the group handle so that the drivers have a chance to
* overwrite the default values. The handle must be reset after this
* because the main process does not have a local configuration. */
*(group->handle) = CFG_GROUP_DATA(block, group);
cfg_notify_drivers(group->name, group->name_len,
group->mapping->def);
*(group->handle) = NULL;
} else {
LOG(L_ERR, "ERROR: cfg_shmize(): Configuration group is declared "
"without any variable: %.*s\n",
group->name_len, group->name);
goto error;
}
/* Create the additional group instances with applying
the temporary list. */
if (apply_add_var_list(block, group))
goto error;
}
/* try to fixup the selects that failed to be fixed-up previously */
if (cfg_fixup_selects()) goto error;
/* install the new config */
cfg_install_global(block, NULL, NULL, NULL);
cfg_shmized = 1;
return 0;
error:
if (block) shm_free(block);
return -1;
}
/* deallocate the list of groups, and the shmized strings */
static void cfg_destory_groups(unsigned char *block)
{
cfg_group_t *group, *group2;
cfg_mapping_t *mapping;
cfg_def_t *def;
void *old_string;
int i;
group = cfg_group;
while(group) {
mapping = group->mapping;
def = mapping ? mapping->def : NULL;
/* destory the shmized strings in the block */
if (block && def)
for (i=0; i<group->num; i++)
if (((CFG_VAR_TYPE(&mapping[i]) == CFG_VAR_STRING) ||
(CFG_VAR_TYPE(&mapping[i]) == CFG_VAR_STR)) &&
mapping[i].flag & cfg_var_shmized) {
old_string = *(char **)(block + group->var_offset + mapping[i].offset);
if (old_string) shm_free(old_string);
}
if (group->dynamic == CFG_GROUP_DYNAMIC) {
/* the group was dynamically allocated */
cfg_script_destroy(group);
} else {
/* only the mapping was allocated, all the other
pointers are just set to static variables */
if (mapping) pkg_free(mapping);
}
/* Delete the additional variable list */
del_add_var_list(group);
group2 = group->next;
pkg_free(group);
group = group2;
}
}
/* initiate the cfg framework */
int sr_cfg_init(void)
{
cfg_global_lock = lock_alloc();
if (!cfg_global_lock) {
LOG(L_ERR, "ERROR: sr_cfg_init(): not enough shm memory\n");
goto error;
}
if (lock_init(cfg_global_lock) == 0) {
LOG(L_ERR, "ERROR: sr_cfg_init(): failed to init lock\n");
lock_dealloc(cfg_global_lock);
cfg_global_lock = 0;
goto error;
}
cfg_writer_lock = lock_alloc();
if (!cfg_writer_lock) {
LOG(L_ERR, "ERROR: sr_cfg_init(): not enough shm memory\n");
goto error;
}
if (lock_init(cfg_writer_lock) == 0) {
LOG(L_ERR, "ERROR: sr_cfg_init(): failed to init lock\n");
lock_dealloc(cfg_writer_lock);
cfg_writer_lock = 0;
goto error;
}
cfg_global = (cfg_block_t **)shm_malloc(sizeof(cfg_block_t *));
if (!cfg_global) {
LOG(L_ERR, "ERROR: sr_cfg_init(): not enough shm memory\n");
goto error;
}
*cfg_global = NULL;
cfg_child_cb_first = (cfg_child_cb_t **)shm_malloc(sizeof(cfg_child_cb_t *));
if (!cfg_child_cb_first) {
LOG(L_ERR, "ERROR: sr_cfg_init(): not enough shm memory\n");
goto error;
}
*cfg_child_cb_first = NULL;
cfg_child_cb_last = (cfg_child_cb_t **)shm_malloc(sizeof(cfg_child_cb_t *));
if (!cfg_child_cb_last) {
LOG(L_ERR, "ERROR: sr_cfg_init(): not enough shm memory\n");
goto error;
}
*cfg_child_cb_last = NULL;
/* A new cfg_child_cb struct must be created with a NULL callback function.
This stucture will be the entry point for the child processes, and
will be freed later, when none of the processes refers to it */
*cfg_child_cb_first = *cfg_child_cb_last =
cfg_child_cb_new(NULL, NULL, NULL, 0);
if (!*cfg_child_cb_first) goto error;
return 0;
error:
cfg_destroy();
return -1;
}
/* destroy the memory allocated for the cfg framework */
void cfg_destroy(void)
{
/* free the contexts */
cfg_ctx_destroy();
/* free the list of groups */
cfg_destory_groups((cfg_global && (*cfg_global)) ? (*cfg_global)->vars : NULL);
/* free the select list */
cfg_free_selects();
if (cfg_child_cb_first) {
if (*cfg_child_cb_first) cfg_child_cb_free_list(*cfg_child_cb_first);
shm_free(cfg_child_cb_first);
cfg_child_cb_first = NULL;
}
if (cfg_child_cb_last) {
shm_free(cfg_child_cb_last);
cfg_child_cb_last = NULL;
}
if (cfg_global) {
if (*cfg_global) cfg_block_free(*cfg_global);
shm_free(cfg_global);
cfg_global = NULL;
}
if (cfg_global_lock) {
lock_destroy(cfg_global_lock);
lock_dealloc(cfg_global_lock);
cfg_global_lock = 0;
}
if (cfg_writer_lock) {
lock_destroy(cfg_writer_lock);
lock_dealloc(cfg_writer_lock);
cfg_writer_lock = 0;
}
}
/* Register num number of child processes that will
* keep updating their local configuration.
* This function needs to be called from mod_init
* before any child process is forked.
*/
void cfg_register_child(int num)
{
/* Increase the reference counter of the first list item
* with the number of child processes.
* If the counter was increased after forking then it
* could happen that a child process is forked and updates
* its local config very fast before the other processes have
* a chance to refer to the list item. The result is that the
* item is freed by the "fast" child process and the other
* processes do not see the beginning of the list and miss
* some config changes.
*/
atomic_add(&((*cfg_child_cb_first)->refcnt), num);
}
/* per-child process init function.
* It needs to be called from the forked process.
* cfg_register_child() must be called before this function!
*/
int cfg_child_init(void)
{
/* set the callback list pointer to the beginning of the list */
cfg_child_cb = *cfg_child_cb_first;
return 0;
}
/* Child process init function that can be called
* without cfg_register_child().
* Note that the child process may miss some configuration changes.
*/
int cfg_late_child_init(void)
{
/* set the callback list pointer to the beginning of the list */
CFG_LOCK();
atomic_inc(&((*cfg_child_cb_first)->refcnt));
cfg_child_cb = *cfg_child_cb_first;
CFG_UNLOCK();
return 0;
}
/* per-child init function for non-cb executing processes.
* Mark this process as not wanting to execute any per-child config
* callback (it will have only limited config functionality, but is useful
* when a process needs only to watch some non-callback cfg. values,
* e.g. the main attendant process, debug and memlog).
* It needs to be called from the forked process.
* cfg_register_child must _not_ be called.
*/
int cfg_child_no_cb_init(void)
{
/* set the callback list pointer to the beginning of the list */
cfg_child_cb = CFG_NO_CHILD_CBS;
return 0;
}
/* per-child process destroy function
* Should be called only when the child process exits,
* but SER continues running
*
* WARNING: this function call must be the very last action
* before the child process exits, because the local config
* is not available afterwards.
*/
void cfg_child_destroy(void)
{
cfg_child_cb_t *prev_cb;
/* unref the local config */
if (cfg_local) {
CFG_UNREF(cfg_local);
cfg_local = NULL;
}
if (!cfg_child_cb || cfg_child_cb==CFG_NO_CHILD_CBS) return;
/* The lock must be held to make sure that the global config
is not replaced meantime, and the other child processes do not
leave the old value of *cfg_child_cb_last. Otherwise it could happen,
that all the other processes move their own cfg_child_cb pointer before
this process reaches *cfg_child_cb_last, though, it is very unlikely. */
CFG_LOCK();
/* go through the list and check whether there is any item that
has to be freed (similar to cfg_update_local(), but without executing
the callback functions) */
while (cfg_child_cb != *cfg_child_cb_last) {
prev_cb = cfg_child_cb;
cfg_child_cb = cfg_child_cb->next;
atomic_inc(&cfg_child_cb->refcnt);
if (atomic_dec_and_test(&prev_cb->refcnt)) {
/* No more pocess refers to this callback.
Did this process block the deletion,
or is there any other process that has not
reached prev_cb yet? */
if (*cfg_child_cb_first == prev_cb) {
/* yes, this process was blocking the deletion */
*cfg_child_cb_first = cfg_child_cb;
cfg_child_cb_free_item(prev_cb);
}
} else {
/* no need to continue, because there is at least
one process that stays exactly at the same point
in the list, so it will free the items later */
break;
}
}
atomic_dec(&cfg_child_cb->refcnt);
CFG_UNLOCK();
cfg_child_cb = NULL;
}
/* searches a group by name */
cfg_group_t *cfg_lookup_group(char *name, int len)
{
cfg_group_t *g;
for ( g = cfg_group;
g;
g = g->next
)
if ((g->name_len == len)
&& (memcmp(g->name, name, len)==0))
return g;
return NULL;
}
/* searches a variable definition by group and variable name */
int cfg_lookup_var(str *gname, str *vname,
cfg_group_t **group, cfg_mapping_t **var)
{
cfg_group_t *g;
int i;
for ( g = cfg_group;
g;
g = g->next
)
if ((g->name_len == gname->len)
&& (memcmp(g->name, gname->s, gname->len)==0)) {
if (!g->mapping) return -1; /* dynamic group is not ready */
for ( i = 0;
i < g->num;
i++
) {
if ((g->mapping[i].name_len == vname->len)
&& (memcmp(g->mapping[i].def->name, vname->s, vname->len)==0)) {
if (group) *group = g;
if (var) *var = &(g->mapping[i]);
return 0;
}
}
break;
}
LOG(L_DBG, "DEBUG: cfg_lookup_var(): variable not found: %.*s.%.*s\n",
gname->len, gname->s,
vname->len, vname->s);
return -1;
}
/* searches a variable definition within a group by its name */
cfg_mapping_t *cfg_lookup_var2(cfg_group_t *group, char *name, int len)
{
int i;
if (!group->mapping) return NULL; /* dynamic group is not ready */
for ( i = 0;
i < group->num;
i++
) {
if ((group->mapping[i].name_len == len)
&& (memcmp(group->mapping[i].def->name, name, len)==0)) {
return &(group->mapping[i]);
}
}
LOG(L_DBG, "DEBUG: cfg_lookup_var2(): variable not found: %.*s.%.*s\n",
group->name_len, group->name,
len, name);
return NULL;
}
/* clones the global config block
* WARNING: unsafe, cfg_writer_lock or cfg_global_lock must be held!
*/
cfg_block_t *cfg_clone_global(void)
{
cfg_block_t *block;
block = (cfg_block_t*)shm_malloc(sizeof(cfg_block_t)+cfg_block_size-1);
if (!block) {
LOG(L_ERR, "ERROR: cfg_clone_global(): not enough shm memory\n");
return NULL;
}
memcpy(block, *cfg_global, sizeof(cfg_block_t)+cfg_block_size-1);
/* reset the reference counter */
atomic_set(&block->refcnt, 0);
return block;
}
/* Clone an array of configuration group instances. */
cfg_group_inst_t *cfg_clone_array(cfg_group_meta_t *meta, cfg_group_t *group)
{
cfg_group_inst_t *new_array;
int size;
if (!meta->array || !meta->num)
return NULL;
size = (sizeof(cfg_group_inst_t) + group->size - 1) * meta->num;
new_array = (cfg_group_inst_t *)shm_malloc(size);
if (!new_array) {
LOG(L_ERR, "ERROR: cfg_clone_array(): not enough shm memory\n");
return NULL;
}
memcpy(new_array, meta->array, size);
return new_array;
}
/* Extend the array of configuration group instances with one more instance.
* Only the ID of the new group is set, nothing else. */
cfg_group_inst_t *cfg_extend_array(cfg_group_meta_t *meta, cfg_group_t *group,
unsigned int group_id,
cfg_group_inst_t **new_group)
{
int i;
cfg_group_inst_t *new_array, *old_array;
int inst_size;
inst_size = sizeof(cfg_group_inst_t) + group->size - 1;
new_array = (cfg_group_inst_t *)shm_malloc(inst_size * (meta->num + 1));
if (!new_array) {
LOG(L_ERR, "ERROR: cfg_extend_array(): not enough shm memory\n");
return NULL;
}
/* Find the position of the new group in the array. The array is ordered
by the group IDs. */
old_array = meta->array;
for ( i = 0;
(i < meta->num)
&& (((cfg_group_inst_t *)((char *)old_array + inst_size * i))->id < group_id);
i++
);
if (i > 0)
memcpy( new_array,
old_array,
inst_size * i);
memset((char*)new_array + inst_size * i, 0, inst_size);
*new_group = (cfg_group_inst_t *)((char*)new_array + inst_size * i);
(*new_group)->id = group_id;
if (i < meta->num)
memcpy( (char*)new_array + inst_size * (i + 1),
(char*)old_array + inst_size * i,
inst_size * (meta->num - i));
return new_array;
}
/* Remove an instance from a group array.
* inst must point to an instance within meta->array.
* *_new_array is set to the newly allocated array. */
int cfg_collapse_array(cfg_group_meta_t *meta, cfg_group_t *group,
cfg_group_inst_t *inst,
cfg_group_inst_t **_new_array)
{
cfg_group_inst_t *new_array, *old_array;
int inst_size, offset;
if (!meta->num)
return -1;
if (meta->num == 1) {
*_new_array = NULL;
return 0;
}
inst_size = sizeof(cfg_group_inst_t) + group->size - 1;
new_array = (cfg_group_inst_t *)shm_malloc(inst_size * (meta->num - 1));
if (!new_array) {
LOG(L_ERR, "ERROR: cfg_collapse_array(): not enough shm memory\n");
return -1;
}
old_array = meta->array;
offset = (char *)inst - (char *)old_array;
if (offset)
memcpy( new_array,
old_array,
offset);
if (meta->num * inst_size > offset + inst_size)
memcpy( (char *)new_array + offset,
(char *)old_array + offset + inst_size,
(meta->num - 1) * inst_size - offset);
*_new_array = new_array;
return 0;
}
/* Find the group instance within the meta-data based on the group_id */
cfg_group_inst_t *cfg_find_group(cfg_group_meta_t *meta, int group_size, unsigned int group_id)
{
int i;
cfg_group_inst_t *ginst;
if (!meta)
return NULL;
/* For now, search lineray.
TODO: improve */
for (i = 0; i < meta->num; i++) {
ginst = (cfg_group_inst_t *)((char *)meta->array
+ (sizeof(cfg_group_inst_t) + group_size - 1) * i);
if (ginst->id == group_id)
return ginst;
else if (ginst->id > group_id)
break; /* needless to continue, the array is ordered */
}
return NULL;
}
/* append new callbacks to the end of the child callback list
*
* WARNING: the function is unsafe, either hold CFG_LOCK(),
* or call the function before forking
*/
void cfg_install_child_cb(cfg_child_cb_t *cb_first, cfg_child_cb_t *cb_last)
{
/* add the new callbacks to the end of the linked-list */
(*cfg_child_cb_last)->next = cb_first;
*cfg_child_cb_last = cb_last;
}
/* installs a new global config
*
* replaced is an array of strings that must be freed together
* with the previous global config.
* cb_first and cb_last define a linked list of per-child process
* callbacks. This list is added to the global linked list.
*/
void cfg_install_global(cfg_block_t *block, void **replaced,
cfg_child_cb_t *cb_first, cfg_child_cb_t *cb_last)
{
cfg_block_t* old_cfg;
CFG_REF(block);
if (replaced) {
/* The replaced array is specified, it has to be linked to the child cb structure.
* The last child process processing this structure will free the old strings and the array. */
if (cb_first) {
cb_first->replaced = replaced;
} else {
/* At least one child cb structure is needed. */
cb_first = cfg_child_cb_new(NULL, NULL, NULL, 0 /* gname, name, cb, type */);
if (cb_first) {
cb_last = cb_first;
cb_first->replaced = replaced;
} else {
LOG(L_ERR, "ERROR: cfg_install_global(): not enough shm memory\n");
/* Nothing more can be done here, the replaced strings are still needed,
* they cannot be freed at this moment.
*/
}
}
}
CFG_LOCK();
old_cfg = *cfg_global;
*cfg_global = block;
if (cb_first)
cfg_install_child_cb(cb_first, cb_last);
CFG_UNLOCK();
if (old_cfg)
CFG_UNREF(old_cfg);
}
/* creates a structure for a per-child process callback */
cfg_child_cb_t *cfg_child_cb_new(str *gname, str *name,
cfg_on_set_child cb,
unsigned int type)
{
cfg_child_cb_t *cb_struct;
cb_struct = (cfg_child_cb_t *)shm_malloc(sizeof(cfg_child_cb_t));
if (!cb_struct) {
LOG(L_ERR, "ERROR: cfg_child_cb_new(): not enough shm memory\n");
return NULL;
}
memset(cb_struct, 0, sizeof(cfg_child_cb_t));
if (gname) {
cb_struct->gname.s = gname->s;
cb_struct->gname.len = gname->len;
}
if (name) {
cb_struct->name.s = name->s;
cb_struct->name.len = name->len;
}
cb_struct->cb = cb;
atomic_set(&cb_struct->refcnt, 0);
if (type & CFG_CB_ONLY_ONCE) {
/* The callback needs to be executed only once.
* Set the cb_count value to 1, so the first child
* process that executes the callback will decrement
* it to 0, and no other children will execute the
* callback again.
*/
atomic_set(&cb_struct->cb_count, 1);
} else {
/* Set the cb_count to a high value, i.e. max signed integer,
* so all the child processes will execute the callback,
* the counter will never reach 0.
*/
atomic_set(&cb_struct->cb_count, (1U<<(sizeof(int)*8-1))-1);
}
return cb_struct;
}
/* free the memory allocated for a child cb list */
void cfg_child_cb_free_list(cfg_child_cb_t *child_cb_first)
{
cfg_child_cb_t *cb, *cb_next;
for( cb = child_cb_first;
cb;
cb = cb_next
) {
cb_next = cb->next;
cfg_child_cb_free_item(cb);
}
}
/* Allocate memory for a new additional variable
* and link it to a configuration group.
* type==0 results in creating a new group instance with the default values.
* The group is created with CFG_GROUP_UNKNOWN type if it does not exist.
* Note: this function is usable only before the configuration is shmized.
*/
int new_add_var(str *group_name, unsigned int group_id, str *var_name,
void *val, unsigned int type)
{
cfg_group_t *group;
cfg_add_var_t *add_var = NULL, **add_var_p;
int len;
if (type && !var_name) {
LOG(L_ERR, "ERROR: new_add_var(): Missing variable specification\n");
goto error;
}
if (type)
LOG(L_DBG, "DEBUG: new_add_var(): declaring a new variable instance %.*s[%u].%.*s\n",
group_name->len, group_name->s,
group_id,
var_name->len, var_name->s);
else
LOG(L_DBG, "DEBUG: new_add_var(): declaring a new group instance %.*s[%u]\n",
group_name->len, group_name->s,
group_id);
if (cfg_shmized) {
LOG(L_ERR, "ERROR: new_add_var(): too late, the configuration has already been shmized\n");
goto error;
}
group = cfg_lookup_group(group_name->s, group_name->len);
if (!group) {
/* create a new group with NULL values, it will be filled in later */
group = cfg_new_group(group_name->s, group_name->len,
0 /* num */, NULL /* mapping */,
NULL /* vars */, 0 /* size */, NULL /* handle */);
if (!group)
goto error;
/* It is not yet known whether the group will be static or dynamic */
group->dynamic = CFG_GROUP_UNKNOWN;
}
add_var = (cfg_add_var_t *)pkg_malloc(sizeof(cfg_add_var_t) +
(type ? (var_name->len - 1) : 0));
if (!add_var) {
LOG(L_ERR, "ERROR: new_add_var(): Not enough memory\n");
goto error;
}
memset(add_var, 0, sizeof(cfg_add_var_t) +
(type ? (var_name->len - 1) : 0));
add_var->group_id = group_id;
if (type) {
add_var->name_len = var_name->len;
memcpy(add_var->name, var_name->s, var_name->len);
switch (type) {
case CFG_VAR_INT:
add_var->val.i = (int)(long)val;
break;
case CFG_VAR_STR:
len = ((str *)val)->len;
if (len) {
add_var->val.s.s = (char *)pkg_malloc(sizeof(char) * len);
if (!add_var->val.s.s) {
LOG(L_ERR, "ERROR: new_add_var(): Not enough memory\n");
goto error;
}
memcpy(add_var->val.s.s, ((str *)val)->s, len);
} else {
add_var->val.s.s = NULL;
}
add_var->val.s.len = len;
break;
case CFG_VAR_STRING:
if (val) {
len = strlen((char *)val);
add_var->val.ch = (char *)pkg_malloc(sizeof(char) * (len + 1));
if (!add_var->val.ch) {
LOG(L_ERR, "ERROR: new_add_var(): Not enough memory\n");
goto error;
}
memcpy(add_var->val.ch, (char *)val, len);
add_var->val.ch[len] = '\0';
} else {
add_var->val.ch = NULL;
}
break;
default:
LOG(L_ERR, "ERROR: new_add_var(): unsupported value type: %u\n",
type);
goto error;
}
add_var->type = type;
}
/* order the list by group_id, it will be easier to count the group instances */
for( add_var_p = &group->add_var;
*add_var_p && ((*add_var_p)->group_id <= group_id);
add_var_p = &((*add_var_p)->next));
add_var->next = *add_var_p;
*add_var_p = add_var;
return 0;
error:
if (!type)
LOG(L_ERR, "ERROR: new_add_var(): failed to add the additional group instance: %.*s[%u]\n",
group_name->len, group_name->s, group_id);
else
LOG(L_ERR, "ERROR: new_add_var(): failed to add the additional variable instance: %.*s[%u].%.*s\n",
group_name->len, group_name->s, group_id,
(var_name)?var_name->len:0, (var_name&&var_name->s)?var_name->s:"");