service: terminate uloop after fork(), redirect stdin, out and err to /dev/null
[project/procd.git] / utils.c
1 /*
2  * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3  * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License version 2.1
7  * as published by the Free Software Foundation
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #include <libubox/avl.h>
16 #include <libubox/avl-cmp.h>
17 #include "utils.h"
18
19 void
20 __blobmsg_list_init(struct blobmsg_list *list, int offset, int len, blobmsg_list_cmp cmp)
21 {
22         avl_init(&list->avl, avl_strcmp, false, NULL);
23         list->node_offset = offset;
24         list->node_len = len;
25         list->cmp = cmp;
26 }
27
28 int
29 blobmsg_list_fill(struct blobmsg_list *list, void *data, int len, bool array)
30 {
31         struct avl_tree *tree = &list->avl;
32         struct blobmsg_list_node *node;
33         struct blob_attr *cur;
34         void *ptr;
35         int count = 0;
36         int rem = len;
37
38         __blob_for_each_attr(cur, data, rem) {
39                 if (!blobmsg_check_attr(cur, !array))
40                         continue;
41
42                 ptr = calloc(1, list->node_len);
43                 if (!ptr)
44                         return -1;
45
46                 node = (void *) ((char *)ptr + list->node_offset);
47                 if (array)
48                         node->avl.key = blobmsg_data(cur);
49                 else
50                         node->avl.key = blobmsg_name(cur);
51                 node->data = cur;
52                 if (avl_insert(tree, &node->avl)) {
53                         free(ptr);
54                         continue;
55                 }
56
57                 count++;
58         }
59
60         return count;
61 }
62
63 void
64 blobmsg_list_move(struct blobmsg_list *list, struct blobmsg_list *src)
65 {
66         struct blobmsg_list_node *node, *tmp;
67         void *ptr;
68
69         avl_remove_all_elements(&src->avl, node, avl, tmp) {
70                 if (avl_insert(&list->avl, &node->avl)) {
71                         ptr = ((char *) node - list->node_offset);
72                         free(ptr);
73                 }
74         }
75 }
76
77 void
78 blobmsg_list_free(struct blobmsg_list *list)
79 {
80         struct blobmsg_list_node *node, *tmp;
81         void *ptr;
82
83         avl_remove_all_elements(&list->avl, node, avl, tmp) {
84                 ptr = ((char *) node - list->node_offset);
85                 free(ptr);
86         }
87 }
88
89 bool
90 blobmsg_list_equal(struct blobmsg_list *l1, struct blobmsg_list *l2)
91 {
92         struct blobmsg_list_node *n1, *n2;
93         int count = l1->avl.count;
94
95         if (count != l2->avl.count)
96                 return false;
97
98         n1 = avl_first_element(&l1->avl, n1, avl);
99         n2 = avl_first_element(&l2->avl, n2, avl);
100
101         while (count-- > 0) {
102                 int len;
103
104                 len = blob_len(n1->data);
105                 if (len != blob_len(n2->data))
106                         return false;
107
108                 if (memcmp(n1->data, n2->data, len) != 0)
109                         return false;
110
111                 if (l1->cmp && !l1->cmp(n1, n2))
112                         return false;
113
114                 if (!count)
115                         break;
116
117                 n1 = avl_next_element(n1, avl);
118                 n2 = avl_next_element(n2, avl);
119         }
120
121         return true;
122 }