ubusd: rename goto label from `error` to `out`
[project/ubus.git] / ubusd_monitor.c
1 /*
2  * Copyright (C) 2015 Felix Fietkau <nbd@openwrt.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License version 2.1
6  * as published by the Free Software Foundation
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  */
13
14 #include "ubusd.h"
15
16 static struct ubus_object *monitor_obj;
17 static LIST_HEAD(monitors);
18
19 struct ubus_monitor {
20         struct list_head list;
21         struct ubus_client *cl;
22         uint32_t seq;
23 };
24
25 static void
26 ubusd_monitor_free(struct ubus_monitor *m)
27 {
28         list_del(&m->list);
29         free(m);
30 }
31
32 static void
33 ubusd_monitor_connect(struct ubus_client *cl, struct ubus_msg_buf *ub)
34 {
35         struct ubus_monitor *m;
36
37         ubusd_monitor_disconnect(cl);
38
39         m = calloc(1, sizeof(*m));
40         m->cl = cl;
41         list_add(&m->list, &monitors);
42 }
43
44 void
45 ubusd_monitor_disconnect(struct ubus_client *cl)
46 {
47         struct ubus_monitor *m;
48
49         list_for_each_entry(m, &monitors, list) {
50                 if (m->cl != cl)
51                         continue;
52
53                 ubusd_monitor_free(m);
54                 return;
55         }
56 }
57
58 void
59 ubusd_monitor_message(struct ubus_client *cl, struct ubus_msg_buf *ub, bool send)
60 {
61         static struct blob_buf mb;
62         struct ubus_monitor *m;
63
64         if (list_empty(&monitors))
65                 return;
66
67         blob_buf_init(&mb, 0);
68         blob_put_int32(&mb, UBUS_MONITOR_CLIENT, cl->id.id);
69         blob_put_int32(&mb, UBUS_MONITOR_PEER, ub->hdr.peer);
70         blob_put_int32(&mb, UBUS_MONITOR_SEQ, ub->hdr.seq);
71         blob_put_int32(&mb, UBUS_MONITOR_TYPE, ub->hdr.type);
72         blob_put_int8(&mb, UBUS_MONITOR_SEND, send);
73         blob_put(&mb, UBUS_MONITOR_DATA, blob_data(ub->data), blob_len(ub->data));
74
75         list_for_each_entry(m, &monitors, list) {
76                 ub = ubus_msg_new(mb.head, blob_raw_len(mb.head), true);
77                 ub->hdr.type = UBUS_MSG_MONITOR;
78                 ub->hdr.seq = ++m->seq;
79                 ubus_msg_send(m->cl, ub);
80                 ubus_msg_free(ub);
81         }
82 }
83
84 static int
85 ubusd_monitor_recv(struct ubus_client *cl, struct ubus_msg_buf *ub,
86                    const char *method, struct blob_attr *msg)
87 {
88         /* Only root is allowed for now */
89         if (cl->uid != 0 || cl->gid != 0)
90                 return UBUS_STATUS_PERMISSION_DENIED;
91
92         if (!strcmp(method, "add")) {
93                 ubusd_monitor_connect(cl, ub);
94                 return 0;
95         }
96
97         if (!strcmp(method, "remove")) {
98                 ubusd_monitor_disconnect(cl);
99                 return 0;
100         }
101
102         return UBUS_STATUS_METHOD_NOT_FOUND;
103 }
104
105 void
106 ubusd_monitor_init(void)
107 {
108         monitor_obj = ubusd_create_object_internal(NULL, UBUS_SYSTEM_OBJECT_MONITOR);
109         if (monitor_obj != NULL)
110                 monitor_obj->recv_msg = ubusd_monitor_recv;
111 }