cli: register event handler first, then do lookup
[project/ubus.git] / libubus-obj.c
1 /*
2 * Copyright (C) 2011-2012 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 "libubus.h"
15 #include "libubus-internal.h"
16
17 static void
18 ubus_process_unsubscribe(struct ubus_context *ctx, struct ubus_msghdr *hdr,
19 struct ubus_object *obj, struct blob_attr **attrbuf)
20 {
21 struct ubus_subscriber *s;
22
23 if (!obj || !attrbuf[UBUS_ATTR_TARGET])
24 return;
25
26 if (obj->methods != &watch_method)
27 return;
28
29 s = container_of(obj, struct ubus_subscriber, obj);
30 if (s->remove_cb)
31 s->remove_cb(ctx, s, blob_get_u32(attrbuf[UBUS_ATTR_TARGET]));
32 }
33
34 static void
35 ubus_process_notify(struct ubus_context *ctx, struct ubus_msghdr *hdr,
36 struct ubus_object *obj, struct blob_attr **attrbuf)
37 {
38 if (!obj || !attrbuf[UBUS_ATTR_ACTIVE])
39 return;
40
41 obj->has_subscribers = blob_get_u8(attrbuf[UBUS_ATTR_ACTIVE]);
42 if (obj->subscribe_cb)
43 obj->subscribe_cb(ctx, obj);
44 }
45 static void
46 ubus_process_invoke(struct ubus_context *ctx, struct ubus_msghdr *hdr,
47 struct ubus_object *obj, struct blob_attr **attrbuf)
48 {
49 struct ubus_request_data req = {
50 .fd = -1,
51 };
52 int method;
53 int ret;
54 bool no_reply = false;
55
56 if (!obj) {
57 ret = UBUS_STATUS_NOT_FOUND;
58 goto send;
59 }
60
61 if (!attrbuf[UBUS_ATTR_METHOD]) {
62 ret = UBUS_STATUS_INVALID_ARGUMENT;
63 goto send;
64 }
65
66 if (attrbuf[UBUS_ATTR_NO_REPLY])
67 no_reply = blob_get_int8(attrbuf[UBUS_ATTR_NO_REPLY]);
68
69 req.peer = hdr->peer;
70 req.seq = hdr->seq;
71 req.object = obj->id;
72 if (attrbuf[UBUS_ATTR_USER] && attrbuf[UBUS_ATTR_GROUP]) {
73 req.acl.user = blobmsg_get_string(attrbuf[UBUS_ATTR_USER]);
74 req.acl.group = blobmsg_get_string(attrbuf[UBUS_ATTR_GROUP]);
75 req.acl.object = obj->name;
76 }
77 for (method = 0; method < obj->n_methods; method++)
78 if (!obj->methods[method].name ||
79 !strcmp(obj->methods[method].name,
80 blob_data(attrbuf[UBUS_ATTR_METHOD])))
81 goto found;
82
83 /* not found */
84 ret = UBUS_STATUS_METHOD_NOT_FOUND;
85 goto send;
86
87 found:
88 ret = obj->methods[method].handler(ctx, obj, &req,
89 blob_data(attrbuf[UBUS_ATTR_METHOD]),
90 attrbuf[UBUS_ATTR_DATA]);
91 if (req.deferred || no_reply)
92 return;
93
94 send:
95 ubus_complete_deferred_request(ctx, &req, ret);
96 }
97
98 void __hidden ubus_process_obj_msg(struct ubus_context *ctx, struct ubus_msghdr_buf *buf)
99 {
100 void (*cb)(struct ubus_context *, struct ubus_msghdr *,
101 struct ubus_object *, struct blob_attr **);
102 struct ubus_msghdr *hdr = &buf->hdr;
103 struct blob_attr **attrbuf;
104 struct ubus_object *obj;
105 uint32_t objid;
106 void *prev_data = NULL;
107
108 attrbuf = ubus_parse_msg(buf->data);
109 if (!attrbuf[UBUS_ATTR_OBJID])
110 return;
111
112 objid = blob_get_u32(attrbuf[UBUS_ATTR_OBJID]);
113 obj = avl_find_element(&ctx->objects, &objid, obj, avl);
114
115 switch (hdr->type) {
116 case UBUS_MSG_INVOKE:
117 cb = ubus_process_invoke;
118 break;
119 case UBUS_MSG_UNSUBSCRIBE:
120 cb = ubus_process_unsubscribe;
121 break;
122 case UBUS_MSG_NOTIFY:
123 cb = ubus_process_notify;
124 break;
125 default:
126 return;
127 }
128
129 if (buf == &ctx->msgbuf) {
130 prev_data = buf->data;
131 buf->data = NULL;
132 }
133
134 cb(ctx, hdr, obj, attrbuf);
135
136 if (prev_data) {
137 if (buf->data)
138 free(prev_data);
139 else
140 buf->data = prev_data;
141 }
142 }
143
144 static void ubus_add_object_cb(struct ubus_request *req, int type, struct blob_attr *msg)
145 {
146 struct ubus_object *obj = req->priv;
147 struct blob_attr **attrbuf = ubus_parse_msg(msg);
148
149 if (!attrbuf[UBUS_ATTR_OBJID])
150 return;
151
152 obj->id = blob_get_u32(attrbuf[UBUS_ATTR_OBJID]);
153
154 if (attrbuf[UBUS_ATTR_OBJTYPE])
155 obj->type->id = blob_get_u32(attrbuf[UBUS_ATTR_OBJTYPE]);
156
157 obj->avl.key = &obj->id;
158 avl_insert(&req->ctx->objects, &obj->avl);
159 }
160
161 static void ubus_push_method_data(const struct ubus_method *m)
162 {
163 void *mtbl;
164 int i;
165
166 mtbl = blobmsg_open_table(&b, m->name);
167
168 for (i = 0; i < m->n_policy; i++) {
169 if (m->mask && !(m->mask & (1 << i)))
170 continue;
171
172 blobmsg_add_u32(&b, m->policy[i].name, m->policy[i].type);
173 }
174
175 blobmsg_close_table(&b, mtbl);
176 }
177
178 static bool ubus_push_object_type(const struct ubus_object_type *type)
179 {
180 void *s;
181 int i;
182
183 s = blob_nest_start(&b, UBUS_ATTR_SIGNATURE);
184
185 for (i = 0; i < type->n_methods; i++)
186 ubus_push_method_data(&type->methods[i]);
187
188 blob_nest_end(&b, s);
189
190 return true;
191 }
192
193 int ubus_add_object(struct ubus_context *ctx, struct ubus_object *obj)
194 {
195 struct ubus_request req;
196 int ret;
197
198 blob_buf_init(&b, 0);
199
200 if (obj->name && obj->type) {
201 blob_put_string(&b, UBUS_ATTR_OBJPATH, obj->name);
202
203 if (obj->type->id)
204 blob_put_int32(&b, UBUS_ATTR_OBJTYPE, obj->type->id);
205 else if (!ubus_push_object_type(obj->type))
206 return UBUS_STATUS_INVALID_ARGUMENT;
207 }
208
209 if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_ADD_OBJECT, 0) < 0)
210 return UBUS_STATUS_INVALID_ARGUMENT;
211
212 req.raw_data_cb = ubus_add_object_cb;
213 req.priv = obj;
214 ret = ubus_complete_request(ctx, &req, 0);
215 if (ret)
216 return ret;
217
218 if (!obj->id)
219 return UBUS_STATUS_NO_DATA;
220
221 return 0;
222 }
223
224 static void ubus_remove_object_cb(struct ubus_request *req, int type, struct blob_attr *msg)
225 {
226 struct ubus_object *obj = req->priv;
227 struct blob_attr **attrbuf = ubus_parse_msg(msg);
228
229 if (!attrbuf[UBUS_ATTR_OBJID])
230 return;
231
232 obj->id = 0;
233
234 if (attrbuf[UBUS_ATTR_OBJTYPE] && obj->type)
235 obj->type->id = 0;
236
237 avl_delete(&req->ctx->objects, &obj->avl);
238 }
239
240 int ubus_remove_object(struct ubus_context *ctx, struct ubus_object *obj)
241 {
242 struct ubus_request req;
243 int ret;
244
245 blob_buf_init(&b, 0);
246 blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id);
247
248 if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_REMOVE_OBJECT, 0) < 0)
249 return UBUS_STATUS_INVALID_ARGUMENT;
250
251 req.raw_data_cb = ubus_remove_object_cb;
252 req.priv = obj;
253 ret = ubus_complete_request(ctx, &req, 0);
254 if (ret)
255 return ret;
256
257 if (obj->id)
258 return UBUS_STATUS_NO_DATA;
259
260 return 0;
261 }