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.
19 #define NO_PLUGIN_VERBOSE_MACRO
28 #include <sys/types.h>
31 #include <afb/afb-plugin.h>
32 #include <afb/afb-req-itf.h>
33 #include <afb/afb-event-sender-itf.h>
36 #include "afb-common.h"
37 #include "afb-context.h"
39 #include "afb-api-so.h"
40 #include "afb-sig-handler.h"
44 * Description of a plugin
47 struct AFB_plugin *plugin; /* descriptor */
48 size_t apilength; /* length of the API name */
49 void *handle; /* context of dlopen */
50 struct AFB_interface interface; /* interface for the plugin */
53 static int api_timeout = 15;
55 static const char plugin_register_function_v1[] = "pluginAfbV1Register";
57 static void afb_api_so_event_sender_push(struct api_so_desc *desc, const char *name, struct json_object *object)
62 assert(desc->plugin != NULL);
63 length = strlen(name);
64 event = alloca(length + 2 + desc->apilength);
65 memcpy(event, desc->plugin->v1.prefix, desc->apilength);
66 event[desc->apilength] = '/';
67 memcpy(event + desc->apilength + 1, name, length + 1);
68 ctxClientEventSend(NULL, event, object);
71 static const struct afb_event_sender_itf event_sender_itf = {
72 .push = (void*)afb_api_so_event_sender_push
75 static struct afb_event_sender afb_api_so_get_event_sender(struct api_so_desc *desc)
77 return (struct afb_event_sender){ .itf = &event_sender_itf, .closure = desc };
80 static void afb_api_so_vverbose(struct api_so_desc *desc, int level, const char *file, int line, const char *fmt, va_list args)
84 if (vasprintf(&p, fmt, args) < 0)
85 vverbose(level, file, line, fmt, args);
87 verbose(level, file, line, "%s {plugin %s}", p, desc->plugin->v1.prefix);
92 static const struct afb_daemon_itf daemon_itf = {
93 .get_event_sender = (void*)afb_api_so_get_event_sender,
94 .get_event_loop = (void*)afb_common_get_event_loop,
95 .get_user_bus = (void*)afb_common_get_user_bus,
96 .get_system_bus = (void*)afb_common_get_system_bus,
97 .vverbose = (void*)afb_api_so_vverbose
102 void (*action)(struct afb_req);
105 static void monitored_call(int signum, struct monitoring *data)
108 afb_req_fail_f(data->req, "aborted", "signal %s(%d) caught", strsignal(signum), signum);
110 data->action(data->req);
113 static void call_check(struct afb_req req, struct afb_context *context, const struct AFB_verb_desc_v1 *verb)
115 struct monitoring data;
117 int stag = (int)verb->session;
119 if ((stag & (AFB_SESSION_CREATE|AFB_SESSION_CLOSE|AFB_SESSION_RENEW|AFB_SESSION_CHECK|AFB_SESSION_LOA_EQ)) != 0) {
120 if (!afb_context_check(context)) {
121 afb_context_close(context);
122 afb_req_fail(req, "failed", "invalid token's identity");
127 if ((stag & AFB_SESSION_CREATE) != 0) {
128 if (afb_context_check_loa(context, 1)) {
129 afb_req_fail(req, "failed", "invalid creation state");
132 afb_context_change_loa(context, 1);
133 afb_context_refresh(context);
136 if ((stag & (AFB_SESSION_CREATE | AFB_SESSION_RENEW)) != 0)
137 afb_context_refresh(context);
139 if ((stag & AFB_SESSION_CLOSE) != 0) {
140 afb_context_change_loa(context, 0);
141 afb_context_close(context);
144 if ((stag & AFB_SESSION_LOA_GE) != 0) {
145 int loa = (stag >> AFB_SESSION_LOA_SHIFT) & AFB_SESSION_LOA_MASK;
146 if (!afb_context_check_loa(context, loa)) {
147 afb_req_fail(req, "failed", "invalid LOA");
152 if ((stag & AFB_SESSION_LOA_LE) != 0) {
153 int loa = (stag >> AFB_SESSION_LOA_SHIFT) & AFB_SESSION_LOA_MASK;
154 if (afb_context_check_loa(context, loa + 1)) {
155 afb_req_fail(req, "failed", "invalid LOA");
161 data.action = verb->callback;
162 afb_sig_monitor((void*)monitored_call, &data, api_timeout);
165 static void call(struct api_so_desc *desc, struct afb_req req, struct afb_context *context, const char *verb, size_t lenverb)
167 const struct AFB_verb_desc_v1 *v;
169 v = desc->plugin->v1.verbs;
170 while (v->name && (strncasecmp(v->name, verb, lenverb) || v->name[lenverb]))
173 call_check(req, context, v);
175 afb_req_fail_f(req, "unknown-verb", "verb %.*s unknown within api %s", (int)lenverb, verb, desc->plugin->v1.prefix);
178 int afb_api_so_add_plugin(const char *path)
182 struct api_so_desc *desc;
183 struct AFB_plugin *(*pluginAfbV1RegisterFct) (const struct AFB_interface *interface);
185 // This is a loadable library let's check if it's a plugin
187 handle = dlopen(path, RTLD_NOW | RTLD_LOCAL);
188 if (handle == NULL) {
189 ERROR("plugin [%s] not loadable", path);
193 /* retrieves the register function */
194 pluginAfbV1RegisterFct = dlsym(handle, plugin_register_function_v1);
195 if (!pluginAfbV1RegisterFct) {
196 ERROR("plugin [%s] is not an AFB plugin", path);
199 INFO("plugin [%s] is a valid AFB plugin", path);
202 /* allocates the description */
203 desc = calloc(1, sizeof *desc);
205 ERROR("out of memory");
208 desc->handle = handle;
210 /* init the interface */
211 desc->interface.verbosity = verbosity;
212 desc->interface.mode = AFB_MODE_LOCAL;
213 desc->interface.daemon.itf = &daemon_itf;
214 desc->interface.daemon.closure = desc;
216 /* init the plugin */
217 NOTICE("plugin [%s] calling registering function %s", path, plugin_register_function_v1);
218 desc->plugin = pluginAfbV1RegisterFct(&desc->interface);
219 if (desc->plugin == NULL) {
220 ERROR("plugin [%s] register function failed. continuing...", path);
224 /* check the returned structure */
225 if (desc->plugin->type != AFB_PLUGIN_VERSION_1) {
226 ERROR("plugin [%s] invalid type %d...", path, desc->plugin->type);
229 if (desc->plugin->v1.prefix == NULL || *desc->plugin->v1.prefix == 0) {
230 ERROR("plugin [%s] bad prefix...", path);
233 if (!afb_apis_is_valid_api_name(desc->plugin->v1.prefix)) {
234 ERROR("plugin [%s] invalid prefix...", path);
237 if (desc->plugin->v1.info == NULL || *desc->plugin->v1.info == 0) {
238 ERROR("plugin [%s] bad description...", path);
241 if (desc->plugin->v1.verbs == NULL) {
242 ERROR("plugin [%s] no APIs...", path);
246 /* records the plugin */
247 desc->apilength = strlen(desc->plugin->v1.prefix);
248 if (afb_apis_add(desc->plugin->v1.prefix, (struct afb_api){
250 .call = (void*)call}) < 0) {
251 ERROR("plugin [%s] can't be registered...", path);
254 NOTICE("plugin %s loaded with API prefix %s", path, desc->plugin->v1.prefix);
265 static int adddirs(char path[PATH_MAX], size_t end)
268 struct dirent ent, *result;
271 /* open the DIR now */
274 ERROR("can't scan plugin directory %s, %m", path);
277 INFO("Scanning dir=[%s] for plugins", path);
279 /* scan each entry */
283 readdir_r(dir, &ent, &result);
287 len = strlen(ent.d_name);
288 if (len + end >= PATH_MAX) {
289 ERROR("path too long while scanning plugins for %s", ent.d_name);
292 memcpy(&path[end], ent.d_name, len+1);
293 if (ent.d_type == DT_DIR) {
294 /* case of directories */
295 if (ent.d_name[0] == '.') {
298 if (ent.d_name[1] == '.' && len == 2)
301 adddirs(path, end+len);;
302 } else if (ent.d_type == DT_REG) {
304 if (!strstr(ent.d_name, ".so"))
306 if (afb_api_so_add_plugin(path) < 0)
314 int afb_api_so_add_directory(const char *path)
317 char buffer[PATH_MAX];
319 length = strlen(path);
320 if (length >= sizeof(buffer)) {
321 ERROR("path too long %lu [%.99s...]", (unsigned long)length, path);
325 memcpy(buffer, path, length + 1);
326 return adddirs(buffer, length);
329 int afb_api_so_add_path(const char *path)
334 rc = stat(path, &st);
336 ERROR("Invalid plugin path [%s]: %m", path);
337 else if (S_ISDIR(st.st_mode))
338 rc = afb_api_so_add_directory(path);
339 else if (strstr(path, ".so"))
340 rc = afb_api_so_add_plugin(path);
342 INFO("not a plugin [%s], skipped", path);
346 int afb_api_so_add_pathset(const char *pathset)
348 static char sep[] = ":";
351 ps = strdupa(pathset);
353 p = strsep(&ps, sep);
356 if (afb_api_so_add_path(p) < 0)