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