X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Frest-api.c;h=398ba5779171087b5b70b42e535976e46f3b25aa;hb=7181ede7add4eb3f5a92f5f8c9fd9cc8fdf6c659;hp=437b97cf9b1e2e7163907260e56b6d275694f111;hpb=97cfff753ccb7e5739ebe53e43a5af29dc0f6577;p=src%2Fapp-framework-binder.git diff --git a/src/rest-api.c b/src/rest-api.c index 437b97cf..398ba577 100644 --- a/src/rest-api.c +++ b/src/rest-api.c @@ -195,9 +195,12 @@ STATIC AFB_error callPluginApi(AFB_request *request, int plugidx, void *context) } } - // Effectively call the API with a subset of the context + // Effectively CALL PLUGIN API with a subset of the context jresp = plugin->apis[idx].callback(request, context); + // Store context in case it was updated by plugins + clientCtx->contexts[plugidx] = request->context; + // handle intermediary Post Iterates out of band if ((jresp == NULL) && (request->errcode == MHD_HTTP_OK)) return (AFB_SUCCESS); @@ -345,12 +348,7 @@ STATIC AFB_request *createRequest (struct MHD_Connection *connection, AFB_sessio request->prefix = strdup (baseurl); request->api = strdup (baseapi); request->plugins= session->plugins; - for (idx = 0; idx < session->config->pluginCount; idx++) { - if (!strcmp(baseurl, session->plugins[idx]->prefix)) { - request->plugin = session->plugins[idx]; - break; - } - } + // note request->handle is fed with request->context in ctxClientGet Done: free(urlcpy1); @@ -577,59 +575,106 @@ STATIC AFB_plugin ** RegisterJsonPlugins(AFB_plugin **plugins) { return (plugins); } -void initPlugins(AFB_session *session) { - static AFB_plugin **plugins; - AFB_plugin* (*pluginRegisterFct)(void); - void *plugin; - char *pluginPath; - struct dirent *pluginDir; +STATIC void scanDirectory(char *dirpath, int dirfd, AFB_plugin **plugins, int *count) { DIR *dir; - afbJsonType = json_object_new_string (AFB_MSG_JTYPE); - int num = 0; - - /* pre-allocate for 20 plugins, we will downsize if necessary */ - plugins = (AFB_plugin **) malloc (20*sizeof(AFB_plugin)); + void *libso; + struct dirent pluginDir, *result; + AFB_plugin* (*pluginRegisterFct)(void); + char pluginPath[255]; - if ((dir = opendir(session->config->plugins)) == NULL) { - fprintf(stderr, "Could not open plugin directory [%s], exiting...\n", session->config->plugins); - exit (-1); + // Open Directory to scan over it + dir = fdopendir (dirfd); + if (dir == NULL) { + fprintf(stderr, "ERROR in scanning directory\n"); + return; } + if (verbose) fprintf (stderr, "Scanning dir=[%s] for plugins\n", dirpath); + + for (;;) { + readdir_r(dir, &pluginDir, &result); + if (result == NULL) break; + + // Loop on any contained directory + if ((pluginDir.d_type == DT_DIR) && (pluginDir.d_name[0] != '.')) { + int fd = openat (dirfd, pluginDir.d_name, O_DIRECTORY); + char newpath[255]; + strncpy (newpath, dirpath, sizeof(newpath)); + strncat (newpath, "/", sizeof(newpath)); + strncat (newpath, pluginDir.d_name, sizeof(newpath)); + + scanDirectory (newpath, fd, plugins, count); + close (fd); - while ((pluginDir = readdir(dir)) != NULL) { + } else { - if (!strstr (pluginDir->d_name, ".so")) - continue; + // This is a file but not a plugin let's move to next directory element + if (!strstr (pluginDir.d_name, ".so")) continue; - asprintf (&pluginPath, "%s/%s", session->config->plugins, pluginDir->d_name); - plugin = dlopen (pluginPath, RTLD_NOW | RTLD_LOCAL); - pluginRegisterFct = dlsym (plugin, "pluginRegister"); - free (pluginPath); - if (!plugin) { - if (verbose) fprintf(stderr, "[%s] is not loadable, continuing...\n", pluginDir->d_name); - continue; - } else if (!pluginRegisterFct) { - if (verbose) fprintf(stderr, "[%s] is not an AFB plugin, continuing...\n", pluginDir->d_name); - continue; - } + // This is a loadable library let's check if it's a plugin + snprintf (pluginPath, sizeof(pluginPath), "%s/%s", dirpath, pluginDir.d_name); + libso = dlopen (pluginPath, RTLD_NOW | RTLD_LOCAL); - if (verbose) fprintf(stderr, "[%s] is a valid AFB plugin, loading it\n", pluginDir->d_name); - plugins[num] = (AFB_plugin *) malloc (sizeof(AFB_plugin)); - plugins[num] = (**pluginRegisterFct)(); - num++; - /* only 20 plugins are supported at that time */ - if (num == 20) break; - } - plugins = (AFB_plugin **) realloc (plugins, (num+1)*sizeof(AFB_plugin)); - plugins[num] = NULL; + // Load fail we ignore this .so file + if (!libso) { + fprintf(stderr, "[%s] is not loadable, continuing...\n", pluginDir.d_name); + continue; + } + + pluginRegisterFct = dlsym (libso, "pluginRegister"); + if (!pluginRegisterFct) { + fprintf(stderr, "[%s] is not an AFB plugin, continuing...\n", pluginDir.d_name); + continue; + } + + // if max plugin is reached let's stop searching + if (*count == AFB_MAX_PLUGINS) { + fprintf(stderr, "[%s] is not loaded [Max Count=%d reached]\n", *count); + continue; + } + + if (verbose) fprintf(stderr, "[%s] is a valid AFB plugin, loading pos[%d]\n", pluginDir.d_name, *count); + plugins[*count] = (AFB_plugin *) malloc (sizeof(AFB_plugin)); + plugins[*count] = (**pluginRegisterFct)(); + *count = *count +1; + + } + } closedir (dir); +} - if (plugins[0] == NULL) { +void initPlugins(AFB_session *session) { + static AFB_plugin **plugins; + + afbJsonType = json_object_new_string (AFB_MSG_JTYPE); + int count = 0; + char *dirpath; + int dirfd; + + /* pre-allocate for AFB_MAX_PLUGINS plugins, we will downsize later */ + plugins = (AFB_plugin **) malloc (AFB_MAX_PLUGINS *sizeof(AFB_plugin)); + + // Loop on every directory passed in --plugins=xxx + while (dirpath = strsep(&session->config->ldpaths, ":")) { + // Ignore any directory we fail to open + if ((dirfd = open(dirpath, O_DIRECTORY)) <= 0) { + fprintf(stderr, "Invalid directory path=[%s]\n", dirpath); + continue; + } + scanDirectory (dirpath, dirfd, plugins, &count); + close (dirfd); + } + + if (count == 0) { fprintf(stderr, "No plugins found, afb-daemon is unlikely to work in this configuration, exiting...\n"); exit (-1); } + + // downsize structure to effective number of loaded plugins + plugins = (AFB_plugin **)realloc (plugins, (count+1)*sizeof(AFB_plugin)); + plugins[count] = NULL; // complete plugins and save them within current sessions session->plugins = RegisterJsonPlugins(plugins); - session->config->pluginCount = num; + session->config->pluginCount = count; }