hotplug-dispatch.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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 (!strlen(++tmp))
  200. continue;
  201. if (!strcmp(enve, "ASYNC=0"))
  202. async = false;
  203. envle = calloc(1, sizeof(struct envlist));
  204. if (!envle)
  205. goto err_envle;
  206. envle->env = strdup(enve);
  207. if (!envle->env) {
  208. free(envle);
  209. goto err_envle;
  210. }
  211. envle->avl.key = envle->env;
  212. if (avl_insert(&env, &envle->avl)) {
  213. /* do not override existing env values, just skip */
  214. free((void*)envle->env);
  215. free(envle);
  216. }
  217. }
  218. /* synchronous calls are unsupported for now */
  219. if (!async) {
  220. err = UBUS_STATUS_NOT_SUPPORTED;
  221. goto err_envle;
  222. }
  223. /* allocating new environment */
  224. avl_for_each_element(&env, envle, avl)
  225. ++envz;
  226. envp = calloc(envz + 1, sizeof(char *));
  227. if (!envp)
  228. goto err_envle;
  229. /* populating new environment */
  230. envz = 0;
  231. avl_for_each_element_safe(&env, envle, avl, p) {
  232. envp[envz++] = envle->env;
  233. avl_delete(&env, &envle->avl);
  234. free(envle);
  235. }
  236. pc = calloc(1, sizeof(struct hotplug_process));
  237. if (!pc) {
  238. env_free(envp);
  239. return UBUS_STATUS_UNKNOWN_ERROR;
  240. }
  241. pc->timeout.cb = hotplug_exec;
  242. pc->envp = envp;
  243. pc->cnt = 0;
  244. pc->ubus = obj;
  245. /* glob'ing for hotplug scripts */
  246. if (asprintf(&globstr, "%s/%s/*", HOTPLUG_BASEDIR, subsys) == -1) {
  247. hotplug_free(pc);
  248. return UBUS_STATUS_UNKNOWN_ERROR;
  249. }
  250. if (glob(globstr, GLOB_DOOFFS, NULL, &pc->globbuf)) {
  251. free(globstr);
  252. hotplug_free(pc);
  253. return UBUS_STATUS_OK;
  254. }
  255. free(globstr);
  256. /* asynchronous call to hotplug_exec() */
  257. uloop_timeout_set(&pc->timeout, 50);
  258. return UBUS_STATUS_OK;
  259. err_envle:
  260. avl_for_each_element_safe(&env, envle, avl, p) {
  261. if (envle->env)
  262. free(envle->env);
  263. avl_delete(&env, &envle->avl);
  264. free(envle);
  265. }
  266. return err;
  267. }
  268. static const struct ubus_method hotplug_methods[] = {
  269. UBUS_METHOD("call", hotplug_call, hotplug_policy),
  270. };
  271. static struct ubus_object_type hotplug_object_type =
  272. UBUS_OBJECT_TYPE("hotplug", hotplug_methods);
  273. static void add_subsystem(int nlen, char *newname)
  274. {
  275. struct hotplug_subsys *nh = calloc(1, sizeof(struct hotplug_subsys));
  276. char *name;
  277. if (asprintf(&name, "%s%.*s", HOTPLUG_OBJECT_PREFIX, nlen, newname) == -1)
  278. exit(ENOMEM);
  279. /* prepare and add ubus object */
  280. nh->ubus.name = name;
  281. nh->ubus.type = &hotplug_object_type;
  282. nh->ubus.methods = hotplug_object_type.methods;
  283. nh->ubus.n_methods = hotplug_object_type.n_methods;
  284. list_add(&nh->list, &subsystems);
  285. ubus_add_object(ctx, &nh->ubus);
  286. }
  287. static void remove_subsystem(int nlen, char *name)
  288. {
  289. struct hotplug_subsys *n, *h;
  290. /* find match subsystem object by name or any if not given */
  291. list_for_each_entry_safe(h, n, &subsystems, list) {
  292. if (nlen && (strlen(h->ubus.name) != strnlen(name, nlen) + strlen(HOTPLUG_OBJECT_PREFIX)))
  293. continue;
  294. if (nlen && (strncmp(name, &h->ubus.name[strlen(HOTPLUG_OBJECT_PREFIX)], nlen)))
  295. continue;
  296. list_del(&h->list);
  297. ubus_remove_object(ctx, &h->ubus);
  298. free((void*)h->ubus.name);
  299. free(h);
  300. }
  301. }
  302. static int init_subsystems(void)
  303. {
  304. DIR *dir;
  305. struct dirent *dirent;
  306. dir = opendir(HOTPLUG_BASEDIR);
  307. if (dir == NULL)
  308. return ENOENT;
  309. while ((dirent = readdir(dir))) {
  310. /* skip everything but directories */
  311. if (dirent->d_type != DT_DIR)
  312. continue;
  313. /* skip '.' and '..' as well as hidden files */
  314. if (dirent->d_name[0] == '.')
  315. continue;
  316. add_subsystem(strlen(dirent->d_name), dirent->d_name);
  317. }
  318. closedir(dir);
  319. return 0;
  320. }
  321. static void inotify_read_handler(struct uloop_fd *u, unsigned int events)
  322. {
  323. int rc;
  324. char *p;
  325. struct inotify_event *in;
  326. /* read inotify events */
  327. while ((rc = read(u->fd, inotify_buffer, INOTIFY_SZ)) == -1 && errno == EINTR);
  328. if (rc <= 0)
  329. return;
  330. /* process events from buffer */
  331. for (p = inotify_buffer;
  332. rc - (p - inotify_buffer) >= (int)sizeof(struct inotify_event);
  333. p += sizeof(struct inotify_event) + in->len) {
  334. in = (struct inotify_event*)p;
  335. /* skip everything but directories */
  336. if (!(in->mask & IN_ISDIR))
  337. continue;
  338. if (in->len < 1)
  339. continue;
  340. /* skip hidden files */
  341. if (in->name[0] == '.')
  342. continue;
  343. /* add/remove subsystem objects */
  344. if (in->mask & (IN_CREATE | IN_MOVED_TO))
  345. add_subsystem(in->len, in->name);
  346. else if (in->mask & (IN_DELETE | IN_MOVED_FROM))
  347. remove_subsystem(in->len, in->name);
  348. }
  349. }
  350. void ubus_init_hotplug(struct ubus_context *newctx)
  351. {
  352. ctx = newctx;
  353. remove_subsystem(0, NULL);
  354. if (init_subsystems()) {
  355. printf("failed to initialize hotplug subsystems from %s\n", HOTPLUG_BASEDIR);
  356. return;
  357. }
  358. fd_inotify_read.fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
  359. fd_inotify_read.cb = inotify_read_handler;
  360. if (fd_inotify_read.fd == -1) {
  361. printf("failed to initialize inotify handler for %s\n", HOTPLUG_BASEDIR);
  362. return;
  363. }
  364. inotify_buffer = calloc(1, INOTIFY_SZ);
  365. if (!inotify_buffer)
  366. return;
  367. if (inotify_add_watch(fd_inotify_read.fd, HOTPLUG_BASEDIR,
  368. IN_CREATE | IN_MOVED_TO | IN_DELETE | IN_MOVED_FROM | IN_ONLYDIR) == -1)
  369. return;
  370. uloop_fd_add(&fd_inotify_read, ULOOP_READ);
  371. }