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