a67c00402eef435040ab85550f408a1fa97dc5dc
[project/procd.git] / utils / 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 #include <regex.h>
19 #include <unistd.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <fcntl.h>
23
24 void
25 __blobmsg_list_init(struct blobmsg_list *list, int offset, int len, blobmsg_list_cmp cmp)
26 {
27         avl_init(&list->avl, avl_strcmp, false, NULL);
28         list->node_offset = offset;
29         list->node_len = len;
30         list->cmp = cmp;
31 }
32
33 int
34 blobmsg_list_fill(struct blobmsg_list *list, void *data, int len, bool array)
35 {
36         struct avl_tree *tree = &list->avl;
37         struct blobmsg_list_node *node;
38         struct blob_attr *cur;
39         void *ptr;
40         int count = 0;
41         int rem = len;
42
43         __blob_for_each_attr(cur, data, rem) {
44                 if (!blobmsg_check_attr(cur, !array))
45                         continue;
46
47                 ptr = calloc(1, list->node_len);
48                 if (!ptr)
49                         return -1;
50
51                 node = (void *) ((char *)ptr + list->node_offset);
52                 if (array)
53                         node->avl.key = blobmsg_data(cur);
54                 else
55                         node->avl.key = blobmsg_name(cur);
56                 node->data = cur;
57                 if (avl_insert(tree, &node->avl)) {
58                         free(ptr);
59                         continue;
60                 }
61
62                 count++;
63         }
64
65         return count;
66 }
67
68 void
69 blobmsg_list_move(struct blobmsg_list *list, struct blobmsg_list *src)
70 {
71         struct blobmsg_list_node *node, *tmp;
72         void *ptr;
73
74         avl_remove_all_elements(&src->avl, node, avl, tmp) {
75                 if (avl_insert(&list->avl, &node->avl)) {
76                         ptr = ((char *) node - list->node_offset);
77                         free(ptr);
78                 }
79         }
80 }
81
82 void
83 blobmsg_list_free(struct blobmsg_list *list)
84 {
85         struct blobmsg_list_node *node, *tmp;
86         void *ptr;
87
88         avl_remove_all_elements(&list->avl, node, avl, tmp) {
89                 ptr = ((char *) node - list->node_offset);
90                 free(ptr);
91         }
92 }
93
94 bool
95 blobmsg_list_equal(struct blobmsg_list *l1, struct blobmsg_list *l2)
96 {
97         struct blobmsg_list_node *n1, *n2;
98         int count = l1->avl.count;
99
100         if (count != l2->avl.count)
101                 return false;
102
103         n1 = avl_first_element(&l1->avl, n1, avl);
104         n2 = avl_first_element(&l2->avl, n2, avl);
105
106         while (count-- > 0) {
107                 int len;
108
109                 len = blob_len(n1->data);
110                 if (len != blob_len(n2->data))
111                         return false;
112
113                 if (memcmp(n1->data, n2->data, len) != 0)
114                         return false;
115
116                 if (l1->cmp && !l1->cmp(n1, n2))
117                         return false;
118
119                 if (!count)
120                         break;
121
122                 n1 = avl_next_element(n1, avl);
123                 n2 = avl_next_element(n2, avl);
124         }
125
126         return true;
127 }
128
129 char* get_cmdline_val(const char* name, char* out, int len)
130 {
131         char line[CMDLINE_SIZE + 1], *c, *sptr;
132         int fd = open("/proc/cmdline", O_RDONLY);
133         ssize_t r = read(fd, line, sizeof(line) - 1);
134         close(fd);
135
136         if (r <= 0)
137                 return NULL;
138
139         line[r] = 0;
140
141         for (c = strtok_r(line, " \t\n", &sptr); c;
142                         c = strtok_r(NULL, " \t\n", &sptr)) {
143                 char *sep = strchr(c, '=');
144                 ssize_t klen = sep - c;
145                 if (klen < 0 || strncmp(name, c, klen) || name[klen] != 0)
146                         continue;
147
148                 strncpy(out, &sep[1], len);
149                 out[len-1] = 0;
150                 return out;
151         }
152
153         return NULL;
154 }