hotplug.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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 <linux/types.h>
  18. #include <linux/netlink.h>
  19. #include <libubox/blobmsg_json.h>
  20. #include <libubox/json_script.h>
  21. #include <libubox/uloop.h>
  22. #include <fcntl.h>
  23. #include <unistd.h>
  24. #include <stdlib.h>
  25. #include <libgen.h>
  26. #include "procd.h"
  27. #include "hotplug.h"
  28. #define HOTPLUG_WAIT 500
  29. struct cmd_queue {
  30. struct list_head list;
  31. struct blob_attr *msg;
  32. struct blob_attr *data;
  33. void (*handler)(struct blob_attr *msg, struct blob_attr *data);
  34. };
  35. static LIST_HEAD(cmd_queue);
  36. static struct uloop_process queue_proc;
  37. static struct uloop_timeout last_event;
  38. static struct blob_buf b;
  39. static char *rule_file;
  40. static struct blob_buf script;
  41. static char *hotplug_msg_find_var(struct blob_attr *msg, const char *name)
  42. {
  43. struct blob_attr *cur;
  44. int rem;
  45. blobmsg_for_each_attr(cur, msg, rem) {
  46. if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
  47. continue;
  48. if (strcmp(blobmsg_name(cur), name) != 0)
  49. continue;
  50. return blobmsg_data(cur);
  51. }
  52. return NULL;
  53. }
  54. static void mkdir_p(char *dir)
  55. {
  56. char *l = strrchr(dir, '/');
  57. if (l) {
  58. *l = '\0';
  59. mkdir_p(dir);
  60. *l = '/';
  61. mkdir(dir, 0755);
  62. }
  63. }
  64. static void handle_makedev(struct blob_attr *msg, struct blob_attr *data)
  65. {
  66. unsigned int oldumask = umask(0);
  67. static struct blobmsg_policy mkdev_policy[2] = {
  68. { .type = BLOBMSG_TYPE_STRING },
  69. { .type = BLOBMSG_TYPE_STRING },
  70. };
  71. struct blob_attr *tb[2];
  72. char *minor = hotplug_msg_find_var(msg, "MINOR");
  73. char *major = hotplug_msg_find_var(msg, "MAJOR");
  74. char *subsystem = hotplug_msg_find_var(msg, "SUBSYSTEM");
  75. blobmsg_parse_array(mkdev_policy, 2, tb, blobmsg_data(data), blobmsg_data_len(data));
  76. if (tb[0] && tb[1] && minor && major && subsystem) {
  77. mode_t m = S_IFCHR;
  78. char *d = strdup(blobmsg_get_string(tb[0]));
  79. d = dirname(d);
  80. mkdir_p(d);
  81. free(d);
  82. if (!strcmp(subsystem, "block"))
  83. m = S_IFBLK;
  84. mknod(blobmsg_get_string(tb[0]),
  85. m | strtoul(blobmsg_data(tb[1]), NULL, 8),
  86. makedev(atoi(major), atoi(minor)));
  87. }
  88. umask(oldumask);
  89. }
  90. static void handle_rm(struct blob_attr *msg, struct blob_attr *data)
  91. {
  92. static struct blobmsg_policy rm_policy = {
  93. .type = BLOBMSG_TYPE_STRING,
  94. };
  95. struct blob_attr *tb;
  96. blobmsg_parse_array(&rm_policy, 1, &tb, blobmsg_data(data), blobmsg_data_len(data));
  97. if (tb)
  98. unlink(blobmsg_data(tb));
  99. }
  100. static void handle_exec(struct blob_attr *msg, struct blob_attr *data)
  101. {
  102. char *argv[8];
  103. struct blob_attr *cur;
  104. int rem, fd;
  105. int i = 0;
  106. blobmsg_for_each_attr(cur, msg, rem)
  107. setenv(blobmsg_name(cur), blobmsg_data(cur), 1);
  108. blobmsg_for_each_attr(cur, data, rem) {
  109. argv[i] = blobmsg_data(cur);
  110. i++;
  111. if (i == 7)
  112. break;
  113. }
  114. if (debug < 2) {
  115. fd = open("/dev/null", O_RDWR);
  116. if (fd > -1) {
  117. dup2(fd, STDIN_FILENO);
  118. dup2(fd, STDOUT_FILENO);
  119. dup2(fd, STDERR_FILENO);
  120. if (fd > STDERR_FILENO)
  121. close(fd);
  122. }
  123. }
  124. if (i > 0) {
  125. argv[i] = NULL;
  126. execvp(argv[0], &argv[0]);
  127. }
  128. exit(-1);
  129. }
  130. static void handle_firmware(struct blob_attr *msg, struct blob_attr *data)
  131. {
  132. char *dir = blobmsg_get_string(blobmsg_data(data));
  133. char *file = hotplug_msg_find_var(msg, "FIRMWARE");
  134. char *dev = hotplug_msg_find_var(msg, "DEVPATH");
  135. void *fw_data;
  136. struct stat s;
  137. char *path, loadpath[256], syspath[256];
  138. int fw, load, sys, len;
  139. DEBUG(1, "Firmware request for %s/%s\n", dir, file);
  140. if (!file || !dir || !dev) {
  141. ERROR("Request for unknown firmware %s/%s\n", dir, file);
  142. exit(-1);
  143. }
  144. path = malloc(strlen(dir) + strlen(file) + 2);
  145. if (!path) {
  146. ERROR("Failed to allocate memory\n");
  147. exit(-1);
  148. }
  149. sprintf(path, "%s/%s", dir, file);
  150. if (stat(path, &s)) {
  151. ERROR("Could not find firmware %s\n", path);
  152. exit(-1);
  153. }
  154. fw_data = malloc(s.st_size);
  155. if (!fw_data) {
  156. ERROR("Failed to allocate firmware data memory\n");
  157. exit(-1);
  158. }
  159. fw = open(path, O_RDONLY);
  160. if (!fw) {
  161. ERROR("Failed to open %s\n", path);
  162. exit(-1);
  163. }
  164. if (read(fw, fw_data, s.st_size) != s.st_size) {
  165. ERROR("Failed to read firmware data\n");
  166. exit(-1);
  167. }
  168. close(fw);
  169. snprintf(loadpath, sizeof(loadpath), "/sys/%s/loading", dev);
  170. load = open(loadpath, O_WRONLY);
  171. if (!load) {
  172. ERROR("Failed to open %s\n", loadpath);
  173. exit(-1);
  174. }
  175. write(load, "1", 1);
  176. close(load);
  177. snprintf(syspath, sizeof(syspath), "/sys/%s/data", dev);
  178. sys = open(syspath, O_WRONLY);
  179. if (!sys) {
  180. ERROR("Failed to open %s\n", syspath);
  181. exit(-1);
  182. }
  183. len = s.st_size;
  184. while (len > 4096) {
  185. write(fw, fw_data, 4096);
  186. len -= 4096;
  187. }
  188. if (len)
  189. write(fw, fw_data, len);
  190. close(fw);
  191. load = open(loadpath, O_WRONLY);
  192. write(load, "0", 1);
  193. close(load);
  194. DEBUG(1, "Done loading %s\n", path);
  195. exit(-1);
  196. }
  197. static struct cmd_handler {
  198. char *name;
  199. int atomic;
  200. void (*handler)(struct blob_attr *msg, struct blob_attr *data);
  201. } handlers[] = {
  202. {
  203. .name = "makedev",
  204. .atomic = 1,
  205. .handler = handle_makedev,
  206. }, {
  207. .name = "rm",
  208. .atomic = 1,
  209. .handler = handle_rm,
  210. }, {
  211. .name = "exec",
  212. .handler = handle_exec,
  213. }, {
  214. .name = "load-firmware",
  215. .handler = handle_firmware,
  216. },
  217. };
  218. static void queue_next(void)
  219. {
  220. struct cmd_queue *c;
  221. if (queue_proc.pending || list_empty(&cmd_queue))
  222. return;
  223. c = list_first_entry(&cmd_queue, struct cmd_queue, list);
  224. queue_proc.pid = fork();
  225. if (!queue_proc.pid) {
  226. uloop_done();
  227. c->handler(c->msg, c->data);
  228. exit(0);
  229. }
  230. list_del(&c->list);
  231. free(c);
  232. if (queue_proc.pid <= 0) {
  233. queue_next();
  234. return;
  235. }
  236. uloop_process_add(&queue_proc);
  237. DEBUG(2, "Launched hotplug exec instance, pid=%d\n", (int) queue_proc.pid);
  238. }
  239. static void queue_proc_cb(struct uloop_process *c, int ret)
  240. {
  241. DEBUG(2, "Finished hotplug exec instance, pid=%d\n", (int) c->pid);
  242. queue_next();
  243. }
  244. static void queue_add(struct cmd_handler *h, struct blob_attr *msg, struct blob_attr *data)
  245. {
  246. struct cmd_queue *c = NULL;
  247. struct blob_attr *_msg, *_data;
  248. c = calloc_a(sizeof(struct cmd_queue),
  249. &_msg, blob_pad_len(msg),
  250. &_data, blob_pad_len(data),
  251. NULL);
  252. c->msg = _msg;
  253. c->data = _data;
  254. if (!c)
  255. return;
  256. memcpy(c->msg, msg, blob_pad_len(msg));
  257. memcpy(c->data, data, blob_pad_len(data));
  258. c->handler = h->handler;
  259. list_add_tail(&c->list, &cmd_queue);
  260. queue_next();
  261. }
  262. static const char* rule_handle_var(struct json_script_ctx *ctx, const char *name, struct blob_attr *vars)
  263. {
  264. const char *str, *sep;
  265. if (!strcmp(name, "DEVICENAME") || !strcmp(name, "DEVNAME")) {
  266. str = json_script_find_var(ctx, vars, "DEVPATH");
  267. if (!str)
  268. return NULL;
  269. sep = strrchr(str, '/');
  270. if (sep)
  271. return sep + 1;
  272. return str;
  273. }
  274. return NULL;
  275. }
  276. static struct json_script_file *
  277. rule_handle_file(struct json_script_ctx *ctx, const char *name)
  278. {
  279. json_object *obj;
  280. obj = json_object_from_file((char*)name);
  281. if (is_error(obj))
  282. return NULL;
  283. blob_buf_init(&script, 0);
  284. blobmsg_add_json_element(&script, "", obj);
  285. return json_script_file_from_blobmsg(name, blob_data(script.head), blob_len(script.head));
  286. }
  287. static void rule_handle_command(struct json_script_ctx *ctx, const char *name,
  288. struct blob_attr *data, struct blob_attr *vars)
  289. {
  290. struct blob_attr *cur;
  291. int rem, i;
  292. if (debug > 1) {
  293. DEBUG(2, "Command: %s", name);
  294. blobmsg_for_each_attr(cur, data, rem)
  295. DEBUG(2, " %s", (char *) blobmsg_data(cur));
  296. DEBUG(2, "\n");
  297. DEBUG(2, "Message:");
  298. blobmsg_for_each_attr(cur, vars, rem)
  299. DEBUG(2, " %s=%s", blobmsg_name(cur), (char *) blobmsg_data(cur));
  300. DEBUG(2, "\n");
  301. }
  302. for (i = 0; i < ARRAY_SIZE(handlers); i++)
  303. if (!strcmp(handlers[i].name, name)) {
  304. if (handlers[i].atomic)
  305. handlers[i].handler(vars, data);
  306. else
  307. queue_add(&handlers[i], vars, data);
  308. break;
  309. }
  310. if (last_event.cb)
  311. uloop_timeout_set(&last_event, HOTPLUG_WAIT);
  312. }
  313. static void rule_handle_error(struct json_script_ctx *ctx, const char *msg,
  314. struct blob_attr *context)
  315. {
  316. char *s;
  317. s = blobmsg_format_json(context, false);
  318. ERROR("ERROR: %s in block: %s\n", msg, s);
  319. free(s);
  320. }
  321. static struct json_script_ctx jctx = {
  322. .handle_var = rule_handle_var,
  323. .handle_error = rule_handle_error,
  324. .handle_command = rule_handle_command,
  325. .handle_file = rule_handle_file,
  326. };
  327. static void hotplug_handler(struct uloop_fd *u, unsigned int ev)
  328. {
  329. int i = 0;
  330. static char buf[4096];
  331. int len = recv(u->fd, buf, sizeof(buf), MSG_DONTWAIT);
  332. void *index;
  333. if (len < 1)
  334. return;
  335. blob_buf_init(&b, 0);
  336. index = blobmsg_open_table(&b, NULL);
  337. while (i < len) {
  338. int l = strlen(buf + i) + 1;
  339. char *e = strstr(&buf[i], "=");
  340. if (e) {
  341. *e = '\0';
  342. blobmsg_add_string(&b, &buf[i], &e[1]);
  343. }
  344. i += l;
  345. }
  346. blobmsg_close_table(&b, index);
  347. DEBUG(3, "%s\n", blobmsg_format_json(b.head, true));
  348. json_script_run(&jctx, rule_file, blob_data(b.head));
  349. }
  350. static struct uloop_fd hotplug_fd = {
  351. .cb = hotplug_handler,
  352. };
  353. void hotplug_last_event(uloop_timeout_handler handler)
  354. {
  355. last_event.cb = handler;
  356. if (handler)
  357. uloop_timeout_set(&last_event, HOTPLUG_WAIT);
  358. else
  359. uloop_timeout_cancel(&last_event);
  360. }
  361. void hotplug(char *rules)
  362. {
  363. struct sockaddr_nl nls;
  364. rule_file = strdup(rules);
  365. memset(&nls,0,sizeof(struct sockaddr_nl));
  366. nls.nl_family = AF_NETLINK;
  367. nls.nl_pid = getpid();
  368. nls.nl_groups = -1;
  369. if ((hotplug_fd.fd = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_KOBJECT_UEVENT)) == -1) {
  370. ERROR("Failed to open hotplug socket: %s\n", strerror(errno));
  371. exit(1);
  372. }
  373. if (bind(hotplug_fd.fd, (void *)&nls, sizeof(struct sockaddr_nl))) {
  374. ERROR("Failed to bind hotplug socket: %s\n", strerror(errno));
  375. exit(1);
  376. }
  377. json_script_init(&jctx);
  378. queue_proc.cb = queue_proc_cb;
  379. uloop_fd_add(&hotplug_fd, ULOOP_READ);
  380. }
  381. void hotplug_shutdown(void)
  382. {
  383. uloop_fd_delete(&hotplug_fd);
  384. close(hotplug_fd.fd);
  385. }