• source navigation  • diff markup  • identifier search  • freetext search  • 

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

This page was automatically generated by LXR 0.3.1.  •  OpenWrt