ubus: Fix issues reported by static code analysis tool Klocwork
[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
73         for (method = 0; method < obj->n_methods; method++)
74                 if (!obj->methods[method].name ||
75                     !strcmp(obj->methods[method].name,
76                             blob_data(attrbuf[UBUS_ATTR_METHOD])))
77                         goto found;
78
79         /* not found */
80         ret = UBUS_STATUS_METHOD_NOT_FOUND;
81         goto send;
82
83 found:
84         ret = obj->methods[method].handler(ctx, obj, &req,
85                                            blob_data(attrbuf[UBUS_ATTR_METHOD]),
86                                            attrbuf[UBUS_ATTR_DATA]);
87         if (req.deferred || no_reply)
88                 return;
89
90 send:
91         ubus_complete_deferred_request(ctx, &req, ret);
92 }
93
94 void __hidden ubus_process_obj_msg(struct ubus_context *ctx, struct ubus_msghdr_buf *buf)
95 {
96         void (*cb)(struct ubus_context *, struct ubus_msghdr *,
97                    struct ubus_object *, struct blob_attr **);
98         struct ubus_msghdr *hdr = &buf->hdr;
99         struct blob_attr **attrbuf;
100         struct ubus_object *obj;
101         uint32_t objid;
102         void *prev_data = NULL;
103
104         attrbuf = ubus_parse_msg(buf->data);
105         if (!attrbuf[UBUS_ATTR_OBJID])
106                 return;
107
108         objid = blob_get_u32(attrbuf[UBUS_ATTR_OBJID]);
109         obj = avl_find_element(&ctx->objects, &objid, obj, avl);
110
111         switch (hdr->type) {
112         case UBUS_MSG_INVOKE:
113                 cb = ubus_process_invoke;
114                 break;
115         case UBUS_MSG_UNSUBSCRIBE:
116                 cb = ubus_process_unsubscribe;
117                 break;
118         case UBUS_MSG_NOTIFY:
119                 cb = ubus_process_notify;
120                 break;
121         default:
122                 return;
123         }
124
125         if (buf == &ctx->msgbuf) {
126                 prev_data = buf->data;
127                 buf->data = NULL;
128         }
129
130         cb(ctx, hdr, obj, attrbuf);
131
132         if (prev_data) {
133                 if (buf->data)
134                         free(prev_data);
135                 else
136                         buf->data = prev_data;
137         }
138 }
139
140 static void ubus_add_object_cb(struct ubus_request *req, int type, struct blob_attr *msg)
141 {
142         struct ubus_object *obj = req->priv;
143         struct blob_attr **attrbuf = ubus_parse_msg(msg);
144
145         if (!attrbuf[UBUS_ATTR_OBJID])
146                 return;
147
148         obj->id = blob_get_u32(attrbuf[UBUS_ATTR_OBJID]);
149
150         if (attrbuf[UBUS_ATTR_OBJTYPE])
151                 obj->type->id = blob_get_u32(attrbuf[UBUS_ATTR_OBJTYPE]);
152
153         obj->avl.key = &obj->id;
154         avl_insert(&req->ctx->objects, &obj->avl);
155 }
156
157 static void ubus_push_method_data(const struct ubus_method *m)
158 {
159         void *mtbl;
160         int i;
161
162         mtbl = blobmsg_open_table(&b, m->name);
163
164         for (i = 0; i < m->n_policy; i++) {
165                 if (m->mask && !(m->mask & (1 << i)))
166                         continue;
167
168                 blobmsg_add_u32(&b, m->policy[i].name, m->policy[i].type);
169         }
170
171         blobmsg_close_table(&b, mtbl);
172 }
173
174 static bool ubus_push_object_type(const struct ubus_object_type *type)
175 {
176         void *s;
177         int i;
178
179         s = blob_nest_start(&b, UBUS_ATTR_SIGNATURE);
180
181         for (i = 0; i < type->n_methods; i++)
182                 ubus_push_method_data(&type->methods[i]);
183
184         blob_nest_end(&b, s);
185
186         return true;
187 }
188
189 int ubus_add_object(struct ubus_context *ctx, struct ubus_object *obj)
190 {
191         struct ubus_request req;
192         int ret;
193
194         blob_buf_init(&b, 0);
195
196         if (obj->name && obj->type) {
197                 blob_put_string(&b, UBUS_ATTR_OBJPATH, obj->name);
198
199                 if (obj->type->id)
200                         blob_put_int32(&b, UBUS_ATTR_OBJTYPE, obj->type->id);
201                 else if (!ubus_push_object_type(obj->type))
202                         return UBUS_STATUS_INVALID_ARGUMENT;
203         }
204
205         if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_ADD_OBJECT, 0) < 0)
206                 return UBUS_STATUS_INVALID_ARGUMENT;
207
208         req.raw_data_cb = ubus_add_object_cb;
209         req.priv = obj;
210         ret = ubus_complete_request(ctx, &req, 0);
211         if (ret)
212                 return ret;
213
214         if (!obj->id)
215                 return UBUS_STATUS_NO_DATA;
216
217         return 0;
218 }
219
220 static void ubus_remove_object_cb(struct ubus_request *req, int type, struct blob_attr *msg)
221 {
222         struct ubus_object *obj = req->priv;
223         struct blob_attr **attrbuf = ubus_parse_msg(msg);
224
225         if (!attrbuf[UBUS_ATTR_OBJID])
226                 return;
227
228         obj->id = 0;
229
230         if (attrbuf[UBUS_ATTR_OBJTYPE] && obj->type)
231                 obj->type->id = 0;
232
233         avl_delete(&req->ctx->objects, &obj->avl);
234 }
235
236 int ubus_remove_object(struct ubus_context *ctx, struct ubus_object *obj)
237 {
238         struct ubus_request req;
239         int ret;
240
241         blob_buf_init(&b, 0);
242         blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id);
243
244         if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_REMOVE_OBJECT, 0) < 0)
245                 return UBUS_STATUS_INVALID_ARGUMENT;
246
247         req.raw_data_cb = ubus_remove_object_cb;
248         req.priv = obj;
249         ret = ubus_complete_request(ctx, &req, 0);
250         if (ret)
251                 return ret;
252
253         if (obj->id)
254                 return UBUS_STATUS_NO_DATA;
255
256         return 0;
257 }