libubus: fix deferring invoke processing for non-uloop usage
[project/ubus.git] / libubus.c
1 /*
2  * Copyright (C) 2011-2014 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 <sys/types.h>
15 #include <sys/socket.h>
16 #include <unistd.h>
17
18 #include <libubox/blob.h>
19 #include <libubox/blobmsg.h>
20
21 #include "libubus.h"
22 #include "libubus-internal.h"
23 #include "ubusmsg.h"
24
25 const char *__ubus_strerror[__UBUS_STATUS_LAST] = {
26         [UBUS_STATUS_OK] = "Success",
27         [UBUS_STATUS_INVALID_COMMAND] = "Invalid command",
28         [UBUS_STATUS_INVALID_ARGUMENT] = "Invalid argument",
29         [UBUS_STATUS_METHOD_NOT_FOUND] = "Method not found",
30         [UBUS_STATUS_NOT_FOUND] = "Not found",
31         [UBUS_STATUS_NO_DATA] = "No response",
32         [UBUS_STATUS_PERMISSION_DENIED] = "Permission denied",
33         [UBUS_STATUS_TIMEOUT] = "Request timed out",
34         [UBUS_STATUS_NOT_SUPPORTED] = "Operation not supported",
35         [UBUS_STATUS_UNKNOWN_ERROR] = "Unknown error",
36         [UBUS_STATUS_CONNECTION_FAILED] = "Connection failed",
37 };
38
39 struct blob_buf b __hidden = {};
40
41 struct ubus_pending_msg {
42         struct list_head list;
43         struct ubus_msghdr hdr;
44 };
45
46 static int ubus_cmp_id(const void *k1, const void *k2, void *ptr)
47 {
48         const uint32_t *id1 = k1, *id2 = k2;
49
50         if (*id1 < *id2)
51                 return -1;
52         else
53                 return *id1 > *id2;
54 }
55
56 const char *ubus_strerror(int error)
57 {
58         static char err[32];
59
60         if (error < 0 || error >= __UBUS_STATUS_LAST)
61                 goto out;
62
63         if (!__ubus_strerror[error])
64                 goto out;
65
66         return __ubus_strerror[error];
67
68 out:
69         sprintf(err, "Unknown error: %d", error);
70         return err;
71 }
72
73 static void
74 ubus_queue_msg(struct ubus_context *ctx, struct ubus_msghdr *hdr)
75 {
76         struct ubus_pending_msg *pending;
77
78         pending = calloc(1, sizeof(*pending) + blob_raw_len(ubus_msghdr_data(hdr)));
79         if (!pending)
80                 return;
81
82         memcpy(&pending->hdr, hdr, sizeof(*hdr) + blob_raw_len(ubus_msghdr_data(hdr)));
83         list_add(&pending->list, &ctx->pending);
84         if (ctx->sock.registered)
85                 uloop_timeout_set(&ctx->pending_timer, 1);
86 }
87
88 void __hidden
89 ubus_process_msg(struct ubus_context *ctx, struct ubus_msghdr *hdr, int fd)
90 {
91
92         switch(hdr->type) {
93         case UBUS_MSG_STATUS:
94         case UBUS_MSG_DATA:
95                 ubus_process_req_msg(ctx, hdr, fd);
96                 break;
97
98         case UBUS_MSG_INVOKE:
99         case UBUS_MSG_UNSUBSCRIBE:
100         case UBUS_MSG_NOTIFY:
101                 if (ctx->stack_depth) {
102                         ubus_queue_msg(ctx, hdr);
103                         break;
104                 }
105
106                 ubus_process_obj_msg(ctx, hdr);
107                 break;
108         }
109 }
110
111 static void ubus_process_pending_msg(struct uloop_timeout *timeout)
112 {
113         struct ubus_context *ctx = container_of(timeout, struct ubus_context, pending_timer);
114         struct ubus_pending_msg *pending;
115
116         while (!ctx->stack_depth && !list_empty(&ctx->pending)) {
117                 pending = list_first_entry(&ctx->pending, struct ubus_pending_msg, list);
118                 list_del(&pending->list);
119                 ubus_process_msg(ctx, &pending->hdr, -1);
120                 free(pending);
121         }
122 }
123
124 struct ubus_lookup_request {
125         struct ubus_request req;
126         ubus_lookup_handler_t cb;
127 };
128
129 static void ubus_lookup_cb(struct ubus_request *ureq, int type, struct blob_attr *msg)
130 {
131         struct ubus_lookup_request *req;
132         struct ubus_object_data obj;
133         struct blob_attr **attr;
134
135         req = container_of(ureq, struct ubus_lookup_request, req);
136         attr = ubus_parse_msg(msg);
137
138         if (!attr[UBUS_ATTR_OBJID] || !attr[UBUS_ATTR_OBJPATH] ||
139             !attr[UBUS_ATTR_OBJTYPE])
140                 return;
141
142         memset(&obj, 0, sizeof(obj));
143         obj.id = blob_get_u32(attr[UBUS_ATTR_OBJID]);
144         obj.path = blob_data(attr[UBUS_ATTR_OBJPATH]);
145         obj.type_id = blob_get_u32(attr[UBUS_ATTR_OBJTYPE]);
146         obj.signature = attr[UBUS_ATTR_SIGNATURE];
147         req->cb(ureq->ctx, &obj, ureq->priv);
148 }
149
150 int ubus_lookup(struct ubus_context *ctx, const char *path,
151                 ubus_lookup_handler_t cb, void *priv)
152 {
153         struct ubus_lookup_request lookup;
154
155         blob_buf_init(&b, 0);
156         if (path)
157                 blob_put_string(&b, UBUS_ATTR_OBJPATH, path);
158
159         if (ubus_start_request(ctx, &lookup.req, b.head, UBUS_MSG_LOOKUP, 0) < 0)
160                 return UBUS_STATUS_INVALID_ARGUMENT;
161
162         lookup.req.raw_data_cb = ubus_lookup_cb;
163         lookup.req.priv = priv;
164         lookup.cb = cb;
165         return ubus_complete_request(ctx, &lookup.req, 0);
166 }
167
168 static void ubus_lookup_id_cb(struct ubus_request *req, int type, struct blob_attr *msg)
169 {
170         struct blob_attr **attr;
171         uint32_t *id = req->priv;
172
173         attr = ubus_parse_msg(msg);
174
175         if (!attr[UBUS_ATTR_OBJID])
176                 return;
177
178         *id = blob_get_u32(attr[UBUS_ATTR_OBJID]);
179 }
180
181 int ubus_lookup_id(struct ubus_context *ctx, const char *path, uint32_t *id)
182 {
183         struct ubus_request req;
184
185         blob_buf_init(&b, 0);
186         if (path)
187                 blob_put_string(&b, UBUS_ATTR_OBJPATH, path);
188
189         if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_LOOKUP, 0) < 0)
190                 return UBUS_STATUS_INVALID_ARGUMENT;
191
192         req.raw_data_cb = ubus_lookup_id_cb;
193         req.priv = id;
194
195         return ubus_complete_request(ctx, &req, 0);
196 }
197
198 static int ubus_event_cb(struct ubus_context *ctx, struct ubus_object *obj,
199                          struct ubus_request_data *req,
200                          const char *method, struct blob_attr *msg)
201 {
202         struct ubus_event_handler *ev;
203
204         ev = container_of(obj, struct ubus_event_handler, obj);
205         ev->cb(ctx, ev, method, msg);
206         return 0;
207 }
208
209 static const struct ubus_method event_method = {
210         .name = NULL,
211         .handler = ubus_event_cb,
212 };
213
214 int ubus_register_event_handler(struct ubus_context *ctx,
215                                 struct ubus_event_handler *ev,
216                                 const char *pattern)
217 {
218         struct ubus_object *obj = &ev->obj;
219         struct blob_buf b2;
220         int ret;
221
222         if (!obj->id) {
223                 obj->methods = &event_method;
224                 obj->n_methods = 1;
225
226                 if (!!obj->name ^ !!obj->type)
227                         return UBUS_STATUS_INVALID_ARGUMENT;
228
229                 ret = ubus_add_object(ctx, obj);
230                 if (ret)
231                         return ret;
232         }
233
234         /* use a second buffer, ubus_invoke() overwrites the primary one */
235         memset(&b2, 0, sizeof(b2));
236         blob_buf_init(&b2, 0);
237         blobmsg_add_u32(&b2, "object", obj->id);
238         if (pattern)
239                 blobmsg_add_string(&b2, "pattern", pattern);
240
241         return ubus_invoke(ctx, UBUS_SYSTEM_OBJECT_EVENT, "register", b2.head,
242                           NULL, NULL, 0);
243 }
244
245 int ubus_send_event(struct ubus_context *ctx, const char *id,
246                     struct blob_attr *data)
247 {
248         struct ubus_request req;
249         void *s;
250
251         blob_buf_init(&b, 0);
252         blob_put_int32(&b, UBUS_ATTR_OBJID, UBUS_SYSTEM_OBJECT_EVENT);
253         blob_put_string(&b, UBUS_ATTR_METHOD, "send");
254         s = blob_nest_start(&b, UBUS_ATTR_DATA);
255         blobmsg_add_string(&b, "id", id);
256         blobmsg_add_field(&b, BLOBMSG_TYPE_TABLE, "data", blob_data(data), blob_len(data));
257         blob_nest_end(&b, s);
258
259         if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_INVOKE, UBUS_SYSTEM_OBJECT_EVENT) < 0)
260                 return UBUS_STATUS_INVALID_ARGUMENT;
261
262         return ubus_complete_request(ctx, &req, 0);
263 }
264
265 static void ubus_default_connection_lost(struct ubus_context *ctx)
266 {
267         if (ctx->sock.registered)
268                 uloop_end();
269 }
270
271 static int _ubus_connect(struct ubus_context *ctx, const char *path)
272 {
273         ctx->sock.fd = -1;
274         ctx->sock.cb = ubus_handle_data;
275         ctx->connection_lost = ubus_default_connection_lost;
276         ctx->pending_timer.cb = ubus_process_pending_msg;
277
278         INIT_LIST_HEAD(&ctx->requests);
279         INIT_LIST_HEAD(&ctx->pending);
280         avl_init(&ctx->objects, ubus_cmp_id, false, NULL);
281         if (ubus_reconnect(ctx, path))
282                 return -1;
283
284         return 0;
285 }
286
287 static void ubus_auto_reconnect_cb(struct uloop_timeout *timeout)
288 {
289         struct ubus_auto_conn *conn = container_of(timeout, struct ubus_auto_conn, timer);
290
291         if (!ubus_reconnect(&conn->ctx, conn->path))
292                 ubus_add_uloop(&conn->ctx);
293         else
294                 uloop_timeout_set(timeout, 1000);
295 }
296
297 static void ubus_auto_disconnect_cb(struct ubus_context *ctx)
298 {
299         struct ubus_auto_conn *conn = container_of(ctx, struct ubus_auto_conn, ctx);
300
301         conn->timer.cb = ubus_auto_reconnect_cb;
302         uloop_timeout_set(&conn->timer, 1000);
303 }
304
305 static void ubus_auto_connect_cb(struct uloop_timeout *timeout)
306 {
307         struct ubus_auto_conn *conn = container_of(timeout, struct ubus_auto_conn, timer);
308
309         if (_ubus_connect(&conn->ctx, conn->path)) {
310                 uloop_timeout_set(timeout, 1000);
311                 fprintf(stderr, "failed to connect to ubus\n");
312                 return;
313         }
314         conn->ctx.connection_lost = ubus_auto_disconnect_cb;
315         if (conn->cb)
316                 conn->cb(&conn->ctx);
317         ubus_add_uloop(&conn->ctx);
318 }
319
320 void ubus_auto_connect(struct ubus_auto_conn *conn)
321 {
322         conn->timer.cb = ubus_auto_connect_cb;
323         ubus_auto_connect_cb(&conn->timer);
324 }
325
326 struct ubus_context *ubus_connect(const char *path)
327 {
328         struct ubus_context *ctx;
329
330         ctx = calloc(1, sizeof(*ctx));
331         if (!ctx)
332                 return NULL;
333
334         if (_ubus_connect(ctx, path)) {
335                 free(ctx);
336                 ctx = NULL;
337         }
338
339         return ctx;
340 }
341
342 void ubus_free(struct ubus_context *ctx)
343 {
344         blob_buf_free(&b);
345         close(ctx->sock.fd);
346         free(ctx);
347 }