static int idesc_file_ops_stat(struct idesc *idesc, void *buf) {
	assert(idesc);

	return kfstat((struct file_desc *)idesc, buf);
}
Example #2
0
struct loaderplugin_t* pluginloader_loadPluginFromFile(struct pluginloader_t* loader, char* pluginPath)
{
	if (!loader || !pluginPath)
		return NULL;

	int fd = kopen(pluginPath, O_RDONLY, 0);
	if (fd < 0)
	{
		WriteLog(LL_Error, "could not load plugin (%s).", pluginPath);
		return NULL;
	}

	struct stat statbuf;
	kmemset(&statbuf, 0, sizeof(statbuf));

	// Get the file size
	int res = kfstat(fd, &statbuf);
	if (res < 0)
	{
		WriteLog(LL_Error, "could not get %d handle stat: %d", fd, res);
		kclose(fd);
		return NULL;
	}

	// Verify that our plugin is under the size limit
	size_t pluginSize = statbuf.st_size;
	if (pluginSize > PLUGIN_MAXSIZE)
	{
		WriteLog(LL_Warn, "plugin (%s) is too large, skipping.", pluginPath);
		kclose(fd);
		return NULL;
	}

	uint8_t* pluginData = (uint8_t*)kmalloc(pluginSize);
	if (!pluginData)
	{
		WriteLog(LL_Error, "could not allocate space for plugin (%s) (size: %lld)", pluginPath, pluginSize);
		kclose(fd);
		return NULL;
	}
	kmemset(pluginData, 0, pluginSize);

	// Read out our plugin data
	klseek(fd, 0, SEEK_SET);
	kread(fd, pluginData, pluginSize);
	kclose(fd);

	// Allocate a new entry for our list
	struct loaderplugin_t* entry = (struct loaderplugin_t*)kmalloc(sizeof(struct loaderplugin_t));
	if (!entry)
	{
		WriteLog(LL_Error, "could not allocate new entry");
		kfree(pluginData, pluginSize);
		return NULL;
	}

	entry->data = pluginData;
	entry->dataLength = pluginSize;
	entry->plugin = NULL; // do not create the plugin here
	
	size_t pathLength = strlen(pluginPath);
	if (pathLength > sizeof(entry->path))
		WriteLog(LL_Warn, "path is too long, not setting");
	else
		kmemcpy(entry->path, pluginPath, pathLength);

	return entry;
}