Add vfail and vsuccess interfaces
[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 /******************************************************************************/
261
262 static struct json_object *xreq_hooked_json_cb(void *closure)
263 {
264         struct json_object *r = xreq_json_cb(closure);
265         struct afb_xreq *xreq = closure;
266         return afb_hook_xreq_json(xreq, r);
267 }
268
269 static struct afb_arg xreq_hooked_get_cb(void *closure, const char *name)
270 {
271         struct afb_arg r = xreq_get_cb(closure, name);
272         struct afb_xreq *xreq = closure;
273         return afb_hook_xreq_get(xreq, name, r);
274 }
275
276 static void xreq_hooked_success_cb(void *closure, struct json_object *obj, const char *info)
277 {
278         struct afb_xreq *xreq = closure;
279         afb_hook_xreq_success(xreq, obj, info);
280         xreq_success_cb(closure, obj, info);
281 }
282
283 static void xreq_hooked_fail_cb(void *closure, const char *status, const char *info)
284 {
285         struct afb_xreq *xreq = closure;
286         afb_hook_xreq_fail(xreq, status, info);
287         xreq_fail_cb(closure, status, info);
288 }
289
290 static void xreq_hooked_vsuccess_cb(void *closure, struct json_object *obj, const char *fmt, va_list args)
291 {
292         vinfo(closure, obj, fmt, args, (void*)xreq_hooked_success_cb);
293 }
294
295 static void xreq_hooked_vfail_cb(void *closure, const char *status, const char *fmt, va_list args)
296 {
297         vinfo(closure, (void*)status, fmt, args, (void*)xreq_hooked_fail_cb);
298 }
299
300 static void *xreq_hooked_context_get_cb(void *closure)
301 {
302         void *r = xreq_context_get_cb(closure);
303         struct afb_xreq *xreq = closure;
304         return afb_hook_xreq_context_get(xreq, r);
305 }
306
307 static void xreq_hooked_context_set_cb(void *closure, void *value, void (*free_value)(void*))
308 {
309         struct afb_xreq *xreq = closure;
310         afb_hook_xreq_context_set(xreq, value, free_value);
311         xreq_context_set_cb(closure, value, free_value);
312 }
313
314 static void xreq_hooked_addref_cb(void *closure)
315 {
316         struct afb_xreq *xreq = closure;
317         afb_hook_xreq_addref(xreq);
318         xreq_addref_cb(closure);
319 }
320
321 static void xreq_hooked_unref_cb(void *closure)
322 {
323         struct afb_xreq *xreq = closure;
324         afb_hook_xreq_unref(xreq);
325         if (!__atomic_sub_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED)) {
326                 afb_hook_xreq_end(xreq);
327                 xreq->queryitf->unref(xreq);
328         }
329 }
330
331 static void xreq_hooked_session_close_cb(void *closure)
332 {
333         struct afb_xreq *xreq = closure;
334         afb_hook_xreq_session_close(xreq);
335         xreq_session_close_cb(closure);
336 }
337
338 static int xreq_hooked_session_set_LOA_cb(void *closure, unsigned level)
339 {
340         int r = xreq_session_set_LOA_cb(closure, level);
341         struct afb_xreq *xreq = closure;
342         return afb_hook_xreq_session_set_LOA(xreq, level, r);
343 }
344
345 static int xreq_hooked_subscribe_cb(void *closure, struct afb_event event)
346 {
347         int r = xreq_subscribe_cb(closure, event);
348         struct afb_xreq *xreq = closure;
349         return afb_hook_xreq_subscribe(xreq, event, r);
350 }
351
352 static int xreq_hooked_unsubscribe_cb(void *closure, struct afb_event event)
353 {
354         int r = xreq_unsubscribe_cb(closure, event);
355         struct afb_xreq *xreq = closure;
356         return afb_hook_xreq_unsubscribe(xreq, event, r);
357 }
358
359 struct reply
360 {
361         struct afb_xreq *xreq;
362         void (*callback)(void*, int, struct json_object*);
363         void *closure;
364 };
365
366 static void xreq_hooked_subcall_reply_cb(void *closure, int iserror, struct json_object *result)
367 {
368         struct reply *reply = closure;
369         
370         afb_hook_xreq_subcall_result(reply->xreq, iserror, result);
371         reply->callback(reply->closure, iserror, result);
372         free(reply);
373 }
374
375 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)
376 {
377         struct reply *reply = malloc(sizeof *reply);
378         struct afb_xreq *xreq = closure;
379         afb_hook_xreq_subcall(xreq, api, verb, args);
380         if (reply) {
381                 reply->xreq = xreq;
382                 reply->callback = callback;
383                 reply->closure = cb_closure;
384                 xreq_subcall_cb(closure, api, verb, args, xreq_hooked_subcall_reply_cb, reply);
385         } else {
386                 ERROR("out of memory");
387                 xreq_subcall_cb(closure, api, verb, args, callback, cb_closure);
388         }
389 }
390
391 static int xreq_hooked_subcallsync_cb(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
392 {
393         int r;
394         struct afb_xreq *xreq = closure;
395         afb_hook_xreq_subcallsync(xreq, api, verb, args);
396         r = xreq_subcallsync_cb(closure, api, verb, args, result);
397         return afb_hook_xreq_subcallsync_result(xreq, r, *result);
398 }
399
400 /******************************************************************************/
401
402 const struct afb_req_itf xreq_itf = {
403         .json = xreq_json_cb,
404         .get = xreq_get_cb,
405         .success = xreq_success_cb,
406         .fail = xreq_fail_cb,
407         .vsuccess = xreq_vsuccess_cb,
408         .vfail = xreq_vfail_cb,
409         .context_get = xreq_context_get_cb,
410         .context_set = xreq_context_set_cb,
411         .addref = xreq_addref_cb,
412         .unref = xreq_unref_cb,
413         .session_close = xreq_session_close_cb,
414         .session_set_LOA = xreq_session_set_LOA_cb,
415         .subscribe = xreq_subscribe_cb,
416         .unsubscribe = xreq_unsubscribe_cb,
417         .subcall = xreq_subcall_cb,
418         .subcallsync = xreq_subcallsync_cb
419 };
420
421 const struct afb_req_itf xreq_hooked_itf = {
422         .json = xreq_hooked_json_cb,
423         .get = xreq_hooked_get_cb,
424         .success = xreq_hooked_success_cb,
425         .fail = xreq_hooked_fail_cb,
426         .vsuccess = xreq_hooked_vsuccess_cb,
427         .vfail = xreq_hooked_vfail_cb,
428         .context_get = xreq_hooked_context_get_cb,
429         .context_set = xreq_hooked_context_set_cb,
430         .addref = xreq_hooked_addref_cb,
431         .unref = xreq_hooked_unref_cb,
432         .session_close = xreq_hooked_session_close_cb,
433         .session_set_LOA = xreq_hooked_session_set_LOA_cb,
434         .subscribe = xreq_hooked_subscribe_cb,
435         .unsubscribe = xreq_hooked_unsubscribe_cb,
436         .subcall = xreq_hooked_subcall_cb,
437         .subcallsync = xreq_hooked_subcallsync_cb
438 };
439
440 static inline struct afb_req to_req(struct afb_xreq *xreq)
441 {
442         return (struct afb_req){ .itf = xreq->hookflags ? &xreq_hooked_itf : &xreq_itf, .closure = xreq };
443 }
444
445 /******************************************************************************/
446
447 struct json_object *afb_xreq_json(struct afb_xreq *xreq)
448 {
449         return afb_req_json(to_req(xreq));
450 }
451
452 void afb_xreq_success(struct afb_xreq *xreq, struct json_object *obj, const char *info)
453 {
454         afb_req_success(to_req(xreq), obj, info);
455 }
456
457 void afb_xreq_success_f(struct afb_xreq *xreq, struct json_object *obj, const char *info, ...)
458 {
459         char *message;
460         va_list args;
461         va_start(args, info);
462         if (info == NULL || vasprintf(&message, info, args) < 0)
463                 message = NULL;
464         va_end(args);
465         afb_xreq_success(xreq, obj, message);
466         free(message);
467 }
468
469 void afb_xreq_fail(struct afb_xreq *xreq, const char *status, const char *info)
470 {
471         afb_req_fail(to_req(xreq), status, info);
472 }
473
474 void afb_xreq_fail_f(struct afb_xreq *xreq, const char *status, const char *info, ...)
475 {
476         char *message;
477         va_list args;
478         va_start(args, info);
479         if (info == NULL || vasprintf(&message, info, args) < 0)
480                 message = NULL;
481         va_end(args);
482         afb_xreq_fail(xreq, status, message);
483         free(message);
484 }
485
486 const char *afb_xreq_raw(struct afb_xreq *xreq, size_t *size)
487 {
488         struct json_object *obj = xreq_json_cb(xreq);
489         const char *result = json_object_to_json_string(obj);
490         if (size != NULL)
491                 *size = strlen(result);
492         return result;
493 }
494
495 void afb_xreq_addref(struct afb_xreq *xreq)
496 {
497         afb_req_addref(to_req(xreq));
498 }
499
500 void afb_xreq_unref(struct afb_xreq *xreq)
501 {
502         afb_req_unref(to_req(xreq));
503 }
504
505 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)
506 {
507         xreq_subcall_cb(xreq, api, verb, args, callback, cb_closure);
508 }
509
510 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)
511 {
512         afb_req_subcall(to_req(xreq), api, verb, args, callback, cb_closure);
513 }
514
515 int afb_xreq_unhooked_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
516 {
517         return xreq_subcallsync_cb(xreq, api, verb, args, result);
518 }
519
520 int afb_xreq_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
521 {
522         return afb_req_subcall_sync(to_req(xreq), api, verb, args, result);
523 }
524
525 static int xreq_session_check_apply_v1(struct afb_xreq *xreq, int sessionflags)
526 {
527         int loa;
528
529         if ((sessionflags & (AFB_SESSION_CLOSE_V1|AFB_SESSION_RENEW_V1|AFB_SESSION_CHECK_V1|AFB_SESSION_LOA_EQ_V1)) != 0) {
530                 if (!afb_context_check(&xreq->context)) {
531                         afb_context_close(&xreq->context);
532                         afb_xreq_fail_f(xreq, "denied", "invalid token's identity");
533                         errno = EINVAL;
534                         return -1;
535                 }
536         }
537
538         if ((sessionflags & AFB_SESSION_LOA_GE_V1) != 0) {
539                 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1;
540                 if (!afb_context_check_loa(&xreq->context, loa)) {
541                         afb_xreq_fail_f(xreq, "denied", "invalid LOA");
542                         errno = EPERM;
543                         return -1;
544                 }
545         }
546
547         if ((sessionflags & AFB_SESSION_LOA_LE_V1) != 0) {
548                 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1;
549                 if (afb_context_check_loa(&xreq->context, loa + 1)) {
550                         afb_xreq_fail_f(xreq, "denied", "invalid LOA");
551                         errno = EPERM;
552                         return -1;
553                 }
554         }
555
556         if ((sessionflags & AFB_SESSION_RENEW_V1) != 0) {
557                 afb_context_refresh(&xreq->context);
558         }
559         if ((sessionflags & AFB_SESSION_CLOSE_V1) != 0) {
560                 afb_context_change_loa(&xreq->context, 0);
561                 afb_context_close(&xreq->context);
562         }
563
564         return 0;
565 }
566
567 static int xreq_session_check_apply_v2(struct afb_xreq *xreq, uint32_t sessionflags, const struct afb_auth *auth)
568 {
569         int loa;
570
571         if (sessionflags != 0) {
572                 if (!afb_context_check(&xreq->context)) {
573                         afb_context_close(&xreq->context);
574                         afb_xreq_fail_f(xreq, "denied", "invalid token's identity");
575                         errno = EINVAL;
576                         return -1;
577                 }
578         }
579
580         loa = (int)(sessionflags & AFB_SESSION_LOA_MASK_V2);
581         if (loa && !afb_context_check_loa(&xreq->context, loa)) {
582                 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
583                 errno = EPERM;
584                 return -1;
585         }
586
587         if (auth && !afb_auth_check(auth, xreq)) {
588                 afb_xreq_fail_f(xreq, "denied", "authorisation refused");
589                 errno = EPERM;
590                 return -1;
591         }
592
593         if ((sessionflags & AFB_SESSION_REFRESH_V2) != 0) {
594                 afb_context_refresh(&xreq->context);
595         }
596         if ((sessionflags & AFB_SESSION_CLOSE_V2) != 0) {
597                 afb_context_close(&xreq->context);
598         }
599
600         return 0;
601 }
602
603 void afb_xreq_call_verb_v1(struct afb_xreq *xreq, const struct afb_verb_desc_v1 *verb)
604 {
605         if (!verb)
606                 afb_xreq_fail_unknown_verb(xreq);
607         else
608                 if (!xreq_session_check_apply_v1(xreq, verb->session))
609                         verb->callback(to_req(xreq));
610 }
611
612 void afb_xreq_call_verb_v2(struct afb_xreq *xreq, const struct afb_verb_v2 *verb)
613 {
614         if (!verb)
615                 afb_xreq_fail_unknown_verb(xreq);
616         else
617                 if (!xreq_session_check_apply_v2(xreq, verb->session, verb->auth))
618                         verb->callback(to_req(xreq));
619 }
620
621 void afb_xreq_init(struct afb_xreq *xreq, const struct afb_xreq_query_itf *queryitf)
622 {
623         memset(xreq, 0, sizeof *xreq);
624         xreq->refcount = 1;
625         xreq->queryitf = queryitf;
626 }
627
628 void afb_xreq_fail_unknown_api(struct afb_xreq *xreq)
629 {
630         afb_xreq_fail_f(xreq, "unknown-api", "api %s not found (for verb %s)", xreq->api, xreq->verb);
631 }
632
633 void afb_xreq_fail_unknown_verb(struct afb_xreq *xreq)
634 {
635         afb_xreq_fail_f(xreq, "unknown-verb", "verb %s unknown within api %s", xreq->verb, xreq->api);
636 }
637
638 static void process_sync(struct afb_xreq *xreq)
639 {
640         struct afb_api api;
641
642         /* init hooking */
643         afb_hook_init_xreq(xreq);
644         if (xreq->hookflags)
645                 afb_hook_xreq_begin(xreq);
646
647         /* search the api */
648         if (afb_apiset_get(xreq->apiset, xreq->api, &api) < 0) {
649                 afb_xreq_fail_f(xreq, "unknown-api", "api %s not found", xreq->api);
650         } else {
651                 xreq->context.api_key = api.closure;
652                 api.itf->call(api.closure, xreq);
653         }
654 }
655
656 static void process_async(int signum, void *arg)
657 {
658         struct afb_xreq *xreq = arg;
659
660         if (signum != 0) {
661                 afb_xreq_fail_f(xreq, "aborted", "signal %s(%d) caught", strsignal(signum), signum);
662         } else {
663                 process_sync(xreq);
664         }
665         afb_xreq_unref(xreq);
666 }
667
668 void afb_xreq_process(struct afb_xreq *xreq, struct afb_apiset *apiset)
669 {
670         xreq->apiset = apiset;
671
672         afb_xreq_addref(xreq);
673         if (jobs_queue(NULL, afb_apiset_timeout_get(apiset), process_async, xreq) < 0) {
674                 /* TODO: allows or not to proccess it directly as when no threading? (see above) */
675                 ERROR("can't process job with threads: %m");
676                 afb_xreq_fail_f(xreq, "cancelled", "not able to create a job for the task");
677                 afb_xreq_unref(xreq);
678         }
679         afb_xreq_unref(xreq);
680 }
681