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