2 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
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
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.
16 #include <sys/socket.h>
17 #include <sys/types.h>
19 #include <libubox/blobmsg_json.h>
20 #include <libubox/json_script.h>
21 #include <libubox/runqueue.h>
22 #include <libubox/ustream.h>
23 #include <libubox/uloop.h>
24 #include <libubox/avl.h>
25 #include <libubox/avl-cmp.h>
35 struct list_head list;
41 struct blob_attr *rule;
42 struct blob_attr *data;
44 struct json_script_ctx jctx;
47 struct trigger_command {
49 struct uloop_timeout delay;
52 struct runqueue_process proc;
53 struct json_script_ctx jctx;
55 struct blob_attr data[];
58 static LIST_HEAD(triggers);
59 static RUNQUEUE(q, 1);
60 static AVL_TREE(trigger_pending, avl_blobcmp, false, NULL);
62 static const char* rule_handle_var(struct json_script_ctx *ctx, const char *name, struct blob_attr *vars)
67 static struct json_script_file *
68 rule_load_script(struct json_script_ctx *ctx, const char *name)
70 struct trigger *t = container_of(ctx, struct trigger, jctx);
72 if (strcmp(name, t->type) != 0)
75 return json_script_file_from_blobmsg(t->type, t->rule, blob_pad_len(t->rule));
78 static void trigger_free(struct trigger *t)
80 json_script_free(&t->jctx);
86 static void trigger_command_complete(struct runqueue *q, struct runqueue_task *p)
88 struct trigger_command *cmd = container_of(p, struct trigger_command, proc.task);
92 runqueue_task_add(q, p, false);
96 avl_delete(&trigger_pending, &cmd->avl);
100 static void trigger_command_run(struct runqueue *q, struct runqueue_task *t)
102 struct trigger_command *cmd = container_of(t, struct trigger_command, proc.task);
103 struct blob_attr *cur;
111 trigger_command_complete(q, t);
116 runqueue_process_add(q, &cmd->proc, pid);
122 close(STDOUT_FILENO);
123 close(STDERR_FILENO);
126 blobmsg_for_each_attr(cur, cmd->data, rem)
129 argv = alloca((n + 1) * sizeof(*argv));
131 blobmsg_for_each_attr(cur, cmd->data, rem)
132 argv[n++] = blobmsg_get_string(cur);
136 execvp(argv[0], &argv[0]);
141 static void trigger_command_start(struct uloop_timeout *timeout)
143 static const struct runqueue_task_type trigger_command_type = {
144 .run = trigger_command_run,
145 .cancel = runqueue_process_cancel_cb,
146 .kill = runqueue_process_kill_cb,
148 struct trigger_command *cmd = container_of(timeout, struct trigger_command, delay);
150 cmd->proc.task.type = &trigger_command_type;
151 cmd->proc.task.complete = trigger_command_complete;
152 runqueue_task_add(&q, &cmd->proc.task, false);
155 static void trigger_command_add(struct trigger *t, struct blob_attr *data)
157 struct trigger_command *cmd;
160 cmd = avl_find_element(&trigger_pending, data, cmd, avl);
162 /* Command currently running? */
163 if (!cmd->delay.pending) {
168 /* Extend timer if trigger timeout is bigger than remaining time */
169 remaining = uloop_timeout_remaining(&cmd->delay);
170 if (remaining < t->timeout)
171 uloop_timeout_set(&cmd->delay, t->timeout);
176 cmd = calloc(1, sizeof(*cmd) + blob_pad_len(data));
180 cmd->avl.key = cmd->data;
181 cmd->delay.cb = trigger_command_start;
182 memcpy(cmd->data, data, blob_pad_len(data));
183 avl_insert(&trigger_pending, &cmd->avl);
184 uloop_timeout_set(&cmd->delay, t->timeout > 0 ? t->timeout : 1);
187 static void rule_handle_command(struct json_script_ctx *ctx, const char *name,
188 struct blob_attr *exec, struct blob_attr *vars)
190 struct trigger *t = container_of(ctx, struct trigger, jctx);
192 if (!strcmp(name, "run_script")) {
193 trigger_command_add(t, exec);
198 static void rule_handle_error(struct json_script_ctx *ctx, const char *msg,
199 struct blob_attr *context)
203 s = blobmsg_format_json(context, false);
204 ERROR("ERROR: %s in block: %s\n", msg, s);
208 static struct trigger* _trigger_add(char *type, struct blob_attr *rule, int timeout, void *id)
211 struct blob_attr *_r;
212 struct trigger *t = calloc_a(sizeof(*t), &_t, strlen(type) + 1, &_r, blob_pad_len(rule));
216 t->timeout = timeout;
218 t->jctx.handle_var = rule_handle_var,
219 t->jctx.handle_error = rule_handle_error,
220 t->jctx.handle_command = rule_handle_command,
221 t->jctx.handle_file = rule_load_script,
223 strcpy(t->type, type);
224 memcpy(t->rule, rule, blob_pad_len(rule));
226 list_add(&t->list, &triggers);
227 json_script_init(&t->jctx);
232 void trigger_add(struct blob_attr *rule, void *id)
234 struct blob_attr *cur;
237 blobmsg_for_each_attr(cur, rule, rem) {
238 struct blob_attr *_cur, *type = NULL, *script = NULL, *timeout = NULL;
242 if (blobmsg_type(cur) != BLOBMSG_TYPE_ARRAY)
245 blobmsg_for_each_attr(_cur, cur, _rem) {
248 if (blobmsg_type(_cur) == BLOBMSG_TYPE_STRING)
253 if (blobmsg_type(_cur) == BLOBMSG_TYPE_ARRAY)
258 if (blobmsg_type(_cur) == BLOBMSG_TYPE_INT32)
264 if (type && script) {
268 t = blobmsg_get_u32(timeout);
269 _trigger_add(blobmsg_get_string(type), script, t, id);
274 void trigger_del(void *id)
276 struct trigger *t, *n;
278 list_for_each_entry_safe(t, n, &triggers, list) {
286 static bool trigger_match(const char *event, const char *match)
288 char *wildcard = strstr(match, ".*");
290 return !strncmp(event, match, wildcard - match);
291 return !strcmp(event, match);
294 void trigger_event(const char *type, struct blob_attr *data)
298 list_for_each_entry(t, &triggers, list) {
299 if (!trigger_match(type, t->type))
301 json_script_run(&t->jctx, t->type, data);