fcb353affc85e63ad0f5535048dc1cc1624c7658
[src/app-framework-binder.git] / src / afb-autoset.c
1 /*
2  * Copyright (C) 2016-2019 "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
20 #include <stdlib.h>
21 #include <stdint.h>
22 #include <stdio.h>
23 #include <limits.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30
31 #include "verbose.h"
32 #include "afb-api-ws.h"
33 #include "afb-api-so.h"
34 #include "afb-apiset.h"
35 #include "afb-autoset.h"
36
37 static void cleanup(void *closure)
38 {
39         struct afb_apiset *call_set = closure;
40         afb_apiset_unref(call_set);
41 }
42
43 static int onlack(void *closure, struct afb_apiset *set, const char *name, int (*create)(const char *path, struct afb_apiset *declare_set, struct afb_apiset *call_set))
44 {
45         struct afb_apiset *call_set = closure;
46         char *path;
47         const char *base;
48         size_t lbase, lname;
49
50         base = afb_apiset_name(set);
51         lbase = strlen(base);
52         lname = strlen(name);
53
54         path = alloca(2 + lbase + lname);
55         memcpy(path, base, lbase);
56         path[lbase] = '/';
57         memcpy(&path[lbase + 1], name, lname + 1);
58
59         return create(path, set, call_set);
60 }
61
62 static int add(const char *path, struct afb_apiset *declare_set, struct afb_apiset *call_set, int (*callback)(void *, struct afb_apiset *, const char*))
63 {
64         struct afb_apiset *ownset;
65
66         /* create a sub-apiset */
67         ownset = afb_apiset_create_subset_last(declare_set, path, 3600);
68         if (!ownset) {
69                 ERROR("Can't create apiset autoset-ws %s", path);
70                 return -1;
71         }
72
73         /* set the onlack behaviour on this set */
74         afb_apiset_onlack_set(ownset, callback, afb_apiset_addref(call_set), cleanup);
75         return 0;
76 }
77
78 /*******************************************************************/
79
80 static int create_ws(const char *path, struct afb_apiset *declare_set, struct afb_apiset *call_set)
81 {
82         return afb_api_ws_add_client(path, declare_set, call_set, 0) >= 0;
83 }
84
85 static int onlack_ws(void *closure, struct afb_apiset *set, const char *name)
86 {
87         return onlack(closure, set, name, create_ws);
88 }
89
90 int afb_autoset_add_ws(const char *path, struct afb_apiset *declare_set, struct afb_apiset *call_set)
91 {
92         return add(path, declare_set, call_set, onlack_ws);
93 }
94
95 /*******************************************************************/
96
97 #if WITH_DYNAMIC_BINDING
98 static int create_so(const char *path, struct afb_apiset *declare_set, struct afb_apiset *call_set)
99 {
100         return afb_api_so_add_binding(path, declare_set, call_set) >= 0;
101 }
102
103 static int onlack_so(void *closure, struct afb_apiset *set, const char *name)
104 {
105         return onlack(closure, set, name, create_so);
106 }
107
108 int afb_autoset_add_so(const char *path, struct afb_apiset *declare_set, struct afb_apiset *call_set)
109 {
110         return add(path, declare_set, call_set, onlack_so);
111 }
112 #endif
113
114 /*******************************************************************/
115
116 static int create_any(const char *path, struct afb_apiset *declare_set, struct afb_apiset *call_set)
117 {
118         int rc;
119         struct stat st;
120         char sockname[PATH_MAX + 7];
121
122         rc = stat(path, &st);
123         if (!rc) {
124                 switch(st.st_mode & S_IFMT) {
125 #if WITH_DYNAMIC_BINDING
126                 case S_IFREG:
127                         rc = afb_api_so_add_binding(path, declare_set, call_set);
128                         break;
129 #endif
130                 case S_IFSOCK:
131                         snprintf(sockname, sizeof sockname, "unix:%s", path);
132                         rc = afb_api_ws_add_client(sockname, declare_set, call_set, 0);
133                         break;
134                 default:
135                         NOTICE("Unexpected autoset entry: %s", path);
136                         rc = -1;
137                         break;
138                 }
139         }
140         return rc >= 0;
141 }
142
143 static int onlack_any(void *closure, struct afb_apiset *set, const char *name)
144 {
145         return onlack(closure, set, name, create_any);
146 }
147
148 int afb_autoset_add_any(const char *path, struct afb_apiset *declare_set, struct afb_apiset *call_set)
149 {
150         return add(path, declare_set, call_set, onlack_any);
151 }