service: ensure that trigger timers are always cancelled before free
[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 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 uloop_timeout_cancel(&t->delay);
94 free(t->data);
95 list_del(&t->list);
96 free(t);
97 }
98
99 static void q_job_complete(struct runqueue *q, struct runqueue_task *p)
100 {
101 struct job *j = container_of(p, struct job, proc.task);
102
103 if (j->trigger->remove) {
104 trigger_free(j->trigger);
105 } else {
106 j->trigger->pending = 0;
107 }
108 free(j);
109 }
110
111 static void add_job(struct trigger *t, struct cmd *cmd, struct blob_attr *exec, struct blob_attr *data)
112 {
113 static const struct runqueue_task_type job_type = {
114 .run = q_job_run,
115 .cancel = runqueue_process_cancel_cb,
116 .kill = runqueue_process_kill_cb,
117 };
118 struct blob_attr *d, *e;
119 struct job *j = calloc_a(sizeof(*j), &e, blob_pad_len(exec), &d, blob_pad_len(data));
120
121 j->env = d;
122 j->exec = e;
123 j->cmd = cmd;
124 j->trigger = t;
125 j->proc.task.type = &job_type;
126 j->proc.task.complete = q_job_complete;
127 t->pending = 1;
128
129 memcpy(j->exec, exec, blob_pad_len(exec));
130 memcpy(j->env, data, blob_pad_len(data));
131
132 runqueue_task_add(&q, &j->proc.task, false);
133 }
134
135 static void _setenv(const char *key, const char *val)
136 {
137 char _key[32];
138
139 snprintf(_key, sizeof(_key), "PARAM_%s", key);
140 setenv(_key, val, 1);
141 }
142
143 static void handle_run_script(struct job *j, struct blob_attr *exec, struct blob_attr *env)
144 {
145 char *argv[8];
146 struct blob_attr *cur;
147 int rem;
148 int i = 0;
149 pid_t pid;
150
151 pid = fork();
152 if (pid < 0)
153 return;
154
155 if (pid) {
156 runqueue_process_add(&q, &j->proc, pid);
157 return;
158 }
159
160 if (debug < 3) {
161 close(STDIN_FILENO);
162 close(STDOUT_FILENO);
163 close(STDERR_FILENO);
164 }
165
166 _setenv("type", j->trigger->type);
167 blobmsg_for_each_attr(cur, j->env, rem)
168 _setenv(blobmsg_name(cur), blobmsg_data(cur));
169
170 blobmsg_for_each_attr(cur, j->exec, rem) {
171 argv[i] = blobmsg_data(cur);
172 i++;
173 if (i == 7)
174 break;
175 }
176
177 if (i > 0) {
178 argv[i] = NULL;
179 execvp(argv[0], &argv[0]);
180 }
181
182 exit(1);
183 }
184
185 static struct cmd handlers[] = {
186 {
187 .name = "run_script",
188 .handler = handle_run_script,
189 },
190 };
191
192 static void rule_handle_command(struct json_script_ctx *ctx, const char *name,
193 struct blob_attr *exec, struct blob_attr *vars)
194 {
195 struct trigger *t = container_of(ctx, struct trigger, jctx);
196 int i;
197
198 if (t->pending)
199 return;
200
201 for (i = 0; i < ARRAY_SIZE(handlers); i++) {
202 if (!strcmp(handlers[i].name, name)) {
203 add_job(t, &handlers[i], exec, vars);
204 break;
205 }
206 }
207 }
208
209 static void rule_handle_error(struct json_script_ctx *ctx, const char *msg,
210 struct blob_attr *context)
211 {
212 char *s;
213
214 s = blobmsg_format_json(context, false);
215 ERROR("ERROR: %s in block: %s\n", msg, s);
216 free(s);
217 }
218
219 static void q_empty(struct runqueue *q)
220 {
221 }
222
223 static void trigger_delay_cb(struct uloop_timeout *tout)
224 {
225 struct trigger *t = container_of(tout, struct trigger, delay);
226
227 json_script_run(&t->jctx, "foo", t->data);
228 free(t->data);
229 t->data = NULL;
230 }
231
232 static struct trigger* _trigger_add(char *type, struct blob_attr *rule, int timeout, void *id)
233 {
234 char *_t;
235 struct blob_attr *_r;
236 struct trigger *t = calloc_a(sizeof(*t), &_t, strlen(type) + 1, &_r, blob_pad_len(rule));
237
238 t->type = _t;
239 t->rule = _r;
240 t->delay.cb = trigger_delay_cb;
241 t->timeout = timeout;
242 t->pending = 0;
243 t->remove = 0;
244 t->id = id;
245 t->jctx.handle_var = rule_handle_var,
246 t->jctx.handle_error = rule_handle_error,
247 t->jctx.handle_command = rule_handle_command,
248 t->jctx.handle_file = rule_load_script,
249
250 strcpy(t->type, type);
251 memcpy(t->rule, rule, blob_pad_len(rule));
252
253 list_add(&t->list, &triggers);
254 json_script_init(&t->jctx);
255
256 return t;
257 }
258
259 void trigger_add(struct blob_attr *rule, void *id)
260 {
261 struct blob_attr *cur;
262 int rem;
263
264 blobmsg_for_each_attr(cur, rule, rem) {
265 struct blob_attr *_cur, *type = NULL, *script = NULL, *timeout = NULL;
266 int _rem;
267 int i = 0;
268
269 if (blobmsg_type(cur) != BLOBMSG_TYPE_ARRAY)
270 continue;
271
272 blobmsg_for_each_attr(_cur, cur, _rem) {
273 switch (i++) {
274 case 0:
275 if (blobmsg_type(_cur) == BLOBMSG_TYPE_STRING)
276 type = _cur;
277 break;
278
279 case 1:
280 if (blobmsg_type(_cur) == BLOBMSG_TYPE_ARRAY)
281 script = _cur;
282 break;
283
284 case 2:
285 if (blobmsg_type(_cur) == BLOBMSG_TYPE_INT32)
286 timeout = _cur;
287 break;
288 }
289 }
290
291 if (type && script) {
292 int t = 0;
293
294 if (timeout)
295 t = blobmsg_get_u32(timeout);
296 _trigger_add(blobmsg_get_string(type), script, t, id);
297 }
298 }
299 }
300
301 void trigger_del(void *id)
302 {
303 struct trigger *t, *n;
304
305 list_for_each_entry_safe(t, n, &triggers, list) {
306 if (t->id != id)
307 continue;
308
309 if (t->pending) {
310 t->remove = 1;
311 continue;
312 }
313
314 trigger_free(t);
315 }
316 }
317
318 void trigger_init(void)
319 {
320 runqueue_init(&q);
321 q.empty_cb = q_empty;
322 q.max_running_tasks = 1;
323 }
324
325 void trigger_event(char *type, struct blob_attr *data)
326 {
327 struct trigger *t;
328
329 list_for_each_entry(t, &triggers, list) {
330 if (t->pending || t->remove)
331 continue;
332 if (!strcmp(t->type, type)) {
333 if (t->timeout) {
334 free(t->data);
335 t->data = blob_memdup(data);
336 uloop_timeout_set(&t->delay, t->timeout);
337 } else {
338 json_script_run(&t->jctx, "foo", data);
339 }
340 }
341 }
342 }