/
fsm-policy.c
1749 lines (1462 loc) · 42.2 KB
/
fsm-policy.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
/*
* Wicked policy handling.
*
* Copyright (C) 2012 Olaf Kirch <okir@suse.de>
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <wicked/logging.h>
#include <wicked/netinfo.h>
#include <wicked/modem.h>
#include <wicked/wireless.h>
#include <wicked/fsm.h>
#include "client/ifconfig.h"
#include "util_priv.h"
/*
* The <match> expression
*/
typedef struct ni_ifcondition ni_ifcondition_t;
typedef ni_bool_t ni_ifcondition_check_fn_t(const ni_ifcondition_t *, ni_ifworker_t *);
struct ni_ifcondition {
ni_ifcondition_check_fn_t *check;
union {
ni_ifworker_type_t type;
struct {
xml_node_t * node;
ni_dbus_object_t *object;
} device;
struct {
ni_ifcondition_t *left;
ni_ifcondition_t *right;
} terms;
const ni_dbus_class_t * class;
char * string;
unsigned int uint;
} args;
};
/*
* A template operates on one or more devices, aggregating
* them or building a virtual device on top of them.
*/
typedef struct ni_fsm_template_input ni_fsm_template_input_t;
struct ni_fsm_template_input {
ni_fsm_template_input_t * next;
char * id;
ni_bool_t shared;
ni_ifcondition_t * match;
ni_ifworker_t * device;
};
/*
* Actions associated with the policy - <merge>, <create> etc.
*/
typedef enum {
NI_IFPOLICY_ACTION_MERGE,
NI_IFPOLICY_ACTION_REPLACE,
NI_IFPOLICY_ACTION_CREATE,
} ni_fsm_policy_action_type_t;
typedef struct ni_fsm_policy_action ni_fsm_policy_action_t;
struct ni_fsm_policy_action {
ni_fsm_policy_action_t * next;
ni_fsm_policy_action_type_t type;
xml_node_t * data;
/* An xml policy may specify the node it
* applies to by a path, which is a bit like a very
* simple xpath, like /foo/bar/baz.
*/
char * xpath;
ni_bool_t final;
/* Templates can create one or more devices */
struct {
const ni_dbus_class_t * class;
unsigned int serial;
ni_bool_t instantiate_multi;
ni_fsm_template_input_t *inputs;
} create;
};
/*
* Opaque policy object
*/
typedef enum {
NI_IFPOLICY_TYPE_CONFIG,
NI_IFPOLICY_TYPE_TEMPLATE,
} ni_fsm_policy_type_t;
struct ni_fsm_policy {
ni_fsm_policy_t * next;
unsigned int seq;
ni_fsm_policy_type_t type;
char * name;
xml_node_t * node;
unsigned int weight;
ni_ifcondition_t * match;
ni_fsm_policy_action_t * create_action;
ni_fsm_policy_action_t * actions;
};
static void __ni_fsm_policy_reset(ni_fsm_policy_t *);
static ni_ifcondition_t * ni_fsm_policy_conditions_from_xml(xml_node_t *);
static ni_bool_t ni_ifcondition_check(const ni_ifcondition_t *, ni_ifworker_t *);
static ni_ifcondition_t * ni_ifcondition_from_xml(xml_node_t *);
static void ni_ifcondition_free(ni_ifcondition_t *);
static ni_fsm_policy_action_t * ni_fsm_policy_action_new(ni_fsm_policy_action_type_t, xml_node_t *, ni_fsm_policy_t *);
static void ni_fsm_policy_action_free(ni_fsm_policy_action_t *);
static xml_node_t * ni_fsm_policy_action_xml_merge(const ni_fsm_policy_action_t *, xml_node_t *);
static xml_node_t * ni_fsm_policy_action_xml_replace(const ni_fsm_policy_action_t *, xml_node_t *);
static xml_node_t * ni_fsm_template_build_document(ni_fsm_policy_t *policy, ni_fsm_policy_action_t *action);
static ni_bool_t ni_fsm_template_bind_devices(ni_fsm_policy_t *, ni_fsm_policy_action_t *, xml_node_t *);
static ni_fsm_template_input_t *ni_fsm_template_input_new(const char *id, ni_fsm_template_input_t ***tailp);
static void ni_fsm_template_input_free(ni_fsm_template_input_t *);
/*
* Destructor for policy objects
*/
void
ni_fsm_policy_free(ni_fsm_policy_t *policy)
{
if (policy) {
ni_string_free(&policy->name);
__ni_fsm_policy_reset(policy);
free(policy);
}
}
static void
__ni_fsm_policy_reset(ni_fsm_policy_t *policy)
{
if (policy->match) {
ni_ifcondition_free(policy->match);
policy->match = NULL;
}
while (policy->actions) {
ni_fsm_policy_action_t *a = policy->actions;
policy->actions = a->next;
ni_fsm_policy_action_free(a);
}
if (policy->create_action) {
ni_fsm_policy_action_free(policy->create_action);
policy->create_action = NULL;
}
}
static ni_bool_t
__ni_fsm_policy_from_xml(ni_fsm_policy_t *policy, xml_node_t *node)
{
static unsigned int __policy_seq = 1;
xml_node_t *item;
const char *attr;
policy->node = node;
if (node == NULL)
return TRUE;
if (ni_string_eq(node->name, "policy"))
policy->type = NI_IFPOLICY_TYPE_CONFIG;
else
if (ni_string_eq(node->name, "template"))
policy->type = NI_IFPOLICY_TYPE_TEMPLATE;
else {
ni_error("invalid policy, must be either <policy> or <template>");
return FALSE;
}
if ((attr = xml_node_get_attr(node, "weight")) != NULL) {
if (ni_parse_uint(attr, &policy->weight, 10) < 0) {
ni_error("%s: cannot parse weight=\"%s\" attribute",
xml_node_location(node), attr);
return FALSE;
}
}
for (item = node->children; item; item = item->next) {
ni_fsm_policy_action_t *action = NULL;
if (ni_string_eq(item->name, "match")) {
if (policy->type == NI_IFPOLICY_TYPE_TEMPLATE) {
ni_error("%s: match elements not permitted in templates", xml_node_location(item));
return FALSE;
}
if (policy->match) {
ni_error("%s: policy specifies multiple <match> elements", xml_node_location(item));
return FALSE;
}
if (!(policy->match = ni_fsm_policy_conditions_from_xml(item))) {
ni_error("%s: trouble parsing policy conditions", xml_node_location(item));
return FALSE;
}
continue;
} else
if (ni_string_eq(item->name, NI_NANNY_IFPOLICY_MERGE)) {
action = ni_fsm_policy_action_new(NI_IFPOLICY_ACTION_MERGE, item, policy);
} else
if (ni_string_eq(item->name, NI_NANNY_IFPOLICY_REPLACE)) {
action = ni_fsm_policy_action_new(NI_IFPOLICY_ACTION_REPLACE, item, policy);
} else
if (ni_string_eq(item->name, NI_NANNY_IFPOLICY_CREATE)) {
if (policy->type != NI_IFPOLICY_TYPE_TEMPLATE) {
ni_error("%s: <create> elements are permitted in templates only",
xml_node_location(item));
return FALSE;
}
if (policy->create_action) {
ni_error("%s: template specifies more than one <create> action",
xml_node_location(item));
return FALSE;
}
policy->create_action = ni_fsm_policy_action_new(NI_IFPOLICY_ACTION_CREATE, item, NULL);
} else {
ni_error("%s: unknown <%s> element in policy",
xml_node_location(item), item->name);
return FALSE;
}
if (action == NULL) {
ni_error("%s: unable to process <%s> action",
xml_node_location(item), item->name);
return FALSE;
}
}
/* if we have a template, make sure it has exactly one <create> element */
if (policy->type == NI_IFPOLICY_TYPE_TEMPLATE && policy->create_action == NULL) {
ni_error("%s: template does not specify a <create> element", xml_node_location(node));
return FALSE;
}
policy->seq = __policy_seq++;
return TRUE;
}
ni_fsm_policy_t *
ni_fsm_policy_new(ni_fsm_t *fsm, const char *name, xml_node_t *node)
{
ni_fsm_policy_t *policy;
ni_fsm_policy_t *pos, **tail;
policy = xcalloc(1, sizeof(*policy));
ni_string_dup(&policy->name, name);
if (!__ni_fsm_policy_from_xml(policy, node)) {
ni_fsm_policy_free(policy);
return NULL;
}
for (tail = &fsm->policies; (pos = *tail) != NULL; tail = &pos->next)
;
*tail = policy;
return policy;
}
ni_bool_t
ni_fsm_policy_update(ni_fsm_policy_t *policy, xml_node_t *node)
{
ni_fsm_policy_t temp;
if (!policy || !ni_ifconfig_is_policy(node)
|| !ni_string_eq(ni_ifpolicy_get_name(node), policy->name))
return FALSE;
memset(&temp, 0, sizeof(temp));
if (!__ni_fsm_policy_from_xml(&temp, node))
return FALSE;
__ni_fsm_policy_reset(policy);
policy->type = temp.type;
policy->seq = temp.seq;
policy->weight = temp.weight;
policy->create_action = temp.create_action;
policy->actions = temp.actions;
policy->match = temp.match;
policy->node = node;
return TRUE;
}
ni_bool_t
ni_fsm_policies_changed_since(const ni_fsm_t *fsm, unsigned int *tstamp)
{
ni_fsm_policy_t *policy;
ni_bool_t rv = FALSE;
for (policy = fsm->policies; policy; policy = policy->next) {
if (policy->seq > *tstamp) {
*tstamp = policy->seq;
rv = TRUE;
}
}
return rv;
}
ni_fsm_policy_t *
ni_fsm_policy_by_name(ni_fsm_t *fsm, const char *name)
{
ni_fsm_policy_t *policy;
for (policy = fsm->policies; policy; policy = policy->next) {
if (policy->name && ni_string_eq(policy->name, name))
return policy;
}
return NULL;
}
ni_bool_t
ni_fsm_policy_remove(ni_fsm_t *fsm, ni_fsm_policy_t *policy)
{
ni_fsm_policy_t **pos, *cur;
ni_assert(fsm);
for (pos = &fsm->policies; (cur = *pos); pos = &cur->next) {
if (cur == policy) {
*pos = cur->next;
ni_fsm_policy_free(cur);
return TRUE;
}
}
return FALSE;
}
/*
* Get the policy's name (if set)
*/
const char *
ni_fsm_policy_name(const ni_fsm_policy_t *policy)
{
return policy->name;
}
/*
* Get the policy's location (if set)
*/
xml_location_t *
ni_fsm_policy_location(const ni_fsm_policy_t *policy)
{
if (!policy || !policy->node)
return NULL;
return policy->node->location;
}
/*
* Check whether policy applies to this ifworker
*/
static ni_bool_t
ni_fsm_policy_applicable(ni_fsm_policy_t *policy, ni_ifworker_t *w)
{
xml_node_t *node;
char *pname;
if (!policy || !w)
return FALSE;
/* 1st match check -ifworker to policy name comparison */
pname = ni_ifpolicy_name_from_ifname(w->name);
if (!ni_string_eq(policy->name, pname)) {
ni_string_free(&pname);
return FALSE;
}
ni_string_free(&pname);
/* 2nd match check - ifworker to config name comparison */
if (!xml_node_is_empty(w->config.node) &&
(node = xml_node_get_child(w->config.node, "name"))) {
const char *namespace = xml_node_get_attr(node, "namespace");
if (!namespace && !ni_string_eq(node->cdata, w->name)) {
ni_error("%s: config name does not match policy name",
policy->name);
return FALSE;
}
}
/* 3rd match check - <match> condition must be fulfilled */
if (!ni_ifcondition_check(policy->match, w)) {
ni_debug_nanny("%s: policy <match> condition is not met for worker %s",
policy->name, w->name);
return FALSE;
}
ni_debug_nanny("%s: found applicable policy: %s", w->name, policy->name);
return TRUE;
}
/*
* Retrieve policy origin
*/
const char *
ni_fsm_policy_get_origin(const ni_fsm_policy_t *policy)
{
const char *origin;
origin = ni_ifpolicy_get_origin(policy->node);
return ni_string_empty(origin) ? "nanny" : origin;
}
/*
* Compare the weight of two policies.
* Returns < 0 if a's weight is smaller than that of b, etc.
*/
static int
__ni_fsm_policy_compare(const void *a, const void *b)
{
const ni_fsm_policy_t *pa = a;
const ni_fsm_policy_t *pb = b;
return ((int) pa->weight) - ((int) pb->weight);
}
/*
* Obtain the list of applicable policies
*/
unsigned int
ni_fsm_policy_get_applicable_policies(ni_fsm_t *fsm, ni_ifworker_t *w,
const ni_fsm_policy_t **result, unsigned int max)
{
unsigned int count = 0;
ni_fsm_policy_t *policy;
if (!w) {
ni_error("unable to get applicable policy for non-existing device");
return 0;
}
if (!w->use_default_policies)
return 0;
for (policy = fsm->policies; policy; policy = policy->next) {
if (!ni_ifpolicy_name_is_valid(policy->name)) {
ni_error("policy with invalid name %s", policy->name);
continue;
}
if (policy->type != NI_IFPOLICY_TYPE_CONFIG) {
ni_error("policy %s: wrong type %d", policy->name, policy->type);
continue;
}
if (!policy->match) {
ni_error("policy %s: no valid <match>", policy->name);
continue;
}
if (ni_fsm_policy_applicable(policy, w)) {
if (count < max)
result[count++] = policy;
}
}
qsort(result, count, sizeof(result[0]), __ni_fsm_policy_compare);
return count;
}
ni_bool_t
ni_fsm_exists_applicable_policy(ni_fsm_policy_t *list, ni_ifworker_t *w)
{
ni_fsm_policy_t *policy;
if (!list || !w)
return FALSE;
for (policy = list; policy; policy = policy->next) {
if (ni_fsm_policy_applicable(policy, w))
return TRUE;
}
return FALSE;
}
/*
* Transform an interface or modem XML document according to a list of policies.
* Usually, you would obtain that list via ni_fsm_policy_get_applicable_policies
* above.
*
* The way this works is
* a) dbus methods expect their input from the XML interface definition,
* explicitly specifying that it takes its input from element <foo>
*
* b) the document may or may not contain a <foo> element, but one or
* more policies may.
*
* c) Policies may specify <merge> actions, which means we merge the
* <foo> element from the policy with the <foo> element from the
* interface definition. Such a policy might look like this:
*
* <policy>
* ... conditions ...
* <merge>
* <foo> <bar/> <baz/> </foo>
* </merge>
* </policy>
*
* Note, in the above example we replace the entire <foo> element
* of the original document with the <foo> element specified
* in the action.
*
* Alternatively, if you want to overwrite specific children of
* the <foo> node only, you could specify an alternative merge
* action:
*
* <merge path="/foo">
* <bar/> <baz/>
* </merge>
*
* This would create the foo node (if it does not exist), and
* add elements <bar> and <baz> to it. If the document contains
* a <foo> node already, the merge rule will only replace <bar>
* and <baz>, but leave all other children of <foo> unchanged.
*
* d) Policies may also specify <replace> actions, which work
* pretty similar to <merge> except that they replace all children
* with what is specified in the action.
*
* e) Policies are applied in order of increasing weight, ie any
* policy with a greater "weight" attribute potentially overwrites
* changes made by a policy with lower weight.
*/
xml_node_t *
ni_fsm_policy_transform_document(xml_node_t *node, ni_fsm_policy_t * const *policies, unsigned int count)
{
unsigned int i = 0;
/* Apply policies in order of decreasing weight */
for (i = count; i--; ) {
const ni_fsm_policy_t *policy = policies[i];
ni_fsm_policy_action_t *action;
if (!policy)
continue;
for (action = policy->actions; action && node; action = action->next) {
switch (action->type) {
case NI_IFPOLICY_ACTION_MERGE:
node = ni_fsm_policy_action_xml_merge(action, node);
break;
case NI_IFPOLICY_ACTION_REPLACE:
node = ni_fsm_policy_action_xml_replace(action, node);
break;
default:
continue;
}
}
}
return node;
}
/*
* Policy actions
*/
ni_fsm_policy_action_t *
ni_fsm_policy_action_new(ni_fsm_policy_action_type_t type, xml_node_t *node, ni_fsm_policy_t *policy)
{
ni_fsm_policy_action_t **list = NULL;
ni_fsm_policy_action_t *action;
const char *attr;
if (policy) {
for (list = &policy->actions; (action = *list) != NULL; list = &action->next)
;
}
action = calloc(1, sizeof(*action));
action->type = type;
action->data = node;
if (list)
*list = action;
if (type == NI_IFPOLICY_ACTION_MERGE || type == NI_IFPOLICY_ACTION_REPLACE) {
if ((attr = xml_node_get_attr(node, "path")) != NULL)
ni_string_dup(&action->xpath, attr);
if ((attr = xml_node_get_attr(node, "final")) != NULL) {
if (!strcasecmp(attr, "true") || !strcmp(attr, "1"))
action->final = TRUE;
}
} else
if (type == NI_IFPOLICY_ACTION_CREATE) {
ni_fsm_template_input_t **input_tail;
xml_node_t *child;
if ((attr = xml_node_get_attr(node, "class")) == NULL) {
ni_error("%s: <%s> element lacks class attribute", xml_node_location(node), node->name);
return NULL;
}
action->create.class = ni_objectmodel_get_class(attr);
if (action->create.class == NULL) {
ni_error("%s: <%s> specifies unknown class \"%s\"", xml_node_location(node), node->name, attr);
return NULL;
}
action->create.instantiate_multi = FALSE;
if ((attr = xml_node_get_attr(node, "instantiate")) != NULL) {
if (ni_string_eq(attr, "multi"))
action->create.instantiate_multi = TRUE;
else if (!ni_string_eq(attr, "once")) {
ni_error("%s: <%s> specifies bad instantiate=\"%s\" attribute",
xml_node_location(node), node->name, attr);
return NULL;
}
}
input_tail = &action->create.inputs;
for (child = node->children; child; child = child->next) {
if (ni_string_eq(child->name, "input-device")) {
ni_fsm_template_input_t *input;
xml_node_t *matchnode;
if (!(attr = xml_node_get_attr(child, "id"))) {
ni_error("%s: <%s> element lacks id attribute", xml_node_location(child), child->name);
return NULL;
}
/* FIXME: check for duplicate IDs */
input = ni_fsm_template_input_new(attr, &input_tail);
if ((attr = xml_node_get_attr(child, "shared")) != NULL) {
if (!strcasecmp(attr, "true") || !strcmp(attr, "1"))
input->shared = TRUE;
}
if (!(matchnode = xml_node_get_child(child, "match"))) {
ni_error("%s: <%s> element lacks <match> child", xml_node_location(child), child->name);
return NULL;
}
if (!(input->match = ni_fsm_policy_conditions_from_xml(matchnode))) {
ni_error("%s: trouble parsing policy conditions", xml_node_location(matchnode));
return NULL;
}
} else {
ni_error("%s: unexpected element <%s>", xml_node_location(child), child->name);
return NULL;
}
}
}
return action;
}
void
ni_fsm_policy_action_free(ni_fsm_policy_action_t *action)
{
if (action->xpath)
ni_string_free(&action->xpath);
if (action->type == NI_IFPOLICY_ACTION_CREATE) {
ni_fsm_template_input_t *input;
while ((input = action->create.inputs) != NULL) {
action->create.inputs = input->next;
ni_fsm_template_input_free(input);
}
}
free(action);
}
ni_fsm_template_input_t *
ni_fsm_template_input_new(const char *id, ni_fsm_template_input_t ***tailp)
{
ni_fsm_template_input_t **tail;
ni_fsm_template_input_t *result;
result = xcalloc(1, sizeof(*result));
ni_string_dup(&result->id, id);
if (tailp && (tail = *tailp)) {
*tail = result;
*tailp = &result->next;
}
return result;
}
void
ni_fsm_template_input_free(ni_fsm_template_input_t *input)
{
ni_string_free(&input->id);
if (input->match) {
ni_ifcondition_free(input->match);
input->match = NULL;
}
free(input);
}
/*
* Look up XML elements based on the fake xpath.
* If the named child does not exist, we create a new child of this name.
* If the named child exists but is marked as "final", we refuse to change it
* any further.
*/
static void
__ni_fsm_policy_action_xml_lookup(xml_node_t *node, const char *name, xml_node_array_t *res)
{
xml_node_t *child;
unsigned int found = 0;
for (child = node->children; child; child = child->next) {
if (ni_string_eq(child->name, name)) {
if (!child->final)
xml_node_array_append(res, child);
found++;
}
}
if (!found)
xml_node_array_append(res, xml_node_new(name, node));
}
static xml_node_array_t *
ni_fsm_policy_action_xml_lookup(xml_node_t *node, const char *path)
{
xml_node_array_t *cur;
char *copy, *name;
if (node->final) {
ni_error("%s: called with XML element that's marked final", __func__);
return NULL;
}
cur = xml_node_array_new();
xml_node_array_append(cur, node);
copy = strdup(path);
for (name = strtok(copy, "/"); name && cur->count; name = strtok(NULL, "/")) {
xml_node_array_t *next;
unsigned int i;
next = xml_node_array_new();
for (i = 0; i < cur->count; ++i) {
xml_node_t *np = cur->data[i];
__ni_fsm_policy_action_xml_lookup(np, name, next);
}
xml_node_array_free(cur);
cur = next;
}
free(copy);
return cur;
}
/*
* ifpolicy merge action
* <merge path="/foo">
* <bar/> <baz/>
* </merge>
* This will look up the <foo> element inside the given document, and merge
* <bar> and <baz> into it, without replacing existing <bar> and <baz> elements.
*
* If the path attribute is not given, this transformation applies to the
* top-level xml node (which is usually an <interface> or <modem> element).
*/
xml_node_t *
ni_fsm_policy_action_xml_merge(const ni_fsm_policy_action_t *action, xml_node_t *node)
{
xml_node_array_t *nodes;
unsigned int i;
if (node->final)
return node;
if (action->xpath == NULL) {
xml_node_merge(node, action->data);
node->final = action->final;
return node;
}
nodes = ni_fsm_policy_action_xml_lookup(node, action->xpath);
if (nodes == NULL)
return NULL;
for (i = 0; i < nodes->count; ++i) {
xml_node_t *np = nodes->data[i];
xml_node_merge(np, action->data);
np->final = action->final;
}
xml_node_array_free(nodes);
return node;
}
/*
* ifpolicy replace action
* <replace path="/foo">
* <bar/> <baz/>
* </replace>
* This will look up the <foo> element inside the given document, remove all of
* its children and replace them with <bar> and <baz>.
*
* If the path attribute is not given, this transformation replaces the
* top-level xml node (which is usually an <interface> or <modem> element).
*/
xml_node_t *
ni_fsm_policy_action_xml_replace(const ni_fsm_policy_action_t *action, xml_node_t *node)
{
xml_node_array_t *nodes;
unsigned int i;
if (node->final)
return node;
if (action->xpath == NULL) {
xml_node_free(node);
return xml_node_clone_ref(action->data);
}
nodes = ni_fsm_policy_action_xml_lookup(node, action->xpath);
if (nodes == NULL)
return NULL;
for (i = 0; i < nodes->count; ++i) {
xml_node_t *np = nodes->data[i];
xml_node_t *child;
/* Remove all children of the node we found. */
while ((child = np->children) != NULL)
xml_node_delete_child_node(np, child);
/* Add all children of the <replace> action to it. */
for (child = action->data->children; child; child = child->next)
xml_node_clone(child, np);
np->final = action->final;
}
xml_node_array_free(nodes);
return node;
}
/*
* Template handling
*/
ni_bool_t
ni_fsm_policy_is_template(const ni_fsm_policy_t *policy)
{
return policy->type == NI_IFPOLICY_TYPE_TEMPLATE;
}
ni_bool_t
ni_fsm_template_multi_instance(const ni_fsm_policy_t *policy)
{
return policy->create_action && policy->create_action->create.instantiate_multi;
}
xml_node_t *
ni_fsm_template_instantiate(ni_fsm_policy_t *policy, const ni_ifworker_array_t *devices)
{
ni_fsm_template_input_t *input;
ni_fsm_policy_action_t *action;
unsigned int i, num_needed;
xml_node_t *config;
if (policy->type != NI_IFPOLICY_TYPE_TEMPLATE
|| (action = policy->create_action) == NULL)
return NULL;
num_needed = 0;
for (input = action->create.inputs; input; input = input->next) {
input->device = NULL;
num_needed++;
}
for (i = 0; i < devices->count && num_needed; ++i) {
ni_ifworker_t *w = devices->data[i];
unsigned int nshared = 0;
for (input = action->create.inputs; input; input = input->next) {
if (input->device != NULL)
continue;
if (!ni_ifcondition_check(input->match, w))
continue;
if (input->shared) {
input->device = w;
num_needed--;
nshared++;
} else if (nshared == 0) {
input->device = w;
num_needed--;
break;
}
}
}
if (num_needed)
return NULL;
config = ni_fsm_template_build_document(policy, action);
if (config == NULL)
return NULL;
if (!ni_fsm_template_bind_devices(policy, action, config)) {
xml_node_free(config);
return NULL;
}
return config;
}
xml_node_t *
ni_fsm_template_build_document(ni_fsm_policy_t *policy, ni_fsm_policy_action_t *action)
{
xml_node_t *config;
if (ni_dbus_class_is_subclass(action->create.class, &ni_objectmodel_netif_class)) {
config = xml_node_new("interface", NULL);
} else {
ni_error("%s: class %s not supported", __func__, action->create.class->name);
return NULL;
}
xml_node_add_attr(config, "class", action->create.class->name);
if (action->create.instantiate_multi) {
char new_name[128];
snprintf(new_name, sizeof(new_name), "%s%u", policy->name, action->create.serial++);
xml_node_new_element("name", config, new_name);
} else {
xml_node_new_element("name", config, policy->name);
}
/* Now transform the document */
for (action = policy->actions; action; action = action->next) {
switch (action->type) {
case NI_IFPOLICY_ACTION_MERGE:
config = ni_fsm_policy_action_xml_merge(action, config);
break;
case NI_IFPOLICY_ACTION_REPLACE:
config = ni_fsm_policy_action_xml_replace(action, config);
break;
default:
continue;
}
if (config == NULL) {
ni_error("%s: failed to build document", policy->name);
return NULL;
}
}
return config;
}
ni_bool_t
ni_fsm_template_bind_devices(ni_fsm_policy_t *policy, ni_fsm_policy_action_t *action, xml_node_t *node)
{
xml_node_t *child;
again:
for (child = node->children; child; child = child->next) {
ni_fsm_template_input_t *input;
const char *id;
if (!ni_string_eq(child->name, "template:use"))
continue;
if (!(id = xml_node_get_attr(child, "id"))) {
ni_error("%s: <%s> element lacking id attribute",
xml_node_location(child), child->name);
return FALSE;
}
for (input = action->create.inputs; input; input = input->next) {
if (ni_string_eq(input->id, id))
break;
}
if (input == NULL) {