trigger.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 <fcntl.h>
  23. #include <unistd.h>
  24. #include <stdlib.h>
  25. #include <libgen.h>
  26. #include "../procd.h"
  27. struct trigger {
  28. struct list_head list;
  29. char *type;
  30. int pending;
  31. int remove;
  32. int timeout;
  33. void *id;
  34. struct blob_attr *rule;
  35. struct blob_attr *data;
  36. struct uloop_timeout delay;
  37. struct json_script_ctx jctx;
  38. };
  39. struct job;
  40. struct cmd {
  41. char *name;
  42. void (*handler)(struct job *job, struct blob_attr *exec, struct blob_attr *env);
  43. };
  44. struct job {
  45. struct runqueue_process proc;
  46. struct cmd *cmd;
  47. struct trigger *trigger;
  48. struct blob_attr *exec;
  49. struct blob_attr *env;
  50. };
  51. static LIST_HEAD(triggers);
  52. static struct runqueue q;
  53. static const char* rule_handle_var(struct json_script_ctx *ctx, const char *name, struct blob_attr *vars)
  54. {
  55. return NULL;
  56. }
  57. static struct json_script_file *
  58. rule_load_script(struct json_script_ctx *ctx, const char *name)
  59. {
  60. struct trigger *t = container_of(ctx, struct trigger, jctx);
  61. if (strcmp(name, t->type) != 0)
  62. return NULL;
  63. return json_script_file_from_blobmsg(t->type, t->rule, blob_pad_len(t->rule));
  64. }
  65. static void q_job_run(struct runqueue *q, struct runqueue_task *t)
  66. {
  67. struct job *j = container_of(t, struct job, proc.task);
  68. DEBUG(4, "handle event %s\n", j->cmd->name);
  69. j->cmd->handler(j, j->exec, j->env);
  70. }
  71. static void trigger_free(struct trigger *t)
  72. {
  73. json_script_free(&t->jctx);
  74. uloop_timeout_cancel(&t->delay);
  75. free(t->data);
  76. list_del(&t->list);
  77. free(t);
  78. }
  79. static void q_job_complete(struct runqueue *q, struct runqueue_task *p)
  80. {
  81. struct job *j = container_of(p, struct job, proc.task);
  82. if (j->trigger->remove) {
  83. trigger_free(j->trigger);
  84. } else {
  85. j->trigger->pending = 0;
  86. }
  87. free(j);
  88. }
  89. static void add_job(struct trigger *t, struct cmd *cmd, struct blob_attr *exec, struct blob_attr *data)
  90. {
  91. static const struct runqueue_task_type job_type = {
  92. .run = q_job_run,
  93. .cancel = runqueue_process_cancel_cb,
  94. .kill = runqueue_process_kill_cb,
  95. };
  96. struct blob_attr *d, *e;
  97. struct job *j = calloc_a(sizeof(*j), &e, blob_pad_len(exec), &d, blob_pad_len(data));
  98. j->env = d;
  99. j->exec = e;
  100. j->cmd = cmd;
  101. j->trigger = t;
  102. j->proc.task.type = &job_type;
  103. j->proc.task.complete = q_job_complete;
  104. t->pending = 1;
  105. memcpy(j->exec, exec, blob_pad_len(exec));
  106. memcpy(j->env, data, blob_pad_len(data));
  107. runqueue_task_add(&q, &j->proc.task, false);
  108. }
  109. static void _setenv(const char *key, const char *val)
  110. {
  111. char _key[32];
  112. snprintf(_key, sizeof(_key), "PARAM_%s", key);
  113. setenv(_key, val, 1);
  114. }
  115. static void handle_run_script(struct job *j, struct blob_attr *exec, struct blob_attr *env)
  116. {
  117. char *argv[8];
  118. struct blob_attr *cur;
  119. int rem;
  120. int i = 0;
  121. pid_t pid;
  122. pid = fork();
  123. if (pid < 0)
  124. return;
  125. if (pid) {
  126. runqueue_process_add(&q, &j->proc, pid);
  127. return;
  128. }
  129. if (debug < 3) {
  130. close(STDIN_FILENO);
  131. close(STDOUT_FILENO);
  132. close(STDERR_FILENO);
  133. }
  134. _setenv("type", j->trigger->type);
  135. blobmsg_for_each_attr(cur, j->env, rem)
  136. _setenv(blobmsg_name(cur), blobmsg_data(cur));
  137. blobmsg_for_each_attr(cur, j->exec, rem) {
  138. argv[i] = blobmsg_data(cur);
  139. i++;
  140. if (i == 7)
  141. break;
  142. }
  143. if (i > 0) {
  144. argv[i] = NULL;
  145. execvp(argv[0], &argv[0]);
  146. }
  147. exit(1);
  148. }
  149. static struct cmd handlers[] = {
  150. {
  151. .name = "run_script",
  152. .handler = handle_run_script,
  153. },
  154. };
  155. static void rule_handle_command(struct json_script_ctx *ctx, const char *name,
  156. struct blob_attr *exec, struct blob_attr *vars)
  157. {
  158. struct trigger *t = container_of(ctx, struct trigger, jctx);
  159. int i;
  160. if (t->pending)
  161. return;
  162. for (i = 0; i < ARRAY_SIZE(handlers); i++) {
  163. if (!strcmp(handlers[i].name, name)) {
  164. add_job(t, &handlers[i], exec, vars);
  165. break;
  166. }
  167. }
  168. }
  169. static void rule_handle_error(struct json_script_ctx *ctx, const char *msg,
  170. struct blob_attr *context)
  171. {
  172. char *s;
  173. s = blobmsg_format_json(context, false);
  174. ERROR("ERROR: %s in block: %s\n", msg, s);
  175. free(s);
  176. }
  177. static void q_empty(struct runqueue *q)
  178. {
  179. }
  180. static void trigger_delay_cb(struct uloop_timeout *tout)
  181. {
  182. struct trigger *t = container_of(tout, struct trigger, delay);
  183. json_script_run(&t->jctx, t->type, t->data);
  184. free(t->data);
  185. t->data = NULL;
  186. }
  187. static struct trigger* _trigger_add(char *type, struct blob_attr *rule, int timeout, void *id)
  188. {
  189. char *_t;
  190. struct blob_attr *_r;
  191. struct trigger *t = calloc_a(sizeof(*t), &_t, strlen(type) + 1, &_r, blob_pad_len(rule));
  192. t->type = _t;
  193. t->rule = _r;
  194. t->delay.cb = trigger_delay_cb;
  195. t->timeout = timeout;
  196. t->pending = 0;
  197. t->remove = 0;
  198. t->id = id;
  199. t->jctx.handle_var = rule_handle_var,
  200. t->jctx.handle_error = rule_handle_error,
  201. t->jctx.handle_command = rule_handle_command,
  202. t->jctx.handle_file = rule_load_script,
  203. strcpy(t->type, type);
  204. memcpy(t->rule, rule, blob_pad_len(rule));
  205. list_add(&t->list, &triggers);
  206. json_script_init(&t->jctx);
  207. return t;
  208. }
  209. void trigger_add(struct blob_attr *rule, void *id)
  210. {
  211. struct blob_attr *cur;
  212. int rem;
  213. blobmsg_for_each_attr(cur, rule, rem) {
  214. struct blob_attr *_cur, *type = NULL, *script = NULL, *timeout = NULL;
  215. int _rem;
  216. int i = 0;
  217. if (blobmsg_type(cur) != BLOBMSG_TYPE_ARRAY)
  218. continue;
  219. blobmsg_for_each_attr(_cur, cur, _rem) {
  220. switch (i++) {
  221. case 0:
  222. if (blobmsg_type(_cur) == BLOBMSG_TYPE_STRING)
  223. type = _cur;
  224. break;
  225. case 1:
  226. if (blobmsg_type(_cur) == BLOBMSG_TYPE_ARRAY)
  227. script = _cur;
  228. break;
  229. case 2:
  230. if (blobmsg_type(_cur) == BLOBMSG_TYPE_INT32)
  231. timeout = _cur;
  232. break;
  233. }
  234. }
  235. if (type && script) {
  236. int t = 0;
  237. if (timeout)
  238. t = blobmsg_get_u32(timeout);
  239. _trigger_add(blobmsg_get_string(type), script, t, id);
  240. }
  241. }
  242. }
  243. void trigger_del(void *id)
  244. {
  245. struct trigger *t, *n;
  246. list_for_each_entry_safe(t, n, &triggers, list) {
  247. if (t->id != id)
  248. continue;
  249. if (t->pending) {
  250. t->remove = 1;
  251. continue;
  252. }
  253. trigger_free(t);
  254. }
  255. }
  256. void trigger_init(void)
  257. {
  258. runqueue_init(&q);
  259. q.empty_cb = q_empty;
  260. q.max_running_tasks = 1;
  261. }
  262. static bool trigger_match(const char *event, const char *match)
  263. {
  264. char *wildcard = strstr(match, ".*");
  265. if (wildcard)
  266. return !strncmp(event, match, wildcard - match);
  267. return !strcmp(event, match);
  268. }
  269. void trigger_event(const char *type, struct blob_attr *data)
  270. {
  271. struct trigger *t;
  272. list_for_each_entry(t, &triggers, list) {
  273. if (t->remove)
  274. continue;
  275. if (trigger_match(type, t->type)) {
  276. if (t->timeout) {
  277. free(t->data);
  278. t->data = blob_memdup(data);
  279. uloop_timeout_set(&t->delay, t->timeout);
  280. } else {
  281. json_script_run(&t->jctx, t->type, data);
  282. }
  283. }
  284. }
  285. }