_GNU_SOURCE should be defined for building vs uClibc
[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 #define _GNU_SOURCE
16 #include <libubox/avl.h>
17 #include <libubox/avl-cmp.h>
18 #include "utils.h"
19 #include <regex.h>
20 #include <unistd.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24 #include <string.h>
25 #include <errno.h>
26
27 #include "../log.h"
28
29 void
30 __blobmsg_list_init(struct blobmsg_list *list, int offset, int len, blobmsg_list_cmp cmp)
31 {
32         avl_init(&list->avl, avl_strcmp, false, NULL);
33         list->node_offset = offset;
34         list->node_len = len;
35         list->cmp = cmp;
36 }
37
38 int
39 blobmsg_list_fill(struct blobmsg_list *list, void *data, int len, bool array)
40 {
41         struct avl_tree *tree = &list->avl;
42         struct blobmsg_list_node *node;
43         struct blob_attr *cur;
44         void *ptr;
45         int count = 0;
46         int rem = len;
47
48         __blob_for_each_attr(cur, data, rem) {
49                 if (!blobmsg_check_attr(cur, !array))
50                         continue;
51
52                 ptr = calloc(1, list->node_len);
53                 if (!ptr)
54                         return -1;
55
56                 node = (void *) ((char *)ptr + list->node_offset);
57                 if (array)
58                         node->avl.key = blobmsg_data(cur);
59                 else
60                         node->avl.key = blobmsg_name(cur);
61                 node->data = cur;
62                 if (avl_insert(tree, &node->avl)) {
63                         free(ptr);
64                         continue;
65                 }
66
67                 count++;
68         }
69
70         return count;
71 }
72
73 void
74 blobmsg_list_move(struct blobmsg_list *list, struct blobmsg_list *src)
75 {
76         struct blobmsg_list_node *node, *tmp;
77         void *ptr;
78
79         avl_remove_all_elements(&src->avl, node, avl, tmp) {
80                 if (avl_insert(&list->avl, &node->avl)) {
81                         ptr = ((char *) node - list->node_offset);
82                         free(ptr);
83                 }
84         }
85 }
86
87 void
88 blobmsg_list_free(struct blobmsg_list *list)
89 {
90         struct blobmsg_list_node *node, *tmp;
91         void *ptr;
92
93         avl_remove_all_elements(&list->avl, node, avl, tmp) {
94                 ptr = ((char *) node - list->node_offset);
95                 free(ptr);
96         }
97 }
98
99 bool
100 blobmsg_list_equal(struct blobmsg_list *l1, struct blobmsg_list *l2)
101 {
102         struct blobmsg_list_node *n1, *n2;
103         int count = l1->avl.count;
104
105         if (count != l2->avl.count)
106                 return false;
107
108         n1 = avl_first_element(&l1->avl, n1, avl);
109         n2 = avl_first_element(&l2->avl, n2, avl);
110
111         while (count-- > 0) {
112                 int len;
113
114                 len = blob_len(n1->data);
115                 if (len != blob_len(n2->data))
116                         return false;
117
118                 if (memcmp(n1->data, n2->data, len) != 0)
119                         return false;
120
121                 if (l1->cmp && !l1->cmp(n1, n2))
122                         return false;
123
124                 if (!count)
125                         break;
126
127                 n1 = avl_next_element(n1, avl);
128                 n2 = avl_next_element(n2, avl);
129         }
130
131         return true;
132 }
133
134 char* get_cmdline_val(const char* name, char* out, int len)
135 {
136         char line[CMDLINE_SIZE + 1], *c, *sptr;
137         int fd = open("/proc/cmdline", O_RDONLY);
138         ssize_t r = read(fd, line, sizeof(line) - 1);
139         close(fd);
140
141         if (r <= 0)
142                 return NULL;
143
144         line[r] = 0;
145
146         for (c = strtok_r(line, " \t\n", &sptr); c;
147                         c = strtok_r(NULL, " \t\n", &sptr)) {
148                 char *sep = strchr(c, '=');
149                 ssize_t klen = sep - c;
150                 if (klen < 0 || strncmp(name, c, klen) || name[klen] != 0)
151                         continue;
152
153                 strncpy(out, &sep[1], len);
154                 out[len-1] = 0;
155                 return out;
156         }
157
158         return NULL;
159 }
160
161 int patch_fd(const char *device, int fd, int flags)
162 {
163         int dfd, nfd;
164
165         if (device == NULL)
166                 device = "/dev/null";
167
168         if (*device != '/') {
169                 dfd = open("/dev", O_PATH|O_DIRECTORY);
170
171                 if (dfd < 0)
172                         return -1;
173
174                 nfd = openat(dfd, device, flags);
175
176                 close(dfd);
177         } else {
178                 nfd = open(device, flags);
179         }
180
181         if (nfd < 0 && strcmp(device, "/dev/null"))
182                 nfd = open("/dev/null", flags);
183
184         if (nfd < 0)
185                 return -1;
186
187         fd = dup2(nfd, fd);
188
189         if (nfd > STDERR_FILENO)
190                 close(nfd);
191
192         return (fd < 0) ? -1 : 0;
193 }
194
195 int patch_stdio(const char *device)
196 {
197         int fd, rv = 0;
198         const char *fdname[3] = { "stdin", "stdout", "stderr" };
199
200         for (fd = STDIN_FILENO; fd <= STDERR_FILENO; fd++) {
201                 if (patch_fd(device, fd, fd ? O_WRONLY : O_RDONLY)) {
202                         ERROR("Failed to redirect %s to %s: %d (%s)\n",
203                               fdname[fd], device, errno, strerror(errno));
204                         rv = -1;
205                 }
206         }
207
208         return rv;
209 }