ubusd: remove systemd socket activation support
[project/ubus.git] / libubus-req.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 <unistd.h>
15 #include "libubus.h"
16 #include "libubus-internal.h"
17
18 struct ubus_pending_data {
19         struct list_head list;
20         int type;
21         struct blob_attr data[];
22 };
23
24 static void req_data_cb(struct ubus_request *req, int type, struct blob_attr *data)
25 {
26         struct blob_attr **attr;
27
28         if (req->raw_data_cb)
29                 req->raw_data_cb(req, type, data);
30
31         if (!req->data_cb)
32                 return;
33
34         attr = ubus_parse_msg(data);
35         req->data_cb(req, type, attr[UBUS_ATTR_DATA]);
36 }
37
38 static void __ubus_process_req_data(struct ubus_request *req)
39 {
40         struct ubus_pending_data *data;
41
42         while (!list_empty(&req->pending)) {
43                 data = list_first_entry(&req->pending,
44                         struct ubus_pending_data, list);
45                 list_del(&data->list);
46                 if (!req->cancelled)
47                         req_data_cb(req, data->type, data->data);
48                 free(data);
49         }
50 }
51
52 int __hidden ubus_start_request(struct ubus_context *ctx, struct ubus_request *req,
53                                 struct blob_attr *msg, int cmd, uint32_t peer)
54 {
55         memset(req, 0, sizeof(*req));
56
57         if (msg && blob_pad_len(msg) > UBUS_MAX_MSGLEN)
58                 return -1;
59
60         INIT_LIST_HEAD(&req->list);
61         INIT_LIST_HEAD(&req->pending);
62         req->ctx = ctx;
63         req->peer = peer;
64         req->seq = ++ctx->request_seq;
65         return ubus_send_msg(ctx, req->seq, msg, cmd, peer, -1);
66 }
67
68 void ubus_abort_request(struct ubus_context *ctx, struct ubus_request *req)
69 {
70         if (list_empty(&req->list))
71                 return;
72
73         req->cancelled = true;
74         __ubus_process_req_data(req);
75         list_del_init(&req->list);
76 }
77
78 void ubus_complete_request_async(struct ubus_context *ctx, struct ubus_request *req)
79 {
80         if (!list_empty(&req->list))
81                 return;
82
83         list_add(&req->list, &ctx->requests);
84 }
85
86 static void
87 ubus_req_complete_cb(struct ubus_request *req)
88 {
89         ubus_complete_handler_t cb = req->complete_cb;
90
91         if (!cb)
92                 return;
93
94         req->complete_cb = NULL;
95         cb(req, req->status_code);
96 }
97
98 static void
99 ubus_set_req_status(struct ubus_request *req, int ret)
100 {
101         if (!list_empty(&req->list))
102                 list_del_init(&req->list);
103
104         req->status_msg = true;
105         req->status_code = ret;
106         if (!req->blocked)
107                 ubus_req_complete_cb(req);
108 }
109
110 static void ubus_sync_req_cb(struct ubus_request *req, int ret)
111 {
112         req->status_msg = true;
113         req->status_code = ret;
114         uloop_end();
115 }
116
117 static int64_t get_time_msec(void)
118 {
119         struct timespec ts;
120         int64_t val;
121
122         clock_gettime(CLOCK_MONOTONIC, &ts);
123         val = (int64_t) ts.tv_sec * 1000LL;
124         val += ts.tv_nsec / 1000000LL;
125         return val;
126 }
127
128 int ubus_complete_request(struct ubus_context *ctx, struct ubus_request *req,
129                           int req_timeout)
130 {
131         ubus_complete_handler_t complete_cb = req->complete_cb;
132         bool registered = ctx->sock.registered;
133         int status = UBUS_STATUS_NO_DATA;
134         int64_t timeout = 0, time_end = 0;
135
136         if (!registered) {
137                 uloop_init();
138                 ubus_add_uloop(ctx);
139         }
140
141         if (req_timeout)
142                 time_end = get_time_msec() + req_timeout;
143
144         ubus_complete_request_async(ctx, req);
145         req->complete_cb = ubus_sync_req_cb;
146
147         ctx->stack_depth++;
148         while (!req->status_msg) {
149                 bool cancelled = uloop_cancelled;
150
151                 uloop_cancelled = false;
152                 if (req_timeout) {
153                         timeout = time_end - get_time_msec();
154                         if (timeout <= 0) {
155                                 ubus_set_req_status(req, UBUS_STATUS_TIMEOUT);
156                                 uloop_cancelled = cancelled;
157                                 break;
158                         }
159                 }
160                 ubus_poll_data(ctx, (unsigned int) timeout);
161
162                 uloop_cancelled = cancelled;
163                 if (ctx->sock.eof) {
164                         ubus_set_req_status(req, UBUS_STATUS_CONNECTION_FAILED);
165                         break;
166                 }
167         }
168         ctx->stack_depth--;
169         if (ctx->stack_depth)
170                 uloop_cancelled = true;
171
172         if (req->status_msg)
173                 status = req->status_code;
174
175         req->complete_cb = complete_cb;
176         if (req->complete_cb)
177                 req->complete_cb(req, status);
178
179         if (!registered) {
180                 uloop_fd_delete(&ctx->sock);
181
182                 if (!ctx->stack_depth)
183                         ctx->pending_timer.cb(&ctx->pending_timer);
184         }
185
186         return status;
187 }
188
189 void ubus_complete_deferred_request(struct ubus_context *ctx, struct ubus_request_data *req, int ret)
190 {
191         blob_buf_init(&b, 0);
192         blob_put_int32(&b, UBUS_ATTR_STATUS, ret);
193         blob_put_int32(&b, UBUS_ATTR_OBJID, req->object);
194         ubus_send_msg(ctx, req->seq, b.head, UBUS_MSG_STATUS, req->peer, req->fd);
195 }
196
197 int ubus_send_reply(struct ubus_context *ctx, struct ubus_request_data *req,
198                     struct blob_attr *msg)
199 {
200         int ret;
201
202         blob_buf_init(&b, 0);
203         blob_put_int32(&b, UBUS_ATTR_OBJID, req->object);
204         blob_put(&b, UBUS_ATTR_DATA, blob_data(msg), blob_len(msg));
205         ret = ubus_send_msg(ctx, req->seq, b.head, UBUS_MSG_DATA, req->peer, -1);
206         if (ret < 0)
207                 return UBUS_STATUS_NO_DATA;
208
209         return 0;
210 }
211
212 int ubus_invoke_async(struct ubus_context *ctx, uint32_t obj, const char *method,
213                        struct blob_attr *msg, struct ubus_request *req)
214 {
215         blob_buf_init(&b, 0);
216         blob_put_int32(&b, UBUS_ATTR_OBJID, obj);
217         blob_put_string(&b, UBUS_ATTR_METHOD, method);
218         if (msg)
219                 blob_put(&b, UBUS_ATTR_DATA, blob_data(msg), blob_len(msg));
220
221         if (ubus_start_request(ctx, req, b.head, UBUS_MSG_INVOKE, obj) < 0)
222                 return UBUS_STATUS_INVALID_ARGUMENT;
223
224         return 0;
225 }
226
227 int ubus_invoke(struct ubus_context *ctx, uint32_t obj, const char *method,
228                 struct blob_attr *msg, ubus_data_handler_t cb, void *priv,
229                 int timeout)
230 {
231         struct ubus_request req;
232         int rc;
233
234         rc = ubus_invoke_async(ctx, obj, method, msg, &req);
235         if (rc)
236                 return rc;
237
238         req.data_cb = cb;
239         req.priv = priv;
240         return ubus_complete_request(ctx, &req, timeout);
241 }
242
243 static void
244 ubus_notify_complete_cb(struct ubus_request *req, int ret)
245 {
246         struct ubus_notify_request *nreq;
247
248         nreq = container_of(req, struct ubus_notify_request, req);
249         if (!nreq->complete_cb)
250                 return;
251
252         nreq->complete_cb(nreq, 0, 0);
253 }
254
255 static int
256 __ubus_notify_async(struct ubus_context *ctx, struct ubus_object *obj,
257                     const char *type, struct blob_attr *msg,
258                     struct ubus_notify_request *req, bool reply)
259 {
260         memset(req, 0, sizeof(*req));
261
262         blob_buf_init(&b, 0);
263         blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id);
264         blob_put_string(&b, UBUS_ATTR_METHOD, type);
265
266         if (!reply)
267                 blob_put_int8(&b, UBUS_ATTR_NO_REPLY, true);
268
269         if (msg)
270                 blob_put(&b, UBUS_ATTR_DATA, blob_data(msg), blob_len(msg));
271
272         if (ubus_start_request(ctx, &req->req, b.head, UBUS_MSG_NOTIFY, obj->id) < 0)
273                 return UBUS_STATUS_INVALID_ARGUMENT;
274
275         /* wait for status message from ubusd first */
276         req->req.notify = true;
277         req->pending = 1;
278         req->id[0] = obj->id;
279         req->req.complete_cb = ubus_notify_complete_cb;
280
281         return 0;
282 }
283
284 int ubus_notify_async(struct ubus_context *ctx, struct ubus_object *obj,
285                       const char *type, struct blob_attr *msg,
286                       struct ubus_notify_request *req)
287 {
288         return __ubus_notify_async(ctx, obj, type, msg, req, true);
289 }
290
291 int ubus_notify(struct ubus_context *ctx, struct ubus_object *obj,
292                 const char *type, struct blob_attr *msg, int timeout)
293 {
294         struct ubus_notify_request req;
295         int ret;
296
297         ret = __ubus_notify_async(ctx, obj, type, msg, &req, timeout >= 0);
298         if (ret < 0)
299                 return ret;
300
301         if (timeout < 0) {
302                 ubus_abort_request(ctx, &req.req);
303                 return 0;
304         }
305
306         return ubus_complete_request(ctx, &req.req, timeout);
307 }
308
309 static bool ubus_get_status(struct ubus_msghdr_buf *buf, int *ret)
310 {
311         struct blob_attr **attrbuf = ubus_parse_msg(buf->data);
312
313         if (!attrbuf[UBUS_ATTR_STATUS])
314                 return false;
315
316         *ret = blob_get_u32(attrbuf[UBUS_ATTR_STATUS]);
317         return true;
318 }
319
320 static int
321 ubus_process_req_status(struct ubus_request *req, struct ubus_msghdr_buf *buf)
322 {
323         int ret = UBUS_STATUS_INVALID_ARGUMENT;
324
325         ubus_get_status(buf, &ret);
326         req->peer = buf->hdr.peer;
327         ubus_set_req_status(req, ret);
328
329         return ret;
330 }
331
332 static void
333 ubus_process_req_data(struct ubus_request *req, struct ubus_msghdr_buf *buf)
334 {
335         struct ubus_pending_data *data;
336         int len;
337
338         if (!req->blocked) {
339                 req->blocked = true;
340                 req_data_cb(req, buf->hdr.type, buf->data);
341                 __ubus_process_req_data(req);
342                 req->blocked = false;
343
344                 if (req->status_msg)
345                         ubus_req_complete_cb(req);
346
347                 return;
348         }
349
350         len = blob_raw_len(buf->data);
351         data = calloc(1, sizeof(*data) + len);
352         if (!data)
353                 return;
354
355         data->type = buf->hdr.type;
356         memcpy(data->data, buf->data, len);
357         list_add(&data->list, &req->pending);
358 }
359
360 static int
361 ubus_find_notify_id(struct ubus_notify_request *n, uint32_t objid)
362 {
363         uint32_t pending = n->pending;
364         int i;
365
366         for (i = 0; pending; i++, pending >>= 1) {
367                 if (!(pending & 1))
368                         continue;
369
370                 if (n->id[i] == objid)
371                         return i;
372         }
373
374         return -1;
375 }
376
377 static struct ubus_request *
378 ubus_find_request(struct ubus_context *ctx, uint32_t seq, uint32_t peer, int *id)
379 {
380         struct ubus_request *req;
381
382         list_for_each_entry(req, &ctx->requests, list) {
383                 struct ubus_notify_request *nreq;
384                 nreq = container_of(req, struct ubus_notify_request, req);
385
386                 if (seq != req->seq)
387                         continue;
388
389                 if (req->notify) {
390                         if (!nreq->pending)
391                                 continue;
392
393                         *id = ubus_find_notify_id(nreq, peer);
394                         if (*id < 0)
395                                 continue;
396                 } else if (peer != req->peer)
397                         continue;
398
399                 return req;
400         }
401         return NULL;
402 }
403
404 static void ubus_process_notify_status(struct ubus_request *req, int id, struct ubus_msghdr_buf *buf)
405 {
406         struct ubus_notify_request *nreq;
407         struct blob_attr **tb;
408         struct blob_attr *cur;
409         int rem, idx = 1;
410         int ret = 0;
411
412         nreq = container_of(req, struct ubus_notify_request, req);
413         nreq->pending &= ~(1 << id);
414
415         if (!id) {
416                 /* first id: ubusd's status message with a list of ids */
417                 tb = ubus_parse_msg(buf->data);
418                 if (tb[UBUS_ATTR_SUBSCRIBERS]) {
419                         blob_for_each_attr(cur, tb[UBUS_ATTR_SUBSCRIBERS], rem) {
420                                 if (!blob_check_type(blob_data(cur), blob_len(cur), BLOB_ATTR_INT32))
421                                         continue;
422
423                                 nreq->pending |= (1 << idx);
424                                 nreq->id[idx] = blob_get_int32(cur);
425                                 idx++;
426
427                                 if (idx == UBUS_MAX_NOTIFY_PEERS + 1)
428                                         break;
429                         }
430                 }
431         } else {
432                 ubus_get_status(buf, &ret);
433                 if (nreq->status_cb)
434                         nreq->status_cb(nreq, id, ret);
435         }
436
437         if (!nreq->pending)
438                 ubus_set_req_status(req, 0);
439 }
440
441 void __hidden ubus_process_req_msg(struct ubus_context *ctx, struct ubus_msghdr_buf *buf, int fd)
442 {
443         struct ubus_msghdr *hdr = &buf->hdr;
444         struct ubus_request *req;
445         int id = -1;
446
447         switch(hdr->type) {
448         case UBUS_MSG_STATUS:
449                 req = ubus_find_request(ctx, hdr->seq, hdr->peer, &id);
450                 if (!req)
451                         break;
452
453                 if (fd >= 0) {
454                         if (req->fd_cb)
455                                 req->fd_cb(req, fd);
456                         else
457                                 close(fd);
458                 }
459
460                 if (id >= 0)
461                         ubus_process_notify_status(req, id, buf);
462                 else
463                         ubus_process_req_status(req, buf);
464                 break;
465
466         case UBUS_MSG_DATA:
467                 req = ubus_find_request(ctx, hdr->seq, hdr->peer, &id);
468                 if (req && (req->data_cb || req->raw_data_cb))
469                         ubus_process_req_data(req, buf);
470                 break;
471         }
472 }
473
474 int __ubus_monitor(struct ubus_context *ctx, const char *type)
475 {
476         blob_buf_init(&b, 0);
477         return ubus_invoke(ctx, UBUS_SYSTEM_OBJECT_MONITOR, type, b.head, NULL, NULL, 1000);
478 }