libubus: use list_empty/list_first_entry in ubus_process_pending_msg
[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_buf 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_buf *buf)
75 {
76 struct ubus_pending_msg *pending;
77 void *data;
78
79 pending = calloc_a(sizeof(*pending), &data, blob_raw_len(buf->data));
80
81 pending->hdr.data = data;
82 memcpy(&pending->hdr.hdr, &buf->hdr, sizeof(buf->hdr));
83 memcpy(data, buf->data, blob_raw_len(buf->data));
84 list_add_tail(&pending->list, &ctx->pending);
85 if (ctx->sock.registered)
86 uloop_timeout_set(&ctx->pending_timer, 1);
87 }
88
89 void __hidden
90 ubus_process_msg(struct ubus_context *ctx, struct ubus_msghdr_buf *buf, int fd)
91 {
92 switch(buf->hdr.type) {
93 case UBUS_MSG_STATUS:
94 case UBUS_MSG_DATA:
95 ubus_process_req_msg(ctx, buf, 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, buf);
103 break;
104 }
105
106 ubus_process_obj_msg(ctx, buf, fd);
107 break;
108 case UBUS_MSG_MONITOR:
109 if (ctx->monitor_cb)
110 ctx->monitor_cb(ctx, buf->hdr.seq, buf->data);
111 break;
112 }
113 }
114
115 static void ubus_process_pending_msg(struct uloop_timeout *timeout)
116 {
117 struct ubus_context *ctx = container_of(timeout, struct ubus_context, pending_timer);
118 struct ubus_pending_msg *pending;
119
120 while (!list_empty(&ctx->pending)) {
121 if (ctx->stack_depth)
122 break;
123
124 pending = list_first_entry(&ctx->pending, struct ubus_pending_msg, list);
125 list_del(&pending->list);
126 ubus_process_msg(ctx, &pending->hdr, -1);
127 free(pending);
128 }
129 }
130
131 struct ubus_lookup_request {
132 struct ubus_request req;
133 ubus_lookup_handler_t cb;
134 };
135
136 static void ubus_lookup_cb(struct ubus_request *ureq, int type, struct blob_attr *msg)
137 {
138 struct ubus_lookup_request *req;
139 struct ubus_object_data obj = {};
140 struct blob_attr **attr;
141
142 req = container_of(ureq, struct ubus_lookup_request, req);
143 attr = ubus_parse_msg(msg, blob_raw_len(msg));
144
145 if (!attr[UBUS_ATTR_OBJID] || !attr[UBUS_ATTR_OBJPATH] ||
146 !attr[UBUS_ATTR_OBJTYPE])
147 return;
148
149 obj.id = blob_get_u32(attr[UBUS_ATTR_OBJID]);
150 obj.path = blob_data(attr[UBUS_ATTR_OBJPATH]);
151 obj.type_id = blob_get_u32(attr[UBUS_ATTR_OBJTYPE]);
152 obj.signature = attr[UBUS_ATTR_SIGNATURE];
153 req->cb(ureq->ctx, &obj, ureq->priv);
154 }
155
156 int ubus_lookup(struct ubus_context *ctx, const char *path,
157 ubus_lookup_handler_t cb, void *priv)
158 {
159 struct ubus_lookup_request lookup;
160
161 blob_buf_init(&b, 0);
162 if (path)
163 blob_put_string(&b, UBUS_ATTR_OBJPATH, path);
164
165 if (ubus_start_request(ctx, &lookup.req, b.head, UBUS_MSG_LOOKUP, 0) < 0)
166 return UBUS_STATUS_INVALID_ARGUMENT;
167
168 lookup.req.raw_data_cb = ubus_lookup_cb;
169 lookup.req.priv = priv;
170 lookup.cb = cb;
171 return ubus_complete_request(ctx, &lookup.req, 0);
172 }
173
174 static void ubus_lookup_id_cb(struct ubus_request *req, int type, struct blob_attr *msg)
175 {
176 struct blob_attr **attr;
177 uint32_t *id = req->priv;
178
179 attr = ubus_parse_msg(msg, blob_raw_len(msg));
180
181 if (!attr[UBUS_ATTR_OBJID])
182 return;
183
184 *id = blob_get_u32(attr[UBUS_ATTR_OBJID]);
185 }
186
187 int ubus_lookup_id(struct ubus_context *ctx, const char *path, uint32_t *id)
188 {
189 struct ubus_request req;
190
191 blob_buf_init(&b, 0);
192 if (path)
193 blob_put_string(&b, UBUS_ATTR_OBJPATH, path);
194
195 if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_LOOKUP, 0) < 0)
196 return UBUS_STATUS_INVALID_ARGUMENT;
197
198 req.raw_data_cb = ubus_lookup_id_cb;
199 req.priv = id;
200
201 return ubus_complete_request(ctx, &req, 0);
202 }
203
204 static int ubus_event_cb(struct ubus_context *ctx, struct ubus_object *obj,
205 struct ubus_request_data *req,
206 const char *method, struct blob_attr *msg)
207 {
208 struct ubus_event_handler *ev;
209
210 ev = container_of(obj, struct ubus_event_handler, obj);
211 ev->cb(ctx, ev, method, msg);
212 return 0;
213 }
214
215 static const struct ubus_method event_method = {
216 .name = NULL,
217 .handler = ubus_event_cb,
218 };
219
220 int ubus_register_event_handler(struct ubus_context *ctx,
221 struct ubus_event_handler *ev,
222 const char *pattern)
223 {
224 struct ubus_object *obj = &ev->obj;
225 struct blob_buf b2 = {};
226 int ret;
227
228 if (!obj->id) {
229 obj->methods = &event_method;
230 obj->n_methods = 1;
231
232 if (!!obj->name ^ !!obj->type)
233 return UBUS_STATUS_INVALID_ARGUMENT;
234
235 ret = ubus_add_object(ctx, obj);
236 if (ret)
237 return ret;
238 }
239
240 /* use a second buffer, ubus_invoke() overwrites the primary one */
241 blob_buf_init(&b2, 0);
242 blobmsg_add_u32(&b2, "object", obj->id);
243 if (pattern)
244 blobmsg_add_string(&b2, "pattern", pattern);
245
246 ret = ubus_invoke(ctx, UBUS_SYSTEM_OBJECT_EVENT, "register", b2.head,
247 NULL, NULL, 0);
248 blob_buf_free(&b2);
249
250 return ret;
251 }
252
253 int ubus_send_event(struct ubus_context *ctx, const char *id,
254 struct blob_attr *data)
255 {
256 struct ubus_request req;
257 void *s;
258
259 blob_buf_init(&b, 0);
260 blob_put_int32(&b, UBUS_ATTR_OBJID, UBUS_SYSTEM_OBJECT_EVENT);
261 blob_put_string(&b, UBUS_ATTR_METHOD, "send");
262 s = blob_nest_start(&b, UBUS_ATTR_DATA);
263 blobmsg_add_string(&b, "id", id);
264 blobmsg_add_field(&b, BLOBMSG_TYPE_TABLE, "data", blob_data(data), blob_len(data));
265 blob_nest_end(&b, s);
266
267 if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_INVOKE, UBUS_SYSTEM_OBJECT_EVENT) < 0)
268 return UBUS_STATUS_INVALID_ARGUMENT;
269
270 return ubus_complete_request(ctx, &req, 0);
271 }
272
273 static void ubus_default_connection_lost(struct ubus_context *ctx)
274 {
275 if (ctx->sock.registered)
276 uloop_end();
277 }
278
279 int ubus_connect_ctx(struct ubus_context *ctx, const char *path)
280 {
281 uloop_init();
282 memset(ctx, 0, sizeof(*ctx));
283
284 ctx->sock.fd = -1;
285 ctx->sock.cb = ubus_handle_data;
286 ctx->connection_lost = ubus_default_connection_lost;
287 ctx->pending_timer.cb = ubus_process_pending_msg;
288
289 ctx->msgbuf.data = calloc(1, UBUS_MSG_CHUNK_SIZE);
290 if (!ctx->msgbuf.data)
291 return -1;
292 ctx->msgbuf_data_len = UBUS_MSG_CHUNK_SIZE;
293
294 INIT_LIST_HEAD(&ctx->requests);
295 INIT_LIST_HEAD(&ctx->pending);
296 avl_init(&ctx->objects, ubus_cmp_id, false, NULL);
297 if (ubus_reconnect(ctx, path)) {
298 free(ctx->msgbuf.data);
299 ctx->msgbuf.data = NULL;
300 return -1;
301 }
302
303 return 0;
304 }
305
306 static void ubus_auto_reconnect_cb(struct uloop_timeout *timeout)
307 {
308 struct ubus_auto_conn *conn = container_of(timeout, struct ubus_auto_conn, timer);
309
310 if (!ubus_reconnect(&conn->ctx, conn->path))
311 ubus_add_uloop(&conn->ctx);
312 else
313 uloop_timeout_set(timeout, 1000);
314 }
315
316 static void ubus_auto_disconnect_cb(struct ubus_context *ctx)
317 {
318 struct ubus_auto_conn *conn = container_of(ctx, struct ubus_auto_conn, ctx);
319
320 conn->timer.cb = ubus_auto_reconnect_cb;
321 uloop_timeout_set(&conn->timer, 1000);
322 }
323
324 static void ubus_auto_connect_cb(struct uloop_timeout *timeout)
325 {
326 struct ubus_auto_conn *conn = container_of(timeout, struct ubus_auto_conn, timer);
327
328 if (ubus_connect_ctx(&conn->ctx, conn->path)) {
329 uloop_timeout_set(timeout, 1000);
330 fprintf(stderr, "failed to connect to ubus\n");
331 return;
332 }
333 conn->ctx.connection_lost = ubus_auto_disconnect_cb;
334 if (conn->cb)
335 conn->cb(&conn->ctx);
336 ubus_add_uloop(&conn->ctx);
337 }
338
339 void ubus_auto_connect(struct ubus_auto_conn *conn)
340 {
341 conn->timer.cb = ubus_auto_connect_cb;
342 ubus_auto_connect_cb(&conn->timer);
343 }
344
345 struct ubus_context *ubus_connect(const char *path)
346 {
347 struct ubus_context *ctx;
348
349 ctx = calloc(1, sizeof(*ctx));
350 if (!ctx)
351 return NULL;
352
353 if (ubus_connect_ctx(ctx, path)) {
354 free(ctx);
355 ctx = NULL;
356 }
357
358 return ctx;
359 }
360
361 void ubus_shutdown(struct ubus_context *ctx)
362 {
363 blob_buf_free(&b);
364 if (!ctx)
365 return;
366 close(ctx->sock.fd);
367 uloop_timeout_cancel(&ctx->pending_timer);
368 free(ctx->msgbuf.data);
369 }
370
371 void ubus_free(struct ubus_context *ctx)
372 {
373 ubus_shutdown(ctx);
374 free(ctx);
375 }