summaryrefslogtreecommitdiff
path: root/service/trigger.c
blob: 440830b26b72ff9afeac701cdd1fb89c6234ef58 (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
/*
 * 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 <libubox/blobmsg_json.h>
#include <libubox/json_script.h>
#include <libubox/runqueue.h>
#include <libubox/ustream.h>
#include <libubox/uloop.h>
#include <libubox/avl.h>
#include <libubox/avl-cmp.h>

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

#include "../procd.h"

struct trigger {
	struct list_head list;

	void *id;
	char *type;
	int timeout;

	struct blob_attr *rule;
	struct blob_attr *data;

	struct json_script_ctx jctx;
};

struct trigger_command {
	struct avl_node avl;
	struct uloop_timeout delay;
	bool requeue;

	struct runqueue_process proc;
	struct json_script_ctx jctx;

	struct blob_attr data[];
};

static LIST_HEAD(triggers);
static RUNQUEUE(q, 1);
static AVL_TREE(trigger_pending, avl_blobcmp, false, NULL);

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);

	if (strcmp(name, t->type) != 0)
		return NULL;

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

static void trigger_free(struct trigger *t)
{
	json_script_free(&t->jctx);
	free(t->data);
	list_del(&t->list);
	free(t);
}

static void trigger_command_complete(struct runqueue *q, struct runqueue_task *p)
{
	struct trigger_command *cmd = container_of(p, struct trigger_command, proc.task);

	if (cmd->requeue) {
		cmd->requeue = false;
		runqueue_task_add(q, p, false);
		return;
	}

	avl_delete(&trigger_pending, &cmd->avl);
	free(cmd);
}

static void trigger_command_run(struct runqueue *q, struct runqueue_task *t)
{
	struct trigger_command *cmd = container_of(t, struct trigger_command, proc.task);
	struct blob_attr *cur;
	char **argv;
	pid_t pid;
	int n = 0;
	int rem;

	pid = fork();
	if (pid < 0) {
		trigger_command_complete(q, t);
		return;
	}

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

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

	blobmsg_for_each_attr(cur, cmd->data, rem)
		n++;

	argv = alloca((n + 1) * sizeof(*argv));
	n = 0;
	blobmsg_for_each_attr(cur, cmd->data, rem)
		argv[n++] = blobmsg_get_string(cur);
	argv[n] = NULL;

	if (n > 0)
		execvp(argv[0], &argv[0]);

	exit(1);
}

static void trigger_command_start(struct uloop_timeout *timeout)
{
	static const struct runqueue_task_type trigger_command_type = {
		.run = trigger_command_run,
		.cancel = runqueue_process_cancel_cb,
		.kill = runqueue_process_kill_cb,
	};
	struct trigger_command *cmd = container_of(timeout, struct trigger_command, delay);

	cmd->proc.task.type = &trigger_command_type;
	cmd->proc.task.complete = trigger_command_complete;
	runqueue_task_add(&q, &cmd->proc.task, false);
}

static void trigger_command_add(struct trigger *t, struct blob_attr *data)
{
	struct trigger_command *cmd;
	int remaining;

	cmd = avl_find_element(&trigger_pending, data, cmd, avl);
	if (cmd) {
		/* Command currently running? */
		if (!cmd->delay.pending) {
			cmd->requeue = true;
			return;
		}

		/* Extend timer if trigger timeout is bigger than remaining time */
		remaining = uloop_timeout_remaining(&cmd->delay);
		if (remaining < t->timeout)
			uloop_timeout_set(&cmd->delay, t->timeout);

		return;
	}

	cmd = calloc(1, sizeof(*cmd) + blob_pad_len(data));
	if (!cmd)
		return;

	cmd->avl.key = cmd->data;
	cmd->delay.cb = trigger_command_start;
	memcpy(cmd->data, data, blob_pad_len(data));
	avl_insert(&trigger_pending, &cmd->avl);
	uloop_timeout_set(&cmd->delay, t->timeout > 0 ? t->timeout : 1);
}

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);

	if (!strcmp(name, "run_script")) {
		trigger_command_add(t, exec);
		return;
	}
}

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 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->timeout = timeout;
	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;

		trigger_free(t);
	}
}

static bool trigger_match(const char *event, const char *match)
{
	char *wildcard = strstr(match, ".*");
	if (wildcard)
		return !strncmp(event, match, wildcard - match);
	return !strcmp(event, match);
}

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

	list_for_each_entry(t, &triggers, list) {
		if (!trigger_match(type, t->type))
			continue;
		json_script_run(&t->jctx, t->type, data);
	}
}