afb-ws & websocket: Fix writing very long data
[src/app-framework-binder.git] / src / afb-apiset.c
1 /*
2  * Copyright (C) 2016, 2017 "IoT.bzh"
3  * Author "Fulup Ar Foll"
4  * Author José Bollo <jose.bollo@iot.bzh>
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *   http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 #define _GNU_SOURCE
20
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <errno.h>
25
26 #include "afb-session.h"
27 #include "verbose.h"
28 #include "afb-api.h"
29 #include "afb-apiset.h"
30 #include "afb-context.h"
31 #include "afb-xreq.h"
32
33 #define INCR 8          /* CAUTION: must be a power of 2 */
34
35 /**
36  * Internal description of an api
37  */
38 struct api_desc
39 {
40         int status;
41         const char *name;       /**< name of the api */
42         struct afb_api api;     /**< handler of the api */
43 };
44
45 /**
46  * Data structure for apiset
47  */
48 struct afb_apiset
49 {
50         struct api_desc *apis;          /**< description of apis */
51         struct afb_apiset *subset;      /**< subset if any */
52         int count;                      /**< count of apis in the set */
53         int timeout;                    /**< the timeout in second for the apiset */
54         int refcount;                   /**< reference count for freeing resources */
55         char name[1];                   /**< name of the apiset */
56 };
57
58 /**
59  * Search the api of 'name'.
60  * @param set the api set
61  * @param name the api name to search
62  * @return the descriptor if found or NULL otherwise
63  */
64 static struct api_desc *search(struct afb_apiset *set, const char *name)
65 {
66         int i, c, up, lo;
67         struct api_desc *a;
68
69         /* dichotomic search of the api */
70         /* initial slice */
71         lo = 0;
72         up = set->count;
73         for (;;) {
74                 /* check remaining slice */
75                 if (lo >= up) {
76                         /* not found */
77                         return NULL;
78                 }
79                 /* check the mid of the slice */
80                 i = (lo + up) >> 1;
81                 a = &set->apis[i];
82                 c = strcasecmp(a->name, name);
83                 if (c == 0) {
84                         /* found */
85                         return a;
86                 }
87                 /* update the slice */
88                 if (c < 0)
89                         lo = i + 1;
90                 else
91                         up = i;
92         }
93 }
94
95 /**
96  * Increases the count of references to the apiset and return its address
97  * @param set the set whose reference count is to be increased
98  * @return the given apiset
99  */
100 struct afb_apiset *afb_apiset_addref(struct afb_apiset *set)
101 {
102         if (set)
103                 __atomic_add_fetch(&set->refcount, 1, __ATOMIC_RELAXED);
104         return set;
105 }
106
107 /**
108  * Decreases the count of references to the apiset and frees its
109  * resources when no more references exists.
110  * @param set the set to unrefrence
111  */
112 void afb_apiset_unref(struct afb_apiset *set)
113 {
114         if (set && !__atomic_sub_fetch(&set->refcount, 1, __ATOMIC_RELAXED)) {
115                 afb_apiset_unref(set->subset);
116                 free(set->apis);
117                 free(set);
118         }
119 }
120
121 /**
122  * Create an apiset
123  * @param name the name of the apiset
124  * @param timeout the default timeout in seconds for the apiset
125  * @return the created apiset or NULL in case of error
126  */
127 struct afb_apiset *afb_apiset_create(const char *name, int timeout)
128 {
129         struct afb_apiset *set;
130
131         set = malloc((name ? strlen(name) : 0) + sizeof *set);
132         if (set) {
133                 set->apis = malloc(INCR * sizeof *set->apis);
134                 set->count = 0;
135                 set->timeout = timeout;
136                 set->refcount = 1;
137                 set->subset = NULL;
138                 if (name)
139                         strcpy(set->name, name);
140                 else
141                         set->name[0] = 0;
142         }
143         return set;
144 }
145
146 /**
147  * the name of the apiset
148  * @param set the api set
149  * @return the name of the set
150  */
151 const char *afb_apiset_name(struct afb_apiset *set)
152 {
153         return set->name;
154 }
155
156 /**
157  * Get the API timeout of the set
158  * @param set the api set
159  * @return the timeout in seconds
160  */
161 int afb_apiset_timeout_get(struct afb_apiset *set)
162 {
163         return set->timeout;
164 }
165
166 /**
167  * Set the API timeout of the set
168  * @param set the api set
169  * @param to the timeout in seconds
170  */
171 void afb_apiset_timeout_set(struct afb_apiset *set, int to)
172 {
173         set->timeout = to;
174 }
175
176 /**
177  * Get the subset of the set
178  * @param set the api set
179  * @return the subset of set
180  */
181 struct afb_apiset *afb_apiset_subset_get(struct afb_apiset *set)
182 {
183         return set->subset;
184 }
185
186 /**
187  * Set the subset of the set
188  * @param set the api set
189  * @param subset the subset to set
190  */
191 void afb_apiset_subset_set(struct afb_apiset *set, struct afb_apiset *subset)
192 {
193         struct afb_apiset *tmp;
194         if (subset == set) {
195                 /* avoid infinite loop */
196                 subset = NULL;
197         }
198         tmp = set->subset;
199         set->subset = afb_apiset_addref(subset);
200         afb_apiset_unref(tmp);
201 }
202
203 /**
204  * Adds the api of 'name' described by 'api'.
205  * @param set the api set
206  * @param name the name of the api to add (have to survive, not copied!)
207  * @param api the api
208  * @returns 0 in case of success or -1 in case
209  * of error with errno set:
210  *   - EEXIST if name already registered
211  *   - ENOMEM when out of memory
212  */
213 int afb_apiset_add(struct afb_apiset *set, const char *name, struct afb_api api)
214 {
215         struct api_desc *apis;
216         int i, c;
217
218         /* check previously existing plugin */
219         for (i = 0 ; i < set->count ; i++) {
220                 c = strcasecmp(set->apis[i].name, name);
221                 if (c == 0) {
222                         ERROR("api of name %s already exists", name);
223                         errno = EEXIST;
224                         goto error;
225                 }
226                 if (c > 0)
227                         break;
228         }
229
230         /* allocates enough memory */
231         c = (set->count + INCR) & ~(INCR - 1);
232         apis = realloc(set->apis, ((unsigned)c) * sizeof * apis);
233         if (apis == NULL) {
234                 ERROR("out of memory");
235                 errno = ENOMEM;
236                 goto error;
237         }
238         set->apis = apis;
239
240         /* copy higher part of the array */
241         apis += i;
242         if (i != set->count)
243                 memmove(apis + 1, apis, ((unsigned)(set->count - i)) * sizeof *apis);
244
245         /* record the plugin */
246         apis->status = -1;
247         apis->api = api;
248         apis->name = name;
249         set->count++;
250
251         INFO("API %s added", name);
252
253         return 0;
254
255 error:
256         return -1;
257 }
258
259 /**
260  * Delete from the 'set' the api of 'name'.
261  * @param set the set to be changed
262  * @param name the name of the API to remove
263  * @return 0 in case of success or -1 in case where the API doesn't exist.
264  */
265 int afb_apiset_del(struct afb_apiset *set, const char *name)
266 {
267         int i, c;
268
269         /* search the api */
270         for (i = 0 ; i < set->count ; i++) {
271                 c = strcasecmp(set->apis[i].name, name);
272                 if (c == 0) {
273                         set->count--;
274                         while(i < set->count) {
275                                 set->apis[i] = set->apis[i + 1];
276                                 i++;
277                         }
278                         return 0;
279                 }
280                 if (c > 0)
281                         break;
282         }
283         errno = ENOENT;
284         return -1;
285 }
286
287 /**
288  * Get from the 'set' the API of 'name' in 'api' with fallback to subset or default api
289  * @param set the set of API
290  * @param name the name of the API to get
291  * @param rec if not zero look also recursively in subsets
292  * @return the api pointer in case of success or NULL in case of error
293  */
294 static struct api_desc *lookup(struct afb_apiset *set, const char *name, int rec)
295 {
296         struct api_desc *i = search(set, name);
297         return i || !rec || !set->subset ? i : lookup(set->subset, name, rec);
298 }
299
300 /**
301  * Get from the 'set' the API of 'name' in 'api'
302  * @param set the set of API
303  * @param name the name of the API to get
304  * @param rec if not zero look also recursively in subsets
305  * @return the api pointer in case of success or NULL in case of error
306  */
307 const struct afb_api *afb_apiset_lookup(struct afb_apiset *set, const char *name, int rec)
308 {
309         struct api_desc *i;
310
311         i = lookup(set, name, rec);
312         if (i)
313                 return &i->api;
314         errno = ENOENT;
315         return NULL;
316 }
317
318 /**
319  * Starts the service 'api'.
320  * @param api the api
321  * @param share_session if true start the servic"e in a shared session
322  *                      if false start it in its own session
323  * @param onneed if true start the service if possible, if false the api
324  *               must be a service
325  * @return a positive number on success
326  */
327 static int start_api(struct afb_apiset *set, struct api_desc *api, int share_session, int onneed)
328 {
329         int rc;
330
331         if (api->status == 0)
332                 return 0;
333         else if (api->status > 0) {
334                 errno = api->status;
335                 return -1;
336         }
337
338         INFO("API %s starting...", api->name);
339         if (api->api.itf->service_start) {
340                 api->status = EBUSY;
341                 rc = api->api.itf->service_start(api->api.closure, share_session, onneed, set);
342                 if (rc < 0) {
343                         api->status = errno ?: ECANCELED;
344                         ERROR("The api %s failed to start (%d)", api->name, rc);
345                         return -1;
346                 }
347         } else if (!onneed) {
348                 /* already started: it is an error */
349                 ERROR("The api %s is not a startable service", api->name);
350                 api->status = EINVAL;
351                 return -1;
352         }
353         NOTICE("API %s started", api->name);
354         api->status = 0;
355         return 0;
356 }
357
358 /**
359  * Get from the 'set' the API of 'name' in 'api'
360  * @param set the set of API
361  * @param name the name of the API to get
362  * @param rec if not zero look also recursively in subsets
363  * @return 0 in case of success or -1 in case of error
364  */
365 const struct afb_api *afb_apiset_lookup_started(struct afb_apiset *set, const char *name, int rec)
366 {
367         struct api_desc *i;
368
369         i = lookup(set, name, rec);
370         if (i)
371                 return i->status && start_api(set, i, 1, 1) ? NULL : &i->api;
372         errno = ENOENT;
373         return NULL;
374 }
375
376 /**
377  * Starts a service by its 'api' name.
378  * @param set the api set
379  * @param name name of the service to start
380  * @param share_session if true start the servic"e in a shared session
381  *                      if false start it in its own session
382  * @param onneed if true start the service if possible, if false the api
383  *               must be a service
384  * @return a positive number on success
385  */
386 int afb_apiset_start_service(struct afb_apiset *set, const char *name, int share_session, int onneed)
387 {
388         struct api_desc *a;
389
390         a = search(set, name);
391         if (!a) {
392                 ERROR("can't find service %s", name);
393                 errno = ENOENT;
394                 return -1;
395         }
396
397         return start_api(set, a, share_session, onneed);
398 }
399
400 /**
401  * Starts all possible services but stops at first error.
402  * @param set the api set
403  * @param share_session if true start the servic"e in a shared session
404  *                      if false start it in its own session
405  * @return 0 on success or a negative number when an error is found
406  */
407 int afb_apiset_start_all_services(struct afb_apiset *set, int share_session)
408 {
409         int rc;
410         struct api_desc *i, *e;
411
412         i = set->apis;
413         e = &set->apis[set->count];
414         while (i != e) {
415                 rc = start_api(set, i, share_session, 1);
416                 if (rc < 0)
417                         return rc;
418                 i++;
419         }
420
421         return set->subset ? afb_apiset_start_all_services(set->subset, share_session) : 0;
422 }
423
424 /**
425  * Ask to update the hook flags of the 'api'
426  * @param set the api set
427  * @param name the api to update (NULL updates all)
428  */
429 void afb_apiset_update_hooks(struct afb_apiset *set, const char *name)
430 {
431         const struct api_desc *i, *e;
432
433         if (!name) {
434                 i = set->apis;
435                 e = &set->apis[set->count];
436         } else {
437                 i = search(set, name);
438                 e = &i[!!i];
439         }
440         while (i != e) {
441                 if (i->api.itf->update_hooks)
442                         i->api.itf->update_hooks(i->api.closure);
443                 i++;
444         }
445 }
446
447 /**
448  * Set the verbosity level of the 'api'
449  * @param set the api set
450  * @param name the api to set (NULL set all)
451  */
452 void afb_apiset_set_verbosity(struct afb_apiset *set, const char *name, int level)
453 {
454         const struct api_desc *i, *e;
455
456         if (!name) {
457                 i = set->apis;
458                 e = &set->apis[set->count];
459         } else {
460                 i = search(set, name);
461                 e = &i[!!i];
462         }
463         while (i != e) {
464                 if (i->api.itf->set_verbosity)
465                         i->api.itf->set_verbosity(i->api.closure, level);
466                 i++;
467         }
468 }
469
470 /**
471  * Get the verbosity level of the 'api'
472  * @param set the api set
473  * @param name the api to get
474  * @return the verbosity level or -1 in case of error
475  */
476 int afb_apiset_get_verbosity(struct afb_apiset *set, const char *name)
477 {
478         const struct api_desc *i;
479
480         i = name ? search(set, name) : NULL;
481         if (!i) {
482                 errno = ENOENT;
483                 return -1;
484         }
485
486         if (!i->api.itf->get_verbosity)
487                 return verbosity;
488
489         return i->api.itf->get_verbosity(i->api.closure);
490 }
491
492 /**
493  * Get the description of the API of 'name'
494  * @param set the api set
495  * @param name the api whose description is required
496  * @return the description or NULL
497  */
498 struct json_object *afb_apiset_describe(struct afb_apiset *set, const char *name)
499 {
500         const struct api_desc *i;
501
502         i = name ? search(set, name) : NULL;
503         return i && i->api.itf->describe ? i->api.itf->describe(i->api.closure) : NULL;
504 }
505
506 /**
507  * Get the list of api names
508  * @param set the api set
509  * @return a NULL terminated array of api names. Must be freed.
510  */
511 const char **afb_apiset_get_names(struct afb_apiset *set)
512 {
513         size_t size;
514         char *dest;
515         const char **names;
516         int i;
517
518         size = set->count * (1 + sizeof(*names)) + sizeof(*names);
519         for (i = 0 ; i < set->count ; i++)
520                 size += strlen(set->apis[i].name);
521
522         names = malloc(size);
523         if (!names)
524                 errno = ENOMEM;
525         else {
526                 dest = (void*)&names[set->count+1];
527                 for (i = 0 ; i < set->count ; i++) {
528                         names[i] = dest;
529                         dest = stpcpy(dest, set->apis[i].name) + 1;
530                 }
531                 names[i] = NULL;
532         }
533         return names;
534 }
535
536 /**
537  * Enumerate the api names to a callback.
538  * @param set the api set
539  * @param callback the function to call for each name
540  * @param closure the closure for the callback
541  */
542 void afb_apiset_enum(struct afb_apiset *set, int rec, void (*callback)(struct afb_apiset *set, const char *name, void *closure), void *closure)
543 {
544         struct afb_apiset *iset;
545         struct api_desc *i, *e;
546
547         iset = set;
548         while (iset) {
549                 i = iset->apis;
550                 e = &i[iset->count];
551                 while (i != e) {
552                         if (lookup(set, i->name, 1) == i)
553                                 callback(iset, i->name, closure);
554                         i++;
555                 }
556                 iset = rec ? iset->subset : NULL;
557         }
558 }
559