hotplug.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  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. #include <sys/stat.h>
  15. #include <sys/socket.h>
  16. #include <sys/types.h>
  17. #include <sys/sysmacros.h>
  18. #include <linux/types.h>
  19. #include <linux/netlink.h>
  20. #include <libubox/blobmsg_json.h>
  21. #include <libubox/json_script.h>
  22. #include <libubox/uloop.h>
  23. #include <libubox/utils.h>
  24. #include <json-c/json.h>
  25. #include <errno.h>
  26. #include <fcntl.h>
  27. #include <unistd.h>
  28. #include <stdlib.h>
  29. #include <libgen.h>
  30. #include <grp.h>
  31. #include "../procd.h"
  32. #include "hotplug.h"
  33. #define HOTPLUG_WAIT 500
  34. struct cmd_handler;
  35. struct cmd_queue {
  36. struct list_head list;
  37. struct blob_attr *msg;
  38. struct blob_attr *data;
  39. int timeout;
  40. void (*handler)(struct blob_attr *msg, struct blob_attr *data);
  41. void (*start)(struct blob_attr *msg, struct blob_attr *data);
  42. void (*complete)(struct blob_attr *msg, struct blob_attr *data, int ret);
  43. };
  44. struct button_timeout {
  45. struct list_head list;
  46. struct uloop_timeout timeout;
  47. char *name;
  48. int seen;
  49. struct blob_attr *data;
  50. };
  51. static LIST_HEAD(cmd_queue);
  52. static LIST_HEAD(button_timer);
  53. static struct uloop_process queue_proc;
  54. static struct uloop_timeout last_event;
  55. static struct blob_buf b, button_buf;
  56. static char *rule_file;
  57. static struct blob_buf script;
  58. static struct cmd_queue *current;
  59. static void queue_add(struct cmd_handler *h, struct blob_attr *msg, struct blob_attr *data);
  60. static void handle_button_complete(struct blob_attr *msg, struct blob_attr *data, int ret);
  61. static void button_free(struct button_timeout *b)
  62. {
  63. uloop_timeout_cancel(&b->timeout);
  64. list_del(&b->list);
  65. free(b->data);
  66. free(b->name);
  67. free(b);
  68. }
  69. static void button_timeout_remove(char *button)
  70. {
  71. struct button_timeout *b, *c;
  72. if (!list_empty(&button_timer)) list_for_each_entry_safe(b, c, &button_timer, list) {
  73. if (!strcmp(b->name, button))
  74. button_free(b);
  75. }
  76. }
  77. static char *hotplug_msg_find_var(struct blob_attr *msg, const char *name)
  78. {
  79. struct blob_attr *cur;
  80. int rem;
  81. blobmsg_for_each_attr(cur, msg, rem) {
  82. if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
  83. continue;
  84. if (strcmp(blobmsg_name(cur), name) != 0)
  85. continue;
  86. return blobmsg_data(cur);
  87. }
  88. return NULL;
  89. }
  90. static void chgrp_error(const char *group, const char *target, const char *failed)
  91. {
  92. ERROR("cannot set group %s for %s (%s: %d)\n",
  93. group, target, failed, errno);
  94. }
  95. static void chgrp_target(struct blob_attr *bgroup, struct blob_attr *btarget)
  96. {
  97. int ret = 0;
  98. struct group *g = NULL;
  99. const char *group = blobmsg_get_string(bgroup);
  100. const char *target = blobmsg_get_string(btarget);
  101. errno = 0;
  102. g = getgrnam(group);
  103. if (!g)
  104. return chgrp_error(group, target, "getgrnam");
  105. ret = chown(target, 0, g->gr_gid);
  106. if (ret < 0)
  107. return chgrp_error(group, target, "chown");
  108. }
  109. static void handle_makedev(struct blob_attr *msg, struct blob_attr *data)
  110. {
  111. unsigned int oldumask = umask(0);
  112. static struct blobmsg_policy mkdev_policy[3] = {
  113. { .type = BLOBMSG_TYPE_STRING },
  114. { .type = BLOBMSG_TYPE_STRING },
  115. { .type = BLOBMSG_TYPE_STRING },
  116. };
  117. struct blob_attr *tb[3];
  118. char *minor = hotplug_msg_find_var(msg, "MINOR");
  119. char *major = hotplug_msg_find_var(msg, "MAJOR");
  120. char *subsystem = hotplug_msg_find_var(msg, "SUBSYSTEM");
  121. blobmsg_parse_array(mkdev_policy, 3, tb, blobmsg_data(data), blobmsg_data_len(data));
  122. if (tb[0] && tb[1] && minor && major && subsystem) {
  123. mode_t m = S_IFCHR;
  124. char *d = strdup(blobmsg_get_string(tb[0]));
  125. d = dirname(d);
  126. mkdir_p(d, 0755);
  127. free(d);
  128. if (!strcmp(subsystem, "block"))
  129. m = S_IFBLK;
  130. mknod(blobmsg_get_string(tb[0]),
  131. m | strtoul(blobmsg_data(tb[1]), NULL, 8),
  132. makedev(atoi(major), atoi(minor)));
  133. if (tb[2])
  134. chgrp_target(tb[2], tb[0]);
  135. }
  136. umask(oldumask);
  137. }
  138. static void handle_rm(struct blob_attr *msg, struct blob_attr *data)
  139. {
  140. static struct blobmsg_policy rm_policy = {
  141. .type = BLOBMSG_TYPE_STRING,
  142. };
  143. struct blob_attr *tb;
  144. blobmsg_parse_array(&rm_policy, 1, &tb, blobmsg_data(data), blobmsg_data_len(data));
  145. if (tb)
  146. unlink(blobmsg_data(tb));
  147. }
  148. static void handle_exec(struct blob_attr *msg, struct blob_attr *data)
  149. {
  150. char *argv[8];
  151. struct blob_attr *cur;
  152. int rem, fd;
  153. int i = 0;
  154. blobmsg_for_each_attr(cur, msg, rem)
  155. setenv(blobmsg_name(cur), blobmsg_data(cur), 1);
  156. blobmsg_for_each_attr(cur, data, rem) {
  157. argv[i] = blobmsg_data(cur);
  158. i++;
  159. if (i == 7)
  160. break;
  161. }
  162. if (debug < 3) {
  163. fd = open("/dev/null", O_RDWR);
  164. if (fd > -1) {
  165. dup2(fd, STDIN_FILENO);
  166. dup2(fd, STDOUT_FILENO);
  167. dup2(fd, STDERR_FILENO);
  168. if (fd > STDERR_FILENO)
  169. close(fd);
  170. }
  171. }
  172. if (i > 0) {
  173. argv[i] = NULL;
  174. execvp(argv[0], &argv[0]);
  175. }
  176. exit(EXIT_FAILURE);
  177. }
  178. static void handle_button_start(struct blob_attr *msg, struct blob_attr *data)
  179. {
  180. char *button = hotplug_msg_find_var(msg, "BUTTON");
  181. if (button)
  182. button_timeout_remove(button);
  183. }
  184. static void handle_firmware(struct blob_attr *msg, struct blob_attr *data)
  185. {
  186. char *dir = blobmsg_get_string(blobmsg_data(data));
  187. char *file = hotplug_msg_find_var(msg, "FIRMWARE");
  188. char *dev = hotplug_msg_find_var(msg, "DEVPATH");
  189. struct stat s = { 0 };
  190. char *path, loadpath[256], syspath[256];
  191. int fw, src, load, len;
  192. static char buf[4096];
  193. P_DEBUG(2, "Firmware request for %s/%s\n", dir, file);
  194. if (!file || !dir || !dev) {
  195. ERROR("Request for unknown firmware %s/%s\n", dir, file);
  196. exit(EXIT_FAILURE);
  197. }
  198. path = alloca(strlen(dir) + strlen(file) + 2);
  199. sprintf(path, "%s/%s", dir, file);
  200. if (stat(path, &s)) {
  201. ERROR("Could not find firmware %s: %m\n", path);
  202. src = -1;
  203. s.st_size = 0;
  204. goto send_to_kernel;
  205. }
  206. src = open(path, O_RDONLY);
  207. if (src < 0) {
  208. ERROR("Failed to open %s: %m\n", path);
  209. s.st_size = 0;
  210. goto send_to_kernel;
  211. }
  212. send_to_kernel:
  213. snprintf(loadpath, sizeof(loadpath), "/sys/%s/loading", dev);
  214. load = open(loadpath, O_WRONLY);
  215. if (!load) {
  216. ERROR("Failed to open %s: %m\n", loadpath);
  217. exit(EXIT_FAILURE);
  218. }
  219. if (write(load, "1", 1) == -1) {
  220. ERROR("Failed to write to %s: %m\n", loadpath);
  221. exit(EXIT_FAILURE);
  222. }
  223. close(load);
  224. snprintf(syspath, sizeof(syspath), "/sys/%s/data", dev);
  225. fw = open(syspath, O_WRONLY);
  226. if (fw < 0) {
  227. ERROR("Failed to open %s: %m\n", syspath);
  228. exit(EXIT_FAILURE);
  229. }
  230. len = s.st_size;
  231. while (len) {
  232. len = read(src, buf, sizeof(buf));
  233. if (len <= 0)
  234. break;
  235. if (write(fw, buf, len) == -1) {
  236. ERROR("failed to write firmware file %s/%s to %s: %m\n", dir, file, dev);
  237. break;
  238. }
  239. }
  240. if (src >= 0)
  241. close(src);
  242. close(fw);
  243. load = open(loadpath, O_WRONLY);
  244. if (write(load, "0", 1) == -1)
  245. ERROR("failed to write to %s: %m\n", loadpath);
  246. close(load);
  247. P_DEBUG(2, "Done loading %s\n", path);
  248. exit(EXIT_FAILURE);
  249. }
  250. static void handle_start_console(struct blob_attr *msg, struct blob_attr *data)
  251. {
  252. char *dev = blobmsg_get_string(blobmsg_data(data));
  253. P_DEBUG(2, "Start console request for %s\n", dev);
  254. procd_inittab_run("respawn");
  255. procd_inittab_run("askfirst");
  256. P_DEBUG(2, "Done starting console for %s\n", dev);
  257. exit(EXIT_FAILURE);
  258. }
  259. enum {
  260. HANDLER_MKDEV = 0,
  261. HANDLER_RM,
  262. HANDLER_EXEC,
  263. HANDLER_BUTTON,
  264. HANDLER_FW,
  265. HANDLER_START_CONSOLE,
  266. };
  267. static struct cmd_handler {
  268. char *name;
  269. int atomic;
  270. void (*handler)(struct blob_attr *msg, struct blob_attr *data);
  271. void (*start)(struct blob_attr *msg, struct blob_attr *data);
  272. void (*complete)(struct blob_attr *msg, struct blob_attr *data, int ret);
  273. } handlers[] = {
  274. [HANDLER_MKDEV] = {
  275. .name = "makedev",
  276. .atomic = 1,
  277. .handler = handle_makedev,
  278. },
  279. [HANDLER_RM] = {
  280. .name = "rm",
  281. .atomic = 1,
  282. .handler = handle_rm,
  283. },
  284. [HANDLER_EXEC] = {
  285. .name = "exec",
  286. .handler = handle_exec,
  287. },
  288. [HANDLER_BUTTON] = {
  289. .name = "button",
  290. .handler = handle_exec,
  291. .start = handle_button_start,
  292. .complete = handle_button_complete,
  293. },
  294. [HANDLER_FW] = {
  295. .name = "load-firmware",
  296. .handler = handle_firmware,
  297. },
  298. [HANDLER_START_CONSOLE] = {
  299. .name = "start-console",
  300. .handler = handle_start_console,
  301. },
  302. };
  303. static void queue_next(void)
  304. {
  305. struct cmd_queue *c;
  306. if (queue_proc.pending || list_empty(&cmd_queue))
  307. return;
  308. c = list_first_entry(&cmd_queue, struct cmd_queue, list);
  309. queue_proc.pid = fork();
  310. if (!queue_proc.pid) {
  311. uloop_done();
  312. c->handler(c->msg, c->data);
  313. exit(0);
  314. }
  315. if (c->start)
  316. c->start(c->msg, c->data);
  317. list_del(&c->list);
  318. if (c->complete)
  319. current = c;
  320. else
  321. free(c);
  322. if (queue_proc.pid <= 0) {
  323. queue_next();
  324. return;
  325. }
  326. uloop_process_add(&queue_proc);
  327. P_DEBUG(4, "Launched hotplug exec instance, pid=%d\n", (int) queue_proc.pid);
  328. }
  329. static void queue_proc_cb(struct uloop_process *c, int ret)
  330. {
  331. P_DEBUG(4, "Finished hotplug exec instance, pid=%d\n", (int) c->pid);
  332. if (current) {
  333. current->complete(current->msg, current->data, ret);
  334. free(current);
  335. current = NULL;
  336. }
  337. queue_next();
  338. }
  339. static void queue_add(struct cmd_handler *h, struct blob_attr *msg, struct blob_attr *data)
  340. {
  341. struct cmd_queue *c = NULL;
  342. struct blob_attr *_msg, *_data;
  343. c = calloc_a(sizeof(struct cmd_queue),
  344. &_msg, blob_pad_len(msg),
  345. &_data, blob_pad_len(data),
  346. NULL);
  347. if (!c)
  348. return;
  349. c->msg = _msg;
  350. c->data = _data;
  351. memcpy(c->msg, msg, blob_pad_len(msg));
  352. memcpy(c->data, data, blob_pad_len(data));
  353. c->handler = h->handler;
  354. c->complete = h->complete;
  355. c->start = h->start;
  356. list_add_tail(&c->list, &cmd_queue);
  357. queue_next();
  358. }
  359. static void handle_button_timeout(struct uloop_timeout *t)
  360. {
  361. struct button_timeout *b;
  362. char seen[16];
  363. b = container_of(t, struct button_timeout, timeout);
  364. blob_buf_init(&button_buf, 0);
  365. blobmsg_add_string(&button_buf, "BUTTON", b->name);
  366. blobmsg_add_string(&button_buf, "ACTION", "timeout");
  367. snprintf(seen, sizeof(seen), "%d", b->seen);
  368. blobmsg_add_string(&button_buf, "SEEN", seen);
  369. queue_add(&handlers[HANDLER_EXEC], button_buf.head, b->data);
  370. button_free(b);
  371. }
  372. static void handle_button_complete(struct blob_attr *msg, struct blob_attr *data, int ret)
  373. {
  374. char *name = hotplug_msg_find_var(msg, "BUTTON");
  375. struct button_timeout *b;
  376. int timeout = ret >> 8;
  377. if (!timeout)
  378. return;
  379. if (!name)
  380. return;
  381. b = calloc(1, sizeof(*b));
  382. if (!b)
  383. return;
  384. b->data = malloc(blob_pad_len(data));
  385. b->name = strdup(name);
  386. b->seen = timeout;
  387. memcpy(b->data, data, blob_pad_len(data));
  388. b->timeout.cb = handle_button_timeout;
  389. uloop_timeout_set(&b->timeout, timeout * 1000);
  390. list_add(&b->list, &button_timer);
  391. }
  392. static const char* rule_handle_var(struct json_script_ctx *ctx, const char *name, struct blob_attr *vars)
  393. {
  394. const char *str, *sep;
  395. if (!strcmp(name, "DEVICENAME") || !strcmp(name, "DEVNAME")) {
  396. str = json_script_find_var(ctx, vars, "DEVPATH");
  397. if (!str)
  398. return NULL;
  399. sep = strrchr(str, '/');
  400. if (sep)
  401. return sep + 1;
  402. return str;
  403. }
  404. return NULL;
  405. }
  406. static struct json_script_file *
  407. rule_handle_file(struct json_script_ctx *ctx, const char *name)
  408. {
  409. json_object *obj;
  410. obj = json_object_from_file((char*)name);
  411. if (!obj)
  412. return NULL;
  413. blob_buf_init(&script, 0);
  414. blobmsg_add_json_element(&script, "", obj);
  415. return json_script_file_from_blobmsg(name, blob_data(script.head), blob_len(script.head));
  416. }
  417. static void rule_handle_command(struct json_script_ctx *ctx, const char *name,
  418. struct blob_attr *data, struct blob_attr *vars)
  419. {
  420. struct blob_attr *cur;
  421. int rem, i;
  422. if (debug > 3) {
  423. P_DEBUG(4, "Command: %s\n", name);
  424. blobmsg_for_each_attr(cur, data, rem)
  425. P_DEBUG(4, " %s\n", (char *) blobmsg_data(cur));
  426. P_DEBUG(4, "Message:\n");
  427. blobmsg_for_each_attr(cur, vars, rem)
  428. P_DEBUG(4, " %s=%s\n", blobmsg_name(cur), (char *) blobmsg_data(cur));
  429. }
  430. for (i = 0; i < ARRAY_SIZE(handlers); i++)
  431. if (!strcmp(handlers[i].name, name)) {
  432. if (handlers[i].atomic)
  433. handlers[i].handler(vars, data);
  434. else
  435. queue_add(&handlers[i], vars, data);
  436. break;
  437. }
  438. if (last_event.cb)
  439. uloop_timeout_set(&last_event, HOTPLUG_WAIT);
  440. }
  441. static void rule_handle_error(struct json_script_ctx *ctx, const char *msg,
  442. struct blob_attr *context)
  443. {
  444. char *s;
  445. s = blobmsg_format_json(context, false);
  446. ERROR("ERROR: %s in block: %s\n", msg, s);
  447. free(s);
  448. }
  449. static struct json_script_ctx jctx = {
  450. .handle_var = rule_handle_var,
  451. .handle_error = rule_handle_error,
  452. .handle_command = rule_handle_command,
  453. .handle_file = rule_handle_file,
  454. };
  455. static void hotplug_handler_debug(struct blob_attr *data)
  456. {
  457. char *str;
  458. if (debug < 3)
  459. return;
  460. str = blobmsg_format_json(data, true);
  461. P_DEBUG(3, "%s\n", str);
  462. free(str);
  463. }
  464. static void hotplug_handler(struct uloop_fd *u, unsigned int ev)
  465. {
  466. int i = 0;
  467. static char buf[4096];
  468. int len = recv(u->fd, buf, sizeof(buf) - 1, MSG_DONTWAIT);
  469. void *index;
  470. if (len < 1)
  471. return;
  472. buf[len] = '\0';
  473. blob_buf_init(&b, 0);
  474. index = blobmsg_open_table(&b, NULL);
  475. while (i < len) {
  476. int l = strlen(buf + i) + 1;
  477. char *e = strstr(&buf[i], "=");
  478. if (e) {
  479. *e = '\0';
  480. blobmsg_add_string(&b, &buf[i], &e[1]);
  481. }
  482. i += l;
  483. }
  484. blobmsg_close_table(&b, index);
  485. hotplug_handler_debug(b.head);
  486. json_script_run(&jctx, rule_file, blob_data(b.head));
  487. }
  488. static struct uloop_fd hotplug_fd = {
  489. .cb = hotplug_handler,
  490. };
  491. void hotplug_last_event(uloop_timeout_handler handler)
  492. {
  493. last_event.cb = handler;
  494. if (handler)
  495. uloop_timeout_set(&last_event, HOTPLUG_WAIT);
  496. else
  497. uloop_timeout_cancel(&last_event);
  498. }
  499. void hotplug(char *rules)
  500. {
  501. struct sockaddr_nl nls = {};
  502. int nlbufsize = 512 * 1024;
  503. rule_file = strdup(rules);
  504. nls.nl_family = AF_NETLINK;
  505. nls.nl_pid = 0;
  506. nls.nl_groups = -1;
  507. if ((hotplug_fd.fd = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_KOBJECT_UEVENT)) == -1) {
  508. ERROR("Failed to open hotplug socket: %m\n");
  509. exit(1);
  510. }
  511. if (bind(hotplug_fd.fd, (void *)&nls, sizeof(struct sockaddr_nl))) {
  512. ERROR("Failed to bind hotplug socket: %m\n");
  513. exit(1);
  514. }
  515. if (setsockopt(hotplug_fd.fd, SOL_SOCKET, SO_RCVBUFFORCE, &nlbufsize, sizeof(nlbufsize)))
  516. ERROR("Failed to resize receive buffer: %m\n");
  517. json_script_init(&jctx);
  518. queue_proc.cb = queue_proc_cb;
  519. uloop_fd_add(&hotplug_fd, ULOOP_READ);
  520. }
  521. int hotplug_run(char *rules)
  522. {
  523. uloop_init();
  524. hotplug(rules);
  525. uloop_run();
  526. return 0;
  527. }
  528. void hotplug_shutdown(void)
  529. {
  530. uloop_fd_delete(&hotplug_fd);
  531. close(hotplug_fd.fd);
  532. }