2 * Copyright (C) 2016 "IoT.bzh"
3 * Author José Bollo <jose.bollo@iot.bzh>
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
27 #include <sys/types.h>
30 #include "afb-plugin.h"
31 #include "afb-req-itf.h"
32 #include "afb-evmgr-itf.h"
35 #include "afb-common.h"
36 #include "afb-context.h"
38 #include "afb-api-so.h"
39 #include "afb-sig-handler.h"
43 struct AFB_plugin *plugin; /* descriptor */
45 void *handle; /* context of dlopen */
46 struct AFB_interface interface; /* interface */
49 static int api_timeout = 15;
51 static const char plugin_register_function[] = "pluginRegister";
53 static void afb_api_so_evmgr_push(struct api_so_desc *desc, const char *name, struct json_object *object)
58 assert(desc->plugin != NULL);
59 length = strlen(name);
60 event = alloca(length + 2 + desc->apilength);
61 memcpy(event, desc->plugin->prefix, desc->apilength);
62 event[desc->apilength] = '/';
63 memcpy(event + desc->apilength + 1, name, length + 1);
64 ctxClientEventSend(NULL, event, object);
67 static const struct afb_evmgr_itf evmgr_itf = {
68 .push = (void*)afb_api_so_evmgr_push
71 static struct afb_evmgr afb_api_so_get_evmgr(struct api_so_desc *desc)
73 return (struct afb_evmgr){ .itf = &evmgr_itf, .closure = desc };
76 static const struct afb_daemon_itf daemon_itf = {
77 .get_evmgr = (void*)afb_api_so_get_evmgr,
78 .get_event_loop = (void*)afb_common_get_event_loop,
79 .get_user_bus = (void*)afb_common_get_user_bus,
80 .get_system_bus = (void*)afb_common_get_system_bus
85 void (*action)(struct afb_req);
88 static void monitored_call(int signum, struct monitoring *data)
91 afb_req_fail_f(data->req, "aborted", "signal %s(%d) caught", strsignal(signum), signum);
93 data->action(data->req);
96 static void call_check(struct afb_req req, struct afb_context *context, const struct AFB_restapi *verb)
98 struct monitoring data;
100 int stag = (int)(verb->session & AFB_SESSION_MASK);
102 if (stag != AFB_SESSION_NONE) {
103 if (!afb_context_check(context)) {
104 afb_context_close(context);
105 afb_req_fail(req, "failed", "invalid token's identity");
110 if ((stag & AFB_SESSION_CREATE) != 0) {
111 if (!afb_context_create(context)) {
112 afb_context_close(context);
113 afb_req_fail(req, "failed", "invalid creation state");
118 if ((stag & (AFB_SESSION_CREATE | AFB_SESSION_RENEW)) != 0)
119 afb_context_refresh(context);
121 if ((stag & AFB_SESSION_CLOSE) != 0)
122 afb_context_close(context);
125 data.action = verb->callback;
126 afb_sig_monitor((void*)monitored_call, &data, api_timeout);
129 static void call(struct api_so_desc *desc, struct afb_req req, struct afb_context *context, const char *verb, size_t lenverb)
131 const struct AFB_restapi *v;
133 v = desc->plugin->apis;
134 while (v->name && (strncasecmp(v->name, verb, lenverb) || v->name[lenverb]))
137 call_check(req, context, v);
139 afb_req_fail_f(req, "unknown-verb", "verb %.*s unknown within api %s", (int)lenverb, verb, desc->plugin->prefix);
142 int afb_api_so_add_plugin(const char *path)
144 struct api_so_desc *desc;
145 struct AFB_plugin *(*pluginRegisterFct) (const struct AFB_interface *interface);
147 desc = calloc(1, sizeof *desc);
149 ERROR("out of memory");
153 // This is a loadable library let's check if it's a plugin
154 desc->handle = dlopen(path, RTLD_NOW | RTLD_LOCAL);
155 if (desc->handle == NULL) {
156 ERROR("plugin [%s] not loadable", path);
160 /* retrieves the register function */
161 pluginRegisterFct = dlsym(desc->handle, plugin_register_function);
162 if (!pluginRegisterFct) {
163 ERROR("plugin [%s] is not an AFB plugin", path);
166 INFO("plugin [%s] is a valid AFB plugin", path);
168 /* init the interface */
169 desc->interface.verbosity = 0;
170 desc->interface.mode = AFB_MODE_LOCAL;
171 desc->interface.daemon.itf = &daemon_itf;
172 desc->interface.daemon.closure = desc;
174 /* init the plugin */
175 desc->plugin = pluginRegisterFct(&desc->interface);
176 if (desc->plugin == NULL) {
177 ERROR("plugin [%s] register function failed. continuing...", path);
181 /* check the returned structure */
182 if (desc->plugin->type != AFB_PLUGIN_JSON) {
183 ERROR("plugin [%s] invalid type %d...", path, desc->plugin->type);
186 if (desc->plugin->prefix == NULL || *desc->plugin->prefix == 0) {
187 ERROR("plugin [%s] bad prefix...", path);
190 if (desc->plugin->info == NULL || *desc->plugin->info == 0) {
191 ERROR("plugin [%s] bad description...", path);
194 if (desc->plugin->apis == NULL) {
195 ERROR("plugin [%s] no APIs...", path);
199 /* records the plugin */
200 desc->apilength = strlen(desc->plugin->prefix);
201 if (afb_apis_add(desc->plugin->prefix, (struct afb_api){
203 .call = (void*)call}) < 0) {
204 ERROR("plugin [%s] can't be registered...", path);
207 NOTICE("plugin %s loaded with API prefix %s", path, desc->plugin->prefix);
211 dlclose(desc->handle);
218 static int adddirs(char path[PATH_MAX], size_t end)
221 struct dirent ent, *result;
224 /* open the DIR now */
227 ERROR("can't scan plugin directory %s, %m", path);
230 INFO("Scanning dir=[%s] for plugins", path);
232 /* scan each entry */
236 readdir_r(dir, &ent, &result);
240 len = strlen(ent.d_name);
241 if (len + end >= PATH_MAX) {
242 ERROR("path too long while scanning plugins for %s", ent.d_name);
245 memcpy(&path[end], ent.d_name, len+1);
246 if (ent.d_type == DT_DIR) {
247 /* case of directories */
248 if (ent.d_name[0] == '.') {
251 if (ent.d_name[1] == '.' && len == 2)
254 adddirs(path, end+len);;
255 } else if (ent.d_type == DT_REG) {
257 if (!strstr(ent.d_name, ".so"))
259 afb_api_so_add_plugin(path);
266 int afb_api_so_add_directory(const char *path)
269 char buffer[PATH_MAX];
271 length = strlen(path);
272 if (length >= sizeof(buffer)) {
273 ERROR("path too long %lu [%.99s...]", (unsigned long)length, path);
277 memcpy(buffer, path, length + 1);
278 return adddirs(buffer, length);
281 int afb_api_so_add_path(const char *path)
286 rc = stat(path, &st);
288 ERROR("Invalid plugin path [%s]: %m", path);
289 else if (S_ISDIR(st.st_mode))
290 rc = afb_api_so_add_directory(path);
291 else if (strstr(path, ".so"))
292 rc = afb_api_so_add_plugin(path);
294 INFO("not a plugin [%s], skipped", path);
298 int afb_api_so_add_pathset(const char *pathset)
300 static char sep[] = ":";
303 ps = strdupa(pathset);
305 p = strsep(&ps, sep);
308 afb_api_so_add_path(p);