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