Localize construction of afb_arg
[src/app-framework-binder.git] / src / afb-xreq.c
1 /*
2  * Copyright (C) 2017 "IoT.bzh"
3  * Author José Bollo <jose.bollo@iot.bzh>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #define _GNU_SOURCE
19 #define AFB_BINDING_PRAGMA_NO_VERBOSE_MACRO
20
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <errno.h>
25
26 #include <json-c/json.h>
27 #include <afb/afb-binding-v1.h>
28 #include <afb/afb-binding-v2.h>
29
30 #include "afb-context.h"
31 #include "afb-xreq.h"
32 #include "afb-evt.h"
33 #include "afb-msg-json.h"
34 #include "afb-subcall.h"
35 #include "afb-hook.h"
36 #include "afb-api.h"
37 #include "afb-apiset.h"
38 #include "afb-auth.h"
39 #include "jobs.h"
40 #include "verbose.h"
41
42 /******************************************************************************/
43
44 static void vinfo(void *first, void *second, const char *fmt, va_list args, void (*fun)(void*,void*,const char*))
45 {
46         char *info;
47         if (fmt == NULL || vasprintf(&info, fmt, args) < 0)
48                 info = NULL;
49         fun(first, second, info);
50         free(info);
51 }
52
53 /******************************************************************************/
54
55 static struct json_object *xreq_json_cb(void *closure)
56 {
57         struct afb_xreq *xreq = closure;
58         if (!xreq->json && xreq->queryitf->json)
59                 xreq->json = xreq->queryitf->json(xreq);
60         return xreq->json;
61 }
62
63 static struct afb_arg xreq_get_cb(void *closure, const char *name)
64 {
65         struct afb_xreq *xreq = closure;
66         struct afb_arg arg;
67         struct json_object *object, *value;
68
69         if (xreq->queryitf->get)
70                 arg = xreq->queryitf->get(xreq, name);
71         else {
72                 object = xreq_json_cb(closure);
73                 if (json_object_object_get_ex(object, name, &value)) {
74                         arg.name = name;
75                         arg.value = json_object_get_string(value);
76                 } else {
77                         arg.name = NULL;
78                         arg.value = NULL;
79                 }
80                 arg.path = NULL;
81         }
82         return arg;
83 }
84
85 static void xreq_success_cb(void *closure, struct json_object *obj, const char *info)
86 {
87         struct afb_xreq *xreq = closure;
88
89         if (xreq->replied) {
90                 ERROR("reply called more than one time!!");
91                 json_object_put(obj);
92         } else {
93                 xreq->replied = 1;
94                 if (xreq->queryitf->success)
95                         xreq->queryitf->success(xreq, obj, info);
96                 else
97                         xreq->queryitf->reply(xreq, 0, afb_msg_json_reply_ok(info, obj, &xreq->context, NULL));
98         }
99 }
100
101 static void xreq_fail_cb(void *closure, const char *status, const char *info)
102 {
103         struct afb_xreq *xreq = closure;
104
105         if (xreq->replied) {
106                 ERROR("reply called more than one time!!");
107         } else {
108                 xreq->replied = 1;
109                 if (xreq->queryitf->fail)
110                         xreq->queryitf->fail(xreq, status, info);
111                 else
112                         xreq->queryitf->reply(xreq, 1, afb_msg_json_reply_error(status, info, &xreq->context, NULL));
113         }
114 }
115
116 static void xreq_vsuccess_cb(void *closure, struct json_object *obj, const char *fmt, va_list args)
117 {
118         vinfo(closure, obj, fmt, args, (void*)xreq_success_cb);
119 }
120
121 static void xreq_vfail_cb(void *closure, const char *status, const char *fmt, va_list args)
122 {
123         vinfo(closure, (void*)status, fmt, args, (void*)xreq_fail_cb);
124 }
125
126 static void *xreq_context_get_cb(void *closure)
127 {
128         struct afb_xreq *xreq = closure;
129         return afb_context_get(&xreq->context);
130 }
131
132 static void xreq_context_set_cb(void *closure, void *value, void (*free_value)(void*))
133 {
134         struct afb_xreq *xreq = closure;
135         afb_context_set(&xreq->context, value, free_value);
136 }
137
138 static void xreq_addref_cb(void *closure)
139 {
140         struct afb_xreq *xreq = closure;
141         __atomic_add_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED);
142 }
143
144 static void xreq_unref_cb(void *closure)
145 {
146         struct afb_xreq *xreq = closure;
147         if (!__atomic_sub_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED)) {
148                 xreq->queryitf->unref(xreq);
149         }
150 }
151
152 static void xreq_session_close_cb(void *closure)
153 {
154         struct afb_xreq *xreq = closure;
155         afb_context_close(&xreq->context);
156 }
157
158 static int xreq_session_set_LOA_cb(void *closure, unsigned level)
159 {
160         struct afb_xreq *xreq = closure;
161         return afb_context_change_loa(&xreq->context, level);
162 }
163
164 static int xreq_subscribe_cb(void *closure, struct afb_event event)
165 {
166         struct afb_xreq *xreq = closure;
167         return afb_xreq_subscribe(xreq, event);
168 }
169
170 int afb_xreq_subscribe(struct afb_xreq *xreq, struct afb_event event)
171 {
172         if (xreq->listener)
173                 return afb_evt_add_watch(xreq->listener, event);
174         if (xreq->queryitf->subscribe)
175                 return xreq->queryitf->subscribe(xreq, event);
176         ERROR("no event listener, subscription impossible");
177         errno = EINVAL;
178         return -1;
179 }
180
181 static int xreq_unsubscribe_cb(void *closure, struct afb_event event)
182 {
183         struct afb_xreq *xreq = closure;
184         return afb_xreq_unsubscribe(xreq, event);
185 }
186
187 int afb_xreq_unsubscribe(struct afb_xreq *xreq, struct afb_event event)
188 {
189         if (xreq->listener)
190                 return afb_evt_remove_watch(xreq->listener, event);
191         if (xreq->queryitf->unsubscribe)
192                 return xreq->queryitf->unsubscribe(xreq, event);
193         ERROR("no event listener, unsubscription impossible");
194         errno = EINVAL;
195         return -1;
196 }
197
198 static void xreq_subcall_cb(void *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cb_closure)
199 {
200         struct afb_xreq *xreq = closure;
201
202         if (xreq->queryitf->subcall) {
203                 xreq->queryitf->subcall(xreq, api, verb, args, callback, cb_closure);
204                 json_object_put(args);
205         } else {
206                 afb_subcall(xreq, api, verb, args, callback, cb_closure);
207         }
208 }
209
210 struct xreq_sync
211 {
212         struct afb_xreq *caller;
213         const char *api;
214         const char *verb;
215         struct json_object *args;
216         struct jobloop *jobloop;
217         struct json_object *result;
218         int iserror;
219 };
220
221 static void xreq_sync_leave(struct xreq_sync *sync)
222 {
223         struct jobloop *jobloop = sync->jobloop;
224         if (jobloop) {
225                 sync->jobloop = NULL;
226                 jobs_leave(jobloop);
227         }
228 }
229
230 static void xreq_sync_reply(void *closure, int iserror, struct json_object *obj)
231 {
232         struct xreq_sync *sync = closure;
233
234         sync->iserror = iserror;
235         sync->result = json_object_get(obj);
236         xreq_sync_leave(sync);
237 }
238
239 static void xreq_sync_enter(int signum, void *closure, struct jobloop *jobloop)
240 {
241         struct xreq_sync *sync = closure;
242
243         if (!signum) {
244                 sync->jobloop = jobloop;
245                 xreq_subcall_cb(sync->caller, sync->api, sync->verb, json_object_get(sync->args), xreq_sync_reply, sync);
246         } else {
247                 sync->iserror = 1;
248                 xreq_sync_leave(sync);
249         }
250 }
251
252 static int xreq_subcallsync_cb(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
253 {
254         int rc;
255         struct xreq_sync sync;
256         struct afb_xreq *xreq = closure;
257
258         sync.caller = xreq;
259         sync.api = api;
260         sync.verb = verb;
261         sync.args = args;
262         sync.jobloop = NULL;
263         sync.result = NULL;
264         sync.iserror = 1;
265
266         rc = jobs_enter(NULL, 0, xreq_sync_enter, &sync);
267         json_object_put(args);
268         if (rc < 0 || sync.iserror) {
269                 *result = sync.result ? : afb_msg_json_internal_error();
270                 return 0;
271         }
272         *result = sync.result;
273         return 1;
274 }
275
276 static void xreq_vverbose_cb(void*closure, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
277 {
278         /* TODO: improves the implementation. example: on condition make a list of log messages that will be returned */
279         vverbose(level, file, line, func, fmt, args);
280 }
281
282 /******************************************************************************/
283
284 static struct json_object *xreq_hooked_json_cb(void *closure)
285 {
286         struct json_object *r = xreq_json_cb(closure);
287         struct afb_xreq *xreq = closure;
288         return afb_hook_xreq_json(xreq, r);
289 }
290
291 static struct afb_arg xreq_hooked_get_cb(void *closure, const char *name)
292 {
293         struct afb_arg r = xreq_get_cb(closure, name);
294         struct afb_xreq *xreq = closure;
295         return afb_hook_xreq_get(xreq, name, r);
296 }
297
298 static void xreq_hooked_success_cb(void *closure, struct json_object *obj, const char *info)
299 {
300         struct afb_xreq *xreq = closure;
301         afb_hook_xreq_success(xreq, obj, info);
302         xreq_success_cb(closure, obj, info);
303 }
304
305 static void xreq_hooked_fail_cb(void *closure, const char *status, const char *info)
306 {
307         struct afb_xreq *xreq = closure;
308         afb_hook_xreq_fail(xreq, status, info);
309         xreq_fail_cb(closure, status, info);
310 }
311
312 static void xreq_hooked_vsuccess_cb(void *closure, struct json_object *obj, const char *fmt, va_list args)
313 {
314         vinfo(closure, obj, fmt, args, (void*)xreq_hooked_success_cb);
315 }
316
317 static void xreq_hooked_vfail_cb(void *closure, const char *status, const char *fmt, va_list args)
318 {
319         vinfo(closure, (void*)status, fmt, args, (void*)xreq_hooked_fail_cb);
320 }
321
322 static void *xreq_hooked_context_get_cb(void *closure)
323 {
324         void *r = xreq_context_get_cb(closure);
325         struct afb_xreq *xreq = closure;
326         return afb_hook_xreq_context_get(xreq, r);
327 }
328
329 static void xreq_hooked_context_set_cb(void *closure, void *value, void (*free_value)(void*))
330 {
331         struct afb_xreq *xreq = closure;
332         afb_hook_xreq_context_set(xreq, value, free_value);
333         xreq_context_set_cb(closure, value, free_value);
334 }
335
336 static void xreq_hooked_addref_cb(void *closure)
337 {
338         struct afb_xreq *xreq = closure;
339         afb_hook_xreq_addref(xreq);
340         xreq_addref_cb(closure);
341 }
342
343 static void xreq_hooked_unref_cb(void *closure)
344 {
345         struct afb_xreq *xreq = closure;
346         afb_hook_xreq_unref(xreq);
347         if (!__atomic_sub_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED)) {
348                 afb_hook_xreq_end(xreq);
349                 xreq->queryitf->unref(xreq);
350         }
351 }
352
353 static void xreq_hooked_session_close_cb(void *closure)
354 {
355         struct afb_xreq *xreq = closure;
356         afb_hook_xreq_session_close(xreq);
357         xreq_session_close_cb(closure);
358 }
359
360 static int xreq_hooked_session_set_LOA_cb(void *closure, unsigned level)
361 {
362         int r = xreq_session_set_LOA_cb(closure, level);
363         struct afb_xreq *xreq = closure;
364         return afb_hook_xreq_session_set_LOA(xreq, level, r);
365 }
366
367 static int xreq_hooked_subscribe_cb(void *closure, struct afb_event event)
368 {
369         int r = xreq_subscribe_cb(closure, event);
370         struct afb_xreq *xreq = closure;
371         return afb_hook_xreq_subscribe(xreq, event, r);
372 }
373
374 static int xreq_hooked_unsubscribe_cb(void *closure, struct afb_event event)
375 {
376         int r = xreq_unsubscribe_cb(closure, event);
377         struct afb_xreq *xreq = closure;
378         return afb_hook_xreq_unsubscribe(xreq, event, r);
379 }
380
381 struct reply
382 {
383         struct afb_xreq *xreq;
384         void (*callback)(void*, int, struct json_object*);
385         void *closure;
386 };
387
388 static void xreq_hooked_subcall_reply_cb(void *closure, int iserror, struct json_object *result)
389 {
390         struct reply *reply = closure;
391         
392         afb_hook_xreq_subcall_result(reply->xreq, iserror, result);
393         reply->callback(reply->closure, iserror, result);
394         free(reply);
395 }
396
397 static void xreq_hooked_subcall_cb(void *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cb_closure)
398 {
399         struct reply *reply = malloc(sizeof *reply);
400         struct afb_xreq *xreq = closure;
401         afb_hook_xreq_subcall(xreq, api, verb, args);
402         if (reply) {
403                 reply->xreq = xreq;
404                 reply->callback = callback;
405                 reply->closure = cb_closure;
406                 xreq_subcall_cb(closure, api, verb, args, xreq_hooked_subcall_reply_cb, reply);
407         } else {
408                 ERROR("out of memory");
409                 xreq_subcall_cb(closure, api, verb, args, callback, cb_closure);
410         }
411 }
412
413 static int xreq_hooked_subcallsync_cb(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
414 {
415         int r;
416         struct afb_xreq *xreq = closure;
417         afb_hook_xreq_subcallsync(xreq, api, verb, args);
418         r = xreq_subcallsync_cb(closure, api, verb, args, result);
419         return afb_hook_xreq_subcallsync_result(xreq, r, *result);
420 }
421
422 static void xreq_hooked_vverbose_cb(void*closure, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
423 {
424         struct afb_xreq *xreq = closure;
425         va_list ap;
426         va_copy(ap, args);
427         xreq_vverbose_cb(closure, level, file, line, func, fmt, args);
428         afb_hook_xreq_vverbose(xreq, level, file, line, func, fmt, ap);
429         va_end(ap);
430 }
431
432 /******************************************************************************/
433
434 const struct afb_req_itf xreq_itf = {
435         .json = xreq_json_cb,
436         .get = xreq_get_cb,
437         .success = xreq_success_cb,
438         .fail = xreq_fail_cb,
439         .vsuccess = xreq_vsuccess_cb,
440         .vfail = xreq_vfail_cb,
441         .context_get = xreq_context_get_cb,
442         .context_set = xreq_context_set_cb,
443         .addref = xreq_addref_cb,
444         .unref = xreq_unref_cb,
445         .session_close = xreq_session_close_cb,
446         .session_set_LOA = xreq_session_set_LOA_cb,
447         .subscribe = xreq_subscribe_cb,
448         .unsubscribe = xreq_unsubscribe_cb,
449         .subcall = xreq_subcall_cb,
450         .subcallsync = xreq_subcallsync_cb,
451         .vverbose = xreq_vverbose_cb
452 };
453
454 const struct afb_req_itf xreq_hooked_itf = {
455         .json = xreq_hooked_json_cb,
456         .get = xreq_hooked_get_cb,
457         .success = xreq_hooked_success_cb,
458         .fail = xreq_hooked_fail_cb,
459         .vsuccess = xreq_hooked_vsuccess_cb,
460         .vfail = xreq_hooked_vfail_cb,
461         .context_get = xreq_hooked_context_get_cb,
462         .context_set = xreq_hooked_context_set_cb,
463         .addref = xreq_hooked_addref_cb,
464         .unref = xreq_hooked_unref_cb,
465         .session_close = xreq_hooked_session_close_cb,
466         .session_set_LOA = xreq_hooked_session_set_LOA_cb,
467         .subscribe = xreq_hooked_subscribe_cb,
468         .unsubscribe = xreq_hooked_unsubscribe_cb,
469         .subcall = xreq_hooked_subcall_cb,
470         .subcallsync = xreq_hooked_subcallsync_cb,
471         .vverbose = xreq_hooked_vverbose_cb
472 };
473
474 static inline struct afb_req to_req(struct afb_xreq *xreq)
475 {
476         return (struct afb_req){ .itf = xreq->hookflags ? &xreq_hooked_itf : &xreq_itf, .closure = xreq };
477 }
478
479 /******************************************************************************/
480
481 struct json_object *afb_xreq_json(struct afb_xreq *xreq)
482 {
483         return afb_req_json(to_req(xreq));
484 }
485
486 void afb_xreq_success(struct afb_xreq *xreq, struct json_object *obj, const char *info)
487 {
488         afb_req_success(to_req(xreq), obj, info);
489 }
490
491 void afb_xreq_success_f(struct afb_xreq *xreq, struct json_object *obj, const char *info, ...)
492 {
493         char *message;
494         va_list args;
495         va_start(args, info);
496         if (info == NULL || vasprintf(&message, info, args) < 0)
497                 message = NULL;
498         va_end(args);
499         afb_xreq_success(xreq, obj, message);
500         free(message);
501 }
502
503 void afb_xreq_fail(struct afb_xreq *xreq, const char *status, const char *info)
504 {
505         afb_req_fail(to_req(xreq), status, info);
506 }
507
508 void afb_xreq_fail_f(struct afb_xreq *xreq, const char *status, const char *info, ...)
509 {
510         char *message;
511         va_list args;
512         va_start(args, info);
513         if (info == NULL || vasprintf(&message, info, args) < 0)
514                 message = NULL;
515         va_end(args);
516         afb_xreq_fail(xreq, status, message);
517         free(message);
518 }
519
520 const char *afb_xreq_raw(struct afb_xreq *xreq, size_t *size)
521 {
522         struct json_object *obj = xreq_json_cb(xreq);
523         const char *result = json_object_to_json_string(obj);
524         if (size != NULL)
525                 *size = strlen(result);
526         return result;
527 }
528
529 void afb_xreq_addref(struct afb_xreq *xreq)
530 {
531         afb_req_addref(to_req(xreq));
532 }
533
534 void afb_xreq_unref(struct afb_xreq *xreq)
535 {
536         afb_req_unref(to_req(xreq));
537 }
538
539 void afb_xreq_unhooked_subcall(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cb_closure)
540 {
541         xreq_subcall_cb(xreq, api, verb, args, callback, cb_closure);
542 }
543
544 void afb_xreq_subcall(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cb_closure)
545 {
546         afb_req_subcall(to_req(xreq), api, verb, args, callback, cb_closure);
547 }
548
549 int afb_xreq_unhooked_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
550 {
551         return xreq_subcallsync_cb(xreq, api, verb, args, result);
552 }
553
554 int afb_xreq_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
555 {
556         return afb_req_subcall_sync(to_req(xreq), api, verb, args, result);
557 }
558
559 static int xreq_session_check_apply_v1(struct afb_xreq *xreq, int sessionflags)
560 {
561         int loa;
562
563         if ((sessionflags & (AFB_SESSION_CLOSE_V1|AFB_SESSION_RENEW_V1|AFB_SESSION_CHECK_V1|AFB_SESSION_LOA_EQ_V1)) != 0) {
564                 if (!afb_context_check(&xreq->context)) {
565                         afb_context_close(&xreq->context);
566                         afb_xreq_fail_f(xreq, "denied", "invalid token's identity");
567                         errno = EINVAL;
568                         return -1;
569                 }
570         }
571
572         if ((sessionflags & AFB_SESSION_LOA_GE_V1) != 0) {
573                 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1;
574                 if (!afb_context_check_loa(&xreq->context, loa)) {
575                         afb_xreq_fail_f(xreq, "denied", "invalid LOA");
576                         errno = EPERM;
577                         return -1;
578                 }
579         }
580
581         if ((sessionflags & AFB_SESSION_LOA_LE_V1) != 0) {
582                 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1;
583                 if (afb_context_check_loa(&xreq->context, loa + 1)) {
584                         afb_xreq_fail_f(xreq, "denied", "invalid LOA");
585                         errno = EPERM;
586                         return -1;
587                 }
588         }
589
590         if ((sessionflags & AFB_SESSION_RENEW_V1) != 0) {
591                 afb_context_refresh(&xreq->context);
592         }
593         if ((sessionflags & AFB_SESSION_CLOSE_V1) != 0) {
594                 afb_context_change_loa(&xreq->context, 0);
595                 afb_context_close(&xreq->context);
596         }
597
598         return 0;
599 }
600
601 static int xreq_session_check_apply_v2(struct afb_xreq *xreq, uint32_t sessionflags, const struct afb_auth *auth)
602 {
603         int loa;
604
605         if (sessionflags != 0) {
606                 if (!afb_context_check(&xreq->context)) {
607                         afb_context_close(&xreq->context);
608                         afb_xreq_fail_f(xreq, "denied", "invalid token's identity");
609                         errno = EINVAL;
610                         return -1;
611                 }
612         }
613
614         loa = (int)(sessionflags & AFB_SESSION_LOA_MASK_V2);
615         if (loa && !afb_context_check_loa(&xreq->context, loa)) {
616                 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
617                 errno = EPERM;
618                 return -1;
619         }
620
621         if (auth && !afb_auth_check(auth, xreq)) {
622                 afb_xreq_fail_f(xreq, "denied", "authorisation refused");
623                 errno = EPERM;
624                 return -1;
625         }
626
627         if ((sessionflags & AFB_SESSION_REFRESH_V2) != 0) {
628                 afb_context_refresh(&xreq->context);
629         }
630         if ((sessionflags & AFB_SESSION_CLOSE_V2) != 0) {
631                 afb_context_close(&xreq->context);
632         }
633
634         return 0;
635 }
636
637 void afb_xreq_call_verb_v1(struct afb_xreq *xreq, const struct afb_verb_desc_v1 *verb)
638 {
639         if (!verb)
640                 afb_xreq_fail_unknown_verb(xreq);
641         else
642                 if (!xreq_session_check_apply_v1(xreq, verb->session))
643                         verb->callback(to_req(xreq));
644 }
645
646 void afb_xreq_call_verb_v2(struct afb_xreq *xreq, const struct afb_verb_v2 *verb)
647 {
648         if (!verb)
649                 afb_xreq_fail_unknown_verb(xreq);
650         else
651                 if (!xreq_session_check_apply_v2(xreq, verb->session, verb->auth))
652                         verb->callback(to_req(xreq));
653 }
654
655 void afb_xreq_init(struct afb_xreq *xreq, const struct afb_xreq_query_itf *queryitf)
656 {
657         memset(xreq, 0, sizeof *xreq);
658         xreq->refcount = 1;
659         xreq->queryitf = queryitf;
660 }
661
662 void afb_xreq_fail_unknown_api(struct afb_xreq *xreq)
663 {
664         afb_xreq_fail_f(xreq, "unknown-api", "api %s not found (for verb %s)", xreq->api, xreq->verb);
665 }
666
667 void afb_xreq_fail_unknown_verb(struct afb_xreq *xreq)
668 {
669         afb_xreq_fail_f(xreq, "unknown-verb", "verb %s unknown within api %s", xreq->verb, xreq->api);
670 }
671
672 static void process_sync(struct afb_xreq *xreq)
673 {
674         struct afb_api api;
675
676         /* init hooking */
677         afb_hook_init_xreq(xreq);
678         if (xreq->hookflags)
679                 afb_hook_xreq_begin(xreq);
680
681         /* search the api */
682         if (afb_apiset_get_started(xreq->apiset, xreq->api, &api) < 0) {
683                 if (errno == ENOENT)
684                         afb_xreq_fail_f(xreq, "unknown-api", "api %s not found", xreq->api);
685                 else
686                         afb_xreq_fail_f(xreq, "bad-api-state", "api %s not started correctly: %m", xreq->api);
687         } else {
688                 xreq->context.api_key = api.closure;
689                 api.itf->call(api.closure, xreq);
690         }
691 }
692
693 static void process_async(int signum, void *arg)
694 {
695         struct afb_xreq *xreq = arg;
696
697         if (signum != 0) {
698                 afb_xreq_fail_f(xreq, "aborted", "signal %s(%d) caught", strsignal(signum), signum);
699         } else {
700                 process_sync(xreq);
701         }
702         afb_xreq_unref(xreq);
703 }
704
705 void afb_xreq_process(struct afb_xreq *xreq, struct afb_apiset *apiset)
706 {
707         xreq->apiset = apiset;
708
709         afb_xreq_addref(xreq);
710         if (jobs_queue(NULL, afb_apiset_timeout_get(apiset), process_async, xreq) < 0) {
711                 /* TODO: allows or not to proccess it directly as when no threading? (see above) */
712                 ERROR("can't process job with threads: %m");
713                 afb_xreq_fail_f(xreq, "cancelled", "not able to create a job for the task");
714                 afb_xreq_unref(xreq);
715         }
716         afb_xreq_unref(xreq);
717 }
718