Пример #1
0
/*! Load a set of plugin objects from a directory and and call their init-function
 * @param[in]  h     Clicon handle
 * @param[in]  function Which function symbol to load and call (eg CLIXON_PLUGIN_INIT)
 * @param[in]  dir   Directory. .so files in this dir will be loaded.
 * @param[in]  regexp Regexp for matching files in plugin directory. Default *.so.
 * @retval     0     OK
 * @retval     -1    Error
 */
int
clixon_plugins_load(clicon_handle h,
    		    char         *function,
		    char         *dir,
    		    char         *regexp)
{
    int            retval = -1;
    int            ndp;
    struct dirent *dp = NULL;
    int            i;
    char           filename[MAXPATHLEN];
    clixon_plugin *cp;
    int            ret;

    clicon_debug(1, "%s", __FUNCTION__); 
    /* Get plugin objects names from plugin directory */
    if((ndp = clicon_file_dirent(dir, &dp, regexp?regexp:"(.so)$", S_IFREG)) < 0)
	goto done;
    /* Load all plugins */
    for (i = 0; i < ndp; i++) {
	snprintf(filename, MAXPATHLEN-1, "%s/%s", dir, dp[i].d_name);
	clicon_debug(1, "DEBUG: Loading plugin '%.*s' ...", 
		     (int)strlen(filename), filename);
	if ((ret = plugin_load_one(h, filename, function, RTLD_NOW, &cp)) < 0)
	    goto done;
	if (ret == 0)
	    continue;
	_clixon_nplugins++;
	if ((_clixon_plugins = realloc(_clixon_plugins, _clixon_nplugins*sizeof(clixon_plugin))) == NULL) {
	    clicon_err(OE_UNIX, errno, "realloc");
	    goto done;
	}
	_clixon_plugins[_clixon_nplugins-1] = *cp;
	free(cp);
    }
    retval = 0;
done:
    if (dp)
	free(dp);
    return retval;
}
Пример #2
0
/*
 * netconf_plugin_load
 * Load allplugins you can find in CLICON_NETCONF_DIR
 */
int 
netconf_plugin_load(clicon_handle h)
{
    int            retval = -1;
    char          *dir;
    int            ndp;
    struct dirent *dp;
    int            i;
    char          *filename;
    plghndl_t     *handle;

    if ((dir = clicon_netconf_dir(h)) == NULL)
	goto quit;

    /* Get plugin objects names from plugin directory */
    if((ndp = clicon_file_dirent(dir, &dp, "(.so)$", S_IFREG, __FUNCTION__))<0)
	goto quit;

    /* Load all plugins */
    for (i = 0; i < ndp; i++) {
	filename = chunk_sprintf(__FUNCTION__, "%s/%s", dir, dp[i].d_name);
	clicon_debug(1, "DEBUG: Loading plugin '%.*s' ...", 
		     (int)strlen(filename), filename);
	if (filename == NULL) {
	    clicon_err(OE_UNIX, errno, "chunk");
	    goto quit;
	}
	if ((handle = plugin_load (h, filename, RTLD_NOW, __FUNCTION__)) == NULL)
	    goto quit;
	if ((plugins = rechunk(plugins, (nplugins+1) * sizeof (*plugins), NULL)) == NULL) {
	    clicon_err(OE_UNIX, errno, "chunk");
	    goto quit;
	}
	plugins[nplugins++] = handle;
	unchunk (filename);
    }
    retval = 0;
quit:
    unchunk_group(__FUNCTION__);
    return retval;
}