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