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/afb-plugin.h>
31 #include <afb/afb-req-itf.h>
32 #include <afb/afb-event-sender-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[] = "pluginAfbV1Register";
53 static void afb_api_so_event_sender_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->v1.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_event_sender_itf event_sender_itf = {
68 .push = (void*)afb_api_so_event_sender_push
71 static struct afb_event_sender afb_api_so_get_event_sender(struct api_so_desc *desc)
73 return (struct afb_event_sender){ .itf = &event_sender_itf, .closure = desc };
76 static const struct afb_daemon_itf daemon_itf = {
77 .get_event_sender = (void*)afb_api_so_get_event_sender,
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_verb_desc_v1 *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_check_loa(context, 1)) {
112 afb_context_close(context);
113 afb_req_fail(req, "failed", "invalid creation state");
116 afb_context_change_loa(context, 1);
117 afb_context_refresh(context);
120 if ((stag & (AFB_SESSION_CREATE | AFB_SESSION_RENEW)) != 0)
121 afb_context_refresh(context);
123 if ((stag & AFB_SESSION_CLOSE) != 0) {
124 afb_context_change_loa(context, 0);
125 afb_context_close(context);
129 data.action = verb->callback;
130 afb_sig_monitor((void*)monitored_call, &data, api_timeout);
133 static void call(struct api_so_desc *desc, struct afb_req req, struct afb_context *context, const char *verb, size_t lenverb)
135 const struct AFB_verb_desc_v1 *v;
137 v = desc->plugin->v1.verbs;
138 while (v->name && (strncasecmp(v->name, verb, lenverb) || v->name[lenverb]))
141 call_check(req, context, v);
143 afb_req_fail_f(req, "unknown-verb", "verb %.*s unknown within api %s", (int)lenverb, verb, desc->plugin->v1.prefix);
146 int afb_api_so_add_plugin(const char *path)
150 struct api_so_desc *desc;
151 struct AFB_plugin *(*pluginAfbV1RegisterFct) (const struct AFB_interface *interface);
153 // This is a loadable library let's check if it's a plugin
155 handle = dlopen(path, RTLD_NOW | RTLD_LOCAL);
156 if (handle == NULL) {
157 ERROR("plugin [%s] not loadable", path);
161 /* retrieves the register function */
162 pluginAfbV1RegisterFct = dlsym(handle, plugin_register_function);
163 if (!pluginAfbV1RegisterFct) {
164 ERROR("plugin [%s] is not an AFB plugin", path);
167 INFO("plugin [%s] is a valid AFB plugin", path);
170 /* allocates the description */
171 desc = calloc(1, sizeof *desc);
173 ERROR("out of memory");
176 desc->handle = handle;
178 /* init the interface */
179 desc->interface.verbosity = 0;
180 desc->interface.mode = AFB_MODE_LOCAL;
181 desc->interface.daemon.itf = &daemon_itf;
182 desc->interface.daemon.closure = desc;
184 /* init the plugin */
185 desc->plugin = pluginAfbV1RegisterFct(&desc->interface);
186 if (desc->plugin == NULL) {
187 ERROR("plugin [%s] register function failed. continuing...", path);
191 /* check the returned structure */
192 if (desc->plugin->type != AFB_PLUGIN_VERSION_1) {
193 ERROR("plugin [%s] invalid type %d...", path, desc->plugin->type);
196 if (desc->plugin->v1.prefix == NULL || *desc->plugin->v1.prefix == 0) {
197 ERROR("plugin [%s] bad prefix...", path);
200 if (!afb_apis_is_valid_api_name(desc->plugin->v1.prefix)) {
201 ERROR("plugin [%s] invalid prefix...", path);
204 if (desc->plugin->v1.info == NULL || *desc->plugin->v1.info == 0) {
205 ERROR("plugin [%s] bad description...", path);
208 if (desc->plugin->v1.verbs == NULL) {
209 ERROR("plugin [%s] no APIs...", path);
213 /* records the plugin */
214 desc->apilength = strlen(desc->plugin->v1.prefix);
215 if (afb_apis_add(desc->plugin->v1.prefix, (struct afb_api){
217 .call = (void*)call}) < 0) {
218 ERROR("plugin [%s] can't be registered...", path);
221 NOTICE("plugin %s loaded with API prefix %s", path, desc->plugin->v1.prefix);
232 static int adddirs(char path[PATH_MAX], size_t end)
235 struct dirent ent, *result;
238 /* open the DIR now */
241 ERROR("can't scan plugin directory %s, %m", path);
244 INFO("Scanning dir=[%s] for plugins", path);
246 /* scan each entry */
250 readdir_r(dir, &ent, &result);
254 len = strlen(ent.d_name);
255 if (len + end >= PATH_MAX) {
256 ERROR("path too long while scanning plugins for %s", ent.d_name);
259 memcpy(&path[end], ent.d_name, len+1);
260 if (ent.d_type == DT_DIR) {
261 /* case of directories */
262 if (ent.d_name[0] == '.') {
265 if (ent.d_name[1] == '.' && len == 2)
268 adddirs(path, end+len);;
269 } else if (ent.d_type == DT_REG) {
271 if (!strstr(ent.d_name, ".so"))
273 if (afb_api_so_add_plugin(path) < 0)
281 int afb_api_so_add_directory(const char *path)
284 char buffer[PATH_MAX];
286 length = strlen(path);
287 if (length >= sizeof(buffer)) {
288 ERROR("path too long %lu [%.99s...]", (unsigned long)length, path);
292 memcpy(buffer, path, length + 1);
293 return adddirs(buffer, length);
296 int afb_api_so_add_path(const char *path)
301 rc = stat(path, &st);
303 ERROR("Invalid plugin path [%s]: %m", path);
304 else if (S_ISDIR(st.st_mode))
305 rc = afb_api_so_add_directory(path);
306 else if (strstr(path, ".so"))
307 rc = afb_api_so_add_plugin(path);
309 INFO("not a plugin [%s], skipped", path);
313 int afb_api_so_add_pathset(const char *pathset)
315 static char sep[] = ":";
318 ps = strdupa(pathset);
320 p = strsep(&ps, sep);
323 if (afb_api_so_add_path(p) < 0)