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