libubus: fix stack space init for processing requests
[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 void __hidden ubus_process_invoke(struct ubus_context *ctx, struct ubus_msghdr *hdr)
18 {
19         struct blob_attr **attrbuf;
20         struct ubus_request_data req = {};
21         struct ubus_object *obj;
22         int method;
23         int ret = 0;
24
25         req.peer = hdr->peer;
26         req.seq = hdr->seq;
27         attrbuf = ubus_parse_msg(hdr->data);
28
29         if (!attrbuf[UBUS_ATTR_OBJID])
30                 return;
31
32         req.object = blob_get_u32(attrbuf[UBUS_ATTR_OBJID]);
33
34         if (!attrbuf[UBUS_ATTR_METHOD]) {
35                 ret = UBUS_STATUS_INVALID_ARGUMENT;
36                 goto send;
37         }
38
39         obj = avl_find_element(&ctx->objects, &req.object, obj, avl);
40         if (!obj) {
41                 ret = UBUS_STATUS_NOT_FOUND;
42                 goto send;
43         }
44
45         for (method = 0; method < obj->n_methods; method++)
46                 if (!obj->methods[method].name ||
47                     !strcmp(obj->methods[method].name,
48                             blob_data(attrbuf[UBUS_ATTR_METHOD])))
49                         goto found;
50
51         /* not found */
52         ret = UBUS_STATUS_METHOD_NOT_FOUND;
53         goto send;
54
55 found:
56         ret = obj->methods[method].handler(ctx, obj, &req,
57                                            blob_data(attrbuf[UBUS_ATTR_METHOD]),
58                                            attrbuf[UBUS_ATTR_DATA]);
59         if (req.deferred)
60                 return;
61
62 send:
63         ubus_complete_deferred_request(ctx, &req, ret);
64 }
65
66 static void ubus_add_object_cb(struct ubus_request *req, int type, struct blob_attr *msg)
67 {
68         struct ubus_object *obj = req->priv;
69         struct blob_attr **attrbuf = ubus_parse_msg(msg);
70
71         if (!attrbuf[UBUS_ATTR_OBJID])
72                 return;
73
74         obj->id = blob_get_u32(attrbuf[UBUS_ATTR_OBJID]);
75
76         if (attrbuf[UBUS_ATTR_OBJTYPE])
77                 obj->type->id = blob_get_u32(attrbuf[UBUS_ATTR_OBJTYPE]);
78
79         obj->avl.key = &obj->id;
80         avl_insert(&req->ctx->objects, &obj->avl);
81 }
82
83 static void ubus_push_method_data(const struct ubus_method *m)
84 {
85         void *mtbl;
86         int i;
87
88         mtbl = blobmsg_open_table(&b, m->name);
89
90         for (i = 0; i < m->n_policy; i++)
91                 blobmsg_add_u32(&b, m->policy[i].name, m->policy[i].type);
92
93         blobmsg_close_table(&b, mtbl);
94 }
95
96 static bool ubus_push_object_type(const struct ubus_object_type *type)
97 {
98         void *s;
99         int i;
100
101         s = blob_nest_start(&b, UBUS_ATTR_SIGNATURE);
102
103         for (i = 0; i < type->n_methods; i++)
104                 ubus_push_method_data(&type->methods[i]);
105
106         blob_nest_end(&b, s);
107
108         return true;
109 }
110
111 int ubus_add_object(struct ubus_context *ctx, struct ubus_object *obj)
112 {
113         struct ubus_request req;
114         int ret;
115
116         blob_buf_init(&b, 0);
117
118         if (obj->name && obj->type) {
119                 blob_put_string(&b, UBUS_ATTR_OBJPATH, obj->name);
120
121                 if (obj->type->id)
122                         blob_put_int32(&b, UBUS_ATTR_OBJTYPE, obj->type->id);
123                 else if (!ubus_push_object_type(obj->type))
124                         return UBUS_STATUS_INVALID_ARGUMENT;
125         }
126
127         if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_ADD_OBJECT, 0) < 0)
128                 return UBUS_STATUS_INVALID_ARGUMENT;
129
130         req.raw_data_cb = ubus_add_object_cb;
131         req.priv = obj;
132         ret = ubus_complete_request(ctx, &req, 0);
133         if (ret)
134                 return ret;
135
136         if (!obj->id)
137                 return UBUS_STATUS_NO_DATA;
138
139         return 0;
140 }
141
142 static void ubus_remove_object_cb(struct ubus_request *req, int type, struct blob_attr *msg)
143 {
144         struct ubus_object *obj = req->priv;
145         struct blob_attr **attrbuf = ubus_parse_msg(msg);
146
147         if (!attrbuf[UBUS_ATTR_OBJID])
148                 return;
149
150         obj->id = 0;
151
152         if (attrbuf[UBUS_ATTR_OBJTYPE] && obj->type)
153                 obj->type->id = 0;
154
155         avl_delete(&req->ctx->objects, &obj->avl);
156 }
157
158 int ubus_remove_object(struct ubus_context *ctx, struct ubus_object *obj)
159 {
160         struct ubus_request req;
161         int ret;
162
163         blob_buf_init(&b, 0);
164         blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id);
165
166         if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_REMOVE_OBJECT, 0) < 0)
167                 return UBUS_STATUS_INVALID_ARGUMENT;
168
169         req.raw_data_cb = ubus_remove_object_cb;
170         req.priv = obj;
171         ret = ubus_complete_request(ctx, &req, 0);
172         if (ret)
173                 return ret;
174
175         if (obj->id)
176                 return UBUS_STATUS_NO_DATA;
177
178         return 0;
179 }