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 <linux/types.h>
20 #include <linux/netlink.h>
22 #include <libubox/blobmsg_json.h>
23 #include <libubox/json_script.h>
24 #include <libubox/runqueue.h>
25 #include <libubox/ustream.h>
26 #include <libubox/uloop.h>
36 struct list_head list;
46 struct blob_attr *rule;
47 struct blob_attr *data;
48 struct uloop_timeout delay;
50 struct json_script_ctx jctx;
56 void (*handler)(struct job *job, struct blob_attr *exec, struct blob_attr *env);
60 struct runqueue_process proc;
62 struct trigger *trigger;
63 struct blob_attr *exec;
64 struct blob_attr *env;
67 static LIST_HEAD(triggers);
68 static struct runqueue q;
70 static const char* rule_handle_var(struct json_script_ctx *ctx, const char *name, struct blob_attr *vars)
75 static struct json_script_file *
76 rule_load_script(struct json_script_ctx *ctx, const char *name)
78 struct trigger *t = container_of(ctx, struct trigger, jctx);
80 return json_script_file_from_blobmsg(t->type, t->rule, blob_pad_len(t->rule));
83 static void q_job_run(struct runqueue *q, struct runqueue_task *t)
85 struct job *j = container_of(t, struct job, proc.task);
87 DEBUG(4, "handle event %s\n", j->cmd->name);
88 j->cmd->handler(j, j->exec, j->env);
91 static void trigger_free(struct trigger *t)
93 json_script_free(&t->jctx);
94 uloop_timeout_cancel(&t->delay);
100 static void q_job_complete(struct runqueue *q, struct runqueue_task *p)
102 struct job *j = container_of(p, struct job, proc.task);
104 if (j->trigger->remove) {
105 trigger_free(j->trigger);
107 j->trigger->pending = 0;
112 static void add_job(struct trigger *t, struct cmd *cmd, struct blob_attr *exec, struct blob_attr *data)
114 static const struct runqueue_task_type job_type = {
116 .cancel = runqueue_process_cancel_cb,
117 .kill = runqueue_process_kill_cb,
119 struct blob_attr *d, *e;
120 struct job *j = calloc_a(sizeof(*j), &e, blob_pad_len(exec), &d, blob_pad_len(data));
126 j->proc.task.type = &job_type;
127 j->proc.task.complete = q_job_complete;
130 memcpy(j->exec, exec, blob_pad_len(exec));
131 memcpy(j->env, data, blob_pad_len(data));
133 runqueue_task_add(&q, &j->proc.task, false);
136 static void _setenv(const char *key, const char *val)
140 snprintf(_key, sizeof(_key), "PARAM_%s", key);
141 setenv(_key, val, 1);
144 static void handle_run_script(struct job *j, struct blob_attr *exec, struct blob_attr *env)
147 struct blob_attr *cur;
157 runqueue_process_add(&q, &j->proc, pid);
163 close(STDOUT_FILENO);
164 close(STDERR_FILENO);
167 _setenv("type", j->trigger->type);
168 blobmsg_for_each_attr(cur, j->env, rem)
169 _setenv(blobmsg_name(cur), blobmsg_data(cur));
171 blobmsg_for_each_attr(cur, j->exec, rem) {
172 argv[i] = blobmsg_data(cur);
180 execvp(argv[0], &argv[0]);
186 static struct cmd handlers[] = {
188 .name = "run_script",
189 .handler = handle_run_script,
193 static void rule_handle_command(struct json_script_ctx *ctx, const char *name,
194 struct blob_attr *exec, struct blob_attr *vars)
196 struct trigger *t = container_of(ctx, struct trigger, jctx);
202 for (i = 0; i < ARRAY_SIZE(handlers); i++) {
203 if (!strcmp(handlers[i].name, name)) {
204 add_job(t, &handlers[i], exec, vars);
210 static void rule_handle_error(struct json_script_ctx *ctx, const char *msg,
211 struct blob_attr *context)
215 s = blobmsg_format_json(context, false);
216 ERROR("ERROR: %s in block: %s\n", msg, s);
220 static void q_empty(struct runqueue *q)
224 static void trigger_delay_cb(struct uloop_timeout *tout)
226 struct trigger *t = container_of(tout, struct trigger, delay);
228 json_script_run(&t->jctx, "foo", t->data);
233 static struct trigger* _trigger_add(char *type, struct blob_attr *rule, int timeout, void *id)
236 struct blob_attr *_r;
237 struct trigger *t = calloc_a(sizeof(*t), &_t, strlen(type) + 1, &_r, blob_pad_len(rule));
241 t->delay.cb = trigger_delay_cb;
242 t->timeout = timeout;
246 t->jctx.handle_var = rule_handle_var,
247 t->jctx.handle_error = rule_handle_error,
248 t->jctx.handle_command = rule_handle_command,
249 t->jctx.handle_file = rule_load_script,
251 strcpy(t->type, type);
252 memcpy(t->rule, rule, blob_pad_len(rule));
254 list_add(&t->list, &triggers);
255 json_script_init(&t->jctx);
260 void trigger_add(struct blob_attr *rule, void *id)
262 struct blob_attr *cur;
265 blobmsg_for_each_attr(cur, rule, rem) {
266 struct blob_attr *_cur, *type = NULL, *script = NULL, *timeout = NULL;
270 if (blobmsg_type(cur) != BLOBMSG_TYPE_ARRAY)
273 blobmsg_for_each_attr(_cur, cur, _rem) {
276 if (blobmsg_type(_cur) == BLOBMSG_TYPE_STRING)
281 if (blobmsg_type(_cur) == BLOBMSG_TYPE_ARRAY)
286 if (blobmsg_type(_cur) == BLOBMSG_TYPE_INT32)
292 if (type && script) {
296 t = blobmsg_get_u32(timeout);
297 _trigger_add(blobmsg_get_string(type), script, t, id);
302 void trigger_del(void *id)
304 struct trigger *t, *n;
306 list_for_each_entry_safe(t, n, &triggers, list) {
319 void trigger_init(void)
322 q.empty_cb = q_empty;
323 q.max_running_tasks = 1;
326 static int trigger_match(const char *event, const char *match)
328 char *wildcard = strstr(match, ".*");
330 return strncmp(event, match, wildcard - match);
331 return strcmp(event, match);
334 void trigger_event(const char *type, struct blob_attr *data)
338 list_for_each_entry(t, &triggers, list) {
339 if (t->pending || t->remove)
341 if (!trigger_match(type, t->type)) {
344 t->data = blob_memdup(data);
345 uloop_timeout_set(&t->delay, t->timeout);
347 json_script_run(&t->jctx, "foo", data);