trigger.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 <libubox/blobmsg_json.h>
  18. #include <libubox/json_script.h>
  19. #include <libubox/runqueue.h>
  20. #include <libubox/ustream.h>
  21. #include <libubox/uloop.h>
  22. #include <libubox/avl.h>
  23. #include <libubox/avl-cmp.h>
  24. #include <fcntl.h>
  25. #include <unistd.h>
  26. #include <stdlib.h>
  27. #include <libgen.h>
  28. #include "../procd.h"
  29. struct trigger {
  30. struct list_head list;
  31. void *id;
  32. char *type;
  33. int timeout;
  34. struct blob_attr *rule;
  35. struct blob_attr *data;
  36. struct json_script_ctx jctx;
  37. };
  38. struct trigger_command {
  39. struct avl_node avl;
  40. struct uloop_timeout delay;
  41. bool requeue;
  42. struct runqueue_process proc;
  43. struct json_script_ctx jctx;
  44. struct blob_attr data[];
  45. };
  46. static LIST_HEAD(triggers);
  47. static RUNQUEUE(q, 1);
  48. static AVL_TREE(trigger_pending, avl_blobcmp, false, NULL);
  49. static const char* rule_handle_var(struct json_script_ctx *ctx, const char *name, struct blob_attr *vars)
  50. {
  51. return NULL;
  52. }
  53. static struct json_script_file *
  54. rule_load_script(struct json_script_ctx *ctx, const char *name)
  55. {
  56. struct trigger *t = container_of(ctx, struct trigger, jctx);
  57. if (strcmp(name, t->type) != 0)
  58. return NULL;
  59. return json_script_file_from_blobmsg(t->type, t->rule, blob_pad_len(t->rule));
  60. }
  61. static void trigger_free(struct trigger *t)
  62. {
  63. json_script_free(&t->jctx);
  64. free(t->data);
  65. list_del(&t->list);
  66. free(t);
  67. }
  68. static void trigger_command_complete(struct runqueue *q, struct runqueue_task *p)
  69. {
  70. struct trigger_command *cmd = container_of(p, struct trigger_command, proc.task);
  71. if (cmd->requeue) {
  72. cmd->requeue = false;
  73. runqueue_task_add(q, p, false);
  74. return;
  75. }
  76. avl_delete(&trigger_pending, &cmd->avl);
  77. free(cmd);
  78. }
  79. static void trigger_command_run(struct runqueue *q, struct runqueue_task *t)
  80. {
  81. struct trigger_command *cmd = container_of(t, struct trigger_command, proc.task);
  82. struct blob_attr *cur;
  83. char **argv;
  84. pid_t pid;
  85. int n = 0;
  86. int rem;
  87. int fd;
  88. pid = fork();
  89. if (pid < 0) {
  90. trigger_command_complete(q, t);
  91. return;
  92. }
  93. if (pid) {
  94. runqueue_process_add(q, &cmd->proc, pid);
  95. return;
  96. }
  97. if (debug < 3 && (fd = open("/dev/null", O_RDWR)) >= 0) {
  98. dup2(fd, STDIN_FILENO);
  99. dup2(fd, STDOUT_FILENO);
  100. dup2(fd, STDERR_FILENO);
  101. if (fd > STDERR_FILENO)
  102. close(fd);
  103. }
  104. blobmsg_for_each_attr(cur, cmd->data, rem)
  105. n++;
  106. argv = alloca((n + 1) * sizeof(*argv));
  107. n = 0;
  108. blobmsg_for_each_attr(cur, cmd->data, rem)
  109. argv[n++] = blobmsg_get_string(cur);
  110. argv[n] = NULL;
  111. if (n > 0)
  112. execvp(argv[0], &argv[0]);
  113. exit(1);
  114. }
  115. static void trigger_command_start(struct uloop_timeout *timeout)
  116. {
  117. static const struct runqueue_task_type trigger_command_type = {
  118. .run = trigger_command_run,
  119. .cancel = runqueue_process_cancel_cb,
  120. .kill = runqueue_process_kill_cb,
  121. };
  122. struct trigger_command *cmd = container_of(timeout, struct trigger_command, delay);
  123. cmd->proc.task.type = &trigger_command_type;
  124. cmd->proc.task.complete = trigger_command_complete;
  125. runqueue_task_add(&q, &cmd->proc.task, false);
  126. }
  127. static void trigger_command_add(struct trigger *t, struct blob_attr *data)
  128. {
  129. struct trigger_command *cmd;
  130. int64_t remaining;
  131. cmd = avl_find_element(&trigger_pending, data, cmd, avl);
  132. if (cmd) {
  133. /* Command currently running? */
  134. if (!cmd->delay.pending) {
  135. cmd->requeue = true;
  136. return;
  137. }
  138. /* Extend timer if trigger timeout is bigger than remaining time */
  139. remaining = uloop_timeout_remaining64(&cmd->delay);
  140. if (remaining < t->timeout)
  141. uloop_timeout_set(&cmd->delay, t->timeout);
  142. return;
  143. }
  144. cmd = calloc(1, sizeof(*cmd) + blob_pad_len(data));
  145. if (!cmd)
  146. return;
  147. cmd->avl.key = cmd->data;
  148. cmd->delay.cb = trigger_command_start;
  149. memcpy(cmd->data, data, blob_pad_len(data));
  150. avl_insert(&trigger_pending, &cmd->avl);
  151. uloop_timeout_set(&cmd->delay, t->timeout > 0 ? t->timeout : 1);
  152. }
  153. static void rule_handle_command(struct json_script_ctx *ctx, const char *name,
  154. struct blob_attr *exec, struct blob_attr *vars)
  155. {
  156. struct trigger *t = container_of(ctx, struct trigger, jctx);
  157. if (!strcmp(name, "run_script")) {
  158. trigger_command_add(t, exec);
  159. return;
  160. }
  161. }
  162. static void rule_handle_error(struct json_script_ctx *ctx, const char *msg,
  163. struct blob_attr *context)
  164. {
  165. char *s;
  166. s = blobmsg_format_json(context, false);
  167. ERROR("ERROR: %s in block: %s\n", msg, s);
  168. free(s);
  169. }
  170. static struct trigger* _trigger_add(char *type, struct blob_attr *rule, int timeout, void *id)
  171. {
  172. char *_t;
  173. struct blob_attr *_r;
  174. struct trigger *t = calloc_a(sizeof(*t), &_t, strlen(type) + 1, &_r, blob_pad_len(rule));
  175. t->type = _t;
  176. t->rule = _r;
  177. t->timeout = timeout;
  178. t->id = id;
  179. t->jctx.handle_var = rule_handle_var,
  180. t->jctx.handle_error = rule_handle_error,
  181. t->jctx.handle_command = rule_handle_command,
  182. t->jctx.handle_file = rule_load_script,
  183. strcpy(t->type, type);
  184. memcpy(t->rule, rule, blob_pad_len(rule));
  185. list_add(&t->list, &triggers);
  186. json_script_init(&t->jctx);
  187. return t;
  188. }
  189. void trigger_add(struct blob_attr *rule, void *id)
  190. {
  191. struct blob_attr *cur;
  192. int rem;
  193. blobmsg_for_each_attr(cur, rule, rem) {
  194. struct blob_attr *_cur, *type = NULL, *script = NULL, *timeout = NULL;
  195. int _rem;
  196. int i = 0;
  197. if (blobmsg_type(cur) != BLOBMSG_TYPE_ARRAY)
  198. continue;
  199. blobmsg_for_each_attr(_cur, cur, _rem) {
  200. switch (i++) {
  201. case 0:
  202. if (blobmsg_type(_cur) == BLOBMSG_TYPE_STRING)
  203. type = _cur;
  204. break;
  205. case 1:
  206. if (blobmsg_type(_cur) == BLOBMSG_TYPE_ARRAY)
  207. script = _cur;
  208. break;
  209. case 2:
  210. if (blobmsg_type(_cur) == BLOBMSG_TYPE_INT32)
  211. timeout = _cur;
  212. break;
  213. }
  214. }
  215. if (type && script) {
  216. int t = 0;
  217. if (timeout)
  218. t = blobmsg_get_u32(timeout);
  219. _trigger_add(blobmsg_get_string(type), script, t, id);
  220. }
  221. }
  222. }
  223. void trigger_del(void *id)
  224. {
  225. struct trigger *t, *n;
  226. list_for_each_entry_safe(t, n, &triggers, list) {
  227. if (t->id != id)
  228. continue;
  229. trigger_free(t);
  230. }
  231. }
  232. static bool trigger_match(const char *event, const char *match)
  233. {
  234. char *wildcard = strstr(match, ".*");
  235. if (wildcard)
  236. return !strncmp(event, match, wildcard - match);
  237. return !strcmp(event, match);
  238. }
  239. void trigger_event(const char *type, struct blob_attr *data)
  240. {
  241. struct trigger *t;
  242. list_for_each_entry(t, &triggers, list) {
  243. if (!trigger_match(type, t->type))
  244. continue;
  245. json_script_run(&t->jctx, t->type, data);
  246. }
  247. }