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