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
;
112 trigger_command_complete(q
, t
);
117 runqueue_process_add(q
, &cmd
->proc
, pid
);
121 if (debug
< 3 && (fd
= open("/dev/null", O_RDWR
)) >= 0) {
122 dup2(fd
, STDIN_FILENO
);
123 dup2(fd
, STDOUT_FILENO
);
124 dup2(fd
, STDERR_FILENO
);
125 if (fd
> STDERR_FILENO
)
129 blobmsg_for_each_attr(cur
, cmd
->data
, rem
)
132 argv
= alloca((n
+ 1) * sizeof(*argv
));
134 blobmsg_for_each_attr(cur
, cmd
->data
, rem
)
135 argv
[n
++] = blobmsg_get_string(cur
);
139 execvp(argv
[0], &argv
[0]);
144 static void trigger_command_start(struct uloop_timeout
*timeout
)
146 static const struct runqueue_task_type trigger_command_type
= {
147 .run
= trigger_command_run
,
148 .cancel
= runqueue_process_cancel_cb
,
149 .kill
= runqueue_process_kill_cb
,
151 struct trigger_command
*cmd
= container_of(timeout
, struct trigger_command
, delay
);
153 cmd
->proc
.task
.type
= &trigger_command_type
;
154 cmd
->proc
.task
.complete
= trigger_command_complete
;
155 runqueue_task_add(&q
, &cmd
->proc
.task
, false);
158 static void trigger_command_add(struct trigger
*t
, struct blob_attr
*data
)
160 struct trigger_command
*cmd
;
163 cmd
= avl_find_element(&trigger_pending
, data
, cmd
, avl
);
165 /* Command currently running? */
166 if (!cmd
->delay
.pending
) {
171 /* Extend timer if trigger timeout is bigger than remaining time */
172 remaining
= uloop_timeout_remaining64(&cmd
->delay
);
173 if (remaining
< t
->timeout
)
174 uloop_timeout_set(&cmd
->delay
, t
->timeout
);
179 cmd
= calloc(1, sizeof(*cmd
) + blob_pad_len(data
));
183 cmd
->avl
.key
= cmd
->data
;
184 cmd
->delay
.cb
= trigger_command_start
;
185 memcpy(cmd
->data
, data
, blob_pad_len(data
));
186 avl_insert(&trigger_pending
, &cmd
->avl
);
187 uloop_timeout_set(&cmd
->delay
, t
->timeout
> 0 ? t
->timeout
: 1);
190 static void rule_handle_command(struct json_script_ctx
*ctx
, const char *name
,
191 struct blob_attr
*exec
, struct blob_attr
*vars
)
193 struct trigger
*t
= container_of(ctx
, struct trigger
, jctx
);
195 if (!strcmp(name
, "run_script")) {
196 trigger_command_add(t
, exec
);
201 static void rule_handle_error(struct json_script_ctx
*ctx
, const char *msg
,
202 struct blob_attr
*context
)
206 s
= blobmsg_format_json(context
, false);
207 ERROR("ERROR: %s in block: %s\n", msg
, s
);
211 static struct trigger
* _trigger_add(char *type
, struct blob_attr
*rule
, int timeout
, void *id
)
214 struct blob_attr
*_r
;
215 struct trigger
*t
= calloc_a(sizeof(*t
), &_t
, strlen(type
) + 1, &_r
, blob_pad_len(rule
));
219 t
->timeout
= timeout
;
221 t
->jctx
.handle_var
= rule_handle_var
,
222 t
->jctx
.handle_error
= rule_handle_error
,
223 t
->jctx
.handle_command
= rule_handle_command
,
224 t
->jctx
.handle_file
= rule_load_script
,
226 strcpy(t
->type
, type
);
227 memcpy(t
->rule
, rule
, blob_pad_len(rule
));
229 list_add(&t
->list
, &triggers
);
230 json_script_init(&t
->jctx
);
235 void trigger_add(struct blob_attr
*rule
, void *id
)
237 struct blob_attr
*cur
;
240 blobmsg_for_each_attr(cur
, rule
, rem
) {
241 struct blob_attr
*_cur
, *type
= NULL
, *script
= NULL
, *timeout
= NULL
;
245 if (blobmsg_type(cur
) != BLOBMSG_TYPE_ARRAY
)
248 blobmsg_for_each_attr(_cur
, cur
, _rem
) {
251 if (blobmsg_type(_cur
) == BLOBMSG_TYPE_STRING
)
256 if (blobmsg_type(_cur
) == BLOBMSG_TYPE_ARRAY
)
261 if (blobmsg_type(_cur
) == BLOBMSG_TYPE_INT32
)
267 if (type
&& script
) {
271 t
= blobmsg_get_u32(timeout
);
272 _trigger_add(blobmsg_get_string(type
), script
, t
, id
);
277 void trigger_del(void *id
)
279 struct trigger
*t
, *n
;
281 list_for_each_entry_safe(t
, n
, &triggers
, list
) {
289 static bool trigger_match(const char *event
, const char *match
)
291 char *wildcard
= strstr(match
, ".*");
293 return !strncmp(event
, match
, wildcard
- match
);
294 return !strcmp(event
, match
);
297 void trigger_event(const char *type
, struct blob_attr
*data
)
301 list_for_each_entry(t
, &triggers
, list
) {
302 if (!trigger_match(type
, t
->type
))
304 json_script_run(&t
->jctx
, t
->type
, data
);