hotplug-dispatch.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*
  2. * Copyright (C) 2021 Daniel Golle <daniel@makrotopia.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU Lesser General Public License version 2.1
  6. * as published by the Free Software Foundation
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #define _GNU_SOURCE
  14. #include <sys/inotify.h>
  15. #include <sys/types.h>
  16. #include <assert.h>
  17. #include <dirent.h>
  18. #include <errno.h>
  19. #include <glob.h>
  20. #include <limits.h>
  21. #include <stdbool.h>
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <unistd.h>
  26. #include <libubox/avl.h>
  27. #include <libubox/avl-cmp.h>
  28. #include <libubox/list.h>
  29. #include <libubox/uloop.h>
  30. #include <libubus.h>
  31. #include "procd.h"
  32. #define HOTPLUG_BASEDIR "/etc/hotplug.d"
  33. #define HOTPLUG_OBJECT_PREFIX "hotplug."
  34. #define INOTIFY_SZ (sizeof(struct inotify_event) + PATH_MAX + 1)
  35. struct ubus_context *ctx;
  36. static char *inotify_buffer;
  37. static struct uloop_fd fd_inotify_read;
  38. static LIST_HEAD(subsystems);
  39. extern char **environ;
  40. struct hotplug_subsys {
  41. struct list_head list;
  42. struct ubus_object ubus;
  43. };
  44. struct envlist {
  45. struct avl_node avl;
  46. char *env;
  47. };
  48. struct hotplug_process {
  49. struct ubus_object *ubus;
  50. char **envp;
  51. struct uloop_timeout timeout;
  52. struct uloop_process process;
  53. glob_t globbuf;
  54. unsigned int cnt;
  55. int ret;
  56. };
  57. static void env_free(char **envp)
  58. {
  59. char **tmp;
  60. tmp = envp;
  61. while (*tmp)
  62. free(*(tmp++));
  63. free(envp);
  64. }
  65. static void hotplug_free(struct hotplug_process *pc)
  66. {
  67. env_free(pc->envp);
  68. globfree(&pc->globbuf);
  69. free(pc);
  70. }
  71. static void hotplug_done(struct uloop_process *c, int ret)
  72. {
  73. struct hotplug_process *pc = container_of(c, struct hotplug_process, process);
  74. pc->ret = ret;
  75. uloop_timeout_set(&pc->timeout, 50);
  76. }
  77. static void hotplug_exec(struct uloop_timeout *t)
  78. {
  79. struct hotplug_process *pc = container_of(t, struct hotplug_process, timeout);
  80. char *script;
  81. char *exec_argv[4];
  82. /* we have reached the last entry in the globbuf */
  83. if (pc->cnt == pc->globbuf.gl_pathc) {
  84. hotplug_free(pc);
  85. return;
  86. }
  87. asprintf(&script, ". /lib/functions.sh\n. %s\n", pc->globbuf.gl_pathv[pc->cnt++]);
  88. /* prepare for execve() */
  89. exec_argv[0] = "/bin/sh";
  90. exec_argv[1] = "-c";
  91. exec_argv[2] = script;
  92. exec_argv[3] = NULL;
  93. /* set callback in uloop_process */
  94. pc->process.cb = hotplug_done;
  95. pc->process.pid = fork();
  96. if (pc->process.pid == 0) {
  97. /* child */
  98. exit(execve(exec_argv[0], exec_argv, pc->envp));
  99. } else if (pc->process.pid < 0) {
  100. /* fork error */
  101. hotplug_free(pc);
  102. return;
  103. }
  104. /* parent */
  105. free(script);
  106. uloop_process_add(&pc->process);
  107. }
  108. static int avl_envcmp(const void *k1, const void *k2, void *ptr)
  109. {
  110. const char *tmp;
  111. tmp = strchr(k1, '=');
  112. if (!tmp)
  113. return -1;
  114. /*
  115. * compare the variable name only, ie. limit strncmp to check
  116. * only up to and including the '=' sign
  117. */
  118. return strncmp(k1, k2, (tmp - (char *)k1) + 1);
  119. }
  120. /* validate NULL-terminated environment variable name */
  121. static int validate_envvarname(const char *envvarname)
  122. {
  123. const char *tmp = envvarname;
  124. /* check for illegal characters in env variable name */
  125. while (tmp[0] != '\0') {
  126. if (!((tmp[0] >= 'a' && tmp[0] <= 'z') ||
  127. (tmp[0] >= 'A' && tmp[0] <= 'Z') ||
  128. (tmp[0] == '_') ||
  129. /* allow numbers unless they are at the first character */
  130. ((tmp != envvarname) && tmp[0] >= '0' && tmp[0] <= '9')))
  131. return EINVAL;
  132. ++tmp;
  133. }
  134. return 0;
  135. }
  136. enum {
  137. HOTPLUG_ENV,
  138. __HOTPLUG_MAX
  139. };
  140. static const struct blobmsg_policy hotplug_policy[__HOTPLUG_MAX] = {
  141. [HOTPLUG_ENV] = { .name = "env", .type = BLOBMSG_TYPE_ARRAY },
  142. };
  143. static int hotplug_call(struct ubus_context *ctx, struct ubus_object *obj,
  144. struct ubus_request_data *req, const char *method,
  145. struct blob_attr *msg)
  146. {
  147. const char *subsys = &obj->name[strlen(HOTPLUG_OBJECT_PREFIX)];
  148. struct blob_attr *tb[__HOTPLUG_MAX], *cur;
  149. AVL_TREE(env, avl_envcmp, false, NULL);
  150. struct envlist *envle, *p;
  151. int rem;
  152. char **envp, *globstr, *tmp, **tmpenv;
  153. size_t envz = 0;
  154. struct hotplug_process *pc;
  155. bool async = true;
  156. blobmsg_parse(hotplug_policy, __HOTPLUG_MAX, tb, blobmsg_data(msg), blobmsg_len(msg));
  157. if (!tb[HOTPLUG_ENV])
  158. return UBUS_STATUS_INVALID_ARGUMENT;
  159. tmpenv = environ;
  160. /* first adding existing environment to avl_tree */
  161. while (*tmpenv) {
  162. envle = calloc(1, sizeof(struct envlist));
  163. assert(envle != NULL);
  164. envle->env = strdup(*tmpenv);
  165. envle->avl.key = envle->env;
  166. avl_insert(&env, &envle->avl);
  167. ++tmpenv;
  168. }
  169. /* then adding additional variables from ubus call */
  170. blobmsg_for_each_attr(cur, tb[HOTPLUG_ENV], rem) {
  171. char *enve = blobmsg_get_string(cur);
  172. if (!enve)
  173. continue;
  174. if (!strncmp(enve, "LD_", 3))
  175. continue;
  176. if (!strcmp(enve, "PATH"))
  177. continue;
  178. if (strlen(enve) < 3)
  179. continue;
  180. if (!(tmp = strchr(enve, '=')))
  181. continue;
  182. *tmp = '\0';
  183. if (validate_envvarname(enve))
  184. continue;
  185. *tmp = '=';
  186. if (!strlen(++tmp))
  187. continue;
  188. if (!strcmp(enve, "ASYNC=0"))
  189. async = false;
  190. envle = calloc(1, sizeof(struct envlist));
  191. assert(envle != NULL);
  192. envle->env = strdup(enve);
  193. envle->avl.key = envle->env;
  194. if (avl_insert(&env, &envle->avl)) {
  195. free((void*)envle->env);
  196. free(envle);
  197. }
  198. ++tmpenv;
  199. }
  200. /* allocating new environment */
  201. avl_for_each_element(&env, envle, avl)
  202. ++envz;
  203. envp = calloc(envz + 1, sizeof(char *));
  204. assert(envp != NULL);
  205. /* populating new environment */
  206. envz = 0;
  207. avl_for_each_element_safe(&env, envle, avl, p) {
  208. envp[envz++] = envle->env;
  209. avl_delete(&env, &envle->avl);
  210. free(envle);
  211. }
  212. /* glob'ing for hotplug scripts */
  213. if (asprintf(&globstr, "%s/%s/*", HOTPLUG_BASEDIR, subsys) == -1) {
  214. env_free(envp);
  215. return UBUS_STATUS_UNKNOWN_ERROR;
  216. }
  217. /* synchronous calls are unsupported for now */
  218. if (!async) {
  219. env_free(envp);
  220. return UBUS_STATUS_NOT_SUPPORTED;
  221. }
  222. pc = calloc(1, sizeof(struct hotplug_process));
  223. assert(pc != NULL);
  224. pc->timeout.cb = hotplug_exec;
  225. pc->envp = envp;
  226. pc->cnt = 0;
  227. pc->ubus = obj;
  228. if (glob(globstr, GLOB_DOOFFS, NULL, &pc->globbuf)) {
  229. free(globstr);
  230. hotplug_free(pc);
  231. return UBUS_STATUS_OK;
  232. }
  233. free(globstr);
  234. /* asynchronous call to hotplug_exec() */
  235. uloop_timeout_set(&pc->timeout, 50);
  236. return UBUS_STATUS_OK;
  237. }
  238. static const struct ubus_method hotplug_methods[] = {
  239. UBUS_METHOD("call", hotplug_call, hotplug_policy),
  240. };
  241. static struct ubus_object_type hotplug_object_type =
  242. UBUS_OBJECT_TYPE("hotplug", hotplug_methods);
  243. static void add_subsystem(int nlen, char *newname)
  244. {
  245. struct hotplug_subsys *nh = calloc(1, sizeof(struct hotplug_subsys));
  246. char *name;
  247. asprintf(&name, "%s%.*s", HOTPLUG_OBJECT_PREFIX, nlen, newname);
  248. /* prepare and add ubus object */
  249. nh->ubus.name = name;
  250. nh->ubus.type = &hotplug_object_type;
  251. nh->ubus.methods = hotplug_object_type.methods;
  252. nh->ubus.n_methods = hotplug_object_type.n_methods;
  253. list_add(&nh->list, &subsystems);
  254. ubus_add_object(ctx, &nh->ubus);
  255. }
  256. static void remove_subsystem(int nlen, char *name)
  257. {
  258. struct hotplug_subsys *n, *h;
  259. /* find match subsystem object by name or any if not given */
  260. list_for_each_entry_safe(h, n, &subsystems, list) {
  261. if (nlen && (strlen(h->ubus.name) != strnlen(name, nlen) + strlen(HOTPLUG_OBJECT_PREFIX)))
  262. continue;
  263. if (nlen && (strncmp(name, &h->ubus.name[strlen(HOTPLUG_OBJECT_PREFIX)], nlen)))
  264. continue;
  265. list_del(&h->list);
  266. ubus_remove_object(ctx, &h->ubus);
  267. free((void*)h->ubus.name);
  268. free(h);
  269. }
  270. }
  271. static int init_subsystems(void)
  272. {
  273. DIR *dir;
  274. struct dirent *dirent;
  275. dir = opendir(HOTPLUG_BASEDIR);
  276. if (dir == NULL)
  277. return ENOENT;
  278. while ((dirent = readdir(dir))) {
  279. /* skip everything but directories */
  280. if (dirent->d_type != DT_DIR)
  281. continue;
  282. /* skip '.' and '..' as well as hidden files */
  283. if (dirent->d_name[0] == '.')
  284. continue;
  285. add_subsystem(strlen(dirent->d_name), dirent->d_name);
  286. }
  287. closedir(dir);
  288. return 0;
  289. }
  290. static void inotify_read_handler(struct uloop_fd *u, unsigned int events)
  291. {
  292. int rc;
  293. char *p;
  294. struct inotify_event *in;
  295. /* read inotify events */
  296. while ((rc = read(u->fd, inotify_buffer, INOTIFY_SZ)) == -1 && errno == EINTR);
  297. if (rc <= 0)
  298. return;
  299. /* process events from buffer */
  300. for (p = inotify_buffer;
  301. rc - (p - inotify_buffer) >= (int)sizeof(struct inotify_event);
  302. p += sizeof(struct inotify_event) + in->len) {
  303. in = (struct inotify_event*)p;
  304. /* skip everything but directories */
  305. if (!(in->mask & IN_ISDIR))
  306. continue;
  307. if (in->len < 1)
  308. continue;
  309. /* skip hidden files */
  310. if (in->name[0] == '.')
  311. continue;
  312. /* add/remove subsystem objects */
  313. if (in->mask & (IN_CREATE | IN_MOVED_TO))
  314. add_subsystem(in->len, in->name);
  315. else if (in->mask & (IN_DELETE | IN_MOVED_FROM))
  316. remove_subsystem(in->len, in->name);
  317. }
  318. }
  319. void ubus_init_hotplug(struct ubus_context *newctx)
  320. {
  321. ctx = newctx;
  322. remove_subsystem(0, NULL);
  323. if (init_subsystems()) {
  324. printf("failed to initialize hotplug subsystems from %s\n", HOTPLUG_BASEDIR);
  325. return;
  326. }
  327. fd_inotify_read.fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
  328. fd_inotify_read.cb = inotify_read_handler;
  329. assert(fd_inotify_read.fd != -1);
  330. inotify_buffer = calloc(1, INOTIFY_SZ);
  331. assert(inotify_buffer != NULL);
  332. inotify_add_watch(fd_inotify_read.fd, HOTPLUG_BASEDIR, IN_CREATE | IN_MOVED_TO | IN_DELETE | IN_MOVED_FROM | IN_ONLYDIR);
  333. uloop_fd_add(&fd_inotify_read, ULOOP_READ);
  334. }