trigger.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. pid = fork();
  88. if (pid < 0) {
  89. trigger_command_complete(q, t);
  90. return;
  91. }
  92. if (pid) {
  93. runqueue_process_add(q, &cmd->proc, pid);
  94. return;
  95. }
  96. if (debug < 3) {
  97. close(STDIN_FILENO);
  98. close(STDOUT_FILENO);
  99. close(STDERR_FILENO);
  100. }
  101. blobmsg_for_each_attr(cur, cmd->data, rem)
  102. n++;
  103. argv = alloca((n + 1) * sizeof(*argv));
  104. n = 0;
  105. blobmsg_for_each_attr(cur, cmd->data, rem)
  106. argv[n++] = blobmsg_get_string(cur);
  107. argv[n] = NULL;
  108. if (n > 0)
  109. execvp(argv[0], &argv[0]);
  110. exit(1);
  111. }
  112. static void trigger_command_start(struct uloop_timeout *timeout)
  113. {
  114. static const struct runqueue_task_type trigger_command_type = {
  115. .run = trigger_command_run,
  116. .cancel = runqueue_process_cancel_cb,
  117. .kill = runqueue_process_kill_cb,
  118. };
  119. struct trigger_command *cmd = container_of(timeout, struct trigger_command, delay);
  120. cmd->proc.task.type = &trigger_command_type;
  121. cmd->proc.task.complete = trigger_command_complete;
  122. runqueue_task_add(&q, &cmd->proc.task, false);
  123. }
  124. static void trigger_command_add(struct trigger *t, struct blob_attr *data)
  125. {
  126. struct trigger_command *cmd;
  127. int64_t remaining;
  128. cmd = avl_find_element(&trigger_pending, data, cmd, avl);
  129. if (cmd) {
  130. /* Command currently running? */
  131. if (!cmd->delay.pending) {
  132. cmd->requeue = true;
  133. return;
  134. }
  135. /* Extend timer if trigger timeout is bigger than remaining time */
  136. remaining = uloop_timeout_remaining64(&cmd->delay);
  137. if (remaining < t->timeout)
  138. uloop_timeout_set(&cmd->delay, t->timeout);
  139. return;
  140. }
  141. cmd = calloc(1, sizeof(*cmd) + blob_pad_len(data));
  142. if (!cmd)
  143. return;
  144. cmd->avl.key = cmd->data;
  145. cmd->delay.cb = trigger_command_start;
  146. memcpy(cmd->data, data, blob_pad_len(data));
  147. avl_insert(&trigger_pending, &cmd->avl);
  148. uloop_timeout_set(&cmd->delay, t->timeout > 0 ? t->timeout : 1);
  149. }
  150. static void rule_handle_command(struct json_script_ctx *ctx, const char *name,
  151. struct blob_attr *exec, struct blob_attr *vars)
  152. {
  153. struct trigger *t = container_of(ctx, struct trigger, jctx);
  154. if (!strcmp(name, "run_script")) {
  155. trigger_command_add(t, exec);
  156. return;
  157. }
  158. }
  159. static void rule_handle_error(struct json_script_ctx *ctx, const char *msg,
  160. struct blob_attr *context)
  161. {
  162. char *s;
  163. s = blobmsg_format_json(context, false);
  164. ERROR("ERROR: %s in block: %s\n", msg, s);
  165. free(s);
  166. }
  167. static struct trigger* _trigger_add(char *type, struct blob_attr *rule, int timeout, void *id)
  168. {
  169. char *_t;
  170. struct blob_attr *_r;
  171. struct trigger *t = calloc_a(sizeof(*t), &_t, strlen(type) + 1, &_r, blob_pad_len(rule));
  172. t->type = _t;
  173. t->rule = _r;
  174. t->timeout = timeout;
  175. t->id = id;
  176. t->jctx.handle_var = rule_handle_var,
  177. t->jctx.handle_error = rule_handle_error,
  178. t->jctx.handle_command = rule_handle_command,
  179. t->jctx.handle_file = rule_load_script,
  180. strcpy(t->type, type);
  181. memcpy(t->rule, rule, blob_pad_len(rule));
  182. list_add(&t->list, &triggers);
  183. json_script_init(&t->jctx);
  184. return t;
  185. }
  186. void trigger_add(struct blob_attr *rule, void *id)
  187. {
  188. struct blob_attr *cur;
  189. int rem;
  190. blobmsg_for_each_attr(cur, rule, rem) {
  191. struct blob_attr *_cur, *type = NULL, *script = NULL, *timeout = NULL;
  192. int _rem;
  193. int i = 0;
  194. if (blobmsg_type(cur) != BLOBMSG_TYPE_ARRAY)
  195. continue;
  196. blobmsg_for_each_attr(_cur, cur, _rem) {
  197. switch (i++) {
  198. case 0:
  199. if (blobmsg_type(_cur) == BLOBMSG_TYPE_STRING)
  200. type = _cur;
  201. break;
  202. case 1:
  203. if (blobmsg_type(_cur) == BLOBMSG_TYPE_ARRAY)
  204. script = _cur;
  205. break;
  206. case 2:
  207. if (blobmsg_type(_cur) == BLOBMSG_TYPE_INT32)
  208. timeout = _cur;
  209. break;
  210. }
  211. }
  212. if (type && script) {
  213. int t = 0;
  214. if (timeout)
  215. t = blobmsg_get_u32(timeout);
  216. _trigger_add(blobmsg_get_string(type), script, t, id);
  217. }
  218. }
  219. }
  220. void trigger_del(void *id)
  221. {
  222. struct trigger *t, *n;
  223. list_for_each_entry_safe(t, n, &triggers, list) {
  224. if (t->id != id)
  225. continue;
  226. trigger_free(t);
  227. }
  228. }
  229. static bool trigger_match(const char *event, const char *match)
  230. {
  231. char *wildcard = strstr(match, ".*");
  232. if (wildcard)
  233. return !strncmp(event, match, wildcard - match);
  234. return !strcmp(event, match);
  235. }
  236. void trigger_event(const char *type, struct blob_attr *data)
  237. {
  238. struct trigger *t;
  239. list_for_each_entry(t, &triggers, list) {
  240. if (!trigger_match(type, t->type))
  241. continue;
  242. json_script_run(&t->jctx, t->type, data);
  243. }
  244. }