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