コード例 #1
0
ファイル: gphoto2-port.c プロジェクト: axxel/libgphoto2
/**
 * \brief Configure a port
 *
 * Makes a port functional by passing in the necessary path
 * information (from the serial:/dev/ttyS0 or similar variables).
 * After calling this function, you can access the port using for
 * example gp_port_open().
 * 
 * \param port a GPPort
 * \param info the GPPortInfo to set
 *
 * \return a gphoto2 error code
 **/
int
gp_port_set_info (GPPort *port, GPPortInfo info)
{
	int ret;

	GPPortLibraryOperations ops_func;

	C_PARAMS (port);

	free (port->pc->info.name);
	C_MEM (port->pc->info.name = strdup (info->name));
	free (port->pc->info.path);
	C_MEM (port->pc->info.path = strdup (info->path));
	port->pc->info.type = info->type;
	free (port->pc->info.library_filename);
	C_MEM (port->pc->info.library_filename = strdup (info->library_filename));

	port->type = info->type;

	/* Clean up */
	if (port->pc->ops) {
		gp_port_exit (port);
		free (port->pc->ops);
		port->pc->ops = NULL;
	}
	if (port->pc->lh) {
#if !defined(VALGRIND)
		lt_dlclose (port->pc->lh);
		lt_dlexit ();
#endif
	}

	lt_dlinit ();
	port->pc->lh = lt_dlopenext (info->library_filename);
	if (!port->pc->lh) {
		GP_LOG_E ("Could not load '%s' ('%s').", info->library_filename, lt_dlerror ());
		lt_dlexit ();
		return (GP_ERROR_LIBRARY);
	}

	/* Load the operations */
	ops_func = lt_dlsym (port->pc->lh, "gp_port_library_operations");
	if (!ops_func) {
		GP_LOG_E ("Could not find 'gp_port_library_operations' in '%s' ('%s')",
			  info->library_filename, lt_dlerror ());
		lt_dlclose (port->pc->lh);
		lt_dlexit ();
		port->pc->lh = NULL;
		return (GP_ERROR_LIBRARY);
	}
	port->pc->ops = ops_func ();
	gp_port_init (port);

	/* Initialize the settings to some default ones */
	switch (info->type) {
	case GP_PORT_SERIAL:
		port->settings.serial.speed = 0;
		port->settings.serial.bits = 8;
		port->settings.serial.parity = 0;
		port->settings.serial.stopbits = 1;
		gp_port_set_timeout (port, 500);
		break;
	case GP_PORT_USB:
		if (sizeof (port->settings.usb.port) <= strlen(info->path)) {
			GP_LOG_E ("Path is too long for static buffer '%s'.", info->path);
			return GP_ERROR_LIBRARY;
		}
		strncpy (port->settings.usb.port, info->path,
			 sizeof (port->settings.usb.port));
		port->settings.usb.inep = -1;
		port->settings.usb.outep = -1;
		port->settings.usb.config = -1;
		port->settings.usb.interface = 0;
		port->settings.usb.altsetting = -1;
		gp_port_set_timeout (port, 5000);
		break;
	case GP_PORT_USB_DISK_DIRECT:
		snprintf(port->settings.usbdiskdirect.path,
			 sizeof(port->settings.usbdiskdirect.path), "%s",
			 strchr(info->path, ':') + 1);
		break;
	case GP_PORT_USB_SCSI:
		snprintf(port->settings.usbscsi.path,
			 sizeof(port->settings.usbscsi.path), "%s",
			 strchr(info->path, ':') + 1);
		break;
	default:
		/* Nothing in here */
		break;
	}
	ret = gp_port_set_settings (port, port->settings);
	if (ret != GP_ERROR_NOT_SUPPORTED)
		CHECK_RESULT (ret);

	return GP_OK;
}
コード例 #2
0
ファイル: modules.c プロジェクト: claude191/freeradius-server
/*
 *	Find a module on disk or in memory, and link to it.
 */
static module_entry_t *linkto_module(const char *module_name,
				     CONF_SECTION *cs)
{
	module_entry_t myentry;
	module_entry_t *node;
	lt_dlhandle handle = NULL;
	char module_struct[256];
	char *p;
	const module_t *module;

	strlcpy(myentry.name, module_name, sizeof(myentry.name));
	node = rbtree_finddata(module_tree, &myentry);
	if (node) return node;

	/*
	 *	Link to the module's rlm_FOO{} module structure.
	 *
	 *	The module_name variable has the version number
	 *	embedded in it, and we don't want that here.
	 */
	strcpy(module_struct, module_name);
	p = strrchr(module_struct, '-');
	if (p) *p = '\0';

#if defined(WITHOUT_LIBLTDL) && defined (WITH_DLOPEN) && defined(RTLD_SELF)
	module = lt_dlsym(RTLD_SELF, module_struct);
	if (module) goto open_self;
#endif

	/*
	 *	Keep the handle around so we can dlclose() it.
	 */
	handle = fr_dlopenext(module_name);
	if (handle == NULL) {
		cf_log_err(cf_sectiontoitem(cs),
			   "Failed to link to module '%s': %s\n",
			   module_name, lt_dlerror());
		return NULL;
	}

	DEBUG3("    (Loaded %s, checking if it's valid)", module_name);

	/*
	 *	libltld MAY core here, if the handle it gives us contains
	 *	garbage data.
	 */
	module = lt_dlsym(handle, module_struct);
	if (!module) {
		cf_log_err(cf_sectiontoitem(cs),
			   "Failed linking to %s structure: %s\n",
			   module_name, lt_dlerror());
		lt_dlclose(handle);
		return NULL;
	}

#if defined(WITHOUT_LIBLTDL) && defined (WITH_DLOPEN) && defined(RTLD_SELF)
 open_self:
#endif
	/*
	 *	Before doing anything else, check if it's sane.
	 */
	if (module->magic != RLM_MODULE_MAGIC_NUMBER) {
		lt_dlclose(handle);
		cf_log_err(cf_sectiontoitem(cs),
			   "Invalid version in module '%s'",
			   module_name);
		return NULL;

	}

	/* make room for the module type */
	node = rad_malloc(sizeof(*node));
	memset(node, 0, sizeof(*node));
	strlcpy(node->name, module_name, sizeof(node->name));
	node->module = module;
	node->handle = handle;

	cf_log_module(cs, "Linked to module %s", module_name);

	/*
	 *	Add the module as "rlm_foo-version" to the configuration
	 *	section.
	 */
	if (!rbtree_insert(module_tree, node)) {
		radlog(L_ERR, "Failed to cache module %s", module_name);
		lt_dlclose(handle);
		free(node);
		return NULL;
	}

	return node;
}
int oph_ioserver_cleanup(oph_ioserver_handler* handle)
{
  if (!handle){
    pmesg(LOG_ERROR, __FILE__, __LINE__, OPH_IOSERVER_LOG_NULL_HANDLE);
  	logging_server(LOG_ERROR, __FILE__, __LINE__, handle->server_type, OPH_IOSERVER_LOG_NULL_HANDLE);    
    return OPH_IOSERVER_NULL_HANDLE;
  }
  
  if (!handle->dlh || !handle->server_type){
  	pmesg(LOG_ERROR, __FILE__, __LINE__, OPH_IOSERVER_LOG_LOAD_SERV_ERROR);
  	logging_server(LOG_ERROR, __FILE__, __LINE__, handle->server_type, OPH_IOSERVER_LOG_LOAD_SERV_ERROR);    
    return OPH_IOSERVER_DLOPEN_ERR;
  }

  char func_name[OPH_IOSERVER_BUFLEN] = {'\0'};
  snprintf(func_name, OPH_IOSERVER_BUFLEN, OPH_IOSERVER_CLEANUP_FUNC, handle->server_type);

  if (!(_SERVER_cleanup = (int (*)(oph_ioserver_handler*)) lt_dlsym (handle->dlh, func_name))){
	  pmesg(LOG_ERROR, __FILE__, __LINE__, OPH_IOSERVER_LOG_LOAD_FUNC_ERROR, lt_dlerror());
  	logging_server(LOG_ERROR, __FILE__, __LINE__, handle->server_type, OPH_IOSERVER_LOG_LOAD_FUNC_ERROR, lt_dlerror());
    return OPH_IOSERVER_DLSYM_ERR;
  }    

  //Release operator resources
  int res;
  if((res = _SERVER_cleanup (handle))){
	  pmesg(LOG_ERROR, __FILE__, __LINE__, OPH_IOSERVER_LOG_RELEASE_RES_ERROR);
  	logging_server(LOG_ERROR, __FILE__, __LINE__, handle->server_type, OPH_IOSERVER_LOG_RELEASE_RES_ERROR);
    return res;
  }

  //Release handle resources
	if(handle->server_type){
		free(handle->server_type);
		handle->server_type = NULL;
	} 
	if(handle->server_subtype){
		free(handle->server_subtype);
		handle->server_subtype = NULL;
	} 
	if(handle->lib){
		free(handle->lib);
		handle->lib = NULL;
	} 

  if ((lt_dlclose(handle->dlh)))
  {
	  pmesg(LOG_ERROR, __FILE__, __LINE__, OPH_IOSERVER_LOG_DLCLOSE_ERROR, lt_dlerror());
  	logging_server(LOG_ERROR, __FILE__, __LINE__, handle->server_type, OPH_IOSERVER_LOG_DLCLOSE_ERROR, lt_dlerror());
	  return OPH_IOSERVER_DLCLOSE_ERR;
  }
  handle->dlh = NULL;

  if (lt_dlexit()){
  	pmesg(LOG_ERROR, __FILE__, __LINE__, OPH_IOSERVER_LOG_DLEXIT_ERROR, lt_dlerror());
  	logging_server(LOG_ERROR, __FILE__, __LINE__, handle->server_type, OPH_IOSERVER_LOG_DLEXIT_ERROR, lt_dlerror());
    return OPH_IOSERVER_DLEXIT_ERR;
  }

  free(handle);
  handle = NULL;

  return res;
}
コード例 #4
0
ファイル: aaa_module.c プロジェクト: BackupTheBerlios/disc
int load_module(char* name)
{
	int ret;
	lt_dlhandle handle;
	struct module_exports* e;
	char* error_msg;
	struct aaa_module* mod;
	struct aaa_module** m;
	
	ret=0;
	mod=0;
	handle=0;
	e=0;
	
	handle=lt_dlopenext(name);
	if (handle==0){
		LOG(L_CRIT, "ERROR: load_module: failed to load <%s> in <%s>: %s\n",
					name, MODULE_SEARCH_PATH, lt_dlerror());
		ret=-1;
		goto error;
	}
	/* get module struct */
	e=(struct module_exports*)lt_dlsym(handle, "exports");
	error_msg=(char*)lt_dlerror();
	if (e==0){
		LOG(L_CRIT, "ERROR: load_module: symbol not found <%s>: %s\n",
				name, error_msg);
		ret=-1;
		goto error;
	}
	/* sanity checks */
	if (e->mod_type!=AAA_CLIENT && e->mod_type!=AAA_SERVER) {
		LOG(L_CRIT, "ERROR: load_module: module \"%s\" has an unknown "
			"type (%d) - try AAA_SERVER or AAA_CLIENT\n",e->name, e->mod_type);
		ret=-1;
		goto error;
	}
	if (e->mod_type!=my_aaa_status) {
		LOG(L_CRIT, "ERROR: load_module: module \"%s\" has a different "
			"type (%d) then the core (%d)\n",e->name,e->app_id,my_aaa_status);
		ret=-1;
		goto error;
	}
	if (e->app_id==AAA_APP_RELAY) {
		LOG(L_CRIT, "ERROR: load_module: module \"%s\" advertises "
			"an app_id that's reserved %x (relay)\n",e->name, e->app_id);
		ret=-1;
		goto error;
	}
	if ((e->flags|DOES_AUTH|DOES_AUTH)==0) {
		LOG(L_CRIT, "ERROR: load_module: module \"%s\" does not support "
			"authentication or accounting for app_id %d\n",e->name, e->app_id);
		ret=-1;
		goto error;
	}
	if (e->mod_msg==0 ) {
		LOG(L_CRIT, "ERROR: load_module: module \"%s\" exports a NULL "
			"function for mod_msg\n",e->name);
		ret=-1;
	}
	/* link it in the module list - TODO*/
	mod=shm_malloc(sizeof(struct aaa_module));
	if (mod==0){
		LOG(L_CRIT, "ERROR: load_module: memory allocation failure\n");
		ret=-2;
		goto error;
	}
	mod->path=name;
	mod->handle=handle;
	mod->exports=e;
	mod->is_init=0;
	mod->next=0;
	for (m=&modules; *m; m=&(*m)->next){
		if ((*m)->handle==mod->handle){
			LOG(L_WARN, "WARNING: load_module: attempting to load the same"
					" module twice (%s)\n", mod->path);
			shm_free(mod);
			goto skip;
		}
		if (strcmp((*m)->exports->name, e->name)==0){
			LOG(L_CRIT, "ERROR: load_module: module name collision for %s\n",
					e->name);
			ret=-3;
			goto error;
		}
		if ((*m)->exports->app_id==e->app_id){
			LOG(L_CRIT, "ERROR: load_module: 2 modules with the same "
					"app_id(%u): %s, %s\n",
					e->app_id, (*m)->exports->name, e->name);
			ret=-4;
			goto error;
		}
	}
	*m=mod;
skip:
	return ret;
	
error:
	if(handle) lt_dlclose(handle);
	if(mod) shm_free(mod);
	return ret;
}
コード例 #5
0
ファイル: gphoto2-camera.c プロジェクト: JohnChu/Snoopy
/**
 * Initiate a connection to the \c camera. 
 *
 * @param camera a #Camera
 * @param context a #GPContext
 * @return a gphoto2 error code
 *
 * Before calling this function, the
 * \c camera should be set up using #gp_camera_set_port_path or
 * #gp_camera_set_port_name and #gp_camera_set_abilities. If that has been
 * omitted, gphoto2 tries to autodetect any cameras and chooses the first one
 * if any cameras are found. It is generally a good idea to call
 * #gp_camera_exit after transactions have been completed in order to give
 * other applications the chance to access the camera, too.
 *
 */
int
gp_camera_init (Camera *camera, GPContext *context)
{
	CameraAbilities a;
	const char *model, *port;
	CameraLibraryInitFunc init_func;
	int result;

	gp_log (GP_LOG_DEBUG, "gphoto2-camera", "Initializing camera...");

	CHECK_NULL (camera);
	/*
	 * Reset the exit_requested flag. If this flag is set, 
	 * gp_camera_exit will be called as soon as the camera is no
	 * longer in use (used flag).
	 */
	camera->pc->exit_requested = 0;

	/*
	 * If the model hasn't been indicated, try to
	 * figure it out (USB only). Beware of "Directory Browse".
	 */
	if (strcasecmp (camera->pc->a.model, "Directory Browse") &&
	    !strcmp ("", camera->pc->a.model)) {
		CameraAbilitiesList *al;
		GPPortInfo	pinfo;
		GPPortInfoList	*il;
		int		m, p;
		GPPortInfo	info;
        	CameraList	*list;

		result = gp_list_new (&list);
		if (result < GP_OK)
			return result;

		result = gp_port_get_info (camera->port, &pinfo);
		if (result < GP_OK)
			return result;

		gp_log (GP_LOG_DEBUG, "gphoto2-camera", "pinfo.type %d", pinfo.type);
		gp_log (GP_LOG_DEBUG, "gphoto2-camera", "pinfo.path %s", pinfo.path);
		gp_log (GP_LOG_DEBUG, "gphoto2-camera", "pinfo.name %s", pinfo.name);
		gp_log (GP_LOG_DEBUG, "gphoto2-camera", "Neither "
			"port nor model set. Trying auto-detection...");

		/* Call auto-detect and choose the first camera */
		gp_abilities_list_new (&al);
		gp_abilities_list_load (al, context);
		gp_port_info_list_new (&il);
		gp_port_info_list_load (il);
		gp_abilities_list_detect (al, il, list, context);
		if (!gp_list_count (list)) {
			gp_abilities_list_free (al);
			gp_port_info_list_free (il);
			gp_context_error (context, _("Could not detect any camera"));
			gp_list_free (list);
			return (GP_ERROR_MODEL_NOT_FOUND);
		}
		p = 0;
		/* if the port was set before, then use that entry. but not if it is "usb:" */
		if (	(pinfo.type == GP_PORT_USB) &&
			strlen(pinfo.path) &&
			strcmp(pinfo.path,"usb:")
		) {
			for (p = gp_list_count (list);p--;) {
				const char *xp;

				gp_list_get_value (list, p, &xp);
				if (!strcmp (xp, pinfo.path))
					break;
			}
			if (p<0) {
				gp_context_error (context, _("Could not detect any camera at port %s"), pinfo.path);
				return (GP_ERROR_FILE_NOT_FOUND);
			}
		}

		gp_list_get_name  (list, p, &model);
		m = gp_abilities_list_lookup_model (al, model);
		gp_abilities_list_get_abilities (al, m, &a);
		gp_abilities_list_free (al);
		CRSL (camera, gp_camera_set_abilities (camera, a), context, list);
		CRSL (camera, gp_list_get_value (list, p, &port), context, list);
		p = gp_port_info_list_lookup_path (il, port);
		gp_port_info_list_get_info (il, p, &info);
		gp_port_info_list_free (il);
		CRSL (camera, gp_camera_set_port_info (camera, info), context, list);
		gp_list_free (list);
	}

	if (strcasecmp (camera->pc->a.model, "Directory Browse")) {
		switch (camera->port->type) {
		case GP_PORT_NONE:
			gp_context_error (context, _("You have to set the "
				"port prior to initialization of the camera."));
			return (GP_ERROR_UNKNOWN_PORT);
		case GP_PORT_USB:
			if (gp_port_usb_find_device (camera->port,
					camera->pc->a.usb_vendor,
					camera->pc->a.usb_product) != GP_OK) {
				CRS (camera, gp_port_usb_find_device_by_class
					(camera->port,
					camera->pc->a.usb_class,
					camera->pc->a.usb_subclass,
					camera->pc->a.usb_protocol), context);
					}
			break;
		default:
			break;
		}
	}

	/* Load the library. */
	gp_log (GP_LOG_DEBUG, "gphoto2-camera", "Loading '%s'...",
		camera->pc->a.library);
	lt_dlinit ();
	camera->pc->lh = lt_dlopenext (camera->pc->a.library);
	if (!camera->pc->lh) {
		gp_context_error (context, _("Could not load required "
			"camera driver '%s' (%s)."), camera->pc->a.library,
			lt_dlerror ());
		lt_dlexit ();
		return (GP_ERROR_LIBRARY);
	}

	/* Initialize the camera */
	init_func = lt_dlsym (camera->pc->lh, "camera_init");
	if (!init_func) {
		lt_dlclose (camera->pc->lh);
		lt_dlexit ();
		camera->pc->lh = NULL;
		gp_context_error (context, _("Camera driver '%s' is "
			"missing the 'camera_init' function."), 
			camera->pc->a.library);
		return (GP_ERROR_LIBRARY);
	}

	if (strcasecmp (camera->pc->a.model, "Directory Browse")) {
		result = gp_port_open (camera->port);
		if (result < 0) {
			lt_dlclose (camera->pc->lh);
			lt_dlexit ();
			camera->pc->lh = NULL;
			return (result);
		}
	}

	result = init_func (camera, context);
	if (result < 0) {
		gp_port_close (camera->port);
		lt_dlclose (camera->pc->lh);
		lt_dlexit ();
		camera->pc->lh = NULL;
		memset (camera->functions, 0, sizeof (CameraFunctions));
		return (result);
	}

	/* We don't care if that goes wrong */
#ifdef HAVE_MULTI
	gp_port_close (camera->port);
#endif

	return (GP_OK);
}
コード例 #6
0
ファイル: open.c プロジェクト: CCI/cci
/*
 * Open plugins for a given framework
 */
int cci_plugins_open_all(const char *framework,
			 cci_plugins_framework_verify_fn_t verify,
			 struct cci_plugin_handle ** plugins)
{
	int i, j;
	size_t prefix_len;
	char *ptr;
	cci_plugin_t *plugin;
	lt_dlhandle handle;
	char prefix[BUFSIZ];
	char *ctpenv;
	int ctpenv_negate;

	if (CCI_SUCCESS != (i = cci_plugins_init())) {
		return i;
	}

	ctpenv = getenv("CCI_CTP");
	if (ctpenv && ctpenv[0] == '^') {
		ctpenv_negate = 1;
		ctpenv++;
		debug(CCI_DB_INFO, "ignoring CTP list: %s", ctpenv);
	} else {
		ctpenv_negate = 0;
		debug(CCI_DB_INFO, "only keeping CTP list: %s", ctpenv);
	}

	snprintf(prefix, BUFSIZ - 1, "%s%s_", plugin_prefix, framework);
	prefix[BUFSIZ - 1] = '\0';
	prefix_len = strlen(prefix);

	for (i = 0; NULL != cci_plugins_filename_cache &&
	     NULL != cci_plugins_filename_cache[i]; ++i);
	*plugins = calloc(i + 1, sizeof(**plugins));
	if (NULL == *plugins)
		return CCI_ENOMEM;

	for (i = 0, j = 0;
	     NULL != cci_plugins_filename_cache &&
	     NULL != cci_plugins_filename_cache[i]; ++i) {
		/* Find the basename */
		if ((ptr = strrchr(cci_plugins_filename_cache[i], '/')) == NULL) {
			ptr = cci_plugins_filename_cache[i];
		} else {
			++ptr;
		}

		/* Is this a possible plugin? */
		if (strncasecmp(ptr, prefix, prefix_len) == 0) {
			if (open_plugin
			    (cci_plugins_filename_cache[i], &handle, &plugin,
			     verify) == CCI_SUCCESS) {
				if (ctpenv) {
					/* see if the ctp name is in ctpenv */
					int namelen = strlen(ptr + prefix_len);
					char *ctpenv_tmp = ctpenv;
					int found = 0;
					while (1) {
						if (!strncmp(ctpenv_tmp, ptr+prefix_len, namelen)
						    && (ctpenv_tmp[namelen] == ','
							|| ctpenv_tmp[namelen] == '\0')) {
							found = 1;
							break;
						}
						if (ctpenv_tmp[namelen] == '\0')
							break;
						ctpenv_tmp++;
					}
					/* filter */
					if (ctpenv_negate == found) {
						debug(CCI_DB_INFO, "ignoring CTP %s", ptr+prefix_len);
						continue;
					}
				}

				if (NULL != plugin->post_load &&
				    CCI_SUCCESS !=
				    plugin->post_load(plugin)) {
					fprintf(stderr,
						"Post load hook for %s failed -- ignored\n",
						ptr);
					lt_dlclose(handle);
					continue;
				}

				/* Post load was happy; this is a keeper */
				(*plugins)[j].plugin = plugin;
				(*plugins)[j].handle = handle;
				j++;
			}
		}
	}

	qsort(*plugins, j, sizeof(**plugins), cci_plugin_priority_compare);

	if (NULL == (*plugins)[0].plugin) {
		fprintf(stderr, "Unable to find suitable CCI plugin\n");
		free(*plugins);
		return CCI_ERROR;
	}

	return CCI_SUCCESS;
}
コード例 #7
0
/**
 * \brief Configure a port
 *
 * Makes a port functional by passing in the necessary path
 * information (from the serial:/dev/ttyS0 or similar variables).
 * After calling this function, you can access the port using for
 * example gp_port_open().
 * 
 * \param port a GPPort
 * \param info the GPPortInfo to set
 *
 * \return a gphoto2 error code
 **/
int
gp_port_set_info (GPPort *port, GPPortInfo info)
{
	GPPortLibraryOperations ops_func;

	CHECK_NULL (port);

	if (port->pc->info.name) free (port->pc->info.name);
	port->pc->info.name = strdup (info->name);
	if (port->pc->info.path) free (port->pc->info.path);
	port->pc->info.path = strdup (info->path);
	port->pc->info.type = info->type;
	if (port->pc->info.library_filename) free (port->pc->info.library_filename);
	port->pc->info.library_filename = strdup (info->library_filename);

	port->type = info->type;

	/* Clean up */
	if (port->pc->ops) {
		gp_port_exit (port);
		free (port->pc->ops);
		port->pc->ops = NULL;
	}
	if (port->pc->lh) {
		lt_dlclose (port->pc->lh);
		lt_dlexit ();
	}

	lt_dlinit ();
	port->pc->lh = lt_dlopenext (info->library_filename);
	if (!port->pc->lh) {
		gp_log (GP_LOG_ERROR, "gphoto2-port", _("Could not load "
			"'%s' ('%s')."), info->library_filename,
			lt_dlerror ());
		lt_dlexit ();
		return (GP_ERROR_LIBRARY);
	}

	/* Load the operations */
	ops_func = lt_dlsym (port->pc->lh, "gp_port_library_operations");
	if (!ops_func) {
		gp_log (GP_LOG_ERROR, "gphoto2-port", _("Could not find "
			"'gp_port_library_operations' in '%s' ('%s')"),
			info->library_filename, lt_dlerror ());
		lt_dlclose (port->pc->lh);
		lt_dlexit ();
		port->pc->lh = NULL;
		return (GP_ERROR_LIBRARY);
	}
	port->pc->ops = ops_func ();
	gp_port_init (port);

	/* Initialize the settings to some default ones */
	switch (info->type) {
	case GP_PORT_SERIAL:
		port->settings.serial.speed = 0;
		port->settings.serial.bits = 8;
		port->settings.serial.parity = 0;
		port->settings.serial.stopbits = 1;
		gp_port_set_timeout (port, 500);
		break;
	case GP_PORT_USB:
		strncpy (port->settings.usb.port, info->path,
			 sizeof (port->settings.usb.port));
		port->settings.usb.inep = -1;
		port->settings.usb.outep = -1;
		port->settings.usb.config = -1;
		port->settings.usb.interface = 0;
		port->settings.usb.altsetting = -1;
		gp_port_set_timeout (port, 5000);
		break;
	case GP_PORT_USB_DISK_DIRECT:
		snprintf(port->settings.usbdiskdirect.path,
			 sizeof(port->settings.usbdiskdirect.path), "%s",
			 strchr(info->path, ':') + 1);
		break;
	case GP_PORT_USB_SCSI:
		snprintf(port->settings.usbscsi.path,
			 sizeof(port->settings.usbscsi.path), "%s",
			 strchr(info->path, ':') + 1);
		break;
	default:
		/* Nothing in here */
		break;
	}
	gp_port_set_settings (port, port->settings);

	return (GP_OK);
}
コード例 #8
0
static int
foreach_func (const char *filename, lt_ptr data)
{
	GPPortInfoList *list = data;
	lt_dlhandle lh;
	GPPortLibraryType lib_type;
	GPPortLibraryList lib_list;
	GPPortType type;
	unsigned int j, old_size = list->count;
	int result;

	gp_log (GP_LOG_DEBUG, "gphoto2-port-info-list",
		_("Called for filename '%s'."), filename );

	lh = lt_dlopenext (filename);
	if (!lh) {
		gp_log (GP_LOG_DEBUG, "gphoto2-port-info-list",
			_("Could not load '%s': '%s'."), filename, lt_dlerror ());
		return (0);
	}

	lib_type = lt_dlsym (lh, "gp_port_library_type");
	lib_list = lt_dlsym (lh, "gp_port_library_list");
	if (!lib_type || !lib_list) {
		gp_log (GP_LOG_DEBUG, "gphoto2-port-info-list",
			_("Could not find some functions in '%s': '%s'."),
			filename, lt_dlerror ());
		lt_dlclose (lh);
		return (0);
	}

	type = lib_type ();
	for (j = 0; j < list->count; j++)
		if (list->info[j].type == type)
			break;
	if (j != list->count) {
		gp_log (GP_LOG_DEBUG, "gphoto2-port-info-list",
			_("'%s' already loaded"), filename);
		lt_dlclose (lh);
		return (0);
	}

	result = lib_list (list);
	lt_dlclose (lh);
	if (result < 0) {
		gp_log (GP_LOG_DEBUG, "gphoto2-port-info-list",
			_("Could not load port driver list: '%s'."),
			gp_port_result_as_string (result));
		return (0);
	}

	for (j = old_size; j < list->count; j++){
		gp_log (GP_LOG_DEBUG, "gphoto2-port-info-list",
			_("Loaded '%s' ('%s') from '%s'."),
			list->info[j].name, list->info[j].path,
			filename);
		strcpy (list->info[j].library_filename, filename);
	}

	return (0);
}
コード例 #9
0
int module_load(const char* file_name, int argc, char *argv[])
{
	module_loaded_t *module;
	const char *error;
	int rc;
	MODULE_INIT_FN initialize;
#ifdef HAVE_EBCDIC
#define	file	ebuf
#else
#define	file	file_name
#endif

	module = module_handle( file_name );
	if ( module ) {
		Debug( LDAP_DEBUG_ANY, "module_load: (%s) already loaded\n",
			file_name, 0, 0 );
		return -1;
	}

	/* If loading a backend, see if we already have it */
	if ( !strncasecmp( file_name, "back_", 5 )) {
		char *name = (char *)file_name + 5;
		char *dot = strchr( name, '.');
		if (dot) *dot = '\0';
		rc = backend_info( name ) != NULL;
		if (dot) *dot = '.';
		if ( rc ) {
			Debug( LDAP_DEBUG_CONFIG, "module_load: (%s) already present (static)\n",
				file_name, 0, 0 );
			return 0;
		}
	} else {
		/* check for overlays too */
		char *dot = strchr( file_name, '.' );
		if ( dot ) *dot = '\0';
		rc = overlay_find( file_name ) != NULL;
		if ( dot ) *dot = '.';
		if ( rc ) {
			Debug( LDAP_DEBUG_CONFIG, "module_load: (%s) already present (static)\n",
				file_name, 0, 0 );
			return 0;
		}
	}

	module = (module_loaded_t *)ch_calloc(1, sizeof(module_loaded_t) +
		strlen(file_name));
	if (module == NULL) {
		Debug(LDAP_DEBUG_ANY, "module_load failed: (%s) out of memory\n", file_name,
			0, 0);

		return -1;
	}
	strcpy( module->name, file_name );

#ifdef HAVE_EBCDIC
	strcpy( file, file_name );
	__atoe( file );
#endif
	/*
	 * The result of lt_dlerror(), when called, must be cached prior
	 * to calling Debug. This is because Debug is a macro that expands
	 * into multiple function calls.
	 */
	if ((module->lib = lt_dlopenext(file)) == NULL) {
		error = lt_dlerror();
#ifdef HAVE_EBCDIC
		strcpy( ebuf, error );
		__etoa( ebuf );
		error = ebuf;
#endif
		Debug(LDAP_DEBUG_ANY, "lt_dlopenext failed: (%s) %s\n", file_name,
			error, 0);

		ch_free(module);
		return -1;
	}

	Debug(LDAP_DEBUG_CONFIG, "loaded module %s\n", file_name, 0, 0);

   
#ifdef HAVE_EBCDIC
#pragma convlit(suspend)
#endif
	if ((initialize = lt_dlsym(module->lib, "init_module")) == NULL) {
#ifdef HAVE_EBCDIC
#pragma convlit(resume)
#endif
		Debug(LDAP_DEBUG_CONFIG, "module %s: no init_module() function found\n",
			file_name, 0, 0);

		lt_dlclose(module->lib);
		ch_free(module);
		return -1;
	}

	/* The imported init_module() routine passes back the type of
	 * module (i.e., which part of slapd it should be hooked into)
	 * or -1 for error.  If it passes back 0, then you get the 
	 * old behavior (i.e., the library is loaded and not hooked
	 * into anything).
	 *
	 * It might be better if the conf file could specify the type
	 * of module.  That way, a single module could support multiple
	 * type of hooks. This could be done by using something like:
	 *
	 *    moduleload extension /usr/local/openldap/whatever.so
	 *
	 * then we'd search through module_regtable for a matching
	 * module type, and hook in there.
	 */
	rc = initialize(argc, argv);
	if (rc == -1) {
		Debug(LDAP_DEBUG_CONFIG, "module %s: init_module() failed\n",
			file_name, 0, 0);

		lt_dlclose(module->lib);
		ch_free(module);
		return rc;
	}

	if (rc >= (int)(sizeof(module_regtable) / sizeof(struct module_regtable_t))
		|| module_regtable[rc].proc == NULL)
	{
		Debug(LDAP_DEBUG_CONFIG, "module %s: unknown registration type (%d)\n",
			file_name, rc, 0);

		module_int_unload(module);
		return -1;
	}

	rc = (module_regtable[rc].proc)(module, file_name);
	if (rc != 0) {
		Debug(LDAP_DEBUG_CONFIG, "module %s: %s module could not be registered\n",
			file_name, module_regtable[rc].type, 0);

		module_int_unload(module);
		return rc;
	}

	module->next = module_list;
	module_list = module;

	Debug(LDAP_DEBUG_CONFIG, "module %s: %s module registered\n",
		file_name, module_regtable[rc].type, 0);

	return 0;
}
コード例 #10
0
ファイル: decoder.c プロジェクト: dpbriggs/mocp
static int lt_load_plugin (const char *file, lt_ptr debug_info_ptr)
{
    int debug_info;
    const char *name;
    plugin_init_func init_func;

    debug_info = *(int *)debug_info_ptr;
    name = strrchr (file, '/');
    name = name ? (name + 1) : file;
    if (debug_info)
        printf ("Loading plugin %s...\n", name);

    if (plugins_num == PLUGINS_NUM) {
        fprintf (stderr, "Can't load plugin, because maximum number "
                 "of plugins reached!\n");
        return 0;
    }

    plugins[plugins_num].handle = lt_dlopenext (file);
    if (!plugins[plugins_num].handle) {
        fprintf (stderr, "Can't load plugin %s: %s\n", name, lt_dlerror ());
        return 0;
    }

    if (present_handle (plugins[plugins_num].handle)) {
        if (debug_info)
            printf ("Already loaded\n");
        if (lt_dlclose (plugins[plugins_num].handle))
            fprintf (stderr, "Error unloading plugin: %s\n", lt_dlerror ());
        return 0;
    }

    init_func = lt_dlsym (plugins[plugins_num].handle, "plugin_init");
    if (!init_func) {
        fprintf (stderr, "No init function in the plugin!\n");
        if (lt_dlclose (plugins[plugins_num].handle))
            fprintf (stderr, "Error unloading plugin: %s\n", lt_dlerror ());
        return 0;
    }

    plugins[plugins_num].decoder = init_func ();
    if (!plugins[plugins_num].decoder) {
        fprintf (stderr, "NULL decoder!\n");
        if (lt_dlclose (plugins[plugins_num].handle))
            fprintf (stderr, "Error unloading plugin: %s\n", lt_dlerror ());
        return 0;
    }

    if (plugins[plugins_num].decoder->api_version != DECODER_API_VERSION) {
        fprintf (stderr, "Plugin uses different API version\n");
        if (lt_dlclose (plugins[plugins_num].handle))
            fprintf (stderr, "Error unloading plugin: %s\n", lt_dlerror ());
        return 0;
    }

    plugins[plugins_num].name = extract_decoder_name (name);

    /* Is the Vorbis decoder using Tremor? */
    if (!strcmp (plugins[plugins_num].name, "vorbis")) {
        bool (*vorbis_is_tremor)();

        vorbis_is_tremor = lt_dlsym (plugins[plugins_num].handle,
                                     "vorbis_is_tremor");
        if (vorbis_is_tremor)
            have_tremor = vorbis_is_tremor ();
    }

    debug ("Loaded %s decoder", plugins[plugins_num].name);

    if (plugins[plugins_num].decoder->init)
        plugins[plugins_num].decoder->init ();
    plugins_num += 1;

    if (debug_info)
        printf ("OK\n");

    return 0;
}
コード例 #11
0
ファイル: mod_dso.c プロジェクト: Distrotech/proftpd
static int dso_load_module(char *name) {
  int res;
  char *symbol_name, *path, *tmp;
  module *m;
  lt_ptr mh = NULL;
  lt_dladvise advise;

  if (name == NULL) {
    errno = EINVAL;
    return -1;
  }

  if (strncmp(name, "mod_", 4) != 0 ||
      name[strlen(name)-2] != '.' ||
      name[strlen(name)-1] != 'c') {
    errno = EINVAL;
    return -1;
  }

  pr_log_debug(DEBUG7, "loading '%s'", name);

  tmp = strrchr(name, '.');
  if (tmp == NULL) {
    errno = EINVAL;
    return -1;
  }

  if (lt_dladvise_init(&advise) < 0) {
    pr_log_pri(PR_LOG_NOTICE, MOD_DSO_VERSION
      ": unable to initialise advise: %s", lt_dlerror());
    errno = EPERM;
    return -1;
  }

  if (lt_dladvise_ext(&advise) < 0) {
    pr_log_pri(PR_LOG_NOTICE, MOD_DSO_VERSION
      ": unable to setting 'ext' advise hint: %s", lt_dlerror());
    lt_dladvise_destroy(&advise);
    errno = EPERM;
    return -1;
  }

  if (lt_dladvise_global(&advise) < 0) {
    pr_log_pri(PR_LOG_NOTICE, MOD_DSO_VERSION
      ": unable to setting 'global' advise hint: %s", lt_dlerror());
    lt_dladvise_destroy(&advise);
    errno = EPERM;
    return -1;
  }

  *tmp = '\0';

  /* Load file: $prefix/libexec/<module> */
  path = pdircat(dso_pool, dso_module_path, name, NULL);

  pr_trace_msg(trace_channel, 5, "loading module '%s'", path);

  mh = lt_dlopenadvise(path, advise);
  if (mh == NULL) {
    *tmp = '.';

    pr_log_debug(DEBUG3, MOD_DSO_VERSION ": unable to dlopen '%s': %s (%s)",
      name, lt_dlerror(), strerror(errno));
    pr_log_debug(DEBUG3, MOD_DSO_VERSION
      ": defaulting to 'self' for symbol resolution");

    lt_dladvise_destroy(&advise);

    mh = lt_dlopen(NULL);
    if (mh == NULL) {
      pr_log_debug(DEBUG0, MOD_DSO_VERSION ": error loading 'self': %s",
        lt_dlerror());

      if (errno == ENOENT) {
        pr_log_pri(PR_LOG_NOTICE, MOD_DSO_VERSION
          ": check to see if '%s.la' exists", path);
      }

      return -1;
    }
  }

  lt_dladvise_destroy(&advise);

  /* Tease name of the module structure out of the given name:
   *  <module>.<ext> --> <module>_module
   */

  *tmp = '\0';
  symbol_name = pstrcat(dso_pool, name+4, "_module", NULL);

  /* Lookup module structure symbol by name. */

  pr_trace_msg(trace_channel, 7, "looking for symbol '%s' in loaded module",
    symbol_name);

  m = (module *) lt_dlsym(mh, symbol_name);
  if (m == NULL) {
    *tmp = '.';
    pr_log_debug(DEBUG1, MOD_DSO_VERSION
      ": unable to find module symbol '%s' in '%s'", symbol_name,
        mh ? name : "self");
    pr_trace_msg(trace_channel, 1, "unable to find module symbol '%s' in '%s'",
      symbol_name, mh ? name : "self");

    lt_dlclose(mh);
    mh = NULL;

    if (errno == ENOENT) {
      pr_log_pri(PR_LOG_NOTICE,
        MOD_DSO_VERSION ": check to see if '%s.la' exists", path);
    }

    return -1;
  }
  *tmp = '.';

  m->handle = mh;

  /* Add the module to the core structures */
  res = pr_module_load(m);
  if (res < 0) {
    if (errno == EEXIST) {
      pr_log_pri(PR_LOG_INFO, MOD_DSO_VERSION
        ": module 'mod_%s.c' already loaded", m->name);
      pr_trace_msg(trace_channel, 1, "module 'mod_%s.c' already loaded",
        m->name);

    } else if (errno == EACCES) {
      pr_log_pri(PR_LOG_ERR, MOD_DSO_VERSION
        ": module 'mod_%s.c' has wrong API version (0x%x), must be 0x%x",
        m->name, m->api_version, PR_MODULE_API_VERSION);
      pr_trace_msg(trace_channel, 1,
        "module 'mod_%s.c' has wrong API version (0x%x), must be 0x%x",
        m->name, m->api_version, PR_MODULE_API_VERSION);

    } else if (errno == EPERM) {
      pr_log_pri(PR_LOG_ERR, MOD_DSO_VERSION
        ": module 'mod_%s.c' failed to initialize", m->name);
      pr_trace_msg(trace_channel, 1, "module 'mod_%s.c' failed to initialize",
        m->name);
    }

    lt_dlclose(mh);
    mh = NULL;
    return -1;
  }

  pr_trace_msg(trace_channel, 8, "module '%s' successfully loaded", path);
  return 0;
}
コード例 #12
0
/*
 * Open a component, chasing down its dependencies first, if possible.
 */
static int open_component(component_file_item_t *target_file, 
                       opal_list_t *found_components)
{
  int show_errors, param;
  lt_dlhandle component_handle;
  mca_base_component_t *component_struct;
  char *struct_name, *err;
  opal_list_t dependencies;
  opal_list_item_t *cur;
  mca_base_component_list_item_t *mitem;
  dependency_item_t *ditem;
  size_t len;

  opal_output_verbose(40, 0, "mca: base: component_find: examining dyanmic %s MCA component \"%s\"",
                     target_file->type, target_file->name, NULL);
  opal_output_verbose(40, 0, "mca: base: component_find: %s", target_file->filename, NULL);
  param = mca_base_param_find("mca", NULL, "component_show_load_errors");
  mca_base_param_lookup_int(param, &show_errors);

  /* Was this component already loaded (e.g., via dependency)? */

  if (LOADED == target_file->status) {
    opal_output_verbose(40, 0, "mca: base: component_find: already loaded (ignored)", NULL);
    return OPAL_SUCCESS;
  }

  /* Ensure that this component is not already loaded (should only happen
     if it was statically loaded).  It's an error if it's already
     loaded because we're evaluating this file -- not this component.
     Hence, returning OPAL_ERR_PARAM indicates that the *file* failed
     to load, not the component. */

  for (cur = opal_list_get_first(found_components); 
       opal_list_get_end(found_components) != cur;
       cur = opal_list_get_next(cur)) {
    mitem = (mca_base_component_list_item_t *) cur;
    if (0 == strcmp(mitem->cli_component->mca_type_name, target_file->type) &&
        0 == strcmp(mitem->cli_component->mca_component_name, target_file->name)) {
      opal_output_verbose(40, 0, "mca: base: component_find: already loaded (ignored)", NULL);
      target_file->status = FAILED_TO_LOAD;
      return OPAL_ERR_BAD_PARAM;
    }
  }

  /* Look at see if this component has any dependencies.  If so, load
     them.  If we can't load them, then this component must also fail to
     load. */

  OBJ_CONSTRUCT(&dependencies, opal_list_t);
  if (0 != check_ompi_info(target_file, &dependencies, found_components)) {
    target_file->status = FAILED_TO_LOAD;
    free_dependency_list(&dependencies);
    return OPAL_ERR_OUT_OF_RESOURCE;
  }

  /* Now try to load the component */

  component_handle = lt_dlopenext(target_file->filename);
  if (NULL == component_handle) {
    err = strdup(lt_dlerror());
    if (0 != show_errors) {
        opal_output(0, "mca: base: component_find: unable to open %s %s: %s (ignored)", 
                    target_file->type, target_file->name, err);
    }
    opal_output_verbose(40, 0, "mca: base: component_find: unable to open %s: %s (ignored)", 
                        target_file->filename, err, NULL);
    free(err);
    target_file->status = FAILED_TO_LOAD;
    free_dependency_list(&dependencies);
    return OPAL_ERR_BAD_PARAM;
  }

  /* Successfully opened the component; now find the public struct.
     Malloc out enough space for it. */

  len = strlen(target_file->type) + strlen(target_file->name) + 32;
  struct_name = (char*)malloc(len);
  if (NULL == struct_name) {
    lt_dlclose(component_handle);
    target_file->status = FAILED_TO_LOAD;
    free_dependency_list(&dependencies);
    return OPAL_ERR_OUT_OF_RESOURCE;
  }
  snprintf(struct_name, len, "mca_%s_%s_component", target_file->type,
           target_file->name);

  mitem = OBJ_NEW(mca_base_component_list_item_t);
  if (NULL == mitem) {
    free(struct_name);
    lt_dlclose(component_handle);
    target_file->status = FAILED_TO_LOAD;
    free_dependency_list(&dependencies);
    return OPAL_ERR_OUT_OF_RESOURCE;
  }

  component_struct = (mca_base_component_t*)lt_dlsym(component_handle, struct_name);
  if (NULL == component_struct) {
    if (0 != show_errors) {
        opal_output(0, "mca: base: component_find: \"%s\" does not appear to be a valid "
                       "%s MCA dynamic component (ignored)", 
                       target_file->basename, target_file->type, NULL);
    }
    opal_output_verbose(40, 0, "mca: base: component_find: \"%s\" does not appear to be a valid "
                       "%s MCA dynamic component (ignored)", 
                       target_file->basename, target_file->type, NULL);
    free(mitem);
    free(struct_name);
    lt_dlclose(component_handle);
    target_file->status = FAILED_TO_LOAD;
    free_dependency_list(&dependencies);
    return OPAL_ERR_BAD_PARAM;
  }

  /* We found the public struct.  Save it, and register this component to
     be closed later. */

  mitem->cli_component = component_struct;
  opal_list_append(found_components, (opal_list_item_t *) mitem);
  mca_base_component_repository_retain(target_file->type, component_handle, 
                                    component_struct);

  /* Now that that's all done, link all the dependencies in to this
     component's repository entry */

  for (cur = opal_list_remove_first(&dependencies);
       NULL != cur;
       cur = opal_list_remove_first(&dependencies)) {
    ditem = (dependency_item_t *) cur;
    mca_base_component_repository_link(target_file->type,
                                       target_file->name,
                                       ditem->di_component_file_item->type,
                                       ditem->di_component_file_item->name);
    OBJ_RELEASE(ditem);
  }
  OBJ_DESTRUCT(&dependencies);

  opal_output_verbose(40, 0, "mca: base: component_find: opened dynamic %s MCA component \"%s\"",
                     target_file->type, target_file->name, NULL);
  target_file->status = LOADED;
    
  /* All done */
    
  free(struct_name);
  return OPAL_SUCCESS;
}
コード例 #13
0
static int do_test(void)
{
    FILE *fp;
    char filename[] = "./libompi_dbg_msgq";
    char full_filename[] = "./libompi_dbg_msgq.la";
    char line[1024];
    int happy;
    lt_dlhandle dlhandle;

#if OPAL_HAVE_LTDL_ADVISE
    lt_dladvise dladvise;
#endif

    /* Double check that the .la file is there that we expect; if it's
       not, skip this test. */
    fp = fopen(full_filename, "r");
    if (NULL == fp) {
        fprintf(stderr, 
                "File %s.la doesn't seem to exist; skipping this test\n",
                full_filename);
        exit(77);
    }
    /* We know the .la file is there, so read it, looking for the
       dlopen value.  If the dlopen value is '' (i.e., empty), then
       there's nothing to dlopen (i.e., OMPI was built with
       --enable-static --disable-shared, so return 77 to skip this
       test.  This is horrible, but I can't think of a better way to
       check it (since there is no good way to #define whether we have
       built statically or not...). */
    happy = 0;
    while (1) {
        if (0 == fgets(line, sizeof(line) - 1, fp)) {
            break;
        }
        if (0 == strncmp(line, "dlname=", 7)) {
            if (0 == strncmp(line + 7, "''", 2)) {
                happy = 0;
            } else {
                happy = 1;
            }
            break;
        }
    }
    fclose(fp);
    if (!happy) {
        fprintf(stderr, "No test file to dlopen (perhaps --enable-static?); skipping\n");
        exit(77);
    }

    /* Startup LT */
    if (lt_dlinit() != 0) {
        fprintf(stderr, "Failed to lt_dlinit\n");
        return 1;
    }

    printf("Trying to lt_dlopen file with dladvise_local: %s\n", filename);

#if OPAL_HAVE_LTDL_ADVISE
    if (lt_dladvise_init(&dladvise) ||
        lt_dladvise_ext(&dladvise) ||
        lt_dladvise_local(&dladvise)) {
        fprintf(stderr, "lt_dladvise failed to initialize properly\n");
        return 1;
    }
    dlhandle = lt_dlopenadvise(filename, dladvise);
    lt_dladvise_destroy(&dladvise);
#else
    dlhandle = lt_dlopenext(filename);
#endif
    if (NULL != dlhandle) {
        lt_dlclose(dlhandle);
	printf("File opened with dladvise_local, all passed\n");
        return 0;
    }

    printf("Failed to open with dladvise_local: %s\n", lt_dlerror());
    printf("Retrying with dladvise_global\n");

#if OPAL_HAVE_LTDL_ADVISE
    if (lt_dladvise_init(&dladvise) ||
        lt_dladvise_ext(&dladvise) ||
        lt_dladvise_global(&dladvise)) {
        fprintf(stderr, "lt_dladvise failed to initialize properly\n");
        return 1;
    }
    dlhandle = lt_dlopenadvise(filename, dladvise);
    lt_dladvise_destroy(&dladvise);
#else
    dlhandle = lt_dlopenext(filename);
#endif
    if (NULL != dlhandle) {
        lt_dlclose(dlhandle);
	printf("File opened with dladvise_global\n");
	return 0;
    }
    fprintf(stderr, "File failed to open with dladvise_global: %s\n", 
            lt_dlerror());

    return 2;
}
コード例 #14
0
ファイル: pkcs15-syn.c プロジェクト: securez/opendnie
static int parse_emu_block(sc_pkcs15_card_t *p15card, scconf_block *conf)
{
	sc_card_t	*card = p15card->card;
	sc_context_t	*ctx = card->ctx;
	sc_pkcs15emu_opt_t opts;
	lt_dlhandle	handle = NULL;
	int		(*init_func)(sc_pkcs15_card_t *);
	int		(*init_func_ex)(sc_pkcs15_card_t *, sc_pkcs15emu_opt_t *);
	int		r, force = 0;
	const char	*driver, *module_name;

	driver = conf->name->data;

	init_func    = NULL;
	init_func_ex = NULL;

	memset(&opts, 0, sizeof(opts));
	opts.blk     = conf;
	if (force != 0)
		opts.flags   = SC_PKCS15EMU_FLAGS_NO_CHECK;

	module_name = scconf_get_str(conf, "module", builtin_name);
	if (!strcmp(module_name, "builtin")) {
		int	i;

		/* This function is built into libopensc itself.
		 * Look it up in the table of emulators */
		module_name = driver;
		for (i = 0; builtin_emulators[i].name; i++) {
			if (!strcmp(builtin_emulators[i].name, module_name)) {
				init_func_ex = builtin_emulators[i].handler;
				break;
			}
		}
	} else {
		const char *(*get_version)(void);
		const char *name = NULL;
		void	*address;

		sc_debug(ctx, SC_LOG_DEBUG_NORMAL, "Loading %s\n", module_name);
		
		/* try to open dynamic library */
		handle = lt_dlopen(module_name);
		if (!handle) {
			sc_debug(ctx, SC_LOG_DEBUG_NORMAL, "unable to open dynamic library '%s': %s\n",
			         module_name, lt_dlerror());
			return SC_ERROR_INTERNAL;
		}
		/* try to get version of the driver/api */
		get_version =  (const char *(*)(void)) lt_dlsym(handle, "sc_driver_version");
		if (!get_version || strcmp(get_version(), "0.9.3") < 0) {
			/* no sc_driver_version function => assume old style
			 * init function (note: this should later give an error
			 */
			/* get the init function name */
			name = scconf_get_str(conf, "function", func_name);

			address = lt_dlsym(handle, name);
			if (address)
				init_func = (int (*)(sc_pkcs15_card_t *)) address;
		} else {
			name = scconf_get_str(conf, "function", exfunc_name);

			address = lt_dlsym(handle, name);
			if (address)
				init_func_ex = (int (*)(sc_pkcs15_card_t *, sc_pkcs15emu_opt_t *)) address;
		}
	}
	/* try to initialize the pkcs15 structures */
	if (init_func_ex)
		r = init_func_ex(p15card, &opts);
	else if (init_func)
		r = init_func(p15card);
	else
		r = SC_ERROR_WRONG_CARD;

	if (r >= 0) {
		sc_debug(card->ctx, SC_LOG_DEBUG_NORMAL, "%s succeeded, card bound\n",
				module_name);
		p15card->dll_handle = handle;
	} else {
		sc_debug(card->ctx, SC_LOG_DEBUG_NORMAL, "%s failed: %s\n",
				module_name, sc_strerror(r));
		/* clear pkcs15 card */
		sc_pkcs15_card_clear(p15card);
		if (handle)
			lt_dlclose(handle);
	}

	return r;
}
コード例 #15
0
void PluginManager::stop(){
  for(std::map<std::string, void*>::iterator itcurr = libs.begin(); itcurr != libs.end(); ++itcurr){
    lt_dlclose((lt_dlhandle)itcurr->second);
  }
  libs.clear();
}
コード例 #16
0
/* Open a module in current directory. */
mpg123_module_t* open_module_here(const char* type, const char* name)
{
	lt_dlhandle handle = NULL;
	mpg123_module_t *module = NULL;
	char* module_path = NULL;
	size_t module_path_len = 0;
	char* module_symbol = NULL;
	size_t module_symbol_len = 0;

	/* Initialize libltdl */
	if(lt_dlinit())
	{
		error("Failed to initialise libltdl");
		return NULL;
	}

	/* Work out the path of the module to open */
	/* Note that we need to open ./file, not just file! */
	module_path_len = 2 + strlen(type) + 1 + strlen(name) + strlen(MODULE_FILE_SUFFIX) + 1;
	module_path = malloc( module_path_len );
	if (module_path == NULL) {
		error1( "Failed to allocate memory for module name: %s", strerror(errno) );
		return NULL;
	}
	snprintf( module_path, module_path_len, "./%s_%s%s", type, name, MODULE_FILE_SUFFIX );
	/* Display the path of the module created */
	if(param.verbose > 1) fprintf(stderr, "Module path: %s\n", module_path );

	/* Open the module */
	handle = lt_dlopen( module_path );
	free( module_path );
	if (handle==NULL) {
		error2( "Failed to open module %s: %s", name, lt_dlerror() );
		if(param.verbose > 1)
		fprintf(stderr, "Note: This could be because of braindead path in the .la file...\n");

		return NULL;
	}
	
	/* Work out the symbol name */
	module_symbol_len = strlen( MODULE_SYMBOL_PREFIX ) +
						strlen( type )  +
						strlen( MODULE_SYMBOL_SUFFIX ) + 1;
	module_symbol = malloc(module_symbol_len);
	if (module_symbol == NULL) {
		error1( "Failed to allocate memory for module symbol: %s", strerror(errno) );
		return NULL;
	}
	snprintf( module_symbol, module_symbol_len, "%s%s%s", MODULE_SYMBOL_PREFIX, type, MODULE_SYMBOL_SUFFIX );
	debug1( "Module symbol: %s", module_symbol );
	
	/* Get the information structure from the module */
	module = (mpg123_module_t*)lt_dlsym(handle, module_symbol );
	free( module_symbol );
	if (module==NULL) {
		error1( "Failed to get module symbol: %s", lt_dlerror() );
		return NULL;
	}
	
	/* Check the API version */
	if (MPG123_MODULE_API_VERSION != module->api_version)
	{
		error2( "API version of module does not match (got %i, expected %i).", module->api_version, MPG123_MODULE_API_VERSION);
		lt_dlclose(handle);
		return NULL;
	}

	/* Store handle in the data structure */
	module->handle = handle;
	return module;
}
コード例 #17
0
ファイル: dl-ltdl.c プロジェクト: BackupTheBerlios/elastic
EC_API void EcDLClose( ec_dlhandle handle )
{
	lt_dlclose( (lt_dlhandle) handle );
}
コード例 #18
0
ファイル: plugin.c プロジェクト: openhpi1/testrepo
/**
 * oh_unload_plugin
 * @plugin_name
 *
 * Load plugin by name and make a instance.
 *
 * Returns: 0 on Success.
 **/
int oh_load_plugin(char *plugin_name)
{
        struct oh_plugin *plugin = NULL;
        int (*get_interface) (struct oh_abi_v2 ** pp, const uuid_t uuid);
        int err;
        struct oh_static_plugin *p = static_plugins;

        if (!plugin_name) {
                dbg("ERROR. NULL plugin name passed.");
                return -1;
        }

        data_access_lock();
        if (oh_init_ltdl()) {                
                data_access_unlock();
                dbg("ERROR. Could not initialize ltdl for loading plugins.");
                return -1;
        }
        
        if (oh_lookup_plugin(plugin_name)) {
                dbg("Warning. Plugin %s already loaded. Not loading twice.",
                    plugin_name);
                data_access_unlock();
                return -1;
        }

        plugin = (struct oh_plugin *)g_malloc0(sizeof(struct oh_plugin));
        if (!plugin) {
                dbg("Out of memory.");
                data_access_unlock();
                return -1;
        }
        plugin->name = g_strdup(plugin_name);
        plugin->refcount = 1;
        
        /* first take search plugin in the array of static plugin */
        while( p->name ) {
                if (!strcmp(plugin->name, p->name)) {
                        plugin->dl_handle = 0;
                        err = (*p->get_interface)((void **)&plugin->abi, UUID_OH_ABI_V2);

                        if (err < 0 || !plugin->abi || !plugin->abi->open) {
                                dbg("Can not get ABI V2");
                                goto err1;
                        }

                        trace( "found static plugin %s", p->name );

                        plugin_list = g_slist_append(plugin_list, plugin);
                        data_access_unlock();

                        return 0;
                }

                p++;
        }

        plugin->dl_handle = lt_dlopenext(plugin->name);
        if (plugin->dl_handle == NULL) {
                dbg("Can not open %s plugin: %s", plugin->name, lt_dlerror());
                goto err1;
        }

        get_interface = lt_dlsym(plugin->dl_handle, "get_interface");
        if (!get_interface) {
                dbg("Can not get 'get_interface' symbol, is it a plugin?!");
                goto err1;
        }

        err = get_interface(&plugin->abi, UUID_OH_ABI_V2);
        if (err < 0 || !plugin->abi || !plugin->abi->open) {
                dbg("Can not get ABI V1");
                goto err1;
        }
        plugin_list = g_slist_append(plugin_list, plugin);
        data_access_unlock();

        return 0;

 err1:
        if (plugin->dl_handle) {
                lt_dlclose(plugin->dl_handle);
                plugin->dl_handle = 0;
        }
        data_access_unlock();
        g_free(plugin);

        return -1;
}
コード例 #19
0
ファイル: load.c プロジェクト: Distrotech/mailutils
/* FIXME: See comment below */ 
static void
_free_loaded_module (void *data)
{
  lt_dlclose ((lt_dlhandle)data);
  lt_dlexit ();
}
コード例 #20
0
ファイル: open.c プロジェクト: CCI/cci
static int open_plugin(const char *filename,
		       lt_dlhandle * handle, cci_plugin_t ** plugin,
		       cci_plugins_framework_verify_fn_t verify)
{
	char *p1, *p2, struct_name[BUFSIZ];
	char *local = strdup(filename);

	if (NULL == local) {
		return CCI_ENOMEM;
	}

	/* Open the DSO */
	*handle = lt_dlopenadvise(local, cci_plugins_dladvise);
	if (NULL == *handle) {
		fprintf(stderr, "Failed to open plugin %s: %s\n",
			filename, lt_dlerror());
		free(local);
		return CCI_ERROR;
	}

	/* Make the struct symbol name */
	p1 = strrchr(local, '/');
	if (NULL == p1) {
		p1 = local;
	} else {
		++p1;
	}
	p2 = strchr(p1, '.');
	if (NULL != p2) {
		*p2 = '\0';
	}

	/* Find the symbol name */
	snprintf(struct_name, BUFSIZ - 1, "%s_plugin", p1);
	free(local);
	struct_name[BUFSIZ - 1] = '\0';
	*plugin = lt_dlsym(*handle, struct_name);
	if (NULL == *plugin) {
		fprintf(stderr,
			"Unable to find \"%s\" symbol in %s -- ignored\n",
			struct_name, filename);
		goto bad;
	}

	/* Version check */
	if ((*plugin)->cci_abi_version != CCI_ABI_VERSION) {
		fprintf(stderr,
			"Plugin \"%s\" in %s supports ABI version %d; only version %d is supported -- ignored\n",
			(*plugin)->plugin_name, filename,
			(*plugin)->cci_abi_version, CCI_ABI_VERSION);
		goto bad;
	}

	/* See if the framework likes it */
	if (NULL != verify && CCI_SUCCESS != verify(*plugin)) {
		goto bad;
	}

	/* Alles gut */
	return CCI_SUCCESS;

      bad:
	lt_dlclose(*handle);
	return CCI_ERROR;
}
コード例 #21
0
int
globus_extension_activate(
    const char *                        extension_name)
{
    globus_l_extension_module_t *       extension;
    globus_l_extension_module_t *       last_extension;
    globus_l_extension_builtin_t *      builtin;
    int                                 rc;
    globus_result_t                     result = GLOBUS_FAILURE;
    GlobusFuncName(globus_extension_activate);
    
    GlobusExtensionDebugEnterSymbol(extension_name);
    
    if(!extension_name)
    {
        goto error_param;
    }
    
    globus_rmutex_lock(&globus_l_extension_mutex);
    {
        extension = (globus_l_extension_module_t *)
            globus_hashtable_lookup(
                &globus_l_extension_loaded, (void *) extension_name);
        if(!extension)
        {
            extension = (globus_l_extension_module_t *)
                globus_malloc(sizeof(globus_l_extension_module_t));
            if(!extension)
            {
                goto error_alloc;
            }
            
            extension->module_ref = 1;
            extension->ref = 1;
            extension->name = globus_libc_strdup(extension_name);
            if(!extension->name)
            {
                goto error_strdup;
            }
            
            builtin = (globus_l_extension_builtin_t *)
                globus_hashtable_lookup(
                    &globus_l_extension_builtins, (void *) extension_name);
            if(builtin && (!builtin->owner || builtin->owner->module_ref > 0))
            {
#               if !defined(BUILD_STATIC_ONLY)
                {

                    extension->dlhandle = NULL;
                }
#               endif
                extension->module = builtin->module;
                extension->owner = builtin->owner;
                if(extension->owner)
                {
                    extension->owner->ref++;
                }
            }
            else
            {
                extension->owner = NULL;

#               if !defined(BUILD_STATIC_ONLY)
                {

                    result =   
                        globus_l_extension_dlopen(
                            extension->name,
                            &extension->dlhandle);
                    if(result != GLOBUS_SUCCESS)
                    {
                        goto error_dll;
                    }
                    
                    result =
                       globus_l_extension_get_module(
                           extension->dlhandle,
                           extension_name,
                           &extension->module);

                }
#               else
                {
                    globus_assert(BUILD_STATIC_ONLY == 0);
                    result = globus_error_put(
                        globus_error_construct_error(
                            GLOBUS_EXTENSION_MODULE,
                            NULL,
                            GLOBUS_EXTENSION_ERROR_OPEN_FAILED,
                            __FILE__,
                            _globus_func_name,
                            __LINE__,
                            "No support for dynamically loading %s\n",
                            extension->name));
                }
#               endif /* !defined(BUILD_STATIC_ONLY) */

                if(result != GLOBUS_SUCCESS)
                {
                    goto error_module;
                }
            }
            
            globus_hashtable_insert(
                &globus_l_extension_loaded,
                extension->name,
                extension);
                
            last_extension = (globus_l_extension_module_t *)
                globus_thread_getspecific(globus_l_extension_owner_key);
            globus_thread_setspecific(globus_l_extension_owner_key, extension);
            
#if USE_SYMBOL_LABELS
            {
                int pre_warned = WARNING_USING_MIXED_THREAD_MODELS;
#endif
            rc = globus_module_activate_proxy(
                extension->module,
                globus_l_extension_deactivate_proxy,
                extension);
#if USE_SYMBOL_LABELS
                if ((!pre_warned) && WARNING_USING_MIXED_THREAD_MODELS)
                {
                    GlobusExtensionDebugPrintf(
                        GLOBUS_L_EXTENSION_DEBUG_VERBOSE,
                        (_GCSL("[%s] Warning: extension %s was compiled with pthreads for GT 5.0.x and may not work correctly\n"),
                            _globus_func_name,
                            extension->name));

                }
            }
#endif
            
            globus_thread_setspecific(
                globus_l_extension_owner_key, last_extension);
            if(rc != GLOBUS_SUCCESS)
            {
                goto error_activate;
            }
        }
        else
        {
            extension->module_ref++;
            extension->ref++;
        }
    }
    globus_rmutex_unlock(&globus_l_extension_mutex);
    
    GlobusExtensionDebugExit();
    return GLOBUS_SUCCESS;

error_activate:
    globus_hashtable_remove(
        &globus_l_extension_loaded, extension->name);
    if(builtin && builtin->owner)
    {
        builtin->owner->ref--;
    }
error_module:
#ifndef BUILD_STATIC_ONLY
    if(extension->dlhandle)
    {
        lt_dlclose(extension->dlhandle);
    }
error_dll:
#endif /* !BUILD_STATIC_ONLY */
    globus_free(extension->name);
error_strdup:
    globus_free(extension);
error_alloc:
    globus_rmutex_unlock(&globus_l_extension_mutex);
error_param:
    GlobusExtensionDebugExitWithError();
    return result;
}
コード例 #22
0
ファイル: libscdl.c プロジェクト: emilianobonassi/OpenSC
int sc_dlclose(void *handle)
{
	return lt_dlclose((lt_dlhandle)handle);
}
コード例 #23
0
int
gp_abilities_list_load_dir (CameraAbilitiesList *list, const char *dir,
			    GPContext *context)
{
	CameraLibraryIdFunc id;
	CameraLibraryAbilitiesFunc ab;
	CameraText text;
	int ret, x, old_count, new_count;
	int i, p;
	const char *filename;
	CameraList *flist;
	int count;
	lt_dlhandle lh;

	C_PARAMS (list && dir);

	GP_LOG_D ("Using ltdl to load camera libraries from '%s'...", dir);
	CHECK_RESULT (gp_list_new (&flist));
	ret = gp_list_reset (flist);
	if (ret < GP_OK) {
		gp_list_free (flist);
		return ret;
	}
	if (1) { /* a new block in which we can define a temporary variable */
		foreach_data_t foreach_data = { NULL, GP_OK };
		foreach_data.list = flist;
		lt_dlinit ();
		lt_dladdsearchdir (dir);
		ret = lt_dlforeachfile (dir, foreach_func, &foreach_data);
		lt_dlexit ();
		if (ret != 0) {
			gp_list_free (flist);
			GP_LOG_E ("Internal error looking for camlibs (%d)", ret);
			gp_context_error (context,
					  _("Internal error looking for camlibs. "
					    "(path names too long?)"));
			return (foreach_data.result!=GP_OK)?foreach_data.result:GP_ERROR;
		}
	}
	count = gp_list_count (flist);
	if (count < GP_OK) {
		gp_list_free (flist);
		return ret;
	}
	GP_LOG_D ("Found %i camera drivers.", count);
	lt_dlinit ();
	p = gp_context_progress_start (context, count,
		_("Loading camera drivers from '%s'..."), dir);
	for (i = 0; i < count; i++) {
		ret = gp_list_get_name (flist, i, &filename);
		if (ret < GP_OK) {
			gp_list_free (flist);
			return ret;
		}
		lh = lt_dlopenext (filename);
		if (!lh) {
			GP_LOG_D ("Failed to load '%s': %s.", filename,
				lt_dlerror ());
			continue;
		}

		/* camera_id */
		id = lt_dlsym (lh, "camera_id");
		if (!id) {
			GP_LOG_D ("Library '%s' does not seem to "
				"contain a camera_id function: %s",
				filename, lt_dlerror ());
			lt_dlclose (lh);
			continue;
		}

		/*
		 * Make sure the camera driver hasn't been
		 * loaded yet.
		 */
		if (id (&text) != GP_OK) {
			lt_dlclose (lh);
			continue;
		}
		if (gp_abilities_list_lookup_id (list, text.text) >= 0) {
			lt_dlclose (lh);
			continue;
		} 

		/* camera_abilities */
		ab = lt_dlsym (lh, "camera_abilities");
		if (!ab) {
			GP_LOG_D ("Library '%s' does not seem to "
				"contain a camera_abilities function: "
				"%s", filename, lt_dlerror ());
			lt_dlclose (lh);
			continue;
		}

		old_count = gp_abilities_list_count (list);
		if (old_count < 0) {
			lt_dlclose (lh);
			continue;
		}

		if (ab (list) != GP_OK) {
			lt_dlclose (lh);
			continue;
		}

		/* do not free the library in valgrind mode */
#if !defined(VALGRIND) 
		lt_dlclose (lh);
#endif

		new_count = gp_abilities_list_count (list);
		if (new_count < 0)
			continue;

		/* Copy in the core-specific information */
		for (x = old_count; x < new_count; x++) {
			strcpy (list->abilities[x].id, text.text);
			strcpy (list->abilities[x].library, filename);
		}

		gp_context_progress_update (context, p, i);
		if (gp_context_cancel (context) == GP_CONTEXT_FEEDBACK_CANCEL) {
			lt_dlexit ();
			gp_list_free (flist);
			return (GP_ERROR_CANCEL); 
		}
	}
	gp_context_progress_stop (context, p);
	lt_dlexit ();
	gp_list_free (flist);

	return (GP_OK);
}
コード例 #24
0
void
DynamicLoaderModule::LoadModuleFunction::execute( SLIInterpreter* i ) const
{
  i->assert_stack_load( 1 );

  sDynModule new_module;

  new_module.name = getValue< std::string >( i->OStack.top() );
  if ( new_module.name.empty() )
    throw DynamicModuleManagementError( "Module name must not be empty." );

  // check if module already loaded
  // this check can happen here, since we are comparing dynamically loaded modules
  // based on the name given to the Install command
  if ( std::find( dyn_modules_.begin(), dyn_modules_.end(), new_module ) != dyn_modules_.end() )
    throw DynamicModuleManagementError( "Module '" + new_module.name + "' is loaded already." );

  // call lt_dlerror() to reset any error messages hanging around
  lt_dlerror();

  // try to open the module
  const lt_dlhandle hModule = lt_dlopenext( new_module.name.c_str() );

  if ( !hModule )
  {
    char* errstr = ( char* ) lt_dlerror();
    std::string msg = "Module '" + new_module.name + "' could not be opened.";
    if ( errstr )
      msg += "\nThe dynamic loader returned the following error: '" + std::string( errstr ) + "'.";
    msg += "\n\nPlease check LD_LIBRARY_PATH (OSX: DYLD_LIBRARY_PATH)!";
    throw DynamicModuleManagementError( msg );
  }

  // see if we can find the mod symbol in the module
  SLIModule* pModule = ( SLIModule* ) lt_dlsym( hModule, "mod" );
  char* errstr = ( char* ) lt_dlerror();
  if ( errstr )
  {
    lt_dlclose( hModule ); // close module again
    lt_dlerror();          // remove any error caused by lt_dlclose()
    throw DynamicModuleManagementError(
            "Module '" + new_module.name + "' could not be loaded.\n"
            "The dynamic loader returned the following error: '" 
            + std::string(errstr) + "'.");
  }

  // check if module is linked in. This test is based on the module name
  // returned by DynModule::name(), since we have no file names for linked modules.
  // We can only perform it after we have loaded the module.
  if ( std::find_if( DynamicLoaderModule::getLinkedModules().begin(),
         DynamicLoaderModule::getLinkedModules().end(),
         std::bind2nd( std::ptr_fun( has_name ), pModule->name() ) )
    != DynamicLoaderModule::getLinkedModules().end() )
  {
    lt_dlclose( hModule ); // close module again
    lt_dlerror();          // remove any error caused by lt_dlclose()
    throw DynamicModuleManagementError(
            "Module '" + new_module.name + "' is linked into NEST.\n"
            "You neither need nor may load it dynamically in addition.");
  }

  // all is well an we can register the module with the interpreter
  try
  {
    pModule->install( std::cerr, i );
  }
  catch ( std::exception& e )
  {
    // We should uninstall the partially installed module here, but
    // this must wait for #152.
    // For now, we just close the module file and rethrow the exception.

    lt_dlclose( hModule );
    lt_dlerror(); // remove any error caused by lt_dlclose()
    throw;        // no arg re-throws entire exception, see Stroustrup 14.3.1
  }

  // add the handle to list of loaded modules
  new_module.handle = hModule;
  new_module.pModule = pModule;
  dyn_modules_.push_back( new_module );

  i->message( SLIInterpreter::M_INFO, "Install", ( "loaded module " + pModule->name() ).c_str() );

  // remove operand and operator from stack
  i->OStack.pop();
  i->EStack.pop();

  // put handle to module onto stack
  int moduleid = dyn_modules_.size() - 1;
  i->OStack.push( moduleid );
  ( *moduledict_ )[ new_module.name ] = moduleid;

  // now we can run the module initializer, after we have cleared the EStack
  if ( !pModule->commandstring().empty() )
  {
    Token t = new StringDatum( pModule->commandstring() );
    i->OStack.push_move( t );
    Token c = new NameDatum( "initialize_module" );
    i->EStack.push_move( c );
  }
}
コード例 #25
0
ファイル: ud-module.c プロジェクト: evelynmitchell/unserding
void
ud_mod_close(ud_mod_t m)
{
	lt_dlclose(m);
	return;
}