summaryrefslogtreecommitdiff
path: root/ubusd_monitor.c
blob: fcbc6a4b10c0199ac30ef545fea66cdf533e00eb (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
/*
 * Copyright (C) 2015 Felix Fietkau <nbd@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 "ubusd.h"

static struct ubus_object *monitor_obj;
static LIST_HEAD(monitors);

struct ubus_monitor {
	struct list_head list;
	struct ubus_client *cl;
	uint32_t seq;
};

static void
ubusd_monitor_free(struct ubus_monitor *m)
{
	list_del(&m->list);
	free(m);
}

static void
ubusd_monitor_connect(struct ubus_client *cl, struct ubus_msg_buf *ub)
{
	struct ubus_monitor *m;

	ubusd_monitor_disconnect(cl);

	m = calloc(1, sizeof(*m));
	m->cl = cl;
	list_add(&m->list, &monitors);
}

void
ubusd_monitor_disconnect(struct ubus_client *cl)
{
	struct ubus_monitor *m;

	list_for_each_entry(m, &monitors, list) {
		if (m->cl != cl)
			continue;

		ubusd_monitor_free(m);
		return;
	}
}

void
ubusd_monitor_message(struct ubus_client *cl, struct ubus_msg_buf *ub, bool send)
{
	static struct blob_buf mb;
	struct ubus_monitor *m;

	if (list_empty(&monitors))
		return;

	blob_buf_init(&mb, 0);
	blob_put_int32(&mb, UBUS_MONITOR_CLIENT, cl->id.id);
	blob_put_int32(&mb, UBUS_MONITOR_PEER, ub->hdr.peer);
	blob_put_int32(&mb, UBUS_MONITOR_SEQ, ub->hdr.seq);
	blob_put_int32(&mb, UBUS_MONITOR_TYPE, ub->hdr.type);
	blob_put_int8(&mb, UBUS_MONITOR_SEND, send);
	blob_put(&mb, UBUS_MONITOR_DATA, blob_data(ub->data), blob_len(ub->data));

	ub = ubus_msg_new(mb.head, blob_raw_len(mb.head), true);
	ub->hdr.type = UBUS_MSG_MONITOR;

	list_for_each_entry(m, &monitors, list) {
		ub->hdr.seq = ++m->seq;
		ubus_msg_send(m->cl, ub);
	}

	ubus_msg_free(ub);
}

static int
ubusd_monitor_recv(struct ubus_client *cl, struct ubus_msg_buf *ub,
		   const char *method, struct blob_attr *msg)
{
	/* Only root is allowed for now */
	if (cl->uid != 0 || cl->gid != 0)
		return UBUS_STATUS_PERMISSION_DENIED;

	if (!strcmp(method, "add")) {
		ubusd_monitor_connect(cl, ub);
		return 0;
	}

	if (!strcmp(method, "remove")) {
		ubusd_monitor_disconnect(cl);
		return 0;
	}

	return UBUS_STATUS_METHOD_NOT_FOUND;
}

void
ubusd_monitor_init(void)
{
	monitor_obj = ubusd_create_object_internal(NULL, UBUS_SYSTEM_OBJECT_MONITOR);
	if (monitor_obj != NULL)
		monitor_obj->recv_msg = ubusd_monitor_recv;
}