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 <limits.h> 25 #include <stdlib.h> 26 #include <string.h> 27 #include <errno.h> 28 29 #include "../log.h" 30 31 #ifndef O_PATH 32 #define O_PATH 010000000 33 #endif 34 35 #define INOTIFY_SZ (sizeof(struct inotify_event) + PATH_MAX + 1) 36 37 static char *inotify_buffer; 38 39 void 40 __blobmsg_list_init(struct blobmsg_list *list, int offset, int len, blobmsg_list_cmp cmp) 41 { 42 avl_init(&list->avl, avl_strcmp, false, NULL); 43 list->node_offset = offset; 44 list->node_len = len; 45 list->cmp = cmp; 46 } 47 48 int 49 blobmsg_list_fill(struct blobmsg_list *list, void *data, int len, bool array) 50 { 51 struct avl_tree *tree = &list->avl; 52 struct blobmsg_list_node *node; 53 struct blob_attr *cur; 54 void *ptr; 55 int count = 0; 56 int rem = len; 57 58 __blob_for_each_attr(cur, data, rem) { 59 if (!blobmsg_check_attr(cur, !array)) 60 continue; 61 62 ptr = calloc(1, list->node_len); 63 if (!ptr) 64 return -1; 65 66 node = (void *) ((char *)ptr + list->node_offset); 67 if (array) 68 node->avl.key = blobmsg_data(cur); 69 else 70 node->avl.key = blobmsg_name(cur); 71 node->data = cur; 72 if (avl_insert(tree, &node->avl)) { 73 free(ptr); 74 continue; 75 } 76 77 count++; 78 } 79 80 return count; 81 } 82 83 void 84 blobmsg_list_move(struct blobmsg_list *list, struct blobmsg_list *src) 85 { 86 struct blobmsg_list_node *node, *tmp; 87 void *ptr; 88 89 avl_remove_all_elements(&src->avl, node, avl, tmp) { 90 if (avl_insert(&list->avl, &node->avl)) { 91 ptr = ((char *) node - list->node_offset); 92 free(ptr); 93 } 94 } 95 } 96 97 void 98 blobmsg_list_free(struct blobmsg_list *list) 99 { 100 struct blobmsg_list_node *node, *tmp; 101 void *ptr; 102 103 avl_remove_all_elements(&list->avl, node, avl, tmp) { 104 ptr = ((char *) node - list->node_offset); 105 free(ptr); 106 } 107 } 108 109 bool 110 blobmsg_list_equal(struct blobmsg_list *l1, struct blobmsg_list *l2) 111 { 112 struct blobmsg_list_node *n1, *n2; 113 int count = l1->avl.count; 114 115 if (count != l2->avl.count) 116 return false; 117 118 n1 = avl_first_element(&l1->avl, n1, avl); 119 n2 = avl_first_element(&l2->avl, n2, avl); 120 121 while (count-- > 0) { 122 int len; 123 124 len = blob_len(n1->data); 125 if (len != blob_len(n2->data)) 126 return false; 127 128 if (memcmp(n1->data, n2->data, len) != 0) 129 return false; 130 131 if (l1->cmp && !l1->cmp(n1, n2)) 132 return false; 133 134 if (!count) 135 break; 136 137 n1 = avl_next_element(n1, avl); 138 n2 = avl_next_element(n2, avl); 139 } 140 141 return true; 142 } 143 144 char *get_active_console(char *out, int len) 145 { 146 char line[CMDLINE_SIZE + 1]; 147 int fd = open("/sys/class/tty/console/active", O_RDONLY); 148 ssize_t r; 149 150 if (fd < 0) 151 return NULL; 152 153 r = read(fd, line, sizeof(line) - 1); 154 line[CMDLINE_SIZE] = '\0'; 155 156 close(fd); 157 158 if (r <= 0) 159 return NULL; 160 161 /* The active file is terminated by a newline which we need to strip */ 162 char *newline = strtok(line, "\n"); 163 164 if (newline != NULL) { 165 strlcpy(out, newline, len); 166 return out; 167 } 168 169 return NULL; 170 } 171 172 char *get_cmdline_val_offset(const char *name, char *out, int len, int offset) 173 { 174 char line[CMDLINE_SIZE + 1], *c, *sptr; 175 int i, fd = open("/proc/cmdline", O_RDONLY); 176 ssize_t r; 177 178 if (fd < 0) 179 return NULL; 180 181 r = read(fd, line, sizeof(line) - 1); 182 close(fd); 183 184 if (r <= 0) 185 return NULL; 186 187 line[r] = 0; 188 189 for (i = 0, c = strtok_r(line, " \t\n", &sptr); c; 190 c = strtok_r(NULL, " \t\n", &sptr)) { 191 char *sep = strchr(c, '='); 192 if (sep == NULL) 193 continue; 194 195 ssize_t klen = sep - c; 196 if (strncmp(name, c, klen) || name[klen] != 0) 197 continue; 198 199 if (i++ < offset) 200 continue; 201 strlcpy(out, &sep[1], len); 202 return out; 203 } 204 205 return NULL; 206 } 207 208 int patch_fd(const char *device, int fd, int flags) 209 { 210 int dfd, nfd; 211 212 flags |= O_NOCTTY; 213 214 if (device == NULL) 215 device = "/dev/null"; 216 217 if (*device != '/') { 218 dfd = open("/dev", O_PATH|O_DIRECTORY); 219 220 if (dfd < 0) 221 return -1; 222 223 nfd = openat(dfd, device, flags); 224 225 close(dfd); 226 } else { 227 nfd = open(device, flags); 228 } 229 230 if (nfd < 0 && strcmp(device, "/dev/null")) 231 nfd = open("/dev/null", flags); 232 233 if (nfd < 0) 234 return -1; 235 236 fd = dup2(nfd, fd); 237 238 if (nfd > STDERR_FILENO) 239 close(nfd); 240 241 return (fd < 0) ? -1 : 0; 242 } 243 244 int patch_stdio(const char *device) 245 { 246 int fd, rv = 0; 247 const char *fdname[3] = { "stdin", "stdout", "stderr" }; 248 249 for (fd = STDIN_FILENO; fd <= STDERR_FILENO; fd++) { 250 if (patch_fd(device, fd, fd ? O_WRONLY : O_RDONLY)) { 251 ERROR("Failed to redirect %s to %s: %m\n", 252 fdname[fd], device); 253 rv = -1; 254 } 255 } 256 257 return rv; 258 } 259 260 static void inotify_watch_handler(struct uloop_fd *ufd, unsigned int events) 261 { 262 struct inotify_watch *w = container_of(ufd, struct inotify_watch, ufd); 263 struct inotify_event *in; 264 char *p; 265 int rc; 266 267 while ((rc = read(ufd->fd, inotify_buffer, INOTIFY_SZ)) == -1 && 268 errno == EINTR); 269 270 if (rc <= 0) 271 return; 272 273 for (p = inotify_buffer; 274 rc - (p - inotify_buffer) >= (int)sizeof(struct inotify_event); 275 p += sizeof(struct inotify_event) + in->len) { 276 in = (struct inotify_event *)p; 277 w->cb(w, in); 278 } 279 } 280 281 int inotify_watch_open(struct inotify_watch *w, inotify_watch_cb cb) 282 { 283 if (!inotify_buffer) { 284 inotify_buffer = calloc(1, INOTIFY_SZ); 285 if (!inotify_buffer) 286 return -1; 287 } 288 289 w->ufd.fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC); 290 if (w->ufd.fd == -1) 291 return -1; 292 293 w->ufd.cb = inotify_watch_handler; 294 w->cb = cb; 295 296 if (uloop_fd_add(&w->ufd, ULOOP_READ)) { 297 close(w->ufd.fd); 298 w->ufd.fd = -1; 299 return -1; 300 } 301 302 return 0; 303 } 304 305 int inotify_watch_add(struct inotify_watch *w, const char *path, uint32_t mask) 306 { 307 return inotify_add_watch(w->ufd.fd, path, mask); 308 } 309
This page was automatically generated by LXR 0.3.1. • OpenWrt