summaryrefslogtreecommitdiffstats
path: root/trigger.c
blob: d14101ea18e332f8ca7418c941ccdf4e3633644c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 2.1
 * as published by the Free Software Foundation
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/types.h>

#include <linux/types.h>
#include <linux/netlink.h>

#include <libubox/blobmsg_json.h>
#include <libubox/json_script.h>
#include <libubox/runqueue.h>
#include <libubox/ustream.h>
#include <libubox/uloop.h>

#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <libgen.h>

#include "procd.h"

struct trigger {
	struct list_head list;

	char *type;

	int pending;
	int remove;
	int timeout;

	void *id;

	struct blob_attr *rule;
	struct blob_attr *data;
	struct uloop_timeout delay;

	struct json_script_ctx jctx;
};

struct job;
struct cmd {
	char *name;
	void (*handler)(struct job *job, struct blob_attr *exec, struct blob_attr *env);
};

struct job {
	struct runqueue_process proc;
	struct cmd *cmd;
	struct trigger *trigger;
	struct blob_attr *exec;
	struct blob_attr *env;
};

static LIST_HEAD(triggers);
static struct runqueue q;

static const char* rule_handle_var(struct json_script_ctx *ctx, const char *name, struct blob_attr *vars)
{
	return NULL;
}

static struct json_script_file *
rule_load_script(struct json_script_ctx *ctx, const char *name)
{
	struct trigger *t = container_of(ctx, struct trigger, jctx);

	return json_script_file_from_blobmsg(t->type, t->rule, blob_pad_len(t->rule));
}

static void q_job_run(struct runqueue *q, struct runqueue_task *t)
{
	struct job *j = container_of(t, struct job, proc.task);

	DEBUG(2, "handle event %s\n", j->cmd->name);
	j->cmd->handler(j, j->exec, j->env);
}

static void q_job_complete(struct runqueue *q, struct runqueue_task *p)
{
	struct job *j = container_of(p, struct job, proc.task);

	if (j->trigger->remove) {
		list_del(&j->trigger->list);
		free(j->trigger);
	} else {
		j->trigger->pending = 0;
	}
	free(j);
}

static void add_job(struct trigger *t, struct cmd *cmd, struct blob_attr *exec, struct blob_attr *data)
{
	static const struct runqueue_task_type job_type = {
		.run = q_job_run,
		.cancel = runqueue_process_cancel_cb,
		.kill = runqueue_process_kill_cb,
	};
	struct blob_attr *d, *e;
	struct job *j = calloc_a(sizeof(*j), &e, blob_pad_len(exec), &d, blob_pad_len(data));

	j->env = d;
	j->exec = e;
	j->cmd = cmd;
	j->trigger = t;
	j->proc.task.type = &job_type;
	j->proc.task.complete = q_job_complete;
	t->pending = 1;

	memcpy(j->exec, exec, blob_pad_len(exec));
	memcpy(j->env, data, blob_pad_len(data));

	runqueue_task_add(&q, &j->proc.task, false);
}

static void _setenv(const char *key, const char *val)
{
	char _key[32];

	snprintf(_key, sizeof(_key), "PARAM_%s", key);
	setenv(_key, val, 1);
}

static void handle_run_script(struct job *j, struct blob_attr *exec, struct blob_attr *env)
{
	char *argv[8];
	struct blob_attr *cur;
	int rem;
	int i = 0;
	pid_t pid;

	pid = fork();
	if (pid < 0)
		return;

	if (pid) {
		runqueue_process_add(&q, &j->proc, pid);
		return;
	}

	if (debug < 2) {
		close(STDIN_FILENO);
		close(STDOUT_FILENO);
		close(STDERR_FILENO);
	}

	_setenv("type", j->trigger->type);
	blobmsg_for_each_attr(cur, j->env, rem)
		_setenv(blobmsg_name(cur), blobmsg_data(cur));

	blobmsg_for_each_attr(cur, j->exec, rem) {
		argv[i] = blobmsg_data(cur);
		i++;
		if (i == 7)
			break;
	}

	if (i > 0) {
		argv[i] = NULL;
		execvp(argv[0], &argv[0]);
	}

	exit(1);
}

static struct cmd handlers[] = {
	{
		.name = "run_script",
		.handler = handle_run_script,
	},
};

static void rule_handle_command(struct json_script_ctx *ctx, const char *name,
				struct blob_attr *exec, struct blob_attr *vars)
{
	struct trigger *t = container_of(ctx, struct trigger, jctx);
	int i;

	if (t->pending)
		return;

	for (i = 0; i < ARRAY_SIZE(handlers); i++) {
		if (!strcmp(handlers[i].name, name)) {
			add_job(t, &handlers[i], exec, vars);
			break;
		}
	}
}

static void rule_handle_error(struct json_script_ctx *ctx, const char *msg,
				struct blob_attr *context)
{
	char *s;

	s = blobmsg_format_json(context, false);
	ERROR("ERROR: %s in block: %s\n", msg, s);
	free(s);
}

static void q_empty(struct runqueue *q)
{
}

static void trigger_delay_cb(struct uloop_timeout *tout)
{
	struct trigger *t = container_of(tout, struct trigger, delay);

	json_script_run(&t->jctx, "foo", t->data);
	free(t->data);
}

static struct trigger* _trigger_add(char *type, struct blob_attr *rule, int timeout, void *id)
{
	char *_t;
	struct blob_attr *_r;
	struct trigger *t = calloc_a(sizeof(*t), &_t, strlen(type) + 1, &_r, blob_pad_len(rule));

	t->type = _t;
	t->rule = _r;
	t->delay.cb = trigger_delay_cb;
	t->timeout = timeout;
	t->pending = 0;
	t->remove = 0;
	t->id = id;
	t->jctx.handle_var = rule_handle_var,
	t->jctx.handle_error = rule_handle_error,
	t->jctx.handle_command = rule_handle_command,
	t->jctx.handle_file = rule_load_script,

	strcpy(t->type, type);
	memcpy(t->rule, rule, blob_pad_len(rule));

	list_add(&t->list, &triggers);
	json_script_init(&t->jctx);

	return t;
}

void trigger_add(struct blob_attr *rule, void *id)
{
	struct blob_attr *cur;
	int rem;

	blobmsg_for_each_attr(cur, rule, rem) {
		struct blob_attr *_cur, *type = NULL, *script = NULL, *timeout = NULL;
		int _rem;
		int i = 0;

		if (blobmsg_type(cur) != BLOBMSG_TYPE_ARRAY)
			continue;

		blobmsg_for_each_attr(_cur, cur, _rem) {
			switch (i++) {
			case 0:
				if (blobmsg_type(_cur) == BLOBMSG_TYPE_STRING)
					type = _cur;
				break;

			case 1:
				if (blobmsg_type(_cur) == BLOBMSG_TYPE_ARRAY)
					script = _cur;
				break;

			case 2:
				if (blobmsg_type(_cur) == BLOBMSG_TYPE_INT32)
					timeout = _cur;
				break;
			}
		}

		if (type && script) {
			int t = 0;

			if (timeout)
				t = blobmsg_get_u32(timeout);
			_trigger_add(blobmsg_get_string(type), script, t, id);
		}
	}
}

void trigger_del(void *id)
{
	struct trigger *t, *n;

	list_for_each_entry_safe(t, n, &triggers, list) {
		if (t->id != id)
			continue;

		if (t->pending) {
			t->remove = 1;
			continue;
		}
		list_del(&t->list);
		free(t);
	}
}

void trigger_init(void)
{
	runqueue_init(&q);
	q.empty_cb = q_empty;
	q.max_running_tasks = 1;
}

void trigger_event(char *type, struct blob_attr *data)
{
	struct trigger *t;

	list_for_each_entry(t, &triggers, list) {
		if (t->pending || t->remove)
			continue;
		if (!strcmp(t->type, type)) {
			if (t->timeout) {
				t->data = malloc(blob_pad_len(data));
				memcpy(t->data, data, blob_pad_len(data));
				uloop_timeout_set(&t->delay, t->timeout);
			} else {
				json_script_run(&t->jctx, "foo", data);
			}
		}
	}
}