summaryrefslogtreecommitdiffstats
path: root/__root__/ccs-flatten/restree.c
blob: 57319e7daf755dea37b95884d73fdc7368258b93 (plain)
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
/*
  Copyright 2015 Red Hat, Inc.

  This program 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, or (at your option) any
  later version.

  This program 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; see the file COPYING.  If not, write to the
  Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
  MA 02110-1301 USA

  Fix for #193859 - relocation of a service w/o umounting file-systems
    by Navid Sheikhol-Eslami [ navid at redhat dot com ]
*/
#include <libxml/parser.h>
#include <libxml/xmlmemory.h>
#include <stdlib.h>
#include <stdio.h>
#include <libgen.h>
#include <sys/types.h>
#include "list.h"
#include "resgroup.h"
#include "reslist.h"
#include "xmlconf.h"

/* XXX from resrules.c */
int store_childtype(resource_child_t ** childp, char *name, int start,
                    int stop, int forbid, int flags);
int _res_op(xmlNode ** xpp, xmlNode * rmp, resource_node_t ** tree, resource_t * first, char *type);
static inline int


_res_op_internal(xmlNode ** xpp, xmlNode * rmp, resource_node_t ** tree, resource_t * first,
                 char *type, resource_node_t * node);

/* XXX from reslist.c */
void *act_dup(resource_act_t * acts);

/**
   Fold a resource into an XML node.

   @param xpp           XML node pp
   @param rmp           resources block pp
   @param node          Resource tree node we're dealing with
   @param op            Operation to perform (stop/start/etc.)
   @param depth         OCF Check level/depth
   @return              Return value of script.
   @see                 build_env
 */
static int
res_do_flatten(xmlNode ** xpp, xmlNode * rmp, resource_node_t * node, const char *arg, int depth)
{
    xmlNode *n, *r;
    resource_attr_t *ra;
    resource_t *res = node->rn_resource;
    resource_act_t *acts = node->rn_actions;
    char *val;
    char buf[256];
    int x, y;

    n = xmlNewNode(NULL, (xmlChar *) res->r_rule->rr_type);

    xmlSetProp(n, (xmlChar *) "rgmanager-meta-agent",
               (xmlChar *) basename(res->r_rule->rr_agent));

    xmlSetProp(n, (xmlChar *) "rgmanager-meta-primary",
               (xmlChar *) primary_attr_name(res));

    /* set actions explicitly */
    for (x = 0; acts[x].ra_name; x++) {
        if (acts[x].ra_timeout < 0 && acts[x].ra_interval < 0)
            continue;
        /* blacklist some effective non-actions */
        if (!strcmp(acts[x].ra_name, "meta-data")
                || !strcmp(acts[x].ra_name, "validate-all"))
            continue;

        r = xmlNewNode(NULL, (xmlChar *) "action");
        xmlSetProp(r, (xmlChar *) "name", (xmlChar *) acts[x].ra_name);

        if (acts[x].ra_timeout >= 0) {
            snprintf(buf, sizeof(buf), "%ld", acts[x].ra_timeout);
            xmlSetProp(r, (xmlChar *) "timeout", (xmlChar *) buf);
        }
        if (acts[x].ra_interval >= 0) {
            snprintf(buf, sizeof(buf), "%ld", acts[x].ra_interval);
            xmlSetProp(r, (xmlChar *) "interval", (xmlChar *) buf);
        }
        if ((!strcmp(acts[x].ra_name, "status")
                || !strcmp(acts[x].ra_name, "monitor"))
                && acts[x].ra_depth >= 0) {
            snprintf(buf, sizeof(buf), "%d", acts[x].ra_depth);
            xmlSetProp(r, (xmlChar *) "depth", (xmlChar *) buf);
        }

        xmlAddChild(n, r);
    }

    /* Multiple-instance resources must be decomposed into separate
       resources */
    if (res->r_refs > 1) {
        snprintf(buf, sizeof(buf), "%s_%d",
                 primary_attr_value(res), res->r_incarnations);
        ++res->r_incarnations;
    } else {
        snprintf(buf, sizeof(buf), "%s", primary_attr_value(res));
    }

    for (x = 0; res->r_attrs && res->r_attrs[x].ra_name; x++) {
        ra = &res->r_attrs[x];

        if (ra->ra_flags & RA_PRIMARY) {
            xmlSetProp(n, (xmlChar *) ra->ra_name, (xmlChar *) buf);
        } else {
            val = attr_value(node, res->r_attrs[x].ra_name);
            if (!val)
                continue;

            for (y = 0; res->r_rule->rr_attrs[y].ra_name; y++) {
                if (strcmp(ra->ra_name, res->r_rule->rr_attrs[y].ra_name))
                    continue;

#if 0
                if (!res->r_rule->rr_attrs[y].ra_value ||
                    strcmp(val, res->r_rule->rr_attrs[y].ra_value))
#endif
                    xmlSetProp(n, (xmlChar *) ra->ra_name, (xmlChar *) val);
            }
        }
    }


    /* also restore some meta-attributes */
    if (node->rn_flags &= (RF_INDEPENDENT|RF_NON_CRITICAL)) {
        xmlSetProp(n, (xmlChar *) "__independent_subtree",
                      (xmlChar *) (node->rn_flags&RF_NON_CRITICAL ? "2" : "1"));
    }

    if (!*xpp) {
        /* Add top-level container */
        *xpp = n;
    } else {
        if (!rmp) {
            xmlAddChild(*xpp, n);
        } else {
            r = xmlNewNode(NULL, (xmlChar *) res->r_rule->rr_type);
            xmlSetProp(r, (xmlChar *) "ref", (xmlChar *) primary_attr_value(res));
            xmlAddChild(rmp, n);
            xmlAddChild(*xpp, r);
        }
    }

    return 0;
}

static inline void
assign_restart_policy(resource_t * curres, resource_node_t * parent,
                      resource_node_t * node, char *base)
{
    char *val;
    int max_restarts = 0;
    time_t restart_expire_time = 0;
    char tok[1024];

    if (!curres || !node)
        return;
    if (parent && !(node->rn_flags & RF_INDEPENDENT))
        return;

    if (node->rn_flags & RF_INDEPENDENT) {
        /* per-resource-node failures / expire times */
        snprintf(tok, sizeof(tok), "%s/@__max_restarts", base);
        if (conf_get(tok, &val) == 0) {
            max_restarts = atoi(val);
            if (max_restarts <= 0)
                max_restarts = 0;
            free(val);
        }

        snprintf(tok, sizeof(tok), "%s/@__restart_expire_time", base);
        if (conf_get(tok, &val) == 0) {
            restart_expire_time = (time_t) expand_time(val);
            if ((int64_t) restart_expire_time <= 0)
                restart_expire_time = 0;
            free(val);
        }
        //if (restart_expire_time == 0 || max_restarts == 0)
        return;
        //goto out_assign;
    }

    val = (char *)res_attr_value(curres, "max_restarts");
    if (!val)
        return;
    max_restarts = atoi(val);
    if (max_restarts <= 0)
        return;
    val = res_attr_value(curres, "restart_expire_time");
    if (val) {
        restart_expire_time = (time_t) expand_time(val);
        if ((int64_t) restart_expire_time < 0)
            return;
    }
//out_assign:
    return;
}

static inline int
do_load_resource(char *base,
                 resource_rule_t * rule,
                 resource_node_t ** tree,
                 resource_t ** reslist, resource_node_t * parent, resource_node_t ** newnode)
{
    char tok[512];
    char *ref;
    resource_node_t *node;
    resource_t *curres;
    time_t failure_expire = 0;
    int max_failures = 0;

    snprintf(tok, sizeof(tok), "%s/@ref", base);

    if (conf_get(tok, &ref) != 0) {
        /* There wasn't an existing resource. See if there
           is one defined inline */
        curres = load_resource(rule, base);
        if (!curres) {
            /* No ref and no new one inline ==
               no more of the selected type */
            return 1;
        }

        if (store_resource(reslist, curres) != 0) {
            fprintf(stderr, "Error storing %s resource\n", curres->r_rule->rr_type);
            destroy_resource(curres);
            return -1;
        }

        curres->r_flags = RF_INLINE;

    } else {

        curres = find_resource_by_ref(reslist, rule->rr_type, ref);
        if (!curres) {
            fprintf(stderr, "Error: Reference to nonexistent "
                    "resource %s (type %s)\n", ref, rule->rr_type);
            free(ref);
            return -1;
        }

        if (curres->r_flags & RF_INLINE) {
            fprintf(stderr, "Error: Reference to inlined "
                    "resource %s (type %s) is illegal\n", ref, rule->rr_type);
            free(ref);
            return -1;
        }
        free(ref);
    }

    /* Load it if its max refs hasn't been exceeded */
    if (rule->rr_maxrefs && (curres->r_refs >= rule->rr_maxrefs)) {
        fprintf(stderr, "Warning: Max references exceeded for resource"
                " %s (type %s)\n", curres->r_attrs[0].ra_name, rule->rr_type);
        return -1;
    }

    node = malloc(sizeof(*node));
    if (!node)
        return -1;

    memset(node, 0, sizeof(*node));

    //printf("New resource tree node: %s:%s \n", curres->r_rule->rr_type,curres->r_attrs->ra_value);

    node->rn_child = NULL;
    node->rn_parent = parent;
    node->rn_resource = curres;
    node->rn_state = RES_STOPPED;
    node->rn_flags = 0;
    node->rn_actions = (resource_act_t *) act_dup(curres->r_actions);

    if (parent) {
        /* Independent subtree / non-critical for top-level is
         * not useful and can interfere with restart thresholds for
         * non critical resources */
        snprintf(tok, sizeof(tok), "%s/@__independent_subtree", base);
        if (conf_get(tok, &ref) == 0) {
            if (atoi(ref) == 1 || strcasecmp(ref, "yes") == 0)
                node->rn_flags |= RF_INDEPENDENT;
            if (atoi(ref) == 2 || strcasecmp(ref, "non-critical") == 0) {
                curres->r_flags |= RF_NON_CRITICAL;
            }
            free(ref);
        }
    }

    snprintf(tok, sizeof(tok), "%s/@__enforce_timeouts", base);
    if (conf_get(tok, &ref) == 0) {
        if (atoi(ref) > 0 || strcasecmp(ref, "yes") == 0)
            node->rn_flags |= RF_ENFORCE_TIMEOUTS;
        free(ref);
    }

    /* per-resource-node failures / expire times */
    snprintf(tok, sizeof(tok), "%s/@__max_failures", base);
    if (conf_get(tok, &ref) == 0) {
        max_failures = atoi(ref);
        if (max_failures < 0)
            max_failures = 0;
        free(ref);
    }

    snprintf(tok, sizeof(tok), "%s/@__failure_expire_time", base);
    if (conf_get(tok, &ref) == 0) {
        failure_expire = (time_t) expand_time(ref);
        if ((int64_t) failure_expire < 0)
            failure_expire = 0;
        free(ref);
    }

    if (max_failures && failure_expire) {
        /*
           node->rn_failure_counter = restart_init(failure_expire,
           max_failures);
         */
    }

    curres->r_refs++;

    if (curres->r_refs > 1 && (curres->r_flags & RF_NON_CRITICAL)) {
        res_build_name(tok, sizeof(tok), curres);
        fprintf(stderr, "Non-critical flag for %s is being cleared due to multiple references.\n",
                tok);
        curres->r_flags &= ~RF_NON_CRITICAL;
    }

    if (curres->r_flags & RF_NON_CRITICAL) {
        /* Independent subtree is implied if a
         * resource is non-critical
         */
        node->rn_flags |= RF_NON_CRITICAL | RF_INDEPENDENT;

    }

    assign_restart_policy(curres, parent, node, base);

    *newnode = node;

    list_insert(tree, node);

    return 0;
}

/**
   Build the resource tree.  If a new resource is defined inline, add it to
   the resource list.  All rules, however, must have already been read in.

   @param tree          Tree to modify/insert on to
   @param parent        Parent node, if one exists.
   @param rule          Rule surrounding the new node
   @param rulelist      List of all rules allowed in the tree.
   @param reslist       List of all currently defined resources
   @param base          Base CCS path.
   @see                 destroy_resource_tree
 */
#define RFL_FOUND 0x1
#define RFL_FORBID 0x2
static int
build_tree(resource_node_t ** tree,
           resource_node_t * parent,
           resource_rule_t * rule, resource_rule_t ** rulelist, resource_t ** reslist, char *base)
{
    char tok[512];
    resource_rule_t *childrule;
    resource_node_t *node;
    char *ref;
    char *tmp;
    int ccount = 0, x = 0, y = 0, flags = 0;

    //printf("DESCEND: %s / %s\n", rule?rule->rr_type:"(none)", base);

    /* Pass 1: typed / defined children */
    for (y = 0; rule && rule->rr_childtypes && rule->rr_childtypes[y].rc_name; y++) {

        flags = 0;
        list_for(rulelist, childrule, x) {
            if (strcmp(rule->rr_childtypes[y].rc_name, childrule->rr_type))
                continue;

            flags |= RFL_FOUND;

            if (rule->rr_childtypes[y].rc_forbid)
                flags |= RFL_FORBID;

            break;
        }

        if (flags & RFL_FORBID)
            /* Allow all *but* forbidden */
            continue;

        if (!(flags & RFL_FOUND))
            /* Not found?  Wait for pass 2 */
            continue;

        //printf("looking for %s %s @ %s\n",
        //rule->rr_childtypes[y].rc_name,
        //childrule->rr_type, base);
        for (x = 1;; x++) {

            /* Search for base/type[x]/@ref - reference an existing
               resource */
            snprintf(tok, sizeof(tok), "%s/%s[%d]", base, childrule->rr_type, x);

            flags = 1;
            switch (do_load_resource(tok, childrule, tree, reslist, parent, &node)) {
                case -1:
                    continue;
                case 1:
                    /* 1 == no more */
                    //printf("No resource found @ %s\n", tok);
                    flags = 0;
                    break;
                case 0:
                    break;
            }
            if (!flags)
                break;

            /* Got a child :: bump count */
            snprintf(tok, sizeof(tok), "%s/%s[%d]", base, childrule->rr_type, x);

            /* Kaboom */
            build_tree(&node->rn_child, node, childrule, rulelist, reslist, tok);

        }
    }

    /* Pass 2: untyped children */
    for (ccount = 1;; ccount++) {
        snprintf(tok, sizeof(tok), "%s/child::*[%d]", base, ccount);

        if (conf_get(tok, &ref) != 0) {
            /* End of the line. */
            //printf("End of the line: %s\n", tok);
            break;
        }

        tmp = strchr(ref, '=');
        if (tmp) {
            *tmp = 0;
        } else {
            /* no = sign... bad */
            free(ref);
            continue;
        }

        /* Find the resource rule */
        flags = 0;
        list_for(rulelist, childrule, x) {
            if (!strcasecmp(childrule->rr_type, ref)) {
                /* Ok, matching rule found */
                flags = 1;
                break;
            }
        }
        /* No resource rule matching the child?  Press on... */
        if (!flags) {
            free(ref);
            continue;
        }

        flags = 0;
        /* Don't descend on anything we should have already picked
           up on in the above loop */
        for (y = 0; rule && rule->rr_childtypes && rule->rr_childtypes[y].rc_name; y++) {
            /* SKIP defined child types of any type */
            if (strcmp(rule->rr_childtypes[y].rc_name, ref))
                continue;
            if (rule->rr_childtypes[y].rc_flags == 0) {
                /* 2 = defined as a real child */
                flags = 2;
                break;
            }

            flags = 1;
            break;
        }

        free(ref);
        if (flags == 2)
            continue;

        x = 1;
        switch (do_load_resource(tok, childrule, tree, reslist, parent, &node)) {
            case -1:
                continue;
            case 1:
                /* no more found */
                x = 0;
                fprintf(stderr, "No resource found @ %s\n", tok);
                break;
            case 0:
                /* another is found */
                break;
        }
        if (!x)                 /* no more found */
            break;

        /* childrule = rule set of this child at this point */
        /* tok = set above; if we got this far, we're all set */
        /* Kaboom */

        build_tree(&node->rn_child, node, childrule, rulelist, reslist, tok);
    }

    //printf("ASCEND: %s / %s\n", rule?rule->rr_type:"(none)", base);
    return 0;
}

/**
   Set up to call build_tree.  Hides the nastiness from the user.

   @param tree          Tree pointer.  Should start as a pointer to NULL.
   @param rulelist      List of all rules allowed
   @param reslist       List of all currently defined resources
   @return              0
   @see                 build_tree destroy_resource_tree
 */
int
build_resource_tree(resource_node_t ** tree, resource_rule_t ** rulelist, resource_t ** reslist)
{
    resource_node_t *root = NULL;
    char tok[512];

    snprintf(tok, sizeof(tok), "%s", RESOURCE_TREE_ROOT);

    /* Find and build the list of root nodes */
    build_tree(&root, NULL, NULL /*curr */ , rulelist, reslist, tok);

    if (root)
        *tree = root;

    return 0;
}

/**
   Deconstruct a resource tree.

   @param tree          Tree to obliterate.
   @see                 build_resource_tree
 */
void
destroy_resource_tree(resource_node_t ** tree)
{
    resource_node_t *node;

    while ((node = *tree)) {
        if ((*tree)->rn_child)
            destroy_resource_tree(&(*tree)->rn_child);

        list_remove(tree, node);

        if (node->rn_actions) {
            free(node->rn_actions);
        }
        free(node);
    }
}

static inline int
_do_child_levels(xmlNode ** xpp, xmlNode * rmp, resource_node_t ** tree, resource_t * first)
{
    resource_node_t *node = *tree;
    resource_t *res = node->rn_resource;
    resource_rule_t *rule = res->r_rule;
    int l, lev, x, rv = 0;

    for (l = 1; l <= RESOURCE_MAX_LEVELS; l++) {

        for (x = 0; rule->rr_childtypes && rule->rr_childtypes[x].rc_name; x++) {

            lev = rule->rr_childtypes[x].rc_startlevel;

            if (!lev || lev != l)
                continue;

            /* Do op on all children at our level */
            rv |= _res_op(xpp, rmp, &node->rn_child, first, rule->rr_childtypes[x].rc_name);

            if (rv & SFL_FAILURE)
                return rv;
        }

        if (rv != 0)
            return rv;
    }

    return rv;
}

static inline int
_xx_child_internal(xmlNode ** xpp, xmlNode * rmp, resource_node_t * node, resource_t * first,
                   resource_node_t * child)
{
    int x;
    resource_rule_t *rule = node->rn_resource->r_rule;

    for (x = 0; rule->rr_childtypes && rule->rr_childtypes[x].rc_name; x++) {
        if (!strcmp(child->rn_resource->r_rule->rr_type, rule->rr_childtypes[x].rc_name)) {
            if (rule->rr_childtypes[x].rc_startlevel || rule->rr_childtypes[x].rc_stoplevel) {
                return 0;
            }
        }
    }

    return _res_op_internal(xpp, rmp, &child, first, child->rn_resource->r_rule->rr_type, child);
}

static inline int
_do_child_default_level(xmlNode ** xpp, xmlNode * rmp, resource_node_t ** tree, resource_t * first)
{
    resource_node_t *node = *tree, *child;
    int y, rv = 0;

    list_for(&node->rn_child, child, y) {
        rv |= _xx_child_internal(xpp, rmp, node, first, child);

        if (rv & SFL_FAILURE)
            return rv;
    }

    return rv;
}

/**
   Nasty codependent function.  Perform an operation by numerical level
   at some point in the tree.  This allows indirectly-dependent resources
   (such as IP addresses and user scripts) to have ordering without requiring
   a direct dependency.

   @param tree          Resource tree to search/perform operations on
   @param first         Resource we're looking to perform the operation on,
                        if one exists.
   @param ret           Unused, but will be used to store status information
                        such as resources consumed, etc, in the future.
   @param op            Operation to perform if either first is found,
                        or no first is declared (in which case, all nodes
                        in the subtree).
   @see                 _res_op res_exec
 */
static int
_res_op_by_level(xmlNode ** xpp, xmlNode * rmp, resource_node_t ** tree, resource_t * first)
{
    resource_node_t *node = *tree;
    resource_t *res = node->rn_resource;
    resource_rule_t *rule = res->r_rule;
    int rv = 0;

    if (!rule->rr_childtypes)
        return _res_op(xpp, rmp, &node->rn_child, first, NULL);

    rv |= _do_child_levels(xpp, rmp, tree, first);
    if (rv & SFL_FAILURE)
        return rv;

    /* default level after specified ones */
    rv |= _do_child_default_level(xpp, rmp, tree, first);

    return rv;
}

/**
   Nasty codependent function.  Perform an operation by type for all siblings
   at some point in the tree.  This allows indirectly-dependent resources
   (such as IP addresses and user scripts) to have ordering without requiring
   a direct dependency.

   @param tree          Resource tree to search/perform operations on
   @param first         Resource we're looking to perform the operation on,
                        if one exists.
   @param type          Type to look for.
   @see                 _res_op_by_level res_exec
 */
static inline int
_res_op_internal(xmlNode ** xpp, xmlNode * rmp,
                 resource_node_t __attribute__ ((unused)) ** tree,
                 resource_t * first, char *type, resource_node_t * node)
{
    int rv = 0, me;

    /* Restore default operation. */

    /* If we're starting by type, do that funky thing. */
    if (type && strlen(type) && strcmp(node->rn_resource->r_rule->rr_type, type))
        return 0;

    /* If the resource is found, all nodes in the subtree must
       have the operation performed as well. */
    me = !first || (node->rn_resource == first);

    /* Start starts before children */
    if (me) {

        rv = res_do_flatten(xpp, rmp, node, NULL, 0);

    }

    if (node->rn_child) {
        rv |= _res_op_by_level(xpp, rmp, &node, me ? NULL : first);
    }

    return rv;
}

/**
   Nasty codependent function.  Perform an operation by type for all siblings
   at some point in the tree.  This allows indirectly-dependent resources
   (such as IP addresses and user scripts) to have ordering without requiring
   a direct dependency.

   @param tree          Resource tree to search/perform operations on
   @param first         Resource we're looking to perform the operation on,
                        if one exists.
   @param type          Type to look for.
   @see                 _res_op_by_level res_exec
 */
int
_res_op(xmlNode ** xpp, xmlNode * rmp, resource_node_t ** tree, resource_t * first, char *type)
{
    resource_node_t *node;
    int count = 0, rv = 0;

    list_for(tree, node, count) {
        rv |= _res_op_internal(xpp, rmp, tree, first, type, node);

        if (rv & SFL_FAILURE)
            return rv;
    }

    return rv;
}

/**
   Flatten resources for a service and return the pointer to it.

   @param tree          Tree to search for our resource.
   @param res           Resource to start/stop
   @param ret           Unused
 */
int
res_flatten(xmlNode ** xpp, xmlNode * rmp, resource_node_t ** tree, resource_t * res)
{
    return _res_op(xpp, rmp, tree, res, NULL);
}