libubus: implement file descriptor passing support
[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 *hdr)
95 {
96 void (*cb)(struct ubus_context *, struct ubus_msghdr *,
97 struct ubus_object *, struct blob_attr **);
98 struct blob_attr **attrbuf;
99 struct ubus_object *obj;
100 uint32_t objid;
101
102 attrbuf = ubus_parse_msg(ubus_msghdr_data(hdr));
103 if (!attrbuf[UBUS_ATTR_OBJID])
104 return;
105
106 objid = blob_get_u32(attrbuf[UBUS_ATTR_OBJID]);
107 obj = avl_find_element(&ctx->objects, &objid, obj, avl);
108
109 switch (hdr->type) {
110 case UBUS_MSG_INVOKE:
111 cb = ubus_process_invoke;
112 break;
113 case UBUS_MSG_UNSUBSCRIBE:
114 cb = ubus_process_unsubscribe;
115 break;
116 case UBUS_MSG_NOTIFY:
117 cb = ubus_process_notify;
118 break;
119 default:
120 return;
121 }
122 cb(ctx, hdr, obj, attrbuf);
123 }
124
125 static void ubus_add_object_cb(struct ubus_request *req, int type, struct blob_attr *msg)
126 {
127 struct ubus_object *obj = req->priv;
128 struct blob_attr **attrbuf = ubus_parse_msg(msg);
129
130 if (!attrbuf[UBUS_ATTR_OBJID])
131 return;
132
133 obj->id = blob_get_u32(attrbuf[UBUS_ATTR_OBJID]);
134
135 if (attrbuf[UBUS_ATTR_OBJTYPE])
136 obj->type->id = blob_get_u32(attrbuf[UBUS_ATTR_OBJTYPE]);
137
138 obj->avl.key = &obj->id;
139 avl_insert(&req->ctx->objects, &obj->avl);
140 }
141
142 static void ubus_push_method_data(const struct ubus_method *m)
143 {
144 void *mtbl;
145 int i;
146
147 mtbl = blobmsg_open_table(&b, m->name);
148
149 for (i = 0; i < m->n_policy; i++)
150 blobmsg_add_u32(&b, m->policy[i].name, m->policy[i].type);
151
152 blobmsg_close_table(&b, mtbl);
153 }
154
155 static bool ubus_push_object_type(const struct ubus_object_type *type)
156 {
157 void *s;
158 int i;
159
160 s = blob_nest_start(&b, UBUS_ATTR_SIGNATURE);
161
162 for (i = 0; i < type->n_methods; i++)
163 ubus_push_method_data(&type->methods[i]);
164
165 blob_nest_end(&b, s);
166
167 return true;
168 }
169
170 int ubus_add_object(struct ubus_context *ctx, struct ubus_object *obj)
171 {
172 struct ubus_request req;
173 int ret;
174
175 blob_buf_init(&b, 0);
176
177 if (obj->name && obj->type) {
178 blob_put_string(&b, UBUS_ATTR_OBJPATH, obj->name);
179
180 if (obj->type->id)
181 blob_put_int32(&b, UBUS_ATTR_OBJTYPE, obj->type->id);
182 else if (!ubus_push_object_type(obj->type))
183 return UBUS_STATUS_INVALID_ARGUMENT;
184 }
185
186 if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_ADD_OBJECT, 0) < 0)
187 return UBUS_STATUS_INVALID_ARGUMENT;
188
189 req.raw_data_cb = ubus_add_object_cb;
190 req.priv = obj;
191 ret = ubus_complete_request(ctx, &req, 0);
192 if (ret)
193 return ret;
194
195 if (!obj->id)
196 return UBUS_STATUS_NO_DATA;
197
198 return 0;
199 }
200
201 static void ubus_remove_object_cb(struct ubus_request *req, int type, struct blob_attr *msg)
202 {
203 struct ubus_object *obj = req->priv;
204 struct blob_attr **attrbuf = ubus_parse_msg(msg);
205
206 if (!attrbuf[UBUS_ATTR_OBJID])
207 return;
208
209 obj->id = 0;
210
211 if (attrbuf[UBUS_ATTR_OBJTYPE] && obj->type)
212 obj->type->id = 0;
213
214 avl_delete(&req->ctx->objects, &obj->avl);
215 }
216
217 int ubus_remove_object(struct ubus_context *ctx, struct ubus_object *obj)
218 {
219 struct ubus_request req;
220 int ret;
221
222 blob_buf_init(&b, 0);
223 blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id);
224
225 if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_REMOVE_OBJECT, 0) < 0)
226 return UBUS_STATUS_INVALID_ARGUMENT;
227
228 req.raw_data_cb = ubus_remove_object_cb;
229 req.priv = obj;
230 ret = ubus_complete_request(ctx, &req, 0);
231 if (ret)
232 return ret;
233
234 if (obj->id)
235 return UBUS_STATUS_NO_DATA;
236
237 return 0;
238 }