hotplug-dispatch.c 10.0 KB

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