session->config->smack= cliconfig->smack;
}
- if (cliconfig->plugins == NULL) {
- session->config->plugins = PLUGIN_INSTALL_DIR;
+ if (cliconfig->ldpaths == NULL) {
+ session->config->ldpaths = PLUGIN_INSTALL_DIR;
} else {
- session->config->plugins= cliconfig->plugins;
+ session->config->ldpaths= cliconfig->ldpaths;
}
// if no session dir create a default path from rootdir
session->config->smack = strdup (json_object_get_string (value));
}
- if (!cliconfig->plugins && json_object_object_get_ex (AFBConfig, "plugins", &value)) {
- session->config->plugins = strdup (json_object_get_string (value));
+ if (!cliconfig->ldpaths && json_object_object_get_ex (AFBConfig, "plugins", &value)) {
+ session->config->ldpaths = strdup (json_object_get_string (value));
}
if (!cliconfig->setuid && json_object_object_get_ex (AFBConfig, "setuid", &value)) {
json_object_object_add (AFBConfig, "rootapi" , json_object_new_string (session->config->rootapi));
json_object_object_add (AFBConfig, "rootbase" , json_object_new_string (session->config->rootbase));
json_object_object_add (AFBConfig, "smack" , json_object_new_string (session->config->smack));
- json_object_object_add (AFBConfig, "plugins" , json_object_new_string (session->config->plugins));
+ json_object_object_add (AFBConfig, "plugins" , json_object_new_string (session->config->ldpaths));
json_object_object_add (AFBConfig, "sessiondir" , json_object_new_string (session->config->sessiondir));
json_object_object_add (AFBConfig, "pidfile" , json_object_new_string (session->config->pidfile));
json_object_object_add (AFBConfig, "setuid" , json_object_new_string (session->config->setuid));
#define SET_SMACK 140
#define SET_AUTH_TOKEN 141
- #define SET_PLUGINS 142
+ #define SET_LDPATH 142
#define SET_APITIMEOUT 143
#define SET_CNTXTIMEOUT 144
{SET_CONFIG_SAVE ,0,"save" , "Save config on disk [default no]"},
{SET_CONFIG_EXIT ,0,"saveonly" , "Save config on disk and then exit"},
- {SET_SMACK ,1,"smack" , "Set Smack Label [default demo]"},
- {SET_PLUGINS ,1,"plugins" , "Load Plugins from dir [default = PLUGIN_INSTALL_DIR"},
+ // {SET_SMACK ,1,"smack" , "Set Smack Label [default demo]"},
+ {SET_LDPATH ,1,"ldpaths" , "Load Plugins from dir1:dir2:... [default = PLUGIN_INSTALL_DIR"},
{SET_AUTH_TOKEN ,1,"token" , "Initial Secret [default=no-session, --token="" for session without authentication]"},
{DISPLAY_VERSION ,0,"version" , "Display version and copyright"},
fprintf (stderr," --%-15s %s\n", command, cliOptions[ind].help);
}
}
- fprintf (stderr,"Example:\n %s\\\n --verbose --port=1234 --smack=xxxx --token='azerty' --plugins=build/plugins\n", name);
+ fprintf (stderr,"Example:\n %s\\\n --verbose --port=1234 --token='azerty' --ldpaths=build/plugins:/usr/lib64/agl/plugins\n", name);
} // end printHelp
/*----------------------------------------------------------
cliconfig.token = optarg;
break;
- case SET_PLUGINS:
+ case SET_LDPATH:
if (optarg == 0) goto needValueForOption;
- cliconfig.plugins = optarg;
+ cliconfig.ldpaths = optarg;
break;
case SET_PID_FILE:
normalExit:
closeSession (session); // try to close everything before leaving
if (verbose) printf ("\n---- Application Framework Binder Normal End ------\n");
- exit (0);
+ exit(0);
// ------------- Fatal ERROR display error and quit -------------
errorSetuid:
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));
-
- if ((dir = opendir(session->config->plugins)) == NULL) {
- fprintf(stderr, "Could not open plugin directory [%s], exiting...\n", session->config->plugins);
- exit (-1);
+ void *libso;
+ struct dirent *pluginDir;
+ AFB_plugin* (*pluginRegisterFct)(void);
+ char pluginPath[255];
+
+ // 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);
while ((pluginDir = readdir(dir)) != NULL) {
+
+ // 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);
+
+ } else {
+
+ // This is a file but not a plugin let's move to next directory element
+ if (!strstr (pluginDir->d_name, ".so")) continue;
- if (!strstr (pluginDir->d_name, ".so"))
- 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);
- 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;
- }
+ // 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");
+ free (libso);
+
+ if (!pluginRegisterFct) {
+ fprintf(stderr, "[%s] is not an AFB plugin, continuing...\n", pluginDir->d_name);
+ continue;
+ }
- 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;
+ // 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);
+}
+
+void initPlugins(AFB_session *session) {
+ static AFB_plugin **plugins;
+
+ afbJsonType = json_object_new_string (AFB_MSG_JTYPE);
+ int count = 0;
+ char *dirpath;
+ int dirfd;
- if (plugins[0] == NULL) {
+ /* 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;
}