2
0

utils.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. #define _GNU_SOURCE
  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. #include "../log.h"
  26. #ifndef O_PATH
  27. #define O_PATH 010000000
  28. #endif
  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. 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. __blob_for_each_attr(cur, data, rem) {
  47. if (!blobmsg_check_attr(cur, !array))
  48. continue;
  49. ptr = calloc(1, list->node_len);
  50. if (!ptr)
  51. return -1;
  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. count++;
  63. }
  64. return count;
  65. }
  66. void
  67. blobmsg_list_move(struct blobmsg_list *list, struct blobmsg_list *src)
  68. {
  69. struct blobmsg_list_node *node, *tmp;
  70. void *ptr;
  71. avl_remove_all_elements(&src->avl, node, avl, tmp) {
  72. if (avl_insert(&list->avl, &node->avl)) {
  73. ptr = ((char *) node - list->node_offset);
  74. free(ptr);
  75. }
  76. }
  77. }
  78. void
  79. blobmsg_list_free(struct blobmsg_list *list)
  80. {
  81. struct blobmsg_list_node *node, *tmp;
  82. void *ptr;
  83. avl_remove_all_elements(&list->avl, node, avl, tmp) {
  84. ptr = ((char *) node - list->node_offset);
  85. free(ptr);
  86. }
  87. }
  88. bool
  89. blobmsg_list_equal(struct blobmsg_list *l1, struct blobmsg_list *l2)
  90. {
  91. struct blobmsg_list_node *n1, *n2;
  92. int count = l1->avl.count;
  93. if (count != l2->avl.count)
  94. return false;
  95. n1 = avl_first_element(&l1->avl, n1, avl);
  96. n2 = avl_first_element(&l2->avl, n2, avl);
  97. while (count-- > 0) {
  98. int len;
  99. len = blob_len(n1->data);
  100. if (len != blob_len(n2->data))
  101. return false;
  102. if (memcmp(n1->data, n2->data, len) != 0)
  103. return false;
  104. if (l1->cmp && !l1->cmp(n1, n2))
  105. return false;
  106. if (!count)
  107. break;
  108. n1 = avl_next_element(n1, avl);
  109. n2 = avl_next_element(n2, avl);
  110. }
  111. return true;
  112. }
  113. char *get_active_console(char *out, int len)
  114. {
  115. char line[CMDLINE_SIZE + 1];
  116. int fd = open("/sys/class/tty/console/active", O_RDONLY);
  117. ssize_t r;
  118. if (fd < 0)
  119. return NULL;
  120. r = read(fd, line, sizeof(line) - 1);
  121. line[CMDLINE_SIZE] = '\0';
  122. close(fd);
  123. if (r <= 0)
  124. return NULL;
  125. /* The active file is terminated by a newline which we need to strip */
  126. char *newline = strtok(line, "\n");
  127. if (newline != NULL) {
  128. strncpy(out, newline, len);
  129. return out;
  130. }
  131. return NULL;
  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. if (r <= 0)
  140. return NULL;
  141. line[r] = 0;
  142. for (c = strtok_r(line, " \t\n", &sptr); c;
  143. c = strtok_r(NULL, " \t\n", &sptr)) {
  144. char *sep = strchr(c, '=');
  145. if (sep == NULL)
  146. continue;
  147. ssize_t klen = sep - c;
  148. if (strncmp(name, c, klen) || name[klen] != 0)
  149. continue;
  150. strncpy(out, &sep[1], len);
  151. out[len-1] = 0;
  152. return out;
  153. }
  154. return NULL;
  155. }
  156. int patch_fd(const char *device, int fd, int flags)
  157. {
  158. int dfd, nfd;
  159. if (device == NULL)
  160. device = "/dev/null";
  161. if (*device != '/') {
  162. dfd = open("/dev", O_PATH|O_DIRECTORY);
  163. if (dfd < 0)
  164. return -1;
  165. nfd = openat(dfd, device, flags);
  166. close(dfd);
  167. } else {
  168. nfd = open(device, flags);
  169. }
  170. if (nfd < 0 && strcmp(device, "/dev/null"))
  171. nfd = open("/dev/null", flags);
  172. if (nfd < 0)
  173. return -1;
  174. fd = dup2(nfd, fd);
  175. if (nfd > STDERR_FILENO)
  176. close(nfd);
  177. return (fd < 0) ? -1 : 0;
  178. }
  179. int patch_stdio(const char *device)
  180. {
  181. int fd, rv = 0;
  182. const char *fdname[3] = { "stdin", "stdout", "stderr" };
  183. for (fd = STDIN_FILENO; fd <= STDERR_FILENO; fd++) {
  184. if (patch_fd(device, fd, fd ? O_WRONLY : O_RDONLY)) {
  185. ERROR("Failed to redirect %s to %s: %m\n",
  186. fdname[fd], device);
  187. rv = -1;
  188. }
  189. }
  190. return rv;
  191. }