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