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